also looking at this
Zig-native mobile: iOS compiles, serves a bridge call on a simulator, and is gated against drift
#83
28 files
+2460
-6557
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -2222,41 +2222,33 @@ struct CraftWebView: UIViewRepresentable { | ||
| 2222 | 2222 | _progressCallbacks: [], |
| 2223 | 2223 | _statusCallbacks: [], |
| 2224 | 2224 | |
| 2225 | // OTA is not implemented natively. These five used to post | |
| 2226 | // to actions the switch below does not handle, and register | |
| 2227 | // a resolve/reject pair that nothing would ever call — they | |
| 2228 | // also bypassed _createCallback, which is what owns the 30s | |
| 2229 | // timeout, so the promises hung forever rather than | |
| 2230 | // rejecting. An immediate rejection is the honest answer: | |
| 2231 | // an app can catch it, where it could never catch a hang. | |
| 2232 | _unavailable: function(name) { | |
| 2233 | return Promise.reject(new Error( | |
| 2234 | 'craft.ota.' + name + ' is not implemented on this platform' | |
| 2235 | )); | |
| 2236 | }, | |
| 2225 | 2237 | configure: function(options) { |
| 2226 | 2238 | this._config = options; |
| 2227 | window.webkit.messageHandlers.craft.postMessage({action: 'otaConfigure', config: options}); | |
| 2239 | console.warn('[craft] ota.configure stored locally; OTA is not implemented natively'); | |
| 2228 | 2240 | }, |
| 2229 | 2241 | checkForUpdate: function() { |
| 2230 | var self = window.craft; | |
| 2231 | var id = 'cb_' + (++self._callbackId); | |
| 2232 | window.webkit.messageHandlers.craft.postMessage({action: 'otaCheckForUpdate', callbackId: id}); | |
| 2233 | return new Promise(function(resolve, reject) { | |
| 2234 | self._callbacks[id] = {resolve: resolve, reject: reject}; | |
| 2235 | }); | |
| 2242 | return window.craft.ota._unavailable('checkForUpdate'); | |
| 2236 | 2243 | }, |
| 2237 | 2244 | downloadUpdate: function(options) { |
| 2238 | var self = window.craft; | |
| 2239 | var id = 'cb_' + (++self._callbackId); | |
| 2240 | window.webkit.messageHandlers.craft.postMessage({action: 'otaDownloadUpdate', options: options || {}, callbackId: id}); | |
| 2241 | return new Promise(function(resolve, reject) { | |
| 2242 | self._callbacks[id] = {resolve: resolve, reject: reject}; | |
| 2243 | }); | |
| 2245 | return window.craft.ota._unavailable('downloadUpdate'); | |
| 2244 | 2246 | }, |
| 2245 | 2247 | applyUpdate: function() { |
| 2246 | var self = window.craft; | |
| 2247 | var id = 'cb_' + (++self._callbackId); | |
| 2248 | window.webkit.messageHandlers.craft.postMessage({action: 'otaApplyUpdate', callbackId: id}); | |
| 2249 | return new Promise(function(resolve, reject) { | |
| 2250 | self._callbacks[id] = {resolve: resolve, reject: reject}; | |
| 2251 | }); | |
| 2248 | return window.craft.ota._unavailable('applyUpdate'); | |
| 2252 | 2249 | }, |
| 2253 | 2250 | rollback: function() { |
| 2254 | var self = window.craft; | |
| 2255 | var id = 'cb_' + (++self._callbackId); | |
| 2256 | window.webkit.messageHandlers.craft.postMessage({action: 'otaRollback', callbackId: id}); | |
| 2257 | return new Promise(function(resolve, reject) { | |
| 2258 | self._callbacks[id] = {resolve: resolve, reject: reject}; | |
| 2259 | }); | |
| 2251 | return window.craft.ota._unavailable('rollback'); | |
| 2260 | 2252 | }, |
| 2261 | 2253 | getCurrentBundle: function() { |
| 2262 | 2254 | // This returns synchronously from stored data |
| @@ -341,6 +341,23 @@ pub fn build(b: *std.Build) void { | ||
| 341 | 341 | ios_surface_tests.root_module.linkFramework("CoreMIDI", .{}); |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | // The iOS conformance gate. It embeds the Swift template as the migration | |
| 345 | // spec and `bridge_mobile.zig` as what Zig currently serves, so an action | |
| 346 | // cannot be dropped on the way across without the build saying so. | |
| 347 | const ios_conformance_tests = b.addTest(.{ | |
| 348 | .root_module = b.createModule(.{ | |
| 349 | .root_source_file = b.path("test/ios_conformance_test.zig"), | |
| 350 | .target = target, | |
| 351 | .optimize = optimize, | |
| 352 | }), | |
| 353 | }); | |
| 354 | ios_conformance_tests.root_module.addAnonymousImport("CraftApp.swift", .{ | |
| 355 | .root_source_file = b.path("../ios/templates/CraftApp.swift"), | |
| 356 | }); | |
| 357 | ios_conformance_tests.root_module.addAnonymousImport("src/bridge_mobile.zig", .{ | |
| 358 | .root_source_file = b.path("src/bridge_mobile.zig"), | |
| 359 | }); | |
| 360 | ||
| 344 | 361 | const menubar_tests = b.addTest(.{ |
| 345 | 362 | .root_module = b.createModule(.{ |
| 346 | 363 | .root_source_file = b.path("test/menubar_test.zig"), |
| @@ -1186,6 +1203,7 @@ pub fn build(b: *std.Build) void { | ||
| 1186 | 1203 | const run_api_tests = b.addRunArtifact(api_tests); |
| 1187 | 1204 | const run_mobile_tests = b.addRunArtifact(mobile_tests); |
| 1188 | 1205 | const run_ios_surface_tests = b.addRunArtifact(ios_surface_tests); |
| 1206 | const run_ios_conformance_tests = b.addRunArtifact(ios_conformance_tests); | |
| 1189 | 1207 | const run_menubar_tests = b.addRunArtifact(menubar_tests); |
| 1190 | 1208 | const run_components_tests = b.addRunArtifact(components_tests); |
| 1191 | 1209 | const run_gpu_tests = b.addRunArtifact(gpu_tests); |
| @@ -1312,6 +1330,7 @@ pub fn build(b: *std.Build) void { | ||
| 1312 | 1330 | test_step.dependOn(&run_api_tests.step); |
| 1313 | 1331 | test_step.dependOn(&run_mobile_tests.step); |
| 1314 | 1332 | test_step.dependOn(&run_ios_surface_tests.step); |
| 1333 | test_step.dependOn(&run_ios_conformance_tests.step); | |
| 1315 | 1334 | test_step.dependOn(&run_menubar_tests.step); |
| 1316 | 1335 | test_step.dependOn(&run_components_tests.step); |
| 1317 | 1336 | test_step.dependOn(&run_gpu_tests.step); |
| @@ -1392,6 +1411,7 @@ pub fn build(b: *std.Build) void { | ||
| 1392 | 1411 | |
| 1393 | 1412 | const test_ios_step = b.step("test:ios", "Run the iOS surface gate"); |
| 1394 | 1413 | test_ios_step.dependOn(&run_ios_surface_tests.step); |
| 1414 | test_ios_step.dependOn(&run_ios_conformance_tests.step); | |
| 1395 | 1415 | |
| 1396 | 1416 | const test_menubar_step = b.step("test:menubar", "Run Menubar tests"); |
| 1397 | 1417 | test_menubar_step.dependOn(&run_menubar_tests.step); |
| @@ -0,0 +1,209 @@ | ||
| 1 | const std = @import("std"); | |
| 2 | const testing = std.testing; | |
| 3 | ||
| 4 | /// What iOS owes the page, and who currently owes it. | |
| 5 | /// | |
| 6 | /// The Swift template is the specification for the Zig-native migration: its | |
| 7 | /// dispatcher is the list of actions a craft iOS app answers today, and Zig has | |
| 8 | /// to end up answering all of them. This file is what stops an action being | |
| 9 | /// dropped on the way across. | |
| 10 | /// | |
| 11 | /// It exists because the repo already demonstrated the failure. `CraftApp.swift` | |
| 12 | /// disagreed with itself: its injected JavaScript offered five `ota*` methods | |
| 13 | /// that its own `switch` never handled, and because those methods bypassed the | |
| 14 | /// `_createCallback` path that owns the timeout, calling one returned a promise | |
| 15 | /// that never settled — not a rejection, not a timeout, nothing. One file, two | |
| 16 | /// halves, no mechanism to notice. | |
| 17 | /// | |
| 18 | /// Embedded with `@embedFile` rather than read from disk, so the test is | |
| 19 | /// hermetic and reads exactly the bytes the build saw. | |
| 20 | const swift_spec = @embedFile("CraftApp.swift"); | |
| 21 | const zig_actions = @embedFile("src/bridge_mobile.zig"); | |
| 22 | ||
| 23 | /// The action list lives in the `switch action` block, and nowhere else. | |
| 24 | /// | |
| 25 | /// Bounding the scan matters more than it looks. `CraftApp.swift` contains 139 | |
| 26 | /// `case "..."` labels, but only 106 are actions — the rest are sub-switches | |
| 27 | /// over haptic styles, permission names, orientations, AR primitives. A whole | |
| 28 | /// file scan would count `case "box"` as a bridge action and then fail forever | |
| 29 | /// on a list nobody can satisfy. | |
| 30 | const dispatch_begin = "func userContentController("; | |
| 31 | const dispatch_end = "func webView("; | |
| 32 | ||
| 33 | /// How many spec actions Zig does not serve yet. | |
| 34 | /// | |
| 35 | /// A ratchet, in the shape `capabilities_test.zig` already uses for | |
| 36 | /// `max_undeclared`: the number may only go down, and lowering it is what a | |
| 37 | /// migration phase costs. It starts at the full spec minus the vertical slice. | |
| 38 | /// | |
| 39 | /// If this ever needs raising, something has been removed from Zig without | |
| 40 | /// being removed from the spec, and that is the conversation this constant | |
| 41 | /// exists to force. | |
| 42 | const max_not_yet_migrated: usize = 105; | |
| 43 | ||
| 44 | fn dispatcherRegion() []const u8 { | |
| 45 | const begin = std.mem.indexOf(u8, swift_spec, dispatch_begin) orelse return ""; | |
| 46 | const rest = swift_spec[begin..]; | |
| 47 | const end = std.mem.indexOf(u8, rest, dispatch_end) orelse return rest; | |
| 48 | return rest[0..end]; | |
| 49 | } | |
| 50 | ||
| 51 | /// Every `case "name":` in a region, as a set. | |
| 52 | fn collectCases(allocator: std.mem.Allocator, region: []const u8) !std.StringHashMap(void) { | |
| 53 | var set = std.StringHashMap(void).init(allocator); | |
| 54 | errdefer set.deinit(); | |
| 55 | ||
| 56 | var search: usize = 0; | |
| 57 | while (std.mem.indexOfPos(u8, region, search, "case \"")) |at| { | |
| 58 | const name_start = at + "case \"".len; | |
| 59 | const name_end = std.mem.indexOfScalarPos(u8, region, name_start, '"') orelse break; | |
| 60 | try set.put(region[name_start..name_end], {}); | |
| 61 | search = name_end; | |
| 62 | } | |
| 63 | return set; | |
| 64 | } | |
| 65 | ||
| 66 | /// Every action the injected JavaScript posts, as a set. | |
| 67 | fn collectPostedActions(allocator: std.mem.Allocator) !std.StringHashMap(void) { | |
| 68 | var set = std.StringHashMap(void).init(allocator); | |
| 69 | errdefer set.deinit(); | |
| 70 | ||
| 71 | const needle = "action: '"; | |
| 72 | var search: usize = 0; | |
| 73 | while (std.mem.indexOfPos(u8, swift_spec, search, needle)) |at| { | |
| 74 | const name_start = at + needle.len; | |
| 75 | const name_end = std.mem.indexOfScalarPos(u8, swift_spec, name_start, '\'') orelse break; | |
| 76 | try set.put(swift_spec[name_start..name_end], {}); | |
| 77 | search = name_end; | |
| 78 | } | |
| 79 | return set; | |
| 80 | } | |
| 81 | ||
| 82 | /// Every action name declared in `bridge_mobile.zig`'s `A` block. | |
| 83 | fn collectZigActions(allocator: std.mem.Allocator) !std.StringHashMap(void) { | |
| 84 | var set = std.StringHashMap(void).init(allocator); | |
| 85 | errdefer set.deinit(); | |
| 86 | ||
| 87 | var it = std.mem.splitScalar(u8, zig_actions, '\n'); | |
| 88 | var in_block = false; | |
| 89 | while (it.next()) |line| { | |
| 90 | const trimmed = std.mem.trim(u8, line, " \t\r"); | |
| 91 | if (std.mem.startsWith(u8, trimmed, "pub const A = struct {")) { | |
| 92 | in_block = true; | |
| 93 | continue; | |
| 94 | } | |
| 95 | if (in_block and std.mem.eql(u8, trimmed, "};")) break; | |
| 96 | if (!in_block) continue; | |
| 97 | ||
| 98 | // pub const get_device_info = "getDeviceInfo"; | |
| 99 | const open = std.mem.indexOfScalar(u8, trimmed, '"') orelse continue; | |
| 100 | const close = std.mem.indexOfScalarPos(u8, trimmed, open + 1, '"') orelse continue; | |
| 101 | try set.put(trimmed[open + 1 .. close], {}); | |
| 102 | } | |
| 103 | return set; | |
| 104 | } | |
| 105 | ||
| 106 | test "the spec scan finds the dispatcher, and finds actions in it" { | |
| 107 | // Non-vacuity, first. Every assertion below is a subset check, and a scan | |
| 108 | // that silently matched nothing would satisfy all of them. This is the | |
| 109 | // guard that makes the rest mean something: rename the Swift dispatcher and | |
| 110 | // this fails, rather than the suite going quietly green on an empty set. | |
| 111 | const region = dispatcherRegion(); | |
| 112 | try testing.expect(region.len > 1000); | |
| 113 | ||
| 114 | var cases = try collectCases(testing.allocator, region); | |
| 115 | defer cases.deinit(); | |
| 116 | try testing.expect(cases.count() >= 100); | |
| 117 | } | |
| 118 | ||
| 119 | test "every action the page can call is one the spec handles" { | |
| 120 | // The `ota*` check. Five methods in the injected JavaScript posted actions | |
| 121 | // the switch never handled, and because they bypassed `_createCallback` | |
| 122 | // they had no timeout either — so calling one returned a promise that never | |
| 123 | // settled. There is deliberately no allow-list here: a page-callable method | |
| 124 | // with nothing behind it is always a bug, and "documented exception" is how | |
| 125 | // it would come back. | |
| 126 | var handled = try collectCases(testing.allocator, dispatcherRegion()); | |
| 127 | defer handled.deinit(); | |
| 128 | ||
| 129 | var posted = try collectPostedActions(testing.allocator); | |
| 130 | defer posted.deinit(); | |
| 131 | ||
| 132 | var checked: usize = 0; | |
| 133 | var it = posted.keyIterator(); | |
| 134 | while (it.next()) |name| { | |
| 135 | if (!handled.contains(name.*)) { | |
| 136 | std.debug.print( | |
| 137 | "craft.{s}() posts action '{s}', which no dispatcher case handles.\n" ++ | |
| 138 | " The promise it returns can never settle.\n", | |
| 139 | .{ name.*, name.* }, | |
| 140 | ); | |
| 141 | return error.PageCanCallAnActionNothingHandles; | |
| 142 | } | |
| 143 | checked += 1; | |
| 144 | } | |
| 145 | try testing.expect(checked >= 50); | |
| 146 | } | |
| 147 | ||
| 148 | test "nothing has been dropped on the way from Swift to Zig" { | |
| 149 | // The migration guard. Every action the spec answers is either served by | |
| 150 | // Zig or explicitly still owed, and the number still owed only goes down. | |
| 151 | // | |
| 152 | // Without this, moving an action across is indistinguishable from losing | |
| 153 | // it: both leave a page calling something nothing answers, which is exactly | |
| 154 | // the state iOS was already in. | |
| 155 | var spec = try collectCases(testing.allocator, dispatcherRegion()); | |
| 156 | defer spec.deinit(); | |
| 157 | ||
| 158 | var zig = try collectZigActions(testing.allocator); | |
| 159 | defer zig.deinit(); | |
| 160 | ||
| 161 | try testing.expect(zig.count() >= 1); | |
| 162 | ||
| 163 | var not_yet: usize = 0; | |
| 164 | var it = spec.keyIterator(); | |
| 165 | while (it.next()) |name| { | |
| 166 | if (!zig.contains(name.*)) not_yet += 1; | |
| 167 | } | |
| 168 | ||
| 169 | if (not_yet > max_not_yet_migrated) { | |
| 170 | std.debug.print( | |
| 171 | "{d} spec actions are not served by Zig, but the ratchet allows {d}.\n" ++ | |
| 172 | " An action has left Zig without leaving the spec.\n", | |
| 173 | .{ not_yet, max_not_yet_migrated }, | |
| 174 | ); | |
| 175 | return error.MigrationWentBackwards; | |
| 176 | } | |
| 177 | ||
| 178 | // The ratchet is only a ratchet if it is tightened. A phase that migrates | |
| 179 | // actions without lowering the constant leaves the guard slack, so say so. | |
| 180 | if (not_yet < max_not_yet_migrated) { | |
| 181 | std.debug.print( | |
| 182 | "note: {d} spec actions remain unmigrated; max_not_yet_migrated is {d} and can be lowered.\n", | |
| 183 | .{ not_yet, max_not_yet_migrated }, | |
| 184 | ); | |
| 185 | } | |
| 186 | } | |
| 187 | ||
| 188 | test "every action Zig declares is one the spec actually has" { | |
| 189 | // The other direction. A Zig action the spec does not list is either a | |
| 190 | // typo — `getDeviceinfo` for `getDeviceInfo`, which a page would never | |
| 191 | // reach — or a surface invented on the Zig side that no page knows to call. | |
| 192 | var spec = try collectCases(testing.allocator, dispatcherRegion()); | |
| 193 | defer spec.deinit(); | |
| 194 | ||
| 195 | var zig = try collectZigActions(testing.allocator); | |
| 196 | defer zig.deinit(); | |
| 197 | ||
| 198 | var it = zig.keyIterator(); | |
| 199 | while (it.next()) |name| { | |
| 200 | if (!spec.contains(name.*)) { | |
| 201 | std.debug.print( | |
| 202 | "bridge_mobile.zig declares '{s}', which the Swift spec does not handle.\n" ++ | |
| 203 | " Either it is misspelled, or it is a surface no page can reach.\n", | |
| 204 | .{name.*}, | |
| 205 | ); | |
| 206 | return error.ZigServesAnActionTheSpecDoesNotHave; | |
| 207 | } | |
| 208 | } | |
| 209 | } | |