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

Side by Side Diff: src/api-natives.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: Addressing comments 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 | « src/api-natives.h ('k') | src/arm/builtins-arm.cc » ('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 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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-natives.h" 5 #include "src/api-natives.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/isolate-inl.h" 8 #include "src/isolate-inl.h"
9 #include "src/lookup.h" 9 #include "src/lookup.h"
10 #include "src/messages.h" 10 #include "src/messages.h"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 switch (intrinsic) { 141 switch (intrinsic) {
142 #define GET_INTRINSIC_VALUE(name, iname) \ 142 #define GET_INTRINSIC_VALUE(name, iname) \
143 case v8::k##name: \ 143 case v8::k##name: \
144 return native_context->iname(); 144 return native_context->iname();
145 V8_INTRINSICS_LIST(GET_INTRINSIC_VALUE) 145 V8_INTRINSICS_LIST(GET_INTRINSIC_VALUE)
146 #undef GET_INTRINSIC_VALUE 146 #undef GET_INTRINSIC_VALUE
147 } 147 }
148 return nullptr; 148 return nullptr;
149 } 149 }
150 150
151 // Returns parent function template or null.
152 FunctionTemplateInfo* GetParent(FunctionTemplateInfo* data) {
153 Object* parent = data->parent_template();
154 return parent->IsUndefined() ? nullptr : FunctionTemplateInfo::cast(parent);
155 }
151 156
157 // Starting from given object template's constructor walk up the inheritance
158 // chain till a function template that has an instance template is found.
159 ObjectTemplateInfo* GetParent(ObjectTemplateInfo* data) {
160 Object* maybe_ctor = data->constructor();
161 if (maybe_ctor->IsUndefined()) return nullptr;
162 FunctionTemplateInfo* ctor = FunctionTemplateInfo::cast(maybe_ctor);
163 while (true) {
164 ctor = GetParent(ctor);
165 if (ctor == nullptr) return nullptr;
166 Object* maybe_obj = ctor->instance_template();
167 if (!maybe_obj->IsUndefined()) return ObjectTemplateInfo::cast(maybe_obj);
168 }
169 }
170
171 template <typename TemplateInfoT>
152 MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj, 172 MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj,
153 Handle<TemplateInfo> data) { 173 Handle<TemplateInfoT> data) {
174 HandleScope scope(isolate);
175 // Disable access checks while instantiating the object.
176 AccessCheckDisableScope access_check_scope(isolate, obj);
177
178 // Walk the inheritance chain and copy all accessors to current object.
179 int max_number_of_properties = 0;
180 TemplateInfoT* info = *data;
181 while (info != nullptr) {
182 if (!info->property_accessors()->IsUndefined()) {
183 Object* props = info->property_accessors();
184 if (!props->IsUndefined()) {
185 Handle<Object> props_handle(props, isolate);
186 NeanderArray props_array(props_handle);
187 max_number_of_properties += props_array.length();
188 }
189 }
190 info = GetParent(info);
191 }
192
193 if (max_number_of_properties > 0) {
194 int valid_descriptors = 0;
195 // Use a temporary FixedArray to accumulate unique accessors.
196 Handle<FixedArray> array =
197 isolate->factory()->NewFixedArray(max_number_of_properties);
198
199 info = *data;
200 while (info != nullptr) {
201 // Accumulate static accessors
202 if (!info->property_accessors()->IsUndefined()) {
203 Handle<Object> props(info->property_accessors(), isolate);
204 valid_descriptors =
205 AccessorInfo::AppendUnique(props, array, valid_descriptors);
206 }
207 info = GetParent(info);
208 }
209
210 // Install accumulated static accessors.
Toon Verwaest 2016/02/02 09:48:49 remove "static"
211 for (int i = 0; i < valid_descriptors; i++) {
212 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i)));
213 JSObject::SetAccessor(obj, accessor).Assert();
214 }
215 }
216
154 auto property_list = handle(data->property_list(), isolate); 217 auto property_list = handle(data->property_list(), isolate);
155 if (property_list->IsUndefined()) return obj; 218 if (property_list->IsUndefined()) return obj;
156 // TODO(dcarney): just use a FixedArray here. 219 // TODO(dcarney): just use a FixedArray here.
157 NeanderArray properties(property_list); 220 NeanderArray properties(property_list);
158 if (properties.length() == 0) return obj; 221 if (properties.length() == 0) return obj;
159 HandleScope scope(isolate);
160 // Disable access checks while instantiating the object.
161 AccessCheckDisableScope access_check_scope(isolate, obj);
162 222
163 int i = 0; 223 int i = 0;
164 for (int c = 0; c < data->number_of_properties(); c++) { 224 for (int c = 0; c < data->number_of_properties(); c++) {
165 auto name = handle(Name::cast(properties.get(i++)), isolate); 225 auto name = handle(Name::cast(properties.get(i++)), isolate);
166 auto bit = handle(properties.get(i++), isolate); 226 auto bit = handle(properties.get(i++), isolate);
167 if (bit->IsSmi()) { 227 if (bit->IsSmi()) {
168 PropertyDetails details(Smi::cast(*bit)); 228 PropertyDetails details(Smi::cast(*bit));
169 PropertyAttributes attributes = details.attributes(); 229 PropertyAttributes attributes = details.attributes();
170 PropertyKind kind = details.kind(); 230 PropertyKind kind = details.kind();
171 231
172 if (kind == kData) { 232 if (kind == kData) {
173 auto prop_data = handle(properties.get(i++), isolate); 233 auto prop_data = handle(properties.get(i++), isolate);
234 // JSReceivers could cause cross-context leaks therefore they must
235 // never appear as data properties.
236 DCHECK(!prop_data->IsJSReceiver());
174 237
175 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 238 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
176 prop_data, attributes), 239 prop_data, attributes),
177 JSObject); 240 JSObject);
178 } else { 241 } else {
179 auto getter = handle(properties.get(i++), isolate); 242 auto getter = handle(properties.get(i++), isolate);
180 auto setter = handle(properties.get(i++), isolate); 243 auto setter = handle(properties.get(i++), isolate);
181 RETURN_ON_EXCEPTION(isolate, 244 RETURN_ON_EXCEPTION(isolate,
182 DefineAccessorProperty(isolate, obj, name, getter, 245 DefineAccessorProperty(isolate, obj, name, getter,
183 setter, attributes), 246 setter, attributes),
(...skipping 11 matching lines...) Expand all
195 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate); 258 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate);
196 259
197 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 260 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
198 prop_data, attributes), 261 prop_data, attributes),
199 JSObject); 262 JSObject);
200 } 263 }
201 } 264 }
202 return obj; 265 return obj;
203 } 266 }
204 267
268 void CacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number,
269 Handle<JSObject> object) {
270 auto cache = isolate->template_instantiations_cache();
271 auto new_cache = ObjectHashTable::Put(cache, serial_number, object);
272 isolate->native_context()->set_template_instantiations_cache(*new_cache);
273 }
274
275 void UncacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number) {
276 auto cache = isolate->template_instantiations_cache();
277 bool was_present = false;
278 auto new_cache = ObjectHashTable::Remove(cache, serial_number, &was_present);
279 DCHECK(was_present);
280 isolate->native_context()->set_template_instantiations_cache(*new_cache);
281 }
282
205 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, 283 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
206 Handle<ObjectTemplateInfo> info) { 284 Handle<ObjectTemplateInfo> info) {
207 // Enter a new scope. Recursion could otherwise create a lot of handles. 285 // Enter a new scope. Recursion could otherwise create a lot of handles.
208 HandleScope scope(isolate); 286 HandleScope scope(isolate);
209 // Fast path. 287 // Fast path.
210 Handle<JSObject> result; 288 Handle<JSObject> result;
211 auto constructor = handle(info->constructor(), isolate); 289 auto constructor = handle(info->constructor(), isolate);
212 Handle<JSFunction> cons; 290 Handle<JSFunction> cons;
213 if (constructor->IsUndefined()) { 291 if (constructor->IsUndefined()) {
214 cons = isolate->object_function(); 292 cons = isolate->object_function();
215 } else { 293 } else {
216 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor); 294 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor);
217 ASSIGN_RETURN_ON_EXCEPTION( 295 ASSIGN_RETURN_ON_EXCEPTION(
218 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction); 296 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction);
219 } 297 }
298 auto serial_number = handle(Smi::cast(info->serial_number()), isolate);
299 if (serial_number->value()) {
300 // Probe cache.
301 auto cache = isolate->template_instantiations_cache();
302 Object* boilerplate = cache->Lookup(serial_number);
303 if (boilerplate->IsJSObject()) {
304 result = handle(JSObject::cast(boilerplate), isolate);
305 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, JSObject::DeepCopy(result),
306 JSObject);
307 return scope.CloseAndEscape(result);
308 }
309 }
220 auto object = isolate->factory()->NewJSObject(cons); 310 auto object = isolate->factory()->NewJSObject(cons);
221 ASSIGN_RETURN_ON_EXCEPTION( 311 ASSIGN_RETURN_ON_EXCEPTION(
222 isolate, result, ConfigureInstance(isolate, object, info), JSFunction); 312 isolate, result, ConfigureInstance(isolate, object, info), JSFunction);
223 // TODO(dcarney): is this necessary? 313 // TODO(dcarney): is this necessary?
224 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject"); 314 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject");
315
316 if (serial_number->value()) {
317 CacheTemplateInstantiation(isolate, serial_number, result);
318 ASSIGN_RETURN_ON_EXCEPTION(isolate, result, JSObject::DeepCopy(result),
319 JSObject);
320 }
225 return scope.CloseAndEscape(result); 321 return scope.CloseAndEscape(result);
226 } 322 }
227 323
228 324
229 void CacheFunction(Isolate* isolate, Handle<Smi> serial_number,
230 Handle<JSFunction> function) {
231 auto cache = isolate->function_cache();
232 auto new_cache = ObjectHashTable::Put(cache, serial_number, function);
233 isolate->native_context()->set_function_cache(*new_cache);
234 }
235
236
237 void UncacheFunction(Isolate* isolate, Handle<Smi> serial_number) {
238 auto cache = isolate->function_cache();
239 bool was_present = false;
240 auto new_cache = ObjectHashTable::Remove(cache, serial_number, &was_present);
241 DCHECK(was_present);
242 isolate->native_context()->set_function_cache(*new_cache);
243 }
244
245
246 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate, 325 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
247 Handle<FunctionTemplateInfo> data, 326 Handle<FunctionTemplateInfo> data,
248 Handle<Name> name) { 327 Handle<Name> name) {
249 auto serial_number = handle(Smi::cast(data->serial_number()), isolate); 328 auto serial_number = handle(Smi::cast(data->serial_number()), isolate);
250 // Probe cache. 329 if (serial_number->value()) {
251 if (!data->do_not_cache()) { 330 // Probe cache.
252 auto cache = isolate->function_cache(); 331 auto cache = isolate->template_instantiations_cache();
253 Object* element = cache->Lookup(serial_number); 332 Object* element = cache->Lookup(serial_number);
254 if (element->IsJSFunction()) { 333 if (element->IsJSFunction()) {
255 return handle(JSFunction::cast(element), isolate); 334 return handle(JSFunction::cast(element), isolate);
256 } 335 }
257 } 336 }
258 // Enter a new scope. Recursion could otherwise create a lot of handles. 337 // Enter a new scope. Recursion could otherwise create a lot of handles.
259 HandleScope scope(isolate); 338 HandleScope scope(isolate);
260 Handle<JSObject> prototype; 339 Handle<JSObject> prototype;
261 if (!data->remove_prototype()) { 340 if (!data->remove_prototype()) {
262 auto prototype_templ = handle(data->prototype_template(), isolate); 341 auto prototype_templ = handle(data->prototype_template(), isolate);
(...skipping 24 matching lines...) Expand all
287 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false, 366 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false,
288 Object::THROW_ON_ERROR), 367 Object::THROW_ON_ERROR),
289 MaybeHandle<JSFunction>()); 368 MaybeHandle<JSFunction>());
290 } 369 }
291 } 370 }
292 auto function = ApiNatives::CreateApiFunction( 371 auto function = ApiNatives::CreateApiFunction(
293 isolate, data, prototype, ApiNatives::JavaScriptObjectType); 372 isolate, data, prototype, ApiNatives::JavaScriptObjectType);
294 if (!name.is_null() && name->IsString()) { 373 if (!name.is_null() && name->IsString()) {
295 function->shared()->set_name(*name); 374 function->shared()->set_name(*name);
296 } 375 }
297 if (!data->do_not_cache()) { 376 if (serial_number->value()) {
298 // Cache the function. 377 // Cache the function.
299 CacheFunction(isolate, serial_number, function); 378 CacheTemplateInstantiation(isolate, serial_number, function);
300 } 379 }
301 auto result = ConfigureInstance(isolate, function, data); 380 auto result = ConfigureInstance(isolate, function, data);
302 if (result.is_null()) { 381 if (result.is_null()) {
303 // Uncache on error. 382 // Uncache on error.
304 if (!data->do_not_cache()) { 383 if (serial_number->value()) {
305 UncacheFunction(isolate, serial_number); 384 UncacheTemplateInstantiation(isolate, serial_number);
306 } 385 }
307 return MaybeHandle<JSFunction>(); 386 return MaybeHandle<JSFunction>();
308 } 387 }
309 return scope.CloseAndEscape(function); 388 return scope.CloseAndEscape(function);
310 } 389 }
311 390
312 391
313 class InvokeScope { 392 class InvokeScope {
314 public: 393 public:
315 explicit InvokeScope(Isolate* isolate) 394 explicit InvokeScope(Isolate* isolate)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 438
360 439
361 MaybeHandle<JSObject> ApiNatives::InstantiateObject( 440 MaybeHandle<JSObject> ApiNatives::InstantiateObject(
362 Handle<ObjectTemplateInfo> data) { 441 Handle<ObjectTemplateInfo> data) {
363 Isolate* isolate = data->GetIsolate(); 442 Isolate* isolate = data->GetIsolate();
364 InvokeScope invoke_scope(isolate); 443 InvokeScope invoke_scope(isolate);
365 return ::v8::internal::InstantiateObject(isolate, data); 444 return ::v8::internal::InstantiateObject(isolate, data);
366 } 445 }
367 446
368 447
369 MaybeHandle<FunctionTemplateInfo> ApiNatives::ConfigureInstance(
370 Isolate* isolate, Handle<FunctionTemplateInfo> desc,
371 Handle<JSObject> instance) {
372 // Configure the instance by adding the properties specified by the
373 // instance template.
374 if (desc->instance_template()->IsUndefined()) return desc;
375 InvokeScope invoke_scope(isolate);
376 Handle<ObjectTemplateInfo> instance_template(
377 ObjectTemplateInfo::cast(desc->instance_template()), isolate);
378 RETURN_ON_EXCEPTION(isolate, ::v8::internal::ConfigureInstance(
379 isolate, instance, instance_template),
380 FunctionTemplateInfo);
381 return desc;
382 }
383
384
385 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info, 448 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
386 Handle<Name> name, Handle<Object> value, 449 Handle<Name> name, Handle<Object> value,
387 PropertyAttributes attributes) { 450 PropertyAttributes attributes) {
451 // JSReceivers could cause cross-context leaks therefore they must
452 // never appear as data properties.
453 CHECK(!value->IsJSReceiver());
388 const int kSize = 3; 454 const int kSize = 3;
389 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); 455 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
390 auto details_handle = handle(details.AsSmi(), isolate); 456 auto details_handle = handle(details.AsSmi(), isolate);
391 Handle<Object> data[kSize] = {name, details_handle, value}; 457 Handle<Object> data[kSize] = {name, details_handle, value};
392 AddPropertyToPropertyList(isolate, info, kSize, data); 458 AddPropertyToPropertyList(isolate, info, kSize, data);
393 } 459 }
394 460
395 461
396 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info, 462 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
397 Handle<Name> name, v8::Intrinsic intrinsic, 463 Handle<Name> name, v8::Intrinsic intrinsic,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 if (!obj->indexed_property_handler()->IsUndefined()) { 608 if (!obj->indexed_property_handler()->IsUndefined()) {
543 map->set_has_indexed_interceptor(); 609 map->set_has_indexed_interceptor();
544 } 610 }
545 611
546 // Mark instance as callable in the map. 612 // Mark instance as callable in the map.
547 if (!obj->instance_call_handler()->IsUndefined()) { 613 if (!obj->instance_call_handler()->IsUndefined()) {
548 map->set_is_callable(); 614 map->set_is_callable();
549 map->set_is_constructor(true); 615 map->set_is_constructor(true);
550 } 616 }
551 617
552 // Recursively copy parent instance templates' accessors,
553 // 'data' may be modified.
554 int max_number_of_additional_properties = 0;
555 int max_number_of_static_properties = 0;
556 FunctionTemplateInfo* info = *obj;
557 while (true) {
558 if (!info->instance_template()->IsUndefined()) {
559 Object* props = ObjectTemplateInfo::cast(info->instance_template())
560 ->property_accessors();
561 if (!props->IsUndefined()) {
562 Handle<Object> props_handle(props, isolate);
563 NeanderArray props_array(props_handle);
564 max_number_of_additional_properties += props_array.length();
565 }
566 }
567 if (!info->property_accessors()->IsUndefined()) {
568 Object* props = info->property_accessors();
569 if (!props->IsUndefined()) {
570 Handle<Object> props_handle(props, isolate);
571 NeanderArray props_array(props_handle);
572 max_number_of_static_properties += props_array.length();
573 }
574 }
575 Object* parent = info->parent_template();
576 if (parent->IsUndefined()) break;
577 info = FunctionTemplateInfo::cast(parent);
578 }
579
580 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
581
582 // Use a temporary FixedArray to acculumate static accessors
583 int valid_descriptors = 0;
584 Handle<FixedArray> array;
585 if (max_number_of_static_properties > 0) {
586 array = isolate->factory()->NewFixedArray(max_number_of_static_properties);
587 }
588
589 while (true) {
590 // Install instance descriptors
591 if (!obj->instance_template()->IsUndefined()) {
592 Handle<ObjectTemplateInfo> instance = Handle<ObjectTemplateInfo>(
593 ObjectTemplateInfo::cast(obj->instance_template()), isolate);
594 Handle<Object> props =
595 Handle<Object>(instance->property_accessors(), isolate);
596 if (!props->IsUndefined()) {
597 Map::AppendCallbackDescriptors(map, props);
598 }
599 }
600 // Accumulate static accessors
601 if (!obj->property_accessors()->IsUndefined()) {
602 Handle<Object> props = Handle<Object>(obj->property_accessors(), isolate);
603 valid_descriptors =
604 AccessorInfo::AppendUnique(props, array, valid_descriptors);
605 }
606 // Climb parent chain
607 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate);
608 if (parent->IsUndefined()) break;
609 obj = Handle<FunctionTemplateInfo>::cast(parent);
610 }
611
612 // Install accumulated static accessors
613 for (int i = 0; i < valid_descriptors; i++) {
614 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i)));
615 JSObject::SetAccessor(result, accessor).Assert();
616 }
617
618 DCHECK(result->shared()->IsApiFunction()); 618 DCHECK(result->shared()->IsApiFunction());
619 return result; 619 return result;
620 } 620 }
621 621
622 } // namespace internal 622 } // namespace internal
623 } // namespace v8 623 } // namespace v8
OLDNEW
« no previous file with comments | « src/api-natives.h ('k') | src/arm/builtins-arm.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698