Skip to content

Make JS.is_a? behave as Module#is_a? when the second argument is missing #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading