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

Add js BigInts #16409

Merged
merged 84 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
45800ea
Add BigInts
juancarlospaco Dec 20, 2020
091109a
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Dec 21, 2020
1fd6edc
Renames tos plurals
juancarlospaco Dec 21, 2020
2492d72
Bear review
juancarlospaco Dec 21, 2020
ceeecbe
Bear review
juancarlospaco Dec 21, 2020
bdc6386
Pear review
juancarlospaco Dec 21, 2020
71a6db5
Peer review
juancarlospaco Dec 22, 2020
230f375
Peer review
juancarlospaco Dec 22, 2020
472546c
Improve Stringifications
juancarlospaco Dec 22, 2020
a1d71a9
Improve Stringifications
juancarlospaco Dec 22, 2020
464f626
Update changelog.md
juancarlospaco Dec 22, 2020
7a8b9f0
RunnableExamplerize
juancarlospaco Dec 22, 2020
516d537
Merge branch 'bigy' of https://github.com/juancarlospaco/Nim into bigy
juancarlospaco Dec 22, 2020
464c0b1
discard the discardable pragma
juancarlospaco Dec 22, 2020
7309b55
Several improvements from peer reviews, more docs
juancarlospaco Dec 22, 2020
c4ddb1f
More doc, more test
juancarlospaco Dec 22, 2020
bd81660
More doc, more test
juancarlospaco Dec 22, 2020
cbed450
More doc, more test
juancarlospaco Dec 22, 2020
816d0fe
More doc, more test
juancarlospaco Dec 22, 2020
38abc40
Better error message 'Error: usage of low is an {.error.} defined at …
juancarlospaco Dec 22, 2020
d546f0f
is an overload, rename
juancarlospaco Dec 22, 2020
cc70f55
proc to scare kids away
juancarlospaco Dec 22, 2020
2759950
moar test moar
juancarlospaco Dec 22, 2020
0fa9599
This useful proc
juancarlospaco Dec 22, 2020
9719ffa
Minor changes
juancarlospaco Dec 22, 2020
79e23c8
Minor changes
juancarlospaco Dec 22, 2020
985d5ce
Update lib/js/jsbigints.nim
juancarlospaco Dec 22, 2020
c0cc419
Get int from BigInt
juancarlospaco Dec 22, 2020
aa0d39a
Update lib/js/jsbigints.nim
juancarlospaco Dec 22, 2020
9292d29
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Dec 27, 2020
3aad4f0
Bigint moves house
juancarlospaco Dec 27, 2020
75c0b51
Merge branch 'bigy' of https://github.com/juancarlospaco/Nim into bigy
juancarlospaco Dec 27, 2020
efb2dc5
koch bigint
juancarlospaco Dec 27, 2020
5240526
move test
juancarlospaco Dec 28, 2020
7a940aa
fixes
juancarlospaco Dec 28, 2020
36b927d
fixes
juancarlospaco Dec 28, 2020
e450d19
is dangerous
juancarlospaco Dec 28, 2020
e71f420
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 4, 2021
cc25689
No better naming found, then newJsBigInt is used, later we can add a …
juancarlospaco Jan 4, 2021
3316879
clean out
juancarlospaco Jan 4, 2021
4fa4a35
clean out
juancarlospaco Jan 4, 2021
f56981f
Update lib/std/jsbigints.nim
juancarlospaco Jan 4, 2021
0334d89
Update lib/std/jsbigints.nim
juancarlospaco Jan 4, 2021
00c0d6c
Addres some comments
juancarlospaco Jan 4, 2021
8958fe8
ReSyncin
juancarlospaco Jan 4, 2021
6d8eba2
https://github.com/nim-lang/Nim/pull/16409#discussion_r551180020
juancarlospaco Jan 4, 2021
181f037
https://github.com/nim-lang/Nim/pull/16409#discussion_r551180898
juancarlospaco Jan 4, 2021
ee8853a
Try to make error messages a little more usable
juancarlospaco Jan 4, 2021
ee9c1a3
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 5, 2021
9c31275
Flip args reversed
juancarlospaco Jan 5, 2021
6668104
Fix inc/dec problem
juancarlospaco Jan 5, 2021
ed6ceda
simple
juancarlospaco Jan 5, 2021
cbb76a1
simple
juancarlospaco Jan 5, 2021
390a5ae
simple
juancarlospaco Jan 5, 2021
03d61c6
Update lib/std/jsbigints.nim
juancarlospaco Jan 6, 2021
05f3fcb
Use BiggestInt
juancarlospaco Jan 7, 2021
c14daf7
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 7, 2021
a88eac0
Use BiggestInt
juancarlospaco Jan 7, 2021
42e77b6
Use BiggestInt
juancarlospaco Jan 7, 2021
39e6572
Merge branch 'bigy' of https://github.com/juancarlospaco/Nim into bigy
juancarlospaco Jan 7, 2021
60f54bf
https://github.com/nim-lang/Nim/pull/16606
juancarlospaco Jan 7, 2021
7ae47f6
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 7, 2021
a6f65f8
https://github.com/nim-lang/Nim/pull/16606
juancarlospaco Jan 7, 2021
a88c401
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 7, 2021
c16c8df
Green test
juancarlospaco Jan 7, 2021
afff5b0
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 7, 2021
6a53e03
Green test
juancarlospaco Jan 7, 2021
82efc22
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 8, 2021
d78f6ce
ReSync
juancarlospaco Jan 8, 2021
a7a9dcf
Fix CI
juancarlospaco Jan 8, 2021
23b7560
ReSync
juancarlospaco Jan 8, 2021
f71ceb3
Update lib/std/jsbigints.nim
juancarlospaco Jan 8, 2021
89e81c7
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 8, 2021
bf7da66
Merge branch 'bigy' of https://github.com/juancarlospaco/Nim into bigy
juancarlospaco Jan 8, 2021
d727b79
String representation is now like 999n
juancarlospaco Jan 9, 2021
a26ca38
Going big. Is not perfect but it works.
juancarlospaco Jan 9, 2021
af01469
Going big. Is not perfect but it works.
juancarlospaco Jan 9, 2021
f045b7b
Update lib/std/jsbigints.nim
juancarlospaco Jan 9, 2021
37d6c66
Update lib/std/jsbigints.nim
juancarlospaco Jan 9, 2021
3ed3672
Update lib/std/jsbigints.nim
juancarlospaco Jan 9, 2021
b879961
Update lib/std/jsbigints.nim
juancarlospaco Jan 9, 2021
b849823
Update lib/std/jsbigints.nim
juancarlospaco Jan 9, 2021
24af3e0
Merge branch 'devel' of https://github.com/nim-lang/Nim into bigy
juancarlospaco Jan 9, 2021
9503383
https://github.com/nim-lang/Nim/pull/16409#discussion_r554365041
juancarlospaco Jan 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
- `writeStackTrace` is available in JS backend now.

