Skip to content

Commit 1d77b26

Browse files
committed
Unify the behavior of JavaScript properties when called as methods.
Specifically, the behavior when a property is called with `?` to be consistent with the behavior when called without `?`. NoMethodError will be raised.
1 parent bff915f commit 1d77b26

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

packages/gems/js/lib/js.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ def method_missing(sym, *args, &block)
178178
result = self.call(sym, *args, &block)
179179
# Type coerce the result to boolean type
180180
# to match the true/false determination in JavaScript's if statement.
181-
JS.global.Boolean(result) == JS::True
182-
else
183-
false
181+
return JS.global.Boolean(result) == JS::True
184182
end
185-
elsif self[sym].typeof == "function"
186-
self.call(sym, *args, &block)
187-
else
188-
super
189183
end
184+
185+
if self[sym].typeof == "function"
186+
return self.call(sym, *args, &block)
187+
end
188+
189+
super
190190
end
191191

192192
# Check if a JavaScript method exists

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,22 +310,14 @@ def test_method_missing_with_block
310310
assert_true block_called
311311
end
312312

313-
def test_method_missing_with_undefined_method
314-
object = JS.eval(<<~JS)
315-
return { foo() { return true; } };
316-
JS
317-
assert_raise(NoMethodError) { object.bar }
318-
end
319-
320313
def test_method_missing_with_?
321314
object = JS.eval(<<~JS)
322315
return {
323316
return_true() { return true; },
324317
return_false() { return false; },
325318
return_object() { return {}; },
326319
return_null() { return null; },
327-
return_empty_string() { return ''; },
328-
true_property: true
320+
return_empty_string() { return ''; }
329321
};
330322
JS
331323

@@ -343,12 +335,22 @@ def test_method_missing_with_?
343335
# Return Ruby false when the return value is JavaScript false
344336
assert_false object.return_null?
345337
assert_false object.return_empty_string?
338+
end
346339

347-
# Return false when the property is not a function
348-
assert_false object.true_property?
340+
def test_method_missing_with_property
341+
object = JS.eval(<<~JS)
342+
return { property: 42 };
343+
JS
344+
assert_raise(NoMethodError) { object.property }
345+
assert_raise(NoMethodError) { object.property? }
346+
end
349347

350-
# Return false when function is not defined
351-
assert_false object.undefined?
348+
def test_method_missing_with_undefined_method
349+
object = JS.eval(<<~JS)
350+
return { foo() { return true; } };
351+
JS
352+
assert_raise(NoMethodError) { object.bar }
353+
assert_raise(NoMethodError) { object.bar? }
352354
end
353355

354356
def test_respond_to_missing?

0 commit comments

Comments
 (0)