From b868bce10833447989eb2b59b122a071970a8c8b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Jul 2023 07:09:51 +0000 Subject: [PATCH 1/3] Adopt standard .rb directory structure To avoid conflicts with other gems installed in the same Ruby package --- ext/js/lib/js.rb | 6 +++--- ext/js/lib/{ => js}/array.rb | 0 ext/js/lib/{ => js}/hash.rb | 0 ext/js/lib/{ => js}/nil_class.rb | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename ext/js/lib/{ => js}/array.rb (100%) rename ext/js/lib/{ => js}/hash.rb (100%) rename ext/js/lib/{ => js}/nil_class.rb (100%) diff --git a/ext/js/lib/js.rb b/ext/js/lib/js.rb index a8fc29f72..d8e1fab1c 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. # 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 From 4187e4feafe05758939c1be4fa87a288728f4521 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Jul 2023 07:32:24 +0000 Subject: [PATCH 2/3] Add boolean conversion to ? suffixed predicate method --- ext/js/lib/js.rb | 18 +++++++++++++--- .../ruby-wasm-wasi/test/unit/test_object.rb | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/ext/js/lib/js.rb b/ext/js/lib/js.rb index d8e1fab1c..140445475 100644 --- a/ext/js/lib/js.rb +++ b/ext/js/lib/js.rb @@ -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 @@ -133,7 +138,12 @@ def new(*args) end 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 @@ -142,6 +152,8 @@ def method_missing(sym, *args, &block) 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/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; } }; From c1e2afd328a6efde3efa21b88338f25cfc6513c0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Jul 2023 07:46:41 +0000 Subject: [PATCH 3/3] Add doc comment for JS::Object#method_missing --- ext/js/lib/js.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ext/js/lib/js.rb b/ext/js/lib/js.rb index 140445475..92393496a 100644 --- a/ext/js/lib/js.rb +++ b/ext/js/lib/js.rb @@ -137,6 +137,21 @@ 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) sym_str = sym.to_s if sym_str.end_with?("?") @@ -150,6 +165,9 @@ def method_missing(sym, *args, &block) 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