QJSEngine,全局和脚本,是什么导致语法错误

QJSEngine, global and script, what is causing the syntax error

我正在使用QJSEngine,我添加了一个全局对象:

QJSValue objGlobal = pobjScriptEng->globalObject();

pobjScriptEng 是指向 QJSEngine.

实例的指针

我有一个全局映射,映射类型定义:

std::map<QString, QString> mpGlobals;

我遍历全局映射,将它们添加到引擎中:

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 是对全局映射的引用。

我有一个脚本,其中包含对全局的引用,全局称为 db 并且是一个 JSON 对象,其中包含:

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

我在调用 setProperty 的循环中添加了一些调试日志,这就是应用程序输出中显示的内容:

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

语法错误来自JSON,但为什么它没有错。我将 result.toString() 转储到控制台,它包含:

SyntaxError: Expected token `,'

脚本:

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 );
    }
}

当 运行 我的应用程序和脚本被评估时,我在 "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] ---------------

忽略[js]之前的所有内容,这是我的时间戳和调试信息,[js]之后是所有控制台输出。

什么是:

SyntaxError: Expected token `,'

我在全局或脚本中看不出有什么问题。

如果我修改脚本并插入:

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

作为第一行,没有显示语法错误,一切正常,那么global added using setProperty有什么问题吗?

这是将全局添加到地图的代码:

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));
    }
}

一些定义:

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

在这些行中,您正在设置 globalObject:

的属性
QJSValue result = pobjScriptEng->evaluate( strData );
objGlobal.setProperty( strName, result );

异常:

SyntaxError: Expected token `,'

是因为配置JSON缺少括号()所以评估不成功。您需要使用 result.

QJSValue::isError() 方法进行检查

示例(参见 documentation):

QJSValue result = pobjScriptEng->evaluate( strData );
if ( result.isError() )
{
    qDebug() << "Uncaught exception at line"
             << result.property("lineNumber").toInt()
             << ":" << result.toString();
    return -1;
}

一旦正确封装,JSON 配置将被成功评估,它应该可以工作。

这是一个完整的工作示例,其中 raw string literals 用于 JSON 和 JavaScript 代码:

#include <QCoreApplication>
#include <QJSEngine>
#include <QJSValue>
#include <QDebug>

int main( int argc, char** argv )
{
    QCoreApplication app{ argc, argv };

    const auto raw_config =
    R"json((
    {
        "db": "test",
        "host": "localhost",
        "usr": "root",
        "pass": "123456"
    }
    ))json";

    QJSEngine engine;
    engine.installExtensions( QJSEngine::ConsoleExtension );

    const auto json_config = engine.evaluate( raw_config );
    if ( json_config.isError() )
    {
        qDebug() << "Uncaught exception at line"
                 << json_config.property("lineNumber").toInt()
                 << ":" << json_config.toString();
        return -1;
    }

    engine.globalObject().setProperty( "db", json_config );

    const auto test_script =
    R"javascript((
    function test() {
        try{
            console.info("---------------");

            if ( db === undefined ) {
                console.info("db is undefined");
                return;
            }
            else if ( typeof db === "object" ) {
                for( k in db ) {
                    console.info( k + ": " + db[k] );
                }
            }
            console.info("---------------");
        } catch( e ) {
            console.warn( "test() WARNING: " + e );
        }
    }
    ))javascript";

    auto test_func = engine.evaluate( test_script );
    if ( test_func.isError() )
    {
        qDebug() << "Uncaught exception at line"
                 << test_func.property("lineNumber").toInt()
                 << ":" << test_func.toString();
        return -1;
    }

    const auto result = test_func.call();
    if ( result.isError() )
    {
        qDebug() << "Uncaught exception at line"
                 << result.property("lineNumber").toInt()
                 << ":" << result.toString();
        return -1;
    }

    return 0;
}

输出:

js: ---------------
js: db: test
js: host: localhost
js: usr: root
js: pass: 123456
js: ---------------

关于分组运算符 () 语法的相关 JavaScript 特定线程:

  • What do parentheses surrounding an object/function/class declaration mean?