@ruby/wasm-wasi 2.3.0 → 2.4.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 (46) hide show
  1. package/README.md +180 -4
  2. package/dist/browser.script.umd.js +170 -2048
  3. package/dist/browser.umd.js +170 -2048
  4. package/dist/cjs/bindgen/intrinsics.d.ts +13 -0
  5. package/dist/cjs/bindgen/intrinsics.js +81 -0
  6. package/dist/cjs/bindgen/rb-abi-guest.js +236 -0
  7. package/dist/cjs/bindgen/rb-js-abi-host.js +283 -0
  8. package/dist/{browser.d.ts → cjs/browser.d.ts} +2 -2
  9. package/dist/cjs/browser.js +30 -0
  10. package/dist/{browser.script.d.ts → cjs/browser.script.d.ts} +1 -1
  11. package/dist/cjs/browser.script.js +90 -0
  12. package/dist/cjs/console.d.ts +33 -0
  13. package/dist/cjs/console.js +104 -0
  14. package/dist/{index.d.ts → cjs/index.d.ts} +7 -3
  15. package/dist/cjs/index.js +646 -0
  16. package/dist/{node.d.ts → cjs/node.d.ts} +1 -1
  17. package/dist/cjs/node.js +23 -0
  18. package/dist/esm/bindgen/intrinsics.d.ts +13 -0
  19. package/dist/esm/bindgen/intrinsics.js +73 -0
  20. package/dist/esm/bindgen/rb-abi-guest.d.ts +140 -0
  21. package/dist/esm/bindgen/rb-abi-guest.js +230 -0
  22. package/dist/esm/bindgen/rb-js-abi-host.d.ts +53 -0
  23. package/dist/esm/bindgen/rb-js-abi-host.js +279 -0
  24. package/dist/esm/browser.d.ts +10 -0
  25. package/dist/esm/browser.js +26 -0
  26. package/dist/esm/browser.script.d.ts +5 -0
  27. package/dist/esm/browser.script.js +86 -0
  28. package/dist/esm/console.d.ts +33 -0
  29. package/dist/esm/console.js +100 -0
  30. package/dist/esm/index.d.ts +196 -0
  31. package/dist/esm/index.js +637 -0
  32. package/dist/esm/node.d.ts +10 -0
  33. package/dist/esm/node.js +19 -0
  34. package/dist/esm/package.json +1 -0
  35. package/dist/index.umd.js +158 -10
  36. package/package.json +22 -38
  37. package/dist/browser.cjs.js +0 -3245
  38. package/dist/browser.esm.js +0 -3243
  39. package/dist/browser.script.cjs.js +0 -3372
  40. package/dist/browser.script.esm.js +0 -3370
  41. package/dist/index.cjs.js +0 -1192
  42. package/dist/index.esm.js +0 -1187
  43. package/dist/node.cjs.js +0 -1209
  44. package/dist/node.esm.js +0 -1207
  45. /package/dist/{bindgen → cjs/bindgen}/rb-abi-guest.d.ts +0 -0
  46. /package/dist/{bindgen → cjs/bindgen}/rb-js-abi-host.d.ts +0 -0
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.main = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const browser_js_1 = require("./browser.js");
6
+ const main = async (pkg, options) => {
7
+ const response = fetch(`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`);
8
+ const module = await compileWebAssemblyModule(response);
9
+ const { vm } = await (0, browser_js_1.DefaultRubyVM)(module, options);
10
+ vm.printVersion();
11
+ globalThis.rubyVM = vm;
12
+ // Wait for the text/ruby script tag to be read.
13
+ // It may take some time to read ruby+stdlib.wasm
14
+ // and DOMContentLoaded has already been fired.
15
+ if (document.readyState === "loading") {
16
+ document.addEventListener("DOMContentLoaded", () => runRubyScriptsInHtml(vm));
17
+ }
18
+ else {
19
+ runRubyScriptsInHtml(vm);
20
+ }
21
+ };
22
+ exports.main = main;
23
+ const runRubyScriptsInHtml = async (vm) => {
24
+ var _a, e_1, _b, _c;
25
+ const tags = document.querySelectorAll('script[type="text/ruby"]');
26
+ // Get Ruby scripts in parallel.
27
+ const promisingRubyScripts = Array.from(tags).map((tag) => loadScriptAsync(tag));
28
+ try {
29
+ // Run Ruby scripts sequentially.
30
+ for (var _d = true, promisingRubyScripts_1 = tslib_1.__asyncValues(promisingRubyScripts), promisingRubyScripts_1_1; promisingRubyScripts_1_1 = await promisingRubyScripts_1.next(), _a = promisingRubyScripts_1_1.done, !_a; _d = true) {
31
+ _c = promisingRubyScripts_1_1.value;
32
+ _d = false;
33
+ const script = _c;
34
+ if (script) {
35
+ const { scriptContent, evalStyle } = script;
36
+ switch (evalStyle) {
37
+ case "async":
38
+ vm.evalAsync(scriptContent);
39
+ break;
40
+ case "sync":
41
+ vm.eval(scriptContent);
42
+ break;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
48
+ finally {
49
+ try {
50
+ if (!_d && !_a && (_b = promisingRubyScripts_1.return)) await _b.call(promisingRubyScripts_1);
51
+ }
52
+ finally { if (e_1) throw e_1.error; }
53
+ }
54
+ };
55
+ const deriveEvalStyle = (tag) => {
56
+ const rawEvalStyle = tag.getAttribute("data-eval") || "sync";
57
+ if (rawEvalStyle !== "async" && rawEvalStyle !== "sync") {
58
+ console.warn(`data-eval attribute of script tag must be "async" or "sync". ${rawEvalStyle} is ignored and "sync" is used instead.`);
59
+ return "sync";
60
+ }
61
+ return rawEvalStyle;
62
+ };
63
+ const loadScriptAsync = async (tag) => {
64
+ const evalStyle = deriveEvalStyle(tag);
65
+ // Inline comments can be written with the src attribute of the script tag.
66
+ // The presence of the src attribute is checked before the presence of the inline.
67
+ // see: https://html.spec.whatwg.org/multipage/scripting.html#inline-documentation-for-external-scripts
68
+ if (tag.hasAttribute("src")) {
69
+ const url = tag.getAttribute("src");
70
+ const response = await fetch(url);
71
+ if (response.ok) {
72
+ return { scriptContent: await response.text(), evalStyle };
73
+ }
74
+ return Promise.resolve(null);
75
+ }
76
+ return Promise.resolve({ scriptContent: tag.innerHTML, evalStyle });
77
+ };
78
+ // WebAssembly.compileStreaming is a relatively new API.
79
+ // For example, it is not available in iOS Safari 14,
80
+ // so check whether WebAssembly.compileStreaming is available and
81
+ // fall back to the existing implementation using WebAssembly.compile if not.
82
+ const compileWebAssemblyModule = async function (response) {
83
+ if (!WebAssembly.compileStreaming) {
84
+ const buffer = await (await response).arrayBuffer();
85
+ return WebAssembly.compile(buffer);
86
+ }
87
+ else {
88
+ return WebAssembly.compileStreaming(response);
89
+ }
90
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Create a console printer that can be used as an overlay of WASI imports.
3
+ * See the example below for how to use it.
4
+ *
5
+ * ```javascript
6
+ * const imports = {
7
+ * "wasi_snapshot_preview1": wasi.wasiImport,
8
+ * }
9
+ * const printer = consolePrinter();
10
+ * printer.addToImports(imports);
11
+ *
12
+ * const instance = await WebAssembly.instantiate(module, imports);
13
+ * printer.setMemory(instance.exports.memory);
14
+ * ```
15
+ *
16
+ * Note that the `stdout` and `stderr` functions are called with text, not
17
+ * bytes. This means that bytes written to stdout/stderr will be decoded as
18
+ * UTF-8 and then passed to the `stdout`/`stderr` functions every time a write
19
+ * occurs without buffering.
20
+ *
21
+ * @param stdout A function that will be called when stdout is written to.
22
+ * Defaults to `console.log`.
23
+ * @param stderr A function that will be called when stderr is written to.
24
+ * Defaults to `console.warn`.
25
+ * @returns An object that can be used as an overlay of WASI imports.
26
+ */
27
+ export declare function consolePrinter({ stdout, stderr, }?: {
28
+ stdout: (str: string) => void;
29
+ stderr: (str: string) => void;
30
+ }): {
31
+ addToImports(imports: WebAssembly.Imports): void;
32
+ setMemory(m: WebAssembly.Memory): void;
33
+ };
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.consolePrinter = void 0;
4
+ /**
5
+ * Create a console printer that can be used as an overlay of WASI imports.
6
+ * See the example below for how to use it.
7
+ *
8
+ * ```javascript
9
+ * const imports = {
10
+ * "wasi_snapshot_preview1": wasi.wasiImport,
11
+ * }
12
+ * const printer = consolePrinter();
13
+ * printer.addToImports(imports);
14
+ *
15
+ * const instance = await WebAssembly.instantiate(module, imports);
16
+ * printer.setMemory(instance.exports.memory);
17
+ * ```
18
+ *
19
+ * Note that the `stdout` and `stderr` functions are called with text, not
20
+ * bytes. This means that bytes written to stdout/stderr will be decoded as
21
+ * UTF-8 and then passed to the `stdout`/`stderr` functions every time a write
22
+ * occurs without buffering.
23
+ *
24
+ * @param stdout A function that will be called when stdout is written to.
25
+ * Defaults to `console.log`.
26
+ * @param stderr A function that will be called when stderr is written to.
27
+ * Defaults to `console.warn`.
28
+ * @returns An object that can be used as an overlay of WASI imports.
29
+ */
30
+ function consolePrinter({ stdout, stderr, } = {
31
+ stdout: console.log,
32
+ stderr: console.warn,
33
+ }) {
34
+ let memory = undefined;
35
+ let _view = undefined;
36
+ function getMemoryView() {
37
+ if (typeof memory === "undefined") {
38
+ throw new Error("Memory is not set");
39
+ }
40
+ if (_view === undefined || _view.buffer.byteLength === 0) {
41
+ _view = new DataView(memory.buffer);
42
+ }
43
+ return _view;
44
+ }
45
+ const decoder = new TextDecoder();
46
+ return {
47
+ addToImports(imports) {
48
+ const wasiImport = imports.wasi_snapshot_preview1;
49
+ const original_fd_write = wasiImport.fd_write;
50
+ wasiImport.fd_write = (fd, iovs, iovsLen, nwritten) => {
51
+ if (fd !== 1 && fd !== 2) {
52
+ return original_fd_write(fd, iovs, iovsLen, nwritten);
53
+ }
54
+ const view = getMemoryView();
55
+ const buffers = Array.from({ length: iovsLen }, (_, i) => {
56
+ const ptr = iovs + i * 8;
57
+ const buf = view.getUint32(ptr, true);
58
+ const bufLen = view.getUint32(ptr + 4, true);
59
+ return new Uint8Array(memory.buffer, buf, bufLen);
60
+ });
61
+ let written = 0;
62
+ let str = "";
63
+ for (const buffer of buffers) {
64
+ str += decoder.decode(buffer);
65
+ written += buffer.byteLength;
66
+ }
67
+ view.setUint32(nwritten, written, true);
68
+ const log = fd === 1 ? stdout : stderr;
69
+ log(str);
70
+ return 0;
71
+ };
72
+ const original_fd_filestat_get = wasiImport.fd_filestat_get;
73
+ wasiImport.fd_filestat_get = (fd, filestat) => {
74
+ if (fd !== 1 && fd !== 2) {
75
+ return original_fd_filestat_get(fd, filestat);
76
+ }
77
+ const view = getMemoryView();
78
+ const result = original_fd_filestat_get(fd, filestat);
79
+ if (result !== 0) {
80
+ return result;
81
+ }
82
+ const filetypePtr = filestat + 0;
83
+ view.setUint8(filetypePtr, 2); // FILETYPE_CHARACTER_DEVICE
84
+ return 0;
85
+ };
86
+ const original_fd_fdstat_get = wasiImport.fd_fdstat_get;
87
+ wasiImport.fd_fdstat_get = (fd, fdstat) => {
88
+ if (fd !== 1 && fd !== 2) {
89
+ return original_fd_fdstat_get(fd, fdstat);
90
+ }
91
+ const view = getMemoryView();
92
+ const fs_filetypePtr = fdstat + 0;
93
+ view.setUint8(fs_filetypePtr, 2); // FILETYPE_CHARACTER_DEVICE
94
+ const fs_rights_basePtr = fdstat + 8;
95
+ view.setBigUint64(fs_rights_basePtr, BigInt(1)); // RIGHTS_FD_WRITE
96
+ return 0;
97
+ };
98
+ },
99
+ setMemory(m) {
100
+ memory = m;
101
+ },
102
+ };
103
+ }
104
+ exports.consolePrinter = consolePrinter;
@@ -1,5 +1,6 @@
1
- import * as RbAbi from "./bindgen/rb-abi-guest";
2
- import { JsAbiValue } from "./bindgen/rb-js-abi-host";
1
+ import * as RbAbi from "./bindgen/rb-abi-guest.js";
2
+ import { JsAbiValue } from "./bindgen/rb-js-abi-host.js";
3
+ export { consolePrinter } from "./console.js";
3
4
  /**
4
5
  * A Ruby VM instance
5
6
  *
@@ -89,7 +90,9 @@ export declare class RubyVM {
89
90
  * hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));
90
91
  */
91
92
  wrap(value: any): RbValue;
93
+ /** @private */
92
94
  private privateObject;
95
+ /** @private */
93
96
  private rbValueOfPointer;
94
97
  }
95
98
  /**
@@ -167,7 +170,9 @@ type RubyVMPrivate = {
167
170
  };
168
171
  declare class RbExceptionFormatter {
169
172
  private literalsCache;
173
+ private isFormmatting;
170
174
  format(error: RbValue, vm: RubyVM, privateObject: RubyVMPrivate): string;
175
+ private _format;
171
176
  formatString(klass: string, message: string, backtrace?: [string, string]): string;
172
177
  }
173
178
  /**
@@ -189,4 +194,3 @@ export declare class RbFatalError extends RbError {
189
194
  */
190
195
  constructor(message: string);
191
196
  }
192
- export {};