Search in sources :

Example 1 with Language

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

the class Launcher method collectAllArguments.

private Set<String> collectAllArguments() {
    Engine engine = getTempEngine();
    Set<String> options = new LinkedHashSet<>();
    collectArguments(options);
    options.add("--polylgot");
    options.add("--native");
    options.add("--jvm");
    options.add("--help");
    options.add("--help:languages");
    options.add("--help:tools");
    options.add("--help:expert");
    options.add("--version:graalvm");
    options.add("--show-version:graalvm");
    if (helpExpert || helpDebug) {
        options.add("--help:debug");
    }
    addOptions(engine.getOptions(), options);
    for (Language language : engine.getLanguages().values()) {
        addOptions(language.getOptions(), options);
    }
    for (Instrument instrument : engine.getInstruments().values()) {
        addOptions(instrument.getOptions(), options);
    }
    return options;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Language(org.graalvm.polyglot.Language) Instrument(org.graalvm.polyglot.Instrument) Engine(org.graalvm.polyglot.Engine)

Example 2 with Language

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

the class Launcher method printLanguageOptions.

private static void printLanguageOptions(Engine engine, OptionCategory optionCategory) {
    Map<Language, List<PrintableOption>> languagesOptions = new HashMap<>();
    List<Language> languages = sortedLanguages(engine);
    for (Language language : languages) {
        List<PrintableOption> options = new ArrayList<>();
        for (OptionDescriptor descriptor : language.getOptions()) {
            if (descriptor.getCategory().ordinal() == optionCategory.ordinal()) {
                options.add(asPrintableOption(descriptor));
            }
        }
        if (!options.isEmpty()) {
            languagesOptions.put(language, options);
        }
    }
    if (!languagesOptions.isEmpty()) {
        System.out.println();
        System.out.println("Language Options:");
        for (Language language : languages) {
            List<PrintableOption> options = languagesOptions.get(language);
            if (options != null) {
                printOptions(options, "  " + language.getName() + ":", 4);
            }
        }
    }
}
Also used : Language(org.graalvm.polyglot.Language) HashMap(java.util.HashMap) OptionDescriptor(org.graalvm.options.OptionDescriptor) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Language

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

the class MultiLanguageShell method readEvalPrint.

public int readEvalPrint() throws IOException {
    ConsoleReader console = new ConsoleReader(in, out);
    console.setHandleUserInterrupt(true);
    console.setExpandEvents(false);
    console.setCopyPasteDetection(true);
    console.println("GraalVM MultiLanguage Shell " + context.getEngine().getVersion());
    console.println("Copyright (c) 2013-2018, Oracle and/or its affiliates");
    List<Language> languages = new ArrayList<>();
    Set<Language> uniqueValues = new HashSet<>();
    for (Language language : context.getEngine().getLanguages().values()) {
        if (language.isInteractive()) {
            if (uniqueValues.add(language)) {
                languages.add(language);
            }
        }
    }
    languages.sort(Comparator.comparing(Language::getName));
    Map<String, Language> prompts = new HashMap<>();
    StringBuilder promptsString = new StringBuilder();
    for (Language language : languages) {
        String prompt = createPrompt(language).trim();
        promptsString.append(prompt).append(" ");
        prompts.put(prompt, language);
        console.println("  " + language.getName() + " version " + language.getVersion());
    }
    if (languages.isEmpty()) {
        throw new Launcher.AbortException("Error: No Graal languages installed. Exiting shell.", 1);
    }
    printUsage(console, promptsString, false);
    int maxNameLength = 0;
    for (Language language : languages) {
        maxNameLength = Math.max(maxNameLength, language.getName().length());
    }
    String startLanguage = defaultStartLanguage;
    if (startLanguage == null) {
        startLanguage = languages.get(0).getId();
    }
    Language currentLanguage = context.getEngine().getLanguages().get(startLanguage);
    if (currentLanguage == null) {
        throw new Launcher.AbortException("Error: could not find language '" + startLanguage + "'", 1);
    }
    assert languages.indexOf(currentLanguage) >= 0;
    Source bufferSource = null;
    String id = currentLanguage.getId();
    // console.println("initialize time: " + (System.currentTimeMillis() - start));
    String prompt = createPrompt(currentLanguage);
    console.getKeys().bind(String.valueOf((char) 12), new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            throw new ChangeLanguageException(null);
        }
    });
    console.getKeys().bind(String.valueOf((char) 10), new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            throw new RuntimeIncompleteSourceException();
        }
    });
    // initializes the language
    context.initialize(currentLanguage.getId());
    boolean verboseErrors = false;
    for (; ; ) {
        String input = null;
        Source source = null;
        try {
            input = console.readLine(bufferSource == null ? prompt : createBufferPrompt(prompt));
            if (input == null) {
                break;
            } else if (input.trim().equals("")) {
                continue;
            }
            Language switchedLanguage = null;
            String trimmedInput = input.trim();
            if (trimmedInput.equals("-usage")) {
                printUsage(console, promptsString, true);
                input = "";
            } else if (trimmedInput.equals("-verboseErrors")) {
                verboseErrors = !verboseErrors;
                if (verboseErrors) {
                    console.println("Verbose errors is now on.");
                } else {
                    console.println("Verbose errors is now off.");
                }
                input = "";
            } else if (prompts.containsKey(trimmedInput)) {
                switchedLanguage = prompts.get(input);
                input = "";
            }
            NonBlockingInputStream nonBlockIn = ((NonBlockingInputStream) console.getInput());
            while (nonBlockIn.isNonBlockingEnabled() && nonBlockIn.peek(10) != -2 && switchedLanguage == null) {
                String line = console.readLine(createBufferPrompt(prompt));
                String trimmedLine = line.trim();
                if (prompts.containsKey(trimmedLine)) {
                    switchedLanguage = prompts.get(trimmedLine);
                    break;
                } else {
                    input += "\n" + line;
                }
            }
            if (!input.trim().equals("")) {
                source = Source.newBuilder(currentLanguage.getId(), input, "<shell>").interactive(true).build();
                context.eval(source);
                bufferSource = null;
                console.getHistory().replace(source.getCharacters());
            }
            if (switchedLanguage != null) {
                throw new ChangeLanguageException(switchedLanguage);
            }
        } catch (UserInterruptException | EOFException e) {
            // interrupted by ctrl-c
            break;
        } catch (ChangeLanguageException e) {
            bufferSource = null;
            histories.put(currentLanguage, console.getHistory());
            currentLanguage = e.getLanguage() == null ? languages.get((languages.indexOf(currentLanguage) + 1) % languages.size()) : e.getLanguage();
            History history = histories.computeIfAbsent(currentLanguage, k -> new MemoryHistory());
            console.setHistory(history);
            id = currentLanguage.getId();
            prompt = createPrompt(currentLanguage);
            console.resetPromptLine("", "", 0);
            context.initialize(id);
        } catch (ThreadDeath e) {
            console.println("Execution killed!");
            continue;
        } catch (RuntimeIncompleteSourceException e) {
            console.println();
            input += "\n";
            bufferSource = source;
        } catch (PolyglotException e) {
            input += "\n";
            bufferSource = source;
            if (e.isExit()) {
                return e.getExitStatus();
            } else if (e.isIncompleteSource()) {
                input += "\n";
                bufferSource = source;
            } else if (!e.isInternalError()) {
                if (e.getMessage() != null && e.getMessage().isEmpty()) {
                    console.println(e.toString());
                } else {
                    if (verboseErrors) {
                        e.printStackTrace(new PrintWriter(console.getOutput()));
                    }
                }
            } else {
                e.printStackTrace(new PrintWriter(console.getOutput()));
            }
        } catch (Throwable e) {
            e.printStackTrace(new PrintWriter(console.getOutput()));
        }
    }
    return 0;
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) History(jline.console.history.History) ActionListener(java.awt.event.ActionListener) PolyglotException(org.graalvm.polyglot.PolyglotException) MemoryHistory(jline.console.history.MemoryHistory) UserInterruptException(jline.console.UserInterruptException) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) ActionEvent(java.awt.event.ActionEvent) EOFException(java.io.EOFException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ConsoleReader(jline.console.ConsoleReader) Language(org.graalvm.polyglot.Language) Map(java.util.Map) Source(org.graalvm.polyglot.Source) Context(org.graalvm.polyglot.Context) Comparator(java.util.Comparator) NonBlockingInputStream(jline.internal.NonBlockingInputStream) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) NonBlockingInputStream(jline.internal.NonBlockingInputStream) UserInterruptException(jline.console.UserInterruptException) History(jline.console.history.History) MemoryHistory(jline.console.history.MemoryHistory) Source(org.graalvm.polyglot.Source) Language(org.graalvm.polyglot.Language) EOFException(java.io.EOFException) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter) ConsoleReader(jline.console.ConsoleReader) MemoryHistory(jline.console.history.MemoryHistory) PolyglotException(org.graalvm.polyglot.PolyglotException) ActionListener(java.awt.event.ActionListener)

