@@ -706,28 +706,21 @@ TimeoutId::NumberType ServiceWorkerGlobalScope::setTimeoutInternal(
706
706
707
707
TimeoutId::NumberType ServiceWorkerGlobalScope::setTimeout (
708
708
jsg::Lock& js,
709
- jsg::V8Ref<v8::Function > function,
709
+ jsg::Function< void (jsg::Arguments<jsg::Value>) > function,
710
710
jsg::Optional<double> msDelay,
711
- jsg::Varargs args) {
712
- KJ_IF_MAYBE (context, jsg::AsyncContextFrame::current (js)) {
713
- function = js.v8Ref (context->wrap (js, function));
714
- }
715
- auto argv = kj::heapArrayFromIterable<jsg::Value>(kj::mv (args));
711
+ jsg::Arguments<jsg::Value> args) {
712
+ function.setReceiver (js.v8Ref <v8::Value>(js.v8Context ()->Global ()));
713
+ auto fn = [function=kj::mv (function),
714
+ args=kj::mv (args),
715
+ context=jsg::AsyncContextFrame::currentRef (js)](jsg::Lock& js) mutable {
716
+ jsg::AsyncContextFrame::Scope scope (js, context);
717
+ function (js, kj::mv (args));
718
+ };
716
719
auto timeoutId = IoContext::current ().setTimeoutImpl (
717
720
timeoutIdGenerator,
718
721
/* repeats = */ false ,
719
- [function = function.addRef (js),
720
- argv = kj::mv (argv)]
721
- (jsg::Lock& js) mutable {
722
- auto context = js.v8Context ();
723
- auto localFunction = function.getHandle (js);
724
- auto localArgs = KJ_MAP (arg, argv) {
725
- return arg.getHandle (js);
726
- };
727
- auto argc = localArgs.size ();
728
-
729
- // Cast to void to discard the result value.
730
- (void )jsg::check (localFunction->Call (context, context->Global (), argc, &localArgs.front ()));
722
+ [function = kj::mv (fn)](jsg::Lock& js) mutable {
723
+ function (js);
731
724
}, msDelay.orDefault (0 ));
732
725
return timeoutId.toNumber ();
733
726
}
@@ -740,28 +733,24 @@ void ServiceWorkerGlobalScope::clearTimeout(kj::Maybe<TimeoutId::NumberType> tim
740
733
741
734
TimeoutId::NumberType ServiceWorkerGlobalScope::setInterval (
742
735
jsg::Lock& js,
743
- jsg::V8Ref<v8::Function > function,
736
+ jsg::Function< void (jsg::Arguments<jsg::Value>) > function,
744
737
jsg::Optional<double> msDelay,
745
- jsg::Varargs args) {
746
- KJ_IF_MAYBE (context, jsg::AsyncContextFrame::current (js)) {
747
- function = js.v8Ref (context->wrap (js, function));
748
- }
749
- auto argv = kj::heapArrayFromIterable<jsg::Value>(kj::mv (args));
738
+ jsg::Arguments<jsg::Value> args) {
739
+ function.setReceiver (js.v8Ref <v8::Value>(js.v8Context ()->Global ()));
740
+ auto fn = [function=kj::mv (function),
741
+ args=kj::mv (args),
742
+ context=jsg::AsyncContextFrame::currentRef (js)]
743
+ (jsg::Lock& js) mutable {
744
+ jsg::AsyncContextFrame::Scope scope (js, context);
745
+ // Because the fn is called multiple times, we will clone the args on each call.
746
+ auto argv = KJ_MAP (i, args) { return i.addRef (js); };
747
+ function (js, jsg::Arguments (kj::mv (argv)));
748
+ };
750
749
auto timeoutId = IoContext::current ().setTimeoutImpl (
751
750
timeoutIdGenerator,
752
751
/* repeats = */ true ,
753
- [function = function.addRef (js),
754
- argv = kj::mv (argv)]
755
- (jsg::Lock& js) mutable {
756
- auto context = js.v8Context ();
757
- auto localFunction = function.getHandle (js);
758
- auto localArgs = KJ_MAP (arg, argv) {
759
- return arg.getHandle (js);
760
- };
761
- auto argc = localArgs.size ();
762
-
763
- // Cast to void to discard the result value.
764
- (void )jsg::check (localFunction->Call (context, context->Global (), argc, &localArgs.front ()));
752
+ [function = kj::mv (fn)](jsg::Lock& js) mutable {
753
+ function (js);
765
754
}, msDelay.orDefault (0 ));
766
755
return timeoutId.toNumber ();
767
756
}
0 commit comments