@ruby/wasm-wasi 2.4.1 → 2.5.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,216 @@
1
+ import * as RbAbi from "./bindgen/rb-abi-guest.js";
2
+ import { JsAbiValue } from "./bindgen/rb-js-abi-host.js";
3
+ /**
4
+ * A Ruby VM instance
5
+ *
6
+ * @example
7
+ *
8
+ * const wasi = new WASI();
9
+ * const vm = new RubyVM();
10
+ * const imports = {
11
+ * wasi_snapshot_preview1: wasi.wasiImport,
12
+ * };
13
+ *
14
+ * vm.addToImports(imports);
15
+ *
16
+ * const instance = await WebAssembly.instantiate(rubyModule, imports);
17
+ * await vm.setInstance(instance);
18
+ * wasi.initialize(instance);
19
+ * vm.initialize();
20
+ *
21
+ */
22
+ export declare class RubyVM {
23
+ guest: RbAbi.RbAbiGuest;
24
+ private instance;
25
+ private transport;
26
+ private exceptionFormatter;
27
+ private interfaceState;
28
+ constructor();
29
+ /**
30
+ * Initialize the Ruby VM with the given command line arguments
31
+ * @param args The command line arguments to pass to Ruby. Must be
32
+ * an array of strings starting with the Ruby program name.
33
+ */
34
+ initialize(args?: string[]): void;
35
+ /**
36
+ * Set a given instance to interact JavaScript and Ruby's
37
+ * WebAssembly instance. This method must be called before calling
38
+ * Ruby API.
39
+ *
40
+ * @param instance The WebAssembly instance to interact with. Must
41
+ * be instantiated from a Ruby built with JS extension, and built
42
+ * with Reactor ABI instead of command line.
43
+ */
44
+ setInstance(instance: WebAssembly.Instance): Promise<void>;
45
+ /**
46
+ * Add intrinsic import entries, which is necessary to interact JavaScript
47
+ * and Ruby's WebAssembly instance.
48
+ * @param imports The import object to add to the WebAssembly instance
49
+ */
50
+ addToImports(imports: WebAssembly.Imports): void;
51
+ /**
52
+ * Print the Ruby version to stdout
53
+ */
54
+ printVersion(): void;
55
+ /**
56
+ * Runs a string of Ruby code from JavaScript
57
+ * @param code The Ruby code to run
58
+ * @returns the result of the last expression
59
+ *
60
+ * @example
61
+ * vm.eval("puts 'hello world'");
62
+ * const result = vm.eval("1 + 2");
63
+ * console.log(result.toString()); // 3
64
+ *
65
+ */
66
+ eval(code: string): RbValue;
67
+ /**
68
+ * Runs a string of Ruby code with top-level `JS::Object#await`
69
+ * Returns a promise that resolves when execution completes.
70
+ * @param code The Ruby code to run
71
+ * @returns a promise that resolves to the result of the last expression
72
+ *
73
+ * @example
74
+ * const text = await vm.evalAsync(`
75
+ * require 'js'
76
+ * response = JS.global.fetch('https://example.com').await
77
+ * response.text.await
78
+ * `);
79
+ * console.log(text.toString()); // <html>...</html>
80
+ */
81
+ evalAsync(code: string): Promise<RbValue>;
82
+ /**
83
+ * Wrap a JavaScript value into a Ruby JS::Object
84
+ * @param value The value to convert to RbValue
85
+ * @returns the RbValue object representing the given JS value
86
+ *
87
+ * @example
88
+ * const hash = vm.eval(`Hash.new`)
89
+ * hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));
90
+ */
91
+ wrap(value: any): RbValue;
92
+ /** @private */
93
+ private privateObject;
94
+ /** @private */
95
+ private rbValueOfPointer;
96
+ }
97
+ /**
98
+ * Export a JS value held by the Ruby VM to the JS environment.
99
+ * This is implemented in a dirty way since wit cannot reference resources
100
+ * defined in other interfaces.
101
+ * In our case, we can't express `function(v: rb-abi-value) -> js-abi-value`
102
+ * because `rb-js-abi-host.wit`, that defines `js-abi-value`, is implemented
103
+ * by embedder side (JS) but `rb-abi-guest.wit`, that defines `rb-abi-value`
104
+ * is implemented by guest side (Wasm).
105
+ *
106
+ * This class is a helper to export by:
107
+ * 1. Call `function __export_to_js(v: rb-abi-value)` defined in guest from embedder side.
108
+ * 2. Call `function takeJsValue(v: js-abi-value)` defined in embedder from guest side with
109
+ * underlying JS value of given `rb-abi-value`.
110
+ * 3. Then `takeJsValue` implementation escapes the given JS value to the `_takenJsValues`
111
+ * stored in embedder side.
112
+ * 4. Finally, embedder side can take `_takenJsValues`.
113
+ *
114
+ * Note that `exportJsValue` is not reentrant.
115
+ *
116
+ * @private
117
+ */
118
+ declare class JsValueTransport {
119
+ private _takenJsValue;
120
+ takeJsValue(value: JsAbiValue): void;
121
+ consumeJsValue(): JsAbiValue;
122
+ exportJsValue(value: RbValue): JsAbiValue;
123
+ importJsValue(value: JsAbiValue, vm: RubyVM): RbValue;
124
+ }
125
+ /**
126
+ * A RbValue is an object that represents a value in Ruby
127
+ */
128
+ export declare class RbValue {
129
+ private inner;
130
+ private vm;
131
+ private privateObject;
132
+ /**
133
+ * @hideconstructor
134
+ */
135
+ constructor(inner: RbAbi.RbAbiValue, vm: RubyVM, privateObject: RubyVMPrivate);
136
+ /**
137
+ * Call a given method with given arguments
138
+ *
139
+ * @param callee name of the Ruby method to call
140
+ * @param args arguments to pass to the method. Must be an array of RbValue
141
+ * @returns The result of the method call as a new RbValue.
142
+ *
143
+ * @example
144
+ * const ary = vm.eval("[1, 2, 3]");
145
+ * ary.call("push", 4);
146
+ * console.log(ary.call("sample").toString());
147
+ */
148
+ call(callee: string, ...args: RbValue[]): RbValue;
149
+ /**
150
+ * Call a given method that may call `JS::Object#await` with given arguments
151
+ *
152
+ * @param callee name of the Ruby method to call
153
+ * @param args arguments to pass to the method. Must be an array of RbValue
154
+ * @returns A Promise that resolves to the result of the method call as a new RbValue.
155
+ *
156
+ * @example
157
+ * const client = vm.eval(`
158
+ * require 'js'
159
+ * class HttpClient
160
+ * def get(url)
161
+ * JS.global.fetch(url).await
162
+ * end
163
+ * end
164
+ * HttpClient.new
165
+ * `);
166
+ * const response = await client.callAsync("get", vm.eval(`"https://example.com"`));
167
+ */
168
+ callAsync(callee: string, ...args: RbValue[]): Promise<RbValue>;
169
+ /**
170
+ * @see {@link https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive}
171
+ * @param hint Preferred type of the result primitive value. `"number"`, `"string"`, or `"default"`.
172
+ */
173
+ [Symbol.toPrimitive](hint: string): string;
174
+ /**
175
+ * Returns a string representation of the value by calling `to_s`
176
+ */
177
+ toString(): string;
178
+ /**
179
+ * Returns a JavaScript object representation of the value
180
+ * by calling `to_js`.
181
+ *
182
+ * Returns null if the value is not convertible to a JavaScript object.
183
+ */
184
+ toJS(): any;
185
+ }
186
+ type RubyVMPrivate = {
187
+ transport: JsValueTransport;
188
+ exceptionFormatter: RbExceptionFormatter;
189
+ };
190
+ declare class RbExceptionFormatter {
191
+ private literalsCache;
192
+ private isFormmatting;
193
+ format(error: RbValue, vm: RubyVM, privateObject: RubyVMPrivate): string;
194
+ private _format;
195
+ formatString(klass: string, message: string, backtrace?: [string, string]): string;
196
+ }
197
+ /**
198
+ * Error class thrown by Ruby execution
199
+ */
200
+ export declare class RbError extends Error {
201
+ /**
202
+ * @hideconstructor
203
+ */
204
+ constructor(message: string);
205
+ }
206
+ /**
207
+ * Error class thrown by Ruby execution when it is not possible to recover.
208
+ * This is usually caused when Ruby VM is in an inconsistent state.
209
+ */
210
+ export declare class RbFatalError extends RbError {
211
+ /**
212
+ * @hideconstructor
213
+ */
214
+ constructor(message: string);
215
+ }
216
+ export {};