also looking at this
fix(ios): hand the page the answers native was already sending
#222
18 files
+597
-164
| @@ -644,6 +644,41 @@ test "the gate table has no entry for an action Zig does not serve" { | ||
| 644 | 644 | } |
| 645 | 645 | } |
| 646 | 646 | |
| 647 | /// The `capability_actions` array of one mobile module, or null when it has | |
| 648 | /// none. | |
| 649 | /// | |
| 650 | /// Bounded to the array, not the whole file: prose quotes declarations — | |
| 651 | /// `bridge_mobile_haptics.zig`'s header describes one it removed — and a scan | |
| 652 | /// that read comments would attribute that sentence to whichever entry | |
| 653 | /// preceded it. | |
| 654 | fn manifestTable(source: []const u8) ?[]const u8 { | |
| 655 | const table_start = std.mem.indexOf(u8, source, "pub const capability_actions = [_]capabilities.ActionDecl{") orelse | |
| 656 | return null; | |
| 657 | const table_end = std.mem.indexOfPos(u8, source, table_start, "\n};") orelse return null; | |
| 658 | return source[table_start..table_end]; | |
| 659 | } | |
| 660 | ||
| 661 | /// The action named by the manifest entry that contains offset `at`. | |
| 662 | /// | |
| 663 | /// The entry names its action as `A.some_const`; this resolves it back through | |
| 664 | /// the `A` block to the string the spec spells. | |
| 665 | fn declaredActionBefore(source: []const u8, table: []const u8, at: usize) ?[]const u8 { | |
| 666 | const name_at = std.mem.lastIndexOf(u8, table[0..at], ".name = A.") orelse return null; | |
| 667 | const const_start = name_at + ".name = A.".len; | |
| 668 | var const_end = const_start; | |
| 669 | while (const_end < table.len and (std.ascii.isAlphanumeric(table[const_end]) or | |
| 670 | table[const_end] == '_')) : (const_end += 1) | |
| 671 | {} | |
| 672 | ||
| 673 | var needle_buf: [96]u8 = undefined; | |
| 674 | const member = table[const_start..const_end]; | |
| 675 | const decl = std.fmt.bufPrint(&needle_buf, "const {s} = \"", .{member}) catch return null; | |
| 676 | const decl_at = std.mem.indexOf(u8, source, decl) orelse return null; | |
| 677 | const value_start = decl_at + decl.len; | |
| 678 | const value_end = std.mem.indexOfScalarPos(u8, source, value_start, '"') orelse return null; | |
| 679 | return source[value_start..value_end]; | |
| 680 | } | |
| 681 | ||
| 647 | 682 | /// The `.reason` text of every `.status = .unavailable` entry, by action. |
| 648 | 683 | /// |
| 649 | 684 | /// The reason is what a reader trusts, so it is what has to be checked. A |
| @@ -656,36 +691,12 @@ fn collectUnavailableReasons(allocator: std.mem.Allocator) !std.StringHashMap([] | ||
| 656 | 691 | errdefer map.deinit(); |
| 657 | 692 | |
| 658 | 693 | for (zig_sources) |source| { |
| 659 | // Bounded to the manifest array, not the whole file: prose quotes | |
| 660 | // `.status = .unavailable` — `bridge_mobile_haptics.zig`'s header does, | |
| 661 | // describing the declaration it removed — and a scan that read comments | |
| 662 | // would attribute that sentence to whichever entry preceded it. | |
| 663 | const table_start = std.mem.indexOf(u8, source, "pub const capability_actions = [_]capabilities.ActionDecl{") orelse | |
| 664 | continue; | |
| 665 | const table_end = std.mem.indexOfPos(u8, source, table_start, "\n};") orelse continue; | |
| 666 | const table = source[table_start..table_end]; | |
| 694 | const table = manifestTable(source) orelse continue; | |
| 667 | 695 | |
| 668 | 696 | var search: usize = 0; |
| 669 | 697 | while (std.mem.indexOfPos(u8, table, search, ".status = .unavailable")) |at| { |
| 670 | 698 | search = at + 1; |
| 671 | ||
| 672 | // The entry names its action as `A.some_const`; resolve it back | |
| 673 | // through the `A` block to the string the spec spells. | |
| 674 | const name_at = std.mem.lastIndexOf(u8, table[0..at], ".name = A.") orelse continue; | |
| 675 | const const_start = name_at + ".name = A.".len; | |
| 676 | var const_end = const_start; | |
| 677 | while (const_end < table.len and (std.ascii.isAlphanumeric(table[const_end]) or | |
| 678 | table[const_end] == '_')) : (const_end += 1) | |
| 679 | {} | |
| 680 | ||
| 681 | var needle_buf: [96]u8 = undefined; | |
| 682 | const member = table[const_start..const_end]; | |
| 683 | if (member.len + 16 > needle_buf.len) continue; | |
| 684 | const decl = try std.fmt.bufPrint(&needle_buf, "const {s} = \"", .{member}); | |
| 685 | const decl_at = std.mem.indexOf(u8, source, decl) orelse continue; | |
| 686 | const value_start = decl_at + decl.len; | |
| 687 | const value_end = std.mem.indexOfScalarPos(u8, source, value_start, '"') orelse continue; | |
| 688 | const action = source[value_start..value_end]; | |
| 699 | const action = declaredActionBefore(source, table, at) orelse continue; | |
| 689 | 700 | |
| 690 | 701 | // The reason, inline or by name. |
| 691 | 702 | const reason_at = std.mem.indexOfPos(u8, table, at, ".reason = ") orelse continue; |
| @@ -1656,18 +1667,16 @@ test "the SDK's event map declares everything that does fire" { | ||
| 1656 | 1667 | // `share` was one (#203). Its case resolves whether the person shared and |
| 1657 | 1668 | // rejects `CAPABILITY_DISABLED` and `INVALID_ARGUMENT`, and `craft.share(text)` |
| 1658 | 1669 | // posted `{action: 'share', text}`, so an app with sharing switched off called |
| 1659 | // it, heard nothing and carried on as if it had worked. | |
| 1660 | ||
| 1661 | /// Actions the page still posts without a callbackId even though the spec | |
| 1662 | /// answers them. Tracked in #207. This list may only shrink: the second test | |
| 1663 | /// below fails when one of them is fixed and left here. | |
| 1664 | const unanswered_posts = [_][]const u8{ | |
| 1665 | "haptic", | |
| 1666 | "startListening", | |
| 1667 | "watchPosition", | |
| 1668 | "clearWatch", | |
| 1669 | "vibrate", | |
| 1670 | }; | |
| 1670 | // it, heard nothing and carried on as if it had worked. `haptic`, | |
| 1671 | // `startListening`, `watchPosition`, `clearWatch` and `vibrate` were five more | |
| 1672 | // (#207). | |
| 1673 | // | |
| 1674 | // Fixing those by adding a callbackId turns the bug around rather than | |
| 1675 | // removing it: `haptic` and `startListening` only ever *rejected*, so a page | |
| 1676 | // that waited on them would have waited for ever on every call that worked. | |
| 1677 | // So the checks run both ways. A call the spec answers must carry an id, a | |
| 1678 | // call that carries an id must have a case that can resolve it, and no Zig | |
| 1679 | // module may declare a reply the page is waiting for as `.none`. | |
| 1671 | 1680 | |
| 1672 | 1681 | /// One `postMessage(...)` call in the page script. |
| 1673 | 1682 | const Post = struct { |
| @@ -1730,13 +1739,45 @@ const PostIterator = struct { | ||
| 1730 | 1739 | } |
| 1731 | 1740 | }; |
| 1732 | 1741 | |
| 1733 | /// Whether the dispatcher's `case` for `action` answers through the callback. | |
| 1742 | /// Every action the page waits on an answer for: literal posts that carry a | |
| 1743 | /// callbackId, and every `_invoke('name'`, which always adds one. | |
| 1744 | fn collectAwaitedActions(allocator: std.mem.Allocator) !std.StringHashMap(void) { | |
| 1745 | var set = std.StringHashMap(void).init(allocator); | |
| 1746 | errdefer set.deinit(); | |
| 1747 | ||
| 1748 | var it = PostIterator{ .source = swift_spec }; | |
| 1749 | while (it.next()) |post| { | |
| 1750 | const action = post.action orelse continue; | |
| 1751 | if (post.carries_callback) try set.put(action, {}); | |
| 1752 | } | |
| 1753 | ||
| 1754 | const needle = "_invoke('"; | |
| 1755 | var search: usize = 0; | |
| 1756 | while (std.mem.indexOfPos(u8, swift_spec, search, needle)) |at| { | |
| 1757 | const name_start = at + needle.len; | |
| 1758 | const name_end = std.mem.indexOfScalarPos(u8, swift_spec, name_start, '\'') orelse break; | |
| 1759 | try set.put(swift_spec[name_start..name_end], {}); | |
| 1760 | search = name_end; | |
| 1761 | } | |
| 1762 | return set; | |
| 1763 | } | |
| 1764 | ||
| 1765 | /// What a `case` has to do with its callbackId to count. | |
| 1766 | const CaseUse = enum { | |
| 1767 | /// Anything: resolve, reject, or hand it to a helper that will. | |
| 1768 | answers, | |
| 1769 | /// Something other than `rejectCallback`, so a call that succeeds settles. | |
| 1770 | resolves, | |
| 1771 | }; | |
| 1772 | ||
| 1773 | /// Whether the dispatcher's `case` for `action` uses the callback the way | |
| 1774 | /// `use` asks. | |
| 1734 | 1775 | /// |
| 1735 | 1776 | /// The case runs from its label to the next line at the same indentation that |
| 1736 | 1777 | /// starts another case, a `default:` or the closing brace. Nested switches sit |
| 1737 | 1778 | /// deeper and stay inside it. Comment lines are skipped so a remark that |
| 1738 | 1779 | /// mentions callbackId cannot pass for code that uses it. |
| 1739 | fn specAnswers(region: []const u8, action: []const u8) bool { | |
| 1780 | fn caseUsesCallback(region: []const u8, action: []const u8, use: CaseUse) bool { | |
| 1740 | 1781 | var lines = std.mem.splitScalar(u8, region, '\n'); |
| 1741 | 1782 | var case_indent: ?usize = null; |
| 1742 | 1783 | while (lines.next()) |line| { |
| @@ -1749,8 +1790,9 @@ fn specAnswers(region: []const u8, action: []const u8) bool { | ||
| 1749 | 1790 | std.mem.startsWith(u8, body, "default") or body[0] == '}'); |
| 1750 | 1791 | if (closes) return false; |
| 1751 | 1792 | if (std.mem.startsWith(u8, body, "//")) continue; |
| 1752 | if (std.mem.indexOf(u8, body, "callbackId") != null) return true; | |
| 1753 | continue; | |
| 1793 | if (std.mem.indexOf(u8, body, "callbackId") == null) continue; | |
| 1794 | if (use == .resolves and std.mem.indexOf(u8, body, "rejectCallback(") != null) continue; | |
| 1795 | return true; | |
| 1754 | 1796 | } |
| 1755 | 1797 | |
| 1756 | 1798 | if (std.mem.startsWith(u8, body, "case ") and labels(body, action)) case_indent = indent; |
| @@ -1758,6 +1800,14 @@ fn specAnswers(region: []const u8, action: []const u8) bool { | ||
| 1758 | 1800 | return false; |
| 1759 | 1801 | } |
| 1760 | 1802 | |
| 1803 | fn specAnswers(region: []const u8, action: []const u8) bool { | |
| 1804 | return caseUsesCallback(region, action, .answers); | |
| 1805 | } | |
| 1806 | ||
| 1807 | fn specResolves(region: []const u8, action: []const u8) bool { | |
| 1808 | return caseUsesCallback(region, action, .resolves); | |
| 1809 | } | |
| 1810 | ||
| 1761 | 1811 | /// Whether a `case` line lists `action` among its labels. |
| 1762 | 1812 | fn labels(case_line: []const u8, action: []const u8) bool { |
| 1763 | 1813 | var search: usize = 0; |
| @@ -1769,13 +1819,6 @@ fn labels(case_line: []const u8, action: []const u8) bool { | ||
| 1769 | 1819 | return false; |
| 1770 | 1820 | } |
| 1771 | 1821 | |
| 1772 | fn isRecordedUnanswered(action: []const u8) bool { | |
| 1773 | for (unanswered_posts) |recorded| { | |
| 1774 | if (std.mem.eql(u8, recorded, action)) return true; | |
| 1775 | } | |
| 1776 | return false; | |
| 1777 | } | |
| 1778 | ||
| 1779 | 1822 | test "the post scan tells a call that waits from one that does not" { |
| 1780 | 1823 | // Shown both answers before it is trusted with the real file. A scanner |
| 1781 | 1824 | // that finds nothing proves nothing. |
| @@ -1818,6 +1861,23 @@ test "the post scan tells a call that waits from one that does not" { | ||
| 1818 | 1861 | try testing.expect(!specAnswers(dispatcher, "stopListening")); |
| 1819 | 1862 | try testing.expect(!specAnswers(dispatcher, "log")); |
| 1820 | 1863 | try testing.expect(!specAnswers(dispatcher, "hap")); |
| 1864 | ||
| 1865 | // The shape #207 would have left behind: a case that answers, but only | |
| 1866 | // ever with a rejection. | |
| 1867 | try testing.expect(!specResolves(dispatcher, "haptic")); | |
| 1868 | const resolving = | |
| 1869 | \\ case "vibrate": | |
| 1870 | \\ pendingCallbackId = callbackId | |
| 1871 | \\ case "haptic": | |
| 1872 | \\ if enabled { | |
| 1873 | \\ resolveCallback(callbackId, result: true) | |
| 1874 | \\ } else { | |
| 1875 | \\ rejectCallback(callbackId, error: "Haptics is disabled") | |
| 1876 | \\ } | |
| 1877 | \\ } | |
| 1878 | ; | |
| 1879 | try testing.expect(specResolves(resolving, "vibrate")); | |
| 1880 | try testing.expect(specResolves(resolving, "haptic")); | |
| 1821 | 1881 | } |
| 1822 | 1882 | |
| 1823 | 1883 | test "every call the spec answers carries a callbackId to answer it on" { |
| @@ -1829,7 +1889,6 @@ test "every call the spec answers carries a callbackId to answer it on" { | ||
| 1829 | 1889 | literal_posts += 1; |
| 1830 | 1890 | if (post.carries_callback) continue; |
| 1831 | 1891 | if (!specAnswers(region, action)) continue; |
| 1832 | if (isRecordedUnanswered(action)) continue; | |
| 1833 | 1892 | |
| 1834 | 1893 | std.debug.print( |
| 1835 | 1894 | "the page posts `{s}` with no callbackId, and the spec answers it.\n" ++ |
| @@ -1844,31 +1903,56 @@ test "every call the spec answers carries a callbackId to answer it on" { | ||
| 1844 | 1903 | try testing.expect(literal_posts >= 40); |
| 1845 | 1904 | } |
| 1846 | 1905 | |
| 1847 | test "every recorded unanswered post is real, and still unanswered" { | |
| 1906 | test "every call the page waits on has a case that can resolve it" { | |
| 1848 | 1907 | const region = dispatcherRegion(); |
| 1849 | for (unanswered_posts) |action| { | |
| 1850 | if (!specAnswers(region, action)) { | |
| 1851 | std.debug.print( | |
| 1852 | "`{s}` is recorded as unanswered, but the spec no longer answers it.\n" ++ | |
| 1853 | " Take it off unanswered_posts.\n", | |
| 1854 | .{action}, | |
| 1855 | ); | |
| 1856 | return error.RecordedPostIsNotAnswered; | |
| 1857 | } | |
| 1908 | var awaited = try collectAwaitedActions(testing.allocator); | |
| 1909 | defer awaited.deinit(); | |
| 1910 | // Non-vacuity: every `_invoke` and callback post counts, and there are | |
| 1911 | // far more than this. | |
| 1912 | try testing.expect(awaited.count() >= 80); | |
| 1913 | ||
| 1914 | var it = awaited.keyIterator(); | |
| 1915 | while (it.next()) |action| { | |
| 1916 | if (specResolves(region, action.*)) continue; | |
| 1917 | std.debug.print( | |
| 1918 | "the page waits on `{s}`, and the spec's case never resolves it.\n" ++ | |
| 1919 | " If the case only rejects (or has no case at all), every call that\n" ++ | |
| 1920 | " works leaves the page's promise open. Resolve it once the work is\n" ++ | |
| 1921 | " done, or once the request is taken if the outcome comes later.\n", | |
| 1922 | .{action.*}, | |
| 1923 | ); | |
| 1924 | return error.PageWaitsOnAnAnswerThatNeverComes; | |
| 1925 | } | |
| 1926 | } | |
| 1858 | 1927 | |
| 1859 | var it = PostIterator{ .source = swift_spec }; | |
| 1860 | const still_dropped = while (it.next()) |post| { | |
| 1861 | const name = post.action orelse continue; | |
| 1862 | if (std.mem.eql(u8, name, action) and !post.carries_callback) break true; | |
| 1863 | } else false; | |
| 1928 | test "no Zig module declares a reply the page waits on as none" { | |
| 1929 | // `.reply = .none` is a promise from the module that nothing is sent on | |
| 1930 | // success. For an action the page awaits, that is the same hang as a Swift | |
| 1931 | // case that only rejects, in the half that serves the call first. | |
| 1932 | var awaited = try collectAwaitedActions(testing.allocator); | |
| 1933 | defer awaited.deinit(); | |
| 1934 | ||
| 1935 | var declarations: usize = 0; | |
| 1936 | for (zig_sources) |source| { | |
| 1937 | const table = manifestTable(source) orelse continue; | |
| 1938 | var search: usize = 0; | |
| 1939 | while (std.mem.indexOfPos(u8, table, search, ".name = A.")) |at| { | |
| 1940 | search = at + 1; | |
| 1941 | declarations += 1; | |
| 1942 | } | |
| 1864 | 1943 | |
| 1865 | if (!still_dropped) { | |
| 1944 | search = 0; | |
| 1945 | while (std.mem.indexOfPos(u8, table, search, ".reply = .none")) |at| { | |
| 1946 | search = at + 1; | |
| 1947 | const action = declaredActionBefore(source, table, at) orelse return error.ManifestEntryUnreadable; | |
| 1948 | if (!awaited.contains(action)) continue; | |
| 1866 | 1949 | std.debug.print( |
| 1867 | "every post of `{s}` carries a callbackId now.\n" ++ | |
| 1868 | " Take it off unanswered_posts so it cannot regress unnoticed.\n", | |
| 1950 | "`{s}` is declared `.reply = .none`, and the page waits on its answer.\n" ++ | |
| 1951 | " Send the result from the handler and declare `.result`.\n", | |
| 1869 | 1952 | .{action}, |
| 1870 | 1953 | ); |
| 1871 | return error.RecordedPostIsFixed; | |
| 1954 | return error.ZigLeavesAnAwaitedCallUnanswered; | |
| 1872 | 1955 | } |
| 1873 | 1956 | } |
| 1957 | try testing.expect(declarations >= 80); | |
| 1874 | 1958 | } |