Search in sources :

Example 96 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLTCKLanguageProvider method createExpressions.

@Override
public Collection<? extends Snippet> createExpressions(Context context) {
    final Collection<Snippet> res = new ArrayList<>();
    final Value fnc = eval(context, String.format(PATTERN_BIN_OP_FNC, "add", "+"), "add");
    Snippet.Builder opb = Snippet.newBuilder("+", fnc, TypeDescriptor.NUMBER).parameterTypes(TypeDescriptor.NUMBER, TypeDescriptor.NUMBER);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.STRING, TypeDescriptor.ANY);
    res.add(opb.build());
    opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, TypeDescriptor.STRING);
    res.add(opb.build());
    res.add(createBinaryOperator(context, "-", "sub", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "*", "mul", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "/", "div", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).resultVerifier((snippetRun) -> {
        final Value dividend = snippetRun.getParameters().get(0);
        final Value divider = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (dividend.isNumber() && divider.fitsInDouble() && divider.asDouble() == 0) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "==", "eq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "!=", "neq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
    res.add(createBinaryOperator(context, "<=", "le", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">=", "ge", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "<", "l", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, ">", "g", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
    res.add(createBinaryOperator(context, "||", "or", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && !firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createBinaryOperator(context, "&&", "land", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
        final Value firstParam = snippetRun.getParameters().get(0);
        final Value secondParam = snippetRun.getParameters().get(1);
        final PolyglotException exception = snippetRun.getException();
        if (firstParam.isBoolean() && firstParam.asBoolean() && !secondParam.isBoolean()) {
            Assert.assertNotNull(exception);
        } else if (exception != null) {
            throw exception;
        } else {
            Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
        }
    }).build());
    res.add(createPostfixOperator(context, "()", "callNoArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY)).build());
    res.add(createPostfixOperator(context, "(1)", "callOneArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER)).build());
    res.add(createPostfixOperator(context, "(1, \"\")", "callTwoArgs", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER, TypeDescriptor.STRING)).build());
    return Collections.unmodifiableCollection(res);
}
Also used : ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) Snippet(org.graalvm.polyglot.tck.Snippet) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 97 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLDebugDirectTest method stepInStepOver.

@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, "return 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, "println(res)", "res", "2");
    stepOut();
    Value value = context.getBindings("sl").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);
}
Also used : Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 98 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLDebugDirectTest method testNull.

@Test
public void testNull() throws Throwable {
    final Source nullTest = createNull();
    context.eval(nullTest);
    session.suspendNextExecution();
    assertLocation("nullTest", 2, true, "res = doNull()", "res", UNASSIGNED);
    stepInto(1);
    assertLocation("nullTest", 3, true, "return res", "res", "NULL");
    continueExecution();
    Value value = context.getBindings("sl").getMember("nullTest").execute();
    assertExecutedOK();
    String val = value.toString();
    assertNotNull(val);
    assertEquals("SL displays null as NULL", "NULL", val);
}
Also used : Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 99 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLExceptionTest method testProxyGuestLanguageStack.

@Test
public void testProxyGuestLanguageStack() {
    Value bar = ctx.eval("sl", "function foo(f) { f(); } function bar(f) { return foo(f); } function main() { return bar; }");
    TestProxy proxy = new TestProxy(3, bar);
    try {
        bar.execute(proxy);
        fail();
    } catch (PolyglotException e) {
        assertProxyException(proxy, e);
        for (PolyglotException seenException : proxy.seenExceptions) {
            // exceptions are unwrapped and wrapped again
            assertNotSame(e, seenException);
            assertSame(e.asHostException(), seenException.asHostException());
        }
    }
}
Also used : Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 100 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class SLParseInContextTest method parseAPlusB.

@Test
public void parseAPlusB() throws Exception {
    Value value = context.eval("x-test-eval", "");
    assertTrue("Result is a number: " + value, value.isNumber());
    assertEquals(42, value.asInt());
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9