// A general system of converting between native objects and their JavaScript representations // Mark Thompson (mark@wildfiregames.com / mot20@cam.ac.uk) #ifndef JSCONVERSIONS_INCLUDED #define JSCONVERSIONS_INCLUDED #include "scripting/ScriptingHost.h" class CEntity; class HEntity; class CBaseEntity; class CStrW; class CScriptObject; class CObjectEntry; class CVector3D; // ----- // // Defaults // // ----- template T* ToNative( JSContext* cx, JSObject* obj ) { return( (T*)JS_GetInstancePrivate( cx, obj, &T::JSI_class, NULL ) ); } template JSObject* ToScript( T* Native ) { if( !Native ) return( JSVAL_NULL ); return( Native->GetScript() ); } template T* ToNative( jsval v ) { if( !JSVAL_IS_OBJECT( v ) ) return( NULL ); return( ToNative( g_ScriptingHost.GetContext(), JSVAL_TO_OBJECT( v ) ) ); } template bool ToPrimitive( JSContext* cx, jsval v, T& Storage ) { T* Native = ToNative( v ); if( !Native ) return( false ); Storage = *Native; return( true ); } template inline T ToPrimitive( JSContext* cx, jsval v ) { T Temp; ToPrimitive( cx, v, Temp ); return( Temp ); } template inline T ToPrimitive( jsval v ) { return( ToPrimitive( g_ScriptingHost.GetContext(), v ) ); } template jsval ToJSVal( T& Native ) { return( OBJECT_TO_JSVAL( ToScript( &Native ) ) ); } template jsval ToJSVal( const T& Native ); // ----- // // Overrides // // ----- // CVector3D template<> CVector3D* ToNative( JSContext* cx, JSObject* obj ); template<> JSObject* ToScript( CVector3D* Native ); // CBaseEntity template<> bool ToPrimitive( JSContext* cx, jsval v, CBaseEntity*& Storage ); template<> JSObject* ToScript( CBaseEntity** Native ); // CObjectEntry template<> bool ToPrimitive( JSContext* cx, jsval v, CObjectEntry*& Storage ); template<> jsval ToJSVal( CObjectEntry*& Native ); // HEntity template<> HEntity* ToNative( JSContext* cx, JSObject* obj ); template<> JSObject* ToScript( HEntity* Native ); // CScriptObject template<> bool ToPrimitive( JSContext* cx, jsval v, CScriptObject& Storage ); template<> jsval ToJSVal( CScriptObject& Native ); // i32 template<> bool ToPrimitive( JSContext* cx, jsval v, i32& Storage ); template<> jsval ToJSVal( const i32& Native ); template<> jsval ToJSVal( i32& Native ); // double template<> bool ToPrimitive( JSContext* cx, jsval v, double& Storage ); template<> jsval ToJSVal( const double& Native ); template<> jsval ToJSVal( double& Native ); // float template<> bool ToPrimitive( JSContext* cx, jsval v, float& Storage ); template<> jsval ToJSVal( const float& Native ); template<> jsval ToJSVal( float& Native ); // bool template<> bool ToPrimitive( JSContext* cx, jsval v, bool& Storage ); template<> jsval ToJSVal( const bool& Native ); template<> jsval ToJSVal( bool& Native ); // CStrW template<> bool ToPrimitive( JSContext* cx, jsval v, CStrW& Storage ); template<> jsval ToJSVal( const CStrW& Native ); template<> jsval ToJSVal( CStrW& Native ); // jsval template<> jsval ToJSVal( const jsval& Native ); #endif