Search in sources :

Example 6 with InteropLibrary

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

the class InstrumentableNodeTest method assertProperties.

private static void assertProperties(Object receiver, Object... properties) {
    try {
        assertTrue(receiver instanceof TruffleObject);
        TruffleObject obj = (TruffleObject) receiver;
        InteropLibrary interop = InteropLibrary.getFactory().getUncached();
        assertTrue(interop.hasMembers(obj));
        Object keys = interop.getMembers(obj);
        for (int i = 0; i < properties.length; i = i + 2) {
            String expectedKey = (String) properties[i];
            Object expectedValue = properties[i + 1];
            Object key = interop.readArrayElement(keys, i / 2);
            assertEquals(expectedKey, key);
            assertEquals(expectedValue, interop.readMember(obj, interop.asString(key)));
        }
    } catch (InteropException e) {
        throw new AssertionError(e);
    }
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 7 with InteropLibrary

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

the class InstrumentationTest method testPolyglotBindings.

@Test
public void testPolyglotBindings() throws Exception {
    Instrument bindingsTestInstrument = context.getEngine().getInstruments().get(BindingsTestInstrument.ID);
    Object bindingsObject = bindingsTestInstrument.lookup(Supplier.class).get();
    InteropLibrary interop = InteropLibrary.getUncached();
    assertTrue(interop.hasMembers(bindingsObject));
    assertFalse(interop.isNull(bindingsObject));
    assertFalse(interop.isExecutable(bindingsObject));
    assertFalse(interop.isInstantiable(bindingsObject));
    final String m1 = "member1";
    final String m2 = "member2";
    // Bindings are empty initially
    assertFalse(interop.isMemberExisting(bindingsObject, m1));
    assertFalse(interop.isMemberExisting(bindingsObject, m2));
    assertFalse(context.getPolyglotBindings().hasMember(m1));
    assertFalse(context.getPolyglotBindings().hasMember(m2));
    // Value set by Context can be read by instrument
    context.getPolyglotBindings().putMember(m1, 10);
    assertTrue(interop.isMemberExisting(bindingsObject, m1));
    assertEquals(10, interop.readMember(bindingsObject, m1));
    // Value set by instrument can be read by Context
    interop.writeMember(bindingsObject, m1, 11);
    interop.writeMember(bindingsObject, m2, 20);
    assertEquals(11, context.getPolyglotBindings().getMember(m1).asInt());
    assertEquals(20, context.getPolyglotBindings().getMember(m2).asInt());
    // Remove works from both sides
    interop.removeMember(bindingsObject, m1);
    context.getPolyglotBindings().removeMember(m2);
    assertFalse(interop.isMemberExisting(bindingsObject, m1));
    assertFalse(interop.isMemberExisting(bindingsObject, m2));
    assertFalse(context.getPolyglotBindings().hasMember(m1));
    assertFalse(context.getPolyglotBindings().hasMember(m2));
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Instrument(org.graalvm.polyglot.Instrument) ProxyInstrument(com.oracle.truffle.api.test.polyglot.ProxyInstrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Supplier(java.util.function.Supplier) Test(org.junit.Test)

Example 8 with InteropLibrary

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

the class ProxySPITest method testProxyObjectAndHostAccess.

/*
     * Test for GR27558.
     */
@Test
public void testProxyObjectAndHostAccess() {
    setupEnv(Context.newBuilder(ProxyLanguage.ID).allowPolyglotAccess(PolyglotAccess.ALL).allowHostAccess(HostAccess.NONE).build(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            String bindingMemberName = request.getSource().getCharacters().toString();
            InteropLibrary interopLibrary = InteropLibrary.getUncached();
            Object memberKeys = interopLibrary.getMembers(interopLibrary.readMember(languageEnv.getPolyglotBindings(), bindingMemberName));
            return RootNode.createConstantNode(memberKeys).getCallTarget();
        }
    });
    class MemberKeysProxy implements ProxyObject {

        private Object memberKeys;

        MemberKeysProxy(Object memberKeys) {
            this.memberKeys = memberKeys;
        }

        @Override
        public Object getMemberKeys() {
            return memberKeys;
        }

        @Override
        public boolean hasMember(String key) {
            return true;
        }

        @Override
        public Object getMember(String key) {
            return null;
        }

        @Override
        public void putMember(String key, Value value) {
        }
    }
    context.initialize(ProxyLanguage.ID);
    Object[] memberKeys = new Object[] { "one", "two" };
    context.getPolyglotBindings().putMember("proxyArray", new MemberKeysProxy(memberKeys));
    context.getPolyglotBindings().putMember("proxyList", new MemberKeysProxy(Arrays.asList(memberKeys)));
    context.getPolyglotBindings().putMember("proxyProxyArray", new MemberKeysProxy(ProxyArray.fromArray(memberKeys)));
    try {
        context.eval(ProxyLanguage.ID, "proxyArray");
        fail();
    } catch (PolyglotException e) {
        assertTrue(e.getMessage().contains("allowArrayAccess in HostAccess is false."));
    }
    try {
        context.eval(ProxyLanguage.ID, "proxyList");
        fail();
    } catch (PolyglotException e) {
        assertTrue(e.getMessage().contains("allowListAccess in HostAccess is false."));
    }
    Value proxyArrayMembers = context.eval(ProxyLanguage.ID, "proxyProxyArray");
    assertTrue(proxyArrayMembers.hasArrayElements());
    assertEquals(2, proxyArrayMembers.getArraySize());
    assertEquals("two", proxyArrayMembers.getArrayElement(1).asString());
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) PolyglotException(org.graalvm.polyglot.PolyglotException) PolyglotException(org.graalvm.polyglot.PolyglotException) InteropException(com.oracle.truffle.api.interop.InteropException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 9 with InteropLibrary

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

the class PolyglotHostObjectPartialEvaluationTest method hasBufferElements.

@Test
public void hasBufferElements() {
    for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
        getContext().initialize(ProxyLanguage.ID);
        final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
        final RootNode node = new RootNode(null) {

            @Child
            InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);

            @Override
            public Object execute(VirtualFrame frame) {
                return interop.hasBufferElements(bufferHostObject);
            }
        };
        assertPartialEvalEquals(PolyglotHostObjectPartialEvaluationTest::constantTrue, node);
    }
}
Also used : Buffer(java.nio.Buffer) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) ValueAPITest(com.oracle.truffle.api.test.polyglot.ValueAPITest)

