mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:12:43 +00:00 
			
		
		
		
	 b84f8fb55b
			
		
	
	
		b84f8fb55b
		
	
	
	
	
		
			
			Some of these are allocated upon initialization of the intrinsics, and some lazily, but in neither case the getters actually return a nullptr. This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has an `operator T&()`), and also has the interesting side effect of forcing us to explicitly use the FunctionObject& overload of call(), as passing a NonnullGCPtr is ambigous - it could implicitly be turned into a Value _or_ a FunctionObject& (so we have to dereference manually).
		
			
				
	
	
		
			62 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibJS/Runtime/AbstractOperations.h>
 | |
| #include <LibJS/Runtime/Error.h>
 | |
| #include <LibJS/Runtime/GlobalObject.h>
 | |
| #include <LibJS/Runtime/WeakRef.h>
 | |
| #include <LibJS/Runtime/WeakRefConstructor.h>
 | |
| 
 | |
| namespace JS {
 | |
| 
 | |
| WeakRefConstructor::WeakRefConstructor(Realm& realm)
 | |
|     : NativeFunction(realm.vm().names.WeakRef.as_string(), realm.intrinsics().function_prototype())
 | |
| {
 | |
| }
 | |
| 
 | |
| ThrowCompletionOr<void> WeakRefConstructor::initialize(Realm& realm)
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
|     MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
 | |
| 
 | |
|     // 26.1.2.1 WeakRef.prototype, https://tc39.es/ecma262/#sec-weak-ref.prototype
 | |
|     define_direct_property(vm.names.prototype, realm.intrinsics().weak_ref_prototype(), 0);
 | |
| 
 | |
|     define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
 | |
| 
 | |
|     return {};
 | |
| }
 | |
| 
 | |
| // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target
 | |
| ThrowCompletionOr<Value> WeakRefConstructor::call()
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
| 
 | |
|     // 1. If NewTarget is undefined, throw a TypeError exception.
 | |
|     return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakRef);
 | |
| }
 | |
| 
 | |
| // 26.1.1.1 WeakRef ( target ), https://tc39.es/ecma262/#sec-weak-ref-target
 | |
| ThrowCompletionOr<NonnullGCPtr<Object>> WeakRefConstructor::construct(FunctionObject& new_target)
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
|     auto target = vm.argument(0);
 | |
| 
 | |
|     // 2. If CanBeHeldWeakly(target) is false, throw a TypeError exception.
 | |
|     if (!can_be_held_weakly(target))
 | |
|         return vm.throw_completion<TypeError>(ErrorType::CannotBeHeldWeakly, TRY_OR_THROW_OOM(vm, target.to_string_without_side_effects()));
 | |
| 
 | |
|     // 3. Let weakRef be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »).
 | |
|     // 4. Perform AddToKeptObjects(target).
 | |
|     // 5. Set weakRef.[[WeakRefTarget]] to target.
 | |
|     // 6. Return weakRef.
 | |
|     if (target.is_object())
 | |
|         return TRY(ordinary_create_from_constructor<WeakRef>(vm, new_target, &Intrinsics::weak_ref_prototype, target.as_object()));
 | |
|     VERIFY(target.is_symbol());
 | |
|     return TRY(ordinary_create_from_constructor<WeakRef>(vm, new_target, &Intrinsics::weak_ref_prototype, target.as_symbol()));
 | |
| }
 | |
| 
 | |
| }
 |