From fafd3017f5cd505b0fc7d5db6fe7a991c70fde90 Mon Sep 17 00:00:00 2001 From: gengjiawen Date: Tue, 30 Apr 2019 00:27:20 +0800 Subject: [PATCH] test: better output for test-report-uv-handles.js PR-URL: https://github.com/nodejs/node/pull/27479 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig --- lib/internal/modules/esm/default_resolve.js | 12 +++++++++++- lib/internal/modules/esm/translators.js | 21 +++++++++++++++++++++ src/node_options.cc | 4 ++++ src/node_options.h | 1 + test/es-module/test-esm-wasm.mjs | 9 +++++++++ test/report/test-report-uv-handles.js | 2 +- 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/es-module/test-esm-wasm.mjs diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js index a83cf9c675..f17316a708 100644 --- a/lib/internal/modules/esm/default_resolve.js +++ b/lib/internal/modules/esm/default_resolve.js @@ -10,7 +10,7 @@ const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalJsonModules = getOptionValue('--experimental-json-modules'); const typeFlag = getOptionValue('--input-type'); - +const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); const { resolve: moduleWrapResolve, getPackageType } = internalBinding('module_wrap'); const { pathToFileURL, fileURLToPath } = require('internal/url'); @@ -44,6 +44,16 @@ const legacyExtensionFormatMap = { '.node': 'commonjs' }; +if (experimentalWasmModules) { + // This is a total hack + Object.assign(extensionFormatMap, { + '.wasm': 'wasm' + }); + Object.assign(legacyExtensionFormatMap, { + '.wasm': 'wasm' + }); +} + if (experimentalJsonModules) { // This is a total hack Object.assign(extensionFormatMap, { diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 72350fb2b2..b03037630b 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -141,3 +141,24 @@ translators.set('json', async function jsonStrategy(url) { reflect.exports.default.set(module.exports); }); }); + +// Strategy for loading a wasm module +translators.set('wasm', async function(url) { + const pathname = fileURLToPath(url); + const buffer = await readFileAsync(pathname); + debug(`Translating WASMModule ${url}`); + let result, keys; + try { + WebAssembly.validate(buffer); + result = await WebAssembly.instantiate(buffer, {}); + keys = Object.keys(result.instance.exports); + } catch (err) { + err.message = pathname + ': ' + err.message; + throw err; + } + return createDynamicModule([...keys, 'default'], url, (reflect) => { + for (const key of keys) + reflect.exports[key].set(result.instance.exports[key]); + reflect.exports.default.set(result.instance.exports); + }); +}); diff --git a/src/node_options.cc b/src/node_options.cc index adc6f61586..98e9f435f0 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -264,6 +264,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "experimental ES Module support and caching modules", &EnvironmentOptions::experimental_modules, kAllowedInEnvironment); + AddOption("--experimental-wasm-modules", + "experimental ES Module support for webassembly modules", + &EnvironmentOptions::experimental_wasm_modules, + kAllowedInEnvironment); AddOption("--experimental-policy", "use the specified file as a " "security policy", diff --git a/src/node_options.h b/src/node_options.h index 3b2513a183..06c07b811e 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -94,6 +94,7 @@ class EnvironmentOptions : public Options { bool experimental_json_modules = false; bool experimental_modules = false; std::string es_module_specifier_resolution; + bool experimental_wasm_modules = false; std::string module_type; std::string experimental_policy; bool experimental_repl_await = false; diff --git a/test/es-module/test-esm-wasm.mjs b/test/es-module/test-esm-wasm.mjs new file mode 100644 index 0000000000..76a43c708a --- /dev/null +++ b/test/es-module/test-esm-wasm.mjs @@ -0,0 +1,9 @@ +// Flags: --experimental-modules --experimental-wasm-modules +/* eslint-disable node-core/required-modules */ +import wasmMod from '../fixtures/simple.wasm' +import {add} from '../fixtures/simple.wasm'; +import {strictEqual} from 'assert'; + +strictEqual(wasmMod.add(10, 20), 30); +strictEqual(add(10, 20), 30); +strictEqual(wasmMod.add, add); diff --git a/test/report/test-report-uv-handles.js b/test/report/test-report-uv-handles.js index 5167716894..2061920432 100644 --- a/test/report/test-report-uv-handles.js +++ b/test/report/test-report-uv-handles.js @@ -85,7 +85,7 @@ if (process.argv[2] === 'child') { const report_msg = 'Report files were written: unexpectedly'; child.stdout.on('data', (chunk) => { stdout += chunk; }); child.on('exit', common.mustCall((code, signal) => { - assert.deepStrictEqual(code, 0, 'Process exited unexpectedly with code' + + assert.deepStrictEqual(code, 0, 'Process exited unexpectedly with code: ' + `${code}`); assert.deepStrictEqual(signal, null, 'Process should have exited cleanly,' + ` but did not: ${signal}`);