NumberConstructor.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/NumberConstructor.h>
  9. #include <LibJS/Runtime/NumberObject.h>
  10. #include <math.h>
  11. #ifdef __clang__
  12. # define EPSILON_VALUE pow(2, -52)
  13. # define MAX_SAFE_INTEGER_VALUE pow(2, 53) - 1
  14. # define MIN_SAFE_INTEGER_VALUE -(pow(2, 53) - 1)
  15. #else
  16. constexpr const double EPSILON_VALUE { __builtin_pow(2, -52) };
  17. constexpr const double MAX_SAFE_INTEGER_VALUE { __builtin_pow(2, 53) - 1 };
  18. constexpr const double MIN_SAFE_INTEGER_VALUE { -(__builtin_pow(2, 53) - 1) };
  19. #endif
  20. namespace JS {
  21. NumberConstructor::NumberConstructor(GlobalObject& global_object)
  22. : NativeFunction(vm().names.Number, *global_object.function_prototype())
  23. {
  24. }
  25. void NumberConstructor::initialize(GlobalObject& global_object)
  26. {
  27. auto& vm = this->vm();
  28. NativeFunction::initialize(global_object);
  29. u8 attr = Attribute::Writable | Attribute::Configurable;
  30. define_native_function(vm.names.isFinite, is_finite, 1, attr);
  31. define_native_function(vm.names.isInteger, is_integer, 1, attr);
  32. define_native_function(vm.names.isNaN, is_nan, 1, attr);
  33. define_native_function(vm.names.isSafeInteger, is_safe_integer, 1, attr);
  34. define_property(vm.names.parseFloat, global_object.get(vm.names.parseFloat));
  35. define_property(vm.names.prototype, global_object.number_prototype(), 0);
  36. define_property(vm.names.length, Value(1), Attribute::Configurable);
  37. define_property(vm.names.EPSILON, Value(EPSILON_VALUE), 0);
  38. define_property(vm.names.MAX_VALUE, Value(NumericLimits<double>::max()), 0);
  39. define_property(vm.names.MIN_VALUE, Value(NumericLimits<double>::min()), 0);
  40. define_property(vm.names.MAX_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0);
  41. define_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0);
  42. define_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0);
  43. define_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0);
  44. define_property(vm.names.NaN, js_nan(), 0);
  45. }
  46. NumberConstructor::~NumberConstructor()
  47. {
  48. }
  49. Value NumberConstructor::call()
  50. {
  51. if (!vm().argument_count())
  52. return Value(0);
  53. return vm().argument(0).to_number(global_object());
  54. }
  55. Value NumberConstructor::construct(Function&)
  56. {
  57. double number = 0;
  58. if (vm().argument_count()) {
  59. number = vm().argument(0).to_double(global_object());
  60. if (vm().exception())
  61. return {};
  62. }
  63. return NumberObject::create(global_object(), number);
  64. }
  65. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite)
  66. {
  67. return Value(vm.argument(0).is_finite_number());
  68. }
  69. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer)
  70. {
  71. return Value(vm.argument(0).is_integer());
  72. }
  73. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan)
  74. {
  75. return Value(vm.argument(0).is_nan());
  76. }
  77. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer)
  78. {
  79. if (!vm.argument(0).is_number())
  80. return Value(false);
  81. auto value = vm.argument(0).as_double();
  82. return Value((int64_t)value == value && value >= MIN_SAFE_INTEGER_VALUE && value <= MAX_SAFE_INTEGER_VALUE);
  83. }
  84. }