Search in sources :

Example 16 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionInDifferentModuleTables.

@Test
public void testExportSameFunctionInDifferentModuleTables() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (func $f (result i32) i32.const 42) (table 1 funcref) (elem (i32.const 0) $f) (export \"t\" (table 0)))");
    final byte[] m2 = compileWat("import", "(module (import \"m\" \"t\" (table $t 1 funcref)) (export \"t\" (table $t)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Table = WebAssembly.instanceExport(m1Instance, "t");
            final Object m1Function = WebAssembly.tableRead((WasmTable) m1Table, 0);
            final Dictionary d = new Dictionary();
            d.addMember("m", Dictionary.create(new Object[] { "t", m1Table }));
            final WasmInstance m2Instance = moduleInstantiate(wasm, m2, d);
            final Object m2Table = WebAssembly.instanceExport(m2Instance, "t");
            final Object m2Function = WebAssembly.tableRead((WasmTable) m2Table, 0);
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m2Function);
            final Object m1Value = lib.execute(m1Function);
            final Object m2Value = lib.execute(m2Function);
            Assert.assertEquals("Return value of functions is equal", m1Value, m2Value);
        } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) Dictionary(org.graalvm.wasm.api.Dictionary) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 17 with WebAssembly

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

the class WasmJsApiSuite method testFuncTypeTable.

@Test
public void testFuncTypeTable() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableExport, null);
        try {
            final Object funcType = wasm.readMember("func_type");
            final WasmTable table = (WasmTable) WebAssembly.instanceExport(instance, "defaultTable");
            final Object fn = WebAssembly.tableRead(table, 0);
            InteropLibrary interop = InteropLibrary.getUncached(funcType);
            Assert.assertEquals("func_type", "0(i32)i32", interop.execute(funcType, fn));
            // set + get should not break func_type()
            WebAssembly.tableWrite(table, 0, fn);
            final Object fnAgain = WebAssembly.tableRead(table, 0);
            Assert.assertEquals("func_type", "0(i32)i32", interop.execute(funcType, fnAgain));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 18 with WebAssembly

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

the class WasmJsApiSuite method testExportMemoryTwice.

@Test
public void testExportMemoryTwice() throws IOException, InterruptedException {
    final byte[] exportMemoryTwice = compileWat("exportMemoryTwice", "(memory 1) (export \"a\" (memory 0)) (export \"b\" (memory 0))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
        try {
            final InteropLibrary lib = InteropLibrary.getUncached();
            final Object memoryABuffer = WebAssembly.instanceExport(instance, "a");
            final Object memoryBBuffer = WebAssembly.instanceExport(instance, "b");
            lib.writeArrayElement(memoryABuffer, 0, (byte) 42);
            final byte readValue = lib.asByte(lib.readArrayElement(memoryBBuffer, 0));
            Assert.assertEquals("Written value should correspond to read value", (byte) 42, readValue);
        } catch (UnsupportedMessageException | UnsupportedTypeException | InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 19 with WebAssembly

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

the class WasmJsApiSuite method testTableInstanceGrowLimit.

@Test
public void testTableInstanceGrowLimit() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        // We should be able to grow the table to 10,000,000.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, table, 9999999);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But growing to 10,000,001 should fail.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, table, 1);
            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) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 20 with WebAssembly

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

the class WasmJsApiSuite method testFuncTypeExport.

@Test
public void testFuncTypeExport() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
        try {
            final Object funcType = wasm.readMember("func_type");
            final Object fn = WebAssembly.instanceExport(instance, "readZero");
            Assert.assertEquals("func_type", "0()i32", InteropLibrary.getUncached(funcType).execute(funcType, fn));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : 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) Test(org.junit.Test)

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