Jelajahi Sumber

LibJS: Convert the RegExpCreate AO to ThrowCompletionOr

Idan Horowitz 3 tahun lalu
induk
melakukan
844be7a0a5

+ 4 - 1
Userland/Libraries/LibJS/Bytecode/Op.cpp

@@ -184,7 +184,10 @@ void NewRegExp::execute_impl(Bytecode::Interpreter& interpreter) const
     auto source = interpreter.current_executable().get_string(m_source_index);
     auto source = interpreter.current_executable().get_string(m_source_index);
     auto flags = interpreter.current_executable().get_string(m_flags_index);
     auto flags = interpreter.current_executable().get_string(m_flags_index);
 
 
-    interpreter.accumulator() = regexp_create(interpreter.global_object(), js_string(interpreter.vm(), source), js_string(interpreter.vm(), flags));
+    auto regexp_or_error = regexp_create(interpreter.global_object(), js_string(interpreter.vm(), source), js_string(interpreter.vm(), flags));
+    if (regexp_or_error.is_error())
+        return;
+    interpreter.accumulator() = regexp_or_error.value();
 }
 }
 
 
 void CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const
 void CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpreter) const

+ 1 - 4
Userland/Libraries/LibJS/Runtime/RegExpConstructor.cpp

@@ -88,10 +88,7 @@ ThrowCompletionOr<Object*> RegExpConstructor::construct(FunctionObject&)
         flags_value = flags;
         flags_value = flags;
     }
     }
 
 
-    auto* regexp = regexp_create(global_object, pattern_value, flags_value);
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
-    return regexp;
+    return TRY(regexp_create(global_object, pattern_value, flags_value));
 }
 }
 
 
 // 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species
 // 22.2.4.2 get RegExp [ @@species ], https://tc39.es/ecma262/#sec-get-regexp-@@species

+ 2 - 2
Userland/Libraries/LibJS/Runtime/RegExpObject.cpp

@@ -190,10 +190,10 @@ String RegExpObject::escape_regexp_pattern() const
 }
 }
 
 
 // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate
 // 22.2.3.2.4 RegExpCreate ( P, F ), https://tc39.es/ecma262/#sec-regexpcreate
-RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags)
+ThrowCompletionOr<RegExpObject*> regexp_create(GlobalObject& global_object, Value pattern, Value flags)
 {
 {
     auto* regexp_object = RegExpObject::create(global_object);
     auto* regexp_object = RegExpObject::create(global_object);
-    return TRY_OR_DISCARD(regexp_object->regexp_initialize(global_object, pattern, flags));
+    return TRY(regexp_object->regexp_initialize(global_object, pattern, flags));
 }
 }
 
 
 }
 }

+ 1 - 1
Userland/Libraries/LibJS/Runtime/RegExpObject.h

@@ -14,7 +14,7 @@
 
 
 namespace JS {
 namespace JS {
 
 
-RegExpObject* regexp_create(GlobalObject&, Value pattern, Value flags);
+ThrowCompletionOr<RegExpObject*> regexp_create(GlobalObject&, Value pattern, Value flags);
 
 
 Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags);
 Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags);
 String parse_regex_pattern(StringView pattern, bool unicode);
 String parse_regex_pattern(StringView pattern, bool unicode);

+ 3 - 9
Userland/Libraries/LibJS/Runtime/StringPrototype.cpp

@@ -729,9 +729,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match)
 
 
     auto string = TRY(this_object.to_utf16_string(global_object));
     auto string = TRY(this_object.to_utf16_string(global_object));
 
 
-    auto* rx = regexp_create(global_object, regexp, js_undefined());
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
+    auto* rx = TRY(regexp_create(global_object, regexp, js_undefined()));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_match(), js_string(vm, move(string))));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_match(), js_string(vm, move(string))));
 }
 }
 
 
@@ -755,9 +753,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::match_all)
 
 
     auto string = TRY(this_object.to_utf16_string(global_object));
     auto string = TRY(this_object.to_utf16_string(global_object));
 
 
-    auto* rx = regexp_create(global_object, regexp, js_string(vm, "g"));
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
+    auto* rx = TRY(regexp_create(global_object, regexp, js_string(vm, "g")));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_match_all(), js_string(vm, move(string))));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_match_all(), js_string(vm, move(string))));
 }
 }
 
 
@@ -884,9 +880,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::search)
 
 
     auto string = TRY(this_object.to_utf16_string(global_object));
     auto string = TRY(this_object.to_utf16_string(global_object));
 
 
-    auto* rx = regexp_create(global_object, regexp, js_undefined());
-    if (auto* exception = vm.exception())
-        return throw_completion(exception->value());
+    auto* rx = TRY(regexp_create(global_object, regexp, js_undefined()));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_search(), js_string(vm, move(string))));
     return TRY(Value(rx).invoke(global_object, *vm.well_known_symbol_search(), js_string(vm, move(string))));
 }
 }