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

Side by Side Diff: src/api.cc

Issue 1642223003: [api] Make ObjectTemplate::SetNativeDataProperty() work even if the ObjectTemplate does not have a … (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Removed asserts preventing JSReceiver properties in ObjectTemplate Created 4 years, 10 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 | « no previous file | src/api-natives.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 951 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 } 962 }
963 963
964 964
965 // --- F u n c t i o n T e m p l a t e --- 965 // --- F u n c t i o n T e m p l a t e ---
966 static void InitializeFunctionTemplate( 966 static void InitializeFunctionTemplate(
967 i::Handle<i::FunctionTemplateInfo> info) { 967 i::Handle<i::FunctionTemplateInfo> info) {
968 InitializeTemplate(info, Consts::FUNCTION_TEMPLATE); 968 InitializeTemplate(info, Consts::FUNCTION_TEMPLATE);
969 info->set_flag(0); 969 info->set_flag(0);
970 } 970 }
971 971
972 static Local<ObjectTemplate> ObjectTemplateNew(
973 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
974 bool do_not_cache);
972 975
973 Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() { 976 Local<ObjectTemplate> FunctionTemplate::PrototypeTemplate() {
974 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate(); 977 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate();
975 ENTER_V8(i_isolate); 978 ENTER_V8(i_isolate);
976 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(), 979 i::Handle<i::Object> result(Utils::OpenHandle(this)->prototype_template(),
977 i_isolate); 980 i_isolate);
978 if (result->IsUndefined()) { 981 if (result->IsUndefined()) {
979 v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(i_isolate); 982 // Do not cache prototype objects.
980 result = Utils::OpenHandle(*ObjectTemplate::New(isolate)); 983 result = Utils::OpenHandle(
984 *ObjectTemplateNew(i_isolate, Local<FunctionTemplate>(), true));
981 Utils::OpenHandle(this)->set_prototype_template(*result); 985 Utils::OpenHandle(this)->set_prototype_template(*result);
982 } 986 }
983 return ToApiHandle<ObjectTemplate>(result); 987 return ToApiHandle<ObjectTemplate>(result);
984 } 988 }
985 989
986 990
987 static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info, 991 static void EnsureNotInstantiated(i::Handle<i::FunctionTemplateInfo> info,
988 const char* func) { 992 const char* func) {
989 Utils::ApiCheck(!info->instantiated(), func, 993 Utils::ApiCheck(!info->instantiated(), func,
990 "FunctionTemplate already instantiated"); 994 "FunctionTemplate already instantiated");
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 Local<ObjectTemplate> ObjectTemplate::New( 1224 Local<ObjectTemplate> ObjectTemplate::New(
1221 Isolate* isolate, v8::Local<FunctionTemplate> constructor) { 1225 Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1222 return New(reinterpret_cast<i::Isolate*>(isolate), constructor); 1226 return New(reinterpret_cast<i::Isolate*>(isolate), constructor);
1223 } 1227 }
1224 1228
1225 1229
1226 Local<ObjectTemplate> ObjectTemplate::New() { 1230 Local<ObjectTemplate> ObjectTemplate::New() {
1227 return New(i::Isolate::Current(), Local<FunctionTemplate>()); 1231 return New(i::Isolate::Current(), Local<FunctionTemplate>());
1228 } 1232 }
1229 1233
1230 1234 static Local<ObjectTemplate> ObjectTemplateNew(
1231 Local<ObjectTemplate> ObjectTemplate::New( 1235 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor,
1232 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) { 1236 bool do_not_cache) {
1233 // Changes to the environment cannot be captured in the snapshot. Expect no 1237 // Changes to the environment cannot be captured in the snapshot. Expect no
1234 // object templates when the isolate is created for serialization. 1238 // object templates when the isolate is created for serialization.
1235 DCHECK(!isolate->serializer_enabled()); 1239 DCHECK(!isolate->serializer_enabled());
1236 LOG_API(isolate, "ObjectTemplate::New"); 1240 LOG_API(isolate, "ObjectTemplate::New");
1237 ENTER_V8(isolate); 1241 ENTER_V8(isolate);
1238 i::Handle<i::Struct> struct_obj = 1242 i::Handle<i::Struct> struct_obj =
1239 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE); 1243 isolate->factory()->NewStruct(i::OBJECT_TEMPLATE_INFO_TYPE);
1240 i::Handle<i::ObjectTemplateInfo> obj = 1244 i::Handle<i::ObjectTemplateInfo> obj =
1241 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj); 1245 i::Handle<i::ObjectTemplateInfo>::cast(struct_obj);
1242 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE); 1246 InitializeTemplate(obj, Consts::OBJECT_TEMPLATE);
1247 int next_serial_number = 0;
1248 if (!do_not_cache) {
1249 next_serial_number = isolate->next_serial_number() + 1;
1250 isolate->set_next_serial_number(next_serial_number);
1251 }
1252 obj->set_serial_number(i::Smi::FromInt(next_serial_number));
1243 if (!constructor.IsEmpty()) 1253 if (!constructor.IsEmpty())
1244 obj->set_constructor(*Utils::OpenHandle(*constructor)); 1254 obj->set_constructor(*Utils::OpenHandle(*constructor));
1245 obj->set_internal_field_count(i::Smi::FromInt(0)); 1255 obj->set_internal_field_count(i::Smi::FromInt(0));
1246 return Utils::ToLocal(obj); 1256 return Utils::ToLocal(obj);
1247 } 1257 }
1248 1258
1259 Local<ObjectTemplate> ObjectTemplate::New(
1260 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1261 return ObjectTemplateNew(isolate, constructor, false);
1262 }
1249 1263
1250 // Ensure that the object template has a constructor. If no 1264 // Ensure that the object template has a constructor. If no
1251 // constructor is available we create one. 1265 // constructor is available we create one.
1252 static i::Handle<i::FunctionTemplateInfo> EnsureConstructor( 1266 static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1253 i::Isolate* isolate, 1267 i::Isolate* isolate,
1254 ObjectTemplate* object_template) { 1268 ObjectTemplate* object_template) {
1255 i::Object* obj = Utils::OpenHandle(object_template)->constructor(); 1269 i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1256 if (!obj ->IsUndefined()) { 1270 if (!obj ->IsUndefined()) {
1257 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj); 1271 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1258 return i::Handle<i::FunctionTemplateInfo>(info, isolate); 1272 return i::Handle<i::FunctionTemplateInfo>(info, isolate);
1259 } 1273 }
1260 Local<FunctionTemplate> templ = 1274 Local<FunctionTemplate> templ =
1261 FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate)); 1275 FunctionTemplate::New(reinterpret_cast<Isolate*>(isolate));
1262 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ); 1276 i::Handle<i::FunctionTemplateInfo> constructor = Utils::OpenHandle(*templ);
1263 constructor->set_instance_template(*Utils::OpenHandle(object_template)); 1277 constructor->set_instance_template(*Utils::OpenHandle(object_template));
1264 Utils::OpenHandle(object_template)->set_constructor(*constructor); 1278 Utils::OpenHandle(object_template)->set_constructor(*constructor);
1265 return constructor; 1279 return constructor;
1266 } 1280 }
1267 1281
1268 1282
1269 static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1270 i::Isolate* isolate,
1271 Template* template_obj) {
1272 return Utils::OpenHandle(template_obj);
1273 }
1274
1275
1276 // TODO(dcarney): remove this with ObjectTemplate::SetAccessor
1277 static inline i::Handle<i::TemplateInfo> GetTemplateInfo(
1278 i::Isolate* isolate,
1279 ObjectTemplate* object_template) {
1280 EnsureConstructor(isolate, object_template);
1281 return Utils::OpenHandle(object_template);
1282 }
1283
1284 template <typename Getter, typename Setter, typename Data, typename Template> 1283 template <typename Getter, typename Setter, typename Data, typename Template>
1285 static bool TemplateSetAccessor(Template* template_obj, v8::Local<Name> name, 1284 static bool TemplateSetAccessor(Template* template_obj, v8::Local<Name> name,
1286 Getter getter, Setter setter, Data data, 1285 Getter getter, Setter setter, Data data,
1287 AccessControl settings, 1286 AccessControl settings,
1288 PropertyAttribute attribute, 1287 PropertyAttribute attribute,
1289 v8::Local<AccessorSignature> signature, 1288 v8::Local<AccessorSignature> signature,
1290 bool is_special_data_property) { 1289 bool is_special_data_property) {
1291 auto isolate = Utils::OpenHandle(template_obj)->GetIsolate(); 1290 auto info = Utils::OpenHandle(template_obj);
1291 auto isolate = info->GetIsolate();
1292 ENTER_V8(isolate); 1292 ENTER_V8(isolate);
1293 i::HandleScope scope(isolate); 1293 i::HandleScope scope(isolate);
1294 auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute, 1294 auto obj = MakeAccessorInfo(name, getter, setter, data, settings, attribute,
1295 signature, is_special_data_property); 1295 signature, is_special_data_property);
1296 if (obj.is_null()) return false; 1296 if (obj.is_null()) return false;
1297 auto info = GetTemplateInfo(isolate, template_obj);
1298 i::ApiNatives::AddNativeDataProperty(isolate, info, obj); 1297 i::ApiNatives::AddNativeDataProperty(isolate, info, obj);
1299 return true; 1298 return true;
1300 } 1299 }
1301 1300
1302 1301
1303 void Template::SetNativeDataProperty(v8::Local<String> name, 1302 void Template::SetNativeDataProperty(v8::Local<String> name,
1304 AccessorGetterCallback getter, 1303 AccessorGetterCallback getter,
1305 AccessorSetterCallback setter, 1304 AccessorSetterCallback setter,
1306 v8::Local<Value> data, 1305 v8::Local<Value> data,
1307 PropertyAttribute attribute, 1306 PropertyAttribute attribute,
(...skipping 1991 matching lines...) Expand 10 before | Expand all | Expand 10 after
3299 double Value::NumberValue() const { 3298 double Value::NumberValue() const {
3300 auto obj = Utils::OpenHandle(this); 3299 auto obj = Utils::OpenHandle(this);
3301 if (obj->IsNumber()) return obj->Number(); 3300 if (obj->IsNumber()) return obj->Number();
3302 return NumberValue(ContextFromHeapObject(obj)) 3301 return NumberValue(ContextFromHeapObject(obj))
3303 .FromMaybe(std::numeric_limits<double>::quiet_NaN()); 3302 .FromMaybe(std::numeric_limits<double>::quiet_NaN());
3304 } 3303 }
3305 3304
3306 3305
3307 Maybe<int64_t> Value::IntegerValue(Local<Context> context) const { 3306 Maybe<int64_t> Value::IntegerValue(Local<Context> context) const {
3308 auto obj = Utils::OpenHandle(this); 3307 auto obj = Utils::OpenHandle(this);
3308 if (obj->IsNumber()) {
3309 return Just(NumberToInt64(*obj));
3310 }
3311 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "IntegerValue", int64_t);
3309 i::Handle<i::Object> num; 3312 i::Handle<i::Object> num;
3310 if (obj->IsNumber()) { 3313 has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
3311 num = obj; 3314 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
3312 } else { 3315 return Just(NumberToInt64(*num));
3313 PREPARE_FOR_EXECUTION_PRIMITIVE(context, "IntegerValue", int64_t);
3314 has_pending_exception = !i::Object::ToInteger(isolate, obj).ToHandle(&num);
3315 RETURN_ON_FAILED_EXECUTION_PRIMITIVE(int64_t);
3316 }
3317 return Just(num->IsSmi() ? static_cast<int64_t>(i::Smi::cast(*num)->value())
3318 : static_cast<int64_t>(num->Number()));
3319 } 3316 }
3320 3317
3321 3318
3322 int64_t Value::IntegerValue() const { 3319 int64_t Value::IntegerValue() const {
3323 auto obj = Utils::OpenHandle(this); 3320 auto obj = Utils::OpenHandle(this);
3324 if (obj->IsNumber()) { 3321 if (obj->IsNumber()) {
3325 if (obj->IsSmi()) { 3322 if (obj->IsSmi()) {
3326 return i::Smi::cast(*obj)->value(); 3323 return i::Smi::cast(*obj)->value();
3327 } else { 3324 } else {
3328 return static_cast<int64_t>(obj->Number()); 3325 return static_cast<int64_t>(obj->Number());
(...skipping 5224 matching lines...) Expand 10 before | Expand all | Expand 10 after
8553 Address callback_address = 8550 Address callback_address =
8554 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8551 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8555 VMState<EXTERNAL> state(isolate); 8552 VMState<EXTERNAL> state(isolate);
8556 ExternalCallbackScope call_scope(isolate, callback_address); 8553 ExternalCallbackScope call_scope(isolate, callback_address);
8557 callback(info); 8554 callback(info);
8558 } 8555 }
8559 8556
8560 8557
8561 } // namespace internal 8558 } // namespace internal
8562 } // namespace v8 8559 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | src/api-natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698