Merge pull request #1355 from folecr/refactor_scripting_core

Refactor scripting core
This commit is contained in:
minggo 2012-09-23 18:45:03 -07:00
commit 2c9399cdea
12 changed files with 201 additions and 182 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "tools/cxx-generator"]
path = tools/cxx-generator
url = git://github.com/funkaster/cxx-generator.git
[submodule "scripting/javascript/bindings/generated"]
path = scripting/javascript/bindings/generated
url = git://github.com/folecr/cocos2dx-autogen-bindings.git

View File

@ -9,8 +9,8 @@ LOCAL_MODULE_FILENAME := libscriptingcore-spidermonkey
LOCAL_SRC_FILES := ScriptingCore.cpp \
cocos2d_specifics.cpp \
CCPhysicsSprite.cpp \
cocos2dx.cpp \
js_manual_conversions.cpp \
cocosjs_manual_conversions.cpp \
js_bindings_chipmunk_manual.cpp \
js_bindings_chipmunk_functions.cpp \
generated/cocos2dx.cpp

View File

@ -21,7 +21,7 @@
#include "CCPhysicsSprite.h"
#include "ScriptingCore.h"
#include "js_manual_conversions.h"
#include "cocosjs_manual_conversions.h"
using namespace cocos2d;

View File

@ -0,0 +1,160 @@
#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, CCPoint *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 = *(CCPoint*)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
}
JSBool jsval_to_cpBB( JSContext *cx, jsval vp, cpBB *ret )
{
JSObject *tmp_arg;
if( ! JS_ValueToObject( cx, vp, &tmp_arg ) )
return JS_FALSE;
JSB_PRECONDITION( JS_IsTypedArrayObject( tmp_arg, cx ), "Not a TypedArray object");
JSB_PRECONDITION( JS_GetTypedArrayByteLength( tmp_arg, cx ) == sizeof(cpFloat)*4, "Invalid length");
*ret = *(cpBB*)JS_GetArrayBufferViewData( tmp_arg, cx);
return JS_TRUE;
}
jsval cpBB_to_jsval(JSContext *cx, cpBB bb )
{
#ifdef __LP64__
JSObject *typedArray = JS_NewFloat64Array( cx, 4 );
#else
JSObject *typedArray = JS_NewFloat32Array( cx, 4 );
#endif
cpBB *buffer = (cpBB*)JS_GetArrayBufferViewData(typedArray, cx);
*buffer = bb;
return OBJECT_TO_JSVAL(typedArray);
}

View File

@ -0,0 +1,19 @@
#include "chipmunk.h"
#include "cocos2d.h"
#include "js_manual_conversions.h"
#ifdef __cplusplus
extern "C" {
#endif
JSBool jsval_to_CGPoint( JSContext *cx, jsval vp, cpVect *out );
jsval CGPoint_to_jsval( JSContext *cx, cpVect p );
JSBool jsval_to_cpBB( JSContext *cx, jsval vp, cpBB *ret );
jsval cpBB_to_jsval(JSContext *cx, cpBB bb );
#ifdef __cplusplus
}
#endif
#define cpVect_to_jsval CGPoint_to_jsval
#define jsval_to_cpVect jsval_to_CGPoint

@ -0,0 +1 @@
Subproject commit 2d18305596b4da4b260bb2fd6d5ed8e878d0b129

View File

@ -1 +1 @@
02ec34ee0597673f01d3cc72224a3f0f52d8c06b
c40aaf99dc1fe3345c11bc4952c69810483ea5d7

View File

@ -6,7 +6,7 @@
#include "js_bindings_chipmunk_manual.hpp"
#include "jsapi.h"
#include "js_manual_conversions.h"
#include "cocosjs_manual_conversions.h"
#include "uthash.h"
#include "ccConfig.h"
#include "ScriptingCore.h"

View File

