Search in sources :

Example 76 with Value

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());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) SLBigNumber(com.oracle.truffle.sl.runtime.SLBigNumber) Value(org.graalvm.polyglot.Value) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 77 with Value

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);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 78 with Value

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);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 79 with Value

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());
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 80 with Value

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');
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) 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