void JSArmatureWrapper::movementCallbackFunc(cocostudio::Armature *armature, cocostudio::MovementEventType movementType, const std::string& movementID) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject *thisObj = JSVAL_IS_VOID(_jsThisObj) ? NULL : JSVAL_TO_OBJECT(_jsThisObj); js_proxy_t *proxy = js_get_or_create_proxy(cx, armature); jsval retval; if (_jsCallback != JSVAL_VOID) { int movementEventType = (int)movementType; jsval movementVal = INT_TO_JSVAL(movementEventType); jsval idVal = std_string_to_jsval(cx, movementID); jsval valArr[3]; valArr[0] = OBJECT_TO_JSVAL(proxy->obj); valArr[1] = movementVal; valArr[2] = idVal; JS_AddValueRoot(cx, valArr); JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS_CallFunctionValue(cx, thisObj, _jsCallback, 3, valArr, &retval); JS_RemoveValueRoot(cx, valArr); } }
void JSArmatureWrapper::frameCallbackFunc(cocostudio::Bone *bone, const std::string& evt, int originFrameIndex, int currentFrameIndex) { JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject *thisObj = JSVAL_IS_VOID(_jsThisObj) ? NULL : JSVAL_TO_OBJECT(_jsThisObj); js_proxy_t *proxy = js_get_or_create_proxy(cx, bone); jsval retval; if (_jsCallback != JSVAL_VOID) { jsval nameVal = std_string_to_jsval(cx, evt); jsval originIndexVal = INT_TO_JSVAL(originFrameIndex); jsval currentIndexVal = INT_TO_JSVAL(currentFrameIndex); jsval valArr[4]; valArr[0] = OBJECT_TO_JSVAL(proxy->obj); valArr[1] = nameVal; valArr[2] = originIndexVal; valArr[3] = currentIndexVal; JS_AddValueRoot(cx, valArr); JS_CallFunctionValue(cx, thisObj, _jsCallback, 4, valArr, &retval); JS_RemoveValueRoot(cx, valArr); } }
void callbackFunc(CCNode *node) const { jsval valArr[2]; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::CCNode>(cx, node); JS_AddValueRoot(cx, valArr); valArr[0] = OBJECT_TO_JSVAL(proxy->obj); if(!JSVAL_IS_VOID(extraData)) { valArr[1] = extraData; } else { valArr[1] = JSVAL_NULL; } jsval retval; if(jsCallback != JSVAL_VOID && jsThisObj != JSVAL_VOID) { JS_CallFunctionValue(cx, JSVAL_TO_OBJECT(jsThisObj), jsCallback, 2, valArr, &retval); } JSCallFuncWrapper::setTargetForNativeNode(node, (JSCallFuncWrapper *)this); JS_RemoveValueRoot(cx, valArr); }
void postEvent (JSContext* jsCtx, AmityEvent* event) { JS_AddObjectRoot(jsCtx, &event->obj); JS_AddValueRoot(jsCtx, &event->param); SDL_Event sdlEvent; sdlEvent.type = SDL_USEREVENT; sdlEvent.user.code = AMITY_EVENT_SDL_CODE; sdlEvent.user.data1 = event; sdlEvent.user.data2 = NULL; SDL_PushEvent(&sdlEvent); }
void JSArmatureWrapper::addArmatureFileInfoAsyncCallbackFunc(float percent) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject *thisObj = JSVAL_IS_VOID(_jsThisObj) ? NULL : JSVAL_TO_OBJECT(_jsThisObj); jsval retval; if (_jsCallback != JSVAL_VOID) { jsval percentVal = DOUBLE_TO_JSVAL(percent); JS_AddValueRoot(cx, &percentVal); JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS_CallFunctionValue(cx, thisObj, _jsCallback, 1, &percentVal, &retval); JS_RemoveValueRoot(cx, &percentVal); } }
void scheduleFunc(float dt) const { jsval retval = JSVAL_NULL, data = DOUBLE_TO_JSVAL(dt); JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSBool ok = JS_AddValueRoot(cx, &data); if(!ok) { return; } if(!JSVAL_IS_VOID(jsCallback) && !JSVAL_IS_VOID(jsThisObj)) { JS_CallFunctionValue(cx, JSVAL_TO_OBJECT(jsThisObj), jsCallback, 1, &data, &retval); } JS_RemoveValueRoot(cx, &data); }
void __JSDownloaderDelegator::onError(const cocos2d::extension::Downloader::Error &error) { if (!JSVAL_IS_NULL(_jsCallback)) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject *global = ScriptingCore::getInstance()->getGlobalObject(); JSAutoCompartment ac(_cx, _obj); jsval succeed = BOOLEAN_TO_JSVAL(false); jsval retval; JS_AddValueRoot(cx, &succeed); JS_CallFunctionValue(cx, global, _jsCallback, 1, &succeed, &retval); JS_RemoveValueRoot(cx, &succeed); JS_RemoveValueRoot(cx, &_jsCallback); } this->release(); }
void IOSiAP_Bridge::onRequestProductsError(int code) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); jsval param[] = { INT_TO_JSVAL(code) }; jsval retval; JSBool ok = JS_AddValueRoot(cx, param); if (!ok) { CCLOG("scheduleFunc: Root value fails."); return; } ScriptingCore::getInstance()->executeFunctionWithOwner( OBJECT_TO_JSVAL(this->jsobj), "onRequestProductsError", 1, param, &retval); JS_RemoveValueRoot(cx, param); }
void __JSDownloaderDelegator::onSuccess(const std::string &srcUrl, const std::string &storagePath, const std::string &customId) { Image *image = new Image(); jsval valArr[2]; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSObject *global = ScriptingCore::getInstance()->getGlobalObject(); JSAutoCompartment ac(_cx, _obj); if(image->initWithImageData(_buffer, _size)) { Texture2D *tex = Director::getInstance()->getTextureCache()->addImage(image, srcUrl); valArr[0] = BOOLEAN_TO_JSVAL(true); js_type_class_t *classType = js_get_type_from_native<cocos2d::Texture2D>(tex); assert(classType); JSObject *obj = JS_NewObject(cx, classType->jsclass, classType->proto, classType->parentProto); // link the native object with the javascript object js_proxy_t* p = jsb_new_proxy(tex, obj); JS_AddNamedObjectRoot(cx, &p->obj, "cocos2d::Texture2D"); valArr[1] = OBJECT_TO_JSVAL(p->obj); } else { valArr[0] = BOOLEAN_TO_JSVAL(false); valArr[1] = JSVAL_NULL; } image->release(); if (!JSVAL_IS_NULL(_jsCallback)) { jsval retval; JS_AddValueRoot(cx, valArr); JS_CallFunctionValue(cx, global, _jsCallback, 2, valArr, &retval); JS_RemoveValueRoot(cx, valArr); JS_RemoveValueRoot(cx, &_jsCallback); } this->release(); }
void IOSiAP_Bridge::onPaymentEvent(std::string &identifier, IOSiAPPaymentEvent event, int quantity) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); JSString *str = JS_InternString(cx, identifier.c_str()); jsval param[] = { STRING_TO_JSVAL(str), INT_TO_JSVAL(event), INT_TO_JSVAL(quantity) }; jsval retval; JSBool ok = JS_AddValueRoot(cx, param); if (!ok) { CCLOG("scheduleFunc: Root value fails."); return; } ScriptingCore::getInstance()->executeFunctionWithOwner( OBJECT_TO_JSVAL(this->jsobj), "onPaymentEvent", 3, param, &retval); JS_RemoveValueRoot(cx, param); }
void JsSchedule::SetScheduleCallback(jsval jsCallback) { this->jsScheduleCallback = jsCallback; JS_AddValueRoot(JavaScriptEngine::ShareInstance().ShareContext(), &this->jsScheduleCallback); }
void gjstest_test_func_gjs_jsapi_util_error_throw(void) { GjsUnitTestFixture fixture; JSContext *context; jsval exc, value, previous; char *s = NULL; int strcmp_result; _gjs_unit_test_fixture_begin(&fixture); context = fixture.context; /* Test that we can throw */ gjs_throw(context, "This is an exception %d", 42); g_assert(JS_IsExceptionPending(context)); exc = JSVAL_VOID; JS_GetPendingException(context, &exc); g_assert(!JSVAL_IS_VOID(exc)); value = JSVAL_VOID; JS_GetProperty(context, JSVAL_TO_OBJECT(exc), "message", &value); g_assert(JSVAL_IS_STRING(value)); gjs_string_get_binary_data (context, value, &s, NULL); g_assert(s != NULL); strcmp_result = strcmp(s, "This is an exception 42"); free(s); if (strcmp_result != 0) g_error("Exception has wrong message '%s'", s); /* keep this around before we clear it */ previous = exc; JS_AddValueRoot(context, &previous); JS_ClearPendingException(context); g_assert(!JS_IsExceptionPending(context)); /* Check that we don't overwrite a pending exception */ JS_SetPendingException(context, previous); g_assert(JS_IsExceptionPending(context)); gjs_throw(context, "Second different exception %s", "foo"); g_assert(JS_IsExceptionPending(context)); exc = JSVAL_VOID; JS_GetPendingException(context, &exc); g_assert(!JSVAL_IS_VOID(exc)); g_assert(exc == previous); JS_RemoveValueRoot(context, &previous); _gjs_unit_test_fixture_finish(&fixture); }
static void closure_marshal(GClosure *closure, GValue *return_value, guint n_param_values, const GValue *param_values, gpointer invocation_hint, gpointer marshal_data) { JSRuntime *runtime; JSContext *context; int argc; jsval *argv; jsval rval; int i; GSignalQuery signal_query = { 0, }; gjs_debug_marshal(GJS_DEBUG_GCLOSURE, "Marshal closure %p", closure); if (!gjs_closure_is_valid(closure)) { /* We were destroyed; become a no-op */ return; } runtime = gjs_closure_get_runtime(closure); context = gjs_runtime_get_current_context(runtime); JS_BeginRequest(context); argc = n_param_values; argv = g_newa(jsval, n_param_values); rval = JSVAL_VOID; gjs_set_values(context, argv, argc, JSVAL_VOID); gjs_root_value_locations(context, argv, argc); JS_AddValueRoot(context, &rval); if (marshal_data) { /* we are used for a signal handler */ guint signal_id; signal_id = GPOINTER_TO_UINT(marshal_data); g_signal_query(signal_id, &signal_query); if (!signal_query.signal_id) { gjs_debug(GJS_DEBUG_GCLOSURE, "Signal handler being called on invalid signal"); goto cleanup; } if (signal_query.n_params + 1 != n_param_values) { gjs_debug(GJS_DEBUG_GCLOSURE, "Signal handler being called with wrong number of parameters"); goto cleanup; } } for (i = 0; i < argc; ++i) { const GValue *gval = ¶m_values[i]; gboolean no_copy; no_copy = FALSE; if (i >= 1 && signal_query.signal_id) { no_copy = (signal_query.param_types[i - 1] & G_SIGNAL_TYPE_STATIC_SCOPE) != 0; } if (!gjs_value_from_g_value_internal(context, &argv[i], gval, no_copy, &signal_query, i)) { gjs_debug(GJS_DEBUG_GCLOSURE, "Unable to convert arg %d in order to invoke closure", i); gjs_log_exception(context, NULL); goto cleanup; } } gjs_closure_invoke(closure, argc, argv, &rval); if (return_value != NULL) { if (JSVAL_IS_VOID(rval)) { /* something went wrong invoking, error should be set already */ goto cleanup; } if (!gjs_value_to_g_value(context, rval, return_value)) { gjs_debug(GJS_DEBUG_GCLOSURE, "Unable to convert return value when invoking closure"); gjs_log_exception(context, NULL); goto cleanup; } } cleanup: gjs_unroot_value_locations(context, argv, argc); JS_RemoveValueRoot(context, &rval); JS_EndRequest(context); }
JSBool JsGlobal::event_GetProperty (JSContext *cx, JSObject *obj, jsid id, jsval *vp) { fsm::StateMachine * pstateMachine = NULL; static log4cplus::Logger log = log4cplus::Logger::getInstance("TUserManager.GetProperty"); pstateMachine = (fsm::StateMachine *)JS_GetContextPrivate(cx); if (!pstateMachine){ LOG4CPLUS_WARN(log,"GetContextPrivate is null."); } if (!JSID_IS_INT(id)) return JS_TRUE; int proid = JSID_TO_INT(id); jsval *val = (jsval *) JS_GetPrivate(cx, obj); if (val) { if(JSVAL_IS_NULL(*vp) || JSVAL_IS_VOID(*vp)) *vp = val[proid]; return JS_TRUE; } val = new jsval[4]; if (!val) { JS_ReportOutOfMemory(cx); JS_SET_RVAL(cx, vp, JSVAL_VOID); return JS_TRUE; } if (!JS_AddValueRoot(cx, val)) { delete[] val; JS_SET_RVAL(cx, vp, JSVAL_VOID); return JS_TRUE; } if (!JS_SetPrivate(cx, obj, (void*)val)) { JS_RemoveValueRoot(cx, val); delete[] val; JS_SET_RVAL(cx, vp, JSVAL_VOID); return JS_TRUE; } std::string prefix = "getting [_event] property:"; fsm::env::Js::IdToString idString(cx, id); if(pstateMachine ){ val[name] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->m_currentEvt.getEventName().c_str())); //val[bodydata] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->m_currentEvt.getData().c_str())); //val[messagetype] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->m_currentEvt.getMsgType().c_str())); //val[ip] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->m_currentEvt.getIP().c_str())); //val[port] = INT_TO_JSVAL(pstateMachine->m_currentEvt.getPort()); val[serviceid] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->getName().c_str())); val[sessionid] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->getSessionId().c_str())); val[callid] = STRING_TO_JSVAL(JS_NewStringCopyZ (cx,pstateMachine->getSessionId().c_str())); switch (proid) { case name: prefix.append("_name"); break; case serviceid: prefix.append("_serviceid"); break; case sessionid: prefix.append("_sessionid"); break; case callid: prefix.append("_callid"); break; //case from:{ // vp.setInt32(pstateMachine->getFrom().c_str()); // break; //case Enable: // vp.setBoolean(extPtr->m_bEnable); // break; default: prefix.append(idString.getBytes()); break; } }else{ prefix.append(idString.getBytes()); } fsm::env::Js::ToString valueString(cx, val[proid]); //fsm::env::Js::ToString objString(cx,OBJECT_TO_JSVAL(obj)); *vp = val[proid]; LOG4CPLUS_DEBUG(log,prefix<< ",value:" << valueString.getBytes()); return JS_TRUE; }