You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, when doing interop with other languages, the callback provided must consume exactly three arguments:
$ 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
js> [1, 2, 3].forEach(Polyglot.eval("python", "lambda currentValue,index,array: print('%s-%s-%s' % (currentValue, index, array))"))
1-0-[1, 2, 3]
2-1-[1, 2, 3]
3-2-[1, 2, 3]
js> [1, 2, 3].forEach(Polyglot.eval("python", "lambda currentValue: print(currentValue)"))
TypeError: <lambda>() takes 1 positional argument but 3 were given
This is not expected from the user's point of view.
I believe the reason for this is that there's no way to determine the arity of an executable interop value prior invocation, so Graal.js always tries to execute the interop callback with three arguments. IIRC all callbacks are always invoked with three arguments. But in case of an anonymous JS function, unused arguments will just be ignored. A naive solution would be to retry invoking an interop executable with fewer arguments until success. If the invocation with no arguments fails, so must the interop call. That, however, may not be the best strategy to provide a more consistent user experience. Maybe the interop protocol needs some sort of arity mechanism, but that may need too support many kinds of arguments (positional, optional, keyword, splat, ...).
callbackfn should be a function that accepts three arguments.
and we call it like that, with three arguments all the time. If an argument is not available, it will be undefined. And this is fine in JS: Excess arguments will land in the arguments array, or your named argument will be undefined, when you provided too many/too few arguments respectively. So while it should be a three argument function, JS does not care too much in practice and you can call a zero-argument function still (if you can live with the consequences, e.g. reading the arguments out of the arguments array). Python does not seem to like that, <lambda>() takes 1 positional argument but 3 were given seems it did not accept our two extra (most likely undefined) arguments.
I am not sure there is a clean way to deal with that.
We could detect this is a foreign lambda and then - in violation of the ECMAScript spec! - call it with fewer arguments. But with how many? Python might expect another number than Ruby. You brought up that question already above.
When a function is called in JavaScript, there can be more arguments than needed. For example,
Array.prototype.forEach()
can be used like this:However, when doing interop with other languages, the callback provided must consume exactly three arguments:
This is not expected from the user's point of view.
I believe the reason for this is that there's no way to determine the arity of an executable interop value prior invocation, so Graal.js always tries to execute the interop callback with three arguments. IIRC all callbacks are always invoked with three arguments. But in case of an anonymous JS function, unused arguments will just be ignored. A naive solution would be to retry invoking an interop executable with fewer arguments until success. If the invocation with no arguments fails, so must the interop call. That, however, may not be the best strategy to provide a more consistent user experience. Maybe the interop protocol needs some sort of arity mechanism, but that may need too support many kinds of arguments (positional, optional, keyword, splat, ...).
/cc @chumer and @LeonBein
The text was updated successfully, but these errors were encountered: