use of org.graalvm.wasm.WasmFunctionInstance in project graal by oracle.
the class WasmBlockNode method executeDirectCall.
@BytecodeInterpreterSwitchBoundary
private Object executeDirectCall(int childrenOffset, WasmFunction function, Object[] args) {
final boolean imported = function.isImported();
CompilerAsserts.partialEvaluationConstant(imported);
DirectCallNode callNode = (DirectCallNode) children[childrenOffset];
assert assertDirectCall(function, callNode);
if (imported) {
WasmFunctionInstance functionInstance = instance().functionInstance(function.index());
TruffleContext truffleContext = functionInstance.getTruffleContext();
Object prev = truffleContext.enter(this);
try {
return callNode.call(args);
} finally {
truffleContext.leave(this, prev);
}
} else {
return callNode.call(args);
}
}
use of org.graalvm.wasm.WasmFunctionInstance 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.WasmFunctionInstance 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.WasmFunctionInstance 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.WasmFunctionInstance 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);
}
});
}
Aggregations