| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/PrimitiveString.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/RegExpObject.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  | static Flags options_from(GlobalObject& global_object, const String& flags) | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |     auto& vm = global_object.vm(); | 
					
						
							|  |  |  |     bool g = false, i = false, m = false, s = false, u = false, y = false; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |     Flags options { | 
					
						
							| 
									
										
										
										
											2021-02-27 00:26:45 +03:30
										 |  |  |         // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
 | 
					
						
							|  |  |  |         // FIXME: Enable 'BrowserExtended' only if in a browser context.
 | 
					
						
							| 
									
										
										
										
											2021-04-03 14:54:04 +02:00
										 |  |  |         .effective_flags = { (regex::ECMAScriptFlags)regex::AllFlags::Global | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches | regex::ECMAScriptFlags::BrowserExtended }, | 
					
						
							|  |  |  |         .declared_flags = {}, | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto ch : flags) { | 
					
						
							|  |  |  |         switch (ch) { | 
					
						
							|  |  |  |         case 'g': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (g) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             g = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             options.effective_flags |= regex::ECMAScriptFlags::Global; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::Global; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 'i': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (i) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             i = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             options.effective_flags |= regex::ECMAScriptFlags::Insensitive; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::Insensitive; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 'm': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (m) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             m = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             options.effective_flags |= regex::ECMAScriptFlags::Multiline; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::Multiline; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 's': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (s) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             s = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             options.effective_flags |= regex::ECMAScriptFlags::SingleLine; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::SingleLine; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 'u': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (u) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             u = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             options.effective_flags |= regex::ECMAScriptFlags::Unicode; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::Unicode; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case 'y': | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             if (y) | 
					
						
							|  |  |  |                 vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch); | 
					
						
							|  |  |  |             y = true; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |             // Now for the more interesting flag, 'sticky' actually unsets 'global', part of which is the default.
 | 
					
						
							|  |  |  |             options.effective_flags.reset_flag(regex::ECMAScriptFlags::Global); | 
					
						
							|  |  |  |             // "What's the difference between sticky and global, then", that's simple.
 | 
					
						
							|  |  |  |             // all the other flags imply 'global', and the "global" flag implies 'stateful';
 | 
					
						
							|  |  |  |             // however, the "sticky" flag does *not* imply 'global', only 'stateful'.
 | 
					
						
							|  |  |  |             options.effective_flags |= (regex::ECMAScriptFlags)regex::AllFlags::Internal_Stateful; | 
					
						
							|  |  |  |             options.effective_flags |= regex::ECMAScriptFlags::Sticky; | 
					
						
							|  |  |  |             options.declared_flags |= regex::ECMAScriptFlags::Sticky; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |             vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectBadFlag, ch); | 
					
						
							|  |  |  |             return options; | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return options; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 17:14:50 +03:30
										 |  |  | RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags) | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-27 17:14:50 +03:30
										 |  |  |     return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype()); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 17:14:50 +03:30
										 |  |  | RegExpObject::RegExpObject(String pattern, String flags, Object& prototype) | 
					
						
							| 
									
										
										
										
											2020-06-23 17:21:53 +02:00
										 |  |  |     : Object(prototype) | 
					
						
							| 
									
										
										
										
											2020-11-27 17:14:50 +03:30
										 |  |  |     , m_pattern(pattern) | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  |     , m_flags(flags) | 
					
						
							| 
									
										
										
										
											2021-05-11 22:47:14 +01:00
										 |  |  |     , m_active_flags(options_from(global_object(), m_flags)) | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |     , m_regex(pattern, m_active_flags.effective_flags) | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |     if (m_regex.parser_result.error != regex::Error::NoError) { | 
					
						
							|  |  |  |         vm().throw_exception<SyntaxError>(global_object(), ErrorType::RegExpCompileError, m_regex.error_string()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RegExpObject::initialize(GlobalObject& global_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     Object::initialize(global_object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     define_native_property(vm.names.lastIndex, last_index, set_last_index, Attribute::Writable); | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RegExpObject::~RegExpObject() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  | static RegExpObject* regexp_object_from(VM& vm, GlobalObject& global_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* this_object = vm.this_value(global_object).to_object(global_object); | 
					
						
							|  |  |  |     if (!this_object) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2021-01-01 17:46:39 +01:00
										 |  |  |     if (!is<RegExpObject>(this_object)) { | 
					
						
							| 
									
										
										
										
											2020-11-19 01:50:00 +03:30
										 |  |  |         vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "RegExp"); | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return static_cast<RegExpObject*>(this_object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS_DEFINE_NATIVE_GETTER(RegExpObject::last_index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto regexp_object = regexp_object_from(vm, global_object); | 
					
						
							|  |  |  |     if (!regexp_object) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Value((unsigned)regexp_object->regex().start_offset); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS_DEFINE_NATIVE_SETTER(RegExpObject::set_last_index) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto regexp_object = regexp_object_from(vm, global_object); | 
					
						
							|  |  |  |     if (!regexp_object) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto index = value.to_i32(global_object); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (index < 0) | 
					
						
							|  |  |  |         index = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     regexp_object->regex().start_offset = index; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 11:03:11 +01:00
										 |  |  | RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // https://tc39.es/ecma262/#sec-regexpcreate
 | 
					
						
							|  |  |  |     String p; | 
					
						
							|  |  |  |     if (pattern.is_undefined()) { | 
					
						
							|  |  |  |         p = String::empty(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         p = pattern.to_string(global_object); | 
					
						
							|  |  |  |         if (p.is_null()) | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     String f; | 
					
						
							| 
									
										
										
										
											2021-03-14 12:02:53 +01:00
										 |  |  |     if (flags.is_undefined()) { | 
					
						
							| 
									
										
										
										
											2021-03-14 11:03:11 +01:00
										 |  |  |         f = String::empty(); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         f = flags.to_string(global_object); | 
					
						
							|  |  |  |         if (f.is_null()) | 
					
						
							|  |  |  |             return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-10 12:01:38 +01:00
										 |  |  |     return RegExpObject::create(global_object, move(p), move(f)); | 
					
						
							| 
									
										
										
										
											2021-03-14 11:03:11 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-03 16:05:49 -07:00
										 |  |  | } |