Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSConversions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 #include "precompiled.h"
19 
20 #include "JSConversions.h"
21 #include "graphics/ObjectManager.h"
23 #include "lib/sysdep/sysdep.h" // isfinite
25 #include <math.h>
26 #include <cfloat>
27 
28 // CVector3D
29 
30 template<> CVector3D* ToNative<CVector3D>( JSContext* cx, JSObject* obj )
31 {
32  JSI_Vector3D::Vector3D_Info* v = (JSI_Vector3D::Vector3D_Info*)JS_GetInstancePrivate( cx, obj, &JSI_Vector3D::JSI_class, NULL );
33  return( v ? v->vector : NULL );
34 }
35 
36 template<> JSObject* ToScript<CVector3D>( CVector3D* Native )
37 {
38  JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
39  JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( *Native ) );
40  return( Script );
41 }
42 
43 template<> jsval ToJSVal<CVector3D>( const CVector3D& Native )
44 {
45  JSObject* Script = JS_NewObject( g_ScriptingHost.GetContext(), &JSI_Vector3D::JSI_class, NULL, NULL );
46  JS_SetPrivate( g_ScriptingHost.GetContext(), Script, new JSI_Vector3D::Vector3D_Info( Native ) );
47  return( OBJECT_TO_JSVAL( Script ) );
48 }
49 
50 // int
51 
52 template<> jsval ToJSVal<int>( const int& Native )
53 {
54  return( INT_TO_JSVAL( Native ) );
55 }
56 
57 template<> jsval ToJSVal<int>( int& Native )
58 {
59  return( INT_TO_JSVAL( Native ) );
60 }
61 
62 template<> bool ToPrimitive<int>( JSContext* cx, jsval v, int& Storage )
63 {
64  JSBool ok = JS_ValueToInt32(cx, v, (int32*)&Storage);
65  return ok == JS_TRUE;
66 }
67 
68 // unsigned
69 
70 template<> jsval ToJSVal<unsigned>( const unsigned& Native )
71 {
72  return( INT_TO_JSVAL( Native ) );
73 }
74 
75 template<> jsval ToJSVal<unsigned>( unsigned& Native )
76 {
77  return( INT_TO_JSVAL( Native ) );
78 }
79 
80 template<> bool ToPrimitive<unsigned>( JSContext* cx, jsval v, unsigned& Storage )
81 {
82  int temp;
83  if(!ToPrimitive<int>(cx, v, temp))
84  return false;
85  if(temp < 0)
86  return false;
87  Storage = (unsigned)temp;
88  return true;
89 }
90 
91 // long
92 template<> jsval ToJSVal<long>( const long& Native )
93 {
94  return( INT_TO_JSVAL( (int)Native ) );
95 }
96 
97 template<> jsval ToJSVal<long>( long& Native )
98 {
99  return( INT_TO_JSVAL( (int)Native ) );
100 }
101 
102 template<> bool ToPrimitive<long>( JSContext* cx, jsval v, long& Storage )
103 {
104  int32 tmp;
105  JSBool ok = JS_ValueToInt32(cx, v, &tmp);
106  Storage = (long)tmp;
107  return ok == JS_TRUE;
108 }
109 
110 // unsigned long
111 template<> jsval ToJSVal<unsigned long>( const unsigned long& Native )
112 {
113  return( INT_TO_JSVAL( (int)Native ) );
114 }
115 
116 template<> jsval ToJSVal<unsigned long>( unsigned long& Native )
117 {
118  return( INT_TO_JSVAL( (int)Native ) );
119 }
120 
121 template<> bool ToPrimitive<unsigned long>( JSContext* cx, jsval v, unsigned long& Storage )
122 {
123  int32 tmp;
124  JSBool ok = JS_ValueToInt32(cx, v, &tmp);
125  Storage = (unsigned long)tmp;
126  return ok == JS_TRUE;
127 }
128 
129 // see comment at declaration of specialization
130 #if !GCC_VERSION
131 #if ARCH_AMD64
132 
133 template<> jsval ToJSVal<size_t>( const size_t& Native )
134 {
135  return( INT_TO_JSVAL( (int)Native ) );
136 }
137 
138 template<> jsval ToJSVal<size_t>( size_t& Native )
139 {
140  return( INT_TO_JSVAL( (int)Native ) );
141 }
142 
143 template<> bool ToPrimitive<size_t>( JSContext* cx, jsval v, size_t& Storage )
144 {
145  int temp;
146  if(!ToPrimitive<int>(cx, v, temp))
147  return false;
148  if(temp < 0)
149  return false;
150  Storage = (size_t)temp;
151  return true;
152 }
153 
154 
155 template<> jsval ToJSVal<ssize_t>( const ssize_t& Native )
156 {
157  return( INT_TO_JSVAL( (int)Native ) );
158 }
159 
160 template<> jsval ToJSVal<ssize_t>( ssize_t& Native )
161 {
162  return( INT_TO_JSVAL( (int)Native ) );
163 }
164 
165 template<> bool ToPrimitive<ssize_t>( JSContext* cx, jsval v, ssize_t& Storage )
166 {
167  int temp;
168  if(!ToPrimitive<int>(cx, v, temp))
169  return false;
170  if(temp < 0)
171  return false;
172  Storage = (ssize_t)temp;
173  return true;
174 }
175 
176 #endif // #if ARCH_AMD64
177 #endif // #if !GCC_VERSION
178 
179 // double
180 
181 template<> jsval ToJSVal<double>( const double& Native )
182 {
183  jsval val = JSVAL_VOID;
184  JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
185  return val;
186 }
187 
188 template<> jsval ToJSVal<double>( double& Native )
189 {
190  jsval val = JSVAL_VOID;
191  JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
192  return val;
193 }
194 
195 template<> bool ToPrimitive<double>( JSContext* cx, jsval v, double& Storage )
196 {
197  JSBool ok = JS_ValueToNumber(cx, v, &Storage);
198  if (ok == JS_FALSE || !isfinite( Storage ) )
199  return false;
200  return true;
201 }
202 
203 // float
204 
205 template<> jsval ToJSVal<float>( const float& Native )
206 {
207  jsval val = JSVAL_VOID;
208  JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
209  return val;
210 }
211 
212 template<> jsval ToJSVal<float>( float& Native )
213 {
214  jsval val = JSVAL_VOID;
215  JS_NewNumberValue( g_ScriptingHost.getContext(), Native, &val );
216  return val;
217 }
218 
219 template<> bool ToPrimitive<float>( JSContext* cx, jsval v, float& Storage )
220 {
221  double temp;
222  if(!ToPrimitive<double>(cx, v, temp))
223  return false;
224  Storage = (float)temp;
225  return true;
226 }
227 
228 
229 // bool
230 
231 template<> jsval ToJSVal<bool>( const bool& Native )
232 {
233  return( BOOLEAN_TO_JSVAL( Native ) );
234 }
235 
236 template<> jsval ToJSVal<bool>( bool& Native )
237 {
238  return( BOOLEAN_TO_JSVAL( Native ) );
239 }
240 
241 template<> bool ToPrimitive<bool>( JSContext* cx, jsval v, bool& Storage )
242 {
243  JSBool temp;
244  JSBool ok = JS_ValueToBoolean(cx, v, &temp);
245  if(ok == JS_FALSE)
246  return false;
247  Storage = (temp == JS_TRUE);
248  return true;
249 }
250 
251 // CStrW
252 template<> bool ToPrimitive<CStrW>( JSContext* UNUSED(cx), jsval v, CStrW& Storage )
253 {
254  try
255  {
256  Storage = g_ScriptingHost.ValueToUCString( v );
257  }
259  {
260  return( false );
261  }
262  return( true );
263 }
264 
265 template<> jsval ToJSVal<CStrW>( const CStrW& Native )
266 {
267  return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), reinterpret_cast<const jschar*>(Native.utf16().c_str()) ) ) );
268 }
269 
270 template<> jsval ToJSVal<CStrW>( CStrW& Native )
271 {
272  return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( g_ScriptingHost.GetContext(), reinterpret_cast<const jschar*>(Native.utf16().c_str()) ) ) );
273 }
274 
275 // CStr/CStr8
276 
277 template<> bool ToPrimitive<CStr8>( JSContext* cx, jsval v, CStr8& Storage )
278 {
279  std::string str;
280  if (!ScriptInterface::FromJSVal(cx, v, str))
281  return false;
282  Storage = str;
283  return true;
284 }
285 
286 template<> jsval ToJSVal<CStr8>( const CStr8& Native )
287 {
288  return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
289 }
290 
291 template<> jsval ToJSVal<CStr8>( CStr8& Native )
292 {
293  return( STRING_TO_JSVAL( JS_NewStringCopyZ( g_ScriptingHost.GetContext(), Native.c_str() ) ) );
294 }
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. ...
bool ToPrimitive< CStr8 >(JSContext *cx, jsval v, CStr8 &Storage)
bool ToPrimitive< unsigned >(JSContext *cx, jsval v, unsigned &Storage)
#define isfinite
Definition: posix.h:127
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)
static bool FromJSVal(JSContext *cx, jsval val, T &ret)
Convert a jsval to a C++ type.
#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)
CVector3D * ToNative< CVector3D >(JSContext *cx, JSObject *obj)
intptr_t ssize_t
Definition: wposix_types.h:82
jsval ToJSVal< CStr8 >(const CStr8 &Native)
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)