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

Commit d849799

Browse files
committed
Add Object::GetPrototype and Object::SetPrototype
1 parent 397a47f commit d849799

3 files changed

Lines changed: 53 additions & 30 deletions

File tree

ext/v8/object.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace rr {
1818
defineMethod("GetOwnPropertyDescriptor", &GetOwnPropertyDescriptor).
1919
defineMethod("GetPropertyNames", &GetPropertyNames).
2020
defineMethod("GetOwnPropertyNames", &GetOwnPropertyNames).
21+
defineMethod("GetPrototype", &GetPrototype).
22+
defineMethod("SetPrototype", &SetPrototype).
2123

2224
store(&Class);
2325
}
@@ -183,6 +185,23 @@ namespace rr {
183185
return Array::Maybe(object.getIsolate(), object->GetOwnPropertyNames(Context(r_context)));
184186
}
185187

188+
VALUE Object::GetPrototype(VALUE self) {
189+
Object object(self);
190+
Locker lock(object);
191+
192+
return Value(object.getIsolate(), object->GetPrototype());
193+
}
194+
195+
VALUE Object::SetPrototype(VALUE self, VALUE r_context, VALUE prototype) {
196+
Object object(self);
197+
Locker lock(object);
198+
199+
return Bool::Maybe(object->SetPrototype(
200+
Context(r_context),
201+
*Value(prototype)
202+
));
203+
}
204+
186205
Object::operator VALUE() {
187206
Isolate isolate(getIsolate());
188207
Locker lock(isolate);

ext/v8/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace rr {
2323
static VALUE GetOwnPropertyDescriptor(VALUE self, VALUE r_context, VALUE key);
2424
static VALUE GetPropertyNames(VALUE self, VALUE r_context);
2525
static VALUE GetOwnPropertyNames(VALUE self, VALUE r_context);
26+
static VALUE GetPrototype(VALUE self);
27+
static VALUE SetPrototype(VALUE self, VALUE r_context, VALUE prototype);
2628

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

spec/c/object_spec.rb

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -250,34 +250,36 @@
250250
end
251251
end
252252

253-
# TODO: Enable this when the methods are implemented in the extension
254-
# it 'can retrieve all property names' do
255-
# o = V8::C::Object.New
256-
# o.Set(V8::C::String.New('foo'), V8::C::String.New('bar'))
257-
# o.Set(V8::C::String.New('baz'), V8::C::String.New('bang'))
258-
#
259-
# names = o.GetPropertyNames()
260-
# names.Length().should eql 2
261-
# names.Get(0).Utf8Value().should eql 'foo'
262-
# names.Get(1).Utf8Value().should eql 'baz'
263-
# end
264-
#
265-
# it 'can set an accessor from ruby' do
266-
# o = V8::C::Object.New
267-
# property = V8::C::String.New('statement')
268-
# callback_data = V8::C::String.New('I am Legend')
269-
# left = V8::C::String.New('Yo! ')
270-
# getter = proc do |name, info|
271-
# info.This().StrictEquals(o).should be_true
272-
# info.Holder().StrictEquals(o).should be_true
273-
# V8::C::String::Concat(left, info.Data())
274-
# end
275-
# setter = proc do |name, value, info|
276-
# left = value
277-
# end
278-
# o.SetAccessor(property, getter, setter, callback_data)
279-
# o.Get(property).Utf8Value().should eql 'Yo! I am Legend'
280-
# o.Set(property, V8::C::String::New('Bro! '))
281-
# o.Get(property).Utf8Value().should eql 'Bro! I am Legend'
282-
# end
253+
context 'with prototypes' do
254+
let(:o) { V8::C::Object.New(@isolate) }
255+
let(:prototype) { V8::C::Object.New(@isolate) }
256+
let(:prototype_key) { V8::C::String.NewFromUtf8(@isolate, 'foo') }
257+
let(:o_key) { V8::C::String.NewFromUtf8(@isolate, 'bar') }
258+
259+
before do
260+
expect(prototype.Set(@ctx, prototype_key, prototype_key)).to be_successful
261+
expect(o.Set(@ctx, o_key, o_key)).to be_successful
262+
263+
expect(o.SetPrototype(@ctx, prototype)).to be_successful
264+
end
265+
266+
it 'can set the object prototype' do
267+
expect(o.Get(@ctx, prototype_key)).to strict_eq prototype_key
268+
end
269+
270+
it 'can enumerate only own properties' do
271+
names = o.GetOwnPropertyNames(@ctx)
272+
273+
expect(names).to be_successful
274+
expect(names.FromJust.Get(@ctx, 0)).to v8_eq o_key
275+
end
276+
277+
it 'can enumerate all properties' do
278+
names = o.GetPropertyNames(@ctx)
279+
280+
expect(names).to be_successful
281+
expect(names.FromJust.Get(@ctx, 0)).to v8_eq o_key
282+
expect(names.FromJust.Get(@ctx, 1)).to v8_eq prototype_key
283+
end
284+
end
283285
end

0 commit comments

Comments
 (0)