History

Merge branch master into protocol_changes

HifiExperiments committed 2 years ago

Commit ee705d21 parent f9e729b370 files · +11827 −6045Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 370 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+132−25
@@ -225,7 +225,12 @@ QString encodeEntityIdIntoEntityUrl(const QString& url, const QString& entityID) · 224 unmodified lines
225 225
226QString ScriptManager::logException(const ScriptValue& exception) {226QString ScriptManager::logException(const ScriptValue& exception) {
227 auto message = formatException(exception, _enableExtendedJSExceptions.get());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 return message;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 if (!_areMetaTypesInitialized) {343 if (!_areMetaTypesInitialized) {
334 initMetaTypes();344 initMetaTypes();
335 }345 }
@@ -468,10 +478,10 @@ void ScriptManager::waitTillDoneRunning(bool shutdown) { · 132 unmodified lines
468 }478 }
469 }479 }
470#else480#else
471 auto startedWaiting = usecTimestampNow();481 //auto startedWaiting = usecTimestampNow();
472 while (!_isDoneRunning) {482 while (!_isDoneRunning) {
473 // If the final evaluation takes too long, then tell the script engine to stop running483 // 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 // TODO: This part was very unsafe and was causing crashes all the time.485 // TODO: This part was very unsafe and was causing crashes all the time.
476 // I disabled it for now until we find a safer solution.486 // I disabled it for now until we find a safer solution.
477 // With it disabled now we get clean shutdowns and restarts.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#endif525#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 return lastPart;542 return lastPart;
533}543}
534 544
545QString ScriptManager::getAbsoluteFilename() const {
546 return _fileNameString;
547}
548
535bool ScriptManager::hasValidScriptSuffix(const QString& scriptFileName) {549bool ScriptManager::hasValidScriptSuffix(const QString& scriptFileName) {
536 QFileInfo fileInfo(scriptFileName);550 QFileInfo fileInfo(scriptFileName);
537 QString scriptSuffixToLower = fileInfo.completeSuffix().toLower();551 QString scriptSuffixToLower = fileInfo.completeSuffix().toLower();
@@ -549,7 +563,7 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 11 unmodified lines
549 563
550 // Check that script has a supported file extension564 // Check that script has a supported file extension
551 if (!hasValidScriptSuffix(_fileNameString)) {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 emit errorLoadingScript(_fileNameString);567 emit errorLoadingScript(_fileNameString);
554 return;568 return;
555 }569 }
@@ -559,7 +573,7 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 3 unmodified lines
559 scriptCache->getScriptContents(url.toString(), [this](const QString& url, const QString& scriptContents, bool isURL, bool success, const QString&status) {573 scriptCache->getScriptContents(url.toString(), [this](const QString& url, const QString& scriptContents, bool isURL, bool success, const QString&status) {
560 qCDebug(scriptengine) << "loadURL" << url << status << QThread::currentThread();574 qCDebug(scriptengine) << "loadURL" << url << status << QThread::currentThread();
561 if (!success) {575 if (!success) {
562 scriptErrorMessage("ERROR Loading file (" + status + "):" + url);576 scriptErrorMessage("ERROR Loading file (" + status + "):" + url, url, -1);
563 emit errorLoadingScript(_fileNameString);577 emit errorLoadingScript(_fileNameString);
564 return;578 return;
565 }579 }
@@ -570,24 +584,54 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) { · 4 unmodified lines
570 }, reload, maxRetries);584 }, reload, maxRetries);
571}585}
572 586
573void ScriptManager::scriptErrorMessage(const QString& message) {587void ScriptManager::scriptErrorMessage(const QString& message, const QString& fileName, int lineNumber) {
574 qCCritical(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));588 qCCritical(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
575 emit errorMessage(message, getFilename());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
578void ScriptManager::scriptWarningMessage(const QString& message) {599void ScriptManager::scriptWarningMessage(const QString& message, const QString& fileName, int lineNumber) {
579 qCWarning(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));600 qCWarning(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
580 emit warningMessage(message, getFilename());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
583void ScriptManager::scriptInfoMessage(const QString& message) {611void ScriptManager::scriptInfoMessage(const QString& message, const QString& fileName, int lineNumber) {
584 qCInfo(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));612 qCInfo(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
585 emit infoMessage(message, getFilename());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
588void ScriptManager::scriptPrintedMessage(const QString& message) {623void ScriptManager::scriptPrintedMessage(const QString& message, const QString& fileName, int lineNumber) {
589 qCDebug(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));624 qCDebug(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
590 emit printedMessage(message, getFilename());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
593void ScriptManager::clearDebugLogWindow() {637void ScriptManager::clearDebugLogWindow() {
@@ -912,7 +956,7 @@ void ScriptManager::run() { · 318 unmodified lines
912 return; // bail early - avoid setting state in init(), as evaluate() will bail too956 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 if (!_isInitialized) {961 if (!_isInitialized) {
918 init();962 init();
@@ -1064,7 +1108,7 @@ void ScriptManager::run() { · 145 unmodified lines
1064 _engine->clearExceptions();1108 _engine->clearExceptions();
1065 }1109 }
1066 }1110 }
1067 scriptInfoMessage("Script Engine stopping:" + getFilename());1111 scriptInfoMessage("Script Engine stopping:" + getFilename(), getFilename(), -1);
1068 1112
1069 stopAllTimers(); // make sure all our timers are stopped if the script is ending1113 stopAllTimers(); // make sure all our timers are stopped if the script is ending
1070 emit scriptEnding();1114 emit scriptEnding();
@@ -1139,7 +1183,7 @@ void ScriptManager::updateMemoryCost(const qint64& deltaSize) { · 68 unmodified lines
1139 1183
1140void ScriptManager::timerFired() {1184void ScriptManager::timerFired() {
1141 if (isStopped()) {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 return; // bail early1187 return; // bail early
1144 }1188 }
1145 1189
@@ -1206,7 +1250,14 @@ QTimer* ScriptManager::setupTimerWithInterval(const ScriptValue& function, int i · 60 unmodified lines
1206 1250
1207QTimer* ScriptManager::setInterval(const ScriptValue& function, int intervalMS) {1251QTimer* ScriptManager::setInterval(const ScriptValue& function, int intervalMS) {
1208 if (isStopped()) {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 return NULL; // bail early1261 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
1216QTimer* ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) {1267QTimer* ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) {
1217 if (isStopped()) {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 return NULL; // bail early1277 return NULL; // bail early
1220 }1278 }
1221 1279
@@ -1281,7 +1339,7 @@ QUrl ScriptManager::resourcesPath() const { · 59 unmodified lines
1281}1339}
1282 1340
1283void ScriptManager::print(const QString& message) {1341void 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 return;1709 return;
1652 }1710 }
1653 if (isStopped()) {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 scriptWarningMessage("Script.include() while shutting down is ignored... includeFiles:"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 return; // bail early1721 return; // bail early
1657 }1722 }
1658 QList<QUrl> urls;1723 QList<QUrl> urls;
@@ -1665,8 +1730,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 6 unmodified lines
1665 thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file)));1730 thisURL = expandScriptUrl(QUrl::fromLocalFile(expandScriptPath(file)));
1666 QUrl defaultScriptsLoc = PathUtils::defaultScriptsLocation();1731 QUrl defaultScriptsLoc = PathUtils::defaultScriptsLocation();
1667 if (!defaultScriptsLoc.isParentOf(thisURL)) {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 //V8TODO this probably needs to be done per context, otherwise file cannot be included again in a module1740 //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 continue;1742 continue;
1671 }1743 }
1672 isStandardLibrary = true;1744 isStandardLibrary = true;
@@ -1676,8 +1748,15 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 3 unmodified lines
1676 1748
1677 bool disallowOutsideFiles = thisURL.isLocalFile() && !isStandardLibrary && !currentSandboxURL.isLocalFile();1749 bool disallowOutsideFiles = thisURL.isLocalFile() && !isStandardLibrary && !currentSandboxURL.isLocalFile();
1678 if (disallowOutsideFiles && !PathUtils::isDescendantOf(thisURL, currentSandboxURL)) {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 scriptWarningMessage("Script.include() ignoring file path" + thisURL.toString()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 } else {1760 } else {
1682 // We could also check here for CORS, but we don't yet.1761 // We could also check here for CORS, but we don't yet.
1683 // It turns out that QUrl.resolve will not change hosts and copy authority, so we don't need to check that here.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 for (QUrl url : urls) {1778 for (QUrl url : urls) {
1700 QString contents = data[url];1779 QString contents = data[url];
1701 if (contents.isNull()) {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 } else {1789 } else {
1704 std::lock_guard<std::recursive_mutex> lock(_lock);1790 std::lock_guard<std::recursive_mutex> lock(_lock);
1705 if (!_includedURLs.contains(url)) {1791 if (!_includedURLs.contains(url)) {
@@ -1719,7 +1805,14 @@ void ScriptManager::include(const QStringList& includeFiles, const ScriptValue& · 13 unmodified lines
1719 _engine->clearExceptions();1805 _engine->clearExceptions();
1720 }1806 }
1721 } else {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
1749void ScriptManager::include(const QString& includeFile, const ScriptValue& callback) {1842void ScriptManager::include(const QString& includeFile, const ScriptValue& callback) {
1750 if (isStopped()) {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 scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:"1851 scriptWarningMessage("Script.include() while shutting down is ignored... includeFile:"
1752 + includeFile + "parent script:" + getFilename());1852 + includeFile + "parent script:" + getFilename(), fileName, lineNumber);
1753 return; // bail early1853 return; // bail early
1754 }1854 }
1755 1855
@@ -1765,14 +1865,21 @@ void ScriptManager::load(const QString& loadFile) { · 9 unmodified lines
1765 if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) {1865 if (!_engine->IS_THREADSAFE_INVOCATION(__FUNCTION__)) {
1766 return;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 if (isStopped()) {1875 if (isStopped()) {
1769 scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:"1876 scriptWarningMessage("Script.load() while shutting down is ignored... loadFile:"
1770 + loadFile + "parent script:" + getFilename());1877 + loadFile + "parent script:" + getFilename(), fileName, lineNumber);
1771 return; // bail early1878 return; // bail early
1772 }1879 }
1773 if (!currentEntityIdentifier.isInvalidID()) {1880 if (!currentEntityIdentifier.isInvalidID()) {
1774 scriptWarningMessage("Script.load() from entity script is ignored... loadFile:"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 return; // bail early1883 return; // bail early
1777 }1884 }
1778 1885
@@ -2440,7 +2547,7 @@ void ScriptManager::refreshFileScript(const EntityItemID& entityID) { · 661 unmodified lines
2440 QString filePath = QUrl(details.scriptText).toLocalFile();2547 QString filePath = QUrl(details.scriptText).toLocalFile();
2441 auto lastModified = QFileInfo(filePath).lastModified().toMSecsSinceEpoch();2548 auto lastModified = QFileInfo(filePath).lastModified().toMSecsSinceEpoch();
2442 if (lastModified > details.lastModified) {2549 if (lastModified > details.lastModified) {
2443 scriptInfoMessage("Reloading modified script " + details.scriptText);2550 scriptInfoMessage("Reloading modified script " + details.scriptText, filePath, -1);
2444 loadEntityScript(entityID, details.scriptText, true);2551 loadEntityScript(entityID, details.scriptText, true);
2445 }2552 }
2446 }2553 }