@ -9,135 +9,6 @@
#include "js_bindings_config.h"
#include "js_manual_conversions.h"
#define JSB_COMPATIBLE_WITH_COCOS2D_HTML5_BASIC_TYPES
//#include "js_bindings_NS_manual.h"
JSBool jsval_to_CCPoint( JSContext *cx, jsval vp, CCPoint *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 = *(CCPoint*)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
}
JSBool jsval_to_opaque( JSContext *cx, jsval vp, void **r)
{
#ifdef __LP64__
@ -261,34 +132,6 @@ jsval long_to_jsval( JSContext *cx, long number )
#endif
}
JSBool jsval_to_cpBB( JSContext *cx, jsval vp, cpBB *ret )
{
JSObject *tmp_arg;
if( ! JS_ValueToObject( cx, vp, &tmp_arg ) )
return JS_FALSE;
JSB_PRECONDITION( JS_IsTypedArrayObject( tmp_arg, cx ), "Not a TypedArray object");
JSB_PRECONDITION( JS_GetTypedArrayByteLength( tmp_arg, cx ) == sizeof(cpFloat)*4, "Invalid length");
*ret = *(cpBB*)JS_GetArrayBufferViewData( tmp_arg, cx);
return JS_TRUE;
}
jsval cpBB_to_jsval(JSContext *cx, cpBB bb )
{
#ifdef __LP64__
JSObject *typedArray = JS_NewFloat64Array( cx, 4 );
#else
JSObject *typedArray = JS_NewFloat32Array( cx, 4 );
#endif
cpBB *buffer = (cpBB*)JS_GetArrayBufferViewData(typedArray, cx);
*buffer = bb;
return OBJECT_TO_JSVAL(typedArray);
}
jsval longlong_to_jsval( JSContext *cx, long long number )
{
//NSCAssert( sizeof(long long)==8, @"Error!");

View File

@ -3,10 +3,8 @@
// Copyright (c) 2012 Zynga Inc. All rights reserved.
//
#include "chipmunk.h"
#include "cocos2d.h"
#include "jsapi.h"
using namespace cocos2d;
#ifdef __cplusplus
extern "C" {
#endif
@ -15,23 +13,11 @@ JSBool jsval_to_opaque( JSContext *cx, jsval vp, void **out );
JSBool jsval_to_int( JSContext *cx, jsval vp, int *out);
JSBool jsval_to_long( JSContext *cx, jsval vp, long *out);
JSBool jsval_to_longlong( JSContext *cx, jsval vp, long long *out);
JSBool jsval_to_CGPoint( JSContext *cx, jsval vp, cpVect *out );
jsval CGPoint_to_jsval( JSContext *cx, cpVect p );
jsval int_to_jsval( JSContext *cx, int l);
jsval long_to_jsval( JSContext *cx, long l);
jsval longlong_to_jsval( JSContext *cx, long long l);
jsval opaque_to_jsval( JSContext *cx, void* opaque);
JSBool jsval_to_cpBB( JSContext *cx, jsval vp, cpBB *ret );
jsval cpBB_to_jsval(JSContext *cx, cpBB bb );
#ifdef __cplusplus
}
#endif
#define cpVect_to_jsval CGPoint_to_jsval
#define jsval_to_cpVect jsval_to_CGPoint

@ -1 +1 @@
Subproject commit db61f8399655dd90f09d550259b45ae8289364fe
Subproject commit 40ad79373b6c041a3312afb36da6985b91f0a3dc

View File

@ -27,8 +27,7 @@ headers = %(cocosdir)s/cocos2dx/include/cocos2d.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^CCMenu*$".
classes = CCSprite.* CCScene CCNode CCDirector CCLayer.* CCMenu.* CCTouch CC.*Action.* CCMove.* CCRotate.* CCBlink.* CCTint.* CCSequence CCRepeat.* CCFade.* CCEase.* CCScale.* CCTransition.* CCSpawn CCSequence CCAnimat.* CCFlip.* CCDelay.* CCSkew.* CCJump.* CCPlace.* CCShow.* CCProgress.* CCToggleVisibility.* CCHide CCParticle.* CCLabel.* CCAtlas.* CCTextureCache.* CCTexture2D CCParallaxNode CCTileMap.* CCTMX.* CCCallFunc CCRenderTexture CCGridAction CCGrid3DAction CCShaky3D CCWaves3D CCFlipX3D CCFlipY3D CCSpeed CCSet CCObject
classes = CCSprite.* CCScene CCNode CCDirector CCLayer.* CCMenu.* CCTouch CC.*Action.* CCMove.* CCRotate.* CCBlink.* CCTint.* CCSequence CCRepeat.* CCFade.* CCEase.* CCScale.* CCTransition.* CCSpawn CCSequence CCAnimat.* CCFlip.* CCDelay.* CCSkew.* CCJump.* CCPlace.* CCShow.* CCProgress.* CCPointArray CCToggleVisibility.* CCHide CCParticle.* CCLabel.* CCAtlas.* CCTextureCache.* CCTexture2D CCCardinal.* CCCatmullRom.* CCParallaxNode CCTileMap.* CCTMX.* CCCallFunc CCRenderTexture CCGridAction CCGrid3DAction CCShaky3D CCWaves3D CCFlipX3D CCFlipY3D CCSpeed CCActionManager CCSet
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
@ -47,6 +46,8 @@ skip = CCNode::[.*Transform convertToWindowSpace getChildren getGrid setGLServer
CC.*Protocol::[*],
CC.*Delegate::[*],
CCPoolManager::[*],
CCTexture2D::[initWithPVRTCData releaseData setTexParameters initWithData keepData],
CCSet::[begin end],
CCIMEDispatcher::[*],
CCSAXParser::[*],
CCThread::[*],
@ -86,11 +87,14 @@ skip = CCNode::[.*Transform convertToWindowSpace getChildren getGrid setGLServer
*::[copyWith.* onEnter.* onExit.* description getObjectType .*RGB.* .*HSV.*]
rename_functions = CCDirector::[sharedDirector=getInstance],
CCSpriteFrameCache::[sharedSpriteFrameCache=getInstance addSpriteFramesWithFile=addSpriteFrames],
CCSpriteFrameCache::[sharedSpriteFrameCache=getInstance addSpriteFramesWithFile=addSpriteFrames spriteFrameByName=getSpriteFrame],
CCMenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize fontSizeObj=fontSize fontNameObj=fontName],
CCProgressTimer::[setReverseProgress=setReverseDirection],
CCTextureCache::[sharedTextureCache=getInstance],
CCMenuItem::[setEnabled=setIsEnabled]
CCMenuItem::[setEnabled=setIsEnabled],
CCAnimation::[addSpriteFrameWithFileName=addSpriteFrameWithFilename],
CCAnimationCache::[sharedAnimationCache=getInstance addAnimationsWithFile=addAnimations animationByName=getAnimation],
CCLayerGradient::[initWithColor=init]
rename_classes = CCParticleSystemQuad::CCParticleSystem
@ -101,4 +105,7 @@ remove_prefix = CC
base_objects = CCNode CCDirector
# classes that create no constructor
# CCSet is special and we will use a hand-written constructor
abstract_classes = CCDirector CCSpriteFrameCache CCTransitionEaseScene CCSet
rename_classes =