From 2949d2070237e5c958460e02f0166111b0db424d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 6 Sep 2024 14:12:43 +0000 Subject: [PATCH] Make JS.is_a? behave as Module#is_a? when the second argument is missing --- lib/ruby_wasm/build/product/crossruby.rb | 4 ++++ packages/gems/js/ext/js/js-core.c | 19 ++++++++++++++++--- .../ruby-wasm-wasi/test/unit/test_js.rb | 4 ++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/ruby_wasm/build/product/crossruby.rb b/lib/ruby_wasm/build/product/crossruby.rb index 8444599a12..73ec915507 100644 --- a/lib/ruby_wasm/build/product/crossruby.rb +++ b/lib/ruby_wasm/build/product/crossruby.rb @@ -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", diff --git a/packages/gems/js/ext/js/js-core.c b/packages/gems/js/ext/js/js-core.c index 3dbccd9536..d42b47758d 100644 --- a/packages/gems/js/ext/js/js-core.c +++ b/packages/gems/js/ext/js/js-core.c @@ -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)); } @@ -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); diff --git a/packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb b/packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb index 2b98924b59..84019434fa 100644 --- a/packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb +++ b/packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb @@ -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