use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InstrumentableNodeTest method assertProperties.
private static void assertProperties(Object receiver, Object... properties) {
try {
assertTrue(receiver instanceof TruffleObject);
TruffleObject obj = (TruffleObject) receiver;
InteropLibrary interop = InteropLibrary.getFactory().getUncached();
assertTrue(interop.hasMembers(obj));
Object keys = interop.getMembers(obj);
for (int i = 0; i < properties.length; i = i + 2) {
String expectedKey = (String) properties[i];
Object expectedValue = properties[i + 1];
Object key = interop.readArrayElement(keys, i / 2);
assertEquals(expectedKey, key);
assertEquals(expectedValue, interop.readMember(obj, interop.asString(key)));
}
} catch (InteropException e) {
throw new AssertionError(e);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InstrumentationTest method testPolyglotBindings.
@Test
public void testPolyglotBindings() throws Exception {
Instrument bindingsTestInstrument = context.getEngine().getInstruments().get(BindingsTestInstrument.ID);
Object bindingsObject = bindingsTestInstrument.lookup(Supplier.class).get();
InteropLibrary interop = InteropLibrary.getUncached();
assertTrue(interop.hasMembers(bindingsObject));
assertFalse(interop.isNull(bindingsObject));
assertFalse(interop.isExecutable(bindingsObject));
assertFalse(interop.isInstantiable(bindingsObject));
final String m1 = "member1";
final String m2 = "member2";
// Bindings are empty initially
assertFalse(interop.isMemberExisting(bindingsObject, m1));
assertFalse(interop.isMemberExisting(bindingsObject, m2));
assertFalse(context.getPolyglotBindings().hasMember(m1));
assertFalse(context.getPolyglotBindings().hasMember(m2));
// Value set by Context can be read by instrument
context.getPolyglotBindings().putMember(m1, 10);
assertTrue(interop.isMemberExisting(bindingsObject, m1));
assertEquals(10, interop.readMember(bindingsObject, m1));
// Value set by instrument can be read by Context
interop.writeMember(bindingsObject, m1, 11);
interop.writeMember(bindingsObject, m2, 20);
assertEquals(11, context.getPolyglotBindings().getMember(m1).asInt());
assertEquals(20, context.getPolyglotBindings().getMember(m2).asInt());
// Remove works from both sides
interop.removeMember(bindingsObject, m1);
context.getPolyglotBindings().removeMember(m2);
assertFalse(interop.isMemberExisting(bindingsObject, m1));
assertFalse(interop.isMemberExisting(bindingsObject, m2));
assertFalse(context.getPolyglotBindings().hasMember(m1));
assertFalse(context.getPolyglotBindings().hasMember(m2));
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class ProxySPITest method testProxyObjectAndHostAccess.
/*
* Test for GR27558.
*/
@Test
public void testProxyObjectAndHostAccess() {
setupEnv(Context.newBuilder(ProxyLanguage.ID).allowPolyglotAccess(PolyglotAccess.ALL).allowHostAccess(HostAccess.NONE).build(), new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
String bindingMemberName = request.getSource().getCharacters().toString();
InteropLibrary interopLibrary = InteropLibrary.getUncached();
Object memberKeys = interopLibrary.getMembers(interopLibrary.readMember(languageEnv.getPolyglotBindings(), bindingMemberName));
return RootNode.createConstantNode(memberKeys).getCallTarget();
}
});
class MemberKeysProxy implements ProxyObject {
private Object memberKeys;
MemberKeysProxy(Object memberKeys) {
this.memberKeys = memberKeys;
}
@Override
public Object getMemberKeys() {
return memberKeys;
}
@Override
public boolean hasMember(String key) {
return true;
}
@Override
public Object getMember(String key) {
return null;
}
@Override
public void putMember(String key, Value value) {
}
}
context.initialize(ProxyLanguage.ID);
Object[] memberKeys = new Object[] { "one", "two" };
context.getPolyglotBindings().putMember("proxyArray", new MemberKeysProxy(memberKeys));
context.getPolyglotBindings().putMember("proxyList", new MemberKeysProxy(Arrays.asList(memberKeys)));
context.getPolyglotBindings().putMember("proxyProxyArray", new MemberKeysProxy(ProxyArray.fromArray(memberKeys)));
try {
context.eval(ProxyLanguage.ID, "proxyArray");
fail();
} catch (PolyglotException e) {
assertTrue(e.getMessage().contains("allowArrayAccess in HostAccess is false."));
}
try {
context.eval(ProxyLanguage.ID, "proxyList");
fail();
} catch (PolyglotException e) {
assertTrue(e.getMessage().contains("allowListAccess in HostAccess is false."));
}
Value proxyArrayMembers = context.eval(ProxyLanguage.ID, "proxyProxyArray");
assertTrue(proxyArrayMembers.hasArrayElements());
assertEquals(2, proxyArrayMembers.getArraySize());
assertEquals("two", proxyArrayMembers.getArrayElement(1).asString());
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method hasBufferElements.
@Test
public void hasBufferElements() {
for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
getContext().initialize(ProxyLanguage.ID);
final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
final RootNode node = new RootNode(null) {
@Child
InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);
@Override
public Object execute(VirtualFrame frame) {
return interop.hasBufferElements(bufferHostObject);
}
};
assertPartialEvalEquals(PolyglotHostObjectPartialEvaluationTest::constantTrue, node);
}
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class PolyglotHostObjectPartialEvaluationTest method getBufferSize.
@Test
public void getBufferSize() {
for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
getContext().initialize(ProxyLanguage.ID);
final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
final RootNode node = new RootNode(null) {
@Child
InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);
@Override
public Object execute(VirtualFrame frame) {
try {
return interop.getBufferSize(bufferHostObject);
} catch (UnsupportedMessageException e) {
throw new RuntimeException(e);
}
}
};
assertPartialEvalNoInvokes(node);
}
}
Aggregations