Search in sources :

Example 1 with Executable

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

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

Example 3 with Executable

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

the class WasmJsApiSuite method testExportImportedFunctionInDifferentModules.

@Test
public void testExportImportedFunctionInDifferentModules() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (import \"a\" \"f\" (func $f (result i32))) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"b\" \"f\" (func $f (result i32))) (func (result i32) (call $f)) (export \"f\" (func $f)))");
    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 Dictionary d = new Dictionary();
            d.addMember("b", 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 : 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

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 WasmInstance (org.graalvm.wasm.WasmInstance)3 Dictionary (org.graalvm.wasm.api.Dictionary)3 Executable (org.graalvm.wasm.api.Executable)3 WebAssembly (org.graalvm.wasm.api.WebAssembly)3 Test (org.junit.Test)3 ArityException (com.oracle.truffle.api.interop.ArityException)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)2 InteropException (com.oracle.truffle.api.interop.InteropException)1