-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create typed arrays from Go #379
Comments
Hi. You can do |
Ok I managed to get it working with your suggestion, thanks. Closing this issue but please consider exposing those methods publicly. |
Hey, just posting this for people who need to do the same thing: func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
// bufType is cached `vm.Get("Uint8Array")`
return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val)))
} Note by first casting to And to get a buffer from JS: func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value) ([]byte, error) {
obj := buf.ToObject(vm)
if !obj.Get("constructor").SameAs(bufType) {
return nil, errors.New("invalid buffer")
}
b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes()
return b, nil
} |
Hello! I was wondering if it's possible to create a
Uint8Array
in Go and pass it to the JS code. I see there are functions likeRuntime.newUint8Array
but they are not exposed publicly. The workaround I came up for now is a JS function which takes in an array and returns a buffer and invoke that in Go, and pass the returned value again to JS which has unnecessary overhead.The text was updated successfully, but these errors were encountered: