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 Dec 10, 2020
1 parent 8065883 commit f016a82
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -304,6 +304,7 @@ module.exports = {
MessagePort: 'readable',
TextEncoder: 'readable',
TextDecoder: 'readable',
WebAssembly: 'readonly',
queueMicrotask: 'readable',
globalThis: 'readable',
},
Expand Down
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Expand Up @@ -83,6 +83,7 @@ rules:
into: Safe
- name: WeakSet
into: Safe
- name: WebAssembly
globals:
Intl: false
# Parameters passed to internal modules
Expand Down
24 changes: 16 additions & 8 deletions lib/internal/freeze_intrinsics.js
Expand Up @@ -19,7 +19,7 @@
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
// https://github.com/tc39/proposal-ses/blob/e5271cc42a257a05dcae2fd94713ed2f46c08620/shim/src/freeze.js

/* global WebAssembly, SharedArrayBuffer, console */
/* global SharedArrayBuffer, console */
'use strict';

const {
Expand Down Expand Up @@ -107,6 +107,14 @@ const {
WeakMapPrototype,
WeakSet,
WeakSetPrototype,
WebAssembly,
WebAssemblyModulePrototype,
WebAssemblyInstancePrototype,
WebAssemblyTablePrototype,
WebAssemblyMemoryPrototype,
WebAssemblyCompileErrorPrototype,
WebAssemblyLinkErrorPrototype,
WebAssemblyRuntimeErrorPrototype,
} = primordials;

module.exports = function() {
Expand Down Expand Up @@ -192,13 +200,13 @@ module.exports = function() {
// Other APIs / Web Compatibility
console.Console.prototype,
BigIntPrototype,
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,
SharedArrayBuffer.prototype
];
const intrinsics = [
Expand Down
15 changes: 9 additions & 6 deletions lib/internal/modules/esm/translators.js
@@ -1,7 +1,5 @@
'use strict';

/* global WebAssembly */

const {
ArrayPrototypeMap,
Boolean,
Expand All @@ -19,6 +17,11 @@ const {
StringPrototypeSplit,
StringPrototypeStartsWith,
SyntaxErrorPrototype,
WebAssembly,
WebAssemblyCompile,
WebAssemblyInstance,
WebAssemblyModuleExports,
WebAssemblyModuleImports,
} = primordials;

let _TYPES = null;
Expand Down Expand Up @@ -361,21 +364,21 @@ translators.set('wasm', async function(url) {
debug(`Translating WASMModule ${url}`);
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
18 changes: 18 additions & 0 deletions lib/internal/per_context/primordials.js
Expand Up @@ -197,5 +197,23 @@ primordials.SafeWeakSet = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

if (typeof WebAssembly !== 'undefined') {
primordials.WebAssembly = WebAssembly;

// Create copies of WebAssembly objects.
// Refs: https://webassembly.github.io/spec/js-api/index.html#idl-index
// Refs: https://heycam.github.io/webidl/#idl-namespaces
Object.getOwnPropertyNames(WebAssembly).forEach((propName) => {
const original = WebAssembly[propName];
const name = `WebAssembly${getNewKey(propName)}`;
primordials[name] = original;

copyPropsRenamed(original, primordials, name);
if ('prototype' in original) {
copyPrototype(original.prototype, primordials, `${name}Prototype`);
}
});
}

Object.setPrototypeOf(primordials, null);
Object.freeze(primordials);

0 comments on commit f016a82

Please sign in to comment.