Skip to content

Commit

Permalink
Merge pull request #526 from ruby/katei/is_a-behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun authored Sep 6, 2024
2 parents 763d5e9 + 2949d20 commit 71c25d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/ruby_wasm/build/product/crossruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ def build(executor, remake: false, reconfigure: false)
end
install_dir = File.join(build_dir, "install")
if !File.exist?(install_dir) || remake || reconfigure
unless target.pic?
# HACK: force static linking for non-pic target
executor.rm_f File.join(build_dir, "ruby")
end
executor.system "make",
"-j#{executor.process_count}",
"install",
Expand Down
19 changes: 16 additions & 3 deletions packages/gems/js/ext/js/js-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,25 @@ VALUE _rb_js_try_convert(VALUE klass, VALUE obj) {
* p JS.is_a?(JS.global, JS.global[:Object]) #=> true
* p JS.is_a?(JS.global, Object) #=> false
*/
static VALUE _rb_js_is_kind_of(VALUE klass, VALUE obj, VALUE c) {
static VALUE _rb_js_is_kind_of(int argc, VALUE *argv, VALUE self) {

if (argc == 1) {
// If the second argument is not given, behaves as `Module#is_a?`.
return rb_obj_is_kind_of(self, argv[0]);
}

if (argc != 2) {
rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2)",
argc);
}

VALUE obj = argv[0];
VALUE klass = argv[1];
if (!IS_JSVALUE(obj)) {
return Qfalse;
}
struct jsvalue *val = DATA_PTR(obj);
VALUE js_klass_v = _rb_js_try_convert(klass, c);
VALUE js_klass_v = _rb_js_try_convert(self, klass);
struct jsvalue *js_klass = DATA_PTR(js_klass_v);
return RBOOL(rb_js_abi_host_instance_of(val->abi, js_klass->abi));
}
Expand Down Expand Up @@ -561,7 +574,7 @@ static VALUE _rb_js_proc_to_js(VALUE obj) {
*/
void Init_js() {
rb_mJS = rb_define_module("JS");
rb_define_module_function(rb_mJS, "is_a?", _rb_js_is_kind_of, 2);
rb_define_module_function(rb_mJS, "is_a?", _rb_js_is_kind_of, -1);
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);
Expand Down
4 changes: 4 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def test_is_a?
assert_false JS.is_a?(JS.global, JS.global)
# globalThis is an instance of Object
assert_true JS.is_a?(JS.global, JS.global[:Object])

# If the second argument is missing, behaves as Module#is_a?
assert_true JS.is_a?(Module)
assert_false JS.is_a?(Class)
end

def test_eval
Expand Down

0 comments on commit 71c25d8

Please sign in to comment.