SharedArrayBufferConstructor.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Forward.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/SharedArrayBufferConstructor.h>
  12. #include <LibJS/Runtime/TypedArray.h>
  13. namespace JS {
  14. JS_DEFINE_ALLOCATOR(SharedArrayBufferConstructor);
  15. SharedArrayBufferConstructor::SharedArrayBufferConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.SharedArrayBuffer.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void SharedArrayBufferConstructor::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. Base::initialize(realm);
  23. // 25.2.4.1 SharedArrayBuffer.prototype, https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().shared_array_buffer_prototype(), 0);
  25. // 25.2.5.7 SharedArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype.toString
  26. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  27. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  28. }
  29. // 25.2.3.1 SharedArrayBuffer ( length [ , options ] ), https://tc39.es/ecma262/#sec-sharedarraybuffer-length
  30. ThrowCompletionOr<Value> SharedArrayBufferConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined, throw a TypeError exception.
  34. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.SharedArrayBuffer);
  35. }
  36. // 25.2.3.1 SharedArrayBuffer ( length [ , options ] ), https://tc39.es/ecma262/#sec-sharedarraybuffer-length
  37. ThrowCompletionOr<NonnullGCPtr<Object>> SharedArrayBufferConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. // 2. Let byteLength be ? ToIndex(length).
  41. auto byte_length_or_error = vm.argument(0).to_index(vm);
  42. if (byte_length_or_error.is_error()) {
  43. auto error = byte_length_or_error.release_error();
  44. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  45. // Re-throw more specific RangeError
  46. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "shared array buffer");
  47. }
  48. return error;
  49. }
  50. // 3. Return ? AllocateSharedArrayBuffer(NewTarget, byteLength).
  51. return *TRY(allocate_shared_array_buffer(vm, new_target, byte_length_or_error.release_value()));
  52. }
  53. // 25.2.4.2 get SharedArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-sharedarraybuffer-@@species
  54. JS_DEFINE_NATIVE_FUNCTION(SharedArrayBufferConstructor::symbol_species_getter)
  55. {
  56. // 1. Return the this value.
  57. return vm.this_value();
  58. }
  59. }