@tybys/wasm-util 0.5.3 → 0.7.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.
Files changed (41) hide show
  1. package/README.md +193 -193
  2. package/dist/tsdoc-metadata.json +1 -1
  3. package/dist/wasm-util.d.ts +1 -1
  4. package/dist/wasm-util.esm-bundler.js +2608 -2482
  5. package/dist/wasm-util.esm.js +2608 -2482
  6. package/dist/wasm-util.esm.min.js +1 -1
  7. package/dist/wasm-util.js +2608 -2482
  8. package/dist/wasm-util.min.js +1 -1
  9. package/lib/cjs/asyncify.js +157 -157
  10. package/lib/cjs/index.js +12 -12
  11. package/lib/cjs/jspi.js +45 -45
  12. package/lib/cjs/load.js +85 -85
  13. package/lib/cjs/memory.js +34 -34
  14. package/lib/cjs/wasi/error.js +102 -102
  15. package/lib/cjs/wasi/fd.js +267 -267
  16. package/lib/cjs/wasi/fs.js +2 -2
  17. package/lib/cjs/wasi/index.js +171 -168
  18. package/lib/cjs/wasi/path.js +173 -173
  19. package/lib/cjs/wasi/preview1.js +1478 -1369
  20. package/lib/cjs/wasi/rights.js +139 -139
  21. package/lib/cjs/wasi/types.js +219 -209
  22. package/lib/cjs/wasi/util.js +121 -67
  23. package/lib/cjs/webassembly.js +12 -12
  24. package/lib/mjs/asyncify.mjs +153 -153
  25. package/lib/mjs/index.mjs +9 -9
  26. package/lib/mjs/jspi.mjs +39 -39
  27. package/lib/mjs/load.mjs +78 -78
  28. package/lib/mjs/memory.mjs +28 -28
  29. package/lib/mjs/wasi/error.mjs +97 -97
  30. package/lib/mjs/wasi/fd.mjs +256 -256
  31. package/lib/mjs/wasi/fs.mjs +1 -1
  32. package/lib/mjs/wasi/index.mjs +167 -164
  33. package/lib/mjs/wasi/path.mjs +168 -168
  34. package/lib/mjs/wasi/preview1.mjs +1474 -1365
  35. package/lib/mjs/wasi/rights.mjs +134 -134
  36. package/lib/mjs/wasi/types.mjs +216 -206
  37. package/lib/mjs/wasi/util.mjs +108 -55
  38. package/lib/mjs/webassembly.mjs +9 -9
  39. package/package.json +58 -57
  40. package/lib/cjs/memfs.js +0 -2689
  41. package/lib/mjs/memfs.mjs +0 -2688
