@@ -26,9 +26,9 @@ import { DefaultRubyVM } from "ruby-head-wasm-wasi/dist/node.cjs.js";
26
26
27
27
const main = async () => {
28
28
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"
32
32
" ./node_modules/ruby-head-wasm-wasi/dist/ruby.wasm"
33
33
);
34
34
const module = await WebAssembly .compile (binary);
@@ -62,9 +62,9 @@ See [the example project](https://github.com/ruby/ruby.wasm/tree/main/packages/n
62
62
const main = async () => {
63
63
// Fetch and instntiate WebAssembly binary
64
64
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"
68
68
" https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.wasm"
69
69
);
70
70
const buffer = await response .arrayBuffer ();
@@ -85,7 +85,6 @@ See [the example project](https://github.com/ruby/ruby.wasm/tree/main/packages/n
85
85
</html >
86
86
```
87
87
88
-
89
88
## GC limitation with JavaScript interoperability
90
89
91
90
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
107
106
end
108
107
end
109
108
RNode.new
110
- ` )
109
+ ` );
111
110
112
- rnode .call (" set_jnode" , vm .wrap (jnode))
111
+ rnode .call (" set_jnode" , vm .wrap (jnode));
113
112
jnode .setRNode (rnode);
114
113
```
115
114
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` -->
117
116
118
117
## APIs
119
118
120
119
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
121
120
122
121
#### Table of Contents
123
122
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 )
154
150
155
151
### RubyVM
156
152
@@ -170,6 +166,7 @@ vm.addToImports(imports);
170
166
const instance = await WebAssembly .instantiate (rubyModule, imports);
171
167
await vm .setInstance (instance);
172
168
wasi .initialize (instance);
169
+ vm .initialize ();
173
170
```
174
171
175
172
#### initialize
@@ -224,6 +221,44 @@ console.log(result.toString()); // 3
224
221
225
222
Returns ** any** the result of the last expression
226
223
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
+
227
262
### RbValue
228
263
229
264
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
232
267
233
268
- ` inner `
234
269
- ` vm `
235
- - ` exporter `
270
+ - ` privateObject `
236
271
237
272
#### call
238
273
0 commit comments