diff options
author | Simon Hausmann <simon.hausmann@digia.com> | 2013-08-22 09:24:19 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-08-23 08:00:59 +0200 |
commit | 1708b36910984b116b12432e7d27f8fbb25fadfa (patch) | |
tree | 903794919f2b14fd66842c72aa5fa756d093a295 | |
parent | fac5337abfef36c631fe73152157446aae0ea3ea (diff) |
Allow for function declarations inside conditionals
This is strictly speaking a regression from 5.1/v8, which allows for that
as real world JavaScript appears to require it.
Task-number: QTBUG-33064
Change-Id: Iceaca84373f12fb08459ed007afb25b5a705fa31
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
-rw-r--r-- | src/qml/compiler/qv4codegen.cpp | 27 | ||||
-rw-r--r-- | src/qml/compiler/qv4codegen_p.h | 5 | ||||
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 18 | ||||
-rw-r--r-- | tests/manual/v4/TestExpectations | 8 |
4 files changed, 26 insertions, 32 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp index 4f9815826b..b3eba3c84b 100644 --- a/src/qml/compiler/qv4codegen.cpp +++ b/src/qml/compiler/qv4codegen.cpp @@ -71,7 +71,6 @@ Codegen::ScanFunctions::ScanFunctions(Codegen *cg, const QString &sourceCode) : _cg(cg) , _sourceCode(sourceCode) , _env(0) - , _inFuncBody(false) , _allowFuncDecls(true) { } @@ -298,13 +297,6 @@ void Codegen::ScanFunctions::endVisit(FunctionDeclaration *) leaveEnvironment(); } -bool Codegen::ScanFunctions::visit(FunctionBody *ast) -{ - TemporaryBoolAssignment inFuncBody(_inFuncBody, true); - Node::accept(ast->elements, this); - return false; -} - bool Codegen::ScanFunctions::visit(WithStatement *ast) { if (_env->isStrict) { @@ -315,25 +307,6 @@ bool Codegen::ScanFunctions::visit(WithStatement *ast) return true; } -bool Codegen::ScanFunctions::visit(IfStatement *ast) { - Node::accept(ast->expression, this); - - TemporaryBoolAssignment allowFuncDecls(_allowFuncDecls, !_inFuncBody); - Node::accept(ast->ok, this); - Node::accept(ast->ko, this); - - return false; -} - -bool Codegen::ScanFunctions::visit(WhileStatement *ast) { - Node::accept(ast->expression, this); - - TemporaryBoolAssignment allowFuncDecls(_allowFuncDecls, !_inFuncBody); - Node::accept(ast->statement, this); - - return false; -} - bool Codegen::ScanFunctions::visit(DoWhileStatement *ast) { { TemporaryBoolAssignment allowFuncDecls(_allowFuncDecls, !_env->isStrict); diff --git a/src/qml/compiler/qv4codegen_p.h b/src/qml/compiler/qv4codegen_p.h index 075371b273..2d2ca55f8d 100644 --- a/src/qml/compiler/qv4codegen_p.h +++ b/src/qml/compiler/qv4codegen_p.h @@ -477,12 +477,8 @@ protected: virtual bool visit(AST::FunctionDeclaration *ast); virtual void endVisit(AST::FunctionDeclaration *); - virtual bool visit(AST::FunctionBody *ast); - virtual bool visit(AST::WithStatement *ast); - virtual bool visit(AST::IfStatement *ast); - virtual bool visit(AST::WhileStatement *ast); virtual bool visit(AST::DoWhileStatement *ast); virtual bool visit(AST::ForStatement *ast); virtual bool visit(AST::LocalForStatement *ast); @@ -500,7 +496,6 @@ protected: Environment *_env; QStack<Environment *> _envStack; - bool _inFuncBody; bool _allowFuncDecls; }; diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index b69f57b339..ae9ac6602c 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -158,6 +158,8 @@ private slots: void dateConversionQtJS(); void functionPrototypeExtensions(); void threadedEngine(); + + void functionDeclarationsInConditionals(); }; tst_QJSEngine::tst_QJSEngine() @@ -2679,6 +2681,22 @@ void tst_QJSEngine::threadedEngine() QCOMPARE(thread2.result, 2); } +void tst_QJSEngine::functionDeclarationsInConditionals() +{ + // Even though this is bad practice (and test262 covers it with best practices test cases), + // we do allow for function declarations in if and while statements, as unfortunately that's + // real world JavaScript. (QTBUG-33064 for example) + QJSEngine eng; + QJSValue result = eng.evaluate("if (true) {\n" + " function blah() { return false; }\n" + "} else {\n" + " function blah() { return true; }\n" + "}\n" + "blah();"); + QVERIFY(result.isBool()); + QCOMPARE(result.toBool(), true); +} + QTEST_MAIN(tst_QJSEngine) #include "tst_qjsengine.moc" diff --git a/tests/manual/v4/TestExpectations b/tests/manual/v4/TestExpectations index 8af484277f..5bf5dbb6c7 100644 --- a/tests/manual/v4/TestExpectations +++ b/tests/manual/v4/TestExpectations @@ -13,3 +13,11 @@ S15.4.4.3_A1_T1 failing S15.4.4.3_A3_T1 failing S15.5.4.11_A5_T1 failing S15.2.4.4_A14 failing + +# Function declarations in conditionals. We allow them, because the real +# world requires them. +Sbp_12.5_A9_T3 failing +Sbp_12.6.1_A13_T3 failing +Sbp_12.6.2_A13_T3 failing +Sbp_12.6.4_A13_T3 failing + |