diff --git a/packages/gems/js/ext/js/js-core.c b/packages/gems/js/ext/js/js-core.c index c28804f2e4..f81ecf1173 100644 --- a/packages/gems/js/ext/js/js-core.c +++ b/packages/gems/js/ext/js/js-core.c @@ -200,6 +200,11 @@ static VALUE _rb_js_obj_aref(VALUE obj, VALUE key) { static VALUE _rb_js_obj_aset(VALUE obj, VALUE key, VALUE val) { struct jsvalue *p = check_jsvalue(obj); VALUE rv = _rb_js_try_convert(rb_mJS, val); + if (rv == Qnil) { + rb_raise(rb_eTypeError, + "wrong argument type %s (expected JS::Object like object)", + rb_class2name(rb_obj_class(val))); + } struct jsvalue *v = check_jsvalue(rv); rb_js_abi_host_string_t key_abi_str; key = rb_obj_as_string(key); diff --git a/packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb b/packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb index b4a53929f7..5fb77c8e21 100644 --- a/packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb +++ b/packages/npm-packages/ruby-wasm-wasi/test/unit/test_object.rb @@ -346,6 +346,12 @@ def test_member_set assert_equal 41.to_s, object["bar"].to_s end + def test_member_set_with_non_js_object + assert_raise_message("wrong argument type Object (expected JS::Object like object)") do + JS.global[:tmp] = Object.new + end + end + def test_member_set_with_stress_gc GC.stress = true JS.global[:tmp] = "1"