Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSConversions.h
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 // A general system of converting between native objects and their JavaScript representations
19 
20 #ifndef INCLUDED_JSCONVERSIONS
21 #define INCLUDED_JSCONVERSIONS
22 
24 
25 class CStrW;
26 class CScriptObject;
27 class CObjectEntry;
28 class CVector3D;
29 
30 // -----
31 //
32 // Defaults
33 //
34 // -----
35 
36 template<typename T> T* ToNative( JSContext* cx, JSObject* obj )
37 {
38  return( (T*)JS_GetInstancePrivate( cx, obj, &T::JSI_class, NULL ) );
39 }
40 
41 template<typename T> JSObject* ToScript( T* Native )
42 {
43  if( !Native )
44  return( (JSObject*)NULL );
45  return( Native->GetScript() );
46 }
47 
48 template<typename T> T* ToNative( jsval v )
49 {
50  if( !JSVAL_IS_OBJECT( v ) ) return( NULL );
51  if( v == JSVAL_NULL ) return( NULL );
52  return( ToNative<T>( g_ScriptingHost.GetContext(), JSVAL_TO_OBJECT( v ) ) );
53 }
54 
55 template<typename T> bool ToPrimitive( JSContext* UNUSED(cx), jsval v, T& Storage )
56 {
57  T* Native = ToNative<T>( v );
58  if( !Native ) return( false );
59  Storage = *Native;
60  return( true );
61 }
62 
63 // Handle pointer-to-objects sensibly (by automatically dereferencing them one level)
64 template<typename T> bool ToPrimitive( JSContext* UNUSED(cx), jsval v, T*& Storage )
65 {
66  T* Native = ToNative<T>( v );
67  if( !Native ) return( false );
68  Storage = Native;
69  return( true );
70 }
71 
72 // Throws PSERROR_Scripting_ConversionFailed on failure.
73 template<typename T> inline T ToPrimitive( JSContext* cx, jsval v )
74 {
75  T Temp;
76  bool ok = ToPrimitive( cx, v, Temp );
77  if( !ok ) throw PSERROR_Scripting_ConversionFailed();
78  return( Temp );
79 }
80 
81 // Throws PSERROR_Scripting_ConversionFailed on failure.
82 template<typename T> inline T ToPrimitive( jsval v )
83 {
84  return( ToPrimitive<T>( g_ScriptingHost.GetContext(), v ) );
85 }
86 
87 template<typename T> jsval ToJSVal( T& Native )
88 {
89  return( OBJECT_TO_JSVAL( ToScript<T>( &Native ) ) );
90 }
91 
92 template<typename T> jsval ToJSVal( T*& Native )
93 {
94  return( OBJECT_TO_JSVAL( ToScript<T>( Native ) ) );
95 }
96 
97 template<typename T> jsval ToJSVal( const T& Native );
98 
99 // -----
100 //
101 // Overrides
102 //
103 // -----
104 
105 // CVector3D
106 template<> CVector3D* ToNative<CVector3D>( JSContext* cx, JSObject* obj );
107 template<> JSObject* ToScript<CVector3D>( CVector3D* Native );
108 template<> jsval ToJSVal<CVector3D>( const CVector3D& Native );
109 
110 // CObjectEntry
111 template<> bool ToPrimitive<CObjectEntry>( JSContext* cx, jsval v, CObjectEntry*& Storage );
112 template<> jsval ToJSVal<CObjectEntry>( CObjectEntry*& Native );
113 
114 // CScriptObject
115 template<> bool ToPrimitive<CScriptObject>( JSContext* cx, jsval v, CScriptObject& Storage );
116 template<> jsval ToJSVal<CScriptObject>( CScriptObject& Native );
117 
118 // int
119 template<> bool ToPrimitive<int>( JSContext* cx, jsval v, int& Storage );
120 template<> jsval ToJSVal<int>( const int& Native );
121 template<> jsval ToJSVal<int>( int& Native );
122 
123 // unsigned
124 template<> bool ToPrimitive<unsigned>( JSContext* cx, jsval v, unsigned& Storage );
125 template<> jsval ToJSVal<unsigned>( const unsigned& Native );
126 template<> jsval ToJSVal<unsigned>( unsigned& Native );
127 
128 // long int
129 template<> bool ToPrimitive<long>( JSContext* cx, jsval v, long& Storage );
130 template<> jsval ToJSVal<long>( const long& Native );
131 template<> jsval ToJSVal<long>( long& Native );
132 
133 // unsigned long int
134 template<> bool ToPrimitive<unsigned long>( JSContext* cx, jsval v, unsigned long& Storage );
135 template<> jsval ToJSVal<unsigned long>( const unsigned long& Native );
136 template<> jsval ToJSVal<unsigned long>( unsigned long& Native );
137 
138 // (s)size_t are considered to be identical to (unsigned) int by GCC and
139 // their specializations would cause conflicts there. On x86_64 GCC, s/size_t
140 // is equivalent to (unsigned) long, but the same solution applies; use the
141 // long and unsigned long specializations instead of s/size_t.
142 #if !GCC_VERSION
143 
144 // for some reason, x64 MSC treats size_t as distinct from unsigned long:
145 #if ARCH_AMD64
146 
147 // size_t
148 template<> bool ToPrimitive<size_t>( JSContext* cx, jsval v, size_t& Storage );
149 template<> jsval ToJSVal<size_t>( const size_t& Native );
150 template<> jsval ToJSVal<size_t>( size_t& Native );
151 
152 // ssize_t
153 template<> bool ToPrimitive<ssize_t>( JSContext* cx, jsval v, ssize_t& Storage );
154 template<> jsval ToJSVal<ssize_t>( const ssize_t& Native );
155 template<> jsval ToJSVal<ssize_t>( ssize_t& Native );
156 
157 #endif
158 
159 #endif
160 
161 // double
162 template<> bool ToPrimitive<double>( JSContext* cx, jsval v, double& Storage );
163 template<> jsval ToJSVal<double>( const double& Native );
164 template<> jsval ToJSVal<double>( double& Native );
165 
166 // float
167 template<> bool ToPrimitive<float>( JSContext* cx, jsval v, float& Storage );
168 template<> jsval ToJSVal<float>( const float& Native );
169 template<> jsval ToJSVal<float>( float& Native );
170 
171 // bool
172 template<> bool ToPrimitive<bool>( JSContext* cx, jsval v, bool& Storage );
173 template<> jsval ToJSVal<bool>( const bool& Native );
174 template<> jsval ToJSVal<bool>( bool& Native );
175 
176 /*
177 // char*
178 template<> bool ToPrimitive<char*>( JSContext* cx, jsval v, char*& Storage );
179 template<> jsval ToJSVal<char*>( const char* Native );
180 template<> jsval ToJSVal<char*>( char* Native );
181 */
182 
183 // CStrW
184 template<> bool ToPrimitive<CStrW>( JSContext* cx, jsval v, CStrW& Storage );
185 template<> jsval ToJSVal<CStrW>( const CStrW& Native );
186 template<> jsval ToJSVal<CStrW>( CStrW& Native );
187 
188 // CStr(8)
189 template<> bool ToPrimitive<CStr8>( JSContext* cx, jsval v, CStr8& Storage );
190 template<> jsval ToJSVal<CStr8>( const CStr8& Native );
191 template<> jsval ToJSVal<CStr8>( CStr8& Native );
192 
193 #endif
bool ToPrimitive< long >(JSContext *cx, jsval v, long &Storage)
jsval ToJSVal< float >(const float &Native)
bool ToPrimitive< CStrW >(JSContext *cx, jsval v, CStrW &Storage)
jsval ToJSVal< double >(const double &Native)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
jsval ToJSVal< CObjectEntry >(CObjectEntry *&Native)
bool ToPrimitive< CStr8 >(JSContext *cx, jsval v, CStr8 &Storage)
jsval ToJSVal< CScriptObject >(CScriptObject &Native)
bool ToPrimitive< unsigned >(JSContext *cx, jsval v, unsigned &Storage)
jsval ToJSVal< CVector3D >(const CVector3D &Native)
JSObject * ToScript< CVector3D >(CVector3D *Native)
bool ToPrimitive< bool >(JSContext *cx, jsval v, bool &Storage)
bool ToPrimitive< unsigned long >(JSContext *cx, jsval v, unsigned long &Storage)
jsval ToJSVal< long >(const long &Native)
JSObject * ToScript(T *Native)
Definition: JSConversions.h:41
bool ToPrimitive< CObjectEntry >(JSContext *cx, jsval v, CObjectEntry *&Storage)
#define g_ScriptingHost
bool ToPrimitive< double >(JSContext *cx, jsval v, double &Storage)
jsval ToJSVal< unsigned long >(const unsigned long &Native)
jsval ToJSVal< int >(const int &Native)
jsval ToJSVal< CStrW >(const CStrW &Native)
bool ToPrimitive(JSContext *cx, jsval v, T &Storage)
Definition: JSConversions.h:55
CVector3D * ToNative< CVector3D >(JSContext *cx, JSObject *obj)
#define T(string_literal)
Definition: secure_crt.cpp:70
intptr_t ssize_t
Definition: wposix_types.h:82
jsval ToJSVal< CStr8 >(const CStr8 &Native)
jsval ToJSVal(T &Native)
Definition: JSConversions.h:87
T * ToNative(JSContext *cx, JSObject *obj)
Definition: JSConversions.h:36
bool ToPrimitive< int >(JSContext *cx, jsval v, int &Storage)
jsval ToJSVal< bool >(const bool &Native)
bool ToPrimitive< float >(JSContext *cx, jsval v, float &Storage)
jsval ToJSVal< unsigned >(const unsigned &Native)
bool ToPrimitive< CScriptObject >(JSContext *cx, jsval v, CScriptObject &Storage)