Search in sources :

Example 16 with InteropLibrary

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

the class PolyglotValueDispatch method toStringImpl.

protected String toStringImpl(@SuppressWarnings("unused") Object languageContext, Object receiver) throws AssertionError {
    InteropLibrary lib = InteropLibrary.getFactory().getUncached(receiver);
    Object result = lib.toDisplayString(receiver);
    InteropLibrary resultLib = InteropLibrary.getFactory().getUncached(result);
    try {
        return resultLib.asString(result);
    } catch (UnsupportedMessageException e) {
        throw shouldNotReachHere("toDisplayString must be coercible to java.lang.String, but is not.", 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 17 with InteropLibrary

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

the class WebAssembly method readModuleImports.

private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
    CompilerAsserts.neverPartOfCompilation();
    final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
    if (imports.getArraySize() != 0 && importObject == null) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
    }
    HashMap<String, ImportModule> importModules = new HashMap<>();
    final InteropLibrary lib = InteropLibrary.getUncached();
    try {
        int i = 0;
        while (i < imports.getArraySize()) {
            final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
            final Object importedModule = getMember(importObject, d.module());
            final Object member = getMember(importedModule, d.name());
            switch(d.kind()) {
                case function:
                    if (!lib.isExecutable(member)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
                    }
                    WasmFunction f = module.importedFunction(d.name());
                    ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
                    break;
                case memory:
                    if (!(member instanceof WasmMemory)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
                    }
                    ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
                    break;
                case table:
                    if (!(member instanceof WasmTable)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
                    }
                    ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
                    break;
                case global:
                    if (!(member instanceof WasmGlobal)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
                    }
                    ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
                    break;
                default:
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
            }
            i += 1;
        }
    } catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
        throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
    }
    return importModules;
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) HashMap(java.util.HashMap) WasmTable(org.graalvm.wasm.WasmTable) WasmFunction(org.graalvm.wasm.WasmFunction) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 18 with InteropLibrary

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

the class HighlightRequestHandler method findOtherReadOrWrites.

List<? extends DocumentHighlight> findOtherReadOrWrites(TextDocumentSurrogate surrogate, InstrumentableNode nodeAtCaret, int line, int character) {
    InteropUtils.VariableInfo[] caretVariables = InteropUtils.getNodeObjectVariables(nodeAtCaret);
    if (caretVariables.length > 0) {
        Set<String> variableNames = new HashSet<>();
        for (InteropUtils.VariableInfo varInfo : caretVariables) {
            if (contains(varInfo.getSourceSection(), line, character)) {
                variableNames.add(varInfo.getName());
            }
        }
        Object scope = getScope(surrogate, nodeAtCaret);
        List<DocumentHighlight> highlights = new ArrayList<>();
        while (scope != null) {
            InteropLibrary interop = InteropLibrary.getUncached(scope);
            if (interop.hasSourceLocation(scope)) {
                try {
                    SourceSection sourceLocation = interop.getSourceLocation(scope);
                    Node[] nodeLocation = new Node[] { null };
                    EventBinding<LoadSourceSectionListener> binding = env.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.newBuilder().sourceSectionEquals(sourceLocation).build(), e -> {
                        Node node = e.getNode();
                        if (nodeLocation[0] == null || isParent(node, nodeLocation[0])) {
                            nodeLocation[0] = node;
                        }
                    }, true);
                    binding.dispose();
                    addHighlights(nodeLocation[0], variableNames, highlights);
                } catch (UnsupportedMessageException e) {
                    throw CompilerDirectives.shouldNotReachHere(e);
                }
            }
            if (interop.hasScopeParent(scope)) {
                try {
                    scope = interop.getScopeParent(scope);
                } catch (UnsupportedMessageException e) {
                    throw CompilerDirectives.shouldNotReachHere(e);
                }
            } else {
                scope = null;
            }
        }
        return highlights;
    }
    return Collections.emptyList();
}
Also used : DocumentHighlight(org.graalvm.tools.lsp.server.types.DocumentHighlight) InteropUtils(org.graalvm.tools.lsp.server.utils.InteropUtils) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) ArrayList(java.util.ArrayList) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection) HashSet(java.util.HashSet)

Example 19 with InteropLibrary

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

the class HashTest method testInteropMessages.

