use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testExportTableTwice.
@Test
public void testExportTableTwice() throws IOException, InterruptedException {
final byte[] exportMemoryTwice = compileWat("exportTableTwice", "(module (table 1 funcref) (export \"a\" (table 0)) (export \"b\" (table 0)))");
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
final InteropLibrary lib = InteropLibrary.getUncached();
try {
final Object f = new WasmFunctionInstance(context, null, new RootNode(context.language()) {
@Override
public Object execute(VirtualFrame frame) {
return 42;
}
}.getCallTarget());
final Object writeTable = wasm.readMember("table_write");
final Object readTable = wasm.readMember("table_read");
final Object a = WebAssembly.instanceExport(instance, "a");
final Object b = WebAssembly.instanceExport(instance, "b");
lib.execute(writeTable, a, 0, f);
final Object readValue = lib.execute(readTable, b, 0);
Assert.assertEquals("Written function should correspond ro read function", 42, lib.asInt(lib.execute(readValue)));
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.WasmInstance 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);
}
});
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testFuncTypeMultipleParameters.
@Test
public void testFuncTypeMultipleParameters() throws IOException, InterruptedException {
final byte[] source = compileWat("data", "(module (func (export \"func\") (param i32 i64) (result f32) f32.const 0))");
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, source, null);
final WasmFunctionInstance fn = (WasmFunctionInstance) WebAssembly.instanceExport(instance, "func");
final String fnType = WebAssembly.functionTypeToString(fn.function());
Assert.assertEquals("func_type", "0(i32 i64)f32", fnType);
});
}
use of org.graalvm.wasm.WasmInstance 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);
}
});
}
use of org.graalvm.wasm.WasmInstance 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);
}
});
}
Aggregations