NumberConstructor.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Math.h>
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/NumberConstructor.h>
  11. #include <LibJS/Runtime/NumberObject.h>
  12. #ifdef __clang__
  13. # define EPSILON_VALUE AK::exp2(-52.)
  14. # define MAX_SAFE_INTEGER_VALUE AK::exp2(53.) - 1
  15. # define MIN_SAFE_INTEGER_VALUE -(AK::exp2(53.) - 1)
  16. #else
  17. constexpr double const EPSILON_VALUE { __builtin_exp2(-52) };
  18. constexpr double const MAX_SAFE_INTEGER_VALUE { __builtin_exp2(53) - 1 };
  19. constexpr double const MIN_SAFE_INTEGER_VALUE { -(__builtin_exp2(53) - 1) };
  20. #endif
  21. namespace JS {
  22. NumberConstructor::NumberConstructor(Realm& realm)
  23. : NativeFunction(vm().names.Number.as_string(), *realm.global_object().function_prototype())
  24. {
  25. }
  26. void NumberConstructor::initialize(Realm& realm)
  27. {
  28. auto& vm = this->vm();
  29. NativeFunction::initialize(realm);
  30. // 21.1.2.15 Number.prototype, https://tc39.es/ecma262/#sec-number.prototype
  31. define_direct_property(vm.names.prototype, realm.global_object().number_prototype(), 0);
  32. u8 attr = Attribute::Writable | Attribute::Configurable;
  33. define_native_function(vm.names.isFinite, is_finite, 1, attr);
  34. define_native_function(vm.names.isInteger, is_integer, 1, attr);
  35. define_native_function(vm.names.isNaN, is_nan, 1, attr);
  36. define_native_function(vm.names.isSafeInteger, is_safe_integer, 1, attr);
  37. // FIXME: Store these as intrinsics (`parse_int_function()`) instead of getting them from the global object
  38. define_direct_property(vm.names.parseInt, realm.global_object().get_without_side_effects(vm.names.parseInt), attr);
  39. define_direct_property(vm.names.parseFloat, realm.global_object().get_without_side_effects(vm.names.parseFloat), attr);
  40. define_direct_property(vm.names.EPSILON, Value(EPSILON_VALUE), 0);
  41. define_direct_property(vm.names.MAX_VALUE, Value(NumericLimits<double>::max()), 0);
  42. define_direct_property(vm.names.MIN_VALUE, Value(NumericLimits<double>::min()), 0);
  43. define_direct_property(vm.names.MAX_SAFE_INTEGER, Value(MAX_SAFE_INTEGER_VALUE), 0);
  44. define_direct_property(vm.names.MIN_SAFE_INTEGER, Value(MIN_SAFE_INTEGER_VALUE), 0);
  45. define_direct_property(vm.names.NEGATIVE_INFINITY, js_negative_infinity(), 0);
  46. define_direct_property(vm.names.POSITIVE_INFINITY, js_infinity(), 0);
  47. define_direct_property(vm.names.NaN, js_nan(), 0);
  48. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  49. }
  50. // Most of 21.1.1.1 Number ( value ) factored into a separate function for sharing between call() and construct().
  51. static ThrowCompletionOr<Value> get_value_from_constructor_argument(GlobalObject& global_object)
  52. {
  53. auto& vm = global_object.vm();
  54. Value number;
  55. if (vm.argument_count() > 0) {
  56. auto primitive = TRY(vm.argument(0).to_numeric(vm));
  57. if (primitive.is_bigint()) {
  58. // FIXME: How should huge values be handled here?
  59. auto& big_integer = primitive.as_bigint().big_integer();
  60. number = Value(static_cast<double>(big_integer.unsigned_value().to_u64()) * (big_integer.is_negative() ? -1.0 : 1.0));
  61. } else {
  62. number = primitive;
  63. }
  64. } else {
  65. number = Value(0);
  66. }
  67. return number;
  68. }
  69. // 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value
  70. ThrowCompletionOr<Value> NumberConstructor::call()
  71. {
  72. return get_value_from_constructor_argument(global_object());
  73. }
  74. // 21.1.1.1 Number ( value ), https://tc39.es/ecma262/#sec-number-constructor-number-value
  75. ThrowCompletionOr<Object*> NumberConstructor::construct(FunctionObject& new_target)
  76. {
  77. auto& global_object = this->global_object();
  78. auto number = TRY(get_value_from_constructor_argument(global_object));
  79. return TRY(ordinary_create_from_constructor<NumberObject>(global_object, new_target, &GlobalObject::number_prototype, number.as_double()));
  80. }
  81. // 21.1.2.2 Number.isFinite ( number ), https://tc39.es/ecma262/#sec-number.isfinite
  82. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_finite)
  83. {
  84. return Value(vm.argument(0).is_finite_number());
  85. }
  86. // 21.1.2.3 Number.isInteger ( number ), https://tc39.es/ecma262/#sec-number.isinteger
  87. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_integer)
  88. {
  89. return Value(vm.argument(0).is_integral_number());
  90. }
  91. // 21.1.2.4 Number.isNaN ( number ), https://tc39.es/ecma262/#sec-number.isnan
  92. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_nan)
  93. {
  94. return Value(vm.argument(0).is_nan());
  95. }
  96. // 21.1.2.5 Number.isSafeInteger ( number ), https://tc39.es/ecma262/#sec-number.issafeinteger
  97. JS_DEFINE_NATIVE_FUNCTION(NumberConstructor::is_safe_integer)
  98. {
  99. if (!vm.argument(0).is_number())
  100. return Value(false);
  101. if (!vm.argument(0).is_integral_number())
  102. return Value(false);
  103. auto value = vm.argument(0).as_double();
  104. return Value(value >= MIN_SAFE_INTEGER_VALUE && value <= MAX_SAFE_INTEGER_VALUE);
  105. }
  106. }