Skip to content

Commit

Permalink
wasm: add instantiateStreaming support
Browse files Browse the repository at this point in the history
This commit adds InstantiateStreaming support to allow for some
compatability with Web browser environments.

Refs: nodejs#21130
  • Loading branch information
danbev committed Aug 15, 2018
1 parent 0257fd7 commit d6a55af
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/env.h
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
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
@@ -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
@@ -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);
});

0 comments on commit d6a55af

Please sign in to comment.