18 #include "precompiled.h"
33 #define FAIL(msg) STMT(JS_ReportError(cx, msg); return false)
35 template<> jsval ScriptInterface::ToJSVal<IComponent*>(JSContext* cx,
IComponent*
const& val)
41 jsval instance = val->GetJSInstance();
42 if (!JSVAL_IS_NULL(instance))
47 JSClass* cls = val->GetJSClass();
51 LOGERROR(L
"IComponent does not have a scriptable interface");
55 JSObject* obj = JS_NewObject(cx, cls, NULL, NULL);
58 LOGERROR(L
"Failed to construct IComponent script object");
61 JS_SetPrivate(cx, obj, static_cast<void*>(val));
63 return OBJECT_TO_JSVAL(obj);
66 template<> jsval ScriptInterface::ToJSVal<CParamNode>(JSContext* cx,
CParamNode const& val)
68 jsval rval = val.ToJSVal(cx,
true);
72 if (JSVAL_IS_OBJECT(rval))
73 JS_DeepFreezeObject(cx, JSVAL_TO_OBJECT(rval));
78 template<> jsval ScriptInterface::ToJSVal<const CParamNode*>(JSContext* cx,
const CParamNode*
const& val)
86 template<>
bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, jsval v,
CColor&
out)
88 if (!JSVAL_IS_OBJECT(v))
89 FAIL(
"jsval not an object");
91 JSObject* obj = JSVAL_TO_OBJECT(v);
94 if (!JS_GetProperty(cx, obj,
"r", &r) || !FromJSVal(cx, r,
out.r))
95 FAIL(
"Failed to get property CColor.r");
96 if (!JS_GetProperty(cx, obj,
"g", &g) || !FromJSVal(cx, g,
out.g))
97 FAIL(
"Failed to get property CColor.g");
98 if (!JS_GetProperty(cx, obj,
"b", &b) || !FromJSVal(cx, b,
out.b))
99 FAIL(
"Failed to get property CColor.b");
100 if (!JS_GetProperty(cx, obj,
"a", &a) || !FromJSVal(cx, a,
out.a))
101 FAIL(
"Failed to get property CColor.a");
107 template<> jsval ScriptInterface::ToJSVal<CColor>(JSContext* cx,
CColor const& val)
109 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
118 JS_SetProperty(cx, obj,
"r", &r);
119 JS_SetProperty(cx, obj,
"g", &g);
120 JS_SetProperty(cx, obj,
"b", &b);
121 JS_SetProperty(cx, obj,
"a", &a);
123 return OBJECT_TO_JSVAL(obj);
126 template<>
bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, jsval v,
fixed&
out)
129 if (!JS_ValueToNumber(cx, v, &ret))
137 template<> jsval ScriptInterface::ToJSVal<fixed>(JSContext* cx,
const fixed& val)
139 jsval rval = JSVAL_VOID;
140 JS_NewNumberValue(cx, val.ToDouble(), &rval);
144 template<>
bool ScriptInterface::FromJSVal<CFixedVector3D>(JSContext* cx, jsval v,
CFixedVector3D&
out)
146 if (!JSVAL_IS_OBJECT(v))
148 JSObject* obj = JSVAL_TO_OBJECT(v);
152 if (!JS_GetProperty(cx, obj,
"x", &p))
return false;
153 if (!FromJSVal(cx, p,
out.X))
return false;
155 if (!JS_GetProperty(cx, obj,
"y", &p))
return false;
156 if (!FromJSVal(cx, p,
out.Y))
return false;
158 if (!JS_GetProperty(cx, obj,
"z", &p))
return false;
159 if (!FromJSVal(cx, p,
out.Z))
return false;
164 template<> jsval ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx,
const CFixedVector3D& val)
166 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
174 JS_SetProperty(cx, obj,
"x", &x);
175 JS_SetProperty(cx, obj,
"y", &y);
176 JS_SetProperty(cx, obj,
"z", &z);
178 return OBJECT_TO_JSVAL(obj);
181 template<>
bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, jsval v,
CFixedVector2D&
out)
183 if (!JSVAL_IS_OBJECT(v))
185 JSObject* obj = JSVAL_TO_OBJECT(v);
189 if (!JS_GetProperty(cx, obj,
"x", &p))
return false;
190 if (!FromJSVal(cx, p,
out.X))
return false;
192 if (!JS_GetProperty(cx, obj,
"y", &p))
return false;
193 if (!FromJSVal(cx, p,
out.Y))
return false;
198 template<> jsval ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx,
const CFixedVector2D& val)
200 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
207 JS_SetProperty(cx, obj,
"x", &x);
208 JS_SetProperty(cx, obj,
"y", &y);
210 return OBJECT_TO_JSVAL(obj);
215 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
219 jsuint len = val.
m_W * val.
m_H;
220 JSObject *darray = js_CreateTypedArray(cx, atype, len);
224 js::TypedArray *tdest = js::TypedArray::fromJSObject(darray);
225 ENSURE(tdest->byteLength == len*
sizeof(
T));
227 memcpy(tdest->data, val.
m_Data, tdest->byteLength);
231 jsval data = OBJECT_TO_JSVAL(darray);
233 JS_SetProperty(cx, obj,
"width", &w);
234 JS_SetProperty(cx, obj,
"height", &h);
235 JS_SetProperty(cx, obj,
"data", &data);
237 return OBJECT_TO_JSVAL(obj);
240 template<> jsval ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx,
const Grid<u8>& val)
242 return ToJSVal_Grid<js::TypedArray::TYPE_UINT8>(cx, val);
245 template<> jsval ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx,
const Grid<u16>& val)
247 return ToJSVal_Grid<js::TypedArray::TYPE_UINT16>(cx, val);
An entity initialisation parameter node.
A simple fixed-point number class.
jsval ToJSVal_Grid(JSContext *cx, const Grid< T > &val)
static void out(const wchar_t *fmt,...)
Basic 2D array, intended for storing tile data, plus support for lazy updates by ICmpObstructionManag...
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
static jsval ToJSVal(JSContext *cx, T const &val)
Convert a C++ type to a jsval.
#define T(string_literal)
static CFixed FromDouble(double n)