NumberConstructor.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  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/Runtime/Error.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibJS/Runtime/NumberConstructor.h>
  29. #include <LibJS/Runtime/NumberObject.h>
  30. #include <math.h>
  31. #ifdef __clang__
  32. # define EPSILON_VALUE pow(2, -52)
  33. # define MAX_SAFE_INTEGER_VALUE pow(2, 53) - 1
  34. # define MIN_SAFE_INTEGER_VALUE -(pow(2, 53) - 1)
  35. #else
  36. constexpr const double EPSILON_VALUE { __builtin_pow(2, -52) };
  37. constexpr const double MAX_SAFE_INTEGER_VALUE { __builtin_pow(2, 53) - 1 };
  38. constexpr const double MIN_SAFE_INTEGER_VALUE { -(__builtin_pow(2, 53) - 1) };
  39. #endif
  40. namespace JS {
  41. NumberConstructor::NumberConstructor(GlobalObject& global_object)
  42. : NativeFunction(vm().names.Number, *global_object.function_prototype())
  43. {
  44. }
  45. void NumberConstructor::initialize(GlobalObject& global_object)
  46. {
  47. auto& vm = this->vm();
  48. NativeFunction::initialize(global_object);
  49. u8 attr = Attribute::Writable | Attribute::Configurable;
  50. define_native_function(vm.names.isFinite, is_finite, 1, attr);
  51. define_native_function(vm.names.isInteger, is_integer, 1, attr);
  52. define_native_function(vm.names.isNaN, is_nan, 1, attr);
  53. define_native_function(vm.names.isSafeInteger, is_safe_integer, 1, attr);
  54. define_property(vm.names.parseFloat, global_object.get(vm.names.parseFloat));
  55. define_property(vm.names.prototype, global_object.number_prototype(), 0);
  56. define_property(vm.names.length, Value(1), Attribute::Configurable);
  57. define_property(vm.names.EPSILON, Value(EPSILON_VALUE), 0);
  58. define_property(vm.names.MAX_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0);
  59. define_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0);
  60. define_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0);
  61. define_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0);
  62. define_property(vm.names.NaN, js_nan(), 0);
  63. }
  64. NumberConstructor::~NumberConstructor()
  65. {
  66. }
  67. Value NumberConstructor::call()
  68. {
  69. if (!vm().argument_count())
  70. return Value(0);
  71. return vm().argument(0).to_number(global_object());
  72. }
  73. Value NumberConstructor::construct(Function&)
  74. {
  75. double number = 0;
  76. if (vm().argument_count()) {
  77. number = vm().argument(0).to_double(global_object());
  78. if (vm().exception())
  79. return {};
  80. }
  81. return NumberObject::create(global_object(), number);
  82. }
  83. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite)
  84. {
  85. return Value(vm.argument(0).is_finite_number());
  86. }
  87. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer)
  88. {
  89. return Value(vm.argument(0).is_integer());
  90. }
  91. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan)
  92. {
  93. return Value(vm.argument(0).is_nan());
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer)
  96. {
  97. if (!vm.argument(0).is_number())
  98. return Value(false);
  99. auto value = vm.argument(0).as_double();
  100. return Value((int64_t)value == value && value >= MIN_SAFE_INTEGER_VALUE && value <= MAX_SAFE_INTEGER_VALUE);
  101. }
  102. }