From 87063a94ff37f1f79d4f8ff19225d431b8ca0f56 Mon Sep 17 00:00:00 2001 From: wraitii Date: Wed, 21 Jun 2023 07:50:00 +0000 Subject: [PATCH] Pass ScriptRequest instead of ScriptInterface in simulation messages. The previous code was constructing un-necessary ScriptRequest objects. Minor optimisation from not entering/leaving realms. Differential Revision: https://code.wildfiregames.com/D5026 This was SVN commit r27728. --- source/simulation2/MessageTypes.h | 4 +- .../scripting/MessageTypeConversions.cpp | 123 +++++++++--------- .../simulation2/scripting/ScriptComponent.cpp | 4 +- .../simulation2/system/ComponentManager.cpp | 13 +- source/simulation2/system/Message.h | 10 +- 5 files changed, 77 insertions(+), 77 deletions(-) diff --git a/source/simulation2/MessageTypes.h b/source/simulation2/MessageTypes.h index cfb50db9a8..d872491bfe 100644 --- a/source/simulation2/MessageTypes.h +++ b/source/simulation2/MessageTypes.h @@ -35,8 +35,8 @@ virtual int GetType() const { return MT_##name; } \ virtual const char* GetScriptHandlerName() const { return "On" #name; } \ virtual const char* GetScriptGlobalHandlerName() const { return "OnGlobal" #name; } \ - virtual JS::Value ToJSVal(const ScriptInterface& scriptInterface) const; \ - static CMessage* FromJSVal(const ScriptInterface&, JS::HandleValue val); + virtual JS::Value ToJSVal(const ScriptRequest& rq) const; \ + static CMessage* FromJSVal(const ScriptRequest&, JS::HandleValue val); class SceneCollector; class CFrustum; diff --git a/source/simulation2/scripting/MessageTypeConversions.cpp b/source/simulation2/scripting/MessageTypeConversions.cpp index 827543b8a8..14cf3a8396 100644 --- a/source/simulation2/scripting/MessageTypeConversions.cpp +++ b/source/simulation2/scripting/MessageTypeConversions.cpp @@ -21,7 +21,6 @@ #include "simulation2/MessageTypes.h" #define TOJSVAL_SETUP() \ - ScriptRequest rq(scriptInterface); \ JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx)); \ if (!obj) \ return JS::UndefinedValue(); @@ -35,7 +34,6 @@ } while (0); #define FROMJSVAL_SETUP() \ - ScriptRequest rq(scriptInterface); \ if (val.isPrimitive()) \ return NULL; \ JS::RootedObject obj(rq.cx, &val.toObject()); \ @@ -50,24 +48,23 @@ return NULL; \ } -JS::Value CMessage::ToJSValCached(const ScriptInterface& scriptInterface) const +JS::Value CMessage::ToJSValCached(const ScriptRequest& rq) const { - ScriptRequest rq(scriptInterface); if (!m_Cached) - m_Cached.reset(new JS::PersistentRootedValue(rq.cx, ToJSVal(scriptInterface))); + m_Cached.reset(new JS::PersistentRootedValue(rq.cx, ToJSVal(rq))); return m_Cached->get(); } //////////////////////////////// -JS::Value CMessageTurnStart::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageTurnStart::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageTurnStart::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageTurnStart::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageTurnStart(); } @@ -75,13 +72,13 @@ CMessage* CMessageTurnStart::FromJSVal(const ScriptInterface& UNUSED(scriptInter //////////////////////////////// #define MESSAGE_1(name, t0, a0) \ - JS::Value CMessage##name::ToJSVal(const ScriptInterface& scriptInterface) const \ + JS::Value CMessage##name::ToJSVal(const ScriptRequest& rq) const \ { \ TOJSVAL_SETUP(); \ SET_MSG_PROPERTY(a0); \ return JS::ObjectValue(*obj); \ } \ - CMessage* CMessage##name::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) \ + CMessage* CMessage##name::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) \ { \ FROMJSVAL_SETUP(); \ GET_MSG_PROPERTY(t0, a0); \ @@ -95,7 +92,7 @@ MESSAGE_1(Update_Final, fixed, turnLength) //////////////////////////////// -JS::Value CMessageInterpolate::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageInterpolate::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(deltaSimTime); @@ -104,7 +101,7 @@ JS::Value CMessageInterpolate::ToJSVal(const ScriptInterface& scriptInterface) c return JS::ObjectValue(*obj); } -CMessage* CMessageInterpolate::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageInterpolate::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(float, deltaSimTime); @@ -115,13 +112,13 @@ CMessage* CMessageInterpolate::FromJSVal(const ScriptInterface& scriptInterface, //////////////////////////////// -JS::Value CMessageRenderSubmit::ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const +JS::Value CMessageRenderSubmit::ToJSVal(const ScriptRequest& UNUSED(rq)) const { LOGWARNING("CMessageRenderSubmit::ToJSVal not implemented"); return JS::UndefinedValue(); } -CMessage* CMessageRenderSubmit::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageRenderSubmit::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { LOGWARNING("CMessageRenderSubmit::FromJSVal not implemented"); return NULL; @@ -129,13 +126,13 @@ CMessage* CMessageRenderSubmit::FromJSVal(const ScriptInterface& UNUSED(scriptIn //////////////////////////////// -JS::Value CMessageProgressiveLoad::ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const +JS::Value CMessageProgressiveLoad::ToJSVal(const ScriptRequest& UNUSED(rq)) const { LOGWARNING("CMessageProgressiveLoad::ToJSVal not implemented"); return JS::UndefinedValue(); } -CMessage* CMessageProgressiveLoad::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageProgressiveLoad::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { LOGWARNING("CMessageProgressiveLoad::FromJSVal not implemented"); return NULL; @@ -143,13 +140,13 @@ CMessage* CMessageProgressiveLoad::FromJSVal(const ScriptInterface& UNUSED(scrip //////////////////////////////// -JS::Value CMessageDeserialized::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageDeserialized::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageDeserialized::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageDeserialized::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); return new CMessageDeserialized(); @@ -157,14 +154,14 @@ CMessage* CMessageDeserialized::FromJSVal(const ScriptInterface& scriptInterface //////////////////////////////// -JS::Value CMessageCreate::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageCreate::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); return JS::ObjectValue(*obj); } -CMessage* CMessageCreate::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageCreate::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -173,14 +170,14 @@ CMessage* CMessageCreate::FromJSVal(const ScriptInterface& scriptInterface, JS:: //////////////////////////////// -JS::Value CMessageDestroy::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageDestroy::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); return JS::ObjectValue(*obj); } -CMessage* CMessageDestroy::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageDestroy::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -189,7 +186,7 @@ CMessage* CMessageDestroy::FromJSVal(const ScriptInterface& scriptInterface, JS: //////////////////////////////// -JS::Value CMessageOwnershipChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageOwnershipChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); @@ -198,7 +195,7 @@ JS::Value CMessageOwnershipChanged::ToJSVal(const ScriptInterface& scriptInterfa return JS::ObjectValue(*obj); } -CMessage* CMessageOwnershipChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageOwnershipChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -209,7 +206,7 @@ CMessage* CMessageOwnershipChanged::FromJSVal(const ScriptInterface& scriptInter //////////////////////////////// -JS::Value CMessagePositionChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessagePositionChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); @@ -220,7 +217,7 @@ JS::Value CMessagePositionChanged::ToJSVal(const ScriptInterface& scriptInterfac return JS::ObjectValue(*obj); } -CMessage* CMessagePositionChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessagePositionChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -233,13 +230,13 @@ CMessage* CMessagePositionChanged::FromJSVal(const ScriptInterface& scriptInterf //////////////////////////////// -JS::Value CMessageInterpolatedPositionChanged::ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const +JS::Value CMessageInterpolatedPositionChanged::ToJSVal(const ScriptRequest& UNUSED(rq)) const { LOGWARNING("CMessageInterpolatedPositionChanged::ToJSVal not implemented"); return JS::UndefinedValue(); } -CMessage* CMessageInterpolatedPositionChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageInterpolatedPositionChanged::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { LOGWARNING("CMessageInterpolatedPositionChanged::FromJSVal not implemented"); return NULL; @@ -251,7 +248,7 @@ const std::array CMessage "likelySuccess", "likelyFailure", "obstructed", "veryObstructed" } }; -JS::Value CMessageMotionUpdate::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageMotionUpdate::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); JS::RootedValue prop(rq.cx); @@ -262,7 +259,7 @@ JS::Value CMessageMotionUpdate::ToJSVal(const ScriptInterface& scriptInterface) return JS::ObjectValue(*obj); } -CMessage* CMessageMotionUpdate::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageMotionUpdate::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(std::wstring, updateString); @@ -282,7 +279,7 @@ CMessage* CMessageMotionUpdate::FromJSVal(const ScriptInterface& scriptInterface //////////////////////////////// -JS::Value CMessageTerrainChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageTerrainChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(i0); @@ -292,7 +289,7 @@ JS::Value CMessageTerrainChanged::ToJSVal(const ScriptInterface& scriptInterface return JS::ObjectValue(*obj); } -CMessage* CMessageTerrainChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageTerrainChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(int32_t, i0); @@ -304,7 +301,7 @@ CMessage* CMessageTerrainChanged::FromJSVal(const ScriptInterface& scriptInterfa //////////////////////////////// -JS::Value CMessageVisibilityChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageVisibilityChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(player); @@ -314,7 +311,7 @@ JS::Value CMessageVisibilityChanged::ToJSVal(const ScriptInterface& scriptInterf return JS::ObjectValue(*obj); } -CMessage* CMessageVisibilityChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageVisibilityChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(player_id_t, player); @@ -326,59 +323,59 @@ CMessage* CMessageVisibilityChanged::FromJSVal(const ScriptInterface& scriptInte //////////////////////////////// -JS::Value CMessageWaterChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageWaterChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageWaterChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageWaterChanged::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageWaterChanged(); } //////////////////////////////// -JS::Value CMessageMovementObstructionChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageMovementObstructionChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageMovementObstructionChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageMovementObstructionChanged::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageMovementObstructionChanged(); } //////////////////////////////// -JS::Value CMessageObstructionMapShapeChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageObstructionMapShapeChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageObstructionMapShapeChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageObstructionMapShapeChanged::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageObstructionMapShapeChanged(); } //////////////////////////////// -JS::Value CMessageTerritoriesChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageTerritoriesChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageTerritoriesChanged::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageTerritoriesChanged::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageTerritoriesChanged(); } //////////////////////////////// -JS::Value CMessageRangeUpdate::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageRangeUpdate::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(tag); @@ -387,7 +384,7 @@ JS::Value CMessageRangeUpdate::ToJSVal(const ScriptInterface& scriptInterface) c return JS::ObjectValue(*obj); } -CMessage* CMessageRangeUpdate::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageRangeUpdate::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { LOGWARNING("CMessageRangeUpdate::FromJSVal not implemented"); return NULL; @@ -395,13 +392,13 @@ CMessage* CMessageRangeUpdate::FromJSVal(const ScriptInterface& UNUSED(scriptInt //////////////////////////////// -JS::Value CMessagePathResult::ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const +JS::Value CMessagePathResult::ToJSVal(const ScriptRequest& UNUSED(rq)) const { LOGWARNING("CMessagePathResult::ToJSVal not implemented"); return JS::UndefinedValue(); } -CMessage* CMessagePathResult::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessagePathResult::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { LOGWARNING("CMessagePathResult::FromJSVal not implemented"); return NULL; @@ -409,7 +406,7 @@ CMessage* CMessagePathResult::FromJSVal(const ScriptInterface& UNUSED(scriptInte //////////////////////////////// -JS::Value CMessageValueModification::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageValueModification::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entities); @@ -418,7 +415,7 @@ JS::Value CMessageValueModification::ToJSVal(const ScriptInterface& scriptInterf return JS::ObjectValue(*obj); } -CMessage* CMessageValueModification::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageValueModification::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(std::vector, entities); @@ -429,7 +426,7 @@ CMessage* CMessageValueModification::FromJSVal(const ScriptInterface& scriptInte //////////////////////////////// -JS::Value CMessageTemplateModification::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageTemplateModification::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(player); @@ -438,7 +435,7 @@ JS::Value CMessageTemplateModification::ToJSVal(const ScriptInterface& scriptInt return JS::ObjectValue(*obj); } -CMessage* CMessageTemplateModification::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageTemplateModification::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(player_id_t, player); @@ -449,7 +446,7 @@ CMessage* CMessageTemplateModification::FromJSVal(const ScriptInterface& scriptI //////////////////////////////// -JS::Value CMessageVisionRangeChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageVisionRangeChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); @@ -458,7 +455,7 @@ JS::Value CMessageVisionRangeChanged::ToJSVal(const ScriptInterface& scriptInter return JS::ObjectValue(*obj); } -CMessage* CMessageVisionRangeChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageVisionRangeChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -467,7 +464,7 @@ CMessage* CMessageVisionRangeChanged::FromJSVal(const ScriptInterface& scriptInt return new CMessageVisionRangeChanged(entity, oldRange, newRange); } -JS::Value CMessageVisionSharingChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageVisionSharingChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(entity); @@ -476,7 +473,7 @@ JS::Value CMessageVisionSharingChanged::ToJSVal(const ScriptInterface& scriptInt return JS::ObjectValue(*obj); } -CMessage* CMessageVisionSharingChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageVisionSharingChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(entity_id_t, entity); @@ -487,27 +484,27 @@ CMessage* CMessageVisionSharingChanged::FromJSVal(const ScriptInterface& scriptI //////////////////////////////// -JS::Value CMessageMinimapPing::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageMinimapPing::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageMinimapPing::FromJSVal(const ScriptInterface& UNUSED(scriptInterface), JS::HandleValue UNUSED(val)) +CMessage* CMessageMinimapPing::FromJSVal(const ScriptRequest& UNUSED(rq), JS::HandleValue UNUSED(val)) { return new CMessageMinimapPing(); } //////////////////////////////// -JS::Value CMessageCinemaPathEnded::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageCinemaPathEnded::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(name); return JS::ObjectValue(*obj); } -CMessage* CMessageCinemaPathEnded::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageCinemaPathEnded::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(CStrW, name); @@ -516,13 +513,13 @@ CMessage* CMessageCinemaPathEnded::FromJSVal(const ScriptInterface& scriptInterf //////////////////////////////// -JS::Value CMessageCinemaQueueEnded::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessageCinemaQueueEnded::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); return JS::ObjectValue(*obj); } -CMessage* CMessageCinemaQueueEnded::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessageCinemaQueueEnded::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); return new CMessageCinemaQueueEnded(); @@ -530,14 +527,14 @@ CMessage* CMessageCinemaQueueEnded::FromJSVal(const ScriptInterface& scriptInter //////////////////////////////////////////////////////////////// -JS::Value CMessagePlayerColorChanged::ToJSVal(const ScriptInterface& scriptInterface) const +JS::Value CMessagePlayerColorChanged::ToJSVal(const ScriptRequest& rq) const { TOJSVAL_SETUP(); SET_MSG_PROPERTY(player); return JS::ObjectValue(*obj); } -CMessage* CMessagePlayerColorChanged::FromJSVal(const ScriptInterface& scriptInterface, JS::HandleValue val) +CMessage* CMessagePlayerColorChanged::FromJSVal(const ScriptRequest& rq, JS::HandleValue val) { FROMJSVAL_SETUP(); GET_MSG_PROPERTY(player_id_t, player); @@ -546,11 +543,11 @@ CMessage* CMessagePlayerColorChanged::FromJSVal(const ScriptInterface& scriptInt //////////////////////////////////////////////////////////////// -CMessage* CMessageFromJSVal(int mtid, const ScriptInterface& scriptingInterface, JS::HandleValue val) +CMessage* CMessageFromJSVal(int mtid, const ScriptRequest& rq, JS::HandleValue val) { switch (mtid) { -#define MESSAGE(name) case MT_##name: return CMessage##name::FromJSVal(scriptingInterface, val); +#define MESSAGE(name) case MT_##name: return CMessage##name::FromJSVal(rq, val); #define INTERFACE(name) #define COMPONENT(name) #include "simulation2/TypeList.h" diff --git a/source/simulation2/scripting/ScriptComponent.cpp b/source/simulation2/scripting/ScriptComponent.cpp index d0e8f76032..42bd6264ee 100644 --- a/source/simulation2/scripting/ScriptComponent.cpp +++ b/source/simulation2/scripting/ScriptComponent.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -52,7 +52,7 @@ void CComponentTypeScript::HandleMessage(const CMessage& msg, bool global) const char* name = global ? msg.GetScriptGlobalHandlerName() : msg.GetScriptHandlerName(); - JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(m_ScriptInterface)); + JS::RootedValue msgVal(rq.cx, msg.ToJSValCached(rq)); if (!ScriptFunction::CallVoid(rq, m_Instance, name, msgVal)) LOGERROR("Script message handler %s failed", name); diff --git a/source/simulation2/system/ComponentManager.cpp b/source/simulation2/system/ComponentManager.cpp index 421bbc2f6c..b78e906a80 100644 --- a/source/simulation2/system/ComponentManager.cpp +++ b/source/simulation2/system/ComponentManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -43,10 +43,10 @@ public: virtual int GetType() const { return mtid; } virtual const char* GetScriptHandlerName() const { return handlerName.c_str(); } virtual const char* GetScriptGlobalHandlerName() const { return globalHandlerName.c_str(); } - virtual JS::Value ToJSVal(const ScriptInterface& UNUSED(scriptInterface)) const { return msg.get(); } + virtual JS::Value ToJSVal(const ScriptRequest& UNUSED(rq)) const { return msg.get(); } - CMessageScripted(const ScriptInterface& scriptInterface, int mtid, const std::string& name, JS::HandleValue msg) : - mtid(mtid), handlerName("On" + name), globalHandlerName("OnGlobal" + name), msg(scriptInterface.GetGeneralJSContext(), msg) + CMessageScripted(const ScriptRequest& rq, int mtid, const std::string& name, JS::HandleValue msg) : + mtid(mtid), handlerName("On" + name), globalHandlerName("OnGlobal" + name), msg(rq.cx, msg) { } @@ -429,13 +429,14 @@ CMessage* CComponentManager::ConstructMessage(int mtid, JS::HandleValue data) if (mtid == MT__Invalid || mtid > (int)m_MessageTypeIdsByName.size()) // (IDs start at 1 so use '>' here) LOGERROR("PostMessage with invalid message type ID '%d'", mtid); + ScriptRequest rq(m_ScriptInterface); if (mtid < MT__LastNative) { - return CMessageFromJSVal(mtid, m_ScriptInterface, data); + return CMessageFromJSVal(mtid, rq, data); } else { - return new CMessageScripted(m_ScriptInterface, mtid, m_MessageTypeNamesById[mtid], data); + return new CMessageScripted(rq, mtid, m_MessageTypeNamesById[mtid], data); } } diff --git a/source/simulation2/system/Message.h b/source/simulation2/system/Message.h index 05386ab0c1..01d49a8254 100644 --- a/source/simulation2/system/Message.h +++ b/source/simulation2/system/Message.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2020 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -20,6 +20,8 @@ #include "scriptinterface/ScriptTypes.h" +class ScriptRequest; + class CMessage { NONCOPYABLE(CMessage); @@ -30,14 +32,14 @@ public: virtual int GetType() const = 0; virtual const char* GetScriptHandlerName() const = 0; virtual const char* GetScriptGlobalHandlerName() const = 0; - virtual JS::Value ToJSVal(const ScriptInterface&) const = 0; - JS::Value ToJSValCached(const ScriptInterface&) const; + virtual JS::Value ToJSVal(const ScriptRequest&) const = 0; + JS::Value ToJSValCached(const ScriptRequest&) const; private: mutable std::unique_ptr m_Cached; }; // TODO: GetType could be replaced with a plain member variable to avoid some // virtual calls, if that turns out to be worthwhile -CMessage* CMessageFromJSVal(int mtid, const ScriptInterface&, JS::HandleValue); +CMessage* CMessageFromJSVal(int mtid, const ScriptRequest&, JS::HandleValue); #endif // INCLUDED_MESSAGE