diff --git a/runtime/js/embind/_wrap/emval.cpp b/runtime/js/embind/_wrap/emval.cpp index 9c81fd7d..44a7eda1 100644 --- a/runtime/js/embind/_wrap/emval.cpp +++ b/runtime/js/embind/_wrap/emval.cpp @@ -173,25 +173,13 @@ EM_VAL llgo_emval_call(EM_VAL fn, EM_VAL args[], int nargs, int kind, int *error return fromGenericWireType(ret).release_ownership(); } -/* -TYPEID llgo_emval_typeid_void() { - return take_typeid(); +EM_VAL llgo_emval_memory_view_uint8(size_t length, uint8_t *data) { + val view{ typed_memory_view(length,data) }; + return view.release_ownership(); } -TYPEID llgo_emval_typeid_double() { - return take_typeid(); -} - -TYPEID llgo_emval_typeid_string() { - return take_typeid(); -} - -TYPEID llgo_emval_typeid_val() { - return take_typeid(); -} -*/ - void llgo_emval_dump(EM_VAL v) { + _emval_incref(v); val console = val::global("console"); console.call("log", val::take_ownership(v)); } diff --git a/runtime/js/emval.go b/runtime/js/emval.go index d8034d50..0e53fdab 100644 --- a/runtime/js/emval.go +++ b/runtime/js/emval.go @@ -83,6 +83,9 @@ func emval_method_call(object Value, name *c.Char, args *Value, nargs c.Int, err //go:linkname emval_call C.llgo_emval_call func emval_call(fn Value, args *Value, nargs c.Int, kind c.Int, err *c.Int) Value +//go:linkname emval_memory_view_uint8 C.llgo_emval_memory_view_uint8 +func emval_memory_view_uint8(length c.SizeT, data *c.Uint8T) Value + //go:linkname emval_dump C.llgo_emval_dump func emval_dump(v Value) diff --git a/runtime/js/js.go b/runtime/js/js.go index 7f4d1bad..40509936 100644 --- a/runtime/js/js.go +++ b/runtime/js/js.go @@ -666,7 +666,13 @@ func (e *ValueError) Error() string { // It panics if src is not a Uint8Array or Uint8ClampedArray. // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. func CopyBytesToGo(dst []byte, src Value) int { - return 0 + if !(emval_instanceof(src, uint8Array) || emval_instanceof(src, uint8ClampedArray)) { + return 0 + } + toCopy := src.Call("subarray", 0, len(dst)) + view := emval_memory_view_uint8(uintptr(len(dst)), *(**byte)(unsafe.Pointer(&dst))) + view.Call("set", toCopy) + return toCopy.Length() // n, ok := copyBytesToGo(dst, src.ref) // runtime.KeepAlive(src) // if !ok { @@ -675,6 +681,23 @@ func CopyBytesToGo(dst []byte, src Value) int { // return n } +/* + // func copyBytesToGo(dst []byte, src ref) (int, bool) + "syscall/js.copyBytesToGo": (sp) => { + sp >>>= 0; + const dst = loadSlice(sp + 8); + const src = loadValue(sp + 32); + if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) { + this.mem.setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + this.mem.setUint8(sp + 48, 1); + }, +*/ + // copyBytesToGo copies bytes from src to dst. // // Using go:noescape is safe because the dst byte slice is only used as a dst @@ -688,7 +711,13 @@ func CopyBytesToGo(dst []byte, src Value) int { // It panics if dst is not a Uint8Array or Uint8ClampedArray. // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. func CopyBytesToJS(dst Value, src []byte) int { - return 0 + if !(emval_instanceof(dst, uint8Array) || emval_instanceof(dst, uint8ClampedArray)) { + return 0 + } + view := emval_memory_view_uint8(uintptr(len(src)), *(**byte)(unsafe.Pointer(&src))) + toCopy := view.Call("subarray", 0, dst.Length()) + dst.Call("set", toCopy) + return toCopy.Length() // n, ok := copyBytesToJS(dst.ref, src) // runtime.KeepAlive(dst) // if !ok { @@ -697,6 +726,28 @@ func CopyBytesToJS(dst Value, src []byte) int { // return n } +/* + // func copyBytesToJS(dst ref, src []byte) (int, bool) + "syscall/js.copyBytesToJS": (sp) => { + sp >>>= 0; + const dst = loadValue(sp + 8); + const src = loadSlice(sp + 16); + if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) { + this.mem.setUint8(sp + 48, 0); + return; + } + const toCopy = src.subarray(0, dst.length); + dst.set(toCopy); + setInt64(sp + 40, toCopy.length); + this.mem.setUint8(sp + 48, 1); + }, +*/ + +var ( + uint8Array = emval_get_global(c.Str("Uint8Array")) + uint8ClampedArray = emval_get_global(c.Str("Uint8ClampedArray")) +) + // copyBytesToJS copies bytes from src to dst. // // Using go:noescape is safe because the src byte slice is only used as a src