HelperMacros.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_V8
  24. extern uint32_t __jsbInvocationCount;
  25. #ifdef __GNUC__
  26. #define SE_UNUSED __attribute__ ((unused))
  27. #else
  28. #define SE_UNUSED
  29. #endif
  30. #define SAFE_INC_REF(obj) if (obj != nullptr) obj->incRef()
  31. #define SAFE_DEC_REF(obj) if ((obj) != nullptr) { (obj)->decRef(); (obj) = nullptr; }
  32. #define _SE(name) name##Registry
  33. #define SE_DECLARE_FUNC(funcName) \
  34. void funcName##Registry(const v8::FunctionCallbackInfo<v8::Value>& v8args)
  35. #define SE_BIND_FUNC(funcName) \
  36. void funcName##Registry(const v8::FunctionCallbackInfo<v8::Value>& _v8args) \
  37. { \
  38. ++__jsbInvocationCount; \
  39. bool ret = false; \
  40. v8::Isolate* _isolate = _v8args.GetIsolate(); \
  41. v8::HandleScope _hs(_isolate); \
  42. SE_UNUSED unsigned argc = (unsigned)_v8args.Length(); \
  43. se::ValueArray args; \
  44. args.reserve(10); \
  45. se::internal::jsToSeArgs(_v8args, &args); \
  46. void* nativeThisObject = se::internal::getPrivate(_isolate, _v8args.This()); \
  47. se::State state(nativeThisObject, args); \
  48. ret = funcName(state); \
  49. if (!ret) { \
  50. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  51. } \
  52. se::internal::setReturnValue(state.rval(), _v8args); \
  53. }
  54. #define SE_BIND_FINALIZE_FUNC(funcName) \
  55. void funcName##Registry(void* nativeThisObject) \
  56. { \
  57. if (nativeThisObject == nullptr) \
  58. return; \
  59. auto se = se::ScriptEngine::getInstance(); \
  60. se->_setGarbageCollecting(true); \
  61. se::State state(nativeThisObject); \
  62. bool ret = funcName(state); \
  63. if (!ret) { \
  64. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  65. } \
  66. se->_setGarbageCollecting(false); \
  67. }
  68. #define SE_DECLARE_FINALIZE_FUNC(funcName) \
  69. void funcName##Registry(void* nativeThisObject);
  70. // v8 doesn't need to create a new JSObject in SE_BIND_CTOR while SpiderMonkey needs.
  71. #define SE_BIND_CTOR(funcName, cls, finalizeCb) \
  72. void funcName##Registry(const v8::FunctionCallbackInfo<v8::Value>& _v8args) \
  73. { \
  74. ++__jsbInvocationCount; \
  75. v8::Isolate* _isolate = _v8args.GetIsolate(); \
  76. v8::HandleScope _hs(_isolate); \
  77. bool ret = true; \
  78. se::ValueArray args; \
  79. args.reserve(10); \
  80. se::internal::jsToSeArgs(_v8args, &args); \
  81. se::Object* thisObject = se::Object::_createJSObject(cls, _v8args.This()); \
  82. thisObject->_setFinalizeCallback(_SE(finalizeCb)); \
  83. se::State state(thisObject, args); \
  84. ret = funcName(state); \
  85. if (!ret) { \
  86. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  87. } \
  88. se::Value _property; \
  89. bool _found = false; \
  90. _found = thisObject->getProperty("_ctor", &_property); \
  91. if (_found) _property.toObject()->call(args, thisObject); \
  92. }
  93. #define SE_BIND_SUB_CLS_CTOR SE_BIND_CTOR
  94. #define SE_BIND_PROP_GET(funcName) \
  95. void funcName##Registry(v8::Local<v8::Name> _property, const v8::PropertyCallbackInfo<v8::Value>& _v8args) \
  96. { \
  97. ++__jsbInvocationCount; \
  98. v8::Isolate* _isolate = _v8args.GetIsolate(); \
  99. v8::HandleScope _hs(_isolate); \
  100. bool ret = true; \
  101. void* nativeThisObject = se::internal::getPrivate(_isolate, _v8args.This()); \
  102. se::State state(nativeThisObject); \
  103. ret = funcName(state); \
  104. if (!ret) { \
  105. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  106. } \
  107. se::internal::setReturnValue(state.rval(), _v8args); \
  108. }
  109. #define SE_BIND_PROP_SET(funcName) \
  110. void funcName##Registry(v8::Local<v8::Name> _property, v8::Local<v8::Value> _value, const v8::PropertyCallbackInfo<void>& _v8args) \
  111. { \
  112. ++__jsbInvocationCount; \
  113. v8::Isolate* _isolate = _v8args.GetIsolate(); \
  114. v8::HandleScope _hs(_isolate); \
  115. bool ret = true; \
  116. void* nativeThisObject = se::internal::getPrivate(_isolate, _v8args.This()); \
  117. se::Value data; \
  118. se::internal::jsToSeValue(_isolate, _value, &data); \
  119. se::ValueArray args; \
  120. args.reserve(10); \
  121. args.push_back(std::move(data)); \
  122. se::State state(nativeThisObject, args); \
  123. ret = funcName(state); \
  124. if (!ret) { \
  125. SE_LOGE("[ERROR] Failed to invoke %s, location: %s:%d\n", #funcName, __FILE__, __LINE__); \
  126. } \
  127. }
  128. #define SE_TYPE_NAME(t) typeid(t).name()
  129. #define SE_QUOTEME_(x) #x
  130. #define SE_QUOTEME(x) SE_QUOTEME_(x)
  131. //IDEA: implement this macro
  132. #define SE_REPORT_ERROR(fmt, ...) SE_LOGE("[ERROR] (" __FILE__ ", " SE_QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__)
  133. #if COCOS2D_DEBUG > 0
  134. #define SE_ASSERT(cond, fmt, ...) \
  135. do \
  136. { \
  137. if (!(cond)) \
  138. { \
  139. SE_LOGE("ASSERT (" __FILE__ ", " SE_QUOTEME(__LINE__) "): " fmt "\n", ##__VA_ARGS__); \
  140. assert(false); \
  141. } \
  142. } while(false)
  143. #else
  144. #define SE_ASSERT(cond, fmt, ...)
  145. #endif // #if COCOS2D_DEBUG > 0
  146. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8