@tybys/wasm-util 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +193 -193
- package/dist/tsdoc-metadata.json +1 -1
- package/dist/wasm-util.d.ts +13 -7
- package/dist/wasm-util.esm-bundler.js +2639 -2604
- package/dist/wasm-util.esm.js +2639 -2604
- package/dist/wasm-util.esm.min.js +1 -1
- package/dist/wasm-util.js +2639 -2603
- package/dist/wasm-util.min.js +1 -1
- package/lib/cjs/asyncify.js +157 -157
- package/lib/cjs/index.js +12 -12
- package/lib/cjs/jspi.js +45 -45
- package/lib/cjs/load.js +96 -85
- package/lib/cjs/memfs.js +2689 -0
- package/lib/cjs/memory.js +34 -34
- package/lib/cjs/wasi/error.js +102 -102
- package/lib/cjs/wasi/fd.js +267 -267
- package/lib/cjs/wasi/fs.js +2 -2
- package/lib/cjs/wasi/index.js +193 -168
- package/lib/cjs/wasi/path.js +173 -173
- package/lib/cjs/wasi/preview1.js +1478 -1478
- package/lib/cjs/wasi/rights.js +139 -139
- package/lib/cjs/wasi/types.js +219 -219
- package/lib/cjs/wasi/util.js +121 -121
- package/lib/cjs/webassembly.js +12 -12
- package/lib/mjs/asyncify.mjs +153 -153
- package/lib/mjs/index.mjs +9 -9
- package/lib/mjs/jspi.mjs +39 -39
- package/lib/mjs/load.mjs +89 -78
- package/lib/mjs/memfs.mjs +2688 -0
- package/lib/mjs/memory.mjs +28 -28
- package/lib/mjs/wasi/error.mjs +97 -97
- package/lib/mjs/wasi/fd.mjs +256 -256
- package/lib/mjs/wasi/fs.mjs +1 -1
- package/lib/mjs/wasi/index.mjs +188 -164
- package/lib/mjs/wasi/path.mjs +168 -168
- package/lib/mjs/wasi/preview1.mjs +1474 -1474
- package/lib/mjs/wasi/rights.mjs +134 -134
- package/lib/mjs/wasi/types.mjs +216 -216
- package/lib/mjs/wasi/util.mjs +108 -108
- package/lib/mjs/webassembly.mjs +9 -9
- package/package.json +58 -58
package/lib/mjs/jspi.mjs
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { wrapInstanceExports } from "./wasi/util.mjs";
|
|
2
|
-
import { _WebAssembly } from "./webassembly.mjs";
|
|
3
|
-
function checkWebAssemblyFunction() {
|
|
4
|
-
const WebAssemblyFunction = _WebAssembly.Function;
|
|
5
|
-
if (typeof WebAssemblyFunction !== 'function') {
|
|
6
|
-
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
7
|
-
' If you are using V8 based browser like Chrome, try to specify' +
|
|
8
|
-
' --js-
|
|
9
|
-
}
|
|
10
|
-
return WebAssemblyFunction;
|
|
11
|
-
}
|
|
12
|
-
/** @public */
|
|
13
|
-
export function wrapAsyncImport(f, parameterType, returnType) {
|
|
14
|
-
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
15
|
-
if (typeof f !== 'function') {
|
|
16
|
-
throw new TypeError('Function required');
|
|
17
|
-
}
|
|
18
|
-
const parameters = parameterType.slice(0);
|
|
19
|
-
parameters.unshift('externref');
|
|
20
|
-
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
21
|
-
}
|
|
22
|
-
/** @public */
|
|
23
|
-
export function wrapAsyncExport(f) {
|
|
24
|
-
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
25
|
-
if (typeof f !== 'function') {
|
|
26
|
-
throw new TypeError('Function required');
|
|
27
|
-
}
|
|
28
|
-
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
29
|
-
}
|
|
30
|
-
/** @public */
|
|
31
|
-
export function wrapExports(exports, needWrap) {
|
|
32
|
-
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
33
|
-
let ignore = typeof exportValue !== 'function';
|
|
34
|
-
if (Array.isArray(needWrap)) {
|
|
35
|
-
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
36
|
-
}
|
|
37
|
-
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
1
|
+
import { wrapInstanceExports } from "./wasi/util.mjs";
|
|
2
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
3
|
+
function checkWebAssemblyFunction() {
|
|
4
|
+
const WebAssemblyFunction = _WebAssembly.Function;
|
|
5
|
+
if (typeof WebAssemblyFunction !== 'function') {
|
|
6
|
+
throw new Error('WebAssembly.Function is not supported in this environment.' +
|
|
7
|
+
' If you are using V8 based browser like Chrome, try to specify' +
|
|
8
|
+
' --js-flags="--wasm-staging --experimental-wasm-stack-switching"');
|
|
9
|
+
}
|
|
10
|
+
return WebAssemblyFunction;
|
|
11
|
+
}
|
|
12
|
+
/** @public */
|
|
13
|
+
export function wrapAsyncImport(f, parameterType, returnType) {
|
|
14
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
15
|
+
if (typeof f !== 'function') {
|
|
16
|
+
throw new TypeError('Function required');
|
|
17
|
+
}
|
|
18
|
+
const parameters = parameterType.slice(0);
|
|
19
|
+
parameters.unshift('externref');
|
|
20
|
+
return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
|
|
21
|
+
}
|
|
22
|
+
/** @public */
|
|
23
|
+
export function wrapAsyncExport(f) {
|
|
24
|
+
const WebAssemblyFunction = checkWebAssemblyFunction();
|
|
25
|
+
if (typeof f !== 'function') {
|
|
26
|
+
throw new TypeError('Function required');
|
|
27
|
+
}
|
|
28
|
+
return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
|
|
29
|
+
}
|
|
30
|
+
/** @public */
|
|
31
|
+
export function wrapExports(exports, needWrap) {
|
|
32
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
33
|
+
let ignore = typeof exportValue !== 'function';
|
|
34
|
+
if (Array.isArray(needWrap)) {
|
|
35
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
36
|
+
}
|
|
37
|
+
return ignore ? exportValue : wrapAsyncExport(exportValue);
|
|
38
|
+
});
|
|
39
|
+
}
|
package/lib/mjs/load.mjs
CHANGED
|
@@ -1,78 +1,89 @@
|
|
|
1
|
-
import { _WebAssembly } from "./webassembly.mjs";
|
|
2
|
-
import { Asyncify } from "./asyncify.mjs";
|
|
3
|
-
function validateImports(imports) {
|
|
4
|
-
if (imports && typeof imports !== 'object') {
|
|
5
|
-
throw new TypeError('imports must be an object or undefined');
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
function fetchWasm(urlOrBuffer, imports) {
|
|
9
|
-
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
10
|
-
return _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
11
|
-
}
|
|
12
|
-
return fetch(urlOrBuffer)
|
|
13
|
-
.then(response => response.arrayBuffer())
|
|
14
|
-
.then(buffer => _WebAssembly.instantiate(buffer, imports));
|
|
15
|
-
}
|
|
16
|
-
/** @public */
|
|
17
|
-
export function load(
|
|
18
|
-
validateImports(imports);
|
|
19
|
-
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
20
|
-
let source;
|
|
21
|
-
if (
|
|
22
|
-
return _WebAssembly.instantiate(
|
|
23
|
-
}
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
source =
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
return
|
|
78
|
-
}
|
|
1
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
2
|
+
import { Asyncify } from "./asyncify.mjs";
|
|
3
|
+
function validateImports(imports) {
|
|
4
|
+
if (imports && typeof imports !== 'object') {
|
|
5
|
+
throw new TypeError('imports must be an object or undefined');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
function fetchWasm(urlOrBuffer, imports) {
|
|
9
|
+
if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
|
|
10
|
+
return _WebAssembly.instantiate(urlOrBuffer, imports);
|
|
11
|
+
}
|
|
12
|
+
return fetch(urlOrBuffer)
|
|
13
|
+
.then(response => response.arrayBuffer())
|
|
14
|
+
.then(buffer => _WebAssembly.instantiate(buffer, imports));
|
|
15
|
+
}
|
|
16
|
+
/** @public */
|
|
17
|
+
export function load(wasmInput, imports) {
|
|
18
|
+
validateImports(imports);
|
|
19
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
20
|
+
let source;
|
|
21
|
+
if (wasmInput instanceof ArrayBuffer || ArrayBuffer.isView(wasmInput)) {
|
|
22
|
+
return _WebAssembly.instantiate(wasmInput, imports);
|
|
23
|
+
}
|
|
24
|
+
if (wasmInput instanceof _WebAssembly.Module) {
|
|
25
|
+
return _WebAssembly.instantiate(wasmInput, imports).then((instance) => {
|
|
26
|
+
return { instance, module: wasmInput };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (typeof wasmInput !== 'string' && !(wasmInput instanceof URL)) {
|
|
30
|
+
throw new TypeError('Invalid source');
|
|
31
|
+
}
|
|
32
|
+
if (typeof _WebAssembly.instantiateStreaming === 'function') {
|
|
33
|
+
let responsePromise;
|
|
34
|
+
try {
|
|
35
|
+
responsePromise = fetch(wasmInput);
|
|
36
|
+
source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => {
|
|
37
|
+
return fetchWasm(wasmInput, imports);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (_) {
|
|
41
|
+
source = fetchWasm(wasmInput, imports);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
source = fetchWasm(wasmInput, imports);
|
|
46
|
+
}
|
|
47
|
+
return source;
|
|
48
|
+
}
|
|
49
|
+
/** @public */
|
|
50
|
+
export function asyncifyLoad(asyncify, urlOrBuffer, imports) {
|
|
51
|
+
validateImports(imports);
|
|
52
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
53
|
+
const asyncifyHelper = new Asyncify();
|
|
54
|
+
imports = asyncifyHelper.wrapImports(imports);
|
|
55
|
+
return load(urlOrBuffer, imports).then(source => {
|
|
56
|
+
var _a;
|
|
57
|
+
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
58
|
+
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** @public */
|
|
62
|
+
export function loadSync(wasmInput, imports) {
|
|
63
|
+
validateImports(imports);
|
|
64
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
65
|
+
let module;
|
|
66
|
+
if ((wasmInput instanceof ArrayBuffer) || ArrayBuffer.isView(wasmInput)) {
|
|
67
|
+
module = new _WebAssembly.Module(wasmInput);
|
|
68
|
+
}
|
|
69
|
+
else if (wasmInput instanceof WebAssembly.Module) {
|
|
70
|
+
module = wasmInput;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
throw new TypeError('Invalid source');
|
|
74
|
+
}
|
|
75
|
+
const instance = new _WebAssembly.Instance(module, imports);
|
|
76
|
+
const source = { instance, module };
|
|
77
|
+
return source;
|
|
78
|
+
}
|
|
79
|
+
/** @public */
|
|
80
|
+
export function asyncifyLoadSync(asyncify, buffer, imports) {
|
|
81
|
+
var _a;
|
|
82
|
+
validateImports(imports);
|
|
83
|
+
imports = imports !== null && imports !== void 0 ? imports : {};
|
|
84
|
+
const asyncifyHelper = new Asyncify();
|
|
85
|
+
imports = asyncifyHelper.wrapImports(imports);
|
|
86
|
+
const source = loadSync(buffer, imports);
|
|
87
|
+
const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
|
|
88
|
+
return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
|
|
89
|
+
}
|