diff --git a/source/gui/CProgressBar.cpp b/source/gui/CProgressBar.cpp index d11d011c31..a08f504efc 100644 --- a/source/gui/CProgressBar.cpp +++ b/source/gui/CProgressBar.cpp @@ -59,8 +59,8 @@ void CProgressBar::Draw() float bz = GetBufferedZ(); CGUISpriteInstance *sprite_background, *sprite_bar; - int cell_id; - float value; + int cell_id = 0; + float value = 0; GUI::GetSettingPointer(this, "sprite_background", sprite_background); GUI::GetSettingPointer(this, "sprite_bar", sprite_bar); GUI::GetSetting(this, "cell_id", cell_id); diff --git a/source/i18n/BufferVariable.cpp b/source/i18n/BufferVariable.cpp index 07406df137..2838b24a2e 100644 --- a/source/i18n/BufferVariable.cpp +++ b/source/i18n/BufferVariable.cpp @@ -8,6 +8,7 @@ // Hashing functions are currently 32-bit only cassert(sizeof(int) == 4); cassert(sizeof(double) == 8); +// (TODO: the hashing here is quite rubbish) using namespace I18n; @@ -48,7 +49,12 @@ StrImW BufferVariable_double::ToString(CLocale*) u32 BufferVariable_double::Hash() { // Add the two four-bytes of the double - return *((u32*)&value) + *((u32*)&value + 1); + union { + u32 i[2]; + double d; + } u; + u.d = value; + return u.i[0]+u.i[1]; } diff --git a/source/lib/res/file/vfs_tree.cpp b/source/lib/res/file/vfs_tree.cpp index d6c6d06342..14c42ea9fd 100644 --- a/source/lib/res/file/vfs_tree.cpp +++ b/source/lib/res/file/vfs_tree.cpp @@ -584,7 +584,7 @@ LibError tree_lookup_dir(const char* V_path, TDir** ptd, uint flags) WARN_RETURN(ERR::TNODE_WRONG_TYPE); TDir* td = (flags & LF_START_DIR)? *ptd : tree_root; - TNode* node; + TNode* node = NULL; CHECK_ERR(lookup(td, V_path, flags, &node)); // directories should exist, so warn if this fails *ptd = (TDir*)node; @@ -598,7 +598,7 @@ LibError tree_lookup(const char* V_path, TFile** pfile, uint flags) if(VFS_PATH_IS_DIR(V_path)) WARN_RETURN(ERR::TNODE_WRONG_TYPE); - TNode* node; + TNode* node = NULL; LibError ret = lookup(tree_root, V_path, flags, &node); RETURN_ERR(ret); *pfile = (TFile*)node; diff --git a/source/lib/res/graphics/ogl_shader.cpp b/source/lib/res/graphics/ogl_shader.cpp index db90db0661..d2d04ec9da 100644 --- a/source/lib/res/graphics/ogl_shader.cpp +++ b/source/lib/res/graphics/ogl_shader.cpp @@ -110,12 +110,15 @@ static LibError Ogl_Shader_reload(Ogl_Shader* shdr, const char* filename, Handle // bad code. ogl_WarnIfError(); - err = ERR::SHDR_CREATE; + err = ERR::SHDR_CREATE; goto fail_fileloaded; } - pglShaderSourceARB(shdr->id, 1, (const char**)&file, (const GLint*)&file_size); - pglCompileShaderARB(shdr->id); + { + const GLchar* strings[] = { (const GLchar*)file }; + pglShaderSourceARB(shdr->id, 1, strings, (const GLint*)&file_size); + pglCompileShaderARB(shdr->id); + } pglGetObjectParameterivARB(shdr->id, GL_OBJECT_COMPILE_STATUS_ARB, &compile_success); pglGetObjectParameterivARB(shdr->id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &log_length); diff --git a/source/ps/GameAttributes.cpp b/source/ps/GameAttributes.cpp index a705eacc5a..b6af324145 100644 --- a/source/ps/GameAttributes.cpp +++ b/source/ps/GameAttributes.cpp @@ -280,13 +280,13 @@ void CGameAttributes::ScriptingInit() g_ScriptingHost.DefineCustomObjectType(&PlayerSlotArray_JS::Class, PlayerSlotArray_JS::Construct, 0, NULL, NULL, NULL, NULL); - AddMethod("getOpenSlot", 0); + AddMethod("getOpenSlot", 0); AddProperty(L"slots", &CGameAttributes::JSI_GetPlayerSlots); CJSObject::ScriptingInit("GameAttributes"); } -jsval CGameAttributes::JSI_GetOpenSlot(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) +jsval_t CGameAttributes::JSI_GetOpenSlot(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) { vector ::iterator it; for (it = m_PlayerSlots.begin();it != m_PlayerSlots.end();++it) diff --git a/source/ps/GameAttributes.h b/source/ps/GameAttributes.h index 85ea4408a2..ba0bf21922 100644 --- a/source/ps/GameAttributes.h +++ b/source/ps/GameAttributes.h @@ -150,7 +150,7 @@ private: static void OnNumSlotsUpdate(CSynchedJSObjectBase *owner); jsval JSI_GetPlayerSlots(JSContext* cx); - jsval JSI_GetOpenSlot(JSContext *cx, uintN argc, jsval *argv); + jsval_t JSI_GetOpenSlot(JSContext *cx, uintN argc, jsval *argv); static void ScriptingInit(); public: diff --git a/source/ps/Hotkey.cpp b/source/ps/Hotkey.cpp index 9c756c74e2..cb3aa874ff 100644 --- a/source/ps/Hotkey.cpp +++ b/source/ps/Hotkey.cpp @@ -25,19 +25,21 @@ struct SHotkeyMapping typedef std::vector KeyMapping; -// 'Keycodes' for the mouse buttons -const int MOUSE_LEFT = SDLK_LAST + SDL_BUTTON_LEFT; -const int MOUSE_RIGHT = SDLK_LAST + SDL_BUTTON_RIGHT; -const int MOUSE_MIDDLE = SDLK_LAST + SDL_BUTTON_MIDDLE; -const int MOUSE_WHEELUP = SDLK_LAST + SDL_BUTTON_WHEELUP; -const int MOUSE_WHEELDOWN = SDLK_LAST + SDL_BUTTON_WHEELDOWN; +enum { + // 'Keycodes' for the mouse buttons + MOUSE_LEFT = SDLK_LAST + SDL_BUTTON_LEFT, + MOUSE_RIGHT = SDLK_LAST + SDL_BUTTON_RIGHT, + MOUSE_MIDDLE = SDLK_LAST + SDL_BUTTON_MIDDLE, + MOUSE_WHEELUP = SDLK_LAST + SDL_BUTTON_WHEELUP, + MOUSE_WHEELDOWN = SDLK_LAST + SDL_BUTTON_WHEELDOWN, -// 'Keycodes' for the unified modifier keys -const int UNIFIED_SHIFT = MOUSE_WHEELDOWN + 1; -const int UNIFIED_CTRL = MOUSE_WHEELDOWN + 2; -const int UNIFIED_ALT = MOUSE_WHEELDOWN + 3; -const int UNIFIED_META = MOUSE_WHEELDOWN + 4; -const int UNIFIED_SUPER = MOUSE_WHEELDOWN + 5; + // 'Keycodes' for the unified modifier keys + UNIFIED_SHIFT, + UNIFIED_CTRL, + UNIFIED_ALT, + UNIFIED_META, + UNIFIED_SUPER +}; /** * HK_MAX_KEYCODES: Global maximum number of keycodes, including our "fake" keycodes for @@ -358,31 +360,31 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev ) phantom.ev.type = ( ( ev->ev.type == SDL_KEYDOWN ) || ( ev->ev.type == SDL_MOUSEBUTTONDOWN ) ) ? SDL_KEYDOWN : SDL_KEYUP; if( ( keycode == SDLK_LSHIFT ) || ( keycode == SDLK_RSHIFT ) ) { - (int&)phantom.ev.key.keysym.sym = UNIFIED_SHIFT; + phantom.ev.key.keysym.sym = (SDLKey)UNIFIED_SHIFT; unified[0] = ( phantom.ev.type == SDL_KEYDOWN ); HotkeyInputHandler( &phantom ); } else if( ( keycode == SDLK_LCTRL ) || ( keycode == SDLK_RCTRL ) ) { - (int&)phantom.ev.key.keysym.sym = UNIFIED_CTRL; + phantom.ev.key.keysym.sym = (SDLKey)UNIFIED_CTRL; unified[1] = ( phantom.ev.type == SDL_KEYDOWN ); HotkeyInputHandler( &phantom ); } else if( ( keycode == SDLK_LALT ) || ( keycode == SDLK_RALT ) ) { - (int&)phantom.ev.key.keysym.sym = UNIFIED_ALT; + phantom.ev.key.keysym.sym = (SDLKey)UNIFIED_ALT; unified[2] = ( phantom.ev.type == SDL_KEYDOWN ); HotkeyInputHandler( &phantom ); } else if( ( keycode == SDLK_LMETA ) || ( keycode == SDLK_RMETA ) ) { - (int&)phantom.ev.key.keysym.sym = UNIFIED_META; + phantom.ev.key.keysym.sym = (SDLKey)UNIFIED_META; unified[3] = ( phantom.ev.type == SDL_KEYDOWN ); HotkeyInputHandler( &phantom ); } else if( ( keycode == SDLK_LSUPER ) || ( keycode == SDLK_RSUPER ) ) { - (int&)phantom.ev.key.keysym.sym = UNIFIED_SUPER; + phantom.ev.key.keysym.sym = (SDLKey)UNIFIED_SUPER; unified[4] = ( phantom.ev.type == SDL_KEYDOWN ); HotkeyInputHandler( &phantom ); } diff --git a/source/ps/Player.cpp b/source/ps/Player.cpp index e5f0eabc55..1d0377ac72 100644 --- a/source/ps/Player.cpp +++ b/source/ps/Player.cpp @@ -41,7 +41,7 @@ CPlayer::CPlayer(uint playerID): for(int i=0; i<=PS_MAX_PLAYERS; i++) { CStrW name = CStrW(L"diplomaticStance_") + CStrW(i); - ISynchedJSProperty *prop=new CSynchedJSProperty(name, (int*)&m_DiplomaticStance[i], this); + ISynchedJSProperty *prop=new CSynchedJSProperty(name, &m_DiplomaticStance[i], this); m_SynchedProperties[name]=prop; } } @@ -66,11 +66,11 @@ void CPlayer::ScriptingInit() g_ScriptingHost.DefineConstant("DIPLOMACY_NEUTRAL", DIPLOMACY_NEUTRAL); g_ScriptingHost.DefineConstant("DIPLOMACY_ALLIED", DIPLOMACY_ALLIED); - AddMethod( "toString", 0 ); - AddMethod( "setColour", 1); - AddMethod( "getColour", 0); - AddMethod( "setDiplomaticStance", 2); - AddMethod( "getDiplomaticStance", 1); + AddMethod("toString", 0); + AddMethod("setColour", 1); + AddMethod("getColour", 0); + AddMethod("setDiplomaticStance", 2); + AddMethod("getDiplomaticStance", 1); AddProperty( L"id", &CPlayer::m_PlayerID, true ); // MT: Work out how this fits with the Synched stuff... @@ -112,13 +112,9 @@ void CPlayer::GetControlledEntities(std::vector& controlled_entities) g_EntityManager.GetMatchingAsHandles( controlled_entities, ControllerPredicate, this ); } -jsval CPlayer::JSI_ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) +CStrW CPlayer::JSI_ToString( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - wchar_t buffer[256]; - swprintf( buffer, 256, L"[object Player: %ls]", m_Name.c_str() ); - buffer[255] = 0; - utf16string str16(buffer, buffer+wcslen(buffer)); - return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) ); + return L"[object Player: " + m_Name + L"]"; } jsval CPlayer::JSI_GetControlledEntities(JSContext* UNUSED(cx)) @@ -129,20 +125,14 @@ jsval CPlayer::JSI_GetControlledEntities(JSContext* UNUSED(cx)) return( vp ); } -jsval CPlayer::JSI_SetColour( JSContext* UNUSED(cx), uintN argc, jsval* argv ) +void CPlayer::JSI_SetColour( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* argv ) { - if (argc != 1) - return JSVAL_NULL; - m_Colour=*( ToNative(argv[0]) ); ISynchedJSProperty *prop=GetSynchedProperty(L"colour"); Update(L"colour", prop); - - // Return something that isn't null, so users can check whether this function succeeded - return argv[0]; } -jsval CPlayer::JSI_GetColour( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CPlayer::JSI_GetColour( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { //ISynchedJSProperty *prop=GetSynchedProperty(L"colour"); //return prop->Get(cx, this); @@ -150,35 +140,32 @@ jsval CPlayer::JSI_GetColour( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* return ToJSVal(col); } -jsval CPlayer::JSI_SetDiplomaticStance(JSContext *cx, uintN argc, jsval *argv) +void CPlayer::JSI_SetDiplomaticStance(JSContext *cx, uintN UNUSED(argc), jsval *argv) { - JSU_ASSERT(argc==2, "2 arguments required"); - JSU_ASSERT( JSVAL_IS_INT(argv[1]), "Argument 2 must be a valid stance ID" ); try { CPlayer* player = ToPrimitive( argv[0] ); int stance = ToPrimitive( argv[1] ); - JSU_ASSERT( stance==DIPLOMACY_ENEMY || stance==DIPLOMACY_NEUTRAL || stance==DIPLOMACY_ALLIED, - "Argument 2 must be a valid stance ID" ); + if (! (stance==DIPLOMACY_ENEMY || stance==DIPLOMACY_NEUTRAL || stance==DIPLOMACY_ALLIED)) + { + JS_ReportError(cx, "Argument 2 must be a valid stance ID"); + return; + } m_DiplomaticStance[player->m_PlayerID] = (EDiplomaticStance) stance; CStrW name = CStrW(L"diplomaticStance_") + CStrW(player->m_PlayerID); ISynchedJSProperty *prop=GetSynchedProperty(name); Update(name, prop); - - return JSVAL_VOID; } catch( PSERROR_Scripting_ConversionFailed ) { JS_ReportError( cx, "Could not convert argument 1 to a Player object" ); - return JSVAL_VOID; } } -jsval CPlayer::JSI_GetDiplomaticStance(JSContext *cx, uintN argc, jsval *argv) +jsval_t CPlayer::JSI_GetDiplomaticStance(JSContext *cx, uintN UNUSED(argc), jsval *argv) { - JSU_ASSERT(argc==1, "1 argument required"); try { CPlayer* player = ToPrimitive( argv[0] ); diff --git a/source/ps/Player.h b/source/ps/Player.h index 7909f309f1..bceaae7499 100644 --- a/source/ps/Player.h +++ b/source/ps/Player.h @@ -31,7 +31,7 @@ private: PS_uint m_PlayerID; PS_uint m_LOSToken; SPlayerColour m_Colour; - EDiplomaticStance m_DiplomaticStance[PS_MAX_PLAYERS+1]; + int /*EDiplomaticStance*/ m_DiplomaticStance[PS_MAX_PLAYERS+1]; std::vector m_ActiveTechs; UpdateCallback *m_UpdateCB; @@ -64,7 +64,7 @@ public: { m_Colour = colour; } inline EDiplomaticStance GetDiplomaticStance(CPlayer* other) const - { return m_DiplomaticStance[other->m_PlayerID]; } + { return (EDiplomaticStance)m_DiplomaticStance[other->m_PlayerID]; } inline void SetDiplomaticStance(CPlayer* other, EDiplomaticStance stance) { m_DiplomaticStance[other->m_PlayerID] = stance; } @@ -88,12 +88,12 @@ public: void GetControlledEntities(std::vector& controlled_entities); // JS Interface Functions - jsval JSI_ToString( JSContext* context, uintN argc, jsval* argv ); + CStrW JSI_ToString( JSContext* context, uintN argc, jsval* argv ); jsval JSI_GetControlledEntities( JSContext* context ); - jsval JSI_SetColour(JSContext *context, uintN argc, jsval *argv); - jsval JSI_GetColour(JSContext *context, uintN argc, jsval *argv); - jsval JSI_SetDiplomaticStance(JSContext *context, uintN argc, jsval *argv); - jsval JSI_GetDiplomaticStance(JSContext *context, uintN argc, jsval *argv); + void JSI_SetColour(JSContext *context, uintN argc, jsval *argv); + jsval_t JSI_GetColour(JSContext *context, uintN argc, jsval *argv); + void JSI_SetDiplomaticStance(JSContext *context, uintN argc, jsval *argv); + jsval_t JSI_GetDiplomaticStance(JSContext *context, uintN argc, jsval *argv); static void ScriptingInit(); }; diff --git a/source/scripting/DOMEvent.cpp b/source/scripting/DOMEvent.cpp index 5f364ad074..2cfd61f857 100644 --- a/source/scripting/DOMEvent.cpp +++ b/source/scripting/DOMEvent.cpp @@ -163,10 +163,10 @@ CScriptEvent::CScriptEvent( const CStrW& Type, unsigned int TypeCode, bool Cance void CScriptEvent::ScriptingInit() { - AddMethod( "toString", 0 ); - AddMethod( "preventDefault", 0 ); - AddMethod( "cancel", 0 ); - AddMethod( "stopPropagation", 0 ); + AddMethod( "toString", 0 ); + AddMethod( "preventDefault", 0 ); + AddMethod( "cancel", 0 ); + AddMethod( "stopPropagation", 0 ); AddProperty( L"type", &CScriptEvent::m_Type, true ); AddProperty( L"cancelable", &CScriptEvent::m_Cancelable, true ); @@ -176,26 +176,19 @@ void CScriptEvent::ScriptingInit() CJSObject::ScriptingInit( "Event" ); } -jsval CScriptEvent::PreventDefault( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +void CScriptEvent::PreventDefault( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { if( m_Cancelable ) m_Cancelled = true; - return( JSVAL_VOID ); } -jsval CScriptEvent::StopPropagation( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +void CScriptEvent::StopPropagation( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { if( m_Blockable ) m_Blocked = true; - return( JSVAL_VOID ); } -jsval CScriptEvent::ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) +CStr CScriptEvent::ToString( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - wchar_t buffer[256]; - swprintf( buffer, 256, L"[object Event: %ls]", m_Type.c_str() ); - buffer[255] = 0; - utf16string str16=utf16string(buffer, buffer+wcslen(buffer)); - return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) ); + return "[object Event: " + CStr(m_Type) + "]"; } - diff --git a/source/scripting/DOMEvent.h b/source/scripting/DOMEvent.h index 80485c3cd6..9e1e57ecfb 100644 --- a/source/scripting/DOMEvent.h +++ b/source/scripting/DOMEvent.h @@ -121,9 +121,9 @@ public: // -- - jsval ToString( JSContext* cx, uintN argc, jsval* argv ); - jsval PreventDefault( JSContext* cx, uintN argc, jsval* argv ); - jsval StopPropagation( JSContext* cx, uintN argc, jsval* argv ); + CStr ToString( JSContext* cx, uintN argc, jsval* argv ); + void PreventDefault( JSContext* cx, uintN argc, jsval* argv ); + void StopPropagation( JSContext* cx, uintN argc, jsval* argv ); public: CScriptEvent( const CStrW& Type, unsigned int TypeCode = (unsigned int)-1, bool Cancelable = true, bool Blockable = true ); diff --git a/source/scripting/JSConversions.cpp b/source/scripting/JSConversions.cpp index d2af7d26bd..1602f5a595 100644 --- a/source/scripting/JSConversions.cpp +++ b/source/scripting/JSConversions.cpp @@ -251,9 +251,9 @@ template<> jsval ToJSVal( CStr8& Native ) // jsval -template<> jsval ToJSVal( const jsval& Native ) +template<> jsval ToJSVal( const jsval_t& Native ) { - return( Native ); + return( Native.v ); } // String->JSVal diff --git a/source/scripting/JSConversions.h b/source/scripting/JSConversions.h index 0ea4fd9ce7..f14abc74c2 100644 --- a/source/scripting/JSConversions.h +++ b/source/scripting/JSConversions.h @@ -158,7 +158,15 @@ template<> jsval ToJSVal( CStr8& Native ); // jsval -template<> jsval ToJSVal( const jsval& Native ); +// Don't want to just use jsval directly, because it's equivalent to long and +// can cause conflicts or confusion. So create a simple wrapper class for it, +// so it's a real distinguishable type. +struct jsval_t +{ + jsval v; + jsval_t(jsval v) : v(v) {} +}; +template<> jsval ToJSVal( const jsval_t& Native ); // Intelligent CStrW->JSVal conversion diff --git a/source/scripting/JSSerialization.h b/source/scripting/JSSerialization.h index 669072dc1c..58f51f0c43 100644 --- a/source/scripting/JSSerialization.h +++ b/source/scripting/JSSerialization.h @@ -111,11 +111,14 @@ public: } break; case TAG_DOUBLE: - // Ehm. I think this works, but I can't say as it's something I've tried before. { - u64 ival; - Deserialize_int_8( buffer, ival ); - JS_NewDoubleValue( g_ScriptingHost.GetContext(), *( (double*)(&ival) ), &m_data ); + union { + u64 ival; + double dval; + } val; + cassert(sizeof(val.ival) == sizeof(val.dval)); + Deserialize_int_8( buffer, val.ival ); + JS_NewDoubleValue( g_ScriptingHost.GetContext(), val.dval, &m_data ); } break; case TAG_STRING: diff --git a/source/scripting/ScriptCustomTypes.cpp b/source/scripting/ScriptCustomTypes.cpp index 95130281c2..9ab9e970e5 100644 --- a/source/scripting/ScriptCustomTypes.cpp +++ b/source/scripting/ScriptCustomTypes.cpp @@ -47,7 +47,7 @@ void SColour::SColourInit( float _r, float _g, float _b, float _a ) void SColour::ScriptingInit() { - AddMethod( "toString", 0 ); + AddMethod( "toString", 0 ); AddProperty( L"r", (float IJSObject::*)&SColour::r ); AddProperty( L"g", (float IJSObject::*)&SColour::g ); AddProperty( L"b", (float IJSObject::*)&SColour::b ); @@ -56,15 +56,9 @@ void SColour::ScriptingInit() CJSObject::ScriptingInit( "Colour", SColour::Construct, 3 ); } -jsval SColour::ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) +CStr SColour::ToString( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - wchar_t buffer[256]; - - swprintf( buffer, 256, L"[object Colour: ( %f, %f, %f, %f )]", r, g, b, a ); - buffer[255] = 0; - - utf16string str16(buffer, buffer+wcslen(buffer)); - return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) ); + return "[object Colour: ( " + CStr(r) + ", " + CStr(g) + ", " + CStr(b) + ", " + CStr(a) + " )]"; } diff --git a/source/scripting/ScriptCustomTypes.h b/source/scripting/ScriptCustomTypes.h index eec1bf65f5..796821b334 100644 --- a/source/scripting/ScriptCustomTypes.h +++ b/source/scripting/ScriptCustomTypes.h @@ -26,7 +26,7 @@ public: SColour &operator = (const SColour &o); - jsval ToString( JSContext* cx, uintN argc, jsval* argv ); + CStr ToString( JSContext* cx, uintN argc, jsval* argv ); static void ScriptingInit(); static JSBool Construct( JSContext* cx, JSObject* obj, uint argc, jsval* argv, jsval* rval ); }; diff --git a/source/scripting/ScriptableObject.h b/source/scripting/ScriptableObject.h index 7903cdbbcf..0183489550 100644 --- a/source/scripting/ScriptableObject.h +++ b/source/scripting/ScriptableObject.h @@ -171,6 +171,23 @@ public: } }; +// Special case for void functions +template +class CNativeFunction +{ +public: + static JSBool JSFunction( JSContext* cx, JSObject* obj, uintN argc, jsval* argv, jsval* UNUSED(rval) ) + { + T* Native = ToNative( cx, obj ); + if( !Native ) + return( JS_TRUE ); + + (Native->*NativeFunction)( cx, argc, argv ); + + return( JS_TRUE ); + } +}; + template class CJSObject : public IJSObject { // This object diff --git a/source/simulation/Entity.h b/source/simulation/Entity.h index a47fca7c64..0c59b1e213 100644 --- a/source/simulation/Entity.h +++ b/source/simulation/Entity.h @@ -371,53 +371,53 @@ public: void DestroyAllNotifiers(); int FindSector( int divs, float angle, float maxAngle, bool negative=true ); - jsval FlattenTerrain( JSContext* cx, uintN argc, jsval* argv ); + jsval_t FlattenTerrain( JSContext* cx, uintN argc, jsval* argv ); CEntityFormation* GetFormation(); - jsval GetFormationPenalty( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationPenaltyBase( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationPenaltyType( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationPenaltyVal( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationPenalty( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationPenaltyBase( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationPenaltyType( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationPenaltyVal( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationBonus( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationBonusBase( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationBonusType( JSContext* cx, uintN argc, jsval* argv ); - jsval GetFormationBonusVal( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationBonus( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationBonusBase( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationBonusType( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetFormationBonusVal( JSContext* cx, uintN argc, jsval* argv ); void DispatchFormationEvent( int type ); - jsval RegisterDamage( JSContext* cx, uintN argc, jsval* argv ); - jsval RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ); - jsval GetAttackDirections( JSContext* cx, uintN argc, jsval* argv ); + jsval_t RegisterDamage( JSContext* cx, uintN argc, jsval* argv ); + jsval_t RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetAttackDirections( JSContext* cx, uintN argc, jsval* argv ); - jsval FindSector( JSContext* cx, uintN argc, jsval* argv ); + jsval_t FindSector( JSContext* cx, uintN argc, jsval* argv ); // Script constructor static JSBool Construct( JSContext* cx, JSObject* obj, uint argc, jsval* argv, jsval* rval ); // Script-bound functions - jsval ToString( JSContext* cx, uintN argc, jsval* argv ); + jsval_t ToString( JSContext* cx, uintN argc, jsval* argv ); bool Kill( JSContext* cx, uintN argc, jsval* argv ); - jsval GetSpawnPoint( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetSpawnPoint( JSContext* cx, uintN argc, jsval* argv ); - inline jsval HasRallyPoint( JSContext* cx, uintN argc, jsval* argv ); - inline jsval GetRallyPoint( JSContext* cx, uintN argc, jsval* argv ); - inline jsval SetRallyPoint( JSContext* cx, uintN argc, jsval* argv ); + jsval_t HasRallyPoint( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetRallyPoint( JSContext* cx, uintN argc, jsval* argv ); + jsval_t SetRallyPoint( JSContext* cx, uintN argc, jsval* argv ); - jsval AddAura( JSContext* cx, uintN argc, jsval* argv ); - jsval RemoveAura( JSContext* cx, uintN argc, jsval* argv ); + jsval_t AddAura( JSContext* cx, uintN argc, jsval* argv ); + jsval_t RemoveAura( JSContext* cx, uintN argc, jsval* argv ); - jsval SetActionParams( JSContext* cx, uintN argc, jsval* argv ); - jsval TriggerRun( JSContext* cx, uintN argc, jsval* argv ); - jsval SetRun( JSContext* cx, uintN argc, jsval* argv ); - jsval IsRunning( JSContext* cx, uintN argc, jsval* argv ); - jsval GetRunState( JSContext* cx, uintN argc, jsval* argv ); + jsval_t SetActionParams( JSContext* cx, uintN argc, jsval* argv ); + jsval_t TriggerRun( JSContext* cx, uintN argc, jsval* argv ); + jsval_t SetRun( JSContext* cx, uintN argc, jsval* argv ); + jsval_t IsRunning( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetRunState( JSContext* cx, uintN argc, jsval* argv ); - jsval OnDamaged( JSContext* cx, uintN argc, jsval* argv ); + jsval_t OnDamaged( JSContext* cx, uintN argc, jsval* argv ); - jsval GetVisibleEntities( JSContext* cx, uintN argc, jsval* argv ); + jsval_t GetVisibleEntities( JSContext* cx, uintN argc, jsval* argv ); float GetDistance( JSContext* cx, uintN argc, jsval* argv ); @@ -426,8 +426,8 @@ public: bool ForceCheckListeners( JSContext* cx, uintN argc, jsval* argv ); int GetCurrentRequest( JSContext* cx, uintN argc, jsval* argv ); void CheckListeners( int type, CEntity *target ); - jsval DestroyAllNotifiers( JSContext* cx, uintN argc, jsval* argv ); - jsval DestroyNotifier( JSContext* cx, uintN argc, jsval* argv ); + jsval_t DestroyAllNotifiers( JSContext* cx, uintN argc, jsval* argv ); + jsval_t DestroyNotifier( JSContext* cx, uintN argc, jsval* argv ); jsval JSI_GetPlayer(); void JSI_SetPlayer(jsval val); @@ -464,7 +464,7 @@ public: return( m_classes.IsMember( ToPrimitive( cx, argv[0] ) ) ); } - jsval TerminateOrder( JSContext* UNUSED(cx), uintN argc, jsval* argv ) + jsval_t TerminateOrder( JSContext* UNUSED(cx), uintN argc, jsval* argv ) { debug_assert( argc >= 1); if ( ToPrimitive( argv[0] ) ) @@ -474,7 +474,7 @@ public: return JSVAL_VOID; } - jsval GetHeight( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) + jsval_t GetHeight( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal(m_position.Y); } diff --git a/source/simulation/EntityOrders.h b/source/simulation/EntityOrders.h index 477125d7f0..32b17a6419 100644 --- a/source/simulation/EntityOrders.h +++ b/source/simulation/EntityOrders.h @@ -52,7 +52,7 @@ struct SOrderData class CEntityListener { public: - enum + enum EType { NOTIFY_NONE = 0x00, diff --git a/source/simulation/EntityScriptInterface.cpp b/source/simulation/EntityScriptInterface.cpp index 4c6c15fccb..9a4afdbb02 100644 --- a/source/simulation/EntityScriptInterface.cpp +++ b/source/simulation/EntityScriptInterface.cpp @@ -46,48 +46,50 @@ extern int g_xres, g_yres; void CEntity::ScriptingInit() { - AddMethod( "toString", 0 ); + // TODO: lots of these return jsval when they should return proper types + // and make use of automatic type conversion + AddMethod( "toString", 0 ); AddMethod( "order", 1 ); AddMethod( "orderQueued", 1 ); AddMethod( "orderFromTriggers", 1 ); - AddMethod( "terminateOrder", 1 ); + AddMethod( "terminateOrder", 1 ); AddMethod( "kill", 0 ); AddMethod( "isIdle", 0 ); AddMethod( "hasClass", 1 ); - AddMethod( "getSpawnPoint", 1 ); - AddMethod( "addAura", 3 ); - AddMethod( "removeAura", 1 ); - AddMethod( "setActionParams", 5 ); + AddMethod( "getSpawnPoint", 1 ); + AddMethod( "addAura", 3 ); + AddMethod( "removeAura", 1 ); + AddMethod( "setActionParams", 5 ); AddMethod( "getCurrentRequest", 0 ); AddMethod( "forceCheckListeners", 2 ); AddMethod( "requestNotification", 4 ); - AddMethod( "destroyNotifier", 1 ); - AddMethod( "destroyAllNotifiers", 0 ); - AddMethod( "triggerRun", 1 ); - AddMethod( "setRun", 1 ); - AddMethod( "getRunState", 0 ); + AddMethod( "destroyNotifier", 1 ); + AddMethod( "destroyAllNotifiers", 0 ); + AddMethod( "triggerRun", 1 ); + AddMethod( "setRun", 1 ); + AddMethod( "getRunState", 0 ); AddMethod( "isInFormation", 0 ); - AddMethod( "getFormationBonus", 0 ); - AddMethod( "getFormationBonusType", 0 ); - AddMethod( "getFormationBonusVal", 0 ); - AddMethod( "getFormationPenalty", 0 ); - AddMethod( "getFormationPenaltyType", 0 ); - AddMethod( "getFormationPenaltyVal", 0 ); - AddMethod( "registerDamage", 0 ); - AddMethod( "registerOrderChange", 0 ); - AddMethod( "getAttackDirections", 0 ); - AddMethod( "findSector", 4); - AddMethod( "getHeight", 0 ); - AddMethod( "hasRallyPoint", 0 ); - AddMethod( "setRallyPoint", 0 ); - AddMethod( "getRallyPoint", 0 ); - AddMethod( "onDamaged", 1 ); - AddMethod( "getVisibleEntities", 0 ); + AddMethod( "getFormationBonus", 0 ); + AddMethod( "getFormationBonusType", 0 ); + AddMethod( "getFormationBonusVal", 0 ); + AddMethod( "getFormationPenalty", 0 ); + AddMethod( "getFormationPenaltyType", 0 ); + AddMethod( "getFormationPenaltyVal", 0 ); + AddMethod( "registerDamage", 0 ); + AddMethod( "registerOrderChange", 0 ); + AddMethod( "getAttackDirections", 0 ); + AddMethod( "findSector", 4); + AddMethod( "getHeight", 0 ); + AddMethod( "hasRallyPoint", 0 ); + AddMethod( "setRallyPoint", 0 ); + AddMethod( "getRallyPoint", 0 ); + AddMethod( "onDamaged", 1 ); + AddMethod( "getVisibleEntities", 0 ); AddMethod( "getDistance", 1 ); - AddMethod( "flattenTerrain", 0 ); + AddMethod( "flattenTerrain", 0 ); - AddClassProperty( L"traits.id.classes", (GetFn)&CEntity::GetClassSet, (SetFn)&CEntity::SetClassSet ); - AddClassProperty( L"template", (CEntityTemplate* CEntity::*)&CEntity::m_base, false, (NotifyFn)&CEntity::LoadBase ); + AddClassProperty( L"traits.id.classes", static_cast(&CEntity::GetClassSet), static_cast(&CEntity::SetClassSet) ); + AddClassProperty( L"template", static_cast(&CEntity::m_base), false, static_cast(&CEntity::LoadBase) ); /* Any inherited property MUST be added to EntityTemplate.cpp as well */ @@ -97,13 +99,13 @@ void CEntity::ScriptingInit() AddClassProperty( L"actions.move.run.range", &CEntity::m_runMaxRange ); AddClassProperty( L"actions.move.run.regenRate", &CEntity::m_runRegenRate ); AddClassProperty( L"actions.move.run.decayRate", &CEntity::m_runDecayRate ); - AddClassProperty( L"selected", &CEntity::m_selected, false, (NotifyFn)&CEntity::CheckSelection ); - AddClassProperty( L"group", &CEntity::m_grouped, false, (NotifyFn)&CEntity::CheckGroup ); + AddClassProperty( L"selected", &CEntity::m_selected, false, static_cast(&CEntity::CheckSelection) ); + AddClassProperty( L"group", &CEntity::m_grouped, false, static_cast(&CEntity::CheckGroup) ); AddClassProperty( L"traits.extant", &CEntity::m_extant ); AddClassProperty( L"actions.move.turningRadius", &CEntity::m_turningRadius ); - AddClassProperty( L"position", &CEntity::m_position, false, (NotifyFn)&CEntity::Teleport ); - AddClassProperty( L"orientation", &CEntity::m_orientation, false, (NotifyFn)&CEntity::Reorient ); - AddClassProperty( L"player", (GetFn)&CEntity::JSI_GetPlayer, (SetFn)&CEntity::JSI_SetPlayer ); + AddClassProperty( L"position", &CEntity::m_position, false, static_cast(&CEntity::Teleport) ); + AddClassProperty( L"orientation", &CEntity::m_orientation, false, static_cast(&CEntity::Reorient) ); + AddClassProperty( L"player", static_cast(&CEntity::JSI_GetPlayer), static_cast(&CEntity::JSI_SetPlayer) ); AddClassProperty( L"traits.health.curr", &CEntity::m_healthCurr ); AddClassProperty( L"traits.health.max", &CEntity::m_healthMax ); AddClassProperty( L"traits.health.regenRate", &CEntity::m_healthRegenRate ); @@ -113,7 +115,7 @@ void CEntity::ScriptingInit() AddClassProperty( L"traits.stamina.max", &CEntity::m_staminaMax ); AddClassProperty( L"traits.rank.name", &CEntity::m_rankName ); AddClassProperty( L"traits.vision.los", &CEntity::m_los ); - AddClassProperty( L"traits.ai.stance.curr", &CEntity::m_stanceName, false, (NotifyFn)&CEntity::StanceChanged ); + AddClassProperty( L"traits.ai.stance.curr", &CEntity::m_stanceName, false, static_cast(&CEntity::StanceChanged) ); AddClassProperty( L"lastCombatTime", &CEntity::m_lastCombatTime ); AddClassProperty( L"lastRunTime", &CEntity::m_lastRunTime ); AddClassProperty( L"building", &CEntity::m_building ); @@ -208,7 +210,7 @@ JSBool CEntity::Construct( JSContext* cx, JSObject* UNUSED(obj), uint argc, jsva // Script-bound methods -jsval CEntity::ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) { CStrW name( L"[object Entity: " + m_base->m_Tag + L"]" ); return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, name.utf16().c_str() ) ) ); @@ -267,7 +269,7 @@ bool CEntity::Order( JSContext* cx, uintN argc, jsval* argv, CEntityOrder::EOrde CEntity* target; - (int&)newOrder.m_type = orderCode; + newOrder.m_type = (CEntityOrder::EOrderType)orderCode; switch( orderCode ) { @@ -359,7 +361,7 @@ bool CEntity::Kill( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(arg return( true ); } -jsval CEntity::GetSpawnPoint( JSContext* UNUSED(cx), uintN argc, jsval* argv ) +jsval_t CEntity::GetSpawnPoint( JSContext* UNUSED(cx), uintN argc, jsval* argv ) { float spawn_clearance = 2.0f; if( argc >= 1 ) @@ -501,7 +503,7 @@ jsval CEntity::GetSpawnPoint( JSContext* UNUSED(cx), uintN argc, jsval* argv ) return( JSVAL_NULL ); } -jsval CEntity::AddAura( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::AddAura( JSContext* cx, uintN argc, jsval* argv ) { debug_assert( argc >= 8 ); debug_assert( JSVAL_IS_OBJECT(argv[7]) ); @@ -527,7 +529,7 @@ jsval CEntity::AddAura( JSContext* cx, uintN argc, jsval* argv ) return JSVAL_VOID; } -jsval CEntity::RemoveAura( JSContext* UNUSED(cx), uintN argc, jsval* argv ) +jsval_t CEntity::RemoveAura( JSContext* UNUSED(cx), uintN argc, jsval* argv ) { debug_assert( argc >= 1 ); CStrW name = ToPrimitive( argv[0] ); @@ -542,7 +544,7 @@ jsval CEntity::RemoveAura( JSContext* UNUSED(cx), uintN argc, jsval* argv ) return JSVAL_VOID; } -jsval CEntity::SetActionParams( JSContext* UNUSED(cx), uintN argc, jsval* argv ) +jsval_t CEntity::SetActionParams( JSContext* UNUSED(cx), uintN argc, jsval* argv ) { debug_assert( argc == 5 ); @@ -563,7 +565,7 @@ bool CEntity::RequestNotification( JSContext* cx, uintN argc, jsval* argv ) CEntityListener notify; CEntity *target = ToNative( argv[0] ); - (int&)notify.m_type = ToPrimitive( argv[1] ); + notify.m_type = (CEntityListener::EType)ToPrimitive( argv[1] ); bool tmpDestroyNotifiers = ToPrimitive( argv[2] ); entf_set_to(ENTF_DESTROY_NOTIFIERS, !ToPrimitive( argv[3] )); @@ -682,25 +684,25 @@ void CEntity::CheckListeners( int type, CEntity *target) } } } -jsval CEntity::DestroyAllNotifiers( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::DestroyAllNotifiers( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { DestroyAllNotifiers(); return JS_TRUE; } -jsval CEntity::DestroyNotifier( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::DestroyNotifier( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(1); DestroyNotifier( ToNative( argv[0] ) ); return JS_TRUE; } -jsval CEntity::TriggerRun( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::TriggerRun( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { entf_set(ENTF_TRIGGER_RUN); return JSVAL_VOID; } -jsval CEntity::SetRun( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::SetRun( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(1); bool should_run = ToPrimitive ( argv[0] ); @@ -708,44 +710,44 @@ jsval CEntity::SetRun( JSContext* cx, uintN argc, jsval* argv ) entf_set_to(ENTF_IS_RUNNING, should_run); return JSVAL_VOID; } -jsval CEntity::GetRunState( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetRunState( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return BOOLEAN_TO_JSVAL( entf_get(ENTF_SHOULD_RUN) ); } -jsval CEntity::GetFormationPenalty( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationPenalty( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetPenalty() ); } -jsval CEntity::GetFormationPenaltyBase( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationPenaltyBase( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetPenaltyBase() ); } -jsval CEntity::GetFormationPenaltyType( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationPenaltyType( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetPenaltyType() ); } -jsval CEntity::GetFormationPenaltyVal( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationPenaltyVal( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetPenaltyVal() ); } -jsval CEntity::GetFormationBonus( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationBonus( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetBonus() ); } -jsval CEntity::GetFormationBonusBase( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationBonusBase( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetBonusBase() ); } -jsval CEntity::GetFormationBonusType( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationBonusType( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetBonusType() ); } -jsval CEntity::GetFormationBonusVal( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetFormationBonusVal( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( GetFormation()->GetBase()->GetBonusVal() ); } -jsval CEntity::RegisterDamage( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::RegisterDamage( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(1); CEntity* inflictor = ToNative( argv[0] ); @@ -759,7 +761,7 @@ jsval CEntity::RegisterDamage( JSContext* cx, uintN argc, jsval* argv ) m_sectorValues[sector]=true; return JS_TRUE; } -jsval CEntity::RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(1); CEntity* idleEntity = ToNative( argv[0] ); @@ -774,7 +776,7 @@ jsval CEntity::RegisterOrderChange( JSContext* cx, uintN argc, jsval* argv ) m_sectorValues[sector]=false; return JS_TRUE; } -jsval CEntity::GetAttackDirections( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetAttackDirections( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { int directions=0; @@ -785,7 +787,7 @@ jsval CEntity::GetAttackDirections( JSContext* UNUSED(cx), uintN UNUSED(argc), j } return ToJSVal( directions ); } -jsval CEntity::FindSector( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::FindSector( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(4); @@ -804,15 +806,15 @@ jsval CEntity::FindSector( JSContext* cx, uintN argc, jsval* argv ) return 0; } } -jsval CEntity::HasRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::HasRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( entf_get(ENTF_HAS_RALLY_POINT) ); } -jsval CEntity::GetRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { return ToJSVal( m_rallyPoint ); } -jsval CEntity::SetRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::SetRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { entf_set(ENTF_HAS_RALLY_POINT); m_rallyPoint = g_Game->GetView()->GetCamera()->GetWorldCoordinates(true); @@ -820,7 +822,7 @@ jsval CEntity::SetRallyPoint( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* } // Called by the script when the entity is damaged, to let it retaliate -jsval CEntity::OnDamaged( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CEntity::OnDamaged( JSContext* cx, uintN argc, jsval* argv ) { JSU_REQUIRE_PARAMS_CPP(1); CEntity* damageSource = ToNative( argv[0] ); @@ -828,7 +830,7 @@ jsval CEntity::OnDamaged( JSContext* cx, uintN argc, jsval* argv ) return JSVAL_VOID; } -jsval CEntity::GetVisibleEntities( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::GetVisibleEntities( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { std::vector pointers; g_EntityManager.GetInLOS( this, pointers ); @@ -870,7 +872,7 @@ int CEntity::GetAttackAction( HEntity target ) return 0; } -jsval CEntity::FlattenTerrain( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +jsval_t CEntity::FlattenTerrain( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { float xDiff, yDiff; CVector3D pos = m_position; diff --git a/source/simulation/EntityTemplate.cpp b/source/simulation/EntityTemplate.cpp index b54a7b1f1c..4abbe60583 100644 --- a/source/simulation/EntityTemplate.cpp +++ b/source/simulation/EntityTemplate.cpp @@ -407,9 +407,9 @@ void CEntityTemplate::XMLLoadProperty( const CXeromyces& XeroFile, const XMBElem void CEntityTemplate::ScriptingInit() { - AddMethod( "toString", 0 ); + AddMethod( "toString", 0 ); - AddClassProperty( L"traits.id.classes", (GetFn)&CEntityTemplate::GetClassSet, (SetFn)&CEntityTemplate::SetClassSet ); + AddClassProperty( L"traits.id.classes", static_cast(&CEntityTemplate::GetClassSet), static_cast(&CEntityTemplate::SetClassSet) ); AddClassProperty( L"actions.move.speed", &CEntityTemplate::m_speed ); AddClassProperty( L"actions.move.turningRadius", &CEntityTemplate::m_turningRadius ); @@ -420,8 +420,8 @@ void CEntityTemplate::ScriptingInit() AddClassProperty( L"actions.move.run.decayRate", &CEntityTemplate::m_runDecayRate ); AddClassProperty( L"actions.move.passThroughAllies", &CEntityTemplate::m_passThroughAllies ); AddClassProperty( L"actor", &CEntityTemplate::m_actorName ); - AddClassProperty( L"traits.health.max", &CEntityTemplate::m_healthMax ); - AddClassProperty( L"traits.health.barHeight", &CEntityTemplate::m_healthBarHeight ); + AddClassProperty( L"traits.health.max", &CEntityTemplate::m_healthMax ); + AddClassProperty( L"traits.health.barHeight", &CEntityTemplate::m_healthBarHeight ); AddClassProperty( L"traits.health.barSize", &CEntityTemplate::m_healthBarSize ); AddClassProperty( L"traits.health.barWidth", &CEntityTemplate::m_healthBarWidth ); AddClassProperty( L"traits.health.borderHeight", &CEntityTemplate::m_healthBorderHeight); @@ -430,8 +430,8 @@ void CEntityTemplate::ScriptingInit() AddClassProperty( L"traits.health.regenRate", &CEntityTemplate::m_healthRegenRate ); AddClassProperty( L"traits.health.regenStart", &CEntityTemplate::m_healthRegenStart ); AddClassProperty( L"traits.health.decayRate", &CEntityTemplate::m_healthDecayRate ); - AddClassProperty( L"traits.stamina.max", &CEntityTemplate::m_staminaMax ); - AddClassProperty( L"traits.stamina.barHeight", &CEntityTemplate::m_staminaBarHeight ); + AddClassProperty( L"traits.stamina.max", &CEntityTemplate::m_staminaMax ); + AddClassProperty( L"traits.stamina.barHeight", &CEntityTemplate::m_staminaBarHeight ); AddClassProperty( L"traits.stamina.barSize", &CEntityTemplate::m_staminaBarSize ); AddClassProperty( L"traits.stamina.barWidth", &CEntityTemplate::m_staminaBarWidth ); AddClassProperty( L"traits.stamina.borderHeight", &CEntityTemplate::m_staminaBorderHeight); @@ -477,14 +477,10 @@ JSObject* CEntityTemplate::GetScriptExecContext( IEventTarget* target ) return( target->GetScriptExecContext( target ) ); } -jsval CEntityTemplate::ToString( JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv) ) +CStr CEntityTemplate::ToString( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - wchar_t buffer[256]; if( m_player == 0 ) - swprintf( buffer, 256, L"[object EntityTemplate: %ls base]", m_Tag.c_str() ); + return "[object EntityTemplate: " + CStr(m_Tag) + " base]"; else - swprintf( buffer, 256, L"[object EntityTemplate: %ls for player %d]", m_Tag.c_str(), m_player->GetPlayerID() ); - buffer[255] = 0; - utf16string str16(buffer, buffer+wcslen(buffer)); - return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) ); + return "[object EntityTemplate: " + CStr(m_Tag) + " for player " + CStr(m_player->GetPlayerID()) + "]"; } diff --git a/source/simulation/EntityTemplate.h b/source/simulation/EntityTemplate.h index c41b8c005c..8391511cef 100644 --- a/source/simulation/EntityTemplate.h +++ b/source/simulation/EntityTemplate.h @@ -164,7 +164,7 @@ public: // Get script execution contexts - always run in the context of the entity that fired it. JSObject* GetScriptExecContext(IEventTarget* target); - jsval ToString(JSContext* cx, uintN argc, jsval* argv); + CStr ToString(JSContext* cx, uintN argc, jsval* argv); static void ScriptingInit(); diff --git a/source/simulation/ProductionQueue.cpp b/source/simulation/ProductionQueue.cpp index 3d2edb09ee..771829ef07 100644 --- a/source/simulation/ProductionQueue.cpp +++ b/source/simulation/ProductionQueue.cpp @@ -43,7 +43,7 @@ void CProductionItem::ScriptingInit() AddProperty(L"name", &CProductionItem::m_name, true); AddProperty(L"elapsedTime", &CProductionItem::m_elapsedTime, true); AddProperty(L"totalTime", &CProductionItem::m_totalTime, true); - AddProperty(L"progress", (GetFn)&CProductionItem::JSI_GetProgress); + AddProperty(L"progress", static_cast(&CProductionItem::JSI_GetProgress)); CJSObject::ScriptingInit("ProductionItem"); } @@ -103,12 +103,12 @@ jsval CProductionQueue::JSI_GetLength( JSContext* UNUSED(cx) ) return ToJSVal( (int) m_items.size() ); } -jsval CProductionQueue::JSI_Get( JSContext* cx, uintN argc, jsval* argv ) +jsval_t CProductionQueue::JSI_Get( JSContext* cx, uintN argc, jsval* argv ) { debug_assert( argc == 1 ); debug_assert( JSVAL_IS_INT(argv[0]) ); - int index = ToPrimitive( argv[0] ); + int index = ToPrimitive( argv[0] ); if(index < 0 || index >= (int)m_items.size() ) { @@ -124,7 +124,7 @@ bool CProductionQueue::JSI_Cancel( JSContext* cx, uintN argc, jsval* argv ) debug_assert( argc == 1 ); debug_assert( JSVAL_IS_INT(argv[0]) ); - int index = ToPrimitive( argv[0] ); + int index = ToPrimitive( argv[0] ); if(index < 0 || index >= (int)m_items.size() ) { @@ -141,8 +141,8 @@ bool CProductionQueue::JSI_Cancel( JSContext* cx, uintN argc, jsval* argv ) void CProductionQueue::ScriptingInit() { - AddProperty(L"length", (GetFn)&CProductionQueue::JSI_GetLength); - AddMethod( "get", 1 ); + AddProperty(L"length", static_cast(&CProductionQueue::JSI_GetLength)); + AddMethod( "get", 1 ); AddMethod( "cancel", 1 ); CJSObject::ScriptingInit("ProductionQueue"); } diff --git a/source/simulation/ProductionQueue.h b/source/simulation/ProductionQueue.h index f7fe0a118a..bf48345454 100644 --- a/source/simulation/ProductionQueue.h +++ b/source/simulation/ProductionQueue.h @@ -41,7 +41,7 @@ public: void CancelAll(); jsval JSI_GetLength( JSContext* cx ); - jsval JSI_Get( JSContext* cx, uintN argc, jsval* argv ); + jsval_t JSI_Get( JSContext* cx, uintN argc, jsval* argv ); bool JSI_Cancel( JSContext* cx, uintN argc, jsval* argv ); static void ScriptingInit(); diff --git a/source/simulation/Technology.cpp b/source/simulation/Technology.cpp index 7e617fb104..3e21a9917e 100644 --- a/source/simulation/Technology.cpp +++ b/source/simulation/Technology.cpp @@ -419,23 +419,23 @@ void CTechnology::ScriptingInit() AddClassProperty(L"time", &CTechnology::m_ReqTime); //Techs may upgrade research time and cost of other techs AddClassProperty(L"in_progress", &CTechnology::m_inProgress); - AddMethod( "applyEffects", 2 ); - AddMethod( "isExcluded", 0 ); - AddMethod( "isValid", 0 ); - AddMethod( "isResearched", 0 ); - AddMethod( "getPlayerID", 0 ); + AddMethod( "applyEffects", 2 ); + AddMethod( "isExcluded", 0 ); + AddMethod( "isValid", 0 ); + AddMethod( "isResearched", 0 ); + AddMethod( "getPlayerID", 0 ); CJSComplex::ScriptingInit("Technology"); } -jsval CTechnology::ApplyEffects( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +bool CTechnology::ApplyEffects( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { // Unmark ourselves as in progress m_inProgress = false; if ( !IsTechValid() ) { - return JSVAL_FALSE; + return false; } // Disable any paired techs @@ -467,27 +467,27 @@ jsval CTechnology::ApplyEffects( JSContext* UNUSED(cx), uintN UNUSED(argc), jsva // Add ourselves to player's researched techs m_player->AddActiveTech( this ); - return JSVAL_TRUE; + return true; } -jsval CTechnology::IsValid( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +bool CTechnology::IsValid( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - return ToJSVal( IsTechValid() ); + return IsTechValid(); } -jsval CTechnology::IsExcluded( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +bool CTechnology::IsExcluded( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - return ToJSVal( m_excluded ); + return m_excluded; } -jsval CTechnology::IsResearched( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +bool CTechnology::IsResearched( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - return ToJSVal( IsResearched() ); + return IsResearched(); } -inline jsval CTechnology::GetPlayerID( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) +int CTechnology::GetPlayerID( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) ) { - return ToJSVal( m_player->GetPlayerID() ); + return m_player->GetPlayerID(); } diff --git a/source/simulation/Technology.h b/source/simulation/Technology.h index 680603b2f8..c22c5fcaa4 100644 --- a/source/simulation/Technology.h +++ b/source/simulation/Technology.h @@ -42,11 +42,11 @@ public: //JS functions static void ScriptingInit(); - jsval ApplyEffects( JSContext* cx, uintN argc, jsval* argv ); - jsval IsValid( JSContext* cx, uintN argc, jsval* argv ); - jsval IsResearched( JSContext* cx, uintN argc, jsval* argv ); - jsval IsExcluded( JSContext* cx, uintN argc, jsval* argv ); - inline jsval GetPlayerID( JSContext* cx, uintN argc, jsval* argv ); + bool ApplyEffects( JSContext* cx, uintN argc, jsval* argv ); + bool IsValid( JSContext* cx, uintN argc, jsval* argv ); + bool IsResearched( JSContext* cx, uintN argc, jsval* argv ); + bool IsExcluded( JSContext* cx, uintN argc, jsval* argv ); + int GetPlayerID( JSContext* cx, uintN argc, jsval* argv ); void Apply( CEntity* entity ); diff --git a/source/simulation/TriggerManager.cpp b/source/simulation/TriggerManager.cpp index f6e4212e7f..acd96678a8 100644 --- a/source/simulation/TriggerManager.cpp +++ b/source/simulation/TriggerManager.cpp @@ -83,8 +83,8 @@ void CTrigger::ScriptingInit() AddProperty(L"maxRunCount", &CTrigger::m_maxRunCount); AddProperty(L"timeDelay", &CTrigger::m_timeDelay); - AddMethod( "activate", 0 ); - AddMethod( "deactivate", 0 ); + AddMethod( "activate", 0 ); + AddMethod( "deactivate", 0 ); CJSObject::ScriptingInit("Trigger", CTrigger::Construct, 6); } @@ -103,15 +103,13 @@ bool CTrigger::Fire() return (m_runCount < m_maxRunCount); } -jsval CTrigger::Activate(JSContext* UNUSED(cx), uint UNUSED(argc), jsval* UNUSED(argv)) +void CTrigger::Activate(JSContext* UNUSED(cx), uint UNUSED(argc), jsval* UNUSED(argv)) { m_active = true; - return JS_TRUE; } -jsval CTrigger::Deactivate(JSContext* UNUSED(cx), uint UNUSED(argc), jsval* UNUSED(argv)) +void CTrigger::Deactivate(JSContext* UNUSED(cx), uint UNUSED(argc), jsval* UNUSED(argv)) { m_active = false; - return JS_TRUE; } diff --git a/source/simulation/TriggerManager.h b/source/simulation/TriggerManager.h index e67249a0c8..0e0accab8d 100644 --- a/source/simulation/TriggerManager.h +++ b/source/simulation/TriggerManager.h @@ -115,8 +115,8 @@ public: //Returns false if trigger exceeds run count bool Fire(); - jsval Activate(JSContext* cx, uint argc, jsval* argv); - jsval Deactivate(JSContext* cx, uint argc, jsval* argv); + void Activate(JSContext* cx, uint argc, jsval* argv); + void Deactivate(JSContext* cx, uint argc, jsval* argv); static JSBool Construct( JSContext* cx, JSObject* obj, uint argc, jsval* argv, jsval* rval ); static void ScriptingInit(); diff --git a/source/sound/JSI_Sound.cpp b/source/sound/JSI_Sound.cpp index 73fbd3a8b5..d268b1e61f 100644 --- a/source/sound/JSI_Sound.cpp +++ b/source/sound/JSI_Sound.cpp @@ -154,7 +154,7 @@ bool JSI_Sound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(ar void JSI_Sound::ScriptingInit() { - AddMethod("toString", 0); + AddMethod("toString", 0); AddMethod("play", 0); AddMethod("loop", 0); AddMethod("free", 0); @@ -166,10 +166,9 @@ void JSI_Sound::ScriptingInit() CJSObject::ScriptingInit("Sound", &JSI_Sound::Construct, 1); } -jsval JSI_Sound::ToString(JSContext* cx, uintN UNUSED(argc), jsval* UNUSED(argv)) +CStr JSI_Sound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) { - CStr name = "[object Sound: " + CStr(h_filename(m_Handle)) + "]"; - return STRING_TO_JSVAL(JS_NewStringCopyZ(cx, name)); + return "[object Sound: " + CStr(h_filename(m_Handle)) + "]"; } JSBool JSI_Sound::Construct(JSContext* cx, JSObject* UNUSED(obj), uint argc, jsval* argv, jsval* rval) diff --git a/source/sound/JSI_Sound.h b/source/sound/JSI_Sound.h index de07cf9773..5308341cba 100644 --- a/source/sound/JSI_Sound.h +++ b/source/sound/JSI_Sound.h @@ -31,7 +31,7 @@ public: // Script-bound functions - jsval ToString( JSContext* cx, uintN argc, jsval* argv ); + CStr ToString( JSContext* cx, uintN argc, jsval* argv ); // start playing the sound (one-shot). // it will automatically be freed when done.