@@ -199,34 +199,30 @@ class CacheTransaction final {
199
199
}
200
200
};
201
201
202
- } // namespace
203
-
204
- bool SimpleCache::getOrSetThenHandler (JSContext *cx, JS::HandleObject owner, JS::HandleValue extra,
205
- JS::CallArgs args) {
206
- MOZ_ASSERT (extra.isObject ());
207
- JS::RootedObject extraObj (cx, &extra.toObject ());
208
- JS::RootedValue handleVal (cx);
209
- JS::RootedValue promiseVal (cx);
210
- if (!JS_GetProperty (cx, extraObj, " promise" , &promiseVal)) {
202
+ bool get_or_set_then_handler (JSContext *cx, JS::HandleObject lookup_state, JS::HandleValue extra,
203
+ JS::CallArgs args) {
204
+ JS::RootedValue handle_val (cx);
205
+ JS::RootedValue promise_val (cx);
206
+ if (!JS_GetProperty (cx, lookup_state, " promise" , &promise_val)) {
211
207
return false ;
212
208
}
213
- MOZ_ASSERT (promiseVal .isObject ());
214
- JS::RootedObject promise (cx, &promiseVal .toObject ());
209
+ MOZ_ASSERT (promise_val .isObject ());
210
+ JS::RootedObject promise (cx, &promise_val .toObject ());
215
211
if (!promise) {
216
212
return ReturnPromiseRejectedWithPendingError (cx, args);
217
213
}
218
214
219
- if (!JS_GetProperty (cx, extraObj , " handle" , &handleVal )) {
215
+ if (!JS_GetProperty (cx, lookup_state , " handle" , &handle_val )) {
220
216
return RejectPromiseWithPendingError (cx, promise);
221
217
}
222
- MOZ_ASSERT (handleVal .isInt32 ());
218
+ MOZ_ASSERT (handle_val .isInt32 ());
223
219
224
- host_api::CacheHandle handle (handleVal .toInt32 ());
220
+ host_api::CacheHandle handle (handle_val .toInt32 ());
225
221
226
222
BEGIN_TRANSACTION (transaction, cx, promise, handle);
227
223
228
224
JS::RootedValue keyVal (cx);
229
- if (!JS_GetProperty (cx, extraObj , " key" , &keyVal)) {
225
+ if (!JS_GetProperty (cx, lookup_state , " key" , &keyVal)) {
230
226
return false ;
231
227
}
232
228
@@ -376,6 +372,30 @@ bool SimpleCache::getOrSetThenHandler(JSContext *cx, JS::HandleObject owner, JS:
376
372
return true ;
377
373
}
378
374
375
+ bool get_or_set_catch_handler (JSContext *cx, JS::HandleObject lookup_state,
376
+ JS::HandleValue inner_promise_val, JS::CallArgs args) {
377
+ JS::RootedValue promise_val (cx);
378
+ if (!JS_GetProperty (cx, lookup_state, " promise" , &promise_val)) {
379
+ return false ;
380
+ }
381
+ MOZ_ASSERT (promise_val.isObject ());
382
+ JS::RootedObject promise (cx, &promise_val.toObject ());
383
+ if (!promise) {
384
+ return ReturnPromiseRejectedWithPendingError (cx, args);
385
+ }
386
+
387
+ JS::RootedObject inner_promise (cx, &inner_promise_val.toObject ());
388
+ if (!inner_promise) {
389
+ return ReturnPromiseRejectedWithPendingError (cx, args);
390
+ }
391
+ MOZ_ASSERT (JS::GetPromiseState (inner_promise) == JS::PromiseState::Rejected);
392
+ JS::RootedValue promise_rejection_err (cx, JS::GetPromiseResult (inner_promise));
393
+ JS::RejectPromise (cx, promise, promise_rejection_err);
394
+ return true ;
395
+ }
396
+
397
+ } // namespace
398
+
379
399
// static getOrSet(key: string, set: () => Promise<{value: BodyInit, ttl: number}>):
380
400
// SimpleCacheEntry | null; static getOrSet(key: string, set: () => Promise<{value: ReadableStream,
381
401
// ttl: number, length: number}>): SimpleCacheEntry | null;
@@ -462,29 +482,34 @@ bool SimpleCache::getOrSet(JSContext *cx, unsigned argc, JS::Value *vp) {
462
482
}
463
483
464
484
// JS::RootedObject owner(cx, JS_NewPlainObject(cx));
465
- JS::RootedObject extraObj (cx, JS_NewPlainObject (cx));
466
- JS::RootedValue handleVal (cx, JS::NumberValue (handle.handle ));
467
- if (!JS_SetProperty (cx, extraObj , " handle" , handleVal )) {
485
+ JS::RootedObject lookup_state (cx, JS_NewPlainObject (cx));
486
+ JS::RootedValue handle_val (cx, JS::NumberValue (handle.handle ));
487
+ if (!JS_SetProperty (cx, lookup_state , " handle" , handle_val )) {
468
488
return false ;
469
489
}
470
490
JS::RootedValue keyVal (
471
491
cx, JS::StringValue (JS_NewStringCopyN (cx, key_chars.begin (), key_chars.len )));
472
- if (!JS_SetProperty (cx, extraObj , " key" , keyVal)) {
492
+ if (!JS_SetProperty (cx, lookup_state , " key" , keyVal)) {
473
493
return false ;
474
494
}
475
- JS::RootedValue promiseVal (cx, JS::ObjectValue (*promise));
476
- if (!JS_SetProperty (cx, extraObj , " promise" , promiseVal )) {
495
+ JS::RootedValue promise_val (cx, JS::ObjectValue (*promise));
496
+ if (!JS_SetProperty (cx, lookup_state , " promise" , promise_val )) {
477
497
return false ;
478
498
}
479
499
480
- JS::RootedValue extra (cx, JS::ObjectValue (*extraObj));
481
500
JS::RootedObject global (cx, JS::CurrentGlobalOrNull (cx));
482
- JS::RootedObject then_handler (cx,
483
- create_internal_method<getOrSetThenHandler>(cx, global, extra));
501
+ JS::RootedObject then_handler (
502
+ cx, create_internal_method<get_or_set_then_handler>(cx, lookup_state));
503
+ if (!then_handler) {
504
+ return false ;
505
+ }
506
+ JS::RootedValue result_promise_val (cx, JS::ObjectValue (*result_promise));
507
+ JS::RootedObject catch_handler (
508
+ cx, create_internal_method<get_or_set_catch_handler>(cx, lookup_state, result_promise_val));
484
509
if (!then_handler) {
485
510
return false ;
486
511
}
487
- if (!JS::AddPromiseReactions (cx, result_promise, then_handler, nullptr )) {
512
+ if (!JS::AddPromiseReactions (cx, result_promise, then_handler, catch_handler )) {
488
513
return false ;
489
514
}
490
515
transaction.commit ();
0 commit comments