Javascriptfunction run(a, b){ return a+b;}
Javascriptfunction run(){ ui.show(); run(1,13);}
C++ (Qt)class EKERNELSHARED_EXPORT scriptengine : public QObject{ Q_OBJECTprivate: static DBEngine m_dbe; usermodules m_um; //QScriptEngine m_se; static QString m_configpath; static QScriptValue load(QScriptContext *context, QScriptEngine *engine);public: explicit scriptengine(QObject *parent = 0);public slots: void setConfigPath(const QString &); QString configPath(); static void loadModule(const QString &moduleName, const QString &functionName = QString("run"), const QScriptValueList arguments= QScriptValueList());};
C++ (Qt)scriptengine::scriptengine(QObject *parent) : QObject(parent){ configs cfg; m_configpath = cfg.readSetting("usermodules","ModulesPath").toString();} void scriptengine::loadModule(const QString &moduleName, const QString &functionName, const QScriptValueList arguments){ QWidget *dialog; QUiLoader loader; QScriptEngine *m_se=new QScriptEngine(); QFile file(m_configpath+"/"+moduleName+".ui"); if (!file.open(QFile::ReadOnly)){ return; } dialog = loader.load(&file); file.close(); file.setFileName(m_configpath+"/"+moduleName+".js"); if (!file.open(QFile::ReadOnly)){ return; } QTextStream filets(&file); QScriptEngineDebugger debugger; // debugger.attachTo(m_se); // debugger.action(QScriptEngineDebugger::InterruptAction)->trigger(); m_se->evaluate(filets.readAll(),moduleName); m_se->globalObject().setProperty("ui",m_se->newQObject(dialog)); m_se->globalObject().setProperty("db",m_se->newQObject(&m_dbe)); m_se->globalObject().setProperty ( "load", m_se->newFunction (scriptengine::load)); file.close(); QScriptValue fun = m_se->globalObject().property(functionName); if(fun.isFunction()){ qDebug()<< fun.call(QScriptValue(), QScriptValueList()<<arguments).toInteger(); } else{ qDebug()<<"undefined function"; }} void scriptengine::setConfigPath(const QString &configPath){ m_configpath = configPath;} QString scriptengine::configPath(){ return m_configpath;} QScriptValue scriptengine::load(QScriptContext *context, QScriptEngine *engine){ QScriptValueList args; for (int i=2;i<context->argumentCount();i++){ args<<context->argument(i); qDebug()<<context->argument(i).toString(); } loadModule(context->argument(0).toString(),context->argument(1).toString(),args); }
Javascriptfunction run(){ ui.show(); load("Script2","run1",1,13);}
Javafunction run1(a, b){ return a+b;}
QScriptValue::call() failed: cannot call function with argument created in a different engine0 ASSERT: "!vv->engine || vv->engine == this" in file c:\ndk_buildrepos\qt-desktop\src\script\api\/qscriptengine_p.h, line 656Invalid parameter passed to C runtime function.Invalid parameter passed to C runtime function.
Javascriptimport Script1 as scfunction run(){ ui.show(); sc.run1(1,4);}
bool loadFile(const QString& fileName, QScriptEngine *engine, QScriptValue& retValue){ QVariantMap loadedFiles = engine->property("_pg_loaded_files").value<QVariantMap>(); QString localFileName(fileName); QFileInfo fileInfo(localFileName); QString absoluteFileName = fileInfo.absoluteFilePath(); QString absolutePath = fileInfo.absolutePath(); QString canonicalFileName = fileInfo.canonicalFilePath(); if (loadedFiles.contains(canonicalFileName)) if (loadedFiles.value(canonicalFileName).toDateTime() == fileInfo.lastModified()) { return true; } loadedFiles.insert(canonicalFileName, fileInfo.lastModified()); engine->setProperty("_pg_loaded_files", loadedFiles); //QString path = fileInfo.path(); // load the file QFile file(fileInfo.absoluteFilePath()); if (file.open(QFile::ReadOnly)) { QTextStream stream(&file); QString contents = stream.readAll(); file.close(); int endlineIndex = contents.indexOf('\n'); QString line = contents.left(endlineIndex); int lineNumber = 1; // strip off #!/usr/bin/env qscript line if (line.startsWith("#!")) { contents.remove(0, endlineIndex+1); ++lineNumber; } // set qt.script.absoluteFilePath QScriptValue script = engine->globalObject().property("script"); QScriptValue oldFilePathValue = script.property("absoluteFilePath"); QScriptValue oldPathValue = script.property("absolutePath"); script.setProperty("absoluteFilePath", engine->toScriptValue(absoluteFileName)); script.setProperty("absolutePath", engine->toScriptValue(absolutePath)); retValue = engine->evaluate(contents, fileInfo.absoluteFilePath(), lineNumber); if (engine->hasUncaughtException()) { QStringList backtrace = engine->uncaughtExceptionBacktrace(); qWarning() << QString(" %1\n%2\n\n").arg(retValue.toString()).arg(backtrace.join("\n")); return true; } script.setProperty("absoluteFilePath", oldFilePathValue); // if we come from includeScript(), or whereever script.setProperty("absolutePath", oldPathValue); // if we come from includeScript(), or whereever } else { qWarning() << QObject::tr("File %1 not found").arg(fileName); return false; } return true;}
QScriptValue includeScript(QScriptContext *context, QScriptEngine *engine){ QString currentFileName = engine->globalObject().property("script").property("absoluteFilePath").toString(); QFileInfo currentFileInfo(currentFileName); QString path = currentFileInfo.path(); QString importFile = context->argument(0).toString(); QFileInfo importInfo(importFile); if (importInfo.isRelative()) { importFile = path + "/" + importInfo.filePath(); } QScriptValue retValue; if (!loadFile(importFile, engine, retValue)) return context->throwError(QObject::tr("Failed to resolve include: %1").arg(importFile)); return retValue;}
...engine->globalObject().setProperty("include", engine->newFunction(includeScript));...
C++ (Qt) int GlobalScriptEngine::main(){...QScriptValue global = staticEngine->globalObject(); // add a 'system' object QScriptValue system = staticEngine->newObject(); global.setProperty("system", system); [b] QScriptValue script = staticEngine->newObject(); global.setProperty("script", script);[/b]...
C++ (Qt)bool loadFile(const QString& fileName, QScriptEngine *engine, QScriptValue& retValue){...QScriptValue script = engine->globalObject().property("script"); QScriptValue oldFilePathValue = script.property("absoluteFilePath"); QScriptValue oldPathValue = script.property("absolutePath"); script.setProperty("absoluteFilePath", engine->toScriptValue(absoluteFileName)); script.setProperty("absolutePath", engine->toScriptValue(absolutePath)); retValue = engine->evaluate(contents, fileInfo.absoluteFilePath(), lineNumber); if (engine->hasUncaughtException()) { QStringList backtrace = engine->uncaughtExceptionBacktrace(); qWarning() << QString(" %1\n%2\n\n").arg(retValue.toString()).arg(backtrace.join("\n")); return true; } script.setProperty("absoluteFilePath", oldFilePathValue); script.setProperty("absolutePath", oldPathValue);...