Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 34 files). Show all
Unified · Split
| @@ -4,6 +4,7 @@ · 3 unmodified lines | |||
| 4 | // | 4 | // |
| 5 | // Created by Brad Hefta-Gaub on 12/14/13. | 5 | // Created by Brad Hefta-Gaub on 12/14/13. |
| 6 | // Copyright 2013 High Fidelity, Inc. | 6 | // Copyright 2013 High Fidelity, Inc. |
| 7 | // Copyright 2022 Overte e.V. | ||
| 7 | // | 8 | // |
| 8 | // Distributed under the Apache License, Version 2.0. | 9 | // Distributed under the Apache License, Version 2.0. |
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 10 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| @@ -88,6 +89,7 @@ Q_DECLARE_METATYPE(ScriptValue); · 78 unmodified lines | |||
| 88 | // --- Static script initialization registry | 89 | // --- Static script initialization registry |
| 89 | 90 | ||
| 90 | static ScriptManager::StaticInitializerNode* rootInitializer = nullptr; | 91 | static ScriptManager::StaticInitializerNode* rootInitializer = nullptr; |
| 92 | static ScriptManager::StaticTypesInitializerNode* rootTypesInitializer = nullptr; | ||
| 91 | 93 | ||
| 92 | void ScriptManager::registerNewStaticInitializer(StaticInitializerNode* dest) { | 94 | void ScriptManager::registerNewStaticInitializer(StaticInitializerNode* dest) { |
| 93 | // this function is assumed to be called on LoadLibrary, where we are explicitly operating in single-threaded mode | 95 | // this function is assumed to be called on LoadLibrary, where we are explicitly operating in single-threaded mode |
| @@ -102,6 +104,19 @@ static void runStaticInitializers(ScriptManager* manager) { · 8 unmodified lines | |||
| 102 | here = here->prev; | 104 | here = here->prev; |
| 103 | } | 105 | } |
| 104 | } | 106 | } |
| 107 | void ScriptManager::registerNewStaticTypesInitializer(StaticTypesInitializerNode* dest) { | ||
| 108 | // this function is assumed to be called on LoadLibrary, where we are explicitly operating in single-threaded mode | ||
| 109 | // Therefore there is no mutex or threadsafety here and the structure is assumed not to change after loading | ||
| 110 | dest->prev = rootTypesInitializer; | ||
| 111 | rootTypesInitializer = dest; | ||
| 112 | } | ||
| 113 | static void runStaticTypesInitializers(ScriptManager* manager) { | ||
| 114 | ScriptManager::StaticTypesInitializerNode* here = rootTypesInitializer; | ||
| 115 | while (here != nullptr) { | ||
| 116 | (*here->init)(manager); | ||
| 117 | here = here->prev; | ||
| 118 | } | ||
| 119 | } | ||
| 105 | 120 | ||
| 106 | // --- | 121 | // --- |
| 107 | 122 | ||
| @@ -210,12 +225,11 @@ ScriptManagerPointer newScriptManager(ScriptManager::Context context, · 102 unmodified lines | |||
| 210 | const QString& fileNameString) { | 225 | const QString& fileNameString) { |
| 211 | ScriptManagerPointer manager(new ScriptManager(context, scriptContents, fileNameString), | 226 | ScriptManagerPointer manager(new ScriptManager(context, scriptContents, fileNameString), |
| 212 | [](ScriptManager* obj) { obj->deleteLater(); }); | 227 | [](ScriptManager* obj) { obj->deleteLater(); }); |
| 213 | ScriptEnginePointer engine = newScriptEngine(manager.get()); | ||
| 214 | manager->_engine = engine; | ||
| 215 | return manager; | 228 | return manager; |
| 216 | } | 229 | } |
| 217 | 230 | ||
| 218 | int ScriptManager::processLevelMaxRetries { ScriptRequest::MAX_RETRIES }; | 231 | int ScriptManager::processLevelMaxRetries { ScriptRequest::MAX_RETRIES }; |
| 232 | |||
| 219 | ScriptManager::ScriptManager(Context context, const QString& scriptContents, const QString& fileNameString) : | 233 | ScriptManager::ScriptManager(Context context, const QString& scriptContents, const QString& fileNameString) : |
| 220 | QObject(), | 234 | QObject(), |
| 221 | _context(context), | 235 | _context(context), |
| @@ -267,6 +281,10 @@ ScriptManager::ScriptManager(Context context, const QString& scriptContents, con · 45 unmodified lines | |||
| 267 | } | 281 | } |
| 268 | }); | 282 | }); |
| 269 | } | 283 | } |
| 284 | |||
| 285 | if (!_areMetaTypesInitialized) { | ||
| 286 | initMetaTypes(); | ||
| 287 | } | ||
| 270 | } | 288 | } |
| 271 | 289 | ||
| 272 | QString ScriptManager::getTypeAsString() const { | 290 | QString ScriptManager::getTypeAsString() const { |
| @@ -615,17 +633,13 @@ void ScriptManager::resetModuleCache(bool deleteScriptCache) { · 342 unmodified lines | |||
| 615 | jsRequire.setProperty("cache", cache, READONLY_PROP_FLAGS); | 633 | jsRequire.setProperty("cache", cache, READONLY_PROP_FLAGS); |
| 616 | } | 634 | } |
| 617 | 635 | ||
| 618 | void ScriptManager::init() { | 636 | void ScriptManager::initMetaTypes() { |
| 619 | if (_isInitialized) { | 637 | if (_areMetaTypesInitialized) { |
| 620 | return; // only initialize once | 638 | return; |
| 621 | } | 639 | } |
| 622 | 640 | _areMetaTypesInitialized = true; | |
| 623 | _isInitialized = true; | ||
| 624 | runStaticInitializers(this); | ||
| 625 | |||
| 626 | auto scriptEngine = _engine.get(); | 641 | auto scriptEngine = _engine.get(); |
| 627 | 642 | runStaticTypesInitializers(this); | |
| 628 | // register various meta-types | ||
| 629 | registerMIDIMetaTypes(scriptEngine); | 643 | registerMIDIMetaTypes(scriptEngine); |
| 630 | registerEventTypes(scriptEngine); | 644 | registerEventTypes(scriptEngine); |
| 631 | registerMenuItemProperties(scriptEngine); | 645 | registerMenuItemProperties(scriptEngine); |
| @@ -636,6 +650,29 @@ void ScriptManager::init() { · 4 unmodified lines | |||
| 636 | scriptRegisterSequenceMetaType<QVector<glm::vec2>>(scriptEngine); | 650 | scriptRegisterSequenceMetaType<QVector<glm::vec2>>(scriptEngine); |
| 637 | scriptRegisterSequenceMetaType<QVector<glm::quat>>(scriptEngine); | 651 | scriptRegisterSequenceMetaType<QVector<glm::quat>>(scriptEngine); |
| 638 | scriptRegisterSequenceMetaType<QVector<QString>>(scriptEngine); | 652 | scriptRegisterSequenceMetaType<QVector<QString>>(scriptEngine); |
| 653 | |||
| 654 | scriptRegisterMetaType(scriptEngine, animationDetailsToScriptValue, animationDetailsFromScriptValue); | ||
| 655 | scriptRegisterMetaType(scriptEngine, webSocketToScriptValue, webSocketFromScriptValue); | ||
| 656 | scriptRegisterMetaType(scriptEngine, qWSCloseCodeToScriptValue, qWSCloseCodeFromScriptValue); | ||
| 657 | scriptRegisterMetaType(scriptEngine, wscReadyStateToScriptValue, wscReadyStateFromScriptValue); | ||
| 658 | |||
| 659 | scriptRegisterMetaType(scriptEngine, externalResourceBucketToScriptValue, externalResourceBucketFromScriptValue); | ||
| 660 | |||
| 661 | scriptRegisterMetaType(scriptEngine, scriptableResourceToScriptValue, scriptableResourceFromScriptValue); | ||
| 662 | |||
| 663 | scriptRegisterMetaType(scriptEngine, meshToScriptValue, meshFromScriptValue); | ||
| 664 | scriptRegisterMetaType(scriptEngine, meshesToScriptValue, meshesFromScriptValue); | ||
| 665 | } | ||
| 666 | |||
| 667 | void ScriptManager::init() { | ||
| 668 | if (_isInitialized) { | ||
| 669 | return; // only initialize once | ||
| 670 | } | ||
| 671 | |||
| 672 | _isInitialized = true; | ||
| 673 | runStaticInitializers(this); | ||
| 674 | |||
| 675 | auto scriptEngine = _engine.get(); | ||
| 639 | 676 | ||
| 640 | ScriptValue xmlHttpRequestConstructorValue = scriptEngine->newFunction(XMLHttpRequestClass::constructor); | 677 | ScriptValue xmlHttpRequestConstructorValue = scriptEngine->newFunction(XMLHttpRequestClass::constructor); |
| 641 | scriptEngine->globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue); | 678 | scriptEngine->globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue); |
| @@ -652,11 +689,6 @@ void ScriptManager::init() { · 10 unmodified lines | |||
| 652 | */ | 689 | */ |
| 653 | scriptEngine->globalObject().setProperty("print", scriptEngine->newFunction(debugPrint)); | 690 | scriptEngine->globalObject().setProperty("print", scriptEngine->newFunction(debugPrint)); |
| 654 | 691 | ||
| 655 | scriptRegisterMetaType(scriptEngine, animationDetailsToScriptValue, animationDetailsFromScriptValue); | ||
| 656 | scriptRegisterMetaType(scriptEngine, webSocketToScriptValue, webSocketFromScriptValue); | ||
| 657 | scriptRegisterMetaType(scriptEngine, qWSCloseCodeToScriptValue, qWSCloseCodeFromScriptValue); | ||
| 658 | scriptRegisterMetaType(scriptEngine, wscReadyStateToScriptValue, wscReadyStateFromScriptValue); | ||
| 659 | |||
| 660 | // NOTE: You do not want to end up creating new instances of singletons here. They will be on the ScriptManager thread | 692 | // NOTE: You do not want to end up creating new instances of singletons here. They will be on the ScriptManager thread |
| 661 | // and are likely to be unusable if we "reset" the ScriptManager by creating a new one (on a whole new thread). | 693 | // and are likely to be unusable if we "reset" the ScriptManager by creating a new one (on a whole new thread). |
| 662 | 694 | ||
| @@ -671,7 +703,6 @@ void ScriptManager::init() { · 8 unmodified lines | |||
| 671 | resetModuleCache(); | 703 | resetModuleCache(); |
| 672 | } | 704 | } |
| 673 | 705 | ||
| 674 | scriptRegisterMetaType(scriptEngine, externalResourceBucketToScriptValue, externalResourceBucketFromScriptValue); | ||
| 675 | scriptEngine->registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>()); | 706 | scriptEngine->registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>()); |
| 676 | 707 | ||
| 677 | scriptEngine->registerGlobalObject("Quat", &_quatLibrary); | 708 | scriptEngine->registerGlobalObject("Quat", &_quatLibrary); |
| @@ -696,7 +727,6 @@ void ScriptManager::init() { · 18 unmodified lines | |||
| 696 | auto resourcePrototype = createScriptableResourcePrototype(shared_from_this()); | 727 | auto resourcePrototype = createScriptableResourcePrototype(shared_from_this()); |
| 697 | scriptEngine->globalObject().setProperty("Resource", resourcePrototype); | 728 | scriptEngine->globalObject().setProperty("Resource", resourcePrototype); |
| 698 | scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype); | 729 | scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype); |
| 699 | scriptRegisterMetaType(scriptEngine, scriptableResourceToScriptValue, scriptableResourceFromScriptValue); | ||
| 700 | 730 | ||
| 701 | // constants | 731 | // constants |
| 702 | scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE)); | 732 | scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE)); |
| @@ -706,9 +736,6 @@ void ScriptManager::init() { · 3 unmodified lines | |||
| 706 | 736 | ||
| 707 | scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance()); | 737 | scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance()); |
| 708 | 738 | ||
| 709 | scriptRegisterMetaType(scriptEngine, meshToScriptValue, meshFromScriptValue); | ||
| 710 | scriptRegisterMetaType(scriptEngine, meshesToScriptValue, meshesFromScriptValue); | ||
| 711 | |||
| 712 | scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data()); | 739 | scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data()); |
| 713 | 740 | ||
| 714 | #if DEV_BUILD || PR_BUILD | 741 | #if DEV_BUILD || PR_BUILD |