All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graalvm.launcher.PolyglotLauncher Maven / Gradle / Ivy

There is a newer version: 1.0.0-rc7
Show newest version
/*
 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package org.graalvm.launcher;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.graalvm.options.OptionCategory;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.Language;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

public final class PolyglotLauncher extends Launcher {

    private String mainLanguage = null;

    @Override
    protected void printHelp(OptionCategory maxCategory) {
        Engine engine = Engine.create();
        // @formatter:off
        printVersion(engine);
        System.out.println();
        System.out.println("Usage: polyglot [OPTION]... [FILE] [ARGS]...");
        List languages = sortedLanguages(engine);
        System.out.print("Available languages: ");
        String sep = "";
        for (Language language : languages) {
            System.out.print(sep);
            System.out.print(language.getId());
            sep = ", ";
        }
        System.out.println();
        System.out.println("Basic Options:");
        printOption("--language ",      "Specifies the main language.");
        printOption("--file [:]FILE",   "Additional file to execute.");
        printOption("--eval [:]CODE",   "Evaluates code snippets, for example, '--eval js:42'.");
        printOption("--shell",                "Start a multi language shell.");
        // @formatter:on
    }

    @Override
    protected void collectArguments(Set args) {
        args.addAll(Arrays.asList(
                        "--language",
                        "--file [:]FILE",
                        "--eval [:]CODE",
                        "--shell"));
    }

    @Override
    protected void printVersion() {
        printVersion(Engine.create());
    }

    protected static void printVersion(Engine engine) {
        System.out.println("GraalVM polyglot launcher " + engine.getVersion());
    }

    private void launch(String[] args) {
        List arguments = new ArrayList<>(Arrays.asList(args));
        if (isAOT()) {
            nativeAccess.maybeExec(arguments, true, Collections.emptyMap(), VMType.Native);
            nativeAccess.setGraalVMProperties();
        }

        Map options = new HashMap<>();

        List