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); }
void invokeDelegate(const char* fName, const char* name, int intVal, int argc) { if (!s_cx) { return; } //cocos2d::CCDirector::getInstance()->getScheduler()->performFunctionInCocosThread([=](){ JSContext* cx = s_cx; const char* func_name = fName; JS::RootedObject obj(cx, mJsDelegate); JSAutoCompartment ac(cx, obj); #if defined(MOZJS_MAJOR_VERSION) #if MOZJS_MAJOR_VERSION >= 33 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else bool hasAction; jsval retval; JS::RootedValue func_handle(cx); #endif #elif defined(JS_VERSION) JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[2]; if (2 == argc) { dataVal[0] = c_string_to_jsval(cx, name); dataVal[1] = INT_TO_JSVAL(intVal); } else if (1 == argc) { dataVal[0] = c_string_to_jsval(cx, name); } if (JS_HasProperty(cx, obj, func_name, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, func_name, &func_handle)) { return; } if(func_handle == JSVAL_VOID) { return; } #if MOZJS_MAJOR_VERSION >= 31 if (0 == argc) { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::empty(), &retval); } else { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(argc, dataVal), &retval); } #else if (0 == argc) { JS_CallFunctionName(cx, obj, func_name, 0, nullptr, &retval); } else { JS_CallFunctionName(cx, obj, func_name, argc, dataVal, &retval); } #endif } //}); }
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); }
void c_addLogToCLI(int logType, const char * pszFormat, ...) { ScriptingCore* sc = ScriptingCore::getInstance(); if (sc) { char szBuf[kMaxLogLen+1] = {0}; va_list ap; va_start(ap, pszFormat); vsnprintf(szBuf, kMaxLogLen, pszFormat, ap); va_end(ap); jsval args[2]; args[0] = c_string_to_jsval(sc->getGlobalContext(), szBuf); args[1] = INT_TO_JSVAL(logType); sc->executeFunctionWithOwner(g_cliJSValue, "addLog", 2, args, NULL); } }
bool js_autogentestbindings_SimpleNativeClass_getAnotherMoreComplexField(JSContext *cx, uint32_t argc, jsval *vp) { JSObject *obj = JS_THIS_OBJECT(cx, vp); js_proxy_t *proxy = jsb_get_js_proxy(obj); SimpleNativeClass* cobj = (SimpleNativeClass *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, false, "js_autogentestbindings_SimpleNativeClass_getAnotherMoreComplexField : Invalid Native Object"); if (argc == 0) { const char* ret = cobj->getAnotherMoreComplexField(); jsval jsret = JSVAL_NULL; jsret = c_string_to_jsval(cx, ret); JS_SET_RVAL(cx, vp, jsret); return true; } JS_ReportError(cx, "js_autogentestbindings_SimpleNativeClass_getAnotherMoreComplexField : wrong number of arguments: %d, was expecting %d", argc, 0); return false; }
// Arguments: char* // Ret value: const char* JSBool JSB_localStorageGetItem(JSContext *cx, uint32_t argc, jsval *vp) { JSB_PRECONDITION2( argc == 1, cx, JS_FALSE, "Invalid number of arguments" ); jsval *argvp = JS_ARGV(cx,vp); JSBool ok = JS_TRUE; const char* arg0; ok &= jsval_to_charptr( cx, *argvp++, &arg0 ); JSB_PRECONDITION2(ok, cx, JS_FALSE, "Error processing arguments"); const char* ret_val; ret_val = localStorageGetItem((char*)arg0 ); jsval ret_jsval = c_string_to_jsval(cx, ret_val ? ret_val : ""); JS_SET_RVAL(cx, vp, ret_jsval ); return JS_TRUE; }
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); }
JS::Value JavascriptJavaBridge::convertReturnValue(JSContext *cx, ReturnValue retValue, ValueType type) { JS::Value ret = JSVAL_NULL; switch (type) { case TypeInteger: return INT_TO_JSVAL(retValue.intValue); case TypeFloat: return DOUBLE_TO_JSVAL((double)retValue.floatValue); case TypeBoolean: return BOOLEAN_TO_JSVAL(retValue.boolValue); case TypeString: return c_string_to_jsval(cx, retValue.stringValue->c_str(),retValue.stringValue->size()); } return ret; }
bool std_vector_string_to_jsval(JSContext* cx, const std::vector<std::string>& arr, JS::MutableHandleValue retVal) { #if defined(MOZJS_MAJOR_VERSION) and MOZJS_MAJOR_VERSION >= 52 return ::std_vector_string_to_jsval(cx, arr, retVal); #elif defined(MOZJS_MAJOR_VERSION) and MOZJS_MAJOR_VERSION >= 26 JS::Value val = ::std_vector_string_to_jsval(cx, arr); retVal.set(val); #else //support v2? JSObject *jsretArr = JS_NewArrayObject(cx, 0, NULL); int i = 0; for(std::vector<std::string>::const_iterator iter = arr.begin(); iter != arr.end(); ++iter, ++i) { jsval arrElement = c_string_to_jsval(cx, iter->c_str()); if(!JS_SetElement(cx, jsretArr, i, &arrElement)) { break; } } return OBJECT_TO_JSVAL(jsretArr); #endif }
JSBool js_cocos2dx_extension_WebSocket_constructor(JSContext *cx, uint32_t argc, jsval *vp) { jsval *argv = JS_ARGV(cx, vp); if (argc == 1 || argc == 2) { std::string url; do { JSBool ok = jsval_to_std_string(cx, argv[0], &url); JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments"); } while (0); JSObject *obj = JS_NewObject(cx, js_cocos2dx_websocket_class, js_cocos2dx_websocket_prototype, NULL); cocos2d::extension::WebSocket* cobj = new cocos2d::extension::WebSocket(); JSB_WebSocketDelegate* delegate = new JSB_WebSocketDelegate(); delegate->setJSDelegate(obj); if (argc == 2) { std::vector<std::string> protocols; if (JSVAL_IS_STRING(argv[1])) { std::string protocol; do { JSBool ok = jsval_to_std_string(cx, argv[1], &protocol); JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments"); } while (0); protocols.push_back(protocol); } else if (argv[1].isObject()) { JSBool ok = JS_TRUE; JSObject* arg2 = JSVAL_TO_OBJECT(argv[1]); JSB_PRECONDITION(JS_IsArrayObject( cx, arg2 ), "Object must be an array"); uint32_t len = 0; JS_GetArrayLength(cx, arg2, &len); for( uint32_t i=0; i< len;i++ ) { jsval valarg; JS_GetElement(cx, arg2, i, &valarg); std::string protocol; do { ok = jsval_to_std_string(cx, valarg, &protocol); JSB_PRECONDITION2( ok, cx, JS_FALSE, "Error processing arguments"); } while (0); protocols.push_back(protocol); } } cobj->init(*delegate, url, &protocols); } else { cobj->init(*delegate, url); } JS_DefineProperty(cx, obj, "URL", argv[0] , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); //protocol not support yet (always return "") JS_DefineProperty(cx, obj, "protocol", c_string_to_jsval(cx, "") , NULL, NULL, JSPROP_ENUMERATE | JSPROP_PERMANENT | JSPROP_READONLY); // link the native object with the javascript object js_proxy_t *p = jsb_new_proxy(cobj, obj); JS_AddNamedObjectRoot(cx, &p->obj, "WebSocket"); JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(obj)); return JS_TRUE; } JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0); return JS_FALSE; }
void TestLayer::testLogic(int testIndex) { if (testIndex == 1) { if (m_bSpiderMonkeyInited) { c_addLogToCLI(3, "[C++] %s", "Spidermonkey already inited!!!"); return; } // init spidermonkey ScriptingCore* sc = ScriptingCore::getInstance(); CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance(); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); sc->addRegisterCallback(register_all_cocos2dx); sc->addRegisterCallback(register_all_cocos2dx_extension); sc->addRegisterCallback(register_cocos2dx_js_extensions); sc->addRegisterCallback(register_all_cocos2dx_extension_manual); sc->addRegisterCallback(jsb_register_system); sc->addRegisterCallback(JSB_register_opengl); sc->start(); if (!JS_DefineFunctions(sc->getGlobalContext(), sc->getGlobalObject(), myjs_global_functions)) { c_addLogToCLI(4, "[C++] JS_DefineFunctions Failed!!!"); return; } sc->runScript("GlobalFuncTest.js"); sc->runScript("SpriteFuncTest.js"); m_bSpiderMonkeyInited = true; //c_addLogToCLI(1, "[C++] %s", "Spidermonkey init success"); return; } if (!m_bSpiderMonkeyInited) { //c_addLogToCLI(4, "[C++] %s", "Init SpiderMonkey First!!!"); return; } if (testIndex == 2) { // call JS function - 0 param, 0 return ScriptingCore* sc = ScriptingCore::getInstance(); sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "MyLogFunc"); c_addLogToCLI(1, "[C++] MyLogFunc called"); } else if (testIndex == 3) { // call JS function - 1 param(Basic Type) , 1 return(Basic Type) ScriptingCore* sc = ScriptingCore::getInstance(); jsval dataVal = INT_TO_JSVAL(10); jsval ret; sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "DubleIntFunc", 1, &dataVal, &ret); c_addLogToCLI(1, "[C++] DubleIntFunc called with result: %d", JSVAL_TO_INT(ret)); } else if (testIndex == 4) { // call JS function - 0 param , 1 return(cocos2d Object) ScriptingCore* sc = ScriptingCore::getInstance(); jsval ret; sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "SpawnIconSprite", 0, NULL, &ret); c_addLogToCLI(1, "[C++] SpawnIconSprite called"); if(!JSVAL_IS_NULL(ret)) { JSObject *obj = JSVAL_TO_OBJECT(ret); js_proxy_t *proxy = jsb_get_js_proxy(obj); CCSprite *sprite = (CCSprite *)(proxy ? proxy->ptr : NULL); if (sprite) { this->addChild(sprite); c_addLogToCLI(1, "[C++] Add sprite from js to c++ layer"); } } } else if (testIndex == 5 ) { // call JS function - 1 param(cocos2d Object) , 0 return ScriptingCore* sc = ScriptingCore::getInstance(); CCSprite *sprite = CCSprite::create("Icon.png"); sprite->setPosition(ccp(CCRANDOM_0_1() * 1000, CCRANDOM_0_1() * 1000)); this->addChild(sprite); js_proxy_t *p = js_get_or_create_proxy<cocos2d::CCSprite>(sc->getGlobalContext(), sprite); jsval dataVal = OBJECT_TO_JSVAL(p->obj); sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "DoubleSpriteSize", 1, &dataVal, NULL); c_addLogToCLI(1, "[C++] Add sprite from js to layer"); } else if (testIndex == 6) { // Bind Simple UI component to c++ CCScene ScriptingCore* sc = ScriptingCore::getInstance(); sc->runScript("CircleLabelTTFTest.js"); c_addLogToCLI(1, "[C++] %s", "Run CircleLabelTTFTest script"); c_addLogToCLI(1, "[C++] %s", "Bind UI Component to CCSCene"); } else if (testIndex == 7) { // Bind Complex UI component to c++ CCScene ScriptingCore* sc = ScriptingCore::getInstance(); sc->runScript("CalendarTest.js"); c_addLogToCLI(1, "[C++] %s", "Run CalendarTest script"); c_addLogToCLI(1, "[C++] %s", "Bind UI Component to CCSCene"); } else if (testIndex == 8) { // call JS function - N param(Mix) , 0 return ScriptingCore* sc = ScriptingCore::getInstance(); js_proxy_t *p = js_get_or_create_proxy<cocos2d::CCLayer>(sc->getGlobalContext(), this); jsval layerVal = OBJECT_TO_JSVAL(p->obj); jsval nowStringVal = c_string_to_jsval(sc->getGlobalContext(), "2013-7-27"); jsval minYearVal = INT_TO_JSVAL(2010); jsval maxYearVal = INT_TO_JSVAL(2016); jsval args[4]; args[0] = layerVal; args[1] = minYearVal; args[2] = maxYearVal; args[3] = nowStringVal; sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "AddCalendarToLayer", 4, args, NULL); c_addLogToCLI(1, "[C++] %s", "Bind Calendar UI to speicfied layer"); } else if (testIndex == 9) { // call JS object function ScriptingCore* sc = ScriptingCore::getInstance(); js_proxy_t *p = js_get_or_create_proxy<cocos2d::CCLayer>(sc->getGlobalContext(), this); jsval layerVal = OBJECT_TO_JSVAL(p->obj); jsval stringVal = c_string_to_jsval(sc->getGlobalContext(), "center"); CCArray *array = CCArray::create(); array->addObject(CCInteger::create(111)); array->addObject(CCInteger::create(222)); array->addObject(CCInteger::create(333)); array->addObject(CCInteger::create(444)); array->addObject(CCInteger::create(555)); array->addObject(CCInteger::create(666)); array->addObject(CCInteger::create(777)); array->addObject(CCInteger::create(888)); jsval arrayVal = ccarray_to_jsval(sc->getGlobalContext(), array); jsval radiusVal = INT_TO_JSVAL(200); jsval args[4]; args[0] = layerVal; args[1] = stringVal; args[2] = arrayVal; args[3] = radiusVal; jsval ret; sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "AddCircleTTFSpriteToLayer", 4, args, &ret); c_addLogToCLI(1, "[C++] %s", "Bind CircleTTF UI to speicfied layer"); if(!JSVAL_IS_NULL(ret)) { sc->executeFunctionWithOwner(ret, "expandTTF", 0, NULL, NULL); c_addLogToCLI(1, "[C++] %s", "Call CircleTTF function expandTTF"); } } else if (testIndex == 10) { // Bind UI Component (CLI) ScriptingCore* sc = ScriptingCore::getInstance(); js_proxy_t *p = js_get_or_create_proxy<cocos2d::CCLayer>(sc->getGlobalContext(), this); jsval layerVal = OBJECT_TO_JSVAL(p->obj); sc->executeFunctionWithOwner(OBJECT_TO_JSVAL(sc->getGlobalObject()), "BindCLILayerTo", 1, &layerVal, &g_cliJSValue); c_addLogToCLI(1, "[C++] %s", "Bind CLI Layer to current layer"); m_bCLIBound = true; } else { c_addLogToCLI(4, "[C++] TEST NOT FOUND!!!"); } }
jsval std_string_to_jsval(JSContext* cx, const std::string& v) { return c_string_to_jsval(cx, v.data()); }