1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/objects.h"
10 #include "src/accessors.h"
11 #include "src/allocation-site-scopes.h"
13 #include "src/arguments.h"
14 #include "src/base/bits.h"
15 #include "src/base/utils/random-number-generator.h"
16 #include "src/bootstrapper.h"
17 #include "src/code-stubs.h"
18 #include "src/codegen.h"
19 #include "src/compilation-dependencies.h"
20 #include "src/compiler.h"
21 #include "src/cpu-profiler.h"
23 #include "src/debug/debug.h"
24 #include "src/deoptimizer.h"
25 #include "src/elements.h"
26 #include "src/execution.h"
27 #include "src/field-index-inl.h"
28 #include "src/field-index.h"
29 #include "src/full-codegen/full-codegen.h"
30 #include "src/hydrogen.h"
31 #include "src/ic/ic.h"
32 #include "src/interpreter/bytecodes.h"
34 #include "src/lookup.h"
35 #include "src/macro-assembler.h"
36 #include "src/messages.h"
37 #include "src/objects-inl.h"
38 #include "src/prototype.h"
39 #include "src/safepoint-table.h"
40 #include "src/string-search.h"
41 #include "src/string-stream.h"
42 #include "src/utils.h"
44 #ifdef ENABLE_DISASSEMBLER
45 #include "src/disasm.h"
46 #include "src/disassembler.h"
52 Handle<HeapType> Object::OptimalType(Isolate* isolate,
53 Representation representation) {
54 if (representation.IsNone()) return HeapType::None(isolate);
55 if (FLAG_track_field_types) {
56 if (representation.IsHeapObject() && IsHeapObject()) {
57 // We can track only JavaScript objects with stable maps.
58 Handle<Map> map(HeapObject::cast(this)->map(), isolate);
59 if (map->is_stable() &&
60 map->instance_type() >= FIRST_NONCALLABLE_SPEC_OBJECT_TYPE &&
61 map->instance_type() <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE) {
62 return HeapType::Class(map, isolate);
66 return HeapType::Any(isolate);
70 MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate,
71 Handle<Object> object,
72 Handle<Context> native_context) {
73 if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
74 Handle<JSFunction> constructor;
75 if (object->IsSmi()) {
76 constructor = handle(native_context->number_function(), isolate);
78 int constructor_function_index =
79 Handle<HeapObject>::cast(object)->map()->GetConstructorFunctionIndex();
80 if (constructor_function_index == Map::kNoConstructorFunctionIndex) {
81 return MaybeHandle<JSReceiver>();
84 JSFunction::cast(native_context->get(constructor_function_index)),
87 Handle<JSObject> result = isolate->factory()->NewJSObject(constructor);
88 Handle<JSValue>::cast(result)->set_value(*object);
94 MaybeHandle<Object> Object::ToNumber(Isolate* isolate, Handle<Object> input) {
96 if (input->IsNumber()) {
99 if (input->IsOddball()) {
100 return handle(Handle<Oddball>::cast(input)->to_number(), isolate);
102 if (input->IsString()) {
103 return String::ToNumber(Handle<String>::cast(input));
105 if (input->IsSymbol()) {
106 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToNumber),
109 if (input->IsSimd128Value()) {
110 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSimdToNumber),
113 ASSIGN_RETURN_ON_EXCEPTION(
114 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
115 ToPrimitiveHint::kNumber),
122 MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) {
124 if (input->IsString()) {
125 return Handle<String>::cast(input);
127 if (input->IsOddball()) {
128 return handle(Handle<Oddball>::cast(input)->to_string(), isolate);
130 if (input->IsNumber()) {
131 return isolate->factory()->NumberToString(input);
133 if (input->IsSymbol()) {
134 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kSymbolToString),
137 if (input->IsSimd128Value()) {
138 return Simd128Value::ToString(Handle<Simd128Value>::cast(input));
140 ASSIGN_RETURN_ON_EXCEPTION(
141 isolate, input, JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input),
142 ToPrimitiveHint::kString),
148 bool Object::BooleanValue() {
149 if (IsBoolean()) return IsTrue();
150 if (IsSmi()) return Smi::cast(this)->value() != 0;
151 if (IsUndefined() || IsNull()) return false;
152 if (IsUndetectableObject()) return false; // Undetectable object is false.
153 if (IsString()) return String::cast(this)->length() != 0;
154 if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue();
159 bool Object::StrictEquals(Object* that) {
160 if (this->IsNumber()) {
161 if (!that->IsNumber()) return false;
162 double const x = this->Number();
163 double const y = that->Number();
164 // Must check explicitly for NaN:s on Windows, but -0 works fine.
165 return x == y && !std::isnan(x) && !std::isnan(y);
166 } else if (this->IsString()) {
167 if (!that->IsString()) return false;
168 return String::cast(this)->Equals(String::cast(that));
169 } else if (this->IsSimd128Value()) {
170 if (!that->IsSimd128Value()) return false;
171 return Simd128Value::cast(this)->Equals(Simd128Value::cast(that));
177 bool Object::IsCallable() const {
178 const Object* fun = this;
179 while (fun->IsJSFunctionProxy()) {
180 fun = JSFunctionProxy::cast(fun)->call_trap();
182 return fun->IsJSFunction() ||
183 (fun->IsHeapObject() &&
184 HeapObject::cast(fun)->map()->has_instance_call_handler());
188 bool Object::IsPromise(Handle<Object> object) {
189 if (!object->IsJSObject()) return false;
190 auto js_object = Handle<JSObject>::cast(object);
191 // Promises can't have access checks.
192 if (js_object->map()->is_access_check_needed()) return false;
193 auto isolate = js_object->GetIsolate();
194 // TODO(dcarney): this should just be read from the symbol registry so as not
195 // to be context dependent.
196 auto key = isolate->factory()->promise_status_symbol();
197 // Shouldn't be possible to throw here.
198 return JSObject::HasRealNamedProperty(js_object, key).FromJust();
203 MaybeHandle<Object> Object::GetMethod(Handle<JSReceiver> receiver,
206 Isolate* isolate = receiver->GetIsolate();
207 ASSIGN_RETURN_ON_EXCEPTION(isolate, func,
208 JSReceiver::GetProperty(receiver, name), Object);
209 if (func->IsNull() || func->IsUndefined()) {
210 return isolate->factory()->undefined_value();
212 if (!func->IsCallable()) {
213 // TODO(bmeurer): Better error message here?
214 THROW_NEW_ERROR(isolate,
215 NewTypeError(MessageTemplate::kCalledNonCallable, func),
222 MaybeHandle<Object> Object::GetProperty(LookupIterator* it,
223 LanguageMode language_mode) {
224 for (; it->IsFound(); it->Next()) {
225 switch (it->state()) {
226 case LookupIterator::NOT_FOUND:
227 case LookupIterator::TRANSITION:
229 case LookupIterator::JSPROXY:
230 return JSProxy::GetPropertyWithHandler(
231 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
232 case LookupIterator::INTERCEPTOR: {
234 Handle<Object> result;
235 ASSIGN_RETURN_ON_EXCEPTION(
236 it->isolate(), result,
237 JSObject::GetPropertyWithInterceptor(it, &done), Object);
238 if (done) return result;
241 case LookupIterator::ACCESS_CHECK:
242 if (it->HasAccess()) break;
243 return JSObject::GetPropertyWithFailedAccessCheck(it);
244 case LookupIterator::ACCESSOR:
245 return GetPropertyWithAccessor(it, language_mode);
246 case LookupIterator::INTEGER_INDEXED_EXOTIC:
247 return ReadAbsentProperty(it, language_mode);
248 case LookupIterator::DATA:
249 return it->GetDataValue();
252 return ReadAbsentProperty(it, language_mode);
256 Handle<Object> JSReceiver::GetDataProperty(Handle<JSReceiver> object,
258 LookupIterator it(object, name,
259 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
260 return GetDataProperty(&it);
264 Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) {
265 for (; it->IsFound(); it->Next()) {
266 switch (it->state()) {
267 case LookupIterator::INTERCEPTOR:
268 case LookupIterator::NOT_FOUND:
269 case LookupIterator::TRANSITION:
271 case LookupIterator::ACCESS_CHECK:
272 if (it->HasAccess()) continue;
274 case LookupIterator::JSPROXY:
276 return it->isolate()->factory()->undefined_value();
277 case LookupIterator::ACCESSOR:
278 // TODO(verwaest): For now this doesn't call into
279 // ExecutableAccessorInfo, since clients don't need it. Update once
282 return it->isolate()->factory()->undefined_value();
283 case LookupIterator::INTEGER_INDEXED_EXOTIC:
284 return it->isolate()->factory()->undefined_value();
285 case LookupIterator::DATA:
286 return it->GetDataValue();
289 return it->isolate()->factory()->undefined_value();
293 bool Object::ToInt32(int32_t* value) {
295 *value = Smi::cast(this)->value();
298 if (IsHeapNumber()) {
299 double num = HeapNumber::cast(this)->value();
300 if (FastI2D(FastD2I(num)) == num) {
301 *value = FastD2I(num);
309 bool Object::ToUint32(uint32_t* value) {
311 int num = Smi::cast(this)->value();
313 *value = static_cast<uint32_t>(num);
317 if (IsHeapNumber()) {
318 double num = HeapNumber::cast(this)->value();
319 if (num >= 0 && FastUI2D(FastD2UI(num)) == num) {
320 *value = FastD2UI(num);
328 bool FunctionTemplateInfo::IsTemplateFor(Object* object) {
329 if (!object->IsHeapObject()) return false;
330 return IsTemplateFor(HeapObject::cast(object)->map());
334 bool FunctionTemplateInfo::IsTemplateFor(Map* map) {
335 // There is a constraint on the object; check.
336 if (!map->IsJSObjectMap()) return false;
337 // Fetch the constructor function of the object.
338 Object* cons_obj = map->GetConstructor();
339 if (!cons_obj->IsJSFunction()) return false;
340 JSFunction* fun = JSFunction::cast(cons_obj);
341 // Iterate through the chain of inheriting function templates to
342 // see if the required one occurs.
343 for (Object* type = fun->shared()->function_data();
344 type->IsFunctionTemplateInfo();
345 type = FunctionTemplateInfo::cast(type)->parent_template()) {
346 if (type == this) return true;
348 // Didn't find the required type in the inheritance chain.
353 // TODO(dcarney): CallOptimization duplicates this logic, merge.
354 Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate,
356 // API calls are only supported with JSObject receivers.
357 if (!receiver->IsJSObject()) return isolate->heap()->null_value();
358 Object* recv_type = this->signature();
359 // No signature, return holder.
360 if (recv_type->IsUndefined()) return receiver;
361 FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type);
362 // Check the receiver.
363 for (PrototypeIterator iter(isolate, receiver,
364 PrototypeIterator::START_AT_RECEIVER);
365 !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) {
366 if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent();
368 return isolate->heap()->null_value();
372 Handle<FixedArray> JSObject::EnsureWritableFastElements(
373 Handle<JSObject> object) {
374 DCHECK(object->HasFastSmiOrObjectElements());
375 Isolate* isolate = object->GetIsolate();
376 Handle<FixedArray> elems(FixedArray::cast(object->elements()), isolate);
377 if (elems->map() != isolate->heap()->fixed_cow_array_map()) return elems;
378 Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap(
379 elems, isolate->factory()->fixed_array_map());
380 object->set_elements(*writable_elems);
381 isolate->counters()->cow_arrays_converted()->Increment();
382 return writable_elems;
386 MaybeHandle<Object> JSProxy::GetPropertyWithHandler(Handle<JSProxy> proxy,
387 Handle<Object> receiver,
389 Isolate* isolate = proxy->GetIsolate();
391 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
392 if (name->IsSymbol()) return isolate->factory()->undefined_value();
394 Handle<Object> args[] = { receiver, name };
396 proxy, "get", isolate->derived_get_trap(), arraysize(args), args);
400 MaybeHandle<Object> Object::GetPropertyWithAccessor(
401 LookupIterator* it, LanguageMode language_mode) {
402 Isolate* isolate = it->isolate();
403 Handle<Object> structure = it->GetAccessors();
404 Handle<Object> receiver = it->GetReceiver();
406 // We should never get here to initialize a const with the hole value since a
407 // const declaration would conflict with the getter.
408 DCHECK(!structure->IsForeign());
410 // API style callbacks.
411 if (structure->IsAccessorInfo()) {
412 Handle<JSObject> holder = it->GetHolder<JSObject>();
413 Handle<Name> name = it->GetName();
414 Handle<ExecutableAccessorInfo> info =
415 Handle<ExecutableAccessorInfo>::cast(structure);
416 if (!info->IsCompatibleReceiver(*receiver)) {
417 THROW_NEW_ERROR(isolate,
418 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
423 v8::AccessorNameGetterCallback call_fun =
424 v8::ToCData<v8::AccessorNameGetterCallback>(info->getter());
425 if (call_fun == nullptr) return isolate->factory()->undefined_value();
427 LOG(isolate, ApiNamedPropertyAccess("load", *holder, *name));
428 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
429 v8::Local<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(name));
430 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
431 if (result.IsEmpty()) {
432 return ReadAbsentProperty(isolate, receiver, name, language_mode);
434 Handle<Object> return_value = v8::Utils::OpenHandle(*result);
435 return_value->VerifyApiCallResultType();
436 // Rebox handle before return.
437 return handle(*return_value, isolate);
441 Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate);
442 if (getter->IsSpecFunction()) {
443 // TODO(rossberg): nicer would be to cast to some JSCallable here...
444 return Object::GetPropertyWithDefinedGetter(
445 receiver, Handle<JSReceiver>::cast(getter));
447 // Getter is not a function.
448 return ReadAbsentProperty(isolate, receiver, it->GetName(), language_mode);
452 bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate,
453 Handle<AccessorInfo> info,
455 if (!info->HasExpectedReceiverType()) return true;
456 if (!map->IsJSObjectMap()) return false;
457 return FunctionTemplateInfo::cast(info->expected_receiver_type())
458 ->IsTemplateFor(*map);
462 MaybeHandle<Object> Object::SetPropertyWithAccessor(
463 LookupIterator* it, Handle<Object> value, LanguageMode language_mode) {
464 Isolate* isolate = it->isolate();
465 Handle<Object> structure = it->GetAccessors();
466 Handle<Object> receiver = it->GetReceiver();
468 // We should never get here to initialize a const with the hole value since a
469 // const declaration would conflict with the setter.
470 DCHECK(!structure->IsForeign());
472 // API style callbacks.
473 if (structure->IsExecutableAccessorInfo()) {
474 Handle<JSObject> holder = it->GetHolder<JSObject>();
475 Handle<Name> name = it->GetName();
476 Handle<ExecutableAccessorInfo> info =
477 Handle<ExecutableAccessorInfo>::cast(structure);
478 if (!info->IsCompatibleReceiver(*receiver)) {
479 THROW_NEW_ERROR(isolate,
480 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
485 v8::AccessorNameSetterCallback call_fun =
486 v8::ToCData<v8::AccessorNameSetterCallback>(info->setter());
487 if (call_fun == nullptr) return value;
489 LOG(isolate, ApiNamedPropertyAccess("store", *holder, *name));
490 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
491 args.Call(call_fun, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
492 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
497 Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate);
498 if (setter->IsSpecFunction()) {
499 // TODO(rossberg): nicer would be to cast to some JSCallable here...
500 return SetPropertyWithDefinedSetter(
501 receiver, Handle<JSReceiver>::cast(setter), value);
504 if (is_sloppy(language_mode)) return value;
506 THROW_NEW_ERROR(isolate,
507 NewTypeError(MessageTemplate::kNoSetterInCallback,
508 it->GetName(), it->GetHolder<JSObject>()),
513 MaybeHandle<Object> Object::GetPropertyWithDefinedGetter(
514 Handle<Object> receiver,
515 Handle<JSReceiver> getter) {
516 Isolate* isolate = getter->GetIsolate();
518 // Platforms with simulators like arm/arm64 expose a funny issue. If the
519 // simulator has a separate JS stack pointer from the C++ stack pointer, it
520 // can miss C++ stack overflows in the stack guard at the start of JavaScript
521 // functions. It would be very expensive to check the C++ stack pointer at
522 // that location. The best solution seems to be to break the impasse by
523 // adding checks at possible recursion points. What's more, we don't put
524 // this stack check behind the USE_SIMULATOR define in order to keep
525 // behavior the same between hardware and simulators.
526 StackLimitCheck check(isolate);
527 if (check.JsHasOverflowed()) {
528 isolate->StackOverflow();
529 return MaybeHandle<Object>();
532 Debug* debug = isolate->debug();
533 // Handle stepping into a getter if step into is active.
534 // TODO(rossberg): should this apply to getters that are function proxies?
535 if (debug->is_active()) debug->HandleStepIn(getter, false);
537 return Execution::Call(isolate, getter, receiver, 0, NULL, true);
541 MaybeHandle<Object> Object::SetPropertyWithDefinedSetter(
542 Handle<Object> receiver,
543 Handle<JSReceiver> setter,
544 Handle<Object> value) {
545 Isolate* isolate = setter->GetIsolate();
547 Debug* debug = isolate->debug();
548 // Handle stepping into a setter if step into is active.
549 // TODO(rossberg): should this apply to getters that are function proxies?
550 if (debug->is_active()) debug->HandleStepIn(setter, false);
552 Handle<Object> argv[] = { value };
553 RETURN_ON_EXCEPTION(isolate, Execution::Call(isolate, setter, receiver,
554 arraysize(argv), argv, true),
561 bool JSObject::AllCanRead(LookupIterator* it) {
562 // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of
563 // which have already been checked.
564 DCHECK(it->state() == LookupIterator::ACCESS_CHECK ||
565 it->state() == LookupIterator::INTERCEPTOR);
566 for (it->Next(); it->IsFound(); it->Next()) {
567 if (it->state() == LookupIterator::ACCESSOR) {
568 auto accessors = it->GetAccessors();
569 if (accessors->IsAccessorInfo()) {
570 if (AccessorInfo::cast(*accessors)->all_can_read()) return true;
572 } else if (it->state() == LookupIterator::INTERCEPTOR) {
573 if (it->GetInterceptor()->all_can_read()) return true;
580 MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck(
581 LookupIterator* it) {
582 Handle<JSObject> checked = it->GetHolder<JSObject>();
583 while (AllCanRead(it)) {
584 if (it->state() == LookupIterator::ACCESSOR) {
585 return GetPropertyWithAccessor(it, SLOPPY);
587 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
589 Handle<Object> result;
590 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result,
591 GetPropertyWithInterceptor(it, &done), Object);
592 if (done) return result;
594 it->isolate()->ReportFailedAccessCheck(checked);
595 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
596 return it->factory()->undefined_value();
600 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck(
601 LookupIterator* it) {
602 Handle<JSObject> checked = it->GetHolder<JSObject>();
603 while (AllCanRead(it)) {
604 if (it->state() == LookupIterator::ACCESSOR) {
605 return Just(it->property_details().attributes());
607 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
608 auto result = GetPropertyAttributesWithInterceptor(it);
609 if (it->isolate()->has_scheduled_exception()) break;
610 if (result.IsJust() && result.FromJust() != ABSENT) return result;
612 it->isolate()->ReportFailedAccessCheck(checked);
613 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(),
614 Nothing<PropertyAttributes>());
620 bool JSObject::AllCanWrite(LookupIterator* it) {
621 for (; it->IsFound(); it->Next()) {
622 if (it->state() == LookupIterator::ACCESSOR) {
623 Handle<Object> accessors = it->GetAccessors();
624 if (accessors->IsAccessorInfo()) {
625 if (AccessorInfo::cast(*accessors)->all_can_write()) return true;
633 MaybeHandle<Object> JSObject::SetPropertyWithFailedAccessCheck(
634 LookupIterator* it, Handle<Object> value) {
635 Handle<JSObject> checked = it->GetHolder<JSObject>();
636 if (AllCanWrite(it)) {
637 // The supplied language-mode is ignored by SetPropertyWithAccessor.
638 return SetPropertyWithAccessor(it, value, SLOPPY);
641 it->isolate()->ReportFailedAccessCheck(checked);
642 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
647 void JSObject::SetNormalizedProperty(Handle<JSObject> object,
649 Handle<Object> value,
650 PropertyDetails details) {
651 DCHECK(!object->HasFastProperties());
652 if (!name->IsUniqueName()) {
653 name = object->GetIsolate()->factory()->InternalizeString(
654 Handle<String>::cast(name));
657 if (object->IsGlobalObject()) {
658 Handle<GlobalDictionary> property_dictionary(object->global_dictionary());
660 int entry = property_dictionary->FindEntry(name);
661 if (entry == GlobalDictionary::kNotFound) {
662 auto cell = object->GetIsolate()->factory()->NewPropertyCell();
663 cell->set_value(*value);
664 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
665 : PropertyCellType::kConstant;
666 details = details.set_cell_type(cell_type);
668 property_dictionary =
669 GlobalDictionary::Add(property_dictionary, name, value, details);
670 object->set_properties(*property_dictionary);
672 PropertyCell::UpdateCell(property_dictionary, entry, value, details);
675 Handle<NameDictionary> property_dictionary(object->property_dictionary());
677 int entry = property_dictionary->FindEntry(name);
678 if (entry == NameDictionary::kNotFound) {
679 property_dictionary =
680 NameDictionary::Add(property_dictionary, name, value, details);
681 object->set_properties(*property_dictionary);
683 PropertyDetails original_details = property_dictionary->DetailsAt(entry);
684 int enumeration_index = original_details.dictionary_index();
685 DCHECK(enumeration_index > 0);
686 details = details.set_index(enumeration_index);
687 property_dictionary->SetEntry(entry, name, value, details);
693 bool Object::HasInPrototypeChain(Isolate* isolate, Object* target) {
694 PrototypeIterator iter(isolate, this, PrototypeIterator::START_AT_RECEIVER);
696 iter.AdvanceIgnoringProxies();
697 if (iter.IsAtEnd()) return false;
698 if (iter.IsAtEnd(target)) return true;
703 Map* Object::GetRootMap(Isolate* isolate) {
704 DisallowHeapAllocation no_alloc;
706 Context* native_context = isolate->context()->native_context();
707 return native_context->number_function()->initial_map();
710 // The object is either a number, a string, a symbol, a boolean, a SIMD value,
711 // a real JS object, or a Harmony proxy.
712 HeapObject* heap_object = HeapObject::cast(this);
713 if (heap_object->IsJSReceiver()) {
714 return heap_object->map();
716 int constructor_function_index =
717 heap_object->map()->GetConstructorFunctionIndex();
718 if (constructor_function_index != Map::kNoConstructorFunctionIndex) {
719 Context* native_context = isolate->context()->native_context();
720 JSFunction* constructor_function =
721 JSFunction::cast(native_context->get(constructor_function_index));
722 return constructor_function->initial_map();
724 return isolate->heap()->null_value()->map();
728 Object* Object::GetHash() {
729 Object* hash = GetSimpleHash();
730 if (hash->IsSmi()) return hash;
732 DCHECK(IsJSReceiver());
733 return JSReceiver::cast(this)->GetIdentityHash();
737 Object* Object::GetSimpleHash() {
738 // The object is either a Smi, a HeapNumber, a name, an odd-ball,
739 // a SIMD value type, a real JS object, or a Harmony proxy.
741 uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed);
742 return Smi::FromInt(hash & Smi::kMaxValue);
744 if (IsHeapNumber()) {
745 double num = HeapNumber::cast(this)->value();
746 if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue);
747 if (i::IsMinusZero(num)) num = 0;
748 if (IsSmiDouble(num)) {
749 return Smi::FromInt(FastD2I(num))->GetHash();
751 uint32_t hash = ComputeLongHash(double_to_uint64(num));
752 return Smi::FromInt(hash & Smi::kMaxValue);
755 uint32_t hash = Name::cast(this)->Hash();
756 return Smi::FromInt(hash);
759 uint32_t hash = Oddball::cast(this)->to_string()->Hash();
760 return Smi::FromInt(hash);
762 if (IsSimd128Value()) {
763 uint32_t hash = Simd128Value::cast(this)->Hash();
764 return Smi::FromInt(hash & Smi::kMaxValue);
766 DCHECK(IsJSReceiver());
767 JSReceiver* receiver = JSReceiver::cast(this);
768 return receiver->GetHeap()->undefined_value();
772 Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) {
773 Handle<Object> hash(object->GetSimpleHash(), isolate);
774 if (hash->IsSmi()) return Handle<Smi>::cast(hash);
776 DCHECK(object->IsJSReceiver());
777 return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object));
781 bool Object::SameValue(Object* other) {
782 if (other == this) return true;
784 // The object is either a number, a name, an odd-ball,
785 // a real JS object, or a Harmony proxy.
786 if (IsNumber() && other->IsNumber()) {
787 double this_value = Number();
788 double other_value = other->Number();
789 // SameValue(NaN, NaN) is true.
790 if (this_value != other_value) {
791 return std::isnan(this_value) && std::isnan(other_value);
793 // SameValue(0.0, -0.0) is false.
794 return (std::signbit(this_value) == std::signbit(other_value));
796 if (IsString() && other->IsString()) {
797 return String::cast(this)->Equals(String::cast(other));
799 if (IsSimd128Value() && other->IsSimd128Value()) {
800 if (IsFloat32x4() && other->IsFloat32x4()) {
801 Float32x4* a = Float32x4::cast(this);
802 Float32x4* b = Float32x4::cast(other);
803 for (int i = 0; i < 4; i++) {
804 float x = a->get_lane(i);
805 float y = b->get_lane(i);
806 // Implements the ES5 SameValue operation for floating point types.
807 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue
808 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
809 if (std::signbit(x) != std::signbit(y)) return false;
813 Simd128Value* a = Simd128Value::cast(this);
814 Simd128Value* b = Simd128Value::cast(other);
815 return a->map()->instance_type() == b->map()->instance_type() &&
823 bool Object::SameValueZero(Object* other) {
824 if (other == this) return true;
826 // The object is either a number, a name, an odd-ball,
827 // a real JS object, or a Harmony proxy.
828 if (IsNumber() && other->IsNumber()) {
829 double this_value = Number();
830 double other_value = other->Number();
832 return this_value == other_value ||
833 (std::isnan(this_value) && std::isnan(other_value));
835 if (IsString() && other->IsString()) {
836 return String::cast(this)->Equals(String::cast(other));
838 if (IsSimd128Value() && other->IsSimd128Value()) {
839 if (IsFloat32x4() && other->IsFloat32x4()) {
840 Float32x4* a = Float32x4::cast(this);
841 Float32x4* b = Float32x4::cast(other);
842 for (int i = 0; i < 4; i++) {
843 float x = a->get_lane(i);
844 float y = b->get_lane(i);
845 // Implements the ES6 SameValueZero operation for floating point types.
846 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero
847 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
848 // SameValueZero doesn't distinguish between 0 and -0.
852 Simd128Value* a = Simd128Value::cast(this);
853 Simd128Value* b = Simd128Value::cast(other);
854 return a->map()->instance_type() == b->map()->instance_type() &&
862 void Object::ShortPrint(FILE* out) {
868 void Object::ShortPrint(StringStream* accumulator) {
869 std::ostringstream os;
871 accumulator->Add(os.str().c_str());
875 void Object::ShortPrint(std::ostream& os) { os << Brief(this); }
878 std::ostream& operator<<(std::ostream& os, const Brief& v) {
879 if (v.value->IsSmi()) {
880 Smi::cast(v.value)->SmiPrint(os);
882 // TODO(svenpanne) Const-correct HeapObjectShortPrint!
883 HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value));
884 obj->HeapObjectShortPrint(os);
890 void Smi::SmiPrint(std::ostream& os) const { // NOLINT
895 // Should a word be prefixed by 'a' or 'an' in order to read naturally in
896 // English? Returns false for non-ASCII or words that don't start with
897 // a capital letter. The a/an rule follows pronunciation in English.
898 // We don't use the BBC's overcorrect "an historic occasion" though if
899 // you speak a dialect you may well say "an 'istoric occasion".
900 static bool AnWord(String* str) {
901 if (str->length() == 0) return false; // A nothing.
902 int c0 = str->Get(0);
903 int c1 = str->length() > 1 ? str->Get(1) : 0;
906 return true; // An Umpire, but a UTF8String, a U.
908 } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') {
909 return true; // An Ape, an ABCBook.
910 } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) &&
911 (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' ||
912 c0 == 'S' || c0 == 'X')) {
913 return true; // An MP3File, an M.
919 Handle<String> String::SlowFlatten(Handle<ConsString> cons,
920 PretenureFlag pretenure) {
921 DCHECK(AllowHeapAllocation::IsAllowed());
922 DCHECK(cons->second()->length() != 0);
923 Isolate* isolate = cons->GetIsolate();
924 int length = cons->length();
925 PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure
927 Handle<SeqString> result;
928 if (cons->IsOneByteRepresentation()) {
929 Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString(
930 length, tenure).ToHandleChecked();
931 DisallowHeapAllocation no_gc;
932 WriteToFlat(*cons, flat->GetChars(), 0, length);
935 Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString(
936 length, tenure).ToHandleChecked();
937 DisallowHeapAllocation no_gc;
938 WriteToFlat(*cons, flat->GetChars(), 0, length);
941 cons->set_first(*result);
942 cons->set_second(isolate->heap()->empty_string());
943 DCHECK(result->IsFlat());
949 bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
950 // Externalizing twice leaks the external resource, so it's
951 // prohibited by the API.
952 DCHECK(!this->IsExternalString());
953 #ifdef ENABLE_SLOW_DCHECKS
954 if (FLAG_enable_slow_asserts) {
955 // Assert that the resource and the string are equivalent.
956 DCHECK(static_cast<size_t>(this->length()) == resource->length());
957 ScopedVector<uc16> smart_chars(this->length());
958 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
959 DCHECK(memcmp(smart_chars.start(),
961 resource->length() * sizeof(smart_chars[0])) == 0);
964 int size = this->Size(); // Byte size of the original string.
965 // Abort if size does not allow in-place conversion.
966 if (size < ExternalString::kShortSize) return false;
967 Heap* heap = GetHeap();
968 bool is_one_byte = this->IsOneByteRepresentation();
969 bool is_internalized = this->IsInternalizedString();
971 // Morph the string to an external string by replacing the map and
972 // reinitializing the fields. This won't work if the space the existing
973 // string occupies is too small for a regular external string.
974 // Instead, we resort to a short external string instead, omitting
975 // the field caching the address of the backing store. When we encounter
976 // short external strings in generated code, we need to bailout to runtime.
978 if (size < ExternalString::kSize) {
979 new_map = is_internalized
981 ? heap->short_external_internalized_string_with_one_byte_data_map()
982 : heap->short_external_internalized_string_map())
983 : (is_one_byte ? heap->short_external_string_with_one_byte_data_map()
984 : heap->short_external_string_map());
986 new_map = is_internalized
988 ? heap->external_internalized_string_with_one_byte_data_map()
989 : heap->external_internalized_string_map())
990 : (is_one_byte ? heap->external_string_with_one_byte_data_map()
991 : heap->external_string_map());
994 // Byte size of the external String object.
995 int new_size = this->SizeFromMap(new_map);
996 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
998 // We are storing the new map using release store after creating a filler for
999 // the left-over space to avoid races with the sweeper thread.
1000 this->synchronized_set_map(new_map);
1002 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
1003 self->set_resource(resource);
1004 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
1006 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
1011 bool String::MakeExternal(v8::String::ExternalOneByteStringResource* resource) {
1012 // Externalizing twice leaks the external resource, so it's
1013 // prohibited by the API.
1014 DCHECK(!this->IsExternalString());
1015 #ifdef ENABLE_SLOW_DCHECKS
1016 if (FLAG_enable_slow_asserts) {
1017 // Assert that the resource and the string are equivalent.
1018 DCHECK(static_cast<size_t>(this->length()) == resource->length());
1019 if (this->IsTwoByteRepresentation()) {
1020 ScopedVector<uint16_t> smart_chars(this->length());
1021 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
1022 DCHECK(String::IsOneByte(smart_chars.start(), this->length()));
1024 ScopedVector<char> smart_chars(this->length());
1025 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
1026 DCHECK(memcmp(smart_chars.start(),
1028 resource->length() * sizeof(smart_chars[0])) == 0);
1031 int size = this->Size(); // Byte size of the original string.
1032 // Abort if size does not allow in-place conversion.
1033 if (size < ExternalString::kShortSize) return false;
1034 Heap* heap = GetHeap();
1035 bool is_internalized = this->IsInternalizedString();
1037 // Morph the string to an external string by replacing the map and
1038 // reinitializing the fields. This won't work if the space the existing
1039 // string occupies is too small for a regular external string.
1040 // Instead, we resort to a short external string instead, omitting
1041 // the field caching the address of the backing store. When we encounter
1042 // short external strings in generated code, we need to bailout to runtime.
1044 if (size < ExternalString::kSize) {
1045 new_map = is_internalized
1046 ? heap->short_external_one_byte_internalized_string_map()
1047 : heap->short_external_one_byte_string_map();
1049 new_map = is_internalized
1050 ? heap->external_one_byte_internalized_string_map()
1051 : heap->external_one_byte_string_map();
1054 // Byte size of the external String object.
1055 int new_size = this->SizeFromMap(new_map);
1056 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
1058 // We are storing the new map using release store after creating a filler for
1059 // the left-over space to avoid races with the sweeper thread.
1060 this->synchronized_set_map(new_map);
1062 ExternalOneByteString* self = ExternalOneByteString::cast(this);
1063 self->set_resource(resource);
1064 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
1066 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
1071 void String::StringShortPrint(StringStream* accumulator) {
1073 if (len > kMaxShortPrintLength) {
1074 accumulator->Add("<Very long string[%u]>", len);
1078 if (!LooksValid()) {
1079 accumulator->Add("<Invalid String>");
1083 StringCharacterStream stream(this);
1085 bool truncated = false;
1086 if (len > kMaxShortPrintLength) {
1087 len = kMaxShortPrintLength;
1090 bool one_byte = true;
1091 for (int i = 0; i < len; i++) {
1092 uint16_t c = stream.GetNext();
1094 if (c < 32 || c >= 127) {
1100 accumulator->Add("<String[%u]: ", length());
1101 for (int i = 0; i < len; i++) {
1102 accumulator->Put(static_cast<char>(stream.GetNext()));
1104 accumulator->Put('>');
1106 // Backslash indicates that the string contains control
1107 // characters and that backslashes are therefore escaped.
1108 accumulator->Add("<String[%u]\\: ", length());
1109 for (int i = 0; i < len; i++) {
1110 uint16_t c = stream.GetNext();
1112 accumulator->Add("\\n");
1113 } else if (c == '\r') {
1114 accumulator->Add("\\r");
1115 } else if (c == '\\') {
1116 accumulator->Add("\\\\");
1117 } else if (c < 32 || c > 126) {
1118 accumulator->Add("\\x%02x", c);
1120 accumulator->Put(static_cast<char>(c));
1124 accumulator->Put('.');
1125 accumulator->Put('.');
1126 accumulator->Put('.');
1128 accumulator->Put('>');
1134 void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT
1135 if (end < 0) end = length();
1136 StringCharacterStream stream(this, start);
1137 for (int i = start; i < end && stream.HasMore(); i++) {
1138 os << AsUC16(stream.GetNext());
1143 void JSObject::JSObjectShortPrint(StringStream* accumulator) {
1144 switch (map()->instance_type()) {
1145 case JS_ARRAY_TYPE: {
1146 double length = JSArray::cast(this)->length()->IsUndefined()
1148 : JSArray::cast(this)->length()->Number();
1149 accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length));
1152 case JS_WEAK_MAP_TYPE: {
1153 accumulator->Add("<JS WeakMap>");
1156 case JS_WEAK_SET_TYPE: {
1157 accumulator->Add("<JS WeakSet>");
1160 case JS_REGEXP_TYPE: {
1161 accumulator->Add("<JS RegExp>");
1164 case JS_FUNCTION_TYPE: {
1165 JSFunction* function = JSFunction::cast(this);
1166 Object* fun_name = function->shared()->DebugName();
1167 bool printed = false;
1168 if (fun_name->IsString()) {
1169 String* str = String::cast(fun_name);
1170 if (str->length() > 0) {
1171 accumulator->Add("<JS Function ");
1172 accumulator->Put(str);
1177 accumulator->Add("<JS Function");
1179 accumulator->Add(" (SharedFunctionInfo %p)",
1180 reinterpret_cast<void*>(function->shared()));
1181 accumulator->Put('>');
1184 case JS_GENERATOR_OBJECT_TYPE: {
1185 accumulator->Add("<JS Generator>");
1188 case JS_MODULE_TYPE: {
1189 accumulator->Add("<JS Module>");
1192 // All other JSObjects are rather similar to each other (JSObject,
1193 // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue).
1195 Map* map_of_this = map();
1196 Heap* heap = GetHeap();
1197 Object* constructor = map_of_this->GetConstructor();
1198 bool printed = false;
1199 if (constructor->IsHeapObject() &&
1200 !heap->Contains(HeapObject::cast(constructor))) {
1201 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
1203 bool global_object = IsJSGlobalProxy();
1204 if (constructor->IsJSFunction()) {
1205 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
1206 accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!");
1208 Object* constructor_name =
1209 JSFunction::cast(constructor)->shared()->name();
1210 if (constructor_name->IsString()) {
1211 String* str = String::cast(constructor_name);
1212 if (str->length() > 0) {
1213 bool vowel = AnWord(str);
1214 accumulator->Add("<%sa%s ",
1215 global_object ? "Global Object: " : "",
1217 accumulator->Put(str);
1218 accumulator->Add(" with %smap %p",
1219 map_of_this->is_deprecated() ? "deprecated " : "",
1227 accumulator->Add("<JS %sObject", global_object ? "Global " : "");
1231 accumulator->Add(" value = ");
1232 JSValue::cast(this)->value()->ShortPrint(accumulator);
1234 accumulator->Put('>');
1241 void JSObject::PrintElementsTransition(
1242 FILE* file, Handle<JSObject> object,
1243 ElementsKind from_kind, Handle<FixedArrayBase> from_elements,
1244 ElementsKind to_kind, Handle<FixedArrayBase> to_elements) {
1245 if (from_kind != to_kind) {
1247 os << "elements transition [" << ElementsKindToString(from_kind) << " -> "
1248 << ElementsKindToString(to_kind) << "] in ";
1249 JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true);
1250 PrintF(file, " for ");
1251 object->ShortPrint(file);
1252 PrintF(file, " from ");
1253 from_elements->ShortPrint(file);
1254 PrintF(file, " to ");
1255 to_elements->ShortPrint(file);
1261 void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind,
1262 PropertyAttributes attributes) {
1264 os << "[reconfiguring ";
1265 constructor_name()->PrintOn(file);
1267 Name* name = instance_descriptors()->GetKey(modify_index);
1268 if (name->IsString()) {
1269 String::cast(name)->PrintOn(file);
1271 os << "{symbol " << static_cast<void*>(name) << "}";
1273 os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: ";
1274 os << attributes << " [";
1275 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1280 void Map::PrintGeneralization(FILE* file,
1285 bool constant_to_field,
1286 Representation old_representation,
1287 Representation new_representation,
1288 HeapType* old_field_type,
1289 HeapType* new_field_type) {
1291 os << "[generalizing ";
1292 constructor_name()->PrintOn(file);
1294 Name* name = instance_descriptors()->GetKey(modify_index);
1295 if (name->IsString()) {
1296 String::cast(name)->PrintOn(file);
1298 os << "{symbol " << static_cast<void*>(name) << "}";
1301 if (constant_to_field) {
1304 os << old_representation.Mnemonic() << "{";
1305 old_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1308 os << "->" << new_representation.Mnemonic() << "{";
1309 new_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1311 if (strlen(reason) > 0) {
1314 os << "+" << (descriptors - split) << " maps";
1317 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1322 void JSObject::PrintInstanceMigration(FILE* file,
1325 PrintF(file, "[migrating ");
1326 map()->constructor_name()->PrintOn(file);
1328 DescriptorArray* o = original_map->instance_descriptors();
1329 DescriptorArray* n = new_map->instance_descriptors();
1330 for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) {
1331 Representation o_r = o->GetDetails(i).representation();
1332 Representation n_r = n->GetDetails(i).representation();
1333 if (!o_r.Equals(n_r)) {
1334 String::cast(o->GetKey(i))->PrintOn(file);
1335 PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic());
1336 } else if (o->GetDetails(i).type() == DATA_CONSTANT &&
1337 n->GetDetails(i).type() == DATA) {
1338 Name* name = o->GetKey(i);
1339 if (name->IsString()) {
1340 String::cast(name)->PrintOn(file);
1342 PrintF(file, "{symbol %p}", static_cast<void*>(name));
1351 void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT
1352 Heap* heap = GetHeap();
1353 if (!heap->Contains(this)) {
1354 os << "!!!INVALID POINTER!!!";
1357 if (!heap->Contains(map())) {
1358 os << "!!!INVALID MAP!!!";
1365 HeapStringAllocator allocator;
1366 StringStream accumulator(&allocator);
1367 String::cast(this)->StringShortPrint(&accumulator);
1368 os << accumulator.ToCString().get();
1372 HeapStringAllocator allocator;
1373 StringStream accumulator(&allocator);
1374 JSObject::cast(this)->JSObjectShortPrint(&accumulator);
1375 os << accumulator.ToCString().get();
1378 switch (map()->instance_type()) {
1380 os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind())
1383 case FIXED_ARRAY_TYPE:
1384 os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>";
1386 case FIXED_DOUBLE_ARRAY_TYPE:
1387 os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length()
1390 case BYTE_ARRAY_TYPE:
1391 os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>";
1393 case BYTECODE_ARRAY_TYPE:
1394 os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>";
1396 case FREE_SPACE_TYPE:
1397 os << "<FreeSpace[" << FreeSpace::cast(this)->Size() << "]>";
1399 #define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \
1400 case FIXED_##TYPE##_ARRAY_TYPE: \
1401 os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \
1405 TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT)
1406 #undef TYPED_ARRAY_SHORT_PRINT
1408 case SHARED_FUNCTION_INFO_TYPE: {
1409 SharedFunctionInfo* shared = SharedFunctionInfo::cast(this);
1410 base::SmartArrayPointer<char> debug_name =
1411 shared->DebugName()->ToCString();
1412 if (debug_name[0] != 0) {
1413 os << "<SharedFunctionInfo " << debug_name.get() << ">";
1415 os << "<SharedFunctionInfo>";
1419 case JS_MESSAGE_OBJECT_TYPE:
1420 os << "<JSMessageObject>";
1422 #define MAKE_STRUCT_CASE(NAME, Name, name) \
1424 os << "<" #Name ">"; \
1426 STRUCT_LIST(MAKE_STRUCT_CASE)
1427 #undef MAKE_STRUCT_CASE
1429 Code* code = Code::cast(this);
1430 os << "<Code: " << Code::Kind2String(code->kind()) << ">";
1433 case ODDBALL_TYPE: {
1434 if (IsUndefined()) {
1435 os << "<undefined>";
1436 } else if (IsTheHole()) {
1438 } else if (IsNull()) {
1440 } else if (IsTrue()) {
1442 } else if (IsFalse()) {
1445 os << "<Odd Oddball>";
1450 Symbol* symbol = Symbol::cast(this);
1451 symbol->SymbolShortPrint(os);
1454 case HEAP_NUMBER_TYPE: {
1456 HeapNumber::cast(this)->HeapNumberPrint(os);
1460 case MUTABLE_HEAP_NUMBER_TYPE: {
1461 os << "<MutableNumber: ";
1462 HeapNumber::cast(this)->HeapNumberPrint(os);
1466 case SIMD128_VALUE_TYPE: {
1467 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
1469 os << "<" #Type ">"; \
1472 SIMD128_TYPES(SIMD128_TYPE)
1480 case JS_FUNCTION_PROXY_TYPE:
1481 os << "<JSFunctionProxy>";
1488 HeapStringAllocator allocator;
1489 StringStream accumulator(&allocator);
1490 Cell::cast(this)->value()->ShortPrint(&accumulator);
1491 os << accumulator.ToCString().get();
1494 case PROPERTY_CELL_TYPE: {
1495 os << "PropertyCell for ";
1496 HeapStringAllocator allocator;
1497 StringStream accumulator(&allocator);
1498 PropertyCell* cell = PropertyCell::cast(this);
1499 cell->value()->ShortPrint(&accumulator);
1500 os << accumulator.ToCString().get() << " " << cell->property_details();
1503 case WEAK_CELL_TYPE: {
1504 os << "WeakCell for ";
1505 HeapStringAllocator allocator;
1506 StringStream accumulator(&allocator);
1507 WeakCell::cast(this)->value()->ShortPrint(&accumulator);
1508 os << accumulator.ToCString().get();
1512 os << "<Other heap object (" << map()->instance_type() << ")>";
1518 void HeapObject::Iterate(ObjectVisitor* v) {
1520 IteratePointer(v, kMapOffset);
1521 // Handle object body
1523 IterateBody(m->instance_type(), SizeFromMap(m), v);
1527 bool HeapNumber::HeapNumberBooleanValue() {
1528 return DoubleToBoolean(value());
1532 void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT
1537 #define FIELD_ADDR_CONST(p, offset) \
1538 (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag)
1540 #define READ_INT32_FIELD(p, offset) \
1541 (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset)))
1543 #define READ_INT64_FIELD(p, offset) \
1544 (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset)))
1546 #define READ_BYTE_FIELD(p, offset) \
1547 (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset)))
1551 Handle<String> Simd128Value::ToString(Handle<Simd128Value> input) {
1552 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
1553 if (input->Is##Type()) return Type::ToString(Handle<Type>::cast(input));
1554 SIMD128_TYPES(SIMD128_TYPE)
1557 return Handle<String>::null();
1562 Handle<String> Float32x4::ToString(Handle<Float32x4> input) {
1563 Isolate* const isolate = input->GetIsolate();
1565 Vector<char> buffer(arr, arraysize(arr));
1566 std::ostringstream os;
1567 os << "SIMD.Float32x4("
1568 << std::string(DoubleToCString(input->get_lane(0), buffer)) << ", "
1569 << std::string(DoubleToCString(input->get_lane(1), buffer)) << ", "
1570 << std::string(DoubleToCString(input->get_lane(2), buffer)) << ", "
1571 << std::string(DoubleToCString(input->get_lane(3), buffer)) << ")";
1572 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str());
1576 #define SIMD128_BOOL_TO_STRING(Type, lane_count) \
1577 Handle<String> Type::ToString(Handle<Type> input) { \
1578 Isolate* const isolate = input->GetIsolate(); \
1579 std::ostringstream os; \
1580 os << "SIMD." #Type "("; \
1581 os << (input->get_lane(0) ? "true" : "false"); \
1582 for (int i = 1; i < lane_count; i++) { \
1583 os << ", " << (input->get_lane(i) ? "true" : "false"); \
1586 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \
1588 SIMD128_BOOL_TO_STRING(Bool32x4, 4)
1589 SIMD128_BOOL_TO_STRING(Bool16x8, 8)
1590 SIMD128_BOOL_TO_STRING(Bool8x16, 16)
1591 #undef SIMD128_BOOL_TO_STRING
1594 #define SIMD128_INT_TO_STRING(Type, lane_count) \
1595 Handle<String> Type::ToString(Handle<Type> input) { \
1596 Isolate* const isolate = input->GetIsolate(); \
1598 Vector<char> buffer(arr, arraysize(arr)); \
1599 std::ostringstream os; \
1600 os << "SIMD." #Type "("; \
1601 os << IntToCString(input->get_lane(0), buffer); \
1602 for (int i = 1; i < lane_count; i++) { \
1603 os << ", " << IntToCString(input->get_lane(i), buffer); \
1606 return isolate->factory()->NewStringFromAsciiChecked(os.str().c_str()); \
1608 SIMD128_INT_TO_STRING(Int32x4, 4)
1609 SIMD128_INT_TO_STRING(Uint32x4, 4)
1610 SIMD128_INT_TO_STRING(Int16x8, 8)
1611 SIMD128_INT_TO_STRING(Uint16x8, 8)
1612 SIMD128_INT_TO_STRING(Int8x16, 16)
1613 SIMD128_INT_TO_STRING(Uint8x16, 16)
1614 #undef SIMD128_INT_TO_STRING
1617 bool Simd128Value::BitwiseEquals(const Simd128Value* other) const {
1618 return READ_INT64_FIELD(this, kValueOffset) ==
1619 READ_INT64_FIELD(other, kValueOffset) &&
1620 READ_INT64_FIELD(this, kValueOffset + kInt64Size) ==
1621 READ_INT64_FIELD(other, kValueOffset + kInt64Size);
1625 uint32_t Simd128Value::Hash() const {
1626 uint32_t seed = v8::internal::kZeroHashSeed;
1628 hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed);
1629 hash = ComputeIntegerHash(
1630 READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31);
1631 hash = ComputeIntegerHash(
1632 READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31);
1633 hash = ComputeIntegerHash(
1634 READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31);
1639 void Simd128Value::CopyBits(void* destination) const {
1640 memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size);
1644 String* JSReceiver::class_name() {
1645 if (IsJSFunction() || IsJSFunctionProxy()) {
1646 return GetHeap()->Function_string();
1648 Object* maybe_constructor = map()->GetConstructor();
1649 if (maybe_constructor->IsJSFunction()) {
1650 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1651 return String::cast(constructor->shared()->instance_class_name());
1653 // If the constructor is not present, return "Object".
1654 return GetHeap()->Object_string();
1658 String* Map::constructor_name() {
1659 if (is_prototype_map() && prototype_info()->IsPrototypeInfo()) {
1660 PrototypeInfo* proto_info = PrototypeInfo::cast(prototype_info());
1661 if (proto_info->constructor_name()->IsString()) {
1662 return String::cast(proto_info->constructor_name());
1665 Object* maybe_constructor = GetConstructor();
1666 if (maybe_constructor->IsJSFunction()) {
1667 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1668 String* name = String::cast(constructor->shared()->name());
1669 if (name->length() > 0) return name;
1670 String* inferred_name = constructor->shared()->inferred_name();
1671 if (inferred_name->length() > 0) return inferred_name;
1672 Object* proto = prototype();
1673 if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name();
1675 // TODO(rossberg): what about proxies?
1676 // If the constructor is not present, return "Object".
1677 return GetHeap()->Object_string();
1681 String* JSReceiver::constructor_name() {
1682 return map()->constructor_name();
1686 static Handle<Object> WrapType(Handle<HeapType> type) {
1687 if (type->IsClass()) return Map::WeakCellForMap(type->AsClass()->Map());
1692 MaybeHandle<Map> Map::CopyWithField(Handle<Map> map,
1694 Handle<HeapType> type,
1695 PropertyAttributes attributes,
1696 Representation representation,
1697 TransitionFlag flag) {
1698 DCHECK(DescriptorArray::kNotFound ==
1699 map->instance_descriptors()->Search(
1700 *name, map->NumberOfOwnDescriptors()));
1702 // Ensure the descriptor array does not get too big.
1703 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1704 return MaybeHandle<Map>();
1707 Isolate* isolate = map->GetIsolate();
1709 // Compute the new index for new field.
1710 int index = map->NextFreePropertyIndex();
1712 if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) {
1713 representation = Representation::Tagged();
1714 type = HeapType::Any(isolate);
1717 Handle<Object> wrapped_type(WrapType(type));
1719 DataDescriptor new_field_desc(name, index, wrapped_type, attributes,
1721 Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag);
1722 int unused_property_fields = new_map->unused_property_fields() - 1;
1723 if (unused_property_fields < 0) {
1724 unused_property_fields += JSObject::kFieldsAdded;
1726 new_map->set_unused_property_fields(unused_property_fields);
1731 MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map,
1733 Handle<Object> constant,
1734 PropertyAttributes attributes,
1735 TransitionFlag flag) {
1736 // Ensure the descriptor array does not get too big.
1737 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1738 return MaybeHandle<Map>();
1741 // Allocate new instance descriptors with (name, constant) added.
1742 DataConstantDescriptor new_constant_desc(name, constant, attributes);
1743 return Map::CopyAddDescriptor(map, &new_constant_desc, flag);
1747 void JSObject::AddSlowProperty(Handle<JSObject> object,
1749 Handle<Object> value,
1750 PropertyAttributes attributes) {
1751 DCHECK(!object->HasFastProperties());
1752 Isolate* isolate = object->GetIsolate();
1753 if (object->IsGlobalObject()) {
1754 Handle<GlobalDictionary> dict(object->global_dictionary());
1755 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1756 int entry = dict->FindEntry(name);
1757 // If there's a cell there, just invalidate and set the property.
1758 if (entry != GlobalDictionary::kNotFound) {
1759 PropertyCell::UpdateCell(dict, entry, value, details);
1760 // TODO(ishell): move this to UpdateCell.
1761 // Need to adjust the details.
1762 int index = dict->NextEnumerationIndex();
1763 dict->SetNextEnumerationIndex(index + 1);
1764 PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry));
1765 details = cell->property_details().set_index(index);
1766 cell->set_property_details(details);
1769 auto cell = isolate->factory()->NewPropertyCell();
1770 cell->set_value(*value);
1771 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
1772 : PropertyCellType::kConstant;
1773 details = details.set_cell_type(cell_type);
1776 Handle<GlobalDictionary> result =
1777 GlobalDictionary::Add(dict, name, value, details);
1778 if (*dict != *result) object->set_properties(*result);
1781 Handle<NameDictionary> dict(object->property_dictionary());
1782 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1783 Handle<NameDictionary> result =
1784 NameDictionary::Add(dict, name, value, details);
1785 if (*dict != *result) object->set_properties(*result);
1790 Context* JSObject::GetCreationContext() {
1791 Object* constructor = this->map()->GetConstructor();
1792 JSFunction* function;
1793 if (!constructor->IsJSFunction()) {
1794 // Functions have null as a constructor,
1795 // but any JSFunction knows its context immediately.
1796 function = JSFunction::cast(this);
1798 function = JSFunction::cast(constructor);
1801 return function->context()->native_context();
1805 MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object,
1806 const char* type_str,
1808 Handle<Object> old_value) {
1809 DCHECK(!object->IsJSGlobalProxy());
1810 DCHECK(!object->IsJSGlobalObject());
1811 Isolate* isolate = object->GetIsolate();
1812 HandleScope scope(isolate);
1813 Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str);
1814 Handle<Object> args[] = { type, object, name, old_value };
1815 int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4;
1817 return Execution::Call(isolate,
1818 Handle<JSFunction>(isolate->observers_notify_change()),
1819 isolate->factory()->undefined_value(), argc, args);
1823 const char* Representation::Mnemonic() const {
1825 case kNone: return "v";
1826 case kTagged: return "t";
1827 case kSmi: return "s";
1828 case kDouble: return "d";
1829 case kInteger32: return "i";
1830 case kHeapObject: return "h";
1831 case kExternal: return "x";
1839 bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields,
1840 int target_inobject, int target_unused,
1841 int* old_number_of_fields) {
1842 // If fields were added (or removed), rewrite the instance.
1843 *old_number_of_fields = NumberOfFields();
1844 DCHECK(target_number_of_fields >= *old_number_of_fields);
1845 if (target_number_of_fields != *old_number_of_fields) return true;
1847 // If smi descriptors were replaced by double descriptors, rewrite.
1848 DescriptorArray* old_desc = instance_descriptors();
1849 DescriptorArray* new_desc = target->instance_descriptors();
1850 int limit = NumberOfOwnDescriptors();
1851 for (int i = 0; i < limit; i++) {
1852 if (new_desc->GetDetails(i).representation().IsDouble() !=
1853 old_desc->GetDetails(i).representation().IsDouble()) {
1858 // If no fields were added, and no inobject properties were removed, setting
1859 // the map is sufficient.
1860 if (target_inobject == GetInObjectProperties()) return false;
1861 // In-object slack tracking may have reduced the object size of the new map.
1862 // In that case, succeed if all existing fields were inobject, and they still
1863 // fit within the new inobject size.
1864 DCHECK(target_inobject < GetInObjectProperties());
1865 if (target_number_of_fields <= target_inobject) {
1866 DCHECK(target_number_of_fields + target_unused == target_inobject);
1869 // Otherwise, properties will need to be moved to the backing store.
1874 static void UpdatePrototypeUserRegistration(Handle<Map> old_map,
1875 Handle<Map> new_map,
1877 if (!FLAG_track_prototype_users) return;
1878 if (!old_map->is_prototype_map()) return;
1879 DCHECK(new_map->is_prototype_map());
1880 bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate);
1881 new_map->set_prototype_info(old_map->prototype_info());
1882 old_map->set_prototype_info(Smi::FromInt(0));
1883 if (FLAG_trace_prototype_users) {
1884 PrintF("Moving prototype_info %p from map %p to map %p.\n",
1885 reinterpret_cast<void*>(new_map->prototype_info()),
1886 reinterpret_cast<void*>(*old_map),
1887 reinterpret_cast<void*>(*new_map));
1889 if (was_registered) {
1890 if (new_map->prototype_info()->IsPrototypeInfo()) {
1891 // The new map isn't registered with its prototype yet; reflect this fact
1892 // in the PrototypeInfo it just inherited from the old map.
1893 PrototypeInfo::cast(new_map->prototype_info())
1894 ->set_registry_slot(PrototypeInfo::UNREGISTERED);
1896 JSObject::LazyRegisterPrototypeUser(new_map, isolate);
1901 void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map,
1902 int expected_additional_properties) {
1903 if (object->map() == *new_map) return;
1904 // If this object is a prototype (the callee will check), invalidate any
1905 // prototype chains involving it.
1906 InvalidatePrototypeChains(object->map());
1907 Handle<Map> old_map(object->map());
1909 // If the map was registered with its prototype before, ensure that it
1910 // registers with its new prototype now. This preserves the invariant that
1911 // when a map on a prototype chain is registered with its prototype, then
1912 // all prototypes further up the chain are also registered with their
1913 // respective prototypes.
1914 UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate());
1916 if (object->HasFastProperties()) {
1917 if (!new_map->is_dictionary_map()) {
1918 MigrateFastToFast(object, new_map);
1919 if (old_map->is_prototype_map()) {
1920 DCHECK(!old_map->is_stable());
1921 DCHECK(new_map->is_stable());
1922 // Clear out the old descriptor array to avoid problems to sharing
1923 // the descriptor array without using an explicit.
1924 old_map->InitializeDescriptors(
1925 old_map->GetHeap()->empty_descriptor_array(),
1926 LayoutDescriptor::FastPointerLayout());
1927 // Ensure that no transition was inserted for prototype migrations.
1928 DCHECK_EQ(0, TransitionArray::NumberOfTransitions(
1929 old_map->raw_transitions()));
1930 DCHECK(new_map->GetBackPointer()->IsUndefined());
1933 MigrateFastToSlow(object, new_map, expected_additional_properties);
1936 // For slow-to-fast migrations JSObject::MigrateSlowToFast()
1937 // must be used instead.
1938 CHECK(new_map->is_dictionary_map());
1940 // Slow-to-slow migration is trivial.
1941 object->set_map(*new_map);
1944 // Careful: Don't allocate here!
1945 // For some callers of this method, |object| might be in an inconsistent
1946 // state now: the new map might have a new elements_kind, but the object's
1947 // elements pointer hasn't been updated yet. Callers will fix this, but in
1948 // the meantime, (indirectly) calling JSObjectVerify() must be avoided.
1949 // When adding code here, add a DisallowHeapAllocation too.
1953 // To migrate a fast instance to a fast map:
1954 // - First check whether the instance needs to be rewritten. If not, simply
1956 // - Otherwise, allocate a fixed array large enough to hold all fields, in
1957 // addition to unused space.
1958 // - Copy all existing properties in, in the following order: backing store
1959 // properties, unused fields, inobject properties.
1960 // - If all allocation succeeded, commit the state atomically:
1961 // * Copy inobject properties from the backing store back into the object.
1962 // * Trim the difference in instance size of the object. This also cleanly
1963 // frees inobject properties that moved to the backing store.
1964 // * If there are properties left in the backing store, trim of the space used
1965 // to temporarily store the inobject properties.
1966 // * If there are properties left in the backing store, install the backing
1968 void JSObject::MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) {
1969 Isolate* isolate = object->GetIsolate();
1970 Handle<Map> old_map(object->map());
1971 int old_number_of_fields;
1972 int number_of_fields = new_map->NumberOfFields();
1973 int inobject = new_map->GetInObjectProperties();
1974 int unused = new_map->unused_property_fields();
1976 // Nothing to do if no functions were converted to fields and no smis were
1977 // converted to doubles.
1978 if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject,
1979 unused, &old_number_of_fields)) {
1980 object->synchronized_set_map(*new_map);
1984 int total_size = number_of_fields + unused;
1985 int external = total_size - inobject;
1987 if (number_of_fields != old_number_of_fields &&
1988 new_map->GetBackPointer() == *old_map) {
1989 PropertyDetails details = new_map->GetLastDescriptorDetails();
1991 if (old_map->unused_property_fields() > 0) {
1992 if (details.representation().IsDouble()) {
1994 FieldIndex::ForDescriptor(*new_map, new_map->LastAdded());
1995 if (new_map->IsUnboxedDoubleField(index)) {
1996 object->RawFastDoublePropertyAtPut(index, 0);
1998 Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1999 object->RawFastPropertyAtPut(index, *value);
2002 object->synchronized_set_map(*new_map);
2006 DCHECK(number_of_fields == old_number_of_fields + 1);
2007 // This migration is a transition from a map that has run out of property
2008 // space. Therefore it could be done by extending the backing store.
2009 int grow_by = external - object->properties()->length();
2010 Handle<FixedArray> old_storage = handle(object->properties(), isolate);
2011 Handle<FixedArray> new_storage =
2012 isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by);
2014 // Properly initialize newly added property.
2015 Handle<Object> value;
2016 if (details.representation().IsDouble()) {
2017 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2019 value = isolate->factory()->uninitialized_value();
2021 DCHECK(details.type() == DATA);
2022 int target_index = details.field_index() - inobject;
2023 DCHECK(target_index >= 0); // Must be a backing store index.
2024 new_storage->set(target_index, *value);
2026 // From here on we cannot fail and we shouldn't GC anymore.
2027 DisallowHeapAllocation no_allocation;
2029 // Set the new property value and do the map transition.
2030 object->set_properties(*new_storage);
2031 object->synchronized_set_map(*new_map);
2034 Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size);
2036 Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors());
2037 Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors());
2038 int old_nof = old_map->NumberOfOwnDescriptors();
2039 int new_nof = new_map->NumberOfOwnDescriptors();
2041 // This method only supports generalizing instances to at least the same
2042 // number of properties.
2043 DCHECK(old_nof <= new_nof);
2045 for (int i = 0; i < old_nof; i++) {
2046 PropertyDetails details = new_descriptors->GetDetails(i);
2047 if (details.type() != DATA) continue;
2048 PropertyDetails old_details = old_descriptors->GetDetails(i);
2049 Representation old_representation = old_details.representation();
2050 Representation representation = details.representation();
2051 Handle<Object> value;
2052 if (old_details.type() == ACCESSOR_CONSTANT) {
2053 // In case of kAccessor -> kData property reconfiguration, the property
2054 // must already be prepared for data or certain type.
2055 DCHECK(!details.representation().IsNone());
2056 if (details.representation().IsDouble()) {
2057 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2059 value = isolate->factory()->uninitialized_value();
2061 } else if (old_details.type() == DATA_CONSTANT) {
2062 value = handle(old_descriptors->GetValue(i), isolate);
2063 DCHECK(!old_representation.IsDouble() && !representation.IsDouble());
2065 FieldIndex index = FieldIndex::ForDescriptor(*old_map, i);
2066 if (object->IsUnboxedDoubleField(index)) {
2067 double old = object->RawFastDoublePropertyAt(index);
2068 value = isolate->factory()->NewHeapNumber(
2069 old, representation.IsDouble() ? MUTABLE : IMMUTABLE);
2072 value = handle(object->RawFastPropertyAt(index), isolate);
2073 if (!old_representation.IsDouble() && representation.IsDouble()) {
2074 if (old_representation.IsNone()) {
2075 value = handle(Smi::FromInt(0), isolate);
2077 value = Object::NewStorageFor(isolate, value, representation);
2078 } else if (old_representation.IsDouble() &&
2079 !representation.IsDouble()) {
2080 value = Object::WrapForRead(isolate, value, old_representation);
2084 DCHECK(!(representation.IsDouble() && value->IsSmi()));
2085 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
2086 if (target_index < 0) target_index += total_size;
2087 array->set(target_index, *value);
2090 for (int i = old_nof; i < new_nof; i++) {
2091 PropertyDetails details = new_descriptors->GetDetails(i);
2092 if (details.type() != DATA) continue;
2093 Handle<Object> value;
2094 if (details.representation().IsDouble()) {
2095 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
2097 value = isolate->factory()->uninitialized_value();
2099 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
2100 if (target_index < 0) target_index += total_size;
2101 array->set(target_index, *value);
2104 // From here on we cannot fail and we shouldn't GC anymore.
2105 DisallowHeapAllocation no_allocation;
2107 // Copy (real) inobject properties. If necessary, stop at number_of_fields to
2108 // avoid overwriting |one_pointer_filler_map|.
2109 int limit = Min(inobject, number_of_fields);
2110 for (int i = 0; i < limit; i++) {
2111 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
2112 Object* value = array->get(external + i);
2113 // Can't use JSObject::FastPropertyAtPut() because proper map was not set
2115 if (new_map->IsUnboxedDoubleField(index)) {
2116 DCHECK(value->IsMutableHeapNumber());
2117 object->RawFastDoublePropertyAtPut(index,
2118 HeapNumber::cast(value)->value());
2120 object->RawFastPropertyAtPut(index, value);
2124 Heap* heap = isolate->heap();
2126 // If there are properties in the new backing store, trim it to the correct
2127 // size and install the backing store into the object.
2129 heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject);
2130 object->set_properties(*array);
2133 // Create filler object past the new instance size.
2134 int new_instance_size = new_map->instance_size();
2135 int instance_size_delta = old_map->instance_size() - new_instance_size;
2136 DCHECK(instance_size_delta >= 0);
2138 if (instance_size_delta > 0) {
2139 Address address = object->address();
2140 heap->CreateFillerObjectAt(
2141 address + new_instance_size, instance_size_delta);
2142 heap->AdjustLiveBytes(*object, -instance_size_delta,
2143 Heap::CONCURRENT_TO_SWEEPER);
2146 // We are storing the new map using release store after creating a filler for
2147 // the left-over space to avoid races with the sweeper thread.
2148 object->synchronized_set_map(*new_map);
2152 int Map::NumberOfFields() {
2153 DescriptorArray* descriptors = instance_descriptors();
2155 for (int i = 0; i < NumberOfOwnDescriptors(); i++) {
2156 if (descriptors->GetDetails(i).location() == kField) result++;
2162 Handle<Map> Map::CopyGeneralizeAllRepresentations(
2163 Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind,
2164 PropertyAttributes attributes, const char* reason) {
2165 Isolate* isolate = map->GetIsolate();
2166 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2167 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
2168 Handle<DescriptorArray> descriptors =
2169 DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors);
2171 for (int i = 0; i < number_of_own_descriptors; i++) {
2172 descriptors->SetRepresentation(i, Representation::Tagged());
2173 if (descriptors->GetDetails(i).type() == DATA) {
2174 descriptors->SetValue(i, HeapType::Any());
2178 Handle<LayoutDescriptor> new_layout_descriptor(
2179 LayoutDescriptor::FastPointerLayout(), isolate);
2180 Handle<Map> new_map = CopyReplaceDescriptors(
2181 map, descriptors, new_layout_descriptor, OMIT_TRANSITION,
2182 MaybeHandle<Name>(), reason, SPECIAL_TRANSITION);
2184 // Unless the instance is being migrated, ensure that modify_index is a field.
2185 if (modify_index >= 0) {
2186 PropertyDetails details = descriptors->GetDetails(modify_index);
2187 if (store_mode == FORCE_FIELD &&
2188 (details.type() != DATA || details.attributes() != attributes)) {
2189 int field_index = details.type() == DATA ? details.field_index()
2190 : new_map->NumberOfFields();
2191 DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate),
2192 field_index, attributes, Representation::Tagged());
2193 descriptors->Replace(modify_index, &d);
2194 if (details.type() != DATA) {
2195 int unused_property_fields = new_map->unused_property_fields() - 1;
2196 if (unused_property_fields < 0) {
2197 unused_property_fields += JSObject::kFieldsAdded;
2199 new_map->set_unused_property_fields(unused_property_fields);
2202 DCHECK(details.attributes() == attributes);
2205 if (FLAG_trace_generalization) {
2206 HeapType* field_type =
2207 (details.type() == DATA)
2208 ? map->instance_descriptors()->GetFieldType(modify_index)
2210 map->PrintGeneralization(
2211 stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(),
2212 new_map->NumberOfOwnDescriptors(),
2213 details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD,
2214 details.representation(), Representation::Tagged(), field_type,
2222 void Map::DeprecateTransitionTree() {
2223 if (is_deprecated()) return;
2224 Object* transitions = raw_transitions();
2225 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2226 for (int i = 0; i < num_transitions; ++i) {
2227 TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree();
2230 dependent_code()->DeoptimizeDependentCodeGroup(
2231 GetIsolate(), DependentCode::kTransitionGroup);
2232 NotifyLeafMapLayoutChange();
2236 static inline bool EqualImmutableValues(Object* obj1, Object* obj2) {
2237 if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds.
2238 // TODO(ishell): compare AccessorPairs.
2243 // Invalidates a transition target at |key|, and installs |new_descriptors| over
2244 // the current instance_descriptors to ensure proper sharing of descriptor
2246 // Returns true if the transition target at given key was deprecated.
2247 bool Map::DeprecateTarget(PropertyKind kind, Name* key,
2248 PropertyAttributes attributes,
2249 DescriptorArray* new_descriptors,
2250 LayoutDescriptor* new_layout_descriptor) {
2251 bool transition_target_deprecated = false;
2252 Map* maybe_transition =
2253 TransitionArray::SearchTransition(this, kind, key, attributes);
2254 if (maybe_transition != NULL) {
2255 maybe_transition->DeprecateTransitionTree();
2256 transition_target_deprecated = true;
2259 // Don't overwrite the empty descriptor array.
2260 if (NumberOfOwnDescriptors() == 0) return transition_target_deprecated;
2262 DescriptorArray* to_replace = instance_descriptors();
2263 Map* current = this;
2264 GetHeap()->incremental_marking()->RecordWrites(to_replace);
2265 while (current->instance_descriptors() == to_replace) {
2266 current->SetEnumLength(kInvalidEnumCacheSentinel);
2267 current->UpdateDescriptors(new_descriptors, new_layout_descriptor);
2268 Object* next = current->GetBackPointer();
2269 if (next->IsUndefined()) break;
2270 current = Map::cast(next);
2273 set_owns_descriptors(false);
2274 return transition_target_deprecated;
2278 Map* Map::FindRootMap() {
2281 Object* back = result->GetBackPointer();
2282 if (back->IsUndefined()) return result;
2283 result = Map::cast(back);
2288 Map* Map::FindLastMatchMap(int verbatim,
2290 DescriptorArray* descriptors) {
2291 DisallowHeapAllocation no_allocation;
2293 // This can only be called on roots of transition trees.
2294 DCHECK_EQ(verbatim, NumberOfOwnDescriptors());
2296 Map* current = this;
2298 for (int i = verbatim; i < length; i++) {
2299 Name* name = descriptors->GetKey(i);
2300 PropertyDetails details = descriptors->GetDetails(i);
2301 Map* next = TransitionArray::SearchTransition(current, details.kind(), name,
2302 details.attributes());
2303 if (next == NULL) break;
2304 DescriptorArray* next_descriptors = next->instance_descriptors();
2306 PropertyDetails next_details = next_descriptors->GetDetails(i);
2307 DCHECK_EQ(details.kind(), next_details.kind());
2308 DCHECK_EQ(details.attributes(), next_details.attributes());
2309 if (details.location() != next_details.location()) break;
2310 if (!details.representation().Equals(next_details.representation())) break;
2312 if (next_details.location() == kField) {
2313 HeapType* next_field_type = next_descriptors->GetFieldType(i);
2314 if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) {
2318 if (!EqualImmutableValues(descriptors->GetValue(i),
2319 next_descriptors->GetValue(i))) {
2329 Map* Map::FindFieldOwner(int descriptor) {
2330 DisallowHeapAllocation no_allocation;
2331 DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type());
2334 Object* back = result->GetBackPointer();
2335 if (back->IsUndefined()) break;
2336 Map* parent = Map::cast(back);
2337 if (parent->NumberOfOwnDescriptors() <= descriptor) break;
2344 void Map::UpdateFieldType(int descriptor, Handle<Name> name,
2345 Representation new_representation,
2346 Handle<Object> new_wrapped_type) {
2347 DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell());
2348 DisallowHeapAllocation no_allocation;
2349 PropertyDetails details = instance_descriptors()->GetDetails(descriptor);
2350 if (details.type() != DATA) return;
2351 Object* transitions = raw_transitions();
2352 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2353 for (int i = 0; i < num_transitions; ++i) {
2354 Map* target = TransitionArray::GetTarget(transitions, i);
2355 target->UpdateFieldType(descriptor, name, new_representation,
2358 // It is allowed to change representation here only from None to something.
2359 DCHECK(details.representation().Equals(new_representation) ||
2360 details.representation().IsNone());
2362 // Skip if already updated the shared descriptor.
2363 if (instance_descriptors()->GetValue(descriptor) == *new_wrapped_type) return;
2364 DataDescriptor d(name, instance_descriptors()->GetFieldIndex(descriptor),
2365 new_wrapped_type, details.attributes(), new_representation);
2366 instance_descriptors()->Replace(descriptor, &d);
2371 Handle<HeapType> Map::GeneralizeFieldType(Handle<HeapType> type1,
2372 Handle<HeapType> type2,
2374 if (type1->NowIs(type2)) return type2;
2375 if (type2->NowIs(type1)) return type1;
2376 return HeapType::Any(isolate);
2381 void Map::GeneralizeFieldType(Handle<Map> map, int modify_index,
2382 Representation new_representation,
2383 Handle<HeapType> new_field_type) {
2384 Isolate* isolate = map->GetIsolate();
2386 // Check if we actually need to generalize the field type at all.
2387 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2388 Representation old_representation =
2389 old_descriptors->GetDetails(modify_index).representation();
2390 Handle<HeapType> old_field_type(old_descriptors->GetFieldType(modify_index),
2393 if (old_representation.Equals(new_representation) &&
2394 new_field_type->NowIs(old_field_type)) {
2395 DCHECK(Map::GeneralizeFieldType(old_field_type,
2397 isolate)->NowIs(old_field_type));
2401 // Determine the field owner.
2402 Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate);
2403 Handle<DescriptorArray> descriptors(
2404 field_owner->instance_descriptors(), isolate);
2405 DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index));
2406 bool old_field_type_was_cleared =
2407 old_field_type->Is(HeapType::None()) && old_representation.IsHeapObject();
2409 // Determine the generalized new field type. Conservatively assume type Any
2410 // for cleared field types because the cleared type could have been a
2411 // deprecated map and there still could be live instances with a non-
2412 // deprecated version of the map.
2414 old_field_type_was_cleared
2415 ? HeapType::Any(isolate)
2416 : Map::GeneralizeFieldType(old_field_type, new_field_type, isolate);
2418 PropertyDetails details = descriptors->GetDetails(modify_index);
2419 Handle<Name> name(descriptors->GetKey(modify_index));
2421 Handle<Object> wrapped_type(WrapType(new_field_type));
2422 field_owner->UpdateFieldType(modify_index, name, new_representation,
2424 field_owner->dependent_code()->DeoptimizeDependentCodeGroup(
2425 isolate, DependentCode::kFieldTypeGroup);
2427 if (FLAG_trace_generalization) {
2428 map->PrintGeneralization(
2429 stdout, "field type generalization",
2430 modify_index, map->NumberOfOwnDescriptors(),
2431 map->NumberOfOwnDescriptors(), false,
2432 details.representation(), details.representation(),
2433 *old_field_type, *new_field_type);
2438 static inline Handle<HeapType> GetFieldType(Isolate* isolate,
2439 Handle<DescriptorArray> descriptors,
2441 PropertyLocation location,
2442 Representation representation) {
2444 PropertyDetails details = descriptors->GetDetails(descriptor);
2445 DCHECK_EQ(kData, details.kind());
2446 DCHECK_EQ(details.location(), location);
2448 if (location == kField) {
2449 return handle(descriptors->GetFieldType(descriptor), isolate);
2451 return descriptors->GetValue(descriptor)
2452 ->OptimalType(isolate, representation);
2457 // Reconfigures property at |modify_index| with |new_kind|, |new_attributes|,
2458 // |store_mode| and/or |new_representation|/|new_field_type|.
2459 // If |modify_index| is negative then no properties are reconfigured but the
2460 // map is migrated to the up-to-date non-deprecated state.
2462 // This method rewrites or completes the transition tree to reflect the new
2463 // change. To avoid high degrees over polymorphism, and to stabilize quickly,
2464 // on every rewrite the new type is deduced by merging the current type with
2465 // any potential new (partial) version of the type in the transition tree.
2466 // To do this, on each rewrite:
2467 // - Search the root of the transition tree using FindRootMap.
2468 // - Find |target_map|, the newest matching version of this map using the
2469 // virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at
2470 // |modify_index| is considered to be of |new_kind| and having
2471 // |new_attributes|) to walk the transition tree.
2472 // - Merge/generalize the "enhanced" descriptor array of the |old_map| and
2473 // descriptor array of the |target_map|.
2474 // - Generalize the |modify_index| descriptor using |new_representation| and
2475 // |new_field_type|.
2476 // - Walk the tree again starting from the root towards |target_map|. Stop at
2477 // |split_map|, the first map who's descriptor array does not match the merged
2478 // descriptor array.
2479 // - If |target_map| == |split_map|, |target_map| is in the expected state.
2481 // - Otherwise, invalidate the outdated transition target from |target_map|, and
2482 // replace its transition tree with a new branch for the updated descriptors.
2483 Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index,
2484 PropertyKind new_kind,
2485 PropertyAttributes new_attributes,
2486 Representation new_representation,
2487 Handle<HeapType> new_field_type,
2488 StoreMode store_mode) {
2489 DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet.
2490 DCHECK(store_mode != FORCE_FIELD || modify_index >= 0);
2491 Isolate* isolate = old_map->GetIsolate();
2493 Handle<DescriptorArray> old_descriptors(
2494 old_map->instance_descriptors(), isolate);
2495 int old_nof = old_map->NumberOfOwnDescriptors();
2497 // If it's just a representation generalization case (i.e. property kind and
2498 // attributes stays unchanged) it's fine to transition from None to anything
2499 // but double without any modification to the object, because the default
2500 // uninitialized value for representation None can be overwritten by both
2501 // smi and tagged values. Doubles, however, would require a box allocation.
2502 if (modify_index >= 0 && !new_representation.IsNone() &&
2503 !new_representation.IsDouble()) {
2504 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2505 Representation old_representation = old_details.representation();
2507 if (old_representation.IsNone()) {
2508 DCHECK_EQ(new_kind, old_details.kind());
2509 DCHECK_EQ(new_attributes, old_details.attributes());
2510 DCHECK_EQ(DATA, old_details.type());
2511 if (FLAG_trace_generalization) {
2512 old_map->PrintGeneralization(
2513 stdout, "uninitialized field", modify_index,
2514 old_map->NumberOfOwnDescriptors(),
2515 old_map->NumberOfOwnDescriptors(), false, old_representation,
2516 new_representation, old_descriptors->GetFieldType(modify_index),
2519 Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate);
2521 GeneralizeFieldType(field_owner, modify_index, new_representation,
2523 DCHECK(old_descriptors->GetDetails(modify_index)
2525 .Equals(new_representation));
2527 old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type));
2532 // Check the state of the root map.
2533 Handle<Map> root_map(old_map->FindRootMap(), isolate);
2534 if (!old_map->EquivalentToForTransition(*root_map)) {
2535 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2536 new_kind, new_attributes,
2537 "GenAll_NotEquivalent");
2540 ElementsKind from_kind = root_map->elements_kind();
2541 ElementsKind to_kind = old_map->elements_kind();
2542 // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
2543 if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS &&
2544 to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS &&
2545 !(IsTransitionableFastElementsKind(from_kind) &&
2546 IsMoreGeneralElementsKindTransition(from_kind, to_kind))) {
2547 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2548 new_kind, new_attributes,
2549 "GenAll_InvalidElementsTransition");
2551 int root_nof = root_map->NumberOfOwnDescriptors();
2552 if (modify_index >= 0 && modify_index < root_nof) {
2553 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2554 if (old_details.kind() != new_kind ||
2555 old_details.attributes() != new_attributes) {
2556 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2557 new_kind, new_attributes,
2558 "GenAll_RootModification1");
2560 if ((old_details.type() != DATA && store_mode == FORCE_FIELD) ||
2561 (old_details.type() == DATA &&
2562 (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) ||
2563 !new_representation.fits_into(old_details.representation())))) {
2564 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2565 new_kind, new_attributes,
2566 "GenAll_RootModification2");
2570 // From here on, use the map with correct elements kind as root map.
2571 if (from_kind != to_kind) {
2572 root_map = Map::AsElementsKind(root_map, to_kind);
2575 Handle<Map> target_map = root_map;
2576 for (int i = root_nof; i < old_nof; ++i) {
2577 PropertyDetails old_details = old_descriptors->GetDetails(i);
2578 PropertyKind next_kind;
2579 PropertyLocation next_location;
2580 PropertyAttributes next_attributes;
2581 Representation next_representation;
2582 bool property_kind_reconfiguration = false;
2584 if (modify_index == i) {
2585 DCHECK_EQ(FORCE_FIELD, store_mode);
2586 property_kind_reconfiguration = old_details.kind() != new_kind;
2588 next_kind = new_kind;
2589 next_location = kField;
2590 next_attributes = new_attributes;
2591 // If property kind is not reconfigured merge the result with
2592 // representation/field type from the old descriptor.
2593 next_representation = new_representation;
2594 if (!property_kind_reconfiguration) {
2595 next_representation =
2596 next_representation.generalize(old_details.representation());
2600 next_kind = old_details.kind();
2601 next_location = old_details.location();
2602 next_attributes = old_details.attributes();
2603 next_representation = old_details.representation();
2605 Map* transition = TransitionArray::SearchTransition(
2606 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2607 if (transition == NULL) break;
2608 Handle<Map> tmp_map(transition, isolate);
2610 Handle<DescriptorArray> tmp_descriptors = handle(
2611 tmp_map->instance_descriptors(), isolate);
2613 // Check if target map is incompatible.
2614 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2615 DCHECK_EQ(next_kind, tmp_details.kind());
2616 DCHECK_EQ(next_attributes, tmp_details.attributes());
2617 if (next_kind == kAccessor &&
2618 !EqualImmutableValues(old_descriptors->GetValue(i),
2619 tmp_descriptors->GetValue(i))) {
2620 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2621 new_kind, new_attributes,
2622 "GenAll_Incompatible");
2624 if (next_location == kField && tmp_details.location() == kDescriptor) break;
2626 Representation tmp_representation = tmp_details.representation();
2627 if (!next_representation.fits_into(tmp_representation)) break;
2629 PropertyLocation old_location = old_details.location();
2630 PropertyLocation tmp_location = tmp_details.location();
2631 if (tmp_location == kField) {
2632 if (next_kind == kData) {
2633 Handle<HeapType> next_field_type;
2634 if (modify_index == i) {
2635 next_field_type = new_field_type;
2636 if (!property_kind_reconfiguration) {
2637 Handle<HeapType> old_field_type =
2638 GetFieldType(isolate, old_descriptors, i,
2639 old_details.location(), tmp_representation);
2641 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2644 Handle<HeapType> old_field_type =
2645 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2646 tmp_representation);
2647 next_field_type = old_field_type;
2649 GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type);
2651 } else if (old_location == kField ||
2652 !EqualImmutableValues(old_descriptors->GetValue(i),
2653 tmp_descriptors->GetValue(i))) {
2656 DCHECK(!tmp_map->is_deprecated());
2657 target_map = tmp_map;
2660 // Directly change the map if the target map is more general.
2661 Handle<DescriptorArray> target_descriptors(
2662 target_map->instance_descriptors(), isolate);
2663 int target_nof = target_map->NumberOfOwnDescriptors();
2664 if (target_nof == old_nof &&
2665 (store_mode != FORCE_FIELD ||
2666 (modify_index >= 0 &&
2667 target_descriptors->GetDetails(modify_index).location() == kField))) {
2669 if (modify_index >= 0) {
2670 PropertyDetails details = target_descriptors->GetDetails(modify_index);
2671 DCHECK_EQ(new_kind, details.kind());
2672 DCHECK_EQ(new_attributes, details.attributes());
2673 DCHECK(new_representation.fits_into(details.representation()));
2674 DCHECK(details.location() != kField ||
2675 new_field_type->NowIs(
2676 target_descriptors->GetFieldType(modify_index)));
2679 if (*target_map != *old_map) {
2680 old_map->NotifyLeafMapLayoutChange();
2685 // Find the last compatible target map in the transition tree.
2686 for (int i = target_nof; i < old_nof; ++i) {
2687 PropertyDetails old_details = old_descriptors->GetDetails(i);
2688 PropertyKind next_kind;
2689 PropertyAttributes next_attributes;
2690 if (modify_index == i) {
2691 next_kind = new_kind;
2692 next_attributes = new_attributes;
2694 next_kind = old_details.kind();
2695 next_attributes = old_details.attributes();
2697 Map* transition = TransitionArray::SearchTransition(
2698 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2699 if (transition == NULL) break;
2700 Handle<Map> tmp_map(transition, isolate);
2701 Handle<DescriptorArray> tmp_descriptors(
2702 tmp_map->instance_descriptors(), isolate);
2704 // Check if target map is compatible.
2706 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2707 DCHECK_EQ(next_kind, tmp_details.kind());
2708 DCHECK_EQ(next_attributes, tmp_details.attributes());
2710 if (next_kind == kAccessor &&
2711 !EqualImmutableValues(old_descriptors->GetValue(i),
2712 tmp_descriptors->GetValue(i))) {
2713 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2714 new_kind, new_attributes,
2715 "GenAll_Incompatible");
2717 DCHECK(!tmp_map->is_deprecated());
2718 target_map = tmp_map;
2720 target_nof = target_map->NumberOfOwnDescriptors();
2721 target_descriptors = handle(target_map->instance_descriptors(), isolate);
2723 // Allocate a new descriptor array large enough to hold the required
2724 // descriptors, with minimally the exact same size as the old descriptor
2726 int new_slack = Max(
2727 old_nof, old_descriptors->number_of_descriptors()) - old_nof;
2728 Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate(
2729 isolate, old_nof, new_slack);
2730 DCHECK(new_descriptors->length() > target_descriptors->length() ||
2731 new_descriptors->NumberOfSlackDescriptors() > 0 ||
2732 new_descriptors->number_of_descriptors() ==
2733 old_descriptors->number_of_descriptors());
2734 DCHECK(new_descriptors->number_of_descriptors() == old_nof);
2737 int current_offset = 0;
2738 for (int i = 0; i < root_nof; ++i) {
2739 PropertyDetails old_details = old_descriptors->GetDetails(i);
2740 if (old_details.location() == kField) {
2741 current_offset += old_details.field_width_in_words();
2743 Descriptor d(handle(old_descriptors->GetKey(i), isolate),
2744 handle(old_descriptors->GetValue(i), isolate),
2746 new_descriptors->Set(i, &d);
2749 // |root_nof| -> |target_nof|
2750 for (int i = root_nof; i < target_nof; ++i) {
2751 Handle<Name> target_key(target_descriptors->GetKey(i), isolate);
2752 PropertyDetails old_details = old_descriptors->GetDetails(i);
2753 PropertyDetails target_details = target_descriptors->GetDetails(i);
2755 PropertyKind next_kind;
2756 PropertyAttributes next_attributes;
2757 PropertyLocation next_location;
2758 Representation next_representation;
2759 bool property_kind_reconfiguration = false;
2761 if (modify_index == i) {
2762 DCHECK_EQ(FORCE_FIELD, store_mode);
2763 property_kind_reconfiguration = old_details.kind() != new_kind;
2765 next_kind = new_kind;
2766 next_attributes = new_attributes;
2767 next_location = kField;
2769 // Merge new representation/field type with ones from the target
2770 // descriptor. If property kind is not reconfigured merge the result with
2771 // representation/field type from the old descriptor.
2772 next_representation =
2773 new_representation.generalize(target_details.representation());
2774 if (!property_kind_reconfiguration) {
2775 next_representation =
2776 next_representation.generalize(old_details.representation());
2779 // Merge old_descriptor and target_descriptor entries.
2780 DCHECK_EQ(target_details.kind(), old_details.kind());
2781 next_kind = target_details.kind();
2782 next_attributes = target_details.attributes();
2784 old_details.location() == kField ||
2785 target_details.location() == kField ||
2786 !EqualImmutableValues(target_descriptors->GetValue(i),
2787 old_descriptors->GetValue(i))
2791 next_representation = old_details.representation().generalize(
2792 target_details.representation());
2794 DCHECK_EQ(next_kind, target_details.kind());
2795 DCHECK_EQ(next_attributes, target_details.attributes());
2797 if (next_location == kField) {
2798 if (next_kind == kData) {
2799 Handle<HeapType> target_field_type =
2800 GetFieldType(isolate, target_descriptors, i,
2801 target_details.location(), next_representation);
2803 Handle<HeapType> next_field_type;
2804 if (modify_index == i) {
2806 GeneralizeFieldType(target_field_type, new_field_type, isolate);
2807 if (!property_kind_reconfiguration) {
2808 Handle<HeapType> old_field_type =
2809 GetFieldType(isolate, old_descriptors, i,
2810 old_details.location(), next_representation);
2812 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2815 Handle<HeapType> old_field_type =
2816 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2817 next_representation);
2819 GeneralizeFieldType(target_field_type, old_field_type, isolate);
2821 Handle<Object> wrapped_type(WrapType(next_field_type));
2822 DataDescriptor d(target_key, current_offset, wrapped_type,
2823 next_attributes, next_representation);
2824 current_offset += d.GetDetails().field_width_in_words();
2825 new_descriptors->Set(i, &d);
2827 UNIMPLEMENTED(); // TODO(ishell): implement.
2830 PropertyDetails details(next_attributes, next_kind, next_location,
2831 next_representation);
2832 Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate),
2834 new_descriptors->Set(i, &d);
2838 // |target_nof| -> |old_nof|
2839 for (int i = target_nof; i < old_nof; ++i) {
2840 PropertyDetails old_details = old_descriptors->GetDetails(i);
2841 Handle<Name> old_key(old_descriptors->GetKey(i), isolate);
2843 // Merge old_descriptor entry and modified details together.
2844 PropertyKind next_kind;
2845 PropertyAttributes next_attributes;
2846 PropertyLocation next_location;
2847 Representation next_representation;
2848 bool property_kind_reconfiguration = false;
2850 if (modify_index == i) {
2851 DCHECK_EQ(FORCE_FIELD, store_mode);
2852 // In case of property kind reconfiguration it is not necessary to
2853 // take into account representation/field type of the old descriptor.
2854 property_kind_reconfiguration = old_details.kind() != new_kind;
2856 next_kind = new_kind;
2857 next_attributes = new_attributes;
2858 next_location = kField;
2859 next_representation = new_representation;
2860 if (!property_kind_reconfiguration) {
2861 next_representation =
2862 next_representation.generalize(old_details.representation());
2865 next_kind = old_details.kind();
2866 next_attributes = old_details.attributes();
2867 next_location = old_details.location();
2868 next_representation = old_details.representation();
2871 if (next_location == kField) {
2872 if (next_kind == kData) {
2873 Handle<HeapType> next_field_type;
2874 if (modify_index == i) {
2875 next_field_type = new_field_type;
2876 if (!property_kind_reconfiguration) {
2877 Handle<HeapType> old_field_type =
2878 GetFieldType(isolate, old_descriptors, i,
2879 old_details.location(), next_representation);
2881 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2884 Handle<HeapType> old_field_type =
2885 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2886 next_representation);
2887 next_field_type = old_field_type;
2890 Handle<Object> wrapped_type(WrapType(next_field_type));
2892 DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes,
2893 next_representation);
2894 current_offset += d.GetDetails().field_width_in_words();
2895 new_descriptors->Set(i, &d);
2897 UNIMPLEMENTED(); // TODO(ishell): implement.
2900 PropertyDetails details(next_attributes, next_kind, next_location,
2901 next_representation);
2902 Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate),
2904 new_descriptors->Set(i, &d);
2908 new_descriptors->Sort();
2910 DCHECK(store_mode != FORCE_FIELD ||
2911 new_descriptors->GetDetails(modify_index).location() == kField);
2913 Handle<Map> split_map(root_map->FindLastMatchMap(
2914 root_nof, old_nof, *new_descriptors), isolate);
2915 int split_nof = split_map->NumberOfOwnDescriptors();
2916 DCHECK_NE(old_nof, split_nof);
2918 Handle<LayoutDescriptor> new_layout_descriptor =
2919 LayoutDescriptor::New(split_map, new_descriptors, old_nof);
2921 PropertyKind split_kind;
2922 PropertyAttributes split_attributes;
2923 if (modify_index == split_nof) {
2924 split_kind = new_kind;
2925 split_attributes = new_attributes;
2927 PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof);
2928 split_kind = split_prop_details.kind();
2929 split_attributes = split_prop_details.attributes();
2931 bool transition_target_deprecated = split_map->DeprecateTarget(
2932 split_kind, old_descriptors->GetKey(split_nof), split_attributes,
2933 *new_descriptors, *new_layout_descriptor);
2935 // If |transition_target_deprecated| is true then the transition array
2936 // already contains entry for given descriptor. This means that the transition
2937 // could be inserted regardless of whether transitions array is full or not.
2938 if (!transition_target_deprecated &&
2939 !TransitionArray::CanHaveMoreTransitions(split_map)) {
2940 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2941 new_kind, new_attributes,
2942 "GenAll_CantHaveMoreTransitions");
2945 old_map->NotifyLeafMapLayoutChange();
2947 if (FLAG_trace_generalization && modify_index >= 0) {
2948 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2949 PropertyDetails new_details = new_descriptors->GetDetails(modify_index);
2950 Handle<HeapType> old_field_type =
2951 (old_details.type() == DATA)
2952 ? handle(old_descriptors->GetFieldType(modify_index), isolate)
2953 : HeapType::Constant(
2954 handle(old_descriptors->GetValue(modify_index), isolate),
2956 Handle<HeapType> new_field_type =
2957 (new_details.type() == DATA)
2958 ? handle(new_descriptors->GetFieldType(modify_index), isolate)
2959 : HeapType::Constant(
2960 handle(new_descriptors->GetValue(modify_index), isolate),
2962 old_map->PrintGeneralization(
2963 stdout, "", modify_index, split_nof, old_nof,
2964 old_details.location() == kDescriptor && store_mode == FORCE_FIELD,
2965 old_details.representation(), new_details.representation(),
2966 *old_field_type, *new_field_type);
2969 // Add missing transitions.
2970 Handle<Map> new_map = split_map;
2971 for (int i = split_nof; i < old_nof; ++i) {
2972 new_map = CopyInstallDescriptors(new_map, i, new_descriptors,
2973 new_layout_descriptor);
2975 new_map->set_owns_descriptors(true);
2980 // Generalize the representation of all DATA descriptors.
2981 Handle<Map> Map::GeneralizeAllFieldRepresentations(
2983 Handle<DescriptorArray> descriptors(map->instance_descriptors());
2984 for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) {
2985 PropertyDetails details = descriptors->GetDetails(i);
2986 if (details.type() == DATA) {
2987 map = ReconfigureProperty(map, i, kData, details.attributes(),
2988 Representation::Tagged(),
2989 HeapType::Any(map->GetIsolate()), FORCE_FIELD);
2997 MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) {
2998 DisallowHeapAllocation no_allocation;
2999 DisallowDeoptimization no_deoptimization(old_map->GetIsolate());
3001 if (!old_map->is_deprecated()) return old_map;
3003 // Check the state of the root map.
3004 Map* root_map = old_map->FindRootMap();
3005 if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>();
3007 ElementsKind from_kind = root_map->elements_kind();
3008 ElementsKind to_kind = old_map->elements_kind();
3009 if (from_kind != to_kind) {
3010 // Try to follow existing elements kind transitions.
3011 root_map = root_map->LookupElementsTransitionMap(to_kind);
3012 if (root_map == NULL) return MaybeHandle<Map>();
3013 // From here on, use the map with correct elements kind as root map.
3015 int root_nof = root_map->NumberOfOwnDescriptors();
3017 int old_nof = old_map->NumberOfOwnDescriptors();
3018 DescriptorArray* old_descriptors = old_map->instance_descriptors();
3020 Map* new_map = root_map;
3021 for (int i = root_nof; i < old_nof; ++i) {
3022 PropertyDetails old_details = old_descriptors->GetDetails(i);
3023 Map* transition = TransitionArray::SearchTransition(
3024 new_map, old_details.kind(), old_descriptors->GetKey(i),
3025 old_details.attributes());
3026 if (transition == NULL) return MaybeHandle<Map>();
3027 new_map = transition;
3028 DescriptorArray* new_descriptors = new_map->instance_descriptors();
3030 PropertyDetails new_details = new_descriptors->GetDetails(i);
3031 DCHECK_EQ(old_details.kind(), new_details.kind());
3032 DCHECK_EQ(old_details.attributes(), new_details.attributes());
3033 if (!old_details.representation().fits_into(new_details.representation())) {
3034 return MaybeHandle<Map>();
3036 switch (new_details.type()) {
3038 HeapType* new_type = new_descriptors->GetFieldType(i);
3039 PropertyType old_property_type = old_details.type();
3040 if (old_property_type == DATA) {
3041 HeapType* old_type = old_descriptors->GetFieldType(i);
3042 if (!old_type->NowIs(new_type)) {
3043 return MaybeHandle<Map>();
3046 DCHECK(old_property_type == DATA_CONSTANT);
3047 Object* old_value = old_descriptors->GetValue(i);
3048 if (!new_type->NowContains(old_value)) {
3049 return MaybeHandle<Map>();
3056 HeapType* new_type = new_descriptors->GetFieldType(i);
3057 DCHECK(HeapType::Any()->Is(new_type));
3063 case ACCESSOR_CONSTANT: {
3064 Object* old_value = old_descriptors->GetValue(i);
3065 Object* new_value = new_descriptors->GetValue(i);
3066 if (old_details.location() == kField || old_value != new_value) {
3067 return MaybeHandle<Map>();
3073 if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>();
3074 return handle(new_map);
3079 Handle<Map> Map::Update(Handle<Map> map) {
3080 if (!map->is_deprecated()) return map;
3081 return ReconfigureProperty(map, -1, kData, NONE, Representation::None(),
3082 HeapType::None(map->GetIsolate()),
3083 ALLOW_IN_DESCRIPTOR);
3087 MaybeHandle<Object> JSObject::SetPropertyWithInterceptor(LookupIterator* it,
3088 Handle<Object> value) {
3089 Isolate* isolate = it->isolate();
3090 // Make sure that the top context does not change when doing callbacks or
3091 // interceptor calls.
3092 AssertNoContextChange ncc(isolate);
3094 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
3095 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
3096 if (interceptor->setter()->IsUndefined()) return MaybeHandle<Object>();
3098 Handle<JSObject> holder = it->GetHolder<JSObject>();
3099 v8::Local<v8::Value> result;
3100 PropertyCallbackArguments args(isolate, interceptor->data(),
3101 *it->GetReceiver(), *holder);
3103 if (it->IsElement()) {
3104 uint32_t index = it->index();
3105 v8::IndexedPropertySetterCallback setter =
3106 v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter());
3108 ApiIndexedPropertyAccess("interceptor-indexed-set", *holder, index));
3109 result = args.Call(setter, index, v8::Utils::ToLocal(value));
3111 Handle<Name> name = it->name();
3113 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
3114 return MaybeHandle<Object>();
3117 v8::GenericNamedPropertySetterCallback setter =
3118 v8::ToCData<v8::GenericNamedPropertySetterCallback>(
3119 interceptor->setter());
3121 ApiNamedPropertyAccess("interceptor-named-set", *holder, *name));
3123 args.Call(setter, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
3126 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
3127 if (result.IsEmpty()) return MaybeHandle<Object>();
3129 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
3130 result_internal->VerifyApiCallResultType();
3136 MaybeHandle<Object> Object::SetProperty(Handle<Object> object,
3137 Handle<Name> name, Handle<Object> value,
3138 LanguageMode language_mode,
3139 StoreFromKeyed store_mode) {
3140 LookupIterator it(object, name);
3141 return SetProperty(&it, value, language_mode, store_mode);
3145 MaybeHandle<Object> Object::SetPropertyInternal(LookupIterator* it,
3146 Handle<Object> value,
3147 LanguageMode language_mode,
3148 StoreFromKeyed store_mode,
3150 // Make sure that the top context does not change when doing callbacks or
3151 // interceptor calls.
3152 AssertNoContextChange ncc(it->isolate());
3157 for (; it->IsFound(); it->Next()) {
3158 switch (it->state()) {
3159 case LookupIterator::NOT_FOUND:
3162 case LookupIterator::ACCESS_CHECK:
3163 if (it->HasAccess()) break;
3164 // Check whether it makes sense to reuse the lookup iterator. Here it
3165 // might still call into setters up the prototype chain.
3166 return JSObject::SetPropertyWithFailedAccessCheck(it, value);
3168 case LookupIterator::JSPROXY:
3169 if (it->HolderIsReceiverOrHiddenPrototype()) {
3170 return JSProxy::SetPropertyWithHandler(
3171 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(), value,
3174 // TODO(verwaest): Use the MaybeHandle to indicate result.
3175 bool has_result = false;
3176 MaybeHandle<Object> maybe_result =
3177 JSProxy::SetPropertyViaPrototypesWithHandler(
3178 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(),
3179 value, language_mode, &has_result);
3180 if (has_result) return maybe_result;
3185 case LookupIterator::INTERCEPTOR:
3186 if (it->HolderIsReceiverOrHiddenPrototype()) {
3187 MaybeHandle<Object> maybe_result =
3188 JSObject::SetPropertyWithInterceptor(it, value);
3189 if (!maybe_result.is_null()) return maybe_result;
3190 if (it->isolate()->has_pending_exception()) return maybe_result;
3192 Maybe<PropertyAttributes> maybe_attributes =
3193 JSObject::GetPropertyAttributesWithInterceptor(it);
3194 if (!maybe_attributes.IsJust()) return MaybeHandle<Object>();
3195 done = maybe_attributes.FromJust() != ABSENT;
3196 if (done && (maybe_attributes.FromJust() & READ_ONLY) != 0) {
3197 return WriteToReadOnlyProperty(it, value, language_mode);
3202 case LookupIterator::ACCESSOR: {
3203 if (it->IsReadOnly()) {
3204 return WriteToReadOnlyProperty(it, value, language_mode);
3206 Handle<Object> accessors = it->GetAccessors();
3207 if (accessors->IsAccessorInfo() &&
3208 !it->HolderIsReceiverOrHiddenPrototype() &&
3209 AccessorInfo::cast(*accessors)->is_special_data_property()) {
3213 return SetPropertyWithAccessor(it, value, language_mode);
3215 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3216 // TODO(verwaest): We should throw an exception.
3219 case LookupIterator::DATA:
3220 if (it->IsReadOnly()) {
3221 return WriteToReadOnlyProperty(it, value, language_mode);
3223 if (it->HolderIsReceiverOrHiddenPrototype()) {
3224 return SetDataProperty(it, value);
3229 case LookupIterator::TRANSITION:
3237 // If the receiver is the JSGlobalObject, the store was contextual. In case
3238 // the property did not exist yet on the global object itself, we have to
3239 // throw a reference error in strict mode.
3240 if (it->GetReceiver()->IsJSGlobalObject() && is_strict(language_mode)) {
3241 THROW_NEW_ERROR(it->isolate(),
3242 NewReferenceError(MessageTemplate::kNotDefined, it->name()),
3247 return MaybeHandle<Object>();
3251 MaybeHandle<Object> Object::SetProperty(LookupIterator* it,
3252 Handle<Object> value,
3253 LanguageMode language_mode,
3254 StoreFromKeyed store_mode) {
3256 MaybeHandle<Object> result =
3257 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3258 if (found) return result;
3259 return AddDataProperty(it, value, NONE, language_mode, store_mode);
3263 MaybeHandle<Object> Object::SetSuperProperty(LookupIterator* it,
3264 Handle<Object> value,
3265 LanguageMode language_mode,
3266 StoreFromKeyed store_mode) {
3268 MaybeHandle<Object> result =
3269 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3270 if (found) return result;
3272 if (!it->GetReceiver()->IsJSReceiver()) {
3273 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3274 it->GetName(), value, language_mode);
3277 LookupIterator::Configuration c = LookupIterator::OWN;
3278 LookupIterator own_lookup =
3280 ? LookupIterator(it->isolate(), it->GetReceiver(), it->index(), c)
3281 : LookupIterator(it->GetReceiver(), it->name(), c);
3283 for (; own_lookup.IsFound(); own_lookup.Next()) {
3284 switch (own_lookup.state()) {
3285 case LookupIterator::ACCESS_CHECK:
3286 if (!own_lookup.HasAccess()) {
3287 return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value);
3291 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3292 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3293 value, language_mode);
3295 case LookupIterator::DATA: {
3296 PropertyDetails details = own_lookup.property_details();
3297 if (details.IsConfigurable() || !details.IsReadOnly()) {
3298 return JSObject::DefineOwnPropertyIgnoreAttributes(
3299 &own_lookup, value, details.attributes());
3301 return WriteToReadOnlyProperty(&own_lookup, value, language_mode);
3304 case LookupIterator::ACCESSOR: {
3305 PropertyDetails details = own_lookup.property_details();
3306 if (details.IsConfigurable()) {
3307 return JSObject::DefineOwnPropertyIgnoreAttributes(
3308 &own_lookup, value, details.attributes());
3311 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3312 value, language_mode);
3315 case LookupIterator::INTERCEPTOR:
3316 case LookupIterator::JSPROXY: {
3318 MaybeHandle<Object> result = SetPropertyInternal(
3319 &own_lookup, value, language_mode, store_mode, &found);
3320 if (found) return result;
3324 case LookupIterator::NOT_FOUND:
3325 case LookupIterator::TRANSITION:
3330 return JSObject::AddDataProperty(&own_lookup, value, NONE, language_mode,
3335 MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it,
3336 LanguageMode language_mode) {
3337 if (is_strong(language_mode)) {
3338 THROW_NEW_ERROR(it->isolate(),
3339 NewTypeError(MessageTemplate::kStrongPropertyAccess,
3340 it->GetName(), it->GetReceiver()),
3343 return it->isolate()->factory()->undefined_value();
3346 MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate,
3347 Handle<Object> receiver,
3348 Handle<Object> name,
3349 LanguageMode language_mode) {
3350 if (is_strong(language_mode)) {
3353 NewTypeError(MessageTemplate::kStrongPropertyAccess, name, receiver),
3356 return isolate->factory()->undefined_value();
3360 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3361 LookupIterator* it, Handle<Object> value, LanguageMode language_mode) {
3362 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3363 it->GetName(), value, language_mode);
3367 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3368 Isolate* isolate, Handle<Object> receiver, Handle<Object> name,
3369 Handle<Object> value, LanguageMode language_mode) {
3370 if (is_sloppy(language_mode)) return value;
3373 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name, receiver),
3378 MaybeHandle<Object> Object::RedefineNonconfigurableProperty(
3379 Isolate* isolate, Handle<Object> name, Handle<Object> value,
3380 LanguageMode language_mode) {
3381 if (is_sloppy(language_mode)) return value;
3382 THROW_NEW_ERROR(isolate,
3383 NewTypeError(MessageTemplate::kRedefineDisallowed, name),
3388 MaybeHandle<Object> Object::SetDataProperty(LookupIterator* it,
3389 Handle<Object> value) {
3390 // Proxies are handled on the WithHandler path. Other non-JSObjects cannot
3391 // have own properties.
3392 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
3394 // Store on the holder which may be hidden behind the receiver.
3395 DCHECK(it->HolderIsReceiverOrHiddenPrototype());
3397 // Old value for the observation change record.
3398 // Fetch before transforming the object since the encoding may become
3399 // incompatible with what's cached in |it|.
3400 bool is_observed = receiver->map()->is_observed() &&
3402 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
3403 MaybeHandle<Object> maybe_old;
3404 if (is_observed) maybe_old = it->GetDataValue();
3406 Handle<Object> to_assign = value;
3407 // Convert the incoming value to a number for storing into typed arrays.
3408 if (it->IsElement() && receiver->HasFixedTypedArrayElements()) {
3409 if (!value->IsNumber() && !value->IsUndefined()) {
3410 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), to_assign,
3411 Object::ToNumber(it->isolate(), value),
3413 // ToNumber above might modify the receiver, causing the cached
3414 // holder_map to mismatch the actual holder->map() after this point.
3415 // Reload the map to be in consistent state. Other cached state cannot
3416 // have been invalidated since typed array elements cannot be reconfigured
3418 it->ReloadHolderMap();
3420 // We have to recheck the length. However, it can only change if the
3421 // underlying buffer was neutered, so just check that.
3422 if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) {
3428 // Possibly migrate to the most up-to-date map that will be able to store
3429 // |value| under it->name().
3430 it->PrepareForDataProperty(to_assign);
3432 // Write the property value.
3433 it->WriteDataValue(to_assign);
3435 // Send the change record if there are observers.
3436 if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) {
3437 RETURN_ON_EXCEPTION(it->isolate(), JSObject::EnqueueChangeRecord(
3438 receiver, "update", it->GetName(),
3439 maybe_old.ToHandleChecked()),
3447 MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice(
3448 Handle<JSArray> object) {
3449 Isolate* isolate = object->GetIsolate();
3450 HandleScope scope(isolate);
3451 Handle<Object> args[] = {object};
3453 return Execution::Call(
3454 isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()),
3455 isolate->factory()->undefined_value(), arraysize(args), args);
3459 MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice(
3460 Handle<JSArray> object) {
3461 Isolate* isolate = object->GetIsolate();
3462 HandleScope scope(isolate);
3463 Handle<Object> args[] = {object};
3465 return Execution::Call(
3466 isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()),
3467 isolate->factory()->undefined_value(), arraysize(args), args);
3471 MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord(
3472 Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted,
3473 uint32_t add_count) {
3474 Isolate* isolate = object->GetIsolate();
3475 HandleScope scope(isolate);
3476 Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index);
3477 Handle<Object> add_count_object =
3478 isolate->factory()->NewNumberFromUint(add_count);
3480 Handle<Object> args[] = {object, index_object, deleted, add_count_object};
3482 return Execution::Call(
3483 isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()),
3484 isolate->factory()->undefined_value(), arraysize(args), args);
3488 MaybeHandle<Object> Object::AddDataProperty(LookupIterator* it,
3489 Handle<Object> value,
3490 PropertyAttributes attributes,
3491 LanguageMode language_mode,
3492 StoreFromKeyed store_mode) {
3493 DCHECK(!it->GetReceiver()->IsJSProxy());
3494 if (!it->GetReceiver()->IsJSObject()) {
3495 // TODO(verwaest): Throw a TypeError with a more specific message.
3496 return WriteToReadOnlyProperty(it, value, language_mode);
3499 DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state());
3501 Handle<JSObject> receiver = it->GetStoreTarget();
3503 // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject)
3504 // instead. If the prototype is Null, the proxy is detached.
3505 if (receiver->IsJSGlobalProxy()) return value;
3507 Isolate* isolate = it->isolate();
3509 if (!receiver->map()->is_extensible() &&
3510 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()))) {
3511 if (is_sloppy(language_mode)) return value;
3512 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kObjectNotExtensible,
3517 if (it->IsElement()) {
3518 if (receiver->IsJSArray()) {
3519 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
3520 if (JSArray::WouldChangeReadOnlyLength(array, it->index())) {
3521 if (is_sloppy(language_mode)) return value;
3522 return JSArray::ReadOnlyLengthError(array);
3525 if (FLAG_trace_external_array_abuse &&
3526 array->HasFixedTypedArrayElements()) {
3527 CheckArrayAbuse(array, "typed elements write", it->index(), true);
3530 if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) {
3531 CheckArrayAbuse(array, "elements write", it->index(), false);
3535 MaybeHandle<Object> result =
3536 JSObject::AddDataElement(receiver, it->index(), value, attributes);
3537 JSObject::ValidateElements(receiver);
3540 // Migrate to the most up-to-date map that will be able to store |value|
3541 // under it->name() with |attributes|.
3542 it->PrepareTransitionToDataProperty(value, attributes, store_mode);
3543 DCHECK_EQ(LookupIterator::TRANSITION, it->state());
3544 it->ApplyTransitionToDataProperty();
3546 // TODO(verwaest): Encapsulate dictionary handling better.
3547 if (receiver->map()->is_dictionary_map()) {
3548 // TODO(verwaest): Probably should ensure this is done beforehand.
3549 it->InternalizeName();
3550 // TODO(dcarney): just populate TransitionPropertyCell here?
3551 JSObject::AddSlowProperty(receiver, it->name(), value, attributes);
3553 // Write the property value.
3554 it->WriteDataValue(value);
3557 // Send the change record if there are observers.
3558 if (receiver->map()->is_observed() &&
3559 !isolate->IsInternallyUsedPropertyName(it->name())) {
3560 RETURN_ON_EXCEPTION(isolate, JSObject::EnqueueChangeRecord(
3561 receiver, "add", it->name(),
3562 it->factory()->the_hole_value()),
3571 void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) {
3572 // Only supports adding slack to owned descriptors.
3573 DCHECK(map->owns_descriptors());
3575 Handle<DescriptorArray> descriptors(map->instance_descriptors());
3576 int old_size = map->NumberOfOwnDescriptors();
3577 if (slack <= descriptors->NumberOfSlackDescriptors()) return;
3579 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
3580 descriptors, old_size, slack);
3582 DisallowHeapAllocation no_allocation;
3583 // The descriptors are still the same, so keep the layout descriptor.
3584 LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor();
3586 if (old_size == 0) {
3587 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3591 // If the source descriptors had an enum cache we copy it. This ensures
3592 // that the maps to which we push the new descriptor array back can rely
3593 // on a cache always being available once it is set. If the map has more
3594 // enumerated descriptors than available in the original cache, the cache
3595 // will be lazily replaced by the extended cache when needed.
3596 if (descriptors->HasEnumCache()) {
3597 new_descriptors->CopyEnumCacheFrom(*descriptors);
3600 // Replace descriptors by new_descriptors in all maps that share it.
3601 map->GetHeap()->incremental_marking()->RecordWrites(*descriptors);
3604 for (Object* current = map->GetBackPointer();
3605 !current->IsUndefined();
3606 current = walk_map->GetBackPointer()) {
3607 walk_map = Map::cast(current);
3608 if (walk_map->instance_descriptors() != *descriptors) break;
3609 walk_map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3612 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3617 static int AppendUniqueCallbacks(NeanderArray* callbacks,
3618 Handle<typename T::Array> array,
3619 int valid_descriptors) {
3620 int nof_callbacks = callbacks->length();
3622 Isolate* isolate = array->GetIsolate();
3623 // Ensure the keys are unique names before writing them into the
3624 // instance descriptor. Since it may cause a GC, it has to be done before we
3625 // temporarily put the heap in an invalid state while appending descriptors.
3626 for (int i = 0; i < nof_callbacks; ++i) {
3627 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3628 if (entry->name()->IsUniqueName()) continue;
3629 Handle<String> key =
3630 isolate->factory()->InternalizeString(
3631 Handle<String>(String::cast(entry->name())));
3632 entry->set_name(*key);
3635 // Fill in new callback descriptors. Process the callbacks from
3636 // back to front so that the last callback with a given name takes
3637 // precedence over previously added callbacks with that name.
3638 for (int i = nof_callbacks - 1; i >= 0; i--) {
3639 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3640 Handle<Name> key(Name::cast(entry->name()));
3641 // Check if a descriptor with this name already exists before writing.
3642 if (!T::Contains(key, entry, valid_descriptors, array)) {
3643 T::Insert(key, entry, valid_descriptors, array);
3644 valid_descriptors++;
3648 return valid_descriptors;
3651 struct DescriptorArrayAppender {
3652 typedef DescriptorArray Array;
3653 static bool Contains(Handle<Name> key,
3654 Handle<AccessorInfo> entry,
3655 int valid_descriptors,
3656 Handle<DescriptorArray> array) {
3657 DisallowHeapAllocation no_gc;
3658 return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound;
3660 static void Insert(Handle<Name> key,
3661 Handle<AccessorInfo> entry,
3662 int valid_descriptors,
3663 Handle<DescriptorArray> array) {
3664 DisallowHeapAllocation no_gc;
3665 AccessorConstantDescriptor desc(key, entry, entry->property_attributes());
3666 array->Append(&desc);
3671 struct FixedArrayAppender {
3672 typedef FixedArray Array;
3673 static bool Contains(Handle<Name> key,
3674 Handle<AccessorInfo> entry,
3675 int valid_descriptors,
3676 Handle<FixedArray> array) {
3677 for (int i = 0; i < valid_descriptors; i++) {
3678 if (*key == AccessorInfo::cast(array->get(i))->name()) return true;
3682 static void Insert(Handle<Name> key,
3683 Handle<AccessorInfo> entry,
3684 int valid_descriptors,
3685 Handle<FixedArray> array) {
3686 DisallowHeapAllocation no_gc;
3687 array->set(valid_descriptors, *entry);
3692 void Map::AppendCallbackDescriptors(Handle<Map> map,
3693 Handle<Object> descriptors) {
3694 int nof = map->NumberOfOwnDescriptors();
3695 Handle<DescriptorArray> array(map->instance_descriptors());
3696 NeanderArray callbacks(descriptors);
3697 DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length());
3698 nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof);
3699 map->SetNumberOfOwnDescriptors(nof);
3703 int AccessorInfo::AppendUnique(Handle<Object> descriptors,
3704 Handle<FixedArray> array,
3705 int valid_descriptors) {
3706 NeanderArray callbacks(descriptors);
3707 DCHECK(array->length() >= callbacks.length() + valid_descriptors);
3708 return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks,
3714 static bool ContainsMap(MapHandleList* maps, Map* map) {
3715 DCHECK_NOT_NULL(map);
3716 for (int i = 0; i < maps->length(); ++i) {
3717 if (!maps->at(i).is_null() && *maps->at(i) == map) return true;
3723 Handle<Map> Map::FindTransitionedMap(Handle<Map> map,
3724 MapHandleList* candidates) {
3725 ElementsKind kind = map->elements_kind();
3726 bool packed = IsFastPackedElementsKind(kind);
3728 Map* transition = nullptr;
3729 if (IsTransitionableFastElementsKind(kind)) {
3730 for (Map* current = map->ElementsTransitionMap();
3731 current != nullptr && current->has_fast_elements();
3732 current = current->ElementsTransitionMap()) {
3733 if (ContainsMap(candidates, current) &&
3734 (packed || !IsFastPackedElementsKind(current->elements_kind()))) {
3735 transition = current;
3736 packed = packed && IsFastPackedElementsKind(current->elements_kind());
3740 return transition == nullptr ? Handle<Map>() : handle(transition);
3744 static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) {
3745 Map* current_map = map;
3747 ElementsKind kind = map->elements_kind();
3748 while (kind != to_kind) {
3749 Map* next_map = current_map->ElementsTransitionMap();
3750 if (next_map == nullptr) return current_map;
3751 kind = next_map->elements_kind();
3752 current_map = next_map;
3755 DCHECK_EQ(to_kind, current_map->elements_kind());
3760 Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) {
3761 Map* to_map = FindClosestElementsTransition(this, to_kind);
3762 if (to_map->elements_kind() == to_kind) return to_map;
3767 bool Map::IsMapInArrayPrototypeChain() {
3768 Isolate* isolate = GetIsolate();
3769 if (isolate->initial_array_prototype()->map() == this) {
3773 if (isolate->initial_object_prototype()->map() == this) {
3781 Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) {
3782 Isolate* isolate = map->GetIsolate();
3783 if (map->weak_cell_cache()->IsWeakCell()) {
3784 return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache()));
3786 Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map);
3787 map->set_weak_cell_cache(*weak_cell);
3792 static Handle<Map> AddMissingElementsTransitions(Handle<Map> map,
3793 ElementsKind to_kind) {
3794 DCHECK(IsTransitionElementsKind(map->elements_kind()));
3796 Handle<Map> current_map = map;
3798 ElementsKind kind = map->elements_kind();
3799 TransitionFlag flag;
3800 if (map->is_prototype_map()) {
3801 flag = OMIT_TRANSITION;
3803 flag = INSERT_TRANSITION;
3804 if (IsFastElementsKind(kind)) {
3805 while (kind != to_kind && !IsTerminalElementsKind(kind)) {
3806 kind = GetNextTransitionElementsKind(kind);
3807 current_map = Map::CopyAsElementsKind(current_map, kind, flag);
3812 // In case we are exiting the fast elements kind system, just add the map in
3814 if (kind != to_kind) {
3815 current_map = Map::CopyAsElementsKind(current_map, to_kind, flag);
3818 DCHECK(current_map->elements_kind() == to_kind);
3823 Handle<Map> Map::TransitionElementsTo(Handle<Map> map,
3824 ElementsKind to_kind) {
3825 ElementsKind from_kind = map->elements_kind();
3826 if (from_kind == to_kind) return map;
3828 Isolate* isolate = map->GetIsolate();
3829 Context* native_context = isolate->context()->native_context();
3830 if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) {
3831 if (*map == native_context->fast_aliased_arguments_map()) {
3832 DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3833 return handle(native_context->slow_aliased_arguments_map());
3835 } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) {
3836 if (*map == native_context->slow_aliased_arguments_map()) {
3837 DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3838 return handle(native_context->fast_aliased_arguments_map());
3841 Object* maybe_array_maps = map->is_strong()
3842 ? native_context->js_array_strong_maps()
3843 : native_context->js_array_maps();
3844 if (maybe_array_maps->IsFixedArray()) {
3845 DisallowHeapAllocation no_gc;
3846 FixedArray* array_maps = FixedArray::cast(maybe_array_maps);
3847 if (array_maps->get(from_kind) == *map) {
3848 Object* maybe_transitioned_map = array_maps->get(to_kind);
3849 if (maybe_transitioned_map->IsMap()) {
3850 return handle(Map::cast(maybe_transitioned_map));
3856 DCHECK(!map->IsUndefined());
3857 bool allow_store_transition = IsTransitionElementsKind(from_kind);
3858 // Only store fast element maps in ascending generality.
3859 if (IsFastElementsKind(to_kind)) {
3860 allow_store_transition =
3861 allow_store_transition && IsTransitionableFastElementsKind(from_kind) &&
3862 IsMoreGeneralElementsKindTransition(from_kind, to_kind);
3865 if (!allow_store_transition) {
3866 return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION);
3869 return Map::AsElementsKind(map, to_kind);
3874 Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) {
3875 Handle<Map> closest_map(FindClosestElementsTransition(*map, kind));
3877 if (closest_map->elements_kind() == kind) {
3881 return AddMissingElementsTransitions(closest_map, kind);
3885 Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
3886 ElementsKind to_kind) {
3887 Handle<Map> map(object->map());
3888 return Map::TransitionElementsTo(map, to_kind);
3892 Maybe<bool> JSProxy::HasPropertyWithHandler(Handle<JSProxy> proxy,
3893 Handle<Name> name) {
3894 Isolate* isolate = proxy->GetIsolate();
3896 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3897 if (name->IsSymbol()) return Just(false);
3899 Handle<Object> args[] = { name };
3900 Handle<Object> result;
3901 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3902 isolate, result, CallTrap(proxy, "has", isolate->derived_has_trap(),
3903 arraysize(args), args),
3906 return Just(result->BooleanValue());
3910 MaybeHandle<Object> JSProxy::SetPropertyWithHandler(
3911 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3912 Handle<Object> value, LanguageMode language_mode) {
3913 Isolate* isolate = proxy->GetIsolate();
3915 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3916 if (name->IsSymbol()) return value;
3918 Handle<Object> args[] = { receiver, name, value };
3919 RETURN_ON_EXCEPTION(
3923 isolate->derived_set_trap(),
3932 MaybeHandle<Object> JSProxy::SetPropertyViaPrototypesWithHandler(
3933 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3934 Handle<Object> value, LanguageMode language_mode, bool* done) {
3935 Isolate* isolate = proxy->GetIsolate();
3936 Handle<Object> handler(proxy->handler(), isolate); // Trap might morph proxy.
3938 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3939 if (name->IsSymbol()) {
3941 return isolate->factory()->the_hole_value();
3944 *done = true; // except where redefined...
3945 Handle<Object> args[] = { name };
3946 Handle<Object> result;
3947 ASSIGN_RETURN_ON_EXCEPTION(
3950 "getPropertyDescriptor",
3956 if (result->IsUndefined()) {
3958 return isolate->factory()->the_hole_value();
3961 // Emulate [[GetProperty]] semantics for proxies.
3962 Handle<Object> argv[] = { result };
3963 Handle<Object> desc;
3964 ASSIGN_RETURN_ON_EXCEPTION(
3966 Execution::Call(isolate,
3967 isolate->to_complete_property_descriptor(),
3973 // [[GetProperty]] requires to check that all properties are configurable.
3974 Handle<String> configurable_name =
3975 isolate->factory()->InternalizeOneByteString(
3976 STATIC_CHAR_VECTOR("configurable_"));
3977 Handle<Object> configurable =
3978 Object::GetProperty(desc, configurable_name).ToHandleChecked();
3979 DCHECK(configurable->IsBoolean());
3980 if (configurable->IsFalse()) {
3981 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
3982 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
3983 THROW_NEW_ERROR(isolate,
3984 NewTypeError(MessageTemplate::kProxyPropNotConfigurable,
3985 handler, name, trap),
3988 DCHECK(configurable->IsTrue());
3990 // Check for DataDescriptor.
3991 Handle<String> hasWritable_name =
3992 isolate->factory()->InternalizeOneByteString(
3993 STATIC_CHAR_VECTOR("hasWritable_"));
3994 Handle<Object> hasWritable =
3995 Object::GetProperty(desc, hasWritable_name).ToHandleChecked();
3996 DCHECK(hasWritable->IsBoolean());
3997 if (hasWritable->IsTrue()) {
3998 Handle<String> writable_name = isolate->factory()->InternalizeOneByteString(
3999 STATIC_CHAR_VECTOR("writable_"));
4000 Handle<Object> writable =
4001 Object::GetProperty(desc, writable_name).ToHandleChecked();
4002 DCHECK(writable->IsBoolean());
4003 *done = writable->IsFalse();
4004 if (!*done) return isolate->factory()->the_hole_value();
4005 return WriteToReadOnlyProperty(isolate, receiver, name, value,
4009 // We have an AccessorDescriptor.
4010 Handle<String> set_name =
4011 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("set_"));
4012 Handle<Object> setter = Object::GetProperty(desc, set_name).ToHandleChecked();
4013 if (!setter->IsUndefined()) {
4014 // TODO(rossberg): nicer would be to cast to some JSCallable here...
4015 return SetPropertyWithDefinedSetter(
4016 receiver, Handle<JSReceiver>::cast(setter), value);
4019 if (is_sloppy(language_mode)) return value;
4021 isolate, NewTypeError(MessageTemplate::kNoSetterInCallback, name, proxy),
4026 MaybeHandle<Object> JSProxy::DeletePropertyWithHandler(
4027 Handle<JSProxy> proxy, Handle<Name> name, LanguageMode language_mode) {
4028 Isolate* isolate = proxy->GetIsolate();
4030 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
4031 if (name->IsSymbol()) return isolate->factory()->false_value();
4033 Handle<Object> args[] = { name };
4034 Handle<Object> result;
4035 ASSIGN_RETURN_ON_EXCEPTION(
4044 bool result_bool = result->BooleanValue();
4045 if (is_strict(language_mode) && !result_bool) {
4046 Handle<Object> handler(proxy->handler(), isolate);
4049 NewTypeError(MessageTemplate::kProxyHandlerDeleteFailed, handler),
4052 return isolate->factory()->ToBoolean(result_bool);
4056 Maybe<PropertyAttributes> JSProxy::GetPropertyAttributesWithHandler(
4057 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name) {
4058 Isolate* isolate = proxy->GetIsolate();
4059 HandleScope scope(isolate);
4061 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
4062 if (name->IsSymbol()) return Just(ABSENT);
4064 Handle<Object> args[] = { name };
4065 Handle<Object> result;
4066 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
4067 isolate, result, proxy->CallTrap(proxy, "getPropertyDescriptor",
4068 Handle<Object>(), arraysize(args), args),
4069 Nothing<PropertyAttributes>());
4071 if (result->IsUndefined()) return Just(ABSENT);
4073 Handle<Object> argv[] = { result };
4074 Handle<Object> desc;
4075 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
4077 Execution::Call(isolate, isolate->to_complete_property_descriptor(),
4078 result, arraysize(argv), argv),
4079 Nothing<PropertyAttributes>());
4081 // Convert result to PropertyAttributes.
4082 Handle<String> enum_n = isolate->factory()->InternalizeOneByteString(
4083 STATIC_CHAR_VECTOR("enumerable_"));
4084 Handle<Object> enumerable;
4085 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, enumerable,
4086 Object::GetProperty(desc, enum_n),
4087 Nothing<PropertyAttributes>());
4088 Handle<String> conf_n = isolate->factory()->InternalizeOneByteString(
4089 STATIC_CHAR_VECTOR("configurable_"));
4090 Handle<Object> configurable;
4091 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, configurable,
4092 Object::GetProperty(desc, conf_n),
4093 Nothing<PropertyAttributes>());
4094 Handle<String> writ_n = isolate->factory()->InternalizeOneByteString(
4095 STATIC_CHAR_VECTOR("writable_"));
4096 Handle<Object> writable;
4097 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, writable,
4098 Object::GetProperty(desc, writ_n),
4099 Nothing<PropertyAttributes>());
4100 if (!writable->BooleanValue()) {
4101 Handle<String> set_n = isolate->factory()->InternalizeOneByteString(
4102 STATIC_CHAR_VECTOR("set_"));
4103 Handle<Object> setter;
4104 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, setter,
4105 Object::GetProperty(desc, set_n),
4106 Nothing<PropertyAttributes>());
4107 writable = isolate->factory()->ToBoolean(!setter->IsUndefined());
4110 if (configurable->IsFalse()) {
4111 Handle<Object> handler(proxy->handler(), isolate);
4112 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
4113 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
4114 Handle<Object> error = isolate->factory()->NewTypeError(
4115 MessageTemplate::kProxyPropNotConfigurable, handler, name, trap);
4116 isolate->Throw(*error);
4117 return Nothing<PropertyAttributes>();
4120 int attributes = NONE;
4121 if (!enumerable->BooleanValue()) attributes |= DONT_ENUM;
4122 if (!configurable->BooleanValue()) attributes |= DONT_DELETE;
4123 if (!writable->BooleanValue()) attributes |= READ_ONLY;
4124 return Just(static_cast<PropertyAttributes>(attributes));
4128 void JSProxy::Fix(Handle<JSProxy> proxy) {
4129 Isolate* isolate = proxy->GetIsolate();
4131 // Save identity hash.
4132 Handle<Object> hash(proxy->GetIdentityHash(), isolate);
4134 if (proxy->IsJSFunctionProxy()) {
4135 isolate->factory()->BecomeJSFunction(proxy);
4136 // Code will be set on the JavaScript side.
4138 isolate->factory()->BecomeJSObject(proxy);
4140 DCHECK(proxy->IsJSObject());
4142 // Inherit identity, if it was present.
4143 if (hash->IsSmi()) {
4144 JSObject::SetIdentityHash(Handle<JSObject>::cast(proxy),
4145 Handle<Smi>::cast(hash));
4150 MaybeHandle<Object> JSProxy::CallTrap(Handle<JSProxy> proxy,
4152 Handle<Object> derived,
4154 Handle<Object> argv[]) {
4155 Isolate* isolate = proxy->GetIsolate();
4156 Handle<Object> handler(proxy->handler(), isolate);
4158 Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name);
4159 Handle<Object> trap;
4160 ASSIGN_RETURN_ON_EXCEPTION(
4162 Object::GetPropertyOrElement(handler, trap_name),
4165 if (trap->IsUndefined()) {
4166 if (derived.is_null()) {
4167 THROW_NEW_ERROR(isolate,
4168 NewTypeError(MessageTemplate::kProxyHandlerTrapMissing,
4169 handler, trap_name),
4172 trap = Handle<Object>(derived);
4175 return Execution::Call(isolate, trap, handler, argc, argv);
4179 void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) {
4180 DCHECK(object->map()->GetInObjectProperties() ==
4181 map->GetInObjectProperties());
4182 ElementsKind obj_kind = object->map()->elements_kind();
4183 ElementsKind map_kind = map->elements_kind();
4184 if (map_kind != obj_kind) {
4185 ElementsKind to_kind = map_kind;
4186 if (IsMoreGeneralElementsKindTransition(map_kind, obj_kind) ||
4187 IsDictionaryElementsKind(obj_kind)) {
4190 if (IsDictionaryElementsKind(to_kind)) {
4191 NormalizeElements(object);
4193 TransitionElementsKind(object, to_kind);
4195 map = Map::AsElementsKind(map, to_kind);
4197 JSObject::MigrateToMap(object, map);
4201 void JSObject::MigrateInstance(Handle<JSObject> object) {
4202 Handle<Map> original_map(object->map());
4203 Handle<Map> map = Map::Update(original_map);
4204 map->set_migration_target(true);
4205 MigrateToMap(object, map);
4206 if (FLAG_trace_migration) {
4207 object->PrintInstanceMigration(stdout, *original_map, *map);
4213 bool JSObject::TryMigrateInstance(Handle<JSObject> object) {
4214 Isolate* isolate = object->GetIsolate();
4215 DisallowDeoptimization no_deoptimization(isolate);
4216 Handle<Map> original_map(object->map(), isolate);
4217 Handle<Map> new_map;
4218 if (!Map::TryUpdate(original_map).ToHandle(&new_map)) {
4221 JSObject::MigrateToMap(object, new_map);
4222 if (FLAG_trace_migration) {
4223 object->PrintInstanceMigration(stdout, *original_map, object->map());
4229 void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name,
4230 Handle<Object> value,
4231 PropertyAttributes attributes) {
4232 LookupIterator it(object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
4233 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
4236 DCHECK(!object->IsJSProxy());
4237 DCHECK(!name->AsArrayIndex(&index));
4238 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
4239 DCHECK(maybe.IsJust());
4240 DCHECK(!it.IsFound());
4241 DCHECK(object->map()->is_extensible() ||
4242 it.isolate()->IsInternallyUsedPropertyName(name));
4244 AddDataProperty(&it, value, attributes, STRICT,
4245 CERTAINLY_NOT_STORE_FROM_KEYED).Check();
4250 void ExecutableAccessorInfo::ClearSetter(Handle<ExecutableAccessorInfo> info) {
4251 Handle<Object> object = v8::FromCData(info->GetIsolate(), nullptr);
4252 info->set_setter(*object);
4256 // Reconfigures a property to a data property with attributes, even if it is not
4258 // Requires a LookupIterator that does not look at the prototype chain beyond
4259 // hidden prototypes.
4260 MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes(
4261 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
4262 ExecutableAccessorInfoHandling handling) {
4263 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
4264 bool is_observed = object->map()->is_observed() &&
4266 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
4268 for (; it->IsFound(); it->Next()) {
4269 switch (it->state()) {
4270 case LookupIterator::JSPROXY:
4271 case LookupIterator::NOT_FOUND:
4272 case LookupIterator::TRANSITION:
4275 case LookupIterator::ACCESS_CHECK:
4276 if (!it->HasAccess()) {
4277 it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>());
4278 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
4283 // If there's an interceptor, try to store the property with the
4285 // In case of success, the attributes will have been reset to the default
4286 // attributes of the interceptor, rather than the incoming attributes.
4288 // TODO(verwaest): JSProxy afterwards verify the attributes that the
4289 // JSProxy claims it has, and verifies that they are compatible. If not,
4290 // they throw. Here we should do the same.
4291 case LookupIterator::INTERCEPTOR:
4292 if (handling == DONT_FORCE_FIELD) {
4293 MaybeHandle<Object> maybe_result =
4294 JSObject::SetPropertyWithInterceptor(it, value);
4295 if (!maybe_result.is_null()) return maybe_result;
4296 if (it->isolate()->has_pending_exception()) return maybe_result;
4300 case LookupIterator::ACCESSOR: {
4301 Handle<Object> accessors = it->GetAccessors();
4303 // Special handling for ExecutableAccessorInfo, which behaves like a
4305 if (accessors->IsExecutableAccessorInfo() &&
4306 handling == DONT_FORCE_FIELD) {
4307 PropertyDetails details = it->property_details();
4308 // Ensure the context isn't changed after calling into accessors.
4309 AssertNoContextChange ncc(it->isolate());
4311 Handle<Object> result;
4312 ASSIGN_RETURN_ON_EXCEPTION(
4313 it->isolate(), result,
4314 JSObject::SetPropertyWithAccessor(it, value, STRICT), Object);
4315 DCHECK(result->SameValue(*value));
4317 if (details.attributes() == attributes) return value;
4319 // Reconfigure the accessor if attributes mismatch.
4320 Handle<ExecutableAccessorInfo> new_data = Accessors::CloneAccessor(
4321 it->isolate(), Handle<ExecutableAccessorInfo>::cast(accessors));
4322 new_data->set_property_attributes(attributes);
4323 // By clearing the setter we don't have to introduce a lookup to
4324 // the setter, simply make it unavailable to reflect the
4326 if (attributes & READ_ONLY) {
4327 ExecutableAccessorInfo::ClearSetter(new_data);
4330 it->TransitionToAccessorPair(new_data, attributes);
4332 it->ReconfigureDataProperty(value, attributes);
4333 it->WriteDataValue(value);
4337 RETURN_ON_EXCEPTION(
4339 EnqueueChangeRecord(object, "reconfigure", it->GetName(),
4340 it->factory()->the_hole_value()),
4346 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4347 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4350 case LookupIterator::DATA: {
4351 PropertyDetails details = it->property_details();
4352 Handle<Object> old_value = it->factory()->the_hole_value();
4353 // Regular property update if the attributes match.
4354 if (details.attributes() == attributes) {
4355 return SetDataProperty(it, value);
4358 // Special case: properties of typed arrays cannot be reconfigured to
4359 // non-writable nor to non-enumerable.
4360 if (it->IsElement() && object->HasFixedTypedArrayElements()) {
4361 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4365 // Reconfigure the data property if the attributes mismatch.
4366 if (is_observed) old_value = it->GetDataValue();
4368 it->ReconfigureDataProperty(value, attributes);
4369 it->WriteDataValue(value);
4372 if (old_value->SameValue(*value)) {
4373 old_value = it->factory()->the_hole_value();
4375 RETURN_ON_EXCEPTION(it->isolate(),
4376 EnqueueChangeRecord(object, "reconfigure",
4377 it->GetName(), old_value),
4385 return AddDataProperty(it, value, attributes, STRICT,
4386 CERTAINLY_NOT_STORE_FROM_KEYED);
4390 MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes(
4391 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4392 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4393 DCHECK(!value->IsTheHole());
4394 LookupIterator it(object, name, LookupIterator::OWN);
4395 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4399 MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes(
4400 Handle<JSObject> object, uint32_t index, Handle<Object> value,
4401 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4402 Isolate* isolate = object->GetIsolate();
4403 LookupIterator it(isolate, object, index, LookupIterator::OWN);
4404 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4408 MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes(
4409 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4410 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4411 Isolate* isolate = object->GetIsolate();
4412 LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name,
4413 LookupIterator::OWN);
4414 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4418 Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it,
4419 Handle<Object> value) {
4420 DCHECK(it->GetReceiver()->IsJSObject());
4421 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(it);
4422 if (maybe.IsNothing()) return Nothing<bool>();
4424 if (it->IsFound()) {
4425 if (!it->IsConfigurable()) return Just(false);
4427 if (!JSObject::cast(*it->GetReceiver())->IsExtensible()) return Just(false);
4430 RETURN_ON_EXCEPTION_VALUE(
4432 DefineOwnPropertyIgnoreAttributes(it, value, NONE, DONT_FORCE_FIELD),
4439 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor(
4440 LookupIterator* it) {
4441 Isolate* isolate = it->isolate();
4442 // Make sure that the top context does not change when doing
4443 // callbacks or interceptor calls.
4444 AssertNoContextChange ncc(isolate);
4445 HandleScope scope(isolate);
4447 Handle<JSObject> holder = it->GetHolder<JSObject>();
4448 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
4449 if (!it->IsElement() && it->name()->IsSymbol() &&
4450 !interceptor->can_intercept_symbols()) {
4451 return Just(ABSENT);
4453 PropertyCallbackArguments args(isolate, interceptor->data(),
4454 *it->GetReceiver(), *holder);
4455 if (!interceptor->query()->IsUndefined()) {
4456 v8::Local<v8::Integer> result;
4457 if (it->IsElement()) {
4458 uint32_t index = it->index();
4459 v8::IndexedPropertyQueryCallback query =
4460 v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query());
4462 ApiIndexedPropertyAccess("interceptor-indexed-has", *holder, index));
4463 result = args.Call(query, index);
4465 Handle<Name> name = it->name();
4466 v8::GenericNamedPropertyQueryCallback query =
4467 v8::ToCData<v8::GenericNamedPropertyQueryCallback>(
4468 interceptor->query());
4470 ApiNamedPropertyAccess("interceptor-named-has", *holder, *name));
4471 result = args.Call(query, v8::Utils::ToLocal(name));
4473 if (!result.IsEmpty()) {
4474 DCHECK(result->IsInt32());
4475 return Just(static_cast<PropertyAttributes>(
4476 result->Int32Value(reinterpret_cast<v8::Isolate*>(isolate)
4477 ->GetCurrentContext()).FromJust()));
4479 } else if (!interceptor->getter()->IsUndefined()) {
4480 // TODO(verwaest): Use GetPropertyWithInterceptor?
4481 v8::Local<v8::Value> result;
4482 if (it->IsElement()) {
4483 uint32_t index = it->index();
4484 v8::IndexedPropertyGetterCallback getter =
4485 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
4486 LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-get-has",
4488 result = args.Call(getter, index);
4490 Handle<Name> name = it->name();
4492 v8::GenericNamedPropertyGetterCallback getter =
4493 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
4494 interceptor->getter());
4496 ApiNamedPropertyAccess("interceptor-named-get-has", *holder, *name));
4497 result = args.Call(getter, v8::Utils::ToLocal(name));
4499 if (!result.IsEmpty()) return Just(DONT_ENUM);
4502 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>());
4503 return Just(ABSENT);
4507 Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes(
4508 LookupIterator* it) {
4509 for (; it->IsFound(); it->Next()) {
4510 switch (it->state()) {
4511 case LookupIterator::NOT_FOUND:
4512 case LookupIterator::TRANSITION:
4514 case LookupIterator::JSPROXY:
4515 return JSProxy::GetPropertyAttributesWithHandler(
4516 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
4517 case LookupIterator::INTERCEPTOR: {
4518 Maybe<PropertyAttributes> result =
4519 JSObject::GetPropertyAttributesWithInterceptor(it);
4520 if (!result.IsJust()) return result;
4521 if (result.FromJust() != ABSENT) return result;
4524 case LookupIterator::ACCESS_CHECK:
4525 if (it->HasAccess()) break;
4526 return JSObject::GetPropertyAttributesWithFailedAccessCheck(it);
4527 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4528 return Just(ABSENT);
4529 case LookupIterator::ACCESSOR:
4530 case LookupIterator::DATA:
4531 return Just(it->property_details().attributes());
4534 return Just(ABSENT);
4538 Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) {
4539 Handle<FixedArray> array(
4540 isolate->factory()->NewFixedArray(kEntries, TENURED));
4541 return Handle<NormalizedMapCache>::cast(array);
4545 MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map,
4546 PropertyNormalizationMode mode) {
4547 DisallowHeapAllocation no_gc;
4548 Object* value = FixedArray::get(GetIndex(fast_map));
4549 if (!value->IsMap() ||
4550 !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) {
4551 return MaybeHandle<Map>();
4553 return handle(Map::cast(value));
4557 void NormalizedMapCache::Set(Handle<Map> fast_map,
4558 Handle<Map> normalized_map) {
4559 DisallowHeapAllocation no_gc;
4560 DCHECK(normalized_map->is_dictionary_map());
4561 FixedArray::set(GetIndex(fast_map), *normalized_map);
4565 void NormalizedMapCache::Clear() {
4566 int entries = length();
4567 for (int i = 0; i != entries; i++) {
4573 void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object,
4575 Handle<Code> code) {
4576 Handle<Map> map(object->map());
4577 Map::UpdateCodeCache(map, name, code);
4581 void JSObject::NormalizeProperties(Handle<JSObject> object,
4582 PropertyNormalizationMode mode,
4583 int expected_additional_properties,
4584 const char* reason) {
4585 if (!object->HasFastProperties()) return;
4587 Handle<Map> map(object->map());
4588 Handle<Map> new_map = Map::Normalize(map, mode, reason);
4590 MigrateToMap(object, new_map, expected_additional_properties);
4594 void JSObject::MigrateFastToSlow(Handle<JSObject> object,
4595 Handle<Map> new_map,
4596 int expected_additional_properties) {
4597 // The global object is always normalized.
4598 DCHECK(!object->IsGlobalObject());
4599 // JSGlobalProxy must never be normalized
4600 DCHECK(!object->IsJSGlobalProxy());
4602 Isolate* isolate = object->GetIsolate();
4603 HandleScope scope(isolate);
4604 Handle<Map> map(object->map());
4606 // Allocate new content.
4607 int real_size = map->NumberOfOwnDescriptors();
4608 int property_count = real_size;
4609 if (expected_additional_properties > 0) {
4610 property_count += expected_additional_properties;
4612 property_count += 2; // Make space for two more properties.
4614 Handle<NameDictionary> dictionary =
4615 NameDictionary::New(isolate, property_count);
4617 Handle<DescriptorArray> descs(map->instance_descriptors());
4618 for (int i = 0; i < real_size; i++) {
4619 PropertyDetails details = descs->GetDetails(i);
4620 Handle<Name> key(descs->GetKey(i));
4621 switch (details.type()) {
4622 case DATA_CONSTANT: {
4623 Handle<Object> value(descs->GetConstant(i), isolate);
4624 PropertyDetails d(details.attributes(), DATA, i + 1,
4625 PropertyCellType::kNoCell);
4626 dictionary = NameDictionary::Add(dictionary, key, value, d);
4630 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4631 Handle<Object> value;
4632 if (object->IsUnboxedDoubleField(index)) {
4633 double old_value = object->RawFastDoublePropertyAt(index);
4634 value = isolate->factory()->NewHeapNumber(old_value);
4636 value = handle(object->RawFastPropertyAt(index), isolate);
4637 if (details.representation().IsDouble()) {
4638 DCHECK(value->IsMutableHeapNumber());
4639 Handle<HeapNumber> old = Handle<HeapNumber>::cast(value);
4640 value = isolate->factory()->NewHeapNumber(old->value());
4643 PropertyDetails d(details.attributes(), DATA, i + 1,
4644 PropertyCellType::kNoCell);
4645 dictionary = NameDictionary::Add(dictionary, key, value, d);
4649 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4650 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
4651 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4652 PropertyCellType::kNoCell);
4653 dictionary = NameDictionary::Add(dictionary, key, value, d);
4656 case ACCESSOR_CONSTANT: {
4657 Handle<Object> value(descs->GetCallbacksObject(i), isolate);
4658 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4659 PropertyCellType::kNoCell);
4660 dictionary = NameDictionary::Add(dictionary, key, value, d);
4666 // Copy the next enumeration index from instance descriptor.
4667 dictionary->SetNextEnumerationIndex(real_size + 1);
4669 // From here on we cannot fail and we shouldn't GC anymore.
4670 DisallowHeapAllocation no_allocation;
4672 // Resize the object in the heap if necessary.
4673 int new_instance_size = new_map->instance_size();
4674 int instance_size_delta = map->instance_size() - new_instance_size;
4675 DCHECK(instance_size_delta >= 0);
4677 if (instance_size_delta > 0) {
4678 Heap* heap = isolate->heap();
4679 heap->CreateFillerObjectAt(object->address() + new_instance_size,
4680 instance_size_delta);
4681 heap->AdjustLiveBytes(*object, -instance_size_delta,
4682 Heap::CONCURRENT_TO_SWEEPER);
4685 // We are storing the new map using release store after creating a filler for
4686 // the left-over space to avoid races with the sweeper thread.
4687 object->synchronized_set_map(*new_map);
4689 object->set_properties(*dictionary);
4691 // Ensure that in-object space of slow-mode object does not contain random
4693 int inobject_properties = new_map->GetInObjectProperties();
4694 for (int i = 0; i < inobject_properties; i++) {
4695 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
4696 object->RawFastPropertyAtPut(index, Smi::FromInt(0));
4699 isolate->counters()->props_to_dictionary()->Increment();
4702 if (FLAG_trace_normalization) {
4703 OFStream os(stdout);
4704 os << "Object properties have been normalized:\n";
4711 void JSObject::MigrateSlowToFast(Handle<JSObject> object,
4712 int unused_property_fields,
4713 const char* reason) {
4714 if (object->HasFastProperties()) return;
4715 DCHECK(!object->IsGlobalObject());
4716 Isolate* isolate = object->GetIsolate();
4717 Factory* factory = isolate->factory();
4718 Handle<NameDictionary> dictionary(object->property_dictionary());
4720 // Make sure we preserve dictionary representation if there are too many
4722 int number_of_elements = dictionary->NumberOfElements();
4723 if (number_of_elements > kMaxNumberOfDescriptors) return;
4725 Handle<FixedArray> iteration_order;
4726 if (number_of_elements != dictionary->NextEnumerationIndex()) {
4728 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
4730 iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary);
4733 int instance_descriptor_length = iteration_order->length();
4734 int number_of_fields = 0;
4736 // Compute the length of the instance descriptor.
4737 for (int i = 0; i < instance_descriptor_length; i++) {
4738 int index = Smi::cast(iteration_order->get(i))->value();
4739 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
4741 Object* value = dictionary->ValueAt(index);
4742 PropertyType type = dictionary->DetailsAt(index).type();
4743 if (type == DATA && !value->IsJSFunction()) {
4744 number_of_fields += 1;
4748 Handle<Map> old_map(object->map(), isolate);
4750 int inobject_props = old_map->GetInObjectProperties();
4752 // Allocate new map.
4753 Handle<Map> new_map = Map::CopyDropDescriptors(old_map);
4754 new_map->set_dictionary_map(false);
4756 UpdatePrototypeUserRegistration(old_map, new_map, isolate);
4759 if (FLAG_trace_maps) {
4760 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n",
4761 reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map),
4766 if (instance_descriptor_length == 0) {
4767 DisallowHeapAllocation no_gc;
4768 DCHECK_LE(unused_property_fields, inobject_props);
4769 // Transform the object.
4770 new_map->set_unused_property_fields(inobject_props);
4771 object->synchronized_set_map(*new_map);
4772 object->set_properties(isolate->heap()->empty_fixed_array());
4773 // Check that it really works.
4774 DCHECK(object->HasFastProperties());
4778 // Allocate the instance descriptor.
4779 Handle<DescriptorArray> descriptors = DescriptorArray::Allocate(
4780 isolate, instance_descriptor_length);
4782 int number_of_allocated_fields =
4783 number_of_fields + unused_property_fields - inobject_props;
4784 if (number_of_allocated_fields < 0) {
4785 // There is enough inobject space for all fields (including unused).
4786 number_of_allocated_fields = 0;
4787 unused_property_fields = inobject_props - number_of_fields;
4790 // Allocate the fixed array for the fields.
4791 Handle<FixedArray> fields = factory->NewFixedArray(
4792 number_of_allocated_fields);
4794 // Fill in the instance descriptor and the fields.
4795 int current_offset = 0;
4796 for (int i = 0; i < instance_descriptor_length; i++) {
4797 int index = Smi::cast(iteration_order->get(i))->value();
4798 Object* k = dictionary->KeyAt(index);
4799 DCHECK(dictionary->IsKey(k));
4801 Object* value = dictionary->ValueAt(index);
4803 if (k->IsSymbol()) {
4804 key = handle(Symbol::cast(k));
4806 // Ensure the key is a unique name before writing into the
4807 // instance descriptor.
4808 key = factory->InternalizeString(handle(String::cast(k)));
4811 PropertyDetails details = dictionary->DetailsAt(index);
4812 int enumeration_index = details.dictionary_index();
4813 PropertyType type = details.type();
4815 if (value->IsJSFunction()) {
4816 DataConstantDescriptor d(key, handle(value, isolate),
4817 details.attributes());
4818 descriptors->Set(enumeration_index - 1, &d);
4819 } else if (type == DATA) {
4820 if (current_offset < inobject_props) {
4821 object->InObjectPropertyAtPut(current_offset, value,
4822 UPDATE_WRITE_BARRIER);
4824 int offset = current_offset - inobject_props;
4825 fields->set(offset, value);
4827 DataDescriptor d(key, current_offset, details.attributes(),
4828 // TODO(verwaest): value->OptimalRepresentation();
4829 Representation::Tagged());
4830 current_offset += d.GetDetails().field_width_in_words();
4831 descriptors->Set(enumeration_index - 1, &d);
4832 } else if (type == ACCESSOR_CONSTANT) {
4833 AccessorConstantDescriptor d(key, handle(value, isolate),
4834 details.attributes());
4835 descriptors->Set(enumeration_index - 1, &d);
4840 DCHECK(current_offset == number_of_fields);
4842 descriptors->Sort();
4844 Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New(
4845 new_map, descriptors, descriptors->number_of_descriptors());
4847 DisallowHeapAllocation no_gc;
4848 new_map->InitializeDescriptors(*descriptors, *layout_descriptor);
4849 new_map->set_unused_property_fields(unused_property_fields);
4851 // Transform the object.
4852 object->synchronized_set_map(*new_map);
4854 object->set_properties(*fields);
4855 DCHECK(object->IsJSObject());
4857 // Check that it really works.
4858 DCHECK(object->HasFastProperties());
4862 void JSObject::ResetElements(Handle<JSObject> object) {
4863 Isolate* isolate = object->GetIsolate();
4864 CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map());
4865 if (object->map()->has_dictionary_elements()) {
4866 Handle<SeededNumberDictionary> new_elements =
4867 SeededNumberDictionary::New(isolate, 0);
4868 object->set_elements(*new_elements);
4870 object->set_elements(object->map()->GetInitialElements());
4875 static Handle<SeededNumberDictionary> CopyFastElementsToDictionary(
4876 Handle<FixedArrayBase> array, int length,
4877 Handle<SeededNumberDictionary> dictionary, bool used_as_prototype) {
4878 Isolate* isolate = array->GetIsolate();
4879 Factory* factory = isolate->factory();
4880 bool has_double_elements = array->IsFixedDoubleArray();
4881 for (int i = 0; i < length; i++) {
4882 Handle<Object> value;
4883 if (has_double_elements) {
4884 Handle<FixedDoubleArray> double_array =
4885 Handle<FixedDoubleArray>::cast(array);
4886 if (double_array->is_the_hole(i)) {
4887 value = factory->the_hole_value();
4889 value = factory->NewHeapNumber(double_array->get_scalar(i));
4892 value = handle(Handle<FixedArray>::cast(array)->get(i), isolate);
4894 if (!value->IsTheHole()) {
4895 PropertyDetails details = PropertyDetails::Empty();
4896 dictionary = SeededNumberDictionary::AddNumberEntry(
4897 dictionary, i, value, details, used_as_prototype);
4904 void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) {
4905 if (dictionary->requires_slow_elements()) return;
4906 dictionary->set_requires_slow_elements();
4907 // TODO(verwaest): Remove this hack.
4908 if (map()->is_prototype_map()) {
4909 GetHeap()->ClearAllKeyedStoreICs();
4914 Handle<SeededNumberDictionary> JSObject::GetNormalizedElementDictionary(
4915 Handle<JSObject> object, Handle<FixedArrayBase> elements) {
4916 DCHECK(!object->HasDictionaryElements());
4917 DCHECK(!object->HasSlowArgumentsElements());
4918 Isolate* isolate = object->GetIsolate();
4919 // Ensure that notifications fire if the array or object prototypes are
4921 isolate->UpdateArrayProtectorOnNormalizeElements(object);
4922 int length = object->IsJSArray()
4923 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
4924 : elements->length();
4925 int used = object->GetFastElementsUsage();
4926 Handle<SeededNumberDictionary> dictionary =
4927 SeededNumberDictionary::New(isolate, used);
4928 return CopyFastElementsToDictionary(elements, length, dictionary,
4929 object->map()->is_prototype_map());
4933 Handle<SeededNumberDictionary> JSObject::NormalizeElements(
4934 Handle<JSObject> object) {
4935 DCHECK(!object->HasFixedTypedArrayElements());
4936 Isolate* isolate = object->GetIsolate();
4938 // Find the backing store.
4939 Handle<FixedArrayBase> elements(object->elements(), isolate);
4940 bool is_arguments = object->HasSloppyArgumentsElements();
4942 FixedArray* parameter_map = FixedArray::cast(*elements);
4943 elements = handle(FixedArrayBase::cast(parameter_map->get(1)), isolate);
4946 if (elements->IsDictionary()) {
4947 return Handle<SeededNumberDictionary>::cast(elements);
4950 DCHECK(object->HasFastSmiOrObjectElements() ||
4951 object->HasFastDoubleElements() ||
4952 object->HasFastArgumentsElements());
4954 Handle<SeededNumberDictionary> dictionary =
4955 GetNormalizedElementDictionary(object, elements);
4957 // Switch to using the dictionary as the backing storage for elements.
4958 ElementsKind target_kind =
4959 is_arguments ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS : DICTIONARY_ELEMENTS;
4960 Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind);
4961 // Set the new map first to satify the elements type assert in set_elements().
4962 JSObject::MigrateToMap(object, new_map);
4965 FixedArray::cast(object->elements())->set(1, *dictionary);
4967 object->set_elements(*dictionary);
4970 isolate->counters()->elements_to_dictionary()->Increment();
4973 if (FLAG_trace_normalization) {
4974 OFStream os(stdout);
4975 os << "Object elements have been normalized:\n";
4980 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
4985 static Smi* GenerateIdentityHash(Isolate* isolate) {
4989 // Generate a random 32-bit hash value but limit range to fit
4991 hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue;
4993 } while (hash_value == 0 && attempts < 30);
4994 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
4996 return Smi::FromInt(hash_value);
5000 void JSObject::SetIdentityHash(Handle<JSObject> object, Handle<Smi> hash) {
5001 DCHECK(!object->IsJSGlobalProxy());
5002 Isolate* isolate = object->GetIsolate();
5003 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
5004 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
5008 template<typename ProxyType>
5009 static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) {
5010 Isolate* isolate = proxy->GetIsolate();
5012 Handle<Object> maybe_hash(proxy->hash(), isolate);
5013 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
5015 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
5016 proxy->set_hash(*hash);
5021 Object* JSObject::GetIdentityHash() {
5022 DisallowHeapAllocation no_gc;
5023 Isolate* isolate = GetIsolate();
5024 if (IsJSGlobalProxy()) {
5025 return JSGlobalProxy::cast(this)->hash();
5027 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
5028 Handle<Object> stored_value =
5029 Object::GetPropertyOrElement(Handle<Object>(this, isolate),
5030 hash_code_symbol).ToHandleChecked();
5031 return stored_value->IsSmi() ? *stored_value
5032 : isolate->heap()->undefined_value();
5036 Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) {
5037 if (object->IsJSGlobalProxy()) {
5038 return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object));
5041 Isolate* isolate = object->GetIsolate();
5043 Handle<Object> maybe_hash(object->GetIdentityHash(), isolate);
5044 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
5046 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
5047 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
5048 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
5053 Object* JSProxy::GetIdentityHash() {
5054 return this->hash();
5058 Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) {
5059 return GetOrCreateIdentityHashHelper(proxy);
5063 Object* JSObject::GetHiddenProperty(Handle<Name> key) {
5064 DisallowHeapAllocation no_gc;
5065 DCHECK(key->IsUniqueName());
5066 if (IsJSGlobalProxy()) {
5067 // For a proxy, use the prototype as target object.
5068 PrototypeIterator iter(GetIsolate(), this);
5069 // If the proxy is detached, return undefined.
5070 if (iter.IsAtEnd()) return GetHeap()->the_hole_value();
5071 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
5072 return JSObject::cast(iter.GetCurrent())->GetHiddenProperty(key);
5074 DCHECK(!IsJSGlobalProxy());
5075 Object* inline_value = GetHiddenPropertiesHashTable();
5077 if (inline_value->IsUndefined()) return GetHeap()->the_hole_value();
5079 ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value);
5080 Object* entry = hashtable->Lookup(key);
5085 Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object,
5087 Handle<Object> value) {
5088 Isolate* isolate = object->GetIsolate();
5090 DCHECK(key->IsUniqueName());
5091 if (object->IsJSGlobalProxy()) {
5092 // For a proxy, use the prototype as target object.
5093 PrototypeIterator iter(isolate, object);
5094 // If the proxy is detached, return undefined.
5095 if (iter.IsAtEnd()) return isolate->factory()->undefined_value();
5096 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5097 return SetHiddenProperty(
5098 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key,
5101 DCHECK(!object->IsJSGlobalProxy());
5103 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
5105 Handle<ObjectHashTable> hashtable =
5106 GetOrCreateHiddenPropertiesHashtable(object);
5108 // If it was found, check if the key is already in the dictionary.
5109 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key,
5111 if (*new_table != *hashtable) {
5112 // If adding the key expanded the dictionary (i.e., Add returned a new
5113 // dictionary), store it back to the object.
5114 SetHiddenPropertiesHashTable(object, new_table);
5117 // Return this to mark success.
5122 void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) {
5123 Isolate* isolate = object->GetIsolate();
5124 DCHECK(key->IsUniqueName());
5126 if (object->IsJSGlobalProxy()) {
5127 PrototypeIterator iter(isolate, object);
5128 if (iter.IsAtEnd()) return;
5129 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5130 return DeleteHiddenProperty(
5131 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key);
5134 Object* inline_value = object->GetHiddenPropertiesHashTable();
5136 if (inline_value->IsUndefined()) return;
5138 Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value));
5139 bool was_present = false;
5140 ObjectHashTable::Remove(hashtable, key, &was_present);
5144 bool JSObject::HasHiddenProperties(Handle<JSObject> object) {
5145 Handle<Name> hidden = object->GetIsolate()->factory()->hidden_string();
5146 LookupIterator it(object, hidden, LookupIterator::OWN_SKIP_INTERCEPTOR);
5147 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
5148 // Cannot get an exception since the hidden_string isn't accessible to JS.
5149 DCHECK(maybe.IsJust());
5150 return maybe.FromJust() != ABSENT;
5154 Object* JSObject::GetHiddenPropertiesHashTable() {
5155 DCHECK(!IsJSGlobalProxy());
5156 if (HasFastProperties()) {
5157 // If the object has fast properties, check whether the first slot
5158 // in the descriptor array matches the hidden string. Since the
5159 // hidden strings hash code is zero (and no other name has hash
5160 // code zero) it will always occupy the first entry if present.
5161 DescriptorArray* descriptors = this->map()->instance_descriptors();
5162 if (descriptors->number_of_descriptors() > 0) {
5163 int sorted_index = descriptors->GetSortedKeyIndex(0);
5164 if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() &&
5165 sorted_index < map()->NumberOfOwnDescriptors()) {
5166 DCHECK(descriptors->GetType(sorted_index) == DATA);
5167 DCHECK(descriptors->GetDetails(sorted_index).representation().
5168 IsCompatibleForLoad(Representation::Tagged()));
5169 FieldIndex index = FieldIndex::ForDescriptor(this->map(),
5171 return this->RawFastPropertyAt(index);
5173 return GetHeap()->undefined_value();
5176 return GetHeap()->undefined_value();
5179 Isolate* isolate = GetIsolate();
5180 LookupIterator it(handle(this), isolate->factory()->hidden_string(),
5181 LookupIterator::OWN_SKIP_INTERCEPTOR);
5182 // Access check is always skipped for the hidden string anyways.
5183 return *GetDataProperty(&it);
5187 Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable(
5188 Handle<JSObject> object) {
5189 Isolate* isolate = object->GetIsolate();
5191 static const int kInitialCapacity = 4;
5192 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
5193 if (inline_value->IsHashTable()) {
5194 return Handle<ObjectHashTable>::cast(inline_value);
5197 Handle<ObjectHashTable> hashtable = ObjectHashTable::New(
5198 isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY);
5200 DCHECK(inline_value->IsUndefined());
5201 SetHiddenPropertiesHashTable(object, hashtable);
5206 Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object,
5207 Handle<Object> value) {
5208 DCHECK(!object->IsJSGlobalProxy());
5209 Isolate* isolate = object->GetIsolate();
5210 Handle<Name> name = isolate->factory()->hidden_string();
5211 SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert();
5216 MaybeHandle<Object> JSObject::DeletePropertyWithInterceptor(
5217 LookupIterator* it) {
5218 Isolate* isolate = it->isolate();
5219 // Make sure that the top context does not change when doing callbacks or
5220 // interceptor calls.
5221 AssertNoContextChange ncc(isolate);
5223 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
5224 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
5225 if (interceptor->deleter()->IsUndefined()) return MaybeHandle<Object>();
5227 Handle<JSObject> holder = it->GetHolder<JSObject>();
5229 PropertyCallbackArguments args(isolate, interceptor->data(),
5230 *it->GetReceiver(), *holder);
5231 v8::Local<v8::Boolean> result;
5232 if (it->IsElement()) {
5233 uint32_t index = it->index();
5234 v8::IndexedPropertyDeleterCallback deleter =
5235 v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter());
5237 ApiIndexedPropertyAccess("interceptor-indexed-delete", *holder, index));
5238 result = args.Call(deleter, index);
5239 } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) {
5240 return MaybeHandle<Object>();
5242 Handle<Name> name = it->name();
5243 v8::GenericNamedPropertyDeleterCallback deleter =
5244 v8::ToCData<v8::GenericNamedPropertyDeleterCallback>(
5245 interceptor->deleter());
5247 ApiNamedPropertyAccess("interceptor-named-delete", *holder, *name));
5248 result = args.Call(deleter, v8::Utils::ToLocal(name));
5251 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5252 if (result.IsEmpty()) return MaybeHandle<Object>();
5254 DCHECK(result->IsBoolean());
5255 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
5256 result_internal->VerifyApiCallResultType();
5257 // Rebox CustomArguments::kReturnValueOffset before returning.
5258 return handle(*result_internal, isolate);
5262 void JSObject::DeleteNormalizedProperty(Handle<JSObject> object,
5263 Handle<Name> name, int entry) {
5264 DCHECK(!object->HasFastProperties());
5265 Isolate* isolate = object->GetIsolate();
5267 if (object->IsGlobalObject()) {
5268 // If we have a global object, invalidate the cell and swap in a new one.
5269 Handle<GlobalDictionary> dictionary(object->global_dictionary());
5270 DCHECK_NE(GlobalDictionary::kNotFound, entry);
5272 auto cell = PropertyCell::InvalidateEntry(dictionary, entry);
5273 cell->set_value(isolate->heap()->the_hole_value());
5274 // TODO(ishell): InvalidateForDelete
5275 cell->set_property_details(
5276 cell->property_details().set_cell_type(PropertyCellType::kInvalidated));
5278 Handle<NameDictionary> dictionary(object->property_dictionary());
5279 DCHECK_NE(NameDictionary::kNotFound, entry);
5281 NameDictionary::DeleteProperty(dictionary, entry);
5282 Handle<NameDictionary> new_properties =
5283 NameDictionary::Shrink(dictionary, name);
5284 object->set_properties(*new_properties);
5289 // ECMA-262, 3rd, 8.6.2.5
5290 MaybeHandle<Object> JSReceiver::DeleteProperty(LookupIterator* it,
5291 LanguageMode language_mode) {
5292 Isolate* isolate = it->isolate();
5293 if (it->state() == LookupIterator::JSPROXY) {
5294 return JSProxy::DeletePropertyWithHandler(it->GetHolder<JSProxy>(),
5295 it->GetName(), language_mode);
5298 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
5301 receiver->map()->is_observed() &&
5302 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()));
5304 Handle<Object> old_value = it->factory()->the_hole_value();
5306 for (; it->IsFound(); it->Next()) {
5307 switch (it->state()) {
5308 case LookupIterator::JSPROXY:
5309 case LookupIterator::NOT_FOUND:
5310 case LookupIterator::TRANSITION:
5312 case LookupIterator::ACCESS_CHECK:
5313 if (it->HasAccess()) break;
5314 isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>());
5315 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5316 return it->factory()->false_value();
5317 case LookupIterator::INTERCEPTOR: {
5318 MaybeHandle<Object> maybe_result =
5319 JSObject::DeletePropertyWithInterceptor(it);
5320 // Delete with interceptor succeeded. Return result.
5321 if (!maybe_result.is_null()) return maybe_result;
5322 // An exception was thrown in the interceptor. Propagate.
5323 if (isolate->has_pending_exception()) return maybe_result;
5326 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5327 return it->factory()->true_value();
5328 case LookupIterator::DATA:
5330 old_value = it->GetDataValue();
5333 case LookupIterator::ACCESSOR: {
5334 if (!it->IsConfigurable() || receiver->map()->is_strong()) {
5335 // Fail if the property is not configurable, or on a strong object.
5336 if (is_strict(language_mode)) {
5337 MessageTemplate::Template templ =
5338 receiver->map()->is_strong()
5339 ? MessageTemplate::kStrongDeleteProperty
5340 : MessageTemplate::kStrictDeleteProperty;
5342 isolate, NewTypeError(templ, it->GetName(), receiver), Object);
5344 return it->factory()->false_value();
5350 RETURN_ON_EXCEPTION(isolate,
5351 JSObject::EnqueueChangeRecord(
5352 receiver, "delete", it->GetName(), old_value),
5356 return it->factory()->true_value();
5361 return it->factory()->true_value();
5365 MaybeHandle<Object> JSReceiver::DeleteElement(Handle<JSReceiver> object,
5367 LanguageMode language_mode) {
5368 LookupIterator it(object->GetIsolate(), object, index,
5369 LookupIterator::HIDDEN);
5370 return DeleteProperty(&it, language_mode);
5374 MaybeHandle<Object> JSReceiver::DeleteProperty(Handle<JSReceiver> object,
5376 LanguageMode language_mode) {
5377 LookupIterator it(object, name, LookupIterator::HIDDEN);
5378 return JSObject::DeleteProperty(&it, language_mode);
5382 MaybeHandle<Object> JSReceiver::DeletePropertyOrElement(
5383 Handle<JSReceiver> object, Handle<Name> name, LanguageMode language_mode) {
5384 LookupIterator it = LookupIterator::PropertyOrElement(
5385 name->GetIsolate(), object, name, LookupIterator::HIDDEN);
5386 return JSObject::DeleteProperty(&it, language_mode);
5390 bool JSObject::ReferencesObjectFromElements(FixedArray* elements,
5393 DCHECK(IsFastObjectElementsKind(kind) ||
5394 kind == DICTIONARY_ELEMENTS);
5395 if (IsFastObjectElementsKind(kind)) {
5396 int length = IsJSArray()
5397 ? Smi::cast(JSArray::cast(this)->length())->value()
5398 : elements->length();
5399 for (int i = 0; i < length; ++i) {
5400 Object* element = elements->get(i);
5401 if (!element->IsTheHole() && element == object) return true;
5405 SeededNumberDictionary::cast(elements)->SlowReverseLookup(object);
5406 if (!key->IsUndefined()) return true;
5412 // Check whether this object references another object.
5413 bool JSObject::ReferencesObject(Object* obj) {
5414 Map* map_of_this = map();
5415 Heap* heap = GetHeap();
5416 DisallowHeapAllocation no_allocation;
5418 // Is the object the constructor for this object?
5419 if (map_of_this->GetConstructor() == obj) {
5423 // Is the object the prototype for this object?
5424 if (map_of_this->prototype() == obj) {
5428 // Check if the object is among the named properties.
5429 Object* key = SlowReverseLookup(obj);
5430 if (!key->IsUndefined()) {
5434 // Check if the object is among the indexed properties.
5435 ElementsKind kind = GetElementsKind();
5437 // Raw pixels and external arrays do not reference other
5439 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5440 case TYPE##_ELEMENTS: \
5443 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5444 #undef TYPED_ARRAY_CASE
5446 case FAST_DOUBLE_ELEMENTS:
5447 case FAST_HOLEY_DOUBLE_ELEMENTS:
5449 case FAST_SMI_ELEMENTS:
5450 case FAST_HOLEY_SMI_ELEMENTS:
5453 case FAST_HOLEY_ELEMENTS:
5454 case DICTIONARY_ELEMENTS: {
5455 FixedArray* elements = FixedArray::cast(this->elements());
5456 if (ReferencesObjectFromElements(elements, kind, obj)) return true;
5459 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5460 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
5461 FixedArray* parameter_map = FixedArray::cast(elements());
5462 // Check the mapped parameters.
5463 int length = parameter_map->length();
5464 for (int i = 2; i < length; ++i) {
5465 Object* value = parameter_map->get(i);
5466 if (!value->IsTheHole() && value == obj) return true;
5468 // Check the arguments.
5469 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
5470 kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS :
5471 FAST_HOLEY_ELEMENTS;
5472 if (ReferencesObjectFromElements(arguments, kind, obj)) return true;
5477 // For functions check the context.
5478 if (IsJSFunction()) {
5479 // Get the constructor function for arguments array.
5480 Map* arguments_map =
5481 heap->isolate()->context()->native_context()->sloppy_arguments_map();
5482 JSFunction* arguments_function =
5483 JSFunction::cast(arguments_map->GetConstructor());
5485 // Get the context and don't check if it is the native context.
5486 JSFunction* f = JSFunction::cast(this);
5487 Context* context = f->context();
5488 if (context->IsNativeContext()) {
5492 // Check the non-special context slots.
5493 for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) {
5494 // Only check JS objects.
5495 if (context->get(i)->IsJSObject()) {
5496 JSObject* ctxobj = JSObject::cast(context->get(i));
5497 // If it is an arguments array check the content.
5498 if (ctxobj->map()->GetConstructor() == arguments_function) {
5499 if (ctxobj->ReferencesObject(obj)) {
5502 } else if (ctxobj == obj) {
5508 // Check the context extension (if any) if it can have references.
5509 if (context->has_extension() && !context->IsCatchContext()) {
5510 // With harmony scoping, a JSFunction may have a script context.
5511 // TODO(mvstanton): walk into the ScopeInfo.
5512 if (context->IsScriptContext()) {
5516 return context->extension_object()->ReferencesObject(obj);
5520 // No references to object.
5525 MaybeHandle<Object> JSObject::PreventExtensions(Handle<JSObject> object) {
5526 if (!object->map()->is_extensible()) return object;
5528 if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) {
5529 return PreventExtensionsWithTransition<NONE>(object);
5532 Isolate* isolate = object->GetIsolate();
5534 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5535 isolate->ReportFailedAccessCheck(object);
5536 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5537 return isolate->factory()->false_value();
5540 if (object->IsJSGlobalProxy()) {
5541 PrototypeIterator iter(isolate, object);
5542 if (iter.IsAtEnd()) return object;
5543 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5544 return PreventExtensions(
5545 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5548 // It's not possible to seal objects with external array elements
5549 if (object->HasFixedTypedArrayElements()) {
5551 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5555 // If there are fast elements we normalize.
5556 Handle<SeededNumberDictionary> dictionary = NormalizeElements(object);
5557 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
5559 // Make sure that we never go back to fast case.
5560 object->RequireSlowElements(*dictionary);
5562 // Do a map transition, other objects with this map may still
5564 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5565 Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions");
5567 new_map->set_is_extensible(false);
5568 JSObject::MigrateToMap(object, new_map);
5569 DCHECK(!object->map()->is_extensible());
5571 if (object->map()->is_observed()) {
5572 RETURN_ON_EXCEPTION(
5574 EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(),
5575 isolate->factory()->the_hole_value()),
5582 bool JSObject::IsExtensible() {
5583 if (IsJSGlobalProxy()) {
5584 PrototypeIterator iter(GetIsolate(), this);
5585 if (iter.IsAtEnd()) return false;
5586 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
5587 return JSObject::cast(iter.GetCurrent())->map()->is_extensible();
5589 return map()->is_extensible();
5593 template <typename Dictionary>
5594 static void ApplyAttributesToDictionary(Dictionary* dictionary,
5595 const PropertyAttributes attributes) {
5596 int capacity = dictionary->Capacity();
5597 for (int i = 0; i < capacity; i++) {
5598 Object* k = dictionary->KeyAt(i);
5599 if (dictionary->IsKey(k) &&
5600 !(k->IsSymbol() && Symbol::cast(k)->is_private())) {
5601 PropertyDetails details = dictionary->DetailsAt(i);
5602 int attrs = attributes;
5603 // READ_ONLY is an invalid attribute for JS setters/getters.
5604 if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) {
5605 Object* v = dictionary->ValueAt(i);
5606 if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value();
5607 if (v->IsAccessorPair()) attrs &= ~READ_ONLY;
5609 details = details.CopyAddAttributes(
5610 static_cast<PropertyAttributes>(attrs));
5611 dictionary->DetailsAtPut(i, details);
5617 template <PropertyAttributes attrs>
5618 MaybeHandle<Object> JSObject::PreventExtensionsWithTransition(
5619 Handle<JSObject> object) {
5620 STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN);
5622 // Sealing/freezing sloppy arguments should be handled elsewhere.
5623 DCHECK(!object->HasSloppyArgumentsElements());
5624 DCHECK(!object->map()->is_observed());
5626 Isolate* isolate = object->GetIsolate();
5627 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5628 isolate->ReportFailedAccessCheck(object);
5629 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5630 return isolate->factory()->false_value();
5633 if (object->IsJSGlobalProxy()) {
5634 PrototypeIterator iter(isolate, object);
5635 if (iter.IsAtEnd()) return object;
5636 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5637 return PreventExtensionsWithTransition<attrs>(
5638 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5641 // It's not possible to seal or freeze objects with external array elements
5642 if (object->HasFixedTypedArrayElements()) {
5644 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5648 Handle<SeededNumberDictionary> new_element_dictionary;
5649 if (!object->HasDictionaryElements()) {
5652 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
5653 : object->elements()->length();
5654 new_element_dictionary =
5655 length == 0 ? isolate->factory()->empty_slow_element_dictionary()
5656 : GetNormalizedElementDictionary(
5657 object, handle(object->elements()));
5660 Handle<Symbol> transition_marker;
5661 if (attrs == NONE) {
5662 transition_marker = isolate->factory()->nonextensible_symbol();
5663 } else if (attrs == SEALED) {
5664 transition_marker = isolate->factory()->sealed_symbol();
5666 DCHECK(attrs == FROZEN);
5667 transition_marker = isolate->factory()->frozen_symbol();
5670 Handle<Map> old_map(object->map(), isolate);
5672 TransitionArray::SearchSpecial(*old_map, *transition_marker);
5673 if (transition != NULL) {
5674 Handle<Map> transition_map(transition, isolate);
5675 DCHECK(transition_map->has_dictionary_elements());
5676 DCHECK(!transition_map->is_extensible());
5677 JSObject::MigrateToMap(object, transition_map);
5678 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5679 // Create a new descriptor array with the appropriate property attributes
5680 Handle<Map> new_map = Map::CopyForPreventExtensions(
5681 old_map, attrs, transition_marker, "CopyForPreventExtensions");
5682 JSObject::MigrateToMap(object, new_map);
5684 DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map());
5685 // Slow path: need to normalize properties for safety
5686 NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0,
5687 "SlowPreventExtensions");
5689 // Create a new map, since other objects with this map may be extensible.
5690 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5691 Handle<Map> new_map =
5692 Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions");
5693 new_map->set_is_extensible(false);
5694 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
5695 JSObject::MigrateToMap(object, new_map);
5697 if (attrs != NONE) {
5698 if (object->IsGlobalObject()) {
5699 ApplyAttributesToDictionary(object->global_dictionary(), attrs);
5701 ApplyAttributesToDictionary(object->property_dictionary(), attrs);
5706 DCHECK(object->map()->has_dictionary_elements());
5707 if (!new_element_dictionary.is_null()) {
5708 object->set_elements(*new_element_dictionary);
5711 if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) {
5712 SeededNumberDictionary* dictionary = object->element_dictionary();
5713 // Make sure we never go back to the fast case
5714 object->RequireSlowElements(dictionary);
5715 if (attrs != NONE) {
5716 ApplyAttributesToDictionary(dictionary, attrs);
5724 MaybeHandle<Object> JSObject::Freeze(Handle<JSObject> object) {
5725 return PreventExtensionsWithTransition<FROZEN>(object);
5729 MaybeHandle<Object> JSObject::Seal(Handle<JSObject> object) {
5730 return PreventExtensionsWithTransition<SEALED>(object);
5734 void JSObject::SetObserved(Handle<JSObject> object) {
5735 DCHECK(!object->IsJSGlobalProxy());
5736 DCHECK(!object->IsJSGlobalObject());
5737 Isolate* isolate = object->GetIsolate();
5738 Handle<Map> new_map;
5739 Handle<Map> old_map(object->map(), isolate);
5740 DCHECK(!old_map->is_observed());
5741 Map* transition = TransitionArray::SearchSpecial(
5742 *old_map, isolate->heap()->observed_symbol());
5743 if (transition != NULL) {
5744 new_map = handle(transition, isolate);
5745 DCHECK(new_map->is_observed());
5746 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5747 new_map = Map::CopyForObserved(old_map);
5749 new_map = Map::Copy(old_map, "SlowObserved");
5750 new_map->set_is_observed();
5752 JSObject::MigrateToMap(object, new_map);
5756 Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object,
5757 Representation representation,
5759 Isolate* isolate = object->GetIsolate();
5760 if (object->IsUnboxedDoubleField(index)) {
5761 double value = object->RawFastDoublePropertyAt(index);
5762 return isolate->factory()->NewHeapNumber(value);
5764 Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate);
5765 return Object::WrapForRead(isolate, raw_value, representation);
5769 template<class ContextObject>
5770 class JSObjectWalkVisitor {
5772 JSObjectWalkVisitor(ContextObject* site_context, bool copying,
5773 JSObject::DeepCopyHints hints)
5774 : site_context_(site_context),
5778 MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object);
5781 MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty(
5782 Handle<JSObject> object,
5783 Handle<JSObject> value) {
5784 Handle<AllocationSite> current_site = site_context()->EnterNewScope();
5785 MaybeHandle<JSObject> copy_of_value = StructureWalk(value);
5786 site_context()->ExitScope(current_site, value);
5787 return copy_of_value;
5790 inline ContextObject* site_context() { return site_context_; }
5791 inline Isolate* isolate() { return site_context()->isolate(); }
5793 inline bool copying() const { return copying_; }
5796 ContextObject* site_context_;
5797 const bool copying_;
5798 const JSObject::DeepCopyHints hints_;
5802 template <class ContextObject>
5803 MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk(
5804 Handle<JSObject> object) {
5805 Isolate* isolate = this->isolate();
5806 bool copying = this->copying();
5807 bool shallow = hints_ == JSObject::kObjectIsShallow;
5810 StackLimitCheck check(isolate);
5812 if (check.HasOverflowed()) {
5813 isolate->StackOverflow();
5814 return MaybeHandle<JSObject>();
5818 if (object->map()->is_deprecated()) {
5819 JSObject::MigrateInstance(object);
5822 Handle<JSObject> copy;
5824 Handle<AllocationSite> site_to_pass;
5825 if (site_context()->ShouldCreateMemento(object)) {
5826 site_to_pass = site_context()->current();
5828 copy = isolate->factory()->CopyJSObjectWithAllocationSite(
5829 object, site_to_pass);
5834 DCHECK(copying || copy.is_identical_to(object));
5836 ElementsKind kind = copy->GetElementsKind();
5837 if (copying && IsFastSmiOrObjectElementsKind(kind) &&
5838 FixedArray::cast(copy->elements())->map() ==
5839 isolate->heap()->fixed_cow_array_map()) {
5840 isolate->counters()->cow_arrays_created_runtime()->Increment();
5844 HandleScope scope(isolate);
5846 // Deep copy own properties.
5847 if (copy->HasFastProperties()) {
5848 Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors());
5849 int limit = copy->map()->NumberOfOwnDescriptors();
5850 for (int i = 0; i < limit; i++) {
5851 PropertyDetails details = descriptors->GetDetails(i);
5852 if (details.type() != DATA) continue;
5853 FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i);
5854 if (object->IsUnboxedDoubleField(index)) {
5856 double value = object->RawFastDoublePropertyAt(index);
5857 copy->RawFastDoublePropertyAtPut(index, value);
5860 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
5861 if (value->IsJSObject()) {
5862 ASSIGN_RETURN_ON_EXCEPTION(
5864 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5867 copy->FastPropertyAtPut(index, *value);
5871 Representation representation = details.representation();
5872 value = Object::NewStorageFor(isolate, value, representation);
5873 copy->FastPropertyAtPut(index, *value);
5879 Handle<FixedArray> names =
5880 isolate->factory()->NewFixedArray(copy->NumberOfOwnProperties());
5881 copy->GetOwnPropertyNames(*names, 0);
5882 for (int i = 0; i < names->length(); i++) {
5883 DCHECK(names->get(i)->IsString());
5884 Handle<String> key_string(String::cast(names->get(i)));
5885 Maybe<PropertyAttributes> maybe =
5886 JSReceiver::GetOwnPropertyAttributes(copy, key_string);
5887 DCHECK(maybe.IsJust());
5888 PropertyAttributes attributes = maybe.FromJust();
5889 // Only deep copy fields from the object literal expression.
5890 // In particular, don't try to copy the length attribute of
5892 if (attributes != NONE) continue;
5893 Handle<Object> value =
5894 Object::GetProperty(copy, key_string).ToHandleChecked();
5895 if (value->IsJSObject()) {
5896 Handle<JSObject> result;
5897 ASSIGN_RETURN_ON_EXCEPTION(
5899 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5902 // Creating object copy for literals. No strict mode needed.
5903 JSObject::SetProperty(copy, key_string, result, SLOPPY).Assert();
5909 // Deep copy own elements.
5910 // Pixel elements cannot be created using an object literal.
5911 DCHECK(!copy->HasFixedTypedArrayElements());
5913 case FAST_SMI_ELEMENTS:
5915 case FAST_HOLEY_SMI_ELEMENTS:
5916 case FAST_HOLEY_ELEMENTS: {
5917 Handle<FixedArray> elements(FixedArray::cast(copy->elements()));
5918 if (elements->map() == isolate->heap()->fixed_cow_array_map()) {
5920 for (int i = 0; i < elements->length(); i++) {
5921 DCHECK(!elements->get(i)->IsJSObject());
5925 for (int i = 0; i < elements->length(); i++) {
5926 Handle<Object> value(elements->get(i), isolate);
5927 DCHECK(value->IsSmi() ||
5928 value->IsTheHole() ||
5929 (IsFastObjectElementsKind(copy->GetElementsKind())));
5930 if (value->IsJSObject()) {
5931 Handle<JSObject> result;
5932 ASSIGN_RETURN_ON_EXCEPTION(
5934 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5937 elements->set(i, *result);
5944 case DICTIONARY_ELEMENTS: {
5945 Handle<SeededNumberDictionary> element_dictionary(
5946 copy->element_dictionary());
5947 int capacity = element_dictionary->Capacity();
5948 for (int i = 0; i < capacity; i++) {
5949 Object* k = element_dictionary->KeyAt(i);
5950 if (element_dictionary->IsKey(k)) {
5951 Handle<Object> value(element_dictionary->ValueAt(i), isolate);
5952 if (value->IsJSObject()) {
5953 Handle<JSObject> result;
5954 ASSIGN_RETURN_ON_EXCEPTION(
5956 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5959 element_dictionary->ValueAtPut(i, *result);
5966 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5967 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
5972 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5973 case TYPE##_ELEMENTS: \
5975 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5976 #undef TYPED_ARRAY_CASE
5978 case FAST_DOUBLE_ELEMENTS:
5979 case FAST_HOLEY_DOUBLE_ELEMENTS:
5980 // No contained objects, nothing to do.
5989 MaybeHandle<JSObject> JSObject::DeepWalk(
5990 Handle<JSObject> object,
5991 AllocationSiteCreationContext* site_context) {
5992 JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false,
5994 MaybeHandle<JSObject> result = v.StructureWalk(object);
5995 Handle<JSObject> for_assert;
5996 DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object));
6001 MaybeHandle<JSObject> JSObject::DeepCopy(
6002 Handle<JSObject> object,
6003 AllocationSiteUsageContext* site_context,
6004 DeepCopyHints hints) {
6005 JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints);
6006 MaybeHandle<JSObject> copy = v.StructureWalk(object);
6007 Handle<JSObject> for_assert;
6008 DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object));
6014 MaybeHandle<Object> JSReceiver::ToPrimitive(Handle<JSReceiver> receiver,
6015 ToPrimitiveHint hint) {
6016 Isolate* const isolate = receiver->GetIsolate();
6017 Handle<Object> exotic_to_prim;
6018 ASSIGN_RETURN_ON_EXCEPTION(
6019 isolate, exotic_to_prim,
6020 GetMethod(receiver, isolate->factory()->to_primitive_symbol()), Object);
6021 if (!exotic_to_prim->IsUndefined()) {
6022 Handle<Object> hint_string;
6024 case ToPrimitiveHint::kDefault:
6025 hint_string = isolate->factory()->default_string();
6027 case ToPrimitiveHint::kNumber:
6028 hint_string = isolate->factory()->number_string();
6030 case ToPrimitiveHint::kString:
6031 hint_string = isolate->factory()->string_string();
6034 Handle<Object> result;
6035 ASSIGN_RETURN_ON_EXCEPTION(
6037 Execution::Call(isolate, exotic_to_prim, receiver, 1, &hint_string),
6039 if (result->IsPrimitive()) return result;
6040 THROW_NEW_ERROR(isolate,
6041 NewTypeError(MessageTemplate::kCannotConvertToPrimitive),
6044 return OrdinaryToPrimitive(receiver,
6045 (hint == ToPrimitiveHint::kString)
6046 ? isolate->factory()->string_string()
6047 : isolate->factory()->number_string());
6052 MaybeHandle<Object> JSReceiver::OrdinaryToPrimitive(Handle<JSReceiver> receiver,
6053 Handle<String> hint) {
6054 Isolate* const isolate = receiver->GetIsolate();
6055 Handle<String> method_names[2];
6056 if (hint.is_identical_to(isolate->factory()->number_string())) {
6057 method_names[0] = isolate->factory()->valueOf_string();
6058 method_names[1] = isolate->factory()->toString_string();
6060 DCHECK(hint.is_identical_to(isolate->factory()->string_string()));
6061 method_names[0] = isolate->factory()->toString_string();
6062 method_names[1] = isolate->factory()->valueOf_string();
6064 for (Handle<String> name : method_names) {
6065 Handle<Object> method;
6066 ASSIGN_RETURN_ON_EXCEPTION(isolate, method,
6067 JSReceiver::GetProperty(receiver, name), Object);
6068 if (method->IsCallable()) {
6069 Handle<Object> result;
6070 ASSIGN_RETURN_ON_EXCEPTION(
6071 isolate, result, Execution::Call(isolate, method, receiver, 0, NULL),
6073 if (result->IsPrimitive()) return result;
6076 THROW_NEW_ERROR(isolate,
6077 NewTypeError(MessageTemplate::kCannotConvertToPrimitive),
6082 // Tests for the fast common case for property enumeration:
6083 // - This object and all prototypes has an enum cache (which means that
6084 // it is no proxy, has no interceptors and needs no access checks).
6085 // - This object has no elements.
6086 // - No prototype has enumerable properties/elements.
6087 bool JSReceiver::IsSimpleEnum() {
6088 for (PrototypeIterator iter(GetIsolate(), this,
6089 PrototypeIterator::START_AT_RECEIVER);
6090 !iter.IsAtEnd(); iter.Advance()) {
6091 if (!iter.GetCurrent()->IsJSObject()) return false;
6092 JSObject* curr = JSObject::cast(iter.GetCurrent());
6093 int enum_length = curr->map()->EnumLength();
6094 if (enum_length == kInvalidEnumCacheSentinel) return false;
6095 if (curr->IsAccessCheckNeeded()) return false;
6096 DCHECK(!curr->HasNamedInterceptor());
6097 DCHECK(!curr->HasIndexedInterceptor());
6098 if (curr->NumberOfEnumElements() > 0) return false;
6099 if (curr != this && enum_length != 0) return false;
6105 static bool FilterKey(Object* key, PropertyAttributes filter) {
6106 if ((filter & SYMBOLIC) && key->IsSymbol()) {
6110 if ((filter & PRIVATE_SYMBOL) &&
6111 key->IsSymbol() && Symbol::cast(key)->is_private()) {
6115 if ((filter & STRING) && !key->IsSymbol()) {
6123 int Map::NumberOfDescribedProperties(DescriptorFlag which,
6124 PropertyAttributes filter) {
6126 DescriptorArray* descs = instance_descriptors();
6127 int limit = which == ALL_DESCRIPTORS
6128 ? descs->number_of_descriptors()
6129 : NumberOfOwnDescriptors();
6130 for (int i = 0; i < limit; i++) {
6131 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
6132 !FilterKey(descs->GetKey(i), filter)) {
6140 int Map::NextFreePropertyIndex() {
6142 int number_of_own_descriptors = NumberOfOwnDescriptors();
6143 DescriptorArray* descs = instance_descriptors();
6144 for (int i = 0; i < number_of_own_descriptors; i++) {
6145 PropertyDetails details = descs->GetDetails(i);
6146 if (details.location() == kField) {
6147 int candidate = details.field_index() + details.field_width_in_words();
6148 if (candidate > free_index) free_index = candidate;
6155 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
6156 int len = array->length();
6157 for (int i = 0; i < len; i++) {
6158 Object* e = array->get(i);
6159 if (!(e->IsName() || e->IsNumber())) return false;
6165 static Handle<FixedArray> ReduceFixedArrayTo(
6166 Handle<FixedArray> array, int length) {
6167 DCHECK(array->length() >= length);
6168 if (array->length() == length) return array;
6170 Handle<FixedArray> new_array =
6171 array->GetIsolate()->factory()->NewFixedArray(length);
6172 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i));
6177 Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object,
6178 bool cache_result) {
6179 Isolate* isolate = object->GetIsolate();
6180 if (object->HasFastProperties()) {
6181 int own_property_count = object->map()->EnumLength();
6182 // If the enum length of the given map is set to kInvalidEnumCache, this
6183 // means that the map itself has never used the present enum cache. The
6184 // first step to using the cache is to set the enum length of the map by
6185 // counting the number of own descriptors that are not DONT_ENUM or
6187 if (own_property_count == kInvalidEnumCacheSentinel) {
6188 own_property_count = object->map()->NumberOfDescribedProperties(
6189 OWN_DESCRIPTORS, DONT_SHOW);
6191 DCHECK(own_property_count == object->map()->NumberOfDescribedProperties(
6192 OWN_DESCRIPTORS, DONT_SHOW));
6195 if (object->map()->instance_descriptors()->HasEnumCache()) {
6196 DescriptorArray* desc = object->map()->instance_descriptors();
6197 Handle<FixedArray> keys(desc->GetEnumCache(), isolate);
6199 // In case the number of properties required in the enum are actually
6200 // present, we can reuse the enum cache. Otherwise, this means that the
6201 // enum cache was generated for a previous (smaller) version of the
6202 // Descriptor Array. In that case we regenerate the enum cache.
6203 if (own_property_count <= keys->length()) {
6204 if (cache_result) object->map()->SetEnumLength(own_property_count);
6205 isolate->counters()->enum_cache_hits()->Increment();
6206 return ReduceFixedArrayTo(keys, own_property_count);
6210 Handle<Map> map(object->map());
6212 if (map->instance_descriptors()->IsEmpty()) {
6213 isolate->counters()->enum_cache_hits()->Increment();
6214 if (cache_result) map->SetEnumLength(0);
6215 return isolate->factory()->empty_fixed_array();
6218 isolate->counters()->enum_cache_misses()->Increment();
6220 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(
6221 own_property_count);
6222 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(
6223 own_property_count);
6225 Handle<DescriptorArray> descs =
6226 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
6228 int size = map->NumberOfOwnDescriptors();
6231 for (int i = 0; i < size; i++) {
6232 PropertyDetails details = descs->GetDetails(i);
6233 Object* key = descs->GetKey(i);
6234 if (!(details.IsDontEnum() || key->IsSymbol())) {
6235 storage->set(index, key);
6236 if (!indices.is_null()) {
6237 if (details.type() != DATA) {
6238 indices = Handle<FixedArray>();
6240 FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
6241 int load_by_field_index = field_index.GetLoadByFieldIndex();
6242 indices->set(index, Smi::FromInt(load_by_field_index));
6248 DCHECK(index == storage->length());
6250 Handle<FixedArray> bridge_storage =
6251 isolate->factory()->NewFixedArray(
6252 DescriptorArray::kEnumCacheBridgeLength);
6253 DescriptorArray* desc = object->map()->instance_descriptors();
6254 desc->SetEnumCache(*bridge_storage,
6256 indices.is_null() ? Object::cast(Smi::FromInt(0))
6257 : Object::cast(*indices));
6259 object->map()->SetEnumLength(own_property_count);
6262 } else if (object->IsGlobalObject()) {
6263 Handle<GlobalDictionary> dictionary(object->global_dictionary());
6264 int length = dictionary->NumberOfEnumElements();
6266 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6268 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6269 dictionary->CopyEnumKeysTo(*storage);
6272 Handle<NameDictionary> dictionary(object->property_dictionary());
6273 int length = dictionary->NumberOfEnumElements();
6275 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6277 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6278 dictionary->CopyEnumKeysTo(*storage);
6284 MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object,
6285 KeyCollectionType type) {
6286 USE(ContainsOnlyValidKeys);
6287 Isolate* isolate = object->GetIsolate();
6288 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
6289 Handle<JSFunction> arguments_function(
6290 JSFunction::cast(isolate->sloppy_arguments_map()->GetConstructor()));
6292 PrototypeIterator::WhereToEnd end = type == OWN_ONLY
6293 ? PrototypeIterator::END_AT_NON_HIDDEN
6294 : PrototypeIterator::END_AT_NULL;
6295 // Only collect keys if access is permitted.
6296 for (PrototypeIterator iter(isolate, object,
6297 PrototypeIterator::START_AT_RECEIVER);
6298 !iter.IsAtEnd(end); iter.Advance()) {
6299 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) {
6300 Handle<JSProxy> proxy(JSProxy::cast(*PrototypeIterator::GetCurrent(iter)),
6302 Handle<Object> args[] = { proxy };
6303 Handle<Object> names;
6304 ASSIGN_RETURN_ON_EXCEPTION(
6306 Execution::Call(isolate,
6307 isolate->proxy_enumerate(),
6312 ASSIGN_RETURN_ON_EXCEPTION(
6314 FixedArray::AddKeysFromArrayLike(
6315 content, Handle<JSObject>::cast(names)),
6320 Handle<JSObject> current =
6321 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
6323 // Check access rights if required.
6324 if (current->IsAccessCheckNeeded() && !isolate->MayAccess(current)) {
6325 if (iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
6326 isolate->ReportFailedAccessCheck(current);
6327 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, FixedArray);
6332 // Compute the element keys.
6333 Handle<FixedArray> element_keys =
6334 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
6335 current->GetEnumElementKeys(*element_keys);
6336 ASSIGN_RETURN_ON_EXCEPTION(
6338 FixedArray::UnionOfKeys(content, element_keys),
6340 DCHECK(ContainsOnlyValidKeys(content));
6342 // Add the element keys from the interceptor.
6343 if (current->HasIndexedInterceptor()) {
6344 Handle<JSObject> result;
6345 if (JSObject::GetKeysForIndexedInterceptor(
6346 current, object).ToHandle(&result)) {
6347 ASSIGN_RETURN_ON_EXCEPTION(
6349 FixedArray::AddKeysFromArrayLike(content, result),
6352 DCHECK(ContainsOnlyValidKeys(content));
6355 // We can cache the computed property keys if access checks are
6356 // not needed and no interceptors are involved.
6358 // We do not use the cache if the object has elements and
6359 // therefore it does not make sense to cache the property names
6360 // for arguments objects. Arguments objects will always have
6362 // Wrapped strings have elements, but don't have an elements
6363 // array or dictionary. So the fast inline test for whether to
6364 // use the cache says yes, so we should not create a cache.
6365 bool cache_enum_keys =
6366 ((current->map()->GetConstructor() != *arguments_function) &&
6367 !current->IsJSValue() && !current->IsAccessCheckNeeded() &&
6368 !current->HasNamedInterceptor() && !current->HasIndexedInterceptor());
6369 // Compute the property keys and cache them if possible.
6370 ASSIGN_RETURN_ON_EXCEPTION(
6372 FixedArray::UnionOfKeys(
6373 content, JSObject::GetEnumPropertyKeys(current, cache_enum_keys)),
6375 DCHECK(ContainsOnlyValidKeys(content));
6377 // Add the non-symbol property keys from the interceptor.
6378 if (current->HasNamedInterceptor()) {
6379 Handle<JSObject> result;
6380 if (JSObject::GetKeysForNamedInterceptor(
6381 current, object).ToHandle(&result)) {
6382 ASSIGN_RETURN_ON_EXCEPTION(
6383 isolate, content, FixedArray::AddKeysFromArrayLike(
6384 content, result, FixedArray::NON_SYMBOL_KEYS),
6387 DCHECK(ContainsOnlyValidKeys(content));
6394 bool Map::DictionaryElementsInPrototypeChainOnly() {
6395 if (IsDictionaryElementsKind(elements_kind())) {
6399 for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) {
6400 // Be conservative, don't walk into proxies.
6401 if (iter.GetCurrent()->IsJSProxy()) return true;
6402 // String wrappers have non-configurable, non-writable elements.
6403 if (iter.GetCurrent()->IsStringWrapper()) return true;
6404 JSObject* current = JSObject::cast(iter.GetCurrent());
6406 if (current->HasDictionaryElements() &&
6407 current->element_dictionary()->requires_slow_elements()) {
6411 if (current->HasSlowArgumentsElements()) {
6412 FixedArray* parameter_map = FixedArray::cast(current->elements());
6413 Object* arguments = parameter_map->get(1);
6414 if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) {
6424 MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object,
6426 Handle<Object> getter,
6427 Handle<Object> setter,
6428 PropertyAttributes attributes) {
6429 Isolate* isolate = object->GetIsolate();
6431 LookupIterator it = LookupIterator::PropertyOrElement(
6432 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6434 if (it.state() == LookupIterator::ACCESS_CHECK) {
6435 if (!it.HasAccess()) {
6436 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6437 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6438 return isolate->factory()->undefined_value();
6443 // Ignore accessors on typed arrays.
6444 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6445 return it.factory()->undefined_value();
6448 Handle<Object> old_value = isolate->factory()->the_hole_value();
6449 bool is_observed = object->map()->is_observed() &&
6450 !isolate->IsInternallyUsedPropertyName(name);
6451 bool preexists = false;
6453 CHECK(GetPropertyAttributes(&it).IsJust());
6454 preexists = it.IsFound();
6455 if (preexists && (it.state() == LookupIterator::DATA ||
6456 it.GetAccessors()->IsAccessorInfo())) {
6457 old_value = GetProperty(&it).ToHandleChecked();
6461 DCHECK(getter->IsSpecFunction() || getter->IsUndefined() || getter->IsNull());
6462 DCHECK(setter->IsSpecFunction() || setter->IsUndefined() || setter->IsNull());
6463 // At least one of the accessors needs to be a new value.
6464 DCHECK(!getter->IsNull() || !setter->IsNull());
6465 if (!getter->IsNull()) {
6466 it.TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes);
6468 if (!setter->IsNull()) {
6469 it.TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes);
6473 // Make sure the top context isn't changed.
6474 AssertNoContextChange ncc(isolate);
6475 const char* type = preexists ? "reconfigure" : "add";
6476 RETURN_ON_EXCEPTION(
6477 isolate, EnqueueChangeRecord(object, type, name, old_value), Object);
6480 return isolate->factory()->undefined_value();
6484 MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object,
6485 Handle<AccessorInfo> info) {
6486 Isolate* isolate = object->GetIsolate();
6487 Handle<Name> name(Name::cast(info->name()), isolate);
6489 LookupIterator it = LookupIterator::PropertyOrElement(
6490 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6492 // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that
6493 // the FailedAccessCheckCallbackFunction doesn't throw an exception.
6495 // TODO(verwaest): Force throw an exception if the callback doesn't, so we can
6496 // remove reliance on default return values.
6497 if (it.state() == LookupIterator::ACCESS_CHECK) {
6498 if (!it.HasAccess()) {
6499 isolate->ReportFailedAccessCheck(object);
6500 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6501 return it.factory()->undefined_value();
6506 // Ignore accessors on typed arrays.
6507 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6508 return it.factory()->undefined_value();
6511 CHECK(GetPropertyAttributes(&it).IsJust());
6513 // ES5 forbids turning a property into an accessor if it's not
6514 // configurable. See 8.6.1 (Table 5).
6515 if (it.IsFound() && !it.IsConfigurable()) {
6516 return it.factory()->undefined_value();
6519 it.TransitionToAccessorPair(info, info->property_attributes());
6525 MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object,
6527 AccessorComponent component) {
6528 Isolate* isolate = object->GetIsolate();
6530 // Make sure that the top context does not change when doing callbacks or
6531 // interceptor calls.
6532 AssertNoContextChange ncc(isolate);
6534 LookupIterator it = LookupIterator::PropertyOrElement(
6535 isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
6537 for (; it.IsFound(); it.Next()) {
6538 switch (it.state()) {
6539 case LookupIterator::INTERCEPTOR:
6540 case LookupIterator::NOT_FOUND:
6541 case LookupIterator::TRANSITION:
6544 case LookupIterator::ACCESS_CHECK:
6545 if (it.HasAccess()) continue;
6546 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6547 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6548 return isolate->factory()->undefined_value();
6550 case LookupIterator::JSPROXY:
6551 return isolate->factory()->undefined_value();
6553 case LookupIterator::INTEGER_INDEXED_EXOTIC:
6554 return isolate->factory()->undefined_value();
6555 case LookupIterator::DATA:
6557 case LookupIterator::ACCESSOR: {
6558 Handle<Object> maybe_pair = it.GetAccessors();
6559 if (maybe_pair->IsAccessorPair()) {
6561 AccessorPair::cast(*maybe_pair)->GetComponent(component),
6568 return isolate->factory()->undefined_value();
6572 Object* JSObject::SlowReverseLookup(Object* value) {
6573 if (HasFastProperties()) {
6574 int number_of_own_descriptors = map()->NumberOfOwnDescriptors();
6575 DescriptorArray* descs = map()->instance_descriptors();
6576 bool value_is_number = value->IsNumber();
6577 for (int i = 0; i < number_of_own_descriptors; i++) {
6578 if (descs->GetType(i) == DATA) {
6579 FieldIndex field_index = FieldIndex::ForDescriptor(map(), i);
6580 if (IsUnboxedDoubleField(field_index)) {
6581 if (value_is_number) {
6582 double property = RawFastDoublePropertyAt(field_index);
6583 if (property == value->Number()) {
6584 return descs->GetKey(i);
6588 Object* property = RawFastPropertyAt(field_index);
6589 if (field_index.is_double()) {
6590 DCHECK(property->IsMutableHeapNumber());
6591 if (value_is_number && property->Number() == value->Number()) {
6592 return descs->GetKey(i);
6594 } else if (property == value) {
6595 return descs->GetKey(i);
6598 } else if (descs->GetType(i) == DATA_CONSTANT) {
6599 if (descs->GetConstant(i) == value) {
6600 return descs->GetKey(i);
6604 return GetHeap()->undefined_value();
6605 } else if (IsGlobalObject()) {
6606 return global_dictionary()->SlowReverseLookup(value);
6608 return property_dictionary()->SlowReverseLookup(value);
6613 Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) {
6614 Isolate* isolate = map->GetIsolate();
6615 Handle<Map> result =
6616 isolate->factory()->NewMap(map->instance_type(), instance_size);
6617 Handle<Object> prototype(map->prototype(), isolate);
6618 Map::SetPrototype(result, prototype);
6619 result->set_constructor_or_backpointer(map->GetConstructor());
6620 result->set_bit_field(map->bit_field());
6621 result->set_bit_field2(map->bit_field2());
6622 int new_bit_field3 = map->bit_field3();
6623 new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true);
6624 new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0);
6625 new_bit_field3 = EnumLengthBits::update(new_bit_field3,
6626 kInvalidEnumCacheSentinel);
6627 new_bit_field3 = Deprecated::update(new_bit_field3, false);
6628 if (!map->is_dictionary_map()) {
6629 new_bit_field3 = IsUnstable::update(new_bit_field3, false);
6631 new_bit_field3 = Counter::update(new_bit_field3, kRetainingCounterStart);
6632 result->set_bit_field3(new_bit_field3);
6637 Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode,
6638 const char* reason) {
6639 DCHECK(!fast_map->is_dictionary_map());
6641 Isolate* isolate = fast_map->GetIsolate();
6642 Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(),
6644 bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined();
6645 Handle<NormalizedMapCache> cache;
6646 if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache);
6648 Handle<Map> new_map;
6649 if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) {
6651 if (FLAG_verify_heap) new_map->DictionaryMapVerify();
6653 #ifdef ENABLE_SLOW_DCHECKS
6654 if (FLAG_enable_slow_asserts) {
6655 // The cached map should match newly created normalized map bit-by-bit,
6656 // except for the code cache, which can contain some ics which can be
6657 // applied to the shared map, dependent code and weak cell cache.
6658 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode);
6660 if (new_map->is_prototype_map()) {
6661 // For prototype maps, the PrototypeInfo is not copied.
6662 DCHECK(memcmp(fresh->address(), new_map->address(),
6663 kTransitionsOrPrototypeInfoOffset) == 0);
6664 DCHECK(fresh->raw_transitions() == Smi::FromInt(0));
6665 STATIC_ASSERT(kDescriptorsOffset ==
6666 kTransitionsOrPrototypeInfoOffset + kPointerSize);
6667 DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset),
6668 HeapObject::RawField(*new_map, kDescriptorsOffset),
6669 kCodeCacheOffset - kDescriptorsOffset) == 0);
6671 DCHECK(memcmp(fresh->address(), new_map->address(),
6672 Map::kCodeCacheOffset) == 0);
6674 STATIC_ASSERT(Map::kDependentCodeOffset ==
6675 Map::kCodeCacheOffset + kPointerSize);
6676 STATIC_ASSERT(Map::kWeakCellCacheOffset ==
6677 Map::kDependentCodeOffset + kPointerSize);
6678 int offset = Map::kWeakCellCacheOffset + kPointerSize;
6679 DCHECK(memcmp(fresh->address() + offset,
6680 new_map->address() + offset,
6681 Map::kSize - offset) == 0);
6685 new_map = Map::CopyNormalized(fast_map, mode);
6687 cache->Set(fast_map, new_map);
6688 isolate->counters()->normalized_maps()->Increment();
6691 if (FLAG_trace_maps) {
6692 PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n",
6693 reinterpret_cast<void*>(*fast_map),
6694 reinterpret_cast<void*>(*new_map), reason);
6698 fast_map->NotifyLeafMapLayoutChange();
6703 Handle<Map> Map::CopyNormalized(Handle<Map> map,
6704 PropertyNormalizationMode mode) {
6705 int new_instance_size = map->instance_size();
6706 if (mode == CLEAR_INOBJECT_PROPERTIES) {
6707 new_instance_size -= map->GetInObjectProperties() * kPointerSize;
6710 Handle<Map> result = RawCopy(map, new_instance_size);
6712 if (mode != CLEAR_INOBJECT_PROPERTIES) {
6713 result->SetInObjectProperties(map->GetInObjectProperties());
6716 result->set_dictionary_map(true);
6717 result->set_migration_target(false);
6720 if (FLAG_verify_heap) result->DictionaryMapVerify();
6727 Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) {
6728 Handle<Map> result = RawCopy(map, map->instance_size());
6730 // Please note instance_type and instance_size are set when allocated.
6731 result->SetInObjectProperties(map->GetInObjectProperties());
6732 result->set_unused_property_fields(map->unused_property_fields());
6734 result->ClearCodeCache(map->GetHeap());
6735 map->NotifyLeafMapLayoutChange();
6740 Handle<Map> Map::ShareDescriptor(Handle<Map> map,
6741 Handle<DescriptorArray> descriptors,
6742 Descriptor* descriptor) {
6743 // Sanity check. This path is only to be taken if the map owns its descriptor
6744 // array, implying that its NumberOfOwnDescriptors equals the number of
6745 // descriptors in the descriptor array.
6746 DCHECK(map->NumberOfOwnDescriptors() ==
6747 map->instance_descriptors()->number_of_descriptors());
6749 Handle<Map> result = CopyDropDescriptors(map);
6750 Handle<Name> name = descriptor->GetKey();
6752 // Ensure there's space for the new descriptor in the shared descriptor array.
6753 if (descriptors->NumberOfSlackDescriptors() == 0) {
6754 int old_size = descriptors->number_of_descriptors();
6755 if (old_size == 0) {
6756 descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1);
6758 int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors);
6759 EnsureDescriptorSlack(map, slack);
6760 descriptors = handle(map->instance_descriptors());
6764 Handle<LayoutDescriptor> layout_descriptor =
6765 FLAG_unbox_double_fields
6766 ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails())
6767 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
6770 DisallowHeapAllocation no_gc;
6771 descriptors->Append(descriptor);
6772 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6775 DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1);
6776 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6785 void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) {
6786 if (FLAG_trace_maps) {
6787 PrintF("[TraceMaps: %s from= %p to= %p name= ", what,
6788 reinterpret_cast<void*>(from), reinterpret_cast<void*>(to));
6789 name->NameShortPrint();
6796 void Map::TraceAllTransitions(Map* map) {
6797 Object* transitions = map->raw_transitions();
6798 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
6799 for (int i = -0; i < num_transitions; ++i) {
6800 Map* target = TransitionArray::GetTarget(transitions, i);
6801 Name* key = TransitionArray::GetKey(transitions, i);
6802 Map::TraceTransition("Transition", map, target, key);
6803 Map::TraceAllTransitions(target);
6807 #endif // TRACE_MAPS
6810 void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child,
6811 Handle<Name> name, SimpleTransitionFlag flag) {
6812 parent->set_owns_descriptors(false);
6813 if (parent->is_prototype_map()) {
6814 DCHECK(child->is_prototype_map());
6816 Map::TraceTransition("NoTransition", *parent, *child, *name);
6819 TransitionArray::Insert(parent, name, child, flag);
6821 Map::TraceTransition("Transition", *parent, *child, *name);
6827 Handle<Map> Map::CopyReplaceDescriptors(
6828 Handle<Map> map, Handle<DescriptorArray> descriptors,
6829 Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag,
6830 MaybeHandle<Name> maybe_name, const char* reason,
6831 SimpleTransitionFlag simple_flag) {
6832 DCHECK(descriptors->IsSortedNoDuplicates());
6834 Handle<Map> result = CopyDropDescriptors(map);
6836 if (!map->is_prototype_map()) {
6837 if (flag == INSERT_TRANSITION &&
6838 TransitionArray::CanHaveMoreTransitions(map)) {
6839 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6842 CHECK(maybe_name.ToHandle(&name));
6843 ConnectTransition(map, result, name, simple_flag);
6845 int length = descriptors->number_of_descriptors();
6846 for (int i = 0; i < length; i++) {
6847 descriptors->SetRepresentation(i, Representation::Tagged());
6848 if (descriptors->GetDetails(i).type() == DATA) {
6849 descriptors->SetValue(i, HeapType::Any());
6852 result->InitializeDescriptors(*descriptors,
6853 LayoutDescriptor::FastPointerLayout());
6856 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6859 if (FLAG_trace_maps &&
6860 // Mirror conditions above that did not call ConnectTransition().
6861 (map->is_prototype_map() ||
6862 !(flag == INSERT_TRANSITION &&
6863 TransitionArray::CanHaveMoreTransitions(map)))) {
6864 PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n",
6865 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result),
6874 // Since this method is used to rewrite an existing transition tree, it can
6875 // always insert transitions without checking.
6876 Handle<Map> Map::CopyInstallDescriptors(
6877 Handle<Map> map, int new_descriptor, Handle<DescriptorArray> descriptors,
6878 Handle<LayoutDescriptor> full_layout_descriptor) {
6879 DCHECK(descriptors->IsSortedNoDuplicates());
6881 Handle<Map> result = CopyDropDescriptors(map);
6883 result->set_instance_descriptors(*descriptors);
6884 result->SetNumberOfOwnDescriptors(new_descriptor + 1);
6886 int unused_property_fields = map->unused_property_fields();
6887 PropertyDetails details = descriptors->GetDetails(new_descriptor);
6888 if (details.location() == kField) {
6889 unused_property_fields = map->unused_property_fields() - 1;
6890 if (unused_property_fields < 0) {
6891 unused_property_fields += JSObject::kFieldsAdded;
6894 result->set_unused_property_fields(unused_property_fields);
6896 if (FLAG_unbox_double_fields) {
6897 Handle<LayoutDescriptor> layout_descriptor =
6898 LayoutDescriptor::AppendIfFastOrUseFull(map, details,
6899 full_layout_descriptor);
6900 result->set_layout_descriptor(*layout_descriptor);
6902 // TODO(ishell): remove these checks from VERIFY_HEAP mode.
6903 if (FLAG_verify_heap) {
6904 CHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6907 SLOW_DCHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6909 result->set_visitor_id(StaticVisitorBase::GetVisitorId(*result));
6912 Handle<Name> name = handle(descriptors->GetKey(new_descriptor));
6913 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6919 Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind,
6920 TransitionFlag flag) {
6921 Map* maybe_elements_transition_map = NULL;
6922 if (flag == INSERT_TRANSITION) {
6923 maybe_elements_transition_map = map->ElementsTransitionMap();
6924 DCHECK(maybe_elements_transition_map == NULL ||
6925 (maybe_elements_transition_map->elements_kind() ==
6926 DICTIONARY_ELEMENTS &&
6927 kind == DICTIONARY_ELEMENTS));
6928 DCHECK(!IsFastElementsKind(kind) ||
6929 IsMoreGeneralElementsKindTransition(map->elements_kind(), kind));
6930 DCHECK(kind != map->elements_kind());
6933 bool insert_transition = flag == INSERT_TRANSITION &&
6934 TransitionArray::CanHaveMoreTransitions(map) &&
6935 maybe_elements_transition_map == NULL;
6937 if (insert_transition) {
6938 Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind");
6939 new_map->set_elements_kind(kind);
6941 Isolate* isolate = map->GetIsolate();
6942 Handle<Name> name = isolate->factory()->elements_transition_symbol();
6943 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6947 // Create a new free-floating map only if we are not allowed to store it.
6948 Handle<Map> new_map = Copy(map, "CopyAsElementsKind");
6949 new_map->set_elements_kind(kind);
6954 Handle<Map> Map::CopyForObserved(Handle<Map> map) {
6955 DCHECK(!map->is_observed());
6957 Isolate* isolate = map->GetIsolate();
6959 bool insert_transition =
6960 TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map();
6962 if (insert_transition) {
6963 Handle<Map> new_map = CopyForTransition(map, "CopyForObserved");
6964 new_map->set_is_observed();
6966 Handle<Name> name = isolate->factory()->observed_symbol();
6967 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6971 // Create a new free-floating map only if we are not allowed to store it.
6972 Handle<Map> new_map = Map::Copy(map, "CopyForObserved");
6973 new_map->set_is_observed();
6978 Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) {
6979 DCHECK(!map->is_prototype_map());
6980 Handle<Map> new_map = CopyDropDescriptors(map);
6982 if (map->owns_descriptors()) {
6983 // In case the map owned its own descriptors, share the descriptors and
6984 // transfer ownership to the new map.
6985 // The properties did not change, so reuse descriptors.
6986 new_map->InitializeDescriptors(map->instance_descriptors(),
6987 map->GetLayoutDescriptor());
6989 // In case the map did not own its own descriptors, a split is forced by
6990 // copying the map; creating a new descriptor array cell.
6991 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6992 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
6993 Handle<DescriptorArray> new_descriptors =
6994 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
6995 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6997 new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor);
7001 if (FLAG_trace_maps) {
7002 PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n",
7003 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map),
7012 Handle<Map> Map::Copy(Handle<Map> map, const char* reason) {
7013 Handle<DescriptorArray> descriptors(map->instance_descriptors());
7014 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
7015 Handle<DescriptorArray> new_descriptors =
7016 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
7017 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
7019 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7020 OMIT_TRANSITION, MaybeHandle<Name>(), reason,
7021 SPECIAL_TRANSITION);
7025 Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) {
7027 Copy(handle(isolate->object_function()->initial_map()), "MapCreate");
7029 // Check that we do not overflow the instance size when adding the extra
7030 // inobject properties. If the instance size overflows, we allocate as many
7031 // properties as we can as inobject properties.
7032 int max_extra_properties =
7033 (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2;
7035 if (inobject_properties > max_extra_properties) {
7036 inobject_properties = max_extra_properties;
7039 int new_instance_size =
7040 JSObject::kHeaderSize + kPointerSize * inobject_properties;
7042 // Adjust the map with the extra inobject properties.
7043 copy->SetInObjectProperties(inobject_properties);
7044 copy->set_unused_property_fields(inobject_properties);
7045 copy->set_instance_size(new_instance_size);
7046 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
7051 Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map,
7052 PropertyAttributes attrs_to_add,
7053 Handle<Symbol> transition_marker,
7054 const char* reason) {
7055 int num_descriptors = map->NumberOfOwnDescriptors();
7056 Isolate* isolate = map->GetIsolate();
7057 Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes(
7058 handle(map->instance_descriptors(), isolate), num_descriptors,
7060 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
7062 Handle<Map> new_map = CopyReplaceDescriptors(
7063 map, new_desc, new_layout_descriptor, INSERT_TRANSITION,
7064 transition_marker, reason, SPECIAL_TRANSITION);
7065 new_map->set_is_extensible(false);
7066 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
7071 Handle<Map> Map::FixProxy(Handle<Map> map, InstanceType type, int size) {
7072 DCHECK(type == JS_OBJECT_TYPE || type == JS_FUNCTION_TYPE);
7073 DCHECK(map->IsJSProxyMap());
7075 Isolate* isolate = map->GetIsolate();
7077 // Allocate fresh map.
7078 // TODO(rossberg): Once we optimize proxies, cache these maps.
7079 Handle<Map> new_map = isolate->factory()->NewMap(type, size);
7081 Handle<Object> prototype(map->prototype(), isolate);
7082 Map::SetPrototype(new_map, prototype);
7084 map->NotifyLeafMapLayoutChange();
7090 bool DescriptorArray::CanHoldValue(int descriptor, Object* value) {
7091 PropertyDetails details = GetDetails(descriptor);
7092 switch (details.type()) {
7094 return value->FitsRepresentation(details.representation()) &&
7095 GetFieldType(descriptor)->NowContains(value);
7098 DCHECK(GetConstant(descriptor) != value ||
7099 value->FitsRepresentation(details.representation()));
7100 return GetConstant(descriptor) == value;
7103 case ACCESSOR_CONSTANT:
7113 Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor,
7114 Handle<Object> value) {
7115 // Dictionaries can store any property value.
7116 if (map->is_dictionary_map()) return map;
7118 // Migrate to the newest map before storing the property.
7121 Handle<DescriptorArray> descriptors(map->instance_descriptors());
7123 if (descriptors->CanHoldValue(descriptor, *value)) return map;
7125 Isolate* isolate = map->GetIsolate();
7126 PropertyAttributes attributes =
7127 descriptors->GetDetails(descriptor).attributes();
7128 Representation representation = value->OptimalRepresentation();
7129 Handle<HeapType> type = value->OptimalType(isolate, representation);
7131 return ReconfigureProperty(map, descriptor, kData, attributes, representation,
7136 Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name,
7137 Handle<Object> value,
7138 PropertyAttributes attributes,
7139 StoreFromKeyed store_mode) {
7140 // Dictionary maps can always have additional data properties.
7141 if (map->is_dictionary_map()) return map;
7143 // Migrate to the newest map before storing the property.
7146 Map* maybe_transition =
7147 TransitionArray::SearchTransition(*map, kData, *name, attributes);
7148 if (maybe_transition != NULL) {
7149 Handle<Map> transition(maybe_transition);
7150 int descriptor = transition->LastAdded();
7152 DCHECK_EQ(attributes, transition->instance_descriptors()
7153 ->GetDetails(descriptor)
7156 return Map::PrepareForDataProperty(transition, descriptor, value);
7159 TransitionFlag flag = INSERT_TRANSITION;
7160 MaybeHandle<Map> maybe_map;
7161 if (value->IsJSFunction()) {
7162 maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag);
7163 } else if (!map->TooManyFastProperties(store_mode)) {
7164 Isolate* isolate = name->GetIsolate();
7165 Representation representation = value->OptimalRepresentation();
7166 Handle<HeapType> type = value->OptimalType(isolate, representation);
7168 Map::CopyWithField(map, name, type, attributes, representation, flag);
7172 if (!maybe_map.ToHandle(&result)) {
7174 if (FLAG_trace_maps) {
7175 Vector<char> name_buffer = Vector<char>::New(100);
7176 name->NameShortPrint(name_buffer);
7177 Vector<char> buffer = Vector<char>::New(128);
7178 SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start());
7179 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start());
7182 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES,
7183 "TooManyFastProperties");
7190 Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor,
7192 PropertyAttributes attributes) {
7193 // Dictionaries have to be reconfigured in-place.
7194 DCHECK(!map->is_dictionary_map());
7196 if (!map->GetBackPointer()->IsMap()) {
7197 // There is no benefit from reconstructing transition tree for maps without
7199 return CopyGeneralizeAllRepresentations(
7200 map, descriptor, FORCE_FIELD, kind, attributes,
7201 "GenAll_AttributesMismatchProtoMap");
7204 if (FLAG_trace_generalization) {
7205 map->PrintReconfiguration(stdout, descriptor, kind, attributes);
7208 Isolate* isolate = map->GetIsolate();
7209 Handle<Map> new_map = ReconfigureProperty(
7210 map, descriptor, kind, attributes, Representation::None(),
7211 HeapType::None(isolate), FORCE_FIELD);
7216 Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map,
7218 AccessorComponent component,
7219 Handle<Object> accessor,
7220 PropertyAttributes attributes) {
7221 Isolate* isolate = name->GetIsolate();
7223 // Dictionary maps can always have additional data properties.
7224 if (map->is_dictionary_map()) return map;
7226 // Migrate to the newest map before transitioning to the new property.
7229 PropertyNormalizationMode mode = map->is_prototype_map()
7230 ? KEEP_INOBJECT_PROPERTIES
7231 : CLEAR_INOBJECT_PROPERTIES;
7233 Map* maybe_transition =
7234 TransitionArray::SearchTransition(*map, kAccessor, *name, attributes);
7235 if (maybe_transition != NULL) {
7236 Handle<Map> transition(maybe_transition, isolate);
7237 DescriptorArray* descriptors = transition->instance_descriptors();
7238 int descriptor = transition->LastAdded();
7239 DCHECK(descriptors->GetKey(descriptor)->Equals(*name));
7241 DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind());
7242 DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes());
7244 Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate);
7245 if (!maybe_pair->IsAccessorPair()) {
7246 return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair");
7249 Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair);
7250 if (pair->get(component) != *accessor) {
7251 return Map::Normalize(map, mode, "TransitionToDifferentAccessor");
7257 Handle<AccessorPair> pair;
7258 DescriptorArray* old_descriptors = map->instance_descriptors();
7259 int descriptor = old_descriptors->SearchWithCache(*name, *map);
7260 if (descriptor != DescriptorArray::kNotFound) {
7261 if (descriptor != map->LastAdded()) {
7262 return Map::Normalize(map, mode, "AccessorsOverwritingNonLast");
7264 PropertyDetails old_details = old_descriptors->GetDetails(descriptor);
7265 if (old_details.type() != ACCESSOR_CONSTANT) {
7266 return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors");
7269 if (old_details.attributes() != attributes) {
7270 return Map::Normalize(map, mode, "AccessorsWithAttributes");
7273 Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate);
7274 if (!maybe_pair->IsAccessorPair()) {
7275 return Map::Normalize(map, mode, "AccessorsOverwritingNonPair");
7278 Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component);
7279 if (current == *accessor) return map;
7281 if (!current->IsTheHole()) {
7282 return Map::Normalize(map, mode, "AccessorsOverwritingAccessors");
7285 pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair));
7286 } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors ||
7287 map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) {
7288 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors");
7290 pair = isolate->factory()->NewAccessorPair();
7293 pair->set(component, *accessor);
7294 TransitionFlag flag = INSERT_TRANSITION;
7295 AccessorConstantDescriptor new_desc(name, pair, attributes);
7296 return Map::CopyInsertDescriptor(map, &new_desc, flag);
7300 Handle<Map> Map::CopyAddDescriptor(Handle<Map> map,
7301 Descriptor* descriptor,
7302 TransitionFlag flag) {
7303 Handle<DescriptorArray> descriptors(map->instance_descriptors());
7305 // Ensure the key is unique.
7306 descriptor->KeyToUniqueName();
7308 if (flag == INSERT_TRANSITION && map->owns_descriptors() &&
7309 TransitionArray::CanHaveMoreTransitions(map)) {
7310 return ShareDescriptor(map, descriptors, descriptor);
7313 int nof = map->NumberOfOwnDescriptors();
7314 Handle<DescriptorArray> new_descriptors =
7315 DescriptorArray::CopyUpTo(descriptors, nof, 1);
7316 new_descriptors->Append(descriptor);
7318 Handle<LayoutDescriptor> new_layout_descriptor =
7319 FLAG_unbox_double_fields
7320 ? LayoutDescriptor::New(map, new_descriptors, nof + 1)
7321 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
7323 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7324 flag, descriptor->GetKey(), "CopyAddDescriptor",
7325 SIMPLE_PROPERTY_TRANSITION);
7329 Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map,
7330 Descriptor* descriptor,
7331 TransitionFlag flag) {
7332 Handle<DescriptorArray> old_descriptors(map->instance_descriptors());
7334 // Ensure the key is unique.
7335 descriptor->KeyToUniqueName();
7337 // We replace the key if it is already present.
7338 int index = old_descriptors->SearchWithCache(*descriptor->GetKey(), *map);
7339 if (index != DescriptorArray::kNotFound) {
7340 return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag);
7342 return CopyAddDescriptor(map, descriptor, flag);
7346 Handle<DescriptorArray> DescriptorArray::CopyUpTo(
7347 Handle<DescriptorArray> desc,
7348 int enumeration_index,
7350 return DescriptorArray::CopyUpToAddAttributes(
7351 desc, enumeration_index, NONE, slack);
7355 Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes(
7356 Handle<DescriptorArray> desc,
7357 int enumeration_index,
7358 PropertyAttributes attributes,
7360 if (enumeration_index + slack == 0) {
7361 return desc->GetIsolate()->factory()->empty_descriptor_array();
7364 int size = enumeration_index;
7366 Handle<DescriptorArray> descriptors =
7367 DescriptorArray::Allocate(desc->GetIsolate(), size, slack);
7368 DescriptorArray::WhitenessWitness witness(*descriptors);
7370 if (attributes != NONE) {
7371 for (int i = 0; i < size; ++i) {
7372 Object* value = desc->GetValue(i);
7373 Name* key = desc->GetKey(i);
7374 PropertyDetails details = desc->GetDetails(i);
7375 // Bulk attribute changes never affect private properties.
7376 if (!key->IsSymbol() || !Symbol::cast(key)->is_private()) {
7377 int mask = DONT_DELETE | DONT_ENUM;
7378 // READ_ONLY is an invalid attribute for JS setters/getters.
7379 if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) {
7382 details = details.CopyAddAttributes(
7383 static_cast<PropertyAttributes>(attributes & mask));
7385 Descriptor inner_desc(
7386 handle(key), handle(value, desc->GetIsolate()), details);
7387 descriptors->Set(i, &inner_desc, witness);
7390 for (int i = 0; i < size; ++i) {
7391 descriptors->CopyFrom(i, *desc, witness);
7395 if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort();
7401 Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map,
7402 Handle<DescriptorArray> descriptors,
7403 Descriptor* descriptor,
7404 int insertion_index,
7405 TransitionFlag flag) {
7406 // Ensure the key is unique.
7407 descriptor->KeyToUniqueName();
7409 Handle<Name> key = descriptor->GetKey();
7410 DCHECK(*key == descriptors->GetKey(insertion_index));
7412 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
7413 descriptors, map->NumberOfOwnDescriptors());
7415 new_descriptors->Replace(insertion_index, descriptor);
7416 Handle<LayoutDescriptor> new_layout_descriptor = LayoutDescriptor::New(
7417 map, new_descriptors, new_descriptors->number_of_descriptors());
7419 SimpleTransitionFlag simple_flag =
7420 (insertion_index == descriptors->number_of_descriptors() - 1)
7421 ? SIMPLE_PROPERTY_TRANSITION
7422 : PROPERTY_TRANSITION;
7423 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7424 flag, key, "CopyReplaceDescriptor",
7429 void Map::UpdateCodeCache(Handle<Map> map,
7431 Handle<Code> code) {
7432 Isolate* isolate = map->GetIsolate();
7433 HandleScope scope(isolate);
7434 // Allocate the code cache if not present.
7435 if (map->code_cache()->IsFixedArray()) {
7436 Handle<Object> result = isolate->factory()->NewCodeCache();
7437 map->set_code_cache(*result);
7440 // Update the code cache.
7441 Handle<CodeCache> code_cache(CodeCache::cast(map->code_cache()), isolate);
7442 CodeCache::Update(code_cache, name, code);
7446 Object* Map::FindInCodeCache(Name* name, Code::Flags flags) {
7447 // Do a lookup if a code cache exists.
7448 if (!code_cache()->IsFixedArray()) {
7449 return CodeCache::cast(code_cache())->Lookup(name, flags);
7451 return GetHeap()->undefined_value();
7456 int Map::IndexInCodeCache(Object* name, Code* code) {
7457 // Get the internal index if a code cache exists.
7458 if (!code_cache()->IsFixedArray()) {
7459 return CodeCache::cast(code_cache())->GetIndex(name, code);
7465 void Map::RemoveFromCodeCache(Name* name, Code* code, int index) {
7466 // No GC is supposed to happen between a call to IndexInCodeCache and
7467 // RemoveFromCodeCache so the code cache must be there.
7468 DCHECK(!code_cache()->IsFixedArray());
7469 CodeCache::cast(code_cache())->RemoveByIndex(name, code, index);
7473 void CodeCache::Update(
7474 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7475 // The number of monomorphic stubs for normal load/store/call IC's can grow to
7476 // a large number and therefore they need to go into a hash table. They are
7477 // used to load global properties from cells.
7478 if (code->type() == Code::NORMAL) {
7479 // Make sure that a hash table is allocated for the normal load code cache.
7480 if (code_cache->normal_type_cache()->IsUndefined()) {
7481 Handle<Object> result =
7482 CodeCacheHashTable::New(code_cache->GetIsolate(),
7483 CodeCacheHashTable::kInitialSize);
7484 code_cache->set_normal_type_cache(*result);
7486 UpdateNormalTypeCache(code_cache, name, code);
7488 DCHECK(code_cache->default_cache()->IsFixedArray());
7489 UpdateDefaultCache(code_cache, name, code);
7494 void CodeCache::UpdateDefaultCache(
7495 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7496 // When updating the default code cache we disregard the type encoded in the
7497 // flags. This allows call constant stubs to overwrite call field
7499 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
7501 // First check whether we can update existing code cache without
7503 Handle<FixedArray> cache = handle(code_cache->default_cache());
7504 int length = cache->length();
7506 DisallowHeapAllocation no_alloc;
7507 int deleted_index = -1;
7508 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7509 Object* key = cache->get(i);
7510 if (key->IsNull()) {
7511 if (deleted_index < 0) deleted_index = i;
7514 if (key->IsUndefined()) {
7515 if (deleted_index >= 0) i = deleted_index;
7516 cache->set(i + kCodeCacheEntryNameOffset, *name);
7517 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7520 if (name->Equals(Name::cast(key))) {
7522 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
7523 if (Code::RemoveTypeFromFlags(found) == flags) {
7524 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7530 // Reached the end of the code cache. If there were deleted
7531 // elements, reuse the space for the first of them.
7532 if (deleted_index >= 0) {
7533 cache->set(deleted_index + kCodeCacheEntryNameOffset, *name);
7534 cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code);
7539 // Extend the code cache with some new entries (at least one). Must be a
7540 // multiple of the entry size.
7541 Isolate* isolate = cache->GetIsolate();
7542 int new_length = length + (length >> 1) + kCodeCacheEntrySize;
7543 new_length = new_length - new_length % kCodeCacheEntrySize;
7544 DCHECK((new_length % kCodeCacheEntrySize) == 0);
7545 cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length);
7547 // Add the (name, code) pair to the new cache.
7548 cache->set(length + kCodeCacheEntryNameOffset, *name);
7549 cache->set(length + kCodeCacheEntryCodeOffset, *code);
7550 code_cache->set_default_cache(*cache);
7554 void CodeCache::UpdateNormalTypeCache(
7555 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7556 // Adding a new entry can cause a new cache to be allocated.
7557 Handle<CodeCacheHashTable> cache(
7558 CodeCacheHashTable::cast(code_cache->normal_type_cache()));
7559 Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code);
7560 code_cache->set_normal_type_cache(*new_cache);
7564 Object* CodeCache::Lookup(Name* name, Code::Flags flags) {
7565 Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags));
7566 if (result->IsCode()) {
7567 if (Code::cast(result)->flags() == flags) return result;
7568 return GetHeap()->undefined_value();
7570 return LookupNormalTypeCache(name, flags);
7574 Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) {
7575 FixedArray* cache = default_cache();
7576 int length = cache->length();
7577 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7578 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
7579 // Skip deleted elements.
7580 if (key->IsNull()) continue;
7581 if (key->IsUndefined()) return key;
7582 if (name->Equals(Name::cast(key))) {
7583 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
7584 if (Code::RemoveTypeFromFlags(code->flags()) == flags) {
7589 return GetHeap()->undefined_value();
7593 Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) {
7594 if (!normal_type_cache()->IsUndefined()) {
7595 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7596 return cache->Lookup(name, flags);
7598 return GetHeap()->undefined_value();
7603 int CodeCache::GetIndex(Object* name, Code* code) {
7604 if (code->type() == Code::NORMAL) {
7605 if (normal_type_cache()->IsUndefined()) return -1;
7606 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7607 return cache->GetIndex(Name::cast(name), code->flags());
7610 FixedArray* array = default_cache();
7611 int len = array->length();
7612 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
7613 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
7619 void CodeCache::RemoveByIndex(Object* name, Code* code, int index) {
7620 if (code->type() == Code::NORMAL) {
7621 DCHECK(!normal_type_cache()->IsUndefined());
7622 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7623 DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index);
7624 cache->RemoveByIndex(index);
7626 FixedArray* array = default_cache();
7627 DCHECK(array->length() >= index && array->get(index)->IsCode());
7628 // Use null instead of undefined for deleted elements to distinguish
7629 // deleted elements from unused elements. This distinction is used
7630 // when looking up in the cache and when updating the cache.
7631 DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset);
7632 array->set_null(index - 1); // Name.
7633 array->set_null(index); // Code.
7638 // The key in the code cache hash table consists of the property name and the
7639 // code object. The actual match is on the name and the code flags. If a key
7640 // is created using the flags and not a code object it can only be used for
7641 // lookup not to create a new entry.
7642 class CodeCacheHashTableKey : public HashTableKey {
7644 CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags)
7645 : name_(name), flags_(flags), code_() { }
7647 CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code)
7648 : name_(name), flags_(code->flags()), code_(code) { }
7650 bool IsMatch(Object* other) override {
7651 if (!other->IsFixedArray()) return false;
7652 FixedArray* pair = FixedArray::cast(other);
7653 Name* name = Name::cast(pair->get(0));
7654 Code::Flags flags = Code::cast(pair->get(1))->flags();
7655 if (flags != flags_) {
7658 return name_->Equals(name);
7661 static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) {
7662 return name->Hash() ^ flags;
7665 uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); }
7667 uint32_t HashForObject(Object* obj) override {
7668 FixedArray* pair = FixedArray::cast(obj);
7669 Name* name = Name::cast(pair->get(0));
7670 Code* code = Code::cast(pair->get(1));
7671 return NameFlagsHashHelper(name, code->flags());
7674 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7675 Handle<Code> code = code_.ToHandleChecked();
7676 Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2);
7677 pair->set(0, *name_);
7678 pair->set(1, *code);
7685 // TODO(jkummerow): We should be able to get by without this.
7686 MaybeHandle<Code> code_;
7690 Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) {
7691 DisallowHeapAllocation no_alloc;
7692 CodeCacheHashTableKey key(handle(name), flags);
7693 int entry = FindEntry(&key);
7694 if (entry == kNotFound) return GetHeap()->undefined_value();
7695 return get(EntryToIndex(entry) + 1);
7699 Handle<CodeCacheHashTable> CodeCacheHashTable::Put(
7700 Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) {
7701 CodeCacheHashTableKey key(name, code);
7703 Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key);
7705 int entry = new_cache->FindInsertionEntry(key.Hash());
7706 Handle<Object> k = key.AsHandle(cache->GetIsolate());
7708 new_cache->set(EntryToIndex(entry), *k);
7709 new_cache->set(EntryToIndex(entry) + 1, *code);
7710 new_cache->ElementAdded();
7715 int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) {
7716 DisallowHeapAllocation no_alloc;
7717 CodeCacheHashTableKey key(handle(name), flags);
7718 int entry = FindEntry(&key);
7719 return (entry == kNotFound) ? -1 : entry;
7723 void CodeCacheHashTable::RemoveByIndex(int index) {
7725 Heap* heap = GetHeap();
7726 set(EntryToIndex(index), heap->the_hole_value());
7727 set(EntryToIndex(index) + 1, heap->the_hole_value());
7732 void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache,
7733 MapHandleList* maps,
7735 Handle<Code> code) {
7736 Isolate* isolate = code_cache->GetIsolate();
7737 if (code_cache->cache()->IsUndefined()) {
7738 Handle<PolymorphicCodeCacheHashTable> result =
7739 PolymorphicCodeCacheHashTable::New(
7741 PolymorphicCodeCacheHashTable::kInitialSize);
7742 code_cache->set_cache(*result);
7744 // This entry shouldn't be contained in the cache yet.
7745 DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache())
7746 ->Lookup(maps, flags)->IsUndefined());
7748 Handle<PolymorphicCodeCacheHashTable> hash_table =
7749 handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache()));
7750 Handle<PolymorphicCodeCacheHashTable> new_cache =
7751 PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code);
7752 code_cache->set_cache(*new_cache);
7756 Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps,
7757 Code::Flags flags) {
7758 if (!cache()->IsUndefined()) {
7759 PolymorphicCodeCacheHashTable* hash_table =
7760 PolymorphicCodeCacheHashTable::cast(cache());
7761 return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate());
7763 return GetIsolate()->factory()->undefined_value();
7768 // Despite their name, object of this class are not stored in the actual
7769 // hash table; instead they're temporarily used for lookups. It is therefore
7770 // safe to have a weak (non-owning) pointer to a MapList as a member field.
7771 class PolymorphicCodeCacheHashTableKey : public HashTableKey {
7773 // Callers must ensure that |maps| outlives the newly constructed object.
7774 PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags)
7776 code_flags_(code_flags) {}
7778 bool IsMatch(Object* other) override {
7779 MapHandleList other_maps(kDefaultListAllocationSize);
7781 FromObject(other, &other_flags, &other_maps);
7782 if (code_flags_ != other_flags) return false;
7783 if (maps_->length() != other_maps.length()) return false;
7784 // Compare just the hashes first because it's faster.
7785 int this_hash = MapsHashHelper(maps_, code_flags_);
7786 int other_hash = MapsHashHelper(&other_maps, other_flags);
7787 if (this_hash != other_hash) return false;
7789 // Full comparison: for each map in maps_, look for an equivalent map in
7790 // other_maps. This implementation is slow, but probably good enough for
7791 // now because the lists are short (<= 4 elements currently).
7792 for (int i = 0; i < maps_->length(); ++i) {
7793 bool match_found = false;
7794 for (int j = 0; j < other_maps.length(); ++j) {
7795 if (*(maps_->at(i)) == *(other_maps.at(j))) {
7800 if (!match_found) return false;
7805 static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) {
7806 uint32_t hash = code_flags;
7807 for (int i = 0; i < maps->length(); ++i) {
7808 hash ^= maps->at(i)->Hash();
7813 uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); }
7815 uint32_t HashForObject(Object* obj) override {
7816 MapHandleList other_maps(kDefaultListAllocationSize);
7818 FromObject(obj, &other_flags, &other_maps);
7819 return MapsHashHelper(&other_maps, other_flags);
7822 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7823 // The maps in |maps_| must be copied to a newly allocated FixedArray,
7824 // both because the referenced MapList is short-lived, and because C++
7825 // objects can't be stored in the heap anyway.
7826 Handle<FixedArray> list =
7827 isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1);
7828 list->set(0, Smi::FromInt(code_flags_));
7829 for (int i = 0; i < maps_->length(); ++i) {
7830 list->set(i + 1, *maps_->at(i));
7836 static MapHandleList* FromObject(Object* obj,
7838 MapHandleList* maps) {
7839 FixedArray* list = FixedArray::cast(obj);
7841 *code_flags = Smi::cast(list->get(0))->value();
7842 for (int i = 1; i < list->length(); ++i) {
7843 maps->Add(Handle<Map>(Map::cast(list->get(i))));
7848 MapHandleList* maps_; // weak.
7850 static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1;
7854 Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps,
7856 DisallowHeapAllocation no_alloc;
7857 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7858 int entry = FindEntry(&key);
7859 if (entry == kNotFound) return GetHeap()->undefined_value();
7860 return get(EntryToIndex(entry) + 1);
7864 Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put(
7865 Handle<PolymorphicCodeCacheHashTable> hash_table,
7866 MapHandleList* maps,
7868 Handle<Code> code) {
7869 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7870 Handle<PolymorphicCodeCacheHashTable> cache =
7871 EnsureCapacity(hash_table, 1, &key);
7872 int entry = cache->FindInsertionEntry(key.Hash());
7874 Handle<Object> obj = key.AsHandle(hash_table->GetIsolate());
7875 cache->set(EntryToIndex(entry), *obj);
7876 cache->set(EntryToIndex(entry) + 1, *code);
7877 cache->ElementAdded();
7882 void FixedArray::Shrink(int new_length) {
7883 DCHECK(0 <= new_length && new_length <= length());
7884 if (new_length < length()) {
7885 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(
7886 this, length() - new_length);
7891 MaybeHandle<FixedArray> FixedArray::AddKeysFromArrayLike(
7892 Handle<FixedArray> content, Handle<JSObject> array, KeyFilter filter) {
7893 DCHECK(array->IsJSArray() || array->HasSloppyArgumentsElements());
7894 ElementsAccessor* accessor = array->GetElementsAccessor();
7895 Handle<FixedArray> result =
7896 accessor->AddElementsToFixedArray(array, content, filter);
7898 #ifdef ENABLE_SLOW_DCHECKS
7899 if (FLAG_enable_slow_asserts) {
7900 DisallowHeapAllocation no_allocation;
7901 for (int i = 0; i < result->length(); i++) {
7902 Object* current = result->get(i);
7903 DCHECK(current->IsNumber() || current->IsName());
7911 MaybeHandle<FixedArray> FixedArray::UnionOfKeys(Handle<FixedArray> first,
7912 Handle<FixedArray> second) {
7913 if (second->length() == 0) return first;
7914 if (first->length() == 0) return second;
7915 Isolate* isolate = first->GetIsolate();
7916 Handle<FixedArray> result =
7917 isolate->factory()->NewFixedArray(first->length() + second->length());
7918 for (int i = 0; i < first->length(); i++) {
7919 result->set(i, first->get(i));
7921 int pos = first->length();
7922 for (int j = 0; j < second->length(); j++) {
7923 Object* current = second->get(j);
7925 for (i = 0; i < first->length(); i++) {
7926 if (current->KeyEquals(first->get(i))) break;
7928 if (i == first->length()) {
7929 result->set(pos++, current);
7933 result->Shrink(pos);
7938 void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
7939 DisallowHeapAllocation no_gc;
7940 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
7941 for (int index = 0; index < len; index++) {
7942 dest->set(dest_pos+index, get(pos+index), mode);
7948 bool FixedArray::IsEqualTo(FixedArray* other) {
7949 if (length() != other->length()) return false;
7950 for (int i = 0 ; i < length(); ++i) {
7951 if (get(i) != other->get(i)) return false;
7959 void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index,
7960 Handle<HeapObject> value) {
7961 DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything.
7962 Handle<WeakCell> cell =
7963 value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value))
7964 : array->GetIsolate()->factory()->NewWeakCell(value);
7965 Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell);
7966 if (FLAG_trace_weak_arrays) {
7967 PrintF("[WeakFixedArray: storing at index %d ]\n", index);
7969 array->set_last_used_index(index);
7974 Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array,
7975 Handle<HeapObject> value,
7976 int* assigned_index) {
7977 Handle<WeakFixedArray> array =
7978 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray())
7979 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null())
7980 : Handle<WeakFixedArray>::cast(maybe_array);
7981 // Try to store the new entry if there's room. Optimize for consecutive
7983 int first_index = array->last_used_index();
7984 int length = array->Length();
7986 for (int i = first_index;;) {
7987 if (array->IsEmptySlot((i))) {
7988 WeakFixedArray::Set(array, i, value);
7989 if (assigned_index != NULL) *assigned_index = i;
7992 if (FLAG_trace_weak_arrays) {
7993 PrintF("[WeakFixedArray: searching for free slot]\n");
7995 i = (i + 1) % length;
7996 if (i == first_index) break;
8000 // No usable slot found, grow the array.
8001 int new_length = length == 0 ? 1 : length + (length >> 1) + 4;
8002 Handle<WeakFixedArray> new_array =
8003 Allocate(array->GetIsolate(), new_length, array);
8004 if (FLAG_trace_weak_arrays) {
8005 PrintF("[WeakFixedArray: growing to size %d ]\n", new_length);
8007 WeakFixedArray::Set(new_array, length, value);
8008 if (assigned_index != NULL) *assigned_index = length;
8013 template <class CompactionCallback>
8014 void WeakFixedArray::Compact() {
8015 FixedArray* array = FixedArray::cast(this);
8016 int new_length = kFirstIndex;
8017 for (int i = kFirstIndex; i < array->length(); i++) {
8018 Object* element = array->get(i);
8019 if (element->IsSmi()) continue;
8020 if (WeakCell::cast(element)->cleared()) continue;
8021 Object* value = WeakCell::cast(element)->value();
8022 CompactionCallback::Callback(value, i - kFirstIndex,
8023 new_length - kFirstIndex);
8024 array->set(new_length++, element);
8026 array->Shrink(new_length);
8027 set_last_used_index(0);
8031 void WeakFixedArray::Iterator::Reset(Object* maybe_array) {
8032 if (maybe_array->IsWeakFixedArray()) {
8033 list_ = WeakFixedArray::cast(maybe_array);
8036 last_used_index_ = list_->last_used_index();
8042 void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value,
8045 DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map());
8046 Map* map = Map::cast(value);
8047 DCHECK(map->prototype_info()->IsPrototypeInfo());
8048 PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info());
8049 DCHECK_EQ(old_index, proto_info->registry_slot());
8050 proto_info->set_registry_slot(new_index);
8054 template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>();
8056 WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>();
8059 bool WeakFixedArray::Remove(Handle<HeapObject> value) {
8060 if (Length() == 0) return false;
8061 // Optimize for the most recently added element to be removed again.
8062 int first_index = last_used_index();
8063 for (int i = first_index;;) {
8064 if (Get(i) == *value) {
8066 // Users of WeakFixedArray should make sure that there are no duplicates.
8069 i = (i + 1) % Length();
8070 if (i == first_index) return false;
8077 Handle<WeakFixedArray> WeakFixedArray::Allocate(
8078 Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) {
8080 Handle<FixedArray> result =
8081 isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex);
8083 if (!initialize_from.is_null()) {
8084 DCHECK(initialize_from->Length() <= size);
8085 Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from);
8086 // Copy the entries without compacting, since the PrototypeInfo relies on
8087 // the index of the entries not to change.
8088 while (index < raw_source->length()) {
8089 result->set(index, raw_source->get(index));
8093 while (index < result->length()) {
8094 result->set(index, Smi::FromInt(0));
8097 return Handle<WeakFixedArray>::cast(result);
8101 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj,
8103 int length = array->Length();
8104 array = EnsureSpace(array, length + 1);
8105 if (mode == kReloadLengthAfterAllocation) {
8106 DCHECK(array->Length() <= length);
8107 length = array->Length();
8109 array->Set(length, *obj);
8110 array->SetLength(length + 1);
8115 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1,
8116 Handle<Object> obj2, AddMode mode) {
8117 int length = array->Length();
8118 array = EnsureSpace(array, length + 2);
8119 if (mode == kReloadLengthAfterAllocation) {
8120 length = array->Length();
8122 array->Set(length, *obj1);
8123 array->Set(length + 1, *obj2);
8124 array->SetLength(length + 2);
8129 Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) {
8130 int capacity = array->length();
8131 bool empty = (capacity == 0);
8132 if (capacity < kFirstIndex + length) {
8133 Isolate* isolate = array->GetIsolate();
8134 int new_capacity = kFirstIndex + length;
8135 new_capacity = new_capacity + Max(new_capacity / 2, 2);
8136 int grow_by = new_capacity - capacity;
8137 array = Handle<ArrayList>::cast(
8138 isolate->factory()->CopyFixedArrayAndGrow(array, grow_by));
8139 if (empty) array->SetLength(0);
8145 Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate,
8146 int number_of_descriptors,
8148 DCHECK(0 <= number_of_descriptors);
8149 Factory* factory = isolate->factory();
8150 // Do not use DescriptorArray::cast on incomplete object.
8151 int size = number_of_descriptors + slack;
8152 if (size == 0) return factory->empty_descriptor_array();
8153 // Allocate the array of keys.
8154 Handle<FixedArray> result = factory->NewFixedArray(LengthFor(size));
8156 result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors));
8157 result->set(kEnumCacheIndex, Smi::FromInt(0));
8158 return Handle<DescriptorArray>::cast(result);
8162 void DescriptorArray::ClearEnumCache() {
8163 set(kEnumCacheIndex, Smi::FromInt(0));
8167 void DescriptorArray::Replace(int index, Descriptor* descriptor) {
8168 descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index));
8169 Set(index, descriptor);
8173 void DescriptorArray::SetEnumCache(FixedArray* bridge_storage,
8174 FixedArray* new_cache,
8175 Object* new_index_cache) {
8176 DCHECK(bridge_storage->length() >= kEnumCacheBridgeLength);
8177 DCHECK(new_index_cache->IsSmi() || new_index_cache->IsFixedArray());
8179 DCHECK(!HasEnumCache() || new_cache->length() > GetEnumCache()->length());
8180 FixedArray::cast(bridge_storage)->
8181 set(kEnumCacheBridgeCacheIndex, new_cache);
8182 FixedArray::cast(bridge_storage)->
8183 set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache);
8184 set(kEnumCacheIndex, bridge_storage);
8188 void DescriptorArray::CopyFrom(int index, DescriptorArray* src,
8189 const WhitenessWitness& witness) {
8190 Object* value = src->GetValue(index);
8191 PropertyDetails details = src->GetDetails(index);
8192 Descriptor desc(handle(src->GetKey(index)),
8193 handle(value, src->GetIsolate()),
8195 Set(index, &desc, witness);
8199 // We need the whiteness witness since sort will reshuffle the entries in the
8200 // descriptor array. If the descriptor array were to be black, the shuffling
8201 // would move a slot that was already recorded as pointing into an evacuation
8202 // candidate. This would result in missing updates upon evacuation.
8203 void DescriptorArray::Sort() {
8204 // In-place heap sort.
8205 int len = number_of_descriptors();
8206 // Reset sorting since the descriptor array might contain invalid pointers.
8207 for (int i = 0; i < len; ++i) SetSortedKey(i, i);
8208 // Bottom-up max-heap construction.
8209 // Index of the last node with children
8210 const int max_parent_index = (len / 2) - 1;
8211 for (int i = max_parent_index; i >= 0; --i) {
8212 int parent_index = i;
8213 const uint32_t parent_hash = GetSortedKey(i)->Hash();
8214 while (parent_index <= max_parent_index) {
8215 int child_index = 2 * parent_index + 1;
8216 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8217 if (child_index + 1 < len) {
8218 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8219 if (right_child_hash > child_hash) {
8221 child_hash = right_child_hash;
8224 if (child_hash <= parent_hash) break;
8225 SwapSortedKeys(parent_index, child_index);
8226 // Now element at child_index could be < its children.
8227 parent_index = child_index; // parent_hash remains correct.
8231 // Extract elements and create sorted array.
8232 for (int i = len - 1; i > 0; --i) {
8233 // Put max element at the back of the array.
8234 SwapSortedKeys(0, i);
8235 // Shift down the new top element.
8236 int parent_index = 0;
8237 const uint32_t parent_hash = GetSortedKey(parent_index)->Hash();
8238 const int max_parent_index = (i / 2) - 1;
8239 while (parent_index <= max_parent_index) {
8240 int child_index = parent_index * 2 + 1;
8241 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8242 if (child_index + 1 < i) {
8243 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8244 if (right_child_hash > child_hash) {
8246 child_hash = right_child_hash;
8249 if (child_hash <= parent_hash) break;
8250 SwapSortedKeys(parent_index, child_index);
8251 parent_index = child_index;
8254 DCHECK(IsSortedNoDuplicates());
8258 Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) {
8259 Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair();
8260 copy->set_getter(pair->getter());
8261 copy->set_setter(pair->setter());
8266 Object* AccessorPair::GetComponent(AccessorComponent component) {
8267 Object* accessor = get(component);
8268 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor;
8272 Handle<DeoptimizationInputData> DeoptimizationInputData::New(
8273 Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) {
8274 return Handle<DeoptimizationInputData>::cast(
8275 isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count),
8280 Handle<DeoptimizationOutputData> DeoptimizationOutputData::New(
8282 int number_of_deopt_points,
8283 PretenureFlag pretenure) {
8284 Handle<FixedArray> result;
8285 if (number_of_deopt_points == 0) {
8286 result = isolate->factory()->empty_fixed_array();
8288 result = isolate->factory()->NewFixedArray(
8289 LengthOfFixedArray(number_of_deopt_points), pretenure);
8291 return Handle<DeoptimizationOutputData>::cast(result);
8295 int HandlerTable::LookupRange(int pc_offset, int* stack_depth_out,
8296 CatchPrediction* prediction_out) {
8297 int innermost_handler = -1, innermost_start = -1;
8298 for (int i = 0; i < length(); i += kRangeEntrySize) {
8299 int start_offset = Smi::cast(get(i + kRangeStartIndex))->value();
8300 int end_offset = Smi::cast(get(i + kRangeEndIndex))->value();
8301 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
8302 int handler_offset = HandlerOffsetField::decode(handler_field);
8303 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
8304 int stack_depth = Smi::cast(get(i + kRangeDepthIndex))->value();
8305 if (pc_offset > start_offset && pc_offset <= end_offset) {
8306 DCHECK_NE(start_offset, innermost_start);
8307 if (start_offset < innermost_start) continue;
8308 innermost_handler = handler_offset;
8309 innermost_start = start_offset;
8310 *stack_depth_out = stack_depth;
8311 if (prediction_out) *prediction_out = prediction;
8314 return innermost_handler;
8318 // TODO(turbofan): Make sure table is sorted and use binary search.
8319 int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) {
8320 for (int i = 0; i < length(); i += kReturnEntrySize) {
8321 int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
8322 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
8323 if (pc_offset == return_offset) {
8324 if (prediction_out) {
8325 *prediction_out = HandlerPredictionField::decode(handler_field);
8327 return HandlerOffsetField::decode(handler_field);
8335 bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
8336 if (IsEmpty()) return other->IsEmpty();
8337 if (other->IsEmpty()) return false;
8338 if (length() != other->length()) return false;
8339 for (int i = 0; i < length(); ++i) {
8340 if (get(i) != other->get(i)) return false;
8347 bool String::LooksValid() {
8348 if (!GetIsolate()->heap()->Contains(this)) return false;
8355 bool AreDigits(const uint8_t* s, int from, int to) {
8356 for (int i = from; i < to; i++) {
8357 if (s[i] < '0' || s[i] > '9') return false;
8364 int ParseDecimalInteger(const uint8_t* s, int from, int to) {
8365 DCHECK(to - from < 10); // Overflow is not possible.
8367 int d = s[from] - '0';
8369 for (int i = from + 1; i < to; i++) {
8370 d = 10 * d + (s[i] - '0');
8380 Handle<Object> String::ToNumber(Handle<String> subject) {
8381 Isolate* const isolate = subject->GetIsolate();
8383 // Flatten {subject} string first.
8384 subject = String::Flatten(subject);
8386 // Fast array index case.
8388 if (subject->AsArrayIndex(&index)) {
8389 return isolate->factory()->NewNumberFromUint(index);
8392 // Fast case: short integer or some sorts of junk values.
8393 if (subject->IsSeqOneByteString()) {
8394 int len = subject->length();
8395 if (len == 0) return handle(Smi::FromInt(0), isolate);
8397 DisallowHeapAllocation no_gc;
8398 uint8_t const* data = Handle<SeqOneByteString>::cast(subject)->GetChars();
8399 bool minus = (data[0] == '-');
8400 int start_pos = (minus ? 1 : 0);
8402 if (start_pos == len) {
8403 return isolate->factory()->nan_value();
8404 } else if (data[start_pos] > '9') {
8405 // Fast check for a junk value. A valid string may start from a
8406 // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit
8407 // or the 'I' character ('Infinity'). All of that have codes not greater
8408 // than '9' except 'I' and .
8409 if (data[start_pos] != 'I' && data[start_pos] != 0xa0) {
8410 return isolate->factory()->nan_value();
8412 } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) {
8413 // The maximal/minimal smi has 10 digits. If the string has less digits
8414 // we know it will fit into the smi-data type.
8415 int d = ParseDecimalInteger(data, start_pos, len);
8417 if (d == 0) return isolate->factory()->minus_zero_value();
8419 } else if (!subject->HasHashCode() && len <= String::kMaxArrayIndexSize &&
8420 (len == 1 || data[0] != '0')) {
8421 // String hash is not calculated yet but all the data are present.
8422 // Update the hash field to speed up sequential convertions.
8423 uint32_t hash = StringHasher::MakeArrayIndexHash(d, len);
8425 subject->Hash(); // Force hash calculation.
8426 DCHECK_EQ(static_cast<int>(subject->hash_field()),
8427 static_cast<int>(hash));
8429 subject->set_hash_field(hash);
8431 return handle(Smi::FromInt(d), isolate);
8436 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_BINARY;
8437 return isolate->factory()->NewNumber(
8438 StringToDouble(isolate->unicode_cache(), subject, flags));
8442 String::FlatContent String::GetFlatContent() {
8443 DCHECK(!AllowHeapAllocation::IsAllowed());
8444 int length = this->length();
8445 StringShape shape(this);
8446 String* string = this;
8448 if (shape.representation_tag() == kConsStringTag) {
8449 ConsString* cons = ConsString::cast(string);
8450 if (cons->second()->length() != 0) {
8451 return FlatContent();
8453 string = cons->first();
8454 shape = StringShape(string);
8456 if (shape.representation_tag() == kSlicedStringTag) {
8457 SlicedString* slice = SlicedString::cast(string);
8458 offset = slice->offset();
8459 string = slice->parent();
8460 shape = StringShape(string);
8461 DCHECK(shape.representation_tag() != kConsStringTag &&
8462 shape.representation_tag() != kSlicedStringTag);
8464 if (shape.encoding_tag() == kOneByteStringTag) {
8465 const uint8_t* start;
8466 if (shape.representation_tag() == kSeqStringTag) {
8467 start = SeqOneByteString::cast(string)->GetChars();
8469 start = ExternalOneByteString::cast(string)->GetChars();
8471 return FlatContent(start + offset, length);
8473 DCHECK(shape.encoding_tag() == kTwoByteStringTag);
8475 if (shape.representation_tag() == kSeqStringTag) {
8476 start = SeqTwoByteString::cast(string)->GetChars();
8478 start = ExternalTwoByteString::cast(string)->GetChars();
8480 return FlatContent(start + offset, length);
8485 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8486 RobustnessFlag robust_flag,
8487 int offset, int length,
8488 int* length_return) {
8489 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8490 return base::SmartArrayPointer<char>(NULL);
8492 // Negative length means the to the end of the string.
8493 if (length < 0) length = kMaxInt - offset;
8495 // Compute the size of the UTF-8 string. Start at the specified offset.
8496 StringCharacterStream stream(this, offset);
8497 int character_position = offset;
8499 int last = unibrow::Utf16::kNoPreviousCharacter;
8500 while (stream.HasMore() && character_position++ < offset + length) {
8501 uint16_t character = stream.GetNext();
8502 utf8_bytes += unibrow::Utf8::Length(character, last);
8506 if (length_return) {
8507 *length_return = utf8_bytes;
8510 char* result = NewArray<char>(utf8_bytes + 1);
8512 // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
8513 stream.Reset(this, offset);
8514 character_position = offset;
8515 int utf8_byte_position = 0;
8516 last = unibrow::Utf16::kNoPreviousCharacter;
8517 while (stream.HasMore() && character_position++ < offset + length) {
8518 uint16_t character = stream.GetNext();
8519 if (allow_nulls == DISALLOW_NULLS && character == 0) {
8522 utf8_byte_position +=
8523 unibrow::Utf8::Encode(result + utf8_byte_position, character, last);
8526 result[utf8_byte_position] = 0;
8527 return base::SmartArrayPointer<char>(result);
8531 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8532 RobustnessFlag robust_flag,
8533 int* length_return) {
8534 return ToCString(allow_nulls, robust_flag, 0, -1, length_return);
8538 const uc16* String::GetTwoByteData(unsigned start) {
8539 DCHECK(!IsOneByteRepresentationUnderneath());
8540 switch (StringShape(this).representation_tag()) {
8542 return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start);
8543 case kExternalStringTag:
8544 return ExternalTwoByteString::cast(this)->
8545 ExternalTwoByteStringGetData(start);
8546 case kSlicedStringTag: {
8547 SlicedString* slice = SlicedString::cast(this);
8548 return slice->parent()->GetTwoByteData(start + slice->offset());
8550 case kConsStringTag:
8559 base::SmartArrayPointer<uc16> String::ToWideCString(
8560 RobustnessFlag robust_flag) {
8561 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8562 return base::SmartArrayPointer<uc16>();
8564 StringCharacterStream stream(this);
8566 uc16* result = NewArray<uc16>(length() + 1);
8569 while (stream.HasMore()) {
8570 uint16_t character = stream.GetNext();
8571 result[i++] = character;
8574 return base::SmartArrayPointer<uc16>(result);
8578 const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
8579 return reinterpret_cast<uc16*>(
8580 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
8584 void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) {
8585 Relocatable* current = isolate->relocatable_top();
8586 while (current != NULL) {
8587 current->PostGarbageCollection();
8588 current = current->prev_;
8593 // Reserve space for statics needing saving and restoring.
8594 int Relocatable::ArchiveSpacePerThread() {
8595 return sizeof(Relocatable*); // NOLINT
8599 // Archive statics that are thread-local.
8600 char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
8601 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
8602 isolate->set_relocatable_top(NULL);
8603 return to + ArchiveSpacePerThread();
8607 // Restore statics that are thread-local.
8608 char* Relocatable::RestoreState(Isolate* isolate, char* from) {
8609 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
8610 return from + ArchiveSpacePerThread();
8614 char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) {
8615 Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage);
8617 return thread_storage + ArchiveSpacePerThread();
8621 void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) {
8622 Iterate(v, isolate->relocatable_top());
8626 void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) {
8627 Relocatable* current = top;
8628 while (current != NULL) {
8629 current->IterateInstance(v);
8630 current = current->prev_;
8635 FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
8636 : Relocatable(isolate),
8637 str_(str.location()),
8638 length_(str->length()) {
8639 PostGarbageCollection();
8643 FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
8644 : Relocatable(isolate),
8647 length_(input.length()),
8648 start_(input.start()) {}
8651 void FlatStringReader::PostGarbageCollection() {
8652 if (str_ == NULL) return;
8653 Handle<String> str(str_);
8654 DCHECK(str->IsFlat());
8655 DisallowHeapAllocation no_gc;
8656 // This does not actually prevent the vector from being relocated later.
8657 String::FlatContent content = str->GetFlatContent();
8658 DCHECK(content.IsFlat());
8659 is_one_byte_ = content.IsOneByte();
8661 start_ = content.ToOneByteVector().start();
8663 start_ = content.ToUC16Vector().start();
8668 void ConsStringIterator::Initialize(ConsString* cons_string, int offset) {
8669 DCHECK(cons_string != NULL);
8670 root_ = cons_string;
8672 // Force stack blown condition to trigger restart.
8674 maximum_depth_ = kStackSize + depth_;
8675 DCHECK(StackBlown());
8679 String* ConsStringIterator::Continue(int* offset_out) {
8680 DCHECK(depth_ != 0);
8681 DCHECK_EQ(0, *offset_out);
8682 bool blew_stack = StackBlown();
8683 String* string = NULL;
8684 // Get the next leaf if there is one.
8685 if (!blew_stack) string = NextLeaf(&blew_stack);
8686 // Restart search from root.
8688 DCHECK(string == NULL);
8689 string = Search(offset_out);
8691 // Ensure future calls return null immediately.
8692 if (string == NULL) Reset(NULL);
8697 String* ConsStringIterator::Search(int* offset_out) {
8698 ConsString* cons_string = root_;
8699 // Reset the stack, pushing the root string.
8702 frames_[0] = cons_string;
8703 const int consumed = consumed_;
8706 // Loop until the string is found which contains the target offset.
8707 String* string = cons_string->first();
8708 int length = string->length();
8710 if (consumed < offset + length) {
8711 // Target offset is in the left branch.
8712 // Keep going if we're still in a ConString.
8713 type = string->map()->instance_type();
8714 if ((type & kStringRepresentationMask) == kConsStringTag) {
8715 cons_string = ConsString::cast(string);
8716 PushLeft(cons_string);
8719 // Tell the stack we're done descending.
8720 AdjustMaximumDepth();
8723 // Update progress through the string.
8725 // Keep going if we're still in a ConString.
8726 string = cons_string->second();
8727 type = string->map()->instance_type();
8728 if ((type & kStringRepresentationMask) == kConsStringTag) {
8729 cons_string = ConsString::cast(string);
8730 PushRight(cons_string);
8733 // Need this to be updated for the current string.
8734 length = string->length();
8735 // Account for the possibility of an empty right leaf.
8736 // This happens only if we have asked for an offset outside the string.
8738 // Reset so future operations will return null immediately.
8742 // Tell the stack we're done descending.
8743 AdjustMaximumDepth();
8744 // Pop stack so next iteration is in correct place.
8747 DCHECK(length != 0);
8748 // Adjust return values and exit.
8749 consumed_ = offset + length;
8750 *offset_out = consumed - offset;
8758 String* ConsStringIterator::NextLeaf(bool* blew_stack) {
8760 // Tree traversal complete.
8762 *blew_stack = false;
8765 // We've lost track of higher nodes.
8771 ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)];
8772 String* string = cons_string->second();
8773 int32_t type = string->map()->instance_type();
8774 if ((type & kStringRepresentationMask) != kConsStringTag) {
8775 // Pop stack so next iteration is in correct place.
8777 int length = string->length();
8778 // Could be a flattened ConsString.
8779 if (length == 0) continue;
8780 consumed_ += length;
8783 cons_string = ConsString::cast(string);
8784 PushRight(cons_string);
8785 // Need to traverse all the way left.
8788 string = cons_string->first();
8789 type = string->map()->instance_type();
8790 if ((type & kStringRepresentationMask) != kConsStringTag) {
8791 AdjustMaximumDepth();
8792 int length = string->length();
8793 DCHECK(length != 0);
8794 consumed_ += length;
8797 cons_string = ConsString::cast(string);
8798 PushLeft(cons_string);
8806 uint16_t ConsString::ConsStringGet(int index) {
8807 DCHECK(index >= 0 && index < this->length());
8809 // Check for a flattened cons string
8810 if (second()->length() == 0) {
8811 String* left = first();
8812 return left->Get(index);
8815 String* string = String::cast(this);
8818 if (StringShape(string).IsCons()) {
8819 ConsString* cons_string = ConsString::cast(string);
8820 String* left = cons_string->first();
8821 if (left->length() > index) {
8824 index -= left->length();
8825 string = cons_string->second();
8828 return string->Get(index);
8837 uint16_t SlicedString::SlicedStringGet(int index) {
8838 return parent()->Get(offset() + index);
8842 template <typename sinkchar>
8843 void String::WriteToFlat(String* src,
8847 String* source = src;
8851 DCHECK(0 <= from && from <= to && to <= source->length());
8852 switch (StringShape(source).full_representation_tag()) {
8853 case kOneByteStringTag | kExternalStringTag: {
8854 CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from,
8858 case kTwoByteStringTag | kExternalStringTag: {
8860 ExternalTwoByteString::cast(source)->GetChars();
8866 case kOneByteStringTag | kSeqStringTag: {
8868 SeqOneByteString::cast(source)->GetChars() + from,
8872 case kTwoByteStringTag | kSeqStringTag: {
8874 SeqTwoByteString::cast(source)->GetChars() + from,
8878 case kOneByteStringTag | kConsStringTag:
8879 case kTwoByteStringTag | kConsStringTag: {
8880 ConsString* cons_string = ConsString::cast(source);
8881 String* first = cons_string->first();
8882 int boundary = first->length();
8883 if (to - boundary >= boundary - from) {
8884 // Right hand side is longer. Recurse over left.
8885 if (from < boundary) {
8886 WriteToFlat(first, sink, from, boundary);
8887 sink += boundary - from;
8893 source = cons_string->second();
8895 // Left hand side is longer. Recurse over right.
8896 if (to > boundary) {
8897 String* second = cons_string->second();
8898 // When repeatedly appending to a string, we get a cons string that
8899 // is unbalanced to the left, a list, essentially. We inline the
8900 // common case of sequential one-byte right child.
8901 if (to - boundary == 1) {
8902 sink[boundary - from] = static_cast<sinkchar>(second->Get(0));
8903 } else if (second->IsSeqOneByteString()) {
8904 CopyChars(sink + boundary - from,
8905 SeqOneByteString::cast(second)->GetChars(),
8909 sink + boundary - from,
8919 case kOneByteStringTag | kSlicedStringTag:
8920 case kTwoByteStringTag | kSlicedStringTag: {
8921 SlicedString* slice = SlicedString::cast(source);
8922 unsigned offset = slice->offset();
8923 WriteToFlat(slice->parent(), sink, from + offset, to + offset);
8932 template <typename SourceChar>
8933 static void CalculateLineEndsImpl(Isolate* isolate,
8934 List<int>* line_ends,
8935 Vector<const SourceChar> src,
8936 bool include_ending_line) {
8937 const int src_len = src.length();
8938 UnicodeCache* cache = isolate->unicode_cache();
8939 for (int i = 0; i < src_len - 1; i++) {
8940 SourceChar current = src[i];
8941 SourceChar next = src[i + 1];
8942 if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i);
8945 if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) {
8946 line_ends->Add(src_len - 1);
8947 } else if (include_ending_line) {
8948 // Even if the last line misses a line end, it is counted.
8949 line_ends->Add(src_len);
8954 Handle<FixedArray> String::CalculateLineEnds(Handle<String> src,
8955 bool include_ending_line) {
8957 // Rough estimate of line count based on a roughly estimated average
8958 // length of (unpacked) code.
8959 int line_count_estimate = src->length() >> 4;
8960 List<int> line_ends(line_count_estimate);
8961 Isolate* isolate = src->GetIsolate();
8962 { DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
8963 // Dispatch on type of strings.
8964 String::FlatContent content = src->GetFlatContent();
8965 DCHECK(content.IsFlat());
8966 if (content.IsOneByte()) {
8967 CalculateLineEndsImpl(isolate,
8969 content.ToOneByteVector(),
8970 include_ending_line);
8972 CalculateLineEndsImpl(isolate,
8974 content.ToUC16Vector(),
8975 include_ending_line);
8978 int line_count = line_ends.length();
8979 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
8980 for (int i = 0; i < line_count; i++) {
8981 array->set(i, Smi::FromInt(line_ends[i]));
8987 // Compares the contents of two strings by reading and comparing
8988 // int-sized blocks of characters.
8989 template <typename Char>
8990 static inline bool CompareRawStringContents(const Char* const a,
8991 const Char* const b,
8993 return CompareChars(a, b, length) == 0;
8997 template<typename Chars1, typename Chars2>
8998 class RawStringComparator : public AllStatic {
9000 static inline bool compare(const Chars1* a, const Chars2* b, int len) {
9001 DCHECK(sizeof(Chars1) != sizeof(Chars2));
9002 for (int i = 0; i < len; i++) {
9013 class RawStringComparator<uint16_t, uint16_t> {
9015 static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
9016 return CompareRawStringContents(a, b, len);
9022 class RawStringComparator<uint8_t, uint8_t> {
9024 static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
9025 return CompareRawStringContents(a, b, len);
9030 class StringComparator {
9033 State() : is_one_byte_(true), length_(0), buffer8_(NULL) {}
9035 void Init(String* string) {
9036 ConsString* cons_string = String::VisitFlat(this, string);
9037 iter_.Reset(cons_string);
9038 if (cons_string != NULL) {
9040 string = iter_.Next(&offset);
9041 String::VisitFlat(this, string, offset);
9045 inline void VisitOneByteString(const uint8_t* chars, int length) {
9046 is_one_byte_ = true;
9051 inline void VisitTwoByteString(const uint16_t* chars, int length) {
9052 is_one_byte_ = false;
9057 void Advance(int consumed) {
9058 DCHECK(consumed <= length_);
9060 if (length_ != consumed) {
9062 buffer8_ += consumed;
9064 buffer16_ += consumed;
9066 length_ -= consumed;
9071 String* next = iter_.Next(&offset);
9072 DCHECK_EQ(0, offset);
9073 DCHECK(next != NULL);
9074 String::VisitFlat(this, next);
9077 ConsStringIterator iter_;
9081 const uint8_t* buffer8_;
9082 const uint16_t* buffer16_;
9086 DISALLOW_COPY_AND_ASSIGN(State);
9090 inline StringComparator() {}
9092 template<typename Chars1, typename Chars2>
9093 static inline bool Equals(State* state_1, State* state_2, int to_check) {
9094 const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
9095 const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
9096 return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
9099 bool Equals(String* string_1, String* string_2) {
9100 int length = string_1->length();
9101 state_1_.Init(string_1);
9102 state_2_.Init(string_2);
9104 int to_check = Min(state_1_.length_, state_2_.length_);
9105 DCHECK(to_check > 0 && to_check <= length);
9107 if (state_1_.is_one_byte_) {
9108 if (state_2_.is_one_byte_) {
9109 is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
9111 is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
9114 if (state_2_.is_one_byte_) {
9115 is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
9117 is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
9121 if (!is_equal) return false;
9123 // Exit condition. Strings are equal.
9124 if (length == 0) return true;
9125 state_1_.Advance(to_check);
9126 state_2_.Advance(to_check);
9134 DISALLOW_COPY_AND_ASSIGN(StringComparator);
9138 bool String::SlowEquals(String* other) {
9139 DisallowHeapAllocation no_gc;
9140 // Fast check: negative check with lengths.
9142 if (len != other->length()) return false;
9143 if (len == 0) return true;
9145 // Fast check: if hash code is computed for both strings
9146 // a fast negative check can be performed.
9147 if (HasHashCode() && other->HasHashCode()) {
9148 #ifdef ENABLE_SLOW_DCHECKS
9149 if (FLAG_enable_slow_asserts) {
9150 if (Hash() != other->Hash()) {
9151 bool found_difference = false;
9152 for (int i = 0; i < len; i++) {
9153 if (Get(i) != other->Get(i)) {
9154 found_difference = true;
9158 DCHECK(found_difference);
9162 if (Hash() != other->Hash()) return false;
9165 // We know the strings are both non-empty. Compare the first chars
9166 // before we try to flatten the strings.
9167 if (this->Get(0) != other->Get(0)) return false;
9169 if (IsSeqOneByteString() && other->IsSeqOneByteString()) {
9170 const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars();
9171 const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars();
9172 return CompareRawStringContents(str1, str2, len);
9175 StringComparator comparator;
9176 return comparator.Equals(this, other);
9180 bool String::SlowEquals(Handle<String> one, Handle<String> two) {
9181 // Fast check: negative check with lengths.
9182 int one_length = one->length();
9183 if (one_length != two->length()) return false;
9184 if (one_length == 0) return true;
9186 // Fast check: if hash code is computed for both strings
9187 // a fast negative check can be performed.
9188 if (one->HasHashCode() && two->HasHashCode()) {
9189 #ifdef ENABLE_SLOW_DCHECKS
9190 if (FLAG_enable_slow_asserts) {
9191 if (one->Hash() != two->Hash()) {
9192 bool found_difference = false;
9193 for (int i = 0; i < one_length; i++) {
9194 if (one->Get(i) != two->Get(i)) {
9195 found_difference = true;
9199 DCHECK(found_difference);
9203 if (one->Hash() != two->Hash()) return false;
9206 // We know the strings are both non-empty. Compare the first chars
9207 // before we try to flatten the strings.
9208 if (one->Get(0) != two->Get(0)) return false;
9210 one = String::Flatten(one);
9211 two = String::Flatten(two);
9213 DisallowHeapAllocation no_gc;
9214 String::FlatContent flat1 = one->GetFlatContent();
9215 String::FlatContent flat2 = two->GetFlatContent();
9217 if (flat1.IsOneByte() && flat2.IsOneByte()) {
9218 return CompareRawStringContents(flat1.ToOneByteVector().start(),
9219 flat2.ToOneByteVector().start(),
9222 for (int i = 0; i < one_length; i++) {
9223 if (flat1.Get(i) != flat2.Get(i)) return false;
9230 bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) {
9231 int slen = length();
9232 // Can't check exact length equality, but we can check bounds.
9233 int str_len = str.length();
9234 if (!allow_prefix_match &&
9236 str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) {
9240 size_t remaining_in_str = static_cast<size_t>(str_len);
9241 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start());
9242 for (i = 0; i < slen && remaining_in_str > 0; i++) {
9244 uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor);
9245 DCHECK(cursor > 0 && cursor <= remaining_in_str);
9246 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
9247 if (i > slen - 1) return false;
9248 if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false;
9249 if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false;
9251 if (Get(i) != r) return false;
9253 utf8_data += cursor;
9254 remaining_in_str -= cursor;
9256 return (allow_prefix_match || i == slen) && remaining_in_str == 0;
9260 bool String::IsOneByteEqualTo(Vector<const uint8_t> str) {
9261 int slen = length();
9262 if (str.length() != slen) return false;
9263 DisallowHeapAllocation no_gc;
9264 FlatContent content = GetFlatContent();
9265 if (content.IsOneByte()) {
9266 return CompareChars(content.ToOneByteVector().start(),
9267 str.start(), slen) == 0;
9269 for (int i = 0; i < slen; i++) {
9270 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
9276 bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
9277 int slen = length();
9278 if (str.length() != slen) return false;
9279 DisallowHeapAllocation no_gc;
9280 FlatContent content = GetFlatContent();
9281 if (content.IsTwoByte()) {
9282 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
9284 for (int i = 0; i < slen; i++) {
9285 if (Get(i) != str[i]) return false;
9291 uint32_t String::ComputeAndSetHash() {
9292 // Should only be called if hash code has not yet been computed.
9293 DCHECK(!HasHashCode());
9295 // Store the hash code in the object.
9296 uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed());
9297 set_hash_field(field);
9299 // Check the hash code is there.
9300 DCHECK(HasHashCode());
9301 uint32_t result = field >> kHashShift;
9302 DCHECK(result != 0); // Ensure that the hash value of 0 is never computed.
9307 bool String::ComputeArrayIndex(uint32_t* index) {
9308 int length = this->length();
9309 if (length == 0 || length > kMaxArrayIndexSize) return false;
9310 StringCharacterStream stream(this);
9311 return StringToArrayIndex(&stream, index);
9315 bool String::SlowAsArrayIndex(uint32_t* index) {
9316 if (length() <= kMaxCachedArrayIndexLength) {
9317 Hash(); // force computation of hash code
9318 uint32_t field = hash_field();
9319 if ((field & kIsNotArrayIndexMask) != 0) return false;
9320 // Isolate the array index form the full hash field.
9321 *index = ArrayIndexValueBits::decode(field);
9324 return ComputeArrayIndex(index);
9329 Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) {
9330 int new_size, old_size;
9331 int old_length = string->length();
9332 if (old_length <= new_length) return string;
9334 if (string->IsSeqOneByteString()) {
9335 old_size = SeqOneByteString::SizeFor(old_length);
9336 new_size = SeqOneByteString::SizeFor(new_length);
9338 DCHECK(string->IsSeqTwoByteString());
9339 old_size = SeqTwoByteString::SizeFor(old_length);
9340 new_size = SeqTwoByteString::SizeFor(new_length);
9343 int delta = old_size - new_size;
9345 Address start_of_string = string->address();
9346 DCHECK_OBJECT_ALIGNED(start_of_string);
9347 DCHECK_OBJECT_ALIGNED(start_of_string + new_size);
9349 Heap* heap = string->GetHeap();
9350 NewSpace* newspace = heap->new_space();
9351 if (newspace->Contains(start_of_string) &&
9352 newspace->top() == start_of_string + old_size) {
9353 // Last allocated object in new space. Simply lower allocation top.
9354 newspace->set_top(start_of_string + new_size);
9356 // Sizes are pointer size aligned, so that we can use filler objects
9357 // that are a multiple of pointer size.
9358 heap->CreateFillerObjectAt(start_of_string + new_size, delta);
9360 heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER);
9362 // We are storing the new length using release store after creating a filler
9363 // for the left-over space to avoid races with the sweeper thread.
9364 string->synchronized_set_length(new_length);
9366 if (new_length == 0) return heap->isolate()->factory()->empty_string();
9371 uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
9372 // For array indexes mix the length into the hash as an array index could
9375 DCHECK(length <= String::kMaxArrayIndexSize);
9376 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
9377 (1 << String::kArrayIndexValueBits));
9379 value <<= String::ArrayIndexValueBits::kShift;
9380 value |= length << String::ArrayIndexLengthBits::kShift;
9382 DCHECK((value & String::kIsNotArrayIndexMask) == 0);
9383 DCHECK((length > String::kMaxCachedArrayIndexLength) ||
9384 (value & String::kContainsCachedArrayIndexMask) == 0);
9389 uint32_t StringHasher::GetHashField() {
9390 if (length_ <= String::kMaxHashCalcLength) {
9391 if (is_array_index_) {
9392 return MakeArrayIndexHash(array_index_, length_);
9394 return (GetHashCore(raw_running_hash_) << String::kHashShift) |
9395 String::kIsNotArrayIndexMask;
9397 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
9402 uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars,
9404 int* utf16_length_out) {
9405 int vector_length = chars.length();
9406 // Handle some edge cases
9407 if (vector_length <= 1) {
9408 DCHECK(vector_length == 0 ||
9409 static_cast<uint8_t>(chars.start()[0]) <=
9410 unibrow::Utf8::kMaxOneByteChar);
9411 *utf16_length_out = vector_length;
9412 return HashSequentialString(chars.start(), vector_length, seed);
9414 // Start with a fake length which won't affect computation.
9415 // It will be updated later.
9416 StringHasher hasher(String::kMaxArrayIndexSize, seed);
9417 size_t remaining = static_cast<size_t>(vector_length);
9418 const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start());
9419 int utf16_length = 0;
9420 bool is_index = true;
9421 DCHECK(hasher.is_array_index_);
9422 while (remaining > 0) {
9423 size_t consumed = 0;
9424 uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed);
9425 DCHECK(consumed > 0 && consumed <= remaining);
9427 remaining -= consumed;
9428 bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode;
9429 utf16_length += is_two_characters ? 2 : 1;
9430 // No need to keep hashing. But we do need to calculate utf16_length.
9431 if (utf16_length > String::kMaxHashCalcLength) continue;
9432 if (is_two_characters) {
9433 uint16_t c1 = unibrow::Utf16::LeadSurrogate(c);
9434 uint16_t c2 = unibrow::Utf16::TrailSurrogate(c);
9435 hasher.AddCharacter(c1);
9436 hasher.AddCharacter(c2);
9437 if (is_index) is_index = hasher.UpdateIndex(c1);
9438 if (is_index) is_index = hasher.UpdateIndex(c2);
9440 hasher.AddCharacter(c);
9441 if (is_index) is_index = hasher.UpdateIndex(c);
9444 *utf16_length_out = static_cast<int>(utf16_length);
9445 // Must set length here so that hash computation is correct.
9446 hasher.length_ = utf16_length;
9447 return hasher.GetHashField();
9451 void IteratingStringHasher::VisitConsString(ConsString* cons_string) {
9452 // Run small ConsStrings through ConsStringIterator.
9453 if (cons_string->length() < 64) {
9454 ConsStringIterator iter(cons_string);
9457 while (nullptr != (string = iter.Next(&offset))) {
9458 DCHECK_EQ(0, offset);
9459 String::VisitFlat(this, string, 0);
9464 const int max_length = String::kMaxHashCalcLength;
9465 int length = std::min(cons_string->length(), max_length);
9466 if (cons_string->HasOnlyOneByteChars()) {
9467 uint8_t* buffer = new uint8_t[length];
9468 String::WriteToFlat(cons_string, buffer, 0, length);
9469 AddCharacters(buffer, length);
9472 uint16_t* buffer = new uint16_t[length];
9473 String::WriteToFlat(cons_string, buffer, 0, length);
9474 AddCharacters(buffer, length);
9480 void String::PrintOn(FILE* file) {
9481 int length = this->length();
9482 for (int i = 0; i < length; i++) {
9483 PrintF(file, "%c", Get(i));
9488 inline static uint32_t ObjectAddressForHashing(Object* object) {
9489 uint32_t value = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object));
9490 return value & MemoryChunk::kAlignmentMask;
9495 // For performance reasons we only hash the 3 most variable fields of a map:
9496 // constructor, prototype and bit_field2. For predictability reasons we
9497 // use objects' offsets in respective pages for hashing instead of raw
9500 // Shift away the tag.
9501 int hash = ObjectAddressForHashing(GetConstructor()) >> 2;
9503 // XOR-ing the prototype and constructor directly yields too many zero bits
9504 // when the two pointers are close (which is fairly common).
9505 // To avoid this we shift the prototype bits relatively to the constructor.
9506 hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits);
9508 return hash ^ (hash >> 16) ^ bit_field2();
9512 static bool CheckEquivalent(Map* first, Map* second) {
9513 return first->GetConstructor() == second->GetConstructor() &&
9514 first->prototype() == second->prototype() &&
9515 first->instance_type() == second->instance_type() &&
9516 first->bit_field() == second->bit_field() &&
9517 first->is_extensible() == second->is_extensible() &&
9518 first->is_strong() == second->is_strong() &&
9519 first->has_instance_call_handler() ==
9520 second->has_instance_call_handler();
9524 bool Map::EquivalentToForTransition(Map* other) {
9525 return CheckEquivalent(this, other);
9529 bool Map::EquivalentToForNormalization(Map* other,
9530 PropertyNormalizationMode mode) {
9532 mode == CLEAR_INOBJECT_PROPERTIES ? 0 : other->GetInObjectProperties();
9533 return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() &&
9534 GetInObjectProperties() == properties;
9538 void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) {
9539 // Iterate over all fields in the body but take care in dealing with
9541 IteratePointers(v, kPropertiesOffset, kCodeEntryOffset);
9542 v->VisitCodeEntry(this->address() + kCodeEntryOffset);
9543 IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size);
9547 bool JSFunction::Inlines(SharedFunctionInfo* candidate) {
9548 DisallowHeapAllocation no_gc;
9549 if (shared() == candidate) return true;
9550 if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false;
9551 DeoptimizationInputData* const data =
9552 DeoptimizationInputData::cast(code()->deoptimization_data());
9553 if (data->length() == 0) return false;
9554 FixedArray* const literals = data->LiteralArray();
9555 int const inlined_count = data->InlinedFunctionCount()->value();
9556 for (int i = 0; i < inlined_count; ++i) {
9557 if (SharedFunctionInfo::cast(literals->get(i)) == candidate) {
9565 void JSFunction::MarkForOptimization() {
9566 Isolate* isolate = GetIsolate();
9567 // Do not optimize if function contains break points.
9568 if (shared()->HasDebugInfo()) return;
9569 DCHECK(!IsOptimized());
9570 DCHECK(shared()->allows_lazy_compilation() ||
9571 !shared()->optimization_disabled());
9572 DCHECK(!shared()->HasDebugInfo());
9573 set_code_no_write_barrier(
9574 isolate->builtins()->builtin(Builtins::kCompileOptimized));
9575 // No write barrier required, since the builtin is part of the root set.
9579 void JSFunction::AttemptConcurrentOptimization() {
9580 Isolate* isolate = GetIsolate();
9581 if (!isolate->concurrent_recompilation_enabled() ||
9582 isolate->bootstrapper()->IsActive()) {
9583 MarkForOptimization();
9586 if (isolate->concurrent_osr_enabled() &&
9587 isolate->optimizing_compile_dispatcher()->IsQueuedForOSR(this)) {
9588 // Do not attempt regular recompilation if we already queued this for OSR.
9589 // TODO(yangguo): This is necessary so that we don't install optimized
9590 // code on a function that is already optimized, since OSR and regular
9591 // recompilation race. This goes away as soon as OSR becomes one-shot.
9594 DCHECK(!IsInOptimizationQueue());
9595 DCHECK(!IsOptimized());
9596 DCHECK(shared()->allows_lazy_compilation() ||
9597 !shared()->optimization_disabled());
9598 DCHECK(isolate->concurrent_recompilation_enabled());
9599 if (FLAG_trace_concurrent_recompilation) {
9600 PrintF(" ** Marking ");
9602 PrintF(" for concurrent recompilation.\n");
9604 set_code_no_write_barrier(
9605 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent));
9606 // No write barrier required, since the builtin is part of the root set.
9610 Handle<JSFunction> JSFunction::CloneClosure(Handle<JSFunction> function) {
9611 Isolate* isolate = function->GetIsolate();
9612 Handle<Map> map(function->map());
9613 Handle<SharedFunctionInfo> shared(function->shared());
9614 Handle<Context> context(function->context());
9615 Handle<JSFunction> clone =
9616 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context);
9618 if (shared->bound()) {
9619 clone->set_function_bindings(function->function_bindings());
9622 // In typical case, __proto__ of ``function`` is the default Function
9623 // prototype, which means that SetPrototype below is a no-op.
9624 // In rare cases when that is not true, we mutate the clone's __proto__.
9625 Handle<Object> original_prototype(map->prototype(), isolate);
9626 if (*original_prototype != clone->map()->prototype()) {
9627 JSObject::SetPrototype(clone, original_prototype, false).Assert();
9634 void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap(
9635 Handle<SharedFunctionInfo> shared, Handle<Code> code) {
9636 Isolate* isolate = shared->GetIsolate();
9637 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9638 Handle<Object> value(shared->optimized_code_map(), isolate);
9639 if (value->IsSmi()) return; // Empty code maps are unsupported.
9640 Handle<FixedArray> code_map = Handle<FixedArray>::cast(value);
9641 code_map->set(kSharedCodeIndex, *code);
9645 void SharedFunctionInfo::AddToOptimizedCodeMap(
9646 Handle<SharedFunctionInfo> shared,
9647 Handle<Context> native_context,
9649 Handle<FixedArray> literals,
9650 BailoutId osr_ast_id) {
9651 Isolate* isolate = shared->GetIsolate();
9652 DCHECK(!shared->SearchOptimizedCodeMap(*native_context, osr_ast_id).code);
9653 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9654 DCHECK(native_context->IsNativeContext());
9655 STATIC_ASSERT(kEntryLength == 4);
9656 Handle<FixedArray> new_code_map;
9657 Handle<Object> value(shared->optimized_code_map(), isolate);
9659 if (value->IsSmi()) {
9660 // No optimized code map.
9661 DCHECK_EQ(0, Smi::cast(*value)->value());
9662 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED);
9663 old_length = kEntriesStart;
9665 // Copy old optimized code map and append one new entry.
9666 Handle<FixedArray> old_code_map = Handle<FixedArray>::cast(value);
9667 new_code_map = isolate->factory()->CopyFixedArrayAndGrow(
9668 old_code_map, kEntryLength, TENURED);
9669 old_length = old_code_map->length();
9670 // Zap the old map to avoid any stale entries. Note that this is required
9671 // for correctness because entries are being treated weakly by the GC.
9672 MemsetPointer(old_code_map->data_start(), isolate->heap()->the_hole_value(),
9675 new_code_map->set(old_length + kContextOffset, *native_context);
9676 new_code_map->set(old_length + kCachedCodeOffset, *code);
9677 new_code_map->set(old_length + kLiteralsOffset, *literals);
9678 new_code_map->set(old_length + kOsrAstIdOffset,
9679 Smi::FromInt(osr_ast_id.ToInt()));
9682 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) {
9683 DCHECK(new_code_map->get(i + kContextOffset)->IsNativeContext());
9684 DCHECK(new_code_map->get(i + kCachedCodeOffset)->IsCode());
9685 DCHECK(Code::cast(new_code_map->get(i + kCachedCodeOffset))->kind() ==
9686 Code::OPTIMIZED_FUNCTION);
9687 DCHECK(new_code_map->get(i + kLiteralsOffset)->IsFixedArray());
9688 DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi());
9691 shared->set_optimized_code_map(*new_code_map);
9695 void SharedFunctionInfo::ClearOptimizedCodeMap() {
9696 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9698 // If the next map link slot is already used then the function was
9699 // enqueued with code flushing and we remove it now.
9700 if (!code_map->get(kNextMapIndex)->IsUndefined()) {
9701 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
9702 flusher->EvictOptimizedCodeMap(this);
9705 DCHECK(code_map->get(kNextMapIndex)->IsUndefined());
9706 set_optimized_code_map(Smi::FromInt(0));
9710 void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code,
9711 const char* reason) {
9712 DisallowHeapAllocation no_gc;
9713 if (optimized_code_map()->IsSmi()) return;
9715 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9716 int dst = kEntriesStart;
9717 int length = code_map->length();
9718 for (int src = kEntriesStart; src < length; src += kEntryLength) {
9719 DCHECK(code_map->get(src)->IsNativeContext());
9720 if (Code::cast(code_map->get(src + kCachedCodeOffset)) == optimized_code) {
9721 // Evict the src entry by not copying it to the dst entry.
9722 if (FLAG_trace_opt) {
9723 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9725 BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value());
9729 PrintF(" (osr ast id %d)]\n", osr.ToInt());
9733 // Keep the src entry by copying it to the dst entry.
9735 code_map->set(dst + kContextOffset,
9736 code_map->get(src + kContextOffset));
9737 code_map->set(dst + kCachedCodeOffset,
9738 code_map->get(src + kCachedCodeOffset));
9739 code_map->set(dst + kLiteralsOffset,
9740 code_map->get(src + kLiteralsOffset));
9741 code_map->set(dst + kOsrAstIdOffset,
9742 code_map->get(src + kOsrAstIdOffset));
9744 dst += kEntryLength;
9747 if (code_map->get(kSharedCodeIndex) == optimized_code) {
9748 // Evict context-independent code as well.
9749 code_map->set_undefined(kSharedCodeIndex);
9750 if (FLAG_trace_opt) {
9751 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9753 PrintF(" (context-independent code)]\n");
9756 if (dst != length) {
9757 // Always trim even when array is cleared because of heap verifier.
9758 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map,
9760 if (code_map->length() == kEntriesStart &&
9761 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9762 ClearOptimizedCodeMap();
9768 void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) {
9769 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9770 DCHECK(shrink_by % kEntryLength == 0);
9771 DCHECK(shrink_by <= code_map->length() - kEntriesStart);
9772 // Always trim even when array is cleared because of heap verifier.
9773 GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map,
9775 if (code_map->length() == kEntriesStart &&
9776 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9777 ClearOptimizedCodeMap();
9782 static void GetMinInobjectSlack(Map* map, void* data) {
9783 int slack = map->unused_property_fields();
9784 if (*reinterpret_cast<int*>(data) > slack) {
9785 *reinterpret_cast<int*>(data) = slack;
9790 static void ShrinkInstanceSize(Map* map, void* data) {
9791 int slack = *reinterpret_cast<int*>(data);
9792 map->SetInObjectProperties(map->GetInObjectProperties() - slack);
9793 map->set_unused_property_fields(map->unused_property_fields() - slack);
9794 map->set_instance_size(map->instance_size() - slack * kPointerSize);
9796 // Visitor id might depend on the instance size, recalculate it.
9797 map->set_visitor_id(StaticVisitorBase::GetVisitorId(map));
9801 void JSFunction::CompleteInobjectSlackTracking() {
9802 DCHECK(has_initial_map());
9803 Map* map = initial_map();
9805 DCHECK(map->counter() >= Map::kSlackTrackingCounterEnd - 1);
9806 map->set_counter(Map::kRetainingCounterStart);
9808 int slack = map->unused_property_fields();
9809 TransitionArray::TraverseTransitionTree(map, &GetMinInobjectSlack, &slack);
9811 // Resize the initial map and all maps in its transition tree.
9812 TransitionArray::TraverseTransitionTree(map, &ShrinkInstanceSize, &slack);
9817 static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) {
9818 DisallowHeapAllocation no_gc;
9819 if (!object->HasFastProperties()) return false;
9820 Map* map = object->map();
9821 if (map->is_prototype_map()) return false;
9822 DescriptorArray* descriptors = map->instance_descriptors();
9823 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
9824 PropertyDetails details = descriptors->GetDetails(i);
9825 if (details.location() == kDescriptor) continue;
9826 if (details.representation().IsHeapObject() ||
9827 details.representation().IsTagged()) {
9828 FieldIndex index = FieldIndex::ForDescriptor(map, i);
9829 if (object->RawFastPropertyAt(index)->IsJSFunction()) return true;
9837 void JSObject::OptimizeAsPrototype(Handle<JSObject> object,
9838 PrototypeOptimizationMode mode) {
9839 if (object->IsGlobalObject()) return;
9840 if (object->IsJSGlobalProxy()) return;
9841 if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) {
9842 // First normalize to ensure all JSFunctions are DATA_CONSTANT.
9843 JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0,
9844 "NormalizeAsPrototype");
9846 Handle<Map> previous_map(object->map());
9847 if (!object->HasFastProperties()) {
9848 JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype");
9850 if (!object->map()->is_prototype_map()) {
9851 if (object->map() == *previous_map) {
9852 Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype");
9853 JSObject::MigrateToMap(object, new_map);
9855 object->map()->set_is_prototype_map(true);
9857 // Replace the pointer to the exact constructor with the Object function
9858 // from the same context if undetectable from JS. This is to avoid keeping
9859 // memory alive unnecessarily.
9860 Object* maybe_constructor = object->map()->GetConstructor();
9861 if (maybe_constructor->IsJSFunction()) {
9862 JSFunction* constructor = JSFunction::cast(maybe_constructor);
9863 Isolate* isolate = object->GetIsolate();
9864 if (!constructor->shared()->IsApiFunction() &&
9865 object->class_name() == isolate->heap()->Object_string()) {
9866 Handle<String> constructor_name(object->constructor_name(), isolate);
9867 Context* context = constructor->context()->native_context();
9868 JSFunction* object_function = context->object_function();
9869 object->map()->SetConstructor(object_function);
9870 Handle<PrototypeInfo> proto_info =
9871 Map::GetOrCreatePrototypeInfo(object, isolate);
9872 proto_info->set_constructor_name(*constructor_name);
9880 void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) {
9881 if (!object->map()->is_prototype_map()) return;
9882 OptimizeAsPrototype(object, FAST_PROTOTYPE);
9887 void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9888 DCHECK(FLAG_track_prototype_users);
9889 // Contract: In line with InvalidatePrototypeChains()'s requirements,
9890 // leaf maps don't need to register as users, only prototypes do.
9891 DCHECK(user->is_prototype_map());
9893 Handle<Map> current_user = user;
9894 Handle<PrototypeInfo> current_user_info =
9895 Map::GetOrCreatePrototypeInfo(user, isolate);
9896 for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) {
9897 // Walk up the prototype chain as far as links haven't been registered yet.
9898 if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) {
9901 Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter);
9902 if (maybe_proto->IsJSGlobalProxy()) continue;
9903 // Proxies on the prototype chain are not supported.
9904 if (maybe_proto->IsJSProxy()) return;
9905 Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto);
9906 Handle<PrototypeInfo> proto_info =
9907 Map::GetOrCreatePrototypeInfo(proto, isolate);
9908 Handle<Object> maybe_registry(proto_info->prototype_users(), isolate);
9910 Handle<WeakFixedArray> new_array =
9911 WeakFixedArray::Add(maybe_registry, current_user, &slot);
9912 current_user_info->set_registry_slot(slot);
9913 if (!maybe_registry.is_identical_to(new_array)) {
9914 proto_info->set_prototype_users(*new_array);
9916 if (FLAG_trace_prototype_users) {
9917 PrintF("Registering %p as a user of prototype %p (map=%p).\n",
9918 reinterpret_cast<void*>(*current_user),
9919 reinterpret_cast<void*>(*proto),
9920 reinterpret_cast<void*>(proto->map()));
9923 current_user = handle(proto->map(), isolate);
9924 current_user_info = proto_info;
9929 // Can be called regardless of whether |user| was actually registered with
9930 // |prototype|. Returns true when there was a registration.
9932 bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9933 DCHECK(user->is_prototype_map());
9934 // If it doesn't have a PrototypeInfo, it was never registered.
9935 if (!user->prototype_info()->IsPrototypeInfo()) return false;
9936 // If it doesn't have a prototype, it can't be registered.
9937 if (!user->prototype()->IsJSObject()) return false;
9938 Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate);
9939 Handle<PrototypeInfo> user_info =
9940 Map::GetOrCreatePrototypeInfo(user, isolate);
9941 int slot = user_info->registry_slot();
9942 if (slot == PrototypeInfo::UNREGISTERED) return false;
9943 if (prototype->IsJSGlobalProxy()) {
9944 PrototypeIterator iter(isolate, prototype);
9945 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
9947 DCHECK(prototype->map()->is_prototype_map());
9948 Object* maybe_proto_info = prototype->map()->prototype_info();
9949 // User knows its registry slot, prototype info and user registry must exist.
9950 DCHECK(maybe_proto_info->IsPrototypeInfo());
9951 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info),
9953 Object* maybe_registry = proto_info->prototype_users();
9954 DCHECK(maybe_registry->IsWeakFixedArray());
9955 DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user);
9956 WeakFixedArray::cast(maybe_registry)->Clear(slot);
9957 if (FLAG_trace_prototype_users) {
9958 PrintF("Unregistering %p as a user of prototype %p.\n",
9959 reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype));
9965 static void InvalidatePrototypeChainsInternal(Map* map) {
9966 if (!map->is_prototype_map()) return;
9967 if (FLAG_trace_prototype_users) {
9968 PrintF("Invalidating prototype map %p 's cell\n",
9969 reinterpret_cast<void*>(map));
9971 Object* maybe_proto_info = map->prototype_info();
9972 if (!maybe_proto_info->IsPrototypeInfo()) return;
9973 PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info);
9974 Object* maybe_cell = proto_info->validity_cell();
9975 if (maybe_cell->IsCell()) {
9976 // Just set the value; the cell will be replaced lazily.
9977 Cell* cell = Cell::cast(maybe_cell);
9978 cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid));
9981 WeakFixedArray::Iterator iterator(proto_info->prototype_users());
9982 // For now, only maps register themselves as users.
9984 while ((user = iterator.Next<Map>())) {
9985 // Walk the prototype chain (backwards, towards leaf objects) if necessary.
9986 InvalidatePrototypeChainsInternal(user);
9992 void JSObject::InvalidatePrototypeChains(Map* map) {
9993 if (!FLAG_eliminate_prototype_chain_checks) return;
9994 DisallowHeapAllocation no_gc;
9995 if (map->IsJSGlobalProxyMap()) {
9996 PrototypeIterator iter(map);
9997 map = JSObject::cast(iter.GetCurrent())->map();
9999 InvalidatePrototypeChainsInternal(map);
10004 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype,
10005 Isolate* isolate) {
10006 Object* maybe_proto_info = prototype->map()->prototype_info();
10007 if (maybe_proto_info->IsPrototypeInfo()) {
10008 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
10010 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
10011 prototype->map()->set_prototype_info(*proto_info);
10017 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map,
10018 Isolate* isolate) {
10019 Object* maybe_proto_info = prototype_map->prototype_info();
10020 if (maybe_proto_info->IsPrototypeInfo()) {
10021 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
10023 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
10024 prototype_map->set_prototype_info(*proto_info);
10030 Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map,
10031 Isolate* isolate) {
10032 Handle<Object> maybe_prototype(map->prototype(), isolate);
10033 if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null();
10034 Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype);
10035 if (prototype->IsJSGlobalProxy()) {
10036 PrototypeIterator iter(isolate, prototype);
10037 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
10039 // Ensure the prototype is registered with its own prototypes so its cell
10040 // will be invalidated when necessary.
10041 JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate),
10043 Handle<PrototypeInfo> proto_info =
10044 GetOrCreatePrototypeInfo(prototype, isolate);
10045 Object* maybe_cell = proto_info->validity_cell();
10046 // Return existing cell if it's still valid.
10047 if (maybe_cell->IsCell()) {
10048 Handle<Cell> cell(Cell::cast(maybe_cell), isolate);
10049 if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) {
10053 // Otherwise create a new cell.
10054 Handle<Cell> cell = isolate->factory()->NewCell(
10055 handle(Smi::FromInt(Map::kPrototypeChainValid), isolate));
10056 proto_info->set_validity_cell(*cell);
10062 void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype,
10063 PrototypeOptimizationMode proto_mode) {
10064 if (prototype->IsJSObject()) {
10065 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype);
10066 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode);
10068 WriteBarrierMode wb_mode =
10069 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER;
10070 map->set_prototype(*prototype, wb_mode);
10074 Handle<Object> CacheInitialJSArrayMaps(
10075 Handle<Context> native_context, Handle<Map> initial_map) {
10076 // Replace all of the cached initial array maps in the native context with
10077 // the appropriate transitioned elements kind maps.
10078 Factory* factory = native_context->GetIsolate()->factory();
10079 Handle<FixedArray> maps = factory->NewFixedArrayWithHoles(
10080 kElementsKindCount, TENURED);
10082 Handle<Map> current_map = initial_map;
10083 ElementsKind kind = current_map->elements_kind();
10084 DCHECK(kind == GetInitialFastElementsKind());
10085 maps->set(kind, *current_map);
10086 for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1;
10087 i < kFastElementsKindCount; ++i) {
10088 Handle<Map> new_map;
10089 ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i);
10090 Map* maybe_elements_transition = current_map->ElementsTransitionMap();
10091 if (maybe_elements_transition != NULL) {
10092 new_map = handle(maybe_elements_transition);
10093 DCHECK(new_map->elements_kind() == next_kind);
10095 new_map = Map::CopyAsElementsKind(
10096 current_map, next_kind, INSERT_TRANSITION);
10098 maps->set(next_kind, *new_map);
10099 current_map = new_map;
10101 if (initial_map->is_strong())
10102 native_context->set_js_array_strong_maps(*maps);
10104 native_context->set_js_array_maps(*maps);
10105 return initial_map;
10109 void JSFunction::SetInstancePrototype(Handle<JSFunction> function,
10110 Handle<Object> value) {
10111 Isolate* isolate = function->GetIsolate();
10113 DCHECK(value->IsJSReceiver());
10115 // Now some logic for the maps of the objects that are created by using this
10116 // function as a constructor.
10117 if (function->has_initial_map()) {
10118 // If the function has allocated the initial map replace it with a
10119 // copy containing the new prototype. Also complete any in-object
10120 // slack tracking that is in progress at this point because it is
10121 // still tracking the old copy.
10122 if (function->IsInobjectSlackTrackingInProgress()) {
10123 function->CompleteInobjectSlackTracking();
10126 Handle<Map> initial_map(function->initial_map(), isolate);
10128 if (!initial_map->GetIsolate()->bootstrapper()->IsActive() &&
10129 initial_map->instance_type() == JS_OBJECT_TYPE) {
10130 // Put the value in the initial map field until an initial map is needed.
10131 // At that point, a new initial map is created and the prototype is put
10132 // into the initial map where it belongs.
10133 function->set_prototype_or_initial_map(*value);
10135 Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype");
10136 JSFunction::SetInitialMap(function, new_map, value);
10138 // If the function is used as the global Array function, cache the
10139 // updated initial maps (and transitioned versions) in the native context.
10140 Handle<Context> native_context(function->context()->native_context(),
10142 Handle<Object> array_function(
10143 native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate);
10144 if (array_function->IsJSFunction() &&
10145 *function == JSFunction::cast(*array_function)) {
10146 CacheInitialJSArrayMaps(native_context, new_map);
10147 Handle<Map> new_strong_map = Map::Copy(new_map, "SetInstancePrototype");
10148 new_strong_map->set_is_strong();
10149 CacheInitialJSArrayMaps(native_context, new_strong_map);
10153 // Deoptimize all code that embeds the previous initial map.
10154 initial_map->dependent_code()->DeoptimizeDependentCodeGroup(
10155 isolate, DependentCode::kInitialMapChangedGroup);
10157 // Put the value in the initial map field until an initial map is
10158 // needed. At that point, a new initial map is created and the
10159 // prototype is put into the initial map where it belongs.
10160 function->set_prototype_or_initial_map(*value);
10161 if (value->IsJSObject()) {
10162 // Optimize as prototype to detach it from its transition tree.
10163 JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value),
10167 isolate->heap()->ClearInstanceofCache();
10171 void JSFunction::SetPrototype(Handle<JSFunction> function,
10172 Handle<Object> value) {
10173 DCHECK(function->should_have_prototype());
10174 Handle<Object> construct_prototype = value;
10176 // If the value is not a JSReceiver, store the value in the map's
10177 // constructor field so it can be accessed. Also, set the prototype
10178 // used for constructing objects to the original object prototype.
10179 // See ECMA-262 13.2.2.
10180 if (!value->IsJSReceiver()) {
10181 // Copy the map so this does not affect unrelated functions.
10182 // Remove map transitions because they point to maps with a
10183 // different prototype.
10184 Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype");
10186 JSObject::MigrateToMap(function, new_map);
10187 new_map->SetConstructor(*value);
10188 new_map->set_non_instance_prototype(true);
10189 Isolate* isolate = new_map->GetIsolate();
10190 construct_prototype = handle(
10191 isolate->context()->native_context()->initial_object_prototype(),
10194 function->map()->set_non_instance_prototype(false);
10197 return SetInstancePrototype(function, construct_prototype);
10201 bool JSFunction::RemovePrototype() {
10202 Context* native_context = context()->native_context();
10203 Map* no_prototype_map =
10204 is_strict(shared()->language_mode())
10205 ? native_context->strict_function_without_prototype_map()
10206 : native_context->sloppy_function_without_prototype_map();
10208 if (map() == no_prototype_map) return true;
10211 if (map() != (is_strict(shared()->language_mode())
10212 ? native_context->strict_function_map()
10213 : native_context->sloppy_function_map())) {
10218 set_map(no_prototype_map);
10219 set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value());
10224 void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map,
10225 Handle<Object> prototype) {
10226 if (map->prototype() != *prototype) {
10227 Map::SetPrototype(map, prototype, FAST_PROTOTYPE);
10229 function->set_prototype_or_initial_map(*map);
10230 map->SetConstructor(*function);
10232 if (FLAG_trace_maps) {
10233 PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n",
10234 reinterpret_cast<void*>(*map), function->shared()->unique_id(),
10235 function->shared()->DebugName()->ToCString().get());
10241 void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) {
10242 if (function->has_initial_map()) return;
10243 Isolate* isolate = function->GetIsolate();
10245 // First create a new map with the size and number of in-object properties
10246 // suggested by the function.
10247 InstanceType instance_type;
10249 int in_object_properties;
10250 if (function->shared()->is_generator()) {
10251 instance_type = JS_GENERATOR_OBJECT_TYPE;
10252 instance_size = JSGeneratorObject::kSize;
10253 in_object_properties = 0;
10255 instance_type = JS_OBJECT_TYPE;
10256 instance_size = function->shared()->CalculateInstanceSize();
10257 in_object_properties = function->shared()->CalculateInObjectProperties();
10259 Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size);
10261 // Fetch or allocate prototype.
10262 Handle<Object> prototype;
10263 if (function->has_instance_prototype()) {
10264 prototype = handle(function->instance_prototype(), isolate);
10266 prototype = isolate->factory()->NewFunctionPrototype(function);
10268 map->SetInObjectProperties(in_object_properties);
10269 map->set_unused_property_fields(in_object_properties);
10270 DCHECK(map->has_fast_object_elements());
10272 // Finally link initial map and constructor function.
10273 JSFunction::SetInitialMap(function, map, Handle<JSReceiver>::cast(prototype));
10275 if (!function->shared()->is_generator()) {
10276 function->StartInobjectSlackTracking();
10281 void JSFunction::SetInstanceClassName(String* name) {
10282 shared()->set_instance_class_name(name);
10286 void JSFunction::PrintName(FILE* out) {
10287 base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString();
10288 PrintF(out, "%s", name.get());
10292 // The filter is a pattern that matches function names in this way:
10293 // "*" all; the default
10294 // "-" all but the top-level function
10295 // "-name" all but the function "name"
10296 // "" only the top-level function
10297 // "name" only the function "name"
10298 // "name*" only functions starting with "name"
10299 // "~" none; the tilde is not an identifier
10300 bool JSFunction::PassesFilter(const char* raw_filter) {
10301 if (*raw_filter == '*') return true;
10302 String* name = shared()->DebugName();
10303 Vector<const char> filter = CStrVector(raw_filter);
10304 if (filter.length() == 0) return name->length() == 0;
10305 if (filter[0] == '-') {
10306 // Negative filter.
10307 if (filter.length() == 1) {
10308 return (name->length() != 0);
10309 } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) {
10312 if (filter[filter.length() - 1] == '*' &&
10313 name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) {
10318 } else if (name->IsUtf8EqualTo(filter)) {
10321 if (filter[filter.length() - 1] == '*' &&
10322 name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) {
10329 Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) {
10330 Isolate* isolate = function->GetIsolate();
10331 Handle<Object> name =
10332 JSReceiver::GetDataProperty(function, isolate->factory()->name_string());
10333 if (name->IsString()) return Handle<String>::cast(name);
10334 return handle(function->shared()->DebugName(), isolate);
10338 void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball,
10339 const char* to_string, Handle<Object> to_number,
10340 const char* type_of, byte kind) {
10341 Handle<String> internalized_to_string =
10342 isolate->factory()->InternalizeUtf8String(to_string);
10343 Handle<String> internalized_type_of =
10344 isolate->factory()->InternalizeUtf8String(type_of);
10345 oddball->set_to_number(*to_number);
10346 oddball->set_to_string(*internalized_to_string);
10347 oddball->set_type_of(*internalized_type_of);
10348 oddball->set_kind(kind);
10352 void Script::InitLineEnds(Handle<Script> script) {
10353 if (!script->line_ends()->IsUndefined()) return;
10355 Isolate* isolate = script->GetIsolate();
10357 if (!script->source()->IsString()) {
10358 DCHECK(script->source()->IsUndefined());
10359 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
10360 script->set_line_ends(*empty);
10361 DCHECK(script->line_ends()->IsFixedArray());
10365 Handle<String> src(String::cast(script->source()), isolate);
10367 Handle<FixedArray> array = String::CalculateLineEnds(src, true);
10369 if (*array != isolate->heap()->empty_fixed_array()) {
10370 array->set_map(isolate->heap()->fixed_cow_array_map());
10373 script->set_line_ends(*array);
10374 DCHECK(script->line_ends()->IsFixedArray());
10378 int Script::GetColumnNumber(Handle<Script> script, int code_pos) {
10379 int line_number = GetLineNumber(script, code_pos);
10380 if (line_number == -1) return -1;
10382 DisallowHeapAllocation no_allocation;
10383 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
10384 line_number = line_number - script->line_offset()->value();
10385 if (line_number == 0) return code_pos + script->column_offset()->value();
10386 int prev_line_end_pos =
10387 Smi::cast(line_ends_array->get(line_number - 1))->value();
10388 return code_pos - (prev_line_end_pos + 1);
10392 int Script::GetLineNumberWithArray(int code_pos) {
10393 DisallowHeapAllocation no_allocation;
10394 DCHECK(line_ends()->IsFixedArray());
10395 FixedArray* line_ends_array = FixedArray::cast(line_ends());
10396 int line_ends_len = line_ends_array->length();
10397 if (line_ends_len == 0) return -1;
10399 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
10400 return line_offset()->value();
10404 int right = line_ends_len;
10405 while (int half = (right - left) / 2) {
10406 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
10412 return right + line_offset()->value();
10416 int Script::GetLineNumber(Handle<Script> script, int code_pos) {
10417 InitLineEnds(script);
10418 return script->GetLineNumberWithArray(code_pos);
10422 int Script::GetLineNumber(int code_pos) {
10423 DisallowHeapAllocation no_allocation;
10424 if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos);
10426 // Slow mode: we do not have line_ends. We have to iterate through source.
10427 if (!source()->IsString()) return -1;
10429 String* source_string = String::cast(source());
10431 int len = source_string->length();
10432 for (int pos = 0; pos < len; pos++) {
10433 if (pos == code_pos) break;
10434 if (source_string->Get(pos) == '\n') line++;
10440 Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) {
10441 Isolate* isolate = script->GetIsolate();
10442 Handle<String> name_or_source_url_key =
10443 isolate->factory()->InternalizeOneByteString(
10444 STATIC_CHAR_VECTOR("nameOrSourceURL"));
10445 Handle<JSObject> script_wrapper = Script::GetWrapper(script);
10446 Handle<Object> property = Object::GetProperty(
10447 script_wrapper, name_or_source_url_key).ToHandleChecked();
10448 DCHECK(property->IsJSFunction());
10449 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
10450 Handle<Object> result;
10451 // Do not check against pending exception, since this function may be called
10452 // when an exception has already been pending.
10453 if (!Execution::TryCall(method, script_wrapper, 0, NULL).ToHandle(&result)) {
10454 return isolate->factory()->undefined_value();
10460 Handle<JSObject> Script::GetWrapper(Handle<Script> script) {
10461 Isolate* isolate = script->GetIsolate();
10462 if (!script->wrapper()->IsUndefined()) {
10463 DCHECK(script->wrapper()->IsWeakCell());
10464 Handle<WeakCell> cell(WeakCell::cast(script->wrapper()));
10465 if (!cell->cleared()) {
10466 // Return a handle for the existing script wrapper from the cache.
10467 return handle(JSObject::cast(cell->value()));
10469 // If we found an empty WeakCell, that means the script wrapper was
10470 // GCed. We are not notified directly of that, so we decrement here
10471 // so that we at least don't count double for any given script.
10472 isolate->counters()->script_wrappers()->Decrement();
10474 // Construct a new script wrapper.
10475 isolate->counters()->script_wrappers()->Increment();
10476 Handle<JSFunction> constructor = isolate->script_function();
10477 Handle<JSValue> result =
10478 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
10479 result->set_value(*script);
10480 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result);
10481 script->set_wrapper(*cell);
10486 MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo(
10487 FunctionLiteral* fun) {
10488 WeakFixedArray::Iterator iterator(shared_function_infos());
10489 SharedFunctionInfo* shared;
10490 while ((shared = iterator.Next<SharedFunctionInfo>())) {
10491 if (fun->function_token_position() == shared->function_token_position() &&
10492 fun->start_position() == shared->start_position()) {
10493 return Handle<SharedFunctionInfo>(shared);
10496 return MaybeHandle<SharedFunctionInfo>();
10500 Script::Iterator::Iterator(Isolate* isolate)
10501 : iterator_(isolate->heap()->script_list()) {}
10504 Script* Script::Iterator::Next() { return iterator_.Next<Script>(); }
10507 SharedFunctionInfo::Iterator::Iterator(Isolate* isolate)
10508 : script_iterator_(isolate), sfi_iterator_(NULL) {
10513 bool SharedFunctionInfo::Iterator::NextScript() {
10514 Script* script = script_iterator_.Next();
10515 if (script == NULL) return false;
10516 sfi_iterator_.Reset(script->shared_function_infos());
10521 SharedFunctionInfo* SharedFunctionInfo::Iterator::Next() {
10523 SharedFunctionInfo* next = sfi_iterator_.Next<SharedFunctionInfo>();
10524 if (next != NULL) return next;
10525 } while (NextScript());
10530 void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared,
10531 Handle<Object> script_object) {
10532 if (shared->script() == *script_object) return;
10533 // Remove shared function info from old script's list.
10534 if (shared->script()->IsScript()) {
10535 Script* old_script = Script::cast(shared->script());
10536 if (old_script->shared_function_infos()->IsWeakFixedArray()) {
10537 WeakFixedArray* list =
10538 WeakFixedArray::cast(old_script->shared_function_infos());
10539 list->Remove(shared);
10542 // Add shared function info to new script's list.
10543 if (script_object->IsScript()) {
10544 Handle<Script> script = Handle<Script>::cast(script_object);
10545 Handle<Object> list(script->shared_function_infos(), shared->GetIsolate());
10548 WeakFixedArray::Iterator iterator(*list);
10549 SharedFunctionInfo* next;
10550 while ((next = iterator.Next<SharedFunctionInfo>())) {
10551 DCHECK_NE(next, *shared);
10555 list = WeakFixedArray::Add(list, shared);
10556 script->set_shared_function_infos(*list);
10558 // Finally set new script.
10559 shared->set_script(*script_object);
10563 String* SharedFunctionInfo::DebugName() {
10564 Object* n = name();
10565 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name();
10566 return String::cast(n);
10570 bool SharedFunctionInfo::HasSourceCode() const {
10571 return !script()->IsUndefined() &&
10572 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
10576 Handle<Object> SharedFunctionInfo::GetSourceCode() {
10577 if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value();
10578 Handle<String> source(String::cast(Script::cast(script())->source()));
10579 return GetIsolate()->factory()->NewSubString(
10580 source, start_position(), end_position());
10584 bool SharedFunctionInfo::IsInlineable() {
10585 // Check that the function has a script associated with it.
10586 if (!script()->IsScript()) return false;
10587 return !optimization_disabled();
10591 int SharedFunctionInfo::SourceSize() {
10592 return end_position() - start_position();
10596 int SharedFunctionInfo::CalculateInstanceSize() {
10597 int instance_size =
10598 JSObject::kHeaderSize +
10599 expected_nof_properties() * kPointerSize;
10600 if (instance_size > JSObject::kMaxInstanceSize) {
10601 instance_size = JSObject::kMaxInstanceSize;
10603 return instance_size;
10607 int SharedFunctionInfo::CalculateInObjectProperties() {
10608 return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize;
10612 // Output the source code without any allocation in the heap.
10613 std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) {
10614 const SharedFunctionInfo* s = v.value;
10615 // For some native functions there is no source.
10616 if (!s->HasSourceCode()) return os << "<No Source>";
10618 // Get the source for the script which this function came from.
10619 // Don't use String::cast because we don't want more assertion errors while
10620 // we are already creating a stack dump.
10621 String* script_source =
10622 reinterpret_cast<String*>(Script::cast(s->script())->source());
10624 if (!script_source->LooksValid()) return os << "<Invalid Source>";
10626 if (!s->is_toplevel()) {
10628 Object* name = s->name();
10629 if (name->IsString() && String::cast(name)->length() > 0) {
10630 String::cast(name)->PrintUC16(os);
10634 int len = s->end_position() - s->start_position();
10635 if (len <= v.max_length || v.max_length < 0) {
10636 script_source->PrintUC16(os, s->start_position(), s->end_position());
10639 script_source->PrintUC16(os, s->start_position(),
10640 s->start_position() + v.max_length);
10641 return os << "...\n";
10646 static bool IsCodeEquivalent(Code* code, Code* recompiled) {
10647 if (code->instruction_size() != recompiled->instruction_size()) return false;
10648 ByteArray* code_relocation = code->relocation_info();
10649 ByteArray* recompiled_relocation = recompiled->relocation_info();
10650 int length = code_relocation->length();
10651 if (length != recompiled_relocation->length()) return false;
10652 int compare = memcmp(code_relocation->GetDataStartAddress(),
10653 recompiled_relocation->GetDataStartAddress(),
10655 return compare == 0;
10659 void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) {
10660 DCHECK(!has_deoptimization_support());
10661 DisallowHeapAllocation no_allocation;
10662 Code* code = this->code();
10663 if (IsCodeEquivalent(code, recompiled)) {
10664 // Copy the deoptimization data from the recompiled code.
10665 code->set_deoptimization_data(recompiled->deoptimization_data());
10666 code->set_has_deoptimization_support(true);
10668 // TODO(3025757): In case the recompiled isn't equivalent to the
10669 // old code, we have to replace it. We should try to avoid this
10670 // altogether because it flushes valuable type feedback by
10671 // effectively resetting all IC state.
10672 ReplaceCode(recompiled);
10674 DCHECK(has_deoptimization_support());
10678 void SharedFunctionInfo::DisableOptimization(BailoutReason reason) {
10679 // Disable optimization for the shared function info and mark the
10680 // code as non-optimizable. The marker on the shared function info
10681 // is there because we flush non-optimized code thereby loosing the
10682 // non-optimizable information for the code. When the code is
10683 // regenerated and set on the shared function info it is marked as
10684 // non-optimizable if optimization is disabled for the shared
10686 DCHECK(reason != kNoReason);
10687 set_optimization_disabled(true);
10688 set_disable_optimization_reason(reason);
10689 // Code should be the lazy compilation stub or else unoptimized.
10690 DCHECK(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN);
10691 PROFILE(GetIsolate(), CodeDisableOptEvent(code(), this));
10692 if (FLAG_trace_opt) {
10693 PrintF("[disabled optimization for ");
10695 PrintF(", reason: %s]\n", GetBailoutReason(reason));
10700 void SharedFunctionInfo::InitFromFunctionLiteral(
10701 Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) {
10702 shared_info->set_length(lit->scope()->default_function_length());
10703 shared_info->set_internal_formal_parameter_count(lit->parameter_count());
10704 shared_info->set_function_token_position(lit->function_token_position());
10705 shared_info->set_start_position(lit->start_position());
10706 shared_info->set_end_position(lit->end_position());
10707 shared_info->set_is_expression(lit->is_expression());
10708 shared_info->set_is_anonymous(lit->is_anonymous());
10709 shared_info->set_inferred_name(*lit->inferred_name());
10710 shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
10711 shared_info->set_allows_lazy_compilation_without_context(
10712 lit->AllowsLazyCompilationWithoutContext());
10713 shared_info->set_language_mode(lit->language_mode());
10714 shared_info->set_uses_arguments(lit->scope()->arguments() != NULL);
10715 shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
10716 shared_info->set_ast_node_count(lit->ast_node_count());
10717 shared_info->set_is_function(lit->is_function());
10718 if (lit->dont_optimize_reason() != kNoReason) {
10719 shared_info->DisableOptimization(lit->dont_optimize_reason());
10721 shared_info->set_dont_crankshaft(lit->flags() &
10722 AstProperties::kDontCrankshaft);
10723 shared_info->set_kind(lit->kind());
10724 shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject());
10725 shared_info->set_asm_function(lit->scope()->asm_function());
10729 bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) {
10730 DCHECK(!id.IsNone());
10731 Code* unoptimized = code();
10732 DeoptimizationOutputData* data =
10733 DeoptimizationOutputData::cast(unoptimized->deoptimization_data());
10734 unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this);
10736 return true; // Return true if there was no DCHECK.
10740 void JSFunction::StartInobjectSlackTracking() {
10741 DCHECK(has_initial_map() && !IsInobjectSlackTrackingInProgress());
10743 Map* map = initial_map();
10745 // No tracking during the snapshot construction phase.
10746 Isolate* isolate = GetIsolate();
10747 if (isolate->serializer_enabled()) return;
10749 if (map->unused_property_fields() == 0) return;
10751 map->set_counter(Map::kSlackTrackingCounterStart);
10755 void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
10756 code()->ClearInlineCaches();
10757 // If we clear ICs, we need to clear the type feedback vector too, since
10758 // CallICs are synced with a feedback vector slot.
10759 ClearTypeFeedbackInfo();
10760 set_ic_age(new_ic_age);
10761 if (code()->kind() == Code::FUNCTION) {
10762 code()->set_profiler_ticks(0);
10763 if (optimization_disabled() &&
10764 opt_count() >= FLAG_max_opt_count) {
10765 // Re-enable optimizations if they were disabled due to opt_count limit.
10766 set_optimization_disabled(false);
10769 set_deopt_count(0);
10774 CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap(
10775 Context* native_context, BailoutId osr_ast_id) {
10776 DisallowHeapAllocation no_gc;
10777 DCHECK(native_context->IsNativeContext());
10778 Object* value = optimized_code_map();
10779 if (!value->IsSmi()) {
10780 FixedArray* optimized_code_map = FixedArray::cast(value);
10781 int length = optimized_code_map->length();
10782 Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt());
10783 for (int i = kEntriesStart; i < length; i += kEntryLength) {
10784 if (optimized_code_map->get(i + kContextOffset) == native_context &&
10785 optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) {
10786 return {Code::cast(optimized_code_map->get(i + kCachedCodeOffset)),
10787 FixedArray::cast(optimized_code_map->get(i + kLiteralsOffset))};
10790 Object* shared_code = optimized_code_map->get(kSharedCodeIndex);
10791 if (shared_code->IsCode() && osr_ast_id.IsNone()) {
10792 return {Code::cast(shared_code), nullptr};
10794 if (FLAG_trace_opt) {
10795 PrintF("[didn't find optimized code in optimized code map for ");
10800 return {nullptr, nullptr};
10804 #define DECLARE_TAG(ignore1, name, ignore2) name,
10805 const char* const VisitorSynchronization::kTags[
10806 VisitorSynchronization::kNumberOfSyncTags] = {
10807 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10812 #define DECLARE_TAG(ignore1, ignore2, name) name,
10813 const char* const VisitorSynchronization::kTagNames[
10814 VisitorSynchronization::kNumberOfSyncTags] = {
10815 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10820 void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) {
10821 DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
10822 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
10823 Object* old_target = target;
10824 VisitPointer(&target);
10825 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10829 void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) {
10830 DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode()));
10831 Object* stub = rinfo->code_age_stub();
10833 VisitPointer(&stub);
10838 void ObjectVisitor::VisitCodeEntry(Address entry_address) {
10839 Object* code = Code::GetObjectFromEntryAddress(entry_address);
10840 Object* old_code = code;
10841 VisitPointer(&code);
10842 if (code != old_code) {
10843 Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry();
10848 void ObjectVisitor::VisitCell(RelocInfo* rinfo) {
10849 DCHECK(rinfo->rmode() == RelocInfo::CELL);
10850 Object* cell = rinfo->target_cell();
10851 Object* old_cell = cell;
10852 VisitPointer(&cell);
10853 if (cell != old_cell) {
10854 rinfo->set_target_cell(reinterpret_cast<Cell*>(cell));
10859 void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
10860 DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
10861 rinfo->IsPatchedDebugBreakSlotSequence());
10862 Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address());
10863 Object* old_target = target;
10864 VisitPointer(&target);
10865 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10869 void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) {
10870 DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
10871 Object* p = rinfo->target_object();
10876 void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) {
10877 Address p = rinfo->target_external_reference();
10878 VisitExternalReference(&p);
10882 void Code::InvalidateRelocation() {
10883 InvalidateEmbeddedObjects();
10884 set_relocation_info(GetHeap()->empty_byte_array());
10888 void Code::InvalidateEmbeddedObjects() {
10889 Object* undefined = GetHeap()->undefined_value();
10890 Cell* undefined_cell = GetHeap()->undefined_cell();
10891 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10892 RelocInfo::ModeMask(RelocInfo::CELL);
10893 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10894 RelocInfo::Mode mode = it.rinfo()->rmode();
10895 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10896 it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER);
10897 } else if (mode == RelocInfo::CELL) {
10898 it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER);
10904 void Code::Relocate(intptr_t delta) {
10905 for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) {
10906 it.rinfo()->apply(delta);
10908 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10912 void Code::CopyFrom(const CodeDesc& desc) {
10913 DCHECK(Marking::Color(this) == Marking::WHITE_OBJECT);
10916 CopyBytes(instruction_start(), desc.buffer,
10917 static_cast<size_t>(desc.instr_size));
10920 CopyBytes(relocation_start(),
10921 desc.buffer + desc.buffer_size - desc.reloc_size,
10922 static_cast<size_t>(desc.reloc_size));
10924 // unbox handles and relocate
10925 intptr_t delta = instruction_start() - desc.buffer;
10926 int mode_mask = RelocInfo::kCodeTargetMask |
10927 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10928 RelocInfo::ModeMask(RelocInfo::CELL) |
10929 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
10930 RelocInfo::kApplyMask;
10931 // Needed to find target_object and runtime_entry on X64
10932 Assembler* origin = desc.origin;
10933 AllowDeferredHandleDereference embedding_raw_address;
10934 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10935 RelocInfo::Mode mode = it.rinfo()->rmode();
10936 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10937 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10938 it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10939 } else if (mode == RelocInfo::CELL) {
10940 Handle<Cell> cell = it.rinfo()->target_cell_handle();
10941 it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10942 } else if (RelocInfo::IsCodeTarget(mode)) {
10943 // rewrite code handles in inline cache targets to direct
10944 // pointers to the first instruction in the code object
10945 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10946 Code* code = Code::cast(*p);
10947 it.rinfo()->set_target_address(code->instruction_start(),
10948 SKIP_WRITE_BARRIER,
10949 SKIP_ICACHE_FLUSH);
10950 } else if (RelocInfo::IsRuntimeEntry(mode)) {
10951 Address p = it.rinfo()->target_runtime_entry(origin);
10952 it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER,
10953 SKIP_ICACHE_FLUSH);
10954 } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) {
10955 Handle<Object> p = it.rinfo()->code_age_stub_handle(origin);
10956 Code* code = Code::cast(*p);
10957 it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH);
10959 it.rinfo()->apply(delta);
10962 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10966 // Locate the source position which is closest to the address in the code. This
10967 // is using the source position information embedded in the relocation info.
10968 // The position returned is relative to the beginning of the script where the
10969 // source for this function is found.
10970 int Code::SourcePosition(Address pc) {
10971 int distance = kMaxInt;
10972 int position = RelocInfo::kNoPosition; // Initially no position found.
10973 // Run through all the relocation info to find the best matching source
10974 // position. All the code needs to be considered as the sequence of the
10975 // instructions in the code does not necessarily follow the same order as the
10977 RelocIterator it(this, RelocInfo::kPositionMask);
10978 while (!it.done()) {
10979 // Only look at positions after the current pc.
10980 if (it.rinfo()->pc() < pc) {
10981 // Get position and distance.
10983 int dist = static_cast<int>(pc - it.rinfo()->pc());
10984 int pos = static_cast<int>(it.rinfo()->data());
10985 // If this position is closer than the current candidate or if it has the
10986 // same distance as the current candidate and the position is higher then
10987 // this position is the new candidate.
10988 if ((dist < distance) ||
10989 (dist == distance && pos > position)) {
11000 // Same as Code::SourcePosition above except it only looks for statement
11002 int Code::SourceStatementPosition(Address pc) {
11003 // First find the position as close as possible using all position
11005 int position = SourcePosition(pc);
11006 // Now find the closest statement position before the position.
11007 int statement_position = 0;
11008 RelocIterator it(this, RelocInfo::kPositionMask);
11009 while (!it.done()) {
11010 if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) {
11011 int p = static_cast<int>(it.rinfo()->data());
11012 if (statement_position < p && p <= position) {
11013 statement_position = p;
11018 return statement_position;
11022 SafepointEntry Code::GetSafepointEntry(Address pc) {
11023 SafepointTable table(this);
11024 return table.FindEntry(pc);
11028 Object* Code::FindNthObject(int n, Map* match_map) {
11029 DCHECK(is_inline_cache_stub());
11030 DisallowHeapAllocation no_allocation;
11031 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11032 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11033 RelocInfo* info = it.rinfo();
11034 Object* object = info->target_object();
11035 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
11036 if (object->IsHeapObject()) {
11037 if (HeapObject::cast(object)->map() == match_map) {
11038 if (--n == 0) return object;
11046 AllocationSite* Code::FindFirstAllocationSite() {
11047 Object* result = FindNthObject(1, GetHeap()->allocation_site_map());
11048 return (result != NULL) ? AllocationSite::cast(result) : NULL;
11052 Map* Code::FindFirstMap() {
11053 Object* result = FindNthObject(1, GetHeap()->meta_map());
11054 return (result != NULL) ? Map::cast(result) : NULL;
11058 void Code::FindAndReplace(const FindAndReplacePattern& pattern) {
11059 DCHECK(is_inline_cache_stub() || is_handler());
11060 DisallowHeapAllocation no_allocation;
11061 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11062 STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32);
11063 int current_pattern = 0;
11064 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11065 RelocInfo* info = it.rinfo();
11066 Object* object = info->target_object();
11067 if (object->IsHeapObject()) {
11068 if (object->IsWeakCell()) {
11069 object = HeapObject::cast(WeakCell::cast(object)->value());
11071 Map* map = HeapObject::cast(object)->map();
11072 if (map == *pattern.find_[current_pattern]) {
11073 info->set_target_object(*pattern.replace_[current_pattern]);
11074 if (++current_pattern == pattern.count_) return;
11082 void Code::FindAllMaps(MapHandleList* maps) {
11083 DCHECK(is_inline_cache_stub());
11084 DisallowHeapAllocation no_allocation;
11085 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11086 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11087 RelocInfo* info = it.rinfo();
11088 Object* object = info->target_object();
11089 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
11090 if (object->IsMap()) maps->Add(handle(Map::cast(object)));
11095 Code* Code::FindFirstHandler() {
11096 DCHECK(is_inline_cache_stub());
11097 DisallowHeapAllocation no_allocation;
11098 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
11099 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11100 bool skip_next_handler = false;
11101 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11102 RelocInfo* info = it.rinfo();
11103 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
11104 Object* obj = info->target_object();
11105 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
11107 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
11108 if (code->kind() == Code::HANDLER) {
11109 if (!skip_next_handler) return code;
11110 skip_next_handler = false;
11118 bool Code::FindHandlers(CodeHandleList* code_list, int length) {
11119 DCHECK(is_inline_cache_stub());
11120 DisallowHeapAllocation no_allocation;
11121 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
11122 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11123 bool skip_next_handler = false;
11125 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11126 if (i == length) return true;
11127 RelocInfo* info = it.rinfo();
11128 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
11129 Object* obj = info->target_object();
11130 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
11132 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
11133 // IC stubs with handlers never contain non-handler code objects before
11134 // handler targets.
11135 if (code->kind() != Code::HANDLER) break;
11136 if (!skip_next_handler) {
11137 code_list->Add(Handle<Code>(code));
11140 skip_next_handler = false;
11143 return i == length;
11147 MaybeHandle<Code> Code::FindHandlerForMap(Map* map) {
11148 DCHECK(is_inline_cache_stub());
11149 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
11150 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11151 bool return_next = false;
11152 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11153 RelocInfo* info = it.rinfo();
11154 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
11155 Object* object = info->target_object();
11156 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
11157 if (object == map) return_next = true;
11158 } else if (return_next) {
11159 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
11160 DCHECK(code->kind() == Code::HANDLER);
11161 return handle(code);
11164 return MaybeHandle<Code>();
11168 Name* Code::FindFirstName() {
11169 DCHECK(is_inline_cache_stub());
11170 DisallowHeapAllocation no_allocation;
11171 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
11172 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11173 RelocInfo* info = it.rinfo();
11174 Object* object = info->target_object();
11175 if (object->IsName()) return Name::cast(object);
11181 void Code::ClearInlineCaches() {
11182 ClearInlineCaches(NULL);
11186 void Code::ClearInlineCaches(Code::Kind kind) {
11187 ClearInlineCaches(&kind);
11191 void Code::ClearInlineCaches(Code::Kind* kind) {
11192 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
11193 RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) |
11194 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
11195 for (RelocIterator it(this, mask); !it.done(); it.next()) {
11196 RelocInfo* info = it.rinfo();
11197 Code* target(Code::GetCodeFromTargetAddress(info->target_address()));
11198 if (target->is_inline_cache_stub()) {
11199 if (kind == NULL || *kind == target->kind()) {
11200 IC::Clear(this->GetIsolate(), info->pc(),
11201 info->host()->constant_pool());
11208 void SharedFunctionInfo::ClearTypeFeedbackInfo() {
11209 feedback_vector()->ClearSlots(this);
11210 feedback_vector()->ClearICSlots(this);
11214 void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() {
11215 feedback_vector()->ClearSlotsAtGCTime(this);
11216 feedback_vector()->ClearICSlotsAtGCTime(this);
11220 BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) {
11221 DisallowHeapAllocation no_gc;
11222 DCHECK(kind() == FUNCTION);
11223 BackEdgeTable back_edges(this, &no_gc);
11224 for (uint32_t i = 0; i < back_edges.length(); i++) {
11225 if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i);
11227 return BailoutId::None();
11231 uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) {
11232 DisallowHeapAllocation no_gc;
11233 DCHECK(kind() == FUNCTION);
11234 BackEdgeTable back_edges(this, &no_gc);
11235 for (uint32_t i = 0; i < back_edges.length(); i++) {
11236 if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i);
11238 UNREACHABLE(); // We expect to find the back edge.
11243 void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) {
11244 PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY);
11248 void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) {
11249 PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge,
11250 NO_MARKING_PARITY);
11254 // NextAge defines the Code::Age state transitions during a GC cycle.
11255 static Code::Age NextAge(Code::Age age) {
11257 case Code::kNotExecutedCodeAge: // Keep, until we've been executed.
11258 case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed.
11259 case Code::kLastCodeAge: // Clamp at last Code::Age value.
11261 case Code::kExecutedOnceCodeAge:
11262 // Pre-age code that has only been executed once.
11263 return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1);
11265 return static_cast<Code::Age>(age + 1); // Default case: Increase age.
11270 // IsOldAge defines the collection criteria for a Code object.
11271 static bool IsOldAge(Code::Age age) {
11272 return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge;
11276 void Code::MakeYoung(Isolate* isolate) {
11277 byte* sequence = FindCodeAgeSequence();
11278 if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate);
11282 void Code::MarkToBeExecutedOnce(Isolate* isolate) {
11283 byte* sequence = FindCodeAgeSequence();
11284 if (sequence != NULL) {
11285 PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge,
11286 NO_MARKING_PARITY);
11291 void Code::MakeOlder(MarkingParity current_parity) {
11292 byte* sequence = FindCodeAgeSequence();
11293 if (sequence != NULL) {
11295 MarkingParity code_parity;
11296 Isolate* isolate = GetIsolate();
11297 GetCodeAgeAndParity(isolate, sequence, &age, &code_parity);
11298 Age next_age = NextAge(age);
11299 if (age != next_age && code_parity != current_parity) {
11300 PatchPlatformCodeAge(isolate, sequence, next_age, current_parity);
11306 bool Code::IsOld() {
11307 return IsOldAge(GetAge());
11311 byte* Code::FindCodeAgeSequence() {
11312 return FLAG_age_code &&
11313 prologue_offset() != Code::kPrologueOffsetNotSet &&
11314 (kind() == OPTIMIZED_FUNCTION ||
11315 (kind() == FUNCTION && !has_debug_break_slots()))
11316 ? instruction_start() + prologue_offset()
11321 Code::Age Code::GetAge() {
11322 byte* sequence = FindCodeAgeSequence();
11323 if (sequence == NULL) {
11324 return kNoAgeCodeAge;
11327 MarkingParity parity;
11328 GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity);
11333 void Code::GetCodeAgeAndParity(Code* code, Age* age,
11334 MarkingParity* parity) {
11335 Isolate* isolate = code->GetIsolate();
11336 Builtins* builtins = isolate->builtins();
11338 #define HANDLE_CODE_AGE(AGE) \
11339 stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \
11340 if (code == stub) { \
11341 *age = k##AGE##CodeAge; \
11342 *parity = EVEN_MARKING_PARITY; \
11345 stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11346 if (code == stub) { \
11347 *age = k##AGE##CodeAge; \
11348 *parity = ODD_MARKING_PARITY; \
11351 CODE_AGE_LIST(HANDLE_CODE_AGE)
11352 #undef HANDLE_CODE_AGE
11353 stub = *builtins->MarkCodeAsExecutedOnce();
11354 if (code == stub) {
11355 *age = kNotExecutedCodeAge;
11356 *parity = NO_MARKING_PARITY;
11359 stub = *builtins->MarkCodeAsExecutedTwice();
11360 if (code == stub) {
11361 *age = kExecutedOnceCodeAge;
11362 *parity = NO_MARKING_PARITY;
11365 stub = *builtins->MarkCodeAsToBeExecutedOnce();
11366 if (code == stub) {
11367 *age = kToBeExecutedOnceCodeAge;
11368 *parity = NO_MARKING_PARITY;
11375 Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) {
11376 Builtins* builtins = isolate->builtins();
11378 #define HANDLE_CODE_AGE(AGE) \
11379 case k##AGE##CodeAge: { \
11380 Code* stub = parity == EVEN_MARKING_PARITY \
11381 ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \
11382 : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11385 CODE_AGE_LIST(HANDLE_CODE_AGE)
11386 #undef HANDLE_CODE_AGE
11387 case kNotExecutedCodeAge: {
11388 DCHECK(parity == NO_MARKING_PARITY);
11389 return *builtins->MarkCodeAsExecutedOnce();
11391 case kExecutedOnceCodeAge: {
11392 DCHECK(parity == NO_MARKING_PARITY);
11393 return *builtins->MarkCodeAsExecutedTwice();
11395 case kToBeExecutedOnceCodeAge: {
11396 DCHECK(parity == NO_MARKING_PARITY);
11397 return *builtins->MarkCodeAsToBeExecutedOnce();
11407 void Code::PrintDeoptLocation(FILE* out, Address pc) {
11408 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc);
11409 class SourcePosition pos = info.position;
11410 if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) {
11411 if (FLAG_hydrogen_track_positions) {
11412 PrintF(out, " ;;; deoptimize at %d_%d: %s\n",
11413 pos.inlining_id(), pos.position(),
11414 Deoptimizer::GetDeoptReason(info.deopt_reason));
11416 PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(),
11417 Deoptimizer::GetDeoptReason(info.deopt_reason));
11423 bool Code::CanDeoptAt(Address pc) {
11424 DeoptimizationInputData* deopt_data =
11425 DeoptimizationInputData::cast(deoptimization_data());
11426 Address code_start_address = instruction_start();
11427 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
11428 if (deopt_data->Pc(i)->value() == -1) continue;
11429 Address address = code_start_address + deopt_data->Pc(i)->value();
11430 if (address == pc) return true;
11436 // Identify kind of code.
11437 const char* Code::Kind2String(Kind kind) {
11439 #define CASE(name) case name: return #name;
11440 CODE_KIND_LIST(CASE)
11442 case NUMBER_OF_KINDS: break;
11449 Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) {
11450 DCHECK(code->kind() == OPTIMIZED_FUNCTION);
11451 WeakCell* raw_cell = code->CachedWeakCell();
11452 if (raw_cell != NULL) return Handle<WeakCell>(raw_cell);
11453 Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code);
11454 DeoptimizationInputData::cast(code->deoptimization_data())
11455 ->SetWeakCellCache(*cell);
11460 WeakCell* Code::CachedWeakCell() {
11461 DCHECK(kind() == OPTIMIZED_FUNCTION);
11462 Object* weak_cell_cache =
11463 DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache();
11464 if (weak_cell_cache->IsWeakCell()) {
11465 DCHECK(this == WeakCell::cast(weak_cell_cache)->value());
11466 return WeakCell::cast(weak_cell_cache);
11472 #ifdef ENABLE_DISASSEMBLER
11474 void DeoptimizationInputData::DeoptimizationInputDataPrint(
11475 std::ostream& os) { // NOLINT
11476 disasm::NameConverter converter;
11477 int const inlined_function_count = InlinedFunctionCount()->value();
11478 os << "Inlined functions (count = " << inlined_function_count << ")\n";
11479 for (int id = 0; id < inlined_function_count; ++id) {
11480 Object* info = LiteralArray()->get(id);
11481 os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n";
11484 int deopt_count = DeoptCount();
11485 os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n";
11486 if (0 != deopt_count) {
11487 os << " index ast id argc pc";
11488 if (FLAG_print_code_verbose) os << " commands";
11491 for (int i = 0; i < deopt_count; i++) {
11492 os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " "
11493 << std::setw(6) << ArgumentsStackHeight(i)->value() << " "
11494 << std::setw(6) << Pc(i)->value();
11496 if (!FLAG_print_code_verbose) {
11500 // Print details of the frame translation.
11501 int translation_index = TranslationIndex(i)->value();
11502 TranslationIterator iterator(TranslationByteArray(), translation_index);
11503 Translation::Opcode opcode =
11504 static_cast<Translation::Opcode>(iterator.Next());
11505 DCHECK(Translation::BEGIN == opcode);
11506 int frame_count = iterator.Next();
11507 int jsframe_count = iterator.Next();
11508 os << " " << Translation::StringFor(opcode)
11509 << " {frame count=" << frame_count
11510 << ", js frame count=" << jsframe_count << "}\n";
11512 while (iterator.HasNext() &&
11513 Translation::BEGIN !=
11514 (opcode = static_cast<Translation::Opcode>(iterator.Next()))) {
11515 os << std::setw(31) << " " << Translation::StringFor(opcode) << " ";
11518 case Translation::BEGIN:
11522 case Translation::JS_FRAME: {
11523 int ast_id = iterator.Next();
11524 int shared_info_id = iterator.Next();
11525 unsigned height = iterator.Next();
11526 Object* shared_info = LiteralArray()->get(shared_info_id);
11527 os << "{ast_id=" << ast_id << ", function="
11528 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11529 << ", height=" << height << "}";
11533 case Translation::JS_FRAME_FUNCTION: {
11534 os << "{function}";
11538 case Translation::COMPILED_STUB_FRAME: {
11539 Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next());
11540 os << "{kind=" << stub_kind << "}";
11544 case Translation::ARGUMENTS_ADAPTOR_FRAME:
11545 case Translation::CONSTRUCT_STUB_FRAME: {
11546 int shared_info_id = iterator.Next();
11547 Object* shared_info = LiteralArray()->get(shared_info_id);
11548 unsigned height = iterator.Next();
11550 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11551 << ", height=" << height << "}";
11555 case Translation::GETTER_STUB_FRAME:
11556 case Translation::SETTER_STUB_FRAME: {
11557 int shared_info_id = iterator.Next();
11558 Object* shared_info = LiteralArray()->get(shared_info_id);
11559 os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info)
11560 ->DebugName()) << "}";
11564 case Translation::REGISTER: {
11565 int reg_code = iterator.Next();
11566 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11570 case Translation::INT32_REGISTER: {
11571 int reg_code = iterator.Next();
11572 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11576 case Translation::UINT32_REGISTER: {
11577 int reg_code = iterator.Next();
11578 os << "{input=" << converter.NameOfCPURegister(reg_code)
11583 case Translation::BOOL_REGISTER: {
11584 int reg_code = iterator.Next();
11585 os << "{input=" << converter.NameOfCPURegister(reg_code)
11590 case Translation::DOUBLE_REGISTER: {
11591 int reg_code = iterator.Next();
11592 os << "{input=" << DoubleRegister::AllocationIndexToString(reg_code)
11597 case Translation::STACK_SLOT: {
11598 int input_slot_index = iterator.Next();
11599 os << "{input=" << input_slot_index << "}";
11603 case Translation::INT32_STACK_SLOT: {
11604 int input_slot_index = iterator.Next();
11605 os << "{input=" << input_slot_index << "}";
11609 case Translation::UINT32_STACK_SLOT: {
11610 int input_slot_index = iterator.Next();
11611 os << "{input=" << input_slot_index << " (unsigned)}";
11615 case Translation::BOOL_STACK_SLOT: {
11616 int input_slot_index = iterator.Next();
11617 os << "{input=" << input_slot_index << " (bool)}";
11621 case Translation::DOUBLE_STACK_SLOT: {
11622 int input_slot_index = iterator.Next();
11623 os << "{input=" << input_slot_index << "}";
11627 case Translation::LITERAL: {
11628 unsigned literal_index = iterator.Next();
11629 os << "{literal_id=" << literal_index << "}";
11633 case Translation::DUPLICATED_OBJECT: {
11634 int object_index = iterator.Next();
11635 os << "{object_index=" << object_index << "}";
11639 case Translation::ARGUMENTS_OBJECT:
11640 case Translation::CAPTURED_OBJECT: {
11641 int args_length = iterator.Next();
11642 os << "{length=" << args_length << "}";
11652 void DeoptimizationOutputData::DeoptimizationOutputDataPrint(
11653 std::ostream& os) { // NOLINT
11654 os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints()
11656 if (this->DeoptPoints() == 0) return;
11658 os << "ast id pc state\n";
11659 for (int i = 0; i < this->DeoptPoints(); i++) {
11660 int pc_and_state = this->PcAndState(i)->value();
11661 os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8)
11662 << FullCodeGenerator::PcField::decode(pc_and_state) << " "
11663 << FullCodeGenerator::State2String(
11664 FullCodeGenerator::StateField::decode(pc_and_state)) << "\n";
11669 void HandlerTable::HandlerTableRangePrint(std::ostream& os) {
11670 os << " from to hdlr\n";
11671 for (int i = 0; i < length(); i += kRangeEntrySize) {
11672 int pc_start = Smi::cast(get(i + kRangeStartIndex))->value();
11673 int pc_end = Smi::cast(get(i + kRangeEndIndex))->value();
11674 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
11675 int handler_offset = HandlerOffsetField::decode(handler_field);
11676 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11677 int depth = Smi::cast(get(i + kRangeDepthIndex))->value();
11678 os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end
11679 << ") -> " << std::setw(4) << handler_offset
11680 << " (prediction=" << prediction << ", depth=" << depth << ")\n";
11685 void HandlerTable::HandlerTableReturnPrint(std::ostream& os) {
11686 os << " off hdlr (c)\n";
11687 for (int i = 0; i < length(); i += kReturnEntrySize) {
11688 int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
11689 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
11690 int handler_offset = HandlerOffsetField::decode(handler_field);
11691 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11692 os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4)
11693 << handler_offset << " (prediction=" << prediction << ")\n";
11698 const char* Code::ICState2String(InlineCacheState state) {
11700 case UNINITIALIZED: return "UNINITIALIZED";
11701 case PREMONOMORPHIC: return "PREMONOMORPHIC";
11702 case MONOMORPHIC: return "MONOMORPHIC";
11703 case PROTOTYPE_FAILURE:
11704 return "PROTOTYPE_FAILURE";
11705 case POLYMORPHIC: return "POLYMORPHIC";
11706 case MEGAMORPHIC: return "MEGAMORPHIC";
11707 case GENERIC: return "GENERIC";
11708 case DEBUG_STUB: return "DEBUG_STUB";
11717 const char* Code::StubType2String(StubType type) {
11719 case NORMAL: return "NORMAL";
11720 case FAST: return "FAST";
11722 UNREACHABLE(); // keep the compiler happy
11727 void Code::PrintExtraICState(std::ostream& os, // NOLINT
11728 Kind kind, ExtraICState extra) {
11729 os << "extra_ic_state = ";
11730 if ((kind == STORE_IC || kind == KEYED_STORE_IC) &&
11731 is_strict(static_cast<LanguageMode>(extra))) {
11734 os << extra << "\n";
11739 void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT
11740 os << "kind = " << Kind2String(kind()) << "\n";
11741 if (IsCodeStubOrIC()) {
11742 const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this));
11743 os << "major_key = " << (n == NULL ? "null" : n) << "\n";
11745 if (is_inline_cache_stub()) {
11746 os << "ic_state = " << ICState2String(ic_state()) << "\n";
11747 PrintExtraICState(os, kind(), extra_ic_state());
11748 if (ic_state() == MONOMORPHIC) {
11749 os << "type = " << StubType2String(type()) << "\n";
11751 if (is_compare_ic_stub()) {
11752 DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC);
11753 CompareICStub stub(stub_key(), GetIsolate());
11754 os << "compare_state = " << CompareICState::GetStateName(stub.left())
11755 << "*" << CompareICState::GetStateName(stub.right()) << " -> "
11756 << CompareICState::GetStateName(stub.state()) << "\n";
11757 os << "compare_operation = " << Token::Name(stub.op()) << "\n";
11760 if ((name != NULL) && (name[0] != '\0')) {
11761 os << "name = " << name << "\n";
11763 if (kind() == OPTIMIZED_FUNCTION) {
11764 os << "stack_slots = " << stack_slots() << "\n";
11766 os << "compiler = " << (is_turbofanned()
11768 : is_crankshafted() ? "crankshaft"
11769 : kind() == Code::FUNCTION
11771 : "unknown") << "\n";
11773 os << "Instructions (size = " << instruction_size() << ")\n";
11775 Isolate* isolate = GetIsolate();
11776 int size = instruction_size();
11777 int safepoint_offset =
11778 is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size;
11779 int back_edge_offset = (kind() == Code::FUNCTION)
11780 ? static_cast<int>(back_edge_table_offset())
11782 int constant_pool_offset = FLAG_enable_embedded_constant_pool
11783 ? this->constant_pool_offset()
11786 // Stop before reaching any embedded tables
11787 int code_size = Min(safepoint_offset, back_edge_offset);
11788 code_size = Min(code_size, constant_pool_offset);
11789 byte* begin = instruction_start();
11790 byte* end = begin + code_size;
11791 Disassembler::Decode(isolate, &os, begin, end, this);
11793 if (constant_pool_offset < size) {
11794 int constant_pool_size = size - constant_pool_offset;
11795 DCHECK((constant_pool_size & kPointerAlignmentMask) == 0);
11796 os << "\nConstant Pool (size = " << constant_pool_size << ")\n";
11797 Vector<char> buf = Vector<char>::New(50);
11798 intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset);
11799 for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) {
11800 SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr);
11801 os << static_cast<const void*>(ptr) << " " << buf.start() << "\n";
11807 if (kind() == FUNCTION) {
11808 DeoptimizationOutputData* data =
11809 DeoptimizationOutputData::cast(this->deoptimization_data());
11810 data->DeoptimizationOutputDataPrint(os);
11811 } else if (kind() == OPTIMIZED_FUNCTION) {
11812 DeoptimizationInputData* data =
11813 DeoptimizationInputData::cast(this->deoptimization_data());
11814 data->DeoptimizationInputDataPrint(os);
11818 if (is_crankshafted()) {
11819 SafepointTable table(this);
11820 os << "Safepoints (size = " << table.size() << ")\n";
11821 for (unsigned i = 0; i < table.length(); i++) {
11822 unsigned pc_offset = table.GetPcOffset(i);
11823 os << static_cast<const void*>(instruction_start() + pc_offset) << " ";
11824 os << std::setw(4) << pc_offset << " ";
11825 table.PrintEntry(i, os);
11826 os << " (sp -> fp) ";
11827 SafepointEntry entry = table.GetEntry(i);
11828 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
11829 os << std::setw(6) << entry.deoptimization_index();
11833 if (entry.argument_count() > 0) {
11834 os << " argc: " << entry.argument_count();
11839 } else if (kind() == FUNCTION) {
11840 unsigned offset = back_edge_table_offset();
11841 // If there is no back edge table, the "table start" will be at or after
11842 // (due to alignment) the end of the instruction stream.
11843 if (static_cast<int>(offset) < instruction_size()) {
11844 DisallowHeapAllocation no_gc;
11845 BackEdgeTable back_edges(this, &no_gc);
11847 os << "Back edges (size = " << back_edges.length() << ")\n";
11848 os << "ast_id pc_offset loop_depth\n";
11850 for (uint32_t i = 0; i < back_edges.length(); i++) {
11851 os << std::setw(6) << back_edges.ast_id(i).ToInt() << " "
11852 << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10)
11853 << back_edges.loop_depth(i) << "\n";
11858 #ifdef OBJECT_PRINT
11859 if (!type_feedback_info()->IsUndefined()) {
11860 OFStream os(stdout);
11861 TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os);
11867 if (handler_table()->length() > 0) {
11868 os << "Handler Table (size = " << handler_table()->Size() << ")\n";
11869 if (kind() == FUNCTION) {
11870 HandlerTable::cast(handler_table())->HandlerTableRangePrint(os);
11871 } else if (kind() == OPTIMIZED_FUNCTION) {
11872 HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os);
11877 os << "RelocInfo (size = " << relocation_size() << ")\n";
11878 for (RelocIterator it(this); !it.done(); it.next()) {
11879 it.rinfo()->Print(GetIsolate(), os);
11883 #endif // ENABLE_DISASSEMBLER
11886 void BytecodeArray::Disassemble(std::ostream& os) {
11887 os << "Parameter count " << parameter_count() << "\n";
11888 os << "Frame size " << frame_size() << "\n";
11889 Vector<char> buf = Vector<char>::New(50);
11891 const uint8_t* first_bytecode_address = GetFirstBytecodeAddress();
11892 int bytecode_size = 0;
11893 for (int i = 0; i < this->length(); i += bytecode_size) {
11894 const uint8_t* bytecode_start = &first_bytecode_address[i];
11895 interpreter::Bytecode bytecode =
11896 interpreter::Bytecodes::FromByte(bytecode_start[0]);
11897 bytecode_size = interpreter::Bytecodes::Size(bytecode);
11899 SNPrintF(buf, "%p", bytecode_start);
11900 os << buf.start() << " : ";
11901 interpreter::Bytecodes::Decode(os, bytecode_start);
11905 os << "Constant pool (size = " << constant_pool()->length() << ")\n";
11906 constant_pool()->Print();
11911 void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) {
11912 DCHECK(capacity >= 0);
11913 array->GetIsolate()->factory()->NewJSArrayStorage(
11914 array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
11918 // Returns false if the passed-in index is marked non-configurable, which will
11919 // cause the truncation operation to halt, and thus no further old values need
11921 static bool GetOldValue(Isolate* isolate,
11922 Handle<JSObject> object,
11924 List<Handle<Object> >* old_values,
11925 List<uint32_t>* indices) {
11926 LookupIterator it(isolate, object, index, LookupIterator::HIDDEN);
11927 CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust());
11928 DCHECK(it.IsFound());
11929 if (!it.IsConfigurable()) return false;
11930 Handle<Object> value =
11931 it.state() == LookupIterator::ACCESSOR
11932 ? Handle<Object>::cast(isolate->factory()->the_hole_value())
11933 : JSReceiver::GetDataProperty(&it);
11934 old_values->Add(value);
11935 indices->Add(index);
11940 void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) {
11941 // We should never end in here with a pixel or external array.
11942 DCHECK(array->AllowsSetLength());
11943 if (array->SetLengthWouldNormalize(new_length)) {
11944 JSObject::NormalizeElements(array);
11946 array->GetElementsAccessor()->SetLength(array, new_length);
11950 MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array,
11951 uint32_t new_length) {
11952 if (!array->map()->is_observed()) {
11953 SetLength(array, new_length);
11957 Isolate* isolate = array->GetIsolate();
11958 List<uint32_t> indices;
11959 List<Handle<Object> > old_values;
11960 Handle<Object> old_length_handle(array->length(), isolate);
11961 uint32_t old_length = 0;
11962 CHECK(old_length_handle->ToArrayLength(&old_length));
11964 static const PropertyAttributes kNoAttrFilter = NONE;
11965 int num_elements = array->NumberOfOwnElements(kNoAttrFilter);
11966 if (num_elements > 0) {
11967 if (old_length == static_cast<uint32_t>(num_elements)) {
11968 // Simple case for arrays without holes.
11969 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) {
11970 if (!GetOldValue(isolate, array, i, &old_values, &indices)) break;
11973 // For sparse arrays, only iterate over existing elements.
11974 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over
11975 // the to-be-removed indices twice.
11976 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements);
11977 array->GetOwnElementKeys(*keys, kNoAttrFilter);
11978 while (num_elements-- > 0) {
11979 uint32_t index = NumberToUint32(keys->get(num_elements));
11980 if (index < new_length) break;
11981 if (!GetOldValue(isolate, array, index, &old_values, &indices)) break;
11986 SetLength(array, new_length);
11988 CHECK(array->length()->ToArrayLength(&new_length));
11989 if (old_length == new_length) return array;
11991 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
11993 for (int i = 0; i < indices.length(); ++i) {
11994 // For deletions where the property was an accessor, old_values[i]
11995 // will be the hole, which instructs EnqueueChangeRecord to elide
11996 // the "oldValue" property.
11997 RETURN_ON_EXCEPTION(
11999 JSObject::EnqueueChangeRecord(
12000 array, "delete", isolate->factory()->Uint32ToString(indices[i]),
12005 RETURN_ON_EXCEPTION(isolate,
12006 JSObject::EnqueueChangeRecord(
12007 array, "update", isolate->factory()->length_string(),
12008 old_length_handle),
12011 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
12013 uint32_t index = Min(old_length, new_length);
12014 uint32_t add_count = new_length > old_length ? new_length - old_length : 0;
12015 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0;
12016 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
12017 if (delete_count > 0) {
12018 for (int i = indices.length() - 1; i >= 0; i--) {
12019 // Skip deletions where the property was an accessor, leaving holes
12020 // in the array of old values.
12021 if (old_values[i]->IsTheHole()) continue;
12022 JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE)
12026 JSArray::SetLength(deleted, delete_count);
12029 RETURN_ON_EXCEPTION(
12030 isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object);
12037 void Map::AddDependentCode(Handle<Map> map,
12038 DependentCode::DependencyGroup group,
12039 Handle<Code> code) {
12040 Handle<WeakCell> cell = Code::WeakCellFor(code);
12041 Handle<DependentCode> codes = DependentCode::InsertWeakCode(
12042 Handle<DependentCode>(map->dependent_code()), group, cell);
12043 if (*codes != map->dependent_code()) map->set_dependent_code(*codes);
12047 DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) {
12048 Recompute(entries);
12052 void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) {
12053 start_indexes_[0] = 0;
12054 for (int g = 1; g <= kGroupCount; g++) {
12055 int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1));
12056 start_indexes_[g] = start_indexes_[g - 1] + count;
12061 Handle<DependentCode> DependentCode::InsertCompilationDependencies(
12062 Handle<DependentCode> entries, DependencyGroup group,
12063 Handle<Foreign> info) {
12064 return Insert(entries, group, info);
12068 Handle<DependentCode> DependentCode::InsertWeakCode(
12069 Handle<DependentCode> entries, DependencyGroup group,
12070 Handle<WeakCell> code_cell) {
12071 return Insert(entries, group, code_cell);
12075 Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries,
12076 DependencyGroup group,
12077 Handle<Object> object) {
12078 GroupStartIndexes starts(*entries);
12079 int start = starts.at(group);
12080 int end = starts.at(group + 1);
12081 int number_of_entries = starts.number_of_entries();
12082 // Check for existing entry to avoid duplicates.
12083 for (int i = start; i < end; i++) {
12084 if (entries->object_at(i) == *object) return entries;
12086 if (entries->length() < kCodesStartIndex + number_of_entries + 1) {
12087 entries = EnsureSpace(entries);
12088 // The number of codes can change after Compact and GC.
12089 starts.Recompute(*entries);
12090 start = starts.at(group);
12091 end = starts.at(group + 1);
12094 entries->ExtendGroup(group);
12095 entries->set_object_at(end, *object);
12096 entries->set_number_of_entries(group, end + 1 - start);
12101 Handle<DependentCode> DependentCode::EnsureSpace(
12102 Handle<DependentCode> entries) {
12103 Isolate* isolate = entries->GetIsolate();
12104 if (entries->length() == 0) {
12105 entries = Handle<DependentCode>::cast(
12106 isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED));
12107 for (int g = 0; g < kGroupCount; g++) {
12108 entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0);
12112 if (entries->Compact()) return entries;
12113 GroupStartIndexes starts(*entries);
12115 kCodesStartIndex + DependentCode::Grow(starts.number_of_entries());
12116 int grow_by = capacity - entries->length();
12117 return Handle<DependentCode>::cast(
12118 isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED));
12122 bool DependentCode::Compact() {
12123 GroupStartIndexes starts(this);
12125 for (int g = 0; g < kGroupCount; g++) {
12126 int start = starts.at(g);
12127 int end = starts.at(g + 1);
12129 DCHECK(start >= n);
12130 for (int i = start; i < end; i++) {
12131 Object* obj = object_at(i);
12132 if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) {
12133 if (i != n + count) {
12134 copy(i, n + count);
12139 if (count != end - start) {
12140 set_number_of_entries(static_cast<DependencyGroup>(g), count);
12144 return n < starts.number_of_entries();
12148 void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info,
12149 WeakCell* code_cell) {
12150 DisallowHeapAllocation no_gc;
12151 GroupStartIndexes starts(this);
12152 int start = starts.at(group);
12153 int end = starts.at(group + 1);
12154 for (int i = start; i < end; i++) {
12155 if (object_at(i) == info) {
12156 set_object_at(i, code_cell);
12162 for (int i = start; i < end; i++) {
12163 DCHECK(object_at(i) != info);
12169 void DependentCode::RemoveCompilationDependencies(
12170 DependentCode::DependencyGroup group, Foreign* info) {
12171 DisallowHeapAllocation no_allocation;
12172 GroupStartIndexes starts(this);
12173 int start = starts.at(group);
12174 int end = starts.at(group + 1);
12175 // Find compilation info wrapper.
12177 for (int i = start; i < end; i++) {
12178 if (object_at(i) == info) {
12183 if (info_pos == -1) return; // Not found.
12184 int gap = info_pos;
12185 // Use the last of each group to fill the gap in the previous group.
12186 for (int i = group; i < kGroupCount; i++) {
12187 int last_of_group = starts.at(i + 1) - 1;
12188 DCHECK(last_of_group >= gap);
12189 if (last_of_group == gap) continue;
12190 copy(last_of_group, gap);
12191 gap = last_of_group;
12193 DCHECK(gap == starts.number_of_entries() - 1);
12194 clear_at(gap); // Clear last gap.
12195 set_number_of_entries(group, end - start - 1);
12198 for (int i = start; i < end - 1; i++) {
12199 DCHECK(object_at(i) != info);
12205 bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) {
12206 GroupStartIndexes starts(this);
12207 int start = starts.at(group);
12208 int end = starts.at(group + 1);
12209 for (int i = start; i < end; i++) {
12210 if (object_at(i) == code_cell) return true;
12216 bool DependentCode::MarkCodeForDeoptimization(
12218 DependentCode::DependencyGroup group) {
12219 DisallowHeapAllocation no_allocation_scope;
12220 DependentCode::GroupStartIndexes starts(this);
12221 int start = starts.at(group);
12222 int end = starts.at(group + 1);
12223 int code_entries = starts.number_of_entries();
12224 if (start == end) return false;
12226 // Mark all the code that needs to be deoptimized.
12227 bool marked = false;
12228 bool invalidate_embedded_objects = group == kWeakCodeGroup;
12229 for (int i = start; i < end; i++) {
12230 Object* obj = object_at(i);
12231 if (obj->IsWeakCell()) {
12232 WeakCell* cell = WeakCell::cast(obj);
12233 if (cell->cleared()) continue;
12234 Code* code = Code::cast(cell->value());
12235 if (!code->marked_for_deoptimization()) {
12236 SetMarkedForDeoptimization(code, group);
12237 if (invalidate_embedded_objects) {
12238 code->InvalidateEmbeddedObjects();
12243 DCHECK(obj->IsForeign());
12244 CompilationDependencies* info =
12245 reinterpret_cast<CompilationDependencies*>(
12246 Foreign::cast(obj)->foreign_address());
12250 // Compact the array by moving all subsequent groups to fill in the new holes.
12251 for (int src = end, dst = start; src < code_entries; src++, dst++) {
12254 // Now the holes are at the end of the array, zap them for heap-verifier.
12255 int removed = end - start;
12256 for (int i = code_entries - removed; i < code_entries; i++) {
12259 set_number_of_entries(group, 0);
12264 void DependentCode::DeoptimizeDependentCodeGroup(
12266 DependentCode::DependencyGroup group) {
12267 DCHECK(AllowCodeDependencyChange::IsAllowed());
12268 DisallowHeapAllocation no_allocation_scope;
12269 bool marked = MarkCodeForDeoptimization(isolate, group);
12270 if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate);
12274 void DependentCode::SetMarkedForDeoptimization(Code* code,
12275 DependencyGroup group) {
12276 code->set_marked_for_deoptimization(true);
12277 if (FLAG_trace_deopt &&
12278 (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) {
12279 DeoptimizationInputData* deopt_data =
12280 DeoptimizationInputData::cast(code->deoptimization_data());
12281 CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
12282 PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR
12283 " (opt #%d) for deoptimization, reason: %s]\n",
12284 reinterpret_cast<intptr_t>(code),
12285 deopt_data->OptimizationId()->value(), DependencyGroupName(group));
12290 const char* DependentCode::DependencyGroupName(DependencyGroup group) {
12292 case kWeakCodeGroup:
12293 return "weak-code";
12294 case kTransitionGroup:
12295 return "transition";
12296 case kPrototypeCheckGroup:
12297 return "prototype-check";
12298 case kPropertyCellChangedGroup:
12299 return "property-cell-changed";
12300 case kFieldTypeGroup:
12301 return "field-type";
12302 case kInitialMapChangedGroup:
12303 return "initial-map-changed";
12304 case kAllocationSiteTenuringChangedGroup:
12305 return "allocation-site-tenuring-changed";
12306 case kAllocationSiteTransitionChangedGroup:
12307 return "allocation-site-transition-changed";
12314 Handle<Map> Map::TransitionToPrototype(Handle<Map> map,
12315 Handle<Object> prototype,
12316 PrototypeOptimizationMode mode) {
12317 Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype);
12318 if (new_map.is_null()) {
12319 new_map = Copy(map, "TransitionToPrototype");
12320 TransitionArray::PutPrototypeTransition(map, prototype, new_map);
12321 Map::SetPrototype(new_map, prototype, mode);
12327 MaybeHandle<Object> JSObject::SetPrototype(Handle<JSObject> object,
12328 Handle<Object> value,
12329 bool from_javascript) {
12331 int size = object->Size();
12334 Isolate* isolate = object->GetIsolate();
12335 // Strong objects may not have their prototype set via __proto__ or
12337 if (from_javascript && object->map()->is_strong()) {
12338 THROW_NEW_ERROR(isolate,
12339 NewTypeError(MessageTemplate::kStrongSetProto, object),
12342 Heap* heap = isolate->heap();
12343 // Silently ignore the change if value is not a JSObject or null.
12344 // SpiderMonkey behaves this way.
12345 if (!value->IsJSReceiver() && !value->IsNull()) return value;
12347 // From 8.6.2 Object Internal Methods
12349 // In addition, if [[Extensible]] is false the value of the [[Class]] and
12350 // [[Prototype]] internal properties of the object may not be modified.
12352 // Implementation specific extensions that modify [[Class]], [[Prototype]]
12353 // or [[Extensible]] must not violate the invariants defined in the preceding
12355 if (!object->map()->is_extensible()) {
12356 THROW_NEW_ERROR(isolate,
12357 NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12361 // Before we can set the prototype we need to be sure
12362 // prototype cycles are prevented.
12363 // It is sufficient to validate that the receiver is not in the new prototype
12365 for (PrototypeIterator iter(isolate, *value,
12366 PrototypeIterator::START_AT_RECEIVER);
12367 !iter.IsAtEnd(); iter.Advance()) {
12368 if (JSReceiver::cast(iter.GetCurrent()) == *object) {
12370 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kCyclicProto),
12375 bool dictionary_elements_in_chain =
12376 object->map()->DictionaryElementsInPrototypeChainOnly();
12377 Handle<JSObject> real_receiver = object;
12379 if (from_javascript) {
12380 // Find the first object in the chain whose prototype object is not
12381 // hidden and set the new prototype on that object.
12382 PrototypeIterator iter(isolate, real_receiver);
12383 while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
12385 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
12387 if (!real_receiver->map()->is_extensible()) {
12389 isolate, NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12395 // Set the new prototype of the object.
12396 Handle<Map> map(real_receiver->map());
12398 // Nothing to do if prototype is already set.
12399 if (map->prototype() == *value) return value;
12401 isolate->UpdateArrayProtectorOnSetPrototype(real_receiver);
12403 PrototypeOptimizationMode mode =
12404 from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE;
12405 Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode);
12406 DCHECK(new_map->prototype() == *value);
12407 JSObject::MigrateToMap(real_receiver, new_map);
12409 if (from_javascript && !dictionary_elements_in_chain &&
12410 new_map->DictionaryElementsInPrototypeChainOnly()) {
12411 // If the prototype chain didn't previously have element callbacks, then
12412 // KeyedStoreICs need to be cleared to ensure any that involve this
12414 object->GetHeap()->ClearAllKeyedStoreICs();
12417 heap->ClearInstanceofCache();
12418 DCHECK(size == object->Size());
12423 void JSObject::EnsureCanContainElements(Handle<JSObject> object,
12425 uint32_t first_arg,
12426 uint32_t arg_count,
12427 EnsureElementsMode mode) {
12428 // Elements in |Arguments| are ordered backwards (because they're on the
12429 // stack), but the method that's called here iterates over them in forward
12431 return EnsureCanContainElements(
12432 object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode);
12436 ElementsAccessor* JSObject::GetElementsAccessor() {
12437 return ElementsAccessor::ForKind(GetElementsKind());
12441 void JSObject::ValidateElements(Handle<JSObject> object) {
12442 #ifdef ENABLE_SLOW_DCHECKS
12443 if (FLAG_enable_slow_asserts) {
12444 ElementsAccessor* accessor = object->GetElementsAccessor();
12445 accessor->Validate(object);
12451 static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity,
12453 uint32_t* new_capacity) {
12454 STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <=
12455 JSObject::kMaxUncheckedFastElementsLength);
12456 if (index < capacity) {
12457 *new_capacity = capacity;
12460 if (index - capacity >= JSObject::kMaxGap) return true;
12461 *new_capacity = JSObject::NewElementsCapacity(index + 1);
12462 DCHECK_LT(index, *new_capacity);
12463 if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength ||
12464 (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength &&
12465 object->GetHeap()->InNewSpace(object))) {
12468 // If the fast-case backing storage takes up roughly three times as
12469 // much space (in machine words) as a dictionary backing storage
12470 // would, the object should have slow elements.
12471 int used_elements = object->GetFastElementsUsage();
12472 int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) *
12473 SeededNumberDictionary::kEntrySize;
12474 return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity;
12478 bool JSObject::WouldConvertToSlowElements(uint32_t index) {
12479 if (HasFastElements()) {
12480 Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements()));
12481 uint32_t capacity = static_cast<uint32_t>(backing_store->length());
12482 uint32_t new_capacity;
12483 return ShouldConvertToSlowElements(this, capacity, index, &new_capacity);
12489 static ElementsKind BestFittingFastElementsKind(JSObject* object) {
12490 if (object->HasSloppyArgumentsElements()) {
12491 return FAST_SLOPPY_ARGUMENTS_ELEMENTS;
12493 DCHECK(object->HasDictionaryElements());
12494 SeededNumberDictionary* dictionary = object->element_dictionary();
12495 ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS;
12496 for (int i = 0; i < dictionary->Capacity(); i++) {
12497 Object* key = dictionary->KeyAt(i);
12498 if (key->IsNumber()) {
12499 Object* value = dictionary->ValueAt(i);
12500 if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS;
12501 if (!value->IsSmi()) {
12502 if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS;
12503 kind = FAST_HOLEY_DOUBLE_ELEMENTS;
12511 static bool ShouldConvertToFastElements(JSObject* object,
12512 SeededNumberDictionary* dictionary,
12514 uint32_t* new_capacity) {
12515 // If properties with non-standard attributes or accessors were added, we
12516 // cannot go back to fast elements.
12517 if (dictionary->requires_slow_elements()) return false;
12519 // Adding a property with this index will require slow elements.
12520 if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false;
12522 if (object->IsJSArray()) {
12523 Object* length = JSArray::cast(object)->length();
12524 if (!length->IsSmi()) return false;
12525 *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value());
12527 *new_capacity = dictionary->max_number_key() + 1;
12529 *new_capacity = Max(index + 1, *new_capacity);
12531 uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) *
12532 SeededNumberDictionary::kEntrySize;
12533 return 2 * dictionary_size >= *new_capacity;
12538 MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object,
12540 Handle<Object> value,
12541 PropertyAttributes attributes) {
12542 DCHECK(object->map()->is_extensible());
12544 Isolate* isolate = object->GetIsolate();
12546 uint32_t old_length = 0;
12547 uint32_t new_capacity = 0;
12549 Handle<Object> old_length_handle;
12550 if (object->IsJSArray()) {
12551 CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length));
12552 if (object->map()->is_observed()) {
12553 old_length_handle = handle(JSArray::cast(*object)->length(), isolate);
12557 ElementsKind kind = object->GetElementsKind();
12558 FixedArrayBase* elements = object->elements();
12559 ElementsKind dictionary_kind = DICTIONARY_ELEMENTS;
12560 if (IsSloppyArgumentsElements(kind)) {
12561 elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1));
12562 dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS;
12565 if (attributes != NONE) {
12566 kind = dictionary_kind;
12567 } else if (elements->IsSeededNumberDictionary()) {
12568 kind = ShouldConvertToFastElements(*object,
12569 SeededNumberDictionary::cast(elements),
12570 index, &new_capacity)
12571 ? BestFittingFastElementsKind(*object)
12572 : dictionary_kind; // Overwrite in case of arguments.
12573 } else if (ShouldConvertToSlowElements(
12574 *object, static_cast<uint32_t>(elements->length()), index,
12576 kind = dictionary_kind;
12579 ElementsKind to = value->OptimalElementsKind();
12580 if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) {
12581 to = GetHoleyElementsKind(to);
12582 kind = GetHoleyElementsKind(kind);
12584 to = IsMoreGeneralElementsKindTransition(kind, to) ? to : kind;
12585 ElementsAccessor* accessor = ElementsAccessor::ForKind(to);
12586 accessor->Add(object, index, value, attributes, new_capacity);
12588 uint32_t new_length = old_length;
12589 Handle<Object> new_length_handle;
12590 if (object->IsJSArray() && index >= old_length) {
12591 new_length = index + 1;
12592 new_length_handle = isolate->factory()->NewNumberFromUint(new_length);
12593 JSArray::cast(*object)->set_length(*new_length_handle);
12596 if (!old_length_handle.is_null() && new_length != old_length) {
12597 // |old_length_handle| is kept null above unless the object is observed.
12598 DCHECK(object->map()->is_observed());
12599 Handle<JSArray> array = Handle<JSArray>::cast(object);
12600 Handle<String> name = isolate->factory()->Uint32ToString(index);
12602 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
12603 RETURN_ON_EXCEPTION(
12604 isolate, EnqueueChangeRecord(array, "add", name,
12605 isolate->factory()->the_hole_value()),
12607 RETURN_ON_EXCEPTION(isolate,
12608 EnqueueChangeRecord(array, "update",
12609 isolate->factory()->length_string(),
12610 old_length_handle),
12612 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
12613 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
12614 RETURN_ON_EXCEPTION(isolate, EnqueueSpliceRecord(array, old_length, deleted,
12615 new_length - old_length),
12617 } else if (object->map()->is_observed()) {
12618 Handle<String> name = isolate->factory()->Uint32ToString(index);
12619 RETURN_ON_EXCEPTION(
12620 isolate, EnqueueChangeRecord(object, "add", name,
12621 isolate->factory()->the_hole_value()),
12629 bool JSArray::SetLengthWouldNormalize(uint32_t new_length) {
12630 if (!HasFastElements()) return false;
12631 uint32_t capacity = static_cast<uint32_t>(elements()->length());
12632 uint32_t new_capacity;
12633 return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) &&
12634 ShouldConvertToSlowElements(this, capacity, new_length - 1,
12639 const double AllocationSite::kPretenureRatio = 0.85;
12642 void AllocationSite::ResetPretenureDecision() {
12643 set_pretenure_decision(kUndecided);
12644 set_memento_found_count(0);
12645 set_memento_create_count(0);
12649 PretenureFlag AllocationSite::GetPretenureMode() {
12650 PretenureDecision mode = pretenure_decision();
12651 // Zombie objects "decide" to be untenured.
12652 return mode == kTenure ? TENURED : NOT_TENURED;
12656 bool AllocationSite::IsNestedSite() {
12657 DCHECK(FLAG_trace_track_allocation_sites);
12658 Object* current = GetHeap()->allocation_sites_list();
12659 while (current->IsAllocationSite()) {
12660 AllocationSite* current_site = AllocationSite::cast(current);
12661 if (current_site->nested_site() == this) {
12664 current = current_site->weak_next();
12670 void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site,
12671 ElementsKind to_kind) {
12672 Isolate* isolate = site->GetIsolate();
12674 if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) {
12675 Handle<JSArray> transition_info =
12676 handle(JSArray::cast(site->transition_info()));
12677 ElementsKind kind = transition_info->GetElementsKind();
12678 // if kind is holey ensure that to_kind is as well.
12679 if (IsHoleyElementsKind(kind)) {
12680 to_kind = GetHoleyElementsKind(to_kind);
12682 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12683 // If the array is huge, it's not likely to be defined in a local
12684 // function, so we shouldn't make new instances of it very often.
12685 uint32_t length = 0;
12686 CHECK(transition_info->length()->ToArrayLength(&length));
12687 if (length <= kMaximumArrayBytesToPretransition) {
12688 if (FLAG_trace_track_allocation_sites) {
12689 bool is_nested = site->IsNestedSite();
12691 "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n",
12692 reinterpret_cast<void*>(*site),
12693 is_nested ? "(nested)" : "",
12694 ElementsKindToString(kind),
12695 ElementsKindToString(to_kind));
12697 JSObject::TransitionElementsKind(transition_info, to_kind);
12698 site->dependent_code()->DeoptimizeDependentCodeGroup(
12699 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12703 ElementsKind kind = site->GetElementsKind();
12704 // if kind is holey ensure that to_kind is as well.
12705 if (IsHoleyElementsKind(kind)) {
12706 to_kind = GetHoleyElementsKind(to_kind);
12708 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12709 if (FLAG_trace_track_allocation_sites) {
12710 PrintF("AllocationSite: JSArray %p site updated %s->%s\n",
12711 reinterpret_cast<void*>(*site),
12712 ElementsKindToString(kind),
12713 ElementsKindToString(to_kind));
12715 site->SetElementsKind(to_kind);
12716 site->dependent_code()->DeoptimizeDependentCodeGroup(
12717 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12723 const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) {
12724 switch (decision) {
12725 case kUndecided: return "undecided";
12726 case kDontTenure: return "don't tenure";
12727 case kMaybeTenure: return "maybe tenure";
12728 case kTenure: return "tenure";
12729 case kZombie: return "zombie";
12730 default: UNREACHABLE();
12736 void JSObject::UpdateAllocationSite(Handle<JSObject> object,
12737 ElementsKind to_kind) {
12738 if (!object->IsJSArray()) return;
12740 Heap* heap = object->GetHeap();
12741 if (!heap->InNewSpace(*object)) return;
12743 Handle<AllocationSite> site;
12745 DisallowHeapAllocation no_allocation;
12747 AllocationMemento* memento = heap->FindAllocationMemento(*object);
12748 if (memento == NULL) return;
12750 // Walk through to the Allocation Site
12751 site = handle(memento->GetAllocationSite());
12753 AllocationSite::DigestTransitionFeedback(site, to_kind);
12757 void JSObject::TransitionElementsKind(Handle<JSObject> object,
12758 ElementsKind to_kind) {
12759 ElementsKind from_kind = object->GetElementsKind();
12761 if (IsFastHoleyElementsKind(from_kind)) {
12762 to_kind = GetHoleyElementsKind(to_kind);
12765 if (from_kind == to_kind) return;
12767 // This method should never be called for any other case.
12768 DCHECK(IsFastElementsKind(from_kind));
12769 DCHECK(IsFastElementsKind(to_kind));
12770 DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind);
12772 UpdateAllocationSite(object, to_kind);
12773 if (object->elements() == object->GetHeap()->empty_fixed_array() ||
12774 IsFastDoubleElementsKind(from_kind) ==
12775 IsFastDoubleElementsKind(to_kind)) {
12776 // No change is needed to the elements() buffer, the transition
12777 // only requires a map change.
12778 Handle<Map> new_map = GetElementsTransitionMap(object, to_kind);
12779 MigrateToMap(object, new_map);
12780 if (FLAG_trace_elements_transitions) {
12781 Handle<FixedArrayBase> elms(object->elements());
12782 PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms);
12785 DCHECK((IsFastSmiElementsKind(from_kind) &&
12786 IsFastDoubleElementsKind(to_kind)) ||
12787 (IsFastDoubleElementsKind(from_kind) &&
12788 IsFastObjectElementsKind(to_kind)));
12789 uint32_t c = static_cast<uint32_t>(object->elements()->length());
12790 ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c);
12796 bool Map::IsValidElementsTransition(ElementsKind from_kind,
12797 ElementsKind to_kind) {
12798 // Transitions can't go backwards.
12799 if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) {
12803 // Transitions from HOLEY -> PACKED are not allowed.
12804 return !IsFastHoleyElementsKind(from_kind) ||
12805 IsFastHoleyElementsKind(to_kind);
12809 bool JSArray::HasReadOnlyLength(Handle<JSArray> array) {
12810 LookupIterator it(array, array->GetIsolate()->factory()->length_string(),
12811 LookupIterator::OWN_SKIP_INTERCEPTOR);
12812 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
12813 CHECK(it.IsFound());
12814 CHECK_EQ(LookupIterator::ACCESSOR, it.state());
12815 return it.IsReadOnly();
12819 bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array,
12821 uint32_t length = 0;
12822 CHECK(array->length()->ToArrayLength(&length));
12823 if (length <= index) return HasReadOnlyLength(array);
12828 MaybeHandle<Object> JSArray::ReadOnlyLengthError(Handle<JSArray> array) {
12829 Isolate* isolate = array->GetIsolate();
12830 Handle<Name> length = isolate->factory()->length_string();
12833 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, length, array),
12838 template <typename BackingStore>
12839 static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) {
12840 int limit = object->IsJSArray()
12841 ? Smi::cast(JSArray::cast(object)->length())->value()
12844 for (int i = 0; i < limit; ++i) {
12845 if (!store->is_the_hole(i)) ++used;
12851 int JSObject::GetFastElementsUsage() {
12852 FixedArrayBase* store = elements();
12853 switch (GetElementsKind()) {
12854 case FAST_SMI_ELEMENTS:
12855 case FAST_DOUBLE_ELEMENTS:
12856 case FAST_ELEMENTS:
12857 // Only JSArray have packed elements.
12858 return Smi::cast(JSArray::cast(this)->length())->value();
12859 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
12860 store = FixedArray::cast(FixedArray::cast(store)->get(1));
12862 case FAST_HOLEY_SMI_ELEMENTS:
12863 case FAST_HOLEY_ELEMENTS:
12864 return FastHoleyElementsUsage(this, FixedArray::cast(store));
12865 case FAST_HOLEY_DOUBLE_ELEMENTS:
12866 if (elements()->length() == 0) return 0;
12867 return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store));
12869 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
12870 case DICTIONARY_ELEMENTS:
12871 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
12872 case TYPE##_ELEMENTS: \
12874 TYPED_ARRAYS(TYPED_ARRAY_CASE)
12875 #undef TYPED_ARRAY_CASE
12882 // Certain compilers request function template instantiation when they
12883 // see the definition of the other template functions in the
12884 // class. This requires us to have the template functions put
12885 // together, so even though this function belongs in objects-debug.cc,
12886 // we keep it here instead to satisfy certain compilers.
12887 #ifdef OBJECT_PRINT
12888 template <typename Derived, typename Shape, typename Key>
12889 void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT
12890 int capacity = this->Capacity();
12891 for (int i = 0; i < capacity; i++) {
12892 Object* k = this->KeyAt(i);
12893 if (this->IsKey(k)) {
12895 if (k->IsString()) {
12896 String::cast(k)->StringPrint(os);
12900 os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i)
12908 template<typename Derived, typename Shape, typename Key>
12909 void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) {
12911 int capacity = this->Capacity();
12912 DisallowHeapAllocation no_gc;
12913 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
12914 for (int i = 0; i < capacity; i++) {
12915 Object* k = this->KeyAt(i);
12916 if (this->IsKey(k)) {
12917 elements->set(pos++, this->ValueAt(i), mode);
12920 DCHECK(pos == elements->length());
12924 InterceptorInfo* JSObject::GetNamedInterceptor() {
12925 DCHECK(map()->has_named_interceptor());
12926 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12927 DCHECK(constructor->shared()->IsApiFunction());
12929 constructor->shared()->get_api_func_data()->named_property_handler();
12930 return InterceptorInfo::cast(result);
12934 InterceptorInfo* JSObject::GetIndexedInterceptor() {
12935 DCHECK(map()->has_indexed_interceptor());
12936 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12937 DCHECK(constructor->shared()->IsApiFunction());
12939 constructor->shared()->get_api_func_data()->indexed_property_handler();
12940 return InterceptorInfo::cast(result);
12944 MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it,
12947 Isolate* isolate = it->isolate();
12948 // Make sure that the top context does not change when doing callbacks or
12949 // interceptor calls.
12950 AssertNoContextChange ncc(isolate);
12952 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
12953 Handle<InterceptorInfo> interceptor = it->GetInterceptor();
12954 if (interceptor->getter()->IsUndefined()) {
12955 return isolate->factory()->undefined_value();
12958 Handle<JSObject> holder = it->GetHolder<JSObject>();
12959 v8::Local<v8::Value> result;
12960 PropertyCallbackArguments args(isolate, interceptor->data(),
12961 *it->GetReceiver(), *holder);
12963 if (it->IsElement()) {
12964 uint32_t index = it->index();
12965 v8::IndexedPropertyGetterCallback getter =
12966 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
12968 ApiIndexedPropertyAccess("interceptor-indexed-get", *holder, index));
12969 result = args.Call(getter, index);
12971 Handle<Name> name = it->name();
12973 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
12974 return isolate->factory()->undefined_value();
12977 v8::GenericNamedPropertyGetterCallback getter =
12978 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
12979 interceptor->getter());
12981 ApiNamedPropertyAccess("interceptor-named-get", *holder, *name));
12982 result = args.Call(getter, v8::Utils::ToLocal(name));
12985 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
12986 if (result.IsEmpty()) return isolate->factory()->undefined_value();
12987 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
12988 result_internal->VerifyApiCallResultType();
12990 // Rebox handle before return
12991 return handle(*result_internal, isolate);
12995 // Compute the property keys from the interceptor.
12996 MaybeHandle<JSObject> JSObject::GetKeysForNamedInterceptor(
12997 Handle<JSObject> object, Handle<JSReceiver> receiver) {
12998 Isolate* isolate = receiver->GetIsolate();
12999 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
13000 PropertyCallbackArguments
13001 args(isolate, interceptor->data(), *receiver, *object);
13002 v8::Local<v8::Object> result;
13003 if (!interceptor->enumerator()->IsUndefined()) {
13004 v8::GenericNamedPropertyEnumeratorCallback enum_fun =
13005 v8::ToCData<v8::GenericNamedPropertyEnumeratorCallback>(
13006 interceptor->enumerator());
13007 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
13008 result = args.Call(enum_fun);
13010 if (result.IsEmpty()) return MaybeHandle<JSObject>();
13011 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
13012 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
13013 // Rebox before returning.
13014 return handle(*v8::Utils::OpenHandle(*result), isolate);
13018 // Compute the element keys from the interceptor.
13019 MaybeHandle<JSObject> JSObject::GetKeysForIndexedInterceptor(
13020 Handle<JSObject> object, Handle<JSReceiver> receiver) {
13021 Isolate* isolate = receiver->GetIsolate();
13022 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
13023 PropertyCallbackArguments
13024 args(isolate, interceptor->data(), *receiver, *object);
13025 v8::Local<v8::Object> result;
13026 if (!interceptor->enumerator()->IsUndefined()) {
13027 v8::IndexedPropertyEnumeratorCallback enum_fun =
13028 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>(
13029 interceptor->enumerator());
13030 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
13031 result = args.Call(enum_fun);
13033 if (result.IsEmpty()) return MaybeHandle<JSObject>();
13034 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
13035 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
13036 // Rebox before returning.
13037 return handle(*v8::Utils::OpenHandle(*result), isolate);
13041 Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object,
13042 Handle<Name> name) {
13043 LookupIterator it = LookupIterator::PropertyOrElement(
13044 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
13045 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
13046 if (!maybe_result.IsJust()) return Nothing<bool>();
13047 return Just(it.IsFound());
13051 Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object,
13053 Isolate* isolate = object->GetIsolate();
13054 LookupIterator it(isolate, object, index,
13055 LookupIterator::OWN_SKIP_INTERCEPTOR);
13056 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
13057 if (!maybe_result.IsJust()) return Nothing<bool>();
13058 return Just(it.IsFound());
13062 Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object,
13063 Handle<Name> name) {
13064 LookupIterator it = LookupIterator::PropertyOrElement(
13065 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
13066 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
13067 return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR)
13072 int JSObject::NumberOfOwnProperties(PropertyAttributes filter) {
13073 if (HasFastProperties()) {
13074 Map* map = this->map();
13075 if (filter == NONE) return map->NumberOfOwnDescriptors();
13076 if (filter & DONT_ENUM) {
13077 int result = map->EnumLength();
13078 if (result != kInvalidEnumCacheSentinel) return result;
13080 return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter);
13081 } else if (IsGlobalObject()) {
13082 return global_dictionary()->NumberOfElementsFilterAttributes(filter);
13084 return property_dictionary()->NumberOfElementsFilterAttributes(filter);
13089 void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) {
13090 Object* temp = get(i);
13093 if (this != numbers) {
13094 temp = numbers->get(i);
13095 numbers->set(i, Smi::cast(numbers->get(j)));
13096 numbers->set(j, Smi::cast(temp));
13101 static void InsertionSortPairs(FixedArray* content,
13102 FixedArray* numbers,
13104 for (int i = 1; i < len; i++) {
13107 (NumberToUint32(numbers->get(j - 1)) >
13108 NumberToUint32(numbers->get(j)))) {
13109 content->SwapPairs(numbers, j - 1, j);
13116 void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) {
13117 // In-place heap sort.
13118 DCHECK(content->length() == numbers->length());
13120 // Bottom-up max-heap construction.
13121 for (int i = 1; i < len; ++i) {
13122 int child_index = i;
13123 while (child_index > 0) {
13124 int parent_index = ((child_index + 1) >> 1) - 1;
13125 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
13126 uint32_t child_value = NumberToUint32(numbers->get(child_index));
13127 if (parent_value < child_value) {
13128 content->SwapPairs(numbers, parent_index, child_index);
13132 child_index = parent_index;
13136 // Extract elements and create sorted array.
13137 for (int i = len - 1; i > 0; --i) {
13138 // Put max element at the back of the array.
13139 content->SwapPairs(numbers, 0, i);
13140 // Sift down the new top element.
13141 int parent_index = 0;
13143 int child_index = ((parent_index + 1) << 1) - 1;
13144 if (child_index >= i) break;
13145 uint32_t child1_value = NumberToUint32(numbers->get(child_index));
13146 uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1));
13147 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
13148 if (child_index + 1 >= i || child1_value > child2_value) {
13149 if (parent_value > child1_value) break;
13150 content->SwapPairs(numbers, parent_index, child_index);
13151 parent_index = child_index;
13153 if (parent_value > child2_value) break;
13154 content->SwapPairs(numbers, parent_index, child_index + 1);
13155 parent_index = child_index + 1;
13162 // Sort this array and the numbers as pairs wrt. the (distinct) numbers.
13163 void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) {
13164 DCHECK(this->length() == numbers->length());
13165 // For small arrays, simply use insertion sort.
13167 InsertionSortPairs(this, numbers, len);
13170 // Check the range of indices.
13171 uint32_t min_index = NumberToUint32(numbers->get(0));
13172 uint32_t max_index = min_index;
13174 for (i = 1; i < len; i++) {
13175 if (NumberToUint32(numbers->get(i)) < min_index) {
13176 min_index = NumberToUint32(numbers->get(i));
13177 } else if (NumberToUint32(numbers->get(i)) > max_index) {
13178 max_index = NumberToUint32(numbers->get(i));
13181 if (max_index - min_index + 1 == len) {
13182 // Indices form a contiguous range, unless there are duplicates.
13183 // Do an in-place linear time sort assuming distinct numbers, but
13184 // avoid hanging in case they are not.
13185 for (i = 0; i < len; i++) {
13188 // While the current element at i is not at its correct position p,
13189 // swap the elements at these two positions.
13190 while ((p = NumberToUint32(numbers->get(i)) - min_index) != i &&
13192 SwapPairs(numbers, i, p);
13196 HeapSortPairs(this, numbers, len);
13202 // Fill in the names of own properties into the supplied storage. The main
13203 // purpose of this function is to provide reflection information for the object
13205 int JSObject::GetOwnPropertyNames(FixedArray* storage, int index,
13206 PropertyAttributes filter) {
13207 DCHECK(storage->length() >= (NumberOfOwnProperties(filter) - index));
13208 if (HasFastProperties()) {
13209 int start_index = index;
13210 int real_size = map()->NumberOfOwnDescriptors();
13211 DescriptorArray* descs = map()->instance_descriptors();
13212 for (int i = 0; i < real_size; i++) {
13213 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
13214 !FilterKey(descs->GetKey(i), filter)) {
13215 storage->set(index++, descs->GetKey(i));
13218 return index - start_index;
13219 } else if (IsGlobalObject()) {
13220 return global_dictionary()->CopyKeysTo(storage, index, filter,
13221 GlobalDictionary::UNSORTED);
13223 return property_dictionary()->CopyKeysTo(storage, index, filter,
13224 NameDictionary::UNSORTED);
13229 int JSObject::NumberOfOwnElements(PropertyAttributes filter) {
13230 return GetOwnElementKeys(NULL, filter);
13234 int JSObject::NumberOfEnumElements() {
13235 // Fast case for objects with no elements.
13236 if (!IsJSValue() && HasFastObjectElements()) {
13237 uint32_t length = IsJSArray() ?
13238 static_cast<uint32_t>(
13239 Smi::cast(JSArray::cast(this)->length())->value()) :
13240 static_cast<uint32_t>(FixedArray::cast(elements())->length());
13241 if (length == 0) return 0;
13243 // Compute the number of enumerable elements.
13244 return NumberOfOwnElements(static_cast<PropertyAttributes>(DONT_ENUM));
13248 int JSObject::GetOwnElementKeys(FixedArray* storage,
13249 PropertyAttributes filter) {
13252 // If this is a String wrapper, add the string indices first,
13253 // as they're guaranteed to preced the elements in numerical order
13254 // and ascending order is required by ECMA-262, 6th, 9.1.12.
13256 Object* val = JSValue::cast(this)->value();
13257 if (val->IsString()) {
13258 String* str = String::cast(val);
13260 for (int i = 0; i < str->length(); i++) {
13261 storage->set(counter + i, Smi::FromInt(i));
13264 counter += str->length();
13268 switch (GetElementsKind()) {
13269 case FAST_SMI_ELEMENTS:
13270 case FAST_ELEMENTS:
13271 case FAST_HOLEY_SMI_ELEMENTS:
13272 case FAST_HOLEY_ELEMENTS: {
13273 int length = IsJSArray() ?
13274 Smi::cast(JSArray::cast(this)->length())->value() :
13275 FixedArray::cast(elements())->length();
13276 for (int i = 0; i < length; i++) {
13277 if (!FixedArray::cast(elements())->get(i)->IsTheHole()) {
13278 if (storage != NULL) {
13279 storage->set(counter, Smi::FromInt(i));
13284 DCHECK(!storage || storage->length() >= counter);
13287 case FAST_DOUBLE_ELEMENTS:
13288 case FAST_HOLEY_DOUBLE_ELEMENTS: {
13289 int length = IsJSArray() ?
13290 Smi::cast(JSArray::cast(this)->length())->value() :
13291 FixedArrayBase::cast(elements())->length();
13292 for (int i = 0; i < length; i++) {
13293 if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) {
13294 if (storage != NULL) {
13295 storage->set(counter, Smi::FromInt(i));
13300 DCHECK(!storage || storage->length() >= counter);
13304 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
13305 case TYPE##_ELEMENTS: \
13307 TYPED_ARRAYS(TYPED_ARRAY_CASE)
13308 #undef TYPED_ARRAY_CASE
13310 int length = FixedArrayBase::cast(elements())->length();
13311 while (counter < length) {
13312 if (storage != NULL) {
13313 storage->set(counter, Smi::FromInt(counter));
13317 DCHECK(!storage || storage->length() >= counter);
13321 case DICTIONARY_ELEMENTS: {
13322 if (storage != NULL) {
13323 element_dictionary()->CopyKeysTo(storage, counter, filter,
13324 SeededNumberDictionary::SORTED);
13326 counter += element_dictionary()->NumberOfElementsFilterAttributes(filter);
13329 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
13330 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
13331 FixedArray* parameter_map = FixedArray::cast(elements());
13332 int mapped_length = parameter_map->length() - 2;
13333 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
13334 if (arguments->IsDictionary()) {
13335 // Copy the keys from arguments first, because Dictionary::CopyKeysTo
13336 // will insert in storage starting at index 0.
13337 SeededNumberDictionary* dictionary =
13338 SeededNumberDictionary::cast(arguments);
13339 if (storage != NULL) {
13340 dictionary->CopyKeysTo(storage, counter, filter,
13341 SeededNumberDictionary::UNSORTED);
13343 counter += dictionary->NumberOfElementsFilterAttributes(filter);
13344 for (int i = 0; i < mapped_length; ++i) {
13345 if (!parameter_map->get(i + 2)->IsTheHole()) {
13346 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13350 if (storage != NULL) storage->SortPairs(storage, counter);
13353 int backing_length = arguments->length();
13355 for (; i < mapped_length; ++i) {
13356 if (!parameter_map->get(i + 2)->IsTheHole()) {
13357 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13359 } else if (i < backing_length && !arguments->get(i)->IsTheHole()) {
13360 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13364 for (; i < backing_length; ++i) {
13365 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13373 DCHECK(!storage || storage->length() == counter);
13378 int JSObject::GetEnumElementKeys(FixedArray* storage) {
13379 return GetOwnElementKeys(storage, static_cast<PropertyAttributes>(DONT_ENUM));
13383 const char* Symbol::PrivateSymbolToName() const {
13384 Heap* heap = GetIsolate()->heap();
13385 #define SYMBOL_CHECK_AND_PRINT(name) \
13386 if (this == heap->name()) return #name;
13387 PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT)
13388 #undef SYMBOL_CHECK_AND_PRINT
13393 void Symbol::SymbolShortPrint(std::ostream& os) {
13394 os << "<Symbol: " << Hash();
13395 if (!name()->IsUndefined()) {
13397 HeapStringAllocator allocator;
13398 StringStream accumulator(&allocator);
13399 String::cast(name())->StringShortPrint(&accumulator);
13400 os << accumulator.ToCString().get();
13402 os << " (" << PrivateSymbolToName() << ")";
13408 // StringSharedKeys are used as keys in the eval cache.
13409 class StringSharedKey : public HashTableKey {
13411 StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared,
13412 LanguageMode language_mode, int scope_position)
13415 language_mode_(language_mode),
13416 scope_position_(scope_position) {}
13418 bool IsMatch(Object* other) override {
13419 DisallowHeapAllocation no_allocation;
13420 if (!other->IsFixedArray()) {
13421 if (!other->IsNumber()) return false;
13422 uint32_t other_hash = static_cast<uint32_t>(other->Number());
13423 return Hash() == other_hash;
13425 FixedArray* other_array = FixedArray::cast(other);
13426 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13427 if (shared != *shared_) return false;
13428 int language_unchecked = Smi::cast(other_array->get(2))->value();
13429 DCHECK(is_valid_language_mode(language_unchecked));
13430 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13431 if (language_mode != language_mode_) return false;
13432 int scope_position = Smi::cast(other_array->get(3))->value();
13433 if (scope_position != scope_position_) return false;
13434 String* source = String::cast(other_array->get(1));
13435 return source->Equals(*source_);
13438 static uint32_t StringSharedHashHelper(String* source,
13439 SharedFunctionInfo* shared,
13440 LanguageMode language_mode,
13441 int scope_position) {
13442 uint32_t hash = source->Hash();
13443 if (shared->HasSourceCode()) {
13444 // Instead of using the SharedFunctionInfo pointer in the hash
13445 // code computation, we use a combination of the hash of the
13446 // script source code and the start position of the calling scope.
13447 // We do this to ensure that the cache entries can survive garbage
13449 Script* script(Script::cast(shared->script()));
13450 hash ^= String::cast(script->source())->Hash();
13451 STATIC_ASSERT(LANGUAGE_END == 3);
13452 if (is_strict(language_mode)) hash ^= 0x8000;
13453 if (is_strong(language_mode)) hash ^= 0x10000;
13454 hash += scope_position;
13459 uint32_t Hash() override {
13460 return StringSharedHashHelper(*source_, *shared_, language_mode_,
13464 uint32_t HashForObject(Object* obj) override {
13465 DisallowHeapAllocation no_allocation;
13466 if (obj->IsNumber()) {
13467 return static_cast<uint32_t>(obj->Number());
13469 FixedArray* other_array = FixedArray::cast(obj);
13470 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13471 String* source = String::cast(other_array->get(1));
13472 int language_unchecked = Smi::cast(other_array->get(2))->value();
13473 DCHECK(is_valid_language_mode(language_unchecked));
13474 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13475 int scope_position = Smi::cast(other_array->get(3))->value();
13476 return StringSharedHashHelper(source, shared, language_mode,
13481 Handle<Object> AsHandle(Isolate* isolate) override {
13482 Handle<FixedArray> array = isolate->factory()->NewFixedArray(4);
13483 array->set(0, *shared_);
13484 array->set(1, *source_);
13485 array->set(2, Smi::FromInt(language_mode_));
13486 array->set(3, Smi::FromInt(scope_position_));
13491 Handle<String> source_;
13492 Handle<SharedFunctionInfo> shared_;
13493 LanguageMode language_mode_;
13494 int scope_position_;
13498 // RegExpKey carries the source and flags of a regular expression as key.
13499 class RegExpKey : public HashTableKey {
13501 RegExpKey(Handle<String> string, JSRegExp::Flags flags)
13503 flags_(Smi::FromInt(flags.value())) { }
13505 // Rather than storing the key in the hash table, a pointer to the
13506 // stored value is stored where the key should be. IsMatch then
13507 // compares the search key to the found object, rather than comparing
13509 bool IsMatch(Object* obj) override {
13510 FixedArray* val = FixedArray::cast(obj);
13511 return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex)))
13512 && (flags_ == val->get(JSRegExp::kFlagsIndex));
13515 uint32_t Hash() override { return RegExpHash(*string_, flags_); }
13517 Handle<Object> AsHandle(Isolate* isolate) override {
13518 // Plain hash maps, which is where regexp keys are used, don't
13519 // use this function.
13521 return MaybeHandle<Object>().ToHandleChecked();
13524 uint32_t HashForObject(Object* obj) override {
13525 FixedArray* val = FixedArray::cast(obj);
13526 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
13527 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
13530 static uint32_t RegExpHash(String* string, Smi* flags) {
13531 return string->Hash() + flags->value();
13534 Handle<String> string_;
13539 Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) {
13540 if (hash_field_ == 0) Hash();
13541 return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_);
13545 Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) {
13546 if (hash_field_ == 0) Hash();
13547 return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_);
13551 Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) {
13552 if (hash_field_ == 0) Hash();
13553 return isolate->factory()->NewOneByteInternalizedSubString(
13554 string_, from_, length_, hash_field_);
13558 bool SeqOneByteSubStringKey::IsMatch(Object* string) {
13559 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
13560 return String::cast(string)->IsOneByteEqualTo(chars);
13564 // InternalizedStringKey carries a string/internalized-string object as key.
13565 class InternalizedStringKey : public HashTableKey {
13567 explicit InternalizedStringKey(Handle<String> string)
13568 : string_(string) { }
13570 bool IsMatch(Object* string) override {
13571 return String::cast(string)->Equals(*string_);
13574 uint32_t Hash() override { return string_->Hash(); }
13576 uint32_t HashForObject(Object* other) override {
13577 return String::cast(other)->Hash();
13580 Handle<Object> AsHandle(Isolate* isolate) override {
13581 // Internalize the string if possible.
13582 MaybeHandle<Map> maybe_map =
13583 isolate->factory()->InternalizedStringMapForString(string_);
13585 if (maybe_map.ToHandle(&map)) {
13586 string_->set_map_no_write_barrier(*map);
13587 DCHECK(string_->IsInternalizedString());
13590 // Otherwise allocate a new internalized string.
13591 return isolate->factory()->NewInternalizedStringImpl(
13592 string_, string_->length(), string_->hash_field());
13595 static uint32_t StringHash(Object* obj) {
13596 return String::cast(obj)->Hash();
13599 Handle<String> string_;
13603 template<typename Derived, typename Shape, typename Key>
13604 void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) {
13605 IteratePointers(v, 0, kElementsStartOffset);
13609 template<typename Derived, typename Shape, typename Key>
13610 void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) {
13612 kElementsStartOffset,
13613 kHeaderSize + length() * kPointerSize);
13617 template<typename Derived, typename Shape, typename Key>
13618 Handle<Derived> HashTable<Derived, Shape, Key>::New(
13620 int at_least_space_for,
13621 MinimumCapacity capacity_option,
13622 PretenureFlag pretenure) {
13623 DCHECK(0 <= at_least_space_for);
13624 DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for));
13626 int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY)
13627 ? at_least_space_for
13628 : ComputeCapacity(at_least_space_for);
13629 if (capacity > HashTable::kMaxCapacity) {
13630 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
13633 Factory* factory = isolate->factory();
13634 int length = EntryToIndex(capacity);
13635 Handle<FixedArray> array = factory->NewFixedArray(length, pretenure);
13636 array->set_map_no_write_barrier(*factory->hash_table_map());
13637 Handle<Derived> table = Handle<Derived>::cast(array);
13639 table->SetNumberOfElements(0);
13640 table->SetNumberOfDeletedElements(0);
13641 table->SetCapacity(capacity);
13646 // Find entry for key otherwise return kNotFound.
13647 template <typename Derived, typename Shape>
13648 int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) {
13649 if (!key->IsUniqueName()) {
13650 return DerivedDictionary::FindEntry(key);
13653 // Optimized for unique names. Knowledge of the key type allows:
13654 // 1. Move the check if the key is unique out of the loop.
13655 // 2. Avoid comparing hash codes in unique-to-unique comparison.
13656 // 3. Detect a case when a dictionary key is not unique but the key is.
13657 // In case of positive result the dictionary key may be replaced by the
13658 // internalized string with minimal performance penalty. It gives a chance
13659 // to perform further lookups in code stubs (and significant performance
13660 // boost a certain style of code).
13662 // EnsureCapacity will guarantee the hash table is never full.
13663 uint32_t capacity = this->Capacity();
13664 uint32_t entry = Derived::FirstProbe(key->Hash(), capacity);
13665 uint32_t count = 1;
13668 int index = Derived::EntryToIndex(entry);
13669 Object* element = this->get(index);
13670 if (element->IsUndefined()) break; // Empty entry.
13671 if (*key == element) return entry;
13672 if (!element->IsUniqueName() &&
13673 !element->IsTheHole() &&
13674 Name::cast(element)->Equals(*key)) {
13675 // Replace a key that is a non-internalized string by the equivalent
13676 // internalized string for faster further lookups.
13677 this->set(index, *key);
13680 DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key));
13681 entry = Derived::NextProbe(entry, count++, capacity);
13683 return Derived::kNotFound;
13687 template<typename Derived, typename Shape, typename Key>
13688 void HashTable<Derived, Shape, Key>::Rehash(
13689 Handle<Derived> new_table,
13691 DCHECK(NumberOfElements() < new_table->Capacity());
13693 DisallowHeapAllocation no_gc;
13694 WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc);
13696 // Copy prefix to new array.
13697 for (int i = kPrefixStartIndex;
13698 i < kPrefixStartIndex + Shape::kPrefixSize;
13700 new_table->set(i, get(i), mode);
13703 // Rehash the elements.
13704 int capacity = this->Capacity();
13705 for (int i = 0; i < capacity; i++) {
13706 uint32_t from_index = EntryToIndex(i);
13707 Object* k = this->get(from_index);
13709 uint32_t hash = this->HashForObject(key, k);
13710 uint32_t insertion_index =
13711 EntryToIndex(new_table->FindInsertionEntry(hash));
13712 for (int j = 0; j < Shape::kEntrySize; j++) {
13713 new_table->set(insertion_index + j, get(from_index + j), mode);
13717 new_table->SetNumberOfElements(NumberOfElements());
13718 new_table->SetNumberOfDeletedElements(0);
13722 template<typename Derived, typename Shape, typename Key>
13723 uint32_t HashTable<Derived, Shape, Key>::EntryForProbe(
13727 uint32_t expected) {
13728 uint32_t hash = this->HashForObject(key, k);
13729 uint32_t capacity = this->Capacity();
13730 uint32_t entry = FirstProbe(hash, capacity);
13731 for (int i = 1; i < probe; i++) {
13732 if (entry == expected) return expected;
13733 entry = NextProbe(entry, i, capacity);
13739 template<typename Derived, typename Shape, typename Key>
13740 void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1,
13742 WriteBarrierMode mode) {
13743 int index1 = EntryToIndex(entry1);
13744 int index2 = EntryToIndex(entry2);
13745 Object* temp[Shape::kEntrySize];
13746 for (int j = 0; j < Shape::kEntrySize; j++) {
13747 temp[j] = get(index1 + j);
13749 for (int j = 0; j < Shape::kEntrySize; j++) {
13750 set(index1 + j, get(index2 + j), mode);
13752 for (int j = 0; j < Shape::kEntrySize; j++) {
13753 set(index2 + j, temp[j], mode);
13758 template<typename Derived, typename Shape, typename Key>
13759 void HashTable<Derived, Shape, Key>::Rehash(Key key) {
13760 DisallowHeapAllocation no_gc;
13761 WriteBarrierMode mode = GetWriteBarrierMode(no_gc);
13762 uint32_t capacity = Capacity();
13764 for (int probe = 1; !done; probe++) {
13765 // All elements at entries given by one of the first _probe_ probes
13766 // are placed correctly. Other elements might need to be moved.
13768 for (uint32_t current = 0; current < capacity; current++) {
13769 Object* current_key = get(EntryToIndex(current));
13770 if (IsKey(current_key)) {
13771 uint32_t target = EntryForProbe(key, current_key, probe, current);
13772 if (current == target) continue;
13773 Object* target_key = get(EntryToIndex(target));
13774 if (!IsKey(target_key) ||
13775 EntryForProbe(key, target_key, probe, target) != target) {
13776 // Put the current element into the correct position.
13777 Swap(current, target, mode);
13778 // The other element will be processed on the next iteration.
13781 // The place for the current element is occupied. Leave the element
13782 // for the next probe.
13791 template<typename Derived, typename Shape, typename Key>
13792 Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
13793 Handle<Derived> table,
13796 PretenureFlag pretenure) {
13797 Isolate* isolate = table->GetIsolate();
13798 int capacity = table->Capacity();
13799 int nof = table->NumberOfElements() + n;
13800 int nod = table->NumberOfDeletedElements();
13802 // 50% is still free after adding n elements and
13803 // at most 50% of the free elements are deleted elements.
13804 if (nod <= (capacity - nof) >> 1) {
13805 int needed_free = nof >> 1;
13806 if (nof + needed_free <= capacity) return table;
13809 const int kMinCapacityForPretenure = 256;
13810 bool should_pretenure = pretenure == TENURED ||
13811 ((capacity > kMinCapacityForPretenure) &&
13812 !isolate->heap()->InNewSpace(*table));
13813 Handle<Derived> new_table = HashTable::New(
13816 USE_DEFAULT_MINIMUM_CAPACITY,
13817 should_pretenure ? TENURED : NOT_TENURED);
13819 table->Rehash(new_table, key);
13824 template<typename Derived, typename Shape, typename Key>
13825 Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
13827 int capacity = table->Capacity();
13828 int nof = table->NumberOfElements();
13830 // Shrink to fit the number of elements if only a quarter of the
13831 // capacity is filled with elements.
13832 if (nof > (capacity >> 2)) return table;
13833 // Allocate a new dictionary with room for at least the current
13834 // number of elements. The allocation method will make sure that
13835 // there is extra room in the dictionary for additions. Don't go
13836 // lower than room for 16 elements.
13837 int at_least_room_for = nof;
13838 if (at_least_room_for < 16) return table;
13840 Isolate* isolate = table->GetIsolate();
13841 const int kMinCapacityForPretenure = 256;
13843 (at_least_room_for > kMinCapacityForPretenure) &&
13844 !isolate->heap()->InNewSpace(*table);
13845 Handle<Derived> new_table = HashTable::New(
13848 USE_DEFAULT_MINIMUM_CAPACITY,
13849 pretenure ? TENURED : NOT_TENURED);
13851 table->Rehash(new_table, key);
13856 template<typename Derived, typename Shape, typename Key>
13857 uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) {
13858 uint32_t capacity = Capacity();
13859 uint32_t entry = FirstProbe(hash, capacity);
13860 uint32_t count = 1;
13861 // EnsureCapacity will guarantee the hash table is never full.
13863 Object* element = KeyAt(entry);
13864 if (element->IsUndefined() || element->IsTheHole()) break;
13865 entry = NextProbe(entry, count++, capacity);
13871 // Force instantiation of template instances class.
13872 // Please note this list is compiler dependent.
13874 template class HashTable<StringTable, StringTableShape, HashTableKey*>;
13876 template class HashTable<CompilationCacheTable,
13877 CompilationCacheShape,
13880 template class HashTable<ObjectHashTable,
13881 ObjectHashTableShape,
13884 template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >;
13886 template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >;
13888 template class Dictionary<GlobalDictionary, GlobalDictionaryShape,
13891 template class Dictionary<SeededNumberDictionary,
13892 SeededNumberDictionaryShape,
13895 template class Dictionary<UnseededNumberDictionary,
13896 UnseededNumberDictionaryShape,
13899 template Handle<SeededNumberDictionary>
13900 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13901 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13903 template Handle<UnseededNumberDictionary>
13904 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13905 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13907 template Handle<NameDictionary>
13908 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13909 New(Isolate*, int n, PretenureFlag pretenure);
13911 template Handle<GlobalDictionary>
13912 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New(
13913 Isolate*, int n, PretenureFlag pretenure);
13915 template Handle<SeededNumberDictionary>
13916 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13917 AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>);
13919 template Handle<UnseededNumberDictionary>
13920 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13921 AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>);
13924 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13925 SlowReverseLookup(Object* value);
13928 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13929 SlowReverseLookup(Object* value);
13931 template Handle<Object>
13932 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty(
13933 Handle<NameDictionary>, int);
13935 template Handle<Object>
13936 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13937 uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int);
13939 template Handle<NameDictionary>
13940 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13941 New(Isolate*, int, MinimumCapacity, PretenureFlag);
13943 template Handle<NameDictionary>
13944 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13945 Shrink(Handle<NameDictionary>, Handle<Name>);
13947 template Handle<SeededNumberDictionary>
13948 HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13949 Shrink(Handle<SeededNumberDictionary>, uint32_t);
13951 template Handle<NameDictionary>
13952 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add(
13953 Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails);
13955 template Handle<GlobalDictionary>
13956 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add(
13957 Handle<GlobalDictionary>, Handle<Name>, Handle<Object>,
13960 template Handle<FixedArray> Dictionary<
13961 NameDictionary, NameDictionaryShape,
13962 Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>);
13964 template Handle<FixedArray> Dictionary<
13965 NameDictionary, NameDictionaryShape,
13966 Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>);
13968 template Handle<SeededNumberDictionary>
13969 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13970 Add(Handle<SeededNumberDictionary>,
13975 template Handle<UnseededNumberDictionary>
13976 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13977 Add(Handle<UnseededNumberDictionary>,
13982 template Handle<SeededNumberDictionary>
13983 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13984 EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t);
13986 template Handle<UnseededNumberDictionary>
13987 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13988 EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t);
13990 template Handle<NameDictionary>
13991 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13992 EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>);
13994 template bool Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13995 uint32_t>::HasComplexElements();
13997 template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape,
13998 uint32_t>::FindEntry(uint32_t);
14000 template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry(
14004 Handle<Object> JSObject::PrepareSlowElementsForSort(
14005 Handle<JSObject> object, uint32_t limit) {
14006 DCHECK(object->HasDictionaryElements());
14007 Isolate* isolate = object->GetIsolate();
14008 // Must stay in dictionary mode, either because of requires_slow_elements,
14009 // or because we are not going to sort (and therefore compact) all of the
14011 Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate);
14012 Handle<SeededNumberDictionary> new_dict =
14013 SeededNumberDictionary::New(isolate, dict->NumberOfElements());
14016 uint32_t undefs = 0;
14017 int capacity = dict->Capacity();
14018 Handle<Smi> bailout(Smi::FromInt(-1), isolate);
14019 // Entry to the new dictionary does not cause it to grow, as we have
14020 // allocated one that is large enough for all entries.
14021 DisallowHeapAllocation no_gc;
14022 for (int i = 0; i < capacity; i++) {
14023 Object* k = dict->KeyAt(i);
14024 if (!dict->IsKey(k)) continue;
14026 DCHECK(k->IsNumber());
14027 DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0);
14028 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0);
14029 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32);
14031 HandleScope scope(isolate);
14032 Handle<Object> value(dict->ValueAt(i), isolate);
14033 PropertyDetails details = dict->DetailsAt(i);
14034 if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) {
14035 // Bail out and do the sorting of undefineds and array holes in JS.
14036 // Also bail out if the element is not supposed to be moved.
14040 uint32_t key = NumberToUint32(k);
14042 if (value->IsUndefined()) {
14044 } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
14045 // Adding an entry with the key beyond smi-range requires
14046 // allocation. Bailout.
14049 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
14050 new_dict, pos, value, details, object->map()->is_prototype_map());
14051 DCHECK(result.is_identical_to(new_dict));
14055 } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) {
14056 // Adding an entry with the key beyond smi-range requires
14057 // allocation. Bailout.
14060 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
14061 new_dict, key, value, details, object->map()->is_prototype_map());
14062 DCHECK(result.is_identical_to(new_dict));
14067 uint32_t result = pos;
14068 PropertyDetails no_details = PropertyDetails::Empty();
14069 while (undefs > 0) {
14070 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
14071 // Adding an entry with the key beyond smi-range requires
14072 // allocation. Bailout.
14075 HandleScope scope(isolate);
14076 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
14077 new_dict, pos, isolate->factory()->undefined_value(), no_details,
14078 object->map()->is_prototype_map());
14079 DCHECK(result.is_identical_to(new_dict));
14085 object->set_elements(*new_dict);
14087 AllowHeapAllocation allocate_return_value;
14088 return isolate->factory()->NewNumberFromUint(result);
14092 // Collects all defined (non-hole) and non-undefined (array) elements at
14093 // the start of the elements array.
14094 // If the object is in dictionary mode, it is converted to fast elements
14096 Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object,
14098 Isolate* isolate = object->GetIsolate();
14099 if (object->HasSloppyArgumentsElements() ||
14100 object->map()->is_observed()) {
14101 return handle(Smi::FromInt(-1), isolate);
14104 if (object->HasDictionaryElements()) {
14105 // Convert to fast elements containing only the existing properties.
14106 // Ordering is irrelevant, since we are going to sort anyway.
14107 Handle<SeededNumberDictionary> dict(object->element_dictionary());
14108 if (object->IsJSArray() || dict->requires_slow_elements() ||
14109 dict->max_number_key() >= limit) {
14110 return JSObject::PrepareSlowElementsForSort(object, limit);
14112 // Convert to fast elements.
14114 Handle<Map> new_map =
14115 JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS);
14117 PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ?
14118 NOT_TENURED: TENURED;
14119 Handle<FixedArray> fast_elements =
14120 isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure);
14121 dict->CopyValuesTo(*fast_elements);
14122 JSObject::ValidateElements(object);
14124 JSObject::SetMapAndElements(object, new_map, fast_elements);
14125 } else if (object->HasFixedTypedArrayElements()) {
14126 // Typed arrays cannot have holes or undefined elements.
14127 return handle(Smi::FromInt(
14128 FixedArrayBase::cast(object->elements())->length()), isolate);
14129 } else if (!object->HasFastDoubleElements()) {
14130 EnsureWritableFastElements(object);
14132 DCHECK(object->HasFastSmiOrObjectElements() ||
14133 object->HasFastDoubleElements());
14135 // Collect holes at the end, undefined before that and the rest at the
14136 // start, and return the number of non-hole, non-undefined values.
14138 Handle<FixedArrayBase> elements_base(object->elements());
14139 uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
14140 if (limit > elements_length) {
14141 limit = elements_length ;
14144 return handle(Smi::FromInt(0), isolate);
14147 uint32_t result = 0;
14148 if (elements_base->map() == isolate->heap()->fixed_double_array_map()) {
14149 FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base);
14150 // Split elements into defined and the_hole, in that order.
14151 unsigned int holes = limit;
14152 // Assume most arrays contain no holes and undefined values, so minimize the
14153 // number of stores of non-undefined, non-the-hole values.
14154 for (unsigned int i = 0; i < holes; i++) {
14155 if (elements->is_the_hole(i)) {
14160 // Position i needs to be filled.
14161 while (holes > i) {
14162 if (elements->is_the_hole(holes)) {
14165 elements->set(i, elements->get_scalar(holes));
14171 while (holes < limit) {
14172 elements->set_the_hole(holes);
14176 FixedArray* elements = FixedArray::cast(*elements_base);
14177 DisallowHeapAllocation no_gc;
14179 // Split elements into defined, undefined and the_hole, in that order. Only
14180 // count locations for undefined and the hole, and fill them afterwards.
14181 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc);
14182 unsigned int undefs = limit;
14183 unsigned int holes = limit;
14184 // Assume most arrays contain no holes and undefined values, so minimize the
14185 // number of stores of non-undefined, non-the-hole values.
14186 for (unsigned int i = 0; i < undefs; i++) {
14187 Object* current = elements->get(i);
14188 if (current->IsTheHole()) {
14191 } else if (current->IsUndefined()) {
14196 // Position i needs to be filled.
14197 while (undefs > i) {
14198 current = elements->get(undefs);
14199 if (current->IsTheHole()) {
14202 } else if (current->IsUndefined()) {
14205 elements->set(i, current, write_barrier);
14211 while (undefs < holes) {
14212 elements->set_undefined(undefs);
14215 while (holes < limit) {
14216 elements->set_the_hole(holes);
14221 return isolate->factory()->NewNumberFromUint(result);
14225 ExternalArrayType JSTypedArray::type() {
14226 switch (elements()->map()->instance_type()) {
14227 #define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \
14228 case FIXED_##TYPE##_ARRAY_TYPE: \
14229 return kExternal##Type##Array;
14231 TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE)
14232 #undef INSTANCE_TYPE_TO_ARRAY_TYPE
14236 return static_cast<ExternalArrayType>(-1);
14241 size_t JSTypedArray::element_size() {
14242 switch (elements()->map()->instance_type()) {
14243 #define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \
14244 case FIXED_##TYPE##_ARRAY_TYPE: \
14247 TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE)
14248 #undef INSTANCE_TYPE_TO_ELEMENT_SIZE
14257 void FixedArray::SetValue(uint32_t index, Object* value) { set(index, value); }
14260 void FixedDoubleArray::SetValue(uint32_t index, Object* value) {
14261 set(index, value->Number());
14263 void GlobalObject::InvalidatePropertyCell(Handle<GlobalObject> global,
14264 Handle<Name> name) {
14265 DCHECK(!global->HasFastProperties());
14266 auto dictionary = handle(global->global_dictionary());
14267 int entry = dictionary->FindEntry(name);
14268 if (entry == GlobalDictionary::kNotFound) return;
14269 PropertyCell::InvalidateEntry(dictionary, entry);
14273 // TODO(ishell): rename to EnsureEmptyPropertyCell or something.
14274 Handle<PropertyCell> GlobalObject::EnsurePropertyCell(
14275 Handle<GlobalObject> global, Handle<Name> name) {
14276 DCHECK(!global->HasFastProperties());
14277 auto dictionary = handle(global->global_dictionary());
14278 int entry = dictionary->FindEntry(name);
14279 Handle<PropertyCell> cell;
14280 if (entry != GlobalDictionary::kNotFound) {
14281 // This call should be idempotent.
14282 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
14283 cell = handle(PropertyCell::cast(dictionary->ValueAt(entry)));
14284 DCHECK(cell->property_details().cell_type() ==
14285 PropertyCellType::kUninitialized ||
14286 cell->property_details().cell_type() ==
14287 PropertyCellType::kInvalidated);
14288 DCHECK(cell->value()->IsTheHole());
14291 Isolate* isolate = global->GetIsolate();
14292 cell = isolate->factory()->NewPropertyCell();
14293 PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized);
14294 dictionary = GlobalDictionary::Add(dictionary, name, cell, details);
14295 global->set_properties(*dictionary);
14300 // This class is used for looking up two character strings in the string table.
14301 // If we don't have a hit we don't want to waste much time so we unroll the
14302 // string hash calculation loop here for speed. Doesn't work if the two
14303 // characters form a decimal integer, since such strings have a different hash
14305 class TwoCharHashTableKey : public HashTableKey {
14307 TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed)
14308 : c1_(c1), c2_(c2) {
14310 uint32_t hash = seed;
14312 hash += hash << 10;
14316 hash += hash << 10;
14320 hash ^= hash >> 11;
14321 hash += hash << 15;
14322 if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash;
14325 // If this assert fails then we failed to reproduce the two-character
14326 // version of the string hashing algorithm above. One reason could be
14327 // that we were passed two digits as characters, since the hash
14328 // algorithm is different in that case.
14329 uint16_t chars[2] = {c1, c2};
14330 uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed);
14331 hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
14332 DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash));
14336 bool IsMatch(Object* o) override {
14337 if (!o->IsString()) return false;
14338 String* other = String::cast(o);
14339 if (other->length() != 2) return false;
14340 if (other->Get(0) != c1_) return false;
14341 return other->Get(1) == c2_;
14344 uint32_t Hash() override { return hash_; }
14345 uint32_t HashForObject(Object* key) override {
14346 if (!key->IsString()) return 0;
14347 return String::cast(key)->Hash();
14350 Handle<Object> AsHandle(Isolate* isolate) override {
14351 // The TwoCharHashTableKey is only used for looking in the string
14352 // table, not for adding to it.
14354 return MaybeHandle<Object>().ToHandleChecked();
14364 MaybeHandle<String> StringTable::InternalizeStringIfExists(
14366 Handle<String> string) {
14367 if (string->IsInternalizedString()) {
14370 return LookupStringIfExists(isolate, string);
14374 MaybeHandle<String> StringTable::LookupStringIfExists(
14376 Handle<String> string) {
14377 Handle<StringTable> string_table = isolate->factory()->string_table();
14378 InternalizedStringKey key(string);
14379 int entry = string_table->FindEntry(&key);
14380 if (entry == kNotFound) {
14381 return MaybeHandle<String>();
14383 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14384 DCHECK(StringShape(*result).IsInternalized());
14390 MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists(
14394 Handle<StringTable> string_table = isolate->factory()->string_table();
14395 TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed());
14396 int entry = string_table->FindEntry(&key);
14397 if (entry == kNotFound) {
14398 return MaybeHandle<String>();
14400 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14401 DCHECK(StringShape(*result).IsInternalized());
14407 void StringTable::EnsureCapacityForDeserialization(Isolate* isolate,
14409 Handle<StringTable> table = isolate->factory()->string_table();
14410 // We need a key instance for the virtual hash function.
14411 InternalizedStringKey dummy_key(Handle<String>::null());
14412 table = StringTable::EnsureCapacity(table, expected, &dummy_key);
14413 isolate->heap()->SetRootStringTable(*table);
14417 Handle<String> StringTable::LookupString(Isolate* isolate,
14418 Handle<String> string) {
14419 InternalizedStringKey key(string);
14420 return LookupKey(isolate, &key);
14424 Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) {
14425 Handle<StringTable> table = isolate->factory()->string_table();
14426 int entry = table->FindEntry(key);
14428 // String already in table.
14429 if (entry != kNotFound) {
14430 return handle(String::cast(table->KeyAt(entry)), isolate);
14433 // Adding new string. Grow table if needed.
14434 table = StringTable::EnsureCapacity(table, 1, key);
14436 // Create string object.
14437 Handle<Object> string = key->AsHandle(isolate);
14438 // There must be no attempts to internalize strings that could throw
14439 // InvalidStringLength error.
14440 CHECK(!string.is_null());
14442 // Add the new string and return it along with the string table.
14443 entry = table->FindInsertionEntry(key->Hash());
14444 table->set(EntryToIndex(entry), *string);
14445 table->ElementAdded();
14447 isolate->heap()->SetRootStringTable(*table);
14448 return Handle<String>::cast(string);
14452 String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) {
14453 Handle<StringTable> table = isolate->factory()->string_table();
14454 int entry = table->FindEntry(key);
14455 if (entry != kNotFound) return String::cast(table->KeyAt(entry));
14460 Handle<Object> CompilationCacheTable::Lookup(Handle<String> src,
14461 Handle<Context> context,
14462 LanguageMode language_mode) {
14463 Isolate* isolate = GetIsolate();
14464 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14465 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14466 int entry = FindEntry(&key);
14467 if (entry == kNotFound) return isolate->factory()->undefined_value();
14468 int index = EntryToIndex(entry);
14469 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14470 return Handle<Object>(get(index + 1), isolate);
14474 Handle<Object> CompilationCacheTable::LookupEval(
14475 Handle<String> src, Handle<SharedFunctionInfo> outer_info,
14476 LanguageMode language_mode, int scope_position) {
14477 Isolate* isolate = GetIsolate();
14478 // Cache key is the tuple (source, outer shared function info, scope position)
14479 // to unambiguously identify the context chain the cached eval code assumes.
14480 StringSharedKey key(src, outer_info, language_mode, scope_position);
14481 int entry = FindEntry(&key);
14482 if (entry == kNotFound) return isolate->factory()->undefined_value();
14483 int index = EntryToIndex(entry);
14484 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14485 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14489 Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src,
14490 JSRegExp::Flags flags) {
14491 Isolate* isolate = GetIsolate();
14492 DisallowHeapAllocation no_allocation;
14493 RegExpKey key(src, flags);
14494 int entry = FindEntry(&key);
14495 if (entry == kNotFound) return isolate->factory()->undefined_value();
14496 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14500 Handle<CompilationCacheTable> CompilationCacheTable::Put(
14501 Handle<CompilationCacheTable> cache, Handle<String> src,
14502 Handle<Context> context, LanguageMode language_mode, Handle<Object> value) {
14503 Isolate* isolate = cache->GetIsolate();
14504 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14505 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14507 Handle<Object> k = key.AsHandle(isolate);
14508 DisallowHeapAllocation no_allocation_scope;
14509 int entry = cache->FindEntry(&key);
14510 if (entry != kNotFound) {
14511 cache->set(EntryToIndex(entry), *k);
14512 cache->set(EntryToIndex(entry) + 1, *value);
14517 cache = EnsureCapacity(cache, 1, &key);
14518 int entry = cache->FindInsertionEntry(key.Hash());
14520 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14521 cache->set(EntryToIndex(entry), *k);
14522 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14523 cache->ElementAdded();
14528 Handle<CompilationCacheTable> CompilationCacheTable::PutEval(
14529 Handle<CompilationCacheTable> cache, Handle<String> src,
14530 Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
14531 int scope_position) {
14532 Isolate* isolate = cache->GetIsolate();
14533 StringSharedKey key(src, outer_info, value->language_mode(), scope_position);
14535 Handle<Object> k = key.AsHandle(isolate);
14536 DisallowHeapAllocation no_allocation_scope;
14537 int entry = cache->FindEntry(&key);
14538 if (entry != kNotFound) {
14539 cache->set(EntryToIndex(entry), *k);
14540 cache->set(EntryToIndex(entry) + 1, *value);
14545 cache = EnsureCapacity(cache, 1, &key);
14546 int entry = cache->FindInsertionEntry(key.Hash());
14548 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14549 cache->set(EntryToIndex(entry), *k);
14550 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14551 cache->ElementAdded();
14556 Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp(
14557 Handle<CompilationCacheTable> cache, Handle<String> src,
14558 JSRegExp::Flags flags, Handle<FixedArray> value) {
14559 RegExpKey key(src, flags);
14560 cache = EnsureCapacity(cache, 1, &key);
14561 int entry = cache->FindInsertionEntry(key.Hash());
14562 // We store the value in the key slot, and compare the search key
14563 // to the stored value with a custon IsMatch function during lookups.
14564 cache->set(EntryToIndex(entry), *value);
14565 cache->set(EntryToIndex(entry) + 1, *value);
14566 cache->ElementAdded();
14571 void CompilationCacheTable::Age() {
14572 DisallowHeapAllocation no_allocation;
14573 Object* the_hole_value = GetHeap()->the_hole_value();
14574 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14575 int entry_index = EntryToIndex(entry);
14576 int value_index = entry_index + 1;
14578 if (get(entry_index)->IsNumber()) {
14579 Smi* count = Smi::cast(get(value_index));
14580 count = Smi::FromInt(count->value() - 1);
14581 if (count->value() == 0) {
14582 NoWriteBarrierSet(this, entry_index, the_hole_value);
14583 NoWriteBarrierSet(this, value_index, the_hole_value);
14586 NoWriteBarrierSet(this, value_index, count);
14588 } else if (get(entry_index)->IsFixedArray()) {
14589 SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index));
14590 if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) {
14591 NoWriteBarrierSet(this, entry_index, the_hole_value);
14592 NoWriteBarrierSet(this, value_index, the_hole_value);
14600 void CompilationCacheTable::Remove(Object* value) {
14601 DisallowHeapAllocation no_allocation;
14602 Object* the_hole_value = GetHeap()->the_hole_value();
14603 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14604 int entry_index = EntryToIndex(entry);
14605 int value_index = entry_index + 1;
14606 if (get(value_index) == value) {
14607 NoWriteBarrierSet(this, entry_index, the_hole_value);
14608 NoWriteBarrierSet(this, value_index, the_hole_value);
14616 // StringsKey used for HashTable where key is array of internalized strings.
14617 class StringsKey : public HashTableKey {
14619 explicit StringsKey(Handle<FixedArray> strings) : strings_(strings) { }
14621 bool IsMatch(Object* strings) override {
14622 FixedArray* o = FixedArray::cast(strings);
14623 int len = strings_->length();
14624 if (o->length() != len) return false;
14625 for (int i = 0; i < len; i++) {
14626 if (o->get(i) != strings_->get(i)) return false;
14631 uint32_t Hash() override { return HashForObject(*strings_); }
14633 uint32_t HashForObject(Object* obj) override {
14634 FixedArray* strings = FixedArray::cast(obj);
14635 int len = strings->length();
14637 for (int i = 0; i < len; i++) {
14638 hash ^= String::cast(strings->get(i))->Hash();
14643 Handle<Object> AsHandle(Isolate* isolate) override { return strings_; }
14646 Handle<FixedArray> strings_;
14650 template<typename Derived, typename Shape, typename Key>
14651 Handle<Derived> Dictionary<Derived, Shape, Key>::New(
14653 int at_least_space_for,
14654 PretenureFlag pretenure) {
14655 DCHECK(0 <= at_least_space_for);
14656 Handle<Derived> dict = DerivedHashTable::New(isolate,
14657 at_least_space_for,
14658 USE_DEFAULT_MINIMUM_CAPACITY,
14661 // Initialize the next enumeration index.
14662 dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
14667 template <typename Derived, typename Shape, typename Key>
14668 Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray(
14669 Handle<Derived> dictionary) {
14670 Factory* factory = dictionary->GetIsolate()->factory();
14671 int length = dictionary->NumberOfElements();
14673 Handle<FixedArray> iteration_order = factory->NewFixedArray(length);
14674 Handle<FixedArray> enumeration_order = factory->NewFixedArray(length);
14676 // Fill both the iteration order array and the enumeration order array
14677 // with property details.
14678 int capacity = dictionary->Capacity();
14680 for (int i = 0; i < capacity; i++) {
14681 if (dictionary->IsKey(dictionary->KeyAt(i))) {
14682 int index = dictionary->DetailsAt(i).dictionary_index();
14683 iteration_order->set(pos, Smi::FromInt(i));
14684 enumeration_order->set(pos, Smi::FromInt(index));
14688 DCHECK(pos == length);
14690 // Sort the arrays wrt. enumeration order.
14691 iteration_order->SortPairs(*enumeration_order, enumeration_order->length());
14692 return iteration_order;
14696 template <typename Derived, typename Shape, typename Key>
14698 Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
14699 Handle<Derived> dictionary) {
14700 int length = dictionary->NumberOfElements();
14702 Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary);
14703 DCHECK(iteration_order->length() == length);
14705 // Iterate over the dictionary using the enumeration order and update
14706 // the dictionary with new enumeration indices.
14707 for (int i = 0; i < length; i++) {
14708 int index = Smi::cast(iteration_order->get(i))->value();
14709 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
14711 int enum_index = PropertyDetails::kInitialIndex + i;
14713 PropertyDetails details = dictionary->DetailsAt(index);
14714 PropertyDetails new_details = details.set_index(enum_index);
14715 dictionary->DetailsAtPut(index, new_details);
14718 // Set the next enumeration index.
14719 dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length);
14720 return iteration_order;
14724 template<typename Derived, typename Shape, typename Key>
14725 Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
14726 Handle<Derived> dictionary, int n, Key key) {
14727 // Check whether there are enough enumeration indices to add n elements.
14728 if (Shape::kIsEnumerable &&
14729 !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) {
14730 // If not, we generate new indices for the properties.
14731 GenerateNewEnumerationIndices(dictionary);
14733 return DerivedHashTable::EnsureCapacity(dictionary, n, key);
14737 template <typename Derived, typename Shape, typename Key>
14738 Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty(
14739 Handle<Derived> dictionary, int entry) {
14740 Factory* factory = dictionary->GetIsolate()->factory();
14741 PropertyDetails details = dictionary->DetailsAt(entry);
14742 if (!details.IsConfigurable()) return factory->false_value();
14744 dictionary->SetEntry(
14745 entry, factory->the_hole_value(), factory->the_hole_value());
14746 dictionary->ElementRemoved();
14747 return factory->true_value();
14751 template<typename Derived, typename Shape, typename Key>
14752 Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut(
14753 Handle<Derived> dictionary, Key key, Handle<Object> value) {
14754 int entry = dictionary->FindEntry(key);
14756 // If the entry is present set the value;
14757 if (entry != Dictionary::kNotFound) {
14758 dictionary->ValueAtPut(entry, *value);
14762 // Check whether the dictionary should be extended.
14763 dictionary = EnsureCapacity(dictionary, 1, key);
14765 USE(Shape::AsHandle(dictionary->GetIsolate(), key));
14767 PropertyDetails details = PropertyDetails::Empty();
14769 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14774 template<typename Derived, typename Shape, typename Key>
14775 Handle<Derived> Dictionary<Derived, Shape, Key>::Add(
14776 Handle<Derived> dictionary,
14778 Handle<Object> value,
14779 PropertyDetails details) {
14780 // Valdate key is absent.
14781 SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound));
14782 // Check whether the dictionary should be extended.
14783 dictionary = EnsureCapacity(dictionary, 1, key);
14785 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14790 // Add a key, value pair to the dictionary.
14791 template<typename Derived, typename Shape, typename Key>
14792 void Dictionary<Derived, Shape, Key>::AddEntry(
14793 Handle<Derived> dictionary,
14795 Handle<Object> value,
14796 PropertyDetails details,
14798 // Compute the key object.
14799 Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key);
14801 uint32_t entry = dictionary->FindInsertionEntry(hash);
14802 // Insert element at empty or deleted entry
14803 if (details.dictionary_index() == 0 && Shape::kIsEnumerable) {
14804 // Assign an enumeration index to the property and update
14805 // SetNextEnumerationIndex.
14806 int index = dictionary->NextEnumerationIndex();
14807 details = details.set_index(index);
14808 dictionary->SetNextEnumerationIndex(index + 1);
14810 dictionary->SetEntry(entry, k, value, details);
14811 DCHECK((dictionary->KeyAt(entry)->IsNumber() ||
14812 dictionary->KeyAt(entry)->IsName()));
14813 dictionary->ElementAdded();
14817 void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key,
14818 bool used_as_prototype) {
14819 DisallowHeapAllocation no_allocation;
14820 // If the dictionary requires slow elements an element has already
14821 // been added at a high index.
14822 if (requires_slow_elements()) return;
14823 // Check if this index is high enough that we should require slow
14825 if (key > kRequiresSlowElementsLimit) {
14826 if (used_as_prototype) {
14827 // TODO(verwaest): Remove this hack.
14828 GetHeap()->ClearAllKeyedStoreICs();
14830 set_requires_slow_elements();
14833 // Update max key value.
14834 Object* max_index_object = get(kMaxNumberKeyIndex);
14835 if (!max_index_object->IsSmi() || max_number_key() < key) {
14836 FixedArray::set(kMaxNumberKeyIndex,
14837 Smi::FromInt(key << kRequiresSlowElementsTagSize));
14842 Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry(
14843 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14844 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14845 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14846 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14847 return Add(dictionary, key, value, details);
14851 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry(
14852 Handle<UnseededNumberDictionary> dictionary,
14854 Handle<Object> value) {
14855 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14856 return Add(dictionary, key, value, PropertyDetails::Empty());
14860 Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut(
14861 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14862 Handle<Object> value, bool used_as_prototype) {
14863 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14864 return AtPut(dictionary, key, value);
14868 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut(
14869 Handle<UnseededNumberDictionary> dictionary,
14871 Handle<Object> value) {
14872 return AtPut(dictionary, key, value);
14876 Handle<SeededNumberDictionary> SeededNumberDictionary::Set(
14877 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14878 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14879 int entry = dictionary->FindEntry(key);
14880 if (entry == kNotFound) {
14881 return AddNumberEntry(dictionary, key, value, details, used_as_prototype);
14883 // Preserve enumeration index.
14884 details = details.set_index(dictionary->DetailsAt(entry).dictionary_index());
14885 Handle<Object> object_key =
14886 SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14887 dictionary->SetEntry(entry, object_key, value, details);
14892 Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set(
14893 Handle<UnseededNumberDictionary> dictionary,
14895 Handle<Object> value) {
14896 int entry = dictionary->FindEntry(key);
14897 if (entry == kNotFound) return AddNumberEntry(dictionary, key, value);
14898 Handle<Object> object_key =
14899 UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14900 dictionary->SetEntry(entry, object_key, value);
14905 template <typename Derived, typename Shape, typename Key>
14906 int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes(
14907 PropertyAttributes filter) {
14908 int capacity = this->Capacity();
14910 for (int i = 0; i < capacity; i++) {
14911 Object* k = this->KeyAt(i);
14912 if (this->IsKey(k) && !FilterKey(k, filter)) {
14913 if (this->IsDeleted(i)) continue;
14914 PropertyDetails details = this->DetailsAt(i);
14915 PropertyAttributes attr = details.attributes();
14916 if ((attr & filter) == 0) result++;
14923 template <typename Derived, typename Shape, typename Key>
14924 bool Dictionary<Derived, Shape, Key>::HasComplexElements() {
14925 int capacity = this->Capacity();
14926 for (int i = 0; i < capacity; i++) {
14927 Object* k = this->KeyAt(i);
14928 if (this->IsKey(k) && !FilterKey(k, NONE)) {
14929 if (this->IsDeleted(i)) continue;
14930 PropertyDetails details = this->DetailsAt(i);
14931 if (details.type() == ACCESSOR_CONSTANT) return true;
14932 PropertyAttributes attr = details.attributes();
14933 if (attr & (READ_ONLY | DONT_DELETE | DONT_ENUM)) return true;
14940 template <typename Dictionary>
14941 struct EnumIndexComparator {
14942 explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {}
14943 bool operator() (Smi* a, Smi* b) {
14944 PropertyDetails da(dict->DetailsAt(a->value()));
14945 PropertyDetails db(dict->DetailsAt(b->value()));
14946 return da.dictionary_index() < db.dictionary_index();
14952 template <typename Derived, typename Shape, typename Key>
14953 void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) {
14954 int length = storage->length();
14955 int capacity = this->Capacity();
14956 int properties = 0;
14957 for (int i = 0; i < capacity; i++) {
14958 Object* k = this->KeyAt(i);
14959 if (this->IsKey(k) && !k->IsSymbol()) {
14960 PropertyDetails details = this->DetailsAt(i);
14961 if (details.IsDontEnum() || this->IsDeleted(i)) continue;
14962 storage->set(properties, Smi::FromInt(i));
14964 if (properties == length) break;
14967 CHECK_EQ(length, properties);
14968 EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this));
14969 Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress());
14970 std::sort(start, start + length, cmp);
14971 for (int i = 0; i < length; i++) {
14972 int index = Smi::cast(storage->get(i))->value();
14973 storage->set(i, this->KeyAt(index));
14978 template <typename Derived, typename Shape, typename Key>
14979 int Dictionary<Derived, Shape, Key>::CopyKeysTo(
14980 FixedArray* storage, int index, PropertyAttributes filter,
14981 typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) {
14982 DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter));
14983 int start_index = index;
14984 int capacity = this->Capacity();
14985 for (int i = 0; i < capacity; i++) {
14986 Object* k = this->KeyAt(i);
14987 if (this->IsKey(k) && !FilterKey(k, filter)) {
14988 if (this->IsDeleted(i)) continue;
14989 PropertyDetails details = this->DetailsAt(i);
14990 PropertyAttributes attr = details.attributes();
14991 if ((attr & filter) == 0) storage->set(index++, k);
14994 if (sort_mode == Dictionary::SORTED) {
14995 storage->SortPairs(storage, index);
14997 DCHECK(storage->length() >= index);
14998 return index - start_index;
15002 // Backwards lookup (slow).
15003 template<typename Derived, typename Shape, typename Key>
15004 Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) {
15005 int capacity = this->Capacity();
15006 for (int i = 0; i < capacity; i++) {
15007 Object* k = this->KeyAt(i);
15008 if (this->IsKey(k)) {
15009 Object* e = this->ValueAt(i);
15010 // TODO(dcarney): this should be templatized.
15011 if (e->IsPropertyCell()) {
15012 e = PropertyCell::cast(e)->value();
15014 if (e == value) return k;
15017 Heap* heap = Dictionary::GetHeap();
15018 return heap->undefined_value();
15022 Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key,
15024 DisallowHeapAllocation no_gc;
15025 DCHECK(IsKey(*key));
15027 int entry = FindEntry(isolate, key, hash);
15028 if (entry == kNotFound) return isolate->heap()->the_hole_value();
15029 return get(EntryToIndex(entry) + 1);
15033 Object* ObjectHashTable::Lookup(Handle<Object> key) {
15034 DisallowHeapAllocation no_gc;
15035 DCHECK(IsKey(*key));
15037 Isolate* isolate = GetIsolate();
15039 // If the object does not have an identity hash, it was never used as a key.
15040 Object* hash = key->GetHash();
15041 if (hash->IsUndefined()) {
15042 return isolate->heap()->the_hole_value();
15044 return Lookup(isolate, key, Smi::cast(hash)->value());
15048 Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) {
15049 return Lookup(GetIsolate(), key, hash);
15053 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
15054 Handle<Object> key,
15055 Handle<Object> value) {
15056 DCHECK(table->IsKey(*key));
15057 DCHECK(!value->IsTheHole());
15059 Isolate* isolate = table->GetIsolate();
15060 // Make sure the key object has an identity hash code.
15061 int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
15063 return Put(table, key, value, hash);
15067 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
15068 Handle<Object> key,
15069 Handle<Object> value,
15071 DCHECK(table->IsKey(*key));
15072 DCHECK(!value->IsTheHole());
15074 Isolate* isolate = table->GetIsolate();
15076 int entry = table->FindEntry(isolate, key, hash);
15078 // Key is already in table, just overwrite value.
15079 if (entry != kNotFound) {
15080 table->set(EntryToIndex(entry) + 1, *value);
15084 // Check whether the hash table should be extended.
15085 table = EnsureCapacity(table, 1, key);
15086 table->AddEntry(table->FindInsertionEntry(hash), *key, *value);
15091 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
15092 Handle<Object> key,
15093 bool* was_present) {
15094 DCHECK(table->IsKey(*key));
15096 Object* hash = key->GetHash();
15097 if (hash->IsUndefined()) {
15098 *was_present = false;
15102 return Remove(table, key, was_present, Smi::cast(hash)->value());
15106 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
15107 Handle<Object> key,
15110 DCHECK(table->IsKey(*key));
15112 int entry = table->FindEntry(table->GetIsolate(), key, hash);
15113 if (entry == kNotFound) {
15114 *was_present = false;
15118 *was_present = true;
15119 table->RemoveEntry(entry);
15120 return Shrink(table, key);
15124 void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) {
15125 set(EntryToIndex(entry), key);
15126 set(EntryToIndex(entry) + 1, value);
15131 void ObjectHashTable::RemoveEntry(int entry) {
15132 set_the_hole(EntryToIndex(entry));
15133 set_the_hole(EntryToIndex(entry) + 1);
15138 Object* WeakHashTable::Lookup(Handle<HeapObject> key) {
15139 DisallowHeapAllocation no_gc;
15140 DCHECK(IsKey(*key));
15141 int entry = FindEntry(key);
15142 if (entry == kNotFound) return GetHeap()->the_hole_value();
15143 return get(EntryToValueIndex(entry));
15147 Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table,
15148 Handle<HeapObject> key,
15149 Handle<HeapObject> value) {
15150 DCHECK(table->IsKey(*key));
15151 int entry = table->FindEntry(key);
15152 // Key is already in table, just overwrite value.
15153 if (entry != kNotFound) {
15154 table->set(EntryToValueIndex(entry), *value);
15158 Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key);
15160 // Check whether the hash table should be extended.
15161 table = EnsureCapacity(table, 1, key, TENURED);
15163 table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value);
15168 void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell,
15169 Handle<HeapObject> value) {
15170 DisallowHeapAllocation no_allocation;
15171 set(EntryToIndex(entry), *key_cell);
15172 set(EntryToValueIndex(entry), *value);
15177 template<class Derived, class Iterator, int entrysize>
15178 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate(
15179 Isolate* isolate, int capacity, PretenureFlag pretenure) {
15180 // Capacity must be a power of two, since we depend on being able
15181 // to divide and multiple by 2 (kLoadFactor) to derive capacity
15182 // from number of buckets. If we decide to change kLoadFactor
15183 // to something other than 2, capacity should be stored as another
15184 // field of this object.
15185 capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity));
15186 if (capacity > kMaxCapacity) {
15187 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
15189 int num_buckets = capacity / kLoadFactor;
15190 Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray(
15191 kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure);
15192 backing_store->set_map_no_write_barrier(
15193 isolate->heap()->ordered_hash_table_map());
15194 Handle<Derived> table = Handle<Derived>::cast(backing_store);
15195 for (int i = 0; i < num_buckets; ++i) {
15196 table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound));
15198 table->SetNumberOfBuckets(num_buckets);
15199 table->SetNumberOfElements(0);
15200 table->SetNumberOfDeletedElements(0);
15205 template<class Derived, class Iterator, int entrysize>
15206 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable(
15207 Handle<Derived> table) {
15208 DCHECK(!table->IsObsolete());
15210 int nof = table->NumberOfElements();
15211 int nod = table->NumberOfDeletedElements();
15212 int capacity = table->Capacity();
15213 if ((nof + nod) < capacity) return table;
15214 // Don't need to grow if we can simply clear out deleted entries instead.
15215 // Note that we can't compact in place, though, so we always allocate
15217 return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity);
15221 template<class Derived, class Iterator, int entrysize>
15222 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink(
15223 Handle<Derived> table) {
15224 DCHECK(!table->IsObsolete());
15226 int nof = table->NumberOfElements();
15227 int capacity = table->Capacity();
15228 if (nof >= (capacity >> 2)) return table;
15229 return Rehash(table, capacity / 2);
15233 template<class Derived, class Iterator, int entrysize>
15234 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear(
15235 Handle<Derived> table) {
15236 DCHECK(!table->IsObsolete());
15238 Handle<Derived> new_table =
15239 Allocate(table->GetIsolate(),
15241 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
15243 table->SetNextTable(*new_table);
15244 table->SetNumberOfDeletedElements(kClearedTableSentinel);
15250 template<class Derived, class Iterator, int entrysize>
15251 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash(
15252 Handle<Derived> table, int new_capacity) {
15253 DCHECK(!table->IsObsolete());
15255 Handle<Derived> new_table =
15256 Allocate(table->GetIsolate(),
15258 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
15259 int nof = table->NumberOfElements();
15260 int nod = table->NumberOfDeletedElements();
15261 int new_buckets = new_table->NumberOfBuckets();
15263 int removed_holes_index = 0;
15265 for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) {
15266 Object* key = table->KeyAt(old_entry);
15267 if (key->IsTheHole()) {
15268 table->SetRemovedIndexAt(removed_holes_index++, old_entry);
15272 Object* hash = key->GetHash();
15273 int bucket = Smi::cast(hash)->value() & (new_buckets - 1);
15274 Object* chain_entry = new_table->get(kHashTableStartIndex + bucket);
15275 new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry));
15276 int new_index = new_table->EntryToIndex(new_entry);
15277 int old_index = table->EntryToIndex(old_entry);
15278 for (int i = 0; i < entrysize; ++i) {
15279 Object* value = table->get(old_index + i);
15280 new_table->set(new_index + i, value);
15282 new_table->set(new_index + kChainOffset, chain_entry);
15286 DCHECK_EQ(nod, removed_holes_index);
15288 new_table->SetNumberOfElements(nof);
15289 table->SetNextTable(*new_table);
15295 template Handle<OrderedHashSet>
15296 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate(
15297 Isolate* isolate, int capacity, PretenureFlag pretenure);
15299 template Handle<OrderedHashSet>
15300 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable(
15301 Handle<OrderedHashSet> table);
15303 template Handle<OrderedHashSet>
15304 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink(
15305 Handle<OrderedHashSet> table);
15307 template Handle<OrderedHashSet>
15308 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear(
15309 Handle<OrderedHashSet> table);
15312 template Handle<OrderedHashMap>
15313 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate(
15314 Isolate* isolate, int capacity, PretenureFlag pretenure);
15316 template Handle<OrderedHashMap>
15317 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable(
15318 Handle<OrderedHashMap> table);
15320 template Handle<OrderedHashMap>
15321 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink(
15322 Handle<OrderedHashMap> table);
15324 template Handle<OrderedHashMap>
15325 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear(
15326 Handle<OrderedHashMap> table);
15329 template<class Derived, class TableType>
15330 void OrderedHashTableIterator<Derived, TableType>::Transition() {
15331 DisallowHeapAllocation no_allocation;
15332 TableType* table = TableType::cast(this->table());
15333 if (!table->IsObsolete()) return;
15335 int index = Smi::cast(this->index())->value();
15336 while (table->IsObsolete()) {
15337 TableType* next_table = table->NextTable();
15340 int nod = table->NumberOfDeletedElements();
15342 if (nod == TableType::kClearedTableSentinel) {
15345 int old_index = index;
15346 for (int i = 0; i < nod; ++i) {
15347 int removed_index = table->RemovedIndexAt(i);
15348 if (removed_index >= old_index) break;
15354 table = next_table;
15358 set_index(Smi::FromInt(index));
15362 template<class Derived, class TableType>
15363 bool OrderedHashTableIterator<Derived, TableType>::HasMore() {
15364 DisallowHeapAllocation no_allocation;
15365 if (this->table()->IsUndefined()) return false;
15369 TableType* table = TableType::cast(this->table());
15370 int index = Smi::cast(this->index())->value();
15371 int used_capacity = table->UsedCapacity();
15373 while (index < used_capacity && table->KeyAt(index)->IsTheHole()) {
15377 set_index(Smi::FromInt(index));
15379 if (index < used_capacity) return true;
15381 set_table(GetHeap()->undefined_value());
15386 template<class Derived, class TableType>
15387 Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) {
15388 DisallowHeapAllocation no_allocation;
15390 FixedArray* array = FixedArray::cast(value_array->elements());
15391 static_cast<Derived*>(this)->PopulateValueArray(array);
15393 return Smi::cast(kind());
15395 return Smi::FromInt(0);
15400 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next(
15401 JSArray* value_array);
15404 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore();
15407 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext();
15410 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey();
15413 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition();
15417 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next(
15418 JSArray* value_array);
15421 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore();
15424 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext();
15427 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey();
15430 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition();
15433 void JSSet::Initialize(Handle<JSSet> set, Isolate* isolate) {
15434 Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet();
15435 set->set_table(*table);
15439 void JSSet::Clear(Handle<JSSet> set) {
15440 Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table()));
15441 table = OrderedHashSet::Clear(table);
15442 set->set_table(*table);
15446 void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) {
15447 Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
15448 map->set_table(*table);
15452 void JSMap::Clear(Handle<JSMap> map) {
15453 Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table()));
15454 table = OrderedHashMap::Clear(table);
15455 map->set_table(*table);
15459 void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection,
15460 Isolate* isolate) {
15461 DCHECK_EQ(0, weak_collection->map()->GetInObjectProperties());
15462 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0);
15463 weak_collection->set_table(*table);
15467 void JSWeakCollection::Set(Handle<JSWeakCollection> weak_collection,
15468 Handle<Object> key, Handle<Object> value,
15470 DCHECK(key->IsJSReceiver() || key->IsSymbol());
15471 Handle<ObjectHashTable> table(
15472 ObjectHashTable::cast(weak_collection->table()));
15473 DCHECK(table->IsKey(*key));
15474 Handle<ObjectHashTable> new_table =
15475 ObjectHashTable::Put(table, key, value, hash);
15476 weak_collection->set_table(*new_table);
15477 if (*table != *new_table) {
15478 // Zap the old table since we didn't record slots for its elements.
15479 table->FillWithHoles(0, table->length());
15484 bool JSWeakCollection::Delete(Handle<JSWeakCollection> weak_collection,
15485 Handle<Object> key, int32_t hash) {
15486 DCHECK(key->IsJSReceiver() || key->IsSymbol());
15487 Handle<ObjectHashTable> table(
15488 ObjectHashTable::cast(weak_collection->table()));
15489 DCHECK(table->IsKey(*key));
15490 bool was_present = false;
15491 Handle<ObjectHashTable> new_table =
15492 ObjectHashTable::Remove(table, key, &was_present, hash);
15493 weak_collection->set_table(*new_table);
15494 if (*table != *new_table) {
15495 // Zap the old table since we didn't record slots for its elements.
15496 table->FillWithHoles(0, table->length());
15498 return was_present;
15502 // Check if there is a break point at this code position.
15503 bool DebugInfo::HasBreakPoint(int code_position) {
15504 // Get the break point info object for this code position.
15505 Object* break_point_info = GetBreakPointInfo(code_position);
15507 // If there is no break point info object or no break points in the break
15508 // point info object there is no break point at this code position.
15509 if (break_point_info->IsUndefined()) return false;
15510 return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0;
15514 // Get the break point info object for this code position.
15515 Object* DebugInfo::GetBreakPointInfo(int code_position) {
15516 // Find the index of the break point info object for this code position.
15517 int index = GetBreakPointInfoIndex(code_position);
15519 // Return the break point info object if any.
15520 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
15521 return BreakPointInfo::cast(break_points()->get(index));
15525 // Clear a break point at the specified code position.
15526 void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info,
15528 Handle<Object> break_point_object) {
15529 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15530 debug_info->GetIsolate());
15531 if (break_point_info->IsUndefined()) return;
15532 BreakPointInfo::ClearBreakPoint(
15533 Handle<BreakPointInfo>::cast(break_point_info),
15534 break_point_object);
15538 void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info,
15540 int source_position,
15541 int statement_position,
15542 Handle<Object> break_point_object) {
15543 Isolate* isolate = debug_info->GetIsolate();
15544 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15546 if (!break_point_info->IsUndefined()) {
15547 BreakPointInfo::SetBreakPoint(
15548 Handle<BreakPointInfo>::cast(break_point_info),
15549 break_point_object);
15553 // Adding a new break point for a code position which did not have any
15554 // break points before. Try to find a free slot.
15555 int index = kNoBreakPointInfo;
15556 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15557 if (debug_info->break_points()->get(i)->IsUndefined()) {
15562 if (index == kNoBreakPointInfo) {
15563 // No free slot - extend break point info array.
15564 Handle<FixedArray> old_break_points =
15565 Handle<FixedArray>(FixedArray::cast(debug_info->break_points()));
15566 Handle<FixedArray> new_break_points =
15567 isolate->factory()->NewFixedArray(
15568 old_break_points->length() +
15569 DebugInfo::kEstimatedNofBreakPointsInFunction);
15571 debug_info->set_break_points(*new_break_points);
15572 for (int i = 0; i < old_break_points->length(); i++) {
15573 new_break_points->set(i, old_break_points->get(i));
15575 index = old_break_points->length();
15577 DCHECK(index != kNoBreakPointInfo);
15579 // Allocate new BreakPointInfo object and set the break point.
15580 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
15581 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
15582 new_break_point_info->set_code_position(Smi::FromInt(code_position));
15583 new_break_point_info->set_source_position(Smi::FromInt(source_position));
15584 new_break_point_info->
15585 set_statement_position(Smi::FromInt(statement_position));
15586 new_break_point_info->set_break_point_objects(
15587 isolate->heap()->undefined_value());
15588 BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object);
15589 debug_info->break_points()->set(index, *new_break_point_info);
15593 // Get the break point objects for a code position.
15594 Handle<Object> DebugInfo::GetBreakPointObjects(int code_position) {
15595 Object* break_point_info = GetBreakPointInfo(code_position);
15596 if (break_point_info->IsUndefined()) {
15597 return GetIsolate()->factory()->undefined_value();
15599 return Handle<Object>(
15600 BreakPointInfo::cast(break_point_info)->break_point_objects(),
15605 // Get the total number of break points.
15606 int DebugInfo::GetBreakPointCount() {
15607 if (break_points()->IsUndefined()) return 0;
15609 for (int i = 0; i < break_points()->length(); i++) {
15610 if (!break_points()->get(i)->IsUndefined()) {
15611 BreakPointInfo* break_point_info =
15612 BreakPointInfo::cast(break_points()->get(i));
15613 count += break_point_info->GetBreakPointCount();
15620 Handle<Object> DebugInfo::FindBreakPointInfo(
15621 Handle<DebugInfo> debug_info, Handle<Object> break_point_object) {
15622 Isolate* isolate = debug_info->GetIsolate();
15623 if (!debug_info->break_points()->IsUndefined()) {
15624 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15625 if (!debug_info->break_points()->get(i)->IsUndefined()) {
15626 Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>(
15627 BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate);
15628 if (BreakPointInfo::HasBreakPointObject(break_point_info,
15629 break_point_object)) {
15630 return break_point_info;
15635 return isolate->factory()->undefined_value();
15639 // Find the index of the break point info object for the specified code
15641 int DebugInfo::GetBreakPointInfoIndex(int code_position) {
15642 if (break_points()->IsUndefined()) return kNoBreakPointInfo;
15643 for (int i = 0; i < break_points()->length(); i++) {
15644 if (!break_points()->get(i)->IsUndefined()) {
15645 BreakPointInfo* break_point_info =
15646 BreakPointInfo::cast(break_points()->get(i));
15647 if (break_point_info->code_position()->value() == code_position) {
15652 return kNoBreakPointInfo;
15656 // Remove the specified break point object.
15657 void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
15658 Handle<Object> break_point_object) {
15659 Isolate* isolate = break_point_info->GetIsolate();
15660 // If there are no break points just ignore.
15661 if (break_point_info->break_point_objects()->IsUndefined()) return;
15662 // If there is a single break point clear it if it is the same.
15663 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15664 if (break_point_info->break_point_objects() == *break_point_object) {
15665 break_point_info->set_break_point_objects(
15666 isolate->heap()->undefined_value());
15670 // If there are multiple break points shrink the array
15671 DCHECK(break_point_info->break_point_objects()->IsFixedArray());
15672 Handle<FixedArray> old_array =
15673 Handle<FixedArray>(
15674 FixedArray::cast(break_point_info->break_point_objects()));
15675 Handle<FixedArray> new_array =
15676 isolate->factory()->NewFixedArray(old_array->length() - 1);
15677 int found_count = 0;
15678 for (int i = 0; i < old_array->length(); i++) {
15679 if (old_array->get(i) == *break_point_object) {
15680 DCHECK(found_count == 0);
15683 new_array->set(i - found_count, old_array->get(i));
15686 // If the break point was found in the list change it.
15687 if (found_count > 0) break_point_info->set_break_point_objects(*new_array);
15691 // Add the specified break point object.
15692 void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info,
15693 Handle<Object> break_point_object) {
15694 Isolate* isolate = break_point_info->GetIsolate();
15696 // If there was no break point objects before just set it.
15697 if (break_point_info->break_point_objects()->IsUndefined()) {
15698 break_point_info->set_break_point_objects(*break_point_object);
15701 // If the break point object is the same as before just ignore.
15702 if (break_point_info->break_point_objects() == *break_point_object) return;
15703 // If there was one break point object before replace with array.
15704 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15705 Handle<FixedArray> array = isolate->factory()->NewFixedArray(2);
15706 array->set(0, break_point_info->break_point_objects());
15707 array->set(1, *break_point_object);
15708 break_point_info->set_break_point_objects(*array);
15711 // If there was more than one break point before extend array.
15712 Handle<FixedArray> old_array =
15713 Handle<FixedArray>(
15714 FixedArray::cast(break_point_info->break_point_objects()));
15715 Handle<FixedArray> new_array =
15716 isolate->factory()->NewFixedArray(old_array->length() + 1);
15717 for (int i = 0; i < old_array->length(); i++) {
15718 // If the break point was there before just ignore.
15719 if (old_array->get(i) == *break_point_object) return;
15720 new_array->set(i, old_array->get(i));
15722 // Add the new break point.
15723 new_array->set(old_array->length(), *break_point_object);
15724 break_point_info->set_break_point_objects(*new_array);
15728 bool BreakPointInfo::HasBreakPointObject(
15729 Handle<BreakPointInfo> break_point_info,
15730 Handle<Object> break_point_object) {
15732 if (break_point_info->break_point_objects()->IsUndefined()) return false;
15733 // Single break point.
15734 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15735 return break_point_info->break_point_objects() == *break_point_object;
15737 // Multiple break points.
15738 FixedArray* array = FixedArray::cast(break_point_info->break_point_objects());
15739 for (int i = 0; i < array->length(); i++) {
15740 if (array->get(i) == *break_point_object) {
15748 // Get the number of break points.
15749 int BreakPointInfo::GetBreakPointCount() {
15751 if (break_point_objects()->IsUndefined()) return 0;
15752 // Single break point.
15753 if (!break_point_objects()->IsFixedArray()) return 1;
15754 // Multiple break points.
15755 return FixedArray::cast(break_point_objects())->length();
15759 Object* JSDate::GetField(Object* object, Smi* index) {
15760 return JSDate::cast(object)->DoGetField(
15761 static_cast<FieldIndex>(index->value()));
15765 Object* JSDate::DoGetField(FieldIndex index) {
15766 DCHECK(index != kDateValue);
15768 DateCache* date_cache = GetIsolate()->date_cache();
15770 if (index < kFirstUncachedField) {
15771 Object* stamp = cache_stamp();
15772 if (stamp != date_cache->stamp() && stamp->IsSmi()) {
15773 // Since the stamp is not NaN, the value is also not NaN.
15774 int64_t local_time_ms =
15775 date_cache->ToLocal(static_cast<int64_t>(value()->Number()));
15776 SetCachedFields(local_time_ms, date_cache);
15779 case kYear: return year();
15780 case kMonth: return month();
15781 case kDay: return day();
15782 case kWeekday: return weekday();
15783 case kHour: return hour();
15784 case kMinute: return min();
15785 case kSecond: return sec();
15786 default: UNREACHABLE();
15790 if (index >= kFirstUTCField) {
15791 return GetUTCField(index, value()->Number(), date_cache);
15794 double time = value()->Number();
15795 if (std::isnan(time)) return GetIsolate()->heap()->nan_value();
15797 int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time));
15798 int days = DateCache::DaysFromTime(local_time_ms);
15800 if (index == kDays) return Smi::FromInt(days);
15802 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15803 if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000);
15804 DCHECK(index == kTimeInDay);
15805 return Smi::FromInt(time_in_day_ms);
15809 Object* JSDate::GetUTCField(FieldIndex index,
15811 DateCache* date_cache) {
15812 DCHECK(index >= kFirstUTCField);
15814 if (std::isnan(value)) return GetIsolate()->heap()->nan_value();
15816 int64_t time_ms = static_cast<int64_t>(value);
15818 if (index == kTimezoneOffset) {
15819 return Smi::FromInt(date_cache->TimezoneOffset(time_ms));
15822 int days = DateCache::DaysFromTime(time_ms);
15824 if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days));
15826 if (index <= kDayUTC) {
15827 int year, month, day;
15828 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15829 if (index == kYearUTC) return Smi::FromInt(year);
15830 if (index == kMonthUTC) return Smi::FromInt(month);
15831 DCHECK(index == kDayUTC);
15832 return Smi::FromInt(day);
15835 int time_in_day_ms = DateCache::TimeInDay(time_ms, days);
15837 case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000));
15838 case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60);
15839 case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60);
15840 case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000);
15841 case kDaysUTC: return Smi::FromInt(days);
15842 case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms);
15843 default: UNREACHABLE();
15851 void JSDate::SetValue(Object* value, bool is_value_nan) {
15853 if (is_value_nan) {
15854 HeapNumber* nan = GetIsolate()->heap()->nan_value();
15855 set_cache_stamp(nan, SKIP_WRITE_BARRIER);
15856 set_year(nan, SKIP_WRITE_BARRIER);
15857 set_month(nan, SKIP_WRITE_BARRIER);
15858 set_day(nan, SKIP_WRITE_BARRIER);
15859 set_hour(nan, SKIP_WRITE_BARRIER);
15860 set_min(nan, SKIP_WRITE_BARRIER);
15861 set_sec(nan, SKIP_WRITE_BARRIER);
15862 set_weekday(nan, SKIP_WRITE_BARRIER);
15864 set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER);
15869 void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) {
15870 int days = DateCache::DaysFromTime(local_time_ms);
15871 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15872 int year, month, day;
15873 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15874 int weekday = date_cache->Weekday(days);
15875 int hour = time_in_day_ms / (60 * 60 * 1000);
15876 int min = (time_in_day_ms / (60 * 1000)) % 60;
15877 int sec = (time_in_day_ms / 1000) % 60;
15878 set_cache_stamp(date_cache->stamp());
15879 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
15880 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
15881 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
15882 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
15883 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
15884 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
15885 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
15889 void JSArrayBuffer::Neuter() {
15890 CHECK(is_neuterable());
15891 CHECK(is_external());
15892 set_backing_store(NULL);
15893 set_byte_length(Smi::FromInt(0));
15894 set_was_neutered(true);
15898 void JSArrayBuffer::Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate,
15899 bool is_external, void* data, size_t allocated_length,
15900 SharedFlag shared) {
15901 DCHECK(array_buffer->GetInternalFieldCount() ==
15902 v8::ArrayBuffer::kInternalFieldCount);
15903 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) {
15904 array_buffer->SetInternalField(i, Smi::FromInt(0));
15906 array_buffer->set_backing_store(data);
15907 array_buffer->set_bit_field(0);
15908 array_buffer->set_is_external(is_external);
15909 array_buffer->set_is_neuterable(shared == SharedFlag::kNotShared);
15910 array_buffer->set_is_shared(shared == SharedFlag::kShared);
15912 if (data && !is_external) {
15913 isolate->heap()->RegisterNewArrayBuffer(
15914 isolate->heap()->InNewSpace(*array_buffer), data, allocated_length);
15917 Handle<Object> byte_length =
15918 isolate->factory()->NewNumberFromSize(allocated_length);
15919 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber());
15920 array_buffer->set_byte_length(*byte_length);
15924 bool JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer> array_buffer,
15926 size_t allocated_length,
15927 bool initialize, SharedFlag shared) {
15929 CHECK(isolate->array_buffer_allocator() != NULL);
15930 // Prevent creating array buffers when serializing.
15931 DCHECK(!isolate->serializer_enabled());
15932 if (allocated_length != 0) {
15934 data = isolate->array_buffer_allocator()->Allocate(allocated_length);
15936 data = isolate->array_buffer_allocator()->AllocateUninitialized(
15939 if (data == NULL) return false;
15944 JSArrayBuffer::Setup(array_buffer, isolate, false, data, allocated_length,
15950 Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer(
15951 Handle<JSTypedArray> typed_array) {
15953 Handle<Map> map(typed_array->map());
15954 Isolate* isolate = typed_array->GetIsolate();
15956 DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind()));
15958 Handle<FixedTypedArrayBase> fixed_typed_array(
15959 FixedTypedArrayBase::cast(typed_array->elements()));
15961 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()),
15963 void* backing_store =
15964 isolate->array_buffer_allocator()->AllocateUninitialized(
15965 fixed_typed_array->DataSize());
15966 buffer->set_backing_store(backing_store);
15967 buffer->set_is_external(false);
15968 isolate->heap()->RegisterNewArrayBuffer(isolate->heap()->InNewSpace(*buffer),
15970 fixed_typed_array->DataSize());
15971 memcpy(buffer->backing_store(),
15972 fixed_typed_array->DataPtr(),
15973 fixed_typed_array->DataSize());
15974 Handle<FixedTypedArrayBase> new_elements =
15975 isolate->factory()->NewFixedTypedArrayWithExternalPointer(
15976 fixed_typed_array->length(), typed_array->type(),
15977 static_cast<uint8_t*>(buffer->backing_store()));
15979 typed_array->set_elements(*new_elements);
15985 Handle<JSArrayBuffer> JSTypedArray::GetBuffer() {
15986 Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()),
15988 if (array_buffer->was_neutered() ||
15989 array_buffer->backing_store() != nullptr) {
15990 return array_buffer;
15992 Handle<JSTypedArray> self(this);
15993 return MaterializeArrayBuffer(self);
15997 Handle<PropertyCell> PropertyCell::InvalidateEntry(
15998 Handle<GlobalDictionary> dictionary, int entry) {
15999 Isolate* isolate = dictionary->GetIsolate();
16000 // Swap with a copy.
16001 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
16002 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
16003 auto new_cell = isolate->factory()->NewPropertyCell();
16004 new_cell->set_value(cell->value());
16005 dictionary->ValueAtPut(entry, *new_cell);
16006 bool is_the_hole = cell->value()->IsTheHole();
16007 // Cell is officially mutable henceforth.
16008 PropertyDetails details = cell->property_details();
16009 details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated
16010 : PropertyCellType::kMutable);
16011 new_cell->set_property_details(details);
16012 // Old cell is ready for invalidation.
16014 cell->set_value(isolate->heap()->undefined_value());
16016 cell->set_value(isolate->heap()->the_hole_value());
16018 details = details.set_cell_type(PropertyCellType::kInvalidated);
16019 cell->set_property_details(details);
16020 cell->dependent_code()->DeoptimizeDependentCodeGroup(
16021 isolate, DependentCode::kPropertyCellChangedGroup);
16026 PropertyCellConstantType PropertyCell::GetConstantType() {
16027 if (value()->IsSmi()) return PropertyCellConstantType::kSmi;
16028 return PropertyCellConstantType::kStableMap;
16032 static bool RemainsConstantType(Handle<PropertyCell> cell,
16033 Handle<Object> value) {
16034 // TODO(dcarney): double->smi and smi->double transition from kConstant
16035 if (cell->value()->IsSmi() && value->IsSmi()) {
16037 } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) {
16038 return HeapObject::cast(cell->value())->map() ==
16039 HeapObject::cast(*value)->map() &&
16040 HeapObject::cast(*value)->map()->is_stable();
16046 PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell,
16047 Handle<Object> value,
16048 PropertyDetails details) {
16049 PropertyCellType type = details.cell_type();
16050 DCHECK(!value->IsTheHole());
16051 if (cell->value()->IsTheHole()) {
16053 // Only allow a cell to transition once into constant state.
16054 case PropertyCellType::kUninitialized:
16055 if (value->IsUndefined()) return PropertyCellType::kUndefined;
16056 return PropertyCellType::kConstant;
16057 case PropertyCellType::kInvalidated:
16058 return PropertyCellType::kMutable;
16061 return PropertyCellType::kMutable;
16065 case PropertyCellType::kUndefined:
16066 return PropertyCellType::kConstant;
16067 case PropertyCellType::kConstant:
16068 if (*value == cell->value()) return PropertyCellType::kConstant;
16070 case PropertyCellType::kConstantType:
16071 if (RemainsConstantType(cell, value)) {
16072 return PropertyCellType::kConstantType;
16075 case PropertyCellType::kMutable:
16076 return PropertyCellType::kMutable;
16079 return PropertyCellType::kMutable;
16083 void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry,
16084 Handle<Object> value, PropertyDetails details) {
16085 DCHECK(!value->IsTheHole());
16086 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
16087 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
16088 const PropertyDetails original_details = cell->property_details();
16089 // Data accesses could be cached in ics or optimized code.
16091 original_details.kind() == kData && details.kind() == kAccessor;
16092 int index = original_details.dictionary_index();
16093 PropertyCellType old_type = original_details.cell_type();
16094 // Preserve the enumeration index unless the property was deleted or never
16096 if (cell->value()->IsTheHole()) {
16097 index = dictionary->NextEnumerationIndex();
16098 dictionary->SetNextEnumerationIndex(index + 1);
16099 // Negative lookup cells must be invalidated.
16103 details = details.set_index(index);
16105 PropertyCellType new_type = UpdatedType(cell, value, original_details);
16106 if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry);
16108 // Install new property details and cell value.
16109 details = details.set_cell_type(new_type);
16110 cell->set_property_details(details);
16111 cell->set_value(*value);
16113 // Deopt when transitioning from a constant type.
16114 if (!invalidate && (old_type != new_type ||
16115 original_details.IsReadOnly() != details.IsReadOnly())) {
16116 Isolate* isolate = dictionary->GetIsolate();
16117 cell->dependent_code()->DeoptimizeDependentCodeGroup(
16118 isolate, DependentCode::kPropertyCellChangedGroup);
16124 void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell,
16125 Handle<Object> new_value) {
16126 if (cell->value() != *new_value) {
16127 cell->set_value(*new_value);
16128 Isolate* isolate = cell->GetIsolate();
16129 cell->dependent_code()->DeoptimizeDependentCodeGroup(
16130 isolate, DependentCode::kPropertyCellChangedGroup);
16134 } // namespace internal