Search in sources :

Example 21 with InteropLibrary

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

the class HostProxy method getMembers.

@ExportMessage
@TruffleBoundary
Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @CachedLibrary("this") InteropLibrary library, @Shared("cache") @Cached(value = "this.context.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws UnsupportedMessageException {
    if (proxy instanceof ProxyObject) {
        Object result = guestToHostCall(library, cache.memberKeys, context, proxy);
        if (result == null) {
            result = EMPTY;
        }
        Object guestValue = context.toGuestValue(library, result);
        InteropLibrary interop = InteropLibrary.getFactory().getUncached();
        if (!interop.hasArrayElements(guestValue)) {
            if (guestValue instanceof HostObject) {
                HostObject hostObject = (HostObject) guestValue;
                if (hostObject.obj.getClass().isArray() && !hostObject.getHostClassCache().isArrayAccess()) {
                    throw illegalProxy(context, "getMemberKeys() returned a Java array %s, but allowArrayAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
                } else if (hostObject.obj instanceof List && !hostObject.getHostClassCache().isListAccess()) {
                    throw illegalProxy(context, "getMemberKeys() returned a Java List %s, but allowListAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
                }
            }
            throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
        }
        // Todo: Use interop to determine an array element type when the GR-5737 is resolved.
        for (int i = 0; i < interop.getArraySize(guestValue); i++) {
            try {
                Object element = interop.readArrayElement(guestValue, i);
                if (!interop.isString(element)) {
                    throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
                }
            } catch (UnsupportedOperationException e) {
                CompilerDirectives.shouldNotReachHere(e);
            } catch (InvalidArrayIndexException e) {
                continue;
            }
        }
        return guestValue;
    } else {
        throw UnsupportedMessageException.create();
    }
}
Also used : ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) List(java.util.List) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 22 with InteropLibrary

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

the class HostLanguage method getLanguageView.

@Override
@TruffleBoundary
protected Object getLanguageView(HostContext hostContext, Object value) {
    Object wrapped;
    if (value instanceof TruffleObject) {
        InteropLibrary lib = InteropLibrary.getFactory().getUncached(value);
        try {
            assert !lib.hasLanguage(value) || lib.getLanguage(value) != HostLanguage.class;
        } catch (UnsupportedMessageException e) {
            throw shouldNotReachHere(e);
        }
        wrapped = HostToTypeNode.convertToObject(hostContext, value, lib);
    } else {
        wrapped = value;
    }
    return HostObject.forObject(wrapped, hostContext);
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ScopedObject(com.oracle.truffle.host.HostMethodScope.ScopedObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 23 with InteropLibrary

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

the class SLException method typeError.

/**
 * Provides a user-readable message for run-time type errors. SL is strongly typed, i.e., there
 * are no automatic type conversions of values.
 */
@TruffleBoundary
public static SLException typeError(Node operation, Object... values) {
    StringBuilder result = new StringBuilder();
    result.append("Type error");
    if (operation != null) {
        SourceSection ss = operation.getEncapsulatingSourceSection();
        if (ss != null && ss.isAvailable()) {
            result.append(" at ").append(ss.getSource().getName()).append(" line ").append(ss.getStartLine()).append(" col ").append(ss.getStartColumn());
        }
    }
    result.append(": operation");
    if (operation != null) {
        NodeInfo nodeInfo = SLLanguage.lookupNodeInfo(operation.getClass());
        if (nodeInfo != null) {
            result.append(" \"").append(nodeInfo.shortName()).append("\"");
        }
    }
    result.append(" not defined for");
    String sep = " ";
    for (int i = 0; i < values.length; i++) {
        /*
             * For primitive or foreign values we request a language view so the values are printed
             * from the perspective of simple language and not another language. Since this is a
             * rather rarely invoked exceptional method, we can just create the language view for
             * primitive values and then conveniently request the meta-object and display strings.
             * Using the language view for core builtins like the typeOf builtin might not be a good
             * idea for performance reasons.
             */
        Object value = SLLanguageView.forValue(values[i]);
        result.append(sep);
        sep = ", ";
        if (value == null) {
            result.append("ANY");
        } else {
            InteropLibrary valueLib = InteropLibrary.getFactory().getUncached(value);
            if (valueLib.hasMetaObject(value) && !valueLib.isNull(value)) {
                String qualifiedName;
                try {
                    qualifiedName = UNCACHED_LIB.asString(UNCACHED_LIB.getMetaQualifiedName(valueLib.getMetaObject(value)));
                } catch (UnsupportedMessageException e) {
                    throw shouldNotReachHere(e);
                }
                result.append(qualifiedName);
                result.append(" ");
            }
            if (valueLib.isString(value)) {
                result.append("\"");
            }
            result.append(valueLib.toDisplayString(value));
            if (valueLib.isString(value)) {
                result.append("\"");
            }
        }
    }
    return new SLException(result.toString(), operation);
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) NodeInfo(com.oracle.truffle.api.nodes.NodeInfo) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 24 with InteropLibrary

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

the class WasmJsApiSuite method checkCustomSection.

private static void checkCustomSection(byte[] expected, ByteArrayBuffer actual) throws InvalidArrayIndexException, UnsupportedMessageException {
    InteropLibrary interop = InteropLibrary.getUncached(actual);
    Assert.assertEquals("Custom section length", expected.length, (int) interop.getArraySize(actual));
    for (int i = 0; i < expected.length; i++) {
        Assert.assertEquals("Custom section data", expected[i], interop.readArrayElement(actual, i));
    }
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary)

Example 25 with InteropLibrary

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

the class WasmJsApiSuite method testTableInstanceOutOfBoundsSet.

@Test
public void testTableInstanceOutOfBoundsSet() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        WasmContext wasmContext = WasmContext.get(null);
        final WasmFunctionInstance functionInstance = new WasmFunctionInstance(wasmContext, null, new RootNode(wasmContext.language()) {

            @Override
            public Object execute(VirtualFrame frame) {
                return 42;
            }
        }.getCallTarget());
        // We should be able to set element 1.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object writeTable = wasm.readMember("table_write");
            lib.execute(writeTable, table, 0, functionInstance);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But not element 2.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object writeTable = wasm.readMember("table_write");
            lib.execute(writeTable, table, 1, functionInstance);
            Assert.fail("Should have failed - export count exceeds the limit");
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) RootNode(com.oracle.truffle.api.nodes.RootNode) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WebAssembly(org.graalvm.wasm.api.WebAssembly) ArityException(com.oracle.truffle.api.interop.ArityException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) WasmContext(org.graalvm.wasm.WasmContext) 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) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) Test(org.junit.Test)

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