[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [qmlweb/development/qmlweb2] /: [Compiler] Add try-catch-finally
From: Jan Marker <jan () jangmarker ! de>
Date: 2015-09-30 20:44:36
Message-ID: E1ZhOEu-0000CB-FN () scm ! kde ! org
[Download RAW message or body]
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/purejavascriptgenerator.cpp index 811ba84..4872158 100644
--- a/src/qmljsc/purejavascriptgenerator.cpp
+++ b/src/qmljsc/purejavascriptgenerator.cpp
@@ -258,6 +258,12 @@ void PureJavaScriptGenerator::endVisit(AST::CaseClauses \
*caseClauses) { reduceListStack<AST::CaseClauses>(caseClauses);
}
+void PureJavaScriptGenerator::endVisit(AST::Catch *catchClause) {
+ const QString block = m_outputStack.pop();
+ const QString variableIdentifier = catchClause->name.toString();
+ m_outputStack << "catch(" + variableIdentifier + ')' + block;
+}
+
void PureJavaScriptGenerator::endVisit(AST::ConditionalExpression *) {
const QString secondExpression = m_outputStack.pop();
const QString firstExpression = m_outputStack.pop();
@@ -325,6 +331,11 @@ void \
PureJavaScriptGenerator::endVisit(AST::FieldMemberExpression *fieldMemberEx \
m_outputStack << objectExpression + '.' + memberName; }
+void PureJavaScriptGenerator::endVisit(AST::Finally *) {
+ const QString block = m_outputStack.pop();
+ m_outputStack << "finally" + block;
+}
+
void PureJavaScriptGenerator::endVisit(AST::ForEachStatement *) {
const QString statement = m_outputStack.pop();
const QString objectExpression = m_outputStack.pop();
@@ -352,7 +363,7 @@ void PureJavaScriptGenerator::endVisit(AST::FunctionDeclaration \
*functionDeclara m_outputStack << "function " + name + '(' + parameters + ')' + \
body; }
-void PureJavaScriptGenerator::endVisit(QQmlJS::AST::FunctionExpression \
*functionExpression) { +void \
PureJavaScriptGenerator::endVisit(AST::FunctionExpression \
*functionExpression) {
const QString body = (functionExpression->body)?m_outputStack.pop():"{}";
const QString parameters = (functionExpression->formals)?m_outputStack.pop():"";
const QString name = functionExpression->name.toString();
@@ -486,11 +497,23 @@ void PureJavaScriptGenerator::endVisit(AST::SwitchStatement *) \
{ m_outputStack << "switch(" + expression + ')' + clauseBlock;
}
+void PureJavaScriptGenerator::endVisit(AST::ThrowStatement *) {
+ const QString expression = m_outputStack.pop();
+ m_outputStack << "throw " + expression + ';';
+}
+
void PureJavaScriptGenerator::endVisit(AST::TildeExpression *) {
const QString expression = m_outputStack.pop();
m_outputStack << '~' + expression;
}
+void PureJavaScriptGenerator::endVisit(AST::TryStatement *tryStatement) {
+ const QString finallyExpression = \
(tryStatement->finallyExpression)?m_outputStack.pop():""; + const QString \
catchExpression = (tryStatement->catchExpression)?m_outputStack.pop():""; + const \
QString block = m_outputStack.pop(); + m_outputStack << "try" + block + \
catchExpression + finallyExpression; +}
+
void PureJavaScriptGenerator::endVisit(AST::TypeOfExpression *) {
const QString expression = m_outputStack.pop();
m_outputStack << "typeof " + expression;
diff --git a/src/qmljsc/purejavascriptgenerator.h \
b/src/qmljsc/purejavascriptgenerator.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/tests/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 = new PureJavaScriptGenerator();
@@ -473,6 +488,7 @@ private slots:
TEST_ENDVISIT_REDUCES(CaseBlock , CasesDefaultCases , \
"{cases;default;cases;}", ({"cases;", "default;", "cases;"}), \
m_caseBlockCasesDefaultCases)
TEST_ENDVISIT_REDUCES(CaseClause , CaseWithStatement , "case \
exp:stm;" , ({"case", "exp", "stm;"}) , m_caseClause)
TEST_ENDVISIT_REDUCES(CaseClauses , TwoClauses , "case \
e:s;case e2:s2;", ({"case e:s;", "case e2:s2;"}), m_twoCaseClauses) + \
TEST_ENDVISIT_REDUCES(Catch , AnyCase , "catch(i){block}" \
, ({"{block}"}) , m_catch)
TEST_ENDVISIT_REDUCES(ConditionalExpression , AnyCase , \
"condition?e1:e2" , ({"condition", "e1", "e2"}) , \
m_conditionalExpression)
TEST_ENDVISIT_REDUCES(DefaultClause , AnyCase , \
"default:stm" , ({"default", "stm"}) , m_defaultClause)
TEST_ENDVISIT_REDUCES(DeleteExpression , AnyCase , "delete 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 , \
"expression;" , ({"expression"}) , \
m_expressionStatement)
TEST_ENDVISIT_REDUCES(FieldMemberExpression , AnyCase , \
"obj.property" , ({"obj"}) , m_fieldMemberExpression) + \
TEST_ENDVISIT_REDUCES(Finally , AnyCase , "finally{block}" \
, ({"{block}"}) , m_finally)
TEST_ENDVISIT_REDUCES(FormalParameterList , AnyCase , "i" \
, ({"i"}) , m_twoParameters) // does nothing
TEST_ENDVISIT_REDUCES(ForEachStatement , AnyCase , "for(i in \
o)stm;" , ({"i", "o", "stm;"}) , m_forEachStatement)
TEST_ENDVISIT_REDUCES(ForStatement , AllParts , \
"for(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 , "another \
string" , ({"another string"}) , m_stringLiteral)
TEST_ENDVISIT_REDUCES(SwitchStatement , AnyCase , \
"switch(e){blk}" , ({"e", "{blk}"}) , m_switchStatement) + \
TEST_ENDVISIT_REDUCES(ThrowStatement , AnyCase , "throw 5;" \
, ({"5"}) , m_throwStatement)
TEST_ENDVISIT_REDUCES(TildeExpression , AnyCase , "~5" \
, ({"5"}) , m_tildeExpression) + \
TEST_ENDVISIT_REDUCES(TryStatement , OnlyCatch , "try{stm}catch" \
, ({"{stm}", "catch"}) , m_tryStatementCatch) + \
TEST_ENDVISIT_REDUCES(TryStatement , OnlyFinally , "try{stm}finally" \
, ({"{stm}", "finally"}) , m_tryStatementFinally) + \
TEST_ENDVISIT_REDUCES(TryStatement , CatchFinally , \
"try{stm}catchfinally", ({"{stm}", "catch", "finally"}), \
m_tryStatementCatchFinally)
TEST_ENDVISIT_REDUCES(TypeOfExpression , AnyCase , "typeof 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() {
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic