ComponentJS::ComponentJS(const std::string& scriptFileName) : _scriptFileName(scriptFileName) , _jsObj(nullptr) { ScriptingCore* engine = ScriptingCore::getInstance(); JSContext* cx = engine->getGlobalContext(); // Require script JS::RootedValue classValue(cx); _succeedLoadingScript = engine->requireScript(_scriptFileName.c_str(), &classValue); if (_succeedLoadingScript) { JS::RootedObject classObj(cx, classValue.toObjectOrNull()); const JSClass* theClass = JS_GetClass(classObj); JS::RootedValue protoValue(cx); JS_GetProperty(cx, classObj, "prototype", &protoValue); TypeTest<ComponentJS> 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; mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new (std::nothrow) mozilla::Maybe<JS::PersistentRootedObject>(); JS::RootedObject proto(cx, protoValue.toObjectOrNull()); JS::RootedObject parent(cx, typeClass->proto.ref()); jsObj->construct(cx); JS::RootedObject obj(cx, JS_NewObject(cx, theClass, proto, parent)); jsObj->ref() = obj; // Unbind current proxy binding js_proxy_t* jsproxy = js_get_or_create_proxy<ComponentJS>(cx, this); JS::RemoveObjectRoot(cx, &jsproxy->obj); jsb_remove_proxy(jsproxy); // link the native object with the javascript object jsb_new_proxy(this, jsObj->ref()); _jsObj = jsObj; } }
ComponentJS::ComponentJS(const std::string& scriptFileName) : _scriptFileName(scriptFileName) , _jsObj(nullptr) { ScriptingCore* engine = ScriptingCore::getInstance(); JSContext* cx = engine->getGlobalContext(); // Require script JS::RootedValue classValue(cx); _succeedLoadingScript = engine->requireScript(_scriptFileName.c_str(), &classValue); if (_succeedLoadingScript) { JS::RootedObject classObj(cx, classValue.toObjectOrNull()); const JSClass* theClass = JS_GetClass(classObj); JS::RootedValue protoValue(cx); JS_GetProperty(cx, classObj, "prototype", &protoValue); mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new (std::nothrow) mozilla::Maybe<JS::PersistentRootedObject>(); js_type_class_t *typeClass = js_get_type_from_native<cocos2d::ComponentJS>(this); JS::RootedObject proto(cx, protoValue.toObjectOrNull()); JS::RootedObject parent(cx, typeClass->proto.ref()); jsObj->construct(cx); JS::RootedObject obj(cx, JS_NewObject(cx, theClass, proto, parent)); jsObj->ref() = obj; // Unbind current proxy binding js_proxy_t* nproxy = jsb_get_native_proxy(this); if (nproxy) { #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS JS::RemoveObjectRoot(cx, &nproxy->obj); #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS jsb_remove_proxy(nproxy, jsb_get_js_proxy(nproxy->obj)); } // link the native object with the javascript object jsb_new_proxy(this, jsObj->ref()); _jsObj = jsObj; } }