BigIntConstructor.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <LibCrypto/BigInt/SignedBigInteger.h>
  28. #include <LibJS/Runtime/BigIntConstructor.h>
  29. #include <LibJS/Runtime/BigIntObject.h>
  30. #include <LibJS/Runtime/Error.h>
  31. #include <LibJS/Runtime/GlobalObject.h>
  32. #include <LibJS/Runtime/VM.h>
  33. namespace JS {
  34. BigIntConstructor::BigIntConstructor(GlobalObject& global_object)
  35. : NativeFunction("BigInt", *global_object.function_prototype())
  36. {
  37. }
  38. void BigIntConstructor::initialize(GlobalObject& global_object)
  39. {
  40. NativeFunction::initialize(global_object);
  41. define_property("prototype", global_object.bigint_prototype(), 0);
  42. define_property("length", Value(1), Attribute::Configurable);
  43. u8 attr = Attribute::Writable | Attribute::Configurable;
  44. define_native_function("asIntN", as_int_n, 2, attr);
  45. define_native_function("asUintN", as_uint_n, 2, attr);
  46. }
  47. BigIntConstructor::~BigIntConstructor()
  48. {
  49. }
  50. Value BigIntConstructor::call()
  51. {
  52. auto primitive = vm().argument(0).to_primitive(Value::PreferredType::Number);
  53. if (vm().exception())
  54. return {};
  55. if (primitive.is_number()) {
  56. if (!primitive.is_integer()) {
  57. vm().throw_exception<RangeError>(global_object(), ErrorType::BigIntIntArgument);
  58. return {};
  59. }
  60. return js_bigint(heap(), Crypto::SignedBigInteger { primitive.as_i32() });
  61. }
  62. auto* bigint = vm().argument(0).to_bigint(global_object());
  63. if (vm().exception())
  64. return {};
  65. return bigint;
  66. }
  67. Value BigIntConstructor::construct(Function&)
  68. {
  69. vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, "BigInt");
  70. return {};
  71. }
  72. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n)
  73. {
  74. TODO();
  75. }
  76. JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n)
  77. {
  78. TODO();
  79. }
  80. }