Search in sources :

Example 41 with Context

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

the class ContextAPITest method testEnteredExecute.

@Test
public void testEnteredExecute() {
    Context context = Context.create(ContextAPITestLanguage.ID);
    // test outside
    testExecute(context);
    // test inside
    context.enter();
    testExecute(context);
    context.leave();
    // test enter twice
    context.enter();
    context.enter();
    testExecute(context);
    context.leave();
    testExecute(context);
    context.leave();
    testExecute(context);
    // test entered with inner context
    context.enter();
    Context context2 = Context.create(ContextAPITestLanguage.ID);
    testExecute(context2);
    context2.enter();
    testExecute(context2);
    context2.leave();
    context2.enter();
    context2.enter();
    testExecute(context2);
    context2.leave();
    testExecute(context2);
    context2.leave();
    context2.close();
    context.leave();
    // finally close the context
    context.close();
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Test(org.junit.Test)

Example 42 with Context

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

the class ContextAPITest method testImportExport.

@Test
public void testImportExport() {
    Context context = Context.create();
    Value polyglotBindings = context.getPolyglotBindings();
    polyglotBindings.putMember("string", "bar");
    polyglotBindings.putMember("null", null);
    polyglotBindings.putMember("int", 42);
    Object object = new Object();
    polyglotBindings.putMember("object", object);
    assertEquals("bar", polyglotBindings.getMember("string").asString());
    assertTrue(polyglotBindings.getMember("null").isNull());
    assertEquals(42, polyglotBindings.getMember("int").asInt());
    assertSame(object, polyglotBindings.getMember("object").asHostObject());
    assertNull(polyglotBindings.getMember("notexisting"));
    context.close();
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 43 with Context

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

the class ContextAPITest method testEnterLeave.

public void testEnterLeave() {
    Context context = Context.create();
    testEnterLeave(context, 0);
    context.close();
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context)

Example 44 with Context

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

the class ContextAPITest method testContextCreateAllLanguages.

@Test
public void testContextCreateAllLanguages() {
    Context context = Context.create();
    context.eval(ContextAPITestLanguage.ID, "");
    context.eval(LanguageSPITestLanguage.ID, "");
    assertInternalNotAccessible(context);
    context.close();
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Test(org.junit.Test)

Example 45 with Context

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

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