Search in sources :

Example 21 with Value

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

the class ContextPreInitializationTest method testContextOptionsNoLanguagePreInitialization.

@Test
public void testContextOptionsNoLanguagePreInitialization() throws Exception {
    setPatchable();
    doContextPreinitialize();
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(0, contexts.size());
    final Context ctx = Context.newBuilder().option(FIRST + ".Option1", "true").build();
    final Value res = ctx.eval(Source.create(FIRST, "test"));
    assertEquals("test", res.asString());
    contexts = new ArrayList<>(emittedContexts);
    assertEquals(1, contexts.size());
    CountingContext context = findContext(FIRST, contexts);
    assertNotNull(context);
    assertTrue(context.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertFalse(context.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
    ctx.close();
}
Also used : Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 22 with Value

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

the class ContextPreInitializationTest method testMoreLanguagesPreInitialization.

@Test
public void testMoreLanguagesPreInitialization() throws Exception {
    setPatchable(FIRST, SECOND);
    doContextPreinitialize(FIRST, SECOND);
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(2, contexts.size());
    final CountingContext firstLangCtx = findContext(FIRST, contexts);
    assertNotNull(firstLangCtx);
    final CountingContext secondLangCtx = findContext(SECOND, contexts);
    assertNotNull(secondLangCtx);
    assertEquals(1, firstLangCtx.createContextCount);
    assertEquals(1, firstLangCtx.initializeContextCount);
    assertEquals(0, firstLangCtx.patchContextCount);
    assertEquals(0, firstLangCtx.disposeContextCount);
    assertEquals(0, firstLangCtx.initializeThreadCount);
    assertEquals(0, firstLangCtx.disposeThreadCount);
    assertEquals(1, secondLangCtx.createContextCount);
    assertEquals(1, secondLangCtx.initializeContextCount);
    assertEquals(0, secondLangCtx.patchContextCount);
    assertEquals(0, secondLangCtx.disposeContextCount);
    assertEquals(0, secondLangCtx.initializeThreadCount);
    assertEquals(0, secondLangCtx.disposeThreadCount);
    final Context ctx = Context.create();
    Value res = ctx.eval(Source.create(FIRST, "test"));
    assertEquals("test", res.asString());
    contexts = new ArrayList<>(emittedContexts);
    assertEquals(2, contexts.size());
    assertEquals(1, firstLangCtx.createContextCount);
    assertEquals(1, firstLangCtx.initializeContextCount);
    assertEquals(1, firstLangCtx.patchContextCount);
    assertEquals(0, firstLangCtx.disposeContextCount);
    assertEquals(1, firstLangCtx.initializeThreadCount);
    assertEquals(0, firstLangCtx.disposeThreadCount);
    assertEquals(1, secondLangCtx.createContextCount);
    assertEquals(1, secondLangCtx.initializeContextCount);
    assertEquals(1, secondLangCtx.patchContextCount);
    assertEquals(0, secondLangCtx.disposeContextCount);
    assertEquals(1, secondLangCtx.initializeThreadCount);
    assertEquals(0, secondLangCtx.disposeThreadCount);
    res = ctx.eval(Source.create(SECOND, "test"));
    assertEquals("test", res.asString());
    contexts = new ArrayList<>(emittedContexts);
    assertEquals(2, contexts.size());
    assertEquals(1, firstLangCtx.createContextCount);
    assertEquals(1, firstLangCtx.initializeContextCount);
    assertEquals(1, firstLangCtx.patchContextCount);
    assertEquals(0, firstLangCtx.disposeContextCount);
    assertEquals(1, firstLangCtx.initializeThreadCount);
    assertEquals(0, firstLangCtx.disposeThreadCount);
    assertEquals(1, secondLangCtx.createContextCount);
    assertEquals(1, secondLangCtx.initializeContextCount);
    assertEquals(1, secondLangCtx.patchContextCount);
    assertEquals(0, secondLangCtx.disposeContextCount);
    assertEquals(1, secondLangCtx.initializeThreadCount);
    assertEquals(0, secondLangCtx.disposeThreadCount);
    ctx.close();
    contexts = new ArrayList<>(emittedContexts);
    assertEquals(2, contexts.size());
    assertEquals(1, firstLangCtx.createContextCount);
    assertEquals(1, firstLangCtx.initializeContextCount);
    assertEquals(1, firstLangCtx.patchContextCount);
    assertEquals(1, firstLangCtx.disposeContextCount);
    assertEquals(1, firstLangCtx.initializeThreadCount);
    assertEquals(1, firstLangCtx.disposeThreadCount);
    assertEquals(1, secondLangCtx.createContextCount);
    assertEquals(1, secondLangCtx.initializeContextCount);
    assertEquals(1, secondLangCtx.patchContextCount);
    assertEquals(1, secondLangCtx.disposeContextCount);
    assertEquals(1, secondLangCtx.initializeThreadCount);
    assertEquals(1, secondLangCtx.disposeThreadCount);
}
Also used : Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 23 with Value

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

the class ConstraintInteropTypesTest method allowStringReturn.

public void allowStringReturn() {
    BrokenTruffleObject obj = new BrokenTruffleObject("30");
    polyglot.putMember("value", obj);
    Value result = polyglot.getMember("value").execute();
    Assert.assertEquals("30", result);
}
Also used : Value(org.graalvm.polyglot.Value)

Example 24 with Value

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

the class ConstraintInteropTypesTest method forbidNullReturn.

@Test(expected = PolyglotException.class)
public void forbidNullReturn() {
    BrokenTruffleObject obj = new BrokenTruffleObject(null);
    polyglot.putMember("value", obj);
    Value result = polyglot.getMember("value").execute();
    fail("No result, an exception should be thrown: " + result);
}
Also used : Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 25 with Value

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

the class ConstraintInteropTypesTest method forbidBigIntegerReturn.

@Test(expected = PolyglotException.class)
public void forbidBigIntegerReturn() {
    BrokenTruffleObject obj = new BrokenTruffleObject(new BigInteger("30"));
    polyglot.putMember("value", obj);
    Value result = polyglot.getMember("value").execute();
    fail("No result, an exception should be thrown: " + result);
}
Also used : Value(org.graalvm.polyglot.Value) BigInteger(java.math.BigInteger) 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