Search in sources :

Example 41 with Instrument

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

the class ContextsEventsTest method testSingleEngineContext.

@Test
public void testSingleEngineContext() {
    Engine engine = Engine.create();
    Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
    TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
    final List<ContextEvent> events = test.events;
    try (Context context = Context.newBuilder().engine(engine).build()) {
        assertEquals(1, events.size());
        assertTrue(events.get(0).created);
        assertNotNull(events.get(0).context);
        assertNull(events.get(0).language);
        context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
        assertEquals(3, events.size());
        assertTrue(events.get(1).created);
        assertNotNull(events.get(1).language);
        assertEquals(events.get(0).context, events.get(1).context);
        assertTrue(events.get(2).languageInitialized);
        assertEquals(InstrumentationTestLanguage.ID, events.get(2).language.getId());
        assertEquals(events.get(0).context, events.get(2).context);
    }
    assertEquals(6, events.size());
    assertTrue(events.get(3).languageFinalized);
    assertFalse(events.get(4).created);
    assertNotNull(events.get(4).language);
    assertFalse(events.get(5).created);
    assertNull(events.get(5).language);
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 42 with Instrument

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

the class ContextsEventsTest method testSingleContext.

@Test
public void testSingleContext() {
    final List<ContextEvent> events;
    try (Context context = Context.create()) {
        Instrument testContexsInstrument = context.getEngine().getInstruments().get("testContexsInstrument");
        TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
        events = test.events;
        context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
        assertEquals(2, events.size());
        assertTrue(events.get(1).languageInitialized);
        assertEquals(InstrumentationTestLanguage.ID, events.get(0).language.getId());
        assertNotNull(events.get(0).context);
        assertTrue(events.get(1).languageInitialized);
    }
    assertEquals(5, events.size());
    assertTrue(events.get(2).languageFinalized);
    assertEquals(InstrumentationTestLanguage.ID, events.get(2).language.getId());
    assertEquals(events.get(0).context, events.get(2).context);
    assertFalse(events.get(3).created);
    assertEquals(InstrumentationTestLanguage.ID, events.get(3).language.getId());
    assertEquals(events.get(0).context, events.get(3).context);
    assertFalse(events.get(4).created);
    assertNull(events.get(4).language);
    assertEquals(events.get(0).context, events.get(4).context);
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 43 with Instrument

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

the class Launcher method printInstruments.

private static void printInstruments(Engine engine, boolean printWhenEmpty) {
    if (engine.getInstruments().isEmpty()) {
        if (printWhenEmpty) {
            System.out.println("  Installed Tools: none");
        }
    } else {
        System.out.println("  Installed Tools:");
        List<Instrument> instruments = sortedInstruments(engine);
        int nameLength = 0;
        for (Instrument instrument : instruments) {
            nameLength = max(nameLength, instrument.getName().length());
        }
        String instrumentFormat = "    %-" + nameLength + "s version %s%n";
        for (Instrument instrument : instruments) {
            String version = instrument.getVersion();
            if (version == null || version.length() == 0) {
                version = "";
            }
            System.out.printf(instrumentFormat, instrument.getName().isEmpty() ? instrument.getId() : instrument.getName(), version);
        }
    }
}
Also used : Instrument(org.graalvm.polyglot.Instrument)

Example 44 with Instrument

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

the class EvalLauncher method printHelp.

private static void printHelp(OptionCategory maxCategory) {
    Engine engine = Engine.create();
    System.out.println(BASIC_HELP);
    System.out.println("Engine options: ");
    for (OptionDescriptor descriptor : engine.getOptions()) {
        printOption(maxCategory, descriptor);
    }
    System.out.println("Language options: ");
    for (String languageId : engine.getLanguages().keySet()) {
        Language language = engine.getLanguage(languageId);
        for (OptionDescriptor descriptor : language.getOptions()) {
            printOption(maxCategory, descriptor);
        }
    }
    System.out.println("Tool options: ");
    for (String instrumentId : engine.getInstruments().keySet()) {
        Instrument instrument = engine.getInstrument(instrumentId);
        for (OptionDescriptor descriptor : instrument.getOptions()) {
            printOption(maxCategory, descriptor);
        }
    }
}
Also used : Language(org.graalvm.polyglot.Language) OptionDescriptor(org.graalvm.options.OptionDescriptor) Instrument(org.graalvm.polyglot.Instrument) Engine(org.graalvm.polyglot.Engine)

Example 45 with Instrument

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

the class PolyglotEngineImpl method requirePublicInstrument.

@Override
public Instrument requirePublicInstrument(String id) {
    checkState();
    Instrument instrument = idToPublicInstrument.get(id);
    if (instrument == null) {
        String misspelledGuess = matchSpellingError(idToPublicInstrument.keySet(), id);
        String didYouMean = "";
        if (misspelledGuess != null) {
            didYouMean = String.format("Did you mean '%s'? ", misspelledGuess);
        }
        throw new PolyglotIllegalStateException(String.format("An instrument with id '%s' is not installed. %sInstalled instruments are: %s.", id, didYouMean, getInstruments().keySet()));
    }
    return instrument;
}
Also used : Instrument(org.graalvm.polyglot.Instrument)

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