Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: src/api.cc

Issue 2244123005: [api] Add PropertyDescriptor and DefineProperty(). (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Minor fixes. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 3753 matching lines...) Expand 10 before | Expand all | Expand 10 after
3764 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); 3764 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3765 3765
3766 i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN); 3766 i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN);
3767 Maybe<bool> result = 3767 Maybe<bool> result =
3768 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW); 3768 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3769 has_pending_exception = result.IsNothing(); 3769 has_pending_exception = result.IsNothing();
3770 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); 3770 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3771 return result; 3771 return result;
3772 } 3772 }
3773 3773
3774 struct v8::PropertyDescriptor::PrivateData {
3775 PrivateData() : desc() {}
3776 i::PropertyDescriptor desc;
3777 };
3778
3779 v8::PropertyDescriptor::PropertyDescriptor() : private_(new PrivateData()) {}
3780
3781 // DataDescriptor
3782 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value)
3783 : private_(new PrivateData()) {
3784 private_->desc.set_value(Utils::OpenHandle(*value, true));
3785 }
3786
3787 // DataDescriptor with writable field
3788 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value,
3789 bool writable)
3790 : private_(new PrivateData()) {
3791 private_->desc.set_value(Utils::OpenHandle(*value, true));
3792 private_->desc.set_writable(writable);
3793 }
3794
3795 // AccessorDescriptor
3796 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> get,
3797 v8::Local<v8::Value> set)
3798 : private_(new PrivateData()) {
3799 DCHECK(get.IsEmpty() || get->IsUndefined() || get->IsFunction());
3800 DCHECK(set.IsEmpty() || set->IsUndefined() || set->IsFunction());
3801 private_->desc.set_get(Utils::OpenHandle(*get, true));
3802 private_->desc.set_set(Utils::OpenHandle(*set, true));
3803 }
3804
3805 v8::PropertyDescriptor::~PropertyDescriptor() { delete private_; }
3806
3807 v8::Local<Value> v8::PropertyDescriptor::value() const {
3808 DCHECK(private_->desc.has_value());
3809 return Utils::ToLocal(private_->desc.value());
3810 }
3811
3812 v8::Local<Value> v8::PropertyDescriptor::get() const {
3813 DCHECK(private_->desc.has_get());
3814 return Utils::ToLocal(private_->desc.get());
3815 }
3816
3817 v8::Local<Value> v8::PropertyDescriptor::set() const {
3818 DCHECK(private_->desc.has_set());
3819 return Utils::ToLocal(private_->desc.set());
3820 }
3821
3822 bool v8::PropertyDescriptor::has_value() const {
3823 return private_->desc.has_value();
3824 }
3825 bool v8::PropertyDescriptor::has_get() const {
3826 return private_->desc.has_get();
3827 }
3828 bool v8::PropertyDescriptor::has_set() const {
3829 return private_->desc.has_set();
3830 }
3831
3832 bool v8::PropertyDescriptor::writable() const {
3833 DCHECK(private_->desc.has_writable());
3834 return private_->desc.writable();
3835 }
3836
3837 bool v8::PropertyDescriptor::has_writable() const {
3838 return private_->desc.has_writable();
3839 }
3840
3841 void v8::PropertyDescriptor::set_enumerable(bool enumerable) {
3842 private_->desc.set_enumerable(enumerable);
3843 }
3844
3845 bool v8::PropertyDescriptor::enumerable() const {
3846 DCHECK(private_->desc.has_enumerable());
3847 return private_->desc.enumerable();
3848 }
3849
3850 bool v8::PropertyDescriptor::has_enumerable() const {
3851 return private_->desc.has_enumerable();
3852 }
3853
3854 void v8::PropertyDescriptor::set_configurable(bool configurable) {
3855 private_->desc.set_configurable(configurable);
3856 }
3857
3858 bool v8::PropertyDescriptor::configurable() const {
3859 DCHECK(private_->desc.has_configurable());
3860 return private_->desc.configurable();
3861 }
3862
3863 bool v8::PropertyDescriptor::has_configurable() const {
3864 return private_->desc.has_configurable();
3865 }
3774 3866
3775 Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context, 3867 Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
3776 v8::Local<Name> key, 3868 v8::Local<Name> key,
3777 v8::Local<Value> value, 3869 v8::Local<Value> value,
3778 v8::PropertyAttribute attributes) { 3870 v8::PropertyAttribute attributes) {
3779 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool); 3871 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool);
3780 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this); 3872 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3781 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key); 3873 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3782 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); 3874 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3783 3875
3784 if (self->IsAccessCheckNeeded() &&
3785 !isolate->MayAccess(handle(isolate->context()),
3786 i::Handle<i::JSObject>::cast(self))) {
3787 isolate->ReportFailedAccessCheck(i::Handle<i::JSObject>::cast(self));
3788 return Nothing<bool>();
3789 }
3790
3791 i::PropertyDescriptor desc; 3876 i::PropertyDescriptor desc;
3792 desc.set_writable(!(attributes & v8::ReadOnly)); 3877 desc.set_writable(!(attributes & v8::ReadOnly));
3793 desc.set_enumerable(!(attributes & v8::DontEnum)); 3878 desc.set_enumerable(!(attributes & v8::DontEnum));
3794 desc.set_configurable(!(attributes & v8::DontDelete)); 3879 desc.set_configurable(!(attributes & v8::DontDelete));
3795 desc.set_value(value_obj); 3880 desc.set_value(value_obj);
3796 Maybe<bool> success = i::JSReceiver::DefineOwnProperty( 3881 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3797 isolate, self, key_obj, &desc, i::Object::DONT_THROW); 3882 isolate, self, key_obj, &desc, i::Object::DONT_THROW);
3798 // Even though we said DONT_THROW, there might be accessors that do throw. 3883 // Even though we said DONT_THROW, there might be accessors that do throw.
3799 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); 3884 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3800 return success; 3885 return success;
3801 } 3886 }
3802 3887
3888 Maybe<bool> v8::Object::DefineProperty(v8::Local<v8::Context> context,
3889 v8::Local<Name> key,
3890 const PropertyDescriptor& descriptor) {
3891 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineProperty, bool);
3892 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3893 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3894
3895 i::PropertyDescriptor desc;
3896 if (descriptor.has_value()) {
3897 desc.set_value(Utils::OpenHandle(*descriptor.value(), true));
Jakob Kummerow 2016/08/31 14:15:13 This works, but it's just about the least efficien
Franzi 2016/08/31 20:10:51 Ok, changed it. Since defineProperty() can change
3898 }
3899 if (descriptor.has_get()) {
3900 desc.set_get(Utils::OpenHandle(*descriptor.get(), true));
3901 }
3902 if (descriptor.has_set()) {
3903 desc.set_set(Utils::OpenHandle(*descriptor.set(), true));
3904 }
3905 if (descriptor.has_enumerable()) {
3906 desc.set_enumerable(descriptor.enumerable());
3907 }
3908 if (descriptor.has_configurable()) {
3909 desc.set_configurable(descriptor.configurable());
3910 }
3911 if (descriptor.has_writable()) {
3912 desc.set_writable(descriptor.writable());
3913 }
3914
3915 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3916 isolate, self, key_obj, &desc, i::Object::DONT_THROW);
3917 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3918 return success;
3919 }
3803 3920
3804 MUST_USE_RESULT 3921 MUST_USE_RESULT
3805 static i::MaybeHandle<i::Object> DefineObjectProperty( 3922 static i::MaybeHandle<i::Object> DefineObjectProperty(
3806 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key, 3923 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key,
3807 i::Handle<i::Object> value, i::PropertyAttributes attrs) { 3924 i::Handle<i::Object> value, i::PropertyAttributes attrs) {
3808 i::Isolate* isolate = js_object->GetIsolate(); 3925 i::Isolate* isolate = js_object->GetIsolate();
3809 bool success = false; 3926 bool success = false;
3810 i::LookupIterator it = i::LookupIterator::PropertyOrElement( 3927 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
3811 isolate, js_object, key, &success, i::LookupIterator::OWN); 3928 isolate, js_object, key, &success, i::LookupIterator::OWN);
3812 if (!success) return i::MaybeHandle<i::Object>(); 3929 if (!success) return i::MaybeHandle<i::Object>();
(...skipping 5284 matching lines...) Expand 10 before | Expand all | Expand 10 after
9097 Address callback_address = 9214 Address callback_address =
9098 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 9215 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
9099 VMState<EXTERNAL> state(isolate); 9216 VMState<EXTERNAL> state(isolate);
9100 ExternalCallbackScope call_scope(isolate, callback_address); 9217 ExternalCallbackScope call_scope(isolate, callback_address);
9101 callback(info); 9218 callback(info);
9102 } 9219 }
9103 9220
9104 9221
9105 } // namespace internal 9222 } // namespace internal
9106 } // namespace v8 9223 } // namespace v8
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/counters.h » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698