bool jsval_to_int32( JSContext *cx, JS::HandleValue vp, int32_t *outval ) { bool ok = true; int32_t ret; ok &= JS::ToInt32(cx, vp, &ret); JSB_PRECONDITION3(ok, cx, false, "Error processing jsval_to_int32"); ok &= !isnan(ret); JSB_PRECONDITION3(ok, cx, false, "Error processing jsval_to_int32"); *outval = ret; return ok; }
bool jsval_to_animationInfo(JSContext* cx, JS::HandleValue vp, cocostudio::timeline::AnimationInfo* ret) { JS::RootedObject tmp(cx); JS::RootedValue jsName(cx); JS::RootedValue jsStartId(cx); JS::RootedValue jsEndId(cx); std::string name; double startIndex, endIndex; bool ok = vp.isObject() && JS_ValueToObject(cx, vp, &tmp) && JS_GetProperty(cx, tmp, "name", &jsName) && JS_GetProperty(cx, tmp, "startIndex", &jsStartId) && JS_GetProperty(cx, tmp, "endIndex", &jsEndId) && JS::ToNumber(cx, jsStartId, &startIndex) && JS::ToNumber(cx, jsEndId, &endIndex) && jsval_to_std_string(cx, jsName, &name) && !std::isnan(startIndex) && !std::isnan(endIndex); JSB_PRECONDITION3(ok, cx, false, "Error processing arguments"); ret->name = name; ret->startIndex = (int)startIndex; ret->endIndex = (int)endIndex; return true; }
bool jsval_to_std_string(JSContext *cx, JS::HandleValue v, std::string* ret) { if(v.isString() || v.isNumber()) { JSString *tmp = JS::ToString(cx, v); JSB_PRECONDITION3(tmp, cx, false, "Error processing jsval_to_std_string"); JSStringWrapper str(tmp, cx); *ret = str.get(); return true; } return false; }
bool js_cocos2dx_DrawNode3D_drawCube(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); js_proxy_t *proxy = jsb_get_js_proxy(obj); cocos2d::DrawNode3D* cobj = (cocos2d::DrawNode3D *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_DrawNode3D_drawCube : Invalid Native Object"); if (argc == 2) { cocos2d::Vec3 arg0[8]; cocos2d::Color4F arg1; JS::RootedObject jsVec3Array(cx, args.get(0).toObjectOrNull()); JSB_PRECONDITION3( jsVec3Array && JS_IsArrayObject( cx, jsVec3Array), cx, false, "augument must be an array"); uint32_t len = 0; JS_GetArrayLength(cx, jsVec3Array, &len); if (len != 8) { JS_ReportError(cx, "array length error: %d, was expecting 8", len); } for (uint32_t i=0; i < len; i++) { JS::RootedValue value(cx); if (JS_GetElement(cx, jsVec3Array, i, &value)) { ok &= jsval_to_vector3(cx, value, &arg0[i]); if(!ok) break; } } ok &= jsval_to_cccolor4f(cx, args.get(1), &arg1); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_DrawNode3D_drawCube : Error processing arguments"); cobj->drawCube(arg0, arg1); args.rval().setUndefined(); return true; } JS_ReportError(cx, "js_cocos2dx_DrawNode3D_drawCube : wrong number of arguments: %d, was expecting %d", argc, 2); return false; }