From aeb7539f67fce7e32a19bbaa27685b6e288db581 Mon Sep 17 00:00:00 2001 From: Justin Clift Date: Sun, 1 Mar 2020 03:44:15 +1100 Subject: [PATCH] wasm: add js.copyBytesToJS() --- targets/wasm_exec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/targets/wasm_exec.js b/targets/wasm_exec.js index 70459ef5..24d55d10 100644 --- a/targets/wasm_exec.js +++ b/targets/wasm_exec.js @@ -319,6 +319,25 @@ //"syscall/js.valueInstanceOf": (sp) => { // mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16)); //}, + + // copyBytesToJS(dst ref, src []byte) (int, bool) + // Originally copied from upstream Go project, then modified: + // https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416 + "syscall/js.copyBytesToJS": (ret_addr, dest_addr, source_addr, source_len, source_cap) => { + let num_bytes_copied_addr = ret_addr; + let returned_status_addr = ret_addr + 4; // Address of returned boolean status variable + + const dst = loadValue(dest_addr); + const src = loadSlice(source_addr, source_len); + if (!(dst instanceof Uint8Array)) { + mem().setUint8(returned_status_addr, 0); // Return "not ok" status + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(num_bytes_copied_addr, toCopy.length); + mem().setUint8(returned_status_addr, 1); // Return "ok" status + }, } }; }