From 2686c5f5034a7a5eec9605aabf37833979e99dec Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 8 Jul 2021 10:52:50 -0400 Subject: [PATCH] LibJS: Do not use "this" object in RegExpExec As an abstraction, RegExpExec should not assume that the RegExp object being used is "this" object. Instead, it should only interact with the provided object. This prepares for some methods, such as @@split, which invoke RegExpExec with a secondary RegExp object. --- .../Libraries/LibJS/Runtime/RegExpPrototype.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp index dd9efb4b9a..46828d5aef 100644 --- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -165,16 +165,16 @@ static Value regexp_builtin_exec(GlobalObject& global_object, RegExpObject& rege } // 22.2.5.2.1 RegExpExec ( R, S ), https://tc39.es/ecma262/#sec-regexpexec -static Value regexp_exec(GlobalObject& global_object, Object& rx, String const& string) +static Value regexp_exec(GlobalObject& global_object, Object& regexp_object, String const& string) { auto& vm = global_object.vm(); - auto exec = rx.get(vm.names.exec); + auto exec = regexp_object.get(vm.names.exec); if (vm.exception()) return {}; if (exec.is_function()) { - auto result = vm.call(exec.as_function(), &rx, js_string(vm, string)); + auto result = vm.call(exec.as_function(), ®exp_object, js_string(vm, string)); if (vm.exception()) return {}; @@ -184,11 +184,12 @@ static Value regexp_exec(GlobalObject& global_object, Object& rx, String const& return result; } - auto regexp_object = regexp_object_from(vm, global_object); - if (!regexp_object) + if (!is(regexp_object)) { + vm.throw_exception(global_object, ErrorType::NotA, "RegExp"); return {}; + } - return regexp_builtin_exec(global_object, *regexp_object, string); + return regexp_builtin_exec(global_object, static_cast(regexp_object), string); } // 22.2.5.3 get RegExp.prototype.dotAll, https://tc39.es/ecma262/#sec-get-regexp.prototype.dotAll