use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithExportMemory.
@Test
public void testInstantiateWithExportMemory() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
try {
final WasmMemory memory = (WasmMemory) WebAssembly.instanceExport(instance, "memory");
final Object readZero = WebAssembly.instanceExport(instance, "readZero");
memory.store_i32(null, 0, 174);
final Object result = InteropLibrary.getUncached(readZero).execute(readZero);
Assert.assertEquals("Must be 174.", 174, 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 checkInstantiateWithImportGlobal.
private static void checkInstantiateWithImportGlobal(byte[] binaryWithGlobalImport, String globalType, Object globalValue) throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmGlobal global = WebAssembly.globalAlloc(ValueType.valueOf(globalType), false, globalValue);
Dictionary importObject = Dictionary.create(new Object[] { "host", Dictionary.create(new Object[] { "defaultGlobal", global }) });
final WasmInstance instance = moduleInstantiate(wasm, binaryWithGlobalImport, importObject);
try {
InteropLibrary interop = InteropLibrary.getUncached();
final Object readGlobal1 = WebAssembly.instanceExport(instance, "readGlobal1");
final Object readGlobal2 = WebAssembly.instanceExport(instance, "readGlobal2");
final Object result1 = interop.execute(readGlobal1);
final Object result2 = interop.execute(readGlobal2);
Assert.assertEquals("Must be " + globalValue + " initially.", globalValue, result1);
Assert.assertEquals("Must be " + globalValue + " initially.", globalValue, result2);
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithUnicodeExport.
@Test
public void testInstantiateWithUnicodeExport() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithUnicodeExport, null);
try {
final Object euroSignFn = WebAssembly.instanceExport(instance, "\u20AC");
final Object result = InteropLibrary.getUncached(euroSignFn).execute(euroSignFn);
Assert.assertEquals("Result should be 42", 42, 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 testTableInstanceOutOfBoundsGet.
@Test
public void testTableInstanceOutOfBoundsGet() 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 get element 1.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object readTable = wasm.readMember("table_read");
lib.execute(readTable, table, 0);
} catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
throw new RuntimeException(e);
}
// But not element 2.
try {
final Object table = WebAssembly.instanceExport(instance, "t");
final Object readTable = wasm.readMember("table_read");
lib.execute(readTable, 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 testExportSameFunctionInAndOutsideTable.
@Test
public void testExportSameFunctionInAndOutsideTable() throws IOException, InterruptedException {
final byte[] sameFunctionInExportAndTable = compileWat("sameFunctionInExportAndTable", "(module (func $f (result i32) i32.const 1) (table 1 funcref) (elem (i32.const 0) $f) (export \"f\" (func $f)) (export \"t\" (table 0)))");
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, sameFunctionInExportAndTable, null);
final Object f = WebAssembly.instanceExport(instance, "f");
final WasmTable t = (WasmTable) WebAssembly.instanceExport(instance, "t");
final Object fInTable = WebAssembly.tableRead(t, 0);
Assert.assertTrue("Returned function instances must be reference equal", f == fInTable);
});
}
Aggregations