zoukankan      html  css  js  c++  java
  • Cocos2d-x使用Javascript开发js绑定C++<代码演示样例>

    class IOSiAPDelegate{

        

    public:

        virtual ~IOSiAPDelegate() {}

    };


    class IOSAlipay{

        

    public:

        IOSAlipay();

        ~IOSAlipay();

        void iOSProductByIdentifier(std::string &identifier,std::string amount);

        IOSiAPDelegate *delegate;

    };




    #pragma mark-------------------------IOSAlipay的实现代理方法--------------------------------

    IOSAlipay::IOSAlipay():delegate(nil){}

    IOSAlipay::~IOSAlipay(){}

    void IOSAlipay::iOSProductByIdentifier(std::string &identifier,std::string amount){

        NSLog(@"%s",__FUNCTION__);

        //将产品id存入缓存中

        CCUserDefault* config = CCUserDefault::sharedUserDefault();//先读

        config->setStringForKey ("productIdentifier" , identifier);//存入

        config->flush();//刷新一下

        config->setStringForKey("amount", amount);//将金额传入

        

    }

    #pragma mark-------------------------IOSAlipay的实现代理方法--------------------------------





    #ifndef __iAP_JSBinding__IOSiAP_JSB__

    #define __iAP_JSBinding__IOSiAP_JSB__


    #include "jsapi.h"


    void JSB_register_iOSAlipay(JSContext* cx, JSObject* obj);


    #endif /* defined(__iAP_JSBinding__IOSiAP_JSB__) */






    #include <stdio.h>


    #include "IOSAlipay.h"

    #include "ScriptingCore.h"


    class IOSAlipay_Bridge : public IOSiAPDelegate

    {

    public:

        IOSAlipay_Bridge();

        ~IOSAlipay_Bridge();

        IOSAlipay *iap;

        JSObject *jsobj;

    };


    IOSAlipay_Bridge::IOSAlipay_Bridge(){

        iap = new IOSAlipay();

        iap->delegate = this;

    }


    IOSAlipay_Bridge::~IOSAlipay_Bridge(){

        delete iap;

    }

    static JSClass *JSB_IOSAlipay_class = NULL;

    static JSObject *JSB_IOSAlipay_object = NULL;

    #pragma mark-----绑定的析构函数

    static void JSB_IOSAlipay_finalize(JSFreeOp *fop, JSObject *obj)

    {

        IOSAlipay_Bridge *bridge = (IOSAlipay_Bridge *)JS_GetPrivate(obj);

        JS_SetPrivate(obj, NULL);

        delete bridge;

    }

    #pragma mark------初始化构造函数

    static JSBool JSB_IOSAlipay_constructor(JSContext *cx, unsigned argc, JS::Value *vp)

    {

        if (argc == 0) {

            JSObject *jsobj = JS_NewObject(cx, JSB_IOSAlipay_class, JSB_IOSAlipay_object, NULL);

            IOSAlipay_Bridge *bridge = new IOSAlipay_Bridge();

            bridge->jsobj = jsobj;

            JS_SetPrivate(jsobj, (void *)bridge);

            JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(jsobj));

            return JS_TRUE;

        }

        JS_ReportError(cx, "Wrong number of arguments: %d, was expecting: %d", argc, 0);

        return JS_FALSE;

    }

    #pragma mark------  该方法主要用于处理用于绑定到js中的方法,并接收js传递过来的參数

    static JSBool JSB_IOSiAP_iOSProductByIdentifier(JSContext *cx, uint32_t argc, jsval *vp)

    {

        JSB_PRECONDITION2(argc==2, cx, JS_FALSE, "Invalid number of arguments");

        jsval *argv = JS_ARGV(cx,vp);

        

        JSObject *obj = (JSObject *)JS_THIS_OBJECT(cx, vp);

        IOSAlipay_Bridge *bridge = (IOSAlipay_Bridge *)JS_GetPrivate(obj);

        

        // safety check for type

        if (JS_FALSE == JSVAL_IS_STRING(argv[0])) {

            JS_SET_RVAL(cx, vp, JSVAL_VOID);

            return JS_TRUE;

        }

        JSString *jsIdentifier = JSVAL_TO_STRING(argv[0]);//获取产品ID

        JSStringWrapper wrapper(jsIdentifier);

        std::string identifier = wrapper.get();

        JSString *jsPrice = JSVAL_TO_STRING(argv[1]);//获得价格

        JSStringWrapper pWrapper(jsPrice);

        std::string price = pWrapper.get();

        bridge->iap->iOSProductByIdentifier(identifier,price);//传入方法參数

        

        return JS_TRUE;

    }

    #pragma mark----------------------创建处理JS绑定对象类

    static void JSB_IOSAlipay_createClass(JSContext *cx, JSObject* globalObj, const char* name)

    {

    JSB_IOSAlipay_class = (JSClass *)calloc(1, sizeof(JSClass));

        // 类型名称为 **IOSAlipay** 正式绑定到 js js 调用的名称

    JSB_IOSAlipay_class->name = name;

    JSB_IOSAlipay_class->addProperty = JS_PropertyStub;

    JSB_IOSAlipay_class->delProperty = JS_PropertyStub;//JS_DeletePropertyStub;

    JSB_IOSAlipay_class->getProperty = JS_PropertyStub;

    JSB_IOSAlipay_class->setProperty = JS_StrictPropertyStub;

    JSB_IOSAlipay_class->enumerate = JS_EnumerateStub;

    JSB_IOSAlipay_class->resolve = JS_ResolveStub;

    JSB_IOSAlipay_class->convert = JS_ConvertStub;

    JSB_IOSAlipay_class->finalize = JSB_IOSAlipay_finalize;

    JSB_IOSAlipay_class->flags = JSCLASS_HAS_PRIVATE;

    //    JSCLASS_HAS_PRIVATE;   JSCLASS_HAS_RESERVED_SLOTS(2)

        // no property for this class

    static JSPropertySpec properties[] = {

            {0, 0, 0, 0, 0}

    };

        // 设定绑定函数。函数名 "iOSProductByIdentifier",绑定函数 "JSB_IOSiAP_iOSProductByIdentifier"

        // 后面能够加入其他函数绑定,假设须要,之后以 "JS_FS_END" 结尾

        // define member function

    static JSFunctionSpec funcs[] = {

            JS_FN("iOSProductByIdentifier", JSB_IOSiAP_iOSProductByIdentifier,1, JSPROP_PERMANENT | JSPROP_ENUMERATE),

    JS_FS_END

    };

        // 这里定义而且绑定了静态函数(static),包含方法名 "create"和相应的绑定实现

    static JSFunctionSpec st_funcs[] = {

    JS_FS_END

    };

    JSB_IOSAlipay_object = JS_InitClass(

                                          cx,globalObj,

                                          NULL,

                                          JSB_IOSAlipay_class,

                                          JSB_IOSAlipay_constructor,0,//这里绑定的是构造函数的实现。也就是用js-new操作符创建的对象

                                          properties,

                                          funcs,// 函数绑定

                                          NULL,// no static properties

                                          st_funcs);// 静态函数绑定

    }

    #pragma mark ----注冊绑定方法  JS 可以找到 全部类

    /**

     *  该方法主要在 本程序中的 GameEnter.cpp中进行集体绑定

     *  sc->addRegisterCallback(JSB_register_iOSiAP);

     *

     *  @param cx  上下文

     *  @param obj 对象

     *  @return 返回值

     */

    // extern init function

    void JSB_register_iOSAlipay(JSContext* cx, JSObject* obj)

    {

        // define name space

        JSObject *myBinding = JS_NewObject(cx, NULL, NULL, NULL);

        js::RootedValue myBindingVal(cx);

        myBindingVal = OBJECT_TO_JSVAL(myBinding);

    JS_SetProperty(cx, obj, "MyBinding", (jsval *)&myBindingVal);

        // register class

        JSB_IOSAlipay_createClass(cx, myBinding, "IOSAlipay");

    }













  • 相关阅读:
    26.列表的循环遍历
    效率比较--链表
    心之距离 再回首 光年之遥远
    效率比较--集合
    效率比较--数组
    哈希表
    栈 VS 队列
    struts1 标签引入
    web 连接池配置
    zip&ftp命令
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7027448.html
Copyright © 2011-2022 走看看