BigIntPrototype.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Numerics;
  2. using Jint.Collections;
  3. using Jint.Native.Object;
  4. using Jint.Native.Symbol;
  5. using Jint.Pooling;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.BigInt;
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-properties-of-the-bigint-prototype-object
  12. /// </summary>
  13. public sealed class BigIntPrototype : ObjectInstance
  14. {
  15. private readonly Realm _realm;
  16. private readonly BigIntConstructor _constructor;
  17. internal BigIntPrototype(
  18. Engine engine,
  19. Realm realm,
  20. BigIntConstructor constructor,
  21. ObjectPrototype objectPrototype)
  22. : base(engine, ObjectClass.Object, InternalTypes.BigInt)
  23. {
  24. _prototype = objectPrototype;
  25. _realm = realm;
  26. _constructor = constructor;
  27. }
  28. protected override void Initialize()
  29. {
  30. var properties = new PropertyDictionary(4, checkExistingKeys: false)
  31. {
  32. ["constructor"] = new(_constructor, true, false, true),
  33. ["toString"] = new(new ClrFunctionInstance(Engine, "toString", ToBigIntString, 0, PropertyFlag.Configurable), true, false, true),
  34. ["toLocaleString"] = new(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString, 0, PropertyFlag.Configurable), true, false, true),
  35. ["valueOf"] = new(new ClrFunctionInstance(Engine, "valueOf", ValueOf, 0, PropertyFlag.Configurable), true, false, true),
  36. };
  37. SetProperties(properties);
  38. var symbols = new SymbolDictionary(1)
  39. {
  40. [GlobalSymbolRegistry.ToStringTag] = new("BigInt", false, false, true)
  41. };
  42. SetSymbols(symbols);
  43. }
  44. /// <summary>
  45. /// https://tc39.es/ecma262/#sec-bigint.prototype.tolocalestring
  46. /// </summary>
  47. private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
  48. {
  49. if (!thisObject.IsBigInt() && thisObject is not BigIntInstance)
  50. {
  51. ExceptionHelper.ThrowTypeError(_realm);
  52. }
  53. var m = TypeConverter.ToBigInt(thisObject);
  54. return m.ToString("R");
  55. }
  56. /// <summary>
  57. /// https://tc39.es/ecma262/#sec-bigint.prototype.valueof
  58. /// </summary>
  59. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  60. {
  61. if (thisObj is BigIntInstance ni)
  62. {
  63. return ni.BigIntData;
  64. }
  65. if (thisObj is JsBigInt)
  66. {
  67. return thisObj;
  68. }
  69. ExceptionHelper.ThrowTypeError(_realm);
  70. return null;
  71. }
  72. /// <summary>
  73. /// https://tc39.es/ecma262/#sec-bigint.prototype.tostring
  74. /// </summary>
  75. private JsValue ToBigIntString(JsValue thisObject, JsValue[] arguments)
  76. {
  77. var x = ThisBigIntValue(thisObject);
  78. var radix = arguments.At(0);
  79. var radixMV = radix.IsUndefined()
  80. ? 10
  81. : (int) TypeConverter.ToIntegerOrInfinity(radix);
  82. if (radixMV is < 2 or > 36)
  83. {
  84. ExceptionHelper.ThrowRangeError(_realm, "radix must be between 2 and 36");
  85. }
  86. var value = x._value;
  87. if (value == BigInteger.Zero)
  88. {
  89. return JsString.NumberZeroString;
  90. }
  91. if (radixMV == 10)
  92. {
  93. return value.ToString("R");
  94. }
  95. var negative = value < 0;
  96. if (negative)
  97. {
  98. value = -value;
  99. }
  100. const string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  101. using var builder = StringBuilderPool.Rent();
  102. var sb = builder.Builder;
  103. for (; value > 0; value /= radixMV)
  104. {
  105. var d = (int) (value % radixMV);
  106. sb.Append(digits[d]);
  107. }
  108. var charArray = new char[sb.Length];
  109. sb.CopyTo(0, charArray, 0, charArray.Length);
  110. System.Array.Reverse(charArray);
  111. return (negative ? "-" : "") + new string(charArray);
  112. }
  113. /// <summary>
  114. /// https://tc39.es/ecma262/#thisbigintvalue
  115. /// </summary>
  116. private JsBigInt ThisBigIntValue(JsValue value)
  117. {
  118. switch (value)
  119. {
  120. case JsBigInt bigInt:
  121. return bigInt;
  122. case BigIntInstance bigIntInstance:
  123. return bigIntInstance.BigIntData;
  124. default:
  125. ExceptionHelper.ThrowTypeError(_realm);
  126. return JsBigInt.One;
  127. }
  128. }
  129. }