-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backport 1.0] add backend support for js bigint (#16606)
* add backend support for js bigint * cleanup * add tests * add -d:nimHasJsBigIntBackend * cleanup * more tests (cherry picked from commit 025ca66)
- Loading branch information
1 parent
822c7ac
commit c346c0b
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
proc jsTypeOf*[T](x: T): cstring {.importjs: "typeof(#)".} | ||
## Returns the name of the JsObject's JavaScript type as a cstring. | ||
# xxx replace jsffi.jsTypeOf with this definition and add tests | ||
|
||
type JsBigIntImpl {.importc: "bigint".} = int | ||
type JsBigInt = distinct JsBigIntImpl | ||
|
||
doAssert JsBigInt isnot int | ||
func big*(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)".} | ||
func big*(integer: cstring): JsBigInt {.importjs: "BigInt(#)".} | ||
func `<=`*(x, y: JsBigInt): bool {.importjs: "(# $1 #)".} | ||
func `==`*(x, y: JsBigInt): bool {.importjs: "(# === #)".} | ||
func inc*(x: var JsBigInt) {.importjs: "[#][0][0]++".} | ||
func inc2*(x: var JsBigInt) {.importjs: "#++".} | ||
func toCstring*(this: JsBigInt): cstring {.importjs: "#.toString()".} | ||
func `$`*(this: JsBigInt): string = | ||
$toCstring(this) | ||
|
||
block: | ||
doAssert defined(nimHasJsBigIntBackend) | ||
let z1 = big"10" | ||
let z2 = big"15" | ||
doAssert z1 == big"10" | ||
doAssert z1 == z1 | ||
doAssert z1 != z2 | ||
var s: seq[cstring] | ||
for i in z1 .. z2: | ||
s.add $i | ||
doAssert s == @["10".cstring, "11", "12", "13", "14", "15"] | ||
block: | ||
var a=big"3" | ||
a.inc | ||
doAssert a == big"4" | ||
block: | ||
var z: JsBigInt | ||
doAssert $z == "0" | ||
doAssert z.jsTypeOf == "bigint" # would fail without codegen change | ||
doAssert z != big(1) | ||
doAssert z == big"0" # ditto | ||
|
||
# ditto below | ||
block: | ||
let z: JsBigInt = big"1" | ||
doAssert $z == "1" | ||
doAssert z.jsTypeOf == "bigint" | ||
doAssert z == big"1" | ||
|
||
block: | ||
let z = JsBigInt.default | ||
doAssert $z == "0" | ||
doAssert z.jsTypeOf == "bigint" | ||
doAssert z == big"0" | ||
|
||
block: | ||
var a: seq[JsBigInt] | ||
a.setLen 3 | ||
doAssert a[^1].jsTypeOf == "bigint" | ||
doAssert a[^1] == big"0" |