void callJSDelegate(TableView* table, TableViewCell* cell, std::string jsFunctionName)
 {
     js_proxy_t * p = jsb_get_native_proxy(table);
     if (!p) return;
     
     js_proxy_t * pCellProxy = jsb_get_native_proxy(cell);
     if (!pCellProxy) return;
     
     jsval args[2];
     args[0] = OBJECT_TO_JSVAL(p->obj);
     args[1] = OBJECT_TO_JSVAL(pCellProxy->obj);
     
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 2, args, NULL);
 }
    bool callJSDelegate(TableView* table, std::string jsFunctionName, JS::MutableHandleValue retVal)
    {
        js_proxy_t * p = jsb_get_native_proxy(table);
        if (!p) return false;
        
        JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
        
        bool hasAction;
        JS::RootedValue temp_retval(cx);
        jsval dataVal = OBJECT_TO_JSVAL(p->obj);
        
        JS::RootedObject obj(cx, _JSTableViewDataSource);
        JSAutoCompartment ac(cx, obj);
        
        if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction)
        {
            if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval))
            {
                return false;
            }
            if(temp_retval == JSVAL_VOID)
            {
                return false;
            }

            JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
                                JS::HandleValueArray::fromMarkedLocation(1, &dataVal), retVal);
            return true;
        }
        return false;
    }
    bool callJSDelegate(TableView* table, ssize_t idx, std::string jsFunctionName, jsval& retVal)
    {
        js_proxy_t * p = jsb_get_native_proxy(table);
        if (!p) return false;
        
        JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
        bool hasAction;
        JS::RootedValue temp_retval(cx);
        jsval dataVal[2];
        dataVal[0] = OBJECT_TO_JSVAL(p->obj);
        dataVal[1] = ssize_to_jsval(cx,idx);
        
        JSObject* obj = _JSTableViewDataSource;
        JSAutoCompartment ac(cx, obj);
        
        if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction)
        {
            if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval))
            {
                return false;
            }
            
            if(temp_retval == JSVAL_VOID)
            {
                return false;
            }

            bool ret = JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
                                2, dataVal, &retVal);
            return ret == true ? true : false;
        }
        return false;
    }
Example #4
0
    virtual void onMessage(WebSocket* ws, const WebSocket::Data& data)
    {
        js_proxy_t * p = jsb_get_native_proxy(ws);
        if (!p) return;
        
        JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
        JSObject* jsobj = JS_NewObject(cx, NULL, NULL, NULL);
        jsval vp = c_string_to_jsval(cx, "message");
        JS_SetProperty(cx, jsobj, "type", &vp);
        
        jsval args = OBJECT_TO_JSVAL(jsobj);
        
        if (data.isBinary)
        {// data is binary
            JSObject* buffer = JS_NewArrayBuffer(cx, data.len);
            uint8_t* bufdata = JS_GetArrayBufferData(buffer);
            memcpy((void*)bufdata, (void*)data.bytes, data.len);
            jsval dataVal = OBJECT_TO_JSVAL(buffer);
            JS_SetProperty(cx, jsobj, "data", &dataVal);
        }
        else
        {// data is string
            jsval dataVal = c_string_to_jsval(cx, data.bytes);
            JS_SetProperty(cx, jsobj, "data", &dataVal);
        }

        ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "onmessage", 1, &args);
    }
JSBool js_CocosBuilder_create(JSContext *cx, uint32_t argc, jsval *vp)
{
    
    CCNodeLoaderLibrary * ccNodeLoaderLibrary = CCNodeLoaderLibrary::sharedCCNodeLoaderLibrary();
    
    ccNodeLoaderLibrary->registerCCNodeLoader("", JSLayerLoader::loader());
    
    cocos2d::extension::CCBReader * ret = new cocos2d::extension::CCBReader(ccNodeLoaderLibrary);
    ret->autorelease();
    
    jsval jsret;
    if (ret) {
        js_proxy_t *proxy = jsb_get_native_proxy(ret);
        if (proxy) {
            jsret = OBJECT_TO_JSVAL(proxy->obj);
        } else {
            // create a new js obj of that class
            proxy = js_get_or_create_proxy<cocos2d::extension::CCBReader>(cx, ret);
            jsret = OBJECT_TO_JSVAL(proxy->obj);
        }
    } else {
        jsret = JSVAL_NULL;
    }
    JS_SET_RVAL(cx, vp, jsret);
    return JS_TRUE;
    
}
inline js_proxy_t *js_get_or_create_proxy(JSContext *cx, T *native_obj) {
    auto proxy = jsb_get_native_proxy(native_obj);
    if (!proxy) {
        js_type_class_t *typeProxy = js_get_type_from_native<T>(native_obj);
        // Return NULL if can't find its type rather than making an assert.
//        assert(typeProxy);
        if (!typeProxy) {
            CCLOGINFO("Could not find the type of native object.");
            return NULL;
        }

        JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET

        JS::RootedObject proto(cx, typeProxy->proto.ref().get());
        JS::RootedObject parent(cx, typeProxy->parentProto.ref().get());
        JS::RootedObject js_obj(cx, JS_NewObject(cx, typeProxy->jsclass, proto, parent));
        proxy = jsb_new_proxy(native_obj, js_obj);
#ifdef DEBUG
        AddNamedObjectRoot(cx, &proxy->obj, typeid(*native_obj).name());
#else
        AddObjectRoot(cx, &proxy->obj);
#endif
        return proxy;
    } else {
        return proxy;
    }
    return NULL;
}
 bool callJSDelegate(TableView* table, std::string jsFunctionName, jsval& retVal)
 {
     js_proxy_t * p = jsb_get_native_proxy(table);
     if (!p) return false;
     
     JSBool hasAction;
     jsval temp_retval;
     jsval dataVal = OBJECT_TO_JSVAL(p->obj);
     
     JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
     JSObject* obj = _JSTableViewDataSource;
     
     if (JS_HasProperty(cx, obj, jsFunctionName.c_str(), &hasAction) && hasAction) {
         if(!JS_GetProperty(cx, obj, jsFunctionName.c_str(), &temp_retval)) {
             return false;
         }
         if(temp_retval == JSVAL_VOID) {
             return false;
         }
         JSAutoCompartment ac(cx, obj);
         JS_CallFunctionName(cx, obj, jsFunctionName.c_str(),
                             1, &dataVal, &retVal);
         return true;
     }
     return false;
 }
 virtual void editBoxReturn(EditBox* editBox)
 {
     js_proxy_t * p = jsb_get_native_proxy(editBox);
     if (!p) return;
     
     jsval arg = OBJECT_TO_JSVAL(p->obj);
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxReturn", 1, &arg, NULL);
 }
 virtual void scrollViewDidZoom(ScrollView* view) override
 {
     js_proxy_t * p = jsb_get_native_proxy(view);
     if (!p) return;
     
     jsval arg = OBJECT_TO_JSVAL(p->obj);
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "scrollViewDidZoom", 1, &arg, NULL);
 }
 void callJSDelegate(ScrollView* view, std::string jsFunctionName)
 {
     js_proxy_t * p = jsb_get_native_proxy(view);
     if (!p) return;
     
     jsval arg = OBJECT_TO_JSVAL(p->obj);
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), jsFunctionName.c_str(), 1, &arg, NULL);
 }
