yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 30 | #include "accessors.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 31 | #include "api.h" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 32 | #include "arguments.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | #include "deoptimizer.h" |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 37 | #include "date.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 38 | #include "elements.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | #include "execution.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 40 | #include "full-codegen.h" |
| 41 | #include "hydrogen.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | #include "objects-inl.h" |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 43 | #include "objects-visiting.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 44 | #include "objects-visiting-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | #include "macro-assembler.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 46 | #include "mark-compact.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 47 | #include "safepoint-table.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | #include "string-stream.h" |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 49 | #include "utils.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 50 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 52 | #ifdef ENABLE_DISASSEMBLER |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 53 | #include "disasm.h" |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 54 | #include "disassembler.h" |
| 55 | #endif |
| 56 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 57 | namespace v8 { |
| 58 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 60 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 61 | MUST_USE_RESULT static MaybeObject* CreateJSValue(JSFunction* constructor, |
| 62 | Object* value) { |
| 63 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 64 | { MaybeObject* maybe_result = |
| 65 | constructor->GetHeap()->AllocateJSObject(constructor); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 66 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 67 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | JSValue::cast(result)->set_value(value); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 73 | MaybeObject* Object::ToObject(Context* native_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | if (IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 75 | return CreateJSValue(native_context->number_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | } else if (IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 77 | return CreateJSValue(native_context->boolean_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | } else if (IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 79 | return CreateJSValue(native_context->string_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | } |
| 81 | ASSERT(IsJSObject()); |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 86 | MaybeObject* Object::ToObject() { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 87 | if (IsJSReceiver()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | return this; |
| 89 | } else if (IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 90 | Isolate* isolate = Isolate::Current(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 91 | Context* native_context = isolate->context()->native_context(); |
| 92 | return CreateJSValue(native_context->number_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | } else if (IsBoolean()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 94 | Isolate* isolate = HeapObject::cast(this)->GetIsolate(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 95 | Context* native_context = isolate->context()->native_context(); |
| 96 | return CreateJSValue(native_context->boolean_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | } else if (IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 98 | Isolate* isolate = HeapObject::cast(this)->GetIsolate(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 99 | Context* native_context = isolate->context()->native_context(); |
| 100 | return CreateJSValue(native_context->string_function(), this); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 101 | } else if (IsSymbol()) { |
| 102 | Isolate* isolate = HeapObject::cast(this)->GetIsolate(); |
| 103 | Context* native_context = isolate->context()->native_context(); |
| 104 | return CreateJSValue(native_context->symbol_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Throw a type error. |
| 108 | return Failure::InternalError(); |
| 109 | } |
| 110 | |
| 111 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 112 | bool Object::BooleanValue() { |
| 113 | if (IsBoolean()) return IsTrue(); |
| 114 | if (IsSmi()) return Smi::cast(this)->value() != 0; |
| 115 | if (IsUndefined() || IsNull()) return false; |
| 116 | if (IsUndetectableObject()) return false; // Undetectable object is false. |
| 117 | if (IsString()) return String::cast(this)->length() != 0; |
| 118 | if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue(); |
| 119 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 123 | void Object::Lookup(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | Object* holder = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 125 | if (IsJSReceiver()) { |
| 126 | holder = this; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 127 | } else { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 128 | Context* native_context = result->isolate()->context()->native_context(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 129 | if (IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 130 | holder = native_context->number_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 131 | } else if (IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 132 | holder = native_context->string_function()->instance_prototype(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 133 | } else if (IsSymbol()) { |
| 134 | holder = native_context->symbol_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 135 | } else if (IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 136 | holder = native_context->boolean_function()->instance_prototype(); |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 137 | } else { |
| 138 | Isolate::Current()->PushStackTraceAndDie( |
| 139 | 0xDEAD0000, this, JSReceiver::cast(this)->map(), 0xDEAD0001); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 140 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 142 | ASSERT(holder != NULL); // Cannot handle null or undefined. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 143 | JSReceiver::cast(holder)->Lookup(name, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 147 | MaybeObject* Object::GetPropertyWithReceiver(Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 148 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 149 | PropertyAttributes* attributes) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 150 | LookupResult result(name->GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | Lookup(name, &result); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 152 | MaybeObject* value = GetProperty(receiver, &result, name, attributes); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 153 | ASSERT(*attributes <= ABSENT); |
| 154 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 158 | template<typename To> |
| 159 | static inline To* CheckedCast(void *from) { |
| 160 | uintptr_t temp = reinterpret_cast<uintptr_t>(from); |
| 161 | ASSERT(temp % sizeof(To) == 0); |
| 162 | return reinterpret_cast<To*>(temp); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static MaybeObject* PerformCompare(const BitmaskCompareDescriptor& descriptor, |
| 167 | char* ptr, |
| 168 | Heap* heap) { |
| 169 | uint32_t bitmask = descriptor.bitmask; |
| 170 | uint32_t compare_value = descriptor.compare_value; |
| 171 | uint32_t value; |
| 172 | switch (descriptor.size) { |
| 173 | case 1: |
| 174 | value = static_cast<uint32_t>(*CheckedCast<uint8_t>(ptr)); |
| 175 | compare_value &= 0xff; |
| 176 | bitmask &= 0xff; |
| 177 | break; |
| 178 | case 2: |
| 179 | value = static_cast<uint32_t>(*CheckedCast<uint16_t>(ptr)); |
| 180 | compare_value &= 0xffff; |
| 181 | bitmask &= 0xffff; |
| 182 | break; |
| 183 | case 4: |
| 184 | value = *CheckedCast<uint32_t>(ptr); |
| 185 | break; |
| 186 | default: |
| 187 | UNREACHABLE(); |
| 188 | return NULL; |
| 189 | } |
| 190 | return heap->ToBoolean((bitmask & value) == (bitmask & compare_value)); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | static MaybeObject* PerformCompare(const PointerCompareDescriptor& descriptor, |
| 195 | char* ptr, |
| 196 | Heap* heap) { |
| 197 | uintptr_t compare_value = |
| 198 | reinterpret_cast<uintptr_t>(descriptor.compare_value); |
| 199 | uintptr_t value = *CheckedCast<uintptr_t>(ptr); |
| 200 | return heap->ToBoolean(compare_value == value); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | static MaybeObject* GetPrimitiveValue( |
| 205 | const PrimitiveValueDescriptor& descriptor, |
| 206 | char* ptr, |
| 207 | Heap* heap) { |
| 208 | int32_t int32_value = 0; |
| 209 | switch (descriptor.data_type) { |
| 210 | case kDescriptorInt8Type: |
| 211 | int32_value = *CheckedCast<int8_t>(ptr); |
| 212 | break; |
| 213 | case kDescriptorUint8Type: |
| 214 | int32_value = *CheckedCast<uint8_t>(ptr); |
| 215 | break; |
| 216 | case kDescriptorInt16Type: |
| 217 | int32_value = *CheckedCast<int16_t>(ptr); |
| 218 | break; |
| 219 | case kDescriptorUint16Type: |
| 220 | int32_value = *CheckedCast<uint16_t>(ptr); |
| 221 | break; |
| 222 | case kDescriptorInt32Type: |
| 223 | int32_value = *CheckedCast<int32_t>(ptr); |
| 224 | break; |
| 225 | case kDescriptorUint32Type: { |
| 226 | uint32_t value = *CheckedCast<uint32_t>(ptr); |
| 227 | return heap->NumberFromUint32(value); |
| 228 | } |
| 229 | case kDescriptorBoolType: { |
| 230 | uint8_t byte = *CheckedCast<uint8_t>(ptr); |
| 231 | return heap->ToBoolean(byte & (0x1 << descriptor.bool_offset)); |
| 232 | } |
| 233 | case kDescriptorFloatType: { |
| 234 | float value = *CheckedCast<float>(ptr); |
| 235 | return heap->NumberFromDouble(value); |
| 236 | } |
| 237 | case kDescriptorDoubleType: { |
| 238 | double value = *CheckedCast<double>(ptr); |
| 239 | return heap->NumberFromDouble(value); |
| 240 | } |
| 241 | } |
| 242 | return heap->NumberFromInt32(int32_value); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | static MaybeObject* GetDeclaredAccessorProperty(Object* receiver, |
| 247 | DeclaredAccessorInfo* info, |
| 248 | Isolate* isolate) { |
| 249 | char* current = reinterpret_cast<char*>(receiver); |
| 250 | DeclaredAccessorDescriptorIterator iterator(info->descriptor()); |
| 251 | while (true) { |
| 252 | const DeclaredAccessorDescriptorData* data = iterator.Next(); |
| 253 | switch (data->type) { |
| 254 | case kDescriptorReturnObject: { |
| 255 | ASSERT(iterator.Complete()); |
| 256 | current = *CheckedCast<char*>(current); |
| 257 | return *CheckedCast<Object*>(current); |
| 258 | } |
| 259 | case kDescriptorPointerDereference: |
| 260 | ASSERT(!iterator.Complete()); |
| 261 | current = *reinterpret_cast<char**>(current); |
| 262 | break; |
| 263 | case kDescriptorPointerShift: |
| 264 | ASSERT(!iterator.Complete()); |
| 265 | current += data->pointer_shift_descriptor.byte_offset; |
| 266 | break; |
| 267 | case kDescriptorObjectDereference: { |
| 268 | ASSERT(!iterator.Complete()); |
| 269 | Object* object = CheckedCast<Object>(current); |
| 270 | int field = data->object_dereference_descriptor.internal_field; |
| 271 | Object* smi = JSObject::cast(object)->GetInternalField(field); |
| 272 | ASSERT(smi->IsSmi()); |
| 273 | current = reinterpret_cast<char*>(smi); |
| 274 | break; |
| 275 | } |
| 276 | case kDescriptorBitmaskCompare: |
| 277 | ASSERT(iterator.Complete()); |
| 278 | return PerformCompare(data->bitmask_compare_descriptor, |
| 279 | current, |
| 280 | isolate->heap()); |
| 281 | case kDescriptorPointerCompare: |
| 282 | ASSERT(iterator.Complete()); |
| 283 | return PerformCompare(data->pointer_compare_descriptor, |
| 284 | current, |
| 285 | isolate->heap()); |
| 286 | case kDescriptorPrimitiveValue: |
| 287 | ASSERT(iterator.Complete()); |
| 288 | return GetPrimitiveValue(data->primitive_value_descriptor, |
| 289 | current, |
| 290 | isolate->heap()); |
| 291 | } |
| 292 | } |
| 293 | UNREACHABLE(); |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 298 | MaybeObject* JSObject::GetPropertyWithCallback(Object* receiver, |
| 299 | Object* structure, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 300 | Name* name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 301 | Isolate* isolate = name->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | // To accommodate both the old and the new api we switch on the |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 303 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 305 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 306 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 307 | reinterpret_cast<AccessorDescriptor*>( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 308 | Foreign::cast(structure)->foreign_address()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 309 | MaybeObject* value = (callback->getter)(receiver, callback->data); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 310 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | return value; |
| 312 | } |
| 313 | |
| 314 | // api style callbacks. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 315 | if (structure->IsAccessorInfo()) { |
| 316 | if (!AccessorInfo::cast(structure)->IsCompatibleReceiver(receiver)) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 317 | Handle<Object> name_handle(name, isolate); |
| 318 | Handle<Object> receiver_handle(receiver, isolate); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 319 | Handle<Object> args[2] = { name_handle, receiver_handle }; |
| 320 | Handle<Object> error = |
| 321 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 322 | HandleVector(args, |
| 323 | ARRAY_SIZE(args))); |
| 324 | return isolate->Throw(*error); |
| 325 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 326 | // TODO(rossberg): Handling symbols in the API requires changing the API, |
| 327 | // so we do not support it for now. |
| 328 | if (name->IsSymbol()) return isolate->heap()->undefined_value(); |
| 329 | if (structure->IsDeclaredAccessorInfo()) { |
| 330 | return GetDeclaredAccessorProperty(receiver, |
| 331 | DeclaredAccessorInfo::cast(structure), |
| 332 | isolate); |
| 333 | } |
| 334 | ExecutableAccessorInfo* data = ExecutableAccessorInfo::cast(structure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | Object* fun_obj = data->getter(); |
| 336 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 337 | if (call_fun == NULL) return isolate->heap()->undefined_value(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 338 | HandleScope scope(isolate); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 339 | JSObject* self = JSObject::cast(receiver); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 340 | Handle<String> key(String::cast(name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 341 | LOG(isolate, ApiNamedPropertyAccess("load", self, name)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 342 | CustomArguments args(isolate, data->data(), self, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 343 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | v8::Handle<v8::Value> result; |
| 345 | { |
| 346 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 347 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 348 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 349 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 351 | if (result.IsEmpty()) { |
| 352 | return isolate->heap()->undefined_value(); |
| 353 | } |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 354 | Object* return_value = *v8::Utils::OpenHandle(*result); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 355 | return_value->VerifyApiCallResultType(); |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 356 | return return_value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // __defineGetter__ callback |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 360 | if (structure->IsAccessorPair()) { |
| 361 | Object* getter = AccessorPair::cast(structure)->getter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 362 | if (getter->IsSpecFunction()) { |
| 363 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 364 | return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | } |
| 366 | // Getter is not a function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 367 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | UNREACHABLE(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 371 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 375 | MaybeObject* JSProxy::GetPropertyWithHandler(Object* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 376 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 377 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 378 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 379 | Handle<Object> receiver(receiver_raw, isolate); |
| 380 | Handle<Object> name(name_raw, isolate); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 381 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 382 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 383 | if (name->IsSymbol()) return isolate->heap()->undefined_value(); |
| 384 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 385 | Handle<Object> args[] = { receiver, name }; |
| 386 | Handle<Object> result = CallTrap( |
| 387 | "get", isolate->derived_get_trap(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 388 | if (isolate->has_pending_exception()) return Failure::Exception(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 389 | |
| 390 | return *result; |
| 391 | } |
| 392 | |
| 393 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 394 | Handle<Object> Object::GetProperty(Handle<Object> object, Handle<Name> name) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 395 | // TODO(rossberg): The index test should not be here but in the GetProperty |
| 396 | // method (or somewhere else entirely). Needs more global clean-up. |
| 397 | uint32_t index; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 398 | if (name->AsArrayIndex(&index)) |
| 399 | return GetElement(object, index); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 400 | Isolate* isolate = object->IsHeapObject() |
| 401 | ? Handle<HeapObject>::cast(object)->GetIsolate() |
| 402 | : Isolate::Current(); |
| 403 | CALL_HEAP_FUNCTION(isolate, object->GetProperty(*name), Object); |
| 404 | } |
| 405 | |
| 406 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 407 | Handle<Object> Object::GetElement(Handle<Object> object, uint32_t index) { |
| 408 | Isolate* isolate = object->IsHeapObject() |
| 409 | ? Handle<HeapObject>::cast(object)->GetIsolate() |
| 410 | : Isolate::Current(); |
| 411 | CALL_HEAP_FUNCTION(isolate, object->GetElement(index), Object); |
| 412 | } |
| 413 | |
| 414 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 415 | MaybeObject* JSProxy::GetElementWithHandler(Object* receiver, |
| 416 | uint32_t index) { |
| 417 | String* name; |
| 418 | MaybeObject* maybe = GetHeap()->Uint32ToString(index); |
| 419 | if (!maybe->To<String>(&name)) return maybe; |
| 420 | return GetPropertyWithHandler(receiver, name); |
| 421 | } |
| 422 | |
| 423 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 424 | MaybeObject* JSProxy::SetElementWithHandler(JSReceiver* receiver, |
| 425 | uint32_t index, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 426 | Object* value, |
| 427 | StrictModeFlag strict_mode) { |
| 428 | String* name; |
| 429 | MaybeObject* maybe = GetHeap()->Uint32ToString(index); |
| 430 | if (!maybe->To<String>(&name)) return maybe; |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 431 | return SetPropertyWithHandler(receiver, name, value, NONE, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
| 435 | bool JSProxy::HasElementWithHandler(uint32_t index) { |
| 436 | String* name; |
| 437 | MaybeObject* maybe = GetHeap()->Uint32ToString(index); |
| 438 | if (!maybe->To<String>(&name)) return maybe; |
| 439 | return HasPropertyWithHandler(name); |
| 440 | } |
| 441 | |
| 442 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 443 | MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 444 | JSReceiver* getter) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 445 | Isolate* isolate = getter->GetIsolate(); |
| 446 | HandleScope scope(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 447 | Handle<JSReceiver> fun(getter); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 448 | Handle<Object> self(receiver, isolate); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 449 | #ifdef ENABLE_DEBUGGER_SUPPORT |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 450 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 451 | // Handle stepping into a getter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 452 | // TODO(rossberg): should this apply to getters that are function proxies? |
| 453 | if (debug->StepInActive() && fun->IsJSFunction()) { |
| 454 | debug->HandleStepIn( |
| 455 | Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 456 | } |
| 457 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 458 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 459 | bool has_pending_exception; |
| 460 | Handle<Object> result = |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 461 | Execution::Call(fun, self, 0, NULL, &has_pending_exception, true); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 462 | // Check for pending exception and return the result. |
| 463 | if (has_pending_exception) return Failure::Exception(); |
| 464 | return *result; |
| 465 | } |
| 466 | |
| 467 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | // Only deal with CALLBACKS and INTERCEPTOR |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 469 | MaybeObject* JSObject::GetPropertyWithFailedAccessCheck( |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 470 | Object* receiver, |
| 471 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 472 | Name* name, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 473 | PropertyAttributes* attributes) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 474 | if (result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | switch (result->type()) { |
| 476 | case CALLBACKS: { |
| 477 | // Only allow API accessors. |
| 478 | Object* obj = result->GetCallbackObject(); |
| 479 | if (obj->IsAccessorInfo()) { |
| 480 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 481 | if (info->all_can_read()) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 482 | *attributes = result->GetAttributes(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 483 | return result->holder()->GetPropertyWithCallback( |
| 484 | receiver, result->GetCallbackObject(), name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | case NORMAL: |
| 490 | case FIELD: |
| 491 | case CONSTANT_FUNCTION: { |
| 492 | // Search ALL_CAN_READ accessors in prototype chain. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 493 | LookupResult r(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 495 | if (r.IsProperty()) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 496 | return GetPropertyWithFailedAccessCheck(receiver, |
| 497 | &r, |
| 498 | name, |
| 499 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 500 | } |
| 501 | break; |
| 502 | } |
| 503 | case INTERCEPTOR: { |
| 504 | // If the object has an interceptor, try real named properties. |
| 505 | // No access check in GetPropertyAttributeWithInterceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 506 | LookupResult r(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 507 | result->holder()->LookupRealNamedProperty(name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 508 | if (r.IsProperty()) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 509 | return GetPropertyWithFailedAccessCheck(receiver, |
| 510 | &r, |
| 511 | name, |
| 512 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | } |
ager@chromium.org | 9ed6c32 | 2010-02-19 12:24:05 +0000 | [diff] [blame] | 514 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 515 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 516 | default: |
| 517 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 521 | // No accessible property found. |
| 522 | *attributes = ABSENT; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 523 | Heap* heap = name->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 524 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_GET); |
| 525 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 529 | PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck( |
| 530 | Object* receiver, |
| 531 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 532 | Name* name, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 533 | bool continue_search) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 534 | if (result->IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 535 | switch (result->type()) { |
| 536 | case CALLBACKS: { |
| 537 | // Only allow API accessors. |
| 538 | Object* obj = result->GetCallbackObject(); |
| 539 | if (obj->IsAccessorInfo()) { |
| 540 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 541 | if (info->all_can_read()) { |
| 542 | return result->GetAttributes(); |
| 543 | } |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | case NORMAL: |
| 549 | case FIELD: |
| 550 | case CONSTANT_FUNCTION: { |
| 551 | if (!continue_search) break; |
| 552 | // Search ALL_CAN_READ accessors in prototype chain. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 553 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 554 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 555 | if (r.IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 556 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 557 | &r, |
| 558 | name, |
| 559 | continue_search); |
| 560 | } |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | case INTERCEPTOR: { |
| 565 | // If the object has an interceptor, try real named properties. |
| 566 | // No access check in GetPropertyAttributeWithInterceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 567 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 568 | if (continue_search) { |
| 569 | result->holder()->LookupRealNamedProperty(name, &r); |
| 570 | } else { |
| 571 | result->holder()->LocalLookupRealNamedProperty(name, &r); |
| 572 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 573 | if (!r.IsFound()) break; |
| 574 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 575 | &r, |
| 576 | name, |
| 577 | continue_search); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 578 | } |
| 579 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 580 | case HANDLER: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 581 | case TRANSITION: |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 582 | case NONEXISTENT: |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 583 | UNREACHABLE(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 587 | GetIsolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 588 | return ABSENT; |
| 589 | } |
| 590 | |
| 591 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 592 | Object* JSObject::GetNormalizedProperty(LookupResult* result) { |
| 593 | ASSERT(!HasFastProperties()); |
| 594 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 595 | if (IsGlobalObject()) { |
| 596 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 597 | } |
| 598 | ASSERT(!value->IsJSGlobalPropertyCell()); |
| 599 | return value; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | Object* JSObject::SetNormalizedProperty(LookupResult* result, Object* value) { |
| 604 | ASSERT(!HasFastProperties()); |
| 605 | if (IsGlobalObject()) { |
| 606 | JSGlobalPropertyCell* cell = |
| 607 | JSGlobalPropertyCell::cast( |
| 608 | property_dictionary()->ValueAt(result->GetDictionaryEntry())); |
| 609 | cell->set_value(value); |
| 610 | } else { |
| 611 | property_dictionary()->ValueAtPut(result->GetDictionaryEntry(), value); |
| 612 | } |
| 613 | return value; |
| 614 | } |
| 615 | |
| 616 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 617 | Handle<Object> JSObject::SetNormalizedProperty(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 618 | Handle<Name> key, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 619 | Handle<Object> value, |
| 620 | PropertyDetails details) { |
| 621 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 622 | object->SetNormalizedProperty(*key, *value, details), |
| 623 | Object); |
| 624 | } |
| 625 | |
| 626 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 627 | MaybeObject* JSObject::SetNormalizedProperty(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 628 | Object* value, |
| 629 | PropertyDetails details) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 630 | ASSERT(!HasFastProperties()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 631 | int entry = property_dictionary()->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 632 | if (entry == NameDictionary::kNotFound) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 633 | Object* store_value = value; |
| 634 | if (IsGlobalObject()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 635 | Heap* heap = name->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 636 | MaybeObject* maybe_store_value = |
| 637 | heap->AllocateJSGlobalPropertyCell(value); |
| 638 | if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 639 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 640 | Object* dict; |
| 641 | { MaybeObject* maybe_dict = |
| 642 | property_dictionary()->Add(name, store_value, details); |
| 643 | if (!maybe_dict->ToObject(&dict)) return maybe_dict; |
| 644 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 645 | set_properties(NameDictionary::cast(dict)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 646 | return value; |
| 647 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 648 | |
| 649 | PropertyDetails original_details = property_dictionary()->DetailsAt(entry); |
| 650 | int enumeration_index; |
| 651 | // Preserve the enumeration index unless the property was deleted. |
| 652 | if (original_details.IsDeleted()) { |
| 653 | enumeration_index = property_dictionary()->NextEnumerationIndex(); |
| 654 | property_dictionary()->SetNextEnumerationIndex(enumeration_index + 1); |
| 655 | } else { |
| 656 | enumeration_index = original_details.dictionary_index(); |
| 657 | ASSERT(enumeration_index > 0); |
| 658 | } |
| 659 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 660 | details = PropertyDetails( |
| 661 | details.attributes(), details.type(), enumeration_index); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 662 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 663 | if (IsGlobalObject()) { |
| 664 | JSGlobalPropertyCell* cell = |
| 665 | JSGlobalPropertyCell::cast(property_dictionary()->ValueAt(entry)); |
| 666 | cell->set_value(value); |
| 667 | // Please note we have to update the property details. |
| 668 | property_dictionary()->DetailsAtPut(entry, details); |
| 669 | } else { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 670 | property_dictionary()->SetEntry(entry, name, value, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 671 | } |
| 672 | return value; |
| 673 | } |
| 674 | |
| 675 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 676 | MaybeObject* JSObject::DeleteNormalizedProperty(Name* name, DeleteMode mode) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 677 | ASSERT(!HasFastProperties()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 678 | NameDictionary* dictionary = property_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 679 | int entry = dictionary->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 680 | if (entry != NameDictionary::kNotFound) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 681 | // If we have a global object set the cell to the hole. |
| 682 | if (IsGlobalObject()) { |
| 683 | PropertyDetails details = dictionary->DetailsAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 684 | if (details.IsDontDelete()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 685 | if (mode != FORCE_DELETION) return GetHeap()->false_value(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 686 | // When forced to delete global properties, we have to make a |
| 687 | // map change to invalidate any ICs that think they can load |
| 688 | // from the DontDelete cell without checking if it contains |
| 689 | // the hole value. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 690 | Map* new_map; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 691 | MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); |
| 692 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 693 | |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 694 | ASSERT(new_map->is_dictionary_map()); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 695 | set_map(new_map); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 696 | } |
| 697 | JSGlobalPropertyCell* cell = |
| 698 | JSGlobalPropertyCell::cast(dictionary->ValueAt(entry)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 699 | cell->set_value(cell->GetHeap()->the_hole_value()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 700 | dictionary->DetailsAtPut(entry, details.AsDeleted()); |
| 701 | } else { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 702 | Object* deleted = dictionary->DeleteProperty(entry, mode); |
| 703 | if (deleted == GetHeap()->true_value()) { |
| 704 | FixedArray* new_properties = NULL; |
| 705 | MaybeObject* maybe_properties = dictionary->Shrink(name); |
| 706 | if (!maybe_properties->To(&new_properties)) { |
| 707 | return maybe_properties; |
| 708 | } |
| 709 | set_properties(new_properties); |
| 710 | } |
| 711 | return deleted; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 714 | return GetHeap()->true_value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 718 | bool JSObject::IsDirty() { |
| 719 | Object* cons_obj = map()->constructor(); |
| 720 | if (!cons_obj->IsJSFunction()) |
| 721 | return true; |
| 722 | JSFunction* fun = JSFunction::cast(cons_obj); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 723 | if (!fun->shared()->IsApiFunction()) |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 724 | return true; |
| 725 | // If the object is fully fast case and has the same map it was |
| 726 | // created with then no changes can have been made to it. |
| 727 | return map() != fun->initial_map() |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 728 | || !HasFastObjectElements() |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 729 | || !HasFastProperties(); |
| 730 | } |
| 731 | |
| 732 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 733 | Handle<Object> Object::GetProperty(Handle<Object> object, |
| 734 | Handle<Object> receiver, |
| 735 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 736 | Handle<Name> key, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 737 | PropertyAttributes* attributes) { |
| 738 | Isolate* isolate = object->IsHeapObject() |
| 739 | ? Handle<HeapObject>::cast(object)->GetIsolate() |
| 740 | : Isolate::Current(); |
| 741 | CALL_HEAP_FUNCTION( |
| 742 | isolate, |
| 743 | object->GetProperty(*receiver, result, *key, attributes), |
| 744 | Object); |
| 745 | } |
| 746 | |
| 747 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 748 | MaybeObject* Object::GetPropertyOrFail(Handle<Object> object, |
| 749 | Handle<Object> receiver, |
| 750 | LookupResult* result, |
| 751 | Handle<Name> key, |
| 752 | PropertyAttributes* attributes) { |
| 753 | Isolate* isolate = object->IsHeapObject() |
| 754 | ? Handle<HeapObject>::cast(object)->GetIsolate() |
| 755 | : Isolate::Current(); |
| 756 | CALL_HEAP_FUNCTION_PASS_EXCEPTION( |
| 757 | isolate, |
| 758 | object->GetProperty(*receiver, result, *key, attributes)); |
| 759 | } |
| 760 | |
| 761 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 762 | MaybeObject* Object::GetProperty(Object* receiver, |
| 763 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 764 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 765 | PropertyAttributes* attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 766 | // Make sure that the top context does not change when doing |
| 767 | // callbacks or interceptor calls. |
| 768 | AssertNoContextChange ncc; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 769 | Isolate* isolate = name->GetIsolate(); |
| 770 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 771 | |
| 772 | // Traverse the prototype chain from the current object (this) to |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 773 | // the holder and check for access rights. This avoids traversing the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 774 | // objects more than once in case of interceptors, because the |
| 775 | // holder will always be the interceptor holder and the search may |
| 776 | // only continue with a current object just after the interceptor |
| 777 | // holder in the prototype chain. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 778 | // Proxy handlers do not use the proxy's prototype, so we can skip this. |
| 779 | if (!result->IsHandler()) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 780 | Object* last = result->IsProperty() |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 781 | ? result->holder() |
| 782 | : Object::cast(heap->null_value()); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 783 | ASSERT(this != this->GetPrototype(isolate)); |
| 784 | for (Object* current = this; |
| 785 | true; |
| 786 | current = current->GetPrototype(isolate)) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 787 | if (current->IsAccessCheckNeeded()) { |
| 788 | // Check if we're allowed to read from the current object. Note |
| 789 | // that even though we may not actually end up loading the named |
| 790 | // property from the current object, we still check that we have |
| 791 | // access to it. |
| 792 | JSObject* checked = JSObject::cast(current); |
| 793 | if (!heap->isolate()->MayNamedAccess(checked, name, v8::ACCESS_GET)) { |
| 794 | return checked->GetPropertyWithFailedAccessCheck(receiver, |
| 795 | result, |
| 796 | name, |
| 797 | attributes); |
| 798 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 800 | // Stop traversing the chain once we reach the last object in the |
| 801 | // chain; either the holder of the result or null in case of an |
| 802 | // absent property. |
| 803 | if (current == last) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 804 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | } |
| 806 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 807 | if (!result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 808 | *attributes = ABSENT; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 809 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | } |
| 811 | *attributes = result->GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 812 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | switch (result->type()) { |
| 814 | case NORMAL: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 815 | value = result->holder()->GetNormalizedProperty(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 816 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 817 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 818 | case FIELD: { |
| 819 | MaybeObject* maybe_result = result->holder()->FastPropertyAt( |
| 820 | result->representation(), |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 821 | result->GetFieldIndex().field_index()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 822 | if (!maybe_result->To(&value)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 823 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 824 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 825 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 826 | case CONSTANT_FUNCTION: |
| 827 | return result->GetConstantFunction(); |
| 828 | case CALLBACKS: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 829 | return result->holder()->GetPropertyWithCallback( |
| 830 | receiver, result->GetCallbackObject(), name); |
| 831 | case HANDLER: |
| 832 | return result->proxy()->GetPropertyWithHandler(receiver, name); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 833 | case INTERCEPTOR: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 834 | return result->holder()->GetPropertyWithInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 835 | receiver, name, attributes); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 836 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 837 | case NONEXISTENT: |
| 838 | UNREACHABLE(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 839 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 841 | UNREACHABLE(); |
| 842 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 846 | MaybeObject* Object::GetElementWithReceiver(Object* receiver, uint32_t index) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 847 | Isolate* isolate = IsSmi() |
| 848 | ? Isolate::Current() |
| 849 | : HeapObject::cast(this)->GetIsolate(); |
| 850 | Heap* heap = isolate->heap(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 851 | Object* holder = this; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 852 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 853 | // Iterate up the prototype chain until an element is found or the null |
| 854 | // prototype is encountered. |
| 855 | for (holder = this; |
| 856 | holder != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 857 | holder = holder->GetPrototype(isolate)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 858 | if (!holder->IsJSObject()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 859 | Context* native_context = isolate->context()->native_context(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 860 | if (holder->IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 861 | holder = native_context->number_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 862 | } else if (holder->IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 863 | holder = native_context->string_function()->instance_prototype(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 864 | } else if (holder->IsSymbol()) { |
| 865 | holder = native_context->symbol_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 866 | } else if (holder->IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 867 | holder = native_context->boolean_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 868 | } else if (holder->IsJSProxy()) { |
| 869 | return JSProxy::cast(holder)->GetElementWithHandler(receiver, index); |
| 870 | } else { |
| 871 | // Undefined and null have no indexed properties. |
| 872 | ASSERT(holder->IsUndefined() || holder->IsNull()); |
| 873 | return heap->undefined_value(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
| 877 | // Inline the case for JSObjects. Doing so significantly improves the |
| 878 | // performance of fetching elements where checking the prototype chain is |
| 879 | // necessary. |
| 880 | JSObject* js_object = JSObject::cast(holder); |
| 881 | |
| 882 | // Check access rights if needed. |
| 883 | if (js_object->IsAccessCheckNeeded()) { |
| 884 | Isolate* isolate = heap->isolate(); |
| 885 | if (!isolate->MayIndexedAccess(js_object, index, v8::ACCESS_GET)) { |
| 886 | isolate->ReportFailedAccessCheck(js_object, v8::ACCESS_GET); |
| 887 | return heap->undefined_value(); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if (js_object->HasIndexedInterceptor()) { |
| 892 | return js_object->GetElementWithInterceptor(receiver, index); |
| 893 | } |
| 894 | |
| 895 | if (js_object->elements() != heap->empty_fixed_array()) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 896 | MaybeObject* result = js_object->GetElementsAccessor()->Get( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 897 | receiver, js_object, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 898 | if (result != heap->the_hole_value()) return result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 899 | } |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 900 | } |
| 901 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 902 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 906 | Object* Object::GetPrototype(Isolate* isolate) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 907 | if (IsSmi()) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 908 | Context* context = isolate->context()->native_context(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 909 | return context->number_function()->instance_prototype(); |
| 910 | } |
| 911 | |
| 912 | HeapObject* heap_object = HeapObject::cast(this); |
| 913 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 914 | // The object is either a number, a string, a boolean, |
| 915 | // a real JS object, or a Harmony proxy. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 916 | if (heap_object->IsJSReceiver()) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 917 | return heap_object->map()->prototype(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 918 | } |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 919 | Context* context = isolate->context()->native_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 921 | if (heap_object->IsHeapNumber()) { |
| 922 | return context->number_function()->instance_prototype(); |
| 923 | } |
| 924 | if (heap_object->IsString()) { |
| 925 | return context->string_function()->instance_prototype(); |
| 926 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 927 | if (heap_object->IsSymbol()) { |
| 928 | return context->symbol_function()->instance_prototype(); |
| 929 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 930 | if (heap_object->IsBoolean()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 931 | return context->boolean_function()->instance_prototype(); |
| 932 | } else { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 933 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 938 | MaybeObject* Object::GetHash(CreationFlag flag) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 939 | // The object is either a number, a name, an odd-ball, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 940 | // a real JS object, or a Harmony proxy. |
| 941 | if (IsNumber()) { |
| 942 | uint32_t hash = ComputeLongHash(double_to_uint64(Number())); |
| 943 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 944 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 945 | if (IsName()) { |
| 946 | uint32_t hash = Name::cast(this)->Hash(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 947 | return Smi::FromInt(hash); |
| 948 | } |
| 949 | if (IsOddball()) { |
| 950 | uint32_t hash = Oddball::cast(this)->to_string()->Hash(); |
| 951 | return Smi::FromInt(hash); |
| 952 | } |
| 953 | if (IsJSReceiver()) { |
| 954 | return JSReceiver::cast(this)->GetIdentityHash(flag); |
| 955 | } |
| 956 | |
| 957 | UNREACHABLE(); |
| 958 | return Smi::FromInt(0); |
| 959 | } |
| 960 | |
| 961 | |
| 962 | bool Object::SameValue(Object* other) { |
| 963 | if (other == this) return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 964 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 965 | // The object is either a number, a name, an odd-ball, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 966 | // a real JS object, or a Harmony proxy. |
| 967 | if (IsNumber() && other->IsNumber()) { |
| 968 | double this_value = Number(); |
| 969 | double other_value = other->Number(); |
| 970 | return (this_value == other_value) || |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 971 | (std::isnan(this_value) && std::isnan(other_value)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 972 | } |
| 973 | if (IsString() && other->IsString()) { |
| 974 | return String::cast(this)->Equals(String::cast(other)); |
| 975 | } |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 980 | void Object::ShortPrint(FILE* out) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | HeapStringAllocator allocator; |
| 982 | StringStream accumulator(&allocator); |
| 983 | ShortPrint(&accumulator); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 984 | accumulator.OutputToFile(out); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | |
| 988 | void Object::ShortPrint(StringStream* accumulator) { |
| 989 | if (IsSmi()) { |
| 990 | Smi::cast(this)->SmiPrint(accumulator); |
| 991 | } else if (IsFailure()) { |
| 992 | Failure::cast(this)->FailurePrint(accumulator); |
| 993 | } else { |
| 994 | HeapObject::cast(this)->HeapObjectShortPrint(accumulator); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 999 | void Smi::SmiPrint(FILE* out) { |
| 1000 | PrintF(out, "%d", value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | void Smi::SmiPrint(StringStream* accumulator) { |
| 1005 | accumulator->Add("%d", value()); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | void Failure::FailurePrint(StringStream* accumulator) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1010 | accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1014 | void Failure::FailurePrint(FILE* out) { |
| 1015 | PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 1020 | // English? Returns false for non-ASCII or words that don't start with |
| 1021 | // a capital letter. The a/an rule follows pronunciation in English. |
| 1022 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 1023 | // you speak a dialect you may well say "an 'istoric occasion". |
| 1024 | static bool AnWord(String* str) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1025 | if (str->length() == 0) return false; // A nothing. |
| 1026 | int c0 = str->Get(0); |
| 1027 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1028 | if (c0 == 'U') { |
| 1029 | if (c1 > 'Z') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1030 | return true; // An Umpire, but a UTF8String, a U. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1033 | return true; // An Ape, an ABCBook. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1034 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 1035 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 1036 | c0 == 'S' || c0 == 'X')) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1037 | return true; // An MP3File, an M. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1038 | } |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
| 1042 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1043 | MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1044 | #ifdef DEBUG |
| 1045 | // Do not attempt to flatten in debug mode when allocation is not |
| 1046 | // allowed. This is to avoid an assertion failure when allocating. |
| 1047 | // Flattening strings is the only case where we always allow |
| 1048 | // allocation because no GC is performed if the allocation fails. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1049 | if (!HEAP->IsAllocationAllowed()) return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | #endif |
| 1051 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1052 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1053 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | case kConsStringTag: { |
| 1055 | ConsString* cs = ConsString::cast(this); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1056 | if (cs->second()->length() == 0) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1057 | return cs->first(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1058 | } |
| 1059 | // There's little point in putting the flat string in new space if the |
| 1060 | // cons string is in old space. It can never get GCed until there is |
| 1061 | // an old space GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1062 | PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1063 | int len = length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1064 | Object* object; |
| 1065 | String* result; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1066 | if (IsOneByteRepresentation()) { |
| 1067 | { MaybeObject* maybe_object = |
| 1068 | heap->AllocateRawOneByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1069 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1070 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1071 | result = String::cast(object); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1072 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1073 | int first_length = first->length(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 1074 | uint8_t* dest = SeqOneByteString::cast(result)->GetChars(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1075 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1076 | String* second = cs->second(); |
| 1077 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1078 | dest + first_length, |
| 1079 | 0, |
| 1080 | len - first_length); |
| 1081 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1082 | { MaybeObject* maybe_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1083 | heap->AllocateRawTwoByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1084 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1085 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1086 | result = String::cast(object); |
| 1087 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1088 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1089 | int first_length = first->length(); |
| 1090 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1091 | String* second = cs->second(); |
| 1092 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1093 | dest + first_length, |
| 1094 | 0, |
| 1095 | len - first_length); |
| 1096 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | cs->set_first(result); |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 1098 | cs->set_second(heap->empty_string(), SKIP_WRITE_BARRIER); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1099 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1100 | } |
| 1101 | default: |
| 1102 | return this; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1107 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1108 | // Externalizing twice leaks the external resource, so it's |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1109 | // prohibited by the API. |
| 1110 | ASSERT(!this->IsExternalString()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1111 | #ifdef DEBUG |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1112 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1113 | // Assert that the resource and the string are equivalent. |
| 1114 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1115 | ScopedVector<uc16> smart_chars(this->length()); |
| 1116 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1117 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1118 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1119 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1120 | } |
| 1121 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1122 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1123 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1124 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1127 | bool is_ascii = this->IsOneByteRepresentation(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1128 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Morph the object to an external string by adjusting the map and |
| 1131 | // reinitializing the fields. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1132 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1133 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1134 | is_internalized |
| 1135 | ? (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1136 | ? heap->external_internalized_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1137 | : heap->external_internalized_string_map()) |
| 1138 | : (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1139 | ? heap->external_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1140 | : heap->external_string_map())); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1141 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1142 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1143 | is_internalized |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1144 | ? (is_ascii |
| 1145 | ? heap-> |
| 1146 | short_external_internalized_string_with_one_byte_data_map() |
| 1147 | : heap->short_external_internalized_string_map()) |
| 1148 | : (is_ascii |
| 1149 | ? heap->short_external_string_with_one_byte_data_map() |
| 1150 | : heap->short_external_string_map())); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1151 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1152 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 1153 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1154 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1155 | |
| 1156 | // Fill the remainder of the string with dead wood. |
| 1157 | int new_size = this->Size(); // Byte size of the external String object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1158 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1159 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1160 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1161 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1162 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { |
| 1168 | #ifdef DEBUG |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1169 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1170 | // Assert that the resource and the string are equivalent. |
| 1171 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1172 | ScopedVector<char> smart_chars(this->length()); |
| 1173 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1174 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1175 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1176 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1177 | } |
| 1178 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1179 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1180 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1181 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1184 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Morph the object to an external string by adjusting the map and |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1187 | // reinitializing the fields. Use short version if space is limited. |
| 1188 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1189 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1190 | is_internalized ? heap->external_ascii_internalized_string_map() |
| 1191 | : heap->external_ascii_string_map()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1192 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1193 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1194 | is_internalized ? heap->short_external_ascii_internalized_string_map() |
| 1195 | : heap->short_external_ascii_string_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1196 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1197 | ExternalAsciiString* self = ExternalAsciiString::cast(this); |
| 1198 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1199 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Fill the remainder of the string with dead wood. |
| 1202 | int new_size = this->Size(); // Byte size of the external String object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1203 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1204 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1205 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1206 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1207 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1212 | void String::StringShortPrint(StringStream* accumulator) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1213 | int len = length(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1214 | if (len > kMaxShortPrintLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1215 | accumulator->Add("<Very long string[%u]>", len); |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | if (!LooksValid()) { |
| 1220 | accumulator->Add("<Invalid String>"); |
| 1221 | return; |
| 1222 | } |
| 1223 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1224 | ConsStringIteratorOp op; |
| 1225 | StringCharacterStream stream(this, &op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | |
| 1227 | bool truncated = false; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1228 | if (len > kMaxShortPrintLength) { |
| 1229 | len = kMaxShortPrintLength; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | truncated = true; |
| 1231 | } |
| 1232 | bool ascii = true; |
| 1233 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1234 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | |
| 1236 | if (c < 32 || c >= 127) { |
| 1237 | ascii = false; |
| 1238 | } |
| 1239 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1240 | stream.Reset(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | if (ascii) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1242 | accumulator->Add("<String[%u]: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1243 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1244 | accumulator->Put(static_cast<char>(stream.GetNext())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1245 | } |
| 1246 | accumulator->Put('>'); |
| 1247 | } else { |
| 1248 | // Backslash indicates that the string contains control |
| 1249 | // characters and that backslashes are therefore escaped. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1250 | accumulator->Add("<String[%u]\\: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1251 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1252 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | if (c == '\n') { |
| 1254 | accumulator->Add("\\n"); |
| 1255 | } else if (c == '\r') { |
| 1256 | accumulator->Add("\\r"); |
| 1257 | } else if (c == '\\') { |
| 1258 | accumulator->Add("\\\\"); |
| 1259 | } else if (c < 32 || c > 126) { |
| 1260 | accumulator->Add("\\x%02x", c); |
| 1261 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1262 | accumulator->Put(static_cast<char>(c)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | if (truncated) { |
| 1266 | accumulator->Put('.'); |
| 1267 | accumulator->Put('.'); |
| 1268 | accumulator->Put('.'); |
| 1269 | } |
| 1270 | accumulator->Put('>'); |
| 1271 | } |
| 1272 | return; |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 1277 | switch (map()->instance_type()) { |
| 1278 | case JS_ARRAY_TYPE: { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1279 | double length = JSArray::cast(this)->length()->IsUndefined() |
| 1280 | ? 0 |
| 1281 | : JSArray::cast(this)->length()->Number(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1282 | accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1285 | case JS_WEAK_MAP_TYPE: { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1286 | accumulator->Add("<JS WeakMap>"); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1289 | case JS_REGEXP_TYPE: { |
| 1290 | accumulator->Add("<JS RegExp>"); |
| 1291 | break; |
| 1292 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | case JS_FUNCTION_TYPE: { |
| 1294 | Object* fun_name = JSFunction::cast(this)->shared()->name(); |
| 1295 | bool printed = false; |
| 1296 | if (fun_name->IsString()) { |
| 1297 | String* str = String::cast(fun_name); |
| 1298 | if (str->length() > 0) { |
| 1299 | accumulator->Add("<JS Function "); |
| 1300 | accumulator->Put(str); |
| 1301 | accumulator->Put('>'); |
| 1302 | printed = true; |
| 1303 | } |
| 1304 | } |
| 1305 | if (!printed) { |
| 1306 | accumulator->Add("<JS Function>"); |
| 1307 | } |
| 1308 | break; |
| 1309 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1310 | case JS_GENERATOR_OBJECT_TYPE: { |
| 1311 | accumulator->Add("<JS Generator>"); |
| 1312 | break; |
| 1313 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1314 | case JS_MODULE_TYPE: { |
| 1315 | accumulator->Add("<JS Module>"); |
| 1316 | break; |
| 1317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1318 | // All other JSObjects are rather similar to each other (JSObject, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1319 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1320 | default: { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1321 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1322 | Heap* heap = GetHeap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1323 | Object* constructor = map_of_this->constructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | bool printed = false; |
| 1325 | if (constructor->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1326 | !heap->Contains(HeapObject::cast(constructor))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1327 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 1328 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1329 | bool global_object = IsJSGlobalProxy(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1330 | if (constructor->IsJSFunction()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1331 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 1333 | } else { |
| 1334 | Object* constructor_name = |
| 1335 | JSFunction::cast(constructor)->shared()->name(); |
| 1336 | if (constructor_name->IsString()) { |
| 1337 | String* str = String::cast(constructor_name); |
| 1338 | if (str->length() > 0) { |
| 1339 | bool vowel = AnWord(str); |
| 1340 | accumulator->Add("<%sa%s ", |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1341 | global_object ? "Global Object: " : "", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1342 | vowel ? "n" : ""); |
| 1343 | accumulator->Put(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | printed = true; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | if (!printed) { |
| 1350 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 1351 | } |
| 1352 | } |
| 1353 | if (IsJSValue()) { |
| 1354 | accumulator->Add(" value = "); |
| 1355 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 1356 | } |
| 1357 | accumulator->Put('>'); |
| 1358 | break; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1364 | void JSObject::PrintElementsTransition( |
| 1365 | FILE* file, ElementsKind from_kind, FixedArrayBase* from_elements, |
| 1366 | ElementsKind to_kind, FixedArrayBase* to_elements) { |
| 1367 | if (from_kind != to_kind) { |
| 1368 | PrintF(file, "elements transition ["); |
| 1369 | PrintElementsKind(file, from_kind); |
| 1370 | PrintF(file, " -> "); |
| 1371 | PrintElementsKind(file, to_kind); |
| 1372 | PrintF(file, "] in "); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1373 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1374 | PrintF(file, " for "); |
| 1375 | ShortPrint(file); |
| 1376 | PrintF(file, " from "); |
| 1377 | from_elements->ShortPrint(file); |
| 1378 | PrintF(file, " to "); |
| 1379 | to_elements->ShortPrint(file); |
| 1380 | PrintF(file, "\n"); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1385 | void HeapObject::HeapObjectShortPrint(StringStream* accumulator) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1386 | Heap* heap = GetHeap(); |
| 1387 | if (!heap->Contains(this)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1388 | accumulator->Add("!!!INVALID POINTER!!!"); |
| 1389 | return; |
| 1390 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1391 | if (!heap->Contains(map())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | accumulator->Add("!!!INVALID MAP!!!"); |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | accumulator->Add("%p ", this); |
| 1397 | |
| 1398 | if (IsString()) { |
| 1399 | String::cast(this)->StringShortPrint(accumulator); |
| 1400 | return; |
| 1401 | } |
| 1402 | if (IsJSObject()) { |
| 1403 | JSObject::cast(this)->JSObjectShortPrint(accumulator); |
| 1404 | return; |
| 1405 | } |
| 1406 | switch (map()->instance_type()) { |
| 1407 | case MAP_TYPE: |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1408 | accumulator->Add("<Map(elements=%u)>", Map::cast(this)->elements_kind()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | case FIXED_ARRAY_TYPE: |
| 1411 | accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length()); |
| 1412 | break; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1413 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1414 | accumulator->Add("<FixedDoubleArray[%u]>", |
| 1415 | FixedDoubleArray::cast(this)->length()); |
| 1416 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1417 | case BYTE_ARRAY_TYPE: |
| 1418 | accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length()); |
| 1419 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1420 | case FREE_SPACE_TYPE: |
| 1421 | accumulator->Add("<FreeSpace[%u]>", FreeSpace::cast(this)->Size()); |
| 1422 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1423 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 1424 | accumulator->Add("<ExternalPixelArray[%u]>", |
| 1425 | ExternalPixelArray::cast(this)->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1426 | break; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1427 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1428 | accumulator->Add("<ExternalByteArray[%u]>", |
| 1429 | ExternalByteArray::cast(this)->length()); |
| 1430 | break; |
| 1431 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1432 | accumulator->Add("<ExternalUnsignedByteArray[%u]>", |
| 1433 | ExternalUnsignedByteArray::cast(this)->length()); |
| 1434 | break; |
| 1435 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1436 | accumulator->Add("<ExternalShortArray[%u]>", |
| 1437 | ExternalShortArray::cast(this)->length()); |
| 1438 | break; |
| 1439 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1440 | accumulator->Add("<ExternalUnsignedShortArray[%u]>", |
| 1441 | ExternalUnsignedShortArray::cast(this)->length()); |
| 1442 | break; |
| 1443 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1444 | accumulator->Add("<ExternalIntArray[%u]>", |
| 1445 | ExternalIntArray::cast(this)->length()); |
| 1446 | break; |
| 1447 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1448 | accumulator->Add("<ExternalUnsignedIntArray[%u]>", |
| 1449 | ExternalUnsignedIntArray::cast(this)->length()); |
| 1450 | break; |
| 1451 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 1452 | accumulator->Add("<ExternalFloatArray[%u]>", |
| 1453 | ExternalFloatArray::cast(this)->length()); |
| 1454 | break; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1455 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 1456 | accumulator->Add("<ExternalDoubleArray[%u]>", |
| 1457 | ExternalDoubleArray::cast(this)->length()); |
| 1458 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1459 | case SHARED_FUNCTION_INFO_TYPE: |
| 1460 | accumulator->Add("<SharedFunctionInfo>"); |
| 1461 | break; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1462 | case JS_MESSAGE_OBJECT_TYPE: |
| 1463 | accumulator->Add("<JSMessageObject>"); |
| 1464 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1466 | case NAME##_TYPE: \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1467 | accumulator->Put('<'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1468 | accumulator->Add(#Name); \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1469 | accumulator->Put('>'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1472 | #undef MAKE_STRUCT_CASE |
| 1473 | case CODE_TYPE: |
| 1474 | accumulator->Add("<Code>"); |
| 1475 | break; |
| 1476 | case ODDBALL_TYPE: { |
| 1477 | if (IsUndefined()) |
| 1478 | accumulator->Add("<undefined>"); |
| 1479 | else if (IsTheHole()) |
| 1480 | accumulator->Add("<the hole>"); |
| 1481 | else if (IsNull()) |
| 1482 | accumulator->Add("<null>"); |
| 1483 | else if (IsTrue()) |
| 1484 | accumulator->Add("<true>"); |
| 1485 | else if (IsFalse()) |
| 1486 | accumulator->Add("<false>"); |
| 1487 | else |
| 1488 | accumulator->Add("<Odd Oddball>"); |
| 1489 | break; |
| 1490 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1491 | case SYMBOL_TYPE: { |
| 1492 | Symbol* symbol = Symbol::cast(this); |
| 1493 | accumulator->Add("<Symbol: %d", symbol->Hash()); |
| 1494 | if (!symbol->name()->IsUndefined()) { |
| 1495 | accumulator->Add(" "); |
| 1496 | String::cast(symbol->name())->StringShortPrint(accumulator); |
| 1497 | } |
| 1498 | accumulator->Add(">"); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1499 | break; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1500 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1501 | case HEAP_NUMBER_TYPE: |
| 1502 | accumulator->Add("<Number: "); |
| 1503 | HeapNumber::cast(this)->HeapNumberPrint(accumulator); |
| 1504 | accumulator->Put('>'); |
| 1505 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1506 | case JS_PROXY_TYPE: |
| 1507 | accumulator->Add("<JSProxy>"); |
| 1508 | break; |
| 1509 | case JS_FUNCTION_PROXY_TYPE: |
| 1510 | accumulator->Add("<JSFunctionProxy>"); |
| 1511 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1512 | case FOREIGN_TYPE: |
| 1513 | accumulator->Add("<Foreign>"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1514 | break; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1515 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
| 1516 | accumulator->Add("Cell for "); |
| 1517 | JSGlobalPropertyCell::cast(this)->value()->ShortPrint(accumulator); |
| 1518 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1519 | default: |
| 1520 | accumulator->Add("<Other heap object (%d)>", map()->instance_type()); |
| 1521 | break; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1526 | void HeapObject::Iterate(ObjectVisitor* v) { |
| 1527 | // Handle header |
| 1528 | IteratePointer(v, kMapOffset); |
| 1529 | // Handle object body |
| 1530 | Map* m = map(); |
| 1531 | IterateBody(m->instance_type(), SizeFromMap(m), v); |
| 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 1536 | ObjectVisitor* v) { |
| 1537 | // Avoiding <Type>::cast(this) because it accesses the map pointer field. |
| 1538 | // During GC, the map pointer field is encoded. |
| 1539 | if (type < FIRST_NONSTRING_TYPE) { |
| 1540 | switch (type & kStringRepresentationMask) { |
| 1541 | case kSeqStringTag: |
| 1542 | break; |
| 1543 | case kConsStringTag: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1544 | ConsString::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1545 | break; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1546 | case kSlicedStringTag: |
| 1547 | SlicedString::BodyDescriptor::IterateBody(this, v); |
| 1548 | break; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1549 | case kExternalStringTag: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1550 | if ((type & kStringEncodingMask) == kOneByteStringTag) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1551 | reinterpret_cast<ExternalAsciiString*>(this)-> |
| 1552 | ExternalAsciiStringIterateBody(v); |
| 1553 | } else { |
| 1554 | reinterpret_cast<ExternalTwoByteString*>(this)-> |
| 1555 | ExternalTwoByteStringIterateBody(v); |
| 1556 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1557 | break; |
| 1558 | } |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | switch (type) { |
| 1563 | case FIXED_ARRAY_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1564 | FixedArray::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1565 | break; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1566 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1567 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1568 | case JS_OBJECT_TYPE: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1569 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1570 | case JS_GENERATOR_OBJECT_TYPE: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1571 | case JS_MODULE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1572 | case JS_VALUE_TYPE: |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1573 | case JS_DATE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1574 | case JS_ARRAY_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1575 | case JS_ARRAY_BUFFER_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1576 | case JS_TYPED_ARRAY_TYPE: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1577 | case JS_SET_TYPE: |
| 1578 | case JS_MAP_TYPE: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1579 | case JS_WEAK_MAP_TYPE: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1580 | case JS_REGEXP_TYPE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1581 | case JS_GLOBAL_PROXY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1582 | case JS_GLOBAL_OBJECT_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1583 | case JS_BUILTINS_OBJECT_TYPE: |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1584 | case JS_MESSAGE_OBJECT_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1585 | JSObject::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1586 | break; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1587 | case JS_FUNCTION_TYPE: |
| 1588 | reinterpret_cast<JSFunction*>(this) |
| 1589 | ->JSFunctionIterateBody(object_size, v); |
| 1590 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | case ODDBALL_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1592 | Oddball::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1593 | break; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1594 | case JS_PROXY_TYPE: |
| 1595 | JSProxy::BodyDescriptor::IterateBody(this, v); |
| 1596 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1597 | case JS_FUNCTION_PROXY_TYPE: |
| 1598 | JSFunctionProxy::BodyDescriptor::IterateBody(this, v); |
| 1599 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1600 | case FOREIGN_TYPE: |
| 1601 | reinterpret_cast<Foreign*>(this)->ForeignIterateBody(v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | case MAP_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1604 | Map::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1605 | break; |
| 1606 | case CODE_TYPE: |
| 1607 | reinterpret_cast<Code*>(this)->CodeIterateBody(v); |
| 1608 | break; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1609 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1610 | JSGlobalPropertyCell::BodyDescriptor::IterateBody(this, v); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1611 | break; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1612 | case SYMBOL_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1613 | Symbol::BodyDescriptor::IterateBody(this, v); |
| 1614 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1615 | case HEAP_NUMBER_TYPE: |
| 1616 | case FILLER_TYPE: |
| 1617 | case BYTE_ARRAY_TYPE: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1618 | case FREE_SPACE_TYPE: |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1619 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1620 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1621 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1622 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1623 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1624 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1625 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1626 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1627 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1628 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1629 | case SHARED_FUNCTION_INFO_TYPE: { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1630 | SharedFunctionInfo::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1631 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1632 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1633 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1634 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1635 | case NAME##_TYPE: |
| 1636 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1637 | #undef MAKE_STRUCT_CASE |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1638 | StructBodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | default: |
| 1641 | PrintF("Unknown type: %d\n", type); |
| 1642 | UNREACHABLE(); |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1647 | bool HeapNumber::HeapNumberBooleanValue() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1648 | // NaN, +0, and -0 should return the false object |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1649 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1650 | union IeeeDoubleLittleEndianArchType u; |
| 1651 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 1652 | union IeeeDoubleBigEndianArchType u; |
| 1653 | #endif |
| 1654 | u.d = value(); |
| 1655 | if (u.bits.exp == 2047) { |
| 1656 | // Detect NaN for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1657 | if ((u.bits.man_low | u.bits.man_high) != 0) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1658 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1659 | if (u.bits.exp == 0) { |
| 1660 | // Detect +0, and -0 for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1661 | if ((u.bits.man_low | u.bits.man_high) == 0) return false; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1662 | } |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1663 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1667 | void HeapNumber::HeapNumberPrint(FILE* out) { |
| 1668 | PrintF(out, "%.16g", Number()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | |
| 1672 | void HeapNumber::HeapNumberPrint(StringStream* accumulator) { |
| 1673 | // The Windows version of vsnprintf can allocate when printing a %g string |
| 1674 | // into a buffer that may not be big enough. We don't want random memory |
| 1675 | // allocation when producing post-crash stack traces, so we print into a |
| 1676 | // buffer that is plenty big enough for any floating point number, then |
| 1677 | // print that using vsnprintf (which may truncate but never allocate if |
| 1678 | // there is no more space in the buffer). |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1679 | EmbeddedVector<char, 100> buffer; |
| 1680 | OS::SNPrintF(buffer, "%.16g", Number()); |
| 1681 | accumulator->Add("%s", buffer.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1685 | String* JSReceiver::class_name() { |
| 1686 | if (IsJSFunction() && IsJSFunctionProxy()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1687 | return GetHeap()->function_class_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1688 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1689 | if (map()->constructor()->IsJSFunction()) { |
| 1690 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1691 | return String::cast(constructor->shared()->instance_class_name()); |
| 1692 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1693 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1694 | return GetHeap()->Object_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1698 | String* JSReceiver::constructor_name() { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1699 | if (map()->constructor()->IsJSFunction()) { |
| 1700 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1701 | String* name = String::cast(constructor->shared()->name()); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1702 | if (name->length() > 0) return name; |
| 1703 | String* inferred_name = constructor->shared()->inferred_name(); |
| 1704 | if (inferred_name->length() > 0) return inferred_name; |
| 1705 | Object* proto = GetPrototype(); |
| 1706 | if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1707 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1708 | // TODO(rossberg): what about proxies? |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1709 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1710 | return GetHeap()->Object_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1714 | MaybeObject* JSObject::AddFastPropertyUsingMap(Map* new_map, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1715 | Name* name, |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 1716 | Object* value, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1717 | int field_index, |
| 1718 | Representation representation) { |
| 1719 | // This method is used to transition to a field. If we are transitioning to a |
| 1720 | // double field, allocate new storage. |
| 1721 | Object* storage; |
| 1722 | MaybeObject* maybe_storage = |
| 1723 | value->AllocateNewStorageFor(GetHeap(), representation); |
| 1724 | if (!maybe_storage->To(&storage)) return maybe_storage; |
| 1725 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1726 | if (map()->unused_property_fields() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1727 | int new_unused = new_map->unused_property_fields(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 1728 | FixedArray* values; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 1729 | MaybeObject* maybe_values = |
| 1730 | properties()->CopySize(properties()->length() + new_unused + 1); |
| 1731 | if (!maybe_values->To(&values)) return maybe_values; |
| 1732 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 1733 | set_properties(values); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1734 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1735 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1736 | set_map(new_map); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1737 | |
| 1738 | FastPropertyAtPut(field_index, storage); |
| 1739 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1743 | static bool IsIdentifier(UnicodeCache* cache, Name* name) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1744 | // Checks whether the buffer contains an identifier (no escape). |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1745 | if (!name->IsString()) return false; |
| 1746 | String* string = String::cast(name); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1747 | if (string->length() == 0) return false; |
| 1748 | ConsStringIteratorOp op; |
| 1749 | StringCharacterStream stream(string, &op); |
| 1750 | if (!cache->IsIdentifierStart(stream.GetNext())) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1753 | while (stream.HasMore()) { |
| 1754 | if (!cache->IsIdentifierPart(stream.GetNext())) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1755 | return false; |
| 1756 | } |
| 1757 | } |
| 1758 | return true; |
| 1759 | } |
| 1760 | |
| 1761 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1762 | MaybeObject* JSObject::AddFastProperty(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1763 | Object* value, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1764 | PropertyAttributes attributes, |
| 1765 | StoreFromKeyed store_mode) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1766 | ASSERT(!IsJSGlobalProxy()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 1767 | ASSERT(DescriptorArray::kNotFound == |
| 1768 | map()->instance_descriptors()->Search( |
| 1769 | name, map()->NumberOfOwnDescriptors())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1770 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1771 | // Normalize the object if the name is an actual name (not the |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1772 | // hidden strings) and is not a real identifier. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1773 | // Normalize the object if it will have too many fast properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1774 | Isolate* isolate = GetHeap()->isolate(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1775 | if ((!name->IsSymbol() && !IsIdentifier(isolate->unicode_cache(), name) |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1776 | && name != isolate->heap()->hidden_string()) || |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1777 | (map()->unused_property_fields() == 0 && |
| 1778 | TooManyFastProperties(properties()->length(), store_mode))) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1779 | Object* obj; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1780 | MaybeObject* maybe_obj = |
| 1781 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1782 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1783 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | return AddSlowProperty(name, value, attributes); |
| 1785 | } |
| 1786 | |
| 1787 | // Compute the new index for new field. |
| 1788 | int index = map()->NextFreePropertyIndex(); |
| 1789 | |
| 1790 | // Allocate new instance descriptors with (name, index) added |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1791 | Representation representation = value->OptimalRepresentation(); |
| 1792 | FieldDescriptor new_field(name, index, attributes, representation); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1793 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1794 | ASSERT(index < map()->inobject_properties() || |
| 1795 | (index - map()->inobject_properties()) < properties()->length() || |
| 1796 | map()->unused_property_fields() == 0); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1797 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1798 | FixedArray* values = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1799 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1800 | // TODO(verwaest): Merge with AddFastPropertyUsingMap. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1801 | if (map()->unused_property_fields() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1802 | // Make room for the new value |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1803 | MaybeObject* maybe_values = |
| 1804 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 1805 | if (!maybe_values->To(&values)) return maybe_values; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1806 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1807 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 1808 | TransitionFlag flag = INSERT_TRANSITION; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1809 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1810 | Heap* heap = isolate->heap(); |
| 1811 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1812 | Map* new_map; |
| 1813 | MaybeObject* maybe_new_map = map()->CopyAddDescriptor(&new_field, flag); |
| 1814 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 1815 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1816 | Object* storage; |
| 1817 | MaybeObject* maybe_storage = |
| 1818 | value->AllocateNewStorageFor(heap, representation); |
| 1819 | if (!maybe_storage->To(&storage)) return maybe_storage; |
| 1820 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1821 | if (map()->unused_property_fields() == 0) { |
| 1822 | ASSERT(values != NULL); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1823 | set_properties(values); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1824 | new_map->set_unused_property_fields(kFieldsAdded - 1); |
| 1825 | } else { |
| 1826 | new_map->set_unused_property_fields(map()->unused_property_fields() - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1827 | } |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1828 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1829 | set_map(new_map); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1830 | |
| 1831 | FastPropertyAtPut(index, storage); |
| 1832 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1836 | MaybeObject* JSObject::AddConstantFunctionProperty( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1837 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1838 | JSFunction* function, |
| 1839 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1840 | // Allocate new instance descriptors with (name, function) added |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1841 | ConstantFunctionDescriptor d(name, function, attributes); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1842 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1843 | TransitionFlag flag = |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 1844 | // Do not add transitions to global objects. |
| 1845 | (IsGlobalObject() || |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1846 | // Don't add transitions to special properties with non-trivial |
| 1847 | // attributes. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1848 | attributes != NONE) |
| 1849 | ? OMIT_TRANSITION |
| 1850 | : INSERT_TRANSITION; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 1852 | Map* new_map; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 1853 | MaybeObject* maybe_new_map = map()->CopyAddDescriptor(&d, flag); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1854 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1855 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 1856 | set_map(new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | return function; |
| 1858 | } |
| 1859 | |
| 1860 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1861 | // Add property in slow mode |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1862 | MaybeObject* JSObject::AddSlowProperty(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1863 | Object* value, |
| 1864 | PropertyAttributes attributes) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1865 | ASSERT(!HasFastProperties()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1866 | NameDictionary* dict = property_dictionary(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1867 | Object* store_value = value; |
| 1868 | if (IsGlobalObject()) { |
| 1869 | // In case name is an orphaned property reuse the cell. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1870 | int entry = dict->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1871 | if (entry != NameDictionary::kNotFound) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1872 | store_value = dict->ValueAt(entry); |
| 1873 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1874 | // Assign an enumeration index to the property and update |
| 1875 | // SetNextEnumerationIndex. |
| 1876 | int index = dict->NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1877 | PropertyDetails details = PropertyDetails(attributes, NORMAL, index); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 1878 | dict->SetNextEnumerationIndex(index + 1); |
| 1879 | dict->SetEntry(entry, name, store_value, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1880 | return value; |
| 1881 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1882 | Heap* heap = GetHeap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1883 | { MaybeObject* maybe_store_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1884 | heap->AllocateJSGlobalPropertyCell(value); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1885 | if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value; |
| 1886 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1887 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1888 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 1889 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1890 | Object* result; |
| 1891 | { MaybeObject* maybe_result = dict->Add(name, store_value, details); |
| 1892 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1893 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1894 | if (dict != result) set_properties(NameDictionary::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | return value; |
| 1896 | } |
| 1897 | |
| 1898 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1899 | MaybeObject* JSObject::AddProperty(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1900 | Object* value, |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1901 | PropertyAttributes attributes, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1902 | StrictModeFlag strict_mode, |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 1903 | JSReceiver::StoreFromKeyed store_mode, |
| 1904 | ExtensibilityCheck extensibility_check) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1905 | ASSERT(!IsJSGlobalProxy()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1906 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1907 | Heap* heap = GetHeap(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1908 | Isolate* isolate = heap->isolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1909 | MaybeObject* result; |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 1910 | if (extensibility_check == PERFORM_EXTENSIBILITY_CHECK && |
| 1911 | !map_of_this->is_extensible()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1912 | if (strict_mode == kNonStrictMode) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1913 | return value; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1914 | } else { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1915 | Handle<Object> args[1] = {Handle<Name>(name)}; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1916 | return isolate->Throw( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1917 | *FACTORY->NewTypeError("object_not_extensible", |
| 1918 | HandleVector(args, 1))); |
| 1919 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1920 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1921 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1922 | if (HasFastProperties()) { |
| 1923 | // Ensure the descriptor array does not get too big. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 1924 | if (map_of_this->NumberOfOwnDescriptors() < |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1925 | DescriptorArray::kMaxNumberOfDescriptors) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1926 | if (value->IsJSFunction()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1927 | result = AddConstantFunctionProperty(name, |
| 1928 | JSFunction::cast(value), |
| 1929 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1930 | } else { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1931 | result = AddFastProperty(name, value, attributes, store_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1932 | } |
| 1933 | } else { |
| 1934 | // Normalize the object to prevent very large instance descriptors. |
| 1935 | // This eliminates unwanted N^2 allocation and lookup behavior. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1936 | Object* obj; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1937 | MaybeObject* maybe = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1938 | if (!maybe->To(&obj)) return maybe; |
| 1939 | result = AddSlowProperty(name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1940 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1941 | } else { |
| 1942 | result = AddSlowProperty(name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1943 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1944 | |
| 1945 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1946 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (FLAG_harmony_observation && map()->is_observed()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 1949 | EnqueueChangeRecord(handle(this, isolate), |
| 1950 | "new", |
| 1951 | handle(name, isolate), |
| 1952 | handle(heap->the_hole_value(), isolate)); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | return *hresult; |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | void JSObject::EnqueueChangeRecord(Handle<JSObject> object, |
| 1960 | const char* type_str, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1961 | Handle<Name> name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1962 | Handle<Object> old_value) { |
| 1963 | Isolate* isolate = object->GetIsolate(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1964 | HandleScope scope(isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1965 | Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 1966 | if (object->IsJSGlobalObject()) { |
| 1967 | object = handle(JSGlobalObject::cast(*object)->global_receiver(), isolate); |
| 1968 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1969 | Handle<Object> args[] = { type, object, name, old_value }; |
| 1970 | bool threw; |
| 1971 | Execution::Call(Handle<JSFunction>(isolate->observers_notify_change()), |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 1972 | isolate->factory()->undefined_value(), |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1973 | old_value->IsTheHole() ? 3 : 4, args, |
| 1974 | &threw); |
| 1975 | ASSERT(!threw); |
| 1976 | } |
| 1977 | |
| 1978 | |
| 1979 | void JSObject::DeliverChangeRecords(Isolate* isolate) { |
| 1980 | ASSERT(isolate->observer_delivery_pending()); |
| 1981 | bool threw = false; |
| 1982 | Execution::Call( |
| 1983 | isolate->observers_deliver_changes(), |
| 1984 | isolate->factory()->undefined_value(), |
| 1985 | 0, |
| 1986 | NULL, |
| 1987 | &threw); |
| 1988 | ASSERT(!threw); |
| 1989 | isolate->set_observer_delivery_pending(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1993 | MaybeObject* JSObject::SetPropertyPostInterceptor( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1994 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1995 | Object* value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1996 | PropertyAttributes attributes, |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 1997 | StrictModeFlag strict_mode, |
| 1998 | ExtensibilityCheck extensibility_check) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | // Check local property, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2000 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2001 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2002 | if (!result.IsFound()) map()->LookupTransition(this, name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2003 | if (result.IsFound()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2004 | // An existing property or a map transition was found. Use set property to |
| 2005 | // handle all these cases. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2006 | return SetProperty(&result, name, value, attributes, strict_mode); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2007 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2008 | bool done = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2009 | MaybeObject* result_object; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2010 | result_object = |
| 2011 | SetPropertyViaPrototypes(name, value, attributes, strict_mode, &done); |
| 2012 | if (done) return result_object; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2013 | // Add a new real property. |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 2014 | return AddProperty(name, value, attributes, strict_mode, |
| 2015 | MAY_BE_STORE_FROM_KEYED, extensibility_check); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2019 | MaybeObject* JSObject::ReplaceSlowProperty(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2020 | Object* value, |
| 2021 | PropertyAttributes attributes) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2022 | NameDictionary* dictionary = property_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2023 | int old_index = dictionary->FindEntry(name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2024 | int new_enumeration_index = 0; // 0 means "Use the next available index." |
| 2025 | if (old_index != -1) { |
| 2026 | // All calls to ReplaceSlowProperty have had all transitions removed. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2027 | new_enumeration_index = dictionary->DetailsAt(old_index).dictionary_index(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2028 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2029 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2030 | PropertyDetails new_details(attributes, NORMAL, new_enumeration_index); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2031 | return SetNormalizedProperty(name, value, new_details); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2034 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2035 | MaybeObject* JSObject::ConvertTransitionToMapTransition( |
| 2036 | int transition_index, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2037 | Name* name, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2038 | Object* new_value, |
| 2039 | PropertyAttributes attributes) { |
| 2040 | Map* old_map = map(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2041 | Map* old_target = old_map->GetTransition(transition_index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2042 | Object* result; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2043 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2044 | MaybeObject* maybe_result = |
| 2045 | ConvertDescriptorToField(name, new_value, attributes); |
| 2046 | if (!maybe_result->To(&result)) return maybe_result; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2047 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2048 | if (!HasFastProperties()) return result; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2049 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 2050 | // This method should only be used to convert existing transitions. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2051 | Map* new_map = map(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2052 | |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2053 | // TODO(verwaest): From here on we lose existing map transitions, causing |
| 2054 | // invalid back pointers. This will change once we can store multiple |
| 2055 | // transitions with the same key. |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 2056 | bool owned_descriptors = old_map->owns_descriptors(); |
| 2057 | if (owned_descriptors || |
| 2058 | old_target->instance_descriptors() == old_map->instance_descriptors()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2059 | // Since the conversion above generated a new fast map with an additional |
| 2060 | // property which can be shared as well, install this descriptor pointer |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2061 | // along the entire chain of smaller maps. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2062 | Map* map; |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 2063 | DescriptorArray* new_descriptors = new_map->instance_descriptors(); |
| 2064 | DescriptorArray* old_descriptors = old_map->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2065 | for (Object* current = old_map; |
| 2066 | !current->IsUndefined(); |
| 2067 | current = map->GetBackPointer()) { |
| 2068 | map = Map::cast(current); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2069 | if (map->instance_descriptors() != old_descriptors) break; |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 2070 | map->SetEnumLength(Map::kInvalidEnumCache); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2071 | map->set_instance_descriptors(new_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2072 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 2073 | old_map->set_owns_descriptors(false); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2076 | old_target->DeprecateTransitionTree(); |
| 2077 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2078 | old_map->SetTransition(transition_index, new_map); |
| 2079 | new_map->SetBackPointer(old_map); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2080 | return result; |
| 2081 | } |
| 2082 | |
| 2083 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2084 | MaybeObject* JSObject::ConvertDescriptorToField(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2085 | Object* new_value, |
| 2086 | PropertyAttributes attributes) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2087 | if (map()->unused_property_fields() == 0 && |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2088 | TooManyFastProperties(properties()->length(), MAY_BE_STORE_FROM_KEYED)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2089 | Object* obj; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2090 | MaybeObject* maybe_obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2091 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2092 | return ReplaceSlowProperty(name, new_value, attributes); |
| 2093 | } |
| 2094 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2095 | Representation representation = new_value->OptimalRepresentation(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2096 | int index = map()->NextFreePropertyIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2097 | FieldDescriptor new_field(name, index, attributes, representation); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2098 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2099 | // Make a new map for the object. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 2100 | Map* new_map; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2101 | MaybeObject* maybe_new_map = map()->CopyInsertDescriptor(&new_field, |
| 2102 | OMIT_TRANSITION); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2103 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Make new properties array if necessary. |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2106 | FixedArray* new_properties = NULL; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2107 | int new_unused_property_fields = map()->unused_property_fields() - 1; |
| 2108 | if (map()->unused_property_fields() == 0) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2109 | new_unused_property_fields = kFieldsAdded - 1; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2110 | MaybeObject* maybe_new_properties = |
| 2111 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 2112 | if (!maybe_new_properties->To(&new_properties)) return maybe_new_properties; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2115 | Heap* heap = GetHeap(); |
| 2116 | Object* storage; |
| 2117 | MaybeObject* maybe_storage = |
| 2118 | new_value->AllocateNewStorageFor(heap, representation); |
| 2119 | if (!maybe_storage->To(&storage)) return maybe_storage; |
| 2120 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2121 | // Update pointers to commit changes. |
| 2122 | // Object points to the new map. |
| 2123 | new_map->set_unused_property_fields(new_unused_property_fields); |
| 2124 | set_map(new_map); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2125 | if (new_properties != NULL) { |
| 2126 | set_properties(new_properties); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2127 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2128 | FastPropertyAtPut(index, new_value); |
| 2129 | return new_value; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2133 | const char* Representation::Mnemonic() const { |
| 2134 | switch (kind_) { |
| 2135 | case kNone: return "v"; |
| 2136 | case kTagged: return "t"; |
| 2137 | case kSmi: return "s"; |
| 2138 | case kDouble: return "d"; |
| 2139 | case kInteger32: return "i"; |
| 2140 | case kExternal: return "x"; |
| 2141 | default: |
| 2142 | UNREACHABLE(); |
| 2143 | return NULL; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | |
| 2148 | enum RightTrimMode { FROM_GC, FROM_MUTATOR }; |
| 2149 | |
| 2150 | |
| 2151 | static void ZapEndOfFixedArray(Address new_end, int to_trim) { |
| 2152 | // If we are doing a big trim in old space then we zap the space. |
| 2153 | Object** zap = reinterpret_cast<Object**>(new_end); |
| 2154 | zap++; // Header of filler must be at least one word so skip that. |
| 2155 | for (int i = 1; i < to_trim; i++) { |
| 2156 | *zap++ = Smi::FromInt(0); |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | |
| 2161 | template<RightTrimMode trim_mode> |
| 2162 | static void RightTrimFixedArray(Heap* heap, FixedArray* elms, int to_trim) { |
| 2163 | ASSERT(elms->map() != HEAP->fixed_cow_array_map()); |
| 2164 | // For now this trick is only applied to fixed arrays in new and paged space. |
| 2165 | ASSERT(!HEAP->lo_space()->Contains(elms)); |
| 2166 | |
| 2167 | const int len = elms->length(); |
| 2168 | |
| 2169 | ASSERT(to_trim < len); |
| 2170 | |
| 2171 | Address new_end = elms->address() + FixedArray::SizeFor(len - to_trim); |
| 2172 | |
| 2173 | if (trim_mode != FROM_GC || Heap::ShouldZapGarbage()) { |
| 2174 | ZapEndOfFixedArray(new_end, to_trim); |
| 2175 | } |
| 2176 | |
| 2177 | int size_delta = to_trim * kPointerSize; |
| 2178 | |
| 2179 | // Technically in new space this write might be omitted (except for |
| 2180 | // debug mode which iterates through the heap), but to play safer |
| 2181 | // we still do it. |
| 2182 | heap->CreateFillerObjectAt(new_end, size_delta); |
| 2183 | |
| 2184 | elms->set_length(len - to_trim); |
| 2185 | |
| 2186 | // Maintain marking consistency for IncrementalMarking. |
| 2187 | if (Marking::IsBlack(Marking::MarkBitFrom(elms))) { |
| 2188 | if (trim_mode == FROM_GC) { |
| 2189 | MemoryChunk::IncrementLiveBytesFromGC(elms->address(), -size_delta); |
| 2190 | } else { |
| 2191 | MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta); |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2197 | bool Map::InstancesNeedRewriting(Map* target, |
| 2198 | int target_number_of_fields, |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2199 | int target_inobject, |
| 2200 | int target_unused) { |
| 2201 | // If fields were added (or removed), rewrite the instance. |
| 2202 | int number_of_fields = NumberOfFields(); |
| 2203 | ASSERT(target_number_of_fields >= number_of_fields); |
| 2204 | if (target_number_of_fields != number_of_fields) return true; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2205 | |
| 2206 | if (FLAG_track_double_fields) { |
| 2207 | // If smi descriptors were replaced by double descriptors, rewrite. |
| 2208 | DescriptorArray* old_desc = instance_descriptors(); |
| 2209 | DescriptorArray* new_desc = target->instance_descriptors(); |
| 2210 | int limit = NumberOfOwnDescriptors(); |
| 2211 | for (int i = 0; i < limit; i++) { |
| 2212 | if (new_desc->GetDetails(i).representation().IsDouble() && |
| 2213 | old_desc->GetDetails(i).representation().IsSmi()) { |
| 2214 | return true; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2219 | // If no fields were added, and no inobject properties were removed, setting |
| 2220 | // the map is sufficient. |
| 2221 | if (target_inobject == inobject_properties()) return false; |
| 2222 | // In-object slack tracking may have reduced the object size of the new map. |
| 2223 | // In that case, succeed if all existing fields were inobject, and they still |
| 2224 | // fit within the new inobject size. |
| 2225 | ASSERT(target_inobject < inobject_properties()); |
| 2226 | if (target_number_of_fields <= target_inobject) { |
| 2227 | ASSERT(target_number_of_fields + target_unused == target_inobject); |
| 2228 | return false; |
| 2229 | } |
| 2230 | // Otherwise, properties will need to be moved to the backing store. |
| 2231 | return true; |
| 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | // To migrate an instance to a map: |
| 2236 | // - First check whether the instance needs to be rewritten. If not, simply |
| 2237 | // change the map. |
| 2238 | // - Otherwise, allocate a fixed array large enough to hold all fields, in |
| 2239 | // addition to unused space. |
| 2240 | // - Copy all existing properties in, in the following order: backing store |
| 2241 | // properties, unused fields, inobject properties. |
| 2242 | // - If all allocation succeeded, commit the state atomically: |
| 2243 | // * Copy inobject properties from the backing store back into the object. |
| 2244 | // * Trim the difference in instance size of the object. This also cleanly |
| 2245 | // frees inobject properties that moved to the backing store. |
| 2246 | // * If there are properties left in the backing store, trim of the space used |
| 2247 | // to temporarily store the inobject properties. |
| 2248 | // * If there are properties left in the backing store, install the backing |
| 2249 | // store. |
| 2250 | MaybeObject* JSObject::MigrateToMap(Map* new_map) { |
| 2251 | Heap* heap = GetHeap(); |
| 2252 | Map* old_map = map(); |
| 2253 | int number_of_fields = new_map->NumberOfFields(); |
| 2254 | int inobject = new_map->inobject_properties(); |
| 2255 | int unused = new_map->unused_property_fields(); |
| 2256 | |
| 2257 | // Nothing to do if no functions were converted to fields. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2258 | if (!old_map->InstancesNeedRewriting( |
| 2259 | new_map, number_of_fields, inobject, unused)) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2260 | set_map(new_map); |
| 2261 | return this; |
| 2262 | } |
| 2263 | |
| 2264 | int total_size = number_of_fields + unused; |
| 2265 | int external = total_size - inobject; |
| 2266 | FixedArray* array; |
| 2267 | MaybeObject* maybe_array = heap->AllocateFixedArray(total_size); |
| 2268 | if (!maybe_array->To(&array)) return maybe_array; |
| 2269 | |
| 2270 | DescriptorArray* old_descriptors = old_map->instance_descriptors(); |
| 2271 | DescriptorArray* new_descriptors = new_map->instance_descriptors(); |
| 2272 | int descriptors = new_map->NumberOfOwnDescriptors(); |
| 2273 | |
| 2274 | for (int i = 0; i < descriptors; i++) { |
| 2275 | PropertyDetails details = new_descriptors->GetDetails(i); |
| 2276 | if (details.type() != FIELD) continue; |
| 2277 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
| 2278 | ASSERT(old_details.type() == CONSTANT_FUNCTION || |
| 2279 | old_details.type() == FIELD); |
| 2280 | Object* value = old_details.type() == CONSTANT_FUNCTION |
| 2281 | ? old_descriptors->GetValue(i) |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2282 | : RawFastPropertyAt(old_descriptors->GetFieldIndex(i)); |
| 2283 | if (FLAG_track_double_fields && |
| 2284 | old_details.representation().IsSmi() && |
| 2285 | details.representation().IsDouble()) { |
| 2286 | // Objects must be allocated in the old object space, since the |
| 2287 | // overall number of HeapNumbers needed for the conversion might |
| 2288 | // exceed the capacity of new space, and we would fail repeatedly |
| 2289 | // trying to migrate the instance. |
| 2290 | MaybeObject* maybe_storage = |
| 2291 | value->AllocateNewStorageFor(heap, details.representation(), TENURED); |
| 2292 | if (!maybe_storage->To(&value)) return maybe_storage; |
| 2293 | } |
| 2294 | ASSERT(!(FLAG_track_double_fields && |
| 2295 | details.representation().IsDouble() && |
| 2296 | value->IsSmi())); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2297 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2298 | if (target_index < 0) target_index += total_size; |
| 2299 | array->set(target_index, value); |
| 2300 | } |
| 2301 | |
| 2302 | // From here on we cannot fail anymore. |
| 2303 | |
| 2304 | // Copy (real) inobject properties. If necessary, stop at number_of_fields to |
| 2305 | // avoid overwriting |one_pointer_filler_map|. |
| 2306 | int limit = Min(inobject, number_of_fields); |
| 2307 | for (int i = 0; i < limit; i++) { |
| 2308 | FastPropertyAtPut(i, array->get(external + i)); |
| 2309 | } |
| 2310 | |
| 2311 | // Create filler object past the new instance size. |
| 2312 | int new_instance_size = new_map->instance_size(); |
| 2313 | int instance_size_delta = old_map->instance_size() - new_instance_size; |
| 2314 | ASSERT(instance_size_delta >= 0); |
| 2315 | Address address = this->address() + new_instance_size; |
| 2316 | heap->CreateFillerObjectAt(address, instance_size_delta); |
| 2317 | |
| 2318 | // If there are properties in the new backing store, trim it to the correct |
| 2319 | // size and install the backing store into the object. |
| 2320 | if (external > 0) { |
| 2321 | RightTrimFixedArray<FROM_MUTATOR>(heap, array, inobject); |
| 2322 | set_properties(array); |
| 2323 | } |
| 2324 | |
| 2325 | set_map(new_map); |
| 2326 | |
| 2327 | return this; |
| 2328 | } |
| 2329 | |
| 2330 | |
| 2331 | MaybeObject* JSObject::GeneralizeFieldRepresentation( |
| 2332 | int modify_index, |
| 2333 | Representation new_representation) { |
| 2334 | Map* new_map; |
| 2335 | MaybeObject* maybe_new_map = |
| 2336 | map()->GeneralizeRepresentation(modify_index, new_representation); |
| 2337 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 2338 | ASSERT(map() != new_map || new_map->FindRootMap()->is_deprecated()); |
| 2339 | |
| 2340 | return MigrateToMap(new_map); |
| 2341 | } |
| 2342 | |
| 2343 | |
| 2344 | int Map::NumberOfFields() { |
| 2345 | DescriptorArray* descriptors = instance_descriptors(); |
| 2346 | int result = 0; |
| 2347 | for (int i = 0; i < NumberOfOwnDescriptors(); i++) { |
| 2348 | if (descriptors->GetDetails(i).type() == FIELD) result++; |
| 2349 | } |
| 2350 | return result; |
| 2351 | } |
| 2352 | |
| 2353 | |
| 2354 | MaybeObject* Map::CopyGeneralizeAllRepresentations() { |
| 2355 | Map* new_map; |
| 2356 | MaybeObject* maybe_map = this->Copy(); |
| 2357 | if (!maybe_map->To(&new_map)) return maybe_map; |
| 2358 | |
| 2359 | new_map->instance_descriptors()->InitializeRepresentations( |
| 2360 | Representation::Tagged()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2361 | if (FLAG_trace_generalization) { |
| 2362 | PrintF("failed generalization %p -> %p\n", |
| 2363 | static_cast<void*>(this), static_cast<void*>(new_map)); |
| 2364 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2365 | return new_map; |
| 2366 | } |
| 2367 | |
| 2368 | |
| 2369 | void Map::DeprecateTransitionTree() { |
| 2370 | if (!FLAG_track_fields) return; |
| 2371 | if (is_deprecated()) return; |
| 2372 | if (HasTransitionArray()) { |
| 2373 | TransitionArray* transitions = this->transitions(); |
| 2374 | for (int i = 0; i < transitions->number_of_transitions(); i++) { |
| 2375 | transitions->GetTarget(i)->DeprecateTransitionTree(); |
| 2376 | } |
| 2377 | } |
| 2378 | deprecate(); |
| 2379 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 2380 | GetIsolate(), DependentCode::kTransitionGroup); |
| 2381 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 2382 | GetIsolate(), DependentCode::kPrototypeCheckGroup); |
| 2383 | } |
| 2384 | |
| 2385 | |
| 2386 | // Invalidates a transition target at |key|, and installs |new_descriptors| over |
| 2387 | // the current instance_descriptors to ensure proper sharing of descriptor |
| 2388 | // arrays. |
| 2389 | void Map::DeprecateTarget(Name* key, DescriptorArray* new_descriptors) { |
| 2390 | if (HasTransitionArray()) { |
| 2391 | TransitionArray* transitions = this->transitions(); |
| 2392 | int transition = transitions->Search(key); |
| 2393 | if (transition != TransitionArray::kNotFound) { |
| 2394 | transitions->GetTarget(transition)->DeprecateTransitionTree(); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | // Don't overwrite the empty descriptor array. |
| 2399 | if (NumberOfOwnDescriptors() == 0) return; |
| 2400 | |
| 2401 | DescriptorArray* to_replace = instance_descriptors(); |
| 2402 | Map* current = this; |
| 2403 | while (current->instance_descriptors() == to_replace) { |
| 2404 | current->SetEnumLength(Map::kInvalidEnumCache); |
| 2405 | current->set_instance_descriptors(new_descriptors); |
| 2406 | Object* next = current->GetBackPointer(); |
| 2407 | if (next->IsUndefined()) break; |
| 2408 | current = Map::cast(next); |
| 2409 | } |
| 2410 | |
| 2411 | set_owns_descriptors(false); |
| 2412 | } |
| 2413 | |
| 2414 | |
| 2415 | Map* Map::FindRootMap() { |
| 2416 | Map* result = this; |
| 2417 | while (true) { |
| 2418 | Object* back = result->GetBackPointer(); |
| 2419 | if (back->IsUndefined()) return result; |
| 2420 | result = Map::cast(back); |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | |
| 2425 | Map* Map::FindUpdatedMap(int verbatim, |
| 2426 | int length, |
| 2427 | DescriptorArray* descriptors) { |
| 2428 | // This can only be called on roots of transition trees. |
| 2429 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2430 | |
| 2431 | Map* current = this; |
| 2432 | |
| 2433 | for (int i = verbatim; i < length; i++) { |
| 2434 | if (!current->HasTransitionArray()) break; |
| 2435 | Name* name = descriptors->GetKey(i); |
| 2436 | TransitionArray* transitions = current->transitions(); |
| 2437 | int transition = transitions->Search(name); |
| 2438 | if (transition == TransitionArray::kNotFound) break; |
| 2439 | current = transitions->GetTarget(transition); |
| 2440 | } |
| 2441 | |
| 2442 | return current; |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | Map* Map::FindLastMatchMap(int verbatim, |
| 2447 | int length, |
| 2448 | DescriptorArray* descriptors) { |
| 2449 | // This can only be called on roots of transition trees. |
| 2450 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2451 | |
| 2452 | Map* current = this; |
| 2453 | |
| 2454 | for (int i = verbatim; i < length; i++) { |
| 2455 | if (!current->HasTransitionArray()) break; |
| 2456 | Name* name = descriptors->GetKey(i); |
| 2457 | TransitionArray* transitions = current->transitions(); |
| 2458 | int transition = transitions->Search(name); |
| 2459 | if (transition == TransitionArray::kNotFound) break; |
| 2460 | |
| 2461 | Map* next = transitions->GetTarget(transition); |
| 2462 | DescriptorArray* next_descriptors = next->instance_descriptors(); |
| 2463 | |
| 2464 | if (next_descriptors->GetValue(i) != descriptors->GetValue(i)) break; |
| 2465 | |
| 2466 | PropertyDetails details = descriptors->GetDetails(i); |
| 2467 | PropertyDetails next_details = next_descriptors->GetDetails(i); |
| 2468 | if (details.type() != next_details.type()) break; |
| 2469 | if (details.attributes() != next_details.attributes()) break; |
| 2470 | if (!details.representation().Equals(next_details.representation())) break; |
| 2471 | ASSERT(!details.IsDeleted()); |
| 2472 | ASSERT(!next_details.IsDeleted()); |
| 2473 | |
| 2474 | current = next; |
| 2475 | } |
| 2476 | return current; |
| 2477 | } |
| 2478 | |
| 2479 | |
| 2480 | // Generalize the representation of the descriptor at |modify_index|. |
| 2481 | // This method rewrites the transition tree to reflect the new change. To avoid |
| 2482 | // high degrees over polymorphism, and to stabilize quickly, on every rewrite |
| 2483 | // the new type is deduced by merging the current type with any potential new |
| 2484 | // (partial) version of the type in the transition tree. |
| 2485 | // To do this, on each rewrite: |
| 2486 | // - Search the root of the transition tree using FindRootMap. |
| 2487 | // - Find |updated|, the newest matching version of this map using |
| 2488 | // FindUpdatedMap. This uses the keys in the own map's descriptor array to |
| 2489 | // walk the transition tree. |
| 2490 | // - Merge/generalize the descriptor array of the current map and |updated|. |
| 2491 | // - Generalize the |modify_index| descriptor using |new_representation|. |
| 2492 | // - Walk the tree again starting from the root towards |updated|. Stop at |
| 2493 | // |split_map|, the first map who's descriptor array does not match the merged |
| 2494 | // descriptor array. |
| 2495 | // - If |updated| == |split_map|, |updated| is in the expected state. Return it. |
| 2496 | // - Otherwise, invalidate the outdated transition target from |updated|, and |
| 2497 | // replace its transition tree with a new branch for the updated descriptors. |
| 2498 | MaybeObject* Map::GeneralizeRepresentation(int modify_index, |
| 2499 | Representation new_representation) { |
| 2500 | Map* old_map = this; |
| 2501 | DescriptorArray* old_descriptors = old_map->instance_descriptors(); |
| 2502 | Representation old_reprepresentation = |
| 2503 | old_descriptors->GetDetails(modify_index).representation(); |
| 2504 | |
| 2505 | if (old_reprepresentation.IsNone()) { |
| 2506 | UNREACHABLE(); |
| 2507 | old_descriptors->SetRepresentation(modify_index, new_representation); |
| 2508 | return this; |
| 2509 | } |
| 2510 | |
| 2511 | int descriptors = old_map->NumberOfOwnDescriptors(); |
| 2512 | Map* root_map = old_map->FindRootMap(); |
| 2513 | |
| 2514 | if (!old_map->EquivalentToForTransition(root_map)) { |
| 2515 | return CopyGeneralizeAllRepresentations(); |
| 2516 | } |
| 2517 | |
| 2518 | int verbatim = root_map->NumberOfOwnDescriptors(); |
| 2519 | |
| 2520 | Map* updated = root_map->FindUpdatedMap( |
| 2521 | verbatim, descriptors, old_descriptors); |
| 2522 | // Check the state of the root map. |
| 2523 | DescriptorArray* updated_descriptors = updated->instance_descriptors(); |
| 2524 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2525 | int valid = updated->NumberOfOwnDescriptors(); |
| 2526 | if (updated_descriptors->IsMoreGeneralThan( |
| 2527 | verbatim, valid, descriptors, old_descriptors)) { |
| 2528 | Representation updated_representation = |
| 2529 | updated_descriptors->GetDetails(modify_index).representation(); |
| 2530 | if (new_representation.fits_into(updated_representation)) { |
| 2531 | if (FLAG_trace_generalization) { |
| 2532 | PrintF("migrating to existing map %p -> %p\n", |
| 2533 | static_cast<void*>(this), static_cast<void*>(updated)); |
| 2534 | } |
| 2535 | return updated; |
| 2536 | } |
| 2537 | } |
| 2538 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2539 | DescriptorArray* new_descriptors; |
| 2540 | MaybeObject* maybe_descriptors = updated_descriptors->Merge( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2541 | verbatim, valid, descriptors, old_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2542 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 2543 | |
| 2544 | old_reprepresentation = |
| 2545 | new_descriptors->GetDetails(modify_index).representation(); |
| 2546 | new_representation = new_representation.generalize(old_reprepresentation); |
| 2547 | new_descriptors->SetRepresentation(modify_index, new_representation); |
| 2548 | |
| 2549 | Map* split_map = root_map->FindLastMatchMap( |
| 2550 | verbatim, descriptors, new_descriptors); |
| 2551 | |
| 2552 | int split_descriptors = split_map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2553 | // This is shadowed by |updated_descriptors| being more general than |
| 2554 | // |old_descriptors|. |
| 2555 | ASSERT(descriptors != split_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2556 | |
| 2557 | int descriptor = split_descriptors; |
| 2558 | split_map->DeprecateTarget( |
| 2559 | old_descriptors->GetKey(descriptor), new_descriptors); |
| 2560 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 2561 | if (FLAG_trace_generalization) { |
| 2562 | PrintF("migrating to new map %p -> %p (%i steps)\n", |
| 2563 | static_cast<void*>(this), |
| 2564 | static_cast<void*>(new_descriptors), |
| 2565 | descriptors - descriptor); |
| 2566 | } |
| 2567 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2568 | Map* new_map = split_map; |
| 2569 | // Add missing transitions. |
| 2570 | for (; descriptor < descriptors; descriptor++) { |
| 2571 | MaybeObject* maybe_map = new_map->CopyInstallDescriptors( |
| 2572 | descriptor, new_descriptors); |
| 2573 | if (!maybe_map->To(&new_map)) { |
| 2574 | // Create a handle for the last created map to ensure it stays alive |
| 2575 | // during GC. Its descriptor array is too large, but it will be |
| 2576 | // overwritten during retry anyway. |
| 2577 | Handle<Map>(new_map); |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | new_map->set_owns_descriptors(true); |
| 2582 | return new_map; |
| 2583 | } |
| 2584 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2585 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2586 | MaybeObject* JSObject::SetPropertyWithInterceptor( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2587 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2588 | Object* value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2589 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2590 | StrictModeFlag strict_mode) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2591 | // TODO(rossberg): Support symbols in the API. |
| 2592 | if (name->IsSymbol()) return value; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2593 | Isolate* isolate = GetIsolate(); |
| 2594 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2595 | Handle<JSObject> this_handle(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2596 | Handle<String> name_handle(String::cast(name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2597 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2598 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 2599 | if (!interceptor->setter()->IsUndefined()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2600 | LOG(isolate, ApiNamedPropertyAccess("interceptor-named-set", this, name)); |
| 2601 | CustomArguments args(isolate, interceptor->data(), this, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2602 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2603 | v8::NamedPropertySetter setter = |
| 2604 | v8::ToCData<v8::NamedPropertySetter>(interceptor->setter()); |
| 2605 | v8::Handle<v8::Value> result; |
| 2606 | { |
| 2607 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2608 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2609 | Handle<Object> value_unhole(value->IsTheHole() ? |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2610 | isolate->heap()->undefined_value() : |
| 2611 | value, |
| 2612 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2613 | result = setter(v8::Utils::ToLocal(name_handle), |
| 2614 | v8::Utils::ToLocal(value_unhole), |
| 2615 | info); |
| 2616 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2617 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | if (!result.IsEmpty()) return *value_handle; |
| 2619 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2620 | MaybeObject* raw_result = |
| 2621 | this_handle->SetPropertyPostInterceptor(*name_handle, |
| 2622 | *value_handle, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2623 | attributes, |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 2624 | strict_mode, |
| 2625 | PERFORM_EXTENSIBILITY_CHECK); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2626 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2627 | return raw_result; |
| 2628 | } |
| 2629 | |
| 2630 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2631 | Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2632 | Handle<Name> key, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2633 | Handle<Object> value, |
| 2634 | PropertyAttributes attributes, |
| 2635 | StrictModeFlag strict_mode) { |
| 2636 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 2637 | object->SetProperty(*key, *value, attributes, strict_mode), |
| 2638 | Object); |
| 2639 | } |
| 2640 | |
| 2641 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 2642 | MaybeObject* JSReceiver::SetPropertyOrFail( |
| 2643 | Handle<JSReceiver> object, |
| 2644 | Handle<Name> key, |
| 2645 | Handle<Object> value, |
| 2646 | PropertyAttributes attributes, |
| 2647 | StrictModeFlag strict_mode, |
| 2648 | JSReceiver::StoreFromKeyed store_mode) { |
| 2649 | CALL_HEAP_FUNCTION_PASS_EXCEPTION( |
| 2650 | object->GetIsolate(), |
| 2651 | object->SetProperty(*key, *value, attributes, strict_mode, store_mode)); |
| 2652 | } |
| 2653 | |
| 2654 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2655 | MaybeObject* JSReceiver::SetProperty(Name* name, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2656 | Object* value, |
| 2657 | PropertyAttributes attributes, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2658 | StrictModeFlag strict_mode, |
| 2659 | JSReceiver::StoreFromKeyed store_mode) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2660 | LookupResult result(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 2661 | LocalLookup(name, &result, true); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2662 | if (!result.IsFound()) { |
| 2663 | map()->LookupTransition(JSObject::cast(this), name, &result); |
| 2664 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2665 | return SetProperty(&result, name, value, attributes, strict_mode, store_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2669 | MaybeObject* JSObject::SetPropertyWithCallback(Object* structure, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2670 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2671 | Object* value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2672 | JSObject* holder, |
| 2673 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2674 | Isolate* isolate = GetIsolate(); |
| 2675 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2676 | |
| 2677 | // We should never get here to initialize a const with the hole |
| 2678 | // value since a const declaration would conflict with the setter. |
| 2679 | ASSERT(!value->IsTheHole()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2680 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2681 | |
| 2682 | // To accommodate both the old and the new api we switch on the |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2683 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2684 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2685 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2686 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2687 | reinterpret_cast<AccessorDescriptor*>( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2688 | Foreign::cast(structure)->foreign_address()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2689 | MaybeObject* obj = (callback->setter)(this, value, callback->data); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2690 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2691 | if (obj->IsFailure()) return obj; |
| 2692 | return *value_handle; |
| 2693 | } |
| 2694 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2695 | if (structure->IsExecutableAccessorInfo()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2696 | // api style callbacks |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2697 | ExecutableAccessorInfo* data = ExecutableAccessorInfo::cast(structure); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2698 | if (!data->IsCompatibleReceiver(this)) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2699 | Handle<Object> name_handle(name, isolate); |
| 2700 | Handle<Object> receiver_handle(this, isolate); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2701 | Handle<Object> args[2] = { name_handle, receiver_handle }; |
| 2702 | Handle<Object> error = |
| 2703 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 2704 | HandleVector(args, |
| 2705 | ARRAY_SIZE(args))); |
| 2706 | return isolate->Throw(*error); |
| 2707 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2708 | // TODO(rossberg): Support symbols in the API. |
| 2709 | if (name->IsSymbol()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2710 | Object* call_obj = data->setter(); |
| 2711 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 2712 | if (call_fun == NULL) return value; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2713 | Handle<String> key(String::cast(name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2714 | LOG(isolate, ApiNamedPropertyAccess("store", this, name)); |
| 2715 | CustomArguments args(isolate, data->data(), this, JSObject::cast(holder)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2716 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2717 | { |
| 2718 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2719 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2720 | call_fun(v8::Utils::ToLocal(key), |
| 2721 | v8::Utils::ToLocal(value_handle), |
| 2722 | info); |
| 2723 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2724 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2725 | return *value_handle; |
| 2726 | } |
| 2727 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2728 | if (structure->IsAccessorPair()) { |
| 2729 | Object* setter = AccessorPair::cast(structure)->setter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2730 | if (setter->IsSpecFunction()) { |
| 2731 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 2732 | return SetPropertyWithDefinedSetter(JSReceiver::cast(setter), value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2733 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2734 | if (strict_mode == kNonStrictMode) { |
| 2735 | return value; |
| 2736 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2737 | Handle<Name> key(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2738 | Handle<Object> holder_handle(holder, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2739 | Handle<Object> args[2] = { key, holder_handle }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2740 | return isolate->Throw( |
| 2741 | *isolate->factory()->NewTypeError("no_setter_in_callback", |
| 2742 | HandleVector(args, 2))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2743 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2746 | // TODO(dcarney): Handle correctly. |
| 2747 | if (structure->IsDeclaredAccessorInfo()) { |
| 2748 | return value; |
| 2749 | } |
| 2750 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2751 | UNREACHABLE(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 2752 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2756 | MaybeObject* JSReceiver::SetPropertyWithDefinedSetter(JSReceiver* setter, |
| 2757 | Object* value) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2758 | Isolate* isolate = GetIsolate(); |
| 2759 | Handle<Object> value_handle(value, isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2760 | Handle<JSReceiver> fun(setter, isolate); |
| 2761 | Handle<JSReceiver> self(this, isolate); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2762 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2763 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2764 | // Handle stepping into a setter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2765 | // TODO(rossberg): should this apply to getters that are function proxies? |
| 2766 | if (debug->StepInActive() && fun->IsJSFunction()) { |
| 2767 | debug->HandleStepIn( |
| 2768 | Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2769 | } |
| 2770 | #endif |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2771 | bool has_pending_exception; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 2772 | Handle<Object> argv[] = { value_handle }; |
| 2773 | Execution::Call(fun, self, ARRAY_SIZE(argv), argv, &has_pending_exception); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2774 | // Check for pending exception and return the result. |
| 2775 | if (has_pending_exception) return Failure::Exception(); |
| 2776 | return *value_handle; |
| 2777 | } |
| 2778 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2779 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2780 | MaybeObject* JSObject::SetElementWithCallbackSetterInPrototypes( |
| 2781 | uint32_t index, |
| 2782 | Object* value, |
| 2783 | bool* found, |
| 2784 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2785 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2786 | for (Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2787 | pt != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2788 | pt = pt->GetPrototype(GetIsolate())) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2789 | if (pt->IsJSProxy()) { |
| 2790 | String* name; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2791 | MaybeObject* maybe = heap->Uint32ToString(index); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2792 | if (!maybe->To<String>(&name)) { |
| 2793 | *found = true; // Force abort |
| 2794 | return maybe; |
| 2795 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2796 | return JSProxy::cast(pt)->SetPropertyViaPrototypesWithHandler( |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 2797 | this, name, value, NONE, strict_mode, found); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2798 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2799 | if (!JSObject::cast(pt)->HasDictionaryElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2800 | continue; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2801 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2802 | SeededNumberDictionary* dictionary = |
| 2803 | JSObject::cast(pt)->element_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2804 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2805 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2806 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 2807 | if (details.type() == CALLBACKS) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2808 | *found = true; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2809 | return SetElementWithCallback(dictionary->ValueAt(entry), |
| 2810 | index, |
| 2811 | value, |
| 2812 | JSObject::cast(pt), |
| 2813 | strict_mode); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2817 | *found = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2818 | return heap->the_hole_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2821 | MaybeObject* JSObject::SetPropertyViaPrototypes( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2822 | Name* name, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2823 | Object* value, |
| 2824 | PropertyAttributes attributes, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2825 | StrictModeFlag strict_mode, |
| 2826 | bool* done) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2827 | Heap* heap = GetHeap(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2828 | Isolate* isolate = heap->isolate(); |
| 2829 | |
| 2830 | *done = false; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2831 | // We could not find a local property so let's check whether there is an |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2832 | // accessor that wants to handle the property, or whether the property is |
| 2833 | // read-only on the prototype chain. |
| 2834 | LookupResult result(isolate); |
| 2835 | LookupRealNamedPropertyInPrototypes(name, &result); |
| 2836 | if (result.IsFound()) { |
| 2837 | switch (result.type()) { |
| 2838 | case NORMAL: |
| 2839 | case FIELD: |
| 2840 | case CONSTANT_FUNCTION: |
| 2841 | *done = result.IsReadOnly(); |
| 2842 | break; |
| 2843 | case INTERCEPTOR: { |
| 2844 | PropertyAttributes attr = |
| 2845 | result.holder()->GetPropertyAttributeWithInterceptor( |
| 2846 | this, name, true); |
| 2847 | *done = !!(attr & READ_ONLY); |
| 2848 | break; |
| 2849 | } |
| 2850 | case CALLBACKS: { |
| 2851 | if (!FLAG_es5_readonly && result.IsReadOnly()) break; |
| 2852 | *done = true; |
| 2853 | return SetPropertyWithCallback(result.GetCallbackObject(), |
| 2854 | name, value, result.holder(), strict_mode); |
| 2855 | } |
| 2856 | case HANDLER: { |
| 2857 | return result.proxy()->SetPropertyViaPrototypesWithHandler( |
| 2858 | this, name, value, attributes, strict_mode, done); |
| 2859 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2860 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 2861 | case NONEXISTENT: |
| 2862 | UNREACHABLE(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2863 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2864 | } |
| 2865 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2866 | |
| 2867 | // If we get here with *done true, we have encountered a read-only property. |
| 2868 | if (!FLAG_es5_readonly) *done = false; |
| 2869 | if (*done) { |
| 2870 | if (strict_mode == kNonStrictMode) return value; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2871 | Handle<Object> args[] = { Handle<Object>(name, isolate), |
| 2872 | Handle<Object>(this, isolate)}; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2873 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 2874 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args)))); |
| 2875 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2876 | return heap->the_hole_value(); |
| 2877 | } |
| 2878 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2879 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2880 | void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) { |
| 2881 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 2882 | if (slack <= descriptors->NumberOfSlackDescriptors()) return; |
| 2883 | int number_of_descriptors = descriptors->number_of_descriptors(); |
| 2884 | Isolate* isolate = map->GetIsolate(); |
| 2885 | Handle<DescriptorArray> new_descriptors = |
| 2886 | isolate->factory()->NewDescriptorArray(number_of_descriptors, slack); |
| 2887 | DescriptorArray::WhitenessWitness witness(*new_descriptors); |
| 2888 | |
| 2889 | for (int i = 0; i < number_of_descriptors; ++i) { |
| 2890 | new_descriptors->CopyFrom(i, *descriptors, i, witness); |
| 2891 | } |
| 2892 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 2893 | map->set_instance_descriptors(*new_descriptors); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | |
| 2897 | void Map::AppendCallbackDescriptors(Handle<Map> map, |
| 2898 | Handle<Object> descriptors) { |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2899 | Isolate* isolate = map->GetIsolate(); |
| 2900 | Handle<DescriptorArray> array(map->instance_descriptors()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2901 | NeanderArray callbacks(descriptors); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2902 | int nof_callbacks = callbacks.length(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2903 | |
| 2904 | ASSERT(array->NumberOfSlackDescriptors() >= nof_callbacks); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2905 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2906 | // Ensure the keys are unique names before writing them into the |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2907 | // instance descriptor. Since it may cause a GC, it has to be done before we |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2908 | // temporarily put the heap in an invalid state while appending descriptors. |
| 2909 | for (int i = 0; i < nof_callbacks; ++i) { |
| 2910 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks.get(i))); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2911 | if (!entry->name()->IsUniqueName()) { |
| 2912 | Handle<String> key = |
| 2913 | isolate->factory()->InternalizedStringFromString( |
| 2914 | Handle<String>(String::cast(entry->name()))); |
| 2915 | entry->set_name(*key); |
| 2916 | } |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2919 | int nof = map->NumberOfOwnDescriptors(); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2920 | |
| 2921 | // Fill in new callback descriptors. Process the callbacks from |
| 2922 | // back to front so that the last callback with a given name takes |
| 2923 | // precedence over previously added callbacks with that name. |
| 2924 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
| 2925 | AccessorInfo* entry = AccessorInfo::cast(callbacks.get(i)); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2926 | Name* key = Name::cast(entry->name()); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2927 | // Check if a descriptor with this name already exists before writing. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2928 | if (array->Search(key, nof) == DescriptorArray::kNotFound) { |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2929 | CallbacksDescriptor desc(key, entry, entry->property_attributes()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2930 | array->Append(&desc); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2931 | nof += 1; |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2932 | } |
| 2933 | } |
| 2934 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 2935 | map->SetNumberOfOwnDescriptors(nof); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2939 | static bool ContainsMap(MapHandleList* maps, Handle<Map> map) { |
| 2940 | ASSERT(!map.is_null()); |
| 2941 | for (int i = 0; i < maps->length(); ++i) { |
| 2942 | if (!maps->at(i).is_null() && maps->at(i).is_identical_to(map)) return true; |
| 2943 | } |
| 2944 | return false; |
| 2945 | } |
| 2946 | |
| 2947 | |
| 2948 | template <class T> |
| 2949 | static Handle<T> MaybeNull(T* p) { |
| 2950 | if (p == NULL) return Handle<T>::null(); |
| 2951 | return Handle<T>(p); |
| 2952 | } |
| 2953 | |
| 2954 | |
| 2955 | Handle<Map> Map::FindTransitionedMap(MapHandleList* candidates) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 2956 | ElementsKind kind = elements_kind(); |
| 2957 | Handle<Map> transitioned_map = Handle<Map>::null(); |
| 2958 | Handle<Map> current_map(this); |
| 2959 | bool packed = IsFastPackedElementsKind(kind); |
| 2960 | if (IsTransitionableFastElementsKind(kind)) { |
| 2961 | while (CanTransitionToMoreGeneralFastElementsKind(kind, false)) { |
| 2962 | kind = GetNextMoreGeneralFastElementsKind(kind, false); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 2963 | Handle<Map> maybe_transitioned_map = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2964 | MaybeNull(current_map->LookupElementsTransitionMap(kind)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 2965 | if (maybe_transitioned_map.is_null()) break; |
| 2966 | if (ContainsMap(candidates, maybe_transitioned_map) && |
| 2967 | (packed || !IsFastPackedElementsKind(kind))) { |
| 2968 | transitioned_map = maybe_transitioned_map; |
| 2969 | if (!IsFastPackedElementsKind(kind)) packed = false; |
| 2970 | } |
| 2971 | current_map = maybe_transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2972 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2973 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 2974 | return transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 2977 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2978 | static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) { |
| 2979 | Map* current_map = map; |
| 2980 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 2981 | int to_index = IsFastElementsKind(to_kind) |
| 2982 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 2983 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 2984 | |
| 2985 | ASSERT(index <= to_index); |
| 2986 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2987 | for (; index < to_index; ++index) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2988 | if (!current_map->HasElementsTransition()) return current_map; |
| 2989 | current_map = current_map->elements_transition_map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2990 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2991 | if (!IsFastElementsKind(to_kind) && current_map->HasElementsTransition()) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 2992 | Map* next_map = current_map->elements_transition_map(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2993 | if (next_map->elements_kind() == to_kind) return next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 2994 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 2995 | ASSERT(IsFastElementsKind(to_kind) |
| 2996 | ? current_map->elements_kind() == to_kind |
| 2997 | : current_map->elements_kind() == TERMINAL_FAST_ELEMENTS_KIND); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2998 | return current_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3002 | Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3003 | Map* to_map = FindClosestElementsTransition(this, to_kind); |
| 3004 | if (to_map->elements_kind() == to_kind) return to_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3005 | return NULL; |
| 3006 | } |
| 3007 | |
| 3008 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3009 | static MaybeObject* AddMissingElementsTransitions(Map* map, |
| 3010 | ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3011 | ASSERT(IsFastElementsKind(map->elements_kind())); |
| 3012 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
| 3013 | int to_index = IsFastElementsKind(to_kind) |
| 3014 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 3015 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 3016 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3017 | ASSERT(index <= to_index); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3018 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3019 | Map* current_map = map; |
| 3020 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3021 | for (; index < to_index; ++index) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3022 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(index + 1); |
| 3023 | MaybeObject* maybe_next_map = |
| 3024 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 3025 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3028 | // In case we are exiting the fast elements kind system, just add the map in |
| 3029 | // the end. |
| 3030 | if (!IsFastElementsKind(to_kind)) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3031 | MaybeObject* maybe_next_map = |
| 3032 | current_map->CopyAsElementsKind(to_kind, INSERT_TRANSITION); |
| 3033 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3036 | ASSERT(current_map->elements_kind() == to_kind); |
| 3037 | return current_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3041 | Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object, |
| 3042 | ElementsKind to_kind) { |
| 3043 | Isolate* isolate = object->GetIsolate(); |
| 3044 | CALL_HEAP_FUNCTION(isolate, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3045 | object->GetElementsTransitionMap(isolate, to_kind), |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3046 | Map); |
| 3047 | } |
| 3048 | |
| 3049 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3050 | MaybeObject* JSObject::GetElementsTransitionMapSlow(ElementsKind to_kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3051 | Map* start_map = map(); |
| 3052 | ElementsKind from_kind = start_map->elements_kind(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3053 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3054 | if (from_kind == to_kind) { |
| 3055 | return start_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3058 | bool allow_store_transition = |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3059 | // Only remember the map transition if there is not an already existing |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3060 | // non-matching element transition. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3061 | !start_map->IsUndefined() && !start_map->is_shared() && |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3062 | IsFastElementsKind(from_kind); |
| 3063 | |
| 3064 | // Only store fast element maps in ascending generality. |
| 3065 | if (IsFastElementsKind(to_kind)) { |
| 3066 | allow_store_transition &= |
| 3067 | IsTransitionableFastElementsKind(from_kind) && |
| 3068 | IsMoreGeneralElementsKindTransition(from_kind, to_kind); |
| 3069 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3070 | |
| 3071 | if (!allow_store_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3072 | return start_map->CopyAsElementsKind(to_kind, OMIT_TRANSITION); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3073 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3074 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3075 | return start_map->AsElementsKind(to_kind); |
| 3076 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3077 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3078 | |
| 3079 | MaybeObject* Map::AsElementsKind(ElementsKind kind) { |
| 3080 | Map* closest_map = FindClosestElementsTransition(this, kind); |
| 3081 | |
| 3082 | if (closest_map->elements_kind() == kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3083 | return closest_map; |
| 3084 | } |
| 3085 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3086 | return AddMissingElementsTransitions(closest_map, kind); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3090 | void JSObject::LocalLookupRealNamedProperty(Name* name, LookupResult* result) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3091 | if (IsJSGlobalProxy()) { |
| 3092 | Object* proto = GetPrototype(); |
| 3093 | if (proto->IsNull()) return result->NotFound(); |
| 3094 | ASSERT(proto->IsJSGlobalObject()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3095 | // A GlobalProxy's prototype should always be a proper JSObject. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3096 | return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result); |
| 3097 | } |
| 3098 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3099 | if (HasFastProperties()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3100 | map()->LookupDescriptor(this, name, result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3101 | // A property or a map transition was found. We return all of these result |
| 3102 | // types because LocalLookupRealNamedProperty is used when setting |
| 3103 | // properties where map transitions are handled. |
| 3104 | ASSERT(!result->IsFound() || |
| 3105 | (result->holder() == this && result->IsFastPropertyType())); |
| 3106 | // Disallow caching for uninitialized constants. These can only |
| 3107 | // occur as fields. |
| 3108 | if (result->IsField() && |
| 3109 | result->IsReadOnly() && |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3110 | RawFastPropertyAt(result->GetFieldIndex().field_index())->IsTheHole()) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3111 | result->DisallowCaching(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3112 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3113 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3114 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3115 | |
| 3116 | int entry = property_dictionary()->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3117 | if (entry != NameDictionary::kNotFound) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3118 | Object* value = property_dictionary()->ValueAt(entry); |
| 3119 | if (IsGlobalObject()) { |
| 3120 | PropertyDetails d = property_dictionary()->DetailsAt(entry); |
| 3121 | if (d.IsDeleted()) { |
| 3122 | result->NotFound(); |
| 3123 | return; |
| 3124 | } |
| 3125 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 3126 | } |
| 3127 | // Make sure to disallow caching for uninitialized constants |
| 3128 | // found in the dictionary-mode objects. |
| 3129 | if (value->IsTheHole()) result->DisallowCaching(); |
| 3130 | result->DictionaryResult(this, entry); |
| 3131 | return; |
| 3132 | } |
| 3133 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3134 | result->NotFound(); |
| 3135 | } |
| 3136 | |
| 3137 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3138 | void JSObject::LookupRealNamedProperty(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3139 | LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3140 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3141 | |
| 3142 | LookupRealNamedPropertyInPrototypes(name, result); |
| 3143 | } |
| 3144 | |
| 3145 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3146 | void JSObject::LookupRealNamedPropertyInPrototypes(Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3147 | LookupResult* result) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3148 | Isolate* isolate = GetIsolate(); |
| 3149 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3150 | for (Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3151 | pt != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3152 | pt = pt->GetPrototype(isolate)) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3153 | if (pt->IsJSProxy()) { |
| 3154 | return result->HandlerResult(JSProxy::cast(pt)); |
| 3155 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3156 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3157 | ASSERT(!(result->IsFound() && result->type() == INTERCEPTOR)); |
| 3158 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3159 | } |
| 3160 | result->NotFound(); |
| 3161 | } |
| 3162 | |
| 3163 | |
| 3164 | // We only need to deal with CALLBACKS and INTERCEPTORS |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3165 | MaybeObject* JSObject::SetPropertyWithFailedAccessCheck( |
| 3166 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3167 | Name* name, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3168 | Object* value, |
| 3169 | bool check_prototype, |
| 3170 | StrictModeFlag strict_mode) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3171 | if (check_prototype && !result->IsProperty()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3172 | LookupRealNamedPropertyInPrototypes(name, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | if (result->IsProperty()) { |
| 3176 | if (!result->IsReadOnly()) { |
| 3177 | switch (result->type()) { |
| 3178 | case CALLBACKS: { |
| 3179 | Object* obj = result->GetCallbackObject(); |
| 3180 | if (obj->IsAccessorInfo()) { |
| 3181 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 3182 | if (info->all_can_write()) { |
| 3183 | return SetPropertyWithCallback(result->GetCallbackObject(), |
| 3184 | name, |
| 3185 | value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3186 | result->holder(), |
| 3187 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | break; |
| 3191 | } |
| 3192 | case INTERCEPTOR: { |
| 3193 | // Try lookup real named properties. Note that only property can be |
| 3194 | // set is callbacks marked as ALL_CAN_WRITE on the prototype chain. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3195 | LookupResult r(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3196 | LookupRealNamedProperty(name, &r); |
| 3197 | if (r.IsProperty()) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3198 | return SetPropertyWithFailedAccessCheck(&r, |
| 3199 | name, |
| 3200 | value, |
| 3201 | check_prototype, |
| 3202 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3203 | } |
| 3204 | break; |
| 3205 | } |
| 3206 | default: { |
| 3207 | break; |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | } |
| 3212 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3213 | Isolate* isolate = GetIsolate(); |
| 3214 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3215 | Handle<Object> value_handle(value, isolate); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3216 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 3217 | return *value_handle; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3221 | MaybeObject* JSReceiver::SetProperty(LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3222 | Name* key, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3223 | Object* value, |
| 3224 | PropertyAttributes attributes, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3225 | StrictModeFlag strict_mode, |
| 3226 | JSReceiver::StoreFromKeyed store_mode) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 3227 | if (result->IsHandler()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3228 | return result->proxy()->SetPropertyWithHandler( |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 3229 | this, key, value, attributes, strict_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3230 | } else { |
| 3231 | return JSObject::cast(this)->SetPropertyForResult( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3232 | result, key, value, attributes, strict_mode, store_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3237 | bool JSProxy::HasPropertyWithHandler(Name* name_raw) { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3238 | Isolate* isolate = GetIsolate(); |
| 3239 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3240 | Handle<Object> receiver(this, isolate); |
| 3241 | Handle<Object> name(name_raw, isolate); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3242 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3243 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3244 | if (name->IsSymbol()) return false; |
| 3245 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3246 | Handle<Object> args[] = { name }; |
| 3247 | Handle<Object> result = CallTrap( |
| 3248 | "has", isolate->derived_has_trap(), ARRAY_SIZE(args), args); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3249 | if (isolate->has_pending_exception()) return false; |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3250 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3251 | return result->BooleanValue(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3255 | MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyWithHandler( |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 3256 | JSReceiver* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3257 | Name* name_raw, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3258 | Object* value_raw, |
| 3259 | PropertyAttributes attributes, |
| 3260 | StrictModeFlag strict_mode) { |
| 3261 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3262 | HandleScope scope(isolate); |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 3263 | Handle<JSReceiver> receiver(receiver_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3264 | Handle<Object> name(name_raw, isolate); |
| 3265 | Handle<Object> value(value_raw, isolate); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3266 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3267 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3268 | if (name->IsSymbol()) return *value; |
| 3269 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3270 | Handle<Object> args[] = { receiver, name, value }; |
| 3271 | CallTrap("set", isolate->derived_set_trap(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3272 | if (isolate->has_pending_exception()) return Failure::Exception(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3273 | |
| 3274 | return *value; |
| 3275 | } |
| 3276 | |
| 3277 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3278 | MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyViaPrototypesWithHandler( |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 3279 | JSReceiver* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3280 | Name* name_raw, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3281 | Object* value_raw, |
| 3282 | PropertyAttributes attributes, |
| 3283 | StrictModeFlag strict_mode, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3284 | bool* done) { |
| 3285 | Isolate* isolate = GetIsolate(); |
rossberg@chromium.org | 052b24a | 2012-06-11 12:32:17 +0000 | [diff] [blame] | 3286 | Handle<JSProxy> proxy(this); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3287 | Handle<JSReceiver> receiver(receiver_raw); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3288 | Handle<Name> name(name_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3289 | Handle<Object> value(value_raw, isolate); |
| 3290 | Handle<Object> handler(this->handler(), isolate); // Trap might morph proxy. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3291 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3292 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3293 | if (name->IsSymbol()) { |
| 3294 | *done = false; |
| 3295 | return isolate->heap()->the_hole_value(); |
| 3296 | } |
| 3297 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3298 | *done = true; // except where redefined... |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3299 | Handle<Object> args[] = { name }; |
| 3300 | Handle<Object> result = proxy->CallTrap( |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3301 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3302 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 3303 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3304 | if (result->IsUndefined()) { |
| 3305 | *done = false; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3306 | return isolate->heap()->the_hole_value(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3309 | // Emulate [[GetProperty]] semantics for proxies. |
| 3310 | bool has_pending_exception; |
| 3311 | Handle<Object> argv[] = { result }; |
| 3312 | Handle<Object> desc = |
| 3313 | Execution::Call(isolate->to_complete_property_descriptor(), result, |
| 3314 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
| 3315 | if (has_pending_exception) return Failure::Exception(); |
| 3316 | |
| 3317 | // [[GetProperty]] requires to check that all properties are configurable. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3318 | Handle<String> configurable_name = |
| 3319 | isolate->factory()->InternalizeOneByteString( |
| 3320 | STATIC_ASCII_VECTOR("configurable_")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3321 | Handle<Object> configurable( |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3322 | v8::internal::GetProperty(isolate, desc, configurable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3323 | ASSERT(!isolate->has_pending_exception()); |
| 3324 | ASSERT(configurable->IsTrue() || configurable->IsFalse()); |
| 3325 | if (configurable->IsFalse()) { |
| 3326 | Handle<String> trap = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3327 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3328 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3329 | Handle<Object> args[] = { handler, trap, name }; |
| 3330 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3331 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
| 3332 | return isolate->Throw(*error); |
| 3333 | } |
| 3334 | ASSERT(configurable->IsTrue()); |
| 3335 | |
| 3336 | // Check for DataDescriptor. |
| 3337 | Handle<String> hasWritable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3338 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3339 | STATIC_ASCII_VECTOR("hasWritable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3340 | Handle<Object> hasWritable( |
| 3341 | v8::internal::GetProperty(isolate, desc, hasWritable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3342 | ASSERT(!isolate->has_pending_exception()); |
| 3343 | ASSERT(hasWritable->IsTrue() || hasWritable->IsFalse()); |
| 3344 | if (hasWritable->IsTrue()) { |
| 3345 | Handle<String> writable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3346 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3347 | STATIC_ASCII_VECTOR("writable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3348 | Handle<Object> writable( |
| 3349 | v8::internal::GetProperty(isolate, desc, writable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3350 | ASSERT(!isolate->has_pending_exception()); |
| 3351 | ASSERT(writable->IsTrue() || writable->IsFalse()); |
| 3352 | *done = writable->IsFalse(); |
| 3353 | if (!*done) return GetHeap()->the_hole_value(); |
| 3354 | if (strict_mode == kNonStrictMode) return *value; |
| 3355 | Handle<Object> args[] = { name, receiver }; |
| 3356 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3357 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
| 3358 | return isolate->Throw(*error); |
| 3359 | } |
| 3360 | |
| 3361 | // We have an AccessorDescriptor. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3362 | Handle<String> set_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3363 | STATIC_ASCII_VECTOR("set_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3364 | Handle<Object> setter(v8::internal::GetProperty(isolate, desc, set_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3365 | ASSERT(!isolate->has_pending_exception()); |
| 3366 | if (!setter->IsUndefined()) { |
| 3367 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 3368 | return receiver->SetPropertyWithDefinedSetter( |
| 3369 | JSReceiver::cast(*setter), *value); |
| 3370 | } |
| 3371 | |
| 3372 | if (strict_mode == kNonStrictMode) return *value; |
| 3373 | Handle<Object> args2[] = { name, proxy }; |
| 3374 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3375 | "no_setter_in_callback", HandleVector(args2, ARRAY_SIZE(args2))); |
| 3376 | return isolate->Throw(*error); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3380 | MUST_USE_RESULT MaybeObject* JSProxy::DeletePropertyWithHandler( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3381 | Name* name_raw, DeleteMode mode) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3382 | Isolate* isolate = GetIsolate(); |
| 3383 | HandleScope scope(isolate); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 3384 | Handle<JSProxy> receiver(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3385 | Handle<Object> name(name_raw, isolate); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3386 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3387 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3388 | if (name->IsSymbol()) return isolate->heap()->false_value(); |
| 3389 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3390 | Handle<Object> args[] = { name }; |
| 3391 | Handle<Object> result = CallTrap( |
| 3392 | "delete", Handle<Object>(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3393 | if (isolate->has_pending_exception()) return Failure::Exception(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3394 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3395 | bool result_bool = result->BooleanValue(); |
| 3396 | if (mode == STRICT_DELETION && !result_bool) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3397 | Handle<Object> handler(receiver->handler(), isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3398 | Handle<String> trap_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3399 | STATIC_ASCII_VECTOR("delete")); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 3400 | Handle<Object> args[] = { handler, trap_name }; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3401 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3402 | "handler_failed", HandleVector(args, ARRAY_SIZE(args))); |
| 3403 | isolate->Throw(*error); |
| 3404 | return Failure::Exception(); |
| 3405 | } |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3406 | return isolate->heap()->ToBoolean(result_bool); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3410 | MUST_USE_RESULT MaybeObject* JSProxy::DeleteElementWithHandler( |
| 3411 | uint32_t index, |
| 3412 | DeleteMode mode) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3413 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3414 | HandleScope scope(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3415 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 3416 | return JSProxy::DeletePropertyWithHandler(*name, mode); |
| 3417 | } |
| 3418 | |
| 3419 | |
| 3420 | MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler( |
| 3421 | JSReceiver* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3422 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3423 | Isolate* isolate = GetIsolate(); |
| 3424 | HandleScope scope(isolate); |
| 3425 | Handle<JSProxy> proxy(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3426 | Handle<Object> handler(this->handler(), isolate); // Trap might morph proxy. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3427 | Handle<JSReceiver> receiver(receiver_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3428 | Handle<Object> name(name_raw, isolate); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3429 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3430 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3431 | if (name->IsSymbol()) return ABSENT; |
| 3432 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3433 | Handle<Object> args[] = { name }; |
| 3434 | Handle<Object> result = CallTrap( |
| 3435 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3436 | if (isolate->has_pending_exception()) return NONE; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3437 | |
| 3438 | if (result->IsUndefined()) return ABSENT; |
| 3439 | |
| 3440 | bool has_pending_exception; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3441 | Handle<Object> argv[] = { result }; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3442 | Handle<Object> desc = |
| 3443 | Execution::Call(isolate->to_complete_property_descriptor(), result, |
| 3444 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
| 3445 | if (has_pending_exception) return NONE; |
| 3446 | |
| 3447 | // Convert result to PropertyAttributes. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3448 | Handle<String> enum_n = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3449 | STATIC_ASCII_VECTOR("enumerable")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3450 | Handle<Object> enumerable(v8::internal::GetProperty(isolate, desc, enum_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3451 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3452 | Handle<String> conf_n = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3453 | STATIC_ASCII_VECTOR("configurable")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3454 | Handle<Object> configurable(v8::internal::GetProperty(isolate, desc, conf_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3455 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3456 | Handle<String> writ_n = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3457 | STATIC_ASCII_VECTOR("writable")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3458 | Handle<Object> writable(v8::internal::GetProperty(isolate, desc, writ_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3459 | if (isolate->has_pending_exception()) return NONE; |
| 3460 | |
| 3461 | if (configurable->IsFalse()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3462 | Handle<String> trap = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3463 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3464 | Handle<Object> args[] = { handler, trap, name }; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3465 | Handle<Object> error = isolate->factory()->NewTypeError( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3466 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3467 | isolate->Throw(*error); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3468 | return NONE; |
| 3469 | } |
| 3470 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3471 | int attributes = NONE; |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3472 | if (!enumerable->BooleanValue()) attributes |= DONT_ENUM; |
| 3473 | if (!configurable->BooleanValue()) attributes |= DONT_DELETE; |
| 3474 | if (!writable->BooleanValue()) attributes |= READ_ONLY; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3475 | return static_cast<PropertyAttributes>(attributes); |
| 3476 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3477 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3478 | |
| 3479 | MUST_USE_RESULT PropertyAttributes JSProxy::GetElementAttributeWithHandler( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3480 | JSReceiver* receiver_raw, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3481 | uint32_t index) { |
| 3482 | Isolate* isolate = GetIsolate(); |
| 3483 | HandleScope scope(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3484 | Handle<JSProxy> proxy(this); |
| 3485 | Handle<JSReceiver> receiver(receiver_raw); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3486 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3487 | return proxy->GetPropertyAttributeWithHandler(*receiver, *name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3491 | void JSProxy::Fix() { |
| 3492 | Isolate* isolate = GetIsolate(); |
| 3493 | HandleScope scope(isolate); |
| 3494 | Handle<JSProxy> self(this); |
| 3495 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3496 | // Save identity hash. |
| 3497 | MaybeObject* maybe_hash = GetIdentityHash(OMIT_CREATION); |
| 3498 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3499 | if (IsJSFunctionProxy()) { |
| 3500 | isolate->factory()->BecomeJSFunction(self); |
| 3501 | // Code will be set on the JavaScript side. |
| 3502 | } else { |
| 3503 | isolate->factory()->BecomeJSObject(self); |
| 3504 | } |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 3505 | ASSERT(self->IsJSObject()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3506 | |
| 3507 | // Inherit identity, if it was present. |
| 3508 | Object* hash; |
| 3509 | if (maybe_hash->To<Object>(&hash) && hash->IsSmi()) { |
| 3510 | Handle<JSObject> new_self(JSObject::cast(*self)); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 3511 | isolate->factory()->SetIdentityHash(new_self, Smi::cast(hash)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3512 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3516 | MUST_USE_RESULT Handle<Object> JSProxy::CallTrap(const char* name, |
| 3517 | Handle<Object> derived, |
| 3518 | int argc, |
| 3519 | Handle<Object> argv[]) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3520 | Isolate* isolate = GetIsolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3521 | Handle<Object> handler(this->handler(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3522 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3523 | Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3524 | Handle<Object> trap(v8::internal::GetProperty(isolate, handler, trap_name)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3525 | if (isolate->has_pending_exception()) return trap; |
| 3526 | |
| 3527 | if (trap->IsUndefined()) { |
| 3528 | if (derived.is_null()) { |
| 3529 | Handle<Object> args[] = { handler, trap_name }; |
| 3530 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3531 | "handler_trap_missing", HandleVector(args, ARRAY_SIZE(args))); |
| 3532 | isolate->Throw(*error); |
| 3533 | return Handle<Object>(); |
| 3534 | } |
| 3535 | trap = Handle<Object>(derived); |
| 3536 | } |
| 3537 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3538 | bool threw; |
| 3539 | return Execution::Call(trap, handler, argc, argv, &threw); |
| 3540 | } |
| 3541 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3542 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3543 | void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3544 | CALL_HEAP_FUNCTION_VOID( |
| 3545 | object->GetIsolate(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3546 | object->AllocateStorageForMap(*map)); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3550 | void JSObject::MigrateInstance(Handle<JSObject> object) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3551 | if (FLAG_trace_migration) { |
| 3552 | PrintF("migrating instance %p (%p)\n", |
| 3553 | static_cast<void*>(*object), |
| 3554 | static_cast<void*>(object->map())); |
| 3555 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3556 | CALL_HEAP_FUNCTION_VOID( |
| 3557 | object->GetIsolate(), |
| 3558 | object->MigrateInstance()); |
| 3559 | } |
| 3560 | |
| 3561 | |
| 3562 | Handle<Map> Map::GeneralizeRepresentation(Handle<Map> map, |
| 3563 | int modify_index, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3564 | Representation representation) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3565 | CALL_HEAP_FUNCTION( |
| 3566 | map->GetIsolate(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3567 | map->GeneralizeRepresentation(modify_index, representation), |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3568 | Map); |
| 3569 | } |
| 3570 | |
| 3571 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3572 | MaybeObject* JSObject::SetPropertyForResult(LookupResult* lookup, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3573 | Name* name_raw, |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3574 | Object* value_raw, |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 3575 | PropertyAttributes attributes, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3576 | StrictModeFlag strict_mode, |
| 3577 | StoreFromKeyed store_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3578 | Heap* heap = GetHeap(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3579 | Isolate* isolate = heap->isolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3580 | // Make sure that the top context does not change when doing callbacks or |
| 3581 | // interceptor calls. |
| 3582 | AssertNoContextChange ncc; |
| 3583 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3584 | // Optimization for 2-byte strings often used as keys in a decompression |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3585 | // dictionary. We internalize these short keys to avoid constantly |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3586 | // reallocating them. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3587 | if (name_raw->IsString() && !name_raw->IsInternalizedString() && |
| 3588 | String::cast(name_raw)->length() <= 2) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3589 | Object* internalized_version; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3590 | { MaybeObject* maybe_string_version = |
| 3591 | heap->InternalizeString(String::cast(name_raw)); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3592 | if (maybe_string_version->ToObject(&internalized_version)) { |
| 3593 | name_raw = String::cast(internalized_version); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3594 | } |
| 3595 | } |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3598 | // Check access rights if needed. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3599 | if (IsAccessCheckNeeded()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3600 | if (!isolate->MayNamedAccess(this, name_raw, v8::ACCESS_SET)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3601 | return SetPropertyWithFailedAccessCheck( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3602 | lookup, name_raw, value_raw, true, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3603 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3604 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3605 | |
| 3606 | if (IsJSGlobalProxy()) { |
| 3607 | Object* proto = GetPrototype(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3608 | if (proto->IsNull()) return value_raw; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3609 | ASSERT(proto->IsJSGlobalObject()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3610 | return JSObject::cast(proto)->SetPropertyForResult( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3611 | lookup, name_raw, value_raw, attributes, strict_mode, store_mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3614 | ASSERT(!lookup->IsFound() || lookup->holder() == this || |
| 3615 | lookup->holder()->map()->is_hidden_prototype()); |
| 3616 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3617 | // From this point on everything needs to be handlified, because |
| 3618 | // SetPropertyViaPrototypes might call back into JavaScript. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3619 | HandleScope scope(isolate); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3620 | Handle<JSObject> self(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3621 | Handle<Name> name(name_raw); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3622 | Handle<Object> value(value_raw, isolate); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3623 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3624 | if (!lookup->IsProperty() && !self->IsJSContextExtensionObject()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3625 | bool done = false; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3626 | MaybeObject* result_object = self->SetPropertyViaPrototypes( |
| 3627 | *name, *value, attributes, strict_mode, &done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3628 | if (done) return result_object; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3629 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3630 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3631 | if (!lookup->IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3632 | // Neither properties nor transitions found. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3633 | return self->AddProperty( |
| 3634 | *name, *value, attributes, strict_mode, store_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3635 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3636 | |
| 3637 | if (lookup->IsProperty() && lookup->IsReadOnly()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3638 | if (strict_mode == kStrictMode) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3639 | Handle<Object> args[] = { name, self }; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3640 | return isolate->Throw(*isolate->factory()->NewTypeError( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3641 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args)))); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3642 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3643 | return *value; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3644 | } |
| 3645 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3646 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3647 | Handle<Object> old_value(heap->the_hole_value(), isolate); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3648 | if (FLAG_harmony_observation && |
| 3649 | map()->is_observed() && lookup->IsDataProperty()) { |
| 3650 | old_value = Object::GetProperty(self, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3653 | // This is a real property that is not read-only, or it is a |
| 3654 | // transition or null descriptor and there are no setters in the prototypes. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3655 | MaybeObject* result = *value; |
| 3656 | switch (lookup->type()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3657 | case NORMAL: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3658 | result = lookup->holder()->SetNormalizedProperty(lookup, *value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3659 | break; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3660 | case FIELD: { |
| 3661 | Representation representation = lookup->representation(); |
| 3662 | if (!value->FitsRepresentation(representation)) { |
| 3663 | MaybeObject* maybe_failure = |
| 3664 | lookup->holder()->GeneralizeFieldRepresentation( |
| 3665 | lookup->GetDescriptorIndex(), value->OptimalRepresentation()); |
| 3666 | if (maybe_failure->IsFailure()) return maybe_failure; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3667 | DescriptorArray* desc = lookup->holder()->map()->instance_descriptors(); |
| 3668 | int descriptor = lookup->GetDescriptorIndex(); |
| 3669 | representation = desc->GetDetails(descriptor).representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3670 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3671 | if (FLAG_track_double_fields && representation.IsDouble()) { |
| 3672 | HeapNumber* storage = |
| 3673 | HeapNumber::cast(lookup->holder()->RawFastPropertyAt( |
| 3674 | lookup->GetFieldIndex().field_index())); |
| 3675 | storage->set_value(value->Number()); |
| 3676 | result = *value; |
| 3677 | break; |
| 3678 | } |
| 3679 | lookup->holder()->FastPropertyAtPut( |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 3680 | lookup->GetFieldIndex().field_index(), *value); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3681 | result = *value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3682 | break; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3683 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3684 | case CONSTANT_FUNCTION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3685 | // Only replace the function if necessary. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3686 | if (*value == lookup->GetConstantFunction()) return *value; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3687 | // Preserve the attributes of this existing property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3688 | attributes = lookup->GetAttributes(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3689 | result = |
| 3690 | lookup->holder()->ConvertDescriptorToField(*name, *value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3691 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3692 | case CALLBACKS: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3693 | Object* callback_object = lookup->GetCallbackObject(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3694 | return self->SetPropertyWithCallback( |
| 3695 | callback_object, *name, *value, lookup->holder(), strict_mode); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3696 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3697 | case INTERCEPTOR: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3698 | result = lookup->holder()->SetPropertyWithInterceptor( |
| 3699 | *name, *value, attributes, strict_mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3700 | break; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3701 | case TRANSITION: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3702 | Map* transition_map = lookup->GetTransitionTarget(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 3703 | int descriptor = transition_map->LastAdded(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3704 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3705 | DescriptorArray* descriptors = transition_map->instance_descriptors(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3706 | PropertyDetails details = descriptors->GetDetails(descriptor); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3707 | |
| 3708 | if (details.type() == FIELD) { |
| 3709 | if (attributes == details.attributes()) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3710 | Representation representation = details.representation(); |
| 3711 | if (!value->FitsRepresentation(representation)) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3712 | MaybeObject* maybe_map = transition_map->GeneralizeRepresentation( |
| 3713 | descriptor, value->OptimalRepresentation()); |
| 3714 | if (!maybe_map->To(&transition_map)) return maybe_map; |
| 3715 | Object* back = transition_map->GetBackPointer(); |
| 3716 | if (back->IsMap()) { |
| 3717 | MaybeObject* maybe_failure = |
| 3718 | lookup->holder()->MigrateToMap(Map::cast(back)); |
| 3719 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 3720 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3721 | DescriptorArray* desc = transition_map->instance_descriptors(); |
| 3722 | int descriptor = transition_map->LastAdded(); |
| 3723 | representation = desc->GetDetails(descriptor).representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3724 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3725 | int field_index = descriptors->GetFieldIndex(descriptor); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3726 | result = lookup->holder()->AddFastPropertyUsingMap( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3727 | transition_map, *name, *value, field_index, representation); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3728 | } else { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3729 | result = lookup->holder()->ConvertDescriptorToField( |
| 3730 | *name, *value, attributes); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3731 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3732 | } else if (details.type() == CALLBACKS) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3733 | result = lookup->holder()->ConvertDescriptorToField( |
| 3734 | *name, *value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3735 | } else { |
| 3736 | ASSERT(details.type() == CONSTANT_FUNCTION); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3737 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3738 | Object* constant_function = descriptors->GetValue(descriptor); |
| 3739 | if (constant_function == *value) { |
| 3740 | // If the same constant function is being added we can simply |
| 3741 | // transition to the target map. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3742 | lookup->holder()->set_map(transition_map); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3743 | result = constant_function; |
| 3744 | } else { |
| 3745 | // Otherwise, replace with a map transition to a new map with a FIELD, |
| 3746 | // even if the value is a constant function. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3747 | result = lookup->holder()->ConvertTransitionToMapTransition( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3748 | lookup->GetTransitionIndex(), *name, *value, attributes); |
| 3749 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 3750 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3751 | break; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 3752 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3753 | case HANDLER: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 3754 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3755 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3756 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3757 | |
| 3758 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3759 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3760 | |
| 3761 | if (FLAG_harmony_observation && map()->is_observed()) { |
| 3762 | if (lookup->IsTransition()) { |
| 3763 | EnqueueChangeRecord(self, "new", name, old_value); |
| 3764 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3765 | LookupResult new_lookup(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 3766 | self->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3767 | if (new_lookup.IsDataProperty()) { |
| 3768 | Handle<Object> new_value = Object::GetProperty(self, name); |
| 3769 | if (!new_value->SameValue(*old_value)) { |
| 3770 | EnqueueChangeRecord(self, "updated", name, old_value); |
| 3771 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | |
| 3780 | // Set a real local property, even if it is READ_ONLY. If the property is not |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3781 | // present, add it with attributes NONE. This code is an exact clone of |
| 3782 | // SetProperty, with the check for IsReadOnly and the check for a |
| 3783 | // callback setter removed. The two lines looking up the LookupResult |
| 3784 | // result are also added. If one of the functions is changed, the other |
| 3785 | // should be. |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 3786 | // Note that this method cannot be used to set the prototype of a function |
| 3787 | // because ConvertDescriptorToField() which is called in "case CALLBACKS:" |
| 3788 | // doesn't handle function prototypes correctly. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3789 | Handle<Object> JSObject::SetLocalPropertyIgnoreAttributes( |
| 3790 | Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3791 | Handle<Name> key, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3792 | Handle<Object> value, |
| 3793 | PropertyAttributes attributes) { |
| 3794 | CALL_HEAP_FUNCTION( |
| 3795 | object->GetIsolate(), |
| 3796 | object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes), |
| 3797 | Object); |
| 3798 | } |
| 3799 | |
| 3800 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3801 | MaybeObject* JSObject::SetLocalPropertyIgnoreAttributes( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3802 | Name* name_raw, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3803 | Object* value_raw, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3804 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3805 | // Make sure that the top context does not change when doing callbacks or |
| 3806 | // interceptor calls. |
| 3807 | AssertNoContextChange ncc; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3808 | Isolate* isolate = GetIsolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3809 | LookupResult lookup(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 3810 | LocalLookup(name_raw, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3811 | if (!lookup.IsFound()) map()->LookupTransition(this, name_raw, &lookup); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3812 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3813 | if (IsAccessCheckNeeded()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3814 | if (!isolate->MayNamedAccess(this, name_raw, v8::ACCESS_SET)) { |
| 3815 | return SetPropertyWithFailedAccessCheck(&lookup, |
| 3816 | name_raw, |
| 3817 | value_raw, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3818 | false, |
| 3819 | kNonStrictMode); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3820 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3821 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3822 | |
| 3823 | if (IsJSGlobalProxy()) { |
| 3824 | Object* proto = GetPrototype(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3825 | if (proto->IsNull()) return value_raw; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3826 | ASSERT(proto->IsJSGlobalObject()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3827 | return JSObject::cast(proto)->SetLocalPropertyIgnoreAttributes( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3828 | name_raw, |
| 3829 | value_raw, |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3830 | attributes); |
| 3831 | } |
| 3832 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3833 | // Check for accessor in prototype chain removed here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3834 | if (!lookup.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3835 | // Neither properties nor transitions found. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3836 | return AddProperty(name_raw, value_raw, attributes, kNonStrictMode); |
| 3837 | } |
| 3838 | |
| 3839 | // From this point on everything needs to be handlified. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3840 | HandleScope scope(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3841 | Handle<JSObject> self(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3842 | Handle<Name> name(name_raw); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3843 | Handle<Object> value(value_raw, isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3844 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3845 | Handle<Object> old_value(isolate->heap()->the_hole_value(), isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3846 | PropertyAttributes old_attributes = ABSENT; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 3847 | bool is_observed = FLAG_harmony_observation && self->map()->is_observed(); |
| 3848 | if (is_observed) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3849 | if (lookup.IsDataProperty()) old_value = Object::GetProperty(self, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3850 | old_attributes = lookup.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3851 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 3852 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3853 | // Check of IsReadOnly removed from here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3854 | MaybeObject* result = *value; |
| 3855 | switch (lookup.type()) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3856 | case NORMAL: { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3857 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3858 | result = self->SetNormalizedProperty(*name, *value, details); |
| 3859 | break; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3860 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3861 | case FIELD: { |
| 3862 | Representation representation = lookup.representation(); |
| 3863 | if (!value->FitsRepresentation(representation)) { |
| 3864 | MaybeObject* maybe_failure = self->GeneralizeFieldRepresentation( |
| 3865 | lookup.GetDescriptorIndex(), value->OptimalRepresentation()); |
| 3866 | if (maybe_failure->IsFailure()) return maybe_failure; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3867 | DescriptorArray* desc = self->map()->instance_descriptors(); |
| 3868 | int descriptor = lookup.GetDescriptorIndex(); |
| 3869 | representation = desc->GetDetails(descriptor).representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3870 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3871 | if (FLAG_track_double_fields && representation.IsDouble()) { |
| 3872 | HeapNumber* storage = |
| 3873 | HeapNumber::cast(self->RawFastPropertyAt( |
| 3874 | lookup.GetFieldIndex().field_index())); |
| 3875 | storage->set_value(value->Number()); |
| 3876 | result = *value; |
| 3877 | break; |
| 3878 | } |
| 3879 | self->FastPropertyAtPut(lookup.GetFieldIndex().field_index(), *value); |
| 3880 | result = *value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3881 | break; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3882 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3883 | case CONSTANT_FUNCTION: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3884 | // Only replace the function if necessary. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3885 | if (*value != lookup.GetConstantFunction()) { |
| 3886 | // Preserve the attributes of this existing property. |
| 3887 | attributes = lookup.GetAttributes(); |
| 3888 | result = self->ConvertDescriptorToField(*name, *value, attributes); |
| 3889 | } |
| 3890 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3891 | case CALLBACKS: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3892 | case INTERCEPTOR: |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3893 | // Override callback in clone |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3894 | result = self->ConvertDescriptorToField(*name, *value, attributes); |
| 3895 | break; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3896 | case TRANSITION: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3897 | Map* transition_map = lookup.GetTransitionTarget(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 3898 | int descriptor = transition_map->LastAdded(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3899 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3900 | DescriptorArray* descriptors = transition_map->instance_descriptors(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3901 | PropertyDetails details = descriptors->GetDetails(descriptor); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3902 | |
| 3903 | if (details.type() == FIELD) { |
| 3904 | if (attributes == details.attributes()) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3905 | Representation representation = details.representation(); |
| 3906 | if (!value->FitsRepresentation(representation)) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3907 | MaybeObject* maybe_map = transition_map->GeneralizeRepresentation( |
| 3908 | descriptor, value->OptimalRepresentation()); |
| 3909 | if (!maybe_map->To(&transition_map)) return maybe_map; |
| 3910 | Object* back = transition_map->GetBackPointer(); |
| 3911 | if (back->IsMap()) { |
| 3912 | MaybeObject* maybe_failure = self->MigrateToMap(Map::cast(back)); |
| 3913 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 3914 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3915 | DescriptorArray* desc = transition_map->instance_descriptors(); |
| 3916 | int descriptor = transition_map->LastAdded(); |
| 3917 | representation = desc->GetDetails(descriptor).representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3918 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3919 | int field_index = descriptors->GetFieldIndex(descriptor); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3920 | result = self->AddFastPropertyUsingMap( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 3921 | transition_map, *name, *value, field_index, representation); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3922 | } else { |
| 3923 | result = self->ConvertDescriptorToField(*name, *value, attributes); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3924 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3925 | } else if (details.type() == CALLBACKS) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3926 | result = self->ConvertDescriptorToField(*name, *value, attributes); |
| 3927 | } else { |
| 3928 | ASSERT(details.type() == CONSTANT_FUNCTION); |
| 3929 | |
| 3930 | // Replace transition to CONSTANT FUNCTION with a map transition to a |
| 3931 | // new map with a FIELD, even if the value is a function. |
| 3932 | result = self->ConvertTransitionToMapTransition( |
| 3933 | lookup.GetTransitionIndex(), *name, *value, attributes); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3934 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3935 | break; |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3936 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 3937 | case HANDLER: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 3938 | case NONEXISTENT: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3939 | UNREACHABLE(); |
| 3940 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3941 | |
| 3942 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 3943 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3944 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 3945 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3946 | if (lookup.IsTransition()) { |
| 3947 | EnqueueChangeRecord(self, "new", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3948 | } else if (old_value->IsTheHole()) { |
| 3949 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3950 | } else { |
| 3951 | LookupResult new_lookup(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 3952 | self->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3953 | bool value_changed = false; |
| 3954 | if (new_lookup.IsDataProperty()) { |
| 3955 | Handle<Object> new_value = Object::GetProperty(self, name); |
| 3956 | value_changed = !old_value->SameValue(*new_value); |
| 3957 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3958 | if (new_lookup.GetAttributes() != old_attributes) { |
| 3959 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3960 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3961 | } else if (value_changed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3962 | EnqueueChangeRecord(self, "updated", name, old_value); |
| 3963 | } |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | |
| 3971 | PropertyAttributes JSObject::GetPropertyAttributePostInterceptor( |
| 3972 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3973 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3974 | bool continue_search) { |
| 3975 | // Check local property, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3976 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3977 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3978 | if (result.IsFound()) return result.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3979 | |
| 3980 | if (continue_search) { |
| 3981 | // Continue searching via the prototype chain. |
| 3982 | Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3983 | if (!pt->IsNull()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3984 | return JSObject::cast(pt)-> |
| 3985 | GetPropertyAttributeWithReceiver(receiver, name); |
| 3986 | } |
| 3987 | } |
| 3988 | return ABSENT; |
| 3989 | } |
| 3990 | |
| 3991 | |
| 3992 | PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor( |
| 3993 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3994 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3995 | bool continue_search) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3996 | // TODO(rossberg): Support symbols in the API. |
| 3997 | if (name->IsSymbol()) return ABSENT; |
| 3998 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3999 | Isolate* isolate = GetIsolate(); |
| 4000 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4001 | // Make sure that the top context does not change when doing |
| 4002 | // callbacks or interceptor calls. |
| 4003 | AssertNoContextChange ncc; |
| 4004 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4005 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4006 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 4007 | Handle<JSObject> receiver_handle(receiver); |
| 4008 | Handle<JSObject> holder_handle(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4009 | Handle<String> name_handle(String::cast(name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4010 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4011 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | if (!interceptor->query()->IsUndefined()) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4013 | v8::NamedPropertyQuery query = |
| 4014 | v8::ToCData<v8::NamedPropertyQuery>(interceptor->query()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4015 | LOG(isolate, |
| 4016 | ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name)); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4017 | v8::Handle<v8::Integer> result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4018 | { |
| 4019 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4020 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | result = query(v8::Utils::ToLocal(name_handle), info); |
| 4022 | } |
| 4023 | if (!result.IsEmpty()) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4024 | ASSERT(result->IsInt32()); |
| 4025 | return static_cast<PropertyAttributes>(result->Int32Value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4026 | } |
| 4027 | } else if (!interceptor->getter()->IsUndefined()) { |
| 4028 | v8::NamedPropertyGetter getter = |
| 4029 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4030 | LOG(isolate, |
| 4031 | ApiNamedPropertyAccess("interceptor-named-get-has", this, name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | v8::Handle<v8::Value> result; |
| 4033 | { |
| 4034 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4035 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4036 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 4037 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4038 | if (!result.IsEmpty()) return DONT_ENUM; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4039 | } |
| 4040 | return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle, |
| 4041 | *name_handle, |
| 4042 | continue_search); |
| 4043 | } |
| 4044 | |
| 4045 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4046 | PropertyAttributes JSReceiver::GetPropertyAttributeWithReceiver( |
| 4047 | JSReceiver* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4048 | Name* key) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4049 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4050 | if (IsJSObject() && key->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4051 | return JSObject::cast(this)->GetElementAttributeWithReceiver( |
| 4052 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4053 | } |
| 4054 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4055 | LookupResult lookup(GetIsolate()); |
| 4056 | Lookup(key, &lookup); |
| 4057 | return GetPropertyAttributeForResult(receiver, &lookup, key, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4061 | PropertyAttributes JSReceiver::GetPropertyAttributeForResult( |
| 4062 | JSReceiver* receiver, |
| 4063 | LookupResult* lookup, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4064 | Name* name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4065 | bool continue_search) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4066 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4067 | if (IsAccessCheckNeeded()) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4068 | JSObject* this_obj = JSObject::cast(this); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4069 | Heap* heap = GetHeap(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4070 | if (!heap->isolate()->MayNamedAccess(this_obj, name, v8::ACCESS_HAS)) { |
| 4071 | return this_obj->GetPropertyAttributeWithFailedAccessCheck( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4072 | receiver, lookup, name, continue_search); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4073 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4075 | if (lookup->IsFound()) { |
| 4076 | switch (lookup->type()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4077 | case NORMAL: // fall through |
| 4078 | case FIELD: |
| 4079 | case CONSTANT_FUNCTION: |
| 4080 | case CALLBACKS: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4081 | return lookup->GetAttributes(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4082 | case HANDLER: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4083 | return JSProxy::cast(lookup->proxy())->GetPropertyAttributeWithHandler( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4084 | receiver, name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4085 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4086 | case INTERCEPTOR: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4087 | return lookup->holder()->GetPropertyAttributeWithInterceptor( |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4088 | JSObject::cast(receiver), name, continue_search); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4089 | case TRANSITION: |
| 4090 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4091 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | return ABSENT; |
| 4095 | } |
| 4096 | |
| 4097 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4098 | PropertyAttributes JSReceiver::GetLocalPropertyAttribute(Name* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4099 | // Check whether the name is an array index. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4100 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4101 | if (IsJSObject() && name->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4102 | return GetLocalElementAttribute(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4103 | } |
| 4104 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4105 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 4106 | LocalLookup(name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4107 | return GetPropertyAttributeForResult(this, &lookup, name, false); |
| 4108 | } |
| 4109 | |
| 4110 | |
| 4111 | PropertyAttributes JSObject::GetElementAttributeWithReceiver( |
| 4112 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4113 | Isolate* isolate = GetIsolate(); |
| 4114 | |
| 4115 | // Check access rights if needed. |
| 4116 | if (IsAccessCheckNeeded()) { |
| 4117 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 4118 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 4119 | return ABSENT; |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | if (IsJSGlobalProxy()) { |
| 4124 | Object* proto = GetPrototype(); |
| 4125 | if (proto->IsNull()) return ABSENT; |
| 4126 | ASSERT(proto->IsJSGlobalObject()); |
| 4127 | return JSObject::cast(proto)->GetElementAttributeWithReceiver( |
| 4128 | receiver, index, continue_search); |
| 4129 | } |
| 4130 | |
| 4131 | // Check for lookup interceptor except when bootstrapping. |
| 4132 | if (HasIndexedInterceptor() && !isolate->bootstrapper()->IsActive()) { |
| 4133 | return GetElementAttributeWithInterceptor(receiver, index, continue_search); |
| 4134 | } |
| 4135 | |
| 4136 | return GetElementAttributeWithoutInterceptor( |
| 4137 | receiver, index, continue_search); |
| 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | PropertyAttributes JSObject::GetElementAttributeWithInterceptor( |
| 4142 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4143 | Isolate* isolate = GetIsolate(); |
| 4144 | // Make sure that the top context does not change when doing |
| 4145 | // callbacks or interceptor calls. |
| 4146 | AssertNoContextChange ncc; |
| 4147 | HandleScope scope(isolate); |
| 4148 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 4149 | Handle<JSReceiver> hreceiver(receiver); |
| 4150 | Handle<JSObject> holder(this); |
| 4151 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
| 4152 | v8::AccessorInfo info(args.end()); |
| 4153 | if (!interceptor->query()->IsUndefined()) { |
| 4154 | v8::IndexedPropertyQuery query = |
| 4155 | v8::ToCData<v8::IndexedPropertyQuery>(interceptor->query()); |
| 4156 | LOG(isolate, |
| 4157 | ApiIndexedPropertyAccess("interceptor-indexed-has", this, index)); |
| 4158 | v8::Handle<v8::Integer> result; |
| 4159 | { |
| 4160 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4161 | VMState<EXTERNAL> state(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4162 | result = query(index, info); |
| 4163 | } |
| 4164 | if (!result.IsEmpty()) |
| 4165 | return static_cast<PropertyAttributes>(result->Int32Value()); |
| 4166 | } else if (!interceptor->getter()->IsUndefined()) { |
| 4167 | v8::IndexedPropertyGetter getter = |
| 4168 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
| 4169 | LOG(isolate, |
| 4170 | ApiIndexedPropertyAccess("interceptor-indexed-get-has", this, index)); |
| 4171 | v8::Handle<v8::Value> result; |
| 4172 | { |
| 4173 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4174 | VMState<EXTERNAL> state(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4175 | result = getter(index, info); |
| 4176 | } |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 4177 | if (!result.IsEmpty()) return NONE; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | return holder->GetElementAttributeWithoutInterceptor( |
| 4181 | *hreceiver, index, continue_search); |
| 4182 | } |
| 4183 | |
| 4184 | |
| 4185 | PropertyAttributes JSObject::GetElementAttributeWithoutInterceptor( |
| 4186 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4187 | PropertyAttributes attr = GetElementsAccessor()->GetAttributes( |
| 4188 | receiver, this, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4189 | if (attr != ABSENT) return attr; |
| 4190 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4191 | // Handle [] on String objects. |
| 4192 | if (IsStringObjectWithCharacterAt(index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4193 | return static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 4194 | } |
| 4195 | |
| 4196 | if (!continue_search) return ABSENT; |
| 4197 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4198 | Object* pt = GetPrototype(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4199 | if (pt->IsJSProxy()) { |
| 4200 | // We need to follow the spec and simulate a call to [[GetOwnProperty]]. |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4201 | return JSProxy::cast(pt)->GetElementAttributeWithHandler(receiver, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4202 | } |
| 4203 | if (pt->IsNull()) return ABSENT; |
| 4204 | return JSObject::cast(pt)->GetElementAttributeWithReceiver( |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4205 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4209 | MaybeObject* NormalizedMapCache::Get(JSObject* obj, |
| 4210 | PropertyNormalizationMode mode) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4211 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4212 | Map* fast = obj->map(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4213 | int index = fast->Hash() % kEntries; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4214 | Object* result = get(index); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4215 | if (result->IsMap() && |
| 4216 | Map::cast(result)->EquivalentToForNormalization(fast, mode)) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4217 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4218 | if (FLAG_verify_heap) { |
| 4219 | Map::cast(result)->SharedMapVerify(); |
| 4220 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4221 | #endif |
| 4222 | #ifdef DEBUG |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4223 | if (FLAG_enable_slow_asserts) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4224 | // The cached map should match newly created normalized map bit-by-bit, |
| 4225 | // except for the code cache, which can contain some ics which can be |
| 4226 | // applied to the shared map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4227 | Object* fresh; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4228 | MaybeObject* maybe_fresh = |
| 4229 | fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP); |
| 4230 | if (maybe_fresh->ToObject(&fresh)) { |
| 4231 | ASSERT(memcmp(Map::cast(fresh)->address(), |
| 4232 | Map::cast(result)->address(), |
| 4233 | Map::kCodeCacheOffset) == 0); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4234 | STATIC_ASSERT(Map::kDependentCodeOffset == |
| 4235 | Map::kCodeCacheOffset + kPointerSize); |
| 4236 | int offset = Map::kDependentCodeOffset + kPointerSize; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4237 | ASSERT(memcmp(Map::cast(fresh)->address() + offset, |
| 4238 | Map::cast(result)->address() + offset, |
| 4239 | Map::kSize - offset) == 0); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4240 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4241 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4242 | #endif |
| 4243 | return result; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4244 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4245 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4246 | { MaybeObject* maybe_result = |
| 4247 | fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP); |
| 4248 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4249 | } |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 4250 | ASSERT(Map::cast(result)->is_dictionary_map()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4251 | set(index, result); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4252 | isolate->counters()->normalized_maps()->Increment(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4253 | |
| 4254 | return result; |
| 4255 | } |
| 4256 | |
| 4257 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4258 | void NormalizedMapCache::Clear() { |
| 4259 | int entries = length(); |
| 4260 | for (int i = 0; i != entries; i++) { |
| 4261 | set_undefined(i); |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4266 | void JSObject::UpdateMapCodeCache(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4267 | Handle<Name> name, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4268 | Handle<Code> code) { |
| 4269 | Isolate* isolate = object->GetIsolate(); |
| 4270 | CALL_HEAP_FUNCTION_VOID(isolate, |
| 4271 | object->UpdateMapCodeCache(*name, *code)); |
| 4272 | } |
| 4273 | |
| 4274 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4275 | MaybeObject* JSObject::UpdateMapCodeCache(Name* name, Code* code) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4276 | if (map()->is_shared()) { |
| 4277 | // Fast case maps are never marked as shared. |
| 4278 | ASSERT(!HasFastProperties()); |
| 4279 | // Replace the map with an identical copy that can be safely modified. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4280 | Object* obj; |
| 4281 | { MaybeObject* maybe_obj = map()->CopyNormalized(KEEP_INOBJECT_PROPERTIES, |
| 4282 | UNIQUE_NORMALIZED_MAP); |
| 4283 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4284 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4285 | GetIsolate()->counters()->normalized_maps()->Increment(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4286 | |
| 4287 | set_map(Map::cast(obj)); |
| 4288 | } |
| 4289 | return map()->UpdateCodeCache(name, code); |
| 4290 | } |
| 4291 | |
| 4292 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4293 | void JSObject::NormalizeProperties(Handle<JSObject> object, |
| 4294 | PropertyNormalizationMode mode, |
| 4295 | int expected_additional_properties) { |
| 4296 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 4297 | object->NormalizeProperties( |
| 4298 | mode, expected_additional_properties)); |
| 4299 | } |
| 4300 | |
| 4301 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4302 | MaybeObject* JSObject::NormalizeProperties(PropertyNormalizationMode mode, |
| 4303 | int expected_additional_properties) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4304 | if (!HasFastProperties()) return this; |
| 4305 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4306 | // The global object is always normalized. |
| 4307 | ASSERT(!IsGlobalObject()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4308 | // JSGlobalProxy must never be normalized |
| 4309 | ASSERT(!IsJSGlobalProxy()); |
| 4310 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4311 | Map* map_of_this = map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4312 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4313 | // Allocate new content. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4314 | int real_size = map_of_this->NumberOfOwnDescriptors(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4315 | int property_count = real_size; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4316 | if (expected_additional_properties > 0) { |
| 4317 | property_count += expected_additional_properties; |
| 4318 | } else { |
| 4319 | property_count += 2; // Make space for two more properties. |
| 4320 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4321 | NameDictionary* dictionary; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4322 | MaybeObject* maybe_dictionary = |
| 4323 | NameDictionary::Allocate(GetHeap(), property_count); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4324 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4325 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4326 | DescriptorArray* descs = map_of_this->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4327 | for (int i = 0; i < real_size; i++) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4328 | PropertyDetails details = descs->GetDetails(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4329 | switch (details.type()) { |
| 4330 | case CONSTANT_FUNCTION: { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4331 | PropertyDetails d = PropertyDetails( |
| 4332 | details.attributes(), NORMAL, i + 1); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4333 | Object* value = descs->GetConstantFunction(i); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 4334 | MaybeObject* maybe_dictionary = |
| 4335 | dictionary->Add(descs->GetKey(i), value, d); |
| 4336 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4337 | break; |
| 4338 | } |
| 4339 | case FIELD: { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4340 | PropertyDetails d = |
| 4341 | PropertyDetails(details.attributes(), NORMAL, i + 1); |
| 4342 | Object* value = RawFastPropertyAt(descs->GetFieldIndex(i)); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 4343 | MaybeObject* maybe_dictionary = |
| 4344 | dictionary->Add(descs->GetKey(i), value, d); |
| 4345 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4346 | break; |
| 4347 | } |
| 4348 | case CALLBACKS: { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4349 | Object* value = descs->GetCallbacksObject(i); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4350 | PropertyDetails d = PropertyDetails( |
| 4351 | details.attributes(), CALLBACKS, i + 1); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 4352 | MaybeObject* maybe_dictionary = |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 4353 | dictionary->Add(descs->GetKey(i), value, d); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 4354 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4355 | break; |
| 4356 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4357 | case INTERCEPTOR: |
| 4358 | break; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4359 | case HANDLER: |
| 4360 | case NORMAL: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4361 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4362 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4363 | UNREACHABLE(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4364 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4365 | } |
| 4366 | } |
| 4367 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4368 | Heap* current_heap = GetHeap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4369 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4370 | // Copy the next enumeration index from instance descriptor. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4371 | dictionary->SetNextEnumerationIndex(real_size + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4372 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 4373 | Map* new_map; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4374 | MaybeObject* maybe_map = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4375 | current_heap->isolate()->context()->native_context()-> |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 4376 | normalized_map_cache()->Get(this, mode); |
| 4377 | if (!maybe_map->To(&new_map)) return maybe_map; |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 4378 | ASSERT(new_map->is_dictionary_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4379 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4380 | // We have now successfully allocated all the necessary objects. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4381 | // Changes can now be made with the guarantee that all of them take effect. |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4382 | |
| 4383 | // Resize the object in the heap if necessary. |
| 4384 | int new_instance_size = new_map->instance_size(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4385 | int instance_size_delta = map_of_this->instance_size() - new_instance_size; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4386 | ASSERT(instance_size_delta >= 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4387 | current_heap->CreateFillerObjectAt(this->address() + new_instance_size, |
| 4388 | instance_size_delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4389 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4390 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 4391 | -instance_size_delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4394 | set_map(new_map); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 4395 | map_of_this->NotifyLeafMapLayoutChange(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4396 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4397 | set_properties(dictionary); |
| 4398 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4399 | current_heap->isolate()->counters()->props_to_dictionary()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4400 | |
| 4401 | #ifdef DEBUG |
| 4402 | if (FLAG_trace_normalization) { |
| 4403 | PrintF("Object properties have been normalized:\n"); |
| 4404 | Print(); |
| 4405 | } |
| 4406 | #endif |
| 4407 | return this; |
| 4408 | } |
| 4409 | |
| 4410 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4411 | void JSObject::TransformToFastProperties(Handle<JSObject> object, |
| 4412 | int unused_property_fields) { |
| 4413 | CALL_HEAP_FUNCTION_VOID( |
| 4414 | object->GetIsolate(), |
| 4415 | object->TransformToFastProperties(unused_property_fields)); |
| 4416 | } |
| 4417 | |
| 4418 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4419 | MaybeObject* JSObject::TransformToFastProperties(int unused_property_fields) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4420 | if (HasFastProperties()) return this; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4421 | ASSERT(!IsGlobalObject()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4422 | return property_dictionary()-> |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4423 | TransformPropertiesToFastFor(this, unused_property_fields); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4427 | Handle<SeededNumberDictionary> JSObject::NormalizeElements( |
| 4428 | Handle<JSObject> object) { |
| 4429 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 4430 | object->NormalizeElements(), |
| 4431 | SeededNumberDictionary); |
| 4432 | } |
| 4433 | |
| 4434 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4435 | MaybeObject* JSObject::NormalizeElements() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4436 | ASSERT(!HasExternalArrayElements()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 4437 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4438 | // Find the backing store. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4439 | FixedArrayBase* array = FixedArrayBase::cast(elements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4440 | Map* old_map = array->map(); |
| 4441 | bool is_arguments = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4442 | (old_map == old_map->GetHeap()->non_strict_arguments_elements_map()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4443 | if (is_arguments) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4444 | array = FixedArrayBase::cast(FixedArray::cast(array)->get(1)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4445 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4446 | if (array->IsDictionary()) return array; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4447 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4448 | ASSERT(HasFastSmiOrObjectElements() || |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4449 | HasFastDoubleElements() || |
| 4450 | HasFastArgumentsElements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4451 | // Compute the effective length and allocate a new backing store. |
| 4452 | int length = IsJSArray() |
| 4453 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 4454 | : array->length(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 4455 | int old_capacity = 0; |
| 4456 | int used_elements = 0; |
| 4457 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4458 | SeededNumberDictionary* dictionary = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4459 | { Object* object; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4460 | MaybeObject* maybe = |
| 4461 | SeededNumberDictionary::Allocate(GetHeap(), used_elements); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4462 | if (!maybe->ToObject(&object)) return maybe; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4463 | dictionary = SeededNumberDictionary::cast(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4464 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4465 | |
| 4466 | // Copy the elements to the new backing store. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4467 | bool has_double_elements = array->IsFixedDoubleArray(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4468 | for (int i = 0; i < length; i++) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4469 | Object* value = NULL; |
| 4470 | if (has_double_elements) { |
| 4471 | FixedDoubleArray* double_array = FixedDoubleArray::cast(array); |
| 4472 | if (double_array->is_the_hole(i)) { |
| 4473 | value = GetIsolate()->heap()->the_hole_value(); |
| 4474 | } else { |
| 4475 | // Objects must be allocated in the old object space, since the |
| 4476 | // overall number of HeapNumbers needed for the conversion might |
| 4477 | // exceed the capacity of new space, and we would fail repeatedly |
| 4478 | // trying to convert the FixedDoubleArray. |
| 4479 | MaybeObject* maybe_value_object = |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4480 | GetHeap()->AllocateHeapNumber(double_array->get_scalar(i), TENURED); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4481 | if (!maybe_value_object->ToObject(&value)) return maybe_value_object; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4482 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4483 | } else { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4484 | ASSERT(old_map->has_fast_smi_or_object_elements()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4485 | value = FixedArray::cast(array)->get(i); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4486 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4487 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4488 | if (!value->IsTheHole()) { |
| 4489 | Object* result; |
| 4490 | MaybeObject* maybe_result = |
| 4491 | dictionary->AddNumberEntry(i, value, details); |
| 4492 | if (!maybe_result->ToObject(&result)) return maybe_result; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4493 | dictionary = SeededNumberDictionary::cast(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4494 | } |
| 4495 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4496 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4497 | // Switch to using the dictionary as the backing storage for elements. |
| 4498 | if (is_arguments) { |
| 4499 | FixedArray::cast(elements())->set(1, dictionary); |
| 4500 | } else { |
| 4501 | // Set the new map first to satify the elements type assert in |
| 4502 | // set_elements(). |
| 4503 | Object* new_map; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4504 | MaybeObject* maybe = GetElementsTransitionMap(GetIsolate(), |
| 4505 | DICTIONARY_ELEMENTS); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4506 | if (!maybe->ToObject(&new_map)) return maybe; |
| 4507 | set_map(Map::cast(new_map)); |
| 4508 | set_elements(dictionary); |
| 4509 | } |
| 4510 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4511 | old_map->GetHeap()->isolate()->counters()->elements_to_dictionary()-> |
| 4512 | Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4513 | |
| 4514 | #ifdef DEBUG |
| 4515 | if (FLAG_trace_normalization) { |
| 4516 | PrintF("Object elements have been normalized:\n"); |
| 4517 | Print(); |
| 4518 | } |
| 4519 | #endif |
| 4520 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4521 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
| 4522 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4526 | Smi* JSReceiver::GenerateIdentityHash() { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4527 | Isolate* isolate = GetIsolate(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4528 | |
| 4529 | int hash_value; |
| 4530 | int attempts = 0; |
| 4531 | do { |
| 4532 | // Generate a random 32-bit hash value but limit range to fit |
| 4533 | // within a smi. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4534 | hash_value = V8::RandomPrivate(isolate) & Smi::kMaxValue; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4535 | attempts++; |
| 4536 | } while (hash_value == 0 && attempts < 30); |
| 4537 | hash_value = hash_value != 0 ? hash_value : 1; // never return 0 |
| 4538 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4539 | return Smi::FromInt(hash_value); |
| 4540 | } |
| 4541 | |
| 4542 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4543 | MaybeObject* JSObject::SetIdentityHash(Smi* hash, CreationFlag flag) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4544 | MaybeObject* maybe = SetHiddenProperty(GetHeap()->identity_hash_string(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4545 | hash); |
| 4546 | if (maybe->IsFailure()) return maybe; |
| 4547 | return this; |
| 4548 | } |
| 4549 | |
| 4550 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4551 | int JSObject::GetIdentityHash(Handle<JSObject> obj) { |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 4552 | CALL_AND_RETRY_OR_DIE(obj->GetIsolate(), |
| 4553 | obj->GetIdentityHash(ALLOW_CREATION), |
| 4554 | return Smi::cast(__object__)->value(), |
| 4555 | return 0); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4559 | MaybeObject* JSObject::GetIdentityHash(CreationFlag flag) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4560 | Object* stored_value = GetHiddenProperty(GetHeap()->identity_hash_string()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4561 | if (stored_value->IsSmi()) return stored_value; |
| 4562 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4563 | // Do not generate permanent identity hash code if not requested. |
| 4564 | if (flag == OMIT_CREATION) return GetHeap()->undefined_value(); |
| 4565 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4566 | Smi* hash = GenerateIdentityHash(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4567 | MaybeObject* result = SetHiddenProperty(GetHeap()->identity_hash_string(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4568 | hash); |
| 4569 | if (result->IsFailure()) return result; |
| 4570 | if (result->ToObjectUnchecked()->IsUndefined()) { |
| 4571 | // Trying to get hash of detached proxy. |
| 4572 | return Smi::FromInt(0); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4573 | } |
| 4574 | return hash; |
| 4575 | } |
| 4576 | |
| 4577 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4578 | MaybeObject* JSProxy::GetIdentityHash(CreationFlag flag) { |
| 4579 | Object* hash = this->hash(); |
| 4580 | if (!hash->IsSmi() && flag == ALLOW_CREATION) { |
| 4581 | hash = GenerateIdentityHash(); |
| 4582 | set_hash(hash); |
| 4583 | } |
| 4584 | return hash; |
| 4585 | } |
| 4586 | |
| 4587 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4588 | Object* JSObject::GetHiddenProperty(Name* key) { |
| 4589 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4590 | if (IsJSGlobalProxy()) { |
| 4591 | // For a proxy, use the prototype as target object. |
| 4592 | Object* proxy_parent = GetPrototype(); |
| 4593 | // If the proxy is detached, return undefined. |
| 4594 | if (proxy_parent->IsNull()) return GetHeap()->undefined_value(); |
| 4595 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4596 | return JSObject::cast(proxy_parent)->GetHiddenProperty(key); |
| 4597 | } |
| 4598 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4599 | MaybeObject* hidden_lookup = |
| 4600 | GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4601 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
| 4602 | |
| 4603 | if (inline_value->IsSmi()) { |
| 4604 | // Handle inline-stored identity hash. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4605 | if (key == GetHeap()->identity_hash_string()) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4606 | return inline_value; |
| 4607 | } else { |
| 4608 | return GetHeap()->undefined_value(); |
| 4609 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4610 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4611 | |
| 4612 | if (inline_value->IsUndefined()) return GetHeap()->undefined_value(); |
| 4613 | |
| 4614 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
| 4615 | Object* entry = hashtable->Lookup(key); |
| 4616 | if (entry->IsTheHole()) return GetHeap()->undefined_value(); |
| 4617 | return entry; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4621 | Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> obj, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4622 | Handle<Name> key, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4623 | Handle<Object> value) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4624 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 4625 | obj->SetHiddenProperty(*key, *value), |
| 4626 | Object); |
| 4627 | } |
| 4628 | |
| 4629 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4630 | MaybeObject* JSObject::SetHiddenProperty(Name* key, Object* value) { |
| 4631 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4632 | if (IsJSGlobalProxy()) { |
| 4633 | // For a proxy, use the prototype as target object. |
| 4634 | Object* proxy_parent = GetPrototype(); |
| 4635 | // If the proxy is detached, return undefined. |
| 4636 | if (proxy_parent->IsNull()) return GetHeap()->undefined_value(); |
| 4637 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4638 | return JSObject::cast(proxy_parent)->SetHiddenProperty(key, value); |
| 4639 | } |
| 4640 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4641 | MaybeObject* hidden_lookup = |
| 4642 | GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4643 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
| 4644 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4645 | // If there is no backing store yet, store the identity hash inline. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4646 | if (value->IsSmi() && |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4647 | key == GetHeap()->identity_hash_string() && |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4648 | (inline_value->IsUndefined() || inline_value->IsSmi())) { |
| 4649 | return SetHiddenPropertiesHashTable(value); |
| 4650 | } |
| 4651 | |
| 4652 | hidden_lookup = GetHiddenPropertiesHashTable(CREATE_NEW_IF_ABSENT); |
| 4653 | ObjectHashTable* hashtable; |
| 4654 | if (!hidden_lookup->To(&hashtable)) return hidden_lookup; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4655 | |
| 4656 | // If it was found, check if the key is already in the dictionary. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4657 | MaybeObject* insert_result = hashtable->Put(key, value); |
| 4658 | ObjectHashTable* new_table; |
| 4659 | if (!insert_result->To(&new_table)) return insert_result; |
| 4660 | if (new_table != hashtable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4661 | // If adding the key expanded the dictionary (i.e., Add returned a new |
| 4662 | // dictionary), store it back to the object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4663 | MaybeObject* store_result = SetHiddenPropertiesHashTable(new_table); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4664 | if (store_result->IsFailure()) return store_result; |
| 4665 | } |
| 4666 | // Return this to mark success. |
| 4667 | return this; |
| 4668 | } |
| 4669 | |
| 4670 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4671 | void JSObject::DeleteHiddenProperty(Name* key) { |
| 4672 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4673 | if (IsJSGlobalProxy()) { |
| 4674 | // For a proxy, use the prototype as target object. |
| 4675 | Object* proxy_parent = GetPrototype(); |
| 4676 | // If the proxy is detached, return immediately. |
| 4677 | if (proxy_parent->IsNull()) return; |
| 4678 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4679 | JSObject::cast(proxy_parent)->DeleteHiddenProperty(key); |
| 4680 | return; |
| 4681 | } |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4682 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4683 | MaybeObject* hidden_lookup = |
| 4684 | GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4685 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4686 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4687 | // We never delete (inline-stored) identity hashes. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4688 | ASSERT(key != GetHeap()->identity_hash_string()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4689 | if (inline_value->IsUndefined() || inline_value->IsSmi()) return; |
| 4690 | |
| 4691 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4692 | MaybeObject* delete_result = hashtable->Put(key, GetHeap()->the_hole_value()); |
| 4693 | USE(delete_result); |
| 4694 | ASSERT(!delete_result->IsFailure()); // Delete does not cause GC. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | |
| 4698 | bool JSObject::HasHiddenProperties() { |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4699 | return GetPropertyAttributePostInterceptor(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4700 | GetHeap()->hidden_string(), |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4701 | false) != ABSENT; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4705 | MaybeObject* JSObject::GetHiddenPropertiesHashTable( |
| 4706 | InitializeHiddenProperties init_option) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4707 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4708 | Object* inline_value; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4709 | if (HasFastProperties()) { |
| 4710 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4711 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4712 | // hidden strings hash code is zero (and no other name has hash |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4713 | // code zero) it will always occupy the first entry if present. |
| 4714 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4715 | if (descriptors->number_of_descriptors() > 0) { |
| 4716 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4717 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4718 | sorted_index < map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4719 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4720 | MaybeObject* maybe_value = this->FastPropertyAt( |
| 4721 | descriptors->GetDetails(sorted_index).representation(), |
| 4722 | descriptors->GetFieldIndex(sorted_index)); |
| 4723 | if (!maybe_value->To(&inline_value)) return maybe_value; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4724 | } else { |
| 4725 | inline_value = GetHeap()->undefined_value(); |
| 4726 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4727 | } else { |
| 4728 | inline_value = GetHeap()->undefined_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } else { |
| 4731 | PropertyAttributes attributes; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4732 | // You can't install a getter on a property indexed by the hidden string, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4733 | // so we can be sure that GetLocalPropertyPostInterceptor returns a real |
| 4734 | // object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4735 | inline_value = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4736 | GetLocalPropertyPostInterceptor(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4737 | GetHeap()->hidden_string(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4738 | &attributes)->ToObjectUnchecked(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4739 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (init_option == ONLY_RETURN_INLINE_VALUE || |
| 4742 | inline_value->IsHashTable()) { |
| 4743 | return inline_value; |
| 4744 | } |
| 4745 | |
| 4746 | ObjectHashTable* hashtable; |
| 4747 | static const int kInitialCapacity = 4; |
| 4748 | MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4749 | ObjectHashTable::Allocate(GetHeap(), |
| 4750 | kInitialCapacity, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4751 | ObjectHashTable::USE_CUSTOM_MINIMUM_CAPACITY); |
| 4752 | if (!maybe_obj->To<ObjectHashTable>(&hashtable)) return maybe_obj; |
| 4753 | |
| 4754 | if (inline_value->IsSmi()) { |
| 4755 | // We were storing the identity hash inline and now allocated an actual |
| 4756 | // dictionary. Put the identity hash into the new dictionary. |
| 4757 | MaybeObject* insert_result = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4758 | hashtable->Put(GetHeap()->identity_hash_string(), inline_value); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4759 | ObjectHashTable* new_table; |
| 4760 | if (!insert_result->To(&new_table)) return insert_result; |
| 4761 | // We expect no resizing for the first insert. |
| 4762 | ASSERT_EQ(hashtable, new_table); |
| 4763 | } |
| 4764 | |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 4765 | MaybeObject* store_result = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4766 | SetPropertyPostInterceptor(GetHeap()->hidden_string(), |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4767 | hashtable, |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 4768 | DONT_ENUM, |
| 4769 | kNonStrictMode, |
| 4770 | OMIT_EXTENSIBILITY_CHECK); |
| 4771 | if (store_result->IsFailure()) return store_result; |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4772 | return hashtable; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4776 | MaybeObject* JSObject::SetHiddenPropertiesHashTable(Object* value) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4777 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4778 | // We can store the identity hash inline iff there is no backing store |
| 4779 | // for hidden properties yet. |
| 4780 | ASSERT(HasHiddenProperties() != value->IsSmi()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4781 | if (HasFastProperties()) { |
| 4782 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4783 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4784 | // hidden strings hash code is zero (and no other name has hash |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4785 | // code zero) it will always occupy the first entry if present. |
| 4786 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4787 | if (descriptors->number_of_descriptors() > 0) { |
| 4788 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4789 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4790 | sorted_index < map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4791 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 4792 | FastPropertyAtPut(descriptors->GetFieldIndex(sorted_index), value); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4793 | return this; |
| 4794 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4795 | } |
| 4796 | } |
| 4797 | MaybeObject* store_result = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4798 | SetPropertyPostInterceptor(GetHeap()->hidden_string(), |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4799 | value, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4800 | DONT_ENUM, |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 4801 | kNonStrictMode, |
| 4802 | OMIT_EXTENSIBILITY_CHECK); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4803 | if (store_result->IsFailure()) return store_result; |
| 4804 | return this; |
| 4805 | } |
| 4806 | |
| 4807 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4808 | MaybeObject* JSObject::DeletePropertyPostInterceptor(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4809 | DeleteMode mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4810 | // Check local property, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4811 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4812 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 4813 | if (!result.IsFound()) return GetHeap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4814 | |
| 4815 | // Normalize object if needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4816 | Object* obj; |
| 4817 | { MaybeObject* maybe_obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 4818 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4819 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4820 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4821 | return DeleteNormalizedProperty(name, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4825 | MaybeObject* JSObject::DeletePropertyWithInterceptor(Name* name) { |
| 4826 | // TODO(rossberg): Support symbols in the API. |
| 4827 | if (name->IsSymbol()) return GetHeap()->false_value(); |
| 4828 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4829 | Isolate* isolate = GetIsolate(); |
| 4830 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4831 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4832 | Handle<String> name_handle(String::cast(name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4833 | Handle<JSObject> this_handle(this); |
| 4834 | if (!interceptor->deleter()->IsUndefined()) { |
| 4835 | v8::NamedPropertyDeleter deleter = |
| 4836 | v8::ToCData<v8::NamedPropertyDeleter>(interceptor->deleter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4837 | LOG(isolate, |
| 4838 | ApiNamedPropertyAccess("interceptor-named-delete", *this_handle, name)); |
| 4839 | CustomArguments args(isolate, interceptor->data(), this, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4840 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4841 | v8::Handle<v8::Boolean> result; |
| 4842 | { |
| 4843 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4844 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4845 | result = deleter(v8::Utils::ToLocal(name_handle), info); |
| 4846 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4847 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4848 | if (!result.IsEmpty()) { |
| 4849 | ASSERT(result->IsBoolean()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 4850 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 4851 | result_internal->VerifyApiCallResultType(); |
| 4852 | return *result_internal; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4853 | } |
| 4854 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4855 | MaybeObject* raw_result = |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4856 | this_handle->DeletePropertyPostInterceptor(*name_handle, NORMAL_DELETION); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4857 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4858 | return raw_result; |
| 4859 | } |
| 4860 | |
| 4861 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4862 | MaybeObject* JSObject::DeleteElementWithInterceptor(uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4863 | Isolate* isolate = GetIsolate(); |
| 4864 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4865 | // Make sure that the top context does not change when doing |
| 4866 | // callbacks or interceptor calls. |
| 4867 | AssertNoContextChange ncc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4868 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4869 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4870 | if (interceptor->deleter()->IsUndefined()) return heap->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4871 | v8::IndexedPropertyDeleter deleter = |
| 4872 | v8::ToCData<v8::IndexedPropertyDeleter>(interceptor->deleter()); |
| 4873 | Handle<JSObject> this_handle(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4874 | LOG(isolate, |
| 4875 | ApiIndexedPropertyAccess("interceptor-indexed-delete", this, index)); |
| 4876 | CustomArguments args(isolate, interceptor->data(), this, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4877 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4878 | v8::Handle<v8::Boolean> result; |
| 4879 | { |
| 4880 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4881 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4882 | result = deleter(index, info); |
| 4883 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4884 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4885 | if (!result.IsEmpty()) { |
| 4886 | ASSERT(result->IsBoolean()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 4887 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 4888 | result_internal->VerifyApiCallResultType(); |
| 4889 | return *result_internal; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4890 | } |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 4891 | MaybeObject* raw_result = this_handle->GetElementsAccessor()->Delete( |
| 4892 | *this_handle, |
| 4893 | index, |
| 4894 | NORMAL_DELETION); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4895 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4896 | return raw_result; |
| 4897 | } |
| 4898 | |
| 4899 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4900 | Handle<Object> JSObject::DeleteElement(Handle<JSObject> obj, |
| 4901 | uint32_t index) { |
| 4902 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 4903 | obj->DeleteElement(index, JSObject::NORMAL_DELETION), |
| 4904 | Object); |
| 4905 | } |
| 4906 | |
| 4907 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4908 | MaybeObject* JSObject::DeleteElement(uint32_t index, DeleteMode mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4909 | Isolate* isolate = GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4910 | // Check access rights if needed. |
| 4911 | if (IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4912 | !isolate->MayIndexedAccess(this, index, v8::ACCESS_DELETE)) { |
| 4913 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 4914 | return isolate->heap()->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4917 | if (IsStringObjectWithCharacterAt(index)) { |
| 4918 | if (mode == STRICT_DELETION) { |
| 4919 | // Deleting a non-configurable property in strict mode. |
| 4920 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 4921 | Handle<Object> holder(this, isolate); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4922 | Handle<Object> name = isolate->factory()->NewNumberFromUint(index); |
| 4923 | Handle<Object> args[2] = { name, holder }; |
| 4924 | Handle<Object> error = |
| 4925 | isolate->factory()->NewTypeError("strict_delete_property", |
| 4926 | HandleVector(args, 2)); |
| 4927 | return isolate->Throw(*error); |
| 4928 | } |
| 4929 | return isolate->heap()->false_value(); |
| 4930 | } |
| 4931 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4932 | if (IsJSGlobalProxy()) { |
| 4933 | Object* proto = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4934 | if (proto->IsNull()) return isolate->heap()->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4935 | ASSERT(proto->IsJSGlobalObject()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4936 | return JSGlobalObject::cast(proto)->DeleteElement(index, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4939 | // From this point on everything needs to be handlified. |
| 4940 | HandleScope scope(isolate); |
| 4941 | Handle<JSObject> self(this); |
| 4942 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 4943 | Handle<Object> old_value; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 4944 | bool should_enqueue_change_record = false; |
| 4945 | if (FLAG_harmony_observation && self->map()->is_observed()) { |
| 4946 | should_enqueue_change_record = self->HasLocalElement(index); |
| 4947 | if (should_enqueue_change_record) { |
| 4948 | old_value = self->GetLocalElementAccessorPair(index) != NULL |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 4949 | ? Handle<Object>::cast(isolate->factory()->the_hole_value()) |
| 4950 | : Object::GetElement(self, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4951 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4954 | MaybeObject* result; |
| 4955 | // Skip interceptor if forcing deletion. |
| 4956 | if (self->HasIndexedInterceptor() && mode != FORCE_DELETION) { |
| 4957 | result = self->DeleteElementWithInterceptor(index); |
| 4958 | } else { |
| 4959 | result = self->GetElementsAccessor()->Delete(*self, index, mode); |
| 4960 | } |
| 4961 | |
| 4962 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 4963 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4964 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 4965 | if (should_enqueue_change_record && !self->HasLocalElement(index)) { |
| 4966 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 4967 | EnqueueChangeRecord(self, "deleted", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
| 4970 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4974 | Handle<Object> JSObject::DeleteProperty(Handle<JSObject> obj, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4975 | Handle<Name> prop) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4976 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 4977 | obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION), |
| 4978 | Object); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4982 | MaybeObject* JSObject::DeleteProperty(Name* name, DeleteMode mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4983 | Isolate* isolate = GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4984 | // ECMA-262, 3rd, 8.6.2.5 |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4985 | ASSERT(name->IsName()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4986 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4987 | // Check access rights if needed. |
| 4988 | if (IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4989 | !isolate->MayNamedAccess(this, name, v8::ACCESS_DELETE)) { |
| 4990 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 4991 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4992 | } |
| 4993 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4994 | if (IsJSGlobalProxy()) { |
| 4995 | Object* proto = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4996 | if (proto->IsNull()) return isolate->heap()->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4997 | ASSERT(proto->IsJSGlobalObject()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4998 | return JSGlobalObject::cast(proto)->DeleteProperty(name, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4999 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5000 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5001 | uint32_t index = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5002 | if (name->AsArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 5003 | return DeleteElement(index, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
| 5006 | LookupResult lookup(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5007 | LocalLookup(name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5008 | if (!lookup.IsFound()) return isolate->heap()->true_value(); |
| 5009 | // Ignore attributes if forcing a deletion. |
| 5010 | if (lookup.IsDontDelete() && mode != FORCE_DELETION) { |
| 5011 | if (mode == STRICT_DELETION) { |
| 5012 | // Deleting a non-configurable property in strict mode. |
| 5013 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5014 | Handle<Object> args[2] = { Handle<Object>(name, isolate), |
| 5015 | Handle<Object>(this, isolate) }; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5016 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 5017 | "strict_delete_property", HandleVector(args, 2))); |
| 5018 | } |
| 5019 | return isolate->heap()->false_value(); |
| 5020 | } |
| 5021 | |
| 5022 | // From this point on everything needs to be handlified. |
| 5023 | HandleScope scope(isolate); |
| 5024 | Handle<JSObject> self(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5025 | Handle<Name> hname(name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5026 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5027 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5028 | bool is_observed = FLAG_harmony_observation && self->map()->is_observed(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 5029 | if (is_observed && lookup.IsDataProperty()) { |
| 5030 | old_value = Object::GetProperty(self, hname); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5031 | } |
| 5032 | MaybeObject* result; |
| 5033 | |
| 5034 | // Check for interceptor. |
| 5035 | if (lookup.IsInterceptor()) { |
| 5036 | // Skip interceptor if forcing a deletion. |
| 5037 | if (mode == FORCE_DELETION) { |
| 5038 | result = self->DeletePropertyPostInterceptor(*hname, mode); |
| 5039 | } else { |
| 5040 | result = self->DeletePropertyWithInterceptor(*hname); |
| 5041 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5042 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5043 | // Normalize object if needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5044 | Object* obj; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5045 | result = self->NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 5046 | if (!result->To(&obj)) return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5047 | // Make sure the properties are normalized before removing the entry. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5048 | result = self->DeleteNormalizedProperty(*hname, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5049 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5050 | |
| 5051 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5052 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5053 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5054 | if (is_observed && !self->HasLocalProperty(*hname)) { |
| 5055 | EnqueueChangeRecord(self, "deleted", hname, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5062 | MaybeObject* JSReceiver::DeleteElement(uint32_t index, DeleteMode mode) { |
| 5063 | if (IsJSProxy()) { |
| 5064 | return JSProxy::cast(this)->DeleteElementWithHandler(index, mode); |
| 5065 | } |
| 5066 | return JSObject::cast(this)->DeleteElement(index, mode); |
| 5067 | } |
| 5068 | |
| 5069 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5070 | MaybeObject* JSReceiver::DeleteProperty(Name* name, DeleteMode mode) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5071 | if (IsJSProxy()) { |
| 5072 | return JSProxy::cast(this)->DeletePropertyWithHandler(name, mode); |
| 5073 | } |
| 5074 | return JSObject::cast(this)->DeleteProperty(name, mode); |
| 5075 | } |
| 5076 | |
| 5077 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5078 | bool JSObject::ReferencesObjectFromElements(FixedArray* elements, |
| 5079 | ElementsKind kind, |
| 5080 | Object* object) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5081 | ASSERT(IsFastObjectElementsKind(kind) || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5082 | kind == DICTIONARY_ELEMENTS); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5083 | if (IsFastObjectElementsKind(kind)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5084 | int length = IsJSArray() |
| 5085 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 5086 | : elements->length(); |
| 5087 | for (int i = 0; i < length; ++i) { |
| 5088 | Object* element = elements->get(i); |
| 5089 | if (!element->IsTheHole() && element == object) return true; |
| 5090 | } |
| 5091 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5092 | Object* key = |
| 5093 | SeededNumberDictionary::cast(elements)->SlowReverseLookup(object); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5094 | if (!key->IsUndefined()) return true; |
| 5095 | } |
| 5096 | return false; |
| 5097 | } |
| 5098 | |
| 5099 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5100 | // Check whether this object references another object. |
| 5101 | bool JSObject::ReferencesObject(Object* obj) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5102 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5103 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5104 | AssertNoAllocation no_alloc; |
| 5105 | |
| 5106 | // Is the object the constructor for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5107 | if (map_of_this->constructor() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5108 | return true; |
| 5109 | } |
| 5110 | |
| 5111 | // Is the object the prototype for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5112 | if (map_of_this->prototype() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5113 | return true; |
| 5114 | } |
| 5115 | |
| 5116 | // Check if the object is among the named properties. |
| 5117 | Object* key = SlowReverseLookup(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5118 | if (!key->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5119 | return true; |
| 5120 | } |
| 5121 | |
| 5122 | // Check if the object is among the indexed properties. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5123 | ElementsKind kind = GetElementsKind(); |
| 5124 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5125 | case EXTERNAL_PIXEL_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5126 | case EXTERNAL_BYTE_ELEMENTS: |
| 5127 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5128 | case EXTERNAL_SHORT_ELEMENTS: |
| 5129 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5130 | case EXTERNAL_INT_ELEMENTS: |
| 5131 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5132 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 5133 | case EXTERNAL_DOUBLE_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5134 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5135 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5136 | // Raw pixels and external arrays do not reference other |
| 5137 | // objects. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5138 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5139 | case FAST_SMI_ELEMENTS: |
| 5140 | case FAST_HOLEY_SMI_ELEMENTS: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5141 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5142 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5143 | case FAST_HOLEY_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5144 | case DICTIONARY_ELEMENTS: { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5145 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 5146 | if (ReferencesObjectFromElements(elements, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5147 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5148 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5149 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 5150 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 5151 | // Check the mapped parameters. |
| 5152 | int length = parameter_map->length(); |
| 5153 | for (int i = 2; i < length; ++i) { |
| 5154 | Object* value = parameter_map->get(i); |
| 5155 | if (!value->IsTheHole() && value == obj) return true; |
| 5156 | } |
| 5157 | // Check the arguments. |
| 5158 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5159 | kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : |
| 5160 | FAST_HOLEY_ELEMENTS; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5161 | if (ReferencesObjectFromElements(arguments, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5162 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5163 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5166 | // For functions check the context. |
| 5167 | if (IsJSFunction()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5168 | // Get the constructor function for arguments array. |
| 5169 | JSObject* arguments_boilerplate = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5170 | heap->isolate()->context()->native_context()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5171 | arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5172 | JSFunction* arguments_function = |
| 5173 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 5174 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5175 | // Get the context and don't check if it is the native context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5176 | JSFunction* f = JSFunction::cast(this); |
| 5177 | Context* context = f->context(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5178 | if (context->IsNativeContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5179 | return false; |
| 5180 | } |
| 5181 | |
| 5182 | // Check the non-special context slots. |
| 5183 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 5184 | // Only check JS objects. |
| 5185 | if (context->get(i)->IsJSObject()) { |
| 5186 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 5187 | // If it is an arguments array check the content. |
| 5188 | if (ctxobj->map()->constructor() == arguments_function) { |
| 5189 | if (ctxobj->ReferencesObject(obj)) { |
| 5190 | return true; |
| 5191 | } |
| 5192 | } else if (ctxobj == obj) { |
| 5193 | return true; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5198 | // Check the context extension (if any) if it can have references. |
| 5199 | if (context->has_extension() && !context->IsCatchContext()) { |
| 5200 | return JSObject::cast(context->extension())->ReferencesObject(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5201 | } |
| 5202 | } |
| 5203 | |
| 5204 | // No references to object. |
| 5205 | return false; |
| 5206 | } |
| 5207 | |
| 5208 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5209 | Handle<Object> JSObject::PreventExtensions(Handle<JSObject> object) { |
| 5210 | CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object); |
| 5211 | } |
| 5212 | |
| 5213 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5214 | MaybeObject* JSObject::PreventExtensions() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5215 | Isolate* isolate = GetIsolate(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5216 | if (IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5217 | !isolate->MayNamedAccess(this, |
| 5218 | isolate->heap()->undefined_value(), |
| 5219 | v8::ACCESS_KEYS)) { |
| 5220 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_KEYS); |
| 5221 | return isolate->heap()->false_value(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 5224 | if (IsJSGlobalProxy()) { |
| 5225 | Object* proto = GetPrototype(); |
| 5226 | if (proto->IsNull()) return this; |
| 5227 | ASSERT(proto->IsJSGlobalObject()); |
| 5228 | return JSObject::cast(proto)->PreventExtensions(); |
| 5229 | } |
| 5230 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5231 | // It's not possible to seal objects with external array elements |
| 5232 | if (HasExternalArrayElements()) { |
| 5233 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5234 | Handle<Object> object(this, isolate); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5235 | Handle<Object> error = |
| 5236 | isolate->factory()->NewTypeError( |
| 5237 | "cant_prevent_ext_external_array_elements", |
| 5238 | HandleVector(&object, 1)); |
| 5239 | return isolate->Throw(*error); |
| 5240 | } |
| 5241 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5242 | // If there are fast elements we normalize. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5243 | SeededNumberDictionary* dictionary = NULL; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5244 | { MaybeObject* maybe = NormalizeElements(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5245 | if (!maybe->To<SeededNumberDictionary>(&dictionary)) return maybe; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5246 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5247 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5248 | // Make sure that we never go back to fast case. |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5249 | dictionary->set_requires_slow_elements(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5250 | |
| 5251 | // Do a map transition, other objects with this map may still |
| 5252 | // be extensible. |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5253 | Map* new_map; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 5254 | MaybeObject* maybe = map()->Copy(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5255 | if (!maybe->To(&new_map)) return maybe; |
| 5256 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5257 | new_map->set_is_extensible(false); |
| 5258 | set_map(new_map); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5259 | ASSERT(!map()->is_extensible()); |
| 5260 | return new_map; |
| 5261 | } |
| 5262 | |
| 5263 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5264 | MUST_USE_RESULT MaybeObject* JSObject::DeepCopy(Isolate* isolate) { |
| 5265 | StackLimitCheck check(isolate); |
| 5266 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
| 5267 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5268 | if (map()->is_deprecated()) { |
| 5269 | MaybeObject* maybe_failure = MigrateInstance(); |
| 5270 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 5271 | } |
| 5272 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5273 | Heap* heap = isolate->heap(); |
| 5274 | Object* result; |
| 5275 | { MaybeObject* maybe_result = heap->CopyJSObject(this); |
| 5276 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5277 | } |
| 5278 | JSObject* copy = JSObject::cast(result); |
| 5279 | |
| 5280 | // Deep copy local properties. |
| 5281 | if (copy->HasFastProperties()) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5282 | DescriptorArray* descriptors = copy->map()->instance_descriptors(); |
| 5283 | int limit = copy->map()->NumberOfOwnDescriptors(); |
| 5284 | for (int i = 0; i < limit; i++) { |
| 5285 | PropertyDetails details = descriptors->GetDetails(i); |
| 5286 | if (details.type() != FIELD) continue; |
| 5287 | int index = descriptors->GetFieldIndex(i); |
| 5288 | Object* value = RawFastPropertyAt(index); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5289 | if (value->IsJSObject()) { |
| 5290 | JSObject* js_object = JSObject::cast(value); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5291 | MaybeObject* maybe_copy = js_object->DeepCopy(isolate); |
| 5292 | if (!maybe_copy->To(&value)) return maybe_copy; |
| 5293 | } else { |
| 5294 | Representation representation = details.representation(); |
| 5295 | MaybeObject* maybe_storage = |
| 5296 | value->AllocateNewStorageFor(heap, representation); |
| 5297 | if (!maybe_storage->To(&value)) return maybe_storage; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5298 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5299 | copy->FastPropertyAtPut(index, value); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5300 | } |
| 5301 | } else { |
| 5302 | { MaybeObject* maybe_result = |
| 5303 | heap->AllocateFixedArray(copy->NumberOfLocalProperties()); |
| 5304 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5305 | } |
| 5306 | FixedArray* names = FixedArray::cast(result); |
| 5307 | copy->GetLocalPropertyNames(names, 0); |
| 5308 | for (int i = 0; i < names->length(); i++) { |
| 5309 | ASSERT(names->get(i)->IsString()); |
| 5310 | String* key_string = String::cast(names->get(i)); |
| 5311 | PropertyAttributes attributes = |
| 5312 | copy->GetLocalPropertyAttribute(key_string); |
| 5313 | // Only deep copy fields from the object literal expression. |
| 5314 | // In particular, don't try to copy the length attribute of |
| 5315 | // an array. |
| 5316 | if (attributes != NONE) continue; |
| 5317 | Object* value = |
| 5318 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
| 5319 | if (value->IsJSObject()) { |
| 5320 | JSObject* js_object = JSObject::cast(value); |
| 5321 | { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 5322 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5323 | } |
| 5324 | { MaybeObject* maybe_result = |
| 5325 | // Creating object copy for literals. No strict mode needed. |
| 5326 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
| 5327 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5328 | } |
| 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | |
| 5333 | // Deep copy local elements. |
| 5334 | // Pixel elements cannot be created using an object literal. |
| 5335 | ASSERT(!copy->HasExternalArrayElements()); |
| 5336 | switch (copy->GetElementsKind()) { |
| 5337 | case FAST_SMI_ELEMENTS: |
| 5338 | case FAST_ELEMENTS: |
| 5339 | case FAST_HOLEY_SMI_ELEMENTS: |
| 5340 | case FAST_HOLEY_ELEMENTS: { |
| 5341 | FixedArray* elements = FixedArray::cast(copy->elements()); |
| 5342 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 5343 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 5344 | #ifdef DEBUG |
| 5345 | for (int i = 0; i < elements->length(); i++) { |
| 5346 | ASSERT(!elements->get(i)->IsJSObject()); |
| 5347 | } |
| 5348 | #endif |
| 5349 | } else { |
| 5350 | for (int i = 0; i < elements->length(); i++) { |
| 5351 | Object* value = elements->get(i); |
| 5352 | ASSERT(value->IsSmi() || |
| 5353 | value->IsTheHole() || |
| 5354 | (IsFastObjectElementsKind(copy->GetElementsKind()))); |
| 5355 | if (value->IsJSObject()) { |
| 5356 | JSObject* js_object = JSObject::cast(value); |
| 5357 | { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 5358 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5359 | } |
| 5360 | elements->set(i, result); |
| 5361 | } |
| 5362 | } |
| 5363 | } |
| 5364 | break; |
| 5365 | } |
| 5366 | case DICTIONARY_ELEMENTS: { |
| 5367 | SeededNumberDictionary* element_dictionary = copy->element_dictionary(); |
| 5368 | int capacity = element_dictionary->Capacity(); |
| 5369 | for (int i = 0; i < capacity; i++) { |
| 5370 | Object* k = element_dictionary->KeyAt(i); |
| 5371 | if (element_dictionary->IsKey(k)) { |
| 5372 | Object* value = element_dictionary->ValueAt(i); |
| 5373 | if (value->IsJSObject()) { |
| 5374 | JSObject* js_object = JSObject::cast(value); |
| 5375 | { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 5376 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 5377 | } |
| 5378 | element_dictionary->ValueAtPut(i, result); |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | break; |
| 5383 | } |
| 5384 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 5385 | UNIMPLEMENTED(); |
| 5386 | break; |
| 5387 | case EXTERNAL_PIXEL_ELEMENTS: |
| 5388 | case EXTERNAL_BYTE_ELEMENTS: |
| 5389 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5390 | case EXTERNAL_SHORT_ELEMENTS: |
| 5391 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5392 | case EXTERNAL_INT_ELEMENTS: |
| 5393 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5394 | case EXTERNAL_FLOAT_ELEMENTS: |
| 5395 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 5396 | case FAST_DOUBLE_ELEMENTS: |
| 5397 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 5398 | // No contained objects, nothing to do. |
| 5399 | break; |
| 5400 | } |
| 5401 | return copy; |
| 5402 | } |
| 5403 | |
| 5404 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5405 | // Tests for the fast common case for property enumeration: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5406 | // - This object and all prototypes has an enum cache (which means that |
| 5407 | // it is no proxy, has no interceptors and needs no access checks). |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5408 | // - This object has no elements. |
| 5409 | // - No prototype has enumerable properties/elements. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5410 | bool JSReceiver::IsSimpleEnum() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5411 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5412 | for (Object* o = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5413 | o != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5414 | o = JSObject::cast(o)->GetPrototype()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5415 | if (!o->IsJSObject()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5416 | JSObject* curr = JSObject::cast(o); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5417 | int enum_length = curr->map()->EnumLength(); |
| 5418 | if (enum_length == Map::kInvalidEnumCache) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5419 | ASSERT(!curr->HasNamedInterceptor()); |
| 5420 | ASSERT(!curr->HasIndexedInterceptor()); |
| 5421 | ASSERT(!curr->IsAccessCheckNeeded()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5422 | if (curr->NumberOfEnumElements() > 0) return false; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5423 | if (curr != this && enum_length != 0) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5424 | } |
| 5425 | return true; |
| 5426 | } |
| 5427 | |
| 5428 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5429 | int Map::NumberOfDescribedProperties(DescriptorFlag which, |
| 5430 | PropertyAttributes filter) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5431 | int result = 0; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5432 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5433 | int limit = which == ALL_DESCRIPTORS |
| 5434 | ? descs->number_of_descriptors() |
| 5435 | : NumberOfOwnDescriptors(); |
| 5436 | for (int i = 0; i < limit; i++) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5437 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 5438 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 5439 | result++; |
| 5440 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5441 | } |
| 5442 | return result; |
| 5443 | } |
| 5444 | |
| 5445 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5446 | int Map::NextFreePropertyIndex() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5447 | int max_index = -1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5448 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5449 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5450 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5451 | if (descs->GetType(i) == FIELD) { |
| 5452 | int current_index = descs->GetFieldIndex(i); |
| 5453 | if (current_index > max_index) max_index = current_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5454 | } |
| 5455 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5456 | return max_index + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5459 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5460 | AccessorDescriptor* Map::FindAccessor(Name* name) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5461 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5462 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 5463 | for (int i = 0; i < number_of_own_descriptors; i++) { |
| 5464 | if (descs->GetType(i) == CALLBACKS && name->Equals(descs->GetKey(i))) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5465 | return descs->GetCallbacks(i); |
| 5466 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5467 | } |
| 5468 | return NULL; |
| 5469 | } |
| 5470 | |
| 5471 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5472 | void JSReceiver::LocalLookup( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5473 | Name* name, LookupResult* result, bool search_hidden_prototypes) { |
| 5474 | ASSERT(name->IsName()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5475 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5476 | Heap* heap = GetHeap(); |
| 5477 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5478 | if (IsJSGlobalProxy()) { |
| 5479 | Object* proto = GetPrototype(); |
| 5480 | if (proto->IsNull()) return result->NotFound(); |
| 5481 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5482 | return JSReceiver::cast(proto)->LocalLookup( |
| 5483 | name, result, search_hidden_prototypes); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | if (IsJSProxy()) { |
| 5487 | result->HandlerResult(JSProxy::cast(this)); |
| 5488 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5491 | // Do not use inline caching if the object is a non-global object |
| 5492 | // that requires access checks. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5493 | if (IsAccessCheckNeeded()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5494 | result->DisallowCaching(); |
| 5495 | } |
| 5496 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5497 | JSObject* js_object = JSObject::cast(this); |
| 5498 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5499 | // Check for lookup interceptor except when bootstrapping. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5500 | if (js_object->HasNamedInterceptor() && |
| 5501 | !heap->isolate()->bootstrapper()->IsActive()) { |
| 5502 | result->InterceptorResult(js_object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5503 | return; |
| 5504 | } |
| 5505 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5506 | js_object->LocalLookupRealNamedProperty(name, result); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5507 | if (result->IsFound() || !search_hidden_prototypes) return; |
| 5508 | |
| 5509 | Object* proto = js_object->GetPrototype(); |
| 5510 | if (!proto->IsJSReceiver()) return; |
| 5511 | JSReceiver* receiver = JSReceiver::cast(proto); |
| 5512 | if (receiver->map()->is_hidden_prototype()) { |
| 5513 | receiver->LocalLookup(name, result, search_hidden_prototypes); |
| 5514 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5518 | void JSReceiver::Lookup(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5519 | // Ecma-262 3rd 8.6.2.4 |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5520 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5521 | for (Object* current = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5522 | current != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5523 | current = JSObject::cast(current)->GetPrototype()) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5524 | JSReceiver::cast(current)->LocalLookup(name, result, false); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5525 | if (result->IsFound()) return; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5526 | } |
| 5527 | result->NotFound(); |
| 5528 | } |
| 5529 | |
| 5530 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5531 | // Search object and its prototype chain for callback properties. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5532 | void JSObject::LookupCallbackProperty(Name* name, LookupResult* result) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5533 | Heap* heap = GetHeap(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5534 | for (Object* current = this; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 5535 | current != heap->null_value() && current->IsJSObject(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5536 | current = JSObject::cast(current)->GetPrototype()) { |
| 5537 | JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5538 | if (result->IsPropertyCallbacks()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5539 | } |
| 5540 | result->NotFound(); |
| 5541 | } |
| 5542 | |
| 5543 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5544 | // Try to update an accessor in an elements dictionary. Return true if the |
| 5545 | // update succeeded, and false otherwise. |
| 5546 | static bool UpdateGetterSetterInDictionary( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5547 | SeededNumberDictionary* dictionary, |
| 5548 | uint32_t index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5549 | Object* getter, |
| 5550 | Object* setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5551 | PropertyAttributes attributes) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5552 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5553 | if (entry != SeededNumberDictionary::kNotFound) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5554 | Object* result = dictionary->ValueAt(entry); |
| 5555 | PropertyDetails details = dictionary->DetailsAt(entry); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5556 | if (details.type() == CALLBACKS && result->IsAccessorPair()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5557 | ASSERT(!details.IsDontDelete()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5558 | if (details.attributes() != attributes) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 5559 | dictionary->DetailsAtPut( |
| 5560 | entry, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5561 | PropertyDetails(attributes, CALLBACKS, index)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5562 | } |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5563 | AccessorPair::cast(result)->SetComponents(getter, setter); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5564 | return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5565 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5566 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5567 | return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5571 | MaybeObject* JSObject::DefineElementAccessor(uint32_t index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5572 | Object* getter, |
| 5573 | Object* setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5574 | PropertyAttributes attributes) { |
| 5575 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5576 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5577 | case FAST_ELEMENTS: |
| 5578 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5579 | case FAST_HOLEY_SMI_ELEMENTS: |
| 5580 | case FAST_HOLEY_ELEMENTS: |
| 5581 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5582 | break; |
| 5583 | case EXTERNAL_PIXEL_ELEMENTS: |
| 5584 | case EXTERNAL_BYTE_ELEMENTS: |
| 5585 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5586 | case EXTERNAL_SHORT_ELEMENTS: |
| 5587 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5588 | case EXTERNAL_INT_ELEMENTS: |
| 5589 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5590 | case EXTERNAL_FLOAT_ELEMENTS: |
| 5591 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 5592 | // Ignore getters and setters on pixel and external array elements. |
| 5593 | return GetHeap()->undefined_value(); |
| 5594 | case DICTIONARY_ELEMENTS: |
| 5595 | if (UpdateGetterSetterInDictionary(element_dictionary(), |
| 5596 | index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5597 | getter, |
| 5598 | setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5599 | attributes)) { |
| 5600 | return GetHeap()->undefined_value(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5601 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5602 | break; |
| 5603 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 5604 | // Ascertain whether we have read-only properties or an existing |
| 5605 | // getter/setter pair in an arguments elements dictionary backing |
| 5606 | // store. |
| 5607 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 5608 | uint32_t length = parameter_map->length(); |
| 5609 | Object* probe = |
| 5610 | index < (length - 2) ? parameter_map->get(index + 2) : NULL; |
| 5611 | if (probe == NULL || probe->IsTheHole()) { |
| 5612 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 5613 | if (arguments->IsDictionary()) { |
| 5614 | SeededNumberDictionary* dictionary = |
| 5615 | SeededNumberDictionary::cast(arguments); |
| 5616 | if (UpdateGetterSetterInDictionary(dictionary, |
| 5617 | index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5618 | getter, |
| 5619 | setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5620 | attributes)) { |
| 5621 | return GetHeap()->undefined_value(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5622 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5623 | } |
| 5624 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5625 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5626 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | AccessorPair* accessors; |
| 5630 | { MaybeObject* maybe_accessors = GetHeap()->AllocateAccessorPair(); |
| 5631 | if (!maybe_accessors->To(&accessors)) return maybe_accessors; |
| 5632 | } |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5633 | accessors->SetComponents(getter, setter); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5634 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 5635 | return SetElementCallback(index, accessors, attributes); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5639 | MaybeObject* JSObject::CreateAccessorPairFor(Name* name) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 5640 | LookupResult result(GetHeap()->isolate()); |
| 5641 | LocalLookupRealNamedProperty(name, &result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 5642 | if (result.IsPropertyCallbacks()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5643 | // Note that the result can actually have IsDontDelete() == true when we |
| 5644 | // e.g. have to fall back to the slow case while adding a setter after |
| 5645 | // successfully reusing a map transition for a getter. Nevertheless, this is |
| 5646 | // OK, because the assertion only holds for the whole addition of both |
| 5647 | // accessors, not for the addition of each part. See first comment in |
| 5648 | // DefinePropertyAccessor below. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 5649 | Object* obj = result.GetCallbackObject(); |
| 5650 | if (obj->IsAccessorPair()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5651 | return AccessorPair::cast(obj)->Copy(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | return GetHeap()->AllocateAccessorPair(); |
| 5655 | } |
| 5656 | |
| 5657 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5658 | MaybeObject* JSObject::DefinePropertyAccessor(Name* name, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5659 | Object* getter, |
| 5660 | Object* setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5661 | PropertyAttributes attributes) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5662 | // We could assert that the property is configurable here, but we would need |
| 5663 | // to do a lookup, which seems to be a bit of overkill. |
| 5664 | Heap* heap = GetHeap(); |
| 5665 | bool only_attribute_changes = getter->IsNull() && setter->IsNull(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5666 | if (HasFastProperties() && !only_attribute_changes && |
| 5667 | (map()->NumberOfOwnDescriptors() < |
| 5668 | DescriptorArray::kMaxNumberOfDescriptors)) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5669 | MaybeObject* getterOk = heap->undefined_value(); |
| 5670 | if (!getter->IsNull()) { |
| 5671 | getterOk = DefineFastAccessor(name, ACCESSOR_GETTER, getter, attributes); |
| 5672 | if (getterOk->IsFailure()) return getterOk; |
| 5673 | } |
| 5674 | |
| 5675 | MaybeObject* setterOk = heap->undefined_value(); |
| 5676 | if (getterOk != heap->null_value() && !setter->IsNull()) { |
| 5677 | setterOk = DefineFastAccessor(name, ACCESSOR_SETTER, setter, attributes); |
| 5678 | if (setterOk->IsFailure()) return setterOk; |
| 5679 | } |
| 5680 | |
| 5681 | if (getterOk != heap->null_value() && setterOk != heap->null_value()) { |
| 5682 | return heap->undefined_value(); |
| 5683 | } |
| 5684 | } |
| 5685 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5686 | AccessorPair* accessors; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5687 | MaybeObject* maybe_accessors = CreateAccessorPairFor(name); |
| 5688 | if (!maybe_accessors->To(&accessors)) return maybe_accessors; |
| 5689 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5690 | accessors->SetComponents(getter, setter); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 5691 | return SetPropertyCallback(name, accessors, attributes); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5695 | bool JSObject::CanSetCallback(Name* name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5696 | ASSERT(!IsAccessCheckNeeded() || |
| 5697 | GetIsolate()->MayNamedAccess(this, name, v8::ACCESS_SET)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5698 | |
| 5699 | // Check if there is an API defined callback object which prohibits |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5700 | // callback overwriting in this object or its prototype chain. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5701 | // This mechanism is needed for instance in a browser setting, where |
| 5702 | // certain accessors such as window.location should not be allowed |
| 5703 | // to be overwritten because allowing overwriting could potentially |
| 5704 | // cause security problems. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5705 | LookupResult callback_result(GetIsolate()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5706 | LookupCallbackProperty(name, &callback_result); |
| 5707 | if (callback_result.IsFound()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5708 | Object* obj = callback_result.GetCallbackObject(); |
| 5709 | if (obj->IsAccessorInfo() && |
| 5710 | AccessorInfo::cast(obj)->prohibits_overwriting()) { |
| 5711 | return false; |
| 5712 | } |
| 5713 | } |
| 5714 | |
| 5715 | return true; |
| 5716 | } |
| 5717 | |
| 5718 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5719 | MaybeObject* JSObject::SetElementCallback(uint32_t index, |
| 5720 | Object* structure, |
| 5721 | PropertyAttributes attributes) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5722 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5723 | |
| 5724 | // Normalize elements to make this operation simple. |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5725 | SeededNumberDictionary* dictionary; |
| 5726 | { MaybeObject* maybe_dictionary = NormalizeElements(); |
| 5727 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5728 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5729 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5730 | |
| 5731 | // Update the dictionary with the new CALLBACKS property. |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5732 | { MaybeObject* maybe_dictionary = dictionary->Set(index, structure, details); |
| 5733 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5734 | } |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5735 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5736 | dictionary->set_requires_slow_elements(); |
| 5737 | // Update the dictionary backing store on the object. |
| 5738 | if (elements()->map() == GetHeap()->non_strict_arguments_elements_map()) { |
| 5739 | // Also delete any parameter alias. |
| 5740 | // |
| 5741 | // TODO(kmillikin): when deleting the last parameter alias we could |
| 5742 | // switch to a direct backing store without the parameter map. This |
| 5743 | // would allow GC of the context. |
| 5744 | FixedArray* parameter_map = FixedArray::cast(elements()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5745 | if (index < static_cast<uint32_t>(parameter_map->length()) - 2) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5746 | parameter_map->set(index + 2, GetHeap()->the_hole_value()); |
| 5747 | } |
| 5748 | parameter_map->set(1, dictionary); |
| 5749 | } else { |
| 5750 | set_elements(dictionary); |
| 5751 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5752 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5753 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5757 | MaybeObject* JSObject::SetPropertyCallback(Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5758 | Object* structure, |
| 5759 | PropertyAttributes attributes) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5760 | // Normalize object to make this operation simple. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5761 | MaybeObject* maybe_ok = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 5762 | if (maybe_ok->IsFailure()) return maybe_ok; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5763 | |
| 5764 | // For the global object allocate a new map to invalidate the global inline |
| 5765 | // caches which have a global property cell reference directly in the code. |
| 5766 | if (IsGlobalObject()) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5767 | Map* new_map; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5768 | MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); |
| 5769 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 5770 | ASSERT(new_map->is_dictionary_map()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5771 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5772 | set_map(new_map); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5773 | // When running crankshaft, changing the map is not enough. We |
| 5774 | // need to deoptimize all functions that rely on this global |
| 5775 | // object. |
| 5776 | Deoptimizer::DeoptimizeGlobalObject(this); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | // Update the dictionary with the new CALLBACKS property. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 5780 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5781 | maybe_ok = SetNormalizedProperty(name, structure, details); |
| 5782 | if (maybe_ok->IsFailure()) return maybe_ok; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5783 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5784 | return GetHeap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5785 | } |
| 5786 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5787 | |
| 5788 | void JSObject::DefineAccessor(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5789 | Handle<Name> name, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5790 | Handle<Object> getter, |
| 5791 | Handle<Object> setter, |
| 5792 | PropertyAttributes attributes) { |
| 5793 | CALL_HEAP_FUNCTION_VOID( |
| 5794 | object->GetIsolate(), |
| 5795 | object->DefineAccessor(*name, *getter, *setter, attributes)); |
| 5796 | } |
| 5797 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5798 | MaybeObject* JSObject::DefineAccessor(Name* name_raw, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5799 | Object* getter_raw, |
| 5800 | Object* setter_raw, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5801 | PropertyAttributes attributes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5802 | Isolate* isolate = GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5803 | // Check access rights if needed. |
| 5804 | if (IsAccessCheckNeeded() && |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5805 | !isolate->MayNamedAccess(this, name_raw, v8::ACCESS_SET)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5806 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 5807 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
| 5810 | if (IsJSGlobalProxy()) { |
| 5811 | Object* proto = GetPrototype(); |
| 5812 | if (proto->IsNull()) return this; |
| 5813 | ASSERT(proto->IsJSGlobalObject()); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 5814 | return JSObject::cast(proto)->DefineAccessor( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5815 | name_raw, getter_raw, setter_raw, attributes); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5816 | } |
| 5817 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5818 | // Make sure that the top context does not change when doing callbacks or |
| 5819 | // interceptor calls. |
| 5820 | AssertNoContextChange ncc; |
| 5821 | |
| 5822 | // Try to flatten before operating on the string. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5823 | if (name_raw->IsString()) String::cast(name_raw)->TryFlatten(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5824 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5825 | if (!CanSetCallback(name_raw)) return isolate->heap()->undefined_value(); |
| 5826 | |
| 5827 | // From this point on everything needs to be handlified. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5828 | HandleScope scope(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5829 | Handle<JSObject> self(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5830 | Handle<Name> name(name_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5831 | Handle<Object> getter(getter_raw, isolate); |
| 5832 | Handle<Object> setter(setter_raw, isolate); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 5833 | |
| 5834 | uint32_t index = 0; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5835 | bool is_element = name->AsArrayIndex(&index); |
| 5836 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5837 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5838 | bool is_observed = FLAG_harmony_observation && self->map()->is_observed(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5839 | bool preexists = false; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5840 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5841 | if (is_element) { |
| 5842 | preexists = HasLocalElement(index); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5843 | if (preexists && self->GetLocalElementAccessorPair(index) == NULL) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5844 | old_value = Object::GetElement(self, index); |
| 5845 | } |
| 5846 | } else { |
| 5847 | LookupResult lookup(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5848 | LocalLookup(*name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5849 | preexists = lookup.IsProperty(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 5850 | if (preexists && lookup.IsDataProperty()) { |
| 5851 | old_value = Object::GetProperty(self, name); |
| 5852 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | MaybeObject* result = is_element ? |
| 5857 | self->DefineElementAccessor(index, *getter, *setter, attributes) : |
| 5858 | self->DefinePropertyAccessor(*name, *getter, *setter, attributes); |
| 5859 | |
| 5860 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5861 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5862 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5863 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5864 | const char* type = preexists ? "reconfigured" : "new"; |
| 5865 | EnqueueChangeRecord(self, type, name, old_value); |
| 5866 | } |
| 5867 | |
| 5868 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5872 | static MaybeObject* TryAccessorTransition(JSObject* self, |
| 5873 | Map* transitioned_map, |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5874 | int target_descriptor, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5875 | AccessorComponent component, |
| 5876 | Object* accessor, |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5877 | PropertyAttributes attributes) { |
| 5878 | DescriptorArray* descs = transitioned_map->instance_descriptors(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5879 | PropertyDetails details = descs->GetDetails(target_descriptor); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5880 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5881 | // If the transition target was not callbacks, fall back to the slow case. |
| 5882 | if (details.type() != CALLBACKS) return self->GetHeap()->null_value(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5883 | Object* descriptor = descs->GetCallbacksObject(target_descriptor); |
| 5884 | if (!descriptor->IsAccessorPair()) return self->GetHeap()->null_value(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5885 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5886 | Object* target_accessor = AccessorPair::cast(descriptor)->get(component); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5887 | PropertyAttributes target_attributes = details.attributes(); |
| 5888 | |
| 5889 | // Reuse transition if adding same accessor with same attributes. |
| 5890 | if (target_accessor == accessor && target_attributes == attributes) { |
| 5891 | self->set_map(transitioned_map); |
| 5892 | return self; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5893 | } |
| 5894 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5895 | // If either not the same accessor, or not the same attributes, fall back to |
| 5896 | // the slow case. |
| 5897 | return self->GetHeap()->null_value(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5901 | MaybeObject* JSObject::DefineFastAccessor(Name* name, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5902 | AccessorComponent component, |
| 5903 | Object* accessor, |
| 5904 | PropertyAttributes attributes) { |
| 5905 | ASSERT(accessor->IsSpecFunction() || accessor->IsUndefined()); |
| 5906 | LookupResult result(GetIsolate()); |
| 5907 | LocalLookup(name, &result); |
| 5908 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 5909 | if (result.IsFound() && !result.IsPropertyCallbacks()) { |
| 5910 | return GetHeap()->null_value(); |
| 5911 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5912 | |
| 5913 | // Return success if the same accessor with the same attributes already exist. |
| 5914 | AccessorPair* source_accessors = NULL; |
| 5915 | if (result.IsPropertyCallbacks()) { |
| 5916 | Object* callback_value = result.GetCallbackObject(); |
| 5917 | if (callback_value->IsAccessorPair()) { |
| 5918 | source_accessors = AccessorPair::cast(callback_value); |
| 5919 | Object* entry = source_accessors->get(component); |
| 5920 | if (entry == accessor && result.GetAttributes() == attributes) { |
| 5921 | return this; |
| 5922 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5923 | } else { |
| 5924 | return GetHeap()->null_value(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5925 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5926 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5927 | int descriptor_number = result.GetDescriptorIndex(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 5928 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5929 | map()->LookupTransition(this, name, &result); |
| 5930 | |
| 5931 | if (result.IsFound()) { |
| 5932 | Map* target = result.GetTransitionTarget(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5933 | ASSERT(target->NumberOfOwnDescriptors() == |
| 5934 | map()->NumberOfOwnDescriptors()); |
| 5935 | // This works since descriptors are sorted in order of addition. |
| 5936 | ASSERT(map()->instance_descriptors()->GetKey(descriptor_number) == name); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5937 | return TryAccessorTransition( |
| 5938 | this, target, descriptor_number, component, accessor, attributes); |
| 5939 | } |
| 5940 | } else { |
| 5941 | // If not, lookup a transition. |
| 5942 | map()->LookupTransition(this, name, &result); |
| 5943 | |
| 5944 | // If there is a transition, try to follow it. |
| 5945 | if (result.IsFound()) { |
| 5946 | Map* target = result.GetTransitionTarget(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 5947 | int descriptor_number = target->LastAdded(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5948 | ASSERT(target->instance_descriptors()->GetKey(descriptor_number) |
| 5949 | ->Equals(name)); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 5950 | return TryAccessorTransition( |
| 5951 | this, target, descriptor_number, component, accessor, attributes); |
| 5952 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5955 | // If there is no transition yet, add a transition to the a new accessor pair |
| 5956 | // containing the accessor. |
| 5957 | AccessorPair* accessors; |
| 5958 | MaybeObject* maybe_accessors; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5959 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5960 | // Allocate a new pair if there were no source accessors. Otherwise, copy the |
| 5961 | // pair and modify the accessor. |
| 5962 | if (source_accessors != NULL) { |
| 5963 | maybe_accessors = source_accessors->Copy(); |
| 5964 | } else { |
| 5965 | maybe_accessors = GetHeap()->AllocateAccessorPair(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5966 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5967 | if (!maybe_accessors->To(&accessors)) return maybe_accessors; |
| 5968 | accessors->set(component, accessor); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5969 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5970 | CallbacksDescriptor new_accessors_desc(name, accessors, attributes); |
| 5971 | |
| 5972 | Map* new_map; |
| 5973 | MaybeObject* maybe_new_map = |
| 5974 | map()->CopyInsertDescriptor(&new_accessors_desc, INSERT_TRANSITION); |
| 5975 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 5976 | |
| 5977 | set_map(new_map); |
| 5978 | return this; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5982 | MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5983 | Isolate* isolate = GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5984 | Name* name = Name::cast(info->name()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5985 | // Check access rights if needed. |
| 5986 | if (IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5987 | !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 5988 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 5989 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | if (IsJSGlobalProxy()) { |
| 5993 | Object* proto = GetPrototype(); |
| 5994 | if (proto->IsNull()) return this; |
| 5995 | ASSERT(proto->IsJSGlobalObject()); |
| 5996 | return JSObject::cast(proto)->DefineAccessor(info); |
| 5997 | } |
| 5998 | |
| 5999 | // Make sure that the top context does not change when doing callbacks or |
| 6000 | // interceptor calls. |
| 6001 | AssertNoContextChange ncc; |
| 6002 | |
| 6003 | // Try to flatten before operating on the string. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6004 | if (name->IsString()) String::cast(name)->TryFlatten(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6005 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6006 | if (!CanSetCallback(name)) return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6007 | |
| 6008 | uint32_t index = 0; |
| 6009 | bool is_element = name->AsArrayIndex(&index); |
| 6010 | |
| 6011 | if (is_element) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6012 | if (IsJSArray()) return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6013 | |
| 6014 | // Accessors overwrite previous callbacks (cf. with getters/setters). |
| 6015 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6016 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6017 | case FAST_ELEMENTS: |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6018 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6019 | case FAST_HOLEY_SMI_ELEMENTS: |
| 6020 | case FAST_HOLEY_ELEMENTS: |
| 6021 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6022 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6023 | case EXTERNAL_PIXEL_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6024 | case EXTERNAL_BYTE_ELEMENTS: |
| 6025 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6026 | case EXTERNAL_SHORT_ELEMENTS: |
| 6027 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6028 | case EXTERNAL_INT_ELEMENTS: |
| 6029 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6030 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 6031 | case EXTERNAL_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6032 | // Ignore getters and setters on pixel and external array |
| 6033 | // elements. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6034 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6035 | case DICTIONARY_ELEMENTS: |
| 6036 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6037 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 6038 | UNIMPLEMENTED(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6039 | break; |
| 6040 | } |
| 6041 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6042 | MaybeObject* maybe_ok = |
| 6043 | SetElementCallback(index, info, info->property_attributes()); |
| 6044 | if (maybe_ok->IsFailure()) return maybe_ok; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6045 | } else { |
| 6046 | // Lookup the name. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6047 | LookupResult result(isolate); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 6048 | LocalLookup(name, &result, true); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6049 | // ES5 forbids turning a property into an accessor if it's not |
| 6050 | // configurable (that is IsDontDelete in ES3 and v8), see 8.6.1 (Table 5). |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6051 | if (result.IsFound() && (result.IsReadOnly() || result.IsDontDelete())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6052 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6053 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6054 | |
| 6055 | MaybeObject* maybe_ok = |
| 6056 | SetPropertyCallback(name, info, info->property_attributes()); |
| 6057 | if (maybe_ok->IsFailure()) return maybe_ok; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | return this; |
| 6061 | } |
| 6062 | |
| 6063 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6064 | Object* JSObject::LookupAccessor(Name* name, AccessorComponent component) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6065 | Heap* heap = GetHeap(); |
| 6066 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6067 | // Make sure that the top context does not change when doing callbacks or |
| 6068 | // interceptor calls. |
| 6069 | AssertNoContextChange ncc; |
| 6070 | |
| 6071 | // Check access rights if needed. |
| 6072 | if (IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6073 | !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_HAS)) { |
| 6074 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 6075 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6078 | // Make the lookup and include prototypes. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6079 | uint32_t index = 0; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6080 | if (name->AsArrayIndex(&index)) { |
| 6081 | for (Object* obj = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6082 | obj != heap->null_value(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6083 | obj = JSReceiver::cast(obj)->GetPrototype()) { |
| 6084 | if (obj->IsJSObject() && JSObject::cast(obj)->HasDictionaryElements()) { |
| 6085 | JSObject* js_object = JSObject::cast(obj); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6086 | SeededNumberDictionary* dictionary = js_object->element_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6087 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6088 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6089 | Object* element = dictionary->ValueAt(entry); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 6090 | if (dictionary->DetailsAt(entry).type() == CALLBACKS && |
| 6091 | element->IsAccessorPair()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6092 | return AccessorPair::cast(element)->GetComponent(component); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | } |
| 6096 | } |
| 6097 | } else { |
| 6098 | for (Object* obj = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6099 | obj != heap->null_value(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6100 | obj = JSReceiver::cast(obj)->GetPrototype()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6101 | LookupResult result(heap->isolate()); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6102 | JSReceiver::cast(obj)->LocalLookup(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6103 | if (result.IsFound()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6104 | if (result.IsReadOnly()) return heap->undefined_value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6105 | if (result.IsPropertyCallbacks()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6106 | Object* obj = result.GetCallbackObject(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6107 | if (obj->IsAccessorPair()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6108 | return AccessorPair::cast(obj)->GetComponent(component); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6109 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | } |
| 6113 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6114 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6115 | } |
| 6116 | |
| 6117 | |
| 6118 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 6119 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6120 | int number_of_own_descriptors = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6121 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6122 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6123 | if (descs->GetType(i) == FIELD) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 6124 | Object* property = RawFastPropertyAt(descs->GetFieldIndex(i)); |
| 6125 | if (FLAG_track_double_fields && |
| 6126 | descs->GetDetails(i).representation().IsDouble()) { |
| 6127 | ASSERT(property->IsHeapNumber()); |
| 6128 | if (value->IsNumber() && property->Number() == value->Number()) { |
| 6129 | return descs->GetKey(i); |
| 6130 | } |
| 6131 | } else if (property == value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6132 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6133 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6134 | } else if (descs->GetType(i) == CONSTANT_FUNCTION) { |
| 6135 | if (descs->GetConstantFunction(i) == value) { |
| 6136 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6140 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6141 | } else { |
| 6142 | return property_dictionary()->SlowReverseLookup(value); |
| 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6147 | MaybeObject* Map::RawCopy(int instance_size) { |
| 6148 | Map* result; |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6149 | MaybeObject* maybe_result = |
| 6150 | GetHeap()->AllocateMap(instance_type(), instance_size); |
| 6151 | if (!maybe_result->To(&result)) return maybe_result; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6152 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6153 | result->set_prototype(prototype()); |
| 6154 | result->set_constructor(constructor()); |
| 6155 | result->set_bit_field(bit_field()); |
| 6156 | result->set_bit_field2(bit_field2()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6157 | int new_bit_field3 = bit_field3(); |
| 6158 | new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true); |
| 6159 | new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0); |
| 6160 | new_bit_field3 = EnumLengthBits::update(new_bit_field3, kInvalidEnumCache); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 6161 | new_bit_field3 = Deprecated::update(new_bit_field3, false); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6162 | result->set_bit_field3(new_bit_field3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6163 | return result; |
| 6164 | } |
| 6165 | |
| 6166 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6167 | MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode, |
| 6168 | NormalizedMapSharingMode sharing) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6169 | int new_instance_size = instance_size(); |
| 6170 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
| 6171 | new_instance_size -= inobject_properties() * kPointerSize; |
| 6172 | } |
| 6173 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6174 | Map* result; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6175 | MaybeObject* maybe_result = RawCopy(new_instance_size); |
| 6176 | if (!maybe_result->To(&result)) return maybe_result; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6177 | |
| 6178 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6179 | result->set_inobject_properties(inobject_properties()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6182 | result->set_is_shared(sharing == SHARED_NORMALIZED_MAP); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 6183 | result->set_dictionary_map(true); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6184 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 6185 | #ifdef VERIFY_HEAP |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6186 | if (FLAG_verify_heap && result->is_shared()) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6187 | result->SharedMapVerify(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6188 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6189 | #endif |
| 6190 | |
| 6191 | return result; |
| 6192 | } |
| 6193 | |
| 6194 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6195 | MaybeObject* Map::CopyDropDescriptors() { |
| 6196 | Map* result; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6197 | MaybeObject* maybe_result = RawCopy(instance_size()); |
| 6198 | if (!maybe_result->To(&result)) return maybe_result; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Please note instance_type and instance_size are set when allocated. |
| 6201 | result->set_inobject_properties(inobject_properties()); |
| 6202 | result->set_unused_property_fields(unused_property_fields()); |
| 6203 | |
| 6204 | result->set_pre_allocated_property_fields(pre_allocated_property_fields()); |
| 6205 | result->set_is_shared(false); |
| 6206 | result->ClearCodeCache(GetHeap()); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 6207 | NotifyLeafMapLayoutChange(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6208 | return result; |
| 6209 | } |
| 6210 | |
| 6211 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6212 | MaybeObject* Map::ShareDescriptor(DescriptorArray* descriptors, |
| 6213 | Descriptor* descriptor) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6214 | // Sanity check. This path is only to be taken if the map owns its descriptor |
| 6215 | // array, implying that its NumberOfOwnDescriptors equals the number of |
| 6216 | // descriptors in the descriptor array. |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6217 | ASSERT(NumberOfOwnDescriptors() == |
| 6218 | instance_descriptors()->number_of_descriptors()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 6219 | Map* result; |
| 6220 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6221 | if (!maybe_result->To(&result)) return maybe_result; |
| 6222 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6223 | Name* name = descriptor->GetKey(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6224 | |
| 6225 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6226 | MaybeObject* maybe_transitions = |
| 6227 | AddTransition(name, result, SIMPLE_TRANSITION); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6228 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6229 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6230 | int old_size = descriptors->number_of_descriptors(); |
| 6231 | |
| 6232 | DescriptorArray* new_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6233 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6234 | if (descriptors->NumberOfSlackDescriptors() > 0) { |
| 6235 | new_descriptors = descriptors; |
| 6236 | new_descriptors->Append(descriptor); |
| 6237 | } else { |
| 6238 | // Descriptor arrays grow by 50%. |
| 6239 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
| 6240 | old_size, old_size < 4 ? 1 : old_size / 2); |
| 6241 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6242 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6243 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
| 6244 | |
| 6245 | // Copy the descriptors, inserting a descriptor. |
| 6246 | for (int i = 0; i < old_size; ++i) { |
| 6247 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 6248 | } |
| 6249 | |
| 6250 | new_descriptors->Append(descriptor, witness); |
| 6251 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6252 | if (old_size > 0) { |
| 6253 | // If the source descriptors had an enum cache we copy it. This ensures |
| 6254 | // that the maps to which we push the new descriptor array back can rely |
| 6255 | // on a cache always being available once it is set. If the map has more |
| 6256 | // enumerated descriptors than available in the original cache, the cache |
| 6257 | // will be lazily replaced by the extended cache when needed. |
| 6258 | if (descriptors->HasEnumCache()) { |
| 6259 | new_descriptors->CopyEnumCacheFrom(descriptors); |
| 6260 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6261 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6262 | Map* map; |
| 6263 | // Replace descriptors by new_descriptors in all maps that share it. |
| 6264 | for (Object* current = GetBackPointer(); |
| 6265 | !current->IsUndefined(); |
| 6266 | current = map->GetBackPointer()) { |
| 6267 | map = Map::cast(current); |
| 6268 | if (map->instance_descriptors() != descriptors) break; |
| 6269 | map->set_instance_descriptors(new_descriptors); |
| 6270 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6271 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6272 | set_instance_descriptors(new_descriptors); |
| 6273 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6274 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6275 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6276 | result->SetBackPointer(this); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6277 | result->InitializeDescriptors(new_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6278 | ASSERT(result->NumberOfOwnDescriptors() == NumberOfOwnDescriptors() + 1); |
| 6279 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6280 | set_transitions(transitions); |
| 6281 | set_owns_descriptors(false); |
| 6282 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6283 | return result; |
| 6284 | } |
| 6285 | |
| 6286 | |
| 6287 | MaybeObject* Map::CopyReplaceDescriptors(DescriptorArray* descriptors, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6288 | Name* name, |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6289 | TransitionFlag flag, |
| 6290 | int descriptor_index) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6291 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6292 | |
| 6293 | Map* result; |
| 6294 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6295 | if (!maybe_result->To(&result)) return maybe_result; |
| 6296 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6297 | result->InitializeDescriptors(descriptors); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6298 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6299 | if (flag == INSERT_TRANSITION && CanHaveMoreTransitions()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6300 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6301 | SimpleTransitionFlag simple_flag = |
| 6302 | (descriptor_index == descriptors->number_of_descriptors() - 1) |
| 6303 | ? SIMPLE_TRANSITION |
| 6304 | : FULL_TRANSITION; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6305 | ASSERT(name == descriptors->GetKey(descriptor_index)); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6306 | MaybeObject* maybe_transitions = AddTransition(name, result, simple_flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6307 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6308 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6309 | set_transitions(transitions); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6310 | result->SetBackPointer(this); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 6311 | } else { |
| 6312 | descriptors->InitializeRepresentations(Representation::Tagged()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6315 | return result; |
| 6316 | } |
| 6317 | |
| 6318 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6319 | MaybeObject* Map::CopyInstallDescriptors(int new_descriptor, |
| 6320 | DescriptorArray* descriptors) { |
| 6321 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6322 | |
| 6323 | Map* result; |
| 6324 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6325 | if (!maybe_result->To(&result)) return maybe_result; |
| 6326 | |
| 6327 | result->InitializeDescriptors(descriptors); |
| 6328 | result->SetNumberOfOwnDescriptors(new_descriptor + 1); |
| 6329 | |
| 6330 | int unused_property_fields = this->unused_property_fields(); |
| 6331 | if (descriptors->GetDetails(new_descriptor).type() == FIELD) { |
| 6332 | unused_property_fields = this->unused_property_fields() - 1; |
| 6333 | if (unused_property_fields < 0) { |
| 6334 | unused_property_fields += JSObject::kFieldsAdded; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | result->set_unused_property_fields(unused_property_fields); |
| 6339 | result->set_owns_descriptors(false); |
| 6340 | |
| 6341 | if (CanHaveMoreTransitions()) { |
| 6342 | Name* name = descriptors->GetKey(new_descriptor); |
| 6343 | TransitionArray* transitions; |
| 6344 | MaybeObject* maybe_transitions = |
| 6345 | AddTransition(name, result, SIMPLE_TRANSITION); |
| 6346 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6347 | |
| 6348 | set_transitions(transitions); |
| 6349 | result->SetBackPointer(this); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 6350 | } else { |
| 6351 | descriptors->InitializeRepresentations(Representation::Tagged()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | return result; |
| 6355 | } |
| 6356 | |
| 6357 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6358 | MaybeObject* Map::CopyAsElementsKind(ElementsKind kind, TransitionFlag flag) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6359 | if (flag == INSERT_TRANSITION) { |
| 6360 | ASSERT(!HasElementsTransition() || |
| 6361 | ((elements_transition_map()->elements_kind() == DICTIONARY_ELEMENTS || |
| 6362 | IsExternalArrayElementsKind( |
| 6363 | elements_transition_map()->elements_kind())) && |
| 6364 | (kind == DICTIONARY_ELEMENTS || |
| 6365 | IsExternalArrayElementsKind(kind)))); |
| 6366 | ASSERT(!IsFastElementsKind(kind) || |
| 6367 | IsMoreGeneralElementsKindTransition(elements_kind(), kind)); |
| 6368 | ASSERT(kind != elements_kind()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6369 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6370 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6371 | bool insert_transition = |
| 6372 | flag == INSERT_TRANSITION && !HasElementsTransition(); |
| 6373 | |
| 6374 | if (insert_transition && owns_descriptors()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6375 | // In case the map owned its own descriptors, share the descriptors and |
| 6376 | // transfer ownership to the new map. |
| 6377 | Map* new_map; |
| 6378 | MaybeObject* maybe_new_map = CopyDropDescriptors(); |
| 6379 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 6380 | |
| 6381 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6382 | if (added_elements->IsFailure()) return added_elements; |
| 6383 | |
| 6384 | new_map->set_elements_kind(kind); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6385 | new_map->InitializeDescriptors(instance_descriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6386 | new_map->SetBackPointer(this); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6387 | set_owns_descriptors(false); |
| 6388 | return new_map; |
| 6389 | } |
| 6390 | |
| 6391 | // In case the map did not own its own descriptors, a split is forced by |
| 6392 | // copying the map; creating a new descriptor array cell. |
| 6393 | // Create a new free-floating map only if we are not allowed to store it. |
| 6394 | Map* new_map; |
| 6395 | MaybeObject* maybe_new_map = Copy(); |
| 6396 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6397 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6398 | new_map->set_elements_kind(kind); |
| 6399 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6400 | if (insert_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6401 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6402 | if (added_elements->IsFailure()) return added_elements; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6403 | new_map->SetBackPointer(this); |
| 6404 | } |
| 6405 | |
| 6406 | return new_map; |
| 6407 | } |
| 6408 | |
| 6409 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6410 | MaybeObject* Map::CopyWithPreallocatedFieldDescriptors() { |
| 6411 | if (pre_allocated_property_fields() == 0) return CopyDropDescriptors(); |
| 6412 | |
| 6413 | // If the map has pre-allocated properties always start out with a descriptor |
| 6414 | // array describing these properties. |
| 6415 | ASSERT(constructor()->IsJSFunction()); |
| 6416 | JSFunction* ctor = JSFunction::cast(constructor()); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6417 | Map* map = ctor->initial_map(); |
| 6418 | DescriptorArray* descriptors = map->instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6419 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6420 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 6421 | DescriptorArray* new_descriptors; |
| 6422 | MaybeObject* maybe_descriptors = |
| 6423 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 6424 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6425 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6426 | return CopyReplaceDescriptors(new_descriptors, NULL, OMIT_TRANSITION, 0); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6430 | MaybeObject* Map::Copy() { |
| 6431 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6432 | DescriptorArray* new_descriptors; |
| 6433 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 6434 | MaybeObject* maybe_descriptors = |
| 6435 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 6436 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6437 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6438 | return CopyReplaceDescriptors(new_descriptors, NULL, OMIT_TRANSITION, 0); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | |
| 6442 | MaybeObject* Map::CopyAddDescriptor(Descriptor* descriptor, |
| 6443 | TransitionFlag flag) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6444 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6445 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6446 | // Ensure the key is unique. |
| 6447 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6448 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 6449 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6450 | int old_size = NumberOfOwnDescriptors(); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 6451 | int new_size = old_size + 1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6452 | |
| 6453 | if (flag == INSERT_TRANSITION && |
| 6454 | owns_descriptors() && |
| 6455 | CanHaveMoreTransitions()) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6456 | return ShareDescriptor(descriptors, descriptor); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6457 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6458 | |
| 6459 | DescriptorArray* new_descriptors; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6460 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate(old_size, 1); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6461 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6462 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 6463 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6464 | |
| 6465 | // Copy the descriptors, inserting a descriptor. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 6466 | for (int i = 0; i < old_size; ++i) { |
| 6467 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6468 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6469 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6470 | if (old_size != descriptors->number_of_descriptors()) { |
| 6471 | new_descriptors->SetNumberOfDescriptors(new_size); |
| 6472 | new_descriptors->Set(old_size, descriptor, witness); |
| 6473 | new_descriptors->Sort(); |
| 6474 | } else { |
| 6475 | new_descriptors->Append(descriptor, witness); |
| 6476 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6477 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6478 | Name* key = descriptor->GetKey(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6479 | int insertion_index = new_descriptors->number_of_descriptors() - 1; |
| 6480 | |
| 6481 | return CopyReplaceDescriptors(new_descriptors, key, flag, insertion_index); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6482 | } |
| 6483 | |
| 6484 | |
| 6485 | MaybeObject* Map::CopyInsertDescriptor(Descriptor* descriptor, |
| 6486 | TransitionFlag flag) { |
| 6487 | DescriptorArray* old_descriptors = instance_descriptors(); |
| 6488 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6489 | // Ensure the key is unique. |
| 6490 | MaybeObject* maybe_result = descriptor->KeyToUniqueName(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6491 | if (maybe_result->IsFailure()) return maybe_result; |
| 6492 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6493 | // We replace the key if it is already present. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6494 | int index = old_descriptors->SearchWithCache(descriptor->GetKey(), this); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6495 | if (index != DescriptorArray::kNotFound) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6496 | return CopyReplaceDescriptor(old_descriptors, descriptor, index, flag); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6497 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6498 | return CopyAddDescriptor(descriptor, flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6502 | MaybeObject* DescriptorArray::CopyUpTo(int enumeration_index) { |
| 6503 | if (enumeration_index == 0) return GetHeap()->empty_descriptor_array(); |
| 6504 | |
| 6505 | int size = enumeration_index; |
| 6506 | |
| 6507 | DescriptorArray* descriptors; |
| 6508 | MaybeObject* maybe_descriptors = Allocate(size); |
| 6509 | if (!maybe_descriptors->To(&descriptors)) return maybe_descriptors; |
| 6510 | DescriptorArray::WhitenessWitness witness(descriptors); |
| 6511 | |
| 6512 | for (int i = 0; i < size; ++i) { |
| 6513 | descriptors->CopyFrom(i, this, i, witness); |
| 6514 | } |
| 6515 | |
| 6516 | if (number_of_descriptors() != enumeration_index) descriptors->Sort(); |
| 6517 | |
| 6518 | return descriptors; |
| 6519 | } |
| 6520 | |
| 6521 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6522 | MaybeObject* Map::CopyReplaceDescriptor(DescriptorArray* descriptors, |
| 6523 | Descriptor* descriptor, |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6524 | int insertion_index, |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6525 | TransitionFlag flag) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6526 | // Ensure the key is unique. |
| 6527 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6528 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 6529 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6530 | Name* key = descriptor->GetKey(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6531 | ASSERT(key == descriptors->GetKey(insertion_index)); |
| 6532 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6533 | int new_size = NumberOfOwnDescriptors(); |
| 6534 | ASSERT(0 <= insertion_index && insertion_index < new_size); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 6535 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 6536 | ASSERT_LT(insertion_index, new_size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6537 | |
| 6538 | DescriptorArray* new_descriptors; |
| 6539 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate(new_size); |
| 6540 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 6541 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6542 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6543 | for (int i = 0; i < new_size; ++i) { |
| 6544 | if (i == insertion_index) { |
| 6545 | new_descriptors->Set(i, descriptor, witness); |
| 6546 | } else { |
| 6547 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 6548 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6551 | // Re-sort if descriptors were removed. |
| 6552 | if (new_size != descriptors->length()) new_descriptors->Sort(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 6553 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6554 | return CopyReplaceDescriptors(new_descriptors, key, flag, insertion_index); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6557 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6558 | void Map::UpdateCodeCache(Handle<Map> map, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6559 | Handle<Name> name, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6560 | Handle<Code> code) { |
| 6561 | Isolate* isolate = map->GetIsolate(); |
| 6562 | CALL_HEAP_FUNCTION_VOID(isolate, |
| 6563 | map->UpdateCodeCache(*name, *code)); |
| 6564 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6565 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6566 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6567 | MaybeObject* Map::UpdateCodeCache(Name* name, Code* code) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6568 | ASSERT(!is_shared() || code->allowed_in_shared_map_code_cache()); |
| 6569 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6570 | // Allocate the code cache if not present. |
| 6571 | if (code_cache()->IsFixedArray()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6572 | Object* result; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6573 | { MaybeObject* maybe_result = GetHeap()->AllocateCodeCache(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6574 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 6575 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6576 | set_code_cache(result); |
| 6577 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6578 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6579 | // Update the code cache. |
| 6580 | return CodeCache::cast(code_cache())->Update(name, code); |
| 6581 | } |
| 6582 | |
| 6583 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6584 | Object* Map::FindInCodeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6585 | // Do a lookup if a code cache exists. |
| 6586 | if (!code_cache()->IsFixedArray()) { |
| 6587 | return CodeCache::cast(code_cache())->Lookup(name, flags); |
| 6588 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6589 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6590 | } |
| 6591 | } |
| 6592 | |
| 6593 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 6594 | int Map::IndexInCodeCache(Object* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6595 | // Get the internal index if a code cache exists. |
| 6596 | if (!code_cache()->IsFixedArray()) { |
| 6597 | return CodeCache::cast(code_cache())->GetIndex(name, code); |
| 6598 | } |
| 6599 | return -1; |
| 6600 | } |
| 6601 | |
| 6602 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6603 | void Map::RemoveFromCodeCache(Name* name, Code* code, int index) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6604 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 6605 | // RemoveFromCodeCache so the code cache must be there. |
| 6606 | ASSERT(!code_cache()->IsFixedArray()); |
| 6607 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 6608 | } |
| 6609 | |
| 6610 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6611 | // An iterator over all map transitions in an descriptor array, reusing the map |
| 6612 | // field of the contens array while it is running. |
| 6613 | class IntrusiveMapTransitionIterator { |
| 6614 | public: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6615 | explicit IntrusiveMapTransitionIterator(TransitionArray* transition_array) |
| 6616 | : transition_array_(transition_array) { } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 6617 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6618 | void Start() { |
| 6619 | ASSERT(!IsIterating()); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6620 | *TransitionArrayHeader() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6621 | } |
| 6622 | |
| 6623 | bool IsIterating() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6624 | return (*TransitionArrayHeader())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
| 6627 | Map* Next() { |
| 6628 | ASSERT(IsIterating()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6629 | int index = Smi::cast(*TransitionArrayHeader())->value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6630 | int number_of_transitions = transition_array_->number_of_transitions(); |
| 6631 | while (index < number_of_transitions) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6632 | *TransitionArrayHeader() = Smi::FromInt(index + 1); |
| 6633 | return transition_array_->GetTarget(index); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6634 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6635 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6636 | if (index == number_of_transitions && |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6637 | transition_array_->HasElementsTransition()) { |
| 6638 | Map* elements_transition = transition_array_->elements_transition(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6639 | *TransitionArrayHeader() = Smi::FromInt(index + 1); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6640 | return elements_transition; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 6641 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6642 | *TransitionArrayHeader() = transition_array_->GetHeap()->fixed_array_map(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6643 | return NULL; |
| 6644 | } |
| 6645 | |
| 6646 | private: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6647 | Object** TransitionArrayHeader() { |
| 6648 | return HeapObject::RawField(transition_array_, TransitionArray::kMapOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6651 | TransitionArray* transition_array_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6652 | }; |
| 6653 | |
| 6654 | |
| 6655 | // An iterator over all prototype transitions, reusing the map field of the |
| 6656 | // underlying array while it is running. |
| 6657 | class IntrusivePrototypeTransitionIterator { |
| 6658 | public: |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6659 | explicit IntrusivePrototypeTransitionIterator(HeapObject* proto_trans) |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6660 | : proto_trans_(proto_trans) { } |
| 6661 | |
| 6662 | void Start() { |
| 6663 | ASSERT(!IsIterating()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 6664 | *Header() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | bool IsIterating() { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 6668 | return (*Header())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6669 | } |
| 6670 | |
| 6671 | Map* Next() { |
| 6672 | ASSERT(IsIterating()); |
| 6673 | int transitionNumber = Smi::cast(*Header())->value(); |
| 6674 | if (transitionNumber < NumberOfTransitions()) { |
| 6675 | *Header() = Smi::FromInt(transitionNumber + 1); |
| 6676 | return GetTransition(transitionNumber); |
| 6677 | } |
| 6678 | *Header() = proto_trans_->GetHeap()->fixed_array_map(); |
| 6679 | return NULL; |
| 6680 | } |
| 6681 | |
| 6682 | private: |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6683 | Object** Header() { |
| 6684 | return HeapObject::RawField(proto_trans_, FixedArray::kMapOffset); |
| 6685 | } |
| 6686 | |
| 6687 | int NumberOfTransitions() { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6688 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 6689 | Object* num = proto_trans->get(Map::kProtoTransitionNumberOfEntriesOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6690 | return Smi::cast(num)->value(); |
| 6691 | } |
| 6692 | |
| 6693 | Map* GetTransition(int transitionNumber) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6694 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 6695 | return Map::cast(proto_trans->get(IndexFor(transitionNumber))); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | int IndexFor(int transitionNumber) { |
| 6699 | return Map::kProtoTransitionHeaderSize + |
| 6700 | Map::kProtoTransitionMapOffset + |
| 6701 | transitionNumber * Map::kProtoTransitionElementsPerEntry; |
| 6702 | } |
| 6703 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 6704 | HeapObject* proto_trans_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6705 | }; |
| 6706 | |
| 6707 | |
| 6708 | // To traverse the transition tree iteratively, we have to store two kinds of |
| 6709 | // information in a map: The parent map in the traversal and which children of a |
| 6710 | // node have already been visited. To do this without additional memory, we |
| 6711 | // temporarily reuse two maps with known values: |
| 6712 | // |
| 6713 | // (1) The map of the map temporarily holds the parent, and is restored to the |
| 6714 | // meta map afterwards. |
| 6715 | // |
| 6716 | // (2) The info which children have already been visited depends on which part |
| 6717 | // of the map we currently iterate: |
| 6718 | // |
| 6719 | // (a) If we currently follow normal map transitions, we temporarily store |
| 6720 | // the current index in the map of the FixedArray of the desciptor |
| 6721 | // array's contents, and restore it to the fixed array map afterwards. |
| 6722 | // Note that a single descriptor can have 0, 1, or 2 transitions. |
| 6723 | // |
| 6724 | // (b) If we currently follow prototype transitions, we temporarily store |
| 6725 | // the current index in the map of the FixedArray holding the prototype |
| 6726 | // transitions, and restore it to the fixed array map afterwards. |
| 6727 | // |
| 6728 | // Note that the child iterator is just a concatenation of two iterators: One |
| 6729 | // iterating over map transitions and one iterating over prototype transisitons. |
| 6730 | class TraversableMap : public Map { |
| 6731 | public: |
| 6732 | // Record the parent in the traversal within this map. Note that this destroys |
| 6733 | // this map's map! |
| 6734 | void SetParent(TraversableMap* parent) { set_map_no_write_barrier(parent); } |
| 6735 | |
| 6736 | // Reset the current map's map, returning the parent previously stored in it. |
| 6737 | TraversableMap* GetAndResetParent() { |
| 6738 | TraversableMap* old_parent = static_cast<TraversableMap*>(map()); |
| 6739 | set_map_no_write_barrier(GetHeap()->meta_map()); |
| 6740 | return old_parent; |
| 6741 | } |
| 6742 | |
| 6743 | // Start iterating over this map's children, possibly destroying a FixedArray |
| 6744 | // map (see explanation above). |
| 6745 | void ChildIteratorStart() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6746 | if (HasTransitionArray()) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 6747 | if (HasPrototypeTransitions()) { |
| 6748 | IntrusivePrototypeTransitionIterator(GetPrototypeTransitions()).Start(); |
| 6749 | } |
| 6750 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6751 | IntrusiveMapTransitionIterator(transitions()).Start(); |
| 6752 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | // If we have an unvisited child map, return that one and advance. If we have |
| 6756 | // none, return NULL and reset any destroyed FixedArray maps. |
| 6757 | TraversableMap* ChildIteratorNext() { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6758 | TransitionArray* transition_array = unchecked_transition_array(); |
| 6759 | if (!transition_array->map()->IsSmi() && |
| 6760 | !transition_array->IsTransitionArray()) { |
| 6761 | return NULL; |
| 6762 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 6763 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6764 | if (transition_array->HasPrototypeTransitions()) { |
| 6765 | HeapObject* proto_transitions = |
| 6766 | transition_array->UncheckedPrototypeTransitions(); |
| 6767 | IntrusivePrototypeTransitionIterator proto_iterator(proto_transitions); |
| 6768 | if (proto_iterator.IsIterating()) { |
| 6769 | Map* next = proto_iterator.Next(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6770 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 6771 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 6772 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 6773 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6774 | IntrusiveMapTransitionIterator transition_iterator(transition_array); |
| 6775 | if (transition_iterator.IsIterating()) { |
| 6776 | Map* next = transition_iterator.Next(); |
| 6777 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 6778 | } |
| 6779 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 6780 | return NULL; |
| 6781 | } |
| 6782 | }; |
| 6783 | |
| 6784 | |
| 6785 | // Traverse the transition tree in postorder without using the C++ stack by |
| 6786 | // doing pointer reversal. |
| 6787 | void Map::TraverseTransitionTree(TraverseCallback callback, void* data) { |
| 6788 | TraversableMap* current = static_cast<TraversableMap*>(this); |
| 6789 | current->ChildIteratorStart(); |
| 6790 | while (true) { |
| 6791 | TraversableMap* child = current->ChildIteratorNext(); |
| 6792 | if (child != NULL) { |
| 6793 | child->ChildIteratorStart(); |
| 6794 | child->SetParent(current); |
| 6795 | current = child; |
| 6796 | } else { |
| 6797 | TraversableMap* parent = current->GetAndResetParent(); |
| 6798 | callback(current, data); |
| 6799 | if (current == this) break; |
| 6800 | current = parent; |
| 6801 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6802 | } |
| 6803 | } |
| 6804 | |
| 6805 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6806 | MaybeObject* CodeCache::Update(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6807 | // The number of monomorphic stubs for normal load/store/call IC's can grow to |
| 6808 | // a large number and therefore they need to go into a hash table. They are |
| 6809 | // used to load global properties from cells. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 6810 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6811 | // Make sure that a hash table is allocated for the normal load code cache. |
| 6812 | if (normal_type_cache()->IsUndefined()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6813 | Object* result; |
| 6814 | { MaybeObject* maybe_result = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 6815 | CodeCacheHashTable::Allocate(GetHeap(), |
| 6816 | CodeCacheHashTable::kInitialSize); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6817 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 6818 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6819 | set_normal_type_cache(result); |
| 6820 | } |
| 6821 | return UpdateNormalTypeCache(name, code); |
| 6822 | } else { |
| 6823 | ASSERT(default_cache()->IsFixedArray()); |
| 6824 | return UpdateDefaultCache(name, code); |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6829 | MaybeObject* CodeCache::UpdateDefaultCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6830 | // When updating the default code cache we disregard the type encoded in the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6831 | // flags. This allows call constant stubs to overwrite call field |
| 6832 | // stubs, etc. |
| 6833 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 6834 | |
| 6835 | // First check whether we can update existing code cache without |
| 6836 | // extending it. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6837 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6838 | int length = cache->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6839 | int deleted_index = -1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6840 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6841 | Object* key = cache->get(i); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6842 | if (key->IsNull()) { |
| 6843 | if (deleted_index < 0) deleted_index = i; |
| 6844 | continue; |
| 6845 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6846 | if (key->IsUndefined()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6847 | if (deleted_index >= 0) i = deleted_index; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6848 | cache->set(i + kCodeCacheEntryNameOffset, name); |
| 6849 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6850 | return this; |
| 6851 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6852 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6853 | Code::Flags found = |
| 6854 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6855 | if (Code::RemoveTypeFromFlags(found) == flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6856 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6857 | return this; |
| 6858 | } |
| 6859 | } |
| 6860 | } |
| 6861 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6862 | // Reached the end of the code cache. If there were deleted |
| 6863 | // elements, reuse the space for the first of them. |
| 6864 | if (deleted_index >= 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6865 | cache->set(deleted_index + kCodeCacheEntryNameOffset, name); |
| 6866 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, code); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6867 | return this; |
| 6868 | } |
| 6869 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6870 | // Extend the code cache with some new entries (at least one). Must be a |
| 6871 | // multiple of the entry size. |
| 6872 | int new_length = length + ((length >> 1)) + kCodeCacheEntrySize; |
| 6873 | new_length = new_length - new_length % kCodeCacheEntrySize; |
| 6874 | ASSERT((new_length % kCodeCacheEntrySize) == 0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6875 | Object* result; |
| 6876 | { MaybeObject* maybe_result = cache->CopySize(new_length); |
| 6877 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 6878 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6879 | |
| 6880 | // Add the (name, code) pair to the new cache. |
| 6881 | cache = FixedArray::cast(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6882 | cache->set(length + kCodeCacheEntryNameOffset, name); |
| 6883 | cache->set(length + kCodeCacheEntryCodeOffset, code); |
| 6884 | set_default_cache(cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6885 | return this; |
| 6886 | } |
| 6887 | |
| 6888 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6889 | MaybeObject* CodeCache::UpdateNormalTypeCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6890 | // Adding a new entry can cause a new cache to be allocated. |
| 6891 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6892 | Object* new_cache; |
| 6893 | { MaybeObject* maybe_new_cache = cache->Put(name, code); |
| 6894 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 6895 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6896 | set_normal_type_cache(new_cache); |
| 6897 | return this; |
| 6898 | } |
| 6899 | |
| 6900 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6901 | Object* CodeCache::Lookup(Name* name, Code::Flags flags) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 6902 | if (Code::ExtractTypeFromFlags(flags) == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6903 | return LookupNormalTypeCache(name, flags); |
| 6904 | } else { |
| 6905 | return LookupDefaultCache(name, flags); |
| 6906 | } |
| 6907 | } |
| 6908 | |
| 6909 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6910 | Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6911 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6912 | int length = cache->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6913 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 6914 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 6915 | // Skip deleted elements. |
| 6916 | if (key->IsNull()) continue; |
| 6917 | if (key->IsUndefined()) return key; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6918 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6919 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
| 6920 | if (code->flags() == flags) { |
| 6921 | return code; |
| 6922 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6923 | } |
| 6924 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6925 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6926 | } |
| 6927 | |
| 6928 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6929 | Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6930 | if (!normal_type_cache()->IsUndefined()) { |
| 6931 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 6932 | return cache->Lookup(name, flags); |
| 6933 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6934 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6935 | } |
| 6936 | } |
| 6937 | |
| 6938 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 6939 | int CodeCache::GetIndex(Object* name, Code* code) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 6940 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6941 | if (normal_type_cache()->IsUndefined()) return -1; |
| 6942 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6943 | return cache->GetIndex(Name::cast(name), code->flags()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | FixedArray* array = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6947 | int len = array->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6948 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 6949 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6950 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 6951 | return -1; |
| 6952 | } |
| 6953 | |
| 6954 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 6955 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 6956 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6957 | ASSERT(!normal_type_cache()->IsUndefined()); |
| 6958 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6959 | ASSERT(cache->GetIndex(Name::cast(name), code->flags()) == index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6960 | cache->RemoveByIndex(index); |
| 6961 | } else { |
| 6962 | FixedArray* array = default_cache(); |
| 6963 | ASSERT(array->length() >= index && array->get(index)->IsCode()); |
| 6964 | // Use null instead of undefined for deleted elements to distinguish |
| 6965 | // deleted elements from unused elements. This distinction is used |
| 6966 | // when looking up in the cache and when updating the cache. |
| 6967 | ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
| 6968 | array->set_null(index - 1); // Name. |
| 6969 | array->set_null(index); // Code. |
| 6970 | } |
| 6971 | } |
| 6972 | |
| 6973 | |
| 6974 | // The key in the code cache hash table consists of the property name and the |
| 6975 | // code object. The actual match is on the name and the code flags. If a key |
| 6976 | // is created using the flags and not a code object it can only be used for |
| 6977 | // lookup not to create a new entry. |
| 6978 | class CodeCacheHashTableKey : public HashTableKey { |
| 6979 | public: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6980 | CodeCacheHashTableKey(Name* name, Code::Flags flags) |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6981 | : name_(name), flags_(flags), code_(NULL) { } |
| 6982 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6983 | CodeCacheHashTableKey(Name* name, Code* code) |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6984 | : name_(name), |
| 6985 | flags_(code->flags()), |
| 6986 | code_(code) { } |
| 6987 | |
| 6988 | |
| 6989 | bool IsMatch(Object* other) { |
| 6990 | if (!other->IsFixedArray()) return false; |
| 6991 | FixedArray* pair = FixedArray::cast(other); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6992 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6993 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 6994 | if (flags != flags_) { |
| 6995 | return false; |
| 6996 | } |
| 6997 | return name_->Equals(name); |
| 6998 | } |
| 6999 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7000 | static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7001 | return name->Hash() ^ flags; |
| 7002 | } |
| 7003 | |
| 7004 | uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); } |
| 7005 | |
| 7006 | uint32_t HashForObject(Object* obj) { |
| 7007 | FixedArray* pair = FixedArray::cast(obj); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7008 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7009 | Code* code = Code::cast(pair->get(1)); |
| 7010 | return NameFlagsHashHelper(name, code->flags()); |
| 7011 | } |
| 7012 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7013 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7014 | ASSERT(code_ != NULL); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7015 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7016 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7017 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7018 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7019 | FixedArray* pair = FixedArray::cast(obj); |
| 7020 | pair->set(0, name_); |
| 7021 | pair->set(1, code_); |
| 7022 | return pair; |
| 7023 | } |
| 7024 | |
| 7025 | private: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7026 | Name* name_; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7027 | Code::Flags flags_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7028 | // TODO(jkummerow): We should be able to get by without this. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7029 | Code* code_; |
| 7030 | }; |
| 7031 | |
| 7032 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7033 | Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7034 | CodeCacheHashTableKey key(name, flags); |
| 7035 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7036 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7037 | return get(EntryToIndex(entry) + 1); |
| 7038 | } |
| 7039 | |
| 7040 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7041 | MaybeObject* CodeCacheHashTable::Put(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7042 | CodeCacheHashTableKey key(name, code); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7043 | Object* obj; |
| 7044 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7045 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7046 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7047 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7048 | // Don't use |this|, as the table might have grown. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7049 | CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj); |
| 7050 | |
| 7051 | int entry = cache->FindInsertionEntry(key.Hash()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7052 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7053 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7054 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 7055 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7056 | |
| 7057 | cache->set(EntryToIndex(entry), k); |
| 7058 | cache->set(EntryToIndex(entry) + 1, code); |
| 7059 | cache->ElementAdded(); |
| 7060 | return cache; |
| 7061 | } |
| 7062 | |
| 7063 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7064 | int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7065 | CodeCacheHashTableKey key(name, flags); |
| 7066 | int entry = FindEntry(&key); |
| 7067 | return (entry == kNotFound) ? -1 : entry; |
| 7068 | } |
| 7069 | |
| 7070 | |
| 7071 | void CodeCacheHashTable::RemoveByIndex(int index) { |
| 7072 | ASSERT(index >= 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7073 | Heap* heap = GetHeap(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7074 | set(EntryToIndex(index), heap->the_hole_value()); |
| 7075 | set(EntryToIndex(index) + 1, heap->the_hole_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7076 | ElementRemoved(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
| 7079 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7080 | void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> cache, |
| 7081 | MapHandleList* maps, |
| 7082 | Code::Flags flags, |
| 7083 | Handle<Code> code) { |
| 7084 | Isolate* isolate = cache->GetIsolate(); |
| 7085 | CALL_HEAP_FUNCTION_VOID(isolate, cache->Update(maps, flags, *code)); |
| 7086 | } |
| 7087 | |
| 7088 | |
| 7089 | MaybeObject* PolymorphicCodeCache::Update(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7090 | Code::Flags flags, |
| 7091 | Code* code) { |
| 7092 | // Initialize cache if necessary. |
| 7093 | if (cache()->IsUndefined()) { |
| 7094 | Object* result; |
| 7095 | { MaybeObject* maybe_result = |
| 7096 | PolymorphicCodeCacheHashTable::Allocate( |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7097 | GetHeap(), |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7098 | PolymorphicCodeCacheHashTable::kInitialSize); |
| 7099 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7100 | } |
| 7101 | set_cache(result); |
| 7102 | } else { |
| 7103 | // This entry shouldn't be contained in the cache yet. |
| 7104 | ASSERT(PolymorphicCodeCacheHashTable::cast(cache()) |
| 7105 | ->Lookup(maps, flags)->IsUndefined()); |
| 7106 | } |
| 7107 | PolymorphicCodeCacheHashTable* hash_table = |
| 7108 | PolymorphicCodeCacheHashTable::cast(cache()); |
| 7109 | Object* new_cache; |
| 7110 | { MaybeObject* maybe_new_cache = hash_table->Put(maps, flags, code); |
| 7111 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 7112 | } |
| 7113 | set_cache(new_cache); |
| 7114 | return this; |
| 7115 | } |
| 7116 | |
| 7117 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7118 | Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps, |
| 7119 | Code::Flags flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7120 | if (!cache()->IsUndefined()) { |
| 7121 | PolymorphicCodeCacheHashTable* hash_table = |
| 7122 | PolymorphicCodeCacheHashTable::cast(cache()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 7123 | return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7124 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7125 | return GetIsolate()->factory()->undefined_value(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | |
| 7130 | // Despite their name, object of this class are not stored in the actual |
| 7131 | // hash table; instead they're temporarily used for lookups. It is therefore |
| 7132 | // safe to have a weak (non-owning) pointer to a MapList as a member field. |
| 7133 | class PolymorphicCodeCacheHashTableKey : public HashTableKey { |
| 7134 | public: |
| 7135 | // Callers must ensure that |maps| outlives the newly constructed object. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7136 | PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags) |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7137 | : maps_(maps), |
| 7138 | code_flags_(code_flags) {} |
| 7139 | |
| 7140 | bool IsMatch(Object* other) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7141 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7142 | int other_flags; |
| 7143 | FromObject(other, &other_flags, &other_maps); |
| 7144 | if (code_flags_ != other_flags) return false; |
| 7145 | if (maps_->length() != other_maps.length()) return false; |
| 7146 | // Compare just the hashes first because it's faster. |
| 7147 | int this_hash = MapsHashHelper(maps_, code_flags_); |
| 7148 | int other_hash = MapsHashHelper(&other_maps, other_flags); |
| 7149 | if (this_hash != other_hash) return false; |
| 7150 | |
| 7151 | // Full comparison: for each map in maps_, look for an equivalent map in |
| 7152 | // other_maps. This implementation is slow, but probably good enough for |
| 7153 | // now because the lists are short (<= 4 elements currently). |
| 7154 | for (int i = 0; i < maps_->length(); ++i) { |
| 7155 | bool match_found = false; |
| 7156 | for (int j = 0; j < other_maps.length(); ++j) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7157 | if (*(maps_->at(i)) == *(other_maps.at(j))) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7158 | match_found = true; |
| 7159 | break; |
| 7160 | } |
| 7161 | } |
| 7162 | if (!match_found) return false; |
| 7163 | } |
| 7164 | return true; |
| 7165 | } |
| 7166 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7167 | static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7168 | uint32_t hash = code_flags; |
| 7169 | for (int i = 0; i < maps->length(); ++i) { |
| 7170 | hash ^= maps->at(i)->Hash(); |
| 7171 | } |
| 7172 | return hash; |
| 7173 | } |
| 7174 | |
| 7175 | uint32_t Hash() { |
| 7176 | return MapsHashHelper(maps_, code_flags_); |
| 7177 | } |
| 7178 | |
| 7179 | uint32_t HashForObject(Object* obj) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7180 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7181 | int other_flags; |
| 7182 | FromObject(obj, &other_flags, &other_maps); |
| 7183 | return MapsHashHelper(&other_maps, other_flags); |
| 7184 | } |
| 7185 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7186 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7187 | Object* obj; |
| 7188 | // The maps in |maps_| must be copied to a newly allocated FixedArray, |
| 7189 | // both because the referenced MapList is short-lived, and because C++ |
| 7190 | // objects can't be stored in the heap anyway. |
| 7191 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7192 | heap->AllocateUninitializedFixedArray(maps_->length() + 1); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7193 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7194 | } |
| 7195 | FixedArray* list = FixedArray::cast(obj); |
| 7196 | list->set(0, Smi::FromInt(code_flags_)); |
| 7197 | for (int i = 0; i < maps_->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7198 | list->set(i + 1, *maps_->at(i)); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7199 | } |
| 7200 | return list; |
| 7201 | } |
| 7202 | |
| 7203 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7204 | static MapHandleList* FromObject(Object* obj, |
| 7205 | int* code_flags, |
| 7206 | MapHandleList* maps) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7207 | FixedArray* list = FixedArray::cast(obj); |
| 7208 | maps->Rewind(0); |
| 7209 | *code_flags = Smi::cast(list->get(0))->value(); |
| 7210 | for (int i = 1; i < list->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7211 | maps->Add(Handle<Map>(Map::cast(list->get(i)))); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7212 | } |
| 7213 | return maps; |
| 7214 | } |
| 7215 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7216 | MapHandleList* maps_; // weak. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7217 | int code_flags_; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7218 | static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7219 | }; |
| 7220 | |
| 7221 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7222 | Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps, |
| 7223 | int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7224 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7225 | int entry = FindEntry(&key); |
| 7226 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
| 7227 | return get(EntryToIndex(entry) + 1); |
| 7228 | } |
| 7229 | |
| 7230 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7231 | MaybeObject* PolymorphicCodeCacheHashTable::Put(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7232 | int code_flags, |
| 7233 | Code* code) { |
| 7234 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7235 | Object* obj; |
| 7236 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7237 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7238 | } |
| 7239 | PolymorphicCodeCacheHashTable* cache = |
| 7240 | reinterpret_cast<PolymorphicCodeCacheHashTable*>(obj); |
| 7241 | int entry = cache->FindInsertionEntry(key.Hash()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7242 | { MaybeObject* maybe_obj = key.AsObject(GetHeap()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7243 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7244 | } |
| 7245 | cache->set(EntryToIndex(entry), obj); |
| 7246 | cache->set(EntryToIndex(entry) + 1, code); |
| 7247 | cache->ElementAdded(); |
| 7248 | return cache; |
| 7249 | } |
| 7250 | |
| 7251 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7252 | MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7253 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 7254 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7255 | accessor->AddElementsToFixedArray(array, array, this); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7256 | FixedArray* result; |
| 7257 | if (!maybe_result->To<FixedArray>(&result)) return maybe_result; |
| 7258 | #ifdef DEBUG |
| 7259 | if (FLAG_enable_slow_asserts) { |
| 7260 | for (int i = 0; i < result->length(); i++) { |
| 7261 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7262 | ASSERT(current->IsNumber() || current->IsName()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7263 | } |
| 7264 | } |
| 7265 | #endif |
| 7266 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7270 | MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7271 | ElementsAccessor* accessor = ElementsAccessor::ForArray(other); |
| 7272 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7273 | accessor->AddElementsToFixedArray(NULL, NULL, this, other); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7274 | FixedArray* result; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 7275 | if (!maybe_result->To(&result)) return maybe_result; |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7276 | #ifdef DEBUG |
| 7277 | if (FLAG_enable_slow_asserts) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7278 | for (int i = 0; i < result->length(); i++) { |
| 7279 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7280 | ASSERT(current->IsNumber() || current->IsName()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7281 | } |
| 7282 | } |
| 7283 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7284 | return result; |
| 7285 | } |
| 7286 | |
| 7287 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7288 | MaybeObject* FixedArray::CopySize(int new_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7289 | Heap* heap = GetHeap(); |
| 7290 | if (new_length == 0) return heap->empty_fixed_array(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7291 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7292 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7293 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7294 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7295 | FixedArray* result = FixedArray::cast(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7296 | // Copy the content |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7297 | AssertNoAllocation no_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7298 | int len = length(); |
| 7299 | if (new_length < len) len = new_length; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7300 | // We are taking the map from the old fixed array so the map is sure to |
| 7301 | // be an immortal immutable object. |
| 7302 | result->set_map_no_write_barrier(map()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7303 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7304 | for (int i = 0; i < len; i++) { |
| 7305 | result->set(i, get(i), mode); |
| 7306 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7307 | return result; |
| 7308 | } |
| 7309 | |
| 7310 | |
| 7311 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7312 | AssertNoAllocation no_gc; |
| 7313 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7314 | for (int index = 0; index < len; index++) { |
| 7315 | dest->set(dest_pos+index, get(pos+index), mode); |
| 7316 | } |
| 7317 | } |
| 7318 | |
| 7319 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7320 | #ifdef DEBUG |
| 7321 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 7322 | if (length() != other->length()) return false; |
| 7323 | for (int i = 0 ; i < length(); ++i) { |
| 7324 | if (get(i) != other->get(i)) return false; |
| 7325 | } |
| 7326 | return true; |
| 7327 | } |
| 7328 | #endif |
| 7329 | |
| 7330 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7331 | MaybeObject* DescriptorArray::Allocate(int number_of_descriptors, int slack) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7332 | Heap* heap = Isolate::Current()->heap(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7333 | // Do not use DescriptorArray::cast on incomplete object. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7334 | int size = number_of_descriptors + slack; |
| 7335 | if (size == 0) return heap->empty_descriptor_array(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7336 | FixedArray* result; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7337 | // Allocate the array of keys. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7338 | MaybeObject* maybe_array = heap->AllocateFixedArray(LengthFor(size)); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 7339 | if (!maybe_array->To(&result)) return maybe_array; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7340 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7341 | result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7342 | result->set(kEnumCacheIndex, Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7343 | return result; |
| 7344 | } |
| 7345 | |
| 7346 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7347 | void DescriptorArray::ClearEnumCache() { |
| 7348 | set(kEnumCacheIndex, Smi::FromInt(0)); |
| 7349 | } |
| 7350 | |
| 7351 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7352 | void DescriptorArray::SetEnumCache(FixedArray* bridge_storage, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7353 | FixedArray* new_cache, |
| 7354 | Object* new_index_cache) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7355 | ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7356 | ASSERT(new_index_cache->IsSmi() || new_index_cache->IsFixedArray()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 7357 | ASSERT(!IsEmpty()); |
| 7358 | ASSERT(!HasEnumCache() || new_cache->length() > GetEnumCache()->length()); |
| 7359 | FixedArray::cast(bridge_storage)-> |
| 7360 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 7361 | FixedArray::cast(bridge_storage)-> |
| 7362 | set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache); |
| 7363 | set(kEnumCacheIndex, bridge_storage); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7364 | } |
| 7365 | |
| 7366 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7367 | void DescriptorArray::CopyFrom(int dst_index, |
| 7368 | DescriptorArray* src, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7369 | int src_index, |
| 7370 | const WhitenessWitness& witness) { |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7371 | Object* value = src->GetValue(src_index); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 7372 | PropertyDetails details = src->GetDetails(src_index); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7373 | Descriptor desc(src->GetKey(src_index), value, details); |
| 7374 | Set(dst_index, &desc, witness); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7377 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 7378 | // Generalize the |other| descriptor array by merging it into the (at least |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7379 | // partly) updated |this| descriptor array. |
| 7380 | // The method merges two descriptor array in three parts. Both descriptor arrays |
| 7381 | // are identical up to |verbatim|. They also overlap in keys up to |valid|. |
| 7382 | // Between |verbatim| and |valid|, the resulting descriptor type as well as the |
| 7383 | // representation are generalized from both |this| and |other|. Beyond |valid|, |
| 7384 | // the descriptors are copied verbatim from |other| up to |new_size|. |
| 7385 | // In case of incompatible types, the type and representation of |other| is |
| 7386 | // used. |
| 7387 | MaybeObject* DescriptorArray::Merge(int verbatim, |
| 7388 | int valid, |
| 7389 | int new_size, |
| 7390 | DescriptorArray* other) { |
| 7391 | ASSERT(verbatim <= valid); |
| 7392 | ASSERT(valid <= new_size); |
| 7393 | |
| 7394 | DescriptorArray* result; |
| 7395 | // Allocate a new descriptor array large enough to hold the required |
| 7396 | // descriptors, with minimally the exact same size as this descriptor array. |
| 7397 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 7398 | new_size, Max(new_size, other->number_of_descriptors()) - new_size); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7399 | if (!maybe_descriptors->To(&result)) return maybe_descriptors; |
| 7400 | ASSERT(result->length() > length() || |
| 7401 | result->NumberOfSlackDescriptors() > 0 || |
| 7402 | result->number_of_descriptors() == other->number_of_descriptors()); |
| 7403 | ASSERT(result->number_of_descriptors() == new_size); |
| 7404 | |
| 7405 | DescriptorArray::WhitenessWitness witness(result); |
| 7406 | |
| 7407 | int descriptor; |
| 7408 | |
| 7409 | // 0 -> |verbatim| |
| 7410 | int current_offset = 0; |
| 7411 | for (descriptor = 0; descriptor < verbatim; descriptor++) { |
| 7412 | if (GetDetails(descriptor).type() == FIELD) current_offset++; |
| 7413 | result->CopyFrom(descriptor, this, descriptor, witness); |
| 7414 | } |
| 7415 | |
| 7416 | // |verbatim| -> |valid| |
| 7417 | for (; descriptor < valid; descriptor++) { |
| 7418 | Name* key = GetKey(descriptor); |
| 7419 | PropertyDetails details = GetDetails(descriptor); |
| 7420 | PropertyDetails other_details = other->GetDetails(descriptor); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7421 | |
| 7422 | if (details.type() == FIELD || other_details.type() == FIELD || |
| 7423 | (details.type() == CONSTANT_FUNCTION && |
| 7424 | other_details.type() == CONSTANT_FUNCTION && |
| 7425 | GetValue(descriptor) != other->GetValue(descriptor))) { |
| 7426 | Representation representation = |
| 7427 | details.representation().generalize(other_details.representation()); |
| 7428 | FieldDescriptor d(key, |
| 7429 | current_offset++, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 7430 | other_details.attributes(), |
| 7431 | representation); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7432 | result->Set(descriptor, &d, witness); |
| 7433 | } else { |
| 7434 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 7435 | } |
| 7436 | } |
| 7437 | |
| 7438 | // |valid| -> |new_size| |
| 7439 | for (; descriptor < new_size; descriptor++) { |
| 7440 | PropertyDetails details = other->GetDetails(descriptor); |
| 7441 | if (details.type() == FIELD) { |
| 7442 | Name* key = other->GetKey(descriptor); |
| 7443 | FieldDescriptor d(key, |
| 7444 | current_offset++, |
| 7445 | details.attributes(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 7446 | details.representation()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7447 | result->Set(descriptor, &d, witness); |
| 7448 | } else { |
| 7449 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 7450 | } |
| 7451 | } |
| 7452 | |
| 7453 | result->Sort(); |
| 7454 | return result; |
| 7455 | } |
| 7456 | |
| 7457 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 7458 | // Checks whether a merge of |other| into |this| would return a copy of |this|. |
| 7459 | bool DescriptorArray::IsMoreGeneralThan(int verbatim, |
| 7460 | int valid, |
| 7461 | int new_size, |
| 7462 | DescriptorArray* other) { |
| 7463 | ASSERT(verbatim <= valid); |
| 7464 | ASSERT(valid <= new_size); |
| 7465 | if (valid != new_size) return false; |
| 7466 | |
| 7467 | for (int descriptor = verbatim; descriptor < valid; descriptor++) { |
| 7468 | PropertyDetails details = GetDetails(descriptor); |
| 7469 | PropertyDetails other_details = other->GetDetails(descriptor); |
| 7470 | if (details.type() != other_details.type()) { |
| 7471 | if (details.type() != FIELD || |
| 7472 | other_details.type() != CONSTANT_FUNCTION) { |
| 7473 | return false; |
| 7474 | } |
| 7475 | } else if (details.type() == CONSTANT_FUNCTION) { |
| 7476 | if (GetValue(descriptor) != other->GetValue(descriptor)) { |
| 7477 | return false; |
| 7478 | } |
| 7479 | } else if (!other_details.representation().fits_into( |
| 7480 | details.representation())) { |
| 7481 | return false; |
| 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | return true; |
| 7486 | } |
| 7487 | |
| 7488 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 7489 | // We need the whiteness witness since sort will reshuffle the entries in the |
| 7490 | // descriptor array. If the descriptor array were to be black, the shuffling |
| 7491 | // would move a slot that was already recorded as pointing into an evacuation |
| 7492 | // candidate. This would result in missing updates upon evacuation. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7493 | void DescriptorArray::Sort() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7494 | // In-place heap sort. |
| 7495 | int len = number_of_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7496 | // Reset sorting since the descriptor array might contain invalid pointers. |
| 7497 | for (int i = 0; i < len; ++i) SetSortedKey(i, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7498 | // Bottom-up max-heap construction. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7499 | // Index of the last node with children |
| 7500 | const int max_parent_index = (len / 2) - 1; |
| 7501 | for (int i = max_parent_index; i >= 0; --i) { |
| 7502 | int parent_index = i; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7503 | const uint32_t parent_hash = GetSortedKey(i)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7504 | while (parent_index <= max_parent_index) { |
| 7505 | int child_index = 2 * parent_index + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7506 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7507 | if (child_index + 1 < len) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7508 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7509 | if (right_child_hash > child_hash) { |
| 7510 | child_index++; |
| 7511 | child_hash = right_child_hash; |
| 7512 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7513 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7514 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7515 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7516 | // Now element at child_index could be < its children. |
| 7517 | parent_index = child_index; // parent_hash remains correct. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | // Extract elements and create sorted array. |
| 7522 | for (int i = len - 1; i > 0; --i) { |
| 7523 | // Put max element at the back of the array. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7524 | SwapSortedKeys(0, i); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7525 | // Shift down the new top element. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7526 | int parent_index = 0; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7527 | const uint32_t parent_hash = GetSortedKey(parent_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7528 | const int max_parent_index = (i / 2) - 1; |
| 7529 | while (parent_index <= max_parent_index) { |
| 7530 | int child_index = parent_index * 2 + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7531 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7532 | if (child_index + 1 < i) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7533 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7534 | if (right_child_hash > child_hash) { |
| 7535 | child_index++; |
| 7536 | child_hash = right_child_hash; |
| 7537 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7538 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7539 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7540 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7541 | parent_index = child_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7542 | } |
| 7543 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7544 | ASSERT(IsSortedNoDuplicates()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7548 | MaybeObject* AccessorPair::Copy() { |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7549 | Heap* heap = GetHeap(); |
| 7550 | AccessorPair* copy; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7551 | MaybeObject* maybe_copy = heap->AllocateAccessorPair(); |
| 7552 | if (!maybe_copy->To(©)) return maybe_copy; |
| 7553 | |
| 7554 | copy->set_getter(getter()); |
| 7555 | copy->set_setter(setter()); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7556 | return copy; |
| 7557 | } |
| 7558 | |
| 7559 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7560 | Object* AccessorPair::GetComponent(AccessorComponent component) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 7561 | Object* accessor = get(component); |
| 7562 | return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7563 | } |
| 7564 | |
| 7565 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7566 | MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count, |
| 7567 | PretenureFlag pretenure) { |
| 7568 | ASSERT(deopt_entry_count > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7569 | return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7570 | pretenure); |
| 7571 | } |
| 7572 | |
| 7573 | |
| 7574 | MaybeObject* DeoptimizationOutputData::Allocate(int number_of_deopt_points, |
| 7575 | PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7576 | if (number_of_deopt_points == 0) return HEAP->empty_fixed_array(); |
| 7577 | return HEAP->AllocateFixedArray(LengthOfFixedArray(number_of_deopt_points), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7578 | pretenure); |
| 7579 | } |
| 7580 | |
| 7581 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7582 | #ifdef DEBUG |
| 7583 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 7584 | if (IsEmpty()) return other->IsEmpty(); |
| 7585 | if (other->IsEmpty()) return false; |
| 7586 | if (length() != other->length()) return false; |
| 7587 | for (int i = 0; i < length(); ++i) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 7588 | if (get(i) != other->get(i)) return false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7589 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 7590 | return true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7591 | } |
| 7592 | #endif |
| 7593 | |
| 7594 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7595 | bool String::LooksValid() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7596 | if (!Isolate::Current()->heap()->Contains(this)) return false; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 7597 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7601 | String::FlatContent String::GetFlatContent() { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7602 | int length = this->length(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7603 | StringShape shape(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7604 | String* string = this; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 7605 | int offset = 0; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7606 | if (shape.representation_tag() == kConsStringTag) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7607 | ConsString* cons = ConsString::cast(string); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7608 | if (cons->second()->length() != 0) { |
| 7609 | return FlatContent(); |
| 7610 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 7611 | string = cons->first(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7612 | shape = StringShape(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7613 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 7614 | if (shape.representation_tag() == kSlicedStringTag) { |
| 7615 | SlicedString* slice = SlicedString::cast(string); |
| 7616 | offset = slice->offset(); |
| 7617 | string = slice->parent(); |
| 7618 | shape = StringShape(string); |
| 7619 | ASSERT(shape.representation_tag() != kConsStringTag && |
| 7620 | shape.representation_tag() != kSlicedStringTag); |
| 7621 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 7622 | if (shape.encoding_tag() == kOneByteStringTag) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 7623 | const uint8_t* start; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7624 | if (shape.representation_tag() == kSeqStringTag) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 7625 | start = SeqOneByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7626 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7627 | start = ExternalAsciiString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7628 | } |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 7629 | return FlatContent(Vector<const uint8_t>(start + offset, length)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7630 | } else { |
| 7631 | ASSERT(shape.encoding_tag() == kTwoByteStringTag); |
| 7632 | const uc16* start; |
| 7633 | if (shape.representation_tag() == kSeqStringTag) { |
| 7634 | start = SeqTwoByteString::cast(string)->GetChars(); |
| 7635 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 7636 | start = ExternalTwoByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7637 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 7638 | return FlatContent(Vector<const uc16>(start + offset, length)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7639 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7640 | } |
| 7641 | |
| 7642 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7643 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 7644 | RobustnessFlag robust_flag, |
| 7645 | int offset, |
| 7646 | int length, |
| 7647 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7648 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7649 | return SmartArrayPointer<char>(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7650 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7651 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7652 | |
| 7653 | // Negative length means the to the end of the string. |
| 7654 | if (length < 0) length = kMaxInt - offset; |
| 7655 | |
| 7656 | // Compute the size of the UTF-8 string. Start at the specified offset. |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7657 | Access<ConsStringIteratorOp> op( |
| 7658 | heap->isolate()->objects_string_iterator()); |
| 7659 | StringCharacterStream stream(this, op.value(), offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7660 | int character_position = offset; |
| 7661 | int utf8_bytes = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7662 | int last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7663 | while (stream.HasMore() && character_position++ < offset + length) { |
| 7664 | uint16_t character = stream.GetNext(); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7665 | utf8_bytes += unibrow::Utf8::Length(character, last); |
| 7666 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7667 | } |
| 7668 | |
| 7669 | if (length_return) { |
| 7670 | *length_return = utf8_bytes; |
| 7671 | } |
| 7672 | |
| 7673 | char* result = NewArray<char>(utf8_bytes + 1); |
| 7674 | |
| 7675 | // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset. |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7676 | stream.Reset(this, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7677 | character_position = offset; |
| 7678 | int utf8_byte_position = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7679 | last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7680 | while (stream.HasMore() && character_position++ < offset + length) { |
| 7681 | uint16_t character = stream.GetNext(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 7682 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 7683 | character = ' '; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7684 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 7685 | utf8_byte_position += |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7686 | unibrow::Utf8::Encode(result + utf8_byte_position, character, last); |
| 7687 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7688 | } |
| 7689 | result[utf8_byte_position] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7690 | return SmartArrayPointer<char>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7691 | } |
| 7692 | |
| 7693 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7694 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 7695 | RobustnessFlag robust_flag, |
| 7696 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7697 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 7698 | } |
| 7699 | |
| 7700 | |
| 7701 | const uc16* String::GetTwoByteData() { |
| 7702 | return GetTwoByteData(0); |
| 7703 | } |
| 7704 | |
| 7705 | |
| 7706 | const uc16* String::GetTwoByteData(unsigned start) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 7707 | ASSERT(!IsOneByteRepresentationUnderneath()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7708 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7709 | case kSeqStringTag: |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7710 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7711 | case kExternalStringTag: |
| 7712 | return ExternalTwoByteString::cast(this)-> |
| 7713 | ExternalTwoByteStringGetData(start); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 7714 | case kSlicedStringTag: { |
| 7715 | SlicedString* slice = SlicedString::cast(this); |
| 7716 | return slice->parent()->GetTwoByteData(start + slice->offset()); |
| 7717 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7718 | case kConsStringTag: |
| 7719 | UNREACHABLE(); |
| 7720 | return NULL; |
| 7721 | } |
| 7722 | UNREACHABLE(); |
| 7723 | return NULL; |
| 7724 | } |
| 7725 | |
| 7726 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7727 | SmartArrayPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7728 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7729 | return SmartArrayPointer<uc16>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7730 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7731 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7732 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7733 | Access<ConsStringIteratorOp> op( |
| 7734 | heap->isolate()->objects_string_iterator()); |
| 7735 | StringCharacterStream stream(this, op.value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7736 | |
| 7737 | uc16* result = NewArray<uc16>(length() + 1); |
| 7738 | |
| 7739 | int i = 0; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 7740 | while (stream.HasMore()) { |
| 7741 | uint16_t character = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7742 | result[i++] = character; |
| 7743 | } |
| 7744 | result[i] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7745 | return SmartArrayPointer<uc16>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 7749 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7750 | return reinterpret_cast<uc16*>( |
| 7751 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 7752 | } |
| 7753 | |
| 7754 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7755 | void Relocatable::PostGarbageCollectionProcessing() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7756 | Isolate* isolate = Isolate::Current(); |
| 7757 | Relocatable* current = isolate->relocatable_top(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7758 | while (current != NULL) { |
| 7759 | current->PostGarbageCollection(); |
| 7760 | current = current->prev_; |
| 7761 | } |
| 7762 | } |
| 7763 | |
| 7764 | |
| 7765 | // Reserve space for statics needing saving and restoring. |
| 7766 | int Relocatable::ArchiveSpacePerThread() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7767 | return sizeof(Isolate::Current()->relocatable_top()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7768 | } |
| 7769 | |
| 7770 | |
| 7771 | // Archive statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7772 | char* Relocatable::ArchiveState(Isolate* isolate, char* to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7773 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 7774 | isolate->set_relocatable_top(NULL); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7775 | return to + ArchiveSpacePerThread(); |
| 7776 | } |
| 7777 | |
| 7778 | |
| 7779 | // Restore statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7780 | char* Relocatable::RestoreState(Isolate* isolate, char* from) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7781 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7782 | return from + ArchiveSpacePerThread(); |
| 7783 | } |
| 7784 | |
| 7785 | |
| 7786 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 7787 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 7788 | Iterate(v, top); |
| 7789 | return thread_storage + ArchiveSpacePerThread(); |
| 7790 | } |
| 7791 | |
| 7792 | |
| 7793 | void Relocatable::Iterate(ObjectVisitor* v) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7794 | Isolate* isolate = Isolate::Current(); |
| 7795 | Iterate(v, isolate->relocatable_top()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | |
| 7799 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 7800 | Relocatable* current = top; |
| 7801 | while (current != NULL) { |
| 7802 | current->IterateInstance(v); |
| 7803 | current = current->prev_; |
| 7804 | } |
| 7805 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7806 | |
| 7807 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7808 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 7809 | : Relocatable(isolate), |
| 7810 | str_(str.location()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7811 | length_(str->length()) { |
| 7812 | PostGarbageCollection(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7816 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 7817 | : Relocatable(isolate), |
| 7818 | str_(0), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7819 | is_ascii_(true), |
| 7820 | length_(input.length()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7821 | start_(input.start()) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7822 | |
| 7823 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 7824 | void FlatStringReader::PostGarbageCollection() { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7825 | if (str_ == NULL) return; |
| 7826 | Handle<String> str(str_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7827 | ASSERT(str->IsFlat()); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7828 | String::FlatContent content = str->GetFlatContent(); |
| 7829 | ASSERT(content.IsFlat()); |
| 7830 | is_ascii_ = content.IsAscii(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7831 | if (is_ascii_) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 7832 | start_ = content.ToOneByteVector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7833 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7834 | start_ = content.ToUC16Vector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 7835 | } |
| 7836 | } |
| 7837 | |
| 7838 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7839 | String* ConsStringIteratorOp::Operate(String* string, |
| 7840 | unsigned* offset_out, |
| 7841 | int32_t* type_out, |
| 7842 | unsigned* length_out) { |
| 7843 | ASSERT(string->IsConsString()); |
| 7844 | ConsString* cons_string = ConsString::cast(string); |
| 7845 | // Set up search data. |
| 7846 | root_ = cons_string; |
| 7847 | consumed_ = *offset_out; |
| 7848 | // Now search. |
| 7849 | return Search(offset_out, type_out, length_out); |
| 7850 | } |
| 7851 | |
| 7852 | |
| 7853 | String* ConsStringIteratorOp::Search(unsigned* offset_out, |
| 7854 | int32_t* type_out, |
| 7855 | unsigned* length_out) { |
| 7856 | ConsString* cons_string = root_; |
| 7857 | // Reset the stack, pushing the root string. |
| 7858 | depth_ = 1; |
| 7859 | maximum_depth_ = 1; |
| 7860 | frames_[0] = cons_string; |
| 7861 | const unsigned consumed = consumed_; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7862 | unsigned offset = 0; |
| 7863 | while (true) { |
| 7864 | // Loop until the string is found which contains the target offset. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7865 | String* string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7866 | unsigned length = string->length(); |
| 7867 | int32_t type; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7868 | if (consumed < offset + length) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7869 | // Target offset is in the left branch. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7870 | // Keep going if we're still in a ConString. |
| 7871 | type = string->map()->instance_type(); |
| 7872 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7873 | cons_string = ConsString::cast(string); |
| 7874 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7875 | continue; |
| 7876 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7877 | // Tell the stack we're done decending. |
| 7878 | AdjustMaximumDepth(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7879 | } else { |
| 7880 | // Descend right. |
| 7881 | // Update progress through the string. |
| 7882 | offset += length; |
| 7883 | // Keep going if we're still in a ConString. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7884 | string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7885 | type = string->map()->instance_type(); |
| 7886 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7887 | cons_string = ConsString::cast(string); |
| 7888 | PushRight(cons_string); |
| 7889 | // TODO(dcarney) Add back root optimization. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7890 | continue; |
| 7891 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7892 | // Need this to be updated for the current string. |
| 7893 | length = string->length(); |
| 7894 | // Account for the possibility of an empty right leaf. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7895 | // This happens only if we have asked for an offset outside the string. |
| 7896 | if (length == 0) { |
| 7897 | // Reset depth so future operations will return null immediately. |
| 7898 | Reset(); |
| 7899 | return NULL; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7900 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7901 | // Tell the stack we're done decending. |
| 7902 | AdjustMaximumDepth(); |
| 7903 | // Pop stack so next iteration is in correct place. |
| 7904 | Pop(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7905 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7906 | ASSERT(length != 0); |
| 7907 | // Adjust return values and exit. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7908 | consumed_ = offset + length; |
| 7909 | *offset_out = consumed - offset; |
| 7910 | *type_out = type; |
| 7911 | *length_out = length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7912 | return string; |
| 7913 | } |
| 7914 | UNREACHABLE(); |
| 7915 | return NULL; |
| 7916 | } |
| 7917 | |
| 7918 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7919 | String* ConsStringIteratorOp::NextLeaf(bool* blew_stack, |
| 7920 | int32_t* type_out, |
| 7921 | unsigned* length_out) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7922 | while (true) { |
| 7923 | // Tree traversal complete. |
| 7924 | if (depth_ == 0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7925 | *blew_stack = false; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7926 | return NULL; |
| 7927 | } |
| 7928 | // We've lost track of higher nodes. |
| 7929 | if (maximum_depth_ - depth_ == kStackSize) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7930 | *blew_stack = true; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7931 | return NULL; |
| 7932 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7933 | // Go right. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7934 | ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)]; |
| 7935 | String* string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7936 | int32_t type = string->map()->instance_type(); |
| 7937 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7938 | // Pop stack so next iteration is in correct place. |
| 7939 | Pop(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 7940 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7941 | // Could be a flattened ConsString. |
| 7942 | if (length == 0) continue; |
| 7943 | *length_out = length; |
| 7944 | *type_out = type; |
| 7945 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7946 | return string; |
| 7947 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7948 | cons_string = ConsString::cast(string); |
| 7949 | // TODO(dcarney) Add back root optimization. |
| 7950 | PushRight(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7951 | // Need to traverse all the way left. |
| 7952 | while (true) { |
| 7953 | // Continue left. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7954 | string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7955 | type = string->map()->instance_type(); |
| 7956 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 7957 | AdjustMaximumDepth(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 7958 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7959 | ASSERT(length != 0); |
| 7960 | *length_out = length; |
| 7961 | *type_out = type; |
| 7962 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7963 | return string; |
| 7964 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 7965 | cons_string = ConsString::cast(string); |
| 7966 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 7967 | } |
| 7968 | } |
| 7969 | UNREACHABLE(); |
| 7970 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7974 | uint16_t ConsString::ConsStringGet(int index) { |
| 7975 | ASSERT(index >= 0 && index < this->length()); |
| 7976 | |
| 7977 | // Check for a flattened cons string |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 7978 | if (second()->length() == 0) { |
| 7979 | String* left = first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7980 | return left->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7981 | } |
| 7982 | |
| 7983 | String* string = String::cast(this); |
| 7984 | |
| 7985 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7986 | if (StringShape(string).IsCons()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7987 | ConsString* cons_string = ConsString::cast(string); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 7988 | String* left = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7989 | if (left->length() > index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7990 | string = left; |
| 7991 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7992 | index -= left->length(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 7993 | string = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7994 | } |
| 7995 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7996 | return string->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7997 | } |
| 7998 | } |
| 7999 | |
| 8000 | UNREACHABLE(); |
| 8001 | return 0; |
| 8002 | } |
| 8003 | |
| 8004 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8005 | uint16_t SlicedString::SlicedStringGet(int index) { |
| 8006 | return parent()->Get(offset() + index); |
| 8007 | } |
| 8008 | |
| 8009 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8010 | template <typename sinkchar> |
| 8011 | void String::WriteToFlat(String* src, |
| 8012 | sinkchar* sink, |
| 8013 | int f, |
| 8014 | int t) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8015 | String* source = src; |
| 8016 | int from = f; |
| 8017 | int to = t; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8018 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8019 | ASSERT(0 <= from && from <= to && to <= source->length()); |
| 8020 | switch (StringShape(source).full_representation_tag()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8021 | case kOneByteStringTag | kExternalStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8022 | CopyChars(sink, |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8023 | ExternalAsciiString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8024 | to - from); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8025 | return; |
| 8026 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8027 | case kTwoByteStringTag | kExternalStringTag: { |
| 8028 | const uc16* data = |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8029 | ExternalTwoByteString::cast(source)->GetChars(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8030 | CopyChars(sink, |
| 8031 | data + from, |
| 8032 | to - from); |
| 8033 | return; |
| 8034 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8035 | case kOneByteStringTag | kSeqStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8036 | CopyChars(sink, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8037 | SeqOneByteString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8038 | to - from); |
| 8039 | return; |
| 8040 | } |
| 8041 | case kTwoByteStringTag | kSeqStringTag: { |
| 8042 | CopyChars(sink, |
| 8043 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 8044 | to - from); |
| 8045 | return; |
| 8046 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8047 | case kOneByteStringTag | kConsStringTag: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8048 | case kTwoByteStringTag | kConsStringTag: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8049 | ConsString* cons_string = ConsString::cast(source); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8050 | String* first = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8051 | int boundary = first->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8052 | if (to - boundary >= boundary - from) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8053 | // Right hand side is longer. Recurse over left. |
| 8054 | if (from < boundary) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8055 | WriteToFlat(first, sink, from, boundary); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8056 | sink += boundary - from; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8057 | from = 0; |
| 8058 | } else { |
| 8059 | from -= boundary; |
| 8060 | } |
| 8061 | to -= boundary; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8062 | source = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8063 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8064 | // Left hand side is longer. Recurse over right. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8065 | if (to > boundary) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8066 | String* second = cons_string->second(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8067 | // When repeatedly appending to a string, we get a cons string that |
| 8068 | // is unbalanced to the left, a list, essentially. We inline the |
| 8069 | // common case of sequential ascii right child. |
| 8070 | if (to - boundary == 1) { |
| 8071 | sink[boundary - from] = static_cast<sinkchar>(second->Get(0)); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8072 | } else if (second->IsSeqOneByteString()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8073 | CopyChars(sink + boundary - from, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8074 | SeqOneByteString::cast(second)->GetChars(), |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8075 | to - boundary); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8076 | } else { |
| 8077 | WriteToFlat(second, |
| 8078 | sink + boundary - from, |
| 8079 | 0, |
| 8080 | to - boundary); |
| 8081 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8082 | to = boundary; |
| 8083 | } |
| 8084 | source = first; |
| 8085 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8086 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8087 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8088 | case kOneByteStringTag | kSlicedStringTag: |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8089 | case kTwoByteStringTag | kSlicedStringTag: { |
| 8090 | SlicedString* slice = SlicedString::cast(source); |
| 8091 | unsigned offset = slice->offset(); |
| 8092 | WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 8093 | return; |
| 8094 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8095 | } |
| 8096 | } |
| 8097 | } |
| 8098 | |
| 8099 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8100 | // Compares the contents of two strings by reading and comparing |
| 8101 | // int-sized blocks of characters. |
| 8102 | template <typename Char> |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8103 | static inline bool CompareRawStringContents(const Char* const a, |
| 8104 | const Char* const b, |
| 8105 | int length) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8106 | int i = 0; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8107 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8108 | // If this architecture isn't comfortable reading unaligned ints |
| 8109 | // then we have to check that the strings are aligned before |
| 8110 | // comparing them blockwise. |
| 8111 | const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8112 | uint32_t pa_addr = reinterpret_cast<uint32_t>(a); |
| 8113 | uint32_t pb_addr = reinterpret_cast<uint32_t>(b); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 8114 | if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8115 | #endif |
| 8116 | const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT |
| 8117 | int endpoint = length - kStepSize; |
| 8118 | // Compare blocks until we reach near the end of the string. |
| 8119 | for (; i <= endpoint; i += kStepSize) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8120 | uint32_t wa = *reinterpret_cast<const uint32_t*>(a + i); |
| 8121 | uint32_t wb = *reinterpret_cast<const uint32_t*>(b + i); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8122 | if (wa != wb) { |
| 8123 | return false; |
| 8124 | } |
| 8125 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8126 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8127 | } |
| 8128 | #endif |
| 8129 | // Compare the remaining characters that didn't fit into a block. |
| 8130 | for (; i < length; i++) { |
| 8131 | if (a[i] != b[i]) { |
| 8132 | return false; |
| 8133 | } |
| 8134 | } |
| 8135 | return true; |
| 8136 | } |
| 8137 | |
| 8138 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8139 | template<typename Chars1, typename Chars2> |
| 8140 | class RawStringComparator : public AllStatic { |
| 8141 | public: |
| 8142 | static inline bool compare(const Chars1* a, const Chars2* b, int len) { |
| 8143 | ASSERT(sizeof(Chars1) != sizeof(Chars2)); |
| 8144 | for (int i = 0; i < len; i++) { |
| 8145 | if (a[i] != b[i]) { |
| 8146 | return false; |
| 8147 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8148 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8149 | return true; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8150 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8151 | }; |
| 8152 | |
| 8153 | |
| 8154 | template<> |
| 8155 | class RawStringComparator<uint16_t, uint16_t> { |
| 8156 | public: |
| 8157 | static inline bool compare(const uint16_t* a, const uint16_t* b, int len) { |
| 8158 | return CompareRawStringContents(a, b, len); |
| 8159 | } |
| 8160 | }; |
| 8161 | |
| 8162 | |
| 8163 | template<> |
| 8164 | class RawStringComparator<uint8_t, uint8_t> { |
| 8165 | public: |
| 8166 | static inline bool compare(const uint8_t* a, const uint8_t* b, int len) { |
| 8167 | return CompareRawStringContents(a, b, len); |
| 8168 | } |
| 8169 | }; |
| 8170 | |
| 8171 | |
| 8172 | class StringComparator { |
| 8173 | class State { |
| 8174 | public: |
| 8175 | explicit inline State(ConsStringIteratorOp* op) |
| 8176 | : op_(op), is_one_byte_(true), length_(0), buffer8_(NULL) {} |
| 8177 | |
| 8178 | inline void Init(String* string, unsigned len) { |
| 8179 | op_->Reset(); |
| 8180 | int32_t type = string->map()->instance_type(); |
| 8181 | String::Visit(string, 0, *this, *op_, type, len); |
| 8182 | } |
| 8183 | |
| 8184 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 8185 | is_one_byte_ = true; |
| 8186 | buffer8_ = chars; |
| 8187 | length_ = length; |
| 8188 | } |
| 8189 | |
| 8190 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 8191 | is_one_byte_ = false; |
| 8192 | buffer16_ = chars; |
| 8193 | length_ = length; |
| 8194 | } |
| 8195 | |
| 8196 | void Advance(unsigned consumed) { |
| 8197 | ASSERT(consumed <= length_); |
| 8198 | // Still in buffer. |
| 8199 | if (length_ != consumed) { |
| 8200 | if (is_one_byte_) { |
| 8201 | buffer8_ += consumed; |
| 8202 | } else { |
| 8203 | buffer16_ += consumed; |
| 8204 | } |
| 8205 | length_ -= consumed; |
| 8206 | return; |
| 8207 | } |
| 8208 | // Advance state. |
| 8209 | ASSERT(op_->HasMore()); |
| 8210 | int32_t type = 0; |
| 8211 | unsigned length = 0; |
| 8212 | String* next = op_->ContinueOperation(&type, &length); |
| 8213 | ASSERT(next != NULL); |
| 8214 | ConsStringNullOp null_op; |
| 8215 | String::Visit(next, 0, *this, null_op, type, length); |
| 8216 | } |
| 8217 | |
| 8218 | ConsStringIteratorOp* const op_; |
| 8219 | bool is_one_byte_; |
| 8220 | unsigned length_; |
| 8221 | union { |
| 8222 | const uint8_t* buffer8_; |
| 8223 | const uint16_t* buffer16_; |
| 8224 | }; |
| 8225 | DISALLOW_IMPLICIT_CONSTRUCTORS(State); |
| 8226 | }; |
| 8227 | |
| 8228 | public: |
| 8229 | inline StringComparator(ConsStringIteratorOp* op_1, |
| 8230 | ConsStringIteratorOp* op_2) |
| 8231 | : state_1_(op_1), |
| 8232 | state_2_(op_2) { |
| 8233 | } |
| 8234 | |
| 8235 | template<typename Chars1, typename Chars2> |
| 8236 | static inline bool Equals(State* state_1, State* state_2, unsigned to_check) { |
| 8237 | const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); |
| 8238 | const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); |
| 8239 | return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check); |
| 8240 | } |
| 8241 | |
| 8242 | bool Equals(unsigned length, String* string_1, String* string_2) { |
| 8243 | ASSERT(length != 0); |
| 8244 | state_1_.Init(string_1, length); |
| 8245 | state_2_.Init(string_2, length); |
| 8246 | while (true) { |
| 8247 | unsigned to_check = Min(state_1_.length_, state_2_.length_); |
| 8248 | ASSERT(to_check > 0 && to_check <= length); |
| 8249 | bool is_equal; |
| 8250 | if (state_1_.is_one_byte_) { |
| 8251 | if (state_2_.is_one_byte_) { |
| 8252 | is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8253 | } else { |
| 8254 | is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8255 | } |
| 8256 | } else { |
| 8257 | if (state_2_.is_one_byte_) { |
| 8258 | is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8259 | } else { |
| 8260 | is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8261 | } |
| 8262 | } |
| 8263 | // Looping done. |
| 8264 | if (!is_equal) return false; |
| 8265 | length -= to_check; |
| 8266 | // Exit condition. Strings are equal. |
| 8267 | if (length == 0) return true; |
| 8268 | state_1_.Advance(to_check); |
| 8269 | state_2_.Advance(to_check); |
| 8270 | } |
| 8271 | } |
| 8272 | |
| 8273 | private: |
| 8274 | State state_1_; |
| 8275 | State state_2_; |
| 8276 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringComparator); |
| 8277 | }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8278 | |
| 8279 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8280 | bool String::SlowEquals(String* other) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8281 | // Fast check: negative check with lengths. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8282 | int len = length(); |
| 8283 | if (len != other->length()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8284 | if (len == 0) return true; |
| 8285 | |
| 8286 | // Fast check: if hash code is computed for both strings |
| 8287 | // a fast negative check can be performed. |
| 8288 | if (HasHashCode() && other->HasHashCode()) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8289 | #ifdef DEBUG |
| 8290 | if (FLAG_enable_slow_asserts) { |
| 8291 | if (Hash() != other->Hash()) { |
| 8292 | bool found_difference = false; |
| 8293 | for (int i = 0; i < len; i++) { |
| 8294 | if (Get(i) != other->Get(i)) { |
| 8295 | found_difference = true; |
| 8296 | break; |
| 8297 | } |
| 8298 | } |
| 8299 | ASSERT(found_difference); |
| 8300 | } |
| 8301 | } |
| 8302 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8303 | if (Hash() != other->Hash()) return false; |
| 8304 | } |
| 8305 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 8306 | // We know the strings are both non-empty. Compare the first chars |
| 8307 | // before we try to flatten the strings. |
| 8308 | if (this->Get(0) != other->Get(0)) return false; |
| 8309 | |
| 8310 | String* lhs = this->TryFlattenGetString(); |
| 8311 | String* rhs = other->TryFlattenGetString(); |
| 8312 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8313 | // TODO(dcarney): Compare all types of flat strings with a Visitor. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 8314 | if (StringShape(lhs).IsSequentialAscii() && |
| 8315 | StringShape(rhs).IsSequentialAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8316 | const uint8_t* str1 = SeqOneByteString::cast(lhs)->GetChars(); |
| 8317 | const uint8_t* str2 = SeqOneByteString::cast(rhs)->GetChars(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8318 | return CompareRawStringContents(str1, str2, len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8321 | Isolate* isolate = GetIsolate(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8322 | StringComparator comparator(isolate->objects_string_compare_iterator_a(), |
| 8323 | isolate->objects_string_compare_iterator_b()); |
| 8324 | |
| 8325 | return comparator.Equals(static_cast<unsigned>(len), lhs, rhs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
| 8328 | |
| 8329 | bool String::MarkAsUndetectable() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 8330 | if (StringShape(this).IsInternalized()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8331 | |
| 8332 | Map* map = this->map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8333 | Heap* heap = GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8334 | if (map == heap->string_map()) { |
| 8335 | this->set_map(heap->undetectable_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8336 | return true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8337 | } else if (map == heap->ascii_string_map()) { |
| 8338 | this->set_map(heap->undetectable_ascii_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8339 | return true; |
| 8340 | } |
| 8341 | // Rest cannot be marked as undetectable |
| 8342 | return false; |
| 8343 | } |
| 8344 | |
| 8345 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8346 | bool String::IsUtf8EqualTo(Vector<const char> str) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8347 | int slen = length(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8348 | // Can't check exact length equality, but we can check bounds. |
| 8349 | int str_len = str.length(); |
| 8350 | if (str_len < slen || |
| 8351 | str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize)) { |
| 8352 | return false; |
| 8353 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8354 | int i; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8355 | unsigned remaining_in_str = static_cast<unsigned>(str_len); |
| 8356 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start()); |
| 8357 | for (i = 0; i < slen && remaining_in_str > 0; i++) { |
| 8358 | unsigned cursor = 0; |
| 8359 | uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor); |
| 8360 | ASSERT(cursor > 0 && cursor <= remaining_in_str); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8361 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 8362 | if (i > slen - 1) return false; |
| 8363 | if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false; |
| 8364 | if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false; |
| 8365 | } else { |
| 8366 | if (Get(i) != r) return false; |
| 8367 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8368 | utf8_data += cursor; |
| 8369 | remaining_in_str -= cursor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8370 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8371 | return i == slen && remaining_in_str == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8372 | } |
| 8373 | |
| 8374 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8375 | bool String::IsOneByteEqualTo(Vector<const uint8_t> str) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 8376 | int slen = length(); |
| 8377 | if (str.length() != slen) return false; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8378 | FlatContent content = GetFlatContent(); |
| 8379 | if (content.IsAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8380 | return CompareChars(content.ToOneByteVector().start(), |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8381 | str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 8382 | } |
| 8383 | for (int i = 0; i < slen; i++) { |
| 8384 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 8385 | } |
| 8386 | return true; |
| 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 8391 | int slen = length(); |
| 8392 | if (str.length() != slen) return false; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8393 | FlatContent content = GetFlatContent(); |
| 8394 | if (content.IsTwoByte()) { |
| 8395 | return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 8396 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 8397 | for (int i = 0; i < slen; i++) { |
| 8398 | if (Get(i) != str[i]) return false; |
| 8399 | } |
| 8400 | return true; |
| 8401 | } |
| 8402 | |
| 8403 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8404 | class IteratingStringHasher: public StringHasher { |
| 8405 | public: |
| 8406 | static inline uint32_t Hash(String* string, uint32_t seed) { |
| 8407 | const unsigned len = static_cast<unsigned>(string->length()); |
| 8408 | IteratingStringHasher hasher(len, seed); |
| 8409 | if (hasher.has_trivial_hash()) { |
| 8410 | return hasher.GetHashField(); |
| 8411 | } |
| 8412 | int32_t type = string->map()->instance_type(); |
| 8413 | ConsStringNullOp null_op; |
| 8414 | String::Visit(string, 0, hasher, null_op, type, len); |
| 8415 | // Flat strings terminate immediately. |
| 8416 | if (hasher.consumed_ == len) { |
| 8417 | ASSERT(!string->IsConsString()); |
| 8418 | return hasher.GetHashField(); |
| 8419 | } |
| 8420 | ASSERT(string->IsConsString()); |
| 8421 | // This is a ConsString, iterate across it. |
| 8422 | ConsStringIteratorOp op; |
| 8423 | unsigned offset = 0; |
| 8424 | unsigned leaf_length = len; |
| 8425 | string = op.Operate(string, &offset, &type, &leaf_length); |
| 8426 | while (true) { |
| 8427 | ASSERT(hasher.consumed_ < len); |
| 8428 | String::Visit(string, 0, hasher, null_op, type, leaf_length); |
| 8429 | if (hasher.consumed_ == len) break; |
| 8430 | string = op.ContinueOperation(&type, &leaf_length); |
| 8431 | // This should be taken care of by the length check. |
| 8432 | ASSERT(string != NULL); |
| 8433 | } |
| 8434 | return hasher.GetHashField(); |
| 8435 | } |
| 8436 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 8437 | AddCharacters(chars, static_cast<int>(length)); |
| 8438 | consumed_ += length; |
| 8439 | } |
| 8440 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 8441 | AddCharacters(chars, static_cast<int>(length)); |
| 8442 | consumed_ += length; |
| 8443 | } |
| 8444 | |
| 8445 | private: |
| 8446 | inline IteratingStringHasher(int len, uint32_t seed) |
| 8447 | : StringHasher(len, seed), |
| 8448 | consumed_(0) {} |
| 8449 | unsigned consumed_; |
| 8450 | DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher); |
| 8451 | }; |
| 8452 | |
| 8453 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8454 | uint32_t String::ComputeAndSetHash() { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 8455 | // Should only be called if hash code has not yet been computed. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 8456 | ASSERT(!HasHashCode()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8457 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8458 | // Store the hash code in the object. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8459 | uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8460 | set_hash_field(field); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8461 | |
| 8462 | // Check the hash code is there. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 8463 | ASSERT(HasHashCode()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 8464 | uint32_t result = field >> kHashShift; |
| 8465 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 8466 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8467 | } |
| 8468 | |
| 8469 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8470 | bool String::ComputeArrayIndex(uint32_t* index) { |
| 8471 | int length = this->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8472 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8473 | ConsStringIteratorOp op; |
| 8474 | StringCharacterStream stream(this, &op); |
| 8475 | uint16_t ch = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8476 | |
| 8477 | // If the string begins with a '0' character, it must only consist |
| 8478 | // of it to be a legal array index. |
| 8479 | if (ch == '0') { |
| 8480 | *index = 0; |
| 8481 | return length == 1; |
| 8482 | } |
| 8483 | |
| 8484 | // Convert string to uint32 array index; character by character. |
| 8485 | int d = ch - '0'; |
| 8486 | if (d < 0 || d > 9) return false; |
| 8487 | uint32_t result = d; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8488 | while (stream.HasMore()) { |
| 8489 | d = stream.GetNext() - '0'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8490 | if (d < 0 || d > 9) return false; |
| 8491 | // Check that the new result is below the 32 bit limit. |
| 8492 | if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 8493 | result = (result * 10) + d; |
| 8494 | } |
| 8495 | |
| 8496 | *index = result; |
| 8497 | return true; |
| 8498 | } |
| 8499 | |
| 8500 | |
| 8501 | bool String::SlowAsArrayIndex(uint32_t* index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8502 | if (length() <= kMaxCachedArrayIndexLength) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8503 | Hash(); // force computation of hash code |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8504 | uint32_t field = hash_field(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8505 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8506 | // Isolate the array index form the full hash field. |
| 8507 | *index = (kArrayIndexHashMask & field) >> kHashShift; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8508 | return true; |
| 8509 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8510 | return ComputeArrayIndex(index); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8511 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8512 | } |
| 8513 | |
| 8514 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8515 | Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) { |
| 8516 | int new_size, old_size; |
| 8517 | int old_length = string->length(); |
| 8518 | if (old_length <= new_length) return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8519 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8520 | if (string->IsSeqOneByteString()) { |
| 8521 | old_size = SeqOneByteString::SizeFor(old_length); |
| 8522 | new_size = SeqOneByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8523 | } else { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8524 | ASSERT(string->IsSeqTwoByteString()); |
| 8525 | old_size = SeqTwoByteString::SizeFor(old_length); |
| 8526 | new_size = SeqTwoByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8527 | } |
| 8528 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8529 | int delta = old_size - new_size; |
| 8530 | string->set_length(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8531 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8532 | Address start_of_string = string->address(); |
| 8533 | ASSERT_OBJECT_ALIGNED(start_of_string); |
| 8534 | ASSERT_OBJECT_ALIGNED(start_of_string + new_size); |
| 8535 | |
| 8536 | Heap* heap = string->GetHeap(); |
| 8537 | NewSpace* newspace = heap->new_space(); |
| 8538 | if (newspace->Contains(start_of_string) && |
| 8539 | newspace->top() == start_of_string + old_size) { |
| 8540 | // Last allocated object in new space. Simply lower allocation top. |
| 8541 | *(newspace->allocation_top_address()) = start_of_string + new_size; |
| 8542 | } else { |
| 8543 | // Sizes are pointer size aligned, so that we can use filler objects |
| 8544 | // that are a multiple of pointer size. |
| 8545 | heap->CreateFillerObjectAt(start_of_string + new_size, delta); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8546 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 8547 | if (Marking::IsBlack(Marking::MarkBitFrom(start_of_string))) { |
| 8548 | MemoryChunk::IncrementLiveBytesFromMutator(start_of_string, -delta); |
| 8549 | } |
| 8550 | |
| 8551 | |
| 8552 | if (new_length == 0) return heap->isolate()->factory()->empty_string(); |
| 8553 | return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8554 | } |
| 8555 | |
| 8556 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8557 | AllocationSiteInfo* AllocationSiteInfo::FindForJSObject(JSObject* object) { |
| 8558 | // Currently, AllocationSiteInfo objects are only allocated immediately |
| 8559 | // after JSArrays in NewSpace, and detecting whether a JSArray has one |
| 8560 | // involves carefully checking the object immediately after the JSArray |
| 8561 | // (if there is one) to see if it's an AllocationSiteInfo. |
| 8562 | if (FLAG_track_allocation_sites && object->GetHeap()->InNewSpace(object)) { |
| 8563 | Address ptr_end = (reinterpret_cast<Address>(object) - kHeapObjectTag) + |
| 8564 | object->Size(); |
| 8565 | if ((ptr_end + AllocationSiteInfo::kSize) <= |
| 8566 | object->GetHeap()->NewSpaceTop()) { |
| 8567 | // There is room in newspace for allocation info. Do we have some? |
| 8568 | Map** possible_allocation_site_info_map = |
| 8569 | reinterpret_cast<Map**>(ptr_end); |
| 8570 | if (*possible_allocation_site_info_map == |
| 8571 | object->GetHeap()->allocation_site_info_map()) { |
| 8572 | AllocationSiteInfo* info = AllocationSiteInfo::cast( |
| 8573 | reinterpret_cast<Object*>(ptr_end + 1)); |
| 8574 | return info; |
| 8575 | } |
| 8576 | } |
| 8577 | } |
| 8578 | return NULL; |
| 8579 | } |
| 8580 | |
| 8581 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 8582 | bool AllocationSiteInfo::GetElementsKindPayload(ElementsKind* kind) { |
| 8583 | ASSERT(kind != NULL); |
| 8584 | if (payload()->IsJSGlobalPropertyCell()) { |
| 8585 | JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(payload()); |
| 8586 | Object* cell_contents = cell->value(); |
| 8587 | if (cell_contents->IsSmi()) { |
| 8588 | *kind = static_cast<ElementsKind>( |
| 8589 | Smi::cast(cell_contents)->value()); |
| 8590 | return true; |
| 8591 | } |
| 8592 | } |
| 8593 | return false; |
| 8594 | } |
| 8595 | |
| 8596 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 8597 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 8598 | // For array indexes mix the length into the hash as an array index could |
| 8599 | // be zero. |
| 8600 | ASSERT(length > 0); |
| 8601 | ASSERT(length <= String::kMaxArrayIndexSize); |
| 8602 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 8603 | (1 << String::kArrayIndexValueBits)); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 8604 | |
| 8605 | value <<= String::kHashShift; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 8606 | value |= length << String::kArrayIndexHashLengthShift; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 8607 | |
| 8608 | ASSERT((value & String::kIsNotArrayIndexMask) == 0); |
| 8609 | ASSERT((length > String::kMaxCachedArrayIndexLength) || |
| 8610 | (value & String::kContainsCachedArrayIndexMask) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 8611 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8612 | } |
| 8613 | |
| 8614 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8615 | uint32_t StringHasher::GetHashField() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8616 | if (length_ <= String::kMaxHashCalcLength) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8617 | if (is_array_index_) { |
| 8618 | return MakeArrayIndexHash(array_index_, length_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8619 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8620 | return (GetHashCore(raw_running_hash_) << String::kHashShift) | |
| 8621 | String::kIsNotArrayIndexMask; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8622 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 8623 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8624 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8626 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8627 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8628 | uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars, |
| 8629 | uint32_t seed, |
| 8630 | int* utf16_length_out) { |
| 8631 | int vector_length = chars.length(); |
| 8632 | // Handle some edge cases |
| 8633 | if (vector_length <= 1) { |
| 8634 | ASSERT(vector_length == 0 || |
| 8635 | static_cast<uint8_t>(chars.start()[0]) <= |
| 8636 | unibrow::Utf8::kMaxOneByteChar); |
| 8637 | *utf16_length_out = vector_length; |
| 8638 | return HashSequentialString(chars.start(), vector_length, seed); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8639 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8640 | // Start with a fake length which won't affect computation. |
| 8641 | // It will be updated later. |
| 8642 | StringHasher hasher(String::kMaxArrayIndexSize, seed); |
| 8643 | unsigned remaining = static_cast<unsigned>(vector_length); |
| 8644 | const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start()); |
| 8645 | int utf16_length = 0; |
| 8646 | bool is_index = true; |
| 8647 | ASSERT(hasher.is_array_index_); |
| 8648 | while (remaining > 0) { |
| 8649 | unsigned consumed = 0; |
| 8650 | uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed); |
| 8651 | ASSERT(consumed > 0 && consumed <= remaining); |
| 8652 | stream += consumed; |
| 8653 | remaining -= consumed; |
| 8654 | bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 8655 | utf16_length += is_two_characters ? 2 : 1; |
| 8656 | // No need to keep hashing. But we do need to calculate utf16_length. |
| 8657 | if (utf16_length > String::kMaxHashCalcLength) continue; |
| 8658 | if (is_two_characters) { |
| 8659 | uint16_t c1 = unibrow::Utf16::LeadSurrogate(c); |
| 8660 | uint16_t c2 = unibrow::Utf16::TrailSurrogate(c); |
| 8661 | hasher.AddCharacter(c1); |
| 8662 | hasher.AddCharacter(c2); |
| 8663 | if (is_index) is_index = hasher.UpdateIndex(c1); |
| 8664 | if (is_index) is_index = hasher.UpdateIndex(c2); |
| 8665 | } else { |
| 8666 | hasher.AddCharacter(c); |
| 8667 | if (is_index) is_index = hasher.UpdateIndex(c); |
| 8668 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8669 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8670 | *utf16_length_out = static_cast<int>(utf16_length); |
| 8671 | // Must set length here so that hash computation is correct. |
| 8672 | hasher.length_ = utf16_length; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8673 | return hasher.GetHashField(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8674 | } |
| 8675 | |
| 8676 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8677 | MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8678 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8679 | if (start == 0 && end == length()) return this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8680 | MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8681 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8682 | } |
| 8683 | |
| 8684 | |
| 8685 | void String::PrintOn(FILE* file) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8686 | int length = this->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8687 | for (int i = 0; i < length; i++) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 8688 | PrintF(file, "%c", Get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8689 | } |
| 8690 | } |
| 8691 | |
| 8692 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8693 | static void TrimEnumCache(Heap* heap, Map* map, DescriptorArray* descriptors) { |
| 8694 | int live_enum = map->EnumLength(); |
| 8695 | if (live_enum == Map::kInvalidEnumCache) { |
| 8696 | live_enum = map->NumberOfDescribedProperties(OWN_DESCRIPTORS, DONT_ENUM); |
| 8697 | } |
| 8698 | if (live_enum == 0) return descriptors->ClearEnumCache(); |
| 8699 | |
| 8700 | FixedArray* enum_cache = descriptors->GetEnumCache(); |
| 8701 | |
| 8702 | int to_trim = enum_cache->length() - live_enum; |
| 8703 | if (to_trim <= 0) return; |
| 8704 | RightTrimFixedArray<FROM_GC>(heap, descriptors->GetEnumCache(), to_trim); |
| 8705 | |
| 8706 | if (!descriptors->HasEnumIndicesCache()) return; |
| 8707 | FixedArray* enum_indices_cache = descriptors->GetEnumIndicesCache(); |
| 8708 | RightTrimFixedArray<FROM_GC>(heap, enum_indices_cache, to_trim); |
| 8709 | } |
| 8710 | |
| 8711 | |
| 8712 | static void TrimDescriptorArray(Heap* heap, |
| 8713 | Map* map, |
| 8714 | DescriptorArray* descriptors, |
| 8715 | int number_of_own_descriptors) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 8716 | int number_of_descriptors = descriptors->number_of_descriptors_storage(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8717 | int to_trim = number_of_descriptors - number_of_own_descriptors; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 8718 | if (to_trim == 0) return; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8719 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 8720 | RightTrimFixedArray<FROM_GC>( |
| 8721 | heap, descriptors, to_trim * DescriptorArray::kDescriptorSize); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8722 | descriptors->SetNumberOfDescriptors(number_of_own_descriptors); |
| 8723 | |
| 8724 | if (descriptors->HasEnumCache()) TrimEnumCache(heap, map, descriptors); |
| 8725 | descriptors->Sort(); |
| 8726 | } |
| 8727 | |
| 8728 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8729 | // Clear a possible back pointer in case the transition leads to a dead map. |
| 8730 | // Return true in case a back pointer has been cleared and false otherwise. |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8731 | static bool ClearBackPointer(Heap* heap, Map* target) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8732 | if (Marking::MarkBitFrom(target).Get()) return false; |
| 8733 | target->SetBackPointer(heap->undefined_value(), SKIP_WRITE_BARRIER); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8734 | return true; |
| 8735 | } |
| 8736 | |
| 8737 | |
| 8738 | // TODO(mstarzinger): This method should be moved into MarkCompactCollector, |
| 8739 | // because it cannot be called from outside the GC and we already have methods |
| 8740 | // depending on the transitions layout in the GC anyways. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 8741 | void Map::ClearNonLiveTransitions(Heap* heap) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8742 | // If there are no transitions to be cleared, return. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8743 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 8744 | // properly cleared. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8745 | if (!HasTransitionArray()) return; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8746 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8747 | TransitionArray* t = transitions(); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 8748 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8749 | |
| 8750 | int transition_index = 0; |
| 8751 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8752 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8753 | bool descriptors_owner_died = false; |
| 8754 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8755 | // Compact all live descriptors to the left. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8756 | for (int i = 0; i < t->number_of_transitions(); ++i) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8757 | Map* target = t->GetTarget(i); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8758 | if (ClearBackPointer(heap, target)) { |
| 8759 | if (target->instance_descriptors() == descriptors) { |
| 8760 | descriptors_owner_died = true; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8761 | } |
| 8762 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8763 | if (i != transition_index) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 8764 | Name* key = t->GetKey(i); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8765 | t->SetKey(transition_index, key); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8766 | Object** key_slot = t->GetKeySlot(transition_index); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8767 | collector->RecordSlot(key_slot, key_slot, key); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 8768 | // Target slots do not need to be recorded since maps are not compacted. |
| 8769 | t->SetTarget(transition_index, t->GetTarget(i)); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8770 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8771 | transition_index++; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8772 | } |
| 8773 | } |
| 8774 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8775 | if (t->HasElementsTransition() && |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8776 | ClearBackPointer(heap, t->elements_transition())) { |
| 8777 | if (t->elements_transition()->instance_descriptors() == descriptors) { |
| 8778 | descriptors_owner_died = true; |
| 8779 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8780 | t->ClearElementsTransition(); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8781 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8782 | // If there are no transitions to be cleared, return. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8783 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 8784 | // properly cleared. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8785 | if (transition_index == t->number_of_transitions()) return; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8786 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8787 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8788 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 8789 | |
| 8790 | if (descriptors_owner_died) { |
| 8791 | if (number_of_own_descriptors > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8792 | TrimDescriptorArray(heap, this, descriptors, number_of_own_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8793 | ASSERT(descriptors->number_of_descriptors() == number_of_own_descriptors); |
| 8794 | } else { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 8795 | ASSERT(descriptors == GetHeap()->empty_descriptor_array()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8796 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8797 | } |
| 8798 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 8799 | int trim = t->number_of_transitions() - transition_index; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 8800 | if (trim > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 8801 | RightTrimFixedArray<FROM_GC>(heap, t, t->IsSimpleTransition() |
| 8802 | ? trim : trim * TransitionArray::kTransitionSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8803 | } |
| 8804 | } |
| 8805 | |
| 8806 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8807 | int Map::Hash() { |
| 8808 | // For performance reasons we only hash the 3 most variable fields of a map: |
| 8809 | // constructor, prototype and bit_field2. |
| 8810 | |
| 8811 | // Shift away the tag. |
| 8812 | int hash = (static_cast<uint32_t>( |
| 8813 | reinterpret_cast<uintptr_t>(constructor())) >> 2); |
| 8814 | |
| 8815 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 8816 | // when the two pointers are close (which is fairly common). |
| 8817 | // To avoid this we shift the prototype 4 bits relatively to the constructor. |
| 8818 | hash ^= (static_cast<uint32_t>( |
| 8819 | reinterpret_cast<uintptr_t>(prototype())) << 2); |
| 8820 | |
| 8821 | return hash ^ (hash >> 16) ^ bit_field2(); |
| 8822 | } |
| 8823 | |
| 8824 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8825 | static bool CheckEquivalent(Map* first, Map* second) { |
| 8826 | return |
| 8827 | first->constructor() == second->constructor() && |
| 8828 | first->prototype() == second->prototype() && |
| 8829 | first->instance_type() == second->instance_type() && |
| 8830 | first->bit_field() == second->bit_field() && |
| 8831 | first->bit_field2() == second->bit_field2() && |
| 8832 | first->is_observed() == second->is_observed() && |
| 8833 | first->function_with_prototype() == second->function_with_prototype(); |
| 8834 | } |
| 8835 | |
| 8836 | |
| 8837 | bool Map::EquivalentToForTransition(Map* other) { |
| 8838 | return CheckEquivalent(this, other); |
| 8839 | } |
| 8840 | |
| 8841 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8842 | bool Map::EquivalentToForNormalization(Map* other, |
| 8843 | PropertyNormalizationMode mode) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8844 | int properties = mode == CLEAR_INOBJECT_PROPERTIES |
| 8845 | ? 0 : other->inobject_properties(); |
| 8846 | return CheckEquivalent(this, other) && inobject_properties() == properties; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8847 | } |
| 8848 | |
| 8849 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 8850 | void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) { |
| 8851 | // Iterate over all fields in the body but take care in dealing with |
| 8852 | // the code entry. |
| 8853 | IteratePointers(v, kPropertiesOffset, kCodeEntryOffset); |
| 8854 | v->VisitCodeEntry(this->address() + kCodeEntryOffset); |
| 8855 | IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size); |
| 8856 | } |
| 8857 | |
| 8858 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8859 | void JSFunction::MarkForLazyRecompilation() { |
| 8860 | ASSERT(is_compiled() && !IsOptimized()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8861 | ASSERT(shared()->allows_lazy_compilation() || |
| 8862 | code()->optimizable()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 8863 | set_code_no_write_barrier( |
| 8864 | GetIsolate()->builtins()->builtin(Builtins::kLazyRecompile)); |
| 8865 | // No write barrier required, since the builtin is part of the root set. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8866 | } |
| 8867 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 8868 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 8869 | void JSFunction::MarkForParallelRecompilation() { |
| 8870 | ASSERT(is_compiled() && !IsOptimized()); |
| 8871 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 8872 | ASSERT(FLAG_parallel_recompilation); |
| 8873 | if (FLAG_trace_parallel_recompilation) { |
| 8874 | PrintF(" ** Marking "); |
| 8875 | PrintName(); |
| 8876 | PrintF(" for parallel recompilation.\n"); |
| 8877 | } |
| 8878 | set_code_no_write_barrier( |
| 8879 | GetIsolate()->builtins()->builtin(Builtins::kParallelRecompile)); |
| 8880 | // No write barrier required, since the builtin is part of the root set. |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 8881 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8882 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 8883 | |
| 8884 | void JSFunction::MarkForInstallingRecompiledCode() { |
| 8885 | ASSERT(is_compiled() && !IsOptimized()); |
| 8886 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
| 8887 | ASSERT(FLAG_parallel_recompilation); |
| 8888 | set_code_no_write_barrier( |
| 8889 | GetIsolate()->builtins()->builtin(Builtins::kInstallRecompiledCode)); |
| 8890 | // No write barrier required, since the builtin is part of the root set. |
| 8891 | } |
| 8892 | |
| 8893 | |
| 8894 | void JSFunction::MarkInRecompileQueue() { |
| 8895 | ASSERT(is_compiled() && !IsOptimized()); |
| 8896 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
| 8897 | ASSERT(FLAG_parallel_recompilation); |
| 8898 | if (FLAG_trace_parallel_recompilation) { |
| 8899 | PrintF(" ** Queueing "); |
| 8900 | PrintName(); |
| 8901 | PrintF(" for parallel recompilation.\n"); |
| 8902 | } |
| 8903 | set_code_no_write_barrier( |
| 8904 | GetIsolate()->builtins()->builtin(Builtins::kInRecompileQueue)); |
| 8905 | // No write barrier required, since the builtin is part of the root set. |
| 8906 | } |
| 8907 | |
| 8908 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8909 | static bool CompileLazyHelper(CompilationInfo* info, |
| 8910 | ClearExceptionFlag flag) { |
| 8911 | // Compile the source information to a code object. |
| 8912 | ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled()); |
| 8913 | ASSERT(!info->isolate()->has_pending_exception()); |
| 8914 | bool result = Compiler::CompileLazy(info); |
| 8915 | ASSERT(result != Isolate::Current()->has_pending_exception()); |
| 8916 | if (!result && flag == CLEAR_EXCEPTION) { |
| 8917 | info->isolate()->clear_pending_exception(); |
| 8918 | } |
| 8919 | return result; |
| 8920 | } |
| 8921 | |
| 8922 | |
| 8923 | bool SharedFunctionInfo::CompileLazy(Handle<SharedFunctionInfo> shared, |
| 8924 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8925 | ASSERT(shared->allows_lazy_compilation_without_context()); |
| 8926 | CompilationInfoWithZone info(shared); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8927 | return CompileLazyHelper(&info, flag); |
| 8928 | } |
| 8929 | |
| 8930 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8931 | void SharedFunctionInfo::AddToOptimizedCodeMap( |
| 8932 | Handle<SharedFunctionInfo> shared, |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8933 | Handle<Context> native_context, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8934 | Handle<Code> code, |
| 8935 | Handle<FixedArray> literals) { |
| 8936 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8937 | ASSERT(native_context->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8938 | STATIC_ASSERT(kEntryLength == 3); |
| 8939 | Object* value = shared->optimized_code_map(); |
| 8940 | Handle<FixedArray> new_code_map; |
| 8941 | if (value->IsSmi()) { |
| 8942 | // No optimized code map. |
| 8943 | ASSERT_EQ(0, Smi::cast(value)->value()); |
| 8944 | // Crate 3 entries per context {context, code, literals}. |
| 8945 | new_code_map = FACTORY->NewFixedArray(kEntryLength); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8946 | new_code_map->set(0, *native_context); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8947 | new_code_map->set(1, *code); |
| 8948 | new_code_map->set(2, *literals); |
| 8949 | } else { |
| 8950 | // Copy old map and append one new entry. |
| 8951 | Handle<FixedArray> old_code_map(FixedArray::cast(value)); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8952 | ASSERT_EQ(-1, shared->SearchOptimizedCodeMap(*native_context)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8953 | int old_length = old_code_map->length(); |
| 8954 | int new_length = old_length + kEntryLength; |
| 8955 | new_code_map = FACTORY->NewFixedArray(new_length); |
| 8956 | old_code_map->CopyTo(0, *new_code_map, 0, old_length); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8957 | new_code_map->set(old_length, *native_context); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8958 | new_code_map->set(old_length + 1, *code); |
| 8959 | new_code_map->set(old_length + 2, *literals); |
| 8960 | } |
| 8961 | #ifdef DEBUG |
| 8962 | for (int i = 0; i < new_code_map->length(); i += kEntryLength) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8963 | ASSERT(new_code_map->get(i)->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8964 | ASSERT(new_code_map->get(i + 1)->IsCode()); |
| 8965 | ASSERT(Code::cast(new_code_map->get(i + 1))->kind() == |
| 8966 | Code::OPTIMIZED_FUNCTION); |
| 8967 | ASSERT(new_code_map->get(i + 2)->IsFixedArray()); |
| 8968 | } |
| 8969 | #endif |
| 8970 | shared->set_optimized_code_map(*new_code_map); |
| 8971 | } |
| 8972 | |
| 8973 | |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 8974 | void SharedFunctionInfo::InstallFromOptimizedCodeMap(JSFunction* function, |
| 8975 | int index) { |
| 8976 | ASSERT(index > 0); |
| 8977 | ASSERT(optimized_code_map()->IsFixedArray()); |
| 8978 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 8979 | if (!bound()) { |
| 8980 | FixedArray* cached_literals = FixedArray::cast(code_map->get(index + 1)); |
| 8981 | ASSERT(cached_literals != NULL); |
| 8982 | function->set_literals(cached_literals); |
| 8983 | } |
| 8984 | Code* code = Code::cast(code_map->get(index)); |
| 8985 | ASSERT(code != NULL); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8986 | ASSERT(function->context()->native_context() == code_map->get(index - 1)); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 8987 | function->ReplaceCode(code); |
| 8988 | } |
| 8989 | |
| 8990 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8991 | bool JSFunction::CompileLazy(Handle<JSFunction> function, |
| 8992 | ClearExceptionFlag flag) { |
| 8993 | bool result = true; |
| 8994 | if (function->shared()->is_compiled()) { |
| 8995 | function->ReplaceCode(function->shared()->code()); |
| 8996 | function->shared()->set_code_age(0); |
| 8997 | } else { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 8998 | ASSERT(function->shared()->allows_lazy_compilation()); |
| 8999 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9000 | result = CompileLazyHelper(&info, flag); |
| 9001 | ASSERT(!result || function->is_compiled()); |
| 9002 | } |
| 9003 | return result; |
| 9004 | } |
| 9005 | |
| 9006 | |
| 9007 | bool JSFunction::CompileOptimized(Handle<JSFunction> function, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 9008 | BailoutId osr_ast_id, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9009 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9010 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9011 | info.SetOptimizing(osr_ast_id); |
| 9012 | return CompileLazyHelper(&info, flag); |
| 9013 | } |
| 9014 | |
| 9015 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9016 | bool JSFunction::EnsureCompiled(Handle<JSFunction> function, |
| 9017 | ClearExceptionFlag flag) { |
| 9018 | return function->is_compiled() || CompileLazy(function, flag); |
| 9019 | } |
| 9020 | |
| 9021 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9022 | bool JSFunction::IsInlineable() { |
| 9023 | if (IsBuiltin()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9024 | SharedFunctionInfo* shared_info = shared(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9025 | // Check that the function has a script associated with it. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9026 | if (!shared_info->script()->IsScript()) return false; |
| 9027 | if (shared_info->optimization_disabled()) return false; |
| 9028 | Code* code = shared_info->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9029 | if (code->kind() == Code::OPTIMIZED_FUNCTION) return true; |
| 9030 | // If we never ran this (unlikely) then lets try to optimize it. |
| 9031 | if (code->kind() != Code::FUNCTION) return true; |
| 9032 | return code->optimizable(); |
| 9033 | } |
| 9034 | |
| 9035 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9036 | MaybeObject* JSObject::OptimizeAsPrototype() { |
| 9037 | if (IsGlobalObject()) return this; |
| 9038 | |
| 9039 | // Make sure prototypes are fast objects and their maps have the bit set |
| 9040 | // so they remain fast. |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 9041 | if (!HasFastProperties()) { |
| 9042 | MaybeObject* new_proto = TransformToFastProperties(0); |
| 9043 | if (new_proto->IsFailure()) return new_proto; |
| 9044 | ASSERT(new_proto == this); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9045 | } |
| 9046 | return this; |
| 9047 | } |
| 9048 | |
| 9049 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9050 | static MUST_USE_RESULT MaybeObject* CacheInitialJSArrayMaps( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9051 | Context* native_context, Map* initial_map) { |
| 9052 | // Replace all of the cached initial array maps in the native context with |
| 9053 | // the appropriate transitioned elements kind maps. |
| 9054 | Heap* heap = native_context->GetHeap(); |
| 9055 | MaybeObject* maybe_maps = |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9056 | heap->AllocateFixedArrayWithHoles(kElementsKindCount, TENURED); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9057 | FixedArray* maps; |
| 9058 | if (!maybe_maps->To(&maps)) return maybe_maps; |
| 9059 | |
| 9060 | Map* current_map = initial_map; |
| 9061 | ElementsKind kind = current_map->elements_kind(); |
| 9062 | ASSERT(kind == GetInitialFastElementsKind()); |
| 9063 | maps->set(kind, current_map); |
| 9064 | for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1; |
| 9065 | i < kFastElementsKindCount; ++i) { |
| 9066 | Map* new_map; |
| 9067 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9068 | if (current_map->HasElementsTransition()) { |
| 9069 | new_map = current_map->elements_transition_map(); |
| 9070 | ASSERT(new_map->elements_kind() == next_kind); |
| 9071 | } else { |
| 9072 | MaybeObject* maybe_new_map = |
| 9073 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 9074 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 9075 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9076 | maps->set(next_kind, new_map); |
| 9077 | current_map = new_map; |
| 9078 | } |
| 9079 | native_context->set_js_array_maps(maps); |
| 9080 | return initial_map; |
| 9081 | } |
| 9082 | |
| 9083 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9084 | Handle<Object> CacheInitialJSArrayMaps(Handle<Context> native_context, |
| 9085 | Handle<Map> initial_map) { |
| 9086 | CALL_HEAP_FUNCTION(native_context->GetIsolate(), |
| 9087 | CacheInitialJSArrayMaps(*native_context, *initial_map), |
| 9088 | Object); |
| 9089 | } |
| 9090 | |
| 9091 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 9092 | MaybeObject* JSFunction::SetInstancePrototype(Object* value) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9093 | ASSERT(value->IsJSReceiver()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9094 | Heap* heap = GetHeap(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9095 | |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 9096 | // First some logic for the map of the prototype to make sure it is in fast |
| 9097 | // mode. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9098 | if (value->IsJSObject()) { |
| 9099 | MaybeObject* ok = JSObject::cast(value)->OptimizeAsPrototype(); |
| 9100 | if (ok->IsFailure()) return ok; |
| 9101 | } |
| 9102 | |
| 9103 | // Now some logic for the maps of the objects that are created by using this |
| 9104 | // function as a constructor. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9105 | if (has_initial_map()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9106 | // If the function has allocated the initial map replace it with a |
| 9107 | // copy containing the new prototype. Also complete any in-object |
| 9108 | // slack tracking that is in progress at this point because it is |
| 9109 | // still tracking the old copy. |
| 9110 | if (shared()->IsInobjectSlackTrackingInProgress()) { |
| 9111 | shared()->CompleteInobjectSlackTracking(); |
| 9112 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 9113 | Map* new_map; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9114 | MaybeObject* maybe_object = initial_map()->Copy(); |
| 9115 | if (!maybe_object->To(&new_map)) return maybe_object; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 9116 | new_map->set_prototype(value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9117 | |
| 9118 | // If the function is used as the global Array function, cache the |
| 9119 | // initial map (and transitioned versions) in the native context. |
| 9120 | Context* native_context = context()->native_context(); |
| 9121 | Object* array_function = native_context->get(Context::ARRAY_FUNCTION_INDEX); |
| 9122 | if (array_function->IsJSFunction() && |
| 9123 | this == JSFunction::cast(array_function)) { |
| 9124 | MaybeObject* ok = CacheInitialJSArrayMaps(native_context, new_map); |
| 9125 | if (ok->IsFailure()) return ok; |
| 9126 | } |
| 9127 | |
| 9128 | set_initial_map(new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9129 | } else { |
| 9130 | // Put the value in the initial map field until an initial map is |
| 9131 | // needed. At that point, a new initial map is created and the |
| 9132 | // prototype is put into the initial map where it belongs. |
| 9133 | set_prototype_or_initial_map(value); |
| 9134 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9135 | heap->ClearInstanceofCache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9136 | return value; |
| 9137 | } |
| 9138 | |
| 9139 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9140 | MaybeObject* JSFunction::SetPrototype(Object* value) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9141 | ASSERT(should_have_prototype()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9142 | Object* construct_prototype = value; |
| 9143 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9144 | // If the value is not a JSReceiver, store the value in the map's |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9145 | // constructor field so it can be accessed. Also, set the prototype |
| 9146 | // used for constructing objects to the original object prototype. |
| 9147 | // See ECMA-262 13.2.2. |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9148 | if (!value->IsJSReceiver()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9149 | // Copy the map so this does not affect unrelated functions. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9150 | // Remove map transitions because they point to maps with a |
| 9151 | // different prototype. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 9152 | Map* new_map; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 9153 | MaybeObject* maybe_new_map = map()->Copy(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 9154 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 9155 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9156 | Heap* heap = new_map->GetHeap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9157 | set_map(new_map); |
| 9158 | new_map->set_constructor(value); |
| 9159 | new_map->set_non_instance_prototype(true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9160 | construct_prototype = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9161 | heap->isolate()->context()->native_context()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9162 | initial_object_prototype(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9163 | } else { |
| 9164 | map()->set_non_instance_prototype(false); |
| 9165 | } |
| 9166 | |
| 9167 | return SetInstancePrototype(construct_prototype); |
| 9168 | } |
| 9169 | |
| 9170 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9171 | void JSFunction::RemovePrototype() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9172 | Context* native_context = context()->native_context(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9173 | Map* no_prototype_map = shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9174 | ? native_context->function_without_prototype_map() |
| 9175 | : native_context->strict_mode_function_without_prototype_map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9176 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9177 | if (map() == no_prototype_map) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9178 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9179 | ASSERT(map() == (shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9180 | ? native_context->function_map() |
| 9181 | : native_context->strict_mode_function_map())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9182 | |
| 9183 | set_map(no_prototype_map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9184 | set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9185 | } |
| 9186 | |
| 9187 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9188 | void JSFunction::SetInstanceClassName(String* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9189 | shared()->set_instance_class_name(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9190 | } |
| 9191 | |
| 9192 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9193 | void JSFunction::PrintName(FILE* out) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9194 | SmartArrayPointer<char> name = shared()->DebugName()->ToCString(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9195 | PrintF(out, "%s", *name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9196 | } |
| 9197 | |
| 9198 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9199 | Context* JSFunction::NativeContextFromLiterals(FixedArray* literals) { |
| 9200 | return Context::cast(literals->get(JSFunction::kLiteralNativeContextIndex)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
| 9203 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9204 | MaybeObject* Oddball::Initialize(const char* to_string, |
| 9205 | Object* to_number, |
| 9206 | byte kind) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9207 | String* internalized_to_string; |
| 9208 | { MaybeObject* maybe_string = |
| 9209 | Isolate::Current()->heap()->InternalizeUtf8String( |
| 9210 | CStrVector(to_string)); |
| 9211 | if (!maybe_string->To(&internalized_to_string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9212 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9213 | set_to_string(internalized_to_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9214 | set_to_number(to_number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9215 | set_kind(kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9216 | return this; |
| 9217 | } |
| 9218 | |
| 9219 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 9220 | String* SharedFunctionInfo::DebugName() { |
| 9221 | Object* n = name(); |
| 9222 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 9223 | return String::cast(n); |
| 9224 | } |
| 9225 | |
| 9226 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9227 | bool SharedFunctionInfo::HasSourceCode() { |
| 9228 | return !script()->IsUndefined() && |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 9229 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9230 | } |
| 9231 | |
| 9232 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 9233 | Handle<Object> SharedFunctionInfo::GetSourceCode() { |
| 9234 | if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value(); |
| 9235 | Handle<String> source(String::cast(Script::cast(script())->source())); |
| 9236 | return SubString(source, start_position(), end_position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9237 | } |
| 9238 | |
| 9239 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9240 | int SharedFunctionInfo::SourceSize() { |
| 9241 | return end_position() - start_position(); |
| 9242 | } |
| 9243 | |
| 9244 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9245 | int SharedFunctionInfo::CalculateInstanceSize() { |
| 9246 | int instance_size = |
| 9247 | JSObject::kHeaderSize + |
| 9248 | expected_nof_properties() * kPointerSize; |
| 9249 | if (instance_size > JSObject::kMaxInstanceSize) { |
| 9250 | instance_size = JSObject::kMaxInstanceSize; |
| 9251 | } |
| 9252 | return instance_size; |
| 9253 | } |
| 9254 | |
| 9255 | |
| 9256 | int SharedFunctionInfo::CalculateInObjectProperties() { |
| 9257 | return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize; |
| 9258 | } |
| 9259 | |
| 9260 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9261 | bool SharedFunctionInfo::CanGenerateInlineConstructor(Object* prototype) { |
| 9262 | // Check the basic conditions for generating inline constructor code. |
| 9263 | if (!FLAG_inline_new |
| 9264 | || !has_only_simple_this_property_assignments() |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 9265 | || is_generator() |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9266 | || this_property_assignments_count() == 0) { |
| 9267 | return false; |
| 9268 | } |
| 9269 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 9270 | Isolate* isolate = GetIsolate(); |
| 9271 | Heap* heap = isolate->heap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9272 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 9273 | // Traverse the proposed prototype chain looking for properties of the |
| 9274 | // same names as are set by the inline constructor. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9275 | for (Object* obj = prototype; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9276 | obj != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 9277 | obj = obj->GetPrototype(isolate)) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 9278 | JSReceiver* receiver = JSReceiver::cast(obj); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9279 | for (int i = 0; i < this_property_assignments_count(); i++) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9280 | LookupResult result(heap->isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9281 | String* name = GetThisPropertyAssignmentName(i); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 9282 | receiver->LocalLookup(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 9283 | if (result.IsFound()) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 9284 | switch (result.type()) { |
| 9285 | case NORMAL: |
| 9286 | case FIELD: |
| 9287 | case CONSTANT_FUNCTION: |
| 9288 | break; |
| 9289 | case INTERCEPTOR: |
| 9290 | case CALLBACKS: |
| 9291 | case HANDLER: |
| 9292 | return false; |
| 9293 | case TRANSITION: |
| 9294 | case NONEXISTENT: |
| 9295 | UNREACHABLE(); |
| 9296 | break; |
| 9297 | } |
| 9298 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9299 | } |
| 9300 | } |
| 9301 | |
| 9302 | return true; |
| 9303 | } |
| 9304 | |
| 9305 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 9306 | void SharedFunctionInfo::ForbidInlineConstructor() { |
| 9307 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
| 9308 | kHasOnlySimpleThisPropertyAssignments, |
| 9309 | false)); |
| 9310 | } |
| 9311 | |
| 9312 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9313 | void SharedFunctionInfo::SetThisPropertyAssignmentsInfo( |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9314 | bool only_simple_this_property_assignments, |
| 9315 | FixedArray* assignments) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9316 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9317 | kHasOnlySimpleThisPropertyAssignments, |
| 9318 | only_simple_this_property_assignments)); |
| 9319 | set_this_property_assignments(assignments); |
| 9320 | set_this_property_assignments_count(assignments->length() / 3); |
| 9321 | } |
| 9322 | |
| 9323 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9324 | void SharedFunctionInfo::ClearThisPropertyAssignmentsInfo() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9325 | Heap* heap = GetHeap(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9326 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9327 | kHasOnlySimpleThisPropertyAssignments, |
| 9328 | false)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9329 | set_this_property_assignments(heap->undefined_value()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9330 | set_this_property_assignments_count(0); |
| 9331 | } |
| 9332 | |
| 9333 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9334 | String* SharedFunctionInfo::GetThisPropertyAssignmentName(int index) { |
| 9335 | Object* obj = this_property_assignments(); |
| 9336 | ASSERT(obj->IsFixedArray()); |
| 9337 | ASSERT(index < this_property_assignments_count()); |
| 9338 | obj = FixedArray::cast(obj)->get(index * 3); |
| 9339 | ASSERT(obj->IsString()); |
| 9340 | return String::cast(obj); |
| 9341 | } |
| 9342 | |
| 9343 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 9344 | bool SharedFunctionInfo::IsThisPropertyAssignmentArgument(int index) { |
| 9345 | Object* obj = this_property_assignments(); |
| 9346 | ASSERT(obj->IsFixedArray()); |
| 9347 | ASSERT(index < this_property_assignments_count()); |
| 9348 | obj = FixedArray::cast(obj)->get(index * 3 + 1); |
| 9349 | return Smi::cast(obj)->value() != -1; |
| 9350 | } |
| 9351 | |
| 9352 | |
| 9353 | int SharedFunctionInfo::GetThisPropertyAssignmentArgument(int index) { |
| 9354 | ASSERT(IsThisPropertyAssignmentArgument(index)); |
| 9355 | Object* obj = |
| 9356 | FixedArray::cast(this_property_assignments())->get(index * 3 + 1); |
| 9357 | return Smi::cast(obj)->value(); |
| 9358 | } |
| 9359 | |
| 9360 | |
| 9361 | Object* SharedFunctionInfo::GetThisPropertyAssignmentConstant(int index) { |
| 9362 | ASSERT(!IsThisPropertyAssignmentArgument(index)); |
| 9363 | Object* obj = |
| 9364 | FixedArray::cast(this_property_assignments())->get(index * 3 + 2); |
| 9365 | return obj; |
| 9366 | } |
| 9367 | |
| 9368 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9369 | // Support function for printing the source code to a StringStream |
| 9370 | // without any allocation in the heap. |
| 9371 | void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator, |
| 9372 | int max_length) { |
| 9373 | // For some native functions there is no source. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9374 | if (!HasSourceCode()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9375 | accumulator->Add("<No Source>"); |
| 9376 | return; |
| 9377 | } |
| 9378 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9379 | // Get the source for the script which this function came from. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9380 | // Don't use String::cast because we don't want more assertion errors while |
| 9381 | // we are already creating a stack dump. |
| 9382 | String* script_source = |
| 9383 | reinterpret_cast<String*>(Script::cast(script())->source()); |
| 9384 | |
| 9385 | if (!script_source->LooksValid()) { |
| 9386 | accumulator->Add("<Invalid Source>"); |
| 9387 | return; |
| 9388 | } |
| 9389 | |
| 9390 | if (!is_toplevel()) { |
| 9391 | accumulator->Add("function "); |
| 9392 | Object* name = this->name(); |
| 9393 | if (name->IsString() && String::cast(name)->length() > 0) { |
| 9394 | accumulator->PrintName(name); |
| 9395 | } |
| 9396 | } |
| 9397 | |
| 9398 | int len = end_position() - start_position(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9399 | if (len <= max_length || max_length < 0) { |
| 9400 | accumulator->Put(script_source, start_position(), end_position()); |
| 9401 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9402 | accumulator->Put(script_source, |
| 9403 | start_position(), |
| 9404 | start_position() + max_length); |
| 9405 | accumulator->Add("...\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9406 | } |
| 9407 | } |
| 9408 | |
| 9409 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9410 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 9411 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 9412 | ByteArray* code_relocation = code->relocation_info(); |
| 9413 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 9414 | int length = code_relocation->length(); |
| 9415 | if (length != recompiled_relocation->length()) return false; |
| 9416 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 9417 | recompiled_relocation->GetDataStartAddress(), |
| 9418 | length); |
| 9419 | return compare == 0; |
| 9420 | } |
| 9421 | |
| 9422 | |
| 9423 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
| 9424 | ASSERT(!has_deoptimization_support()); |
| 9425 | AssertNoAllocation no_allocation; |
| 9426 | Code* code = this->code(); |
| 9427 | if (IsCodeEquivalent(code, recompiled)) { |
| 9428 | // Copy the deoptimization data from the recompiled code. |
| 9429 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 9430 | code->set_has_deoptimization_support(true); |
| 9431 | } else { |
| 9432 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 9433 | // old code, we have to replace it. We should try to avoid this |
| 9434 | // altogether because it flushes valuable type feedback by |
| 9435 | // effectively resetting all IC state. |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 9436 | ReplaceCode(recompiled); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9437 | } |
| 9438 | ASSERT(has_deoptimization_support()); |
| 9439 | } |
| 9440 | |
| 9441 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9442 | void SharedFunctionInfo::DisableOptimization(const char* reason) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 9443 | // Disable optimization for the shared function info and mark the |
| 9444 | // code as non-optimizable. The marker on the shared function info |
| 9445 | // is there because we flush non-optimized code thereby loosing the |
| 9446 | // non-optimizable information for the code. When the code is |
| 9447 | // regenerated and set on the shared function info it is marked as |
| 9448 | // non-optimizable if optimization is disabled for the shared |
| 9449 | // function info. |
| 9450 | set_optimization_disabled(true); |
| 9451 | // Code should be the lazy compilation stub or else unoptimized. If the |
| 9452 | // latter, disable optimization for the code too. |
| 9453 | ASSERT(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN); |
| 9454 | if (code()->kind() == Code::FUNCTION) { |
| 9455 | code()->set_optimizable(false); |
| 9456 | } |
| 9457 | if (FLAG_trace_opt) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9458 | PrintF("[disabled optimization for %s, reason: %s]\n", |
| 9459 | *DebugName()->ToCString(), reason); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 9460 | } |
| 9461 | } |
| 9462 | |
| 9463 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 9464 | bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) { |
| 9465 | ASSERT(!id.IsNone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9466 | Code* unoptimized = code(); |
| 9467 | DeoptimizationOutputData* data = |
| 9468 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 9469 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 9470 | USE(ignore); |
| 9471 | return true; // Return true if there was no ASSERT. |
| 9472 | } |
| 9473 | |
| 9474 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9475 | void SharedFunctionInfo::StartInobjectSlackTracking(Map* map) { |
| 9476 | ASSERT(!IsInobjectSlackTrackingInProgress()); |
| 9477 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 9478 | if (!FLAG_clever_optimizations) return; |
| 9479 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9480 | // Only initiate the tracking the first time. |
| 9481 | if (live_objects_may_exist()) return; |
| 9482 | set_live_objects_may_exist(true); |
| 9483 | |
| 9484 | // No tracking during the snapshot construction phase. |
| 9485 | if (Serializer::enabled()) return; |
| 9486 | |
| 9487 | if (map->unused_property_fields() == 0) return; |
| 9488 | |
| 9489 | // Nonzero counter is a leftover from the previous attempt interrupted |
| 9490 | // by GC, keep it. |
| 9491 | if (construction_count() == 0) { |
| 9492 | set_construction_count(kGenerousAllocationCount); |
| 9493 | } |
| 9494 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9495 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9496 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9497 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9498 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
| 9501 | |
| 9502 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 9503 | void SharedFunctionInfo::DetachInitialMap() { |
| 9504 | Map* map = reinterpret_cast<Map*>(initial_map()); |
| 9505 | |
| 9506 | // Make the map remember to restore the link if it survives the GC. |
| 9507 | map->set_bit_field2( |
| 9508 | map->bit_field2() | (1 << Map::kAttachedToSharedFunctionInfo)); |
| 9509 | |
| 9510 | // Undo state changes made by StartInobjectTracking (except the |
| 9511 | // construction_count). This way if the initial map does not survive the GC |
| 9512 | // then StartInobjectTracking will be called again the next time the |
| 9513 | // constructor is called. The countdown will continue and (possibly after |
| 9514 | // several more GCs) CompleteInobjectSlackTracking will eventually be called. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9515 | Heap* heap = map->GetHeap(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 9516 | set_initial_map(heap->undefined_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9517 | Builtins* builtins = heap->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9518 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9519 | *RawField(this, kConstructStubOffset)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9520 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9521 | // It is safe to clear the flag: it will be set again if the map is live. |
| 9522 | set_live_objects_may_exist(false); |
| 9523 | } |
| 9524 | |
| 9525 | |
| 9526 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 9527 | void SharedFunctionInfo::AttachInitialMap(Map* map) { |
| 9528 | map->set_bit_field2( |
| 9529 | map->bit_field2() & ~(1 << Map::kAttachedToSharedFunctionInfo)); |
| 9530 | |
| 9531 | // Resume inobject slack tracking. |
| 9532 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9533 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9534 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9535 | *RawField(this, kConstructStubOffset)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9536 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9537 | // The map survived the gc, so there may be objects referencing it. |
| 9538 | set_live_objects_may_exist(true); |
| 9539 | } |
| 9540 | |
| 9541 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 9542 | void SharedFunctionInfo::ResetForNewContext(int new_ic_age) { |
| 9543 | code()->ClearInlineCaches(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 9544 | set_ic_age(new_ic_age); |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 9545 | if (code()->kind() == Code::FUNCTION) { |
| 9546 | code()->set_profiler_ticks(0); |
| 9547 | if (optimization_disabled() && |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 9548 | opt_count() >= FLAG_max_opt_count) { |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 9549 | // Re-enable optimizations if they were disabled due to opt_count limit. |
| 9550 | set_optimization_disabled(false); |
| 9551 | code()->set_optimizable(true); |
| 9552 | } |
| 9553 | set_opt_count(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9554 | set_deopt_count(0); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 9555 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 9556 | } |
| 9557 | |
| 9558 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9559 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 9560 | int slack = map->unused_property_fields(); |
| 9561 | if (*reinterpret_cast<int*>(data) > slack) { |
| 9562 | *reinterpret_cast<int*>(data) = slack; |
| 9563 | } |
| 9564 | } |
| 9565 | |
| 9566 | |
| 9567 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 9568 | int slack = *reinterpret_cast<int*>(data); |
| 9569 | map->set_inobject_properties(map->inobject_properties() - slack); |
| 9570 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 9571 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
| 9572 | |
| 9573 | // Visitor id might depend on the instance size, recalculate it. |
| 9574 | map->set_visitor_id(StaticVisitorBase::GetVisitorId(map)); |
| 9575 | } |
| 9576 | |
| 9577 | |
| 9578 | void SharedFunctionInfo::CompleteInobjectSlackTracking() { |
| 9579 | ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress()); |
| 9580 | Map* map = Map::cast(initial_map()); |
| 9581 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9582 | Heap* heap = map->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9583 | set_initial_map(heap->undefined_value()); |
| 9584 | Builtins* builtins = heap->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9585 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9586 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9587 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9588 | |
| 9589 | int slack = map->unused_property_fields(); |
| 9590 | map->TraverseTransitionTree(&GetMinInobjectSlack, &slack); |
| 9591 | if (slack != 0) { |
| 9592 | // Resize the initial map and all maps in its transition tree. |
| 9593 | map->TraverseTransitionTree(&ShrinkInstanceSize, &slack); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 9594 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9595 | // Give the correct expected_nof_properties to initial maps created later. |
| 9596 | ASSERT(expected_nof_properties() >= slack); |
| 9597 | set_expected_nof_properties(expected_nof_properties() - slack); |
| 9598 | } |
| 9599 | } |
| 9600 | |
| 9601 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9602 | int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context) { |
| 9603 | ASSERT(native_context->IsNativeContext()); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9604 | if (!FLAG_cache_optimized_code) return -1; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9605 | Object* value = optimized_code_map(); |
| 9606 | if (!value->IsSmi()) { |
| 9607 | FixedArray* optimized_code_map = FixedArray::cast(value); |
| 9608 | int length = optimized_code_map->length(); |
| 9609 | for (int i = 0; i < length; i += 3) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9610 | if (optimized_code_map->get(i) == native_context) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9611 | return i + 1; |
| 9612 | } |
| 9613 | } |
| 9614 | } |
| 9615 | return -1; |
| 9616 | } |
| 9617 | |
| 9618 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 9619 | #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 9620 | const char* const VisitorSynchronization::kTags[ |
| 9621 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 9622 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 9623 | }; |
| 9624 | #undef DECLARE_TAG |
| 9625 | |
| 9626 | |
| 9627 | #define DECLARE_TAG(ignore1, ignore2, name) name, |
| 9628 | const char* const VisitorSynchronization::kTagNames[ |
| 9629 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 9630 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 9631 | }; |
| 9632 | #undef DECLARE_TAG |
| 9633 | |
| 9634 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9635 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9636 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9637 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 9638 | Object* old_target = target; |
| 9639 | VisitPointer(&target); |
| 9640 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
| 9643 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9644 | void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 9645 | ASSERT(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 9646 | Object* stub = rinfo->code_age_stub(); |
| 9647 | if (stub) { |
| 9648 | VisitPointer(&stub); |
| 9649 | } |
| 9650 | } |
| 9651 | |
| 9652 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 9653 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 9654 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 9655 | Object* old_code = code; |
| 9656 | VisitPointer(&code); |
| 9657 | if (code != old_code) { |
| 9658 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 9659 | } |
| 9660 | } |
| 9661 | |
| 9662 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9663 | void ObjectVisitor::VisitGlobalPropertyCell(RelocInfo* rinfo) { |
| 9664 | ASSERT(rinfo->rmode() == RelocInfo::GLOBAL_PROPERTY_CELL); |
| 9665 | Object* cell = rinfo->target_cell(); |
| 9666 | Object* old_cell = cell; |
| 9667 | VisitPointer(&cell); |
| 9668 | if (cell != old_cell) { |
| 9669 | rinfo->set_target_cell(reinterpret_cast<JSGlobalPropertyCell*>(cell)); |
| 9670 | } |
| 9671 | } |
| 9672 | |
| 9673 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9674 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 9675 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 9676 | rinfo->IsPatchedReturnSequence()) || |
| 9677 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 9678 | rinfo->IsPatchedDebugBreakSlotSequence())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9679 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 9680 | Object* old_target = target; |
| 9681 | VisitPointer(&target); |
| 9682 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9683 | } |
| 9684 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 9685 | void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) { |
| 9686 | ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 9687 | VisitPointer(rinfo->target_object_address()); |
| 9688 | } |
| 9689 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9690 | void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) { |
| 9691 | Address* p = rinfo->target_reference_address(); |
| 9692 | VisitExternalReferences(p, p + 1); |
| 9693 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 9694 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9695 | byte Code::compare_nil_state() { |
| 9696 | ASSERT(is_compare_nil_ic_stub()); |
| 9697 | return CompareNilICStub::TypesFromExtraICState(extended_extra_ic_state()); |
| 9698 | } |
| 9699 | |
| 9700 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9701 | void Code::InvalidateRelocation() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9702 | set_relocation_info(GetHeap()->empty_byte_array()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9703 | } |
| 9704 | |
| 9705 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9706 | void Code::Relocate(intptr_t delta) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9707 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
| 9708 | it.rinfo()->apply(delta); |
| 9709 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9710 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9711 | } |
| 9712 | |
| 9713 | |
| 9714 | void Code::CopyFrom(const CodeDesc& desc) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9715 | ASSERT(Marking::Color(this) == Marking::WHITE_OBJECT); |
| 9716 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9717 | // copy code |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9718 | CopyBytes(instruction_start(), desc.buffer, |
| 9719 | static_cast<size_t>(desc.instr_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9720 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9721 | // copy reloc info |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 9722 | CopyBytes(relocation_start(), |
| 9723 | desc.buffer + desc.buffer_size - desc.reloc_size, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9724 | static_cast<size_t>(desc.reloc_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9725 | |
| 9726 | // unbox handles and relocate |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 9727 | intptr_t delta = instruction_start() - desc.buffer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9728 | int mode_mask = RelocInfo::kCodeTargetMask | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9729 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9730 | RelocInfo::ModeMask(RelocInfo::GLOBAL_PROPERTY_CELL) | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9731 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9732 | RelocInfo::kApplyMask; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9733 | // Needed to find target_object and runtime_entry on X64 |
| 9734 | Assembler* origin = desc.origin; |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 9735 | ALLOW_HANDLE_DEREF(GetIsolate(), "embedding raw addresses into code"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9736 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9737 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 9738 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 9739 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9740 | it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9741 | } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9742 | Handle<JSGlobalPropertyCell> cell = it.rinfo()->target_cell_handle(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9743 | it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9744 | } else if (RelocInfo::IsCodeTarget(mode)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9745 | // rewrite code handles in inline cache targets to direct |
| 9746 | // pointers to the first instruction in the code object |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 9747 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9748 | Code* code = Code::cast(*p); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9749 | it.rinfo()->set_target_address(code->instruction_start(), |
| 9750 | SKIP_WRITE_BARRIER); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9751 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
| 9752 | Address p = it.rinfo()->target_runtime_entry(origin); |
| 9753 | it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9754 | } else { |
| 9755 | it.rinfo()->apply(delta); |
| 9756 | } |
| 9757 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9758 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9759 | } |
| 9760 | |
| 9761 | |
| 9762 | // Locate the source position which is closest to the address in the code. This |
| 9763 | // is using the source position information embedded in the relocation info. |
| 9764 | // The position returned is relative to the beginning of the script where the |
| 9765 | // source for this function is found. |
| 9766 | int Code::SourcePosition(Address pc) { |
| 9767 | int distance = kMaxInt; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9768 | int position = RelocInfo::kNoPosition; // Initially no position found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9769 | // Run through all the relocation info to find the best matching source |
| 9770 | // position. All the code needs to be considered as the sequence of the |
| 9771 | // instructions in the code does not necessarily follow the same order as the |
| 9772 | // source. |
| 9773 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 9774 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9775 | // Only look at positions after the current pc. |
| 9776 | if (it.rinfo()->pc() < pc) { |
| 9777 | // Get position and distance. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9778 | |
| 9779 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 9780 | int pos = static_cast<int>(it.rinfo()->data()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9781 | // If this position is closer than the current candidate or if it has the |
| 9782 | // same distance as the current candidate and the position is higher then |
| 9783 | // this position is the new candidate. |
| 9784 | if ((dist < distance) || |
| 9785 | (dist == distance && pos > position)) { |
| 9786 | position = pos; |
| 9787 | distance = dist; |
| 9788 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9789 | } |
| 9790 | it.next(); |
| 9791 | } |
| 9792 | return position; |
| 9793 | } |
| 9794 | |
| 9795 | |
| 9796 | // Same as Code::SourcePosition above except it only looks for statement |
| 9797 | // positions. |
| 9798 | int Code::SourceStatementPosition(Address pc) { |
| 9799 | // First find the position as close as possible using all position |
| 9800 | // information. |
| 9801 | int position = SourcePosition(pc); |
| 9802 | // Now find the closest statement position before the position. |
| 9803 | int statement_position = 0; |
| 9804 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 9805 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9806 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9807 | int p = static_cast<int>(it.rinfo()->data()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9808 | if (statement_position < p && p <= position) { |
| 9809 | statement_position = p; |
| 9810 | } |
| 9811 | } |
| 9812 | it.next(); |
| 9813 | } |
| 9814 | return statement_position; |
| 9815 | } |
| 9816 | |
| 9817 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9818 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9819 | SafepointTable table(this); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9820 | return table.FindEntry(pc); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9821 | } |
| 9822 | |
| 9823 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9824 | Map* Code::FindFirstMap() { |
| 9825 | ASSERT(is_inline_cache_stub()); |
| 9826 | AssertNoAllocation no_allocation; |
| 9827 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 9828 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9829 | RelocInfo* info = it.rinfo(); |
| 9830 | Object* object = info->target_object(); |
| 9831 | if (object->IsMap()) return Map::cast(object); |
| 9832 | } |
| 9833 | return NULL; |
| 9834 | } |
| 9835 | |
| 9836 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9837 | void Code::ReplaceFirstMap(Map* replace_with) { |
| 9838 | ASSERT(is_inline_cache_stub()); |
| 9839 | AssertNoAllocation no_allocation; |
| 9840 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 9841 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9842 | RelocInfo* info = it.rinfo(); |
| 9843 | Object* object = info->target_object(); |
| 9844 | if (object->IsMap()) { |
| 9845 | info->set_target_object(replace_with); |
| 9846 | return; |
| 9847 | } |
| 9848 | } |
| 9849 | UNREACHABLE(); |
| 9850 | } |
| 9851 | |
| 9852 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9853 | void Code::FindAllMaps(MapHandleList* maps) { |
| 9854 | ASSERT(is_inline_cache_stub()); |
| 9855 | AssertNoAllocation no_allocation; |
| 9856 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 9857 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9858 | RelocInfo* info = it.rinfo(); |
| 9859 | Object* object = info->target_object(); |
| 9860 | if (object->IsMap()) maps->Add(Handle<Map>(Map::cast(object))); |
| 9861 | } |
| 9862 | } |
| 9863 | |
| 9864 | |
| 9865 | Code* Code::FindFirstCode() { |
| 9866 | ASSERT(is_inline_cache_stub()); |
| 9867 | AssertNoAllocation no_allocation; |
| 9868 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 9869 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9870 | RelocInfo* info = it.rinfo(); |
| 9871 | return Code::GetCodeFromTargetAddress(info->target_address()); |
| 9872 | } |
| 9873 | return NULL; |
| 9874 | } |
| 9875 | |
| 9876 | |
| 9877 | void Code::FindAllCode(CodeHandleList* code_list, int length) { |
| 9878 | ASSERT(is_inline_cache_stub()); |
| 9879 | AssertNoAllocation no_allocation; |
| 9880 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 9881 | int i = 0; |
| 9882 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9883 | if (i++ == length) return; |
| 9884 | RelocInfo* info = it.rinfo(); |
| 9885 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 9886 | ASSERT(code->kind() == Code::STUB); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9887 | code_list->Add(Handle<Code>(code)); |
| 9888 | } |
| 9889 | UNREACHABLE(); |
| 9890 | } |
| 9891 | |
| 9892 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 9893 | Name* Code::FindFirstName() { |
| 9894 | ASSERT(is_inline_cache_stub()); |
| 9895 | AssertNoAllocation no_allocation; |
| 9896 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 9897 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9898 | RelocInfo* info = it.rinfo(); |
| 9899 | Object* object = info->target_object(); |
| 9900 | if (object->IsName()) return Name::cast(object); |
| 9901 | } |
| 9902 | return NULL; |
| 9903 | } |
| 9904 | |
| 9905 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 9906 | void Code::ClearInlineCaches() { |
| 9907 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 9908 | RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) | |
| 9909 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID) | |
| 9910 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_CONTEXT); |
| 9911 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 9912 | RelocInfo* info = it.rinfo(); |
| 9913 | Code* target(Code::GetCodeFromTargetAddress(info->target_address())); |
| 9914 | if (target->is_inline_cache_stub()) { |
| 9915 | IC::Clear(info->pc()); |
| 9916 | } |
| 9917 | } |
| 9918 | } |
| 9919 | |
| 9920 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 9921 | void Code::ClearTypeFeedbackCells(Heap* heap) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 9922 | if (kind() != FUNCTION) return; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 9923 | Object* raw_info = type_feedback_info(); |
| 9924 | if (raw_info->IsTypeFeedbackInfo()) { |
| 9925 | TypeFeedbackCells* type_feedback_cells = |
| 9926 | TypeFeedbackInfo::cast(raw_info)->type_feedback_cells(); |
| 9927 | for (int i = 0; i < type_feedback_cells->CellCount(); i++) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 9928 | JSGlobalPropertyCell* cell = type_feedback_cells->Cell(i); |
| 9929 | cell->set_value(TypeFeedbackCells::RawUninitializedSentinel(heap)); |
| 9930 | } |
| 9931 | } |
| 9932 | } |
| 9933 | |
| 9934 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9935 | bool Code::allowed_in_shared_map_code_cache() { |
mstarzinger@chromium.org | 06e015b | 2012-06-14 15:34:20 +0000 | [diff] [blame] | 9936 | return is_keyed_load_stub() || is_keyed_store_stub() || |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 9937 | (is_compare_ic_stub() && |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 9938 | ICCompareStub::CompareState(stub_info()) == CompareIC::KNOWN_OBJECT); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9939 | } |
| 9940 | |
| 9941 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9942 | void Code::MakeCodeAgeSequenceYoung(byte* sequence) { |
| 9943 | PatchPlatformCodeAge(sequence, kNoAge, NO_MARKING_PARITY); |
| 9944 | } |
| 9945 | |
| 9946 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9947 | void Code::MakeOlder(MarkingParity current_parity) { |
| 9948 | byte* sequence = FindCodeAgeSequence(); |
| 9949 | if (sequence != NULL) { |
| 9950 | Age age; |
| 9951 | MarkingParity code_parity; |
| 9952 | GetCodeAgeAndParity(sequence, &age, &code_parity); |
| 9953 | if (age != kLastCodeAge && code_parity != current_parity) { |
| 9954 | PatchPlatformCodeAge(sequence, static_cast<Age>(age + 1), |
| 9955 | current_parity); |
| 9956 | } |
| 9957 | } |
| 9958 | } |
| 9959 | |
| 9960 | |
| 9961 | bool Code::IsOld() { |
| 9962 | byte* sequence = FindCodeAgeSequence(); |
| 9963 | if (sequence == NULL) return false; |
| 9964 | Age age; |
| 9965 | MarkingParity parity; |
| 9966 | GetCodeAgeAndParity(sequence, &age, &parity); |
| 9967 | return age >= kSexagenarianCodeAge; |
| 9968 | } |
| 9969 | |
| 9970 | |
| 9971 | byte* Code::FindCodeAgeSequence() { |
| 9972 | return FLAG_age_code && |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 9973 | prologue_offset() != kPrologueOffsetNotSet && |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9974 | (kind() == OPTIMIZED_FUNCTION || |
| 9975 | (kind() == FUNCTION && !has_debug_break_slots())) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 9976 | ? instruction_start() + prologue_offset() |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9977 | : NULL; |
| 9978 | } |
| 9979 | |
| 9980 | |
| 9981 | void Code::GetCodeAgeAndParity(Code* code, Age* age, |
| 9982 | MarkingParity* parity) { |
| 9983 | Isolate* isolate = Isolate::Current(); |
| 9984 | Builtins* builtins = isolate->builtins(); |
| 9985 | Code* stub = NULL; |
| 9986 | #define HANDLE_CODE_AGE(AGE) \ |
| 9987 | stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \ |
| 9988 | if (code == stub) { \ |
| 9989 | *age = k##AGE##CodeAge; \ |
| 9990 | *parity = EVEN_MARKING_PARITY; \ |
| 9991 | return; \ |
| 9992 | } \ |
| 9993 | stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 9994 | if (code == stub) { \ |
| 9995 | *age = k##AGE##CodeAge; \ |
| 9996 | *parity = ODD_MARKING_PARITY; \ |
| 9997 | return; \ |
| 9998 | } |
| 9999 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10000 | #undef HANDLE_CODE_AGE |
| 10001 | UNREACHABLE(); |
| 10002 | } |
| 10003 | |
| 10004 | |
| 10005 | Code* Code::GetCodeAgeStub(Age age, MarkingParity parity) { |
| 10006 | Isolate* isolate = Isolate::Current(); |
| 10007 | Builtins* builtins = isolate->builtins(); |
| 10008 | switch (age) { |
| 10009 | #define HANDLE_CODE_AGE(AGE) \ |
| 10010 | case k##AGE##CodeAge: { \ |
| 10011 | Code* stub = parity == EVEN_MARKING_PARITY \ |
| 10012 | ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \ |
| 10013 | : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 10014 | return stub; \ |
| 10015 | } |
| 10016 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10017 | #undef HANDLE_CODE_AGE |
| 10018 | default: |
| 10019 | UNREACHABLE(); |
| 10020 | break; |
| 10021 | } |
| 10022 | return NULL; |
| 10023 | } |
| 10024 | |
| 10025 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10026 | void Code::PrintDeoptLocation(int bailout_id) { |
| 10027 | const char* last_comment = NULL; |
| 10028 | int mask = RelocInfo::ModeMask(RelocInfo::COMMENT) |
| 10029 | | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY); |
| 10030 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10031 | RelocInfo* info = it.rinfo(); |
| 10032 | if (info->rmode() == RelocInfo::COMMENT) { |
| 10033 | last_comment = reinterpret_cast<const char*>(info->data()); |
| 10034 | } else if (last_comment != NULL && |
| 10035 | bailout_id == Deoptimizer::GetDeoptimizationId( |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 10036 | GetIsolate(), info->target_address(), Deoptimizer::EAGER)) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10037 | CHECK(RelocInfo::IsRuntimeEntry(info->rmode())); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10038 | PrintF(" %s\n", last_comment); |
| 10039 | return; |
| 10040 | } |
| 10041 | } |
| 10042 | } |
| 10043 | |
| 10044 | |
| 10045 | // Identify kind of code. |
| 10046 | const char* Code::Kind2String(Kind kind) { |
| 10047 | switch (kind) { |
| 10048 | case FUNCTION: return "FUNCTION"; |
| 10049 | case OPTIMIZED_FUNCTION: return "OPTIMIZED_FUNCTION"; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10050 | case STUB: return "STUB"; |
| 10051 | case BUILTIN: return "BUILTIN"; |
| 10052 | case LOAD_IC: return "LOAD_IC"; |
| 10053 | case KEYED_LOAD_IC: return "KEYED_LOAD_IC"; |
| 10054 | case STORE_IC: return "STORE_IC"; |
| 10055 | case KEYED_STORE_IC: return "KEYED_STORE_IC"; |
| 10056 | case CALL_IC: return "CALL_IC"; |
| 10057 | case KEYED_CALL_IC: return "KEYED_CALL_IC"; |
| 10058 | case UNARY_OP_IC: return "UNARY_OP_IC"; |
| 10059 | case BINARY_OP_IC: return "BINARY_OP_IC"; |
| 10060 | case COMPARE_IC: return "COMPARE_IC"; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10061 | case COMPARE_NIL_IC: return "COMPARE_NIL_IC"; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10062 | case TO_BOOLEAN_IC: return "TO_BOOLEAN_IC"; |
| 10063 | } |
| 10064 | UNREACHABLE(); |
| 10065 | return NULL; |
| 10066 | } |
| 10067 | |
| 10068 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10069 | #ifdef ENABLE_DISASSEMBLER |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10070 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10071 | void DeoptimizationInputData::DeoptimizationInputDataPrint(FILE* out) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10072 | disasm::NameConverter converter; |
| 10073 | int deopt_count = DeoptCount(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10074 | PrintF(out, "Deoptimization Input Data (deopt points = %d)\n", deopt_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10075 | if (0 == deopt_count) return; |
| 10076 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10077 | PrintF(out, "%6s %6s %6s %6s %12s\n", "index", "ast id", "argc", "pc", |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10078 | FLAG_print_code_verbose ? "commands" : ""); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10079 | for (int i = 0; i < deopt_count; i++) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10080 | PrintF(out, "%6d %6d %6d %6d", |
| 10081 | i, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10082 | AstId(i).ToInt(), |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10083 | ArgumentsStackHeight(i)->value(), |
| 10084 | Pc(i)->value()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10085 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10086 | if (!FLAG_print_code_verbose) { |
| 10087 | PrintF(out, "\n"); |
| 10088 | continue; |
| 10089 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10090 | // Print details of the frame translation. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10091 | int translation_index = TranslationIndex(i)->value(); |
| 10092 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 10093 | Translation::Opcode opcode = |
| 10094 | static_cast<Translation::Opcode>(iterator.Next()); |
| 10095 | ASSERT(Translation::BEGIN == opcode); |
| 10096 | int frame_count = iterator.Next(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10097 | int jsframe_count = iterator.Next(); |
| 10098 | PrintF(out, " %s {frame count=%d, js frame count=%d}\n", |
| 10099 | Translation::StringFor(opcode), |
| 10100 | frame_count, |
| 10101 | jsframe_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10102 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10103 | while (iterator.HasNext() && |
| 10104 | Translation::BEGIN != |
| 10105 | (opcode = static_cast<Translation::Opcode>(iterator.Next()))) { |
| 10106 | PrintF(out, "%24s %s ", "", Translation::StringFor(opcode)); |
| 10107 | |
| 10108 | switch (opcode) { |
| 10109 | case Translation::BEGIN: |
| 10110 | UNREACHABLE(); |
| 10111 | break; |
| 10112 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10113 | case Translation::JS_FRAME: { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10114 | int ast_id = iterator.Next(); |
| 10115 | int function_id = iterator.Next(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10116 | unsigned height = iterator.Next(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 10117 | PrintF(out, "{ast_id=%d, function=", ast_id); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10118 | if (function_id != Translation::kSelfLiteralId) { |
| 10119 | Object* function = LiteralArray()->get(function_id); |
| 10120 | JSFunction::cast(function)->PrintName(out); |
| 10121 | } else { |
| 10122 | PrintF(out, "<self>"); |
| 10123 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10124 | PrintF(out, ", height=%u}", height); |
| 10125 | break; |
| 10126 | } |
| 10127 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10128 | case Translation::COMPILED_STUB_FRAME: { |
| 10129 | Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next()); |
| 10130 | PrintF(out, "{kind=%d}", stub_kind); |
| 10131 | break; |
| 10132 | } |
| 10133 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10134 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
| 10135 | case Translation::CONSTRUCT_STUB_FRAME: { |
| 10136 | int function_id = iterator.Next(); |
| 10137 | JSFunction* function = |
| 10138 | JSFunction::cast(LiteralArray()->get(function_id)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10139 | unsigned height = iterator.Next(); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10140 | PrintF(out, "{function="); |
| 10141 | function->PrintName(out); |
| 10142 | PrintF(out, ", height=%u}", height); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10143 | break; |
| 10144 | } |
| 10145 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 10146 | case Translation::GETTER_STUB_FRAME: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10147 | case Translation::SETTER_STUB_FRAME: { |
| 10148 | int function_id = iterator.Next(); |
| 10149 | JSFunction* function = |
| 10150 | JSFunction::cast(LiteralArray()->get(function_id)); |
| 10151 | PrintF(out, "{function="); |
| 10152 | function->PrintName(out); |
| 10153 | PrintF(out, "}"); |
| 10154 | break; |
| 10155 | } |
| 10156 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10157 | case Translation::DUPLICATE: |
| 10158 | break; |
| 10159 | |
| 10160 | case Translation::REGISTER: { |
| 10161 | int reg_code = iterator.Next(); |
| 10162 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10163 | break; |
| 10164 | } |
| 10165 | |
| 10166 | case Translation::INT32_REGISTER: { |
| 10167 | int reg_code = iterator.Next(); |
| 10168 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10169 | break; |
| 10170 | } |
| 10171 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10172 | case Translation::UINT32_REGISTER: { |
| 10173 | int reg_code = iterator.Next(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10174 | PrintF(out, "{input=%s (unsigned)}", |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10175 | converter.NameOfCPURegister(reg_code)); |
| 10176 | break; |
| 10177 | } |
| 10178 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10179 | case Translation::DOUBLE_REGISTER: { |
| 10180 | int reg_code = iterator.Next(); |
| 10181 | PrintF(out, "{input=%s}", |
| 10182 | DoubleRegister::AllocationIndexToString(reg_code)); |
| 10183 | break; |
| 10184 | } |
| 10185 | |
| 10186 | case Translation::STACK_SLOT: { |
| 10187 | int input_slot_index = iterator.Next(); |
| 10188 | PrintF(out, "{input=%d}", input_slot_index); |
| 10189 | break; |
| 10190 | } |
| 10191 | |
| 10192 | case Translation::INT32_STACK_SLOT: { |
| 10193 | int input_slot_index = iterator.Next(); |
| 10194 | PrintF(out, "{input=%d}", input_slot_index); |
| 10195 | break; |
| 10196 | } |
| 10197 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10198 | case Translation::UINT32_STACK_SLOT: { |
| 10199 | int input_slot_index = iterator.Next(); |
| 10200 | PrintF(out, "{input=%d (unsigned)}", input_slot_index); |
| 10201 | break; |
| 10202 | } |
| 10203 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10204 | case Translation::DOUBLE_STACK_SLOT: { |
| 10205 | int input_slot_index = iterator.Next(); |
| 10206 | PrintF(out, "{input=%d}", input_slot_index); |
| 10207 | break; |
| 10208 | } |
| 10209 | |
| 10210 | case Translation::LITERAL: { |
| 10211 | unsigned literal_index = iterator.Next(); |
| 10212 | PrintF(out, "{literal_id=%u}", literal_index); |
| 10213 | break; |
| 10214 | } |
| 10215 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10216 | case Translation::ARGUMENTS_OBJECT: { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10217 | bool args_known = iterator.Next(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10218 | int args_index = iterator.Next(); |
| 10219 | int args_length = iterator.Next(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10220 | PrintF(out, "{index=%d, length=%d, known=%d}", |
| 10221 | args_index, args_length, args_known); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10222 | break; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10223 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10224 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10225 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10226 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10227 | } |
| 10228 | } |
| 10229 | |
| 10230 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10231 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint(FILE* out) { |
| 10232 | PrintF(out, "Deoptimization Output Data (deopt points = %d)\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10233 | this->DeoptPoints()); |
| 10234 | if (this->DeoptPoints() == 0) return; |
| 10235 | |
| 10236 | PrintF("%6s %8s %s\n", "ast id", "pc", "state"); |
| 10237 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 10238 | int pc_and_state = this->PcAndState(i)->value(); |
| 10239 | PrintF("%6d %8d %s\n", |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10240 | this->AstId(i).ToInt(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10241 | FullCodeGenerator::PcField::decode(pc_and_state), |
| 10242 | FullCodeGenerator::State2String( |
| 10243 | FullCodeGenerator::StateField::decode(pc_and_state))); |
| 10244 | } |
| 10245 | } |
| 10246 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10247 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10248 | const char* Code::ICState2String(InlineCacheState state) { |
| 10249 | switch (state) { |
| 10250 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 10251 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 10252 | case MONOMORPHIC: return "MONOMORPHIC"; |
| 10253 | case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE"; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 10254 | case POLYMORPHIC: return "POLYMORPHIC"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10255 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 10256 | case GENERIC: return "GENERIC"; |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 10257 | case DEBUG_STUB: return "DEBUG_STUB"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10258 | } |
| 10259 | UNREACHABLE(); |
| 10260 | return NULL; |
| 10261 | } |
| 10262 | |
| 10263 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10264 | const char* Code::StubType2String(StubType type) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10265 | switch (type) { |
| 10266 | case NORMAL: return "NORMAL"; |
| 10267 | case FIELD: return "FIELD"; |
| 10268 | case CONSTANT_FUNCTION: return "CONSTANT_FUNCTION"; |
| 10269 | case CALLBACKS: return "CALLBACKS"; |
| 10270 | case INTERCEPTOR: return "INTERCEPTOR"; |
| 10271 | case MAP_TRANSITION: return "MAP_TRANSITION"; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10272 | case NONEXISTENT: return "NONEXISTENT"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10273 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 10274 | UNREACHABLE(); // keep the compiler happy |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10275 | return NULL; |
| 10276 | } |
| 10277 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10278 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10279 | void Code::PrintExtraICState(FILE* out, Kind kind, ExtraICState extra) { |
| 10280 | const char* name = NULL; |
| 10281 | switch (kind) { |
| 10282 | case CALL_IC: |
| 10283 | if (extra == STRING_INDEX_OUT_OF_BOUNDS) { |
| 10284 | name = "STRING_INDEX_OUT_OF_BOUNDS"; |
| 10285 | } |
| 10286 | break; |
| 10287 | case STORE_IC: |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10288 | case KEYED_STORE_IC: |
| 10289 | if (extra == kStrictMode) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10290 | name = "STRICT"; |
| 10291 | } |
| 10292 | break; |
| 10293 | default: |
| 10294 | break; |
| 10295 | } |
| 10296 | if (name != NULL) { |
| 10297 | PrintF(out, "extra_ic_state = %s\n", name); |
| 10298 | } else { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10299 | PrintF(out, "extra_ic_state = %d\n", extra); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10300 | } |
| 10301 | } |
| 10302 | |
| 10303 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10304 | void Code::Disassemble(const char* name, FILE* out) { |
| 10305 | PrintF(out, "kind = %s\n", Kind2String(kind())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10306 | if (is_inline_cache_stub()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10307 | PrintF(out, "ic_state = %s\n", ICState2String(ic_state())); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10308 | PrintExtraICState(out, kind(), extra_ic_state()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10309 | if (ic_state() == MONOMORPHIC) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10310 | PrintF(out, "type = %s\n", StubType2String(type())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10311 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10312 | if (is_call_stub() || is_keyed_call_stub()) { |
| 10313 | PrintF(out, "argc = %d\n", arguments_count()); |
| 10314 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 10315 | if (is_compare_ic_stub()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10316 | ASSERT(major_key() == CodeStub::CompareIC); |
| 10317 | CompareIC::State left_state, right_state, handler_state; |
| 10318 | Token::Value op; |
| 10319 | ICCompareStub::DecodeMinorKey(stub_info(), &left_state, &right_state, |
| 10320 | &handler_state, &op); |
| 10321 | PrintF(out, "compare_state = %s*%s -> %s\n", |
| 10322 | CompareIC::GetStateName(left_state), |
| 10323 | CompareIC::GetStateName(right_state), |
| 10324 | CompareIC::GetStateName(handler_state)); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10325 | PrintF(out, "compare_operation = %s\n", Token::Name(op)); |
| 10326 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10327 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 10328 | if ((name != NULL) && (name[0] != '\0')) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10329 | PrintF(out, "name = %s\n", name); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 10330 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10331 | if (kind() == OPTIMIZED_FUNCTION) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10332 | PrintF(out, "stack_slots = %d\n", stack_slots()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10333 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10334 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10335 | PrintF(out, "Instructions (size = %d)\n", instruction_size()); |
| 10336 | Disassembler::Decode(out, this); |
| 10337 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10338 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10339 | if (kind() == FUNCTION) { |
| 10340 | DeoptimizationOutputData* data = |
| 10341 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10342 | data->DeoptimizationOutputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10343 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 10344 | DeoptimizationInputData* data = |
| 10345 | DeoptimizationInputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10346 | data->DeoptimizationInputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10347 | } |
| 10348 | PrintF("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10349 | |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 10350 | if (is_crankshafted()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10351 | SafepointTable table(this); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10352 | PrintF(out, "Safepoints (size = %u)\n", table.size()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10353 | for (unsigned i = 0; i < table.length(); i++) { |
| 10354 | unsigned pc_offset = table.GetPcOffset(i); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10355 | PrintF(out, "%p %4d ", (instruction_start() + pc_offset), pc_offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10356 | table.PrintEntry(i); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10357 | PrintF(out, " (sp -> fp)"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10358 | SafepointEntry entry = table.GetEntry(i); |
| 10359 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
| 10360 | PrintF(out, " %6d", entry.deoptimization_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10361 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10362 | PrintF(out, " <none>"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10363 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10364 | if (entry.argument_count() > 0) { |
| 10365 | PrintF(out, " argc: %d", entry.argument_count()); |
| 10366 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10367 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10368 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10369 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10370 | } else if (kind() == FUNCTION) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 10371 | unsigned offset = back_edge_table_offset(); |
| 10372 | // If there is no back edge table, the "table start" will be at or after |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10373 | // (due to alignment) the end of the instruction stream. |
| 10374 | if (static_cast<int>(offset) < instruction_size()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 10375 | Address back_edge_cursor = instruction_start() + offset; |
| 10376 | uint32_t table_length = Memory::uint32_at(back_edge_cursor); |
| 10377 | PrintF(out, "Back edges (size = %u)\n", table_length); |
| 10378 | PrintF(out, "ast_id pc_offset loop_depth\n"); |
| 10379 | for (uint32_t i = 0; i < table_length; ++i) { |
| 10380 | uint32_t ast_id = Memory::uint32_at(back_edge_cursor); |
| 10381 | uint32_t pc_offset = Memory::uint32_at(back_edge_cursor + kIntSize); |
| 10382 | uint8_t loop_depth = Memory::uint8_at(back_edge_cursor + 2 * kIntSize); |
| 10383 | PrintF(out, "%6u %9u %10u\n", ast_id, pc_offset, loop_depth); |
| 10384 | back_edge_cursor += FullCodeGenerator::kBackEdgeEntrySize; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10385 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10386 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10387 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 10388 | #ifdef OBJECT_PRINT |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 10389 | if (!type_feedback_info()->IsUndefined()) { |
| 10390 | TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(out); |
| 10391 | PrintF(out, "\n"); |
| 10392 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 10393 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10394 | } |
| 10395 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10396 | PrintF("RelocInfo (size = %d)\n", relocation_size()); |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 10397 | for (RelocIterator it(this); !it.done(); it.next()) { |
| 10398 | it.rinfo()->Print(GetIsolate(), out); |
| 10399 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10400 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10401 | } |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 10402 | #endif // ENABLE_DISASSEMBLER |
| 10403 | |
| 10404 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10405 | MaybeObject* JSObject::SetFastElementsCapacityAndLength( |
| 10406 | int capacity, |
| 10407 | int length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10408 | SetFastElementsCapacitySmiMode smi_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10409 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 10410 | // We should never end in here with a pixel or external array. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 10411 | ASSERT(!HasExternalArrayElements()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10412 | ASSERT(!map()->is_observed()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10413 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10414 | // Allocate a new fast elements backing store. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 10415 | FixedArray* new_elements; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10416 | MaybeObject* maybe = heap->AllocateUninitializedFixedArray(capacity); |
| 10417 | if (!maybe->To(&new_elements)) return maybe; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10418 | |
jkummerow@chromium.org | f3eae90 | 2012-05-24 16:42:53 +0000 | [diff] [blame] | 10419 | ElementsKind elements_kind = GetElementsKind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10420 | ElementsKind new_elements_kind; |
| 10421 | // The resized array has FAST_*_SMI_ELEMENTS if the capacity mode forces it, |
| 10422 | // or if it's allowed and the old elements array contained only SMIs. |
| 10423 | bool has_fast_smi_elements = |
| 10424 | (smi_mode == kForceSmiElements) || |
| 10425 | ((smi_mode == kAllowSmiElements) && HasFastSmiElements()); |
| 10426 | if (has_fast_smi_elements) { |
| 10427 | if (IsHoleyElementsKind(elements_kind)) { |
| 10428 | new_elements_kind = FAST_HOLEY_SMI_ELEMENTS; |
| 10429 | } else { |
| 10430 | new_elements_kind = FAST_SMI_ELEMENTS; |
| 10431 | } |
| 10432 | } else { |
| 10433 | if (IsHoleyElementsKind(elements_kind)) { |
| 10434 | new_elements_kind = FAST_HOLEY_ELEMENTS; |
| 10435 | } else { |
| 10436 | new_elements_kind = FAST_ELEMENTS; |
| 10437 | } |
| 10438 | } |
| 10439 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 10440 | ElementsAccessor* accessor = ElementsAccessor::ForKind(new_elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10441 | MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 10442 | accessor->CopyElements(this, new_elements, elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10443 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 10444 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 10445 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10446 | Map* new_map = map(); |
| 10447 | if (new_elements_kind != elements_kind) { |
| 10448 | MaybeObject* maybe = |
| 10449 | GetElementsTransitionMap(GetIsolate(), new_elements_kind); |
| 10450 | if (!maybe->To(&new_map)) return maybe; |
| 10451 | } |
| 10452 | ValidateElements(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 10453 | set_map_and_elements(new_map, new_elements); |
| 10454 | } else { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 10455 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 10456 | parameter_map->set(1, new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10457 | } |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10458 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10459 | if (FLAG_trace_elements_transitions) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 10460 | PrintElementsTransition(stdout, elements_kind, old_elements, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 10461 | GetElementsKind(), new_elements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10462 | } |
| 10463 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10464 | if (IsJSArray()) { |
| 10465 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 10466 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10467 | return new_elements; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10468 | } |
| 10469 | |
| 10470 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10471 | MaybeObject* JSObject::SetFastDoubleElementsCapacityAndLength( |
| 10472 | int capacity, |
| 10473 | int length) { |
| 10474 | Heap* heap = GetHeap(); |
| 10475 | // We should never end in here with a pixel or external array. |
| 10476 | ASSERT(!HasExternalArrayElements()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10477 | ASSERT(!map()->is_observed()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10478 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10479 | FixedArrayBase* elems; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10480 | { MaybeObject* maybe_obj = |
| 10481 | heap->AllocateUninitializedFixedDoubleArray(capacity); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 10482 | if (!maybe_obj->To(&elems)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10483 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10484 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10485 | ElementsKind elements_kind = GetElementsKind(); |
| 10486 | ElementsKind new_elements_kind = elements_kind; |
| 10487 | if (IsHoleyElementsKind(elements_kind)) { |
| 10488 | new_elements_kind = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 10489 | } else { |
| 10490 | new_elements_kind = FAST_DOUBLE_ELEMENTS; |
| 10491 | } |
| 10492 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 10493 | Map* new_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10494 | { MaybeObject* maybe_obj = |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10495 | GetElementsTransitionMap(heap->isolate(), new_elements_kind); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 10496 | if (!maybe_obj->To(&new_map)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10497 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10498 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10499 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 10500 | ElementsAccessor* accessor = ElementsAccessor::ForKind(FAST_DOUBLE_ELEMENTS); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10501 | { MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 10502 | accessor->CopyElements(this, elems, elements_kind); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10503 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 10504 | } |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 10505 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10506 | ValidateElements(); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 10507 | set_map_and_elements(new_map, elems); |
| 10508 | } else { |
| 10509 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
| 10510 | parameter_map->set(1, elems); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10511 | } |
| 10512 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10513 | if (FLAG_trace_elements_transitions) { |
| 10514 | PrintElementsTransition(stdout, elements_kind, old_elements, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 10515 | GetElementsKind(), elems); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10516 | } |
| 10517 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10518 | if (IsJSArray()) { |
| 10519 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 10520 | } |
| 10521 | |
| 10522 | return this; |
| 10523 | } |
| 10524 | |
| 10525 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10526 | MaybeObject* JSArray::Initialize(int capacity, int length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10527 | ASSERT(capacity >= 0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10528 | return GetHeap()->AllocateJSArrayStorage(this, length, capacity, |
| 10529 | INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10530 | } |
| 10531 | |
| 10532 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10533 | void JSArray::Expand(int required_size) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 10534 | GetIsolate()->factory()->SetElementsCapacityAndLength( |
| 10535 | Handle<JSArray>(this), required_size, required_size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 10536 | } |
| 10537 | |
| 10538 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 10539 | // Returns false if the passed-in index is marked non-configurable, |
| 10540 | // which will cause the ES5 truncation operation to halt, and thus |
| 10541 | // no further old values need be collected. |
| 10542 | static bool GetOldValue(Isolate* isolate, |
| 10543 | Handle<JSObject> object, |
| 10544 | uint32_t index, |
| 10545 | List<Handle<Object> >* old_values, |
| 10546 | List<Handle<String> >* indices) { |
| 10547 | PropertyAttributes attributes = object->GetLocalElementAttribute(index); |
| 10548 | ASSERT(attributes != ABSENT); |
| 10549 | if (attributes == DONT_DELETE) return false; |
| 10550 | old_values->Add(object->GetLocalElementAccessorPair(index) == NULL |
| 10551 | ? Object::GetElement(object, index) |
| 10552 | : Handle<Object>::cast(isolate->factory()->the_hole_value())); |
| 10553 | indices->Add(isolate->factory()->Uint32ToString(index)); |
| 10554 | return true; |
| 10555 | } |
| 10556 | |
| 10557 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 10558 | MaybeObject* JSArray::SetElementsLength(Object* len) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 10559 | // We should never end in here with a pixel or external array. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10560 | ASSERT(AllowsSetElementsLength()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10561 | if (!(FLAG_harmony_observation && map()->is_observed())) |
| 10562 | return GetElementsAccessor()->SetLength(this, len); |
| 10563 | |
| 10564 | Isolate* isolate = GetIsolate(); |
| 10565 | HandleScope scope(isolate); |
| 10566 | Handle<JSArray> self(this); |
| 10567 | List<Handle<String> > indices; |
| 10568 | List<Handle<Object> > old_values; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 10569 | Handle<Object> old_length_handle(self->length(), isolate); |
| 10570 | Handle<Object> new_length_handle(len, isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10571 | uint32_t old_length = 0; |
| 10572 | CHECK(old_length_handle->ToArrayIndex(&old_length)); |
| 10573 | uint32_t new_length = 0; |
| 10574 | if (!new_length_handle->ToArrayIndex(&new_length)) |
| 10575 | return Failure::InternalError(); |
| 10576 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 10577 | // Observed arrays should always be in dictionary mode; |
| 10578 | // if they were in fast mode, the below is slower than necessary |
| 10579 | // as it iterates over the array backing store multiple times. |
| 10580 | ASSERT(self->HasDictionaryElements()); |
| 10581 | static const PropertyAttributes kNoAttrFilter = NONE; |
| 10582 | int num_elements = self->NumberOfLocalElements(kNoAttrFilter); |
| 10583 | if (num_elements > 0) { |
| 10584 | if (old_length == static_cast<uint32_t>(num_elements)) { |
| 10585 | // Simple case for arrays without holes. |
| 10586 | for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { |
| 10587 | if (!GetOldValue(isolate, self, i, &old_values, &indices)) break; |
| 10588 | } |
| 10589 | } else { |
| 10590 | // For sparse arrays, only iterate over existing elements. |
| 10591 | Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); |
| 10592 | self->GetLocalElementKeys(*keys, kNoAttrFilter); |
| 10593 | while (num_elements-- > 0) { |
| 10594 | uint32_t index = NumberToUint32(keys->get(num_elements)); |
| 10595 | if (index < new_length) break; |
| 10596 | if (!GetOldValue(isolate, self, index, &old_values, &indices)) break; |
| 10597 | } |
| 10598 | } |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10599 | } |
| 10600 | |
| 10601 | MaybeObject* result = |
| 10602 | self->GetElementsAccessor()->SetLength(*self, *new_length_handle); |
| 10603 | Handle<Object> hresult; |
| 10604 | if (!result->ToHandle(&hresult, isolate)) return result; |
| 10605 | |
| 10606 | CHECK(self->length()->ToArrayIndex(&new_length)); |
| 10607 | if (old_length != new_length) { |
| 10608 | for (int i = 0; i < indices.length(); ++i) { |
| 10609 | JSObject::EnqueueChangeRecord( |
| 10610 | self, "deleted", indices[i], old_values[i]); |
| 10611 | } |
| 10612 | JSObject::EnqueueChangeRecord( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10613 | self, "updated", isolate->factory()->length_string(), |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10614 | old_length_handle); |
| 10615 | } |
| 10616 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10617 | } |
| 10618 | |
| 10619 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10620 | Map* Map::GetPrototypeTransition(Object* prototype) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 10621 | FixedArray* cache = GetPrototypeTransitions(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10622 | int number_of_transitions = NumberOfProtoTransitions(); |
| 10623 | const int proto_offset = |
| 10624 | kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset; |
| 10625 | const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset; |
| 10626 | const int step = kProtoTransitionElementsPerEntry; |
| 10627 | for (int i = 0; i < number_of_transitions; i++) { |
| 10628 | if (cache->get(proto_offset + i * step) == prototype) { |
| 10629 | Object* map = cache->get(map_offset + i * step); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10630 | return Map::cast(map); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10631 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10632 | } |
| 10633 | return NULL; |
| 10634 | } |
| 10635 | |
| 10636 | |
| 10637 | MaybeObject* Map::PutPrototypeTransition(Object* prototype, Map* map) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10638 | ASSERT(map->IsMap()); |
| 10639 | ASSERT(HeapObject::cast(prototype)->map()->IsMap()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10640 | // Don't cache prototype transition if this map is shared. |
| 10641 | if (is_shared() || !FLAG_cache_prototype_transitions) return this; |
| 10642 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 10643 | FixedArray* cache = GetPrototypeTransitions(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10644 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10645 | const int step = kProtoTransitionElementsPerEntry; |
| 10646 | const int header = kProtoTransitionHeaderSize; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10647 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10648 | int capacity = (cache->length() - header) / step; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10649 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10650 | int transitions = NumberOfProtoTransitions() + 1; |
| 10651 | |
| 10652 | if (transitions > capacity) { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10653 | if (capacity > kMaxCachedPrototypeTransitions) return this; |
| 10654 | |
| 10655 | FixedArray* new_cache; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10656 | // Grow array by factor 2 over and above what we need. |
| 10657 | { MaybeObject* maybe_cache = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10658 | GetHeap()->AllocateFixedArray(transitions * 2 * step + header); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 10659 | if (!maybe_cache->To(&new_cache)) return maybe_cache; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10660 | } |
| 10661 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10662 | for (int i = 0; i < capacity * step; i++) { |
| 10663 | new_cache->set(i + header, cache->get(i + header)); |
| 10664 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10665 | cache = new_cache; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 10666 | MaybeObject* set_result = SetPrototypeTransitions(cache); |
| 10667 | if (set_result->IsFailure()) return set_result; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10668 | } |
| 10669 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10670 | int last = transitions - 1; |
| 10671 | |
| 10672 | cache->set(header + last * step + kProtoTransitionPrototypeOffset, prototype); |
| 10673 | cache->set(header + last * step + kProtoTransitionMapOffset, map); |
| 10674 | SetNumberOfProtoTransitions(transitions); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10675 | |
| 10676 | return cache; |
| 10677 | } |
| 10678 | |
| 10679 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 10680 | void Map::ZapTransitions() { |
| 10681 | TransitionArray* transition_array = transitions(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 10682 | // TODO(mstarzinger): Temporarily use a slower version instead of the faster |
| 10683 | // MemsetPointer to investigate a crasher. Switch back to MemsetPointer. |
| 10684 | Object** data = transition_array->data_start(); |
| 10685 | Object* the_hole = GetHeap()->the_hole_value(); |
| 10686 | int length = transition_array->length(); |
| 10687 | for (int i = 0; i < length; i++) { |
| 10688 | data[i] = the_hole; |
| 10689 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 10690 | } |
| 10691 | |
| 10692 | |
| 10693 | void Map::ZapPrototypeTransitions() { |
| 10694 | FixedArray* proto_transitions = GetPrototypeTransitions(); |
| 10695 | MemsetPointer(proto_transitions->data_start(), |
| 10696 | GetHeap()->the_hole_value(), |
| 10697 | proto_transitions->length()); |
| 10698 | } |
| 10699 | |
| 10700 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10701 | DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) { |
| 10702 | Recompute(entries); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 10703 | } |
| 10704 | |
| 10705 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10706 | void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) { |
| 10707 | start_indexes_[0] = 0; |
| 10708 | for (int g = 1; g <= kGroupCount; g++) { |
| 10709 | int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1)); |
| 10710 | start_indexes_[g] = start_indexes_[g - 1] + count; |
| 10711 | } |
| 10712 | } |
| 10713 | |
| 10714 | |
| 10715 | Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries, |
| 10716 | DependencyGroup group, |
| 10717 | Handle<Code> value) { |
| 10718 | GroupStartIndexes starts(*entries); |
| 10719 | int start = starts.at(group); |
| 10720 | int end = starts.at(group + 1); |
| 10721 | int number_of_entries = starts.number_of_entries(); |
| 10722 | if (start < end && entries->code_at(end - 1) == *value) { |
| 10723 | // Do not append the code if it is already in the array. |
| 10724 | // It is sufficient to just check only the last element because |
| 10725 | // we process embedded maps of an optimized code in one batch. |
| 10726 | return entries; |
| 10727 | } |
| 10728 | if (entries->length() < kCodesStartIndex + number_of_entries + 1) { |
| 10729 | Factory* factory = entries->GetIsolate()->factory(); |
| 10730 | int capacity = kCodesStartIndex + number_of_entries + 1; |
| 10731 | if (capacity > 5) capacity = capacity * 5 / 4; |
| 10732 | Handle<DependentCode> new_entries = Handle<DependentCode>::cast( |
| 10733 | factory->CopySizeFixedArray(entries, capacity)); |
| 10734 | // The number of codes can change after GC. |
| 10735 | starts.Recompute(*entries); |
| 10736 | start = starts.at(group); |
| 10737 | end = starts.at(group + 1); |
| 10738 | number_of_entries = starts.number_of_entries(); |
| 10739 | for (int i = 0; i < number_of_entries; i++) { |
| 10740 | entries->clear_code_at(i); |
| 10741 | } |
| 10742 | // If the old fixed array was empty, we need to reset counters of the |
| 10743 | // new array. |
| 10744 | if (number_of_entries == 0) { |
| 10745 | for (int g = 0; g < kGroupCount; g++) { |
| 10746 | new_entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0); |
| 10747 | } |
| 10748 | } |
| 10749 | entries = new_entries; |
| 10750 | } |
| 10751 | entries->ExtendGroup(group); |
| 10752 | entries->set_code_at(end, *value); |
| 10753 | entries->set_number_of_entries(group, end + 1 - start); |
| 10754 | return entries; |
| 10755 | } |
| 10756 | |
| 10757 | |
| 10758 | bool DependentCode::Contains(DependencyGroup group, Code* code) { |
| 10759 | GroupStartIndexes starts(this); |
| 10760 | int number_of_entries = starts.at(kGroupCount); |
| 10761 | for (int i = 0; i < number_of_entries; i++) { |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 10762 | if (code_at(i) == code) return true; |
| 10763 | } |
| 10764 | return false; |
| 10765 | } |
| 10766 | |
| 10767 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10768 | class DeoptimizeDependentCodeFilter : public OptimizedFunctionFilter { |
| 10769 | public: |
| 10770 | virtual bool TakeFunction(JSFunction* function) { |
| 10771 | return function->code()->marked_for_deoptimization(); |
| 10772 | } |
| 10773 | }; |
| 10774 | |
| 10775 | |
| 10776 | void DependentCode::DeoptimizeDependentCodeGroup( |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 10777 | Isolate* isolate, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10778 | DependentCode::DependencyGroup group) { |
| 10779 | AssertNoAllocation no_allocation_scope; |
| 10780 | DependentCode::GroupStartIndexes starts(this); |
| 10781 | int start = starts.at(group); |
| 10782 | int end = starts.at(group + 1); |
| 10783 | int number_of_entries = starts.at(DependentCode::kGroupCount); |
| 10784 | if (start == end) return; |
| 10785 | for (int i = start; i < end; i++) { |
| 10786 | Code* code = code_at(i); |
| 10787 | code->set_marked_for_deoptimization(true); |
| 10788 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 10789 | // Compact the array by moving all subsequent groups to fill in the new holes. |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10790 | for (int src = end, dst = start; src < number_of_entries; src++, dst++) { |
| 10791 | set_code_at(dst, code_at(src)); |
| 10792 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 10793 | // Now the holes are at the end of the array, zap them for heap-verifier. |
| 10794 | int removed = end - start; |
| 10795 | for (int i = number_of_entries - removed; i < number_of_entries; i++) { |
| 10796 | clear_code_at(i); |
| 10797 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10798 | set_number_of_entries(group, 0); |
| 10799 | DeoptimizeDependentCodeFilter filter; |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 10800 | Deoptimizer::DeoptimizeAllFunctionsWith(isolate, &filter); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 10801 | } |
| 10802 | |
| 10803 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 10804 | MaybeObject* JSReceiver::SetPrototype(Object* value, |
| 10805 | bool skip_hidden_prototypes) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10806 | #ifdef DEBUG |
| 10807 | int size = Size(); |
| 10808 | #endif |
| 10809 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10810 | Isolate* isolate = GetIsolate(); |
| 10811 | Heap* heap = isolate->heap(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10812 | // Silently ignore the change if value is not a JSObject or null. |
| 10813 | // SpiderMonkey behaves this way. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 10814 | if (!value->IsJSReceiver() && !value->IsNull()) return value; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10815 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 10816 | // From 8.6.2 Object Internal Methods |
| 10817 | // ... |
| 10818 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 10819 | // [[Prototype]] internal properties of the object may not be modified. |
| 10820 | // ... |
| 10821 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 10822 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 10823 | // paragraph. |
| 10824 | if (!this->map()->is_extensible()) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10825 | HandleScope scope(isolate); |
| 10826 | Handle<Object> handle(this, isolate); |
| 10827 | return isolate->Throw( |
| 10828 | *isolate->factory()->NewTypeError("non_extensible_proto", |
| 10829 | HandleVector<Object>(&handle, 1))); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 10830 | } |
| 10831 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10832 | // Before we can set the prototype we need to be sure |
| 10833 | // prototype cycles are prevented. |
| 10834 | // It is sufficient to validate that the receiver is not in the new prototype |
| 10835 | // chain. |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10836 | for (Object* pt = value; |
| 10837 | pt != heap->null_value(); |
| 10838 | pt = pt->GetPrototype(isolate)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10839 | if (JSReceiver::cast(pt) == this) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10840 | // Cycle detected. |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10841 | HandleScope scope(isolate); |
| 10842 | return isolate->Throw( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10843 | *FACTORY->NewError("cyclic_proto", HandleVector<Object>(NULL, 0))); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10844 | } |
| 10845 | } |
| 10846 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 10847 | JSReceiver* real_receiver = this; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10848 | |
| 10849 | if (skip_hidden_prototypes) { |
| 10850 | // Find the first object in the chain whose prototype object is not |
| 10851 | // hidden and set the new prototype on that object. |
| 10852 | Object* current_proto = real_receiver->GetPrototype(); |
| 10853 | while (current_proto->IsJSObject() && |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10854 | JSReceiver::cast(current_proto)->map()->is_hidden_prototype()) { |
| 10855 | real_receiver = JSReceiver::cast(current_proto); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10856 | current_proto = current_proto->GetPrototype(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10857 | } |
| 10858 | } |
| 10859 | |
| 10860 | // Set the new prototype of the object. |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10861 | Map* map = real_receiver->map(); |
| 10862 | |
| 10863 | // Nothing to do if prototype is already set. |
| 10864 | if (map->prototype() == value) return value; |
| 10865 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10866 | if (value->IsJSObject()) { |
| 10867 | MaybeObject* ok = JSObject::cast(value)->OptimizeAsPrototype(); |
| 10868 | if (ok->IsFailure()) return ok; |
| 10869 | } |
| 10870 | |
| 10871 | Map* new_map = map->GetPrototypeTransition(value); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10872 | if (new_map == NULL) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 10873 | MaybeObject* maybe_new_map = map->Copy(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 10874 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10875 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 10876 | MaybeObject* maybe_new_cache = |
| 10877 | map->PutPrototypeTransition(value, new_map); |
| 10878 | if (maybe_new_cache->IsFailure()) return maybe_new_cache; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10879 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10880 | new_map->set_prototype(value); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10881 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10882 | ASSERT(new_map->prototype() == value); |
| 10883 | real_receiver->set_map(new_map); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10884 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10885 | heap->ClearInstanceofCache(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10886 | ASSERT(size == Size()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 10887 | return value; |
| 10888 | } |
| 10889 | |
| 10890 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10891 | MaybeObject* JSObject::EnsureCanContainElements(Arguments* args, |
| 10892 | uint32_t first_arg, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 10893 | uint32_t arg_count, |
| 10894 | EnsureElementsMode mode) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10895 | // Elements in |Arguments| are ordered backwards (because they're on the |
| 10896 | // stack), but the method that's called here iterates over them in forward |
| 10897 | // direction. |
| 10898 | return EnsureCanContainElements( |
| 10899 | args->arguments() - first_arg - (arg_count - 1), |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 10900 | arg_count, mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10901 | } |
| 10902 | |
| 10903 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 10904 | PropertyType JSObject::GetLocalPropertyType(Name* name) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10905 | uint32_t index = 0; |
| 10906 | if (name->AsArrayIndex(&index)) { |
| 10907 | return GetLocalElementType(index); |
| 10908 | } |
| 10909 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10910 | LocalLookup(name, &lookup, true); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10911 | return lookup.type(); |
| 10912 | } |
| 10913 | |
| 10914 | |
| 10915 | PropertyType JSObject::GetLocalElementType(uint32_t index) { |
| 10916 | return GetElementsAccessor()->GetType(this, this, index); |
| 10917 | } |
| 10918 | |
| 10919 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 10920 | AccessorPair* JSObject::GetLocalPropertyAccessorPair(Name* name) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10921 | uint32_t index = 0; |
| 10922 | if (name->AsArrayIndex(&index)) { |
| 10923 | return GetLocalElementAccessorPair(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10924 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10925 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10926 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10927 | LocalLookupRealNamedProperty(name, &lookup); |
| 10928 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10929 | if (lookup.IsPropertyCallbacks() && |
| 10930 | lookup.GetCallbackObject()->IsAccessorPair()) { |
| 10931 | return AccessorPair::cast(lookup.GetCallbackObject()); |
| 10932 | } |
| 10933 | return NULL; |
| 10934 | } |
| 10935 | |
| 10936 | |
| 10937 | AccessorPair* JSObject::GetLocalElementAccessorPair(uint32_t index) { |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 10938 | if (IsJSGlobalProxy()) { |
| 10939 | Object* proto = GetPrototype(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10940 | if (proto->IsNull()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 10941 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10942 | return JSObject::cast(proto)->GetLocalElementAccessorPair(index); |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 10943 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10944 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10945 | // Check for lookup interceptor. |
| 10946 | if (HasIndexedInterceptor()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 10947 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 10948 | return GetElementsAccessor()->GetAccessorPair(this, this, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10949 | } |
| 10950 | |
| 10951 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10952 | MaybeObject* JSObject::SetElementWithInterceptor(uint32_t index, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 10953 | Object* value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 10954 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 10955 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 10956 | bool check_prototype, |
| 10957 | SetPropertyMode set_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10958 | Isolate* isolate = GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10959 | // Make sure that the top context does not change when doing |
| 10960 | // callbacks or interceptor calls. |
| 10961 | AssertNoContextChange ncc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10962 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10963 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 10964 | Handle<JSObject> this_handle(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10965 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10966 | if (!interceptor->setter()->IsUndefined()) { |
| 10967 | v8::IndexedPropertySetter setter = |
| 10968 | v8::ToCData<v8::IndexedPropertySetter>(interceptor->setter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10969 | LOG(isolate, |
| 10970 | ApiIndexedPropertyAccess("interceptor-indexed-set", this, index)); |
| 10971 | CustomArguments args(isolate, interceptor->data(), this, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 10972 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10973 | v8::Handle<v8::Value> result; |
| 10974 | { |
| 10975 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10976 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10977 | result = setter(index, v8::Utils::ToLocal(value_handle), info); |
| 10978 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10979 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10980 | if (!result.IsEmpty()) return *value_handle; |
| 10981 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10982 | MaybeObject* raw_result = |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 10983 | this_handle->SetElementWithoutInterceptor(index, |
| 10984 | *value_handle, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 10985 | attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 10986 | strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 10987 | check_prototype, |
| 10988 | set_mode); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10989 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10990 | return raw_result; |
| 10991 | } |
| 10992 | |
| 10993 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10994 | MaybeObject* JSObject::GetElementWithCallback(Object* receiver, |
| 10995 | Object* structure, |
| 10996 | uint32_t index, |
| 10997 | Object* holder) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10998 | Isolate* isolate = GetIsolate(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10999 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11000 | |
| 11001 | // api style callbacks. |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11002 | if (structure->IsExecutableAccessorInfo()) { |
| 11003 | Handle<ExecutableAccessorInfo> data( |
| 11004 | ExecutableAccessorInfo::cast(structure)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11005 | Object* fun_obj = data->getter(); |
| 11006 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 11007 | if (call_fun == NULL) return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11008 | HandleScope scope(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11009 | Handle<JSObject> self(JSObject::cast(receiver)); |
| 11010 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11011 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11012 | Handle<String> key = isolate->factory()->NumberToString(number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11013 | LOG(isolate, ApiNamedPropertyAccess("load", *self, *key)); |
| 11014 | CustomArguments args(isolate, data->data(), *self, *holder_handle); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11015 | v8::AccessorInfo info(args.end()); |
| 11016 | v8::Handle<v8::Value> result; |
| 11017 | { |
| 11018 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 11019 | VMState<EXTERNAL> state(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11020 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 11021 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11022 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 11023 | if (result.IsEmpty()) return isolate->heap()->undefined_value(); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 11024 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 11025 | result_internal->VerifyApiCallResultType(); |
| 11026 | return *result_internal; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11027 | } |
| 11028 | |
| 11029 | // __defineGetter__ callback |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11030 | if (structure->IsAccessorPair()) { |
| 11031 | Object* getter = AccessorPair::cast(structure)->getter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11032 | if (getter->IsSpecFunction()) { |
| 11033 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 11034 | return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11035 | } |
| 11036 | // Getter is not a function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11037 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11038 | } |
| 11039 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11040 | if (structure->IsDeclaredAccessorInfo()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 11041 | return GetDeclaredAccessorProperty(receiver, |
| 11042 | DeclaredAccessorInfo::cast(structure), |
| 11043 | isolate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11044 | } |
| 11045 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11046 | UNREACHABLE(); |
| 11047 | return NULL; |
| 11048 | } |
| 11049 | |
| 11050 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11051 | MaybeObject* JSObject::SetElementWithCallback(Object* structure, |
| 11052 | uint32_t index, |
| 11053 | Object* value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 11054 | JSObject* holder, |
| 11055 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11056 | Isolate* isolate = GetIsolate(); |
| 11057 | HandleScope scope(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11058 | |
| 11059 | // We should never get here to initialize a const with the hole |
| 11060 | // value since a const declaration would conflict with the setter. |
| 11061 | ASSERT(!value->IsTheHole()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11062 | Handle<Object> value_handle(value, isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11063 | |
| 11064 | // To accommodate both the old and the new api we switch on the |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 11065 | // data structure used to store the callbacks. Eventually foreign |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11066 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 11067 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11068 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11069 | if (structure->IsExecutableAccessorInfo()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11070 | // api style callbacks |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 11071 | Handle<JSObject> self(this); |
| 11072 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11073 | Handle<ExecutableAccessorInfo> data( |
| 11074 | ExecutableAccessorInfo::cast(structure)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11075 | Object* call_obj = data->setter(); |
| 11076 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 11077 | if (call_fun == NULL) return value; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11078 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 11079 | Handle<String> key(isolate->factory()->NumberToString(number)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 11080 | LOG(isolate, ApiNamedPropertyAccess("store", *self, *key)); |
| 11081 | CustomArguments args(isolate, data->data(), *self, *holder_handle); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11082 | v8::AccessorInfo info(args.end()); |
| 11083 | { |
| 11084 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 11085 | VMState<EXTERNAL> state(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11086 | call_fun(v8::Utils::ToLocal(key), |
| 11087 | v8::Utils::ToLocal(value_handle), |
| 11088 | info); |
| 11089 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11090 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11091 | return *value_handle; |
| 11092 | } |
| 11093 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11094 | if (structure->IsAccessorPair()) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11095 | Handle<Object> setter(AccessorPair::cast(structure)->setter(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11096 | if (setter->IsSpecFunction()) { |
| 11097 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 11098 | return SetPropertyWithDefinedSetter(JSReceiver::cast(*setter), value); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11099 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 11100 | if (strict_mode == kNonStrictMode) { |
| 11101 | return value; |
| 11102 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11103 | Handle<Object> holder_handle(holder, isolate); |
| 11104 | Handle<Object> key(isolate->factory()->NewNumberFromUint(index)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11105 | Handle<Object> args[2] = { key, holder_handle }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11106 | return isolate->Throw( |
| 11107 | *isolate->factory()->NewTypeError("no_setter_in_callback", |
| 11108 | HandleVector(args, 2))); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11109 | } |
| 11110 | } |
| 11111 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11112 | // TODO(dcarney): Handle correctly. |
| 11113 | if (structure->IsDeclaredAccessorInfo()) return value; |
| 11114 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11115 | UNREACHABLE(); |
| 11116 | return NULL; |
| 11117 | } |
| 11118 | |
| 11119 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11120 | bool JSObject::HasFastArgumentsElements() { |
| 11121 | Heap* heap = GetHeap(); |
| 11122 | if (!elements()->IsFixedArray()) return false; |
| 11123 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 11124 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 11125 | return false; |
| 11126 | } |
| 11127 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 11128 | return !arguments->IsDictionary(); |
| 11129 | } |
| 11130 | |
| 11131 | |
| 11132 | bool JSObject::HasDictionaryArgumentsElements() { |
| 11133 | Heap* heap = GetHeap(); |
| 11134 | if (!elements()->IsFixedArray()) return false; |
| 11135 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 11136 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 11137 | return false; |
| 11138 | } |
| 11139 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 11140 | return arguments->IsDictionary(); |
| 11141 | } |
| 11142 | |
| 11143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11144 | // Adding n elements in fast case is O(n*n). |
| 11145 | // Note: revisit design to have dual undefined values to capture absent |
| 11146 | // elements. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11147 | MaybeObject* JSObject::SetFastElement(uint32_t index, |
| 11148 | Object* value, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11149 | StrictModeFlag strict_mode, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11150 | bool check_prototype) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11151 | ASSERT(HasFastSmiOrObjectElements() || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11152 | HasFastArgumentsElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11153 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11154 | FixedArray* backing_store = FixedArray::cast(elements()); |
| 11155 | if (backing_store->map() == GetHeap()->non_strict_arguments_elements_map()) { |
| 11156 | backing_store = FixedArray::cast(backing_store->get(1)); |
| 11157 | } else { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11158 | MaybeObject* maybe = EnsureWritableFastElements(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11159 | if (!maybe->To(&backing_store)) return maybe; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11160 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11161 | uint32_t capacity = static_cast<uint32_t>(backing_store->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11162 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11163 | if (check_prototype && |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11164 | (index >= capacity || backing_store->get(index)->IsTheHole())) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 11165 | bool found; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 11166 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index, |
| 11167 | value, |
| 11168 | &found, |
| 11169 | strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 11170 | if (found) return result; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11171 | } |
| 11172 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11173 | uint32_t new_capacity = capacity; |
| 11174 | // Check if the length property of this object needs to be updated. |
| 11175 | uint32_t array_length = 0; |
| 11176 | bool must_update_array_length = false; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11177 | bool introduces_holes = true; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11178 | if (IsJSArray()) { |
| 11179 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11180 | introduces_holes = index > array_length; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11181 | if (index >= array_length) { |
| 11182 | must_update_array_length = true; |
| 11183 | array_length = index + 1; |
| 11184 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11185 | } else { |
| 11186 | introduces_holes = index >= capacity; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11187 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11188 | |
| 11189 | // If the array is growing, and it's not growth by a single element at the |
| 11190 | // end, make sure that the ElementsKind is HOLEY. |
| 11191 | ElementsKind elements_kind = GetElementsKind(); |
| 11192 | if (introduces_holes && |
| 11193 | IsFastElementsKind(elements_kind) && |
| 11194 | !IsFastHoleyElementsKind(elements_kind)) { |
| 11195 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
| 11196 | MaybeObject* maybe = TransitionElementsKind(transitioned_kind); |
| 11197 | if (maybe->IsFailure()) return maybe; |
| 11198 | } |
| 11199 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11200 | // Check if the capacity of the backing store needs to be increased, or if |
| 11201 | // a transition to slow elements is necessary. |
| 11202 | if (index >= capacity) { |
| 11203 | bool convert_to_slow = true; |
| 11204 | if ((index - capacity) < kMaxGap) { |
| 11205 | new_capacity = NewElementsCapacity(index + 1); |
| 11206 | ASSERT(new_capacity > index); |
| 11207 | if (!ShouldConvertToSlowElements(new_capacity)) { |
| 11208 | convert_to_slow = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11209 | } |
| 11210 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11211 | if (convert_to_slow) { |
| 11212 | MaybeObject* result = NormalizeElements(); |
| 11213 | if (result->IsFailure()) return result; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11214 | return SetDictionaryElement(index, value, NONE, strict_mode, |
| 11215 | check_prototype); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11216 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11217 | } |
| 11218 | // Convert to fast double elements if appropriate. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11219 | if (HasFastSmiElements() && !value->IsSmi() && value->IsNumber()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11220 | // Consider fixing the boilerplate as well if we have one. |
| 11221 | ElementsKind to_kind = IsHoleyElementsKind(elements_kind) |
| 11222 | ? FAST_HOLEY_DOUBLE_ELEMENTS |
| 11223 | : FAST_DOUBLE_ELEMENTS; |
| 11224 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11225 | MaybeObject* maybe_failure = UpdateAllocationSiteInfo(to_kind); |
| 11226 | if (maybe_failure->IsFailure()) return maybe_failure; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11227 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11228 | MaybeObject* maybe = |
| 11229 | SetFastDoubleElementsCapacityAndLength(new_capacity, array_length); |
| 11230 | if (maybe->IsFailure()) return maybe; |
| 11231 | FixedDoubleArray::cast(elements())->set(index, value->Number()); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11232 | ValidateElements(); |
ager@chromium.org | bdf2f94 | 2008-10-17 07:23:00 +0000 | [diff] [blame] | 11233 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11234 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11235 | // Change elements kind from Smi-only to generic FAST if necessary. |
| 11236 | if (HasFastSmiElements() && !value->IsSmi()) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11237 | Map* new_map; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11238 | ElementsKind kind = HasFastHoleyElements() |
| 11239 | ? FAST_HOLEY_ELEMENTS |
| 11240 | : FAST_ELEMENTS; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11241 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11242 | MaybeObject* maybe_failure = UpdateAllocationSiteInfo(kind); |
| 11243 | if (maybe_failure->IsFailure()) return maybe_failure; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11244 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11245 | MaybeObject* maybe_new_map = GetElementsTransitionMap(GetIsolate(), |
| 11246 | kind); |
| 11247 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 11248 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11249 | set_map(new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11250 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11251 | // Increase backing store capacity if that's been decided previously. |
| 11252 | if (new_capacity != capacity) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11253 | FixedArray* new_elements; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11254 | SetFastElementsCapacitySmiMode smi_mode = |
| 11255 | value->IsSmi() && HasFastSmiElements() |
| 11256 | ? kAllowSmiElements |
| 11257 | : kDontAllowSmiElements; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11258 | { MaybeObject* maybe = |
| 11259 | SetFastElementsCapacityAndLength(new_capacity, |
| 11260 | array_length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11261 | smi_mode); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11262 | if (!maybe->To(&new_elements)) return maybe; |
| 11263 | } |
| 11264 | new_elements->set(index, value); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11265 | ValidateElements(); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11266 | return value; |
| 11267 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11268 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 11269 | // Finally, set the new element and length. |
| 11270 | ASSERT(elements()->IsFixedArray()); |
| 11271 | backing_store->set(index, value); |
| 11272 | if (must_update_array_length) { |
| 11273 | JSArray::cast(this)->set_length(Smi::FromInt(array_length)); |
| 11274 | } |
| 11275 | return value; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11276 | } |
| 11277 | |
| 11278 | |
| 11279 | MaybeObject* JSObject::SetDictionaryElement(uint32_t index, |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11280 | Object* value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11281 | PropertyAttributes attributes, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11282 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11283 | bool check_prototype, |
| 11284 | SetPropertyMode set_mode) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11285 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
| 11286 | Isolate* isolate = GetIsolate(); |
| 11287 | Heap* heap = isolate->heap(); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11288 | Handle<JSObject> self(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11289 | Handle<Object> value(value_raw, isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11290 | |
| 11291 | // Insert element in the dictionary. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11292 | Handle<FixedArray> elements(FixedArray::cast(this->elements())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11293 | bool is_arguments = |
| 11294 | (elements->map() == heap->non_strict_arguments_elements_map()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11295 | Handle<SeededNumberDictionary> dictionary(is_arguments |
| 11296 | ? SeededNumberDictionary::cast(elements->get(1)) |
| 11297 | : SeededNumberDictionary::cast(*elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11298 | |
| 11299 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11300 | if (entry != SeededNumberDictionary::kNotFound) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11301 | Object* element = dictionary->ValueAt(entry); |
| 11302 | PropertyDetails details = dictionary->DetailsAt(entry); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11303 | if (details.type() == CALLBACKS && set_mode == SET_PROPERTY) { |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11304 | return SetElementWithCallback(element, index, *value, this, strict_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11305 | } else { |
| 11306 | dictionary->UpdateMaxNumberKey(index); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11307 | // If a value has not been initialized we allow writing to it even if it |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11308 | // is read-only (a declared const that has not been initialized). If a |
| 11309 | // value is being defined we skip attribute checks completely. |
| 11310 | if (set_mode == DEFINE_PROPERTY) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 11311 | details = PropertyDetails( |
| 11312 | attributes, NORMAL, details.dictionary_index()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11313 | dictionary->DetailsAtPut(entry, details); |
| 11314 | } else if (details.IsReadOnly() && !element->IsTheHole()) { |
| 11315 | if (strict_mode == kNonStrictMode) { |
| 11316 | return isolate->heap()->undefined_value(); |
| 11317 | } else { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11318 | Handle<Object> holder(this, isolate); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11319 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 11320 | Handle<Object> args[2] = { number, holder }; |
| 11321 | Handle<Object> error = |
| 11322 | isolate->factory()->NewTypeError("strict_read_only_property", |
| 11323 | HandleVector(args, 2)); |
| 11324 | return isolate->Throw(*error); |
| 11325 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11326 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11327 | // Elements of the arguments object in slow mode might be slow aliases. |
| 11328 | if (is_arguments && element->IsAliasedArgumentsEntry()) { |
| 11329 | AliasedArgumentsEntry* entry = AliasedArgumentsEntry::cast(element); |
| 11330 | Context* context = Context::cast(elements->get(0)); |
| 11331 | int context_index = entry->aliased_context_slot(); |
| 11332 | ASSERT(!context->get(context_index)->IsTheHole()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11333 | context->set(context_index, *value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11334 | // For elements that are still writable we keep slow aliasing. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11335 | if (!details.IsReadOnly()) value = handle(element, isolate); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11336 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11337 | dictionary->ValueAtPut(entry, *value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11338 | } |
| 11339 | } else { |
| 11340 | // Index not already used. Look for an accessor in the prototype chain. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11341 | // Can cause GC! |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11342 | if (check_prototype) { |
| 11343 | bool found; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11344 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes( |
| 11345 | index, *value, &found, strict_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11346 | if (found) return result; |
| 11347 | } |
| 11348 | // When we set the is_extensible flag to false we always force the |
| 11349 | // element into dictionary mode (and force them to stay there). |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11350 | if (!self->map()->is_extensible()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11351 | if (strict_mode == kNonStrictMode) { |
| 11352 | return isolate->heap()->undefined_value(); |
| 11353 | } else { |
| 11354 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 11355 | Handle<String> name = isolate->factory()->NumberToString(number); |
| 11356 | Handle<Object> args[1] = { name }; |
| 11357 | Handle<Object> error = |
| 11358 | isolate->factory()->NewTypeError("object_not_extensible", |
| 11359 | HandleVector(args, 1)); |
| 11360 | return isolate->Throw(*error); |
| 11361 | } |
| 11362 | } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11363 | FixedArrayBase* new_dictionary; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 11364 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11365 | MaybeObject* maybe = dictionary->AddNumberEntry(index, *value, details); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11366 | if (!maybe->To(&new_dictionary)) return maybe; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11367 | if (*dictionary != SeededNumberDictionary::cast(new_dictionary)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11368 | if (is_arguments) { |
| 11369 | elements->set(1, new_dictionary); |
| 11370 | } else { |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11371 | self->set_elements(new_dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11372 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11373 | dictionary = |
| 11374 | handle(SeededNumberDictionary::cast(new_dictionary), isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11375 | } |
| 11376 | } |
| 11377 | |
| 11378 | // Update the array length if this JSObject is an array. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11379 | if (self->IsJSArray()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11380 | MaybeObject* result = |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11381 | JSArray::cast(*self)->JSArrayUpdateLengthFromIndex(index, *value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11382 | if (result->IsFailure()) return result; |
| 11383 | } |
| 11384 | |
| 11385 | // Attempt to put this object back in fast case. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11386 | if (self->ShouldConvertToFastElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11387 | uint32_t new_length = 0; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11388 | if (self->IsJSArray()) { |
| 11389 | CHECK(JSArray::cast(*self)->length()->ToArrayIndex(&new_length)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11390 | } else { |
| 11391 | new_length = dictionary->max_number_key() + 1; |
| 11392 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11393 | SetFastElementsCapacitySmiMode smi_mode = FLAG_smi_only_arrays |
| 11394 | ? kAllowSmiElements |
| 11395 | : kDontAllowSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 11396 | bool has_smi_only_elements = false; |
| 11397 | bool should_convert_to_fast_double_elements = |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11398 | self->ShouldConvertToFastDoubleElements(&has_smi_only_elements); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 11399 | if (has_smi_only_elements) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11400 | smi_mode = kForceSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 11401 | } |
| 11402 | MaybeObject* result = should_convert_to_fast_double_elements |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11403 | ? self->SetFastDoubleElementsCapacityAndLength(new_length, new_length) |
| 11404 | : self->SetFastElementsCapacityAndLength( |
| 11405 | new_length, new_length, smi_mode); |
| 11406 | self->ValidateElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11407 | if (result->IsFailure()) return result; |
| 11408 | #ifdef DEBUG |
| 11409 | if (FLAG_trace_normalization) { |
| 11410 | PrintF("Object elements are fast case again:\n"); |
| 11411 | Print(); |
| 11412 | } |
| 11413 | #endif |
| 11414 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 11415 | return *value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11416 | } |
| 11417 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 11418 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11419 | MUST_USE_RESULT MaybeObject* JSObject::SetFastDoubleElement( |
| 11420 | uint32_t index, |
| 11421 | Object* value, |
| 11422 | StrictModeFlag strict_mode, |
| 11423 | bool check_prototype) { |
| 11424 | ASSERT(HasFastDoubleElements()); |
| 11425 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 11426 | FixedArrayBase* base_elms = FixedArrayBase::cast(elements()); |
| 11427 | uint32_t elms_length = static_cast<uint32_t>(base_elms->length()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11428 | |
| 11429 | // If storing to an element that isn't in the array, pass the store request |
| 11430 | // up the prototype chain before storing in the receiver's elements. |
| 11431 | if (check_prototype && |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 11432 | (index >= elms_length || |
| 11433 | FixedDoubleArray::cast(base_elms)->is_the_hole(index))) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11434 | bool found; |
| 11435 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index, |
| 11436 | value, |
| 11437 | &found, |
| 11438 | strict_mode); |
| 11439 | if (found) return result; |
| 11440 | } |
| 11441 | |
| 11442 | // If the value object is not a heap number, switch to fast elements and try |
| 11443 | // again. |
| 11444 | bool value_is_smi = value->IsSmi(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11445 | bool introduces_holes = true; |
| 11446 | uint32_t length = elms_length; |
| 11447 | if (IsJSArray()) { |
| 11448 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
| 11449 | introduces_holes = index > length; |
| 11450 | } else { |
| 11451 | introduces_holes = index >= elms_length; |
| 11452 | } |
| 11453 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11454 | if (!value->IsNumber()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11455 | MaybeObject* maybe_obj = SetFastElementsCapacityAndLength( |
| 11456 | elms_length, |
| 11457 | length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11458 | kDontAllowSmiElements); |
| 11459 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11460 | maybe_obj = SetFastElement(index, value, strict_mode, check_prototype); |
| 11461 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11462 | ValidateElements(); |
| 11463 | return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11464 | } |
| 11465 | |
| 11466 | double double_value = value_is_smi |
| 11467 | ? static_cast<double>(Smi::cast(value)->value()) |
| 11468 | : HeapNumber::cast(value)->value(); |
| 11469 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11470 | // If the array is growing, and it's not growth by a single element at the |
| 11471 | // end, make sure that the ElementsKind is HOLEY. |
| 11472 | ElementsKind elements_kind = GetElementsKind(); |
| 11473 | if (introduces_holes && !IsFastHoleyElementsKind(elements_kind)) { |
| 11474 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
| 11475 | MaybeObject* maybe = TransitionElementsKind(transitioned_kind); |
| 11476 | if (maybe->IsFailure()) return maybe; |
| 11477 | } |
| 11478 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11479 | // Check whether there is extra space in the fixed array. |
| 11480 | if (index < elms_length) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 11481 | FixedDoubleArray* elms = FixedDoubleArray::cast(elements()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11482 | elms->set(index, double_value); |
| 11483 | if (IsJSArray()) { |
| 11484 | // Update the length of the array if needed. |
| 11485 | uint32_t array_length = 0; |
| 11486 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
| 11487 | if (index >= array_length) { |
| 11488 | JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
| 11489 | } |
| 11490 | } |
| 11491 | return value; |
| 11492 | } |
| 11493 | |
| 11494 | // Allow gap in fast case. |
| 11495 | if ((index - elms_length) < kMaxGap) { |
| 11496 | // Try allocating extra space. |
| 11497 | int new_capacity = NewElementsCapacity(index+1); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 11498 | if (!ShouldConvertToSlowElements(new_capacity)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11499 | ASSERT(static_cast<uint32_t>(new_capacity) > index); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11500 | MaybeObject* maybe_obj = |
| 11501 | SetFastDoubleElementsCapacityAndLength(new_capacity, index + 1); |
| 11502 | if (maybe_obj->IsFailure()) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11503 | FixedDoubleArray::cast(elements())->set(index, double_value); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11504 | ValidateElements(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11505 | return value; |
| 11506 | } |
| 11507 | } |
| 11508 | |
| 11509 | // Otherwise default to slow case. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11510 | ASSERT(HasFastDoubleElements()); |
| 11511 | ASSERT(map()->has_fast_double_elements()); |
| 11512 | ASSERT(elements()->IsFixedDoubleArray()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11513 | Object* obj; |
| 11514 | { MaybeObject* maybe_obj = NormalizeElements(); |
| 11515 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 11516 | } |
| 11517 | ASSERT(HasDictionaryElements()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11518 | return SetElement(index, value, NONE, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11519 | } |
| 11520 | |
| 11521 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11522 | MaybeObject* JSReceiver::SetElement(uint32_t index, |
| 11523 | Object* value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11524 | PropertyAttributes attributes, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11525 | StrictModeFlag strict_mode, |
| 11526 | bool check_proto) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11527 | if (IsJSProxy()) { |
| 11528 | return JSProxy::cast(this)->SetElementWithHandler( |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 11529 | this, index, value, strict_mode); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11530 | } else { |
| 11531 | return JSObject::cast(this)->SetElement( |
| 11532 | index, value, attributes, strict_mode, check_proto); |
| 11533 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11534 | } |
| 11535 | |
| 11536 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11537 | Handle<Object> JSObject::SetOwnElement(Handle<JSObject> object, |
| 11538 | uint32_t index, |
| 11539 | Handle<Object> value, |
| 11540 | StrictModeFlag strict_mode) { |
| 11541 | ASSERT(!object->HasExternalArrayElements()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11542 | CALL_HEAP_FUNCTION( |
| 11543 | object->GetIsolate(), |
| 11544 | object->SetElement(index, *value, NONE, strict_mode, false), |
| 11545 | Object); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11546 | } |
| 11547 | |
| 11548 | |
| 11549 | Handle<Object> JSObject::SetElement(Handle<JSObject> object, |
| 11550 | uint32_t index, |
| 11551 | Handle<Object> value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11552 | PropertyAttributes attr, |
| 11553 | StrictModeFlag strict_mode, |
| 11554 | SetPropertyMode set_mode) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11555 | if (object->HasExternalArrayElements()) { |
| 11556 | if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) { |
| 11557 | bool has_exception; |
| 11558 | Handle<Object> number = Execution::ToNumber(value, &has_exception); |
| 11559 | if (has_exception) return Handle<Object>(); |
| 11560 | value = number; |
| 11561 | } |
| 11562 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11563 | CALL_HEAP_FUNCTION( |
| 11564 | object->GetIsolate(), |
| 11565 | object->SetElement(index, *value, attr, strict_mode, true, set_mode), |
| 11566 | Object); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11567 | } |
| 11568 | |
| 11569 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11570 | MaybeObject* JSObject::SetElement(uint32_t index, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11571 | Object* value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11572 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11573 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11574 | bool check_prototype, |
| 11575 | SetPropertyMode set_mode) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11576 | Isolate* isolate = GetIsolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11577 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11578 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11579 | if (IsAccessCheckNeeded()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11580 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_SET)) { |
| 11581 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 11582 | return value_raw; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11583 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11584 | } |
| 11585 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 11586 | if (IsJSGlobalProxy()) { |
| 11587 | Object* proto = GetPrototype(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11588 | if (proto->IsNull()) return value_raw; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 11589 | ASSERT(proto->IsJSGlobalObject()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11590 | return JSObject::cast(proto)->SetElement(index, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11591 | value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11592 | attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11593 | strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11594 | check_prototype, |
| 11595 | set_mode); |
| 11596 | } |
| 11597 | |
| 11598 | // Don't allow element properties to be redefined for external arrays. |
| 11599 | if (HasExternalArrayElements() && set_mode == DEFINE_PROPERTY) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11600 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11601 | Handle<Object> args[] = { handle(this, isolate), number }; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11602 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 11603 | "redef_external_array_element", HandleVector(args, ARRAY_SIZE(args))); |
| 11604 | return isolate->Throw(*error); |
| 11605 | } |
| 11606 | |
| 11607 | // Normalize the elements to enable attributes on the property. |
| 11608 | if ((attributes & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) { |
| 11609 | SeededNumberDictionary* dictionary; |
| 11610 | MaybeObject* maybe_object = NormalizeElements(); |
| 11611 | if (!maybe_object->To(&dictionary)) return maybe_object; |
| 11612 | // Make sure that we never go back to fast case. |
| 11613 | dictionary->set_requires_slow_elements(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 11614 | } |
| 11615 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11616 | if (!(FLAG_harmony_observation && map()->is_observed())) { |
| 11617 | return HasIndexedInterceptor() |
| 11618 | ? SetElementWithInterceptor( |
| 11619 | index, value_raw, attributes, strict_mode, check_prototype, set_mode) |
| 11620 | : SetElementWithoutInterceptor( |
| 11621 | index, value_raw, attributes, strict_mode, check_prototype, set_mode); |
| 11622 | } |
| 11623 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11624 | // From here on, everything has to be handlified. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11625 | Handle<JSObject> self(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11626 | Handle<Object> value(value_raw, isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11627 | PropertyAttributes old_attributes = self->GetLocalElementAttribute(index); |
| 11628 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
| 11629 | Handle<Object> old_length; |
| 11630 | |
| 11631 | if (old_attributes != ABSENT) { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11632 | if (self->GetLocalElementAccessorPair(index) == NULL) |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11633 | old_value = Object::GetElement(self, index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11634 | } else if (self->IsJSArray()) { |
| 11635 | // Store old array length in case adding an element grows the array. |
| 11636 | old_length = handle(Handle<JSArray>::cast(self)->length(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11637 | } |
| 11638 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11639 | // Check for lookup interceptor |
| 11640 | MaybeObject* result = self->HasIndexedInterceptor() |
| 11641 | ? self->SetElementWithInterceptor( |
| 11642 | index, *value, attributes, strict_mode, check_prototype, set_mode) |
| 11643 | : self->SetElementWithoutInterceptor( |
| 11644 | index, *value, attributes, strict_mode, check_prototype, set_mode); |
| 11645 | |
| 11646 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11647 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11648 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11649 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 11650 | PropertyAttributes new_attributes = self->GetLocalElementAttribute(index); |
| 11651 | if (old_attributes == ABSENT) { |
| 11652 | EnqueueChangeRecord(self, "new", name, old_value); |
| 11653 | if (self->IsJSArray() && |
| 11654 | !old_length->SameValue(Handle<JSArray>::cast(self)->length())) { |
| 11655 | EnqueueChangeRecord( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11656 | self, "updated", isolate->factory()->length_string(), old_length); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11657 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 11658 | } else if (old_value->IsTheHole()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11659 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 11660 | } else { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 11661 | Handle<Object> new_value = Object::GetElement(self, index); |
| 11662 | bool value_changed = !old_value->SameValue(*new_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 11663 | if (old_attributes != new_attributes) { |
| 11664 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
| 11665 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
| 11666 | } else if (value_changed) { |
| 11667 | EnqueueChangeRecord(self, "updated", name, old_value); |
| 11668 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 11669 | } |
| 11670 | |
| 11671 | return *hresult; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
| 11674 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11675 | MaybeObject* JSObject::SetElementWithoutInterceptor(uint32_t index, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11676 | Object* value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11677 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11678 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11679 | bool check_prototype, |
| 11680 | SetPropertyMode set_mode) { |
| 11681 | ASSERT(HasDictionaryElements() || |
| 11682 | HasDictionaryArgumentsElements() || |
| 11683 | (attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11684 | Isolate* isolate = GetIsolate(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11685 | if (FLAG_trace_external_array_abuse && |
| 11686 | IsExternalArrayElementsKind(GetElementsKind())) { |
| 11687 | CheckArrayAbuse(this, "external elements write", index); |
| 11688 | } |
| 11689 | if (FLAG_trace_js_array_abuse && |
| 11690 | !IsExternalArrayElementsKind(GetElementsKind())) { |
| 11691 | if (IsJSArray()) { |
| 11692 | CheckArrayAbuse(this, "elements write", index, true); |
| 11693 | } |
| 11694 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11695 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11696 | case FAST_SMI_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11697 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11698 | case FAST_HOLEY_SMI_ELEMENTS: |
| 11699 | case FAST_HOLEY_ELEMENTS: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11700 | return SetFastElement(index, value, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11701 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11702 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11703 | return SetFastDoubleElement(index, value, strict_mode, check_prototype); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 11704 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 11705 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11706 | return pixels->SetValue(index, value); |
| 11707 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11708 | case EXTERNAL_BYTE_ELEMENTS: { |
| 11709 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 11710 | return array->SetValue(index, value); |
| 11711 | } |
| 11712 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 11713 | ExternalUnsignedByteArray* array = |
| 11714 | ExternalUnsignedByteArray::cast(elements()); |
| 11715 | return array->SetValue(index, value); |
| 11716 | } |
| 11717 | case EXTERNAL_SHORT_ELEMENTS: { |
| 11718 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 11719 | return array->SetValue(index, value); |
| 11720 | } |
| 11721 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 11722 | ExternalUnsignedShortArray* array = |
| 11723 | ExternalUnsignedShortArray::cast(elements()); |
| 11724 | return array->SetValue(index, value); |
| 11725 | } |
| 11726 | case EXTERNAL_INT_ELEMENTS: { |
| 11727 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 11728 | return array->SetValue(index, value); |
| 11729 | } |
| 11730 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 11731 | ExternalUnsignedIntArray* array = |
| 11732 | ExternalUnsignedIntArray::cast(elements()); |
| 11733 | return array->SetValue(index, value); |
| 11734 | } |
| 11735 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 11736 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 11737 | return array->SetValue(index, value); |
| 11738 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11739 | case EXTERNAL_DOUBLE_ELEMENTS: { |
| 11740 | ExternalDoubleArray* array = ExternalDoubleArray::cast(elements()); |
| 11741 | return array->SetValue(index, value); |
| 11742 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11743 | case DICTIONARY_ELEMENTS: |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11744 | return SetDictionaryElement(index, value, attr, strict_mode, |
| 11745 | check_prototype, set_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11746 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 11747 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 11748 | uint32_t length = parameter_map->length(); |
| 11749 | Object* probe = |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 11750 | (index < length - 2) ? parameter_map->get(index + 2) : NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11751 | if (probe != NULL && !probe->IsTheHole()) { |
| 11752 | Context* context = Context::cast(parameter_map->get(0)); |
| 11753 | int context_index = Smi::cast(probe)->value(); |
| 11754 | ASSERT(!context->get(context_index)->IsTheHole()); |
| 11755 | context->set(context_index, value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11756 | // Redefining attributes of an aliased element destroys fast aliasing. |
| 11757 | if (set_mode == SET_PROPERTY || attr == NONE) return value; |
| 11758 | parameter_map->set_the_hole(index + 2); |
| 11759 | // For elements that are still writable we re-establish slow aliasing. |
| 11760 | if ((attr & READ_ONLY) == 0) { |
| 11761 | MaybeObject* maybe_entry = |
| 11762 | isolate->heap()->AllocateAliasedArgumentsEntry(context_index); |
| 11763 | if (!maybe_entry->ToObject(&value)) return maybe_entry; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11764 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11765 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11766 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 11767 | if (arguments->IsDictionary()) { |
| 11768 | return SetDictionaryElement(index, value, attr, strict_mode, |
| 11769 | check_prototype, set_mode); |
| 11770 | } else { |
| 11771 | return SetFastElement(index, value, strict_mode, check_prototype); |
| 11772 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11773 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11774 | } |
| 11775 | // All possible cases have been handled above. Add a return to avoid the |
| 11776 | // complaints from the compiler. |
| 11777 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11778 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11779 | } |
| 11780 | |
| 11781 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11782 | Handle<Object> JSObject::TransitionElementsKind(Handle<JSObject> object, |
| 11783 | ElementsKind to_kind) { |
| 11784 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 11785 | object->TransitionElementsKind(to_kind), |
| 11786 | Object); |
| 11787 | } |
| 11788 | |
| 11789 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11790 | MaybeObject* JSObject::UpdateAllocationSiteInfo(ElementsKind to_kind) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11791 | if (!FLAG_track_allocation_sites || !IsJSArray()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11792 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11793 | } |
| 11794 | |
| 11795 | AllocationSiteInfo* info = AllocationSiteInfo::FindForJSObject(this); |
| 11796 | if (info == NULL) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11797 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11798 | } |
| 11799 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11800 | if (info->payload()->IsJSArray()) { |
| 11801 | JSArray* payload = JSArray::cast(info->payload()); |
| 11802 | ElementsKind kind = payload->GetElementsKind(); |
| 11803 | if (AllocationSiteInfo::GetMode(kind, to_kind) == TRACK_ALLOCATION_SITE) { |
| 11804 | // If the array is huge, it's not likely to be defined in a local |
| 11805 | // function, so we shouldn't make new instances of it very often. |
| 11806 | uint32_t length = 0; |
| 11807 | CHECK(payload->length()->ToArrayIndex(&length)); |
| 11808 | if (length <= AllocationSiteInfo::kMaximumArrayBytesToPretransition) { |
| 11809 | if (FLAG_trace_track_allocation_sites) { |
| 11810 | PrintF( |
| 11811 | "AllocationSiteInfo: JSArray %p boilerplate updated %s->%s\n", |
| 11812 | reinterpret_cast<void*>(this), |
| 11813 | ElementsKindToString(kind), |
| 11814 | ElementsKindToString(to_kind)); |
| 11815 | } |
| 11816 | return payload->TransitionElementsKind(to_kind); |
| 11817 | } |
| 11818 | } |
| 11819 | } else if (info->payload()->IsJSGlobalPropertyCell()) { |
| 11820 | JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(info->payload()); |
| 11821 | Object* cell_contents = cell->value(); |
| 11822 | if (cell_contents->IsSmi()) { |
| 11823 | ElementsKind kind = static_cast<ElementsKind>( |
| 11824 | Smi::cast(cell_contents)->value()); |
| 11825 | if (AllocationSiteInfo::GetMode(kind, to_kind) == TRACK_ALLOCATION_SITE) { |
| 11826 | if (FLAG_trace_track_allocation_sites) { |
| 11827 | PrintF("AllocationSiteInfo: JSArray %p info updated %s->%s\n", |
| 11828 | reinterpret_cast<void*>(this), |
| 11829 | ElementsKindToString(kind), |
| 11830 | ElementsKindToString(to_kind)); |
| 11831 | } |
| 11832 | cell->set_value(Smi::FromInt(to_kind)); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11833 | } |
| 11834 | } |
| 11835 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11836 | return this; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
| 11839 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11840 | MaybeObject* JSObject::TransitionElementsKind(ElementsKind to_kind) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 11841 | ASSERT(!map()->is_observed()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11842 | ElementsKind from_kind = map()->elements_kind(); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11843 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11844 | if (IsFastHoleyElementsKind(from_kind)) { |
| 11845 | to_kind = GetHoleyElementsKind(to_kind); |
| 11846 | } |
| 11847 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11848 | if (from_kind == to_kind) return this; |
| 11849 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11850 | MaybeObject* maybe_failure = UpdateAllocationSiteInfo(to_kind); |
| 11851 | if (maybe_failure->IsFailure()) return maybe_failure; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11852 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11853 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11854 | if (elements() == isolate->heap()->empty_fixed_array() || |
| 11855 | (IsFastSmiOrObjectElementsKind(from_kind) && |
| 11856 | IsFastSmiOrObjectElementsKind(to_kind)) || |
| 11857 | (from_kind == FAST_DOUBLE_ELEMENTS && |
| 11858 | to_kind == FAST_HOLEY_DOUBLE_ELEMENTS)) { |
| 11859 | ASSERT(from_kind != TERMINAL_FAST_ELEMENTS_KIND); |
| 11860 | // No change is needed to the elements() buffer, the transition |
| 11861 | // only requires a map change. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11862 | MaybeObject* maybe_new_map = GetElementsTransitionMap(isolate, to_kind); |
| 11863 | Map* new_map; |
| 11864 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 11865 | set_map(new_map); |
| 11866 | if (FLAG_trace_elements_transitions) { |
| 11867 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 11868 | PrintElementsTransition(stdout, from_kind, elms, to_kind, elms); |
| 11869 | } |
| 11870 | return this; |
| 11871 | } |
| 11872 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11873 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 11874 | uint32_t capacity = static_cast<uint32_t>(elms->length()); |
| 11875 | uint32_t length = capacity; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11876 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11877 | if (IsJSArray()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11878 | Object* raw_length = JSArray::cast(this)->length(); |
| 11879 | if (raw_length->IsUndefined()) { |
| 11880 | // If length is undefined, then JSArray is being initialized and has no |
| 11881 | // elements, assume a length of zero. |
| 11882 | length = 0; |
| 11883 | } else { |
| 11884 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11885 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11886 | } |
| 11887 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11888 | if (IsFastSmiElementsKind(from_kind) && |
| 11889 | IsFastDoubleElementsKind(to_kind)) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11890 | MaybeObject* maybe_result = |
| 11891 | SetFastDoubleElementsCapacityAndLength(capacity, length); |
| 11892 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11893 | ValidateElements(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11894 | return this; |
| 11895 | } |
| 11896 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11897 | if (IsFastDoubleElementsKind(from_kind) && |
| 11898 | IsFastObjectElementsKind(to_kind)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11899 | MaybeObject* maybe_result = SetFastElementsCapacityAndLength( |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11900 | capacity, length, kDontAllowSmiElements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11901 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11902 | ValidateElements(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11903 | return this; |
| 11904 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11905 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11906 | // This method should never be called for any other case than the ones |
| 11907 | // handled above. |
| 11908 | UNREACHABLE(); |
| 11909 | return GetIsolate()->heap()->null_value(); |
| 11910 | } |
| 11911 | |
| 11912 | |
| 11913 | // static |
| 11914 | bool Map::IsValidElementsTransition(ElementsKind from_kind, |
| 11915 | ElementsKind to_kind) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11916 | // Transitions can't go backwards. |
| 11917 | if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) { |
| 11918 | return false; |
| 11919 | } |
| 11920 | |
| 11921 | // Transitions from HOLEY -> PACKED are not allowed. |
| 11922 | return !IsFastHoleyElementsKind(from_kind) || |
| 11923 | IsFastHoleyElementsKind(to_kind); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11924 | } |
| 11925 | |
| 11926 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11927 | MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, |
| 11928 | Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11929 | uint32_t old_len = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 11930 | CHECK(length()->ToArrayIndex(&old_len)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11931 | // Check to see if we need to update the length. For now, we make |
| 11932 | // sure that the length stays within 32-bits (unsigned). |
| 11933 | if (index >= old_len && index != 0xffffffff) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11934 | Object* len; |
| 11935 | { MaybeObject* maybe_len = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11936 | GetHeap()->NumberFromDouble(static_cast<double>(index) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11937 | if (!maybe_len->ToObject(&len)) return maybe_len; |
| 11938 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11939 | set_length(len); |
| 11940 | } |
| 11941 | return value; |
| 11942 | } |
| 11943 | |
| 11944 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 11945 | MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11946 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11947 | Isolate* isolate = GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11948 | // Make sure that the top context does not change when doing |
| 11949 | // callbacks or interceptor calls. |
| 11950 | AssertNoContextChange ncc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11951 | HandleScope scope(isolate); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 11952 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate); |
| 11953 | Handle<Object> this_handle(receiver, isolate); |
| 11954 | Handle<JSObject> holder_handle(this, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11955 | if (!interceptor->getter()->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11956 | v8::IndexedPropertyGetter getter = |
| 11957 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11958 | LOG(isolate, |
| 11959 | ApiIndexedPropertyAccess("interceptor-indexed-get", this, index)); |
| 11960 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 11961 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11962 | v8::Handle<v8::Value> result; |
| 11963 | { |
| 11964 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 11965 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11966 | result = getter(index, info); |
| 11967 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11968 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 11969 | if (!result.IsEmpty()) { |
| 11970 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 11971 | result_internal->VerifyApiCallResultType(); |
| 11972 | return *result_internal; |
| 11973 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11974 | } |
| 11975 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11976 | Heap* heap = holder_handle->GetHeap(); |
| 11977 | ElementsAccessor* handler = holder_handle->GetElementsAccessor(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11978 | MaybeObject* raw_result = handler->Get(*this_handle, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 11979 | *holder_handle, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11980 | index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11981 | if (raw_result != heap->the_hole_value()) return raw_result; |
| 11982 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11983 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11984 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11985 | Object* pt = holder_handle->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11986 | if (pt == heap->null_value()) return heap->undefined_value(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11987 | return pt->GetElementWithReceiver(*this_handle, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11988 | } |
| 11989 | |
| 11990 | |
| 11991 | bool JSObject::HasDenseElements() { |
| 11992 | int capacity = 0; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 11993 | int used = 0; |
| 11994 | GetElementsCapacityAndUsage(&capacity, &used); |
| 11995 | return (capacity == 0) || (used > (capacity / 2)); |
| 11996 | } |
| 11997 | |
| 11998 | |
| 11999 | void JSObject::GetElementsCapacityAndUsage(int* capacity, int* used) { |
| 12000 | *capacity = 0; |
| 12001 | *used = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12002 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12003 | FixedArrayBase* backing_store_base = FixedArrayBase::cast(elements()); |
| 12004 | FixedArray* backing_store = NULL; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12005 | switch (GetElementsKind()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12006 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12007 | backing_store_base = |
| 12008 | FixedArray::cast(FixedArray::cast(backing_store_base)->get(1)); |
| 12009 | backing_store = FixedArray::cast(backing_store_base); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12010 | if (backing_store->IsDictionary()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12011 | SeededNumberDictionary* dictionary = |
| 12012 | SeededNumberDictionary::cast(backing_store); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12013 | *capacity = dictionary->Capacity(); |
| 12014 | *used = dictionary->NumberOfElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12015 | break; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12016 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12017 | // Fall through. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12018 | case FAST_SMI_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12019 | case FAST_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12020 | if (IsJSArray()) { |
| 12021 | *capacity = backing_store_base->length(); |
| 12022 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 12023 | break; |
| 12024 | } |
| 12025 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12026 | case FAST_HOLEY_SMI_ELEMENTS: |
| 12027 | case FAST_HOLEY_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12028 | backing_store = FixedArray::cast(backing_store_base); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12029 | *capacity = backing_store->length(); |
| 12030 | for (int i = 0; i < *capacity; ++i) { |
| 12031 | if (!backing_store->get(i)->IsTheHole()) ++(*used); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12032 | } |
| 12033 | break; |
| 12034 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12035 | SeededNumberDictionary* dictionary = |
| 12036 | SeededNumberDictionary::cast(FixedArray::cast(elements())); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12037 | *capacity = dictionary->Capacity(); |
| 12038 | *used = dictionary->NumberOfElements(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12039 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12040 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12041 | case FAST_DOUBLE_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12042 | if (IsJSArray()) { |
| 12043 | *capacity = backing_store_base->length(); |
| 12044 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 12045 | break; |
| 12046 | } |
| 12047 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12048 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12049 | FixedDoubleArray* elms = FixedDoubleArray::cast(elements()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12050 | *capacity = elms->length(); |
| 12051 | for (int i = 0; i < *capacity; i++) { |
| 12052 | if (!elms->is_the_hole(i)) ++(*used); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12053 | } |
| 12054 | break; |
| 12055 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12056 | case EXTERNAL_BYTE_ELEMENTS: |
| 12057 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 12058 | case EXTERNAL_SHORT_ELEMENTS: |
| 12059 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 12060 | case EXTERNAL_INT_ELEMENTS: |
| 12061 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12062 | case EXTERNAL_FLOAT_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12063 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 12064 | case EXTERNAL_PIXEL_ELEMENTS: |
| 12065 | // External arrays are considered 100% used. |
| 12066 | ExternalArray* external_array = ExternalArray::cast(elements()); |
| 12067 | *capacity = external_array->length(); |
| 12068 | *used = external_array->length(); |
| 12069 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12070 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12071 | } |
| 12072 | |
| 12073 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12074 | bool JSObject::ShouldConvertToSlowElements(int new_capacity) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12075 | STATIC_ASSERT(kMaxUncheckedOldFastElementsLength <= |
| 12076 | kMaxUncheckedFastElementsLength); |
| 12077 | if (new_capacity <= kMaxUncheckedOldFastElementsLength || |
| 12078 | (new_capacity <= kMaxUncheckedFastElementsLength && |
| 12079 | GetHeap()->InNewSpace(this))) { |
| 12080 | return false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12081 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12082 | // If the fast-case backing storage takes up roughly three times as |
| 12083 | // much space (in machine words) as a dictionary backing storage |
| 12084 | // would, the object should have slow elements. |
| 12085 | int old_capacity = 0; |
| 12086 | int used_elements = 0; |
| 12087 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12088 | int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) * |
| 12089 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12090 | return 3 * dictionary_size <= new_capacity; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12091 | } |
| 12092 | |
| 12093 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12094 | bool JSObject::ShouldConvertToFastElements() { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12095 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12096 | // If the elements are sparse, we should not go back to fast case. |
| 12097 | if (!HasDenseElements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12098 | // An object requiring access checks is never allowed to have fast |
| 12099 | // elements. If it had fast elements we would skip security checks. |
| 12100 | if (IsAccessCheckNeeded()) return false; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12101 | // Observed objects may not go to fast mode because they rely on map checks, |
| 12102 | // and for fast element accesses we sometimes check element kinds only. |
| 12103 | if (FLAG_harmony_observation && map()->is_observed()) return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12104 | |
| 12105 | FixedArray* elements = FixedArray::cast(this->elements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12106 | SeededNumberDictionary* dictionary = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12107 | if (elements->map() == GetHeap()->non_strict_arguments_elements_map()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12108 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12109 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12110 | dictionary = SeededNumberDictionary::cast(elements); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12111 | } |
| 12112 | // If an element has been added at a very high index in the elements |
| 12113 | // dictionary, we cannot go back to fast case. |
| 12114 | if (dictionary->requires_slow_elements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12115 | // If the dictionary backing storage takes up roughly half as much |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12116 | // space (in machine words) as a fast-case backing storage would, |
| 12117 | // the object should have fast elements. |
| 12118 | uint32_t array_size = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12119 | if (IsJSArray()) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12120 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12121 | } else { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12122 | array_size = dictionary->max_number_key(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12123 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12124 | uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) * |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12125 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12126 | return 2 * dictionary_size >= array_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12127 | } |
| 12128 | |
| 12129 | |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12130 | bool JSObject::ShouldConvertToFastDoubleElements( |
| 12131 | bool* has_smi_only_elements) { |
| 12132 | *has_smi_only_elements = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12133 | if (FLAG_unbox_double_arrays) { |
| 12134 | ASSERT(HasDictionaryElements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12135 | SeededNumberDictionary* dictionary = |
| 12136 | SeededNumberDictionary::cast(elements()); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12137 | bool found_double = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12138 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 12139 | Object* key = dictionary->KeyAt(i); |
| 12140 | if (key->IsNumber()) { |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12141 | Object* value = dictionary->ValueAt(i); |
| 12142 | if (!value->IsNumber()) return false; |
| 12143 | if (!value->IsSmi()) { |
| 12144 | found_double = true; |
| 12145 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12146 | } |
| 12147 | } |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12148 | *has_smi_only_elements = !found_double; |
| 12149 | return found_double; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12150 | } else { |
| 12151 | return false; |
| 12152 | } |
| 12153 | } |
| 12154 | |
| 12155 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12156 | // Certain compilers request function template instantiation when they |
| 12157 | // see the definition of the other template functions in the |
| 12158 | // class. This requires us to have the template functions put |
| 12159 | // together, so even though this function belongs in objects-debug.cc, |
| 12160 | // we keep it here instead to satisfy certain compilers. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12161 | #ifdef OBJECT_PRINT |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12162 | template<typename Shape, typename Key> |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12163 | void Dictionary<Shape, Key>::Print(FILE* out) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12164 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 12165 | for (int i = 0; i < capacity; i++) { |
| 12166 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 12167 | if (HashTable<Shape, Key>::IsKey(k)) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12168 | PrintF(out, " "); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12169 | if (k->IsString()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12170 | String::cast(k)->StringPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12171 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12172 | k->ShortPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12173 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 12174 | PrintF(out, ": "); |
| 12175 | ValueAt(i)->ShortPrint(out); |
| 12176 | PrintF(out, "\n"); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12177 | } |
| 12178 | } |
| 12179 | } |
| 12180 | #endif |
| 12181 | |
| 12182 | |
| 12183 | template<typename Shape, typename Key> |
| 12184 | void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12185 | int pos = 0; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12186 | int capacity = HashTable<Shape, Key>::Capacity(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12187 | AssertNoAllocation no_gc; |
| 12188 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12189 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12190 | Object* k = Dictionary<Shape, Key>::KeyAt(i); |
| 12191 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 12192 | elements->set(pos++, ValueAt(i), mode); |
| 12193 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12194 | } |
| 12195 | ASSERT(pos == elements->length()); |
| 12196 | } |
| 12197 | |
| 12198 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12199 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
| 12200 | ASSERT(map()->has_named_interceptor()); |
| 12201 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12202 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12203 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12204 | constructor->shared()->get_api_func_data()->named_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12205 | return InterceptorInfo::cast(result); |
| 12206 | } |
| 12207 | |
| 12208 | |
| 12209 | InterceptorInfo* JSObject::GetIndexedInterceptor() { |
| 12210 | ASSERT(map()->has_indexed_interceptor()); |
| 12211 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12212 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12213 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12214 | constructor->shared()->get_api_func_data()->indexed_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12215 | return InterceptorInfo::cast(result); |
| 12216 | } |
| 12217 | |
| 12218 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12219 | MaybeObject* JSObject::GetPropertyPostInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12220 | Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12221 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12222 | PropertyAttributes* attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12223 | // Check local property in holder, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12224 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12225 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 12226 | if (result.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 12227 | return GetProperty(receiver, &result, name, attributes); |
| 12228 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12229 | // Continue searching via the prototype chain. |
| 12230 | Object* pt = GetPrototype(); |
| 12231 | *attributes = ABSENT; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12232 | if (pt->IsNull()) return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12233 | return pt->GetPropertyWithReceiver(receiver, name, attributes); |
| 12234 | } |
| 12235 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12236 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12237 | MaybeObject* JSObject::GetLocalPropertyPostInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12238 | Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12239 | Name* name, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12240 | PropertyAttributes* attributes) { |
| 12241 | // Check local property in holder, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12242 | LookupResult result(GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12243 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 12244 | if (result.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 12245 | return GetProperty(receiver, &result, name, attributes); |
| 12246 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12247 | return GetHeap()->undefined_value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12248 | } |
| 12249 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12250 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12251 | MaybeObject* JSObject::GetPropertyWithInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12252 | Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12253 | Name* name, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 12254 | PropertyAttributes* attributes) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12255 | // TODO(rossberg): Support symbols in the API. |
| 12256 | if (name->IsSymbol()) return GetHeap()->undefined_value(); |
| 12257 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12258 | Isolate* isolate = GetIsolate(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12259 | InterceptorInfo* interceptor = GetNamedInterceptor(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12260 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 12261 | Handle<Object> receiver_handle(receiver, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12262 | Handle<JSObject> holder_handle(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12263 | Handle<String> name_handle(String::cast(name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12264 | |
| 12265 | if (!interceptor->getter()->IsUndefined()) { |
| 12266 | v8::NamedPropertyGetter getter = |
| 12267 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12268 | LOG(isolate, |
| 12269 | ApiNamedPropertyAccess("interceptor-named-get", *holder_handle, name)); |
| 12270 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 12271 | v8::AccessorInfo info(args.end()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12272 | v8::Handle<v8::Value> result; |
| 12273 | { |
| 12274 | // Leaving JavaScript. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 12275 | VMState<EXTERNAL> state(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12276 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 12277 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12278 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12279 | if (!result.IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12280 | *attributes = NONE; |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 12281 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 12282 | result_internal->VerifyApiCallResultType(); |
| 12283 | return *result_internal; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12284 | } |
| 12285 | } |
| 12286 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12287 | MaybeObject* result = holder_handle->GetPropertyPostInterceptor( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12288 | *receiver_handle, |
| 12289 | *name_handle, |
| 12290 | attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12291 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 12292 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12293 | } |
| 12294 | |
| 12295 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 12296 | bool JSObject::HasRealNamedProperty(Isolate* isolate, Name* key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12297 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12298 | if (IsAccessCheckNeeded()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12299 | if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 12300 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12301 | return false; |
| 12302 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12303 | } |
| 12304 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12305 | LookupResult result(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12306 | LocalLookupRealNamedProperty(key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 12307 | return result.IsFound() && !result.IsInterceptor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12308 | } |
| 12309 | |
| 12310 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 12311 | bool JSObject::HasRealElementProperty(Isolate* isolate, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12312 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12313 | if (IsAccessCheckNeeded()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 12314 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 12315 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12316 | return false; |
| 12317 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12318 | } |
| 12319 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 12320 | return GetElementAttributeWithoutInterceptor(this, index, false) != ABSENT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12321 | } |
| 12322 | |
| 12323 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 12324 | bool JSObject::HasRealNamedCallbackProperty(Isolate* isolate, Name* key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12325 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12326 | if (IsAccessCheckNeeded()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12327 | if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 12328 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12329 | return false; |
| 12330 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12331 | } |
| 12332 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12333 | LookupResult result(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12334 | LocalLookupRealNamedProperty(key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 12335 | return result.IsPropertyCallbacks(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12336 | } |
| 12337 | |
| 12338 | |
| 12339 | int JSObject::NumberOfLocalProperties(PropertyAttributes filter) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12340 | if (HasFastProperties()) { |
| 12341 | Map* map = this->map(); |
| 12342 | if (filter == NONE) return map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12343 | if (filter & DONT_ENUM) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 12344 | int result = map->EnumLength(); |
| 12345 | if (result != Map::kInvalidEnumCache) return result; |
| 12346 | } |
| 12347 | return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter); |
| 12348 | } |
| 12349 | return property_dictionary()->NumberOfElementsFilterAttributes(filter); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12350 | } |
| 12351 | |
| 12352 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12353 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12354 | Object* temp = get(i); |
| 12355 | set(i, get(j)); |
| 12356 | set(j, temp); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12357 | if (this != numbers) { |
| 12358 | temp = numbers->get(i); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 12359 | numbers->set(i, Smi::cast(numbers->get(j))); |
| 12360 | numbers->set(j, Smi::cast(temp)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12361 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12362 | } |
| 12363 | |
| 12364 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12365 | static void InsertionSortPairs(FixedArray* content, |
| 12366 | FixedArray* numbers, |
| 12367 | int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12368 | for (int i = 1; i < len; i++) { |
| 12369 | int j = i; |
| 12370 | while (j > 0 && |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12371 | (NumberToUint32(numbers->get(j - 1)) > |
| 12372 | NumberToUint32(numbers->get(j)))) { |
| 12373 | content->SwapPairs(numbers, j - 1, j); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12374 | j--; |
| 12375 | } |
| 12376 | } |
| 12377 | } |
| 12378 | |
| 12379 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12380 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12381 | // In-place heap sort. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12382 | ASSERT(content->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12383 | |
| 12384 | // Bottom-up max-heap construction. |
| 12385 | for (int i = 1; i < len; ++i) { |
| 12386 | int child_index = i; |
| 12387 | while (child_index > 0) { |
| 12388 | int parent_index = ((child_index + 1) >> 1) - 1; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12389 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 12390 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12391 | if (parent_value < child_value) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12392 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12393 | } else { |
| 12394 | break; |
| 12395 | } |
| 12396 | child_index = parent_index; |
| 12397 | } |
| 12398 | } |
| 12399 | |
| 12400 | // Extract elements and create sorted array. |
| 12401 | for (int i = len - 1; i > 0; --i) { |
| 12402 | // Put max element at the back of the array. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12403 | content->SwapPairs(numbers, 0, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12404 | // Sift down the new top element. |
| 12405 | int parent_index = 0; |
| 12406 | while (true) { |
| 12407 | int child_index = ((parent_index + 1) << 1) - 1; |
| 12408 | if (child_index >= i) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12409 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 12410 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 12411 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12412 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 12413 | if (parent_value > child1_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12414 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12415 | parent_index = child_index; |
| 12416 | } else { |
| 12417 | if (parent_value > child2_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12418 | content->SwapPairs(numbers, parent_index, child_index + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12419 | parent_index = child_index + 1; |
| 12420 | } |
| 12421 | } |
| 12422 | } |
| 12423 | } |
| 12424 | |
| 12425 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12426 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 12427 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
| 12428 | ASSERT(this->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12429 | // For small arrays, simply use insertion sort. |
| 12430 | if (len <= 10) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12431 | InsertionSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12432 | return; |
| 12433 | } |
| 12434 | // Check the range of indices. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12435 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 12436 | uint32_t max_index = min_index; |
| 12437 | uint32_t i; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12438 | for (i = 1; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12439 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 12440 | min_index = NumberToUint32(numbers->get(i)); |
| 12441 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 12442 | max_index = NumberToUint32(numbers->get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12443 | } |
| 12444 | } |
| 12445 | if (max_index - min_index + 1 == len) { |
| 12446 | // Indices form a contiguous range, unless there are duplicates. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12447 | // Do an in-place linear time sort assuming distinct numbers, but |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12448 | // avoid hanging in case they are not. |
| 12449 | for (i = 0; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12450 | uint32_t p; |
| 12451 | uint32_t j = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12452 | // While the current element at i is not at its correct position p, |
| 12453 | // swap the elements at these two positions. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12454 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12455 | j++ < len) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12456 | SwapPairs(numbers, i, p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12457 | } |
| 12458 | } |
| 12459 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12460 | HeapSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12461 | return; |
| 12462 | } |
| 12463 | } |
| 12464 | |
| 12465 | |
| 12466 | // Fill in the names of local properties into the supplied storage. The main |
| 12467 | // purpose of this function is to provide reflection information for the object |
| 12468 | // mirrors. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 12469 | void JSObject::GetLocalPropertyNames( |
| 12470 | FixedArray* storage, int index, PropertyAttributes filter) { |
| 12471 | ASSERT(storage->length() >= (NumberOfLocalProperties(filter) - index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12472 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 12473 | int real_size = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 12474 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 12475 | for (int i = 0; i < real_size; i++) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 12476 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 12477 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 12478 | storage->set(index++, descs->GetKey(i)); |
| 12479 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12480 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12481 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12482 | property_dictionary()->CopyKeysTo(storage, |
| 12483 | index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 12484 | filter, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12485 | NameDictionary::UNSORTED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12486 | } |
| 12487 | } |
| 12488 | |
| 12489 | |
| 12490 | int JSObject::NumberOfLocalElements(PropertyAttributes filter) { |
| 12491 | return GetLocalElementKeys(NULL, filter); |
| 12492 | } |
| 12493 | |
| 12494 | |
| 12495 | int JSObject::NumberOfEnumElements() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12496 | // Fast case for objects with no elements. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12497 | if (!IsJSValue() && HasFastObjectElements()) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12498 | uint32_t length = IsJSArray() ? |
| 12499 | static_cast<uint32_t>( |
| 12500 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 12501 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 12502 | if (length == 0) return 0; |
| 12503 | } |
| 12504 | // Compute the number of enumerable elements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12505 | return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 12506 | } |
| 12507 | |
| 12508 | |
| 12509 | int JSObject::GetLocalElementKeys(FixedArray* storage, |
| 12510 | PropertyAttributes filter) { |
| 12511 | int counter = 0; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12512 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12513 | case FAST_SMI_ELEMENTS: |
| 12514 | case FAST_ELEMENTS: |
| 12515 | case FAST_HOLEY_SMI_ELEMENTS: |
| 12516 | case FAST_HOLEY_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12517 | int length = IsJSArray() ? |
| 12518 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 12519 | FixedArray::cast(elements())->length(); |
| 12520 | for (int i = 0; i < length; i++) { |
| 12521 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 12522 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12523 | storage->set(counter, Smi::FromInt(i)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12524 | } |
| 12525 | counter++; |
| 12526 | } |
| 12527 | } |
| 12528 | ASSERT(!storage || storage->length() >= counter); |
| 12529 | break; |
| 12530 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12531 | case FAST_DOUBLE_ELEMENTS: |
| 12532 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 12533 | int length = IsJSArray() ? |
| 12534 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 12535 | FixedDoubleArray::cast(elements())->length(); |
| 12536 | for (int i = 0; i < length; i++) { |
| 12537 | if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) { |
| 12538 | if (storage != NULL) { |
| 12539 | storage->set(counter, Smi::FromInt(i)); |
| 12540 | } |
| 12541 | counter++; |
| 12542 | } |
| 12543 | } |
| 12544 | ASSERT(!storage || storage->length() >= counter); |
| 12545 | break; |
| 12546 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 12547 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 12548 | int length = ExternalPixelArray::cast(elements())->length(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12549 | while (counter < length) { |
| 12550 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12551 | storage->set(counter, Smi::FromInt(counter)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12552 | } |
| 12553 | counter++; |
| 12554 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12555 | ASSERT(!storage || storage->length() >= counter); |
| 12556 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12557 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12558 | case EXTERNAL_BYTE_ELEMENTS: |
| 12559 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 12560 | case EXTERNAL_SHORT_ELEMENTS: |
| 12561 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 12562 | case EXTERNAL_INT_ELEMENTS: |
| 12563 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12564 | case EXTERNAL_FLOAT_ELEMENTS: |
| 12565 | case EXTERNAL_DOUBLE_ELEMENTS: { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12566 | int length = ExternalArray::cast(elements())->length(); |
| 12567 | while (counter < length) { |
| 12568 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12569 | storage->set(counter, Smi::FromInt(counter)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12570 | } |
| 12571 | counter++; |
| 12572 | } |
| 12573 | ASSERT(!storage || storage->length() >= counter); |
| 12574 | break; |
| 12575 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12576 | case DICTIONARY_ELEMENTS: { |
| 12577 | if (storage != NULL) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12578 | element_dictionary()->CopyKeysTo(storage, |
| 12579 | filter, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12580 | SeededNumberDictionary::SORTED); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12581 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12582 | counter += element_dictionary()->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12583 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12584 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12585 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 12586 | FixedArray* parameter_map = FixedArray::cast(elements()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12587 | int mapped_length = parameter_map->length() - 2; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12588 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 12589 | if (arguments->IsDictionary()) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12590 | // Copy the keys from arguments first, because Dictionary::CopyKeysTo |
| 12591 | // will insert in storage starting at index 0. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12592 | SeededNumberDictionary* dictionary = |
| 12593 | SeededNumberDictionary::cast(arguments); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12594 | if (storage != NULL) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12595 | dictionary->CopyKeysTo( |
| 12596 | storage, filter, SeededNumberDictionary::UNSORTED); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12597 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12598 | counter += dictionary->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12599 | for (int i = 0; i < mapped_length; ++i) { |
| 12600 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 12601 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12602 | ++counter; |
| 12603 | } |
| 12604 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 12605 | if (storage != NULL) storage->SortPairs(storage, counter); |
| 12606 | |
| 12607 | } else { |
| 12608 | int backing_length = arguments->length(); |
| 12609 | int i = 0; |
| 12610 | for (; i < mapped_length; ++i) { |
| 12611 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 12612 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 12613 | ++counter; |
| 12614 | } else if (i < backing_length && !arguments->get(i)->IsTheHole()) { |
| 12615 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 12616 | ++counter; |
| 12617 | } |
| 12618 | } |
| 12619 | for (; i < backing_length; ++i) { |
| 12620 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 12621 | ++counter; |
| 12622 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12623 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12624 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12626 | } |
| 12627 | |
| 12628 | if (this->IsJSValue()) { |
| 12629 | Object* val = JSValue::cast(this)->value(); |
| 12630 | if (val->IsString()) { |
| 12631 | String* str = String::cast(val); |
| 12632 | if (storage) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 12633 | for (int i = 0; i < str->length(); i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12634 | storage->set(counter + i, Smi::FromInt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12635 | } |
| 12636 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 12637 | counter += str->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12638 | } |
| 12639 | } |
| 12640 | ASSERT(!storage || storage->length() == counter); |
| 12641 | return counter; |
| 12642 | } |
| 12643 | |
| 12644 | |
| 12645 | int JSObject::GetEnumElementKeys(FixedArray* storage) { |
| 12646 | return GetLocalElementKeys(storage, |
| 12647 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 12648 | } |
| 12649 | |
| 12650 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12651 | // StringKey simply carries a string object as key. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 12652 | class StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12653 | public: |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12654 | explicit StringKey(String* string) : |
| 12655 | string_(string), |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12656 | hash_(HashForObject(string)) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12657 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12658 | bool IsMatch(Object* string) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12659 | // We know that all entries in a hash table had their hash keys created. |
| 12660 | // Use that knowledge to have fast failure. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12661 | if (hash_ != HashForObject(string)) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12662 | return false; |
| 12663 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12664 | return string_->Equals(String::cast(string)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12665 | } |
| 12666 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12667 | uint32_t Hash() { return hash_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12668 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12669 | uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12670 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12671 | Object* AsObject(Heap* heap) { return string_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12672 | |
| 12673 | String* string_; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12674 | uint32_t hash_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12675 | }; |
| 12676 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12677 | |
| 12678 | // StringSharedKeys are used as keys in the eval cache. |
| 12679 | class StringSharedKey : public HashTableKey { |
| 12680 | public: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12681 | StringSharedKey(String* source, |
| 12682 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12683 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12684 | int scope_position) |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12685 | : source_(source), |
| 12686 | shared_(shared), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12687 | language_mode_(language_mode), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12688 | scope_position_(scope_position) { } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12689 | |
| 12690 | bool IsMatch(Object* other) { |
| 12691 | if (!other->IsFixedArray()) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12692 | FixedArray* other_array = FixedArray::cast(other); |
| 12693 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12694 | if (shared != shared_) return false; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12695 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 12696 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 12697 | language_unchecked == STRICT_MODE || |
| 12698 | language_unchecked == EXTENDED_MODE); |
| 12699 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
| 12700 | if (language_mode != language_mode_) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12701 | int scope_position = Smi::cast(other_array->get(3))->value(); |
| 12702 | if (scope_position != scope_position_) return false; |
| 12703 | String* source = String::cast(other_array->get(1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12704 | return source->Equals(source_); |
| 12705 | } |
| 12706 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12707 | static uint32_t StringSharedHashHelper(String* source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12708 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12709 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12710 | int scope_position) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12711 | uint32_t hash = source->Hash(); |
| 12712 | if (shared->HasSourceCode()) { |
| 12713 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 12714 | // code computation, we use a combination of the hash of the |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12715 | // script source code and the start position of the calling scope. |
| 12716 | // We do this to ensure that the cache entries can survive garbage |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12717 | // collection. |
| 12718 | Script* script = Script::cast(shared->script()); |
| 12719 | hash ^= String::cast(script->source())->Hash(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12720 | if (language_mode == STRICT_MODE) hash ^= 0x8000; |
| 12721 | if (language_mode == EXTENDED_MODE) hash ^= 0x0080; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12722 | hash += scope_position; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12723 | } |
| 12724 | return hash; |
| 12725 | } |
| 12726 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12727 | uint32_t Hash() { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12728 | return StringSharedHashHelper( |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12729 | source_, shared_, language_mode_, scope_position_); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12730 | } |
| 12731 | |
| 12732 | uint32_t HashForObject(Object* obj) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12733 | FixedArray* other_array = FixedArray::cast(obj); |
| 12734 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
| 12735 | String* source = String::cast(other_array->get(1)); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12736 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 12737 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 12738 | language_unchecked == STRICT_MODE || |
| 12739 | language_unchecked == EXTENDED_MODE); |
| 12740 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12741 | int scope_position = Smi::cast(other_array->get(3))->value(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12742 | return StringSharedHashHelper( |
| 12743 | source, shared, language_mode, scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12744 | } |
| 12745 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12746 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12747 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12748 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12749 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 12750 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12751 | FixedArray* other_array = FixedArray::cast(obj); |
| 12752 | other_array->set(0, shared_); |
| 12753 | other_array->set(1, source_); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12754 | other_array->set(2, Smi::FromInt(language_mode_)); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12755 | other_array->set(3, Smi::FromInt(scope_position_)); |
| 12756 | return other_array; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12757 | } |
| 12758 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12759 | private: |
| 12760 | String* source_; |
| 12761 | SharedFunctionInfo* shared_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12762 | LanguageMode language_mode_; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12763 | int scope_position_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12764 | }; |
| 12765 | |
| 12766 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12767 | // RegExpKey carries the source and flags of a regular expression as key. |
| 12768 | class RegExpKey : public HashTableKey { |
| 12769 | public: |
| 12770 | RegExpKey(String* string, JSRegExp::Flags flags) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 12771 | : string_(string), |
| 12772 | flags_(Smi::FromInt(flags.value())) { } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12773 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 12774 | // Rather than storing the key in the hash table, a pointer to the |
| 12775 | // stored value is stored where the key should be. IsMatch then |
| 12776 | // compares the search key to the found object, rather than comparing |
| 12777 | // a key to a key. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12778 | bool IsMatch(Object* obj) { |
| 12779 | FixedArray* val = FixedArray::cast(obj); |
| 12780 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 12781 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 12782 | } |
| 12783 | |
| 12784 | uint32_t Hash() { return RegExpHash(string_, flags_); } |
| 12785 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12786 | Object* AsObject(Heap* heap) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12787 | // Plain hash maps, which is where regexp keys are used, don't |
| 12788 | // use this function. |
| 12789 | UNREACHABLE(); |
| 12790 | return NULL; |
| 12791 | } |
| 12792 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12793 | uint32_t HashForObject(Object* obj) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12794 | FixedArray* val = FixedArray::cast(obj); |
| 12795 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 12796 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 12797 | } |
| 12798 | |
| 12799 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 12800 | return string->Hash() + flags->value(); |
| 12801 | } |
| 12802 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12803 | String* string_; |
| 12804 | Smi* flags_; |
| 12805 | }; |
| 12806 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12807 | // Utf8StringKey carries a vector of chars as key. |
| 12808 | class Utf8StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12809 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12810 | explicit Utf8StringKey(Vector<const char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 12811 | : string_(string), hash_field_(0), seed_(seed) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12812 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12813 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12814 | return String::cast(string)->IsUtf8EqualTo(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12815 | } |
| 12816 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12817 | uint32_t Hash() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12818 | if (hash_field_ != 0) return hash_field_ >> String::kHashShift; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12819 | hash_field_ = StringHasher::ComputeUtf8Hash(string_, seed_, &chars_); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12820 | uint32_t result = hash_field_ >> String::kHashShift; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 12821 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 12822 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12823 | } |
| 12824 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12825 | uint32_t HashForObject(Object* other) { |
| 12826 | return String::cast(other)->Hash(); |
| 12827 | } |
| 12828 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12829 | MaybeObject* AsObject(Heap* heap) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12830 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12831 | return heap->AllocateInternalizedStringFromUtf8(string_, |
| 12832 | chars_, |
| 12833 | hash_field_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12834 | } |
| 12835 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12836 | Vector<const char> string_; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12837 | uint32_t hash_field_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12838 | int chars_; // Caches the number of characters when computing the hash code. |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 12839 | uint32_t seed_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12840 | }; |
| 12841 | |
| 12842 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12843 | template <typename Char> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12844 | class SequentialStringKey : public HashTableKey { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12845 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12846 | explicit SequentialStringKey(Vector<const Char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 12847 | : string_(string), hash_field_(0), seed_(seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12848 | |
| 12849 | uint32_t Hash() { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12850 | hash_field_ = StringHasher::HashSequentialString<Char>(string_.start(), |
| 12851 | string_.length(), |
| 12852 | seed_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12853 | |
| 12854 | uint32_t result = hash_field_ >> String::kHashShift; |
| 12855 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 12856 | return result; |
| 12857 | } |
| 12858 | |
| 12859 | |
| 12860 | uint32_t HashForObject(Object* other) { |
| 12861 | return String::cast(other)->Hash(); |
| 12862 | } |
| 12863 | |
| 12864 | Vector<const Char> string_; |
| 12865 | uint32_t hash_field_; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 12866 | uint32_t seed_; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12867 | }; |
| 12868 | |
| 12869 | |
| 12870 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12871 | class OneByteStringKey : public SequentialStringKey<uint8_t> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12872 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12873 | OneByteStringKey(Vector<const uint8_t> str, uint32_t seed) |
| 12874 | : SequentialStringKey<uint8_t>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12875 | |
| 12876 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12877 | return String::cast(string)->IsOneByteEqualTo(string_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12878 | } |
| 12879 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12880 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12881 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12882 | return heap->AllocateOneByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12883 | } |
| 12884 | }; |
| 12885 | |
| 12886 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12887 | class SubStringOneByteStringKey : public HashTableKey { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12888 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12889 | explicit SubStringOneByteStringKey(Handle<SeqOneByteString> string, |
| 12890 | int from, |
| 12891 | int length) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 12892 | : string_(string), from_(from), length_(length) { } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12893 | |
| 12894 | uint32_t Hash() { |
| 12895 | ASSERT(length_ >= 0); |
| 12896 | ASSERT(from_ + length_ <= string_->length()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12897 | uint8_t* chars = string_->GetChars() + from_; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12898 | hash_field_ = StringHasher::HashSequentialString( |
| 12899 | chars, length_, string_->GetHeap()->HashSeed()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12900 | uint32_t result = hash_field_ >> String::kHashShift; |
| 12901 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 12902 | return result; |
| 12903 | } |
| 12904 | |
| 12905 | |
| 12906 | uint32_t HashForObject(Object* other) { |
| 12907 | return String::cast(other)->Hash(); |
| 12908 | } |
| 12909 | |
| 12910 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12911 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
| 12912 | return String::cast(string)->IsOneByteEqualTo(chars); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12913 | } |
| 12914 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12915 | MaybeObject* AsObject(Heap* heap) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12916 | if (hash_field_ == 0) Hash(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12917 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12918 | return heap->AllocateOneByteInternalizedString(chars, hash_field_); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12919 | } |
| 12920 | |
| 12921 | private: |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12922 | Handle<SeqOneByteString> string_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 12923 | int from_; |
| 12924 | int length_; |
| 12925 | uint32_t hash_field_; |
| 12926 | }; |
| 12927 | |
| 12928 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12929 | class TwoByteStringKey : public SequentialStringKey<uc16> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12930 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12931 | explicit TwoByteStringKey(Vector<const uc16> str, uint32_t seed) |
| 12932 | : SequentialStringKey<uc16>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12933 | |
| 12934 | bool IsMatch(Object* string) { |
| 12935 | return String::cast(string)->IsTwoByteEqualTo(string_); |
| 12936 | } |
| 12937 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12938 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12939 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12940 | return heap->AllocateTwoByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 12941 | } |
| 12942 | }; |
| 12943 | |
| 12944 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12945 | // InternalizedStringKey carries a string/internalized-string object as key. |
| 12946 | class InternalizedStringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12947 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12948 | explicit InternalizedStringKey(String* string) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12949 | : string_(string) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12950 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12951 | bool IsMatch(Object* string) { |
| 12952 | return String::cast(string)->Equals(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12953 | } |
| 12954 | |
| 12955 | uint32_t Hash() { return string_->Hash(); } |
| 12956 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12957 | uint32_t HashForObject(Object* other) { |
| 12958 | return String::cast(other)->Hash(); |
| 12959 | } |
| 12960 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 12961 | MaybeObject* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12962 | // Attempt to flatten the string, so that internalized strings will most |
| 12963 | // often be flat strings. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 12964 | string_ = string_->TryFlattenGetString(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12965 | // Internalize the string if possible. |
| 12966 | Map* map = heap->InternalizedStringMapForString(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12967 | if (map != NULL) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12968 | string_->set_map_no_write_barrier(map); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12969 | ASSERT(string_->IsInternalizedString()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12970 | return string_; |
| 12971 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12972 | // Otherwise allocate a new internalized string. |
| 12973 | return heap->AllocateInternalizedStringImpl( |
| 12974 | string_, string_->length(), string_->hash_field()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12975 | } |
| 12976 | |
| 12977 | static uint32_t StringHash(Object* obj) { |
| 12978 | return String::cast(obj)->Hash(); |
| 12979 | } |
| 12980 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12981 | String* string_; |
| 12982 | }; |
| 12983 | |
| 12984 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12985 | template<typename Shape, typename Key> |
| 12986 | void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12987 | IteratePointers(v, 0, kElementsStartOffset); |
| 12988 | } |
| 12989 | |
| 12990 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12991 | template<typename Shape, typename Key> |
| 12992 | void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12993 | IteratePointers(v, |
| 12994 | kElementsStartOffset, |
| 12995 | kHeaderSize + length() * kPointerSize); |
| 12996 | } |
| 12997 | |
| 12998 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12999 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13000 | MaybeObject* HashTable<Shape, Key>::Allocate(Heap* heap, |
| 13001 | int at_least_space_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13002 | MinimumCapacity capacity_option, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13003 | PretenureFlag pretenure) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13004 | ASSERT(!capacity_option || IS_POWER_OF_TWO(at_least_space_for)); |
| 13005 | int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY) |
| 13006 | ? at_least_space_for |
| 13007 | : ComputeCapacity(at_least_space_for); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13008 | if (capacity > HashTable::kMaxCapacity) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13009 | return Failure::OutOfMemoryException(0x10); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13010 | } |
| 13011 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13012 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13013 | { MaybeObject* maybe_obj = |
| 13014 | heap-> AllocateHashTable(EntryToIndex(capacity), pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13015 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13016 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13017 | HashTable::cast(obj)->SetNumberOfElements(0); |
| 13018 | HashTable::cast(obj)->SetNumberOfDeletedElements(0); |
| 13019 | HashTable::cast(obj)->SetCapacity(capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13020 | return obj; |
| 13021 | } |
| 13022 | |
| 13023 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13024 | // Find entry for key otherwise return kNotFound. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13025 | int NameDictionary::FindEntry(Name* key) { |
| 13026 | if (!key->IsUniqueName()) { |
| 13027 | return HashTable<NameDictionaryShape, Name*>::FindEntry(key); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13028 | } |
| 13029 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13030 | // Optimized for unique names. Knowledge of the key type allows: |
| 13031 | // 1. Move the check if the key is unique out of the loop. |
| 13032 | // 2. Avoid comparing hash codes in unique-to-unique comparison. |
| 13033 | // 3. Detect a case when a dictionary key is not unique but the key is. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13034 | // In case of positive result the dictionary key may be replaced by the |
| 13035 | // internalized string with minimal performance penalty. It gives a chance |
| 13036 | // to perform further lookups in code stubs (and significant performance |
| 13037 | // boost a certain style of code). |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13038 | |
| 13039 | // EnsureCapacity will guarantee the hash table is never full. |
| 13040 | uint32_t capacity = Capacity(); |
| 13041 | uint32_t entry = FirstProbe(key->Hash(), capacity); |
| 13042 | uint32_t count = 1; |
| 13043 | |
| 13044 | while (true) { |
| 13045 | int index = EntryToIndex(entry); |
| 13046 | Object* element = get(index); |
| 13047 | if (element->IsUndefined()) break; // Empty entry. |
| 13048 | if (key == element) return entry; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13049 | if (!element->IsUniqueName() && |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 13050 | !element->IsTheHole() && |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13051 | Name::cast(element)->Equals(key)) { |
| 13052 | // Replace a key that is a non-internalized string by the equivalent |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13053 | // internalized string for faster further lookups. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13054 | set(index, key); |
| 13055 | return entry; |
| 13056 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13057 | ASSERT(element->IsTheHole() || !Name::cast(element)->Equals(key)); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13058 | entry = NextProbe(entry, count++, capacity); |
| 13059 | } |
| 13060 | return kNotFound; |
| 13061 | } |
| 13062 | |
| 13063 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13064 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13065 | MaybeObject* HashTable<Shape, Key>::Rehash(HashTable* new_table, Key key) { |
| 13066 | ASSERT(NumberOfElements() < new_table->Capacity()); |
| 13067 | |
| 13068 | AssertNoAllocation no_gc; |
| 13069 | WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc); |
| 13070 | |
| 13071 | // Copy prefix to new array. |
| 13072 | for (int i = kPrefixStartIndex; |
| 13073 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 13074 | i++) { |
| 13075 | new_table->set(i, get(i), mode); |
| 13076 | } |
| 13077 | |
| 13078 | // Rehash the elements. |
| 13079 | int capacity = Capacity(); |
| 13080 | for (int i = 0; i < capacity; i++) { |
| 13081 | uint32_t from_index = EntryToIndex(i); |
| 13082 | Object* k = get(from_index); |
| 13083 | if (IsKey(k)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13084 | uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13085 | uint32_t insertion_index = |
| 13086 | EntryToIndex(new_table->FindInsertionEntry(hash)); |
| 13087 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 13088 | new_table->set(insertion_index + j, get(from_index + j), mode); |
| 13089 | } |
| 13090 | } |
| 13091 | } |
| 13092 | new_table->SetNumberOfElements(NumberOfElements()); |
| 13093 | new_table->SetNumberOfDeletedElements(0); |
| 13094 | return new_table; |
| 13095 | } |
| 13096 | |
| 13097 | |
| 13098 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13099 | MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, Key key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13100 | int capacity = Capacity(); |
| 13101 | int nof = NumberOfElements() + n; |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 13102 | int nod = NumberOfDeletedElements(); |
| 13103 | // Return if: |
| 13104 | // 50% is still free after adding n elements and |
| 13105 | // at most 50% of the free elements are deleted elements. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 13106 | if (nod <= (capacity - nof) >> 1) { |
| 13107 | int needed_free = nof >> 1; |
| 13108 | if (nof + needed_free <= capacity) return this; |
| 13109 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13110 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 13111 | const int kMinCapacityForPretenure = 256; |
| 13112 | bool pretenure = |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13113 | (capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13114 | Object* obj; |
| 13115 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13116 | Allocate(GetHeap(), |
| 13117 | nof * 2, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13118 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 13119 | pretenure ? TENURED : NOT_TENURED); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13120 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13121 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13122 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13123 | return Rehash(HashTable::cast(obj), key); |
| 13124 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13125 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13126 | |
| 13127 | template<typename Shape, typename Key> |
| 13128 | MaybeObject* HashTable<Shape, Key>::Shrink(Key key) { |
| 13129 | int capacity = Capacity(); |
| 13130 | int nof = NumberOfElements(); |
| 13131 | |
| 13132 | // Shrink to fit the number of elements if only a quarter of the |
| 13133 | // capacity is filled with elements. |
| 13134 | if (nof > (capacity >> 2)) return this; |
| 13135 | // Allocate a new dictionary with room for at least the current |
| 13136 | // number of elements. The allocation method will make sure that |
| 13137 | // there is extra room in the dictionary for additions. Don't go |
| 13138 | // lower than room for 16 elements. |
| 13139 | int at_least_room_for = nof; |
| 13140 | if (at_least_room_for < 16) return this; |
| 13141 | |
| 13142 | const int kMinCapacityForPretenure = 256; |
| 13143 | bool pretenure = |
| 13144 | (at_least_room_for > kMinCapacityForPretenure) && |
| 13145 | !GetHeap()->InNewSpace(this); |
| 13146 | Object* obj; |
| 13147 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13148 | Allocate(GetHeap(), |
| 13149 | at_least_room_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13150 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 13151 | pretenure ? TENURED : NOT_TENURED); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13152 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13153 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13154 | |
| 13155 | return Rehash(HashTable::cast(obj), key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13156 | } |
| 13157 | |
| 13158 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13159 | template<typename Shape, typename Key> |
| 13160 | uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13161 | uint32_t capacity = Capacity(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13162 | uint32_t entry = FirstProbe(hash, capacity); |
| 13163 | uint32_t count = 1; |
| 13164 | // EnsureCapacity will guarantee the hash table is never full. |
| 13165 | while (true) { |
| 13166 | Object* element = KeyAt(entry); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 13167 | if (element->IsUndefined() || element->IsTheHole()) break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13168 | entry = NextProbe(entry, count++, capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13169 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13170 | return entry; |
| 13171 | } |
| 13172 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13173 | // Force instantiation of template instances class. |
| 13174 | // Please note this list is compiler dependent. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13175 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13176 | template class HashTable<StringTableShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13177 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13178 | template class HashTable<CompilationCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13179 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13180 | template class HashTable<MapCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13181 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13182 | template class HashTable<ObjectHashTableShape<1>, Object*>; |
| 13183 | |
| 13184 | template class HashTable<ObjectHashTableShape<2>, Object*>; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 13185 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13186 | template class Dictionary<NameDictionaryShape, Name*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13187 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13188 | template class Dictionary<SeededNumberDictionaryShape, uint32_t>; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13189 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13190 | template class Dictionary<UnseededNumberDictionaryShape, uint32_t>; |
| 13191 | |
| 13192 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13193 | Allocate(Heap* heap, int at_least_space_for); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13194 | |
| 13195 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13196 | Allocate(Heap* heap, int at_least_space_for); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13197 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13198 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
| 13199 | Allocate(Heap* heap, int n); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13200 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13201 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::AtPut( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13202 | uint32_t, Object*); |
| 13203 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13204 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 13205 | AtPut(uint32_t, Object*); |
| 13206 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 13207 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 13208 | SlowReverseLookup(Object* value); |
| 13209 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13210 | template Object* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 13211 | SlowReverseLookup(Object* value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13212 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13213 | template Object* Dictionary<NameDictionaryShape, Name*>::SlowReverseLookup( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13214 | Object*); |
| 13215 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13216 | template void Dictionary<SeededNumberDictionaryShape, uint32_t>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13217 | FixedArray*, |
| 13218 | PropertyAttributes, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13219 | Dictionary<SeededNumberDictionaryShape, uint32_t>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13220 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13221 | template Object* Dictionary<NameDictionaryShape, Name*>::DeleteProperty( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13222 | int, JSObject::DeleteMode); |
| 13223 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13224 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 13225 | DeleteProperty(int, JSObject::DeleteMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13226 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13227 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Shrink(Name* n); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13228 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13229 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Shrink( |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13230 | uint32_t); |
| 13231 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13232 | template void Dictionary<NameDictionaryShape, Name*>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13233 | FixedArray*, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13234 | int, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13235 | PropertyAttributes, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13236 | Dictionary<NameDictionaryShape, Name*>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13237 | |
| 13238 | template int |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13239 | Dictionary<NameDictionaryShape, Name*>::NumberOfElementsFilterAttributes( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13240 | PropertyAttributes); |
| 13241 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13242 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Add( |
| 13243 | Name*, Object*, PropertyDetails); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13244 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13245 | template MaybeObject* |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13246 | Dictionary<NameDictionaryShape, Name*>::GenerateNewEnumerationIndices(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13247 | |
| 13248 | template int |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13249 | Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 13250 | NumberOfElementsFilterAttributes(PropertyAttributes); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13251 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13252 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Add( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13253 | uint32_t, Object*, PropertyDetails); |
| 13254 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13255 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::Add( |
| 13256 | uint32_t, Object*, PropertyDetails); |
| 13257 | |
| 13258 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 13259 | EnsureCapacity(int, uint32_t); |
| 13260 | |
| 13261 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13262 | EnsureCapacity(int, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13263 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13264 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
| 13265 | EnsureCapacity(int, Name*); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13266 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13267 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 13268 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
| 13269 | |
| 13270 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 13271 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13272 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13273 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::AddEntry( |
| 13274 | Name*, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13275 | |
| 13276 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13277 | int Dictionary<SeededNumberDictionaryShape, uint32_t>::NumberOfEnumElements(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13278 | |
| 13279 | template |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13280 | int Dictionary<NameDictionaryShape, Name*>::NumberOfEnumElements(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13281 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13282 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13283 | int HashTable<SeededNumberDictionaryShape, uint32_t>::FindEntry(uint32_t); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13284 | |
| 13285 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13286 | // Collates undefined and unexisting elements below limit from position |
| 13287 | // zero of the elements. The object stays in Dictionary mode. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13288 | MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13289 | ASSERT(HasDictionaryElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13290 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 13291 | // or because we are not going to sort (and therefore compact) all of the |
| 13292 | // elements. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13293 | SeededNumberDictionary* dict = element_dictionary(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13294 | HeapNumber* result_double = NULL; |
| 13295 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13296 | // Allocate space for result before we start mutating the object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13297 | Object* new_double; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13298 | { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13299 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 13300 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13301 | result_double = HeapNumber::cast(new_double); |
| 13302 | } |
| 13303 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13304 | Object* obj; |
| 13305 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13306 | SeededNumberDictionary::Allocate(GetHeap(), dict->NumberOfElements()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13307 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13308 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13309 | SeededNumberDictionary* new_dict = SeededNumberDictionary::cast(obj); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13310 | |
| 13311 | AssertNoAllocation no_alloc; |
| 13312 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13313 | uint32_t pos = 0; |
| 13314 | uint32_t undefs = 0; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 13315 | int capacity = dict->Capacity(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13316 | for (int i = 0; i < capacity; i++) { |
| 13317 | Object* k = dict->KeyAt(i); |
| 13318 | if (dict->IsKey(k)) { |
| 13319 | ASSERT(k->IsNumber()); |
| 13320 | ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 13321 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 13322 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 13323 | Object* value = dict->ValueAt(i); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 13324 | PropertyDetails details = dict->DetailsAt(i); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13325 | if (details.type() == CALLBACKS || details.IsReadOnly()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 13326 | // Bail out and do the sorting of undefineds and array holes in JS. |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13327 | // Also bail out if the element is not supposed to be moved. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 13328 | return Smi::FromInt(-1); |
| 13329 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13330 | uint32_t key = NumberToUint32(k); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13331 | // In the following we assert that adding the entry to the new dictionary |
| 13332 | // does not cause GC. This is the case because we made sure to allocate |
| 13333 | // the dictionary big enough above, so it need not grow. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13334 | if (key < limit) { |
| 13335 | if (value->IsUndefined()) { |
| 13336 | undefs++; |
| 13337 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 13338 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13339 | // Adding an entry with the key beyond smi-range requires |
| 13340 | // allocation. Bailout. |
| 13341 | return Smi::FromInt(-1); |
| 13342 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13343 | new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13344 | pos++; |
| 13345 | } |
| 13346 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 13347 | if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13348 | // Adding an entry with the key beyond smi-range requires |
| 13349 | // allocation. Bailout. |
| 13350 | return Smi::FromInt(-1); |
| 13351 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13352 | new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13353 | } |
| 13354 | } |
| 13355 | } |
| 13356 | |
| 13357 | uint32_t result = pos; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 13358 | PropertyDetails no_details = PropertyDetails(NONE, NORMAL, 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13359 | Heap* heap = GetHeap(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13360 | while (undefs > 0) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 13361 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13362 | // Adding an entry with the key beyond smi-range requires |
| 13363 | // allocation. Bailout. |
| 13364 | return Smi::FromInt(-1); |
| 13365 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13366 | new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)-> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13367 | ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13368 | pos++; |
| 13369 | undefs--; |
| 13370 | } |
| 13371 | |
| 13372 | set_elements(new_dict); |
| 13373 | |
| 13374 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13375 | return Smi::FromInt(static_cast<int>(result)); |
| 13376 | } |
| 13377 | |
| 13378 | ASSERT_NE(NULL, result_double); |
| 13379 | result_double->set_value(static_cast<double>(result)); |
| 13380 | return result_double; |
| 13381 | } |
| 13382 | |
| 13383 | |
| 13384 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 13385 | // the start of the elements array. |
| 13386 | // If the object is in dictionary mode, it is converted to fast elements |
| 13387 | // mode. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13388 | MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13389 | Heap* heap = GetHeap(); |
| 13390 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13391 | if (HasDictionaryElements()) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13392 | // Convert to fast elements containing only the existing properties. |
| 13393 | // Ordering is irrelevant, since we are going to sort anyway. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13394 | SeededNumberDictionary* dict = element_dictionary(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13395 | if (IsJSArray() || dict->requires_slow_elements() || |
| 13396 | dict->max_number_key() >= limit) { |
| 13397 | return PrepareSlowElementsForSort(limit); |
| 13398 | } |
| 13399 | // Convert to fast elements. |
| 13400 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13401 | Object* obj; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13402 | MaybeObject* maybe_obj = GetElementsTransitionMap(GetIsolate(), |
| 13403 | FAST_HOLEY_ELEMENTS); |
| 13404 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 13405 | Map* new_map = Map::cast(obj); |
| 13406 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13407 | PretenureFlag tenure = heap->InNewSpace(this) ? NOT_TENURED: TENURED; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13408 | Object* new_array; |
| 13409 | { MaybeObject* maybe_new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13410 | heap->AllocateFixedArray(dict->NumberOfElements(), tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13411 | if (!maybe_new_array->ToObject(&new_array)) return maybe_new_array; |
| 13412 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13413 | FixedArray* fast_elements = FixedArray::cast(new_array); |
| 13414 | dict->CopyValuesTo(fast_elements); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13415 | ValidateElements(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 13416 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13417 | set_map_and_elements(new_map, fast_elements); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 13418 | } else if (HasExternalArrayElements()) { |
| 13419 | // External arrays cannot have holes or undefined elements. |
| 13420 | return Smi::FromInt(ExternalArray::cast(elements())->length()); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13421 | } else if (!HasFastDoubleElements()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13422 | Object* obj; |
| 13423 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 13424 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13425 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13426 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13427 | ASSERT(HasFastSmiOrObjectElements() || HasFastDoubleElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13428 | |
| 13429 | // Collect holes at the end, undefined before that and the rest at the |
| 13430 | // start, and return the number of non-hole, non-undefined values. |
| 13431 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13432 | FixedArrayBase* elements_base = FixedArrayBase::cast(this->elements()); |
| 13433 | uint32_t elements_length = static_cast<uint32_t>(elements_base->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13434 | if (limit > elements_length) { |
| 13435 | limit = elements_length ; |
| 13436 | } |
| 13437 | if (limit == 0) { |
| 13438 | return Smi::FromInt(0); |
| 13439 | } |
| 13440 | |
| 13441 | HeapNumber* result_double = NULL; |
| 13442 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13443 | // Pessimistically allocate space for return value before |
| 13444 | // we start mutating the array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13445 | Object* new_double; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13446 | { MaybeObject* maybe_new_double = heap->AllocateHeapNumber(0.0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13447 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 13448 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13449 | result_double = HeapNumber::cast(new_double); |
| 13450 | } |
| 13451 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13452 | uint32_t result = 0; |
| 13453 | if (elements_base->map() == heap->fixed_double_array_map()) { |
| 13454 | FixedDoubleArray* elements = FixedDoubleArray::cast(elements_base); |
| 13455 | // Split elements into defined and the_hole, in that order. |
| 13456 | unsigned int holes = limit; |
| 13457 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 13458 | // number of stores of non-undefined, non-the-hole values. |
| 13459 | for (unsigned int i = 0; i < holes; i++) { |
| 13460 | if (elements->is_the_hole(i)) { |
| 13461 | holes--; |
| 13462 | } else { |
| 13463 | continue; |
| 13464 | } |
| 13465 | // Position i needs to be filled. |
| 13466 | while (holes > i) { |
| 13467 | if (elements->is_the_hole(holes)) { |
| 13468 | holes--; |
| 13469 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13470 | elements->set(i, elements->get_scalar(holes)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13471 | break; |
| 13472 | } |
| 13473 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13474 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13475 | result = holes; |
| 13476 | while (holes < limit) { |
| 13477 | elements->set_the_hole(holes); |
| 13478 | holes++; |
| 13479 | } |
| 13480 | } else { |
| 13481 | FixedArray* elements = FixedArray::cast(elements_base); |
| 13482 | AssertNoAllocation no_alloc; |
| 13483 | |
| 13484 | // Split elements into defined, undefined and the_hole, in that order. Only |
| 13485 | // count locations for undefined and the hole, and fill them afterwards. |
| 13486 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc); |
| 13487 | unsigned int undefs = limit; |
| 13488 | unsigned int holes = limit; |
| 13489 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 13490 | // number of stores of non-undefined, non-the-hole values. |
| 13491 | for (unsigned int i = 0; i < undefs; i++) { |
| 13492 | Object* current = elements->get(i); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13493 | if (current->IsTheHole()) { |
| 13494 | holes--; |
| 13495 | undefs--; |
| 13496 | } else if (current->IsUndefined()) { |
| 13497 | undefs--; |
| 13498 | } else { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13499 | continue; |
| 13500 | } |
| 13501 | // Position i needs to be filled. |
| 13502 | while (undefs > i) { |
| 13503 | current = elements->get(undefs); |
| 13504 | if (current->IsTheHole()) { |
| 13505 | holes--; |
| 13506 | undefs--; |
| 13507 | } else if (current->IsUndefined()) { |
| 13508 | undefs--; |
| 13509 | } else { |
| 13510 | elements->set(i, current, write_barrier); |
| 13511 | break; |
| 13512 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13513 | } |
| 13514 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 13515 | result = undefs; |
| 13516 | while (undefs < holes) { |
| 13517 | elements->set_undefined(undefs); |
| 13518 | undefs++; |
| 13519 | } |
| 13520 | while (holes < limit) { |
| 13521 | elements->set_the_hole(holes); |
| 13522 | holes++; |
| 13523 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13524 | } |
| 13525 | |
| 13526 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 13527 | return Smi::FromInt(static_cast<int>(result)); |
| 13528 | } |
| 13529 | ASSERT_NE(NULL, result_double); |
| 13530 | result_double->set_value(static_cast<double>(result)); |
| 13531 | return result_double; |
| 13532 | } |
| 13533 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 13534 | ExternalArrayType JSTypedArray::type() { |
| 13535 | switch (elements()->map()->instance_type()) { |
| 13536 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 13537 | return kExternalByteArray; |
| 13538 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 13539 | return kExternalUnsignedByteArray; |
| 13540 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 13541 | return kExternalShortArray; |
| 13542 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 13543 | return kExternalUnsignedShortArray; |
| 13544 | case EXTERNAL_INT_ARRAY_TYPE: |
| 13545 | return kExternalIntArray; |
| 13546 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 13547 | return kExternalUnsignedIntArray; |
| 13548 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 13549 | return kExternalFloatArray; |
| 13550 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 13551 | return kExternalDoubleArray; |
| 13552 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 13553 | return kExternalPixelArray; |
| 13554 | default: |
| 13555 | return static_cast<ExternalArrayType>(-1); |
| 13556 | } |
| 13557 | } |
| 13558 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 13559 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 13560 | size_t JSTypedArray::element_size() { |
| 13561 | switch (elements()->map()->instance_type()) { |
| 13562 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 13563 | return 1; |
| 13564 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 13565 | return 1; |
| 13566 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 13567 | return 2; |
| 13568 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 13569 | return 2; |
| 13570 | case EXTERNAL_INT_ARRAY_TYPE: |
| 13571 | return 4; |
| 13572 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 13573 | return 4; |
| 13574 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 13575 | return 4; |
| 13576 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 13577 | return 8; |
| 13578 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 13579 | return 1; |
| 13580 | default: |
| 13581 | UNREACHABLE(); |
| 13582 | return 0; |
| 13583 | } |
| 13584 | } |
| 13585 | |
| 13586 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 13587 | Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13588 | uint8_t clamped_value = 0; |
| 13589 | if (index < static_cast<uint32_t>(length())) { |
| 13590 | if (value->IsSmi()) { |
| 13591 | int int_value = Smi::cast(value)->value(); |
| 13592 | if (int_value < 0) { |
| 13593 | clamped_value = 0; |
| 13594 | } else if (int_value > 255) { |
| 13595 | clamped_value = 255; |
| 13596 | } else { |
| 13597 | clamped_value = static_cast<uint8_t>(int_value); |
| 13598 | } |
| 13599 | } else if (value->IsHeapNumber()) { |
| 13600 | double double_value = HeapNumber::cast(value)->value(); |
| 13601 | if (!(double_value > 0)) { |
| 13602 | // NaN and less than zero clamp to zero. |
| 13603 | clamped_value = 0; |
| 13604 | } else if (double_value > 255) { |
| 13605 | // Greater than 255 clamp to 255. |
| 13606 | clamped_value = 255; |
| 13607 | } else { |
| 13608 | // Other doubles are rounded to the nearest integer. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 13609 | clamped_value = static_cast<uint8_t>(lrint(double_value)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13610 | } |
| 13611 | } else { |
| 13612 | // Clamp undefined to zero (default). All other types have been |
| 13613 | // converted to a number type further up in the call chain. |
| 13614 | ASSERT(value->IsUndefined()); |
| 13615 | } |
| 13616 | set(index, clamped_value); |
| 13617 | } |
| 13618 | return Smi::FromInt(clamped_value); |
| 13619 | } |
| 13620 | |
| 13621 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13622 | template<typename ExternalArrayClass, typename ValueType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13623 | static MaybeObject* ExternalArrayIntSetter(Heap* heap, |
| 13624 | ExternalArrayClass* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13625 | uint32_t index, |
| 13626 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13627 | ValueType cast_value = 0; |
| 13628 | if (index < static_cast<uint32_t>(receiver->length())) { |
| 13629 | if (value->IsSmi()) { |
| 13630 | int int_value = Smi::cast(value)->value(); |
| 13631 | cast_value = static_cast<ValueType>(int_value); |
| 13632 | } else if (value->IsHeapNumber()) { |
| 13633 | double double_value = HeapNumber::cast(value)->value(); |
| 13634 | cast_value = static_cast<ValueType>(DoubleToInt32(double_value)); |
| 13635 | } else { |
| 13636 | // Clamp undefined to zero (default). All other types have been |
| 13637 | // converted to a number type further up in the call chain. |
| 13638 | ASSERT(value->IsUndefined()); |
| 13639 | } |
| 13640 | receiver->set(index, cast_value); |
| 13641 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13642 | return heap->NumberFromInt32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13643 | } |
| 13644 | |
| 13645 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13646 | MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13647 | return ExternalArrayIntSetter<ExternalByteArray, int8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13648 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13649 | } |
| 13650 | |
| 13651 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13652 | MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index, |
| 13653 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13654 | return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13655 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13656 | } |
| 13657 | |
| 13658 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13659 | MaybeObject* ExternalShortArray::SetValue(uint32_t index, |
| 13660 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13661 | return ExternalArrayIntSetter<ExternalShortArray, int16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13662 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13663 | } |
| 13664 | |
| 13665 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13666 | MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index, |
| 13667 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13668 | return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13669 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13670 | } |
| 13671 | |
| 13672 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13673 | MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13674 | return ExternalArrayIntSetter<ExternalIntArray, int32_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13675 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13676 | } |
| 13677 | |
| 13678 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13679 | MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13680 | uint32_t cast_value = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13681 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13682 | if (index < static_cast<uint32_t>(length())) { |
| 13683 | if (value->IsSmi()) { |
| 13684 | int int_value = Smi::cast(value)->value(); |
| 13685 | cast_value = static_cast<uint32_t>(int_value); |
| 13686 | } else if (value->IsHeapNumber()) { |
| 13687 | double double_value = HeapNumber::cast(value)->value(); |
| 13688 | cast_value = static_cast<uint32_t>(DoubleToUint32(double_value)); |
| 13689 | } else { |
| 13690 | // Clamp undefined to zero (default). All other types have been |
| 13691 | // converted to a number type further up in the call chain. |
| 13692 | ASSERT(value->IsUndefined()); |
| 13693 | } |
| 13694 | set(index, cast_value); |
| 13695 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13696 | return heap->NumberFromUint32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13697 | } |
| 13698 | |
| 13699 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13700 | MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 13701 | float cast_value = static_cast<float>(OS::nan_value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13702 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13703 | if (index < static_cast<uint32_t>(length())) { |
| 13704 | if (value->IsSmi()) { |
| 13705 | int int_value = Smi::cast(value)->value(); |
| 13706 | cast_value = static_cast<float>(int_value); |
| 13707 | } else if (value->IsHeapNumber()) { |
| 13708 | double double_value = HeapNumber::cast(value)->value(); |
| 13709 | cast_value = static_cast<float>(double_value); |
| 13710 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 13711 | // Clamp undefined to NaN (default). All other types have been |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13712 | // converted to a number type further up in the call chain. |
| 13713 | ASSERT(value->IsUndefined()); |
| 13714 | } |
| 13715 | set(index, cast_value); |
| 13716 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13717 | return heap->AllocateHeapNumber(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13718 | } |
| 13719 | |
| 13720 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13721 | MaybeObject* ExternalDoubleArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 13722 | double double_value = OS::nan_value(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13723 | Heap* heap = GetHeap(); |
| 13724 | if (index < static_cast<uint32_t>(length())) { |
| 13725 | if (value->IsSmi()) { |
| 13726 | int int_value = Smi::cast(value)->value(); |
| 13727 | double_value = static_cast<double>(int_value); |
| 13728 | } else if (value->IsHeapNumber()) { |
| 13729 | double_value = HeapNumber::cast(value)->value(); |
| 13730 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 13731 | // Clamp undefined to NaN (default). All other types have been |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13732 | // converted to a number type further up in the call chain. |
| 13733 | ASSERT(value->IsUndefined()); |
| 13734 | } |
| 13735 | set(index, double_value); |
| 13736 | } |
| 13737 | return heap->AllocateHeapNumber(double_value); |
| 13738 | } |
| 13739 | |
| 13740 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 13741 | JSGlobalPropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13742 | ASSERT(!HasFastProperties()); |
| 13743 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 13744 | return JSGlobalPropertyCell::cast(value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13745 | } |
| 13746 | |
| 13747 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13748 | Handle<JSGlobalPropertyCell> GlobalObject::EnsurePropertyCell( |
| 13749 | Handle<GlobalObject> global, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13750 | Handle<Name> name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13751 | Isolate* isolate = global->GetIsolate(); |
| 13752 | CALL_HEAP_FUNCTION(isolate, |
| 13753 | global->EnsurePropertyCell(*name), |
| 13754 | JSGlobalPropertyCell); |
| 13755 | } |
| 13756 | |
| 13757 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13758 | MaybeObject* GlobalObject::EnsurePropertyCell(Name* name) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 13759 | ASSERT(!HasFastProperties()); |
| 13760 | int entry = property_dictionary()->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13761 | if (entry == NameDictionary::kNotFound) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13762 | Heap* heap = GetHeap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13763 | Object* cell; |
| 13764 | { MaybeObject* maybe_cell = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13765 | heap->AllocateJSGlobalPropertyCell(heap->the_hole_value()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13766 | if (!maybe_cell->ToObject(&cell)) return maybe_cell; |
| 13767 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 13768 | PropertyDetails details(NONE, NORMAL, 0); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 13769 | details = details.AsDeleted(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13770 | Object* dictionary; |
| 13771 | { MaybeObject* maybe_dictionary = |
| 13772 | property_dictionary()->Add(name, cell, details); |
| 13773 | if (!maybe_dictionary->ToObject(&dictionary)) return maybe_dictionary; |
| 13774 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13775 | set_properties(NameDictionary::cast(dictionary)); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 13776 | return cell; |
| 13777 | } else { |
| 13778 | Object* value = property_dictionary()->ValueAt(entry); |
| 13779 | ASSERT(value->IsJSGlobalPropertyCell()); |
| 13780 | return value; |
| 13781 | } |
| 13782 | } |
| 13783 | |
| 13784 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13785 | MaybeObject* StringTable::LookupString(String* string, Object** s) { |
| 13786 | InternalizedStringKey key(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13787 | return LookupKey(&key, s); |
| 13788 | } |
| 13789 | |
| 13790 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13791 | // This class is used for looking up two character strings in the string table. |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13792 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 13793 | // string hash calculation loop here for speed. Doesn't work if the two |
| 13794 | // characters form a decimal integer, since such strings have a different hash |
| 13795 | // algorithm. |
| 13796 | class TwoCharHashTableKey : public HashTableKey { |
| 13797 | public: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13798 | TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed) |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13799 | : c1_(c1), c2_(c2) { |
| 13800 | // Char 1. |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13801 | uint32_t hash = seed; |
| 13802 | hash += c1; |
| 13803 | hash += hash << 10; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13804 | hash ^= hash >> 6; |
| 13805 | // Char 2. |
| 13806 | hash += c2; |
| 13807 | hash += hash << 10; |
| 13808 | hash ^= hash >> 6; |
| 13809 | // GetHash. |
| 13810 | hash += hash << 3; |
| 13811 | hash ^= hash >> 11; |
| 13812 | hash += hash << 15; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 13813 | if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13814 | hash_ = hash; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13815 | #ifdef DEBUG |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13816 | // If this assert fails then we failed to reproduce the two-character |
| 13817 | // version of the string hashing algorithm above. One reason could be |
| 13818 | // that we were passed two digits as characters, since the hash |
| 13819 | // algorithm is different in that case. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13820 | uint16_t chars[2] = {c1, c2}; |
| 13821 | uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed); |
| 13822 | hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; |
| 13823 | ASSERT_EQ(static_cast<int32_t>(hash), static_cast<int32_t>(check_hash)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13824 | #endif |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13825 | } |
| 13826 | |
| 13827 | bool IsMatch(Object* o) { |
| 13828 | if (!o->IsString()) return false; |
| 13829 | String* other = String::cast(o); |
| 13830 | if (other->length() != 2) return false; |
| 13831 | if (other->Get(0) != c1_) return false; |
| 13832 | return other->Get(1) == c2_; |
| 13833 | } |
| 13834 | |
| 13835 | uint32_t Hash() { return hash_; } |
| 13836 | uint32_t HashForObject(Object* key) { |
| 13837 | if (!key->IsString()) return 0; |
| 13838 | return String::cast(key)->Hash(); |
| 13839 | } |
| 13840 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13841 | Object* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13842 | // The TwoCharHashTableKey is only used for looking in the string |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13843 | // table, not for adding to it. |
| 13844 | UNREACHABLE(); |
| 13845 | return NULL; |
| 13846 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 13847 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13848 | private: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13849 | uint16_t c1_; |
| 13850 | uint16_t c2_; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13851 | uint32_t hash_; |
| 13852 | }; |
| 13853 | |
| 13854 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13855 | bool StringTable::LookupStringIfExists(String* string, String** result) { |
| 13856 | InternalizedStringKey key(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 13857 | int entry = FindEntry(&key); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13858 | if (entry == kNotFound) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 13859 | return false; |
| 13860 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13861 | *result = String::cast(KeyAt(entry)); |
| 13862 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 13863 | return true; |
| 13864 | } |
| 13865 | } |
| 13866 | |
| 13867 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13868 | bool StringTable::LookupTwoCharsStringIfExists(uint16_t c1, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13869 | uint16_t c2, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13870 | String** result) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13871 | TwoCharHashTableKey key(c1, c2, GetHeap()->HashSeed()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13872 | int entry = FindEntry(&key); |
| 13873 | if (entry == kNotFound) { |
| 13874 | return false; |
| 13875 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13876 | *result = String::cast(KeyAt(entry)); |
| 13877 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 13878 | return true; |
| 13879 | } |
| 13880 | } |
| 13881 | |
| 13882 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13883 | MaybeObject* StringTable::LookupUtf8String(Vector<const char> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13884 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13885 | Utf8StringKey key(str, GetHeap()->HashSeed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13886 | return LookupKey(&key, s); |
| 13887 | } |
| 13888 | |
| 13889 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13890 | MaybeObject* StringTable::LookupOneByteString(Vector<const uint8_t> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13891 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13892 | OneByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13893 | return LookupKey(&key, s); |
| 13894 | } |
| 13895 | |
| 13896 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13897 | MaybeObject* StringTable::LookupSubStringOneByteString( |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 13898 | Handle<SeqOneByteString> str, |
| 13899 | int from, |
| 13900 | int length, |
| 13901 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13902 | SubStringOneByteStringKey key(str, from, length); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13903 | return LookupKey(&key, s); |
| 13904 | } |
| 13905 | |
| 13906 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13907 | MaybeObject* StringTable::LookupTwoByteString(Vector<const uc16> str, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13908 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13909 | TwoByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13910 | return LookupKey(&key, s); |
| 13911 | } |
| 13912 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13913 | MaybeObject* StringTable::LookupKey(HashTableKey* key, Object** s) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13914 | int entry = FindEntry(key); |
| 13915 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13916 | // String already in table. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13917 | if (entry != kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13918 | *s = KeyAt(entry); |
| 13919 | return this; |
| 13920 | } |
| 13921 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13922 | // Adding new string. Grow table if needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13923 | Object* obj; |
| 13924 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 13925 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13926 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13927 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13928 | // Create string object. |
| 13929 | Object* string; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13930 | { MaybeObject* maybe_string = key->AsObject(GetHeap()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13931 | if (!maybe_string->ToObject(&string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13932 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13933 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13934 | // If the string table grew as part of EnsureCapacity, obj is not |
| 13935 | // the current string table and therefore we cannot use |
| 13936 | // StringTable::cast here. |
| 13937 | StringTable* table = reinterpret_cast<StringTable*>(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13938 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13939 | // Add the new string and return it along with the string table. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13940 | entry = table->FindInsertionEntry(key->Hash()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13941 | table->set(EntryToIndex(entry), string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13942 | table->ElementAdded(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13943 | *s = string; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13944 | return table; |
| 13945 | } |
| 13946 | |
| 13947 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13948 | // The key for the script compilation cache is dependent on the mode flags, |
| 13949 | // because they change the global language mode and thus binding behaviour. |
| 13950 | // If flags change at some point, we must ensure that we do not hit the cache |
| 13951 | // for code compiled with different settings. |
| 13952 | static LanguageMode CurrentGlobalLanguageMode() { |
| 13953 | return FLAG_use_strict |
| 13954 | ? (FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE) |
| 13955 | : CLASSIC_MODE; |
| 13956 | } |
| 13957 | |
| 13958 | |
| 13959 | Object* CompilationCacheTable::Lookup(String* src, Context* context) { |
| 13960 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 13961 | StringSharedKey key(src, |
| 13962 | shared, |
| 13963 | CurrentGlobalLanguageMode(), |
| 13964 | RelocInfo::kNoPosition); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13965 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13966 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13967 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13968 | } |
| 13969 | |
| 13970 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13971 | Object* CompilationCacheTable::LookupEval(String* src, |
| 13972 | Context* context, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13973 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13974 | int scope_position) { |
| 13975 | StringSharedKey key(src, |
| 13976 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13977 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13978 | scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13979 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13980 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13981 | return get(EntryToIndex(entry) + 1); |
| 13982 | } |
| 13983 | |
| 13984 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13985 | Object* CompilationCacheTable::LookupRegExp(String* src, |
| 13986 | JSRegExp::Flags flags) { |
| 13987 | RegExpKey key(src, flags); |
| 13988 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13989 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13990 | return get(EntryToIndex(entry) + 1); |
| 13991 | } |
| 13992 | |
| 13993 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13994 | MaybeObject* CompilationCacheTable::Put(String* src, |
| 13995 | Context* context, |
| 13996 | Object* value) { |
| 13997 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 13998 | StringSharedKey key(src, |
| 13999 | shared, |
| 14000 | CurrentGlobalLanguageMode(), |
| 14001 | RelocInfo::kNoPosition); |
| 14002 | CompilationCacheTable* cache; |
| 14003 | MaybeObject* maybe_cache = EnsureCapacity(1, &key); |
| 14004 | if (!maybe_cache->To(&cache)) return maybe_cache; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14005 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14006 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14007 | MaybeObject* maybe_k = key.AsObject(GetHeap()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14008 | if (!maybe_k->To(&k)) return maybe_k; |
| 14009 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14010 | int entry = cache->FindInsertionEntry(key.Hash()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14011 | cache->set(EntryToIndex(entry), k); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14012 | cache->set(EntryToIndex(entry) + 1, value); |
| 14013 | cache->ElementAdded(); |
| 14014 | return cache; |
| 14015 | } |
| 14016 | |
| 14017 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14018 | MaybeObject* CompilationCacheTable::PutEval(String* src, |
| 14019 | Context* context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14020 | SharedFunctionInfo* value, |
| 14021 | int scope_position) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 14022 | StringSharedKey key(src, |
| 14023 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 14024 | value->language_mode(), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14025 | scope_position); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14026 | Object* obj; |
| 14027 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 14028 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14029 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14030 | |
| 14031 | CompilationCacheTable* cache = |
| 14032 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14033 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14034 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14035 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14036 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14037 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 14038 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14039 | |
| 14040 | cache->set(EntryToIndex(entry), k); |
| 14041 | cache->set(EntryToIndex(entry) + 1, value); |
| 14042 | cache->ElementAdded(); |
| 14043 | return cache; |
| 14044 | } |
| 14045 | |
| 14046 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14047 | MaybeObject* CompilationCacheTable::PutRegExp(String* src, |
| 14048 | JSRegExp::Flags flags, |
| 14049 | FixedArray* value) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14050 | RegExpKey key(src, flags); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14051 | Object* obj; |
| 14052 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 14053 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14054 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14055 | |
| 14056 | CompilationCacheTable* cache = |
| 14057 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14058 | int entry = cache->FindInsertionEntry(key.Hash()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 14059 | // We store the value in the key slot, and compare the search key |
| 14060 | // to the stored value with a custon IsMatch function during lookups. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14061 | cache->set(EntryToIndex(entry), value); |
| 14062 | cache->set(EntryToIndex(entry) + 1, value); |
| 14063 | cache->ElementAdded(); |
| 14064 | return cache; |
| 14065 | } |
| 14066 | |
| 14067 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14068 | void CompilationCacheTable::Remove(Object* value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14069 | Object* the_hole_value = GetHeap()->the_hole_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14070 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 14071 | int entry_index = EntryToIndex(entry); |
| 14072 | int value_index = entry_index + 1; |
| 14073 | if (get(value_index) == value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14074 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 14075 | NoWriteBarrierSet(this, value_index, the_hole_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14076 | ElementRemoved(); |
| 14077 | } |
| 14078 | } |
| 14079 | return; |
| 14080 | } |
| 14081 | |
| 14082 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14083 | // StringsKey used for HashTable where key is array of internalized strings. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14084 | class StringsKey : public HashTableKey { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14085 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14086 | explicit StringsKey(FixedArray* strings) : strings_(strings) { } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14087 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14088 | bool IsMatch(Object* strings) { |
| 14089 | FixedArray* o = FixedArray::cast(strings); |
| 14090 | int len = strings_->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14091 | if (o->length() != len) return false; |
| 14092 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14093 | if (o->get(i) != strings_->get(i)) return false; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14094 | } |
| 14095 | return true; |
| 14096 | } |
| 14097 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14098 | uint32_t Hash() { return HashForObject(strings_); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14099 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14100 | uint32_t HashForObject(Object* obj) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14101 | FixedArray* strings = FixedArray::cast(obj); |
| 14102 | int len = strings->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14103 | uint32_t hash = 0; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14104 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14105 | hash ^= String::cast(strings->get(i))->Hash(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14106 | } |
| 14107 | return hash; |
| 14108 | } |
| 14109 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14110 | Object* AsObject(Heap* heap) { return strings_; } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14111 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14112 | private: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14113 | FixedArray* strings_; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14114 | }; |
| 14115 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14116 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14117 | Object* MapCache::Lookup(FixedArray* array) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14118 | StringsKey key(array); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14119 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14120 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14121 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14122 | } |
| 14123 | |
| 14124 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14125 | MaybeObject* MapCache::Put(FixedArray* array, Map* value) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14126 | StringsKey key(array); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14127 | Object* obj; |
| 14128 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 14129 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14130 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14131 | |
| 14132 | MapCache* cache = reinterpret_cast<MapCache*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14133 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 14134 | cache->set(EntryToIndex(entry), array); |
| 14135 | cache->set(EntryToIndex(entry) + 1, value); |
| 14136 | cache->ElementAdded(); |
| 14137 | return cache; |
| 14138 | } |
| 14139 | |
| 14140 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14141 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14142 | MaybeObject* Dictionary<Shape, Key>::Allocate(Heap* heap, |
| 14143 | int at_least_space_for) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14144 | Object* obj; |
| 14145 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14146 | HashTable<Shape, Key>::Allocate(heap, at_least_space_for); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14147 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14148 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14149 | // Initialize the next enumeration index. |
| 14150 | Dictionary<Shape, Key>::cast(obj)-> |
| 14151 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14152 | return obj; |
| 14153 | } |
| 14154 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14155 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14156 | void NameDictionary::DoGenerateNewEnumerationIndices( |
| 14157 | Handle<NameDictionary> dictionary) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14158 | CALL_HEAP_FUNCTION_VOID(dictionary->GetIsolate(), |
| 14159 | dictionary->GenerateNewEnumerationIndices()); |
| 14160 | } |
| 14161 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14162 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14163 | MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14164 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14165 | int length = HashTable<Shape, Key>::NumberOfElements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14166 | |
| 14167 | // Allocate and initialize iteration order array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14168 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14169 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14170 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14171 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14172 | FixedArray* iteration_order = FixedArray::cast(obj); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14173 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 14174 | iteration_order->set(i, Smi::FromInt(i)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14176 | |
| 14177 | // Allocate array with enumeration order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14178 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14179 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14180 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14181 | FixedArray* enumeration_order = FixedArray::cast(obj); |
| 14182 | |
| 14183 | // Fill the enumeration order array with property details. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14184 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14185 | int pos = 0; |
| 14186 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14187 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 14188 | int index = DetailsAt(i).dictionary_index(); |
| 14189 | enumeration_order->set(pos++, Smi::FromInt(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14190 | } |
| 14191 | } |
| 14192 | |
| 14193 | // Sort the arrays wrt. enumeration order. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14194 | iteration_order->SortPairs(enumeration_order, enumeration_order->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14195 | |
| 14196 | // Overwrite the enumeration_order with the enumeration indices. |
| 14197 | for (int i = 0; i < length; i++) { |
| 14198 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 14199 | int enum_index = PropertyDetails::kInitialIndex + i; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 14200 | enumeration_order->set(index, Smi::FromInt(enum_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14201 | } |
| 14202 | |
| 14203 | // Update the dictionary with new indices. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14204 | capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14205 | pos = 0; |
| 14206 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14207 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14208 | int enum_index = Smi::cast(enumeration_order->get(pos++))->value(); |
| 14209 | PropertyDetails details = DetailsAt(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 14210 | PropertyDetails new_details = PropertyDetails( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14211 | details.attributes(), details.type(), enum_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14212 | DetailsAtPut(i, new_details); |
| 14213 | } |
| 14214 | } |
| 14215 | |
| 14216 | // Set the next enumeration index. |
| 14217 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
| 14218 | return this; |
| 14219 | } |
| 14220 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14221 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14222 | MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 14223 | // Check whether there are enough enumeration indices to add n elements. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14224 | if (Shape::kIsEnumerable && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14225 | !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) { |
| 14226 | // If not, we generate new indices for the properties. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14227 | Object* result; |
| 14228 | { MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 14229 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 14230 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14231 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14232 | return HashTable<Shape, Key>::EnsureCapacity(n, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14233 | } |
| 14234 | |
| 14235 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14236 | template<typename Shape, typename Key> |
| 14237 | Object* Dictionary<Shape, Key>::DeleteProperty(int entry, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 14238 | JSReceiver::DeleteMode mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14239 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14240 | PropertyDetails details = DetailsAt(entry); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14241 | // Ignore attributes if forcing a deletion. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 14242 | if (details.IsDontDelete() && mode != JSReceiver::FORCE_DELETION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14243 | return heap->false_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14244 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14245 | SetEntry(entry, heap->the_hole_value(), heap->the_hole_value()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14246 | HashTable<Shape, Key>::ElementRemoved(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14247 | return heap->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14248 | } |
| 14249 | |
| 14250 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14251 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14252 | MaybeObject* Dictionary<Shape, Key>::Shrink(Key key) { |
| 14253 | return HashTable<Shape, Key>::Shrink(key); |
| 14254 | } |
| 14255 | |
| 14256 | |
| 14257 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14258 | MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 14259 | int entry = this->FindEntry(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14260 | |
| 14261 | // If the entry is present set the value; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14262 | if (entry != Dictionary<Shape, Key>::kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14263 | ValueAtPut(entry, value); |
| 14264 | return this; |
| 14265 | } |
| 14266 | |
| 14267 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14268 | Object* obj; |
| 14269 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 14270 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14271 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14272 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14273 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14274 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14275 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 14276 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14277 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14278 | |
| 14279 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 14280 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14281 | } |
| 14282 | |
| 14283 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14284 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14285 | MaybeObject* Dictionary<Shape, Key>::Add(Key key, |
| 14286 | Object* value, |
| 14287 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14288 | // Valdate key is absent. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 14289 | SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14290 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14291 | Object* obj; |
| 14292 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 14293 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14294 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14295 | |
| 14296 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 14297 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14298 | } |
| 14299 | |
| 14300 | |
| 14301 | // Add a key, value pair to the dictionary. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14302 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14303 | MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key, |
| 14304 | Object* value, |
| 14305 | PropertyDetails details, |
| 14306 | uint32_t hash) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14307 | // Compute the key object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14308 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14309 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14310 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 14311 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14312 | |
| 14313 | uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14314 | // Insert element at empty or deleted entry |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14315 | if (!details.IsDeleted() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 14316 | details.dictionary_index() == 0 && |
| 14317 | Shape::kIsEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14318 | // Assign an enumeration index to the property and update |
| 14319 | // SetNextEnumerationIndex. |
| 14320 | int index = NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14321 | details = PropertyDetails(details.attributes(), details.type(), index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14322 | SetNextEnumerationIndex(index + 1); |
| 14323 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14324 | SetEntry(entry, k, value, details); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14325 | ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber() || |
| 14326 | Dictionary<Shape, Key>::KeyAt(entry)->IsName())); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14327 | HashTable<Shape, Key>::ElementAdded(); |
| 14328 | return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14329 | } |
| 14330 | |
| 14331 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14332 | void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14333 | // If the dictionary requires slow elements an element has already |
| 14334 | // been added at a high index. |
| 14335 | if (requires_slow_elements()) return; |
| 14336 | // Check if this index is high enough that we should require slow |
| 14337 | // elements. |
| 14338 | if (key > kRequiresSlowElementsLimit) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14339 | set_requires_slow_elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14340 | return; |
| 14341 | } |
| 14342 | // Update max key value. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14343 | Object* max_index_object = get(kMaxNumberKeyIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14344 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14345 | FixedArray::set(kMaxNumberKeyIndex, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 14346 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14347 | } |
| 14348 | } |
| 14349 | |
| 14350 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14351 | MaybeObject* SeededNumberDictionary::AddNumberEntry(uint32_t key, |
| 14352 | Object* value, |
| 14353 | PropertyDetails details) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14354 | UpdateMaxNumberKey(key); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 14355 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14356 | return Add(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14357 | } |
| 14358 | |
| 14359 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14360 | MaybeObject* UnseededNumberDictionary::AddNumberEntry(uint32_t key, |
| 14361 | Object* value) { |
| 14362 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14363 | return Add(key, value, PropertyDetails(NONE, NORMAL, 0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14364 | } |
| 14365 | |
| 14366 | |
| 14367 | MaybeObject* SeededNumberDictionary::AtNumberPut(uint32_t key, Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14368 | UpdateMaxNumberKey(key); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14369 | return AtPut(key, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14370 | } |
| 14371 | |
| 14372 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14373 | MaybeObject* UnseededNumberDictionary::AtNumberPut(uint32_t key, |
| 14374 | Object* value) { |
| 14375 | return AtPut(key, value); |
| 14376 | } |
| 14377 | |
| 14378 | |
| 14379 | Handle<SeededNumberDictionary> SeededNumberDictionary::Set( |
| 14380 | Handle<SeededNumberDictionary> dictionary, |
| 14381 | uint32_t index, |
| 14382 | Handle<Object> value, |
| 14383 | PropertyDetails details) { |
| 14384 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 14385 | dictionary->Set(index, *value, details), |
| 14386 | SeededNumberDictionary); |
| 14387 | } |
| 14388 | |
| 14389 | |
| 14390 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set( |
| 14391 | Handle<UnseededNumberDictionary> dictionary, |
| 14392 | uint32_t index, |
| 14393 | Handle<Object> value) { |
| 14394 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 14395 | dictionary->Set(index, *value), |
| 14396 | UnseededNumberDictionary); |
| 14397 | } |
| 14398 | |
| 14399 | |
| 14400 | MaybeObject* SeededNumberDictionary::Set(uint32_t key, |
| 14401 | Object* value, |
| 14402 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14403 | int entry = FindEntry(key); |
| 14404 | if (entry == kNotFound) return AddNumberEntry(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14405 | // Preserve enumeration index. |
| 14406 | details = PropertyDetails(details.attributes(), |
| 14407 | details.type(), |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14408 | DetailsAt(entry).dictionary_index()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14409 | MaybeObject* maybe_object_key = |
| 14410 | SeededNumberDictionaryShape::AsObject(GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14411 | Object* object_key; |
| 14412 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 14413 | SetEntry(entry, object_key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14414 | return this; |
| 14415 | } |
| 14416 | |
| 14417 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14418 | MaybeObject* UnseededNumberDictionary::Set(uint32_t key, |
| 14419 | Object* value) { |
| 14420 | int entry = FindEntry(key); |
| 14421 | if (entry == kNotFound) return AddNumberEntry(key, value); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14422 | MaybeObject* maybe_object_key = |
| 14423 | UnseededNumberDictionaryShape::AsObject(GetHeap(), key); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14424 | Object* object_key; |
| 14425 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
| 14426 | SetEntry(entry, object_key, value); |
| 14427 | return this; |
| 14428 | } |
| 14429 | |
| 14430 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14431 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14432 | template<typename Shape, typename Key> |
| 14433 | int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes( |
| 14434 | PropertyAttributes filter) { |
| 14435 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14436 | int result = 0; |
| 14437 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14438 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14439 | if (HashTable<Shape, Key>::IsKey(k) && |
| 14440 | ((filter & SYMBOLIC) == 0 || !k->IsSymbol())) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14441 | PropertyDetails details = DetailsAt(i); |
| 14442 | if (details.IsDeleted()) continue; |
| 14443 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14444 | if ((attr & filter) == 0) result++; |
| 14445 | } |
| 14446 | } |
| 14447 | return result; |
| 14448 | } |
| 14449 | |
| 14450 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14451 | template<typename Shape, typename Key> |
| 14452 | int Dictionary<Shape, Key>::NumberOfEnumElements() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14453 | return NumberOfElementsFilterAttributes( |
| 14454 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 14455 | } |
| 14456 | |
| 14457 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14458 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14459 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 14460 | FixedArray* storage, |
| 14461 | PropertyAttributes filter, |
| 14462 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14463 | ASSERT(storage->length() >= NumberOfEnumElements()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14464 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14465 | int index = 0; |
| 14466 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14467 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 14468 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14469 | PropertyDetails details = DetailsAt(i); |
| 14470 | if (details.IsDeleted()) continue; |
| 14471 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14472 | if ((attr & filter) == 0) storage->set(index++, k); |
| 14473 | } |
| 14474 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14475 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 14476 | storage->SortPairs(storage, index); |
| 14477 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14478 | ASSERT(storage->length() >= index); |
| 14479 | } |
| 14480 | |
| 14481 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14482 | FixedArray* NameDictionary::CopyEnumKeysTo(FixedArray* storage) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14483 | int length = storage->length(); |
| 14484 | ASSERT(length >= NumberOfEnumElements()); |
| 14485 | Heap* heap = GetHeap(); |
| 14486 | Object* undefined_value = heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14487 | int capacity = Capacity(); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14488 | int properties = 0; |
| 14489 | |
| 14490 | // Fill in the enumeration array by assigning enumerable keys at their |
| 14491 | // enumeration index. This will leave holes in the array if there are keys |
| 14492 | // that are deleted or not enumerable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14493 | for (int i = 0; i < capacity; i++) { |
| 14494 | Object* k = KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14495 | if (IsKey(k) && !k->IsSymbol()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14496 | PropertyDetails details = DetailsAt(i); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14497 | if (details.IsDeleted() || details.IsDontEnum()) continue; |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14498 | properties++; |
| 14499 | storage->set(details.dictionary_index() - 1, k); |
| 14500 | if (properties == length) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14501 | } |
| 14502 | } |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14503 | |
| 14504 | // There are holes in the enumeration array if less properties were assigned |
| 14505 | // than the length of the array. If so, crunch all the existing properties |
| 14506 | // together by shifting them to the left (maintaining the enumeration order), |
| 14507 | // and trimming of the right side of the array. |
| 14508 | if (properties < length) { |
| 14509 | if (properties == 0) return heap->empty_fixed_array(); |
| 14510 | properties = 0; |
| 14511 | for (int i = 0; i < length; ++i) { |
| 14512 | Object* value = storage->get(i); |
| 14513 | if (value != undefined_value) { |
| 14514 | storage->set(properties, value); |
| 14515 | ++properties; |
| 14516 | } |
| 14517 | } |
| 14518 | RightTrimFixedArray<FROM_MUTATOR>(heap, storage, length - properties); |
| 14519 | } |
| 14520 | return storage; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14521 | } |
| 14522 | |
| 14523 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14524 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14525 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 14526 | FixedArray* storage, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 14527 | int index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 14528 | PropertyAttributes filter, |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14529 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14530 | ASSERT(storage->length() >= NumberOfElementsFilterAttributes( |
| 14531 | static_cast<PropertyAttributes>(NONE))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14532 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14533 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14534 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 14535 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14536 | PropertyDetails details = DetailsAt(i); |
| 14537 | if (details.IsDeleted()) continue; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 14538 | PropertyAttributes attr = details.attributes(); |
| 14539 | if ((attr & filter) == 0) storage->set(index++, k); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14540 | } |
| 14541 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14542 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 14543 | storage->SortPairs(storage, index); |
| 14544 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14545 | ASSERT(storage->length() >= index); |
| 14546 | } |
| 14547 | |
| 14548 | |
| 14549 | // Backwards lookup (slow). |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14550 | template<typename Shape, typename Key> |
| 14551 | Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) { |
| 14552 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14553 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14554 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 14555 | if (Dictionary<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14556 | Object* e = ValueAt(i); |
| 14557 | if (e->IsJSGlobalPropertyCell()) { |
| 14558 | e = JSGlobalPropertyCell::cast(e)->value(); |
| 14559 | } |
| 14560 | if (e == value) return k; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14561 | } |
| 14562 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14563 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14564 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14565 | } |
| 14566 | |
| 14567 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14568 | MaybeObject* NameDictionary::TransformPropertiesToFastFor( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14569 | JSObject* obj, int unused_property_fields) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14570 | // Make sure we preserve dictionary representation if there are too many |
| 14571 | // descriptors. |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14572 | int number_of_elements = NumberOfElements(); |
| 14573 | if (number_of_elements > DescriptorArray::kMaxNumberOfDescriptors) return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14574 | |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 14575 | if (number_of_elements != NextEnumerationIndex()) { |
| 14576 | MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 14577 | if (maybe_result->IsFailure()) return maybe_result; |
| 14578 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14579 | |
| 14580 | int instance_descriptor_length = 0; |
| 14581 | int number_of_fields = 0; |
| 14582 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14583 | Heap* heap = GetHeap(); |
| 14584 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14585 | // Compute the length of the instance descriptor. |
| 14586 | int capacity = Capacity(); |
| 14587 | for (int i = 0; i < capacity; i++) { |
| 14588 | Object* k = KeyAt(i); |
| 14589 | if (IsKey(k)) { |
| 14590 | Object* value = ValueAt(i); |
| 14591 | PropertyType type = DetailsAt(i).type(); |
| 14592 | ASSERT(type != FIELD); |
| 14593 | instance_descriptor_length++; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 14594 | if (type == NORMAL && !value->IsJSFunction()) { |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 14595 | number_of_fields += 1; |
| 14596 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14597 | } |
| 14598 | } |
| 14599 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 14600 | int inobject_props = obj->map()->inobject_properties(); |
| 14601 | |
| 14602 | // Allocate new map. |
| 14603 | Map* new_map; |
| 14604 | MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors(); |
| 14605 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 14606 | new_map->set_dictionary_map(false); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 14607 | |
| 14608 | if (instance_descriptor_length == 0) { |
| 14609 | ASSERT_LE(unused_property_fields, inobject_props); |
| 14610 | // Transform the object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14611 | new_map->set_unused_property_fields(inobject_props); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 14612 | obj->set_map(new_map); |
| 14613 | obj->set_properties(heap->empty_fixed_array()); |
| 14614 | // Check that it really works. |
| 14615 | ASSERT(obj->HasFastProperties()); |
| 14616 | return obj; |
| 14617 | } |
| 14618 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14619 | // Allocate the instance descriptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14620 | DescriptorArray* descriptors; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14621 | MaybeObject* maybe_descriptors = |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 14622 | DescriptorArray::Allocate(instance_descriptor_length); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14623 | if (!maybe_descriptors->To(&descriptors)) { |
| 14624 | return maybe_descriptors; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14625 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14626 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 14627 | DescriptorArray::WhitenessWitness witness(descriptors); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14628 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14629 | int number_of_allocated_fields = |
| 14630 | number_of_fields + unused_property_fields - inobject_props; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 14631 | if (number_of_allocated_fields < 0) { |
| 14632 | // There is enough inobject space for all fields (including unused). |
| 14633 | number_of_allocated_fields = 0; |
| 14634 | unused_property_fields = inobject_props - number_of_fields; |
| 14635 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14636 | |
| 14637 | // Allocate the fixed array for the fields. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14638 | FixedArray* fields; |
| 14639 | MaybeObject* maybe_fields = |
| 14640 | heap->AllocateFixedArray(number_of_allocated_fields); |
| 14641 | if (!maybe_fields->To(&fields)) return maybe_fields; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14642 | |
| 14643 | // Fill in the instance descriptor and the fields. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14644 | int current_offset = 0; |
| 14645 | for (int i = 0; i < capacity; i++) { |
| 14646 | Object* k = KeyAt(i); |
| 14647 | if (IsKey(k)) { |
| 14648 | Object* value = ValueAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14649 | Name* key; |
| 14650 | if (k->IsSymbol()) { |
| 14651 | key = Symbol::cast(k); |
| 14652 | } else { |
| 14653 | // Ensure the key is a unique name before writing into the |
| 14654 | // instance descriptor. |
| 14655 | MaybeObject* maybe_key = heap->InternalizeString(String::cast(k)); |
| 14656 | if (!maybe_key->To(&key)) return maybe_key; |
| 14657 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14658 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14659 | PropertyDetails details = DetailsAt(i); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14660 | int enumeration_index = details.dictionary_index(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14661 | PropertyType type = details.type(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14662 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 14663 | if (value->IsJSFunction()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14664 | ConstantFunctionDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14665 | JSFunction::cast(value), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14666 | details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14667 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14668 | } else if (type == NORMAL) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14669 | if (current_offset < inobject_props) { |
| 14670 | obj->InObjectPropertyAtPut(current_offset, |
| 14671 | value, |
| 14672 | UPDATE_WRITE_BARRIER); |
| 14673 | } else { |
| 14674 | int offset = current_offset - inobject_props; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14675 | fields->set(offset, value); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14676 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14677 | FieldDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14678 | current_offset++, |
| 14679 | details.attributes(), |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 14680 | // TODO(verwaest): value->OptimalRepresentation(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14681 | Representation::Tagged()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14682 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14683 | } else if (type == CALLBACKS) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14684 | CallbacksDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14685 | value, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame^] | 14686 | details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14687 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14688 | } else { |
| 14689 | UNREACHABLE(); |
| 14690 | } |
| 14691 | } |
| 14692 | } |
| 14693 | ASSERT(current_offset == number_of_fields); |
| 14694 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14695 | descriptors->Sort(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14696 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 14697 | new_map->InitializeDescriptors(descriptors); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 14698 | new_map->set_unused_property_fields(unused_property_fields); |
| 14699 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14700 | // Transform the object. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 14701 | obj->set_map(new_map); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 14702 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 14703 | obj->set_properties(fields); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14704 | ASSERT(obj->IsJSObject()); |
| 14705 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14706 | // Check that it really works. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14707 | ASSERT(obj->HasFastProperties()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 14708 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14709 | return obj; |
| 14710 | } |
| 14711 | |
| 14712 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14713 | bool ObjectHashSet::Contains(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14714 | ASSERT(IsKey(key)); |
| 14715 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14716 | // If the object does not have an identity hash, it was never used as a key. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14717 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 14718 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return false; |
| 14719 | } |
| 14720 | return (FindEntry(key) != kNotFound); |
| 14721 | } |
| 14722 | |
| 14723 | |
| 14724 | MaybeObject* ObjectHashSet::Add(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14725 | ASSERT(IsKey(key)); |
| 14726 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14727 | // Make sure the key object has an identity hash code. |
| 14728 | int hash; |
| 14729 | { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION); |
| 14730 | if (maybe_hash->IsFailure()) return maybe_hash; |
| 14731 | hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value(); |
| 14732 | } |
| 14733 | int entry = FindEntry(key); |
| 14734 | |
| 14735 | // Check whether key is already present. |
| 14736 | if (entry != kNotFound) return this; |
| 14737 | |
| 14738 | // Check whether the hash set should be extended and add entry. |
| 14739 | Object* obj; |
| 14740 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 14741 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14742 | } |
| 14743 | ObjectHashSet* table = ObjectHashSet::cast(obj); |
| 14744 | entry = table->FindInsertionEntry(hash); |
| 14745 | table->set(EntryToIndex(entry), key); |
| 14746 | table->ElementAdded(); |
| 14747 | return table; |
| 14748 | } |
| 14749 | |
| 14750 | |
| 14751 | MaybeObject* ObjectHashSet::Remove(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14752 | ASSERT(IsKey(key)); |
| 14753 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14754 | // If the object does not have an identity hash, it was never used as a key. |
| 14755 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 14756 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return this; |
| 14757 | } |
| 14758 | int entry = FindEntry(key); |
| 14759 | |
| 14760 | // Check whether key is actually present. |
| 14761 | if (entry == kNotFound) return this; |
| 14762 | |
| 14763 | // Remove entry and try to shrink this hash set. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14764 | set_the_hole(EntryToIndex(entry)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14765 | ElementRemoved(); |
| 14766 | return Shrink(key); |
| 14767 | } |
| 14768 | |
| 14769 | |
| 14770 | Object* ObjectHashTable::Lookup(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14771 | ASSERT(IsKey(key)); |
| 14772 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14773 | // If the object does not have an identity hash, it was never used as a key. |
| 14774 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 14775 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 14776 | return GetHeap()->the_hole_value(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14777 | } |
| 14778 | } |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14779 | int entry = FindEntry(key); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 14780 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14781 | return get(EntryToIndex(entry) + 1); |
| 14782 | } |
| 14783 | |
| 14784 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14785 | MaybeObject* ObjectHashTable::Put(Object* key, Object* value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14786 | ASSERT(IsKey(key)); |
| 14787 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14788 | // Make sure the key object has an identity hash code. |
| 14789 | int hash; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14790 | { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14791 | if (maybe_hash->IsFailure()) return maybe_hash; |
| 14792 | hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value(); |
| 14793 | } |
| 14794 | int entry = FindEntry(key); |
| 14795 | |
| 14796 | // Check whether to perform removal operation. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 14797 | if (value->IsTheHole()) { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14798 | if (entry == kNotFound) return this; |
| 14799 | RemoveEntry(entry); |
| 14800 | return Shrink(key); |
| 14801 | } |
| 14802 | |
| 14803 | // Key is already in table, just overwrite value. |
| 14804 | if (entry != kNotFound) { |
| 14805 | set(EntryToIndex(entry) + 1, value); |
| 14806 | return this; |
| 14807 | } |
| 14808 | |
| 14809 | // Check whether the hash table should be extended. |
| 14810 | Object* obj; |
| 14811 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 14812 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14813 | } |
| 14814 | ObjectHashTable* table = ObjectHashTable::cast(obj); |
| 14815 | table->AddEntry(table->FindInsertionEntry(hash), key, value); |
| 14816 | return table; |
| 14817 | } |
| 14818 | |
| 14819 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14820 | void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14821 | set(EntryToIndex(entry), key); |
| 14822 | set(EntryToIndex(entry) + 1, value); |
| 14823 | ElementAdded(); |
| 14824 | } |
| 14825 | |
| 14826 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14827 | void ObjectHashTable::RemoveEntry(int entry) { |
| 14828 | set_the_hole(EntryToIndex(entry)); |
| 14829 | set_the_hole(EntryToIndex(entry) + 1); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14830 | ElementRemoved(); |
| 14831 | } |
| 14832 | |
| 14833 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14834 | DeclaredAccessorDescriptorIterator::DeclaredAccessorDescriptorIterator( |
| 14835 | DeclaredAccessorDescriptor* descriptor) |
| 14836 | : array_(descriptor->serialized_data()->GetDataStartAddress()), |
| 14837 | length_(descriptor->serialized_data()->length()), |
| 14838 | offset_(0) { |
| 14839 | } |
| 14840 | |
| 14841 | |
| 14842 | const DeclaredAccessorDescriptorData* |
| 14843 | DeclaredAccessorDescriptorIterator::Next() { |
| 14844 | ASSERT(offset_ < length_); |
| 14845 | uint8_t* ptr = &array_[offset_]; |
| 14846 | ASSERT(reinterpret_cast<uintptr_t>(ptr) % sizeof(uintptr_t) == 0); |
| 14847 | const DeclaredAccessorDescriptorData* data = |
| 14848 | reinterpret_cast<const DeclaredAccessorDescriptorData*>(ptr); |
| 14849 | offset_ += sizeof(*data); |
| 14850 | ASSERT(offset_ <= length_); |
| 14851 | return data; |
| 14852 | } |
| 14853 | |
| 14854 | |
| 14855 | Handle<DeclaredAccessorDescriptor> DeclaredAccessorDescriptor::Create( |
| 14856 | Isolate* isolate, |
| 14857 | const DeclaredAccessorDescriptorData& descriptor, |
| 14858 | Handle<DeclaredAccessorDescriptor> previous) { |
| 14859 | int previous_length = |
| 14860 | previous.is_null() ? 0 : previous->serialized_data()->length(); |
| 14861 | int length = sizeof(descriptor) + previous_length; |
| 14862 | Handle<ByteArray> serialized_descriptor = |
| 14863 | isolate->factory()->NewByteArray(length); |
| 14864 | Handle<DeclaredAccessorDescriptor> value = |
| 14865 | isolate->factory()->NewDeclaredAccessorDescriptor(); |
| 14866 | value->set_serialized_data(*serialized_descriptor); |
| 14867 | // Copy in the data. |
| 14868 | { |
| 14869 | AssertNoAllocation no_allocation; |
| 14870 | uint8_t* array = serialized_descriptor->GetDataStartAddress(); |
| 14871 | if (previous_length != 0) { |
| 14872 | uint8_t* previous_array = |
| 14873 | previous->serialized_data()->GetDataStartAddress(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 14874 | OS::MemCopy(array, previous_array, previous_length); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14875 | array += previous_length; |
| 14876 | } |
| 14877 | ASSERT(reinterpret_cast<uintptr_t>(array) % sizeof(uintptr_t) == 0); |
| 14878 | DeclaredAccessorDescriptorData* data = |
| 14879 | reinterpret_cast<DeclaredAccessorDescriptorData*>(array); |
| 14880 | *data = descriptor; |
| 14881 | } |
| 14882 | return value; |
| 14883 | } |
| 14884 | |
| 14885 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14886 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14887 | // Check if there is a break point at this code position. |
| 14888 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 14889 | // Get the break point info object for this code position. |
| 14890 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 14891 | |
| 14892 | // If there is no break point info object or no break points in the break |
| 14893 | // point info object there is no break point at this code position. |
| 14894 | if (break_point_info->IsUndefined()) return false; |
| 14895 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 14896 | } |
| 14897 | |
| 14898 | |
| 14899 | // Get the break point info object for this code position. |
| 14900 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 14901 | // Find the index of the break point info object for this code position. |
| 14902 | int index = GetBreakPointInfoIndex(code_position); |
| 14903 | |
| 14904 | // Return the break point info object if any. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14905 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14906 | return BreakPointInfo::cast(break_points()->get(index)); |
| 14907 | } |
| 14908 | |
| 14909 | |
| 14910 | // Clear a break point at the specified code position. |
| 14911 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 14912 | int code_position, |
| 14913 | Handle<Object> break_point_object) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 14914 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 14915 | Isolate::Current()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14916 | if (break_point_info->IsUndefined()) return; |
| 14917 | BreakPointInfo::ClearBreakPoint( |
| 14918 | Handle<BreakPointInfo>::cast(break_point_info), |
| 14919 | break_point_object); |
| 14920 | } |
| 14921 | |
| 14922 | |
| 14923 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 14924 | int code_position, |
| 14925 | int source_position, |
| 14926 | int statement_position, |
| 14927 | Handle<Object> break_point_object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14928 | Isolate* isolate = Isolate::Current(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 14929 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 14930 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14931 | if (!break_point_info->IsUndefined()) { |
| 14932 | BreakPointInfo::SetBreakPoint( |
| 14933 | Handle<BreakPointInfo>::cast(break_point_info), |
| 14934 | break_point_object); |
| 14935 | return; |
| 14936 | } |
| 14937 | |
| 14938 | // Adding a new break point for a code position which did not have any |
| 14939 | // break points before. Try to find a free slot. |
| 14940 | int index = kNoBreakPointInfo; |
| 14941 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 14942 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 14943 | index = i; |
| 14944 | break; |
| 14945 | } |
| 14946 | } |
| 14947 | if (index == kNoBreakPointInfo) { |
| 14948 | // No free slot - extend break point info array. |
| 14949 | Handle<FixedArray> old_break_points = |
| 14950 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14951 | Handle<FixedArray> new_break_points = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14952 | isolate->factory()->NewFixedArray( |
| 14953 | old_break_points->length() + |
| 14954 | Debug::kEstimatedNofBreakPointsInFunction); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 14955 | |
| 14956 | debug_info->set_break_points(*new_break_points); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14957 | for (int i = 0; i < old_break_points->length(); i++) { |
| 14958 | new_break_points->set(i, old_break_points->get(i)); |
| 14959 | } |
| 14960 | index = old_break_points->length(); |
| 14961 | } |
| 14962 | ASSERT(index != kNoBreakPointInfo); |
| 14963 | |
| 14964 | // Allocate new BreakPointInfo object and set the break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14965 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 14966 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14967 | new_break_point_info->set_code_position(Smi::FromInt(code_position)); |
| 14968 | new_break_point_info->set_source_position(Smi::FromInt(source_position)); |
| 14969 | new_break_point_info-> |
| 14970 | set_statement_position(Smi::FromInt(statement_position)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14971 | new_break_point_info->set_break_point_objects( |
| 14972 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14973 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 14974 | debug_info->break_points()->set(index, *new_break_point_info); |
| 14975 | } |
| 14976 | |
| 14977 | |
| 14978 | // Get the break point objects for a code position. |
| 14979 | Object* DebugInfo::GetBreakPointObjects(int code_position) { |
| 14980 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 14981 | if (break_point_info->IsUndefined()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14982 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14983 | } |
| 14984 | return BreakPointInfo::cast(break_point_info)->break_point_objects(); |
| 14985 | } |
| 14986 | |
| 14987 | |
| 14988 | // Get the total number of break points. |
| 14989 | int DebugInfo::GetBreakPointCount() { |
| 14990 | if (break_points()->IsUndefined()) return 0; |
| 14991 | int count = 0; |
| 14992 | for (int i = 0; i < break_points()->length(); i++) { |
| 14993 | if (!break_points()->get(i)->IsUndefined()) { |
| 14994 | BreakPointInfo* break_point_info = |
| 14995 | BreakPointInfo::cast(break_points()->get(i)); |
| 14996 | count += break_point_info->GetBreakPointCount(); |
| 14997 | } |
| 14998 | } |
| 14999 | return count; |
| 15000 | } |
| 15001 | |
| 15002 | |
| 15003 | Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 15004 | Handle<Object> break_point_object) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15005 | Heap* heap = debug_info->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15006 | if (debug_info->break_points()->IsUndefined()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15007 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 15008 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 15009 | Handle<BreakPointInfo> break_point_info = |
| 15010 | Handle<BreakPointInfo>(BreakPointInfo::cast( |
| 15011 | debug_info->break_points()->get(i))); |
| 15012 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 15013 | break_point_object)) { |
| 15014 | return *break_point_info; |
| 15015 | } |
| 15016 | } |
| 15017 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15018 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15019 | } |
| 15020 | |
| 15021 | |
| 15022 | // Find the index of the break point info object for the specified code |
| 15023 | // position. |
| 15024 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 15025 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 15026 | for (int i = 0; i < break_points()->length(); i++) { |
| 15027 | if (!break_points()->get(i)->IsUndefined()) { |
| 15028 | BreakPointInfo* break_point_info = |
| 15029 | BreakPointInfo::cast(break_points()->get(i)); |
| 15030 | if (break_point_info->code_position()->value() == code_position) { |
| 15031 | return i; |
| 15032 | } |
| 15033 | } |
| 15034 | } |
| 15035 | return kNoBreakPointInfo; |
| 15036 | } |
| 15037 | |
| 15038 | |
| 15039 | // Remove the specified break point object. |
| 15040 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 15041 | Handle<Object> break_point_object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15042 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15043 | // If there are no break points just ignore. |
| 15044 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 15045 | // If there is a single break point clear it if it is the same. |
| 15046 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 15047 | if (break_point_info->break_point_objects() == *break_point_object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15048 | break_point_info->set_break_point_objects( |
| 15049 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15050 | } |
| 15051 | return; |
| 15052 | } |
| 15053 | // If there are multiple break points shrink the array |
| 15054 | ASSERT(break_point_info->break_point_objects()->IsFixedArray()); |
| 15055 | Handle<FixedArray> old_array = |
| 15056 | Handle<FixedArray>( |
| 15057 | FixedArray::cast(break_point_info->break_point_objects())); |
| 15058 | Handle<FixedArray> new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15059 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15060 | int found_count = 0; |
| 15061 | for (int i = 0; i < old_array->length(); i++) { |
| 15062 | if (old_array->get(i) == *break_point_object) { |
| 15063 | ASSERT(found_count == 0); |
| 15064 | found_count++; |
| 15065 | } else { |
| 15066 | new_array->set(i - found_count, old_array->get(i)); |
| 15067 | } |
| 15068 | } |
| 15069 | // If the break point was found in the list change it. |
| 15070 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 15071 | } |
| 15072 | |
| 15073 | |
| 15074 | // Add the specified break point object. |
| 15075 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 15076 | Handle<Object> break_point_object) { |
| 15077 | // If there was no break point objects before just set it. |
| 15078 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 15079 | break_point_info->set_break_point_objects(*break_point_object); |
| 15080 | return; |
| 15081 | } |
| 15082 | // If the break point object is the same as before just ignore. |
| 15083 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 15084 | // If there was one break point object before replace with array. |
| 15085 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15086 | Handle<FixedArray> array = FACTORY->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15087 | array->set(0, break_point_info->break_point_objects()); |
| 15088 | array->set(1, *break_point_object); |
| 15089 | break_point_info->set_break_point_objects(*array); |
| 15090 | return; |
| 15091 | } |
| 15092 | // If there was more than one break point before extend array. |
| 15093 | Handle<FixedArray> old_array = |
| 15094 | Handle<FixedArray>( |
| 15095 | FixedArray::cast(break_point_info->break_point_objects())); |
| 15096 | Handle<FixedArray> new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15097 | FACTORY->NewFixedArray(old_array->length() + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15098 | for (int i = 0; i < old_array->length(); i++) { |
| 15099 | // If the break point was there before just ignore. |
| 15100 | if (old_array->get(i) == *break_point_object) return; |
| 15101 | new_array->set(i, old_array->get(i)); |
| 15102 | } |
| 15103 | // Add the new break point. |
| 15104 | new_array->set(old_array->length(), *break_point_object); |
| 15105 | break_point_info->set_break_point_objects(*new_array); |
| 15106 | } |
| 15107 | |
| 15108 | |
| 15109 | bool BreakPointInfo::HasBreakPointObject( |
| 15110 | Handle<BreakPointInfo> break_point_info, |
| 15111 | Handle<Object> break_point_object) { |
| 15112 | // No break point. |
| 15113 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 15114 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15115 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 15116 | return break_point_info->break_point_objects() == *break_point_object; |
| 15117 | } |
| 15118 | // Multiple break points. |
| 15119 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 15120 | for (int i = 0; i < array->length(); i++) { |
| 15121 | if (array->get(i) == *break_point_object) { |
| 15122 | return true; |
| 15123 | } |
| 15124 | } |
| 15125 | return false; |
| 15126 | } |
| 15127 | |
| 15128 | |
| 15129 | // Get the number of break points. |
| 15130 | int BreakPointInfo::GetBreakPointCount() { |
| 15131 | // No break point. |
| 15132 | if (break_point_objects()->IsUndefined()) return 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 15133 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15134 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 15135 | // Multiple break points. |
| 15136 | return FixedArray::cast(break_point_objects())->length(); |
| 15137 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 15138 | #endif // ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15139 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15140 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 15141 | Object* JSDate::GetField(Object* object, Smi* index) { |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 15142 | return JSDate::cast(object)->DoGetField( |
| 15143 | static_cast<FieldIndex>(index->value())); |
| 15144 | } |
| 15145 | |
| 15146 | |
| 15147 | Object* JSDate::DoGetField(FieldIndex index) { |
| 15148 | ASSERT(index != kDateValue); |
| 15149 | |
| 15150 | DateCache* date_cache = GetIsolate()->date_cache(); |
| 15151 | |
| 15152 | if (index < kFirstUncachedField) { |
| 15153 | Object* stamp = cache_stamp(); |
| 15154 | if (stamp != date_cache->stamp() && stamp->IsSmi()) { |
| 15155 | // Since the stamp is not NaN, the value is also not NaN. |
| 15156 | int64_t local_time_ms = |
| 15157 | date_cache->ToLocal(static_cast<int64_t>(value()->Number())); |
| 15158 | SetLocalFields(local_time_ms, date_cache); |
| 15159 | } |
| 15160 | switch (index) { |
| 15161 | case kYear: return year(); |
| 15162 | case kMonth: return month(); |
| 15163 | case kDay: return day(); |
| 15164 | case kWeekday: return weekday(); |
| 15165 | case kHour: return hour(); |
| 15166 | case kMinute: return min(); |
| 15167 | case kSecond: return sec(); |
| 15168 | default: UNREACHABLE(); |
| 15169 | } |
| 15170 | } |
| 15171 | |
| 15172 | if (index >= kFirstUTCField) { |
| 15173 | return GetUTCField(index, value()->Number(), date_cache); |
| 15174 | } |
| 15175 | |
| 15176 | double time = value()->Number(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 15177 | if (std::isnan(time)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 15178 | |
| 15179 | int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time)); |
| 15180 | int days = DateCache::DaysFromTime(local_time_ms); |
| 15181 | |
| 15182 | if (index == kDays) return Smi::FromInt(days); |
| 15183 | |
| 15184 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 15185 | if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000); |
| 15186 | ASSERT(index == kTimeInDay); |
| 15187 | return Smi::FromInt(time_in_day_ms); |
| 15188 | } |
| 15189 | |
| 15190 | |
| 15191 | Object* JSDate::GetUTCField(FieldIndex index, |
| 15192 | double value, |
| 15193 | DateCache* date_cache) { |
| 15194 | ASSERT(index >= kFirstUTCField); |
| 15195 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 15196 | if (std::isnan(value)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 15197 | |
| 15198 | int64_t time_ms = static_cast<int64_t>(value); |
| 15199 | |
| 15200 | if (index == kTimezoneOffset) { |
| 15201 | return Smi::FromInt(date_cache->TimezoneOffset(time_ms)); |
| 15202 | } |
| 15203 | |
| 15204 | int days = DateCache::DaysFromTime(time_ms); |
| 15205 | |
| 15206 | if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days)); |
| 15207 | |
| 15208 | if (index <= kDayUTC) { |
| 15209 | int year, month, day; |
| 15210 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 15211 | if (index == kYearUTC) return Smi::FromInt(year); |
| 15212 | if (index == kMonthUTC) return Smi::FromInt(month); |
| 15213 | ASSERT(index == kDayUTC); |
| 15214 | return Smi::FromInt(day); |
| 15215 | } |
| 15216 | |
| 15217 | int time_in_day_ms = DateCache::TimeInDay(time_ms, days); |
| 15218 | switch (index) { |
| 15219 | case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000)); |
| 15220 | case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60); |
| 15221 | case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60); |
| 15222 | case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000); |
| 15223 | case kDaysUTC: return Smi::FromInt(days); |
| 15224 | case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms); |
| 15225 | default: UNREACHABLE(); |
| 15226 | } |
| 15227 | |
| 15228 | UNREACHABLE(); |
| 15229 | return NULL; |
| 15230 | } |
| 15231 | |
| 15232 | |
| 15233 | void JSDate::SetValue(Object* value, bool is_value_nan) { |
| 15234 | set_value(value); |
| 15235 | if (is_value_nan) { |
| 15236 | HeapNumber* nan = GetIsolate()->heap()->nan_value(); |
| 15237 | set_cache_stamp(nan, SKIP_WRITE_BARRIER); |
| 15238 | set_year(nan, SKIP_WRITE_BARRIER); |
| 15239 | set_month(nan, SKIP_WRITE_BARRIER); |
| 15240 | set_day(nan, SKIP_WRITE_BARRIER); |
| 15241 | set_hour(nan, SKIP_WRITE_BARRIER); |
| 15242 | set_min(nan, SKIP_WRITE_BARRIER); |
| 15243 | set_sec(nan, SKIP_WRITE_BARRIER); |
| 15244 | set_weekday(nan, SKIP_WRITE_BARRIER); |
| 15245 | } else { |
| 15246 | set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER); |
| 15247 | } |
| 15248 | } |
| 15249 | |
| 15250 | |
| 15251 | void JSDate::SetLocalFields(int64_t local_time_ms, DateCache* date_cache) { |
| 15252 | int days = DateCache::DaysFromTime(local_time_ms); |
| 15253 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 15254 | int year, month, day; |
| 15255 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 15256 | int weekday = date_cache->Weekday(days); |
| 15257 | int hour = time_in_day_ms / (60 * 60 * 1000); |
| 15258 | int min = (time_in_day_ms / (60 * 1000)) % 60; |
| 15259 | int sec = (time_in_day_ms / 1000) % 60; |
| 15260 | set_cache_stamp(date_cache->stamp()); |
| 15261 | set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
| 15262 | set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
| 15263 | set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
| 15264 | set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
| 15265 | set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
| 15266 | set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
| 15267 | set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
| 15268 | } |
| 15269 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15270 | } } // namespace v8::internal |