Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
test: better output for test-report-uv-handles.js
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#27479
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
gengjiawen authored and MylesBorins committed May 1, 2019
1 parent 97ee1c9 commit fafd301
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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, {
Expand Down
21 changes: 21 additions & 0 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
4 changes: 4 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions test/es-module/test-esm-wasm.mjs
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion test/report/test-report-uv-handles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down

0 comments on commit fafd301

Please sign in to comment.