Adding JS conversion of CCDictionary objects, also modified CCArray conversion

This commit is contained in:
Rohan Kuruvilla 2012-11-06 11:03:07 -08:00 committed by James Chen
parent 672bed3ed0
commit 66f85468e5
2 changed files with 41 additions and 2 deletions

View File

@ -1024,6 +1024,7 @@ jsval ccarray_to_jsval(JSContext* cx, CCArray *arr) {
for(int i = 0; i < arr->count(); ++i) { for(int i = 0; i < arr->count(); ++i) {
jsval arrElement; jsval arrElement;
CCObject *obj = arr->objectAtIndex(i); CCObject *obj = arr->objectAtIndex(i);
const char *type = typeid(*obj).name();
CCString *testString = dynamic_cast<cocos2d::CCString *>(obj); CCString *testString = dynamic_cast<cocos2d::CCString *>(obj);
CCDictionary* testDict = NULL; CCDictionary* testDict = NULL;
@ -1082,6 +1083,42 @@ jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict)
return OBJECT_TO_JSVAL(jsRet); return OBJECT_TO_JSVAL(jsRet);
} }
CCDictionary* jsval_to_ccdictionary(JSContext* cx, jsval v) {
JSObject *itEl = JS_NewPropertyIterator(cx, JSVAL_TO_OBJECT(v));
CCDictionary *dict = NULL;
jsid propId;
do {
jsval prop;
JS_GetPropertyById(cx, JSVAL_TO_OBJECT(v), propId, &prop);
js_proxy_t *proxy;
JSObject *tmp = JSVAL_TO_OBJECT(prop);
JS_GET_NATIVE_PROXY(proxy, tmp);
cocos2d::CCObject* cobj = (cocos2d::CCObject *)(proxy ? proxy->ptr : NULL);
TEST_NATIVE_OBJECT(cx, cobj)
jsval key;
std::string keyStr;
if(JSID_IS_STRING(propId)) {
JS_IdToValue(cx, propId, &key);
keyStr = jsval_to_std_string(cx, key);
}
if(JSVAL_IS_NULL(key)) continue;
if(!dict) {
dict = CCDictionary::create();
}
dict->setObject(cobj, keyStr);
} while(JS_NextProperty(cx, itEl, &propId));
return dict;
}
jsval long_long_to_jsval(JSContext* cx, long long v) { jsval long_long_to_jsval(JSContext* cx, long long v) {
JSObject *tmp = JS_NewUint32Array(cx, 2); JSObject *tmp = JS_NewUint32Array(cx, 2);
uint32_t *data = (uint32_t *)JS_GetArrayBufferViewData(tmp, cx); uint32_t *data = (uint32_t *)JS_GetArrayBufferViewData(tmp, cx);

View File

@ -188,8 +188,8 @@ ccColor4F jsval_to_cccolor4f(JSContext *cx, jsval v);
ccColor3B jsval_to_cccolor3b(JSContext *cx, jsval v); ccColor3B jsval_to_cccolor3b(JSContext *cx, jsval v);
JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int *numPoints); JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int *numPoints);
CCArray* jsval_to_ccarray(JSContext* cx, jsval v); CCArray* jsval_to_ccarray(JSContext* cx, jsval v);
jsval ccarray_to_jsval(JSContext* cx, CCArray *arr); CCDictionary* jsval_to_ccdictionary(JSContext* cx, jsval v);
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary* dict);
// from native // from native
jsval long_long_to_jsval(JSContext* cx, long long v); jsval long_long_to_jsval(JSContext* cx, long long v);
jsval std_string_to_jsval(JSContext* cx, std::string& v); jsval std_string_to_jsval(JSContext* cx, std::string& v);
@ -201,6 +201,8 @@ jsval ccgridsize_to_jsval(JSContext* cx, ccGridSize& v);
jsval cccolor4b_to_jsval(JSContext* cx, ccColor4B& v); jsval cccolor4b_to_jsval(JSContext* cx, ccColor4B& v);
jsval cccolor4f_to_jsval(JSContext* cx, ccColor4F& v); jsval cccolor4f_to_jsval(JSContext* cx, ccColor4F& v);
jsval cccolor3b_to_jsval(JSContext* cx, const ccColor3B& v); jsval cccolor3b_to_jsval(JSContext* cx, const ccColor3B& v);
jsval ccdictionary_to_jsval(JSContext* cx, CCDictionary *dict);
jsval ccarray_to_jsval(JSContext* cx, CCArray *arr);
JSObject* NewGlobalObject(JSContext* cx); JSObject* NewGlobalObject(JSContext* cx);
JSBool jsNewGlobal(JSContext* cx, unsigned argc, jsval* vp); JSBool jsNewGlobal(JSContext* cx, unsigned argc, jsval* vp);