Search in sources :

Example 1 with WasmGlobal

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]);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal)

Example 2 with WasmGlobal

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;
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) HashMap(java.util.HashMap) WasmTable(org.graalvm.wasm.WasmTable) WasmFunction(org.graalvm.wasm.WasmFunction) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 3 with WasmGlobal

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);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal)

Example 4 with WasmGlobal

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);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 5 with WasmGlobal

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;
    }
}
Also used : WasmGlobal(org.graalvm.wasm.globals.WasmGlobal)

Aggregations

WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)8 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)3 WasmInstance (org.graalvm.wasm.WasmInstance)3 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)3 DefaultWasmGlobal (org.graalvm.wasm.globals.DefaultWasmGlobal)3 ExportedWasmGlobal (org.graalvm.wasm.globals.ExportedWasmGlobal)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 HashMap (java.util.HashMap)2 WasmFunction (org.graalvm.wasm.WasmFunction)2 WasmTable (org.graalvm.wasm.WasmTable)2 WebAssembly (org.graalvm.wasm.api.WebAssembly)2 WasmMemory (org.graalvm.wasm.memory.WasmMemory)2 Test (org.junit.Test)2 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Map (java.util.Map)1 Pair (org.graalvm.collections.Pair)1 SymbolTable (org.graalvm.wasm.SymbolTable)1