Java Code Examples for org.graalvm.polyglot.Value#asInt()

The following examples show how to use org.graalvm.polyglot.Value#asInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HashemDebugDirectTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void testBreakpoint() throws Throwable {
    final Source factorial = createFactorial();

    session.install(Breakpoint.newBuilder(getSourceImpl(factorial)).lineIs(8).build());
    context.eval(factorial);
    assertExecutedOK();

    assertLocation("fac", 8, true,
                    "bede 1", "n",
                    "1", "nMinusOne",
                    UNASSIGNED, "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    continueExecution();

    Value value = context.getBindings("hashemi").getMember("test").execute();
    assertExecutedOK();
    Assert.assertEquals("2\n", getOut());
    Assert.assertTrue(value.isNumber());
    int n = value.asInt();
    assertEquals("Factorial computed OK", 2, n);
}
 
Example 2
Source File: HashemDebugDirectTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void testDebuggerBreakpoint() throws Throwable {
    final Source factorial = createFactorialWithDebugger();

    context.eval(factorial);
    assertExecutedOK();

    assertLocation("fac", 12, true,
                    "debugger", "n",
                    "2", "nMinusOne",
                    "1", "nMOFact",
                    "1", "res", UNASSIGNED);
    continueExecution();

    Value value = context.getBindings("hashemi").getMember("test").execute();
    assertExecutedOK();
    Assert.assertEquals("2\n", getOut());
    Assert.assertTrue(value.isNumber());
    int n = value.asInt();
    assertEquals("Factorial computed OK", 2, n);
}
 
Example 3
Source File: HashemDebugDirectTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@Test
public void stepInStepOver() throws Throwable {
    final Source factorial = createFactorial();
    context.eval(factorial);

    session.suspendNextExecution();

    assertLocation("test", 2, true, "res = fac(2)", "res", UNASSIGNED);
    stepInto(1);
    assertLocation("fac", 7, true,
                    "n <= 1", "n",
                    "2", "nMinusOne",
                    UNASSIGNED, "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 10, true,
                    "nMinusOne = n - 1", "n",
                    "2", "nMinusOne",
                    UNASSIGNED, "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 11, true,
                    "nMOFact = fac(nMinusOne)", "n",
                    "2", "nMinusOne",
                    "1", "nMOFact",
                    UNASSIGNED, "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 12, true,
                    "res = n * nMOFact", "n", "2", "nMinusOne",
                    "1", "nMOFact",
                    "1", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("fac", 13, true,
                    "bede res", "n",
                    "2", "nMinusOne",
                    "1", "nMOFact",
                    "1", "res", "2");
    stepOver(1);
    assertLocation("test", 2, false, "fac(2)", "res", UNASSIGNED);
    stepOver(1);
    assertLocation("test", 3, true, "bechap(res)", "res", "2");
    stepOut();

    Value value = context.getBindings("hashemi").getMember("test");
    assertTrue(value.canExecute());
    Value resultValue = value.execute();
    String resultStr = resultValue.toString();
    Number result = resultValue.asInt();
    assertExecutedOK();

    assertNotNull(result);
    assertEquals("Factorial computed OK", 2, result.intValue());
    assertEquals("Factorial computed OK", "2", resultStr);
}
 
Example 4
Source File: GraalScriptRunner.java    From citeproc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively convert a JavaScript value to a Java object
 * @param v the value to convert
 * @return the object
 */
private static Object convert(Value v) {
    Object o = v;
    if (v.isNull()) {
        o = null;
    } else if (v.isBoolean()) {
        o = v.asBoolean();
    } else if (v.isDate()) {
        o = v.asDate();
    } else if (v.isDuration()) {
        o = v.asDuration();
    } else if (v.isHostObject()) {
        o = v.asHostObject();
    } else if (v.isInstant()) {
        o = v.asInstant();
    } else if (v.isNativePointer()) {
        o = v.asNativePointer();
    } else if (v.isNumber()) {
        if (v.fitsInInt()) {
            o = v.asInt();
        } else if (v.fitsInLong()) {
            o = v.asLong();
        } else if (v.fitsInDouble()) {
            o = v.asDouble();
        } else {
            throw new IllegalStateException("Unknown type of number");
        }
    } else if (v.isProxyObject()) {
        o = v.asProxyObject();
    } else if (v.isString()) {
        o = v.asString();
    } else if (v.isTime()) {
        o = v.asTime();
    } else if (v.isTimeZone()) {
        o = v.asTimeZone();
    } else if (v.hasArrayElements()) {
        o = convertArray(v);
    } else if (v.hasMembers()) {
        o = convertObject(v);
    }
    return o;
}