Skip to content

Commit f016a82

Browse files
ExE-Bossaduh95
andcommitted
lib: add WebAssembly to primordials
Co-authored-by: Antoine du Hamel <[email protected]>
1 parent 8065883 commit f016a82

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ module.exports = {
304304
MessagePort: 'readable',
305305
TextEncoder: 'readable',
306306
TextDecoder: 'readable',
307+
WebAssembly: 'readonly',
307308
queueMicrotask: 'readable',
308309
globalThis: 'readable',
309310
},

lib/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ rules:
8383
into: Safe
8484
- name: WeakSet
8585
into: Safe
86+
- name: WebAssembly
8687
globals:
8788
Intl: false
8889
# Parameters passed to internal modules

lib/internal/freeze_intrinsics.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
2020
// https://github.com/tc39/proposal-ses/blob/e5271cc42a257a05dcae2fd94713ed2f46c08620/shim/src/freeze.js
2121

22-
/* global WebAssembly, SharedArrayBuffer, console */
22+
/* global SharedArrayBuffer, console */
2323
'use strict';
2424

2525
const {
@@ -107,6 +107,14 @@ const {
107107
WeakMapPrototype,
108108
WeakSet,
109109
WeakSetPrototype,
110+
WebAssembly,
111+
WebAssemblyModulePrototype,
112+
WebAssemblyInstancePrototype,
113+
WebAssemblyTablePrototype,
114+
WebAssemblyMemoryPrototype,
115+
WebAssemblyCompileErrorPrototype,
116+
WebAssemblyLinkErrorPrototype,
117+
WebAssemblyRuntimeErrorPrototype,
110118
} = primordials;
111119

112120
module.exports = function() {
@@ -192,13 +200,13 @@ module.exports = function() {
192200
// Other APIs / Web Compatibility
193201
console.Console.prototype,
194202
BigIntPrototype,
195-
WebAssembly.Module.prototype,
196-
WebAssembly.Instance.prototype,
197-
WebAssembly.Table.prototype,
198-
WebAssembly.Memory.prototype,
199-
WebAssembly.CompileError.prototype,
200-
WebAssembly.LinkError.prototype,
201-
WebAssembly.RuntimeError.prototype,
203+
WebAssemblyModulePrototype,
204+
WebAssemblyInstancePrototype,
205+
WebAssemblyTablePrototype,
206+
WebAssemblyMemoryPrototype,
207+
WebAssemblyCompileErrorPrototype,
208+
WebAssemblyLinkErrorPrototype,
209+
WebAssemblyRuntimeErrorPrototype,
202210
SharedArrayBuffer.prototype
203211
];
204212
const intrinsics = [

lib/internal/modules/esm/translators.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
/* global WebAssembly */
4-
53
const {
64
ArrayPrototypeMap,
75
Boolean,
@@ -19,6 +17,11 @@ const {
1917
StringPrototypeSplit,
2018
StringPrototypeStartsWith,
2119
SyntaxErrorPrototype,
20+
WebAssembly,
21+
WebAssemblyCompile,
22+
WebAssemblyInstance,
23+
WebAssemblyModuleExports,
24+
WebAssemblyModuleImports,
2225
} = primordials;
2326

2427
let _TYPES = null;
@@ -361,21 +364,21 @@ translators.set('wasm', async function(url) {
361364
debug(`Translating WASMModule ${url}`);
362365
let compiled;
363366
try {
364-
compiled = await WebAssembly.compile(source);
367+
compiled = await WebAssemblyCompile(source);
365368
} catch (err) {
366369
err.message = errPath(url) + ': ' + err.message;
367370
throw err;
368371
}
369372

370373
const imports =
371-
ArrayPrototypeMap(WebAssembly.Module.imports(compiled),
374+
ArrayPrototypeMap(WebAssemblyModuleImports(compiled),
372375
({ module }) => module);
373376
const exports =
374-
ArrayPrototypeMap(WebAssembly.Module.exports(compiled),
377+
ArrayPrototypeMap(WebAssemblyModuleExports(compiled),
375378
({ name }) => name);
376379

377380
return createDynamicModule(imports, exports, url, (reflect) => {
378-
const { exports } = new WebAssembly.Instance(compiled, reflect.imports);
381+
const { exports } = new WebAssemblyInstance(compiled, reflect.imports);
379382
for (const expt of ObjectKeys(exports))
380383
reflect.exports[expt].set(exports[expt]);
381384
}).module;

lib/internal/per_context/primordials.js

+18
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,23 @@ primordials.SafeWeakSet = makeSafe(
197197
copyPrototype(original.prototype, primordials, `${name}Prototype`);
198198
});
199199

200+
if (typeof WebAssembly !== 'undefined') {
201+
primordials.WebAssembly = WebAssembly;
202+
203+
// Create copies of WebAssembly objects.
204+
// Refs: https://webassembly.github.io/spec/js-api/index.html#idl-index
205+
// Refs: https://heycam.github.io/webidl/#idl-namespaces
206+
Object.getOwnPropertyNames(WebAssembly).forEach((propName) => {
207+
const original = WebAssembly[propName];
208+
const name = `WebAssembly${getNewKey(propName)}`;
209+
primordials[name] = original;
210+
211+
copyPropsRenamed(original, primordials, name);
212+
if ('prototype' in original) {
213+
copyPrototype(original.prototype, primordials, `${name}Prototype`);
214+
}
215+
});
216+
}
217+
200218
Object.setPrototypeOf(primordials, null);
201219
Object.freeze(primordials);

0 commit comments

Comments
 (0)