Search in sources :

Example 21 with PolyglotException

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

the class ScriptTest method testScript.

@Test
public void testScript() {
    Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
    boolean success = false;
    try {
        try {
            final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
            TestUtil.validateResult(testRun, result, null);
            success = true;
        } catch (PolyglotException pe) {
            TestUtil.validateResult(testRun, null, pe);
            success = true;
        }
    } finally {
        TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
    }
}
Also used : AbstractMap(java.util.AbstractMap) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 22 with PolyglotException

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

the class DebugALot method logThrowable.

private void logThrowable(Throwable t) {
    logger.print("\nERROR: Thrown: '");
    logger.print(t.getLocalizedMessage());
    logger.print("', throwable class = ");
    logger.println(t.getClass());
    if (t instanceof PolyglotException) {
        PolyglotException pe = (PolyglotException) t;
        logger.print("  Polyglot Message: '");
        logger.print(pe.getMessage());
        logger.println("'");
        logger.print("  canceled = ");
        logger.print(pe.isCancelled());
        logger.print(", exited = ");
        logger.print(pe.isExit());
        logger.print(", guest ex. = ");
        logger.print(pe.isGuestException());
        logger.print(", host ex. = ");
        logger.print(pe.isHostException());
        logger.print(", incompl. source = ");
        logger.print(pe.isIncompleteSource());
        logger.print(", internal = ");
        logger.print(pe.isInternalError());
        logger.print(", syntax error = ");
        logger.println(pe.isSyntaxError());
        logger.print("  Source Section: ");
        logSourceSection(pe.getSourceLocation());
        if (pe.isExit()) {
            logger.print("  Exit Status = ");
            logger.println(pe.getExitStatus());
        }
        if (pe.isGuestException()) {
            Value guestObject = pe.getGuestObject();
            logger.print("  Guest Object = ");
            logger.println(guestObject.toString());
        }
        if (pe.isHostException()) {
            logger.println("  Host Exception:");
            pe.asHostException().printStackTrace(logger);
        }
        logger.println("  Polyglot Stack Trace:");
        for (PolyglotException.StackFrame sf : pe.getPolyglotStackTrace()) {
            logger.print("    Language ID: ");
            logger.println(sf.getLanguage().getId());
            logger.print("    Root Name: ");
            logger.println(sf.getRootName());
            logger.print("    Source Location: ");
            logSourceSection(sf.getSourceLocation());
            logger.print("    Guest Frame: ");
            logger.println(sf.isGuestFrame());
            logger.print("    Host Frame: ");
            if (sf.isHostFrame()) {
                logger.println(sf.toHostFrame());
            } else {
                logger.println(false);
            }
        }
    } else {
        t.printStackTrace(logger);
    }
}
Also used : Value(org.graalvm.polyglot.Value) DebugValue(com.oracle.truffle.api.debug.DebugValue) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 23 with PolyglotException

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

the class IdentityFunctionTest method testIdentityFunction.

@Test
public void testIdentityFunction() {
    Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
    boolean success = false;
    try {
        try {
            final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
            TestUtil.validateResult(testRun, result, null);
            success = true;
        } catch (PolyglotException pe) {
            TestUtil.validateResult(testRun, null, pe);
            success = true;
        }
    } finally {
        TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
    }
}
Also used : AbstractMap(java.util.AbstractMap) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 24 with PolyglotException

use of org.graalvm.polyglot.PolyglotException 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 25 with PolyglotException

use of org.graalvm.polyglot.PolyglotException 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)

Aggregations

PolyglotException (org.graalvm.polyglot.PolyglotException)43 Value (org.graalvm.polyglot.Value)32 Test (org.junit.Test)32 Context (org.graalvm.polyglot.Context)19 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)9 Source (org.graalvm.polyglot.Source)9 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)6 StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)6 AbstractMap (java.util.AbstractMap)5 TruffleContext (com.oracle.truffle.api.TruffleContext)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 Engine (org.graalvm.polyglot.Engine)4 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)4 Env (com.oracle.truffle.api.TruffleLanguage.Env)3 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)3 IOException (java.io.IOException)3 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 CodeAction (com.oracle.truffle.api.instrumentation.test.UnwindReenterReturnTest.TestControlFlow.CodeAction)2