Skip to content

Commit 71c25d8

Browse files
Merge pull request #526 from ruby/katei/is_a-behavior
2 parents 763d5e9 + 2949d20 commit 71c25d8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/ruby_wasm/build/product/crossruby.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ def build(executor, remake: false, reconfigure: false)
222222
end
223223
install_dir = File.join(build_dir, "install")
224224
if !File.exist?(install_dir) || remake || reconfigure
225+
unless target.pic?
226+
# HACK: force static linking for non-pic target
227+
executor.rm_f File.join(build_dir, "ruby")
228+
end
225229
executor.system "make",
226230
"-j#{executor.process_count}",
227231
"install",

packages/gems/js/ext/js/js-core.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,25 @@ VALUE _rb_js_try_convert(VALUE klass, VALUE obj) {
146146
* p JS.is_a?(JS.global, JS.global[:Object]) #=> true
147147
* p JS.is_a?(JS.global, Object) #=> false
148148
*/
149-
static VALUE _rb_js_is_kind_of(VALUE klass, VALUE obj, VALUE c) {
149+
static VALUE _rb_js_is_kind_of(int argc, VALUE *argv, VALUE self) {
150+
151+
if (argc == 1) {
152+
// If the second argument is not given, behaves as `Module#is_a?`.
153+
return rb_obj_is_kind_of(self, argv[0]);
154+
}
155+
156+
if (argc != 2) {
157+
rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 2)",
158+
argc);
159+
}
160+
161+
VALUE obj = argv[0];
162+
VALUE klass = argv[1];
150163
if (!IS_JSVALUE(obj)) {
151164
return Qfalse;
152165
}
153166
struct jsvalue *val = DATA_PTR(obj);
154-
VALUE js_klass_v = _rb_js_try_convert(klass, c);
167+
VALUE js_klass_v = _rb_js_try_convert(self, klass);
155168
struct jsvalue *js_klass = DATA_PTR(js_klass_v);
156169
return RBOOL(rb_js_abi_host_instance_of(val->abi, js_klass->abi));
157170
}
@@ -561,7 +574,7 @@ static VALUE _rb_js_proc_to_js(VALUE obj) {
561574
*/
562575
void Init_js() {
563576
rb_mJS = rb_define_module("JS");
564-
rb_define_module_function(rb_mJS, "is_a?", _rb_js_is_kind_of, 2);
577+
rb_define_module_function(rb_mJS, "is_a?", _rb_js_is_kind_of, -1);
565578
rb_define_module_function(rb_mJS, "try_convert", _rb_js_try_convert, 1);
566579
rb_define_module_function(rb_mJS, "eval", _rb_js_eval_js, 1);
567580
rb_define_module_function(rb_mJS, "global", _rb_js_global_this, 0);

packages/npm-packages/ruby-wasm-wasi/test/unit/test_js.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ def test_is_a?
1010
assert_false JS.is_a?(JS.global, JS.global)
1111
# globalThis is an instance of Object
1212
assert_true JS.is_a?(JS.global, JS.global[:Object])
13+
14+
# If the second argument is missing, behaves as Module#is_a?
15+
assert_true JS.is_a?(Module)
16+
assert_false JS.is_a?(Class)
1317
end
1418

1519
def test_eval

0 commit comments

Comments
 (0)