A question about Uint8Array #371
Replies: 10 comments
-
Posted at 2015-05-08 by DrAzzy But you supplied an array buffer, someUint8array.buffer, not an array buffer view. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-08 by @gfwilliams As said, using .buffer means you're using the backing ArrayBuffer, not a view of it. Espruino is behaving just like normal Javascript should here. If you want a copy, just remove the .buffer and the array will be copied instead. Actually being able to create a view of an existing array is something that's really useful sometimes. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-08 by @allObjects @gordon, are some new exciting things making your regular timezone float (irregular), or some open items? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-09 by Uhv @gordon |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-09 by Uhv I have decided the problem by next way: first of all someInt8Array is copied with offset and len to a interim array tempArr and then the last one is copied fully to the newArr. tempArr = new Uint8Array(someInt8Array.buffer, offset, len); But it seems me, that is not a finest decision... |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-09 by DrAzzy This isn't Espruino, this is plain old JS. The chrome js console is the same:
Why they did it that way is unclear to me. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-09 by Uhv The constructor Uint8Array uses second and third parameters when first parameter is ArrayBuffer only. Therefore in second case (string 8 in your code) array v gets the full copy of array t. There was a surprise for me when i have found out that in first case (string 4 in your code) array u gets not a copy of t but reference to elements t[1] and t[2]. And if I change u[0] and u[1] t[1] and t[2] are changed too :-0 In addition the u.buffer consists of all elements of t array :-D and some function may work not quite properly :-( I have found this when I worked with LCD.drawImage() Now I have decided a problem by method you can see in post #6 of this conversation. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-11 by @gfwilliams
Seems like a good solution to me - it won't be much slower than if you could have done it directly with the Uint8Array constructor. If you want more speed you can make sure you pre-allocate the array:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-11 by Uhv @gordon |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-05-12 by @gfwilliams Thanks - yes, absolutely. I've just changed it :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-05-08 by Uhv
When I use the constructor
newArr = new Uint8Array(someInt8Array.buffer, offset, len),
it doesn't copy the someInt8Array into newArr, but makes the reference to it for newArr and if I change some element of newArr, the element of source array someInt8Array with offset index is changed too!
In Espruino Reference it says: "If an ArrayBuffer view (eg. Uint8Array rather than ArrayBuffer) is given, it will be completely copied rather than referenced." Does it mean, that someInt8Array must be copied to newArr?
Is it real bug? Or do I understand it wrong?
Beta Was this translation helpful? Give feedback.
All reactions