diff --git a/libs/sysjs/src/mach-sysjs.js b/libs/sysjs/src/mach-sysjs.js index 8c742f43..fd177130 100644 --- a/libs/sysjs/src/mach-sysjs.js +++ b/libs/sysjs/src/mach-sysjs.js @@ -40,6 +40,10 @@ class MemoryBlock { return this.getMemory().getFloat64(offset, true); } + getSlice(offset, len) { + return new Uint8Array(this.mem, offset, len); + } + getString(offset, len) { return text_decoder.decode(new Uint8Array(this.mem, offset, len)); } @@ -252,6 +256,16 @@ const zig = { delete values[id][index]; }, + zigCopyBytes(id, bytes, expected_length) { + let memory = new MemoryBlock(zig.wasm.exports.memory.buffer); + const array = values[id]; + if (array.length != expected_length) { + throw Error("copyBytes given array of length " + expected_length + " but destination has length " + array.length); + } + const slice = memory.getSlice(bytes, array.length); + array.set(slice); + }, + zigGetAttributeCount(id) { let obj = values[id]; return Object.keys(obj).length; diff --git a/libs/sysjs/src/main.zig b/libs/sysjs/src/main.zig index eafb75d5..77b4747e 100644 --- a/libs/sysjs/src/main.zig +++ b/libs/sysjs/src/main.zig @@ -18,6 +18,7 @@ const js = struct { extern fn zigValueEqual(val: *const anyopaque, other: *const anyopaque) bool; extern fn zigValueInstanceOf(val: *const anyopaque, other: *const anyopaque) bool; extern fn zigDeleteIndex(id: u64, index: u32) void; + extern fn zigCopyBytes(id: u64, bytes: [*]u8, expected_len: u32) void; extern fn zigFunctionCall(id: u64, name: [*]const u8, len: u32, args: ?*const anyopaque, args_len: u32, ret_ptr: *anyopaque) void; extern fn zigFunctionInvoke(id: u64, args: ?*const anyopaque, args_len: u32, ret_ptr: *anyopaque) void; extern fn zigGetParamCount(id: u64) u32; @@ -140,6 +141,10 @@ pub const Object = struct { js.zigDeleteIndex(obj.ref, index); } + pub fn copyBytes(obj: *const Object, bytes: []u8) void { + js.zigCopyBytes(obj.ref, bytes.ptr, bytes.len); + } + pub fn call(obj: *const Object, fun: []const u8, args: []const Value) Value { var ret: Value = undefined; js.zigFunctionCall(obj.ref, fun.ptr, @intCast(u32, fun.len), args.ptr, @intCast(u32, args.len), &ret);