@@ -330,42 +330,30 @@ MaybeLocal<Value> WebAssemblyInstantiateImpl(Isolate* isolate,
330330 i::MaybeHandle<i::Object> instance_object;
331331 {
332332 ScheduledErrorThrower thrower (i_isolate, " WebAssembly Instantiation" );
333+
334+ // TODO(ahaas): These checks on the module should not be necessary here They
335+ // are just a workaround for https://crbug.com/837417.
336+ i::Handle<i::Object> module_obj = Utils::OpenHandle (*module );
337+ if (!module_obj->IsWasmModuleObject ()) {
338+ thrower.TypeError (" Argument 0 must be a WebAssembly.Module object" );
339+ return {};
340+ }
341+
333342 i::MaybeHandle<i::JSReceiver> maybe_imports =
334343 GetValueAsImports (ffi, &thrower);
335344 if (thrower.error ()) return {};
336345
337- i::Handle<i::WasmModuleObject> module_obj =
338- i::Handle<i::WasmModuleObject>::cast (
339- Utils::OpenHandle (Object::Cast (*module )));
340346 instance_object = i_isolate->wasm_engine ()->SyncInstantiate (
341- i_isolate, &thrower, module_obj, maybe_imports ,
342- i::MaybeHandle<i::JSArrayBuffer>());
347+ i_isolate, &thrower, i::Handle<i::WasmModuleObject>:: cast ( module_obj) ,
348+ maybe_imports, i::MaybeHandle<i::JSArrayBuffer>());
343349 }
344350
345351 DCHECK_EQ (instance_object.is_null (), i_isolate->has_scheduled_exception ());
346352 if (instance_object.is_null ()) return {};
347353 return Utils::ToLocal (instance_object.ToHandleChecked ());
348354}
349355
350- // Entered as internal implementation detail of sync and async instantiate.
351- // args[0] *must* be a WebAssembly.Module.
352- void WebAssemblyInstantiateImplCallback (
353- const v8::FunctionCallbackInfo<v8::Value>& args) {
354- DCHECK_GE (args.Length (), 1 );
355- v8::Isolate* isolate = args.GetIsolate ();
356- MicrotasksScope does_not_run_microtasks (isolate,
357- MicrotasksScope::kDoNotRunMicrotasks );
358-
359- HandleScope scope (args.GetIsolate ());
360- Local<Value> module = args[0 ];
361- Local<Value> ffi = args.Data ();
362- Local<Value> instance;
363- if (WebAssemblyInstantiateImpl (isolate, module , ffi).ToLocal (&instance)) {
364- args.GetReturnValue ().Set (instance);
365- }
366- }
367-
368- void WebAssemblyInstantiateToPairCallback (
356+ void WebAssemblyInstantiateCallback (
369357 const v8::FunctionCallbackInfo<v8::Value>& args) {
370358 DCHECK_GE (args.Length (), 1 );
371359 Isolate* isolate = args.GetIsolate ();
@@ -454,7 +442,7 @@ void WebAssemblyInstantiateStreaming(
454442 DCHECK (!module_promise.IsEmpty ());
455443 Local<Value> data = args[1 ];
456444 ASSIGN (Function, instantiate_impl,
457- Function::New (context, WebAssemblyInstantiateToPairCallback , data));
445+ Function::New (context, WebAssemblyInstantiateCallback , data));
458446 ASSIGN (Promise, result, module_promise->Then (context, instantiate_impl));
459447 args.GetReturnValue ().Set (result);
460448}
@@ -476,10 +464,12 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
476464 Local<Context> context = isolate->GetCurrentContext ();
477465
478466 ASSIGN (Promise::Resolver, resolver, Promise::Resolver::New (context));
479- Local<Promise> module_promise = resolver->GetPromise ();
480- args.GetReturnValue ().Set (module_promise );
467+ Local<Promise> promise = resolver->GetPromise ();
468+ args.GetReturnValue ().Set (promise );
481469
482470 Local<Value> first_arg_value = args[0 ];
471+ // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
472+ Local<Value> ffi = args[1 ];
483473 i::Handle<i::Object> first_arg = Utils::OpenHandle (*first_arg_value);
484474 if (!first_arg->IsJSObject ()) {
485475 thrower.TypeError (
@@ -490,26 +480,35 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
490480 return ;
491481 }
492482
493- FunctionCallback instantiator = nullptr ;
494483 if (first_arg->IsWasmModuleObject ()) {
495- module_promise = resolver->GetPromise ();
496- if (!resolver->Resolve (context, first_arg_value).IsJust ()) return ;
497- instantiator = WebAssemblyInstantiateImplCallback;
498- } else {
499- ASSIGN (Function, async_compile, Function::New (context, WebAssemblyCompile));
500- ASSIGN (Value, async_compile_retval,
501- async_compile->Call (context, args.Holder (), 1 , &first_arg_value));
502- module_promise = Local<Promise>::Cast (async_compile_retval);
503- instantiator = WebAssemblyInstantiateToPairCallback;
484+ i::Handle<i::WasmModuleObject> module_obj =
485+ i::Handle<i::WasmModuleObject>::cast (first_arg);
486+ // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
487+ i::MaybeHandle<i::JSReceiver> maybe_imports =
488+ GetValueAsImports (ffi, &thrower);
489+
490+ if (thrower.error ()) {
491+ auto maybe = resolver->Reject (context, Utils::ToLocal (thrower.Reify ()));
492+ CHECK_IMPLIES (!maybe.FromMaybe (false ),
493+ i_isolate->has_scheduled_exception ());
494+ return ;
495+ }
496+
497+ i_isolate->wasm_engine ()->AsyncInstantiate (
498+ i_isolate, Utils::OpenHandle (*promise), module_obj, maybe_imports);
499+ return ;
504500 }
505- DCHECK (!module_promise.IsEmpty ());
506- DCHECK_NOT_NULL (instantiator);
507- // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
508- // We'll check for that in WebAssemblyInstantiateImpl.
509- Local<Value> data = args[1 ];
501+
502+ // We did not get a WasmModuleObject as input, we first have to compile the
503+ // input.
504+ ASSIGN (Function, async_compile, Function::New (context, WebAssemblyCompile));
505+ ASSIGN (Value, async_compile_retval,
506+ async_compile->Call (context, args.Holder (), 1 , &first_arg_value));
507+ promise = Local<Promise>::Cast (async_compile_retval);
508+ DCHECK (!promise.IsEmpty ());
510509 ASSIGN (Function, instantiate_impl,
511- Function::New (context, instantiator, data ));
512- ASSIGN (Promise, result, module_promise ->Then (context, instantiate_impl));
510+ Function::New (context, WebAssemblyInstantiateCallback, ffi ));
511+ ASSIGN (Promise, result, promise ->Then (context, instantiate_impl));
513512 args.GetReturnValue ().Set (result);
514513}
515514
0 commit comments