Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 370 files). Show all
Unified · Split
| @@ -225,7 +225,12 @@ QString encodeEntityIdIntoEntityUrl(const QString& url, const QString& entityID) · 224 unmodified lines | ||
| 225 | 225 | |
| 226 | 226 | QString ScriptManager::logException(const ScriptValue& exception) { |
| 227 | 227 | auto message = formatException(exception, _enableExtendedJSExceptions.get()); |
| 228 | − scriptErrorMessage(message); | |
| 228 | + auto context = _engine->currentContext(); | |
| 229 | + if (context) { | |
| 230 | + scriptErrorMessage(message, context->currentFileName(), context->currentLineNumber()); | |
| 231 | + } else { | |
| 232 | + scriptErrorMessage(message, "", -1); | |
| 233 | + } | |
| 229 | 234 | return message; |
| 230 | 235 | } |
| 231 | 236 | |
| @@ -330,6 +335,11 @@ ScriptManager::ScriptManager(Context context, const QString& scriptContents, con · 98 unmodified lines | ||
| 330 | 335 | }); |
| 331 | 336 | } |
| 332 | 337 | |
| 338 | + //Gather entity script messages for transmission when running server side. | |
| 339 | + if (_type == Type::ENTITY_SERVER) { | |
| 340 | + ; | |
| 341 | + } | |
| 342 | + | |
| 333 | 343 | if (!_areMetaTypesInitialized) { |
| 334 | 344 | initMetaTypes(); |
| 335 | 345 | } |
| @@ -468,10 +478,10 @@ void ScriptManager::waitTillDoneRunning(bool shutdown) { · 132 unmodified lines | ||
| 468 | 478 | } |
| 469 | 479 | } |
| 470 | 480 | #else |
| 471 | − auto startedWaiting = usecTimestampNow(); | |
| 481 | + //auto startedWaiting = usecTimestampNow(); | |
| 472 | 482 | while (!_isDoneRunning) { |
| 473 | 483 | // If the final evaluation takes too long, then tell the script engine to stop running |
| 474 | − auto elapsedUsecs = usecTimestampNow() - startedWaiting; | |
| 484 | + //auto elapsedUsecs = usecTimestampNow() - startedWaiting; | |
| 475 | 485 | // TODO: This part was very unsafe and was causing crashes all the time. |
| 476 | 486 | // I disabled it for now until we find a safer solution. |
| 477 | 487 | // With it disabled now we get clean shutdowns and restarts. |
| @@ -514,7 +524,7 @@ void ScriptManager::waitTillDoneRunning(bool shutdown) { · 36 unmodified lines | ||
| 514 | 524 | } |
| 515 | 525 | #endif |
| 516 | 526 | |
| 517 | − scriptInfoMessage("Script Engine has stopped:" + getFilename()); | |
| 527 | + scriptInfoMessage("Script Engine has stopped:" + getFilename(), "", -1); | |
| 518 | 528 | } |
| 519 | 529 | } |
| 520 | 530 | |
| @@ -532,6 +542,10 @@ QString ScriptManager::getFilename() const { · 11 unmodified lines | ||
| 532 | 542 | return lastPart; |
| 533 | 543 | } |
| 534 | 544 | |
| 545 | +QString ScriptManager::getAbsoluteFilename() const { | |
| 546 | + return _fileNameString; | |
| 547 | +} | |
| 548 | + | |
| 535 | 549 | bool ScriptManager::hasValidScriptSuffix(const QString& scriptFileName) { |
| 536 | 550 | QFileInfo fileInfo(scriptFileName); |
| 537 | 551 | QString scriptSuffixToLower = fileInfo.completeSuffix().toLower(); |
| @@ -549,7 +563,7 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 11 unmodified lines | ||
| 549 | 563 | |
| 550 | 564 | // Check that script has a supported file extension |
| 551 | 565 | if (!hasValidScriptSuffix(_fileNameString)) { |
| 552 | − scriptErrorMessage("File extension of file: " + _fileNameString + " is not a currently supported script type"); | |
| 566 | + scriptErrorMessage("File extension of file: " + _fileNameString + " is not a currently supported script type", _fileNameString, -1); | |
| 553 | 567 | emit errorLoadingScript(_fileNameString); |
| 554 | 568 | return; |
| 555 | 569 | } |
| @@ -559,7 +573,7 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 3 unmodified lines | ||
| 559 | 573 | scriptCache->getScriptContents(url.toString(), [this](const QString& url, const QString& scriptContents, bool isURL, bool success, const QString&status) { |
| 560 | 574 | qCDebug(scriptengine) << "loadURL" << url << status << QThread::currentThread(); |
| 561 | 575 | if (!success) { |
| 562 | − scriptErrorMessage("ERROR Loading file (" + status + "):" + url); | |
| 576 | + scriptErrorMessage("ERROR Loading file (" + status + "):" + url, url, -1); | |
| 563 | 577 | emit errorLoadingScript(_fileNameString); |
| 564 | 578 | return; |
| 565 | 579 | } |
| @@ -570,24 +584,54 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 4 unmodified lines | ||
| 570 | 584 | }, reload, maxRetries); |
| 571 | 585 | } |
| 572 | 586 | |
| 573 | −void ScriptManager::scriptErrorMessage(const QString& message) { | |
| 587 | +void ScriptManager::scriptErrorMessage(const QString& message, const QString& fileName, int lineNumber) { | |
| 574 | 588 | qCCritical(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message)); |
| 575 | 589 | emit errorMessage(message, getFilename()); |
| 590 | + if (!currentEntityIdentifier.isInvalidID()) { | |
| 591 | + emit errorEntityMessage(message, fileName, lineNumber, currentEntityIdentifier, isEntityServerScript()); | |
| 592 | + } else { | |
| 593 | + if (isEntityServerScript()) { | |
| 594 | + emit errorEntityMessage(message, fileName, lineNumber, EntityItemID(), isEntityServerScript()); | |
| 595 | + } | |
| 596 | + } | |
| 576 | 597 | } |
| 577 | 598 | |
| 578 | −void ScriptManager::scriptWarningMessage(const QString& message) { | |
| 599 | +void ScriptManager::scriptWarningMessage(const QString& message, const QString& fileName, int lineNumber) { | |
| 579 | 600 | qCWarning(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message)); |
| 580 | 601 | emit warningMessage(message, getFilename()); |
| 602 | + if (!currentEntityIdentifier.isInvalidID()) { | |
| 603 | + emit warningEntityMessage(message, fileName, lineNumber, currentEntityIdentifier, isEntityServerScript()); | |
| 604 | + } else { | |
| 605 | + if (isEntityServerScript()) { | |
| 606 | + emit warningEntityMessage(message, fileName, lineNumber, EntityItemID(), isEntityServerScript()); | |
| 607 | + } | |
| 608 | + } | |
| 581 | 609 | } |
| 582 | 610 | |
| 583 | −void ScriptManager::scriptInfoMessage(const QString& message) { | |
| 611 | +void ScriptManager::scriptInfoMessage(const QString& message, const QString& fileName, int lineNumber) { | |
| 584 | 612 | qCInfo(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message)); |
| 585 | 613 | emit infoMessage(message, getFilename()); |
| 614 | + if (!currentEntityIdentifier.isInvalidID()) { | |
| 615 | + emit infoEntityMessage(message, fileName, lineNumber, currentEntityIdentifier, isEntityServerScript()); | |
| 616 | + } else { | |
| 617 | + if (isEntityServerScript()) { | |
| 618 | + emit infoEntityMessage(message, fileName, lineNumber, EntityItemID(), isEntityServerScript()); | |
| 619 | + } | |
| 620 | + } | |
| 586 | 621 | } |
| 587 | 622 | |
| 588 | −void ScriptManager::scriptPrintedMessage(const QString& message) { | |
| 623 | +void ScriptManager::scriptPrintedMessage(const QString& message, const QString& fileName, int lineNumber) { | |
| 589 | 624 | qCDebug(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message)); |
| 590 | 625 | emit printedMessage(message, getFilename()); |
| 626 | + if (!currentEntityIdentifier.isInvalidID()) { | |
| 627 | + emit printedEntityMessage(message, fileName, lineNumber, currentEntityIdentifier, isEntityServerScript()); | |
| 628 | + } else { | |
| 629 | + if (isEntityServerScript()) { | |
| 630 | + // TODO: Some callbacks like for example websockets one right now | |
| 631 | + // don't set currentEntityIdentifier and there doesn't seem to be easy way to add it currently | |
| 632 | + emit printedEntityMessage(message, fileName, lineNumber, EntityItemID(), isEntityServerScript()); | |
| 633 | + } | |
| 634 | + } | |
| 591 | 635 | } |
| 592 | 636 | |
| 593 | 637 | void ScriptManager::clearDebugLogWindow() { |
| @@ -912,7 +956,7 @@ void ScriptManager::run() { · 318 unmodified lines | ||
| 912 | 956 | return; // bail early - avoid setting state in init(), as evaluate() will bail too |
| 913 | 957 | } |
| 914 | 958 | |
| 915 | − scriptInfoMessage("Script Engine starting:" + getFilename()); | |
| 959 | + scriptInfoMessage("Script Engine starting:" + getFilename(), getFilename(), -1); | |
| 916 | 960 | |
| 917 | 961 | if (!_isInitialized) { |
| 918 | 962 | init(); |
| @@ -1064,7 +1108,7 @@ void ScriptManager::run() { · 145 unmodified lines | ||
| 1064 | 1108 | _engine->clearExceptions(); |
| 1065 | 1109 | } |
| 1066 | 1110 | } |
| 1067 | − scriptInfoMessage("Script Engine stopping:" + getFilename()); | |
| 1111 | + scriptInfoMessage("Script Engine stopping:" + getFilename(), getFilename(), -1); | |
| 1068 | 1112 | |
| 1069 | 1113 | stopAllTimers(); // make sure all our timers are stopped if the script is ending |
| 1070 | 1114 | emit scriptEnding(); |
| @@ -1139,7 +1183,7 @@ void ScriptManager::updateMemoryCost(const qint64& deltaSize) { · 68 unmodified lines | ||
| 1139 | 1183 | |
| 1140 | 1184 | void ScriptManager::timerFired() { |
| 1141 | 1185 | if (isStopped()) { |
| 1142 | − scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename()); | |
| 1186 | + scriptWarningMessage("Script.timerFired() while shutting down is ignored... parent script:" + getFilename(), getFilename(), -1); | |
| 1143 | 1187 | return; // bail early |
| 1144 | 1188 | } |
| 1145 | 1189 | |
| @@ -1206,7 +1250,14 @@ QTimer* ScriptManager::setupTimerWithInterval(const ScriptValue& function, int i · 60 unmodified lines | ||
| 1206 | 1250 | |
| 1207 | 1251 | QTimer* ScriptManager::setInterval(const ScriptValue& function, int intervalMS) { |
| 1208 | 1252 | if (isStopped()) { |
| 1209 | − scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename()); | |
| 1253 | + int lineNumber = -1; | |
| 1254 | + QString fileName = getFilename(); | |
| 1255 | + auto context = _engine->currentContext(); | |
| 1256 | + if (context) { | |
| 1257 | + lineNumber = context->currentLineNumber(); | |
| 1258 | + fileName = context->currentFileName(); | |
| 1259 | + } | |
| 1260 | + scriptWarningMessage("Script.setInterval() while shutting down is ignored... parent script:" + getFilename(), fileName, lineNumber); | |
| 1210 | 1261 | return NULL; // bail early |
| 1211 | 1262 | } |
| 1212 | 1263 | |
| @@ -1215,7 +1266,14 @@ QTimer* ScriptManager::setInterval(const ScriptValue& function, int intervalMS) · 2 unmodified lines | ||
| 1215 | 1266 | |
| 1216 | 1267 | QTimer* ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) { |
| 1217 | 1268 | if (isStopped()) { |
| 1218 | − scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename()); | |
| 1269 | + int lineNumber = -1; | |
| 1270 | + QString fileName = getFilename(); | |
| 1271 | + auto context = _engine->currentContext(); | |
| 1272 | + if (context) { | |
| 1273 | + lineNumber = context->currentLineNumber(); | |
| 1274 | + fileName = context->currentFileName(); | |
| 1275 | + } | |
| 1276 | + scriptWarningMessage("Script.setTimeout() while shutting down is ignored... parent script:" + getFilename(), fileName, lineNumber); | |
| 1219 | 1277 | return NULL; // bail early |
| 1220 | 1278 | } |
| 1221 | 1279 | |
| @@ -1281,7 +1339,7 @@ QUrl ScriptManager::resourcesPath() const { · 59 unmodified lines | ||
| 1281 | 1339 | } |
| 1282 | 1340 | |
| 1283 | 1341 | void ScriptManager::print(const QString& message) { |
| 1284 | − emit printedMessage(message, getFilename()); | |
| 1342 | + emit scriptPrintedMessage(message, getFilename(), engine()->currentContext()->currentLineNumber()); | |
| 1285 | 1343 | } |
| 1286 | 1344 | |
| 1287 | 1345 | |
| @@ -1651,8 +1709,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 363 unmodified lines | ||
| 1651 | 1709 | return; |
| 1652 | 1710 | } |
| 1653 | 1711 | if (isStopped()) { |
| 1712 | + int lineNumber = -1; | |
| 1713 | + QString fileName = getFilename(); | |
| 1714 | + auto context = _engine->currentContext(); | |
| 1715 | + if (context) { | |
| 1716 | + lineNumber = context->currentLineNumber(); | |
| 1717 | + fileName = context->currentFileName(); | |
| 1718 | + } | |
| 1654 | 1719 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:" |
| 1655 | − + includeFiles.join(",") + "parent script:" + getFilename()); | |
| 1720 | + + includeFiles.join(",") + "parent script:" + getFilename(), fileName, lineNumber); | |
| 1656 | 1721 | return; // bail early |
| 1657 | 1722 | } |
| 1658 | 1723 | QList<QUrl> urls; |
| @@ -1665,8 +1730,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 6 unmodified lines | ||
| 1665 | 1730 | thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file))); |
| 1666 | 1731 | QUrl defaultScriptsLoc = PathUtils::defaultScriptsLocation(); |
| 1667 | 1732 | if (!defaultScriptsLoc.isParentOf(thisURL)) { |
| 1733 | + int lineNumber = -1; | |
| 1734 | + QString fileName = getFilename(); | |
| 1735 | + auto context = _engine->currentContext(); | |
| 1736 | + if (context) { | |
| 1737 | + lineNumber = context->currentLineNumber(); | |
| 1738 | + fileName = context->currentFileName(); | |
| 1739 | + } | |
| 1668 | 1740 | //V8TODO this probably needs to be done per context, otherwise file cannot be included again in a module |
| 1669 | − scriptWarningMessage("Script.include() -- skipping" + file + "-- outside of standard libraries"); | |
| 1741 | + scriptWarningMessage("Script.include() -- skipping" + file + "-- outside of standard libraries", fileName, lineNumber); | |
| 1670 | 1742 | continue; |
| 1671 | 1743 | } |
| 1672 | 1744 | isStandardLibrary = true; |
| @@ -1676,8 +1748,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 3 unmodified lines | ||
| 1676 | 1748 | |
| 1677 | 1749 | bool disallowOutsideFiles = thisURL.isLocalFile() && !isStandardLibrary && !currentSandboxURL.isLocalFile(); |
| 1678 | 1750 | if (disallowOutsideFiles && !PathUtils::isDescendantOf(thisURL, currentSandboxURL)) { |
| 1751 | + int lineNumber = -1; | |
| 1752 | + QString fileName = currentSandboxURL.toString(); | |
| 1753 | + auto context = _engine->currentContext(); | |
| 1754 | + if (context) { | |
| 1755 | + lineNumber = context->currentLineNumber(); | |
| 1756 | + fileName = context->currentFileName(); | |
| 1757 | + } | |
| 1679 | 1758 | scriptWarningMessage("Script.include() ignoring file path" + thisURL.toString() |
| 1680 | − + "outside of original entity script" + currentSandboxURL.toString()); | |
| 1759 | + + "outside of original entity script" + currentSandboxURL.toString(), fileName, lineNumber); | |
| 1681 | 1760 | } else { |
| 1682 | 1761 | // We could also check here for CORS, but we don't yet. |
| 1683 | 1762 | // It turns out that QUrl.resolve will not change hosts and copy authority, so we don't need to check that here. |
| @@ -1699,7 +1778,14 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 15 unmodified lines | ||
| 1699 | 1778 | for (QUrl url : urls) { |
| 1700 | 1779 | QString contents = data[url]; |
| 1701 | 1780 | if (contents.isNull()) { |
| 1702 | − scriptErrorMessage("Error loading file (" + status[url] +"): " + url.toString()); | |
| 1781 | + int lineNumber = -1; | |
| 1782 | + QString fileName = url.toString(); | |
| 1783 | + auto context = _engine->currentContext(); | |
| 1784 | + if (context) { | |
| 1785 | + lineNumber = context->currentLineNumber(); | |
| 1786 | + fileName = context->currentFileName(); | |
| 1787 | + } | |
| 1788 | + scriptErrorMessage("Error loading file (" + status[url] +"): " + url.toString(), fileName, lineNumber); | |
| 1703 | 1789 | } else { |
| 1704 | 1790 | std::lock_guard<std::recursive_mutex> lock(_lock); |
| 1705 | 1791 | if (!_includedURLs.contains(url)) { |
| @@ -1719,7 +1805,14 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 13 unmodified lines | ||
| 1719 | 1805 | _engine->clearExceptions(); |
| 1720 | 1806 | } |
| 1721 | 1807 | } else { |
| 1722 | − scriptPrintedMessage("Script.include() skipping evaluation of previously included url:" + url.toString()); | |
| 1808 | + int lineNumber = -1; | |
| 1809 | + QString fileName = url.toString(); | |
| 1810 | + auto context = _engine->currentContext(); | |
| 1811 | + if (context) { | |
| 1812 | + lineNumber = context->currentLineNumber(); | |
| 1813 | + fileName = context->currentFileName(); | |
| 1814 | + } | |
| 1815 | + scriptPrintedMessage("Script.include() skipping evaluation of previously included url:" + url.toString(), fileName, lineNumber); | |
| 1723 | 1816 | } |
| 1724 | 1817 | } |
| 1725 | 1818 | } |
| @@ -1748,8 +1841,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 22 unmodified lines | ||
| 1748 | 1841 | |
| 1749 | 1842 | void ScriptManager::include(const QString& includeFile, const ScriptValue& callback) { |
| 1750 | 1843 | if (isStopped()) { |
| 1844 | + int lineNumber = -1; | |
| 1845 | + QString fileName = currentSandboxURL.toString(); | |
| 1846 | + auto context = _engine->currentContext(); | |
| 1847 | + if (context) { | |
| 1848 | + lineNumber = context->currentLineNumber(); | |
| 1849 | + fileName = context->currentFileName(); | |
| 1850 | + } | |
| 1751 | 1851 | scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:" |
| 1752 | − + includeFile + "parent script:" + getFilename()); | |
| 1852 | + + includeFile + "parent script:" + getFilename(), fileName, lineNumber); | |
| 1753 | 1853 | return; // bail early |
| 1754 | 1854 | } |
| 1755 | 1855 | |
| @@ -1765,14 +1865,21 @@ void ScriptManager::load(const QString& loadFile) { · 9 unmodified lines | ||
| 1765 | 1865 | if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) { |
| 1766 | 1866 | return; |
| 1767 | 1867 | } |
| 1868 | + int lineNumber = -1; | |
| 1869 | + QString fileName = getFilename(); | |
| 1870 | + auto context = _engine->currentContext(); | |
| 1871 | + if (context) { | |
| 1872 | + lineNumber = context->currentLineNumber(); | |
| 1873 | + fileName = context->currentFileName(); | |
| 1874 | + } | |
| 1768 | 1875 | if (isStopped()) { |
| 1769 | 1876 | scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:" |
| 1770 | − + loadFile + "parent script:" + getFilename()); | |
| 1877 | + + loadFile + "parent script:" + getFilename(), fileName, lineNumber); | |
| 1771 | 1878 | return; // bail early |
| 1772 | 1879 | } |
| 1773 | 1880 | if (!currentEntityIdentifier.isInvalidID()) { |
| 1774 | 1881 | scriptWarningMessage("Script.load() from entity script is ignored... loadFile:" |
| 1775 | − + loadFile + "parent script:" + getFilename() + "entity: " + currentEntityIdentifier.toString()); | |
| 1882 | + + loadFile + "parent script:" + getFilename() + "entity: " + currentEntityIdentifier.toString(), fileName, lineNumber); | |
| 1776 | 1883 | return; // bail early |
| 1777 | 1884 | } |
| 1778 | 1885 | |
| @@ -2440,7 +2547,7 @@ void ScriptManager::refreshFileScript(const EntityItemID& entityID) { · 661 unmodified lines | ||
| 2440 | 2547 | QString filePath = QUrl(details.scriptText).toLocalFile(); |
| 2441 | 2548 | auto lastModified = QFileInfo(filePath).lastModified().toMSecsSinceEpoch(); |
| 2442 | 2549 | if (lastModified > details.lastModified) { |
| 2443 | − scriptInfoMessage("Reloading modified script " + details.scriptText); | |
| 2550 | + scriptInfoMessage("Reloading modified script " + details.scriptText, filePath, -1); | |
| 2444 | 2551 | loadEntityScript(entityID, details.scriptText, true); |
| 2445 | 2552 | } |
| 2446 | 2553 | } |