Commit ab3d4cf7 authored by keuchel@chromium.org's avatar keuchel@chromium.org

Proper handling of future reserved words in strict and normal mode.

BUG=86442
TEST=mjsunit/keywords-and-reserved_words.js

Review URL: http://codereview.chromium.org/7207007

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8421 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 628a2e2a
...@@ -148,6 +148,7 @@ function FormatMessage(message) { ...@@ -148,6 +148,7 @@ function FormatMessage(message) {
unexpected_token_number: ["Unexpected number"], unexpected_token_number: ["Unexpected number"],
unexpected_token_string: ["Unexpected string"], unexpected_token_string: ["Unexpected string"],
unexpected_token_identifier: ["Unexpected identifier"], unexpected_token_identifier: ["Unexpected identifier"],
unexpected_reserved: ["Unexpected reserved word"],
unexpected_strict_reserved: ["Unexpected strict mode reserved word"], unexpected_strict_reserved: ["Unexpected strict mode reserved word"],
unexpected_eos: ["Unexpected end of input"], unexpected_eos: ["Unexpected end of input"],
malformed_regexp: ["Invalid regular expression: /", "%0", "/: ", "%1"], malformed_regexp: ["Invalid regular expression: /", "%0", "/: ", "%1"],
......
...@@ -1445,10 +1445,11 @@ Statement* Parser::ParseFunctionDeclaration(bool* ok) { ...@@ -1445,10 +1445,11 @@ Statement* Parser::ParseFunctionDeclaration(bool* ok) {
// 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
Expect(Token::FUNCTION, CHECK_OK); Expect(Token::FUNCTION, CHECK_OK);
int function_token_position = scanner().location().beg_pos; int function_token_position = scanner().location().beg_pos;
bool is_reserved = false; bool is_strict_reserved = false;
Handle<String> name = ParseIdentifierOrReservedWord(&is_reserved, CHECK_OK); Handle<String> name = ParseIdentifierOrStrictReservedWord(
&is_strict_reserved, CHECK_OK);
FunctionLiteral* fun = ParseFunctionLiteral(name, FunctionLiteral* fun = ParseFunctionLiteral(name,
is_reserved, is_strict_reserved,
function_token_position, function_token_position,
DECLARATION, DECLARATION,
CHECK_OK); CHECK_OK);
...@@ -2774,11 +2775,12 @@ Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack, ...@@ -2774,11 +2775,12 @@ Expression* Parser::ParseMemberWithNewPrefixesExpression(PositionStack* stack,
Expect(Token::FUNCTION, CHECK_OK); Expect(Token::FUNCTION, CHECK_OK);
int function_token_position = scanner().location().beg_pos; int function_token_position = scanner().location().beg_pos;
Handle<String> name; Handle<String> name;
bool is_reserved_name = false; bool is_strict_reserved_name = false;
if (peek_any_identifier()) { if (peek_any_identifier()) {
name = ParseIdentifierOrReservedWord(&is_reserved_name, CHECK_OK); name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
CHECK_OK);
} }
result = ParseFunctionLiteral(name, is_reserved_name, result = ParseFunctionLiteral(name, is_strict_reserved_name,
function_token_position, NESTED, CHECK_OK); function_token_position, NESTED, CHECK_OK);
} else { } else {
result = ParsePrimaryExpression(CHECK_OK); result = ParsePrimaryExpression(CHECK_OK);
...@@ -2857,6 +2859,9 @@ void Parser::ReportUnexpectedToken(Token::Value token) { ...@@ -2857,6 +2859,9 @@ void Parser::ReportUnexpectedToken(Token::Value token) {
return ReportMessage("unexpected_token_identifier", return ReportMessage("unexpected_token_identifier",
Vector<const char*>::empty()); Vector<const char*>::empty());
case Token::FUTURE_RESERVED_WORD: case Token::FUTURE_RESERVED_WORD:
return ReportMessage("unexpected_reserved",
Vector<const char*>::empty());
case Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessage(top_scope_->is_strict_mode() ? return ReportMessage(top_scope_->is_strict_mode() ?
"unexpected_strict_reserved" : "unexpected_strict_reserved" :
"unexpected_token_identifier", "unexpected_token_identifier",
...@@ -2917,7 +2922,7 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) { ...@@ -2917,7 +2922,7 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) {
break; break;
case Token::IDENTIFIER: case Token::IDENTIFIER:
case Token::FUTURE_RESERVED_WORD: { case Token::FUTURE_STRICT_RESERVED_WORD: {
Handle<String> name = ParseIdentifier(CHECK_OK); Handle<String> name = ParseIdentifier(CHECK_OK);
if (fni_ != NULL) fni_->PushVariableName(name); if (fni_ != NULL) fni_->PushVariableName(name);
result = top_scope_->NewUnresolved(name, result = top_scope_->NewUnresolved(name,
...@@ -3328,6 +3333,7 @@ ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter, ...@@ -3328,6 +3333,7 @@ ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter,
bool is_keyword = Token::IsKeyword(next); bool is_keyword = Token::IsKeyword(next);
if (next == Token::IDENTIFIER || next == Token::NUMBER || if (next == Token::IDENTIFIER || next == Token::NUMBER ||
next == Token::FUTURE_RESERVED_WORD || next == Token::FUTURE_RESERVED_WORD ||
next == Token::FUTURE_STRICT_RESERVED_WORD ||
next == Token::STRING || is_keyword) { next == Token::STRING || is_keyword) {
Handle<String> name; Handle<String> name;
if (is_keyword) { if (is_keyword) {
...@@ -3382,11 +3388,12 @@ Expression* Parser::ParseObjectLiteral(bool* ok) { ...@@ -3382,11 +3388,12 @@ Expression* Parser::ParseObjectLiteral(bool* ok) {
switch (next) { switch (next) {
case Token::FUTURE_RESERVED_WORD: case Token::FUTURE_RESERVED_WORD:
case Token::FUTURE_STRICT_RESERVED_WORD:
case Token::IDENTIFIER: { case Token::IDENTIFIER: {
bool is_getter = false; bool is_getter = false;
bool is_setter = false; bool is_setter = false;
Handle<String> id = Handle<String> id =
ParseIdentifierOrGetOrSet(&is_getter, &is_setter, CHECK_OK); ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
if (fni_ != NULL) fni_->PushLiteralName(id); if (fni_ != NULL) fni_->PushLiteralName(id);
if ((is_getter || is_setter) && peek() != Token::COLON) { if ((is_getter || is_setter) && peek() != Token::COLON) {
...@@ -3545,7 +3552,7 @@ ZoneList<Expression*>* Parser::ParseArguments(bool* ok) { ...@@ -3545,7 +3552,7 @@ ZoneList<Expression*>* Parser::ParseArguments(bool* ok) {
FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
bool name_is_reserved, bool name_is_strict_reserved,
int function_token_position, int function_token_position,
FunctionLiteralType type, FunctionLiteralType type,
bool* ok) { bool* ok) {
...@@ -3589,9 +3596,10 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3589,9 +3596,10 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
bool done = (peek() == Token::RPAREN); bool done = (peek() == Token::RPAREN);
while (!done) { while (!done) {
bool is_reserved = false; bool is_strict_reserved = false;
Handle<String> param_name = Handle<String> param_name =
ParseIdentifierOrReservedWord(&is_reserved, CHECK_OK); ParseIdentifierOrStrictReservedWord(&is_strict_reserved,
CHECK_OK);
// Store locations for possible future error reports. // Store locations for possible future error reports.
if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) { if (!name_loc.IsValid() && IsEvalOrArguments(param_name)) {
...@@ -3601,7 +3609,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3601,7 +3609,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
has_duplicate_parameters = true; has_duplicate_parameters = true;
dupe_loc = scanner().location(); dupe_loc = scanner().location();
} }
if (!reserved_loc.IsValid() && is_reserved) { if (!reserved_loc.IsValid() && is_strict_reserved) {
reserved_loc = scanner().location(); reserved_loc = scanner().location();
} }
...@@ -3703,7 +3711,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name, ...@@ -3703,7 +3711,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> var_name,
*ok = false; *ok = false;
return NULL; return NULL;
} }
if (name_is_reserved) { if (name_is_strict_reserved) {
int position = function_token_position != RelocInfo::kNoPosition int position = function_token_position != RelocInfo::kNoPosition
? function_token_position ? function_token_position
: (start_pos > 0 ? start_pos - 1 : start_pos); : (start_pos > 0 ? start_pos - 1 : start_pos);
...@@ -3792,7 +3800,8 @@ Expression* Parser::ParseV8Intrinsic(bool* ok) { ...@@ -3792,7 +3800,8 @@ Expression* Parser::ParseV8Intrinsic(bool* ok) {
bool Parser::peek_any_identifier() { bool Parser::peek_any_identifier() {
Token::Value next = peek(); Token::Value next = peek();
return next == Token::IDENTIFIER || return next == Token::IDENTIFIER ||
next == Token::FUTURE_RESERVED_WORD; next == Token::FUTURE_RESERVED_WORD ||
next == Token::FUTURE_STRICT_RESERVED_WORD;
} }
...@@ -3854,22 +3863,27 @@ Literal* Parser::GetLiteralNumber(double value) { ...@@ -3854,22 +3863,27 @@ Literal* Parser::GetLiteralNumber(double value) {
} }
// Parses and identifier that is valid for the current scope, in particular it
// fails on strict mode future reserved keywords in a strict scope.
Handle<String> Parser::ParseIdentifier(bool* ok) { Handle<String> Parser::ParseIdentifier(bool* ok) {
bool is_reserved; if (top_scope_->is_strict_mode()) {
return ParseIdentifierOrReservedWord(&is_reserved, ok); Expect(Token::IDENTIFIER, ok);
} else if (!Check(Token::IDENTIFIER)) {
Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok);
}
if (!*ok) return Handle<String>();
return GetSymbol(ok);
} }
Handle<String> Parser::ParseIdentifierOrReservedWord(bool* is_reserved, // Parses and identifier or a strict mode future reserved word, and indicate
bool* ok) { // whether it is strict mode future reserved.
*is_reserved = false; Handle<String> Parser::ParseIdentifierOrStrictReservedWord(
if (top_scope_->is_strict_mode()) { bool* is_strict_reserved, bool* ok) {
Expect(Token::IDENTIFIER, ok); *is_strict_reserved = false;
} else {
if (!Check(Token::IDENTIFIER)) { if (!Check(Token::IDENTIFIER)) {
Expect(Token::FUTURE_RESERVED_WORD, ok); Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok);
*is_reserved = true; *is_strict_reserved = true;
}
} }
if (!*ok) return Handle<String>(); if (!*ok) return Handle<String>();
return GetSymbol(ok); return GetSymbol(ok);
...@@ -3880,6 +3894,7 @@ Handle<String> Parser::ParseIdentifierName(bool* ok) { ...@@ -3880,6 +3894,7 @@ Handle<String> Parser::ParseIdentifierName(bool* ok) {
Token::Value next = Next(); Token::Value next = Next();
if (next != Token::IDENTIFIER && if (next != Token::IDENTIFIER &&
next != Token::FUTURE_RESERVED_WORD && next != Token::FUTURE_RESERVED_WORD &&
next != Token::FUTURE_STRICT_RESERVED_WORD &&
!Token::IsKeyword(next)) { !Token::IsKeyword(next)) {
ReportUnexpectedToken(next); ReportUnexpectedToken(next);
*ok = false; *ok = false;
...@@ -3921,12 +3936,12 @@ void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) { ...@@ -3921,12 +3936,12 @@ void Parser::CheckOctalLiteral(int beg_pos, int end_pos, bool* ok) {
} }
// This function reads an identifier and determines whether or not it // This function reads an identifier name and determines whether or not it
// is 'get' or 'set'. // is 'get' or 'set'.
Handle<String> Parser::ParseIdentifierOrGetOrSet(bool* is_get, Handle<String> Parser::ParseIdentifierNameOrGetOrSet(bool* is_get,
bool* is_set, bool* is_set,
bool* ok) { bool* ok) {
Handle<String> result = ParseIdentifier(ok); Handle<String> result = ParseIdentifierName(ok);
if (!*ok) return Handle<String>(); if (!*ok) return Handle<String>();
if (scanner().is_literal_ascii() && scanner().literal_length() == 3) { if (scanner().is_literal_ascii() && scanner().literal_length() == 3) {
const char* token = scanner().literal_ascii_string().start(); const char* token = scanner().literal_ascii_string().start();
......
...@@ -623,9 +623,10 @@ class Parser { ...@@ -623,9 +623,10 @@ class Parser {
Literal* GetLiteralNumber(double value); Literal* GetLiteralNumber(double value);
Handle<String> ParseIdentifier(bool* ok); Handle<String> ParseIdentifier(bool* ok);
Handle<String> ParseIdentifierOrReservedWord(bool* is_reserved, bool* ok); Handle<String> ParseIdentifierOrStrictReservedWord(
bool* is_strict_reserved, bool* ok);
Handle<String> ParseIdentifierName(bool* ok); Handle<String> ParseIdentifierName(bool* ok);
Handle<String> ParseIdentifierOrGetOrSet(bool* is_get, Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get,
bool* is_set, bool* is_set,
bool* ok); bool* ok);
......
...@@ -77,9 +77,14 @@ void PreParser::ReportUnexpectedToken(i::Token::Value token) { ...@@ -77,9 +77,14 @@ void PreParser::ReportUnexpectedToken(i::Token::Value token) {
return ReportMessageAt(source_location.beg_pos, source_location.end_pos, return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_token_string", NULL); "unexpected_token_string", NULL);
case i::Token::IDENTIFIER: case i::Token::IDENTIFIER:
case i::Token::FUTURE_RESERVED_WORD:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos, return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_token_identifier", NULL); "unexpected_token_identifier", NULL);
case i::Token::FUTURE_RESERVED_WORD:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_reserved", NULL);
case i::Token::FUTURE_STRICT_RESERVED_WORD:
return ReportMessageAt(source_location.beg_pos, source_location.end_pos,
"unexpected_strict_reserved", NULL);
default: default:
const char* name = i::Token::String(token); const char* name = i::Token::String(token);
ReportMessageAt(source_location.beg_pos, source_location.end_pos, ReportMessageAt(source_location.beg_pos, source_location.end_pos,
...@@ -233,7 +238,7 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) { ...@@ -233,7 +238,7 @@ PreParser::Statement PreParser::ParseFunctionDeclaration(bool* ok) {
// Strict mode violation, using either reserved word or eval/arguments // Strict mode violation, using either reserved word or eval/arguments
// as name of strict function. // as name of strict function.
const char* type = "strict_function_name"; const char* type = "strict_function_name";
if (identifier.IsFutureReserved()) { if (identifier.IsFutureStrictReserved()) {
type = "strict_reserved_word"; type = "strict_reserved_word";
} }
ReportMessageAt(location.beg_pos, location.end_pos, type, NULL); ReportMessageAt(location.beg_pos, location.end_pos, type, NULL);
...@@ -979,7 +984,16 @@ PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) { ...@@ -979,7 +984,16 @@ PreParser::Expression PreParser::ParsePrimaryExpression(bool* ok) {
break; break;
} }
case i::Token::FUTURE_RESERVED_WORD: case i::Token::FUTURE_RESERVED_WORD: {
Next();
i::Scanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"reserved_word", NULL);
*ok = false;
return Expression::Default();
}
case i::Token::FUTURE_STRICT_RESERVED_WORD:
if (strict_mode()) { if (strict_mode()) {
Next(); Next();
i::Scanner::Location location = scanner_->location(); i::Scanner::Location location = scanner_->location();
...@@ -1078,15 +1092,17 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) { ...@@ -1078,15 +1092,17 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) {
i::Token::Value next = peek(); i::Token::Value next = peek();
switch (next) { switch (next) {
case i::Token::IDENTIFIER: case i::Token::IDENTIFIER:
case i::Token::FUTURE_RESERVED_WORD: { case i::Token::FUTURE_RESERVED_WORD:
case i::Token::FUTURE_STRICT_RESERVED_WORD: {
bool is_getter = false; bool is_getter = false;
bool is_setter = false; bool is_setter = false;
ParseIdentifierOrGetOrSet(&is_getter, &is_setter, CHECK_OK); ParseIdentifierNameOrGetOrSet(&is_getter, &is_setter, CHECK_OK);
if ((is_getter || is_setter) && peek() != i::Token::COLON) { if ((is_getter || is_setter) && peek() != i::Token::COLON) {
i::Token::Value name = Next(); i::Token::Value name = Next();
bool is_keyword = i::Token::IsKeyword(name); bool is_keyword = i::Token::IsKeyword(name);
if (name != i::Token::IDENTIFIER && if (name != i::Token::IDENTIFIER &&
name != i::Token::FUTURE_RESERVED_WORD && name != i::Token::FUTURE_RESERVED_WORD &&
name != i::Token::FUTURE_STRICT_RESERVED_WORD &&
name != i::Token::NUMBER && name != i::Token::NUMBER &&
name != i::Token::STRING && name != i::Token::STRING &&
!is_keyword) { !is_keyword) {
...@@ -1312,6 +1328,9 @@ PreParser::Identifier PreParser::GetIdentifierSymbol() { ...@@ -1312,6 +1328,9 @@ PreParser::Identifier PreParser::GetIdentifierSymbol() {
LogSymbol(); LogSymbol();
if (scanner_->current_token() == i::Token::FUTURE_RESERVED_WORD) { if (scanner_->current_token() == i::Token::FUTURE_RESERVED_WORD) {
return Identifier::FutureReserved(); return Identifier::FutureReserved();
} else if (scanner_->current_token() ==
i::Token::FUTURE_STRICT_RESERVED_WORD) {
return Identifier::FutureStrictReserved();
} }
if (scanner_->is_literal_ascii()) { if (scanner_->is_literal_ascii()) {
// Detect strict-mode poison words. // Detect strict-mode poison words.
...@@ -1329,11 +1348,22 @@ PreParser::Identifier PreParser::GetIdentifierSymbol() { ...@@ -1329,11 +1348,22 @@ PreParser::Identifier PreParser::GetIdentifierSymbol() {
PreParser::Identifier PreParser::ParseIdentifier(bool* ok) { PreParser::Identifier PreParser::ParseIdentifier(bool* ok) {
if (!Check(i::Token::FUTURE_RESERVED_WORD)) { i::Token::Value next = Next();
Expect(i::Token::IDENTIFIER, ok); switch (next) {
if (!*ok) return Identifier::Default(); case i::Token::FUTURE_RESERVED_WORD: {
i::Scanner::Location location = scanner_->location();
ReportMessageAt(location.beg_pos, location.end_pos,
"reserved_word", NULL);
*ok = false;
} }
// FALLTHROUGH
case i::Token::FUTURE_STRICT_RESERVED_WORD:
case i::Token::IDENTIFIER:
return GetIdentifierSymbol(); return GetIdentifierSymbol();
default:
*ok = false;
return Identifier::Default();
}
} }
...@@ -1373,6 +1403,8 @@ void PreParser::StrictModeIdentifierViolation(i::Scanner::Location location, ...@@ -1373,6 +1403,8 @@ void PreParser::StrictModeIdentifierViolation(i::Scanner::Location location,
bool* ok) { bool* ok) {
const char* type = eval_args_type; const char* type = eval_args_type;
if (identifier.IsFutureReserved()) { if (identifier.IsFutureReserved()) {
type = "reserved_word";
} else if (identifier.IsFutureStrictReserved()) {
type = "strict_reserved_word"; type = "strict_reserved_word";
} }
if (strict_mode()) { if (strict_mode()) {
...@@ -1395,7 +1427,8 @@ PreParser::Identifier PreParser::ParseIdentifierName(bool* ok) { ...@@ -1395,7 +1427,8 @@ PreParser::Identifier PreParser::ParseIdentifierName(bool* ok) {
return Identifier::Default(); return Identifier::Default();
} }
if (next == i::Token::IDENTIFIER || if (next == i::Token::IDENTIFIER ||
next == i::Token::FUTURE_RESERVED_WORD) { next == i::Token::FUTURE_RESERVED_WORD ||
next == i::Token::FUTURE_STRICT_RESERVED_WORD) {
return GetIdentifierSymbol(); return GetIdentifierSymbol();
} }
*ok = false; *ok = false;
...@@ -1407,10 +1440,10 @@ PreParser::Identifier PreParser::ParseIdentifierName(bool* ok) { ...@@ -1407,10 +1440,10 @@ PreParser::Identifier PreParser::ParseIdentifierName(bool* ok) {
// This function reads an identifier and determines whether or not it // This function reads an identifier and determines whether or not it
// is 'get' or 'set'. // is 'get' or 'set'.
PreParser::Identifier PreParser::ParseIdentifierOrGetOrSet(bool* is_get, PreParser::Identifier PreParser::ParseIdentifierNameOrGetOrSet(bool* is_get,
bool* is_set, bool* is_set,
bool* ok) { bool* ok) {
Identifier result = ParseIdentifier(ok); Identifier result = ParseIdentifierName(ok);
if (!*ok) return Identifier::Default(); if (!*ok) return Identifier::Default();
if (scanner_->is_literal_ascii() && if (scanner_->is_literal_ascii() &&
scanner_->literal_length() == 3) { scanner_->literal_length() == 3) {
...@@ -1424,6 +1457,7 @@ PreParser::Identifier PreParser::ParseIdentifierOrGetOrSet(bool* is_get, ...@@ -1424,6 +1457,7 @@ PreParser::Identifier PreParser::ParseIdentifierOrGetOrSet(bool* is_get,
bool PreParser::peek_any_identifier() { bool PreParser::peek_any_identifier() {
i::Token::Value next = peek(); i::Token::Value next = peek();
return next == i::Token::IDENTIFIER || return next == i::Token::IDENTIFIER ||
next == i::Token::FUTURE_RESERVED_WORD; next == i::Token::FUTURE_RESERVED_WORD ||
next == i::Token::FUTURE_STRICT_RESERVED_WORD;
} }
} } // v8::preparser } } // v8::preparser
...@@ -93,16 +93,23 @@ class PreParser { ...@@ -93,16 +93,23 @@ class PreParser {
static Identifier FutureReserved() { static Identifier FutureReserved() {
return Identifier(kFutureReservedIdentifier); return Identifier(kFutureReservedIdentifier);
} }
static Identifier FutureStrictReserved() {
return Identifier(kFutureStrictReservedIdentifier);
}
bool IsEval() { return type_ == kEvalIdentifier; } bool IsEval() { return type_ == kEvalIdentifier; }
bool IsArguments() { return type_ == kArgumentsIdentifier; } bool IsArguments() { return type_ == kArgumentsIdentifier; }
bool IsEvalOrArguments() { return type_ >= kEvalIdentifier; } bool IsEvalOrArguments() { return type_ >= kEvalIdentifier; }
bool IsFutureReserved() { return type_ == kFutureReservedIdentifier; } bool IsFutureReserved() { return type_ == kFutureReservedIdentifier; }
bool IsFutureStrictReserved() {
return type_ == kFutureStrictReservedIdentifier;
}
bool IsValidStrictVariable() { return type_ == kUnknownIdentifier; } bool IsValidStrictVariable() { return type_ == kUnknownIdentifier; }
private: private:
enum Type { enum Type {
kUnknownIdentifier, kUnknownIdentifier,
kFutureReservedIdentifier, kFutureReservedIdentifier,
kFutureStrictReservedIdentifier,
kEvalIdentifier, kEvalIdentifier,
kArgumentsIdentifier kArgumentsIdentifier
}; };
...@@ -411,7 +418,9 @@ class PreParser { ...@@ -411,7 +418,9 @@ class PreParser {
Identifier ParseIdentifier(bool* ok); Identifier ParseIdentifier(bool* ok);
Identifier ParseIdentifierName(bool* ok); Identifier ParseIdentifierName(bool* ok);
Identifier ParseIdentifierOrGetOrSet(bool* is_get, bool* is_set, bool* ok); Identifier ParseIdentifierNameOrGetOrSet(bool* is_get,
bool* is_set,
bool* ok);
// Logs the currently parsed literal as a symbol in the preparser data. // Logs the currently parsed literal as a symbol in the preparser data.
void LogSymbol(); void LogSymbol();
......
...@@ -799,7 +799,7 @@ KeywordMatcher::FirstState KeywordMatcher::first_states_[] = { ...@@ -799,7 +799,7 @@ KeywordMatcher::FirstState KeywordMatcher::first_states_[] = {
{ NULL, I, Token::ILLEGAL }, { NULL, I, Token::ILLEGAL },
{ NULL, UNMATCHABLE, Token::ILLEGAL }, { NULL, UNMATCHABLE, Token::ILLEGAL },
{ NULL, UNMATCHABLE, Token::ILLEGAL }, { NULL, UNMATCHABLE, Token::ILLEGAL },
{ "let", KEYWORD_PREFIX, Token::FUTURE_RESERVED_WORD }, { "let", KEYWORD_PREFIX, Token::FUTURE_STRICT_RESERVED_WORD },
{ NULL, UNMATCHABLE, Token::ILLEGAL }, { NULL, UNMATCHABLE, Token::ILLEGAL },
{ NULL, N, Token::ILLEGAL }, { NULL, N, Token::ILLEGAL },
{ NULL, UNMATCHABLE, Token::ILLEGAL }, { NULL, UNMATCHABLE, Token::ILLEGAL },
...@@ -812,7 +812,7 @@ KeywordMatcher::FirstState KeywordMatcher::first_states_[] = { ...@@ -812,7 +812,7 @@ KeywordMatcher::FirstState KeywordMatcher::first_states_[] = {
{ NULL, V, Token::ILLEGAL }, { NULL, V, Token::ILLEGAL },
{ NULL, W, Token::ILLEGAL }, { NULL, W, Token::ILLEGAL },
{ NULL, UNMATCHABLE, Token::ILLEGAL }, { NULL, UNMATCHABLE, Token::ILLEGAL },
{ "yield", KEYWORD_PREFIX, Token::FUTURE_RESERVED_WORD } { "yield", KEYWORD_PREFIX, Token::FUTURE_STRICT_RESERVED_WORD }
}; };
...@@ -900,14 +900,14 @@ void KeywordMatcher::Step(unibrow::uchar input) { ...@@ -900,14 +900,14 @@ void KeywordMatcher::Step(unibrow::uchar input) {
break; break;
case IMP: case IMP:
if (MatchKeywordStart(input, "implements", 3, if (MatchKeywordStart(input, "implements", 3,
Token::FUTURE_RESERVED_WORD )) return; Token::FUTURE_STRICT_RESERVED_WORD )) return;
if (MatchKeywordStart(input, "import", 3, if (MatchKeywordStart(input, "import", 3,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_RESERVED_WORD)) return;
break; break;
case IN: case IN:
token_ = Token::IDENTIFIER; token_ = Token::IDENTIFIER;
if (MatchKeywordStart(input, "interface", 2, if (MatchKeywordStart(input, "interface", 2,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
if (MatchKeywordStart(input, "instanceof", 2, Token::INSTANCEOF)) return; if (MatchKeywordStart(input, "instanceof", 2, Token::INSTANCEOF)) return;
break; break;
case N: case N:
...@@ -916,20 +916,20 @@ void KeywordMatcher::Step(unibrow::uchar input) { ...@@ -916,20 +916,20 @@ void KeywordMatcher::Step(unibrow::uchar input) {
break; break;
case P: case P:
if (MatchKeywordStart(input, "package", 1, if (MatchKeywordStart(input, "package", 1,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
if (MatchState(input, 'r', PR)) return; if (MatchState(input, 'r', PR)) return;
if (MatchKeywordStart(input, "public", 1, if (MatchKeywordStart(input, "public", 1,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
break; break;
case PR: case PR:
if (MatchKeywordStart(input, "private", 2, if (MatchKeywordStart(input, "private", 2,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
if (MatchKeywordStart(input, "protected", 2, if (MatchKeywordStart(input, "protected", 2,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
break; break;
case S: case S:
if (MatchKeywordStart(input, "static", 1, if (MatchKeywordStart(input, "static", 1,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_STRICT_RESERVED_WORD)) return;
if (MatchKeywordStart(input, "super", 1, if (MatchKeywordStart(input, "super", 1,
Token::FUTURE_RESERVED_WORD)) return; Token::FUTURE_RESERVED_WORD)) return;
if (MatchKeywordStart(input, "switch", 1, if (MatchKeywordStart(input, "switch", 1,
......
...@@ -549,14 +549,20 @@ class JavaScriptScanner : public Scanner { ...@@ -549,14 +549,20 @@ class JavaScriptScanner : public Scanner {
class KeywordMatcher { class KeywordMatcher {
// Incrementally recognize keywords. // Incrementally recognize keywords.
// //
// We distinguish between normal future reserved words and words that are
// considered to be future reserved words only in strict mode as required by
// ECMA-262 7.6.1.2.
//
// Recognized as keywords: // Recognized as keywords:
// break, case, catch, const*, continue, debugger, default, delete, do, // break, case, catch, const*, continue, debugger, default, delete, do,
// else, finally, false, for, function, if, in, instanceof, new, null, // else, finally, false, for, function, if, in, instanceof, new, null,
// return, switch, this, throw, true, try, typeof, var, void, while, with. // return, switch, this, throw, true, try, typeof, var, void, while, with.
// //
// Recognized as Future Reserved Keywords (normal and strict mode): // Recognized as Future Reserved Keywords:
// class, enum, export, extends, implements, import, interface, // class, enum, export, extends, import, super.
// let, package, private, private, protected, public, public, //
// Recognized as Future Reserved Keywords (strict mode only):
// implements, interface, let, package, private, protected, public,
// static, yield. // static, yield.
// //
// *: Actually a "future reserved keyword". It's the only one we are // *: Actually a "future reserved keyword". It's the only one we are
......
...@@ -167,6 +167,7 @@ namespace internal { ...@@ -167,6 +167,7 @@ namespace internal {
\ \
/* Future reserved words (ECMA-262, section 7.6.1.2). */ \ /* Future reserved words (ECMA-262, section 7.6.1.2). */ \
T(FUTURE_RESERVED_WORD, NULL, 0) \ T(FUTURE_RESERVED_WORD, NULL, 0) \
T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \
K(CONST, "const", 0) \ K(CONST, "const", 0) \
\ \
/* Illegal token - not able to scan. */ \ /* Illegal token - not able to scan. */ \
......
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Test proper handling of keywords, future reserved words and
// future reserved words in strict mode as specific by 7.6.1 and 7.6.2
// in ECMA-262.
// This code is based on:
// http://trac.webkit.org/export/89109/trunk/LayoutTests/fast/js/script-tests/keywords-and-reserved_words.js
function isKeyword(x)
{
try {
eval("var " + x + ";");
} catch(e) {
return true;
}
return false;
}
function isStrictKeyword(x)
{
try {
eval("'use strict'; var "+x+";");
} catch(e) {
return true;
}
return false;
}
function classifyIdentifier(x)
{
if (isKeyword(x)) {
// All non-strict keywords are also keywords in strict code.
if (!isStrictKeyword(x)) {
return "ERROR";
}
return "keyword";
}
// Check for strict mode future reserved words.
if (isStrictKeyword(x)) {
return "strict";
}
return "identifier";
}
function testKeyword(word) {
// Classify word
assertEquals("keyword", classifyIdentifier(word));
// Simple use of a keyword
assertThrows("var " + word + " = 1;", SyntaxError);
if (word != "this") {
assertThrows("typeof (" + word + ");", SyntaxError);
}
// object literal properties
eval("var x = { " + word + " : 42 };");
eval("var x = { get " + word + " () {} };");
eval("var x = { set " + word + " (value) {} };");
// object literal with string literal property names
eval("var x = { '" + word + "' : 42 };");
eval("var x = { get '" + word + "' () { } };");
eval("var x = { set '" + word + "' (value) { } };");
// Function names and arguments
assertThrows("function " + word + " () { }", SyntaxError);
assertThrows("function foo (" + word + ") {}", SyntaxError);
assertThrows("function foo (a, " + word + ") { }", SyntaxError);
assertThrows("function foo (" + word + ", a) { }", SyntaxError);
assertThrows("function foo (a, " + word + ", b) { }", SyntaxError);
assertThrows("var foo = function (" + word + ") { }", SyntaxError);
// setter parameter
assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError);
}
// Not keywords - these are all just identifiers.
var identifiers = [
"x", "keyword",
"id", "strict",
"identifier", "use",
// The following are reserved in ES3 but not in ES5.
"abstract", "int",
"boolean", "long",
"byte", "native",
"char", "short",
"double", "synchronized",
"final", "throws",
"float", "transient",
"goto", "volatile" ];
for (var i = 0; i < identifiers.length; i++) {
assertEquals ("identifier", classifyIdentifier(identifiers[i]));
}
// 7.6.1.1 Keywords
var keywords = [
"break", "in",
"case", "instanceof",
"catch", "new",
"continue", "return",
"debugger", "switch",
"default", "this",
"delete", "throw",
"do", "try",
"else", "typeof",
"finally", "var",
"for", "void",
"function", "while",
"if", "with",
// In ES5 "const" is a "future reserved word" but we treat it as a keyword.
"const" ];
for (var i = 0; i < keywords.length; i++) {
testKeyword(keywords[i]);
}
// 7.6.1.2 Future Reserved Words (without "const")
var future_reserved_words = [
"class",
"enum",
"export",
"extends",
"import",
"super" ];
for (var i = 0; i < future_reserved_words.length; i++) {
testKeyword(future_reserved_words[i]);
}
// 7.6.1.2 Future Reserved Words, in strict mode only.
var future_strict_reserved_words = [
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield" ];
for (var i = 0; i < future_strict_reserved_words.length; i++) {
assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i]));
}
// More strict mode specific tests can be found in mjsunit/strict-mode.js.
...@@ -67,6 +67,9 @@ function CheckFunctionConstructorStrictMode() { ...@@ -67,6 +67,9 @@ function CheckFunctionConstructorStrictMode() {
with ({}) {}; with ({}) {};
})(); })();
// Incorrectly place 'use strict' directive.
assertThrows("function foo (x) 'use strict'; {}", SyntaxError);
// 'use strict' in non-directive position. // 'use strict' in non-directive position.
(function UseStrictNonDirective() { (function UseStrictNonDirective() {
void(0); void(0);
...@@ -319,14 +322,8 @@ CheckStrictMode("var variable; delete variable;", SyntaxError); ...@@ -319,14 +322,8 @@ CheckStrictMode("var variable; delete variable;", SyntaxError);
+arguments, -arguments, ~arguments, !arguments]; +arguments, -arguments, ~arguments, !arguments];
})(); })();
// 7.6.1.2 Future Reserved Words // 7.6.1.2 Future Reserved Words in strict mode
var future_reserved_words = [ var future_strict_reserved_words = [
"class",
"enum",
"export",
"extends",
"import",
"super",
"implements", "implements",
"interface", "interface",
"let", "let",
...@@ -337,14 +334,17 @@ var future_reserved_words = [ ...@@ -337,14 +334,17 @@ var future_reserved_words = [
"static", "static",
"yield" ]; "yield" ];
function testFutureReservedWord(word) { function testFutureStrictReservedWord(word) {
// Simple use of each reserved word // Simple use of each reserved word
CheckStrictMode("var " + word + " = 1;", SyntaxError); CheckStrictMode("var " + word + " = 1;", SyntaxError);
CheckStrictMode("typeof (" + word + ");", SyntaxError);
// object literal properties // object literal properties
eval("var x = { " + word + " : 42 };"); eval("var x = { " + word + " : 42 };");
eval("var x = { get " + word + " () {} };"); eval("var x = { get " + word + " () {} };");
eval("var x = { set " + word + " (value) {} };"); eval("var x = { set " + word + " (value) {} };");
eval("var x = { get " + word + " () { 'use strict'; } };");
eval("var x = { set " + word + " (value) { 'use strict'; } };");
// object literal with string literal property names // object literal with string literal property names
eval("var x = { '" + word + "' : 42 };"); eval("var x = { '" + word + "' : 42 };");
...@@ -364,7 +364,6 @@ function testFutureReservedWord(word) { ...@@ -364,7 +364,6 @@ function testFutureReservedWord(word) {
// Function names and arguments when the body is strict // Function names and arguments when the body is strict
assertThrows("function " + word + " () { 'use strict'; }", SyntaxError); assertThrows("function " + word + " () { 'use strict'; }", SyntaxError);
assertThrows("function foo (" + word + ") 'use strict'; {}", SyntaxError);
assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }", assertThrows("function foo (" + word + ", " + word + ") { 'use strict'; }",
SyntaxError); SyntaxError);
assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError); assertThrows("function foo (a, " + word + ") { 'use strict'; }", SyntaxError);
...@@ -374,17 +373,14 @@ function testFutureReservedWord(word) { ...@@ -374,17 +373,14 @@ function testFutureReservedWord(word) {
assertThrows("var foo = function (" + word + ") { 'use strict'; }", assertThrows("var foo = function (" + word + ") { 'use strict'; }",
SyntaxError); SyntaxError);
// get/set when the body is strict // setter parameter when the body is strict
eval("var x = { get " + word + " () { 'use strict'; } };"); CheckStrictMode("var x = { set foo(" + word + ") {} };", SyntaxError);
eval("var x = { set " + word + " (value) { 'use strict'; } };");
assertThrows("var x = { get foo(" + word + ") { 'use strict'; } };",
SyntaxError);
assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };", assertThrows("var x = { set foo(" + word + ") { 'use strict'; } };",
SyntaxError); SyntaxError);
} }
for (var i = 0; i < future_reserved_words.length; i++) { for (var i = 0; i < future_strict_reserved_words.length; i++) {
testFutureReservedWord(future_reserved_words[i]); testFutureStrictReservedWord(future_strict_reserved_words[i]);
} }
function testAssignToUndefined(test, should_throw) { function testAssignToUndefined(test, should_throw) {
......
...@@ -164,6 +164,20 @@ non_strict_use = Template("nonstrict-$id", """ ...@@ -164,6 +164,20 @@ non_strict_use = Template("nonstrict-$id", """
x = {get $id() {}, set $id(value) {}}; x = {get $id() {}, set $id(value) {}};
""") """)
identifier_name_source = """
var x = {$id: 42};
x = {get $id() {}, set $id(value) {}};
x.$id = 42;
function foo() { "use strict;" }
x = {$id: 42};
x = {get $id() {}, set $id(value) {}};
x.$id = 42;
"""
identifier_name = Template("identifier_name-$id", identifier_name_source)
identifier_name_strict = StrictTemplate("identifier_name_strict-$id",
identifier_name_source)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# Run tests # Run tests
...@@ -189,8 +203,12 @@ for id in ["eval", "arguments"]: ...@@ -189,8 +203,12 @@ for id in ["eval", "arguments"]:
# Reserved words just throw the same exception in all cases # Reserved words just throw the same exception in all cases
# (with "const" being special, as usual). # (with "const" being special, as usual).
for reserved_word in reserved_words + strict_reserved_words: for reserved_word in reserved_words + strict_reserved_words:
if (reserved_word in strict_reserved_words):
message = "strict_reserved_word" message = "strict_reserved_word"
if (reserved_word == "const"): message = "unexpected_token" elif (reserved_word == "const"):
message = "unexpected_token"
else:
message = "reserved_word"
arg_name_own({"id":reserved_word}, message) arg_name_own({"id":reserved_word}, message)
arg_name_nested({"id":reserved_word}, message) arg_name_nested({"id":reserved_word}, message)
setter_arg({"id": reserved_word}, message) setter_arg({"id": reserved_word}, message)
...@@ -205,5 +223,11 @@ for reserved_word in reserved_words + strict_reserved_words: ...@@ -205,5 +223,11 @@ for reserved_word in reserved_words + strict_reserved_words:
postfix_var({"id":reserved_word, "op":"++", "opname":"inc"}, message) postfix_var({"id":reserved_word, "op":"++", "opname":"inc"}, message)
postfix_var({"id":reserved_word, "op":"--", "opname":"dec"}, message) postfix_var({"id":reserved_word, "op":"--", "opname":"dec"}, message)
read_var({"id": reserved_word}, message) read_var({"id": reserved_word}, message)
identifier_name({"id": reserved_word}, None);
identifier_name_strict({"id": reserved_word}, None);
# Future reserved words in strict mode behave like normal identifiers
# in a non strict context.
for reserved_word in strict_reserved_words:
non_strict_use({"id": id}, None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment