2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								/*
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  Copyright  ( c )  2022 ,  Idan  Horowitz  < idan . horowitz @ serenityos . org > 
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 *  Copyright  ( c )  2023 - 2025 ,  Tim  Flynn  < trflynn89 @ ladybird . org > 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								 * 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 *  SPDX - License - Identifier :  BSD - 2 - Clause 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 */ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/AbstractOperations.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:53:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/Array.h> 
  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/GlobalObject.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/Intl/AbstractOperations.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/Intl/Segmenter.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/Intl/SegmenterConstructor.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <LibJS/Runtime/Temporal/AbstractOperations.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								namespace  JS : : Intl  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-11-15 04:01:23 +13:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								GC_DEFINE_ALLOCATOR ( SegmenterConstructor ) ;  
						 
					
						
							
								
									
										
										
										
											2023-11-19 09:45:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// 19.1 The Intl.Segmenter Constructor, https://tc39.es/ecma402/#sec-intl-segmenter-constructor
  
						 
					
						
							
								
									
										
										
										
											2022-08-16 00:20:49 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								SegmenterConstructor : : SegmenterConstructor ( Realm &  realm )  
						 
					
						
							
								
									
										
										
										
											2023-04-13 00:47:15 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    :  NativeFunction ( realm . vm ( ) . names . Segmenter . as_string ( ) ,  realm . intrinsics ( ) . function_prototype ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-08-07 08:41:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  SegmenterConstructor : : initialize ( Realm &  realm )  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2023-08-07 08:41:28 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Base : : initialize ( realm ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto &  vm  =  this - > vm ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 19.2.1 Intl.Segmenter.prototype, https://tc39.es/ecma402/#sec-intl.segmenter.prototype
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-27 00:54:55 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    define_direct_property ( vm . names . prototype ,  realm . intrinsics ( ) . intl_segmenter_prototype ( ) ,  0 ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    define_direct_property ( vm . names . length ,  Value ( 0 ) ,  Attribute : : Configurable ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:53:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    u8  attr  =  Attribute : : Writable  |  Attribute : : Configurable ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-22 21:47:35 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    define_native_function ( realm ,  vm . names . supportedLocalesOf ,  supported_locales_of ,  1 ,  attr ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// 19.1.1 Intl.Segmenter ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.segmenter
  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								ThrowCompletionOr < Value >  SegmenterConstructor : : call ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 1. If NewTarget is undefined, throw a TypeError exception.
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 20:33:17 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  vm ( ) . throw_completion < TypeError > ( ErrorType : : ConstructorWithoutNew ,  " Intl.Segmenter " ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// 19.1.1 Intl.Segmenter ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.segmenter
  
						 
					
						
							
								
									
										
										
										
											2024-11-15 04:01:23 +13:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ThrowCompletionOr < GC : : Ref < Object > >  SegmenterConstructor : : construct ( FunctionObject &  new_target )  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto &  vm  =  this - > vm ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto  locales  =  vm . argument ( 0 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto  options_value  =  vm . argument ( 1 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 2. Let internalSlotsList be « [[InitializedSegmenter]], [[Locale]], [[SegmenterGranularity]] ».
 
							 
						 
					
						
							
								
									
										
										
										
											2024-08-15 15:55:33 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 3. Let segmenter be ? OrdinaryCreateFromConstructor(NewTarget, "%Intl.Segmenter.prototype%", internalSlotsList).
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-14 18:34:32 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  segmenter  =  TRY ( ordinary_create_from_constructor < Segmenter > ( vm ,  new_target ,  & Intrinsics : : intl_segmenter_prototype ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 4. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-20 08:25:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  requested_locales  =  TRY ( canonicalize_locale_list ( vm ,  locales ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 5. Set options to ? GetOptionsObject(options).
 
							 
						 
					
						
							
								
									
										
										
										
											2024-11-18 10:36:03 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  options  =  TRY ( Temporal : : get_options_object ( vm ,  options_value ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 6. Let opt be a new Record.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    LocaleOptions  opt  { } ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-01-12 10:22:37 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 7. Let matcher be ? GetOption(options, "localeMatcher", string, « "lookup", "best fit" », "best fit").
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-20 08:52:42 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  matcher  =  TRY ( get_option ( vm ,  * options ,  vm . names . localeMatcher ,  OptionType : : String ,  {  " lookup " sv ,  " best fit " sv  } ,  " best fit " sv ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 8. Set opt.[[localeMatcher]] to matcher.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    opt . locale_matcher  =  matcher ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 18:51:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 9. Let r be ResolveLocale(%Intl.Segmenter%.[[AvailableLocales]], requestedLocales, opt, %Intl.Segmenter%.[[RelevantExtensionKeys]], %Intl.Segmenter%.[[LocaleData]]).
 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-30 11:08:15 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  result  =  resolve_locale ( requested_locales ,  opt ,  { } ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 18:51:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 10. Set segmenter.[[Locale]] to r.[[locale]].
 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    segmenter - > set_locale ( move ( result . locale ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 18:51:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 11. Let granularity be ? GetOption(options, "granularity", string, « "grapheme", "word", "sentence" », "grapheme").
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-20 08:52:42 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  granularity  =  TRY ( get_option ( vm ,  * options ,  vm . names . granularity ,  OptionType : : String ,  {  " grapheme " sv ,  " word " sv ,  " sentence " sv  } ,  " grapheme " sv ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 18:51:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 12. Set segmenter.[[SegmenterGranularity]] to granularity.
 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-08 19:17:55 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    segmenter - > set_segmenter_granularity ( granularity . as_string ( ) . utf8_string_view ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-23 09:14:27 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  locale_segmenter  =  Unicode : : Segmenter : : create ( segmenter - > locale ( ) ,  segmenter - > segmenter_granularity ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 18:51:06 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    segmenter - > set_segmenter ( move ( locale_segmenter ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 13. Return segmenter.
 
							 
						 
					
						
							
								
									
										
										
										
											2022-12-14 19:18:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return  segmenter ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-02-28 08:54:42 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// 19.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.segmenter.supportedlocalesof
  
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:53:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								JS_DEFINE_NATIVE_FUNCTION ( SegmenterConstructor : : supported_locales_of )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto  locales  =  vm . argument ( 0 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    auto  options  =  vm . argument ( 1 ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 1. Let availableLocales be %Segmenter%.[[AvailableLocales]].
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-20 08:25:24 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    auto  requested_locales  =  TRY ( canonicalize_locale_list ( vm ,  locales ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:53:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-06-18 10:13:30 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // 3. Return ? FilterLocales(availableLocales, requestedLocales, options).
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return  TRY ( filter_locales ( vm ,  requested_locales ,  options ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:53:24 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-29 23:47:29 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}