Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 3 files). Show all
Unified · Split
| @@ -1374,6 +1374,15 @@ void ScriptEngine::forwardHandlerCall(const EntityItemID& entityID, const QStrin · 1373 unmodified lines | |||
| 1374 | } | 1374 | } |
| 1375 | } | 1375 | } |
| 1376 | 1376 | ||
| 1377 | bool ScriptEngine::getEntityScriptDetails(const EntityItemID& entityID, EntityScriptDetails &details) const { | ||
| 1378 | auto it = _entityScripts.constFind(entityID); | ||
| 1379 | if (it == _entityScripts.constEnd()) { | ||
| 1380 | return false; | ||
| 1381 | } | ||
| 1382 | details = it.value(); | ||
| 1383 | return true; | ||
| 1384 | } | ||
| 1385 | |||
| 1377 | // since all of these operations can be asynch we will always do the actual work in the response handler | 1386 | // since all of these operations can be asynch we will always do the actual work in the response handler |
| 1378 | // for the download | 1387 | // for the download |
| 1379 | void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { | 1388 | void ScriptEngine::loadEntityScript(QWeakPointer<ScriptEngine> theEngine, const EntityItemID& entityID, const QString& entityScript, bool forceRedownload) { |
| @@ -1421,11 +1430,24 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 41 unmodified lines | |||
| 1421 | bool isFileUrl = isURL && scriptOrURL.startsWith("file://"); | 1430 | bool isFileUrl = isURL && scriptOrURL.startsWith("file://"); |
| 1422 | auto fileName = isURL ? scriptOrURL : "EmbeddedEntityScript"; | 1431 | auto fileName = isURL ? scriptOrURL : "EmbeddedEntityScript"; |
| 1423 | 1432 | ||
| 1433 | EntityScriptDetails newDetails; | ||
| 1434 | newDetails.scriptText = isURL ? contents : scriptOrURL; | ||
| 1435 | |||
| 1436 | if (!success) { | ||
| 1437 | newDetails.status = ERROR_LOADING_SCRIPT; | ||
| 1438 | newDetails.errorInfo = "Failed to load script"; | ||
| 1439 | _entityScripts[entityID] = newDetails; | ||
| 1440 | return; | ||
| 1441 | } | ||
| 1442 | |||
| 1424 | QScriptProgram program(contents, fileName); | 1443 | QScriptProgram program(contents, fileName); |
| 1425 | if (!hasCorrectSyntax(program, this)) { | 1444 | if (!hasCorrectSyntax(program, this)) { |
| 1426 | if (!isFileUrl) { | 1445 | if (!isFileUrl) { |
| 1427 | scriptCache->addScriptToBadScriptList(scriptOrURL); | 1446 | scriptCache->addScriptToBadScriptList(scriptOrURL); |
| 1428 | } | 1447 | } |
| 1448 | newDetails.status = ERROR_RUNNING_SCRIPT; | ||
| 1449 | newDetails.errorInfo = "Bad syntax"; | ||
| 1450 | _entityScripts[entityID] = newDetails; | ||
| 1429 | return; // done processing script | 1451 | return; // done processing script |
| 1430 | } | 1452 | } |
| 1431 | 1453 | ||
| @@ -1451,6 +1473,10 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 19 unmodified lines | |||
| 1451 | testConstructor = sandbox.evaluate(program); | 1473 | testConstructor = sandbox.evaluate(program); |
| 1452 | } | 1474 | } |
| 1453 | if (hadUncaughtExceptions(sandbox, program.fileName(), this)) { | 1475 | if (hadUncaughtExceptions(sandbox, program.fileName(), this)) { |
| 1476 | newDetails.status = ERROR_RUNNING_SCRIPT; | ||
| 1477 | newDetails.errorInfo = "Exception"; | ||
| 1478 | _entityScripts[entityID] = newDetails; | ||
| 1479 | |||
| 1454 | return; | 1480 | return; |
| 1455 | } | 1481 | } |
| 1456 | 1482 | ||
| @@ -1473,6 +1499,10 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 16 unmodified lines | |||
| 1473 | scriptCache->addScriptToBadScriptList(scriptOrURL); | 1499 | scriptCache->addScriptToBadScriptList(scriptOrURL); |
| 1474 | } | 1500 | } |
| 1475 | 1501 | ||
| 1502 | newDetails.status = ERROR_RUNNING_SCRIPT; | ||
| 1503 | newDetails.errorInfo = "Could not find constructor"; | ||
| 1504 | _entityScripts[entityID] = newDetails; | ||
| 1505 | |||
| 1476 | return; // done processing script | 1506 | return; // done processing script |
| 1477 | } | 1507 | } |
| 1478 | 1508 | ||
| @@ -1489,8 +1519,11 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 10 unmodified lines | |||
| 1489 | }; | 1519 | }; |
| 1490 | doWithEnvironment(entityID, sandboxURL, initialization); | 1520 | doWithEnvironment(entityID, sandboxURL, initialization); |
| 1491 | 1521 | ||
| 1492 | EntityScriptDetails newDetails = { scriptOrURL, entityScriptObject, lastModified, sandboxURL }; | 1522 | newDetails.scriptObject = entityScriptObject; |
| 1523 | newDetails.lastModified = lastModified; | ||
| 1524 | newDetails.definingSandboxURL = sandboxURL; | ||
| 1493 | _entityScripts[entityID] = newDetails; | 1525 | _entityScripts[entityID] = newDetails; |
| 1526 | |||
| 1494 | if (isURL) { | 1527 | if (isURL) { |
| 1495 | setParentURL(""); | 1528 | setParentURL(""); |
| 1496 | } | 1529 | } |
| @@ -1516,7 +1549,9 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID) { · 19 unmodified lines | |||
| 1516 | #endif | 1549 | #endif |
| 1517 | 1550 | ||
| 1518 | if (_entityScripts.contains(entityID)) { | 1551 | if (_entityScripts.contains(entityID)) { |
| 1519 | callEntityScriptMethod(entityID, "unload"); | 1552 | if (_entityScripts[entityID].status == RUNNING) { |
| 1553 | callEntityScriptMethod(entityID, "unload"); | ||
| 1554 | } | ||
| 1520 | _entityScripts.remove(entityID); | 1555 | _entityScripts.remove(entityID); |
| 1521 | stopAllTimersForEntityScript(entityID); | 1556 | stopAllTimersForEntityScript(entityID); |
| 1522 | } | 1557 | } |
| @@ -1535,7 +1570,9 @@ void ScriptEngine::unloadAllEntityScripts() { · 12 unmodified lines | |||
| 1535 | qCDebug(scriptengine) << "ScriptEngine::unloadAllEntityScripts() called on correct thread [" << thread() << "]"; | 1570 | qCDebug(scriptengine) << "ScriptEngine::unloadAllEntityScripts() called on correct thread [" << thread() << "]"; |
| 1536 | #endif | 1571 | #endif |
| 1537 | foreach(const EntityItemID& entityID, _entityScripts.keys()) { | 1572 | foreach(const EntityItemID& entityID, _entityScripts.keys()) { |
| 1538 | callEntityScriptMethod(entityID, "unload"); | 1573 | if (_entityScripts[entityID].status == RUNNING) { |
| 1574 | callEntityScriptMethod(entityID, "unload"); | ||
| 1575 | } | ||
| 1539 | } | 1576 | } |
| 1540 | _entityScripts.clear(); | 1577 | _entityScripts.clear(); |
| 1541 | 1578 | ||
| @@ -1574,7 +1611,7 @@ void ScriptEngine::refreshFileScript(const EntityItemID& entityID) { · 32 unmodified lines | |||
| 1574 | QString scriptContents = QTextStream(&file).readAll(); | 1611 | QString scriptContents = QTextStream(&file).readAll(); |
| 1575 | this->unloadEntityScript(entityID); | 1612 | this->unloadEntityScript(entityID); |
| 1576 | this->entityScriptContentAvailable(entityID, details.scriptText, scriptContents, true, true); | 1613 | this->entityScriptContentAvailable(entityID, details.scriptText, scriptContents, true, true); |
| 1577 | if (!_entityScripts.contains(entityID)) { | 1614 | if (!_entityScripts.contains(entityID) || _entityScripts[entityID].status != RUNNING) { |
| 1578 | scriptWarningMessage("Reload script " + details.scriptText + " failed"); | 1615 | scriptWarningMessage("Reload script " + details.scriptText + " failed"); |
| 1579 | } else { | 1616 | } else { |
| 1580 | details = _entityScripts[entityID]; | 1617 | details = _entityScripts[entityID]; |
| @@ -1633,7 +1670,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 52 unmodified lines | |||
| 1633 | #endif | 1670 | #endif |
| 1634 | 1671 | ||
| 1635 | refreshFileScript(entityID); | 1672 | refreshFileScript(entityID); |
| 1636 | if (_entityScripts.contains(entityID)) { | 1673 | if (_entityScripts.contains(entityID) && _entityScripts[entityID].status == RUNNING) { |
| 1637 | EntityScriptDetails details = _entityScripts[entityID]; | 1674 | EntityScriptDetails details = _entityScripts[entityID]; |
| 1638 | QScriptValue entityScript = details.scriptObject; // previously loaded | 1675 | QScriptValue entityScript = details.scriptObject; // previously loaded |
| 1639 | if (entityScript.property(methodName).isFunction()) { | 1676 | if (entityScript.property(methodName).isFunction()) { |
| @@ -1665,7 +1702,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 25 unmodified lines | |||
| 1665 | #endif | 1702 | #endif |
| 1666 | 1703 | ||
| 1667 | refreshFileScript(entityID); | 1704 | refreshFileScript(entityID); |
| 1668 | if (_entityScripts.contains(entityID)) { | 1705 | if (_entityScripts.contains(entityID) && _entityScripts[entityID].status == RUNNING) { |
| 1669 | EntityScriptDetails details = _entityScripts[entityID]; | 1706 | EntityScriptDetails details = _entityScripts[entityID]; |
| 1670 | QScriptValue entityScript = details.scriptObject; // previously loaded | 1707 | QScriptValue entityScript = details.scriptObject; // previously loaded |
| 1671 | if (entityScript.property(methodName).isFunction()) { | 1708 | if (entityScript.property(methodName).isFunction()) { |
| @@ -1698,7 +1735,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 26 unmodified lines | |||
| 1698 | #endif | 1735 | #endif |
| 1699 | 1736 | ||
| 1700 | refreshFileScript(entityID); | 1737 | refreshFileScript(entityID); |
| 1701 | if (_entityScripts.contains(entityID)) { | 1738 | if (_entityScripts.contains(entityID) && _entityScripts[entityID].status == RUNNING) { |
| 1702 | EntityScriptDetails details = _entityScripts[entityID]; | 1739 | EntityScriptDetails details = _entityScripts[entityID]; |
| 1703 | QScriptValue entityScript = details.scriptObject; // previously loaded | 1740 | QScriptValue entityScript = details.scriptObject; // previously loaded |
| 1704 | if (entityScript.property(methodName).isFunction()) { | 1741 | if (entityScript.property(methodName).isFunction()) { |