- `strscans.scanf` now supports parsing single characters.
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.
- `strscans.scanTuple` added which uses `strscans.scanf` internally, returning a tuple which can be unpacked for easier usage of `scanf`.

- Added `setutils.toSet` that can take any iterable and convert it to a built-in set,
if the iterable yields a built-in settable type.
Expand All @@ -66,6 +66,8 @@
- `echo` and `debugEcho` will now raise `IOError` if writing to stdout fails. Previous behavior
silently ignored errors. See #16366. Use `-d:nimLegacyEchoNoRaise` for previous behavior.

- Add `jsbigint` module, arbitrary precision integers for JavaScript target.

- Added new operations for singly- and doubly linked lists: `lists.toSinglyLinkedList`
and `lists.toDoublyLinkedList` convert from `openArray`s; `lists.copy` implements
shallow copying; `lists.add` concatenates two lists - an O(1) variation that consumes
Expand Down
132 changes: 132 additions & 0 deletions lib/js/jsbigint.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
## Arbitrary precision integers.
juancarlospaco marked this conversation as resolved.
Show resolved Hide resolved
## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

type JsBigInt* = ref object of JsRoot ## Arbitrary precision integer for JavaScript target.

func newBigInt*(integer: cint): JsBigInt {.importjs: "BigInt(#)".}
## Constructor for `JsBigInt`.

func newBigInt*(integer: cstring): JsBigInt {.importjs: "BigInt(#)".}
## Constructor for `JsBigInt`.

