@tybys/wasm-util 0.1.0 → 0.3.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.
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapExports = exports.wrapAsyncExport = exports.wrapAsyncImport = void 0;
4
+ const util_1 = require("./wasi/util");
5
+ const webassembly_1 = require("./webassembly");
6
+ function checkWebAssemblyFunction() {
7
+ const WebAssemblyFunction = webassembly_1._WebAssembly.Function;
8
+ if (typeof WebAssemblyFunction !== 'function') {
9
+ throw new Error('WebAssembly.Function is not supported in this environment.' +
10
+ ' If you are using V8 based browser like Chrome, try to specify' +
11
+ ' --js-flag="--wasm-staging --experimental-wasm-stack-switching"');
12
+ }
13
+ return WebAssemblyFunction;
14
+ }
15
+ /** @public */
16
+ function wrapAsyncImport(f, parameterType, returnType) {
17
+ const WebAssemblyFunction = checkWebAssemblyFunction();
18
+ if (typeof f !== 'function') {
19
+ throw new TypeError('Function required');
20
+ }
21
+ const parameters = parameterType.slice(0);
22
+ parameters.unshift('externref');
23
+ return new WebAssemblyFunction({ parameters, results: returnType }, f, { suspending: 'first' });
24
+ }
25
+ exports.wrapAsyncImport = wrapAsyncImport;
26
+ /** @public */
27
+ function wrapAsyncExport(f) {
28
+ const WebAssemblyFunction = checkWebAssemblyFunction();
29
+ if (typeof f !== 'function') {
30
+ throw new TypeError('Function required');
31
+ }
32
+ return new WebAssemblyFunction({ parameters: [...WebAssemblyFunction.type(f).parameters.slice(1)], results: ['externref'] }, f, { promising: 'first' });
33
+ }
34
+ exports.wrapAsyncExport = wrapAsyncExport;
35
+ /** @public */
36
+ function wrapExports(exports, needWrap) {
37
+ return (0, util_1.wrapInstanceExports)(exports, (exportValue, name) => {
38
+ let ignore = typeof exportValue !== 'function';
39
+ if (Array.isArray(needWrap)) {
40
+ ignore = ignore || (needWrap.indexOf(name) === -1);
41
+ }
42
+ return ignore ? exportValue : wrapAsyncExport(exportValue);
43
+ });
44
+ }
45
+ exports.wrapExports = wrapExports;
46
+ //# sourceMappingURL=jspi.js.map
package/lib/cjs/load.js CHANGED
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.loadSync = exports.load = void 0;
4
+ const webassembly_1 = require("./webassembly");
4
5
  const asyncify_1 = require("./asyncify");
5
6
  async function fetchWasm(urlOrBuffer, imports) {
7
+ if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
8
+ return await webassembly_1._WebAssembly.instantiate(urlOrBuffer, imports);
9
+ }
6
10
  const response = await fetch(urlOrBuffer);
7
11
  const buffer = await response.arrayBuffer();
8
- const source = await WebAssembly.instantiate(buffer, imports);
12
+ const source = await webassembly_1._WebAssembly.instantiate(buffer, imports);
9
13
  return source;
10
14
  }
11
15
  /** @public */
@@ -22,7 +26,7 @@ async function load(urlOrBuffer, imports, asyncify) {
22
26
  imports = asyncifyHelper.wrapImports(imports);
23
27
  }
