Skip to content

Commit

Permalink
Merge pull request #30 from ruby/katei/js-null
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Jul 10, 2022
2 parents e5bcf7c + 3294e8a commit 706ca01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ static VALUE _rb_js_eval_js(VALUE _, VALUE code_str) {
return jsvalue_s_new(rb_js_abi_host_eval_js(&abi_str));
}

static VALUE _rb_js_eval_js_cstr(const char *code_str) {
rb_js_abi_host_string_t abi_str;
rb_js_abi_host_string_set(&abi_str, code_str);
return jsvalue_s_new(rb_js_abi_host_eval_js(&abi_str));
}

static VALUE _rb_js_is_js(VALUE _, VALUE obj) {
if (!IS_JSVALUE(obj)) {
return Qfalse;
Expand Down Expand Up @@ -370,6 +376,8 @@ void Init_js() {
rb_define_module_function(rb_mJS, "try_convert", _rb_js_try_convert, 1);
rb_define_module_function(rb_mJS, "eval", _rb_js_eval_js, 1);
rb_define_module_function(rb_mJS, "global", _rb_js_global_this, 0);
rb_define_const(rb_mJS, "NULL", _rb_js_eval_js_cstr("return null"));
rb_define_const(rb_mJS, "UNDEFINED", _rb_js_eval_js_cstr("return undefined"));

i_to_js = rb_intern("to_js");
rb_cJS_Object = rb_define_class_under(rb_mJS, "Object", rb_cObject);
Expand Down
11 changes: 7 additions & 4 deletions packages/npm-packages/ruby-wasm-wasi/test/js_from_rb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,21 @@ describe("Manipulation of JS from Ruby", () => {

test("Guard null", async () => {
const vm = await initRubyVM();
const result = vm.eval(`
const results = vm.eval(`
require "js"
intrinsics = JS.eval(<<-JS)
return {
returnNull(v) { return null },
returnUndef(v) { return undefined },
}
JS
js_null = JS.eval("return null")
o1 = intrinsics.call(:returnNull)
o1 == js_null
o2 = intrinsics.call(:returnUndef)
[o1 == JS::NULL, o2 == JS::UNDEFINED]
`);
expect(result.toString()).toEqual("true");
const e1 = results.call("at", vm.eval("0"));
const e2 = results.call("at", vm.eval("1"));
expect(e1.toString()).toEqual("true");
expect(e2.toString()).toEqual("true");
});
});

0 comments on commit 706ca01

Please sign in to comment.