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);
}
});
}
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);
}
});
}
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);
});
}
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);
}
});
}
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);
}
});
}
Aggregations