Fix Chipmunk bindings issue

This commit is contained in:
pandamicro 2015-11-27 18:27:08 +08:00
parent e13e0cb886
commit 43c68aa16b
5 changed files with 57 additions and 105 deletions

View File

@ -7149,7 +7149,6 @@
"COCOS2D_DEBUG=1", "COCOS2D_DEBUG=1",
USE_FILE32API, USE_FILE32API,
"CC_ENABLE_CHIPMUNK_INTEGRATION=1", "CC_ENABLE_CHIPMUNK_INTEGRATION=1",
DEBUG,
); );
GCC_SYMBOLS_PRIVATE_EXTERN = YES; GCC_SYMBOLS_PRIVATE_EXTERN = YES;
GCC_TREAT_WARNINGS_AS_ERRORS = YES; GCC_TREAT_WARNINGS_AS_ERRORS = YES;

View File

@ -601,7 +601,7 @@ void ScriptingCore::createGlobalContext() {
JS_SetErrorReporter(_cx, ScriptingCore::reportError); JS_SetErrorReporter(_cx, ScriptingCore::reportError);
#if defined(JS_GC_ZEAL) && defined(DEBUG) #if defined(JS_GC_ZEAL) && defined(DEBUG)
//JS_SetGCZeal(this->_cx, 2, JS_DEFAULT_ZEAL_FREQ); JS_SetGCZeal(this->_cx, 2, JS_DEFAULT_ZEAL_FREQ);
#endif #endif
_global.construct(_cx); _global.construct(_cx);

View File

@ -762,11 +762,11 @@ struct collision_handler {
cpCollisionType typeA; cpCollisionType typeA;
cpCollisionType typeB; cpCollisionType typeB;
JS::Heap<JSObject*> begin; mozilla::Maybe<JS::PersistentRootedObject> begin;
JS::Heap<JSObject*> pre; mozilla::Maybe<JS::PersistentRootedObject> pre;
JS::Heap<JSObject*> post; mozilla::Maybe<JS::PersistentRootedObject> post;
JS::Heap<JSObject*> separate; mozilla::Maybe<JS::PersistentRootedObject> separate;
JS::Heap<JSObject*> jsthis; mozilla::Maybe<JS::PersistentRootedObject> jsthis;
JSContext *cx; JSContext *cx;
// "owner" of the collision handler // "owner" of the collision handler
@ -777,6 +777,16 @@ struct collision_handler {
unsigned int is_oo; // Objected oriented API ? unsigned int is_oo; // Objected oriented API ?
UT_hash_handle hh; UT_hash_handle hh;
collision_handler()
{
JSContext *globalcx = ScriptingCore::getInstance()->getGlobalContext();
begin.construct(globalcx);
pre.construct(globalcx);
post.construct(globalcx);
separate.construct(globalcx);
jsthis.construct(globalcx);
}
}; };
// hash // hash
@ -803,16 +813,16 @@ static cpBool myCollisionBegin(cpArbiter *arb, cpSpace *space, void *data)
args[0] = c_class_to_jsval(handler->cx, arb, arbiterProto, JSB_cpArbiter_class, "cpArbiter"); args[0] = c_class_to_jsval(handler->cx, arb, arbiterProto, JSB_cpArbiter_class, "cpArbiter");
args[1] = c_class_to_jsval(handler->cx, space, spaceProto, JSB_cpSpace_class, "cpArbiter"); args[1] = c_class_to_jsval(handler->cx, space, spaceProto, JSB_cpSpace_class, "cpArbiter");
} else { } else {
args[0] = opaque_to_jsval( handler->cx, arb); args[0] = opaque_to_jsval(handler->cx, arb);
args[1] = opaque_to_jsval( handler->cx, space ); args[1] = opaque_to_jsval(handler->cx, space );
} }
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
JS::RootedValue rval(handler->cx); JS::RootedValue rval(handler->cx);
JS::RootedObject jsthis(handler->cx, handler->jsthis); JS::RootedObject jsthis(handler->cx, handler->jsthis.ref());
JS::RootedValue jsbegin(handler->cx, OBJECT_TO_JSVAL(handler->begin)); JS::RootedValue jsbegin(handler->cx, OBJECT_TO_JSVAL(handler->begin.ref()));
bool ok = JS_CallFunctionValue( handler->cx, jsthis, jsbegin, JS::HandleValueArray::fromMarkedLocation(2, args), &rval); bool ok = JS_CallFunctionValue(handler->cx, jsthis, jsbegin, JS::HandleValueArray::fromMarkedLocation(2, args), &rval);
JSB_PRECONDITION2(ok, handler->cx, cpFalse, "Error calling collision callback: begin"); JSB_PRECONDITION2(ok, handler->cx, cpFalse, "Error calling collision callback: begin");
if( rval.isBoolean() ) { if( rval.isBoolean() ) {
@ -840,8 +850,8 @@ static cpBool myCollisionPre(cpArbiter *arb, cpSpace *space, void *data)
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
JS::RootedValue rval(handler->cx); JS::RootedValue rval(handler->cx);
JS::RootedObject jsthis(handler->cx, handler->jsthis); JS::RootedObject jsthis(handler->cx, handler->jsthis.ref());
JS::RootedValue jspre(handler->cx, OBJECT_TO_JSVAL(handler->pre)); JS::RootedValue jspre(handler->cx, OBJECT_TO_JSVAL(handler->pre.ref()));
bool ok = JS_CallFunctionValue( handler->cx, jsthis, jspre, JS::HandleValueArray::fromMarkedLocation(2, args), &rval); bool ok = JS_CallFunctionValue( handler->cx, jsthis, jspre, JS::HandleValueArray::fromMarkedLocation(2, args), &rval);
JSB_PRECONDITION2(ok, handler->cx, false, "Error calling collision callback: pre"); JSB_PRECONDITION2(ok, handler->cx, false, "Error calling collision callback: pre");
@ -871,8 +881,8 @@ static void myCollisionPost(cpArbiter *arb, cpSpace *space, void *data)
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
JS::RootedValue ignore(handler->cx); JS::RootedValue ignore(handler->cx);
JS::RootedObject jsthis(handler->cx, handler->jsthis); JS::RootedObject jsthis(handler->cx, handler->jsthis.ref());
JS::RootedValue jspost(handler->cx, OBJECT_TO_JSVAL(handler->post)); JS::RootedValue jspost(handler->cx, OBJECT_TO_JSVAL(handler->post.ref()));
bool ok = JS_CallFunctionValue( handler->cx, jsthis, jspost, JS::HandleValueArray::fromMarkedLocation(2, args), &ignore); bool ok = JS_CallFunctionValue( handler->cx, jsthis, jspost, JS::HandleValueArray::fromMarkedLocation(2, args), &ignore);
JSB_PRECONDITION2(ok, handler->cx, , "Error calling collision callback: Post"); JSB_PRECONDITION2(ok, handler->cx, , "Error calling collision callback: Post");
} }
@ -897,8 +907,8 @@ static void myCollisionSeparate(cpArbiter *arb, cpSpace *space, void *data)
JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET
JS::RootedValue ignore(handler->cx); JS::RootedValue ignore(handler->cx);
JS::RootedObject jsthis(handler->cx, handler->jsthis); JS::RootedObject jsthis(handler->cx, handler->jsthis.ref());
JS::RootedValue jssep(handler->cx, OBJECT_TO_JSVAL(handler->separate)); JS::RootedValue jssep(handler->cx, OBJECT_TO_JSVAL(handler->separate.ref()));
bool ok = JS_CallFunctionValue( handler->cx, jsthis, jssep, JS::HandleValueArray::fromMarkedLocation(2, args), &ignore); bool ok = JS_CallFunctionValue( handler->cx, jsthis, jssep, JS::HandleValueArray::fromMarkedLocation(2, args), &ignore);
JSB_PRECONDITION2(ok, handler->cx, , "Error calling collision callback: Separate");} JSB_PRECONDITION2(ok, handler->cx, , "Error calling collision callback: Separate");}
@ -920,22 +930,8 @@ void JSB_cpSpace_finalize(JSFreeOp *fop, JSObject *jsthis)
struct collision_handler *current = nullptr, *tmp = nullptr; struct collision_handler *current = nullptr, *tmp = nullptr;
HASH_ITER(hh, collision_handler_hash, current, tmp) { HASH_ITER(hh, collision_handler_hash, current, tmp) {
if( current->space == space ) { if( current->space == space ) {
JSContext *cx = current->cx;
// unroot it
if( current->begin ) {
JS::RemoveObjectRoot(cx, &current->begin);
}
if( current->pre )
JS::RemoveObjectRoot(cx, &current->pre);
if( current->post )
JS::RemoveObjectRoot(cx, &current->post);
if( current->separate )
JS::RemoveObjectRoot(cx, &current->separate);
HASH_DEL(collision_handler_hash,current); /* delete; users advances to next */ HASH_DEL(collision_handler_hash,current); /* delete; users advances to next */
free(current); /* optional- if you want to free */ delete current; /* optional- if you want to free */
} }
} }
@ -953,14 +949,16 @@ void JSB_cpSpace_finalize(JSFreeOp *fop, JSObject *jsthis)
#pragma mark addCollisionHandler #pragma mark addCollisionHandler
static static
bool __jsb_cpSpace_addCollisionHandler(JSContext *cx, jsval *vp, jsval *argvp, cpSpace *space, unsigned int is_oo) bool __jsb_cpSpace_addCollisionHandler(JSContext *cx, jsval *vp, jsval *argvp, JS::HandleObject jsspace, cpSpace *space, unsigned int is_oo)
{ {
struct collision_handler *handler = (struct collision_handler*) malloc( sizeof(*handler) ); struct collision_handler *handler = new collision_handler();
handler->typeA = 0; handler->typeA = 0;
handler->typeB = 0; handler->typeB = 0;
JSB_PRECONDITION(handler, "Error allocating memory"); JSB_PRECONDITION(handler, "Error allocating memory");
handler->jsthis.ref() = jsspace;
bool ok = true; bool ok = true;
// args // args
@ -969,40 +967,28 @@ bool __jsb_cpSpace_addCollisionHandler(JSContext *cx, jsval *vp, jsval *argvp, c
ok &= jsval_to_int(cx, jstypeA, (int32_t*) &handler->typeA ); ok &= jsval_to_int(cx, jstypeA, (int32_t*) &handler->typeA );
ok &= jsval_to_int(cx, jstypeB, (int32_t*) &handler->typeB ); ok &= jsval_to_int(cx, jstypeB, (int32_t*) &handler->typeB );
handler->begin = argvp->toObjectOrNull(); handler->begin.ref() = argvp->toObjectOrNull();
argvp++; argvp++;
handler->pre = argvp->toObjectOrNull(); handler->pre.ref() = argvp->toObjectOrNull();
argvp++; argvp++;
handler->post = argvp->toObjectOrNull(); handler->post.ref() = argvp->toObjectOrNull();
argvp++; argvp++;
handler->separate = argvp->toObjectOrNull(); handler->separate.ref() = argvp->toObjectOrNull();
argvp++; argvp++;
JSB_PRECONDITION(ok, "Error parsing arguments"); JSB_PRECONDITION(ok, "Error parsing arguments");
// Object Oriented API ? // Object Oriented API ?
handler->is_oo = is_oo; handler->is_oo = is_oo;
// owner of the collision handler // owner of the collision handler
handler->space = space; handler->space = space;
// Root it
if( handler->begin )
JS::AddNamedObjectRoot(cx, &handler->begin, "begin collision_handler");
if( handler->pre )
JS::AddNamedObjectRoot(cx, &handler->pre, "pre collision_handler");
if( handler->post )
JS::AddNamedObjectRoot(cx, &handler->post, "post collision_handler");
if( handler->separate )
JS::AddNamedObjectRoot(cx, &handler->separate, "separate collision_handler");
handler->cx = cx; handler->cx = cx;
cpSpaceAddCollisionHandler(space, handler->typeA, handler->typeB, cpSpaceAddCollisionHandler(space, handler->typeA, handler->typeB,
!handler->begin ? NULL : &myCollisionBegin, !handler->begin.ref() ? NULL : &myCollisionBegin,
!handler->pre ? NULL : &myCollisionPre, !handler->pre.ref() ? NULL : &myCollisionPre,
!handler->post ? NULL : &myCollisionPost, !handler->post.ref() ? NULL : &myCollisionPost,
!handler->separate ? NULL : &myCollisionSeparate, !handler->separate.ref() ? NULL : &myCollisionSeparate,
handler ); handler );
@ -1015,7 +1001,7 @@ bool __jsb_cpSpace_addCollisionHandler(JSContext *cx, jsval *vp, jsval *argvp, c
HASH_FIND_INT(collision_handler_hash, &paired_key, hashElement); HASH_FIND_INT(collision_handler_hash, &paired_key, hashElement);
if( hashElement ) { if( hashElement ) {
HASH_DEL( collision_handler_hash, hashElement ); HASH_DEL( collision_handler_hash, hashElement );
free( hashElement ); delete hashElement;
} }
handler->hash_key = paired_key; handler->hash_key = paired_key;
@ -1037,7 +1023,8 @@ bool JSB_cpSpaceAddCollisionHandler(JSContext *cx, uint32_t argc, jsval *vp)
bool ok = jsval_to_opaque(cx, jsarg, (void**)&space); bool ok = jsval_to_opaque(cx, jsarg, (void**)&space);
JSB_PRECONDITION(ok, "Error parsing arguments"); JSB_PRECONDITION(ok, "Error parsing arguments");
return __jsb_cpSpace_addCollisionHandler(cx, vp, argvp, space, 0); JS::RootedObject jsspace(cx, jsarg.toObjectOrNull());
return __jsb_cpSpace_addCollisionHandler(cx, vp, argvp, jsspace, space, 0);
} }
// method // method
@ -1050,7 +1037,7 @@ bool JSB_cpSpace_addCollisionHandler(JSContext *cx, uint32_t argc, jsval *vp)
struct jsb_c_proxy_s* proxy = jsb_get_c_proxy_for_jsobject(jsthis); struct jsb_c_proxy_s* proxy = jsb_get_c_proxy_for_jsobject(jsthis);
void *handle = proxy->handle; void *handle = proxy->handle;
return __jsb_cpSpace_addCollisionHandler(cx, vp, args.array(), (cpSpace*)handle, 1); return __jsb_cpSpace_addCollisionHandler(cx, vp, args.array(), jsthis, (cpSpace*)handle, 1);
} }
bool JSB_cpSpace_setDefaultCollisionHandler(JSContext *cx, uint32_t argc, jsval *vp) bool JSB_cpSpace_setDefaultCollisionHandler(JSContext *cx, uint32_t argc, jsval *vp)
@ -1061,30 +1048,28 @@ bool JSB_cpSpace_setDefaultCollisionHandler(JSContext *cx, uint32_t argc, jsval
struct jsb_c_proxy_s* proxy = jsb_get_c_proxy_for_jsobject(jsthis); struct jsb_c_proxy_s* proxy = jsb_get_c_proxy_for_jsobject(jsthis);
cpSpace* space = (cpSpace*) proxy->handle; cpSpace* space = (cpSpace*) proxy->handle;
collision_handler *handler = (collision_handler*) malloc( sizeof(collision_handler) ); collision_handler *handler = new collision_handler();
JSB_PRECONDITION(handler, "Error allocating memory"); JSB_PRECONDITION(handler, "Error allocating memory");
handler->typeA = 0; handler->typeA = 0;
handler->typeB = 0; handler->typeB = 0;
handler->jsthis = jsthis; handler->jsthis.ref() = jsthis;
handler->begin.ref() = args.get(0).toObjectOrNull();
handler->begin = args.get(0).toObjectOrNull(); handler->pre.ref() = args.get(1).toObjectOrNull();
handler->pre = args.get(1).toObjectOrNull(); handler->post.ref() = args.get(2).toObjectOrNull();
handler->post = args.get(2).toObjectOrNull(); handler->separate.ref() = args.get(3).toObjectOrNull();
handler->separate = args.get(3).toObjectOrNull();
// Object Oriented API ? // Object Oriented API ?
handler->is_oo = 1; handler->is_oo = 1;
// owner of the collision handler // owner of the collision handler
handler->space = space; handler->space = space;
handler->cx = cx; handler->cx = cx;
cpSpaceSetDefaultCollisionHandler(space, cpSpaceSetDefaultCollisionHandler(space,
!handler->begin ? NULL : &myCollisionBegin, !handler->begin.ref() ? NULL : &myCollisionBegin,
!handler->pre ? NULL : &myCollisionPre, !handler->pre.ref() ? NULL : &myCollisionPre,
!handler->post ? NULL : &myCollisionPost, !handler->post.ref() ? NULL : &myCollisionPost,
!handler->separate ? NULL : &myCollisionSeparate, !handler->separate.ref() ? NULL : &myCollisionSeparate,
handler ); handler );
// //
@ -1095,34 +1080,14 @@ bool JSB_cpSpace_setDefaultCollisionHandler(JSContext *cx, uint32_t argc, jsval
unsigned long paired_key = pair_ints(handler->typeA, handler->typeB ); unsigned long paired_key = pair_ints(handler->typeA, handler->typeB );
HASH_FIND_INT(collision_handler_hash, &paired_key, hashElement); HASH_FIND_INT(collision_handler_hash, &paired_key, hashElement);
if( hashElement ) { if( hashElement ) {
if( hashElement->begin ) {
JS::RemoveObjectRoot(cx, &hashElement->begin);
}
if( hashElement->pre )
JS::RemoveObjectRoot(cx, &hashElement->pre);
if( hashElement->post )
JS::RemoveObjectRoot(cx, &hashElement->post);
if( hashElement->separate )
JS::RemoveObjectRoot(cx, &hashElement->separate);
HASH_DEL( collision_handler_hash, hashElement ); HASH_DEL( collision_handler_hash, hashElement );
free( hashElement ); delete hashElement;
} }
handler->hash_key = paired_key; handler->hash_key = paired_key;
HASH_ADD_INT( collision_handler_hash, hash_key, handler ); HASH_ADD_INT( collision_handler_hash, hash_key, handler );
// Root it
if( handler->begin )
JS::AddNamedObjectRoot(cx, &handler->begin, "begin collision_handler");
if( handler->pre )
JS::AddNamedObjectRoot(cx, &handler->pre, "pre collision_handler");
if( handler->post )
JS::AddNamedObjectRoot(cx, &handler->post, "post collision_handler");
if( handler->separate )
JS::AddNamedObjectRoot(cx, &handler->separate, "separate collision_handler");
args.rval().setUndefined(); args.rval().setUndefined();
return true; return true;
} }
@ -1149,19 +1114,8 @@ bool __jsb_cpSpace_removeCollisionHandler(JSContext *cx, jsval *vp, jsval *argvp
unsigned long key = pair_ints(typeA, typeB ); unsigned long key = pair_ints(typeA, typeB );
HASH_FIND_INT(collision_handler_hash, &key, hashElement); HASH_FIND_INT(collision_handler_hash, &key, hashElement);
if( hashElement ) { if( hashElement ) {
// unroot it
if( hashElement->begin )
JS::RemoveObjectRoot(cx, &hashElement->begin);
if( hashElement->pre )
JS::RemoveObjectRoot(cx, &hashElement->pre);
if( hashElement->post )
JS::RemoveObjectRoot(cx, &hashElement->post);
if( hashElement->separate )
JS::RemoveObjectRoot(cx, &hashElement->separate);
HASH_DEL( collision_handler_hash, hashElement ); HASH_DEL( collision_handler_hash, hashElement );
free( hashElement ); delete hashElement;
} }
return true; return true;

View File

@ -903,7 +903,6 @@
"COCOS2D_DEBUG=1", "COCOS2D_DEBUG=1",
USE_FILE32API, USE_FILE32API,
"CC_ENABLE_CHIPMUNK_INTEGRATION=1", "CC_ENABLE_CHIPMUNK_INTEGRATION=1",
DEBUG,
); );
GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;

@ -1 +1 @@
Subproject commit 3dd232b790e85b2cc4a0a0a0e2fb1fc103f57249 Subproject commit 250673608e23c9644b96a8e9ef886ab4db96aaf7