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);
93 MaybeHandle<Name> Object::ToName(Isolate* isolate, Handle<Object> object) {
94 if (object->IsName()) {
95 return Handle<Name>::cast(object);
97 Handle<Object> converted;
98 ASSIGN_RETURN_ON_EXCEPTION(isolate, converted,
99 Execution::ToString(isolate, object), Name);
100 return Handle<Name>::cast(converted);
105 bool Object::BooleanValue() {
106 if (IsBoolean()) return IsTrue();
107 if (IsSmi()) return Smi::cast(this)->value() != 0;
108 if (IsUndefined() || IsNull()) return false;
109 if (IsUndetectableObject()) return false; // Undetectable object is false.
110 if (IsString()) return String::cast(this)->length() != 0;
111 if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue();
112 if (IsSimd128Value()) return true; // Simd value types evaluate to true.
117 bool Object::StrictEquals(Object* that) {
118 if (this->IsNumber()) {
119 if (!that->IsNumber()) return false;
120 double const x = this->Number();
121 double const y = that->Number();
122 // Must check explicitly for NaN:s on Windows, but -0 works fine.
123 return x == y && !std::isnan(x) && !std::isnan(y);
124 } else if (this->IsString()) {
125 if (!that->IsString()) return false;
126 return String::cast(this)->Equals(String::cast(that));
127 } else if (this->IsSimd128Value()) {
128 if (!that->IsSimd128Value()) return false;
129 return Simd128Value::cast(this)->Equals(Simd128Value::cast(that));
135 bool Object::IsCallable() const {
136 const Object* fun = this;
137 while (fun->IsJSFunctionProxy()) {
138 fun = JSFunctionProxy::cast(fun)->call_trap();
140 return fun->IsJSFunction() ||
141 (fun->IsHeapObject() &&
142 HeapObject::cast(fun)->map()->has_instance_call_handler());
146 bool Object::IsPromise(Handle<Object> object) {
147 if (!object->IsJSObject()) return false;
148 auto js_object = Handle<JSObject>::cast(object);
149 // Promises can't have access checks.
150 if (js_object->map()->is_access_check_needed()) return false;
151 auto isolate = js_object->GetIsolate();
152 // TODO(dcarney): this should just be read from the symbol registry so as not
153 // to be context dependent.
154 auto key = isolate->factory()->promise_status_symbol();
155 // Shouldn't be possible to throw here.
156 return JSObject::HasRealNamedProperty(js_object, key).FromJust();
160 MaybeHandle<Object> Object::GetProperty(LookupIterator* it,
161 LanguageMode language_mode) {
162 for (; it->IsFound(); it->Next()) {
163 switch (it->state()) {
164 case LookupIterator::NOT_FOUND:
165 case LookupIterator::TRANSITION:
167 case LookupIterator::JSPROXY:
168 return JSProxy::GetPropertyWithHandler(
169 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
170 case LookupIterator::INTERCEPTOR: {
172 Handle<Object> result;
173 ASSIGN_RETURN_ON_EXCEPTION(
174 it->isolate(), result,
175 JSObject::GetPropertyWithInterceptor(it, &done), Object);
176 if (done) return result;
179 case LookupIterator::ACCESS_CHECK:
180 if (it->HasAccess()) break;
181 return JSObject::GetPropertyWithFailedAccessCheck(it);
182 case LookupIterator::ACCESSOR:
183 return GetPropertyWithAccessor(it, language_mode);
184 case LookupIterator::INTEGER_INDEXED_EXOTIC:
185 return ReadAbsentProperty(it, language_mode);
186 case LookupIterator::DATA:
187 return it->GetDataValue();
190 return ReadAbsentProperty(it, language_mode);
194 Handle<Object> JSReceiver::GetDataProperty(Handle<JSReceiver> object,
196 LookupIterator it(object, name,
197 LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
198 return GetDataProperty(&it);
202 Handle<Object> JSReceiver::GetDataProperty(LookupIterator* it) {
203 for (; it->IsFound(); it->Next()) {
204 switch (it->state()) {
205 case LookupIterator::INTERCEPTOR:
206 case LookupIterator::NOT_FOUND:
207 case LookupIterator::TRANSITION:
209 case LookupIterator::ACCESS_CHECK:
210 if (it->HasAccess()) continue;
212 case LookupIterator::JSPROXY:
214 return it->isolate()->factory()->undefined_value();
215 case LookupIterator::ACCESSOR:
216 // TODO(verwaest): For now this doesn't call into
217 // ExecutableAccessorInfo, since clients don't need it. Update once
220 return it->isolate()->factory()->undefined_value();
221 case LookupIterator::INTEGER_INDEXED_EXOTIC:
222 return it->isolate()->factory()->undefined_value();
223 case LookupIterator::DATA:
224 return it->GetDataValue();
227 return it->isolate()->factory()->undefined_value();
231 bool Object::ToInt32(int32_t* value) {
233 *value = Smi::cast(this)->value();
236 if (IsHeapNumber()) {
237 double num = HeapNumber::cast(this)->value();
238 if (FastI2D(FastD2I(num)) == num) {
239 *value = FastD2I(num);
247 bool Object::ToUint32(uint32_t* value) {
249 int num = Smi::cast(this)->value();
251 *value = static_cast<uint32_t>(num);
255 if (IsHeapNumber()) {
256 double num = HeapNumber::cast(this)->value();
257 if (num >= 0 && FastUI2D(FastD2UI(num)) == num) {
258 *value = FastD2UI(num);
266 bool FunctionTemplateInfo::IsTemplateFor(Object* object) {
267 if (!object->IsHeapObject()) return false;
268 return IsTemplateFor(HeapObject::cast(object)->map());
272 bool FunctionTemplateInfo::IsTemplateFor(Map* map) {
273 // There is a constraint on the object; check.
274 if (!map->IsJSObjectMap()) return false;
275 // Fetch the constructor function of the object.
276 Object* cons_obj = map->GetConstructor();
277 if (!cons_obj->IsJSFunction()) return false;
278 JSFunction* fun = JSFunction::cast(cons_obj);
279 // Iterate through the chain of inheriting function templates to
280 // see if the required one occurs.
281 for (Object* type = fun->shared()->function_data();
282 type->IsFunctionTemplateInfo();
283 type = FunctionTemplateInfo::cast(type)->parent_template()) {
284 if (type == this) return true;
286 // Didn't find the required type in the inheritance chain.
291 // TODO(dcarney): CallOptimization duplicates this logic, merge.
292 Object* FunctionTemplateInfo::GetCompatibleReceiver(Isolate* isolate,
294 // API calls are only supported with JSObject receivers.
295 if (!receiver->IsJSObject()) return isolate->heap()->null_value();
296 Object* recv_type = this->signature();
297 // No signature, return holder.
298 if (recv_type->IsUndefined()) return receiver;
299 FunctionTemplateInfo* signature = FunctionTemplateInfo::cast(recv_type);
300 // Check the receiver.
301 for (PrototypeIterator iter(isolate, receiver,
302 PrototypeIterator::START_AT_RECEIVER);
303 !iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN); iter.Advance()) {
304 if (signature->IsTemplateFor(iter.GetCurrent())) return iter.GetCurrent();
306 return isolate->heap()->null_value();
310 Handle<FixedArray> JSObject::EnsureWritableFastElements(
311 Handle<JSObject> object) {
312 DCHECK(object->HasFastSmiOrObjectElements());
313 Isolate* isolate = object->GetIsolate();
314 Handle<FixedArray> elems(FixedArray::cast(object->elements()), isolate);
315 if (elems->map() != isolate->heap()->fixed_cow_array_map()) return elems;
316 Handle<FixedArray> writable_elems = isolate->factory()->CopyFixedArrayWithMap(
317 elems, isolate->factory()->fixed_array_map());
318 object->set_elements(*writable_elems);
319 isolate->counters()->cow_arrays_converted()->Increment();
320 return writable_elems;
324 MaybeHandle<Object> JSProxy::GetPropertyWithHandler(Handle<JSProxy> proxy,
325 Handle<Object> receiver,
327 Isolate* isolate = proxy->GetIsolate();
329 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
330 if (name->IsSymbol()) return isolate->factory()->undefined_value();
332 Handle<Object> args[] = { receiver, name };
334 proxy, "get", isolate->derived_get_trap(), arraysize(args), args);
338 MaybeHandle<Object> Object::GetPropertyWithAccessor(
339 LookupIterator* it, LanguageMode language_mode) {
340 Isolate* isolate = it->isolate();
341 Handle<Object> structure = it->GetAccessors();
342 Handle<Object> receiver = it->GetReceiver();
344 // We should never get here to initialize a const with the hole value since a
345 // const declaration would conflict with the getter.
346 DCHECK(!structure->IsForeign());
348 // API style callbacks.
349 if (structure->IsAccessorInfo()) {
350 Handle<JSObject> holder = it->GetHolder<JSObject>();
351 Handle<Name> name = it->GetName();
352 Handle<ExecutableAccessorInfo> info =
353 Handle<ExecutableAccessorInfo>::cast(structure);
354 if (!info->IsCompatibleReceiver(*receiver)) {
355 THROW_NEW_ERROR(isolate,
356 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
361 v8::AccessorNameGetterCallback call_fun =
362 v8::ToCData<v8::AccessorNameGetterCallback>(info->getter());
363 if (call_fun == nullptr) return isolate->factory()->undefined_value();
365 LOG(isolate, ApiNamedPropertyAccess("load", *holder, *name));
366 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
367 v8::Local<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(name));
368 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
369 if (result.IsEmpty()) {
370 return ReadAbsentProperty(isolate, receiver, name, language_mode);
372 Handle<Object> return_value = v8::Utils::OpenHandle(*result);
373 return_value->VerifyApiCallResultType();
374 // Rebox handle before return.
375 return handle(*return_value, isolate);
379 Handle<Object> getter(AccessorPair::cast(*structure)->getter(), isolate);
380 if (getter->IsSpecFunction()) {
381 // TODO(rossberg): nicer would be to cast to some JSCallable here...
382 return Object::GetPropertyWithDefinedGetter(
383 receiver, Handle<JSReceiver>::cast(getter));
385 // Getter is not a function.
386 return ReadAbsentProperty(isolate, receiver, it->GetName(), language_mode);
390 bool AccessorInfo::IsCompatibleReceiverMap(Isolate* isolate,
391 Handle<AccessorInfo> info,
393 if (!info->HasExpectedReceiverType()) return true;
394 if (!map->IsJSObjectMap()) return false;
395 return FunctionTemplateInfo::cast(info->expected_receiver_type())
396 ->IsTemplateFor(*map);
400 MaybeHandle<Object> Object::SetPropertyWithAccessor(
401 LookupIterator* it, Handle<Object> value, 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 setter.
408 DCHECK(!structure->IsForeign());
410 // API style callbacks.
411 if (structure->IsExecutableAccessorInfo()) {
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::AccessorNameSetterCallback call_fun =
424 v8::ToCData<v8::AccessorNameSetterCallback>(info->setter());
425 if (call_fun == nullptr) return value;
427 LOG(isolate, ApiNamedPropertyAccess("store", *holder, *name));
428 PropertyCallbackArguments args(isolate, info->data(), *receiver, *holder);
429 args.Call(call_fun, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
430 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
435 Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate);
436 if (setter->IsSpecFunction()) {
437 // TODO(rossberg): nicer would be to cast to some JSCallable here...
438 return SetPropertyWithDefinedSetter(
439 receiver, Handle<JSReceiver>::cast(setter), value);
442 if (is_sloppy(language_mode)) return value;
444 THROW_NEW_ERROR(isolate,
445 NewTypeError(MessageTemplate::kNoSetterInCallback,
446 it->GetName(), it->GetHolder<JSObject>()),
451 MaybeHandle<Object> Object::GetPropertyWithDefinedGetter(
452 Handle<Object> receiver,
453 Handle<JSReceiver> getter) {
454 Isolate* isolate = getter->GetIsolate();
456 // Platforms with simulators like arm/arm64 expose a funny issue. If the
457 // simulator has a separate JS stack pointer from the C++ stack pointer, it
458 // can miss C++ stack overflows in the stack guard at the start of JavaScript
459 // functions. It would be very expensive to check the C++ stack pointer at
460 // that location. The best solution seems to be to break the impasse by
461 // adding checks at possible recursion points. What's more, we don't put
462 // this stack check behind the USE_SIMULATOR define in order to keep
463 // behavior the same between hardware and simulators.
464 StackLimitCheck check(isolate);
465 if (check.JsHasOverflowed()) {
466 isolate->StackOverflow();
467 return MaybeHandle<Object>();
470 Debug* debug = isolate->debug();
471 // Handle stepping into a getter if step into is active.
472 // TODO(rossberg): should this apply to getters that are function proxies?
473 if (debug->is_active()) debug->HandleStepIn(getter, false);
475 return Execution::Call(isolate, getter, receiver, 0, NULL, true);
479 MaybeHandle<Object> Object::SetPropertyWithDefinedSetter(
480 Handle<Object> receiver,
481 Handle<JSReceiver> setter,
482 Handle<Object> value) {
483 Isolate* isolate = setter->GetIsolate();
485 Debug* debug = isolate->debug();
486 // Handle stepping into a setter if step into is active.
487 // TODO(rossberg): should this apply to getters that are function proxies?
488 if (debug->is_active()) debug->HandleStepIn(setter, false);
490 Handle<Object> argv[] = { value };
491 RETURN_ON_EXCEPTION(isolate, Execution::Call(isolate, setter, receiver,
492 arraysize(argv), argv, true),
499 bool JSObject::AllCanRead(LookupIterator* it) {
500 // Skip current iteration, it's in state ACCESS_CHECK or INTERCEPTOR, both of
501 // which have already been checked.
502 DCHECK(it->state() == LookupIterator::ACCESS_CHECK ||
503 it->state() == LookupIterator::INTERCEPTOR);
504 for (it->Next(); it->IsFound(); it->Next()) {
505 if (it->state() == LookupIterator::ACCESSOR) {
506 auto accessors = it->GetAccessors();
507 if (accessors->IsAccessorInfo()) {
508 if (AccessorInfo::cast(*accessors)->all_can_read()) return true;
510 } else if (it->state() == LookupIterator::INTERCEPTOR) {
511 if (it->GetInterceptor()->all_can_read()) return true;
518 MaybeHandle<Object> JSObject::GetPropertyWithFailedAccessCheck(
519 LookupIterator* it) {
520 Handle<JSObject> checked = it->GetHolder<JSObject>();
521 while (AllCanRead(it)) {
522 if (it->state() == LookupIterator::ACCESSOR) {
523 return GetPropertyWithAccessor(it, SLOPPY);
525 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
527 Handle<Object> result;
528 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), result,
529 GetPropertyWithInterceptor(it, &done), Object);
530 if (done) return result;
532 it->isolate()->ReportFailedAccessCheck(checked);
533 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
534 return it->factory()->undefined_value();
538 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithFailedAccessCheck(
539 LookupIterator* it) {
540 Handle<JSObject> checked = it->GetHolder<JSObject>();
541 while (AllCanRead(it)) {
542 if (it->state() == LookupIterator::ACCESSOR) {
543 return Just(it->property_details().attributes());
545 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
546 auto result = GetPropertyAttributesWithInterceptor(it);
547 if (it->isolate()->has_scheduled_exception()) break;
548 if (result.IsJust() && result.FromJust() != ABSENT) return result;
550 it->isolate()->ReportFailedAccessCheck(checked);
551 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(it->isolate(),
552 Nothing<PropertyAttributes>());
558 bool JSObject::AllCanWrite(LookupIterator* it) {
559 for (; it->IsFound(); it->Next()) {
560 if (it->state() == LookupIterator::ACCESSOR) {
561 Handle<Object> accessors = it->GetAccessors();
562 if (accessors->IsAccessorInfo()) {
563 if (AccessorInfo::cast(*accessors)->all_can_write()) return true;
571 MaybeHandle<Object> JSObject::SetPropertyWithFailedAccessCheck(
572 LookupIterator* it, Handle<Object> value) {
573 Handle<JSObject> checked = it->GetHolder<JSObject>();
574 if (AllCanWrite(it)) {
575 // The supplied language-mode is ignored by SetPropertyWithAccessor.
576 return SetPropertyWithAccessor(it, value, SLOPPY);
579 it->isolate()->ReportFailedAccessCheck(checked);
580 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
585 void JSObject::SetNormalizedProperty(Handle<JSObject> object,
587 Handle<Object> value,
588 PropertyDetails details) {
589 DCHECK(!object->HasFastProperties());
590 if (!name->IsUniqueName()) {
591 name = object->GetIsolate()->factory()->InternalizeString(
592 Handle<String>::cast(name));
595 if (object->IsGlobalObject()) {
596 Handle<GlobalDictionary> property_dictionary(object->global_dictionary());
598 int entry = property_dictionary->FindEntry(name);
599 if (entry == GlobalDictionary::kNotFound) {
600 auto cell = object->GetIsolate()->factory()->NewPropertyCell();
601 cell->set_value(*value);
602 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
603 : PropertyCellType::kConstant;
604 details = details.set_cell_type(cell_type);
606 property_dictionary =
607 GlobalDictionary::Add(property_dictionary, name, value, details);
608 object->set_properties(*property_dictionary);
610 PropertyCell::UpdateCell(property_dictionary, entry, value, details);
613 Handle<NameDictionary> property_dictionary(object->property_dictionary());
615 int entry = property_dictionary->FindEntry(name);
616 if (entry == NameDictionary::kNotFound) {
617 property_dictionary =
618 NameDictionary::Add(property_dictionary, name, value, details);
619 object->set_properties(*property_dictionary);
621 PropertyDetails original_details = property_dictionary->DetailsAt(entry);
622 int enumeration_index = original_details.dictionary_index();
623 DCHECK(enumeration_index > 0);
624 details = details.set_index(enumeration_index);
625 property_dictionary->SetEntry(entry, name, value, details);
631 bool Object::HasInPrototypeChain(Isolate* isolate, Object* target) {
632 PrototypeIterator iter(isolate, this, PrototypeIterator::START_AT_RECEIVER);
634 iter.AdvanceIgnoringProxies();
635 if (iter.IsAtEnd()) return false;
636 if (iter.IsAtEnd(target)) return true;
641 Map* Object::GetRootMap(Isolate* isolate) {
642 DisallowHeapAllocation no_alloc;
644 Context* native_context = isolate->context()->native_context();
645 return native_context->number_function()->initial_map();
648 // The object is either a number, a string, a symbol, a boolean, a SIMD value,
649 // a real JS object, or a Harmony proxy.
650 HeapObject* heap_object = HeapObject::cast(this);
651 if (heap_object->IsJSReceiver()) {
652 return heap_object->map();
654 int constructor_function_index =
655 heap_object->map()->GetConstructorFunctionIndex();
656 if (constructor_function_index != Map::kNoConstructorFunctionIndex) {
657 Context* native_context = isolate->context()->native_context();
658 JSFunction* constructor_function =
659 JSFunction::cast(native_context->get(constructor_function_index));
660 return constructor_function->initial_map();
662 return isolate->heap()->null_value()->map();
666 Object* Object::GetHash() {
667 Object* hash = GetSimpleHash();
668 if (hash->IsSmi()) return hash;
670 DCHECK(IsJSReceiver());
671 return JSReceiver::cast(this)->GetIdentityHash();
675 Object* Object::GetSimpleHash() {
676 // The object is either a Smi, a HeapNumber, a name, an odd-ball,
677 // a SIMD value type, a real JS object, or a Harmony proxy.
679 uint32_t hash = ComputeIntegerHash(Smi::cast(this)->value(), kZeroHashSeed);
680 return Smi::FromInt(hash & Smi::kMaxValue);
682 if (IsHeapNumber()) {
683 double num = HeapNumber::cast(this)->value();
684 if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue);
685 if (i::IsMinusZero(num)) num = 0;
686 if (IsSmiDouble(num)) {
687 return Smi::FromInt(FastD2I(num))->GetHash();
689 uint32_t hash = ComputeLongHash(double_to_uint64(num));
690 return Smi::FromInt(hash & Smi::kMaxValue);
693 uint32_t hash = Name::cast(this)->Hash();
694 return Smi::FromInt(hash);
697 uint32_t hash = Oddball::cast(this)->to_string()->Hash();
698 return Smi::FromInt(hash);
700 if (IsSimd128Value()) {
701 uint32_t hash = Simd128Value::cast(this)->Hash();
702 return Smi::FromInt(hash & Smi::kMaxValue);
704 DCHECK(IsJSReceiver());
705 JSReceiver* receiver = JSReceiver::cast(this);
706 return receiver->GetHeap()->undefined_value();
710 Handle<Smi> Object::GetOrCreateHash(Isolate* isolate, Handle<Object> object) {
711 Handle<Object> hash(object->GetSimpleHash(), isolate);
712 if (hash->IsSmi()) return Handle<Smi>::cast(hash);
714 DCHECK(object->IsJSReceiver());
715 return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object));
719 bool Object::SameValue(Object* other) {
720 if (other == this) return true;
722 // The object is either a number, a name, an odd-ball,
723 // a real JS object, or a Harmony proxy.
724 if (IsNumber() && other->IsNumber()) {
725 double this_value = Number();
726 double other_value = other->Number();
727 // SameValue(NaN, NaN) is true.
728 if (this_value != other_value) {
729 return std::isnan(this_value) && std::isnan(other_value);
731 // SameValue(0.0, -0.0) is false.
732 return (std::signbit(this_value) == std::signbit(other_value));
734 if (IsString() && other->IsString()) {
735 return String::cast(this)->Equals(String::cast(other));
737 if (IsSimd128Value() && other->IsSimd128Value()) {
738 if (IsFloat32x4() && other->IsFloat32x4()) {
739 Float32x4* a = Float32x4::cast(this);
740 Float32x4* b = Float32x4::cast(other);
741 for (int i = 0; i < 4; i++) {
742 float x = a->get_lane(i);
743 float y = b->get_lane(i);
744 // Implements the ES5 SameValue operation for floating point types.
745 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevalue
746 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
747 if (std::signbit(x) != std::signbit(y)) return false;
751 Simd128Value* a = Simd128Value::cast(this);
752 Simd128Value* b = Simd128Value::cast(other);
753 return a->map()->instance_type() == b->map()->instance_type() &&
761 bool Object::SameValueZero(Object* other) {
762 if (other == this) return true;
764 // The object is either a number, a name, an odd-ball,
765 // a real JS object, or a Harmony proxy.
766 if (IsNumber() && other->IsNumber()) {
767 double this_value = Number();
768 double other_value = other->Number();
770 return this_value == other_value ||
771 (std::isnan(this_value) && std::isnan(other_value));
773 if (IsString() && other->IsString()) {
774 return String::cast(this)->Equals(String::cast(other));
776 if (IsSimd128Value() && other->IsSimd128Value()) {
777 if (IsFloat32x4() && other->IsFloat32x4()) {
778 Float32x4* a = Float32x4::cast(this);
779 Float32x4* b = Float32x4::cast(other);
780 for (int i = 0; i < 4; i++) {
781 float x = a->get_lane(i);
782 float y = b->get_lane(i);
783 // Implements the ES6 SameValueZero operation for floating point types.
784 // http://www.ecma-international.org/ecma-262/6.0/#sec-samevaluezero
785 if (x != y && !(std::isnan(x) && std::isnan(y))) return false;
786 // SameValueZero doesn't distinguish between 0 and -0.
790 Simd128Value* a = Simd128Value::cast(this);
791 Simd128Value* b = Simd128Value::cast(other);
792 return a->map()->instance_type() == b->map()->instance_type() &&
800 void Object::ShortPrint(FILE* out) {
806 void Object::ShortPrint(StringStream* accumulator) {
807 std::ostringstream os;
809 accumulator->Add(os.str().c_str());
813 void Object::ShortPrint(std::ostream& os) { os << Brief(this); }
816 std::ostream& operator<<(std::ostream& os, const Brief& v) {
817 if (v.value->IsSmi()) {
818 Smi::cast(v.value)->SmiPrint(os);
820 // TODO(svenpanne) Const-correct HeapObjectShortPrint!
821 HeapObject* obj = const_cast<HeapObject*>(HeapObject::cast(v.value));
822 obj->HeapObjectShortPrint(os);
828 void Smi::SmiPrint(std::ostream& os) const { // NOLINT
833 // Should a word be prefixed by 'a' or 'an' in order to read naturally in
834 // English? Returns false for non-ASCII or words that don't start with
835 // a capital letter. The a/an rule follows pronunciation in English.
836 // We don't use the BBC's overcorrect "an historic occasion" though if
837 // you speak a dialect you may well say "an 'istoric occasion".
838 static bool AnWord(String* str) {
839 if (str->length() == 0) return false; // A nothing.
840 int c0 = str->Get(0);
841 int c1 = str->length() > 1 ? str->Get(1) : 0;
844 return true; // An Umpire, but a UTF8String, a U.
846 } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') {
847 return true; // An Ape, an ABCBook.
848 } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) &&
849 (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' ||
850 c0 == 'S' || c0 == 'X')) {
851 return true; // An MP3File, an M.
857 Handle<String> String::SlowFlatten(Handle<ConsString> cons,
858 PretenureFlag pretenure) {
859 DCHECK(AllowHeapAllocation::IsAllowed());
860 DCHECK(cons->second()->length() != 0);
861 Isolate* isolate = cons->GetIsolate();
862 int length = cons->length();
863 PretenureFlag tenure = isolate->heap()->InNewSpace(*cons) ? pretenure
865 Handle<SeqString> result;
866 if (cons->IsOneByteRepresentation()) {
867 Handle<SeqOneByteString> flat = isolate->factory()->NewRawOneByteString(
868 length, tenure).ToHandleChecked();
869 DisallowHeapAllocation no_gc;
870 WriteToFlat(*cons, flat->GetChars(), 0, length);
873 Handle<SeqTwoByteString> flat = isolate->factory()->NewRawTwoByteString(
874 length, tenure).ToHandleChecked();
875 DisallowHeapAllocation no_gc;
876 WriteToFlat(*cons, flat->GetChars(), 0, length);
879 cons->set_first(*result);
880 cons->set_second(isolate->heap()->empty_string());
881 DCHECK(result->IsFlat());
887 bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
888 // Externalizing twice leaks the external resource, so it's
889 // prohibited by the API.
890 DCHECK(!this->IsExternalString());
891 #ifdef ENABLE_SLOW_DCHECKS
892 if (FLAG_enable_slow_asserts) {
893 // Assert that the resource and the string are equivalent.
894 DCHECK(static_cast<size_t>(this->length()) == resource->length());
895 ScopedVector<uc16> smart_chars(this->length());
896 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
897 DCHECK(memcmp(smart_chars.start(),
899 resource->length() * sizeof(smart_chars[0])) == 0);
902 int size = this->Size(); // Byte size of the original string.
903 // Abort if size does not allow in-place conversion.
904 if (size < ExternalString::kShortSize) return false;
905 Heap* heap = GetHeap();
906 bool is_one_byte = this->IsOneByteRepresentation();
907 bool is_internalized = this->IsInternalizedString();
909 // Morph the string to an external string by replacing the map and
910 // reinitializing the fields. This won't work if the space the existing
911 // string occupies is too small for a regular external string.
912 // Instead, we resort to a short external string instead, omitting
913 // the field caching the address of the backing store. When we encounter
914 // short external strings in generated code, we need to bailout to runtime.
916 if (size < ExternalString::kSize) {
917 new_map = is_internalized
919 ? heap->short_external_internalized_string_with_one_byte_data_map()
920 : heap->short_external_internalized_string_map())
921 : (is_one_byte ? heap->short_external_string_with_one_byte_data_map()
922 : heap->short_external_string_map());
924 new_map = is_internalized
926 ? heap->external_internalized_string_with_one_byte_data_map()
927 : heap->external_internalized_string_map())
928 : (is_one_byte ? heap->external_string_with_one_byte_data_map()
929 : heap->external_string_map());
932 // Byte size of the external String object.
933 int new_size = this->SizeFromMap(new_map);
934 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
936 // We are storing the new map using release store after creating a filler for
937 // the left-over space to avoid races with the sweeper thread.
938 this->synchronized_set_map(new_map);
940 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
941 self->set_resource(resource);
942 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
944 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
949 bool String::MakeExternal(v8::String::ExternalOneByteStringResource* 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 if (this->IsTwoByteRepresentation()) {
958 ScopedVector<uint16_t> smart_chars(this->length());
959 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
960 DCHECK(String::IsOneByte(smart_chars.start(), this->length()));
962 ScopedVector<char> smart_chars(this->length());
963 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
964 DCHECK(memcmp(smart_chars.start(),
966 resource->length() * sizeof(smart_chars[0])) == 0);
969 int size = this->Size(); // Byte size of the original string.
970 // Abort if size does not allow in-place conversion.
971 if (size < ExternalString::kShortSize) return false;
972 Heap* heap = GetHeap();
973 bool is_internalized = this->IsInternalizedString();
975 // Morph the string to an external string by replacing the map and
976 // reinitializing the fields. This won't work if the space the existing
977 // string occupies is too small for a regular external string.
978 // Instead, we resort to a short external string instead, omitting
979 // the field caching the address of the backing store. When we encounter
980 // short external strings in generated code, we need to bailout to runtime.
982 if (size < ExternalString::kSize) {
983 new_map = is_internalized
984 ? heap->short_external_one_byte_internalized_string_map()
985 : heap->short_external_one_byte_string_map();
987 new_map = is_internalized
988 ? heap->external_one_byte_internalized_string_map()
989 : heap->external_one_byte_string_map();
992 // Byte size of the external String object.
993 int new_size = this->SizeFromMap(new_map);
994 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
996 // We are storing the new map using release store after creating a filler for
997 // the left-over space to avoid races with the sweeper thread.
998 this->synchronized_set_map(new_map);
1000 ExternalOneByteString* self = ExternalOneByteString::cast(this);
1001 self->set_resource(resource);
1002 if (is_internalized) self->Hash(); // Force regeneration of the hash value.
1004 heap->AdjustLiveBytes(this, new_size - size, Heap::CONCURRENT_TO_SWEEPER);
1009 void String::StringShortPrint(StringStream* accumulator) {
1011 if (len > kMaxShortPrintLength) {
1012 accumulator->Add("<Very long string[%u]>", len);
1016 if (!LooksValid()) {
1017 accumulator->Add("<Invalid String>");
1021 StringCharacterStream stream(this);
1023 bool truncated = false;
1024 if (len > kMaxShortPrintLength) {
1025 len = kMaxShortPrintLength;
1028 bool one_byte = true;
1029 for (int i = 0; i < len; i++) {
1030 uint16_t c = stream.GetNext();
1032 if (c < 32 || c >= 127) {
1038 accumulator->Add("<String[%u]: ", length());
1039 for (int i = 0; i < len; i++) {
1040 accumulator->Put(static_cast<char>(stream.GetNext()));
1042 accumulator->Put('>');
1044 // Backslash indicates that the string contains control
1045 // characters and that backslashes are therefore escaped.
1046 accumulator->Add("<String[%u]\\: ", length());
1047 for (int i = 0; i < len; i++) {
1048 uint16_t c = stream.GetNext();
1050 accumulator->Add("\\n");
1051 } else if (c == '\r') {
1052 accumulator->Add("\\r");
1053 } else if (c == '\\') {
1054 accumulator->Add("\\\\");
1055 } else if (c < 32 || c > 126) {
1056 accumulator->Add("\\x%02x", c);
1058 accumulator->Put(static_cast<char>(c));
1062 accumulator->Put('.');
1063 accumulator->Put('.');
1064 accumulator->Put('.');
1066 accumulator->Put('>');
1072 void String::PrintUC16(std::ostream& os, int start, int end) { // NOLINT
1073 if (end < 0) end = length();
1074 StringCharacterStream stream(this, start);
1075 for (int i = start; i < end && stream.HasMore(); i++) {
1076 os << AsUC16(stream.GetNext());
1081 void JSObject::JSObjectShortPrint(StringStream* accumulator) {
1082 switch (map()->instance_type()) {
1083 case JS_ARRAY_TYPE: {
1084 double length = JSArray::cast(this)->length()->IsUndefined()
1086 : JSArray::cast(this)->length()->Number();
1087 accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length));
1090 case JS_WEAK_MAP_TYPE: {
1091 accumulator->Add("<JS WeakMap>");
1094 case JS_WEAK_SET_TYPE: {
1095 accumulator->Add("<JS WeakSet>");
1098 case JS_REGEXP_TYPE: {
1099 accumulator->Add("<JS RegExp>");
1102 case JS_FUNCTION_TYPE: {
1103 JSFunction* function = JSFunction::cast(this);
1104 Object* fun_name = function->shared()->DebugName();
1105 bool printed = false;
1106 if (fun_name->IsString()) {
1107 String* str = String::cast(fun_name);
1108 if (str->length() > 0) {
1109 accumulator->Add("<JS Function ");
1110 accumulator->Put(str);
1115 accumulator->Add("<JS Function");
1117 accumulator->Add(" (SharedFunctionInfo %p)",
1118 reinterpret_cast<void*>(function->shared()));
1119 accumulator->Put('>');
1122 case JS_GENERATOR_OBJECT_TYPE: {
1123 accumulator->Add("<JS Generator>");
1126 case JS_MODULE_TYPE: {
1127 accumulator->Add("<JS Module>");
1130 // All other JSObjects are rather similar to each other (JSObject,
1131 // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue).
1133 Map* map_of_this = map();
1134 Heap* heap = GetHeap();
1135 Object* constructor = map_of_this->GetConstructor();
1136 bool printed = false;
1137 if (constructor->IsHeapObject() &&
1138 !heap->Contains(HeapObject::cast(constructor))) {
1139 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
1141 bool global_object = IsJSGlobalProxy();
1142 if (constructor->IsJSFunction()) {
1143 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
1144 accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!");
1146 Object* constructor_name =
1147 JSFunction::cast(constructor)->shared()->name();
1148 if (constructor_name->IsString()) {
1149 String* str = String::cast(constructor_name);
1150 if (str->length() > 0) {
1151 bool vowel = AnWord(str);
1152 accumulator->Add("<%sa%s ",
1153 global_object ? "Global Object: " : "",
1155 accumulator->Put(str);
1156 accumulator->Add(" with %smap %p",
1157 map_of_this->is_deprecated() ? "deprecated " : "",
1165 accumulator->Add("<JS %sObject", global_object ? "Global " : "");
1169 accumulator->Add(" value = ");
1170 JSValue::cast(this)->value()->ShortPrint(accumulator);
1172 accumulator->Put('>');
1179 void JSObject::PrintElementsTransition(
1180 FILE* file, Handle<JSObject> object,
1181 ElementsKind from_kind, Handle<FixedArrayBase> from_elements,
1182 ElementsKind to_kind, Handle<FixedArrayBase> to_elements) {
1183 if (from_kind != to_kind) {
1185 os << "elements transition [" << ElementsKindToString(from_kind) << " -> "
1186 << ElementsKindToString(to_kind) << "] in ";
1187 JavaScriptFrame::PrintTop(object->GetIsolate(), file, false, true);
1188 PrintF(file, " for ");
1189 object->ShortPrint(file);
1190 PrintF(file, " from ");
1191 from_elements->ShortPrint(file);
1192 PrintF(file, " to ");
1193 to_elements->ShortPrint(file);
1199 void Map::PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind,
1200 PropertyAttributes attributes) {
1202 os << "[reconfiguring ";
1203 constructor_name()->PrintOn(file);
1205 Name* name = instance_descriptors()->GetKey(modify_index);
1206 if (name->IsString()) {
1207 String::cast(name)->PrintOn(file);
1209 os << "{symbol " << static_cast<void*>(name) << "}";
1211 os << ": " << (kind == kData ? "kData" : "ACCESSORS") << ", attrs: ";
1212 os << attributes << " [";
1213 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1218 void Map::PrintGeneralization(FILE* file,
1223 bool constant_to_field,
1224 Representation old_representation,
1225 Representation new_representation,
1226 HeapType* old_field_type,
1227 HeapType* new_field_type) {
1229 os << "[generalizing ";
1230 constructor_name()->PrintOn(file);
1232 Name* name = instance_descriptors()->GetKey(modify_index);
1233 if (name->IsString()) {
1234 String::cast(name)->PrintOn(file);
1236 os << "{symbol " << static_cast<void*>(name) << "}";
1239 if (constant_to_field) {
1242 os << old_representation.Mnemonic() << "{";
1243 old_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1246 os << "->" << new_representation.Mnemonic() << "{";
1247 new_field_type->PrintTo(os, HeapType::SEMANTIC_DIM);
1249 if (strlen(reason) > 0) {
1252 os << "+" << (descriptors - split) << " maps";
1255 JavaScriptFrame::PrintTop(GetIsolate(), file, false, true);
1260 void JSObject::PrintInstanceMigration(FILE* file,
1263 PrintF(file, "[migrating ");
1264 map()->constructor_name()->PrintOn(file);
1266 DescriptorArray* o = original_map->instance_descriptors();
1267 DescriptorArray* n = new_map->instance_descriptors();
1268 for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) {
1269 Representation o_r = o->GetDetails(i).representation();
1270 Representation n_r = n->GetDetails(i).representation();
1271 if (!o_r.Equals(n_r)) {
1272 String::cast(o->GetKey(i))->PrintOn(file);
1273 PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic());
1274 } else if (o->GetDetails(i).type() == DATA_CONSTANT &&
1275 n->GetDetails(i).type() == DATA) {
1276 Name* name = o->GetKey(i);
1277 if (name->IsString()) {
1278 String::cast(name)->PrintOn(file);
1280 PrintF(file, "{symbol %p}", static_cast<void*>(name));
1289 void HeapObject::HeapObjectShortPrint(std::ostream& os) { // NOLINT
1290 Heap* heap = GetHeap();
1291 if (!heap->Contains(this)) {
1292 os << "!!!INVALID POINTER!!!";
1295 if (!heap->Contains(map())) {
1296 os << "!!!INVALID MAP!!!";
1303 HeapStringAllocator allocator;
1304 StringStream accumulator(&allocator);
1305 String::cast(this)->StringShortPrint(&accumulator);
1306 os << accumulator.ToCString().get();
1310 HeapStringAllocator allocator;
1311 StringStream accumulator(&allocator);
1312 JSObject::cast(this)->JSObjectShortPrint(&accumulator);
1313 os << accumulator.ToCString().get();
1316 switch (map()->instance_type()) {
1318 os << "<Map(" << ElementsKindToString(Map::cast(this)->elements_kind())
1321 case FIXED_ARRAY_TYPE:
1322 os << "<FixedArray[" << FixedArray::cast(this)->length() << "]>";
1324 case FIXED_DOUBLE_ARRAY_TYPE:
1325 os << "<FixedDoubleArray[" << FixedDoubleArray::cast(this)->length()
1328 case BYTE_ARRAY_TYPE:
1329 os << "<ByteArray[" << ByteArray::cast(this)->length() << "]>";
1331 case BYTECODE_ARRAY_TYPE:
1332 os << "<BytecodeArray[" << BytecodeArray::cast(this)->length() << "]>";
1334 case FREE_SPACE_TYPE:
1335 os << "<FreeSpace[" << FreeSpace::cast(this)->Size() << "]>";
1337 #define TYPED_ARRAY_SHORT_PRINT(Type, type, TYPE, ctype, size) \
1338 case FIXED_##TYPE##_ARRAY_TYPE: \
1339 os << "<Fixed" #Type "Array[" << Fixed##Type##Array::cast(this)->length() \
1343 TYPED_ARRAYS(TYPED_ARRAY_SHORT_PRINT)
1344 #undef TYPED_ARRAY_SHORT_PRINT
1346 case SHARED_FUNCTION_INFO_TYPE: {
1347 SharedFunctionInfo* shared = SharedFunctionInfo::cast(this);
1348 base::SmartArrayPointer<char> debug_name =
1349 shared->DebugName()->ToCString();
1350 if (debug_name[0] != 0) {
1351 os << "<SharedFunctionInfo " << debug_name.get() << ">";
1353 os << "<SharedFunctionInfo>";
1357 case JS_MESSAGE_OBJECT_TYPE:
1358 os << "<JSMessageObject>";
1360 #define MAKE_STRUCT_CASE(NAME, Name, name) \
1362 os << "<" #Name ">"; \
1364 STRUCT_LIST(MAKE_STRUCT_CASE)
1365 #undef MAKE_STRUCT_CASE
1367 Code* code = Code::cast(this);
1368 os << "<Code: " << Code::Kind2String(code->kind()) << ">";
1371 case ODDBALL_TYPE: {
1372 if (IsUndefined()) {
1373 os << "<undefined>";
1374 } else if (IsTheHole()) {
1376 } else if (IsNull()) {
1378 } else if (IsTrue()) {
1380 } else if (IsFalse()) {
1383 os << "<Odd Oddball>";
1388 Symbol* symbol = Symbol::cast(this);
1389 symbol->SymbolShortPrint(os);
1392 case HEAP_NUMBER_TYPE: {
1394 HeapNumber::cast(this)->HeapNumberPrint(os);
1398 case MUTABLE_HEAP_NUMBER_TYPE: {
1399 os << "<MutableNumber: ";
1400 HeapNumber::cast(this)->HeapNumberPrint(os);
1404 case SIMD128_VALUE_TYPE: {
1405 #define SIMD128_TYPE(TYPE, Type, type, lane_count, lane_type) \
1407 os << "<" #Type ">"; \
1410 SIMD128_TYPES(SIMD128_TYPE)
1418 case JS_FUNCTION_PROXY_TYPE:
1419 os << "<JSFunctionProxy>";
1426 HeapStringAllocator allocator;
1427 StringStream accumulator(&allocator);
1428 Cell::cast(this)->value()->ShortPrint(&accumulator);
1429 os << accumulator.ToCString().get();
1432 case PROPERTY_CELL_TYPE: {
1433 os << "PropertyCell for ";
1434 HeapStringAllocator allocator;
1435 StringStream accumulator(&allocator);
1436 PropertyCell* cell = PropertyCell::cast(this);
1437 cell->value()->ShortPrint(&accumulator);
1438 os << accumulator.ToCString().get() << " " << cell->property_details();
1441 case WEAK_CELL_TYPE: {
1442 os << "WeakCell for ";
1443 HeapStringAllocator allocator;
1444 StringStream accumulator(&allocator);
1445 WeakCell::cast(this)->value()->ShortPrint(&accumulator);
1446 os << accumulator.ToCString().get();
1450 os << "<Other heap object (" << map()->instance_type() << ")>";
1456 void HeapObject::Iterate(ObjectVisitor* v) {
1458 IteratePointer(v, kMapOffset);
1459 // Handle object body
1461 IterateBody(m->instance_type(), SizeFromMap(m), v);
1465 bool HeapNumber::HeapNumberBooleanValue() {
1466 return DoubleToBoolean(value());
1470 void HeapNumber::HeapNumberPrint(std::ostream& os) { // NOLINT
1475 #define FIELD_ADDR_CONST(p, offset) \
1476 (reinterpret_cast<const byte*>(p) + offset - kHeapObjectTag)
1478 #define READ_INT32_FIELD(p, offset) \
1479 (*reinterpret_cast<const int32_t*>(FIELD_ADDR_CONST(p, offset)))
1481 #define READ_INT64_FIELD(p, offset) \
1482 (*reinterpret_cast<const int64_t*>(FIELD_ADDR_CONST(p, offset)))
1484 #define READ_BYTE_FIELD(p, offset) \
1485 (*reinterpret_cast<const byte*>(FIELD_ADDR_CONST(p, offset)))
1488 bool Simd128Value::BitwiseEquals(const Simd128Value* other) const {
1489 return READ_INT64_FIELD(this, kValueOffset) ==
1490 READ_INT64_FIELD(other, kValueOffset) &&
1491 READ_INT64_FIELD(this, kValueOffset + kInt64Size) ==
1492 READ_INT64_FIELD(other, kValueOffset + kInt64Size);
1496 uint32_t Simd128Value::Hash() const {
1497 uint32_t seed = v8::internal::kZeroHashSeed;
1499 hash = ComputeIntegerHash(READ_INT32_FIELD(this, kValueOffset), seed);
1500 hash = ComputeIntegerHash(
1501 READ_INT32_FIELD(this, kValueOffset + 1 * kInt32Size), hash * 31);
1502 hash = ComputeIntegerHash(
1503 READ_INT32_FIELD(this, kValueOffset + 2 * kInt32Size), hash * 31);
1504 hash = ComputeIntegerHash(
1505 READ_INT32_FIELD(this, kValueOffset + 3 * kInt32Size), hash * 31);
1510 void Simd128Value::CopyBits(void* destination) const {
1511 memcpy(destination, &READ_BYTE_FIELD(this, kValueOffset), kSimd128Size);
1515 String* JSReceiver::class_name() {
1516 if (IsJSFunction() || IsJSFunctionProxy()) {
1517 return GetHeap()->Function_string();
1519 Object* maybe_constructor = map()->GetConstructor();
1520 if (maybe_constructor->IsJSFunction()) {
1521 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1522 return String::cast(constructor->shared()->instance_class_name());
1524 // If the constructor is not present, return "Object".
1525 return GetHeap()->Object_string();
1529 String* Map::constructor_name() {
1530 if (is_prototype_map() && prototype_info()->IsPrototypeInfo()) {
1531 PrototypeInfo* proto_info = PrototypeInfo::cast(prototype_info());
1532 if (proto_info->constructor_name()->IsString()) {
1533 return String::cast(proto_info->constructor_name());
1536 Object* maybe_constructor = GetConstructor();
1537 if (maybe_constructor->IsJSFunction()) {
1538 JSFunction* constructor = JSFunction::cast(maybe_constructor);
1539 String* name = String::cast(constructor->shared()->name());
1540 if (name->length() > 0) return name;
1541 String* inferred_name = constructor->shared()->inferred_name();
1542 if (inferred_name->length() > 0) return inferred_name;
1543 Object* proto = prototype();
1544 if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name();
1546 // TODO(rossberg): what about proxies?
1547 // If the constructor is not present, return "Object".
1548 return GetHeap()->Object_string();
1552 String* JSReceiver::constructor_name() {
1553 return map()->constructor_name();
1557 static Handle<Object> WrapType(Handle<HeapType> type) {
1558 if (type->IsClass()) return Map::WeakCellForMap(type->AsClass()->Map());
1563 MaybeHandle<Map> Map::CopyWithField(Handle<Map> map,
1565 Handle<HeapType> type,
1566 PropertyAttributes attributes,
1567 Representation representation,
1568 TransitionFlag flag) {
1569 DCHECK(DescriptorArray::kNotFound ==
1570 map->instance_descriptors()->Search(
1571 *name, map->NumberOfOwnDescriptors()));
1573 // Ensure the descriptor array does not get too big.
1574 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1575 return MaybeHandle<Map>();
1578 Isolate* isolate = map->GetIsolate();
1580 // Compute the new index for new field.
1581 int index = map->NextFreePropertyIndex();
1583 if (map->instance_type() == JS_CONTEXT_EXTENSION_OBJECT_TYPE) {
1584 representation = Representation::Tagged();
1585 type = HeapType::Any(isolate);
1588 Handle<Object> wrapped_type(WrapType(type));
1590 DataDescriptor new_field_desc(name, index, wrapped_type, attributes,
1592 Handle<Map> new_map = Map::CopyAddDescriptor(map, &new_field_desc, flag);
1593 int unused_property_fields = new_map->unused_property_fields() - 1;
1594 if (unused_property_fields < 0) {
1595 unused_property_fields += JSObject::kFieldsAdded;
1597 new_map->set_unused_property_fields(unused_property_fields);
1602 MaybeHandle<Map> Map::CopyWithConstant(Handle<Map> map,
1604 Handle<Object> constant,
1605 PropertyAttributes attributes,
1606 TransitionFlag flag) {
1607 // Ensure the descriptor array does not get too big.
1608 if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors) {
1609 return MaybeHandle<Map>();
1612 // Allocate new instance descriptors with (name, constant) added.
1613 DataConstantDescriptor new_constant_desc(name, constant, attributes);
1614 return Map::CopyAddDescriptor(map, &new_constant_desc, flag);
1618 void JSObject::AddSlowProperty(Handle<JSObject> object,
1620 Handle<Object> value,
1621 PropertyAttributes attributes) {
1622 DCHECK(!object->HasFastProperties());
1623 Isolate* isolate = object->GetIsolate();
1624 if (object->IsGlobalObject()) {
1625 Handle<GlobalDictionary> dict(object->global_dictionary());
1626 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1627 int entry = dict->FindEntry(name);
1628 // If there's a cell there, just invalidate and set the property.
1629 if (entry != GlobalDictionary::kNotFound) {
1630 PropertyCell::UpdateCell(dict, entry, value, details);
1631 // TODO(ishell): move this to UpdateCell.
1632 // Need to adjust the details.
1633 int index = dict->NextEnumerationIndex();
1634 dict->SetNextEnumerationIndex(index + 1);
1635 PropertyCell* cell = PropertyCell::cast(dict->ValueAt(entry));
1636 details = cell->property_details().set_index(index);
1637 cell->set_property_details(details);
1640 auto cell = isolate->factory()->NewPropertyCell();
1641 cell->set_value(*value);
1642 auto cell_type = value->IsUndefined() ? PropertyCellType::kUndefined
1643 : PropertyCellType::kConstant;
1644 details = details.set_cell_type(cell_type);
1647 Handle<GlobalDictionary> result =
1648 GlobalDictionary::Add(dict, name, value, details);
1649 if (*dict != *result) object->set_properties(*result);
1652 Handle<NameDictionary> dict(object->property_dictionary());
1653 PropertyDetails details(attributes, DATA, 0, PropertyCellType::kNoCell);
1654 Handle<NameDictionary> result =
1655 NameDictionary::Add(dict, name, value, details);
1656 if (*dict != *result) object->set_properties(*result);
1661 Context* JSObject::GetCreationContext() {
1662 Object* constructor = this->map()->GetConstructor();
1663 JSFunction* function;
1664 if (!constructor->IsJSFunction()) {
1665 // Functions have null as a constructor,
1666 // but any JSFunction knows its context immediately.
1667 function = JSFunction::cast(this);
1669 function = JSFunction::cast(constructor);
1672 return function->context()->native_context();
1676 MaybeHandle<Object> JSObject::EnqueueChangeRecord(Handle<JSObject> object,
1677 const char* type_str,
1679 Handle<Object> old_value) {
1680 DCHECK(!object->IsJSGlobalProxy());
1681 DCHECK(!object->IsJSGlobalObject());
1682 Isolate* isolate = object->GetIsolate();
1683 HandleScope scope(isolate);
1684 Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str);
1685 Handle<Object> args[] = { type, object, name, old_value };
1686 int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4;
1688 return Execution::Call(isolate,
1689 Handle<JSFunction>(isolate->observers_notify_change()),
1690 isolate->factory()->undefined_value(), argc, args);
1694 const char* Representation::Mnemonic() const {
1696 case kNone: return "v";
1697 case kTagged: return "t";
1698 case kSmi: return "s";
1699 case kDouble: return "d";
1700 case kInteger32: return "i";
1701 case kHeapObject: return "h";
1702 case kExternal: return "x";
1710 bool Map::InstancesNeedRewriting(Map* target, int target_number_of_fields,
1711 int target_inobject, int target_unused,
1712 int* old_number_of_fields) {
1713 // If fields were added (or removed), rewrite the instance.
1714 *old_number_of_fields = NumberOfFields();
1715 DCHECK(target_number_of_fields >= *old_number_of_fields);
1716 if (target_number_of_fields != *old_number_of_fields) return true;
1718 // If smi descriptors were replaced by double descriptors, rewrite.
1719 DescriptorArray* old_desc = instance_descriptors();
1720 DescriptorArray* new_desc = target->instance_descriptors();
1721 int limit = NumberOfOwnDescriptors();
1722 for (int i = 0; i < limit; i++) {
1723 if (new_desc->GetDetails(i).representation().IsDouble() !=
1724 old_desc->GetDetails(i).representation().IsDouble()) {
1729 // If no fields were added, and no inobject properties were removed, setting
1730 // the map is sufficient.
1731 if (target_inobject == GetInObjectProperties()) return false;
1732 // In-object slack tracking may have reduced the object size of the new map.
1733 // In that case, succeed if all existing fields were inobject, and they still
1734 // fit within the new inobject size.
1735 DCHECK(target_inobject < GetInObjectProperties());
1736 if (target_number_of_fields <= target_inobject) {
1737 DCHECK(target_number_of_fields + target_unused == target_inobject);
1740 // Otherwise, properties will need to be moved to the backing store.
1745 static void UpdatePrototypeUserRegistration(Handle<Map> old_map,
1746 Handle<Map> new_map,
1748 if (!FLAG_track_prototype_users) return;
1749 if (!old_map->is_prototype_map()) return;
1750 DCHECK(new_map->is_prototype_map());
1751 bool was_registered = JSObject::UnregisterPrototypeUser(old_map, isolate);
1752 new_map->set_prototype_info(old_map->prototype_info());
1753 old_map->set_prototype_info(Smi::FromInt(0));
1754 if (FLAG_trace_prototype_users) {
1755 PrintF("Moving prototype_info %p from map %p to map %p.\n",
1756 reinterpret_cast<void*>(new_map->prototype_info()),
1757 reinterpret_cast<void*>(*old_map),
1758 reinterpret_cast<void*>(*new_map));
1760 if (was_registered) {
1761 if (new_map->prototype_info()->IsPrototypeInfo()) {
1762 // The new map isn't registered with its prototype yet; reflect this fact
1763 // in the PrototypeInfo it just inherited from the old map.
1764 PrototypeInfo::cast(new_map->prototype_info())
1765 ->set_registry_slot(PrototypeInfo::UNREGISTERED);
1767 JSObject::LazyRegisterPrototypeUser(new_map, isolate);
1772 void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map,
1773 int expected_additional_properties) {
1774 if (object->map() == *new_map) return;
1775 // If this object is a prototype (the callee will check), invalidate any
1776 // prototype chains involving it.
1777 InvalidatePrototypeChains(object->map());
1778 Handle<Map> old_map(object->map());
1780 // If the map was registered with its prototype before, ensure that it
1781 // registers with its new prototype now. This preserves the invariant that
1782 // when a map on a prototype chain is registered with its prototype, then
1783 // all prototypes further up the chain are also registered with their
1784 // respective prototypes.
1785 UpdatePrototypeUserRegistration(old_map, new_map, new_map->GetIsolate());
1787 if (object->HasFastProperties()) {
1788 if (!new_map->is_dictionary_map()) {
1789 MigrateFastToFast(object, new_map);
1790 if (old_map->is_prototype_map()) {
1791 DCHECK(!old_map->is_stable());
1792 DCHECK(new_map->is_stable());
1793 // Clear out the old descriptor array to avoid problems to sharing
1794 // the descriptor array without using an explicit.
1795 old_map->InitializeDescriptors(
1796 old_map->GetHeap()->empty_descriptor_array(),
1797 LayoutDescriptor::FastPointerLayout());
1798 // Ensure that no transition was inserted for prototype migrations.
1799 DCHECK_EQ(0, TransitionArray::NumberOfTransitions(
1800 old_map->raw_transitions()));
1801 DCHECK(new_map->GetBackPointer()->IsUndefined());
1804 MigrateFastToSlow(object, new_map, expected_additional_properties);
1807 // For slow-to-fast migrations JSObject::MigrateSlowToFast()
1808 // must be used instead.
1809 CHECK(new_map->is_dictionary_map());
1811 // Slow-to-slow migration is trivial.
1812 object->set_map(*new_map);
1815 // Careful: Don't allocate here!
1816 // For some callers of this method, |object| might be in an inconsistent
1817 // state now: the new map might have a new elements_kind, but the object's
1818 // elements pointer hasn't been updated yet. Callers will fix this, but in
1819 // the meantime, (indirectly) calling JSObjectVerify() must be avoided.
1820 // When adding code here, add a DisallowHeapAllocation too.
1824 // To migrate a fast instance to a fast map:
1825 // - First check whether the instance needs to be rewritten. If not, simply
1827 // - Otherwise, allocate a fixed array large enough to hold all fields, in
1828 // addition to unused space.
1829 // - Copy all existing properties in, in the following order: backing store
1830 // properties, unused fields, inobject properties.
1831 // - If all allocation succeeded, commit the state atomically:
1832 // * Copy inobject properties from the backing store back into the object.
1833 // * Trim the difference in instance size of the object. This also cleanly
1834 // frees inobject properties that moved to the backing store.
1835 // * If there are properties left in the backing store, trim of the space used
1836 // to temporarily store the inobject properties.
1837 // * If there are properties left in the backing store, install the backing
1839 void JSObject::MigrateFastToFast(Handle<JSObject> object, Handle<Map> new_map) {
1840 Isolate* isolate = object->GetIsolate();
1841 Handle<Map> old_map(object->map());
1842 int old_number_of_fields;
1843 int number_of_fields = new_map->NumberOfFields();
1844 int inobject = new_map->GetInObjectProperties();
1845 int unused = new_map->unused_property_fields();
1847 // Nothing to do if no functions were converted to fields and no smis were
1848 // converted to doubles.
1849 if (!old_map->InstancesNeedRewriting(*new_map, number_of_fields, inobject,
1850 unused, &old_number_of_fields)) {
1851 object->synchronized_set_map(*new_map);
1855 int total_size = number_of_fields + unused;
1856 int external = total_size - inobject;
1858 if (number_of_fields != old_number_of_fields &&
1859 new_map->GetBackPointer() == *old_map) {
1860 PropertyDetails details = new_map->GetLastDescriptorDetails();
1862 if (old_map->unused_property_fields() > 0) {
1863 if (details.representation().IsDouble()) {
1865 FieldIndex::ForDescriptor(*new_map, new_map->LastAdded());
1866 if (new_map->IsUnboxedDoubleField(index)) {
1867 object->RawFastDoublePropertyAtPut(index, 0);
1869 Handle<Object> value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1870 object->RawFastPropertyAtPut(index, *value);
1873 object->synchronized_set_map(*new_map);
1877 DCHECK(number_of_fields == old_number_of_fields + 1);
1878 // This migration is a transition from a map that has run out of property
1879 // space. Therefore it could be done by extending the backing store.
1880 int grow_by = external - object->properties()->length();
1881 Handle<FixedArray> old_storage = handle(object->properties(), isolate);
1882 Handle<FixedArray> new_storage =
1883 isolate->factory()->CopyFixedArrayAndGrow(old_storage, grow_by);
1885 // Properly initialize newly added property.
1886 Handle<Object> value;
1887 if (details.representation().IsDouble()) {
1888 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1890 value = isolate->factory()->uninitialized_value();
1892 DCHECK(details.type() == DATA);
1893 int target_index = details.field_index() - inobject;
1894 DCHECK(target_index >= 0); // Must be a backing store index.
1895 new_storage->set(target_index, *value);
1897 // From here on we cannot fail and we shouldn't GC anymore.
1898 DisallowHeapAllocation no_allocation;
1900 // Set the new property value and do the map transition.
1901 object->set_properties(*new_storage);
1902 object->synchronized_set_map(*new_map);
1905 Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size);
1907 Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors());
1908 Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors());
1909 int old_nof = old_map->NumberOfOwnDescriptors();
1910 int new_nof = new_map->NumberOfOwnDescriptors();
1912 // This method only supports generalizing instances to at least the same
1913 // number of properties.
1914 DCHECK(old_nof <= new_nof);
1916 for (int i = 0; i < old_nof; i++) {
1917 PropertyDetails details = new_descriptors->GetDetails(i);
1918 if (details.type() != DATA) continue;
1919 PropertyDetails old_details = old_descriptors->GetDetails(i);
1920 Representation old_representation = old_details.representation();
1921 Representation representation = details.representation();
1922 Handle<Object> value;
1923 if (old_details.type() == ACCESSOR_CONSTANT) {
1924 // In case of kAccessor -> kData property reconfiguration, the property
1925 // must already be prepared for data or certain type.
1926 DCHECK(!details.representation().IsNone());
1927 if (details.representation().IsDouble()) {
1928 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1930 value = isolate->factory()->uninitialized_value();
1932 } else if (old_details.type() == DATA_CONSTANT) {
1933 value = handle(old_descriptors->GetValue(i), isolate);
1934 DCHECK(!old_representation.IsDouble() && !representation.IsDouble());
1936 FieldIndex index = FieldIndex::ForDescriptor(*old_map, i);
1937 if (object->IsUnboxedDoubleField(index)) {
1938 double old = object->RawFastDoublePropertyAt(index);
1939 value = isolate->factory()->NewHeapNumber(
1940 old, representation.IsDouble() ? MUTABLE : IMMUTABLE);
1943 value = handle(object->RawFastPropertyAt(index), isolate);
1944 if (!old_representation.IsDouble() && representation.IsDouble()) {
1945 if (old_representation.IsNone()) {
1946 value = handle(Smi::FromInt(0), isolate);
1948 value = Object::NewStorageFor(isolate, value, representation);
1949 } else if (old_representation.IsDouble() &&
1950 !representation.IsDouble()) {
1951 value = Object::WrapForRead(isolate, value, old_representation);
1955 DCHECK(!(representation.IsDouble() && value->IsSmi()));
1956 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
1957 if (target_index < 0) target_index += total_size;
1958 array->set(target_index, *value);
1961 for (int i = old_nof; i < new_nof; i++) {
1962 PropertyDetails details = new_descriptors->GetDetails(i);
1963 if (details.type() != DATA) continue;
1964 Handle<Object> value;
1965 if (details.representation().IsDouble()) {
1966 value = isolate->factory()->NewHeapNumber(0, MUTABLE);
1968 value = isolate->factory()->uninitialized_value();
1970 int target_index = new_descriptors->GetFieldIndex(i) - inobject;
1971 if (target_index < 0) target_index += total_size;
1972 array->set(target_index, *value);
1975 // From here on we cannot fail and we shouldn't GC anymore.
1976 DisallowHeapAllocation no_allocation;
1978 // Copy (real) inobject properties. If necessary, stop at number_of_fields to
1979 // avoid overwriting |one_pointer_filler_map|.
1980 int limit = Min(inobject, number_of_fields);
1981 for (int i = 0; i < limit; i++) {
1982 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
1983 Object* value = array->get(external + i);
1984 // Can't use JSObject::FastPropertyAtPut() because proper map was not set
1986 if (new_map->IsUnboxedDoubleField(index)) {
1987 DCHECK(value->IsMutableHeapNumber());
1988 object->RawFastDoublePropertyAtPut(index,
1989 HeapNumber::cast(value)->value());
1991 object->RawFastPropertyAtPut(index, value);
1995 Heap* heap = isolate->heap();
1997 // If there are properties in the new backing store, trim it to the correct
1998 // size and install the backing store into the object.
2000 heap->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(*array, inobject);
2001 object->set_properties(*array);
2004 // Create filler object past the new instance size.
2005 int new_instance_size = new_map->instance_size();
2006 int instance_size_delta = old_map->instance_size() - new_instance_size;
2007 DCHECK(instance_size_delta >= 0);
2009 if (instance_size_delta > 0) {
2010 Address address = object->address();
2011 heap->CreateFillerObjectAt(
2012 address + new_instance_size, instance_size_delta);
2013 heap->AdjustLiveBytes(*object, -instance_size_delta,
2014 Heap::CONCURRENT_TO_SWEEPER);
2017 // We are storing the new map using release store after creating a filler for
2018 // the left-over space to avoid races with the sweeper thread.
2019 object->synchronized_set_map(*new_map);
2023 int Map::NumberOfFields() {
2024 DescriptorArray* descriptors = instance_descriptors();
2026 for (int i = 0; i < NumberOfOwnDescriptors(); i++) {
2027 if (descriptors->GetDetails(i).location() == kField) result++;
2033 Handle<Map> Map::CopyGeneralizeAllRepresentations(
2034 Handle<Map> map, int modify_index, StoreMode store_mode, PropertyKind kind,
2035 PropertyAttributes attributes, const char* reason) {
2036 Isolate* isolate = map->GetIsolate();
2037 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2038 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
2039 Handle<DescriptorArray> descriptors =
2040 DescriptorArray::CopyUpTo(old_descriptors, number_of_own_descriptors);
2042 for (int i = 0; i < number_of_own_descriptors; i++) {
2043 descriptors->SetRepresentation(i, Representation::Tagged());
2044 if (descriptors->GetDetails(i).type() == DATA) {
2045 descriptors->SetValue(i, HeapType::Any());
2049 Handle<LayoutDescriptor> new_layout_descriptor(
2050 LayoutDescriptor::FastPointerLayout(), isolate);
2051 Handle<Map> new_map = CopyReplaceDescriptors(
2052 map, descriptors, new_layout_descriptor, OMIT_TRANSITION,
2053 MaybeHandle<Name>(), reason, SPECIAL_TRANSITION);
2055 // Unless the instance is being migrated, ensure that modify_index is a field.
2056 if (modify_index >= 0) {
2057 PropertyDetails details = descriptors->GetDetails(modify_index);
2058 if (store_mode == FORCE_FIELD &&
2059 (details.type() != DATA || details.attributes() != attributes)) {
2060 int field_index = details.type() == DATA ? details.field_index()
2061 : new_map->NumberOfFields();
2062 DataDescriptor d(handle(descriptors->GetKey(modify_index), isolate),
2063 field_index, attributes, Representation::Tagged());
2064 descriptors->Replace(modify_index, &d);
2065 if (details.type() != DATA) {
2066 int unused_property_fields = new_map->unused_property_fields() - 1;
2067 if (unused_property_fields < 0) {
2068 unused_property_fields += JSObject::kFieldsAdded;
2070 new_map->set_unused_property_fields(unused_property_fields);
2073 DCHECK(details.attributes() == attributes);
2076 if (FLAG_trace_generalization) {
2077 HeapType* field_type =
2078 (details.type() == DATA)
2079 ? map->instance_descriptors()->GetFieldType(modify_index)
2081 map->PrintGeneralization(
2082 stdout, reason, modify_index, new_map->NumberOfOwnDescriptors(),
2083 new_map->NumberOfOwnDescriptors(),
2084 details.type() == DATA_CONSTANT && store_mode == FORCE_FIELD,
2085 details.representation(), Representation::Tagged(), field_type,
2093 void Map::DeprecateTransitionTree() {
2094 if (is_deprecated()) return;
2095 Object* transitions = raw_transitions();
2096 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2097 for (int i = 0; i < num_transitions; ++i) {
2098 TransitionArray::GetTarget(transitions, i)->DeprecateTransitionTree();
2101 dependent_code()->DeoptimizeDependentCodeGroup(
2102 GetIsolate(), DependentCode::kTransitionGroup);
2103 NotifyLeafMapLayoutChange();
2107 static inline bool EqualImmutableValues(Object* obj1, Object* obj2) {
2108 if (obj1 == obj2) return true; // Valid for both kData and kAccessor kinds.
2109 // TODO(ishell): compare AccessorPairs.
2114 // Invalidates a transition target at |key|, and installs |new_descriptors| over
2115 // the current instance_descriptors to ensure proper sharing of descriptor
2117 // Returns true if the transition target at given key was deprecated.
2118 bool Map::DeprecateTarget(PropertyKind kind, Name* key,
2119 PropertyAttributes attributes,
2120 DescriptorArray* new_descriptors,
2121 LayoutDescriptor* new_layout_descriptor) {
2122 bool transition_target_deprecated = false;
2123 Map* maybe_transition =
2124 TransitionArray::SearchTransition(this, kind, key, attributes);
2125 if (maybe_transition != NULL) {
2126 maybe_transition->DeprecateTransitionTree();
2127 transition_target_deprecated = true;
2130 // Don't overwrite the empty descriptor array.
2131 if (NumberOfOwnDescriptors() == 0) return transition_target_deprecated;
2133 DescriptorArray* to_replace = instance_descriptors();
2134 Map* current = this;
2135 GetHeap()->incremental_marking()->RecordWrites(to_replace);
2136 while (current->instance_descriptors() == to_replace) {
2137 current->SetEnumLength(kInvalidEnumCacheSentinel);
2138 current->UpdateDescriptors(new_descriptors, new_layout_descriptor);
2139 Object* next = current->GetBackPointer();
2140 if (next->IsUndefined()) break;
2141 current = Map::cast(next);
2144 set_owns_descriptors(false);
2145 return transition_target_deprecated;
2149 Map* Map::FindRootMap() {
2152 Object* back = result->GetBackPointer();
2153 if (back->IsUndefined()) return result;
2154 result = Map::cast(back);
2159 Map* Map::FindLastMatchMap(int verbatim,
2161 DescriptorArray* descriptors) {
2162 DisallowHeapAllocation no_allocation;
2164 // This can only be called on roots of transition trees.
2165 DCHECK_EQ(verbatim, NumberOfOwnDescriptors());
2167 Map* current = this;
2169 for (int i = verbatim; i < length; i++) {
2170 Name* name = descriptors->GetKey(i);
2171 PropertyDetails details = descriptors->GetDetails(i);
2172 Map* next = TransitionArray::SearchTransition(current, details.kind(), name,
2173 details.attributes());
2174 if (next == NULL) break;
2175 DescriptorArray* next_descriptors = next->instance_descriptors();
2177 PropertyDetails next_details = next_descriptors->GetDetails(i);
2178 DCHECK_EQ(details.kind(), next_details.kind());
2179 DCHECK_EQ(details.attributes(), next_details.attributes());
2180 if (details.location() != next_details.location()) break;
2181 if (!details.representation().Equals(next_details.representation())) break;
2183 if (next_details.location() == kField) {
2184 HeapType* next_field_type = next_descriptors->GetFieldType(i);
2185 if (!descriptors->GetFieldType(i)->NowIs(next_field_type)) {
2189 if (!EqualImmutableValues(descriptors->GetValue(i),
2190 next_descriptors->GetValue(i))) {
2200 Map* Map::FindFieldOwner(int descriptor) {
2201 DisallowHeapAllocation no_allocation;
2202 DCHECK_EQ(DATA, instance_descriptors()->GetDetails(descriptor).type());
2205 Object* back = result->GetBackPointer();
2206 if (back->IsUndefined()) break;
2207 Map* parent = Map::cast(back);
2208 if (parent->NumberOfOwnDescriptors() <= descriptor) break;
2215 void Map::UpdateFieldType(int descriptor, Handle<Name> name,
2216 Representation new_representation,
2217 Handle<Object> new_wrapped_type) {
2218 DCHECK(new_wrapped_type->IsSmi() || new_wrapped_type->IsWeakCell());
2219 DisallowHeapAllocation no_allocation;
2220 PropertyDetails details = instance_descriptors()->GetDetails(descriptor);
2221 if (details.type() != DATA) return;
2222 Object* transitions = raw_transitions();
2223 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
2224 for (int i = 0; i < num_transitions; ++i) {
2225 Map* target = TransitionArray::GetTarget(transitions, i);
2226 target->UpdateFieldType(descriptor, name, new_representation,
2229 // It is allowed to change representation here only from None to something.
2230 DCHECK(details.representation().Equals(new_representation) ||
2231 details.representation().IsNone());
2233 // Skip if already updated the shared descriptor.
2234 if (instance_descriptors()->GetValue(descriptor) == *new_wrapped_type) return;
2235 DataDescriptor d(name, instance_descriptors()->GetFieldIndex(descriptor),
2236 new_wrapped_type, details.attributes(), new_representation);
2237 instance_descriptors()->Replace(descriptor, &d);
2242 Handle<HeapType> Map::GeneralizeFieldType(Handle<HeapType> type1,
2243 Handle<HeapType> type2,
2245 if (type1->NowIs(type2)) return type2;
2246 if (type2->NowIs(type1)) return type1;
2247 return HeapType::Any(isolate);
2252 void Map::GeneralizeFieldType(Handle<Map> map, int modify_index,
2253 Representation new_representation,
2254 Handle<HeapType> new_field_type) {
2255 Isolate* isolate = map->GetIsolate();
2257 // Check if we actually need to generalize the field type at all.
2258 Handle<DescriptorArray> old_descriptors(map->instance_descriptors(), isolate);
2259 Representation old_representation =
2260 old_descriptors->GetDetails(modify_index).representation();
2261 Handle<HeapType> old_field_type(old_descriptors->GetFieldType(modify_index),
2264 if (old_representation.Equals(new_representation) &&
2265 new_field_type->NowIs(old_field_type)) {
2266 DCHECK(Map::GeneralizeFieldType(old_field_type,
2268 isolate)->NowIs(old_field_type));
2272 // Determine the field owner.
2273 Handle<Map> field_owner(map->FindFieldOwner(modify_index), isolate);
2274 Handle<DescriptorArray> descriptors(
2275 field_owner->instance_descriptors(), isolate);
2276 DCHECK_EQ(*old_field_type, descriptors->GetFieldType(modify_index));
2277 bool old_field_type_was_cleared =
2278 old_field_type->Is(HeapType::None()) && old_representation.IsHeapObject();
2280 // Determine the generalized new field type. Conservatively assume type Any
2281 // for cleared field types because the cleared type could have been a
2282 // deprecated map and there still could be live instances with a non-
2283 // deprecated version of the map.
2285 old_field_type_was_cleared
2286 ? HeapType::Any(isolate)
2287 : Map::GeneralizeFieldType(old_field_type, new_field_type, isolate);
2289 PropertyDetails details = descriptors->GetDetails(modify_index);
2290 Handle<Name> name(descriptors->GetKey(modify_index));
2292 Handle<Object> wrapped_type(WrapType(new_field_type));
2293 field_owner->UpdateFieldType(modify_index, name, new_representation,
2295 field_owner->dependent_code()->DeoptimizeDependentCodeGroup(
2296 isolate, DependentCode::kFieldTypeGroup);
2298 if (FLAG_trace_generalization) {
2299 map->PrintGeneralization(
2300 stdout, "field type generalization",
2301 modify_index, map->NumberOfOwnDescriptors(),
2302 map->NumberOfOwnDescriptors(), false,
2303 details.representation(), details.representation(),
2304 *old_field_type, *new_field_type);
2309 static inline Handle<HeapType> GetFieldType(Isolate* isolate,
2310 Handle<DescriptorArray> descriptors,
2312 PropertyLocation location,
2313 Representation representation) {
2315 PropertyDetails details = descriptors->GetDetails(descriptor);
2316 DCHECK_EQ(kData, details.kind());
2317 DCHECK_EQ(details.location(), location);
2319 if (location == kField) {
2320 return handle(descriptors->GetFieldType(descriptor), isolate);
2322 return descriptors->GetValue(descriptor)
2323 ->OptimalType(isolate, representation);
2328 // Reconfigures property at |modify_index| with |new_kind|, |new_attributes|,
2329 // |store_mode| and/or |new_representation|/|new_field_type|.
2330 // If |modify_index| is negative then no properties are reconfigured but the
2331 // map is migrated to the up-to-date non-deprecated state.
2333 // This method rewrites or completes the transition tree to reflect the new
2334 // change. To avoid high degrees over polymorphism, and to stabilize quickly,
2335 // on every rewrite the new type is deduced by merging the current type with
2336 // any potential new (partial) version of the type in the transition tree.
2337 // To do this, on each rewrite:
2338 // - Search the root of the transition tree using FindRootMap.
2339 // - Find |target_map|, the newest matching version of this map using the
2340 // virtually "enhanced" |old_map|'s descriptor array (i.e. whose entry at
2341 // |modify_index| is considered to be of |new_kind| and having
2342 // |new_attributes|) to walk the transition tree.
2343 // - Merge/generalize the "enhanced" descriptor array of the |old_map| and
2344 // descriptor array of the |target_map|.
2345 // - Generalize the |modify_index| descriptor using |new_representation| and
2346 // |new_field_type|.
2347 // - Walk the tree again starting from the root towards |target_map|. Stop at
2348 // |split_map|, the first map who's descriptor array does not match the merged
2349 // descriptor array.
2350 // - If |target_map| == |split_map|, |target_map| is in the expected state.
2352 // - Otherwise, invalidate the outdated transition target from |target_map|, and
2353 // replace its transition tree with a new branch for the updated descriptors.
2354 Handle<Map> Map::ReconfigureProperty(Handle<Map> old_map, int modify_index,
2355 PropertyKind new_kind,
2356 PropertyAttributes new_attributes,
2357 Representation new_representation,
2358 Handle<HeapType> new_field_type,
2359 StoreMode store_mode) {
2360 DCHECK_NE(kAccessor, new_kind); // TODO(ishell): not supported yet.
2361 DCHECK(store_mode != FORCE_FIELD || modify_index >= 0);
2362 Isolate* isolate = old_map->GetIsolate();
2364 Handle<DescriptorArray> old_descriptors(
2365 old_map->instance_descriptors(), isolate);
2366 int old_nof = old_map->NumberOfOwnDescriptors();
2368 // If it's just a representation generalization case (i.e. property kind and
2369 // attributes stays unchanged) it's fine to transition from None to anything
2370 // but double without any modification to the object, because the default
2371 // uninitialized value for representation None can be overwritten by both
2372 // smi and tagged values. Doubles, however, would require a box allocation.
2373 if (modify_index >= 0 && !new_representation.IsNone() &&
2374 !new_representation.IsDouble()) {
2375 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2376 Representation old_representation = old_details.representation();
2378 if (old_representation.IsNone()) {
2379 DCHECK_EQ(new_kind, old_details.kind());
2380 DCHECK_EQ(new_attributes, old_details.attributes());
2381 DCHECK_EQ(DATA, old_details.type());
2382 if (FLAG_trace_generalization) {
2383 old_map->PrintGeneralization(
2384 stdout, "uninitialized field", modify_index,
2385 old_map->NumberOfOwnDescriptors(),
2386 old_map->NumberOfOwnDescriptors(), false, old_representation,
2387 new_representation, old_descriptors->GetFieldType(modify_index),
2390 Handle<Map> field_owner(old_map->FindFieldOwner(modify_index), isolate);
2392 GeneralizeFieldType(field_owner, modify_index, new_representation,
2394 DCHECK(old_descriptors->GetDetails(modify_index)
2396 .Equals(new_representation));
2398 old_descriptors->GetFieldType(modify_index)->NowIs(new_field_type));
2403 // Check the state of the root map.
2404 Handle<Map> root_map(old_map->FindRootMap(), isolate);
2405 if (!old_map->EquivalentToForTransition(*root_map)) {
2406 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2407 new_kind, new_attributes,
2408 "GenAll_NotEquivalent");
2411 ElementsKind from_kind = root_map->elements_kind();
2412 ElementsKind to_kind = old_map->elements_kind();
2413 // TODO(ishell): Add a test for SLOW_SLOPPY_ARGUMENTS_ELEMENTS.
2414 if (from_kind != to_kind && to_kind != DICTIONARY_ELEMENTS &&
2415 to_kind != SLOW_SLOPPY_ARGUMENTS_ELEMENTS &&
2416 !(IsTransitionableFastElementsKind(from_kind) &&
2417 IsMoreGeneralElementsKindTransition(from_kind, to_kind))) {
2418 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2419 new_kind, new_attributes,
2420 "GenAll_InvalidElementsTransition");
2422 int root_nof = root_map->NumberOfOwnDescriptors();
2423 if (modify_index >= 0 && modify_index < root_nof) {
2424 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2425 if (old_details.kind() != new_kind ||
2426 old_details.attributes() != new_attributes) {
2427 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2428 new_kind, new_attributes,
2429 "GenAll_RootModification1");
2431 if ((old_details.type() != DATA && store_mode == FORCE_FIELD) ||
2432 (old_details.type() == DATA &&
2433 (!new_field_type->NowIs(old_descriptors->GetFieldType(modify_index)) ||
2434 !new_representation.fits_into(old_details.representation())))) {
2435 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2436 new_kind, new_attributes,
2437 "GenAll_RootModification2");
2441 // From here on, use the map with correct elements kind as root map.
2442 if (from_kind != to_kind) {
2443 root_map = Map::AsElementsKind(root_map, to_kind);
2446 Handle<Map> target_map = root_map;
2447 for (int i = root_nof; i < old_nof; ++i) {
2448 PropertyDetails old_details = old_descriptors->GetDetails(i);
2449 PropertyKind next_kind;
2450 PropertyLocation next_location;
2451 PropertyAttributes next_attributes;
2452 Representation next_representation;
2453 bool property_kind_reconfiguration = false;
2455 if (modify_index == i) {
2456 DCHECK_EQ(FORCE_FIELD, store_mode);
2457 property_kind_reconfiguration = old_details.kind() != new_kind;
2459 next_kind = new_kind;
2460 next_location = kField;
2461 next_attributes = new_attributes;
2462 // If property kind is not reconfigured merge the result with
2463 // representation/field type from the old descriptor.
2464 next_representation = new_representation;
2465 if (!property_kind_reconfiguration) {
2466 next_representation =
2467 next_representation.generalize(old_details.representation());
2471 next_kind = old_details.kind();
2472 next_location = old_details.location();
2473 next_attributes = old_details.attributes();
2474 next_representation = old_details.representation();
2476 Map* transition = TransitionArray::SearchTransition(
2477 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2478 if (transition == NULL) break;
2479 Handle<Map> tmp_map(transition, isolate);
2481 Handle<DescriptorArray> tmp_descriptors = handle(
2482 tmp_map->instance_descriptors(), isolate);
2484 // Check if target map is incompatible.
2485 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2486 DCHECK_EQ(next_kind, tmp_details.kind());
2487 DCHECK_EQ(next_attributes, tmp_details.attributes());
2488 if (next_kind == kAccessor &&
2489 !EqualImmutableValues(old_descriptors->GetValue(i),
2490 tmp_descriptors->GetValue(i))) {
2491 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2492 new_kind, new_attributes,
2493 "GenAll_Incompatible");
2495 if (next_location == kField && tmp_details.location() == kDescriptor) break;
2497 Representation tmp_representation = tmp_details.representation();
2498 if (!next_representation.fits_into(tmp_representation)) break;
2500 PropertyLocation old_location = old_details.location();
2501 PropertyLocation tmp_location = tmp_details.location();
2502 if (tmp_location == kField) {
2503 if (next_kind == kData) {
2504 Handle<HeapType> next_field_type;
2505 if (modify_index == i) {
2506 next_field_type = new_field_type;
2507 if (!property_kind_reconfiguration) {
2508 Handle<HeapType> old_field_type =
2509 GetFieldType(isolate, old_descriptors, i,
2510 old_details.location(), tmp_representation);
2512 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2515 Handle<HeapType> old_field_type =
2516 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2517 tmp_representation);
2518 next_field_type = old_field_type;
2520 GeneralizeFieldType(tmp_map, i, tmp_representation, next_field_type);
2522 } else if (old_location == kField ||
2523 !EqualImmutableValues(old_descriptors->GetValue(i),
2524 tmp_descriptors->GetValue(i))) {
2527 DCHECK(!tmp_map->is_deprecated());
2528 target_map = tmp_map;
2531 // Directly change the map if the target map is more general.
2532 Handle<DescriptorArray> target_descriptors(
2533 target_map->instance_descriptors(), isolate);
2534 int target_nof = target_map->NumberOfOwnDescriptors();
2535 if (target_nof == old_nof &&
2536 (store_mode != FORCE_FIELD ||
2537 (modify_index >= 0 &&
2538 target_descriptors->GetDetails(modify_index).location() == kField))) {
2540 if (modify_index >= 0) {
2541 PropertyDetails details = target_descriptors->GetDetails(modify_index);
2542 DCHECK_EQ(new_kind, details.kind());
2543 DCHECK_EQ(new_attributes, details.attributes());
2544 DCHECK(new_representation.fits_into(details.representation()));
2545 DCHECK(details.location() != kField ||
2546 new_field_type->NowIs(
2547 target_descriptors->GetFieldType(modify_index)));
2550 if (*target_map != *old_map) {
2551 old_map->NotifyLeafMapLayoutChange();
2556 // Find the last compatible target map in the transition tree.
2557 for (int i = target_nof; i < old_nof; ++i) {
2558 PropertyDetails old_details = old_descriptors->GetDetails(i);
2559 PropertyKind next_kind;
2560 PropertyAttributes next_attributes;
2561 if (modify_index == i) {
2562 next_kind = new_kind;
2563 next_attributes = new_attributes;
2565 next_kind = old_details.kind();
2566 next_attributes = old_details.attributes();
2568 Map* transition = TransitionArray::SearchTransition(
2569 *target_map, next_kind, old_descriptors->GetKey(i), next_attributes);
2570 if (transition == NULL) break;
2571 Handle<Map> tmp_map(transition, isolate);
2572 Handle<DescriptorArray> tmp_descriptors(
2573 tmp_map->instance_descriptors(), isolate);
2575 // Check if target map is compatible.
2577 PropertyDetails tmp_details = tmp_descriptors->GetDetails(i);
2578 DCHECK_EQ(next_kind, tmp_details.kind());
2579 DCHECK_EQ(next_attributes, tmp_details.attributes());
2581 if (next_kind == kAccessor &&
2582 !EqualImmutableValues(old_descriptors->GetValue(i),
2583 tmp_descriptors->GetValue(i))) {
2584 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2585 new_kind, new_attributes,
2586 "GenAll_Incompatible");
2588 DCHECK(!tmp_map->is_deprecated());
2589 target_map = tmp_map;
2591 target_nof = target_map->NumberOfOwnDescriptors();
2592 target_descriptors = handle(target_map->instance_descriptors(), isolate);
2594 // Allocate a new descriptor array large enough to hold the required
2595 // descriptors, with minimally the exact same size as the old descriptor
2597 int new_slack = Max(
2598 old_nof, old_descriptors->number_of_descriptors()) - old_nof;
2599 Handle<DescriptorArray> new_descriptors = DescriptorArray::Allocate(
2600 isolate, old_nof, new_slack);
2601 DCHECK(new_descriptors->length() > target_descriptors->length() ||
2602 new_descriptors->NumberOfSlackDescriptors() > 0 ||
2603 new_descriptors->number_of_descriptors() ==
2604 old_descriptors->number_of_descriptors());
2605 DCHECK(new_descriptors->number_of_descriptors() == old_nof);
2608 int current_offset = 0;
2609 for (int i = 0; i < root_nof; ++i) {
2610 PropertyDetails old_details = old_descriptors->GetDetails(i);
2611 if (old_details.location() == kField) {
2612 current_offset += old_details.field_width_in_words();
2614 Descriptor d(handle(old_descriptors->GetKey(i), isolate),
2615 handle(old_descriptors->GetValue(i), isolate),
2617 new_descriptors->Set(i, &d);
2620 // |root_nof| -> |target_nof|
2621 for (int i = root_nof; i < target_nof; ++i) {
2622 Handle<Name> target_key(target_descriptors->GetKey(i), isolate);
2623 PropertyDetails old_details = old_descriptors->GetDetails(i);
2624 PropertyDetails target_details = target_descriptors->GetDetails(i);
2626 PropertyKind next_kind;
2627 PropertyAttributes next_attributes;
2628 PropertyLocation next_location;
2629 Representation next_representation;
2630 bool property_kind_reconfiguration = false;
2632 if (modify_index == i) {
2633 DCHECK_EQ(FORCE_FIELD, store_mode);
2634 property_kind_reconfiguration = old_details.kind() != new_kind;
2636 next_kind = new_kind;
2637 next_attributes = new_attributes;
2638 next_location = kField;
2640 // Merge new representation/field type with ones from the target
2641 // descriptor. If property kind is not reconfigured merge the result with
2642 // representation/field type from the old descriptor.
2643 next_representation =
2644 new_representation.generalize(target_details.representation());
2645 if (!property_kind_reconfiguration) {
2646 next_representation =
2647 next_representation.generalize(old_details.representation());
2650 // Merge old_descriptor and target_descriptor entries.
2651 DCHECK_EQ(target_details.kind(), old_details.kind());
2652 next_kind = target_details.kind();
2653 next_attributes = target_details.attributes();
2655 old_details.location() == kField ||
2656 target_details.location() == kField ||
2657 !EqualImmutableValues(target_descriptors->GetValue(i),
2658 old_descriptors->GetValue(i))
2662 next_representation = old_details.representation().generalize(
2663 target_details.representation());
2665 DCHECK_EQ(next_kind, target_details.kind());
2666 DCHECK_EQ(next_attributes, target_details.attributes());
2668 if (next_location == kField) {
2669 if (next_kind == kData) {
2670 Handle<HeapType> target_field_type =
2671 GetFieldType(isolate, target_descriptors, i,
2672 target_details.location(), next_representation);
2674 Handle<HeapType> next_field_type;
2675 if (modify_index == i) {
2677 GeneralizeFieldType(target_field_type, new_field_type, isolate);
2678 if (!property_kind_reconfiguration) {
2679 Handle<HeapType> old_field_type =
2680 GetFieldType(isolate, old_descriptors, i,
2681 old_details.location(), next_representation);
2683 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2686 Handle<HeapType> old_field_type =
2687 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2688 next_representation);
2690 GeneralizeFieldType(target_field_type, old_field_type, isolate);
2692 Handle<Object> wrapped_type(WrapType(next_field_type));
2693 DataDescriptor d(target_key, current_offset, wrapped_type,
2694 next_attributes, next_representation);
2695 current_offset += d.GetDetails().field_width_in_words();
2696 new_descriptors->Set(i, &d);
2698 UNIMPLEMENTED(); // TODO(ishell): implement.
2701 PropertyDetails details(next_attributes, next_kind, next_location,
2702 next_representation);
2703 Descriptor d(target_key, handle(target_descriptors->GetValue(i), isolate),
2705 new_descriptors->Set(i, &d);
2709 // |target_nof| -> |old_nof|
2710 for (int i = target_nof; i < old_nof; ++i) {
2711 PropertyDetails old_details = old_descriptors->GetDetails(i);
2712 Handle<Name> old_key(old_descriptors->GetKey(i), isolate);
2714 // Merge old_descriptor entry and modified details together.
2715 PropertyKind next_kind;
2716 PropertyAttributes next_attributes;
2717 PropertyLocation next_location;
2718 Representation next_representation;
2719 bool property_kind_reconfiguration = false;
2721 if (modify_index == i) {
2722 DCHECK_EQ(FORCE_FIELD, store_mode);
2723 // In case of property kind reconfiguration it is not necessary to
2724 // take into account representation/field type of the old descriptor.
2725 property_kind_reconfiguration = old_details.kind() != new_kind;
2727 next_kind = new_kind;
2728 next_attributes = new_attributes;
2729 next_location = kField;
2730 next_representation = new_representation;
2731 if (!property_kind_reconfiguration) {
2732 next_representation =
2733 next_representation.generalize(old_details.representation());
2736 next_kind = old_details.kind();
2737 next_attributes = old_details.attributes();
2738 next_location = old_details.location();
2739 next_representation = old_details.representation();
2742 if (next_location == kField) {
2743 if (next_kind == kData) {
2744 Handle<HeapType> next_field_type;
2745 if (modify_index == i) {
2746 next_field_type = new_field_type;
2747 if (!property_kind_reconfiguration) {
2748 Handle<HeapType> old_field_type =
2749 GetFieldType(isolate, old_descriptors, i,
2750 old_details.location(), next_representation);
2752 GeneralizeFieldType(next_field_type, old_field_type, isolate);
2755 Handle<HeapType> old_field_type =
2756 GetFieldType(isolate, old_descriptors, i, old_details.location(),
2757 next_representation);
2758 next_field_type = old_field_type;
2761 Handle<Object> wrapped_type(WrapType(next_field_type));
2763 DataDescriptor d(old_key, current_offset, wrapped_type, next_attributes,
2764 next_representation);
2765 current_offset += d.GetDetails().field_width_in_words();
2766 new_descriptors->Set(i, &d);
2768 UNIMPLEMENTED(); // TODO(ishell): implement.
2771 PropertyDetails details(next_attributes, next_kind, next_location,
2772 next_representation);
2773 Descriptor d(old_key, handle(old_descriptors->GetValue(i), isolate),
2775 new_descriptors->Set(i, &d);
2779 new_descriptors->Sort();
2781 DCHECK(store_mode != FORCE_FIELD ||
2782 new_descriptors->GetDetails(modify_index).location() == kField);
2784 Handle<Map> split_map(root_map->FindLastMatchMap(
2785 root_nof, old_nof, *new_descriptors), isolate);
2786 int split_nof = split_map->NumberOfOwnDescriptors();
2787 DCHECK_NE(old_nof, split_nof);
2789 Handle<LayoutDescriptor> new_layout_descriptor =
2790 LayoutDescriptor::New(split_map, new_descriptors, old_nof);
2792 PropertyKind split_kind;
2793 PropertyAttributes split_attributes;
2794 if (modify_index == split_nof) {
2795 split_kind = new_kind;
2796 split_attributes = new_attributes;
2798 PropertyDetails split_prop_details = old_descriptors->GetDetails(split_nof);
2799 split_kind = split_prop_details.kind();
2800 split_attributes = split_prop_details.attributes();
2802 bool transition_target_deprecated = split_map->DeprecateTarget(
2803 split_kind, old_descriptors->GetKey(split_nof), split_attributes,
2804 *new_descriptors, *new_layout_descriptor);
2806 // If |transition_target_deprecated| is true then the transition array
2807 // already contains entry for given descriptor. This means that the transition
2808 // could be inserted regardless of whether transitions array is full or not.
2809 if (!transition_target_deprecated &&
2810 !TransitionArray::CanHaveMoreTransitions(split_map)) {
2811 return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode,
2812 new_kind, new_attributes,
2813 "GenAll_CantHaveMoreTransitions");
2816 old_map->NotifyLeafMapLayoutChange();
2818 if (FLAG_trace_generalization && modify_index >= 0) {
2819 PropertyDetails old_details = old_descriptors->GetDetails(modify_index);
2820 PropertyDetails new_details = new_descriptors->GetDetails(modify_index);
2821 Handle<HeapType> old_field_type =
2822 (old_details.type() == DATA)
2823 ? handle(old_descriptors->GetFieldType(modify_index), isolate)
2824 : HeapType::Constant(
2825 handle(old_descriptors->GetValue(modify_index), isolate),
2827 Handle<HeapType> new_field_type =
2828 (new_details.type() == DATA)
2829 ? handle(new_descriptors->GetFieldType(modify_index), isolate)
2830 : HeapType::Constant(
2831 handle(new_descriptors->GetValue(modify_index), isolate),
2833 old_map->PrintGeneralization(
2834 stdout, "", modify_index, split_nof, old_nof,
2835 old_details.location() == kDescriptor && store_mode == FORCE_FIELD,
2836 old_details.representation(), new_details.representation(),
2837 *old_field_type, *new_field_type);
2840 // Add missing transitions.
2841 Handle<Map> new_map = split_map;
2842 for (int i = split_nof; i < old_nof; ++i) {
2843 new_map = CopyInstallDescriptors(new_map, i, new_descriptors,
2844 new_layout_descriptor);
2846 new_map->set_owns_descriptors(true);
2851 // Generalize the representation of all DATA descriptors.
2852 Handle<Map> Map::GeneralizeAllFieldRepresentations(
2854 Handle<DescriptorArray> descriptors(map->instance_descriptors());
2855 for (int i = 0; i < map->NumberOfOwnDescriptors(); ++i) {
2856 PropertyDetails details = descriptors->GetDetails(i);
2857 if (details.type() == DATA) {
2858 map = ReconfigureProperty(map, i, kData, details.attributes(),
2859 Representation::Tagged(),
2860 HeapType::Any(map->GetIsolate()), FORCE_FIELD);
2868 MaybeHandle<Map> Map::TryUpdate(Handle<Map> old_map) {
2869 DisallowHeapAllocation no_allocation;
2870 DisallowDeoptimization no_deoptimization(old_map->GetIsolate());
2872 if (!old_map->is_deprecated()) return old_map;
2874 // Check the state of the root map.
2875 Map* root_map = old_map->FindRootMap();
2876 if (!old_map->EquivalentToForTransition(root_map)) return MaybeHandle<Map>();
2878 ElementsKind from_kind = root_map->elements_kind();
2879 ElementsKind to_kind = old_map->elements_kind();
2880 if (from_kind != to_kind) {
2881 // Try to follow existing elements kind transitions.
2882 root_map = root_map->LookupElementsTransitionMap(to_kind);
2883 if (root_map == NULL) return MaybeHandle<Map>();
2884 // From here on, use the map with correct elements kind as root map.
2886 int root_nof = root_map->NumberOfOwnDescriptors();
2888 int old_nof = old_map->NumberOfOwnDescriptors();
2889 DescriptorArray* old_descriptors = old_map->instance_descriptors();
2891 Map* new_map = root_map;
2892 for (int i = root_nof; i < old_nof; ++i) {
2893 PropertyDetails old_details = old_descriptors->GetDetails(i);
2894 Map* transition = TransitionArray::SearchTransition(
2895 new_map, old_details.kind(), old_descriptors->GetKey(i),
2896 old_details.attributes());
2897 if (transition == NULL) return MaybeHandle<Map>();
2898 new_map = transition;
2899 DescriptorArray* new_descriptors = new_map->instance_descriptors();
2901 PropertyDetails new_details = new_descriptors->GetDetails(i);
2902 DCHECK_EQ(old_details.kind(), new_details.kind());
2903 DCHECK_EQ(old_details.attributes(), new_details.attributes());
2904 if (!old_details.representation().fits_into(new_details.representation())) {
2905 return MaybeHandle<Map>();
2907 switch (new_details.type()) {
2909 HeapType* new_type = new_descriptors->GetFieldType(i);
2910 PropertyType old_property_type = old_details.type();
2911 if (old_property_type == DATA) {
2912 HeapType* old_type = old_descriptors->GetFieldType(i);
2913 if (!old_type->NowIs(new_type)) {
2914 return MaybeHandle<Map>();
2917 DCHECK(old_property_type == DATA_CONSTANT);
2918 Object* old_value = old_descriptors->GetValue(i);
2919 if (!new_type->NowContains(old_value)) {
2920 return MaybeHandle<Map>();
2927 HeapType* new_type = new_descriptors->GetFieldType(i);
2928 DCHECK(HeapType::Any()->Is(new_type));
2934 case ACCESSOR_CONSTANT: {
2935 Object* old_value = old_descriptors->GetValue(i);
2936 Object* new_value = new_descriptors->GetValue(i);
2937 if (old_details.location() == kField || old_value != new_value) {
2938 return MaybeHandle<Map>();
2944 if (new_map->NumberOfOwnDescriptors() != old_nof) return MaybeHandle<Map>();
2945 return handle(new_map);
2950 Handle<Map> Map::Update(Handle<Map> map) {
2951 if (!map->is_deprecated()) return map;
2952 return ReconfigureProperty(map, -1, kData, NONE, Representation::None(),
2953 HeapType::None(map->GetIsolate()),
2954 ALLOW_IN_DESCRIPTOR);
2958 MaybeHandle<Object> JSObject::SetPropertyWithInterceptor(LookupIterator* it,
2959 Handle<Object> value) {
2960 Isolate* isolate = it->isolate();
2961 // Make sure that the top context does not change when doing callbacks or
2962 // interceptor calls.
2963 AssertNoContextChange ncc(isolate);
2965 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
2966 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
2967 if (interceptor->setter()->IsUndefined()) return MaybeHandle<Object>();
2969 Handle<JSObject> holder = it->GetHolder<JSObject>();
2970 v8::Local<v8::Value> result;
2971 PropertyCallbackArguments args(isolate, interceptor->data(),
2972 *it->GetReceiver(), *holder);
2974 if (it->IsElement()) {
2975 uint32_t index = it->index();
2976 v8::IndexedPropertySetterCallback setter =
2977 v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter());
2979 ApiIndexedPropertyAccess("interceptor-indexed-set", *holder, index));
2980 result = args.Call(setter, index, v8::Utils::ToLocal(value));
2982 Handle<Name> name = it->name();
2984 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
2985 return MaybeHandle<Object>();
2988 v8::GenericNamedPropertySetterCallback setter =
2989 v8::ToCData<v8::GenericNamedPropertySetterCallback>(
2990 interceptor->setter());
2992 ApiNamedPropertyAccess("interceptor-named-set", *holder, *name));
2994 args.Call(setter, v8::Utils::ToLocal(name), v8::Utils::ToLocal(value));
2997 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
2998 if (result.IsEmpty()) return MaybeHandle<Object>();
3000 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
3001 result_internal->VerifyApiCallResultType();
3007 MaybeHandle<Object> Object::SetProperty(Handle<Object> object,
3008 Handle<Name> name, Handle<Object> value,
3009 LanguageMode language_mode,
3010 StoreFromKeyed store_mode) {
3011 LookupIterator it(object, name);
3012 return SetProperty(&it, value, language_mode, store_mode);
3016 MaybeHandle<Object> Object::SetPropertyInternal(LookupIterator* it,
3017 Handle<Object> value,
3018 LanguageMode language_mode,
3019 StoreFromKeyed store_mode,
3021 // Make sure that the top context does not change when doing callbacks or
3022 // interceptor calls.
3023 AssertNoContextChange ncc(it->isolate());
3028 for (; it->IsFound(); it->Next()) {
3029 switch (it->state()) {
3030 case LookupIterator::NOT_FOUND:
3033 case LookupIterator::ACCESS_CHECK:
3034 if (it->HasAccess()) break;
3035 // Check whether it makes sense to reuse the lookup iterator. Here it
3036 // might still call into setters up the prototype chain.
3037 return JSObject::SetPropertyWithFailedAccessCheck(it, value);
3039 case LookupIterator::JSPROXY:
3040 if (it->HolderIsReceiverOrHiddenPrototype()) {
3041 return JSProxy::SetPropertyWithHandler(
3042 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(), value,
3045 // TODO(verwaest): Use the MaybeHandle to indicate result.
3046 bool has_result = false;
3047 MaybeHandle<Object> maybe_result =
3048 JSProxy::SetPropertyViaPrototypesWithHandler(
3049 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName(),
3050 value, language_mode, &has_result);
3051 if (has_result) return maybe_result;
3056 case LookupIterator::INTERCEPTOR:
3057 if (it->HolderIsReceiverOrHiddenPrototype()) {
3058 MaybeHandle<Object> maybe_result =
3059 JSObject::SetPropertyWithInterceptor(it, value);
3060 if (!maybe_result.is_null()) return maybe_result;
3061 if (it->isolate()->has_pending_exception()) return maybe_result;
3063 Maybe<PropertyAttributes> maybe_attributes =
3064 JSObject::GetPropertyAttributesWithInterceptor(it);
3065 if (!maybe_attributes.IsJust()) return MaybeHandle<Object>();
3066 done = maybe_attributes.FromJust() != ABSENT;
3067 if (done && (maybe_attributes.FromJust() & READ_ONLY) != 0) {
3068 return WriteToReadOnlyProperty(it, value, language_mode);
3073 case LookupIterator::ACCESSOR: {
3074 if (it->IsReadOnly()) {
3075 return WriteToReadOnlyProperty(it, value, language_mode);
3077 Handle<Object> accessors = it->GetAccessors();
3078 if (accessors->IsAccessorInfo() &&
3079 !it->HolderIsReceiverOrHiddenPrototype() &&
3080 AccessorInfo::cast(*accessors)->is_special_data_property()) {
3084 return SetPropertyWithAccessor(it, value, language_mode);
3086 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3087 // TODO(verwaest): We should throw an exception.
3090 case LookupIterator::DATA:
3091 if (it->IsReadOnly()) {
3092 return WriteToReadOnlyProperty(it, value, language_mode);
3094 if (it->HolderIsReceiverOrHiddenPrototype()) {
3095 return SetDataProperty(it, value);
3100 case LookupIterator::TRANSITION:
3108 // If the receiver is the JSGlobalObject, the store was contextual. In case
3109 // the property did not exist yet on the global object itself, we have to
3110 // throw a reference error in strict mode.
3111 if (it->GetReceiver()->IsJSGlobalObject() && is_strict(language_mode)) {
3112 THROW_NEW_ERROR(it->isolate(),
3113 NewReferenceError(MessageTemplate::kNotDefined, it->name()),
3118 return MaybeHandle<Object>();
3122 MaybeHandle<Object> Object::SetProperty(LookupIterator* it,
3123 Handle<Object> value,
3124 LanguageMode language_mode,
3125 StoreFromKeyed store_mode) {
3127 MaybeHandle<Object> result =
3128 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3129 if (found) return result;
3130 return AddDataProperty(it, value, NONE, language_mode, store_mode);
3134 MaybeHandle<Object> Object::SetSuperProperty(LookupIterator* it,
3135 Handle<Object> value,
3136 LanguageMode language_mode,
3137 StoreFromKeyed store_mode) {
3139 MaybeHandle<Object> result =
3140 SetPropertyInternal(it, value, language_mode, store_mode, &found);
3141 if (found) return result;
3143 if (!it->GetReceiver()->IsJSReceiver()) {
3144 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3145 it->GetName(), value, language_mode);
3148 LookupIterator::Configuration c = LookupIterator::OWN;
3149 LookupIterator own_lookup =
3151 ? LookupIterator(it->isolate(), it->GetReceiver(), it->index(), c)
3152 : LookupIterator(it->GetReceiver(), it->name(), c);
3154 for (; own_lookup.IsFound(); own_lookup.Next()) {
3155 switch (own_lookup.state()) {
3156 case LookupIterator::ACCESS_CHECK:
3157 if (!own_lookup.HasAccess()) {
3158 return JSObject::SetPropertyWithFailedAccessCheck(&own_lookup, value);
3162 case LookupIterator::INTEGER_INDEXED_EXOTIC:
3163 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3164 value, language_mode);
3166 case LookupIterator::DATA: {
3167 PropertyDetails details = own_lookup.property_details();
3168 if (details.IsConfigurable() || !details.IsReadOnly()) {
3169 return JSObject::DefineOwnPropertyIgnoreAttributes(
3170 &own_lookup, value, details.attributes());
3172 return WriteToReadOnlyProperty(&own_lookup, value, language_mode);
3175 case LookupIterator::ACCESSOR: {
3176 PropertyDetails details = own_lookup.property_details();
3177 if (details.IsConfigurable()) {
3178 return JSObject::DefineOwnPropertyIgnoreAttributes(
3179 &own_lookup, value, details.attributes());
3182 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
3183 value, language_mode);
3186 case LookupIterator::INTERCEPTOR:
3187 case LookupIterator::JSPROXY: {
3189 MaybeHandle<Object> result = SetPropertyInternal(
3190 &own_lookup, value, language_mode, store_mode, &found);
3191 if (found) return result;
3195 case LookupIterator::NOT_FOUND:
3196 case LookupIterator::TRANSITION:
3201 return JSObject::AddDataProperty(&own_lookup, value, NONE, language_mode,
3206 MaybeHandle<Object> Object::ReadAbsentProperty(LookupIterator* it,
3207 LanguageMode language_mode) {
3208 if (is_strong(language_mode)) {
3209 THROW_NEW_ERROR(it->isolate(),
3210 NewTypeError(MessageTemplate::kStrongPropertyAccess,
3211 it->GetName(), it->GetReceiver()),
3214 return it->isolate()->factory()->undefined_value();
3217 MaybeHandle<Object> Object::ReadAbsentProperty(Isolate* isolate,
3218 Handle<Object> receiver,
3219 Handle<Object> name,
3220 LanguageMode language_mode) {
3221 if (is_strong(language_mode)) {
3224 NewTypeError(MessageTemplate::kStrongPropertyAccess, name, receiver),
3227 return isolate->factory()->undefined_value();
3231 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3232 LookupIterator* it, Handle<Object> value, LanguageMode language_mode) {
3233 return WriteToReadOnlyProperty(it->isolate(), it->GetReceiver(),
3234 it->GetName(), value, language_mode);
3238 MaybeHandle<Object> Object::WriteToReadOnlyProperty(
3239 Isolate* isolate, Handle<Object> receiver, Handle<Object> name,
3240 Handle<Object> value, LanguageMode language_mode) {
3241 if (is_sloppy(language_mode)) return value;
3244 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, name, receiver),
3249 MaybeHandle<Object> Object::RedefineNonconfigurableProperty(
3250 Isolate* isolate, Handle<Object> name, Handle<Object> value,
3251 LanguageMode language_mode) {
3252 if (is_sloppy(language_mode)) return value;
3253 THROW_NEW_ERROR(isolate,
3254 NewTypeError(MessageTemplate::kRedefineDisallowed, name),
3259 MaybeHandle<Object> Object::SetDataProperty(LookupIterator* it,
3260 Handle<Object> value) {
3261 // Proxies are handled on the WithHandler path. Other non-JSObjects cannot
3262 // have own properties.
3263 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
3265 // Store on the holder which may be hidden behind the receiver.
3266 DCHECK(it->HolderIsReceiverOrHiddenPrototype());
3268 // Old value for the observation change record.
3269 // Fetch before transforming the object since the encoding may become
3270 // incompatible with what's cached in |it|.
3271 bool is_observed = receiver->map()->is_observed() &&
3273 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
3274 MaybeHandle<Object> maybe_old;
3275 if (is_observed) maybe_old = it->GetDataValue();
3277 Handle<Object> to_assign = value;
3278 // Convert the incoming value to a number for storing into typed arrays.
3279 if (it->IsElement() && receiver->HasFixedTypedArrayElements()) {
3280 if (!value->IsNumber() && !value->IsUndefined()) {
3281 ASSIGN_RETURN_ON_EXCEPTION(it->isolate(), to_assign,
3282 Execution::ToNumber(it->isolate(), value),
3284 // ToNumber above might modify the receiver, causing the cached
3285 // holder_map to mismatch the actual holder->map() after this point.
3286 // Reload the map to be in consistent state. Other cached state cannot
3287 // have been invalidated since typed array elements cannot be reconfigured
3289 it->ReloadHolderMap();
3291 // We have to recheck the length. However, it can only change if the
3292 // underlying buffer was neutered, so just check that.
3293 if (Handle<JSArrayBufferView>::cast(receiver)->WasNeutered()) {
3299 // Possibly migrate to the most up-to-date map that will be able to store
3300 // |value| under it->name().
3301 it->PrepareForDataProperty(to_assign);
3303 // Write the property value.
3304 it->WriteDataValue(to_assign);
3306 // Send the change record if there are observers.
3307 if (is_observed && !value->SameValue(*maybe_old.ToHandleChecked())) {
3308 RETURN_ON_EXCEPTION(it->isolate(), JSObject::EnqueueChangeRecord(
3309 receiver, "update", it->GetName(),
3310 maybe_old.ToHandleChecked()),
3318 MUST_USE_RESULT static MaybeHandle<Object> BeginPerformSplice(
3319 Handle<JSArray> object) {
3320 Isolate* isolate = object->GetIsolate();
3321 HandleScope scope(isolate);
3322 Handle<Object> args[] = {object};
3324 return Execution::Call(
3325 isolate, Handle<JSFunction>(isolate->observers_begin_perform_splice()),
3326 isolate->factory()->undefined_value(), arraysize(args), args);
3330 MUST_USE_RESULT static MaybeHandle<Object> EndPerformSplice(
3331 Handle<JSArray> object) {
3332 Isolate* isolate = object->GetIsolate();
3333 HandleScope scope(isolate);
3334 Handle<Object> args[] = {object};
3336 return Execution::Call(
3337 isolate, Handle<JSFunction>(isolate->observers_end_perform_splice()),
3338 isolate->factory()->undefined_value(), arraysize(args), args);
3342 MUST_USE_RESULT static MaybeHandle<Object> EnqueueSpliceRecord(
3343 Handle<JSArray> object, uint32_t index, Handle<JSArray> deleted,
3344 uint32_t add_count) {
3345 Isolate* isolate = object->GetIsolate();
3346 HandleScope scope(isolate);
3347 Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index);
3348 Handle<Object> add_count_object =
3349 isolate->factory()->NewNumberFromUint(add_count);
3351 Handle<Object> args[] = {object, index_object, deleted, add_count_object};
3353 return Execution::Call(
3354 isolate, Handle<JSFunction>(isolate->observers_enqueue_splice()),
3355 isolate->factory()->undefined_value(), arraysize(args), args);
3359 MaybeHandle<Object> Object::AddDataProperty(LookupIterator* it,
3360 Handle<Object> value,
3361 PropertyAttributes attributes,
3362 LanguageMode language_mode,
3363 StoreFromKeyed store_mode) {
3364 DCHECK(!it->GetReceiver()->IsJSProxy());
3365 if (!it->GetReceiver()->IsJSObject()) {
3366 // TODO(verwaest): Throw a TypeError with a more specific message.
3367 return WriteToReadOnlyProperty(it, value, language_mode);
3370 DCHECK_NE(LookupIterator::INTEGER_INDEXED_EXOTIC, it->state());
3372 Handle<JSObject> receiver = it->GetStoreTarget();
3374 // If the receiver is a JSGlobalProxy, store on the prototype (JSGlobalObject)
3375 // instead. If the prototype is Null, the proxy is detached.
3376 if (receiver->IsJSGlobalProxy()) return value;
3378 Isolate* isolate = it->isolate();
3380 if (!receiver->map()->is_extensible() &&
3381 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()))) {
3382 if (is_sloppy(language_mode)) return value;
3383 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kObjectNotExtensible,
3388 if (it->IsElement()) {
3389 if (receiver->IsJSArray()) {
3390 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
3391 if (JSArray::WouldChangeReadOnlyLength(array, it->index())) {
3392 if (is_sloppy(language_mode)) return value;
3393 return JSArray::ReadOnlyLengthError(array);
3396 if (FLAG_trace_external_array_abuse &&
3397 array->HasFixedTypedArrayElements()) {
3398 CheckArrayAbuse(array, "typed elements write", it->index(), true);
3401 if (FLAG_trace_js_array_abuse && !array->HasFixedTypedArrayElements()) {
3402 CheckArrayAbuse(array, "elements write", it->index(), false);
3406 MaybeHandle<Object> result =
3407 JSObject::AddDataElement(receiver, it->index(), value, attributes);
3408 JSObject::ValidateElements(receiver);
3411 // Migrate to the most up-to-date map that will be able to store |value|
3412 // under it->name() with |attributes|.
3413 it->PrepareTransitionToDataProperty(value, attributes, store_mode);
3414 DCHECK_EQ(LookupIterator::TRANSITION, it->state());
3415 it->ApplyTransitionToDataProperty();
3417 // TODO(verwaest): Encapsulate dictionary handling better.
3418 if (receiver->map()->is_dictionary_map()) {
3419 // TODO(verwaest): Probably should ensure this is done beforehand.
3420 it->InternalizeName();
3421 // TODO(dcarney): just populate TransitionPropertyCell here?
3422 JSObject::AddSlowProperty(receiver, it->name(), value, attributes);
3424 // Write the property value.
3425 it->WriteDataValue(value);
3428 // Send the change record if there are observers.
3429 if (receiver->map()->is_observed() &&
3430 !isolate->IsInternallyUsedPropertyName(it->name())) {
3431 RETURN_ON_EXCEPTION(isolate, JSObject::EnqueueChangeRecord(
3432 receiver, "add", it->name(),
3433 it->factory()->the_hole_value()),
3442 void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) {
3443 // Only supports adding slack to owned descriptors.
3444 DCHECK(map->owns_descriptors());
3446 Handle<DescriptorArray> descriptors(map->instance_descriptors());
3447 int old_size = map->NumberOfOwnDescriptors();
3448 if (slack <= descriptors->NumberOfSlackDescriptors()) return;
3450 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
3451 descriptors, old_size, slack);
3453 DisallowHeapAllocation no_allocation;
3454 // The descriptors are still the same, so keep the layout descriptor.
3455 LayoutDescriptor* layout_descriptor = map->GetLayoutDescriptor();
3457 if (old_size == 0) {
3458 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3462 // If the source descriptors had an enum cache we copy it. This ensures
3463 // that the maps to which we push the new descriptor array back can rely
3464 // on a cache always being available once it is set. If the map has more
3465 // enumerated descriptors than available in the original cache, the cache
3466 // will be lazily replaced by the extended cache when needed.
3467 if (descriptors->HasEnumCache()) {
3468 new_descriptors->CopyEnumCacheFrom(*descriptors);
3471 // Replace descriptors by new_descriptors in all maps that share it.
3472 map->GetHeap()->incremental_marking()->RecordWrites(*descriptors);
3475 for (Object* current = map->GetBackPointer();
3476 !current->IsUndefined();
3477 current = walk_map->GetBackPointer()) {
3478 walk_map = Map::cast(current);
3479 if (walk_map->instance_descriptors() != *descriptors) break;
3480 walk_map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3483 map->UpdateDescriptors(*new_descriptors, layout_descriptor);
3488 static int AppendUniqueCallbacks(NeanderArray* callbacks,
3489 Handle<typename T::Array> array,
3490 int valid_descriptors) {
3491 int nof_callbacks = callbacks->length();
3493 Isolate* isolate = array->GetIsolate();
3494 // Ensure the keys are unique names before writing them into the
3495 // instance descriptor. Since it may cause a GC, it has to be done before we
3496 // temporarily put the heap in an invalid state while appending descriptors.
3497 for (int i = 0; i < nof_callbacks; ++i) {
3498 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3499 if (entry->name()->IsUniqueName()) continue;
3500 Handle<String> key =
3501 isolate->factory()->InternalizeString(
3502 Handle<String>(String::cast(entry->name())));
3503 entry->set_name(*key);
3506 // Fill in new callback descriptors. Process the callbacks from
3507 // back to front so that the last callback with a given name takes
3508 // precedence over previously added callbacks with that name.
3509 for (int i = nof_callbacks - 1; i >= 0; i--) {
3510 Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i)));
3511 Handle<Name> key(Name::cast(entry->name()));
3512 // Check if a descriptor with this name already exists before writing.
3513 if (!T::Contains(key, entry, valid_descriptors, array)) {
3514 T::Insert(key, entry, valid_descriptors, array);
3515 valid_descriptors++;
3519 return valid_descriptors;
3522 struct DescriptorArrayAppender {
3523 typedef DescriptorArray Array;
3524 static bool Contains(Handle<Name> key,
3525 Handle<AccessorInfo> entry,
3526 int valid_descriptors,
3527 Handle<DescriptorArray> array) {
3528 DisallowHeapAllocation no_gc;
3529 return array->Search(*key, valid_descriptors) != DescriptorArray::kNotFound;
3531 static void Insert(Handle<Name> key,
3532 Handle<AccessorInfo> entry,
3533 int valid_descriptors,
3534 Handle<DescriptorArray> array) {
3535 DisallowHeapAllocation no_gc;
3536 AccessorConstantDescriptor desc(key, entry, entry->property_attributes());
3537 array->Append(&desc);
3542 struct FixedArrayAppender {
3543 typedef FixedArray Array;
3544 static bool Contains(Handle<Name> key,
3545 Handle<AccessorInfo> entry,
3546 int valid_descriptors,
3547 Handle<FixedArray> array) {
3548 for (int i = 0; i < valid_descriptors; i++) {
3549 if (*key == AccessorInfo::cast(array->get(i))->name()) return true;
3553 static void Insert(Handle<Name> key,
3554 Handle<AccessorInfo> entry,
3555 int valid_descriptors,
3556 Handle<FixedArray> array) {
3557 DisallowHeapAllocation no_gc;
3558 array->set(valid_descriptors, *entry);
3563 void Map::AppendCallbackDescriptors(Handle<Map> map,
3564 Handle<Object> descriptors) {
3565 int nof = map->NumberOfOwnDescriptors();
3566 Handle<DescriptorArray> array(map->instance_descriptors());
3567 NeanderArray callbacks(descriptors);
3568 DCHECK(array->NumberOfSlackDescriptors() >= callbacks.length());
3569 nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof);
3570 map->SetNumberOfOwnDescriptors(nof);
3574 int AccessorInfo::AppendUnique(Handle<Object> descriptors,
3575 Handle<FixedArray> array,
3576 int valid_descriptors) {
3577 NeanderArray callbacks(descriptors);
3578 DCHECK(array->length() >= callbacks.length() + valid_descriptors);
3579 return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks,
3585 static bool ContainsMap(MapHandleList* maps, Map* map) {
3586 DCHECK_NOT_NULL(map);
3587 for (int i = 0; i < maps->length(); ++i) {
3588 if (!maps->at(i).is_null() && *maps->at(i) == map) return true;
3594 Handle<Map> Map::FindTransitionedMap(Handle<Map> map,
3595 MapHandleList* candidates) {
3596 ElementsKind kind = map->elements_kind();
3597 bool packed = IsFastPackedElementsKind(kind);
3599 Map* transition = nullptr;
3600 if (IsTransitionableFastElementsKind(kind)) {
3601 for (Map* current = map->ElementsTransitionMap();
3602 current != nullptr && current->has_fast_elements();
3603 current = current->ElementsTransitionMap()) {
3604 if (ContainsMap(candidates, current) &&
3605 (packed || !IsFastPackedElementsKind(current->elements_kind()))) {
3606 transition = current;
3607 packed = packed && IsFastPackedElementsKind(current->elements_kind());
3611 return transition == nullptr ? Handle<Map>() : handle(transition);
3615 static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) {
3616 Map* current_map = map;
3618 ElementsKind kind = map->elements_kind();
3619 while (kind != to_kind) {
3620 Map* next_map = current_map->ElementsTransitionMap();
3621 if (next_map == nullptr) return current_map;
3622 kind = next_map->elements_kind();
3623 current_map = next_map;
3626 DCHECK_EQ(to_kind, current_map->elements_kind());
3631 Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) {
3632 Map* to_map = FindClosestElementsTransition(this, to_kind);
3633 if (to_map->elements_kind() == to_kind) return to_map;
3638 bool Map::IsMapInArrayPrototypeChain() {
3639 Isolate* isolate = GetIsolate();
3640 if (isolate->initial_array_prototype()->map() == this) {
3644 if (isolate->initial_object_prototype()->map() == this) {
3652 Handle<WeakCell> Map::WeakCellForMap(Handle<Map> map) {
3653 Isolate* isolate = map->GetIsolate();
3654 if (map->weak_cell_cache()->IsWeakCell()) {
3655 return Handle<WeakCell>(WeakCell::cast(map->weak_cell_cache()));
3657 Handle<WeakCell> weak_cell = isolate->factory()->NewWeakCell(map);
3658 map->set_weak_cell_cache(*weak_cell);
3663 static Handle<Map> AddMissingElementsTransitions(Handle<Map> map,
3664 ElementsKind to_kind) {
3665 DCHECK(IsTransitionElementsKind(map->elements_kind()));
3667 Handle<Map> current_map = map;
3669 ElementsKind kind = map->elements_kind();
3670 TransitionFlag flag;
3671 if (map->is_prototype_map()) {
3672 flag = OMIT_TRANSITION;
3674 flag = INSERT_TRANSITION;
3675 if (IsFastElementsKind(kind)) {
3676 while (kind != to_kind && !IsTerminalElementsKind(kind)) {
3677 kind = GetNextTransitionElementsKind(kind);
3678 current_map = Map::CopyAsElementsKind(current_map, kind, flag);
3683 // In case we are exiting the fast elements kind system, just add the map in
3685 if (kind != to_kind) {
3686 current_map = Map::CopyAsElementsKind(current_map, to_kind, flag);
3689 DCHECK(current_map->elements_kind() == to_kind);
3694 Handle<Map> Map::TransitionElementsTo(Handle<Map> map,
3695 ElementsKind to_kind) {
3696 ElementsKind from_kind = map->elements_kind();
3697 if (from_kind == to_kind) return map;
3699 Isolate* isolate = map->GetIsolate();
3700 Context* native_context = isolate->context()->native_context();
3701 if (from_kind == FAST_SLOPPY_ARGUMENTS_ELEMENTS) {
3702 if (*map == native_context->fast_aliased_arguments_map()) {
3703 DCHECK_EQ(SLOW_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3704 return handle(native_context->slow_aliased_arguments_map());
3706 } else if (from_kind == SLOW_SLOPPY_ARGUMENTS_ELEMENTS) {
3707 if (*map == native_context->slow_aliased_arguments_map()) {
3708 DCHECK_EQ(FAST_SLOPPY_ARGUMENTS_ELEMENTS, to_kind);
3709 return handle(native_context->fast_aliased_arguments_map());
3712 Object* maybe_array_maps = map->is_strong()
3713 ? native_context->js_array_strong_maps()
3714 : native_context->js_array_maps();
3715 if (maybe_array_maps->IsFixedArray()) {
3716 DisallowHeapAllocation no_gc;
3717 FixedArray* array_maps = FixedArray::cast(maybe_array_maps);
3718 if (array_maps->get(from_kind) == *map) {
3719 Object* maybe_transitioned_map = array_maps->get(to_kind);
3720 if (maybe_transitioned_map->IsMap()) {
3721 return handle(Map::cast(maybe_transitioned_map));
3727 DCHECK(!map->IsUndefined());
3728 bool allow_store_transition = IsTransitionElementsKind(from_kind);
3729 // Only store fast element maps in ascending generality.
3730 if (IsFastElementsKind(to_kind)) {
3731 allow_store_transition =
3732 allow_store_transition && IsTransitionableFastElementsKind(from_kind) &&
3733 IsMoreGeneralElementsKindTransition(from_kind, to_kind);
3736 if (!allow_store_transition) {
3737 return Map::CopyAsElementsKind(map, to_kind, OMIT_TRANSITION);
3740 return Map::AsElementsKind(map, to_kind);
3745 Handle<Map> Map::AsElementsKind(Handle<Map> map, ElementsKind kind) {
3746 Handle<Map> closest_map(FindClosestElementsTransition(*map, kind));
3748 if (closest_map->elements_kind() == kind) {
3752 return AddMissingElementsTransitions(closest_map, kind);
3756 Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
3757 ElementsKind to_kind) {
3758 Handle<Map> map(object->map());
3759 return Map::TransitionElementsTo(map, to_kind);
3763 Maybe<bool> JSProxy::HasPropertyWithHandler(Handle<JSProxy> proxy,
3764 Handle<Name> name) {
3765 Isolate* isolate = proxy->GetIsolate();
3767 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3768 if (name->IsSymbol()) return Just(false);
3770 Handle<Object> args[] = { name };
3771 Handle<Object> result;
3772 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3773 isolate, result, CallTrap(proxy, "has", isolate->derived_has_trap(),
3774 arraysize(args), args),
3777 return Just(result->BooleanValue());
3781 MaybeHandle<Object> JSProxy::SetPropertyWithHandler(
3782 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3783 Handle<Object> value, LanguageMode language_mode) {
3784 Isolate* isolate = proxy->GetIsolate();
3786 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3787 if (name->IsSymbol()) return value;
3789 Handle<Object> args[] = { receiver, name, value };
3790 RETURN_ON_EXCEPTION(
3794 isolate->derived_set_trap(),
3803 MaybeHandle<Object> JSProxy::SetPropertyViaPrototypesWithHandler(
3804 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name,
3805 Handle<Object> value, LanguageMode language_mode, bool* done) {
3806 Isolate* isolate = proxy->GetIsolate();
3807 Handle<Object> handler(proxy->handler(), isolate); // Trap might morph proxy.
3809 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3810 if (name->IsSymbol()) {
3812 return isolate->factory()->the_hole_value();
3815 *done = true; // except where redefined...
3816 Handle<Object> args[] = { name };
3817 Handle<Object> result;
3818 ASSIGN_RETURN_ON_EXCEPTION(
3821 "getPropertyDescriptor",
3827 if (result->IsUndefined()) {
3829 return isolate->factory()->the_hole_value();
3832 // Emulate [[GetProperty]] semantics for proxies.
3833 Handle<Object> argv[] = { result };
3834 Handle<Object> desc;
3835 ASSIGN_RETURN_ON_EXCEPTION(
3837 Execution::Call(isolate,
3838 isolate->to_complete_property_descriptor(),
3844 // [[GetProperty]] requires to check that all properties are configurable.
3845 Handle<String> configurable_name =
3846 isolate->factory()->InternalizeOneByteString(
3847 STATIC_CHAR_VECTOR("configurable_"));
3848 Handle<Object> configurable =
3849 Object::GetProperty(desc, configurable_name).ToHandleChecked();
3850 DCHECK(configurable->IsBoolean());
3851 if (configurable->IsFalse()) {
3852 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
3853 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
3854 THROW_NEW_ERROR(isolate,
3855 NewTypeError(MessageTemplate::kProxyPropNotConfigurable,
3856 handler, name, trap),
3859 DCHECK(configurable->IsTrue());
3861 // Check for DataDescriptor.
3862 Handle<String> hasWritable_name =
3863 isolate->factory()->InternalizeOneByteString(
3864 STATIC_CHAR_VECTOR("hasWritable_"));
3865 Handle<Object> hasWritable =
3866 Object::GetProperty(desc, hasWritable_name).ToHandleChecked();
3867 DCHECK(hasWritable->IsBoolean());
3868 if (hasWritable->IsTrue()) {
3869 Handle<String> writable_name = isolate->factory()->InternalizeOneByteString(
3870 STATIC_CHAR_VECTOR("writable_"));
3871 Handle<Object> writable =
3872 Object::GetProperty(desc, writable_name).ToHandleChecked();
3873 DCHECK(writable->IsBoolean());
3874 *done = writable->IsFalse();
3875 if (!*done) return isolate->factory()->the_hole_value();
3876 return WriteToReadOnlyProperty(isolate, receiver, name, value,
3880 // We have an AccessorDescriptor.
3881 Handle<String> set_name =
3882 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("set_"));
3883 Handle<Object> setter = Object::GetProperty(desc, set_name).ToHandleChecked();
3884 if (!setter->IsUndefined()) {
3885 // TODO(rossberg): nicer would be to cast to some JSCallable here...
3886 return SetPropertyWithDefinedSetter(
3887 receiver, Handle<JSReceiver>::cast(setter), value);
3890 if (is_sloppy(language_mode)) return value;
3892 isolate, NewTypeError(MessageTemplate::kNoSetterInCallback, name, proxy),
3897 MaybeHandle<Object> JSProxy::DeletePropertyWithHandler(
3898 Handle<JSProxy> proxy, Handle<Name> name, LanguageMode language_mode) {
3899 Isolate* isolate = proxy->GetIsolate();
3901 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3902 if (name->IsSymbol()) return isolate->factory()->false_value();
3904 Handle<Object> args[] = { name };
3905 Handle<Object> result;
3906 ASSIGN_RETURN_ON_EXCEPTION(
3915 bool result_bool = result->BooleanValue();
3916 if (is_strict(language_mode) && !result_bool) {
3917 Handle<Object> handler(proxy->handler(), isolate);
3920 NewTypeError(MessageTemplate::kProxyHandlerDeleteFailed, handler),
3923 return isolate->factory()->ToBoolean(result_bool);
3927 Maybe<PropertyAttributes> JSProxy::GetPropertyAttributesWithHandler(
3928 Handle<JSProxy> proxy, Handle<Object> receiver, Handle<Name> name) {
3929 Isolate* isolate = proxy->GetIsolate();
3930 HandleScope scope(isolate);
3932 // TODO(rossberg): adjust once there is a story for symbols vs proxies.
3933 if (name->IsSymbol()) return Just(ABSENT);
3935 Handle<Object> args[] = { name };
3936 Handle<Object> result;
3937 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3938 isolate, result, proxy->CallTrap(proxy, "getPropertyDescriptor",
3939 Handle<Object>(), arraysize(args), args),
3940 Nothing<PropertyAttributes>());
3942 if (result->IsUndefined()) return Just(ABSENT);
3944 Handle<Object> argv[] = { result };
3945 Handle<Object> desc;
3946 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
3948 Execution::Call(isolate, isolate->to_complete_property_descriptor(),
3949 result, arraysize(argv), argv),
3950 Nothing<PropertyAttributes>());
3952 // Convert result to PropertyAttributes.
3953 Handle<String> enum_n = isolate->factory()->InternalizeOneByteString(
3954 STATIC_CHAR_VECTOR("enumerable_"));
3955 Handle<Object> enumerable;
3956 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, enumerable,
3957 Object::GetProperty(desc, enum_n),
3958 Nothing<PropertyAttributes>());
3959 Handle<String> conf_n = isolate->factory()->InternalizeOneByteString(
3960 STATIC_CHAR_VECTOR("configurable_"));
3961 Handle<Object> configurable;
3962 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, configurable,
3963 Object::GetProperty(desc, conf_n),
3964 Nothing<PropertyAttributes>());
3965 Handle<String> writ_n = isolate->factory()->InternalizeOneByteString(
3966 STATIC_CHAR_VECTOR("writable_"));
3967 Handle<Object> writable;
3968 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, writable,
3969 Object::GetProperty(desc, writ_n),
3970 Nothing<PropertyAttributes>());
3971 if (!writable->BooleanValue()) {
3972 Handle<String> set_n = isolate->factory()->InternalizeOneByteString(
3973 STATIC_CHAR_VECTOR("set_"));
3974 Handle<Object> setter;
3975 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, setter,
3976 Object::GetProperty(desc, set_n),
3977 Nothing<PropertyAttributes>());
3978 writable = isolate->factory()->ToBoolean(!setter->IsUndefined());
3981 if (configurable->IsFalse()) {
3982 Handle<Object> handler(proxy->handler(), isolate);
3983 Handle<String> trap = isolate->factory()->InternalizeOneByteString(
3984 STATIC_CHAR_VECTOR("getPropertyDescriptor"));
3985 Handle<Object> error = isolate->factory()->NewTypeError(
3986 MessageTemplate::kProxyPropNotConfigurable, handler, name, trap);
3987 isolate->Throw(*error);
3988 return Nothing<PropertyAttributes>();
3991 int attributes = NONE;
3992 if (!enumerable->BooleanValue()) attributes |= DONT_ENUM;
3993 if (!configurable->BooleanValue()) attributes |= DONT_DELETE;
3994 if (!writable->BooleanValue()) attributes |= READ_ONLY;
3995 return Just(static_cast<PropertyAttributes>(attributes));
3999 void JSProxy::Fix(Handle<JSProxy> proxy) {
4000 Isolate* isolate = proxy->GetIsolate();
4002 // Save identity hash.
4003 Handle<Object> hash(proxy->GetIdentityHash(), isolate);
4005 if (proxy->IsJSFunctionProxy()) {
4006 isolate->factory()->BecomeJSFunction(proxy);
4007 // Code will be set on the JavaScript side.
4009 isolate->factory()->BecomeJSObject(proxy);
4011 DCHECK(proxy->IsJSObject());
4013 // Inherit identity, if it was present.
4014 if (hash->IsSmi()) {
4015 JSObject::SetIdentityHash(Handle<JSObject>::cast(proxy),
4016 Handle<Smi>::cast(hash));
4021 MaybeHandle<Object> JSProxy::CallTrap(Handle<JSProxy> proxy,
4023 Handle<Object> derived,
4025 Handle<Object> argv[]) {
4026 Isolate* isolate = proxy->GetIsolate();
4027 Handle<Object> handler(proxy->handler(), isolate);
4029 Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name);
4030 Handle<Object> trap;
4031 ASSIGN_RETURN_ON_EXCEPTION(
4033 Object::GetPropertyOrElement(handler, trap_name),
4036 if (trap->IsUndefined()) {
4037 if (derived.is_null()) {
4038 THROW_NEW_ERROR(isolate,
4039 NewTypeError(MessageTemplate::kProxyHandlerTrapMissing,
4040 handler, trap_name),
4043 trap = Handle<Object>(derived);
4046 return Execution::Call(isolate, trap, handler, argc, argv);
4050 void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) {
4051 DCHECK(object->map()->GetInObjectProperties() ==
4052 map->GetInObjectProperties());
4053 ElementsKind obj_kind = object->map()->elements_kind();
4054 ElementsKind map_kind = map->elements_kind();
4055 if (map_kind != obj_kind) {
4056 ElementsKind to_kind = map_kind;
4057 if (IsMoreGeneralElementsKindTransition(map_kind, obj_kind) ||
4058 IsDictionaryElementsKind(obj_kind)) {
4061 if (IsDictionaryElementsKind(to_kind)) {
4062 NormalizeElements(object);
4064 TransitionElementsKind(object, to_kind);
4066 map = Map::AsElementsKind(map, to_kind);
4068 JSObject::MigrateToMap(object, map);
4072 void JSObject::MigrateInstance(Handle<JSObject> object) {
4073 Handle<Map> original_map(object->map());
4074 Handle<Map> map = Map::Update(original_map);
4075 map->set_migration_target(true);
4076 MigrateToMap(object, map);
4077 if (FLAG_trace_migration) {
4078 object->PrintInstanceMigration(stdout, *original_map, *map);
4084 bool JSObject::TryMigrateInstance(Handle<JSObject> object) {
4085 Isolate* isolate = object->GetIsolate();
4086 DisallowDeoptimization no_deoptimization(isolate);
4087 Handle<Map> original_map(object->map(), isolate);
4088 Handle<Map> new_map;
4089 if (!Map::TryUpdate(original_map).ToHandle(&new_map)) {
4092 JSObject::MigrateToMap(object, new_map);
4093 if (FLAG_trace_migration) {
4094 object->PrintInstanceMigration(stdout, *original_map, object->map());
4100 void JSObject::AddProperty(Handle<JSObject> object, Handle<Name> name,
4101 Handle<Object> value,
4102 PropertyAttributes attributes) {
4103 LookupIterator it(object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
4104 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
4107 DCHECK(!object->IsJSProxy());
4108 DCHECK(!name->AsArrayIndex(&index));
4109 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
4110 DCHECK(maybe.IsJust());
4111 DCHECK(!it.IsFound());
4112 DCHECK(object->map()->is_extensible() ||
4113 it.isolate()->IsInternallyUsedPropertyName(name));
4115 AddDataProperty(&it, value, attributes, STRICT,
4116 CERTAINLY_NOT_STORE_FROM_KEYED).Check();
4121 void ExecutableAccessorInfo::ClearSetter(Handle<ExecutableAccessorInfo> info) {
4122 Handle<Object> object = v8::FromCData(info->GetIsolate(), nullptr);
4123 info->set_setter(*object);
4127 // Reconfigures a property to a data property with attributes, even if it is not
4129 // Requires a LookupIterator that does not look at the prototype chain beyond
4130 // hidden prototypes.
4131 MaybeHandle<Object> JSObject::DefineOwnPropertyIgnoreAttributes(
4132 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
4133 ExecutableAccessorInfoHandling handling) {
4134 Handle<JSObject> object = Handle<JSObject>::cast(it->GetReceiver());
4135 bool is_observed = object->map()->is_observed() &&
4137 !it->isolate()->IsInternallyUsedPropertyName(it->name()));
4139 for (; it->IsFound(); it->Next()) {
4140 switch (it->state()) {
4141 case LookupIterator::JSPROXY:
4142 case LookupIterator::NOT_FOUND:
4143 case LookupIterator::TRANSITION:
4146 case LookupIterator::ACCESS_CHECK:
4147 if (!it->HasAccess()) {
4148 it->isolate()->ReportFailedAccessCheck(it->GetHolder<JSObject>());
4149 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(it->isolate(), Object);
4154 // If there's an interceptor, try to store the property with the
4156 // In case of success, the attributes will have been reset to the default
4157 // attributes of the interceptor, rather than the incoming attributes.
4159 // TODO(verwaest): JSProxy afterwards verify the attributes that the
4160 // JSProxy claims it has, and verifies that they are compatible. If not,
4161 // they throw. Here we should do the same.
4162 case LookupIterator::INTERCEPTOR:
4163 if (handling == DONT_FORCE_FIELD) {
4164 MaybeHandle<Object> maybe_result =
4165 JSObject::SetPropertyWithInterceptor(it, value);
4166 if (!maybe_result.is_null()) return maybe_result;
4167 if (it->isolate()->has_pending_exception()) return maybe_result;
4171 case LookupIterator::ACCESSOR: {
4172 Handle<Object> accessors = it->GetAccessors();
4174 // Special handling for ExecutableAccessorInfo, which behaves like a
4176 if (accessors->IsExecutableAccessorInfo() &&
4177 handling == DONT_FORCE_FIELD) {
4178 PropertyDetails details = it->property_details();
4179 // Ensure the context isn't changed after calling into accessors.
4180 AssertNoContextChange ncc(it->isolate());
4182 Handle<Object> result;
4183 ASSIGN_RETURN_ON_EXCEPTION(
4184 it->isolate(), result,
4185 JSObject::SetPropertyWithAccessor(it, value, STRICT), Object);
4186 DCHECK(result->SameValue(*value));
4188 if (details.attributes() == attributes) return value;
4190 // Reconfigure the accessor if attributes mismatch.
4191 Handle<ExecutableAccessorInfo> new_data = Accessors::CloneAccessor(
4192 it->isolate(), Handle<ExecutableAccessorInfo>::cast(accessors));
4193 new_data->set_property_attributes(attributes);
4194 // By clearing the setter we don't have to introduce a lookup to
4195 // the setter, simply make it unavailable to reflect the
4197 if (attributes & READ_ONLY) {
4198 ExecutableAccessorInfo::ClearSetter(new_data);
4201 it->TransitionToAccessorPair(new_data, attributes);
4203 it->ReconfigureDataProperty(value, attributes);
4204 it->WriteDataValue(value);
4208 RETURN_ON_EXCEPTION(
4210 EnqueueChangeRecord(object, "reconfigure", it->GetName(),
4211 it->factory()->the_hole_value()),
4217 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4218 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4221 case LookupIterator::DATA: {
4222 PropertyDetails details = it->property_details();
4223 Handle<Object> old_value = it->factory()->the_hole_value();
4224 // Regular property update if the attributes match.
4225 if (details.attributes() == attributes) {
4226 return SetDataProperty(it, value);
4229 // Special case: properties of typed arrays cannot be reconfigured to
4230 // non-writable nor to non-enumerable.
4231 if (it->IsElement() && object->HasFixedTypedArrayElements()) {
4232 return RedefineNonconfigurableProperty(it->isolate(), it->GetName(),
4236 // Reconfigure the data property if the attributes mismatch.
4237 if (is_observed) old_value = it->GetDataValue();
4239 it->ReconfigureDataProperty(value, attributes);
4240 it->WriteDataValue(value);
4243 if (old_value->SameValue(*value)) {
4244 old_value = it->factory()->the_hole_value();
4246 RETURN_ON_EXCEPTION(it->isolate(),
4247 EnqueueChangeRecord(object, "reconfigure",
4248 it->GetName(), old_value),
4256 return AddDataProperty(it, value, attributes, STRICT,
4257 CERTAINLY_NOT_STORE_FROM_KEYED);
4261 MaybeHandle<Object> JSObject::SetOwnPropertyIgnoreAttributes(
4262 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4263 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4264 DCHECK(!value->IsTheHole());
4265 LookupIterator it(object, name, LookupIterator::OWN);
4266 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4270 MaybeHandle<Object> JSObject::SetOwnElementIgnoreAttributes(
4271 Handle<JSObject> object, uint32_t index, Handle<Object> value,
4272 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4273 Isolate* isolate = object->GetIsolate();
4274 LookupIterator it(isolate, object, index, LookupIterator::OWN);
4275 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4279 MaybeHandle<Object> JSObject::DefinePropertyOrElementIgnoreAttributes(
4280 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
4281 PropertyAttributes attributes, ExecutableAccessorInfoHandling handling) {
4282 Isolate* isolate = object->GetIsolate();
4283 LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name,
4284 LookupIterator::OWN);
4285 return DefineOwnPropertyIgnoreAttributes(&it, value, attributes, handling);
4289 Maybe<bool> JSObject::CreateDataProperty(LookupIterator* it,
4290 Handle<Object> value) {
4291 DCHECK(it->GetReceiver()->IsJSObject());
4292 Maybe<PropertyAttributes> maybe = JSReceiver::GetPropertyAttributes(it);
4293 if (maybe.IsNothing()) return Nothing<bool>();
4295 if (it->IsFound()) {
4296 if (!it->IsConfigurable()) return Just(false);
4298 if (!JSObject::cast(*it->GetReceiver())->IsExtensible()) return Just(false);
4301 RETURN_ON_EXCEPTION_VALUE(
4303 DefineOwnPropertyIgnoreAttributes(it, value, NONE, DONT_FORCE_FIELD),
4310 Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor(
4311 LookupIterator* it) {
4312 Isolate* isolate = it->isolate();
4313 // Make sure that the top context does not change when doing
4314 // callbacks or interceptor calls.
4315 AssertNoContextChange ncc(isolate);
4316 HandleScope scope(isolate);
4318 Handle<JSObject> holder = it->GetHolder<JSObject>();
4319 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
4320 if (!it->IsElement() && it->name()->IsSymbol() &&
4321 !interceptor->can_intercept_symbols()) {
4322 return Just(ABSENT);
4324 PropertyCallbackArguments args(isolate, interceptor->data(),
4325 *it->GetReceiver(), *holder);
4326 if (!interceptor->query()->IsUndefined()) {
4327 v8::Local<v8::Integer> result;
4328 if (it->IsElement()) {
4329 uint32_t index = it->index();
4330 v8::IndexedPropertyQueryCallback query =
4331 v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query());
4333 ApiIndexedPropertyAccess("interceptor-indexed-has", *holder, index));
4334 result = args.Call(query, index);
4336 Handle<Name> name = it->name();
4337 v8::GenericNamedPropertyQueryCallback query =
4338 v8::ToCData<v8::GenericNamedPropertyQueryCallback>(
4339 interceptor->query());
4341 ApiNamedPropertyAccess("interceptor-named-has", *holder, *name));
4342 result = args.Call(query, v8::Utils::ToLocal(name));
4344 if (!result.IsEmpty()) {
4345 DCHECK(result->IsInt32());
4346 return Just(static_cast<PropertyAttributes>(
4347 result->Int32Value(reinterpret_cast<v8::Isolate*>(isolate)
4348 ->GetCurrentContext()).FromJust()));
4350 } else if (!interceptor->getter()->IsUndefined()) {
4351 // TODO(verwaest): Use GetPropertyWithInterceptor?
4352 v8::Local<v8::Value> result;
4353 if (it->IsElement()) {
4354 uint32_t index = it->index();
4355 v8::IndexedPropertyGetterCallback getter =
4356 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
4357 LOG(isolate, ApiIndexedPropertyAccess("interceptor-indexed-get-has",
4359 result = args.Call(getter, index);
4361 Handle<Name> name = it->name();
4363 v8::GenericNamedPropertyGetterCallback getter =
4364 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
4365 interceptor->getter());
4367 ApiNamedPropertyAccess("interceptor-named-get-has", *holder, *name));
4368 result = args.Call(getter, v8::Utils::ToLocal(name));
4370 if (!result.IsEmpty()) return Just(DONT_ENUM);
4373 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<PropertyAttributes>());
4374 return Just(ABSENT);
4378 Maybe<PropertyAttributes> JSReceiver::GetPropertyAttributes(
4379 LookupIterator* it) {
4380 for (; it->IsFound(); it->Next()) {
4381 switch (it->state()) {
4382 case LookupIterator::NOT_FOUND:
4383 case LookupIterator::TRANSITION:
4385 case LookupIterator::JSPROXY:
4386 return JSProxy::GetPropertyAttributesWithHandler(
4387 it->GetHolder<JSProxy>(), it->GetReceiver(), it->GetName());
4388 case LookupIterator::INTERCEPTOR: {
4389 Maybe<PropertyAttributes> result =
4390 JSObject::GetPropertyAttributesWithInterceptor(it);
4391 if (!result.IsJust()) return result;
4392 if (result.FromJust() != ABSENT) return result;
4395 case LookupIterator::ACCESS_CHECK:
4396 if (it->HasAccess()) break;
4397 return JSObject::GetPropertyAttributesWithFailedAccessCheck(it);
4398 case LookupIterator::INTEGER_INDEXED_EXOTIC:
4399 return Just(ABSENT);
4400 case LookupIterator::ACCESSOR:
4401 case LookupIterator::DATA:
4402 return Just(it->property_details().attributes());
4405 return Just(ABSENT);
4409 Handle<NormalizedMapCache> NormalizedMapCache::New(Isolate* isolate) {
4410 Handle<FixedArray> array(
4411 isolate->factory()->NewFixedArray(kEntries, TENURED));
4412 return Handle<NormalizedMapCache>::cast(array);
4416 MaybeHandle<Map> NormalizedMapCache::Get(Handle<Map> fast_map,
4417 PropertyNormalizationMode mode) {
4418 DisallowHeapAllocation no_gc;
4419 Object* value = FixedArray::get(GetIndex(fast_map));
4420 if (!value->IsMap() ||
4421 !Map::cast(value)->EquivalentToForNormalization(*fast_map, mode)) {
4422 return MaybeHandle<Map>();
4424 return handle(Map::cast(value));
4428 void NormalizedMapCache::Set(Handle<Map> fast_map,
4429 Handle<Map> normalized_map) {
4430 DisallowHeapAllocation no_gc;
4431 DCHECK(normalized_map->is_dictionary_map());
4432 FixedArray::set(GetIndex(fast_map), *normalized_map);
4436 void NormalizedMapCache::Clear() {
4437 int entries = length();
4438 for (int i = 0; i != entries; i++) {
4444 void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object,
4446 Handle<Code> code) {
4447 Handle<Map> map(object->map());
4448 Map::UpdateCodeCache(map, name, code);
4452 void JSObject::NormalizeProperties(Handle<JSObject> object,
4453 PropertyNormalizationMode mode,
4454 int expected_additional_properties,
4455 const char* reason) {
4456 if (!object->HasFastProperties()) return;
4458 Handle<Map> map(object->map());
4459 Handle<Map> new_map = Map::Normalize(map, mode, reason);
4461 MigrateToMap(object, new_map, expected_additional_properties);
4465 void JSObject::MigrateFastToSlow(Handle<JSObject> object,
4466 Handle<Map> new_map,
4467 int expected_additional_properties) {
4468 // The global object is always normalized.
4469 DCHECK(!object->IsGlobalObject());
4470 // JSGlobalProxy must never be normalized
4471 DCHECK(!object->IsJSGlobalProxy());
4473 Isolate* isolate = object->GetIsolate();
4474 HandleScope scope(isolate);
4475 Handle<Map> map(object->map());
4477 // Allocate new content.
4478 int real_size = map->NumberOfOwnDescriptors();
4479 int property_count = real_size;
4480 if (expected_additional_properties > 0) {
4481 property_count += expected_additional_properties;
4483 property_count += 2; // Make space for two more properties.
4485 Handle<NameDictionary> dictionary =
4486 NameDictionary::New(isolate, property_count);
4488 Handle<DescriptorArray> descs(map->instance_descriptors());
4489 for (int i = 0; i < real_size; i++) {
4490 PropertyDetails details = descs->GetDetails(i);
4491 Handle<Name> key(descs->GetKey(i));
4492 switch (details.type()) {
4493 case DATA_CONSTANT: {
4494 Handle<Object> value(descs->GetConstant(i), isolate);
4495 PropertyDetails d(details.attributes(), DATA, i + 1,
4496 PropertyCellType::kNoCell);
4497 dictionary = NameDictionary::Add(dictionary, key, value, d);
4501 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4502 Handle<Object> value;
4503 if (object->IsUnboxedDoubleField(index)) {
4504 double old_value = object->RawFastDoublePropertyAt(index);
4505 value = isolate->factory()->NewHeapNumber(old_value);
4507 value = handle(object->RawFastPropertyAt(index), isolate);
4508 if (details.representation().IsDouble()) {
4509 DCHECK(value->IsMutableHeapNumber());
4510 Handle<HeapNumber> old = Handle<HeapNumber>::cast(value);
4511 value = isolate->factory()->NewHeapNumber(old->value());
4514 PropertyDetails d(details.attributes(), DATA, i + 1,
4515 PropertyCellType::kNoCell);
4516 dictionary = NameDictionary::Add(dictionary, key, value, d);
4520 FieldIndex index = FieldIndex::ForDescriptor(*map, i);
4521 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
4522 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4523 PropertyCellType::kNoCell);
4524 dictionary = NameDictionary::Add(dictionary, key, value, d);
4527 case ACCESSOR_CONSTANT: {
4528 Handle<Object> value(descs->GetCallbacksObject(i), isolate);
4529 PropertyDetails d(details.attributes(), ACCESSOR_CONSTANT, i + 1,
4530 PropertyCellType::kNoCell);
4531 dictionary = NameDictionary::Add(dictionary, key, value, d);
4537 // Copy the next enumeration index from instance descriptor.
4538 dictionary->SetNextEnumerationIndex(real_size + 1);
4540 // From here on we cannot fail and we shouldn't GC anymore.
4541 DisallowHeapAllocation no_allocation;
4543 // Resize the object in the heap if necessary.
4544 int new_instance_size = new_map->instance_size();
4545 int instance_size_delta = map->instance_size() - new_instance_size;
4546 DCHECK(instance_size_delta >= 0);
4548 if (instance_size_delta > 0) {
4549 Heap* heap = isolate->heap();
4550 heap->CreateFillerObjectAt(object->address() + new_instance_size,
4551 instance_size_delta);
4552 heap->AdjustLiveBytes(*object, -instance_size_delta,
4553 Heap::CONCURRENT_TO_SWEEPER);
4556 // We are storing the new map using release store after creating a filler for
4557 // the left-over space to avoid races with the sweeper thread.
4558 object->synchronized_set_map(*new_map);
4560 object->set_properties(*dictionary);
4562 // Ensure that in-object space of slow-mode object does not contain random
4564 int inobject_properties = new_map->GetInObjectProperties();
4565 for (int i = 0; i < inobject_properties; i++) {
4566 FieldIndex index = FieldIndex::ForPropertyIndex(*new_map, i);
4567 object->RawFastPropertyAtPut(index, Smi::FromInt(0));
4570 isolate->counters()->props_to_dictionary()->Increment();
4573 if (FLAG_trace_normalization) {
4574 OFStream os(stdout);
4575 os << "Object properties have been normalized:\n";
4582 void JSObject::MigrateSlowToFast(Handle<JSObject> object,
4583 int unused_property_fields,
4584 const char* reason) {
4585 if (object->HasFastProperties()) return;
4586 DCHECK(!object->IsGlobalObject());
4587 Isolate* isolate = object->GetIsolate();
4588 Factory* factory = isolate->factory();
4589 Handle<NameDictionary> dictionary(object->property_dictionary());
4591 // Make sure we preserve dictionary representation if there are too many
4593 int number_of_elements = dictionary->NumberOfElements();
4594 if (number_of_elements > kMaxNumberOfDescriptors) return;
4596 Handle<FixedArray> iteration_order;
4597 if (number_of_elements != dictionary->NextEnumerationIndex()) {
4599 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
4601 iteration_order = NameDictionary::BuildIterationIndicesArray(dictionary);
4604 int instance_descriptor_length = iteration_order->length();
4605 int number_of_fields = 0;
4607 // Compute the length of the instance descriptor.
4608 for (int i = 0; i < instance_descriptor_length; i++) {
4609 int index = Smi::cast(iteration_order->get(i))->value();
4610 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
4612 Object* value = dictionary->ValueAt(index);
4613 PropertyType type = dictionary->DetailsAt(index).type();
4614 if (type == DATA && !value->IsJSFunction()) {
4615 number_of_fields += 1;
4619 Handle<Map> old_map(object->map(), isolate);
4621 int inobject_props = old_map->GetInObjectProperties();
4623 // Allocate new map.
4624 Handle<Map> new_map = Map::CopyDropDescriptors(old_map);
4625 new_map->set_dictionary_map(false);
4627 UpdatePrototypeUserRegistration(old_map, new_map, isolate);
4630 if (FLAG_trace_maps) {
4631 PrintF("[TraceMaps: SlowToFast from= %p to= %p reason= %s ]\n",
4632 reinterpret_cast<void*>(*old_map), reinterpret_cast<void*>(*new_map),
4637 if (instance_descriptor_length == 0) {
4638 DisallowHeapAllocation no_gc;
4639 DCHECK_LE(unused_property_fields, inobject_props);
4640 // Transform the object.
4641 new_map->set_unused_property_fields(inobject_props);
4642 object->synchronized_set_map(*new_map);
4643 object->set_properties(isolate->heap()->empty_fixed_array());
4644 // Check that it really works.
4645 DCHECK(object->HasFastProperties());
4649 // Allocate the instance descriptor.
4650 Handle<DescriptorArray> descriptors = DescriptorArray::Allocate(
4651 isolate, instance_descriptor_length);
4653 int number_of_allocated_fields =
4654 number_of_fields + unused_property_fields - inobject_props;
4655 if (number_of_allocated_fields < 0) {
4656 // There is enough inobject space for all fields (including unused).
4657 number_of_allocated_fields = 0;
4658 unused_property_fields = inobject_props - number_of_fields;
4661 // Allocate the fixed array for the fields.
4662 Handle<FixedArray> fields = factory->NewFixedArray(
4663 number_of_allocated_fields);
4665 // Fill in the instance descriptor and the fields.
4666 int current_offset = 0;
4667 for (int i = 0; i < instance_descriptor_length; i++) {
4668 int index = Smi::cast(iteration_order->get(i))->value();
4669 Object* k = dictionary->KeyAt(index);
4670 DCHECK(dictionary->IsKey(k));
4672 Object* value = dictionary->ValueAt(index);
4674 if (k->IsSymbol()) {
4675 key = handle(Symbol::cast(k));
4677 // Ensure the key is a unique name before writing into the
4678 // instance descriptor.
4679 key = factory->InternalizeString(handle(String::cast(k)));
4682 PropertyDetails details = dictionary->DetailsAt(index);
4683 int enumeration_index = details.dictionary_index();
4684 PropertyType type = details.type();
4686 if (value->IsJSFunction()) {
4687 DataConstantDescriptor d(key, handle(value, isolate),
4688 details.attributes());
4689 descriptors->Set(enumeration_index - 1, &d);
4690 } else if (type == DATA) {
4691 if (current_offset < inobject_props) {
4692 object->InObjectPropertyAtPut(current_offset, value,
4693 UPDATE_WRITE_BARRIER);
4695 int offset = current_offset - inobject_props;
4696 fields->set(offset, value);
4698 DataDescriptor d(key, current_offset, details.attributes(),
4699 // TODO(verwaest): value->OptimalRepresentation();
4700 Representation::Tagged());
4701 current_offset += d.GetDetails().field_width_in_words();
4702 descriptors->Set(enumeration_index - 1, &d);
4703 } else if (type == ACCESSOR_CONSTANT) {
4704 AccessorConstantDescriptor d(key, handle(value, isolate),
4705 details.attributes());
4706 descriptors->Set(enumeration_index - 1, &d);
4711 DCHECK(current_offset == number_of_fields);
4713 descriptors->Sort();
4715 Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New(
4716 new_map, descriptors, descriptors->number_of_descriptors());
4718 DisallowHeapAllocation no_gc;
4719 new_map->InitializeDescriptors(*descriptors, *layout_descriptor);
4720 new_map->set_unused_property_fields(unused_property_fields);
4722 // Transform the object.
4723 object->synchronized_set_map(*new_map);
4725 object->set_properties(*fields);
4726 DCHECK(object->IsJSObject());
4728 // Check that it really works.
4729 DCHECK(object->HasFastProperties());
4733 void JSObject::ResetElements(Handle<JSObject> object) {
4734 Isolate* isolate = object->GetIsolate();
4735 CHECK(object->map() != isolate->heap()->sloppy_arguments_elements_map());
4736 if (object->map()->has_dictionary_elements()) {
4737 Handle<SeededNumberDictionary> new_elements =
4738 SeededNumberDictionary::New(isolate, 0);
4739 object->set_elements(*new_elements);
4741 object->set_elements(object->map()->GetInitialElements());
4746 static Handle<SeededNumberDictionary> CopyFastElementsToDictionary(
4747 Handle<FixedArrayBase> array, int length,
4748 Handle<SeededNumberDictionary> dictionary, bool used_as_prototype) {
4749 Isolate* isolate = array->GetIsolate();
4750 Factory* factory = isolate->factory();
4751 bool has_double_elements = array->IsFixedDoubleArray();
4752 for (int i = 0; i < length; i++) {
4753 Handle<Object> value;
4754 if (has_double_elements) {
4755 Handle<FixedDoubleArray> double_array =
4756 Handle<FixedDoubleArray>::cast(array);
4757 if (double_array->is_the_hole(i)) {
4758 value = factory->the_hole_value();
4760 value = factory->NewHeapNumber(double_array->get_scalar(i));
4763 value = handle(Handle<FixedArray>::cast(array)->get(i), isolate);
4765 if (!value->IsTheHole()) {
4766 PropertyDetails details = PropertyDetails::Empty();
4767 dictionary = SeededNumberDictionary::AddNumberEntry(
4768 dictionary, i, value, details, used_as_prototype);
4775 void JSObject::RequireSlowElements(SeededNumberDictionary* dictionary) {
4776 if (dictionary->requires_slow_elements()) return;
4777 dictionary->set_requires_slow_elements();
4778 // TODO(verwaest): Remove this hack.
4779 if (map()->is_prototype_map()) {
4780 GetHeap()->ClearAllKeyedStoreICs();
4785 Handle<SeededNumberDictionary> JSObject::GetNormalizedElementDictionary(
4786 Handle<JSObject> object, Handle<FixedArrayBase> elements) {
4787 DCHECK(!object->HasDictionaryElements());
4788 DCHECK(!object->HasSlowArgumentsElements());
4789 Isolate* isolate = object->GetIsolate();
4790 // Ensure that notifications fire if the array or object prototypes are
4792 isolate->UpdateArrayProtectorOnNormalizeElements(object);
4793 int length = object->IsJSArray()
4794 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
4795 : elements->length();
4796 int used = object->GetFastElementsUsage();
4797 Handle<SeededNumberDictionary> dictionary =
4798 SeededNumberDictionary::New(isolate, used);
4799 return CopyFastElementsToDictionary(elements, length, dictionary,
4800 object->map()->is_prototype_map());
4804 Handle<SeededNumberDictionary> JSObject::NormalizeElements(
4805 Handle<JSObject> object) {
4806 DCHECK(!object->HasFixedTypedArrayElements());
4807 Isolate* isolate = object->GetIsolate();
4809 // Find the backing store.
4810 Handle<FixedArrayBase> elements(object->elements(), isolate);
4811 bool is_arguments = object->HasSloppyArgumentsElements();
4813 FixedArray* parameter_map = FixedArray::cast(*elements);
4814 elements = handle(FixedArrayBase::cast(parameter_map->get(1)), isolate);
4817 if (elements->IsDictionary()) {
4818 return Handle<SeededNumberDictionary>::cast(elements);
4821 DCHECK(object->HasFastSmiOrObjectElements() ||
4822 object->HasFastDoubleElements() ||
4823 object->HasFastArgumentsElements());
4825 Handle<SeededNumberDictionary> dictionary =
4826 GetNormalizedElementDictionary(object, elements);
4828 // Switch to using the dictionary as the backing storage for elements.
4829 ElementsKind target_kind =
4830 is_arguments ? SLOW_SLOPPY_ARGUMENTS_ELEMENTS : DICTIONARY_ELEMENTS;
4831 Handle<Map> new_map = JSObject::GetElementsTransitionMap(object, target_kind);
4832 // Set the new map first to satify the elements type assert in set_elements().
4833 JSObject::MigrateToMap(object, new_map);
4836 FixedArray::cast(object->elements())->set(1, *dictionary);
4838 object->set_elements(*dictionary);
4841 isolate->counters()->elements_to_dictionary()->Increment();
4844 if (FLAG_trace_normalization) {
4845 OFStream os(stdout);
4846 os << "Object elements have been normalized:\n";
4851 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
4856 static Smi* GenerateIdentityHash(Isolate* isolate) {
4860 // Generate a random 32-bit hash value but limit range to fit
4862 hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue;
4864 } while (hash_value == 0 && attempts < 30);
4865 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
4867 return Smi::FromInt(hash_value);
4871 void JSObject::SetIdentityHash(Handle<JSObject> object, Handle<Smi> hash) {
4872 DCHECK(!object->IsJSGlobalProxy());
4873 Isolate* isolate = object->GetIsolate();
4874 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4875 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
4879 template<typename ProxyType>
4880 static Handle<Smi> GetOrCreateIdentityHashHelper(Handle<ProxyType> proxy) {
4881 Isolate* isolate = proxy->GetIsolate();
4883 Handle<Object> maybe_hash(proxy->hash(), isolate);
4884 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
4886 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
4887 proxy->set_hash(*hash);
4892 Object* JSObject::GetIdentityHash() {
4893 DisallowHeapAllocation no_gc;
4894 Isolate* isolate = GetIsolate();
4895 if (IsJSGlobalProxy()) {
4896 return JSGlobalProxy::cast(this)->hash();
4898 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4899 Handle<Object> stored_value =
4900 Object::GetPropertyOrElement(Handle<Object>(this, isolate),
4901 hash_code_symbol).ToHandleChecked();
4902 return stored_value->IsSmi() ? *stored_value
4903 : isolate->heap()->undefined_value();
4907 Handle<Smi> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) {
4908 if (object->IsJSGlobalProxy()) {
4909 return GetOrCreateIdentityHashHelper(Handle<JSGlobalProxy>::cast(object));
4912 Isolate* isolate = object->GetIsolate();
4914 Handle<Object> maybe_hash(object->GetIdentityHash(), isolate);
4915 if (maybe_hash->IsSmi()) return Handle<Smi>::cast(maybe_hash);
4917 Handle<Smi> hash(GenerateIdentityHash(isolate), isolate);
4918 Handle<Name> hash_code_symbol(isolate->heap()->hash_code_symbol());
4919 JSObject::AddProperty(object, hash_code_symbol, hash, NONE);
4924 Object* JSProxy::GetIdentityHash() {
4925 return this->hash();
4929 Handle<Smi> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) {
4930 return GetOrCreateIdentityHashHelper(proxy);
4934 Object* JSObject::GetHiddenProperty(Handle<Name> key) {
4935 DisallowHeapAllocation no_gc;
4936 DCHECK(key->IsUniqueName());
4937 if (IsJSGlobalProxy()) {
4938 // For a proxy, use the prototype as target object.
4939 PrototypeIterator iter(GetIsolate(), this);
4940 // If the proxy is detached, return undefined.
4941 if (iter.IsAtEnd()) return GetHeap()->the_hole_value();
4942 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
4943 return JSObject::cast(iter.GetCurrent())->GetHiddenProperty(key);
4945 DCHECK(!IsJSGlobalProxy());
4946 Object* inline_value = GetHiddenPropertiesHashTable();
4948 if (inline_value->IsUndefined()) return GetHeap()->the_hole_value();
4950 ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value);
4951 Object* entry = hashtable->Lookup(key);
4956 Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object,
4958 Handle<Object> value) {
4959 Isolate* isolate = object->GetIsolate();
4961 DCHECK(key->IsUniqueName());
4962 if (object->IsJSGlobalProxy()) {
4963 // For a proxy, use the prototype as target object.
4964 PrototypeIterator iter(isolate, object);
4965 // If the proxy is detached, return undefined.
4966 if (iter.IsAtEnd()) return isolate->factory()->undefined_value();
4967 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
4968 return SetHiddenProperty(
4969 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key,
4972 DCHECK(!object->IsJSGlobalProxy());
4974 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
4976 Handle<ObjectHashTable> hashtable =
4977 GetOrCreateHiddenPropertiesHashtable(object);
4979 // If it was found, check if the key is already in the dictionary.
4980 Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key,
4982 if (*new_table != *hashtable) {
4983 // If adding the key expanded the dictionary (i.e., Add returned a new
4984 // dictionary), store it back to the object.
4985 SetHiddenPropertiesHashTable(object, new_table);
4988 // Return this to mark success.
4993 void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) {
4994 Isolate* isolate = object->GetIsolate();
4995 DCHECK(key->IsUniqueName());
4997 if (object->IsJSGlobalProxy()) {
4998 PrototypeIterator iter(isolate, object);
4999 if (iter.IsAtEnd()) return;
5000 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5001 return DeleteHiddenProperty(
5002 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)), key);
5005 Object* inline_value = object->GetHiddenPropertiesHashTable();
5007 if (inline_value->IsUndefined()) return;
5009 Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value));
5010 bool was_present = false;
5011 ObjectHashTable::Remove(hashtable, key, &was_present);
5015 bool JSObject::HasHiddenProperties(Handle<JSObject> object) {
5016 Handle<Name> hidden = object->GetIsolate()->factory()->hidden_string();
5017 LookupIterator it(object, hidden, LookupIterator::OWN_SKIP_INTERCEPTOR);
5018 Maybe<PropertyAttributes> maybe = GetPropertyAttributes(&it);
5019 // Cannot get an exception since the hidden_string isn't accessible to JS.
5020 DCHECK(maybe.IsJust());
5021 return maybe.FromJust() != ABSENT;
5025 Object* JSObject::GetHiddenPropertiesHashTable() {
5026 DCHECK(!IsJSGlobalProxy());
5027 if (HasFastProperties()) {
5028 // If the object has fast properties, check whether the first slot
5029 // in the descriptor array matches the hidden string. Since the
5030 // hidden strings hash code is zero (and no other name has hash
5031 // code zero) it will always occupy the first entry if present.
5032 DescriptorArray* descriptors = this->map()->instance_descriptors();
5033 if (descriptors->number_of_descriptors() > 0) {
5034 int sorted_index = descriptors->GetSortedKeyIndex(0);
5035 if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() &&
5036 sorted_index < map()->NumberOfOwnDescriptors()) {
5037 DCHECK(descriptors->GetType(sorted_index) == DATA);
5038 DCHECK(descriptors->GetDetails(sorted_index).representation().
5039 IsCompatibleForLoad(Representation::Tagged()));
5040 FieldIndex index = FieldIndex::ForDescriptor(this->map(),
5042 return this->RawFastPropertyAt(index);
5044 return GetHeap()->undefined_value();
5047 return GetHeap()->undefined_value();
5050 Isolate* isolate = GetIsolate();
5051 LookupIterator it(handle(this), isolate->factory()->hidden_string(),
5052 LookupIterator::OWN_SKIP_INTERCEPTOR);
5053 // Access check is always skipped for the hidden string anyways.
5054 return *GetDataProperty(&it);
5058 Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable(
5059 Handle<JSObject> object) {
5060 Isolate* isolate = object->GetIsolate();
5062 static const int kInitialCapacity = 4;
5063 Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate);
5064 if (inline_value->IsHashTable()) {
5065 return Handle<ObjectHashTable>::cast(inline_value);
5068 Handle<ObjectHashTable> hashtable = ObjectHashTable::New(
5069 isolate, kInitialCapacity, USE_CUSTOM_MINIMUM_CAPACITY);
5071 DCHECK(inline_value->IsUndefined());
5072 SetHiddenPropertiesHashTable(object, hashtable);
5077 Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object,
5078 Handle<Object> value) {
5079 DCHECK(!object->IsJSGlobalProxy());
5080 Isolate* isolate = object->GetIsolate();
5081 Handle<Name> name = isolate->factory()->hidden_string();
5082 SetOwnPropertyIgnoreAttributes(object, name, value, DONT_ENUM).Assert();
5087 MaybeHandle<Object> JSObject::DeletePropertyWithInterceptor(
5088 LookupIterator* it) {
5089 Isolate* isolate = it->isolate();
5090 // Make sure that the top context does not change when doing callbacks or
5091 // interceptor calls.
5092 AssertNoContextChange ncc(isolate);
5094 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
5095 Handle<InterceptorInfo> interceptor(it->GetInterceptor());
5096 if (interceptor->deleter()->IsUndefined()) return MaybeHandle<Object>();
5098 Handle<JSObject> holder = it->GetHolder<JSObject>();
5100 PropertyCallbackArguments args(isolate, interceptor->data(),
5101 *it->GetReceiver(), *holder);
5102 v8::Local<v8::Boolean> result;
5103 if (it->IsElement()) {
5104 uint32_t index = it->index();
5105 v8::IndexedPropertyDeleterCallback deleter =
5106 v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter());
5108 ApiIndexedPropertyAccess("interceptor-indexed-delete", *holder, index));
5109 result = args.Call(deleter, index);
5110 } else if (it->name()->IsSymbol() && !interceptor->can_intercept_symbols()) {
5111 return MaybeHandle<Object>();
5113 Handle<Name> name = it->name();
5114 v8::GenericNamedPropertyDeleterCallback deleter =
5115 v8::ToCData<v8::GenericNamedPropertyDeleterCallback>(
5116 interceptor->deleter());
5118 ApiNamedPropertyAccess("interceptor-named-delete", *holder, *name));
5119 result = args.Call(deleter, v8::Utils::ToLocal(name));
5122 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5123 if (result.IsEmpty()) return MaybeHandle<Object>();
5125 DCHECK(result->IsBoolean());
5126 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
5127 result_internal->VerifyApiCallResultType();
5128 // Rebox CustomArguments::kReturnValueOffset before returning.
5129 return handle(*result_internal, isolate);
5133 void JSObject::DeleteNormalizedProperty(Handle<JSObject> object,
5134 Handle<Name> name, int entry) {
5135 DCHECK(!object->HasFastProperties());
5136 Isolate* isolate = object->GetIsolate();
5138 if (object->IsGlobalObject()) {
5139 // If we have a global object, invalidate the cell and swap in a new one.
5140 Handle<GlobalDictionary> dictionary(object->global_dictionary());
5141 DCHECK_NE(GlobalDictionary::kNotFound, entry);
5143 auto cell = PropertyCell::InvalidateEntry(dictionary, entry);
5144 cell->set_value(isolate->heap()->the_hole_value());
5145 // TODO(ishell): InvalidateForDelete
5146 cell->set_property_details(
5147 cell->property_details().set_cell_type(PropertyCellType::kInvalidated));
5149 Handle<NameDictionary> dictionary(object->property_dictionary());
5150 DCHECK_NE(NameDictionary::kNotFound, entry);
5152 NameDictionary::DeleteProperty(dictionary, entry);
5153 Handle<NameDictionary> new_properties =
5154 NameDictionary::Shrink(dictionary, name);
5155 object->set_properties(*new_properties);
5160 // ECMA-262, 3rd, 8.6.2.5
5161 MaybeHandle<Object> JSReceiver::DeleteProperty(LookupIterator* it,
5162 LanguageMode language_mode) {
5163 Isolate* isolate = it->isolate();
5164 if (it->state() == LookupIterator::JSPROXY) {
5165 return JSProxy::DeletePropertyWithHandler(it->GetHolder<JSProxy>(),
5166 it->GetName(), language_mode);
5169 Handle<JSObject> receiver = Handle<JSObject>::cast(it->GetReceiver());
5172 receiver->map()->is_observed() &&
5173 (it->IsElement() || !isolate->IsInternallyUsedPropertyName(it->name()));
5175 Handle<Object> old_value = it->factory()->the_hole_value();
5177 for (; it->IsFound(); it->Next()) {
5178 switch (it->state()) {
5179 case LookupIterator::JSPROXY:
5180 case LookupIterator::NOT_FOUND:
5181 case LookupIterator::TRANSITION:
5183 case LookupIterator::ACCESS_CHECK:
5184 if (it->HasAccess()) break;
5185 isolate->ReportFailedAccessCheck(it->GetHolder<JSObject>());
5186 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5187 return it->factory()->false_value();
5188 case LookupIterator::INTERCEPTOR: {
5189 MaybeHandle<Object> maybe_result =
5190 JSObject::DeletePropertyWithInterceptor(it);
5191 // Delete with interceptor succeeded. Return result.
5192 if (!maybe_result.is_null()) return maybe_result;
5193 // An exception was thrown in the interceptor. Propagate.
5194 if (isolate->has_pending_exception()) return maybe_result;
5197 case LookupIterator::INTEGER_INDEXED_EXOTIC:
5198 return it->factory()->true_value();
5199 case LookupIterator::DATA:
5201 old_value = it->GetDataValue();
5204 case LookupIterator::ACCESSOR: {
5205 if (!it->IsConfigurable() || receiver->map()->is_strong()) {
5206 // Fail if the property is not configurable, or on a strong object.
5207 if (is_strict(language_mode)) {
5208 MessageTemplate::Template templ =
5209 receiver->map()->is_strong()
5210 ? MessageTemplate::kStrongDeleteProperty
5211 : MessageTemplate::kStrictDeleteProperty;
5213 isolate, NewTypeError(templ, it->GetName(), receiver), Object);
5215 return it->factory()->false_value();
5221 RETURN_ON_EXCEPTION(isolate,
5222 JSObject::EnqueueChangeRecord(
5223 receiver, "delete", it->GetName(), old_value),
5227 return it->factory()->true_value();
5232 return it->factory()->true_value();
5236 MaybeHandle<Object> JSReceiver::DeleteElement(Handle<JSReceiver> object,
5238 LanguageMode language_mode) {
5239 LookupIterator it(object->GetIsolate(), object, index,
5240 LookupIterator::HIDDEN);
5241 return DeleteProperty(&it, language_mode);
5245 MaybeHandle<Object> JSReceiver::DeleteProperty(Handle<JSReceiver> object,
5247 LanguageMode language_mode) {
5248 LookupIterator it(object, name, LookupIterator::HIDDEN);
5249 return JSObject::DeleteProperty(&it, language_mode);
5253 MaybeHandle<Object> JSReceiver::DeletePropertyOrElement(
5254 Handle<JSReceiver> object, Handle<Name> name, LanguageMode language_mode) {
5255 LookupIterator it = LookupIterator::PropertyOrElement(
5256 name->GetIsolate(), object, name, LookupIterator::HIDDEN);
5257 return JSObject::DeleteProperty(&it, language_mode);
5261 bool JSObject::ReferencesObjectFromElements(FixedArray* elements,
5264 DCHECK(IsFastObjectElementsKind(kind) ||
5265 kind == DICTIONARY_ELEMENTS);
5266 if (IsFastObjectElementsKind(kind)) {
5267 int length = IsJSArray()
5268 ? Smi::cast(JSArray::cast(this)->length())->value()
5269 : elements->length();
5270 for (int i = 0; i < length; ++i) {
5271 Object* element = elements->get(i);
5272 if (!element->IsTheHole() && element == object) return true;
5276 SeededNumberDictionary::cast(elements)->SlowReverseLookup(object);
5277 if (!key->IsUndefined()) return true;
5283 // Check whether this object references another object.
5284 bool JSObject::ReferencesObject(Object* obj) {
5285 Map* map_of_this = map();
5286 Heap* heap = GetHeap();
5287 DisallowHeapAllocation no_allocation;
5289 // Is the object the constructor for this object?
5290 if (map_of_this->GetConstructor() == obj) {
5294 // Is the object the prototype for this object?
5295 if (map_of_this->prototype() == obj) {
5299 // Check if the object is among the named properties.
5300 Object* key = SlowReverseLookup(obj);
5301 if (!key->IsUndefined()) {
5305 // Check if the object is among the indexed properties.
5306 ElementsKind kind = GetElementsKind();
5308 // Raw pixels and external arrays do not reference other
5310 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5311 case TYPE##_ELEMENTS: \
5314 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5315 #undef TYPED_ARRAY_CASE
5317 case FAST_DOUBLE_ELEMENTS:
5318 case FAST_HOLEY_DOUBLE_ELEMENTS:
5320 case FAST_SMI_ELEMENTS:
5321 case FAST_HOLEY_SMI_ELEMENTS:
5324 case FAST_HOLEY_ELEMENTS:
5325 case DICTIONARY_ELEMENTS: {
5326 FixedArray* elements = FixedArray::cast(this->elements());
5327 if (ReferencesObjectFromElements(elements, kind, obj)) return true;
5330 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5331 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
5332 FixedArray* parameter_map = FixedArray::cast(elements());
5333 // Check the mapped parameters.
5334 int length = parameter_map->length();
5335 for (int i = 2; i < length; ++i) {
5336 Object* value = parameter_map->get(i);
5337 if (!value->IsTheHole() && value == obj) return true;
5339 // Check the arguments.
5340 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
5341 kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS :
5342 FAST_HOLEY_ELEMENTS;
5343 if (ReferencesObjectFromElements(arguments, kind, obj)) return true;
5348 // For functions check the context.
5349 if (IsJSFunction()) {
5350 // Get the constructor function for arguments array.
5351 Map* arguments_map =
5352 heap->isolate()->context()->native_context()->sloppy_arguments_map();
5353 JSFunction* arguments_function =
5354 JSFunction::cast(arguments_map->GetConstructor());
5356 // Get the context and don't check if it is the native context.
5357 JSFunction* f = JSFunction::cast(this);
5358 Context* context = f->context();
5359 if (context->IsNativeContext()) {
5363 // Check the non-special context slots.
5364 for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) {
5365 // Only check JS objects.
5366 if (context->get(i)->IsJSObject()) {
5367 JSObject* ctxobj = JSObject::cast(context->get(i));
5368 // If it is an arguments array check the content.
5369 if (ctxobj->map()->GetConstructor() == arguments_function) {
5370 if (ctxobj->ReferencesObject(obj)) {
5373 } else if (ctxobj == obj) {
5379 // Check the context extension (if any) if it can have references.
5380 if (context->has_extension() && !context->IsCatchContext()) {
5381 // With harmony scoping, a JSFunction may have a script context.
5382 // TODO(mvstanton): walk into the ScopeInfo.
5383 if (context->IsScriptContext()) {
5387 return context->extension_object()->ReferencesObject(obj);
5391 // No references to object.
5396 MaybeHandle<Object> JSObject::PreventExtensions(Handle<JSObject> object) {
5397 if (!object->map()->is_extensible()) return object;
5399 if (!object->HasSloppyArgumentsElements() && !object->map()->is_observed()) {
5400 return PreventExtensionsWithTransition<NONE>(object);
5403 Isolate* isolate = object->GetIsolate();
5405 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5406 isolate->ReportFailedAccessCheck(object);
5407 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5408 return isolate->factory()->false_value();
5411 if (object->IsJSGlobalProxy()) {
5412 PrototypeIterator iter(isolate, object);
5413 if (iter.IsAtEnd()) return object;
5414 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5415 return PreventExtensions(
5416 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5419 // It's not possible to seal objects with external array elements
5420 if (object->HasFixedTypedArrayElements()) {
5422 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5426 // If there are fast elements we normalize.
5427 Handle<SeededNumberDictionary> dictionary = NormalizeElements(object);
5428 DCHECK(object->HasDictionaryElements() || object->HasSlowArgumentsElements());
5430 // Make sure that we never go back to fast case.
5431 object->RequireSlowElements(*dictionary);
5433 // Do a map transition, other objects with this map may still
5435 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5436 Handle<Map> new_map = Map::Copy(handle(object->map()), "PreventExtensions");
5438 new_map->set_is_extensible(false);
5439 JSObject::MigrateToMap(object, new_map);
5440 DCHECK(!object->map()->is_extensible());
5442 if (object->map()->is_observed()) {
5443 RETURN_ON_EXCEPTION(
5445 EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(),
5446 isolate->factory()->the_hole_value()),
5453 bool JSObject::IsExtensible() {
5454 if (IsJSGlobalProxy()) {
5455 PrototypeIterator iter(GetIsolate(), this);
5456 if (iter.IsAtEnd()) return false;
5457 DCHECK(iter.GetCurrent()->IsJSGlobalObject());
5458 return JSObject::cast(iter.GetCurrent())->map()->is_extensible();
5460 return map()->is_extensible();
5464 template <typename Dictionary>
5465 static void ApplyAttributesToDictionary(Dictionary* dictionary,
5466 const PropertyAttributes attributes) {
5467 int capacity = dictionary->Capacity();
5468 for (int i = 0; i < capacity; i++) {
5469 Object* k = dictionary->KeyAt(i);
5470 if (dictionary->IsKey(k) &&
5471 !(k->IsSymbol() && Symbol::cast(k)->is_private())) {
5472 PropertyDetails details = dictionary->DetailsAt(i);
5473 int attrs = attributes;
5474 // READ_ONLY is an invalid attribute for JS setters/getters.
5475 if ((attributes & READ_ONLY) && details.type() == ACCESSOR_CONSTANT) {
5476 Object* v = dictionary->ValueAt(i);
5477 if (v->IsPropertyCell()) v = PropertyCell::cast(v)->value();
5478 if (v->IsAccessorPair()) attrs &= ~READ_ONLY;
5480 details = details.CopyAddAttributes(
5481 static_cast<PropertyAttributes>(attrs));
5482 dictionary->DetailsAtPut(i, details);
5488 template <PropertyAttributes attrs>
5489 MaybeHandle<Object> JSObject::PreventExtensionsWithTransition(
5490 Handle<JSObject> object) {
5491 STATIC_ASSERT(attrs == NONE || attrs == SEALED || attrs == FROZEN);
5493 // Sealing/freezing sloppy arguments should be handled elsewhere.
5494 DCHECK(!object->HasSloppyArgumentsElements());
5495 DCHECK(!object->map()->is_observed());
5497 Isolate* isolate = object->GetIsolate();
5498 if (object->IsAccessCheckNeeded() && !isolate->MayAccess(object)) {
5499 isolate->ReportFailedAccessCheck(object);
5500 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
5501 return isolate->factory()->false_value();
5504 if (object->IsJSGlobalProxy()) {
5505 PrototypeIterator iter(isolate, object);
5506 if (iter.IsAtEnd()) return object;
5507 DCHECK(PrototypeIterator::GetCurrent(iter)->IsJSGlobalObject());
5508 return PreventExtensionsWithTransition<attrs>(
5509 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter)));
5512 // It's not possible to seal or freeze objects with external array elements
5513 if (object->HasFixedTypedArrayElements()) {
5515 isolate, NewTypeError(MessageTemplate::kCannotPreventExtExternalArray),
5519 Handle<SeededNumberDictionary> new_element_dictionary;
5520 if (!object->HasDictionaryElements()) {
5523 ? Smi::cast(Handle<JSArray>::cast(object)->length())->value()
5524 : object->elements()->length();
5525 new_element_dictionary =
5526 length == 0 ? isolate->factory()->empty_slow_element_dictionary()
5527 : GetNormalizedElementDictionary(
5528 object, handle(object->elements()));
5531 Handle<Symbol> transition_marker;
5532 if (attrs == NONE) {
5533 transition_marker = isolate->factory()->nonextensible_symbol();
5534 } else if (attrs == SEALED) {
5535 transition_marker = isolate->factory()->sealed_symbol();
5537 DCHECK(attrs == FROZEN);
5538 transition_marker = isolate->factory()->frozen_symbol();
5541 Handle<Map> old_map(object->map(), isolate);
5543 TransitionArray::SearchSpecial(*old_map, *transition_marker);
5544 if (transition != NULL) {
5545 Handle<Map> transition_map(transition, isolate);
5546 DCHECK(transition_map->has_dictionary_elements());
5547 DCHECK(!transition_map->is_extensible());
5548 JSObject::MigrateToMap(object, transition_map);
5549 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5550 // Create a new descriptor array with the appropriate property attributes
5551 Handle<Map> new_map = Map::CopyForPreventExtensions(
5552 old_map, attrs, transition_marker, "CopyForPreventExtensions");
5553 JSObject::MigrateToMap(object, new_map);
5555 DCHECK(old_map->is_dictionary_map() || !old_map->is_prototype_map());
5556 // Slow path: need to normalize properties for safety
5557 NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0,
5558 "SlowPreventExtensions");
5560 // Create a new map, since other objects with this map may be extensible.
5561 // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps.
5562 Handle<Map> new_map =
5563 Map::Copy(handle(object->map()), "SlowCopyForPreventExtensions");
5564 new_map->set_is_extensible(false);
5565 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
5566 JSObject::MigrateToMap(object, new_map);
5568 if (attrs != NONE) {
5569 if (object->IsGlobalObject()) {
5570 ApplyAttributesToDictionary(object->global_dictionary(), attrs);
5572 ApplyAttributesToDictionary(object->property_dictionary(), attrs);
5577 DCHECK(object->map()->has_dictionary_elements());
5578 if (!new_element_dictionary.is_null()) {
5579 object->set_elements(*new_element_dictionary);
5582 if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) {
5583 SeededNumberDictionary* dictionary = object->element_dictionary();
5584 // Make sure we never go back to the fast case
5585 object->RequireSlowElements(dictionary);
5586 if (attrs != NONE) {
5587 ApplyAttributesToDictionary(dictionary, attrs);
5595 MaybeHandle<Object> JSObject::Freeze(Handle<JSObject> object) {
5596 return PreventExtensionsWithTransition<FROZEN>(object);
5600 MaybeHandle<Object> JSObject::Seal(Handle<JSObject> object) {
5601 return PreventExtensionsWithTransition<SEALED>(object);
5605 void JSObject::SetObserved(Handle<JSObject> object) {
5606 DCHECK(!object->IsJSGlobalProxy());
5607 DCHECK(!object->IsJSGlobalObject());
5608 Isolate* isolate = object->GetIsolate();
5609 Handle<Map> new_map;
5610 Handle<Map> old_map(object->map(), isolate);
5611 DCHECK(!old_map->is_observed());
5612 Map* transition = TransitionArray::SearchSpecial(
5613 *old_map, isolate->heap()->observed_symbol());
5614 if (transition != NULL) {
5615 new_map = handle(transition, isolate);
5616 DCHECK(new_map->is_observed());
5617 } else if (TransitionArray::CanHaveMoreTransitions(old_map)) {
5618 new_map = Map::CopyForObserved(old_map);
5620 new_map = Map::Copy(old_map, "SlowObserved");
5621 new_map->set_is_observed();
5623 JSObject::MigrateToMap(object, new_map);
5627 Handle<Object> JSObject::FastPropertyAt(Handle<JSObject> object,
5628 Representation representation,
5630 Isolate* isolate = object->GetIsolate();
5631 if (object->IsUnboxedDoubleField(index)) {
5632 double value = object->RawFastDoublePropertyAt(index);
5633 return isolate->factory()->NewHeapNumber(value);
5635 Handle<Object> raw_value(object->RawFastPropertyAt(index), isolate);
5636 return Object::WrapForRead(isolate, raw_value, representation);
5640 template<class ContextObject>
5641 class JSObjectWalkVisitor {
5643 JSObjectWalkVisitor(ContextObject* site_context, bool copying,
5644 JSObject::DeepCopyHints hints)
5645 : site_context_(site_context),
5649 MUST_USE_RESULT MaybeHandle<JSObject> StructureWalk(Handle<JSObject> object);
5652 MUST_USE_RESULT inline MaybeHandle<JSObject> VisitElementOrProperty(
5653 Handle<JSObject> object,
5654 Handle<JSObject> value) {
5655 Handle<AllocationSite> current_site = site_context()->EnterNewScope();
5656 MaybeHandle<JSObject> copy_of_value = StructureWalk(value);
5657 site_context()->ExitScope(current_site, value);
5658 return copy_of_value;
5661 inline ContextObject* site_context() { return site_context_; }
5662 inline Isolate* isolate() { return site_context()->isolate(); }
5664 inline bool copying() const { return copying_; }
5667 ContextObject* site_context_;
5668 const bool copying_;
5669 const JSObject::DeepCopyHints hints_;
5673 template <class ContextObject>
5674 MaybeHandle<JSObject> JSObjectWalkVisitor<ContextObject>::StructureWalk(
5675 Handle<JSObject> object) {
5676 Isolate* isolate = this->isolate();
5677 bool copying = this->copying();
5678 bool shallow = hints_ == JSObject::kObjectIsShallow;
5681 StackLimitCheck check(isolate);
5683 if (check.HasOverflowed()) {
5684 isolate->StackOverflow();
5685 return MaybeHandle<JSObject>();
5689 if (object->map()->is_deprecated()) {
5690 JSObject::MigrateInstance(object);
5693 Handle<JSObject> copy;
5695 Handle<AllocationSite> site_to_pass;
5696 if (site_context()->ShouldCreateMemento(object)) {
5697 site_to_pass = site_context()->current();
5699 copy = isolate->factory()->CopyJSObjectWithAllocationSite(
5700 object, site_to_pass);
5705 DCHECK(copying || copy.is_identical_to(object));
5707 ElementsKind kind = copy->GetElementsKind();
5708 if (copying && IsFastSmiOrObjectElementsKind(kind) &&
5709 FixedArray::cast(copy->elements())->map() ==
5710 isolate->heap()->fixed_cow_array_map()) {
5711 isolate->counters()->cow_arrays_created_runtime()->Increment();
5715 HandleScope scope(isolate);
5717 // Deep copy own properties.
5718 if (copy->HasFastProperties()) {
5719 Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors());
5720 int limit = copy->map()->NumberOfOwnDescriptors();
5721 for (int i = 0; i < limit; i++) {
5722 PropertyDetails details = descriptors->GetDetails(i);
5723 if (details.type() != DATA) continue;
5724 FieldIndex index = FieldIndex::ForDescriptor(copy->map(), i);
5725 if (object->IsUnboxedDoubleField(index)) {
5727 double value = object->RawFastDoublePropertyAt(index);
5728 copy->RawFastDoublePropertyAtPut(index, value);
5731 Handle<Object> value(object->RawFastPropertyAt(index), isolate);
5732 if (value->IsJSObject()) {
5733 ASSIGN_RETURN_ON_EXCEPTION(
5735 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5738 copy->FastPropertyAtPut(index, *value);
5742 Representation representation = details.representation();
5743 value = Object::NewStorageFor(isolate, value, representation);
5744 copy->FastPropertyAtPut(index, *value);
5750 Handle<FixedArray> names =
5751 isolate->factory()->NewFixedArray(copy->NumberOfOwnProperties());
5752 copy->GetOwnPropertyNames(*names, 0);
5753 for (int i = 0; i < names->length(); i++) {
5754 DCHECK(names->get(i)->IsString());
5755 Handle<String> key_string(String::cast(names->get(i)));
5756 Maybe<PropertyAttributes> maybe =
5757 JSReceiver::GetOwnPropertyAttributes(copy, key_string);
5758 DCHECK(maybe.IsJust());
5759 PropertyAttributes attributes = maybe.FromJust();
5760 // Only deep copy fields from the object literal expression.
5761 // In particular, don't try to copy the length attribute of
5763 if (attributes != NONE) continue;
5764 Handle<Object> value =
5765 Object::GetProperty(copy, key_string).ToHandleChecked();
5766 if (value->IsJSObject()) {
5767 Handle<JSObject> result;
5768 ASSIGN_RETURN_ON_EXCEPTION(
5770 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5773 // Creating object copy for literals. No strict mode needed.
5774 JSObject::SetProperty(copy, key_string, result, SLOPPY).Assert();
5780 // Deep copy own elements.
5781 // Pixel elements cannot be created using an object literal.
5782 DCHECK(!copy->HasFixedTypedArrayElements());
5784 case FAST_SMI_ELEMENTS:
5786 case FAST_HOLEY_SMI_ELEMENTS:
5787 case FAST_HOLEY_ELEMENTS: {
5788 Handle<FixedArray> elements(FixedArray::cast(copy->elements()));
5789 if (elements->map() == isolate->heap()->fixed_cow_array_map()) {
5791 for (int i = 0; i < elements->length(); i++) {
5792 DCHECK(!elements->get(i)->IsJSObject());
5796 for (int i = 0; i < elements->length(); i++) {
5797 Handle<Object> value(elements->get(i), isolate);
5798 DCHECK(value->IsSmi() ||
5799 value->IsTheHole() ||
5800 (IsFastObjectElementsKind(copy->GetElementsKind())));
5801 if (value->IsJSObject()) {
5802 Handle<JSObject> result;
5803 ASSIGN_RETURN_ON_EXCEPTION(
5805 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5808 elements->set(i, *result);
5815 case DICTIONARY_ELEMENTS: {
5816 Handle<SeededNumberDictionary> element_dictionary(
5817 copy->element_dictionary());
5818 int capacity = element_dictionary->Capacity();
5819 for (int i = 0; i < capacity; i++) {
5820 Object* k = element_dictionary->KeyAt(i);
5821 if (element_dictionary->IsKey(k)) {
5822 Handle<Object> value(element_dictionary->ValueAt(i), isolate);
5823 if (value->IsJSObject()) {
5824 Handle<JSObject> result;
5825 ASSIGN_RETURN_ON_EXCEPTION(
5827 VisitElementOrProperty(copy, Handle<JSObject>::cast(value)),
5830 element_dictionary->ValueAtPut(i, *result);
5837 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
5838 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
5843 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
5844 case TYPE##_ELEMENTS: \
5846 TYPED_ARRAYS(TYPED_ARRAY_CASE)
5847 #undef TYPED_ARRAY_CASE
5849 case FAST_DOUBLE_ELEMENTS:
5850 case FAST_HOLEY_DOUBLE_ELEMENTS:
5851 // No contained objects, nothing to do.
5860 MaybeHandle<JSObject> JSObject::DeepWalk(
5861 Handle<JSObject> object,
5862 AllocationSiteCreationContext* site_context) {
5863 JSObjectWalkVisitor<AllocationSiteCreationContext> v(site_context, false,
5865 MaybeHandle<JSObject> result = v.StructureWalk(object);
5866 Handle<JSObject> for_assert;
5867 DCHECK(!result.ToHandle(&for_assert) || for_assert.is_identical_to(object));
5872 MaybeHandle<JSObject> JSObject::DeepCopy(
5873 Handle<JSObject> object,
5874 AllocationSiteUsageContext* site_context,
5875 DeepCopyHints hints) {
5876 JSObjectWalkVisitor<AllocationSiteUsageContext> v(site_context, true, hints);
5877 MaybeHandle<JSObject> copy = v.StructureWalk(object);
5878 Handle<JSObject> for_assert;
5879 DCHECK(!copy.ToHandle(&for_assert) || !for_assert.is_identical_to(object));
5884 // Tests for the fast common case for property enumeration:
5885 // - This object and all prototypes has an enum cache (which means that
5886 // it is no proxy, has no interceptors and needs no access checks).
5887 // - This object has no elements.
5888 // - No prototype has enumerable properties/elements.
5889 bool JSReceiver::IsSimpleEnum() {
5890 for (PrototypeIterator iter(GetIsolate(), this,
5891 PrototypeIterator::START_AT_RECEIVER);
5892 !iter.IsAtEnd(); iter.Advance()) {
5893 if (!iter.GetCurrent()->IsJSObject()) return false;
5894 JSObject* curr = JSObject::cast(iter.GetCurrent());
5895 int enum_length = curr->map()->EnumLength();
5896 if (enum_length == kInvalidEnumCacheSentinel) return false;
5897 if (curr->IsAccessCheckNeeded()) return false;
5898 DCHECK(!curr->HasNamedInterceptor());
5899 DCHECK(!curr->HasIndexedInterceptor());
5900 if (curr->NumberOfEnumElements() > 0) return false;
5901 if (curr != this && enum_length != 0) return false;
5907 static bool FilterKey(Object* key, PropertyAttributes filter) {
5908 if ((filter & SYMBOLIC) && key->IsSymbol()) {
5912 if ((filter & PRIVATE_SYMBOL) &&
5913 key->IsSymbol() && Symbol::cast(key)->is_private()) {
5917 if ((filter & STRING) && !key->IsSymbol()) {
5925 int Map::NumberOfDescribedProperties(DescriptorFlag which,
5926 PropertyAttributes filter) {
5928 DescriptorArray* descs = instance_descriptors();
5929 int limit = which == ALL_DESCRIPTORS
5930 ? descs->number_of_descriptors()
5931 : NumberOfOwnDescriptors();
5932 for (int i = 0; i < limit; i++) {
5933 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
5934 !FilterKey(descs->GetKey(i), filter)) {
5942 int Map::NextFreePropertyIndex() {
5944 int number_of_own_descriptors = NumberOfOwnDescriptors();
5945 DescriptorArray* descs = instance_descriptors();
5946 for (int i = 0; i < number_of_own_descriptors; i++) {
5947 PropertyDetails details = descs->GetDetails(i);
5948 if (details.location() == kField) {
5949 int candidate = details.field_index() + details.field_width_in_words();
5950 if (candidate > free_index) free_index = candidate;
5957 static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
5958 int len = array->length();
5959 for (int i = 0; i < len; i++) {
5960 Object* e = array->get(i);
5961 if (!(e->IsName() || e->IsNumber())) return false;
5967 static Handle<FixedArray> ReduceFixedArrayTo(
5968 Handle<FixedArray> array, int length) {
5969 DCHECK(array->length() >= length);
5970 if (array->length() == length) return array;
5972 Handle<FixedArray> new_array =
5973 array->GetIsolate()->factory()->NewFixedArray(length);
5974 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i));
5979 Handle<FixedArray> JSObject::GetEnumPropertyKeys(Handle<JSObject> object,
5980 bool cache_result) {
5981 Isolate* isolate = object->GetIsolate();
5982 if (object->HasFastProperties()) {
5983 int own_property_count = object->map()->EnumLength();
5984 // If the enum length of the given map is set to kInvalidEnumCache, this
5985 // means that the map itself has never used the present enum cache. The
5986 // first step to using the cache is to set the enum length of the map by
5987 // counting the number of own descriptors that are not DONT_ENUM or
5989 if (own_property_count == kInvalidEnumCacheSentinel) {
5990 own_property_count = object->map()->NumberOfDescribedProperties(
5991 OWN_DESCRIPTORS, DONT_SHOW);
5993 DCHECK(own_property_count == object->map()->NumberOfDescribedProperties(
5994 OWN_DESCRIPTORS, DONT_SHOW));
5997 if (object->map()->instance_descriptors()->HasEnumCache()) {
5998 DescriptorArray* desc = object->map()->instance_descriptors();
5999 Handle<FixedArray> keys(desc->GetEnumCache(), isolate);
6001 // In case the number of properties required in the enum are actually
6002 // present, we can reuse the enum cache. Otherwise, this means that the
6003 // enum cache was generated for a previous (smaller) version of the
6004 // Descriptor Array. In that case we regenerate the enum cache.
6005 if (own_property_count <= keys->length()) {
6006 if (cache_result) object->map()->SetEnumLength(own_property_count);
6007 isolate->counters()->enum_cache_hits()->Increment();
6008 return ReduceFixedArrayTo(keys, own_property_count);
6012 Handle<Map> map(object->map());
6014 if (map->instance_descriptors()->IsEmpty()) {
6015 isolate->counters()->enum_cache_hits()->Increment();
6016 if (cache_result) map->SetEnumLength(0);
6017 return isolate->factory()->empty_fixed_array();
6020 isolate->counters()->enum_cache_misses()->Increment();
6022 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(
6023 own_property_count);
6024 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(
6025 own_property_count);
6027 Handle<DescriptorArray> descs =
6028 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
6030 int size = map->NumberOfOwnDescriptors();
6033 for (int i = 0; i < size; i++) {
6034 PropertyDetails details = descs->GetDetails(i);
6035 Object* key = descs->GetKey(i);
6036 if (!(details.IsDontEnum() || key->IsSymbol())) {
6037 storage->set(index, key);
6038 if (!indices.is_null()) {
6039 if (details.type() != DATA) {
6040 indices = Handle<FixedArray>();
6042 FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
6043 int load_by_field_index = field_index.GetLoadByFieldIndex();
6044 indices->set(index, Smi::FromInt(load_by_field_index));
6050 DCHECK(index == storage->length());
6052 Handle<FixedArray> bridge_storage =
6053 isolate->factory()->NewFixedArray(
6054 DescriptorArray::kEnumCacheBridgeLength);
6055 DescriptorArray* desc = object->map()->instance_descriptors();
6056 desc->SetEnumCache(*bridge_storage,
6058 indices.is_null() ? Object::cast(Smi::FromInt(0))
6059 : Object::cast(*indices));
6061 object->map()->SetEnumLength(own_property_count);
6064 } else if (object->IsGlobalObject()) {
6065 Handle<GlobalDictionary> dictionary(object->global_dictionary());
6066 int length = dictionary->NumberOfEnumElements();
6068 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6070 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6071 dictionary->CopyEnumKeysTo(*storage);
6074 Handle<NameDictionary> dictionary(object->property_dictionary());
6075 int length = dictionary->NumberOfEnumElements();
6077 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
6079 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
6080 dictionary->CopyEnumKeysTo(*storage);
6086 MaybeHandle<FixedArray> JSReceiver::GetKeys(Handle<JSReceiver> object,
6087 KeyCollectionType type) {
6088 USE(ContainsOnlyValidKeys);
6089 Isolate* isolate = object->GetIsolate();
6090 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
6091 Handle<JSFunction> arguments_function(
6092 JSFunction::cast(isolate->sloppy_arguments_map()->GetConstructor()));
6094 PrototypeIterator::WhereToEnd end = type == OWN_ONLY
6095 ? PrototypeIterator::END_AT_NON_HIDDEN
6096 : PrototypeIterator::END_AT_NULL;
6097 // Only collect keys if access is permitted.
6098 for (PrototypeIterator iter(isolate, object,
6099 PrototypeIterator::START_AT_RECEIVER);
6100 !iter.IsAtEnd(end); iter.Advance()) {
6101 if (PrototypeIterator::GetCurrent(iter)->IsJSProxy()) {
6102 Handle<JSProxy> proxy(JSProxy::cast(*PrototypeIterator::GetCurrent(iter)),
6104 Handle<Object> args[] = { proxy };
6105 Handle<Object> names;
6106 ASSIGN_RETURN_ON_EXCEPTION(
6108 Execution::Call(isolate,
6109 isolate->proxy_enumerate(),
6114 ASSIGN_RETURN_ON_EXCEPTION(
6116 FixedArray::AddKeysFromArrayLike(
6117 content, Handle<JSObject>::cast(names)),
6122 Handle<JSObject> current =
6123 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
6125 // Check access rights if required.
6126 if (current->IsAccessCheckNeeded() && !isolate->MayAccess(current)) {
6127 if (iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
6128 isolate->ReportFailedAccessCheck(current);
6129 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, FixedArray);
6134 // Compute the element keys.
6135 Handle<FixedArray> element_keys =
6136 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
6137 current->GetEnumElementKeys(*element_keys);
6138 ASSIGN_RETURN_ON_EXCEPTION(
6140 FixedArray::UnionOfKeys(content, element_keys),
6142 DCHECK(ContainsOnlyValidKeys(content));
6144 // Add the element keys from the interceptor.
6145 if (current->HasIndexedInterceptor()) {
6146 Handle<JSObject> result;
6147 if (JSObject::GetKeysForIndexedInterceptor(
6148 current, object).ToHandle(&result)) {
6149 ASSIGN_RETURN_ON_EXCEPTION(
6151 FixedArray::AddKeysFromArrayLike(content, result),
6154 DCHECK(ContainsOnlyValidKeys(content));
6157 // We can cache the computed property keys if access checks are
6158 // not needed and no interceptors are involved.
6160 // We do not use the cache if the object has elements and
6161 // therefore it does not make sense to cache the property names
6162 // for arguments objects. Arguments objects will always have
6164 // Wrapped strings have elements, but don't have an elements
6165 // array or dictionary. So the fast inline test for whether to
6166 // use the cache says yes, so we should not create a cache.
6167 bool cache_enum_keys =
6168 ((current->map()->GetConstructor() != *arguments_function) &&
6169 !current->IsJSValue() && !current->IsAccessCheckNeeded() &&
6170 !current->HasNamedInterceptor() && !current->HasIndexedInterceptor());
6171 // Compute the property keys and cache them if possible.
6172 ASSIGN_RETURN_ON_EXCEPTION(
6174 FixedArray::UnionOfKeys(
6175 content, JSObject::GetEnumPropertyKeys(current, cache_enum_keys)),
6177 DCHECK(ContainsOnlyValidKeys(content));
6179 // Add the non-symbol property keys from the interceptor.
6180 if (current->HasNamedInterceptor()) {
6181 Handle<JSObject> result;
6182 if (JSObject::GetKeysForNamedInterceptor(
6183 current, object).ToHandle(&result)) {
6184 ASSIGN_RETURN_ON_EXCEPTION(
6185 isolate, content, FixedArray::AddKeysFromArrayLike(
6186 content, result, FixedArray::NON_SYMBOL_KEYS),
6189 DCHECK(ContainsOnlyValidKeys(content));
6196 bool Map::DictionaryElementsInPrototypeChainOnly() {
6197 if (IsDictionaryElementsKind(elements_kind())) {
6201 for (PrototypeIterator iter(this); !iter.IsAtEnd(); iter.Advance()) {
6202 // Be conservative, don't walk into proxies.
6203 if (iter.GetCurrent()->IsJSProxy()) return true;
6204 // String wrappers have non-configurable, non-writable elements.
6205 if (iter.GetCurrent()->IsStringWrapper()) return true;
6206 JSObject* current = JSObject::cast(iter.GetCurrent());
6208 if (current->HasDictionaryElements() &&
6209 current->element_dictionary()->requires_slow_elements()) {
6213 if (current->HasSlowArgumentsElements()) {
6214 FixedArray* parameter_map = FixedArray::cast(current->elements());
6215 Object* arguments = parameter_map->get(1);
6216 if (SeededNumberDictionary::cast(arguments)->requires_slow_elements()) {
6226 MaybeHandle<Object> JSObject::DefineAccessor(Handle<JSObject> object,
6228 Handle<Object> getter,
6229 Handle<Object> setter,
6230 PropertyAttributes attributes) {
6231 Isolate* isolate = object->GetIsolate();
6233 LookupIterator it = LookupIterator::PropertyOrElement(
6234 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6236 if (it.state() == LookupIterator::ACCESS_CHECK) {
6237 if (!it.HasAccess()) {
6238 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6239 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6240 return isolate->factory()->undefined_value();
6245 // Ignore accessors on typed arrays.
6246 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6247 return it.factory()->undefined_value();
6250 Handle<Object> old_value = isolate->factory()->the_hole_value();
6251 bool is_observed = object->map()->is_observed() &&
6252 !isolate->IsInternallyUsedPropertyName(name);
6253 bool preexists = false;
6255 CHECK(GetPropertyAttributes(&it).IsJust());
6256 preexists = it.IsFound();
6257 if (preexists && (it.state() == LookupIterator::DATA ||
6258 it.GetAccessors()->IsAccessorInfo())) {
6259 old_value = GetProperty(&it).ToHandleChecked();
6263 DCHECK(getter->IsSpecFunction() || getter->IsUndefined() || getter->IsNull());
6264 DCHECK(setter->IsSpecFunction() || setter->IsUndefined() || setter->IsNull());
6265 // At least one of the accessors needs to be a new value.
6266 DCHECK(!getter->IsNull() || !setter->IsNull());
6267 if (!getter->IsNull()) {
6268 it.TransitionToAccessorProperty(ACCESSOR_GETTER, getter, attributes);
6270 if (!setter->IsNull()) {
6271 it.TransitionToAccessorProperty(ACCESSOR_SETTER, setter, attributes);
6275 // Make sure the top context isn't changed.
6276 AssertNoContextChange ncc(isolate);
6277 const char* type = preexists ? "reconfigure" : "add";
6278 RETURN_ON_EXCEPTION(
6279 isolate, EnqueueChangeRecord(object, type, name, old_value), Object);
6282 return isolate->factory()->undefined_value();
6286 MaybeHandle<Object> JSObject::SetAccessor(Handle<JSObject> object,
6287 Handle<AccessorInfo> info) {
6288 Isolate* isolate = object->GetIsolate();
6289 Handle<Name> name(Name::cast(info->name()), isolate);
6291 LookupIterator it = LookupIterator::PropertyOrElement(
6292 isolate, object, name, LookupIterator::HIDDEN_SKIP_INTERCEPTOR);
6294 // Duplicate ACCESS_CHECK outside of GetPropertyAttributes for the case that
6295 // the FailedAccessCheckCallbackFunction doesn't throw an exception.
6297 // TODO(verwaest): Force throw an exception if the callback doesn't, so we can
6298 // remove reliance on default return values.
6299 if (it.state() == LookupIterator::ACCESS_CHECK) {
6300 if (!it.HasAccess()) {
6301 isolate->ReportFailedAccessCheck(object);
6302 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6303 return it.factory()->undefined_value();
6308 // Ignore accessors on typed arrays.
6309 if (it.IsElement() && object->HasFixedTypedArrayElements()) {
6310 return it.factory()->undefined_value();
6313 CHECK(GetPropertyAttributes(&it).IsJust());
6315 // ES5 forbids turning a property into an accessor if it's not
6316 // configurable. See 8.6.1 (Table 5).
6317 if (it.IsFound() && !it.IsConfigurable()) {
6318 return it.factory()->undefined_value();
6321 it.TransitionToAccessorPair(info, info->property_attributes());
6327 MaybeHandle<Object> JSObject::GetAccessor(Handle<JSObject> object,
6329 AccessorComponent component) {
6330 Isolate* isolate = object->GetIsolate();
6332 // Make sure that the top context does not change when doing callbacks or
6333 // interceptor calls.
6334 AssertNoContextChange ncc(isolate);
6336 LookupIterator it = LookupIterator::PropertyOrElement(
6337 isolate, object, name, LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
6339 for (; it.IsFound(); it.Next()) {
6340 switch (it.state()) {
6341 case LookupIterator::INTERCEPTOR:
6342 case LookupIterator::NOT_FOUND:
6343 case LookupIterator::TRANSITION:
6346 case LookupIterator::ACCESS_CHECK:
6347 if (it.HasAccess()) continue;
6348 isolate->ReportFailedAccessCheck(it.GetHolder<JSObject>());
6349 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
6350 return isolate->factory()->undefined_value();
6352 case LookupIterator::JSPROXY:
6353 return isolate->factory()->undefined_value();
6355 case LookupIterator::INTEGER_INDEXED_EXOTIC:
6356 return isolate->factory()->undefined_value();
6357 case LookupIterator::DATA:
6359 case LookupIterator::ACCESSOR: {
6360 Handle<Object> maybe_pair = it.GetAccessors();
6361 if (maybe_pair->IsAccessorPair()) {
6363 AccessorPair::cast(*maybe_pair)->GetComponent(component),
6370 return isolate->factory()->undefined_value();
6374 Object* JSObject::SlowReverseLookup(Object* value) {
6375 if (HasFastProperties()) {
6376 int number_of_own_descriptors = map()->NumberOfOwnDescriptors();
6377 DescriptorArray* descs = map()->instance_descriptors();
6378 bool value_is_number = value->IsNumber();
6379 for (int i = 0; i < number_of_own_descriptors; i++) {
6380 if (descs->GetType(i) == DATA) {
6381 FieldIndex field_index = FieldIndex::ForDescriptor(map(), i);
6382 if (IsUnboxedDoubleField(field_index)) {
6383 if (value_is_number) {
6384 double property = RawFastDoublePropertyAt(field_index);
6385 if (property == value->Number()) {
6386 return descs->GetKey(i);
6390 Object* property = RawFastPropertyAt(field_index);
6391 if (field_index.is_double()) {
6392 DCHECK(property->IsMutableHeapNumber());
6393 if (value_is_number && property->Number() == value->Number()) {
6394 return descs->GetKey(i);
6396 } else if (property == value) {
6397 return descs->GetKey(i);
6400 } else if (descs->GetType(i) == DATA_CONSTANT) {
6401 if (descs->GetConstant(i) == value) {
6402 return descs->GetKey(i);
6406 return GetHeap()->undefined_value();
6407 } else if (IsGlobalObject()) {
6408 return global_dictionary()->SlowReverseLookup(value);
6410 return property_dictionary()->SlowReverseLookup(value);
6415 Handle<Map> Map::RawCopy(Handle<Map> map, int instance_size) {
6416 Isolate* isolate = map->GetIsolate();
6417 Handle<Map> result =
6418 isolate->factory()->NewMap(map->instance_type(), instance_size);
6419 Handle<Object> prototype(map->prototype(), isolate);
6420 Map::SetPrototype(result, prototype);
6421 result->set_constructor_or_backpointer(map->GetConstructor());
6422 result->set_bit_field(map->bit_field());
6423 result->set_bit_field2(map->bit_field2());
6424 int new_bit_field3 = map->bit_field3();
6425 new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true);
6426 new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0);
6427 new_bit_field3 = EnumLengthBits::update(new_bit_field3,
6428 kInvalidEnumCacheSentinel);
6429 new_bit_field3 = Deprecated::update(new_bit_field3, false);
6430 if (!map->is_dictionary_map()) {
6431 new_bit_field3 = IsUnstable::update(new_bit_field3, false);
6433 new_bit_field3 = Counter::update(new_bit_field3, kRetainingCounterStart);
6434 result->set_bit_field3(new_bit_field3);
6439 Handle<Map> Map::Normalize(Handle<Map> fast_map, PropertyNormalizationMode mode,
6440 const char* reason) {
6441 DCHECK(!fast_map->is_dictionary_map());
6443 Isolate* isolate = fast_map->GetIsolate();
6444 Handle<Object> maybe_cache(isolate->native_context()->normalized_map_cache(),
6446 bool use_cache = !fast_map->is_prototype_map() && !maybe_cache->IsUndefined();
6447 Handle<NormalizedMapCache> cache;
6448 if (use_cache) cache = Handle<NormalizedMapCache>::cast(maybe_cache);
6450 Handle<Map> new_map;
6451 if (use_cache && cache->Get(fast_map, mode).ToHandle(&new_map)) {
6453 if (FLAG_verify_heap) new_map->DictionaryMapVerify();
6455 #ifdef ENABLE_SLOW_DCHECKS
6456 if (FLAG_enable_slow_asserts) {
6457 // The cached map should match newly created normalized map bit-by-bit,
6458 // except for the code cache, which can contain some ics which can be
6459 // applied to the shared map, dependent code and weak cell cache.
6460 Handle<Map> fresh = Map::CopyNormalized(fast_map, mode);
6462 if (new_map->is_prototype_map()) {
6463 // For prototype maps, the PrototypeInfo is not copied.
6464 DCHECK(memcmp(fresh->address(), new_map->address(),
6465 kTransitionsOrPrototypeInfoOffset) == 0);
6466 DCHECK(fresh->raw_transitions() == Smi::FromInt(0));
6467 STATIC_ASSERT(kDescriptorsOffset ==
6468 kTransitionsOrPrototypeInfoOffset + kPointerSize);
6469 DCHECK(memcmp(HeapObject::RawField(*fresh, kDescriptorsOffset),
6470 HeapObject::RawField(*new_map, kDescriptorsOffset),
6471 kCodeCacheOffset - kDescriptorsOffset) == 0);
6473 DCHECK(memcmp(fresh->address(), new_map->address(),
6474 Map::kCodeCacheOffset) == 0);
6476 STATIC_ASSERT(Map::kDependentCodeOffset ==
6477 Map::kCodeCacheOffset + kPointerSize);
6478 STATIC_ASSERT(Map::kWeakCellCacheOffset ==
6479 Map::kDependentCodeOffset + kPointerSize);
6480 int offset = Map::kWeakCellCacheOffset + kPointerSize;
6481 DCHECK(memcmp(fresh->address() + offset,
6482 new_map->address() + offset,
6483 Map::kSize - offset) == 0);
6487 new_map = Map::CopyNormalized(fast_map, mode);
6489 cache->Set(fast_map, new_map);
6490 isolate->counters()->normalized_maps()->Increment();
6493 if (FLAG_trace_maps) {
6494 PrintF("[TraceMaps: Normalize from= %p to= %p reason= %s ]\n",
6495 reinterpret_cast<void*>(*fast_map),
6496 reinterpret_cast<void*>(*new_map), reason);
6500 fast_map->NotifyLeafMapLayoutChange();
6505 Handle<Map> Map::CopyNormalized(Handle<Map> map,
6506 PropertyNormalizationMode mode) {
6507 int new_instance_size = map->instance_size();
6508 if (mode == CLEAR_INOBJECT_PROPERTIES) {
6509 new_instance_size -= map->GetInObjectProperties() * kPointerSize;
6512 Handle<Map> result = RawCopy(map, new_instance_size);
6514 if (mode != CLEAR_INOBJECT_PROPERTIES) {
6515 result->SetInObjectProperties(map->GetInObjectProperties());
6518 result->set_dictionary_map(true);
6519 result->set_migration_target(false);
6522 if (FLAG_verify_heap) result->DictionaryMapVerify();
6529 Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) {
6530 Handle<Map> result = RawCopy(map, map->instance_size());
6532 // Please note instance_type and instance_size are set when allocated.
6533 result->SetInObjectProperties(map->GetInObjectProperties());
6534 result->set_unused_property_fields(map->unused_property_fields());
6536 result->ClearCodeCache(map->GetHeap());
6537 map->NotifyLeafMapLayoutChange();
6542 Handle<Map> Map::ShareDescriptor(Handle<Map> map,
6543 Handle<DescriptorArray> descriptors,
6544 Descriptor* descriptor) {
6545 // Sanity check. This path is only to be taken if the map owns its descriptor
6546 // array, implying that its NumberOfOwnDescriptors equals the number of
6547 // descriptors in the descriptor array.
6548 DCHECK(map->NumberOfOwnDescriptors() ==
6549 map->instance_descriptors()->number_of_descriptors());
6551 Handle<Map> result = CopyDropDescriptors(map);
6552 Handle<Name> name = descriptor->GetKey();
6554 // Ensure there's space for the new descriptor in the shared descriptor array.
6555 if (descriptors->NumberOfSlackDescriptors() == 0) {
6556 int old_size = descriptors->number_of_descriptors();
6557 if (old_size == 0) {
6558 descriptors = DescriptorArray::Allocate(map->GetIsolate(), 0, 1);
6560 int slack = SlackForArraySize(old_size, kMaxNumberOfDescriptors);
6561 EnsureDescriptorSlack(map, slack);
6562 descriptors = handle(map->instance_descriptors());
6566 Handle<LayoutDescriptor> layout_descriptor =
6567 FLAG_unbox_double_fields
6568 ? LayoutDescriptor::ShareAppend(map, descriptor->GetDetails())
6569 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
6572 DisallowHeapAllocation no_gc;
6573 descriptors->Append(descriptor);
6574 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6577 DCHECK(result->NumberOfOwnDescriptors() == map->NumberOfOwnDescriptors() + 1);
6578 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6587 void Map::TraceTransition(const char* what, Map* from, Map* to, Name* name) {
6588 if (FLAG_trace_maps) {
6589 PrintF("[TraceMaps: %s from= %p to= %p name= ", what,
6590 reinterpret_cast<void*>(from), reinterpret_cast<void*>(to));
6591 name->NameShortPrint();
6598 void Map::TraceAllTransitions(Map* map) {
6599 Object* transitions = map->raw_transitions();
6600 int num_transitions = TransitionArray::NumberOfTransitions(transitions);
6601 for (int i = -0; i < num_transitions; ++i) {
6602 Map* target = TransitionArray::GetTarget(transitions, i);
6603 Name* key = TransitionArray::GetKey(transitions, i);
6604 Map::TraceTransition("Transition", map, target, key);
6605 Map::TraceAllTransitions(target);
6609 #endif // TRACE_MAPS
6612 void Map::ConnectTransition(Handle<Map> parent, Handle<Map> child,
6613 Handle<Name> name, SimpleTransitionFlag flag) {
6614 parent->set_owns_descriptors(false);
6615 if (parent->is_prototype_map()) {
6616 DCHECK(child->is_prototype_map());
6618 Map::TraceTransition("NoTransition", *parent, *child, *name);
6621 TransitionArray::Insert(parent, name, child, flag);
6623 Map::TraceTransition("Transition", *parent, *child, *name);
6629 Handle<Map> Map::CopyReplaceDescriptors(
6630 Handle<Map> map, Handle<DescriptorArray> descriptors,
6631 Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag,
6632 MaybeHandle<Name> maybe_name, const char* reason,
6633 SimpleTransitionFlag simple_flag) {
6634 DCHECK(descriptors->IsSortedNoDuplicates());
6636 Handle<Map> result = CopyDropDescriptors(map);
6638 if (!map->is_prototype_map()) {
6639 if (flag == INSERT_TRANSITION &&
6640 TransitionArray::CanHaveMoreTransitions(map)) {
6641 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6644 CHECK(maybe_name.ToHandle(&name));
6645 ConnectTransition(map, result, name, simple_flag);
6647 int length = descriptors->number_of_descriptors();
6648 for (int i = 0; i < length; i++) {
6649 descriptors->SetRepresentation(i, Representation::Tagged());
6650 if (descriptors->GetDetails(i).type() == DATA) {
6651 descriptors->SetValue(i, HeapType::Any());
6654 result->InitializeDescriptors(*descriptors,
6655 LayoutDescriptor::FastPointerLayout());
6658 result->InitializeDescriptors(*descriptors, *layout_descriptor);
6661 if (FLAG_trace_maps &&
6662 // Mirror conditions above that did not call ConnectTransition().
6663 (map->is_prototype_map() ||
6664 !(flag == INSERT_TRANSITION &&
6665 TransitionArray::CanHaveMoreTransitions(map)))) {
6666 PrintF("[TraceMaps: ReplaceDescriptors from= %p to= %p reason= %s ]\n",
6667 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*result),
6676 // Since this method is used to rewrite an existing transition tree, it can
6677 // always insert transitions without checking.
6678 Handle<Map> Map::CopyInstallDescriptors(
6679 Handle<Map> map, int new_descriptor, Handle<DescriptorArray> descriptors,
6680 Handle<LayoutDescriptor> full_layout_descriptor) {
6681 DCHECK(descriptors->IsSortedNoDuplicates());
6683 Handle<Map> result = CopyDropDescriptors(map);
6685 result->set_instance_descriptors(*descriptors);
6686 result->SetNumberOfOwnDescriptors(new_descriptor + 1);
6688 int unused_property_fields = map->unused_property_fields();
6689 PropertyDetails details = descriptors->GetDetails(new_descriptor);
6690 if (details.location() == kField) {
6691 unused_property_fields = map->unused_property_fields() - 1;
6692 if (unused_property_fields < 0) {
6693 unused_property_fields += JSObject::kFieldsAdded;
6696 result->set_unused_property_fields(unused_property_fields);
6698 if (FLAG_unbox_double_fields) {
6699 Handle<LayoutDescriptor> layout_descriptor =
6700 LayoutDescriptor::AppendIfFastOrUseFull(map, details,
6701 full_layout_descriptor);
6702 result->set_layout_descriptor(*layout_descriptor);
6704 // TODO(ishell): remove these checks from VERIFY_HEAP mode.
6705 if (FLAG_verify_heap) {
6706 CHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6709 SLOW_DCHECK(result->layout_descriptor()->IsConsistentWithMap(*result));
6711 result->set_visitor_id(StaticVisitorBase::GetVisitorId(*result));
6714 Handle<Name> name = handle(descriptors->GetKey(new_descriptor));
6715 ConnectTransition(map, result, name, SIMPLE_PROPERTY_TRANSITION);
6721 Handle<Map> Map::CopyAsElementsKind(Handle<Map> map, ElementsKind kind,
6722 TransitionFlag flag) {
6723 Map* maybe_elements_transition_map = NULL;
6724 if (flag == INSERT_TRANSITION) {
6725 maybe_elements_transition_map = map->ElementsTransitionMap();
6726 DCHECK(maybe_elements_transition_map == NULL ||
6727 (maybe_elements_transition_map->elements_kind() ==
6728 DICTIONARY_ELEMENTS &&
6729 kind == DICTIONARY_ELEMENTS));
6730 DCHECK(!IsFastElementsKind(kind) ||
6731 IsMoreGeneralElementsKindTransition(map->elements_kind(), kind));
6732 DCHECK(kind != map->elements_kind());
6735 bool insert_transition = flag == INSERT_TRANSITION &&
6736 TransitionArray::CanHaveMoreTransitions(map) &&
6737 maybe_elements_transition_map == NULL;
6739 if (insert_transition) {
6740 Handle<Map> new_map = CopyForTransition(map, "CopyAsElementsKind");
6741 new_map->set_elements_kind(kind);
6743 Isolate* isolate = map->GetIsolate();
6744 Handle<Name> name = isolate->factory()->elements_transition_symbol();
6745 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6749 // Create a new free-floating map only if we are not allowed to store it.
6750 Handle<Map> new_map = Copy(map, "CopyAsElementsKind");
6751 new_map->set_elements_kind(kind);
6756 Handle<Map> Map::CopyForObserved(Handle<Map> map) {
6757 DCHECK(!map->is_observed());
6759 Isolate* isolate = map->GetIsolate();
6761 bool insert_transition =
6762 TransitionArray::CanHaveMoreTransitions(map) && !map->is_prototype_map();
6764 if (insert_transition) {
6765 Handle<Map> new_map = CopyForTransition(map, "CopyForObserved");
6766 new_map->set_is_observed();
6768 Handle<Name> name = isolate->factory()->observed_symbol();
6769 ConnectTransition(map, new_map, name, SPECIAL_TRANSITION);
6773 // Create a new free-floating map only if we are not allowed to store it.
6774 Handle<Map> new_map = Map::Copy(map, "CopyForObserved");
6775 new_map->set_is_observed();
6780 Handle<Map> Map::CopyForTransition(Handle<Map> map, const char* reason) {
6781 DCHECK(!map->is_prototype_map());
6782 Handle<Map> new_map = CopyDropDescriptors(map);
6784 if (map->owns_descriptors()) {
6785 // In case the map owned its own descriptors, share the descriptors and
6786 // transfer ownership to the new map.
6787 // The properties did not change, so reuse descriptors.
6788 new_map->InitializeDescriptors(map->instance_descriptors(),
6789 map->GetLayoutDescriptor());
6791 // In case the map did not own its own descriptors, a split is forced by
6792 // copying the map; creating a new descriptor array cell.
6793 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6794 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
6795 Handle<DescriptorArray> new_descriptors =
6796 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
6797 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6799 new_map->InitializeDescriptors(*new_descriptors, *new_layout_descriptor);
6803 if (FLAG_trace_maps) {
6804 PrintF("[TraceMaps: CopyForTransition from= %p to= %p reason= %s ]\n",
6805 reinterpret_cast<void*>(*map), reinterpret_cast<void*>(*new_map),
6814 Handle<Map> Map::Copy(Handle<Map> map, const char* reason) {
6815 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6816 int number_of_own_descriptors = map->NumberOfOwnDescriptors();
6817 Handle<DescriptorArray> new_descriptors =
6818 DescriptorArray::CopyUpTo(descriptors, number_of_own_descriptors);
6819 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6821 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
6822 OMIT_TRANSITION, MaybeHandle<Name>(), reason,
6823 SPECIAL_TRANSITION);
6827 Handle<Map> Map::Create(Isolate* isolate, int inobject_properties) {
6829 Copy(handle(isolate->object_function()->initial_map()), "MapCreate");
6831 // Check that we do not overflow the instance size when adding the extra
6832 // inobject properties. If the instance size overflows, we allocate as many
6833 // properties as we can as inobject properties.
6834 int max_extra_properties =
6835 (JSObject::kMaxInstanceSize - JSObject::kHeaderSize) >> kPointerSizeLog2;
6837 if (inobject_properties > max_extra_properties) {
6838 inobject_properties = max_extra_properties;
6841 int new_instance_size =
6842 JSObject::kHeaderSize + kPointerSize * inobject_properties;
6844 // Adjust the map with the extra inobject properties.
6845 copy->SetInObjectProperties(inobject_properties);
6846 copy->set_unused_property_fields(inobject_properties);
6847 copy->set_instance_size(new_instance_size);
6848 copy->set_visitor_id(StaticVisitorBase::GetVisitorId(*copy));
6853 Handle<Map> Map::CopyForPreventExtensions(Handle<Map> map,
6854 PropertyAttributes attrs_to_add,
6855 Handle<Symbol> transition_marker,
6856 const char* reason) {
6857 int num_descriptors = map->NumberOfOwnDescriptors();
6858 Isolate* isolate = map->GetIsolate();
6859 Handle<DescriptorArray> new_desc = DescriptorArray::CopyUpToAddAttributes(
6860 handle(map->instance_descriptors(), isolate), num_descriptors,
6862 Handle<LayoutDescriptor> new_layout_descriptor(map->GetLayoutDescriptor(),
6864 Handle<Map> new_map = CopyReplaceDescriptors(
6865 map, new_desc, new_layout_descriptor, INSERT_TRANSITION,
6866 transition_marker, reason, SPECIAL_TRANSITION);
6867 new_map->set_is_extensible(false);
6868 new_map->set_elements_kind(DICTIONARY_ELEMENTS);
6873 Handle<Map> Map::FixProxy(Handle<Map> map, InstanceType type, int size) {
6874 DCHECK(type == JS_OBJECT_TYPE || type == JS_FUNCTION_TYPE);
6875 DCHECK(map->IsJSProxyMap());
6877 Isolate* isolate = map->GetIsolate();
6879 // Allocate fresh map.
6880 // TODO(rossberg): Once we optimize proxies, cache these maps.
6881 Handle<Map> new_map = isolate->factory()->NewMap(type, size);
6883 Handle<Object> prototype(map->prototype(), isolate);
6884 Map::SetPrototype(new_map, prototype);
6886 map->NotifyLeafMapLayoutChange();
6892 bool DescriptorArray::CanHoldValue(int descriptor, Object* value) {
6893 PropertyDetails details = GetDetails(descriptor);
6894 switch (details.type()) {
6896 return value->FitsRepresentation(details.representation()) &&
6897 GetFieldType(descriptor)->NowContains(value);
6900 DCHECK(GetConstant(descriptor) != value ||
6901 value->FitsRepresentation(details.representation()));
6902 return GetConstant(descriptor) == value;
6905 case ACCESSOR_CONSTANT:
6915 Handle<Map> Map::PrepareForDataProperty(Handle<Map> map, int descriptor,
6916 Handle<Object> value) {
6917 // Dictionaries can store any property value.
6918 if (map->is_dictionary_map()) return map;
6920 // Migrate to the newest map before storing the property.
6923 Handle<DescriptorArray> descriptors(map->instance_descriptors());
6925 if (descriptors->CanHoldValue(descriptor, *value)) return map;
6927 Isolate* isolate = map->GetIsolate();
6928 PropertyAttributes attributes =
6929 descriptors->GetDetails(descriptor).attributes();
6930 Representation representation = value->OptimalRepresentation();
6931 Handle<HeapType> type = value->OptimalType(isolate, representation);
6933 return ReconfigureProperty(map, descriptor, kData, attributes, representation,
6938 Handle<Map> Map::TransitionToDataProperty(Handle<Map> map, Handle<Name> name,
6939 Handle<Object> value,
6940 PropertyAttributes attributes,
6941 StoreFromKeyed store_mode) {
6942 // Dictionary maps can always have additional data properties.
6943 if (map->is_dictionary_map()) return map;
6945 // Migrate to the newest map before storing the property.
6948 Map* maybe_transition =
6949 TransitionArray::SearchTransition(*map, kData, *name, attributes);
6950 if (maybe_transition != NULL) {
6951 Handle<Map> transition(maybe_transition);
6952 int descriptor = transition->LastAdded();
6954 DCHECK_EQ(attributes, transition->instance_descriptors()
6955 ->GetDetails(descriptor)
6958 return Map::PrepareForDataProperty(transition, descriptor, value);
6961 TransitionFlag flag = INSERT_TRANSITION;
6962 MaybeHandle<Map> maybe_map;
6963 if (value->IsJSFunction()) {
6964 maybe_map = Map::CopyWithConstant(map, name, value, attributes, flag);
6965 } else if (!map->TooManyFastProperties(store_mode)) {
6966 Isolate* isolate = name->GetIsolate();
6967 Representation representation = value->OptimalRepresentation();
6968 Handle<HeapType> type = value->OptimalType(isolate, representation);
6970 Map::CopyWithField(map, name, type, attributes, representation, flag);
6974 if (!maybe_map.ToHandle(&result)) {
6976 if (FLAG_trace_maps) {
6977 Vector<char> name_buffer = Vector<char>::New(100);
6978 name->NameShortPrint(name_buffer);
6979 Vector<char> buffer = Vector<char>::New(128);
6980 SNPrintF(buffer, "TooManyFastProperties %s", name_buffer.start());
6981 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, buffer.start());
6984 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES,
6985 "TooManyFastProperties");
6992 Handle<Map> Map::ReconfigureExistingProperty(Handle<Map> map, int descriptor,
6994 PropertyAttributes attributes) {
6995 // Dictionaries have to be reconfigured in-place.
6996 DCHECK(!map->is_dictionary_map());
6998 if (!map->GetBackPointer()->IsMap()) {
6999 // There is no benefit from reconstructing transition tree for maps without
7001 return CopyGeneralizeAllRepresentations(
7002 map, descriptor, FORCE_FIELD, kind, attributes,
7003 "GenAll_AttributesMismatchProtoMap");
7006 if (FLAG_trace_generalization) {
7007 map->PrintReconfiguration(stdout, descriptor, kind, attributes);
7010 Isolate* isolate = map->GetIsolate();
7011 Handle<Map> new_map = ReconfigureProperty(
7012 map, descriptor, kind, attributes, Representation::None(),
7013 HeapType::None(isolate), FORCE_FIELD);
7018 Handle<Map> Map::TransitionToAccessorProperty(Handle<Map> map,
7020 AccessorComponent component,
7021 Handle<Object> accessor,
7022 PropertyAttributes attributes) {
7023 Isolate* isolate = name->GetIsolate();
7025 // Dictionary maps can always have additional data properties.
7026 if (map->is_dictionary_map()) return map;
7028 // Migrate to the newest map before transitioning to the new property.
7031 PropertyNormalizationMode mode = map->is_prototype_map()
7032 ? KEEP_INOBJECT_PROPERTIES
7033 : CLEAR_INOBJECT_PROPERTIES;
7035 Map* maybe_transition =
7036 TransitionArray::SearchTransition(*map, kAccessor, *name, attributes);
7037 if (maybe_transition != NULL) {
7038 Handle<Map> transition(maybe_transition, isolate);
7039 DescriptorArray* descriptors = transition->instance_descriptors();
7040 int descriptor = transition->LastAdded();
7041 DCHECK(descriptors->GetKey(descriptor)->Equals(*name));
7043 DCHECK_EQ(kAccessor, descriptors->GetDetails(descriptor).kind());
7044 DCHECK_EQ(attributes, descriptors->GetDetails(descriptor).attributes());
7046 Handle<Object> maybe_pair(descriptors->GetValue(descriptor), isolate);
7047 if (!maybe_pair->IsAccessorPair()) {
7048 return Map::Normalize(map, mode, "TransitionToAccessorFromNonPair");
7051 Handle<AccessorPair> pair = Handle<AccessorPair>::cast(maybe_pair);
7052 if (pair->get(component) != *accessor) {
7053 return Map::Normalize(map, mode, "TransitionToDifferentAccessor");
7059 Handle<AccessorPair> pair;
7060 DescriptorArray* old_descriptors = map->instance_descriptors();
7061 int descriptor = old_descriptors->SearchWithCache(*name, *map);
7062 if (descriptor != DescriptorArray::kNotFound) {
7063 if (descriptor != map->LastAdded()) {
7064 return Map::Normalize(map, mode, "AccessorsOverwritingNonLast");
7066 PropertyDetails old_details = old_descriptors->GetDetails(descriptor);
7067 if (old_details.type() != ACCESSOR_CONSTANT) {
7068 return Map::Normalize(map, mode, "AccessorsOverwritingNonAccessors");
7071 if (old_details.attributes() != attributes) {
7072 return Map::Normalize(map, mode, "AccessorsWithAttributes");
7075 Handle<Object> maybe_pair(old_descriptors->GetValue(descriptor), isolate);
7076 if (!maybe_pair->IsAccessorPair()) {
7077 return Map::Normalize(map, mode, "AccessorsOverwritingNonPair");
7080 Object* current = Handle<AccessorPair>::cast(maybe_pair)->get(component);
7081 if (current == *accessor) return map;
7083 if (!current->IsTheHole()) {
7084 return Map::Normalize(map, mode, "AccessorsOverwritingAccessors");
7087 pair = AccessorPair::Copy(Handle<AccessorPair>::cast(maybe_pair));
7088 } else if (map->NumberOfOwnDescriptors() >= kMaxNumberOfDescriptors ||
7089 map->TooManyFastProperties(CERTAINLY_NOT_STORE_FROM_KEYED)) {
7090 return Map::Normalize(map, CLEAR_INOBJECT_PROPERTIES, "TooManyAccessors");
7092 pair = isolate->factory()->NewAccessorPair();
7095 pair->set(component, *accessor);
7096 TransitionFlag flag = INSERT_TRANSITION;
7097 AccessorConstantDescriptor new_desc(name, pair, attributes);
7098 return Map::CopyInsertDescriptor(map, &new_desc, flag);
7102 Handle<Map> Map::CopyAddDescriptor(Handle<Map> map,
7103 Descriptor* descriptor,
7104 TransitionFlag flag) {
7105 Handle<DescriptorArray> descriptors(map->instance_descriptors());
7107 // Ensure the key is unique.
7108 descriptor->KeyToUniqueName();
7110 if (flag == INSERT_TRANSITION && map->owns_descriptors() &&
7111 TransitionArray::CanHaveMoreTransitions(map)) {
7112 return ShareDescriptor(map, descriptors, descriptor);
7115 int nof = map->NumberOfOwnDescriptors();
7116 Handle<DescriptorArray> new_descriptors =
7117 DescriptorArray::CopyUpTo(descriptors, nof, 1);
7118 new_descriptors->Append(descriptor);
7120 Handle<LayoutDescriptor> new_layout_descriptor =
7121 FLAG_unbox_double_fields
7122 ? LayoutDescriptor::New(map, new_descriptors, nof + 1)
7123 : handle(LayoutDescriptor::FastPointerLayout(), map->GetIsolate());
7125 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7126 flag, descriptor->GetKey(), "CopyAddDescriptor",
7127 SIMPLE_PROPERTY_TRANSITION);
7131 Handle<Map> Map::CopyInsertDescriptor(Handle<Map> map,
7132 Descriptor* descriptor,
7133 TransitionFlag flag) {
7134 Handle<DescriptorArray> old_descriptors(map->instance_descriptors());
7136 // Ensure the key is unique.
7137 descriptor->KeyToUniqueName();
7139 // We replace the key if it is already present.
7140 int index = old_descriptors->SearchWithCache(*descriptor->GetKey(), *map);
7141 if (index != DescriptorArray::kNotFound) {
7142 return CopyReplaceDescriptor(map, old_descriptors, descriptor, index, flag);
7144 return CopyAddDescriptor(map, descriptor, flag);
7148 Handle<DescriptorArray> DescriptorArray::CopyUpTo(
7149 Handle<DescriptorArray> desc,
7150 int enumeration_index,
7152 return DescriptorArray::CopyUpToAddAttributes(
7153 desc, enumeration_index, NONE, slack);
7157 Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes(
7158 Handle<DescriptorArray> desc,
7159 int enumeration_index,
7160 PropertyAttributes attributes,
7162 if (enumeration_index + slack == 0) {
7163 return desc->GetIsolate()->factory()->empty_descriptor_array();
7166 int size = enumeration_index;
7168 Handle<DescriptorArray> descriptors =
7169 DescriptorArray::Allocate(desc->GetIsolate(), size, slack);
7170 DescriptorArray::WhitenessWitness witness(*descriptors);
7172 if (attributes != NONE) {
7173 for (int i = 0; i < size; ++i) {
7174 Object* value = desc->GetValue(i);
7175 Name* key = desc->GetKey(i);
7176 PropertyDetails details = desc->GetDetails(i);
7177 // Bulk attribute changes never affect private properties.
7178 if (!key->IsSymbol() || !Symbol::cast(key)->is_private()) {
7179 int mask = DONT_DELETE | DONT_ENUM;
7180 // READ_ONLY is an invalid attribute for JS setters/getters.
7181 if (details.type() != ACCESSOR_CONSTANT || !value->IsAccessorPair()) {
7184 details = details.CopyAddAttributes(
7185 static_cast<PropertyAttributes>(attributes & mask));
7187 Descriptor inner_desc(
7188 handle(key), handle(value, desc->GetIsolate()), details);
7189 descriptors->Set(i, &inner_desc, witness);
7192 for (int i = 0; i < size; ++i) {
7193 descriptors->CopyFrom(i, *desc, witness);
7197 if (desc->number_of_descriptors() != enumeration_index) descriptors->Sort();
7203 Handle<Map> Map::CopyReplaceDescriptor(Handle<Map> map,
7204 Handle<DescriptorArray> descriptors,
7205 Descriptor* descriptor,
7206 int insertion_index,
7207 TransitionFlag flag) {
7208 // Ensure the key is unique.
7209 descriptor->KeyToUniqueName();
7211 Handle<Name> key = descriptor->GetKey();
7212 DCHECK(*key == descriptors->GetKey(insertion_index));
7214 Handle<DescriptorArray> new_descriptors = DescriptorArray::CopyUpTo(
7215 descriptors, map->NumberOfOwnDescriptors());
7217 new_descriptors->Replace(insertion_index, descriptor);
7218 Handle<LayoutDescriptor> new_layout_descriptor = LayoutDescriptor::New(
7219 map, new_descriptors, new_descriptors->number_of_descriptors());
7221 SimpleTransitionFlag simple_flag =
7222 (insertion_index == descriptors->number_of_descriptors() - 1)
7223 ? SIMPLE_PROPERTY_TRANSITION
7224 : PROPERTY_TRANSITION;
7225 return CopyReplaceDescriptors(map, new_descriptors, new_layout_descriptor,
7226 flag, key, "CopyReplaceDescriptor",
7231 void Map::UpdateCodeCache(Handle<Map> map,
7233 Handle<Code> code) {
7234 Isolate* isolate = map->GetIsolate();
7235 HandleScope scope(isolate);
7236 // Allocate the code cache if not present.
7237 if (map->code_cache()->IsFixedArray()) {
7238 Handle<Object> result = isolate->factory()->NewCodeCache();
7239 map->set_code_cache(*result);
7242 // Update the code cache.
7243 Handle<CodeCache> code_cache(CodeCache::cast(map->code_cache()), isolate);
7244 CodeCache::Update(code_cache, name, code);
7248 Object* Map::FindInCodeCache(Name* name, Code::Flags flags) {
7249 // Do a lookup if a code cache exists.
7250 if (!code_cache()->IsFixedArray()) {
7251 return CodeCache::cast(code_cache())->Lookup(name, flags);
7253 return GetHeap()->undefined_value();
7258 int Map::IndexInCodeCache(Object* name, Code* code) {
7259 // Get the internal index if a code cache exists.
7260 if (!code_cache()->IsFixedArray()) {
7261 return CodeCache::cast(code_cache())->GetIndex(name, code);
7267 void Map::RemoveFromCodeCache(Name* name, Code* code, int index) {
7268 // No GC is supposed to happen between a call to IndexInCodeCache and
7269 // RemoveFromCodeCache so the code cache must be there.
7270 DCHECK(!code_cache()->IsFixedArray());
7271 CodeCache::cast(code_cache())->RemoveByIndex(name, code, index);
7275 void CodeCache::Update(
7276 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7277 // The number of monomorphic stubs for normal load/store/call IC's can grow to
7278 // a large number and therefore they need to go into a hash table. They are
7279 // used to load global properties from cells.
7280 if (code->type() == Code::NORMAL) {
7281 // Make sure that a hash table is allocated for the normal load code cache.
7282 if (code_cache->normal_type_cache()->IsUndefined()) {
7283 Handle<Object> result =
7284 CodeCacheHashTable::New(code_cache->GetIsolate(),
7285 CodeCacheHashTable::kInitialSize);
7286 code_cache->set_normal_type_cache(*result);
7288 UpdateNormalTypeCache(code_cache, name, code);
7290 DCHECK(code_cache->default_cache()->IsFixedArray());
7291 UpdateDefaultCache(code_cache, name, code);
7296 void CodeCache::UpdateDefaultCache(
7297 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7298 // When updating the default code cache we disregard the type encoded in the
7299 // flags. This allows call constant stubs to overwrite call field
7301 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
7303 // First check whether we can update existing code cache without
7305 Handle<FixedArray> cache = handle(code_cache->default_cache());
7306 int length = cache->length();
7308 DisallowHeapAllocation no_alloc;
7309 int deleted_index = -1;
7310 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7311 Object* key = cache->get(i);
7312 if (key->IsNull()) {
7313 if (deleted_index < 0) deleted_index = i;
7316 if (key->IsUndefined()) {
7317 if (deleted_index >= 0) i = deleted_index;
7318 cache->set(i + kCodeCacheEntryNameOffset, *name);
7319 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7322 if (name->Equals(Name::cast(key))) {
7324 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
7325 if (Code::RemoveTypeFromFlags(found) == flags) {
7326 cache->set(i + kCodeCacheEntryCodeOffset, *code);
7332 // Reached the end of the code cache. If there were deleted
7333 // elements, reuse the space for the first of them.
7334 if (deleted_index >= 0) {
7335 cache->set(deleted_index + kCodeCacheEntryNameOffset, *name);
7336 cache->set(deleted_index + kCodeCacheEntryCodeOffset, *code);
7341 // Extend the code cache with some new entries (at least one). Must be a
7342 // multiple of the entry size.
7343 Isolate* isolate = cache->GetIsolate();
7344 int new_length = length + (length >> 1) + kCodeCacheEntrySize;
7345 new_length = new_length - new_length % kCodeCacheEntrySize;
7346 DCHECK((new_length % kCodeCacheEntrySize) == 0);
7347 cache = isolate->factory()->CopyFixedArrayAndGrow(cache, new_length - length);
7349 // Add the (name, code) pair to the new cache.
7350 cache->set(length + kCodeCacheEntryNameOffset, *name);
7351 cache->set(length + kCodeCacheEntryCodeOffset, *code);
7352 code_cache->set_default_cache(*cache);
7356 void CodeCache::UpdateNormalTypeCache(
7357 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code) {
7358 // Adding a new entry can cause a new cache to be allocated.
7359 Handle<CodeCacheHashTable> cache(
7360 CodeCacheHashTable::cast(code_cache->normal_type_cache()));
7361 Handle<Object> new_cache = CodeCacheHashTable::Put(cache, name, code);
7362 code_cache->set_normal_type_cache(*new_cache);
7366 Object* CodeCache::Lookup(Name* name, Code::Flags flags) {
7367 Object* result = LookupDefaultCache(name, Code::RemoveTypeFromFlags(flags));
7368 if (result->IsCode()) {
7369 if (Code::cast(result)->flags() == flags) return result;
7370 return GetHeap()->undefined_value();
7372 return LookupNormalTypeCache(name, flags);
7376 Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) {
7377 FixedArray* cache = default_cache();
7378 int length = cache->length();
7379 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
7380 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
7381 // Skip deleted elements.
7382 if (key->IsNull()) continue;
7383 if (key->IsUndefined()) return key;
7384 if (name->Equals(Name::cast(key))) {
7385 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
7386 if (Code::RemoveTypeFromFlags(code->flags()) == flags) {
7391 return GetHeap()->undefined_value();
7395 Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) {
7396 if (!normal_type_cache()->IsUndefined()) {
7397 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7398 return cache->Lookup(name, flags);
7400 return GetHeap()->undefined_value();
7405 int CodeCache::GetIndex(Object* name, Code* code) {
7406 if (code->type() == Code::NORMAL) {
7407 if (normal_type_cache()->IsUndefined()) return -1;
7408 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7409 return cache->GetIndex(Name::cast(name), code->flags());
7412 FixedArray* array = default_cache();
7413 int len = array->length();
7414 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
7415 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
7421 void CodeCache::RemoveByIndex(Object* name, Code* code, int index) {
7422 if (code->type() == Code::NORMAL) {
7423 DCHECK(!normal_type_cache()->IsUndefined());
7424 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
7425 DCHECK(cache->GetIndex(Name::cast(name), code->flags()) == index);
7426 cache->RemoveByIndex(index);
7428 FixedArray* array = default_cache();
7429 DCHECK(array->length() >= index && array->get(index)->IsCode());
7430 // Use null instead of undefined for deleted elements to distinguish
7431 // deleted elements from unused elements. This distinction is used
7432 // when looking up in the cache and when updating the cache.
7433 DCHECK_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset);
7434 array->set_null(index - 1); // Name.
7435 array->set_null(index); // Code.
7440 // The key in the code cache hash table consists of the property name and the
7441 // code object. The actual match is on the name and the code flags. If a key
7442 // is created using the flags and not a code object it can only be used for
7443 // lookup not to create a new entry.
7444 class CodeCacheHashTableKey : public HashTableKey {
7446 CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags)
7447 : name_(name), flags_(flags), code_() { }
7449 CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code)
7450 : name_(name), flags_(code->flags()), code_(code) { }
7452 bool IsMatch(Object* other) override {
7453 if (!other->IsFixedArray()) return false;
7454 FixedArray* pair = FixedArray::cast(other);
7455 Name* name = Name::cast(pair->get(0));
7456 Code::Flags flags = Code::cast(pair->get(1))->flags();
7457 if (flags != flags_) {
7460 return name_->Equals(name);
7463 static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) {
7464 return name->Hash() ^ flags;
7467 uint32_t Hash() override { return NameFlagsHashHelper(*name_, flags_); }
7469 uint32_t HashForObject(Object* obj) override {
7470 FixedArray* pair = FixedArray::cast(obj);
7471 Name* name = Name::cast(pair->get(0));
7472 Code* code = Code::cast(pair->get(1));
7473 return NameFlagsHashHelper(name, code->flags());
7476 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7477 Handle<Code> code = code_.ToHandleChecked();
7478 Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2);
7479 pair->set(0, *name_);
7480 pair->set(1, *code);
7487 // TODO(jkummerow): We should be able to get by without this.
7488 MaybeHandle<Code> code_;
7492 Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) {
7493 DisallowHeapAllocation no_alloc;
7494 CodeCacheHashTableKey key(handle(name), flags);
7495 int entry = FindEntry(&key);
7496 if (entry == kNotFound) return GetHeap()->undefined_value();
7497 return get(EntryToIndex(entry) + 1);
7501 Handle<CodeCacheHashTable> CodeCacheHashTable::Put(
7502 Handle<CodeCacheHashTable> cache, Handle<Name> name, Handle<Code> code) {
7503 CodeCacheHashTableKey key(name, code);
7505 Handle<CodeCacheHashTable> new_cache = EnsureCapacity(cache, 1, &key);
7507 int entry = new_cache->FindInsertionEntry(key.Hash());
7508 Handle<Object> k = key.AsHandle(cache->GetIsolate());
7510 new_cache->set(EntryToIndex(entry), *k);
7511 new_cache->set(EntryToIndex(entry) + 1, *code);
7512 new_cache->ElementAdded();
7517 int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) {
7518 DisallowHeapAllocation no_alloc;
7519 CodeCacheHashTableKey key(handle(name), flags);
7520 int entry = FindEntry(&key);
7521 return (entry == kNotFound) ? -1 : entry;
7525 void CodeCacheHashTable::RemoveByIndex(int index) {
7527 Heap* heap = GetHeap();
7528 set(EntryToIndex(index), heap->the_hole_value());
7529 set(EntryToIndex(index) + 1, heap->the_hole_value());
7534 void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> code_cache,
7535 MapHandleList* maps,
7537 Handle<Code> code) {
7538 Isolate* isolate = code_cache->GetIsolate();
7539 if (code_cache->cache()->IsUndefined()) {
7540 Handle<PolymorphicCodeCacheHashTable> result =
7541 PolymorphicCodeCacheHashTable::New(
7543 PolymorphicCodeCacheHashTable::kInitialSize);
7544 code_cache->set_cache(*result);
7546 // This entry shouldn't be contained in the cache yet.
7547 DCHECK(PolymorphicCodeCacheHashTable::cast(code_cache->cache())
7548 ->Lookup(maps, flags)->IsUndefined());
7550 Handle<PolymorphicCodeCacheHashTable> hash_table =
7551 handle(PolymorphicCodeCacheHashTable::cast(code_cache->cache()));
7552 Handle<PolymorphicCodeCacheHashTable> new_cache =
7553 PolymorphicCodeCacheHashTable::Put(hash_table, maps, flags, code);
7554 code_cache->set_cache(*new_cache);
7558 Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps,
7559 Code::Flags flags) {
7560 if (!cache()->IsUndefined()) {
7561 PolymorphicCodeCacheHashTable* hash_table =
7562 PolymorphicCodeCacheHashTable::cast(cache());
7563 return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate());
7565 return GetIsolate()->factory()->undefined_value();
7570 // Despite their name, object of this class are not stored in the actual
7571 // hash table; instead they're temporarily used for lookups. It is therefore
7572 // safe to have a weak (non-owning) pointer to a MapList as a member field.
7573 class PolymorphicCodeCacheHashTableKey : public HashTableKey {
7575 // Callers must ensure that |maps| outlives the newly constructed object.
7576 PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags)
7578 code_flags_(code_flags) {}
7580 bool IsMatch(Object* other) override {
7581 MapHandleList other_maps(kDefaultListAllocationSize);
7583 FromObject(other, &other_flags, &other_maps);
7584 if (code_flags_ != other_flags) return false;
7585 if (maps_->length() != other_maps.length()) return false;
7586 // Compare just the hashes first because it's faster.
7587 int this_hash = MapsHashHelper(maps_, code_flags_);
7588 int other_hash = MapsHashHelper(&other_maps, other_flags);
7589 if (this_hash != other_hash) return false;
7591 // Full comparison: for each map in maps_, look for an equivalent map in
7592 // other_maps. This implementation is slow, but probably good enough for
7593 // now because the lists are short (<= 4 elements currently).
7594 for (int i = 0; i < maps_->length(); ++i) {
7595 bool match_found = false;
7596 for (int j = 0; j < other_maps.length(); ++j) {
7597 if (*(maps_->at(i)) == *(other_maps.at(j))) {
7602 if (!match_found) return false;
7607 static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) {
7608 uint32_t hash = code_flags;
7609 for (int i = 0; i < maps->length(); ++i) {
7610 hash ^= maps->at(i)->Hash();
7615 uint32_t Hash() override { return MapsHashHelper(maps_, code_flags_); }
7617 uint32_t HashForObject(Object* obj) override {
7618 MapHandleList other_maps(kDefaultListAllocationSize);
7620 FromObject(obj, &other_flags, &other_maps);
7621 return MapsHashHelper(&other_maps, other_flags);
7624 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
7625 // The maps in |maps_| must be copied to a newly allocated FixedArray,
7626 // both because the referenced MapList is short-lived, and because C++
7627 // objects can't be stored in the heap anyway.
7628 Handle<FixedArray> list =
7629 isolate->factory()->NewUninitializedFixedArray(maps_->length() + 1);
7630 list->set(0, Smi::FromInt(code_flags_));
7631 for (int i = 0; i < maps_->length(); ++i) {
7632 list->set(i + 1, *maps_->at(i));
7638 static MapHandleList* FromObject(Object* obj,
7640 MapHandleList* maps) {
7641 FixedArray* list = FixedArray::cast(obj);
7643 *code_flags = Smi::cast(list->get(0))->value();
7644 for (int i = 1; i < list->length(); ++i) {
7645 maps->Add(Handle<Map>(Map::cast(list->get(i))));
7650 MapHandleList* maps_; // weak.
7652 static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1;
7656 Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps,
7658 DisallowHeapAllocation no_alloc;
7659 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7660 int entry = FindEntry(&key);
7661 if (entry == kNotFound) return GetHeap()->undefined_value();
7662 return get(EntryToIndex(entry) + 1);
7666 Handle<PolymorphicCodeCacheHashTable> PolymorphicCodeCacheHashTable::Put(
7667 Handle<PolymorphicCodeCacheHashTable> hash_table,
7668 MapHandleList* maps,
7670 Handle<Code> code) {
7671 PolymorphicCodeCacheHashTableKey key(maps, code_kind);
7672 Handle<PolymorphicCodeCacheHashTable> cache =
7673 EnsureCapacity(hash_table, 1, &key);
7674 int entry = cache->FindInsertionEntry(key.Hash());
7676 Handle<Object> obj = key.AsHandle(hash_table->GetIsolate());
7677 cache->set(EntryToIndex(entry), *obj);
7678 cache->set(EntryToIndex(entry) + 1, *code);
7679 cache->ElementAdded();
7684 void FixedArray::Shrink(int new_length) {
7685 DCHECK(0 <= new_length && new_length <= length());
7686 if (new_length < length()) {
7687 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(
7688 this, length() - new_length);
7693 MaybeHandle<FixedArray> FixedArray::AddKeysFromArrayLike(
7694 Handle<FixedArray> content, Handle<JSObject> array, KeyFilter filter) {
7695 DCHECK(array->IsJSArray() || array->HasSloppyArgumentsElements());
7696 ElementsAccessor* accessor = array->GetElementsAccessor();
7697 Handle<FixedArray> result =
7698 accessor->AddElementsToFixedArray(array, content, filter);
7700 #ifdef ENABLE_SLOW_DCHECKS
7701 if (FLAG_enable_slow_asserts) {
7702 DisallowHeapAllocation no_allocation;
7703 for (int i = 0; i < result->length(); i++) {
7704 Object* current = result->get(i);
7705 DCHECK(current->IsNumber() || current->IsName());
7713 MaybeHandle<FixedArray> FixedArray::UnionOfKeys(Handle<FixedArray> first,
7714 Handle<FixedArray> second) {
7715 if (second->length() == 0) return first;
7716 if (first->length() == 0) return second;
7717 Isolate* isolate = first->GetIsolate();
7718 Handle<FixedArray> result =
7719 isolate->factory()->NewFixedArray(first->length() + second->length());
7720 for (int i = 0; i < first->length(); i++) {
7721 result->set(i, first->get(i));
7723 int pos = first->length();
7724 for (int j = 0; j < second->length(); j++) {
7725 Object* current = second->get(j);
7727 for (i = 0; i < first->length(); i++) {
7728 if (current->KeyEquals(first->get(i))) break;
7730 if (i == first->length()) {
7731 result->set(pos++, current);
7735 result->Shrink(pos);
7740 void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
7741 DisallowHeapAllocation no_gc;
7742 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
7743 for (int index = 0; index < len; index++) {
7744 dest->set(dest_pos+index, get(pos+index), mode);
7750 bool FixedArray::IsEqualTo(FixedArray* other) {
7751 if (length() != other->length()) return false;
7752 for (int i = 0 ; i < length(); ++i) {
7753 if (get(i) != other->get(i)) return false;
7761 void WeakFixedArray::Set(Handle<WeakFixedArray> array, int index,
7762 Handle<HeapObject> value) {
7763 DCHECK(array->IsEmptySlot(index)); // Don't overwrite anything.
7764 Handle<WeakCell> cell =
7765 value->IsMap() ? Map::WeakCellForMap(Handle<Map>::cast(value))
7766 : array->GetIsolate()->factory()->NewWeakCell(value);
7767 Handle<FixedArray>::cast(array)->set(index + kFirstIndex, *cell);
7768 if (FLAG_trace_weak_arrays) {
7769 PrintF("[WeakFixedArray: storing at index %d ]\n", index);
7771 array->set_last_used_index(index);
7776 Handle<WeakFixedArray> WeakFixedArray::Add(Handle<Object> maybe_array,
7777 Handle<HeapObject> value,
7778 int* assigned_index) {
7779 Handle<WeakFixedArray> array =
7780 (maybe_array.is_null() || !maybe_array->IsWeakFixedArray())
7781 ? Allocate(value->GetIsolate(), 1, Handle<WeakFixedArray>::null())
7782 : Handle<WeakFixedArray>::cast(maybe_array);
7783 // Try to store the new entry if there's room. Optimize for consecutive
7785 int first_index = array->last_used_index();
7786 int length = array->Length();
7788 for (int i = first_index;;) {
7789 if (array->IsEmptySlot((i))) {
7790 WeakFixedArray::Set(array, i, value);
7791 if (assigned_index != NULL) *assigned_index = i;
7794 if (FLAG_trace_weak_arrays) {
7795 PrintF("[WeakFixedArray: searching for free slot]\n");
7797 i = (i + 1) % length;
7798 if (i == first_index) break;
7802 // No usable slot found, grow the array.
7803 int new_length = length == 0 ? 1 : length + (length >> 1) + 4;
7804 Handle<WeakFixedArray> new_array =
7805 Allocate(array->GetIsolate(), new_length, array);
7806 if (FLAG_trace_weak_arrays) {
7807 PrintF("[WeakFixedArray: growing to size %d ]\n", new_length);
7809 WeakFixedArray::Set(new_array, length, value);
7810 if (assigned_index != NULL) *assigned_index = length;
7815 template <class CompactionCallback>
7816 void WeakFixedArray::Compact() {
7817 FixedArray* array = FixedArray::cast(this);
7818 int new_length = kFirstIndex;
7819 for (int i = kFirstIndex; i < array->length(); i++) {
7820 Object* element = array->get(i);
7821 if (element->IsSmi()) continue;
7822 if (WeakCell::cast(element)->cleared()) continue;
7823 Object* value = WeakCell::cast(element)->value();
7824 CompactionCallback::Callback(value, i - kFirstIndex,
7825 new_length - kFirstIndex);
7826 array->set(new_length++, element);
7828 array->Shrink(new_length);
7829 set_last_used_index(0);
7833 void WeakFixedArray::Iterator::Reset(Object* maybe_array) {
7834 if (maybe_array->IsWeakFixedArray()) {
7835 list_ = WeakFixedArray::cast(maybe_array);
7838 last_used_index_ = list_->last_used_index();
7844 void JSObject::PrototypeRegistryCompactionCallback::Callback(Object* value,
7847 DCHECK(value->IsMap() && Map::cast(value)->is_prototype_map());
7848 Map* map = Map::cast(value);
7849 DCHECK(map->prototype_info()->IsPrototypeInfo());
7850 PrototypeInfo* proto_info = PrototypeInfo::cast(map->prototype_info());
7851 DCHECK_EQ(old_index, proto_info->registry_slot());
7852 proto_info->set_registry_slot(new_index);
7856 template void WeakFixedArray::Compact<WeakFixedArray::NullCallback>();
7858 WeakFixedArray::Compact<JSObject::PrototypeRegistryCompactionCallback>();
7861 bool WeakFixedArray::Remove(Handle<HeapObject> value) {
7862 if (Length() == 0) return false;
7863 // Optimize for the most recently added element to be removed again.
7864 int first_index = last_used_index();
7865 for (int i = first_index;;) {
7866 if (Get(i) == *value) {
7868 // Users of WeakFixedArray should make sure that there are no duplicates.
7871 i = (i + 1) % Length();
7872 if (i == first_index) return false;
7879 Handle<WeakFixedArray> WeakFixedArray::Allocate(
7880 Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from) {
7882 Handle<FixedArray> result =
7883 isolate->factory()->NewUninitializedFixedArray(size + kFirstIndex);
7885 if (!initialize_from.is_null()) {
7886 DCHECK(initialize_from->Length() <= size);
7887 Handle<FixedArray> raw_source = Handle<FixedArray>::cast(initialize_from);
7888 // Copy the entries without compacting, since the PrototypeInfo relies on
7889 // the index of the entries not to change.
7890 while (index < raw_source->length()) {
7891 result->set(index, raw_source->get(index));
7895 while (index < result->length()) {
7896 result->set(index, Smi::FromInt(0));
7899 return Handle<WeakFixedArray>::cast(result);
7903 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj,
7905 int length = array->Length();
7906 array = EnsureSpace(array, length + 1);
7907 if (mode == kReloadLengthAfterAllocation) {
7908 DCHECK(array->Length() <= length);
7909 length = array->Length();
7911 array->Set(length, *obj);
7912 array->SetLength(length + 1);
7917 Handle<ArrayList> ArrayList::Add(Handle<ArrayList> array, Handle<Object> obj1,
7918 Handle<Object> obj2, AddMode mode) {
7919 int length = array->Length();
7920 array = EnsureSpace(array, length + 2);
7921 if (mode == kReloadLengthAfterAllocation) {
7922 length = array->Length();
7924 array->Set(length, *obj1);
7925 array->Set(length + 1, *obj2);
7926 array->SetLength(length + 2);
7931 Handle<ArrayList> ArrayList::EnsureSpace(Handle<ArrayList> array, int length) {
7932 int capacity = array->length();
7933 bool empty = (capacity == 0);
7934 if (capacity < kFirstIndex + length) {
7935 Isolate* isolate = array->GetIsolate();
7936 int new_capacity = kFirstIndex + length;
7937 new_capacity = new_capacity + Max(new_capacity / 2, 2);
7938 int grow_by = new_capacity - capacity;
7939 array = Handle<ArrayList>::cast(
7940 isolate->factory()->CopyFixedArrayAndGrow(array, grow_by));
7941 if (empty) array->SetLength(0);
7947 Handle<DescriptorArray> DescriptorArray::Allocate(Isolate* isolate,
7948 int number_of_descriptors,
7950 DCHECK(0 <= number_of_descriptors);
7951 Factory* factory = isolate->factory();
7952 // Do not use DescriptorArray::cast on incomplete object.
7953 int size = number_of_descriptors + slack;
7954 if (size == 0) return factory->empty_descriptor_array();
7955 // Allocate the array of keys.
7956 Handle<FixedArray> result = factory->NewFixedArray(LengthFor(size));
7958 result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors));
7959 result->set(kEnumCacheIndex, Smi::FromInt(0));
7960 return Handle<DescriptorArray>::cast(result);
7964 void DescriptorArray::ClearEnumCache() {
7965 set(kEnumCacheIndex, Smi::FromInt(0));
7969 void DescriptorArray::Replace(int index, Descriptor* descriptor) {
7970 descriptor->SetSortedKeyIndex(GetSortedKeyIndex(index));
7971 Set(index, descriptor);
7975 void DescriptorArray::SetEnumCache(FixedArray* bridge_storage,
7976 FixedArray* new_cache,
7977 Object* new_index_cache) {
7978 DCHECK(bridge_storage->length() >= kEnumCacheBridgeLength);
7979 DCHECK(new_index_cache->IsSmi() || new_index_cache->IsFixedArray());
7981 DCHECK(!HasEnumCache() || new_cache->length() > GetEnumCache()->length());
7982 FixedArray::cast(bridge_storage)->
7983 set(kEnumCacheBridgeCacheIndex, new_cache);
7984 FixedArray::cast(bridge_storage)->
7985 set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache);
7986 set(kEnumCacheIndex, bridge_storage);
7990 void DescriptorArray::CopyFrom(int index, DescriptorArray* src,
7991 const WhitenessWitness& witness) {
7992 Object* value = src->GetValue(index);
7993 PropertyDetails details = src->GetDetails(index);
7994 Descriptor desc(handle(src->GetKey(index)),
7995 handle(value, src->GetIsolate()),
7997 Set(index, &desc, witness);
8001 // We need the whiteness witness since sort will reshuffle the entries in the
8002 // descriptor array. If the descriptor array were to be black, the shuffling
8003 // would move a slot that was already recorded as pointing into an evacuation
8004 // candidate. This would result in missing updates upon evacuation.
8005 void DescriptorArray::Sort() {
8006 // In-place heap sort.
8007 int len = number_of_descriptors();
8008 // Reset sorting since the descriptor array might contain invalid pointers.
8009 for (int i = 0; i < len; ++i) SetSortedKey(i, i);
8010 // Bottom-up max-heap construction.
8011 // Index of the last node with children
8012 const int max_parent_index = (len / 2) - 1;
8013 for (int i = max_parent_index; i >= 0; --i) {
8014 int parent_index = i;
8015 const uint32_t parent_hash = GetSortedKey(i)->Hash();
8016 while (parent_index <= max_parent_index) {
8017 int child_index = 2 * parent_index + 1;
8018 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8019 if (child_index + 1 < len) {
8020 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8021 if (right_child_hash > child_hash) {
8023 child_hash = right_child_hash;
8026 if (child_hash <= parent_hash) break;
8027 SwapSortedKeys(parent_index, child_index);
8028 // Now element at child_index could be < its children.
8029 parent_index = child_index; // parent_hash remains correct.
8033 // Extract elements and create sorted array.
8034 for (int i = len - 1; i > 0; --i) {
8035 // Put max element at the back of the array.
8036 SwapSortedKeys(0, i);
8037 // Shift down the new top element.
8038 int parent_index = 0;
8039 const uint32_t parent_hash = GetSortedKey(parent_index)->Hash();
8040 const int max_parent_index = (i / 2) - 1;
8041 while (parent_index <= max_parent_index) {
8042 int child_index = parent_index * 2 + 1;
8043 uint32_t child_hash = GetSortedKey(child_index)->Hash();
8044 if (child_index + 1 < i) {
8045 uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash();
8046 if (right_child_hash > child_hash) {
8048 child_hash = right_child_hash;
8051 if (child_hash <= parent_hash) break;
8052 SwapSortedKeys(parent_index, child_index);
8053 parent_index = child_index;
8056 DCHECK(IsSortedNoDuplicates());
8060 Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) {
8061 Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair();
8062 copy->set_getter(pair->getter());
8063 copy->set_setter(pair->setter());
8068 Object* AccessorPair::GetComponent(AccessorComponent component) {
8069 Object* accessor = get(component);
8070 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor;
8074 Handle<DeoptimizationInputData> DeoptimizationInputData::New(
8075 Isolate* isolate, int deopt_entry_count, PretenureFlag pretenure) {
8076 return Handle<DeoptimizationInputData>::cast(
8077 isolate->factory()->NewFixedArray(LengthFor(deopt_entry_count),
8082 Handle<DeoptimizationOutputData> DeoptimizationOutputData::New(
8084 int number_of_deopt_points,
8085 PretenureFlag pretenure) {
8086 Handle<FixedArray> result;
8087 if (number_of_deopt_points == 0) {
8088 result = isolate->factory()->empty_fixed_array();
8090 result = isolate->factory()->NewFixedArray(
8091 LengthOfFixedArray(number_of_deopt_points), pretenure);
8093 return Handle<DeoptimizationOutputData>::cast(result);
8097 int HandlerTable::LookupRange(int pc_offset, int* stack_depth_out,
8098 CatchPrediction* prediction_out) {
8099 int innermost_handler = -1, innermost_start = -1;
8100 for (int i = 0; i < length(); i += kRangeEntrySize) {
8101 int start_offset = Smi::cast(get(i + kRangeStartIndex))->value();
8102 int end_offset = Smi::cast(get(i + kRangeEndIndex))->value();
8103 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
8104 int handler_offset = HandlerOffsetField::decode(handler_field);
8105 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
8106 int stack_depth = Smi::cast(get(i + kRangeDepthIndex))->value();
8107 if (pc_offset > start_offset && pc_offset <= end_offset) {
8108 DCHECK_NE(start_offset, innermost_start);
8109 if (start_offset < innermost_start) continue;
8110 innermost_handler = handler_offset;
8111 innermost_start = start_offset;
8112 *stack_depth_out = stack_depth;
8113 if (prediction_out) *prediction_out = prediction;
8116 return innermost_handler;
8120 // TODO(turbofan): Make sure table is sorted and use binary search.
8121 int HandlerTable::LookupReturn(int pc_offset, CatchPrediction* prediction_out) {
8122 for (int i = 0; i < length(); i += kReturnEntrySize) {
8123 int return_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
8124 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
8125 if (pc_offset == return_offset) {
8126 if (prediction_out) {
8127 *prediction_out = HandlerPredictionField::decode(handler_field);
8129 return HandlerOffsetField::decode(handler_field);
8137 bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
8138 if (IsEmpty()) return other->IsEmpty();
8139 if (other->IsEmpty()) return false;
8140 if (length() != other->length()) return false;
8141 for (int i = 0; i < length(); ++i) {
8142 if (get(i) != other->get(i)) return false;
8149 bool String::LooksValid() {
8150 if (!GetIsolate()->heap()->Contains(this)) return false;
8155 String::FlatContent String::GetFlatContent() {
8156 DCHECK(!AllowHeapAllocation::IsAllowed());
8157 int length = this->length();
8158 StringShape shape(this);
8159 String* string = this;
8161 if (shape.representation_tag() == kConsStringTag) {
8162 ConsString* cons = ConsString::cast(string);
8163 if (cons->second()->length() != 0) {
8164 return FlatContent();
8166 string = cons->first();
8167 shape = StringShape(string);
8169 if (shape.representation_tag() == kSlicedStringTag) {
8170 SlicedString* slice = SlicedString::cast(string);
8171 offset = slice->offset();
8172 string = slice->parent();
8173 shape = StringShape(string);
8174 DCHECK(shape.representation_tag() != kConsStringTag &&
8175 shape.representation_tag() != kSlicedStringTag);
8177 if (shape.encoding_tag() == kOneByteStringTag) {
8178 const uint8_t* start;
8179 if (shape.representation_tag() == kSeqStringTag) {
8180 start = SeqOneByteString::cast(string)->GetChars();
8182 start = ExternalOneByteString::cast(string)->GetChars();
8184 return FlatContent(start + offset, length);
8186 DCHECK(shape.encoding_tag() == kTwoByteStringTag);
8188 if (shape.representation_tag() == kSeqStringTag) {
8189 start = SeqTwoByteString::cast(string)->GetChars();
8191 start = ExternalTwoByteString::cast(string)->GetChars();
8193 return FlatContent(start + offset, length);
8198 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8199 RobustnessFlag robust_flag,
8200 int offset, int length,
8201 int* length_return) {
8202 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8203 return base::SmartArrayPointer<char>(NULL);
8205 // Negative length means the to the end of the string.
8206 if (length < 0) length = kMaxInt - offset;
8208 // Compute the size of the UTF-8 string. Start at the specified offset.
8209 StringCharacterStream stream(this, offset);
8210 int character_position = offset;
8212 int last = unibrow::Utf16::kNoPreviousCharacter;
8213 while (stream.HasMore() && character_position++ < offset + length) {
8214 uint16_t character = stream.GetNext();
8215 utf8_bytes += unibrow::Utf8::Length(character, last);
8219 if (length_return) {
8220 *length_return = utf8_bytes;
8223 char* result = NewArray<char>(utf8_bytes + 1);
8225 // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
8226 stream.Reset(this, offset);
8227 character_position = offset;
8228 int utf8_byte_position = 0;
8229 last = unibrow::Utf16::kNoPreviousCharacter;
8230 while (stream.HasMore() && character_position++ < offset + length) {
8231 uint16_t character = stream.GetNext();
8232 if (allow_nulls == DISALLOW_NULLS && character == 0) {
8235 utf8_byte_position +=
8236 unibrow::Utf8::Encode(result + utf8_byte_position, character, last);
8239 result[utf8_byte_position] = 0;
8240 return base::SmartArrayPointer<char>(result);
8244 base::SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
8245 RobustnessFlag robust_flag,
8246 int* length_return) {
8247 return ToCString(allow_nulls, robust_flag, 0, -1, length_return);
8251 const uc16* String::GetTwoByteData(unsigned start) {
8252 DCHECK(!IsOneByteRepresentationUnderneath());
8253 switch (StringShape(this).representation_tag()) {
8255 return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start);
8256 case kExternalStringTag:
8257 return ExternalTwoByteString::cast(this)->
8258 ExternalTwoByteStringGetData(start);
8259 case kSlicedStringTag: {
8260 SlicedString* slice = SlicedString::cast(this);
8261 return slice->parent()->GetTwoByteData(start + slice->offset());
8263 case kConsStringTag:
8272 base::SmartArrayPointer<uc16> String::ToWideCString(
8273 RobustnessFlag robust_flag) {
8274 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
8275 return base::SmartArrayPointer<uc16>();
8277 StringCharacterStream stream(this);
8279 uc16* result = NewArray<uc16>(length() + 1);
8282 while (stream.HasMore()) {
8283 uint16_t character = stream.GetNext();
8284 result[i++] = character;
8287 return base::SmartArrayPointer<uc16>(result);
8291 const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
8292 return reinterpret_cast<uc16*>(
8293 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
8297 void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) {
8298 Relocatable* current = isolate->relocatable_top();
8299 while (current != NULL) {
8300 current->PostGarbageCollection();
8301 current = current->prev_;
8306 // Reserve space for statics needing saving and restoring.
8307 int Relocatable::ArchiveSpacePerThread() {
8308 return sizeof(Relocatable*); // NOLINT
8312 // Archive statics that are thread-local.
8313 char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
8314 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
8315 isolate->set_relocatable_top(NULL);
8316 return to + ArchiveSpacePerThread();
8320 // Restore statics that are thread-local.
8321 char* Relocatable::RestoreState(Isolate* isolate, char* from) {
8322 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
8323 return from + ArchiveSpacePerThread();
8327 char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) {
8328 Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage);
8330 return thread_storage + ArchiveSpacePerThread();
8334 void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) {
8335 Iterate(v, isolate->relocatable_top());
8339 void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) {
8340 Relocatable* current = top;
8341 while (current != NULL) {
8342 current->IterateInstance(v);
8343 current = current->prev_;
8348 FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
8349 : Relocatable(isolate),
8350 str_(str.location()),
8351 length_(str->length()) {
8352 PostGarbageCollection();
8356 FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
8357 : Relocatable(isolate),
8360 length_(input.length()),
8361 start_(input.start()) {}
8364 void FlatStringReader::PostGarbageCollection() {
8365 if (str_ == NULL) return;
8366 Handle<String> str(str_);
8367 DCHECK(str->IsFlat());
8368 DisallowHeapAllocation no_gc;
8369 // This does not actually prevent the vector from being relocated later.
8370 String::FlatContent content = str->GetFlatContent();
8371 DCHECK(content.IsFlat());
8372 is_one_byte_ = content.IsOneByte();
8374 start_ = content.ToOneByteVector().start();
8376 start_ = content.ToUC16Vector().start();
8381 void ConsStringIterator::Initialize(ConsString* cons_string, int offset) {
8382 DCHECK(cons_string != NULL);
8383 root_ = cons_string;
8385 // Force stack blown condition to trigger restart.
8387 maximum_depth_ = kStackSize + depth_;
8388 DCHECK(StackBlown());
8392 String* ConsStringIterator::Continue(int* offset_out) {
8393 DCHECK(depth_ != 0);
8394 DCHECK_EQ(0, *offset_out);
8395 bool blew_stack = StackBlown();
8396 String* string = NULL;
8397 // Get the next leaf if there is one.
8398 if (!blew_stack) string = NextLeaf(&blew_stack);
8399 // Restart search from root.
8401 DCHECK(string == NULL);
8402 string = Search(offset_out);
8404 // Ensure future calls return null immediately.
8405 if (string == NULL) Reset(NULL);
8410 String* ConsStringIterator::Search(int* offset_out) {
8411 ConsString* cons_string = root_;
8412 // Reset the stack, pushing the root string.
8415 frames_[0] = cons_string;
8416 const int consumed = consumed_;
8419 // Loop until the string is found which contains the target offset.
8420 String* string = cons_string->first();
8421 int length = string->length();
8423 if (consumed < offset + length) {
8424 // Target offset is in the left branch.
8425 // Keep going if we're still in a ConString.
8426 type = string->map()->instance_type();
8427 if ((type & kStringRepresentationMask) == kConsStringTag) {
8428 cons_string = ConsString::cast(string);
8429 PushLeft(cons_string);
8432 // Tell the stack we're done descending.
8433 AdjustMaximumDepth();
8436 // Update progress through the string.
8438 // Keep going if we're still in a ConString.
8439 string = cons_string->second();
8440 type = string->map()->instance_type();
8441 if ((type & kStringRepresentationMask) == kConsStringTag) {
8442 cons_string = ConsString::cast(string);
8443 PushRight(cons_string);
8446 // Need this to be updated for the current string.
8447 length = string->length();
8448 // Account for the possibility of an empty right leaf.
8449 // This happens only if we have asked for an offset outside the string.
8451 // Reset so future operations will return null immediately.
8455 // Tell the stack we're done descending.
8456 AdjustMaximumDepth();
8457 // Pop stack so next iteration is in correct place.
8460 DCHECK(length != 0);
8461 // Adjust return values and exit.
8462 consumed_ = offset + length;
8463 *offset_out = consumed - offset;
8471 String* ConsStringIterator::NextLeaf(bool* blew_stack) {
8473 // Tree traversal complete.
8475 *blew_stack = false;
8478 // We've lost track of higher nodes.
8484 ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)];
8485 String* string = cons_string->second();
8486 int32_t type = string->map()->instance_type();
8487 if ((type & kStringRepresentationMask) != kConsStringTag) {
8488 // Pop stack so next iteration is in correct place.
8490 int length = string->length();
8491 // Could be a flattened ConsString.
8492 if (length == 0) continue;
8493 consumed_ += length;
8496 cons_string = ConsString::cast(string);
8497 PushRight(cons_string);
8498 // Need to traverse all the way left.
8501 string = cons_string->first();
8502 type = string->map()->instance_type();
8503 if ((type & kStringRepresentationMask) != kConsStringTag) {
8504 AdjustMaximumDepth();
8505 int length = string->length();
8506 DCHECK(length != 0);
8507 consumed_ += length;
8510 cons_string = ConsString::cast(string);
8511 PushLeft(cons_string);
8519 uint16_t ConsString::ConsStringGet(int index) {
8520 DCHECK(index >= 0 && index < this->length());
8522 // Check for a flattened cons string
8523 if (second()->length() == 0) {
8524 String* left = first();
8525 return left->Get(index);
8528 String* string = String::cast(this);
8531 if (StringShape(string).IsCons()) {
8532 ConsString* cons_string = ConsString::cast(string);
8533 String* left = cons_string->first();
8534 if (left->length() > index) {
8537 index -= left->length();
8538 string = cons_string->second();
8541 return string->Get(index);
8550 uint16_t SlicedString::SlicedStringGet(int index) {
8551 return parent()->Get(offset() + index);
8555 template <typename sinkchar>
8556 void String::WriteToFlat(String* src,
8560 String* source = src;
8564 DCHECK(0 <= from && from <= to && to <= source->length());
8565 switch (StringShape(source).full_representation_tag()) {
8566 case kOneByteStringTag | kExternalStringTag: {
8567 CopyChars(sink, ExternalOneByteString::cast(source)->GetChars() + from,
8571 case kTwoByteStringTag | kExternalStringTag: {
8573 ExternalTwoByteString::cast(source)->GetChars();
8579 case kOneByteStringTag | kSeqStringTag: {
8581 SeqOneByteString::cast(source)->GetChars() + from,
8585 case kTwoByteStringTag | kSeqStringTag: {
8587 SeqTwoByteString::cast(source)->GetChars() + from,
8591 case kOneByteStringTag | kConsStringTag:
8592 case kTwoByteStringTag | kConsStringTag: {
8593 ConsString* cons_string = ConsString::cast(source);
8594 String* first = cons_string->first();
8595 int boundary = first->length();
8596 if (to - boundary >= boundary - from) {
8597 // Right hand side is longer. Recurse over left.
8598 if (from < boundary) {
8599 WriteToFlat(first, sink, from, boundary);
8600 sink += boundary - from;
8606 source = cons_string->second();
8608 // Left hand side is longer. Recurse over right.
8609 if (to > boundary) {
8610 String* second = cons_string->second();
8611 // When repeatedly appending to a string, we get a cons string that
8612 // is unbalanced to the left, a list, essentially. We inline the
8613 // common case of sequential one-byte right child.
8614 if (to - boundary == 1) {
8615 sink[boundary - from] = static_cast<sinkchar>(second->Get(0));
8616 } else if (second->IsSeqOneByteString()) {
8617 CopyChars(sink + boundary - from,
8618 SeqOneByteString::cast(second)->GetChars(),
8622 sink + boundary - from,
8632 case kOneByteStringTag | kSlicedStringTag:
8633 case kTwoByteStringTag | kSlicedStringTag: {
8634 SlicedString* slice = SlicedString::cast(source);
8635 unsigned offset = slice->offset();
8636 WriteToFlat(slice->parent(), sink, from + offset, to + offset);
8645 template <typename SourceChar>
8646 static void CalculateLineEndsImpl(Isolate* isolate,
8647 List<int>* line_ends,
8648 Vector<const SourceChar> src,
8649 bool include_ending_line) {
8650 const int src_len = src.length();
8651 UnicodeCache* cache = isolate->unicode_cache();
8652 for (int i = 0; i < src_len - 1; i++) {
8653 SourceChar current = src[i];
8654 SourceChar next = src[i + 1];
8655 if (cache->IsLineTerminatorSequence(current, next)) line_ends->Add(i);
8658 if (src_len > 0 && cache->IsLineTerminatorSequence(src[src_len - 1], 0)) {
8659 line_ends->Add(src_len - 1);
8660 } else if (include_ending_line) {
8661 // Even if the last line misses a line end, it is counted.
8662 line_ends->Add(src_len);
8667 Handle<FixedArray> String::CalculateLineEnds(Handle<String> src,
8668 bool include_ending_line) {
8670 // Rough estimate of line count based on a roughly estimated average
8671 // length of (unpacked) code.
8672 int line_count_estimate = src->length() >> 4;
8673 List<int> line_ends(line_count_estimate);
8674 Isolate* isolate = src->GetIsolate();
8675 { DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
8676 // Dispatch on type of strings.
8677 String::FlatContent content = src->GetFlatContent();
8678 DCHECK(content.IsFlat());
8679 if (content.IsOneByte()) {
8680 CalculateLineEndsImpl(isolate,
8682 content.ToOneByteVector(),
8683 include_ending_line);
8685 CalculateLineEndsImpl(isolate,
8687 content.ToUC16Vector(),
8688 include_ending_line);
8691 int line_count = line_ends.length();
8692 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
8693 for (int i = 0; i < line_count; i++) {
8694 array->set(i, Smi::FromInt(line_ends[i]));
8700 // Compares the contents of two strings by reading and comparing
8701 // int-sized blocks of characters.
8702 template <typename Char>
8703 static inline bool CompareRawStringContents(const Char* const a,
8704 const Char* const b,
8706 return CompareChars(a, b, length) == 0;
8710 template<typename Chars1, typename Chars2>
8711 class RawStringComparator : public AllStatic {
8713 static inline bool compare(const Chars1* a, const Chars2* b, int len) {
8714 DCHECK(sizeof(Chars1) != sizeof(Chars2));
8715 for (int i = 0; i < len; i++) {
8726 class RawStringComparator<uint16_t, uint16_t> {
8728 static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
8729 return CompareRawStringContents(a, b, len);
8735 class RawStringComparator<uint8_t, uint8_t> {
8737 static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
8738 return CompareRawStringContents(a, b, len);
8743 class StringComparator {
8746 State() : is_one_byte_(true), length_(0), buffer8_(NULL) {}
8748 void Init(String* string) {
8749 ConsString* cons_string = String::VisitFlat(this, string);
8750 iter_.Reset(cons_string);
8751 if (cons_string != NULL) {
8753 string = iter_.Next(&offset);
8754 String::VisitFlat(this, string, offset);
8758 inline void VisitOneByteString(const uint8_t* chars, int length) {
8759 is_one_byte_ = true;
8764 inline void VisitTwoByteString(const uint16_t* chars, int length) {
8765 is_one_byte_ = false;
8770 void Advance(int consumed) {
8771 DCHECK(consumed <= length_);
8773 if (length_ != consumed) {
8775 buffer8_ += consumed;
8777 buffer16_ += consumed;
8779 length_ -= consumed;
8784 String* next = iter_.Next(&offset);
8785 DCHECK_EQ(0, offset);
8786 DCHECK(next != NULL);
8787 String::VisitFlat(this, next);
8790 ConsStringIterator iter_;
8794 const uint8_t* buffer8_;
8795 const uint16_t* buffer16_;
8799 DISALLOW_COPY_AND_ASSIGN(State);
8803 inline StringComparator() {}
8805 template<typename Chars1, typename Chars2>
8806 static inline bool Equals(State* state_1, State* state_2, int to_check) {
8807 const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
8808 const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
8809 return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
8812 bool Equals(String* string_1, String* string_2) {
8813 int length = string_1->length();
8814 state_1_.Init(string_1);
8815 state_2_.Init(string_2);
8817 int to_check = Min(state_1_.length_, state_2_.length_);
8818 DCHECK(to_check > 0 && to_check <= length);
8820 if (state_1_.is_one_byte_) {
8821 if (state_2_.is_one_byte_) {
8822 is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
8824 is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
8827 if (state_2_.is_one_byte_) {
8828 is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
8830 is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
8834 if (!is_equal) return false;
8836 // Exit condition. Strings are equal.
8837 if (length == 0) return true;
8838 state_1_.Advance(to_check);
8839 state_2_.Advance(to_check);
8847 DISALLOW_COPY_AND_ASSIGN(StringComparator);
8851 bool String::SlowEquals(String* other) {
8852 DisallowHeapAllocation no_gc;
8853 // Fast check: negative check with lengths.
8855 if (len != other->length()) return false;
8856 if (len == 0) return true;
8858 // Fast check: if hash code is computed for both strings
8859 // a fast negative check can be performed.
8860 if (HasHashCode() && other->HasHashCode()) {
8861 #ifdef ENABLE_SLOW_DCHECKS
8862 if (FLAG_enable_slow_asserts) {
8863 if (Hash() != other->Hash()) {
8864 bool found_difference = false;
8865 for (int i = 0; i < len; i++) {
8866 if (Get(i) != other->Get(i)) {
8867 found_difference = true;
8871 DCHECK(found_difference);
8875 if (Hash() != other->Hash()) return false;
8878 // We know the strings are both non-empty. Compare the first chars
8879 // before we try to flatten the strings.
8880 if (this->Get(0) != other->Get(0)) return false;
8882 if (IsSeqOneByteString() && other->IsSeqOneByteString()) {
8883 const uint8_t* str1 = SeqOneByteString::cast(this)->GetChars();
8884 const uint8_t* str2 = SeqOneByteString::cast(other)->GetChars();
8885 return CompareRawStringContents(str1, str2, len);
8888 StringComparator comparator;
8889 return comparator.Equals(this, other);
8893 bool String::SlowEquals(Handle<String> one, Handle<String> two) {
8894 // Fast check: negative check with lengths.
8895 int one_length = one->length();
8896 if (one_length != two->length()) return false;
8897 if (one_length == 0) return true;
8899 // Fast check: if hash code is computed for both strings
8900 // a fast negative check can be performed.
8901 if (one->HasHashCode() && two->HasHashCode()) {
8902 #ifdef ENABLE_SLOW_DCHECKS
8903 if (FLAG_enable_slow_asserts) {
8904 if (one->Hash() != two->Hash()) {
8905 bool found_difference = false;
8906 for (int i = 0; i < one_length; i++) {
8907 if (one->Get(i) != two->Get(i)) {
8908 found_difference = true;
8912 DCHECK(found_difference);
8916 if (one->Hash() != two->Hash()) return false;
8919 // We know the strings are both non-empty. Compare the first chars
8920 // before we try to flatten the strings.
8921 if (one->Get(0) != two->Get(0)) return false;
8923 one = String::Flatten(one);
8924 two = String::Flatten(two);
8926 DisallowHeapAllocation no_gc;
8927 String::FlatContent flat1 = one->GetFlatContent();
8928 String::FlatContent flat2 = two->GetFlatContent();
8930 if (flat1.IsOneByte() && flat2.IsOneByte()) {
8931 return CompareRawStringContents(flat1.ToOneByteVector().start(),
8932 flat2.ToOneByteVector().start(),
8935 for (int i = 0; i < one_length; i++) {
8936 if (flat1.Get(i) != flat2.Get(i)) return false;
8943 bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) {
8944 int slen = length();
8945 // Can't check exact length equality, but we can check bounds.
8946 int str_len = str.length();
8947 if (!allow_prefix_match &&
8949 str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) {
8953 size_t remaining_in_str = static_cast<size_t>(str_len);
8954 const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start());
8955 for (i = 0; i < slen && remaining_in_str > 0; i++) {
8957 uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor);
8958 DCHECK(cursor > 0 && cursor <= remaining_in_str);
8959 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
8960 if (i > slen - 1) return false;
8961 if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false;
8962 if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false;
8964 if (Get(i) != r) return false;
8966 utf8_data += cursor;
8967 remaining_in_str -= cursor;
8969 return (allow_prefix_match || i == slen) && remaining_in_str == 0;
8973 bool String::IsOneByteEqualTo(Vector<const uint8_t> str) {
8974 int slen = length();
8975 if (str.length() != slen) return false;
8976 DisallowHeapAllocation no_gc;
8977 FlatContent content = GetFlatContent();
8978 if (content.IsOneByte()) {
8979 return CompareChars(content.ToOneByteVector().start(),
8980 str.start(), slen) == 0;
8982 for (int i = 0; i < slen; i++) {
8983 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
8989 bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
8990 int slen = length();
8991 if (str.length() != slen) return false;
8992 DisallowHeapAllocation no_gc;
8993 FlatContent content = GetFlatContent();
8994 if (content.IsTwoByte()) {
8995 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
8997 for (int i = 0; i < slen; i++) {
8998 if (Get(i) != str[i]) return false;
9004 uint32_t String::ComputeAndSetHash() {
9005 // Should only be called if hash code has not yet been computed.
9006 DCHECK(!HasHashCode());
9008 // Store the hash code in the object.
9009 uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed());
9010 set_hash_field(field);
9012 // Check the hash code is there.
9013 DCHECK(HasHashCode());
9014 uint32_t result = field >> kHashShift;
9015 DCHECK(result != 0); // Ensure that the hash value of 0 is never computed.
9020 bool String::ComputeArrayIndex(uint32_t* index) {
9021 int length = this->length();
9022 if (length == 0 || length > kMaxArrayIndexSize) return false;
9023 StringCharacterStream stream(this);
9024 return StringToArrayIndex(&stream, index);
9028 bool String::SlowAsArrayIndex(uint32_t* index) {
9029 if (length() <= kMaxCachedArrayIndexLength) {
9030 Hash(); // force computation of hash code
9031 uint32_t field = hash_field();
9032 if ((field & kIsNotArrayIndexMask) != 0) return false;
9033 // Isolate the array index form the full hash field.
9034 *index = ArrayIndexValueBits::decode(field);
9037 return ComputeArrayIndex(index);
9042 Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) {
9043 int new_size, old_size;
9044 int old_length = string->length();
9045 if (old_length <= new_length) return string;
9047 if (string->IsSeqOneByteString()) {
9048 old_size = SeqOneByteString::SizeFor(old_length);
9049 new_size = SeqOneByteString::SizeFor(new_length);
9051 DCHECK(string->IsSeqTwoByteString());
9052 old_size = SeqTwoByteString::SizeFor(old_length);
9053 new_size = SeqTwoByteString::SizeFor(new_length);
9056 int delta = old_size - new_size;
9058 Address start_of_string = string->address();
9059 DCHECK_OBJECT_ALIGNED(start_of_string);
9060 DCHECK_OBJECT_ALIGNED(start_of_string + new_size);
9062 Heap* heap = string->GetHeap();
9063 NewSpace* newspace = heap->new_space();
9064 if (newspace->Contains(start_of_string) &&
9065 newspace->top() == start_of_string + old_size) {
9066 // Last allocated object in new space. Simply lower allocation top.
9067 newspace->set_top(start_of_string + new_size);
9069 // Sizes are pointer size aligned, so that we can use filler objects
9070 // that are a multiple of pointer size.
9071 heap->CreateFillerObjectAt(start_of_string + new_size, delta);
9073 heap->AdjustLiveBytes(*string, -delta, Heap::CONCURRENT_TO_SWEEPER);
9075 // We are storing the new length using release store after creating a filler
9076 // for the left-over space to avoid races with the sweeper thread.
9077 string->synchronized_set_length(new_length);
9079 if (new_length == 0) return heap->isolate()->factory()->empty_string();
9084 uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
9085 // For array indexes mix the length into the hash as an array index could
9088 DCHECK(length <= String::kMaxArrayIndexSize);
9089 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
9090 (1 << String::kArrayIndexValueBits));
9092 value <<= String::ArrayIndexValueBits::kShift;
9093 value |= length << String::ArrayIndexLengthBits::kShift;
9095 DCHECK((value & String::kIsNotArrayIndexMask) == 0);
9096 DCHECK((length > String::kMaxCachedArrayIndexLength) ||
9097 (value & String::kContainsCachedArrayIndexMask) == 0);
9102 uint32_t StringHasher::GetHashField() {
9103 if (length_ <= String::kMaxHashCalcLength) {
9104 if (is_array_index_) {
9105 return MakeArrayIndexHash(array_index_, length_);
9107 return (GetHashCore(raw_running_hash_) << String::kHashShift) |
9108 String::kIsNotArrayIndexMask;
9110 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
9115 uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars,
9117 int* utf16_length_out) {
9118 int vector_length = chars.length();
9119 // Handle some edge cases
9120 if (vector_length <= 1) {
9121 DCHECK(vector_length == 0 ||
9122 static_cast<uint8_t>(chars.start()[0]) <=
9123 unibrow::Utf8::kMaxOneByteChar);
9124 *utf16_length_out = vector_length;
9125 return HashSequentialString(chars.start(), vector_length, seed);
9127 // Start with a fake length which won't affect computation.
9128 // It will be updated later.
9129 StringHasher hasher(String::kMaxArrayIndexSize, seed);
9130 size_t remaining = static_cast<size_t>(vector_length);
9131 const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start());
9132 int utf16_length = 0;
9133 bool is_index = true;
9134 DCHECK(hasher.is_array_index_);
9135 while (remaining > 0) {
9136 size_t consumed = 0;
9137 uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed);
9138 DCHECK(consumed > 0 && consumed <= remaining);
9140 remaining -= consumed;
9141 bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode;
9142 utf16_length += is_two_characters ? 2 : 1;
9143 // No need to keep hashing. But we do need to calculate utf16_length.
9144 if (utf16_length > String::kMaxHashCalcLength) continue;
9145 if (is_two_characters) {
9146 uint16_t c1 = unibrow::Utf16::LeadSurrogate(c);
9147 uint16_t c2 = unibrow::Utf16::TrailSurrogate(c);
9148 hasher.AddCharacter(c1);
9149 hasher.AddCharacter(c2);
9150 if (is_index) is_index = hasher.UpdateIndex(c1);
9151 if (is_index) is_index = hasher.UpdateIndex(c2);
9153 hasher.AddCharacter(c);
9154 if (is_index) is_index = hasher.UpdateIndex(c);
9157 *utf16_length_out = static_cast<int>(utf16_length);
9158 // Must set length here so that hash computation is correct.
9159 hasher.length_ = utf16_length;
9160 return hasher.GetHashField();
9164 void IteratingStringHasher::VisitConsString(ConsString* cons_string) {
9165 // Run small ConsStrings through ConsStringIterator.
9166 if (cons_string->length() < 64) {
9167 ConsStringIterator iter(cons_string);
9170 while (nullptr != (string = iter.Next(&offset))) {
9171 DCHECK_EQ(0, offset);
9172 String::VisitFlat(this, string, 0);
9177 const int max_length = String::kMaxHashCalcLength;
9178 int length = std::min(cons_string->length(), max_length);
9179 if (cons_string->HasOnlyOneByteChars()) {
9180 uint8_t* buffer = new uint8_t[length];
9181 String::WriteToFlat(cons_string, buffer, 0, length);
9182 AddCharacters(buffer, length);
9185 uint16_t* buffer = new uint16_t[length];
9186 String::WriteToFlat(cons_string, buffer, 0, length);
9187 AddCharacters(buffer, length);
9193 void String::PrintOn(FILE* file) {
9194 int length = this->length();
9195 for (int i = 0; i < length; i++) {
9196 PrintF(file, "%c", Get(i));
9201 inline static uint32_t ObjectAddressForHashing(Object* object) {
9202 uint32_t value = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(object));
9203 return value & MemoryChunk::kAlignmentMask;
9208 // For performance reasons we only hash the 3 most variable fields of a map:
9209 // constructor, prototype and bit_field2. For predictability reasons we
9210 // use objects' offsets in respective pages for hashing instead of raw
9213 // Shift away the tag.
9214 int hash = ObjectAddressForHashing(GetConstructor()) >> 2;
9216 // XOR-ing the prototype and constructor directly yields too many zero bits
9217 // when the two pointers are close (which is fairly common).
9218 // To avoid this we shift the prototype bits relatively to the constructor.
9219 hash ^= ObjectAddressForHashing(prototype()) << (32 - kPageSizeBits);
9221 return hash ^ (hash >> 16) ^ bit_field2();
9225 static bool CheckEquivalent(Map* first, Map* second) {
9226 return first->GetConstructor() == second->GetConstructor() &&
9227 first->prototype() == second->prototype() &&
9228 first->instance_type() == second->instance_type() &&
9229 first->bit_field() == second->bit_field() &&
9230 first->is_extensible() == second->is_extensible() &&
9231 first->is_strong() == second->is_strong() &&
9232 first->has_instance_call_handler() ==
9233 second->has_instance_call_handler();
9237 bool Map::EquivalentToForTransition(Map* other) {
9238 return CheckEquivalent(this, other);
9242 bool Map::EquivalentToForNormalization(Map* other,
9243 PropertyNormalizationMode mode) {
9245 mode == CLEAR_INOBJECT_PROPERTIES ? 0 : other->GetInObjectProperties();
9246 return CheckEquivalent(this, other) && bit_field2() == other->bit_field2() &&
9247 GetInObjectProperties() == properties;
9251 void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) {
9252 // Iterate over all fields in the body but take care in dealing with
9254 IteratePointers(v, kPropertiesOffset, kCodeEntryOffset);
9255 v->VisitCodeEntry(this->address() + kCodeEntryOffset);
9256 IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size);
9260 bool JSFunction::Inlines(SharedFunctionInfo* candidate) {
9261 DisallowHeapAllocation no_gc;
9262 if (shared() == candidate) return true;
9263 if (code()->kind() != Code::OPTIMIZED_FUNCTION) return false;
9264 DeoptimizationInputData* const data =
9265 DeoptimizationInputData::cast(code()->deoptimization_data());
9266 if (data->length() == 0) return false;
9267 FixedArray* const literals = data->LiteralArray();
9268 int const inlined_count = data->InlinedFunctionCount()->value();
9269 for (int i = 0; i < inlined_count; ++i) {
9270 if (SharedFunctionInfo::cast(literals->get(i)) == candidate) {
9278 void JSFunction::MarkForOptimization() {
9279 Isolate* isolate = GetIsolate();
9280 // Do not optimize if function contains break points.
9281 if (shared()->HasDebugInfo()) return;
9282 DCHECK(!IsOptimized());
9283 DCHECK(shared()->allows_lazy_compilation() ||
9284 !shared()->optimization_disabled());
9285 DCHECK(!shared()->HasDebugInfo());
9286 set_code_no_write_barrier(
9287 isolate->builtins()->builtin(Builtins::kCompileOptimized));
9288 // No write barrier required, since the builtin is part of the root set.
9292 void JSFunction::AttemptConcurrentOptimization() {
9293 Isolate* isolate = GetIsolate();
9294 if (!isolate->concurrent_recompilation_enabled() ||
9295 isolate->bootstrapper()->IsActive()) {
9296 MarkForOptimization();
9299 if (isolate->concurrent_osr_enabled() &&
9300 isolate->optimizing_compile_dispatcher()->IsQueuedForOSR(this)) {
9301 // Do not attempt regular recompilation if we already queued this for OSR.
9302 // TODO(yangguo): This is necessary so that we don't install optimized
9303 // code on a function that is already optimized, since OSR and regular
9304 // recompilation race. This goes away as soon as OSR becomes one-shot.
9307 DCHECK(!IsInOptimizationQueue());
9308 DCHECK(!IsOptimized());
9309 DCHECK(shared()->allows_lazy_compilation() ||
9310 !shared()->optimization_disabled());
9311 DCHECK(isolate->concurrent_recompilation_enabled());
9312 if (FLAG_trace_concurrent_recompilation) {
9313 PrintF(" ** Marking ");
9315 PrintF(" for concurrent recompilation.\n");
9317 set_code_no_write_barrier(
9318 isolate->builtins()->builtin(Builtins::kCompileOptimizedConcurrent));
9319 // No write barrier required, since the builtin is part of the root set.
9323 Handle<JSFunction> JSFunction::CloneClosure(Handle<JSFunction> function) {
9324 Isolate* isolate = function->GetIsolate();
9325 Handle<Map> map(function->map());
9326 Handle<SharedFunctionInfo> shared(function->shared());
9327 Handle<Context> context(function->context());
9328 Handle<JSFunction> clone =
9329 isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context);
9331 if (shared->bound()) {
9332 clone->set_function_bindings(function->function_bindings());
9335 // In typical case, __proto__ of ``function`` is the default Function
9336 // prototype, which means that SetPrototype below is a no-op.
9337 // In rare cases when that is not true, we mutate the clone's __proto__.
9338 Handle<Object> original_prototype(map->prototype(), isolate);
9339 if (*original_prototype != clone->map()->prototype()) {
9340 JSObject::SetPrototype(clone, original_prototype, false).Assert();
9347 void SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap(
9348 Handle<SharedFunctionInfo> shared, Handle<Code> code) {
9349 Isolate* isolate = shared->GetIsolate();
9350 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9351 Handle<Object> value(shared->optimized_code_map(), isolate);
9352 if (value->IsSmi()) return; // Empty code maps are unsupported.
9353 Handle<FixedArray> code_map = Handle<FixedArray>::cast(value);
9354 code_map->set(kSharedCodeIndex, *code);
9358 void SharedFunctionInfo::AddToOptimizedCodeMap(
9359 Handle<SharedFunctionInfo> shared,
9360 Handle<Context> native_context,
9362 Handle<FixedArray> literals,
9363 BailoutId osr_ast_id) {
9364 Isolate* isolate = shared->GetIsolate();
9365 DCHECK(!shared->SearchOptimizedCodeMap(*native_context, osr_ast_id).code);
9366 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
9367 DCHECK(native_context->IsNativeContext());
9368 STATIC_ASSERT(kEntryLength == 4);
9369 Handle<FixedArray> new_code_map;
9370 Handle<Object> value(shared->optimized_code_map(), isolate);
9372 if (value->IsSmi()) {
9373 // No optimized code map.
9374 DCHECK_EQ(0, Smi::cast(*value)->value());
9375 new_code_map = isolate->factory()->NewFixedArray(kInitialLength, TENURED);
9376 old_length = kEntriesStart;
9378 // Copy old optimized code map and append one new entry.
9379 Handle<FixedArray> old_code_map = Handle<FixedArray>::cast(value);
9380 new_code_map = isolate->factory()->CopyFixedArrayAndGrow(
9381 old_code_map, kEntryLength, TENURED);
9382 old_length = old_code_map->length();
9383 // Zap the old map to avoid any stale entries. Note that this is required
9384 // for correctness because entries are being treated weakly by the GC.
9385 MemsetPointer(old_code_map->data_start(), isolate->heap()->the_hole_value(),
9388 new_code_map->set(old_length + kContextOffset, *native_context);
9389 new_code_map->set(old_length + kCachedCodeOffset, *code);
9390 new_code_map->set(old_length + kLiteralsOffset, *literals);
9391 new_code_map->set(old_length + kOsrAstIdOffset,
9392 Smi::FromInt(osr_ast_id.ToInt()));
9395 for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) {
9396 DCHECK(new_code_map->get(i + kContextOffset)->IsNativeContext());
9397 DCHECK(new_code_map->get(i + kCachedCodeOffset)->IsCode());
9398 DCHECK(Code::cast(new_code_map->get(i + kCachedCodeOffset))->kind() ==
9399 Code::OPTIMIZED_FUNCTION);
9400 DCHECK(new_code_map->get(i + kLiteralsOffset)->IsFixedArray());
9401 DCHECK(new_code_map->get(i + kOsrAstIdOffset)->IsSmi());
9404 shared->set_optimized_code_map(*new_code_map);
9408 void SharedFunctionInfo::ClearOptimizedCodeMap() {
9409 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9411 // If the next map link slot is already used then the function was
9412 // enqueued with code flushing and we remove it now.
9413 if (!code_map->get(kNextMapIndex)->IsUndefined()) {
9414 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
9415 flusher->EvictOptimizedCodeMap(this);
9418 DCHECK(code_map->get(kNextMapIndex)->IsUndefined());
9419 set_optimized_code_map(Smi::FromInt(0));
9423 void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code,
9424 const char* reason) {
9425 DisallowHeapAllocation no_gc;
9426 if (optimized_code_map()->IsSmi()) return;
9428 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9429 int dst = kEntriesStart;
9430 int length = code_map->length();
9431 for (int src = kEntriesStart; src < length; src += kEntryLength) {
9432 DCHECK(code_map->get(src)->IsNativeContext());
9433 if (Code::cast(code_map->get(src + kCachedCodeOffset)) == optimized_code) {
9434 // Evict the src entry by not copying it to the dst entry.
9435 if (FLAG_trace_opt) {
9436 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9438 BailoutId osr(Smi::cast(code_map->get(src + kOsrAstIdOffset))->value());
9442 PrintF(" (osr ast id %d)]\n", osr.ToInt());
9446 // Keep the src entry by copying it to the dst entry.
9448 code_map->set(dst + kContextOffset,
9449 code_map->get(src + kContextOffset));
9450 code_map->set(dst + kCachedCodeOffset,
9451 code_map->get(src + kCachedCodeOffset));
9452 code_map->set(dst + kLiteralsOffset,
9453 code_map->get(src + kLiteralsOffset));
9454 code_map->set(dst + kOsrAstIdOffset,
9455 code_map->get(src + kOsrAstIdOffset));
9457 dst += kEntryLength;
9460 if (code_map->get(kSharedCodeIndex) == optimized_code) {
9461 // Evict context-independent code as well.
9462 code_map->set_undefined(kSharedCodeIndex);
9463 if (FLAG_trace_opt) {
9464 PrintF("[evicting entry from optimizing code map (%s) for ", reason);
9466 PrintF(" (context-independent code)]\n");
9469 if (dst != length) {
9470 // Always trim even when array is cleared because of heap verifier.
9471 GetHeap()->RightTrimFixedArray<Heap::CONCURRENT_TO_SWEEPER>(code_map,
9473 if (code_map->length() == kEntriesStart &&
9474 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9475 ClearOptimizedCodeMap();
9481 void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) {
9482 FixedArray* code_map = FixedArray::cast(optimized_code_map());
9483 DCHECK(shrink_by % kEntryLength == 0);
9484 DCHECK(shrink_by <= code_map->length() - kEntriesStart);
9485 // Always trim even when array is cleared because of heap verifier.
9486 GetHeap()->RightTrimFixedArray<Heap::SEQUENTIAL_TO_SWEEPER>(code_map,
9488 if (code_map->length() == kEntriesStart &&
9489 code_map->get(kSharedCodeIndex)->IsUndefined()) {
9490 ClearOptimizedCodeMap();
9495 static void GetMinInobjectSlack(Map* map, void* data) {
9496 int slack = map->unused_property_fields();
9497 if (*reinterpret_cast<int*>(data) > slack) {
9498 *reinterpret_cast<int*>(data) = slack;
9503 static void ShrinkInstanceSize(Map* map, void* data) {
9504 int slack = *reinterpret_cast<int*>(data);
9505 map->SetInObjectProperties(map->GetInObjectProperties() - slack);
9506 map->set_unused_property_fields(map->unused_property_fields() - slack);
9507 map->set_instance_size(map->instance_size() - slack * kPointerSize);
9509 // Visitor id might depend on the instance size, recalculate it.
9510 map->set_visitor_id(StaticVisitorBase::GetVisitorId(map));
9514 void JSFunction::CompleteInobjectSlackTracking() {
9515 DCHECK(has_initial_map());
9516 Map* map = initial_map();
9518 DCHECK(map->counter() >= Map::kSlackTrackingCounterEnd - 1);
9519 map->set_counter(Map::kRetainingCounterStart);
9521 int slack = map->unused_property_fields();
9522 TransitionArray::TraverseTransitionTree(map, &GetMinInobjectSlack, &slack);
9524 // Resize the initial map and all maps in its transition tree.
9525 TransitionArray::TraverseTransitionTree(map, &ShrinkInstanceSize, &slack);
9530 static bool PrototypeBenefitsFromNormalization(Handle<JSObject> object) {
9531 DisallowHeapAllocation no_gc;
9532 if (!object->HasFastProperties()) return false;
9533 Map* map = object->map();
9534 if (map->is_prototype_map()) return false;
9535 DescriptorArray* descriptors = map->instance_descriptors();
9536 for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
9537 PropertyDetails details = descriptors->GetDetails(i);
9538 if (details.location() == kDescriptor) continue;
9539 if (details.representation().IsHeapObject() ||
9540 details.representation().IsTagged()) {
9541 FieldIndex index = FieldIndex::ForDescriptor(map, i);
9542 if (object->RawFastPropertyAt(index)->IsJSFunction()) return true;
9550 void JSObject::OptimizeAsPrototype(Handle<JSObject> object,
9551 PrototypeOptimizationMode mode) {
9552 if (object->IsGlobalObject()) return;
9553 if (object->IsJSGlobalProxy()) return;
9554 if (mode == FAST_PROTOTYPE && PrototypeBenefitsFromNormalization(object)) {
9555 // First normalize to ensure all JSFunctions are DATA_CONSTANT.
9556 JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0,
9557 "NormalizeAsPrototype");
9559 Handle<Map> previous_map(object->map());
9560 if (!object->HasFastProperties()) {
9561 JSObject::MigrateSlowToFast(object, 0, "OptimizeAsPrototype");
9563 if (!object->map()->is_prototype_map()) {
9564 if (object->map() == *previous_map) {
9565 Handle<Map> new_map = Map::Copy(handle(object->map()), "CopyAsPrototype");
9566 JSObject::MigrateToMap(object, new_map);
9568 object->map()->set_is_prototype_map(true);
9570 // Replace the pointer to the exact constructor with the Object function
9571 // from the same context if undetectable from JS. This is to avoid keeping
9572 // memory alive unnecessarily.
9573 Object* maybe_constructor = object->map()->GetConstructor();
9574 if (maybe_constructor->IsJSFunction()) {
9575 JSFunction* constructor = JSFunction::cast(maybe_constructor);
9576 Isolate* isolate = object->GetIsolate();
9577 if (!constructor->shared()->IsApiFunction() &&
9578 object->class_name() == isolate->heap()->Object_string()) {
9579 Handle<String> constructor_name(object->constructor_name(), isolate);
9580 Context* context = constructor->context()->native_context();
9581 JSFunction* object_function = context->object_function();
9582 object->map()->SetConstructor(object_function);
9583 Handle<PrototypeInfo> proto_info =
9584 Map::GetOrCreatePrototypeInfo(object, isolate);
9585 proto_info->set_constructor_name(*constructor_name);
9593 void JSObject::ReoptimizeIfPrototype(Handle<JSObject> object) {
9594 if (!object->map()->is_prototype_map()) return;
9595 OptimizeAsPrototype(object, FAST_PROTOTYPE);
9600 void JSObject::LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9601 DCHECK(FLAG_track_prototype_users);
9602 // Contract: In line with InvalidatePrototypeChains()'s requirements,
9603 // leaf maps don't need to register as users, only prototypes do.
9604 DCHECK(user->is_prototype_map());
9606 Handle<Map> current_user = user;
9607 Handle<PrototypeInfo> current_user_info =
9608 Map::GetOrCreatePrototypeInfo(user, isolate);
9609 for (PrototypeIterator iter(user); !iter.IsAtEnd(); iter.Advance()) {
9610 // Walk up the prototype chain as far as links haven't been registered yet.
9611 if (current_user_info->registry_slot() != PrototypeInfo::UNREGISTERED) {
9614 Handle<Object> maybe_proto = PrototypeIterator::GetCurrent(iter);
9615 if (maybe_proto->IsJSGlobalProxy()) continue;
9616 // Proxies on the prototype chain are not supported.
9617 if (maybe_proto->IsJSProxy()) return;
9618 Handle<JSObject> proto = Handle<JSObject>::cast(maybe_proto);
9619 Handle<PrototypeInfo> proto_info =
9620 Map::GetOrCreatePrototypeInfo(proto, isolate);
9621 Handle<Object> maybe_registry(proto_info->prototype_users(), isolate);
9623 Handle<WeakFixedArray> new_array =
9624 WeakFixedArray::Add(maybe_registry, current_user, &slot);
9625 current_user_info->set_registry_slot(slot);
9626 if (!maybe_registry.is_identical_to(new_array)) {
9627 proto_info->set_prototype_users(*new_array);
9629 if (FLAG_trace_prototype_users) {
9630 PrintF("Registering %p as a user of prototype %p (map=%p).\n",
9631 reinterpret_cast<void*>(*current_user),
9632 reinterpret_cast<void*>(*proto),
9633 reinterpret_cast<void*>(proto->map()));
9636 current_user = handle(proto->map(), isolate);
9637 current_user_info = proto_info;
9642 // Can be called regardless of whether |user| was actually registered with
9643 // |prototype|. Returns true when there was a registration.
9645 bool JSObject::UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate) {
9646 DCHECK(user->is_prototype_map());
9647 // If it doesn't have a PrototypeInfo, it was never registered.
9648 if (!user->prototype_info()->IsPrototypeInfo()) return false;
9649 // If it doesn't have a prototype, it can't be registered.
9650 if (!user->prototype()->IsJSObject()) return false;
9651 Handle<JSObject> prototype(JSObject::cast(user->prototype()), isolate);
9652 Handle<PrototypeInfo> user_info =
9653 Map::GetOrCreatePrototypeInfo(user, isolate);
9654 int slot = user_info->registry_slot();
9655 if (slot == PrototypeInfo::UNREGISTERED) return false;
9656 if (prototype->IsJSGlobalProxy()) {
9657 PrototypeIterator iter(isolate, prototype);
9658 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
9660 DCHECK(prototype->map()->is_prototype_map());
9661 Object* maybe_proto_info = prototype->map()->prototype_info();
9662 // User knows its registry slot, prototype info and user registry must exist.
9663 DCHECK(maybe_proto_info->IsPrototypeInfo());
9664 Handle<PrototypeInfo> proto_info(PrototypeInfo::cast(maybe_proto_info),
9666 Object* maybe_registry = proto_info->prototype_users();
9667 DCHECK(maybe_registry->IsWeakFixedArray());
9668 DCHECK(WeakFixedArray::cast(maybe_registry)->Get(slot) == *user);
9669 WeakFixedArray::cast(maybe_registry)->Clear(slot);
9670 if (FLAG_trace_prototype_users) {
9671 PrintF("Unregistering %p as a user of prototype %p.\n",
9672 reinterpret_cast<void*>(*user), reinterpret_cast<void*>(*prototype));
9678 static void InvalidatePrototypeChainsInternal(Map* map) {
9679 if (!map->is_prototype_map()) return;
9680 if (FLAG_trace_prototype_users) {
9681 PrintF("Invalidating prototype map %p 's cell\n",
9682 reinterpret_cast<void*>(map));
9684 Object* maybe_proto_info = map->prototype_info();
9685 if (!maybe_proto_info->IsPrototypeInfo()) return;
9686 PrototypeInfo* proto_info = PrototypeInfo::cast(maybe_proto_info);
9687 Object* maybe_cell = proto_info->validity_cell();
9688 if (maybe_cell->IsCell()) {
9689 // Just set the value; the cell will be replaced lazily.
9690 Cell* cell = Cell::cast(maybe_cell);
9691 cell->set_value(Smi::FromInt(Map::kPrototypeChainInvalid));
9694 WeakFixedArray::Iterator iterator(proto_info->prototype_users());
9695 // For now, only maps register themselves as users.
9697 while ((user = iterator.Next<Map>())) {
9698 // Walk the prototype chain (backwards, towards leaf objects) if necessary.
9699 InvalidatePrototypeChainsInternal(user);
9705 void JSObject::InvalidatePrototypeChains(Map* map) {
9706 if (!FLAG_eliminate_prototype_chain_checks) return;
9707 DisallowHeapAllocation no_gc;
9708 if (map->IsJSGlobalProxyMap()) {
9709 PrototypeIterator iter(map);
9710 map = JSObject::cast(iter.GetCurrent())->map();
9712 InvalidatePrototypeChainsInternal(map);
9717 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<JSObject> prototype,
9719 Object* maybe_proto_info = prototype->map()->prototype_info();
9720 if (maybe_proto_info->IsPrototypeInfo()) {
9721 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
9723 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
9724 prototype->map()->set_prototype_info(*proto_info);
9730 Handle<PrototypeInfo> Map::GetOrCreatePrototypeInfo(Handle<Map> prototype_map,
9732 Object* maybe_proto_info = prototype_map->prototype_info();
9733 if (maybe_proto_info->IsPrototypeInfo()) {
9734 return handle(PrototypeInfo::cast(maybe_proto_info), isolate);
9736 Handle<PrototypeInfo> proto_info = isolate->factory()->NewPrototypeInfo();
9737 prototype_map->set_prototype_info(*proto_info);
9743 Handle<Cell> Map::GetOrCreatePrototypeChainValidityCell(Handle<Map> map,
9745 Handle<Object> maybe_prototype(map->prototype(), isolate);
9746 if (!maybe_prototype->IsJSObject()) return Handle<Cell>::null();
9747 Handle<JSObject> prototype = Handle<JSObject>::cast(maybe_prototype);
9748 if (prototype->IsJSGlobalProxy()) {
9749 PrototypeIterator iter(isolate, prototype);
9750 prototype = Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
9752 // Ensure the prototype is registered with its own prototypes so its cell
9753 // will be invalidated when necessary.
9754 JSObject::LazyRegisterPrototypeUser(handle(prototype->map(), isolate),
9756 Handle<PrototypeInfo> proto_info =
9757 GetOrCreatePrototypeInfo(prototype, isolate);
9758 Object* maybe_cell = proto_info->validity_cell();
9759 // Return existing cell if it's still valid.
9760 if (maybe_cell->IsCell()) {
9761 Handle<Cell> cell(Cell::cast(maybe_cell), isolate);
9762 if (cell->value() == Smi::FromInt(Map::kPrototypeChainValid)) {
9766 // Otherwise create a new cell.
9767 Handle<Cell> cell = isolate->factory()->NewCell(
9768 handle(Smi::FromInt(Map::kPrototypeChainValid), isolate));
9769 proto_info->set_validity_cell(*cell);
9775 void Map::SetPrototype(Handle<Map> map, Handle<Object> prototype,
9776 PrototypeOptimizationMode proto_mode) {
9777 if (prototype->IsJSObject()) {
9778 Handle<JSObject> prototype_jsobj = Handle<JSObject>::cast(prototype);
9779 JSObject::OptimizeAsPrototype(prototype_jsobj, proto_mode);
9781 WriteBarrierMode wb_mode =
9782 prototype->IsNull() ? SKIP_WRITE_BARRIER : UPDATE_WRITE_BARRIER;
9783 map->set_prototype(*prototype, wb_mode);
9787 Handle<Object> CacheInitialJSArrayMaps(
9788 Handle<Context> native_context, Handle<Map> initial_map) {
9789 // Replace all of the cached initial array maps in the native context with
9790 // the appropriate transitioned elements kind maps.
9791 Factory* factory = native_context->GetIsolate()->factory();
9792 Handle<FixedArray> maps = factory->NewFixedArrayWithHoles(
9793 kElementsKindCount, TENURED);
9795 Handle<Map> current_map = initial_map;
9796 ElementsKind kind = current_map->elements_kind();
9797 DCHECK(kind == GetInitialFastElementsKind());
9798 maps->set(kind, *current_map);
9799 for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1;
9800 i < kFastElementsKindCount; ++i) {
9801 Handle<Map> new_map;
9802 ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i);
9803 Map* maybe_elements_transition = current_map->ElementsTransitionMap();
9804 if (maybe_elements_transition != NULL) {
9805 new_map = handle(maybe_elements_transition);
9806 DCHECK(new_map->elements_kind() == next_kind);
9808 new_map = Map::CopyAsElementsKind(
9809 current_map, next_kind, INSERT_TRANSITION);
9811 maps->set(next_kind, *new_map);
9812 current_map = new_map;
9814 if (initial_map->is_strong())
9815 native_context->set_js_array_strong_maps(*maps);
9817 native_context->set_js_array_maps(*maps);
9822 void JSFunction::SetInstancePrototype(Handle<JSFunction> function,
9823 Handle<Object> value) {
9824 Isolate* isolate = function->GetIsolate();
9826 DCHECK(value->IsJSReceiver());
9828 // Now some logic for the maps of the objects that are created by using this
9829 // function as a constructor.
9830 if (function->has_initial_map()) {
9831 // If the function has allocated the initial map replace it with a
9832 // copy containing the new prototype. Also complete any in-object
9833 // slack tracking that is in progress at this point because it is
9834 // still tracking the old copy.
9835 if (function->IsInobjectSlackTrackingInProgress()) {
9836 function->CompleteInobjectSlackTracking();
9839 Handle<Map> initial_map(function->initial_map(), isolate);
9841 if (!initial_map->GetIsolate()->bootstrapper()->IsActive() &&
9842 initial_map->instance_type() == JS_OBJECT_TYPE) {
9843 // Put the value in the initial map field until an initial map is needed.
9844 // At that point, a new initial map is created and the prototype is put
9845 // into the initial map where it belongs.
9846 function->set_prototype_or_initial_map(*value);
9848 Handle<Map> new_map = Map::Copy(initial_map, "SetInstancePrototype");
9849 JSFunction::SetInitialMap(function, new_map, value);
9851 // If the function is used as the global Array function, cache the
9852 // updated initial maps (and transitioned versions) in the native context.
9853 Handle<Context> native_context(function->context()->native_context(),
9855 Handle<Object> array_function(
9856 native_context->get(Context::ARRAY_FUNCTION_INDEX), isolate);
9857 if (array_function->IsJSFunction() &&
9858 *function == JSFunction::cast(*array_function)) {
9859 CacheInitialJSArrayMaps(native_context, new_map);
9860 Handle<Map> new_strong_map = Map::Copy(new_map, "SetInstancePrototype");
9861 new_strong_map->set_is_strong();
9862 CacheInitialJSArrayMaps(native_context, new_strong_map);
9866 // Deoptimize all code that embeds the previous initial map.
9867 initial_map->dependent_code()->DeoptimizeDependentCodeGroup(
9868 isolate, DependentCode::kInitialMapChangedGroup);
9870 // Put the value in the initial map field until an initial map is
9871 // needed. At that point, a new initial map is created and the
9872 // prototype is put into the initial map where it belongs.
9873 function->set_prototype_or_initial_map(*value);
9874 if (value->IsJSObject()) {
9875 // Optimize as prototype to detach it from its transition tree.
9876 JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value),
9880 isolate->heap()->ClearInstanceofCache();
9884 void JSFunction::SetPrototype(Handle<JSFunction> function,
9885 Handle<Object> value) {
9886 DCHECK(function->should_have_prototype());
9887 Handle<Object> construct_prototype = value;
9889 // If the value is not a JSReceiver, store the value in the map's
9890 // constructor field so it can be accessed. Also, set the prototype
9891 // used for constructing objects to the original object prototype.
9892 // See ECMA-262 13.2.2.
9893 if (!value->IsJSReceiver()) {
9894 // Copy the map so this does not affect unrelated functions.
9895 // Remove map transitions because they point to maps with a
9896 // different prototype.
9897 Handle<Map> new_map = Map::Copy(handle(function->map()), "SetPrototype");
9899 JSObject::MigrateToMap(function, new_map);
9900 new_map->SetConstructor(*value);
9901 new_map->set_non_instance_prototype(true);
9902 Isolate* isolate = new_map->GetIsolate();
9903 construct_prototype = handle(
9904 isolate->context()->native_context()->initial_object_prototype(),
9907 function->map()->set_non_instance_prototype(false);
9910 return SetInstancePrototype(function, construct_prototype);
9914 bool JSFunction::RemovePrototype() {
9915 Context* native_context = context()->native_context();
9916 Map* no_prototype_map =
9917 is_strict(shared()->language_mode())
9918 ? native_context->strict_function_without_prototype_map()
9919 : native_context->sloppy_function_without_prototype_map();
9921 if (map() == no_prototype_map) return true;
9924 if (map() != (is_strict(shared()->language_mode())
9925 ? native_context->strict_function_map()
9926 : native_context->sloppy_function_map())) {
9931 set_map(no_prototype_map);
9932 set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value());
9937 void JSFunction::SetInitialMap(Handle<JSFunction> function, Handle<Map> map,
9938 Handle<Object> prototype) {
9939 if (map->prototype() != *prototype) {
9940 Map::SetPrototype(map, prototype, FAST_PROTOTYPE);
9942 function->set_prototype_or_initial_map(*map);
9943 map->SetConstructor(*function);
9945 if (FLAG_trace_maps) {
9946 PrintF("[TraceMaps: InitialMap map= %p SFI= %d_%s ]\n",
9947 reinterpret_cast<void*>(*map), function->shared()->unique_id(),
9948 function->shared()->DebugName()->ToCString().get());
9954 void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) {
9955 if (function->has_initial_map()) return;
9956 Isolate* isolate = function->GetIsolate();
9958 // First create a new map with the size and number of in-object properties
9959 // suggested by the function.
9960 InstanceType instance_type;
9962 int in_object_properties;
9963 if (function->shared()->is_generator()) {
9964 instance_type = JS_GENERATOR_OBJECT_TYPE;
9965 instance_size = JSGeneratorObject::kSize;
9966 in_object_properties = 0;
9968 instance_type = JS_OBJECT_TYPE;
9969 instance_size = function->shared()->CalculateInstanceSize();
9970 in_object_properties = function->shared()->CalculateInObjectProperties();
9972 Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size);
9974 // Fetch or allocate prototype.
9975 Handle<Object> prototype;
9976 if (function->has_instance_prototype()) {
9977 prototype = handle(function->instance_prototype(), isolate);
9979 prototype = isolate->factory()->NewFunctionPrototype(function);
9981 map->SetInObjectProperties(in_object_properties);
9982 map->set_unused_property_fields(in_object_properties);
9983 DCHECK(map->has_fast_object_elements());
9985 // Finally link initial map and constructor function.
9986 JSFunction::SetInitialMap(function, map, Handle<JSReceiver>::cast(prototype));
9988 if (!function->shared()->is_generator()) {
9989 function->StartInobjectSlackTracking();
9994 void JSFunction::SetInstanceClassName(String* name) {
9995 shared()->set_instance_class_name(name);
9999 void JSFunction::PrintName(FILE* out) {
10000 base::SmartArrayPointer<char> name = shared()->DebugName()->ToCString();
10001 PrintF(out, "%s", name.get());
10005 // The filter is a pattern that matches function names in this way:
10006 // "*" all; the default
10007 // "-" all but the top-level function
10008 // "-name" all but the function "name"
10009 // "" only the top-level function
10010 // "name" only the function "name"
10011 // "name*" only functions starting with "name"
10012 // "~" none; the tilde is not an identifier
10013 bool JSFunction::PassesFilter(const char* raw_filter) {
10014 if (*raw_filter == '*') return true;
10015 String* name = shared()->DebugName();
10016 Vector<const char> filter = CStrVector(raw_filter);
10017 if (filter.length() == 0) return name->length() == 0;
10018 if (filter[0] == '-') {
10019 // Negative filter.
10020 if (filter.length() == 1) {
10021 return (name->length() != 0);
10022 } else if (name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) {
10025 if (filter[filter.length() - 1] == '*' &&
10026 name->IsUtf8EqualTo(filter.SubVector(1, filter.length() - 1), true)) {
10031 } else if (name->IsUtf8EqualTo(filter)) {
10034 if (filter[filter.length() - 1] == '*' &&
10035 name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) {
10042 Handle<String> JSFunction::GetDebugName(Handle<JSFunction> function) {
10043 Isolate* isolate = function->GetIsolate();
10044 Handle<Object> name =
10045 JSReceiver::GetDataProperty(function, isolate->factory()->name_string());
10046 if (name->IsString()) return Handle<String>::cast(name);
10047 return handle(function->shared()->DebugName(), isolate);
10051 void Oddball::Initialize(Isolate* isolate, Handle<Oddball> oddball,
10052 const char* to_string, Handle<Object> to_number,
10053 const char* type_of, byte kind) {
10054 Handle<String> internalized_to_string =
10055 isolate->factory()->InternalizeUtf8String(to_string);
10056 Handle<String> internalized_type_of =
10057 isolate->factory()->InternalizeUtf8String(type_of);
10058 oddball->set_to_number(*to_number);
10059 oddball->set_to_string(*internalized_to_string);
10060 oddball->set_type_of(*internalized_type_of);
10061 oddball->set_kind(kind);
10065 void Script::InitLineEnds(Handle<Script> script) {
10066 if (!script->line_ends()->IsUndefined()) return;
10068 Isolate* isolate = script->GetIsolate();
10070 if (!script->source()->IsString()) {
10071 DCHECK(script->source()->IsUndefined());
10072 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
10073 script->set_line_ends(*empty);
10074 DCHECK(script->line_ends()->IsFixedArray());
10078 Handle<String> src(String::cast(script->source()), isolate);
10080 Handle<FixedArray> array = String::CalculateLineEnds(src, true);
10082 if (*array != isolate->heap()->empty_fixed_array()) {
10083 array->set_map(isolate->heap()->fixed_cow_array_map());
10086 script->set_line_ends(*array);
10087 DCHECK(script->line_ends()->IsFixedArray());
10091 int Script::GetColumnNumber(Handle<Script> script, int code_pos) {
10092 int line_number = GetLineNumber(script, code_pos);
10093 if (line_number == -1) return -1;
10095 DisallowHeapAllocation no_allocation;
10096 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
10097 line_number = line_number - script->line_offset()->value();
10098 if (line_number == 0) return code_pos + script->column_offset()->value();
10099 int prev_line_end_pos =
10100 Smi::cast(line_ends_array->get(line_number - 1))->value();
10101 return code_pos - (prev_line_end_pos + 1);
10105 int Script::GetLineNumberWithArray(int code_pos) {
10106 DisallowHeapAllocation no_allocation;
10107 DCHECK(line_ends()->IsFixedArray());
10108 FixedArray* line_ends_array = FixedArray::cast(line_ends());
10109 int line_ends_len = line_ends_array->length();
10110 if (line_ends_len == 0) return -1;
10112 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
10113 return line_offset()->value();
10117 int right = line_ends_len;
10118 while (int half = (right - left) / 2) {
10119 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
10125 return right + line_offset()->value();
10129 int Script::GetLineNumber(Handle<Script> script, int code_pos) {
10130 InitLineEnds(script);
10131 return script->GetLineNumberWithArray(code_pos);
10135 int Script::GetLineNumber(int code_pos) {
10136 DisallowHeapAllocation no_allocation;
10137 if (!line_ends()->IsUndefined()) return GetLineNumberWithArray(code_pos);
10139 // Slow mode: we do not have line_ends. We have to iterate through source.
10140 if (!source()->IsString()) return -1;
10142 String* source_string = String::cast(source());
10144 int len = source_string->length();
10145 for (int pos = 0; pos < len; pos++) {
10146 if (pos == code_pos) break;
10147 if (source_string->Get(pos) == '\n') line++;
10153 Handle<Object> Script::GetNameOrSourceURL(Handle<Script> script) {
10154 Isolate* isolate = script->GetIsolate();
10155 Handle<String> name_or_source_url_key =
10156 isolate->factory()->InternalizeOneByteString(
10157 STATIC_CHAR_VECTOR("nameOrSourceURL"));
10158 Handle<JSObject> script_wrapper = Script::GetWrapper(script);
10159 Handle<Object> property = Object::GetProperty(
10160 script_wrapper, name_or_source_url_key).ToHandleChecked();
10161 DCHECK(property->IsJSFunction());
10162 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
10163 Handle<Object> result;
10164 // Do not check against pending exception, since this function may be called
10165 // when an exception has already been pending.
10166 if (!Execution::TryCall(method, script_wrapper, 0, NULL).ToHandle(&result)) {
10167 return isolate->factory()->undefined_value();
10173 Handle<JSObject> Script::GetWrapper(Handle<Script> script) {
10174 Isolate* isolate = script->GetIsolate();
10175 if (!script->wrapper()->IsUndefined()) {
10176 DCHECK(script->wrapper()->IsWeakCell());
10177 Handle<WeakCell> cell(WeakCell::cast(script->wrapper()));
10178 if (!cell->cleared()) {
10179 // Return a handle for the existing script wrapper from the cache.
10180 return handle(JSObject::cast(cell->value()));
10182 // If we found an empty WeakCell, that means the script wrapper was
10183 // GCed. We are not notified directly of that, so we decrement here
10184 // so that we at least don't count double for any given script.
10185 isolate->counters()->script_wrappers()->Decrement();
10187 // Construct a new script wrapper.
10188 isolate->counters()->script_wrappers()->Increment();
10189 Handle<JSFunction> constructor = isolate->script_function();
10190 Handle<JSValue> result =
10191 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
10192 result->set_value(*script);
10193 Handle<WeakCell> cell = isolate->factory()->NewWeakCell(result);
10194 script->set_wrapper(*cell);
10199 MaybeHandle<SharedFunctionInfo> Script::FindSharedFunctionInfo(
10200 FunctionLiteral* fun) {
10201 WeakFixedArray::Iterator iterator(shared_function_infos());
10202 SharedFunctionInfo* shared;
10203 while ((shared = iterator.Next<SharedFunctionInfo>())) {
10204 if (fun->function_token_position() == shared->function_token_position() &&
10205 fun->start_position() == shared->start_position()) {
10206 return Handle<SharedFunctionInfo>(shared);
10209 return MaybeHandle<SharedFunctionInfo>();
10213 Script::Iterator::Iterator(Isolate* isolate)
10214 : iterator_(isolate->heap()->script_list()) {}
10217 Script* Script::Iterator::Next() { return iterator_.Next<Script>(); }
10220 SharedFunctionInfo::Iterator::Iterator(Isolate* isolate)
10221 : script_iterator_(isolate), sfi_iterator_(NULL) {
10226 bool SharedFunctionInfo::Iterator::NextScript() {
10227 Script* script = script_iterator_.Next();
10228 if (script == NULL) return false;
10229 sfi_iterator_.Reset(script->shared_function_infos());
10234 SharedFunctionInfo* SharedFunctionInfo::Iterator::Next() {
10236 SharedFunctionInfo* next = sfi_iterator_.Next<SharedFunctionInfo>();
10237 if (next != NULL) return next;
10238 } while (NextScript());
10243 void SharedFunctionInfo::SetScript(Handle<SharedFunctionInfo> shared,
10244 Handle<Object> script_object) {
10245 if (shared->script() == *script_object) return;
10246 // Remove shared function info from old script's list.
10247 if (shared->script()->IsScript()) {
10248 Script* old_script = Script::cast(shared->script());
10249 if (old_script->shared_function_infos()->IsWeakFixedArray()) {
10250 WeakFixedArray* list =
10251 WeakFixedArray::cast(old_script->shared_function_infos());
10252 list->Remove(shared);
10255 // Add shared function info to new script's list.
10256 if (script_object->IsScript()) {
10257 Handle<Script> script = Handle<Script>::cast(script_object);
10258 Handle<Object> list(script->shared_function_infos(), shared->GetIsolate());
10261 WeakFixedArray::Iterator iterator(*list);
10262 SharedFunctionInfo* next;
10263 while ((next = iterator.Next<SharedFunctionInfo>())) {
10264 DCHECK_NE(next, *shared);
10268 list = WeakFixedArray::Add(list, shared);
10269 script->set_shared_function_infos(*list);
10271 // Finally set new script.
10272 shared->set_script(*script_object);
10276 String* SharedFunctionInfo::DebugName() {
10277 Object* n = name();
10278 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name();
10279 return String::cast(n);
10283 bool SharedFunctionInfo::HasSourceCode() const {
10284 return !script()->IsUndefined() &&
10285 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
10289 Handle<Object> SharedFunctionInfo::GetSourceCode() {
10290 if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value();
10291 Handle<String> source(String::cast(Script::cast(script())->source()));
10292 return GetIsolate()->factory()->NewSubString(
10293 source, start_position(), end_position());
10297 bool SharedFunctionInfo::IsInlineable() {
10298 // Check that the function has a script associated with it.
10299 if (!script()->IsScript()) return false;
10300 return !optimization_disabled();
10304 int SharedFunctionInfo::SourceSize() {
10305 return end_position() - start_position();
10309 int SharedFunctionInfo::CalculateInstanceSize() {
10310 int instance_size =
10311 JSObject::kHeaderSize +
10312 expected_nof_properties() * kPointerSize;
10313 if (instance_size > JSObject::kMaxInstanceSize) {
10314 instance_size = JSObject::kMaxInstanceSize;
10316 return instance_size;
10320 int SharedFunctionInfo::CalculateInObjectProperties() {
10321 return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize;
10325 // Output the source code without any allocation in the heap.
10326 std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v) {
10327 const SharedFunctionInfo* s = v.value;
10328 // For some native functions there is no source.
10329 if (!s->HasSourceCode()) return os << "<No Source>";
10331 // Get the source for the script which this function came from.
10332 // Don't use String::cast because we don't want more assertion errors while
10333 // we are already creating a stack dump.
10334 String* script_source =
10335 reinterpret_cast<String*>(Script::cast(s->script())->source());
10337 if (!script_source->LooksValid()) return os << "<Invalid Source>";
10339 if (!s->is_toplevel()) {
10341 Object* name = s->name();
10342 if (name->IsString() && String::cast(name)->length() > 0) {
10343 String::cast(name)->PrintUC16(os);
10347 int len = s->end_position() - s->start_position();
10348 if (len <= v.max_length || v.max_length < 0) {
10349 script_source->PrintUC16(os, s->start_position(), s->end_position());
10352 script_source->PrintUC16(os, s->start_position(),
10353 s->start_position() + v.max_length);
10354 return os << "...\n";
10359 static bool IsCodeEquivalent(Code* code, Code* recompiled) {
10360 if (code->instruction_size() != recompiled->instruction_size()) return false;
10361 ByteArray* code_relocation = code->relocation_info();
10362 ByteArray* recompiled_relocation = recompiled->relocation_info();
10363 int length = code_relocation->length();
10364 if (length != recompiled_relocation->length()) return false;
10365 int compare = memcmp(code_relocation->GetDataStartAddress(),
10366 recompiled_relocation->GetDataStartAddress(),
10368 return compare == 0;
10372 void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) {
10373 DCHECK(!has_deoptimization_support());
10374 DisallowHeapAllocation no_allocation;
10375 Code* code = this->code();
10376 if (IsCodeEquivalent(code, recompiled)) {
10377 // Copy the deoptimization data from the recompiled code.
10378 code->set_deoptimization_data(recompiled->deoptimization_data());
10379 code->set_has_deoptimization_support(true);
10381 // TODO(3025757): In case the recompiled isn't equivalent to the
10382 // old code, we have to replace it. We should try to avoid this
10383 // altogether because it flushes valuable type feedback by
10384 // effectively resetting all IC state.
10385 ReplaceCode(recompiled);
10387 DCHECK(has_deoptimization_support());
10391 void SharedFunctionInfo::DisableOptimization(BailoutReason reason) {
10392 // Disable optimization for the shared function info and mark the
10393 // code as non-optimizable. The marker on the shared function info
10394 // is there because we flush non-optimized code thereby loosing the
10395 // non-optimizable information for the code. When the code is
10396 // regenerated and set on the shared function info it is marked as
10397 // non-optimizable if optimization is disabled for the shared
10399 DCHECK(reason != kNoReason);
10400 set_optimization_disabled(true);
10401 set_disable_optimization_reason(reason);
10402 // Code should be the lazy compilation stub or else unoptimized.
10403 DCHECK(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN);
10404 PROFILE(GetIsolate(), CodeDisableOptEvent(code(), this));
10405 if (FLAG_trace_opt) {
10406 PrintF("[disabled optimization for ");
10408 PrintF(", reason: %s]\n", GetBailoutReason(reason));
10413 void SharedFunctionInfo::InitFromFunctionLiteral(
10414 Handle<SharedFunctionInfo> shared_info, FunctionLiteral* lit) {
10415 shared_info->set_length(lit->scope()->default_function_length());
10416 shared_info->set_internal_formal_parameter_count(lit->parameter_count());
10417 shared_info->set_function_token_position(lit->function_token_position());
10418 shared_info->set_start_position(lit->start_position());
10419 shared_info->set_end_position(lit->end_position());
10420 shared_info->set_is_expression(lit->is_expression());
10421 shared_info->set_is_anonymous(lit->is_anonymous());
10422 shared_info->set_inferred_name(*lit->inferred_name());
10423 shared_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
10424 shared_info->set_allows_lazy_compilation_without_context(
10425 lit->AllowsLazyCompilationWithoutContext());
10426 shared_info->set_language_mode(lit->language_mode());
10427 shared_info->set_uses_arguments(lit->scope()->arguments() != NULL);
10428 shared_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
10429 shared_info->set_ast_node_count(lit->ast_node_count());
10430 shared_info->set_is_function(lit->is_function());
10431 if (lit->dont_optimize_reason() != kNoReason) {
10432 shared_info->DisableOptimization(lit->dont_optimize_reason());
10434 shared_info->set_dont_crankshaft(lit->flags() &
10435 AstProperties::kDontCrankshaft);
10436 shared_info->set_kind(lit->kind());
10437 shared_info->set_needs_home_object(lit->scope()->NeedsHomeObject());
10438 shared_info->set_asm_function(lit->scope()->asm_function());
10442 bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) {
10443 DCHECK(!id.IsNone());
10444 Code* unoptimized = code();
10445 DeoptimizationOutputData* data =
10446 DeoptimizationOutputData::cast(unoptimized->deoptimization_data());
10447 unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this);
10449 return true; // Return true if there was no DCHECK.
10453 void JSFunction::StartInobjectSlackTracking() {
10454 DCHECK(has_initial_map() && !IsInobjectSlackTrackingInProgress());
10456 Map* map = initial_map();
10458 // No tracking during the snapshot construction phase.
10459 Isolate* isolate = GetIsolate();
10460 if (isolate->serializer_enabled()) return;
10462 if (map->unused_property_fields() == 0) return;
10464 map->set_counter(Map::kSlackTrackingCounterStart);
10468 void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
10469 code()->ClearInlineCaches();
10470 // If we clear ICs, we need to clear the type feedback vector too, since
10471 // CallICs are synced with a feedback vector slot.
10472 ClearTypeFeedbackInfo();
10473 set_ic_age(new_ic_age);
10474 if (code()->kind() == Code::FUNCTION) {
10475 code()->set_profiler_ticks(0);
10476 if (optimization_disabled() &&
10477 opt_count() >= FLAG_max_opt_count) {
10478 // Re-enable optimizations if they were disabled due to opt_count limit.
10479 set_optimization_disabled(false);
10482 set_deopt_count(0);
10487 CodeAndLiterals SharedFunctionInfo::SearchOptimizedCodeMap(
10488 Context* native_context, BailoutId osr_ast_id) {
10489 DisallowHeapAllocation no_gc;
10490 DCHECK(native_context->IsNativeContext());
10491 Object* value = optimized_code_map();
10492 if (!value->IsSmi()) {
10493 FixedArray* optimized_code_map = FixedArray::cast(value);
10494 int length = optimized_code_map->length();
10495 Smi* osr_ast_id_smi = Smi::FromInt(osr_ast_id.ToInt());
10496 for (int i = kEntriesStart; i < length; i += kEntryLength) {
10497 if (optimized_code_map->get(i + kContextOffset) == native_context &&
10498 optimized_code_map->get(i + kOsrAstIdOffset) == osr_ast_id_smi) {
10499 return {Code::cast(optimized_code_map->get(i + kCachedCodeOffset)),
10500 FixedArray::cast(optimized_code_map->get(i + kLiteralsOffset))};
10503 Object* shared_code = optimized_code_map->get(kSharedCodeIndex);
10504 if (shared_code->IsCode() && osr_ast_id.IsNone()) {
10505 return {Code::cast(shared_code), nullptr};
10507 if (FLAG_trace_opt) {
10508 PrintF("[didn't find optimized code in optimized code map for ");
10513 return {nullptr, nullptr};
10517 #define DECLARE_TAG(ignore1, name, ignore2) name,
10518 const char* const VisitorSynchronization::kTags[
10519 VisitorSynchronization::kNumberOfSyncTags] = {
10520 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10525 #define DECLARE_TAG(ignore1, ignore2, name) name,
10526 const char* const VisitorSynchronization::kTagNames[
10527 VisitorSynchronization::kNumberOfSyncTags] = {
10528 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
10533 void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) {
10534 DCHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
10535 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
10536 Object* old_target = target;
10537 VisitPointer(&target);
10538 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10542 void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) {
10543 DCHECK(RelocInfo::IsCodeAgeSequence(rinfo->rmode()));
10544 Object* stub = rinfo->code_age_stub();
10546 VisitPointer(&stub);
10551 void ObjectVisitor::VisitCodeEntry(Address entry_address) {
10552 Object* code = Code::GetObjectFromEntryAddress(entry_address);
10553 Object* old_code = code;
10554 VisitPointer(&code);
10555 if (code != old_code) {
10556 Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry();
10561 void ObjectVisitor::VisitCell(RelocInfo* rinfo) {
10562 DCHECK(rinfo->rmode() == RelocInfo::CELL);
10563 Object* cell = rinfo->target_cell();
10564 Object* old_cell = cell;
10565 VisitPointer(&cell);
10566 if (cell != old_cell) {
10567 rinfo->set_target_cell(reinterpret_cast<Cell*>(cell));
10572 void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
10573 DCHECK(RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
10574 rinfo->IsPatchedDebugBreakSlotSequence());
10575 Object* target = Code::GetCodeFromTargetAddress(rinfo->debug_call_address());
10576 Object* old_target = target;
10577 VisitPointer(&target);
10578 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
10582 void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) {
10583 DCHECK(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
10584 Object* p = rinfo->target_object();
10589 void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) {
10590 Address p = rinfo->target_external_reference();
10591 VisitExternalReference(&p);
10595 void Code::InvalidateRelocation() {
10596 InvalidateEmbeddedObjects();
10597 set_relocation_info(GetHeap()->empty_byte_array());
10601 void Code::InvalidateEmbeddedObjects() {
10602 Object* undefined = GetHeap()->undefined_value();
10603 Cell* undefined_cell = GetHeap()->undefined_cell();
10604 int mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10605 RelocInfo::ModeMask(RelocInfo::CELL);
10606 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10607 RelocInfo::Mode mode = it.rinfo()->rmode();
10608 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10609 it.rinfo()->set_target_object(undefined, SKIP_WRITE_BARRIER);
10610 } else if (mode == RelocInfo::CELL) {
10611 it.rinfo()->set_target_cell(undefined_cell, SKIP_WRITE_BARRIER);
10617 void Code::Relocate(intptr_t delta) {
10618 for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) {
10619 it.rinfo()->apply(delta);
10621 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10625 void Code::CopyFrom(const CodeDesc& desc) {
10626 DCHECK(Marking::Color(this) == Marking::WHITE_OBJECT);
10629 CopyBytes(instruction_start(), desc.buffer,
10630 static_cast<size_t>(desc.instr_size));
10633 CopyBytes(relocation_start(),
10634 desc.buffer + desc.buffer_size - desc.reloc_size,
10635 static_cast<size_t>(desc.reloc_size));
10637 // unbox handles and relocate
10638 intptr_t delta = instruction_start() - desc.buffer;
10639 int mode_mask = RelocInfo::kCodeTargetMask |
10640 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
10641 RelocInfo::ModeMask(RelocInfo::CELL) |
10642 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
10643 RelocInfo::kApplyMask;
10644 // Needed to find target_object and runtime_entry on X64
10645 Assembler* origin = desc.origin;
10646 AllowDeferredHandleDereference embedding_raw_address;
10647 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
10648 RelocInfo::Mode mode = it.rinfo()->rmode();
10649 if (mode == RelocInfo::EMBEDDED_OBJECT) {
10650 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10651 it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10652 } else if (mode == RelocInfo::CELL) {
10653 Handle<Cell> cell = it.rinfo()->target_cell_handle();
10654 it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER, SKIP_ICACHE_FLUSH);
10655 } else if (RelocInfo::IsCodeTarget(mode)) {
10656 // rewrite code handles in inline cache targets to direct
10657 // pointers to the first instruction in the code object
10658 Handle<Object> p = it.rinfo()->target_object_handle(origin);
10659 Code* code = Code::cast(*p);
10660 it.rinfo()->set_target_address(code->instruction_start(),
10661 SKIP_WRITE_BARRIER,
10662 SKIP_ICACHE_FLUSH);
10663 } else if (RelocInfo::IsRuntimeEntry(mode)) {
10664 Address p = it.rinfo()->target_runtime_entry(origin);
10665 it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER,
10666 SKIP_ICACHE_FLUSH);
10667 } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) {
10668 Handle<Object> p = it.rinfo()->code_age_stub_handle(origin);
10669 Code* code = Code::cast(*p);
10670 it.rinfo()->set_code_age_stub(code, SKIP_ICACHE_FLUSH);
10672 it.rinfo()->apply(delta);
10675 CpuFeatures::FlushICache(instruction_start(), instruction_size());
10679 // Locate the source position which is closest to the address in the code. This
10680 // is using the source position information embedded in the relocation info.
10681 // The position returned is relative to the beginning of the script where the
10682 // source for this function is found.
10683 int Code::SourcePosition(Address pc) {
10684 int distance = kMaxInt;
10685 int position = RelocInfo::kNoPosition; // Initially no position found.
10686 // Run through all the relocation info to find the best matching source
10687 // position. All the code needs to be considered as the sequence of the
10688 // instructions in the code does not necessarily follow the same order as the
10690 RelocIterator it(this, RelocInfo::kPositionMask);
10691 while (!it.done()) {
10692 // Only look at positions after the current pc.
10693 if (it.rinfo()->pc() < pc) {
10694 // Get position and distance.
10696 int dist = static_cast<int>(pc - it.rinfo()->pc());
10697 int pos = static_cast<int>(it.rinfo()->data());
10698 // If this position is closer than the current candidate or if it has the
10699 // same distance as the current candidate and the position is higher then
10700 // this position is the new candidate.
10701 if ((dist < distance) ||
10702 (dist == distance && pos > position)) {
10713 // Same as Code::SourcePosition above except it only looks for statement
10715 int Code::SourceStatementPosition(Address pc) {
10716 // First find the position as close as possible using all position
10718 int position = SourcePosition(pc);
10719 // Now find the closest statement position before the position.
10720 int statement_position = 0;
10721 RelocIterator it(this, RelocInfo::kPositionMask);
10722 while (!it.done()) {
10723 if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) {
10724 int p = static_cast<int>(it.rinfo()->data());
10725 if (statement_position < p && p <= position) {
10726 statement_position = p;
10731 return statement_position;
10735 SafepointEntry Code::GetSafepointEntry(Address pc) {
10736 SafepointTable table(this);
10737 return table.FindEntry(pc);
10741 Object* Code::FindNthObject(int n, Map* match_map) {
10742 DCHECK(is_inline_cache_stub());
10743 DisallowHeapAllocation no_allocation;
10744 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10745 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10746 RelocInfo* info = it.rinfo();
10747 Object* object = info->target_object();
10748 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10749 if (object->IsHeapObject()) {
10750 if (HeapObject::cast(object)->map() == match_map) {
10751 if (--n == 0) return object;
10759 AllocationSite* Code::FindFirstAllocationSite() {
10760 Object* result = FindNthObject(1, GetHeap()->allocation_site_map());
10761 return (result != NULL) ? AllocationSite::cast(result) : NULL;
10765 Map* Code::FindFirstMap() {
10766 Object* result = FindNthObject(1, GetHeap()->meta_map());
10767 return (result != NULL) ? Map::cast(result) : NULL;
10771 void Code::FindAndReplace(const FindAndReplacePattern& pattern) {
10772 DCHECK(is_inline_cache_stub() || is_handler());
10773 DisallowHeapAllocation no_allocation;
10774 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10775 STATIC_ASSERT(FindAndReplacePattern::kMaxCount < 32);
10776 int current_pattern = 0;
10777 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10778 RelocInfo* info = it.rinfo();
10779 Object* object = info->target_object();
10780 if (object->IsHeapObject()) {
10781 if (object->IsWeakCell()) {
10782 object = HeapObject::cast(WeakCell::cast(object)->value());
10784 Map* map = HeapObject::cast(object)->map();
10785 if (map == *pattern.find_[current_pattern]) {
10786 info->set_target_object(*pattern.replace_[current_pattern]);
10787 if (++current_pattern == pattern.count_) return;
10795 void Code::FindAllMaps(MapHandleList* maps) {
10796 DCHECK(is_inline_cache_stub());
10797 DisallowHeapAllocation no_allocation;
10798 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10799 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10800 RelocInfo* info = it.rinfo();
10801 Object* object = info->target_object();
10802 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10803 if (object->IsMap()) maps->Add(handle(Map::cast(object)));
10808 Code* Code::FindFirstHandler() {
10809 DCHECK(is_inline_cache_stub());
10810 DisallowHeapAllocation no_allocation;
10811 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10812 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10813 bool skip_next_handler = false;
10814 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10815 RelocInfo* info = it.rinfo();
10816 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10817 Object* obj = info->target_object();
10818 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
10820 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10821 if (code->kind() == Code::HANDLER) {
10822 if (!skip_next_handler) return code;
10823 skip_next_handler = false;
10831 bool Code::FindHandlers(CodeHandleList* code_list, int length) {
10832 DCHECK(is_inline_cache_stub());
10833 DisallowHeapAllocation no_allocation;
10834 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10835 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10836 bool skip_next_handler = false;
10838 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10839 if (i == length) return true;
10840 RelocInfo* info = it.rinfo();
10841 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10842 Object* obj = info->target_object();
10843 skip_next_handler |= obj->IsWeakCell() && WeakCell::cast(obj)->cleared();
10845 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10846 // IC stubs with handlers never contain non-handler code objects before
10847 // handler targets.
10848 if (code->kind() != Code::HANDLER) break;
10849 if (!skip_next_handler) {
10850 code_list->Add(Handle<Code>(code));
10853 skip_next_handler = false;
10856 return i == length;
10860 MaybeHandle<Code> Code::FindHandlerForMap(Map* map) {
10861 DCHECK(is_inline_cache_stub());
10862 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10863 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10864 bool return_next = false;
10865 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10866 RelocInfo* info = it.rinfo();
10867 if (info->rmode() == RelocInfo::EMBEDDED_OBJECT) {
10868 Object* object = info->target_object();
10869 if (object->IsWeakCell()) object = WeakCell::cast(object)->value();
10870 if (object == map) return_next = true;
10871 } else if (return_next) {
10872 Code* code = Code::GetCodeFromTargetAddress(info->target_address());
10873 DCHECK(code->kind() == Code::HANDLER);
10874 return handle(code);
10877 return MaybeHandle<Code>();
10881 Name* Code::FindFirstName() {
10882 DCHECK(is_inline_cache_stub());
10883 DisallowHeapAllocation no_allocation;
10884 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
10885 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10886 RelocInfo* info = it.rinfo();
10887 Object* object = info->target_object();
10888 if (object->IsName()) return Name::cast(object);
10894 void Code::ClearInlineCaches() {
10895 ClearInlineCaches(NULL);
10899 void Code::ClearInlineCaches(Code::Kind kind) {
10900 ClearInlineCaches(&kind);
10904 void Code::ClearInlineCaches(Code::Kind* kind) {
10905 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
10906 RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) |
10907 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID);
10908 for (RelocIterator it(this, mask); !it.done(); it.next()) {
10909 RelocInfo* info = it.rinfo();
10910 Code* target(Code::GetCodeFromTargetAddress(info->target_address()));
10911 if (target->is_inline_cache_stub()) {
10912 if (kind == NULL || *kind == target->kind()) {
10913 IC::Clear(this->GetIsolate(), info->pc(),
10914 info->host()->constant_pool());
10921 void SharedFunctionInfo::ClearTypeFeedbackInfo() {
10922 feedback_vector()->ClearSlots(this);
10923 feedback_vector()->ClearICSlots(this);
10927 void SharedFunctionInfo::ClearTypeFeedbackInfoAtGCTime() {
10928 feedback_vector()->ClearSlotsAtGCTime(this);
10929 feedback_vector()->ClearICSlotsAtGCTime(this);
10933 BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) {
10934 DisallowHeapAllocation no_gc;
10935 DCHECK(kind() == FUNCTION);
10936 BackEdgeTable back_edges(this, &no_gc);
10937 for (uint32_t i = 0; i < back_edges.length(); i++) {
10938 if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i);
10940 return BailoutId::None();
10944 uint32_t Code::TranslateAstIdToPcOffset(BailoutId ast_id) {
10945 DisallowHeapAllocation no_gc;
10946 DCHECK(kind() == FUNCTION);
10947 BackEdgeTable back_edges(this, &no_gc);
10948 for (uint32_t i = 0; i < back_edges.length(); i++) {
10949 if (back_edges.ast_id(i) == ast_id) return back_edges.pc_offset(i);
10951 UNREACHABLE(); // We expect to find the back edge.
10956 void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) {
10957 PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY);
10961 void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) {
10962 PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge,
10963 NO_MARKING_PARITY);
10967 // NextAge defines the Code::Age state transitions during a GC cycle.
10968 static Code::Age NextAge(Code::Age age) {
10970 case Code::kNotExecutedCodeAge: // Keep, until we've been executed.
10971 case Code::kToBeExecutedOnceCodeAge: // Keep, until we've been executed.
10972 case Code::kLastCodeAge: // Clamp at last Code::Age value.
10974 case Code::kExecutedOnceCodeAge:
10975 // Pre-age code that has only been executed once.
10976 return static_cast<Code::Age>(Code::kPreAgedCodeAge + 1);
10978 return static_cast<Code::Age>(age + 1); // Default case: Increase age.
10983 // IsOldAge defines the collection criteria for a Code object.
10984 static bool IsOldAge(Code::Age age) {
10985 return age >= Code::kIsOldCodeAge || age == Code::kNotExecutedCodeAge;
10989 void Code::MakeYoung(Isolate* isolate) {
10990 byte* sequence = FindCodeAgeSequence();
10991 if (sequence != NULL) MakeCodeAgeSequenceYoung(sequence, isolate);
10995 void Code::MarkToBeExecutedOnce(Isolate* isolate) {
10996 byte* sequence = FindCodeAgeSequence();
10997 if (sequence != NULL) {
10998 PatchPlatformCodeAge(isolate, sequence, kToBeExecutedOnceCodeAge,
10999 NO_MARKING_PARITY);
11004 void Code::MakeOlder(MarkingParity current_parity) {
11005 byte* sequence = FindCodeAgeSequence();
11006 if (sequence != NULL) {
11008 MarkingParity code_parity;
11009 Isolate* isolate = GetIsolate();
11010 GetCodeAgeAndParity(isolate, sequence, &age, &code_parity);
11011 Age next_age = NextAge(age);
11012 if (age != next_age && code_parity != current_parity) {
11013 PatchPlatformCodeAge(isolate, sequence, next_age, current_parity);
11019 bool Code::IsOld() {
11020 return IsOldAge(GetAge());
11024 byte* Code::FindCodeAgeSequence() {
11025 return FLAG_age_code &&
11026 prologue_offset() != Code::kPrologueOffsetNotSet &&
11027 (kind() == OPTIMIZED_FUNCTION ||
11028 (kind() == FUNCTION && !has_debug_break_slots()))
11029 ? instruction_start() + prologue_offset()
11034 Code::Age Code::GetAge() {
11035 byte* sequence = FindCodeAgeSequence();
11036 if (sequence == NULL) {
11037 return kNoAgeCodeAge;
11040 MarkingParity parity;
11041 GetCodeAgeAndParity(GetIsolate(), sequence, &age, &parity);
11046 void Code::GetCodeAgeAndParity(Code* code, Age* age,
11047 MarkingParity* parity) {
11048 Isolate* isolate = code->GetIsolate();
11049 Builtins* builtins = isolate->builtins();
11051 #define HANDLE_CODE_AGE(AGE) \
11052 stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \
11053 if (code == stub) { \
11054 *age = k##AGE##CodeAge; \
11055 *parity = EVEN_MARKING_PARITY; \
11058 stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11059 if (code == stub) { \
11060 *age = k##AGE##CodeAge; \
11061 *parity = ODD_MARKING_PARITY; \
11064 CODE_AGE_LIST(HANDLE_CODE_AGE)
11065 #undef HANDLE_CODE_AGE
11066 stub = *builtins->MarkCodeAsExecutedOnce();
11067 if (code == stub) {
11068 *age = kNotExecutedCodeAge;
11069 *parity = NO_MARKING_PARITY;
11072 stub = *builtins->MarkCodeAsExecutedTwice();
11073 if (code == stub) {
11074 *age = kExecutedOnceCodeAge;
11075 *parity = NO_MARKING_PARITY;
11078 stub = *builtins->MarkCodeAsToBeExecutedOnce();
11079 if (code == stub) {
11080 *age = kToBeExecutedOnceCodeAge;
11081 *parity = NO_MARKING_PARITY;
11088 Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) {
11089 Builtins* builtins = isolate->builtins();
11091 #define HANDLE_CODE_AGE(AGE) \
11092 case k##AGE##CodeAge: { \
11093 Code* stub = parity == EVEN_MARKING_PARITY \
11094 ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \
11095 : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \
11098 CODE_AGE_LIST(HANDLE_CODE_AGE)
11099 #undef HANDLE_CODE_AGE
11100 case kNotExecutedCodeAge: {
11101 DCHECK(parity == NO_MARKING_PARITY);
11102 return *builtins->MarkCodeAsExecutedOnce();
11104 case kExecutedOnceCodeAge: {
11105 DCHECK(parity == NO_MARKING_PARITY);
11106 return *builtins->MarkCodeAsExecutedTwice();
11108 case kToBeExecutedOnceCodeAge: {
11109 DCHECK(parity == NO_MARKING_PARITY);
11110 return *builtins->MarkCodeAsToBeExecutedOnce();
11120 void Code::PrintDeoptLocation(FILE* out, Address pc) {
11121 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(this, pc);
11122 class SourcePosition pos = info.position;
11123 if (info.deopt_reason != Deoptimizer::kNoReason || !pos.IsUnknown()) {
11124 if (FLAG_hydrogen_track_positions) {
11125 PrintF(out, " ;;; deoptimize at %d_%d: %s\n",
11126 pos.inlining_id(), pos.position(),
11127 Deoptimizer::GetDeoptReason(info.deopt_reason));
11129 PrintF(out, " ;;; deoptimize at %d: %s\n", pos.raw(),
11130 Deoptimizer::GetDeoptReason(info.deopt_reason));
11136 bool Code::CanDeoptAt(Address pc) {
11137 DeoptimizationInputData* deopt_data =
11138 DeoptimizationInputData::cast(deoptimization_data());
11139 Address code_start_address = instruction_start();
11140 for (int i = 0; i < deopt_data->DeoptCount(); i++) {
11141 if (deopt_data->Pc(i)->value() == -1) continue;
11142 Address address = code_start_address + deopt_data->Pc(i)->value();
11143 if (address == pc) return true;
11149 // Identify kind of code.
11150 const char* Code::Kind2String(Kind kind) {
11152 #define CASE(name) case name: return #name;
11153 CODE_KIND_LIST(CASE)
11155 case NUMBER_OF_KINDS: break;
11162 Handle<WeakCell> Code::WeakCellFor(Handle<Code> code) {
11163 DCHECK(code->kind() == OPTIMIZED_FUNCTION);
11164 WeakCell* raw_cell = code->CachedWeakCell();
11165 if (raw_cell != NULL) return Handle<WeakCell>(raw_cell);
11166 Handle<WeakCell> cell = code->GetIsolate()->factory()->NewWeakCell(code);
11167 DeoptimizationInputData::cast(code->deoptimization_data())
11168 ->SetWeakCellCache(*cell);
11173 WeakCell* Code::CachedWeakCell() {
11174 DCHECK(kind() == OPTIMIZED_FUNCTION);
11175 Object* weak_cell_cache =
11176 DeoptimizationInputData::cast(deoptimization_data())->WeakCellCache();
11177 if (weak_cell_cache->IsWeakCell()) {
11178 DCHECK(this == WeakCell::cast(weak_cell_cache)->value());
11179 return WeakCell::cast(weak_cell_cache);
11185 #ifdef ENABLE_DISASSEMBLER
11187 void DeoptimizationInputData::DeoptimizationInputDataPrint(
11188 std::ostream& os) { // NOLINT
11189 disasm::NameConverter converter;
11190 int const inlined_function_count = InlinedFunctionCount()->value();
11191 os << "Inlined functions (count = " << inlined_function_count << ")\n";
11192 for (int id = 0; id < inlined_function_count; ++id) {
11193 Object* info = LiteralArray()->get(id);
11194 os << " " << Brief(SharedFunctionInfo::cast(info)) << "\n";
11197 int deopt_count = DeoptCount();
11198 os << "Deoptimization Input Data (deopt points = " << deopt_count << ")\n";
11199 if (0 != deopt_count) {
11200 os << " index ast id argc pc";
11201 if (FLAG_print_code_verbose) os << " commands";
11204 for (int i = 0; i < deopt_count; i++) {
11205 os << std::setw(6) << i << " " << std::setw(6) << AstId(i).ToInt() << " "
11206 << std::setw(6) << ArgumentsStackHeight(i)->value() << " "
11207 << std::setw(6) << Pc(i)->value();
11209 if (!FLAG_print_code_verbose) {
11213 // Print details of the frame translation.
11214 int translation_index = TranslationIndex(i)->value();
11215 TranslationIterator iterator(TranslationByteArray(), translation_index);
11216 Translation::Opcode opcode =
11217 static_cast<Translation::Opcode>(iterator.Next());
11218 DCHECK(Translation::BEGIN == opcode);
11219 int frame_count = iterator.Next();
11220 int jsframe_count = iterator.Next();
11221 os << " " << Translation::StringFor(opcode)
11222 << " {frame count=" << frame_count
11223 << ", js frame count=" << jsframe_count << "}\n";
11225 while (iterator.HasNext() &&
11226 Translation::BEGIN !=
11227 (opcode = static_cast<Translation::Opcode>(iterator.Next()))) {
11228 os << std::setw(31) << " " << Translation::StringFor(opcode) << " ";
11231 case Translation::BEGIN:
11235 case Translation::JS_FRAME: {
11236 int ast_id = iterator.Next();
11237 int shared_info_id = iterator.Next();
11238 unsigned height = iterator.Next();
11239 Object* shared_info = LiteralArray()->get(shared_info_id);
11240 os << "{ast_id=" << ast_id << ", function="
11241 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11242 << ", height=" << height << "}";
11246 case Translation::JS_FRAME_FUNCTION: {
11247 os << "{function}";
11251 case Translation::COMPILED_STUB_FRAME: {
11252 Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next());
11253 os << "{kind=" << stub_kind << "}";
11257 case Translation::ARGUMENTS_ADAPTOR_FRAME:
11258 case Translation::CONSTRUCT_STUB_FRAME: {
11259 int shared_info_id = iterator.Next();
11260 Object* shared_info = LiteralArray()->get(shared_info_id);
11261 unsigned height = iterator.Next();
11263 << Brief(SharedFunctionInfo::cast(shared_info)->DebugName())
11264 << ", height=" << height << "}";
11268 case Translation::GETTER_STUB_FRAME:
11269 case Translation::SETTER_STUB_FRAME: {
11270 int shared_info_id = iterator.Next();
11271 Object* shared_info = LiteralArray()->get(shared_info_id);
11272 os << "{function=" << Brief(SharedFunctionInfo::cast(shared_info)
11273 ->DebugName()) << "}";
11277 case Translation::REGISTER: {
11278 int reg_code = iterator.Next();
11279 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11283 case Translation::INT32_REGISTER: {
11284 int reg_code = iterator.Next();
11285 os << "{input=" << converter.NameOfCPURegister(reg_code) << "}";
11289 case Translation::UINT32_REGISTER: {
11290 int reg_code = iterator.Next();
11291 os << "{input=" << converter.NameOfCPURegister(reg_code)
11296 case Translation::BOOL_REGISTER: {
11297 int reg_code = iterator.Next();
11298 os << "{input=" << converter.NameOfCPURegister(reg_code)
11303 case Translation::DOUBLE_REGISTER: {
11304 int reg_code = iterator.Next();
11305 os << "{input=" << DoubleRegister::AllocationIndexToString(reg_code)
11310 case Translation::STACK_SLOT: {
11311 int input_slot_index = iterator.Next();
11312 os << "{input=" << input_slot_index << "}";
11316 case Translation::INT32_STACK_SLOT: {
11317 int input_slot_index = iterator.Next();
11318 os << "{input=" << input_slot_index << "}";
11322 case Translation::UINT32_STACK_SLOT: {
11323 int input_slot_index = iterator.Next();
11324 os << "{input=" << input_slot_index << " (unsigned)}";
11328 case Translation::BOOL_STACK_SLOT: {
11329 int input_slot_index = iterator.Next();
11330 os << "{input=" << input_slot_index << " (bool)}";
11334 case Translation::DOUBLE_STACK_SLOT: {
11335 int input_slot_index = iterator.Next();
11336 os << "{input=" << input_slot_index << "}";
11340 case Translation::LITERAL: {
11341 unsigned literal_index = iterator.Next();
11342 os << "{literal_id=" << literal_index << "}";
11346 case Translation::DUPLICATED_OBJECT: {
11347 int object_index = iterator.Next();
11348 os << "{object_index=" << object_index << "}";
11352 case Translation::ARGUMENTS_OBJECT:
11353 case Translation::CAPTURED_OBJECT: {
11354 int args_length = iterator.Next();
11355 os << "{length=" << args_length << "}";
11365 void DeoptimizationOutputData::DeoptimizationOutputDataPrint(
11366 std::ostream& os) { // NOLINT
11367 os << "Deoptimization Output Data (deopt points = " << this->DeoptPoints()
11369 if (this->DeoptPoints() == 0) return;
11371 os << "ast id pc state\n";
11372 for (int i = 0; i < this->DeoptPoints(); i++) {
11373 int pc_and_state = this->PcAndState(i)->value();
11374 os << std::setw(6) << this->AstId(i).ToInt() << " " << std::setw(8)
11375 << FullCodeGenerator::PcField::decode(pc_and_state) << " "
11376 << FullCodeGenerator::State2String(
11377 FullCodeGenerator::StateField::decode(pc_and_state)) << "\n";
11382 void HandlerTable::HandlerTableRangePrint(std::ostream& os) {
11383 os << " from to hdlr\n";
11384 for (int i = 0; i < length(); i += kRangeEntrySize) {
11385 int pc_start = Smi::cast(get(i + kRangeStartIndex))->value();
11386 int pc_end = Smi::cast(get(i + kRangeEndIndex))->value();
11387 int handler_field = Smi::cast(get(i + kRangeHandlerIndex))->value();
11388 int handler_offset = HandlerOffsetField::decode(handler_field);
11389 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11390 int depth = Smi::cast(get(i + kRangeDepthIndex))->value();
11391 os << " (" << std::setw(4) << pc_start << "," << std::setw(4) << pc_end
11392 << ") -> " << std::setw(4) << handler_offset
11393 << " (prediction=" << prediction << ", depth=" << depth << ")\n";
11398 void HandlerTable::HandlerTableReturnPrint(std::ostream& os) {
11399 os << " off hdlr (c)\n";
11400 for (int i = 0; i < length(); i += kReturnEntrySize) {
11401 int pc_offset = Smi::cast(get(i + kReturnOffsetIndex))->value();
11402 int handler_field = Smi::cast(get(i + kReturnHandlerIndex))->value();
11403 int handler_offset = HandlerOffsetField::decode(handler_field);
11404 CatchPrediction prediction = HandlerPredictionField::decode(handler_field);
11405 os << " " << std::setw(4) << pc_offset << " -> " << std::setw(4)
11406 << handler_offset << " (prediction=" << prediction << ")\n";
11411 const char* Code::ICState2String(InlineCacheState state) {
11413 case UNINITIALIZED: return "UNINITIALIZED";
11414 case PREMONOMORPHIC: return "PREMONOMORPHIC";
11415 case MONOMORPHIC: return "MONOMORPHIC";
11416 case PROTOTYPE_FAILURE:
11417 return "PROTOTYPE_FAILURE";
11418 case POLYMORPHIC: return "POLYMORPHIC";
11419 case MEGAMORPHIC: return "MEGAMORPHIC";
11420 case GENERIC: return "GENERIC";
11421 case DEBUG_STUB: return "DEBUG_STUB";
11430 const char* Code::StubType2String(StubType type) {
11432 case NORMAL: return "NORMAL";
11433 case FAST: return "FAST";
11435 UNREACHABLE(); // keep the compiler happy
11440 void Code::PrintExtraICState(std::ostream& os, // NOLINT
11441 Kind kind, ExtraICState extra) {
11442 os << "extra_ic_state = ";
11443 if ((kind == STORE_IC || kind == KEYED_STORE_IC) &&
11444 is_strict(static_cast<LanguageMode>(extra))) {
11447 os << extra << "\n";
11452 void Code::Disassemble(const char* name, std::ostream& os) { // NOLINT
11453 os << "kind = " << Kind2String(kind()) << "\n";
11454 if (IsCodeStubOrIC()) {
11455 const char* n = CodeStub::MajorName(CodeStub::GetMajorKey(this));
11456 os << "major_key = " << (n == NULL ? "null" : n) << "\n";
11458 if (is_inline_cache_stub()) {
11459 os << "ic_state = " << ICState2String(ic_state()) << "\n";
11460 PrintExtraICState(os, kind(), extra_ic_state());
11461 if (ic_state() == MONOMORPHIC) {
11462 os << "type = " << StubType2String(type()) << "\n";
11464 if (is_compare_ic_stub()) {
11465 DCHECK(CodeStub::GetMajorKey(this) == CodeStub::CompareIC);
11466 CompareICStub stub(stub_key(), GetIsolate());
11467 os << "compare_state = " << CompareICState::GetStateName(stub.left())
11468 << "*" << CompareICState::GetStateName(stub.right()) << " -> "
11469 << CompareICState::GetStateName(stub.state()) << "\n";
11470 os << "compare_operation = " << Token::Name(stub.op()) << "\n";
11473 if ((name != NULL) && (name[0] != '\0')) {
11474 os << "name = " << name << "\n";
11476 if (kind() == OPTIMIZED_FUNCTION) {
11477 os << "stack_slots = " << stack_slots() << "\n";
11479 os << "compiler = " << (is_turbofanned()
11481 : is_crankshafted() ? "crankshaft"
11482 : kind() == Code::FUNCTION
11484 : "unknown") << "\n";
11486 os << "Instructions (size = " << instruction_size() << ")\n";
11488 Isolate* isolate = GetIsolate();
11489 int size = instruction_size();
11490 int safepoint_offset =
11491 is_crankshafted() ? static_cast<int>(safepoint_table_offset()) : size;
11492 int back_edge_offset = (kind() == Code::FUNCTION)
11493 ? static_cast<int>(back_edge_table_offset())
11495 int constant_pool_offset = FLAG_enable_embedded_constant_pool
11496 ? this->constant_pool_offset()
11499 // Stop before reaching any embedded tables
11500 int code_size = Min(safepoint_offset, back_edge_offset);
11501 code_size = Min(code_size, constant_pool_offset);
11502 byte* begin = instruction_start();
11503 byte* end = begin + code_size;
11504 Disassembler::Decode(isolate, &os, begin, end, this);
11506 if (constant_pool_offset < size) {
11507 int constant_pool_size = size - constant_pool_offset;
11508 DCHECK((constant_pool_size & kPointerAlignmentMask) == 0);
11509 os << "\nConstant Pool (size = " << constant_pool_size << ")\n";
11510 Vector<char> buf = Vector<char>::New(50);
11511 intptr_t* ptr = reinterpret_cast<intptr_t*>(begin + constant_pool_offset);
11512 for (int i = 0; i < constant_pool_size; i += kPointerSize, ptr++) {
11513 SNPrintF(buf, "%4d %08" V8PRIxPTR, i, *ptr);
11514 os << static_cast<const void*>(ptr) << " " << buf.start() << "\n";
11520 if (kind() == FUNCTION) {
11521 DeoptimizationOutputData* data =
11522 DeoptimizationOutputData::cast(this->deoptimization_data());
11523 data->DeoptimizationOutputDataPrint(os);
11524 } else if (kind() == OPTIMIZED_FUNCTION) {
11525 DeoptimizationInputData* data =
11526 DeoptimizationInputData::cast(this->deoptimization_data());
11527 data->DeoptimizationInputDataPrint(os);
11531 if (is_crankshafted()) {
11532 SafepointTable table(this);
11533 os << "Safepoints (size = " << table.size() << ")\n";
11534 for (unsigned i = 0; i < table.length(); i++) {
11535 unsigned pc_offset = table.GetPcOffset(i);
11536 os << static_cast<const void*>(instruction_start() + pc_offset) << " ";
11537 os << std::setw(4) << pc_offset << " ";
11538 table.PrintEntry(i, os);
11539 os << " (sp -> fp) ";
11540 SafepointEntry entry = table.GetEntry(i);
11541 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
11542 os << std::setw(6) << entry.deoptimization_index();
11546 if (entry.argument_count() > 0) {
11547 os << " argc: " << entry.argument_count();
11552 } else if (kind() == FUNCTION) {
11553 unsigned offset = back_edge_table_offset();
11554 // If there is no back edge table, the "table start" will be at or after
11555 // (due to alignment) the end of the instruction stream.
11556 if (static_cast<int>(offset) < instruction_size()) {
11557 DisallowHeapAllocation no_gc;
11558 BackEdgeTable back_edges(this, &no_gc);
11560 os << "Back edges (size = " << back_edges.length() << ")\n";
11561 os << "ast_id pc_offset loop_depth\n";
11563 for (uint32_t i = 0; i < back_edges.length(); i++) {
11564 os << std::setw(6) << back_edges.ast_id(i).ToInt() << " "
11565 << std::setw(9) << back_edges.pc_offset(i) << " " << std::setw(10)
11566 << back_edges.loop_depth(i) << "\n";
11571 #ifdef OBJECT_PRINT
11572 if (!type_feedback_info()->IsUndefined()) {
11573 OFStream os(stdout);
11574 TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(os);
11580 if (handler_table()->length() > 0) {
11581 os << "Handler Table (size = " << handler_table()->Size() << ")\n";
11582 if (kind() == FUNCTION) {
11583 HandlerTable::cast(handler_table())->HandlerTableRangePrint(os);
11584 } else if (kind() == OPTIMIZED_FUNCTION) {
11585 HandlerTable::cast(handler_table())->HandlerTableReturnPrint(os);
11590 os << "RelocInfo (size = " << relocation_size() << ")\n";
11591 for (RelocIterator it(this); !it.done(); it.next()) {
11592 it.rinfo()->Print(GetIsolate(), os);
11596 #endif // ENABLE_DISASSEMBLER
11599 void BytecodeArray::Disassemble(std::ostream& os) {
11600 os << "Parameter count " << parameter_count() << "\n";
11601 os << "Frame size " << frame_size() << "\n";
11602 Vector<char> buf = Vector<char>::New(50);
11604 const uint8_t* first_bytecode_address = GetFirstBytecodeAddress();
11605 int bytecode_size = 0;
11606 for (int i = 0; i < this->length(); i += bytecode_size) {
11607 const uint8_t* bytecode_start = &first_bytecode_address[i];
11608 interpreter::Bytecode bytecode =
11609 interpreter::Bytecodes::FromByte(bytecode_start[0]);
11610 bytecode_size = interpreter::Bytecodes::Size(bytecode);
11612 SNPrintF(buf, "%p", bytecode_start);
11613 os << buf.start() << " : ";
11614 interpreter::Bytecodes::Decode(os, bytecode_start);
11618 os << "Constant pool (size = " << constant_pool()->length() << ")\n";
11619 constant_pool()->Print();
11624 void JSArray::Initialize(Handle<JSArray> array, int capacity, int length) {
11625 DCHECK(capacity >= 0);
11626 array->GetIsolate()->factory()->NewJSArrayStorage(
11627 array, length, capacity, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
11631 // Returns false if the passed-in index is marked non-configurable, which will
11632 // cause the truncation operation to halt, and thus no further old values need
11634 static bool GetOldValue(Isolate* isolate,
11635 Handle<JSObject> object,
11637 List<Handle<Object> >* old_values,
11638 List<uint32_t>* indices) {
11639 LookupIterator it(isolate, object, index, LookupIterator::HIDDEN);
11640 CHECK(JSReceiver::GetPropertyAttributes(&it).IsJust());
11641 DCHECK(it.IsFound());
11642 if (!it.IsConfigurable()) return false;
11643 Handle<Object> value =
11644 it.state() == LookupIterator::ACCESSOR
11645 ? Handle<Object>::cast(isolate->factory()->the_hole_value())
11646 : JSReceiver::GetDataProperty(&it);
11647 old_values->Add(value);
11648 indices->Add(index);
11653 void JSArray::SetLength(Handle<JSArray> array, uint32_t new_length) {
11654 // We should never end in here with a pixel or external array.
11655 DCHECK(array->AllowsSetLength());
11656 if (array->SetLengthWouldNormalize(new_length)) {
11657 JSObject::NormalizeElements(array);
11659 array->GetElementsAccessor()->SetLength(array, new_length);
11663 MaybeHandle<Object> JSArray::ObservableSetLength(Handle<JSArray> array,
11664 uint32_t new_length) {
11665 if (!array->map()->is_observed()) {
11666 SetLength(array, new_length);
11670 Isolate* isolate = array->GetIsolate();
11671 List<uint32_t> indices;
11672 List<Handle<Object> > old_values;
11673 Handle<Object> old_length_handle(array->length(), isolate);
11674 uint32_t old_length = 0;
11675 CHECK(old_length_handle->ToArrayLength(&old_length));
11677 static const PropertyAttributes kNoAttrFilter = NONE;
11678 int num_elements = array->NumberOfOwnElements(kNoAttrFilter);
11679 if (num_elements > 0) {
11680 if (old_length == static_cast<uint32_t>(num_elements)) {
11681 // Simple case for arrays without holes.
11682 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) {
11683 if (!GetOldValue(isolate, array, i, &old_values, &indices)) break;
11686 // For sparse arrays, only iterate over existing elements.
11687 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over
11688 // the to-be-removed indices twice.
11689 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements);
11690 array->GetOwnElementKeys(*keys, kNoAttrFilter);
11691 while (num_elements-- > 0) {
11692 uint32_t index = NumberToUint32(keys->get(num_elements));
11693 if (index < new_length) break;
11694 if (!GetOldValue(isolate, array, index, &old_values, &indices)) break;
11699 SetLength(array, new_length);
11701 CHECK(array->length()->ToArrayLength(&new_length));
11702 if (old_length == new_length) return array;
11704 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
11706 for (int i = 0; i < indices.length(); ++i) {
11707 // For deletions where the property was an accessor, old_values[i]
11708 // will be the hole, which instructs EnqueueChangeRecord to elide
11709 // the "oldValue" property.
11710 RETURN_ON_EXCEPTION(
11712 JSObject::EnqueueChangeRecord(
11713 array, "delete", isolate->factory()->Uint32ToString(indices[i]),
11718 RETURN_ON_EXCEPTION(isolate,
11719 JSObject::EnqueueChangeRecord(
11720 array, "update", isolate->factory()->length_string(),
11721 old_length_handle),
11724 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
11726 uint32_t index = Min(old_length, new_length);
11727 uint32_t add_count = new_length > old_length ? new_length - old_length : 0;
11728 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0;
11729 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
11730 if (delete_count > 0) {
11731 for (int i = indices.length() - 1; i >= 0; i--) {
11732 // Skip deletions where the property was an accessor, leaving holes
11733 // in the array of old values.
11734 if (old_values[i]->IsTheHole()) continue;
11735 JSObject::AddDataElement(deleted, indices[i] - index, old_values[i], NONE)
11739 JSArray::SetLength(deleted, delete_count);
11742 RETURN_ON_EXCEPTION(
11743 isolate, EnqueueSpliceRecord(array, index, deleted, add_count), Object);
11750 void Map::AddDependentCode(Handle<Map> map,
11751 DependentCode::DependencyGroup group,
11752 Handle<Code> code) {
11753 Handle<WeakCell> cell = Code::WeakCellFor(code);
11754 Handle<DependentCode> codes = DependentCode::InsertWeakCode(
11755 Handle<DependentCode>(map->dependent_code()), group, cell);
11756 if (*codes != map->dependent_code()) map->set_dependent_code(*codes);
11760 DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) {
11761 Recompute(entries);
11765 void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) {
11766 start_indexes_[0] = 0;
11767 for (int g = 1; g <= kGroupCount; g++) {
11768 int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1));
11769 start_indexes_[g] = start_indexes_[g - 1] + count;
11774 Handle<DependentCode> DependentCode::InsertCompilationDependencies(
11775 Handle<DependentCode> entries, DependencyGroup group,
11776 Handle<Foreign> info) {
11777 return Insert(entries, group, info);
11781 Handle<DependentCode> DependentCode::InsertWeakCode(
11782 Handle<DependentCode> entries, DependencyGroup group,
11783 Handle<WeakCell> code_cell) {
11784 return Insert(entries, group, code_cell);
11788 Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries,
11789 DependencyGroup group,
11790 Handle<Object> object) {
11791 GroupStartIndexes starts(*entries);
11792 int start = starts.at(group);
11793 int end = starts.at(group + 1);
11794 int number_of_entries = starts.number_of_entries();
11795 // Check for existing entry to avoid duplicates.
11796 for (int i = start; i < end; i++) {
11797 if (entries->object_at(i) == *object) return entries;
11799 if (entries->length() < kCodesStartIndex + number_of_entries + 1) {
11800 entries = EnsureSpace(entries);
11801 // The number of codes can change after Compact and GC.
11802 starts.Recompute(*entries);
11803 start = starts.at(group);
11804 end = starts.at(group + 1);
11807 entries->ExtendGroup(group);
11808 entries->set_object_at(end, *object);
11809 entries->set_number_of_entries(group, end + 1 - start);
11814 Handle<DependentCode> DependentCode::EnsureSpace(
11815 Handle<DependentCode> entries) {
11816 Isolate* isolate = entries->GetIsolate();
11817 if (entries->length() == 0) {
11818 entries = Handle<DependentCode>::cast(
11819 isolate->factory()->NewFixedArray(kCodesStartIndex + 1, TENURED));
11820 for (int g = 0; g < kGroupCount; g++) {
11821 entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0);
11825 if (entries->Compact()) return entries;
11826 GroupStartIndexes starts(*entries);
11828 kCodesStartIndex + DependentCode::Grow(starts.number_of_entries());
11829 int grow_by = capacity - entries->length();
11830 return Handle<DependentCode>::cast(
11831 isolate->factory()->CopyFixedArrayAndGrow(entries, grow_by, TENURED));
11835 bool DependentCode::Compact() {
11836 GroupStartIndexes starts(this);
11838 for (int g = 0; g < kGroupCount; g++) {
11839 int start = starts.at(g);
11840 int end = starts.at(g + 1);
11842 DCHECK(start >= n);
11843 for (int i = start; i < end; i++) {
11844 Object* obj = object_at(i);
11845 if (!obj->IsWeakCell() || !WeakCell::cast(obj)->cleared()) {
11846 if (i != n + count) {
11847 copy(i, n + count);
11852 if (count != end - start) {
11853 set_number_of_entries(static_cast<DependencyGroup>(g), count);
11857 return n < starts.number_of_entries();
11861 void DependentCode::UpdateToFinishedCode(DependencyGroup group, Foreign* info,
11862 WeakCell* code_cell) {
11863 DisallowHeapAllocation no_gc;
11864 GroupStartIndexes starts(this);
11865 int start = starts.at(group);
11866 int end = starts.at(group + 1);
11867 for (int i = start; i < end; i++) {
11868 if (object_at(i) == info) {
11869 set_object_at(i, code_cell);
11875 for (int i = start; i < end; i++) {
11876 DCHECK(object_at(i) != info);
11882 void DependentCode::RemoveCompilationDependencies(
11883 DependentCode::DependencyGroup group, Foreign* info) {
11884 DisallowHeapAllocation no_allocation;
11885 GroupStartIndexes starts(this);
11886 int start = starts.at(group);
11887 int end = starts.at(group + 1);
11888 // Find compilation info wrapper.
11890 for (int i = start; i < end; i++) {
11891 if (object_at(i) == info) {
11896 if (info_pos == -1) return; // Not found.
11897 int gap = info_pos;
11898 // Use the last of each group to fill the gap in the previous group.
11899 for (int i = group; i < kGroupCount; i++) {
11900 int last_of_group = starts.at(i + 1) - 1;
11901 DCHECK(last_of_group >= gap);
11902 if (last_of_group == gap) continue;
11903 copy(last_of_group, gap);
11904 gap = last_of_group;
11906 DCHECK(gap == starts.number_of_entries() - 1);
11907 clear_at(gap); // Clear last gap.
11908 set_number_of_entries(group, end - start - 1);
11911 for (int i = start; i < end - 1; i++) {
11912 DCHECK(object_at(i) != info);
11918 bool DependentCode::Contains(DependencyGroup group, WeakCell* code_cell) {
11919 GroupStartIndexes starts(this);
11920 int start = starts.at(group);
11921 int end = starts.at(group + 1);
11922 for (int i = start; i < end; i++) {
11923 if (object_at(i) == code_cell) return true;
11929 bool DependentCode::MarkCodeForDeoptimization(
11931 DependentCode::DependencyGroup group) {
11932 DisallowHeapAllocation no_allocation_scope;
11933 DependentCode::GroupStartIndexes starts(this);
11934 int start = starts.at(group);
11935 int end = starts.at(group + 1);
11936 int code_entries = starts.number_of_entries();
11937 if (start == end) return false;
11939 // Mark all the code that needs to be deoptimized.
11940 bool marked = false;
11941 bool invalidate_embedded_objects = group == kWeakCodeGroup;
11942 for (int i = start; i < end; i++) {
11943 Object* obj = object_at(i);
11944 if (obj->IsWeakCell()) {
11945 WeakCell* cell = WeakCell::cast(obj);
11946 if (cell->cleared()) continue;
11947 Code* code = Code::cast(cell->value());
11948 if (!code->marked_for_deoptimization()) {
11949 SetMarkedForDeoptimization(code, group);
11950 if (invalidate_embedded_objects) {
11951 code->InvalidateEmbeddedObjects();
11956 DCHECK(obj->IsForeign());
11957 CompilationDependencies* info =
11958 reinterpret_cast<CompilationDependencies*>(
11959 Foreign::cast(obj)->foreign_address());
11963 // Compact the array by moving all subsequent groups to fill in the new holes.
11964 for (int src = end, dst = start; src < code_entries; src++, dst++) {
11967 // Now the holes are at the end of the array, zap them for heap-verifier.
11968 int removed = end - start;
11969 for (int i = code_entries - removed; i < code_entries; i++) {
11972 set_number_of_entries(group, 0);
11977 void DependentCode::DeoptimizeDependentCodeGroup(
11979 DependentCode::DependencyGroup group) {
11980 DCHECK(AllowCodeDependencyChange::IsAllowed());
11981 DisallowHeapAllocation no_allocation_scope;
11982 bool marked = MarkCodeForDeoptimization(isolate, group);
11983 if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate);
11987 void DependentCode::SetMarkedForDeoptimization(Code* code,
11988 DependencyGroup group) {
11989 code->set_marked_for_deoptimization(true);
11990 if (FLAG_trace_deopt &&
11991 (code->deoptimization_data() != code->GetHeap()->empty_fixed_array())) {
11992 DeoptimizationInputData* deopt_data =
11993 DeoptimizationInputData::cast(code->deoptimization_data());
11994 CodeTracer::Scope scope(code->GetHeap()->isolate()->GetCodeTracer());
11995 PrintF(scope.file(), "[marking dependent code 0x%08" V8PRIxPTR
11996 " (opt #%d) for deoptimization, reason: %s]\n",
11997 reinterpret_cast<intptr_t>(code),
11998 deopt_data->OptimizationId()->value(), DependencyGroupName(group));
12003 const char* DependentCode::DependencyGroupName(DependencyGroup group) {
12005 case kWeakCodeGroup:
12006 return "weak-code";
12007 case kTransitionGroup:
12008 return "transition";
12009 case kPrototypeCheckGroup:
12010 return "prototype-check";
12011 case kPropertyCellChangedGroup:
12012 return "property-cell-changed";
12013 case kFieldTypeGroup:
12014 return "field-type";
12015 case kInitialMapChangedGroup:
12016 return "initial-map-changed";
12017 case kAllocationSiteTenuringChangedGroup:
12018 return "allocation-site-tenuring-changed";
12019 case kAllocationSiteTransitionChangedGroup:
12020 return "allocation-site-transition-changed";
12027 Handle<Map> Map::TransitionToPrototype(Handle<Map> map,
12028 Handle<Object> prototype,
12029 PrototypeOptimizationMode mode) {
12030 Handle<Map> new_map = TransitionArray::GetPrototypeTransition(map, prototype);
12031 if (new_map.is_null()) {
12032 new_map = Copy(map, "TransitionToPrototype");
12033 TransitionArray::PutPrototypeTransition(map, prototype, new_map);
12034 Map::SetPrototype(new_map, prototype, mode);
12040 MaybeHandle<Object> JSObject::SetPrototype(Handle<JSObject> object,
12041 Handle<Object> value,
12042 bool from_javascript) {
12044 int size = object->Size();
12047 Isolate* isolate = object->GetIsolate();
12048 // Strong objects may not have their prototype set via __proto__ or
12050 if (from_javascript && object->map()->is_strong()) {
12051 THROW_NEW_ERROR(isolate,
12052 NewTypeError(MessageTemplate::kStrongSetProto, object),
12055 Heap* heap = isolate->heap();
12056 // Silently ignore the change if value is not a JSObject or null.
12057 // SpiderMonkey behaves this way.
12058 if (!value->IsJSReceiver() && !value->IsNull()) return value;
12060 // From 8.6.2 Object Internal Methods
12062 // In addition, if [[Extensible]] is false the value of the [[Class]] and
12063 // [[Prototype]] internal properties of the object may not be modified.
12065 // Implementation specific extensions that modify [[Class]], [[Prototype]]
12066 // or [[Extensible]] must not violate the invariants defined in the preceding
12068 if (!object->map()->is_extensible()) {
12069 THROW_NEW_ERROR(isolate,
12070 NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12074 // Before we can set the prototype we need to be sure
12075 // prototype cycles are prevented.
12076 // It is sufficient to validate that the receiver is not in the new prototype
12078 for (PrototypeIterator iter(isolate, *value,
12079 PrototypeIterator::START_AT_RECEIVER);
12080 !iter.IsAtEnd(); iter.Advance()) {
12081 if (JSReceiver::cast(iter.GetCurrent()) == *object) {
12083 THROW_NEW_ERROR(isolate, NewTypeError(MessageTemplate::kCyclicProto),
12088 bool dictionary_elements_in_chain =
12089 object->map()->DictionaryElementsInPrototypeChainOnly();
12090 Handle<JSObject> real_receiver = object;
12092 if (from_javascript) {
12093 // Find the first object in the chain whose prototype object is not
12094 // hidden and set the new prototype on that object.
12095 PrototypeIterator iter(isolate, real_receiver);
12096 while (!iter.IsAtEnd(PrototypeIterator::END_AT_NON_HIDDEN)) {
12098 Handle<JSObject>::cast(PrototypeIterator::GetCurrent(iter));
12100 if (!real_receiver->map()->is_extensible()) {
12102 isolate, NewTypeError(MessageTemplate::kNonExtensibleProto, object),
12108 // Set the new prototype of the object.
12109 Handle<Map> map(real_receiver->map());
12111 // Nothing to do if prototype is already set.
12112 if (map->prototype() == *value) return value;
12114 isolate->UpdateArrayProtectorOnSetPrototype(real_receiver);
12116 PrototypeOptimizationMode mode =
12117 from_javascript ? REGULAR_PROTOTYPE : FAST_PROTOTYPE;
12118 Handle<Map> new_map = Map::TransitionToPrototype(map, value, mode);
12119 DCHECK(new_map->prototype() == *value);
12120 JSObject::MigrateToMap(real_receiver, new_map);
12122 if (from_javascript && !dictionary_elements_in_chain &&
12123 new_map->DictionaryElementsInPrototypeChainOnly()) {
12124 // If the prototype chain didn't previously have element callbacks, then
12125 // KeyedStoreICs need to be cleared to ensure any that involve this
12127 object->GetHeap()->ClearAllKeyedStoreICs();
12130 heap->ClearInstanceofCache();
12131 DCHECK(size == object->Size());
12136 void JSObject::EnsureCanContainElements(Handle<JSObject> object,
12138 uint32_t first_arg,
12139 uint32_t arg_count,
12140 EnsureElementsMode mode) {
12141 // Elements in |Arguments| are ordered backwards (because they're on the
12142 // stack), but the method that's called here iterates over them in forward
12144 return EnsureCanContainElements(
12145 object, args->arguments() - first_arg - (arg_count - 1), arg_count, mode);
12149 ElementsAccessor* JSObject::GetElementsAccessor() {
12150 return ElementsAccessor::ForKind(GetElementsKind());
12154 void JSObject::ValidateElements(Handle<JSObject> object) {
12155 #ifdef ENABLE_SLOW_DCHECKS
12156 if (FLAG_enable_slow_asserts) {
12157 ElementsAccessor* accessor = object->GetElementsAccessor();
12158 accessor->Validate(object);
12164 static bool ShouldConvertToSlowElements(JSObject* object, uint32_t capacity,
12166 uint32_t* new_capacity) {
12167 STATIC_ASSERT(JSObject::kMaxUncheckedOldFastElementsLength <=
12168 JSObject::kMaxUncheckedFastElementsLength);
12169 if (index < capacity) {
12170 *new_capacity = capacity;
12173 if (index - capacity >= JSObject::kMaxGap) return true;
12174 *new_capacity = JSObject::NewElementsCapacity(index + 1);
12175 DCHECK_LT(index, *new_capacity);
12176 if (*new_capacity <= JSObject::kMaxUncheckedOldFastElementsLength ||
12177 (*new_capacity <= JSObject::kMaxUncheckedFastElementsLength &&
12178 object->GetHeap()->InNewSpace(object))) {
12181 // If the fast-case backing storage takes up roughly three times as
12182 // much space (in machine words) as a dictionary backing storage
12183 // would, the object should have slow elements.
12184 int used_elements = object->GetFastElementsUsage();
12185 int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) *
12186 SeededNumberDictionary::kEntrySize;
12187 return 3 * static_cast<uint32_t>(dictionary_size) <= *new_capacity;
12191 bool JSObject::WouldConvertToSlowElements(uint32_t index) {
12192 if (HasFastElements()) {
12193 Handle<FixedArrayBase> backing_store(FixedArrayBase::cast(elements()));
12194 uint32_t capacity = static_cast<uint32_t>(backing_store->length());
12195 uint32_t new_capacity;
12196 return ShouldConvertToSlowElements(this, capacity, index, &new_capacity);
12202 static ElementsKind BestFittingFastElementsKind(JSObject* object) {
12203 if (object->HasSloppyArgumentsElements()) {
12204 return FAST_SLOPPY_ARGUMENTS_ELEMENTS;
12206 DCHECK(object->HasDictionaryElements());
12207 SeededNumberDictionary* dictionary = object->element_dictionary();
12208 ElementsKind kind = FAST_HOLEY_SMI_ELEMENTS;
12209 for (int i = 0; i < dictionary->Capacity(); i++) {
12210 Object* key = dictionary->KeyAt(i);
12211 if (key->IsNumber()) {
12212 Object* value = dictionary->ValueAt(i);
12213 if (!value->IsNumber()) return FAST_HOLEY_ELEMENTS;
12214 if (!value->IsSmi()) {
12215 if (!FLAG_unbox_double_arrays) return FAST_HOLEY_ELEMENTS;
12216 kind = FAST_HOLEY_DOUBLE_ELEMENTS;
12224 static bool ShouldConvertToFastElements(JSObject* object,
12225 SeededNumberDictionary* dictionary,
12227 uint32_t* new_capacity) {
12228 // If properties with non-standard attributes or accessors were added, we
12229 // cannot go back to fast elements.
12230 if (dictionary->requires_slow_elements()) return false;
12232 // Adding a property with this index will require slow elements.
12233 if (index >= static_cast<uint32_t>(Smi::kMaxValue)) return false;
12235 if (object->IsJSArray()) {
12236 Object* length = JSArray::cast(object)->length();
12237 if (!length->IsSmi()) return false;
12238 *new_capacity = static_cast<uint32_t>(Smi::cast(length)->value());
12240 *new_capacity = dictionary->max_number_key() + 1;
12242 *new_capacity = Max(index + 1, *new_capacity);
12244 uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) *
12245 SeededNumberDictionary::kEntrySize;
12246 return 2 * dictionary_size >= *new_capacity;
12251 MaybeHandle<Object> JSObject::AddDataElement(Handle<JSObject> object,
12253 Handle<Object> value,
12254 PropertyAttributes attributes) {
12255 DCHECK(object->map()->is_extensible());
12257 Isolate* isolate = object->GetIsolate();
12259 uint32_t old_length = 0;
12260 uint32_t new_capacity = 0;
12262 Handle<Object> old_length_handle;
12263 if (object->IsJSArray()) {
12264 CHECK(JSArray::cast(*object)->length()->ToArrayLength(&old_length));
12265 if (object->map()->is_observed()) {
12266 old_length_handle = handle(JSArray::cast(*object)->length(), isolate);
12270 ElementsKind kind = object->GetElementsKind();
12271 FixedArrayBase* elements = object->elements();
12272 ElementsKind dictionary_kind = DICTIONARY_ELEMENTS;
12273 if (IsSloppyArgumentsElements(kind)) {
12274 elements = FixedArrayBase::cast(FixedArray::cast(elements)->get(1));
12275 dictionary_kind = SLOW_SLOPPY_ARGUMENTS_ELEMENTS;
12278 if (attributes != NONE) {
12279 kind = dictionary_kind;
12280 } else if (elements->IsSeededNumberDictionary()) {
12281 kind = ShouldConvertToFastElements(*object,
12282 SeededNumberDictionary::cast(elements),
12283 index, &new_capacity)
12284 ? BestFittingFastElementsKind(*object)
12285 : dictionary_kind; // Overwrite in case of arguments.
12286 } else if (ShouldConvertToSlowElements(
12287 *object, static_cast<uint32_t>(elements->length()), index,
12289 kind = dictionary_kind;
12292 ElementsKind to = value->OptimalElementsKind();
12293 if (IsHoleyElementsKind(kind) || !object->IsJSArray() || index > old_length) {
12294 to = GetHoleyElementsKind(to);
12295 kind = GetHoleyElementsKind(kind);
12297 to = IsMoreGeneralElementsKindTransition(kind, to) ? to : kind;
12298 ElementsAccessor* accessor = ElementsAccessor::ForKind(to);
12299 accessor->Add(object, index, value, attributes, new_capacity);
12301 uint32_t new_length = old_length;
12302 Handle<Object> new_length_handle;
12303 if (object->IsJSArray() && index >= old_length) {
12304 new_length = index + 1;
12305 new_length_handle = isolate->factory()->NewNumberFromUint(new_length);
12306 JSArray::cast(*object)->set_length(*new_length_handle);
12309 if (!old_length_handle.is_null() && new_length != old_length) {
12310 // |old_length_handle| is kept null above unless the object is observed.
12311 DCHECK(object->map()->is_observed());
12312 Handle<JSArray> array = Handle<JSArray>::cast(object);
12313 Handle<String> name = isolate->factory()->Uint32ToString(index);
12315 RETURN_ON_EXCEPTION(isolate, BeginPerformSplice(array), Object);
12316 RETURN_ON_EXCEPTION(
12317 isolate, EnqueueChangeRecord(array, "add", name,
12318 isolate->factory()->the_hole_value()),
12320 RETURN_ON_EXCEPTION(isolate,
12321 EnqueueChangeRecord(array, "update",
12322 isolate->factory()->length_string(),
12323 old_length_handle),
12325 RETURN_ON_EXCEPTION(isolate, EndPerformSplice(array), Object);
12326 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
12327 RETURN_ON_EXCEPTION(isolate, EnqueueSpliceRecord(array, old_length, deleted,
12328 new_length - old_length),
12330 } else if (object->map()->is_observed()) {
12331 Handle<String> name = isolate->factory()->Uint32ToString(index);
12332 RETURN_ON_EXCEPTION(
12333 isolate, EnqueueChangeRecord(object, "add", name,
12334 isolate->factory()->the_hole_value()),
12342 bool JSArray::SetLengthWouldNormalize(uint32_t new_length) {
12343 if (!HasFastElements()) return false;
12344 uint32_t capacity = static_cast<uint32_t>(elements()->length());
12345 uint32_t new_capacity;
12346 return JSArray::SetLengthWouldNormalize(GetHeap(), new_length) &&
12347 ShouldConvertToSlowElements(this, capacity, new_length - 1,
12352 const double AllocationSite::kPretenureRatio = 0.85;
12355 void AllocationSite::ResetPretenureDecision() {
12356 set_pretenure_decision(kUndecided);
12357 set_memento_found_count(0);
12358 set_memento_create_count(0);
12362 PretenureFlag AllocationSite::GetPretenureMode() {
12363 PretenureDecision mode = pretenure_decision();
12364 // Zombie objects "decide" to be untenured.
12365 return mode == kTenure ? TENURED : NOT_TENURED;
12369 bool AllocationSite::IsNestedSite() {
12370 DCHECK(FLAG_trace_track_allocation_sites);
12371 Object* current = GetHeap()->allocation_sites_list();
12372 while (current->IsAllocationSite()) {
12373 AllocationSite* current_site = AllocationSite::cast(current);
12374 if (current_site->nested_site() == this) {
12377 current = current_site->weak_next();
12383 void AllocationSite::DigestTransitionFeedback(Handle<AllocationSite> site,
12384 ElementsKind to_kind) {
12385 Isolate* isolate = site->GetIsolate();
12387 if (site->SitePointsToLiteral() && site->transition_info()->IsJSArray()) {
12388 Handle<JSArray> transition_info =
12389 handle(JSArray::cast(site->transition_info()));
12390 ElementsKind kind = transition_info->GetElementsKind();
12391 // if kind is holey ensure that to_kind is as well.
12392 if (IsHoleyElementsKind(kind)) {
12393 to_kind = GetHoleyElementsKind(to_kind);
12395 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12396 // If the array is huge, it's not likely to be defined in a local
12397 // function, so we shouldn't make new instances of it very often.
12398 uint32_t length = 0;
12399 CHECK(transition_info->length()->ToArrayLength(&length));
12400 if (length <= kMaximumArrayBytesToPretransition) {
12401 if (FLAG_trace_track_allocation_sites) {
12402 bool is_nested = site->IsNestedSite();
12404 "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n",
12405 reinterpret_cast<void*>(*site),
12406 is_nested ? "(nested)" : "",
12407 ElementsKindToString(kind),
12408 ElementsKindToString(to_kind));
12410 JSObject::TransitionElementsKind(transition_info, to_kind);
12411 site->dependent_code()->DeoptimizeDependentCodeGroup(
12412 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12416 ElementsKind kind = site->GetElementsKind();
12417 // if kind is holey ensure that to_kind is as well.
12418 if (IsHoleyElementsKind(kind)) {
12419 to_kind = GetHoleyElementsKind(to_kind);
12421 if (IsMoreGeneralElementsKindTransition(kind, to_kind)) {
12422 if (FLAG_trace_track_allocation_sites) {
12423 PrintF("AllocationSite: JSArray %p site updated %s->%s\n",
12424 reinterpret_cast<void*>(*site),
12425 ElementsKindToString(kind),
12426 ElementsKindToString(to_kind));
12428 site->SetElementsKind(to_kind);
12429 site->dependent_code()->DeoptimizeDependentCodeGroup(
12430 isolate, DependentCode::kAllocationSiteTransitionChangedGroup);
12436 const char* AllocationSite::PretenureDecisionName(PretenureDecision decision) {
12437 switch (decision) {
12438 case kUndecided: return "undecided";
12439 case kDontTenure: return "don't tenure";
12440 case kMaybeTenure: return "maybe tenure";
12441 case kTenure: return "tenure";
12442 case kZombie: return "zombie";
12443 default: UNREACHABLE();
12449 void JSObject::UpdateAllocationSite(Handle<JSObject> object,
12450 ElementsKind to_kind) {
12451 if (!object->IsJSArray()) return;
12453 Heap* heap = object->GetHeap();
12454 if (!heap->InNewSpace(*object)) return;
12456 Handle<AllocationSite> site;
12458 DisallowHeapAllocation no_allocation;
12460 AllocationMemento* memento = heap->FindAllocationMemento(*object);
12461 if (memento == NULL) return;
12463 // Walk through to the Allocation Site
12464 site = handle(memento->GetAllocationSite());
12466 AllocationSite::DigestTransitionFeedback(site, to_kind);
12470 void JSObject::TransitionElementsKind(Handle<JSObject> object,
12471 ElementsKind to_kind) {
12472 ElementsKind from_kind = object->GetElementsKind();
12474 if (IsFastHoleyElementsKind(from_kind)) {
12475 to_kind = GetHoleyElementsKind(to_kind);
12478 if (from_kind == to_kind) return;
12480 // This method should never be called for any other case.
12481 DCHECK(IsFastElementsKind(from_kind));
12482 DCHECK(IsFastElementsKind(to_kind));
12483 DCHECK_NE(TERMINAL_FAST_ELEMENTS_KIND, from_kind);
12485 UpdateAllocationSite(object, to_kind);
12486 if (object->elements() == object->GetHeap()->empty_fixed_array() ||
12487 IsFastDoubleElementsKind(from_kind) ==
12488 IsFastDoubleElementsKind(to_kind)) {
12489 // No change is needed to the elements() buffer, the transition
12490 // only requires a map change.
12491 Handle<Map> new_map = GetElementsTransitionMap(object, to_kind);
12492 MigrateToMap(object, new_map);
12493 if (FLAG_trace_elements_transitions) {
12494 Handle<FixedArrayBase> elms(object->elements());
12495 PrintElementsTransition(stdout, object, from_kind, elms, to_kind, elms);
12498 DCHECK((IsFastSmiElementsKind(from_kind) &&
12499 IsFastDoubleElementsKind(to_kind)) ||
12500 (IsFastDoubleElementsKind(from_kind) &&
12501 IsFastObjectElementsKind(to_kind)));
12502 uint32_t c = static_cast<uint32_t>(object->elements()->length());
12503 ElementsAccessor::ForKind(to_kind)->GrowCapacityAndConvert(object, c);
12509 bool Map::IsValidElementsTransition(ElementsKind from_kind,
12510 ElementsKind to_kind) {
12511 // Transitions can't go backwards.
12512 if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) {
12516 // Transitions from HOLEY -> PACKED are not allowed.
12517 return !IsFastHoleyElementsKind(from_kind) ||
12518 IsFastHoleyElementsKind(to_kind);
12522 bool JSArray::HasReadOnlyLength(Handle<JSArray> array) {
12523 LookupIterator it(array, array->GetIsolate()->factory()->length_string(),
12524 LookupIterator::OWN_SKIP_INTERCEPTOR);
12525 CHECK_NE(LookupIterator::ACCESS_CHECK, it.state());
12526 CHECK(it.IsFound());
12527 CHECK_EQ(LookupIterator::ACCESSOR, it.state());
12528 return it.IsReadOnly();
12532 bool JSArray::WouldChangeReadOnlyLength(Handle<JSArray> array,
12534 uint32_t length = 0;
12535 CHECK(array->length()->ToArrayLength(&length));
12536 if (length <= index) return HasReadOnlyLength(array);
12541 MaybeHandle<Object> JSArray::ReadOnlyLengthError(Handle<JSArray> array) {
12542 Isolate* isolate = array->GetIsolate();
12543 Handle<Name> length = isolate->factory()->length_string();
12546 NewTypeError(MessageTemplate::kStrictReadOnlyProperty, length, array),
12551 template <typename BackingStore>
12552 static int FastHoleyElementsUsage(JSObject* object, BackingStore* store) {
12553 int limit = object->IsJSArray()
12554 ? Smi::cast(JSArray::cast(object)->length())->value()
12557 for (int i = 0; i < limit; ++i) {
12558 if (!store->is_the_hole(i)) ++used;
12564 int JSObject::GetFastElementsUsage() {
12565 FixedArrayBase* store = elements();
12566 switch (GetElementsKind()) {
12567 case FAST_SMI_ELEMENTS:
12568 case FAST_DOUBLE_ELEMENTS:
12569 case FAST_ELEMENTS:
12570 // Only JSArray have packed elements.
12571 return Smi::cast(JSArray::cast(this)->length())->value();
12572 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
12573 store = FixedArray::cast(FixedArray::cast(store)->get(1));
12575 case FAST_HOLEY_SMI_ELEMENTS:
12576 case FAST_HOLEY_ELEMENTS:
12577 return FastHoleyElementsUsage(this, FixedArray::cast(store));
12578 case FAST_HOLEY_DOUBLE_ELEMENTS:
12579 if (elements()->length() == 0) return 0;
12580 return FastHoleyElementsUsage(this, FixedDoubleArray::cast(store));
12582 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS:
12583 case DICTIONARY_ELEMENTS:
12584 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
12585 case TYPE##_ELEMENTS: \
12587 TYPED_ARRAYS(TYPED_ARRAY_CASE)
12588 #undef TYPED_ARRAY_CASE
12595 // Certain compilers request function template instantiation when they
12596 // see the definition of the other template functions in the
12597 // class. This requires us to have the template functions put
12598 // together, so even though this function belongs in objects-debug.cc,
12599 // we keep it here instead to satisfy certain compilers.
12600 #ifdef OBJECT_PRINT
12601 template <typename Derived, typename Shape, typename Key>
12602 void Dictionary<Derived, Shape, Key>::Print(std::ostream& os) { // NOLINT
12603 int capacity = this->Capacity();
12604 for (int i = 0; i < capacity; i++) {
12605 Object* k = this->KeyAt(i);
12606 if (this->IsKey(k)) {
12608 if (k->IsString()) {
12609 String::cast(k)->StringPrint(os);
12613 os << ": " << Brief(this->ValueAt(i)) << " " << this->DetailsAt(i)
12621 template<typename Derived, typename Shape, typename Key>
12622 void Dictionary<Derived, Shape, Key>::CopyValuesTo(FixedArray* elements) {
12624 int capacity = this->Capacity();
12625 DisallowHeapAllocation no_gc;
12626 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
12627 for (int i = 0; i < capacity; i++) {
12628 Object* k = this->KeyAt(i);
12629 if (this->IsKey(k)) {
12630 elements->set(pos++, this->ValueAt(i), mode);
12633 DCHECK(pos == elements->length());
12637 InterceptorInfo* JSObject::GetNamedInterceptor() {
12638 DCHECK(map()->has_named_interceptor());
12639 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12640 DCHECK(constructor->shared()->IsApiFunction());
12642 constructor->shared()->get_api_func_data()->named_property_handler();
12643 return InterceptorInfo::cast(result);
12647 InterceptorInfo* JSObject::GetIndexedInterceptor() {
12648 DCHECK(map()->has_indexed_interceptor());
12649 JSFunction* constructor = JSFunction::cast(map()->GetConstructor());
12650 DCHECK(constructor->shared()->IsApiFunction());
12652 constructor->shared()->get_api_func_data()->indexed_property_handler();
12653 return InterceptorInfo::cast(result);
12657 MaybeHandle<Object> JSObject::GetPropertyWithInterceptor(LookupIterator* it,
12660 Isolate* isolate = it->isolate();
12661 // Make sure that the top context does not change when doing callbacks or
12662 // interceptor calls.
12663 AssertNoContextChange ncc(isolate);
12665 DCHECK_EQ(LookupIterator::INTERCEPTOR, it->state());
12666 Handle<InterceptorInfo> interceptor = it->GetInterceptor();
12667 if (interceptor->getter()->IsUndefined()) {
12668 return isolate->factory()->undefined_value();
12671 Handle<JSObject> holder = it->GetHolder<JSObject>();
12672 v8::Local<v8::Value> result;
12673 PropertyCallbackArguments args(isolate, interceptor->data(),
12674 *it->GetReceiver(), *holder);
12676 if (it->IsElement()) {
12677 uint32_t index = it->index();
12678 v8::IndexedPropertyGetterCallback getter =
12679 v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter());
12681 ApiIndexedPropertyAccess("interceptor-indexed-get", *holder, index));
12682 result = args.Call(getter, index);
12684 Handle<Name> name = it->name();
12686 if (name->IsSymbol() && !interceptor->can_intercept_symbols()) {
12687 return isolate->factory()->undefined_value();
12690 v8::GenericNamedPropertyGetterCallback getter =
12691 v8::ToCData<v8::GenericNamedPropertyGetterCallback>(
12692 interceptor->getter());
12694 ApiNamedPropertyAccess("interceptor-named-get", *holder, *name));
12695 result = args.Call(getter, v8::Utils::ToLocal(name));
12698 RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
12699 if (result.IsEmpty()) return isolate->factory()->undefined_value();
12700 Handle<Object> result_internal = v8::Utils::OpenHandle(*result);
12701 result_internal->VerifyApiCallResultType();
12703 // Rebox handle before return
12704 return handle(*result_internal, isolate);
12708 // Compute the property keys from the interceptor.
12709 MaybeHandle<JSObject> JSObject::GetKeysForNamedInterceptor(
12710 Handle<JSObject> object, Handle<JSReceiver> receiver) {
12711 Isolate* isolate = receiver->GetIsolate();
12712 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
12713 PropertyCallbackArguments
12714 args(isolate, interceptor->data(), *receiver, *object);
12715 v8::Local<v8::Object> result;
12716 if (!interceptor->enumerator()->IsUndefined()) {
12717 v8::GenericNamedPropertyEnumeratorCallback enum_fun =
12718 v8::ToCData<v8::GenericNamedPropertyEnumeratorCallback>(
12719 interceptor->enumerator());
12720 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
12721 result = args.Call(enum_fun);
12723 if (result.IsEmpty()) return MaybeHandle<JSObject>();
12724 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
12725 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
12726 // Rebox before returning.
12727 return handle(*v8::Utils::OpenHandle(*result), isolate);
12731 // Compute the element keys from the interceptor.
12732 MaybeHandle<JSObject> JSObject::GetKeysForIndexedInterceptor(
12733 Handle<JSObject> object, Handle<JSReceiver> receiver) {
12734 Isolate* isolate = receiver->GetIsolate();
12735 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
12736 PropertyCallbackArguments
12737 args(isolate, interceptor->data(), *receiver, *object);
12738 v8::Local<v8::Object> result;
12739 if (!interceptor->enumerator()->IsUndefined()) {
12740 v8::IndexedPropertyEnumeratorCallback enum_fun =
12741 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>(
12742 interceptor->enumerator());
12743 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
12744 result = args.Call(enum_fun);
12746 if (result.IsEmpty()) return MaybeHandle<JSObject>();
12747 DCHECK(v8::Utils::OpenHandle(*result)->IsJSArray() ||
12748 v8::Utils::OpenHandle(*result)->HasSloppyArgumentsElements());
12749 // Rebox before returning.
12750 return handle(*v8::Utils::OpenHandle(*result), isolate);
12754 Maybe<bool> JSObject::HasRealNamedProperty(Handle<JSObject> object,
12755 Handle<Name> name) {
12756 LookupIterator it = LookupIterator::PropertyOrElement(
12757 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
12758 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12759 if (!maybe_result.IsJust()) return Nothing<bool>();
12760 return Just(it.IsFound());
12764 Maybe<bool> JSObject::HasRealElementProperty(Handle<JSObject> object,
12766 Isolate* isolate = object->GetIsolate();
12767 LookupIterator it(isolate, object, index,
12768 LookupIterator::OWN_SKIP_INTERCEPTOR);
12769 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12770 if (!maybe_result.IsJust()) return Nothing<bool>();
12771 return Just(it.IsFound());
12775 Maybe<bool> JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object,
12776 Handle<Name> name) {
12777 LookupIterator it = LookupIterator::PropertyOrElement(
12778 name->GetIsolate(), object, name, LookupIterator::OWN_SKIP_INTERCEPTOR);
12779 Maybe<PropertyAttributes> maybe_result = GetPropertyAttributes(&it);
12780 return maybe_result.IsJust() ? Just(it.state() == LookupIterator::ACCESSOR)
12785 int JSObject::NumberOfOwnProperties(PropertyAttributes filter) {
12786 if (HasFastProperties()) {
12787 Map* map = this->map();
12788 if (filter == NONE) return map->NumberOfOwnDescriptors();
12789 if (filter & DONT_ENUM) {
12790 int result = map->EnumLength();
12791 if (result != kInvalidEnumCacheSentinel) return result;
12793 return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter);
12794 } else if (IsGlobalObject()) {
12795 return global_dictionary()->NumberOfElementsFilterAttributes(filter);
12797 return property_dictionary()->NumberOfElementsFilterAttributes(filter);
12802 void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) {
12803 Object* temp = get(i);
12806 if (this != numbers) {
12807 temp = numbers->get(i);
12808 numbers->set(i, Smi::cast(numbers->get(j)));
12809 numbers->set(j, Smi::cast(temp));
12814 static void InsertionSortPairs(FixedArray* content,
12815 FixedArray* numbers,
12817 for (int i = 1; i < len; i++) {
12820 (NumberToUint32(numbers->get(j - 1)) >
12821 NumberToUint32(numbers->get(j)))) {
12822 content->SwapPairs(numbers, j - 1, j);
12829 void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) {
12830 // In-place heap sort.
12831 DCHECK(content->length() == numbers->length());
12833 // Bottom-up max-heap construction.
12834 for (int i = 1; i < len; ++i) {
12835 int child_index = i;
12836 while (child_index > 0) {
12837 int parent_index = ((child_index + 1) >> 1) - 1;
12838 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
12839 uint32_t child_value = NumberToUint32(numbers->get(child_index));
12840 if (parent_value < child_value) {
12841 content->SwapPairs(numbers, parent_index, child_index);
12845 child_index = parent_index;
12849 // Extract elements and create sorted array.
12850 for (int i = len - 1; i > 0; --i) {
12851 // Put max element at the back of the array.
12852 content->SwapPairs(numbers, 0, i);
12853 // Sift down the new top element.
12854 int parent_index = 0;
12856 int child_index = ((parent_index + 1) << 1) - 1;
12857 if (child_index >= i) break;
12858 uint32_t child1_value = NumberToUint32(numbers->get(child_index));
12859 uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1));
12860 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
12861 if (child_index + 1 >= i || child1_value > child2_value) {
12862 if (parent_value > child1_value) break;
12863 content->SwapPairs(numbers, parent_index, child_index);
12864 parent_index = child_index;
12866 if (parent_value > child2_value) break;
12867 content->SwapPairs(numbers, parent_index, child_index + 1);
12868 parent_index = child_index + 1;
12875 // Sort this array and the numbers as pairs wrt. the (distinct) numbers.
12876 void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) {
12877 DCHECK(this->length() == numbers->length());
12878 // For small arrays, simply use insertion sort.
12880 InsertionSortPairs(this, numbers, len);
12883 // Check the range of indices.
12884 uint32_t min_index = NumberToUint32(numbers->get(0));
12885 uint32_t max_index = min_index;
12887 for (i = 1; i < len; i++) {
12888 if (NumberToUint32(numbers->get(i)) < min_index) {
12889 min_index = NumberToUint32(numbers->get(i));
12890 } else if (NumberToUint32(numbers->get(i)) > max_index) {
12891 max_index = NumberToUint32(numbers->get(i));
12894 if (max_index - min_index + 1 == len) {
12895 // Indices form a contiguous range, unless there are duplicates.
12896 // Do an in-place linear time sort assuming distinct numbers, but
12897 // avoid hanging in case they are not.
12898 for (i = 0; i < len; i++) {
12901 // While the current element at i is not at its correct position p,
12902 // swap the elements at these two positions.
12903 while ((p = NumberToUint32(numbers->get(i)) - min_index) != i &&
12905 SwapPairs(numbers, i, p);
12909 HeapSortPairs(this, numbers, len);
12915 // Fill in the names of own properties into the supplied storage. The main
12916 // purpose of this function is to provide reflection information for the object
12918 int JSObject::GetOwnPropertyNames(FixedArray* storage, int index,
12919 PropertyAttributes filter) {
12920 DCHECK(storage->length() >= (NumberOfOwnProperties(filter) - index));
12921 if (HasFastProperties()) {
12922 int start_index = index;
12923 int real_size = map()->NumberOfOwnDescriptors();
12924 DescriptorArray* descs = map()->instance_descriptors();
12925 for (int i = 0; i < real_size; i++) {
12926 if ((descs->GetDetails(i).attributes() & filter) == 0 &&
12927 !FilterKey(descs->GetKey(i), filter)) {
12928 storage->set(index++, descs->GetKey(i));
12931 return index - start_index;
12932 } else if (IsGlobalObject()) {
12933 return global_dictionary()->CopyKeysTo(storage, index, filter,
12934 GlobalDictionary::UNSORTED);
12936 return property_dictionary()->CopyKeysTo(storage, index, filter,
12937 NameDictionary::UNSORTED);
12942 int JSObject::NumberOfOwnElements(PropertyAttributes filter) {
12943 return GetOwnElementKeys(NULL, filter);
12947 int JSObject::NumberOfEnumElements() {
12948 // Fast case for objects with no elements.
12949 if (!IsJSValue() && HasFastObjectElements()) {
12950 uint32_t length = IsJSArray() ?
12951 static_cast<uint32_t>(
12952 Smi::cast(JSArray::cast(this)->length())->value()) :
12953 static_cast<uint32_t>(FixedArray::cast(elements())->length());
12954 if (length == 0) return 0;
12956 // Compute the number of enumerable elements.
12957 return NumberOfOwnElements(static_cast<PropertyAttributes>(DONT_ENUM));
12961 int JSObject::GetOwnElementKeys(FixedArray* storage,
12962 PropertyAttributes filter) {
12965 // If this is a String wrapper, add the string indices first,
12966 // as they're guaranteed to preced the elements in numerical order
12967 // and ascending order is required by ECMA-262, 6th, 9.1.12.
12969 Object* val = JSValue::cast(this)->value();
12970 if (val->IsString()) {
12971 String* str = String::cast(val);
12973 for (int i = 0; i < str->length(); i++) {
12974 storage->set(counter + i, Smi::FromInt(i));
12977 counter += str->length();
12981 switch (GetElementsKind()) {
12982 case FAST_SMI_ELEMENTS:
12983 case FAST_ELEMENTS:
12984 case FAST_HOLEY_SMI_ELEMENTS:
12985 case FAST_HOLEY_ELEMENTS: {
12986 int length = IsJSArray() ?
12987 Smi::cast(JSArray::cast(this)->length())->value() :
12988 FixedArray::cast(elements())->length();
12989 for (int i = 0; i < length; i++) {
12990 if (!FixedArray::cast(elements())->get(i)->IsTheHole()) {
12991 if (storage != NULL) {
12992 storage->set(counter, Smi::FromInt(i));
12997 DCHECK(!storage || storage->length() >= counter);
13000 case FAST_DOUBLE_ELEMENTS:
13001 case FAST_HOLEY_DOUBLE_ELEMENTS: {
13002 int length = IsJSArray() ?
13003 Smi::cast(JSArray::cast(this)->length())->value() :
13004 FixedArrayBase::cast(elements())->length();
13005 for (int i = 0; i < length; i++) {
13006 if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) {
13007 if (storage != NULL) {
13008 storage->set(counter, Smi::FromInt(i));
13013 DCHECK(!storage || storage->length() >= counter);
13017 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \
13018 case TYPE##_ELEMENTS: \
13020 TYPED_ARRAYS(TYPED_ARRAY_CASE)
13021 #undef TYPED_ARRAY_CASE
13023 int length = FixedArrayBase::cast(elements())->length();
13024 while (counter < length) {
13025 if (storage != NULL) {
13026 storage->set(counter, Smi::FromInt(counter));
13030 DCHECK(!storage || storage->length() >= counter);
13034 case DICTIONARY_ELEMENTS: {
13035 if (storage != NULL) {
13036 element_dictionary()->CopyKeysTo(storage, counter, filter,
13037 SeededNumberDictionary::SORTED);
13039 counter += element_dictionary()->NumberOfElementsFilterAttributes(filter);
13042 case FAST_SLOPPY_ARGUMENTS_ELEMENTS:
13043 case SLOW_SLOPPY_ARGUMENTS_ELEMENTS: {
13044 FixedArray* parameter_map = FixedArray::cast(elements());
13045 int mapped_length = parameter_map->length() - 2;
13046 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
13047 if (arguments->IsDictionary()) {
13048 // Copy the keys from arguments first, because Dictionary::CopyKeysTo
13049 // will insert in storage starting at index 0.
13050 SeededNumberDictionary* dictionary =
13051 SeededNumberDictionary::cast(arguments);
13052 if (storage != NULL) {
13053 dictionary->CopyKeysTo(storage, counter, filter,
13054 SeededNumberDictionary::UNSORTED);
13056 counter += dictionary->NumberOfElementsFilterAttributes(filter);
13057 for (int i = 0; i < mapped_length; ++i) {
13058 if (!parameter_map->get(i + 2)->IsTheHole()) {
13059 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13063 if (storage != NULL) storage->SortPairs(storage, counter);
13066 int backing_length = arguments->length();
13068 for (; i < mapped_length; ++i) {
13069 if (!parameter_map->get(i + 2)->IsTheHole()) {
13070 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13072 } else if (i < backing_length && !arguments->get(i)->IsTheHole()) {
13073 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13077 for (; i < backing_length; ++i) {
13078 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
13086 DCHECK(!storage || storage->length() == counter);
13091 int JSObject::GetEnumElementKeys(FixedArray* storage) {
13092 return GetOwnElementKeys(storage, static_cast<PropertyAttributes>(DONT_ENUM));
13096 const char* Symbol::PrivateSymbolToName() const {
13097 Heap* heap = GetIsolate()->heap();
13098 #define SYMBOL_CHECK_AND_PRINT(name) \
13099 if (this == heap->name()) return #name;
13100 PRIVATE_SYMBOL_LIST(SYMBOL_CHECK_AND_PRINT)
13101 #undef SYMBOL_CHECK_AND_PRINT
13106 void Symbol::SymbolShortPrint(std::ostream& os) {
13107 os << "<Symbol: " << Hash();
13108 if (!name()->IsUndefined()) {
13110 HeapStringAllocator allocator;
13111 StringStream accumulator(&allocator);
13112 String::cast(name())->StringShortPrint(&accumulator);
13113 os << accumulator.ToCString().get();
13115 os << " (" << PrivateSymbolToName() << ")";
13121 // StringSharedKeys are used as keys in the eval cache.
13122 class StringSharedKey : public HashTableKey {
13124 StringSharedKey(Handle<String> source, Handle<SharedFunctionInfo> shared,
13125 LanguageMode language_mode, int scope_position)
13128 language_mode_(language_mode),
13129 scope_position_(scope_position) {}
13131 bool IsMatch(Object* other) override {
13132 DisallowHeapAllocation no_allocation;
13133 if (!other->IsFixedArray()) {
13134 if (!other->IsNumber()) return false;
13135 uint32_t other_hash = static_cast<uint32_t>(other->Number());
13136 return Hash() == other_hash;
13138 FixedArray* other_array = FixedArray::cast(other);
13139 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13140 if (shared != *shared_) return false;
13141 int language_unchecked = Smi::cast(other_array->get(2))->value();
13142 DCHECK(is_valid_language_mode(language_unchecked));
13143 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13144 if (language_mode != language_mode_) return false;
13145 int scope_position = Smi::cast(other_array->get(3))->value();
13146 if (scope_position != scope_position_) return false;
13147 String* source = String::cast(other_array->get(1));
13148 return source->Equals(*source_);
13151 static uint32_t StringSharedHashHelper(String* source,
13152 SharedFunctionInfo* shared,
13153 LanguageMode language_mode,
13154 int scope_position) {
13155 uint32_t hash = source->Hash();
13156 if (shared->HasSourceCode()) {
13157 // Instead of using the SharedFunctionInfo pointer in the hash
13158 // code computation, we use a combination of the hash of the
13159 // script source code and the start position of the calling scope.
13160 // We do this to ensure that the cache entries can survive garbage
13162 Script* script(Script::cast(shared->script()));
13163 hash ^= String::cast(script->source())->Hash();
13164 STATIC_ASSERT(LANGUAGE_END == 3);
13165 if (is_strict(language_mode)) hash ^= 0x8000;
13166 if (is_strong(language_mode)) hash ^= 0x10000;
13167 hash += scope_position;
13172 uint32_t Hash() override {
13173 return StringSharedHashHelper(*source_, *shared_, language_mode_,
13177 uint32_t HashForObject(Object* obj) override {
13178 DisallowHeapAllocation no_allocation;
13179 if (obj->IsNumber()) {
13180 return static_cast<uint32_t>(obj->Number());
13182 FixedArray* other_array = FixedArray::cast(obj);
13183 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
13184 String* source = String::cast(other_array->get(1));
13185 int language_unchecked = Smi::cast(other_array->get(2))->value();
13186 DCHECK(is_valid_language_mode(language_unchecked));
13187 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
13188 int scope_position = Smi::cast(other_array->get(3))->value();
13189 return StringSharedHashHelper(source, shared, language_mode,
13194 Handle<Object> AsHandle(Isolate* isolate) override {
13195 Handle<FixedArray> array = isolate->factory()->NewFixedArray(4);
13196 array->set(0, *shared_);
13197 array->set(1, *source_);
13198 array->set(2, Smi::FromInt(language_mode_));
13199 array->set(3, Smi::FromInt(scope_position_));
13204 Handle<String> source_;
13205 Handle<SharedFunctionInfo> shared_;
13206 LanguageMode language_mode_;
13207 int scope_position_;
13211 // RegExpKey carries the source and flags of a regular expression as key.
13212 class RegExpKey : public HashTableKey {
13214 RegExpKey(Handle<String> string, JSRegExp::Flags flags)
13216 flags_(Smi::FromInt(flags.value())) { }
13218 // Rather than storing the key in the hash table, a pointer to the
13219 // stored value is stored where the key should be. IsMatch then
13220 // compares the search key to the found object, rather than comparing
13222 bool IsMatch(Object* obj) override {
13223 FixedArray* val = FixedArray::cast(obj);
13224 return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex)))
13225 && (flags_ == val->get(JSRegExp::kFlagsIndex));
13228 uint32_t Hash() override { return RegExpHash(*string_, flags_); }
13230 Handle<Object> AsHandle(Isolate* isolate) override {
13231 // Plain hash maps, which is where regexp keys are used, don't
13232 // use this function.
13234 return MaybeHandle<Object>().ToHandleChecked();
13237 uint32_t HashForObject(Object* obj) override {
13238 FixedArray* val = FixedArray::cast(obj);
13239 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
13240 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
13243 static uint32_t RegExpHash(String* string, Smi* flags) {
13244 return string->Hash() + flags->value();
13247 Handle<String> string_;
13252 Handle<Object> OneByteStringKey::AsHandle(Isolate* isolate) {
13253 if (hash_field_ == 0) Hash();
13254 return isolate->factory()->NewOneByteInternalizedString(string_, hash_field_);
13258 Handle<Object> TwoByteStringKey::AsHandle(Isolate* isolate) {
13259 if (hash_field_ == 0) Hash();
13260 return isolate->factory()->NewTwoByteInternalizedString(string_, hash_field_);
13264 Handle<Object> SeqOneByteSubStringKey::AsHandle(Isolate* isolate) {
13265 if (hash_field_ == 0) Hash();
13266 return isolate->factory()->NewOneByteInternalizedSubString(
13267 string_, from_, length_, hash_field_);
13271 bool SeqOneByteSubStringKey::IsMatch(Object* string) {
13272 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
13273 return String::cast(string)->IsOneByteEqualTo(chars);
13277 // InternalizedStringKey carries a string/internalized-string object as key.
13278 class InternalizedStringKey : public HashTableKey {
13280 explicit InternalizedStringKey(Handle<String> string)
13281 : string_(string) { }
13283 bool IsMatch(Object* string) override {
13284 return String::cast(string)->Equals(*string_);
13287 uint32_t Hash() override { return string_->Hash(); }
13289 uint32_t HashForObject(Object* other) override {
13290 return String::cast(other)->Hash();
13293 Handle<Object> AsHandle(Isolate* isolate) override {
13294 // Internalize the string if possible.
13295 MaybeHandle<Map> maybe_map =
13296 isolate->factory()->InternalizedStringMapForString(string_);
13298 if (maybe_map.ToHandle(&map)) {
13299 string_->set_map_no_write_barrier(*map);
13300 DCHECK(string_->IsInternalizedString());
13303 // Otherwise allocate a new internalized string.
13304 return isolate->factory()->NewInternalizedStringImpl(
13305 string_, string_->length(), string_->hash_field());
13308 static uint32_t StringHash(Object* obj) {
13309 return String::cast(obj)->Hash();
13312 Handle<String> string_;
13316 template<typename Derived, typename Shape, typename Key>
13317 void HashTable<Derived, Shape, Key>::IteratePrefix(ObjectVisitor* v) {
13318 IteratePointers(v, 0, kElementsStartOffset);
13322 template<typename Derived, typename Shape, typename Key>
13323 void HashTable<Derived, Shape, Key>::IterateElements(ObjectVisitor* v) {
13325 kElementsStartOffset,
13326 kHeaderSize + length() * kPointerSize);
13330 template<typename Derived, typename Shape, typename Key>
13331 Handle<Derived> HashTable<Derived, Shape, Key>::New(
13333 int at_least_space_for,
13334 MinimumCapacity capacity_option,
13335 PretenureFlag pretenure) {
13336 DCHECK(0 <= at_least_space_for);
13337 DCHECK(!capacity_option || base::bits::IsPowerOfTwo32(at_least_space_for));
13339 int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY)
13340 ? at_least_space_for
13341 : ComputeCapacity(at_least_space_for);
13342 if (capacity > HashTable::kMaxCapacity) {
13343 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
13346 Factory* factory = isolate->factory();
13347 int length = EntryToIndex(capacity);
13348 Handle<FixedArray> array = factory->NewFixedArray(length, pretenure);
13349 array->set_map_no_write_barrier(*factory->hash_table_map());
13350 Handle<Derived> table = Handle<Derived>::cast(array);
13352 table->SetNumberOfElements(0);
13353 table->SetNumberOfDeletedElements(0);
13354 table->SetCapacity(capacity);
13359 // Find entry for key otherwise return kNotFound.
13360 template <typename Derived, typename Shape>
13361 int NameDictionaryBase<Derived, Shape>::FindEntry(Handle<Name> key) {
13362 if (!key->IsUniqueName()) {
13363 return DerivedDictionary::FindEntry(key);
13366 // Optimized for unique names. Knowledge of the key type allows:
13367 // 1. Move the check if the key is unique out of the loop.
13368 // 2. Avoid comparing hash codes in unique-to-unique comparison.
13369 // 3. Detect a case when a dictionary key is not unique but the key is.
13370 // In case of positive result the dictionary key may be replaced by the
13371 // internalized string with minimal performance penalty. It gives a chance
13372 // to perform further lookups in code stubs (and significant performance
13373 // boost a certain style of code).
13375 // EnsureCapacity will guarantee the hash table is never full.
13376 uint32_t capacity = this->Capacity();
13377 uint32_t entry = Derived::FirstProbe(key->Hash(), capacity);
13378 uint32_t count = 1;
13381 int index = Derived::EntryToIndex(entry);
13382 Object* element = this->get(index);
13383 if (element->IsUndefined()) break; // Empty entry.
13384 if (*key == element) return entry;
13385 if (!element->IsUniqueName() &&
13386 !element->IsTheHole() &&
13387 Name::cast(element)->Equals(*key)) {
13388 // Replace a key that is a non-internalized string by the equivalent
13389 // internalized string for faster further lookups.
13390 this->set(index, *key);
13393 DCHECK(element->IsTheHole() || !Name::cast(element)->Equals(*key));
13394 entry = Derived::NextProbe(entry, count++, capacity);
13396 return Derived::kNotFound;
13400 template<typename Derived, typename Shape, typename Key>
13401 void HashTable<Derived, Shape, Key>::Rehash(
13402 Handle<Derived> new_table,
13404 DCHECK(NumberOfElements() < new_table->Capacity());
13406 DisallowHeapAllocation no_gc;
13407 WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc);
13409 // Copy prefix to new array.
13410 for (int i = kPrefixStartIndex;
13411 i < kPrefixStartIndex + Shape::kPrefixSize;
13413 new_table->set(i, get(i), mode);
13416 // Rehash the elements.
13417 int capacity = this->Capacity();
13418 for (int i = 0; i < capacity; i++) {
13419 uint32_t from_index = EntryToIndex(i);
13420 Object* k = this->get(from_index);
13422 uint32_t hash = this->HashForObject(key, k);
13423 uint32_t insertion_index =
13424 EntryToIndex(new_table->FindInsertionEntry(hash));
13425 for (int j = 0; j < Shape::kEntrySize; j++) {
13426 new_table->set(insertion_index + j, get(from_index + j), mode);
13430 new_table->SetNumberOfElements(NumberOfElements());
13431 new_table->SetNumberOfDeletedElements(0);
13435 template<typename Derived, typename Shape, typename Key>
13436 uint32_t HashTable<Derived, Shape, Key>::EntryForProbe(
13440 uint32_t expected) {
13441 uint32_t hash = this->HashForObject(key, k);
13442 uint32_t capacity = this->Capacity();
13443 uint32_t entry = FirstProbe(hash, capacity);
13444 for (int i = 1; i < probe; i++) {
13445 if (entry == expected) return expected;
13446 entry = NextProbe(entry, i, capacity);
13452 template<typename Derived, typename Shape, typename Key>
13453 void HashTable<Derived, Shape, Key>::Swap(uint32_t entry1,
13455 WriteBarrierMode mode) {
13456 int index1 = EntryToIndex(entry1);
13457 int index2 = EntryToIndex(entry2);
13458 Object* temp[Shape::kEntrySize];
13459 for (int j = 0; j < Shape::kEntrySize; j++) {
13460 temp[j] = get(index1 + j);
13462 for (int j = 0; j < Shape::kEntrySize; j++) {
13463 set(index1 + j, get(index2 + j), mode);
13465 for (int j = 0; j < Shape::kEntrySize; j++) {
13466 set(index2 + j, temp[j], mode);
13471 template<typename Derived, typename Shape, typename Key>
13472 void HashTable<Derived, Shape, Key>::Rehash(Key key) {
13473 DisallowHeapAllocation no_gc;
13474 WriteBarrierMode mode = GetWriteBarrierMode(no_gc);
13475 uint32_t capacity = Capacity();
13477 for (int probe = 1; !done; probe++) {
13478 // All elements at entries given by one of the first _probe_ probes
13479 // are placed correctly. Other elements might need to be moved.
13481 for (uint32_t current = 0; current < capacity; current++) {
13482 Object* current_key = get(EntryToIndex(current));
13483 if (IsKey(current_key)) {
13484 uint32_t target = EntryForProbe(key, current_key, probe, current);
13485 if (current == target) continue;
13486 Object* target_key = get(EntryToIndex(target));
13487 if (!IsKey(target_key) ||
13488 EntryForProbe(key, target_key, probe, target) != target) {
13489 // Put the current element into the correct position.
13490 Swap(current, target, mode);
13491 // The other element will be processed on the next iteration.
13494 // The place for the current element is occupied. Leave the element
13495 // for the next probe.
13504 template<typename Derived, typename Shape, typename Key>
13505 Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
13506 Handle<Derived> table,
13509 PretenureFlag pretenure) {
13510 Isolate* isolate = table->GetIsolate();
13511 int capacity = table->Capacity();
13512 int nof = table->NumberOfElements() + n;
13513 int nod = table->NumberOfDeletedElements();
13515 // 50% is still free after adding n elements and
13516 // at most 50% of the free elements are deleted elements.
13517 if (nod <= (capacity - nof) >> 1) {
13518 int needed_free = nof >> 1;
13519 if (nof + needed_free <= capacity) return table;
13522 const int kMinCapacityForPretenure = 256;
13523 bool should_pretenure = pretenure == TENURED ||
13524 ((capacity > kMinCapacityForPretenure) &&
13525 !isolate->heap()->InNewSpace(*table));
13526 Handle<Derived> new_table = HashTable::New(
13529 USE_DEFAULT_MINIMUM_CAPACITY,
13530 should_pretenure ? TENURED : NOT_TENURED);
13532 table->Rehash(new_table, key);
13537 template<typename Derived, typename Shape, typename Key>
13538 Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
13540 int capacity = table->Capacity();
13541 int nof = table->NumberOfElements();
13543 // Shrink to fit the number of elements if only a quarter of the
13544 // capacity is filled with elements.
13545 if (nof > (capacity >> 2)) return table;
13546 // Allocate a new dictionary with room for at least the current
13547 // number of elements. The allocation method will make sure that
13548 // there is extra room in the dictionary for additions. Don't go
13549 // lower than room for 16 elements.
13550 int at_least_room_for = nof;
13551 if (at_least_room_for < 16) return table;
13553 Isolate* isolate = table->GetIsolate();
13554 const int kMinCapacityForPretenure = 256;
13556 (at_least_room_for > kMinCapacityForPretenure) &&
13557 !isolate->heap()->InNewSpace(*table);
13558 Handle<Derived> new_table = HashTable::New(
13561 USE_DEFAULT_MINIMUM_CAPACITY,
13562 pretenure ? TENURED : NOT_TENURED);
13564 table->Rehash(new_table, key);
13569 template<typename Derived, typename Shape, typename Key>
13570 uint32_t HashTable<Derived, Shape, Key>::FindInsertionEntry(uint32_t hash) {
13571 uint32_t capacity = Capacity();
13572 uint32_t entry = FirstProbe(hash, capacity);
13573 uint32_t count = 1;
13574 // EnsureCapacity will guarantee the hash table is never full.
13576 Object* element = KeyAt(entry);
13577 if (element->IsUndefined() || element->IsTheHole()) break;
13578 entry = NextProbe(entry, count++, capacity);
13584 // Force instantiation of template instances class.
13585 // Please note this list is compiler dependent.
13587 template class HashTable<StringTable, StringTableShape, HashTableKey*>;
13589 template class HashTable<CompilationCacheTable,
13590 CompilationCacheShape,
13593 template class HashTable<ObjectHashTable,
13594 ObjectHashTableShape,
13597 template class HashTable<WeakHashTable, WeakHashTableShape<2>, Handle<Object> >;
13599 template class Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >;
13601 template class Dictionary<GlobalDictionary, GlobalDictionaryShape,
13604 template class Dictionary<SeededNumberDictionary,
13605 SeededNumberDictionaryShape,
13608 template class Dictionary<UnseededNumberDictionary,
13609 UnseededNumberDictionaryShape,
13612 template Handle<SeededNumberDictionary>
13613 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13614 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13616 template Handle<UnseededNumberDictionary>
13617 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13618 New(Isolate*, int at_least_space_for, PretenureFlag pretenure);
13620 template Handle<NameDictionary>
13621 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13622 New(Isolate*, int n, PretenureFlag pretenure);
13624 template Handle<GlobalDictionary>
13625 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::New(
13626 Isolate*, int n, PretenureFlag pretenure);
13628 template Handle<SeededNumberDictionary>
13629 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13630 AtPut(Handle<SeededNumberDictionary>, uint32_t, Handle<Object>);
13632 template Handle<UnseededNumberDictionary>
13633 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13634 AtPut(Handle<UnseededNumberDictionary>, uint32_t, Handle<Object>);
13637 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13638 SlowReverseLookup(Object* value);
13641 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13642 SlowReverseLookup(Object* value);
13644 template Handle<Object>
13645 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::DeleteProperty(
13646 Handle<NameDictionary>, int);
13648 template Handle<Object>
13649 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13650 uint32_t>::DeleteProperty(Handle<SeededNumberDictionary>, int);
13652 template Handle<NameDictionary>
13653 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13654 New(Isolate*, int, MinimumCapacity, PretenureFlag);
13656 template Handle<NameDictionary>
13657 HashTable<NameDictionary, NameDictionaryShape, Handle<Name> >::
13658 Shrink(Handle<NameDictionary>, Handle<Name>);
13660 template Handle<SeededNumberDictionary>
13661 HashTable<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13662 Shrink(Handle<SeededNumberDictionary>, uint32_t);
13664 template Handle<NameDictionary>
13665 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::Add(
13666 Handle<NameDictionary>, Handle<Name>, Handle<Object>, PropertyDetails);
13668 template Handle<GlobalDictionary>
13669 Dictionary<GlobalDictionary, GlobalDictionaryShape, Handle<Name> >::Add(
13670 Handle<GlobalDictionary>, Handle<Name>, Handle<Object>,
13673 template Handle<FixedArray> Dictionary<
13674 NameDictionary, NameDictionaryShape,
13675 Handle<Name> >::BuildIterationIndicesArray(Handle<NameDictionary>);
13677 template Handle<FixedArray> Dictionary<
13678 NameDictionary, NameDictionaryShape,
13679 Handle<Name> >::GenerateNewEnumerationIndices(Handle<NameDictionary>);
13681 template Handle<SeededNumberDictionary>
13682 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13683 Add(Handle<SeededNumberDictionary>,
13688 template Handle<UnseededNumberDictionary>
13689 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13690 Add(Handle<UnseededNumberDictionary>,
13695 template Handle<SeededNumberDictionary>
13696 Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape, uint32_t>::
13697 EnsureCapacity(Handle<SeededNumberDictionary>, int, uint32_t);
13699 template Handle<UnseededNumberDictionary>
13700 Dictionary<UnseededNumberDictionary, UnseededNumberDictionaryShape, uint32_t>::
13701 EnsureCapacity(Handle<UnseededNumberDictionary>, int, uint32_t);
13703 template Handle<NameDictionary>
13704 Dictionary<NameDictionary, NameDictionaryShape, Handle<Name> >::
13705 EnsureCapacity(Handle<NameDictionary>, int, Handle<Name>);
13707 template bool Dictionary<SeededNumberDictionary, SeededNumberDictionaryShape,
13708 uint32_t>::HasComplexElements();
13710 template int HashTable<SeededNumberDictionary, SeededNumberDictionaryShape,
13711 uint32_t>::FindEntry(uint32_t);
13713 template int NameDictionaryBase<NameDictionary, NameDictionaryShape>::FindEntry(
13717 Handle<Object> JSObject::PrepareSlowElementsForSort(
13718 Handle<JSObject> object, uint32_t limit) {
13719 DCHECK(object->HasDictionaryElements());
13720 Isolate* isolate = object->GetIsolate();
13721 // Must stay in dictionary mode, either because of requires_slow_elements,
13722 // or because we are not going to sort (and therefore compact) all of the
13724 Handle<SeededNumberDictionary> dict(object->element_dictionary(), isolate);
13725 Handle<SeededNumberDictionary> new_dict =
13726 SeededNumberDictionary::New(isolate, dict->NumberOfElements());
13729 uint32_t undefs = 0;
13730 int capacity = dict->Capacity();
13731 Handle<Smi> bailout(Smi::FromInt(-1), isolate);
13732 // Entry to the new dictionary does not cause it to grow, as we have
13733 // allocated one that is large enough for all entries.
13734 DisallowHeapAllocation no_gc;
13735 for (int i = 0; i < capacity; i++) {
13736 Object* k = dict->KeyAt(i);
13737 if (!dict->IsKey(k)) continue;
13739 DCHECK(k->IsNumber());
13740 DCHECK(!k->IsSmi() || Smi::cast(k)->value() >= 0);
13741 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0);
13742 DCHECK(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32);
13744 HandleScope scope(isolate);
13745 Handle<Object> value(dict->ValueAt(i), isolate);
13746 PropertyDetails details = dict->DetailsAt(i);
13747 if (details.type() == ACCESSOR_CONSTANT || details.IsReadOnly()) {
13748 // Bail out and do the sorting of undefineds and array holes in JS.
13749 // Also bail out if the element is not supposed to be moved.
13753 uint32_t key = NumberToUint32(k);
13755 if (value->IsUndefined()) {
13757 } else if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
13758 // Adding an entry with the key beyond smi-range requires
13759 // allocation. Bailout.
13762 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13763 new_dict, pos, value, details, object->map()->is_prototype_map());
13764 DCHECK(result.is_identical_to(new_dict));
13768 } else if (key > static_cast<uint32_t>(Smi::kMaxValue)) {
13769 // Adding an entry with the key beyond smi-range requires
13770 // allocation. Bailout.
13773 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13774 new_dict, key, value, details, object->map()->is_prototype_map());
13775 DCHECK(result.is_identical_to(new_dict));
13780 uint32_t result = pos;
13781 PropertyDetails no_details = PropertyDetails::Empty();
13782 while (undefs > 0) {
13783 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
13784 // Adding an entry with the key beyond smi-range requires
13785 // allocation. Bailout.
13788 HandleScope scope(isolate);
13789 Handle<Object> result = SeededNumberDictionary::AddNumberEntry(
13790 new_dict, pos, isolate->factory()->undefined_value(), no_details,
13791 object->map()->is_prototype_map());
13792 DCHECK(result.is_identical_to(new_dict));
13798 object->set_elements(*new_dict);
13800 AllowHeapAllocation allocate_return_value;
13801 return isolate->factory()->NewNumberFromUint(result);
13805 // Collects all defined (non-hole) and non-undefined (array) elements at
13806 // the start of the elements array.
13807 // If the object is in dictionary mode, it is converted to fast elements
13809 Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object,
13811 Isolate* isolate = object->GetIsolate();
13812 if (object->HasSloppyArgumentsElements() ||
13813 object->map()->is_observed()) {
13814 return handle(Smi::FromInt(-1), isolate);
13817 if (object->HasDictionaryElements()) {
13818 // Convert to fast elements containing only the existing properties.
13819 // Ordering is irrelevant, since we are going to sort anyway.
13820 Handle<SeededNumberDictionary> dict(object->element_dictionary());
13821 if (object->IsJSArray() || dict->requires_slow_elements() ||
13822 dict->max_number_key() >= limit) {
13823 return JSObject::PrepareSlowElementsForSort(object, limit);
13825 // Convert to fast elements.
13827 Handle<Map> new_map =
13828 JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS);
13830 PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ?
13831 NOT_TENURED: TENURED;
13832 Handle<FixedArray> fast_elements =
13833 isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure);
13834 dict->CopyValuesTo(*fast_elements);
13835 JSObject::ValidateElements(object);
13837 JSObject::SetMapAndElements(object, new_map, fast_elements);
13838 } else if (object->HasFixedTypedArrayElements()) {
13839 // Typed arrays cannot have holes or undefined elements.
13840 return handle(Smi::FromInt(
13841 FixedArrayBase::cast(object->elements())->length()), isolate);
13842 } else if (!object->HasFastDoubleElements()) {
13843 EnsureWritableFastElements(object);
13845 DCHECK(object->HasFastSmiOrObjectElements() ||
13846 object->HasFastDoubleElements());
13848 // Collect holes at the end, undefined before that and the rest at the
13849 // start, and return the number of non-hole, non-undefined values.
13851 Handle<FixedArrayBase> elements_base(object->elements());
13852 uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
13853 if (limit > elements_length) {
13854 limit = elements_length ;
13857 return handle(Smi::FromInt(0), isolate);
13860 uint32_t result = 0;
13861 if (elements_base->map() == isolate->heap()->fixed_double_array_map()) {
13862 FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base);
13863 // Split elements into defined and the_hole, in that order.
13864 unsigned int holes = limit;
13865 // Assume most arrays contain no holes and undefined values, so minimize the
13866 // number of stores of non-undefined, non-the-hole values.
13867 for (unsigned int i = 0; i < holes; i++) {
13868 if (elements->is_the_hole(i)) {
13873 // Position i needs to be filled.
13874 while (holes > i) {
13875 if (elements->is_the_hole(holes)) {
13878 elements->set(i, elements->get_scalar(holes));
13884 while (holes < limit) {
13885 elements->set_the_hole(holes);
13889 FixedArray* elements = FixedArray::cast(*elements_base);
13890 DisallowHeapAllocation no_gc;
13892 // Split elements into defined, undefined and the_hole, in that order. Only
13893 // count locations for undefined and the hole, and fill them afterwards.
13894 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc);
13895 unsigned int undefs = limit;
13896 unsigned int holes = limit;
13897 // Assume most arrays contain no holes and undefined values, so minimize the
13898 // number of stores of non-undefined, non-the-hole values.
13899 for (unsigned int i = 0; i < undefs; i++) {
13900 Object* current = elements->get(i);
13901 if (current->IsTheHole()) {
13904 } else if (current->IsUndefined()) {
13909 // Position i needs to be filled.
13910 while (undefs > i) {
13911 current = elements->get(undefs);
13912 if (current->IsTheHole()) {
13915 } else if (current->IsUndefined()) {
13918 elements->set(i, current, write_barrier);
13924 while (undefs < holes) {
13925 elements->set_undefined(undefs);
13928 while (holes < limit) {
13929 elements->set_the_hole(holes);
13934 return isolate->factory()->NewNumberFromUint(result);
13938 ExternalArrayType JSTypedArray::type() {
13939 switch (elements()->map()->instance_type()) {
13940 #define INSTANCE_TYPE_TO_ARRAY_TYPE(Type, type, TYPE, ctype, size) \
13941 case FIXED_##TYPE##_ARRAY_TYPE: \
13942 return kExternal##Type##Array;
13944 TYPED_ARRAYS(INSTANCE_TYPE_TO_ARRAY_TYPE)
13945 #undef INSTANCE_TYPE_TO_ARRAY_TYPE
13949 return static_cast<ExternalArrayType>(-1);
13954 size_t JSTypedArray::element_size() {
13955 switch (elements()->map()->instance_type()) {
13956 #define INSTANCE_TYPE_TO_ELEMENT_SIZE(Type, type, TYPE, ctype, size) \
13957 case FIXED_##TYPE##_ARRAY_TYPE: \
13960 TYPED_ARRAYS(INSTANCE_TYPE_TO_ELEMENT_SIZE)
13961 #undef INSTANCE_TYPE_TO_ELEMENT_SIZE
13970 void FixedArray::SetValue(uint32_t index, Object* value) { set(index, value); }
13973 void FixedDoubleArray::SetValue(uint32_t index, Object* value) {
13974 set(index, value->Number());
13976 void GlobalObject::InvalidatePropertyCell(Handle<GlobalObject> global,
13977 Handle<Name> name) {
13978 DCHECK(!global->HasFastProperties());
13979 auto dictionary = handle(global->global_dictionary());
13980 int entry = dictionary->FindEntry(name);
13981 if (entry == GlobalDictionary::kNotFound) return;
13982 PropertyCell::InvalidateEntry(dictionary, entry);
13986 // TODO(ishell): rename to EnsureEmptyPropertyCell or something.
13987 Handle<PropertyCell> GlobalObject::EnsurePropertyCell(
13988 Handle<GlobalObject> global, Handle<Name> name) {
13989 DCHECK(!global->HasFastProperties());
13990 auto dictionary = handle(global->global_dictionary());
13991 int entry = dictionary->FindEntry(name);
13992 Handle<PropertyCell> cell;
13993 if (entry != GlobalDictionary::kNotFound) {
13994 // This call should be idempotent.
13995 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
13996 cell = handle(PropertyCell::cast(dictionary->ValueAt(entry)));
13997 DCHECK(cell->property_details().cell_type() ==
13998 PropertyCellType::kUninitialized ||
13999 cell->property_details().cell_type() ==
14000 PropertyCellType::kInvalidated);
14001 DCHECK(cell->value()->IsTheHole());
14004 Isolate* isolate = global->GetIsolate();
14005 cell = isolate->factory()->NewPropertyCell();
14006 PropertyDetails details(NONE, DATA, 0, PropertyCellType::kUninitialized);
14007 dictionary = GlobalDictionary::Add(dictionary, name, cell, details);
14008 global->set_properties(*dictionary);
14013 // This class is used for looking up two character strings in the string table.
14014 // If we don't have a hit we don't want to waste much time so we unroll the
14015 // string hash calculation loop here for speed. Doesn't work if the two
14016 // characters form a decimal integer, since such strings have a different hash
14018 class TwoCharHashTableKey : public HashTableKey {
14020 TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed)
14021 : c1_(c1), c2_(c2) {
14023 uint32_t hash = seed;
14025 hash += hash << 10;
14029 hash += hash << 10;
14033 hash ^= hash >> 11;
14034 hash += hash << 15;
14035 if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash;
14038 // If this assert fails then we failed to reproduce the two-character
14039 // version of the string hashing algorithm above. One reason could be
14040 // that we were passed two digits as characters, since the hash
14041 // algorithm is different in that case.
14042 uint16_t chars[2] = {c1, c2};
14043 uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed);
14044 hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask;
14045 DCHECK_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash));
14049 bool IsMatch(Object* o) override {
14050 if (!o->IsString()) return false;
14051 String* other = String::cast(o);
14052 if (other->length() != 2) return false;
14053 if (other->Get(0) != c1_) return false;
14054 return other->Get(1) == c2_;
14057 uint32_t Hash() override { return hash_; }
14058 uint32_t HashForObject(Object* key) override {
14059 if (!key->IsString()) return 0;
14060 return String::cast(key)->Hash();
14063 Handle<Object> AsHandle(Isolate* isolate) override {
14064 // The TwoCharHashTableKey is only used for looking in the string
14065 // table, not for adding to it.
14067 return MaybeHandle<Object>().ToHandleChecked();
14077 MaybeHandle<String> StringTable::InternalizeStringIfExists(
14079 Handle<String> string) {
14080 if (string->IsInternalizedString()) {
14083 return LookupStringIfExists(isolate, string);
14087 MaybeHandle<String> StringTable::LookupStringIfExists(
14089 Handle<String> string) {
14090 Handle<StringTable> string_table = isolate->factory()->string_table();
14091 InternalizedStringKey key(string);
14092 int entry = string_table->FindEntry(&key);
14093 if (entry == kNotFound) {
14094 return MaybeHandle<String>();
14096 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14097 DCHECK(StringShape(*result).IsInternalized());
14103 MaybeHandle<String> StringTable::LookupTwoCharsStringIfExists(
14107 Handle<StringTable> string_table = isolate->factory()->string_table();
14108 TwoCharHashTableKey key(c1, c2, isolate->heap()->HashSeed());
14109 int entry = string_table->FindEntry(&key);
14110 if (entry == kNotFound) {
14111 return MaybeHandle<String>();
14113 Handle<String> result(String::cast(string_table->KeyAt(entry)), isolate);
14114 DCHECK(StringShape(*result).IsInternalized());
14120 void StringTable::EnsureCapacityForDeserialization(Isolate* isolate,
14122 Handle<StringTable> table = isolate->factory()->string_table();
14123 // We need a key instance for the virtual hash function.
14124 InternalizedStringKey dummy_key(Handle<String>::null());
14125 table = StringTable::EnsureCapacity(table, expected, &dummy_key);
14126 isolate->heap()->SetRootStringTable(*table);
14130 Handle<String> StringTable::LookupString(Isolate* isolate,
14131 Handle<String> string) {
14132 InternalizedStringKey key(string);
14133 return LookupKey(isolate, &key);
14137 Handle<String> StringTable::LookupKey(Isolate* isolate, HashTableKey* key) {
14138 Handle<StringTable> table = isolate->factory()->string_table();
14139 int entry = table->FindEntry(key);
14141 // String already in table.
14142 if (entry != kNotFound) {
14143 return handle(String::cast(table->KeyAt(entry)), isolate);
14146 // Adding new string. Grow table if needed.
14147 table = StringTable::EnsureCapacity(table, 1, key);
14149 // Create string object.
14150 Handle<Object> string = key->AsHandle(isolate);
14151 // There must be no attempts to internalize strings that could throw
14152 // InvalidStringLength error.
14153 CHECK(!string.is_null());
14155 // Add the new string and return it along with the string table.
14156 entry = table->FindInsertionEntry(key->Hash());
14157 table->set(EntryToIndex(entry), *string);
14158 table->ElementAdded();
14160 isolate->heap()->SetRootStringTable(*table);
14161 return Handle<String>::cast(string);
14165 String* StringTable::LookupKeyIfExists(Isolate* isolate, HashTableKey* key) {
14166 Handle<StringTable> table = isolate->factory()->string_table();
14167 int entry = table->FindEntry(key);
14168 if (entry != kNotFound) return String::cast(table->KeyAt(entry));
14173 Handle<Object> CompilationCacheTable::Lookup(Handle<String> src,
14174 Handle<Context> context,
14175 LanguageMode language_mode) {
14176 Isolate* isolate = GetIsolate();
14177 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14178 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14179 int entry = FindEntry(&key);
14180 if (entry == kNotFound) return isolate->factory()->undefined_value();
14181 int index = EntryToIndex(entry);
14182 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14183 return Handle<Object>(get(index + 1), isolate);
14187 Handle<Object> CompilationCacheTable::LookupEval(
14188 Handle<String> src, Handle<SharedFunctionInfo> outer_info,
14189 LanguageMode language_mode, int scope_position) {
14190 Isolate* isolate = GetIsolate();
14191 // Cache key is the tuple (source, outer shared function info, scope position)
14192 // to unambiguously identify the context chain the cached eval code assumes.
14193 StringSharedKey key(src, outer_info, language_mode, scope_position);
14194 int entry = FindEntry(&key);
14195 if (entry == kNotFound) return isolate->factory()->undefined_value();
14196 int index = EntryToIndex(entry);
14197 if (!get(index)->IsFixedArray()) return isolate->factory()->undefined_value();
14198 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14202 Handle<Object> CompilationCacheTable::LookupRegExp(Handle<String> src,
14203 JSRegExp::Flags flags) {
14204 Isolate* isolate = GetIsolate();
14205 DisallowHeapAllocation no_allocation;
14206 RegExpKey key(src, flags);
14207 int entry = FindEntry(&key);
14208 if (entry == kNotFound) return isolate->factory()->undefined_value();
14209 return Handle<Object>(get(EntryToIndex(entry) + 1), isolate);
14213 Handle<CompilationCacheTable> CompilationCacheTable::Put(
14214 Handle<CompilationCacheTable> cache, Handle<String> src,
14215 Handle<Context> context, LanguageMode language_mode, Handle<Object> value) {
14216 Isolate* isolate = cache->GetIsolate();
14217 Handle<SharedFunctionInfo> shared(context->closure()->shared());
14218 StringSharedKey key(src, shared, language_mode, RelocInfo::kNoPosition);
14220 Handle<Object> k = key.AsHandle(isolate);
14221 DisallowHeapAllocation no_allocation_scope;
14222 int entry = cache->FindEntry(&key);
14223 if (entry != kNotFound) {
14224 cache->set(EntryToIndex(entry), *k);
14225 cache->set(EntryToIndex(entry) + 1, *value);
14230 cache = EnsureCapacity(cache, 1, &key);
14231 int entry = cache->FindInsertionEntry(key.Hash());
14233 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14234 cache->set(EntryToIndex(entry), *k);
14235 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14236 cache->ElementAdded();
14241 Handle<CompilationCacheTable> CompilationCacheTable::PutEval(
14242 Handle<CompilationCacheTable> cache, Handle<String> src,
14243 Handle<SharedFunctionInfo> outer_info, Handle<SharedFunctionInfo> value,
14244 int scope_position) {
14245 Isolate* isolate = cache->GetIsolate();
14246 StringSharedKey key(src, outer_info, value->language_mode(), scope_position);
14248 Handle<Object> k = key.AsHandle(isolate);
14249 DisallowHeapAllocation no_allocation_scope;
14250 int entry = cache->FindEntry(&key);
14251 if (entry != kNotFound) {
14252 cache->set(EntryToIndex(entry), *k);
14253 cache->set(EntryToIndex(entry) + 1, *value);
14258 cache = EnsureCapacity(cache, 1, &key);
14259 int entry = cache->FindInsertionEntry(key.Hash());
14261 isolate->factory()->NewNumber(static_cast<double>(key.Hash()));
14262 cache->set(EntryToIndex(entry), *k);
14263 cache->set(EntryToIndex(entry) + 1, Smi::FromInt(kHashGenerations));
14264 cache->ElementAdded();
14269 Handle<CompilationCacheTable> CompilationCacheTable::PutRegExp(
14270 Handle<CompilationCacheTable> cache, Handle<String> src,
14271 JSRegExp::Flags flags, Handle<FixedArray> value) {
14272 RegExpKey key(src, flags);
14273 cache = EnsureCapacity(cache, 1, &key);
14274 int entry = cache->FindInsertionEntry(key.Hash());
14275 // We store the value in the key slot, and compare the search key
14276 // to the stored value with a custon IsMatch function during lookups.
14277 cache->set(EntryToIndex(entry), *value);
14278 cache->set(EntryToIndex(entry) + 1, *value);
14279 cache->ElementAdded();
14284 void CompilationCacheTable::Age() {
14285 DisallowHeapAllocation no_allocation;
14286 Object* the_hole_value = GetHeap()->the_hole_value();
14287 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14288 int entry_index = EntryToIndex(entry);
14289 int value_index = entry_index + 1;
14291 if (get(entry_index)->IsNumber()) {
14292 Smi* count = Smi::cast(get(value_index));
14293 count = Smi::FromInt(count->value() - 1);
14294 if (count->value() == 0) {
14295 NoWriteBarrierSet(this, entry_index, the_hole_value);
14296 NoWriteBarrierSet(this, value_index, the_hole_value);
14299 NoWriteBarrierSet(this, value_index, count);
14301 } else if (get(entry_index)->IsFixedArray()) {
14302 SharedFunctionInfo* info = SharedFunctionInfo::cast(get(value_index));
14303 if (info->code()->kind() != Code::FUNCTION || info->code()->IsOld()) {
14304 NoWriteBarrierSet(this, entry_index, the_hole_value);
14305 NoWriteBarrierSet(this, value_index, the_hole_value);
14313 void CompilationCacheTable::Remove(Object* value) {
14314 DisallowHeapAllocation no_allocation;
14315 Object* the_hole_value = GetHeap()->the_hole_value();
14316 for (int entry = 0, size = Capacity(); entry < size; entry++) {
14317 int entry_index = EntryToIndex(entry);
14318 int value_index = entry_index + 1;
14319 if (get(value_index) == value) {
14320 NoWriteBarrierSet(this, entry_index, the_hole_value);
14321 NoWriteBarrierSet(this, value_index, the_hole_value);
14329 // StringsKey used for HashTable where key is array of internalized strings.
14330 class StringsKey : public HashTableKey {
14332 explicit StringsKey(Handle<FixedArray> strings) : strings_(strings) { }
14334 bool IsMatch(Object* strings) override {
14335 FixedArray* o = FixedArray::cast(strings);
14336 int len = strings_->length();
14337 if (o->length() != len) return false;
14338 for (int i = 0; i < len; i++) {
14339 if (o->get(i) != strings_->get(i)) return false;
14344 uint32_t Hash() override { return HashForObject(*strings_); }
14346 uint32_t HashForObject(Object* obj) override {
14347 FixedArray* strings = FixedArray::cast(obj);
14348 int len = strings->length();
14350 for (int i = 0; i < len; i++) {
14351 hash ^= String::cast(strings->get(i))->Hash();
14356 Handle<Object> AsHandle(Isolate* isolate) override { return strings_; }
14359 Handle<FixedArray> strings_;
14363 template<typename Derived, typename Shape, typename Key>
14364 Handle<Derived> Dictionary<Derived, Shape, Key>::New(
14366 int at_least_space_for,
14367 PretenureFlag pretenure) {
14368 DCHECK(0 <= at_least_space_for);
14369 Handle<Derived> dict = DerivedHashTable::New(isolate,
14370 at_least_space_for,
14371 USE_DEFAULT_MINIMUM_CAPACITY,
14374 // Initialize the next enumeration index.
14375 dict->SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
14380 template <typename Derived, typename Shape, typename Key>
14381 Handle<FixedArray> Dictionary<Derived, Shape, Key>::BuildIterationIndicesArray(
14382 Handle<Derived> dictionary) {
14383 Factory* factory = dictionary->GetIsolate()->factory();
14384 int length = dictionary->NumberOfElements();
14386 Handle<FixedArray> iteration_order = factory->NewFixedArray(length);
14387 Handle<FixedArray> enumeration_order = factory->NewFixedArray(length);
14389 // Fill both the iteration order array and the enumeration order array
14390 // with property details.
14391 int capacity = dictionary->Capacity();
14393 for (int i = 0; i < capacity; i++) {
14394 if (dictionary->IsKey(dictionary->KeyAt(i))) {
14395 int index = dictionary->DetailsAt(i).dictionary_index();
14396 iteration_order->set(pos, Smi::FromInt(i));
14397 enumeration_order->set(pos, Smi::FromInt(index));
14401 DCHECK(pos == length);
14403 // Sort the arrays wrt. enumeration order.
14404 iteration_order->SortPairs(*enumeration_order, enumeration_order->length());
14405 return iteration_order;
14409 template <typename Derived, typename Shape, typename Key>
14411 Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
14412 Handle<Derived> dictionary) {
14413 int length = dictionary->NumberOfElements();
14415 Handle<FixedArray> iteration_order = BuildIterationIndicesArray(dictionary);
14416 DCHECK(iteration_order->length() == length);
14418 // Iterate over the dictionary using the enumeration order and update
14419 // the dictionary with new enumeration indices.
14420 for (int i = 0; i < length; i++) {
14421 int index = Smi::cast(iteration_order->get(i))->value();
14422 DCHECK(dictionary->IsKey(dictionary->KeyAt(index)));
14424 int enum_index = PropertyDetails::kInitialIndex + i;
14426 PropertyDetails details = dictionary->DetailsAt(index);
14427 PropertyDetails new_details = details.set_index(enum_index);
14428 dictionary->DetailsAtPut(index, new_details);
14431 // Set the next enumeration index.
14432 dictionary->SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length);
14433 return iteration_order;
14437 template<typename Derived, typename Shape, typename Key>
14438 Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
14439 Handle<Derived> dictionary, int n, Key key) {
14440 // Check whether there are enough enumeration indices to add n elements.
14441 if (Shape::kIsEnumerable &&
14442 !PropertyDetails::IsValidIndex(dictionary->NextEnumerationIndex() + n)) {
14443 // If not, we generate new indices for the properties.
14444 GenerateNewEnumerationIndices(dictionary);
14446 return DerivedHashTable::EnsureCapacity(dictionary, n, key);
14450 template <typename Derived, typename Shape, typename Key>
14451 Handle<Object> Dictionary<Derived, Shape, Key>::DeleteProperty(
14452 Handle<Derived> dictionary, int entry) {
14453 Factory* factory = dictionary->GetIsolate()->factory();
14454 PropertyDetails details = dictionary->DetailsAt(entry);
14455 if (!details.IsConfigurable()) return factory->false_value();
14457 dictionary->SetEntry(
14458 entry, factory->the_hole_value(), factory->the_hole_value());
14459 dictionary->ElementRemoved();
14460 return factory->true_value();
14464 template<typename Derived, typename Shape, typename Key>
14465 Handle<Derived> Dictionary<Derived, Shape, Key>::AtPut(
14466 Handle<Derived> dictionary, Key key, Handle<Object> value) {
14467 int entry = dictionary->FindEntry(key);
14469 // If the entry is present set the value;
14470 if (entry != Dictionary::kNotFound) {
14471 dictionary->ValueAtPut(entry, *value);
14475 // Check whether the dictionary should be extended.
14476 dictionary = EnsureCapacity(dictionary, 1, key);
14478 USE(Shape::AsHandle(dictionary->GetIsolate(), key));
14480 PropertyDetails details = PropertyDetails::Empty();
14482 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14487 template<typename Derived, typename Shape, typename Key>
14488 Handle<Derived> Dictionary<Derived, Shape, Key>::Add(
14489 Handle<Derived> dictionary,
14491 Handle<Object> value,
14492 PropertyDetails details) {
14493 // Valdate key is absent.
14494 SLOW_DCHECK((dictionary->FindEntry(key) == Dictionary::kNotFound));
14495 // Check whether the dictionary should be extended.
14496 dictionary = EnsureCapacity(dictionary, 1, key);
14498 AddEntry(dictionary, key, value, details, dictionary->Hash(key));
14503 // Add a key, value pair to the dictionary.
14504 template<typename Derived, typename Shape, typename Key>
14505 void Dictionary<Derived, Shape, Key>::AddEntry(
14506 Handle<Derived> dictionary,
14508 Handle<Object> value,
14509 PropertyDetails details,
14511 // Compute the key object.
14512 Handle<Object> k = Shape::AsHandle(dictionary->GetIsolate(), key);
14514 uint32_t entry = dictionary->FindInsertionEntry(hash);
14515 // Insert element at empty or deleted entry
14516 if (details.dictionary_index() == 0 && Shape::kIsEnumerable) {
14517 // Assign an enumeration index to the property and update
14518 // SetNextEnumerationIndex.
14519 int index = dictionary->NextEnumerationIndex();
14520 details = details.set_index(index);
14521 dictionary->SetNextEnumerationIndex(index + 1);
14523 dictionary->SetEntry(entry, k, value, details);
14524 DCHECK((dictionary->KeyAt(entry)->IsNumber() ||
14525 dictionary->KeyAt(entry)->IsName()));
14526 dictionary->ElementAdded();
14530 void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key,
14531 bool used_as_prototype) {
14532 DisallowHeapAllocation no_allocation;
14533 // If the dictionary requires slow elements an element has already
14534 // been added at a high index.
14535 if (requires_slow_elements()) return;
14536 // Check if this index is high enough that we should require slow
14538 if (key > kRequiresSlowElementsLimit) {
14539 if (used_as_prototype) {
14540 // TODO(verwaest): Remove this hack.
14541 GetHeap()->ClearAllKeyedStoreICs();
14543 set_requires_slow_elements();
14546 // Update max key value.
14547 Object* max_index_object = get(kMaxNumberKeyIndex);
14548 if (!max_index_object->IsSmi() || max_number_key() < key) {
14549 FixedArray::set(kMaxNumberKeyIndex,
14550 Smi::FromInt(key << kRequiresSlowElementsTagSize));
14555 Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry(
14556 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14557 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14558 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14559 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14560 return Add(dictionary, key, value, details);
14564 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AddNumberEntry(
14565 Handle<UnseededNumberDictionary> dictionary,
14567 Handle<Object> value) {
14568 SLOW_DCHECK(dictionary->FindEntry(key) == kNotFound);
14569 return Add(dictionary, key, value, PropertyDetails::Empty());
14573 Handle<SeededNumberDictionary> SeededNumberDictionary::AtNumberPut(
14574 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14575 Handle<Object> value, bool used_as_prototype) {
14576 dictionary->UpdateMaxNumberKey(key, used_as_prototype);
14577 return AtPut(dictionary, key, value);
14581 Handle<UnseededNumberDictionary> UnseededNumberDictionary::AtNumberPut(
14582 Handle<UnseededNumberDictionary> dictionary,
14584 Handle<Object> value) {
14585 return AtPut(dictionary, key, value);
14589 Handle<SeededNumberDictionary> SeededNumberDictionary::Set(
14590 Handle<SeededNumberDictionary> dictionary, uint32_t key,
14591 Handle<Object> value, PropertyDetails details, bool used_as_prototype) {
14592 int entry = dictionary->FindEntry(key);
14593 if (entry == kNotFound) {
14594 return AddNumberEntry(dictionary, key, value, details, used_as_prototype);
14596 // Preserve enumeration index.
14597 details = details.set_index(dictionary->DetailsAt(entry).dictionary_index());
14598 Handle<Object> object_key =
14599 SeededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14600 dictionary->SetEntry(entry, object_key, value, details);
14605 Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set(
14606 Handle<UnseededNumberDictionary> dictionary,
14608 Handle<Object> value) {
14609 int entry = dictionary->FindEntry(key);
14610 if (entry == kNotFound) return AddNumberEntry(dictionary, key, value);
14611 Handle<Object> object_key =
14612 UnseededNumberDictionaryShape::AsHandle(dictionary->GetIsolate(), key);
14613 dictionary->SetEntry(entry, object_key, value);
14618 template <typename Derived, typename Shape, typename Key>
14619 int Dictionary<Derived, Shape, Key>::NumberOfElementsFilterAttributes(
14620 PropertyAttributes filter) {
14621 int capacity = this->Capacity();
14623 for (int i = 0; i < capacity; i++) {
14624 Object* k = this->KeyAt(i);
14625 if (this->IsKey(k) && !FilterKey(k, filter)) {
14626 if (this->IsDeleted(i)) continue;
14627 PropertyDetails details = this->DetailsAt(i);
14628 PropertyAttributes attr = details.attributes();
14629 if ((attr & filter) == 0) result++;
14636 template <typename Derived, typename Shape, typename Key>
14637 bool Dictionary<Derived, Shape, Key>::HasComplexElements() {
14638 int capacity = this->Capacity();
14639 for (int i = 0; i < capacity; i++) {
14640 Object* k = this->KeyAt(i);
14641 if (this->IsKey(k) && !FilterKey(k, NONE)) {
14642 if (this->IsDeleted(i)) continue;
14643 PropertyDetails details = this->DetailsAt(i);
14644 if (details.type() == ACCESSOR_CONSTANT) return true;
14645 PropertyAttributes attr = details.attributes();
14646 if (attr & (READ_ONLY | DONT_DELETE | DONT_ENUM)) return true;
14653 template <typename Dictionary>
14654 struct EnumIndexComparator {
14655 explicit EnumIndexComparator(Dictionary* dict) : dict(dict) {}
14656 bool operator() (Smi* a, Smi* b) {
14657 PropertyDetails da(dict->DetailsAt(a->value()));
14658 PropertyDetails db(dict->DetailsAt(b->value()));
14659 return da.dictionary_index() < db.dictionary_index();
14665 template <typename Derived, typename Shape, typename Key>
14666 void Dictionary<Derived, Shape, Key>::CopyEnumKeysTo(FixedArray* storage) {
14667 int length = storage->length();
14668 int capacity = this->Capacity();
14669 int properties = 0;
14670 for (int i = 0; i < capacity; i++) {
14671 Object* k = this->KeyAt(i);
14672 if (this->IsKey(k) && !k->IsSymbol()) {
14673 PropertyDetails details = this->DetailsAt(i);
14674 if (details.IsDontEnum() || this->IsDeleted(i)) continue;
14675 storage->set(properties, Smi::FromInt(i));
14677 if (properties == length) break;
14680 CHECK_EQ(length, properties);
14681 EnumIndexComparator<Derived> cmp(static_cast<Derived*>(this));
14682 Smi** start = reinterpret_cast<Smi**>(storage->GetFirstElementAddress());
14683 std::sort(start, start + length, cmp);
14684 for (int i = 0; i < length; i++) {
14685 int index = Smi::cast(storage->get(i))->value();
14686 storage->set(i, this->KeyAt(index));
14691 template <typename Derived, typename Shape, typename Key>
14692 int Dictionary<Derived, Shape, Key>::CopyKeysTo(
14693 FixedArray* storage, int index, PropertyAttributes filter,
14694 typename Dictionary<Derived, Shape, Key>::SortMode sort_mode) {
14695 DCHECK(storage->length() >= NumberOfElementsFilterAttributes(filter));
14696 int start_index = index;
14697 int capacity = this->Capacity();
14698 for (int i = 0; i < capacity; i++) {
14699 Object* k = this->KeyAt(i);
14700 if (this->IsKey(k) && !FilterKey(k, filter)) {
14701 if (this->IsDeleted(i)) continue;
14702 PropertyDetails details = this->DetailsAt(i);
14703 PropertyAttributes attr = details.attributes();
14704 if ((attr & filter) == 0) storage->set(index++, k);
14707 if (sort_mode == Dictionary::SORTED) {
14708 storage->SortPairs(storage, index);
14710 DCHECK(storage->length() >= index);
14711 return index - start_index;
14715 // Backwards lookup (slow).
14716 template<typename Derived, typename Shape, typename Key>
14717 Object* Dictionary<Derived, Shape, Key>::SlowReverseLookup(Object* value) {
14718 int capacity = this->Capacity();
14719 for (int i = 0; i < capacity; i++) {
14720 Object* k = this->KeyAt(i);
14721 if (this->IsKey(k)) {
14722 Object* e = this->ValueAt(i);
14723 // TODO(dcarney): this should be templatized.
14724 if (e->IsPropertyCell()) {
14725 e = PropertyCell::cast(e)->value();
14727 if (e == value) return k;
14730 Heap* heap = Dictionary::GetHeap();
14731 return heap->undefined_value();
14735 Object* ObjectHashTable::Lookup(Isolate* isolate, Handle<Object> key,
14737 DisallowHeapAllocation no_gc;
14738 DCHECK(IsKey(*key));
14740 int entry = FindEntry(isolate, key, hash);
14741 if (entry == kNotFound) return isolate->heap()->the_hole_value();
14742 return get(EntryToIndex(entry) + 1);
14746 Object* ObjectHashTable::Lookup(Handle<Object> key) {
14747 DisallowHeapAllocation no_gc;
14748 DCHECK(IsKey(*key));
14750 Isolate* isolate = GetIsolate();
14752 // If the object does not have an identity hash, it was never used as a key.
14753 Object* hash = key->GetHash();
14754 if (hash->IsUndefined()) {
14755 return isolate->heap()->the_hole_value();
14757 return Lookup(isolate, key, Smi::cast(hash)->value());
14761 Object* ObjectHashTable::Lookup(Handle<Object> key, int32_t hash) {
14762 return Lookup(GetIsolate(), key, hash);
14766 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
14767 Handle<Object> key,
14768 Handle<Object> value) {
14769 DCHECK(table->IsKey(*key));
14770 DCHECK(!value->IsTheHole());
14772 Isolate* isolate = table->GetIsolate();
14773 // Make sure the key object has an identity hash code.
14774 int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
14776 return Put(table, key, value, hash);
14780 Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
14781 Handle<Object> key,
14782 Handle<Object> value,
14784 DCHECK(table->IsKey(*key));
14785 DCHECK(!value->IsTheHole());
14787 Isolate* isolate = table->GetIsolate();
14789 int entry = table->FindEntry(isolate, key, hash);
14791 // Key is already in table, just overwrite value.
14792 if (entry != kNotFound) {
14793 table->set(EntryToIndex(entry) + 1, *value);
14797 // Check whether the hash table should be extended.
14798 table = EnsureCapacity(table, 1, key);
14799 table->AddEntry(table->FindInsertionEntry(hash), *key, *value);
14804 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
14805 Handle<Object> key,
14806 bool* was_present) {
14807 DCHECK(table->IsKey(*key));
14809 Object* hash = key->GetHash();
14810 if (hash->IsUndefined()) {
14811 *was_present = false;
14815 return Remove(table, key, was_present, Smi::cast(hash)->value());
14819 Handle<ObjectHashTable> ObjectHashTable::Remove(Handle<ObjectHashTable> table,
14820 Handle<Object> key,
14823 DCHECK(table->IsKey(*key));
14825 int entry = table->FindEntry(table->GetIsolate(), key, hash);
14826 if (entry == kNotFound) {
14827 *was_present = false;
14831 *was_present = true;
14832 table->RemoveEntry(entry);
14833 return Shrink(table, key);
14837 void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) {
14838 set(EntryToIndex(entry), key);
14839 set(EntryToIndex(entry) + 1, value);
14844 void ObjectHashTable::RemoveEntry(int entry) {
14845 set_the_hole(EntryToIndex(entry));
14846 set_the_hole(EntryToIndex(entry) + 1);
14851 Object* WeakHashTable::Lookup(Handle<HeapObject> key) {
14852 DisallowHeapAllocation no_gc;
14853 DCHECK(IsKey(*key));
14854 int entry = FindEntry(key);
14855 if (entry == kNotFound) return GetHeap()->the_hole_value();
14856 return get(EntryToValueIndex(entry));
14860 Handle<WeakHashTable> WeakHashTable::Put(Handle<WeakHashTable> table,
14861 Handle<HeapObject> key,
14862 Handle<HeapObject> value) {
14863 DCHECK(table->IsKey(*key));
14864 int entry = table->FindEntry(key);
14865 // Key is already in table, just overwrite value.
14866 if (entry != kNotFound) {
14867 table->set(EntryToValueIndex(entry), *value);
14871 Handle<WeakCell> key_cell = key->GetIsolate()->factory()->NewWeakCell(key);
14873 // Check whether the hash table should be extended.
14874 table = EnsureCapacity(table, 1, key, TENURED);
14876 table->AddEntry(table->FindInsertionEntry(table->Hash(key)), key_cell, value);
14881 void WeakHashTable::AddEntry(int entry, Handle<WeakCell> key_cell,
14882 Handle<HeapObject> value) {
14883 DisallowHeapAllocation no_allocation;
14884 set(EntryToIndex(entry), *key_cell);
14885 set(EntryToValueIndex(entry), *value);
14890 template<class Derived, class Iterator, int entrysize>
14891 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Allocate(
14892 Isolate* isolate, int capacity, PretenureFlag pretenure) {
14893 // Capacity must be a power of two, since we depend on being able
14894 // to divide and multiple by 2 (kLoadFactor) to derive capacity
14895 // from number of buckets. If we decide to change kLoadFactor
14896 // to something other than 2, capacity should be stored as another
14897 // field of this object.
14898 capacity = base::bits::RoundUpToPowerOfTwo32(Max(kMinCapacity, capacity));
14899 if (capacity > kMaxCapacity) {
14900 v8::internal::Heap::FatalProcessOutOfMemory("invalid table size", true);
14902 int num_buckets = capacity / kLoadFactor;
14903 Handle<FixedArray> backing_store = isolate->factory()->NewFixedArray(
14904 kHashTableStartIndex + num_buckets + (capacity * kEntrySize), pretenure);
14905 backing_store->set_map_no_write_barrier(
14906 isolate->heap()->ordered_hash_table_map());
14907 Handle<Derived> table = Handle<Derived>::cast(backing_store);
14908 for (int i = 0; i < num_buckets; ++i) {
14909 table->set(kHashTableStartIndex + i, Smi::FromInt(kNotFound));
14911 table->SetNumberOfBuckets(num_buckets);
14912 table->SetNumberOfElements(0);
14913 table->SetNumberOfDeletedElements(0);
14918 template<class Derived, class Iterator, int entrysize>
14919 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::EnsureGrowable(
14920 Handle<Derived> table) {
14921 DCHECK(!table->IsObsolete());
14923 int nof = table->NumberOfElements();
14924 int nod = table->NumberOfDeletedElements();
14925 int capacity = table->Capacity();
14926 if ((nof + nod) < capacity) return table;
14927 // Don't need to grow if we can simply clear out deleted entries instead.
14928 // Note that we can't compact in place, though, so we always allocate
14930 return Rehash(table, (nod < (capacity >> 1)) ? capacity << 1 : capacity);
14934 template<class Derived, class Iterator, int entrysize>
14935 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Shrink(
14936 Handle<Derived> table) {
14937 DCHECK(!table->IsObsolete());
14939 int nof = table->NumberOfElements();
14940 int capacity = table->Capacity();
14941 if (nof >= (capacity >> 2)) return table;
14942 return Rehash(table, capacity / 2);
14946 template<class Derived, class Iterator, int entrysize>
14947 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Clear(
14948 Handle<Derived> table) {
14949 DCHECK(!table->IsObsolete());
14951 Handle<Derived> new_table =
14952 Allocate(table->GetIsolate(),
14954 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
14956 table->SetNextTable(*new_table);
14957 table->SetNumberOfDeletedElements(kClearedTableSentinel);
14963 template<class Derived, class Iterator, int entrysize>
14964 Handle<Derived> OrderedHashTable<Derived, Iterator, entrysize>::Rehash(
14965 Handle<Derived> table, int new_capacity) {
14966 DCHECK(!table->IsObsolete());
14968 Handle<Derived> new_table =
14969 Allocate(table->GetIsolate(),
14971 table->GetHeap()->InNewSpace(*table) ? NOT_TENURED : TENURED);
14972 int nof = table->NumberOfElements();
14973 int nod = table->NumberOfDeletedElements();
14974 int new_buckets = new_table->NumberOfBuckets();
14976 int removed_holes_index = 0;
14978 for (int old_entry = 0; old_entry < (nof + nod); ++old_entry) {
14979 Object* key = table->KeyAt(old_entry);
14980 if (key->IsTheHole()) {
14981 table->SetRemovedIndexAt(removed_holes_index++, old_entry);
14985 Object* hash = key->GetHash();
14986 int bucket = Smi::cast(hash)->value() & (new_buckets - 1);
14987 Object* chain_entry = new_table->get(kHashTableStartIndex + bucket);
14988 new_table->set(kHashTableStartIndex + bucket, Smi::FromInt(new_entry));
14989 int new_index = new_table->EntryToIndex(new_entry);
14990 int old_index = table->EntryToIndex(old_entry);
14991 for (int i = 0; i < entrysize; ++i) {
14992 Object* value = table->get(old_index + i);
14993 new_table->set(new_index + i, value);
14995 new_table->set(new_index + kChainOffset, chain_entry);
14999 DCHECK_EQ(nod, removed_holes_index);
15001 new_table->SetNumberOfElements(nof);
15002 table->SetNextTable(*new_table);
15008 template Handle<OrderedHashSet>
15009 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Allocate(
15010 Isolate* isolate, int capacity, PretenureFlag pretenure);
15012 template Handle<OrderedHashSet>
15013 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::EnsureGrowable(
15014 Handle<OrderedHashSet> table);
15016 template Handle<OrderedHashSet>
15017 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Shrink(
15018 Handle<OrderedHashSet> table);
15020 template Handle<OrderedHashSet>
15021 OrderedHashTable<OrderedHashSet, JSSetIterator, 1>::Clear(
15022 Handle<OrderedHashSet> table);
15025 template Handle<OrderedHashMap>
15026 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Allocate(
15027 Isolate* isolate, int capacity, PretenureFlag pretenure);
15029 template Handle<OrderedHashMap>
15030 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::EnsureGrowable(
15031 Handle<OrderedHashMap> table);
15033 template Handle<OrderedHashMap>
15034 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Shrink(
15035 Handle<OrderedHashMap> table);
15037 template Handle<OrderedHashMap>
15038 OrderedHashTable<OrderedHashMap, JSMapIterator, 2>::Clear(
15039 Handle<OrderedHashMap> table);
15042 template<class Derived, class TableType>
15043 void OrderedHashTableIterator<Derived, TableType>::Transition() {
15044 DisallowHeapAllocation no_allocation;
15045 TableType* table = TableType::cast(this->table());
15046 if (!table->IsObsolete()) return;
15048 int index = Smi::cast(this->index())->value();
15049 while (table->IsObsolete()) {
15050 TableType* next_table = table->NextTable();
15053 int nod = table->NumberOfDeletedElements();
15055 if (nod == TableType::kClearedTableSentinel) {
15058 int old_index = index;
15059 for (int i = 0; i < nod; ++i) {
15060 int removed_index = table->RemovedIndexAt(i);
15061 if (removed_index >= old_index) break;
15067 table = next_table;
15071 set_index(Smi::FromInt(index));
15075 template<class Derived, class TableType>
15076 bool OrderedHashTableIterator<Derived, TableType>::HasMore() {
15077 DisallowHeapAllocation no_allocation;
15078 if (this->table()->IsUndefined()) return false;
15082 TableType* table = TableType::cast(this->table());
15083 int index = Smi::cast(this->index())->value();
15084 int used_capacity = table->UsedCapacity();
15086 while (index < used_capacity && table->KeyAt(index)->IsTheHole()) {
15090 set_index(Smi::FromInt(index));
15092 if (index < used_capacity) return true;
15094 set_table(GetHeap()->undefined_value());
15099 template<class Derived, class TableType>
15100 Smi* OrderedHashTableIterator<Derived, TableType>::Next(JSArray* value_array) {
15101 DisallowHeapAllocation no_allocation;
15103 FixedArray* array = FixedArray::cast(value_array->elements());
15104 static_cast<Derived*>(this)->PopulateValueArray(array);
15106 return Smi::cast(kind());
15108 return Smi::FromInt(0);
15113 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Next(
15114 JSArray* value_array);
15117 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::HasMore();
15120 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::MoveNext();
15123 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::CurrentKey();
15126 OrderedHashTableIterator<JSSetIterator, OrderedHashSet>::Transition();
15130 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Next(
15131 JSArray* value_array);
15134 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::HasMore();
15137 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::MoveNext();
15140 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::CurrentKey();
15143 OrderedHashTableIterator<JSMapIterator, OrderedHashMap>::Transition();
15146 void JSSet::Initialize(Handle<JSSet> set, Isolate* isolate) {
15147 Handle<OrderedHashSet> table = isolate->factory()->NewOrderedHashSet();
15148 set->set_table(*table);
15152 void JSSet::Clear(Handle<JSSet> set) {
15153 Handle<OrderedHashSet> table(OrderedHashSet::cast(set->table()));
15154 table = OrderedHashSet::Clear(table);
15155 set->set_table(*table);
15159 void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) {
15160 Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
15161 map->set_table(*table);
15165 void JSMap::Clear(Handle<JSMap> map) {
15166 Handle<OrderedHashMap> table(OrderedHashMap::cast(map->table()));
15167 table = OrderedHashMap::Clear(table);
15168 map->set_table(*table);
15172 void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection,
15173 Isolate* isolate) {
15174 DCHECK_EQ(0, weak_collection->map()->GetInObjectProperties());
15175 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 0);
15176 weak_collection->set_table(*table);
15180 void JSWeakCollection::Set(Handle<JSWeakCollection> weak_collection,
15181 Handle<Object> key, Handle<Object> value,
15183 DCHECK(key->IsJSReceiver() || key->IsSymbol());
15184 Handle<ObjectHashTable> table(
15185 ObjectHashTable::cast(weak_collection->table()));
15186 DCHECK(table->IsKey(*key));
15187 Handle<ObjectHashTable> new_table =
15188 ObjectHashTable::Put(table, key, value, hash);
15189 weak_collection->set_table(*new_table);
15190 if (*table != *new_table) {
15191 // Zap the old table since we didn't record slots for its elements.
15192 table->FillWithHoles(0, table->length());
15197 bool JSWeakCollection::Delete(Handle<JSWeakCollection> weak_collection,
15198 Handle<Object> key, int32_t hash) {
15199 DCHECK(key->IsJSReceiver() || key->IsSymbol());
15200 Handle<ObjectHashTable> table(
15201 ObjectHashTable::cast(weak_collection->table()));
15202 DCHECK(table->IsKey(*key));
15203 bool was_present = false;
15204 Handle<ObjectHashTable> new_table =
15205 ObjectHashTable::Remove(table, key, &was_present, hash);
15206 weak_collection->set_table(*new_table);
15207 if (*table != *new_table) {
15208 // Zap the old table since we didn't record slots for its elements.
15209 table->FillWithHoles(0, table->length());
15211 return was_present;
15215 // Check if there is a break point at this code position.
15216 bool DebugInfo::HasBreakPoint(int code_position) {
15217 // Get the break point info object for this code position.
15218 Object* break_point_info = GetBreakPointInfo(code_position);
15220 // If there is no break point info object or no break points in the break
15221 // point info object there is no break point at this code position.
15222 if (break_point_info->IsUndefined()) return false;
15223 return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0;
15227 // Get the break point info object for this code position.
15228 Object* DebugInfo::GetBreakPointInfo(int code_position) {
15229 // Find the index of the break point info object for this code position.
15230 int index = GetBreakPointInfoIndex(code_position);
15232 // Return the break point info object if any.
15233 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
15234 return BreakPointInfo::cast(break_points()->get(index));
15238 // Clear a break point at the specified code position.
15239 void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info,
15241 Handle<Object> break_point_object) {
15242 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15243 debug_info->GetIsolate());
15244 if (break_point_info->IsUndefined()) return;
15245 BreakPointInfo::ClearBreakPoint(
15246 Handle<BreakPointInfo>::cast(break_point_info),
15247 break_point_object);
15251 void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info,
15253 int source_position,
15254 int statement_position,
15255 Handle<Object> break_point_object) {
15256 Isolate* isolate = debug_info->GetIsolate();
15257 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position),
15259 if (!break_point_info->IsUndefined()) {
15260 BreakPointInfo::SetBreakPoint(
15261 Handle<BreakPointInfo>::cast(break_point_info),
15262 break_point_object);
15266 // Adding a new break point for a code position which did not have any
15267 // break points before. Try to find a free slot.
15268 int index = kNoBreakPointInfo;
15269 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15270 if (debug_info->break_points()->get(i)->IsUndefined()) {
15275 if (index == kNoBreakPointInfo) {
15276 // No free slot - extend break point info array.
15277 Handle<FixedArray> old_break_points =
15278 Handle<FixedArray>(FixedArray::cast(debug_info->break_points()));
15279 Handle<FixedArray> new_break_points =
15280 isolate->factory()->NewFixedArray(
15281 old_break_points->length() +
15282 DebugInfo::kEstimatedNofBreakPointsInFunction);
15284 debug_info->set_break_points(*new_break_points);
15285 for (int i = 0; i < old_break_points->length(); i++) {
15286 new_break_points->set(i, old_break_points->get(i));
15288 index = old_break_points->length();
15290 DCHECK(index != kNoBreakPointInfo);
15292 // Allocate new BreakPointInfo object and set the break point.
15293 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
15294 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
15295 new_break_point_info->set_code_position(Smi::FromInt(code_position));
15296 new_break_point_info->set_source_position(Smi::FromInt(source_position));
15297 new_break_point_info->
15298 set_statement_position(Smi::FromInt(statement_position));
15299 new_break_point_info->set_break_point_objects(
15300 isolate->heap()->undefined_value());
15301 BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object);
15302 debug_info->break_points()->set(index, *new_break_point_info);
15306 // Get the break point objects for a code position.
15307 Handle<Object> DebugInfo::GetBreakPointObjects(int code_position) {
15308 Object* break_point_info = GetBreakPointInfo(code_position);
15309 if (break_point_info->IsUndefined()) {
15310 return GetIsolate()->factory()->undefined_value();
15312 return Handle<Object>(
15313 BreakPointInfo::cast(break_point_info)->break_point_objects(),
15318 // Get the total number of break points.
15319 int DebugInfo::GetBreakPointCount() {
15320 if (break_points()->IsUndefined()) return 0;
15322 for (int i = 0; i < break_points()->length(); i++) {
15323 if (!break_points()->get(i)->IsUndefined()) {
15324 BreakPointInfo* break_point_info =
15325 BreakPointInfo::cast(break_points()->get(i));
15326 count += break_point_info->GetBreakPointCount();
15333 Handle<Object> DebugInfo::FindBreakPointInfo(
15334 Handle<DebugInfo> debug_info, Handle<Object> break_point_object) {
15335 Isolate* isolate = debug_info->GetIsolate();
15336 if (!debug_info->break_points()->IsUndefined()) {
15337 for (int i = 0; i < debug_info->break_points()->length(); i++) {
15338 if (!debug_info->break_points()->get(i)->IsUndefined()) {
15339 Handle<BreakPointInfo> break_point_info = Handle<BreakPointInfo>(
15340 BreakPointInfo::cast(debug_info->break_points()->get(i)), isolate);
15341 if (BreakPointInfo::HasBreakPointObject(break_point_info,
15342 break_point_object)) {
15343 return break_point_info;
15348 return isolate->factory()->undefined_value();
15352 // Find the index of the break point info object for the specified code
15354 int DebugInfo::GetBreakPointInfoIndex(int code_position) {
15355 if (break_points()->IsUndefined()) return kNoBreakPointInfo;
15356 for (int i = 0; i < break_points()->length(); i++) {
15357 if (!break_points()->get(i)->IsUndefined()) {
15358 BreakPointInfo* break_point_info =
15359 BreakPointInfo::cast(break_points()->get(i));
15360 if (break_point_info->code_position()->value() == code_position) {
15365 return kNoBreakPointInfo;
15369 // Remove the specified break point object.
15370 void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
15371 Handle<Object> break_point_object) {
15372 Isolate* isolate = break_point_info->GetIsolate();
15373 // If there are no break points just ignore.
15374 if (break_point_info->break_point_objects()->IsUndefined()) return;
15375 // If there is a single break point clear it if it is the same.
15376 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15377 if (break_point_info->break_point_objects() == *break_point_object) {
15378 break_point_info->set_break_point_objects(
15379 isolate->heap()->undefined_value());
15383 // If there are multiple break points shrink the array
15384 DCHECK(break_point_info->break_point_objects()->IsFixedArray());
15385 Handle<FixedArray> old_array =
15386 Handle<FixedArray>(
15387 FixedArray::cast(break_point_info->break_point_objects()));
15388 Handle<FixedArray> new_array =
15389 isolate->factory()->NewFixedArray(old_array->length() - 1);
15390 int found_count = 0;
15391 for (int i = 0; i < old_array->length(); i++) {
15392 if (old_array->get(i) == *break_point_object) {
15393 DCHECK(found_count == 0);
15396 new_array->set(i - found_count, old_array->get(i));
15399 // If the break point was found in the list change it.
15400 if (found_count > 0) break_point_info->set_break_point_objects(*new_array);
15404 // Add the specified break point object.
15405 void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info,
15406 Handle<Object> break_point_object) {
15407 Isolate* isolate = break_point_info->GetIsolate();
15409 // If there was no break point objects before just set it.
15410 if (break_point_info->break_point_objects()->IsUndefined()) {
15411 break_point_info->set_break_point_objects(*break_point_object);
15414 // If the break point object is the same as before just ignore.
15415 if (break_point_info->break_point_objects() == *break_point_object) return;
15416 // If there was one break point object before replace with array.
15417 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15418 Handle<FixedArray> array = isolate->factory()->NewFixedArray(2);
15419 array->set(0, break_point_info->break_point_objects());
15420 array->set(1, *break_point_object);
15421 break_point_info->set_break_point_objects(*array);
15424 // If there was more than one break point before extend array.
15425 Handle<FixedArray> old_array =
15426 Handle<FixedArray>(
15427 FixedArray::cast(break_point_info->break_point_objects()));
15428 Handle<FixedArray> new_array =
15429 isolate->factory()->NewFixedArray(old_array->length() + 1);
15430 for (int i = 0; i < old_array->length(); i++) {
15431 // If the break point was there before just ignore.
15432 if (old_array->get(i) == *break_point_object) return;
15433 new_array->set(i, old_array->get(i));
15435 // Add the new break point.
15436 new_array->set(old_array->length(), *break_point_object);
15437 break_point_info->set_break_point_objects(*new_array);
15441 bool BreakPointInfo::HasBreakPointObject(
15442 Handle<BreakPointInfo> break_point_info,
15443 Handle<Object> break_point_object) {
15445 if (break_point_info->break_point_objects()->IsUndefined()) return false;
15446 // Single break point.
15447 if (!break_point_info->break_point_objects()->IsFixedArray()) {
15448 return break_point_info->break_point_objects() == *break_point_object;
15450 // Multiple break points.
15451 FixedArray* array = FixedArray::cast(break_point_info->break_point_objects());
15452 for (int i = 0; i < array->length(); i++) {
15453 if (array->get(i) == *break_point_object) {
15461 // Get the number of break points.
15462 int BreakPointInfo::GetBreakPointCount() {
15464 if (break_point_objects()->IsUndefined()) return 0;
15465 // Single break point.
15466 if (!break_point_objects()->IsFixedArray()) return 1;
15467 // Multiple break points.
15468 return FixedArray::cast(break_point_objects())->length();
15472 Object* JSDate::GetField(Object* object, Smi* index) {
15473 return JSDate::cast(object)->DoGetField(
15474 static_cast<FieldIndex>(index->value()));
15478 Object* JSDate::DoGetField(FieldIndex index) {
15479 DCHECK(index != kDateValue);
15481 DateCache* date_cache = GetIsolate()->date_cache();
15483 if (index < kFirstUncachedField) {
15484 Object* stamp = cache_stamp();
15485 if (stamp != date_cache->stamp() && stamp->IsSmi()) {
15486 // Since the stamp is not NaN, the value is also not NaN.
15487 int64_t local_time_ms =
15488 date_cache->ToLocal(static_cast<int64_t>(value()->Number()));
15489 SetCachedFields(local_time_ms, date_cache);
15492 case kYear: return year();
15493 case kMonth: return month();
15494 case kDay: return day();
15495 case kWeekday: return weekday();
15496 case kHour: return hour();
15497 case kMinute: return min();
15498 case kSecond: return sec();
15499 default: UNREACHABLE();
15503 if (index >= kFirstUTCField) {
15504 return GetUTCField(index, value()->Number(), date_cache);
15507 double time = value()->Number();
15508 if (std::isnan(time)) return GetIsolate()->heap()->nan_value();
15510 int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time));
15511 int days = DateCache::DaysFromTime(local_time_ms);
15513 if (index == kDays) return Smi::FromInt(days);
15515 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15516 if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000);
15517 DCHECK(index == kTimeInDay);
15518 return Smi::FromInt(time_in_day_ms);
15522 Object* JSDate::GetUTCField(FieldIndex index,
15524 DateCache* date_cache) {
15525 DCHECK(index >= kFirstUTCField);
15527 if (std::isnan(value)) return GetIsolate()->heap()->nan_value();
15529 int64_t time_ms = static_cast<int64_t>(value);
15531 if (index == kTimezoneOffset) {
15532 return Smi::FromInt(date_cache->TimezoneOffset(time_ms));
15535 int days = DateCache::DaysFromTime(time_ms);
15537 if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days));
15539 if (index <= kDayUTC) {
15540 int year, month, day;
15541 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15542 if (index == kYearUTC) return Smi::FromInt(year);
15543 if (index == kMonthUTC) return Smi::FromInt(month);
15544 DCHECK(index == kDayUTC);
15545 return Smi::FromInt(day);
15548 int time_in_day_ms = DateCache::TimeInDay(time_ms, days);
15550 case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000));
15551 case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60);
15552 case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60);
15553 case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000);
15554 case kDaysUTC: return Smi::FromInt(days);
15555 case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms);
15556 default: UNREACHABLE();
15564 void JSDate::SetValue(Object* value, bool is_value_nan) {
15566 if (is_value_nan) {
15567 HeapNumber* nan = GetIsolate()->heap()->nan_value();
15568 set_cache_stamp(nan, SKIP_WRITE_BARRIER);
15569 set_year(nan, SKIP_WRITE_BARRIER);
15570 set_month(nan, SKIP_WRITE_BARRIER);
15571 set_day(nan, SKIP_WRITE_BARRIER);
15572 set_hour(nan, SKIP_WRITE_BARRIER);
15573 set_min(nan, SKIP_WRITE_BARRIER);
15574 set_sec(nan, SKIP_WRITE_BARRIER);
15575 set_weekday(nan, SKIP_WRITE_BARRIER);
15577 set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER);
15582 void JSDate::SetCachedFields(int64_t local_time_ms, DateCache* date_cache) {
15583 int days = DateCache::DaysFromTime(local_time_ms);
15584 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
15585 int year, month, day;
15586 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
15587 int weekday = date_cache->Weekday(days);
15588 int hour = time_in_day_ms / (60 * 60 * 1000);
15589 int min = (time_in_day_ms / (60 * 1000)) % 60;
15590 int sec = (time_in_day_ms / 1000) % 60;
15591 set_cache_stamp(date_cache->stamp());
15592 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
15593 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
15594 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
15595 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
15596 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
15597 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
15598 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
15602 void JSArrayBuffer::Neuter() {
15603 CHECK(is_neuterable());
15604 CHECK(is_external());
15605 set_backing_store(NULL);
15606 set_byte_length(Smi::FromInt(0));
15607 set_was_neutered(true);
15611 void JSArrayBuffer::Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate,
15612 bool is_external, void* data, size_t allocated_length,
15613 SharedFlag shared) {
15614 DCHECK(array_buffer->GetInternalFieldCount() ==
15615 v8::ArrayBuffer::kInternalFieldCount);
15616 for (int i = 0; i < v8::ArrayBuffer::kInternalFieldCount; i++) {
15617 array_buffer->SetInternalField(i, Smi::FromInt(0));
15619 array_buffer->set_backing_store(data);
15620 array_buffer->set_bit_field(0);
15621 array_buffer->set_is_external(is_external);
15622 array_buffer->set_is_neuterable(shared == SharedFlag::kNotShared);
15623 array_buffer->set_is_shared(shared == SharedFlag::kShared);
15625 if (data && !is_external) {
15626 isolate->heap()->RegisterNewArrayBuffer(
15627 isolate->heap()->InNewSpace(*array_buffer), data, allocated_length);
15630 Handle<Object> byte_length =
15631 isolate->factory()->NewNumberFromSize(allocated_length);
15632 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber());
15633 array_buffer->set_byte_length(*byte_length);
15637 bool JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer> array_buffer,
15639 size_t allocated_length,
15640 bool initialize, SharedFlag shared) {
15642 CHECK(isolate->array_buffer_allocator() != NULL);
15643 // Prevent creating array buffers when serializing.
15644 DCHECK(!isolate->serializer_enabled());
15645 if (allocated_length != 0) {
15647 data = isolate->array_buffer_allocator()->Allocate(allocated_length);
15649 data = isolate->array_buffer_allocator()->AllocateUninitialized(
15652 if (data == NULL) return false;
15657 JSArrayBuffer::Setup(array_buffer, isolate, false, data, allocated_length,
15663 Handle<JSArrayBuffer> JSTypedArray::MaterializeArrayBuffer(
15664 Handle<JSTypedArray> typed_array) {
15666 Handle<Map> map(typed_array->map());
15667 Isolate* isolate = typed_array->GetIsolate();
15669 DCHECK(IsFixedTypedArrayElementsKind(map->elements_kind()));
15671 Handle<FixedTypedArrayBase> fixed_typed_array(
15672 FixedTypedArrayBase::cast(typed_array->elements()));
15674 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(typed_array->buffer()),
15676 void* backing_store =
15677 isolate->array_buffer_allocator()->AllocateUninitialized(
15678 fixed_typed_array->DataSize());
15679 buffer->set_backing_store(backing_store);
15680 buffer->set_is_external(false);
15681 isolate->heap()->RegisterNewArrayBuffer(isolate->heap()->InNewSpace(*buffer),
15683 fixed_typed_array->DataSize());
15684 memcpy(buffer->backing_store(),
15685 fixed_typed_array->DataPtr(),
15686 fixed_typed_array->DataSize());
15687 Handle<FixedTypedArrayBase> new_elements =
15688 isolate->factory()->NewFixedTypedArrayWithExternalPointer(
15689 fixed_typed_array->length(), typed_array->type(),
15690 static_cast<uint8_t*>(buffer->backing_store()));
15692 typed_array->set_elements(*new_elements);
15698 Handle<JSArrayBuffer> JSTypedArray::GetBuffer() {
15699 Handle<JSArrayBuffer> array_buffer(JSArrayBuffer::cast(buffer()),
15701 if (array_buffer->was_neutered() ||
15702 array_buffer->backing_store() != nullptr) {
15703 return array_buffer;
15705 Handle<JSTypedArray> self(this);
15706 return MaterializeArrayBuffer(self);
15710 Handle<PropertyCell> PropertyCell::InvalidateEntry(
15711 Handle<GlobalDictionary> dictionary, int entry) {
15712 Isolate* isolate = dictionary->GetIsolate();
15713 // Swap with a copy.
15714 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
15715 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
15716 auto new_cell = isolate->factory()->NewPropertyCell();
15717 new_cell->set_value(cell->value());
15718 dictionary->ValueAtPut(entry, *new_cell);
15719 bool is_the_hole = cell->value()->IsTheHole();
15720 // Cell is officially mutable henceforth.
15721 PropertyDetails details = cell->property_details();
15722 details = details.set_cell_type(is_the_hole ? PropertyCellType::kInvalidated
15723 : PropertyCellType::kMutable);
15724 new_cell->set_property_details(details);
15725 // Old cell is ready for invalidation.
15727 cell->set_value(isolate->heap()->undefined_value());
15729 cell->set_value(isolate->heap()->the_hole_value());
15731 details = details.set_cell_type(PropertyCellType::kInvalidated);
15732 cell->set_property_details(details);
15733 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15734 isolate, DependentCode::kPropertyCellChangedGroup);
15739 PropertyCellConstantType PropertyCell::GetConstantType() {
15740 if (value()->IsSmi()) return PropertyCellConstantType::kSmi;
15741 return PropertyCellConstantType::kStableMap;
15745 static bool RemainsConstantType(Handle<PropertyCell> cell,
15746 Handle<Object> value) {
15747 // TODO(dcarney): double->smi and smi->double transition from kConstant
15748 if (cell->value()->IsSmi() && value->IsSmi()) {
15750 } else if (cell->value()->IsHeapObject() && value->IsHeapObject()) {
15751 return HeapObject::cast(cell->value())->map() ==
15752 HeapObject::cast(*value)->map() &&
15753 HeapObject::cast(*value)->map()->is_stable();
15759 PropertyCellType PropertyCell::UpdatedType(Handle<PropertyCell> cell,
15760 Handle<Object> value,
15761 PropertyDetails details) {
15762 PropertyCellType type = details.cell_type();
15763 DCHECK(!value->IsTheHole());
15764 if (cell->value()->IsTheHole()) {
15766 // Only allow a cell to transition once into constant state.
15767 case PropertyCellType::kUninitialized:
15768 if (value->IsUndefined()) return PropertyCellType::kUndefined;
15769 return PropertyCellType::kConstant;
15770 case PropertyCellType::kInvalidated:
15771 return PropertyCellType::kMutable;
15774 return PropertyCellType::kMutable;
15778 case PropertyCellType::kUndefined:
15779 return PropertyCellType::kConstant;
15780 case PropertyCellType::kConstant:
15781 if (*value == cell->value()) return PropertyCellType::kConstant;
15783 case PropertyCellType::kConstantType:
15784 if (RemainsConstantType(cell, value)) {
15785 return PropertyCellType::kConstantType;
15788 case PropertyCellType::kMutable:
15789 return PropertyCellType::kMutable;
15792 return PropertyCellType::kMutable;
15796 void PropertyCell::UpdateCell(Handle<GlobalDictionary> dictionary, int entry,
15797 Handle<Object> value, PropertyDetails details) {
15798 DCHECK(!value->IsTheHole());
15799 DCHECK(dictionary->ValueAt(entry)->IsPropertyCell());
15800 Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry)));
15801 const PropertyDetails original_details = cell->property_details();
15802 // Data accesses could be cached in ics or optimized code.
15804 original_details.kind() == kData && details.kind() == kAccessor;
15805 int index = original_details.dictionary_index();
15806 PropertyCellType old_type = original_details.cell_type();
15807 // Preserve the enumeration index unless the property was deleted or never
15809 if (cell->value()->IsTheHole()) {
15810 index = dictionary->NextEnumerationIndex();
15811 dictionary->SetNextEnumerationIndex(index + 1);
15812 // Negative lookup cells must be invalidated.
15816 details = details.set_index(index);
15818 PropertyCellType new_type = UpdatedType(cell, value, original_details);
15819 if (invalidate) cell = PropertyCell::InvalidateEntry(dictionary, entry);
15821 // Install new property details and cell value.
15822 details = details.set_cell_type(new_type);
15823 cell->set_property_details(details);
15824 cell->set_value(*value);
15826 // Deopt when transitioning from a constant type.
15827 if (!invalidate && (old_type != new_type ||
15828 original_details.IsReadOnly() != details.IsReadOnly())) {
15829 Isolate* isolate = dictionary->GetIsolate();
15830 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15831 isolate, DependentCode::kPropertyCellChangedGroup);
15837 void PropertyCell::SetValueWithInvalidation(Handle<PropertyCell> cell,
15838 Handle<Object> new_value) {
15839 if (cell->value() != *new_value) {
15840 cell->set_value(*new_value);
15841 Isolate* isolate = cell->GetIsolate();
15842 cell->dependent_code()->DeoptimizeDependentCodeGroup(
15843 isolate, DependentCode::kPropertyCellChangedGroup);
15847 } // namespace internal