dartI64 function

int dartI64(
  1. JSAny? v
)

JS BigInt (or number) → Dart int, for int64_t returns and callback args.

Exact over the full int64 range on dart2wasm; 53-bit on dart2js, where a Dart int is a JS double.

Implementation

int dartI64(JSAny? v) {
  if (v == null) return 0;
  if (v.typeofEquals('number')) return (v as JSNumber).toDartDouble.toInt();

  // Fast path: values inside the exact-double range convert losslessly.
  final asDouble = (_jsNumber.callAsFunction(null, v)! as JSNumber).toDartDouble;
  if (asDouble >= -_maxExactInJsNumber && asDouble <= _maxExactInJsNumber) {
    return asDouble.toInt();
  }

  // `Number(bigint)` rounds past 2^53; decimal text is lossless.
  return BigInt.parse(v.toString()).toSigned(64).toInt();
}