Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions targets/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@
//"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
// param4 and param5 (below) are likely "length" variables of some sort
"syscall/js.copyBytesToJS": (ret_addr, dest_addr, source_addr, param4, param5) => {
Copy link
Member

@aykevl aykevl Nov 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calling convention changes have changed this function conceptually to the following:

func copyBytesToJS(returnValue *struct{int; bool}, dst *ref, srcPtr *byte, srcLen, srcCap uintptr)

The following three rules apply to it:

  • large return values are passed as a pointer-to-struct and prepended to the parameter list
  • int64/uint64 (the ref type) are passed as a pointer-to-value
  • slices are split into 3 parts: the pointer to the underlying array, the length, and the capacity.

So yes, param4 is len and param5 is cap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll update things accordingly over the next few days. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance you can take a look at this PR? The next release is getting close.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I can make time this weekend. 😄

let num_bytes_copied_addr = ret_addr;
let returned_status_addr = ret_addr + 4; // address of returned "ok" status variable

const dst = loadValue(dest_addr);
const src = loadSlice(source_addr);
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
},
}
};
}
Expand Down