Skip to content

Commit 3294e8a

Browse files
Add JS::NULL and JS::UNDEFINED constants
1 parent e5bcf7c commit 3294e8a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

ext/js/js-core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ static VALUE _rb_js_eval_js(VALUE _, VALUE code_str) {
8787
return jsvalue_s_new(rb_js_abi_host_eval_js(&abi_str));
8888
}
8989

90+
static VALUE _rb_js_eval_js_cstr(const char *code_str) {
91+
rb_js_abi_host_string_t abi_str;
92+
rb_js_abi_host_string_set(&abi_str, code_str);
93+
return jsvalue_s_new(rb_js_abi_host_eval_js(&abi_str));
94+
}
95+
9096
static VALUE _rb_js_is_js(VALUE _, VALUE obj) {
9197
if (!IS_JSVALUE(obj)) {
9298
return Qfalse;
@@ -370,6 +376,8 @@ void Init_js() {
370376
rb_define_module_function(rb_mJS, "try_convert", _rb_js_try_convert, 1);
371377
rb_define_module_function(rb_mJS, "eval", _rb_js_eval_js, 1);
372378
rb_define_module_function(rb_mJS, "global", _rb_js_global_this, 0);
379+
rb_define_const(rb_mJS, "NULL", _rb_js_eval_js_cstr("return null"));
380+
rb_define_const(rb_mJS, "UNDEFINED", _rb_js_eval_js_cstr("return undefined"));
373381

374382
i_to_js = rb_intern("to_js");
375383
rb_cJS_Object = rb_define_class_under(rb_mJS, "Object", rb_cObject);

packages/npm-packages/ruby-wasm-wasi/test/js_from_rb.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,21 @@ describe("Manipulation of JS from Ruby", () => {
210210

211211
test("Guard null", async () => {
212212
const vm = await initRubyVM();
213-
const result = vm.eval(`
213+
const results = vm.eval(`
214214
require "js"
215215
intrinsics = JS.eval(<<-JS)
216216
return {
217217
returnNull(v) { return null },
218218
returnUndef(v) { return undefined },
219219
}
220220
JS
221-
js_null = JS.eval("return null")
222221
o1 = intrinsics.call(:returnNull)
223-
o1 == js_null
222+
o2 = intrinsics.call(:returnUndef)
223+
[o1 == JS::NULL, o2 == JS::UNDEFINED]
224224
`);
225-
expect(result.toString()).toEqual("true");
225+
const e1 = results.call("at", vm.eval("0"));
226+
const e2 = results.call("at", vm.eval("1"));
227+
expect(e1.toString()).toEqual("true");
228+
expect(e2.toString()).toEqual("true");
226229
});
227230
});

0 commit comments

Comments
 (0)