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 | 5 | // Created by Brad Hefta-Gaub on 12/14/13. |
| 6 | 6 | // Copyright 2013 High Fidelity, Inc. |
| 7 | +// Copyright 2022 Overte e.V. | |
| 7 | 8 | // |
| 8 | 9 | // Distributed under the Apache License, Version 2.0. |
| 9 | 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 | 89 | // --- Static script initialization registry |
| 89 | 90 | |
| 90 | 91 | static ScriptManager::StaticInitializerNode* rootInitializer = nullptr; |
| 92 | +static ScriptManager::StaticTypesInitializerNode* rootTypesInitializer = nullptr; | |
| 91 | 93 | |
| 92 | 94 | void ScriptManager::registerNewStaticInitializer(StaticInitializerNode* dest) { |
| 93 | 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 | 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 | 225 | const QString& fileNameString) { |
| 211 | 226 | ScriptManagerPointer manager(new ScriptManager(context, scriptContents, fileNameString), |
| 212 | 227 | [](ScriptManager* obj) { obj->deleteLater(); }); |
| 213 | − ScriptEnginePointer engine = newScriptEngine(manager.get()); | |
| 214 | − manager->_engine = engine; | |
| 215 | 228 | return manager; |
| 216 | 229 | } |
| 217 | 230 | |
| 218 | 231 | int ScriptManager::processLevelMaxRetries { ScriptRequest::MAX_RETRIES }; |
| 232 | + | |
| 219 | 233 | ScriptManager::ScriptManager(Context context, const QString& scriptContents, const QString& fileNameString) : |
| 220 | 234 | QObject(), |
| 221 | 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 | 290 | QString ScriptManager::getTypeAsString() const { |
| @@ -615,17 +633,13 @@ void ScriptManager::resetModuleCache(bool deleteScriptCache) { · 342 unmodified lines | ||
| 615 | 633 | jsRequire.setProperty("cache", cache, READONLY_PROP_FLAGS); |
| 616 | 634 | } |
| 617 | 635 | |
| 618 | −void ScriptManager::init() { | |
| 619 | − if (_isInitialized) { | |
| 620 | − return; // only initialize once | |
| 636 | +void ScriptManager::initMetaTypes() { | |
| 637 | + if (_areMetaTypesInitialized) { | |
| 638 | + return; | |
| 621 | 639 | } |
| 622 | − | |
| 623 | − _isInitialized = true; | |
| 624 | − runStaticInitializers(this); | |
| 625 | − | |
| 640 | + _areMetaTypesInitialized = true; | |
| 626 | 641 | auto scriptEngine = _engine.get(); |
| 627 | − | |
| 628 | − // register various meta-types | |
| 642 | + runStaticTypesInitializers(this); | |
| 629 | 643 | registerMIDIMetaTypes(scriptEngine); |
| 630 | 644 | registerEventTypes(scriptEngine); |
| 631 | 645 | registerMenuItemProperties(scriptEngine); |
| @@ -636,6 +650,29 @@ void ScriptManager::init() { · 4 unmodified lines | ||
| 636 | 650 | scriptRegisterSequenceMetaType<QVector<glm::vec2>>(scriptEngine); |
| 637 | 651 | scriptRegisterSequenceMetaType<QVector<glm::quat>>(scriptEngine); |
| 638 | 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 | 677 | ScriptValue xmlHttpRequestConstructorValue = scriptEngine->newFunction(XMLHttpRequestClass::constructor); |
| 641 | 678 | scriptEngine->globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue); |
| @@ -652,11 +689,6 @@ void ScriptManager::init() { · 10 unmodified lines | ||
| 652 | 689 | */ |
| 653 | 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 | 692 | // NOTE: You do not want to end up creating new instances of singletons here. They will be on the ScriptManager thread |
| 661 | 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 | 703 | resetModuleCache(); |
| 672 | 704 | } |
| 673 | 705 | |
| 674 | − scriptRegisterMetaType(scriptEngine, externalResourceBucketToScriptValue, externalResourceBucketFromScriptValue); | |
| 675 | 706 | scriptEngine->registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>()); |
| 676 | 707 | |
| 677 | 708 | scriptEngine->registerGlobalObject("Quat", &_quatLibrary); |
| @@ -696,7 +727,6 @@ void ScriptManager::init() { · 18 unmodified lines | ||
| 696 | 727 | auto resourcePrototype = createScriptableResourcePrototype(shared_from_this()); |
| 697 | 728 | scriptEngine->globalObject().setProperty("Resource", resourcePrototype); |
| 698 | 729 | scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype); |
| 699 | − scriptRegisterMetaType(scriptEngine, scriptableResourceToScriptValue, scriptableResourceFromScriptValue); | |
| 700 | 730 | |
| 701 | 731 | // constants |
| 702 | 732 | scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE)); |
| @@ -706,9 +736,6 @@ void ScriptManager::init() { · 3 unmodified lines | ||
| 706 | 736 | |
| 707 | 737 | scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance()); |
| 708 | 738 | |
| 709 | − scriptRegisterMetaType(scriptEngine, meshToScriptValue, meshFromScriptValue); | |
| 710 | − scriptRegisterMetaType(scriptEngine, meshesToScriptValue, meshesFromScriptValue); | |
| 711 | − | |
| 712 | 739 | scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data()); |
| 713 | 740 | |
| 714 | 741 | #if DEV_BUILD || PR_BUILD |