Search in sources :

Example 6 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WebAssembly method globalRead.

private static Object globalRead(Object[] args) {
    checkArgumentCount(args, 1);
    if (!(args[0] instanceof WasmGlobal)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm global");
    }
    WasmGlobal global = (WasmGlobal) args[0];
    return globalRead(global);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal)

Example 7 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WebAssembly method instanceExport.

public static Object instanceExport(WasmInstance instance, String name) {
    CompilerAsserts.neverPartOfCompilation();
    WasmFunction function = instance.module().exportedFunctions().get(name);
    Integer globalIndex = instance.module().exportedGlobals().get(name);
    if (function != null) {
        return instance.functionInstance(function);
    } else if (globalIndex != null) {
        final int index = globalIndex;
        final int address = instance.globalAddress(index);
        if (address < 0) {
            return instance.context().globals().externalGlobal(address);
        } else {
            final ValueType valueType = ValueType.fromByteValue(instance.symbolTable().globalValueType(index));
            final boolean mutable = instance.symbolTable().isGlobalMutable(index);
            return new ExportedWasmGlobal(valueType, mutable, instance.context().globals(), address);
        }
    } else if (instance.module().exportedMemoryNames().contains(name)) {
        return instance.memory();
    } else if (instance.module().exportedTableNames().contains(name)) {
        return instance.table();
    } else {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, name + " is not a exported name of the given instance");
    }
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmFunction(org.graalvm.wasm.WasmFunction) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal)

Example 8 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WebAssembly method tableSize.

private static Object tableSize(Object[] args) {
    checkArgumentCount(args, 1);
    if (!(args[0] instanceof WasmTable)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm table");
    }
    WasmTable table = (WasmTable) args[0];
    return tableSize(table);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmTable(org.graalvm.wasm.WasmTable)

Example 9 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.

the class WebAssembly method globalAlloc.

private static Object globalAlloc(Object[] args) {
    checkArgumentCount(args, 3);
    ValueType valueType;
    try {
        String valueTypeString = InteropLibrary.getUncached().asString(args[0]);
        valueType = ValueType.valueOf(valueTypeString);
    } catch (UnsupportedMessageException ex) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument (value type) must be convertible to String");
    } catch (IllegalArgumentException ex) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Invalid value type");
    }
    boolean mutable;
    try {
        mutable = InteropLibrary.getUncached().asBoolean(args[1]);
    } catch (UnsupportedMessageException ex) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument (mutable) must be convertible to boolean");
    }
    return globalAlloc(valueType, mutable, args[2]);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException)

Example 10 with WasmJsApiException

use of org.graalvm.wasm.exception.WasmJsApiException 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

WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)20 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)6 WasmTable (org.graalvm.wasm.WasmTable)6 Test (org.junit.Test)6 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)4 WasmInstance (org.graalvm.wasm.WasmInstance)4 ExportedWasmGlobal (org.graalvm.wasm.globals.ExportedWasmGlobal)4 WasmMemory (org.graalvm.wasm.memory.WasmMemory)4 ArityException (com.oracle.truffle.api.interop.ArityException)3 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)3 WebAssembly (org.graalvm.wasm.api.WebAssembly)3 DefaultWasmGlobal (org.graalvm.wasm.globals.DefaultWasmGlobal)3 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)3 ByteArrayWasmMemory (org.graalvm.wasm.memory.ByteArrayWasmMemory)3 UnsafeWasmMemory (org.graalvm.wasm.memory.UnsafeWasmMemory)3 WasmFunction (org.graalvm.wasm.WasmFunction)2 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1