| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 16:53:07 -07:00
										 |  |  |  * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Error.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/RegExpConstructor.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/RegExpObject.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | RegExpConstructor::RegExpConstructor(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2021-06-28 03:45:49 +03:00
										 |  |  |     : NativeFunction(vm().names.RegExp.as_string(), *global_object.function_prototype()) | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  | void RegExpConstructor::initialize(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-13 23:49:19 +02:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  |     NativeFunction::initialize(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 22.2.4.1 RegExp.prototype, https://tc39.es/ecma262/#sec-regexp.prototype
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.prototype, global_object.regexp_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-25 18:37:14 +01:00
										 |  |  |     define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2021-07-08 02:49:53 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     define_direct_property(vm.names.length, Value(2), Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RegExpConstructor::~RegExpConstructor() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | // 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
 | 
					
						
							| 
									
										
										
										
											2020-09-27 17:24:14 +02:00
										 |  |  | Value RegExpConstructor::call() | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto pattern = vm.argument(0); | 
					
						
							|  |  |  |     auto flags = vm.argument(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 20:10:20 +03:00
										 |  |  |     bool pattern_is_regexp = TRY_OR_DISCARD(pattern.is_regexp(global_object)); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (pattern_is_regexp && flags.is_undefined()) { | 
					
						
							| 
									
										
										
										
											2021-10-02 23:52:27 +01:00
										 |  |  |         auto pattern_constructor = TRY_OR_DISCARD(pattern.as_object().get(vm.names.constructor)); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  |         if (same_value(this, pattern_constructor)) | 
					
						
							|  |  |  |             return pattern; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  |     return construct(*this); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | // 22.2.3.1 RegExp ( pattern, flags ), https://tc39.es/ecma262/#sec-regexp-pattern-flags
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | Value RegExpConstructor::construct(FunctionObject&) | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto pattern = vm.argument(0); | 
					
						
							|  |  |  |     auto flags = vm.argument(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-23 20:10:20 +03:00
										 |  |  |     bool pattern_is_regexp = TRY_OR_DISCARD(pattern.is_regexp(global_object)); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Value pattern_value; | 
					
						
							|  |  |  |     Value flags_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (pattern.is_object() && is<RegExpObject>(pattern.as_object())) { | 
					
						
							|  |  |  |         auto& regexp_pattern = static_cast<RegExpObject&>(pattern.as_object()); | 
					
						
							|  |  |  |         pattern_value = js_string(vm, regexp_pattern.pattern()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (flags.is_undefined()) | 
					
						
							|  |  |  |             flags_value = js_string(vm, regexp_pattern.flags()); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             flags_value = flags; | 
					
						
							|  |  |  |     } else if (pattern_is_regexp) { | 
					
						
							| 
									
										
										
										
											2021-10-02 23:52:27 +01:00
										 |  |  |         pattern_value = TRY_OR_DISCARD(pattern.as_object().get(vm.names.source)); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (flags.is_undefined()) { | 
					
						
							| 
									
										
										
										
											2021-10-02 23:52:27 +01:00
										 |  |  |             flags_value = TRY_OR_DISCARD(pattern.as_object().get(vm.names.flags)); | 
					
						
							| 
									
										
										
										
											2021-07-08 13:36:09 -04:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             flags_value = flags; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         pattern_value = pattern; | 
					
						
							|  |  |  |         flags_value = flags; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return regexp_create(global_object, pattern_value, flags_value); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | // 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
 | 
					
						
							| 
									
										
										
										
											2021-10-19 20:18:01 +03:00
										 |  |  | JS_DEFINE_OLD_NATIVE_FUNCTION(RegExpConstructor::symbol_species_getter) | 
					
						
							| 
									
										
										
										
											2021-06-07 19:31:32 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     return vm.this_value(global_object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } |