use of org.graalvm.polyglot.Value in project graal by oracle.
the class ExportValueTest method testToString.
@Test
public void testToString() {
bindings.putMember("tmp", 10);
Value value = bindings.getMember("tmp");
value.toString();
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class LanguageSPITest method testBindingsWithMultipleScopes.
@Test
public void testBindingsWithMultipleScopes() {
// innermost to outermost
TestScope[] scopes = new TestScope[5];
for (int i = 0; i < 5; i++) {
scopes[i] = new TestScope();
}
setupTopScopes((Object[]) scopes);
Context c = Context.create();
assertEquals(0, findScopeInvokes);
Value bindings = c.getBindings(ProxyLanguage.ID);
assertEquals(1, findScopeInvokes);
assertTrue(bindings.hasMembers());
assertFalse(bindings.hasMember(""));
assertNull(bindings.getMember(""));
ValueAssert.assertFails(() -> bindings.putMember("foo", "bar"), UnsupportedOperationException.class);
// test insertion into first insertable scope
scopes[1].insertable = true;
scopes[2].insertable = true;
// should end up in scope 1
bindings.putMember("foo", "bar");
assertEquals("bar", bindings.getMember("foo").asString());
assertEquals("bar", scopes[1].values.get("foo"));
assertNull(scopes[0].values.get("foo"));
assertNull(scopes[2].values.get("foo"));
ValueAssert.assertValue(c, bindings, ValueAssert.Trait.MEMBERS);
// test check for existing keys for remove
scopes[2].removable = true;
scopes[2].values.put("foo", "baz");
scopes[2].values.put("bar", "baz");
scopes[3].values.put("bar", "42");
assertEquals("bar", bindings.getMember("foo").asString());
assertEquals("baz", bindings.getMember("bar").asString());
ValueAssert.assertFails(() -> bindings.removeMember("foo"), UnsupportedOperationException.class);
assertTrue(bindings.removeMember("bar"));
assertNotNull(scopes[2].values.get("foo"));
assertNull(scopes[2].values.get("bar"));
assertEquals("42", bindings.getMember("bar").asString());
ValueAssert.assertValue(c, bindings, ValueAssert.Trait.MEMBERS);
c.close();
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class LanguageSPITest method testPolyglotBindingsMultiThreaded.
@Test
public void testPolyglotBindingsMultiThreaded() throws InterruptedException, ExecutionException, TimeoutException {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
return true;
}
@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();
ExecutorService service = Executors.newFixedThreadPool(20);
Value languageBindings = c.eval(ProxyLanguage.ID, "");
Value polyglotBindings = c.getPolyglotBindings();
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
futures.add(service.submit(() -> {
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());
}));
}
for (Future<?> future : futures) {
future.get(100000, TimeUnit.MILLISECONDS);
}
service.shutdown();
service.awaitTermination(100000, TimeUnit.MILLISECONDS);
c.close();
}
use of org.graalvm.polyglot.Value 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.Value 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