Search in sources :

Example 11 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject 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 12 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject 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 13 with TruffleObject

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

the class ValueHostInteropTest method executableAsFunctionalInterface1.

@Test
public void executableAsFunctionalInterface1() throws Exception {
    TruffleObject executable = new FunctionObject();
    FunctionalWithDefaults f = context.asValue(executable).as(FunctionalWithDefaults.class);
    assertEquals(50, f.call((Object) 13, (Object) 37));
    f.hashCode();
    f.equals(null);
    f.toString();
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 14 with TruffleObject

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

the class ValueHostInteropTest method executableAsFunctionalInterface3.

@Ignore("Interface not accessible")
@Test
public void executableAsFunctionalInterface3() throws Exception {
    assumeTrue("JDK 9 or later", System.getProperty("java.specification.version").compareTo("1.9") >= 0);
    TruffleObject executable = new FunctionObject();
    FunctionalWithDefaults f = context.asValue(executable).as(FunctionalWithDefaults.class);
    assertEquals(42, f.call((Object) 13, (Object) 29));
    assertEquals(50, f.call(13, 37));
    f.hashCode();
    f.equals(null);
    f.toString();
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with TruffleObject

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

the class ConvertAndClassCastTest method convertObjToIntThrowsClassCastException.

@Test(expected = ClassCastException.class)
public void convertObjToIntThrowsClassCastException() {
    Object obj = new Object();
    TruffleObject truffleObj = JavaInterop.asTruffleObject(obj);
    PolyglotEngine eng = PolyglotEngine.newBuilder().globalSymbol("value", truffleObj).build();
    Integer v = eng.findGlobalSymbol("value").as(Integer.class);
    fail("No value, but exception: " + v);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)201 Test (org.junit.Test)135 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)34 InteropException (com.oracle.truffle.api.interop.InteropException)18 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)17 Specialization (com.oracle.truffle.api.dsl.Specialization)16 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)14 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)12 ArrayTruffleObject (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest.ArrayTruffleObject)10 Node (com.oracle.truffle.api.nodes.Node)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 CallTarget (com.oracle.truffle.api.CallTarget)7 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)7 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)7 LinkedHashMap (java.util.LinkedHashMap)7 Source (com.oracle.truffle.api.source.Source)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5