-
Notifications
You must be signed in to change notification settings - Fork 23
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
js: use BigInt64Array
or BigInt
instead of Number
for int64, int, uint64, uint
#187
Comments
Looking into of mUnaryMinusI64: applyFormat("negInt64($1)", "-($1)")
# ...
of mInt64ToStr: applyFormat("cstrToNimstr(($1)+\"\")", "cstrToNimstr(($1)+\"\")") and in proc of nkCharLit..nkUInt64Lit:
if n.typ.kind == tyBool:
r.res = if n.intVal == 0: rope"false" else: rope"true"
else:
r.res = rope(n.intVal) I would think it the key is to target |
take a look at nim-lang/Nim#13298 as a starting point; I'll let you take over :-) |
nim-lang/Nim#4714 (comment) links to Scala JS implementation of Long, and that is with 2 seperate 32 bit integers, someone also linked their repo with a port of the Kotlin solution. Since Nim targets Javascript 1.5 (or thats what the backends page says) this might be a better solution. |
definitely worth considering (analog to compiler/int128.nim), but I suspect it will be slower than BigInt. We should run some benchmarks. |
just found this relevant article: https://medium.com/leaningtech/how-cheerp-supports-64-bit-integers-in-both-javascript-and-webassembly-79485761615a for how the handle 64 bit integers in js, exploring bigint, wasm, and a custom solution.
|
Although that article expresses problems with BigInt, double int32 is not much better for our pure JS output. BigInt is slow and supported less and incompatible with the Number type but has convenient things like asIntN and BigInt64Array. Double int32 entirely clashes with the handling of all the other numbers and we would have to choose between an object and an array. It's not going to be fun either way. Both things could be done in a library/module, maybe that could be done before implementing it in the core language to better gauge the semantics. |
there's also the more recent
it's supported on almost all browsers except safari (and IE), for now |
BigInt64Array
or BigInt
instead of Number
for int64, int, uint64, uint
It's supported on less than 3/4ths of the browsers currently in use: https://caniuse.com/mdn-javascript_builtins_bigint64array |
Safari seems to begin to implement |
https://bugs.webkit.org/show_bug.cgi?id=190800 is now marked as fixed |
IMO we should use BigInt.
It's weird to me that no one has attempted a PR after nim-lang/Nim#13298 unless I'm missing something. Also I don't know why |
in nim js,
int
(and even int64 etc) is represented as Number, which can hold 32 bit integers but behave weirdly beyond that (eg float). As mentioned here [1]:BigInt is supported not everywhere but in most major browsers: https://caniuse.com/#search=bigint ; and you can easily play with it, eg in chrome console:
it's part of ES2020 https://www.freecodecamp.org/news/javascript-new-features-es2020/
proposal
--experimental:js_enable_bigint
that makesint
,int64
,uint64
useBigInt
instead ofNumber
for nim jslinks
BigInt64Array
andBigUInt64Array
, refs developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64ArrayThe text was updated successfully, but these errors were encountered: