Search in sources :

Example 1 with Proxy

use of org.graalvm.polyglot.proxy.Proxy in project graal by oracle.

the class ProxyAPITest method testProxy.

@Test
public void testProxy() {
    Proxy proxy = new Proxy() {
    };
    Value value = context.asValue(proxy);
    assertTrue(value.isProxyObject());
    assertSame(proxy, value.asProxyObject());
    assertUnsupported(value, PROXY_OBJECT);
}
Also used : Proxy(org.graalvm.polyglot.proxy.Proxy) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 2 with Proxy

use of org.graalvm.polyglot.proxy.Proxy in project graal by oracle.

the class ProxySPITest method testSimpleProxy.

@Test
public void testSimpleProxy() throws Throwable {
    Context context = Context.create();
    Proxy proxyOuter = new Proxy() {
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertUnsupported(Message.GET_SIZE, proxyInner);
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner);
        assertUnsupported(Message.WRITE, proxyInner);
        assertUnsupported(Message.REMOVE, proxyInner);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.UNBOX, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertUnsupported(Message.createExecute(0), proxyInner);
        assertUnsupported(Message.createNew(0), proxyInner);
        assertEquals(false, Message.IS_BOXED, proxyInner);
        assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(false, Message.HAS_KEYS, proxyInner);
        assertEquals(false, Message.HAS_SIZE, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Proxy(org.graalvm.polyglot.proxy.Proxy) Test(org.junit.Test)

Example 3 with Proxy

use of org.graalvm.polyglot.proxy.Proxy in project graal by oracle.

the class ProxySPITest method testProxyError.

@Test
public void testProxyError() throws Throwable {
    Context context = Context.create();
    Proxy proxyOuter = new AllProxy();
    eval(context, proxyOuter, (proxyInner) -> {
        assertHostError(Message.AS_POINTER, proxyInner);
        assertHostError(Message.GET_SIZE, proxyInner);
        assertHostError(Message.KEYS, proxyInner);
        assertHostError(Message.READ, proxyInner, "");
        assertHostError(Message.READ, proxyInner, 42);
        assertHostError(Message.WRITE, proxyInner, "", 42);
        assertHostError(Message.WRITE, proxyInner, 42, 42);
        assertHostError(Message.REMOVE, proxyInner, 10);
        assertHostError(Message.UNBOX, proxyInner);
        assertHostError(Message.createInvoke(0), proxyInner, "");
        assertHostError(Message.createExecute(0), proxyInner);
        assertHostError(Message.createNew(0), proxyInner);
        assertHostError(Message.KEY_INFO, proxyInner, "");
        assertHostError(Message.KEY_INFO, proxyInner, 42);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertEquals(true, Message.IS_BOXED, proxyInner);
        assertEquals(true, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(true, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(true, Message.HAS_KEYS, proxyInner);
        assertEquals(true, Message.HAS_SIZE, proxyInner);
        assertEquals(true, Message.IS_POINTER, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Proxy(org.graalvm.polyglot.proxy.Proxy) Test(org.junit.Test)

Example 4 with Proxy

use of org.graalvm.polyglot.proxy.Proxy in project graal by oracle.

the class ValueHostConversionTest method testProxyIdentityRestore.

@Test
public void testProxyIdentityRestore() {
    Proxy proxy = new Proxy() {
    };
    Value value = context.asValue(proxy);
    assertUnsupported(value, PROXY_OBJECT);
    assertSame(proxy, context.asValue(value.as(Object.class)).asProxyObject());
}
Also used : Proxy(org.graalvm.polyglot.proxy.Proxy) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 5 with Proxy

use of org.graalvm.polyglot.proxy.Proxy in project graal by oracle.

the class HostLanguage method findMetaObject.

@Override
protected Object findMetaObject(HostContext context, Object value) {
    if (value instanceof TruffleObject) {
        TruffleObject to = (TruffleObject) value;
        if (VMAccessor.JAVAINTEROP.isHostObject(to)) {
            Object javaObject = VMAccessor.JAVAINTEROP.asHostObject(to);
            Class<?> javaType;
            if (javaObject == null) {
                javaType = Void.class;
            } else {
                javaType = javaObject.getClass();
            }
            return javaClassToGuestObject(javaType, context.internalContext);
        } else if (PolyglotProxy.isProxyGuestObject(to)) {
            Proxy proxy = PolyglotProxy.toProxyHostObject(to);
            return javaClassToGuestObject(proxy.getClass(), context.internalContext);
        } else if (VMAccessor.JAVAINTEROP.isHostFunction(value)) {
            return "Bound Method";
        } else {
            return "Foreign Object";
        }
    } else {
        return javaClassToGuestObject(value.getClass(), context.internalContext);
    }
}
Also used : Proxy(org.graalvm.polyglot.proxy.Proxy) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

Proxy (org.graalvm.polyglot.proxy.Proxy)6 Test (org.junit.Test)4 Value (org.graalvm.polyglot.Value)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)2 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)2 Context (org.graalvm.polyglot.Context)2