Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
krmbn0576 opened this issue Feb 29, 2024 · 2 comments · Fixed by #413
Closed

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

krmbn0576 opened this issue Feb 29, 2024 · 2 comments · Fixed by #413

Comments

@krmbn0576
Copy link
Contributor

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
Copy link

Largo commented Feb 29, 2024

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

@krmbn0576
Copy link
Contributor Author

Oh thank you.
I wish you would support Integer 😇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants