Comments (2)
from graal.
I will probably use this workaround (targetTypeMapping
), question is: is it valid, that Graal does not do this out-of-the-box?
public static class TakesInt {
@Export
public void method(int arg) {};
}
@Test
public void testTakesIntWithCoercion() {
// https://github.com/oracle/graal/issues/9096
AtomicInteger predicateSucceededCount = new AtomicInteger();
AtomicInteger predicateFailedCount = new AtomicInteger();
AtomicInteger conversionCount = new AtomicInteger();
HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL)
// Rhino would do the same!?
.targetTypeMapping(String.class, Integer.class, target -> {
boolean result;
try {
double d = Double.valueOf(target);
if (Math.floor(d) != Math.ceil(d)) {
result = false;
} else if (d > Integer.MAX_VALUE) {
result = false;
} else if (d < Integer.MIN_VALUE) {
result = false;
} else {
result = true;
}
} catch (NumberFormatException e) {
result = false;
}
if (result) {
predicateSucceededCount.incrementAndGet();
} else {
predicateFailedCount.incrementAndGet();
}
return result;
}, target -> {
conversionCount.incrementAndGet();
return Double.valueOf(target).intValue();
})
//
.build();
Context context = Context.newBuilder("js")
.allowHostAccess(hostAccess)
// required to declare class with Java.type
.allowHostClassLookup(s -> true).build();
Value bindings = context.getBindings("js");
bindings.putMember("takesInt", new TakesInt());
context.eval("js", "const Integer = Java.type('" + Integer.class.getName() + "');");
// these must not throw
context.eval("js", "takesInt.method('0');");
context.eval("js", "takesInt.method('' + Integer.MAX_VALUE);");
context.eval("js", "takesInt.method('' + Integer.MIN_VALUE);");
assertEquals(3, predicateSucceededCount.get());
assertEquals(0, predicateFailedCount.get());
assertEquals(3, conversionCount.get());
List.of(
// too small
"takesInt.method('' + (Integer.MIN_VALUE - 1));",
// too large
"takesInt.method('' + (Integer.MAX_VALUE + 1));",
// not integer
"takesInt.method('1.5');")
//
.forEach((script) -> {
try {
context.eval("js", script);
fail("for script [" + script + "]: expected excpetion to be thrown");
} catch (Exception e) {
assertEquals("for script [" + script + "]: exception class mismatch", PolyglotException.class, e.getClass());
assertTrue("for script [" + script + "]: unexpected message: [" + e.getMessage() + "]", StringUtils.contains(e.getMessage(), "Invalid or lossy primitive coercion"));
}
});
assertEquals(3, predicateSucceededCount.get());
assertEquals(3, predicateFailedCount.get());
assertEquals(3, conversionCount.get());
}
from graal.
Related Issues (20)
- Static native executables using G1 GC crashes when attempting to run it (Segmentation fault) HOT 2
- ScriptEngine does not support `require()`
- [GR-57127] Native Image generated by `native-image` tool is not working with ZoomBA project HOT 4
- [GR-56749] Missing Documentation Video/Tutorial HOT 2
- Failure to build libgraal on OpenJDK change
- [GR-56882] PLDI 2017, June 18-23, Barcelona, Spain presentation and video unavailable. HOT 4
- Cannot convert "1682293037722628098'(1anguage: Java, type: com.oracle.truffle.api.strings.Trufflestring) to Java type "java.lang .0bject[]':Unsupported target type HOT 3
- Got ClassNotFoundException while building native image with grpc-netty-shaded HOT 2
- native-image-configure tool is throwing a ClassCastException on latest Oracle EA Build HOT 6
- Getting Error: Missing CAP cache value for: NativeCodeInfo:RISCV64LibCHelperDirectives:StructInfo:CPUFeatures HOT 3
- Exception in thread "main" java.lang.IllegalArgumentException: invalid type parameter: void HOT 1
- [GR-57724] Using GraalVM with JBOSS/Wildfly throws warning: does not support runtime compilation HOT 11
- GraalVM Fatal error reported via JNI: Could not allocate library name HOT 2
- [GR-57252] Improve error message when trying to build static image on unsupported platforms
- NPE in com.oracle.truffle.runtime.hotspot.HotSpotTruffleRuntimeAccess.createRuntime(): "jdk.internal.module.Modules.JLA" is null HOT 1
- [Feature Request] Turn off truffle compilation dynamically
- [GR-57284] `JAVA_TOOL_OPTIONS` environment variable in native executables HOT 1
- JavaFX native image doesnt work on different cpu (both windows and 64 bit) HOT 1
- Provide graaljs that's useable across JDK versions 11 through 22 by backporting #7510 HOT 1
- Unable to use GDB to debug the actual value of variables in GraalVM Native Image HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graal.