Open
Description
When objects from Python (and possibly other languages) are printed (either directly in the console or via console.log()
), the other language's printing behavior is expected. However, Graal.js uses a custom format for printing foreign objects. The same is also true the other way around: JS objects are printed in Python without consulting toString()
. This is unexpected behavior from the user's point of view. Maybe the new interop messages (e.g. toDisplayString()
) can help to make this more consistent
Repro session
$ polyglot --jvm --shell
GraalVM MultiLanguage Shell 20.1.0
Copyright (c) 2013-2019, Oracle and/or its affiliates
JavaScript version 20.1.0
Python version 3.8.2
python> class A:
+ def __repr__(self):
+ return "Hello from Python"
+
python> A()
Hello from Python
python> print(A())
Hello from Python
python> str(A())
'Hello from Python'
python> js>
js> Polyglot.eval("python", "A()")
{} # `Hello from Python` expected
js> console.log(Polyglot.eval("python", "A()"))
{} # `Hello from Python` expected
js> class B { toString() { return "Hello from JS" } }
js> console.log(new B())
Hello from JS
js> python>
python> import polyglot
python> polyglot.eval(language="js", string="new B()")
<foreign '{}'> # `Hello from JS` expected
python> print(polyglot.eval(language="js", string="new B()"))
<foreign object at 0xeabecf5> # `Hello from JS` expected