Class.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_V8
  23. #include "Object.hpp"
  24. #include "Utils.hpp"
  25. #include "ScriptEngine.hpp"
  26. namespace se {
  27. // ------------------------------------------------------- Object
  28. namespace {
  29. // std::unordered_map<std::string, Class *> __clsMap;
  30. v8::Isolate* __isolate = nullptr;
  31. std::vector<Class*> __allClasses;
  32. }
  33. Class::Class()
  34. : _parent(nullptr)
  35. , _parentProto(nullptr)
  36. , _proto(nullptr)
  37. , _ctor(nullptr)
  38. , _finalizeFunc(nullptr)
  39. , _createProto(true)
  40. {
  41. __allClasses.push_back(this);
  42. }
  43. Class::~Class()
  44. {
  45. }
  46. /* static */
  47. Class* Class::create(const std::string& clsName, se::Object* parent, Object* parentProto, v8::FunctionCallback ctor)
  48. {
  49. Class* cls = new Class();
  50. if (cls != nullptr && !cls->init(clsName, parent, parentProto, ctor))
  51. {
  52. delete cls;
  53. cls = nullptr;
  54. }
  55. return cls;
  56. }
  57. bool Class::init(const std::string& clsName, Object* parent, Object* parentProto, v8::FunctionCallback ctor)
  58. {
  59. _name = clsName;
  60. _parent = parent;
  61. if (_parent != nullptr)
  62. _parent->incRef();
  63. _parentProto = parentProto;
  64. if (_parentProto != nullptr)
  65. _parentProto->incRef();
  66. _ctor = ctor;
  67. _ctorTemplate.Reset(__isolate, v8::FunctionTemplate::New(__isolate, _ctor));
  68. v8::MaybeLocal<v8::String> jsNameVal = v8::String::NewFromUtf8(__isolate, _name.c_str(), v8::NewStringType::kNormal);
  69. if (jsNameVal.IsEmpty())
  70. return false;
  71. _ctorTemplate.Get(__isolate)->SetClassName(jsNameVal.ToLocalChecked());
  72. _ctorTemplate.Get(__isolate)->InstanceTemplate()->SetInternalFieldCount(1);
  73. return true;
  74. }
  75. void Class::destroy()
  76. {
  77. SAFE_DEC_REF(_parent);
  78. SAFE_DEC_REF(_proto);
  79. SAFE_DEC_REF(_parentProto);
  80. _ctorTemplate.Reset();
  81. }
  82. void Class::cleanup()
  83. {
  84. for (auto cls : __allClasses)
  85. {
  86. cls->destroy();
  87. }
  88. se::ScriptEngine::getInstance()->addAfterCleanupHook([](){
  89. for (auto cls : __allClasses)
  90. {
  91. delete cls;
  92. }
  93. __allClasses.clear();
  94. });
  95. }
  96. void Class::setCreateProto(bool createProto)
  97. {
  98. _createProto = createProto;
  99. }
  100. bool Class::install()
  101. {
  102. // assert(__clsMap.find(_name) == __clsMap.end());
  103. //
  104. // __clsMap.emplace(_name, this);
  105. if (_parentProto != nullptr)
  106. {
  107. _ctorTemplate.Get(__isolate)->Inherit(_parentProto->_getClass()->_ctorTemplate.Get(__isolate));
  108. }
  109. v8::Local<v8::Context> context = __isolate->GetCurrentContext();
  110. v8::MaybeLocal<v8::Function> ctor = _ctorTemplate.Get(__isolate)->GetFunction(context);
  111. if (ctor.IsEmpty())
  112. return false;
  113. v8::Local<v8::Function> ctorChecked = ctor.ToLocalChecked();
  114. v8::MaybeLocal<v8::String> name = v8::String::NewFromUtf8(__isolate, _name.c_str(), v8::NewStringType::kNormal);
  115. if (name.IsEmpty())
  116. return false;
  117. v8::Maybe<bool> result = _parent->_getJSObject()->Set(context, name.ToLocalChecked(), ctorChecked);
  118. if (result.IsNothing())
  119. return false;
  120. v8::MaybeLocal<v8::String> prototypeName = v8::String::NewFromUtf8(__isolate, "prototype", v8::NewStringType::kNormal);
  121. if (prototypeName.IsEmpty())
  122. return false;
  123. v8::MaybeLocal<v8::Value> prototypeObj = ctorChecked->Get(context, prototypeName.ToLocalChecked());
  124. if (prototypeObj.IsEmpty())
  125. return false;
  126. if (_createProto)
  127. {
  128. // Proto object is released in Class::destroy.
  129. _proto = Object::_createJSObject(this, v8::Local<v8::Object>::Cast(prototypeObj.ToLocalChecked()));
  130. _proto->root();
  131. }
  132. return true;
  133. }
  134. bool Class::defineFunction(const char *name, v8::FunctionCallback func)
  135. {
  136. v8::MaybeLocal<v8::String> jsName = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
  137. if (jsName.IsEmpty())
  138. return false;
  139. _ctorTemplate.Get(__isolate)->PrototypeTemplate()->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func));
  140. return true;
  141. }
  142. bool Class::defineProperty(const char *name, v8::AccessorNameGetterCallback getter, v8::AccessorNameSetterCallback setter)
  143. {
  144. v8::MaybeLocal<v8::String> jsName = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
  145. if (jsName.IsEmpty())
  146. return false;
  147. _ctorTemplate.Get(__isolate)->PrototypeTemplate()->SetAccessor(jsName.ToLocalChecked(), getter, setter);
  148. return true;
  149. }
  150. bool Class::defineStaticFunction(const char *name, v8::FunctionCallback func)
  151. {
  152. v8::MaybeLocal<v8::String> jsName = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
  153. if (jsName.IsEmpty())
  154. return false;
  155. _ctorTemplate.Get(__isolate)->Set(jsName.ToLocalChecked(), v8::FunctionTemplate::New(__isolate, func));
  156. return true;
  157. }
  158. bool Class::defineStaticProperty(const char *name, v8::AccessorNameGetterCallback getter, v8::AccessorNameSetterCallback setter)
  159. {
  160. v8::MaybeLocal<v8::String> jsName = v8::String::NewFromUtf8(__isolate, name, v8::NewStringType::kNormal);
  161. if (jsName.IsEmpty())
  162. return false;
  163. _ctorTemplate.Get(__isolate)->SetNativeDataProperty(jsName.ToLocalChecked(), getter, setter);
  164. return true;
  165. }
  166. bool Class::defineFinalizeFunction(V8FinalizeFunc finalizeFunc)
  167. {
  168. assert(finalizeFunc != nullptr);
  169. _finalizeFunc = finalizeFunc;
  170. return true;
  171. }
  172. // v8::Local<v8::Object> Class::_createJSObject(const std::string &clsName, Class** outCls)
  173. // {
  174. // auto iter = __clsMap.find(clsName);
  175. // if (iter == __clsMap.end())
  176. // {
  177. // *outCls = nullptr;
  178. // return v8::Local<v8::Object>::Cast(v8::Undefined(__isolate));
  179. // }
  180. //
  181. // *outCls = iter->second;
  182. // return _createJSObjectWithClass(iter->second);
  183. // }
  184. v8::Local<v8::Object> Class::_createJSObjectWithClass(Class* cls)
  185. {
  186. v8::MaybeLocal<v8::Object> ret = cls->_ctorTemplate.Get(__isolate)->InstanceTemplate()->NewInstance(__isolate->GetCurrentContext());
  187. assert(!ret.IsEmpty());
  188. return ret.ToLocalChecked();
  189. }
  190. Object* Class::getProto() const
  191. {
  192. return _proto;
  193. }
  194. V8FinalizeFunc Class::_getFinalizeFunction() const
  195. {
  196. return _finalizeFunc;
  197. }
  198. /* static */
  199. void Class::setIsolate(v8::Isolate* isolate)
  200. {
  201. __isolate = isolate;
  202. }
  203. } // namespace se {
  204. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8