use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class EmscriptenModule method createInstance.
@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), NUMBER_OF_FUNCTIONS);
final WasmInstance testutil = context.moduleInstances().get("testutil");
if (testutil != null) {
// re-export some symbols from the testutil module.
if (testutil.symbolTable().function(TestutilModule.Names.SAVE_BINARY_FILE) != null) {
importFunction(instance, "testutil", TestutilModule.Names.SAVE_BINARY_FILE, types(I32_TYPE, I32_TYPE, I32_TYPE), types(), "" + TestutilModule.Names.SAVE_BINARY_FILE);
}
}
defineFunction(instance, "abort", types(), types(), new AbortNode(language, instance));
defineFunction(instance, "abortOnCannotGrowMemory", types(I32_TYPE), types(I32_TYPE), new AbortOnCannotGrowMemoryNode(language, instance));
defineFunction(instance, "segfault", types(), types(), new SegfaultNode(language, instance));
defineFunction(instance, "alignfault", types(), types(), new AlignfaultNode(language, instance));
defineFunction(instance, "emscripten_memcpy_big", types(I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new EmscriptenMemcpyBigNode(language, instance));
defineFunction(instance, "emscripten_get_heap_size", types(), types(I32_TYPE), new EmscriptenGetHeapSizeNode(language, instance));
defineFunction(instance, "emscripten_resize_heap", types(I32_TYPE), types(I32_TYPE), new EmscriptenResizeHeapNode(language, instance));
defineFunction(instance, "gettimeofday", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new GetTimeOfDayNode(language, instance));
defineFunction(instance, "llvm_exp2_f64", types(F64_TYPE), types(F64_TYPE), new LLVMExp2F64Node(language, instance));
defineFunction(instance, "__wasi_fd_write", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdWriteNode(language, instance));
defineFunction(instance, "__lock", types(I32_TYPE), types(), new LockNode(language, instance));
defineFunction(instance, "__unlock", types(I32_TYPE), types(), new UnlockNode(language, instance));
defineFunction(instance, "__setErrNo", types(I32_TYPE), types(), new SetErrNoNode(language, instance));
defineFunction(instance, "__syscall140", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new UnimplementedNode("__syscall140", language, instance));
defineFunction(instance, "__syscall146", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new UnimplementedNode("__syscall146", language, instance));
defineFunction(instance, "__syscall54", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new UnimplementedNode("__syscall54", language, instance));
defineFunction(instance, "__syscall6", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new UnimplementedNode("__syscall6", language, instance));
defineFunction(instance, "setTempRet0", types(I32_TYPE), types(), new UnimplementedNode("setTempRet0", language, instance));
defineGlobal(instance, "_table_base", I32_TYPE, (byte) GlobalModifier.CONSTANT, 0);
defineGlobal(instance, "_memory_base", I32_TYPE, (byte) GlobalModifier.CONSTANT, 0);
defineGlobal(instance, "DYNAMICTOP_PTR", I32_TYPE, (byte) GlobalModifier.CONSTANT, 0);
defineGlobal(instance, "DYNAMIC_BASE", I32_TYPE, (byte) GlobalModifier.CONSTANT, 0);
defineTable(instance, "table", 0, -1, ReferenceTypes.FUNCREF);
return instance;
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class SpectestModule method createInstance.
@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
WasmInstance module = new WasmInstance(context, WasmModule.createBuiltin(name), NUMBER_OF_FUNCTIONS);
defineFunction(module, "print", types(), types(), new PrintNode(language, module));
defineFunction(module, "print_i32", types(I32_TYPE), types(), new PrintNode(language, module));
defineFunction(module, "print_i64", types(I64_TYPE), types(), new PrintNode(language, module));
defineFunction(module, "print_f32", types(F32_TYPE), types(), new PrintNode(language, module));
defineFunction(module, "print_f64", types(F64_TYPE), types(), new PrintNode(language, module));
defineFunction(module, "print_i32_f32", types(I32_TYPE, F32_TYPE), types(), new PrintNode(language, module));
defineFunction(module, "print_f64_f64", types(F64_TYPE, F64_TYPE), types(), new PrintNode(language, module));
defineGlobal(module, "global_i32", I32_TYPE, (byte) GlobalModifier.CONSTANT, 666);
defineGlobal(module, "global_i64", I64_TYPE, (byte) GlobalModifier.CONSTANT, 666L);
defineGlobal(module, "global_f32", F32_TYPE, (byte) GlobalModifier.CONSTANT, Float.floatToRawIntBits(666.0f));
defineGlobal(module, "global_f64", F64_TYPE, (byte) GlobalModifier.CONSTANT, Double.doubleToRawLongBits(666.0));
defineTable(module, "table", 10, 20, ReferenceTypes.FUNCREF);
defineMemory(module, "memory", 1, 2);
return module;
}
use of org.graalvm.wasm.WasmInstance 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.WasmInstance 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.WasmInstance 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());
}
});
}
Aggregations