ReviewOS

also looking at this

craft-native/craft

feat(ios): the Zig runtime, offered to a Swift-hosted app

#103
Merged glennmichael123 wants to merge feat/ios-swift-host-seam into main
7 files +868 -59
packages/zig/test/ios_conformance_test.zigmodified+48-0
Changes to packages/zig/test/ios_conformance_test.zig
@@ -649,3 +649,51 @@ test "Zig requires exactly the keys the spec's decoder requires" {
649649 try testing.expect(required >= 40);
650650 try testing.expect(optional_seen >= 1);
651651}
652
653test "getDeviceInfo answers every field the spec answers" {
654 // The regression that made this test necessary. Zig's `getDeviceInfo` is
655 // `.live`, so the Swift-hosted seam prefers it over the spec's arm — and
656 // it answered four of the spec's fourteen fields. A page reading
657 // `screenWidth` or `locale` got `undefined` from an action that reported
658 // success, which is the exact failure `.unavailable` exists to prevent and
659 // could not catch, because the action does work; it just works less.
660 //
661 // Keys are read out of the spec rather than listed here, so a field added
662 // to `CraftApp.swift` fails this test until Zig answers it too.
663 // Anchored to the function, not to `let info:` — the spec has more than
664 // one dictionary spelled that way (`getMemoryUsage` has another), and the
665 // first match is not this one.
666 const fn_start = std.mem.indexOf(u8, swift_spec, "private func getDeviceInfo(callbackId:") orelse
667 return error.SpecShapeChanged;
668 const body = swift_spec[fn_start..];
669 const start = std.mem.indexOf(u8, body, "let info: [String: Any] = [") orelse
670 return error.SpecShapeChanged;
671 const after = body[start..];
672 const end = std.mem.indexOf(u8, after, "\n ]") orelse return error.SpecShapeChanged;
673 const block = after[0..end];
674
675 const zig_src = @embedFile("src/bridge_mobile.zig");
676
677 var it = std.mem.splitScalar(u8, block, '\n');
678 var checked: usize = 0;
679 while (it.next()) |line| {
680 const q1 = std.mem.indexOfScalar(u8, line, '"') orelse continue;
681 const rest = line[q1 + 1 ..];
682 const q2 = std.mem.indexOfScalar(u8, rest, '"') orelse continue;
683 const key = rest[0..q2];
684 if (key.len == 0) continue;
685
686 // Zig builds the reply as a format string, so the key appears in the
687 // source as the escaped `\"key\":` it will emit.
688 var needle_buf: [64]u8 = undefined;
689 const needle = try std.fmt.bufPrint(&needle_buf, "\\\"{s}\\\":", .{key});
690 if (std.mem.indexOf(u8, zig_src, needle) == null) {
691 std.debug.print("getDeviceInfo: the spec answers `{s}` and Zig does not\n", .{key});
692 return error.FieldMissingFromZig;
693 }
694 checked += 1;
695 }
696
697 // A shape change that silently matched nothing would otherwise pass.
698 try testing.expectEqual(@as(usize, 14), checked);
699}