oracle / graalpython

GraalPy – A high-performance embeddable Python 3 runtime for Java
https://www.graalvm.org/python/
Other
1.24k stars 108 forks source link

[interop] `in` operator does not work for interop string #223

Closed fniephaus closed 3 years ago

fniephaus commented 3 years ago

Title says it all :)

Repro

$ polyglot --jvm --shell
GraalVM MultiLanguage Shell 21.1.0
Copyright (c) 2013-2020, Oracle and/or its affiliates
  JavaScript version 21.1.0
  Python version 3.8.5
  Ruby version 2.7.2
  Squeak/Smalltalk version 21.2.0-dev
python> import polyglot
python> 'foo' in polyglot.eval(language='ruby', string='"foobar"')
False
python> polyglot.eval(language='ruby', string='"foobar"')
'foobar'
python> 'foo' in str(polyglot.eval(language='ruby', string='"foobar"'))
True
lukasstadler commented 3 years ago

This should definitely work, but it's an interesting case. It's a bit more tricky than it looks at first, since "in" works for both string search and array element search. @eregon - out of curiosity, what does a Ruby string look like at the interop level? I assume it's both an array and a string?

fniephaus commented 3 years ago

Technically, languages could of course expose strings as interop strings and with interop array elements. But I think what the in operator should do is simple:

If its a string (or interop string), do a string search. Otherwise, if its an array (or interop array), do an array element search.

eregon commented 3 years ago

Ruby Strings implement isString/asString, but not hasArrayElements. It's the same for the default exports of java.lang.String.