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);
}
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);
}
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);
}
}
}
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);
}
}
}
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;
}
Aggregations