Search in sources :

Example 1 with Dictionary

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

use of org.graalvm.wasm.api.Dictionary 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 3 with Dictionary

use of org.graalvm.wasm.api.Dictionary 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 4 with Dictionary

use of org.graalvm.wasm.api.Dictionary 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 5 with Dictionary

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

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 Dictionary (org.graalvm.wasm.api.Dictionary)13 WebAssembly (org.graalvm.wasm.api.WebAssembly)13 WasmInstance (org.graalvm.wasm.WasmInstance)12 Test (org.junit.Test)12 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)8 InteropException (com.oracle.truffle.api.interop.InteropException)7 ArityException (com.oracle.truffle.api.interop.ArityException)5 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)5 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)5 Executable (org.graalvm.wasm.api.Executable)3 WasmModule (org.graalvm.wasm.WasmModule)2 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)1 WasmTable (org.graalvm.wasm.WasmTable)1 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)1 WasmMemory (org.graalvm.wasm.memory.WasmMemory)1