1

We're migrating legacy code from Nashorn to Graal.js, and facing issues with how numeric values are mapped internally. In Nashorn, large numbers like 15-digit integers were mapped to java.lang.Double. Now, with Graal.js, those are being interpreted as SafeInteger, which implements TruffleObject.

This breaks the following assertion in our polyglot interop logic:

assert !(result instanceof TruffleObject); What’s confusing is that this assertion fails during TestNG tests, but passes when run as a standalone Java application — the returned value is a SafeInteger in standalone and gets null when in test environment.

Here’s a simplified test case:

@Test
public static void test() throws ScriptException {
    Scope scope = ScriptManager.createScope();
    
    String test = "314159265358979";

    scope.setValue(new Variable("maxPrecisionJavascript", DataType.NUMBER), test);
    Expression expr1 = ScriptManager.createExpression("result = maxPrecisionJavascript;");
    expr1.eval(scope);
    String actual = scope.getValueAsString("result");
    System.out.println("Result: " + actual);

}

This test results in a java.lang.AssertionError in HostToGuestRootNode.java when run in the test environment at scope.getValueAsString("result")

Here’s the full stack trace:

org.graalvm.polyglot.PolyglotException: java.lang.AssertionError
    at com.oracle.truffle.polyglot.HostToGuestRootNode.execute(HostToGuestRootNode.java:125)
    ...
    at com.tibco.bpm.se.core.test.GraalTest.test(GraalTest.java:56)
Caused by: java.lang.AssertionError
    at com.oracle.truffle.polyglot.HostToGuestRootNode.execute(HostToGuestRootNode.java:125)

Question:

Why does SafeInteger (a TruffleObject) cause the !(result instanceof TruffleObject) assertion to fail in the test environment but not in the standalone app — even though the object appears to be the same? Are there any known differences in Graal.js behavior or polyglot engine setup between test (e.g. TestNG) and runtime environments? and How to solve for Large Number.

New contributor
Srikanth Allapu is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
6
  • Are you running your application with -enableassertions (short form: -ea)?
    – Sören
    Commented yesterday
  • This question is similar to: assert not working the way i thought in java. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – Sören
    Commented yesterday
  • Good point @Sören TestNG is running with -ea enabled, but our standalone app isn’t. That explains why the assertion triggers in tests but not in the standalone app. Another interesting case: if I run mvn test for the whole suite, the test passes — but if I run it individually, it fails. Any idea why that might be? Commented yesterday
  • Just guessing: You have disabled assertions for tests in your pom.xml.
    – Sören
    Commented yesterday
  • 1
    If you are also interested in the root of the AssertionError, it would be nice to provide a test-case that has minimal/clear dependencies. I have no idea where the classes ScriptManager, Expression or Variable come from. Also your "full stack trace" is not full at all. It contains just two stack-frames.
    – Jan Štola
    Commented 22 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.