title: QJSEngine, global and script, what is causing the syntax error
body: <p>I am using the <a href="https://doc.qt.io/qt-5/qjsengine.html" rel="nofollow noreferrer">QJSEngine</a>, I added a global to the object:</p>
  
  <pre><code>QJSValue objGlobal = pobjScriptEng-&gt;globalObject();
  </code></pre>
  
  <p><code>pobjScriptEng</code> is a pointer to an instance of <code>QJSEngine</code>.</p>
  
  <p>I have a map of globals, the map type definition:</p>
  
  <pre><code>std::map&lt;QString, QString&gt; mpGlobals;
  </code></pre>
  
  <p>I iterate through the globals map adding them to the engine:</p>
  
  <pre><code>for( mpGlobals::iterator itr=rmpGlobals.begin(); itr!=rmpGlobals.end(); itr++ ) {
      QString strName(itr-&gt;first);
      if ( objGlobal.hasProperty(strName) == false ) {
          QString strData(itr-&gt;second);
          QJSValue result = pobjScriptEng-&gt;evaluate(strData);
          objGlobal.setProperty(strName, result);
      }
  }
  </code></pre>
  
  <p><code>rmpGlobals</code> is a reference to the globals map.</p>
  
  <p>I have a script which includes a reference to a global, the global is called <code>db</code> and is a JSON object that contains:</p>
  
  <pre><code>{"db":"test","host":"localhost","usr":"root","pass":"123456"}
  </code></pre>
  
  <p>I added some debug logs in the loop that calls <code>setProperty</code> and this is what's displayed in the Application Output:</p>
  
  <pre><code>itr-&gt;first "db" 
  itr-&gt;second "{\"db\":\"test\",\"host\":\"localhost\",\"usr\":\"root\",\"pass\":\"resuocra\"}"
  </code></pre>
  
  <p>The syntax error is coming from the JSON, but why there is nothing wrong with it. I dumped <code>result.toString()</code> to the console and it contains:</p>
  
  <blockquote>
    <p>SyntaxError: Expected token `,'</p>
  </blockquote>
  
  <p>The script:</p>
  
  <pre><code>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 );
      }
  }
  </code></pre>
  
  <p>When run my application and the script is evaluated I see the following in the "Application Output":</p>
  
  <pre><code>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] ---------------
  </code></pre>
  
  <p>Ignore everything before the <code>[js]</code> that's my timestamp and debug information, after the <code>[js]</code> is all the console output.</p>
  
  <p>What is the:</p>
  
  <blockquote>
    <p>SyntaxError: Expected token `,'</p>
  </blockquote>
  
  <p>I can see nothing wrong in the global or the script.</p>
  
  <p>If I modify the script and insert:</p>
  
  <pre><code>var db =  {"db":"test","host":"localhost","usr":"root","pass":"123456"};
  </code></pre>
  
  <p>As the first line, the syntax error is not displayed and everything is ok, so what is wrong with global added using <code>setProperty</code>?</p>
  
  <p>Here is the code that adds a global to the map:</p>
  
  <pre><code>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));
      }
  }
  </code></pre>
  
  <p>Some definitions:</p>
  
  <pre><code>clsXMLnode::msccGlobalDelimiter        is "="
  clsXMLnode::mscintAssignmentParts      is 2
  clsXMLnode::mscintGlobalName           is 0
  clsXMLnode::mscintGlobalValue          is 1
  </code></pre>