@ruby/wasm-wasi 2.6.2 → 2.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.
- package/README.md +1 -232
- package/dist/browser.script.umd.js +166 -130
- package/dist/browser.umd.js +146 -129
- package/dist/cjs/bindgen/interfaces/ruby-js-js-runtime.d.ts +1 -1
- package/dist/cjs/bindgen/interfaces/ruby-js-ruby-runtime.d.ts +2 -9
- package/dist/cjs/bindgen/legacy/intrinsics.js +5 -5
- package/dist/cjs/bindgen/legacy/rb-abi-guest.d.ts +1 -31
- package/dist/cjs/bindgen/legacy/rb-abi-guest.js +16 -84
- package/dist/cjs/bindgen/legacy/rb-js-abi-host.js +1 -2
- package/dist/cjs/binding.d.ts +2 -8
- package/dist/cjs/binding.js +2 -11
- package/dist/cjs/browser.js +9 -11
- package/dist/cjs/browser.script.d.ts +14 -0
- package/dist/cjs/browser.script.js +22 -2
- package/dist/cjs/console.js +1 -2
- package/dist/cjs/node.d.ts +0 -1
- package/dist/cjs/node.js +1 -9
- package/dist/cjs/vm.d.ts +157 -19
- package/dist/cjs/vm.js +120 -21
- package/dist/esm/bindgen/interfaces/ruby-js-js-runtime.d.ts +1 -1
- package/dist/esm/bindgen/interfaces/ruby-js-ruby-runtime.d.ts +2 -9
- package/dist/esm/bindgen/legacy/rb-abi-guest.d.ts +1 -31
- package/dist/esm/bindgen/legacy/rb-abi-guest.js +15 -82
- package/dist/esm/binding.d.ts +2 -8
- package/dist/esm/binding.js +2 -11
- package/dist/esm/browser.js +9 -11
- package/dist/esm/browser.script.d.ts +14 -0
- package/dist/esm/browser.script.js +19 -0
- package/dist/esm/node.d.ts +0 -1
- package/dist/esm/node.js +1 -9
- package/dist/esm/vm.d.ts +157 -19
- package/dist/esm/vm.js +120 -21
- package/dist/index.umd.js +137 -118
- package/package.json +8 -9
|
@@ -1,42 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RbAbiValue = exports.
|
|
3
|
+
exports.RbAbiValue = exports.RbAbiGuest = void 0;
|
|
4
4
|
const intrinsics_js_1 = require("./intrinsics.js");
|
|
5
5
|
class RbAbiGuest {
|
|
6
6
|
constructor() {
|
|
7
7
|
this._resource0_slab = new intrinsics_js_1.Slab();
|
|
8
|
-
this._resource1_slab = new intrinsics_js_1.Slab();
|
|
9
8
|
}
|
|
10
9
|
addToImports(imports) {
|
|
11
10
|
if (!("canonical_abi" in imports))
|
|
12
11
|
imports["canonical_abi"] = {};
|
|
13
|
-
imports.canonical_abi['resource_drop_rb-
|
|
12
|
+
imports.canonical_abi['resource_drop_rb-abi-value'] = i => {
|
|
14
13
|
this._resource0_slab.remove(i).drop();
|
|
15
14
|
};
|
|
16
|
-
imports.canonical_abi['resource_clone_rb-
|
|
15
|
+
imports.canonical_abi['resource_clone_rb-abi-value'] = i => {
|
|
17
16
|
const obj = this._resource0_slab.get(i);
|
|
18
17
|
return this._resource0_slab.insert(obj.clone());
|
|
19
18
|
};
|
|
20
|
-
imports.canonical_abi['resource_get_rb-iseq'] = i => {
|
|
21
|
-
return this._resource0_slab.get(i)._wasm_val;
|
|
22
|
-
};
|
|
23
|
-
imports.canonical_abi['resource_new_rb-iseq'] = i => {
|
|
24
|
-
const registry = this._registry0;
|
|
25
|
-
return this._resource0_slab.insert(new RbIseq(i, this));
|
|
26
|
-
};
|
|
27
|
-
imports.canonical_abi['resource_drop_rb-abi-value'] = i => {
|
|
28
|
-
this._resource1_slab.remove(i).drop();
|
|
29
|
-
};
|
|
30
|
-
imports.canonical_abi['resource_clone_rb-abi-value'] = i => {
|
|
31
|
-
const obj = this._resource1_slab.get(i);
|
|
32
|
-
return this._resource1_slab.insert(obj.clone());
|
|
33
|
-
};
|
|
34
19
|
imports.canonical_abi['resource_get_rb-abi-value'] = i => {
|
|
35
|
-
return this.
|
|
20
|
+
return this._resource0_slab.get(i)._wasm_val;
|
|
36
21
|
};
|
|
37
22
|
imports.canonical_abi['resource_new_rb-abi-value'] = i => {
|
|
38
|
-
const registry = this.
|
|
39
|
-
return this.
|
|
23
|
+
const registry = this._registry0;
|
|
24
|
+
return this._resource0_slab.insert(new RbAbiValue(i, this));
|
|
40
25
|
};
|
|
41
26
|
}
|
|
42
27
|
async instantiate(module, imports) {
|
|
@@ -57,32 +42,12 @@ class RbAbiGuest {
|
|
|
57
42
|
this.instance = instance;
|
|
58
43
|
}
|
|
59
44
|
this._exports = this.instance.exports;
|
|
60
|
-
this._registry0 = new FinalizationRegistry(this._exports['canonical_abi_drop_rb-
|
|
61
|
-
this._registry1 = new FinalizationRegistry(this._exports['canonical_abi_drop_rb-abi-value']);
|
|
45
|
+
this._registry0 = new FinalizationRegistry(this._exports['canonical_abi_drop_rb-abi-value']);
|
|
62
46
|
}
|
|
63
47
|
rubyShowVersion() {
|
|
64
48
|
this._exports['ruby-show-version: func() -> ()']();
|
|
65
49
|
}
|
|
66
|
-
rubyInit() {
|
|
67
|
-
this._exports['ruby-init: func() -> ()']();
|
|
68
|
-
}
|
|
69
|
-
rubySysinit(arg0) {
|
|
70
|
-
const memory = this._exports.memory;
|
|
71
|
-
const realloc = this._exports["cabi_realloc"];
|
|
72
|
-
const vec1 = arg0;
|
|
73
|
-
const len1 = vec1.length;
|
|
74
|
-
const result1 = realloc(0, 0, 4, len1 * 8);
|
|
75
|
-
for (let i = 0; i < vec1.length; i++) {
|
|
76
|
-
const e = vec1[i];
|
|
77
|
-
const base = result1 + i * 8;
|
|
78
|
-
const ptr0 = (0, intrinsics_js_1.utf8_encode)(e, realloc, memory);
|
|
79
|
-
const len0 = intrinsics_js_1.UTF8_ENCODED_LEN;
|
|
80
|
-
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 4, len0, true);
|
|
81
|
-
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 0, ptr0, true);
|
|
82
|
-
}
|
|
83
|
-
this._exports['ruby-sysinit: func(args: list<string>) -> ()'](result1, len1);
|
|
84
|
-
}
|
|
85
|
-
rubyOptions(arg0) {
|
|
50
|
+
rubyInit(arg0) {
|
|
86
51
|
const memory = this._exports.memory;
|
|
87
52
|
const realloc = this._exports["cabi_realloc"];
|
|
88
53
|
const vec1 = arg0;
|
|
@@ -96,15 +61,7 @@ class RbAbiGuest {
|
|
|
96
61
|
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 4, len0, true);
|
|
97
62
|
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 0, ptr0, true);
|
|
98
63
|
}
|
|
99
|
-
|
|
100
|
-
return this._resource0_slab.remove(ret);
|
|
101
|
-
}
|
|
102
|
-
rubyScript(arg0) {
|
|
103
|
-
const memory = this._exports.memory;
|
|
104
|
-
const realloc = this._exports["cabi_realloc"];
|
|
105
|
-
const ptr0 = (0, intrinsics_js_1.utf8_encode)(arg0, realloc, memory);
|
|
106
|
-
const len0 = intrinsics_js_1.UTF8_ENCODED_LEN;
|
|
107
|
-
this._exports['ruby-script: func(name: string) -> ()'](ptr0, len0);
|
|
64
|
+
this._exports['ruby-init: func(args: list<string>) -> ()'](result1, len1);
|
|
108
65
|
}
|
|
109
66
|
rubyInitLoadpath() {
|
|
110
67
|
this._exports['ruby-init-loadpath: func() -> ()']();
|
|
@@ -115,7 +72,7 @@ class RbAbiGuest {
|
|
|
115
72
|
const ptr0 = (0, intrinsics_js_1.utf8_encode)(arg0, realloc, memory);
|
|
116
73
|
const len0 = intrinsics_js_1.UTF8_ENCODED_LEN;
|
|
117
74
|
const ret = this._exports['rb-eval-string-protect: func(str: string) -> tuple<handle<rb-abi-value>, s32>'](ptr0, len0);
|
|
118
|
-
return [this.
|
|
75
|
+
return [this._resource0_slab.remove((0, intrinsics_js_1.data_view)(memory).getInt32(ret + 0, true)), (0, intrinsics_js_1.data_view)(memory).getInt32(ret + 4, true)];
|
|
119
76
|
}
|
|
120
77
|
rbFuncallvProtect(arg0, arg1, arg2) {
|
|
121
78
|
const memory = this._exports.memory;
|
|
@@ -132,10 +89,10 @@ class RbAbiGuest {
|
|
|
132
89
|
const obj1 = e;
|
|
133
90
|
if (!(obj1 instanceof RbAbiValue))
|
|
134
91
|
throw new TypeError('expected instance of RbAbiValue');
|
|
135
|
-
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 0, this.
|
|
92
|
+
(0, intrinsics_js_1.data_view)(memory).setInt32(base + 0, this._resource0_slab.insert(obj1.clone()), true);
|
|
136
93
|
}
|
|
137
|
-
const ret = this._exports['rb-funcallv-protect: func(recv: handle<rb-abi-value>, mid: u32, args: list<handle<rb-abi-value>>) -> tuple<handle<rb-abi-value>, s32>'](this.
|
|
138
|
-
return [this.
|
|
94
|
+
const ret = this._exports['rb-funcallv-protect: func(recv: handle<rb-abi-value>, mid: u32, args: list<handle<rb-abi-value>>) -> tuple<handle<rb-abi-value>, s32>'](this._resource0_slab.insert(obj0.clone()), (0, intrinsics_js_1.to_uint32)(arg1), result2, len2);
|
|
95
|
+
return [this._resource0_slab.remove((0, intrinsics_js_1.data_view)(memory).getInt32(ret + 0, true)), (0, intrinsics_js_1.data_view)(memory).getInt32(ret + 4, true)];
|
|
139
96
|
}
|
|
140
97
|
rbIntern(arg0) {
|
|
141
98
|
const memory = this._exports.memory;
|
|
@@ -147,7 +104,7 @@ class RbAbiGuest {
|
|
|
147
104
|
}
|
|
148
105
|
rbErrinfo() {
|
|
149
106
|
const ret = this._exports['rb-errinfo: func() -> handle<rb-abi-value>']();
|
|
150
|
-
return this.
|
|
107
|
+
return this._resource0_slab.remove(ret);
|
|
151
108
|
}
|
|
152
109
|
rbClearErrinfo() {
|
|
153
110
|
this._exports['rb-clear-errinfo: func() -> ()']();
|
|
@@ -157,7 +114,7 @@ class RbAbiGuest {
|
|
|
157
114
|
const obj0 = arg0;
|
|
158
115
|
if (!(obj0 instanceof RbAbiValue))
|
|
159
116
|
throw new TypeError('expected instance of RbAbiValue');
|
|
160
|
-
const ret = this._exports['rstring-ptr: func(value: handle<rb-abi-value>) -> string'](this.
|
|
117
|
+
const ret = this._exports['rstring-ptr: func(value: handle<rb-abi-value>) -> string'](this._resource0_slab.insert(obj0.clone()));
|
|
161
118
|
const ptr1 = (0, intrinsics_js_1.data_view)(memory).getInt32(ret + 0, true);
|
|
162
119
|
const len1 = (0, intrinsics_js_1.data_view)(memory).getInt32(ret + 4, true);
|
|
163
120
|
const result1 = intrinsics_js_1.UTF8_DECODER.decode(new Uint8Array(memory.buffer, ptr1, len1));
|
|
@@ -184,7 +141,7 @@ class RbAbiGuest {
|
|
|
184
141
|
}
|
|
185
142
|
}
|
|
186
143
|
exports.RbAbiGuest = RbAbiGuest;
|
|
187
|
-
class
|
|
144
|
+
class RbAbiValue {
|
|
188
145
|
constructor(wasm_val, obj) {
|
|
189
146
|
this._wasm_val = wasm_val;
|
|
190
147
|
this._obj = obj;
|
|
@@ -200,31 +157,6 @@ class RbIseq {
|
|
|
200
157
|
if (this._refcnt !== 0)
|
|
201
158
|
return;
|
|
202
159
|
this._obj._registry0.unregister(this);
|
|
203
|
-
const dtor = this._obj._exports['canonical_abi_drop_rb-iseq'];
|
|
204
|
-
const wasm_val = this._wasm_val;
|
|
205
|
-
delete this._obj;
|
|
206
|
-
delete this._refcnt;
|
|
207
|
-
delete this._wasm_val;
|
|
208
|
-
dtor(wasm_val);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
exports.RbIseq = RbIseq;
|
|
212
|
-
class RbAbiValue {
|
|
213
|
-
constructor(wasm_val, obj) {
|
|
214
|
-
this._wasm_val = wasm_val;
|
|
215
|
-
this._obj = obj;
|
|
216
|
-
this._refcnt = 1;
|
|
217
|
-
obj._registry1.register(this, wasm_val, this);
|
|
218
|
-
}
|
|
219
|
-
clone() {
|
|
220
|
-
this._refcnt += 1;
|
|
221
|
-
return this;
|
|
222
|
-
}
|
|
223
|
-
drop() {
|
|
224
|
-
this._refcnt -= 1;
|
|
225
|
-
if (this._refcnt !== 0)
|
|
226
|
-
return;
|
|
227
|
-
this._obj._registry1.unregister(this);
|
|
228
160
|
const dtor = this._obj._exports['canonical_abi_drop_rb-abi-value'];
|
|
229
161
|
const wasm_val = this._wasm_val;
|
|
230
162
|
delete this._obj;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.addRbJsAbiHostToImports =
|
|
3
|
+
exports.addRbJsAbiHostToImports = addRbJsAbiHostToImports;
|
|
4
4
|
const intrinsics_js_1 = require("./intrinsics.js");
|
|
5
5
|
function addRbJsAbiHostToImports(imports, obj, get_export) {
|
|
6
6
|
if (!("rb-js-abi-host" in imports))
|
|
@@ -280,4 +280,3 @@ function addRbJsAbiHostToImports(imports, obj, get_export) {
|
|
|
280
280
|
obj.dropJsAbiValue(val);
|
|
281
281
|
};
|
|
282
282
|
}
|
|
283
|
-
exports.addRbJsAbiHostToImports = addRbJsAbiHostToImports;
|
package/dist/cjs/binding.d.ts
CHANGED
|
@@ -6,10 +6,7 @@ import * as RbAbi from "./bindgen/legacy/rb-abi-guest.js";
|
|
|
6
6
|
*/
|
|
7
7
|
export interface Binding {
|
|
8
8
|
rubyShowVersion(): void;
|
|
9
|
-
rubyInit(): void;
|
|
10
|
-
rubySysinit(args: string[]): void;
|
|
11
|
-
rubyOptions(args: string[]): void;
|
|
12
|
-
rubyScript(name: string): void;
|
|
9
|
+
rubyInit(args: string[]): void;
|
|
13
10
|
rubyInitLoadpath(): void;
|
|
14
11
|
rbEvalStringProtect(str: string): [RbAbiValue, number];
|
|
15
12
|
rbFuncallvProtect(recv: RbAbiValue, mid: RbAbi.RbId, args: RbAbiValue[]): [RbAbiValue, number];
|
|
@@ -34,10 +31,7 @@ export declare class ComponentBinding implements Binding {
|
|
|
34
31
|
constructor();
|
|
35
32
|
setUnderlying(underlying: typeof RubyJsRubyRuntime): void;
|
|
36
33
|
rubyShowVersion(): void;
|
|
37
|
-
rubyInit(): void;
|
|
38
|
-
rubySysinit(args: string[]): void;
|
|
39
|
-
rubyOptions(args: string[]): void;
|
|
40
|
-
rubyScript(name: string): void;
|
|
34
|
+
rubyInit(args: string[]): void;
|
|
41
35
|
rubyInitLoadpath(): void;
|
|
42
36
|
rbEvalStringProtect(str: string): [RbAbiValue, number];
|
|
43
37
|
rbFuncallvProtect(recv: RbAbiValue, mid: number, args: RbAbiValue[]): [RbAbiValue, number];
|
package/dist/cjs/binding.js
CHANGED
|
@@ -17,17 +17,8 @@ class ComponentBinding {
|
|
|
17
17
|
rubyShowVersion() {
|
|
18
18
|
this.underlying.rubyShowVersion();
|
|
19
19
|
}
|
|
20
|
-
rubyInit() {
|
|
21
|
-
this.underlying.rubyInit();
|
|
22
|
-
}
|
|
23
|
-
rubySysinit(args) {
|
|
24
|
-
this.underlying.rubySysinit(args);
|
|
25
|
-
}
|
|
26
|
-
rubyOptions(args) {
|
|
27
|
-
this.underlying.rubyOptions(args);
|
|
28
|
-
}
|
|
29
|
-
rubyScript(name) {
|
|
30
|
-
this.underlying.rubyScript(name);
|
|
20
|
+
rubyInit(args) {
|
|
21
|
+
this.underlying.rubyInit(args);
|
|
31
22
|
}
|
|
32
23
|
rubyInitLoadpath() {
|
|
33
24
|
this.underlying.rubyInitLoadpath();
|
package/dist/cjs/browser.js
CHANGED
|
@@ -15,18 +15,16 @@ const DefaultRubyVM = async (rubyModule, options = {}) => {
|
|
|
15
15
|
new browser_wasi_shim_1.PreopenDirectory("/", new Map()),
|
|
16
16
|
];
|
|
17
17
|
const wasi = new browser_wasi_shim_1.WASI(args, env, fds, { debug: false });
|
|
18
|
-
const vm = new vm_js_1.RubyVM();
|
|
19
|
-
const imports = {
|
|
20
|
-
wasi_snapshot_preview1: wasi.wasiImport,
|
|
21
|
-
};
|
|
22
|
-
vm.addToImports(imports);
|
|
23
18
|
const printer = ((_b = options.consolePrint) !== null && _b !== void 0 ? _b : true) ? (0, console_js_1.consolePrinter)() : undefined;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
const { vm, instance } = await vm_js_1.RubyVM.instantiateModule({
|
|
20
|
+
module: rubyModule, wasip1: wasi,
|
|
21
|
+
addToImports: (imports) => {
|
|
22
|
+
printer === null || printer === void 0 ? void 0 : printer.addToImports(imports);
|
|
23
|
+
},
|
|
24
|
+
setMemory: (memory) => {
|
|
25
|
+
printer === null || printer === void 0 ? void 0 : printer.setMemory(memory);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
30
28
|
return {
|
|
31
29
|
vm,
|
|
32
30
|
wasi,
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { DefaultRubyVM } from "./browser.js";
|
|
2
|
+
import { RubyComponentInstantiator } from "./vm.js";
|
|
3
|
+
/**
|
|
4
|
+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Core Module.
|
|
5
|
+
*/
|
|
2
6
|
export declare const main: (pkg: {
|
|
3
7
|
name: string;
|
|
4
8
|
version: string;
|
|
5
9
|
}, options?: Parameters<typeof DefaultRubyVM>[1]) => Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Component.
|
|
12
|
+
*/
|
|
13
|
+
export declare const componentMain: (pkg: {
|
|
14
|
+
name: string;
|
|
15
|
+
version: string;
|
|
16
|
+
}, options: {
|
|
17
|
+
instantiate: RubyComponentInstantiator;
|
|
18
|
+
wasip2: any;
|
|
19
|
+
}) => Promise<void>;
|
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.main = void 0;
|
|
3
|
+
exports.componentMain = exports.main = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const browser_js_1 = require("./browser.js");
|
|
6
|
+
const vm_js_1 = require("./vm.js");
|
|
7
|
+
/**
|
|
8
|
+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Core Module.
|
|
9
|
+
*/
|
|
6
10
|
const main = async (pkg, options) => {
|
|
7
11
|
const response = fetch(`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`);
|
|
8
12
|
const module = await compileWebAssemblyModule(response);
|
|
9
13
|
const { vm } = await (0, browser_js_1.DefaultRubyVM)(module, options);
|
|
14
|
+
await mainWithRubyVM(vm);
|
|
15
|
+
};
|
|
16
|
+
exports.main = main;
|
|
17
|
+
/**
|
|
18
|
+
* The main entry point of `<script type="text/ruby">`-based scripting with WebAssembly Component.
|
|
19
|
+
*/
|
|
20
|
+
const componentMain = async (pkg, options) => {
|
|
21
|
+
const componentUrl = `https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/component`;
|
|
22
|
+
const fetchComponentFile = (relativePath) => fetch(`${componentUrl}/${relativePath}`);
|
|
23
|
+
const { vm } = await vm_js_1.RubyVM.instantiateComponent(Object.assign(Object.assign({}, options), { getCoreModule: (relativePath) => {
|
|
24
|
+
const response = fetchComponentFile(relativePath);
|
|
25
|
+
return compileWebAssemblyModule(response);
|
|
26
|
+
} }));
|
|
27
|
+
await mainWithRubyVM(vm);
|
|
28
|
+
};
|
|
29
|
+
exports.componentMain = componentMain;
|
|
30
|
+
const mainWithRubyVM = async (vm) => {
|
|
10
31
|
vm.printVersion();
|
|
11
32
|
globalThis.rubyVM = vm;
|
|
12
33
|
// Wait for the text/ruby script tag to be read.
|
|
@@ -19,7 +40,6 @@ const main = async (pkg, options) => {
|
|
|
19
40
|
runRubyScriptsInHtml(vm);
|
|
20
41
|
}
|
|
21
42
|
};
|
|
22
|
-
exports.main = main;
|
|
23
43
|
const runRubyScriptsInHtml = async (vm) => {
|
|
24
44
|
var _a, e_1, _b, _c;
|
|
25
45
|
const tags = document.querySelectorAll('script[type="text/ruby"]');
|
package/dist/cjs/console.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.consolePrinter =
|
|
3
|
+
exports.consolePrinter = consolePrinter;
|
|
4
4
|
/**
|
|
5
5
|
* Create a console printer that can be used as an overlay of WASI imports.
|
|
6
6
|
* See the example below for how to use it.
|
|
@@ -103,4 +103,3 @@ function consolePrinter({ stdout, stderr, } = {
|
|
|
103
103
|
},
|
|
104
104
|
};
|
|
105
105
|
}
|
|
106
|
-
exports.consolePrinter = consolePrinter;
|
package/dist/cjs/node.d.ts
CHANGED
package/dist/cjs/node.js
CHANGED
|
@@ -5,15 +5,7 @@ const wasi_1 = require("wasi");
|
|
|
5
5
|
const vm_js_1 = require("./vm.js");
|
|
6
6
|
const DefaultRubyVM = async (rubyModule, options = {}) => {
|
|
7
7
|
const wasi = new wasi_1.WASI({ env: options.env, version: "preview1", returnOnExit: true });
|
|
8
|
-
const vm =
|
|
9
|
-
const imports = {
|
|
10
|
-
wasi_snapshot_preview1: wasi.wasiImport,
|
|
11
|
-
};
|
|
12
|
-
vm.addToImports(imports);
|
|
13
|
-
const instance = await WebAssembly.instantiate(rubyModule, imports);
|
|
14
|
-
await vm.setInstance(instance);
|
|
15
|
-
wasi.initialize(instance);
|
|
16
|
-
vm.initialize();
|
|
8
|
+
const { vm, instance } = await vm_js_1.RubyVM.instantiateModule({ module: rubyModule, wasip1: wasi });
|
|
17
9
|
return {
|
|
18
10
|
vm,
|
|
19
11
|
wasi,
|
package/dist/cjs/vm.d.ts
CHANGED
|
@@ -2,39 +2,172 @@ import type { RubyJsJsRuntime } from "./bindgen/interfaces/ruby-js-js-runtime.js
|
|
|
2
2
|
import type { RubyJsRubyRuntime } from "./bindgen/interfaces/ruby-js-ruby-runtime.js";
|
|
3
3
|
import { JsAbiValue } from "./bindgen/legacy/rb-js-abi-host.js";
|
|
4
4
|
import { Binding, RbAbiValue } from "./binding.js";
|
|
5
|
+
/**
|
|
6
|
+
* A function type that instantiates a Ruby component
|
|
7
|
+
*/
|
|
8
|
+
export type RubyComponentInstantiator = ((getCoreModule: (path: string) => WebAssembly.Module, importObject: any, instantiateCore?: (module: WebAssembly.Module, imports: Record<string, any>) => WebAssembly.Instance | Promise<WebAssembly.Instance>) => Promise<({
|
|
9
|
+
rubyRuntime: typeof RubyJsRubyRuntime;
|
|
10
|
+
})>);
|
|
11
|
+
export type RubyInitComponentOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* A lower-level instantiation function that instantiates the Ruby component with the given component
|
|
14
|
+
* that implements "ruby:js/js-runtime" WIT interface.
|
|
15
|
+
*/
|
|
16
|
+
instantiate: (_: typeof RubyJsJsRuntime) => Promise<typeof RubyJsRubyRuntime>;
|
|
17
|
+
/**
|
|
18
|
+
* The arguments to pass to the Ruby VM. Note that the first argument must be the Ruby program name.
|
|
19
|
+
*
|
|
20
|
+
* @default ["ruby.wasm", "-EUTF-8", "-e_=0"]
|
|
21
|
+
*/
|
|
22
|
+
args?: string[];
|
|
23
|
+
} | {
|
|
24
|
+
/**
|
|
25
|
+
* An `instantiate` function generated by `@bytecodealliance/jco` that instantiates the Ruby component.
|
|
26
|
+
*/
|
|
27
|
+
instantiate: RubyComponentInstantiator;
|
|
28
|
+
/**
|
|
29
|
+
* A function that returns a WebAssembly Core module within the Ruby component transpiled by `@bytecodealliance/jco`.
|
|
30
|
+
*/
|
|
31
|
+
getCoreModule: (path: string) => Promise<WebAssembly.Module>;
|
|
32
|
+
/**
|
|
33
|
+
* An optional function used to instantiate a WebAssembly Core module
|
|
34
|
+
*/
|
|
35
|
+
instantiateCore?: (module: WebAssembly.Module, imports: Record<string, any>) => WebAssembly.Instance | Promise<WebAssembly.Instance>;
|
|
36
|
+
/**
|
|
37
|
+
* WASI Preview 2 implementation, typically imported from `import * as wasip2 from "@bytecodealliance/preview2-shim"`
|
|
38
|
+
*/
|
|
39
|
+
wasip2: any;
|
|
40
|
+
/**
|
|
41
|
+
* The arguments to pass to the Ruby VM. Note that the first argument must be the Ruby program name.
|
|
42
|
+
*
|
|
43
|
+
* @default ["ruby.wasm", "-EUTF-8", "-e_=0"]
|
|
44
|
+
*/
|
|
45
|
+
args?: string[];
|
|
46
|
+
};
|
|
47
|
+
export type RubyInitModuleOptions = {
|
|
48
|
+
/**
|
|
49
|
+
* The WebAssembly module that contains the Ruby VM
|
|
50
|
+
*/
|
|
51
|
+
module: WebAssembly.Module;
|
|
52
|
+
/**
|
|
53
|
+
* WASI Preview 1 implementation supporting reactor model ABI
|
|
54
|
+
*/
|
|
55
|
+
wasip1: {
|
|
56
|
+
wasiImport: WebAssembly.ModuleImports;
|
|
57
|
+
initialize(instance: WebAssembly.Instance): void;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The arguments to pass to the Ruby VM. Note that the first argument must be the Ruby program name.
|
|
61
|
+
* @default ["ruby.wasm", "-EUTF-8", "-e_=0"]
|
|
62
|
+
*/
|
|
63
|
+
args?: string[];
|
|
64
|
+
/**
|
|
65
|
+
* A hook to add additional imports to the WebAssembly instance
|
|
66
|
+
*/
|
|
67
|
+
addToImports?: (imports: WebAssembly.Imports) => void;
|
|
68
|
+
/**
|
|
69
|
+
* A hook called with the WebAssembly memory instance just after the Ruby VM is instantiated
|
|
70
|
+
*/
|
|
71
|
+
setMemory?: (memory: WebAssembly.Memory) => void;
|
|
72
|
+
};
|
|
73
|
+
export type RubyInitOptions = RubyInitComponentOptions | RubyInitModuleOptions;
|
|
5
74
|
/**
|
|
6
75
|
* A Ruby VM instance
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
* const wasi = new WASI();
|
|
11
|
-
* const vm = new RubyVM();
|
|
12
|
-
* const imports = {
|
|
13
|
-
* wasi_snapshot_preview1: wasi.wasiImport,
|
|
14
|
-
* };
|
|
15
|
-
*
|
|
16
|
-
* vm.addToImports(imports);
|
|
17
|
-
*
|
|
18
|
-
* const instance = await WebAssembly.instantiate(rubyModule, imports);
|
|
19
|
-
* await vm.setInstance(instance);
|
|
20
|
-
* wasi.initialize(instance);
|
|
21
|
-
* vm.initialize();
|
|
22
|
-
*
|
|
76
|
+
* @see {@link RubyVM.instantiateComponent} and {@link RubyVM.instantiateModule} to create a new instance
|
|
77
|
+
* @category Essentials
|
|
23
78
|
*/
|
|
24
79
|
export declare class RubyVM {
|
|
80
|
+
/**
|
|
81
|
+
* @private Only for internal use.
|
|
82
|
+
*/
|
|
25
83
|
guest: Binding;
|
|
26
84
|
private instance;
|
|
27
85
|
private transport;
|
|
28
86
|
private exceptionFormatter;
|
|
29
87
|
private interfaceState;
|
|
88
|
+
/**
|
|
89
|
+
* Instantiate a Ruby VM with the given WebAssembly Core module with WASI Preview 1 implementation.
|
|
90
|
+
*
|
|
91
|
+
* @param options The options to instantiate the Ruby VM
|
|
92
|
+
* @returns A promise that resolves to the Ruby VM instance and the WebAssembly instance
|
|
93
|
+
* @category Essentials
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
*
|
|
97
|
+
* import { WASI } from "@bjorn3/browser_wasi_shim";
|
|
98
|
+
* const wasip1 = new WASI([], [], []);
|
|
99
|
+
* const module = await WebAssembly.compile("./path/to/ruby.wasm");
|
|
100
|
+
* const { vm } = await RubyVM.instantiateModule({ module, wasip1 });
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
static instantiateModule(options: RubyInitModuleOptions): Promise<{
|
|
104
|
+
vm: RubyVM;
|
|
105
|
+
instance: WebAssembly.Instance;
|
|
106
|
+
}>;
|
|
107
|
+
/**
|
|
108
|
+
* Instantiate a Ruby VM with the given WebAssembly component with WASI Preview 2 implementation.
|
|
109
|
+
*
|
|
110
|
+
* @param options The options to instantiate the Ruby VM
|
|
111
|
+
* @returns A promise that resolves to the Ruby VM instance
|
|
112
|
+
* @category Essentials
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
*
|
|
116
|
+
* // First, you need to transpile the Ruby component to a JavaScript module using jco.
|
|
117
|
+
* // $ jco transpile --no-wasi-shim --instantiation --valid-lifting-optimization ./ruby.component.wasm -o ./component
|
|
118
|
+
* // Then, you can instantiate the Ruby VM with the component:
|
|
119
|
+
*
|
|
120
|
+
* import * as wasip2 from "@bytecodealliance/preview2-shim"
|
|
121
|
+
* import fs from "fs/promises";
|
|
122
|
+
* import path from "path";
|
|
123
|
+
*
|
|
124
|
+
* const { instantiate } = await import("./component/ruby.component.js");
|
|
125
|
+
* const getCoreModule = async (relativePath) => {
|
|
126
|
+
* const buffer = await fs.readFile(path.join("./component", relativePath));
|
|
127
|
+
* return WebAssembly.compile(buffer);
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* const { vm } = await RubyVM.instantiateComponent({
|
|
131
|
+
* instantiate, getCoreModule, wasip2,
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
static instantiateComponent(options: RubyInitComponentOptions): Promise<{
|
|
136
|
+
vm: RubyVM;
|
|
137
|
+
}>;
|
|
138
|
+
/**
|
|
139
|
+
* Create a new Ruby VM instance with low-level initialization control.
|
|
140
|
+
*
|
|
141
|
+
* @category Low-level initialization
|
|
142
|
+
* @example
|
|
143
|
+
*
|
|
144
|
+
* ```javascript
|
|
145
|
+
* const wasi = new WASI();
|
|
146
|
+
* const vm = new RubyVM();
|
|
147
|
+
* const imports = {
|
|
148
|
+
* wasi_snapshot_preview1: wasi.wasiImport,
|
|
149
|
+
* };
|
|
150
|
+
*
|
|
151
|
+
* vm.addToImports(imports);
|
|
152
|
+
*
|
|
153
|
+
* const instance = await WebAssembly.instantiate(rubyModule, imports);
|
|
154
|
+
* await vm.setInstance(instance);
|
|
155
|
+
* wasi.initialize(instance);
|
|
156
|
+
* vm.initialize();
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
*/
|
|
160
|
+
constructor();
|
|
161
|
+
/**
|
|
162
|
+
* @private Only for internal use.
|
|
163
|
+
*/
|
|
30
164
|
constructor(binding?: Binding);
|
|
31
|
-
static _instantiate
|
|
32
|
-
args?: string[];
|
|
33
|
-
}): Promise<RubyVM>;
|
|
165
|
+
private static _instantiate;
|
|
34
166
|
/**
|
|
35
167
|
* Initialize the Ruby VM with the given command line arguments
|
|
36
168
|
* @param args The command line arguments to pass to Ruby. Must be
|
|
37
169
|
* an array of strings starting with the Ruby program name.
|
|
170
|
+
* @category Low-level initialization
|
|
38
171
|
*/
|
|
39
172
|
initialize(args?: string[]): void;
|
|
40
173
|
/**
|
|
@@ -45,12 +178,14 @@ export declare class RubyVM {
|
|
|
45
178
|
* @param instance The WebAssembly instance to interact with. Must
|
|
46
179
|
* be instantiated from a Ruby built with JS extension, and built
|
|
47
180
|
* with Reactor ABI instead of command line.
|
|
181
|
+
* @category Low-level initialization
|
|
48
182
|
*/
|
|
49
183
|
setInstance(instance: WebAssembly.Instance): Promise<void>;
|
|
50
184
|
/**
|
|
51
185
|
* Add intrinsic import entries, which is necessary to interact JavaScript
|
|
52
186
|
* and Ruby's WebAssembly instance.
|
|
53
187
|
* @param imports The import object to add to the WebAssembly instance
|
|
188
|
+
* @category Low-level initialization
|
|
54
189
|
*/
|
|
55
190
|
addToImports(imports: WebAssembly.Imports): void;
|
|
56
191
|
private throwProhibitRewindException;
|
|
@@ -63,6 +198,7 @@ export declare class RubyVM {
|
|
|
63
198
|
* Runs a string of Ruby code from JavaScript
|
|
64
199
|
* @param code The Ruby code to run
|
|
65
200
|
* @returns the result of the last expression
|
|
201
|
+
* @category Essentials
|
|
66
202
|
*
|
|
67
203
|
* @example
|
|
68
204
|
* vm.eval("puts 'hello world'");
|
|
@@ -76,6 +212,7 @@ export declare class RubyVM {
|
|
|
76
212
|
* Returns a promise that resolves when execution completes.
|
|
77
213
|
* @param code The Ruby code to run
|
|
78
214
|
* @returns a promise that resolves to the result of the last expression
|
|
215
|
+
* @category Essentials
|
|
79
216
|
*
|
|
80
217
|
* @example
|
|
81
218
|
* const text = await vm.evalAsync(`
|
|
@@ -131,6 +268,7 @@ declare class JsValueTransport {
|
|
|
131
268
|
}
|
|
132
269
|
/**
|
|
133
270
|
* A RbValue is an object that represents a value in Ruby
|
|
271
|
+
* @category Essentials
|
|
134
272
|
*/
|
|
135
273
|
export declare class RbValue {
|
|
136
274
|
private inner;
|