Skip to content

Commit f54bf38

Browse files
Accept JS::Object like object in JS::Object#==
Fixes #395
1 parent 84dfd76 commit f54bf38

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,14 @@ static VALUE _rb_js_obj_strictly_eql(VALUE obj, VALUE other) {
240240
* Performs "==" comparison, a.k.a the "Abstract Equality Comparison"
241241
* algorithm defined in the ECMAScript.
242242
* https://262.ecma-international.org/11.0/#sec-abstract-equality-comparison
243+
* If the given other object is not a JS::Object, try to convert it to a
244+
* JS::Object using JS.try_convert. If the conversion fails, returns false.
243245
*/
244246
static VALUE _rb_js_obj_eql(VALUE obj, VALUE other) {
247+
other = _rb_js_try_convert(rb_mJS, other);
248+
if (other == Qnil) {
249+
return Qfalse;
250+
}
245251
struct jsvalue *lhs = check_jsvalue(obj);
246252
struct jsvalue *rhs = check_jsvalue(other);
247253
bool result = rb_js_abi_host_js_value_equal(lhs->abi, rhs->abi);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ def test_eql?
3131

3232
assert_object_eql? false, JS.eval("return 24;"), JS.eval("return 42;")
3333
assert_object_eql? false, JS.eval("return NaN;"), JS.eval("return NaN;")
34+
35+
# Compare with JS::Object like object
36+
assert_equal true, JS.eval("return 42;") == 42
37+
assert_equal true, JS.eval("return 42;").eql?(42)
38+
assert_equal false, JS.eval("return 42;") != 42
39+
# Compare with non JS::Object like object
40+
assert_equal false, JS.eval("return 42;") != Object.new
3441
end
3542

3643
def assert_object_strictly_eql?(result, a, b)

0 commit comments

Comments
 (0)