Search in sources :

Example 46 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class LanguageSPITest method testLookupHost.

@Test
public void testLookupHost() {
    Context context = Context.newBuilder().allowHostAccess(true).build();
    Value value = eval(context, new Function<Env, Object>() {

        public Object apply(Env t) {
            return t.lookupHostSymbol("java.util.HashMap");
        }
    });
    assertTrue(value.isHostObject());
    Object map = value.asHostObject();
    assertSame(map, HashMap.class);
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 47 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testCreateDirectoryTest.

@Test
public void testCreateDirectoryTest() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    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_NEW_CREATE_DIR);
            toCreate.createDirectories();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
            if (canRead) {
                Assert.assertTrue(cfg.formatErrorMessage("Expected dir exists"), toCreate.exists());
            }
        } 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 48 with Env

use of com.oracle.truffle.api.TruffleLanguage.Env in project graal by oracle.

the class VirtualizedFileSystemTest method testGetAbsoluteFile.

@Test
public void testGetAbsoluteFile() {
    final Context ctx = cfg.getContext();
    final boolean allowsUserDir = cfg.allowsUserDir();
    if (cfg.needsURI()) {
        // Nothing to test for URI path
        return;
    }
    languageAction = (Env env) -> {
        final TruffleFile file = env.getTruffleFile(FOLDER_EXISTING).resolve(FILE_EXISTING);
        try {
            final TruffleFile absolute = file.getAbsoluteFile();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), allowsUserDir);
            Assert.assertEquals(absolute.getPath(), cfg.getUserDir().resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toString());
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), allowsUserDir);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Aggregations

Env (com.oracle.truffle.api.TruffleLanguage.Env)48 Context (org.graalvm.polyglot.Context)41 Test (org.junit.Test)41 TruffleContext (com.oracle.truffle.api.TruffleContext)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 Path (java.nio.file.Path)19 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)16 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 IOException (java.io.IOException)12 Function (java.util.function.Function)10 Engine (org.graalvm.polyglot.Engine)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 PolyglotException (org.graalvm.polyglot.PolyglotException)7 Value (org.graalvm.polyglot.Value)6 LanguageContext (com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExecutionException (java.util.concurrent.ExecutionException)4 ExecutorService (java.util.concurrent.ExecutorService)4