Example 10 with InteropLibrary

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

the class PolyglotHostObjectPartialEvaluationTest method getBufferSize.

@Test
public void getBufferSize() {
    for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
        getContext().initialize(ProxyLanguage.ID);
        final Object bufferHostObject = LanguageContext.get(null).getEnv().asGuestValue(buffer);
        final RootNode node = new RootNode(null) {

            @Child
            InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1);

            @Override
            public Object execute(VirtualFrame frame) {
                try {
                    return interop.getBufferSize(bufferHostObject);
                } catch (UnsupportedMessageException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        assertPartialEvalNoInvokes(node);
    }
}
Also used : Buffer(java.nio.Buffer) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) ValueAPITest(com.oracle.truffle.api.test.polyglot.ValueAPITest)

Aggregations

InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)158 Test (org.junit.Test)76 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)60 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)51 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)18 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)18 ArityException (com.oracle.truffle.api.interop.ArityException)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)15 WasmInstance (org.graalvm.wasm.WasmInstance)15 WebAssembly (org.graalvm.wasm.api.WebAssembly)15 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)12 RootNode (com.oracle.truffle.api.nodes.RootNode)12 InteropException (com.oracle.truffle.api.interop.InteropException)11 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)11 Dictionary (org.graalvm.wasm.api.Dictionary)8 SourceSection (com.oracle.truffle.api.source.SourceSection)6 CallTarget (com.oracle.truffle.api.CallTarget)5 Node (com.oracle.truffle.api.nodes.Node)5 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)5 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)5