number.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. defineProperties, fromTwos, getBigInt, mask, toTwos
  3. } from "../../utils/index.js";
  4. import { Typed } from "../typed.js";
  5. import { Coder, WordSize } from "./abstract-coder.js";
  6. import type { BigNumberish } from "../../utils/index.js";
  7. import type { Reader, Writer } from "./abstract-coder.js";
  8. const BN_0 = BigInt(0);
  9. const BN_1 = BigInt(1);
  10. const BN_MAX_UINT256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
  11. /**
  12. * @_ignore
  13. */
  14. export class NumberCoder extends Coder {
  15. readonly size!: number;
  16. readonly signed!: boolean;
  17. constructor(size: number, signed: boolean, localName: string) {
  18. const name = ((signed ? "int": "uint") + (size * 8));
  19. super(name, name, localName, false);
  20. defineProperties<NumberCoder>(this, { size, signed }, { size: "number", signed: "boolean" });
  21. }
  22. defaultValue(): number {
  23. return 0;
  24. }
  25. encode(writer: Writer, _value: BigNumberish | Typed): number {
  26. let value = getBigInt(Typed.dereference(_value, this.type));
  27. // Check bounds are safe for encoding
  28. let maxUintValue = mask(BN_MAX_UINT256, WordSize * 8);
  29. if (this.signed) {
  30. let bounds = mask(maxUintValue, (this.size * 8) - 1);
  31. if (value > bounds || value < -(bounds + BN_1)) {
  32. this._throwError("value out-of-bounds", _value);
  33. }
  34. value = toTwos(value, 8 * WordSize);
  35. } else if (value < BN_0 || value > mask(maxUintValue, this.size * 8)) {
  36. this._throwError("value out-of-bounds", _value);
  37. }
  38. return writer.writeValue(value);
  39. }
  40. decode(reader: Reader): any {
  41. let value = mask(reader.readValue(), this.size * 8);
  42. if (this.signed) {
  43. value = fromTwos(value, this.size * 8);
  44. }
  45. return value;
  46. }
  47. }