Search in sources :

Example 1 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testTableImport.

@Test
public void testTableImport() throws IOException, InterruptedException {
    // Exports table with a function
    final byte[] exportTable = compileWat("exportTable", "(module" + "(func $f0 (result i32) i32.const 42)" + "(table 1 1 funcref)" + "(export \"table\" (table 0))" + "(elem (i32.const 0) $f0)" + ")");
    // Imports table and exports function that invokes functions from the table
    final byte[] importTable = compileWat("importTable", "(module" + "(type (func (param i32) (result i32)))" + "(type (func (result i32)))" + "(import \"tableImport\" \"table\" (table 1 1 funcref))" + "(func (type 0) (param i32) (result i32) local.get 0 call_indirect (type 1))" + "(export \"testFunc\" (func 0))" + ")");
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        WasmInstance exportInstance = moduleInstantiate(wasm, exportTable, null);
        try {
            Object exportedTable = WebAssembly.instanceExport(exportInstance, "table");
            Dictionary importObject = new Dictionary();
            Dictionary tableImport = new Dictionary();
            tableImport.addMember("table", exportedTable);
            importObject.addMember("tableImport", tableImport);
            WasmInstance importInstance = moduleInstantiate(wasm, importTable, importObject);
            Object testFunc = WebAssembly.instanceExport(importInstance, "testFunc");
            Object result = InteropLibrary.getUncached().execute(testFunc, 0);
            Assert.assertEquals("Return value should be 42", 42, result);
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) Dictionary(org.graalvm.wasm.api.Dictionary) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 2 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testCompile.

@Test
public void testCompile() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmModule module = wasm.moduleDecode(binaryWithExports);
        try {
            HashMap<String, ModuleExportDescriptor> exports = new HashMap<>();
            int i = 0;
            while (i < WebAssembly.moduleExports(module).getArraySize()) {
                final ModuleExportDescriptor d = (ModuleExportDescriptor) WebAssembly.moduleExports(module).readArrayElement(i);
                exports.put(d.name(), d);
                i++;
            }
            Assert.assertEquals("Should export main.", ImportExportKind.function, exports.get("main").kind());
            Assert.assertEquals("Should export memory.", ImportExportKind.memory, exports.get("memory").kind());
            Assert.assertEquals("Should export global __heap_base.", ImportExportKind.global, exports.get("__heap_base").kind());
            Assert.assertEquals("Should export global __data_end.", ImportExportKind.global, exports.get("__data_end").kind());
            Assert.assertEquals("Should have empty imports.", 0L, WebAssembly.moduleImports(module).getArraySize());
        } catch (InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmModule(org.graalvm.wasm.WasmModule) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) HashMap(java.util.HashMap) WebAssembly(org.graalvm.wasm.api.WebAssembly) ModuleExportDescriptor(org.graalvm.wasm.api.ModuleExportDescriptor) Test(org.junit.Test)

Example 3 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testInstantiateWithImports.

@Test
public void testInstantiateWithImports() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        Dictionary importObject = Dictionary.create(new Object[] { "host", Dictionary.create(new Object[] { "inc", new Executable(args -> ((int) args[0]) + 1) }) });
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithImportsAndExports, importObject);
        try {
            final Object addPlusOne = WebAssembly.instanceExport(instance, "addPlusOne");
            final Object result = InteropLibrary.getUncached(addPlusOne).execute(addPlusOne, 17, 3);
            Assert.assertEquals("17 + 3 + 1 = 21.", 21, InteropLibrary.getUncached(result).asInt(result));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Dictionary(org.graalvm.wasm.api.Dictionary) WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Executable(org.graalvm.wasm.api.Executable) Test(org.junit.Test)

Example 4 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly 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)

Example 5 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method runValidationValid.

private static void runValidationValid(byte[] data) throws IOException {
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        Assert.assertTrue("Should have failed - valid module", wasm.moduleValidate(data));
    });
}
Also used : WebAssembly(org.graalvm.wasm.api.WebAssembly)

Aggregations

WebAssembly (org.graalvm.wasm.api.WebAssembly)36 Test (org.junit.Test)33 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)29 WasmInstance (org.graalvm.wasm.WasmInstance)29 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)15 InteropException (com.oracle.truffle.api.interop.InteropException)14 Dictionary (org.graalvm.wasm.api.Dictionary)13 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)11 ArityException (com.oracle.truffle.api.interop.ArityException)10 WasmModule (org.graalvm.wasm.WasmModule)5 WasmTable (org.graalvm.wasm.WasmTable)5 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)4 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)4 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)3 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)3 RootNode (com.oracle.truffle.api.nodes.RootNode)3 Executable (org.graalvm.wasm.api.Executable)3 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)3 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)2