Example 4 with Language

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

the class EvalLauncher method printVersions.

private static void printVersions() {
    Engine engine = Engine.create();
    System.out.println("GraalVM Polyglot Engine Version " + engine.getVersion());
    System.out.println("Installed Languages: ");
    for (Language language : engine.getLanguages().values()) {
        System.out.printf("    %-10s: %-10s Version %s%n", language.getId(), language.getName(), language.getVersion());
    }
    System.out.println("Installed Instruments: ");
    for (Instrument instrument : engine.getInstruments().values()) {
        System.out.printf("    %-10s: %-10s Version %s%n", instrument.getId(), instrument.getName(), instrument.getVersion());
    }
}
Also used : Language(org.graalvm.polyglot.Language) Instrument(org.graalvm.polyglot.Instrument) Engine(org.graalvm.polyglot.Engine)

Example 5 with Language

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

the class SourceSectionFilterTest method createRootNode.

static RootNode createRootNode(Engine engine, final SourceSection section, final Boolean internal, Node... children) throws Exception {
    Language language = engine.getLanguages().get(InstrumentationTestLanguage.ID);
    Field impl = Language.class.getDeclaredField("impl");
    ReflectionUtils.setAccessible(impl, true);
    Object polyglotLanguage = impl.get(language);
    Method ensureInitialized = polyglotLanguage.getClass().getDeclaredMethod("ensureInitialized");
    ReflectionUtils.setAccessible(ensureInitialized, true);
    ensureInitialized.invoke(polyglotLanguage);
    Field info = polyglotLanguage.getClass().getDeclaredField("info");
    ReflectionUtils.setAccessible(info, true);
    LanguageInfo languageInfo = (LanguageInfo) info.get(polyglotLanguage);
    Field spi = LanguageInfo.class.getDeclaredField("spi");
    ReflectionUtils.setAccessible(spi, true);
    TruffleLanguage<?> truffleLanguage = (TruffleLanguage<?>) spi.get(languageInfo);
    return new RootNode(truffleLanguage) {

        @Node.Children
        Node[] rootChildren = children;

        @Override
        protected boolean isInstrumentable() {
            return true;
        }

        @Override
        public SourceSection getSourceSection() {
            return section;
        }

        @Override
        public boolean isInternal() {
            if (internal == null) {
                return super.isInternal();
            } else {
                return internal;
            }
        }

        @Override
        public Object execute(VirtualFrame frame) {
            return null;
        }
    };
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Field(java.lang.reflect.Field) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) RootNode(com.oracle.truffle.api.nodes.RootNode) Language(org.graalvm.polyglot.Language) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Method(java.lang.reflect.Method) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Aggregations

Language (org.graalvm.polyglot.Language)13 Engine (org.graalvm.polyglot.Engine)6 ArrayList (java.util.ArrayList)3 OptionDescriptor (org.graalvm.options.OptionDescriptor)3 Instrument (org.graalvm.polyglot.Instrument)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Test (org.junit.Test)2 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Field (java.lang.reflect.Field)1