Added v8::Object::GetProperties method that returns an array of all

the enumerable properties of an object.


git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@568 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 187879a2
......@@ -1022,6 +1022,14 @@ class EXPORT Object : public Value {
bool Has(uint32_t index);
bool Delete(uint32_t index);
/**
* Returns an array containing the names of the enumerable properties
* of this object, including properties from prototype objects. The
* array returned by this method contains the same values as would
* be enumerated by a for-in statement over this object.
*/
Local<Array> GetPropertyNames();
/**
* Get the prototype object. This does not skip objects marked to
* be skipped by __proto__ and it does not consult the security
......
......@@ -1750,6 +1750,20 @@ Local<Value> v8::Object::GetPrototype() {
}
Local<Array> v8::Object::GetPropertyNames() {
ON_BAILOUT("v8::Object::GetPropertyNames()", return Local<v8::Array>());
v8::HandleScope scope;
i::Handle<i::JSObject> self = Utils::OpenHandle(this);
i::Handle<i::FixedArray> value = i::GetKeysInFixedArrayFor(self);
// Because we use caching to speed up enumeration it is important
// to never change the result of the basic enumeration function so
// we clone the result.
i::Handle<i::FixedArray> elms = i::Factory::CopyFixedArray(value);
i::Handle<i::JSArray> result = i::Factory::NewJSArrayWithElements(elms);
return scope.Close(Utils::ToLocal(result));
}
Local<String> v8::Object::ObjectProtoToString() {
ON_BAILOUT("v8::Object::ObjectProtoToString()", return Local<v8::String>());
i::Handle<i::JSObject> self = Utils::OpenHandle(this);
......
......@@ -5033,3 +5033,44 @@ THREADED_TEST(DateAccess) {
CHECK(date->IsDate());
CHECK_EQ(1224744689038.0, v8::Handle<v8::Date>::Cast(date)->NumberValue());
}
void CheckProperties(v8::Handle<v8::Value> val, int elmc, const char* elmv[]) {
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val);
v8::Handle<v8::Array> props = obj->GetPropertyNames();
CHECK_EQ(elmc, props->Length());
for (int i = 0; i < elmc; i++) {
v8::String::Utf8Value elm(props->Get(v8::Integer::New(i)));
CHECK_EQ(elmv[i], *elm);
}
}
THREADED_TEST(PropertyEnumeration) {
v8::HandleScope scope;
LocalContext context;
v8::Handle<v8::Value> obj = v8::Script::Compile(v8::String::New(
"var result = [];"
"result[0] = {};"
"result[1] = {a: 1, b: 2};"
"result[2] = [1, 2, 3];"
"var proto = {x: 1, y: 2, z: 3};"
"var x = { __proto__: proto, w: 0, z: 1 };"
"result[3] = x;"
"result;"
))->Run();
v8::Handle<v8::Array> elms = v8::Handle<v8::Array>::Cast(obj);
CHECK_EQ(4, elms->Length());
int elmc0 = 0;
const char** elmv0 = NULL;
CheckProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0);
int elmc1 = 2;
const char* elmv1[] = {"a", "b"};
CheckProperties(elms->Get(v8::Integer::New(1)), elmc1, elmv1);
int elmc2 = 3;
const char* elmv2[] = {"0", "1", "2"};
CheckProperties(elms->Get(v8::Integer::New(2)), elmc2, elmv2);
int elmc3 = 4;
const char* elmv3[] = {"w", "z", "x", "y"};
CheckProperties(elms->Get(v8::Integer::New(3)), elmc3, elmv3);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment