Skip to content

Commit

Permalink
lib: add WebAssembly to primordials
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
ExE-Boss and aduh95 committed Sep 27, 2021
1 parent 92ecd9f commit 5788f16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -359,6 +359,7 @@ module.exports = {
MessagePort: 'readable',
TextEncoder: 'readable',
TextDecoder: 'readable',
WebAssembly: 'readonly',
queueMicrotask: 'readable',
globalThis: 'readable',
btoa: 'readable',
Expand Down
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Expand Up @@ -152,6 +152,7 @@ rules:
into: Safe
- name: WeakSet
into: Safe
- name: WebAssembly
globals:
Intl: false
# Parameters passed to internal modules
Expand Down
23 changes: 15 additions & 8 deletions lib/internal/freeze_intrinsics.js
Expand Up @@ -118,6 +118,14 @@ const {
WeakRefPrototype,
WeakSet,
WeakSetPrototype,
WebAssembly,
WebAssemblyModulePrototype,
WebAssemblyInstancePrototype,
WebAssemblyTablePrototype,
WebAssemblyMemoryPrototype,
WebAssemblyCompileErrorPrototype,
WebAssemblyLinkErrorPrototype,
WebAssemblyRuntimeErrorPrototype,
decodeURI,
decodeURIComponent,
encodeURI,
Expand All @@ -129,7 +137,6 @@ const {
Atomics,
Intl,
SharedArrayBuffer,
WebAssembly
} = globalThis;

module.exports = function() {
Expand Down Expand Up @@ -217,13 +224,13 @@ module.exports = function() {

// Other APIs / Web Compatibility
console.Console.prototype,
WebAssembly.Module.prototype,
WebAssembly.Instance.prototype,
WebAssembly.Table.prototype,
WebAssembly.Memory.prototype,
WebAssembly.CompileError.prototype,
WebAssembly.LinkError.prototype,
WebAssembly.RuntimeError.prototype,
WebAssemblyModulePrototype,
WebAssemblyInstancePrototype,
WebAssemblyTablePrototype,
WebAssemblyMemoryPrototype,
WebAssemblyCompileErrorPrototype,
WebAssemblyLinkErrorPrototype,
WebAssemblyRuntimeErrorPrototype,
];
const intrinsics = [
// 10.2.4.1 ThrowTypeError
Expand Down
14 changes: 9 additions & 5 deletions lib/internal/modules/esm/translators.js
Expand Up @@ -17,7 +17,11 @@ const {
StringPrototypeSlice,
StringPrototypeStartsWith,
SyntaxErrorPrototype,
globalThis: { WebAssembly },
WebAssembly,
WebAssemblyCompile,
WebAssemblyInstance,
WebAssemblyModuleExports,
WebAssemblyModuleImports,
} = primordials;

let _TYPES = null;
Expand Down Expand Up @@ -345,21 +349,21 @@ translators.set('wasm', async function(url, source) {

let compiled;
try {
compiled = await WebAssembly.compile(source);
compiled = await WebAssemblyCompile(source);
} catch (err) {
err.message = errPath(url) + ': ' + err.message;
throw err;
}

const imports =
ArrayPrototypeMap(WebAssembly.Module.imports(compiled),
ArrayPrototypeMap(WebAssemblyModuleImports(compiled),
({ module }) => module);
const exports =
ArrayPrototypeMap(WebAssembly.Module.exports(compiled),
ArrayPrototypeMap(WebAssemblyModuleExports(compiled),
({ name }) => name);

return createDynamicModule(imports, exports, url, (reflect) => {
const { exports } = new WebAssembly.Instance(compiled, reflect.imports);
const { exports } = new WebAssemblyInstance(compiled, reflect.imports);
for (const expt of ObjectKeys(exports))
reflect.exports[expt].set(exports[expt]);
}).module;
Expand Down

0 comments on commit 5788f16

Please sign in to comment.