/* @smjs_globhist_class.getProperty */ static JSBool smjs_globhist_get_property(JSContext *ctx, JSObject *obj, jsid id, jsval *vp) { JSObject *jsobj; unsigned char *uri_string; struct global_history_item *history_item; jsval tmp; if (!JS_IdToValue(ctx, id, &tmp)) goto ret_null; uri_string = JS_EncodeString(ctx, JS_ValueToString(ctx, tmp)); if (!uri_string) goto ret_null; history_item = get_global_history_item(uri_string); if (!history_item) goto ret_null; jsobj = smjs_get_globhist_item_object(history_item); if (!jsobj) goto ret_null; *vp = OBJECT_TO_JSVAL(jsobj); return JS_TRUE; ret_null: *vp = JSVAL_NULL; return JS_TRUE; }
JSBool jsval_to_TProductInfo(JSContext *cx, jsval v, TProductInfo* ret) { JSObject* tmp = JSVAL_TO_OBJECT(v); if (!tmp) { LOGD("jsval_to_TProductInfo: the jsval is not an object."); return JS_FALSE; } JSObject* it = JS_NewPropertyIterator(cx, tmp); while (true) { jsid idp; jsval key; if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) return JS_FALSE; // error if (key == JSVAL_VOID) break; // end of iteration if (! JSVAL_IS_STRING(key)) continue; // ignore integer properties jsval value; JS_GetPropertyById(cx, tmp, idp, &value); if (! JSVAL_IS_STRING(value)) continue; // ignore integer properties JSStringWrapper strWrapper(JSVAL_TO_STRING(key), cx); JSStringWrapper strWrapper2(JSVAL_TO_STRING(value), cx); (*ret)[strWrapper.get()] = strWrapper2.get(); LOGD("iterate object: key = %s, value = %s", strWrapper.get().c_str(), strWrapper2.get().c_str()); } return JS_TRUE; }
bool jsval_to_FBInfo(JSContext *cx, jsval v, StringMap* ret) { JSObject* tmp = JSVAL_TO_OBJECT(v); if (!tmp) { LOGD("jsval_to_TProductInfo: the jsval is not an object."); return false; } JSObject* it = JS_NewPropertyIterator(cx, tmp); while (true) { jsid idp; jsval key; if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) return false; // error if (key == JSVAL_VOID) break; // end of iteration if (! JSVAL_IS_STRING(key)) continue; // ignore integer properties JS::RootedValue value(cx); JS_GetPropertyById(cx, tmp, idp, &value); // if (! JSVAL_IS_STRING(value)) // continue; // ignore integer properties if(JSVAL_IS_STRING(value)) { JSStringWrapper strWrapper(JSVAL_TO_STRING(key), cx); JSStringWrapper strWrapper2(JSVAL_TO_STRING(value), cx); ret->insert(std::map<std::string, std::string>::value_type(strWrapper.get(), strWrapper2.get())); } else if(JSVAL_IS_NUMBER(value)) { double number = 0.0; JS::ToNumber(cx, value, &number); std::stringstream ss; ss << number; JSStringWrapper strWrapper(JSVAL_TO_STRING(key), cx); //JSStringWrapper strWrapper2(JSVAL_TO_STRING(value), cx); ret->insert(std::map<std::string, std::string>::value_type(strWrapper.get(), ss.str())); } else if(JSVAL_IS_BOOLEAN(value)) { bool boolVal = JS::ToBoolean(value); JSStringWrapper strWrapper(JSVAL_TO_STRING(key), cx); //JSStringWrapper strWrapper2(JSVAL_TO_STRING(value), cx); std::string boolstring = boolVal ? "true" : "false"; ret->insert(std::map<std::string, std::string>::value_type(strWrapper.get(), boolstring)); } } return true; }
void gjs_log_object_props(JSContext *context, JSObject *obj, GjsDebugTopic topic, const char *prefix) { JSObject *props_iter; jsid prop_id; JS_BeginRequest(context); /* We potentially create new strings, plus the property iterator, * that could get collected as we go through this process. So * create a local root scope. */ JS_EnterLocalRootScope(context); props_iter = JS_NewPropertyIterator(context, obj); if (props_iter == NULL) { gjs_debug(GJS_DEBUG_ERROR, "Failed to create property iterator for object props"); goto done; } prop_id = JSVAL_VOID; if (!JS_NextProperty(context, props_iter, &prop_id)) goto done; while (prop_id != JSVAL_VOID) { jsval nameval; const char *name; jsval propval; if (!JS_IdToValue(context, prop_id, &nameval)) goto next; if (!gjs_get_string_id(nameval, &name)) goto next; if (!gjs_object_get_property(context, obj, name, &propval)) goto next; gjs_debug(topic, "%s%s = '%s'", prefix, name, gjs_value_debug_string(context, propval)); next: prop_id = JSVAL_VOID; if (!JS_NextProperty(context, props_iter, &prop_id)) break; } done: JS_LeaveLocalRootScope(context); JS_EndRequest(context); }
static JSBool lookup_static_member_by_id(JSContext *cx, JNIEnv *jEnv, JSObject *obj, JavaClassDescriptor **class_descriptorp, jsid id, JavaMemberDescriptor **memberp) { jsval idval; JavaMemberDescriptor *member_descriptor; const char *member_name; JavaClassDescriptor *class_descriptor; class_descriptor = JS_GetPrivate(cx, obj); if (!class_descriptor) { *class_descriptorp = NULL; *memberp = NULL; return JS_TRUE; } if (class_descriptorp) *class_descriptorp = class_descriptor; member_descriptor = jsj_LookupJavaStaticMemberDescriptorById(cx, jEnv, class_descriptor, id); if (!member_descriptor) { JS_IdToValue(cx, id, &idval); if (!JSVAL_IS_STRING(idval)) { JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_BAD_JCLASS_EXPR); return JS_FALSE; } member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); /* * See if the property looks like the explicit resolution of an * overloaded method, e.g. "max(double,double)". */ member_descriptor = jsj_ResolveExplicitMethod(cx, jEnv, class_descriptor, id, JS_TRUE); if (member_descriptor) goto done; /* Why do we have to do this ? */ if (!strcmp(member_name, "prototype")) { *memberp = NULL; return JS_TRUE; } JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_MISSING_NAME, class_descriptor->name, member_name); return JS_FALSE; } done: if (memberp) *memberp = member_descriptor; return JS_TRUE; }
JSBool Dynamic_delete (JSContext* context, JSObject* owner, jsid id, jsval* value) { jsval name; CDHash* self = (CDHash*) JS_GetPrivate(context, owner); JS_IdToValue(context, id, &name); return JS_TRUE; }
/** * Convert a JavaScript Object to a map * * @param cx the JavaScript context * @param t the JavaScript Object to convert * @return a new map containing the JavaScript Object */ map* mapFromJSObject(JSContext *cx,jsval t){ map *res=NULL; JSIdArray *idp=JS_Enumerate(cx,JSVAL_TO_OBJECT(t)); #ifdef JS_DEBUG fprintf(stderr,"Properties %p\n",(void*)t); #endif if(idp!=NULL) { int index; jsdouble argNum; #ifdef JS_DEBUG fprintf(stderr,"Properties length : %d \n",idp->length); #endif for (index=0,argNum=idp->length;index<argNum;index++) { jsval id = idp->vector[index]; jsval vp; JS_IdToValue(cx,id,&vp); char *tmp, *tmp1; JSString *jsmsg,*jsmsg1; size_t len,len1; jsmsg = JS_ValueToString(cx,vp); len = JS_GetStringLength(jsmsg); jsval nvp; tmp=JS_EncodeString(cx,jsmsg); JS_GetProperty(cx, JSVAL_TO_OBJECT(t), tmp, &nvp); jsmsg1 = JS_ValueToString(cx,nvp); len1 = JS_GetStringLength(jsmsg1); tmp1=JS_EncodeString(cx,jsmsg1); #ifdef JS_DEBUG fprintf(stderr,"Enumerate id : %d [ %s => %s ]\n",index,tmp,tmp1); #endif if(strcasecmp(tmp,"child")!=0){ if(res!=NULL){ #ifdef JS_DEBUG fprintf(stderr,"%s - %s\n",tmp,tmp1); #endif addToMap(res,tmp,tmp1); } else{ res=createMap(tmp,tmp1); res->next=NULL; } } free(tmp); free(tmp1); #ifdef JS_DEBUG dumpMap(res); #endif } JS_DestroyIdArray(cx,idp); } #ifdef JS_DEBUG dumpMap(res); #endif return res; }
bool ScriptInterface::EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector<std::string>& out) { JSAutoRequest rq(m->m_cx); if (!objVal.isObjectOrNull()) { LOGERROR("EnumeratePropertyNamesWithPrefix expected object type!"); return false; } if(objVal.isNull()) return true; // reached the end of the prototype chain JS::RootedObject obj(m->m_cx, &objVal.toObject()); JS::RootedObject it(m->m_cx, JS_NewPropertyIterator(m->m_cx, obj)); if (!it) return false; while (true) { JS::RootedId idp(m->m_cx); JS::RootedValue val(m->m_cx); if (! JS_NextProperty(m->m_cx, it, idp.address()) || ! JS_IdToValue(m->m_cx, idp, &val)) return false; if (val.isUndefined()) break; // end of iteration if (!val.isString()) continue; // ignore integer properties JS::RootedString name(m->m_cx, val.toString()); size_t len = strlen(prefix)+1; std::vector<char> buf(len); size_t prefixLen = strlen(prefix) * sizeof(char); JS_EncodeStringToBuffer(m->m_cx, name, &buf[0], prefixLen); buf[len-1]= '\0'; if(0 == strcmp(&buf[0], prefix)) { size_t len; const jschar* chars = JS_GetStringCharsAndLength(m->m_cx, name, &len); out.push_back(std::string(chars, chars+len)); } } // Recurse up the prototype chain JS::RootedObject prototype(m->m_cx); if (JS_GetPrototype(m->m_cx, obj, &prototype)) { JS::RootedValue prototypeVal(m->m_cx, JS::ObjectOrNullValue(prototype)); if (! EnumeratePropertyNamesWithPrefix(prototypeVal, prefix, out)) return false; } return true; }
int to_erl_object(ErlNifEnv* env, JSContext* cx, JSObject* obj, ERL_NIF_TERM* term) { ERL_NIF_TERM* array = NULL; ERL_NIF_TERM list; ERL_NIF_TERM keyterm; ERL_NIF_TERM valterm; JSObject* iter; jsid idp; jsval val; int length; int index; int ret = ERROR; iter = JS_NewPropertyIterator(cx, obj); if(iter == NULL) goto done; length = 0; while(JS_NextProperty(cx, iter, &idp)) { if(idp == JSID_VOID) break; length += 1; } array = enif_alloc(length * sizeof(ERL_NIF_TERM)); if(array == NULL) goto done; iter = JS_NewPropertyIterator(cx, obj); if(iter == NULL) goto done; index = 0; while(JS_NextProperty(cx, iter, &idp)) { if(idp == JSID_VOID) { list = enif_make_list_from_array(env, array, length); *term = enif_make_tuple1(env, list); ret = OK; goto done; } if(!JS_IdToValue(cx, idp, &val)) goto done; if(!to_erl_string(env, cx, val, &keyterm)) goto done; if(!JS_GetPropertyById(cx, obj, idp, &val)) goto done; if(!to_erl_intern(env, cx, val, &valterm)) goto done; array[index] = enif_make_tuple2(env, keyterm, valterm); index += 1; } done: if(array != NULL) enif_free(array); return ret; }
BSONObj toObject( JSObject * o , int depth = 0) { if ( ! o ) return BSONObj(); if ( JS_InstanceOf( _context , o , &bson_ro_class , 0 ) ) { BSONHolder * holder = GETHOLDER( _context , o ); assert( holder ); return holder->_obj.getOwned(); } BSONObj orig; if ( JS_InstanceOf( _context , o , &bson_class , 0 ) ) { BSONHolder * holder = GETHOLDER(_context,o); assert( holder ); if ( ! holder->_modified ) { return holder->_obj; } orig = holder->_obj; } BSONObjBuilder b; if ( ! appendSpecialDBObject( this , b , "value" , OBJECT_TO_JSVAL( o ) , o ) ) { if ( depth == 0 ) { jsval theid = getProperty( o , "_id" ); if ( ! JSVAL_IS_VOID( theid ) ) { append( b , "_id" , theid , EOO , depth + 1 ); } } JSIdArray * properties = JS_Enumerate( _context , o ); assert( properties ); for ( jsint i=0; i<properties->length; i++ ) { jsid id = properties->vector[i]; jsval nameval; assert( JS_IdToValue( _context ,id , &nameval ) ); string name = toString( nameval ); if ( depth == 0 && name == "_id" ) continue; append( b , name , getProperty( o , name.c_str() ) , orig[name].type() , depth + 1 ); } JS_DestroyIdArray( _context , properties ); } return b.obj(); }
bool jsval_to_std_map_string_string(JSContext *cx, jsval v, std::map<std::string, std::string>* ret) { if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) { return true; } JSObject* tmp = JSVAL_TO_OBJECT(v); if (!tmp) { CCLOG("%s", "jsval_to_ccvaluemap: the jsval is not an object."); return false; } JSObject* it = JS_NewPropertyIterator(cx, tmp); while (true) { jsid idp; jsval key; if (! JS_NextProperty(cx, it, &idp) || ! JS_IdToValue(cx, idp, &key)) { return false; // error } if (key == JSVAL_VOID) { break; // end of iteration } if (!JSVAL_IS_STRING(key)) { continue; // ignore integer properties } JSStringWrapper keyWrapper(JSVAL_TO_STRING(key), cx); JS::RootedValue value(cx); JS_GetPropertyById(cx, tmp, idp, &value); if (value.isString()) { JSStringWrapper valueWapper(JSVAL_TO_STRING(value), cx); ret->insert(std::make_pair(keyWrapper.get(), valueWapper.get())); } else { CCASSERT(false, "not a string"); } } return true; }
JavaClass_setPropertyById(JSContext *cx, JSObject *obj, jsid id, jsval *vp) { jclass java_class; const char *member_name; JavaClassDescriptor *class_descriptor; JavaMemberDescriptor *member_descriptor; jsval idval; JNIEnv *jEnv; JSJavaThreadState *jsj_env; JSBool result; /* printf("In JavaClass_setProperty\n"); */ /* Get the Java per-thread environment pointer for this JSContext */ jsj_env = jsj_EnterJava(cx, &jEnv); if (!jEnv) return JS_FALSE; if (!lookup_static_member_by_id(cx, jEnv, obj, &class_descriptor, id, &member_descriptor)) { jsj_ExitJava(jsj_env); return JS_FALSE; } /* Check for the case where there is a method with the given name, but no field with that name */ if (!member_descriptor->field) goto no_such_field; /* Silently fail if field value is final (immutable), as required by ECMA spec */ if (member_descriptor->field->modifiers & ACC_FINAL) { jsj_ExitJava(jsj_env); return JS_TRUE; } java_class = class_descriptor->java_class; result = jsj_SetJavaFieldValue(cx, jEnv, member_descriptor->field, java_class, *vp); jsj_ExitJava(jsj_env); return result; no_such_field: JS_IdToValue(cx, id, &idval); member_name = JS_GetStringBytes(JSVAL_TO_STRING(idval)); JS_ReportErrorNumber(cx, jsj_GetErrorMessage, NULL, JSJMSG_MISSING_STATIC, member_name, class_descriptor->name); jsj_ExitJava(jsj_env); return JS_FALSE; }
/** * gjs_get_string_id: * @context: a #JSContext * @id: a jsid that is an object hash key (could be an int or string) * @name_p place to store ASCII string version of key * * If the id is not a string ID, return false and set *name_p to %NULL. * Otherwise, return true and fill in *name_p with ASCII name of id. * * Returns: true if *name_p is non-%NULL **/ bool gjs_get_string_id (JSContext *context, jsid id, char **name_p) { JS::RootedValue id_val(context); if (!JS_IdToValue(context, id, &id_val)) return false; if (id_val.isString()) { return gjs_string_to_utf8(context, id_val, name_p); } else { *name_p = NULL; return false; } }
static JSBool js_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp) { jsval idval; jsint tiny; js_callback_t* cb; if((cb=(js_callback_t*)JS_GetPrivate(cx,obj))==NULL) return(JS_FALSE); JS_IdToValue(cx, id, &idval); tiny = JSVAL_TO_INT(idval); switch(tiny) { case PROP_TERMINATED: if(cb->terminated!=NULL) JS_ValueToBoolean(cx, *vp, (int *)cb->terminated); break; case PROP_AUTO_TERMINATE: JS_ValueToBoolean(cx,*vp,&cb->auto_terminate); break; case PROP_COUNTER: if(!JS_ValueToInt32(cx, *vp, (int32*)&cb->counter)) return JS_FALSE; break; case PROP_TIME_LIMIT: if(!JS_ValueToInt32(cx, *vp, (int32*)&cb->limit)) return JS_FALSE; break; case PROP_GC_INTERVAL: if(!JS_ValueToInt32(cx, *vp, (int32*)&cb->gc_interval)) return JS_FALSE; break; case PROP_YIELD_INTERVAL: if(!JS_ValueToInt32(cx, *vp, (int32*)&cb->yield_interval)) return JS_FALSE; break; #ifdef jscntxt_h___ case PROP_MAXBYTES: if(!JS_ValueToInt32(cx, *vp, (int32*)&cx->runtime->gcMaxBytes)) return JS_FALSE; break; #endif } return(JS_TRUE); }
static JSBool js_client_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp) { jsval idval; const char* p=NULL; int32 val=0; jsint tiny; JSString* js_str; client_t* client; if((client=(client_t*)JS_GetPrivate(cx,obj))==NULL) return(JS_FALSE); JS_IdToValue(cx, id, &idval); tiny = JSVAL_TO_INT(idval); switch(tiny) { case CLIENT_PROP_ADDR: p=client->addr; break; case CLIENT_PROP_HOST: p=client->host; break; case CLIENT_PROP_PORT: val=client->port; break; case CLIENT_PROP_TIME: val=(int32)client->time; break; case CLIENT_PROP_PROTOCOL: p=(char*)client->protocol; break; case CLIENT_PROP_USER: p=client->user; break; default: return(JS_TRUE); } if(p!=NULL) { if((js_str=JS_NewStringCopyZ(cx, p))==NULL) return(JS_FALSE); *vp = STRING_TO_JSVAL(js_str); } else *vp = INT_TO_JSVAL(val); return(JS_TRUE); }
/** * gjs_get_string_id: * @context: a #JSContext * @id: a jsid that is an object hash key (could be an int or string) * @name_p place to store ASCII string version of key * * If the id is not a string ID, return false and set *name_p to %NULL. * Otherwise, return true and fill in *name_p with ASCII name of id. * * Returns: true if *name_p is non-%NULL **/ JSBool gjs_get_string_id (JSContext *context, jsid id, char **name_p) { jsval id_val; if (!JS_IdToValue(context, id, &id_val)) return JS_FALSE; if (JSVAL_IS_STRING(id_val)) { return gjs_string_to_utf8(context, id_val, name_p); } else { *name_p = NULL; return JS_FALSE; } }
nsresult leakmonJSObjectInfo::AppendProperty(jsid aID, JSContext *aCx, leakmonObjectsInReportTable &aObjectsInReport) { jsval n; JSBool ok = JS_IdToValue(aCx, aID, &n); NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); // n should be an integer, a string, or an XML QName, // AttributeName, or AnyName // XXX This can execute JS code! How bad is that? // shaver didn't seem too scared when I described it to him. // XXX Could I avoid JS_ValueToString and still handle XML objects // correctly? JSString *nstr = JS_ValueToString(aCx, n); NS_ENSURE_TRUE(nstr, NS_ERROR_OUT_OF_MEMORY); size_t propname_len; const jschar *propname = JS_GetStringCharsAndLength(aCx, nstr, &propname_len); NS_ENSURE_TRUE(propname, NS_ERROR_OUT_OF_MEMORY); // XXX JS_GetUCProperty can execute JS code! How bad is that? // shaver didn't seem too scared when I described it to him. // Since js_GetProperty starts with a call to js_LookupProperty, // it's clear that JS_LookupUCProperty does less than // JS_GetUCProperty, so prefer Lookup over Get (although it's not // clear to me exactly what the differences are). jsval v; ok = JS_LookupPropertyById(aCx, JSVAL_TO_OBJECT(mJSValue), aID, &v); NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); leakmonJSObjectInfo *info; void *key = reinterpret_cast<void*>(JSVAL_BITS(v)); if (!aObjectsInReport.Get(key, &info)) { info = new leakmonJSObjectInfo(v); NS_ENSURE_TRUE(info, NS_ERROR_OUT_OF_MEMORY); aObjectsInReport.Put(key, info); } PropertyStruct *ps = mProperties.AppendElement(); ps->mName.Assign(reinterpret_cast<const PRUnichar*>(propname), propname_len); ps->mValue = info; return NS_OK; }
/** * gjs_get_string_id: * @context: a #JSContext * @id: a jsid that is an object hash key (could be an int or string) * @name_p place to store ASCII string version of key * * If the id is not a string ID, return false and set *name_p to %NULL. * Otherwise, return true and fill in *name_p with ASCII name of id. * * Returns: true if *name_p is non-%NULL **/ bool gjs_get_string_id (JSContext *context, jsid id, GjsAutoJSChar *name_p) { JS::RootedValue id_val(context); if (!JS_IdToValue(context, id, &id_val)) return false; if (id_val.isString()) { JS::RootedString str(context, id_val.toString()); name_p->reset(JS_EncodeStringToUTF8(context, str)); return !!*name_p; } else { return false; } }
static void add_multiopt(JSContext *cx, js_setting_t *jss, JSObject *optlist, const char *vdef) { JSIdArray *opts, *opt; int i; if((opts = JS_Enumerate(cx, optlist)) == NULL) return; for(i = 0; i < opts->length; i++) { jsval name, value; if(!JS_IdToValue(cx, opts->vector[i], &name) || !JSVAL_IS_INT(name) || !JS_GetElement(cx, optlist, JSVAL_TO_INT(name), &value) || !JSVAL_IS_OBJECT(value) || (opt = JS_Enumerate(cx, JSVAL_TO_OBJECT(value))) == NULL) continue; if(opt->length >= 2) { jsval id, title, def; if(JS_GetElement(cx, JSVAL_TO_OBJECT(value), 0, &id) && JS_GetElement(cx, JSVAL_TO_OBJECT(value), 1, &title)) { if(opt->length < 3 || !JS_GetElement(cx, JSVAL_TO_OBJECT(value), 2, &def)) def = JSVAL_FALSE; const char *k = JS_GetStringBytes(JS_ValueToString(cx, id)); if(vdef) def = !strcmp(k, vdef) ? JSVAL_TRUE : JSVAL_FALSE; settings_multiopt_add_opt_cstr(jss->jss_s, k, JS_GetStringBytes(JS_ValueToString(cx, title)), def == JSVAL_TRUE); } } JS_DestroyIdArray(cx, opt); } JS_DestroyIdArray(cx, opts); }
static JSBool js_server_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp) { jsval idval; char* ip; jsint tiny; struct in_addr in_addr; js_server_props_t* p; if((p=(js_server_props_t*)JS_GetPrivate(cx,obj))==NULL) return(JS_FALSE); JS_IdToValue(cx, id, &idval); tiny = JSVAL_TO_INT(idval); switch(tiny) { case SERVER_PROP_VER: if(p->version!=NULL) *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,p->version)); break; case SERVER_PROP_VER_DETAIL: if(p->version_detail!=NULL) *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,p->version_detail)); break; case SERVER_PROP_INTERFACE: if(p->interface_addr!=NULL) { in_addr.s_addr=*(p->interface_addr); in_addr.s_addr=htonl(in_addr.s_addr); if((ip=inet_ntoa(in_addr))!=NULL) *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(cx,ip)); } break; case SERVER_PROP_OPTIONS: if(p->options!=NULL) *vp=UINT_TO_JSVAL(*p->options); break; case SERVER_PROP_CLIENTS: if(p->clients!=NULL) *vp=UINT_TO_JSVAL(*p->clients); break; } return(JS_TRUE); }
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; }
static JSBool js_internal_resolve(JSContext *cx, JSObject *obj, jsid id) { char* name=NULL; JSBool ret; if(id != JSID_VOID && id != JSID_EMPTY) { jsval idval; JS_IdToValue(cx, id, &idval); if(JSVAL_IS_STRING(idval)) { JSSTRING_TO_MSTRING(cx, JSVAL_TO_STRING(idval), name, NULL); HANDLE_PENDING(cx); } } ret=js_SyncResolve(cx, obj, name, js_properties, js_functions, NULL, 0); if(name) free(name); return(ret); }
static JSBool js_client_resolve(JSContext *cx, JSObject *obj, jsid id) { char* name=NULL; JSBool ret; if(id != JSID_VOID && id != JSID_EMPTY) { jsval idval; JS_IdToValue(cx, id, &idval); if(JSVAL_IS_STRING(idval)) { JSSTRING_TO_MSTRING(cx, JSVAL_TO_STRING(idval), name, NULL); if(name==NULL) return JS_FALSE; } } ret=js_SyncResolve(cx, obj, name, js_client_properties, NULL, NULL, 0); if(name) free(name); return ret; }
JSBool SetProperty(JSContext* cx, JSObject* obj, jsid id, JSBool UNUSED(strict), jsval* vp) { EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj); if (cfgNs < 0 || cfgNs >= CFG_LAST) return JS_FALSE; jsval idval; if (!JS_IdToValue(cx, id, &idval)) return JS_FALSE; std::string propName; if (!ScriptInterface::FromJSVal(cx, idval, propName)) return JS_FALSE; CConfigValue *val = g_ConfigDB.CreateValue(cfgNs, propName); if (!ScriptInterface::FromJSVal(cx, *vp, val->m_String)) return JS_FALSE; return JS_TRUE; }
JSBool document_resolve(JSContext *cx, JSHandleObject obj, JSHandleId id, unsigned flags, JSObject **objp) { // If id is "all", and we're not detecting, resolve document.all=true. jsvalRoot v(cx); if (!JS_IdToValue(cx, id, v.addr())) return false; if (JSVAL_IS_STRING(v.value())) { JSString *str = JSVAL_TO_STRING(v.value()); JSFlatString *flatStr = JS_FlattenString(cx, str); if (!flatStr) return false; if (JS_FlatStringEqualsAscii(flatStr, "all") && !(flags & JSRESOLVE_DETECTING)) { JSBool ok = JS_DefinePropertyById(cx, obj, id, JSVAL_TRUE, NULL, NULL, 0); *objp = ok ? obj.value() : NULL; return ok; } } *objp = NULL; return true; }
static JSBool rpmsp_getprop(JSContext *cx, JSObject *obj, jsid id, jsval *vp) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmspClass, NULL); jsval idval; JS_IdToValue(cx, id, &idval); jsint tiny = JSVAL_TO_INT(idval); /* XXX the class has ptr == NULL, instances have ptr != NULL. */ if (ptr == NULL) return JS_TRUE; switch (tiny) { case _DEBUG: *vp = INT_TO_JSVAL(_debug); break; default: break; } return JS_TRUE; }
JSBool GetProperty(JSContext* cx, JSObject* obj, jsid id, jsval* vp) { EConfigNamespace cfgNs = GET_NS_PRIVATE(cx, obj); if (cfgNs < 0 || cfgNs >= CFG_LAST) return JS_FALSE; jsval idval; if (!JS_IdToValue(cx, id, &idval)) return JS_FALSE; std::string propName; if (!ScriptInterface::FromJSVal(cx, idval, propName)) return JS_FALSE; CConfigValue *val = g_ConfigDB.GetValue(cfgNs, propName); if (val) { JSString *js_str = JS_NewStringCopyN(cx, val->m_String.c_str(), val->m_String.size()); *vp = STRING_TO_JSVAL(js_str); } return JS_TRUE; }
bool ScriptInterface::EnumeratePropertyNamesWithPrefix(jsval obj, const char* prefix, std::vector<std::string>& out) { utf16string prefix16 (prefix, prefix+strlen(prefix)); if (! JSVAL_IS_OBJECT(obj)) return false; // TODO: log error messages JSObject* it = JS_NewPropertyIterator(m->m_cx, JSVAL_TO_OBJECT(obj)); if (!it) return false; while (true) { jsid idp; jsval val; if (! JS_NextProperty(m->m_cx, it, &idp) || ! JS_IdToValue(m->m_cx, idp, &val)) return false; if (val == JSVAL_VOID) break; // end of iteration if (! JSVAL_IS_STRING(val)) continue; // ignore integer properties JSString* name = JSVAL_TO_STRING(val); size_t len; const jschar* chars = JS_GetStringCharsAndLength(m->m_cx, name, &len); if (chars && len >= prefix16.size() && memcmp(chars, prefix16.c_str(), prefix16.size()*2) == 0) out.push_back(std::string(chars, chars+len)); // handles Unicode poorly } // Recurse up the prototype chain JSObject* prototype = JS_GetPrototype(m->m_cx, JSVAL_TO_OBJECT(obj)); if (prototype) { if (! EnumeratePropertyNamesWithPrefix(OBJECT_TO_JSVAL(prototype), prefix, out)) return false; } return true; }
bool SetRenderTargetProperty( JSContext *cx, Handle<JSObject *> object, Handle<jsid> id, bool, MutableHandle<Value> value) { RootedValue val(cx); JS_IdToValue( cx, id, MutableHandleValue(&val) ); JSString *propertyString = val.toString(); const char *propertyName=JS_EncodeString(cx, propertyString); //printf("SetRenderTargetProperty: %s\n", propertyName ); RenderTarget *target=(RenderTarget *) JS_GetPrivate( object ); if ( !strcmp( propertyName, "w" ) ) { target->w=value.toInt32(); } else if ( !strcmp( propertyName, "h" ) ) { target->h=value.toInt32(); } else if ( !strcmp( propertyName, "autoResize" ) ) { target->autoResize=value.toBoolean(); } else if ( !strcmp( propertyName, "main" ) ) { target->main=value.toBoolean(); } return true; }
bool GetRenderTargetProperty( JSContext *cx, Handle<JSObject *> object, Handle<jsid> id, MutableHandle<Value> value ) { RootedValue val(cx); JS_IdToValue( cx, id, MutableHandleValue(&val) ); JSString *propertyString = val.toString(); const char *propertyName=JS_EncodeString(cx, propertyString); //printf("GetRenderTargetProperty: %s\n", propertyName ); RenderTarget *target=(RenderTarget *) JS_GetPrivate( object ); if ( !strcmp( propertyName, "w" ) ) { value.set( INT_TO_JSVAL( target->w ) ); } else if ( !strcmp( propertyName, "h" ) ) { value.set( INT_TO_JSVAL( target->h ) ); } else if ( !strcmp( propertyName, "autoResize" ) ) { value.set( BOOLEAN_TO_JSVAL( target->autoResize ) ); } else if ( !strcmp( propertyName, "main" ) ) { value.set( BOOLEAN_TO_JSVAL( target->main ) ); } return true; }