void QScriptDebuggerScriptedConsoleCommandJob::handleResponse( const QScriptDebuggerResponse &response, int commandId) { Q_D(QScriptDebuggerScriptedConsoleCommandJob); // ### generalize QScriptEngine *engine = d->command->globalObject.engine(); engine->setGlobalObject(d->command->globalObject); QScriptValueList args; args.append(engine->toScriptValue(response)); args.append(QScriptValue(engine, commandId)); QScriptDebuggerConsoleGlobalObject *global; global = qobject_cast<QScriptDebuggerConsoleGlobalObject*>(d->command->globalObject.toQObject()); Q_ASSERT(global != 0); global->setScheduler(this); global->setResponseHandler(this); global->setMessageHandler(d->messageHandler); global->setConsole(d->console); d->commandCount = 0; QScriptValue ret = d->command->responseFunction.call(QScriptValue(), args); global->setScheduler(0); global->setResponseHandler(0); global->setMessageHandler(0); global->setConsole(0); if (ret.isError()) { qWarning("*** internal error: %s", qPrintable(ret.toString())); } if (d->commandCount == 0) finish(); }
bool loadScriptStates(const char *filename) { WzConfig ini(filename); QStringList list = ini.childGroups(); debug(LOG_SAVE, "Loading script states for %d script contexts", scripts.size()); for (int i = 0; i < list.size(); ++i) { ini.beginGroup(list[i]); int player = ini.value("me").toInt(); QString scriptName = ini.value("scriptName").toString(); QScriptEngine *engine = findEngineForPlayer(player, scriptName); if (engine && list[i].startsWith("triggers_")) { timerNode node; node.player = player; node.ms = ini.value("ms").toInt(); node.frameTime = ini.value("frame").toInt(); node.engine = engine; debug(LOG_SAVE, "Registering trigger %d for player %d, script %s", i, node.player, scriptName.toUtf8().constData()); node.function = ini.value("function").toString(); node.baseobj = ini.value("baseobj", -1).toInt(); node.type = (timerType)ini.value("type", TIMER_REPEAT).toInt(); timers.push_back(node); } else if (engine && list[i].startsWith("globals_")) { QStringList keys = ini.childKeys(); debug(LOG_SAVE, "Loading script globals for player %d, script %s -- found %d values", player, scriptName.toUtf8().constData(), keys.size()); for (int j = 0; j < keys.size(); ++j) { engine->globalObject().setProperty(keys.at(j), engine->toScriptValue(ini.value(keys.at(j)))); } } else if (engine && list[i].startsWith("groups_")) { QStringList keys = ini.childKeys(); for (int j = 0; j < keys.size(); ++j) { QStringList values = ini.value(keys.at(j)).toStringList(); bool ok = false; // check if number int droidId = keys.at(j).toInt(&ok); for (int k = 0; ok && k < values.size(); k++) { int groupId = values.at(k).toInt(); loadGroup(engine, groupId, droidId); } } } ini.endGroup(); } return true; }
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QStringList paths = QStringList() << QCoreApplication::applicationDirPath() + "/../../plugins"; app.setLibraryPaths(paths); QScriptEngine *engine = new QScriptEngine(); QScriptValue global = engine->globalObject(); // add the qt object global.setProperty("qs", engine->newObject()); // add a 'script' object QScriptValue script = engine->newObject(); global.property("qs").setProperty("script", script); // add a 'system' object QScriptValue system = engine->newObject(); global.property("qs").setProperty("system", system); // add os information to qt.system.os #ifdef Q_OS_WIN32 QScriptValue osName = engine->toScriptValue(QString("windows")); #elif defined(Q_OS_LINUX) QScriptValue osName = engine->toScriptValue(QString("linux")); #elif defined(Q_OS_MAC) QScriptValue osName = engine->toScriptValue(QString("mac")); #elif defined(Q_OS_UNIX) QScriptValue osName = engine->toScriptValue(QString("unix")); #endif system.setProperty("os", osName); // add environment variables to qt.system.env QMap<QString,QVariant> envMap; QStringList envList = QProcess::systemEnvironment(); foreach (const QString &entry, envList) { QStringList keyVal = entry.split('='); if (keyVal.size() == 1) envMap.insert(keyVal.at(0), QString()); else envMap.insert(keyVal.at(0), keyVal.at(1)); }
bool loadScriptStates(const char *filename) { WzConfig ini(filename); QStringList list = ini.childGroups(); debug(LOG_SAVE, "Loading script states for %d script contexts", scripts.size()); for (int i = 0; i < list.size(); ++i) { if (list[i].startsWith("triggers_")) { ini.beginGroup(list[i]); timerNode node; node.player = ini.value("player").toInt(); node.ms = ini.value("ms").toInt(); node.frameTime = ini.value("frame").toInt(); debug(LOG_SAVE, "Registering trigger %d for player %d", i, node.player); node.engine = findEngineForPlayer(node.player); if (node.engine) { node.function = ini.value("function").toString(); node.baseobj = ini.value("baseobj", -1).toInt(); node.type = (timerType)ini.value("type", TIMER_REPEAT).toInt(); timers.push_back(node); } ini.endGroup(); } else if (list[i].startsWith("globals_")) { ini.beginGroup(list[i]); int player = ini.value("me").toInt(); QStringList keys = ini.childKeys(); debug(LOG_SAVE, "Loading script globals for player %d -- found %d values", player, keys.size()); QScriptEngine *engine = findEngineForPlayer(player); if (engine) { for (int j = 0; j < keys.size(); ++j) { engine->globalObject().setProperty(keys.at(j), engine->toScriptValue(ini.value(keys.at(j)))); } } ini.endGroup(); } } return true; }
//! [55] QScriptValue rectifier(QScriptContext *ctx, QScriptEngine *eng) { QRectF magicRect = qscriptvalue_cast<QRectF>(ctx->callee().data()); QRectF sourceRect = qscriptvalue_cast<QRectF>(ctx->argument(0)); return eng->toScriptValue(sourceRect.intersected(magicRect)); }
//! [44] QScriptValue QPoint_ctor(QScriptContext *context, QScriptEngine *engine) { int x = context->argument(0).toInt32(); int y = context->argument(1).toInt32(); return engine->toScriptValue(QPoint(x, y)); }
qDebug() << array.toString(); //! [54] //! [55] QScriptValue rectifier(QScriptContext *ctx, QScriptEngine *eng) { QRectF magicRect = qscriptvalue_cast<QRectF>(ctx->callee().data()); QRectF sourceRect = qscriptvalue_cast<QRectF>(ctx->argument(0)); return eng->toScriptValue(sourceRect.intersected(magicRect)); } ... QScriptValue fun = eng.newFunction(rectifier); QRectF magicRect = QRectF(10, 20, 30, 40); fun.setData(eng.toScriptValue(magicRect)); eng.globalObject().setProperty("rectifier", fun); //! [55] //! [58] QScriptValue add(QScriptContext *ctx, QScriptEngine *eng) { double a = ctx->argument(0).toNumber(); double b = ctx->argument(1).toNumber(); return a + b; } //! [58] //! [62] QScriptValue add(QScriptContext *ctx, QScriptEngine *eng) {