-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
array literals uses typed arrays; fix a jsgen bug #16850
Conversation
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
this needs a test.
proc jsTypeof*(a: auto): cstring =
asm """`result` = typeof(`a`)"""
proc jsConstructorName*(a: auto): cstring =
asm """`result` = `a`.constructor.name""" (that file exists in devel but somehow not in your PR, which looks like it hasn't been rebased in a while?)
import std/private/jsutils
proc main()=
template fn(a): untyped = jsConstructorName(a)
doAssert fn(array[2, int8].default) == "Int8Array"
doAssert fn(array[2, uint8].default) == "Uint8Array"
doAssert fn(array[2, byte].default) == "Uint8Array"
# doAssert fn(array[2, char].default) == "Uint8Array" # xxx fails; bug?
doAssert fn(array[2, uint64].default) == "Array"
# pending https://github.com/nim-lang/RFCs/issues/187 maybe use `BigUint64Array`
doAssert fn([1'u8]) == "Uint8Array"
doAssert fn([1'u16]) == "Uint16Array"
doAssert fn([byte(1)]) == "Uint8Array"
main() |
I find a bug proc hello(): array[5, int] = discard
var x = @(hello())
x.add(2)
echo x x_369098759[0].push(2);; TypeError: x_369098759[0].push is not a function |
@xflywind needs rebase |
I'm still pondering the significance of timotheecour#567, IMO that benchmark i wrote is contrived/not representative but would still be nice to have at least 1 non-contribed benchmark which shows benefit of typed array (but in future work is fine) bugfix: more type safe even in presence of caststhis PR fixes the following bug; we should add a test either in this PR or in followup PRs: when true:
import jsconsole
template fn(a) =
echo a
console.log a
var x = 257.2
var b = cast[uint8](x)
echo b
a[0] = b
echo a
console.log a
block:
var a: array[3, uint8]
a = [2'u8,4,5]
fn(a)
block:
var a: array[3, uint8] = [2'u8,4,5]
fn(a) before PR: bug after PR: |
Is this still a draft? |
Typed arrays are fast for access, but slow to be allocated, and normal arrays can be as fast as typed arrays if you do a lot of sequential access. Random access outside the bounds of the array causes the array to grow. This PR is ready to review. |
* array litterals uses typed arrays * Update compiler/jsgen.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Ref timotheecour#557
Before this PR:
After