ReviewOS

also looking at this

craft-native/craft

fix(ios): hand the page the answers native was already sending

#222
Merged glennmichael123 wants to merge fix/ios-unanswered-posts into main
18 files +597 -164
packages/zig/test/ios_conformance_test.zigmodified+153-69
Changes to packages/zig/test/ios_conformance_test.zig
@@ -644,6 +644,41 @@ test "the gate table has no entry for an action Zig does not serve" {
644644 }
645645}
646646
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.
654fn 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.
665fn 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
647682/// The `.reason` text of every `.status = .unavailable` entry, by action.
648683///
649684/// 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([]
656691 errdefer map.deinit();
657692
658693 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;
667695
668696 var search: usize = 0;
669697 while (std.mem.indexOfPos(u8, table, search, ".status = .unavailable")) |at| {
670698 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;
689700
690701 // The reason, inline or by name.
691702 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" {
16561667// `share` was one (#203). Its case resolves whether the person shared and
16571668// rejects `CAPABILITY_DISABLED` and `INVALID_ARGUMENT`, and `craft.share(text)`
16581669// 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.
1664const 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`.
16711680
16721681/// One `postMessage(...)` call in the page script.
16731682const Post = struct {
@@ -1730,13 +1739,45 @@ const PostIterator = struct {
17301739 }
17311740};
17321741
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.
1744fn 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.
1766const 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.
17341775///
17351776/// The case runs from its label to the next line at the same indentation that
17361777/// starts another case, a `default:` or the closing brace. Nested switches sit
17371778/// deeper and stay inside it. Comment lines are skipped so a remark that
17381779/// mentions callbackId cannot pass for code that uses it.
1739fn specAnswers(region: []const u8, action: []const u8) bool {
1780fn caseUsesCallback(region: []const u8, action: []const u8, use: CaseUse) bool {
17401781 var lines = std.mem.splitScalar(u8, region, '\n');
17411782 var case_indent: ?usize = null;
17421783 while (lines.next()) |line| {
@@ -1749,8 +1790,9 @@ fn specAnswers(region: []const u8, action: []const u8) bool {
17491790 std.mem.startsWith(u8, body, "default") or body[0] == '}');
17501791 if (closes) return false;
17511792 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;
17541796 }
17551797
17561798 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 {
17581800 return false;
17591801}
17601802
1803fn specAnswers(region: []const u8, action: []const u8) bool {
1804 return caseUsesCallback(region, action, .answers);
1805}
1806
1807fn specResolves(region: []const u8, action: []const u8) bool {
1808 return caseUsesCallback(region, action, .resolves);
1809}
1810
17611811/// Whether a `case` line lists `action` among its labels.
17621812fn labels(case_line: []const u8, action: []const u8) bool {
17631813 var search: usize = 0;
@@ -1769,13 +1819,6 @@ fn labels(case_line: []const u8, action: []const u8) bool {
17691819 return false;
17701820}
17711821
1772fn 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
17791822test "the post scan tells a call that waits from one that does not" {
17801823 // Shown both answers before it is trusted with the real file. A scanner
17811824 // that finds nothing proves nothing.
@@ -1818,6 +1861,23 @@ test "the post scan tells a call that waits from one that does not" {
18181861 try testing.expect(!specAnswers(dispatcher, "stopListening"));
18191862 try testing.expect(!specAnswers(dispatcher, "log"));
18201863 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"));
18211881}
18221882
18231883test "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" {
18291889 literal_posts += 1;
18301890 if (post.carries_callback) continue;
18311891 if (!specAnswers(region, action)) continue;
1832 if (isRecordedUnanswered(action)) continue;
18331892
18341893 std.debug.print(
18351894 "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" {
18441903 try testing.expect(literal_posts >= 40);
18451904}
18461905
1847test "every recorded unanswered post is real, and still unanswered" {
1906test "every call the page waits on has a case that can resolve it" {
18481907 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}
18581927
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;
1928test "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 }
18641943
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;
18661949 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",
18691952 .{action},
18701953 );
1871 return error.RecordedPostIsFixed;
1954 return error.ZigLeavesAnAwaitedCallUnanswered;
18721955 }
18731956 }
1957 try testing.expect(declarations >= 80);
18741958}