Search in sources :

Example 1 with UnknownIdentifierException

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());
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 2 with UnknownIdentifierException

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);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) Test(org.junit.Test)

Example 3 with UnknownIdentifierException

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) {
    }
}
Also used : UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 4 with UnknownIdentifierException

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);
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 5 with UnknownIdentifierException

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());
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) NoSuchElementException(java.util.NoSuchElementException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test) ValueHostInteropTest(com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)

Aggregations

UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)11 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)8 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)6 Test (org.junit.Test)6 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)4 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)3 Specialization (com.oracle.truffle.api.dsl.Specialization)2 CallTarget (com.oracle.truffle.api.CallTarget)1 RootCallTarget (com.oracle.truffle.api.RootCallTarget)1 TruffleContext (com.oracle.truffle.api.TruffleContext)1 TruffleException (com.oracle.truffle.api.TruffleException)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 ArityException (com.oracle.truffle.api.interop.ArityException)1 InteropException (com.oracle.truffle.api.interop.InteropException)1 ExplodeLoop (com.oracle.truffle.api.nodes.ExplodeLoop)1 Node (com.oracle.truffle.api.nodes.Node)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)1 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)1 LLVMContext (com.oracle.truffle.llvm.runtime.LLVMContext)1