Serenity Operating System
at master 71 lines 2.2 kB view raw
1/* 2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibJS/Runtime/AbstractOperations.h> 8#include <LibJS/Runtime/Error.h> 9#include <LibJS/Runtime/GlobalObject.h> 10#include <LibJS/Runtime/IteratorOperations.h> 11#include <LibJS/Runtime/Set.h> 12#include <LibJS/Runtime/SetConstructor.h> 13 14namespace JS { 15 16SetConstructor::SetConstructor(Realm& realm) 17 : NativeFunction(realm.vm().names.Set.as_string(), *realm.intrinsics().function_prototype()) 18{ 19} 20 21ThrowCompletionOr<void> SetConstructor::initialize(Realm& realm) 22{ 23 auto& vm = this->vm(); 24 MUST_OR_THROW_OOM(NativeFunction::initialize(realm)); 25 26 // 24.2.2.1 Set.prototype, https://tc39.es/ecma262/#sec-set.prototype 27 define_direct_property(vm.names.prototype, realm.intrinsics().set_prototype(), 0); 28 29 define_native_accessor(realm, *vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable); 30 31 define_direct_property(vm.names.length, Value(0), Attribute::Configurable); 32 33 return {}; 34} 35 36// 24.2.1.1 Set ( [ iterable ] ), https://tc39.es/ecma262/#sec-set-iterable 37ThrowCompletionOr<Value> SetConstructor::call() 38{ 39 auto& vm = this->vm(); 40 return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Set); 41} 42 43// 24.2.1.1 Set ( [ iterable ] ), https://tc39.es/ecma262/#sec-set-iterable 44ThrowCompletionOr<NonnullGCPtr<Object>> SetConstructor::construct(FunctionObject& new_target) 45{ 46 auto& vm = this->vm(); 47 48 auto set = TRY(ordinary_create_from_constructor<Set>(vm, new_target, &Intrinsics::set_prototype)); 49 50 if (vm.argument(0).is_nullish()) 51 return set; 52 53 auto adder = TRY(set->get(vm.names.add)); 54 if (!adder.is_function()) 55 return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'add' property of Set"); 56 57 (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> { 58 TRY(JS::call(vm, adder.as_function(), set, iterator_value)); 59 return {}; 60 })); 61 62 return set; 63} 64 65// 24.2.2.2 get Set [ @@species ], https://tc39.es/ecma262/#sec-get-set-@@species 66JS_DEFINE_NATIVE_FUNCTION(SetConstructor::symbol_species_getter) 67{ 68 return vm.this_value(); 69} 70 71}