Class.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "Class.hpp"
  22. #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC
  23. #include "Object.hpp"
  24. #include "Utils.hpp"
  25. #include "ScriptEngine.hpp"
  26. #include "State.hpp"
  27. namespace se {
  28. #define JS_FN(name, func, attr) {name, func, attr}
  29. #define JS_FS_END JS_FN(0, 0, 0)
  30. #define JS_PSGS(name, getter, setter, attr) {name, getter, setter, attr}
  31. #define JS_PS_END JS_PSGS(0, 0, 0, 0)
  32. namespace {
  33. // std::unordered_map<std::string, Class *> __clsMap;
  34. JSContextRef __cx = nullptr;
  35. std::vector<Class*> __allClasses;
  36. void defaultFinalizeCallback(JSObjectRef _obj)
  37. {
  38. void* nativeThisObject = JSObjectGetPrivate(_obj);
  39. if (nativeThisObject != nullptr)
  40. {
  41. State state(nativeThisObject);
  42. Object* _thisObject = state.thisObject();
  43. if (_thisObject) _thisObject->_cleanup(nativeThisObject);
  44. JSObjectSetPrivate(_obj, nullptr);
  45. SAFE_DEC_REF(_thisObject);
  46. }
  47. }
  48. }
  49. Class::Class()
  50. : _parent(nullptr)
  51. , _proto(nullptr)
  52. , _parentProto(nullptr)
  53. , _ctor(nullptr)
  54. , _jsCls(nullptr)
  55. , _finalizeOp(nullptr)
  56. {
  57. _jsClsDef = kJSClassDefinitionEmpty;
  58. __allClasses.push_back(this);
  59. }
  60. Class::~Class()
  61. {
  62. }
  63. Class* Class::create(const std::string& className, Object* obj, Object* parentProto, JSObjectCallAsConstructorCallback ctor)
  64. {
  65. Class* cls = new Class();
  66. if (cls != nullptr && !cls->init(className, obj, parentProto, ctor))
  67. {
  68. delete cls;
  69. cls = nullptr;
  70. }
  71. return cls;
  72. }
  73. bool Class::init(const std::string &clsName, Object* parent, Object *parentProto, JSObjectCallAsConstructorCallback ctor)
  74. {
  75. _name = clsName;
  76. _parent = parent;
  77. SAFE_INC_REF(_parent);
  78. _parentProto = parentProto;
  79. SAFE_INC_REF(_parentProto);
  80. _ctor = ctor;
  81. return true;
  82. }
  83. bool Class::install()
  84. {
  85. // assert(__clsMap.find(_name) == __clsMap.end());
  86. //
  87. // __clsMap.emplace(_name, this);
  88. _jsClsDef.version = 0;
  89. _jsClsDef.attributes = kJSClassAttributeNone;
  90. _jsClsDef.className = _name.c_str();
  91. if (_parentProto != nullptr)
  92. {
  93. _jsClsDef.parentClass = _parentProto->_getClass()->_jsCls;
  94. }
  95. _funcs.push_back(JS_FS_END);
  96. // _properties.push_back(JS_PS_END);
  97. // _jsClsDef.staticValues = _properties.data();
  98. _jsClsDef.staticFunctions = _funcs.data();
  99. // _jsClsDef.getProperty = _getPropertyCallback;
  100. // _jsClsDef.setProperty = _setPropertyCallback;
  101. // _jsClsDef.hasProperty = _hasPropertyCallback;
  102. if (_finalizeOp != nullptr)
  103. _jsClsDef.finalize = _finalizeOp;
  104. else
  105. _jsClsDef.finalize = defaultFinalizeCallback;
  106. _jsCls = JSClassCreate(&_jsClsDef);
  107. JSObjectRef jsCtor = JSObjectMakeConstructor(__cx, _jsCls, _ctor);
  108. HandleObject ctorObj(Object::_createJSObject(nullptr, jsCtor));
  109. Value functionCtor;
  110. ScriptEngine::getInstance()->getGlobalObject()->getProperty("Function", &functionCtor);
  111. ctorObj->setProperty("constructor", functionCtor);
  112. JSValueRef exception = nullptr;
  113. for (const auto& staticfunc : _staticFuncs)
  114. {
  115. JSStringRef name = JSStringCreateWithUTF8CString(staticfunc.name);
  116. JSObjectRef func = JSObjectMakeFunctionWithCallback(__cx, nullptr, staticfunc.callAsFunction);
  117. exception = nullptr;
  118. JSObjectSetProperty(__cx, jsCtor, name, func, kJSPropertyAttributeNone, &exception);
  119. if (exception != nullptr)
  120. {
  121. ScriptEngine::getInstance()->_clearException(exception);
  122. }
  123. JSStringRelease(name);
  124. }
  125. JSValueRef prototypeObj = nullptr;
  126. JSStringRef prototypeName = JSStringCreateWithUTF8CString("prototype");
  127. bool exist = JSObjectHasProperty(__cx, jsCtor, prototypeName);
  128. if (exist)
  129. {
  130. exception = nullptr;
  131. prototypeObj = JSObjectGetProperty(__cx, jsCtor, prototypeName, &exception);
  132. if (exception != nullptr)
  133. {
  134. ScriptEngine::getInstance()->_clearException(exception);
  135. }
  136. }
  137. JSStringRelease(prototypeName);
  138. assert(prototypeObj != nullptr);
  139. exception = nullptr;
  140. JSObjectRef protoJSObj = JSValueToObject(__cx, prototypeObj, &exception);
  141. if (exception != nullptr)
  142. {
  143. ScriptEngine::getInstance()->_clearException(exception);
  144. }
  145. //NOTE: I's weird that proto object has a private data which is an invalid adress.
  146. // We have to reset its private data to the max value of unsigned long.
  147. // Therefore, in SE_BIND_FUNC of HelperMacro.h, we could distinguish whether it's a
  148. // proto object. Don't set it to nullptr since static method will get private data
  149. // with nullptr. This line is needed by Web Inspector because it needs to extend
  150. // all global JS values including global proto object. However, proto objects will
  151. // not have a private data which will cause crash while debugging in Safari since
  152. // se::State::nativeThisObject() will return nullptr.
  153. JSObjectSetPrivate(protoJSObj, (void*)std::numeric_limits<unsigned long>::max());
  154. _proto = Object::_createJSObject(this, protoJSObj);
  155. _proto->root();
  156. // reset constructor
  157. _proto->setProperty("constructor", Value(ctorObj));
  158. // Set instance properties
  159. for (const auto& property : _properties)
  160. {
  161. internal::defineProperty(_proto, property.name, property.getter, property.setter);
  162. }
  163. // Set class properties
  164. for (const auto& property : _staticProperties)
  165. {
  166. internal::defineProperty(ctorObj.get(), property.name, property.getter, property.setter);
  167. }
  168. _parent->setProperty(_name.c_str(), Value(ctorObj));
  169. return true;
  170. }
  171. bool Class::defineFunction(const char *name, JSObjectCallAsFunctionCallback func)
  172. {
  173. JSStaticFunction cb = JS_FN(name, func, kJSPropertyAttributeNone);
  174. _funcs.push_back(cb);
  175. return true;
  176. }
  177. bool Class::defineProperty(const char *name, JSObjectCallAsFunctionCallback getter, JSObjectCallAsFunctionCallback setter)
  178. {
  179. JSPropertySpec property = JS_PSGS(name, getter, setter, kJSPropertyAttributeNone);
  180. _properties.push_back(property);
  181. return true;
  182. }
  183. bool Class::defineStaticFunction(const char *name, JSObjectCallAsFunctionCallback func)
  184. {
  185. JSStaticFunction cb = JS_FN(name, func, kJSPropertyAttributeNone);
  186. _staticFuncs.push_back(cb);
  187. return true;
  188. }
  189. bool Class::defineStaticProperty(const char *name, JSObjectCallAsFunctionCallback getter, JSObjectCallAsFunctionCallback setter)
  190. {
  191. JSPropertySpec property = JS_PSGS(name, getter, setter, kJSPropertyAttributeNone);
  192. _staticProperties.push_back(property);
  193. return true;
  194. }
  195. bool Class::defineFinalizeFunction(JSObjectFinalizeCallback func)
  196. {
  197. _finalizeOp = func;
  198. return true;
  199. }
  200. JSObjectRef Class::_createJSObjectWithClass(Class* cls)
  201. {
  202. return JSObjectMake(__cx, cls->_jsCls, nullptr);
  203. }
  204. void Class::setContext(JSContextRef cx)
  205. {
  206. __cx = cx;
  207. }
  208. Object *Class::getProto()
  209. {
  210. return _proto;
  211. }
  212. void Class::destroy()
  213. {
  214. SAFE_DEC_REF(_parent);
  215. SAFE_DEC_REF(_proto);
  216. SAFE_DEC_REF(_parentProto);
  217. JSClassRelease(_jsCls);
  218. }
  219. void Class::cleanup()
  220. {
  221. for (auto cls : __allClasses)
  222. {
  223. cls->destroy();
  224. }
  225. ScriptEngine::getInstance()->addAfterCleanupHook([](){
  226. for (auto cls : __allClasses)
  227. {
  228. delete cls;
  229. }
  230. __allClasses.clear();
  231. });
  232. }
  233. } // namespace se {
  234. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC