use of org.graalvm.wasm.api.WebAssembly 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.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testCompile.
@Test
public void testCompile() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmModule module = wasm.moduleDecode(binaryWithExports);
try {
HashMap<String, ModuleExportDescriptor> exports = new HashMap<>();
int i = 0;
while (i < WebAssembly.moduleExports(module).getArraySize()) {
final ModuleExportDescriptor d = (ModuleExportDescriptor) WebAssembly.moduleExports(module).readArrayElement(i);
exports.put(d.name(), d);
i++;
}
Assert.assertEquals("Should export main.", ImportExportKind.function, exports.get("main").kind());
Assert.assertEquals("Should export memory.", ImportExportKind.memory, exports.get("memory").kind());
Assert.assertEquals("Should export global __heap_base.", ImportExportKind.global, exports.get("__heap_base").kind());
Assert.assertEquals("Should export global __data_end.", ImportExportKind.global, exports.get("__data_end").kind());
Assert.assertEquals("Should have empty imports.", 0L, WebAssembly.moduleImports(module).getArraySize());
} catch (InvalidArrayIndexException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.api.WebAssembly 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.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testTableInstanceOutOfBoundsSet.
@Test
public void testTableInstanceOutOfBoundsSet() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
final InteropLibrary lib = InteropLibrary.getUncached();
WasmContext wasmContext = WasmContext.get(null);
final WasmFunctionInstance functionInstance = new WasmFunctionInstance(wasmContext, null, new RootNode(wasmContext.language()) {
@Override
public Object execute(VirtualFrame frame) {
return 42;
}
}.getCallTarget());
// We should be able to set element 1.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object writeTable = wasm.readMember("table_write");
lib.execute(writeTable, table, 0, functionInstance);
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
}
// But not element 2.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object writeTable = wasm.readMember("table_write");
lib.execute(writeTable, table, 1, functionInstance);
Assert.fail("Should have failed - export count exceeds the limit");
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
} catch (WasmJsApiException e) {
Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
}
});
}
use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method runValidationValid.
private static void runValidationValid(byte[] data) throws IOException {
runTest(context -> {
WebAssembly wasm = new WebAssembly(context);
Assert.assertTrue("Should have failed - valid module", wasm.moduleValidate(data));
});
}
Aggregations