use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class RewriteDuringCompilationTest method testCompilation.
private void testCompilation(BaseNode testedCode, LoopNode loopNode, DetectInvalidCodeNode nodeToRewrite, int rewriteCount, int maxDelayBeforeRewrite) throws IOException, InterruptedException, ExecutionException {
// DetectInvalidCodeNode.invalidTwice does not work with multi-tier
// code can remain active of another tier with local invalidation.
setupEnv(Context.newBuilder().allowExperimentalOptions(true).option("engine.MultiTier", "false"), new ProxyLanguage() {
private CallTarget target;
@Override
protected synchronized CallTarget parse(ParsingRequest request) throws Exception {
com.oracle.truffle.api.source.Source source = request.getSource();
if (target == null) {
target = new RootNode(languageInstance) {
@Node.Child
private volatile BaseNode child = testedCode;
@Override
public Object execute(VirtualFrame frame) {
return child.execute(frame);
}
@Override
public SourceSection getSourceSection() {
return source.createSection(1);
}
}.getCallTarget();
}
return target;
}
});
AtomicReference<DetectInvalidCodeNode> nodeToRewriteReference = new AtomicReference<>(nodeToRewrite);
Random rnd = new Random();
CountDownLatch nodeRewritingLatch = new CountDownLatch(1);
List<Object> callTargetsToCheck = new ArrayList<>();
rewriting = true;
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<?> future = executor.submit(() -> {
try {
for (int i = 1; i <= rewriteCount && rewriting; i++) {
try {
Thread.sleep(rnd.nextInt(maxDelayBeforeRewrite));
nodeRewritingLatch.await();
} catch (InterruptedException ie) {
}
if (loopNode != null) {
Object loopNodeCallTarget = ((OptimizedOSRLoopNode) loopNode).getCompiledOSRLoop();
if (loopNodeCallTarget != null) {
callTargetsToCheck.add(loopNodeCallTarget);
}
}
DetectInvalidCodeNode previousNode = nodeToRewriteReference.get();
DetectInvalidCodeNode newNode = new DetectInvalidCodeNode();
nodeToRewriteReference.set(newNode);
previousNode.replace(newNode);
previousNode.valid = false;
}
} finally {
rewriting = false;
}
});
Source source = Source.newBuilder(ProxyLanguage.ID, "", "DummySource").build();
try {
for (; ; ) {
context.eval(source);
nodeRewritingLatch.countDown();
if (!rewriting) {
break;
}
}
} finally {
rewriting = false;
future.get();
executor.shutdownNow();
executor.awaitTermination(100, TimeUnit.SECONDS);
}
for (Object callTarget : callTargetsToCheck) {
Assert.assertFalse("Obsolete loop call target is still valid", ((OptimizedCallTarget) callTarget).isValid());
}
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class HostExceptionTest method before.
@Before
public void before() {
context = Context.newBuilder().allowAllAccess(true).build();
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected LanguageContext createContext(Env contextEnv) {
env = contextEnv;
return super.createContext(contextEnv);
}
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
RootNode rootNode;
switch(request.getSource().getCharacters().toString()) {
case "catcher":
rootNode = new CatcherRootNode();
break;
case "runner":
rootNode = new RunnerRootNode();
break;
case "rethrower":
rootNode = new RethrowerRootNode();
break;
default:
throw new IllegalArgumentException();
}
return RootNode.createConstantNode(new CatcherObject(rootNode.getCallTarget())).getCallTarget();
}
});
context.initialize(ProxyLanguage.ID);
context.enter();
assertNotNull(env);
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class GR31558 method before.
@Before
public void before() {
HostAccess.Builder hostAccessBuilder = //
HostAccess.newBuilder().allowAccessAnnotatedBy(//
HostAccess.Export.class).allowPublicAccess(//
false).allowArrayAccess(//
true).allowIterableAccess(//
true).allowIteratorAccess(//
true).allowImplementationsAnnotatedBy(FunctionalInterface.class);
Context.Builder contextBuilder = Context.newBuilder();
contextBuilder.allowHostAccess(hostAccessBuilder.build());
setupEnv(contextBuilder, new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
String src = request.getSource().getCharacters().toString();
RootCallTarget invokeTestApi;
if ("testFunction".equals(src)) {
invokeTestApi = new RootNode(ProxyLanguage.get(null)) {
@Override
public Object execute(VirtualFrame frame) {
try {
InteropLibrary interop = InteropLibrary.getUncached();
Object test = frame.getArguments()[0];
interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction());
if (interop.isMemberExisting(test, "testMap")) {
interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction(false, false, false, true));
}
if (interop.isMemberExisting(test, "testMapEntry")) {
interop.invokeMember(test, "testMapEntry", "hi", new ArgumentsCollectorFunction(false, false, true, false));
}
if (interop.isMemberExisting(test, "testList")) {
interop.invokeMember(test, "testList", "hi", new ArgumentsCollectorFunction(false, false, true, false));
}
if (interop.isMemberExisting(test, "testIterator")) {
interop.invokeMember(test, "testIterator", "hi", new ArgumentsCollectorFunction(false, true, false, false));
}
if (interop.isMemberExisting(test, "testIterable")) {
interop.invokeMember(test, "testIterable", "hi", new ArgumentsCollectorFunction(true, false, false, false));
}
return "success";
} catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
CompilerDirectives.transferToInterpreter();
throw new AssertionError(e);
}
}
}.getCallTarget();
} else {
throw new IllegalArgumentException(src);
}
return RootNode.createConstantNode(new HostExceptionTest.CatcherObject(invokeTestApi)).getCallTarget();
}
});
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class TruffleExceptionTest method testExceptionFromCreateContext.
@Test
public void testExceptionFromCreateContext() {
String message = "Failed to create";
ExceptionType type = ExceptionType.EXIT;
assertFails(() -> setupEnv(Context.create(), new ProxyLanguage() {
@Override
protected LanguageContext createContext(Env env) {
throw new TruffleExceptionImpl(message, null, type, null);
}
}), PolyglotException.class, (pe) -> {
Assert.assertEquals(message, pe.getMessage());
Assert.assertTrue(pe.isExit());
Assert.assertFalse(pe.isInternalError());
Assert.assertEquals(0, pe.getExitStatus());
Assert.assertNull(pe.getGuestObject());
});
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class TruffleExceptionTest method testTruffleException.
@Test
public void testTruffleException() {
setupEnv(createContext(verifyingHandler), new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
return createAST(AbstractTruffleException.class, languageInstance, (n) -> new TruffleExceptionImpl("Test exception", n), false);
}
});
verifyingHandler.expect(BlockNode.Kind.TRY, BlockNode.Kind.CATCH, BlockNode.Kind.FINALLY);
context.eval(ProxyLanguage.ID, "Test");
}
Aggregations