use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInstrumentTest method testEarlyReturn.
/**
* Test that we can forcibly return early from call nodes with an arbitrary value.
*/
@Test
public void testEarlyReturn() throws Exception {
String code = "function main() {\n" + " a = 10;\n" + " b = a;\n" + " // Let fce() warm up and specialize:\n" + " while (a == b && a < 100000) {\n" + " a = fce(a);\n" + " b = b + 1;\n" + " }\n" + " c = a;\n" + " // Run fce() and alter it's return type in an instrument:\n" + " c = fce(c);\n" + " return c;\n" + "}\n" + "function fce(x) {\n" + " return x + 1;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
Engine engine = Engine.newBuilder().err(engineOut).build();
Context context = Context.newBuilder().engine(engine).build();
// No instrument:
Value ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = 200000L;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(200001L, ret.asLong());
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertFalse(ret.isNumber());
assertTrue(ret.isString());
assertEquals("Hello!1", ret.asString());
// Specialize to long again:
earlyReturn.fceCode = "<>";
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = new BigInteger("-42");
boolean interopFailure;
try {
context.eval(source);
interopFailure = false;
} catch (PolyglotException err) {
interopFailure = true;
}
assertTrue(interopFailure);
earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-41L, ret.asLong());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = Boolean.TRUE;
ret = context.eval(source);
assertTrue(ret.isBoolean());
assertEquals(Boolean.TRUE, ret.asBoolean());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = -42.42;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-42.42, ret.asDouble(), 1e-8);
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertTrue(ret.isString());
assertEquals("Hello!", ret.asString());
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInteropControlFlowTest method testIf.
@Test
public void testIf() throws IOException {
final Source src = Source.newBuilder("sl", "function testIf(a) {if(a) {return 1;} else {return 0;}} function main() {return testIf;}", "testIf.sl").build();
final Value fnc = context.eval(src);
fnc.execute(false);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInteropControlFlowTest method testWhile.
@Test
public void testWhile() throws IOException {
final Source src = Source.newBuilder("sl", "function testWhile(a) {while(a) {break;}} function main() {return testWhile;}", "testWhile.sl").build();
final Value fnc = context.eval(src);
Assert.assertTrue(fnc.canExecute());
fnc.execute(false);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInteropObjectTest method testObject.
@Test
public void testObject() throws IOException {
final Source src = Source.newBuilder("sl", "function main() {o = new(); o.a = 10; o.b = \"B\"; return o;}", "testObject.sl").build();
final Value obj = context.eval(src);
Assert.assertTrue(obj.hasMembers());
Value a = obj.getMember("a");
Assert.assertNotNull(a);
Assert.assertTrue(a.isNumber());
Assert.assertEquals(10, a.asInt());
Value b = obj.getMember("b");
Assert.assertNotNull(b);
Assert.assertTrue(b.isString());
Assert.assertEquals("B", b.asString());
obj.putMember("a", b);
a = obj.getMember("a");
Assert.assertTrue(a.isString());
Assert.assertEquals("B", a.asString());
obj.removeMember("a");
Assert.assertFalse(obj.hasMember("a"));
Assert.assertEquals("[b]", obj.getMemberKeys().toString());
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class SLInteropPrimitiveTest method testChar.
@Test
public void testChar() throws IOException {
final Source src = Source.newBuilder("sl", "function testChar(a,b) {return a == b;} function main() {return testChar;}", "testChar.sl").build();
final Value fnc = context.eval(src);
Assert.assertTrue(fnc.canExecute());
fnc.execute('a', 'b');
}
Aggregations