Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 6 files). Show all
Unified · Split
| @@ -222,7 +222,7 @@ QString ScriptEngine::getFilename() const { · 221 unmodified lines | |||
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | 224 | ||
| 225 | // FIXME - remove the file/url scheme code here and let the script cache handle things | 225 | // FIXME - switch this to the new model of ScriptCache callbacks |
| 226 | void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { | 226 | void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { |
| 227 | if (_isRunning) { | 227 | if (_isRunning) { |
| 228 | return; | 228 | return; |
| @@ -238,6 +238,7 @@ void ScriptEngine::loadURL(const QUrl& scriptURL, bool reload) { · 9 unmodified lines | |||
| 238 | scriptCache->getScript(url, this, isPending, reload); | 238 | scriptCache->getScript(url, this, isPending, reload); |
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | // FIXME - switch this to the new model of ScriptCache callbacks | ||
| 241 | void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) { | 242 | void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scriptContents) { |
| 242 | _scriptContents = scriptContents; | 243 | _scriptContents = scriptContents; |
| 243 | if (_wantSignals) { | 244 | if (_wantSignals) { |
| @@ -245,6 +246,7 @@ void ScriptEngine::scriptContentsAvailable(const QUrl& url, const QString& scrip · 1 unmodified line | |||
| 245 | } | 246 | } |
| 246 | } | 247 | } |
| 247 | 248 | ||
| 249 | // FIXME - switch this to the new model of ScriptCache callbacks | ||
| 248 | void ScriptEngine::errorInLoadingScript(const QUrl& url) { | 250 | void ScriptEngine::errorInLoadingScript(const QUrl& url) { |
| 249 | qCDebug(scriptengine) << "ERROR Loading file:" << url.toString() << "line:" << __LINE__; | 251 | qCDebug(scriptengine) << "ERROR Loading file:" << url.toString() << "line:" << __LINE__; |
| 250 | if (_wantSignals) { | 252 | if (_wantSignals) { |
| @@ -320,13 +322,17 @@ void ScriptEngine::init() { · 69 unmodified lines | |||
| 320 | 322 | ||
| 321 | void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { | 323 | void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { |
| 322 | if (QThread::currentThread() != thread()) { | 324 | if (QThread::currentThread() != thread()) { |
| 325 | #ifdef THREAD_DEBUGGING | ||
| 323 | qDebug() << "*** WARNING *** ScriptEngine::registerGlobalObject() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name; | 326 | qDebug() << "*** WARNING *** ScriptEngine::registerGlobalObject() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name; |
| 327 | #endif | ||
| 324 | QMetaObject::invokeMethod(this, "registerGlobalObject", | 328 | QMetaObject::invokeMethod(this, "registerGlobalObject", |
| 325 | Q_ARG(const QString&, name), | 329 | Q_ARG(const QString&, name), |
| 326 | Q_ARG(QObject*, object)); | 330 | Q_ARG(QObject*, object)); |
| 327 | return; | 331 | return; |
| 328 | } | 332 | } |
| 329 | //qDebug() << "ScriptEngine::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name; | 333 | #ifdef THREAD_DEBUGGING |
| 334 | qDebug() << "ScriptEngine::registerGlobalObject() called on thread [" << QThread::currentThread() << "] name:" << name; | ||
| 335 | #endif | ||
| 330 | 336 | ||
| 331 | if (object) { | 337 | if (object) { |
| 332 | QScriptValue value = newQObject(object); | 338 | QScriptValue value = newQObject(object); |
| @@ -336,14 +342,18 @@ void ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { · 3 unmodified lines | |||
| 336 | 342 | ||
| 337 | void ScriptEngine::registerFunction(const QString& name, QScriptEngine::FunctionSignature functionSignature, int numArguments) { | 343 | void ScriptEngine::registerFunction(const QString& name, QScriptEngine::FunctionSignature functionSignature, int numArguments) { |
| 338 | if (QThread::currentThread() != thread()) { | 344 | if (QThread::currentThread() != thread()) { |
| 345 | #ifdef THREAD_DEBUGGING | ||
| 339 | qDebug() << "*** WARNING *** ScriptEngine::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name; | 346 | qDebug() << "*** WARNING *** ScriptEngine::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] name:" << name; |
| 347 | #endif | ||
| 340 | QMetaObject::invokeMethod(this, "registerFunction", | 348 | QMetaObject::invokeMethod(this, "registerFunction", |
| 341 | Q_ARG(const QString&, name), | 349 | Q_ARG(const QString&, name), |
| 342 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), | 350 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), |
| 343 | Q_ARG(int, numArguments)); | 351 | Q_ARG(int, numArguments)); |
| 344 | return; | 352 | return; |
| 345 | } | 353 | } |
| 346 | //qDebug() << "ScriptEngine::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name; | 354 | #ifdef THREAD_DEBUGGING |
| 355 | qDebug() << "ScriptEngine::registerFunction() called on thread [" << QThread::currentThread() << "] name:" << name; | ||
| 356 | #endif | ||
| 347 | 357 | ||
| 348 | QScriptValue scriptFun = newFunction(functionSignature, numArguments); | 358 | QScriptValue scriptFun = newFunction(functionSignature, numArguments); |
| 349 | globalObject().setProperty(name, scriptFun); | 359 | globalObject().setProperty(name, scriptFun); |
| @@ -351,14 +361,18 @@ void ScriptEngine::registerFunction(const QString& name, QScriptEngine::Function · 1 unmodified line | |||
| 351 | 361 | ||
| 352 | void ScriptEngine::registerFunction(const QString& parent, const QString& name, QScriptEngine::FunctionSignature functionSignature, int numArguments) { | 362 | void ScriptEngine::registerFunction(const QString& parent, const QString& name, QScriptEngine::FunctionSignature functionSignature, int numArguments) { |
| 353 | if (QThread::currentThread() != thread()) { | 363 | if (QThread::currentThread() != thread()) { |
| 364 | #ifdef THREAD_DEBUGGING | ||
| 354 | qDebug() << "*** WARNING *** ScriptEngine::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] parent:" << parent << "name:" << name; | 365 | qDebug() << "*** WARNING *** ScriptEngine::registerFunction() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] parent:" << parent << "name:" << name; |
| 366 | #endif | ||
| 355 | QMetaObject::invokeMethod(this, "registerFunction", | 367 | QMetaObject::invokeMethod(this, "registerFunction", |
| 356 | Q_ARG(const QString&, name), | 368 | Q_ARG(const QString&, name), |
| 357 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), | 369 | Q_ARG(QScriptEngine::FunctionSignature, functionSignature), |
| 358 | Q_ARG(int, numArguments)); | 370 | Q_ARG(int, numArguments)); |
| 359 | return; | 371 | return; |
| 360 | } | 372 | } |
| 361 | //qDebug() << "ScriptEngine::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name; | 373 | #ifdef THREAD_DEBUGGING |
| 374 | qDebug() << "ScriptEngine::registerFunction() called on thread [" << QThread::currentThread() << "] parent:" << parent << "name:" << name; | ||
| 375 | #endif | ||
| 362 | 376 | ||
| 363 | QScriptValue object = globalObject().property(parent); | 377 | QScriptValue object = globalObject().property(parent); |
| 364 | if (object.isValid()) { | 378 | if (object.isValid()) { |
| @@ -370,8 +384,10 @@ void ScriptEngine::registerFunction(const QString& parent, const QString& name, · 5 unmodified lines | |||
| 370 | void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter, | 384 | void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter, |
| 371 | QScriptEngine::FunctionSignature setter, const QString& parent) { | 385 | QScriptEngine::FunctionSignature setter, const QString& parent) { |
| 372 | if (QThread::currentThread() != thread()) { | 386 | if (QThread::currentThread() != thread()) { |
| 387 | #ifdef THREAD_DEBUGGING | ||
| 373 | qDebug() << "*** WARNING *** ScriptEngine::registerGetterSetter() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 388 | qDebug() << "*** WARNING *** ScriptEngine::registerGetterSetter() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 374 | " name:" << name << "parent:" << parent; | 389 | " name:" << name << "parent:" << parent; |
| 390 | #endif | ||
| 375 | QMetaObject::invokeMethod(this, "registerGetterSetter", | 391 | QMetaObject::invokeMethod(this, "registerGetterSetter", |
| 376 | Q_ARG(const QString&, name), | 392 | Q_ARG(const QString&, name), |
| 377 | Q_ARG(QScriptEngine::FunctionSignature, getter), | 393 | Q_ARG(QScriptEngine::FunctionSignature, getter), |
| @@ -379,7 +395,9 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 1 unmodified line | |||
| 379 | Q_ARG(const QString&, parent)); | 395 | Q_ARG(const QString&, parent)); |
| 380 | return; | 396 | return; |
| 381 | } | 397 | } |
| 382 | //qDebug() << "ScriptEngine::registerGetterSetter() called on thread [" << QThread::currentThread() << "] name:" << name << "parent:" << parent; | 398 | #ifdef THREAD_DEBUGGING |
| 399 | qDebug() << "ScriptEngine::registerGetterSetter() called on thread [" << QThread::currentThread() << "] name:" << name << "parent:" << parent; | ||
| 400 | #endif | ||
| 383 | 401 | ||
| 384 | QScriptValue setterFunction = newFunction(setter, 1); | 402 | QScriptValue setterFunction = newFunction(setter, 1); |
| 385 | QScriptValue getterFunction = newFunction(getter); | 403 | QScriptValue getterFunction = newFunction(getter); |
| @@ -399,15 +417,19 @@ void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::Func · 13 unmodified lines | |||
| 399 | // Unregister the handlers for this eventName and entityID. | 417 | // Unregister the handlers for this eventName and entityID. |
| 400 | void ScriptEngine::removeEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { | 418 | void ScriptEngine::removeEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { |
| 401 | if (QThread::currentThread() != thread()) { | 419 | if (QThread::currentThread() != thread()) { |
| 420 | #ifdef THREAD_DEBUGGING | ||
| 402 | qDebug() << "*** WARNING *** ScriptEngine::removeEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 421 | qDebug() << "*** WARNING *** ScriptEngine::removeEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 403 | "entityID:" << entityID << " eventName:" << eventName; | 422 | "entityID:" << entityID << " eventName:" << eventName; |
| 423 | #endif | ||
| 404 | QMetaObject::invokeMethod(this, "removeEventHandler", | 424 | QMetaObject::invokeMethod(this, "removeEventHandler", |
| 405 | Q_ARG(const EntityItemID&, entityID), | 425 | Q_ARG(const EntityItemID&, entityID), |
| 406 | Q_ARG(const QString&, eventName), | 426 | Q_ARG(const QString&, eventName), |
| 407 | Q_ARG(QScriptValue, handler)); | 427 | Q_ARG(QScriptValue, handler)); |
| 408 | return; | 428 | return; |
| 409 | } | 429 | } |
| 410 | //qDebug() << "ScriptEngine::removeEventHandler() called on thread [" << QThread::currentThread() << "] entityID:" << entityID << " eventName : " << eventName; | 430 | #ifdef THREAD_DEBUGGING |
| 431 | qDebug() << "ScriptEngine::removeEventHandler() called on thread [" << QThread::currentThread() << "] entityID:" << entityID << " eventName : " << eventName; | ||
| 432 | #endif | ||
| 411 | 433 | ||
| 412 | if (!_registeredHandlers.contains(entityID)) { | 434 | if (!_registeredHandlers.contains(entityID)) { |
| 413 | return; | 435 | return; |
| @@ -425,15 +447,20 @@ void ScriptEngine::removeEventHandler(const EntityItemID& entityID, const QStrin · 11 unmodified lines | |||
| 425 | // Register the handler. | 447 | // Register the handler. |
| 426 | void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { | 448 | void ScriptEngine::addEventHandler(const EntityItemID& entityID, const QString& eventName, QScriptValue handler) { |
| 427 | if (QThread::currentThread() != thread()) { | 449 | if (QThread::currentThread() != thread()) { |
| 450 | #ifdef THREAD_DEBUGGING | ||
| 428 | qDebug() << "*** WARNING *** ScriptEngine::addEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 451 | qDebug() << "*** WARNING *** ScriptEngine::addEventHandler() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 429 | "entityID:" << entityID << " eventName:" << eventName; | 452 | "entityID:" << entityID << " eventName:" << eventName; |
| 453 | #endif | ||
| 454 | |||
| 430 | QMetaObject::invokeMethod(this, "addEventHandler", | 455 | QMetaObject::invokeMethod(this, "addEventHandler", |
| 431 | Q_ARG(const EntityItemID&, entityID), | 456 | Q_ARG(const EntityItemID&, entityID), |
| 432 | Q_ARG(const QString&, eventName), | 457 | Q_ARG(const QString&, eventName), |
| 433 | Q_ARG(QScriptValue, handler)); | 458 | Q_ARG(QScriptValue, handler)); |
| 434 | return; | 459 | return; |
| 435 | } | 460 | } |
| 436 | //qDebug() << "ScriptEngine::addEventHandler() called on thread [" << QThread::currentThread() << "] entityID:" << entityID << " eventName : " << eventName; | 461 | #ifdef THREAD_DEBUGGING |
| 462 | qDebug() << "ScriptEngine::addEventHandler() called on thread [" << QThread::currentThread() << "] entityID:" << entityID << " eventName : " << eventName; | ||
| 463 | #endif | ||
| 437 | 464 | ||
| 438 | if (_registeredHandlers.count() == 0) { // First time any per-entity handler has been added in this script... | 465 | if (_registeredHandlers.count() == 0) { // First time any per-entity handler has been added in this script... |
| 439 | // Connect up ALL the handlers to the global entities object's signals. | 466 | // Connect up ALL the handlers to the global entities object's signals. |
| @@ -496,8 +523,10 @@ QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileN · 56 unmodified lines | |||
| 496 | 523 | ||
| 497 | if (QThread::currentThread() != thread()) { | 524 | if (QThread::currentThread() != thread()) { |
| 498 | QScriptValue result; | 525 | QScriptValue result; |
| 526 | #ifdef THREAD_DEBUGGING | ||
| 499 | qDebug() << "*** WARNING *** ScriptEngine::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 527 | qDebug() << "*** WARNING *** ScriptEngine::evaluate() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 500 | "program:" << program << " fileName:" << fileName << "lineNumber:" << lineNumber; | 528 | "program:" << program << " fileName:" << fileName << "lineNumber:" << lineNumber; |
| 529 | #endif | ||
| 501 | QMetaObject::invokeMethod(this, "evaluate", Qt::BlockingQueuedConnection, | 530 | QMetaObject::invokeMethod(this, "evaluate", Qt::BlockingQueuedConnection, |
| 502 | Q_RETURN_ARG(QScriptValue, result), | 531 | Q_RETURN_ARG(QScriptValue, result), |
| 503 | Q_ARG(const QString&, program), | 532 | Q_ARG(const QString&, program), |
| @@ -854,10 +883,10 @@ void ScriptEngine::generalHandler(const EntityItemID& entityID, const QString& e · 350 unmodified lines | |||
| 854 | // for the download | 883 | // for the download |
| 855 | void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { | 884 | void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { |
| 856 | if (QThread::currentThread() != thread()) { | 885 | if (QThread::currentThread() != thread()) { |
| 886 | #ifdef THREAD_DEBUGGING | ||
| 857 | qDebug() << "*** WARNING *** ScriptEngine::loadEntityScript() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 887 | qDebug() << "*** WARNING *** ScriptEngine::loadEntityScript() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 858 | "entityID:" << entityID << | 888 | "entityID:" << entityID << "entityScript:" << entityScript <<"forceRedownload:" << forceRedownload; |
| 859 | "entityScript:" << entityScript << | 889 | #endif |
| 860 | "forceRedownload:" << forceRedownload; | ||
| 861 | 890 | ||
| 862 | QMetaObject::invokeMethod(this, "loadEntityScript", | 891 | QMetaObject::invokeMethod(this, "loadEntityScript", |
| 863 | Q_ARG(const EntityItemID&, entityID), | 892 | Q_ARG(const EntityItemID&, entityID), |
| @@ -865,71 +894,104 @@ void ScriptEngine::loadEntityScript(const EntityItemID& entityID, const QString& · 1 unmodified line | |||
| 865 | Q_ARG(bool, forceRedownload)); | 894 | Q_ARG(bool, forceRedownload)); |
| 866 | return; | 895 | return; |
| 867 | } | 896 | } |
| 897 | #ifdef THREAD_DEBUGGING | ||
| 868 | qDebug() << "ScriptEngine::loadEntityScript() called on correct thread [" << thread() << "] " | 898 | qDebug() << "ScriptEngine::loadEntityScript() called on correct thread [" << thread() << "] " |
| 869 | "entityID:" << entityID << | 899 | "entityID:" << entityID << "entityScript:" << entityScript << "forceRedownload:" << forceRedownload; |
| 870 | "entityScript:" << entityScript << | 900 | #endif |
| 871 | "forceRedownload:" << forceRedownload; | ||
| 872 | 901 | ||
| 873 | // If we've been called our known entityScripts should not know about us.. | 902 | // If we've been called our known entityScripts should not know about us.. |
| 874 | assert(!_entityScripts.contains(entityID)); | 903 | assert(!_entityScripts.contains(entityID)); |
| 875 | 904 | ||
| 876 | auto scriptCache = DependencyManager::get<ScriptCache>(); | 905 | #ifdef THREAD_DEBUGGING |
| 906 | qDebug() << "ScriptEngine::loadEntityScript() calling scriptCache->getScriptContents() on thread [" << QThread::currentThread() << "] expected thread [" << thread() << "]"; | ||
| 907 | #endif | ||
| 908 | DependencyManager::get<ScriptCache>()->getScriptContents(entityScript, [=](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) { | ||
| 909 | #ifdef THREAD_DEBUGGING | ||
| 910 | qDebug() << "ScriptEngine::entityScriptContentAvailable() IN LAMBDA contentAvailable on thread [" << QThread::currentThread() << "] expected thread [" << thread() << "]"; | ||
| 911 | #endif | ||
| 877 | 912 | ||
| 878 | scriptCache->getScriptContents(entityScript, [=](const QString& scriptOrURL, const QString& contents, bool isURL, bool success) { | 913 | this->entityScriptContentAvailable(entityID, scriptOrURL, contents, isURL, success); |
| 914 | }, forceRedownload); | ||
| 915 | } | ||
| 879 | 916 | ||
| 880 | // first check the syntax of the script contents | 917 | // since all of these operations can be asynch we will always do the actual work in the response handler |
| 881 | QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(contents); | 918 | // for the download |
| 882 | if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) { | 919 | void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, const QString& scriptOrURL, const QString& contents, bool isURL, bool success) { |
| 883 | qCDebug(scriptengine) << "ScriptEngine::loadEntityScript() entity:" << entityID; | 920 | if (QThread::currentThread() != thread()) { |
| 884 | qCDebug(scriptengine) << " " << syntaxCheck.errorMessage() << ":" | 921 | #ifdef THREAD_DEBUGGING |
| 885 | << syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber(); | 922 | qDebug() << "*** WARNING *** ScriptEngine::entityScriptContentAvailable() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 886 | qCDebug(scriptengine) << " SCRIPT:" << scriptOrURL; | 923 | "entityID:" << entityID << "scriptOrURL:" << scriptOrURL << "contents:" << contents << "isURL:" << isURL << "success:" << success; |
| 887 | scriptCache->addScriptToBadScriptList(scriptOrURL); | 924 | #endif |
| 888 | return; // done processing script | ||
| 889 | } | ||
| 890 | 925 | ||
| 891 | if (isURL) { | 926 | QMetaObject::invokeMethod(this, "entityScriptContentAvailable", |
| 892 | setParentURL(scriptOrURL); | 927 | Q_ARG(const EntityItemID&, entityID), |
| 893 | } | 928 | Q_ARG(const QString&, scriptOrURL), |
| 929 | Q_ARG(const QString&, contents), | ||
| 930 | Q_ARG(bool, isURL), | ||
| 931 | Q_ARG(bool, success)); | ||
| 932 | return; | ||
| 933 | } | ||
| 894 | 934 | ||
| 895 | QScriptEngine sandbox; | 935 | #ifdef THREAD_DEBUGGING |
| 896 | QScriptValue testConstructor = sandbox.evaluate(contents); | 936 | qDebug() << "ScriptEngine::entityScriptContentAvailable() thread [" << QThread::currentThread() << "] expected thread [" << thread() << "]"; |
| 937 | #endif | ||
| 897 | 938 | ||
| 898 | if (!testConstructor.isFunction()) { | 939 | auto scriptCache = DependencyManager::get<ScriptCache>(); |
| 899 | qCDebug(scriptengine) << "ScriptEngine::loadEntityScript() entity:" << entityID; | ||
| 900 | qCDebug(scriptengine) << " NOT CONSTRUCTOR"; | ||
| 901 | qCDebug(scriptengine) << " SCRIPT:" << scriptOrURL; | ||
| 902 | scriptCache->addScriptToBadScriptList(scriptOrURL); | ||
| 903 | return; // done processing script | ||
| 904 | } | ||
| 905 | 940 | ||
| 906 | QScriptValue entityScriptConstructor = evaluate(contents); | 941 | // first check the syntax of the script contents |
| 942 | QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(contents); | ||
| 943 | if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) { | ||
| 944 | qCDebug(scriptengine) << "ScriptEngine::loadEntityScript() entity:" << entityID; | ||
| 945 | qCDebug(scriptengine) << " " << syntaxCheck.errorMessage() << ":" | ||
| 946 | << syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber(); | ||
| 947 | qCDebug(scriptengine) << " SCRIPT:" << scriptOrURL; | ||
| 948 | scriptCache->addScriptToBadScriptList(scriptOrURL); | ||
| 949 | return; // done processing script | ||
| 950 | } | ||
| 907 | 951 | ||
| 908 | QScriptValue entityScriptObject = entityScriptConstructor.construct(); | 952 | if (isURL) { |
| 909 | EntityScriptDetails newDetails = { scriptOrURL, entityScriptObject }; | 953 | setParentURL(scriptOrURL); |
| 910 | _entityScripts[entityID] = newDetails; | 954 | } |
| 911 | 955 | ||
| 912 | if (isURL) { | 956 | QScriptEngine sandbox; |
| 913 | setParentURL(""); | 957 | QScriptValue testConstructor = sandbox.evaluate(contents); |
| 914 | } | ||
| 915 | 958 | ||
| 916 | // if we got this far, then call the preload method | 959 | if (!testConstructor.isFunction()) { |
| 917 | callEntityScriptMethod(entityID, "preload"); | 960 | qCDebug(scriptengine) << "ScriptEngine::loadEntityScript() entity:" << entityID; |
| 961 | qCDebug(scriptengine) << " NOT CONSTRUCTOR"; | ||
| 962 | qCDebug(scriptengine) << " SCRIPT:" << scriptOrURL; | ||
| 963 | scriptCache->addScriptToBadScriptList(scriptOrURL); | ||
| 964 | return; // done processing script | ||
| 965 | } | ||
| 966 | |||
| 967 | QScriptValue entityScriptConstructor = evaluate(contents); | ||
| 918 | 968 | ||
| 919 | }, forceRedownload); | 969 | QScriptValue entityScriptObject = entityScriptConstructor.construct(); |
| 970 | EntityScriptDetails newDetails = { scriptOrURL, entityScriptObject }; | ||
| 971 | _entityScripts[entityID] = newDetails; | ||
| 972 | if (isURL) { | ||
| 973 | setParentURL(""); | ||
| 974 | } | ||
| 975 | |||
| 976 | // if we got this far, then call the preload method | ||
| 977 | callEntityScriptMethod(entityID, "preload"); | ||
| 920 | } | 978 | } |
| 921 | 979 | ||
| 922 | void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { | 980 | void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { |
| 923 | if (QThread::currentThread() != thread()) { | 981 | if (QThread::currentThread() != thread()) { |
| 982 | #ifdef THREAD_DEBUGGING | ||
| 924 | qDebug() << "*** WARNING *** ScriptEngine::unloadEntityScript() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 983 | qDebug() << "*** WARNING *** ScriptEngine::unloadEntityScript() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 925 | "entityID:" << entityID; | 984 | "entityID:" << entityID; |
| 985 | #endif | ||
| 926 | 986 | ||
| 927 | QMetaObject::invokeMethod(this, "unloadEntityScript", | 987 | QMetaObject::invokeMethod(this, "unloadEntityScript", |
| 928 | Q_ARG(const EntityItemID&, entityID)); | 988 | Q_ARG(const EntityItemID&, entityID)); |
| 929 | return; | 989 | return; |
| 930 | } | 990 | } |
| 991 | #ifdef THREAD_DEBUGGING | ||
| 931 | qDebug() << "ScriptEngine::unloadEntityScript() called on correct thread [" << thread() << "] " | 992 | qDebug() << "ScriptEngine::unloadEntityScript() called on correct thread [" << thread() << "] " |
| 932 | "entityID:" << entityID; | 993 | "entityID:" << entityID; |
| 994 | #endif | ||
| 933 | 995 | ||
| 934 | if (_entityScripts.contains(entityID)) { | 996 | if (_entityScripts.contains(entityID)) { |
| 935 | callEntityScriptMethod(entityID, "unload"); | 997 | callEntityScriptMethod(entityID, "unload"); |
| @@ -939,18 +1001,20 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { · 3 unmodified lines | |||
| 939 | 1001 | ||
| 940 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) { | 1002 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName) { |
| 941 | if (QThread::currentThread() != thread()) { | 1003 | if (QThread::currentThread() != thread()) { |
| 1004 | #ifdef THREAD_DEBUGGING | ||
| 942 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 1005 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 943 | "entityID:" << entityID << | 1006 | "entityID:" << entityID << "methodName:" << methodName; |
| 944 | "methodName:" << methodName; | 1007 | #endif |
| 945 | 1008 | ||
| 946 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", | 1009 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", |
| 947 | Q_ARG(const EntityItemID&, entityID), | 1010 | Q_ARG(const EntityItemID&, entityID), |
| 948 | Q_ARG(const QString&, methodName)); | 1011 | Q_ARG(const QString&, methodName)); |
| 949 | return; | 1012 | return; |
| 950 | } | 1013 | } |
| 1014 | #ifdef THREAD_DEBUGGING | ||
| 951 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " | 1015 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " |
| 952 | "entityID:" << entityID << | 1016 | "entityID:" << entityID << "methodName:" << methodName; |
| 953 | "methodName:" << methodName; | 1017 | #endif |
| 954 | 1018 | ||
| 955 | if (_entityScripts.contains(entityID)) { | 1019 | if (_entityScripts.contains(entityID)) { |
| 956 | EntityScriptDetails details = _entityScripts[entityID]; | 1020 | EntityScriptDetails details = _entityScripts[entityID]; |
| @@ -960,15 +1024,16 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 3 unmodified lines | |||
| 960 | args << entityID.toScriptValue(this); | 1024 | args << entityID.toScriptValue(this); |
| 961 | entityScript.property(methodName).call(entityScript, args); | 1025 | entityScript.property(methodName).call(entityScript, args); |
| 962 | } | 1026 | } |
| 1027 | |||
| 963 | } | 1028 | } |
| 964 | } | 1029 | } |
| 965 | 1030 | ||
| 966 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const MouseEvent& event) { | 1031 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const MouseEvent& event) { |
| 967 | if (QThread::currentThread() != thread()) { | 1032 | if (QThread::currentThread() != thread()) { |
| 1033 | #ifdef THREAD_DEBUGGING | ||
| 968 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 1034 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 969 | "entityID:" << entityID << | 1035 | "entityID:" << entityID << "methodName:" << methodName << "event: mouseEvent"; |
| 970 | "methodName:" << methodName << | 1036 | #endif |
| 971 | "event: mouseEvent"; | ||
| 972 | 1037 | ||
| 973 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", | 1038 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", |
| 974 | Q_ARG(const EntityItemID&, entityID), | 1039 | Q_ARG(const EntityItemID&, entityID), |
| @@ -976,10 +1041,10 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 1 unmodified line | |||
| 976 | Q_ARG(const MouseEvent&, event)); | 1041 | Q_ARG(const MouseEvent&, event)); |
| 977 | return; | 1042 | return; |
| 978 | } | 1043 | } |
| 1044 | #ifdef THREAD_DEBUGGING | ||
| 979 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " | 1045 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " |
| 980 | "entityID:" << entityID << | 1046 | "entityID:" << entityID << "methodName:" << methodName << "event: mouseEvent"; |
| 981 | "methodName:" << methodName << | 1047 | #endif |
| 982 | "event: mouseEvent"; | ||
| 983 | 1048 | ||
| 984 | if (_entityScripts.contains(entityID)) { | 1049 | if (_entityScripts.contains(entityID)) { |
| 985 | EntityScriptDetails details = _entityScripts[entityID]; | 1050 | EntityScriptDetails details = _entityScripts[entityID]; |
| @@ -996,11 +1061,10 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 10 unmodified lines | |||
| 996 | 1061 | ||
| 997 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const EntityItemID& otherID, const Collision& collision) { | 1062 | void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QString& methodName, const EntityItemID& otherID, const Collision& collision) { |
| 998 | if (QThread::currentThread() != thread()) { | 1063 | if (QThread::currentThread() != thread()) { |
| 1064 | #ifdef THREAD_DEBUGGING | ||
| 999 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " | 1065 | qDebug() << "*** WARNING *** ScriptEngine::callEntityScriptMethod() called on wrong thread [" << QThread::currentThread() << "], invoking on correct thread [" << thread() << "] " |
| 1000 | "entityID:" << entityID << | 1066 | "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision"; |
| 1001 | "methodName:" << methodName << | 1067 | #endif |
| 1002 | "otherID:" << otherID << | ||
| 1003 | "collision: collision"; | ||
| 1004 | 1068 | ||
| 1005 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", | 1069 | QMetaObject::invokeMethod(this, "callEntityScriptMethod", |
| 1006 | Q_ARG(const EntityItemID&, entityID), | 1070 | Q_ARG(const EntityItemID&, entityID), |
| @@ -1009,11 +1073,10 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 2 unmodified lines | |||
| 1009 | Q_ARG(const Collision&, collision)); | 1073 | Q_ARG(const Collision&, collision)); |
| 1010 | return; | 1074 | return; |
| 1011 | } | 1075 | } |
| 1076 | #ifdef THREAD_DEBUGGING | ||
| 1012 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " | 1077 | qDebug() << "ScriptEngine::callEntityScriptMethod() called on correct thread [" << thread() << "] " |
| 1013 | "entityID:" << entityID << | 1078 | "entityID:" << entityID << "methodName:" << methodName << "otherID:" << otherID << "collision: collision"; |
| 1014 | "methodName:" << methodName << | 1079 | #endif |
| 1015 | "otherID:" << otherID << | ||
| 1016 | "collision: collision"; | ||
| 1017 | 1080 | ||
| 1018 | if (_entityScripts.contains(entityID)) { | 1081 | if (_entityScripts.contains(entityID)) { |
| 1019 | EntityScriptDetails details = _entityScripts[entityID]; | 1082 | EntityScriptDetails details = _entityScripts[entityID]; |