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
Using latest GraalVM.js Engine, we are facing some issues on methods that accepts Object as parameters and validates the received class. Our biggest problem involves Kafka Connect Struct which on the method put validates the number type its been used as parameter.
The main issue with GraalVM.js is that even using numeric wrapper class, all numbers are converted based on its value. This mean that if I use Long.valueOf("5"), this will be converted in a integer value. If I use Long.valueOf("2147483648") it will be converted in a long value.
Is there a way to keep the numeric wrapper class?
This is the code I used for testing.
packagedev.vepo.graalvmjs.numbers;
importjava.security.InvalidParameterException;
importjavax.script.ScriptEngine;
importjavax.script.ScriptException;
importcom.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
importorg.graalvm.polyglot.Context;
importorg.graalvm.polyglot.HostAccess;
publicclassNumberConversionError {
publicstaticvoidexpectLong(ObjectshouldBeLong) {
System.out.println(shouldBeLong.getClass());
if (!(shouldBeLonginstanceofLong)) {
thrownewInvalidParameterException("This is not a long value!");
}
}
publicstaticvoidmain(String[] args) throwsScriptException {
ScriptEngineengine = GraalJSScriptEngine.create(null, Context.newBuilder("js")
.allowHostAccess(HostAccess.ALL)
.allowHostClassLookup(s -> true)
.allowExperimentalOptions(true)
.out(System.out)
.err(System.err)
.option("js.nashorn-compat", "true"));
engine.eval(""" var NumberConversionError = Java.type("dev.vepo.graalvmjs.numbers.NumberConversionError"); var Long = Java.type("java.lang.Long"); print(NumberConversionError); NumberConversionError.expectLong(Long.valueOf("2147483648")); NumberConversionError.expectLong(Long.valueOf("5")); """);
}
}
Not when you are invoking a Java method that takes Object. When you need a particular primitive type, I suggest you to invoke a (helper) Java method that takes a parameter of the desired type. I understand that this can be clumsy but it is the safe approach.
Some background: JavaScript has just one number type (ignoring bigint). This type corresponds to Java double. That's why graal-js works with the numbers as if they were doubles. It is using various types (like int) internally but that's an implementation detail. The good news is that the internals of graal-js support long in a limited way. So, it was not that difficult to ensure that long is not converted to a different numeric type eagerly (as it was done in your test-case). In other words, your long-based test-case should work with the mentioned change.
I understand that it would be nice to be able to pass other numeric types (byte, short, float) in the same way. Unfortunately, that would require larger and more risky changes within graal-js. So, we are not pursuing this direction (for now). We may revisit this decision if we see some other compelling use-cases.
Using latest GraalVM.js Engine, we are facing some issues on methods that accepts Object as parameters and validates the received class. Our biggest problem involves Kafka Connect Struct which on the method put validates the number type its been used as parameter.
The main issue with GraalVM.js is that even using numeric wrapper class, all numbers are converted based on its value. This mean that if I use
Long.valueOf("5")
, this will be converted in a integer value. If I useLong.valueOf("2147483648")
it will be converted in a long value.Is there a way to keep the numeric wrapper class?
This is the code I used for testing.
The sample code can be found here: https://github.com/vepo/graalvm-js-number-conversion
The text was updated successfully, but these errors were encountered: