use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testNameSection.
@Test
public void testNameSection() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
// Should not throw an exception i.e. is a valid module
// (despite the name section may not be formed correctly).
wasm.moduleDecode(binaryWithEmptyNameSection);
wasm.moduleDecode(binaryWithTruncatedNameSection);
wasm.moduleDecode(binaryWithNameSectionWithInvalidIndex);
});
}
use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testFunctionEmbedderData.
@Test
public void testFunctionEmbedderData() throws IOException {
runTest(context -> {
WebAssembly wasm = new WebAssembly(context);
WasmInstance instance = moduleInstantiate(wasm, binaryWithExports, null);
Object fn = WebAssembly.instanceExport(instance, "main");
checkEmbedderData(fn);
});
}
use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithImportTable.
@Test
public void testInstantiateWithImportTable() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmTable table = WebAssembly.tableAlloc(4, 8);
Dictionary importObject = Dictionary.create(new Object[] { "host", Dictionary.create(new Object[] { "defaultTable", table }) });
WebAssembly.tableWrite(table, 0, new WasmFunctionInstance(context, null, new RootNode(context.language()) {
@Override
public Object execute(VirtualFrame frame) {
return 210;
}
}.getCallTarget()));
final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableImport, importObject);
try {
final Object callFirst = WebAssembly.instanceExport(instance, "callFirst");
Object result = InteropLibrary.getUncached(callFirst).execute(callFirst);
Assert.assertEquals("Must return 210.", 210, InteropLibrary.getUncached(result).asInt(result));
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testImportManyGlobals.
@Test
public void testImportManyGlobals() throws IOException, InterruptedException {
String importManyGlobalsWat = "(module\n" + "(global $global0 (import \"globals\" \"global0\") i32)\n" + "(global $global1 (import \"globals\" \"global1\") i32)\n" + "(global $global2 (import \"globals\" \"global2\") i32)\n" + "(global $global3 (import \"globals\" \"global3\") i32)\n" + "(global $global4 (import \"globals\" \"global4\") i32)\n" + "(global $global5 (import \"globals\" \"global5\") i32)\n" + "(global $global6 (import \"globals\" \"global6\") i32)\n" + "(global $global7 (import \"globals\" \"global7\") i32)\n" + "(global $global8 (import \"globals\" \"global8\") i32)\n" + "(func (export \"sum\") (result i32)\n" + " get_global $global0\n" + " get_global $global1\n" + " i32.add\n" + " get_global $global2\n" + " i32.add\n" + " get_global $global3\n" + " i32.add\n" + " get_global $global4\n" + " i32.add\n" + " get_global $global5\n" + " i32.add\n" + " get_global $global6\n" + " i32.add\n" + " get_global $global7\n" + " i32.add\n" + " get_global $global8\n" + " i32.add\n" + ")\n" + ")";
byte[] importManyGlobalsBytes = compileWat("importManyGlobals", importManyGlobalsWat);
runTest(context -> {
WebAssembly wasm = new WebAssembly(context);
Dictionary importObject = Dictionary.create(new Object[] { "globals", Dictionary.create(new Object[] { "global0", WebAssembly.globalAlloc(ValueType.i32, false, 1), "global1", WebAssembly.globalAlloc(ValueType.i32, false, 2), "global2", WebAssembly.globalAlloc(ValueType.i32, false, 3), "global3", WebAssembly.globalAlloc(ValueType.i32, false, 4), "global4", WebAssembly.globalAlloc(ValueType.i32, false, 5), "global5", WebAssembly.globalAlloc(ValueType.i32, false, 6), "global6", WebAssembly.globalAlloc(ValueType.i32, false, 7), "global7", WebAssembly.globalAlloc(ValueType.i32, false, 8), "global8", WebAssembly.globalAlloc(ValueType.i32, false, 9) }) });
WasmInstance instance = moduleInstantiate(wasm, importManyGlobalsBytes, importObject);
try {
InteropLibrary lib = InteropLibrary.getUncached();
Object sum = lib.execute(WebAssembly.instanceExport(instance, "sum"));
int intSum = lib.asInt(sum);
Assert.assertEquals("Incorrect sum of imported globals", 45, intSum);
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testExportSameFunctionWithDifferentNames.
@Test
public void testExportSameFunctionWithDifferentNames() throws IOException, InterruptedException {
final byte[] sameFunctionWithDifferentNames = compileWat("sameFunctionWithDifferentNames", "(module (func $f (result i32) i32.const 1) (export \"f1\" (func $f)) (export \"f2\" (func $f)))");
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, sameFunctionWithDifferentNames, null);
final Object f1 = WebAssembly.instanceExport(instance, "f1");
final Object f2 = WebAssembly.instanceExport(instance, "f2");
Assert.assertTrue("Returned function instances must be reference equal", f1 == f2);
});
}
Aggregations