Search in sources :

Example 81 with InteropLibrary

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

the class PolyglotHostObjectPartialEvaluationTest method writeBufferByte.

@Test
public void writeBufferByte() {
    for (final Buffer buffer : ValueAPITest.makeTestBuffers()) {
        if (buffer.isReadOnly()) {
            continue;
        }
        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 {
                    interop.writeBufferByte(bufferHostObject, 0, (byte) 42);
                } catch (UnsupportedMessageException | InvalidBufferOffsetException e) {
                    throw new RuntimeException(e);
                }
                return null;
            }
        };
        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 82 with InteropLibrary

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

the class BaseInterop method toDisplayString.

@ExportMessage
@TruffleBoundary
public static Object toDisplayString(StaticObject object, boolean allowSideEffects) {
    if (object.isForeignObject()) {
        InteropLibrary interopLibrary = InteropLibrary.getUncached();
        try {
            return "Foreign object: " + interopLibrary.asString(interopLibrary.toDisplayString(object.rawForeignObject(), allowSideEffects));
        } catch (UnsupportedMessageException e) {
            throw EspressoError.shouldNotReachHere("Interop library failed to convert display string to string");
        }
    }
    if (StaticObject.isNull(object)) {
        return "NULL";
    }
    Klass thisKlass = object.getKlass();
    Meta meta = thisKlass.getMeta();
    if (allowSideEffects) {
        // Call guest toString.
        int toStringIndex = meta.java_lang_Object_toString.getVTableIndex();
        Method toString = thisKlass.vtableLookup(toStringIndex);
        return meta.toHostString((StaticObject) toString.invokeDirect(object));
    }
    // Handle some special instances without side effects.
    if (thisKlass == meta.java_lang_Class) {
        return "class " + thisKlass.getTypeAsString();
    }
    if (thisKlass == meta.java_lang_String) {
        return meta.toHostString(object);
    }
    return thisKlass.getTypeAsString() + "@" + Integer.toHexString(System.identityHashCode(object));
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 83 with InteropLibrary

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

the class Target_java_lang_System method handleForeignArray.

@TruffleBoundary
private static void handleForeignArray(Object src, int srcPos, Object dest, int destPos, int length, Klass destType, Meta meta, SubstitutionProfiler profiler) {
    InteropLibrary library = InteropLibrary.getUncached();
    ToEspressoNode toEspressoNode = ToEspressoNodeGen.getUncached();
    if (library.isNull(src) || library.isNull(dest)) {
        throw throwNullPointerEx(meta, profiler);
    }
    if (!library.hasArrayElements(src) || !library.hasArrayElements(dest)) {
        throw throwArrayStoreEx(meta, profiler);
    }
    try {
        int srclen = (int) library.getArraySize(src);
        int destlen = (int) library.getArraySize(dest);
        boundsCheck(meta, srclen, srcPos, destlen, destPos, length, profiler);
        for (int i = 0; i < length; i++) {
            Object cpy = toEspressoNode.execute(library.readArrayElement(src, i + srcPos), destType);
            library.writeArrayElement(dest, destPos + i, cpy);
        }
    } catch (UnsupportedMessageException | UnsupportedTypeException e) {
        CompilerDirectives.transferToInterpreter();
        throw EspressoError.shouldNotReachHere();
    } catch (InvalidArrayIndexException e) {
        throw throwArrayStoreEx(meta, profiler);
    }
}
Also used : InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ToEspressoNode(com.oracle.truffle.espresso.nodes.interop.ToEspressoNode) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 84 with InteropLibrary

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

the class GR31558 method expectArgs.

private void expectArgs(int argCount, Object result) {
    assertEquals(Arrays.toString(actualArguments), argCount, actualArguments.length);
    assertEquals("EXECUTE", result);
    if (argCount == 0) {
        return;
    }
    Object arg0 = actualArguments[0];
    InteropLibrary interop = InteropLibrary.getUncached();
    try {
        assertFalse(interop.asString(interop.toDisplayString(arg0)), interop.hasArrayElements(arg0));
    } catch (UnsupportedMessageException e) {
        throw new AssertionError(e);
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 85 with InteropLibrary

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

the class GR31558 method before.

@Before
public void before() {
    HostAccess.Builder hostAccessBuilder = // 
    HostAccess.newBuilder().allowAccessAnnotatedBy(// 
    HostAccess.Export.class).allowPublicAccess(// 
    false).allowArrayAccess(// 
    true).allowIterableAccess(// 
    true).allowIteratorAccess(// 
    true).allowImplementationsAnnotatedBy(FunctionalInterface.class);
    Context.Builder contextBuilder = Context.newBuilder();
    contextBuilder.allowHostAccess(hostAccessBuilder.build());
    setupEnv(contextBuilder, new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            String src = request.getSource().getCharacters().toString();
            RootCallTarget invokeTestApi;
            if ("testFunction".equals(src)) {
                invokeTestApi = new RootNode(ProxyLanguage.get(null)) {

                    @Override
                    public Object execute(VirtualFrame frame) {
                        try {
                            InteropLibrary interop = InteropLibrary.getUncached();
                            Object test = frame.getArguments()[0];
                            interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction());
                            if (interop.isMemberExisting(test, "testMap")) {
                                interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction(false, false, false, true));
                            }
                            if (interop.isMemberExisting(test, "testMapEntry")) {
                                interop.invokeMember(test, "testMapEntry", "hi", new ArgumentsCollectorFunction(false, false, true, false));
                            }
                            if (interop.isMemberExisting(test, "testList")) {
                                interop.invokeMember(test, "testList", "hi", new ArgumentsCollectorFunction(false, false, true, false));
                            }
                            if (interop.isMemberExisting(test, "testIterator")) {
                                interop.invokeMember(test, "testIterator", "hi", new ArgumentsCollectorFunction(false, true, false, false));
                            }
                            if (interop.isMemberExisting(test, "testIterable")) {
                                interop.invokeMember(test, "testIterable", "hi", new ArgumentsCollectorFunction(true, false, false, false));
                            }
                            return "success";
                        } catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
                            CompilerDirectives.transferToInterpreter();
                            throw new AssertionError(e);
                        }
                    }
                }.getCallTarget();
            } else {
                throw new IllegalArgumentException(src);
            }
            return RootNode.createConstantNode(new HostExceptionTest.CatcherObject(invokeTestApi)).getCallTarget();
        }
    });
}
Also used : Context(org.graalvm.polyglot.Context) RootNode(com.oracle.truffle.api.nodes.RootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) StopIterationException(com.oracle.truffle.api.interop.StopIterationException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) HostAccess(org.graalvm.polyglot.HostAccess) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Before(org.junit.Before)

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