Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix [RegExp|String].prototype methods and Regex matcher #6615

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
33 changes: 27 additions & 6 deletions lib/Runtime/Library/RegexHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2396,18 +2396,39 @@ namespace Js
{
if (matchStr->GetLength() == 0)
{
CharCount lastIndex = JavascriptRegExp::GetLastIndexProperty(instance, scriptContext);

int64 lastIndex = JavascriptConversion::ToLength(
JavascriptOperators::GetProperty(instance, PropertyIds::lastIndex, scriptContext),
scriptContext);
lastIndex = AdvanceStringIndex(input, lastIndex, unicode);
JavascriptRegExp::SetLastIndexProperty(instance, lastIndex, scriptContext);
JavascriptRegExp::SetLastIndexProperty(
instance,
JavascriptNumber::ToVar(lastIndex, scriptContext),
scriptContext);
}
}

CharCount RegexHelper::AdvanceStringIndex(JavascriptString* string, CharCount index, bool isUnicode)
int64_t RegexHelper::AdvanceStringIndex(JavascriptString* string, int64_t index, bool isUnicode)
{
// TODO: Change the increment to 2 depending on the "unicode" flag and
// the code point at "index". The increment is currently constant at 1
// in order to be compatible with the rest of the RegExp code.
if (string->GetLength() > (0 > index || (uint64_t)index + (uint64_t)1 > UINT32_MAX ? UINT32_MAX : (uint32_t)(index + 1)) &&
NumberUtilities::IsSurrogateLowerPart(string->GetString()[index]) &&
NumberUtilities::IsSurrogateUpperPart(string->GetString()[index + 1]))
{
return index + 2;
}

return index + 1;
}

CharCount RegexHelper::AdvanceStringIndex(JavascriptString* string, CharCount index, bool isUnicode)
{
if (string->GetLength() > index + 1 &&
NumberUtilities::IsSurrogateLowerPart(string->GetString()[index]) &&
NumberUtilities::IsSurrogateUpperPart(string->GetString()[index + 1]))
{
return JavascriptRegExp::AddIndex(index, 2);
}

return JavascriptRegExp::AddIndex(index, 1);
MadProbe marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions lib/Runtime/Library/RegexHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ namespace Js
static RecyclableObject* ExecResultToRecyclableObject(Var result);
static JavascriptString* GetMatchStrFromResult(RecyclableObject* result, ScriptContext* scriptContext);
static void AdvanceLastIndex(RecyclableObject* instance, JavascriptString* input, JavascriptString* matchStr, bool unicode, ScriptContext* scriptContext);
static int64_t AdvanceStringIndex(JavascriptString* string, int64_t index, bool isUnicode);
static charcount_t AdvanceStringIndex(JavascriptString* string, charcount_t index, bool isUnicode);
};
}