forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-module-hooks-import-wasm.mjs
More file actions
34 lines (30 loc) · 1.09 KB
/
Copy pathtest-module-hooks-import-wasm.mjs
File metadata and controls
34 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// This tests that module.registerHooks() can be used to support unknown formats, like
// import(wasm)
import { mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import { registerHooks, createRequire } from 'node:module';
import { readFileSync } from 'node:fs';
registerHooks({
load: mustCall((url, context, nextLoad) => {
assert.match(url, /simple\.wasm$/);
const source =
`const buf = Buffer.from([${Array.from(readFileSync(new URL(url))).join(',')}]);
const compiled = new WebAssembly.Module(buf);
const { exports } = new WebAssembly.Instance(compiled);
export default exports;
export { exports as 'module.exports' };
`;
return {
shortCircuit: true,
source,
format: 'module',
};
}),
});
// Checks that it works with require.
const require = createRequire(import.meta.url);
const { add } = require('../fixtures/simple.wasm');
assert.strictEqual(add(1, 2), 3);
// Checks that it works with import.
const { default: { add: add2 } } = await import('../fixtures/simple.wasm');
assert.strictEqual(add2(1, 2), 3);