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:
Stephen Gutekanst 2022-08-27 12:02:30 -07:00 committed by Stephen Gutekanst
parent 28fb75c7eb
commit 3e69c54e79
2 changed files with 19 additions and 0 deletions

View file

@ -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;