Search in sources :

Example 11 with InteropLibrary

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

the class PolyglotHostObjectPartialEvaluationTest method isBufferWritable.

@Test
public void isBufferWritable() {
    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.isBufferWritable(bufferHostObject);
                } catch (UnsupportedMessageException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        assertPartialEvalEquals(buffer.isReadOnly() ? PolyglotHostObjectPartialEvaluationTest::constantFalse : PolyglotHostObjectPartialEvaluationTest::constantTrue, 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)

Example 12 with InteropLibrary

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

the class PolyglotHostObjectPartialEvaluationTest method readBufferByte.

@Test
public void readBufferByte() {
    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.readBufferByte(bufferHostObject, 0);
                } catch (UnsupportedMessageException | InvalidBufferOffsetException 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) InvalidBufferOffsetException(com.oracle.truffle.api.interop.InvalidBufferOffsetException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Test(org.junit.Test) ValueAPITest(com.oracle.truffle.api.test.polyglot.ValueAPITest)

Example 13 with InteropLibrary

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

the class PolyglotValueDispatch method getSourceLocation.

@Override
public SourceSection getSourceLocation(Object languageContext, Object receiver) {
    if (languageContext == null) {
        return null;
    }
    PolyglotLanguageContext context = (PolyglotLanguageContext) languageContext;
    Object prev = hostEnter(context);
    try {
        InteropLibrary lib = InteropLibrary.getFactory().getUncached(receiver);
        com.oracle.truffle.api.source.SourceSection result = null;
        if (lib.hasSourceLocation(receiver)) {
            try {
                result = lib.getSourceLocation(receiver);
            } catch (UnsupportedMessageException e) {
            }
        }
        if (result == null) {
            return null;
        }
        return PolyglotImpl.getPolyglotSourceSection(impl, result);
    } catch (Throwable e) {
        throw guestToHostException(context, e, true);
    } finally {
        hostLeave(context, prev);
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 14 with InteropLibrary

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

the class PolyglotWrapper method equals.

@TruffleBoundary
static boolean equals(Object context, Object receiver, Object obj) {
    if (obj == null) {
        return false;
    } else if (receiver == obj) {
        return true;
    }
    PolyglotLanguageContext languageContext = (PolyglotLanguageContext) context;
    if (languageContext != null) {
        PolyglotContextImpl.State localContextState = languageContext.context.state;
        if (localContextState.isInvalidOrClosed()) {
            return false;
        }
    }
    Object prev = null;
    try {
        prev = PolyglotValueDispatch.hostEnter(languageContext);
    } catch (Throwable t) {
        // Can no longer call interop.
        return false;
    }
    try {
        InteropLibrary receiverLib = InteropLibrary.getUncached(receiver);
        InteropLibrary objLib = InteropLibrary.getUncached(obj);
        return receiverLib.isIdentical(receiver, obj, objLib);
    } catch (Throwable t) {
        // propagate errors in languages they should be reported.
        throw PolyglotImpl.guestToHostException(languageContext, t, true);
    } finally {
        try {
            PolyglotValueDispatch.hostLeave(languageContext, prev);
        } catch (Throwable t) {
        // ignore errors leaving we cannot propagate them.
        }
    }
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 15 with InteropLibrary

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

the class PolyglotWrapper method hashCode.

@TruffleBoundary
static int hashCode(Object context, Object receiver) {
    PolyglotLanguageContext languageContext = (PolyglotLanguageContext) context;
    if (languageContext != null) {
        PolyglotContextImpl.State localContextState = languageContext.context.state;
        if (localContextState.isInvalidOrClosed()) {
            return System.identityHashCode(receiver);
        }
    }
    Object prev = null;
    try {
        prev = PolyglotValueDispatch.hostEnter(languageContext);
    } catch (Throwable t) {
        // Can no longer call interop.
        return System.identityHashCode(receiver);
    }
    try {
        InteropLibrary receiverLib = InteropLibrary.getUncached(receiver);
        if (receiverLib.hasIdentity(receiver)) {
            return receiverLib.identityHashCode(receiver);
        } else {
            return System.identityHashCode(receiver);
        }
    } catch (Throwable t) {
        // propagate errors in languages they should be reported.
        throw PolyglotImpl.guestToHostException(languageContext, t, true);
    } finally {
        try {
            PolyglotValueDispatch.hostLeave(languageContext, prev);
        } catch (Throwable t) {
        // ignore errors leaving we cannot propagate them.
        }
    }
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

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