From 0eb78450c062c7f2124c7aeaaf211046b99d9044 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Wed, 30 Mar 2005 22:33:10 +0000 Subject: [PATCH] Maybe fixed GUI event-handler function things. Other minor changes and fixes. This was SVN commit r2096. --- .../data/mods/official/gui/test/4_session.xml | 9 +++- source/gui/IGUIObject.cpp | 47 +++++++++++++++---- source/gui/IGUIObject.h | 8 ++-- .../gui/scripting/JSInterface_IGUIObject.cpp | 9 ++-- source/main.cpp | 2 + source/ps/CConsole.h | 2 +- source/scripting/ScriptableObject.h | 2 +- source/simulation/Entity.cpp | 2 +- 8 files changed, 57 insertions(+), 24 deletions(-) diff --git a/binaries/data/mods/official/gui/test/4_session.xml b/binaries/data/mods/official/gui/test/4_session.xml index 8471763779..204f0912b8 100644 --- a/binaries/data/mods/official/gui/test/4_session.xml +++ b/binaries/data/mods/official/gui/test/4_session.xml @@ -143,7 +143,8 @@ second.m_pSetting; break; - map::iterator it; - for (it = m_Settings.begin(); it != m_Settings.end(); ++it) - switch (it->second.m_Type) + { + map::iterator it; + for (it = m_Settings.begin(); it != m_Settings.end(); ++it) { - #include "GUItypes.h" + switch (it->second.m_Type) + { + // delete() needs to know the type of the variable - never delete a void* +#define TYPE(t) case GUIST_##t: delete (t*)it->second.m_pSetting; break; +#include "GUItypes.h" +#undef TYPE default: debug_warn("Invalid setting type"); + } } -#undef TYPE + } + + { + std::map::iterator it; + for (it = m_ScriptHandlers.begin(); it != m_ScriptHandlers.end(); ++it) + { + JS_RemoveRoot(g_ScriptingHost.getContext(), it->second); + delete it->second; + } + } } //------------------------------------------------------------------- @@ -413,13 +426,27 @@ void IGUIObject::RegisterScriptHandler(const CStr& Action, const CStr& Code, CGU JSFunction* func = JS_CompileFunction(g_ScriptingHost.getContext(), pGUI->m_ScriptObject, buf, paramCount, paramNames, (const char*)Code, Code.Length(), CodeName, 0); assert(func); // TODO: Handle errors + if (func) + SetScriptHandler(Action, JS_GetFunctionObject(func)); +} - m_ScriptHandlers[Action] = func; +void IGUIObject::SetScriptHandler(const CStr& Action, JSObject* Function) +{ + JSObject** obj = new JSObject*; + *obj = Function; + JS_AddRoot(g_ScriptingHost.getContext(), obj); + + if (m_ScriptHandlers[Action]) + { + JS_RemoveRoot(g_ScriptingHost.getContext(), m_ScriptHandlers[Action]); + delete m_ScriptHandlers[Action]; + } + m_ScriptHandlers[Action] = obj; } void IGUIObject::ScriptEvent(const CStr& Action) { - map::iterator it = m_ScriptHandlers.find(Action); + map::iterator it = m_ScriptHandlers.find(Action); if (it == m_ScriptHandlers.end()) return; @@ -454,7 +481,7 @@ void IGUIObject::ScriptEvent(const CStr& Action) jsval result; - JSBool ok = JS_CallFunction(g_ScriptingHost.getContext(), jsGuiObject, it->second, 1, paramData, &result); + JSBool ok = JS_CallFunctionValue(g_ScriptingHost.getContext(), jsGuiObject, OBJECT_TO_JSVAL(*it->second), 1, paramData, &result); if (!ok) { JS_ReportError(g_ScriptingHost.getContext(), "Errors executing script action \"%s\"", Action.c_str()); diff --git a/source/gui/IGUIObject.h b/source/gui/IGUIObject.h index 3a731f56db..6a0962f338 100755 --- a/source/gui/IGUIObject.h +++ b/source/gui/IGUIObject.h @@ -431,10 +431,7 @@ protected: */ void ScriptEvent(const CStr& Action); - /** - * Internal storage for registered script handlers. - */ - std::map m_ScriptHandlers; + void SetScriptHandler(const CStr& Action, JSObject* Function); //@} private: @@ -511,6 +508,9 @@ protected: private: // An object can't function stand alone CGUI *m_pGUI; + + // Internal storage for registered script handlers. + std::map m_ScriptHandlers; }; diff --git a/source/gui/scripting/JSInterface_IGUIObject.cpp b/source/gui/scripting/JSInterface_IGUIObject.cpp index 5c5a48ad33..34b9fbd9d2 100755 --- a/source/gui/scripting/JSInterface_IGUIObject.cpp +++ b/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -53,11 +53,11 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval if (propName.Left(2) == "on") { CStr eventName (CStr(propName.substr(2)).LowerCase()); - std::map::iterator it = e->m_ScriptHandlers.find(eventName); + std::map::iterator it = e->m_ScriptHandlers.find(eventName); if (it == e->m_ScriptHandlers.end()) *vp = JSVAL_NULL; else - *vp = OBJECT_TO_JSVAL(JS_GetFunctionObject(it->second)); + *vp = OBJECT_TO_JSVAL(*(it->second)); return JS_TRUE; } @@ -264,15 +264,14 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval // Use onWhatever to set event handlers if (propName.Left(2) == "on") { - JSFunction* func = JS_ValueToFunction(cx, *vp); - if (! func) + if (!JSVAL_IS_OBJECT(*vp) || !JS_ValueToFunction(cx, *vp)) { JS_ReportError(cx, "on- event-handlers must be functions"); return JS_FALSE; } CStr eventName (CStr(propName.substr(2)).LowerCase()); - e->m_ScriptHandlers[eventName] = func; + e->SetScriptHandler(eventName, JSVAL_TO_OBJECT(*vp)); return JS_TRUE; } diff --git a/source/main.cpp b/source/main.cpp index de92d63fce..496b4b61c6 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -1408,7 +1408,9 @@ static void Frame() MICROLOG(L"render"); Render(); MICROLOG(L"finished render"); + PROFILE_START( "swap buffers" ); SDL_GL_SwapBuffers(); + PROFILE_END( "swap buffers" ); } // inactive; relinquish CPU for a little while // don't use SDL_WaitEvent: don't want the main loop to freeze until app focus is restored diff --git a/source/ps/CConsole.h b/source/ps/CConsole.h index 23964ce899..5287c32d3e 100755 --- a/source/ps/CConsole.h +++ b/source/ps/CConsole.h @@ -24,7 +24,7 @@ #ifndef CCONSOLE_H #define CCONSOLE_H -#define CONSOLE_BUFFER_SIZE 256 // for text being typed into the console +#define CONSOLE_BUFFER_SIZE 1024 // for text being typed into the console #define CONSOLE_MESSAGE_SIZE 1024 // for messages being printed into the console typedef void(*fptr)(void); diff --git a/source/scripting/ScriptableObject.h b/source/scripting/ScriptableObject.h index 0321e06c69..21623e316b 100755 --- a/source/scripting/ScriptableObject.h +++ b/source/scripting/ScriptableObject.h @@ -258,7 +258,7 @@ public: } void ImmediateCopy( IJSObject* CopyTo, IJSObject* CopyFrom, IJSProperty* CopyProperty ) { - assert( "Inheritance not supported for CJSSharedProperties" ); + debug_warn( "Inheritance not supported for CJSSharedProperties" ); } void Set( JSContext* cx, IJSObject* owner, jsval Value ) { diff --git a/source/simulation/Entity.cpp b/source/simulation/Entity.cpp index b5e12891b8..eb07033c0c 100755 --- a/source/simulation/Entity.cpp +++ b/source/simulation/Entity.cpp @@ -333,7 +333,7 @@ bool CEntity::DispatchEvent( CScriptEvent* evt ) else { CStr8 short_string( evt->m_Type ); - int length = short_string.length(); + size_t length = short_string.length(); data = new char[length + 9]; strcpy( data, "script: " ); strcpy( data + 8, short_string.c_str() );