Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: add instantiateStreaming support #22332

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ struct PackageConfig {
V(message_port_string, "messagePort") \
V(message_port_constructor_string, "MessagePort") \
V(minttl_string, "minttl") \
V(module_string, "Module") \
V(modulus_string, "modulus") \
V(name_string, "name") \
V(netmask_string, "netmask") \
Expand Down Expand Up @@ -288,6 +289,7 @@ struct PackageConfig {
V(valid_from_string, "valid_from") \
V(valid_to_string, "valid_to") \
V(value_string, "value") \
V(webassembly_string, "WebAssembly") \
V(verify_error_string, "verifyError") \
V(version_string, "version") \
V(weight_string, "weight") \
Expand Down
32 changes: 32 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3732,6 +3732,37 @@ bool AllowWasmCodeGenerationCallback(
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
}

void WasmCreateModule(const FunctionCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info.GetIsolate());
Local<Context> context = env->context();
Local<Value> buffer = info[0];
CHECK(!buffer.IsEmpty());

Local<Object> wasm = Local<Object>::Cast(
context->Global()->Get(context, env->webassembly_string())
.ToLocalChecked());
Local<Function> module = Local<Function>::Cast(wasm->Get(context,
env->module_string()).ToLocalChecked());
Local<Value> args[] = {buffer};
Local<Value> module_instance = module->NewInstance(context, 1, args)
.ToLocalChecked();
info.GetReturnValue().Set(module_instance);
}

void WasmInstantiateStreamingCallback(const FunctionCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info.GetIsolate());
Local<Context> context = env->context();
CHECK(!info[0]->IsUndefined());
CHECK(info[0]->IsPromise());
Local<Promise> buffer_promise = info[0].As<Promise>();

Local<Function> create_module = Function::New(context, WasmCreateModule)
.ToLocalChecked();
Local<Promise> module_promise = buffer_promise->Then(context, create_module)
.ToLocalChecked();
info.GetReturnValue().Set(module_promise);
}

Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
Isolate::CreateParams params;
params.array_buffer_allocator = allocator;
Expand All @@ -3748,6 +3779,7 @@ Isolate* NewIsolate(ArrayBufferAllocator* allocator) {
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
isolate->SetFatalErrorHandler(OnFatalError);
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
isolate->SetWasmCompileStreamingCallback(WasmInstantiateStreamingCallback);

return isolate;
}
Expand Down
Binary file added test/fixtures/instantiate_streaming.wasm
Binary file not shown.
9 changes: 9 additions & 0 deletions test/fixtures/instantiate_streaming.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; $ wat2wasm instantiate_streaming.wat -o instantiate_streaming.wasm
(module
(func $add (import "test" "add") (param i32) (param i32) (result i32))
(func (export "add") (param i32) (param i32) (result i32)
get_local 0
get_local 1
call $add
)
)
23 changes: 23 additions & 0 deletions test/parallel/test-wasm-instantiatestreaming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// This test verifies that instantiateStreaming can be used with node.

require('../common');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const fs = require('fs');

const promise = fs.promises.readFile(
fixtures.path('instantiate_streaming.wasm'));

// Using an import object just to verify that this works without anything
// needed to be specifically implemented for it to work.
const importObject = {
test: {
add: (arg1, arg2) => arg1 + arg2
},
}

WebAssembly.instantiateStreaming(promise, importObject).then((results) => {
assert.strictEqual(results.instance.exports.add(2, 3), 5);
});