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

Add WASM native module loader #3

Open
wants to merge 1 commit into
base: feat/wasm-napi-port
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ if (isMainThread) {
// Set up methods on the process object for all threads
{
process.dlopen = rawMethods.dlopen;
process.wasmOpen = rawMethods.wasmOpen;
process.uptime = rawMethods.uptime;

// TODO(joyeecheung): either remove them or make them public
Expand Down
14 changes: 14 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,20 @@ Module._extensions['.json'] = function(module, filename) {
}
};

Module._extensions['.node.wasm'] = function(module, filename) {
const content = fs.readFileSync(filename);
const mod = new WebAssembly.Module(content);
let imports = WebAssembly.embedderBuiltins();
// TODO(ohadrau): Load WASI
imports.wasi_unstable = {
fd_close: () => 0,
fd_write: () => 0,
fd_seek: () => 0,
fd_fdstat_get: () => 0
}
const instance = new WebAssembly.Instance(mod, imports);
return process.wasmOpen(module, instance.exports.main);
}

// Native extension for .node
Module._extensions['.node'] = function(module, filename) {
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const extensionFormatMap = {
'.cjs': 'commonjs',
'.js': 'module',
'.json': 'json',
'.mjs': 'module'
'.mjs': 'module',
};

const legacyExtensionFormatMap = {
Expand All @@ -38,7 +38,8 @@ const legacyExtensionFormatMap = {
'.js': 'commonjs',
'.json': 'json',
'.mjs': 'module',
'.node': 'commonjs'
'.node': 'commonjs',
'.node.wasm': 'commonjs'
};

if (experimentalWasmModules)
Expand Down
12 changes: 11 additions & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ static const char* const EXTENSIONS[] = {
".cjs",
".js",
".json",
".node"
".node",
".node.wasm"
};

ModuleWrap::ModuleWrap(Environment* env,
Expand Down Expand Up @@ -681,6 +682,9 @@ Maybe<URL> LegacyMainResolve(const URL& pjson_url,
if (FileExists(guess = URL("./" + pcfg.main + ".node", pjson_url))) {
return Just(guess);
}
if (FileExists(guess = URL("./" + pcfg.main + ".node.wasm", pjson_url))) {
return Just(guess);
}
if (FileExists(guess = URL("./" + pcfg.main + "/index.js", pjson_url))) {
return Just(guess);
}
Expand All @@ -691,6 +695,9 @@ Maybe<URL> LegacyMainResolve(const URL& pjson_url,
if (FileExists(guess = URL("./" + pcfg.main + "/index.node", pjson_url))) {
return Just(guess);
}
if (FileExists(guess = URL("./" + pcfg.main + "/index.node.wasm", pjson_url))) {
return Just(guess);
}
// Fallthrough.
}
if (FileExists(guess = URL("./index.js", pjson_url))) {
Expand All @@ -703,6 +710,9 @@ Maybe<URL> LegacyMainResolve(const URL& pjson_url,
if (FileExists(guess = URL("./index.node", pjson_url))) {
return Just(guess);
}
if (FileExists(guess = URL("./index.node.wasm", pjson_url))) {
return Just(guess);
}
// Not found.
return Nothing<URL>();
}
Expand Down
80 changes: 80 additions & 0 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,86 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
// coverity[leaked_storage]
}

// WASMOpen is process.wasmOpen(module, instance, flags).
// Used to load 'module.node' dynamically shared objects.
//
// FIXME(bnoordhuis) Not multi-context ready. TBD how to resolve the conflict
// when two contexts try to load the same shared object. Maybe have a shadow
// cache that's a plain C list or hash table that's shared across contexts?
void WASMOpen(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
auto context = env->context();

uv_once(&init_modpending_once, InitModpendingOnce);
CHECK_NULL(uv_key_get(&thread_local_modpending));

if (args.Length() < 2) {
env->ThrowError("process.wasmOpen needs at least 2 arguments.");
return;
}

Local<Object> module;
Local<Object> exports;
Local<Value> exports_v;
if (!args[0]->ToObject(context).ToLocal(&module) ||
!module->Get(context, env->exports_string()).ToLocal(&exports_v) ||
!exports_v->ToObject(context).ToLocal(&exports)) {
return; // Exception pending.
}

Local<Object> mainFunction;
if (!args[1]->ToObject(context).ToLocal(&mainFunction) ||
!mainFunction->IsFunction()) {
return; // Exception pending.
}

int argc = 2;
Local<Value> argv[2];
argv[0] = v8::Integer::New(context->GetIsolate(), 0); // int argc
argv[1] = v8::Integer::New(context->GetIsolate(), 0); // char** argv
mainFunction->CallAsFunction(context, context->Global(), argc, argv);

// Objects containing v14 or later modules will have registered themselves
// on the pending list. Activate all of them now. At present, only one
// module per object is supported.
node_module* mp =
static_cast<node_module*>(uv_key_get(&thread_local_modpending));
uv_key_set(&thread_local_modpending, nullptr);

if (mp == nullptr || mp->nm_context_register_func == nullptr) {
env->ThrowError("Module did not self-register.");
return;
}

// -1 is used for N-API modules. WASM loading ONLY supports N-API modules.
if (mp->nm_version != -1) {
char errmsg[1024];
snprintf(errmsg,
sizeof(errmsg),
"The module '%s'"
"\nwas compiled against a different Node.js version using"
"\nNODE_MODULE_VERSION %d. This version of Node.js requires"
"\nNODE_MODULE_VERSION %d. Please try re-compiling or "
"re-installing\nthe module (for instance, using `npm rebuild` "
"or `npm install`).",
mp->nm_modname,
mp->nm_version,
NODE_MODULE_VERSION);

// NOTE: `mp` is allocated inside of Node's memory, so we should free it
free(mp);
env->ThrowError(errmsg);
return;
}
CHECK_EQ(mp->nm_flags & NM_F_BUILTIN, 0);

if (mp->nm_context_register_func != nullptr) {
mp->nm_context_register_func(exports, module, context, mp->nm_priv);
} else {
env->ThrowError("Module has no declared entry point.");
}
}

inline struct node_module* FindModule(struct node_module* list,
const char* name,
int flag) {
Expand Down
1 change: 1 addition & 0 deletions src/node_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void RegisterBuiltinModules();
void GetInternalBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
void GetLinkedBinding(const v8::FunctionCallbackInfo<v8::Value>& args);
void DLOpen(const v8::FunctionCallbackInfo<v8::Value>& args);
void WASMOpen(const v8::FunctionCallbackInfo<v8::Value>& args);

} // namespace binding

Expand Down
1 change: 1 addition & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ static void InitializeProcessMethods(Local<Object> target,

env->SetMethodNoSideEffect(target, "cwd", Cwd);
env->SetMethod(target, "dlopen", binding::DLOpen);
env->SetMethod(target, "wasmOpen", binding::WASMOpen);
env->SetMethod(target, "reallyExit", ReallyExit);
env->SetMethodNoSideEffect(target, "uptime", Uptime);
env->SetMethod(target, "patchProcessObject", PatchProcessObject);
Expand Down