issue #1583: Added jsvals_variadic_to_ccarray function.

This commit is contained in:
James Chen 2012-11-30 21:12:14 +08:00
parent e8c5f5f38c
commit 96e2707f39
2 changed files with 35 additions and 0 deletions

View File

@ -906,6 +906,40 @@ CCAcceleration jsval_to_ccacceleration(JSContext *cx, jsval v) {
return ret;
}
CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc)
{
JSBool ok = JS_FALSE;
CCArray* pArray = CCArray::create();
for( int i=0; i < argc; i++ )
{
double num = 0.0;
// optimization: JS_ValueToNumber is expensive. And can convert an string like "12" to a number
if( JSVAL_IS_NUMBER(*vp)) {
ok = JS_ValueToNumber(cx, *vp, &num );
if (!ok) {
break;
}
pArray->addObject(CCInteger::create((int)num));
}
else if (JSVAL_IS_STRING(*vp))
{
JSStringWrapper str(JSVAL_TO_STRING(*vp), cx);
pArray->addObject(CCString::create(str));
}
else
{
js_proxy_t* p;
JSObject* obj = JSVAL_TO_OBJECT(*vp);
JS_GET_NATIVE_PROXY(p, obj);
if (p) {
pArray->addObject((CCObject*)p->ptr);
}
}
// next
vp++;
}
return pArray;
}
CCRect jsval_to_ccrect(JSContext *cx, jsval v) {
JSObject *tmp;

View File

@ -197,6 +197,7 @@ JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int
CCArray* jsval_to_ccarray(JSContext* cx, jsval v);
CCDictionary* jsval_to_ccdictionary(JSContext* cx, jsval v);
CCAcceleration jsval_to_ccacceleration(JSContext* cx,jsval v);
CCArray* jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc);
// from native
jsval long_long_to_jsval(JSContext* cx, long long v);