Search in sources :

Example 1 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class EmscriptenGetHeapSizeNode method executeWithContext.

@Override
public Object executeWithContext(VirtualFrame frame, WasmContext context) {
    WasmMemory memory = instance.memory();
    final long byteSize = memory.byteSize();
    return (int) byteSize;
}
Also used : WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 2 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class SetErrNoNode method executeWithContext.

@Override
public Object executeWithContext(VirtualFrame frame, WasmContext context) {
    Object[] args = frame.getArguments();
    assert args.length == 1;
    int value = (int) args[0];
    // TODO: Get address (3120) via call to `___errno_location` WebAssembly function.
    WasmMemory memory = instance.memory();
    memory.store_i32(this, 3120, value);
    return value;
}
Also used : WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 3 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory 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 4 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class EmscriptenMemcpyBigNode method executeWithContext.

@Override
public Object executeWithContext(VirtualFrame frame, WasmContext context) {
    Object[] args = frame.getArguments();
    assert args.length == 3;
    int dest = (int) args[0];
    int src = (int) args[1];
    int num = (int) args[2];
    WasmMemory memory = instance.memory();
    memory.copy(this, src, dest, num);
    return 0;
}
Also used : WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 5 with WasmMemory

use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.

the class WasmJsApiSuite method testMemoryGrowLimit.

@Test
public void testMemoryGrowLimit() throws IOException {
    runTest(context -> {
        try {
            WasmMemory memory = WebAssembly.memAlloc(1, 1);
            WebAssembly.memGrow(memory, 1);
            Assert.fail("Should have failed - try to grow memory beyond max size");
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmMemory(org.graalvm.wasm.memory.WasmMemory) Test(org.junit.Test)

Aggregations

WasmMemory (org.graalvm.wasm.memory.WasmMemory)23 ByteArrayWasmMemory (org.graalvm.wasm.memory.ByteArrayWasmMemory)6 UnsafeWasmMemory (org.graalvm.wasm.memory.UnsafeWasmMemory)6 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)4 Test (org.junit.Test)4 WasmFunction (org.graalvm.wasm.WasmFunction)3 WasmInstance (org.graalvm.wasm.WasmInstance)3 WasmTable (org.graalvm.wasm.WasmTable)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 GlobalRegistry (org.graalvm.wasm.GlobalRegistry)2 ExportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ExportMemorySym)2 ImportMemorySym (org.graalvm.wasm.Linker.ResolutionDag.ImportMemorySym)2 SymbolTable (org.graalvm.wasm.SymbolTable)2 WasmContext (org.graalvm.wasm.WasmContext)2 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)2