private static void testInteropMessages(Object hash, KeyFactory<?> keyFactory) throws InteropException {
    final int count = 100;
    final int inc = 2;
    InteropLibrary interop = InteropLibrary.getUncached();
    assertTrue(interop.hasHashEntries(hash));
    for (int i = 0; i < count; i += inc) {
        Object key = keyFactory.create(i);
        Object value = i;
        assertNonExisting(hash, key, interop);
        interop.writeHashEntry(hash, key, value);
    }
    assertEquals((count / inc), interop.getHashSize(hash));
    for (int i = 0; i < count; i++) {
        Object key = keyFactory.create(i);
        if ((i & 1) == 0) {
            Object expectedValue = i;
            assertExisting(hash, key, interop);
            assertEquals(expectedValue, interop.readHashValue(hash, key));
            assertEquals(expectedValue, interop.readHashValueOrDefault(hash, key, "failure"));
        } else {
            assertNonExisting(hash, key, interop);
            assertEquals("failure", interop.readHashValueOrDefault(hash, key, "failure"));
        }
    }
    Map<Object, Integer> expected = new HashMap<>();
    for (int i = 0; i < count; i += inc) {
        Object key = keyFactory.create(i);
        int value = -1 * i;
        interop.writeHashEntry(hash, key, value);
        assertExisting(hash, key, interop);
        assertEquals(value, interop.readHashValue(hash, key));
        expected.put(key, value);
    }
    Object iterator = interop.getHashEntriesIterator(hash);
    assertTrue(interop.isIterator(iterator));
    Map<Object, Integer> expected2 = new HashMap<>();
    while (interop.hasIteratorNextElement(iterator)) {
        Object entry = interop.getIteratorNextElement(iterator);
        assertTrue(interop.hasArrayElements(entry));
        Object key = interop.readArrayElement(entry, 0);
        int value = (int) interop.readArrayElement(entry, 1);
        int expectedValue = expected.remove(key);
        assertEquals(expectedValue, value);
        int newValue = -1 * value;
        interop.writeArrayElement(entry, 1, newValue);
        expected2.put(key, newValue);
    }
    assertTrue(expected.isEmpty());
    Set<Object> expectedKeys = new HashSet<>(expected2.keySet());
    Collection<Integer> expectedValues = new ArrayList<>(expected2.values());
    iterator = interop.getHashEntriesIterator(hash);
    while (interop.hasIteratorNextElement(iterator)) {
        Object entry = interop.getIteratorNextElement(iterator);
        Object key = interop.readArrayElement(entry, 0);
        int value = (int) interop.readArrayElement(entry, 1);
        int expectedValue = expected2.remove(key);
        assertEquals(expectedValue, value);
    }
    assertTrue(expected2.isEmpty());
    iterator = interop.getHashKeysIterator(hash);
    while (interop.hasIteratorNextElement(iterator)) {
        Object key = interop.getIteratorNextElement(iterator);
        assertTrue(expectedKeys.remove(key));
    }
    assertTrue(expectedKeys.isEmpty());
    iterator = interop.getHashValuesIterator(hash);
    while (interop.hasIteratorNextElement(iterator)) {
        Object value = interop.getIteratorNextElement(iterator);
        assertTrue(expectedValues.remove(value));
    }
    assertTrue(expectedValues.isEmpty());
    for (int i = 0; i < count; i += inc) {
        Object key = keyFactory.create(i);
        interop.removeHashEntry(hash, key);
        assertNonExisting(hash, key, interop);
    }
    assertEquals(0, interop.getHashSize(hash));
}
Also used : HashMap(java.util.HashMap) ProxyHashMap(org.graalvm.polyglot.proxy.ProxyHashMap) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ArrayList(java.util.ArrayList) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) HashSet(java.util.HashSet)

Example 20 with InteropLibrary

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

the class GR31558 method expectArrayArg.

private void expectArrayArg(int elementCount, Object result) {
    assertEquals(Arrays.toString(actualArguments), 1, actualArguments.length);
    assertEquals("EXECUTE", result);
    Object arg0 = actualArguments[0];
    InteropLibrary interop = InteropLibrary.getUncached();
    try {
        assertTrue(interop.asString(interop.toDisplayString(arg0)), interop.hasArrayElements(arg0));
        assertEquals(interop.asString(interop.toDisplayString(arg0)), elementCount, interop.getArraySize(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)

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