NumberPrototype.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <LibJS/Interpreter.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. 0, 0, 52, 32, 26, 22, 20, 18, 17, 16,
  34. 15, 15, 14, 14, 13, 13, 13, 12, 12, 12,
  35. 12, 11, 11, 11, 11, 11, 11, 10, 10, 10,
  36. 10, 10, 10, 10, 10, 10, 10,
  37. };
  38. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  39. NumberPrototype::NumberPrototype(GlobalObject& global_object)
  40. : NumberObject(0, *global_object.object_prototype())
  41. {
  42. }
  43. void NumberPrototype::initialize(GlobalObject& object)
  44. {
  45. Object::initialize(object);
  46. define_native_function("toString", to_string, 1, Attribute::Configurable | Attribute::Writable);
  47. }
  48. NumberPrototype::~NumberPrototype()
  49. {
  50. }
  51. JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
  52. {
  53. Value number_value;
  54. auto this_value = interpreter.this_value(global_object);
  55. if (this_value.is_number()) {
  56. number_value = this_value;
  57. } else if (this_value.is_object() && this_value.as_object().is_number_object()) {
  58. number_value = static_cast<NumberObject&>(this_value.as_object()).value_of();
  59. } else {
  60. interpreter.throw_exception<TypeError>(ErrorType::NumberIncompatibleThis, "toString");
  61. return {};
  62. }
  63. int radix;
  64. auto argument = interpreter.argument(0);
  65. if (argument.is_undefined()) {
  66. radix = 10;
  67. } else {
  68. radix = argument.to_i32(interpreter);
  69. }
  70. if (interpreter.exception() || radix < 2 || radix > 36) {
  71. interpreter.throw_exception<RangeError>(ErrorType::InvalidRadix);
  72. return {};
  73. }
  74. if (number_value.is_positive_infinity())
  75. return js_string(interpreter, "Infinity");
  76. if (number_value.is_negative_infinity())
  77. return js_string(interpreter, "-Infinity");
  78. if (number_value.is_nan())
  79. return js_string(interpreter, "NaN");
  80. if (number_value.is_positive_zero() || number_value.is_negative_zero())
  81. return js_string(interpreter, "0");
  82. double number = number_value.as_double();
  83. bool negative = number < 0;
  84. if (negative)
  85. number *= -1;
  86. int int_part = floor(number);
  87. double decimal_part = number - int_part;
  88. Vector<char> backwards_characters;
  89. if (int_part == 0) {
  90. backwards_characters.append('0');
  91. } else {
  92. while (int_part > 0) {
  93. backwards_characters.append(digits[int_part % radix]);
  94. int_part /= radix;
  95. }
  96. }
  97. Vector<char> characters;
  98. if (negative)
  99. characters.append('-');
  100. // Reverse characters;
  101. for (ssize_t i = backwards_characters.size() - 1; i >= 0; --i) {
  102. characters.append(backwards_characters[i]);
  103. }
  104. // decimal part
  105. if (decimal_part != 0.0) {
  106. characters.append('.');
  107. int precision = max_precision_for_radix[radix];
  108. for (int i = 0; i < precision; ++i) {
  109. decimal_part *= radix;
  110. int integral = floor(decimal_part);
  111. characters.append(digits[integral]);
  112. decimal_part -= integral;
  113. }
  114. while (characters.last() == '0')
  115. characters.take_last();
  116. }
  117. return js_string(interpreter, String(characters.data(), characters.size()));
  118. }
  119. }