axmol/plugin/jsbindings/manual/jsb_pluginx_manual_callback...

238 lines
7.2 KiB
C++
Raw Normal View History

#include "jsb_pluginx_manual_callback.h"
2013-04-07 16:58:26 +08:00
#include "jsb_pluginx_basic_conversions.h"
#include "jsb_pluginx_spidermonkey_specifics.h"
2013-04-24 09:43:40 +08:00
#include "ProtocolAds.h"
2013-04-07 16:58:26 +08:00
using namespace pluginx;
static JSContext* s_cx = NULL;
class Pluginx_PurchaseResult : public cocos2d::plugin::PayResultListener
{
public:
2013-04-27 11:32:12 +08:00
virtual void onPayResult(cocos2d::plugin::PayResultCode ret, const char* msg, cocos2d::plugin::TProductInfo info)
2013-04-07 16:58:26 +08:00
{
char goodInfo[1024] = { 0 };
sprintf(goodInfo, "商品名称:%s\n商品价格:%s\n商品描述:%s",
info.find("productName")->second.c_str(),
info.find("productPrice")->second.c_str(),
info.find("productDesc")->second.c_str());
LOGD(goodInfo);
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
jsval dataVal[3];
dataVal[0] = INT_TO_JSVAL(ret);
std::string strMsgInfo = msg;
dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
dataVal[2] = TProductInfo_to_jsval(cx, info);
JSObject* obj = m_pJSDelegate;
2013-04-27 11:32:12 +08:00
if (JS_HasProperty(cx, obj, "onPayResult", &hasAction) && hasAction) {
if(!JS_GetProperty(cx, obj, "onPayResult", &temp_retval)) {
2013-04-07 16:58:26 +08:00
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
2013-04-27 11:32:12 +08:00
JS_CallFunctionName(cx, obj, "onPayResult",
2013-04-07 16:58:26 +08:00
3, dataVal, &retval);
}
}
void setJSDelegate(JSObject* pJSDelegate)
{
m_pJSDelegate = pJSDelegate;
}
private:
JSObject* m_pJSDelegate;
};
JSBool js_pluginx_ProtocolIAP_setResultListener(JSContext *cx, uint32_t argc, jsval *vp)
{
s_cx = cx;
jsval *argv = JS_ARGV(cx, vp);
2013-04-27 11:32:12 +08:00
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy; JS_GET_NATIVE_PROXY(proxy, obj);
cocos2d::plugin::ProtocolIAP* cobj = (cocos2d::plugin::ProtocolIAP *)(proxy ? proxy->ptr : NULL);
2013-04-07 16:58:26 +08:00
JSBool ok = JS_TRUE;
if (argc == 1) {
// save the delegate
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
Pluginx_PurchaseResult* nativeDelegate = new Pluginx_PurchaseResult();
nativeDelegate->setJSDelegate(jsDelegate);
2013-04-27 11:32:12 +08:00
cobj->setResultListener(nativeDelegate);
2013-04-07 16:58:26 +08:00
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return JS_FALSE;
}
2013-04-27 11:32:12 +08:00
class Pluginx_AdsListener : public cocos2d::plugin::AdsListener
2013-04-07 16:58:26 +08:00
{
public:
2013-04-27 11:32:12 +08:00
virtual void onAdsResult(AdsResultCode code, const char* msg)
2013-04-24 09:43:40 +08:00
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
2013-04-27 11:32:12 +08:00
jsval dataVal[2];
dataVal[0] = INT_TO_JSVAL(code);
std::string strMsgInfo = msg;
dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
2013-04-24 09:43:40 +08:00
JSObject* obj = m_pJSDelegate;
2013-04-27 11:32:12 +08:00
JSBool bRet = JS_HasProperty(cx, obj, "onAdsResult", &hasAction);
2013-04-24 09:43:40 +08:00
if (bRet && hasAction) {
2013-04-27 11:32:12 +08:00
if(!JS_GetProperty(cx, obj, "onAdsResult", &temp_retval)) {
2013-04-24 09:43:40 +08:00
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
2013-04-27 11:32:12 +08:00
JS_CallFunctionName(cx, obj, "onAdsResult",
2, dataVal, &retval);
2013-04-24 09:43:40 +08:00
}
}
2013-04-27 11:32:12 +08:00
virtual void onPlayerGetPoints(ProtocolAds* pAdsPlugin, int points)
2013-04-24 09:43:40 +08:00
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
2013-04-27 11:32:12 +08:00
js_proxy_t * p;
JS_GET_PROXY(p, pAdsPlugin);
if (! p) return;
jsval dataVal[2];
jsval arg = OBJECT_TO_JSVAL(p->obj);
dataVal[0] = arg;
dataVal[1] = INT_TO_JSVAL(points);
2013-04-24 09:43:40 +08:00
JSObject* obj = m_pJSDelegate;
2013-04-27 11:32:12 +08:00
JSBool bRet = JS_HasProperty(cx, obj, "onPlayerGetPoints", &hasAction);
2013-04-24 09:43:40 +08:00
if (bRet && hasAction) {
2013-04-27 11:32:12 +08:00
if(!JS_GetProperty(cx, obj, "onPlayerGetPoints", &temp_retval)) {
2013-04-24 09:43:40 +08:00
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
2013-04-27 11:32:12 +08:00
JS_CallFunctionName(cx, obj, "onPlayerGetPoints",
2, NULL, &retval);
2013-04-24 09:43:40 +08:00
}
}
2013-04-27 11:32:12 +08:00
void setJSDelegate(JSObject* pJSDelegate)
2013-04-07 16:58:26 +08:00
{
2013-04-27 11:32:12 +08:00
m_pJSDelegate = pJSDelegate;
}
2013-04-07 16:58:26 +08:00
2013-04-27 11:32:12 +08:00
private:
JSObject* m_pJSDelegate;
};
JSBool js_pluginx_ProtocolAds_setAdsListener(JSContext *cx, uint32_t argc, jsval *vp)
{
s_cx = cx;
jsval *argv = JS_ARGV(cx, vp);
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy; JS_GET_NATIVE_PROXY(proxy, obj);
cocos2d::plugin::ProtocolAds* cobj = (cocos2d::plugin::ProtocolAds *)(proxy ? proxy->ptr : NULL);
JSBool ok = JS_TRUE;
if (argc == 1) {
// save the delegate
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
Pluginx_AdsListener* nativeDelegate = new Pluginx_AdsListener();
nativeDelegate->setJSDelegate(jsDelegate);
cobj->setAdsListener(nativeDelegate);
2013-04-07 16:58:26 +08:00
2013-04-27 11:32:12 +08:00
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
2013-04-07 16:58:26 +08:00
}
2013-04-27 11:32:12 +08:00
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return JS_FALSE;
}
class Pluginx_ShareResult : public cocos2d::plugin::ShareResultListener
{
public:
virtual void onShareResult(cocos2d::plugin::ShareResultCode ret, const char* msg)
2013-04-24 09:43:40 +08:00
{
JSContext* cx = s_cx;
JSBool hasAction;
jsval retval;
jsval temp_retval;
2013-04-27 11:32:12 +08:00
jsval dataVal[2];
dataVal[0] = INT_TO_JSVAL(ret);
std::string strMsgInfo = msg;
dataVal[1] = std_string_to_jsval(cx, strMsgInfo);
2013-04-24 09:43:40 +08:00
JSObject* obj = m_pJSDelegate;
2013-04-27 11:32:12 +08:00
if (JS_HasProperty(cx, obj, "onShareResult", &hasAction) && hasAction) {
if(!JS_GetProperty(cx, obj, "onShareResult", &temp_retval)) {
2013-04-24 09:43:40 +08:00
return;
}
if(temp_retval == JSVAL_VOID) {
return;
}
JSAutoCompartment ac(cx, obj);
2013-04-27 11:32:12 +08:00
JS_CallFunctionName(cx, obj, "onShareResult",
2, dataVal, &retval);
2013-04-24 09:43:40 +08:00
}
}
2013-04-07 16:58:26 +08:00
void setJSDelegate(JSObject* pJSDelegate)
{
m_pJSDelegate = pJSDelegate;
}
private:
JSObject* m_pJSDelegate;
};
2013-04-27 11:32:12 +08:00
JSBool js_pluginx_ProtocolSocial_setResultListener(JSContext *cx, uint32_t argc, jsval *vp)
2013-04-07 16:58:26 +08:00
{
s_cx = cx;
jsval *argv = JS_ARGV(cx, vp);
2013-04-27 11:32:12 +08:00
JSObject *obj = JS_THIS_OBJECT(cx, vp);
js_proxy_t *proxy; JS_GET_NATIVE_PROXY(proxy, obj);
cocos2d::plugin::ProtocolSocial* cobj = (cocos2d::plugin::ProtocolSocial *)(proxy ? proxy->ptr : NULL);
2013-04-07 16:58:26 +08:00
JSBool ok = JS_TRUE;
2013-04-27 11:32:12 +08:00
2013-04-07 16:58:26 +08:00
if (argc == 1) {
// save the delegate
JSObject *jsDelegate = JSVAL_TO_OBJECT(argv[0]);
2013-04-27 11:32:12 +08:00
Pluginx_ShareResult* nativeDelegate = new Pluginx_ShareResult();
2013-04-07 16:58:26 +08:00
nativeDelegate->setJSDelegate(jsDelegate);
2013-04-27 11:32:12 +08:00
cobj->setResultListener(nativeDelegate);
2013-04-07 16:58:26 +08:00
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 1);
return JS_FALSE;
}