|
1 | | -/* eslint-disable camelcase */ |
2 | | - |
3 | | -/** @type {typeof WebAssembly} */ |
4 | | -const _WebAssembly = typeof WebAssembly !== 'undefined' |
5 | | - ? WebAssembly |
6 | | - : typeof WXWebAssembly !== 'undefined' |
7 | | - // eslint-disable-next-line no-undef |
8 | | - ? WXWebAssembly |
9 | | - : undefined |
10 | | - |
11 | | -function validateImports (imports) { |
12 | | - if (imports && typeof imports !== 'object') { |
13 | | - throw new TypeError('imports must be an object or undefined') |
14 | | - } |
15 | | -} |
16 | | - |
17 | | -function fetchWasm (strOrUrl, imports) { |
18 | | - if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') { |
19 | | - return _WebAssembly.instantiate(strOrUrl, imports) |
20 | | - } |
21 | | - return fetch(strOrUrl) |
22 | | - .then(response => response.arrayBuffer()) |
23 | | - .then(buffer => _WebAssembly.instantiate(buffer, imports)) |
24 | | -} |
25 | | - |
26 | | -/** |
27 | | - * @param {string | URL | BufferSource | WebAssembly.Module} wasmInput |
28 | | - * @param {WebAssembly.Imports=} imports |
29 | | - * @returns {Promise<WebAssembly.WebAssemblyInstantiatedSource>} |
30 | | - */ |
31 | | -function load (wasmInput, imports) { |
32 | | - validateImports(imports) |
33 | | - imports = imports != null ? imports : {} |
34 | | - |
35 | | - if ((wasmInput instanceof ArrayBuffer) || ArrayBuffer.isView(wasmInput)) { |
36 | | - return _WebAssembly.instantiate(wasmInput, imports) |
37 | | - } |
38 | | - |
39 | | - if (wasmInput instanceof _WebAssembly.Module) { |
40 | | - return _WebAssembly.instantiate(wasmInput, imports).then((instance) => { |
41 | | - return { instance, module: wasmInput } |
42 | | - }) |
43 | | - } |
44 | | - |
45 | | - if (typeof wasmInput !== 'string' && !(wasmInput instanceof URL)) { |
46 | | - throw new TypeError('Invalid source') |
47 | | - } |
48 | | - |
49 | | - let source |
50 | | - if (typeof _WebAssembly.instantiateStreaming === 'function') { |
51 | | - let responsePromise |
52 | | - try { |
53 | | - responsePromise = fetch(wasmInput) |
54 | | - source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => { |
55 | | - return fetchWasm(wasmInput, imports) |
56 | | - }) |
57 | | - } catch (_) { |
58 | | - source = fetchWasm(wasmInput, imports) |
59 | | - } |
60 | | - } else { |
61 | | - source = fetchWasm(wasmInput, imports) |
62 | | - } |
63 | | - return source |
64 | | -} |
65 | | - |
66 | | -/** |
67 | | - * @param {BufferSource | WebAssembly.Module} wasmInput |
68 | | - * @param {WebAssembly.Imports=} imports |
69 | | - * @returns {WebAssembly.WebAssemblyInstantiatedSource} |
70 | | - */ |
71 | | -function loadSync (wasmInput, imports) { |
72 | | - validateImports(imports) |
73 | | - imports = imports != null ? imports : {} |
74 | | - |
75 | | - /** @type {WebAssembly.Module} */ |
76 | | - let module |
77 | | - |
78 | | - if ((wasmInput instanceof ArrayBuffer) || ArrayBuffer.isView(wasmInput)) { |
79 | | - module = new _WebAssembly.Module(wasmInput) |
80 | | - } else if (wasmInput instanceof WebAssembly.Module) { |
81 | | - module = wasmInput |
82 | | - } else { |
83 | | - throw new TypeError('Invalid source') |
84 | | - } |
85 | | - |
86 | | - const instance = new _WebAssembly.Instance(module, imports) |
87 | | - const source = { instance, module } |
88 | | - |
89 | | - return source |
90 | | -} |
| 1 | +import { load, loadSync } from '@tybys/wasm-util' |
91 | 2 |
|
92 | 3 | function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) { |
93 | 4 | if (!napiModule) { |
@@ -154,8 +65,9 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) { |
154 | 65 | } |
155 | 66 |
|
156 | 67 | let instance = source.instance |
157 | | - const exportMemory = source.instance.exports.memory instanceof _WebAssembly.Memory |
158 | | - const importMemory = importObject.env.memory instanceof _WebAssembly.Memory |
| 68 | + |
| 69 | + const exportMemory = 'memory' in source.instance.exports |
| 70 | + const importMemory = 'memory' in importObject.env |
159 | 71 | /** @type {WebAssembly.Memory} */ |
160 | 72 | const memory = exportMemory ? source.instance.exports.memory : importMemory ? importObject.env.memory : undefined |
161 | 73 | if (!memory) { |
|
0 commit comments