Skip to content

Commit c12c0af

Browse files
committed
wasm: add js.copyBytesToJS()
1 parent 978a0cd commit c12c0af

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

targets/wasm_exec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@
319319
//"syscall/js.valueInstanceOf": (sp) => {
320320
// mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
321321
//},
322+
323+
// copyBytesToJS(dst ref, src []byte) (int, bool)
324+
// Originally copied from upstream Go project, then modified:
325+
// https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416
326+
"syscall/js.copyBytesToJS": (ret_addr, dest_addr, source_addr, source_len, source_cap) => {
327+
let num_bytes_copied_addr = ret_addr;
328+
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
329+
330+
const dst = loadValue(dest_addr);
331+
const src = loadSlice(source_addr);
332+
if (!(dst instanceof Uint8Array)) {
333+
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
334+
return;
335+
}
336+
const toCopy = src.subarray(0, dst.length);
337+
dst.set(toCopy);
338+
setInt64(num_bytes_copied_addr, toCopy.length);
339+
mem().setUint8(returned_status_addr, 1); // Return "ok" status
340+
},
322341
}
323342
};
324343
}

0 commit comments

Comments
 (0)