use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.
the class LanguageSPITest method testPolyglotBindingsPreserveLanguage.
@Test
public void testPolyglotBindingsPreserveLanguage() {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return Truffle.getRuntime().createCallTarget(new RootNode(languageInstance) {
@Override
public Object execute(VirtualFrame frame) {
Object bindings = getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
try {
ForeignAccess.sendWrite(Message.WRITE.createNode(), (TruffleObject) bindings, "exportedValue", "convertOnToString");
} catch (UnknownIdentifierException | UnsupportedTypeException | UnsupportedMessageException e) {
throw new AssertionError(e);
}
return bindings;
}
});
}
@Override
protected String toString(LanguageContext context, Object value) {
if (value.equals("convertOnToString")) {
return "myStringToString";
}
return super.toString(context, value);
}
});
Context c = Context.create();
c.eval(ProxyLanguage.ID, "");
assertEquals("Make sure language specific toString was invoked.", "myStringToString", c.getPolyglotBindings().getMember("exportedValue").toString());
}
use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.
the class LanguageSPITest method testExportSymbolInCreate.
@SuppressWarnings("deprecation")
@Test
public void testExportSymbolInCreate() {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected LanguageContext createContext(com.oracle.truffle.api.TruffleLanguage.Env env) {
env.exportSymbol("symbol", env.asGuestValue(env));
return super.createContext(env);
}
});
Context c = Context.create();
c.initialize(ProxyLanguage.ID);
assertTrue(c.importSymbol("symbol").isHostObject());
c.close();
}
use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.
the class LanguageSPITest method testErrorInFindMetaObject.
@Test
public void testErrorInFindMetaObject() {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected Object findMetaObject(LanguageContext context, Object value) {
throw new RuntimeException();
}
@Override
protected CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest request) throws Exception {
return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(42));
}
});
Context c = Context.create();
Value v = c.eval(ProxyLanguage.ID, "");
testFails(() -> v.getMetaObject());
testFails(() -> v.getMetaObject());
c.close();
}
use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.
the class LanguageSPITest method testCloseInnerContextWithParent.
@Test
public void testCloseInnerContextWithParent() {
Context context = Context.create();
LanguageContext returnedInnerContext = eval(context, new Function<Env, Object>() {
public Object apply(Env env) {
TruffleContext innerContext = env.newContextBuilder().build();
Object p = innerContext.enter();
LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
innerContext.leave(p);
return innerLangContext;
}
}).asHostObject();
context.close();
// inner context automatically closed
assertEquals(1, returnedInnerContext.disposeCalled);
}
use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.
the class LanguageSPITest method testCreateThreadDuringDispose.
@Test
public void testCreateThreadDuringDispose() {
AtomicBoolean contextOnFinalize = new AtomicBoolean(true);
AtomicBoolean contextOnDispose = new AtomicBoolean(false);
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected LanguageContext createContext(Env env) {
return new LanguageContext(env);
}
@Override
protected void finalizeContext(LanguageContext context) {
if (contextOnFinalize.get()) {
context.env.createThread(() -> {
}).start();
}
}
@Override
protected void disposeContext(LanguageContext context) {
if (contextOnDispose.get()) {
context.env.createThread(() -> {
}).start();
}
}
});
Context c = Context.create();
c.initialize(ProxyLanguage.ID);
// Fails on finalize
testFails(() -> {
c.close();
});
contextOnFinalize.set(false);
contextOnDispose.set(true);
// Fails on dispose
testFails(() -> {
c.close();
});
// clean up the context
contextOnDispose.set(false);
c.close();
}
Aggregations