Skip to content
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

Typed arrays #89

Open
linlexing opened this issue Jul 12, 2014 · 16 comments
Open

Typed arrays #89

linlexing opened this issue Jul 12, 2014 · 16 comments

Comments

@linlexing
Copy link
Contributor

When can support Typed arrays?

@stevenh
Copy link
Collaborator

stevenh commented Feb 12, 2015

Do you have an example of what your trying to do?

@linlexing
Copy link
Contributor Author

"Typed Array" is good and consume less memory than the "array " performance. "Typed Array" for reading and writing the file contents , as well as for data encryption and decryption.

// From a length
var uint8 = new Uint8Array(2);
uint8[0] = 42;
console.log(uint8[0]); // 42
console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1

// From an array
var arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint8Array([21, 31]);
var y = new Uint8Array(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8Array(buffer, 1, 4);

@stevenh
Copy link
Collaborator

stevenh commented Feb 23, 2015

Apart from the BYTES_PER_ELEMENT, that should just work.

For the passing arrays in you'll need: #108

@boomshroom
Copy link

undefined ReferenceError: Float32Array is not defined

@ble
Copy link

ble commented Apr 24, 2015

References:
MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
Relevant section from Draft ECMA-262 6th Edition (April 3, 2015): http://people.mozilla.org/~jorendorff/es6-draft.html#sec-typedarray-objects

@robertkrimen
Copy link
Owner

I thought about this briefly before. It would be nice to have with Go's natives types, but I don't know of a straightforward way (leveraging Go) to present "typed views" of the same data.

@ble
Copy link

ble commented Apr 24, 2015

There is a standard library way of treating arbitrary bytes as a sequence of fixed-size values: http://golang.org/pkg/encoding/binary/#Read
http://golang.org/pkg/encoding/binary/#Write

It seems designed for bulk transform, not for providing a typed view of bytes. It might not perform very well if it gets called repeatedly, as you'd want to do for providing this interface. Docs for the package say "This package favors simplicity over efficiency."

There's always the package unsafe way, too.

@3dseals
Copy link

3dseals commented Jun 24, 2016

I got the error:
'Uint8Array' is not defined
'ArrayBuffer' is not defined
when require github.com/dcodeIO/ProtoBuf.js
I thought slices can be converted toArrayBuffer
Please refer to:

Go type JavaScript type Conversions back to interface{}
bool Boolean bool
integers and floats Number float64
string String string
[]int8 Int8Array []int8
[]int16 Int16Array []int16
[]int32, []int Int32Array []int
[]uint8 Uint8Array []uint8
[]uint16 Uint16Array []uint16
[]uint32, []uint Uint32Array []uint
[]float32 Float32Array []float32
[]float64 Float64Array []float64
all other slices Array []interface{}
arrays see slice type see slice type
functions Function func(...interface{}) *js.Object
time.Time Date time.Time
- instanceof Node *js.Object
maps, structs instanceof Object map[string]interface{}

form gopherjs

by the way When can we use the ArrayBuffer

@boomshroom
Copy link

A little off topic, but Int32Array always gets converted to a []int? Even on a 64 bit machine where int means int64?

@hochhaus
Copy link
Contributor

@stevenh Does your comment mean that typed arrays are currently implemented in otto?

When I try to to do something simple with types arrays (eg: var uint8 = new Uint8Array(2);) I receive a ReferenceError: 'Uint8Array' is not defined error.

@hochhaus
Copy link
Contributor

Looking at the code a bit I'm reasonably confident that no attempt has been made to implement typed arrays. Could this limitation be documented in the caveat-emptor section of the README?

@deoxxa
Copy link
Collaborator

deoxxa commented Sep 19, 2016

Typed Arrays aren't part of ES5, and I don't think we need to start listing all the ES6 features otto doesn't support.

@hochhaus
Copy link
Contributor

hochhaus commented Sep 19, 2016

That is fair, how about listing that otto targets ES5 then?

Typed arrays are relatively ubiquitous at this point (more so than much of ES6) so this seems like it might be a common pitfall.

@deoxxa
Copy link
Collaborator

deoxxa commented Sep 19, 2016

Yep, fair call. I actually thought it was listed clearly already, but I appear to be mistaken! I'd happily merge a change to make this more obvious in the readme and the inline docs - could you put that in for me, @hochhaus?

@hochhaus
Copy link
Contributor

Thanks @deoxxa. Change #216 sent.

@stevenh
Copy link
Collaborator

stevenh commented Nov 29, 2022

Typed arrays are part of ES6 so not currently supported, but it should be possible to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants