use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class LegacyTruffleExceptionTest method testLegacyCancelException.
@Test
public void testLegacyCancelException() {
setupEnv(createContext(verifyingHandler), new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
return createAST(LegacyTruffleExceptionTest.class, languageInstance, (n) -> {
LegacyCancelException exception = new LegacyCancelException(n);
LangObject exceptionObject = new LangObject(exception);
exception.setExceptionObject(exceptionObject);
return exceptionObject;
}, false);
}
});
verifyingHandler.expect(BlockNode.Kind.TRY);
assertFails(() -> context.eval(ProxyLanguage.ID, "Test"), PolyglotException.class, (e) -> {
Assert.assertTrue(e.isCancelled());
Assert.assertNotNull(e.getGuestObject());
});
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class InstrumentationTest method testAsynchronousStacks3.
@Test
public void testAsynchronousStacks3() {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return new AsyncRootNode(language, 0).getCallTarget();
}
class AsyncRootNode extends RootNode {
private final TruffleLanguage<?> language;
private final int level;
@Node.Child
private AsyncNode child;
AsyncRootNode(TruffleLanguage<?> language, int level) {
super(language, createFrameDescriptor(1));
this.language = language;
this.level = level;
this.child = new AsyncNode(level);
}
@Override
public Object execute(VirtualFrame frame) {
storeInvokedTarget(frame.materialize());
child.execute(frame);
return 42;
}
@TruffleBoundary
private void storeInvokedTarget(MaterializedFrame frame) {
CallTarget callTarget = Truffle.getRuntime().iterateFrames((f) -> f.getCallTarget());
frame.setObject(TARGET_SLOT, callTarget);
}
@Override
public SourceSection getSourceSection() {
return com.oracle.truffle.api.source.Source.newBuilder(ID, Integer.toString(level), "level").build().createSection(1);
}
@Override
protected boolean isInstrumentable() {
return true;
}
@Override
protected List<TruffleStackTraceElement> findAsynchronousFrames(Frame frame) {
assertSame(this.getFrameDescriptor(), frame.getFrameDescriptor());
AsyncRootNode invoker = new AsyncRootNode(language, level + 1);
RootCallTarget invokerTarget = invoker.getCallTarget();
Frame invokerFrame = Truffle.getRuntime().createMaterializedFrame(new Object[] { level + 1 }, invoker.getFrameDescriptor());
TruffleStackTraceElement element = TruffleStackTraceElement.create(invoker.child, invokerTarget, invokerFrame);
return Collections.singletonList(element);
}
}
});
AtomicReference<List<TruffleStackTraceElement>> asyncStack = new AtomicReference<>();
instrumentEnv.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.ANY, new ExecutionEventListener() {
@Override
public void onEnter(EventContext ctx, VirtualFrame frame) {
assertNull(asyncStack.get());
asyncStack.set(TruffleStackTrace.getAsynchronousStackTrace(ctx.getInstrumentedNode().getRootNode().getCallTarget(), frame));
}
@Override
public void onReturnValue(EventContext ctx, VirtualFrame frame, Object result) {
}
@Override
public void onReturnExceptional(EventContext ctx, VirtualFrame frame, Throwable exception) {
}
});
int ret = context.eval(ProxyLanguage.ID, "").asInt();
assertEquals(42, ret);
List<TruffleStackTraceElement> stack = asyncStack.get();
int numTestLevels = 10;
for (int i = 1; i < numTestLevels; i++) {
assertEquals(1, stack.size());
TruffleStackTraceElement element = stack.get(0);
assertEquals(i, ((AsyncNode) element.getLocation()).getLevel());
assertEquals(Integer.toString(i), element.getTarget().getRootNode().getSourceSection().getCharacters());
stack = TruffleStackTrace.getAsynchronousStackTrace(element.getTarget(), element.getFrame());
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class TruffleContextTest method testCancellingUncaughtExceptionHandler.
@Test
public void testCancellingUncaughtExceptionHandler() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
setupEnv(Context.newBuilder().allowAllAccess(true).err(out).build(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) {
RootNode rootNode;
String command = request.getSource().getCharacters().toString();
switch(command) {
case "controller":
rootNode = new ControllerNode(languageInstance);
break;
case "worker":
rootNode = new WorkerNode(languageInstance);
break;
default:
throw CompilerDirectives.shouldNotReachHere("Unknown request: " + command);
}
return rootNode.getCallTarget();
}
});
context.eval(Source.newBuilder(ProxyLanguage.ID, "controller", "test").buildLiteral());
assertFalse(out.toString().contains(getCancelExecutionClass().getName()));
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class TruffleContextTest method testInitializeMultiContextForInnerContext.
@Test
public void testInitializeMultiContextForInnerContext() {
AtomicBoolean multiContextInitialized = new AtomicBoolean(false);
setupEnv(Context.newBuilder().engine(Engine.create()).build(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return RootNode.createConstantNode(42).getCallTarget();
}
@Override
protected void initializeMultipleContexts() {
multiContextInitialized.set(true);
}
@Override
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
return true;
}
});
TruffleContext ic = languageEnv.newContextBuilder().initializeCreatorContext(true).build();
assertTrue(multiContextInitialized.get());
ic.close();
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class SourceListenerTest method testNoMaterializationOnSourceListener.
@Test
public void testNoMaterializationOnSourceListener() {
setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
com.oracle.truffle.api.source.Source source = request.getSource();
return new RootNode(languageInstance) {
@Node.Child
private NeverMaterializedNode child = new NeverMaterializedNode();
@Override
public Object execute(VirtualFrame frame) {
return child.execute(frame);
}
@Override
public SourceSection getSourceSection() {
return source.createSection(1);
}
}.getCallTarget();
}
});
context.eval(Source.create(ProxyLanguage.ID, "a"));
instrumentEnv.getInstrumenter().attachLoadSourceListener(SourceFilter.ANY, s -> {
}, true);
instrumentEnv.getInstrumenter().attachExecuteSourceListener(SourceFilter.ANY, s -> {
}, true);
context.eval(Source.create(ProxyLanguage.ID, "b"));
// Verify that it fails when materialized:
try {
instrumentEnv.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.ANY, e -> {
}, true);
Assert.fail();
} catch (IllegalStateException ex) {
// O.K.
}
}
Aggregations