Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 397a47f

Browse files
committed
Add Object::Get(Own)PropertyNames
1 parent 1cfbe81 commit 397a47f

4 files changed

Lines changed: 56 additions & 0 deletions

File tree

ext/v8/array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace rr {
1313

1414
inline Array(v8::Isolate* isolate, v8::Handle<v8::Array> array) : Ref<v8::Array>(isolate, array) {}
1515
inline Array(VALUE value) : Ref<v8::Array>(value) {}
16+
17+
typedef MaybeLocal<Array, v8::Array> Maybe;
1618
};
1719

1820
}

ext/v8/object.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace rr {
1616
defineMethod("DefineOwnProperty", &DefineOwnProperty).
1717
defineMethod("GetPropertyAttributes", &GetPropertyAttributes).
1818
defineMethod("GetOwnPropertyDescriptor", &GetOwnPropertyDescriptor).
19+
defineMethod("GetPropertyNames", &GetPropertyNames).
20+
defineMethod("GetOwnPropertyNames", &GetOwnPropertyNames).
1921

2022
store(&Class);
2123
}
@@ -167,6 +169,20 @@ namespace rr {
167169
));
168170
}
169171

172+
VALUE Object::GetPropertyNames(VALUE self, VALUE r_context) {
173+
Object object(self);
174+
Locker lock(object);
175+
176+
return Array::Maybe(object.getIsolate(), object->GetPropertyNames(Context(r_context)));
177+
}
178+
179+
VALUE Object::GetOwnPropertyNames(VALUE self, VALUE r_context) {
180+
Object object(self);
181+
Locker lock(object);
182+
183+
return Array::Maybe(object.getIsolate(), object->GetOwnPropertyNames(Context(r_context)));
184+
}
185+
170186
Object::operator VALUE() {
171187
Isolate isolate(getIsolate());
172188
Locker lock(isolate);

ext/v8/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace rr {
2121
static VALUE DefineOwnProperty(int argc, VALUE* argv, VALUE self);
2222
static VALUE GetPropertyAttributes(VALUE self, VALUE r_context, VALUE key);
2323
static VALUE GetOwnPropertyDescriptor(VALUE self, VALUE r_context, VALUE key);
24+
static VALUE GetPropertyNames(VALUE self, VALUE r_context);
25+
static VALUE GetOwnPropertyNames(VALUE self, VALUE r_context);
2426

2527
inline Object(VALUE value) : Ref<v8::Object>(value) {}
2628
inline Object(v8::Isolate* isolate, v8::Handle<v8::Object> object) : Ref<v8::Object>(isolate, object) {}

spec/c/object_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,42 @@
214214
end
215215
end
216216

217+
describe '#GetPropertyNames' do
218+
it 'can get array of attribute names' do
219+
o = V8::C::Object.New(@isolate)
220+
key_one = V8::C::String.NewFromUtf8(@isolate, 'foo 1')
221+
key_two = V8::C::String.NewFromUtf8(@isolate, 'foo 2')
222+
data = V8::C::String.NewFromUtf8(@isolate, 'data')
223+
224+
expect(o.DefineOwnProperty(@ctx, key_one, data)).to be_successful
225+
expect(o.DefineOwnProperty(@ctx, key_two, data)).to be_successful
226+
227+
names = o.GetPropertyNames(@ctx)
228+
expect(names).to be_successful
229+
230+
expect(names.FromJust.Get(@ctx, 0)).to v8_eq key_one
231+
expect(names.FromJust.Get(@ctx, 1)).to v8_eq key_two
232+
end
233+
end
234+
235+
describe '#GetOwnPropertyNames' do
236+
it 'can get array of attribute names' do
237+
o = V8::C::Object.New(@isolate)
238+
key_one = V8::C::String.NewFromUtf8(@isolate, 'foo 1')
239+
key_two = V8::C::String.NewFromUtf8(@isolate, 'foo 2')
240+
data = V8::C::String.NewFromUtf8(@isolate, 'data')
241+
242+
expect(o.DefineOwnProperty(@ctx, key_one, data)).to be_successful
243+
expect(o.DefineOwnProperty(@ctx, key_two, data)).to be_successful
244+
245+
names = o.GetOwnPropertyNames(@ctx)
246+
expect(names).to be_successful
247+
248+
expect(names.FromJust.Get(@ctx, 0)).to v8_eq key_one
249+
expect(names.FromJust.Get(@ctx, 1)).to v8_eq key_two
250+
end
251+
end
252+
217253
# TODO: Enable this when the methods are implemented in the extension
218254
# it 'can retrieve all property names' do
219255
# o = V8::C::Object.New

0 commit comments

Comments
 (0)