Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSInterface_Vector3D.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 "JSInterface_Vector3D.h"
23 
25  "Vector3D", JSCLASS_HAS_PRIVATE,
26  JS_PropertyStub, JS_PropertyStub,
28  JS_EnumerateStub, JS_ResolveStub,
29  JS_ConvertStub, JSI_Vector3D::finalize,
30  NULL, NULL, NULL, NULL
31 };
32 
33 JSPropertySpec JSI_Vector3D::JSI_props[] =
34 {
35  { "x", JSI_Vector3D::component_x, JSPROP_ENUMERATE },
36  { "y", JSI_Vector3D::component_y, JSPROP_ENUMERATE },
37  { "z", JSI_Vector3D::component_z, JSPROP_ENUMERATE },
38  { 0 }
39 };
40 
41 JSFunctionSpec JSI_Vector3D::JSI_methods[] =
42 {
43  { "toString", JSI_Vector3D::toString, 0, 0 },
44  { 0 }
45 };
46 
48 {
49  g_ScriptingHost.DefineCustomObjectType(&JSI_class, JSI_Vector3D::construct, 0, JSI_props, JSI_methods, NULL, NULL);
50 }
51 
53 {
54  owner = NULL;
55  vector = new CVector3D();
56 }
57 
59 {
60  owner = NULL;
61  vector = new CVector3D(x, y, z);
62 }
63 
65 {
66  owner = NULL;
67  vector = new CVector3D(copy.X, copy.Y, copy.Z);
68 }
69 
71 {
72  owner = _owner;
73  updateFn = NULL;
74  freshenFn = NULL;
75  vector = attach;
76 }
77 
79 {
80  owner = _owner;
81  updateFn = _updateFn;
82  freshenFn = NULL;
83  vector = attach;
84 }
85 
87  void(IPropertyOwner::*_freshenFn)(void))
88 {
89  owner = _owner;
90  updateFn = _updateFn;
91  freshenFn = _freshenFn;
92  vector = attach;
93 }
94 
96 {
97  if (!owner)
98  delete (vector);
99 }
100 
101 JSBool JSI_Vector3D::getProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp)
102 {
103  if (!JSID_IS_INT(id))
104  return JS_TRUE;
105 
106  Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, obj, &JSI_Vector3D::JSI_class, NULL);
107  if (!vectorInfo)
108  return JS_FALSE;
109 
110  CVector3D* vectorData = vectorInfo->vector;
111 
112  if (vectorInfo->owner && vectorInfo->freshenFn)
113  ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
114 
115  switch (JSID_TO_INT(id))
116  {
117  case component_x:
118  return JS_NewNumberValue(cx, vectorData->X, vp);
119  case component_y:
120  return JS_NewNumberValue(cx, vectorData->Y, vp);
121  case component_z:
122  return JS_NewNumberValue(cx, vectorData->Z, vp);
123  }
124 
125  return JS_FALSE;
126 }
127 
128 JSBool JSI_Vector3D::setProperty(JSContext* cx, JSObject* obj, jsid id, JSBool UNUSED(strict), jsval* vp)
129 {
130  if (!JSID_IS_INT(id))
131  return JS_TRUE;
132 
133  Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, obj, &JSI_Vector3D::JSI_class, NULL);
134  if (!vectorInfo)
135  return JS_FALSE;
136 
137  CVector3D* vectorData = vectorInfo->vector;
138 
139  if (vectorInfo->owner && vectorInfo->freshenFn)
140  ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
141 
142  switch (JSID_TO_INT(id))
143  {
144  case component_x:
145  vectorData->X = ToPrimitive<float> (*vp);
146  break;
147  case component_y:
148  vectorData->Y = ToPrimitive<float> (*vp);
149  break;
150  case component_z:
151  vectorData->Z = ToPrimitive<float> (*vp);
152  break;
153  }
154 
155  if (vectorInfo->owner && vectorInfo->updateFn)
156  ((vectorInfo->owner)->*(vectorInfo->updateFn))();
157 
158  return JS_TRUE;
159 }
160 
161 JSBool JSI_Vector3D::construct(JSContext* cx, uintN argc, jsval* vp)
162 {
163  JSObject* vector = JS_NewObject(cx, &JSI_Vector3D::JSI_class, NULL, NULL);
164 
165  if (argc == 0)
166  {
167  JS_SetPrivate(cx, vector, new Vector3D_Info());
168  JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(vector));
169  return JS_TRUE;
170  }
171 
173  try
174  {
175  float x = ToPrimitive<float> (JS_ARGV(cx, vp)[0]);
176  float y = ToPrimitive<float> (JS_ARGV(cx, vp)[1]);
177  float z = ToPrimitive<float> (JS_ARGV(cx, vp)[2]);
178  JS_SetPrivate(cx, vector, new Vector3D_Info(x, y, z));
179  JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(vector));
180  return JS_TRUE;
181  }
183  {
184  // Invalid input (i.e. can't be coerced into doubles) - fail
185  JS_ReportError(cx, "Invalid parameters to Vector3D constructor");
186  return JS_FALSE;
187  }
188 }
189 
190 void JSI_Vector3D::finalize(JSContext* cx, JSObject* obj)
191 {
192  delete ((Vector3D_Info*)JS_GetPrivate(cx, obj));
193 }
194 
195 JSBool JSI_Vector3D::toString(JSContext* cx, uintN UNUSED(argc), jsval* vp)
196 {
197  char buffer[256];
198  Vector3D_Info* vectorInfo = (Vector3D_Info*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &JSI_Vector3D::JSI_class, NULL);
199  if (!vectorInfo)
200  return JS_FALSE;
201 
202  if (vectorInfo->owner && vectorInfo->freshenFn)
203  ((vectorInfo->owner)->*(vectorInfo->freshenFn))();
204 
205  CVector3D* vectorData = vectorInfo->vector;
206  sprintf_s(buffer, ARRAY_SIZE(buffer), "[object Vector3D: ( %f, %f, %f )]", vectorData->X, vectorData->Y, vectorData->Z);
207  JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer)));
208  return JS_TRUE;
209 }
JSFunctionSpec JSI_methods[]
void finalize(JSContext *cx, JSObject *obj)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
JSPropertySpec JSI_props[]
void(IPropertyOwner::* updateFn)()
JSBool setProperty(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
int sprintf_s(char *buf, size_t max_chars, const char *fmt,...) PRINTF_ARGS(3)
#define ARRAY_SIZE(name)
#define g_ScriptingHost
JSBool construct(JSContext *cx, uintN argc, jsval *vp)
float X
Definition: Vector3D.h:31
JSBool getProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
float Y
Definition: Vector3D.h:31
JSFunctionSpec JSI_methods[]
#define JSU_REQUIRE_PARAMS(exact_number)
Definition: JSUtil.h:26
void(IPropertyOwner::* freshenFn)()
JSBool toString(JSContext *cx, uintN argc, jsval *vp)
float Z
Definition: Vector3D.h:31
JSPropertySpec JSI_props[]
bool ToPrimitive< float >(JSContext *cx, jsval v, float &Storage)