mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 09:24:36 +00:00
refs #6978, refs #6752, refs #21613, refs #24234
The `jsNoInt64`, `whenHasBigInt64`, `whenJsNoBigInt64` templates are
replaced with bool constants to use with `when`. Weird that I didn't do
this in the first place.
The `whenJsNoBigInt64` template was also slightly misleading. The first
branch was compiled for both no bigint64 on JS as well as on C/C++. It
seems only `trandom` depended on this by mistake.
The workaround for #6752 added in #6978 to `times` is also removed with
`--jsbigint64:on`, but #24233 was also encountered with this, so this PR
depends on #24234.
(cherry picked from commit 041098e882)
104 lines
3.9 KiB
Nim
104 lines
3.9 KiB
Nim
when defined(js):
|
|
import std/jsbigints
|
|
|
|
type
|
|
ArrayBuffer* = ref object of JsRoot
|
|
Float64Array* = ref object of JsRoot
|
|
Uint32Array* = ref object of JsRoot
|
|
Uint8Array* = ref object of JsRoot
|
|
BigUint64Array* = ref object of JsRoot
|
|
|
|
|
|
func newArrayBuffer*(n: int): ArrayBuffer {.importjs: "new ArrayBuffer(#)".}
|
|
func newFloat64Array*(buffer: ArrayBuffer): Float64Array {.importjs: "new Float64Array(#)".}
|
|
func newUint32Array*(buffer: ArrayBuffer): Uint32Array {.importjs: "new Uint32Array(#)".}
|
|
func newBigUint64Array*(buffer: ArrayBuffer): BigUint64Array {.importjs: "new BigUint64Array(#)".}
|
|
|
|
func newUint8Array*(n: int): Uint8Array {.importjs: "new Uint8Array(#)".}
|
|
|
|
func `[]`*(arr: Uint32Array, i: int): uint32 {.importjs: "#[#]".}
|
|
func `[]`*(arr: Uint8Array, i: int): uint8 {.importjs: "#[#]".}
|
|
func `[]`*(arr: BigUint64Array, i: int): JsBigInt {.importjs: "#[#]".}
|
|
func `[]=`*(arr: Float64Array, i: int, v: float) {.importjs: "#[#] = #".}
|
|
|
|
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
|
|
runnableExamples:
|
|
import std/[jsffi, jsbigints]
|
|
assert jsTypeOf(1.toJs) == "number"
|
|
assert jsTypeOf(false.toJs) == "boolean"
|
|
assert [1].toJs.jsTypeOf == "object" # note the difference with `getProtoName`
|
|
assert big"1".toJs.jsTypeOf == "bigint"
|
|
|
|
proc jsConstructorName*[T](a: T): cstring =
|
|
runnableExamples:
|
|
import std/jsffi
|
|
let a = array[2, float64].default
|
|
assert jsConstructorName(a) == "Float64Array"
|
|
assert jsConstructorName(a.toJs) == "Float64Array"
|
|
{.emit: """`result` = `a`.constructor.name;""".}
|
|
|
|
proc hasJsBigInt*(): bool =
|
|
{.emit: """`result` = typeof BigInt != 'undefined';""".}
|
|
|
|
proc hasBigUint64Array*(): bool =
|
|
{.emit: """`result` = typeof BigUint64Array != 'undefined';""".}
|
|
|
|
proc getProtoName*[T](a: T): cstring {.importjs: "Object.prototype.toString.call(#)".} =
|
|
runnableExamples:
|
|
import std/[jsffi, jsbigints]
|
|
type A = ref object
|
|
assert 1.toJs.getProtoName == "[object Number]"
|
|
assert "a".toJs.getProtoName == "[object String]"
|
|
assert big"1".toJs.getProtoName == "[object BigInt]"
|
|
assert false.toJs.getProtoName == "[object Boolean]"
|
|
assert (a: 1).toJs.getProtoName == "[object Object]"
|
|
assert A.default.toJs.getProtoName == "[object Null]"
|
|
assert [1].toJs.getProtoName == "[object Int32Array]" # implementation defined
|
|
assert @[1].toJs.getProtoName == "[object Array]" # ditto
|
|
|
|
const maxSafeInteger* = 9007199254740991
|
|
## The same as `Number.MAX_SAFE_INTEGER` or `2^53 - 1`.
|
|
## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
|
|
runnableExamples:
|
|
let a {.importjs: "Number.MAX_SAFE_INTEGER".}: int64
|
|
assert a == maxSafeInteger
|
|
|
|
proc isInteger*[T](x: T): bool {.importjs: "Number.isInteger(#)".} =
|
|
runnableExamples:
|
|
import std/jsffi
|
|
assert 1.isInteger
|
|
assert not 1.5.isInteger
|
|
assert 1.toJs.isInteger
|
|
assert not 1.5.toJs.isInteger
|
|
|
|
proc isSafeInteger*[T](x: T): bool {.importjs: "Number.isSafeInteger(#)".} =
|
|
runnableExamples:
|
|
import std/jsffi
|
|
assert not "123".toJs.isSafeInteger
|
|
assert 123.isSafeInteger
|
|
assert 123.toJs.isSafeInteger
|
|
when false:
|
|
assert 9007199254740991.toJs.isSafeInteger
|
|
assert not 9007199254740992.toJs.isSafeInteger
|
|
|
|
const jsNoBigInt64* =
|
|
when defined(js):
|
|
when compiles(compileOption("jsbigint64")):
|
|
not compileOption("jsbigint64")
|
|
else:
|
|
true
|
|
else:
|
|
false
|
|
|
|
const hasWorkingInt64* =
|
|
# equal to `not jsNoBigInt64`, but define it by itself anyway
|
|
when defined(js):
|
|
when compiles(compileOption("jsbigint64")):
|
|
compileOption("jsbigint64")
|
|
else:
|
|
false
|
|
else:
|
|
true
|