@@ -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
@@ -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";
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-flag="--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
- }
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,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(urlOrBuffer, imports) {
18
- validateImports(imports);
19
- imports = imports !== null && imports !== void 0 ? imports : {};
20
- let source;
21
- if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
22
- return _WebAssembly.instantiate(urlOrBuffer, imports);
23
- }
24
- if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
25
- throw new TypeError('Invalid source');
26
- }
27
- if (typeof _WebAssembly.instantiateStreaming === 'function') {
28
- let responsePromise;
29
- try {
30
- responsePromise = fetch(urlOrBuffer);
31
- source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => {
32
- return fetchWasm(urlOrBuffer, imports);
33
- });
34
- }
35
- catch (_) {
36
- source = fetchWasm(urlOrBuffer, imports);
37
- }
38
- }
39
- else {
40
- source = fetchWasm(urlOrBuffer, imports);
41
- }
42
- return source;
43
- }
44
- /** @public */
45
- export function asyncifyLoad(asyncify, urlOrBuffer, imports) {
46
- validateImports(imports);
47
- imports = imports !== null && imports !== void 0 ? imports : {};
48
- const asyncifyHelper = new Asyncify();
49
- imports = asyncifyHelper.wrapImports(imports);
50
- return load(urlOrBuffer, imports).then(source => {
51
- var _a;
52
- const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
53
- return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
54
- });
55
- }
56
- /** @public */
57
- export function loadSync(buffer, imports) {
58
- if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
59
- throw new TypeError('Invalid source');
60
- }
61
- validateImports(imports);
62
- imports = imports !== null && imports !== void 0 ? imports : {};
63
- const module = new _WebAssembly.Module(buffer);
64
- const instance = new _WebAssembly.Instance(module, imports);
65
- const source = { instance, module };
66
- return source;
67
- }
68
- /** @public */
69
- export function asyncifyLoadSync(asyncify, buffer, imports) {
70
- var _a;
71
- validateImports(imports);
72
- imports = imports !== null && imports !== void 0 ? imports : {};
73
- const asyncifyHelper = new Asyncify();
74
- imports = asyncifyHelper.wrapImports(imports);
75
- const source = loadSync(buffer, imports);
76
- const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
77
- return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
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(urlOrBuffer, imports) {
18
+ validateImports(imports);
19
+ imports = imports !== null && imports !== void 0 ? imports : {};
20
+ let source;
21
+ if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
22
+ return _WebAssembly.instantiate(urlOrBuffer, imports);
23
+ }
24
+ if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
25
+ throw new TypeError('Invalid source');
26
+ }
27
+ if (typeof _WebAssembly.instantiateStreaming === 'function') {
28
+ let responsePromise;
29
+ try {
30
+ responsePromise = fetch(urlOrBuffer);
31
+ source = _WebAssembly.instantiateStreaming(responsePromise, imports).catch(() => {
32
+ return fetchWasm(urlOrBuffer, imports);
33
+ });
34
+ }
35
+ catch (_) {
36
+ source = fetchWasm(urlOrBuffer, imports);
37
+ }
38
+ }
39
+ else {
40
+ source = fetchWasm(urlOrBuffer, imports);
41
+ }
42
+ return source;
43
+ }
44
+ /** @public */
45
+ export function asyncifyLoad(asyncify, urlOrBuffer, imports) {
46
+ validateImports(imports);
47
+ imports = imports !== null && imports !== void 0 ? imports : {};
48
+ const asyncifyHelper = new Asyncify();
49
+ imports = asyncifyHelper.wrapImports(imports);
50
+ return load(urlOrBuffer, imports).then(source => {
51
+ var _a;
52
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
53
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
54
+ });
55
+ }
56
+ /** @public */
57
+ export function loadSync(buffer, imports) {
58
+ if ((buffer instanceof ArrayBuffer) && !ArrayBuffer.isView(buffer)) {
59
+ throw new TypeError('Invalid source');
60
+ }
61
+ validateImports(imports);
62
+ imports = imports !== null && imports !== void 0 ? imports : {};
63
+ const module = new _WebAssembly.Module(buffer);
64
+ const instance = new _WebAssembly.Instance(module, imports);
65
+ const source = { instance, module };
66
+ return source;
67
+ }
68
+ /** @public */
69
+ export function asyncifyLoadSync(asyncify, buffer, imports) {
70
+ var _a;
71
+ validateImports(imports);
72
+ imports = imports !== null && imports !== void 0 ? imports : {};
73
+ const asyncifyHelper = new Asyncify();
74
+ imports = asyncifyHelper.wrapImports(imports);
75
+ const source = loadSync(buffer, imports);
76
+ const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
77
+ return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
78
+ }
@@ -1,28 +1,28 @@
1
- import { _WebAssembly } from "./webassembly.mjs";
2
- /** @public */
3
- export const WebAssemblyMemory = /*#__PURE__*/ (function () { return _WebAssembly.Memory; })();
4
- /** @public */
5
- export class Memory extends WebAssemblyMemory {
6
- // eslint-disable-next-line @typescript-eslint/no-useless-constructor
7
- constructor(descriptor) {
8
- super(descriptor);
9
- }
10
- get HEAP8() { return new Int8Array(super.buffer); }
11
- get HEAPU8() { return new Uint8Array(super.buffer); }
12
- get HEAP16() { return new Int16Array(super.buffer); }
13
- get HEAPU16() { return new Uint16Array(super.buffer); }
14
- get HEAP32() { return new Int32Array(super.buffer); }
15
- get HEAPU32() { return new Uint32Array(super.buffer); }
16
- get HEAP64() { return new BigInt64Array(super.buffer); }
17
- get HEAPU64() { return new BigUint64Array(super.buffer); }
18
- get HEAPF32() { return new Float32Array(super.buffer); }
19
- get HEAPF64() { return new Float64Array(super.buffer); }
20
- get view() { return new DataView(super.buffer); }
21
- }
22
- /** @public */
23
- export function extendMemory(memory) {
24
- if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
25
- Object.setPrototypeOf(memory, Memory.prototype);
26
- }
27
- return memory;
28
- }
1
+ import { _WebAssembly } from "./webassembly.mjs";
2
+ /** @public */
3
+ export const WebAssemblyMemory = /*#__PURE__*/ (function () { return _WebAssembly.Memory; })();
4
+ /** @public */
5
+ export class Memory extends WebAssemblyMemory {
6
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
7
+ constructor(descriptor) {
8
+ super(descriptor);
9
+ }
10
+ get HEAP8() { return new Int8Array(super.buffer); }
11
+ get HEAPU8() { return new Uint8Array(super.buffer); }
12
+ get HEAP16() { return new Int16Array(super.buffer); }
13
+ get HEAPU16() { return new Uint16Array(super.buffer); }
14
+ get HEAP32() { return new Int32Array(super.buffer); }
15
+ get HEAPU32() { return new Uint32Array(super.buffer); }
16
+ get HEAP64() { return new BigInt64Array(super.buffer); }
17
+ get HEAPU64() { return new BigUint64Array(super.buffer); }
18
+ get HEAPF32() { return new Float32Array(super.buffer); }
19
+ get HEAPF64() { return new Float64Array(super.buffer); }
20
+ get view() { return new DataView(super.buffer); }
21
+ }
22
+ /** @public */
23
+ export function extendMemory(memory) {
24
+ if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
25
+ Object.setPrototypeOf(memory, Memory.prototype);
26
+ }
27
+ return memory;
28
+ }