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;
}
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);
}
}
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;
}
Aggregations