Search in sources :

Example 66 with Context

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

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

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

the class PolyglotLauncher method runScripts.

private void runScripts(List<Script> scripts, Context.Builder contextBuilder, String[] programArgs) {
    Script mainScript = scripts.get(scripts.size() - 1);
    try (Context context = contextBuilder.arguments(mainScript.getLanguage(), programArgs).build()) {
        Engine engine = context.getEngine();
        checkLanguage(mainLanguage, engine);
        for (Script script : scripts) {
            checkLanguage(script.languageId, engine);
        }
        for (Script script : scripts) {
            try {
                Value result = context.eval(script.getSource());
                if (script.isPrintResult()) {
                    System.out.println(result);
                }
            } catch (PolyglotException e) {
                if (e.isExit()) {
                    throw exit(e.getExitStatus());
                } else if (e.isGuestException()) {
                    e.printStackTrace();
                    throw exit(1);
                } else {
                    throw abort(e);
                }
            } catch (IOException e) {
                throw abort(e);
            } catch (Throwable t) {
                throw abort(t);
            }
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) IOException(java.io.IOException) PolyglotException(org.graalvm.polyglot.PolyglotException) Engine(org.graalvm.polyglot.Engine)

Example 69 with Context

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

the class PolyglotNativeAPI method polyglot_export_symbol.

@CEntryPoint(name = "polyglot_export_symbol")
public static PolyglotStatus polyglot_export_symbol(IsolateThread isolate_thread, PolyglotContextPointer context, CCharPointer symbol_name, PolyglotValuePointer value) {
    return withHandledErrors(() -> {
        Context jContext = ObjectHandles.getGlobal().get(context);
        String symbolName = CTypeConversion.toJavaString(symbol_name);
        jContext.getPolyglotBindings().putMember(symbolName, fetchHandle(value));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 70 with Context

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

the class PolyglotNativeAPI method polyglot_create_int64.

@SuppressWarnings("UnnecessaryBoxing")
@CEntryPoint(name = "polyglot_create_int64")
public static PolyglotStatus polyglot_create_int64(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, long value, PolyglotValuePointerPointer result) {
    return withHandledErrors(() -> {
        Context ctx = ObjectHandles.getGlobal().get(polyglot_context);
        result.write(createHandle(ctx.asValue(Long.valueOf(value))));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

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