oracle / truffleruby

A high performance implementation of the Ruby programming language, built on GraalVM.
https://www.graalvm.org/ruby/
Other
2.98k stars 180 forks source link

[interop] Cannot use foreign objects as keys in Hash due to #hash NameError #2138

Open fniephaus opened 3 years ago

fniephaus commented 3 years ago

Looks like foreign objects do not expose #hash? I think it should be provided through InteropLibrary's identityHashCode.

Repro Session

$ polyglot --jvm --shell
GraalVM MultiLanguage Shell 21.0.0-dev
Copyright (c) 2013-2020, Oracle and/or its affiliates
  JavaScript version 21.0.0-dev
  Python version 3.8.5
  R version 3.6.1
  Ruby version 2.6.6
ruby> x = {}
{}
ruby> x[Object.new] = true
true
ruby> x.keys
[#<Object:0x250>]
ruby> x[Polyglot.eval('python', 'object()')] = true
Unknown identifier: hash (NameError)
        at <ruby> <top (required)>(resource:/truffleruby/core/truffle/boot.rb:16:15-59)
        at <ruby> null(Unknown)
chrisseaton commented 3 years ago

Ruby's #hash isn't an identity hash, though.

eregon commented 3 years ago

We need support for hashcodes in InteropLibrary for this to work correctly. Otherwise there is no way to know how to hash a foreign object (not just by identity). cc @chumer

When using a {}.compare_by_identity Hash, then we should use InteropLibrary#identityHashCode() (might not be the case yet).