use of org.graalvm.wasm.globals.WasmGlobal 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.globals.WasmGlobal 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;
}
use of org.graalvm.wasm.globals.WasmGlobal 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.globals.WasmGlobal in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithExportGlobal.
@Test
public void testInstantiateWithExportGlobal() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmInstance instance = moduleInstantiate(wasm, binaryWithGlobalExport, null);
try {
final WasmGlobal global = (WasmGlobal) WebAssembly.instanceExport(instance, "exportedGlobal");
Assert.assertEquals("Exported global must be 1096.", 1096, global.loadAsInt());
final Object setGlobal = WebAssembly.instanceExport(instance, "setGlobal");
final Object getGlobal = WebAssembly.instanceExport(instance, "getGlobal");
InteropLibrary interop = InteropLibrary.getUncached();
final Object result1 = interop.execute(getGlobal);
Assert.assertEquals("Must be 2345 initially.", 2345, interop.asInt(result1));
interop.execute(setGlobal, 25);
final Object result2 = interop.execute(getGlobal);
Assert.assertEquals("Must be 25 later.", 25, interop.asInt(result2));
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.globals.WasmGlobal in project graal by oracle.
the class GlobalRegistry method storeLong.
public void storeLong(int address, long value) {
if (address < 0) {
final WasmGlobal global = externalGlobals[-address - 1];
global.storeLong(value);
} else {
globals[address] = value;
}
}
Aggregations