use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithExportTable.
@Test
public void testInstantiateWithExportTable() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableExport, null);
try {
final WasmTable table = (WasmTable) WebAssembly.instanceExport(instance, "defaultTable");
final Object result = InteropLibrary.getUncached().execute(WebAssembly.tableRead(table, 0), 9);
Assert.assertEquals("Must be 81.", 81, result);
} catch (UnsupportedTypeException | UnsupportedMessageException | ArityException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.api.WebAssembly 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.api.WebAssembly 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.api.WebAssembly 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.api.WebAssembly 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());
}
});
}
Aggregations