mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 00:32:45 +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).
		
			
				
	
	
		
			80 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			80 lines
		
	
	
	
		
			2.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibJS/Runtime/Date.h>
 | |
| #include <LibJS/Runtime/GlobalObject.h>
 | |
| #include <LibJS/Runtime/Temporal/TimeZone.h>
 | |
| #include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
 | |
| 
 | |
| namespace JS::Temporal {
 | |
| 
 | |
| // 11.2 The Temporal.TimeZone Constructor, https://tc39.es/proposal-temporal/#sec-temporal-timezone-constructor
 | |
| TimeZoneConstructor::TimeZoneConstructor(Realm& realm)
 | |
|     : NativeFunction(realm.vm().names.TimeZone.as_string(), realm.intrinsics().function_prototype())
 | |
| {
 | |
| }
 | |
| 
 | |
| ThrowCompletionOr<void> TimeZoneConstructor::initialize(Realm& realm)
 | |
| {
 | |
|     MUST_OR_THROW_OOM(NativeFunction::initialize(realm));
 | |
| 
 | |
|     auto& vm = this->vm();
 | |
| 
 | |
|     // 11.3.1 Temporal.TimeZone.prototype, https://tc39.es/proposal-temporal/#sec-temporal.timezone.prototype
 | |
|     define_direct_property(vm.names.prototype, realm.intrinsics().temporal_time_zone_prototype(), 0);
 | |
| 
 | |
|     u8 attr = Attribute::Writable | Attribute::Configurable;
 | |
|     define_native_function(realm, vm.names.from, from, 1, attr);
 | |
| 
 | |
|     define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
 | |
| 
 | |
|     return {};
 | |
| }
 | |
| 
 | |
| // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | |
| ThrowCompletionOr<Value> TimeZoneConstructor::call()
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
| 
 | |
|     // 1. If NewTarget is undefined, then
 | |
|     // a. Throw a TypeError exception.
 | |
|     return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Temporal.TimeZone");
 | |
| }
 | |
| 
 | |
| // 11.2.1 Temporal.TimeZone ( identifier ), https://tc39.es/proposal-temporal/#sec-temporal.timezone
 | |
| ThrowCompletionOr<NonnullGCPtr<Object>> TimeZoneConstructor::construct(FunctionObject& new_target)
 | |
| {
 | |
|     auto& vm = this->vm();
 | |
| 
 | |
|     // 2. Set identifier to ? ToString(identifier).
 | |
|     auto identifier = TRY(vm.argument(0).to_string(vm));
 | |
| 
 | |
|     // 3. If IsTimeZoneOffsetString(identifier) is false, then
 | |
|     if (!is_time_zone_offset_string(identifier)) {
 | |
|         // a. If IsAvailableTimeZoneName(identifier) is false, then
 | |
|         if (!is_available_time_zone_name(identifier)) {
 | |
|             // i. Throw a RangeError exception.
 | |
|             return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, identifier);
 | |
|         }
 | |
| 
 | |
|         // b. Set identifier to ! CanonicalizeTimeZoneName(identifier).
 | |
|         identifier = MUST_OR_THROW_OOM(canonicalize_time_zone_name(vm, identifier));
 | |
|     }
 | |
| 
 | |
|     // 4. Return ? CreateTemporalTimeZone(identifier, NewTarget).
 | |
|     return *TRY(create_temporal_time_zone(vm, identifier, &new_target));
 | |
| }
 | |
| 
 | |
| // 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from
 | |
| JS_DEFINE_NATIVE_FUNCTION(TimeZoneConstructor::from)
 | |
| {
 | |
|     auto item = vm.argument(0);
 | |
| 
 | |
|     // 1. Return ? ToTemporalTimeZone(item).
 | |
|     return TRY(to_temporal_time_zone(vm, item));
 | |
| }
 | |
| 
 | |
| }
 |