Skip to content

Commit fd9a332

Browse files
committed
bundle load & loadSync from @tybys/wasm-util
1 parent b4465e0 commit fd9a332

File tree

3 files changed

+6
-93
lines changed

3 files changed

+6
-93
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"tslib": "^2.4.0"
2828
},
2929
"devDependencies": {
30+
"@tybys/wasm-util": "^0.8.0",
3031
"@rollup/plugin-node-resolve": "^13.3.0",
3132
"@rollup/plugin-replace": "^5.0.2",
3233
"rollup": "^2.77.2",

packages/core/src/load.js

Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,4 @@
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'
912

923
function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
934
if (!napiModule) {
@@ -154,8 +65,9 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
15465
}
15566

15667
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
15971
/** @type {WebAssembly.Memory} */
16072
const memory = exportMemory ? source.instance.exports.memory : importMemory ? importObject.env.memory : undefined
16173
if (!memory) {

packages/test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "emnapi test",
66
"main": "index.js",
77
"devDependencies": {
8-
"@tybys/wasm-util": "^0.7.0",
8+
"@tybys/wasm-util": "^0.8.0",
99
"chalk": "^4.1.1",
1010
"cmake-js": "^7.1.1",
1111
"cross-env": "^7.0.3",

0 commit comments

Comments
 (0)