diff --git a/source/simulation2/scripting/EngineScriptConversions.cpp b/source/simulation2/scripting/EngineScriptConversions.cpp index c5150a3a3b..419676da83 100644 --- a/source/simulation2/scripting/EngineScriptConversions.cpp +++ b/source/simulation2/scripting/EngineScriptConversions.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2024 Wildfire Games. +/* Copyright (C) 2025 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -69,11 +69,6 @@ template<> void Script::ToJSVal(const ScriptRequest& rq, JS::Mutab template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, CParamNode const& val) { val.ToJSVal(rq, true, ret); - - // Prevent modifications to the object, so that it's safe to share between - // components and to reconstruct on deserialization - if (ret.isObject()) - Script::DeepFreezeObject(rq, ret); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const CParamNode* const& val) diff --git a/source/simulation2/system/ParamNode.cpp b/source/simulation2/system/ParamNode.cpp index 51dafc12ba..31daf05146 100644 --- a/source/simulation2/system/ParamNode.cpp +++ b/source/simulation2/system/ParamNode.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2024 Wildfire Games. +/* Copyright (C) 2025 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -25,6 +25,7 @@ #include "ps/CStrIntern.h" #include "ps/Filesystem.h" #include "ps/XML/Xeromyces.h" +#include "scriptinterface/Object.h" #include "scriptinterface/ScriptRequest.h" #include @@ -382,7 +383,12 @@ void CParamNode::ToJSVal(const ScriptRequest& rq, bool cacheValue, JS::MutableHa ConstructJSVal(rq, ret); if (cacheValue) + { + if (ret.isObject()) + Script::DeepFreezeObject(rq, ret); + m_ScriptVal.reset(new JS::PersistentRootedValue(rq.cx, ret)); + } } void CParamNode::ConstructJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret) const diff --git a/source/simulation2/system/ParamNode.h b/source/simulation2/system/ParamNode.h index acd0ae0736..cb22fa33ad 100644 --- a/source/simulation2/system/ParamNode.h +++ b/source/simulation2/system/ParamNode.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2025 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -252,6 +252,8 @@ public: * If @p cacheValue is true, then the same JS::Value will be returned each time * this is called (regardless of whether you passed the same @p cx - be careful * to only use the cache in one context). + * Cached object values are frozen, using DeepFreezeObject, so that it's safe to + * share between components and to reconstruct on deserialization. * When caching, the lifetime of @p cx must be longer than the lifetime of this node. * The cache will be reset if *this* node is modified (e.g. by LoadXML), * but *not* if any child nodes are modified (so don't do that).