-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to identify arguments object as Value? #34
Comments
I don't think you can detect whether an array is in fact the arguments array, but you may detect that it is an array. This is how you can check for an array: public static void main(String[] args) {
try(Context ctx = Context.newBuilder().arguments("js", new String[] {"arg0", "arg1"}).build()) {
Value arguments = ctx.eval("js", "arguments");
System.out.println("getMetaObject().toString(): " + arguments.getMetaObject().toString());
System.out.println("hasArrayElements(): " + arguments.hasArrayElements());
System.out.println("string array(): " + Arrays.toString(arguments.as(String[].class)));
}
} This prints:
I'd recommend to use hasArrayElements as it is language agnostic. Does this help? (feel free to close if it does) |
What I'm after is the
It prints:
|
I tested another variant:
And it prints:
Hooray! :) Can I obtain that value in Java land without executing JS? |
@woess Why does the arguments object not return true for |
@chumer technically, an arguments object is not a JS array ( |
I'm afraid that's the only reliable way to identify if something is an arguments object currently. You can of course do something like this:
|
@woess That is what I'm doing now, and it works well. Feel free to close the issue unless you want to keep it open for the |
As of 8423934, |
I'm trying to migrate a big Nashorn application to GraalJS. One of the things the code does is convert a JavaScript value to a Scala/Java value. An
Arguments
object is detected and converted to an array in Nashorn like this:I haven't succeeded in testing if a
Value
isArguments
. My test method looks like this:And I'm calling it like this:
This is the printout:
Sadly, neither
type
norclassName
are useful here.Any suggestions?
The text was updated successfully, but these errors were encountered: