File tree Expand file tree Collapse file tree 3 files changed +27
-1
lines changed
packages/npm-packages/ruby-wasm-wasi/test Expand file tree Collapse file tree 3 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,9 @@ extern VALUE rb_cString;
16
16
extern VALUE rb_cTrueClass ;
17
17
extern VALUE rb_cFalseClass ;
18
18
19
+ // from js/js-core.c
20
+ void rb_abi_lend_object (VALUE obj );
21
+
19
22
static VALUE rb_mJS ;
20
23
static VALUE rb_cJS_Object ;
21
24
@@ -312,6 +315,7 @@ static VALUE _rb_js_import_from_js(VALUE obj) {
312
315
* Returns +obj+ wrapped by JS class RbValue.
313
316
*/
314
317
static VALUE _rb_js_obj_wrap (VALUE obj , VALUE wrapping ) {
318
+ rb_abi_lend_object (wrapping );
315
319
return jsvalue_s_new (rb_js_abi_host_rb_object_to_js_rb_value ((uint32_t )wrapping ));
316
320
}
317
321
Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ static VALUE rb_abi_lend_object_internal(VALUE obj) {
90
90
}
91
91
return Qundef ;
92
92
}
93
- static void rb_abi_lend_object (VALUE obj ) {
93
+ void rb_abi_lend_object (VALUE obj ) {
94
94
RB_WASM_DEBUG_LOG ("rb_abi_lend_object: obj = %p\n" , (void * )obj );
95
95
int state ;
96
96
RB_WASM_LIB_RT (rb_protect (rb_abi_lend_object_internal , obj , & state ));
Original file line number Diff line number Diff line change
1
+ import { RbValue } from "../dist/index.umd" ;
1
2
import { initRubyVM } from "./init" ;
2
3
3
4
describe ( "Manipulation of JS from Ruby" , ( ) => {
@@ -208,6 +209,27 @@ describe("Manipulation of JS from Ruby", () => {
208
209
expect ( o1 . toString ( ) ) . toEqual ( o1Clone . toString ( ) ) ;
209
210
} ) ;
210
211
212
+ test ( "Wrapped Ruby object should live until wrapper will be released" , async ( ) => {
213
+ const vm = await initRubyVM ( ) ;
214
+ const run = vm . eval ( `
215
+ require "js"
216
+ proc do |imports|
217
+ imports.call(:mark_js_object_live, JS::Object.wrap(Object.new))
218
+ end
219
+ ` ) ;
220
+ const livingObjects = new Set < RbValue > ( ) ;
221
+ run . call ( "call" , vm . wrap ( {
222
+ mark_js_object_live : ( object : RbValue ) => {
223
+ livingObjects . add ( object ) ;
224
+ }
225
+ } ) ) ;
226
+ vm . eval ( "GC.start" ) ;
227
+ for ( const object of livingObjects ) {
228
+ // Ensure that all objects are still alive
229
+ object . call ( "itself" )
230
+ }
231
+ } )
232
+
211
233
test ( "Guard null" , async ( ) => {
212
234
const vm = await initRubyVM ( ) ;
213
235
const result = vm . eval ( `
You can’t perform that action at this time.
0 commit comments