ReviewOS

also looking at this

craft-native/craft

iOS Phase 8: the presented pickers — ratchet 58 → 53, last mechanism landed

#90
Merged glennmichael123 wants to merge feat/ios-phase8-ui into main
11 files +5752 -12
packages/zig/src/ios_async.zigmodified+66-5
Changes to packages/zig/src/ios_async.zig
@@ -62,6 +62,10 @@ const Slot = struct {
6262 /// A module-shaped reply, for completions this file cannot shape itself.
6363 /// Owned by the slot; freed on delivery. Null means use `granted`.
6464 json: ?[]u8 = null,
65 /// Which error to report when `failed`. Defaults to the generic one, so
66 /// `deliverError` keeps its meaning and only a caller that knows better
67 /// has to say so.
68 fail_code: bridge_error.BridgeError = bridge_error.BridgeError.NativeCallFailed,
6569 /// Set when the reply could not be produced at all. Distinct from a null
6670 /// `json`, which legitimately means "this was a BOOL completion" — without
6771 /// the flag, a failure falls through to the BOOL wording and resolves
@@ -230,6 +234,7 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void {
230234 var granted = false;
231235 var json: ?[]u8 = null;
232236 var failed = false;
237 var fail_code = bridge_error.BridgeError.NativeCallFailed;
233238 {
234239 slots_mutex.lock();
235240 defer slots_mutex.unlock();
@@ -241,8 +246,10 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void {
241246 granted = slot.granted;
242247 json = slot.json;
243248 failed = slot.failed;
249 fail_code = slot.fail_code;
244250 slot.json = null;
245251 slot.failed = false;
252 slot.fail_code = bridge_error.BridgeError.NativeCallFailed;
246253 slot.in_use = false;
247254 slot.generation +%= 1;
248255 }
@@ -256,11 +263,7 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void {
256263 defer request_context.pop();
257264
258265 if (failed) {
259 bridge_error.sendErrorToJS(
260 std.heap.c_allocator,
261 action,
262 bridge_error.BridgeError.NativeCallFailed,
263 );
266 bridge_error.sendErrorToJS(std.heap.c_allocator, action, fail_code);
264267 return;
265268 }
266269
@@ -305,6 +308,26 @@ pub fn deliverJson(ticket: Ticket, json: []const u8) void {
305308 dispatch_async_f(&_dispatch_main_q, @ptrFromInt(@as(usize, ticket.index) + 1), deliverOnMain);
306309}
307310
311/// Reply to a captured call with a *specific* error.
312///
313/// `deliverError` reports `NativeCallFailed`, which is right for "the answer
314/// could not be shaped" and wrong for everything else. A user tapping Cancel
315/// on a picker is not a native call failing — the call worked perfectly and
316/// the answer is "no". Telling the page otherwise sends anyone reading the
317/// error looking for a bug that is not there.
318pub fn deliverErrorCode(ticket: Ticket, err: bridge_error.BridgeError) void {
319 slots_mutex.lock();
320 const valid = slots[ticket.index].in_use and slots[ticket.index].generation == ticket.generation;
321 if (valid) {
322 slots[ticket.index].failed = true;
323 slots[ticket.index].fail_code = err;
324 }
325 slots_mutex.unlock();
326
327 if (!valid) return;
328 dispatch_async_f(&_dispatch_main_q, @ptrFromInt(@as(usize, ticket.index) + 1), deliverOnMain);
329}
330
308331/// Reply to a captured call with an error rather than a result.
309332///
310333/// A module's own completion can fail to shape its answer — a nil object where
@@ -425,3 +448,41 @@ test "a reply that could not be shaped is flagged, not silently downgraded" {
425448 slots[ticket.index].in_use = false;
426449 slots[ticket.index].generation +%= 1;
427450}
451
452test "a cancel reports being cancelled, not a native failure" {
453 // The distinction matters to whoever reads the error. A user tapping
454 // Cancel is not a native call failing — the call worked and the answer is
455 // "no" — and reporting NativeCallFailed sends a reader looking for a bug
456 // that is not there.
457 request_context.push(11);
458 const ticket = acquire("pickImage") orelse return error.PoolUnexpectedlyFull;
459 request_context.pop();
460
461 deliverErrorCode(ticket, bridge_error.BridgeError.Cancelled);
462
463 slots_mutex.lock();
464 defer slots_mutex.unlock();
465 try testing.expect(slots[ticket.index].failed);
466 try testing.expectEqual(bridge_error.BridgeError.Cancelled, slots[ticket.index].fail_code);
467
468 slots[ticket.index].failed = false;
469 slots[ticket.index].fail_code = bridge_error.BridgeError.NativeCallFailed;
470 slots[ticket.index].in_use = false;
471 slots[ticket.index].generation +%= 1;
472}
473
474test "the generic deliverError still means a native failure" {
475 // deliverErrorCode must not have changed what deliverError says: a
476 // shaping failure genuinely is a native call that did not produce an
477 // answer, and that is the default a slot resets to.
478 const ticket = acquire("x") orelse return error.PoolUnexpectedlyFull;
479 deliverError(ticket);
480
481 slots_mutex.lock();
482 defer slots_mutex.unlock();
483 try testing.expectEqual(bridge_error.BridgeError.NativeCallFailed, slots[ticket.index].fail_code);
484
485 slots[ticket.index].failed = false;
486 slots[ticket.index].in_use = false;
487 slots[ticket.index].generation +%= 1;
488}