Search in sources :

Example 91 with Context

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

the class EvalLauncher method main.

public static void main(String[] args) {
    Map<String, String> options = new HashMap<>();
    List<String> scripts = new ArrayList<>();
    for (String option : args) {
        if (option.equals("--version")) {
            printVersions();
            return;
        } else if (option.equals("--help")) {
            printHelp(OptionCategory.USER);
            return;
        } else if (option.equals("--experthelp")) {
            printHelp(OptionCategory.EXPERT);
            return;
        } else if (option.equals("--debughelp")) {
            printHelp(OptionCategory.DEBUG);
            return;
        } else if (option.startsWith("--")) {
            int equalIndex = option.indexOf("=");
            int keyEndIndex;
            String value = "";
            if (equalIndex == -1) {
                keyEndIndex = option.length();
            } else {
                keyEndIndex = equalIndex;
                value = option.substring(equalIndex + 1, option.length());
            }
            String key = option.substring(2, keyEndIndex);
            options.put(key, value);
        } else {
            scripts.add(option);
        }
    }
    if (scripts.isEmpty()) {
        System.err.println("Error: No files to execute specified.\nUse --help for usage information.");
        return;
    }
    Context context = Context.newBuilder().options(options).build();
    for (String script : scripts) {
        int index = script.indexOf(':');
        if (index == -1) {
            System.err.println(String.format("Error: Invalid script %s provided.\nUse --help for usage information.", script));
            return;
        }
        String languageId = script.substring(0, index);
        if (context.getEngine().getLanguages().containsKey(languageId)) {
            System.err.println(String.format("Error: Invalid language %s provided.\nUse --help for usage information.", languageId));
            return;
        }
        String code = script.substring(index + 1, script.length());
        System.out.println("Script:       " + script + ": ");
        try {
            Value evalValue = context.eval(languageId, code);
            System.out.println("Result type:  " + evalValue.getMetaObject());
            System.out.println("Result value: " + evalValue);
        } catch (PolyglotException e) {
            System.err.println("Error:        " + e.getMessage());
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 92 with Context

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

the class CancelExecution method main.

public static void main(String[] args) throws InterruptedException {
    ExecutorService service = Executors.newFixedThreadPool(1);
    try {
        Context context = Context.create("js");
        // we submit a harmful infinite script to the executor
        Future<Value> future = service.submit(() -> context.eval("js", "while(true);"));
        // wait some time to let the execution start.
        Thread.sleep(1000);
        /*
             * closes the context and cancels the running execution. This can be done on any
             * parallel thread. Alternatively context.close(true) can be used to close all running
             * contexts of an engine.
             */
        context.close(true);
        try {
            future.get();
        } catch (ExecutionException e) {
            PolyglotException polyglotException = (PolyglotException) e.getCause();
            polyglotException.printStackTrace();
            /*
                 * After the execution got cancelled the executing thread stops by throwning a
                 * PolyglotException with the cancelled flag set.
                 */
            assert polyglotException.isCancelled();
        }
    } finally {
        service.shutdown();
    }
}
Also used : Context(org.graalvm.polyglot.Context) ExecutorService(java.util.concurrent.ExecutorService) Value(org.graalvm.polyglot.Value) ExecutionException(java.util.concurrent.ExecutionException) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 93 with Context

use of org.graalvm.polyglot.Context in project sulong by graalvm.

the class ParserTortureSuite method test.

@Test
public void test() throws Exception {
    List<Path> testCandidates = Files.walk(path).filter(BaseTestHarness.isFile).filter(BaseTestHarness.isSulong).collect(Collectors.toList());
    for (Path candidate : testCandidates) {
        if (!candidate.toAbsolutePath().toFile().exists()) {
            throw new AssertionError("File " + candidate.toAbsolutePath().toFile() + " does not exist.");
        }
        try {
            Context context = Context.create();
            context.eval(org.graalvm.polyglot.Source.newBuilder(LLVMLanguage.NAME, candidate.toFile()).build());
            context.close();
        } catch (Throwable e) {
            throw e;
        }
    }
}
Also used : Path(java.nio.file.Path) Context(org.graalvm.polyglot.Context) Test(org.junit.Test)

Example 94 with Context

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

the class PolyglotEngineOptionsTest method testCompilationThreshold.

private static void testCompilationThreshold(int value, String optionValue, Runnable doWhile) {
    Context.Builder builder = Context.newBuilder("sl");
    if (optionValue != null) {
        builder.option(COMPILATION_THRESHOLD_OPTION, optionValue);
    }
    Context context = builder.build();
    // installs isOptimized
    installSLBuiltin(context, SLIsOptimizedBuiltinFactory.getInstance());
    context.eval("sl", "function test() {}");
    Value test = context.getBindings("sl").getMember("test");
    Value isOptimized = context.getBindings("sl").getMember("isOptimized");
    Assert.assertFalse(isOptimized.execute(test).asBoolean());
    for (int i = 0; i < value - 1; i++) {
        Assert.assertFalse(isOptimized.execute(test).asBoolean());
        test.execute();
    }
    if (doWhile != null) {
        doWhile.run();
    }
    Assert.assertFalse(isOptimized.execute(test).asBoolean());
    test.execute();
    Assert.assertTrue(isOptimized.execute(test).asBoolean());
    test.execute();
    Assert.assertTrue(isOptimized.execute(test).asBoolean());
}
Also used : SLContext(com.oracle.truffle.sl.runtime.SLContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value)

Example 95 with Context

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

the class SplittingStrategyTest method testSplitLimitIsContextSpecific.

@Test
public void testSplitLimitIsContextSpecific() {
    Context c1 = Context.create();
    Context c2 = Context.create();
    // Use up the c1 budget
    useUpTheBudget(c1);
    final int c1BaseSplitCount = listener.splitCount;
    // Try to split some more in c1
    for (int i = 0; i < 10; i++) {
        eval(c1, "exec");
    }
    Assert.assertEquals("Splitting over budget!", c1BaseSplitCount, listener.splitCount);
    // Try to split in c2
    for (int i = 0; i < 10; i++) {
        eval(c2, "exec");
    }
    Assert.assertTrue("No splitting in different context", c1BaseSplitCount < listener.splitCount);
}
Also used : Context(org.graalvm.polyglot.Context) 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