axmol/scripting/javascript/bindings/cocosjs_manual_conversions.cpp

133 lines
3.2 KiB
C++

#include "jsapi.h"
#include "jsfriendapi.h"
#include "ScriptingCore.h"
#include "js_bindings_config.h"
#include "cocosjs_manual_conversions.h"
#define JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
using namespace cocos2d;
JSBool jsval_to_CCPoint( JSContext *cx, jsval vp, Point *ret )
{
#ifdef JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
JSObject *jsobj;
if( ! JS_ValueToObject( cx, vp, &jsobj ) )
return JS_FALSE;
JSB_PRECONDITION( jsobj, "Not a valid JS object");
jsval valx, valy;
JSBool ok = JS_TRUE;
ok &= JS_GetProperty(cx, jsobj, "x", &valx);
ok &= JS_GetProperty(cx, jsobj, "y", &valy);
if( ! ok )
return JS_FALSE;
double x, y;
ok &= JS_ValueToNumber(cx, valx, &x);
ok &= JS_ValueToNumber(cx, valy, &y);
if( ! ok )
return JS_FALSE;
ret->x = x;
ret->y = y;
return JS_TRUE;
#else // #! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
JSObject *tmp_arg;
if( ! JS_ValueToObject( cx, vp, &tmp_arg ) )
return JS_FALSE;
JSB_PRECONDITION( tmp_arg && JS_IsTypedArrayObject( tmp_arg, cx ), "Not a TypedArray object");
JSB_PRECONDITION( JS_GetTypedArrayByteLength( tmp_arg, cx ) == sizeof(cpVect), "Invalid length");
*ret = *(Point*)JS_GetArrayBufferViewData( tmp_arg, cx );
return JS_TRUE;
#endif // #! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
}
JSBool jsval_to_CGPoint( JSContext *cx, jsval vp, cpVect *ret )
{
#ifdef JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
JSObject *jsobj;
if( ! JS_ValueToObject( cx, vp, &jsobj ) )
return JS_FALSE;
JSB_PRECONDITION( jsobj, "Not a valid JS object");
jsval valx, valy;
JSBool ok = JS_TRUE;
ok &= JS_GetProperty(cx, jsobj, "x", &valx);
ok &= JS_GetProperty(cx, jsobj, "y", &valy);
if( ! ok )
return JS_FALSE;
double x, y;
ok &= JS_ValueToNumber(cx, valx, &x);
ok &= JS_ValueToNumber(cx, valy, &y);
if( ! ok )
return JS_FALSE;
ret->x = x;
ret->y = y;
return JS_TRUE;
#else // #! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
JSObject *tmp_arg;
if( ! JS_ValueToObject( cx, vp, &tmp_arg ) )
return JS_FALSE;
JSB_PRECONDITION( tmp_arg && JS_IsTypedArrayObject( tmp_arg, cx ), "Not a TypedArray object");
JSB_PRECONDITION( JS_GetTypedArrayByteLength( tmp_arg, cx ) == sizeof(cpVect), "Invalid length");
*ret = *(cpVect*)JS_GetArrayBufferViewData( tmp_arg, cx );
return JS_TRUE;
#endif // #! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
}
jsval CGPoint_to_jsval( JSContext *cx, cpVect p)
{
#ifdef JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
JSObject *object = JS_NewObject(cx, NULL, NULL, NULL );
if (!object)
return JSVAL_VOID;
if (!JS_DefineProperty(cx, object, "x", DOUBLE_TO_JSVAL(p.x), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) ||
!JS_DefineProperty(cx, object, "y", DOUBLE_TO_JSVAL(p.y), NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT) )
return JSVAL_VOID;
return OBJECT_TO_JSVAL(object);
#else // JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
#ifdef __LP64__
JSObject *typedArray = JS_NewFloat64Array( cx, 2 );
#else
JSObject *typedArray = JS_NewFloat32Array( cx, 2 );
#endif
cpVect *buffer = (cpVect*)JS_GetArrayBufferViewData(typedArray, cx );
*buffer = p;
return OBJECT_TO_JSVAL(typedArray);
#endif // ! JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
}