24
28
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
25
- source = await WebAssembly.instantiate(urlOrBuffer, imports);
29
+ source = await webassembly_1._WebAssembly.instantiate(urlOrBuffer, imports);
26
30
  if (asyncify) {
27
31
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
28
32
  return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
@@ -32,9 +36,9 @@ async function load(urlOrBuffer, imports, asyncify) {
32
36
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
33
37
  throw new TypeError('Invalid source');
34
38
  }
35
- if (typeof WebAssembly.instantiateStreaming === 'function') {
39
+ if (typeof webassembly_1._WebAssembly.instantiateStreaming === 'function') {
36
40
  try {
37
- source = await WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
41
+ source = await webassembly_1._WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
38
42
  }
39
43
  catch (_) {
40
44
  source = await fetchWasm(urlOrBuffer, imports);
@@ -65,8 +69,8 @@ function loadSync(buffer, imports, asyncify) {
65
69
  asyncifyHelper = new asyncify_1.Asyncify();
66
70
  imports = asyncifyHelper.wrapImports(imports);
67
71
  }
68
- const module = new WebAssembly.Module(buffer);
69
- const instance = new WebAssembly.Instance(module, imports);
72
+ const module = new webassembly_1._WebAssembly.Module(buffer);
73
+ const instance = new webassembly_1._WebAssembly.Instance(module, imports);
70
74
  const source = { instance, module };
71
75
  if (asyncify) {
72
76
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
package/lib/cjs/memory.js CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extendMemory = exports.Memory = void 0;
3
+ exports.extendMemory = exports.Memory = exports.WebAssemblyMemory = void 0;
4
+ const webassembly_1 = require("./webassembly");
4
5
  /** @public */
5
- class Memory extends WebAssembly.Memory {
6
+ exports.WebAssemblyMemory = webassembly_1._WebAssembly.Memory;
7
+ /** @public */
8
+ class Memory extends exports.WebAssemblyMemory {
6
9
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
7
10
  constructor(descriptor) {
8
11
  super(descriptor);
@@ -22,7 +25,7 @@ class Memory extends WebAssembly.Memory {
22
25
  exports.Memory = Memory;
23
26
  /** @public */
24
27
  function extendMemory(memory) {
25
- if (Object.getPrototypeOf(memory) === WebAssembly.Memory.prototype) {
28
+ if (Object.getPrototypeOf(memory) === webassembly_1._WebAssembly.Memory.prototype) {
26
29
  Object.setPrototypeOf(memory, Memory.prototype);
27
30
  }
28
31
  return memory;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WASI = void 0;
4
+ // import { vol } from 'memfs-browser'
5
+ const webassembly_1 = require("../webassembly");
4
6
  const path_1 = require("./path");
5
7
  const types_1 = require("./types");
6
8
  const fd_1 = require("./fd");
@@ -113,7 +115,7 @@ function readStdin() {
113
115
  class WASI {
114
116
  constructor(args, env, preopens, stdio, filesystem, print, printErr) {
115
117
  this._setMemory = function _setMemory(m) {
116
- if (!(m instanceof WebAssembly.Memory)) {
118
+ if (!(m instanceof webassembly_1._WebAssembly.Memory)) {
117
119
  throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
118
120
  }
119
121
  _memory.set(this, (0, memory_1.extendMemory)(m));
@@ -420,6 +422,9 @@ class WASI {
420
422
  let buffer;
421
423
  let nread = 0;
422
424
  if (fd === 0) {
425
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
426
+ return types_1.WasiErrno.ENOTSUP;
427
+ }
423
428
  buffer = readStdin();
424
429
  nread = buffer ? copyMemory(ioVecs, buffer) : 0;
425
430
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
3
+ exports.wrapInstanceExports = exports.isPromiseLike = exports.validateInt32 = exports.validateUndefined = exports.validateFunction = exports.validateString = exports.validateBoolean = exports.validateArray = exports.validateObject = void 0;
4
4
  function validateObject(value, name) {
5
5
  if (value === null || typeof value !== 'object') {
6
6
  throw new TypeError(`${name} must be an object. Received ${value === null ? 'null' : typeof value}`);
@@ -53,4 +53,16 @@ function isPromiseLike(obj) {
53
53
  return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
54
54
  }
55
55
  exports.isPromiseLike = isPromiseLike;
56
+ function wrapInstanceExports(exports, mapFn) {
57
+ const newExports = Object.create(null);
58
+ Object.keys(exports).forEach(name => {
59
+ const exportValue = exports[name];
60
+ Object.defineProperty(newExports, name, {
61
+ enumerable: true,
62
+ value: mapFn(exportValue, name)
63
+ });
64
+ });
65
+ return newExports;
66
+ }
67
+ exports.wrapInstanceExports = wrapInstanceExports;
56
68
  //# sourceMappingURL=util.js.map
@@ -0,0 +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
+ }
13
+ //# sourceMappingURL=webassembly.js.map
@@ -1,4 +1,5 @@
1
- import { isPromiseLike } from "./wasi/util.mjs";
1
+ import { _WebAssembly } from "./webassembly.mjs";
2
+ import { isPromiseLike, wrapInstanceExports } from "./wasi/util.mjs";
2
3
  const ignoreNames = [
3
4
  'asyncify_get_state',
4
5
  'asyncify_start_rewind',
@@ -43,7 +44,7 @@ export class Asyncify {
43
44
  if (this.exports) {
44
45
  throw new Error('Asyncify has been initialized');
45
46
  }
46
- if (!(memory instanceof WebAssembly.Memory)) {
47
+ if (!(memory instanceof _WebAssembly.Memory)) {
47
48
  throw new TypeError('Require WebAssembly.Memory object');
48
49
  }
49
50
  const exports = instance.exports;
@@ -78,7 +79,7 @@ export class Asyncify {
78
79
  new Int32Array(memory.buffer, this.dataPtr).set([address.start, address.end]);
79
80
  }
80
81
  this.exports = this.wrapExports(exports, options.wrapExports);
81
- const asyncifiedInstance = Object.create(WebAssembly.Instance.prototype);
82
+ const asyncifiedInstance = Object.create(_WebAssembly.Instance.prototype);
82
83
  Object.defineProperty(asyncifiedInstance, 'exports', { value: this.exports });
83
84
  // Object.setPrototypeOf(instance, Instance.prototype)
84
85
  return asyncifiedInstance;
@@ -137,20 +138,13 @@ export class Asyncify {
137
138
  });
138
139
  }
139
140
  wrapExports(exports, needWrap) {
140
- const newExports = Object.create(null);
141
- Object.keys(exports).forEach(name => {
142
- const exportValue = exports[name];
141
+ return wrapInstanceExports(exports, (exportValue, name) => {
143
142
  let ignore = ignoreNames.indexOf(name) !== -1 || typeof exportValue !== 'function';
144
143
  if (Array.isArray(needWrap)) {
145
144
  ignore = ignore || (needWrap.indexOf(name) === -1);
146
145
  }
147
- Object.defineProperty(newExports, name, {
148
- enumerable: true,
149
- value: ignore ? exportValue : this.wrapExportFunction(exportValue)
150
- });
146
+ return ignore ? exportValue : this.wrapExportFunction(exportValue);
151
147
  });
152
- // wrappedExports.set(exports, newExports)
153
- return newExports;
154
148
  }
155
149
  }
156
150
  // /** @public */
package/lib/mjs/index.mjs CHANGED
@@ -5,3 +5,4 @@ export * from "./asyncify.mjs";
5
5
  export * from "./load.mjs";
6
6
  export * from "./wasi/index.mjs";
7
7
  export * from "./memory.mjs";
8
+ export * from "./jspi.mjs";
@@ -0,0 +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
+ }
package/lib/mjs/load.mjs CHANGED
@@ -1,8 +1,12 @@
1
+ import { _WebAssembly } from "./webassembly.mjs";
1
2
  import { Asyncify } from "./asyncify.mjs";
2
3
  async function fetchWasm(urlOrBuffer, imports) {
4
+ if (typeof wx !== 'undefined' && typeof __wxConfig !== 'undefined') {
5
+ return await _WebAssembly.instantiate(urlOrBuffer, imports);
6
+ }
3
7
  const response = await fetch(urlOrBuffer);
4
8
  const buffer = await response.arrayBuffer();
5
- const source = await WebAssembly.instantiate(buffer, imports);
9
+ const source = await _WebAssembly.instantiate(buffer, imports);
6
10
  return source;
7
11
  }
8
12
  /** @public */
@@ -19,7 +23,7 @@ export async function load(urlOrBuffer, imports, asyncify) {
19
23
  imports = asyncifyHelper.wrapImports(imports);
20
24
  }
21
25
  if (urlOrBuffer instanceof ArrayBuffer || ArrayBuffer.isView(urlOrBuffer)) {
22
- source = await WebAssembly.instantiate(urlOrBuffer, imports);
26
+ source = await _WebAssembly.instantiate(urlOrBuffer, imports);
23
27
  if (asyncify) {
24
28
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
25
29
  return { module: source.module, instance: asyncifyHelper.init(memory, source.instance, asyncify) };
@@ -29,9 +33,9 @@ export async function load(urlOrBuffer, imports, asyncify) {
29
33
  if (typeof urlOrBuffer !== 'string' && !(urlOrBuffer instanceof URL)) {
30
34
  throw new TypeError('Invalid source');
31
35
  }
32
- if (typeof WebAssembly.instantiateStreaming === 'function') {
36
+ if (typeof _WebAssembly.instantiateStreaming === 'function') {
33
37
  try {
34
- source = await WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
38
+ source = await _WebAssembly.instantiateStreaming(fetch(urlOrBuffer), imports);
35
39
  }
36
40
  catch (_) {
37
41
  source = await fetchWasm(urlOrBuffer, imports);
@@ -61,8 +65,8 @@ export function loadSync(buffer, imports, asyncify) {
61
65
  asyncifyHelper = new Asyncify();
62
66
  imports = asyncifyHelper.wrapImports(imports);
63
67
  }
64
- const module = new WebAssembly.Module(buffer);
65
- const instance = new WebAssembly.Instance(module, imports);
68
+ const module = new _WebAssembly.Module(buffer);
69
+ const instance = new _WebAssembly.Instance(module, imports);
66
70
  const source = { instance, module };
67
71
  if (asyncify) {
68
72
  const memory = source.instance.exports.memory || ((_a = imports.env) === null || _a === void 0 ? void 0 : _a.memory);
@@ -1,5 +1,8 @@
1
+ import { _WebAssembly } from "./webassembly.mjs";
1
2
  /** @public */
2
- export class Memory extends WebAssembly.Memory {
3
+ export const WebAssemblyMemory = _WebAssembly.Memory;
4
+ /** @public */
5
+ export class Memory extends WebAssemblyMemory {
3
6
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
4
7
  constructor(descriptor) {
5
8
  super(descriptor);
@@ -18,7 +21,7 @@ export class Memory extends WebAssembly.Memory {
18
21
  }
19
22
  /** @public */
20
23
  export function extendMemory(memory) {
21
- if (Object.getPrototypeOf(memory) === WebAssembly.Memory.prototype) {
24
+ if (Object.getPrototypeOf(memory) === _WebAssembly.Memory.prototype) {
22
25
  Object.setPrototypeOf(memory, Memory.prototype);
23
26
  }
24
27
  return memory;
@@ -1,3 +1,4 @@
1
+ import { _WebAssembly } from "../webassembly.mjs";
1
2
  import { resolve } from "./path.mjs";
2
3
  import { WasiErrno, WasiRights, FileControlFlag, WasiFileControlFlag, WasiFdFlag, WasiFileType, WasiClockid, WasiFstFlag } from "./types.mjs";
3
4
  import { FileDescriptorTable, concatBuffer, toFileStat } from "./fd.mjs";
@@ -110,7 +111,7 @@ function readStdin() {
110
111
  export class WASI {
111
112
  constructor(args, env, preopens, stdio, filesystem, print, printErr) {
112
113
  this._setMemory = function _setMemory(m) {
113
- if (!(m instanceof WebAssembly.Memory)) {
114
+ if (!(m instanceof _WebAssembly.Memory)) {
114
115
  throw new TypeError('"instance.exports.memory" property must be a WebAssembly.Memory');
115
116
  }
116
117
  _memory.set(this, extendMemory(m));
@@ -417,6 +418,9 @@ export class WASI {
417
418
  let buffer;
418
419
  let nread = 0;
419
420
  if (fd === 0) {
421
+ if (typeof window === 'undefined' || typeof window.prompt !== 'function') {
422
+ return WasiErrno.ENOTSUP;
423
+ }
420
424
  buffer = readStdin();
421
425
  nread = buffer ? copyMemory(ioVecs, buffer) : 0;
422
426
  }
@@ -42,3 +42,14 @@ export function validateInt32(value, name, min = -2147483648, max = 2147483647)
42
42
  export function isPromiseLike(obj) {
43
43
  return !!(obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function');
44
44
  }
45
+ export function wrapInstanceExports(exports, mapFn) {
46
+ const newExports = Object.create(null);
47
+ Object.keys(exports).forEach(name => {
48
+ const exportValue = exports[name];
49
+ Object.defineProperty(newExports, name, {
50
+ enumerable: true,
51
+ value: mapFn(exportValue, name)
52
+ });
53
+ });
54
+ return newExports;
55
+ }
@@ -0,0 +1,9 @@
1
+ const _WebAssembly = typeof WebAssembly !== 'undefined'
2
+ ? WebAssembly
3
+ : typeof WXWebAssembly !== 'undefined'
4
+ ? WXWebAssembly
5
+ : undefined;
6
+ if (!_WebAssembly) {
7
+ throw new Error('WebAssembly is not supported in this environment');
8
+ }
9
+ export { _WebAssembly };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tybys/wasm-util",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "WASI polyfill for browser and some wasm util",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./dist/wasm-util.esm-bundler.js",