@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.
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 +13 -7
  4. package/dist/wasm-util.esm-bundler.js +2639 -2604
  5. package/dist/wasm-util.esm.js +2639 -2604
  6. package/dist/wasm-util.esm.min.js +1 -1
  7. package/dist/wasm-util.js +2639 -2603
  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 +96 -85
  13. package/lib/cjs/memfs.js +2689 -0
  14. package/lib/cjs/memory.js +34 -34
  15. package/lib/cjs/wasi/error.js +102 -102
  16. package/lib/cjs/wasi/fd.js +267 -267
  17. package/lib/cjs/wasi/fs.js +2 -2
  18. package/lib/cjs/wasi/index.js +193 -168
  19. package/lib/cjs/wasi/path.js +173 -173
  20. package/lib/cjs/wasi/preview1.js +1478 -1478
  21. package/lib/cjs/wasi/rights.js +139 -139
  22. package/lib/cjs/wasi/types.js +219 -219
  23. package/lib/cjs/wasi/util.js +121 -121
  24. package/lib/cjs/webassembly.js +12 -12
  25. package/lib/mjs/asyncify.mjs +153 -153
  26. package/lib/mjs/index.mjs +9 -9
  27. package/lib/mjs/jspi.mjs +39 -39
  28. package/lib/mjs/load.mjs +89 -78
  29. package/lib/mjs/memfs.mjs +2688 -0
  30. package/lib/mjs/memory.mjs +28 -28
  31. package/lib/mjs/wasi/error.mjs +97 -97
  32. package/lib/mjs/wasi/fd.mjs +256 -256
  33. package/lib/mjs/wasi/fs.mjs +1 -1
  34. package/lib/mjs/wasi/index.mjs +188 -164
  35. package/lib/mjs/wasi/path.mjs +168 -168
  36. package/lib/mjs/wasi/preview1.mjs +1474 -1474
  37. package/lib/mjs/wasi/rights.mjs +134 -134
  38. package/lib/mjs/wasi/types.mjs +216 -216
  39. package/lib/mjs/wasi/util.mjs +108 -108
  40. package/lib/mjs/webassembly.mjs +9 -9
  41. 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-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,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(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(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
+ }