truffleruby icon indicating copy to clipboard operation
truffleruby copied to clipboard

[InteropUX] Ruby String vs. Array of String

Open fniephaus opened this issue 5 years ago • 0 comments

I'm experimenting with some polyglot data analyses and for that, I'm move different arrays between Truffle languages. When I import a Ruby string in Python and JavaScript, it behaves just like any other string of the corresponding language. The behavior changes, when I do the same with arrays of strings. Then, strings appear to be instances of Ruby String, which they are. I'm not yet sure what the correct behavior should be, I'm only noticing and reporting this inconsistency. Maybe @chumer can comment on this? I'm guessing this has something to do with the fact that Ruby strings are mutable, while Python and JavaScript strings aren't. In that case, I'd say this is a TruffleRuby interop bug as Ruby strings are not interpreted as immutable strings of other languages when they are read from an array.

Here's a polyglot shell session describing the problem:

GraalVM MultiLanguage Shell 1.0.0-rc15
Copyright (c) 2013-2019, Oracle and/or its affiliates
  JavaScript version 1.0
  Python version 3.7.0
  Ruby version 2.6.2
ruby> Polyglot.export("rubyString", "foo bar")
"foo bar"
ruby> Polyglot.export("rubyArrayOfStrings", ["foo bar", "buzz"])
["foo bar", "buzz"]
ruby> python>
python> import polyglot
python> 'foo' in polyglot.import_value('rubyString')
True
python> polyglot.import_value('rubyArrayOfStrings')
[foo bar, buzz]
python> polyglot.import_value('rubyArrayOfStrings')[0]
foo bar
python> 'foo' in polyglot.import_value('rubyArrayOfStrings')[0]
False
python> js>
js> Polyglot.import('rubyString')
foo bar
js> Polyglot.import('rubyString').indexOf('bar')
4
js> Polyglot.import('rubyArrayOfStrings')[0]
foo bar
js> Polyglot.import('rubyArrayOfStrings')[0].indexOf('bar')
undefined method `indexOf' for "foo bar":String
Did you mean?  index (NoMethodError)
js> python>
python> polyglot.export_value(["foo bar", "spam"], "pythonArrayOfStrings")
['foo bar', 'spam']
python> ruby>
ruby> Polyglot.import("pythonArrayOfStrings")
<foreign>
ruby> Polyglot.import("pythonArrayOfStrings")[0]
"foo bar"
ruby> Polyglot.import("pythonArrayOfStrings")[0].index("bar")
4

fniephaus avatar Apr 14 '19 17:04 fniephaus