BigIntConstructor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/BigInt.h>
  9. #include <LibJS/Runtime/BigIntConstructor.h>
  10. #include <LibJS/Runtime/BigIntObject.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/GlobalObject.h>
  13. #include <LibJS/Runtime/VM.h>
  14. namespace JS {
  15. static const Crypto::SignedBigInteger BIGINT_ONE { 1 };
  16. BigIntConstructor::BigIntConstructor(GlobalObject& global_object)
  17. : NativeFunction(vm().names.BigInt.as_string(), *global_object.function_prototype())
  18. {
  19. }
  20. void BigIntConstructor::initialize(GlobalObject& global_object)
  21. {
  22. auto& vm = this->vm();
  23. NativeFunction::initialize(global_object);
  24. // 21.2.2.3 BigInt.prototype, https://tc39.es/ecma262/#sec-bigint.prototype
  25. define_direct_property(vm.names.prototype, global_object.bigint_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(vm.names.asIntN, as_int_n, 2, attr);
  28. // define_native_function(vm.names.asUintN, as_uint_n, 2, attr);
  29. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  30. }
  31. BigIntConstructor::~BigIntConstructor()
  32. {
  33. }
  34. // 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
  35. ThrowCompletionOr<Value> BigIntConstructor::call()
  36. {
  37. auto& vm = this->vm();
  38. auto& global_object = this->global_object();
  39. auto value = vm.argument(0);
  40. // 2. Let prim be ? ToPrimitive(value, number).
  41. auto primitive = TRY(value.to_primitive(global_object, Value::PreferredType::Number));
  42. // 3. If Type(prim) is Number, return ? NumberToBigInt(prim).
  43. if (primitive.is_number())
  44. return TRY(number_to_bigint(global_object, primitive));
  45. // 4. Otherwise, return ? ToBigInt(value).
  46. return TRY(value.to_bigint(global_object));
  47. }
  48. // 21.2.1.1 BigInt ( value ), https://tc39.es/ecma262/#sec-bigint-constructor-number-value
  49. ThrowCompletionOr<Object*> BigIntConstructor::construct(FunctionObject&)
  50. {
  51. return vm().throw_completion<TypeError>(global_object(), ErrorType::NotAConstructor, "BigInt");
  52. }
  53. // 21.2.2.1 BigInt.asIntN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asintn
  54. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
  55. {
  56. // 1. Set bits to ? ToIndex(bits).
  57. auto bits = TRY(vm.argument(0).to_index(global_object));
  58. // 2. Set bigint to ? ToBigInt(bigint).
  59. auto* bigint = TRY(vm.argument(1).to_bigint(global_object));
  60. // 3. Let mod be ℝ(bigint) modulo 2^bits.
  61. // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to
  62. // drop the most significant bits.
  63. auto bits_shift_left = BIGINT_ONE.shift_left(bits);
  64. auto mod = modulo(bigint->big_integer(), bits_shift_left);
  65. // 4. If mod ≥ 2^(bits-1), return ℤ(mod - 2^bits); otherwise, return ℤ(mod).
  66. // NOTE: Some of the below conditionals are non-standard, but are to protect SignedBigInteger from
  67. // allocating an absurd amount of memory if `bits - 1` overflows to NumericLimits<size_t>::max.
  68. if ((bits == 0) && (mod >= BIGINT_ONE))
  69. return js_bigint(vm, mod.minus(bits_shift_left));
  70. if ((bits > 0) && (mod >= BIGINT_ONE.shift_left(bits - 1)))
  71. return js_bigint(vm, mod.minus(bits_shift_left));
  72. return js_bigint(vm, mod);
  73. }
  74. // 21.2.2.2 BigInt.asUintN ( bits, bigint ), https://tc39.es/ecma262/#sec-bigint.asuintn
  75. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
  76. {
  77. TODO();
  78. }
  79. }