Example #11
0
static void removeJSTouchObject(JSContext *cx, Touch *x, jsval &jsret) {
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;
    void *ptr = (void*)x;
    nproxy = jsb_get_native_proxy(ptr);
    if (nproxy) {
        jsproxy = jsb_get_js_proxy(nproxy->obj);
        JS_RemoveObjectRoot(cx, &jsproxy->obj);
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
 virtual void editBoxTextChanged(EditBox* editBox, const std::string& text)
 {
     js_proxy_t * p = jsb_get_native_proxy(editBox);
     if (!p) return;
     
     jsval dataVal[2];
     dataVal[0] = OBJECT_TO_JSVAL(p->obj);
     std::string arg1 = text;
     dataVal[1] = std_string_to_jsval(ScriptingCore::getInstance()->getGlobalContext(), arg1);
     
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "editBoxTextChanged", 2, dataVal, NULL);
 }
Example #13
0
 virtual void onOpen(WebSocket* ws)
 {
     js_proxy_t * p = jsb_get_native_proxy(ws);
     if (!p) return;
     
     JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
     JSObject* jsobj = JS_NewObject(cx, NULL, NULL, NULL);
     jsval vp = c_string_to_jsval(cx, "open");
     JS_SetProperty(cx, jsobj, "type", &vp);
     
     jsval args = OBJECT_TO_JSVAL(jsobj);
     
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "onopen", 1, &args);
 }
 virtual void didFinishPickingWithResult(cocos2d::Texture2D* result)
 {
     js_proxy_t* p = jsb_get_native_proxy(this);
     jsval data;
     if(result == nullptr)
     {
         ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "didFinishPickingWithResult", 0, &data);
         return;
     }
     
     js_proxy_t *jsProxy = js_get_or_create_proxy<cocos2d::Texture2D>(ScriptingCore::getInstance()->getGlobalContext(), result);
     data = OBJECT_TO_JSVAL(jsProxy->obj);
     
     ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "didFinishPickingWithResult", 1, &data);
 }
Example #15
0
void js_jsb_iOSiapWrapper_iOSiapWrapperCallBackClass_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (iOSiapWrapperCallBackClass)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;
    jsproxy = jsb_get_js_proxy(obj);
    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        iOSiapWrapperCallBackClass *nobj = static_cast<iOSiapWrapperCallBackClass *>(nproxy->ptr);
        if (nobj)
            delete nobj;
        
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
void js_PluginFacebookJS_PluginFacebook_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (PluginFacebook)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;
    jsproxy = jsb_get_js_proxy(obj);
    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        sdkbox::PluginFacebook *nobj = static_cast<sdkbox::PluginFacebook *>(nproxy->ptr);
        if (nobj)
            delete nobj;
        
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
Example #17
0
void ls::Leafsoar::functionTest(){
    cocos2d::CCLog("function Test");
    js_proxy_t * p = jsb_get_native_proxy(this);
    jsval retval;
    JSContext* jc = ScriptingCore::getInstance()->getGlobalContext();
    
    jsval v[] = {
        v[0] = int32_to_jsval(jc, 32),
        v[1] =UINT_TO_JSVAL(88)
    };

    
    ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "callback", 2, v, &retval);
    
}
void js_cocos2d_ImagePicker_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (ImagePicker)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;
    jsproxy = jsb_get_js_proxy(obj);
    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        cocos2d::ImagePicker *nobj = static_cast<cocos2d::ImagePicker *>(nproxy->ptr);
        if (nobj)
            delete nobj;
        
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
void js_cocos2d_experimental_AudioProfile_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (AudioProfile)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;
    jsproxy = jsb_get_js_proxy(obj);
    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        cocos2d::experimental::AudioProfile *nobj = static_cast<cocos2d::experimental::AudioProfile *>(nproxy->ptr);
        if (nobj)
            delete nobj;
        
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
Example #20
0
    virtual void onClose(WebSocket* ws)
    {
        js_proxy_t * p = jsb_get_native_proxy(ws);
        if (!p) return;
        
        JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();
        JSObject* jsobj = JS_NewObject(cx, NULL, NULL, NULL);
        jsval vp = c_string_to_jsval(cx, "close");
        JS_SetProperty(cx, jsobj, "type", &vp);
        
        jsval args = OBJECT_TO_JSVAL(jsobj);
        ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(_JSDelegate), "onclose", 1, &args);

        js_proxy_t* jsproxy = jsb_get_js_proxy(p->obj);
        JS_RemoveObjectRoot(cx, &jsproxy->obj);
        jsb_remove_proxy(p, jsproxy);
        CC_SAFE_DELETE(ws);
    }
    virtual void onEvent(Ref *controlButton, Control::EventType event)
    {
        js_proxy_t* p = jsb_get_native_proxy(controlButton);
        if (!p)
        {
            log("Failed to get proxy for control button %p", controlButton);
            return;
        }

        JSContext* cx = ScriptingCore::getInstance()->getGlobalContext();

        jsval dataVal[2];
        dataVal[0] = OBJECT_TO_JSVAL(p->obj);
        int arg1 = (int)event;
        dataVal[1] = INT_TO_JSVAL(arg1);
        JS::RootedValue jsRet(cx);

        _callback->invoke(2, dataVal, &jsRet);
    }
Example #22
0
ComponentJS::ComponentJS(const std::string& scriptFileName)
: _scriptFileName(scriptFileName)
, _jsObj(nullptr)
{
    ScriptingCore* engine = ScriptingCore::getInstance();
    JSContext* cx = engine->getGlobalContext();
    // Require script
    JS::RootedValue classValue(cx);
    _succeedLoadingScript = engine->requireScript(_scriptFileName.c_str(), &classValue);
    
    if (_succeedLoadingScript)
    {
        JS::RootedObject classObj(cx, classValue.toObjectOrNull());
        const JSClass* theClass = JS_GetClass(classObj);
        JS::RootedValue protoValue(cx);
        JS_GetProperty(cx, classObj, "prototype", &protoValue);
        
        mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new (std::nothrow) mozilla::Maybe<JS::PersistentRootedObject>();
        
        js_type_class_t *typeClass = js_get_type_from_native<cocos2d::ComponentJS>(this);
        JS::RootedObject proto(cx, protoValue.toObjectOrNull());
        JS::RootedObject parent(cx, typeClass->proto.ref());
        jsObj->construct(cx);
        JS::RootedObject obj(cx, JS_NewObject(cx, theClass, proto, parent));
        jsObj->ref() = obj;
        
        // Unbind current proxy binding
        js_proxy_t* nproxy = jsb_get_native_proxy(this);
        if (nproxy)
        {
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
            JS::RemoveObjectRoot(cx, &nproxy->obj);
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
            jsb_remove_proxy(nproxy, jsb_get_js_proxy(nproxy->obj));
        }
        // link the native object with the javascript object
        jsb_new_proxy(this, jsObj->ref());
        
        _jsObj = jsObj;
    }
}
void js_PluginIAPJS_IAP_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (IAP)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;

#if (COCOS2D_VERSION >= 0x00031000)
    JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
    JS::RootedObject jsobj(cx, obj);
    jsproxy = jsb_get_js_proxy(jsobj);
#else
    jsproxy = jsb_get_js_proxy(obj);
#endif

    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        sdkbox::IAP *nobj = static_cast<sdkbox::IAP *>(nproxy->ptr);
        if (nobj)
            delete nobj;
        
        jsb_remove_proxy(nproxy, jsproxy);
    }
}
void js_PluginGoogleAnalyticsJS_PluginGoogleAnalytics_finalize(JSFreeOp *fop, JSObject *obj) {
    CCLOGINFO("jsbindings: finalizing JS object %p (PluginGoogleAnalytics)", obj);
    js_proxy_t* nproxy;
    js_proxy_t* jsproxy;

#if (SDKBOX_COCOS_JSB_VERSION >= 2)
    JSContext *cx = ScriptingCore::getInstance()->getGlobalContext();
    JS::RootedObject jsobj(cx, obj);
    jsproxy = jsb_get_js_proxy(jsobj);
#else
    jsproxy = jsb_get_js_proxy(obj);
#endif

    if (jsproxy) {
        nproxy = jsb_get_native_proxy(jsproxy->ptr);

        sdkbox::PluginGoogleAnalytics *nobj = static_cast<sdkbox::PluginGoogleAnalytics *>(nproxy->ptr);
        if (nobj)
            delete nobj;

        jsb_remove_proxy(nproxy, jsproxy);
    }
}