void js_prop_set_from_jsval(JSContext *cx, prop_t *p, jsval value) { JSBool b; if(JSVAL_IS_INT(value)) { prop_set_int(p, JSVAL_TO_INT(value)); } else if(JSVAL_IS_BOOLEAN(value)) { prop_set_int(p, JSVAL_TO_BOOLEAN(value)); } else if(JSVAL_IS_NULL(value) || JSVAL_IS_VOID(value)) { prop_set_void(p); } else if(JSVAL_IS_DOUBLE(value)) { double d; if(JS_ValueToNumber(cx, value, &d)) prop_set_float(p, d); } else if(JS_HasInstance(cx, RichText, value, &b) && b) { JSObject *o = JSVAL_TO_OBJECT(value); jsval v2; if(!JS_EnterLocalRootScope(cx)) return; if(!JS_GetProperty(cx, o, "text", &v2)) { JS_LeaveLocalRootScope(cx); return; } prop_set_string_ex(p, NULL, JS_GetStringBytes(JS_ValueToString(cx, v2)), PROP_STR_RICH); JS_LeaveLocalRootScope(cx); } else if(JSVAL_IS_STRING(value)) { js_prop_from_str(cx, p, value); } else if(JSVAL_IS_OBJECT(value)) { JSObject *obj = JSVAL_TO_OBJECT(value); JSClass *c = JS_GetClass(cx, obj); if(!strcmp(c->name, "XML")) // Treat some classes special js_prop_from_str(cx, p, value); else js_prop_from_object(cx, obj, p); } else { prop_set_void(p); } }
void register_LoadUrlImage_js(JSContext* cx, JSObject* global) { JS::RootedValue nsval(cx); JS::RootedValue tmpval(cx); JS::RootedObject pJsbObject(cx); JS_GetProperty(cx, global, "LoadUrlImage", &nsval); if (nsval == JSVAL_VOID) { pJsbObject = JS_NewObject(cx, NULL, NULL, NULL); nsval = OBJECT_TO_JSVAL(global); JS_SetProperty(cx, global, "LoadUrlImage", nsval); } else { JS_ValueToObject(cx, nsval, &pJsbObject); } JS_DefineFunction(cx, global, "loadUrlImage", JSB_LoadUrlImage_loadUrlImage, 2, JSPROP_READONLY | JSPROP_PERMANENT); }
void FFSessionHandler::disconnectDetectedImpl() { JSContext* ctx = getJSContext(); if (!ctx) { return; } Debug::log(Debug::Debugging) << "Getting function \"__gwt_disconnected\"" << Debug::flush; jsval funcVal; if (!JS_GetProperty(ctx, global, "__gwt_disconnected", &funcVal) || funcVal == JSVAL_VOID) { Debug::log(Debug::Error) << "Could not get function \"__gwt_disconnected\"" << Debug::flush; return; } jsval rval; JS_CallFunctionValue(ctx, global, funcVal, 0, 0, &rval); }
bool ScriptEvent_OnSwapBuffers(HDC hDC, BOOL & bSwapRes) { JSAutoCompartment ac(g_JsCx, g_JsGlobal); jsval f; if(!JS_GetProperty(g_JsCx, g_JsGlobal, "onSwapBuffers", &f) || JSVAL_IS_PRIMITIVE(f)) return false; jsval y; jsval x[1] = { JS_NumberValue((unsigned int)hDC) }; if(!JS_CallFunctionValue(g_JsCx, g_JsGlobal, f, 1, x, &y)) return false; bSwapRes = JS::ToBoolean(y); return true; }
/** * Get a field of an object as an object. * * If the field does not exist, create it. If it exists but is not an * object, throw a JS error. */ JSObject *GetOrCreateObjectProperty(JSContext *cx, JS::Handle<JSObject*> aObject, const char *aProperty) { JS::Rooted<JS::Value> val(cx); if (!JS_GetProperty(cx, aObject, aProperty, &val)) { return nullptr; } if (!val.isUndefined()) { if (val.isObject()) { return &val.toObject(); } JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE, aProperty, "not an object"); return nullptr; } return JS_DefineObject(cx, aObject, aProperty, nullptr, nullptr, JSPROP_ENUMERATE); }
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; } JSAutoCompartment ac(cx, obj); JS_CallFunctionName(cx, obj, funcName, 1, &dataVal, &retval); } }
bool TaskEndWait( volatile ProcessEvent *pe, bool *hasEvent, JSContext *cx, JSObject * ) { TaskEvent *upe = (TaskEvent*)pe; *hasEvent = (upe->pv->pendingResponseCount > 0); if ( !*hasEvent ) return true; jsval fct, argv[2]; argv[1] = OBJECT_TO_JSVAL(upe->obj); // already rooted JL_CHK( JS_GetProperty(cx, upe->obj, "onResponse", &fct) ); if ( JL_ValueIsCallable(cx, fct) ) JL_CHK( JS_CallFunctionValue(cx, upe->obj, fct, COUNTOF(argv)-1, argv+1, argv) ); return true; JL_BAD; }
int js_prop_from_object(JSContext *cx, JSObject *obj, prop_t *p) { JSIdArray *ida; int i, r = 0; const char *n; int array_zapped = 0; if((ida = JS_Enumerate(cx, obj)) == NULL) return -1; for(i = 0; i < ida->length; i++) { jsval name, value; if(!JS_IdToValue(cx, ida->vector[i], &name)) continue; if(JSVAL_IS_STRING(name)) { n = JS_GetStringBytes(JSVAL_TO_STRING(name)); if(!JS_GetProperty(cx, obj, n, &value)) continue; } else if(JSVAL_IS_INT(name)) { if(!JS_GetElement(cx, obj, JSVAL_TO_INT(name), &value) || JSVAL_IS_VOID(value)) continue; if(!array_zapped) { array_zapped = 1; prop_destroy_by_name(p, NULL); } n = NULL; } else { continue; } if(JSVAL_TO_OBJECT(value) == obj) continue; js_prop_set_from_jsval(cx, prop_create(p, n), value); } JS_DestroyIdArray(cx, ida); return r; }
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); TypeTest<ComponentJS> 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; mozilla::Maybe<JS::PersistentRootedObject> *jsObj = new (std::nothrow) mozilla::Maybe<JS::PersistentRootedObject>(); 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* jsproxy = js_get_or_create_proxy<ComponentJS>(cx, this); JS::RemoveObjectRoot(cx, &jsproxy->obj); jsb_remove_proxy(jsproxy); // link the native object with the javascript object jsb_new_proxy(this, jsObj->ref()); _jsObj = jsObj; } }
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 } }
void register_CCBuilderReader(JSContext *cx, JSObject *obj) { jsval nsval; JSObject *ns; JS_GetProperty(cx, obj, "cc", &nsval); if (nsval == JSVAL_VOID) { ns = JS_NewObject(cx, NULL, NULL, NULL); nsval = OBJECT_TO_JSVAL(ns); JS_SetProperty(cx, obj, "cc", &nsval); } else { JS_ValueToObject(cx, nsval, &ns); } obj = ns; JSObject *tmpObj = JSVAL_TO_OBJECT(anonEvaluate(cx, obj, "(function () { return cc._Reader; })()")); JS_DefineFunction(cx, tmpObj, "create", js_CocosBuilder_create, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, tmpObj, "loadScene", js_cocos2dx_CCBReader_createSceneWithNodeGraphFromFile, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, jsb_CCBReader_prototype, "load", js_cocos2dx_CCBReader_readNodeGraphFromFile, 2, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, jsb_CCBAnimationManager_prototype, "setCompletedAnimationCallback", js_cocos2dx_CCBAnimationManager_animationCompleteCallback, 2, JSPROP_READONLY | JSPROP_PERMANENT); }
void CancelLoad(const CStrW& message) { // Cancel loader LDR_Cancel(); // Call the cancelOnError GUI function, defined in ..gui/common/functions_utility_error.js // So all GUI pages that load games should include this script if (g_GUI && g_GUI->HasPages()) { JSContext* cx = g_ScriptingHost.getContext(); jsval fval, rval; JSBool ok = JS_GetProperty(cx, g_GUI->GetScriptObject(), "cancelOnError", &fval); ENSURE(ok); jsval msgval = ToJSVal(message); if (ok && !JSVAL_IS_VOID(fval)) JS_CallFunctionValue(cx, g_GUI->GetScriptObject(), fval, 1, &msgval, &rval); } }
bool ScriptInterface::ReplaceNondeterministicRNG(boost::rand48& rng) { JSAutoRequest rq(m->m_cx); JS::RootedValue math(m->m_cx); JS::RootedObject global(m->m_cx, m->m_glob); if (JS_GetProperty(m->m_cx, global, "Math", &math) && math.isObject()) { JS::RootedObject mathObj(m->m_cx, &math.toObject()); JS::RootedFunction random(m->m_cx, JS_DefineFunction(m->m_cx, mathObj, "random", Math_random, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)); if (random) { m->m_rng = &rng; return true; } } LOGERROR("ReplaceNondeterministicRNG: failed to replace Math.random"); return false; }
int add_body_stream(bfd_t *body_stream) { jsval smtpClient, bodyStream; JSObject *global; global = JS_GetGlobalForScopeChain(js_context); // Get smtpClient if (!JS_GetProperty(js_context, global, "smtpClient", &smtpClient)) { return -1; } bodyStream = PRIVATE_TO_JSVAL(body_stream); // Add path property if (!JS_SetProperty(js_context, JSVAL_TO_OBJECT(smtpClient), "bodyStream", &bodyStream)) { return -1; } return 0; }
static JSBool convert_symbol_to_js(JohnsonRuntime* runtime, VALUE symbol, jsval* retval) { JSContext * context = johnson_get_current_context(runtime); PREPARE_JROOTS(context, 2); VALUE to_s = CALL_RUBY_WRAPPER(rb_funcall_0, symbol, rb_intern("to_s"), 0); jsval name = STRING_TO_JSVAL(JS_NewStringCopyN(context, StringValuePtr(to_s), (size_t) StringValueLen(to_s))); JROOT(name); // calls Johnson.symbolize(name) in JS-land. See lib/prelude.js jsval nsJohnson; JCHECK(JS_GetProperty(context, runtime->global, "Johnson", &nsJohnson)); JROOT(nsJohnson); JCHECK(JS_CallFunctionName(context, JSVAL_TO_OBJECT(nsJohnson), "symbolize", 1, &name, retval)); JRETURN; }
LogicEntityPtr TraceMonkeyEngine::getCLogicEntity(JSObject* scriptingEntity) { // We do this in a slow but sure manner: read the uniqueId from JS, // and look up the entity using that. In the future, speed this up // using private data or some other method jsval temp; bool success = JS_GetProperty(context, scriptingEntity, "uniqueId", &temp); assert(success); assert(JSVAL_IS_INT(temp)); int uniqueId = JSVAL_TO_INT(temp); LogicEntityPtr ret = LogicSystem::getLogicEntity(uniqueId); Logging::log(Logging::DEBUG, "TraceMonkey getting the CLE for UID %d\r\n", uniqueId); assert(ret.get()); return ret; }
native_netscape_javascript_JSObject_getMember( JRIEnv* env, struct netscape_javascript_JSObject* self, struct java_lang_String *name) { #ifndef JAVA return NULL; #else JSContext *cx; JSObject *jso; JSSavedState saved; const char *cstr; struct java_lang_Object *member; jsval val; int cost = 0; if (!enterJS(env, self, &cx, &jso, &saved)) return NULL; if (! name || ! (cstr = JRI_GetStringPlatformChars(env, name, (const jbyte *) cx->charSetName, (jint) cx->charSetNameLength))) { /* FIXME this should be an error of some sort */ js_throwJSException(env, "illegal member name"); member = NULL; goto do_exit; } if (! JS_GetProperty(cx, jso, cstr, &val) || ! js_convertJSValueToJObject(/*FIXME*/(HObject**)&member, cx, val, 0, 0, JS_FALSE, &cost)) member = NULL; do_exit: if (!exitJS(env, self, cx, jso, &saved)) return NULL; return member; #endif }
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; } }
static Eina_Bool _egueb_script_js_sm_scripter_load(void *prv, Egueb_Dom_String *s, void **obj) { Egueb_Script_Js_Sm_Scripter *thiz = prv; Egueb_Dom_String *uri = NULL; jsval val; JSObject *so; Eina_Bool ret = EINA_FALSE; const char *data; *obj = NULL; data = egueb_dom_string_chars_get(s); /* get the document, get the uri of the document, pass it as the filename */ if (JS_GetProperty(thiz->cx, thiz->global, "document", &val)) { JSObject *doc; doc = JSVAL_TO_OBJECT(val); if (ender_js_sm_is_instance(thiz->cx, doc)) { Egueb_Dom_Node *n; n = ender_js_sm_instance_ptr_get(thiz->cx, doc); uri = egueb_dom_document_uri_get(n); } } so = JS_CompileScript(thiz->cx, NULL, data, strlen(data), uri ? egueb_dom_string_chars_get(uri) : NULL, 1); if (so) { Egueb_Script_Js_Sm_Scripter_Script *script; script = calloc(1, sizeof(Egueb_Script_Js_Sm_Scripter_Script)); script->obj = so; *obj = script; ret = EINA_TRUE; } if (uri) { egueb_dom_string_unref(uri); } return ret; }
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 } }
ScriptValuePtr TraceMonkeyValue::getProperty(std::string propertyName) { assert(isValid()); Logging::log(Logging::DEBUG, "TraceMonkeyValue::getProperty(%s): \r\n", propertyName.c_str()); printJSVAL(value); assert(JSVAL_IS_OBJECT(value)); jsval ret; assert(JS_GetProperty(TraceMonkeyEngine::context, JSVAL_TO_OBJECT(value), propertyName.c_str(), &ret)); bool success = JS_AddNamedRoot(TraceMonkeyEngine::context, &ret, "TraceMonkeyValue::getProperty temp val"); // Ensure our value won't be GCed assert(success); Logging::log(Logging::DEBUG, "returning: \r\n"); printJSVAL(ret); ScriptValuePtr retValue(new TraceMonkeyValue(engine, true, ret)); success = JS_RemoveRoot(TraceMonkeyEngine::context, &ret); // Allow GCing, it is already marked in the retValue assert(success); return retValue; }
int add_domain(jsval *smtpPath, char *domain) { jsval domains; uint32_t arr_len; // Get smtpPath.domains property if (!JS_GetProperty(js_context, JSVAL_TO_OBJECT(*smtpPath), "domains", &domains)) { return -1; } // Get number of recipients if (!JS_GetArrayLength(js_context, JSVAL_TO_OBJECT(domains), &arr_len)) { return -1; } // Add recipient if (!JS_DefineElement(js_context, JSVAL_TO_OBJECT(domains), arr_len, STRING_TO_JSVAL(JS_InternString(js_context, domain)), NULL, NULL, 0)) { return -1; } return 0; }
/* JSCall function by name, cvalues will be converted * * deactivates controller if any script errors! * * format values: * case 'b': BOOLEAN_TO_JSVAL((JSBool) va_arg(ap, int)); * case 'c': INT_TO_JSVAL((uint16) va_arg(ap, unsigned int)); * case 'i': * case 'j': js_NewNumberValue(cx, (jsdouble) va_arg(ap, int32), sp) * case 'u': js_NewNumberValue(cx, (jsdouble) va_arg(ap, uint32), sp) * case 'd': * case 'I': js_NewDoubleValue(cx, va_arg(ap, jsdouble), sp) * case 's': JS_NewStringCopyZ(cx, va_arg(ap, char *)) * case 'W': JS_NewUCStringCopyZ(cx, va_arg(ap, jschar *)) * case 'S': va_arg(ap, JSString *) * case 'o': OBJECT_TO_JSVAL(va_arg(ap, JSObject *) * case 'f': * fun = va_arg(ap, JSFunction *); * fun ? OBJECT_TO_JSVAL(fun->object) : JSVAL_NULL; * case 'v': va_arg(ap, jsval); */ bool ControllerListener::call(const char *funcname, int argc, const char *format, ...) { va_list ap; jsval fval = JSVAL_VOID; jsval ret = JSVAL_VOID; func("%s try calling method %s.%s(argc:%i)", __func__, name, funcname, argc); JS_SetContextThread(jsContext); JS_BeginRequest(jsContext); int res = JS_GetProperty(jsContext, jsObject, funcname, &fval); if(JSVAL_IS_VOID(fval)) { warning("method unresolved by JS_GetProperty"); } else { jsval *argv; void *markp; va_start(ap, format); argv = JS_PushArgumentsVA(jsContext, &markp, format, ap); va_end(ap); res = JS_CallFunctionValue(jsContext, jsObject, fval, argc, argv, &ret); JS_PopArguments(jsContext, &markp); if (res) { if(!JSVAL_IS_VOID(ret)) { JSBool ok; JS_ValueToBoolean(jsContext, ret, &ok); if (ok) // JSfunc returned 'true', so event is done { JS_EndRequest(jsContext); JS_ClearContextThread(jsContext); return true; } } } } JS_EndRequest(jsContext); JS_ClearContextThread(jsContext); return false; // no callback, redo on next controller }
void registerDefaultClasses(JSContext* cx, JSObject* global) { // first, try to get the ns jsval nsval; JSObject *ns; JS_GetProperty(cx, global, "cc", &nsval); if (nsval == JSVAL_VOID) { ns = JS_NewObject(cx, NULL, NULL, NULL); nsval = OBJECT_TO_JSVAL(ns); JS_SetProperty(cx, global, "cc", &nsval); } else { JS_ValueToObject(cx, nsval, &ns); } jsb_register_cocos2d_config(cx, ns); // // Javascript controller (__jsc__) // JSObject *jsc = JS_NewObject(cx, NULL, NULL, NULL); jsval jscVal = OBJECT_TO_JSVAL(jsc); JS_SetProperty(cx, global, "__jsc__", &jscVal); JS_DefineFunction(cx, jsc, "garbageCollect", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE ); JS_DefineFunction(cx, jsc, "dumpRoot", ScriptingCore::dumpRoot, 0, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE ); JS_DefineFunction(cx, jsc, "addGCRootObject", ScriptingCore::addRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE ); JS_DefineFunction(cx, jsc, "removeGCRootObject", ScriptingCore::removeRootJS, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE ); JS_DefineFunction(cx, jsc, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE ); // register some global functions JS_DefineFunction(cx, global, "require", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "log", ScriptingCore::log, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "executeScript", ScriptingCore::executeScript, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "forceGC", ScriptingCore::forceGC, 0, JSPROP_READONLY | JSPROP_PERMANENT); // these are used in the debug socket JS_DefineFunction(cx, global, "_socketOpen", jsSocketOpen, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "_socketWrite", jsSocketWrite, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "_socketRead", jsSocketRead, 1, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(cx, global, "_socketClose", jsSocketClose, 1, JSPROP_READONLY | JSPROP_PERMANENT); }
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()); } } } }
bool CreateAlias(JSContext* cx, const char* dstName, JS::HandleObject namespaceObj, const char* srcName) { RootedObject global(cx, JS_GetGlobalForObject(cx, namespaceObj)); if (!global) return false; RootedValue val(cx); if (!JS_GetProperty(cx, namespaceObj, srcName, &val)) return false; if (!val.isObject()) { JS_ReportErrorASCII(cx, "attempted to alias nonexistent function"); return false; } RootedObject function(cx, &val.toObject()); if (!JS_DefineProperty(cx, global, dstName, function, 0)) return false; return true; }
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); } } } }
void __Window_updatePosition (JSContext* cx, JSObject* object) { int y, x; WINDOW* win = (WINDOW*) JS_GetPrivate(cx, object); getbegyx(win, y, x); JS_BeginRequest(cx); JS_EnterLocalRootScope(cx); jsval jsPosition; JS_GetProperty(cx, object, "Position", &jsPosition); JSObject* Position = JSVAL_TO_OBJECT(jsPosition); jsval property; property = INT_TO_JSVAL(y); JS_SetProperty(cx, Position, "Y", &property); property = INT_TO_JSVAL(x); JS_SetProperty(cx, Position, "X", &property); JS_LeaveLocalRootScope(cx); JS_EndRequest(cx); }
/* Returns whether the object had the property; if the object did * not have the property, always sets an exception. Treats * "the property's value is JSVAL_VOID" the same as "no such property," * while JS_GetProperty() treats only "no such property" as an error. * Guarantees that *value_p is set to something, if only JSVAL_VOID, * even if an exception is set and false is returned. */ gboolean gjs_object_require_property(JSContext *context, JSObject *obj, const char *obj_description, const char *property_name, jsval *value_p) { jsval value; JS_BeginRequest(context); value = JSVAL_VOID; JS_GetProperty(context, obj, property_name, &value); if (value_p) *value_p = value; if (value != JSVAL_VOID) { JS_ClearPendingException(context); /* in case JS_GetProperty() was on crack */ JS_EndRequest(context); return TRUE; } else { /* remember gjs_throw() is a no-op if JS_GetProperty() * already set an exception */ if (obj_description) gjs_throw(context, "No property '%s' in %s (or its value was undefined)", property_name, obj_description); else gjs_throw(context, "No property '%s' in object %p (or its value was undefined)", property_name, obj); JS_EndRequest(context); return FALSE; } }
int add_part_to_header(jsval *header, char *c_str) { jsval part, parts; uint32_t parts_len; // Get parts array if (!JS_GetProperty(js_context, JSVAL_TO_OBJECT(*header), "parts", &parts)) { return 1; } // Get parts count if (!JS_GetArrayLength(js_context, JSVAL_TO_OBJECT(parts), &parts_len)) { return 1; } part = STRING_TO_JSVAL(JS_InternString(js_context, c_str)); // Add part to array if (!JS_SetElement(js_context, JSVAL_TO_OBJECT(parts), parts_len, &part)) { return -1; } return 0; }