also looking at this
iOS Phase 8: the presented pickers — ratchet 58 → 53, last mechanism landed
#90
11 files
+5752
-12
| @@ -62,6 +62,10 @@ const Slot = struct { | ||
| 62 | 62 | /// A module-shaped reply, for completions this file cannot shape itself. |
| 63 | 63 | /// Owned by the slot; freed on delivery. Null means use `granted`. |
| 64 | 64 | 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, | |
| 65 | 69 | /// Set when the reply could not be produced at all. Distinct from a null |
| 66 | 70 | /// `json`, which legitimately means "this was a BOOL completion" — without |
| 67 | 71 | /// the flag, a failure falls through to the BOOL wording and resolves |
| @@ -230,6 +234,7 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void { | ||
| 230 | 234 | var granted = false; |
| 231 | 235 | var json: ?[]u8 = null; |
| 232 | 236 | var failed = false; |
| 237 | var fail_code = bridge_error.BridgeError.NativeCallFailed; | |
| 233 | 238 | { |
| 234 | 239 | slots_mutex.lock(); |
| 235 | 240 | defer slots_mutex.unlock(); |
| @@ -241,8 +246,10 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void { | ||
| 241 | 246 | granted = slot.granted; |
| 242 | 247 | json = slot.json; |
| 243 | 248 | failed = slot.failed; |
| 249 | fail_code = slot.fail_code; | |
| 244 | 250 | slot.json = null; |
| 245 | 251 | slot.failed = false; |
| 252 | slot.fail_code = bridge_error.BridgeError.NativeCallFailed; | |
| 246 | 253 | slot.in_use = false; |
| 247 | 254 | slot.generation +%= 1; |
| 248 | 255 | } |
| @@ -256,11 +263,7 @@ fn deliverOnMain(context: ?*anyopaque) callconv(.c) void { | ||
| 256 | 263 | defer request_context.pop(); |
| 257 | 264 | |
| 258 | 265 | 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); | |
| 264 | 267 | return; |
| 265 | 268 | } |
| 266 | 269 | |
| @@ -305,6 +308,26 @@ pub fn deliverJson(ticket: Ticket, json: []const u8) void { | ||
| 305 | 308 | dispatch_async_f(&_dispatch_main_q, @ptrFromInt(@as(usize, ticket.index) + 1), deliverOnMain); |
| 306 | 309 | } |
| 307 | 310 | |
| 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. | |
| 318 | pub 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 | ||
| 308 | 331 | /// Reply to a captured call with an error rather than a result. |
| 309 | 332 | /// |
| 310 | 333 | /// 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" { | ||
| 425 | 448 | slots[ticket.index].in_use = false; |
| 426 | 449 | slots[ticket.index].generation +%= 1; |
| 427 | 450 | } |
| 451 | ||
| 452 | test "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 | ||
| 474 | test "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 | } | |