Skip to content

Commit 2afc05e

Browse files
ofrobotsMylesBorins
authored andcommitted
deps: V8: backport 49712d8a from upstream
Original commit message: [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. [email protected] Bug: chromium:836141 Change-Id: I67b76d0d7761c5aeb2cf1deda45b6842e494eed4 Reviewed-on: https://chromium-review.googlesource.com/1025774 Reviewed-by: Michael Starzinger <[email protected]> Commit-Queue: Andreas Haas <[email protected]> Cr-Commit-Position: refs/heads/master@{#52755} PR-URL: #21334 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 4be5252 commit 2afc05e

File tree

3 files changed

+54
-50
lines changed

3 files changed

+54
-50
lines changed

deps/v8/include/v8-version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define V8_MAJOR_VERSION 6
1212
#define V8_MINOR_VERSION 2
1313
#define V8_BUILD_NUMBER 414
14-
#define V8_PATCH_LEVEL 60
14+
#define V8_PATCH_LEVEL 61
1515

1616
// Use 1 for candidates and 0 otherwise.
1717
// (Boolean macro values are not supported by all preprocessors.)

deps/v8/src/wasm/wasm-js.cc

+32-49
Original file line numberDiff line numberDiff line change
@@ -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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
load('test/mjsunit/wasm/wasm-constants.js');
6+
load('test/mjsunit/wasm/wasm-module-builder.js');
7+
8+
const builder = new WasmModuleBuilder();
9+
builder.addMemory(16, 32);
10+
builder.addFunction("test", kSig_i_v).addBody([
11+
kExprI32Const, 12, // i32.const 0
12+
]);
13+
14+
let bla = 0;
15+
let module = new WebAssembly.Module(builder.toBuffer());
16+
module.then = () => {
17+
// Use setTimeout to get out of the promise chain.
18+
setTimeout(assertUnreachable);
19+
};
20+
21+
WebAssembly.instantiate(module);

0 commit comments

Comments
 (0)