BigIntConstructor.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <LibCrypto/BigInt/SignedBigInteger.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. BigIntConstructor::BigIntConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.BigInt, *global_object.function_prototype())
  16. {
  17. }
  18. void BigIntConstructor::initialize(GlobalObject& global_object)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::initialize(global_object);
  22. define_property(vm.names.prototype, global_object.bigint_prototype(), 0);
  23. define_property(vm.names.length, Value(1), Attribute::Configurable);
  24. // TODO: Implement these functions below and uncomment this.
  25. // u8 attr = Attribute::Writable | Attribute::Configurable;
  26. // define_native_function(vm.names.asIntN, as_int_n, 2, attr);
  27. // define_native_function(vm.names.asUintN, as_uint_n, 2, attr);
  28. }
  29. BigIntConstructor::~BigIntConstructor()
  30. {
  31. }
  32. Value BigIntConstructor::call()
  33. {
  34. auto primitive = vm().argument(0).to_primitive(global_object(), Value::PreferredType::Number);
  35. if (vm().exception())
  36. return {};
  37. if (primitive.is_number()) {
  38. if (!primitive.is_integer()) {
  39. vm().throw_exception<RangeError>(global_object(), ErrorType::BigIntIntArgument);
  40. return {};
  41. }
  42. return js_bigint(heap(), Crypto::SignedBigInteger { primitive.as_i32() });
  43. }
  44. auto* bigint = vm().argument(0).to_bigint(global_object());
  45. if (vm().exception())
  46. return {};
  47. return bigint;
  48. }
  49. Value BigIntConstructor::construct(Function&)
  50. {
  51. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "BigInt");
  52. return {};
  53. }
  54. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
  55. {
  56. TODO();
  57. }
  58. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
  59. {
  60. TODO();
  61. }
  62. }