use of com.oracle.truffle.api.interop.UnknownIdentifierException in project graal by oracle.
the class LanguageSPITest method testPolyglotBindingsPreserveLanguage.
@Test
public void testPolyglotBindingsPreserveLanguage() {
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) {
Object bindings = getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
try {
ForeignAccess.sendWrite(Message.WRITE.createNode(), (TruffleObject) bindings, "exportedValue", "convertOnToString");
} catch (UnknownIdentifierException | UnsupportedTypeException | UnsupportedMessageException e) {
throw new AssertionError(e);
}
return bindings;
}
});
}
@Override
protected String toString(LanguageContext context, Object value) {
if (value.equals("convertOnToString")) {
return "myStringToString";
}
return super.toString(context, value);
}
});
Context c = Context.create();
c.eval(ProxyLanguage.ID, "");
assertEquals("Make sure language specific toString was invoked.", "myStringToString", c.getPolyglotBindings().getMember("exportedValue").toString());
}
use of com.oracle.truffle.api.interop.UnknownIdentifierException in project graal by oracle.
the class TestMemberAccess method testFields.
@Test
public void testFields() throws IllegalAccessException, InteropException {
TestClass t = new TestClass();
Field[] fields = t.getClass().getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
String name = f.getName();
if (name.startsWith("field")) {
boolean isPublic = (f.getModifiers() & Modifier.PUBLIC) != 0;
boolean wasUIE = false;
try {
testForValue(name, f.get(t));
} catch (UnknownIdentifierException e) {
if (isPublic) {
throw e;
}
wasUIE = true;
}
if (!isPublic && !wasUIE) {
fail("expected UnknownIdentifierException when accessing field: " + name);
}
}
}
}
use of com.oracle.truffle.api.interop.UnknownIdentifierException in project graal by oracle.
the class TestMemberAccess method testArrayOutOfBoundsAccess.
@Test
public void testArrayOutOfBoundsAccess() throws InteropException {
Object[] array = new Object[1];
TruffleObject arrayObject = JavaInterop.asTruffleObject(array);
assertTrue(JavaInteropTest.isArray(arrayObject));
ForeignAccess.sendRead(readNode, arrayObject, 0);
try {
ForeignAccess.sendRead(readNode, arrayObject, 1);
fail();
} catch (UnknownIdentifierException e) {
}
}
use of com.oracle.truffle.api.interop.UnknownIdentifierException in project sulong by graalvm.
the class LLVMTruffleReadNString method interop.
@Specialization
protected Object interop(LLVMTruffleObject objectWithOffset, int n, @Cached("createForeignReadNode()") Node foreignRead, @Cached("createToByteNode()") ForeignToLLVM toLLVM) {
long offset = objectWithOffset.getOffset();
TruffleObject object = objectWithOffset.getObject();
char[] chars = new char[n];
for (int i = 0; i < n; i++) {
Object rawValue;
try {
rawValue = ForeignAccess.sendRead(foreignRead, object, offset + i);
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException(e);
}
byte byteValue = (byte) toLLVM.executeWithTarget(rawValue);
chars[i] = (char) Byte.toUnsignedInt(byteValue);
}
return new String(chars);
}
use of com.oracle.truffle.api.interop.UnknownIdentifierException in project graal by oracle.
the class JavaInteropTest method testRemoveMessage.
@Test
public void testRemoveMessage() {
data.arr = new String[] { "Hello", "World", "!" };
TruffleObject truffleList = JavaInterop.asTruffleObject(new ArrayList<>(Arrays.asList(data.arr)));
assertEquals(3, message(Message.GET_SIZE, truffleList));
assertEquals(true, message(Message.REMOVE, truffleList, 1));
assertEquals(2, message(Message.GET_SIZE, truffleList));
try {
message(Message.REMOVE, truffleList, 10);
fail("Out of bounds.");
} catch (Exception e) {
assertTrue(e.toString(), e instanceof UnknownIdentifierException);
assertEquals("10", ((UnknownIdentifierException) e).getUnknownIdentifier());
}
Object arrObj = message(Message.READ, obj, "arr");
TruffleObject truffleArr = (TruffleObject) arrObj;
try {
message(Message.REMOVE, truffleArr, 0);
fail("Remove of elements of an array is not supported.");
} catch (Exception e) {
assertTrue(e.toString(), e instanceof UnsupportedMessageException);
assertEquals(Message.REMOVE, ((UnsupportedMessageException) e).getUnsupportedMessage());
}
Map<String, String> map = new HashMap<>();
map.put("a", "aa");
map.put("b", "bb");
TruffleObject truffleMap = JavaInterop.asTruffleObject(map);
assertEquals(true, message(Message.REMOVE, truffleMap, "a"));
assertEquals(1, map.size());
try {
message(Message.REMOVE, truffleMap, "a");
fail("UnknownIdentifierException");
} catch (Exception e) {
assertTrue(e.toString(), e instanceof UnknownIdentifierException);
assertEquals("a", ((UnknownIdentifierException) e).getUnknownIdentifier());
}
}
Aggregations