NumberConstructor.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0);
  39. define_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0);
  40. define_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0);
  41. define_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0);
  42. define_property(vm.names.NaN, js_nan(), 0);
  43. }
  44. NumberConstructor::~NumberConstructor()
  45. {
  46. }
  47. Value NumberConstructor::call()
  48. {
  49. if (!vm().argument_count())
  50. return Value(0);
  51. return vm().argument(0).to_number(global_object());
  52. }
  53. Value NumberConstructor::construct(Function&)
  54. {
  55. double number = 0;
  56. if (vm().argument_count()) {
  57. number = vm().argument(0).to_double(global_object());
  58. if (vm().exception())
  59. return {};
  60. }
  61. return NumberObject::create(global_object(), number);
  62. }
  63. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite)
  64. {
  65. return Value(vm.argument(0).is_finite_number());
  66. }
  67. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer)
  68. {
  69. return Value(vm.argument(0).is_integer());
  70. }
  71. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan)
  72. {
  73. return Value(vm.argument(0).is_nan());
  74. }
  75. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer)
  76. {
  77. if (!vm.argument(0).is_number())
  78. return Value(false);
  79. auto value = vm.argument(0).as_double();
  80. return Value((int64_t)value == value && value >= MIN_SAFE_INTEGER_VALUE && value <= MAX_SAFE_INTEGER_VALUE);
  81. }
  82. }