1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:37:45 +00:00

LibJS: Parse RegExp literals at AST creation time, not execution time

The spec requires that invalid RegExp literals must cause a Syntax Error
before the JavaScript is executed. See:
https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors

This is explicitly tested in the RegExp/property-escapes test262 tests.
For example, see unsupported-property-Line_Break.js:

    $DONOTEVALUATE();
    /\p{Line_Break}/u;

That RegExp literal is invalid because Line_Break is not a supported
Unicode property. $DONOTEVALUATE() just throws an exception when it is
executed. The test expects that this file will fail to be parsed.

Note that RegExp patterns can still be parsed at execution time by way
of "new RegExp(...)".
This commit is contained in:
Timothy Flynn 2021-07-29 10:34:37 -04:00 committed by Linus Groh
parent 1400e3cf58
commit f1dd770a8a
6 changed files with 124 additions and 94 deletions

View file

@ -6,40 +6,40 @@
#pragma once
#include <AK/Result.h>
#include <LibJS/AST.h>
#include <LibJS/Runtime/Object.h>
#include <LibRegex/Regex.h>
struct Flags {
regex::RegexOptions<ECMAScriptFlags> effective_flags;
regex::RegexOptions<ECMAScriptFlags> declared_flags;
};
namespace JS {
RegExpObject* regexp_create(GlobalObject&, Value pattern, Value flags);
Result<regex::RegexOptions<ECMAScriptFlags>, String> regex_flags_from_string(StringView flags);
String parse_regex_pattern(StringView pattern, bool unicode);
class RegExpObject : public Object {
JS_OBJECT(RegExpObject, Object);
public:
static RegExpObject* create(GlobalObject&, String original_pattern, String parsed_pattern, String flags);
// 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.
static constexpr regex::RegexOptions<ECMAScriptFlags> default_flags { (regex::ECMAScriptFlags)regex::AllFlags::Global | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches | regex::ECMAScriptFlags::BrowserExtended };
RegExpObject(String original_pattern, String parsed_pattern, String flags, Object& prototype);
static RegExpObject* create(GlobalObject&, Regex<ECMA262> regex, String pattern, String flags);
RegExpObject(Regex<ECMA262> regex, String pattern, String flags, Object& prototype);
virtual void initialize(GlobalObject&) override;
virtual ~RegExpObject() override;
const String& pattern() const { return m_original_pattern; }
const String& pattern() const { return m_pattern; }
const String& flags() const { return m_flags; }
const regex::RegexOptions<ECMAScriptFlags>& declared_options() { return m_active_flags.declared_flags; }
const Regex<ECMA262>& regex() { return m_regex; }
const Regex<ECMA262>& regex() const { return m_regex; }
private:
String m_original_pattern;
String m_parsed_pattern;
String m_pattern;
String m_flags;
Flags m_active_flags;
Regex<ECMA262> m_regex;
};