Search in sources :

Example 1 with InteropLibrary

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

the class DebugExprTypeofNode method getLLVMSourceType.

@Specialization
public LLVMSourceType getLLVMSourceType(VirtualFrame frame) {
    NodeLibrary nodeLibrary = NodeLibrary.getUncached();
    InteropLibrary interopLibrary = InteropLibrary.getUncached();
    try {
        LLVMDebuggerValue entries = (LLVMDebuggerValue) nodeLibrary.getScope(location, frame, false);
        if (interopLibrary.isMemberReadable(entries, name)) {
            Object member = interopLibrary.readMember(entries, name);
            LLVMDebuggerValue ldv = (LLVMDebuggerValue) member;
            Object metaObj = ldv.resolveMetaObject();
            return (LLVMSourceType) metaObj;
        }
    } catch (ClassCastException e) {
        // OR metaObj is no primitive type
        throw DebugExprException.create(this, "\"%s\" cannot be casted to a LLVMDebuggerValue", name);
    } catch (UnsupportedMessageException e) {
        // should only happen if hasMembers == false
        throw DebugExprException.symbolNotFound(this, name, null);
    } catch (UnknownIdentifierException e) {
        throw DebugExprException.symbolNotFound(this, e.getUnknownIdentifier(), null);
    }
    return LLVMSourceType.UNKNOWN;
}
Also used : NodeLibrary(com.oracle.truffle.api.interop.NodeLibrary) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) LLVMDebuggerValue(com.oracle.truffle.llvm.runtime.debug.LLVMDebuggerValue) LLVMSourceType(com.oracle.truffle.llvm.runtime.debug.type.LLVMSourceType) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 2 with InteropLibrary

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

the class TypeHandler method getMetaObjectString.

static String getMetaObjectString(TruffleInstrument.Env env, final LanguageInfo language, Object argument) {
    Object view = env.getLanguageView(language, argument);
    InteropLibrary viewLib = InteropLibrary.getFactory().getUncached(view);
    String retType = null;
    if (viewLib.hasMetaObject(view)) {
        try {
            retType = INTEROP.asString(INTEROP.getMetaQualifiedName(viewLib.getMetaObject(view)));
        } catch (UnsupportedMessageException e) {
            CompilerDirectives.transferToInterpreter();
            throw new AssertionError(e);
        }
    }
    return retType;
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary)

Example 3 with InteropLibrary

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

the class ExposeToGuestTest method assertMember.

private static void assertMember(Object object, String member, boolean readable, boolean modifiable) throws InteropException {
    InteropLibrary interop = InteropLibrary.getFactory().getUncached();
    assertTrue(interop.hasMembers(object));
    assertEquals(readable, interop.isMemberReadable(object, member));
    assertEquals(modifiable, interop.isMemberModifiable(object, member));
    assertFalse(interop.isMemberInsertable(object, member));
    assertFalse(interop.isMemberRemovable(object, member));
    if (readable) {
        assertEquals("42", interop.readMember(object, member));
    } else {
        try {
            interop.readMember(object, member);
            fail();
        } catch (UnknownIdentifierException e) {
        }
    }
    if (modifiable) {
        interop.writeMember(object, member, "42");
    } else {
        try {
            interop.writeMember(object, member, "43");
            fail();
        } catch (UnknownIdentifierException e) {
        }
    }
    try {
        interop.removeMember(object, member);
        fail();
    } catch (UnsupportedMessageException e) {
    }
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary)

Example 4 with InteropLibrary

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

the class ContextInterruptStandaloneTest method testCancelDuringHostSleep.

@Test
public void testCancelDuringHostSleep() throws ExecutionException, InterruptedException {
    CountDownLatch beforeSleep = new CountDownLatch(1);
    enterContext = false;
    setupEnv(Context.newBuilder(ProxyLanguage.ID).allowHostClassLookup((s) -> true).allowHostAccess(HostAccess.ALL), new ProxyLanguage() {

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return new RootNode(languageInstance) {

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

                @Override
                public Object execute(VirtualFrame frame) {
                    callHostSleep();
                    return 0;
                }

                @CompilerDirectives.TruffleBoundary
                private void callHostSleep() {
                    Object javaThread = LanguageContext.get(this).getEnv().lookupHostSymbol("java.lang.Thread");
                    beforeSleep.countDown();
                    try {
                        library.invokeMember(javaThread, "sleep", 10000);
                    } catch (UnsupportedMessageException | ArityException | UnknownIdentifierException | UnsupportedTypeException e) {
                        throw new AssertionError(e);
                    }
                }
            }.getCallTarget();
        }

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }
    });
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    try {
        Future<?> future = executorService.submit(() -> {
            try {
                context.eval(ProxyLanguage.ID, "");
                Assert.fail();
            } catch (PolyglotException pe) {
                if (!pe.isCancelled() || pe.isInterrupted()) {
                    throw pe;
                }
            }
        });
        beforeSleep.await();
        context.close(true);
        future.get();
    } finally {
        executorService.shutdownNow();
        executorService.awaitTermination(100, TimeUnit.SECONDS);
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) CountDownLatch(java.util.concurrent.CountDownLatch) PolyglotException(org.graalvm.polyglot.PolyglotException) ArityException(com.oracle.truffle.api.interop.ArityException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TimeoutException(java.util.concurrent.TimeoutException) ArityException(com.oracle.truffle.api.interop.ArityException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExecutorService(java.util.concurrent.ExecutorService) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 5 with InteropLibrary

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

the class ContextInterruptStandaloneTest method testCancelFromHostCall.

private void testCancelFromHostCall(boolean nestedContextEntered) {
    setupEnv(Context.newBuilder(ProxyLanguage.ID).allowHostClassLookup((s) -> true).allowHostAccess(HostAccess.ALL), new ProxyLanguage() {

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return new RootNode(languageInstance) {

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

                @Override
                public Object execute(VirtualFrame frame) {
                    Object thisTestClass = ProxyLanguage.LanguageContext.get(this).getEnv().lookupHostSymbol(ContextInterruptStandaloneTest.class.getName());
                    try {
                        library.invokeMember(thisTestClass, "callStaticContextCancel", nestedContextEntered);
                    } catch (UnsupportedMessageException | ArityException | UnknownIdentifierException | UnsupportedTypeException e) {
                        throw new AssertionError(e);
                    }
                    return 0;
                }
            }.getCallTarget();
        }
    });
    try {
        staticContext = context;
        context.eval(ProxyLanguage.ID, "");
        Assert.fail();
    } catch (PolyglotException pe) {
        if (!pe.isCancelled()) {
            throw pe;
        }
    } finally {
        staticContext = null;
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) PolyglotException(org.graalvm.polyglot.PolyglotException) ArityException(com.oracle.truffle.api.interop.ArityException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TimeoutException(java.util.concurrent.TimeoutException) ArityException(com.oracle.truffle.api.interop.ArityException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

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