summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-15 09:13:56 -0400
committerLinus Groh <mail@linusgroh.de>2021-07-16 13:53:11 +0100
commit5135f4000c3a090c612f54c8bd5f00b25dc218a2 (patch)
treeb38016b55fa97a6a4babae25c1ccf4030af30ab9 /Userland/Libraries/LibJS/Runtime
parentcfddcad7cfebcb61ff2b8dd52d94cd5950d5347a (diff)
downloadserenity-5135f4000c3a090c612f54c8bd5f00b25dc218a2.zip
LibJS: Implement RegExp.prototype [ @@matchAll ]
This also allows String.prototype.matchAll to work, as all calls to that method result in an invocation to @@matchAll.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp51
-rw-r--r--Userland/Libraries/LibJS/Runtime/RegExpPrototype.h1
2 files changed, 52 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
index d0427395cb..55563ffeca 100644
--- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
@@ -13,6 +13,7 @@
#include <LibJS/Runtime/RegExpConstructor.h>
#include <LibJS/Runtime/RegExpObject.h>
#include <LibJS/Runtime/RegExpPrototype.h>
+#include <LibJS/Runtime/RegExpStringIterator.h>
#include <LibJS/Token.h>
namespace JS {
@@ -32,6 +33,7 @@ void RegExpPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.exec, exec, 1, attr);
define_native_function(*vm.well_known_symbol_match(), symbol_match, 1, attr);
+ define_native_function(*vm.well_known_symbol_match_all(), symbol_match_all, 1, attr);
define_native_function(*vm.well_known_symbol_replace(), symbol_replace, 2, attr);
define_native_function(*vm.well_known_symbol_search(), symbol_search, 1, attr);
define_native_function(*vm.well_known_symbol_split(), symbol_split, 2, attr);
@@ -507,6 +509,55 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
}
}
+// 22.2.5.8 RegExp.prototype [ @@matchAll ] ( string ), https://tc39.es/ecma262/#sec-regexp-prototype-matchall
+JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
+{
+ auto* regexp_object = this_object_from(vm, global_object);
+ if (!regexp_object)
+ return {};
+
+ auto string = vm.argument(0).to_string(global_object);
+ if (vm.exception())
+ return {};
+
+ auto* constructor = species_constructor(global_object, *regexp_object, *global_object.regexp_constructor());
+ if (vm.exception())
+ return {};
+
+ auto flags_value = regexp_object->get(vm.names.flags);
+ if (vm.exception())
+ return {};
+ auto flags = flags_value.to_string(global_object);
+ if (vm.exception())
+ return {};
+
+ bool global = flags.find('g').has_value();
+ bool unicode = flags.find('u').has_value();
+
+ MarkedValueList arguments(vm.heap());
+ arguments.append(regexp_object);
+ arguments.append(js_string(vm, move(flags)));
+ auto matcher_value = vm.construct(*constructor, *constructor, move(arguments));
+ if (vm.exception())
+ return {};
+ auto* matcher = matcher_value.to_object(global_object);
+ if (!matcher)
+ return {};
+
+ auto last_index_value = regexp_object->get(vm.names.lastIndex);
+ if (vm.exception())
+ return {};
+ auto last_index = last_index_value.to_length(global_object);
+ if (vm.exception())
+ return {};
+
+ matcher->set(vm.names.lastIndex, Value(last_index), true);
+ if (vm.exception())
+ return {};
+
+ return RegExpStringIterator::create(global_object, *matcher, move(string), global, unicode);
+}
+
// 22.2.5.10 RegExp.prototype [ @@replace ] ( string, replaceValue ), https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
{
diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.h b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.h
index abed0b05ce..fce9688abb 100644
--- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.h
@@ -28,6 +28,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(test);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(symbol_match);
+ JS_DECLARE_NATIVE_FUNCTION(symbol_match_all);
JS_DECLARE_NATIVE_FUNCTION(symbol_replace);
JS_DECLARE_NATIVE_FUNCTION(symbol_search);
JS_DECLARE_NATIVE_FUNCTION(symbol_split);