@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/cjs/wasi/util.js
CHANGED
|
@@ -1,122 +1,122 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sleepBreakIf = exports.postMsg = exports.isMainThread = exports.wrapInstanceExports = exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
|
|
4
|
-
/* eslint-disable spaced-comment */
|
|
5
|
-
function validateObject(value, name) {
|
|
6
|
-
if (value === null || typeof value !== 'object') {
|
|
7
|
-
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.validateObject = validateObject;
|
|
11
|
-
function validateArray(value, name) {
|
|
12
|
-
if (!Array.isArray(value)) {
|
|
13
|
-
throw new TypeError(`${name} must be an array. Received ${value === null ? 'null' : typeof value}`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.validateArray = validateArray;
|
|
17
|
-
function validateBoolean(value, name) {
|
|
18
|
-
if (typeof value !== 'boolean') {
|
|
19
|
-
throw new TypeError(`${name} must be a boolean. Received ${value === null ? 'null' : typeof value}`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.validateBoolean = validateBoolean;
|
|
23
|
-
function validateString(value, name) {
|
|
24
|
-
if (typeof value !== 'string') {
|
|
25
|
-
throw new TypeError(`${name} must be a string. Received ${value === null ? 'null' : typeof value}`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.validateString = validateString;
|
|
29
|
-
function validateFunction(value, name) {
|
|
30
|
-
if (typeof value !== 'function') {
|
|
31
|
-
throw new TypeError(`${name} must be a function. Received ${value === null ? 'null' : typeof value}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.validateFunction = validateFunction;
|
|
35
|
-
function validateUndefined(value, name) {
|
|
36
|
-
if (value !== undefined) {
|
|
37
|
-
throw new TypeError(`${name} must be undefined. Received ${value === null ? 'null' : typeof value}`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.validateUndefined = validateUndefined;
|
|
41
|
-
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
|
|
42
|
-
if (typeof value !== 'number') {
|
|
43
|
-
throw new TypeError(`${name} must be a number. Received ${value === null ? 'null' : typeof value}`);
|
|
44
|
-
}
|
|
45
|
-
if (!Number.isInteger(value)) {
|
|
46
|
-
throw new RangeError(`${name} must be a integer.`);
|
|
47
|
-
}
|
|
48
|
-
if (value < min || value > max) {
|
|
49
|
-
throw new RangeError(`${name} must be >= ${min} && <= ${max}. Received ${value}`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
exports.validateInt32 = validateInt32;
|
|
53
|
-
function isPromiseLike(obj) {
|
|
54
|
-
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
55
|
-
}
|
|
56
|
-
exports.isPromiseLike = isPromiseLike;
|
|
57
|
-
function wrapInstanceExports(exports, mapFn) {
|
|
58
|
-
const newExports = Object.create(null);
|
|
59
|
-
Object.keys(exports).forEach(name => {
|
|
60
|
-
const exportValue = exports[name];
|
|
61
|
-
Object.defineProperty(newExports, name, {
|
|
62
|
-
enumerable: true,
|
|
63
|
-
value: mapFn(exportValue, name)
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
return newExports;
|
|
67
|
-
}
|
|
68
|
-
exports.wrapInstanceExports = wrapInstanceExports;
|
|
69
|
-
const _require = /*#__PURE__*/ (function () {
|
|
70
|
-
let nativeRequire;
|
|
71
|
-
if (typeof __webpack_public_path__ !== 'undefined') {
|
|
72
|
-
nativeRequire = (function () {
|
|
73
|
-
return typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined;
|
|
74
|
-
})();
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
nativeRequire = (function () {
|
|
78
|
-
return typeof __webpack_public_path__ !== 'undefined' ? (typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined) : (typeof require !== 'undefined' ? require : undefined);
|
|
79
|
-
})();
|
|
80
|
-
}
|
|
81
|
-
return nativeRequire;
|
|
82
|
-
})();
|
|
83
|
-
exports.isMainThread = (function () {
|
|
84
|
-
let worker_threads;
|
|
85
|
-
try {
|
|
86
|
-
worker_threads = _require('worker_threads');
|
|
87
|
-
}
|
|
88
|
-
catch (_) { }
|
|
89
|
-
if (!worker_threads) {
|
|
90
|
-
return typeof importScripts === 'undefined';
|
|
91
|
-
}
|
|
92
|
-
return worker_threads.isMainThread;
|
|
93
|
-
})();
|
|
94
|
-
exports.postMsg = exports.isMainThread
|
|
95
|
-
? () => { }
|
|
96
|
-
: /*#__PURE__*/ (function () {
|
|
97
|
-
let worker_threads;
|
|
98
|
-
try {
|
|
99
|
-
worker_threads = _require('worker_threads');
|
|
100
|
-
}
|
|
101
|
-
catch (_) { }
|
|
102
|
-
if (!worker_threads) {
|
|
103
|
-
return postMessage;
|
|
104
|
-
}
|
|
105
|
-
return function postMessage(data) {
|
|
106
|
-
worker_threads.parentPort.postMessage({ data });
|
|
107
|
-
};
|
|
108
|
-
})();
|
|
109
|
-
function sleepBreakIf(delay, breakIf) {
|
|
110
|
-
const start = Date.now();
|
|
111
|
-
const end = start + delay;
|
|
112
|
-
let ret = false;
|
|
113
|
-
while (Date.now() < end) {
|
|
114
|
-
if (breakIf()) {
|
|
115
|
-
ret = true;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return ret;
|
|
120
|
-
}
|
|
121
|
-
exports.sleepBreakIf = sleepBreakIf;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleepBreakIf = exports.postMsg = exports.isMainThread = exports.wrapInstanceExports = exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
|
|
4
|
+
/* eslint-disable spaced-comment */
|
|
5
|
+
function validateObject(value, name) {
|
|
6
|
+
if (value === null || typeof value !== 'object') {
|
|
7
|
+
throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.validateObject = validateObject;
|
|
11
|
+
function validateArray(value, name) {
|
|
12
|
+
if (!Array.isArray(value)) {
|
|
13
|
+
throw new TypeError(`${name} must be an array. Received ${value === null ? 'null' : typeof value}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.validateArray = validateArray;
|
|
17
|
+
function validateBoolean(value, name) {
|
|
18
|
+
if (typeof value !== 'boolean') {
|
|
19
|
+
throw new TypeError(`${name} must be a boolean. Received ${value === null ? 'null' : typeof value}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.validateBoolean = validateBoolean;
|
|
23
|
+
function validateString(value, name) {
|
|
24
|
+
if (typeof value !== 'string') {
|
|
25
|
+
throw new TypeError(`${name} must be a string. Received ${value === null ? 'null' : typeof value}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.validateString = validateString;
|
|
29
|
+
function validateFunction(value, name) {
|
|
30
|
+
if (typeof value !== 'function') {
|
|
31
|
+
throw new TypeError(`${name} must be a function. Received ${value === null ? 'null' : typeof value}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.validateFunction = validateFunction;
|
|
35
|
+
function validateUndefined(value, name) {
|
|
36
|
+
if (value !== undefined) {
|
|
37
|
+
throw new TypeError(`${name} must be undefined. Received ${value === null ? 'null' : typeof value}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.validateUndefined = validateUndefined;
|
|
41
|
+
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
|
|
42
|
+
if (typeof value !== 'number') {
|
|
43
|
+
throw new TypeError(`${name} must be a number. Received ${value === null ? 'null' : typeof value}`);
|
|
44
|
+
}
|
|
45
|
+
if (!Number.isInteger(value)) {
|
|
46
|
+
throw new RangeError(`${name} must be a integer.`);
|
|
47
|
+
}
|
|
48
|
+
if (value < min || value > max) {
|
|
49
|
+
throw new RangeError(`${name} must be >= ${min} && <= ${max}. Received ${value}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.validateInt32 = validateInt32;
|
|
53
|
+
function isPromiseLike(obj) {
|
|
54
|
+
return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
|
|
55
|
+
}
|
|
56
|
+
exports.isPromiseLike = isPromiseLike;
|
|
57
|
+
function wrapInstanceExports(exports, mapFn) {
|
|
58
|
+
const newExports = Object.create(null);
|
|
59
|
+
Object.keys(exports).forEach(name => {
|
|
60
|
+
const exportValue = exports[name];
|
|
61
|
+
Object.defineProperty(newExports, name, {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
value: mapFn(exportValue, name)
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
return newExports;
|
|
67
|
+
}
|
|
68
|
+
exports.wrapInstanceExports = wrapInstanceExports;
|
|
69
|
+
const _require = /*#__PURE__*/ (function () {
|
|
70
|
+
let nativeRequire;
|
|
71
|
+
if (typeof __webpack_public_path__ !== 'undefined') {
|
|
72
|
+
nativeRequire = (function () {
|
|
73
|
+
return typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined;
|
|
74
|
+
})();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
nativeRequire = (function () {
|
|
78
|
+
return typeof __webpack_public_path__ !== 'undefined' ? (typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : undefined) : (typeof require !== 'undefined' ? require : undefined);
|
|
79
|
+
})();
|
|
80
|
+
}
|
|
81
|
+
return nativeRequire;
|
|
82
|
+
})();
|
|
83
|
+
exports.isMainThread = (function () {
|
|
84
|
+
let worker_threads;
|
|
85
|
+
try {
|
|
86
|
+
worker_threads = _require('worker_threads');
|
|
87
|
+
}
|
|
88
|
+
catch (_) { }
|
|
89
|
+
if (!worker_threads) {
|
|
90
|
+
return typeof importScripts === 'undefined';
|
|
91
|
+
}
|
|
92
|
+
return worker_threads.isMainThread;
|
|
93
|
+
})();
|
|
94
|
+
exports.postMsg = exports.isMainThread
|
|
95
|
+
? () => { }
|
|
96
|
+
: /*#__PURE__*/ (function () {
|
|
97
|
+
let worker_threads;
|
|
98
|
+
try {
|
|
99
|
+
worker_threads = _require('worker_threads');
|
|
100
|
+
}
|
|
101
|
+
catch (_) { }
|
|
102
|
+
if (!worker_threads) {
|
|
103
|
+
return postMessage;
|
|
104
|
+
}
|
|
105
|
+
return function postMessage(data) {
|
|
106
|
+
worker_threads.parentPort.postMessage({ data });
|
|
107
|
+
};
|
|
108
|
+
})();
|
|
109
|
+
function sleepBreakIf(delay, breakIf) {
|
|
110
|
+
const start = Date.now();
|
|
111
|
+
const end = start + delay;
|
|
112
|
+
let ret = false;
|
|
113
|
+
while (Date.now() < end) {
|
|
114
|
+
if (breakIf()) {
|
|
115
|
+
ret = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return ret;
|
|
120
|
+
}
|
|
121
|
+
exports.sleepBreakIf = sleepBreakIf;
|
|
122
122
|
//# sourceMappingURL=util.js.map
|
package/lib/cjs/webassembly.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports._WebAssembly = void 0;
|
|
4
|
-
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
5
|
-
? WebAssembly
|
|
6
|
-
: typeof WXWebAssembly !== 'undefined'
|
|
7
|
-
? WXWebAssembly
|
|
8
|
-
: undefined;
|
|
9
|
-
exports._WebAssembly = _WebAssembly;
|
|
10
|
-
if (!_WebAssembly) {
|
|
11
|
-
throw new Error('WebAssembly is not supported in this environment');
|
|
12
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports._WebAssembly = void 0;
|
|
4
|
+
const _WebAssembly = typeof WebAssembly !== 'undefined'
|
|
5
|
+
? WebAssembly
|
|
6
|
+
: typeof WXWebAssembly !== 'undefined'
|
|
7
|
+
? WXWebAssembly
|
|
8
|
+
: undefined;
|
|
9
|
+
exports._WebAssembly = _WebAssembly;
|
|
10
|
+
if (!_WebAssembly) {
|
|
11
|
+
throw new Error('WebAssembly is not supported in this environment');
|
|
12
|
+
}
|
|
13
13
|
//# sourceMappingURL=webassembly.js.map
|
package/lib/mjs/asyncify.mjs
CHANGED
|
@@ -1,153 +1,153 @@
|
|
|
1
|
-
import { _WebAssembly } from "./webassembly.mjs";
|
|
2
|
-
import { isPromiseLike, wrapInstanceExports } from "./wasi/util.mjs";
|
|
3
|
-
const ignoreNames = [
|
|
4
|
-
'asyncify_get_state',
|
|
5
|
-
'asyncify_start_rewind',
|
|
6
|
-
'asyncify_start_unwind',
|
|
7
|
-
'asyncify_stop_rewind',
|
|
8
|
-
'asyncify_stop_unwind'
|
|
9
|
-
];
|
|
10
|
-
// const wrappedExports = new WeakMap<WebAssembly.Exports, WebAssembly.Exports>()
|
|
11
|
-
var AsyncifyState;
|
|
12
|
-
(function (AsyncifyState) {
|
|
13
|
-
AsyncifyState[AsyncifyState["NONE"] = 0] = "NONE";
|
|
14
|
-
AsyncifyState[AsyncifyState["UNWINDING"] = 1] = "UNWINDING";
|
|
15
|
-
AsyncifyState[AsyncifyState["REWINDING"] = 2] = "REWINDING";
|
|
16
|
-
})(AsyncifyState || (AsyncifyState = {}));
|
|
17
|
-
function tryAllocate(instance, wasm64, size, mallocName) {
|
|
18
|
-
if (typeof instance.exports[mallocName] !== 'function' || size <= 0) {
|
|
19
|
-
return {
|
|
20
|
-
wasm64,
|
|
21
|
-
dataPtr: 16,
|
|
22
|
-
start: wasm64 ? 32 : 24,
|
|
23
|
-
end: 1024
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
const malloc = instance.exports[mallocName];
|
|
27
|
-
const dataPtr = wasm64 ? Number(malloc(BigInt(16) + BigInt(size))) : malloc(8 + size);
|
|
28
|
-
if (dataPtr === 0) {
|
|
29
|
-
throw new Error('Allocate asyncify data failed');
|
|
30
|
-
}
|
|
31
|
-
return wasm64
|
|
32
|
-
? { wasm64, dataPtr, start: dataPtr + 16, end: dataPtr + 16 + size }
|
|
33
|
-
: { wasm64, dataPtr, start: dataPtr + 8, end: dataPtr + 8 + size };
|
|
34
|
-
}
|
|
35
|
-
/** @public */
|
|
36
|
-
export class Asyncify {
|
|
37
|
-
constructor() {
|
|
38
|
-
this.value = undefined;
|
|
39
|
-
this.exports = undefined;
|
|
40
|
-
this.dataPtr = 0;
|
|
41
|
-
}
|
|
42
|
-
init(memory, instance, options) {
|
|
43
|
-
var _a, _b;
|
|
44
|
-
if (this.exports) {
|
|
45
|
-
throw new Error('Asyncify has been initialized');
|
|
46
|
-
}
|
|
47
|
-
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
48
|
-
throw new TypeError('Require WebAssembly.Memory object');
|
|
49
|
-
}
|
|
50
|
-
const exports = instance.exports;
|
|
51
|
-
for (let i = 0; i < ignoreNames.length; ++i) {
|
|
52
|
-
if (typeof exports[ignoreNames[i]] !== 'function') {
|
|
53
|
-
throw new TypeError('Invalid asyncify wasm');
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
let address;
|
|
57
|
-
const wasm64 = Boolean(options.wasm64);
|
|
58
|
-
if (!options.tryAllocate) {
|
|
59
|
-
address = {
|
|
60
|
-
wasm64,
|
|
61
|
-
dataPtr: 16,
|
|
62
|
-
start: wasm64 ? 32 : 24,
|
|
63
|
-
end: 1024
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
if (options.tryAllocate === true) {
|
|
68
|
-
address = tryAllocate(instance, wasm64, 4096, 'malloc');
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
address = tryAllocate(instance, wasm64, (_a = options.tryAllocate.size) !== null && _a !== void 0 ? _a : 4096, (_b = options.tryAllocate.name) !== null && _b !== void 0 ? _b : 'malloc');
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
this.dataPtr = address.dataPtr;
|
|
75
|
-
if (wasm64) {
|
|
76
|
-
new BigInt64Array(memory.buffer, this.dataPtr).set([BigInt(address.start), BigInt(address.end)]);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
80
|
-
}
|
|
81
|
-
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
82
|
-
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
83
|
-
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
84
|
-
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
85
|
-
return asyncifiedInstance;
|
|
86
|
-
}
|
|
87
|
-
assertState() {
|
|
88
|
-
if (this.exports.asyncify_get_state() !== AsyncifyState.NONE) {
|
|
89
|
-
throw new Error('Asyncify state error');
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
wrapImportFunction(f) {
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
94
|
-
const _this = this;
|
|
95
|
-
return (function () {
|
|
96
|
-
// eslint-disable-next-line no-unreachable-loop
|
|
97
|
-
while (_this.exports.asyncify_get_state() === AsyncifyState.REWINDING) {
|
|
98
|
-
_this.exports.asyncify_stop_rewind();
|
|
99
|
-
return _this.value;
|
|
100
|
-
}
|
|
101
|
-
_this.assertState();
|
|
102
|
-
const v = f.apply(this, arguments);
|
|
103
|
-
if (!isPromiseLike(v))
|
|
104
|
-
return v;
|
|
105
|
-
_this.exports.asyncify_start_unwind(_this.dataPtr);
|
|
106
|
-
_this.value = v;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
wrapImports(imports) {
|
|
110
|
-
const importObject = {};
|
|
111
|
-
Object.keys(imports).forEach(k => {
|
|
112
|
-
const mod = imports[k];
|
|
113
|
-
const newModule = {};
|
|
114
|
-
Object.keys(mod).forEach(name => {
|
|
115
|
-
const importValue = mod[name];
|
|
116
|
-
if (typeof importValue === 'function') {
|
|
117
|
-
newModule[name] = this.wrapImportFunction(importValue);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
newModule[name] = importValue;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
importObject[k] = newModule;
|
|
124
|
-
});
|
|
125
|
-
return importObject;
|
|
126
|
-
}
|
|
127
|
-
wrapExportFunction(f) {
|
|
128
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
129
|
-
const _this = this;
|
|
130
|
-
return (async function () {
|
|
131
|
-
_this.assertState();
|
|
132
|
-
let ret = f.apply(this, arguments);
|
|
133
|
-
while (_this.exports.asyncify_get_state() === AsyncifyState.UNWINDING) {
|
|
134
|
-
_this.exports.asyncify_stop_unwind();
|
|
135
|
-
_this.value = await _this.value;
|
|
136
|
-
_this.assertState();
|
|
137
|
-
_this.exports.asyncify_start_rewind(_this.dataPtr);
|
|
138
|
-
ret = f.call(this);
|
|
139
|
-
}
|
|
140
|
-
_this.assertState();
|
|
141
|
-
return ret;
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
wrapExports(exports, needWrap) {
|
|
145
|
-
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
146
|
-
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
147
|
-
if (Array.isArray(needWrap)) {
|
|
148
|
-
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
149
|
-
}
|
|
150
|
-
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
1
|
+
import { _WebAssembly } from "./webassembly.mjs";
|
|
2
|
+
import { isPromiseLike, wrapInstanceExports } from "./wasi/util.mjs";
|
|
3
|
+
const ignoreNames = [
|
|
4
|
+
'asyncify_get_state',
|
|
5
|
+
'asyncify_start_rewind',
|
|
6
|
+
'asyncify_start_unwind',
|
|
7
|
+
'asyncify_stop_rewind',
|
|
8
|
+
'asyncify_stop_unwind'
|
|
9
|
+
];
|
|
10
|
+
// const wrappedExports = new WeakMap<WebAssembly.Exports, WebAssembly.Exports>()
|
|
11
|
+
var AsyncifyState;
|
|
12
|
+
(function (AsyncifyState) {
|
|
13
|
+
AsyncifyState[AsyncifyState["NONE"] = 0] = "NONE";
|
|
14
|
+
AsyncifyState[AsyncifyState["UNWINDING"] = 1] = "UNWINDING";
|
|
15
|
+
AsyncifyState[AsyncifyState["REWINDING"] = 2] = "REWINDING";
|
|
16
|
+
})(AsyncifyState || (AsyncifyState = {}));
|
|
17
|
+
function tryAllocate(instance, wasm64, size, mallocName) {
|
|
18
|
+
if (typeof instance.exports[mallocName] !== 'function' || size <= 0) {
|
|
19
|
+
return {
|
|
20
|
+
wasm64,
|
|
21
|
+
dataPtr: 16,
|
|
22
|
+
start: wasm64 ? 32 : 24,
|
|
23
|
+
end: 1024
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const malloc = instance.exports[mallocName];
|
|
27
|
+
const dataPtr = wasm64 ? Number(malloc(BigInt(16) + BigInt(size))) : malloc(8 + size);
|
|
28
|
+
if (dataPtr === 0) {
|
|
29
|
+
throw new Error('Allocate asyncify data failed');
|
|
30
|
+
}
|
|
31
|
+
return wasm64
|
|
32
|
+
? { wasm64, dataPtr, start: dataPtr + 16, end: dataPtr + 16 + size }
|
|
33
|
+
: { wasm64, dataPtr, start: dataPtr + 8, end: dataPtr + 8 + size };
|
|
34
|
+
}
|
|
35
|
+
/** @public */
|
|
36
|
+
export class Asyncify {
|
|
37
|
+
constructor() {
|
|
38
|
+
this.value = undefined;
|
|
39
|
+
this.exports = undefined;
|
|
40
|
+
this.dataPtr = 0;
|
|
41
|
+
}
|
|
42
|
+
init(memory, instance, options) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
if (this.exports) {
|
|
45
|
+
throw new Error('Asyncify has been initialized');
|
|
46
|
+
}
|
|
47
|
+
if (!(memory instanceof _WebAssembly.Memory)) {
|
|
48
|
+
throw new TypeError('Require WebAssembly.Memory object');
|
|
49
|
+
}
|
|
50
|
+
const exports = instance.exports;
|
|
51
|
+
for (let i = 0; i < ignoreNames.length; ++i) {
|
|
52
|
+
if (typeof exports[ignoreNames[i]] !== 'function') {
|
|
53
|
+
throw new TypeError('Invalid asyncify wasm');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
let address;
|
|
57
|
+
const wasm64 = Boolean(options.wasm64);
|
|
58
|
+
if (!options.tryAllocate) {
|
|
59
|
+
address = {
|
|
60
|
+
wasm64,
|
|
61
|
+
dataPtr: 16,
|
|
62
|
+
start: wasm64 ? 32 : 24,
|
|
63
|
+
end: 1024
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
if (options.tryAllocate === true) {
|
|
68
|
+
address = tryAllocate(instance, wasm64, 4096, 'malloc');
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
address = tryAllocate(instance, wasm64, (_a = options.tryAllocate.size) !== null && _a !== void 0 ? _a : 4096, (_b = options.tryAllocate.name) !== null && _b !== void 0 ? _b : 'malloc');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
this.dataPtr = address.dataPtr;
|
|
75
|
+
if (wasm64) {
|
|
76
|
+
new BigInt64Array(memory.buffer, this.dataPtr).set([BigInt(address.start), BigInt(address.end)]);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
|
|
80
|
+
}
|
|
81
|
+
this.exports = this.wrapExports(exports, options.wrapExports);
|
|
82
|
+
const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
|
|
83
|
+
Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
|
|
84
|
+
// Object.setPrototypeOf(instance, Instance.prototype)
|
|
85
|
+
return asyncifiedInstance;
|
|
86
|
+
}
|
|
87
|
+
assertState() {
|
|
88
|
+
if (this.exports.asyncify_get_state() !== AsyncifyState.NONE) {
|
|
89
|
+
throw new Error('Asyncify state error');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
wrapImportFunction(f) {
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
94
|
+
const _this = this;
|
|
95
|
+
return (function () {
|
|
96
|
+
// eslint-disable-next-line no-unreachable-loop
|
|
97
|
+
while (_this.exports.asyncify_get_state() === AsyncifyState.REWINDING) {
|
|
98
|
+
_this.exports.asyncify_stop_rewind();
|
|
99
|
+
return _this.value;
|
|
100
|
+
}
|
|
101
|
+
_this.assertState();
|
|
102
|
+
const v = f.apply(this, arguments);
|
|
103
|
+
if (!isPromiseLike(v))
|
|
104
|
+
return v;
|
|
105
|
+
_this.exports.asyncify_start_unwind(_this.dataPtr);
|
|
106
|
+
_this.value = v;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
wrapImports(imports) {
|
|
110
|
+
const importObject = {};
|
|
111
|
+
Object.keys(imports).forEach(k => {
|
|
112
|
+
const mod = imports[k];
|
|
113
|
+
const newModule = {};
|
|
114
|
+
Object.keys(mod).forEach(name => {
|
|
115
|
+
const importValue = mod[name];
|
|
116
|
+
if (typeof importValue === 'function') {
|
|
117
|
+
newModule[name] = this.wrapImportFunction(importValue);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
newModule[name] = importValue;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
importObject[k] = newModule;
|
|
124
|
+
});
|
|
125
|
+
return importObject;
|
|
126
|
+
}
|
|
127
|
+
wrapExportFunction(f) {
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
129
|
+
const _this = this;
|
|
130
|
+
return (async function () {
|
|
131
|
+
_this.assertState();
|
|
132
|
+
let ret = f.apply(this, arguments);
|
|
133
|
+
while (_this.exports.asyncify_get_state() === AsyncifyState.UNWINDING) {
|
|
134
|
+
_this.exports.asyncify_stop_unwind();
|
|
135
|
+
_this.value = await _this.value;
|
|
136
|
+
_this.assertState();
|
|
137
|
+
_this.exports.asyncify_start_rewind(_this.dataPtr);
|
|
138
|
+
ret = f.call(this);
|
|
139
|
+
}
|
|
140
|
+
_this.assertState();
|
|
141
|
+
return ret;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
wrapExports(exports, needWrap) {
|
|
145
|
+
return wrapInstanceExports(exports, (exportValue, name) => {
|
|
146
|
+
let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
|
|
147
|
+
if (Array.isArray(needWrap)) {
|
|
148
|
+
ignore = ignore || (needWrap.indexOf(name) === -1);
|
|
149
|
+
}
|
|
150
|
+
return ignore ? exportValue : this.wrapExportFunction(exportValue);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
package/lib/mjs/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @packageDocumentation
|
|
3
|
-
*/
|
|
4
|
-
export * from "./asyncify.mjs";
|
|
5
|
-
export * from "./load.mjs";
|
|
6
|
-
export * from "./wasi/index.mjs";
|
|
7
|
-
export * from "./memory.mjs";
|
|
8
|
-
export * from "./jspi.mjs";
|
|
9
|
-
export * from "./wasi/fs.mjs";
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*/
|
|
4
|
+
export * from "./asyncify.mjs";
|
|
5
|
+
export * from "./load.mjs";
|
|
6
|
+
export * from "./wasi/index.mjs";
|
|
7
|
+
export * from "./memory.mjs";
|
|
8
|
+
export * from "./jspi.mjs";
|
|
9
|
+
export * from "./wasi/fs.mjs";
|