Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJSEngine and SyntaxError ?
Forum Updated to NodeBB v4.3 + New Features

QJSEngine and SyntaxError ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 671 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I am using the QJSEngine, I added a global to the object:

        QJSValue objGlobal = pobjScriptEng->globalObject();
    

    pobjScriptEng is a pointer to an instance of QJSEngine.

    I have a map of globals, the map type definition:

        std::map<QString, QString> mpGlobals;
    

    I iterate through the globals map adding them to the engine:

        for( mpGlobals::iterator itr=rmpGlobals.begin(); itr!=rmpGlobals.end(); itr++ ) {
            QString strName(itr->first);
    
            if ( objGlobal.hasProperty(strName) == false ) {
                QString strData(itr->second);
                QJSValue result = pobjScriptEng->evaluate(strData);
                objGlobal.setProperty(strName, result);
            }
        }
    

    rmpGlobals is a reference to the globals map.

    I have a script which includes a reference to a global, the global is called "db" and is a JSON object that contains:

        {"db":"test","host":"localhost","usr":"root","pass":"123456"}
    

    I added some debug to the loop that calls setProperty and this is whats displayed in the Application Output:

        itr->first "db" 
        itr->second "{\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"resuocra\"}"
    

    The syntax error is coming from the JSON, but why there is nothing wrong with it. I dumped result.toString() to the console and it contains:

        SyntaxError: Expected token `,'
    

    The script:

        function test() {
        	try{
        		console.info("---------------");
        		console.info("test(), Line 4");
        
        		if ( db === undefined ) {
        			console.info("db is undefined");
        			return;
        		}
        		if ( typeof db === "object" ) {
        			var mbr;
        			console.info("test(), Line 7");
        			console.info(db);
        			console.info("test(), Line 9");
        			
        			for( mbr in db ) {
        				console.info("test(), Line12");
        				console.info(mbr);
        				console.info("test(), Line14");
        			}
        			console.info("test(), Line 14");			
        		}
        		console.info("test(), Line 16");		
        		console.info("---------------");
        	} catch( e ) {
        		console.warn( "test() WARNING: " + e );
        	}
        }
    

    When run my application and the script is evaluated I see the following in the "Application Output"

        2020-04-08 08:21:36.320693+0100 XMLMPAM[3657:59571] [js] ---------------
        2020-04-08 08:21:36.320732+0100 XMLMPAM[3657:59571] [js] test(), Line 4
        2020-04-08 08:21:36.320747+0100 XMLMPAM[3657:59571] [js] test(), Line 7
        2020-04-08 08:21:36.320762+0100 XMLMPAM[3657:59571] [js] SyntaxError: Expected token `,'
        2020-04-08 08:21:36.320769+0100 XMLMPAM[3657:59571] [js] test(), Line 9
        2020-04-08 08:21:36.320790+0100 XMLMPAM[3657:59571] [js] test(), Line 14
        2020-04-08 08:21:36.320798+0100 XMLMPAM[3657:59571] [js] test(), Line 16
        2020-04-08 08:21:36.320804+0100 XMLMPAM[3657:59571] [js] ---------------
    

    Ignore everything before the [js] thats my time stamp and debug information, after the [js] is all the console output.

    What is the:

        SyntaxError: Expected token `,'
    

    I can see nothing wrong in the global or the script.

    If I modify the script and insert:

        var db =  {"db":"test","host":"localhost","usr":"root","pass":"123456"};
    

    As the first line, the syntax error is not displayed and everything is ok, so what is wrong with global added using setProperty ?

    Here is the code that adds a global to the map:

        void clsXMLnode::addGlobal(QString strGlobal) {
            QStringList slstGlobal = strGlobal.split(clsXMLnode::msccGlobalDelimiter);
    
            if ( slstGlobal.length() == clsXMLnode::mscintAssignmentParts ) {
                QString strName(slstGlobal[clsXMLnode::mscintGlobalName].trimmed())
               ,strValue(slstGlobal[clsXMLnode::mscintGlobalValue].trimmed());
                msmpGlobals.insert(std::make_pair(strName, strValue));
            }
        }
    

    Some definitions:

        clsXMLnode::msccGlobalDelimiter        is "="
        clsXMLnode::mscintAssignmentParts      is 2
        clsXMLnode::mscintGlobalName           is 0
        clsXMLnode::mscintGlobalValue          is 1
    

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #2

      The solution was to wrap the JSON in (), this was not required in earlier versions and I fail to see why its required now, but it has solved the issue.

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @SPlatten said in QJSEngine and SyntaxError ?:

        The solution was to wrap the JSON in ()

        Where are you doing this? On the c++ side or the qml side? I seem to remember some weirdness with something like this, but I honestly don't remember where the problem was.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #4

          In the C++, I'm not using QML in my application.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @fcarney, can you help with this problem? https://forum.qt.io/topic/113478/qjsengine-error-2-problem-after-qt-update/2

            Kind Regards,
            Sy

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved