static bool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::RootedValue initializing(cx); bool isNewValid = true; if (isNewValid) { TypeTest<T> t; js_type_class_t *typeClass = nullptr; std::string typeName = t.s_name(); auto typeMapIter = _js_global_type_map.find(typeName); CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!"); typeClass = typeMapIter->second; CCASSERT(typeClass, "The value is null."); #if (COCOS2D_VERSION >= 0x00031000) JS::RootedObject proto(cx, typeClass->proto.ref()); JS::RootedObject parent(cx, typeClass->parentProto.ref()); #else JS::RootedObject proto(cx, typeClass->proto.get()); JS::RootedObject parent(cx, typeClass->parentProto.get()); #endif JS::RootedObject _tmp(cx, JS_NewObject(cx, typeClass->jsclass, proto, parent)); T* cobj = new T(); js_proxy_t *pp = jsb_new_proxy(cobj, _tmp); AddObjectRoot(cx, &pp->obj); args.rval().set(OBJECT_TO_JSVAL(_tmp)); return true; } return false; }
inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) { auto proxy = jsb_get_native_proxy(native_obj); if (!proxy) { js_type_class_t *typeProxy = js_get_type_from_native<T>(native_obj); // Return NULL if can't find its type rather than making an assert. // assert(typeProxy); if (!typeProxy) { CCLOGINFO("Could not find the type of native object."); return NULL; } JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS::RootedObject proto(cx, typeProxy->proto.ref().get()); JS::RootedObject parent(cx, typeProxy->parentProto.ref().get()); JS::RootedObject js_obj(cx, JS_NewObject(cx, typeProxy->jsclass, proto, parent)); proxy = jsb_new_proxy(native_obj, js_obj); #ifdef DEBUG AddNamedObjectRoot(cx, &proxy->obj, typeid(*native_obj).name()); #else AddObjectRoot(cx, &proxy->obj); #endif return proxy; } else { return proxy; } return NULL; }
static bool dummy_constructor(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::RootedValue initializing(cx); bool isNewValid = true; #if not $script_control_cpp JS::RootedObject global(cx, ScriptingCore::getInstance()->getGlobalObject()); isNewValid = JS_GetProperty(cx, global, "initializing", &initializing) && initializing.toBoolean(); #end if if (isNewValid) { TypeTest<T> t; js_type_class_t *typeClass = nullptr; std::string typeName = t.s_name(); auto typeMapIter = _js_global_type_map.find(typeName); CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!"); typeClass = typeMapIter->second; CCASSERT(typeClass, "The value is null."); JS::RootedObject proto(cx, typeClass->proto.get()); JS::RootedObject parent(cx, typeClass->parentProto.get()); JS::RootedObject _tmp(cx, JS_NewObject(cx, typeClass->jsclass, proto, parent)); #if $script_control_cpp T* cobj = new T(); js_proxy_t *pp = jsb_new_proxy(cobj, _tmp); AddObjectRoot(cx, &pp->obj); #end if args.rval().set(OBJECT_TO_JSVAL(_tmp)); return true; } #if not $script_control_cpp JS_ReportError(cx, "Constructor for the requested class is not available, please refer to the API reference."); #end if return false; }