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

Commit 1cfbe81

Browse files
committed
Implement Object::SetAccessorProperty
1 parent 21c35c6 commit 1cfbe81

3 files changed

Lines changed: 82 additions & 38 deletions

File tree

ext/v8/object.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace rr {
1111
defineMethod("Has", &Has).
1212
defineMethod("Delete", &Delete).
1313
defineMethod("SetAccessor", &SetAccessor).
14+
defineMethod("SetAccessorProperty", &SetAccessorProperty).
1415
defineMethod("CreateDataProperty", &CreateDataProperty).
1516
defineMethod("DefineOwnProperty", &DefineOwnProperty).
1617
defineMethod("GetPropertyAttributes", &GetPropertyAttributes).
@@ -102,6 +103,24 @@ namespace rr {
102103
));
103104
}
104105

106+
VALUE Object::SetAccessorProperty(int argc, VALUE* argv, VALUE self) {
107+
VALUE r_context, name, getter, setter, attribute, settings;
108+
rb_scan_args(argc, argv, "33", &r_context, &name, &getter, &setter, &attribute, &settings);
109+
110+
Object object(self);
111+
Locker lock(object);
112+
113+
object->SetAccessorProperty(
114+
Name(name),
115+
*Function(getter),
116+
RTEST(setter) ? *Function(setter) : v8::Local<v8::Function>(),
117+
Enum<v8::PropertyAttribute>(attribute, v8::None),
118+
Enum<v8::AccessControl>(settings, v8::DEFAULT)
119+
);
120+
121+
return Qnil;
122+
}
123+
105124
VALUE Object::CreateDataProperty(VALUE self, VALUE r_context, VALUE key, VALUE value) {
106125
Object object(self);
107126
Locker lock(object);

ext/v8/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace rr {
1313
static VALUE GetIdentityHash(VALUE self);
1414
static VALUE Has(VALUE self, VALUE r_context, VALUE key);
1515
static VALUE Delete(VALUE self, VALUE r_context, VALUE key);
16+
1617
static VALUE SetAccessor(int argc, VALUE* argv, VALUE self);
18+
static VALUE SetAccessorProperty(int argc, VALUE* argv, VALUE self);
1719

1820
static VALUE CreateDataProperty(VALUE self, VALUE r_context, VALUE key, VALUE value);
1921
static VALUE DefineOwnProperty(int argc, VALUE* argv, VALUE self);

spec/c/object_spec.rb

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@
8181
end
8282
end
8383

84+
describe '#SetAccessorProperty' do
85+
it 'can set getters' do
86+
o = V8::C::Object.New(@isolate)
87+
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
88+
89+
get_value = V8::C::String.NewFromUtf8(@isolate, 'bar')
90+
getter = V8::C::Function.New(@isolate, proc { |info| info.GetReturnValue.Set(get_value) })
91+
92+
o.SetAccessorProperty(@ctx, key, getter)
93+
expect(o.Get(@ctx, key)).to strict_eq get_value
94+
end
95+
96+
it 'can set setters' do
97+
o = V8::C::Object.New(@isolate)
98+
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
99+
100+
set_value = nil
101+
102+
getter = V8::C::Function.New(@isolate, proc { })
103+
setter = V8::C::Function.New(@isolate, proc { |info| set_value = info[0] })
104+
105+
o.SetAccessorProperty(@ctx, key, getter, setter)
106+
expect(o.Set(@ctx, key, key)).to be_successful
107+
expect(set_value).to strict_eq key
108+
end
109+
end
110+
84111
describe '#CreateDataProperty' do
85112
it 'can set the property' do
86113
o = V8::C::Object.New(@isolate)
@@ -151,44 +178,40 @@
151178
expect(enumerable.FromJust.Value).to be false
152179
end
153180

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
181+
it 'can read the descriptor of an accessor property' do
182+
o = V8::C::Object.New(@isolate)
183+
key = V8::C::String.NewFromUtf8(@isolate, 'foo')
184+
data = V8::C::String.NewFromUtf8(@isolate, 'data')
185+
186+
getter = V8::C::Function.New @isolate, proc { }, V8::C::Object::New(@isolate)
187+
setter = V8::C::Function.New @isolate, proc { }, V8::C::Object::New(@isolate)
188+
189+
o.SetAccessorProperty(@ctx, key, getter, setter)
190+
191+
maybe = o.GetOwnPropertyDescriptor(@ctx, key)
192+
expect(maybe).to be_successful
193+
194+
descriptor = maybe.FromJust
195+
value = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'value'))
196+
writable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'writable'))
197+
get = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'get'))
198+
set = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'set'))
199+
configurable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'configurable'))
200+
enumerable = descriptor.Get(@ctx, V8::C::String.NewFromUtf8(@isolate, 'enumerable'))
201+
202+
expect(value).to be_successful
203+
expect(value.FromJust).to be_a V8::C::Undefined
204+
expect(writable).to be_successful
205+
206+
expect(get).to strict_eq getter
207+
expect(set).to strict_eq setter
208+
209+
expect(configurable).to be_successful
210+
expect(configurable.FromJust.Value).to be true
211+
212+
expect(enumerable).to be_successful
213+
expect(enumerable.FromJust.Value).to be true
214+
end
192215
end
193216

194217
# TODO: Enable this when the methods are implemented in the extension

0 commit comments

Comments
 (0)