LibJS: Make Token::m_message a StringView

This is only ever a string literal, so there's no need to keep creating
the same strings at runtime.
This commit is contained in:
Andreas Kling 2024-03-23 11:38:58 +01:00
commit 3851d3add0
Notes: sideshowbarker 2024-07-17 18:23:22 +09:00
2 changed files with 9 additions and 9 deletions

View file

@ -567,7 +567,7 @@ Token Lexer::next()
// This is being used to communicate info about invalid tokens to the parser, which then // This is being used to communicate info about invalid tokens to the parser, which then
// can turn that into more specific error messages - instead of us having to make up a // can turn that into more specific error messages - instead of us having to make up a
// bunch of Invalid* tokens (bad numeric literals, unterminated comments etc.) // bunch of Invalid* tokens (bad numeric literals, unterminated comments etc.)
ByteString token_message; StringView token_message;
Optional<DeprecatedFlyString> identifier; Optional<DeprecatedFlyString> identifier;
size_t identifier_length = 0; size_t identifier_length = 0;
@ -643,7 +643,7 @@ Token Lexer::next()
m_parsed_identifiers->identifiers.set(*identifier); m_parsed_identifiers->identifiers.set(*identifier);
} else { } else {
token_type = TokenType::Invalid; token_type = TokenType::Invalid;
token_message = "Start of private name '#' but not followed by valid identifier"; token_message = "Start of private name '#' but not followed by valid identifier"sv;
} }
} else if (auto code_point = is_identifier_start(identifier_length); code_point.has_value()) { } else if (auto code_point = is_identifier_start(identifier_length); code_point.has_value()) {
bool has_escaped_character = false; bool has_escaped_character = false;
@ -734,7 +734,7 @@ Token Lexer::next()
} }
if (is_invalid_numeric_literal) { if (is_invalid_numeric_literal) {
token_type = TokenType::Invalid; token_type = TokenType::Invalid;
token_message = "Invalid numeric literal"; token_message = "Invalid numeric literal"sv;
} }
} else if (m_current_char == '"' || m_current_char == '\'') { } else if (m_current_char == '"' || m_current_char == '\'') {
char stop_char = m_current_char; char stop_char = m_current_char;
@ -761,7 +761,7 @@ Token Lexer::next()
} else if (m_eof) { } else if (m_eof) {
if (unterminated_comment) { if (unterminated_comment) {
token_type = TokenType::Invalid; token_type = TokenType::Invalid;
token_message = "Unterminated multi-line comment"; token_message = "Unterminated multi-line comment"sv;
} else { } else {
token_type = TokenType::Eof; token_type = TokenType::Eof;
} }
@ -844,7 +844,7 @@ Token Lexer::next()
} else { } else {
m_current_token = Token( m_current_token = Token(
token_type, token_type,
String::from_byte_string(token_message).release_value_but_fixme_should_propagate_errors(), token_message,
m_source.substring_view(trivia_start - 1, value_start - trivia_start), m_source.substring_view(trivia_start - 1, value_start - trivia_start),
m_source.substring_view(value_start - 1, m_position - value_start), m_source.substring_view(value_start - 1, m_position - value_start),
m_filename, m_filename,

View file

@ -181,9 +181,9 @@ class Token {
public: public:
Token() = default; Token() = default;
Token(TokenType type, String message, StringView trivia, StringView value, StringView filename, size_t line_number, size_t line_column, size_t offset) Token(TokenType type, StringView message, StringView trivia, StringView value, StringView filename, size_t line_number, size_t line_column, size_t offset)
: m_type(type) : m_type(type)
, m_message(move(message)) , m_message(message)
, m_trivia(trivia) , m_trivia(trivia)
, m_original_value(value) , m_original_value(value)
, m_value(value) , m_value(value)
@ -200,7 +200,7 @@ public:
char const* name() const; char const* name() const;
static char const* name(TokenType); static char const* name(TokenType);
String const& message() const { return m_message; } StringView message() const { return m_message; }
StringView trivia() const { return m_trivia; } StringView trivia() const { return m_trivia; }
StringView original_value() const { return m_original_value; } StringView original_value() const { return m_original_value; }
StringView value() const StringView value() const
@ -246,7 +246,7 @@ public:
private: private:
TokenType m_type { TokenType::Invalid }; TokenType m_type { TokenType::Invalid };
String m_message; StringView m_message;
StringView m_trivia; StringView m_trivia;
StringView m_original_value; StringView m_original_value;
Variant<Empty, StringView, DeprecatedFlyString> m_value {}; Variant<Empty, StringView, DeprecatedFlyString> m_value {};