JavascriptBigInt.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. namespace Js
  7. {
  8. JavascriptBigInt * JavascriptBigInt::Create(const char16 * content, charcount_t cchUseLength, bool isNegative, ScriptContext * scriptContext)
  9. {
  10. return RecyclerNew(scriptContext->GetRecycler(), JavascriptBigInt, content, cchUseLength, isNegative, scriptContext->GetLibrary()->GetBigIntTypeStatic());
  11. }
  12. Var JavascriptBigInt::NewInstance(RecyclableObject* function, CallInfo callInfo, ...)
  13. {
  14. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
  15. ARGUMENTS(args, callInfo);
  16. ScriptContext* scriptContext = function->GetScriptContext();
  17. AssertMsg(args.HasArg(), "Should always have implicit 'this'");
  18. // SkipDefaultNewObject function flag should have prevented the default object from
  19. // being created, except when call true a host dispatch.
  20. Var newTarget = args.GetNewTarget();
  21. bool isCtorSuperCall = JavascriptOperators::GetAndAssertIsConstructorSuperCall(args);
  22. Var result = nullptr;
  23. if (args.Info.Count > 1)
  24. {
  25. result = JavascriptConversion::ToBigInt(args[1], scriptContext);
  26. }
  27. else
  28. {
  29. // TODO:
  30. // v8 throw: cannot convert from undefined to bigint
  31. // we can consider creating a Zero BigInt
  32. AssertOrFailFast(false);
  33. }
  34. if (callInfo.Flags & CallFlags_New)
  35. {
  36. // TODO: handle new constructor
  37. // v8 throw: bigint is not a constructor
  38. AssertOrFailFast(false);
  39. }
  40. return isCtorSuperCall ?
  41. JavascriptOperators::OrdinaryCreateFromConstructor(VarTo<RecyclableObject>(newTarget), UnsafeVarTo<RecyclableObject>(result), nullptr, scriptContext) :
  42. result;
  43. }
  44. BOOL JavascriptBigInt::Equals(Var other, BOOL* value, ScriptContext * requestContext)
  45. {
  46. return JavascriptBigInt::Equals(this, other, value, requestContext);
  47. }
  48. BOOL JavascriptBigInt::Equals(JavascriptBigInt* left, Var right, BOOL* value, ScriptContext * requestContext)
  49. {
  50. switch (JavascriptOperators::GetTypeId(right))
  51. {
  52. case TypeIds_BigInt:
  53. *value = JavascriptBigInt::Equals(left, right);
  54. break;
  55. default:
  56. AssertMsg(VarIs<JavascriptBigInt>(right), "do not support comparison with types other than BigInt");
  57. *value = FALSE;
  58. break;
  59. }
  60. return true;
  61. }
  62. template <typename EncodedChar>
  63. void JavascriptBigInt::InitFromCharDigits(const EncodedChar *pChar, uint32 charLength, bool isNegative)
  64. {
  65. Assert(charLength >= 0);
  66. Assert(pChar != 0);
  67. AssertMsg(charLength <= MaxStringLength, "do not support BigInt out of the range");
  68. const EncodedChar *pCharLimit = pChar + charLength - 1;//'n' at the end
  69. m_length = 0;
  70. m_digits = m_reservedDigitsInit;
  71. m_isNegative = isNegative;
  72. uint32 digitMul = 1;
  73. uint32 digitAdd = 0;
  74. bool check = true;
  75. for (; pChar < pCharLimit; pChar++)
  76. {
  77. Assert(NumberUtilities::IsDigit(*pChar));
  78. if (digitMul == 1e9)
  79. {
  80. check = MulThenAdd(digitMul, digitAdd);
  81. Assert(check);
  82. digitMul = 1;
  83. digitAdd = 0;
  84. }
  85. digitMul *= 10;
  86. digitAdd = digitAdd * 10 + *pChar - '0';
  87. }
  88. Assert(1 < digitMul);
  89. check = MulThenAdd(digitMul, digitAdd);
  90. Assert(check);
  91. // make sure this is no negative zero
  92. if (m_length == 0)
  93. {
  94. m_isNegative = false;
  95. m_length = 1;
  96. m_digits[0] = 0;
  97. }
  98. }
  99. bool JavascriptBigInt::MulThenAdd(uint32 digitMul, uint32 digitAdd)
  100. {
  101. Assert(digitMul != 0);
  102. uint32 carryDigit = 0;
  103. uint32 *pDigit = m_digits;
  104. uint32 *pDigitLimit = pDigit + m_length;
  105. for (; pDigit < pDigitLimit; pDigit++)
  106. {
  107. *pDigit = NumberUtilities::MulLu(*pDigit, digitMul, &carryDigit);// return low Digit to digit, hight Digit to carry
  108. if (digitAdd > 0)
  109. {
  110. carryDigit += NumberUtilities::AddLu(pDigit, digitAdd);// add carry to result
  111. }
  112. digitAdd = carryDigit;
  113. }
  114. if (0 < digitAdd) // length increase by 1
  115. {
  116. AssertOrFailFast(m_length < MaxDigitLength);// check out of bound
  117. m_digits[m_length++] = digitAdd;
  118. }
  119. return true;
  120. }
  121. int JavascriptBigInt::Compare(JavascriptBigInt *pbi)
  122. {
  123. if (m_isNegative != pbi->m_isNegative)
  124. {
  125. if (m_isNegative)
  126. {
  127. return -1;
  128. }
  129. else
  130. {
  131. return 1;
  132. }
  133. }
  134. uint32 index;
  135. int sign = m_isNegative ? -1 : 1;
  136. if (m_length > pbi->m_length)
  137. {
  138. return 1 * sign;
  139. }
  140. if (m_length < pbi->m_length)
  141. {
  142. return -1 * sign;
  143. }
  144. if (0 == m_length)
  145. {
  146. return 0;
  147. }
  148. #pragma prefast(suppress:__WARNING_LOOP_ONLY_EXECUTED_ONCE,"noise")
  149. for (index = m_length - 1; m_digits[index] == pbi->m_digits[index]; index--)
  150. {
  151. if (0 == index)
  152. return 0;
  153. }
  154. Assert(m_digits[index] != pbi->m_digits[index]);
  155. return sign*((m_digits[index] > pbi->m_digits[index]) ? 1 : -1);
  156. }
  157. bool JavascriptBigInt::LessThan(Var aLeft, Var aRight)
  158. {
  159. AssertMsg(VarIs<JavascriptBigInt>(aLeft) && VarIs<JavascriptBigInt>(aRight), "BigInt Equals");
  160. JavascriptBigInt *leftBigInt = VarTo<JavascriptBigInt>(aLeft);
  161. JavascriptBigInt *rightBigInt = VarTo<JavascriptBigInt>(aRight);
  162. return (leftBigInt->Compare(rightBigInt) < 0);
  163. }
  164. bool JavascriptBigInt::Equals(Var aLeft, Var aRight)
  165. {
  166. AssertMsg(VarIs<JavascriptBigInt>(aLeft) && VarIs<JavascriptBigInt>(aRight), "BigInt Equals");
  167. JavascriptBigInt *leftBigInt = VarTo<JavascriptBigInt>(aLeft);
  168. JavascriptBigInt *rightBigInt = VarTo<JavascriptBigInt>(aRight);
  169. return (leftBigInt->Compare(rightBigInt) == 0);
  170. }
  171. template void JavascriptBigInt::InitFromCharDigits<char16>(const char16 *pChar, uint32 charLength, bool isNegative);
  172. template void JavascriptBigInt::InitFromCharDigits<utf8char_t>(const utf8char_t *pChar, uint32 charLength, bool isNegative);
  173. } // namespace Js