/scripting/javascript/bindings/cocos2d_specifics.cpp

https://bitbucket.org/Tsiannian/cocos2d-x
C++ | 685 lines | 609 code | 66 blank | 10 comment | 114 complexity | d62f6c49226b8a8bf0a21883f713831b MD5 | raw file
  1. #include "cocos2d.h"
  2. #include "cocos2d_specifics.hpp"
  3. #include <typeinfo>
  4. void JSTouchDelegate::setJSObject(JSObject *obj) {
  5. _mObj = obj;
  6. }
  7. void JSTouchDelegate::registerStandardDelegate() {
  8. CCDirector* pDirector = CCDirector::sharedDirector();
  9. pDirector->getTouchDispatcher()->addStandardDelegate(this,0);
  10. }
  11. void JSTouchDelegate::registerTargettedDelegate(int priority, bool swallowsTouches) {
  12. CCDirector* pDirector = CCDirector::sharedDirector();
  13. pDirector->getTouchDispatcher()->addTargetedDelegate(this,
  14. priority,
  15. swallowsTouches);
  16. }
  17. static void addCallBackAndThis(JSObject *obj, jsval callback, jsval &thisObj) {
  18. if(callback != JSVAL_VOID) {
  19. ScriptingCore::getInstance()->setReservedSpot(0, obj, callback);
  20. }
  21. if(thisObj != JSVAL_VOID) {
  22. ScriptingCore::getInstance()->setReservedSpot(1, obj, thisObj);
  23. }
  24. }
  25. template<class T>
  26. JSObject* bind_menu_item(JSContext *cx, T* nativeObj, jsval callback, jsval thisObj) {
  27. js_proxy_t *p;
  28. JS_GET_PROXY(p, nativeObj);
  29. if (p) {
  30. addCallBackAndThis(p->obj, callback, thisObj);
  31. return p->obj;
  32. } else {
  33. js_type_class_t *classType = js_get_type_from_native<T>(nativeObj);
  34. assert(classType);
  35. JSObject *tmp = JS_NewObject(cx, classType->jsclass, classType->proto, classType->parentProto);
  36. // bind nativeObj <-> JSObject
  37. js_proxy_t *proxy;
  38. JS_NEW_PROXY(proxy, nativeObj, tmp);
  39. JS_AddNamedObjectRoot(cx, &proxy->obj, "MenuItem");
  40. addCallBackAndThis(tmp, callback, thisObj);
  41. return tmp;
  42. }
  43. }
  44. JSBool js_cocos2dx_CCNode_getChildren(JSContext *cx, uint32_t argc, jsval *vp)
  45. {
  46. JSObject *thisObj = JS_THIS_OBJECT(cx, vp);
  47. if (thisObj) {
  48. js_proxy_t *proxy;
  49. JS_GET_NATIVE_PROXY(proxy, thisObj);
  50. if (proxy) {
  51. cocos2d::CCNode *node = (cocos2d::CCNode *)(proxy->ptr ? proxy->ptr : NULL);
  52. cocos2d::CCArray *children = node->getChildren();
  53. JSObject *jsarr = JS_NewArrayObject(cx, children->count(), NULL);
  54. for (int i=0; i < children->count(); i++) {
  55. cocos2d::CCNode *child = (cocos2d::CCNode*)children->objectAtIndex(i);
  56. js_proxy_t *childProxy = js_get_or_create_proxy<cocos2d::CCNode>(cx, child);
  57. jsval childVal = OBJECT_TO_JSVAL(childProxy->obj);
  58. JS_SetElement(cx, jsarr, i, &childVal);
  59. }
  60. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsarr));
  61. }
  62. return JS_TRUE;
  63. }
  64. return JS_FALSE;
  65. }
  66. JSBool js_cocos2dx_CCMenu_create(JSContext *cx, uint32_t argc, jsval *vp)
  67. {
  68. jsval *argv = JS_ARGV(cx, vp);
  69. if (argc > 0) {
  70. cocos2d::CCArray* array = cocos2d::CCArray::create();
  71. int i = 0;
  72. while (i < argc) {
  73. js_proxy_t *proxy;
  74. JSObject *tmpObj = JSVAL_TO_OBJECT(argv[i]);
  75. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  76. cocos2d::CCObject *item = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
  77. TEST_NATIVE_OBJECT(cx, item)
  78. array->addObject(item);
  79. i++;
  80. }
  81. cocos2d::CCMenu* ret = cocos2d::CCMenu::createWithArray(array);
  82. jsval jsret;
  83. do {
  84. if (ret) {
  85. js_proxy_t *p;
  86. JS_GET_PROXY(p, ret);
  87. if (p) {
  88. jsret = OBJECT_TO_JSVAL(p->obj);
  89. } else {
  90. // create a new js obj of that class
  91. js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCMenu>(cx, ret);
  92. jsret = OBJECT_TO_JSVAL(proxy->obj);
  93. }
  94. } else {
  95. jsret = JSVAL_NULL;
  96. }
  97. } while (0);
  98. JS_SET_RVAL(cx, vp, jsret);
  99. return JS_TRUE;
  100. }
  101. if (argc == 0) {
  102. cocos2d::CCMenu* ret = cocos2d::CCMenu::create();
  103. jsval jsret;
  104. do {
  105. if (ret) {
  106. js_proxy_t *p;
  107. JS_GET_PROXY(p, ret);
  108. if (p) {
  109. jsret = OBJECT_TO_JSVAL(p->obj);
  110. } else {
  111. // create a new js obj of that class
  112. js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCMenu>(cx, ret);
  113. jsret = OBJECT_TO_JSVAL(proxy->obj);
  114. }
  115. } else {
  116. jsret = JSVAL_NULL;
  117. }
  118. } while (0);
  119. JS_SET_RVAL(cx, vp, jsret);
  120. return JS_TRUE;
  121. }
  122. return JS_FALSE;
  123. }
  124. JSBool js_cocos2dx_CCSequence_create(JSContext *cx, uint32_t argc, jsval *vp)
  125. {
  126. jsval *argv = JS_ARGV(cx, vp);
  127. if (argc > 0) {
  128. cocos2d::CCArray* array = cocos2d::CCArray::create();
  129. int i = 0;
  130. while (i < argc) {
  131. js_proxy_t *proxy;
  132. JSObject *tmpObj = JSVAL_TO_OBJECT(argv[i]);
  133. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  134. cocos2d::CCObject *item = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
  135. TEST_NATIVE_OBJECT(cx, item)
  136. array->addObject(item);
  137. i++;
  138. }
  139. cocos2d::CCFiniteTimeAction* ret = cocos2d::CCSequence::create(array);
  140. jsval jsret;
  141. do {
  142. if (ret) {
  143. js_proxy_t *p;
  144. JS_GET_PROXY(p, ret);
  145. if (p) {
  146. jsret = OBJECT_TO_JSVAL(p->obj);
  147. } else {
  148. // create a new js obj of that class
  149. js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCFiniteTimeAction>(cx, ret);
  150. jsret = OBJECT_TO_JSVAL(proxy->obj);
  151. }
  152. } else {
  153. jsret = JSVAL_NULL;
  154. }
  155. } while (0);
  156. JS_SET_RVAL(cx, vp, jsret);
  157. return JS_TRUE;
  158. }
  159. return JS_FALSE;
  160. }
  161. JSBool js_cocos2dx_CCSpawn_create(JSContext *cx, uint32_t argc, jsval *vp)
  162. {
  163. jsval *argv = JS_ARGV(cx, vp);
  164. if (argc > 0) {
  165. cocos2d::CCArray* array = cocos2d::CCArray::create();
  166. int i = 0;
  167. while (i < argc) {
  168. js_proxy_t *proxy;
  169. JSObject *tmpObj = JSVAL_TO_OBJECT(argv[i]);
  170. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  171. cocos2d::CCObject *item = (cocos2d::CCObject*)(proxy ? proxy->ptr : NULL);
  172. TEST_NATIVE_OBJECT(cx, item)
  173. array->addObject(item);
  174. i++;
  175. }
  176. cocos2d::CCFiniteTimeAction* ret = cocos2d::CCSpawn::create(array);
  177. jsval jsret;
  178. do {
  179. if (ret) {
  180. js_proxy_t *p;
  181. JS_GET_PROXY(p, ret);
  182. if (p) {
  183. jsret = OBJECT_TO_JSVAL(p->obj);
  184. } else {
  185. // create a new js obj of that class
  186. js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCFiniteTimeAction>(cx, ret);
  187. jsret = OBJECT_TO_JSVAL(proxy->obj);
  188. }
  189. } else {
  190. jsret = JSVAL_NULL;
  191. }
  192. } while (0);
  193. JS_SET_RVAL(cx, vp, jsret);
  194. return JS_TRUE;
  195. }
  196. return JS_FALSE;
  197. }
  198. JSBool js_cocos2dx_CCMenuItem_create(JSContext *cx, uint32_t argc, jsval *vp)
  199. {
  200. if (argc >= 1) {
  201. jsval *argv = JS_ARGV(cx, vp);
  202. cocos2d::CCMenuItem* ret = cocos2d::CCMenuItem::create();
  203. JSObject *obj = bind_menu_item<cocos2d::CCMenuItem>(cx, ret, argc == 2? argv[1] : JSVAL_VOID, argv[0]);
  204. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  205. return JS_TRUE;
  206. }
  207. return JS_FALSE;
  208. }
  209. JSBool js_cocos2dx_CCMenuItemSprite_create(JSContext *cx, uint32_t argc, jsval *vp)
  210. {
  211. if (argc >= 2) {
  212. jsval *argv = JS_ARGV(cx, vp);
  213. js_proxy_t *proxy;
  214. JSObject *tmpObj;
  215. tmpObj = JSVAL_TO_OBJECT(argv[0]);
  216. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  217. cocos2d::CCNode* arg0 = (cocos2d::CCNode*)(proxy ? proxy->ptr : NULL);
  218. TEST_NATIVE_OBJECT(cx, arg0);
  219. tmpObj = JSVAL_TO_OBJECT(argv[1]);
  220. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  221. cocos2d::CCNode* arg1 = (cocos2d::CCNode*)(proxy ? proxy->ptr : NULL);
  222. TEST_NATIVE_OBJECT(cx, arg1);
  223. int last = 2;
  224. cocos2d::CCNode* arg2 = NULL;
  225. if (argc == 5 || argc == 3) {
  226. tmpObj = JSVAL_TO_OBJECT(argv[2]);
  227. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  228. arg2 = (cocos2d::CCNode*)(proxy ? proxy->ptr : NULL);
  229. TEST_NATIVE_OBJECT(cx, arg2);
  230. last = 3;
  231. }
  232. cocos2d::CCMenuItemSprite* ret = cocos2d::CCMenuItemSprite::create(arg0, arg1, arg2);
  233. jsval thisObj = argv[last++];
  234. jsval callback = argv[last];
  235. JSObject *obj = bind_menu_item<cocos2d::CCMenuItemSprite>(cx, ret, callback, thisObj);
  236. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  237. return JS_TRUE;
  238. }
  239. return JS_FALSE;
  240. }
  241. JSBool js_cocos2dx_CCMenuItemImage_create(JSContext *cx, uint32_t argc, jsval *vp)
  242. {
  243. if (argc >= 2) {
  244. jsval *argv = JS_ARGV(cx, vp);
  245. const char *arg0; do { JSString *tmp = JS_ValueToString(cx, argv[0]); arg0 = JS_EncodeString(cx, tmp); } while (0);
  246. const char *arg1; do { JSString *tmp = JS_ValueToString(cx, argv[1]); arg1 = JS_EncodeString(cx, tmp); } while (0);
  247. const char *arg2 = NULL;
  248. int last = 2;
  249. if (JSVAL_IS_STRING(argv[2])) {
  250. do { JSString *tmp = JS_ValueToString(cx, argv[2]); arg2 = JS_EncodeString(cx, tmp); } while (0);
  251. last = 3;
  252. }
  253. cocos2d::CCMenuItemImage* ret = cocos2d::CCMenuItemImage::create(arg0, arg1, arg2);
  254. jsval thisObj = argv[last++];
  255. jsval callback = argv[last];
  256. JSObject *obj = bind_menu_item<cocos2d::CCMenuItemImage>(cx, ret, callback, thisObj);
  257. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  258. return JS_TRUE;
  259. }
  260. return JS_FALSE;
  261. }
  262. JSBool js_cocos2dx_CCMenuItemLabel_create(JSContext *cx, uint32_t argc, jsval *vp)
  263. {
  264. if (argc >= 1) {
  265. jsval *argv = JS_ARGV(cx, vp);
  266. js_proxy_t *proxy;
  267. JSObject *tmpObj = JSVAL_TO_OBJECT(argv[0]);
  268. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  269. cocos2d::CCNode* arg0 = (cocos2d::CCNode*)(proxy ? proxy->ptr : NULL);
  270. TEST_NATIVE_OBJECT(cx, arg0)
  271. cocos2d::CCMenuItemLabel* ret = cocos2d::CCMenuItemLabel::create(arg0);
  272. JSObject *obj = bind_menu_item<cocos2d::CCMenuItemLabel>(cx, ret, (argc == 3 ? argv[2] : JSVAL_VOID), (argc >= 2 ? argv[1] : JSVAL_VOID));
  273. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  274. return JS_TRUE;
  275. }
  276. return JS_FALSE;
  277. }
  278. JSBool js_cocos2dx_CCMenuItemAtlasFont_create(JSContext *cx, uint32_t argc, jsval *vp)
  279. {
  280. if (argc >= 5) {
  281. jsval *argv = JS_ARGV(cx, vp);
  282. const char *arg0; do { JSString *tmp = JS_ValueToString(cx, argv[0]); arg0 = JS_EncodeString(cx, tmp); } while (0);
  283. const char *arg1; do { JSString *tmp = JS_ValueToString(cx, argv[1]); arg1 = JS_EncodeString(cx, tmp); } while (0);
  284. int arg2; if (!JS_ValueToInt32(cx, argv[2], &arg2)) return JS_FALSE;
  285. int arg3; if (!JS_ValueToInt32(cx, argv[3], &arg3)) return JS_FALSE;
  286. int arg4; if (!JS_ValueToInt32(cx, argv[4], &arg4)) return JS_FALSE;
  287. cocos2d::CCMenuItemAtlasFont* ret = cocos2d::CCMenuItemAtlasFont::create(arg0, arg1, arg2, arg3, arg4);
  288. JSObject *obj = bind_menu_item<cocos2d::CCMenuItemAtlasFont>(cx, ret, (argc == 7 ? argv[6] : JSVAL_VOID), (argc >= 6 ? argv[5] : JSVAL_VOID));
  289. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  290. return JS_TRUE;
  291. }
  292. return JS_FALSE;
  293. }
  294. JSBool js_cocos2dx_CCMenuItemFont_create(JSContext *cx, uint32_t argc, jsval *vp)
  295. {
  296. if (argc >= 1) {
  297. jsval *argv = JS_ARGV(cx, vp);
  298. const char *arg0; do { JSString *tmp = JS_ValueToString(cx, argv[0]); arg0 = JS_EncodeString(cx, tmp); } while (0);
  299. cocos2d::CCMenuItemFont* ret = cocos2d::CCMenuItemFont::create(arg0);
  300. JSObject *obj = bind_menu_item<cocos2d::CCMenuItemFont>(cx, ret, (argc == 3 ? argv[2] : JSVAL_VOID), (argc >= 2 ? argv[1] : JSVAL_VOID));
  301. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  302. return JS_TRUE;
  303. }
  304. return JS_FALSE;
  305. }
  306. JSBool js_cocos2dx_CCMenuItemToggle_create(JSContext *cx, uint32_t argc, jsval *vp)
  307. {
  308. if (argc >= 1) {
  309. jsval *argv = JS_ARGV(cx, vp);
  310. cocos2d::CCMenuItemToggle* ret = cocos2d::CCMenuItemToggle::create();
  311. JSObject *obj = bind_menu_item(cx, ret, (argc == 2 ? argv[1] : JSVAL_VOID), argv[0]);
  312. for (int i=1; i < argc; i++) {
  313. js_proxy_t *proxy;
  314. JSObject *tmpObj = JSVAL_TO_OBJECT(argv[i]);
  315. JS_GET_NATIVE_PROXY(proxy, tmpObj);
  316. cocos2d::CCMenuItem* item = (cocos2d::CCMenuItem*)(proxy ? proxy->ptr : NULL);
  317. TEST_NATIVE_OBJECT(cx, item)
  318. ret->addSubItem(item);
  319. }
  320. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  321. return JS_TRUE;
  322. }
  323. return JS_FALSE;
  324. }
  325. JSBool js_cocos2dx_setCallback(JSContext *cx, uint32_t argc, jsval *vp) {
  326. if(argc == 2) {
  327. jsval *argv = JS_ARGV(cx, vp);
  328. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  329. js_proxy_t *proxy;
  330. JS_GET_NATIVE_PROXY(proxy, obj);
  331. cocos2d::CCMenuItem* item = (cocos2d::CCMenuItem*)(proxy ? proxy->ptr : NULL);
  332. TEST_NATIVE_OBJECT(cx, item)
  333. bind_menu_item(cx, item, argv[1], argv[0]);
  334. return JS_TRUE;
  335. }
  336. return JS_FALSE;
  337. }
  338. JSBool js_cocos2dx_CCAnimation_create(JSContext *cx, uint32_t argc, jsval *vp)
  339. {
  340. jsval *argv = JS_ARGV(cx, vp);
  341. if (argc <= 3) {
  342. cocos2d::CCArray* arg0;
  343. if (argc > 0) {
  344. arg0 = jsval_to_ccarray(cx, argv[0]);
  345. }
  346. cocos2d::CCAnimation* ret;
  347. double arg1 = 0.0f;
  348. if (argc > 0 && argc == 2) {
  349. if (argc == 2) {
  350. JS_ValueToNumber(cx, argv[1], &arg1);
  351. }
  352. ret = cocos2d::CCAnimation::create(arg0, arg1);
  353. } else if (argc > 0) {
  354. unsigned int loops;
  355. JS_ValueToNumber(cx, argv[1], &arg1);
  356. JS_ValueToECMAUint32(cx, argv[1], &loops);
  357. ret = cocos2d::CCAnimation::create(arg0, arg1, loops);
  358. } else if (argc == 0) {
  359. ret = cocos2d::CCAnimation::create();
  360. }
  361. jsval jsret;
  362. if (ret) {
  363. js_proxy_t *proxy;
  364. JS_GET_PROXY(proxy, ret);
  365. if (proxy) {
  366. jsret = OBJECT_TO_JSVAL(proxy->obj);
  367. } else {
  368. // create a new js obj of that class
  369. proxy = js_get_or_create_proxy<cocos2d::CCAnimation>(cx, ret);
  370. jsret = OBJECT_TO_JSVAL(proxy->obj);
  371. }
  372. } else {
  373. jsret = JSVAL_NULL;
  374. }
  375. JS_SET_RVAL(cx, vp, jsret);
  376. return JS_TRUE;
  377. }
  378. return JS_FALSE;
  379. }
  380. JSBool js_cocos2dx_JSTouchDelegate_registerStandardDelegate(JSContext *cx, uint32_t argc, jsval *vp)
  381. {
  382. if (argc >= 1) {
  383. jsval *argv = JS_ARGV(cx, vp);
  384. JSTouchDelegate *touch = new JSTouchDelegate();
  385. touch->registerStandardDelegate();
  386. touch->setJSObject((argc == 1 ? JSVAL_TO_OBJECT(argv[0]) : JSVAL_TO_OBJECT(JSVAL_VOID)));
  387. return JS_TRUE;
  388. }
  389. return JS_FALSE;
  390. }
  391. JSBool js_cocos2dx_JSTouchDelegate_registerTargettedDelegate(JSContext *cx, uint32_t argc, jsval *vp)
  392. {
  393. if (argc >= 1) {
  394. jsval *argv = JS_ARGV(cx, vp);
  395. JSTouchDelegate *touch = new JSTouchDelegate();
  396. touch->registerTargettedDelegate((argc >= 1 ? JSVAL_TO_INT(argv[0]) : 0), (argc >= 2 ? JSVAL_TO_BOOLEAN(argv[1]) : true));
  397. touch->setJSObject((argc == 3 ? JSVAL_TO_OBJECT(argv[2]) : JSVAL_TO_OBJECT(JSVAL_VOID)));
  398. return JS_TRUE;
  399. }
  400. return JS_FALSE;
  401. }
  402. JSBool js_cocos2dx_swap_native_object(JSContext *cx, uint32_t argc, jsval *vp)
  403. {
  404. if (argc == 2) {
  405. // get the native object from the second object to the first object
  406. jsval *argv = JS_ARGV(cx, vp);
  407. JSObject *one = JSVAL_TO_OBJECT(argv[0]);
  408. JSObject *two = JSVAL_TO_OBJECT(argv[1]);
  409. js_proxy_t *nproxy;
  410. JS_GET_NATIVE_PROXY(nproxy, two);
  411. void *ptrTwo = (nproxy ? nproxy->ptr : NULL);
  412. if (nproxy) {
  413. js_proxy_t *jsproxy;
  414. JS_GET_PROXY(jsproxy, ptrTwo);
  415. if (jsproxy) {
  416. JS_REMOVE_PROXY(jsproxy, nproxy);
  417. JS_NEW_PROXY(nproxy, ptrTwo, one);
  418. }
  419. }
  420. }
  421. return JS_TRUE;
  422. }
  423. JSBool js_cocos2dx_CCNode_copy(JSContext *cx, uint32_t argc, jsval *vp)
  424. {
  425. if (argc == 0) {
  426. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  427. js_proxy_t *proxy;
  428. JS_GET_NATIVE_PROXY(proxy, obj);
  429. cocos2d::CCNode *node = (cocos2d::CCNode *)(proxy ? proxy->ptr : NULL);
  430. TEST_NATIVE_OBJECT(cx, node)
  431. JSClass *jsclass = JS_GetClass(obj);
  432. JSObject *proto = JS_GetPrototype(obj);
  433. JSObject *parent = JS_GetParent(obj);
  434. JSObject *jsret = JS_NewObject(cx, jsclass, proto, parent);
  435. cocos2d::CCObject *ret = node->copy();
  436. if (ret && jsret) {
  437. JS_NEW_PROXY(proxy, ret, jsret);
  438. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsret));
  439. return JS_TRUE;
  440. }
  441. }
  442. return JS_FALSE;
  443. }
  444. JSObject* getObjectFromNamespace(JSContext* cx, JSObject *ns, const char *name) {
  445. jsval out;
  446. if (JS_GetProperty(cx, ns, name, &out) == JS_TRUE) {
  447. JSObject *obj;
  448. if (JS_ValueToObject(cx, out, &obj) == JS_TRUE) {
  449. }
  450. }
  451. return NULL;
  452. }
  453. jsval anonEvaluate(JSContext *cx, JSObject *thisObj, const char* string) {
  454. jsval out;
  455. if (JS_EvaluateScript(cx, thisObj, string, strlen(string), "(string)", 1, &out) == JS_TRUE) {
  456. return out;
  457. }
  458. return JSVAL_VOID;
  459. }
  460. JSBool js_platform(JSContext *cx, uint32_t argc, jsval *vp)
  461. {
  462. JSString *str = JS_NewStringCopyZ(cx, "mobile");
  463. jsval out = STRING_TO_JSVAL(str);
  464. JS_SET_RVAL(cx, vp, out);
  465. return JS_TRUE;
  466. }
  467. void JSCallFunc::setJSCallbackFunc(jsval func) {
  468. jsCallback = func;
  469. }
  470. void JSCallFunc::setJSCallbackThis(jsval thisObj) {
  471. jsThisObj = thisObj;
  472. }
  473. void JSCallFunc::setExtraDataField(jsval data) {
  474. extraData = new jsval();
  475. *extraData = data;
  476. }
  477. JSBool js_callFunc(JSContext *cx, uint32_t argc, jsval *vp)
  478. {
  479. if (argc >= 1) {
  480. jsval *argv = JS_ARGV(cx, vp);
  481. JSCallFunc *tmpCobj = new JSCallFunc();
  482. tmpCobj->setJSCallbackThis(argv[0]);
  483. if(argc >= 2) {
  484. tmpCobj->setJSCallbackFunc(argv[1]);
  485. } if(argc == 3) {
  486. tmpCobj->setExtraDataField(argv[2]);
  487. }
  488. CCCallFunc *ret = (CCCallFunc *)CCCallFuncN::create((CCObject *)tmpCobj,
  489. callfuncN_selector(JSCallFunc::callbackFunc));
  490. js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCCallFunc>(cx, ret);
  491. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(proxy->obj));
  492. // test->execute();
  493. }
  494. return JS_TRUE;
  495. }
  496. JSBool js_forceGC(JSContext *cx, uint32_t argc, jsval *vp) {
  497. JSRuntime *rt = JS_GetRuntime(cx);
  498. JS_GC(rt);
  499. return JS_TRUE;
  500. }
  501. JSBool js_cocos2dx_retain(JSContext *cx, uint32_t argc, jsval *vp)
  502. {
  503. JSObject *thisObj = JS_THIS_OBJECT(cx, vp);
  504. if (thisObj) {
  505. js_proxy_t *proxy;
  506. JS_GET_NATIVE_PROXY(proxy, thisObj);
  507. if (proxy) {
  508. ((CCObject *)proxy->ptr)->retain();
  509. return JS_TRUE;
  510. }
  511. }
  512. return JS_FALSE;
  513. }
  514. JSBool js_cocos2dx_release(JSContext *cx, uint32_t argc, jsval *vp)
  515. {
  516. JSObject *thisObj = JS_THIS_OBJECT(cx, vp);
  517. if (thisObj) {
  518. js_proxy_t *proxy;
  519. JS_GET_NATIVE_PROXY(proxy, thisObj);
  520. if (proxy) {
  521. ((CCObject *)proxy->ptr)->release();
  522. return JS_TRUE;
  523. }
  524. }
  525. return JS_FALSE;
  526. }
  527. JSBool js_cocos2dx_CCSet_constructor(JSContext *cx, uint32_t argc, jsval *vp)
  528. {
  529. JSObject *obj;
  530. cocos2d::CCSet* cobj;
  531. if (argc == 0) {
  532. cobj = new cocos2d::CCSet();
  533. cobj->autorelease();
  534. TypeTest<cocos2d::CCSet> t;
  535. js_type_class_t *typeClass;
  536. uint32_t typeId = t.s_id();
  537. HASH_FIND_INT(_js_global_type_ht, &typeId, typeClass);
  538. assert(typeClass);
  539. obj = JS_NewObject(cx, typeClass->jsclass, typeClass->proto, typeClass->parentProto);
  540. js_proxy_t *proxy;
  541. JS_NEW_PROXY(proxy, cobj, obj);
  542. JS_AddNamedObjectRoot(cx, &proxy->obj, typeid(cobj).name());
  543. }
  544. if (cobj) {
  545. JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj));
  546. return JS_TRUE;
  547. }
  548. return JS_FALSE;
  549. }
  550. extern JSObject* js_cocos2dx_CCNode_prototype;
  551. extern JSObject* js_cocos2dx_CCAction_prototype;
  552. extern JSObject* js_cocos2dx_CCAnimation_prototype;
  553. extern JSObject* js_cocos2dx_CCMenuItem_prototype;
  554. extern JSObject* js_cocos2dx_CCSpriteFrame_prototype;
  555. extern JSObject* js_cocos2dx_CCSet_prototype;
  556. void register_cocos2dx_js_extensions(JSContext* cx, JSObject* global)
  557. {
  558. // first, try to get the ns
  559. jsval nsval;
  560. JSObject *ns;
  561. JS_GetProperty(cx, global, "cc", &nsval);
  562. if (nsval == JSVAL_VOID) {
  563. ns = JS_NewObject(cx, NULL, NULL, NULL);
  564. nsval = OBJECT_TO_JSVAL(ns);
  565. JS_SetProperty(cx, global, "cc", &nsval);
  566. } else {
  567. JS_ValueToObject(cx, nsval, &ns);
  568. }
  569. JS_DefineFunction(cx, global, "__associateObjWithNative", js_cocos2dx_swap_native_object, 2, JSPROP_READONLY | JSPROP_PERMANENT);
  570. JS_DefineFunction(cx, global, "__getPlatform", js_platform, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  571. JSObject *tmpObj;
  572. JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "getChildren", js_cocos2dx_CCNode_getChildren, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  573. JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  574. JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  575. JS_DefineFunction(cx, js_cocos2dx_CCNode_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  576. JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  577. JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  578. JS_DefineFunction(cx, js_cocos2dx_CCAction_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  579. JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  580. JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  581. JS_DefineFunction(cx, js_cocos2dx_CCAnimation_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  582. JS_DefineFunction(cx, js_cocos2dx_CCSpriteFrame_prototype, "retain", js_cocos2dx_retain, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  583. JS_DefineFunction(cx, js_cocos2dx_CCSpriteFrame_prototype, "release", js_cocos2dx_release, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  584. JS_DefineFunction(cx, js_cocos2dx_CCMenuItem_prototype, "setCallback", js_cocos2dx_setCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT);
  585. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Node.prototype; })()"));
  586. JS_DefineFunction(cx, tmpObj, "copy", js_cocos2dx_CCNode_copy, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  587. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Menu; })()"));
  588. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenu_create, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  589. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItem; })()"));
  590. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItem_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  591. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemSprite; })()"));
  592. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemSprite_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  593. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemImage; })()"));
  594. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemImage_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  595. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemLabel; })()"));
  596. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemLabel_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  597. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemAtlasFont; })()"));
  598. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemAtlasFont_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  599. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemFont; })()"));
  600. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemFont_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  601. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.MenuItemToggle; })()"));
  602. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCMenuItemToggle_create, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  603. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Sequence; })()"));
  604. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCSequence_create, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  605. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Spawn; })()"));
  606. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCSpawn_create, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  607. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.Animation; })()"));
  608. JS_DefineFunction(cx, tmpObj, "create", js_cocos2dx_CCAnimation_create, 0, JSPROP_READONLY | JSPROP_PERMANENT);
  609. JS_DefineFunction(cx, ns, "registerTargettedDelegate", js_cocos2dx_JSTouchDelegate_registerTargettedDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  610. JS_DefineFunction(cx, ns, "registerStandardDelegate", js_cocos2dx_JSTouchDelegate_registerStandardDelegate, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  611. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return cc.CallFunc; })()"));
  612. JS_DefineFunction(cx, tmpObj, "create", js_callFunc, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  613. tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, global, "(function () { return this; })()"));
  614. JS_DefineFunction(cx, tmpObj, "garbageCollect", js_forceGC, 1, JSPROP_READONLY | JSPROP_PERMANENT);
  615. // add constructor for CCSet
  616. JSFunction *ccSetConstructor = JS_NewFunction(cx, js_cocos2dx_CCSet_constructor, 0, JSPROP_READONLY | JSPROP_PERMANENT, NULL, "constructor");
  617. JSObject *ctor = JS_GetFunctionObject(ccSetConstructor);
  618. JS_LinkConstructorAndPrototype(cx, ctor, js_cocos2dx_CCSet_prototype);
  619. }