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, 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; }
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; }
bool js_cocos2dx_DrawNode3D_constructor(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; cocos2d::DrawNode3D* cobj = new (std::nothrow) cocos2d::DrawNode3D(); cobj->init(); cocos2d::Ref *_ccobj = dynamic_cast<cocos2d::Ref *>(cobj); if (_ccobj) { _ccobj->autorelease(); } TypeTest<cocos2d::DrawNode3D> t; js_type_class_t *typeClass = nullptr; std::string typeName = t.s_name(); auto typeMapIter = _js_global_type_map.find(typeName); CCASSERT(typeMapIter != _js_global_type_map.end(), "Can't find the class type!"); typeClass = typeMapIter->second; CCASSERT(typeClass, "The value is null."); // JSObject *obj = JS_NewObject(cx, typeClass->jsclass, typeClass->proto, typeClass->parentProto); JS::RootedObject proto(cx, typeClass->proto.get()); JS::RootedObject parent(cx, typeClass->parentProto.get()); JS::RootedObject obj(cx, JS_NewObject(cx, typeClass->jsclass, proto, parent)); args.rval().set(OBJECT_TO_JSVAL(obj)); // link the native object with the javascript object js_proxy_t* p = jsb_new_proxy(cobj, obj); AddNamedObjectRoot(cx, &p->obj, "cocos2d::DrawNode3D"); if (JS_HasProperty(cx, obj, "_ctor", &ok) && ok) ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), "_ctor", args); return true; }
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 } //}); }
void GLNode::onDraw(Mat4 &transform, uint32_t flags) { JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); js_type_class_t *typeClass = js_get_type_from_native<cocos2d::GLNode>(this); JS::RootedObject jsObj(cx, jsb_ref_get_or_create_jsobject(cx, this, typeClass, "cocos2d::GLNode")); if (jsObj.get()) { bool found = false; JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS_HasProperty(cx, jsObj, "draw", &found); if (found) { auto director = Director::getInstance(); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); JS::RootedValue rval(cx); JS::RootedValue fval(cx); JS_GetProperty(cx, jsObj, "draw", &fval); JS_CallFunctionValue(cx, jsObj, fval, JS::HandleValueArray::empty(), &rval); director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); } } }
/* * call-seq: * respond_to?(symbol) * * Returns <code>true</code> if this JavaScript object responds to the * named method. */ static VALUE respond_to_p(int argc, const VALUE* argv, VALUE self) { VALUE sym, priv; rb_scan_args(argc, argv, "11", &sym, &priv); RubyLandProxy* proxy; Data_Get_Struct(self, RubyLandProxy, proxy); JSContext * context = johnson_get_current_context(proxy->runtime); PREPARE_RUBY_JROOTS(context, 2); VALUE stringval = rb_funcall(sym, rb_intern("to_s"), 0); char* name = StringValuePtr(stringval); // assignment is always okay if (name[strlen(name) - 1] == '=') JRETURN_RUBY(Qtrue); jsval proxy_value; JCHECK(get_jsval_for_proxy(proxy, &proxy_value)); JROOT(proxy_value); JSObject *obj; JSBool found; JCHECK(JS_ValueToObject(context, proxy_value, &obj)); JROOT(obj); JCHECK(JS_HasProperty(context, obj, name, &found)); JRETURN_RUBY(found ? Qtrue : CALL_RUBY_WRAPPER(rb_call_super, argc, argv)); }
Result dispatch(JSContext *cx, JSObject *thisobj, const char *dataPropName, const char *methodName, Result noHandler) { if (!data) return fail; JSBool found; if (!JS_HasProperty(cx, thisobj, methodName, &found)) return fail; if (!found) return noHandler; // Create event object. jsval v; if (!deserializeData(cx, &v)) return fail; JSObject *obj = JS_NewObject(cx, NULL, NULL, NULL); if (!obj || !JS_DefineProperty(cx, obj, dataPropName, v, NULL, NULL, 0)) return fail; // Call event handler. jsval argv[1] = { OBJECT_TO_JSVAL(obj) }; jsval rval = JSVAL_VOID; return Result(JS_CallFunctionName(cx, thisobj, methodName, 1, argv, &rval)); }
bool ObjectWrapper::Key::has(JSContext* cx, JS::HandleObject o) { bool has; switch (_type) { case Type::Field: if (JS_HasProperty(cx, o, _field, &has)) return has; break; case Type::Index: if (JS_HasElement(cx, o, _idx, &has)) return has; break; case Type::Id: { JS::RootedId id(cx, _id); if (JS_HasPropertyById(cx, o, id, &has)) return has; break; } case Type::InternedString: { InternedStringId id(cx, _internedString); if (JS_HasPropertyById(cx, o, id, &has)) return has; break; } } throwCurrentJSException(cx, ErrorCodes::InternalError, "Failed to has value on a JSObject"); }
bool ScriptInterface::HasProperty(jsval obj, const char* name) { if (! JSVAL_IS_OBJECT(obj)) return false; JSObject* object = JSVAL_TO_OBJECT(obj); JSBool found; if (!JS_HasProperty(m->m_cx, object, name, &found)) return false; return (found != JS_FALSE); }
static bool js_coolexp_ErayLayer_ctor(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::RootedObject obj(cx, args.thisv().toObjectOrNull()); ErayLayer *nobj = new (std::nothrow) ErayLayer(); auto newproxy = jsb_new_proxy(nobj, obj); jsb_ref_init(cx, &newproxy->obj, nobj, "ErayLayer"); bool isFound = false; if (JS_HasProperty(cx, obj, "_ctor", &isFound) && isFound) ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), "_ctor", args); args.rval().setUndefined(); return true; }
bool ScriptInterface::HasProperty(JS::HandleValue obj, const char* name) { // TODO: proper errorhandling JSAutoRequest rq(m->m_cx); if (!obj.isObject()) return false; JS::RootedObject object(m->m_cx, &obj.toObject()); bool found; if (!JS_HasProperty(m->m_cx, object, name, &found)) return false; return found; }
void invokeJS(const char* func, IMCallbackJS* cb) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = func; JS::RootedObject obj(cx, _JSDelegate); 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 int valueSize = 0; jsval* pVals = nullptr; valueSize = cb->transParams(&pVals); 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 == valueSize) { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::empty(), &retval); } else { JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(valueSize, pVals), &retval); } #else if (0 == valueSize) { JS_CallFunctionName(cx, obj, func_name, 0, nullptr, &retval); } else { JS_CallFunctionName(cx, obj, func_name, valueSize, pVals, &retval); } #endif } }
bool js_cocos2dx_experimental_video_VideoPlayer_constructor(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; cocos2d::experimental::ui::VideoPlayer* cobj = new (std::nothrow) cocos2d::experimental::ui::VideoPlayer(); js_type_class_t *typeClass = js_get_type_from_native<cocos2d::experimental::ui::VideoPlayer>(cobj); // link the native object with the javascript object JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, cobj, typeClass, "cocos2d::experimental::ui::VideoPlayer")); args.rval().set(OBJECT_TO_JSVAL(jsobj)); if (JS_HasProperty(cx, jsobj, "_ctor", &ok) && ok) ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsobj), "_ctor", args); return true; }
bool ScriptInterface::CallFunction_(jsval val, const char* name, size_t argc, jsval* argv, jsval& ret) { JSObject* obj; if (!JS_ValueToObject(m->m_cx, val, &obj) || obj == NULL) return false; // Check that the named function actually exists, to avoid ugly JS error reports // when calling an undefined value JSBool found; if (!JS_HasProperty(m->m_cx, obj, name, &found) || !found) return false; JSBool ok = JS_CallFunctionName(m->m_cx, obj, name, (uintN)argc, argv, &ret); return ok ? true : false; }
static bool js_es_Button_ctor(JSContext *cx, uint32_t argc, jsval *vp) { jsval *argv = JS_ARGV(cx, vp); JSObject *obj = JS_THIS_OBJECT(cx, vp); es::Button *nobj = new es::Button(); if (nobj) { nobj->autorelease(); } js_proxy_t* p = jsb_new_proxy(nobj, obj); JS_AddNamedObjectRoot(cx, &p->obj, "es::Button"); bool isFound = false; if (JS_HasProperty(cx, obj, "_ctor", &isFound)) ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(obj), "_ctor", argc, argv); JS_SET_RVAL(cx, vp, JSVAL_VOID); return true; }
bool ScriptInterface::CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) { JSAutoRequest rq(m->m_cx); JS::RootedObject obj(m->m_cx); if (!JS_ValueToObject(m->m_cx, val, &obj) || !obj) return false; // Check that the named function actually exists, to avoid ugly JS error reports // when calling an undefined value bool found; if (!JS_HasProperty(m->m_cx, obj, name, &found) || !found) return false; bool ok = JS_CallFunctionName(m->m_cx, obj, name, argv, ret); return ok; }
bool ScriptInterface::SetGlobal_(const char* name, jsval value, bool replace) { if (!replace) { JSBool found; if (!JS_HasProperty(m->m_cx, m->m_glob, name, &found)) return false; if (found) { JS_ReportError(m->m_cx, "SetGlobal \"%s\" called multiple times", name); return false; } } JSBool ok = JS_DefineProperty(m->m_cx, m->m_glob, name, value, NULL, NULL, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); return ok ? true : false; }
static void executeJSFunctionWithName(JSContext *cx, JSObject *obj, const char *funcName, jsval &dataVal, jsval &retval) { JSBool hasAction; jsval temp_retval; if (JS_HasProperty(cx, obj, funcName, &hasAction) && hasAction) { if(!JS_GetProperty(cx, obj, funcName, &temp_retval)) { return; } if(temp_retval == JSVAL_VOID) { return; } JS_CallFunctionName(cx, obj, funcName, 1, &dataVal, &retval); } }
void onFailure(const sdkbox::Product& info, const std::string& msg) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = "onFailure"; JS::RootedObject obj(cx, _JSDelegate); JSAutoCompartment ac(cx, obj); #if MOZJS_MAJOR_VERSION >= 31 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[2]; jsval value = OBJECT_TO_JSVAL(product_to_obj(s_cx, info)); dataVal[0] = value; dataVal[1] = std_string_to_jsval(cx, msg); 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 JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(sizeof(dataVal)/sizeof(*dataVal), dataVal), &retval); #else JS_CallFunctionName(cx, obj, func_name, sizeof(dataVal)/sizeof(*dataVal), dataVal, &retval); #endif } }
bool ScriptInterface::SetGlobal_(const char* name, JS::HandleValue value, bool replace) { JSAutoRequest rq(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); if (!replace) { bool found; if (!JS_HasProperty(m->m_cx, global, name, &found)) return false; if (found) { JS_ReportError(m->m_cx, "SetGlobal \"%s\" called multiple times", name); return false; } } bool ok = JS_DefineProperty(m->m_cx, global, name, value, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT); return ok; }
bool js_cocos2dx_GLNode_constructor(JSContext *cx, uint32_t argc, jsval *vp) { if (argc == 0) { cocos2d::GLNode* cobj = new (std::nothrow) cocos2d::GLNode; js_type_class_t *typeClass = js_get_type_from_native<cocos2d::GLNode>(cobj); JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, cobj, typeClass, "cocos2d::GLNode")); JS::CallArgs args = JS::CallArgsFromVp(argc, vp); args.rval().set(OBJECT_TO_JSVAL(jsobj)); bool ok=false; if (JS_HasProperty(cx, jsobj, "_ctor", &ok) && ok) ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsobj), "_ctor", args); return true; } JS_ReportError(cx, "wrong number of arguments: %d, was expecting %d", argc, 0); return false; }
void onProductRequestSuccess(const std::vector<sdkbox::Product>& products) { if (!s_cx) { return; } JSContext* cx = s_cx; const char* func_name = "onProductRequestSuccess"; JS::RootedObject obj(cx, _JSDelegate); JSAutoCompartment ac(cx, obj); #if MOZJS_MAJOR_VERSION >= 31 bool hasAction; JS::RootedValue retval(cx); JS::RootedValue func_handle(cx); #else JSBool hasAction; jsval retval; jsval func_handle; #endif jsval dataVal[1]; jsval value = std_vector_product_to_jsval(s_cx, products); dataVal[0] = value; 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 JS_CallFunctionName(cx, obj, func_name, JS::HandleValueArray::fromMarkedLocation(sizeof(dataVal)/sizeof(*dataVal), dataVal), &retval); #else JS_CallFunctionName(cx, obj, func_name, sizeof(dataVal)/sizeof(*dataVal), dataVal, &retval); #endif } }
static JSBool resolverHasMethod(JSContext *cx, JSObject *obj, const char *name) { // If we're the prototype of some other object, then obj won't be of // the JSClass we need it to be, so just deny that our membrane has // the method we're looking for. JSClass *klass = JS_GET_CLASS(cx, obj); if (klass != &sFlexibleWrapper_JSClass.base) return JS_FALSE; jsval resolver; if (!JS_GetReservedSlot(cx, obj, SLOT_RESOLVER, &resolver)) return JS_FALSE; JSObject *resolverObj = JSVAL_TO_OBJECT(resolver); JSBool hasProperty; if (!JS_HasProperty(cx, resolverObj, name, &hasProperty)) return JS_FALSE; return hasProperty; // TODO: Check to make sure the property is a function? }
void GLNode::draw() { js_proxy_t* proxy = NULL; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); proxy = js_get_or_create_proxy<cocos2d::Node>(cx, this); if( proxy ) { JSObject *jsObj = proxy->obj; if (jsObj) { bool found; JSB_AUTOCOMPARTMENT_WITH_GLOBAL_OBJCET JS_HasProperty(cx, jsObj, "draw", &found); if (found == true) { JS::RootedValue rval(cx); JS::RootedValue fval(cx); jsval *argv = NULL; unsigned argc=0; JS_GetProperty(cx, jsObj, "draw", &fval); JS_CallFunctionValue(cx, jsObj, fval, argc, argv, rval.address()); } } } }
void GLNode::draw() { js_proxy_t* proxy = NULL; JSContext *cx = ScriptingCore::getInstance()->getGlobalContext(); proxy = js_get_or_create_proxy<cocos2d::Node>(cx, this); //JSB_CCNode *proxy = objc_getAssociatedObject(self, &JSB_association_proxy_key); if( proxy ) { JSObject *jsObj = proxy->obj; if (jsObj) { JSBool found; JSB_ENSURE_AUTOCOMPARTMENT(cx, jsObj); JS_HasProperty(cx, jsObj, "draw", &found); if (found == JS_TRUE) { jsval rval, fval; jsval *argv = NULL; unsigned argc=0; JS_GetProperty(cx, jsObj, "draw", &fval); JS_CallFunctionValue(cx, jsObj, fval, argc, argv, &rval); } } } }
bool DispatchEventToTarget(JSContext* aCx, JSObject* aTarget, JSObject* aEvent, bool* aPreventDefaultCalled) { static const char kFunctionName[] = "dispatchEvent"; JSBool hasProperty; if (!JS_HasProperty(aCx, aTarget, kFunctionName, &hasProperty)) { return false; } JSBool preventDefaultCalled = false; if (hasProperty) { jsval argv[] = { OBJECT_TO_JSVAL(aEvent) }; jsval rval = JSVAL_VOID; if (!JS_CallFunctionName(aCx, aTarget, kFunctionName, JS_ARRAY_LENGTH(argv), argv, &rval) || !JS_ValueToBoolean(aCx, rval, &preventDefaultCalled)) { return false; } } *aPreventDefaultCalled = !!preventDefaultCalled; return true; }
static JSObject * gjs_keep_alive_new(JSContext *context) { KeepAlive *priv; bool found; /* This function creates an unattached KeepAlive object; following our * general strategy, we have a single KeepAlive class with a constructor * stored on our single "load global" pseudo-global object, and we create * instances with the load global as parent. */ g_assert(context != NULL); JSAutoRequest ar(context); JS::RootedObject global(context, gjs_get_import_global(context)); g_assert(global != NULL); if (!JS_HasProperty(context, global, gjs_keep_alive_class.name, &found)) return NULL; if (!found) { JSObject *prototype; gjs_debug(GJS_DEBUG_KEEP_ALIVE, "Initializing keep-alive class in context %p global %p", context, global.get()); prototype = JS_InitClass(context, global, /* parent prototype JSObject* for * prototype; NULL for * Object.prototype */ JS::NullPtr(), &gjs_keep_alive_class, /* constructor for instances (NULL for * none - just name the prototype like * Math - rarely correct) */ gjs_keep_alive_constructor, /* number of constructor args */ 0, /* props of prototype */ &gjs_keep_alive_proto_props[0], /* funcs of prototype */ &gjs_keep_alive_proto_funcs[0], /* props of constructor, MyConstructor.myprop */ NULL, /* funcs of constructor, MyConstructor.myfunc() */ NULL); if (prototype == NULL) g_error("Can't init class %s", gjs_keep_alive_class.name); gjs_debug(GJS_DEBUG_KEEP_ALIVE, "Initialized class %s prototype %p", gjs_keep_alive_class.name, prototype); } gjs_debug(GJS_DEBUG_KEEP_ALIVE, "Creating new keep-alive object for context %p global %p", context, global.get()); JS::RootedObject keep_alive(context, JS_NewObject(context, &gjs_keep_alive_class, JS::NullPtr(), global)); if (keep_alive == NULL) { gjs_log_exception(context); g_error("Failed to create keep_alive object"); } priv = g_slice_new0(KeepAlive); priv->children = g_hash_table_new_full(child_hash, child_equal, NULL, child_free); g_assert(priv_from_js(context, keep_alive) == NULL); JS_SetPrivate(keep_alive, priv); gjs_debug_lifecycle(GJS_DEBUG_KEEP_ALIVE, "keep_alive constructor, obj %p priv %p", keep_alive.get(), priv); return keep_alive; }
static JSObject* importer_new(JSContext *context, gboolean is_root) { JSObject *importer; Importer *priv; JSObject *global; JSBool found; global = gjs_get_import_global(context); if (!JS_HasProperty(context, global, gjs_importer_class.name, &found)) g_error("HasProperty call failed creating importer class"); if (!found) { JSObject *prototype; prototype = JS_InitClass(context, global, /* parent prototype JSObject* for * prototype; NULL for * Object.prototype */ NULL, &gjs_importer_class, /* constructor for instances (NULL for * none - just name the prototype like * Math - rarely correct) */ gjs_importer_constructor, /* number of constructor args */ 0, /* props of prototype */ &gjs_importer_proto_props[0], /* funcs of prototype */ &gjs_importer_proto_funcs[0], /* props of constructor, MyConstructor.myprop */ NULL, /* funcs of constructor, MyConstructor.myfunc() */ NULL); if (prototype == NULL) g_error("Can't init class %s", gjs_importer_class.name); gjs_debug(GJS_DEBUG_IMPORTER, "Initialized class %s prototype %p", gjs_importer_class.name, prototype); } importer = JS_NewObject(context, &gjs_importer_class, NULL, global); if (importer == NULL) g_error("No memory to create importer importer"); priv = g_slice_new0(Importer); priv->is_root = is_root; GJS_INC_COUNTER(importer); g_assert(priv_from_js(context, importer) == NULL); JS_SetPrivate(importer, priv); gjs_debug_lifecycle(GJS_DEBUG_IMPORTER, "importer constructor, obj %p priv %p", importer, priv); return importer; }
void XMLHttpRequest::onHttpRequestCompleted(cocos2d::CCNode *sender, void *data){ readyState = 4; CCHttpResponse *response = (CCHttpResponse *)data; if (!response) { return; } // You can get original request type from: response->request->reqType if (0 != strlen(response->getHttpRequest()->getTag())) { CCLog("%s completed", response->getHttpRequest()->getTag()); } int statusCode = response->getResponseCode(); char statusString[64] = {}; sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); status = statusCode; //http status if (!response->isSucceed()) { CCLog("response failed"); CCLog("error buffer: %s", response->getErrorBuffer()); return; } // dump data std::string res; std::vector<char> *buffer = response->getResponseData(); for (unsigned int i = 0; i < buffer->size(); i++) { res += ((*buffer)[i]); } // std::string res; // std::vector<char> *buffer = response->getResponseData(); // res.clear(); // res.assign((*buffer).begin(), (*buffer).end()); responseText = res; JSBool hasAction; jsval temp_retval; do{ if (JS_HasProperty(ScriptingCore::getInstance()->getGlobalContext(), ScriptingCore::getInstance()->getGlobalObject(), "onreadystatechange", &hasAction) && hasAction) { if(!JS_GetProperty(ScriptingCore::getInstance()->getGlobalContext(), ScriptingCore::getInstance()->getGlobalObject(), "onreadystatechange", &temp_retval)) { break; } if(temp_retval == JSVAL_VOID) { break; } jsval jsret; JS_CallFunctionName(ScriptingCore::getInstance()->getGlobalContext(), ScriptingCore::getInstance()->getGlobalObject(), "onreadystatechange", 0, NULL, &jsret); } }while(0); // jsval *retval; // JS_CallFunctionName(ScriptingCore::getInstance()->getGlobalContext(), // ScriptingCore::getInstance()->getGlobalObject(), // "onreadystatechange", NULL, NULL, retval); }