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

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: 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 | « 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 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 accessors.
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);
174
175 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 234 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
176 prop_data, attributes), 235 prop_data, attributes),
177 JSObject); 236 JSObject);
178 } else { 237 } else {
179 auto getter = handle(properties.get(i++), isolate); 238 auto getter = handle(properties.get(i++), isolate);
180 auto setter = handle(properties.get(i++), isolate); 239 auto setter = handle(properties.get(i++), isolate);
181 RETURN_ON_EXCEPTION(isolate, 240 RETURN_ON_EXCEPTION(isolate,
182 DefineAccessorProperty(isolate, obj, name, getter, 241 DefineAccessorProperty(isolate, obj, name, getter,
183 setter, attributes), 242 setter, attributes),
184 JSObject); 243 JSObject);
(...skipping 10 matching lines...) Expand all
195 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate); 254 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate);
196 255
197 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 256 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
198 prop_data, attributes), 257 prop_data, attributes),
199 JSObject); 258 JSObject);
200 } 259 }
201 } 260 }
202 return obj; 261 return obj;
203 } 262 }
204 263
264 void CacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number,
265 Handle<JSObject> object) {
266 auto cache = isolate->template_instantiations_cache();
267 auto new_cache = ObjectHashTable::Put(cache, serial_number, object);
268 isolate->native_context()->set_template_instantiations_cache(*new_cache);
269 }
270
271 void UncacheTemplateInstantiation(Isolate* isolate, Handle<Smi> serial_number) {
272 auto cache = isolate->template_instantiations_cache();
273 bool was_present = false;
274 auto new_cache = ObjectHashTable::Remove(cache, serial_number, &was_present);
275 DCHECK(was_present);
276 isolate->native_context()->set_template_instantiations_cache(*new_cache);
277 }
278
205 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, 279 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
206 Handle<ObjectTemplateInfo> info) { 280 Handle<ObjectTemplateInfo> info) {
207 // Enter a new scope. Recursion could otherwise create a lot of handles. 281 // Enter a new scope. Recursion could otherwise create a lot of handles.
208 HandleScope scope(isolate); 282 HandleScope scope(isolate);
209 // Fast path. 283 // Fast path.
210 Handle<JSObject> result; 284 Handle<JSObject> result;
211 auto constructor = handle(info->constructor(), isolate); 285 auto constructor = handle(info->constructor(), isolate);
212 Handle<JSFunction> cons; 286 Handle<JSFunction> cons;
213 if (constructor->IsUndefined()) { 287 if (constructor->IsUndefined()) {
214 cons = isolate->object_function(); 288 cons = isolate->object_function();
215 } else { 289 } else {
216 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor); 290 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor);
217 ASSIGN_RETURN_ON_EXCEPTION( 291 ASSIGN_RETURN_ON_EXCEPTION(
218 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction); 292 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction);
219 } 293 }
294 auto serial_number = handle(Smi::cast(info->serial_number()), isolate);
295 if (serial_number->value()) {
296 // Probe cache.
297 auto cache = isolate->template_instantiations_cache();
298 Object* boilerplate = cache->Lookup(serial_number);
299 if (boilerplate->IsJSObject()) {
300 result = handle(JSObject::cast(boilerplate), isolate);
301 ASSIGN_RETURN_ON_EXCEPTION(
302 isolate, result, JSObject::DeepCopyApiBoilerplate(result), JSObject);
303 return scope.CloseAndEscape(result);
304 }
305 }
220 auto object = isolate->factory()->NewJSObject(cons); 306 auto object = isolate->factory()->NewJSObject(cons);
221 ASSIGN_RETURN_ON_EXCEPTION( 307 ASSIGN_RETURN_ON_EXCEPTION(
222 isolate, result, ConfigureInstance(isolate, object, info), JSFunction); 308 isolate, result, ConfigureInstance(isolate, object, info), JSFunction);
223 // TODO(dcarney): is this necessary? 309 // TODO(dcarney): is this necessary?
224 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject"); 310 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject");
311
312 if (serial_number->value()) {
313 CacheTemplateInstantiation(isolate, serial_number, result);
314 ASSIGN_RETURN_ON_EXCEPTION(
315 isolate, result, JSObject::DeepCopyApiBoilerplate(result), JSObject);
316 }
225 return scope.CloseAndEscape(result); 317 return scope.CloseAndEscape(result);
226 } 318 }
227 319
228 320
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, 321 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
247 Handle<FunctionTemplateInfo> data, 322 Handle<FunctionTemplateInfo> data,
248 Handle<Name> name) { 323 Handle<Name> name) {
249 auto serial_number = handle(Smi::cast(data->serial_number()), isolate); 324 auto serial_number = handle(Smi::cast(data->serial_number()), isolate);
250 // Probe cache. 325 if (serial_number->value()) {
251 if (!data->do_not_cache()) { 326 // Probe cache.
252 auto cache = isolate->function_cache(); 327 auto cache = isolate->template_instantiations_cache();
253 Object* element = cache->Lookup(serial_number); 328 Object* element = cache->Lookup(serial_number);
254 if (element->IsJSFunction()) { 329 if (element->IsJSFunction()) {
255 return handle(JSFunction::cast(element), isolate); 330 return handle(JSFunction::cast(element), isolate);
256 } 331 }
257 } 332 }
258 // Enter a new scope. Recursion could otherwise create a lot of handles. 333 // Enter a new scope. Recursion could otherwise create a lot of handles.
259 HandleScope scope(isolate); 334 HandleScope scope(isolate);
260 Handle<JSObject> prototype; 335 Handle<JSObject> prototype;
261 if (!data->remove_prototype()) { 336 if (!data->remove_prototype()) {
262 auto prototype_templ = handle(data->prototype_template(), isolate); 337 auto prototype_templ = handle(data->prototype_template(), isolate);
(...skipping 24 matching lines...) Expand all
287 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false, 362 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false,
288 Object::THROW_ON_ERROR), 363 Object::THROW_ON_ERROR),
289 MaybeHandle<JSFunction>()); 364 MaybeHandle<JSFunction>());
290 } 365 }
291 } 366 }
292 auto function = ApiNatives::CreateApiFunction( 367 auto function = ApiNatives::CreateApiFunction(
293 isolate, data, prototype, ApiNatives::JavaScriptObjectType); 368 isolate, data, prototype, ApiNatives::JavaScriptObjectType);
294 if (!name.is_null() && name->IsString()) { 369 if (!name.is_null() && name->IsString()) {
295 function->shared()->set_name(*name); 370 function->shared()->set_name(*name);
296 } 371 }
297 if (!data->do_not_cache()) { 372 if (serial_number->value()) {
298 // Cache the function. 373 // Cache the function.
299 CacheFunction(isolate, serial_number, function); 374 CacheTemplateInstantiation(isolate, serial_number, function);
300 } 375 }
301 auto result = ConfigureInstance(isolate, function, data); 376 auto result = ConfigureInstance(isolate, function, data);
302 if (result.is_null()) { 377 if (result.is_null()) {
303 // Uncache on error. 378 // Uncache on error.
304 if (!data->do_not_cache()) { 379 if (serial_number->value()) {
305 UncacheFunction(isolate, serial_number); 380 UncacheTemplateInstantiation(isolate, serial_number);
306 } 381 }
307 return MaybeHandle<JSFunction>(); 382 return MaybeHandle<JSFunction>();
308 } 383 }
309 return scope.CloseAndEscape(function); 384 return scope.CloseAndEscape(function);
310 } 385 }
311 386
312 387
313 class InvokeScope { 388 class InvokeScope {
314 public: 389 public:
315 explicit InvokeScope(Isolate* isolate) 390 explicit InvokeScope(Isolate* isolate)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 434
360 435
361 MaybeHandle<JSObject> ApiNatives::InstantiateObject( 436 MaybeHandle<JSObject> ApiNatives::InstantiateObject(
362 Handle<ObjectTemplateInfo> data) { 437 Handle<ObjectTemplateInfo> data) {
363 Isolate* isolate = data->GetIsolate(); 438 Isolate* isolate = data->GetIsolate();
364 InvokeScope invoke_scope(isolate); 439 InvokeScope invoke_scope(isolate);
365 return ::v8::internal::InstantiateObject(isolate, data); 440 return ::v8::internal::InstantiateObject(isolate, data);
366 } 441 }
367 442
368 443
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, 444 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
386 Handle<Name> name, Handle<Object> value, 445 Handle<Name> name, Handle<Object> value,
387 PropertyAttributes attributes) { 446 PropertyAttributes attributes) {
388 const int kSize = 3; 447 const int kSize = 3;
389 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); 448 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
390 auto details_handle = handle(details.AsSmi(), isolate); 449 auto details_handle = handle(details.AsSmi(), isolate);
391 Handle<Object> data[kSize] = {name, details_handle, value}; 450 Handle<Object> data[kSize] = {name, details_handle, value};
392 AddPropertyToPropertyList(isolate, info, kSize, data); 451 AddPropertyToPropertyList(isolate, info, kSize, data);
393 } 452 }
394 453
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 if (!obj->indexed_property_handler()->IsUndefined()) { 601 if (!obj->indexed_property_handler()->IsUndefined()) {
543 map->set_has_indexed_interceptor(); 602 map->set_has_indexed_interceptor();
544 } 603 }
545 604
546 // Mark instance as callable in the map. 605 // Mark instance as callable in the map.
547 if (!obj->instance_call_handler()->IsUndefined()) { 606 if (!obj->instance_call_handler()->IsUndefined()) {
548 map->set_is_callable(); 607 map->set_is_callable();
549 map->set_is_constructor(true); 608 map->set_is_constructor(true);
550 } 609 }
551 610
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()); 611 DCHECK(result->shared()->IsApiFunction());
619 return result; 612 return result;
620 } 613 }
621 614
622 } // namespace internal 615 } // namespace internal
623 } // namespace v8 616 } // 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