SharedArrayBufferConstructor.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. SharedArrayBufferConstructor::SharedArrayBufferConstructor(Realm& realm)
  15. : NativeFunction(realm.vm().names.SharedArrayBuffer.as_string(), realm.intrinsics().function_prototype())
  16. {
  17. }
  18. void SharedArrayBufferConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. // 25.2.4.2 SharedArrayBuffer.prototype, https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().shared_array_buffer_prototype(), 0);
  24. // 25.2.4.4 SharedArrayBuffer.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-sharedarraybuffer.prototype.toString
  25. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  26. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  27. }
  28. // 25.2.2.1 SharedArrayBuffer ( length ), https://tc39.es/ecma262/#sec-sharedarraybuffer-length
  29. ThrowCompletionOr<Value> SharedArrayBufferConstructor::call()
  30. {
  31. auto& vm = this->vm();
  32. // 1. If NewTarget is undefined, throw a TypeError exception.
  33. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.SharedArrayBuffer);
  34. }
  35. // 25.2.2.1 SharedArrayBuffer ( length ), https://tc39.es/ecma262/#sec-sharedarraybuffer-length
  36. ThrowCompletionOr<NonnullGCPtr<Object>> SharedArrayBufferConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. // 2. Let byteLength be ? ToIndex(length).
  40. auto byte_length_or_error = vm.argument(0).to_index(vm);
  41. if (byte_length_or_error.is_error()) {
  42. auto error = byte_length_or_error.release_error();
  43. if (error.value()->is_object() && is<RangeError>(error.value()->as_object())) {
  44. // Re-throw more specific RangeError
  45. return vm.throw_completion<RangeError>(ErrorType::InvalidLength, "shared array buffer");
  46. }
  47. return error;
  48. }
  49. // 3. Return ? AllocateSharedArrayBuffer(NewTarget, byteLength).
  50. return *TRY(allocate_shared_array_buffer(vm, new_target, byte_length_or_error.release_value()));
  51. }
  52. // 25.2.3.2 get SharedArrayBuffer [ @@species ], https://tc39.es/ecma262/#sec-sharedarraybuffer-@@species
  53. JS_DEFINE_NATIVE_FUNCTION(SharedArrayBufferConstructor::symbol_species_getter)
  54. {
  55. // 1. Return the this value.
  56. return vm.this_value();
  57. }
  58. }