issue #1549: Added "ccdictionary_to_jsval" function, now TMXOrthoObjectsTest and TMXIsoObjectsTest don't crash.

This commit is contained in:
James Chen 2012-11-06 16:11:45 +08:00
parent 26af535521
commit 449c719d96
2 changed files with 43 additions and 0 deletions

View File

@ -1062,8 +1062,16 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) {
CCObject *obj = arr->objectAtIndex(i);
CCString *testString = dynamic_cast<cocos2d::CCString *>(obj);
CCDictionary* testDict = NULL;
CCArray* testArray = NULL;
// XXX: Only supports string, since all data read from plist files will be stored as string in cocos2d-x
// Do we need to convert string to js base type ?
if(testString) {
arrElement = c_string_to_jsval(cx, testString->getCString());
} else if (testDict = dynamic_cast<cocos2d::CCDictionary*>(obj)) {
arrElement = ccdictionary_to_jsval(cx, testDict);
} else if (testArray = dynamic_cast<cocos2d::CCArray*>(obj)) {
arrElement = ccarray_to_jsval(cx, testArray);
} else {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
arrElement = OBJECT_TO_JSVAL(proxy->obj);
@ -1076,6 +1084,40 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) {
return OBJECT_TO_JSVAL(jsretArr);
}
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict)
{
JSObject* jsRet = JS_NewObject(cx, NULL, NULL, NULL);
CCDictElement* pElement = NULL;
CCDICT_FOREACH(dict, pElement)
{
jsval dictElement;
CCString* obj = dynamic_cast<CCString*>(pElement->getObject());
CCString *testString = dynamic_cast<cocos2d::CCString *>(obj);
CCDictionary* testDict = NULL;
CCArray* testArray = NULL;
// XXX: Only supports string, since all data read from plist files will be stored as string in cocos2d-x
// Do we need to convert string to js base type ?
if(testString) {
dictElement = c_string_to_jsval(cx, testString->getCString());
} else if (testDict = dynamic_cast<cocos2d::CCDictionary*>(obj)) {
dictElement = ccdictionary_to_jsval(cx, testDict);
} else if (testArray = dynamic_cast<cocos2d::CCArray*>(obj)) {
dictElement = ccarray_to_jsval(cx, testArray);
} else {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
dictElement = OBJECT_TO_JSVAL(proxy->obj);
}
const char* key = pElement->getStrKey();
if (key && strlen(key) > 0)
{
JS_SetProperty(cx, jsRet, key, &dictElement);
}
}
return OBJECT_TO_JSVAL(jsRet);
}
jsval long_long_to_jsval(JSContext* cx, long long v) {
JSObject *tmp = JS_NewUint32Array(cx, 2);
uint32_t *data = (uint32_t *)JS_GetArrayBufferViewData(tmp, cx);

View File

@ -189,6 +189,7 @@ ccColor3B jsval_to_cccolor3b(JSContext *cx, jsval v);
JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int *numPoints);
CCArray* jsval_to_ccarray(JSContext* cx, jsval v);
jsval ccarray_to_jsval(JSContext* cx, CCArray *arr);
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict);
// from native
jsval long_long_to_jsval(JSContext* cx, long long v);
jsval std_string_to_jsval(JSContext* cx, std::string& v);