Search in sources :

Example 1 with InteropException

use of com.oracle.truffle.api.interop.InteropException in project graal by oracle.

the class ProxySPITest method assertEmpty.

private static void assertEmpty(Message message, TruffleObject proxyInner) {
    try {
        TruffleObject values = (TruffleObject) ForeignAccess.send(message.createNode(), proxyInner);
        Assert.assertEquals(true, ForeignAccess.sendHasSize(Message.HAS_SIZE.createNode(), values));
        Assert.assertEquals(0, ((Number) ForeignAccess.sendGetSize(Message.GET_SIZE.createNode(), values)).intValue());
    } catch (InteropException e) {
        Assert.fail();
    }
}
Also used : InteropException(com.oracle.truffle.api.interop.InteropException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 2 with InteropException

use of com.oracle.truffle.api.interop.InteropException in project graal by oracle.

the class NumericNFITest method testPingPong.

@Test
public void testPingPong(@Inject(TestPingPongNode.class) CallTarget callTarget) {
    TruffleObject wrap = new TestCallback(1, (args) -> {
        Assert.assertThat("argument", args[0], is(instanceOf(TruffleObject.class)));
        TruffleObject fn = (TruffleObject) args[0];
        TruffleObject wrapped = new TestCallback(1, (innerArgs) -> {
            checkExpectedArg(6, innerArgs[0]);
            try {
                return ForeignAccess.sendExecute(Message.createExecute(1).createNode(), fn, unboxNumber(innerArgs[0]) * 3);
            } catch (InteropException ex) {
                throw new AssertionError(ex);
            }
        });
        return wrapped;
    });
    Object ret = callTarget.call(wrap, 5);
    checkExpectedRet(38, ret);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 3 with InteropException

use of com.oracle.truffle.api.interop.InteropException in project graal by oracle.

the class ObjectNFITest method initEnv.

@BeforeClass
public static void initEnv() {
    TruffleObject createNewObject = new TestCallback(0, (args) -> new TestObject());
    TruffleObject readIntField = new TestCallback(2, (args) -> {
        Assert.assertThat("args[0]", args[0], is(instanceOf(TestObject.class)));
        Assert.assertThat("args[1]", args[1], is(instanceOf(String.class)));
        return ((TestObject) args[0]).readField((String) args[1]);
    });
    TruffleObject writeIntField = new TestCallback(3, (args) -> {
        Assert.assertThat("args[0]", args[0], is(instanceOf(TestObject.class)));
        Assert.assertThat("args[1]", args[1], is(instanceOf(String.class)));
        Assert.assertThat("args[2]", args[2], is(instanceOf(Integer.class)));
        ((TestObject) args[0]).writeField((String) args[1], (Integer) args[2]);
        return null;
    });
    TruffleObject initializeAPI = lookupAndBind("initialize_api", "( env, ():object, (object,string):sint32, (object,string,sint32):void ) : pointer");
    try {
        nativeAPI = (TruffleObject) ForeignAccess.sendExecute(Message.createExecute(3).createNode(), initializeAPI, createNewObject, readIntField, writeIntField);
    } catch (InteropException ex) {
        throw new AssertionError(ex);
    }
}
Also used : InteropException(com.oracle.truffle.api.interop.InteropException) TestCallback(com.oracle.truffle.nfi.test.interop.TestCallback) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) BeforeClass(org.junit.BeforeClass)

Example 4 with InteropException

use of com.oracle.truffle.api.interop.InteropException in project graal by oracle.

the class AsCollectionsTest method testInvokeListOfMapProxy.

@Test
public void testInvokeListOfMapProxy() throws InteropException {
    TruffleObject validator = com.oracle.truffle.api.interop.java.JavaInterop.asTruffleObject(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ProxyValidator.class }, (p, method, args) -> {
        List<Map> listOfMap = (List<Map>) args[0];
        assertEquals(1, listOfMap.size());
        Map map = listOfMap.get(0);
        assertEquals(1, map.size());
        assertEquals("bar", map.get("foo"));
        return true;
    }));
    MapBasedTO mapTO = new MapBasedTO(Collections.singletonMap("foo", "bar"));
    TruffleObject listOfMapTO = new ListBasedTO(Arrays.asList(mapTO));
    assertEquals(Boolean.TRUE, ForeignAccess.sendInvoke(Message.createInvoke(1).createNode(), validator, "test", listOfMapTO));
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) InteropException(com.oracle.truffle.api.interop.InteropException) ForeignAccess(com.oracle.truffle.api.interop.ForeignAccess) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) LinkedHashMap(java.util.LinkedHashMap) Resolve(com.oracle.truffle.api.interop.Resolve) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) Map(java.util.Map) After(org.junit.After) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Node(com.oracle.truffle.api.nodes.Node) MessageResolution(com.oracle.truffle.api.interop.MessageResolution) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Message(com.oracle.truffle.api.interop.Message) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 5 with InteropException

use of com.oracle.truffle.api.interop.InteropException in project graal by oracle.

the class PolyglotLanguageContext method lookupGuest.

Object lookupGuest(String symbolName) {
    ensureInitialized(null);
    Iterable<?> topScopes = VMAccessor.instrumentAccess().findTopScopes(env);
    for (Object topScope : topScopes) {
        Scope scope = (Scope) topScope;
        TruffleObject variables = (TruffleObject) scope.getVariables();
        int symbolInfo = ForeignAccess.sendKeyInfo(keyInfoNode, variables, symbolName);
        if (KeyInfo.isExisting(symbolInfo) && KeyInfo.isReadable(symbolInfo)) {
            try {
                return ForeignAccess.sendRead(readNode, variables, symbolName);
            } catch (InteropException ex) {
                throw new AssertionError(symbolName, ex);
            }
        }
    }
    return null;
}
Also used : Scope(com.oracle.truffle.api.Scope) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

InteropException (com.oracle.truffle.api.interop.InteropException)14 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)14 Test (org.junit.Test)4 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)3 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 Specialization (com.oracle.truffle.api.dsl.Specialization)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)2 Node (com.oracle.truffle.api.nodes.Node)2 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)2 LLVMContext (com.oracle.truffle.llvm.runtime.LLVMContext)2 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)2 NoSuchElementException (java.util.NoSuchElementException)2 BeforeClass (org.junit.BeforeClass)2 Scope (com.oracle.truffle.api.Scope)1 ExpressionNode (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.ExpressionNode)1 MaterializeChildExpressionNode (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializeChildExpressionNode)1 MaterializedChildExpressionNode (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializedChildExpressionNode)1 ForeignAccess (com.oracle.truffle.api.interop.ForeignAccess)1 Message (com.oracle.truffle.api.interop.Message)1