use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testProxyPrimitive.
@Test
@SuppressWarnings("deprecation")
public void testProxyPrimitive() throws Throwable {
Context context = Context.create();
org.graalvm.polyglot.proxy.ProxyPrimitive proxyOuter = new org.graalvm.polyglot.proxy.ProxyPrimitive() {
public Object asPrimitive() {
return 42;
}
};
eval(context, proxyOuter, (proxyInner) -> {
assertEquals(true, Message.IS_BOXED, proxyInner);
assertEquals(42, Message.UNBOX, proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.AS_POINTER, proxyInner);
assertUnsupported(Message.GET_SIZE, proxyInner);
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner);
assertUnsupported(Message.WRITE, proxyInner);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.createNew(0), proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(false, Message.HAS_KEYS, proxyInner);
assertEquals(false, Message.HAS_SIZE, proxyInner);
assertEquals(false, Message.IS_POINTER, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testProxyObjectUnsupported.
@Test
public void testProxyObjectUnsupported() throws Throwable {
Context context = Context.create();
ProxyObject proxyOuter = new ProxyObject() {
public void putMember(String key, Value value) {
throw new UnsupportedOperationException();
}
public boolean hasMember(String key) {
return true;
}
public ProxyArray getMemberKeys() {
return null;
}
public Object getMember(String key) {
throw new UnsupportedOperationException();
}
};
eval(context, proxyOuter, (proxyInner) -> {
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner, "");
assertUnsupported(Message.WRITE, proxyInner, "", 42);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.GET_SIZE, proxyInner);
assertUnsupported(Message.READ, proxyInner, 0);
assertUnsupported(Message.WRITE, proxyInner, 1);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.AS_POINTER, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.createNew(0), proxyInner);
assertEquals(false, Message.IS_BOXED, proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(true, Message.HAS_KEYS, proxyInner);
assertEquals(false, Message.HAS_SIZE, proxyInner);
assertEquals(false, Message.IS_POINTER, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testProxyError.
@Test
public void testProxyError() throws Throwable {
Context context = Context.create();
Proxy proxyOuter = new AllProxy();
eval(context, proxyOuter, (proxyInner) -> {
assertHostError(Message.AS_POINTER, proxyInner);
assertHostError(Message.GET_SIZE, proxyInner);
assertHostError(Message.KEYS, proxyInner);
assertHostError(Message.READ, proxyInner, "");
assertHostError(Message.READ, proxyInner, 42);
assertHostError(Message.WRITE, proxyInner, "", 42);
assertHostError(Message.WRITE, proxyInner, 42, 42);
assertHostError(Message.REMOVE, proxyInner, 10);
assertHostError(Message.UNBOX, proxyInner);
assertHostError(Message.createInvoke(0), proxyInner, "");
assertHostError(Message.createExecute(0), proxyInner);
assertHostError(Message.createNew(0), proxyInner);
assertHostError(Message.KEY_INFO, proxyInner, "");
assertHostError(Message.KEY_INFO, proxyInner, 42);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertEquals(true, Message.IS_BOXED, proxyInner);
assertEquals(true, Message.IS_EXECUTABLE, proxyInner);
assertEquals(true, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(true, Message.HAS_KEYS, proxyInner);
assertEquals(true, Message.HAS_SIZE, proxyInner);
assertEquals(true, Message.IS_POINTER, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testProxyInstantiable.
@Test
public void testProxyInstantiable() throws Throwable {
Context context = Context.create();
ProxyInstantiable proxyOuter = new ProxyInstantiable() {
@Override
public Object newInstance(Value... t) {
return t[0].newInstance();
}
};
eval(context, proxyOuter, (proxyInner) -> {
assertEquals(true, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner, 42);
assertUnsupported(Message.AS_POINTER, proxyInner);
assertUnsupported(Message.GET_SIZE, proxyInner);
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner);
assertUnsupported(Message.WRITE, proxyInner);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertEquals(false, Message.IS_BOXED, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(false, Message.HAS_KEYS, proxyInner);
assertEquals(false, Message.HAS_SIZE, proxyInner);
assertEquals(false, Message.IS_POINTER, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SourceAPITest method testCharSequenceNotMaterialized.
@Test
public void testCharSequenceNotMaterialized() throws IOException {
AtomicBoolean materialized = new AtomicBoolean(false);
final CharSequence testString = "testString";
Source source = Source.newBuilder(SourceAPITestLanguage.ID, new CharSequence() {
public CharSequence subSequence(int start, int end) {
return testString.subSequence(start, end);
}
public int length() {
return testString.length();
}
public char charAt(int index) {
return testString.charAt(index);
}
@Override
public String toString() {
materialized.set(true);
throw new AssertionError("Should not materialize CharSequence.");
}
}, "testsource").buildLiteral();
Context context = Context.create(SourceAPITestLanguage.ID);
context.eval(source);
assertEquals(1, source.getLineCount());
assertTrue(equalsCharSequence(testString, source.getCharacters()));
assertTrue(equalsCharSequence(testString, source.getCharacters(1)));
assertEquals(0, source.getLineStartOffset(1));
assertNull(source.getURL());
assertNotNull(source.getName());
assertNull(source.getPath());
assertEquals(6, source.getColumnNumber(5));
assertEquals(SourceAPITestLanguage.ID, source.getLanguage());
assertEquals(testString.length(), source.getLength());
assertFalse(source.isInteractive());
assertFalse(source.isInternal());
// consume reader CharSequence should not be materialized
CharBuffer charBuffer = CharBuffer.allocate(source.getLength());
Reader reader = source.getReader();
reader.read(charBuffer);
charBuffer.position(0);
assertEquals(testString, charBuffer.toString());
assertFalse(materialized.get());
}
Aggregations