Git commit 7c39c498e7ef86b0a61b1cd9dcfdf22fc65b9440 by Jan Marker. Committed on 30/09/2015 at 20:41. Pushed by jangmarker into branch 'development/qmlweb2'. [Compiler] Add try-catch-finally M +24 -1 src/qmljsc/purejavascriptgenerator.cpp M +4 -0 src/qmljsc/purejavascriptgenerator.h A +1 -0 tests/auto/data/javascript/exceptionstatements.compiled.js A +27 -0 tests/auto/data/javascript/exceptionstatements.js M +21 -0 tests/auto/qmljsc/testpurejavascriptgenerator.cpp M +1 -0 tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp http://commits.kde.org/qmlweb/7c39c498e7ef86b0a61b1cd9dcfdf22fc65b9440 diff --git a/src/qmljsc/purejavascriptgenerator.cpp b/src/qmljsc/purejavasc= riptgenerator.cpp index 811ba84..4872158 100644 --- a/src/qmljsc/purejavascriptgenerator.cpp +++ b/src/qmljsc/purejavascriptgenerator.cpp @@ -258,6 +258,12 @@ void PureJavaScriptGenerator::endVisit(AST::CaseClause= s *caseClauses) { reduceListStack(caseClauses); } = +void PureJavaScriptGenerator::endVisit(AST::Catch *catchClause) { + const QString block =3D m_outputStack.pop(); + const QString variableIdentifier =3D catchClause->name.toString(); + m_outputStack << "catch(" + variableIdentifier + ')' + block; +} + void PureJavaScriptGenerator::endVisit(AST::ConditionalExpression *) { const QString secondExpression =3D m_outputStack.pop(); const QString firstExpression =3D m_outputStack.pop(); @@ -325,6 +331,11 @@ void PureJavaScriptGenerator::endVisit(AST::FieldMembe= rExpression *fieldMemberEx m_outputStack << objectExpression + '.' + memberName; } = +void PureJavaScriptGenerator::endVisit(AST::Finally *) { + const QString block =3D m_outputStack.pop(); + m_outputStack << "finally" + block; +} + void PureJavaScriptGenerator::endVisit(AST::ForEachStatement *) { const QString statement =3D m_outputStack.pop(); const QString objectExpression =3D m_outputStack.pop(); @@ -352,7 +363,7 @@ void PureJavaScriptGenerator::endVisit(AST::FunctionDec= laration *functionDeclara m_outputStack << "function " + name + '(' + parameters + ')' + body; } = -void PureJavaScriptGenerator::endVisit(QQmlJS::AST::FunctionExpression *fu= nctionExpression) { +void PureJavaScriptGenerator::endVisit(AST::FunctionExpression *functionEx= pression) { const QString body =3D (functionExpression->body)?m_outputStack.pop():= "{}"; const QString parameters =3D (functionExpression->formals)?m_outputSta= ck.pop():""; const QString name =3D functionExpression->name.toString(); @@ -486,11 +497,23 @@ void PureJavaScriptGenerator::endVisit(AST::SwitchSta= tement *) { m_outputStack << "switch(" + expression + ')' + clauseBlock; } = +void PureJavaScriptGenerator::endVisit(AST::ThrowStatement *) { + const QString expression =3D m_outputStack.pop(); + m_outputStack << "throw " + expression + ';'; +} + void PureJavaScriptGenerator::endVisit(AST::TildeExpression *) { const QString expression =3D m_outputStack.pop(); m_outputStack << '~' + expression; } = +void PureJavaScriptGenerator::endVisit(AST::TryStatement *tryStatement) { + const QString finallyExpression =3D (tryStatement->finallyExpression)?= m_outputStack.pop():""; + const QString catchExpression =3D (tryStatement->catchExpression)?m_ou= tputStack.pop():""; + const QString block =3D m_outputStack.pop(); + m_outputStack << "try" + block + catchExpression + finallyExpression; +} + void PureJavaScriptGenerator::endVisit(AST::TypeOfExpression *) { const QString expression =3D m_outputStack.pop(); m_outputStack << "typeof " + expression; diff --git a/src/qmljsc/purejavascriptgenerator.h b/src/qmljsc/purejavascri= ptgenerator.h index 9994b62..584e59c 100644 --- a/src/qmljsc/purejavascriptgenerator.h +++ b/src/qmljsc/purejavascriptgenerator.h @@ -60,6 +60,7 @@ public: virtual void endVisit(QQmlJS::AST::CaseBlock *) override; virtual void endVisit(QQmlJS::AST::CaseClause *) override; virtual void endVisit(QQmlJS::AST::CaseClauses *) override; + virtual void endVisit(QQmlJS::AST::Catch *) override; virtual void endVisit(QQmlJS::AST::ConditionalExpression *) override; virtual void endVisit(QQmlJS::AST::DefaultClause *) override; virtual void endVisit(QQmlJS::AST::DeleteExpression *) override; @@ -69,6 +70,7 @@ public: virtual void endVisit(QQmlJS::AST::Expression *) override; virtual void endVisit(QQmlJS::AST::ExpressionStatement *) override; virtual void endVisit(QQmlJS::AST::FieldMemberExpression *) override; + virtual void endVisit(QQmlJS::AST::Finally *) override; virtual void endVisit(QQmlJS::AST::ForEachStatement *) override; virtual void endVisit(QQmlJS::AST::ForStatement *) override; virtual void endVisit(QQmlJS::AST::FunctionBody *) override; @@ -97,7 +99,9 @@ public: virtual void endVisit(QQmlJS::AST::StringLiteral *) override; virtual void endVisit(QQmlJS::AST::StatementList *) override; virtual void endVisit(QQmlJS::AST::SwitchStatement *) override; + virtual void endVisit(QQmlJS::AST::ThrowStatement *) override; virtual void endVisit(QQmlJS::AST::TildeExpression *) override; + virtual void endVisit(QQmlJS::AST::TryStatement *) override; virtual void endVisit(QQmlJS::AST::TypeOfExpression *) override; virtual void endVisit(QQmlJS::AST::UnaryMinusExpression *) override; virtual void endVisit(QQmlJS::AST::UnaryPlusExpression *) override; diff --git a/tests/auto/data/javascript/exceptionstatements.compiled.js b/t= ests/auto/data/javascript/exceptionstatements.compiled.js new file mode 100644 index 0000000..42e6ed1 --- /dev/null +++ b/tests/auto/data/javascript/exceptionstatements.compiled.js @@ -0,0 +1 @@ +throw 42;try{var i;}catch(e){var e;}try{var i;}catch(e){var e;}try{var i;}= catch(e){var e;}finally{var f;}try{var i;}finally{var f;} \ No newline at end of file diff --git a/tests/auto/data/javascript/exceptionstatements.js b/tests/auto= /data/javascript/exceptionstatements.js new file mode 100644 index 0000000..ce850cc --- /dev/null +++ b/tests/auto/data/javascript/exceptionstatements.js @@ -0,0 +1,27 @@ +throw 42; + +try { + var i; +} catch (e) { + var e; +} + +try { + var i; +} catch (e) { + var e; +} + +try { + var i; +} catch (e) { + var e; +} finally { + var f; +} + +try { + var i; +} finally { + var f; +} \ No newline at end of file diff --git a/tests/auto/qmljsc/testpurejavascriptgenerator.cpp b/tests/auto= /qmljsc/testpurejavascriptgenerator.cpp index 164b5f4..74a0200 100644 --- a/tests/auto/qmljsc/testpurejavascriptgenerator.cpp +++ b/tests/auto/qmljsc/testpurejavascriptgenerator.cpp @@ -212,6 +212,13 @@ public: , m_localForStatementNoPart(nullptr, nullptr, nullptr, &m_block) , m_forEachStatement(&m_trueLiteral, &m_trueLiteral, &m_block) , m_localForEachStatement(&m_variableDeclarationWithoutAssignment,= &m_trueLiteral, &m_block) + /* Exceptions */ + , m_throwStatement(&m_trueLiteral) + , m_catch(m_someIdentifierStringRef, &m_block) + , m_finally(&m_block) + , m_tryStatementCatchFinally(&m_block, &m_catch, &m_finally) + , m_tryStatementCatch(&m_block, &m_catch) + , m_tryStatementFinally(&m_block, &m_finally) { m_elisionsPart2.finish(); m_arrayElementsExp.finish(); @@ -388,6 +395,14 @@ private: AST::ForEachStatement m_forEachStatement; AST::LocalForEachStatement m_localForEachStatement; = + /* Exceptions */ + AST::ThrowStatement m_throwStatement; + AST::Catch m_catch; + AST::Finally m_finally; + AST::TryStatement m_tryStatementCatchFinally; + AST::TryStatement m_tryStatementCatch; + AST::TryStatement m_tryStatementFinally; + private slots: void init() { m_generator =3D new PureJavaScriptGenerator(); @@ -473,6 +488,7 @@ private slots: TEST_ENDVISIT_REDUCES(CaseBlock , CasesDefaultCases , "{= cases;default;cases;}", ({"cases;", "default;", "cases;"}), m_caseBlockCase= sDefaultCases) TEST_ENDVISIT_REDUCES(CaseClause , CaseWithStatement , "c= ase exp:stm;" , ({"case", "exp", "stm;"}) , m_caseClause) TEST_ENDVISIT_REDUCES(CaseClauses , TwoClauses , "c= ase e:s;case e2:s2;", ({"case e:s;", "case e2:s2;"}), m_twoCaseClauses) + TEST_ENDVISIT_REDUCES(Catch , AnyCase , "c= atch(i){block}" , ({"{block}"}) , m_catch) TEST_ENDVISIT_REDUCES(ConditionalExpression , AnyCase , "c= ondition?e1:e2" , ({"condition", "e1", "e2"}) , m_conditionalExpression) TEST_ENDVISIT_REDUCES(DefaultClause , AnyCase , "d= efault:stm" , ({"default", "stm"}) , m_defaultClause) TEST_ENDVISIT_REDUCES(DeleteExpression , AnyCase , "d= elete v" , ({"v"}) , m_deleteExpression) @@ -485,6 +501,7 @@ private slots: TEST_ENDVISIT_REDUCES(/*Comma*/Expression , AnyCase , "a= ,b" , ({"a", "b"}) , m_commaExpression) TEST_ENDVISIT_REDUCES(ExpressionStatement , AnyCase , "e= xpression;" , ({"expression"}) , m_expressionStatement) TEST_ENDVISIT_REDUCES(FieldMemberExpression , AnyCase , "o= bj.property" , ({"obj"}) , m_fieldMemberExpression) + TEST_ENDVISIT_REDUCES(Finally , AnyCase , "f= inally{block}" , ({"{block}"}) , m_finally) TEST_ENDVISIT_REDUCES(FormalParameterList , AnyCase , "i= " , ({"i"}) , m_twoParameters) // does n= othing TEST_ENDVISIT_REDUCES(ForEachStatement , AnyCase , "f= or(i in o)stm;" , ({"i", "o", "stm;"}) , m_forEachStatement) TEST_ENDVISIT_REDUCES(ForStatement , AllParts , "f= or(i;c;++)stm;" , ({"i", "c", "++", "stm;"}) , m_forStatementAllParts) @@ -528,7 +545,11 @@ private slots: TEST_ENDVISIT_REDUCES(StatementSourceElement , AnyCase , "i= " , ({"i"}) , m_statementSourceElement) = // does nothing TEST_ENDVISIT_REDUCES(StringLiteral , AnyCase , "a= nother string" , ({"another string"}) , m_stringLiteral) TEST_ENDVISIT_REDUCES(SwitchStatement , AnyCase , "s= witch(e){blk}" , ({"e", "{blk}"}) , m_switchStatement) + TEST_ENDVISIT_REDUCES(ThrowStatement , AnyCase , "t= hrow 5;" , ({"5"}) , m_throwStatement) TEST_ENDVISIT_REDUCES(TildeExpression , AnyCase , "~= 5" , ({"5"}) , m_tildeExpression) + TEST_ENDVISIT_REDUCES(TryStatement , OnlyCatch , "t= ry{stm}catch" , ({"{stm}", "catch"}) , m_tryStatementCatch) + TEST_ENDVISIT_REDUCES(TryStatement , OnlyFinally , "t= ry{stm}finally" , ({"{stm}", "finally"}) , m_tryStatementFinally) + TEST_ENDVISIT_REDUCES(TryStatement , CatchFinally , "t= ry{stm}catchfinally", ({"{stm}", "catch", "finally"}), m_tryStatementCatchF= inally) TEST_ENDVISIT_REDUCES(TypeOfExpression , AnyCase , "t= ypeof v" , ({"v"}) , m_typeOfExpression) TEST_ENDVISIT_REDUCES(UnaryMinusExpression , AnyCase , "-= 5" , ({"5"}) , m_unaryMinusExpression) TEST_ENDVISIT_REDUCES(UnaryPlusExpression , AnyCase , "+= 5" , ({"5"}) , m_unaryPlusExpression) diff --git a/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp = b/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp index 606e556..45f73d8 100644 --- a/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp +++ b/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp @@ -94,6 +94,7 @@ private slots: addRowForFileWithCompiled("lefthandexpressions"); addRowForFileWithCompiled("functioncalls"); addRowForFileWithCompiled("loops"); + addRowForFileWithCompiled("exceptionstatements"); } = void test_compileJavaScriptFile() {