jsb_pluginx_manual_callback.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. #include "jsb_pluginx_manual_callback.h"
  2. #include "jsb_pluginx_basic_conversions.h"
  3. #include "jsb_pluginx_spidermonkey_specifics.h"
  4. #include "ProtocolAds.h"
  5. #include "AgentManager.h"
  6. #include "FacebookAgent.h"
  7. using namespace pluginx;
  8. static JSContext* s_cx = NULL;
  9. class Pluginx_PurchaseResult : public cocos2d::plugin::PayResultListener
  10. {
  11. public:
  12. virtual void onPayResult(cocos2d::plugin::PayResultCode ret, const char* msg, cocos2d::plugin::TProductInfo info)
  13. {
  14. char goodInfo[1024] = { 0 };
  15. sprintf(goodInfo, "商品名称:%s\n商品价格:%s\n商品描述:%s",
  16. info.find("productName")->second.c_str(),
  17. info.find("productPrice")->second.c_str(),
  18. info.find("productDesc")->second.c_str());
  19. LOGD(goodInfo);
  20. JSContext* cx = s_cx;
  21. JS::RootedObject obj(cx, _JSDelegate);
  22. JSAutoCompartment ac(cx, obj);
  23. bool hasAction;
  24. JS::RootedValue retval(cx);
  25. JS::RootedValue temp_retval(cx);
  26. jsval dataVal[3];
  27. dataVal[0] = INT_TO_JSVAL(ret);
  28. std::string strMsgInfo = msg;
  29. dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
  30. dataVal[2] = TProductInfo_to_jsval(cx, info);
  31. if (JS_HasProperty(cx, obj, "onPayResult", &hasAction) && hasAction) {
  32. if(!JS_GetProperty(cx, obj, "onPayResult", &temp_retval)) {
  33. return;
  34. }
  35. if(temp_retval == JSVAL_VOID) {
  36. return;
  37. }
  38. JS_CallFunctionName(cx, obj, "onPayResult", JS::HandleValueArray::fromMarkedLocation(3, dataVal), &retval);
  39. }
  40. }
  41. void setJSDelegate(JSObject* pJSDelegate)
  42. {
  43. _JSDelegate = pJSDelegate;
  44. }
  45. JSObject* getJSDelegate()
  46. {
  47. return _JSDelegate;
  48. }
  49. virtual void onRequestProductsResult(cocos2d::plugin::IAPProductRequest ret, cocos2d::plugin::TProductList info){
  50. JSContext* cx = s_cx;
  51. JS::RootedObject obj(cx, _JSDelegate);
  52. JSAutoCompartment ac(cx, obj);
  53. bool hasAction;
  54. JS::RootedValue retval(cx);
  55. JS::RootedValue temp_retval(cx);
  56. jsval dataVal[2];
  57. dataVal[0] = INT_TO_JSVAL(ret);
  58. if(info.size() > 0){
  59. dataVal[1] = TProductList_to_jsval(cx, info);
  60. }
  61. if (JS_HasProperty(cx, obj, "onRequestProductResult", &hasAction) && hasAction) {
  62. if(!JS_GetProperty(cx, obj, "onRequestProductResult", &temp_retval)) {
  63. return;
  64. }
  65. if(temp_retval == JSVAL_VOID) {
  66. return;
  67. }
  68. JS_CallFunctionName(cx, obj, "onRequestProductResult", JS::HandleValueArray::fromMarkedLocation(2, dataVal), &retval);
  69. }
  70. }
  71. private:
  72. JS::Heap<JSObject*> _JSDelegate;
  73. };
  74. bool js_pluginx_ProtocolIAP_setResultListener(JSContext *cx, uint32_t argc, jsval *vp)
  75. {
  76. s_cx = cx;
  77. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  78. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  79. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  80. cocos2d::plugin::ProtocolIAP* cobj = (cocos2d::plugin::ProtocolIAP *)(proxy ? proxy->ptr : NULL);
  81. bool ok = true;
  82. if (argc == 1) {
  83. // save the delegate
  84. JSObject *jsDelegate = args.get(0).toObjectOrNull();
  85. Pluginx_PurchaseResult* nativeDelegate = new Pluginx_PurchaseResult();
  86. nativeDelegate->setJSDelegate(jsDelegate);
  87. cobj->setResultListener(nativeDelegate);
  88. args.rval().setUndefined();
  89. return true;
  90. }
  91. JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
  92. return false;
  93. }
  94. bool js_pluginx_ProtocolIAP_getResultListener(JSContext *cx, uint32_t argc, jsval *vp)
  95. {
  96. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  97. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  98. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  99. cocos2d::plugin::ProtocolIAP* cobj = (cocos2d::plugin::ProtocolIAP *)(proxy ? proxy->ptr : NULL);
  100. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_ProtocolIAP_getResultListener : Invalid Native Object");
  101. if (argc == 0) {
  102. Pluginx_PurchaseResult* listener = (Pluginx_PurchaseResult*)cobj->getResultListener();
  103. JSObject *ret = listener->getJSDelegate();
  104. jsval jsret = JSVAL_NULL;
  105. do {
  106. if (ret) {
  107. jsret = OBJECT_TO_JSVAL(ret);
  108. } else {
  109. jsret = JSVAL_NULL;
  110. }
  111. } while (0);
  112. args.rval().setUndefined();
  113. return true;
  114. }
  115. JS_ReportError(cx, "js_pluginx_ProtocolIAP_getResultListener : wrong number of arguments: %d, was expecting %d", argc, 0);
  116. return false;
  117. }
  118. bool js_pluginx_ProtocolIAP_payForProduct(JSContext *cx, uint32_t argc, jsval *vp)
  119. {
  120. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  121. bool ok = true;
  122. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  123. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  124. cocos2d::plugin::ProtocolIAP* cobj = (cocos2d::plugin::ProtocolIAP *)(proxy ? proxy->ptr : NULL);
  125. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolIAP_payForProduct : Invalid Native Object");
  126. if (argc == 1) {
  127. cocos2d::plugin::TProductInfo arg0;
  128. ok &= pluginx::jsval_to_TProductInfo(cx, args.get(0), &arg0);
  129. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolIAP_payForProduct : Error processing arguments");
  130. cobj->payForProduct(arg0);
  131. args.rval().setUndefined();
  132. return true;
  133. }
  134. if(argc == 2){
  135. cocos2d::plugin::TProductInfo arg0;
  136. ok &= pluginx::jsval_to_TProductInfo(cx, args.get(0), &arg0);
  137. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolIAP_payForProduct : Error processing arguments");
  138. std::function<void (int, std::string&)> arg1;
  139. do {
  140. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  141. auto lambda = [=](int larg0, std::string& larg1) -> void {
  142. JSAutoCompartment ac(cx, obj);
  143. jsval largv[2];
  144. largv[0] = int32_to_jsval(cx, larg0);
  145. largv[1] = std_string_to_jsval(cx, larg1);
  146. JS::RootedValue rval(cx);
  147. bool succeed = func->invoke(2, &largv[0], &rval);
  148. if (!succeed && JS_IsExceptionPending(cx)) {
  149. JS_ReportPendingException(cx);
  150. }
  151. };
  152. arg1 = lambda;
  153. } while(0);
  154. cobj->payForProduct(arg0, arg1);
  155. args.rval().setUndefined();
  156. return true;
  157. }
  158. JS_ReportError(cx, "js_pluginx_protocols_ProtocolIAP_payForProduct : wrong number of arguments");
  159. return false;
  160. }
  161. class Pluginx_AdsListener : public cocos2d::plugin::AdsListener
  162. {
  163. public:
  164. virtual void onAdsResult(AdsResultCode code, const char* msg)
  165. {
  166. JSContext* cx = s_cx;
  167. JS::RootedObject obj(cx, _JSDelegate);
  168. JSAutoCompartment ac(cx, obj);
  169. bool hasAction;
  170. JS::RootedValue retval(cx);
  171. JS::RootedValue temp_retval(cx);
  172. jsval dataVal[2];
  173. dataVal[0] = INT_TO_JSVAL(code);
  174. std::string strMsgInfo = msg;
  175. dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
  176. bool bRet = JS_HasProperty(cx, obj, "onAdsResult", &hasAction);
  177. if (bRet && hasAction) {
  178. if(!JS_GetProperty(cx, obj, "onAdsResult", &temp_retval)) {
  179. return;
  180. }
  181. if(temp_retval == JSVAL_VOID) {
  182. return;
  183. }
  184. JS_CallFunctionName(cx, obj, "onAdsResult", JS::HandleValueArray::fromMarkedLocation(2, dataVal), &retval);
  185. }
  186. }
  187. virtual void onPlayerGetPoints(ProtocolAds* pAdsPlugin, int points)
  188. {
  189. JSContext* cx = s_cx;
  190. JS::RootedObject obj(cx, _JSDelegate);
  191. JSAutoCompartment ac(cx, obj);
  192. bool hasAction;
  193. JS::RootedValue retval(cx);
  194. JS::RootedValue temp_retval(cx);
  195. js_proxy_t * p;
  196. JS_GET_PROXY(p, pAdsPlugin);
  197. if (! p) return;
  198. jsval dataVal[2];
  199. jsval arg = OBJECT_TO_JSVAL(p->obj);
  200. dataVal[0] = arg;
  201. dataVal[1] = INT_TO_JSVAL(points);
  202. bool bRet = JS_HasProperty(cx, obj, "onPlayerGetPoints", &hasAction);
  203. if (bRet && hasAction) {
  204. if(!JS_GetProperty(cx, obj, "onPlayerGetPoints", &temp_retval)) {
  205. return;
  206. }
  207. if(temp_retval == JSVAL_VOID) {
  208. return;
  209. }
  210. JS_CallFunctionName(cx, obj, "onPlayerGetPoints", JS::HandleValueArray::fromMarkedLocation(2, dataVal), &retval);
  211. }
  212. }
  213. void setJSDelegate(JSObject* pJSDelegate)
  214. {
  215. _JSDelegate = pJSDelegate;
  216. }
  217. JSObject* getJSDelegate()
  218. {
  219. return _JSDelegate;
  220. }
  221. private:
  222. JS::Heap<JSObject*> _JSDelegate;
  223. };
  224. bool js_pluginx_ProtocolAds_setAdsListener(JSContext *cx, uint32_t argc, jsval *vp)
  225. {
  226. s_cx = cx;
  227. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  228. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  229. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  230. cocos2d::plugin::ProtocolAds* cobj = (cocos2d::plugin::ProtocolAds *)(proxy ? proxy->ptr : NULL);
  231. bool ok = true;
  232. if (argc == 1) {
  233. // save the delegate
  234. JSObject *jsDelegate = args.get(0).toObjectOrNull();
  235. Pluginx_AdsListener* nativeDelegate = new Pluginx_AdsListener();
  236. nativeDelegate->setJSDelegate(jsDelegate);
  237. cobj->setAdsListener(nativeDelegate);
  238. args.rval().setUndefined();
  239. return true;
  240. }
  241. JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
  242. return false;
  243. }
  244. bool js_pluginx_ProtocolAds_getAdsListener(JSContext *cx, uint32_t argc, jsval *vp)
  245. {
  246. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  247. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  248. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  249. cocos2d::plugin::ProtocolAds* cobj = (cocos2d::plugin::ProtocolAds *)(proxy ? proxy->ptr : NULL);
  250. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_ProtocolAds_getAdsListener : Invalid Native Object");
  251. if (argc == 0) {
  252. Pluginx_AdsListener* listener = (Pluginx_AdsListener*)cobj->getAdsListener();
  253. JSObject *ret = listener->getJSDelegate();
  254. jsval jsret = JSVAL_NULL;
  255. do {
  256. if (ret) {
  257. jsret = OBJECT_TO_JSVAL(ret);
  258. } else {
  259. jsret = JSVAL_NULL;
  260. }
  261. } while (0);
  262. args.rval().set(jsret);
  263. return true;
  264. }
  265. JS_ReportError(cx, "js_pluginx_ProtocolAds_getAdsListener : wrong number of arguments: %d, was expecting %d", argc, 0);
  266. return false;
  267. }
  268. class Pluginx_ShareResult : public cocos2d::plugin::ShareResultListener
  269. {
  270. public:
  271. virtual void onShareResult(cocos2d::plugin::ShareResultCode ret, const char* msg)
  272. {
  273. JSContext* cx = s_cx;
  274. JS::RootedObject obj(cx, _JSDelegate);
  275. JSAutoCompartment ac(cx, obj);
  276. bool hasAction;
  277. JS::RootedValue retval(cx);
  278. JS::RootedValue temp_retval(cx);
  279. jsval dataVal[2];
  280. dataVal[0] = INT_TO_JSVAL(ret);
  281. std::string strMsgInfo = msg;
  282. dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
  283. if (JS_HasProperty(cx, obj, "onShareResult", &hasAction) && hasAction) {
  284. if(!JS_GetProperty(cx, obj, "onShareResult", &temp_retval)) {
  285. return;
  286. }
  287. if(temp_retval == JSVAL_VOID) {
  288. return;
  289. }
  290. JS_CallFunctionName(cx, obj, "onShareResult", JS::HandleValueArray::fromMarkedLocation(2, dataVal), &retval);
  291. }
  292. }
  293. void setJSDelegate(JSObject* pJSDelegate)
  294. {
  295. _JSDelegate = pJSDelegate;
  296. }
  297. JSObject* getJSDelegate()
  298. {
  299. return _JSDelegate;
  300. }
  301. private:
  302. JS::Heap<JSObject*> _JSDelegate;
  303. };
  304. bool js_pluginx_ProtocolShare_setResultListener(JSContext *cx, uint32_t argc, jsval *vp)
  305. {
  306. s_cx = cx;
  307. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  308. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  309. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  310. cocos2d::plugin::ProtocolShare* cobj = (cocos2d::plugin::ProtocolShare *)(proxy ? proxy->ptr : NULL);
  311. bool ok = true;
  312. if (argc == 1) {
  313. // save the delegate
  314. JSObject *jsDelegate = args.get(0).toObjectOrNull();
  315. Pluginx_ShareResult* nativeDelegate = new Pluginx_ShareResult();
  316. nativeDelegate->setJSDelegate(jsDelegate);
  317. cobj->setResultListener(nativeDelegate);
  318. args.rval().setUndefined();
  319. return true;
  320. }
  321. JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
  322. return false;
  323. }
  324. bool js_pluginx_ProtocolShare_getResultListener(JSContext *cx, uint32_t argc, jsval *vp)
  325. {
  326. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  327. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  328. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  329. cocos2d::plugin::ProtocolShare* cobj = (cocos2d::plugin::ProtocolShare *)(proxy ? proxy->ptr : NULL);
  330. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_ProtocolShare_getResultListener : Invalid Native Object");
  331. if (argc == 0) {
  332. Pluginx_ShareResult* listener = (Pluginx_ShareResult*)cobj->getResultListener();
  333. JSObject *ret = listener->getJSDelegate();
  334. jsval jsret = JSVAL_NULL;
  335. do {
  336. if (ret) {
  337. jsret = OBJECT_TO_JSVAL(ret);
  338. } else {
  339. jsret = JSVAL_NULL;
  340. }
  341. } while (0);
  342. args.rval().set(jsret);
  343. return true;
  344. }
  345. JS_ReportError(cx, "js_pluginx_ProtocolShare_getResultListener : wrong number of arguments: %d, was expecting %d", argc, 0);
  346. return false;
  347. }
  348. bool js_pluginx_ProtocolShare_share(JSContext *cx, uint32_t argc, jsval *vp)
  349. {
  350. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  351. bool ok = true;
  352. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  353. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  354. cocos2d::plugin::ProtocolShare* cobj = (cocos2d::plugin::ProtocolShare *)(proxy ? proxy->ptr : NULL);
  355. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolShare_share : Invalid Native Object");
  356. if (argc == 1) {
  357. cocos2d::plugin::TShareInfo arg0;
  358. ok &= pluginx::jsval_to_TShareInfo(cx, args.get(0), &arg0);
  359. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolShare_share : Error processing arguments");
  360. cobj->share(arg0);
  361. args.rval().setUndefined();
  362. return true;
  363. }
  364. if(argc == 2){
  365. cocos2d::plugin::TShareInfo arg0;
  366. ok &= pluginx::jsval_to_TShareInfo(cx, args.get(0), &arg0);
  367. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolShare_share : Error processing arguments");
  368. std::function<void (int, std::string&)> arg1;
  369. do {
  370. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  371. auto lambda = [=](int larg0, std::string& larg1) -> void {
  372. JSAutoCompartment ac(cx, obj);
  373. jsval largv[2];
  374. largv[0] = int32_to_jsval(cx, larg0);
  375. largv[1] = std_string_to_jsval(cx, larg1);
  376. JS::RootedValue rval(cx);
  377. bool succeed = func->invoke(2, &largv[0], &rval);
  378. if (!succeed && JS_IsExceptionPending(cx)) {
  379. JS_ReportPendingException(cx);
  380. }
  381. };
  382. arg1 = lambda;
  383. } while(0);
  384. cobj->share(arg0, arg1);
  385. args.rval().setUndefined();
  386. return true;
  387. }
  388. JS_ReportError(cx, "js_pluginx_protocols_ProtocolShare_share : wrong number of arguments");
  389. return false;
  390. }
  391. class Pluginx_SocialResult : public cocos2d::plugin::SocialListener
  392. {
  393. public:
  394. virtual void onSocialResult(cocos2d::plugin::SocialRetCode ret, const char* msg)
  395. {
  396. JSContext* cx = s_cx;
  397. JS::RootedObject obj(cx, _JSDelegate);
  398. JSAutoCompartment ac(cx, obj);
  399. bool hasAction;
  400. JS::RootedValue retval(cx);
  401. JS::RootedValue temp_retval(cx);
  402. jsval dataVal[2];
  403. dataVal[0] = INT_TO_JSVAL(ret);
  404. std::string strMsgInfo = msg;
  405. dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
  406. if (JS_HasProperty(cx, obj, "onSocialResult", &hasAction) && hasAction) {
  407. if(!JS_GetProperty(cx, obj, "onSocialResult", &temp_retval)) {
  408. return;
  409. }
  410. if(temp_retval == JSVAL_VOID) {
  411. return;
  412. }
  413. JS_CallFunctionName(cx, obj, "onSocialResult", JS::HandleValueArray::fromMarkedLocation(2, dataVal), &retval);
  414. }
  415. }
  416. void setJSDelegate(JSObject* pJSDelegate)
  417. {
  418. _JSDelegate = pJSDelegate;
  419. }
  420. JSObject* getJSDelegate()
  421. {
  422. return _JSDelegate;
  423. }
  424. private:
  425. JS::Heap<JSObject*> _JSDelegate;
  426. };
  427. bool js_pluginx_ProtocolSocial_setListener(JSContext *cx, uint32_t argc, jsval *vp)
  428. {
  429. s_cx = cx;
  430. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  431. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  432. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  433. cocos2d::plugin::ProtocolSocial* cobj = (cocos2d::plugin::ProtocolSocial *)(proxy ? proxy->ptr : NULL);
  434. bool ok = true;
  435. if (argc == 1) {
  436. // save the delegate
  437. JSObject *jsDelegate = args.get(0).toObjectOrNull();
  438. Pluginx_SocialResult* nativeDelegate = new Pluginx_SocialResult();
  439. nativeDelegate->setJSDelegate(jsDelegate);
  440. cobj->setListener(nativeDelegate);
  441. args.rval().setUndefined();
  442. return true;
  443. }
  444. JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
  445. return false;
  446. }
  447. bool js_pluginx_ProtocolSocial_getListener(JSContext *cx, uint32_t argc, jsval *vp)
  448. {
  449. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  450. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  451. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  452. cocos2d::plugin::ProtocolSocial* cobj = (cocos2d::plugin::ProtocolSocial *)(proxy ? proxy->ptr : NULL);
  453. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_ProtocolSocial_getListener : Invalid Native Object");
  454. if (argc == 0) {
  455. Pluginx_SocialResult* listener = (Pluginx_SocialResult*)cobj->getListener();
  456. JSObject *ret = listener->getJSDelegate();
  457. jsval jsret = JSVAL_NULL;
  458. do {
  459. if (ret) {
  460. jsret = OBJECT_TO_JSVAL(ret);
  461. } else {
  462. jsret = JSVAL_NULL;
  463. }
  464. } while (0);
  465. args.rval().set(jsret);
  466. return true;
  467. }
  468. JS_ReportError(cx, "js_pluginx_ProtocolSocial_getListener : wrong number of arguments: %d, was expecting %d", argc, 0);
  469. return false;
  470. }
  471. bool js_pluginx_ProtocolSocial_submitScore(JSContext *cx, uint32_t argc, jsval *vp)
  472. {
  473. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  474. bool ok = true;
  475. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  476. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  477. cocos2d::plugin::ProtocolSocial* cobj = (cocos2d::plugin::ProtocolSocial *)(proxy ? proxy->ptr : NULL);
  478. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolSocial_submitScore : Invalid Native Object");
  479. if (argc == 2) {
  480. const char* arg0;
  481. long arg1;
  482. std::string arg0_tmp; ok &= jsval_to_std_string(cx, args.get(0), &arg0_tmp); arg0 = arg0_tmp.c_str();
  483. ok &= jsval_to_long(cx, args.get(1), (long *)&arg1);
  484. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolSocial_submitScore : Error processing arguments");
  485. cobj->submitScore(arg0, arg1);
  486. args.rval().setUndefined();
  487. return true;
  488. }
  489. if(argc == 3){
  490. const char* arg0;
  491. long arg1;
  492. std::string arg0_tmp; ok &= jsval_to_std_string(cx, args.get(0), &arg0_tmp); arg0 = arg0_tmp.c_str();
  493. ok &= jsval_to_long(cx, args.get(1), (long *)&arg1);
  494. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolSocial_submitScore : Error processing arguments");
  495. std::function<void (int, std::string&)> arg2;
  496. do {
  497. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(2)));
  498. auto lambda = [=](int larg0, std::string& larg1) -> void {
  499. JSAutoCompartment ac(cx, obj);
  500. jsval largv[2];
  501. largv[0] = int32_to_jsval(cx, larg0);
  502. largv[1] = std_string_to_jsval(cx, larg1);
  503. JS::RootedValue rval(cx);
  504. bool succeed = func->invoke(2, &largv[0], &rval);
  505. if (!succeed && JS_IsExceptionPending(cx)) {
  506. JS_ReportPendingException(cx);
  507. }
  508. };
  509. arg2 = lambda;
  510. } while(0);
  511. cobj->submitScore(arg0, arg1, arg2);
  512. args.rval().setUndefined();
  513. return true;
  514. }
  515. JS_ReportError(cx, "js_pluginx_protocols_ProtocolSocial_submitScore : wrong number of arguments");
  516. return false;
  517. }
  518. bool js_pluginx_ProtocolSocial_unlockAchievement(JSContext *cx, uint32_t argc, jsval *vp)
  519. {
  520. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  521. bool ok = true;
  522. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  523. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  524. cocos2d::plugin::ProtocolSocial* cobj = (cocos2d::plugin::ProtocolSocial *)(proxy ? proxy->ptr : NULL);
  525. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolSocial_unlockAchievement : Invalid Native Object");
  526. if (argc == 1) {
  527. cocos2d::plugin::TAchievementInfo arg0;
  528. ok &= pluginx::jsval_to_TAchievementInfo(cx, args.get(0), &arg0);
  529. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolSocial_unlockAchievement : Error processing arguments");
  530. cobj->unlockAchievement(arg0);
  531. args.rval().setUndefined();
  532. return true;
  533. }
  534. if(argc ==2){
  535. cocos2d::plugin::TAchievementInfo arg0;
  536. ok &= pluginx::jsval_to_TAchievementInfo(cx, args.get(0), &arg0);
  537. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_ProtocolSocial_unlockAchievement : Error processing arguments");
  538. std::function<void (int, std::string&)> arg1;
  539. do {
  540. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  541. auto lambda = [=](int larg0, std::string& larg1) -> void {
  542. JSAutoCompartment ac(cx, obj);
  543. jsval largv[2];
  544. largv[0] = int32_to_jsval(cx, larg0);
  545. largv[1] = std_string_to_jsval(cx, larg1);
  546. JS::RootedValue rval(cx);
  547. bool succeed = func->invoke(2, &largv[0], &rval);
  548. if (!succeed && JS_IsExceptionPending(cx)) {
  549. JS_ReportPendingException(cx);
  550. }
  551. };
  552. arg1 = lambda;
  553. } while(0);
  554. cobj->unlockAchievement(arg0, arg1);
  555. args.rval().setUndefined();
  556. return true;
  557. }
  558. JS_ReportError(cx, "js_pluginx_protocols_ProtocolSocial_unlockAchievement : wrong number of arguments");
  559. return false;
  560. }
  561. class Pluginx_UserActionListener : public cocos2d::plugin::UserActionListener
  562. {
  563. public:
  564. virtual void onActionResult(ProtocolUser* userPlugin, cocos2d::plugin::UserActionResultCode ret, const char* msg)
  565. {
  566. JSContext* cx = s_cx;
  567. JS::RootedObject obj(cx, _JSDelegate);
  568. JSAutoCompartment ac(cx, obj);
  569. bool hasAction;
  570. JS::RootedValue retval(cx);
  571. JS::RootedValue temp_retval(cx);
  572. js_proxy_t * p;
  573. JS_GET_PROXY(p, userPlugin);
  574. if (! p) return;
  575. jsval dataVal[3];
  576. jsval arg1 = OBJECT_TO_JSVAL(p->obj);
  577. dataVal[0] = arg1;
  578. dataVal[1] = INT_TO_JSVAL(ret);
  579. std::string strMsgInfo = msg;
  580. dataVal[2] = std_string_to_jsval(cx, strMsgInfo);
  581. if (JS_HasProperty(cx, obj, "onActionResult", &hasAction) && hasAction) {
  582. if(!JS_GetProperty(cx, obj, "onActionResult", &temp_retval)) {
  583. return;
  584. }
  585. if(temp_retval == JSVAL_VOID) {
  586. return;
  587. }
  588. JS_CallFunctionName(cx, obj, "onActionResult", JS::HandleValueArray::fromMarkedLocation(3, dataVal), &retval);
  589. }
  590. }
  591. void setJSDelegate(JSObject* pJSDelegate)
  592. {
  593. _JSDelegate = pJSDelegate;
  594. }
  595. JSObject* getJSDelegate()
  596. {
  597. return _JSDelegate;
  598. }
  599. private:
  600. JS::Heap<JSObject*> _JSDelegate;
  601. };
  602. bool js_pluginx_ProtocolUser_setActionListener(JSContext *cx, uint32_t argc, jsval *vp)
  603. {
  604. s_cx = cx;
  605. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  606. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  607. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  608. cocos2d::plugin::ProtocolUser* cobj = (cocos2d::plugin::ProtocolUser *)(proxy ? proxy->ptr : NULL);
  609. bool ok = true;
  610. if (argc == 1) {
  611. // save the delegate
  612. JSObject *jsDelegate = args.get(0).toObjectOrNull();
  613. Pluginx_UserActionListener* nativeDelegate = new Pluginx_UserActionListener();
  614. nativeDelegate->setJSDelegate(jsDelegate);
  615. cobj->setActionListener(nativeDelegate);
  616. args.rval().setUndefined();
  617. return true;
  618. }
  619. JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
  620. return false;
  621. }
  622. bool js_pluginx_ProtocolUser_getActionListener(JSContext *cx, uint32_t argc, jsval *vp)
  623. {
  624. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  625. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  626. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  627. cocos2d::plugin::ProtocolUser* cobj = (cocos2d::plugin::ProtocolUser *)(proxy ? proxy->ptr : NULL);
  628. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_ProtocolUser_getActionListener : Invalid Native Object");
  629. if (argc == 0) {
  630. Pluginx_UserActionListener* listener = (Pluginx_UserActionListener*)cobj->getActionListener();
  631. JSObject *ret = listener->getJSDelegate();
  632. jsval jsret = JSVAL_NULL;
  633. do {
  634. if (ret) {
  635. jsret = OBJECT_TO_JSVAL(ret);
  636. } else {
  637. jsret = JSVAL_NULL;
  638. }
  639. } while (0);
  640. args.rval().set(jsret);
  641. return true;
  642. }
  643. JS_ReportError(cx, "js_pluginx_ProtocolUser_getActionListener : wrong number of arguments: %d, was expecting %d", argc, 0);
  644. return false;
  645. }
  646. bool js_pluginx_ProtocolUser_login(JSContext *cx, uint32_t argc, jsval *vp)
  647. {
  648. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  649. bool ok = true;
  650. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  651. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  652. cocos2d::plugin::ProtocolUser* cobj = (cocos2d::plugin::ProtocolUser *)(proxy ? proxy->ptr : NULL);
  653. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolUser_login : Invalid Native Object");
  654. do {
  655. if (argc == 0) {
  656. cobj->login();
  657. args.rval().setUndefined();
  658. return true;
  659. }
  660. } while(0);
  661. do {
  662. if (argc == 1) {
  663. std::function<void (int, std::string&)> arg0;
  664. do {
  665. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(0)));
  666. auto lambda = [=](int larg0, std::string& larg1) -> void {
  667. JSAutoCompartment ac(cx, obj);
  668. jsval largv[2];
  669. largv[0] = int32_to_jsval(cx, larg0);
  670. largv[1] = std_string_to_jsval(cx, larg1);
  671. JS::RootedValue rval(cx);
  672. bool succeed = func->invoke(2, &largv[0], &rval);
  673. if (!succeed && JS_IsExceptionPending(cx)) {
  674. JS_ReportPendingException(cx);
  675. }
  676. };
  677. arg0 = lambda;
  678. } while(0);
  679. if (!ok) { ok = true; break; }
  680. cobj->login(arg0);
  681. args.rval().setUndefined();
  682. return true;
  683. }
  684. } while(0);
  685. JS_ReportError(cx, "js_pluginx_protocols_ProtocolUser_login : wrong number of arguments");
  686. return false;
  687. }
  688. bool js_pluginx_ProtocolUser_logout(JSContext *cx, uint32_t argc, jsval *vp)
  689. {
  690. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  691. bool ok = true;
  692. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  693. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  694. cocos2d::plugin::ProtocolUser* cobj = (cocos2d::plugin::ProtocolUser *)(proxy ? proxy->ptr : NULL);
  695. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_ProtocolUser_logout : Invalid Native Object");
  696. if (argc == 0) {
  697. cobj->logout();
  698. args.rval().setUndefined();
  699. return true;
  700. }
  701. do {
  702. if (argc == 1) {
  703. std::function<void (int, std::string&)> arg0;
  704. do {
  705. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(0)));
  706. auto lambda = [=](int larg0, std::string& larg1) -> void {
  707. JSAutoCompartment ac(cx, obj);
  708. jsval largv[2];
  709. largv[0] = int32_to_jsval(cx, larg0);
  710. largv[1] = std_string_to_jsval(cx, larg1);
  711. JS::RootedValue rval(cx);
  712. bool succeed = func->invoke(2, &largv[0], &rval);
  713. if (!succeed && JS_IsExceptionPending(cx)) {
  714. JS_ReportPendingException(cx);
  715. }
  716. };
  717. arg0 = lambda;
  718. } while(0);
  719. if (!ok) { ok = true; break; }
  720. cobj->logout(arg0);
  721. args.rval().setUndefined();
  722. return true;
  723. }
  724. } while(0);
  725. JS_ReportError(cx, "js_pluginx_protocols_ProtocolUser_logout : wrong number of arguments");
  726. return false;
  727. }
  728. bool js_pluginx_FacebookAgent_login(JSContext *cx, uint32_t argc, jsval *vp)
  729. {
  730. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  731. bool ok = true;
  732. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  733. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  734. cocos2d::plugin::FacebookAgent* cobj = (cocos2d::plugin::FacebookAgent *)(proxy ? proxy->ptr : NULL);
  735. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_FacebookAgent_login : Invalid Native Object");
  736. do {
  737. if (argc == 1) {
  738. std::function<void (int, std::string&)> arg0;
  739. do {
  740. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(0)));
  741. auto lambda = [=](int larg0, std::string& larg1) -> void {
  742. JSAutoCompartment ac(cx, obj);
  743. jsval largv[2];
  744. largv[0] = int32_to_jsval(cx, larg0);
  745. jsval temp = std_string_to_jsval(cx, larg1);
  746. JS::RootedValue outVal(cx);
  747. // size_t utf16Count = 0;
  748. // const jschar* utf16Buf = JS_GetStringCharsZAndLength(cx, JSVAL_TO_STRING(temp), &utf16Count);
  749. // JS_ParseJSON(cx, utf16Buf, static_cast<uint32_t>(utf16Count), &outVal);
  750. JS_ParseJSON(cx, JS::RootedString(cx, temp.toString()), &outVal);
  751. largv[1] = outVal.get();
  752. JS::RootedValue rval(cx);
  753. bool succeed = func->invoke(2, &largv[0], &rval);
  754. if (!succeed && JS_IsExceptionPending(cx)) {
  755. JS_ReportPendingException(cx);
  756. }
  757. };
  758. arg0 = lambda;
  759. } while(0);
  760. if (!ok) { ok = true; break; }
  761. cobj->login(arg0);
  762. args.rval().setUndefined();
  763. return true;
  764. }
  765. if (argc == 2) {
  766. std::string arg0;
  767. ok &= pluginx::jsval_array_to_string(cx, args.get(0), &arg0);
  768. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_FacebookAgent_login : Error processing arguments");
  769. if (!ok) { ok = true; break; }
  770. std::function<void (int, std::string&)> arg1;
  771. do {
  772. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  773. auto lambda = [=](int larg0, std::string& larg1) -> void {
  774. JSAutoCompartment ac(cx, obj);
  775. jsval largv[2];
  776. largv[0] = int32_to_jsval(cx, larg0);
  777. jsval temp = std_string_to_jsval(cx, larg1);
  778. JS::RootedValue outVal(cx);
  779. // size_t utf16Count = 0;
  780. // const jschar* utf16Buf = JS_GetStringCharsZAndLength(cx, JSVAL_TO_STRING(temp), &utf16Count);
  781. // JS_ParseJSON(cx, utf16Buf, static_cast<uint32_t>(utf16Count), &outVal);
  782. JS_ParseJSON(cx, JS::RootedString(cx, temp.toString()), &outVal);
  783. largv[1] = outVal.get();
  784. JS::RootedValue rval(cx);
  785. bool succeed = func->invoke(2, &largv[0], &rval);
  786. if (!succeed && JS_IsExceptionPending(cx)) {
  787. JS_ReportPendingException(cx);
  788. }
  789. };
  790. arg1 = lambda;
  791. } while(0);
  792. if (!ok) { ok = true; break; }
  793. cobj->login(arg0, arg1);
  794. args.rval().setUndefined();
  795. return true;
  796. }
  797. } while(0);
  798. JS_ReportError(cx, "js_pluginx_protocols_FacebookAgent_login : wrong number of arguments");
  799. return false;
  800. }
  801. bool js_pluginx_FacebookAgent_api(JSContext *cx, uint32_t argc, jsval *vp)
  802. {
  803. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  804. bool ok = true;
  805. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  806. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  807. cocos2d::plugin::FacebookAgent* cobj = (cocos2d::plugin::FacebookAgent *)(proxy ? proxy->ptr : NULL);
  808. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_FacebookAgent_api : Invalid Native Object");
  809. do {
  810. if (argc == 4) {
  811. std::string arg0;
  812. ok &= jsval_to_std_string(cx, args.get(0), &arg0);
  813. if (!ok) { ok = true; break; }
  814. int arg1;
  815. ok &= jsval_to_int32(cx, args.get(1), (int32_t *)&arg1);
  816. if (!ok) { ok = true; break; }
  817. cocos2d::plugin::TShareInfo arg2;
  818. ok &= pluginx::jsval_to_FBInfo(cx, args.get(2), &arg2);
  819. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_FacebookAgent_api : Error processing arguments");
  820. if (!ok) { ok = true; break; }
  821. std::function<void (int, std::string&)> arg3;
  822. do {
  823. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(3)));
  824. auto lambda = [=](int larg0, std::string& larg1) -> void {
  825. JSAutoCompartment ac(cx, obj);
  826. jsval largv[2];
  827. largv[0] = int32_to_jsval(cx, larg0);
  828. largv[1] = std_string_to_jsval(cx, larg1);
  829. JS::RootedValue rval(cx);
  830. bool succeed = func->invoke(2, &largv[0], &rval);
  831. if (!succeed && JS_IsExceptionPending(cx)) {
  832. JS_ReportPendingException(cx);
  833. }
  834. };
  835. arg3 = lambda;
  836. } while(0);
  837. if (!ok) { ok = true; break; }
  838. cobj->api(arg0, arg1, arg2, arg3);
  839. args.rval().setUndefined();
  840. return true;
  841. }
  842. } while(0);
  843. JS_ReportError(cx, "js_pluginx_protocols_FacebookAgent_api : wrong number of arguments");
  844. return false;
  845. }
  846. bool js_pluginx_FacebookAgent_appRequest(JSContext *cx, uint32_t argc, jsval *vp)
  847. {
  848. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  849. bool ok = true;
  850. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  851. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  852. cocos2d::plugin::FacebookAgent* cobj = (cocos2d::plugin::FacebookAgent *)(proxy ? proxy->ptr : NULL);
  853. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_FacebookAgent_appRequest : Invalid Native Object");
  854. do {
  855. if (argc == 2) {
  856. cocos2d::plugin::TShareInfo arg0;
  857. ok &= pluginx::jsval_to_TShareInfo(cx, args.get(0), &arg0);
  858. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_FacebookAgent_appRequest : Error processing arguments");
  859. if (!ok) { ok = true; break; }
  860. std::function<void (int, std::string&)> arg1;
  861. do {
  862. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  863. auto lambda = [=](int larg0, std::string& larg1) -> void {
  864. JSAutoCompartment ac(cx, obj);
  865. jsval largv[2];
  866. largv[0] = int32_to_jsval(cx, larg0);
  867. jsval temp = std_string_to_jsval(cx, larg1);
  868. JS::RootedValue outVal(cx);
  869. // size_t utf16Count = 0;
  870. // const jschar* utf16Buf = JS_GetStringCharsZAndLength(cx, JSVAL_TO_STRING(temp), &utf16Count);
  871. // JS_ParseJSON(cx, utf16Buf, static_cast<uint32_t>(utf16Count), &outVal);
  872. JS_ParseJSON(cx, JS::RootedString(cx, temp.toString()), &outVal);
  873. largv[1] = outVal.get();
  874. JS::RootedValue rval(cx);
  875. bool succeed = func->invoke(2, &largv[0], &rval);
  876. if (!succeed && JS_IsExceptionPending(cx)) {
  877. JS_ReportPendingException(cx);
  878. }
  879. };
  880. arg1 = lambda;
  881. } while(0);
  882. if (!ok) { ok = true; break; }
  883. cobj->appRequest(arg0, arg1);
  884. args.rval().setUndefined();
  885. return true;
  886. }
  887. } while(0);
  888. JS_ReportError(cx, "js_pluginx_protocols_FacebookAgent_appRequest : wrong number of arguments");
  889. return false;
  890. }
  891. bool js_pluginx_FacebookAgent_dialog(JSContext *cx, uint32_t argc, jsval *vp)
  892. {
  893. JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  894. bool ok = true;
  895. JSObject *obj = JS_THIS_OBJECT(cx, vp);
  896. js_proxy_t *proxy = jsb_get_js_proxy(obj);
  897. cocos2d::plugin::FacebookAgent* cobj = (cocos2d::plugin::FacebookAgent *)(proxy ? proxy->ptr : NULL);
  898. JSB_PRECONDITION2( cobj, cx, false, "js_pluginx_protocols_FacebookAgent_dialog : Invalid Native Object");
  899. do {
  900. if (argc == 2) {
  901. cocos2d::plugin::TShareInfo arg0;
  902. ok &= pluginx::jsval_to_TShareInfo(cx, args.get(0), &arg0);
  903. JSB_PRECONDITION2(ok, cx, false, "js_pluginx_protocols_FacebookAgent_dialog : Error processing arguments");
  904. if (!ok) { ok = true; break; }
  905. std::function<void (int, std::string&)> arg1;
  906. do {
  907. std::shared_ptr<JSFunctionWrapper> func(new JSFunctionWrapper(cx, JS_THIS_OBJECT(cx, vp), args.get(1)));
  908. auto lambda = [=](int larg0, std::string& larg1) -> void {
  909. JSAutoCompartment ac(cx, obj);
  910. jsval largv[2];
  911. largv[0] = int32_to_jsval(cx, larg0);
  912. jsval temp = std_string_to_jsval(cx, larg1);
  913. JS::RootedValue outVal(cx);
  914. // size_t utf16Count = 0;
  915. // const jschar* utf16Buf = JS_GetStringCharsZAndLength(cx, JSVAL_TO_STRING(temp), &utf16Count);
  916. // JS_ParseJSON(cx, utf16Buf, static_cast<uint32_t>(utf16Count), &outVal);
  917. JS_ParseJSON(cx, JS::RootedString(cx, temp.toString()), &outVal);
  918. largv[1] = outVal.get();
  919. JS::RootedValue rval(cx);
  920. bool succeed = func->invoke(2, &largv[0], &rval);
  921. if (!succeed && JS_IsExceptionPending(cx)) {
  922. JS_ReportPendingException(cx);
  923. }
  924. };
  925. arg1 = lambda;
  926. } while(0);
  927. if (!ok) { ok = true; break; }
  928. cobj->dialog(arg0, arg1);
  929. args.rval().setUndefined();
  930. return true;
  931. }
  932. } while(0);
  933. JS_ReportError(cx, "js_pluginx_protocols_FacebookAgent_dialog : wrong number of arguments");
  934. return false;
  935. }