mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:58:11 +00:00
LibJS: Hook up Regex<ECMA262> to RegExpObject and implement `test()'
This makes RegExpObject compile and store a Regex<ECMA262>, adds all flag-related properties, and implements `RegExpPrototype.test()` (complete with 'lastIndex' support) :^) It should be noted that this only implements `test()' using the builtin `exec()'.
This commit is contained in:
parent
75081b2bdd
commit
8ba273a2f3
13 changed files with 396 additions and 12 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
|
@ -33,6 +34,73 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
static Flags options_from(const String& flags, VM& vm, GlobalObject& global_object)
|
||||
{
|
||||
bool g = false, i = false, m = false, s = false, u = false, y = false;
|
||||
Flags options {
|
||||
{ (regex::ECMAScriptFlags)regex::AllFlags::Global }, // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
|
||||
{},
|
||||
};
|
||||
|
||||
for (auto ch : flags) {
|
||||
switch (ch) {
|
||||
case 'g':
|
||||
if (g)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
g = true;
|
||||
options.effective_flags |= regex::ECMAScriptFlags::Global;
|
||||
options.declared_flags |= regex::ECMAScriptFlags::Global;
|
||||
break;
|
||||
case 'i':
|
||||
if (i)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
i = true;
|
||||
options.effective_flags |= regex::ECMAScriptFlags::Insensitive;
|
||||
options.declared_flags |= regex::ECMAScriptFlags::Insensitive;
|
||||
break;
|
||||
case 'm':
|
||||
if (m)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
m = true;
|
||||
options.effective_flags |= regex::ECMAScriptFlags::Multiline;
|
||||
options.declared_flags |= regex::ECMAScriptFlags::Multiline;
|
||||
break;
|
||||
case 's':
|
||||
if (s)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
s = true;
|
||||
options.effective_flags |= regex::ECMAScriptFlags::SingleLine;
|
||||
options.declared_flags |= regex::ECMAScriptFlags::SingleLine;
|
||||
break;
|
||||
case 'u':
|
||||
if (u)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
u = true;
|
||||
options.effective_flags |= regex::ECMAScriptFlags::Unicode;
|
||||
options.declared_flags |= regex::ECMAScriptFlags::Unicode;
|
||||
break;
|
||||
case 'y':
|
||||
if (y)
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectRepeatedFlag, ch);
|
||||
y = true;
|
||||
// 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:
|
||||
vm.throw_exception<SyntaxError>(global_object, ErrorType::RegExpObjectBadFlag, ch);
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags)
|
||||
{
|
||||
return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype());
|
||||
|
@ -42,11 +110,61 @@ RegExpObject::RegExpObject(String pattern, String flags, Object& prototype)
|
|||
: Object(prototype)
|
||||
, m_pattern(pattern)
|
||||
, m_flags(flags)
|
||||
, m_active_flags(options_from(m_flags, this->vm(), this->global_object()))
|
||||
, m_regex(pattern, m_active_flags.effective_flags)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
RegExpObject::~RegExpObject()
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
if (!this_object->is_regexp_object()) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue