use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method toSizeLimits.
private static int[] toSizeLimits(Object[] args) {
if (args.length == 0) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Initial argument is required");
}
int initial;
try {
initial = InteropLibrary.getUncached().asInt(args[0]);
} catch (UnsupportedMessageException ex) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Initial argument must be convertible to int");
}
int maximum;
if (args.length == 1) {
maximum = -1;
} else {
try {
maximum = InteropLibrary.getUncached().asInt(args[1]);
} catch (UnsupportedMessageException ex) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Maximum argument must be convertible to int");
}
}
return new int[] { initial, maximum };
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method tableWrite.
private static Object tableWrite(Object[] args) {
checkArgumentCount(args, 3);
if (!(args[0] instanceof WasmTable)) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm table");
}
if (!(args[1] instanceof Integer)) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Second argument must be integer");
}
WasmTable table = (WasmTable) args[0];
int index = (Integer) args[1];
return tableWrite(table, index, args[2]);
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method globalWrite.
private static Object globalWrite(Object[] args) {
checkArgumentCount(args, 2);
if (!(args[0] instanceof WasmGlobal)) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm global");
}
WasmGlobal global = (WasmGlobal) args[0];
return globalWrite(global, args[1]);
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method cannotConvertToBytesError.
private static WasmJsApiException cannotConvertToBytesError(Throwable cause) {
WasmJsApiException.Kind kind = WasmJsApiException.Kind.TypeError;
String message = "Cannot convert to bytes";
return (cause == null) ? new WasmJsApiException(kind, message) : new WasmJsApiException(kind, message, cause);
}
use of org.graalvm.wasm.exception.WasmJsApiException in project graal by oracle.
the class WebAssembly method readModuleImports.
private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
CompilerAsserts.neverPartOfCompilation();
final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
if (imports.getArraySize() != 0 && importObject == null) {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
}
HashMap<String, ImportModule> importModules = new HashMap<>();
final InteropLibrary lib = InteropLibrary.getUncached();
try {
int i = 0;
while (i < imports.getArraySize()) {
final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
final Object importedModule = getMember(importObject, d.module());
final Object member = getMember(importedModule, d.name());
switch(d.kind()) {
case function:
if (!lib.isExecutable(member)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
}
WasmFunction f = module.importedFunction(d.name());
ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
break;
case memory:
if (!(member instanceof WasmMemory)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
}
ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
break;
case table:
if (!(member instanceof WasmTable)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
}
ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
break;
case global:
if (!(member instanceof WasmGlobal)) {
throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
}
ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
break;
default:
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
}
i += 1;
}
} catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
}
return importModules;
}
Aggregations