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/src/bridge_mobile.zigmodified+131-3
Changes to packages/zig/src/bridge_mobile.zig
@@ -52,7 +52,7 @@ pub const MobileBridge = struct {
5252 }
5353
5454 fn getDeviceInfo(self: *Self) !void {
55 var buf: [512]u8 = undefined;
55 var buf: [4096]u8 = undefined;
5656 const json = try describeDevice(&buf);
5757 bridge_error.sendResultToJS(self.allocator, A.get_device_info, json);
5858 }
@@ -88,14 +88,142 @@ fn describeDevice(buf: []u8) ![]const u8 {
8888 const system_name = try nsStringField(device, "systemName");
8989 const system_version = try nsStringField(device, "systemVersion");
9090 const model = try nsStringField(device, "model");
91 // The one field a user can set, so the one that can carry a quote or a
92 // backslash: "Glenn\"s iPhone" is a legal device name and an illegal JSON
93 // string. Everything here goes through `escapeJsonString` for that reason,
94 // not just this one — a locale identifier is tame today and the escaping
95 // costs nothing.
96 const device_name = try nsStringField(device, "name");
97
98 // `identifierForVendor` is nil before the device is unlocked once after
99 // boot. The spec answers `""` there (`device.identifierForVendor?.uuidString
100 // ?? ""`), so this does too: a missing id and an empty one are the same
101 // thing to a page, and inventing a UUID would be worse than either.
102 const vendor_id: []const u8 = blk: {
103 const sel_ifv = objc.sel_registerName("identifierForVendor") orelse break :blk "";
104 const uuid = objc.msgSendId(device, sel_ifv);
105 if (uuid == null) break :blk "";
106 break :blk nsStringField(uuid, "UUIDString") catch "";
107 };
108
109 // UIScreen, for the three screen fields. A nil mainScreen is not a case
110 // that happens in an app with a window, but reading `bounds` off nil would
111 // answer a zero rect rather than fail, and a page cannot tell a 0x0 screen
112 // from a broken one.
113 const UIScreen = objc.objc_getClass("UIScreen") orelse return error.ClassNotFound;
114 const sel_main = objc.sel_registerName("mainScreen") orelse return error.SelectorNotFound;
115 const screen = objc.msgSendId(UIScreen, sel_main);
116 if (screen == null) return error.NoMainScreen;
117
118 const bounds = try msgSendRect(screen, "bounds");
119 const scale = try msgSendCGFloat(screen, "scale");
120
121 // `batteryLevel` is -1 unless battery monitoring is enabled, and neither
122 // the spec nor this enables it. Reporting the same -1 is the parity that
123 // matters: a page that special-cases it keeps working, and turning
124 // monitoring on here would be a behaviour change the spec never made.
125 const battery_level = try msgSendFloat(device, "batteryLevel");
126 const battery_state = batteryStateName(try msgSendInteger(device, "batteryState"));
127
128 const NSLocale = objc.objc_getClass("NSLocale") orelse return error.ClassNotFound;
129 const sel_curloc = objc.sel_registerName("currentLocale") orelse return error.SelectorNotFound;
130 const locale_obj = objc.msgSendId(NSLocale, sel_curloc);
131 const locale = if (locale_obj != null) nsStringField(locale_obj, "localeIdentifier") catch "" else "";
132
133 const NSTimeZone = objc.objc_getClass("NSTimeZone") orelse return error.ClassNotFound;
134 const sel_localtz = objc.sel_registerName("localTimeZone") orelse return error.SelectorNotFound;
135 const tz_obj = objc.msgSendId(NSTimeZone, sel_localtz);
136 const timezone = if (tz_obj != null) nsStringField(tz_obj, "name") catch "" else "";
137
138 var esc: [5][512]u8 = undefined;
91139
92140 return std.fmt.bufPrint(
93141 buf,
94 "{⁠{\"systemName\":\"{s}\",\"systemVersion\":\"{s}\",\"model\":\"{s}\",\"isSimulator\":{}}}",
95 .{ system_name, system_version, model, isSimulator() },
142 "{⁠{" ++
143 "\"platform\":\"ios\"," ++
144 "\"model\":\"{s}\"," ++
145 "\"name\":\"{s}\"," ++
146 "\"systemName\":\"{s}\"," ++
147 "\"systemVersion\":\"{s}\"," ++
148 "\"identifierForVendor\":\"{s}\"," ++
149 "\"isSimulator\":{}," ++
150 "\"screenWidth\":{d}," ++
151 "\"screenHeight\":{d}," ++
152 "\"screenScale\":{d}," ++
153 "\"batteryLevel\":{d}," ++
154 "\"batteryState\":\"{s}\"," ++
155 "\"locale\":\"{s}\"," ++
156 "\"timezone\":\"{s}\"" ++
157 "}}",
158 .{
159 try bridge_error.escapeJsonString(&esc[0], model),
160 try bridge_error.escapeJsonString(&esc[1], device_name),
161 system_name,
162 system_version,
163 vendor_id,
164 isSimulator(),
165 bounds.size.width,
166 bounds.size.height,
167 scale,
168 battery_level,
169 battery_state,
170 try bridge_error.escapeJsonString(&esc[2], locale),
171 try bridge_error.escapeJsonString(&esc[3], timezone),
172 },
96173 );
97174}
98175
176/// `UIDevice.BatteryState` as the spec spells it (`getBatteryState`,
177/// `CraftApp.swift:3204`). The raw values are UIKit's: unknown 0, unplugged 1,
178/// charging 2, full 3. Anything else is "unknown", matching the spec's
179/// `default` rather than inventing a fifth name for a value UIKit does not
180/// currently produce.
181fn batteryStateName(raw: isize) []const u8 {
182 return switch (raw) {
183 1 => "unplugged",
184 2 => "charging",
185 3 => "full",
186 else => "unknown",
187 };
188}
189
190/// A zero-argument selector returning `CGRect`.
191///
192/// A plain `objc_msgSend` cast, deliberately — **not**
193/// `objc_runtime.msgSendStret`, which picks `objc_msgSend_stret` for anything
194/// over 16 bytes. `CGRect` is 32, and `objc_msgSend_stret` does not exist on
195/// arm64: the struct return travels in x8 through ordinary `objc_msgSend`.
196/// `bridge_mobile_motion.zig:124` records the same trap, and `macos.zig`'s
197/// `msgSendRect` is the shape this copies.
198fn msgSendRect(target: objc.id, comptime selector: [:0]const u8) !objc.CGRect {
199 const sel = objc.sel_registerName(selector) orelse return error.SelectorNotFound;
200 const func: *const fn (objc.id, objc.SEL) callconv(.c) objc.CGRect = @ptrCast(&objc.objc_msgSend);
201 return func(target, sel);
202}
203
204/// A zero-argument selector returning `CGFloat` (`double` on 64-bit).
205fn msgSendCGFloat(target: objc.id, comptime selector: [:0]const u8) !objc.CGFloat {
206 const sel = objc.sel_registerName(selector) orelse return error.SelectorNotFound;
207 const func: *const fn (objc.id, objc.SEL) callconv(.c) objc.CGFloat = @ptrCast(&objc.objc_msgSend);
208 return func(target, sel);
209}
210
211/// A zero-argument selector returning `float`. `batteryLevel` is a `float`,
212/// not a `CGFloat` — reading it as a double returns garbage, because the two
213/// come back in different halves of the same register.
214fn msgSendFloat(target: objc.id, comptime selector: [:0]const u8) !f32 {
215 const sel = objc.sel_registerName(selector) orelse return error.SelectorNotFound;
216 const func: *const fn (objc.id, objc.SEL) callconv(.c) f32 = @ptrCast(&objc.objc_msgSend);
217 return func(target, sel);
218}
219
220/// A zero-argument selector returning `NSInteger`.
221fn msgSendInteger(target: objc.id, comptime selector: [:0]const u8) !isize {
222 const sel = objc.sel_registerName(selector) orelse return error.SelectorNotFound;
223 const func: *const fn (objc.id, objc.SEL) callconv(.c) isize = @ptrCast(&objc.objc_msgSend);
224 return func(target, sel);
225}
226
99227/// Send a zero-argument selector that returns an NSString, and borrow its UTF-8.
100228///
101229/// The returned slice points into the NSString's own buffer, which is