use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextsEventsTest method testGetActiveContexts.
@Test
public void testGetActiveContexts() {
try {
TestContextsInstrument.includeActiveContexts = true;
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()"));
assertTrue(Integer.toString(events.size()), events.size() > 1);
// Have creation of polyglot context and a language
assertEquals(3, events.size());
assertTrue(events.get(0).created);
assertNotNull(events.get(0).context);
assertNull(events.get(0).language);
assertTrue(events.get(1).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
assertTrue(events.get(2).languageInitialized);
}
assertEquals(6, events.size());
Engine engine = Engine.create();
// Contexts created and closed:
try (Context context = Context.newBuilder().engine(engine).build()) {
context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
}
Instrument testBlockOnStatementsInstrument = engine.getInstruments().get("testBlockOnStatementsInstrument");
ThreadsEventsTest.TestBlockOnStatementsInstrument testBlock = testBlockOnStatementsInstrument.lookup(ThreadsEventsTest.TestBlockOnStatementsInstrument.class);
testBlock.runOnBlock = new Runnable() {
@Override
// We want to hide 'events' from the outer context not to use it by a mistake
@SuppressWarnings("hiding")
public void run() {
Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
final List<ContextEvent> events = test.events;
assertEquals(3, events.size());
assertTrue(events.get(0).created);
assertNull(events.get(0).language);
assertTrue(events.get(1).created);
assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
assertTrue(events.get(2).languageInitialized);
testBlock.blockOnStatements.set(false);
}
};
Context context = Context.newBuilder().engine(engine).build();
context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(CONTEXT(EXPRESSION()), CONTEXT(STATEMENT))"));
} finally {
TestContextsInstrument.includeActiveContexts = false;
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextsEventsTest method testMultipleContexts.
@Test
public void testMultipleContexts() {
final int numContexts = 5;
final List<ContextEvent> events;
try (Engine engine = Engine.create()) {
Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
events = test.events;
Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
for (int i = 0; i < numContexts; i++) {
try (Context context = Context.newBuilder().engine(engine).build()) {
assertEquals(6 * i + 1, events.size());
context.eval(source);
}
assertEquals(6 * i + 6, events.size());
}
assertEquals(6 * numContexts, events.size());
TruffleContext lastContext = null;
for (int i = 0; i < numContexts; i++) {
int ci = 6 * i;
assertTrue(events.get(ci).created);
assertNull(events.get(ci).language);
assertNotEquals(lastContext, events.get(ci).context);
lastContext = events.get(ci).context;
assertTrue(events.get(ci + 1).created);
assertNotNull(events.get(ci + 1).language);
assertEquals(lastContext, events.get(ci + 1).context);
assertTrue(events.get(ci + 2).languageInitialized);
assertTrue(events.get(ci + 3).languageFinalized);
assertNotNull(events.get(ci + 4).language);
assertNull(events.get(ci + 5).language);
}
}
// No more events
assertEquals(6 * numContexts, events.size());
}
use of org.graalvm.polyglot.Context in project sieve by jtulach.
the class Main method execute.
private static void execute(Integer repeat) throws Exception {
Context vm = Context.newBuilder().build();
if (repeat != null) {
vm.eval("js", "count=" + repeat);
}
File scriptDir = findScriptDir();
Source ruby = Source.newBuilder("ruby", new File(scriptDir, "sieve.rb")).build();
Source js = Source.newBuilder("js", new File(scriptDir, "sieve.js")).build();
vm.eval(ruby);
vm.eval(js);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SplittingStrategyTest method testHardSplitLimitInContext.
@Test
@SuppressWarnings("try")
public void testHardSplitLimitInContext() {
final int expectedSplittingIncrease = 20;
try (TruffleCompilerOptions.TruffleOptionsOverrideScope s = TruffleCompilerOptions.overrideOptions(TruffleCompilerOptions.TruffleSplittingMaxNumberOfSplitNodes, expectedSplittingIncrease)) {
Context c = Context.create();
for (int i = 0; i < 100; i++) {
eval(c, "exec");
}
Assert.assertEquals("Wrong number of splits: ", expectedSplittingIncrease, listener.splitCount * DUMMYROOTNODECOUNT);
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SplittingStrategyTest method testGrowingSplitLimitInContext.
@Test
public void testGrowingSplitLimitInContext() {
Context c = Context.create();
// Eval a lot to fill out budget
useUpTheBudget(c);
final int baseSplitCount = listener.splitCount;
for (int i = 0; i < 10; i++) {
eval(c, "exec");
}
Assert.assertEquals("Split count growing without new call targets", baseSplitCount, listener.splitCount);
eval(c, "new");
for (int i = 0; i < 10; i++) {
eval(c, "exec");
}
Assert.assertEquals("Split count not correct after one new target", (int) (baseSplitCount + TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleSplittingGrowthLimit)), listener.splitCount);
eval(c, "new2");
for (int i = 0; i < 10; i++) {
eval(c, "exec");
}
Assert.assertEquals("Split count not correct after one new target", (int) (baseSplitCount + 2 * TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleSplittingGrowthLimit)), listener.splitCount);
}
Aggregations