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

Commit 21c35c6

Browse files
committed
Add Object::GetOwnPropertyAccessor
One of the tests is commented out until SetAccessorProperty is implemented
1 parent a018c34 commit 21c35c6

4 files changed

Lines changed: 91 additions & 2 deletions

File tree

ext/v8/object.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace rr {
1414
defineMethod("CreateDataProperty", &CreateDataProperty).
1515
defineMethod("DefineOwnProperty", &DefineOwnProperty).
1616
defineMethod("GetPropertyAttributes", &GetPropertyAttributes).
17+
defineMethod("GetOwnPropertyDescriptor", &GetOwnPropertyDescriptor).
1718

1819
store(&Class);
1920
}
@@ -137,6 +138,16 @@ namespace rr {
137138
));
138139
}
139140

141+
VALUE Object::GetOwnPropertyDescriptor(VALUE self, VALUE r_context, VALUE key) {
142+
Object object(self);
143+
Locker lock(object);
144+
145+
return Value::Maybe(object.getIsolate(), object->GetOwnPropertyDescriptor(
146+
Context(r_context),
147+
*rr::String(key)
148+
));
149+
}
150+
140151
Object::operator VALUE() {
141152
Isolate isolate(getIsolate());
142153
Locker lock(isolate);

ext/v8/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace rr {
1818
static VALUE CreateDataProperty(VALUE self, VALUE r_context, VALUE key, VALUE value);
1919
static VALUE DefineOwnProperty(int argc, VALUE* argv, VALUE self);
2020
static VALUE GetPropertyAttributes(VALUE self, VALUE r_context, VALUE key);
21+
static VALUE GetOwnPropertyDescriptor(VALUE self, VALUE r_context, VALUE key);
2122

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

spec/c/function_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def call(info)
6363
end
6464

6565
let(:data) { V8::C::Object::New(@isolate) }
66-
let(:callback) { FunctionCallback.new @isolate}
67-
let(:fn) { V8::C::Function::New(@isolate, callback, data)}
66+
let(:callback) { FunctionCallback.new @isolate }
67+
let(:fn) { V8::C::Function::New(@isolate, callback, data) }
6868

6969
before do
7070
expect(fn.Call(@ctx.Global(), ["world"]).Utf8Value()).to eql "ohai world"

spec/c/object_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,83 @@
114114
end
115115
end
116116

117+
describe '#GetOwnPropertyDescriptor' do
118+
it 'can read the descriptor of a data property' do
119+
o = V8::C::Object.New(@isolate)
120+
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
121+
data = V8::C::String.NewFromUtf8(@isolate, 'data')
122+
123+
expect(o.DefineOwnProperty(@ctx, key, data, V8::C::PropertyAttribute::DontEnum)).to be_successful
124+
125+
maybe = o.GetOwnPropertyDescriptor(@ctx, key)
126+
expect(maybe).to be_successful
127+
128+
descriptor = maybe.FromJust
129+
value = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'value'))
130+
writable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'writable'))
131+
get = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'get'))
132+
set = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'set'))
133+
configurable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'configurable'))
134+
enumerable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'enumerable'))
135+
136+
expect(value).to strict_eq data
137+
138+
expect(writable).to be_successful
139+
expect(writable.FromJust.Value).to be true
140+
141+
expect(get).to be_successful
142+
expect(get.FromJust).to be_a V8::C::Undefined
143+
144+
expect(set).to be_successful
145+
expect(set.FromJust).to be_a V8::C::Undefined
146+
147+
expect(configurable).to be_successful
148+
expect(configurable.FromJust.Value).to be true
149+
150+
expect(enumerable).to be_successful
151+
expect(enumerable.FromJust.Value).to be false
152+
end
153+
154+
# it 'can read the descriptor of an accessor property' do
155+
# o = V8::C::Object.New(@isolate)
156+
# key = V8::C::String.NewFromUtf8(@isolate, 'foo')
157+
# data = V8::C::String.NewFromUtf8(@isolate, 'data')
158+
#
159+
# getter = V8::C::Function.New @isolate, proc { }, V8::C::Object::New(@isolate)
160+
# setter = V8::C::Function.New @isolate, proc { }, V8::C::Object::New(@isolate)
161+
#
162+
# expect(o.SetAccessorProperty(@ctx, key, getter, setter)).to be_successful
163+
#
164+
# maybe = o.GetOwnPropertyDescriptor(@ctx, key)
165+
# expect(maybe).to be_successful
166+
#
167+
# descriptor = maybe.FromJust
168+
# value = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'value'))
169+
# writable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'writable'))
170+
# get = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'get'))
171+
# set = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'set'))
172+
# configurable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'configurable'))
173+
# enumerable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'enumerable'))
174+
#
175+
# expect(value).to be_successful
176+
#
177+
# expect(writable).to be_successful
178+
# expect(writable.FromJust.Value).to be true
179+
#
180+
# expect(get).to be_successful
181+
# expect(get.FromJust).to be_a V8::C::Undefined
182+
#
183+
# expect(set).to be_successful
184+
# expect(set.FromJust).to be_a V8::C::Undefined
185+
#
186+
# expect(configurable).to be_successful
187+
# expect(configurable.FromJust.Value).to be true
188+
#
189+
# expect(enumerable).to be_successful
190+
# expect(enumerable.FromJust.Value).to be true
191+
# end
192+
end
193+
117194
# TODO: Enable this when the methods are implemented in the extension
118195
# it 'can retrieve all property names' do
119196
# o = V8::C::Object.New

0 commit comments

Comments
 (0)