[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [qmlweb/development/qmlweb2] /: [Compiler] Implement object literals
From:       Jan Marker <jan () jangmarker ! de>
Date:       2015-09-30 20:44:33
Message-ID: E1ZhOEr-0000CB-Im () scm ! kde ! org
[Download RAW message or body]

Git commit cced0e2387398165b7d909b876542680b24d69de by Jan Marker.
Committed on 30/09/2015 at 20:41.
Pushed by jangmarker into branch 'development/qmlweb2'.

[Compiler] Implement object literals

M  +31   -0    src/qmljsc/purejavascriptgenerator.cpp
M  +5    -0    src/qmljsc/purejavascriptgenerator.h
A  +1    -0    tests/auto/data/javascript/arrayliterals.compiled.js
A  +5    -0    tests/auto/data/javascript/arrayliterals.js
M  +1    -1    tests/auto/data/javascript/literals.compiled.js
M  +1    -6    tests/auto/data/javascript/literals.js
A  +1    -0    tests/auto/data/javascript/objectliterals.compiled.js
A  +3    -0    tests/auto/data/javascript/objectliterals.js
M  +28   -0    tests/auto/qmljsc/testpurejavascriptgenerator.cpp
M  +2    -0    tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp

http://commits.kde.org/qmlweb/cced0e2387398165b7d909b876542680b24d69de

diff --git a/src/qmljsc/purejavascriptgenerator.cpp \
b/src/qmljsc/purejavascriptgenerator.cpp index 5c4a399..9066f93 100644
--- a/src/qmljsc/purejavascriptgenerator.cpp
+++ b/src/qmljsc/purejavascriptgenerator.cpp
@@ -134,6 +134,11 @@ bool PureJavaScriptGenerator::visit(AST::IdentifierExpression \
*identifierExpress  return true;
 }
 
+bool PureJavaScriptGenerator::visit(AST::IdentifierPropertyName \
*identifierPropertyName) { +    m_outputStack << \
identifierPropertyName->id.toString(); +    return true;
+}
+
 bool PureJavaScriptGenerator::visit(AST::NullExpression *) {
     m_outputStack << "null";
     return true;
@@ -274,6 +279,11 @@ void PureJavaScriptGenerator::endVisit(AST::IfStatement \
*ifExpression) {  void PureJavaScriptGenerator::endVisit(AST::NumericLiteral *) {
 }
 
+void PureJavaScriptGenerator::endVisit(AST::ObjectLiteral *) {
+    const QString properties = m_outputStack.pop();
+    m_outputStack << '{' + properties + '}';
+}
+
 void PureJavaScriptGenerator::endVisit(AST::PostDecrementExpression *) {
     const QString expression = m_outputStack.pop();
     m_outputStack << expression + "--";
@@ -294,6 +304,27 @@ void \
PureJavaScriptGenerator::endVisit(AST::PreIncrementExpression *) {  m_outputStack << \
"++" + expression;  }
 
+void PureJavaScriptGenerator::endVisit(AST::PropertyAssignmentList *assignmentList) \
{ +    reduceListStack<AST::PropertyAssignmentList>(assignmentList, ",");
+}
+
+void PureJavaScriptGenerator::endVisit(AST::PropertyGetterSetter *getterSetter) {
+    const QString functionBody = \
(getterSetter->functionBody)?m_outputStack.pop():"{}"; +    const QString parameters \
= (getterSetter->formals)?m_outputStack.pop():""; +    const QString propertyName = \
m_outputStack.pop(); +    if (getterSetter->type == \
AST::PropertyGetterSetter::Getter) { +        m_outputStack << "get " + propertyName \
+ "()" + functionBody; +    } else {
+        m_outputStack << "set " + propertyName + '(' + parameters + ')' + \
functionBody; +    }
+}
+
+void PureJavaScriptGenerator::endVisit(AST::PropertyNameAndValue *) {
+    const QString expression = m_outputStack.pop();
+    const QString propertyName = m_outputStack.pop();
+    m_outputStack << propertyName + ':' + expression;
+}
+
 void PureJavaScriptGenerator::endVisit(AST::ReturnStatement *returnStatement) {
     const QString expression = (returnStatement->expression) ? ' \
'+m_outputStack.pop() : "";  m_outputStack << "return" + expression + ';';
diff --git a/src/qmljsc/purejavascriptgenerator.h \
b/src/qmljsc/purejavascriptgenerator.h index f1374d5..415bf62 100644
--- a/src/qmljsc/purejavascriptgenerator.h
+++ b/src/qmljsc/purejavascriptgenerator.h
@@ -41,6 +41,7 @@ public:
     virtual bool visit(QQmlJS::AST::FalseLiteral *) override;
     virtual bool visit(QQmlJS::AST::FormalParameterList *) override;
     virtual bool visit(QQmlJS::AST::IdentifierExpression *) override;
+    virtual bool visit(QQmlJS::AST::IdentifierPropertyName *) override;
     virtual bool visit(QQmlJS::AST::NullExpression *) override;
     virtual bool visit(QQmlJS::AST::NumericLiteral *) override;
     virtual bool visit(QQmlJS::AST::StringLiteral *) override;
@@ -62,10 +63,14 @@ public:
     virtual void endVisit(QQmlJS::AST::IdentifierExpression *) override;
     virtual void endVisit(QQmlJS::AST::IfStatement *) override;
     virtual void endVisit(QQmlJS::AST::NumericLiteral *) override;
+    virtual void endVisit(QQmlJS::AST::ObjectLiteral *) override;
     virtual void endVisit(QQmlJS::AST::PostDecrementExpression *) override;
     virtual void endVisit(QQmlJS::AST::PostIncrementExpression *) override;
     virtual void endVisit(QQmlJS::AST::PreDecrementExpression *) override;
     virtual void endVisit(QQmlJS::AST::PreIncrementExpression *) override;
+    virtual void endVisit(QQmlJS::AST::PropertyAssignmentList *) override;
+    virtual void endVisit(QQmlJS::AST::PropertyGetterSetter *) override;
+    virtual void endVisit(QQmlJS::AST::PropertyNameAndValue *) override;
     virtual void endVisit(QQmlJS::AST::ReturnStatement *) override;
     virtual void endVisit(QQmlJS::AST::SourceElements *) override;
     virtual void endVisit(QQmlJS::AST::StringLiteral *) override;
diff --git a/tests/auto/data/javascript/arrayliterals.compiled.js \
b/tests/auto/data/javascript/arrayliterals.compiled.js new file mode 100644
index 0000000..5763660
--- /dev/null
+++ b/tests/auto/data/javascript/arrayliterals.compiled.js
@@ -0,0 +1 @@
+[];[1,2,];[,,,5,];[5,,,5,];[5,];
\ No newline at end of file
diff --git a/tests/auto/data/javascript/arrayliterals.js \
b/tests/auto/data/javascript/arrayliterals.js new file mode 100644
index 0000000..6aa6e32
--- /dev/null
+++ b/tests/auto/data/javascript/arrayliterals.js
@@ -0,0 +1,5 @@
+[];
+[1, 2,];
+[,,,5,];
+[5,,,5,];
+[5];
\ No newline at end of file
diff --git a/tests/auto/data/javascript/literals.compiled.js \
b/tests/auto/data/javascript/literals.compiled.js index bcb5ddd..4a47b75 100644
--- a/tests/auto/data/javascript/literals.compiled.js
+++ b/tests/auto/data/javascript/literals.compiled.js
@@ -1 +1 @@
-this;null;identifier;true;false;4.1;"string";[];[1,2,];[,,,5,];[5,,,5,];[5,];
\ No newline at end of file
+this;null;identifier;true;false;4.1;"string";
\ No newline at end of file
diff --git a/tests/auto/data/javascript/literals.js \
b/tests/auto/data/javascript/literals.js index 7f07a67..a6a89c0 100644
--- a/tests/auto/data/javascript/literals.js
+++ b/tests/auto/data/javascript/literals.js
@@ -4,9 +4,4 @@ identifier;
 true;
 false;
 4.1;
-"string";
-[];
-[1, 2,];
-[,,,5,];
-[5,,,5,];
-[5];
\ No newline at end of file
+"string";
\ No newline at end of file
diff --git a/tests/auto/data/javascript/objectliterals.compiled.js \
b/tests/auto/data/javascript/objectliterals.compiled.js new file mode 100644
index 0000000..ddcf169
--- /dev/null
+++ b/tests/auto/data/javascript/objectliterals.compiled.js
@@ -0,0 +1 @@
+x={property1:"value"};x={property1:"value",property2:"value2"};x={property1:null,get \
property1(){},set property1(x){}}; \ No newline at end of file
diff --git a/tests/auto/data/javascript/objectliterals.js \
b/tests/auto/data/javascript/objectliterals.js new file mode 100644
index 0000000..030ad6a
--- /dev/null
+++ b/tests/auto/data/javascript/objectliterals.js
@@ -0,0 +1,3 @@
+x = {property1: "value"};
+x = {property1: "value", property2: "value2"};
+x = {property1: null, get property1(){}, set property1(x){}};
\ No newline at end of file
diff --git a/tests/auto/qmljsc/testpurejavascriptgenerator.cpp \
b/tests/auto/qmljsc/testpurejavascriptgenerator.cpp index 9282edc..9ba9676 100644
--- a/tests/auto/qmljsc/testpurejavascriptgenerator.cpp
+++ b/tests/auto/qmljsc/testpurejavascriptgenerator.cpp
@@ -102,6 +102,15 @@ public:
         , m_arrayLiteralWithoutElision(&m_arrayElementsExp)
         , m_arrayLiteralOnlyElision(&m_twoElisions)
         , m_arrayElementsExpElisionExpPart2(&m_arrayElementsExpElisionExp, \
&m_twoElisions, &m_trueLiteral) +        , \
m_identifierPropertyName(m_someIdentifierStringRef) +        , \
m_propertyNameAndValue(&m_identifierPropertyName, &m_falseLiteral) +        , \
m_propertyGetter(&m_identifierPropertyName, &m_functionBody) +        , \
m_propertyGetterEmptyBody(&m_identifierPropertyName, nullptr) +        , \
m_propertySetter(&m_identifierPropertyName, &m_twoParameters, &m_functionBody) +      \
, m_propertySetterEmptyBody(&m_identifierPropertyName, &m_twoParameters, nullptr) +   \
, m_twoProperties(&m_propertyNameAndValue) +        , \
m_twoPropertiesPart2(&m_twoProperties, &m_propertyNameAndValue) +        , \
m_objectLiteral(&m_twoProperties)  , m_equalsBinaryExpression(nullptr, \
QSOperator::Equal, nullptr)  , m_postDecrementExpression(&m_numericalExpressionPi)
         , m_postIncrementExpression(&m_numericalExpressionPi)
@@ -166,6 +175,7 @@ public:
         m_arrayElementsExpExpPart2.finish();
         m_arrayElementsElisionExp.finish();
         m_arrayElementsExpElisionExpPart2.finish();
+        m_twoPropertiesPart2.finish();
         m_statementListPart3.finish();
         m_sourceElementsListPart3.finish();
         m_parameterListPart2.finish();
@@ -210,6 +220,16 @@ private:
     AST::ArrayLiteral m_arrayLiteralWithoutElision;
     AST::ArrayLiteral m_arrayLiteralOnlyElision;
 
+    AST::IdentifierPropertyName m_identifierPropertyName;
+    AST::PropertyNameAndValue m_propertyNameAndValue;
+    AST::PropertyGetterSetter m_propertyGetter;
+    AST::PropertyGetterSetter m_propertyGetterEmptyBody;
+    AST::PropertyGetterSetter m_propertySetter;
+    AST::PropertyGetterSetter m_propertySetterEmptyBody;
+    AST::PropertyAssignmentList m_twoProperties;
+    AST::PropertyAssignmentList m_twoPropertiesPart2;
+    AST::ObjectLiteral m_objectLiteral;
+
     AST::BinaryExpression m_equalsBinaryExpression;
 
     AST::PostDecrementExpression m_postDecrementExpression;
@@ -321,6 +341,7 @@ private slots:
     TEST_VISIT_DEFAULT_IMPL_(FunctionBody                                            \
                , m_functionBody)
     TEST_VISIT_DEFAULT_IMPL_(FunctionDeclaration                                     \
                , m_functionDeclarationWithParameters)
     TEST_VISIT_PUTS_ON_STACK(IdentifierExpression   , AnyCase           , "i"        \
, m_identifierExpression) +    TEST_VISIT_PUTS_ON_STACK(IdentifierPropertyName , \
                AnyCase           , "i"               , m_identifierPropertyName)
     TEST_VISIT_DEFAULT_IMPL_(IfStatement                                             \
                , m_ifStatementWithoutElse)
     TEST_VISIT_PUTS_ON_STACK(NullExpression         , AnyCase           , "null"     \
                , m_nullExpression)
     TEST_VISIT_PUTS_ON_STACK(NumericLiteral         , Pi                , "3.14"     \
, m_numericalExpressionPi) @@ -365,10 +386,17 @@ private slots:
     TEST_ENDVISIT_REDUCES(IfStatement             , OnlyIf            , \
                "if(exp)stm;"     , ({"exp", "stm;"})            , \
                m_ifStatementWithoutElse)
     TEST_ENDVISIT_REDUCES(IfStatement             , IfElse            , \
                "if(exp)s;else s;", ({"exp", "s;", "s;"})        , \
                m_ifStatementWithElse)
     TEST_ENDVISIT_REDUCES(NumericLiteral          , AnyCase           , "2.7"        \
, ({"2.7"})                    , m_numericalExpressionPi) +    \
TEST_ENDVISIT_REDUCES(ObjectLiteral           , AnyCase           , "{properties}"    \
                , ({"properties"})             , m_objectLiteral)
     TEST_ENDVISIT_REDUCES(PostDecrementExpression , AnyCase           , "2.7--"      \
                , ({"2.7"})                    , m_postDecrementExpression)
     TEST_ENDVISIT_REDUCES(PostIncrementExpression , AnyCase           , "2.7++"      \
                , ({"2.7"})                    , m_postIncrementExpression)
     TEST_ENDVISIT_REDUCES(PreDecrementExpression  , AnyCase           , "--2.7"      \
                , ({"2.7"})                    , m_preDecrementExpression)
     TEST_ENDVISIT_REDUCES(PreIncrementExpression  , AnyCase           , "++2.7"      \
, ({"2.7"})                    , m_preIncrementExpression) +    \
TEST_ENDVISIT_REDUCES(PropertyAssignmentList  , TwoProperties     , "prop1,prop2"     \
, ({"prop1", "prop2"})         , m_twoProperties) +    \
TEST_ENDVISIT_REDUCES(PropertyGetterSetter    , Getter            , "get i(){body}"   \
, ({"i", "{body}"})            , m_propertyGetter) +    \
TEST_ENDVISIT_REDUCES(PropertyGetterSetter    , GetterEmptyBody   , "get i(){}"       \
, ({"i"})                      , m_propertyGetterEmptyBody) +    \
TEST_ENDVISIT_REDUCES(PropertyGetterSetter    , Setter            , "set \
i(param){body}", ({"i", "param", "{body}"}) , m_propertySetter) +    \
TEST_ENDVISIT_REDUCES(PropertyGetterSetter    , SetterEmptyBody   , "set i(param){}"  \
, ({"i", "param"})             , m_propertySetterEmptyBody) +    \
TEST_ENDVISIT_REDUCES(PropertyNameAndValue    , AnyCase           , "prop:expr"       \
                , ({"prop", "expr"})           , m_propertyNameAndValue)
     TEST_ENDVISIT_REDUCES(ReturnStatement         , WithoutReturnValue, "return;"    \
                , ({})                         , m_returnStatementWithoutValue)
     TEST_ENDVISIT_REDUCES(ReturnStatement         , WithReturnValue   , "return \
                true;"    , ({"true"})                   , \
                m_returnStatementWithValue)
     TEST_ENDVISIT_REDUCES(SourceElements          , ThreeSrcElements  , \
                "sEl1sEl2sEl3"    , ({"sEl1", "sEl2", "sEl3"})   , \
                m_threeSourceElementsList)
diff --git a/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp \
b/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp index \
                ba68e96..ea1729d 100644
--- a/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp
+++ b/tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp
@@ -88,6 +88,8 @@ private slots:
         addRowForFileWithCompiled("ifstatements");
         addRowForFileWithCompiled("switchstatements");
         addRowForFileWithCompiled("literals");
+        addRowForFileWithCompiled("arrayliterals");
+        addRowForFileWithCompiled("objectliterals");
     }
 
     void test_compileJavaScriptFile() {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic