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