-
Notifications
You must be signed in to change notification settings - Fork 1
/
jest-runtime-npm-29.1.2-567b60ffd5.patch
70 lines (65 loc) · 2.41 KB
/
jest-runtime-npm-29.1.2-567b60ffd5.patch
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
diff --git a/build/index.js b/build/index.js
index 30d7657b066f12b1002f5d802a367c50e9819d79..4cba43bfb7d5d2f1c2a3aafdb1e58e265f6aea79 100644
--- a/build/index.js
+++ b/build/index.js
@@ -496,7 +496,7 @@ class Runtime {
} // unstable as it should be replaced by https://github.com/nodejs/modules/issues/393, and we don't want people to use it
unstable_shouldLoadAsEsm(path) {
- return _jestResolve().default.unstable_shouldLoadAsEsm(
+ return path.endsWith('wasm') || _jestResolve().default.unstable_shouldLoadAsEsm(
path,
this._config.extensionsToTreatAsEsm
);
@@ -534,6 +534,16 @@ class Runtime {
'Promise initialization should be sync - please report this bug to Jest!'
);
+ if (modulePath.endsWith('wasm')) {
+ const wasm = this._importWasmModule(modulePath, context);
+
+ this._esmoduleRegistry.set(cacheKey, wasm);
+
+ transformResolve();
+ return wasm;
+
+ }
+
if (this._resolver.isCoreModule(modulePath)) {
const core = this._importCoreModule(modulePath, context);
@@ -1716,6 +1726,39 @@ class Runtime {
return require(moduleWithoutNodePrefix);
}
+ async _importWasmModule(moduleName, context) {
+ const wasmModule = await WebAssembly.compile(fs().readFileSync(moduleName));
+
+ const exports = WebAssembly.Module.exports(wasmModule);
+ const imports = WebAssembly.Module.imports(wasmModule);
+
+ const moduleLookup = {};
+ for (const { module } of imports) {
+ if (moduleLookup[module] === undefined) {
+ moduleLookup[module] = await this.linkAndEvaluateModule(await this.resolveModule(module, moduleName, context))
+ }
+ }
+
+ const syntheticModule = new (_vm().SyntheticModule)(exports.map(({ name }) => name), function() {
+ const importsObject = {};
+ for (const { module, name } of imports) {
+ if (!importsObject[module]) {
+ importsObject[module] = {}
+ }
+ importsObject[module][name] = moduleLookup[module].namespace[name];
+ }
+ const wasmInstance = new WebAssembly.Instance(wasmModule, importsObject);
+ for (const { name } of exports) {
+ this.setExport(name, wasmInstance.exports[name]);
+ }
+ }, {
+ context,
+ identifier: moduleName
+ });
+
+ return syntheticModule;
+ }
+
_importCoreModule(moduleName, context) {
const required = this._requireCoreModule(moduleName, true);