Search in sources :

Example 21 with Env

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

the class VirtualizedFileSystemTest method testToUri.

@Test
public void testToUri() {
    final Context ctx = cfg.getContext();
    final Path userDir = cfg.getUserDir();
    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 URI uri = file.toUri();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), allowsUserDir);
            Assert.assertEquals(cfg.formatErrorMessage("URI"), userDir.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toUri(), uri);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), allowsUserDir);
        }
    };
    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) URI(java.net.URI) Test(org.junit.Test)

Example 22 with Env

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

the class VirtualizedFileSystemTest method testList.

@Test
public void testList() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final Path folderExisting = path.resolve(FOLDER_EXISTING);
        final TruffleFile file = cfg.needsURI() ? env.getTruffleFile(folderExisting.toUri()) : env.getTruffleFile(folderExisting.toString());
        try {
            final String expected = path.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toString();
            final Collection<? extends TruffleFile> children = file.list();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            final Optional<String> expectedFile = children.stream().map(TruffleFile::getAbsoluteFile).map(TruffleFile::getPath).filter(expected::equals).findAny();
            Assert.assertTrue(cfg.formatErrorMessage("Expected child"), expectedFile.isPresent());
        } 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 23 with Env

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

the class VirtualizedFileSystemTest method isReadable.

@Test
public void isReadable() {
    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 readable = file.isReadable();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertTrue(cfg.formatErrorMessage("Is readable"), readable);
        } 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)

Example 24 with Env

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

the class PolyglotLanguageContext method patch.

boolean patch(Map<String, String> newOptions, String[] newApplicationArguments) {
    final boolean preInitialized = isInitialized();
    if (preInitialized) {
        // Reset options from image generation time
        optionValues = null;
    }
    if (newOptions != null) {
        getOptionValues().putAll(newOptions);
    }
    setApplicationArguments(newApplicationArguments);
    if (preInitialized) {
        try {
            final Env newEnv = LANGUAGE.patchEnvContext(env, context.out, context.err, context.in, config, getOptionValues(), newApplicationArguments, context.fileSystem);
            if (newEnv != null) {
                env = newEnv;
                return true;
            }
            return false;
        } catch (Throwable t) {
            if (t instanceof ThreadDeath) {
                throw t;
            }
            throw PolyglotImpl.wrapGuestException(this, t);
        }
    } else {
        return true;
    }
}
Also used : Env(com.oracle.truffle.api.TruffleLanguage.Env)

Example 25 with Env

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

the class PolyglotLanguageContext method ensureCreated.

void ensureCreated(PolyglotLanguage accessingLanguage) {
    language.ensureInitialized();
    if (creating) {
        throw new PolyglotIllegalStateException(String.format("Cyclic access to language context for language %s. " + "The context is currently being created.", language.getId()));
    }
    boolean created = false;
    if (env == null) {
        synchronized (context) {
            if (env == null) {
                checkAccess(accessingLanguage);
                boolean singleThreaded = context.isSingleThreaded();
                Thread firstFailingThread = null;
                for (PolyglotThreadInfo threadInfo : context.getSeenThreads().values()) {
                    if (!LANGUAGE.isThreadAccessAllowed(language.info, threadInfo.thread, singleThreaded)) {
                        firstFailingThread = threadInfo.thread;
                        break;
                    }
                }
                if (firstFailingThread != null) {
                    throw PolyglotContextImpl.throwDeniedThreadAccess(firstFailingThread, singleThreaded, Arrays.asList(language));
                }
                creating = true;
                try {
                    initializeCaches();
                    try {
                        Env createdEnv = env = LANGUAGE.createEnv(this, language.info, context.out, context.err, context.in, config, getOptionValues(), applicationArguments, context.fileSystem);
                        LANGUAGE.createEnvContext(createdEnv);
                        language.requireProfile().notifyContextCreate(createdEnv);
                    } finally {
                        creating = false;
                    }
                    created = true;
                } catch (Throwable e) {
                    // language not successfully created, reset to avoid inconsistent
                    // language contexts
                    env = null;
                    throw e;
                }
            }
        }
    }
    if (created && eventsEnabled) {
        VMAccessor.INSTRUMENT.notifyLanguageContextCreated(context.engine, context.truffleContext, language.info);
    }
}
Also used : Env(com.oracle.truffle.api.TruffleLanguage.Env)

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