Utils.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "Utils.hpp"
  22. #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC
  23. #include "Object.hpp"
  24. #include "ScriptEngine.hpp"
  25. #include "../HandleObject.hpp"
  26. namespace se {
  27. namespace internal {
  28. namespace {
  29. JSContextRef __cx = nullptr;
  30. }
  31. void setContext(JSContextRef cx)
  32. {
  33. __cx = cx;
  34. }
  35. bool defineProperty(Object* obj, const char* name, JSObjectCallAsFunctionCallback jsGetter, JSObjectCallAsFunctionCallback jsSetter)
  36. {
  37. bool ok = true;
  38. Object* globalObject = ScriptEngine::getInstance()->getGlobalObject();
  39. Value v;
  40. ok = globalObject->getProperty("Object", &v);
  41. if (!ok || !v.isObject())
  42. {
  43. SE_LOGD("ERROR: couldn't find Object\n");
  44. return false;
  45. }
  46. Value definePropertyFunc;
  47. ok = v.toObject()->getProperty("defineProperty", &definePropertyFunc);
  48. if (!ok || !v.isObject())
  49. {
  50. SE_LOGD("ERROR: couldn't find Object.defineProperty\n");
  51. return false;
  52. }
  53. ValueArray args;
  54. args.reserve(3);
  55. args.push_back(Value(obj));
  56. args.push_back(Value(name));
  57. HandleObject desc(Object::createPlainObject());
  58. JSObjectRef getter = nullptr;
  59. if (jsGetter != nullptr)
  60. {
  61. getter = JSObjectMakeFunctionWithCallback(__cx, nullptr, jsGetter);
  62. }
  63. JSObjectRef setter = nullptr;
  64. if (jsSetter != nullptr)
  65. {
  66. setter = JSObjectMakeFunctionWithCallback(__cx, nullptr, jsSetter);
  67. }
  68. assert(getter != nullptr || setter != nullptr);
  69. if (getter != nullptr)
  70. {
  71. HandleObject getterObj(Object::_createJSObject(nullptr, getter));
  72. desc->setProperty("get", Value(getterObj));
  73. }
  74. if (setter != nullptr)
  75. {
  76. HandleObject setterObj(Object::_createJSObject(nullptr, setter));
  77. desc->setProperty("set", Value(setterObj));
  78. }
  79. args.push_back(Value(desc));
  80. definePropertyFunc.toObject()->call(args, nullptr);
  81. return true;
  82. }
  83. void jsToSeArgs(JSContextRef cx, unsigned short argc, const JSValueRef* argv, ValueArray* outArr)
  84. {
  85. outArr->reserve(argc);
  86. for (unsigned short i = 0; i < argc; ++i)
  87. {
  88. Value v;
  89. jsToSeValue(cx, argv[i], &v);
  90. outArr->push_back(v);
  91. }
  92. }
  93. void seToJsArgs(JSContextRef cx, const ValueArray& args, JSValueRef* outArr)
  94. {
  95. for (size_t i = 0, len = args.size(); i < len; ++i)
  96. {
  97. seToJsValue(cx, args[i], &outArr[i]);
  98. }
  99. }
  100. void jsToSeValue(JSContextRef cx, JSValueRef jsValue, Value* data)
  101. {
  102. if (JSValueIsNull(cx, jsValue))
  103. {
  104. data->setNull();
  105. }
  106. else if (JSValueIsUndefined(cx, jsValue))
  107. {
  108. data->setUndefined();
  109. }
  110. else if (JSValueIsNumber(cx, jsValue))
  111. {
  112. JSValueRef exception = nullptr;
  113. double number = JSValueToNumber(cx, jsValue, &exception);
  114. if (exception != nullptr)
  115. {
  116. ScriptEngine::getInstance()->_clearException(exception);
  117. }
  118. else
  119. {
  120. data->setNumber(number);
  121. }
  122. }
  123. else if (JSValueIsBoolean(cx, jsValue))
  124. {
  125. data->setBoolean(JSValueToBoolean(cx, jsValue));
  126. }
  127. else if (JSValueIsString(cx, jsValue))
  128. {
  129. std::string str;
  130. forceConvertJsValueToStdString(cx, jsValue, &str);
  131. data->setString(str);
  132. }
  133. else if (JSValueIsObject(cx, jsValue))
  134. {
  135. JSValueRef exception = nullptr;
  136. JSObjectRef jsobj = JSValueToObject(cx, jsValue, &exception);
  137. if (exception != nullptr)
  138. {
  139. ScriptEngine::getInstance()->_clearException(exception);
  140. }
  141. else
  142. {
  143. void* nativePtr = internal::getPrivate(jsobj);
  144. Object* obj = nullptr;
  145. if (nativePtr != nullptr)
  146. {
  147. obj = Object::getObjectWithPtr(nativePtr);
  148. }
  149. if (obj == nullptr)
  150. {
  151. obj = Object::_createJSObject(nullptr, jsobj);
  152. }
  153. data->setObject(obj, true);
  154. obj->decRef();
  155. }
  156. }
  157. else
  158. {
  159. assert(false);
  160. }
  161. }
  162. void seToJsValue(JSContextRef cx, const Value& v, JSValueRef* jsval)
  163. {
  164. switch (v.getType()) {
  165. case Value::Type::Null:
  166. *jsval = JSValueMakeNull(cx);
  167. break;
  168. case Value::Type::Number:
  169. *jsval = JSValueMakeNumber(cx, v.toNumber());
  170. break;
  171. case Value::Type::String:
  172. {
  173. JSStringRef str = JSStringCreateWithUTF8CString(v.toString().c_str());
  174. *jsval = JSValueMakeString(cx, str);
  175. JSStringRelease(str);
  176. }
  177. break;
  178. case Value::Type::Boolean:
  179. *jsval = JSValueMakeBoolean(cx, v.toBoolean());
  180. break;
  181. case Value::Type::Object:
  182. *jsval = v.toObject()->_getJSObject();
  183. break;
  184. default: // Undefined
  185. *jsval = JSValueMakeUndefined(cx);
  186. break;
  187. }
  188. }
  189. void forceConvertJsValueToStdString(JSContextRef cx, JSValueRef jsval, std::string* ret, bool ignoreException/* = false */)
  190. {
  191. JSValueRef exception = nullptr;
  192. JSStringRef jsstr = JSValueToStringCopy(cx, jsval, &exception);
  193. if (exception != nullptr)
  194. {
  195. // NOTE: ignoreException is true in ScriptEngine::_formatException because it's already in the _clearException process.
  196. // Adds this flag to avoid infinite loop of _clearException -> _formatException -> forceConvertJsValueToStdString -> _clearException -> ...
  197. if (!ignoreException)
  198. {
  199. ScriptEngine::getInstance()->_clearException(exception);
  200. }
  201. ret->clear();
  202. }
  203. else
  204. {
  205. jsStringToStdString(cx, jsstr, ret);
  206. }
  207. if (jsstr != nullptr)
  208. JSStringRelease(jsstr);
  209. }
  210. void jsStringToStdString(JSContextRef cx, JSStringRef jsStr, std::string* ret)
  211. {
  212. size_t len = JSStringGetLength(jsStr);
  213. const size_t BUF_SIZE = len * 4 + 1;
  214. char* buf = (char*)malloc(BUF_SIZE);
  215. memset(buf, 0, BUF_SIZE);
  216. JSStringGetUTF8CString(jsStr, buf, BUF_SIZE);
  217. *ret = buf;
  218. free(buf);
  219. }
  220. const char* KEY_PRIVATE_DATA = "__cc_private_data";
  221. bool hasPrivate(JSObjectRef obj)
  222. {
  223. void* data = JSObjectGetPrivate(obj);
  224. if (data != nullptr)
  225. return true;
  226. JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA);
  227. bool found = JSObjectHasProperty(__cx, obj, key);
  228. JSStringRelease(key);
  229. return found;
  230. }
  231. void setPrivate(JSObjectRef obj, void* data, JSObjectFinalizeCallback finalizeCb)
  232. {
  233. bool ok = JSObjectSetPrivate(obj, data);
  234. if (ok)
  235. {
  236. return;
  237. }
  238. assert(finalizeCb);
  239. Object* privateObj = Object::createObjectWithClass(__jsb_CCPrivateData_class);
  240. privateObj->root();
  241. internal::PrivateData* privateData = (internal::PrivateData*)malloc(sizeof(internal::PrivateData));
  242. privateData->data = data;
  243. privateData->finalizeCb = finalizeCb;
  244. ok = JSObjectSetPrivate(privateObj->_getJSObject(), privateData);
  245. assert(ok);
  246. JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA);
  247. JSValueRef exception = nullptr;
  248. JSObjectSetProperty(__cx, obj, key, privateObj->_getJSObject(), kJSPropertyAttributeDontEnum, &exception);
  249. if (exception != nullptr)
  250. {
  251. ScriptEngine::getInstance()->_clearException(exception);
  252. }
  253. JSStringRelease(key);
  254. privateObj->unroot();
  255. privateObj->decRef();
  256. }
  257. void* getPrivate(JSObjectRef obj)
  258. {
  259. void* data = JSObjectGetPrivate(obj);
  260. if (data != nullptr)
  261. return data;
  262. JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA);
  263. bool found = JSObjectHasProperty(__cx, obj, key);
  264. if (found)
  265. {
  266. JSValueRef exception = nullptr;
  267. JSValueRef privateDataVal = JSObjectGetProperty(__cx, obj, key, &exception);
  268. do
  269. {
  270. if (exception != nullptr)
  271. break;
  272. JSObjectRef jsobj = JSValueToObject(__cx, privateDataVal, &exception);
  273. if (exception != nullptr)
  274. break;
  275. internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(jsobj);
  276. assert(privateData != nullptr);
  277. data = privateData->data;
  278. } while(false);
  279. if (exception != nullptr)
  280. {
  281. ScriptEngine::getInstance()->_clearException(exception);
  282. }
  283. }
  284. JSStringRelease(key);
  285. return data;
  286. }
  287. void clearPrivate(JSObjectRef obj)
  288. {
  289. void* data = JSObjectGetPrivate(obj);
  290. if (data != nullptr)
  291. {
  292. JSObjectSetPrivate(obj, nullptr);
  293. }
  294. else
  295. {
  296. JSStringRef key = JSStringCreateWithUTF8CString(KEY_PRIVATE_DATA); //IDEA: cache the key string
  297. if (JSObjectHasProperty(__cx, obj, key))
  298. {
  299. JSValueRef exception = nullptr;
  300. do
  301. {
  302. JSValueRef value = JSObjectGetProperty(__cx, obj, key, &exception);
  303. if (exception != nullptr)
  304. break;
  305. JSObjectRef propertyObj = JSValueToObject(__cx, value, &exception);
  306. if (exception != nullptr)
  307. break;
  308. internal::PrivateData* privateData = (internal::PrivateData*)JSObjectGetPrivate(propertyObj);
  309. free(privateData);
  310. JSObjectSetPrivate(propertyObj, nullptr);
  311. bool ok = JSObjectDeleteProperty(__cx, obj, key, nullptr);
  312. assert(ok);
  313. } while (false);
  314. if (exception != nullptr)
  315. {
  316. ScriptEngine::getInstance()->_clearException(exception);
  317. }
  318. }
  319. JSStringRelease(key);
  320. }
  321. }
  322. }} // namespace se { namespace internal {
  323. #endif // #if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_JSC