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);
}
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);
}
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);
}
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());
}
}
}
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());
}
Aggregations