Skip to content

Commit d0db285

Browse files
Update README.md in ruby-head-wasm-wasi
1 parent a35575e commit d0db285

File tree

1 file changed

+76
-41
lines changed
  • packages/npm-packages/ruby-head-wasm-wasi

1 file changed

+76
-41
lines changed

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

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { DefaultRubyVM } from "ruby-head-wasm-wasi/dist/node.cjs.js";
2626

2727
const main = async () => {
2828
const binary = await fs.readFile(
29-
// Tips: Replace the binary with debug info if you want symbolicated stack trace.
30-
// (only nightly release for now)
31-
// "./node_modules/ruby-head-wasm-wasi/dist/ruby.debug.wasm"
29+
// Tips: Replace the binary with debug info if you want symbolicated stack trace.
30+
// (only nightly release for now)
31+
// "./node_modules/ruby-head-wasm-wasi/dist/ruby.debug.wasm"
3232
"./node_modules/ruby-head-wasm-wasi/dist/ruby.wasm"
3333
);
3434
const module = await WebAssembly.compile(binary);
@@ -62,9 +62,9 @@ See [the example project](https://github.com/ruby/ruby.wasm/tree/main/packages/n
6262
const main = async () => {
6363
// Fetch and instntiate WebAssembly binary
6464
const response = await fetch(
65-
// Tips: Replace the binary with debug info if you want symbolicated stack trace.
66-
// (only nightly release for now)
67-
// "https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.debug.wasm"
65+
// Tips: Replace the binary with debug info if you want symbolicated stack trace.
66+
// (only nightly release for now)
67+
// "https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.debug.wasm"
6868
"https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.wasm"
6969
);
7070
const buffer = await response.arrayBuffer();
@@ -85,7 +85,6 @@ See [the example project](https://github.com/ruby/ruby.wasm/tree/main/packages/n
8585
</html>
8686
```
8787

88-
8988
## GC limitation with JavaScript interoperability
9089

9190
Since JavaScript's GC system and Ruby's GC system are separated and not cooperative, they cannot collect cyclic references between JavaScript and Ruby objects.
@@ -107,50 +106,47 @@ class RNode
107106
end
108107
end
109108
RNode.new
110-
`)
109+
`);
111110

112-
rnode.call("set_jnode", vm.wrap(jnode))
111+
rnode.call("set_jnode", vm.wrap(jnode));
113112
jnode.setRNode(rnode);
114113
```
115114

116-
<!-- The APIs section was generated by `npx documentation readme ./dist/index.esm.js --section=APIs` -->
115+
<!-- The APIs section was generated by `npx documentation readme ../ruby-wasm-wasi/dist/index.esm.js --section=APIs` -->
117116

118117
## APIs
119118

120119
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
121120

122121
#### Table of Contents
123122

124-
- [ruby-head-wasm-wasi](#ruby-head-wasm-wasi)
125-
- [Installation](#installation)
126-
- [Quick Start (for Node.js)](#quick-start-for-nodejs)
127-
- [Quick Start (for Browser)](#quick-start-for-browser)
128-
- [APIs](#apis)
129-
- [Table of Contents](#table-of-contents)
130-
- [RubyVM](#rubyvm)
131-
- [Examples](#examples)
132-
- [initialize](#initialize)
133-
- [Parameters](#parameters)
134-
- [setInstance](#setinstance)
135-
- [Parameters](#parameters-1)
136-
- [addToImports](#addtoimports)
137-
- [Parameters](#parameters-2)
138-
- [printVersion](#printversion)
139-
- [eval](#eval)
140-
- [Parameters](#parameters-3)
141-
- [Examples](#examples-1)
142-
- [RbValue](#rbvalue)
143-
- [Parameters](#parameters-4)
144-
- [call](#call)
145-
- [Parameters](#parameters-5)
146-
- [Examples](#examples-2)
147-
- [toPrimitive](#toprimitive)
148-
- [Parameters](#parameters-6)
149-
- [toString](#tostring)
150-
- [toJS](#tojs)
151-
- [RbError](#rberror)
152-
- [Parameters](#parameters-7)
153-
- [Building the package from source](#building-the-package-from-source)
123+
- [RubyVM](#rubyvm)
124+
- [Examples](#examples)
125+
- [initialize](#initialize)
126+
- [Parameters](#parameters)
127+
- [setInstance](#setinstance)
128+
- [Parameters](#parameters-1)
129+
- [addToImports](#addtoimports)
130+
- [Parameters](#parameters-2)
131+
- [printVersion](#printversion)
132+
- [eval](#eval)
133+
- [Parameters](#parameters-3)
134+
- [Examples](#examples-1)
135+
- [wrap](#wrap)
136+
- [Parameters](#parameters-4)
137+
- [Examples](#examples-2)
138+
- [JsValueTransport](#jsvaluetransport)
139+
- [RbValue](#rbvalue)
140+
- [Parameters](#parameters-5)
141+
- [call](#call)
142+
- [Parameters](#parameters-6)
143+
- [Examples](#examples-3)
144+
- [toPrimitive](#toprimitive)
145+
- [Parameters](#parameters-7)
146+
- [toString](#tostring)
147+
- [toJS](#tojs)
148+
- [RbError](#rberror)
149+
- [Parameters](#parameters-8)
154150

155151
### RubyVM
156152

@@ -170,6 +166,7 @@ vm.addToImports(imports);
170166
const instance = await WebAssembly.instantiate(rubyModule, imports);
171167
await vm.setInstance(instance);
172168
wasi.initialize(instance);
169+
vm.initialize();
173170
```
174171

175172
#### initialize
@@ -224,6 +221,44 @@ console.log(result.toString()); // 3
224221

225222
Returns **any** the result of the last expression
226223

224+
#### wrap
225+
226+
Wrap a JavaScript value into a Ruby JS::Object
227+
228+
##### Parameters
229+
230+
- `value` The value to convert to RbValue
231+
232+
##### Examples
233+
234+
```javascript
235+
const hash = vm.eval(`Hash.new`);
236+
hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));
237+
```
238+
239+
Returns **any** the RbValue object representing the given JS value
240+
241+
### JsValueTransport
242+
243+
Export a JS value held by the Ruby VM to the JS environment.
244+
This is implemented in a dirty way since wit cannot reference resources
245+
defined in other interfaces.
246+
In our case, we can't express `function(v: rb-abi-value) -> js-abi-value`
247+
because `rb-js-abi-host.wit`, that defines `js-abi-value`, is implemented
248+
by embedder side (JS) but `rb-abi-guest.wit`, that defines `rb-abi-value`
249+
is implemented by guest side (Wasm).
250+
251+
This class is a helper to export by:
252+
253+
1. Call `function __export_to_js(v: rb-abi-value)` defined in guest from embedder side.
254+
2. Call `function takeJsValue(v: js-abi-value)` defined in embedder from guest side with
255+
underlying JS value of given `rb-abi-value`.
256+
3. Then `takeJsValue` implementation escapes the given JS value to the `_takenJsValues`
257+
stored in embedder side.
258+
4. Finally, embedder side can take `_takenJsValues`.
259+
260+
Note that `exportJsValue` is not reentrant.
261+
227262
### RbValue
228263

229264
A RbValue is an object that represents a value in Ruby
@@ -232,7 +267,7 @@ A RbValue is an object that represents a value in Ruby
232267

233268
- `inner`
234269
- `vm`
235-
- `exporter`
270+
- `privateObject`
236271

237272
#### call
238273

0 commit comments

Comments
 (0)