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

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: Fix typo. 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
« no previous file with comments | « include/v8.h ('k') | src/counters.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 3766 matching lines...) Expand 10 before | Expand all | Expand 10 after
3777 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); 3777 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3778 3778
3779 i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN); 3779 i::LookupIterator it(isolate, self, index, self, i::LookupIterator::OWN);
3780 Maybe<bool> result = 3780 Maybe<bool> result =
3781 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW); 3781 i::JSReceiver::CreateDataProperty(&it, value_obj, i::Object::DONT_THROW);
3782 has_pending_exception = result.IsNothing(); 3782 has_pending_exception = result.IsNothing();
3783 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); 3783 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3784 return result; 3784 return result;
3785 } 3785 }
3786 3786
3787 struct v8::PropertyDescriptor::PrivateData {
3788 PrivateData() : desc() {}
3789 i::PropertyDescriptor desc;
3790 };
3791
3792 v8::PropertyDescriptor::PropertyDescriptor() : private_(new PrivateData()) {}
3793
3794 // DataDescriptor
3795 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value)
3796 : private_(new PrivateData()) {
3797 private_->desc.set_value(Utils::OpenHandle(*value, true));
3798 }
3799
3800 // DataDescriptor with writable field
3801 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> value,
3802 bool writable)
3803 : private_(new PrivateData()) {
3804 private_->desc.set_value(Utils::OpenHandle(*value, true));
3805 private_->desc.set_writable(writable);
3806 }
3807
3808 // AccessorDescriptor
3809 v8::PropertyDescriptor::PropertyDescriptor(v8::Local<v8::Value> get,
3810 v8::Local<v8::Value> set)
3811 : private_(new PrivateData()) {
3812 DCHECK(get.IsEmpty() || get->IsUndefined() || get->IsFunction());
3813 DCHECK(set.IsEmpty() || set->IsUndefined() || set->IsFunction());
3814 private_->desc.set_get(Utils::OpenHandle(*get, true));
3815 private_->desc.set_set(Utils::OpenHandle(*set, true));
3816 }
3817
3818 v8::PropertyDescriptor::~PropertyDescriptor() { delete private_; }
3819
3820 v8::Local<Value> v8::PropertyDescriptor::value() const {
3821 DCHECK(private_->desc.has_value());
3822 return Utils::ToLocal(private_->desc.value());
3823 }
3824
3825 v8::Local<Value> v8::PropertyDescriptor::get() const {
3826 DCHECK(private_->desc.has_get());
3827 return Utils::ToLocal(private_->desc.get());
3828 }
3829
3830 v8::Local<Value> v8::PropertyDescriptor::set() const {
3831 DCHECK(private_->desc.has_set());
3832 return Utils::ToLocal(private_->desc.set());
3833 }
3834
3835 bool v8::PropertyDescriptor::has_value() const {
3836 return private_->desc.has_value();
3837 }
3838 bool v8::PropertyDescriptor::has_get() const {
3839 return private_->desc.has_get();
3840 }
3841 bool v8::PropertyDescriptor::has_set() const {
3842 return private_->desc.has_set();
3843 }
3844
3845 bool v8::PropertyDescriptor::writable() const {
3846 DCHECK(private_->desc.has_writable());
3847 return private_->desc.writable();
3848 }
3849
3850 bool v8::PropertyDescriptor::has_writable() const {
3851 return private_->desc.has_writable();
3852 }
3853
3854 void v8::PropertyDescriptor::set_enumerable(bool enumerable) {
3855 private_->desc.set_enumerable(enumerable);
3856 }
3857
3858 bool v8::PropertyDescriptor::enumerable() const {
3859 DCHECK(private_->desc.has_enumerable());
3860 return private_->desc.enumerable();
3861 }
3862
3863 bool v8::PropertyDescriptor::has_enumerable() const {
3864 return private_->desc.has_enumerable();
3865 }
3866
3867 void v8::PropertyDescriptor::set_configurable(bool configurable) {
3868 private_->desc.set_configurable(configurable);
3869 }
3870
3871 bool v8::PropertyDescriptor::configurable() const {
3872 DCHECK(private_->desc.has_configurable());
3873 return private_->desc.configurable();
3874 }
3875
3876 bool v8::PropertyDescriptor::has_configurable() const {
3877 return private_->desc.has_configurable();
3878 }
3787 3879
3788 Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context, 3880 Maybe<bool> v8::Object::DefineOwnProperty(v8::Local<v8::Context> context,
3789 v8::Local<Name> key, 3881 v8::Local<Name> key,
3790 v8::Local<Value> value, 3882 v8::Local<Value> value,
3791 v8::PropertyAttribute attributes) { 3883 v8::PropertyAttribute attributes) {
3792 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool); 3884 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineOwnProperty, bool);
3793 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this); 3885 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3794 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key); 3886 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3795 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value); 3887 i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
3796 3888
3797 if (self->IsAccessCheckNeeded() &&
3798 !isolate->MayAccess(handle(isolate->context()),
3799 i::Handle<i::JSObject>::cast(self))) {
3800 isolate->ReportFailedAccessCheck(i::Handle<i::JSObject>::cast(self));
3801 return Nothing<bool>();
3802 }
3803
3804 i::PropertyDescriptor desc; 3889 i::PropertyDescriptor desc;
3805 desc.set_writable(!(attributes & v8::ReadOnly)); 3890 desc.set_writable(!(attributes & v8::ReadOnly));
3806 desc.set_enumerable(!(attributes & v8::DontEnum)); 3891 desc.set_enumerable(!(attributes & v8::DontEnum));
3807 desc.set_configurable(!(attributes & v8::DontDelete)); 3892 desc.set_configurable(!(attributes & v8::DontDelete));
3808 desc.set_value(value_obj); 3893 desc.set_value(value_obj);
3809 Maybe<bool> success = i::JSReceiver::DefineOwnProperty( 3894 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3810 isolate, self, key_obj, &desc, i::Object::DONT_THROW); 3895 isolate, self, key_obj, &desc, i::Object::DONT_THROW);
3811 // Even though we said DONT_THROW, there might be accessors that do throw. 3896 // Even though we said DONT_THROW, there might be accessors that do throw.
3812 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool); 3897 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3813 return success; 3898 return success;
3814 } 3899 }
3815 3900
3901 Maybe<bool> v8::Object::DefineProperty(v8::Local<v8::Context> context,
3902 v8::Local<Name> key,
3903 PropertyDescriptor& descriptor) {
3904 PREPARE_FOR_EXECUTION_PRIMITIVE(context, Object, DefineProperty, bool);
3905 i::Handle<i::JSReceiver> self = Utils::OpenHandle(this);
3906 i::Handle<i::Name> key_obj = Utils::OpenHandle(*key);
3907
3908 Maybe<bool> success = i::JSReceiver::DefineOwnProperty(
3909 isolate, self, key_obj, &descriptor.get_private()->desc,
3910 i::Object::DONT_THROW);
3911 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
3912 return success;
3913 }
3816 3914
3817 MUST_USE_RESULT 3915 MUST_USE_RESULT
3818 static i::MaybeHandle<i::Object> DefineObjectProperty( 3916 static i::MaybeHandle<i::Object> DefineObjectProperty(
3819 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key, 3917 i::Handle<i::JSObject> js_object, i::Handle<i::Object> key,
3820 i::Handle<i::Object> value, i::PropertyAttributes attrs) { 3918 i::Handle<i::Object> value, i::PropertyAttributes attrs) {
3821 i::Isolate* isolate = js_object->GetIsolate(); 3919 i::Isolate* isolate = js_object->GetIsolate();
3822 bool success = false; 3920 bool success = false;
3823 i::LookupIterator it = i::LookupIterator::PropertyOrElement( 3921 i::LookupIterator it = i::LookupIterator::PropertyOrElement(
3824 isolate, js_object, key, &success, i::LookupIterator::OWN); 3922 isolate, js_object, key, &success, i::LookupIterator::OWN);
3825 if (!success) return i::MaybeHandle<i::Object>(); 3923 if (!success) return i::MaybeHandle<i::Object>();
(...skipping 5281 matching lines...) Expand 10 before | Expand all | Expand 10 after
9107 Address callback_address = 9205 Address callback_address =
9108 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 9206 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
9109 VMState<EXTERNAL> state(isolate); 9207 VMState<EXTERNAL> state(isolate);
9110 ExternalCallbackScope call_scope(isolate, callback_address); 9208 ExternalCallbackScope call_scope(isolate, callback_address);
9111 callback(info); 9209 callback(info);
9112 } 9210 }
9113 9211
9114 9212
9115 } // namespace internal 9213 } // namespace internal
9116 } // namespace v8 9214 } // namespace v8
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/counters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698