use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testExportMemoryTwice.
@Test
public void testExportMemoryTwice() throws IOException, InterruptedException {
final byte[] exportMemoryTwice = compileWat("exportMemoryTwice", "(memory 1) (export \"a\" (memory 0)) (export \"b\" (memory 0))");
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
try {
final InteropLibrary lib = InteropLibrary.getUncached();
final Object memoryABuffer = WebAssembly.instanceExport(instance, "a");
final Object memoryBBuffer = WebAssembly.instanceExport(instance, "b");
lib.writeArrayElement(memoryABuffer, 0, (byte) 42);
final byte readValue = lib.asByte(lib.readArrayElement(memoryBBuffer, 0));
Assert.assertEquals("Written value should correspond to read value", (byte) 42, readValue);
} catch (UnsupportedMessageException | UnsupportedTypeException | InvalidArrayIndexException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testTableInstanceGrowLimit.
@Test
public void testTableInstanceGrowLimit() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
final InteropLibrary lib = InteropLibrary.getUncached();
// We should be able to grow the table to 10,000,000.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object grow = wasm.readMember("table_grow");
lib.execute(grow, table, 9999999);
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
}
// But growing to 10,000,001 should fail.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object grow = wasm.readMember("table_grow");
lib.execute(grow, table, 1);
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.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testFuncTypeExport.
@Test
public void testFuncTypeExport() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
try {
final Object funcType = wasm.readMember("func_type");
final Object fn = WebAssembly.instanceExport(instance, "readZero");
Assert.assertEquals("func_type", "0()i32", InteropLibrary.getUncached(funcType).execute(funcType, fn));
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.WasmInstance 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.WasmInstance in project graal by oracle.
the class WasiModule method createInstance.
@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), NUMBER_OF_FUNCTIONS);
importMemory(instance, "main", "memory", 0, MAX_MEMORY_DECLARATION_SIZE);
defineFunction(instance, "args_sizes_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiArgsSizesGetNode(language, instance));
defineFunction(instance, "args_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiArgsGetNode(language, instance));
defineFunction(instance, "environ_sizes_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiEnvironSizesGetNode(language, instance));
defineFunction(instance, "environ_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiEnvironGetNode(language, instance));
defineFunction(instance, "clock_time_get", types(I32_TYPE, I64_TYPE, I32_TYPE), types(I32_TYPE), new WasiClockTimeGetNode(language, instance));
defineFunction(instance, "proc_exit", types(I32_TYPE), types(), new WasiProcExitNode(language, instance));
defineFunction(instance, "fd_write", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdWriteNode(language, instance));
defineFunction(instance, "fd_read", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdReadNode(language, instance));
defineFunction(instance, "fd_close", types(I32_TYPE), types(I32_TYPE), new WasiFdCloseNode(language, instance));
defineFunction(instance, "fd_seek", types(I32_TYPE, I64_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdSeekNode(language, instance));
defineFunction(instance, "fd_fdstat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFdstatGetNode(language, instance));
defineFunction(instance, "fd_fdstat_set_flags", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFdstatSetFlagsNode(language, instance));
defineFunction(instance, "fd_prestat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdPrestatGetNode(language, instance));
defineFunction(instance, "fd_prestat_dir_name", types(I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdPrestatDirNameNode(language, instance));
defineFunction(instance, "fd_filestat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFilestatGetNode(language, instance));
defineFunction(instance, "path_open", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE, I64_TYPE, I64_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiPathOpenNode(language, instance));
return instance;
}
Aggregations