BigIntConstructor.cpp 3.8 KB

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