diff --git a/ext/js/lib/js.rb b/ext/js/lib/js.rb index a8fc29f72..92393496a 100644 --- a/ext/js/lib/js.rb +++ b/ext/js/lib/js.rb @@ -1,7 +1,7 @@ require "js.so" -require_relative "./hash.rb" -require_relative "./array.rb" -require_relative "./nil_class.rb" +require_relative "js/hash.rb" +require_relative "js/array.rb" +require_relative "js/nil_class.rb" # The JS module provides a way to interact with JavaScript from Ruby. # @@ -12,7 +12,10 @@ # JS.global[:document].write("Hello, world!") # div = JS.global[:document].createElement("div") # div[:innerText] = "click me" -# JS.global[:document][:body].appendChild(div) +# body = JS.global[:document][:body] +# if body[:classList].contains?("main") +# body.appendChild(div) +# end # div.addEventListener("click") do |event| # puts event # => # [object MouseEvent] # puts event[:detail] # => 1 @@ -30,7 +33,9 @@ # JS.global[:document].call(:write, "Hello, world!") # div = JS.global[:document].call(:createElement, "div") # div[:innerText] = "click me" -# JS.global[:document][:body].call(:appendChild, div) +# if body[:classList].call(:contains, "main") == JS::True +# body.appendChild(div) +# end # div.call(:addEventListener, "click") do |event| # puts event # => # [object MouseEvent] # puts event[:detail] # => 1 @@ -132,16 +137,41 @@ def new(*args) JS.global[:Reflect].construct(self, args.to_js) end + # Provide a shorthand form for JS::Object#call + # + # This method basically calls the JavaScript method with the same + # name as the Ruby method name as is using JS::Object#call. + # + # Exceptions are the following cases: + # * If the method name ends with a question mark (?), the question mark is removed + # and the method is called as a predicate method. The return value is converted to + # a Ruby boolean value automatically. + # + # This shorthand is unavailable for the following cases and you need to use + # JS::Object#call instead: + # * If the method name is invalid as a Ruby method name (e.g. contains a hyphen, reserved word, etc.) + # * If the method name is already defined as a Ruby method under JS::Object + # * If the JavaScript method name ends with a question mark (?) def method_missing(sym, *args, &block) - if self[sym].typeof == "function" + sym_str = sym.to_s + if sym_str.end_with?("?") + # When a JS method is called with a ? suffix, it is treated as a predicate method, + # and the return value is converted to a Ruby boolean value automatically. + self.call(sym_str[0..-2].to_sym, *args, &block) == JS::True + elsif self[sym].typeof == "function" self.call(sym, *args, &block) else super end end + # Check if a JavaScript method exists + # + # See JS::Object#method_missing for details. def respond_to_missing?(sym, include_private) return true if super + sym_str = sym.to_s + sym = sym_str[0..-2].to_sym if sym_str.end_with?("?") self[sym].typeof == "function" end diff --git a/ext/js/lib/array.rb b/ext/js/lib/js/array.rb similarity index 100% rename from ext/js/lib/array.rb rename to ext/js/lib/js/array.rb diff --git a/ext/js/lib/hash.rb b/ext/js/lib/js/hash.rb similarity index 100% rename from ext/js/lib/hash.rb rename to ext/js/lib/js/hash.rb diff --git a/ext/js/lib/nil_class.rb b/ext/js/lib/js/nil_class.rb similarity index 100% rename from ext/js/lib/nil_class.rb rename to ext/js/lib/js/nil_class.rb 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 c2587136c..b25734bd3 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 @@ -277,6 +277,27 @@ def test_method_missing_with_undefined_method assert_raise(NoMethodError) { object.bar } end + def test_method_missing_with_? + object = JS.eval(<<~JS) + return { + return_true() { return true; }, + return_false() { return false; }, + return_object() { return {}; } + }; + JS + + # Normally return JS::Object when without ? + assert_true object.return_true == JS::True + assert_true object.return_false == JS::False + + # Return Ruby boolean value when with ? + assert_true object.return_true? + assert_false object.return_false? + + # Return Ruby false when the return value is not JS::True + assert_false object.return_object? + end + def test_respond_to_missing? object = JS.eval(<<~JS) return { foo() { return true; } };