|
@@ -9,6 +9,7 @@
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
#include <LibJS/Runtime/RegExpObject.h>
|
|
|
+#include <LibJS/Runtime/StringPrototype.h>
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
namespace JS {
|
|
@@ -88,17 +89,18 @@ static Flags options_from(GlobalObject& global_object, const String& flags)
|
|
|
return options;
|
|
|
}
|
|
|
|
|
|
-RegExpObject* RegExpObject::create(GlobalObject& global_object, String pattern, String flags)
|
|
|
+RegExpObject* RegExpObject::create(GlobalObject& global_object, String original_pattern, String parsed_pattern, String flags)
|
|
|
{
|
|
|
- return global_object.heap().allocate<RegExpObject>(global_object, pattern, flags, *global_object.regexp_prototype());
|
|
|
+ return global_object.heap().allocate<RegExpObject>(global_object, move(original_pattern), move(parsed_pattern), move(flags), *global_object.regexp_prototype());
|
|
|
}
|
|
|
|
|
|
-RegExpObject::RegExpObject(String pattern, String flags, Object& prototype)
|
|
|
+RegExpObject::RegExpObject(String original_pattern, String parsed_pattern, String flags, Object& prototype)
|
|
|
: Object(prototype)
|
|
|
- , m_pattern(pattern)
|
|
|
- , m_flags(flags)
|
|
|
+ , m_original_pattern(move(original_pattern))
|
|
|
+ , m_parsed_pattern(move(parsed_pattern))
|
|
|
+ , m_flags(move(flags))
|
|
|
, m_active_flags(options_from(global_object(), m_flags))
|
|
|
- , m_regex(pattern, m_active_flags.effective_flags)
|
|
|
+ , m_regex(m_parsed_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());
|
|
@@ -120,14 +122,7 @@ void RegExpObject::initialize(GlobalObject& global_object)
|
|
|
RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value flags)
|
|
|
{
|
|
|
auto& vm = global_object.vm();
|
|
|
- String p;
|
|
|
- if (pattern.is_undefined()) {
|
|
|
- p = String::empty();
|
|
|
- } else {
|
|
|
- p = pattern.to_string(global_object);
|
|
|
- if (vm.exception())
|
|
|
- return {};
|
|
|
- }
|
|
|
+
|
|
|
String f;
|
|
|
if (flags.is_undefined()) {
|
|
|
f = String::empty();
|
|
@@ -136,7 +131,46 @@ RegExpObject* regexp_create(GlobalObject& global_object, Value pattern, Value fl
|
|
|
if (vm.exception())
|
|
|
return {};
|
|
|
}
|
|
|
- auto* object = RegExpObject::create(global_object, move(p), move(f));
|
|
|
+
|
|
|
+ String original_pattern;
|
|
|
+ String parsed_pattern;
|
|
|
+
|
|
|
+ if (pattern.is_undefined()) {
|
|
|
+ original_pattern = String::empty();
|
|
|
+ parsed_pattern = String::empty();
|
|
|
+ } else {
|
|
|
+ auto utf16_pattern = pattern.to_utf16_string(global_object);
|
|
|
+ if (vm.exception())
|
|
|
+ return {};
|
|
|
+
|
|
|
+ Utf16View utf16_pattern_view { utf16_pattern };
|
|
|
+ bool unicode = f.find('u').has_value();
|
|
|
+ StringBuilder builder;
|
|
|
+
|
|
|
+ // If the Unicode flag is set, append each code point to the pattern. Otherwise, append each
|
|
|
+ // code unit. But unlike the spec, multi-byte code units must be escaped for LibRegex to parse.
|
|
|
+ for (size_t i = 0; i < utf16_pattern_view.length_in_code_units();) {
|
|
|
+ if (unicode) {
|
|
|
+ auto code_point = code_point_at(utf16_pattern_view, i);
|
|
|
+ builder.append_code_point(code_point.code_point);
|
|
|
+ i += code_point.code_unit_count;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ u16 code_unit = utf16_pattern_view.code_unit_at(i);
|
|
|
+ ++i;
|
|
|
+
|
|
|
+ if (code_unit > 0x7f)
|
|
|
+ builder.appendff("\\u{:04x}", code_unit);
|
|
|
+ else
|
|
|
+ builder.append_code_point(code_unit);
|
|
|
+ }
|
|
|
+
|
|
|
+ original_pattern = utf16_pattern_view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes);
|
|
|
+ parsed_pattern = builder.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ auto* object = RegExpObject::create(global_object, move(original_pattern), move(parsed_pattern), move(f));
|
|
|
object->set(vm.names.lastIndex, Value(0), Object::ShouldThrowExceptions::Yes);
|
|
|
if (vm.exception())
|
|
|
return {};
|