func toLocaleString*(this: JsBigInt): cstring {.importjs: "#.$1()".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString

func toLocaleString*(this: JsBigInt; locales: cstring): cstring {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString

func toLocaleString*(this: JsBigInt; locales: openArray[cstring]): cstring {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toLocaleString

func toString*(this: JsBigInt; radix: cint): cstring {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString

func toString*(this: JsBigInt): cstring {.importjs: "#.$1()".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString

func asIntN*(width: cint; bigInteger: JsBigInt): cint {.importjs: "BigInt.$1(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN

func asUintN*(width: cint; bigInteger: JsBigInt): cint {.importjs: "BigInt.$1(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN

func `+`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `-`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `*`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `/`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `%`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `+=`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", discardable.}

func `-=`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", discardable.}

func `*=`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", discardable.}

func `/=`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", discardable.}

func `%=`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)", discardable.}

func `++`*(x: JsBigInt): JsBigInt {.importjs: "($1#)".}

func `--`*(x: JsBigInt): JsBigInt {.importjs: "($1#)".}

func `>`*(x, y: JsBigInt): bool {.importjs: "(# $1 #)".}

func `<`*(x, y: JsBigInt): bool {.importjs: "(# $1 #)".}

func `>=`*(x, y: JsBigInt): bool {.importjs: "(# $1 #)".}

func `<=`*(x, y: JsBigInt): bool {.importjs: "(# $1 #)".}

func `==`*(x, y: JsBigInt): bool {.importjs: "(# $1= #)".}

func `**`*(x, y: JsBigInt): JsBigInt {.importjs: "((#) $1 #)".}

func `and`*(x, y: JsBigInt): JsBigInt {.importjs: "(# && #)".}

func `or`*(x, y: JsBigInt): JsBigInt {.importjs: "(# || #)".}

func `not`*(x: JsBigInt): JsBigInt {.importjs: "(!#)".}

func `in`*(x, y: JsBigInt): JsBigInt {.importjs: "(# $1 #)".}

func `-`*(a: JsBigInt): JsBigInt {.importjs: "($1#)".}

func `xor`*(x, y: JsBigInt): JsBigInt {.importjs: "(# ^ #)".}

func `shl`*(a, b: JsBigInt): JsBigInt {.importjs: "(# << #)".}

func `shr`*(a, b: JsBigInt): JsBigInt {.importjs: "(# >> #)".}

func inc*(a: JsBigInt): JsBigInt {.importjs: "(# += BigInt(1))", discardable.}

func dec*(a: JsBigInt): JsBigInt {.importjs: "(# -= BigInt(1))", discardable.}

func inc*(a, b: JsBigInt): JsBigInt {.importjs: "(# += #)", discardable.}

func dec*(a, b: JsBigInt): JsBigInt {.importjs: "(# -= #)", discardable.}

func `+`*(a: JsBigInt): JsBigInt {.error.} # Can not be used by design.
## https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs


runnableExamples:
let big1: JsBigInt = newBigInt(2147483647.cint)
let big2: JsBigInt = newBigInt("666".cstring)
var big3: JsBigInt = newBigInt("2".cstring)
doAssert big1 != big2
doAssert big1 > big2
doAssert big1 >= big2
doAssert big2 < big1
doAssert big2 <= big1
doAssert not(big1 == big2)
inc big3
doAssert big3 == newBigInt(3.cint)
dec big3
doAssert big3 == newBigInt(2.cint)
inc big3, newBigInt(420.cint)
doAssert big3 == newBigInt(422.cint)
dec big3, newBigInt(420.cint)
doAssert big3 == newBigInt(2.cint)
doAssert (big3 xor big2) == newBigInt(664.cint)
doAssert big1 % big2 == newBigInt("613".cstring)
doAssert -big1 == newBigInt("-2147483647".cstring)
doAssert big1 / big2 == newBigInt("3224449".cstring)
doAssert big1 + big2 == newBigInt("2147484313".cstring)
doAssert big1 - big2 == newBigInt("2147482981".cstring)
doAssert big1 shl big3 == newBigInt("8589934588".cstring)
doAssert big1 shr big3 == newBigInt("536870911".cstring)
doAssert big1 * big2 == newBigInt("1430224108902".cstring)
doAssert big1.toLocaleString("EN".cstring) == "2,147,483,647".cstring
doAssert big1.toLocaleString(["EN".cstring, "ES".cstring]) == "2,147,483,647".cstring
doAssert big1.toString() == "2147483647".cstring
doAssert big1.toString(10.cint) == "2147483647".cstring
doAssert big1.toString(2.cint) == "1111111111111111111111111111111".cstring
doAssert big2 ** big3 == newBigInt(443556.cint)
discard newBigInt("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999".cstring)
discard newBigInt("0".cstring)
discard newBigInt("-999999999999999999999999999999999999999999999999999999999999999999999999999999999999999".cstring)