Throw a syntax error if eval or arguments is used in increment/decrement expressions
authorSimon Hausmann <simon.hausmann@digia.com>
Thu, 24 Jan 2013 14:36:33 +0000 (15:36 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Thu, 24 Jan 2013 14:43:33 +0000 (15:43 +0100)
Change-Id: Icd966b626a172302eb4b22f5cadba0085d31c320
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
qv4codegen.cpp
qv4codegen_p.h
tests/TestExpectations

index 655a707..0552616 100644 (file)
@@ -305,7 +305,7 @@ protected:
 
     virtual bool visit(VariableDeclaration *ast)
     {
-        if (_env->isStrict && (ast->name == QLatin1String("eval") || ast->name == "arguments"))
+        if (_env->isStrict && (ast->name == QLatin1String("eval") || ast->name == QLatin1String("arguments")))
             _cg->throwSyntaxError(ast->identifierToken, QCoreApplication::translate("qv4codegen", "Variable name may not be eval or arguments in strict mode"));
         checkName(ast->name, ast->identifierToken);
         if (ast->name == QLatin1String("arguments"))
@@ -1138,11 +1138,7 @@ bool Codegen::visit(BinaryExpression *ast)
     }
 
     IR::Expr* left = *expression(ast->left);
-    if (_function->isStrict) {
-        if (IR::Name *n = left->asName())
-            if (*n->id == QLatin1String("eval") || *n->id == QLatin1String("arguments"))
-            throwSyntaxError(ast->left->lastSourceLocation(), QCoreApplication::translate("qv4codegen", "Variable name may not be eval or arguments in strict mode"));
-    }
+    throwSyntaxErrorOnEvalOrArgumentsInStrictMode(left, ast->left->lastSourceLocation());
 
     switch (ast->op) {
     case QSOperator::Or:
@@ -1531,15 +1527,8 @@ bool Codegen::visit(ObjectLiteral *ast)
 
 bool Codegen::visit(PostDecrementExpression *ast)
 {
-    // ###
-    //    Throw a SyntaxError exception if the following conditions are all true:
-    //    Type(lhs) is Reference is true
-    //    IsStrictReference(lhs) is true
-    //    Type(GetBase(lhs)) is Environment Record
-    //    GetReferencedName(lhs) is either "eval" or "arguments"
-
-
     Result expr = expression(ast->base);
+    throwSyntaxErrorOnEvalOrArgumentsInStrictMode(*expr, ast->decrementToken);
     if (_expr.accept(nx)) {
         move(*expr, unop(IR::OpDecrement, *expr));
     } else {
@@ -1553,14 +1542,8 @@ bool Codegen::visit(PostDecrementExpression *ast)
 
 bool Codegen::visit(PostIncrementExpression *ast)
 {
-    // ###
-    //    Throw a SyntaxError exception if the following conditions are all true:
-    //    Type(lhs) is Reference is true
-    //    IsStrictReference(lhs) is true
-    //    Type(GetBase(lhs)) is Environment Record
-    //    GetReferencedName(lhs) is either "eval" or "arguments"
-
     Result expr = expression(ast->base);
+    throwSyntaxErrorOnEvalOrArgumentsInStrictMode(*expr, ast->incrementToken);
     if (_expr.accept(nx)) {
         move(*expr, unop(IR::OpIncrement, *expr));
     } else {
@@ -1574,14 +1557,8 @@ bool Codegen::visit(PostIncrementExpression *ast)
 
 bool Codegen::visit(PreDecrementExpression *ast)
 {
-    // ###
-    //    Throw a SyntaxError exception if the following conditions are all true:
-    //    Type(lhs) is Reference is true
-    //    IsStrictReference(lhs) is true
-    //    Type(GetBase(lhs)) is Environment Record
-    //    GetReferencedName(lhs) is either "eval" or "arguments"
-
     Result expr = expression(ast->expression);
+    throwSyntaxErrorOnEvalOrArgumentsInStrictMode(*expr, ast->decrementToken);
     move(*expr, unop(IR::OpDecrement, *expr));
     if (_expr.accept(nx)) {
         // nothing to do
@@ -1593,14 +1570,8 @@ bool Codegen::visit(PreDecrementExpression *ast)
 
 bool Codegen::visit(PreIncrementExpression *ast)
 {
-    // ###
-    //    Throw a SyntaxError exception if the following conditions are all true:
-    //    Type(lhs) is Reference is true
-    //    IsStrictReference(lhs) is true
-    //    Type(GetBase(lhs)) is Environment Record
-    //    GetReferencedName(lhs) is either "eval" or "arguments"
-
     Result expr = expression(ast->expression);
+    throwSyntaxErrorOnEvalOrArgumentsInStrictMode(*expr, ast->incrementToken);
     move(*expr, unop(IR::OpIncrement, *expr));
     if (_expr.accept(nx)) {
         // nothing to do
@@ -2386,8 +2357,8 @@ bool Codegen::visit(ThrowStatement *ast)
 bool Codegen::visit(TryStatement *ast)
 {
     if (_function->isStrict && ast->catchExpression &&
-        (ast->catchExpression->name == QLatin1String("eval") || ast->catchExpression->name == QLatin1String("arguments")))
-            throwSyntaxError(ast->catchExpression->identifierToken, QCoreApplication::translate("qv4codegen", "Catch variable name may not be eval or arguments in strict mode"));
+            (ast->catchExpression->name == QLatin1String("eval") || ast->catchExpression->name == QLatin1String("arguments")))
+        throwSyntaxError(ast->catchExpression->identifierToken, QCoreApplication::translate("qv4codegen", "Catch variable name may not be eval or arguments in strict mode"));
 
     IR::BasicBlock *tryBody = _function->newBasicBlock();
     IR::BasicBlock *catchBody = ast->catchExpression ?  _function->newBasicBlock() : 0;
@@ -2592,6 +2563,17 @@ bool Codegen::visit(UiSourceElement *)
     return false;
 }
 
+void Codegen::throwSyntaxErrorOnEvalOrArgumentsInStrictMode(IR::Expr *expr, const SourceLocation& loc)
+{
+    if (!_env->isStrict)
+        return;
+    IR::Name *n = expr->asName();
+    if (!n)
+        return;
+    if (*n->id == QLatin1String("eval") || *n->id == QLatin1String("arguments"))
+        throwSyntaxError(loc, QCoreApplication::translate("qv4codegen", "Variable name may not be eval or arguments in strict mode"));
+}
+
 void Codegen::throwSyntaxError(const SourceLocation &loc, const QString &detail)
 {
     VM::DiagnosticMessage *msg = new VM::DiagnosticMessage;
index ffb9f90..89953c8 100644 (file)
@@ -384,6 +384,8 @@ protected:
     virtual bool visit(AST::UiScriptBinding *ast);
     virtual bool visit(AST::UiSourceElement *ast);
 
+    void throwSyntaxErrorOnEvalOrArgumentsInStrictMode(IR::Expr* expr, const AST::SourceLocation &loc);
+
     void throwSyntaxError(const AST::SourceLocation &loc, const QString &detail);
     void throwReferenceError(const AST::SourceLocation &loc, const QString &detail);
 
index 47572a6..b90a9f1 100644 (file)
@@ -22,15 +22,11 @@ S11.2.1_A3_T2 failing
 S11.2.1_A3_T3 failing
 S11.2.1_A4_T3 failing
 11.2.3-3_3 failing
-11.3.1-2-1-s failing
-11.3.1-2-2-s failing
 S11.3.1_A2.2_T1 failing
 S11.3.1_A4_T1 failing
 S11.3.1_A4_T2 failing
 S11.3.1_A4_T3 failing
 S11.3.1_A4_T4 failing
-11.3.2-2-1-s failing
-11.3.2-2-2-s failing
 S11.3.2_A2.2_T1 failing
 S11.3.2_A4_T1 failing
 S11.3.2_A4_T2 failing
@@ -47,10 +43,6 @@ S11.3.2_A4_T4 failing
 11.4.1-5-2 failing
 S11.4.1_A1 failing
 S11.4.1_A2.1 failing
-11.4.4-2-1-s failing
-11.4.4-2-2-s failing
-11.4.5-2-1-s failing
-11.4.5-2-2-s failing
 S11.5.3_A4_T2 failing
 S11.8.6_A3 failing
 S11.8.6_A5_T2 failing