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

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: 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
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 if (i::FLAG_new_api_accessors) {
179 // Walk the inheritance chain and copy all accessors to current object.
180 int max_number_of_properties = 0;
181 TemplateInfoT* info = *data;
182 while (info != nullptr) {
183 if (!info->property_accessors()->IsUndefined()) {
184 Object* props = info->property_accessors();
185 if (!props->IsUndefined()) {
186 Handle<Object> props_handle(props, isolate);
187 NeanderArray props_array(props_handle);
188 max_number_of_properties += props_array.length();
189 }
190 }
191 info = GetParent(info);
192 }
193
194 if (max_number_of_properties > 0) {
195 int valid_descriptors = 0;
196 // Use a temporary FixedArray to accumulate unique accessors.
197 Handle<FixedArray> array =
198 isolate->factory()->NewFixedArray(max_number_of_properties);
199
200 info = *data;
201 while (info != nullptr) {
202 // Accumulate static accessors
203 if (!info->property_accessors()->IsUndefined()) {
204 Handle<Object> props(info->property_accessors(), isolate);
205 valid_descriptors =
206 AccessorInfo::AppendUnique(props, array, valid_descriptors);
207 }
208 info = GetParent(info);
209 }
210
211 // Install accumulated static accessors.
212 for (int i = 0; i < valid_descriptors; i++) {
213 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i)));
214 JSObject::SetAccessor(obj, accessor).Assert();
215 }
216 }
217 }
218
154 auto property_list = handle(data->property_list(), isolate); 219 auto property_list = handle(data->property_list(), isolate);
155 if (property_list->IsUndefined()) return obj; 220 if (property_list->IsUndefined()) return obj;
156 // TODO(dcarney): just use a FixedArray here. 221 // TODO(dcarney): just use a FixedArray here.
157 NeanderArray properties(property_list); 222 NeanderArray properties(property_list);
158 if (properties.length() == 0) return obj; 223 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 224
163 int i = 0; 225 int i = 0;
164 for (int c = 0; c < data->number_of_properties(); c++) { 226 for (int c = 0; c < data->number_of_properties(); c++) {
165 auto name = handle(Name::cast(properties.get(i++)), isolate); 227 auto name = handle(Name::cast(properties.get(i++)), isolate);
166 auto bit = handle(properties.get(i++), isolate); 228 auto bit = handle(properties.get(i++), isolate);
167 if (bit->IsSmi()) { 229 if (bit->IsSmi()) {
168 PropertyDetails details(Smi::cast(*bit)); 230 PropertyDetails details(Smi::cast(*bit));
169 PropertyAttributes attributes = details.attributes(); 231 PropertyAttributes attributes = details.attributes();
170 PropertyKind kind = details.kind(); 232 PropertyKind kind = details.kind();
171 233
(...skipping 23 matching lines...) Expand all
195 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate); 257 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate);
196 258
197 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 259 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
198 prop_data, attributes), 260 prop_data, attributes),
199 JSObject); 261 JSObject);
200 } 262 }
201 } 263 }
202 return obj; 264 return obj;
203 } 265 }
204 266
267 void CacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number,
268 Handle<JSObject> object) {
269 auto cache = isolate->template_instantiations_cache();
270 auto new_cache = ObjectHashTable::Put(cache, serial_number, object);
271 isolate->native_context()->set_template_instantiations_cache(*new_cache);
272 }
273
274 void UnacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number) {
Toon Verwaest 2016/01/29 14:37:45 Unache? :)
Igor Sheludko 2016/02/01 20:43:55 Done.
275 auto cache = isolate->template_instantiations_cache();
276 bool was_present = false;
277 auto new_cache = ObjectHashTable::Remove(cache, serial_number, &was_present);
278 DCHECK(was_present);
279 isolate->native_context()->set_template_instantiations_cache(*new_cache);
280 }
281
205 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, 282 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
206 Handle<ObjectTemplateInfo> info) { 283 Handle<ObjectTemplateInfo> info) {
207 // Enter a new scope. Recursion could otherwise create a lot of handles. 284 // Enter a new scope. Recursion could otherwise create a lot of handles.
208 HandleScope scope(isolate); 285 HandleScope scope(isolate);
209 // Fast path. 286 // Fast path.
210 Handle<JSObject> result; 287 Handle<JSObject> result;
211 auto constructor = handle(info->constructor(), isolate); 288 auto constructor = handle(info->constructor(), isolate);
212 Handle<JSFunction> cons; 289 Handle<JSFunction> cons;
213 if (constructor->IsUndefined()) { 290 if (constructor->IsUndefined()) {
214 cons = isolate->object_function(); 291 cons = isolate->object_function();
215 } else { 292 } else {
216 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor); 293 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor);
217 ASSIGN_RETURN_ON_EXCEPTION( 294 ASSIGN_RETURN_ON_EXCEPTION(
218 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction); 295 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction);
219 } 296 }
297 auto serial_number = handle(Smi::cast(info->serial_number()), isolate);
298 if (serial_number->value()) {
299 // Probe cache.
300 auto cache = isolate->template_instantiations_cache();
301 Object* boilerplate = cache->Lookup(serial_number);
302 if (boilerplate->IsJSObject()) {
303 result = handle(JSObject::cast(boilerplate), isolate);
304 result = JSObject::DeepCopy(result, JSObject::kObjectIsShallow)
Toon Verwaest 2016/01/29 14:37:45 Object templates can contain object templates as d
Igor Sheludko 2016/02/01 20:43:55 Done. Thanks for noticing!
305 .ToHandleChecked();
Toon Verwaest 2016/01/29 14:37:45 Note that this only works because all AccessorPair
Igor Sheludko 2016/02/01 20:43:55 It could actually fail here if the stack is almost
306 return scope.CloseAndEscape(result);
307 }
308 }
220 auto object = isolate->factory()->NewJSObject(cons); 309 auto object = isolate->factory()->NewJSObject(cons);
221 ASSIGN_RETURN_ON_EXCEPTION( 310 ASSIGN_RETURN_ON_EXCEPTION(
222 isolate, result, ConfigureInstance(isolate, object, info), JSFunction); 311 isolate, result, ConfigureInstance(isolate, object, info), JSFunction);
223 // TODO(dcarney): is this necessary? 312 // TODO(dcarney): is this necessary?
224 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject"); 313 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject");
314
315 if (serial_number->value()) {
316 CacheTemplateInstantiation(isolate, serial_number, result);
317 result = JSObject::DeepCopy(result, JSObject::kObjectIsShallow)
318 .ToHandleChecked();
319 }
225 return scope.CloseAndEscape(result); 320 return scope.CloseAndEscape(result);
226 } 321 }
227 322
228 323
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, 324 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
247 Handle<FunctionTemplateInfo> data, 325 Handle<FunctionTemplateInfo> data,
248 Handle<Name> name) { 326 Handle<Name> name) {
249 auto serial_number = handle(Smi::cast(data->serial_number()), isolate); 327 auto serial_number = handle(Smi::cast(data->serial_number()), isolate);
250 // Probe cache. 328 if (serial_number->value()) {
251 if (!data->do_not_cache()) { 329 // Probe cache.
252 auto cache = isolate->function_cache(); 330 auto cache = isolate->template_instantiations_cache();
253 Object* element = cache->Lookup(serial_number); 331 Object* element = cache->Lookup(serial_number);
254 if (element->IsJSFunction()) { 332 if (element->IsJSFunction()) {
255 return handle(JSFunction::cast(element), isolate); 333 return handle(JSFunction::cast(element), isolate);
256 } 334 }
257 } 335 }
258 // Enter a new scope. Recursion could otherwise create a lot of handles. 336 // Enter a new scope. Recursion could otherwise create a lot of handles.
259 HandleScope scope(isolate); 337 HandleScope scope(isolate);
260 Handle<JSObject> prototype; 338 Handle<JSObject> prototype;
261 if (!data->remove_prototype()) { 339 if (!data->remove_prototype()) {
262 auto prototype_templ = handle(data->prototype_template(), isolate); 340 auto prototype_templ = handle(data->prototype_template(), isolate);
(...skipping 24 matching lines...) Expand all
287 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false, 365 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false,
288 Object::THROW_ON_ERROR), 366 Object::THROW_ON_ERROR),
289 MaybeHandle<JSFunction>()); 367 MaybeHandle<JSFunction>());
290 } 368 }
291 } 369 }
292 auto function = ApiNatives::CreateApiFunction( 370 auto function = ApiNatives::CreateApiFunction(
293 isolate, data, prototype, ApiNatives::JavaScriptObjectType); 371 isolate, data, prototype, ApiNatives::JavaScriptObjectType);
294 if (!name.is_null() && name->IsString()) { 372 if (!name.is_null() && name->IsString()) {
295 function->shared()->set_name(*name); 373 function->shared()->set_name(*name);
296 } 374 }
297 if (!data->do_not_cache()) { 375 if (serial_number->value()) {
298 // Cache the function. 376 // Cache the function.
299 CacheFunction(isolate, serial_number, function); 377 CacheTemplateInstantiation(isolate, serial_number, function);
300 } 378 }
301 auto result = ConfigureInstance(isolate, function, data); 379 auto result = ConfigureInstance(isolate, function, data);
302 if (result.is_null()) { 380 if (result.is_null()) {
303 // Uncache on error. 381 // Uncache on error.
304 if (!data->do_not_cache()) { 382 if (serial_number->value()) {
305 UncacheFunction(isolate, serial_number); 383 UnacheTemplateInstantiation(isolate, serial_number);
306 } 384 }
307 return MaybeHandle<JSFunction>(); 385 return MaybeHandle<JSFunction>();
308 } 386 }
309 return scope.CloseAndEscape(function); 387 return scope.CloseAndEscape(function);
310 } 388 }
311 389
312 390
313 class InvokeScope { 391 class InvokeScope {
314 public: 392 public:
315 explicit InvokeScope(Isolate* isolate) 393 explicit InvokeScope(Isolate* isolate)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 437
360 438
361 MaybeHandle<JSObject> ApiNatives::InstantiateObject( 439 MaybeHandle<JSObject> ApiNatives::InstantiateObject(
362 Handle<ObjectTemplateInfo> data) { 440 Handle<ObjectTemplateInfo> data) {
363 Isolate* isolate = data->GetIsolate(); 441 Isolate* isolate = data->GetIsolate();
364 InvokeScope invoke_scope(isolate); 442 InvokeScope invoke_scope(isolate);
365 return ::v8::internal::InstantiateObject(isolate, data); 443 return ::v8::internal::InstantiateObject(isolate, data);
366 } 444 }
367 445
368 446
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, 447 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
386 Handle<Name> name, Handle<Object> value, 448 Handle<Name> name, Handle<Object> value,
387 PropertyAttributes attributes) { 449 PropertyAttributes attributes) {
388 const int kSize = 3; 450 const int kSize = 3;
389 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); 451 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
390 auto details_handle = handle(details.AsSmi(), isolate); 452 auto details_handle = handle(details.AsSmi(), isolate);
391 Handle<Object> data[kSize] = {name, details_handle, value}; 453 Handle<Object> data[kSize] = {name, details_handle, value};
392 AddPropertyToPropertyList(isolate, info, kSize, data); 454 AddPropertyToPropertyList(isolate, info, kSize, data);
393 } 455 }
394 456
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 if (!obj->indexed_property_handler()->IsUndefined()) { 604 if (!obj->indexed_property_handler()->IsUndefined()) {
543 map->set_has_indexed_interceptor(); 605 map->set_has_indexed_interceptor();
544 } 606 }
545 607
546 // Mark instance as callable in the map. 608 // Mark instance as callable in the map.
547 if (!obj->instance_call_handler()->IsUndefined()) { 609 if (!obj->instance_call_handler()->IsUndefined()) {
548 map->set_is_callable(); 610 map->set_is_callable();
549 map->set_is_constructor(true); 611 map->set_is_constructor(true);
550 } 612 }
551 613
552 // Recursively copy parent instance templates' accessors, 614 if (!i::FLAG_new_api_accessors) {
553 // 'data' may be modified. 615 // Recursively copy parent instance templates' accessors,
554 int max_number_of_additional_properties = 0; 616 // 'data' may be modified.
555 int max_number_of_static_properties = 0; 617 int max_number_of_additional_properties = 0;
556 FunctionTemplateInfo* info = *obj; 618 int max_number_of_static_properties = 0;
557 while (true) { 619 FunctionTemplateInfo* info = *obj;
558 if (!info->instance_template()->IsUndefined()) { 620 while (true) {
559 Object* props = ObjectTemplateInfo::cast(info->instance_template()) 621 if (!i::FLAG_new_api_accessors &&
560 ->property_accessors(); 622 !info->instance_template()->IsUndefined()) {
561 if (!props->IsUndefined()) { 623 Object* props = ObjectTemplateInfo::cast(info->instance_template())
562 Handle<Object> props_handle(props, isolate); 624 ->property_accessors();
563 NeanderArray props_array(props_handle); 625 if (!props->IsUndefined()) {
564 max_number_of_additional_properties += props_array.length(); 626 Handle<Object> props_handle(props, isolate);
627 NeanderArray props_array(props_handle);
628 max_number_of_additional_properties += props_array.length();
629 }
565 } 630 }
631 if (!info->property_accessors()->IsUndefined()) {
632 Object* props = info->property_accessors();
633 if (!props->IsUndefined()) {
634 Handle<Object> props_handle(props, isolate);
635 NeanderArray props_array(props_handle);
636 max_number_of_static_properties += props_array.length();
637 }
638 }
639 Object* parent = info->parent_template();
640 if (parent->IsUndefined()) break;
641 info = FunctionTemplateInfo::cast(parent);
566 } 642 }
567 if (!info->property_accessors()->IsUndefined()) { 643
568 Object* props = info->property_accessors(); 644 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties);
569 if (!props->IsUndefined()) { 645
570 Handle<Object> props_handle(props, isolate); 646 // Use a temporary FixedArray to acculumate static accessors
571 NeanderArray props_array(props_handle); 647 int valid_descriptors = 0;
572 max_number_of_static_properties += props_array.length(); 648 Handle<FixedArray> array;
649 if (max_number_of_static_properties > 0) {
650 array =
651 isolate->factory()->NewFixedArray(max_number_of_static_properties);
652 }
653
654 while (true) {
655 // Install instance descriptors
656 if (!i::FLAG_new_api_accessors &&
657 !obj->instance_template()->IsUndefined()) {
658 Handle<ObjectTemplateInfo> instance = Handle<ObjectTemplateInfo>(
659 ObjectTemplateInfo::cast(obj->instance_template()), isolate);
660 Handle<Object> props =
661 Handle<Object>(instance->property_accessors(), isolate);
662 if (!props->IsUndefined()) {
663 Map::AppendCallbackDescriptors(map, props);
664 }
573 } 665 }
666 // Accumulate static accessors
667 if (!obj->property_accessors()->IsUndefined()) {
668 Handle<Object> props =
669 Handle<Object>(obj->property_accessors(), isolate);
670 valid_descriptors =
671 AccessorInfo::AppendUnique(props, array, valid_descriptors);
672 }
673 // Climb parent chain
674 Handle<Object> parent = Handle<Object>(obj->parent_template(), isolate);
675 if (parent->IsUndefined()) break;
676 obj = Handle<FunctionTemplateInfo>::cast(parent);
574 } 677 }
575 Object* parent = info->parent_template();
576 if (parent->IsUndefined()) break;
577 info = FunctionTemplateInfo::cast(parent);
578 }
579 678
580 Map::EnsureDescriptorSlack(map, max_number_of_additional_properties); 679 // Install accumulated static accessors
581 680 for (int i = 0; i < valid_descriptors; i++) {
582 // Use a temporary FixedArray to acculumate static accessors 681 Handle<AccessorInfo> accessor(AccessorInfo::cast(array->get(i)));
583 int valid_descriptors = 0; 682 JSObject::SetAccessor(result, accessor).Assert();
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 } 683 }
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 } 684 }
617 685
618 DCHECK(result->shared()->IsApiFunction()); 686 DCHECK(result->shared()->IsApiFunction());
619 return result; 687 return result;
620 } 688 }
621 689
622 } // namespace internal 690 } // namespace internal
623 } // namespace v8 691 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698