HelperMacros.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #pragma once
  22. #include "../config.hpp"
  23. #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC
  24. #ifdef __GNUC__
  25. #define SE_UNUSED __attribute__ ((unused))
  26. #else
  27. #define SE_UNUSED
  28. #endif
  29. #define SAFE_INC_REF(obj) if (obj != nullptr) obj->incRef()
  30. #define SAFE_DEC_REF(obj) if ((obj) != nullptr) { (obj)->decRef(); (obj) = nullptr; }
  31. #define _SE(name) name##Registry
  32. #define SE_DECLARE_FUNC(funcName) \
  33. JSValueRef funcName##Registry(JSContextRef _cx, JSObjectRef _function, JSObjectRef _thisObject, size_t argc, const JSValueRef _argv[], JSValueRef* _exception)
  34. #define SE_BIND_FUNC(funcName) \
  35. JSValueRef funcName##Registry(JSContextRef _cx, JSObjectRef _function, JSObjectRef _thisObject, size_t _argc, const JSValueRef _argv[], JSValueRef* _exception) \
  36. { \
  37. unsigned short argc = (unsigned short) _argc; \
  38. JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
  39. void* nativeThisObject = se::internal::getPrivate(_thisObject); \
  40. if (nativeThisObject != (void*)std::numeric_limits<unsigned long>::max()) \
  41. { \
  42. bool ret = true; \
  43. se::ValueArray args; \
  44. se::internal::jsToSeArgs(_cx, argc, _argv, &args); \
  45. se::State state(nativeThisObject, args); \
  46. ret = funcName(state); \
  47. if (!ret) { \
  48. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  49. } \
  50. se::internal::seToJsValue(_cx, state.rval(), &_jsRet); \
  51. } \
  52. return _jsRet; \
  53. }
  54. #define SE_BIND_FINALIZE_FUNC(funcName) \
  55. void funcName##Registry(JSObjectRef _obj) \
  56. { \
  57. auto se = se::ScriptEngine::getInstance(); \
  58. se->_setGarbageCollecting(true); \
  59. void* nativeThisObject = JSObjectGetPrivate(_obj); \
  60. if (nativeThisObject != nullptr) \
  61. { \
  62. bool ret = false; \
  63. se::State state(nativeThisObject); \
  64. se::Object* _thisObject = state.thisObject(); \
  65. if (_thisObject) _thisObject->_cleanup(nativeThisObject); \
  66. ret = funcName(state); \
  67. if (!ret) { \
  68. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  69. } \
  70. JSObjectSetPrivate(_obj, nullptr); \
  71. SAFE_DEC_REF(_thisObject); \
  72. } \
  73. se->_setGarbageCollecting(false); \
  74. }
  75. #define SE_DECLARE_FINALIZE_FUNC(funcName) \
  76. void funcName##Registry(JSObjectRef _obj);
  77. // NOTE: se::Object::createObjectWithClass(cls) will return a se::Object pointer which is watched by garbage collector.
  78. // If there is a '_ctor' function of current class, '_property.toObject->call(...)' will be invoked which is an operation that may
  79. // make garbage collector to mark the created JS object as a garbage and set it to an invalid state.
  80. // If this happens, crash will be triggered. So please take care of the value returned from se::Object::createObjectWithClass.
  81. // HOW TO FIX: Use a rooted se::Value to save the se::Object poiner returned by se::Object::createObjectWithClass.
  82. #define SE_BIND_CTOR(funcName, cls, finalizeCb) \
  83. JSObjectRef funcName##Registry(JSContextRef _cx, JSObjectRef _constructor, size_t argc, const JSValueRef _argv[], JSValueRef* _exception) \
  84. { \
  85. bool ret = true; \
  86. se::ValueArray args; \
  87. se::internal::jsToSeArgs(_cx, argc, _argv, &args); \
  88. se::Value thisVal(se::Object::createObjectWithClass(cls), true); \
  89. se::Object* thisObject = thisVal.toObject(); \
  90. JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
  91. se::State state(thisObject, args); \
  92. ret = funcName(state); \
  93. if (ret) \
  94. { \
  95. _jsRet = thisObject->_getJSObject(); \
  96. se::Value _property; \
  97. bool _found = false; \
  98. _found = thisObject->getProperty("_ctor", &_property); \
  99. if (_found) _property.toObject()->call(args, thisObject); \
  100. } \
  101. else \
  102. { \
  103. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  104. } \
  105. return JSValueToObject(_cx, _jsRet, nullptr); \
  106. }
  107. #define SE_BIND_SUB_CLS_CTOR(funcName, cls, finalizeCb) \
  108. JSValueRef funcName##Registry(JSContextRef _cx, JSObjectRef _function, JSObjectRef _thisObject, size_t argc, const JSValueRef _argv[], JSValueRef* _exception) \
  109. { \
  110. bool ret = true; \
  111. JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
  112. se::ValueArray args; \
  113. se::internal::jsToSeArgs(_cx, argc, _argv, &args); \
  114. se::Object* thisObject = se::Object::_createJSObject(cls, _thisObject); \
  115. thisObject->_setFinalizeCallback(_SE(finalizeCb)); \
  116. se::State state(thisObject, args); \
  117. ret = funcName(state); \
  118. if (ret) \
  119. { \
  120. se::Value _property; \
  121. bool _found = false; \
  122. _found = thisObject->getProperty("_ctor", &_property); \
  123. if (_found) _property.toObject()->call(args, thisObject); \
  124. } \
  125. else \
  126. { \
  127. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  128. } \
  129. return _jsRet; \
  130. }
  131. #define SE_BIND_PROP_GET(funcName) \
  132. JSValueRef funcName##Registry(JSContextRef _cx, JSObjectRef _function, JSObjectRef _thisObject, size_t argc, const JSValueRef _argv[], JSValueRef* _exception) \
  133. { \
  134. assert(argc == 0); \
  135. JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
  136. void* nativeThisObject = se::internal::getPrivate(_thisObject); \
  137. if (nativeThisObject != (void*)std::numeric_limits<unsigned long>::max()) \
  138. { \
  139. se::State state(nativeThisObject); \
  140. if (funcName(state)) \
  141. { \
  142. se::internal::seToJsValue(_cx, state.rval(), &_jsRet); \
  143. } \
  144. else \
  145. { \
  146. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  147. } \
  148. } \
  149. return _jsRet; \
  150. }
  151. #define SE_BIND_PROP_SET(funcName) \
  152. JSValueRef funcName##Registry(JSContextRef _cx, JSObjectRef _function, JSObjectRef _thisObject, size_t argc, const JSValueRef _argv[], JSValueRef* _exception) \
  153. { \
  154. assert(argc == 1); \
  155. JSValueRef _jsRet = JSValueMakeUndefined(_cx); \
  156. void* nativeThisObject = se::internal::getPrivate(_thisObject); \
  157. if (nativeThisObject != (void*)std::numeric_limits<unsigned long>::max()) \
  158. { \
  159. bool ret = true; \
  160. se::Value data; \
  161. se::internal::jsToSeValue(_cx, _argv[0], &data); \
  162. se::ValueArray args; \
  163. args.push_back(std::move(data)); \
  164. se::State state(nativeThisObject, args); \
  165. ret = funcName(state); \
  166. if (!ret) { \
  167. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  168. } \
  169. } \
  170. return _jsRet; \
  171. }
  172. #define SE_TYPE_NAME(t) typeid(t).name()
  173. #define SE_QUOTEME_(x) #x
  174. #define SE_QUOTEME(x) SE_QUOTEME_(x)
  175. //IDEA: implement this macro
  176. #define SE_REPORT_ERROR(fmt, ...) SE_LOGE("[ERROR] (" __FILE__ ", " SE_QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
  177. #if COCOS2D_DEBUG > 0
  178. #define SE_ASSERT(cond, fmt, ...) \
  179. do \
  180. { \
  181. if (!(cond)) \
  182. { \
  183. SE_LOGE("ASSERT (" __FILE__ ", " SE_QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__); \
  184. assert(false); \
  185. } \
  186. } while(false)
  187. #else
  188. #define SE_ASSERT(cond, fmt, ...)
  189. #endif // #if COCOS2D_DEBUG > 0
  190. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC