@@ -270,25 +270,7 @@ MaybeLocal<Value> WebAssemblyInstantiateImpl(Isolate* isolate,
270270 return Utils::ToLocal (instance_object.ToHandleChecked ());
271271}
272272
273- // Entered as internal implementation detail of sync and async instantiate.
274- // args[0] *must* be a WebAssembly.Module.
275- void WebAssemblyInstantiateImplCallback (
276- const v8::FunctionCallbackInfo<v8::Value>& args) {
277- DCHECK_GE (args.Length (), 1 );
278- v8::Isolate* isolate = args.GetIsolate ();
279- MicrotasksScope does_not_run_microtasks (isolate,
280- MicrotasksScope::kDoNotRunMicrotasks );
281-
282- HandleScope scope (args.GetIsolate ());
283- Local<Value> module = args[0 ];
284- Local<Value> ffi = args.Data ();
285- Local<Value> instance;
286- if (WebAssemblyInstantiateImpl (isolate, module , ffi).ToLocal (&instance)) {
287- args.GetReturnValue ().Set (instance);
288- }
289- }
290-
291- void WebAssemblyInstantiateToPairCallback (
273+ void WebAssemblyInstantiateCallback (
292274 const v8::FunctionCallbackInfo<v8::Value>& args) {
293275 DCHECK_GE (args.Length (), 1 );
294276 Isolate* isolate = args.GetIsolate ();
@@ -369,7 +351,7 @@ void WebAssemblyInstantiateStreaming(
369351 DCHECK (!module_promise.IsEmpty ());
370352 Local<Value> data = args[1 ];
371353 ASSIGN (Function, instantiate_impl,
372- Function::New (context, WebAssemblyInstantiateToPairCallback , data));
354+ Function::New (context, WebAssemblyInstantiateCallback , data));
373355 ASSIGN (Promise, result, module_promise->Then (context, instantiate_impl));
374356 args.GetReturnValue ().Set (result);
375357}
@@ -390,20 +372,12 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
390372 Local<Context> context = isolate->GetCurrentContext ();
391373
392374 ASSIGN (Promise::Resolver, resolver, Promise::Resolver::New (context));
393- Local<Promise> module_promise = resolver->GetPromise ();
394- args.GetReturnValue ().Set (module_promise);
395-
396- if (args.Length () < 1 ) {
397- thrower.TypeError (
398- " Argument 0 must be provided and must be either a buffer source or a "
399- " WebAssembly.Module object" );
400- auto maybe = resolver->Reject (context, Utils::ToLocal (thrower.Reify ()));
401- CHECK_IMPLIES (!maybe.FromMaybe (false ),
402- i_isolate->has_scheduled_exception ());
403- return ;
404- }
375+ Local<Promise> promise = resolver->GetPromise ();
376+ args.GetReturnValue ().Set (promise);
405377
406378 Local<Value> first_arg_value = args[0 ];
379+ // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
380+ Local<Value> ffi = args[1 ];
407381 i::Handle<i::Object> first_arg = Utils::OpenHandle (*first_arg_value);
408382 if (!first_arg->IsJSObject ()) {
409383 thrower.TypeError (
@@ -414,26 +388,35 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
414388 return ;
415389 }
416390
417- FunctionCallback instantiator = nullptr ;
418391 if (first_arg->IsWasmModuleObject ()) {
419- module_promise = resolver->GetPromise ();
420- if (!resolver->Resolve (context, first_arg_value).IsJust ()) return ;
421- instantiator = WebAssemblyInstantiateImplCallback;
422- } else {
423- ASSIGN (Function, async_compile, Function::New (context, WebAssemblyCompile));
424- ASSIGN (Value, async_compile_retval,
425- async_compile->Call (context, args.Holder (), 1 , &first_arg_value));
426- module_promise = Local<Promise>::Cast (async_compile_retval);
427- instantiator = WebAssemblyInstantiateToPairCallback;
392+ i::Handle<i::WasmModuleObject> module_obj =
393+ i::Handle<i::WasmModuleObject>::cast (first_arg);
394+ // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
395+ i::MaybeHandle<i::JSReceiver> maybe_imports =
396+ GetValueAsImports (ffi, &thrower);
397+
398+ if (thrower.error ()) {
399+ auto maybe = resolver->Reject (context, Utils::ToLocal (thrower.Reify ()));
400+ CHECK_IMPLIES (!maybe.FromMaybe (false ),
401+ i_isolate->has_scheduled_exception ());
402+ return ;
403+ }
404+
405+ i::wasm::AsyncInstantiate (
406+ i_isolate, Utils::OpenHandle (*promise), module_obj, maybe_imports);
407+ return ;
428408 }
429- DCHECK (!module_promise.IsEmpty ());
430- DCHECK_NOT_NULL (instantiator);
431- // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
432- // We'll check for that in WebAssemblyInstantiateImpl.
433- Local<Value> data = args[1 ];
409+
410+ // We did not get a WasmModuleObject as input, we first have to compile the
411+ // input.
412+ ASSIGN (Function, async_compile, Function::New (context, WebAssemblyCompile));
413+ ASSIGN (Value, async_compile_retval,
414+ async_compile->Call (context, args.Holder (), 1 , &first_arg_value));
415+ promise = Local<Promise>::Cast (async_compile_retval);
416+ DCHECK (!promise.IsEmpty ());
434417 ASSIGN (Function, instantiate_impl,
435- Function::New (context, instantiator, data ));
436- ASSIGN (Promise, result, module_promise ->Then (context, instantiate_impl));
418+ Function::New (context, WebAssemblyInstantiateCallback, ffi ));
419+ ASSIGN (Promise, result, promise ->Then (context, instantiate_impl));
437420 args.GetReturnValue ().Set (result);
438421}
439422
0 commit comments