Skip to content

Commit a9c9c6a

Browse files
rake doc:api_js
1 parent d5bcc56 commit a9c9c6a

File tree

1 file changed

+67
-1
lines changed
  • packages/npm-packages/ruby-wasm-wasi

1 file changed

+67
-1
lines changed

packages/npm-packages/ruby-wasm-wasi/README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ See [Cheat Sheet](https://github.com/ruby/ruby.wasm/blob/main/docs/cheat_sheet.m
1818

1919
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2020

21+
### consolePrinter
22+
23+
Create a console printer that can be used as an overlay of WASI imports.
24+
See the example below for how to use it.
25+
26+
```javascript
27+
const imports = {
28+
wasi_snapshot_preview1: wasi.wasiImport,
29+
};
30+
const printer = consolePrinter();
31+
printer.addToImports(imports);
32+
33+
const instance = await WebAssembly.instantiate(module, imports);
34+
printer.setMemory(instance.exports.memory);
35+
```
36+
37+
Note that the `stdout` and `stderr` functions are called with text, not
38+
bytes. This means that bytes written to stdout/stderr will be decoded as
39+
UTF-8 and then passed to the `stdout`/`stderr` functions every time a write
40+
occurs without buffering.
41+
42+
#### Parameters
43+
44+
- `$0` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{stdout:console.log,stderr:console.warn}`)
45+
46+
- `$0.stdout` &#x20;
47+
- `$0.stderr` &#x20;
48+
49+
- `stdout` A function that will be called when stdout is written to.
50+
Defaults to `console.log`.
51+
- `stderr` A function that will be called when stderr is written to.
52+
Defaults to `console.warn`.
53+
54+
Returns **any** An object that can be used as an overlay of WASI imports.
55+
2156
### RubyVM
2257

2358
A Ruby VM instance
@@ -46,7 +81,7 @@ Initialize the Ruby VM with the given command line arguments
4681
##### Parameters
4782

4883
- `args` The command line arguments to pass to Ruby. Must be
49-
an array of strings starting with the Ruby program name. (optional, default `["ruby.wasm","--disable-gems","-EUTF-8","-e_=0"]`)
84+
an array of strings starting with the Ruby program name. (optional, default `["ruby.wasm","-EUTF-8","-e_=0"]`)
5085

5186
#### setInstance
5287

@@ -151,6 +186,37 @@ ary.call("push", 4);
151186
console.log(ary.call("sample").toString());
152187
```
153188

189+
Returns **any** The result of the method call as a new RbValue.
190+
191+
#### callAsync
192+
193+
Call a given method that may call `JS::Object#await` with given arguments
194+
195+
##### Parameters
196+
197+
- `callee` name of the Ruby method to call
198+
- `args` **...any** arguments to pass to the method. Must be an array of RbValue
199+
200+
##### Examples
201+
202+
```javascript
203+
const client = vm.eval(`
204+
require 'js'
205+
class HttpClient
206+
def get(url)
207+
JS.global.fetch(url).await
208+
end
209+
end
210+
HttpClient.new
211+
`);
212+
const response = await client.callAsync(
213+
"get",
214+
vm.eval(`"https://example.com"`),
215+
);
216+
```
217+
218+
Returns **any** A Promise that resolves to the result of the method call as a new RbValue.
219+
154220
#### toPrimitive
155221

156222
- **See**: <https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive>

0 commit comments

Comments
 (0)