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

Side by Side Diff: src/api-natives.cc

Issue 1660263003: Revert of [api] Make ObjectTemplate::SetNativeDataProperty() work even if the ObjectTemplate ... (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
« 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 }
156 151
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>
172 MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj, 152 MaybeHandle<JSObject> ConfigureInstance(Isolate* isolate, Handle<JSObject> obj,
173 Handle<TemplateInfoT> data) { 153 Handle<TemplateInfo> 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
217 auto property_list = handle(data->property_list(), isolate); 154 auto property_list = handle(data->property_list(), isolate);
218 if (property_list->IsUndefined()) return obj; 155 if (property_list->IsUndefined()) return obj;
219 // TODO(dcarney): just use a FixedArray here. 156 // TODO(dcarney): just use a FixedArray here.
220 NeanderArray properties(property_list); 157 NeanderArray properties(property_list);
221 if (properties.length() == 0) return obj; 158 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);
222 162
223 int i = 0; 163 int i = 0;
224 for (int c = 0; c < data->number_of_properties(); c++) { 164 for (int c = 0; c < data->number_of_properties(); c++) {
225 auto name = handle(Name::cast(properties.get(i++)), isolate); 165 auto name = handle(Name::cast(properties.get(i++)), isolate);
226 auto bit = handle(properties.get(i++), isolate); 166 auto bit = handle(properties.get(i++), isolate);
227 if (bit->IsSmi()) { 167 if (bit->IsSmi()) {
228 PropertyDetails details(Smi::cast(*bit)); 168 PropertyDetails details(Smi::cast(*bit));
229 PropertyAttributes attributes = details.attributes(); 169 PropertyAttributes attributes = details.attributes();
230 PropertyKind kind = details.kind(); 170 PropertyKind kind = details.kind();
231 171
232 if (kind == kData) { 172 if (kind == kData) {
233 auto prop_data = handle(properties.get(i++), isolate); 173 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());
237 174
238 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 175 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
239 prop_data, attributes), 176 prop_data, attributes),
240 JSObject); 177 JSObject);
241 } else { 178 } else {
242 auto getter = handle(properties.get(i++), isolate); 179 auto getter = handle(properties.get(i++), isolate);
243 auto setter = handle(properties.get(i++), isolate); 180 auto setter = handle(properties.get(i++), isolate);
244 RETURN_ON_EXCEPTION(isolate, 181 RETURN_ON_EXCEPTION(isolate,
245 DefineAccessorProperty(isolate, obj, name, getter, 182 DefineAccessorProperty(isolate, obj, name, getter,
246 setter, attributes), 183 setter, attributes),
(...skipping 11 matching lines...) Expand all
258 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate); 195 auto prop_data = handle(GetIntrinsic(isolate, intrinsic), isolate);
259 196
260 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name, 197 RETURN_ON_EXCEPTION(isolate, DefineDataProperty(isolate, obj, name,
261 prop_data, attributes), 198 prop_data, attributes),
262 JSObject); 199 JSObject);
263 } 200 }
264 } 201 }
265 return obj; 202 return obj;
266 } 203 }
267 204
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
283 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate, 205 MaybeHandle<JSObject> InstantiateObject(Isolate* isolate,
284 Handle<ObjectTemplateInfo> info) { 206 Handle<ObjectTemplateInfo> info) {
285 // Enter a new scope. Recursion could otherwise create a lot of handles. 207 // Enter a new scope. Recursion could otherwise create a lot of handles.
286 HandleScope scope(isolate); 208 HandleScope scope(isolate);
287 // Fast path. 209 // Fast path.
288 Handle<JSObject> result; 210 Handle<JSObject> result;
289 auto constructor = handle(info->constructor(), isolate); 211 auto constructor = handle(info->constructor(), isolate);
290 Handle<JSFunction> cons; 212 Handle<JSFunction> cons;
291 if (constructor->IsUndefined()) { 213 if (constructor->IsUndefined()) {
292 cons = isolate->object_function(); 214 cons = isolate->object_function();
293 } else { 215 } else {
294 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor); 216 auto cons_templ = Handle<FunctionTemplateInfo>::cast(constructor);
295 ASSIGN_RETURN_ON_EXCEPTION( 217 ASSIGN_RETURN_ON_EXCEPTION(
296 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction); 218 isolate, cons, InstantiateFunction(isolate, cons_templ), JSFunction);
297 } 219 }
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 }
310 auto object = isolate->factory()->NewJSObject(cons); 220 auto object = isolate->factory()->NewJSObject(cons);
311 ASSIGN_RETURN_ON_EXCEPTION( 221 ASSIGN_RETURN_ON_EXCEPTION(
312 isolate, result, ConfigureInstance(isolate, object, info), JSFunction); 222 isolate, result, ConfigureInstance(isolate, object, info), JSFunction);
313 // TODO(dcarney): is this necessary? 223 // TODO(dcarney): is this necessary?
314 JSObject::MigrateSlowToFast(result, 0, "ApiNatives::InstantiateObject"); 224 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 }
321 return scope.CloseAndEscape(result); 225 return scope.CloseAndEscape(result);
322 } 226 }
323 227
324 228
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
325 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate, 246 MaybeHandle<JSFunction> InstantiateFunction(Isolate* isolate,
326 Handle<FunctionTemplateInfo> data, 247 Handle<FunctionTemplateInfo> data,
327 Handle<Name> name) { 248 Handle<Name> name) {
328 auto serial_number = handle(Smi::cast(data->serial_number()), isolate); 249 auto serial_number = handle(Smi::cast(data->serial_number()), isolate);
329 if (serial_number->value()) { 250 // Probe cache.
330 // Probe cache. 251 if (!data->do_not_cache()) {
331 auto cache = isolate->template_instantiations_cache(); 252 auto cache = isolate->function_cache();
332 Object* element = cache->Lookup(serial_number); 253 Object* element = cache->Lookup(serial_number);
333 if (element->IsJSFunction()) { 254 if (element->IsJSFunction()) {
334 return handle(JSFunction::cast(element), isolate); 255 return handle(JSFunction::cast(element), isolate);
335 } 256 }
336 } 257 }
337 // Enter a new scope. Recursion could otherwise create a lot of handles. 258 // Enter a new scope. Recursion could otherwise create a lot of handles.
338 HandleScope scope(isolate); 259 HandleScope scope(isolate);
339 Handle<JSObject> prototype; 260 Handle<JSObject> prototype;
340 if (!data->remove_prototype()) { 261 if (!data->remove_prototype()) {
341 auto prototype_templ = handle(data->prototype_template(), isolate); 262 auto prototype_templ = handle(data->prototype_template(), isolate);
(...skipping 24 matching lines...) Expand all
366 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false, 287 MAYBE_RETURN(JSObject::SetPrototype(prototype, parent_prototype, false,
367 Object::THROW_ON_ERROR), 288 Object::THROW_ON_ERROR),
368 MaybeHandle<JSFunction>()); 289 MaybeHandle<JSFunction>());
369 } 290 }
370 } 291 }
371 auto function = ApiNatives::CreateApiFunction( 292 auto function = ApiNatives::CreateApiFunction(
372 isolate, data, prototype, ApiNatives::JavaScriptObjectType); 293 isolate, data, prototype, ApiNatives::JavaScriptObjectType);
373 if (!name.is_null() && name->IsString()) { 294 if (!name.is_null() && name->IsString()) {
374 function->shared()->set_name(*name); 295 function->shared()->set_name(*name);
375 } 296 }
376 if (serial_number->value()) { 297 if (!data->do_not_cache()) {
377 // Cache the function. 298 // Cache the function.
378 CacheTemplateInstantiation(isolate, serial_number, function); 299 CacheFunction(isolate, serial_number, function);
379 } 300 }
380 auto result = ConfigureInstance(isolate, function, data); 301 auto result = ConfigureInstance(isolate, function, data);
381 if (result.is_null()) { 302 if (result.is_null()) {
382 // Uncache on error. 303 // Uncache on error.
383 if (serial_number->value()) { 304 if (!data->do_not_cache()) {
384 UncacheTemplateInstantiation(isolate, serial_number); 305 UncacheFunction(isolate, serial_number);
385 } 306 }
386 return MaybeHandle<JSFunction>(); 307 return MaybeHandle<JSFunction>();
387 } 308 }
388 return scope.CloseAndEscape(function); 309 return scope.CloseAndEscape(function);
389 } 310 }
390 311
391 312
392 class InvokeScope { 313 class InvokeScope {
393 public: 314 public:
394 explicit InvokeScope(Isolate* isolate) 315 explicit InvokeScope(Isolate* isolate)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 359
439 360
440 MaybeHandle<JSObject> ApiNatives::InstantiateObject( 361 MaybeHandle<JSObject> ApiNatives::InstantiateObject(
441 Handle<ObjectTemplateInfo> data) { 362 Handle<ObjectTemplateInfo> data) {
442 Isolate* isolate = data->GetIsolate(); 363 Isolate* isolate = data->GetIsolate();
443 InvokeScope invoke_scope(isolate); 364 InvokeScope invoke_scope(isolate);
444 return ::v8::internal::InstantiateObject(isolate, data); 365 return ::v8::internal::InstantiateObject(isolate, data);
445 } 366 }
446 367
447 368
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
448 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info, 385 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
449 Handle<Name> name, Handle<Object> value, 386 Handle<Name> name, Handle<Object> value,
450 PropertyAttributes attributes) { 387 PropertyAttributes attributes) {
451 // JSReceivers could cause cross-context leaks therefore they must
452 // never appear as data properties.
453 CHECK(!value->IsJSReceiver());
454 const int kSize = 3; 388 const int kSize = 3;
455 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell); 389 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
456 auto details_handle = handle(details.AsSmi(), isolate); 390 auto details_handle = handle(details.AsSmi(), isolate);
457 Handle<Object> data[kSize] = {name, details_handle, value}; 391 Handle<Object> data[kSize] = {name, details_handle, value};
458 AddPropertyToPropertyList(isolate, info, kSize, data); 392 AddPropertyToPropertyList(isolate, info, kSize, data);
459 } 393 }
460 394
461 395
462 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info, 396 void ApiNatives::AddDataProperty(Isolate* isolate, Handle<TemplateInfo> info,
463 Handle<Name> name, v8::Intrinsic intrinsic, 397 Handle<Name> name, v8::Intrinsic intrinsic,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 if (!obj->indexed_property_handler()->IsUndefined()) { 542 if (!obj->indexed_property_handler()->IsUndefined()) {
609 map->set_has_indexed_interceptor(); 543 map->set_has_indexed_interceptor();
610 } 544 }
611 545
612 // Mark instance as callable in the map. 546 // Mark instance as callable in the map.
613 if (!obj->instance_call_handler()->IsUndefined()) { 547 if (!obj->instance_call_handler()->IsUndefined()) {
614 map->set_is_callable(); 548 map->set_is_callable();
615 map->set_is_constructor(true); 549 map->set_is_constructor(true);
616 } 550 }
617 551
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