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

Fix bugs in RegExp and Lexer #1360

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/parser/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ Scanner::ScanIDResult Scanner::getIdentifier()
// Blackslash (U+005C) marks Unicode escape sequence.
this->index = start;
return this->getComplexIdentifier();
} else if (UNLIKELY(ch >= 0xD800 && ch < 0xDFFF)) {
} else if (UNLIKELY(ch >= 0xD800 && ch <= 0xDFFF)) {
// Need to handle surrogate pairs.
this->index = start;
return this->getComplexIdentifier();
Expand All @@ -887,7 +887,7 @@ Scanner::ScanIDResult Scanner::getIdentifier()

Scanner::ScanIDResult Scanner::getComplexIdentifier()
{
char16_t cp = this->codePointAt(this->index);
char32_t cp = this->codePointAt(this->index);
ParserCharPiece piece = ParserCharPiece(cp);
UTF16StringDataNonGCStd id(piece.data, piece.length);
this->index += id.length();
Expand All @@ -902,14 +902,17 @@ Scanner::ScanIDResult Scanner::getComplexIdentifier()
if (this->peekChar() == '{') {
++this->index;
ch = this->scanUnicodeCodePointEscape();
id.erase(id.length() - 1);
} else {
ch = this->scanHexEscape('u');
id.erase(id.length() - 1);
cp = ch;
if (ch == EMPTY_CODE_POINT || ch == '\\' || !isIdentifierStart(cp)) {
this->throwUnexpectedToken();
}
}
id = ch;
piece = ParserCharPiece(ch);
id += UTF16StringDataNonGCStd(piece.data, piece.length);
}

while (!this->eof()) {
Expand Down Expand Up @@ -2282,7 +2285,7 @@ static ALWAYS_INLINE KeywordKind getKeyword(const StringBufferAccessData& data)
return NotKeyword;
}

ALWAYS_INLINE void Scanner::scanIdentifier(Scanner::ScannerResult* token, char16_t ch0)
ALWAYS_INLINE void Scanner::scanIdentifier(Scanner::ScannerResult* token, char32_t ch0)
{
ASSERT(token != nullptr);
Token type = Token::IdentifierToken;
Expand Down Expand Up @@ -2341,13 +2344,15 @@ void Scanner::lex(Scanner::ScannerResult* token)
return;
}

char16_t cp = this->peekCharWithoutEOF();
char32_t cp = this->peekCharWithoutEOF();

if (UNLIKELY(cp >= 0xD800 && cp < 0xDFFF)) {
if (UNLIKELY(cp >= 0xD800 && cp <= 0xDFFF)) {
++this->index;
char32_t ch2 = this->peekChar();
if (U16_IS_TRAIL(ch2)) {
cp = U16_GET_SUPPLEMENTARY(cp, ch2);
cp = (cp - 0xd800) << 10;
cp += (ch2 - 0xdc00) + 0x10000UL;
this->index--;
} else {
this->throwUnexpectedToken();
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ class Scanner {
void scanStringLiteral(Scanner::ScannerResult* token);

// ECMA-262 11.6 Names and Keywords
ALWAYS_INLINE void scanIdentifier(Scanner::ScannerResult* token, char16_t ch0);
ALWAYS_INLINE void scanIdentifier(Scanner::ScannerResult* token, char32_t ch0);

String* scanRegExpBody();
String* scanRegExpFlags();
Expand Down
35 changes: 33 additions & 2 deletions src/runtime/RegExpObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,17 @@ ArrayObject* RegExpObject::createRegExpMatchedArray(ExecutionState& state, const
for (auto it = m_yarrPattern->m_captureGroupNames.begin(); it != m_yarrPattern->m_captureGroupNames.end(); ++it) {
auto foundMapElement = m_yarrPattern->m_namedGroupToParenIndices.find(*it);
if (foundMapElement != m_yarrPattern->m_namedGroupToParenIndices.end()) {
Value value;
for (size_t i = 0; i < foundMapElement->second.size(); i++) {
Value indexValue = indices->getOwnProperty(state,
ObjectPropertyName(state, foundMapElement->second[i]))
.value(state, indices);
if (!indexValue.isUndefinedOrNull()) {
value = indexValue;
}
}
groups->directDefineOwnProperty(state, ObjectPropertyName(state, it->impl()),
ObjectPropertyDescriptor(indices->getOwnProperty(state, ObjectPropertyName(state, foundMapElement->second[0])).value(state, this), ObjectPropertyDescriptor::AllPresent));
ObjectPropertyDescriptor(value, ObjectPropertyDescriptor::AllPresent));
}
}
indices->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().groups), ObjectPropertyDescriptor(Value(groups), ObjectPropertyDescriptor::AllPresent));
Expand All @@ -518,8 +527,30 @@ ArrayObject* RegExpObject::createRegExpMatchedArray(ExecutionState& state, const
for (auto it = m_yarrPattern->m_captureGroupNames.begin(); it != m_yarrPattern->m_captureGroupNames.end(); ++it) {
auto foundMapElement = m_yarrPattern->m_namedGroupToParenIndices.find(*it);
if (foundMapElement != m_yarrPattern->m_namedGroupToParenIndices.end()) {
Value value;
for (size_t i = 0; i < foundMapElement->second.size(); i++) {
Value indexValue;
size_t index = foundMapElement->second[i];
size_t indicesIndex = 0;
for (unsigned i = 0; i < result.m_matchResults.size(); i++) {
for (unsigned j = 0; j < result.m_matchResults[i].size(); j++) {
if (indicesIndex == index) {
if (result.m_matchResults[i][j].m_start != std::numeric_limits<unsigned>::max()) {
indexValue = new StringView(input, result.m_matchResults[i][j].m_start, result.m_matchResults[i][j].m_end);
}
break;
}
indicesIndex++;
}
}

if (!indexValue.isUndefinedOrNull()) {
value = indexValue;
}
}

groups->directDefineOwnProperty(state, ObjectPropertyName(state, it->impl()),
ObjectPropertyDescriptor(arr->getOwnProperty(state, ObjectPropertyName(state, foundMapElement->second[0])).value(state, this), ObjectPropertyDescriptor::AllPresent));
ObjectPropertyDescriptor(value, ObjectPropertyDescriptor::AllPresent));
}
}
arr->directDefineOwnProperty(state, ObjectPropertyName(state.context()->staticStrings().groups), ObjectPropertyDescriptor(Value(groups), ObjectPropertyDescriptor::AllPresent));
Expand Down
Loading
Loading