sysjs: Implement Object.copyBytes to copy over a slice to Js array type objects
It works with Uint8Array objects only but no type checking is done right now. Originally authored by iddev5 :) Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
28fb75c7eb
commit
3e69c54e79
2 changed files with 19 additions and 0 deletions
|
|
@ -40,6 +40,10 @@ class MemoryBlock {
|
||||||
return this.getMemory().getFloat64(offset, true);
|
return this.getMemory().getFloat64(offset, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getSlice(offset, len) {
|
||||||
|
return new Uint8Array(this.mem, offset, len);
|
||||||
|
}
|
||||||
|
|
||||||
getString(offset, len) {
|
getString(offset, len) {
|
||||||
return text_decoder.decode(new Uint8Array(this.mem, offset, len));
|
return text_decoder.decode(new Uint8Array(this.mem, offset, len));
|
||||||
}
|
}
|
||||||
|
|
@ -252,6 +256,16 @@ const zig = {
|
||||||
delete values[id][index];
|
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) {
|
zigGetAttributeCount(id) {
|
||||||
let obj = values[id];
|
let obj = values[id];
|
||||||
return Object.keys(obj).length;
|
return Object.keys(obj).length;
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ const js = struct {
|
||||||
extern fn zigValueEqual(val: *const anyopaque, other: *const anyopaque) bool;
|
extern fn zigValueEqual(val: *const anyopaque, other: *const anyopaque) bool;
|
||||||
extern fn zigValueInstanceOf(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 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 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 zigFunctionInvoke(id: u64, args: ?*const anyopaque, args_len: u32, ret_ptr: *anyopaque) void;
|
||||||
extern fn zigGetParamCount(id: u64) u32;
|
extern fn zigGetParamCount(id: u64) u32;
|
||||||
|
|
@ -140,6 +141,10 @@ pub const Object = struct {
|
||||||
js.zigDeleteIndex(obj.ref, index);
|
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 {
|
pub fn call(obj: *const Object, fun: []const u8, args: []const Value) Value {
|
||||||
var ret: Value = undefined;
|
var ret: Value = undefined;
|
||||||
js.zigFunctionCall(obj.ref, fun.ptr, @intCast(u32, fun.len), args.ptr, @intCast(u32, args.len), &ret);
|
js.zigFunctionCall(obj.ref, fun.ptr, @intCast(u32, fun.len), args.ptr, @intCast(u32, args.len), &ret);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue