Skip to content

Commit a374eeb

Browse files
committed
[JSC] Location in JSToken is duplicate information
https://bugs.webkit.org/show_bug.cgi?id=297107 rdar://157820631 Reviewed by Yijia Huang. JSLocation stored in JSToken is completely duplicate information. This can be constructed by using m_startPosition and m_endPosition. Originally only JSLocation exists, but we added m_startPosition and m_endPosition later. As a result, we are storing duplicate information. This patch removes it. * Source/JavaScriptCore/inspector/agents/InspectorRuntimeAgent.cpp: (Inspector::InspectorRuntimeAgent::parse): * Source/JavaScriptCore/parser/Lexer.cpp: (JSC::Lexer<T>::fillTokenInfo): (JSC::Lexer<T>::lexWithoutClearingLineTerminator): (JSC::Lexer<T>::scanRegExp): (JSC::Lexer<T>::scanTemplateString): * Source/JavaScriptCore/parser/Lexer.h: (JSC::Lexer::getToken): (JSC::Lexer<T>::lexExpectIdentifier): * Source/JavaScriptCore/parser/Parser.cpp: (JSC::Parser<LexerType>::Parser): (JSC::Parser<LexerType>::parseGeneratorFunctionSourceElements): (JSC::Parser<LexerType>::parseAsyncFunctionSourceElements): (JSC::Parser<LexerType>::parseAsyncGeneratorFunctionSourceElements): (JSC::Parser<LexerType>::parseVariableDeclarationList): (JSC::Parser<LexerType>::createBindingPattern): (JSC::Parser<LexerType>::parseDestructuringPattern): (JSC::Parser<LexerType>::parseTryStatement): (JSC::Parser<LexerType>::parseStatement): (JSC::Parser<LexerType>::parseFunctionInfo): (JSC::Parser<LexerType>::parseProperty): (JSC::Parser<LexerType>::parseArrayLiteral): * Source/JavaScriptCore/parser/Parser.h: (JSC::Parser::tokenStart): (JSC::Parser::tokenLine): (JSC::Parser::tokenLineStart): (JSC::Parser::tokenLocation): (JSC::Parser::internalSaveLexerState): * Source/JavaScriptCore/parser/ParserTokens.h: (JSC::JSToken::location const): * Source/JavaScriptCore/parser/SourceProviderCacheItem.h: (JSC::SourceProviderCacheItem::endFunctionToken const): Canonical link: https://commits.webkit.org/298443@main
1 parent fae5b69 commit a374eeb

7 files changed

Lines changed: 82 additions & 96 deletions

File tree

‎Source/JavaScriptCore/inspector/agents/InspectorRuntimeAgent.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Protocol::ErrorStringOr<std::tuple<Protocol::Runtime::SyntaxErrorType, String /*
118118

119119
if (error.syntaxErrorType() != ParserError::SyntaxErrorNone) {
120120
message = error.message();
121-
range = buildErrorRangeObject(error.token().m_location);
121+
range = buildErrorRangeObject(error.token().location());
122122
}
123123

124124
return { { *result, message, WTFMove(range) } };

‎Source/JavaScriptCore/parser/Lexer.cpp‎

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,13 +1999,8 @@ bool Lexer<T>::nextTokenIsColon()
19991999
}
20002000

20012001
template <typename T>
2002-
void Lexer<T>::fillTokenInfo(JSToken* tokenRecord, JSTokenType token, int lineNumber, int endOffset, int lineStartOffset, JSTextPosition endPosition)
2002+
void Lexer<T>::fillTokenInfo(JSToken* tokenRecord, JSTokenType token, JSTextPosition endPosition)
20032003
{
2004-
JSTokenLocation* tokenLocation = &tokenRecord->m_location;
2005-
tokenLocation->line = lineNumber;
2006-
tokenLocation->endOffset = endOffset;
2007-
tokenLocation->lineStartOffset = lineStartOffset;
2008-
ASSERT(tokenLocation->endOffset >= tokenLocation->lineStartOffset);
20092004
tokenRecord->m_endPosition = endPosition;
20102005
m_lastToken = token;
20112006
}
@@ -2014,19 +2009,15 @@ template <typename T>
20142009
JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, OptionSet<LexerFlags> lexerFlags, bool strictMode)
20152010
{
20162011
JSTokenData* tokenData = &tokenRecord->m_data;
2017-
JSTokenLocation* tokenLocation = &tokenRecord->m_location;
2018-
m_lastTokenLocation = JSTokenLocation(tokenRecord->m_location);
2019-
20202012
ASSERT(!m_error);
2013+
m_lastTokenLocation = tokenRecord->location();
20212014
ASSERT(m_buffer8.isEmpty());
20222015
ASSERT(m_buffer16.isEmpty());
2023-
20242016
JSTokenType token = ERRORTOK;
20252017

20262018
start:
20272019
skipWhitespace();
20282020

2029-
tokenLocation->startOffset = currentOffset();
20302021
ASSERT(currentOffset() >= currentLineStartOffset());
20312022
tokenRecord->m_startPosition = currentPosition();
20322023

@@ -2221,14 +2212,12 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
22212212
}
22222213
if (m_current == '*') {
22232214
shift();
2224-
auto startLineNumber = m_lineNumber;
2225-
auto startLineStartOffset = currentLineStartOffset();
22262215
if (parseMultilineComment())
22272216
goto start;
22282217
m_lexErrorMessage = "Multiline comment was not closed properly"_s;
22292218
token = UNTERMINATED_MULTILINE_COMMENT_ERRORTOK;
22302219
m_error = true;
2231-
fillTokenInfo(tokenRecord, token, startLineNumber, currentOffset(), startLineStartOffset, currentPosition());
2220+
fillTokenInfo(tokenRecord, token, currentPosition());
22322221
return token;
22332222
}
22342223
if (m_current == '=') {
@@ -2647,8 +2636,6 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
26472636

26482637
case 34 /* 34 = " CharacterQuote */:
26492638
case 39 /* 39 = ' CharacterQuote */: {
2650-
auto startLineNumber = m_lineNumber;
2651-
auto startLineStartOffset = currentLineStartOffset();
26522639
StringParseResult result = StringCannotBeParsed;
26532640
if (lexerFlags.contains(LexerFlags::DontBuildStrings))
26542641
result = parseString<false>(tokenData, strictMode);
@@ -2658,13 +2645,13 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
26582645
if (result != StringParsedSuccessfully) [[unlikely]] {
26592646
token = result == StringUnterminated ? UNTERMINATED_STRING_LITERAL_ERRORTOK : INVALID_STRING_LITERAL_ERRORTOK;
26602647
m_error = true;
2661-
fillTokenInfo(tokenRecord, token, startLineNumber, currentOffset(), startLineStartOffset, currentPosition());
2648+
fillTokenInfo(tokenRecord, token, currentPosition());
26622649
return token;
26632650
}
26642651
shift();
26652652
token = STRING;
26662653
m_atLineStart = false;
2667-
fillTokenInfo(tokenRecord, token, startLineNumber, currentOffset(), startLineStartOffset, currentPosition());
2654+
fillTokenInfo(tokenRecord, token, currentPosition());
26682655
return token;
26692656
}
26702657

@@ -2980,9 +2967,6 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
29802967

29812968
inSingleLineComment:
29822969
{
2983-
auto lineNumber = m_lineNumber;
2984-
auto endOffset = currentOffset();
2985-
auto lineStartOffset = currentLineStartOffset();
29862970
auto endPosition = currentPosition();
29872971

29882972
using UnsignedType = std::make_unsigned_t<T>;
@@ -3012,7 +2996,7 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
30122996
if (m_code == m_codeEnd) {
30132997
m_current = 0;
30142998
token = EOFTOK;
3015-
fillTokenInfo(tokenRecord, token, lineNumber, endOffset, lineStartOffset, endPosition);
2999+
fillTokenInfo(tokenRecord, token, endPosition);
30163000
return token;
30173001
}
30183002

@@ -3024,12 +3008,12 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
30243008
goto start;
30253009

30263010
token = SEMICOLON;
3027-
fillTokenInfo(tokenRecord, token, lineNumber, endOffset, lineStartOffset, endPosition);
3011+
fillTokenInfo(tokenRecord, token, endPosition);
30283012
return token;
30293013
}
30303014

30313015
returnToken:
3032-
fillTokenInfo(tokenRecord, token, m_lineNumber, currentOffset(), currentLineStartOffset(), currentPosition());
3016+
fillTokenInfo(tokenRecord, token, currentPosition());
30333017
return token;
30343018

30353019
invalidCharacter:
@@ -3039,7 +3023,7 @@ JSTokenType Lexer<T>::lexWithoutClearingLineTerminator(JSToken* tokenRecord, Opt
30393023

30403024
returnError:
30413025
m_error = true;
3042-
fillTokenInfo(tokenRecord, token, m_lineNumber, currentOffset(), currentLineStartOffset(), currentPosition());
3026+
fillTokenInfo(tokenRecord, token, currentPosition());
30433027
RELEASE_ASSERT(token & CanBeErrorTokenFlag);
30443028
return token;
30453029
}
@@ -3077,7 +3061,7 @@ JSTokenType Lexer<T>::scanRegExp(JSToken* tokenRecord, char16_t patternPrefix)
30773061
if (isLineTerminator(m_current) || atEnd()) {
30783062
m_buffer16.shrink(0);
30793063
JSTokenType token = UNTERMINATED_REGEXP_LITERAL_ERRORTOK;
3080-
fillTokenInfo(tokenRecord, token, m_lineNumber, currentOffset(), currentLineStartOffset(), currentPosition());
3064+
fillTokenInfo(tokenRecord, token, currentPosition());
30813065
m_error = true;
30823066
m_lexErrorMessage = makeString("Unterminated regular expression literal '"_s, getToken(*tokenRecord), '\'');
30833067
return token;
@@ -3126,7 +3110,7 @@ JSTokenType Lexer<T>::scanRegExp(JSToken* tokenRecord, char16_t patternPrefix)
31263110
if (!isLatin1(m_current) && !isWhiteSpace(m_current) && !isLineTerminator(m_current)) [[unlikely]] {
31273111
m_buffer8.shrink(0);
31283112
JSTokenType token = INVALID_IDENTIFIER_UNICODE_ERRORTOK;
3129-
fillTokenInfo(tokenRecord, token, m_lineNumber, currentOffset(), currentLineStartOffset(), currentPosition());
3113+
fillTokenInfo(tokenRecord, token, currentPosition());
31303114
m_error = true;
31313115
String codePoint = String::fromCodePoint(currentCodePoint());
31323116
if (!codePoint)
@@ -3142,7 +3126,7 @@ JSTokenType Lexer<T>::scanRegExp(JSToken* tokenRecord, char16_t patternPrefix)
31423126
m_atLineStart = false;
31433127

31443128
JSTokenType token = REGEXP;
3145-
fillTokenInfo(tokenRecord, token, m_lineNumber, currentOffset(), currentLineStartOffset(), currentPosition());
3129+
fillTokenInfo(tokenRecord, token, currentPosition());
31463130
return token;
31473131
}
31483132

@@ -3153,9 +3137,6 @@ JSTokenType Lexer<T>::scanTemplateString(JSToken* tokenRecord, RawStringsBuildMo
31533137
ASSERT(!m_error);
31543138
ASSERT(m_buffer16.isEmpty());
31553139

3156-
int startingLineStartOffset = currentLineStartOffset();
3157-
int startingLineNumber = lineNumber();
3158-
31593140
// Leading backquote ` (for template head) or closing brace } (for template trailing) are already shifted in the previous token scan.
31603141
// So in this re-scan phase, shift() is not needed here.
31613142
StringParseResult result = parseTemplateLiteral(tokenData, rawStringsBuildMode);
@@ -3168,7 +3149,7 @@ JSTokenType Lexer<T>::scanTemplateString(JSToken* tokenRecord, RawStringsBuildMo
31683149

31693150
// Since TemplateString always ends with ` or }, m_atLineStart always becomes false.
31703151
m_atLineStart = false;
3171-
fillTokenInfo(tokenRecord, token, startingLineNumber, currentOffset(), startingLineStartOffset, currentPosition());
3152+
fillTokenInfo(tokenRecord, token, currentPosition());
31723153
return token;
31733154
}
31743155

‎Source/JavaScriptCore/parser/Lexer.h‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ class Lexer {
131131
ALWAYS_INLINE StringView getToken(const JSToken& token)
132132
{
133133
SourceProvider* sourceProvider = m_source->provider();
134-
ASSERT_WITH_MESSAGE(token.m_location.startOffset <= token.m_location.endOffset, "Calling this function with the baked token.");
135-
return sourceProvider->getRange(token.m_location.startOffset, token.m_location.endOffset);
134+
ASSERT_WITH_MESSAGE(token.m_startPosition.offset <= token.m_endPosition.offset, "Calling this function with the baked token.");
135+
return sourceProvider->getRange(token.m_startPosition.offset, token.m_endPosition.offset);
136136
}
137137

138138
size_t codeLength() { return m_codeEnd - m_codeStart; }
@@ -207,7 +207,7 @@ class Lexer {
207207
template <unsigned length>
208208
ALWAYS_INLINE bool consume(const char (&input)[length]);
209209

210-
void fillTokenInfo(JSToken*, JSTokenType, int lineNumber, int endOffset, int lineStartOffset, JSTextPosition endPosition);
210+
void fillTokenInfo(JSToken*, JSTokenType, JSTextPosition endPosition);
211211

212212
static constexpr size_t initialReadBufferCapacity = 32;
213213

@@ -356,7 +356,6 @@ template <typename T>
356356
ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, OptionSet<LexerFlags> lexerFlags, bool strictMode)
357357
{
358358
JSTokenData* tokenData = &tokenRecord->m_data;
359-
JSTokenLocation* tokenLocation = &tokenRecord->m_location;
360359
ASSERT(lexerFlags.contains(LexerFlags::IgnoreReservedWords));
361360
const T* start = m_code;
362361
const T* ptr = start;
@@ -396,11 +395,6 @@ ALWAYS_INLINE JSTokenType Lexer<T>::lexExpectIdentifier(JSToken* tokenRecord, Op
396395
else
397396
tokenData->ident = makeLCharIdentifier({ start, ptr });
398397

399-
tokenLocation->line = m_lineNumber;
400-
tokenLocation->lineStartOffset = currentLineStartOffset();
401-
tokenLocation->startOffset = offsetFromSourcePtr(start);
402-
tokenLocation->endOffset = currentOffset();
403-
ASSERT(tokenLocation->startOffset >= tokenLocation->lineStartOffset);
404398
tokenRecord->m_startPosition = startPosition;
405399
tokenRecord->m_endPosition = currentPosition();
406400
#if ASSERT_ENABLED

0 commit comments

Comments
 (0)