Search in sources :

Example 21 with WasmMemory

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

the class GetTimeOfDayNode method executeWithContext.

@Override
public Object executeWithContext(VirtualFrame frame, WasmContext context) {
    Object[] args = frame.getArguments();
    assert args.length == 2;
    int ptr = (int) args[0];
    long now = getCurrentTime();
    WasmMemory memory = instance.memory();
    memory.store_i32(this, ptr, (int) (now / 1000));
    memory.store_i32(this, ptr + 4, (int) (now % 1000 * 1000));
    return 0;
}
Also used : WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 22 with WasmMemory

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

the class SaveBinaryFileNode method saveFile.

@CompilerDirectives.TruffleBoundary
private void saveFile(int filenamePtr, int dataPtr, int size) {
    final WasmContext context = getContext();
    Assert.assertIntLessOrEqual(context.memories().count(), 1, "Currently, dumping works with only 1 memory.", Failure.UNSPECIFIED_MALFORMED);
    final WasmMemory memory = context.memories().memory(0);
    // Read the file name.
    String filename = readFileName(memory, filenamePtr);
    final Path temporaryFile = temporaryDirectory.resolve(filename);
    if (!TestutilModule.Options.KEEP_TEMP_FILES.equals("true")) {
        temporaryFile.toFile().deleteOnExit();
    }
    // Read the byte array.
    byte[] bytes = new byte[size];
    for (int i = 0; i < size; i++) {
        bytes[i] = (byte) memory.load_i32_8u(this, dataPtr + i);
    }
    // Store the byte array to a temporary file.
    try (FileOutputStream stream = new FileOutputStream(temporaryFile.toFile())) {
        stream.write(bytes);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) WasmContext(org.graalvm.wasm.WasmContext) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) WasmMemory(org.graalvm.wasm.memory.WasmMemory)

Example 23 with WasmMemory

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

the class ImportModule method createInstance.

@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
    WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), functions.size());
    for (Map.Entry<String, Pair<WasmFunction, Object>> entry : functions.entrySet()) {
        final String functionName = entry.getKey();
        final Pair<WasmFunction, Object> info = entry.getValue();
        final WasmFunction function = info.getLeft();
        final SymbolTable.FunctionType type = function.type();
        if (info.getRight() instanceof WasmFunctionInstance) {
            defineExportedFunction(instance, functionName, type.paramTypes(), type.returnTypes(), (WasmFunctionInstance) info.getRight());
        } else {
            defineFunction(instance, functionName, type.paramTypes(), type.returnTypes(), new ExecuteInParentContextNode(context.language(), instance, info.getRight()));
        }
    }
    for (Map.Entry<String, WasmMemory> entry : memories.entrySet()) {
        final String memoryName = entry.getKey();
        final WasmMemory memory = entry.getValue();
        defineExternalMemory(instance, memoryName, memory);
    }
    for (Map.Entry<String, WasmTable> entry : tables.entrySet()) {
        final String tableName = entry.getKey();
        final WasmTable table = entry.getValue();
        defineExternalTable(instance, tableName, table);
    }
    for (Map.Entry<String, WasmGlobal> entry : globals.entrySet()) {
        final String globalName = entry.getKey();
        final WasmGlobal global = entry.getValue();
        defineExternalGlobal(instance, globalName, global);
    }
    return instance;
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmTable(org.graalvm.wasm.WasmTable) SymbolTable(org.graalvm.wasm.SymbolTable) WasmFunction(org.graalvm.wasm.WasmFunction) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) WasmMemory(org.graalvm.wasm.memory.WasmMemory) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.graalvm.collections.Pair)

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