Search in sources :

Example 6 with WebAssembly

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

the class WasmJsApiSuite method testCustomSections.

@Test
public void testCustomSections() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmModule module = wasm.moduleDecode(binaryWithCustomSections);
        try {
            checkCustomSections(new byte[][] {}, WebAssembly.customSections(module, ""));
            checkCustomSections(new byte[][] {}, WebAssembly.customSections(module, "zero"));
            checkCustomSections(new byte[][] { { 1, 3, 5 } }, WebAssembly.customSections(module, "odd"));
            checkCustomSections(new byte[][] { { 2, 4 }, { 6 } }, WebAssembly.customSections(module, "even"));
        } catch (InteropException ex) {
            throw new RuntimeException(ex);
        }
    });
}
Also used : WasmModule(org.graalvm.wasm.WasmModule) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 7 with WebAssembly

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

the class WasmJsApiSuite method testInstantiateModuleTwice.

@Test
public void testInstantiateModuleTwice() throws IOException {
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        WasmModule module = wasm.moduleDecode(binaryWithExports);
        Object importObject = new Dictionary();
        wasm.moduleInstantiate(module, importObject);
        wasm.moduleInstantiate(module, importObject);
    });
}
Also used : Dictionary(org.graalvm.wasm.api.Dictionary) WasmModule(org.graalvm.wasm.WasmModule) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 8 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionInDifferentModuleTable.

@Test
public void testExportSameFunctionInDifferentModuleTable() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (func $f (result i32) i32.const 42) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"m\" \"f\" (func $f (result i32))) (table 1 funcref) (elem (i32.const 0) $f) (export \"t\" (table 0)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Dictionary d = new Dictionary();
            d.addMember("m", Dictionary.create(new Object[] { "f", m1Function }));
            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 9 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionInDifferentModules.

@Test
public void testExportSameFunctionInDifferentModules() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (func $f (result i32) i32.const 42) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"m\" \"f\" (func $f (result i32))) (export \"f\" (func $f)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Dictionary d = new Dictionary();
            d.addMember("m", Dictionary.create(new Object[] { "f", m1Function }));
            final WasmInstance m2Instance = moduleInstantiate(wasm, m2, d);
            final Object m2Function = WebAssembly.instanceExport(m2Instance, "f");
            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 10 with WebAssembly

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

the class WasmJsApiSuite method testExportImportedFunctionInDifferentModuleTable.

@Test
public void testExportImportedFunctionInDifferentModuleTable() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (import \"a\" \"f\" (func $f (result i32))) (table $f 1 funcref) (elem (i32.const 0) $f) (export \"t\" (table 0)) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"b\" \"t\" (table $t 1 funcref)) (export \"t\" (table 0)))");
    runTest(context -> {
        final Dictionary importObject = new Dictionary();
        importObject.addMember("a", Dictionary.create(new Object[] { "f", new Executable(args -> 2) }));
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, importObject);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Object m1Table = WebAssembly.instanceExport(m1Instance, "t");
            final Object m1TableFunction = WebAssembly.tableRead((WasmTable) m1Table, 0);
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m1TableFunction);
            Object m1Value = lib.execute(m1Function);
            final Object m1TableValue = lib.execute(m1TableFunction);
            Assert.assertEquals("Return value of functions is equal", m1Value, m1TableValue);
            final Dictionary d = new Dictionary();
            d.addMember("b", Dictionary.create(new Object[] { "f", m1Function, "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);
            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 : Dictionary(org.graalvm.wasm.api.Dictionary) WasmInstance(org.graalvm.wasm.WasmInstance) 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) Executable(org.graalvm.wasm.api.Executable) ArityException(com.oracle.truffle.api.interop.ArityException) 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