@@ -413,15 +413,18 @@ jsg::Ref<FormData::ValueIterator> FormData::values(jsg::Lock&) {
413
413
414
414
void FormData::forEach (
415
415
jsg::Lock& js,
416
- jsg::V8Ref<v8::Function> callback,
417
- jsg::Optional<jsg::Value> thisArg,
418
- const jsg::TypeHandler<EntryType>& handler) {
419
- auto localCallback = callback.getHandle (js);
420
- auto localThisArg = thisArg.map ([&js](jsg::Value& v) { return v.getHandle (js); })
421
- .orDefault (js.v8Undefined ());
422
- // JSG_THIS.tryGetHandle() is guaranteed safe because `forEach()` is only called
423
- // from JavaScript, which means a Headers JS wrapper object must already exist.
424
- auto localParams = KJ_ASSERT_NONNULL (JSG_THIS.tryGetHandle (js.v8Isolate ));
416
+ jsg::Function<void (EntryType, kj::StringPtr , jsg::Ref<FormData>)> callback,
417
+ jsg::Optional<jsg::Value> thisArg) {
418
+ // Here, if the thisArg is not passed, or is passed explicitly as a null or
419
+ // undefined, then undefined is used as the thisArg.
420
+ auto receiver = js.v8Undefined ();
421
+ KJ_IF_MAYBE (arg, thisArg) {
422
+ auto handle = arg->getHandle (js);
423
+ if (!handle->IsNullOrUndefined ()) {
424
+ receiver = handle;
425
+ }
426
+ }
427
+ callback.setReceiver (js.v8Ref (receiver));
425
428
426
429
// On each iteration of the for loop, a JavaScript callback is invokved. If a new
427
430
// item is appended to the URLSearchParams within that function, the loop must pick
@@ -430,16 +433,7 @@ void FormData::forEach(
430
433
// are added to the search params unconditionally on each iteration.
431
434
for (size_t i = 0 ; i < this ->data .size (); i++) {
432
435
auto & [key, value] = this ->data [i];
433
- static constexpr auto ARG_COUNT = 3 ;
434
-
435
- v8::Local<v8::Value> args[ARG_COUNT] = {
436
- handler.wrap (js, clone (value)),
437
- jsg::v8Str (js.v8Isolate , key),
438
- localParams,
439
- };
440
- // Call jsg::check() to propagate exceptions, but we don't expect any
441
- // particular return value.
442
- jsg::check (localCallback->Call (js.v8Context (), localThisArg, ARG_COUNT, args));
436
+ callback (js, clone (value), key, JSG_THIS);
443
437
}
444
438
}
445
439
0 commit comments