Skip to content

Commit 5706b06

Browse files
QuLogicdeadprogram
authored andcommitted
wasm_exec: Add copyBytesToGo.
This is basically just a copy of copyBytesToJS, but with arguments reversed.
1 parent 4347496 commit 5706b06

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

targets/wasm_exec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,23 @@
393393
// mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
394394
//},
395395

396+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
397+
"syscall/js.copyBytesToGo": (ret_addr, dest_addr, dest_len, dest_cap, source_addr) => {
398+
let num_bytes_copied_addr = ret_addr;
399+
let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable
400+
401+
const dst = loadSlice(dest_addr, dest_len);
402+
const src = loadValue(source_addr);
403+
if (!(src instanceof Uint8Array)) {
404+
mem().setUint8(returned_status_addr, 0); // Return "not ok" status
405+
return;
406+
}
407+
const toCopy = src.subarray(0, dst.length);
408+
dst.set(toCopy);
409+
setInt64(num_bytes_copied_addr, toCopy.length);
410+
mem().setUint8(returned_status_addr, 1); // Return "ok" status
411+
},
412+
396413
// copyBytesToJS(dst ref, src []byte) (int, bool)
397414
// Originally copied from upstream Go project, then modified:
398415
// https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416

0 commit comments

Comments
 (0)