use of org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testEngineCloseInsideFromSameThreadCancelExecution.
@Test
public void testEngineCloseInsideFromSameThreadCancelExecution() {
Engine engine = Engine.create();
langContext = null;
Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
eval(context, new Function<Env, Object>() {
public Object apply(Env t) {
engine.close(true);
return null;
}
});
assertEquals(1, langContext.disposeCalled);
engine.close();
}
use of org.graalvm.polyglot.Context 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 org.graalvm.polyglot.Context 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();
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testContextCloseInsideFromSameThreadCancelExecution.
@Test
public void testContextCloseInsideFromSameThreadCancelExecution() {
Engine engine = Engine.create();
langContext = null;
Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
eval(context, new Function<Env, Object>() {
public Object apply(Env t) {
context.close(true);
return null;
}
});
engine.close();
assertEquals(1, langContext.disposeCalled);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testPolyglotBindings.
@Test
public void testPolyglotBindings() {
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) {
return getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
}
});
}
});
Context c = Context.create();
Value languageBindings = c.eval(ProxyLanguage.ID, "");
Value polyglotBindings = c.getPolyglotBindings();
polyglotBindings.putMember("foo", "bar");
assertEquals("bar", polyglotBindings.getMember("foo").asString());
assertEquals("bar", languageBindings.getMember("foo").asString());
languageBindings.putMember("baz", "42");
assertEquals("42", polyglotBindings.getMember("baz").asString());
assertEquals("42", languageBindings.getMember("baz").asString());
ValueAssert.assertValue(c, polyglotBindings);
ValueAssert.assertValue(c, languageBindings);
c.close();
}
Aggregations