Commit 49712d8a authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Call AsyncInstantiate directly when instantiating a module object

WebAssembly.instantiate is polymorphic, it can either take a module
object as parameter, or a buffer source which should be compiled first.
To share code between the two implementations, the module object was
first passed to a promise (i.e. which is the result of compilation).
However, passing the module object to a promise has a side effect if
the module object has a then function. To avoid this side effect I
remove this code sharing and call AsyncInstantiate directly in case
the parameter is a module object.

R=mstarzinger@chromium.org

Bug: chromium:836141
Change-Id: I67b76d0d7761c5aeb2cf1deda45b6842e494eed4
Reviewed-on: https://chromium-review.googlesource.com/1025774Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52755}
parent ba864684
...@@ -346,25 +346,7 @@ MaybeLocal<Value> WebAssemblyInstantiateImpl(Isolate* isolate, ...@@ -346,25 +346,7 @@ MaybeLocal<Value> WebAssemblyInstantiateImpl(Isolate* isolate,
return Utils::ToLocal(instance_object.ToHandleChecked()); return Utils::ToLocal(instance_object.ToHandleChecked());
} }
// Entered as internal implementation detail of sync and async instantiate. void WebAssemblyInstantiateCallback(
// args[0] *must* be a WebAssembly.Module.
void WebAssemblyInstantiateImplCallback(
const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK_GE(args.Length(), 1);
v8::Isolate* isolate = args.GetIsolate();
MicrotasksScope does_not_run_microtasks(isolate,
MicrotasksScope::kDoNotRunMicrotasks);
HandleScope scope(args.GetIsolate());
Local<Value> module = args[0];
Local<Value> ffi = args.Data();
Local<Value> instance;
if (WebAssemblyInstantiateImpl(isolate, module, ffi).ToLocal(&instance)) {
args.GetReturnValue().Set(instance);
}
}
void WebAssemblyInstantiateToPairCallback(
const v8::FunctionCallbackInfo<v8::Value>& args) { const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK_GE(args.Length(), 1); DCHECK_GE(args.Length(), 1);
Isolate* isolate = args.GetIsolate(); Isolate* isolate = args.GetIsolate();
...@@ -453,7 +435,7 @@ void WebAssemblyInstantiateStreaming( ...@@ -453,7 +435,7 @@ void WebAssemblyInstantiateStreaming(
DCHECK(!module_promise.IsEmpty()); DCHECK(!module_promise.IsEmpty());
Local<Value> data = args[1]; Local<Value> data = args[1];
ASSIGN(Function, instantiate_impl, ASSIGN(Function, instantiate_impl,
Function::New(context, WebAssemblyInstantiateToPairCallback, data)); Function::New(context, WebAssemblyInstantiateCallback, data));
ASSIGN(Promise, result, module_promise->Then(context, instantiate_impl)); ASSIGN(Promise, result, module_promise->Then(context, instantiate_impl));
args.GetReturnValue().Set(result); args.GetReturnValue().Set(result);
} }
...@@ -475,10 +457,12 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -475,10 +457,12 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
Local<Context> context = isolate->GetCurrentContext(); Local<Context> context = isolate->GetCurrentContext();
ASSIGN(Promise::Resolver, resolver, Promise::Resolver::New(context)); ASSIGN(Promise::Resolver, resolver, Promise::Resolver::New(context));
Local<Promise> module_promise = resolver->GetPromise(); Local<Promise> promise = resolver->GetPromise();
args.GetReturnValue().Set(module_promise); args.GetReturnValue().Set(promise);
Local<Value> first_arg_value = args[0]; Local<Value> first_arg_value = args[0];
// If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
Local<Value> ffi = args[1];
i::Handle<i::Object> first_arg = Utils::OpenHandle(*first_arg_value); i::Handle<i::Object> first_arg = Utils::OpenHandle(*first_arg_value);
if (!first_arg->IsJSObject()) { if (!first_arg->IsJSObject()) {
thrower.TypeError( thrower.TypeError(
...@@ -489,26 +473,35 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -489,26 +473,35 @@ void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
return; return;
} }
FunctionCallback instantiator = nullptr;
if (first_arg->IsWasmModuleObject()) { if (first_arg->IsWasmModuleObject()) {
module_promise = resolver->GetPromise(); i::Handle<i::WasmModuleObject> module_obj =
if (!resolver->Resolve(context, first_arg_value).IsJust()) return; i::Handle<i::WasmModuleObject>::cast(first_arg);
instantiator = WebAssemblyInstantiateImplCallback; // If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
} else { i::MaybeHandle<i::JSReceiver> maybe_imports =
GetValueAsImports(ffi, &thrower);
if (thrower.error()) {
auto maybe = resolver->Reject(context, Utils::ToLocal(thrower.Reify()));
CHECK_IMPLIES(!maybe.FromMaybe(false),
i_isolate->has_scheduled_exception());
return;
}
i_isolate->wasm_engine()->AsyncInstantiate(
i_isolate, Utils::OpenHandle(*promise), module_obj, maybe_imports);
return;
}
// We did not get a WasmModuleObject as input, we first have to compile the
// input.
ASSIGN(Function, async_compile, Function::New(context, WebAssemblyCompile)); ASSIGN(Function, async_compile, Function::New(context, WebAssemblyCompile));
ASSIGN(Value, async_compile_retval, ASSIGN(Value, async_compile_retval,
async_compile->Call(context, args.Holder(), 1, &first_arg_value)); async_compile->Call(context, args.Holder(), 1, &first_arg_value));
module_promise = Local<Promise>::Cast(async_compile_retval); promise = Local<Promise>::Cast(async_compile_retval);
instantiator = WebAssemblyInstantiateToPairCallback; DCHECK(!promise.IsEmpty());
}
DCHECK(!module_promise.IsEmpty());
DCHECK_NOT_NULL(instantiator);
// If args.Length < 2, this will be undefined - see FunctionCallbackInfo.
// We'll check for that in WebAssemblyInstantiateImpl.
Local<Value> data = args[1];
ASSIGN(Function, instantiate_impl, ASSIGN(Function, instantiate_impl,
Function::New(context, instantiator, data)); Function::New(context, WebAssemblyInstantiateCallback, ffi));
ASSIGN(Promise, result, module_promise->Then(context, instantiate_impl)); ASSIGN(Promise, result, promise->Then(context, instantiate_impl));
args.GetReturnValue().Set(result); args.GetReturnValue().Set(result);
} }
......
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
load('test/mjsunit/wasm/wasm-constants.js');
load('test/mjsunit/wasm/wasm-module-builder.js');
const builder = new WasmModuleBuilder();
builder.addMemory(16, 32);
builder.addFunction("test", kSig_i_v).addBody([
kExprI32Const, 12, // i32.const 0
]);
let bla = 0;
let module = new WebAssembly.Module(builder.toBuffer());
module.then = () => {
// Use setTimeout to get out of the promise chain.
setTimeout(assertUnreachable);
};
WebAssembly.instantiate(module);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment