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