BSONObj v8ToMongo( v8::Handle<v8::Object> o ){ BSONObjBuilder b; v8::Handle<v8::String> idName = String::New( "_id" ); if ( o->HasRealNamedProperty( idName ) ){ v8ToMongoElement( b , idName , "_id" , o->Get( idName ) ); } Local<v8::Array> names = o->GetPropertyNames(); for ( unsigned int i=0; i<names->Length(); i++ ){ v8::Local<v8::String> name = names->Get(v8::Integer::New(i) )->ToString(); if ( o->GetPrototype()->IsObject() && o->GetPrototype()->ToObject()->HasRealNamedProperty( name ) ) continue; v8::Local<v8::Value> value = o->Get( name ); const string sname = toSTLString( name ); if ( sname == "_id" ) continue; v8ToMongoElement( b , name , sname , value ); } return b.obj(); }
void printAllPropertyNames(v8::Handle<v8::Object> objToPrint) { v8::Local<v8::Array> allProps = objToPrint->GetPropertyNames(); std::vector<v8::Local<v8::Object> > propertyNames; for (int s=0; s < (int)allProps->Length(); ++s) { v8::Local<v8::Object>toPrint= v8::Local<v8::Object>::Cast(allProps->Get(s)); String errorMessage = "Error: error decoding first string in debug_checkCurrentContextX. "; String strVal, strVal2; bool stringDecoded = decodeString(toPrint, strVal,errorMessage); if (!stringDecoded) { SILOG(js,error,errorMessage); return; } v8::Local<v8::Value> valueToPrint = objToPrint->Get(v8::String::New(strVal.c_str(), strVal.length())); errorMessage = "Error: error decoding second string in debug_checkCurrentContextX. "; stringDecoded = decodeString(valueToPrint,strVal2,errorMessage); if (!stringDecoded) { SILOG(js,error,errorMessage); return; } std::cout<<" property "<< s <<": "<<strVal <<": "<<strVal2<<"\n"; } }
static Value v8ValueToValue(v8::Handle<v8::Value> v8Value) { v8::HandleScope scope; if (v8Value->IsArray()) { List<Value> value; const v8::Handle<v8::Array> v8Array = v8::Handle<v8::Array>::Cast(v8Value); const uint32_t size = v8Array->Length(); for (uint32_t i = 0; i < size; ++i) { if (v8Array->Has(i)) value.append(v8ValueToValue(v8Array->Get(i))); else value.append(Value()); } return Value(value); } else if (v8Value->IsObject()) { Map<String, Value> value; const v8::Handle<v8::Object> v8Object = v8Value->ToObject(); const v8::Handle<v8::Array> props = v8Object->GetPropertyNames(); const uint32_t size = props->Length(); for (uint32_t i = 0; i < size; ++i) { assert(props->Has(i)); const v8::Handle<v8::Value> name = props->Get(i); value[String(*v8::String::Utf8Value(name))] = v8ValueToValue(v8Object->Get(name)); } return Value(value); } else if (v8Value->IsBoolean()) { return Value(v8Value->BooleanValue()); } else if (v8Value->IsInt32() || v8Value->IsUint32()) { return Value(v8Value->Int32Value()); } else if (v8Value->IsNumber()) { return Value(v8Value->NumberValue()); } else if (v8Value->IsString()) { return Value(String(*v8::String::Utf8Value(v8Value))); } else { error() << "Unexpected v8 value type in JSONParser"; } // undefined or null? return Value(); }
/** Get the properties of an object (and its prototype) as a comma-delimited string */ std::string v8ObjectToString(const v8::Handle<v8::Object>& o) { v8::Local<v8::Array> properties = o->GetPropertyNames(); v8::String::Utf8Value str(properties); massert(16696 , "error converting js type to Utf8Value", *str); return std::string(*str, str.length()); }
v8::Handle<v8::Array> JS_GetObjectElementNames(v8::Handle<v8::Object> pObj) { if(pObj.IsEmpty()) return v8::Handle<v8::Array>(); return pObj->GetPropertyNames(); }