Search in sources :

Example 1 with Instrument

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

the class Launcher method sortedInstruments.

static List<Instrument> sortedInstruments(Engine engine) {
    List<Instrument> instruments = new ArrayList<>();
    for (Instrument instrument : engine.getInstruments().values()) {
        // no options not accessible to the user.
        if (!instrument.getOptions().iterator().hasNext()) {
            continue;
        }
        instruments.add(instrument);
    }
    instruments.sort(Comparator.comparing(Instrument::getId));
    return instruments;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) ArrayList(java.util.ArrayList)

Example 2 with Instrument

use of org.graalvm.polyglot.Instrument 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 3 with Instrument

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

the class Launcher method printInstrumentOptions.

private static void printInstrumentOptions(Engine engine, OptionCategory optionCategory) {
    Map<Instrument, List<PrintableOption>> instrumentsOptions = new HashMap<>();
    List<Instrument> instruments = sortedInstruments(engine);
    for (Instrument instrument : instruments) {
        List<PrintableOption> options = new ArrayList<>();
        for (OptionDescriptor descriptor : instrument.getOptions()) {
            if (descriptor.getCategory().ordinal() == optionCategory.ordinal()) {
                options.add(asPrintableOption(descriptor));
            }
        }
        if (!options.isEmpty()) {
            instrumentsOptions.put(instrument, options);
        }
    }
    if (!instrumentsOptions.isEmpty()) {
        System.out.println();
        System.out.println("Tool options:");
        for (Instrument instrument : instruments) {
            List<PrintableOption> options = instrumentsOptions.get(instrument);
            if (options != null) {
                printOptions(options, "  " + instrument.getName() + ":", 4);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) OptionDescriptor(org.graalvm.options.OptionDescriptor) Instrument(org.graalvm.polyglot.Instrument) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Instrument

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

the class SLSharedCodeSeparatedEnvTest method instrumentsSeeOutputOfBoth.

@Test
public void instrumentsSeeOutputOfBoth() throws Exception {
    Instrument outInstr = e2.getEngine().getInstruments().get("captureOutput");
    ByteArrayOutputStream outConsumer = outInstr.lookup(ByteArrayOutputStream.class);
    assertNotNull("Stream capturing is ready", outConsumer);
    String sayHello = "function main() {\n" + "  println(\"Ahoj\" + import(\"extra\"));" + "}";
    // @formatter:on
    e1.eval("sl", sayHello);
    assertEquals("Ahoj1\n", os1.toString("UTF-8"));
    assertEquals("", os2.toString("UTF-8"));
    e2.eval("sl", sayHello);
    assertEquals("Ahoj1\n", os1.toString("UTF-8"));
    assertEquals("Ahoj2\n", os2.toString("UTF-8"));
    engine.close();
    assertEquals("Output of both contexts and instruments is capturable", "initializingOutputCapture\n" + "Ahoj1\n" + "Ahoj2\n" + "endOfOutputCapture\n", outConsumer.toString("UTF-8"));
    assertEquals("Output of instrument goes not to os runtime if specified otherwise", "initializingOutputCapture\n" + "endOfOutputCapture\n", osRuntime.toString("UTF-8"));
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with Instrument

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

the class PolyglotEngineImpl method initializeInstruments.

private Map<String, PolyglotInstrument> initializeInstruments(Map<String, InstrumentInfo> infos) {
    Map<String, PolyglotInstrument> instruments = new LinkedHashMap<>();
    List<InstrumentCache> cachedInstruments = InstrumentCache.load(SPI.allLoaders());
    for (InstrumentCache instrumentCache : cachedInstruments) {
        PolyglotInstrument instrumentImpl = new PolyglotInstrument(this, instrumentCache);
        instrumentImpl.info = LANGUAGE.createInstrument(instrumentImpl, instrumentCache.getId(), instrumentCache.getName(), instrumentCache.getVersion());
        Instrument instrument = impl.getAPIAccess().newInstrument(instrumentImpl);
        instrumentImpl.api = instrument;
        String id = instrumentImpl.cache.getId();
        verifyId(id, instrumentCache.getClassName());
        if (instruments.containsKey(id)) {
            throw failDuplicateId(id, instrumentImpl.cache.getClassName(), instruments.get(id).cache.getClassName());
        }
        instruments.put(id, instrumentImpl);
        infos.put(id, instrumentImpl.info);
    }
    return instruments;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2