use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method globalRead.
private static Object globalRead(Object[] args) {
checkArgumentCount(args, 1);
if (!(args[0] instanceof WasmGlobal)) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm global");
}
WasmGlobal global = (WasmGlobal) args[0];
return globalRead(global);
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method instanceExport.
public static Object instanceExport(WasmInstance instance, String name) {
CompilerAsserts.neverPartOfCompilation();
WasmFunction function = instance.module().exportedFunctions().get(name);
Integer globalIndex = instance.module().exportedGlobals().get(name);
if (function != null) {
return instance.functionInstance(function);
} else if (globalIndex != null) {
final int index = globalIndex;
final int address = instance.globalAddress(index);
if (address < 0) {
return instance.context().globals().externalGlobal(address);
} else {
final ValueType valueType = ValueType.fromByteValue(instance.symbolTable().globalValueType(index));
final boolean mutable = instance.symbolTable().isGlobalMutable(index);
return new ExportedWasmGlobal(valueType, mutable, instance.context().globals(), address);
}
} else if (instance.module().exportedMemoryNames().contains(name)) {
return instance.memory();
} else if (instance.module().exportedTableNames().contains(name)) {
return instance.table();
} else {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, name + " is not a exported name of the given instance");
}
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method tableSize.
private static Object tableSize(Object[] args) {
checkArgumentCount(args, 1);
if (!(args[0] instanceof WasmTable)) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm table");
}
WasmTable table = (WasmTable) args[0];
return tableSize(table);
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method globalAlloc.
private static Object globalAlloc(Object[] args) {
checkArgumentCount(args, 3);
ValueType valueType;
try {
String valueTypeString = InteropLibrary.getUncached().asString(args[0]);
valueType = ValueType.valueOf(valueTypeString);
} catch (UnsupportedMessageException ex) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument (value type) must be convertible to String");
} catch (IllegalArgumentException ex) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Invalid value type");
}
boolean mutable;
try {
mutable = InteropLibrary.getUncached().asBoolean(args[1]);
} catch (UnsupportedMessageException ex) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument (mutable) must be convertible to boolean");
}
return globalAlloc(valueType, mutable, args[2]);
}
use of org.graalvm.wasm.exception.WasmJsApiException 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