Search in sources :

Example 16 with Value

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

the class ContextPreInitializationTest method testSystemPropertiesOptionsFailedPatch.

@Test
public void testSystemPropertiesOptionsFailedPatch() throws Exception {
    System.setProperty(SYS_OPTION1_KEY, "true");
    setPatchable();
    doContextPreinitialize(FIRST);
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    final CountingContext firstLangCtx = findContext(FIRST, contexts);
    assertNotNull(firstLangCtx);
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
    firstLangCtx.optionValues.clear();
    System.getProperties().remove(SYS_OPTION1_KEY);
    System.setProperty(SYS_OPTION2_KEY, "true");
    final Context ctx = Context.create();
    Value res = ctx.eval(Source.create(FIRST, "test"));
    assertEquals("test", res.asString());
    contexts = new ArrayList<>(emittedContexts);
    contexts.remove(firstLangCtx);
    final CountingContext firstLangCtx2 = findContext(FIRST, contexts);
    assertNotNull(firstLangCtx2);
    assertFalse(firstLangCtx2.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertTrue(firstLangCtx2.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 17 with Value

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

the class ContextPreInitializationTest method testSystemPropertiesOptionsSuccessfulPatch.

@Test
public void testSystemPropertiesOptionsSuccessfulPatch() throws Exception {
    System.setProperty(SYS_OPTION1_KEY, "true");
    setPatchable(FIRST);
    doContextPreinitialize(FIRST);
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    final CountingContext firstLangCtx = findContext(FIRST, contexts);
    assertNotNull(firstLangCtx);
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
    firstLangCtx.optionValues.clear();
    System.getProperties().remove(SYS_OPTION1_KEY);
    System.setProperty(SYS_OPTION2_KEY, "true");
    Context ctx = Context.create();
    Value res = ctx.eval(Source.create(FIRST, "test"));
    assertEquals("test", res.asString());
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertTrue(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
    ctx.close();
    ctx = Context.create();
    res = ctx.eval(Source.create(FIRST, "test"));
    assertEquals("test", res.asString());
    assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
    assertTrue(firstLangCtx.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 18 with Value

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

the class ContextPreInitializationTest method testOutputNoLanguagePreInitialization.

@Test
public void testOutputNoLanguagePreInitialization() throws Exception {
    setPatchable();
    final String stdOutContent = "output";
    final String stdErrContent = "error";
    BaseLanguage.parseStdOutOutput.put(FIRST, stdOutContent);
    BaseLanguage.parseStdErrOutput.put(FIRST, stdErrContent);
    doContextPreinitialize();
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(0, contexts.size());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    try (Context ctx = Context.newBuilder().out(out).err(err).build()) {
        final Value res = ctx.eval(Source.create(FIRST, "test"));
        assertEquals("test", res.asString());
        assertEquals(stdOutContent, new String(out.toByteArray(), "UTF-8"));
        assertEquals(stdErrContent, new String(err.toByteArray(), "UTF-8"));
    }
}
Also used : Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 19 with Value

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

the class ContextPreInitializationTest method testOutputSingleLanguagePreInitialization.

@Test
public void testOutputSingleLanguagePreInitialization() throws Exception {
    setPatchable(FIRST);
    final String firstStdOutContent = "first-output";
    final String firstStdErrContent = "first-error";
    final String secondStdOutContent = "second-output";
    final String secondStdErrContent = "second-error";
    BaseLanguage.parseStdOutOutput.put(FIRST, firstStdOutContent);
    BaseLanguage.parseStdErrOutput.put(FIRST, firstStdErrContent);
    BaseLanguage.parseStdOutOutput.put(SECOND, secondStdOutContent);
    BaseLanguage.parseStdErrOutput.put(SECOND, secondStdErrContent);
    doContextPreinitialize(FIRST);
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(1, contexts.size());
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream err = new ByteArrayOutputStream();
    try (Context ctx = Context.newBuilder().out(out).err(err).build()) {
        Value res = ctx.eval(Source.create(FIRST, "test"));
        assertEquals("test", res.asString());
        contexts = new ArrayList<>(emittedContexts);
        assertEquals(1, contexts.size());
        assertEquals(firstStdOutContent, new String(out.toByteArray(), "UTF-8"));
        assertEquals(firstStdErrContent, new String(err.toByteArray(), "UTF-8"));
        out.reset();
        err.reset();
        res = ctx.eval(Source.create(SECOND, "test"));
        assertEquals("test", res.asString());
        contexts = new ArrayList<>(emittedContexts);
        assertEquals(2, contexts.size());
        assertEquals(secondStdOutContent, new String(out.toByteArray(), "UTF-8"));
        assertEquals(secondStdErrContent, new String(err.toByteArray(), "UTF-8"));
    }
}
Also used : Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 20 with Value

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

the class ContextPreInitializationTest method testArgumentsSingleLanguPreInitialization.

@Test
public void testArgumentsSingleLanguPreInitialization() throws Exception {
    setPatchable(FIRST);
    doContextPreinitialize(FIRST);
    List<CountingContext> contexts = new ArrayList<>(emittedContexts);
    assertEquals(1, contexts.size());
    try (Context ctx = Context.newBuilder().arguments(FIRST, new String[] { "a", "b" }).arguments(SECOND, new String[] { "c", "d" }).build()) {
        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);
        assertEquals(Arrays.asList("a", "b"), context.arguments);
        res = ctx.eval(Source.create(SECOND, "test"));
        assertEquals("test", res.asString());
        contexts = new ArrayList<>(emittedContexts);
        assertEquals(2, contexts.size());
        context = findContext(SECOND, contexts);
        assertNotNull(context);
        assertEquals(Arrays.asList("c", "d"), context.arguments);
    }
}
Also used : Context(org.graalvm.polyglot.Context) ArrayList(java.util.ArrayList) 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