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

[docs] Missing bytesLength from Array #135

Open
inkeliz opened this issue Apr 17, 2022 · 3 comments
Open

[docs] Missing bytesLength from Array #135

inkeliz opened this issue Apr 17, 2022 · 3 comments

Comments

@inkeliz
Copy link

inkeliz commented Apr 17, 2022

Currently, on Array documentation, doesn't mention the existence of bytesLength:

### Fields
* ```ts
var length: i32
```
The length of this array. Setting the length to a value larger than internal capacity will automatically grow the array.
### Methods

However, it's possible to use array.bytesLength.

@dcodeIO
Copy link
Member

dcodeIO commented Apr 17, 2022

The property is private on normal Arrays, though this restriction is currently not enforced. Accessing it nonetheless is technically possible but considered unsafe. Within the standard library, we'd do something like

changetype<ArrayBufferView>(theArray).byteLength

or

load<i32>(changetype<usize>(theArray), offsetof<ArrayBufferView>("byteLength"))

to make the nature of the operation clear. Its's mostly an implementation detail in AS to have it on Arrays as well, while in JS only ArrayBufferViews (basically the typed arrays like Int32Array etc.) have it.

@inkeliz
Copy link
Author

inkeliz commented Apr 17, 2022

Oh... The byteLength is the capacity of ArrayBuffer, not the size... So, running:

    let x = new Array<u32>(10)
    x.length = 1

    console.log(x.byteLength.toString())

Returns 40 instead of 4. There's any way to get the "`array.byteLength" (that is actually the length, not the capacity)?

@inkeliz
Copy link
Author

inkeliz commented Apr 17, 2022

The solution is to use sizeof<valueof<T>>() (assuming T is Array<u64>, for instance). Then, it's possible to use array.length * sizeof<valueof<T>>().

However, both array.byteLength (for capacity) and valueof is missing from Docs. 🤔

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

No branches or pull requests

2 participants