ArrayBufferConstructor.cpp 2.9 KB

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