Search in sources :

Example 51 with Context

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

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

the class EngineAPITest method testCreateContextWithAutomaticEngine.

@Test
public void testCreateContextWithAutomaticEngine() {
    Context context = Context.create();
    try {
        Context.newBuilder().engine(context.getEngine()).build();
        fail();
    } catch (IllegalArgumentException e) {
    }
}
Also used : Context(org.graalvm.polyglot.Context) Test(org.junit.Test)

Example 53 with Context

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

the class VirtualizedFileSystemTest method testReadUsingChannel.

@Test
public void testReadUsingChannel() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
            final String content = new String(file.readAllBytes(), StandardCharsets.UTF_8);
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertEquals(cfg.formatErrorMessage("Expected file content"), FILE_EXISTING_CONTENT, content);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        } catch (IOException ioe) {
            throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 54 with Context

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

the class VirtualizedFileSystemTest method testDelete.

@Test
public void testDelete() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canWrite = cfg.canWrite();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final TruffleFile toCreate = root.resolve(FILE_EXISTING_DELETE);
            toCreate.delete();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canWrite);
        } catch (IOException ioe) {
            throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 55 with Context

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

the class VirtualizedFileSystemTest method isWritable.

@Test
public void isWritable() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
        try {
            final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
            final boolean writable = file.isWritable();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertTrue(cfg.formatErrorMessage("Is writable"), writable);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) 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