std/private/jsutils

  Source   Edit

Example:

import std/private/jsutils
let a {.importjs: "Number.MAX_SAFE_INTEGER".}: int64
assert a == maxSafeInteger

Types

ArrayBuffer = ref object of JsRoot
  Source   Edit
BigUint64Array = ref object of JsRoot
  Source   Edit
Float64Array = ref object of JsRoot
  Source   Edit
Uint8Array = ref object of JsRoot
  Source   Edit
Uint32Array = ref object of JsRoot
  Source   Edit

Consts

maxSafeInteger = 9007199254740991'i64
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   Source   Edit

Procs

func `[]=`(arr: Float64Array; i: int; v: float) {.importjs: "#[#] = #",
    ...raises: [], tags: [].}
  Source   Edit
func `[]`(arr: BigUint64Array; i: int): JsBigInt {.importjs: "#[#]", ...raises: [],
    tags: [].}
  Source   Edit
func `[]`(arr: Uint8Array; i: int): uint8 {.importjs: "#[#]", ...raises: [],
    tags: [].}
  Source   Edit
func `[]`(arr: Uint32Array; i: int): uint32 {.importjs: "#[#]", ...raises: [],
    tags: [].}
  Source   Edit
proc getProtoName[T](a: T): cstring {.importjs: "Object.prototype.toString.call(#)",
                                      ...raises: [], tags: [].}

Example:

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]" # implementation defined
  Source   Edit
proc hasBigUint64Array(): bool {....raises: [], tags: [].}
  Source   Edit
proc hasJsBigInt(): bool {....raises: [], tags: [].}
  Source   Edit
proc isInteger[T](x: T): bool {.importjs: "Number.isInteger(#)", ...raises: [],
                                tags: [].}

Example:

import std/jsffi
assert 1.isInteger
assert not 1.5.isInteger
assert 1.toJs.isInteger
assert not 1.5.toJs.isInteger
  Source   Edit
proc isSafeInteger[T](x: T): bool {.importjs: "Number.isSafeInteger(#)",
                                    ...raises: [], tags: [].}

Example:

import std/jsffi
assert not "123".toJs.isSafeInteger
assert 123.isSafeInteger
assert 123.toJs.isSafeInteger
assert 9007199254740991.toJs.isSafeInteger
assert not 9007199254740992.toJs.isSafeInteger
  Source   Edit
proc jsConstructorName[T](a: T): cstring

Example:

import std/jsffi
let a = array[2, float64].default
assert jsConstructorName(a) == "Float64Array"
assert jsConstructorName(a.toJs) == "Float64Array"
  Source   Edit
proc jsTypeOf[T](x: T): cstring {.importjs: "typeof(#)", ...raises: [], tags: [].}
Returns the name of the JsObject's JavaScript type as a cstring.

Example:

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"
  Source   Edit
func newArrayBuffer(n: int): ArrayBuffer {.importjs: "new ArrayBuffer(#)",
    ...raises: [], tags: [].}
  Source   Edit
func newBigUint64Array(buffer: ArrayBuffer): BigUint64Array {.
    importjs: "new BigUint64Array(#)", ...raises: [], tags: [].}
  Source   Edit
func newFloat64Array(buffer: ArrayBuffer): Float64Array {.
    importjs: "new Float64Array(#)", ...raises: [], tags: [].}
  Source   Edit
func newUint8Array(n: int): Uint8Array {.importjs: "new Uint8Array(#)",
    ...raises: [], tags: [].}
  Source   Edit
func newUint32Array(buffer: ArrayBuffer): Uint32Array {.
    importjs: "new Uint32Array(#)", ...raises: [], tags: [].}
  Source   Edit