ruby/ruby.wasm

Error on <non-js-value> == <js-value>

krmbn0576 opened this issue · 2 comments

Code:

require "js"

p 24 == JS.eval("return 24;")

Result:

<anonymous>': eval:3:in `==': wrong argument type Integer (expected jsvalue) (TypeError)
eval:3:in `=='
eval:3:in `<main>'

Hmmm the equal operator causing errors is so inconvenient and unpredictable.
Can you return the value or at least improve it to a clear error message?

Largo commented

Oh, I did some work overwriting the equal operator for JS:Object.
Maybe we can extend this. My code is still work in progress, but I decided to open a pull request draft #396.

I overwrote the == operator so JS.eval("return true;") == true works

  # Support self == true instead of self == JS:True
  alias_method :orig_eq, :==
  def ==(other)
    if other.equal? true
      return orig_eq(JS::True)
    elsif other.equal? false
      return orig_eq(JS::False)
    elsif other.equal? nil
      return orig_eq(JS::Null) || orig_eq(JS::Undefined)
    end
    
    orig_eq(other)
  end

Oh thank you.
I wish you would support Integer 😇