ArrayBufferConstructor.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. ThrowCompletionOr<Value> ArrayBufferConstructor::call()
  34. {
  35. auto& vm = this->vm();
  36. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ArrayBuffer);
  37. }
  38. // 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
  39. ThrowCompletionOr<Object*> ArrayBufferConstructor::construct(FunctionObject& new_target)
  40. {
  41. auto& vm = this->vm();
  42. auto byte_length_or_error = vm.argument(0).to_index(global_object());
  43. if (byte_length_or_error.is_error()) {
  44. auto error = byte_length_or_error.release_error();
  45. if (error.value().is_object() && is<RangeError>(error.value().as_object())) {
  46. // Re-throw more specific RangeError
  47. vm.clear_exception();
  48. return vm.throw_completion<RangeError>(global_object(), ErrorType::InvalidLength, "array buffer");
  49. }
  50. return error;
  51. }
  52. return TRY(allocate_array_buffer(global_object(), new_target, byte_length_or_error.release_value()));
  53. }
  54. // 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
  55. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::is_view)
  56. {
  57. auto arg = vm.argument(0);
  58. if (!arg.is_object())
  59. return Value(false);
  60. if (arg.as_object().is_typed_array())
  61. return Value(true);
  62. if (is<DataView>(arg.as_object()))
  63. return Value(true);
  64. return Value(false);
  65. }
  66. // 25.1.4.3 get ArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
  67. JS_DEFINE_NATIVE_FUNCTION(ArrayBufferConstructor::symbol_species_getter)
  68. {
  69. return vm.this_value(global_object);
  70. }
  71. }