mirror of https://github.com/axmolengine/axmol.git
Merge pull request #2610 from dumganhar/iss1644-dict2jsval-crash
fixed #1644: Passing NULL parameter to ccdictionary_to_jsval will cause crash, and improving performance for ccarray_to_jsval.
This commit is contained in:
commit
b037a38821
|
@ -139,6 +139,7 @@ public:
|
|||
*/
|
||||
#define CCDICT_FOREACH(__dict__, __el__) \
|
||||
CCDictElement* pTmp##__dict__##__el__ = NULL; \
|
||||
if (__dict__) \
|
||||
HASH_ITER(hh, (__dict__)->m_pElements, __el__, pTmp##__dict__##__el__)
|
||||
|
||||
|
||||
|
|
|
@ -1348,47 +1348,48 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr)
|
|||
{
|
||||
JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL);
|
||||
|
||||
if (arr && arr->count() > 0) {
|
||||
for(unsigned int i = 0; i < arr->count(); ++i) {
|
||||
jsval arrElement;
|
||||
CCObject *obj = arr->objectAtIndex(i);
|
||||
CCObject* obj;
|
||||
int i = 0;
|
||||
CCARRAY_FOREACH(arr, obj)
|
||||
{
|
||||
jsval arrElement;
|
||||
|
||||
//First, check whether object is associated with js object.
|
||||
js_proxy_t* jsproxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
|
||||
if (jsproxy) {
|
||||
arrElement = OBJECT_TO_JSVAL(jsproxy->obj);
|
||||
}
|
||||
else {
|
||||
CCString* strVal = NULL;
|
||||
CCDictionary* dictVal = NULL;
|
||||
CCArray* arrVal = NULL;
|
||||
CCDouble* doubleVal = NULL;
|
||||
CCBool* boolVal = NULL;
|
||||
CCFloat* floatVal = NULL;
|
||||
CCInteger* intVal = NULL;
|
||||
|
||||
if((strVal = dynamic_cast<cocos2d::CCString *>(obj))) {
|
||||
arrElement = c_string_to_jsval(cx, strVal->getCString());
|
||||
} else if ((dictVal = dynamic_cast<cocos2d::CCDictionary*>(obj))) {
|
||||
arrElement = ccdictionary_to_jsval(cx, dictVal);
|
||||
} else if ((arrVal = dynamic_cast<cocos2d::CCArray*>(obj))) {
|
||||
arrElement = ccarray_to_jsval(cx, arrVal);
|
||||
} else if ((doubleVal = dynamic_cast<CCDouble*>(obj))) {
|
||||
arrElement = DOUBLE_TO_JSVAL(doubleVal->getValue());
|
||||
} else if ((floatVal = dynamic_cast<CCFloat*>(obj))) {
|
||||
arrElement = DOUBLE_TO_JSVAL(floatVal->getValue());
|
||||
} else if ((intVal = dynamic_cast<CCInteger*>(obj))) {
|
||||
arrElement = INT_TO_JSVAL(intVal->getValue());
|
||||
} else if ((boolVal = dynamic_cast<CCBool*>(obj))) {
|
||||
arrElement = BOOLEAN_TO_JSVAL(boolVal->getValue() ? JS_TRUE : JS_FALSE);
|
||||
} else {
|
||||
CCAssert(false, "the type isn't suppored.");
|
||||
}
|
||||
}
|
||||
if(!JS_SetElement(cx, jsretArr, i, &arrElement)) {
|
||||
break;
|
||||
//First, check whether object is associated with js object.
|
||||
js_proxy_t* jsproxy = js_get_or_create_proxy<cocos2d::CCObject>(cx, obj);
|
||||
if (jsproxy) {
|
||||
arrElement = OBJECT_TO_JSVAL(jsproxy->obj);
|
||||
}
|
||||
else {
|
||||
CCString* strVal = NULL;
|
||||
CCDictionary* dictVal = NULL;
|
||||
CCArray* arrVal = NULL;
|
||||
CCDouble* doubleVal = NULL;
|
||||
CCBool* boolVal = NULL;
|
||||
CCFloat* floatVal = NULL;
|
||||
CCInteger* intVal = NULL;
|
||||
|
||||
if((strVal = dynamic_cast<cocos2d::CCString *>(obj))) {
|
||||
arrElement = c_string_to_jsval(cx, strVal->getCString());
|
||||
} else if ((dictVal = dynamic_cast<cocos2d::CCDictionary*>(obj))) {
|
||||
arrElement = ccdictionary_to_jsval(cx, dictVal);
|
||||
} else if ((arrVal = dynamic_cast<cocos2d::CCArray*>(obj))) {
|
||||
arrElement = ccarray_to_jsval(cx, arrVal);
|
||||
} else if ((doubleVal = dynamic_cast<CCDouble*>(obj))) {
|
||||
arrElement = DOUBLE_TO_JSVAL(doubleVal->getValue());
|
||||
} else if ((floatVal = dynamic_cast<CCFloat*>(obj))) {
|
||||
arrElement = DOUBLE_TO_JSVAL(floatVal->getValue());
|
||||
} else if ((intVal = dynamic_cast<CCInteger*>(obj))) {
|
||||
arrElement = INT_TO_JSVAL(intVal->getValue());
|
||||
} else if ((boolVal = dynamic_cast<CCBool*>(obj))) {
|
||||
arrElement = BOOLEAN_TO_JSVAL(boolVal->getValue() ? JS_TRUE : JS_FALSE);
|
||||
} else {
|
||||
CCAssert(false, "the type isn't suppored.");
|
||||
}
|
||||
}
|
||||
if(!JS_SetElement(cx, jsretArr, i, &arrElement)) {
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return OBJECT_TO_JSVAL(jsretArr);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue