From ceaa6df2fe1b07048ff424b03544b9df2685203b Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 27 Oct 2022 03:44:26 +0000 Subject: [PATCH] Add strict mode reserved words of js Issue: https://e.gitee.com/open_harmony/dashboard?issue=I5Y0MO Test: test262,parser tests, compiler tests Signed-off-by: zhangrengao Change-Id: Ib0f870b27d89ad86c8225e133968366c58760145 --- es2panda/lexer/token/token.cpp | 21 +++ es2panda/lexer/token/token.h | 1 + es2panda/parser/expressionParser.cpp | 4 +- es2panda/parser/parserImpl.cpp | 13 ++ es2panda/parser/parserImpl.h | 1 + es2panda/parser/statementParser.cpp | 4 +- ...unction-declaration-arguments-expected.txt | 1 + .../js/function-declaration-arguments.js | 4 + .../js/function-declaration-is-expected.txt | 101 ++++++++++++ .../test/parser/js/function-declaration-is.js | 4 + ...function-expression-arguments-expected.txt | 1 + .../js/function-expression-arguments.js | 4 + .../js/function-expression-is-expected.txt | 145 ++++++++++++++++++ .../test/parser/js/function-expression-is.js | 4 + 14 files changed, 303 insertions(+), 5 deletions(-) create mode 100644 es2panda/test/parser/js/function-declaration-arguments-expected.txt create mode 100644 es2panda/test/parser/js/function-declaration-arguments.js create mode 100644 es2panda/test/parser/js/function-declaration-is-expected.txt create mode 100644 es2panda/test/parser/js/function-declaration-is.js create mode 100644 es2panda/test/parser/js/function-expression-arguments-expected.txt create mode 100644 es2panda/test/parser/js/function-expression-arguments.js create mode 100644 es2panda/test/parser/js/function-expression-is-expected.txt create mode 100644 es2panda/test/parser/js/function-expression-is.js diff --git a/es2panda/lexer/token/token.cpp b/es2panda/lexer/token/token.cpp index 12521b9dad..ba04e5d974 100644 --- a/es2panda/lexer/token/token.cpp +++ b/es2panda/lexer/token/token.cpp @@ -92,6 +92,27 @@ bool Token::IsReservedTypeName() const } } +bool Token::IsJsStrictReservedWord() const +{ + switch (keywordType_) + { + case TokenType::KEYW_ARGUMENTS: + case TokenType::KEYW_EVAL: + case TokenType::KEYW_STATIC: + case TokenType::KEYW_PRIVATE: + case TokenType::KEYW_PROTECTED: + case TokenType::KEYW_PUBLIC: + case TokenType::KEYW_IMPLEMENTS: + case TokenType::KEYW_INTERFACE: + case TokenType::KEYW_PACKAGE: + case TokenType::KEYW_LET: + case TokenType::KEYW_YIELD: + return true; + default: + return false; + } +} + bool Token::IsBinaryToken(TokenType type) { return (type >= TokenType::PUNCTUATOR_NULLISH_COALESCING && type <= TokenType::PUNCTUATOR_EXPONENTIATION); diff --git a/es2panda/lexer/token/token.h b/es2panda/lexer/token/token.h index 46a87d432c..26edb60d38 100644 --- a/es2panda/lexer/token/token.h +++ b/es2panda/lexer/token/token.h @@ -116,6 +116,7 @@ public: bool IsPropNameLiteral() const; bool IsKeyword() const; bool IsReservedTypeName() const; + bool IsJsStrictReservedWord() const; static bool IsBinaryToken(TokenType type); static bool IsBinaryLvalueToken(TokenType type); diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index e66239d189..4644359241 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -2339,9 +2339,7 @@ ir::FunctionExpression *ParserImpl::ParseFunctionExpression(ParserStatus newStat ThrowSyntaxError("Expected an identifier."); } - if (lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { - ThrowSyntaxError("Unexpected reserved word in strict mode."); - } + CheckStrictReservedWord(); ident = AllocNode(lexer_->GetToken().Ident(), Allocator()); ident->SetRange(lexer_->GetToken().Loc()); diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index d3039e2a13..7e415b2480 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -3450,4 +3450,17 @@ void ParserImpl::AddHotfixHelper(util::Hotfix *hotfixHelper) program_.AddHotfixHelper(hotfixHelper); } +void ParserImpl::CheckStrictReservedWord() const +{ + if (Extension() == ScriptExtension::JS) { + if (lexer_->GetToken().IsJsStrictReservedWord()) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } + } else { + if (lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { + ThrowSyntaxError("Unexpected reserved word in strict mode."); + } + } +} + } // namespace panda::es2panda::parser diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index 19138ee7a6..edbee27d3a 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -401,6 +401,7 @@ private: ir::ExportNamedDeclaration *ParseNamedExportDeclaration(const lexer::SourcePosition &startLoc, ArenaVector &&decorators); ir::Identifier *ParseNamedExport(const lexer::Token &exportedToken); + void CheckStrictReservedWord() const; // StatementParser.Cpp diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index ffa8688480..18e08914fb 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1009,8 +1009,8 @@ ir::FunctionDeclaration *ParserImpl::ParseFunctionDeclaration(bool canBeAnonymou ThrowSyntaxError("Unexpected token, expected identifier after 'function' keyword"); } - if (!isDeclare && lexer_->GetToken().KeywordType() >= lexer::TokenType::KEYW_ARGUMENTS) { - ThrowSyntaxError("Unexpected reserved word in strict mode."); + if (!isDeclare) { + CheckStrictReservedWord(); } util::StringView ident = lexer_->GetToken().Ident(); diff --git a/es2panda/test/parser/js/function-declaration-arguments-expected.txt b/es2panda/test/parser/js/function-declaration-arguments-expected.txt new file mode 100644 index 0000000000..5ee5461ec9 --- /dev/null +++ b/es2panda/test/parser/js/function-declaration-arguments-expected.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected reserved word in strict mode. [function-declaration-arguments.js:3:10] diff --git a/es2panda/test/parser/js/function-declaration-arguments.js b/es2panda/test/parser/js/function-declaration-arguments.js new file mode 100644 index 0000000000..696c3c46cf --- /dev/null +++ b/es2panda/test/parser/js/function-declaration-arguments.js @@ -0,0 +1,4 @@ +'use strict' + +function arguments() { +} \ No newline at end of file diff --git a/es2panda/test/parser/js/function-declaration-is-expected.txt b/es2panda/test/parser/js/function-declaration-is-expected.txt new file mode 100644 index 0000000000..140342c6b1 --- /dev/null +++ b/es2panda/test/parser/js/function-declaration-is-expected.txt @@ -0,0 +1,101 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "StringLiteral", + "value": "use strict", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "is", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 12 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/js/function-declaration-is.js b/es2panda/test/parser/js/function-declaration-is.js new file mode 100644 index 0000000000..6db6828316 --- /dev/null +++ b/es2panda/test/parser/js/function-declaration-is.js @@ -0,0 +1,4 @@ +'use strict' + +function is() { +} \ No newline at end of file diff --git a/es2panda/test/parser/js/function-expression-arguments-expected.txt b/es2panda/test/parser/js/function-expression-arguments-expected.txt new file mode 100644 index 0000000000..1861431645 --- /dev/null +++ b/es2panda/test/parser/js/function-expression-arguments-expected.txt @@ -0,0 +1 @@ +SyntaxError: Unexpected reserved word in strict mode. [function-expression-arguments.js:3:20] diff --git a/es2panda/test/parser/js/function-expression-arguments.js b/es2panda/test/parser/js/function-expression-arguments.js new file mode 100644 index 0000000000..a8d93883a7 --- /dev/null +++ b/es2panda/test/parser/js/function-expression-arguments.js @@ -0,0 +1,4 @@ +'use strict' + +let foo = function arguments() { +} \ No newline at end of file diff --git a/es2panda/test/parser/js/function-expression-is-expected.txt b/es2panda/test/parser/js/function-expression-is-expected.txt new file mode 100644 index 0000000000..25297cebbb --- /dev/null +++ b/es2panda/test/parser/js/function-expression-is-expected.txt @@ -0,0 +1,145 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "StringLiteral", + "value": "use strict", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + "init": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "is", + "decorators": [], + "loc": { + "start": { + "line": 3, + "column": 20 + }, + "end": { + "line": 3, + "column": 22 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 3, + "column": 25 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 4, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 4, + "column": 2 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 4, + "column": 2 + } + } +} diff --git a/es2panda/test/parser/js/function-expression-is.js b/es2panda/test/parser/js/function-expression-is.js new file mode 100644 index 0000000000..6b90fce307 --- /dev/null +++ b/es2panda/test/parser/js/function-expression-is.js @@ -0,0 +1,4 @@ +'use strict' + +let foo = function is() { +} \ No newline at end of file -- Gitee