NumberPrototype.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/NumberObject.h>
  30. #include <LibJS/Runtime/NumberPrototype.h>
  31. namespace JS {
  32. static const u8 max_precision_for_radix[37] = {
  33. // clang-format off
  34. 0, 0, 52, 32, 26, 22, 20, 18, 17, 16,
  35. 15, 15, 14, 14, 13, 13, 13, 12, 12, 12,
  36. 12, 11, 11, 11, 11, 11, 11, 10, 10, 10,
  37. 10, 10, 10, 10, 10, 10, 10,
  38. // clang-format on
  39. };
  40. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  41. NumberPrototype::NumberPrototype(GlobalObject& global_object)
  42. : NumberObject(0, *global_object.object_prototype())
  43. {
  44. }
  45. void NumberPrototype::initialize(GlobalObject& object)
  46. {
  47. Object::initialize(object);
  48. define_native_function("toString", to_string, 1, Attribute::Configurable | Attribute::Writable);
  49. }
  50. NumberPrototype::~NumberPrototype()
  51. {
  52. }
  53. JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
  54. {
  55. Value number_value;
  56. auto this_value = vm.this_value(global_object);
  57. if (this_value.is_number()) {
  58. number_value = this_value;
  59. } else if (this_value.is_object() && this_value.as_object().is_number_object()) {
  60. number_value = static_cast<NumberObject&>(this_value.as_object()).value_of();
  61. } else {
  62. vm.throw_exception<TypeError>(global_object, ErrorType::NumberIncompatibleThis, "toString");
  63. return {};
  64. }
  65. int radix;
  66. auto argument = vm.argument(0);
  67. if (argument.is_undefined()) {
  68. radix = 10;
  69. } else {
  70. radix = argument.to_i32(global_object);
  71. }
  72. if (vm.exception() || radix < 2 || radix > 36) {
  73. vm.throw_exception<RangeError>(global_object, ErrorType::InvalidRadix);
  74. return {};
  75. }
  76. if (number_value.is_positive_infinity())
  77. return js_string(vm, "Infinity");
  78. if (number_value.is_negative_infinity())
  79. return js_string(vm, "-Infinity");
  80. if (number_value.is_nan())
  81. return js_string(vm, "NaN");
  82. if (number_value.is_positive_zero() || number_value.is_negative_zero())
  83. return js_string(vm, "0");
  84. double number = number_value.as_double();
  85. bool negative = number < 0;
  86. if (negative)
  87. number *= -1;
  88. int int_part = floor(number);
  89. double decimal_part = number - int_part;
  90. Vector<char> backwards_characters;
  91. if (int_part == 0) {
  92. backwards_characters.append('0');
  93. } else {
  94. while (int_part > 0) {
  95. backwards_characters.append(digits[int_part % radix]);
  96. int_part /= radix;
  97. }
  98. }
  99. Vector<char> characters;
  100. if (negative)
  101. characters.append('-');
  102. // Reverse characters;
  103. for (ssize_t i = backwards_characters.size() - 1; i >= 0; --i) {
  104. characters.append(backwards_characters[i]);
  105. }
  106. // decimal part
  107. if (decimal_part != 0.0) {
  108. characters.append('.');
  109. int precision = max_precision_for_radix[radix];
  110. for (int i = 0; i < precision; ++i) {
  111. decimal_part *= radix;
  112. int integral = floor(decimal_part);
  113. characters.append(digits[integral]);
  114. decimal_part -= integral;
  115. }
  116. while (characters.last() == '0')
  117. characters.take_last();
  118. }
  119. return js_string(vm, String(characters.data(), characters.size()));
  120. }
  121. }