ArrayBufferConstructor.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ArrayBuffer.h>
  7. #include <LibJS/Runtime/ArrayBufferConstructor.h>
  8. #include <LibJS/Runtime/DataView.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/TypedArray.h>
  12. namespace JS {
  13. ArrayBufferConstructor::ArrayBufferConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.ArrayBuffer.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void ArrayBufferConstructor::initialize(GlobalObject& global_object)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(global_object);
  21. // 25.1.4.2 ArrayBuffer.prototype, https://tc39.es/ecma262/#sec-arraybuffer.prototype
  22. define_direct_property(vm.names.prototype, global_object.array_buffer_prototype(), 0);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(vm.names.isView, is_view, 1, attr);
  25. // 25.1.5.4 ArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-arraybuffer.prototype-@@tostringtag
  26. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  27. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  28. }
  29. ArrayBufferConstructor::~ArrayBufferConstructor()
  30. {
  31. }
  32. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  33. Value ArrayBufferConstructor::call()
  34. {
  35. auto& vm = this->vm();
  36. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  37. return {};
  38. }
  39. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  40. Value ArrayBufferConstructor::construct(FunctionObject&)
  41. {
  42. auto& vm = this->vm();
  43. auto byte_length = vm.argument(0).to_index(global_object());
  44. if (vm.exception()) {
  45. if (vm.exception()->value().is_object() && is<RangeError>(vm.exception()->value().as_object())) {
  46. // Re-throw more specific RangeError
  47. vm.clear_exception();
  48. vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array buffer");
  49. }
  50. return {};
  51. }
  52. auto array_buffer = ArrayBuffer::create(global_object(), byte_length);
  53. if (!array_buffer)
  54. return {};
  55. return array_buffer;
  56. }
  57. // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  58. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  59. {
  60. auto arg = vm.argument(0);
  61. if (!arg.is_object())
  62. return Value(false);
  63. if (arg.as_object().is_typed_array())
  64. return Value(true);
  65. if (is<DataView>(arg.as_object()))
  66. return Value(true);
  67. return Value(false);
  68. }
  69. // 25.1.4.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  70. JS_DEFINE_NATIVE_GETTER(ArrayBufferConstructor::symbol_species_getter)
  71. {
  72. return vm.this_value(global_object);
  73. }
  74. }