use of org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testImplicitCloseFromOtherThread.
@Test
public void testImplicitCloseFromOtherThread() throws InterruptedException {
Engine engine = Engine.create();
Context context = Context.newBuilder().engine(engine).build();
langContext = null;
context.initialize(LanguageSPITestLanguage.ID);
Thread t = new Thread(new Runnable() {
public void run() {
engine.close();
}
});
t.start();
t.join(10000);
assertEquals(1, langContext.disposeCalled);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testLazyOptionInit.
@Test
@SuppressWarnings("all")
public void testLazyOptionInit() {
AtomicInteger getOptionDescriptors = new AtomicInteger(0);
AtomicInteger iterator = new AtomicInteger(0);
AtomicInteger get = new AtomicInteger(0);
ProxyLanguage.setDelegate(new ProxyLanguage() {
final OptionKey<String> option = new OptionKey<>("");
final List<OptionDescriptor> descriptors;
{
descriptors = new ArrayList<>();
descriptors.add(OptionDescriptor.newBuilder(option, ProxyLanguage.ID + ".option").build());
}
@Override
protected OptionDescriptors getOptionDescriptors() {
getOptionDescriptors.incrementAndGet();
return new OptionDescriptors() {
public Iterator<OptionDescriptor> iterator() {
iterator.incrementAndGet();
return descriptors.iterator();
}
public OptionDescriptor get(String optionName) {
get.incrementAndGet();
for (OptionDescriptor optionDescriptor : descriptors) {
if (optionDescriptor.getName().equals(optionName)) {
return optionDescriptor;
}
}
return null;
}
};
}
});
Context c = Context.create();
c.close();
assertEquals(0, getOptionDescriptors.get());
assertEquals(0, iterator.get());
assertEquals(0, get.get());
c = Context.newBuilder(ProxyLanguage.ID).option(ProxyLanguage.ID + ".option", "foobar").build();
assertEquals(1, getOptionDescriptors.get());
assertEquals(1, get.get());
boolean assertionsEnabled = false;
assert assertionsEnabled = true;
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(1, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(0, iterator.get());
}
c.close();
// test lazyness when using meta-data
getOptionDescriptors.set(0);
iterator.set(0);
get.set(0);
c = Context.create();
OptionDescriptors descriptors = c.getEngine().getLanguages().get(ProxyLanguage.ID).getOptions();
assertEquals(1, getOptionDescriptors.get());
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(1, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(0, iterator.get());
}
assertEquals(0, get.get());
for (OptionDescriptor descriptor : descriptors) {
}
assertEquals(1, getOptionDescriptors.get());
if (assertionsEnabled) {
// with assertions enabled we assert the options.
assertEquals(2, iterator.get());
} else {
// for valid options we should not need the iterator
assertEquals(1, iterator.get());
}
assertEquals(0, get.get());
assertNotNull(descriptors.get(ProxyLanguage.ID + ".option"));
assertEquals(1, getOptionDescriptors.get());
assertEquals(1, get.get());
c.close();
}
use of org.graalvm.polyglot.Context 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 org.graalvm.polyglot.Context in project graal by oracle.
the class LanguageSPITest method testBindingsWithDefaultScope.
@Test
public void testBindingsWithDefaultScope() {
Context c = Context.create();
Value bindings = c.getBindings(ProxyLanguage.ID);
assertTrue(bindings.hasMembers());
assertFalse(bindings.hasMember(""));
assertTrue(bindings.getMemberKeys().isEmpty());
assertNull(bindings.getMember(""));
ValueAssert.assertFails(() -> bindings.putMember("", ""), UnsupportedOperationException.class);
assertFalse(bindings.removeMember(""));
ValueAssert.assertValue(c, bindings);
c.close();
}
use of org.graalvm.polyglot.Context 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();
}
Aggregations