Search in sources :

Example 46 with Context

use of org.graalvm.polyglot.Context 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 47 with Context

use of org.graalvm.polyglot.Context 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 48 with Context

use of org.graalvm.polyglot.Context 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 49 with Context

use of org.graalvm.polyglot.Context 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)

Example 50 with Context

use of org.graalvm.polyglot.Context 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)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12