History

script-engine: force callers of register* functions to hold a scope guard

v8 allows us to enter an Isolate::scope multiple times in the same thread, so I widened it to our ScopeGuard as well.

RTUnreal committed 9 months ago

Commit 3a6d77a1 parent 82fb89f14 files · +165 −132Browse files

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

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+25−23
@@ -772,6 +772,8 @@ void ScriptManager::initMetaTypes() { · 771 unmodified lines
772}772}
773 773
774void ScriptManager::init() {774void ScriptManager::init() {
775 auto scopeGuard = engine()->getScopeGuard();
776 auto* sgp = scopeGuard.get();
775 if (_isInitialized) {777 if (_isInitialized) {
776 return; // only initialize once778 return; // only initialize once
777 }779 }
@@ -809,7 +811,7 @@ void ScriptManager::init() { · 31 unmodified lines
809 // NOTE: You do not want to end up creating new instances of singletons here. They will be on the ScriptManager thread811 // NOTE: You do not want to end up creating new instances of singletons here. They will be on the ScriptManager thread
810 // and are likely to be unusable if we "reset" the ScriptManager by creating a new one (on a whole new thread).812 // and are likely to be unusable if we "reset" the ScriptManager by creating a new one (on a whole new thread).
811 813
812 scriptEngine->registerGlobalObject("Script", _scriptingInterface.get());814 scriptEngine->registerGlobalObject(sgp, "Script", _scriptingInterface.get());
813 815
814 {816 {
815 // set up Script.require.resolve and Script.require.cache817 // set up Script.require.resolve and Script.require.cache
@@ -822,29 +824,29 @@ void ScriptManager::init() { · 6 unmodified lines
822 resetModuleCache();824 resetModuleCache();
823 }825 }
824 826
825 scriptEngine->registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>());827 scriptEngine->registerEnum(sgp, "Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>());
826 828
827 scriptEngine->registerGlobalObject("Quat", _quatLibrary.get());829 scriptEngine->registerGlobalObject(sgp, "Quat", _quatLibrary.get());
828 scriptEngine->registerGlobalObject("Vec3", _vec3Library.get());830 scriptEngine->registerGlobalObject(sgp, "Vec3", _vec3Library.get());
829 scriptEngine->registerGlobalObject("Mat4", _mat4Library.get());831 scriptEngine->registerGlobalObject(sgp, "Mat4", _mat4Library.get());
830 scriptEngine->registerGlobalObject("Uuid", _uuidLibrary.get());832 scriptEngine->registerGlobalObject(sgp, "Uuid", _uuidLibrary.get());
831 833
832 if (_context != NETWORKLESS_TEST_SCRIPT) {834 if (_context != NETWORKLESS_TEST_SCRIPT) {
833 // This requires networking, we want to avoid the need for it in test scripts835 // This requires networking, we want to avoid the need for it in test scripts
834 scriptEngine->registerGlobalObject("Messages", DependencyManager::get<MessagesClient>().data());836 scriptEngine->registerGlobalObject(sgp, "Messages", DependencyManager::get<MessagesClient>().data());
835 }837 }
836 838
837 scriptEngine->registerGlobalObject("console", _consoleScriptingInterface.get());839 scriptEngine->registerGlobalObject(sgp, "console", _consoleScriptingInterface.get());
838 scriptEngine->registerFunction("console", "info", ConsoleScriptingInterface::info, scriptEngine->currentContext()->argumentCount());840 scriptEngine->registerFunction(sgp, "console", "info", ConsoleScriptingInterface::info, scriptEngine->currentContext()->argumentCount());
839 scriptEngine->registerFunction("console", "log", ConsoleScriptingInterface::log, scriptEngine->currentContext()->argumentCount());841 scriptEngine->registerFunction(sgp, "console", "log", ConsoleScriptingInterface::log, scriptEngine->currentContext()->argumentCount());
840 scriptEngine->registerFunction("console", "debug", ConsoleScriptingInterface::debug, scriptEngine->currentContext()->argumentCount());842 scriptEngine->registerFunction(sgp, "console", "debug", ConsoleScriptingInterface::debug, scriptEngine->currentContext()->argumentCount());
841 scriptEngine->registerFunction("console", "warn", ConsoleScriptingInterface::warn, scriptEngine->currentContext()->argumentCount());843 scriptEngine->registerFunction(sgp, "console", "warn", ConsoleScriptingInterface::warn, scriptEngine->currentContext()->argumentCount());
842 scriptEngine->registerFunction("console", "error", ConsoleScriptingInterface::error, scriptEngine->currentContext()->argumentCount());844 scriptEngine->registerFunction(sgp, "console", "error", ConsoleScriptingInterface::error, scriptEngine->currentContext()->argumentCount());
843 scriptEngine->registerFunction("console", "exception", ConsoleScriptingInterface::exception, scriptEngine->currentContext()->argumentCount());845 scriptEngine->registerFunction(sgp, "console", "exception", ConsoleScriptingInterface::exception, scriptEngine->currentContext()->argumentCount());
844 scriptEngine->registerFunction("console", "assert", ConsoleScriptingInterface::assertion, scriptEngine->currentContext()->argumentCount());846 scriptEngine->registerFunction(sgp, "console", "assert", ConsoleScriptingInterface::assertion, scriptEngine->currentContext()->argumentCount());
845 scriptEngine->registerFunction("console", "group", ConsoleScriptingInterface::group, 1);847 scriptEngine->registerFunction(sgp, "console", "group", ConsoleScriptingInterface::group, 1);
846 scriptEngine->registerFunction("console", "groupCollapsed", ConsoleScriptingInterface::groupCollapsed, 1);848 scriptEngine->registerFunction(sgp, "console", "groupCollapsed", ConsoleScriptingInterface::groupCollapsed, 1);
847 scriptEngine->registerFunction("console", "groupEnd", ConsoleScriptingInterface::groupEnd, 0);849 scriptEngine->registerFunction(sgp, "console", "groupEnd", ConsoleScriptingInterface::groupEnd, 0);
848 850
849 // constants851 // constants
850 scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE));852 scriptEngine->globalObject().setProperty("TREE_SCALE", scriptEngine->newValue(TREE_SCALE));
@@ -855,16 +857,16 @@ void ScriptManager::init() { · 4 unmodified lines
855 scriptEngine->globalObject().setProperty("Resource", resourcePrototype);857 scriptEngine->globalObject().setProperty("Resource", resourcePrototype);
856 scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype);858 scriptEngine->setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype);
857 859
858 scriptEngine->registerGlobalObject("Assets", _assetScriptingInterface.get());860 scriptEngine->registerGlobalObject(sgp, "Assets", _assetScriptingInterface.get());
859 scriptEngine->registerGlobalObject("Resources", DependencyManager::get<ResourceScriptingInterface>().data());861 scriptEngine->registerGlobalObject(sgp, "Resources", DependencyManager::get<ResourceScriptingInterface>().data());
860 862
861 scriptEngine->registerGlobalObject("DebugDraw", &DebugDraw::getInstance());863 scriptEngine->registerGlobalObject(sgp, "DebugDraw", &DebugDraw::getInstance());
862 864
863 scriptEngine->registerGlobalObject("UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data());865 scriptEngine->registerGlobalObject(sgp, "UserActivityLogger", DependencyManager::get<UserActivityLoggerScriptingInterface>().data());
864 }866 }
865 867
866#if DEV_BUILD || PR_BUILD868#if DEV_BUILD || PR_BUILD
867 scriptEngine->registerGlobalObject("StackTest", new StackTestScriptingInterface(this));869 scriptEngine->registerGlobalObject(sgp, "StackTest", new StackTestScriptingInterface(this));
868#endif870#endif
869 871
870 qCDebug(scriptengine) << "Engine initialized";872 qCDebug(scriptengine) << "Engine initialized";