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" |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 31 | #include "allocation-site-scopes.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "api.h" |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 33 | #include "arguments.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | #include "bootstrapper.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 35 | #include "codegen.h" |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 36 | #include "cpu-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 38 | #include "deoptimizer.h" |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 39 | #include "date.h" |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 40 | #include "elements.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "execution.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | #include "full-codegen.h" |
| 43 | #include "hydrogen.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 44 | #include "isolate-inl.h" |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 45 | #include "log.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | #include "objects-inl.h" |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 47 | #include "objects-visiting.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 48 | #include "objects-visiting-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | #include "macro-assembler.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 50 | #include "mark-compact.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 51 | #include "safepoint-table.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | #include "string-stream.h" |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 53 | #include "utils.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 55 | #ifdef ENABLE_DISASSEMBLER |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 56 | #include "disasm.h" |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 57 | #include "disassembler.h" |
| 58 | #endif |
| 59 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 60 | namespace v8 { |
| 61 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 63 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 64 | MUST_USE_RESULT static MaybeObject* CreateJSValue(JSFunction* constructor, |
| 65 | Object* value) { |
| 66 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | { MaybeObject* maybe_result = |
| 68 | constructor->GetHeap()->AllocateJSObject(constructor); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 69 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 70 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | JSValue::cast(result)->set_value(value); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 76 | MaybeObject* Object::ToObject(Context* native_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | if (IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 78 | return CreateJSValue(native_context->number_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | } else if (IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 80 | return CreateJSValue(native_context->boolean_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | } else if (IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 82 | return CreateJSValue(native_context->string_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | } |
| 84 | ASSERT(IsJSObject()); |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 89 | MaybeObject* Object::ToObject(Isolate* isolate) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 90 | if (IsJSReceiver()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 91 | return this; |
| 92 | } else if (IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 93 | Context* native_context = isolate->context()->native_context(); |
| 94 | return CreateJSValue(native_context->number_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | } else if (IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 96 | Context* native_context = isolate->context()->native_context(); |
| 97 | return CreateJSValue(native_context->boolean_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | } else if (IsString()) { |
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()) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 102 | Context* native_context = isolate->context()->native_context(); |
| 103 | return CreateJSValue(native_context->symbol_function(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Throw a type error. |
| 107 | return Failure::InternalError(); |
| 108 | } |
| 109 | |
| 110 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 111 | bool Object::BooleanValue() { |
| 112 | if (IsBoolean()) return IsTrue(); |
| 113 | if (IsSmi()) return Smi::cast(this)->value() != 0; |
| 114 | if (IsUndefined() || IsNull()) return false; |
| 115 | if (IsUndetectableObject()) return false; // Undetectable object is false. |
| 116 | if (IsString()) return String::cast(this)->length() != 0; |
| 117 | if (IsHeapNumber()) return HeapNumber::cast(this)->HeapNumberBooleanValue(); |
| 118 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 122 | void Object::Lookup(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | Object* holder = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 124 | if (IsJSReceiver()) { |
| 125 | holder = this; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 126 | } else { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 127 | Context* native_context = result->isolate()->context()->native_context(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 128 | if (IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 129 | holder = native_context->number_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 130 | } else if (IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 131 | holder = native_context->string_function()->instance_prototype(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 132 | } else if (IsSymbol()) { |
| 133 | holder = native_context->symbol_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 134 | } else if (IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 135 | holder = native_context->boolean_function()->instance_prototype(); |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 136 | } else { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 137 | result->isolate()->PushStackTraceAndDie( |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 138 | 0xDEAD0000, this, JSReceiver::cast(this)->map(), 0xDEAD0001); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 139 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 141 | ASSERT(holder != NULL); // Cannot handle null or undefined. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 142 | JSReceiver::cast(holder)->Lookup(name, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 146 | Handle<Object> Object::GetPropertyWithReceiver( |
| 147 | Handle<Object> object, |
| 148 | Handle<Object> receiver, |
| 149 | Handle<Name> name, |
| 150 | PropertyAttributes* attributes) { |
| 151 | LookupResult lookup(name->GetIsolate()); |
| 152 | object->Lookup(*name, &lookup); |
| 153 | Handle<Object> result = |
| 154 | GetProperty(object, receiver, &lookup, name, attributes); |
| 155 | ASSERT(*attributes <= ABSENT); |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 160 | MaybeObject* Object::GetPropertyWithReceiver(Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 161 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 162 | PropertyAttributes* attributes) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 163 | LookupResult result(name->GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | Lookup(name, &result); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 165 | MaybeObject* value = GetProperty(receiver, &result, name, attributes); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 166 | ASSERT(*attributes <= ABSENT); |
| 167 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 171 | bool Object::ToInt32(int32_t* value) { |
| 172 | if (IsSmi()) { |
| 173 | *value = Smi::cast(this)->value(); |
| 174 | return true; |
| 175 | } |
| 176 | if (IsHeapNumber()) { |
| 177 | double num = HeapNumber::cast(this)->value(); |
| 178 | if (FastI2D(FastD2I(num)) == num) { |
| 179 | *value = FastD2I(num); |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | bool Object::ToUint32(uint32_t* value) { |
| 188 | if (IsSmi()) { |
| 189 | int num = Smi::cast(this)->value(); |
| 190 | if (num >= 0) { |
| 191 | *value = static_cast<uint32_t>(num); |
| 192 | return true; |
| 193 | } |
| 194 | } |
| 195 | if (IsHeapNumber()) { |
| 196 | double num = HeapNumber::cast(this)->value(); |
| 197 | if (num >= 0 && FastUI2D(FastD2UI(num)) == num) { |
| 198 | *value = FastD2UI(num); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 206 | template<typename To> |
| 207 | static inline To* CheckedCast(void *from) { |
| 208 | uintptr_t temp = reinterpret_cast<uintptr_t>(from); |
| 209 | ASSERT(temp % sizeof(To) == 0); |
| 210 | return reinterpret_cast<To*>(temp); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | static MaybeObject* PerformCompare(const BitmaskCompareDescriptor& descriptor, |
| 215 | char* ptr, |
| 216 | Heap* heap) { |
| 217 | uint32_t bitmask = descriptor.bitmask; |
| 218 | uint32_t compare_value = descriptor.compare_value; |
| 219 | uint32_t value; |
| 220 | switch (descriptor.size) { |
| 221 | case 1: |
| 222 | value = static_cast<uint32_t>(*CheckedCast<uint8_t>(ptr)); |
| 223 | compare_value &= 0xff; |
| 224 | bitmask &= 0xff; |
| 225 | break; |
| 226 | case 2: |
| 227 | value = static_cast<uint32_t>(*CheckedCast<uint16_t>(ptr)); |
| 228 | compare_value &= 0xffff; |
| 229 | bitmask &= 0xffff; |
| 230 | break; |
| 231 | case 4: |
| 232 | value = *CheckedCast<uint32_t>(ptr); |
| 233 | break; |
| 234 | default: |
| 235 | UNREACHABLE(); |
| 236 | return NULL; |
| 237 | } |
| 238 | return heap->ToBoolean((bitmask & value) == (bitmask & compare_value)); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | static MaybeObject* PerformCompare(const PointerCompareDescriptor& descriptor, |
| 243 | char* ptr, |
| 244 | Heap* heap) { |
| 245 | uintptr_t compare_value = |
| 246 | reinterpret_cast<uintptr_t>(descriptor.compare_value); |
| 247 | uintptr_t value = *CheckedCast<uintptr_t>(ptr); |
| 248 | return heap->ToBoolean(compare_value == value); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | static MaybeObject* GetPrimitiveValue( |
| 253 | const PrimitiveValueDescriptor& descriptor, |
| 254 | char* ptr, |
| 255 | Heap* heap) { |
| 256 | int32_t int32_value = 0; |
| 257 | switch (descriptor.data_type) { |
| 258 | case kDescriptorInt8Type: |
| 259 | int32_value = *CheckedCast<int8_t>(ptr); |
| 260 | break; |
| 261 | case kDescriptorUint8Type: |
| 262 | int32_value = *CheckedCast<uint8_t>(ptr); |
| 263 | break; |
| 264 | case kDescriptorInt16Type: |
| 265 | int32_value = *CheckedCast<int16_t>(ptr); |
| 266 | break; |
| 267 | case kDescriptorUint16Type: |
| 268 | int32_value = *CheckedCast<uint16_t>(ptr); |
| 269 | break; |
| 270 | case kDescriptorInt32Type: |
| 271 | int32_value = *CheckedCast<int32_t>(ptr); |
| 272 | break; |
| 273 | case kDescriptorUint32Type: { |
| 274 | uint32_t value = *CheckedCast<uint32_t>(ptr); |
| 275 | return heap->NumberFromUint32(value); |
| 276 | } |
| 277 | case kDescriptorBoolType: { |
| 278 | uint8_t byte = *CheckedCast<uint8_t>(ptr); |
| 279 | return heap->ToBoolean(byte & (0x1 << descriptor.bool_offset)); |
| 280 | } |
| 281 | case kDescriptorFloatType: { |
| 282 | float value = *CheckedCast<float>(ptr); |
| 283 | return heap->NumberFromDouble(value); |
| 284 | } |
| 285 | case kDescriptorDoubleType: { |
| 286 | double value = *CheckedCast<double>(ptr); |
| 287 | return heap->NumberFromDouble(value); |
| 288 | } |
| 289 | } |
| 290 | return heap->NumberFromInt32(int32_value); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | static MaybeObject* GetDeclaredAccessorProperty(Object* receiver, |
| 295 | DeclaredAccessorInfo* info, |
| 296 | Isolate* isolate) { |
| 297 | char* current = reinterpret_cast<char*>(receiver); |
| 298 | DeclaredAccessorDescriptorIterator iterator(info->descriptor()); |
| 299 | while (true) { |
| 300 | const DeclaredAccessorDescriptorData* data = iterator.Next(); |
| 301 | switch (data->type) { |
| 302 | case kDescriptorReturnObject: { |
| 303 | ASSERT(iterator.Complete()); |
| 304 | current = *CheckedCast<char*>(current); |
| 305 | return *CheckedCast<Object*>(current); |
| 306 | } |
| 307 | case kDescriptorPointerDereference: |
| 308 | ASSERT(!iterator.Complete()); |
| 309 | current = *reinterpret_cast<char**>(current); |
| 310 | break; |
| 311 | case kDescriptorPointerShift: |
| 312 | ASSERT(!iterator.Complete()); |
| 313 | current += data->pointer_shift_descriptor.byte_offset; |
| 314 | break; |
| 315 | case kDescriptorObjectDereference: { |
| 316 | ASSERT(!iterator.Complete()); |
| 317 | Object* object = CheckedCast<Object>(current); |
| 318 | int field = data->object_dereference_descriptor.internal_field; |
| 319 | Object* smi = JSObject::cast(object)->GetInternalField(field); |
| 320 | ASSERT(smi->IsSmi()); |
| 321 | current = reinterpret_cast<char*>(smi); |
| 322 | break; |
| 323 | } |
| 324 | case kDescriptorBitmaskCompare: |
| 325 | ASSERT(iterator.Complete()); |
| 326 | return PerformCompare(data->bitmask_compare_descriptor, |
| 327 | current, |
| 328 | isolate->heap()); |
| 329 | case kDescriptorPointerCompare: |
| 330 | ASSERT(iterator.Complete()); |
| 331 | return PerformCompare(data->pointer_compare_descriptor, |
| 332 | current, |
| 333 | isolate->heap()); |
| 334 | case kDescriptorPrimitiveValue: |
| 335 | ASSERT(iterator.Complete()); |
| 336 | return GetPrimitiveValue(data->primitive_value_descriptor, |
| 337 | current, |
| 338 | isolate->heap()); |
| 339 | } |
| 340 | } |
| 341 | UNREACHABLE(); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 346 | MaybeObject* JSObject::GetPropertyWithCallback(Object* receiver, |
| 347 | Object* structure, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 348 | Name* name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 349 | Isolate* isolate = name->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | // 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] | 351 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 352 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 353 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 355 | reinterpret_cast<AccessorDescriptor*>( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 356 | Foreign::cast(structure)->foreign_address()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 357 | MaybeObject* value = (callback->getter)(isolate, receiver, callback->data); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 358 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | return value; |
| 360 | } |
| 361 | |
| 362 | // api style callbacks. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 363 | if (structure->IsAccessorInfo()) { |
| 364 | if (!AccessorInfo::cast(structure)->IsCompatibleReceiver(receiver)) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 365 | Handle<Object> name_handle(name, isolate); |
| 366 | Handle<Object> receiver_handle(receiver, isolate); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 367 | Handle<Object> args[2] = { name_handle, receiver_handle }; |
| 368 | Handle<Object> error = |
| 369 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 370 | HandleVector(args, |
| 371 | ARRAY_SIZE(args))); |
| 372 | return isolate->Throw(*error); |
| 373 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 374 | // TODO(rossberg): Handling symbols in the API requires changing the API, |
| 375 | // so we do not support it for now. |
| 376 | if (name->IsSymbol()) return isolate->heap()->undefined_value(); |
| 377 | if (structure->IsDeclaredAccessorInfo()) { |
| 378 | return GetDeclaredAccessorProperty(receiver, |
| 379 | DeclaredAccessorInfo::cast(structure), |
| 380 | isolate); |
| 381 | } |
| 382 | ExecutableAccessorInfo* data = ExecutableAccessorInfo::cast(structure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 383 | Object* fun_obj = data->getter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 384 | v8::AccessorGetterCallback call_fun = |
| 385 | v8::ToCData<v8::AccessorGetterCallback>(fun_obj); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 386 | if (call_fun == NULL) return isolate->heap()->undefined_value(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 387 | HandleScope scope(isolate); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 388 | JSObject* self = JSObject::cast(receiver); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 389 | Handle<String> key(String::cast(name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 390 | LOG(isolate, ApiNamedPropertyAccess("load", self, name)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 391 | PropertyCallbackArguments args(isolate, data->data(), self, this); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 392 | v8::Handle<v8::Value> result = |
| 393 | args.Call(call_fun, v8::Utils::ToLocal(key)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 394 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 395 | if (result.IsEmpty()) { |
| 396 | return isolate->heap()->undefined_value(); |
| 397 | } |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 398 | Object* return_value = *v8::Utils::OpenHandle(*result); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 399 | return_value->VerifyApiCallResultType(); |
jkummerow@chromium.org | 67255be | 2012-09-05 16:44:50 +0000 | [diff] [blame] | 400 | return return_value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // __defineGetter__ callback |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 404 | if (structure->IsAccessorPair()) { |
| 405 | Object* getter = AccessorPair::cast(structure)->getter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 406 | if (getter->IsSpecFunction()) { |
| 407 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 408 | return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | } |
| 410 | // Getter is not a function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 411 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | UNREACHABLE(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 415 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 419 | MaybeObject* JSProxy::GetPropertyWithHandler(Object* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 420 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 421 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 422 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 423 | Handle<Object> receiver(receiver_raw, isolate); |
| 424 | Handle<Object> name(name_raw, isolate); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 425 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 426 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 427 | if (name->IsSymbol()) return isolate->heap()->undefined_value(); |
| 428 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 429 | Handle<Object> args[] = { receiver, name }; |
| 430 | Handle<Object> result = CallTrap( |
| 431 | "get", isolate->derived_get_trap(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 432 | if (isolate->has_pending_exception()) return Failure::Exception(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 433 | |
| 434 | return *result; |
| 435 | } |
| 436 | |
| 437 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 438 | Handle<Object> Object::GetProperty(Handle<Object> object, |
| 439 | Handle<Name> name) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 440 | // TODO(rossberg): The index test should not be here but in the GetProperty |
| 441 | // method (or somewhere else entirely). Needs more global clean-up. |
| 442 | uint32_t index; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 443 | Isolate* isolate = name->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 444 | if (name->AsArrayIndex(&index)) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 445 | return GetElement(isolate, object, index); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 446 | CALL_HEAP_FUNCTION(isolate, object->GetProperty(*name), Object); |
| 447 | } |
| 448 | |
| 449 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 450 | Handle<Object> Object::GetElement(Isolate* isolate, |
| 451 | Handle<Object> object, |
| 452 | uint32_t index) { |
| 453 | CALL_HEAP_FUNCTION(isolate, object->GetElement(isolate, index), Object); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 457 | MaybeObject* JSProxy::GetElementWithHandler(Object* receiver, |
| 458 | uint32_t index) { |
| 459 | String* name; |
| 460 | MaybeObject* maybe = GetHeap()->Uint32ToString(index); |
| 461 | if (!maybe->To<String>(&name)) return maybe; |
| 462 | return GetPropertyWithHandler(receiver, name); |
| 463 | } |
| 464 | |
| 465 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 466 | Handle<Object> JSProxy::SetElementWithHandler(Handle<JSProxy> proxy, |
| 467 | Handle<JSReceiver> receiver, |
| 468 | uint32_t index, |
| 469 | Handle<Object> value, |
| 470 | StrictModeFlag strict_mode) { |
| 471 | Isolate* isolate = proxy->GetIsolate(); |
| 472 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 473 | return SetPropertyWithHandler( |
| 474 | proxy, receiver, name, value, NONE, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 478 | bool JSProxy::HasElementWithHandler(Handle<JSProxy> proxy, uint32_t index) { |
| 479 | Isolate* isolate = proxy->GetIsolate(); |
| 480 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 481 | return HasPropertyWithHandler(proxy, name); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 485 | MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 486 | JSReceiver* getter) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 487 | Isolate* isolate = getter->GetIsolate(); |
| 488 | HandleScope scope(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 489 | Handle<JSReceiver> fun(getter); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 490 | Handle<Object> self(receiver, isolate); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 491 | #ifdef ENABLE_DEBUGGER_SUPPORT |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 492 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 493 | // Handle stepping into a getter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 494 | // TODO(rossberg): should this apply to getters that are function proxies? |
| 495 | if (debug->StepInActive() && fun->IsJSFunction()) { |
| 496 | debug->HandleStepIn( |
| 497 | Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 498 | } |
| 499 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 500 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 501 | bool has_pending_exception; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 502 | Handle<Object> result = Execution::Call( |
| 503 | isolate, fun, self, 0, NULL, &has_pending_exception, true); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 504 | // Check for pending exception and return the result. |
| 505 | if (has_pending_exception) return Failure::Exception(); |
| 506 | return *result; |
| 507 | } |
| 508 | |
| 509 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 510 | // TODO(yangguo): this should eventually replace the non-handlified version. |
| 511 | Handle<Object> JSObject::GetPropertyWithCallback(Handle<JSObject> object, |
| 512 | Handle<Object> receiver, |
| 513 | Handle<Object> structure, |
| 514 | Handle<Name> name) { |
| 515 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 516 | object->GetPropertyWithCallback(*receiver, |
| 517 | *structure, |
| 518 | *name), |
| 519 | Object); |
| 520 | } |
| 521 | |
| 522 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | // Only deal with CALLBACKS and INTERCEPTOR |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 524 | Handle<Object> JSObject::GetPropertyWithFailedAccessCheck( |
| 525 | Handle<JSObject> object, |
| 526 | Handle<Object> receiver, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 527 | LookupResult* result, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 528 | Handle<Name> name, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 529 | PropertyAttributes* attributes) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 530 | Isolate* isolate = name->GetIsolate(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 531 | if (result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | switch (result->type()) { |
| 533 | case CALLBACKS: { |
| 534 | // Only allow API accessors. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 535 | Handle<Object> callback_obj(result->GetCallbackObject(), isolate); |
| 536 | if (callback_obj->IsAccessorInfo()) { |
| 537 | if (!AccessorInfo::cast(*callback_obj)->all_can_read()) break; |
| 538 | *attributes = result->GetAttributes(); |
| 539 | // Fall through to GetPropertyWithCallback. |
| 540 | } else if (callback_obj->IsAccessorPair()) { |
| 541 | if (!AccessorPair::cast(*callback_obj)->all_can_read()) break; |
| 542 | // Fall through to GetPropertyWithCallback. |
| 543 | } else { |
| 544 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | } |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 546 | Handle<JSObject> holder(result->holder(), isolate); |
| 547 | return GetPropertyWithCallback(holder, receiver, callback_obj, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 548 | } |
| 549 | case NORMAL: |
| 550 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 551 | case CONSTANT: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | // Search ALL_CAN_READ accessors in prototype chain. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 553 | LookupResult r(isolate); |
| 554 | result->holder()->LookupRealNamedPropertyInPrototypes(*name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 555 | if (r.IsProperty()) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 556 | return GetPropertyWithFailedAccessCheck( |
| 557 | object, receiver, &r, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 558 | } |
| 559 | break; |
| 560 | } |
| 561 | case INTERCEPTOR: { |
| 562 | // If the object has an interceptor, try real named properties. |
| 563 | // No access check in GetPropertyAttributeWithInterceptor. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 564 | LookupResult r(isolate); |
| 565 | result->holder()->LookupRealNamedProperty(*name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 566 | if (r.IsProperty()) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 567 | return GetPropertyWithFailedAccessCheck( |
| 568 | object, receiver, &r, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 569 | } |
ager@chromium.org | 9ed6c32 | 2010-02-19 12:24:05 +0000 | [diff] [blame] | 570 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 571 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 572 | default: |
| 573 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 577 | // No accessible property found. |
| 578 | *attributes = ABSENT; |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 579 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_GET); |
| 580 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 581 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 585 | PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck( |
| 586 | Object* receiver, |
| 587 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 588 | Name* name, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 589 | bool continue_search) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 590 | if (result->IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 591 | switch (result->type()) { |
| 592 | case CALLBACKS: { |
| 593 | // Only allow API accessors. |
| 594 | Object* obj = result->GetCallbackObject(); |
| 595 | if (obj->IsAccessorInfo()) { |
| 596 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 597 | if (info->all_can_read()) { |
| 598 | return result->GetAttributes(); |
| 599 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 600 | } else if (obj->IsAccessorPair()) { |
| 601 | AccessorPair* pair = AccessorPair::cast(obj); |
| 602 | if (pair->all_can_read()) { |
| 603 | return result->GetAttributes(); |
| 604 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 605 | } |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | case NORMAL: |
| 610 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 611 | case CONSTANT: { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 612 | if (!continue_search) break; |
| 613 | // Search ALL_CAN_READ accessors in prototype chain. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 614 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 615 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 616 | if (r.IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 617 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 618 | &r, |
| 619 | name, |
| 620 | continue_search); |
| 621 | } |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | case INTERCEPTOR: { |
| 626 | // If the object has an interceptor, try real named properties. |
| 627 | // No access check in GetPropertyAttributeWithInterceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 628 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 629 | if (continue_search) { |
| 630 | result->holder()->LookupRealNamedProperty(name, &r); |
| 631 | } else { |
| 632 | result->holder()->LocalLookupRealNamedProperty(name, &r); |
| 633 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 634 | if (!r.IsFound()) break; |
| 635 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 636 | &r, |
| 637 | name, |
| 638 | continue_search); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 639 | } |
| 640 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 641 | case HANDLER: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 642 | case TRANSITION: |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 643 | case NONEXISTENT: |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 644 | UNREACHABLE(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 648 | GetIsolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 649 | return ABSENT; |
| 650 | } |
| 651 | |
| 652 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 653 | Object* JSObject::GetNormalizedProperty(LookupResult* result) { |
| 654 | ASSERT(!HasFastProperties()); |
| 655 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 656 | if (IsGlobalObject()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 657 | value = PropertyCell::cast(value)->value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 658 | } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 659 | ASSERT(!value->IsPropertyCell() && !value->IsCell()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 660 | return value; |
| 661 | } |
| 662 | |
| 663 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 664 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 665 | LookupResult* result, |
| 666 | Handle<Object> value) { |
| 667 | ASSERT(!object->HasFastProperties()); |
| 668 | NameDictionary* property_dictionary = object->property_dictionary(); |
| 669 | if (object->IsGlobalObject()) { |
| 670 | Handle<PropertyCell> cell(PropertyCell::cast( |
| 671 | property_dictionary->ValueAt(result->GetDictionaryEntry()))); |
| 672 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 673 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 674 | property_dictionary->ValueAtPut(result->GetDictionaryEntry(), *value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 675 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 679 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 680 | static Handle<NameDictionary> NameDictionaryAdd(Handle<NameDictionary> dict, |
| 681 | Handle<Name> name, |
| 682 | Handle<Object> value, |
| 683 | PropertyDetails details) { |
| 684 | CALL_HEAP_FUNCTION(dict->GetIsolate(), |
| 685 | dict->Add(*name, *value, details), |
| 686 | NameDictionary); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 690 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 691 | Handle<Name> name, |
| 692 | Handle<Object> value, |
| 693 | PropertyDetails details) { |
| 694 | ASSERT(!object->HasFastProperties()); |
| 695 | Handle<NameDictionary> property_dictionary(object->property_dictionary()); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 696 | |
| 697 | if (!name->IsUniqueName()) { |
| 698 | name = object->GetIsolate()->factory()->InternalizedStringFromString( |
| 699 | Handle<String>::cast(name)); |
| 700 | } |
| 701 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 702 | int entry = property_dictionary->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 703 | if (entry == NameDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 704 | Handle<Object> store_value = value; |
| 705 | if (object->IsGlobalObject()) { |
| 706 | store_value = object->GetIsolate()->factory()->NewPropertyCell(value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 707 | } |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 708 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 709 | property_dictionary = |
| 710 | NameDictionaryAdd(property_dictionary, name, store_value, details); |
| 711 | object->set_properties(*property_dictionary); |
| 712 | return; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 713 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 714 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 715 | PropertyDetails original_details = property_dictionary->DetailsAt(entry); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 716 | int enumeration_index; |
| 717 | // Preserve the enumeration index unless the property was deleted. |
| 718 | if (original_details.IsDeleted()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 719 | enumeration_index = property_dictionary->NextEnumerationIndex(); |
| 720 | property_dictionary->SetNextEnumerationIndex(enumeration_index + 1); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 721 | } else { |
| 722 | enumeration_index = original_details.dictionary_index(); |
| 723 | ASSERT(enumeration_index > 0); |
| 724 | } |
| 725 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 726 | details = PropertyDetails( |
| 727 | details.attributes(), details.type(), enumeration_index); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 728 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 729 | if (object->IsGlobalObject()) { |
| 730 | Handle<PropertyCell> cell( |
| 731 | PropertyCell::cast(property_dictionary->ValueAt(entry))); |
| 732 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 733 | // Please note we have to update the property details. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 734 | property_dictionary->DetailsAtPut(entry, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 735 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 736 | property_dictionary->SetEntry(entry, *name, *value, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 737 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 741 | // TODO(mstarzinger): Temporary wrapper until target is handlified. |
| 742 | Handle<NameDictionary> NameDictionaryShrink(Handle<NameDictionary> dict, |
| 743 | Handle<Name> name) { |
| 744 | CALL_HEAP_FUNCTION(dict->GetIsolate(), dict->Shrink(*name), NameDictionary); |
| 745 | } |
| 746 | |
| 747 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 748 | Handle<Object> JSObject::DeleteNormalizedProperty(Handle<JSObject> object, |
| 749 | Handle<Name> name, |
| 750 | DeleteMode mode) { |
| 751 | ASSERT(!object->HasFastProperties()); |
| 752 | Isolate* isolate = object->GetIsolate(); |
| 753 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 754 | int entry = dictionary->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 755 | if (entry != NameDictionary::kNotFound) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 756 | // If we have a global object set the cell to the hole. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 757 | if (object->IsGlobalObject()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 758 | PropertyDetails details = dictionary->DetailsAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 759 | if (details.IsDontDelete()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 760 | if (mode != FORCE_DELETION) return isolate->factory()->false_value(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 761 | // When forced to delete global properties, we have to make a |
| 762 | // map change to invalidate any ICs that think they can load |
| 763 | // from the DontDelete cell without checking if it contains |
| 764 | // the hole value. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 765 | Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 766 | ASSERT(new_map->is_dictionary_map()); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 767 | object->set_map(*new_map); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 768 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 769 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 770 | Handle<Object> value = isolate->factory()->the_hole_value(); |
| 771 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 772 | dictionary->DetailsAtPut(entry, details.AsDeleted()); |
| 773 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 774 | Handle<Object> deleted(dictionary->DeleteProperty(entry, mode), isolate); |
| 775 | if (*deleted == isolate->heap()->true_value()) { |
| 776 | Handle<NameDictionary> new_properties = |
| 777 | NameDictionaryShrink(dictionary, name); |
| 778 | object->set_properties(*new_properties); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 779 | } |
| 780 | return deleted; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 783 | return isolate->factory()->true_value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 787 | bool JSObject::IsDirty() { |
| 788 | Object* cons_obj = map()->constructor(); |
| 789 | if (!cons_obj->IsJSFunction()) |
| 790 | return true; |
| 791 | JSFunction* fun = JSFunction::cast(cons_obj); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 792 | if (!fun->shared()->IsApiFunction()) |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 793 | return true; |
| 794 | // If the object is fully fast case and has the same map it was |
| 795 | // created with then no changes can have been made to it. |
| 796 | return map() != fun->initial_map() |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 797 | || !HasFastObjectElements() |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 798 | || !HasFastProperties(); |
| 799 | } |
| 800 | |
| 801 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 802 | Handle<Object> Object::GetProperty(Handle<Object> object, |
| 803 | Handle<Object> receiver, |
| 804 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 805 | Handle<Name> key, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 806 | PropertyAttributes* attributes) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 807 | Isolate* isolate = result->isolate(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 808 | CALL_HEAP_FUNCTION( |
| 809 | isolate, |
| 810 | object->GetProperty(*receiver, result, *key, attributes), |
| 811 | Object); |
| 812 | } |
| 813 | |
| 814 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 815 | MaybeObject* Object::GetPropertyOrFail(Handle<Object> object, |
| 816 | Handle<Object> receiver, |
| 817 | LookupResult* result, |
| 818 | Handle<Name> key, |
| 819 | PropertyAttributes* attributes) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 820 | Isolate* isolate = result->isolate(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 821 | CALL_HEAP_FUNCTION_PASS_EXCEPTION( |
| 822 | isolate, |
| 823 | object->GetProperty(*receiver, result, *key, attributes)); |
| 824 | } |
| 825 | |
| 826 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 827 | // TODO(yangguo): handlify this and get rid of. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 828 | MaybeObject* Object::GetProperty(Object* receiver, |
| 829 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 830 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 831 | PropertyAttributes* attributes) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 832 | Isolate* isolate = name->GetIsolate(); |
| 833 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 834 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 835 | #ifdef DEBUG |
| 836 | // TODO(mstarzinger): Only because of the AssertNoContextChange, drop as soon |
| 837 | // as this method has been fully handlified. |
| 838 | HandleScope scope(isolate); |
| 839 | #endif |
| 840 | |
| 841 | // Make sure that the top context does not change when doing |
| 842 | // callbacks or interceptor calls. |
| 843 | AssertNoContextChange ncc(isolate); |
| 844 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | // Traverse the prototype chain from the current object (this) to |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 846 | // the holder and check for access rights. This avoids traversing the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 847 | // objects more than once in case of interceptors, because the |
| 848 | // holder will always be the interceptor holder and the search may |
| 849 | // only continue with a current object just after the interceptor |
| 850 | // holder in the prototype chain. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 851 | // Proxy handlers do not use the proxy's prototype, so we can skip this. |
| 852 | if (!result->IsHandler()) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 853 | Object* last = result->IsProperty() |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 854 | ? result->holder() |
| 855 | : Object::cast(heap->null_value()); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 856 | ASSERT(this != this->GetPrototype(isolate)); |
| 857 | for (Object* current = this; |
| 858 | true; |
| 859 | current = current->GetPrototype(isolate)) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 860 | if (current->IsAccessCheckNeeded()) { |
| 861 | // Check if we're allowed to read from the current object. Note |
| 862 | // that even though we may not actually end up loading the named |
| 863 | // property from the current object, we still check that we have |
| 864 | // access to it. |
| 865 | JSObject* checked = JSObject::cast(current); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 866 | if (!isolate->MayNamedAccess(checked, name, v8::ACCESS_GET)) { |
| 867 | HandleScope scope(isolate); |
| 868 | Handle<Object> value = JSObject::GetPropertyWithFailedAccessCheck( |
| 869 | handle(checked, isolate), |
| 870 | handle(receiver, isolate), |
| 871 | result, |
| 872 | handle(name, isolate), |
| 873 | attributes); |
| 874 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
| 875 | return *value; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 876 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 877 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 878 | // Stop traversing the chain once we reach the last object in the |
| 879 | // chain; either the holder of the result or null in case of an |
| 880 | // absent property. |
| 881 | if (current == last) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | } |
| 884 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 885 | if (!result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | *attributes = ABSENT; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 887 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 888 | } |
| 889 | *attributes = result->GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 890 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 891 | switch (result->type()) { |
| 892 | case NORMAL: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 893 | value = result->holder()->GetNormalizedProperty(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 894 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 895 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 896 | case FIELD: { |
| 897 | MaybeObject* maybe_result = result->holder()->FastPropertyAt( |
| 898 | result->representation(), |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 899 | result->GetFieldIndex().field_index()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 900 | if (!maybe_result->To(&value)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 901 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 902 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 903 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 904 | case CONSTANT: |
| 905 | return result->GetConstant(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 906 | case CALLBACKS: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 907 | return result->holder()->GetPropertyWithCallback( |
| 908 | receiver, result->GetCallbackObject(), name); |
| 909 | case HANDLER: |
| 910 | return result->proxy()->GetPropertyWithHandler(receiver, name); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 911 | case INTERCEPTOR: { |
| 912 | HandleScope scope(isolate); |
| 913 | Handle<Object> value = JSObject::GetPropertyWithInterceptor( |
| 914 | handle(result->holder(), isolate), |
| 915 | handle(receiver, isolate), |
| 916 | handle(name, isolate), |
| 917 | attributes); |
| 918 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
| 919 | return *value; |
| 920 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 921 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 922 | case NONEXISTENT: |
| 923 | UNREACHABLE(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 924 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 925 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 926 | UNREACHABLE(); |
| 927 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 931 | MaybeObject* Object::GetElementWithReceiver(Isolate* isolate, |
| 932 | Object* receiver, |
| 933 | uint32_t index) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 934 | Heap* heap = isolate->heap(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 935 | Object* holder = this; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 936 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 937 | // Iterate up the prototype chain until an element is found or the null |
| 938 | // prototype is encountered. |
| 939 | for (holder = this; |
| 940 | holder != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 941 | holder = holder->GetPrototype(isolate)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 942 | if (!holder->IsJSObject()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 943 | Context* native_context = isolate->context()->native_context(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 944 | if (holder->IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 945 | holder = native_context->number_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 946 | } else if (holder->IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 947 | holder = native_context->string_function()->instance_prototype(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 948 | } else if (holder->IsSymbol()) { |
| 949 | holder = native_context->symbol_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 950 | } else if (holder->IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 951 | holder = native_context->boolean_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 952 | } else if (holder->IsJSProxy()) { |
| 953 | return JSProxy::cast(holder)->GetElementWithHandler(receiver, index); |
| 954 | } else { |
| 955 | // Undefined and null have no indexed properties. |
| 956 | ASSERT(holder->IsUndefined() || holder->IsNull()); |
| 957 | return heap->undefined_value(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
| 961 | // Inline the case for JSObjects. Doing so significantly improves the |
| 962 | // performance of fetching elements where checking the prototype chain is |
| 963 | // necessary. |
| 964 | JSObject* js_object = JSObject::cast(holder); |
| 965 | |
| 966 | // Check access rights if needed. |
| 967 | if (js_object->IsAccessCheckNeeded()) { |
| 968 | Isolate* isolate = heap->isolate(); |
| 969 | if (!isolate->MayIndexedAccess(js_object, index, v8::ACCESS_GET)) { |
| 970 | isolate->ReportFailedAccessCheck(js_object, v8::ACCESS_GET); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 971 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 972 | return heap->undefined_value(); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | if (js_object->HasIndexedInterceptor()) { |
| 977 | return js_object->GetElementWithInterceptor(receiver, index); |
| 978 | } |
| 979 | |
| 980 | if (js_object->elements() != heap->empty_fixed_array()) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 981 | MaybeObject* result = js_object->GetElementsAccessor()->Get( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 982 | receiver, js_object, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 983 | if (result != heap->the_hole_value()) return result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 984 | } |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 985 | } |
| 986 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 987 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 991 | Object* Object::GetPrototype(Isolate* isolate) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 992 | if (IsSmi()) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 993 | Context* context = isolate->context()->native_context(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 994 | return context->number_function()->instance_prototype(); |
| 995 | } |
| 996 | |
| 997 | HeapObject* heap_object = HeapObject::cast(this); |
| 998 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 999 | // The object is either a number, a string, a boolean, |
| 1000 | // a real JS object, or a Harmony proxy. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1001 | if (heap_object->IsJSReceiver()) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1002 | return heap_object->map()->prototype(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1003 | } |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1004 | Context* context = isolate->context()->native_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1005 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1006 | if (heap_object->IsHeapNumber()) { |
| 1007 | return context->number_function()->instance_prototype(); |
| 1008 | } |
| 1009 | if (heap_object->IsString()) { |
| 1010 | return context->string_function()->instance_prototype(); |
| 1011 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1012 | if (heap_object->IsSymbol()) { |
| 1013 | return context->symbol_function()->instance_prototype(); |
| 1014 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1015 | if (heap_object->IsBoolean()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | return context->boolean_function()->instance_prototype(); |
| 1017 | } else { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1018 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1023 | MaybeObject* Object::GetHash(CreationFlag flag) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1024 | // 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] | 1025 | // a real JS object, or a Harmony proxy. |
| 1026 | if (IsNumber()) { |
| 1027 | uint32_t hash = ComputeLongHash(double_to_uint64(Number())); |
| 1028 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1029 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1030 | if (IsName()) { |
| 1031 | uint32_t hash = Name::cast(this)->Hash(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1032 | return Smi::FromInt(hash); |
| 1033 | } |
| 1034 | if (IsOddball()) { |
| 1035 | uint32_t hash = Oddball::cast(this)->to_string()->Hash(); |
| 1036 | return Smi::FromInt(hash); |
| 1037 | } |
| 1038 | if (IsJSReceiver()) { |
| 1039 | return JSReceiver::cast(this)->GetIdentityHash(flag); |
| 1040 | } |
| 1041 | |
| 1042 | UNREACHABLE(); |
| 1043 | return Smi::FromInt(0); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | bool Object::SameValue(Object* other) { |
| 1048 | if (other == this) return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1049 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1050 | // 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] | 1051 | // a real JS object, or a Harmony proxy. |
| 1052 | if (IsNumber() && other->IsNumber()) { |
| 1053 | double this_value = Number(); |
| 1054 | double other_value = other->Number(); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 1055 | bool equal = this_value == other_value; |
| 1056 | // SameValue(NaN, NaN) is true. |
| 1057 | if (!equal) return std::isnan(this_value) && std::isnan(other_value); |
| 1058 | // SameValue(0.0, -0.0) is false. |
| 1059 | return (this_value != 0) || ((1 / this_value) == (1 / other_value)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1060 | } |
| 1061 | if (IsString() && other->IsString()) { |
| 1062 | return String::cast(this)->Equals(String::cast(other)); |
| 1063 | } |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1068 | void Object::ShortPrint(FILE* out) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1069 | HeapStringAllocator allocator; |
| 1070 | StringStream accumulator(&allocator); |
| 1071 | ShortPrint(&accumulator); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1072 | accumulator.OutputToFile(out); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | void Object::ShortPrint(StringStream* accumulator) { |
| 1077 | if (IsSmi()) { |
| 1078 | Smi::cast(this)->SmiPrint(accumulator); |
| 1079 | } else if (IsFailure()) { |
| 1080 | Failure::cast(this)->FailurePrint(accumulator); |
| 1081 | } else { |
| 1082 | HeapObject::cast(this)->HeapObjectShortPrint(accumulator); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1087 | void Smi::SmiPrint(FILE* out) { |
| 1088 | PrintF(out, "%d", value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | void Smi::SmiPrint(StringStream* accumulator) { |
| 1093 | accumulator->Add("%d", value()); |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | void Failure::FailurePrint(StringStream* accumulator) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1098 | accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1102 | void Failure::FailurePrint(FILE* out) { |
| 1103 | PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1107 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 1108 | // English? Returns false for non-ASCII or words that don't start with |
| 1109 | // a capital letter. The a/an rule follows pronunciation in English. |
| 1110 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 1111 | // you speak a dialect you may well say "an 'istoric occasion". |
| 1112 | static bool AnWord(String* str) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1113 | if (str->length() == 0) return false; // A nothing. |
| 1114 | int c0 = str->Get(0); |
| 1115 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | if (c0 == 'U') { |
| 1117 | if (c1 > 'Z') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1118 | return true; // An Umpire, but a UTF8String, a U. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1121 | return true; // An Ape, an ABCBook. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1122 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 1123 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 1124 | c0 == 'S' || c0 == 'X')) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1125 | return true; // An MP3File, an M. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | } |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
| 1130 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1131 | MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1132 | #ifdef DEBUG |
| 1133 | // Do not attempt to flatten in debug mode when allocation is not |
| 1134 | // allowed. This is to avoid an assertion failure when allocating. |
| 1135 | // Flattening strings is the only case where we always allow |
| 1136 | // allocation because no GC is performed if the allocation fails. |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 1137 | if (!AllowHeapAllocation::IsAllowed()) return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1138 | #endif |
| 1139 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1140 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1141 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1142 | case kConsStringTag: { |
| 1143 | ConsString* cs = ConsString::cast(this); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1144 | if (cs->second()->length() == 0) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1145 | return cs->first(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1146 | } |
| 1147 | // There's little point in putting the flat string in new space if the |
| 1148 | // cons string is in old space. It can never get GCed until there is |
| 1149 | // an old space GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1150 | PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1151 | int len = length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1152 | Object* object; |
| 1153 | String* result; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1154 | if (IsOneByteRepresentation()) { |
| 1155 | { MaybeObject* maybe_object = |
| 1156 | heap->AllocateRawOneByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1157 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1158 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1159 | result = String::cast(object); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1160 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1161 | int first_length = first->length(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 1162 | uint8_t* dest = SeqOneByteString::cast(result)->GetChars(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1163 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1164 | String* second = cs->second(); |
| 1165 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1166 | dest + first_length, |
| 1167 | 0, |
| 1168 | len - first_length); |
| 1169 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1170 | { MaybeObject* maybe_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1171 | heap->AllocateRawTwoByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1172 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1173 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1174 | result = String::cast(object); |
| 1175 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1176 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1177 | int first_length = first->length(); |
| 1178 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1179 | String* second = cs->second(); |
| 1180 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1181 | dest + first_length, |
| 1182 | 0, |
| 1183 | len - first_length); |
| 1184 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1185 | cs->set_first(result); |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 1186 | cs->set_second(heap->empty_string(), SKIP_WRITE_BARRIER); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1187 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | } |
| 1189 | default: |
| 1190 | return this; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1195 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1196 | // Externalizing twice leaks the external resource, so it's |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1197 | // prohibited by the API. |
| 1198 | ASSERT(!this->IsExternalString()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1199 | #ifdef DEBUG |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1200 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1201 | // Assert that the resource and the string are equivalent. |
| 1202 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1203 | ScopedVector<uc16> smart_chars(this->length()); |
| 1204 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1205 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1206 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1207 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1208 | } |
| 1209 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1210 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1211 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1212 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1213 | return false; |
| 1214 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1215 | bool is_ascii = this->IsOneByteRepresentation(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1216 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Morph the object to an external string by adjusting the map and |
| 1219 | // reinitializing the fields. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1220 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1221 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1222 | is_internalized |
| 1223 | ? (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1224 | ? heap->external_internalized_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1225 | : heap->external_internalized_string_map()) |
| 1226 | : (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1227 | ? heap->external_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1228 | : heap->external_string_map())); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1229 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1230 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1231 | is_internalized |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1232 | ? (is_ascii |
| 1233 | ? heap-> |
| 1234 | short_external_internalized_string_with_one_byte_data_map() |
| 1235 | : heap->short_external_internalized_string_map()) |
| 1236 | : (is_ascii |
| 1237 | ? heap->short_external_string_with_one_byte_data_map() |
| 1238 | : heap->short_external_string_map())); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1239 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1240 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 1241 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1242 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Fill the remainder of the string with dead wood. |
| 1245 | 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] | 1246 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1247 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1248 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1249 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1250 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1251 | return true; |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { |
| 1256 | #ifdef DEBUG |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1257 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1258 | // Assert that the resource and the string are equivalent. |
| 1259 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 1260 | if (this->IsTwoByteRepresentation()) { |
| 1261 | ScopedVector<uint16_t> smart_chars(this->length()); |
| 1262 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1263 | ASSERT(String::IsOneByte(smart_chars.start(), this->length())); |
| 1264 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1265 | ScopedVector<char> smart_chars(this->length()); |
| 1266 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1267 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1268 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1269 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1270 | } |
| 1271 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1272 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1273 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1274 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1277 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1278 | |
| 1279 | // 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] | 1280 | // reinitializing the fields. Use short version if space is limited. |
| 1281 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1282 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1283 | is_internalized ? heap->external_ascii_internalized_string_map() |
| 1284 | : heap->external_ascii_string_map()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1285 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1286 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1287 | is_internalized ? heap->short_external_ascii_internalized_string_map() |
| 1288 | : heap->short_external_ascii_string_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1289 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1290 | ExternalAsciiString* self = ExternalAsciiString::cast(this); |
| 1291 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1292 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1293 | |
| 1294 | // Fill the remainder of the string with dead wood. |
| 1295 | 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] | 1296 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1297 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1298 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1299 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1300 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1301 | return true; |
| 1302 | } |
| 1303 | |
| 1304 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1305 | void String::StringShortPrint(StringStream* accumulator) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1306 | int len = length(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1307 | if (len > kMaxShortPrintLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | accumulator->Add("<Very long string[%u]>", len); |
| 1309 | return; |
| 1310 | } |
| 1311 | |
| 1312 | if (!LooksValid()) { |
| 1313 | accumulator->Add("<Invalid String>"); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1317 | ConsStringIteratorOp op; |
| 1318 | StringCharacterStream stream(this, &op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | |
| 1320 | bool truncated = false; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1321 | if (len > kMaxShortPrintLength) { |
| 1322 | len = kMaxShortPrintLength; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1323 | truncated = true; |
| 1324 | } |
| 1325 | bool ascii = true; |
| 1326 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1327 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (c < 32 || c >= 127) { |
| 1330 | ascii = false; |
| 1331 | } |
| 1332 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1333 | stream.Reset(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1334 | if (ascii) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1335 | accumulator->Add("<String[%u]: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1336 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1337 | accumulator->Put(static_cast<char>(stream.GetNext())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | accumulator->Put('>'); |
| 1340 | } else { |
| 1341 | // Backslash indicates that the string contains control |
| 1342 | // characters and that backslashes are therefore escaped. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1343 | accumulator->Add("<String[%u]\\: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1345 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | if (c == '\n') { |
| 1347 | accumulator->Add("\\n"); |
| 1348 | } else if (c == '\r') { |
| 1349 | accumulator->Add("\\r"); |
| 1350 | } else if (c == '\\') { |
| 1351 | accumulator->Add("\\\\"); |
| 1352 | } else if (c < 32 || c > 126) { |
| 1353 | accumulator->Add("\\x%02x", c); |
| 1354 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1355 | accumulator->Put(static_cast<char>(c)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | if (truncated) { |
| 1359 | accumulator->Put('.'); |
| 1360 | accumulator->Put('.'); |
| 1361 | accumulator->Put('.'); |
| 1362 | } |
| 1363 | accumulator->Put('>'); |
| 1364 | } |
| 1365 | return; |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 1370 | switch (map()->instance_type()) { |
| 1371 | case JS_ARRAY_TYPE: { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1372 | double length = JSArray::cast(this)->length()->IsUndefined() |
| 1373 | ? 0 |
| 1374 | : JSArray::cast(this)->length()->Number(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1375 | accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1378 | case JS_WEAK_MAP_TYPE: { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1379 | accumulator->Add("<JS WeakMap>"); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1382 | case JS_WEAK_SET_TYPE: { |
| 1383 | accumulator->Add("<JS WeakSet>"); |
| 1384 | break; |
| 1385 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1386 | case JS_REGEXP_TYPE: { |
| 1387 | accumulator->Add("<JS RegExp>"); |
| 1388 | break; |
| 1389 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | case JS_FUNCTION_TYPE: { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1391 | JSFunction* function = JSFunction::cast(this); |
| 1392 | Object* fun_name = function->shared()->DebugName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | bool printed = false; |
| 1394 | if (fun_name->IsString()) { |
| 1395 | String* str = String::cast(fun_name); |
| 1396 | if (str->length() > 0) { |
| 1397 | accumulator->Add("<JS Function "); |
| 1398 | accumulator->Put(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | printed = true; |
| 1400 | } |
| 1401 | } |
| 1402 | if (!printed) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1403 | accumulator->Add("<JS Function"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1404 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1405 | accumulator->Add(" (SharedFunctionInfo %p)", |
| 1406 | reinterpret_cast<void*>(function->shared())); |
| 1407 | accumulator->Put('>'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1410 | case JS_GENERATOR_OBJECT_TYPE: { |
| 1411 | accumulator->Add("<JS Generator>"); |
| 1412 | break; |
| 1413 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1414 | case JS_MODULE_TYPE: { |
| 1415 | accumulator->Add("<JS Module>"); |
| 1416 | break; |
| 1417 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1418 | // All other JSObjects are rather similar to each other (JSObject, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1419 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1420 | default: { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1421 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1422 | Heap* heap = GetHeap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1423 | Object* constructor = map_of_this->constructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1424 | bool printed = false; |
| 1425 | if (constructor->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1426 | !heap->Contains(HeapObject::cast(constructor))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1427 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 1428 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1429 | bool global_object = IsJSGlobalProxy(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | if (constructor->IsJSFunction()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1431 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1432 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 1433 | } else { |
| 1434 | Object* constructor_name = |
| 1435 | JSFunction::cast(constructor)->shared()->name(); |
| 1436 | if (constructor_name->IsString()) { |
| 1437 | String* str = String::cast(constructor_name); |
| 1438 | if (str->length() > 0) { |
| 1439 | bool vowel = AnWord(str); |
| 1440 | accumulator->Add("<%sa%s ", |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1441 | global_object ? "Global Object: " : "", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1442 | vowel ? "n" : ""); |
| 1443 | accumulator->Put(str); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1444 | accumulator->Add(" with %smap %p", |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1445 | map_of_this->is_deprecated() ? "deprecated " : "", |
| 1446 | map_of_this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1447 | printed = true; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | if (!printed) { |
| 1453 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 1454 | } |
| 1455 | } |
| 1456 | if (IsJSValue()) { |
| 1457 | accumulator->Add(" value = "); |
| 1458 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 1459 | } |
| 1460 | accumulator->Put('>'); |
| 1461 | break; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1467 | void JSObject::PrintElementsTransition( |
| 1468 | FILE* file, ElementsKind from_kind, FixedArrayBase* from_elements, |
| 1469 | ElementsKind to_kind, FixedArrayBase* to_elements) { |
| 1470 | if (from_kind != to_kind) { |
| 1471 | PrintF(file, "elements transition ["); |
| 1472 | PrintElementsKind(file, from_kind); |
| 1473 | PrintF(file, " -> "); |
| 1474 | PrintElementsKind(file, to_kind); |
| 1475 | PrintF(file, "] in "); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1476 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1477 | PrintF(file, " for "); |
| 1478 | ShortPrint(file); |
| 1479 | PrintF(file, " from "); |
| 1480 | from_elements->ShortPrint(file); |
| 1481 | PrintF(file, " to "); |
| 1482 | to_elements->ShortPrint(file); |
| 1483 | PrintF(file, "\n"); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1488 | void Map::PrintGeneralization(FILE* file, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1489 | const char* reason, |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1490 | int modify_index, |
| 1491 | int split, |
| 1492 | int descriptors, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1493 | bool constant_to_field, |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1494 | Representation old_representation, |
| 1495 | Representation new_representation) { |
| 1496 | PrintF(file, "[generalizing "); |
| 1497 | constructor_name()->PrintOn(file); |
| 1498 | PrintF(file, "] "); |
| 1499 | String::cast(instance_descriptors()->GetKey(modify_index))->PrintOn(file); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1500 | if (constant_to_field) { |
| 1501 | PrintF(file, ":c->f"); |
| 1502 | } else { |
| 1503 | PrintF(file, ":%s->%s", |
| 1504 | old_representation.Mnemonic(), |
| 1505 | new_representation.Mnemonic()); |
| 1506 | } |
| 1507 | PrintF(file, " ("); |
| 1508 | if (strlen(reason) > 0) { |
| 1509 | PrintF(file, "%s", reason); |
| 1510 | } else { |
| 1511 | PrintF(file, "+%i maps", descriptors - split); |
| 1512 | } |
| 1513 | PrintF(file, ") ["); |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1514 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 1515 | PrintF(file, "]\n"); |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | void JSObject::PrintInstanceMigration(FILE* file, |
| 1520 | Map* original_map, |
| 1521 | Map* new_map) { |
| 1522 | PrintF(file, "[migrating "); |
| 1523 | map()->constructor_name()->PrintOn(file); |
| 1524 | PrintF(file, "] "); |
| 1525 | DescriptorArray* o = original_map->instance_descriptors(); |
| 1526 | DescriptorArray* n = new_map->instance_descriptors(); |
| 1527 | for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) { |
| 1528 | Representation o_r = o->GetDetails(i).representation(); |
| 1529 | Representation n_r = n->GetDetails(i).representation(); |
| 1530 | if (!o_r.Equals(n_r)) { |
| 1531 | String::cast(o->GetKey(i))->PrintOn(file); |
| 1532 | PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic()); |
| 1533 | } else if (o->GetDetails(i).type() == CONSTANT && |
| 1534 | n->GetDetails(i).type() == FIELD) { |
| 1535 | Name* name = o->GetKey(i); |
| 1536 | if (name->IsString()) { |
| 1537 | String::cast(name)->PrintOn(file); |
| 1538 | } else { |
| 1539 | PrintF(file, "???"); |
| 1540 | } |
| 1541 | PrintF(file, " "); |
| 1542 | } |
| 1543 | } |
| 1544 | PrintF(file, "\n"); |
| 1545 | } |
| 1546 | |
| 1547 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1548 | void HeapObject::HeapObjectShortPrint(StringStream* accumulator) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1549 | Heap* heap = GetHeap(); |
| 1550 | if (!heap->Contains(this)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1551 | accumulator->Add("!!!INVALID POINTER!!!"); |
| 1552 | return; |
| 1553 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1554 | if (!heap->Contains(map())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | accumulator->Add("!!!INVALID MAP!!!"); |
| 1556 | return; |
| 1557 | } |
| 1558 | |
| 1559 | accumulator->Add("%p ", this); |
| 1560 | |
| 1561 | if (IsString()) { |
| 1562 | String::cast(this)->StringShortPrint(accumulator); |
| 1563 | return; |
| 1564 | } |
| 1565 | if (IsJSObject()) { |
| 1566 | JSObject::cast(this)->JSObjectShortPrint(accumulator); |
| 1567 | return; |
| 1568 | } |
| 1569 | switch (map()->instance_type()) { |
| 1570 | case MAP_TYPE: |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1571 | accumulator->Add("<Map(elements=%u)>", Map::cast(this)->elements_kind()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1572 | break; |
| 1573 | case FIXED_ARRAY_TYPE: |
| 1574 | accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length()); |
| 1575 | break; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1576 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1577 | accumulator->Add("<FixedDoubleArray[%u]>", |
| 1578 | FixedDoubleArray::cast(this)->length()); |
| 1579 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | case BYTE_ARRAY_TYPE: |
| 1581 | accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length()); |
| 1582 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1583 | case FREE_SPACE_TYPE: |
| 1584 | accumulator->Add("<FreeSpace[%u]>", FreeSpace::cast(this)->Size()); |
| 1585 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1586 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 1587 | accumulator->Add("<ExternalPixelArray[%u]>", |
| 1588 | ExternalPixelArray::cast(this)->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1589 | break; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1590 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1591 | accumulator->Add("<ExternalByteArray[%u]>", |
| 1592 | ExternalByteArray::cast(this)->length()); |
| 1593 | break; |
| 1594 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1595 | accumulator->Add("<ExternalUnsignedByteArray[%u]>", |
| 1596 | ExternalUnsignedByteArray::cast(this)->length()); |
| 1597 | break; |
| 1598 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1599 | accumulator->Add("<ExternalShortArray[%u]>", |
| 1600 | ExternalShortArray::cast(this)->length()); |
| 1601 | break; |
| 1602 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1603 | accumulator->Add("<ExternalUnsignedShortArray[%u]>", |
| 1604 | ExternalUnsignedShortArray::cast(this)->length()); |
| 1605 | break; |
| 1606 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1607 | accumulator->Add("<ExternalIntArray[%u]>", |
| 1608 | ExternalIntArray::cast(this)->length()); |
| 1609 | break; |
| 1610 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1611 | accumulator->Add("<ExternalUnsignedIntArray[%u]>", |
| 1612 | ExternalUnsignedIntArray::cast(this)->length()); |
| 1613 | break; |
| 1614 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 1615 | accumulator->Add("<ExternalFloatArray[%u]>", |
| 1616 | ExternalFloatArray::cast(this)->length()); |
| 1617 | break; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1618 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 1619 | accumulator->Add("<ExternalDoubleArray[%u]>", |
| 1620 | ExternalDoubleArray::cast(this)->length()); |
| 1621 | break; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1622 | case SHARED_FUNCTION_INFO_TYPE: { |
| 1623 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(this); |
| 1624 | SmartArrayPointer<char> debug_name = |
| 1625 | shared->DebugName()->ToCString(); |
| 1626 | if (debug_name[0] != 0) { |
| 1627 | accumulator->Add("<SharedFunctionInfo %s>", *debug_name); |
| 1628 | } else { |
| 1629 | accumulator->Add("<SharedFunctionInfo>"); |
| 1630 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1631 | break; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1632 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1633 | case JS_MESSAGE_OBJECT_TYPE: |
| 1634 | accumulator->Add("<JSMessageObject>"); |
| 1635 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1636 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1637 | case NAME##_TYPE: \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1638 | accumulator->Put('<'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | accumulator->Add(#Name); \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1640 | accumulator->Put('>'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1641 | break; |
| 1642 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1643 | #undef MAKE_STRUCT_CASE |
| 1644 | case CODE_TYPE: |
| 1645 | accumulator->Add("<Code>"); |
| 1646 | break; |
| 1647 | case ODDBALL_TYPE: { |
| 1648 | if (IsUndefined()) |
| 1649 | accumulator->Add("<undefined>"); |
| 1650 | else if (IsTheHole()) |
| 1651 | accumulator->Add("<the hole>"); |
| 1652 | else if (IsNull()) |
| 1653 | accumulator->Add("<null>"); |
| 1654 | else if (IsTrue()) |
| 1655 | accumulator->Add("<true>"); |
| 1656 | else if (IsFalse()) |
| 1657 | accumulator->Add("<false>"); |
| 1658 | else |
| 1659 | accumulator->Add("<Odd Oddball>"); |
| 1660 | break; |
| 1661 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1662 | case SYMBOL_TYPE: { |
| 1663 | Symbol* symbol = Symbol::cast(this); |
| 1664 | accumulator->Add("<Symbol: %d", symbol->Hash()); |
| 1665 | if (!symbol->name()->IsUndefined()) { |
| 1666 | accumulator->Add(" "); |
| 1667 | String::cast(symbol->name())->StringShortPrint(accumulator); |
| 1668 | } |
| 1669 | accumulator->Add(">"); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1670 | break; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1671 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1672 | case HEAP_NUMBER_TYPE: |
| 1673 | accumulator->Add("<Number: "); |
| 1674 | HeapNumber::cast(this)->HeapNumberPrint(accumulator); |
| 1675 | accumulator->Put('>'); |
| 1676 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1677 | case JS_PROXY_TYPE: |
| 1678 | accumulator->Add("<JSProxy>"); |
| 1679 | break; |
| 1680 | case JS_FUNCTION_PROXY_TYPE: |
| 1681 | accumulator->Add("<JSFunctionProxy>"); |
| 1682 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1683 | case FOREIGN_TYPE: |
| 1684 | accumulator->Add("<Foreign>"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1685 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1686 | case CELL_TYPE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1687 | accumulator->Add("Cell for "); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1688 | Cell::cast(this)->value()->ShortPrint(accumulator); |
| 1689 | break; |
| 1690 | case PROPERTY_CELL_TYPE: |
| 1691 | accumulator->Add("PropertyCell for "); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1692 | PropertyCell::cast(this)->value()->ShortPrint(accumulator); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1693 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1694 | default: |
| 1695 | accumulator->Add("<Other heap object (%d)>", map()->instance_type()); |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1701 | void HeapObject::Iterate(ObjectVisitor* v) { |
| 1702 | // Handle header |
| 1703 | IteratePointer(v, kMapOffset); |
| 1704 | // Handle object body |
| 1705 | Map* m = map(); |
| 1706 | IterateBody(m->instance_type(), SizeFromMap(m), v); |
| 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 1711 | ObjectVisitor* v) { |
| 1712 | // Avoiding <Type>::cast(this) because it accesses the map pointer field. |
| 1713 | // During GC, the map pointer field is encoded. |
| 1714 | if (type < FIRST_NONSTRING_TYPE) { |
| 1715 | switch (type & kStringRepresentationMask) { |
| 1716 | case kSeqStringTag: |
| 1717 | break; |
| 1718 | case kConsStringTag: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1719 | ConsString::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1720 | break; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1721 | case kSlicedStringTag: |
| 1722 | SlicedString::BodyDescriptor::IterateBody(this, v); |
| 1723 | break; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1724 | case kExternalStringTag: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1725 | if ((type & kStringEncodingMask) == kOneByteStringTag) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1726 | reinterpret_cast<ExternalAsciiString*>(this)-> |
| 1727 | ExternalAsciiStringIterateBody(v); |
| 1728 | } else { |
| 1729 | reinterpret_cast<ExternalTwoByteString*>(this)-> |
| 1730 | ExternalTwoByteStringIterateBody(v); |
| 1731 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | } |
| 1734 | return; |
| 1735 | } |
| 1736 | |
| 1737 | switch (type) { |
| 1738 | case FIXED_ARRAY_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1739 | FixedArray::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1740 | break; |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1741 | case CONSTANT_POOL_ARRAY_TYPE: |
| 1742 | reinterpret_cast<ConstantPoolArray*>(this)->ConstantPoolIterateBody(v); |
| 1743 | break; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1744 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1745 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1746 | case JS_OBJECT_TYPE: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1747 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1748 | case JS_GENERATOR_OBJECT_TYPE: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1749 | case JS_MODULE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1750 | case JS_VALUE_TYPE: |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1751 | case JS_DATE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1752 | case JS_ARRAY_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1753 | case JS_ARRAY_BUFFER_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1754 | case JS_TYPED_ARRAY_TYPE: |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1755 | case JS_DATA_VIEW_TYPE: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1756 | case JS_SET_TYPE: |
| 1757 | case JS_MAP_TYPE: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1758 | case JS_WEAK_MAP_TYPE: |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1759 | case JS_WEAK_SET_TYPE: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1760 | case JS_REGEXP_TYPE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1761 | case JS_GLOBAL_PROXY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | case JS_GLOBAL_OBJECT_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1763 | case JS_BUILTINS_OBJECT_TYPE: |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1764 | case JS_MESSAGE_OBJECT_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1765 | JSObject::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | break; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1767 | case JS_FUNCTION_TYPE: |
| 1768 | reinterpret_cast<JSFunction*>(this) |
| 1769 | ->JSFunctionIterateBody(object_size, v); |
| 1770 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1771 | case ODDBALL_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1772 | Oddball::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1773 | break; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1774 | case JS_PROXY_TYPE: |
| 1775 | JSProxy::BodyDescriptor::IterateBody(this, v); |
| 1776 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1777 | case JS_FUNCTION_PROXY_TYPE: |
| 1778 | JSFunctionProxy::BodyDescriptor::IterateBody(this, v); |
| 1779 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1780 | case FOREIGN_TYPE: |
| 1781 | reinterpret_cast<Foreign*>(this)->ForeignIterateBody(v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1782 | break; |
| 1783 | case MAP_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1784 | Map::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | break; |
| 1786 | case CODE_TYPE: |
| 1787 | reinterpret_cast<Code*>(this)->CodeIterateBody(v); |
| 1788 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1789 | case CELL_TYPE: |
| 1790 | Cell::BodyDescriptor::IterateBody(this, v); |
| 1791 | break; |
| 1792 | case PROPERTY_CELL_TYPE: |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1793 | PropertyCell::BodyDescriptor::IterateBody(this, v); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1794 | break; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1795 | case SYMBOL_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1796 | Symbol::BodyDescriptor::IterateBody(this, v); |
| 1797 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | case HEAP_NUMBER_TYPE: |
| 1799 | case FILLER_TYPE: |
| 1800 | case BYTE_ARRAY_TYPE: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1801 | case FREE_SPACE_TYPE: |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1802 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1803 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1804 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1805 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1806 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1807 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1808 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1809 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1810 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1811 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1812 | case SHARED_FUNCTION_INFO_TYPE: { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1813 | SharedFunctionInfo::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1814 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1815 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1816 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1817 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1818 | case NAME##_TYPE: |
| 1819 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1820 | #undef MAKE_STRUCT_CASE |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1821 | if (type == ALLOCATION_SITE_TYPE) { |
| 1822 | AllocationSite::BodyDescriptor::IterateBody(this, v); |
| 1823 | } else { |
| 1824 | StructBodyDescriptor::IterateBody(this, object_size, v); |
| 1825 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1826 | break; |
| 1827 | default: |
| 1828 | PrintF("Unknown type: %d\n", type); |
| 1829 | UNREACHABLE(); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1834 | bool HeapNumber::HeapNumberBooleanValue() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1835 | // NaN, +0, and -0 should return the false object |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1836 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1837 | union IeeeDoubleLittleEndianArchType u; |
| 1838 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 1839 | union IeeeDoubleBigEndianArchType u; |
| 1840 | #endif |
| 1841 | u.d = value(); |
| 1842 | if (u.bits.exp == 2047) { |
| 1843 | // Detect NaN for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1844 | 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] | 1845 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1846 | if (u.bits.exp == 0) { |
| 1847 | // Detect +0, and -0 for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1848 | 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] | 1849 | } |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1850 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1854 | void HeapNumber::HeapNumberPrint(FILE* out) { |
| 1855 | PrintF(out, "%.16g", Number()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | |
| 1859 | void HeapNumber::HeapNumberPrint(StringStream* accumulator) { |
| 1860 | // The Windows version of vsnprintf can allocate when printing a %g string |
| 1861 | // into a buffer that may not be big enough. We don't want random memory |
| 1862 | // allocation when producing post-crash stack traces, so we print into a |
| 1863 | // buffer that is plenty big enough for any floating point number, then |
| 1864 | // print that using vsnprintf (which may truncate but never allocate if |
| 1865 | // there is no more space in the buffer). |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1866 | EmbeddedVector<char, 100> buffer; |
| 1867 | OS::SNPrintF(buffer, "%.16g", Number()); |
| 1868 | accumulator->Add("%s", buffer.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1872 | String* JSReceiver::class_name() { |
| 1873 | if (IsJSFunction() && IsJSFunctionProxy()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1874 | return GetHeap()->function_class_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | if (map()->constructor()->IsJSFunction()) { |
| 1877 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1878 | return String::cast(constructor->shared()->instance_class_name()); |
| 1879 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1880 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1881 | return GetHeap()->Object_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1885 | String* Map::constructor_name() { |
| 1886 | if (constructor()->IsJSFunction()) { |
| 1887 | JSFunction* constructor = JSFunction::cast(this->constructor()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1888 | String* name = String::cast(constructor->shared()->name()); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1889 | if (name->length() > 0) return name; |
| 1890 | String* inferred_name = constructor->shared()->inferred_name(); |
| 1891 | if (inferred_name->length() > 0) return inferred_name; |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1892 | Object* proto = prototype(); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1893 | if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1894 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1895 | // TODO(rossberg): what about proxies? |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1896 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1897 | return GetHeap()->Object_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1901 | String* JSReceiver::constructor_name() { |
| 1902 | return map()->constructor_name(); |
| 1903 | } |
| 1904 | |
| 1905 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1906 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 1907 | static Handle<Object> NewStorageFor(Isolate* isolate, |
| 1908 | Handle<Object> object, |
| 1909 | Representation representation) { |
| 1910 | Heap* heap = isolate->heap(); |
| 1911 | CALL_HEAP_FUNCTION(isolate, |
| 1912 | object->AllocateNewStorageFor(heap, representation), |
| 1913 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1917 | void JSObject::AddFastPropertyUsingMap(Handle<JSObject> object, |
| 1918 | Handle<Map> new_map, |
| 1919 | Handle<Name> name, |
| 1920 | Handle<Object> value, |
| 1921 | int field_index, |
| 1922 | Representation representation) { |
| 1923 | Isolate* isolate = object->GetIsolate(); |
| 1924 | |
| 1925 | // This method is used to transition to a field. If we are transitioning to a |
| 1926 | // double field, allocate new storage. |
| 1927 | Handle<Object> storage = NewStorageFor(isolate, value, representation); |
| 1928 | |
| 1929 | if (object->map()->unused_property_fields() == 0) { |
| 1930 | int new_unused = new_map->unused_property_fields(); |
| 1931 | Handle<FixedArray> properties(object->properties()); |
| 1932 | Handle<FixedArray> values = isolate->factory()->CopySizeFixedArray( |
| 1933 | properties, properties->length() + new_unused + 1); |
| 1934 | object->set_properties(*values); |
| 1935 | } |
| 1936 | |
| 1937 | object->set_map(*new_map); |
| 1938 | object->FastPropertyAtPut(field_index, *storage); |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | static MaybeObject* CopyAddFieldDescriptor(Map* map, |
| 1943 | Name* name, |
| 1944 | int index, |
| 1945 | PropertyAttributes attributes, |
| 1946 | Representation representation, |
| 1947 | TransitionFlag flag) { |
| 1948 | Map* new_map; |
| 1949 | FieldDescriptor new_field_desc(name, index, attributes, representation); |
| 1950 | MaybeObject* maybe_map = map->CopyAddDescriptor(&new_field_desc, flag); |
| 1951 | if (!maybe_map->To(&new_map)) return maybe_map; |
| 1952 | int unused_property_fields = map->unused_property_fields() - 1; |
| 1953 | if (unused_property_fields < 0) { |
| 1954 | unused_property_fields += JSObject::kFieldsAdded; |
| 1955 | } |
| 1956 | new_map->set_unused_property_fields(unused_property_fields); |
| 1957 | return new_map; |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | static Handle<Map> CopyAddFieldDescriptor(Handle<Map> map, |
| 1962 | Handle<Name> name, |
| 1963 | int index, |
| 1964 | PropertyAttributes attributes, |
| 1965 | Representation representation, |
| 1966 | TransitionFlag flag) { |
| 1967 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 1968 | CopyAddFieldDescriptor( |
| 1969 | *map, *name, index, attributes, representation, flag), |
| 1970 | Map); |
| 1971 | } |
| 1972 | |
| 1973 | |
| 1974 | void JSObject::AddFastProperty(Handle<JSObject> object, |
| 1975 | Handle<Name> name, |
| 1976 | Handle<Object> value, |
| 1977 | PropertyAttributes attributes, |
| 1978 | StoreFromKeyed store_mode, |
| 1979 | ValueType value_type, |
| 1980 | TransitionFlag flag) { |
| 1981 | ASSERT(!object->IsJSGlobalProxy()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 1982 | ASSERT(DescriptorArray::kNotFound == |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1983 | object->map()->instance_descriptors()->Search( |
| 1984 | *name, object->map()->NumberOfOwnDescriptors())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1985 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1986 | // 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] | 1987 | // hidden strings) and is not a real identifier. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 1988 | // Normalize the object if it will have too many fast properties. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1989 | Isolate* isolate = object->GetIsolate(); |
| 1990 | if (!name->IsCacheable(isolate) || |
| 1991 | object->TooManyFastProperties(store_mode)) { |
| 1992 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
| 1993 | AddSlowProperty(object, name, value, attributes); |
| 1994 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | // Compute the new index for new field. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1998 | int index = object->map()->NextFreePropertyIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Allocate new instance descriptors with (name, index) added |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2001 | if (object->IsJSContextExtensionObject()) value_type = FORCE_TAGGED; |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2002 | Representation representation = value->OptimalRepresentation(value_type); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2003 | Handle<Map> new_map = CopyAddFieldDescriptor( |
| 2004 | handle(object->map()), name, index, attributes, representation, flag); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2005 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2006 | AddFastPropertyUsingMap(object, new_map, name, value, index, representation); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2010 | static MaybeObject* CopyAddConstantDescriptor(Map* map, |
| 2011 | Name* name, |
| 2012 | Object* value, |
| 2013 | PropertyAttributes attributes, |
| 2014 | TransitionFlag flag) { |
| 2015 | ConstantDescriptor new_constant_desc(name, value, attributes); |
| 2016 | return map->CopyAddDescriptor(&new_constant_desc, flag); |
| 2017 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2018 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2019 | |
| 2020 | static Handle<Map> CopyAddConstantDescriptor(Handle<Map> map, |
| 2021 | Handle<Name> name, |
| 2022 | Handle<Object> value, |
| 2023 | PropertyAttributes attributes, |
| 2024 | TransitionFlag flag) { |
| 2025 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 2026 | CopyAddConstantDescriptor( |
| 2027 | *map, *name, *value, attributes, flag), |
| 2028 | Map); |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | void JSObject::AddConstantProperty(Handle<JSObject> object, |
| 2033 | Handle<Name> name, |
| 2034 | Handle<Object> constant, |
| 2035 | PropertyAttributes attributes, |
| 2036 | TransitionFlag initial_flag) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2037 | TransitionFlag flag = |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2038 | // Do not add transitions to global objects. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2039 | (object->IsGlobalObject() || |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2040 | // Don't add transitions to special properties with non-trivial |
| 2041 | // attributes. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2042 | attributes != NONE) |
| 2043 | ? OMIT_TRANSITION |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2044 | : initial_flag; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2046 | // Allocate new instance descriptors with (name, constant) added. |
| 2047 | Handle<Map> new_map = CopyAddConstantDescriptor( |
| 2048 | handle(object->map()), name, constant, attributes, flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2050 | object->set_map(*new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2054 | void JSObject::AddSlowProperty(Handle<JSObject> object, |
| 2055 | Handle<Name> name, |
| 2056 | Handle<Object> value, |
| 2057 | PropertyAttributes attributes) { |
| 2058 | ASSERT(!object->HasFastProperties()); |
| 2059 | Isolate* isolate = object->GetIsolate(); |
| 2060 | Handle<NameDictionary> dict(object->property_dictionary()); |
| 2061 | if (object->IsGlobalObject()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2062 | // In case name is an orphaned property reuse the cell. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2063 | int entry = dict->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2064 | if (entry != NameDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2065 | Handle<PropertyCell> cell(PropertyCell::cast(dict->ValueAt(entry))); |
| 2066 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2067 | // Assign an enumeration index to the property and update |
| 2068 | // SetNextEnumerationIndex. |
| 2069 | int index = dict->NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2070 | PropertyDetails details = PropertyDetails(attributes, NORMAL, index); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2071 | dict->SetNextEnumerationIndex(index + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2072 | dict->SetEntry(entry, *name, *cell, details); |
| 2073 | return; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2074 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2075 | Handle<PropertyCell> cell = isolate->factory()->NewPropertyCell(value); |
| 2076 | PropertyCell::SetValueInferType(cell, value); |
| 2077 | value = cell; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2078 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2079 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2080 | Handle<NameDictionary> result = NameDictionaryAdd(dict, name, value, details); |
| 2081 | if (*dict != *result) object->set_properties(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2085 | Handle<Object> JSObject::AddProperty(Handle<JSObject> object, |
| 2086 | Handle<Name> name, |
| 2087 | Handle<Object> value, |
| 2088 | PropertyAttributes attributes, |
| 2089 | StrictModeFlag strict_mode, |
| 2090 | JSReceiver::StoreFromKeyed store_mode, |
| 2091 | ExtensibilityCheck extensibility_check, |
| 2092 | ValueType value_type, |
| 2093 | StoreMode mode, |
| 2094 | TransitionFlag transition_flag) { |
| 2095 | ASSERT(!object->IsJSGlobalProxy()); |
| 2096 | Isolate* isolate = object->GetIsolate(); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 2097 | |
| 2098 | if (!name->IsUniqueName()) { |
| 2099 | name = isolate->factory()->InternalizedStringFromString( |
| 2100 | Handle<String>::cast(name)); |
| 2101 | } |
| 2102 | |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 2103 | if (extensibility_check == PERFORM_EXTENSIBILITY_CHECK && |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2104 | !object->map()->is_extensible()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2105 | if (strict_mode == kNonStrictMode) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2106 | return value; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2107 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2108 | Handle<Object> args[1] = { name }; |
| 2109 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 2110 | "object_not_extensible", HandleVector(args, ARRAY_SIZE(args))); |
| 2111 | isolate->Throw(*error); |
| 2112 | return Handle<Object>(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2113 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2114 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2115 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2116 | if (object->HasFastProperties()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2117 | // Ensure the descriptor array does not get too big. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2118 | if (object->map()->NumberOfOwnDescriptors() < |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2119 | DescriptorArray::kMaxNumberOfDescriptors) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2120 | // TODO(verwaest): Support other constants. |
| 2121 | // if (mode == ALLOW_AS_CONSTANT && |
| 2122 | // !value->IsTheHole() && |
| 2123 | // !value->IsConsString()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2124 | if (value->IsJSFunction()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2125 | AddConstantProperty(object, name, value, attributes, transition_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2126 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2127 | AddFastProperty(object, name, value, attributes, store_mode, |
| 2128 | value_type, transition_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } else { |
| 2131 | // Normalize the object to prevent very large instance descriptors. |
| 2132 | // This eliminates unwanted N^2 allocation and lookup behavior. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2133 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
| 2134 | AddSlowProperty(object, name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2135 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2136 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2137 | AddSlowProperty(object, name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2138 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2139 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2140 | if (FLAG_harmony_observation && object->map()->is_observed()) { |
| 2141 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
| 2142 | EnqueueChangeRecord(object, "new", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2145 | return value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | |
| 2149 | void JSObject::EnqueueChangeRecord(Handle<JSObject> object, |
| 2150 | const char* type_str, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2151 | Handle<Name> name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2152 | Handle<Object> old_value) { |
| 2153 | Isolate* isolate = object->GetIsolate(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2154 | HandleScope scope(isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2155 | Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2156 | if (object->IsJSGlobalObject()) { |
| 2157 | object = handle(JSGlobalObject::cast(*object)->global_receiver(), isolate); |
| 2158 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2159 | Handle<Object> args[] = { type, object, name, old_value }; |
| 2160 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2161 | Execution::Call(isolate, |
| 2162 | Handle<JSFunction>(isolate->observers_notify_change()), |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2163 | isolate->factory()->undefined_value(), |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2164 | old_value->IsTheHole() ? 3 : 4, args, |
| 2165 | &threw); |
| 2166 | ASSERT(!threw); |
| 2167 | } |
| 2168 | |
| 2169 | |
| 2170 | void JSObject::DeliverChangeRecords(Isolate* isolate) { |
| 2171 | ASSERT(isolate->observer_delivery_pending()); |
| 2172 | bool threw = false; |
| 2173 | Execution::Call( |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2174 | isolate, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2175 | isolate->observers_deliver_changes(), |
| 2176 | isolate->factory()->undefined_value(), |
| 2177 | 0, |
| 2178 | NULL, |
| 2179 | &threw); |
| 2180 | ASSERT(!threw); |
| 2181 | isolate->set_observer_delivery_pending(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2185 | Handle<Object> JSObject::SetPropertyPostInterceptor( |
| 2186 | Handle<JSObject> object, |
| 2187 | Handle<Name> name, |
| 2188 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2189 | PropertyAttributes attributes, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2190 | StrictModeFlag strict_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2191 | // Check local property, ignore interceptor. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2192 | LookupResult result(object->GetIsolate()); |
| 2193 | object->LocalLookupRealNamedProperty(*name, &result); |
| 2194 | if (!result.IsFound()) { |
| 2195 | object->map()->LookupTransition(*object, *name, &result); |
| 2196 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2197 | if (result.IsFound()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2198 | // An existing property or a map transition was found. Use set property to |
| 2199 | // handle all these cases. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2200 | return SetPropertyForResult(object, &result, name, value, attributes, |
| 2201 | strict_mode, MAY_BE_STORE_FROM_KEYED); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2202 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2203 | bool done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2204 | Handle<Object> result_object = SetPropertyViaPrototypes( |
| 2205 | object, name, value, attributes, strict_mode, &done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2206 | if (done) return result_object; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2207 | // Add a new real property. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2208 | return AddProperty(object, name, value, attributes, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2212 | static void ReplaceSlowProperty(Handle<JSObject> object, |
| 2213 | Handle<Name> name, |
| 2214 | Handle<Object> value, |
| 2215 | PropertyAttributes attributes) { |
| 2216 | NameDictionary* dictionary = object->property_dictionary(); |
| 2217 | int old_index = dictionary->FindEntry(*name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2218 | int new_enumeration_index = 0; // 0 means "Use the next available index." |
| 2219 | if (old_index != -1) { |
| 2220 | // All calls to ReplaceSlowProperty have had all transitions removed. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2221 | new_enumeration_index = dictionary->DetailsAt(old_index).dictionary_index(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2222 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2223 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2224 | PropertyDetails new_details(attributes, NORMAL, new_enumeration_index); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2225 | JSObject::SetNormalizedProperty(object, name, value, new_details); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2228 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2229 | const char* Representation::Mnemonic() const { |
| 2230 | switch (kind_) { |
| 2231 | case kNone: return "v"; |
| 2232 | case kTagged: return "t"; |
| 2233 | case kSmi: return "s"; |
| 2234 | case kDouble: return "d"; |
| 2235 | case kInteger32: return "i"; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2236 | case kHeapObject: return "h"; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2237 | case kExternal: return "x"; |
| 2238 | default: |
| 2239 | UNREACHABLE(); |
| 2240 | return NULL; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | |
| 2245 | enum RightTrimMode { FROM_GC, FROM_MUTATOR }; |
| 2246 | |
| 2247 | |
| 2248 | static void ZapEndOfFixedArray(Address new_end, int to_trim) { |
| 2249 | // If we are doing a big trim in old space then we zap the space. |
| 2250 | Object** zap = reinterpret_cast<Object**>(new_end); |
| 2251 | zap++; // Header of filler must be at least one word so skip that. |
| 2252 | for (int i = 1; i < to_trim; i++) { |
| 2253 | *zap++ = Smi::FromInt(0); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | |
| 2258 | template<RightTrimMode trim_mode> |
| 2259 | static void RightTrimFixedArray(Heap* heap, FixedArray* elms, int to_trim) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 2260 | ASSERT(elms->map() != heap->fixed_cow_array_map()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2261 | // For now this trick is only applied to fixed arrays in new and paged space. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 2262 | ASSERT(!heap->lo_space()->Contains(elms)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2263 | |
| 2264 | const int len = elms->length(); |
| 2265 | |
| 2266 | ASSERT(to_trim < len); |
| 2267 | |
| 2268 | Address new_end = elms->address() + FixedArray::SizeFor(len - to_trim); |
| 2269 | |
| 2270 | if (trim_mode != FROM_GC || Heap::ShouldZapGarbage()) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2271 | ZapEndOfFixedArray(new_end, to_trim); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | int size_delta = to_trim * kPointerSize; |
| 2275 | |
| 2276 | // Technically in new space this write might be omitted (except for |
| 2277 | // debug mode which iterates through the heap), but to play safer |
| 2278 | // we still do it. |
| 2279 | heap->CreateFillerObjectAt(new_end, size_delta); |
| 2280 | |
| 2281 | elms->set_length(len - to_trim); |
| 2282 | |
| 2283 | // Maintain marking consistency for IncrementalMarking. |
| 2284 | if (Marking::IsBlack(Marking::MarkBitFrom(elms))) { |
| 2285 | if (trim_mode == FROM_GC) { |
| 2286 | MemoryChunk::IncrementLiveBytesFromGC(elms->address(), -size_delta); |
| 2287 | } else { |
| 2288 | MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta); |
| 2289 | } |
| 2290 | } |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 2291 | |
| 2292 | // The array may not be moved during GC, |
| 2293 | // and size has to be adjusted nevertheless. |
| 2294 | HeapProfiler* profiler = heap->isolate()->heap_profiler(); |
| 2295 | if (profiler->is_tracking_allocations()) { |
| 2296 | profiler->UpdateObjectSizeEvent(elms->address(), elms->Size()); |
| 2297 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2301 | bool Map::InstancesNeedRewriting(Map* target, |
| 2302 | int target_number_of_fields, |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2303 | int target_inobject, |
| 2304 | int target_unused) { |
| 2305 | // If fields were added (or removed), rewrite the instance. |
| 2306 | int number_of_fields = NumberOfFields(); |
| 2307 | ASSERT(target_number_of_fields >= number_of_fields); |
| 2308 | if (target_number_of_fields != number_of_fields) return true; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2309 | |
| 2310 | if (FLAG_track_double_fields) { |
| 2311 | // If smi descriptors were replaced by double descriptors, rewrite. |
| 2312 | DescriptorArray* old_desc = instance_descriptors(); |
| 2313 | DescriptorArray* new_desc = target->instance_descriptors(); |
| 2314 | int limit = NumberOfOwnDescriptors(); |
| 2315 | for (int i = 0; i < limit; i++) { |
| 2316 | if (new_desc->GetDetails(i).representation().IsDouble() && |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2317 | !old_desc->GetDetails(i).representation().IsDouble()) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2318 | return true; |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2323 | // If no fields were added, and no inobject properties were removed, setting |
| 2324 | // the map is sufficient. |
| 2325 | if (target_inobject == inobject_properties()) return false; |
| 2326 | // In-object slack tracking may have reduced the object size of the new map. |
| 2327 | // In that case, succeed if all existing fields were inobject, and they still |
| 2328 | // fit within the new inobject size. |
| 2329 | ASSERT(target_inobject < inobject_properties()); |
| 2330 | if (target_number_of_fields <= target_inobject) { |
| 2331 | ASSERT(target_number_of_fields + target_unused == target_inobject); |
| 2332 | return false; |
| 2333 | } |
| 2334 | // Otherwise, properties will need to be moved to the backing store. |
| 2335 | return true; |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | // To migrate an instance to a map: |
| 2340 | // - First check whether the instance needs to be rewritten. If not, simply |
| 2341 | // change the map. |
| 2342 | // - Otherwise, allocate a fixed array large enough to hold all fields, in |
| 2343 | // addition to unused space. |
| 2344 | // - Copy all existing properties in, in the following order: backing store |
| 2345 | // properties, unused fields, inobject properties. |
| 2346 | // - If all allocation succeeded, commit the state atomically: |
| 2347 | // * Copy inobject properties from the backing store back into the object. |
| 2348 | // * Trim the difference in instance size of the object. This also cleanly |
| 2349 | // frees inobject properties that moved to the backing store. |
| 2350 | // * If there are properties left in the backing store, trim of the space used |
| 2351 | // to temporarily store the inobject properties. |
| 2352 | // * If there are properties left in the backing store, install the backing |
| 2353 | // store. |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2354 | void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map) { |
| 2355 | Isolate* isolate = object->GetIsolate(); |
| 2356 | Handle<Map> old_map(object->map()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2357 | int number_of_fields = new_map->NumberOfFields(); |
| 2358 | int inobject = new_map->inobject_properties(); |
| 2359 | int unused = new_map->unused_property_fields(); |
| 2360 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2361 | // Nothing to do if no functions were converted to fields and no smis were |
| 2362 | // converted to doubles. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2363 | if (!old_map->InstancesNeedRewriting( |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2364 | *new_map, number_of_fields, inobject, unused)) { |
| 2365 | object->set_map(*new_map); |
| 2366 | return; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | int total_size = number_of_fields + unused; |
| 2370 | int external = total_size - inobject; |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2371 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2372 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2373 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
| 2374 | Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2375 | int descriptors = new_map->NumberOfOwnDescriptors(); |
| 2376 | |
| 2377 | for (int i = 0; i < descriptors; i++) { |
| 2378 | PropertyDetails details = new_descriptors->GetDetails(i); |
| 2379 | if (details.type() != FIELD) continue; |
| 2380 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2381 | if (old_details.type() == CALLBACKS) { |
| 2382 | ASSERT(details.representation().IsTagged()); |
| 2383 | continue; |
| 2384 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2385 | ASSERT(old_details.type() == CONSTANT || |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2386 | old_details.type() == FIELD); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2387 | Object* raw_value = old_details.type() == CONSTANT |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2388 | ? old_descriptors->GetValue(i) |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2389 | : object->RawFastPropertyAt(old_descriptors->GetFieldIndex(i)); |
| 2390 | Handle<Object> value(raw_value, isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2391 | if (FLAG_track_double_fields && |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2392 | !old_details.representation().IsDouble() && |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2393 | details.representation().IsDouble()) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2394 | if (old_details.representation().IsNone()) { |
| 2395 | value = handle(Smi::FromInt(0), isolate); |
| 2396 | } |
| 2397 | value = NewStorageFor(isolate, value, details.representation()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2398 | } |
| 2399 | ASSERT(!(FLAG_track_double_fields && |
| 2400 | details.representation().IsDouble() && |
| 2401 | value->IsSmi())); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2402 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2403 | if (target_index < 0) target_index += total_size; |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2404 | array->set(target_index, *value); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2407 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2408 | DisallowHeapAllocation no_allocation; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Copy (real) inobject properties. If necessary, stop at number_of_fields to |
| 2411 | // avoid overwriting |one_pointer_filler_map|. |
| 2412 | int limit = Min(inobject, number_of_fields); |
| 2413 | for (int i = 0; i < limit; i++) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2414 | object->FastPropertyAtPut(i, array->get(external + i)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | // Create filler object past the new instance size. |
| 2418 | int new_instance_size = new_map->instance_size(); |
| 2419 | int instance_size_delta = old_map->instance_size() - new_instance_size; |
| 2420 | ASSERT(instance_size_delta >= 0); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2421 | Address address = object->address() + new_instance_size; |
| 2422 | isolate->heap()->CreateFillerObjectAt(address, instance_size_delta); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2423 | |
| 2424 | // If there are properties in the new backing store, trim it to the correct |
| 2425 | // size and install the backing store into the object. |
| 2426 | if (external > 0) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2427 | RightTrimFixedArray<FROM_MUTATOR>(isolate->heap(), *array, inobject); |
| 2428 | object->set_properties(*array); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2431 | object->set_map(*new_map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2435 | void JSObject::GeneralizeFieldRepresentation(Handle<JSObject> object, |
| 2436 | int modify_index, |
| 2437 | Representation new_representation, |
| 2438 | StoreMode store_mode) { |
| 2439 | Handle<Map> new_map = Map::GeneralizeRepresentation( |
| 2440 | handle(object->map()), modify_index, new_representation, store_mode); |
| 2441 | if (object->map() == *new_map) return; |
| 2442 | return MigrateToMap(object, new_map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | int Map::NumberOfFields() { |
| 2447 | DescriptorArray* descriptors = instance_descriptors(); |
| 2448 | int result = 0; |
| 2449 | for (int i = 0; i < NumberOfOwnDescriptors(); i++) { |
| 2450 | if (descriptors->GetDetails(i).type() == FIELD) result++; |
| 2451 | } |
| 2452 | return result; |
| 2453 | } |
| 2454 | |
| 2455 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2456 | Handle<Map> Map::CopyGeneralizeAllRepresentations(Handle<Map> map, |
| 2457 | int modify_index, |
| 2458 | StoreMode store_mode, |
| 2459 | PropertyAttributes attributes, |
| 2460 | const char* reason) { |
| 2461 | Handle<Map> new_map = Copy(map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2462 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2463 | DescriptorArray* descriptors = new_map->instance_descriptors(); |
| 2464 | descriptors->InitializeRepresentations(Representation::Tagged()); |
| 2465 | |
| 2466 | // Unless the instance is being migrated, ensure that modify_index is a field. |
| 2467 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 2468 | if (store_mode == FORCE_FIELD && details.type() != FIELD) { |
| 2469 | FieldDescriptor d(descriptors->GetKey(modify_index), |
| 2470 | new_map->NumberOfFields(), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2471 | attributes, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2472 | Representation::Tagged()); |
| 2473 | d.SetSortedKeyIndex(details.pointer()); |
| 2474 | descriptors->Set(modify_index, &d); |
| 2475 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 2476 | if (unused_property_fields < 0) { |
| 2477 | unused_property_fields += JSObject::kFieldsAdded; |
| 2478 | } |
| 2479 | new_map->set_unused_property_fields(unused_property_fields); |
| 2480 | } |
| 2481 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2482 | if (FLAG_trace_generalization) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2483 | map->PrintGeneralization(stdout, reason, modify_index, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2484 | new_map->NumberOfOwnDescriptors(), |
| 2485 | new_map->NumberOfOwnDescriptors(), |
| 2486 | details.type() == CONSTANT && store_mode == FORCE_FIELD, |
| 2487 | Representation::Tagged(), Representation::Tagged()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2488 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2489 | return new_map; |
| 2490 | } |
| 2491 | |
| 2492 | |
| 2493 | void Map::DeprecateTransitionTree() { |
| 2494 | if (!FLAG_track_fields) return; |
| 2495 | if (is_deprecated()) return; |
| 2496 | if (HasTransitionArray()) { |
| 2497 | TransitionArray* transitions = this->transitions(); |
| 2498 | for (int i = 0; i < transitions->number_of_transitions(); i++) { |
| 2499 | transitions->GetTarget(i)->DeprecateTransitionTree(); |
| 2500 | } |
| 2501 | } |
| 2502 | deprecate(); |
| 2503 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 2504 | GetIsolate(), DependentCode::kTransitionGroup); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 2505 | NotifyLeafMapLayoutChange(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | // Invalidates a transition target at |key|, and installs |new_descriptors| over |
| 2510 | // the current instance_descriptors to ensure proper sharing of descriptor |
| 2511 | // arrays. |
| 2512 | void Map::DeprecateTarget(Name* key, DescriptorArray* new_descriptors) { |
| 2513 | if (HasTransitionArray()) { |
| 2514 | TransitionArray* transitions = this->transitions(); |
| 2515 | int transition = transitions->Search(key); |
| 2516 | if (transition != TransitionArray::kNotFound) { |
| 2517 | transitions->GetTarget(transition)->DeprecateTransitionTree(); |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | // Don't overwrite the empty descriptor array. |
| 2522 | if (NumberOfOwnDescriptors() == 0) return; |
| 2523 | |
| 2524 | DescriptorArray* to_replace = instance_descriptors(); |
| 2525 | Map* current = this; |
| 2526 | while (current->instance_descriptors() == to_replace) { |
| 2527 | current->SetEnumLength(Map::kInvalidEnumCache); |
| 2528 | current->set_instance_descriptors(new_descriptors); |
| 2529 | Object* next = current->GetBackPointer(); |
| 2530 | if (next->IsUndefined()) break; |
| 2531 | current = Map::cast(next); |
| 2532 | } |
| 2533 | |
| 2534 | set_owns_descriptors(false); |
| 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | Map* Map::FindRootMap() { |
| 2539 | Map* result = this; |
| 2540 | while (true) { |
| 2541 | Object* back = result->GetBackPointer(); |
| 2542 | if (back->IsUndefined()) return result; |
| 2543 | result = Map::cast(back); |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2548 | // Returns NULL if the updated map is incompatible. |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2549 | Map* Map::FindUpdatedMap(int verbatim, |
| 2550 | int length, |
| 2551 | DescriptorArray* descriptors) { |
| 2552 | // This can only be called on roots of transition trees. |
| 2553 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2554 | |
| 2555 | Map* current = this; |
| 2556 | |
| 2557 | for (int i = verbatim; i < length; i++) { |
| 2558 | if (!current->HasTransitionArray()) break; |
| 2559 | Name* name = descriptors->GetKey(i); |
| 2560 | TransitionArray* transitions = current->transitions(); |
| 2561 | int transition = transitions->Search(name); |
| 2562 | if (transition == TransitionArray::kNotFound) break; |
| 2563 | current = transitions->GetTarget(transition); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2564 | PropertyDetails details = descriptors->GetDetails(i); |
| 2565 | PropertyDetails target_details = |
| 2566 | current->instance_descriptors()->GetDetails(i); |
| 2567 | if (details.attributes() != target_details.attributes()) return NULL; |
| 2568 | if (details.type() == CALLBACKS) { |
| 2569 | if (target_details.type() != CALLBACKS) return NULL; |
| 2570 | if (descriptors->GetValue(i) != |
| 2571 | current->instance_descriptors()->GetValue(i)) { |
| 2572 | return NULL; |
| 2573 | } |
| 2574 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | return current; |
| 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | Map* Map::FindLastMatchMap(int verbatim, |
| 2582 | int length, |
| 2583 | DescriptorArray* descriptors) { |
| 2584 | // This can only be called on roots of transition trees. |
| 2585 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2586 | |
| 2587 | Map* current = this; |
| 2588 | |
| 2589 | for (int i = verbatim; i < length; i++) { |
| 2590 | if (!current->HasTransitionArray()) break; |
| 2591 | Name* name = descriptors->GetKey(i); |
| 2592 | TransitionArray* transitions = current->transitions(); |
| 2593 | int transition = transitions->Search(name); |
| 2594 | if (transition == TransitionArray::kNotFound) break; |
| 2595 | |
| 2596 | Map* next = transitions->GetTarget(transition); |
| 2597 | DescriptorArray* next_descriptors = next->instance_descriptors(); |
| 2598 | |
| 2599 | if (next_descriptors->GetValue(i) != descriptors->GetValue(i)) break; |
| 2600 | |
| 2601 | PropertyDetails details = descriptors->GetDetails(i); |
| 2602 | PropertyDetails next_details = next_descriptors->GetDetails(i); |
| 2603 | if (details.type() != next_details.type()) break; |
| 2604 | if (details.attributes() != next_details.attributes()) break; |
| 2605 | if (!details.representation().Equals(next_details.representation())) break; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2606 | |
| 2607 | current = next; |
| 2608 | } |
| 2609 | return current; |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | // Generalize the representation of the descriptor at |modify_index|. |
| 2614 | // This method rewrites the transition tree to reflect the new change. To avoid |
| 2615 | // high degrees over polymorphism, and to stabilize quickly, on every rewrite |
| 2616 | // the new type is deduced by merging the current type with any potential new |
| 2617 | // (partial) version of the type in the transition tree. |
| 2618 | // To do this, on each rewrite: |
| 2619 | // - Search the root of the transition tree using FindRootMap. |
| 2620 | // - Find |updated|, the newest matching version of this map using |
| 2621 | // FindUpdatedMap. This uses the keys in the own map's descriptor array to |
| 2622 | // walk the transition tree. |
| 2623 | // - Merge/generalize the descriptor array of the current map and |updated|. |
| 2624 | // - Generalize the |modify_index| descriptor using |new_representation|. |
| 2625 | // - Walk the tree again starting from the root towards |updated|. Stop at |
| 2626 | // |split_map|, the first map who's descriptor array does not match the merged |
| 2627 | // descriptor array. |
| 2628 | // - If |updated| == |split_map|, |updated| is in the expected state. Return it. |
| 2629 | // - Otherwise, invalidate the outdated transition target from |updated|, and |
| 2630 | // replace its transition tree with a new branch for the updated descriptors. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2631 | Handle<Map> Map::GeneralizeRepresentation(Handle<Map> old_map, |
| 2632 | int modify_index, |
| 2633 | Representation new_representation, |
| 2634 | StoreMode store_mode) { |
| 2635 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2636 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 2637 | Representation old_representation = old_details.representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2638 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2639 | // It's fine to transition from None to anything but double without any |
| 2640 | // modification to the object, because the default uninitialized value for |
| 2641 | // representation None can be overwritten by both smi and tagged values. |
| 2642 | // Doubles, however, would require a box allocation. |
| 2643 | if (old_representation.IsNone() && |
| 2644 | !new_representation.IsNone() && |
| 2645 | !new_representation.IsDouble()) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2646 | old_descriptors->SetRepresentation(modify_index, new_representation); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2647 | return old_map; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | int descriptors = old_map->NumberOfOwnDescriptors(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2651 | Handle<Map> root_map(old_map->FindRootMap()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2652 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2653 | // Check the state of the root map. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2654 | if (!old_map->EquivalentToForTransition(*root_map)) { |
| 2655 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 2656 | old_details.attributes(), "not equivalent"); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | int verbatim = root_map->NumberOfOwnDescriptors(); |
| 2660 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2661 | if (store_mode != ALLOW_AS_CONSTANT && modify_index < verbatim) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2662 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2663 | old_details.attributes(), "root modification"); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2666 | Map* raw_updated = root_map->FindUpdatedMap( |
| 2667 | verbatim, descriptors, *old_descriptors); |
| 2668 | if (raw_updated == NULL) { |
| 2669 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 2670 | old_details.attributes(), "incompatible"); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2671 | } |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2672 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2673 | Handle<Map> updated(raw_updated); |
| 2674 | Handle<DescriptorArray> updated_descriptors(updated->instance_descriptors()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2675 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2676 | int valid = updated->NumberOfOwnDescriptors(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2677 | |
| 2678 | // Directly change the map if the target map is more general. Ensure that the |
| 2679 | // target type of the modify_index is a FIELD, unless we are migrating. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2680 | if (updated_descriptors->IsMoreGeneralThan( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2681 | verbatim, valid, descriptors, *old_descriptors) && |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2682 | (store_mode == ALLOW_AS_CONSTANT || |
| 2683 | updated_descriptors->GetDetails(modify_index).type() == FIELD)) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2684 | Representation updated_representation = |
| 2685 | updated_descriptors->GetDetails(modify_index).representation(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2686 | if (new_representation.fits_into(updated_representation)) return updated; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2689 | Handle<DescriptorArray> new_descriptors = DescriptorArray::Merge( |
| 2690 | updated_descriptors, verbatim, valid, descriptors, modify_index, |
| 2691 | store_mode, old_descriptors); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2692 | ASSERT(store_mode == ALLOW_AS_CONSTANT || |
| 2693 | new_descriptors->GetDetails(modify_index).type() == FIELD); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2694 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2695 | old_representation = |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2696 | new_descriptors->GetDetails(modify_index).representation(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2697 | Representation updated_representation = |
| 2698 | new_representation.generalize(old_representation); |
| 2699 | if (!updated_representation.Equals(old_representation)) { |
| 2700 | new_descriptors->SetRepresentation(modify_index, updated_representation); |
| 2701 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2702 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2703 | Handle<Map> split_map(root_map->FindLastMatchMap( |
| 2704 | verbatim, descriptors, *new_descriptors)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2705 | |
| 2706 | int split_descriptors = split_map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2707 | // This is shadowed by |updated_descriptors| being more general than |
| 2708 | // |old_descriptors|. |
| 2709 | ASSERT(descriptors != split_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2710 | |
| 2711 | int descriptor = split_descriptors; |
| 2712 | split_map->DeprecateTarget( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2713 | old_descriptors->GetKey(descriptor), *new_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2714 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2715 | if (FLAG_trace_generalization) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2716 | old_map->PrintGeneralization( |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2717 | stdout, "", modify_index, descriptor, descriptors, |
| 2718 | old_descriptors->GetDetails(modify_index).type() == CONSTANT && |
| 2719 | store_mode == FORCE_FIELD, |
| 2720 | old_representation, updated_representation); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2723 | // Add missing transitions. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2724 | Handle<Map> new_map = split_map; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2725 | for (; descriptor < descriptors; descriptor++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2726 | new_map = Map::CopyInstallDescriptors(new_map, descriptor, new_descriptors); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 2727 | new_map->set_migration_target(true); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | new_map->set_owns_descriptors(true); |
| 2731 | return new_map; |
| 2732 | } |
| 2733 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2734 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2735 | Map* Map::CurrentMapForDeprecated() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 2736 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2737 | if (!is_deprecated()) return this; |
| 2738 | |
| 2739 | DescriptorArray* old_descriptors = instance_descriptors(); |
| 2740 | |
| 2741 | int descriptors = NumberOfOwnDescriptors(); |
| 2742 | Map* root_map = FindRootMap(); |
| 2743 | |
| 2744 | // Check the state of the root map. |
| 2745 | if (!EquivalentToForTransition(root_map)) return NULL; |
| 2746 | int verbatim = root_map->NumberOfOwnDescriptors(); |
| 2747 | |
| 2748 | Map* updated = root_map->FindUpdatedMap( |
| 2749 | verbatim, descriptors, old_descriptors); |
| 2750 | if (updated == NULL) return NULL; |
| 2751 | |
| 2752 | DescriptorArray* updated_descriptors = updated->instance_descriptors(); |
| 2753 | int valid = updated->NumberOfOwnDescriptors(); |
| 2754 | if (!updated_descriptors->IsMoreGeneralThan( |
| 2755 | verbatim, valid, descriptors, old_descriptors)) { |
| 2756 | return NULL; |
| 2757 | } |
| 2758 | |
| 2759 | return updated; |
| 2760 | } |
| 2761 | |
| 2762 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2763 | Handle<Object> JSObject::SetPropertyWithInterceptor( |
| 2764 | Handle<JSObject> object, |
| 2765 | Handle<Name> name, |
| 2766 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2767 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2768 | StrictModeFlag strict_mode) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2769 | // TODO(rossberg): Support symbols in the API. |
| 2770 | if (name->IsSymbol()) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2771 | Isolate* isolate = object->GetIsolate(); |
| 2772 | Handle<String> name_string = Handle<String>::cast(name); |
| 2773 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2774 | if (!interceptor->setter()->IsUndefined()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2775 | LOG(isolate, |
| 2776 | ApiNamedPropertyAccess("interceptor-named-set", *object, *name)); |
| 2777 | PropertyCallbackArguments args( |
| 2778 | isolate, interceptor->data(), *object, *object); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 2779 | v8::NamedPropertySetterCallback setter = |
| 2780 | v8::ToCData<v8::NamedPropertySetterCallback>(interceptor->setter()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2781 | Handle<Object> value_unhole = value->IsTheHole() |
| 2782 | ? Handle<Object>(isolate->factory()->undefined_value()) : value; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2783 | v8::Handle<v8::Value> result = args.Call(setter, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2784 | v8::Utils::ToLocal(name_string), |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2785 | v8::Utils::ToLocal(value_unhole)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2786 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2787 | if (!result.IsEmpty()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2788 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2789 | Handle<Object> result = |
| 2790 | SetPropertyPostInterceptor(object, name, value, attributes, strict_mode); |
| 2791 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2792 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2796 | Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2797 | Handle<Name> name, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2798 | Handle<Object> value, |
| 2799 | PropertyAttributes attributes, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2800 | StrictModeFlag strict_mode, |
| 2801 | StoreFromKeyed store_mode) { |
| 2802 | LookupResult result(object->GetIsolate()); |
| 2803 | object->LocalLookup(*name, &result, true); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2804 | if (!result.IsFound()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2805 | object->map()->LookupTransition(JSObject::cast(*object), *name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2806 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2807 | return SetProperty(object, &result, name, value, attributes, strict_mode, |
| 2808 | store_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2812 | Handle<Object> JSObject::SetPropertyWithCallback(Handle<JSObject> object, |
| 2813 | Handle<Object> structure, |
| 2814 | Handle<Name> name, |
| 2815 | Handle<Object> value, |
| 2816 | Handle<JSObject> holder, |
| 2817 | StrictModeFlag strict_mode) { |
| 2818 | Isolate* isolate = object->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2819 | |
| 2820 | // We should never get here to initialize a const with the hole |
| 2821 | // value since a const declaration would conflict with the setter. |
| 2822 | ASSERT(!value->IsTheHole()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2823 | |
| 2824 | // 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] | 2825 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2826 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2827 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2828 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2829 | reinterpret_cast<AccessorDescriptor*>( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2830 | Handle<Foreign>::cast(structure)->foreign_address()); |
| 2831 | CALL_AND_RETRY_OR_DIE(isolate, |
| 2832 | (callback->setter)( |
| 2833 | isolate, *object, *value, callback->data), |
| 2834 | break, |
| 2835 | return Handle<Object>()); |
| 2836 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2837 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2840 | if (structure->IsExecutableAccessorInfo()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2841 | // api style callbacks |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2842 | ExecutableAccessorInfo* data = ExecutableAccessorInfo::cast(*structure); |
| 2843 | if (!data->IsCompatibleReceiver(*object)) { |
| 2844 | Handle<Object> args[2] = { name, object }; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2845 | Handle<Object> error = |
| 2846 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 2847 | HandleVector(args, |
| 2848 | ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2849 | isolate->Throw(*error); |
| 2850 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2851 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2852 | // TODO(rossberg): Support symbols in the API. |
| 2853 | if (name->IsSymbol()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2854 | Object* call_obj = data->setter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 2855 | v8::AccessorSetterCallback call_fun = |
| 2856 | v8::ToCData<v8::AccessorSetterCallback>(call_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2857 | if (call_fun == NULL) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2858 | Handle<String> key = Handle<String>::cast(name); |
| 2859 | LOG(isolate, ApiNamedPropertyAccess("store", *object, *name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2860 | PropertyCallbackArguments args( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2861 | isolate, data->data(), *object, JSObject::cast(*holder)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2862 | args.Call(call_fun, |
| 2863 | v8::Utils::ToLocal(key), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2864 | v8::Utils::ToLocal(value)); |
| 2865 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2866 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2869 | if (structure->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2870 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2871 | if (setter->IsSpecFunction()) { |
| 2872 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2873 | return SetPropertyWithDefinedSetter( |
| 2874 | object, Handle<JSReceiver>::cast(setter), value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2875 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2876 | if (strict_mode == kNonStrictMode) { |
| 2877 | return value; |
| 2878 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2879 | Handle<Object> args[2] = { name, holder }; |
| 2880 | Handle<Object> error = |
| 2881 | isolate->factory()->NewTypeError("no_setter_in_callback", |
| 2882 | HandleVector(args, 2)); |
| 2883 | isolate->Throw(*error); |
| 2884 | return Handle<Object>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2885 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2888 | // TODO(dcarney): Handle correctly. |
| 2889 | if (structure->IsDeclaredAccessorInfo()) { |
| 2890 | return value; |
| 2891 | } |
| 2892 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2893 | UNREACHABLE(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2894 | return Handle<Object>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2898 | Handle<Object> JSReceiver::SetPropertyWithDefinedSetter( |
| 2899 | Handle<JSReceiver> object, |
| 2900 | Handle<JSReceiver> setter, |
| 2901 | Handle<Object> value) { |
| 2902 | Isolate* isolate = object->GetIsolate(); |
| 2903 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2904 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2905 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2906 | // Handle stepping into a setter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2907 | // TODO(rossberg): should this apply to getters that are function proxies? |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2908 | if (debug->StepInActive() && setter->IsJSFunction()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2909 | debug->HandleStepIn( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2910 | Handle<JSFunction>::cast(setter), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2911 | } |
| 2912 | #endif |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2913 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2914 | bool has_pending_exception; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2915 | Handle<Object> argv[] = { value }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2916 | Execution::Call( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2917 | isolate, setter, object, ARRAY_SIZE(argv), argv, &has_pending_exception); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2918 | // Check for pending exception and return the result. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2919 | if (has_pending_exception) return Handle<Object>(); |
| 2920 | return value; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2923 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2924 | MaybeObject* JSObject::SetElementWithCallbackSetterInPrototypes( |
| 2925 | uint32_t index, |
| 2926 | Object* value, |
| 2927 | bool* found, |
| 2928 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2929 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2930 | for (Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2931 | pt != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 2932 | pt = pt->GetPrototype(GetIsolate())) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2933 | if (pt->IsJSProxy()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2934 | Isolate* isolate = GetIsolate(); |
| 2935 | HandleScope scope(isolate); |
| 2936 | Handle<JSProxy> proxy(JSProxy::cast(pt)); |
| 2937 | Handle<JSObject> self(this, isolate); |
| 2938 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 2939 | Handle<Object> value_handle(value, isolate); |
| 2940 | Handle<Object> result = JSProxy::SetPropertyViaPrototypesWithHandler( |
| 2941 | proxy, self, name, value_handle, NONE, strict_mode, found); |
| 2942 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
| 2943 | return *result; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2944 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2945 | if (!JSObject::cast(pt)->HasDictionaryElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2946 | continue; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2947 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2948 | SeededNumberDictionary* dictionary = |
| 2949 | JSObject::cast(pt)->element_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2950 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2951 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2952 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 2953 | if (details.type() == CALLBACKS) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2954 | *found = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2955 | Isolate* isolate = GetIsolate(); |
| 2956 | HandleScope scope(isolate); |
| 2957 | Handle<JSObject> self(this, isolate); |
| 2958 | Handle<Object> structure(dictionary->ValueAt(entry), isolate); |
| 2959 | Handle<Object> value_handle(value, isolate); |
| 2960 | Handle<JSObject> holder(JSObject::cast(pt)); |
| 2961 | Handle<Object> result = SetElementWithCallback( |
| 2962 | self, structure, index, value_handle, holder, strict_mode); |
| 2963 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
| 2964 | return *result; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 2968 | *found = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2969 | return heap->the_hole_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2972 | |
| 2973 | Handle<Object> JSObject::SetPropertyViaPrototypes(Handle<JSObject> object, |
| 2974 | Handle<Name> name, |
| 2975 | Handle<Object> value, |
| 2976 | PropertyAttributes attributes, |
| 2977 | StrictModeFlag strict_mode, |
| 2978 | bool* done) { |
| 2979 | Isolate* isolate = object->GetIsolate(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2980 | |
| 2981 | *done = false; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2982 | // 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] | 2983 | // accessor that wants to handle the property, or whether the property is |
| 2984 | // read-only on the prototype chain. |
| 2985 | LookupResult result(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2986 | object->LookupRealNamedPropertyInPrototypes(*name, &result); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2987 | if (result.IsFound()) { |
| 2988 | switch (result.type()) { |
| 2989 | case NORMAL: |
| 2990 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2991 | case CONSTANT: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2992 | *done = result.IsReadOnly(); |
| 2993 | break; |
| 2994 | case INTERCEPTOR: { |
| 2995 | PropertyAttributes attr = |
| 2996 | result.holder()->GetPropertyAttributeWithInterceptor( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2997 | *object, *name, true); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2998 | *done = !!(attr & READ_ONLY); |
| 2999 | break; |
| 3000 | } |
| 3001 | case CALLBACKS: { |
| 3002 | if (!FLAG_es5_readonly && result.IsReadOnly()) break; |
| 3003 | *done = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3004 | Handle<Object> callback_object(result.GetCallbackObject(), isolate); |
| 3005 | return SetPropertyWithCallback(object, callback_object, name, value, |
| 3006 | handle(result.holder()), strict_mode); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3007 | } |
| 3008 | case HANDLER: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3009 | Handle<JSProxy> proxy(result.proxy()); |
| 3010 | return JSProxy::SetPropertyViaPrototypesWithHandler( |
| 3011 | proxy, object, name, value, attributes, strict_mode, done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3012 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3013 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 3014 | case NONEXISTENT: |
| 3015 | UNREACHABLE(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3016 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3019 | |
| 3020 | // If we get here with *done true, we have encountered a read-only property. |
| 3021 | if (!FLAG_es5_readonly) *done = false; |
| 3022 | if (*done) { |
| 3023 | if (strict_mode == kNonStrictMode) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3024 | Handle<Object> args[] = { name, object }; |
| 3025 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3026 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
| 3027 | isolate->Throw(*error); |
| 3028 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3029 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3030 | return isolate->factory()->the_hole_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3033 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3034 | void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) { |
| 3035 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 3036 | if (slack <= descriptors->NumberOfSlackDescriptors()) return; |
| 3037 | int number_of_descriptors = descriptors->number_of_descriptors(); |
| 3038 | Isolate* isolate = map->GetIsolate(); |
| 3039 | Handle<DescriptorArray> new_descriptors = |
| 3040 | isolate->factory()->NewDescriptorArray(number_of_descriptors, slack); |
| 3041 | DescriptorArray::WhitenessWitness witness(*new_descriptors); |
| 3042 | |
| 3043 | for (int i = 0; i < number_of_descriptors; ++i) { |
| 3044 | new_descriptors->CopyFrom(i, *descriptors, i, witness); |
| 3045 | } |
| 3046 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3047 | map->set_instance_descriptors(*new_descriptors); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3051 | template<class T> |
| 3052 | static int AppendUniqueCallbacks(NeanderArray* callbacks, |
| 3053 | Handle<typename T::Array> array, |
| 3054 | int valid_descriptors) { |
| 3055 | int nof_callbacks = callbacks->length(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3056 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3057 | Isolate* isolate = array->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3058 | // Ensure the keys are unique names before writing them into the |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3059 | // 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] | 3060 | // temporarily put the heap in an invalid state while appending descriptors. |
| 3061 | for (int i = 0; i < nof_callbacks; ++i) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3062 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 3063 | if (entry->name()->IsUniqueName()) continue; |
| 3064 | Handle<String> key = |
| 3065 | isolate->factory()->InternalizedStringFromString( |
| 3066 | Handle<String>(String::cast(entry->name()))); |
| 3067 | entry->set_name(*key); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3070 | // Fill in new callback descriptors. Process the callbacks from |
| 3071 | // back to front so that the last callback with a given name takes |
| 3072 | // precedence over previously added callbacks with that name. |
| 3073 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3074 | AccessorInfo* entry = AccessorInfo::cast(callbacks->get(i)); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3075 | Name* key = Name::cast(entry->name()); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3076 | // Check if a descriptor with this name already exists before writing. |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3077 | if (!T::Contains(key, entry, valid_descriptors, array)) { |
| 3078 | T::Insert(key, entry, valid_descriptors, array); |
| 3079 | valid_descriptors++; |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3080 | } |
| 3081 | } |
| 3082 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3083 | return valid_descriptors; |
| 3084 | } |
| 3085 | |
| 3086 | struct DescriptorArrayAppender { |
| 3087 | typedef DescriptorArray Array; |
| 3088 | static bool Contains(Name* key, |
| 3089 | AccessorInfo* entry, |
| 3090 | int valid_descriptors, |
| 3091 | Handle<DescriptorArray> array) { |
| 3092 | return array->Search(key, valid_descriptors) != DescriptorArray::kNotFound; |
| 3093 | } |
| 3094 | static void Insert(Name* key, |
| 3095 | AccessorInfo* entry, |
| 3096 | int valid_descriptors, |
| 3097 | Handle<DescriptorArray> array) { |
| 3098 | CallbacksDescriptor desc(key, entry, entry->property_attributes()); |
| 3099 | array->Append(&desc); |
| 3100 | } |
| 3101 | }; |
| 3102 | |
| 3103 | |
| 3104 | struct FixedArrayAppender { |
| 3105 | typedef FixedArray Array; |
| 3106 | static bool Contains(Name* key, |
| 3107 | AccessorInfo* entry, |
| 3108 | int valid_descriptors, |
| 3109 | Handle<FixedArray> array) { |
| 3110 | for (int i = 0; i < valid_descriptors; i++) { |
| 3111 | if (key == AccessorInfo::cast(array->get(i))->name()) return true; |
| 3112 | } |
| 3113 | return false; |
| 3114 | } |
| 3115 | static void Insert(Name* key, |
| 3116 | AccessorInfo* entry, |
| 3117 | int valid_descriptors, |
| 3118 | Handle<FixedArray> array) { |
| 3119 | array->set(valid_descriptors, entry); |
| 3120 | } |
| 3121 | }; |
| 3122 | |
| 3123 | |
| 3124 | void Map::AppendCallbackDescriptors(Handle<Map> map, |
| 3125 | Handle<Object> descriptors) { |
| 3126 | int nof = map->NumberOfOwnDescriptors(); |
| 3127 | Handle<DescriptorArray> array(map->instance_descriptors()); |
| 3128 | NeanderArray callbacks(descriptors); |
| 3129 | ASSERT(array->NumberOfSlackDescriptors() >= callbacks.length()); |
| 3130 | nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3131 | map->SetNumberOfOwnDescriptors(nof); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3135 | int AccessorInfo::AppendUnique(Handle<Object> descriptors, |
| 3136 | Handle<FixedArray> array, |
| 3137 | int valid_descriptors) { |
| 3138 | NeanderArray callbacks(descriptors); |
| 3139 | ASSERT(array->length() >= callbacks.length() + valid_descriptors); |
| 3140 | return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks, |
| 3141 | array, |
| 3142 | valid_descriptors); |
| 3143 | } |
| 3144 | |
| 3145 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3146 | static bool ContainsMap(MapHandleList* maps, Handle<Map> map) { |
| 3147 | ASSERT(!map.is_null()); |
| 3148 | for (int i = 0; i < maps->length(); ++i) { |
| 3149 | if (!maps->at(i).is_null() && maps->at(i).is_identical_to(map)) return true; |
| 3150 | } |
| 3151 | return false; |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | template <class T> |
| 3156 | static Handle<T> MaybeNull(T* p) { |
| 3157 | if (p == NULL) return Handle<T>::null(); |
| 3158 | return Handle<T>(p); |
| 3159 | } |
| 3160 | |
| 3161 | |
| 3162 | Handle<Map> Map::FindTransitionedMap(MapHandleList* candidates) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3163 | ElementsKind kind = elements_kind(); |
| 3164 | Handle<Map> transitioned_map = Handle<Map>::null(); |
| 3165 | Handle<Map> current_map(this); |
| 3166 | bool packed = IsFastPackedElementsKind(kind); |
| 3167 | if (IsTransitionableFastElementsKind(kind)) { |
| 3168 | while (CanTransitionToMoreGeneralFastElementsKind(kind, false)) { |
| 3169 | kind = GetNextMoreGeneralFastElementsKind(kind, false); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3170 | Handle<Map> maybe_transitioned_map = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3171 | MaybeNull(current_map->LookupElementsTransitionMap(kind)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3172 | if (maybe_transitioned_map.is_null()) break; |
| 3173 | if (ContainsMap(candidates, maybe_transitioned_map) && |
| 3174 | (packed || !IsFastPackedElementsKind(kind))) { |
| 3175 | transitioned_map = maybe_transitioned_map; |
| 3176 | if (!IsFastPackedElementsKind(kind)) packed = false; |
| 3177 | } |
| 3178 | current_map = maybe_transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3179 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3180 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3181 | return transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3184 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3185 | static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) { |
| 3186 | Map* current_map = map; |
| 3187 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3188 | int to_index = IsFastElementsKind(to_kind) |
| 3189 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 3190 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 3191 | |
| 3192 | ASSERT(index <= to_index); |
| 3193 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3194 | for (; index < to_index; ++index) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3195 | if (!current_map->HasElementsTransition()) return current_map; |
| 3196 | current_map = current_map->elements_transition_map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3197 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3198 | if (!IsFastElementsKind(to_kind) && current_map->HasElementsTransition()) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3199 | Map* next_map = current_map->elements_transition_map(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3200 | if (next_map->elements_kind() == to_kind) return next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3201 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3202 | ASSERT(IsFastElementsKind(to_kind) |
| 3203 | ? current_map->elements_kind() == to_kind |
| 3204 | : current_map->elements_kind() == TERMINAL_FAST_ELEMENTS_KIND); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3205 | return current_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3209 | Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3210 | Map* to_map = FindClosestElementsTransition(this, to_kind); |
| 3211 | if (to_map->elements_kind() == to_kind) return to_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3212 | return NULL; |
| 3213 | } |
| 3214 | |
| 3215 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3216 | bool Map::IsMapInArrayPrototypeChain() { |
| 3217 | Isolate* isolate = GetIsolate(); |
| 3218 | if (isolate->initial_array_prototype()->map() == this) { |
| 3219 | return true; |
| 3220 | } |
| 3221 | |
| 3222 | if (isolate->initial_object_prototype()->map() == this) { |
| 3223 | return true; |
| 3224 | } |
| 3225 | |
| 3226 | return false; |
| 3227 | } |
| 3228 | |
| 3229 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3230 | static MaybeObject* AddMissingElementsTransitions(Map* map, |
| 3231 | ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3232 | ASSERT(IsFastElementsKind(map->elements_kind())); |
| 3233 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
| 3234 | int to_index = IsFastElementsKind(to_kind) |
| 3235 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 3236 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 3237 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3238 | ASSERT(index <= to_index); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3239 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3240 | Map* current_map = map; |
| 3241 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3242 | for (; index < to_index; ++index) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3243 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(index + 1); |
| 3244 | MaybeObject* maybe_next_map = |
| 3245 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 3246 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3249 | // In case we are exiting the fast elements kind system, just add the map in |
| 3250 | // the end. |
| 3251 | if (!IsFastElementsKind(to_kind)) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3252 | MaybeObject* maybe_next_map = |
| 3253 | current_map->CopyAsElementsKind(to_kind, INSERT_TRANSITION); |
| 3254 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3257 | ASSERT(current_map->elements_kind() == to_kind); |
| 3258 | return current_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3262 | Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object, |
| 3263 | ElementsKind to_kind) { |
| 3264 | Isolate* isolate = object->GetIsolate(); |
| 3265 | CALL_HEAP_FUNCTION(isolate, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3266 | object->GetElementsTransitionMap(isolate, to_kind), |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3267 | Map); |
| 3268 | } |
| 3269 | |
| 3270 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3271 | MaybeObject* JSObject::GetElementsTransitionMapSlow(ElementsKind to_kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3272 | Map* start_map = map(); |
| 3273 | ElementsKind from_kind = start_map->elements_kind(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3274 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3275 | if (from_kind == to_kind) { |
| 3276 | return start_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3279 | bool allow_store_transition = |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3280 | // 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] | 3281 | // non-matching element transition. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3282 | !start_map->IsUndefined() && !start_map->is_shared() && |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3283 | IsFastElementsKind(from_kind); |
| 3284 | |
| 3285 | // Only store fast element maps in ascending generality. |
| 3286 | if (IsFastElementsKind(to_kind)) { |
| 3287 | allow_store_transition &= |
| 3288 | IsTransitionableFastElementsKind(from_kind) && |
| 3289 | IsMoreGeneralElementsKindTransition(from_kind, to_kind); |
| 3290 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3291 | |
| 3292 | if (!allow_store_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3293 | return start_map->CopyAsElementsKind(to_kind, OMIT_TRANSITION); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3294 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3295 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3296 | return start_map->AsElementsKind(to_kind); |
| 3297 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3298 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3299 | |
| 3300 | MaybeObject* Map::AsElementsKind(ElementsKind kind) { |
| 3301 | Map* closest_map = FindClosestElementsTransition(this, kind); |
| 3302 | |
| 3303 | if (closest_map->elements_kind() == kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3304 | return closest_map; |
| 3305 | } |
| 3306 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3307 | return AddMissingElementsTransitions(closest_map, kind); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3311 | void JSObject::LocalLookupRealNamedProperty(Name* name, LookupResult* result) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3312 | if (IsJSGlobalProxy()) { |
| 3313 | Object* proto = GetPrototype(); |
| 3314 | if (proto->IsNull()) return result->NotFound(); |
| 3315 | ASSERT(proto->IsJSGlobalObject()); |
| 3316 | return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result); |
| 3317 | } |
| 3318 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3319 | if (HasFastProperties()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3320 | map()->LookupDescriptor(this, name, result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3321 | // A property or a map transition was found. We return all of these result |
| 3322 | // types because LocalLookupRealNamedProperty is used when setting |
| 3323 | // properties where map transitions are handled. |
| 3324 | ASSERT(!result->IsFound() || |
| 3325 | (result->holder() == this && result->IsFastPropertyType())); |
| 3326 | // Disallow caching for uninitialized constants. These can only |
| 3327 | // occur as fields. |
| 3328 | if (result->IsField() && |
| 3329 | result->IsReadOnly() && |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3330 | RawFastPropertyAt(result->GetFieldIndex().field_index())->IsTheHole()) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3331 | result->DisallowCaching(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3332 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3333 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3334 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3335 | |
| 3336 | int entry = property_dictionary()->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3337 | if (entry != NameDictionary::kNotFound) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3338 | Object* value = property_dictionary()->ValueAt(entry); |
| 3339 | if (IsGlobalObject()) { |
| 3340 | PropertyDetails d = property_dictionary()->DetailsAt(entry); |
| 3341 | if (d.IsDeleted()) { |
| 3342 | result->NotFound(); |
| 3343 | return; |
| 3344 | } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 3345 | value = PropertyCell::cast(value)->value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3346 | } |
| 3347 | // Make sure to disallow caching for uninitialized constants |
| 3348 | // found in the dictionary-mode objects. |
| 3349 | if (value->IsTheHole()) result->DisallowCaching(); |
| 3350 | result->DictionaryResult(this, entry); |
| 3351 | return; |
| 3352 | } |
| 3353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3354 | result->NotFound(); |
| 3355 | } |
| 3356 | |
| 3357 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3358 | void JSObject::LookupRealNamedProperty(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3359 | LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3360 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3361 | |
| 3362 | LookupRealNamedPropertyInPrototypes(name, result); |
| 3363 | } |
| 3364 | |
| 3365 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3366 | void JSObject::LookupRealNamedPropertyInPrototypes(Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3367 | LookupResult* result) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3368 | Isolate* isolate = GetIsolate(); |
| 3369 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3370 | for (Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3371 | pt != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3372 | pt = pt->GetPrototype(isolate)) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3373 | if (pt->IsJSProxy()) { |
| 3374 | return result->HandlerResult(JSProxy::cast(pt)); |
| 3375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3376 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3377 | ASSERT(!(result->IsFound() && result->type() == INTERCEPTOR)); |
| 3378 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3379 | } |
| 3380 | result->NotFound(); |
| 3381 | } |
| 3382 | |
| 3383 | |
| 3384 | // We only need to deal with CALLBACKS and INTERCEPTORS |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3385 | Handle<Object> JSObject::SetPropertyWithFailedAccessCheck( |
| 3386 | Handle<JSObject> object, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3387 | LookupResult* result, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3388 | Handle<Name> name, |
| 3389 | Handle<Object> value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3390 | bool check_prototype, |
| 3391 | StrictModeFlag strict_mode) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3392 | if (check_prototype && !result->IsProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3393 | object->LookupRealNamedPropertyInPrototypes(*name, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | if (result->IsProperty()) { |
| 3397 | if (!result->IsReadOnly()) { |
| 3398 | switch (result->type()) { |
| 3399 | case CALLBACKS: { |
| 3400 | Object* obj = result->GetCallbackObject(); |
| 3401 | if (obj->IsAccessorInfo()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3402 | Handle<AccessorInfo> info(AccessorInfo::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3403 | if (info->all_can_write()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3404 | return SetPropertyWithCallback(object, |
| 3405 | info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3406 | name, |
| 3407 | value, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3408 | handle(result->holder()), |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3409 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3410 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3411 | } else if (obj->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3412 | Handle<AccessorPair> pair(AccessorPair::cast(obj)); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3413 | if (pair->all_can_read()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3414 | return SetPropertyWithCallback(object, |
| 3415 | pair, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3416 | name, |
| 3417 | value, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3418 | handle(result->holder()), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3419 | strict_mode); |
| 3420 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3421 | } |
| 3422 | break; |
| 3423 | } |
| 3424 | case INTERCEPTOR: { |
| 3425 | // Try lookup real named properties. Note that only property can be |
| 3426 | // set is callbacks marked as ALL_CAN_WRITE on the prototype chain. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3427 | LookupResult r(object->GetIsolate()); |
| 3428 | object->LookupRealNamedProperty(*name, &r); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3429 | if (r.IsProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3430 | return SetPropertyWithFailedAccessCheck(object, |
| 3431 | &r, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3432 | name, |
| 3433 | value, |
| 3434 | check_prototype, |
| 3435 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3436 | } |
| 3437 | break; |
| 3438 | } |
| 3439 | default: { |
| 3440 | break; |
| 3441 | } |
| 3442 | } |
| 3443 | } |
| 3444 | } |
| 3445 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3446 | Isolate* isolate = object->GetIsolate(); |
| 3447 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 3448 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 3449 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3453 | Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object, |
| 3454 | LookupResult* result, |
| 3455 | Handle<Name> key, |
| 3456 | Handle<Object> value, |
| 3457 | PropertyAttributes attributes, |
| 3458 | StrictModeFlag strict_mode, |
| 3459 | StoreFromKeyed store_mode) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 3460 | if (result->IsHandler()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3461 | return JSProxy::SetPropertyWithHandler(handle(result->proxy()), |
| 3462 | object, key, value, attributes, strict_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3463 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3464 | return JSObject::SetPropertyForResult(Handle<JSObject>::cast(object), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3465 | result, key, value, attributes, strict_mode, store_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3470 | bool JSProxy::HasPropertyWithHandler(Handle<JSProxy> proxy, Handle<Name> name) { |
| 3471 | Isolate* isolate = proxy->GetIsolate(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3472 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3473 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3474 | if (name->IsSymbol()) return false; |
| 3475 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3476 | Handle<Object> args[] = { name }; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3477 | Handle<Object> result = proxy->CallTrap( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3478 | "has", isolate->derived_has_trap(), ARRAY_SIZE(args), args); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3479 | if (isolate->has_pending_exception()) return false; |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3480 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3481 | return result->BooleanValue(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
| 3484 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3485 | Handle<Object> JSProxy::SetPropertyWithHandler(Handle<JSProxy> proxy, |
| 3486 | Handle<JSReceiver> receiver, |
| 3487 | Handle<Name> name, |
| 3488 | Handle<Object> value, |
| 3489 | PropertyAttributes attributes, |
| 3490 | StrictModeFlag strict_mode) { |
| 3491 | Isolate* isolate = proxy->GetIsolate(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3492 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3493 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3494 | if (name->IsSymbol()) return value; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3495 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3496 | Handle<Object> args[] = { receiver, name, value }; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3497 | proxy->CallTrap("set", isolate->derived_set_trap(), ARRAY_SIZE(args), args); |
| 3498 | if (isolate->has_pending_exception()) return Handle<Object>(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3499 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3500 | return value; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3504 | Handle<Object> JSProxy::SetPropertyViaPrototypesWithHandler( |
| 3505 | Handle<JSProxy> proxy, |
| 3506 | Handle<JSReceiver> receiver, |
| 3507 | Handle<Name> name, |
| 3508 | Handle<Object> value, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3509 | PropertyAttributes attributes, |
| 3510 | StrictModeFlag strict_mode, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3511 | bool* done) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3512 | Isolate* isolate = proxy->GetIsolate(); |
| 3513 | Handle<Object> handler(proxy->handler(), isolate); // Trap might morph proxy. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3514 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3515 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3516 | if (name->IsSymbol()) { |
| 3517 | *done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3518 | return isolate->factory()->the_hole_value(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3521 | *done = true; // except where redefined... |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3522 | Handle<Object> args[] = { name }; |
| 3523 | Handle<Object> result = proxy->CallTrap( |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3524 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3525 | if (isolate->has_pending_exception()) return Handle<Object>(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3526 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3527 | if (result->IsUndefined()) { |
| 3528 | *done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3529 | return isolate->factory()->the_hole_value(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3532 | // Emulate [[GetProperty]] semantics for proxies. |
| 3533 | bool has_pending_exception; |
| 3534 | Handle<Object> argv[] = { result }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3535 | Handle<Object> desc = Execution::Call( |
| 3536 | isolate, isolate->to_complete_property_descriptor(), result, |
| 3537 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3538 | if (has_pending_exception) return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3539 | |
| 3540 | // [[GetProperty]] requires to check that all properties are configurable. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3541 | Handle<String> configurable_name = |
| 3542 | isolate->factory()->InternalizeOneByteString( |
| 3543 | STATIC_ASCII_VECTOR("configurable_")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3544 | Handle<Object> configurable( |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3545 | v8::internal::GetProperty(isolate, desc, configurable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3546 | ASSERT(!isolate->has_pending_exception()); |
| 3547 | ASSERT(configurable->IsTrue() || configurable->IsFalse()); |
| 3548 | if (configurable->IsFalse()) { |
| 3549 | Handle<String> trap = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3550 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3551 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3552 | Handle<Object> args[] = { handler, trap, name }; |
| 3553 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3554 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3555 | isolate->Throw(*error); |
| 3556 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3557 | } |
| 3558 | ASSERT(configurable->IsTrue()); |
| 3559 | |
| 3560 | // Check for DataDescriptor. |
| 3561 | Handle<String> hasWritable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3562 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3563 | STATIC_ASCII_VECTOR("hasWritable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3564 | Handle<Object> hasWritable( |
| 3565 | v8::internal::GetProperty(isolate, desc, hasWritable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3566 | ASSERT(!isolate->has_pending_exception()); |
| 3567 | ASSERT(hasWritable->IsTrue() || hasWritable->IsFalse()); |
| 3568 | if (hasWritable->IsTrue()) { |
| 3569 | Handle<String> writable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3570 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3571 | STATIC_ASCII_VECTOR("writable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3572 | Handle<Object> writable( |
| 3573 | v8::internal::GetProperty(isolate, desc, writable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3574 | ASSERT(!isolate->has_pending_exception()); |
| 3575 | ASSERT(writable->IsTrue() || writable->IsFalse()); |
| 3576 | *done = writable->IsFalse(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3577 | if (!*done) return isolate->factory()->the_hole_value(); |
| 3578 | if (strict_mode == kNonStrictMode) return value; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3579 | Handle<Object> args[] = { name, receiver }; |
| 3580 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3581 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3582 | isolate->Throw(*error); |
| 3583 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | // We have an AccessorDescriptor. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3587 | Handle<String> set_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3588 | STATIC_ASCII_VECTOR("set_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3589 | Handle<Object> setter(v8::internal::GetProperty(isolate, desc, set_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3590 | ASSERT(!isolate->has_pending_exception()); |
| 3591 | if (!setter->IsUndefined()) { |
| 3592 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3593 | return SetPropertyWithDefinedSetter( |
| 3594 | receiver, Handle<JSReceiver>::cast(setter), value); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3597 | if (strict_mode == kNonStrictMode) return value; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3598 | Handle<Object> args2[] = { name, proxy }; |
| 3599 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3600 | "no_setter_in_callback", HandleVector(args2, ARRAY_SIZE(args2))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3601 | isolate->Throw(*error); |
| 3602 | return Handle<Object>(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3606 | Handle<Object> JSProxy::DeletePropertyWithHandler( |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3607 | Handle<JSProxy> proxy, Handle<Name> name, DeleteMode mode) { |
| 3608 | Isolate* isolate = proxy->GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3609 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3610 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3611 | if (name->IsSymbol()) return isolate->factory()->false_value(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3612 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3613 | Handle<Object> args[] = { name }; |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3614 | Handle<Object> result = proxy->CallTrap( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3615 | "delete", Handle<Object>(), ARRAY_SIZE(args), args); |
| 3616 | if (isolate->has_pending_exception()) return Handle<Object>(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3617 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3618 | bool result_bool = result->BooleanValue(); |
| 3619 | if (mode == STRICT_DELETION && !result_bool) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3620 | Handle<Object> handler(proxy->handler(), isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3621 | Handle<String> trap_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3622 | STATIC_ASCII_VECTOR("delete")); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 3623 | Handle<Object> args[] = { handler, trap_name }; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3624 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3625 | "handler_failed", HandleVector(args, ARRAY_SIZE(args))); |
| 3626 | isolate->Throw(*error); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3627 | return Handle<Object>(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3628 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3629 | return isolate->factory()->ToBoolean(result_bool); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3633 | Handle<Object> JSProxy::DeleteElementWithHandler( |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3634 | Handle<JSProxy> proxy, uint32_t index, DeleteMode mode) { |
| 3635 | Isolate* isolate = proxy->GetIsolate(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3636 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3637 | return JSProxy::DeletePropertyWithHandler(proxy, name, mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | |
| 3641 | MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler( |
| 3642 | JSReceiver* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3643 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3644 | Isolate* isolate = GetIsolate(); |
| 3645 | HandleScope scope(isolate); |
| 3646 | Handle<JSProxy> proxy(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3647 | Handle<Object> handler(this->handler(), isolate); // Trap might morph proxy. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3648 | Handle<JSReceiver> receiver(receiver_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3649 | Handle<Object> name(name_raw, isolate); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3650 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3651 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3652 | if (name->IsSymbol()) return ABSENT; |
| 3653 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3654 | Handle<Object> args[] = { name }; |
| 3655 | Handle<Object> result = CallTrap( |
| 3656 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3657 | if (isolate->has_pending_exception()) return NONE; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3658 | |
| 3659 | if (result->IsUndefined()) return ABSENT; |
| 3660 | |
| 3661 | bool has_pending_exception; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3662 | Handle<Object> argv[] = { result }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3663 | Handle<Object> desc = Execution::Call( |
| 3664 | isolate, isolate->to_complete_property_descriptor(), result, |
| 3665 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3666 | if (has_pending_exception) return NONE; |
| 3667 | |
| 3668 | // Convert result to PropertyAttributes. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3669 | Handle<String> enum_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3670 | STATIC_ASCII_VECTOR("enumerable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3671 | Handle<Object> enumerable(v8::internal::GetProperty(isolate, desc, enum_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3672 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3673 | Handle<String> conf_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3674 | STATIC_ASCII_VECTOR("configurable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3675 | Handle<Object> configurable(v8::internal::GetProperty(isolate, desc, conf_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3676 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3677 | Handle<String> writ_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3678 | STATIC_ASCII_VECTOR("writable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3679 | Handle<Object> writable(v8::internal::GetProperty(isolate, desc, writ_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3680 | if (isolate->has_pending_exception()) return NONE; |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3681 | if (!writable->BooleanValue()) { |
| 3682 | Handle<String> set_n = isolate->factory()->InternalizeOneByteString( |
| 3683 | STATIC_ASCII_VECTOR("set_")); |
| 3684 | Handle<Object> setter(v8::internal::GetProperty(isolate, desc, set_n)); |
| 3685 | if (isolate->has_pending_exception()) return NONE; |
| 3686 | writable = isolate->factory()->ToBoolean(!setter->IsUndefined()); |
| 3687 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3688 | |
| 3689 | if (configurable->IsFalse()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3690 | Handle<String> trap = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3691 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3692 | Handle<Object> args[] = { handler, trap, name }; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3693 | Handle<Object> error = isolate->factory()->NewTypeError( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3694 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3695 | isolate->Throw(*error); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3696 | return NONE; |
| 3697 | } |
| 3698 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3699 | int attributes = NONE; |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3700 | if (!enumerable->BooleanValue()) attributes |= DONT_ENUM; |
| 3701 | if (!configurable->BooleanValue()) attributes |= DONT_DELETE; |
| 3702 | if (!writable->BooleanValue()) attributes |= READ_ONLY; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3703 | return static_cast<PropertyAttributes>(attributes); |
| 3704 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3705 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3706 | |
| 3707 | MUST_USE_RESULT PropertyAttributes JSProxy::GetElementAttributeWithHandler( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3708 | JSReceiver* receiver_raw, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3709 | uint32_t index) { |
| 3710 | Isolate* isolate = GetIsolate(); |
| 3711 | HandleScope scope(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3712 | Handle<JSProxy> proxy(this); |
| 3713 | Handle<JSReceiver> receiver(receiver_raw); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3714 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3715 | return proxy->GetPropertyAttributeWithHandler(*receiver, *name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3719 | void JSProxy::Fix(Handle<JSProxy> proxy) { |
| 3720 | Isolate* isolate = proxy->GetIsolate(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3721 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3722 | // Save identity hash. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3723 | Handle<Object> hash = JSProxy::GetIdentityHash(proxy, OMIT_CREATION); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3724 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3725 | if (proxy->IsJSFunctionProxy()) { |
| 3726 | isolate->factory()->BecomeJSFunction(proxy); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3727 | // Code will be set on the JavaScript side. |
| 3728 | } else { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3729 | isolate->factory()->BecomeJSObject(proxy); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3730 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3731 | ASSERT(proxy->IsJSObject()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Inherit identity, if it was present. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3734 | if (hash->IsSmi()) { |
| 3735 | JSObject::SetIdentityHash(Handle<JSObject>::cast(proxy), Smi::cast(*hash)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3736 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3740 | MUST_USE_RESULT Handle<Object> JSProxy::CallTrap(const char* name, |
| 3741 | Handle<Object> derived, |
| 3742 | int argc, |
| 3743 | Handle<Object> argv[]) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3744 | Isolate* isolate = GetIsolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3745 | Handle<Object> handler(this->handler(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3746 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3747 | Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3748 | Handle<Object> trap(v8::internal::GetProperty(isolate, handler, trap_name)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3749 | if (isolate->has_pending_exception()) return trap; |
| 3750 | |
| 3751 | if (trap->IsUndefined()) { |
| 3752 | if (derived.is_null()) { |
| 3753 | Handle<Object> args[] = { handler, trap_name }; |
| 3754 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3755 | "handler_trap_missing", HandleVector(args, ARRAY_SIZE(args))); |
| 3756 | isolate->Throw(*error); |
| 3757 | return Handle<Object>(); |
| 3758 | } |
| 3759 | trap = Handle<Object>(derived); |
| 3760 | } |
| 3761 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3762 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3763 | return Execution::Call(isolate, trap, handler, argc, argv, &threw); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3766 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3767 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 3768 | static Handle<Map> MapAsElementsKind(Handle<Map> map, ElementsKind kind) { |
| 3769 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->AsElementsKind(kind), Map); |
| 3770 | } |
| 3771 | |
| 3772 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3773 | void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3774 | ASSERT(object->map()->inobject_properties() == map->inobject_properties()); |
| 3775 | ElementsKind obj_kind = object->map()->elements_kind(); |
| 3776 | ElementsKind map_kind = map->elements_kind(); |
| 3777 | if (map_kind != obj_kind) { |
| 3778 | ElementsKind to_kind = map_kind; |
| 3779 | if (IsMoreGeneralElementsKindTransition(map_kind, obj_kind) || |
| 3780 | IsDictionaryElementsKind(obj_kind)) { |
| 3781 | to_kind = obj_kind; |
| 3782 | } |
| 3783 | if (IsDictionaryElementsKind(to_kind)) { |
| 3784 | NormalizeElements(object); |
| 3785 | } else { |
| 3786 | TransitionElementsKind(object, to_kind); |
| 3787 | } |
| 3788 | map = MapAsElementsKind(map, to_kind); |
| 3789 | } |
| 3790 | int total_size = |
| 3791 | map->NumberOfOwnDescriptors() + map->unused_property_fields(); |
| 3792 | int out_of_object = total_size - map->inobject_properties(); |
| 3793 | if (out_of_object != object->properties()->length()) { |
| 3794 | Isolate* isolate = object->GetIsolate(); |
| 3795 | Handle<FixedArray> new_properties = isolate->factory()->CopySizeFixedArray( |
| 3796 | handle(object->properties()), out_of_object); |
| 3797 | object->set_properties(*new_properties); |
| 3798 | } |
| 3799 | object->set_map(*map); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3803 | void JSObject::MigrateInstance(Handle<JSObject> object) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3804 | // Converting any field to the most specific type will cause the |
| 3805 | // GeneralizeFieldRepresentation algorithm to create the most general existing |
| 3806 | // transition that matches the object. This achieves what is needed. |
| 3807 | Handle<Map> original_map(object->map()); |
| 3808 | GeneralizeFieldRepresentation( |
| 3809 | object, 0, Representation::None(), ALLOW_AS_CONSTANT); |
| 3810 | if (FLAG_trace_migration) { |
| 3811 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 3812 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 3816 | Handle<Object> JSObject::TryMigrateInstance(Handle<JSObject> object) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 3817 | Map* new_map = object->map()->CurrentMapForDeprecated(); |
| 3818 | if (new_map == NULL) return Handle<Object>(); |
| 3819 | Handle<Map> original_map(object->map()); |
| 3820 | JSObject::MigrateToMap(object, handle(new_map)); |
| 3821 | if (FLAG_trace_migration) { |
| 3822 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 3823 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3824 | return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3828 | Handle<Object> JSObject::SetPropertyUsingTransition( |
| 3829 | Handle<JSObject> object, |
| 3830 | LookupResult* lookup, |
| 3831 | Handle<Name> name, |
| 3832 | Handle<Object> value, |
| 3833 | PropertyAttributes attributes) { |
| 3834 | Handle<Map> transition_map(lookup->GetTransitionTarget()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3835 | int descriptor = transition_map->LastAdded(); |
| 3836 | |
| 3837 | DescriptorArray* descriptors = transition_map->instance_descriptors(); |
| 3838 | PropertyDetails details = descriptors->GetDetails(descriptor); |
| 3839 | |
| 3840 | if (details.type() == CALLBACKS || attributes != details.attributes()) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3841 | // AddProperty will either normalize the object, or create a new fast copy |
| 3842 | // of the map. If we get a fast copy of the map, all field representations |
| 3843 | // will be tagged since the transition is omitted. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3844 | return JSObject::AddProperty( |
| 3845 | object, name, value, attributes, kNonStrictMode, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3846 | JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED, |
| 3847 | JSReceiver::OMIT_EXTENSIBILITY_CHECK, |
| 3848 | JSObject::FORCE_TAGGED, FORCE_FIELD, OMIT_TRANSITION); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | // Keep the target CONSTANT if the same value is stored. |
| 3852 | // TODO(verwaest): Also support keeping the placeholder |
| 3853 | // (value->IsUninitialized) as constant. |
| 3854 | if (details.type() == CONSTANT && |
| 3855 | descriptors->GetValue(descriptor) == *value) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3856 | object->set_map(*transition_map); |
| 3857 | return value; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | Representation representation = details.representation(); |
| 3861 | |
| 3862 | if (!value->FitsRepresentation(representation) || |
| 3863 | details.type() == CONSTANT) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3864 | transition_map = Map::GeneralizeRepresentation(transition_map, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3865 | descriptor, value->OptimalRepresentation(), FORCE_FIELD); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3866 | Object* back = transition_map->GetBackPointer(); |
| 3867 | if (back->IsMap()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3868 | MigrateToMap(object, handle(Map::cast(back))); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3869 | } |
| 3870 | descriptors = transition_map->instance_descriptors(); |
| 3871 | representation = descriptors->GetDetails(descriptor).representation(); |
| 3872 | } |
| 3873 | |
| 3874 | int field_index = descriptors->GetFieldIndex(descriptor); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3875 | AddFastPropertyUsingMap( |
| 3876 | object, transition_map, name, value, field_index, representation); |
| 3877 | return value; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3881 | static void SetPropertyToField(LookupResult* lookup, |
| 3882 | Handle<Name> name, |
| 3883 | Handle<Object> value) { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3884 | Representation representation = lookup->representation(); |
| 3885 | if (!value->FitsRepresentation(representation) || |
| 3886 | lookup->type() == CONSTANT) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3887 | JSObject::GeneralizeFieldRepresentation(handle(lookup->holder()), |
| 3888 | lookup->GetDescriptorIndex(), |
| 3889 | value->OptimalRepresentation(), |
| 3890 | FORCE_FIELD); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3891 | DescriptorArray* desc = lookup->holder()->map()->instance_descriptors(); |
| 3892 | int descriptor = lookup->GetDescriptorIndex(); |
| 3893 | representation = desc->GetDetails(descriptor).representation(); |
| 3894 | } |
| 3895 | |
| 3896 | if (FLAG_track_double_fields && representation.IsDouble()) { |
| 3897 | HeapNumber* storage = HeapNumber::cast(lookup->holder()->RawFastPropertyAt( |
| 3898 | lookup->GetFieldIndex().field_index())); |
| 3899 | storage->set_value(value->Number()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3900 | return; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | lookup->holder()->FastPropertyAtPut( |
| 3904 | lookup->GetFieldIndex().field_index(), *value); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3908 | static void ConvertAndSetLocalProperty(LookupResult* lookup, |
| 3909 | Handle<Name> name, |
| 3910 | Handle<Object> value, |
| 3911 | PropertyAttributes attributes) { |
| 3912 | Handle<JSObject> object(lookup->holder()); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3913 | if (object->TooManyFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3914 | JSObject::NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | if (!object->HasFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3918 | ReplaceSlowProperty(object, name, value, attributes); |
| 3919 | return; |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | int descriptor_index = lookup->GetDescriptorIndex(); |
| 3923 | if (lookup->GetAttributes() == attributes) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3924 | JSObject::GeneralizeFieldRepresentation( |
| 3925 | object, descriptor_index, Representation::Tagged(), FORCE_FIELD); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3926 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3927 | Handle<Map> old_map(object->map()); |
| 3928 | Handle<Map> new_map = Map::CopyGeneralizeAllRepresentations(old_map, |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3929 | descriptor_index, FORCE_FIELD, attributes, "attributes mismatch"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3930 | JSObject::MigrateToMap(object, new_map); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | DescriptorArray* descriptors = object->map()->instance_descriptors(); |
| 3934 | int index = descriptors->GetDetails(descriptor_index).field_index(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3935 | object->FastPropertyAtPut(index, *value); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3939 | static void SetPropertyToFieldWithAttributes(LookupResult* lookup, |
| 3940 | Handle<Name> name, |
| 3941 | Handle<Object> value, |
| 3942 | PropertyAttributes attributes) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3943 | if (lookup->GetAttributes() == attributes) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3944 | if (value->IsUninitialized()) return; |
| 3945 | SetPropertyToField(lookup, name, value); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3946 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3947 | ConvertAndSetLocalProperty(lookup, name, value, attributes); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3952 | Handle<Object> JSObject::SetPropertyForResult(Handle<JSObject> object, |
| 3953 | LookupResult* lookup, |
| 3954 | Handle<Name> name, |
| 3955 | Handle<Object> value, |
| 3956 | PropertyAttributes attributes, |
| 3957 | StrictModeFlag strict_mode, |
| 3958 | StoreFromKeyed store_mode) { |
| 3959 | Isolate* isolate = object->GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 3960 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3961 | // Make sure that the top context does not change when doing callbacks or |
| 3962 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 3963 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3964 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3965 | // 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] | 3966 | // dictionary. We internalize these short keys to avoid constantly |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3967 | // reallocating them. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3968 | if (name->IsString() && !name->IsInternalizedString() && |
| 3969 | Handle<String>::cast(name)->length() <= 2) { |
| 3970 | name = isolate->factory()->InternalizeString(Handle<String>::cast(name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3972 | |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 3973 | // Check access rights if needed. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3974 | if (object->IsAccessCheckNeeded()) { |
| 3975 | if (!isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 3976 | return SetPropertyWithFailedAccessCheck(object, lookup, name, value, |
| 3977 | true, strict_mode); |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 3978 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3981 | if (object->IsJSGlobalProxy()) { |
| 3982 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 3983 | if (proto->IsNull()) return value; |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 3984 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3985 | return SetPropertyForResult(Handle<JSObject>::cast(proto), |
| 3986 | lookup, name, value, attributes, strict_mode, store_mode); |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3989 | ASSERT(!lookup->IsFound() || lookup->holder() == *object || |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3990 | lookup->holder()->map()->is_hidden_prototype()); |
| 3991 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3992 | if (!lookup->IsProperty() && !object->IsJSContextExtensionObject()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3993 | bool done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3994 | Handle<Object> result_object = SetPropertyViaPrototypes( |
| 3995 | object, name, value, attributes, strict_mode, &done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3996 | if (done) return result_object; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3997 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3998 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3999 | if (!lookup->IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4000 | // Neither properties nor transitions found. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4001 | return AddProperty( |
| 4002 | object, name, value, attributes, strict_mode, store_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4003 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4004 | |
| 4005 | if (lookup->IsProperty() && lookup->IsReadOnly()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4006 | if (strict_mode == kStrictMode) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4007 | Handle<Object> args[] = { name, object }; |
| 4008 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 4009 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
| 4010 | isolate->Throw(*error); |
| 4011 | return Handle<Object>(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4012 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4013 | return value; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4016 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4017 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 4018 | if (FLAG_harmony_observation && |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4019 | object->map()->is_observed() && lookup->IsDataProperty()) { |
| 4020 | old_value = Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4023 | // This is a real property that is not read-only, or it is a |
| 4024 | // transition or null descriptor and there are no setters in the prototypes. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4025 | Handle<Object> result = value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4026 | switch (lookup->type()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4027 | case NORMAL: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4028 | SetNormalizedProperty(handle(lookup->holder()), lookup, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4029 | break; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4030 | case FIELD: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4031 | SetPropertyToField(lookup, name, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4032 | break; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4033 | case CONSTANT: |
| 4034 | // Only replace the constant if necessary. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4035 | if (*value == lookup->GetConstant()) return value; |
| 4036 | SetPropertyToField(lookup, name, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4037 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4038 | case CALLBACKS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4039 | Handle<Object> callback_object(lookup->GetCallbackObject(), isolate); |
| 4040 | return SetPropertyWithCallback(object, callback_object, name, value, |
| 4041 | handle(lookup->holder()), strict_mode); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4042 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4043 | case INTERCEPTOR: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4044 | result = SetPropertyWithInterceptor(handle(lookup->holder()), name, value, |
| 4045 | attributes, strict_mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4046 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4047 | case TRANSITION: |
| 4048 | result = SetPropertyUsingTransition(handle(lookup->holder()), lookup, |
| 4049 | name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4050 | break; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4051 | case HANDLER: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4052 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4053 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4055 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4056 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<Object>()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4057 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4058 | if (FLAG_harmony_observation && object->map()->is_observed()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4059 | if (lookup->IsTransition()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4060 | EnqueueChangeRecord(object, "new", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4061 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 4062 | LookupResult new_lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4063 | object->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4064 | if (new_lookup.IsDataProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4065 | Handle<Object> new_value = Object::GetProperty(object, name); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4066 | if (!new_value->SameValue(*old_value)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4067 | EnqueueChangeRecord(object, "updated", name, old_value); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4068 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4073 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4077 | MaybeObject* JSObject::SetLocalPropertyIgnoreAttributesTrampoline( |
| 4078 | Name* key, |
| 4079 | Object* value, |
| 4080 | PropertyAttributes attributes, |
| 4081 | ValueType value_type, |
| 4082 | StoreMode mode, |
| 4083 | ExtensibilityCheck extensibility_check) { |
| 4084 | // TODO(mstarzinger): The trampoline is a giant hack, don't use it anywhere |
| 4085 | // else or handlification people will start hating you for all eternity. |
| 4086 | HandleScope scope(GetIsolate()); |
| 4087 | IdempotentPointerToHandleCodeTrampoline trampoline(GetIsolate()); |
| 4088 | return trampoline.CallWithReturnValue( |
| 4089 | &JSObject::SetLocalPropertyIgnoreAttributes, |
| 4090 | Handle<JSObject>(this), |
| 4091 | Handle<Name>(key), |
| 4092 | Handle<Object>(value, GetIsolate()), |
| 4093 | attributes, |
| 4094 | value_type, |
| 4095 | mode, |
| 4096 | extensibility_check); |
| 4097 | } |
| 4098 | |
| 4099 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4100 | // 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] | 4101 | // present, add it with attributes NONE. This code is an exact clone of |
| 4102 | // SetProperty, with the check for IsReadOnly and the check for a |
| 4103 | // callback setter removed. The two lines looking up the LookupResult |
| 4104 | // result are also added. If one of the functions is changed, the other |
| 4105 | // should be. |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 4106 | // Note that this method cannot be used to set the prototype of a function |
| 4107 | // because ConvertDescriptorToField() which is called in "case CALLBACKS:" |
| 4108 | // doesn't handle function prototypes correctly. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4109 | Handle<Object> JSObject::SetLocalPropertyIgnoreAttributes( |
| 4110 | Handle<JSObject> object, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4111 | Handle<Name> name, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4112 | Handle<Object> value, |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 4113 | PropertyAttributes attributes, |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4114 | ValueType value_type, |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4115 | StoreMode mode, |
| 4116 | ExtensibilityCheck extensibility_check) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4117 | Isolate* isolate = object->GetIsolate(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4118 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4119 | // Make sure that the top context does not change when doing callbacks or |
| 4120 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4121 | AssertNoContextChange ncc(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4122 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4123 | LookupResult lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4124 | object->LocalLookup(*name, &lookup, true); |
| 4125 | if (!lookup.IsFound()) { |
| 4126 | object->map()->LookupTransition(*object, *name, &lookup); |
| 4127 | } |
| 4128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4129 | // Check access rights if needed. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4130 | if (object->IsAccessCheckNeeded()) { |
| 4131 | if (!isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 4132 | return SetPropertyWithFailedAccessCheck(object, &lookup, name, value, |
| 4133 | false, kNonStrictMode); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4134 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4135 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4136 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4137 | if (object->IsJSGlobalProxy()) { |
| 4138 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 4139 | if (proto->IsNull()) return value; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4140 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4141 | return SetLocalPropertyIgnoreAttributes(Handle<JSObject>::cast(proto), |
| 4142 | name, value, attributes, value_type, mode, extensibility_check); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4145 | if (lookup.IsFound() && |
| 4146 | (lookup.type() == INTERCEPTOR || lookup.type() == CALLBACKS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4147 | object->LocalLookupRealNamedProperty(*name, &lookup); |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4150 | // Check for accessor in prototype chain removed here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4151 | if (!lookup.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4152 | // Neither properties nor transitions found. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4153 | return AddProperty(object, name, value, attributes, kNonStrictMode, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4154 | MAY_BE_STORE_FROM_KEYED, extensibility_check, value_type, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4157 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4158 | PropertyAttributes old_attributes = ABSENT; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4159 | bool is_observed = FLAG_harmony_observation && object->map()->is_observed(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 4160 | if (is_observed && lookup.IsProperty()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 4161 | if (lookup.IsDataProperty()) old_value = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4162 | Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4163 | old_attributes = lookup.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4164 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 4165 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4166 | // Check of IsReadOnly removed from here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4167 | switch (lookup.type()) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4168 | case NORMAL: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4169 | ReplaceSlowProperty(object, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4170 | break; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4171 | case FIELD: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4172 | SetPropertyToFieldWithAttributes(&lookup, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4173 | break; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4174 | case CONSTANT: |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4175 | // Only replace the constant if necessary. |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4176 | if (lookup.GetAttributes() != attributes || |
| 4177 | *value != lookup.GetConstant()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4178 | SetPropertyToFieldWithAttributes(&lookup, name, value, attributes); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4179 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4180 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4181 | case CALLBACKS: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4182 | ConvertAndSetLocalProperty(&lookup, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4183 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4184 | case TRANSITION: { |
| 4185 | Handle<Object> result = SetPropertyUsingTransition( |
| 4186 | handle(lookup.holder()), &lookup, name, value, attributes); |
| 4187 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<Object>()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4188 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4189 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4190 | case NONEXISTENT: |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4191 | case HANDLER: |
| 4192 | case INTERCEPTOR: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4193 | UNREACHABLE(); |
| 4194 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4195 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 4196 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4197 | if (lookup.IsTransition()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4198 | EnqueueChangeRecord(object, "new", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4199 | } else if (old_value->IsTheHole()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4200 | EnqueueChangeRecord(object, "reconfigured", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4201 | } else { |
| 4202 | LookupResult new_lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4203 | object->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4204 | bool value_changed = false; |
| 4205 | if (new_lookup.IsDataProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4206 | Handle<Object> new_value = Object::GetProperty(object, name); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4207 | value_changed = !old_value->SameValue(*new_value); |
| 4208 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4209 | if (new_lookup.GetAttributes() != old_attributes) { |
| 4210 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4211 | EnqueueChangeRecord(object, "reconfigured", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4212 | } else if (value_changed) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4213 | EnqueueChangeRecord(object, "updated", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4214 | } |
| 4215 | } |
| 4216 | } |
| 4217 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4218 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | |
| 4222 | PropertyAttributes JSObject::GetPropertyAttributePostInterceptor( |
| 4223 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4224 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4225 | bool continue_search) { |
| 4226 | // Check local property, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4227 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4228 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 4229 | if (result.IsFound()) return result.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4230 | |
| 4231 | if (continue_search) { |
| 4232 | // Continue searching via the prototype chain. |
| 4233 | Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4234 | if (!pt->IsNull()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4235 | return JSObject::cast(pt)-> |
| 4236 | GetPropertyAttributeWithReceiver(receiver, name); |
| 4237 | } |
| 4238 | } |
| 4239 | return ABSENT; |
| 4240 | } |
| 4241 | |
| 4242 | |
| 4243 | PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor( |
| 4244 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4245 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | bool continue_search) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4247 | // TODO(rossberg): Support symbols in the API. |
| 4248 | if (name->IsSymbol()) return ABSENT; |
| 4249 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4250 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4251 | HandleScope scope(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4253 | // Make sure that the top context does not change when doing |
| 4254 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4255 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4256 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4257 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 4258 | Handle<JSObject> receiver_handle(receiver); |
| 4259 | Handle<JSObject> holder_handle(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4260 | Handle<String> name_handle(String::cast(name)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 4261 | PropertyCallbackArguments args(isolate, interceptor->data(), receiver, this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4262 | if (!interceptor->query()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4263 | v8::NamedPropertyQueryCallback query = |
| 4264 | v8::ToCData<v8::NamedPropertyQueryCallback>(interceptor->query()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4265 | LOG(isolate, |
| 4266 | ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4267 | v8::Handle<v8::Integer> result = |
| 4268 | args.Call(query, v8::Utils::ToLocal(name_handle)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4269 | if (!result.IsEmpty()) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4270 | ASSERT(result->IsInt32()); |
| 4271 | return static_cast<PropertyAttributes>(result->Int32Value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } else if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4274 | v8::NamedPropertyGetterCallback getter = |
| 4275 | v8::ToCData<v8::NamedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4276 | LOG(isolate, |
| 4277 | ApiNamedPropertyAccess("interceptor-named-get-has", this, name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4278 | v8::Handle<v8::Value> result = |
| 4279 | args.Call(getter, v8::Utils::ToLocal(name_handle)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4280 | if (!result.IsEmpty()) return DONT_ENUM; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4281 | } |
| 4282 | return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle, |
| 4283 | *name_handle, |
| 4284 | continue_search); |
| 4285 | } |
| 4286 | |
| 4287 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4288 | PropertyAttributes JSReceiver::GetPropertyAttributeWithReceiver( |
| 4289 | JSReceiver* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4290 | Name* key) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4291 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4292 | if (IsJSObject() && key->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4293 | return JSObject::cast(this)->GetElementAttributeWithReceiver( |
| 4294 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4295 | } |
| 4296 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4297 | LookupResult lookup(GetIsolate()); |
| 4298 | Lookup(key, &lookup); |
| 4299 | return GetPropertyAttributeForResult(receiver, &lookup, key, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4303 | PropertyAttributes JSReceiver::GetPropertyAttributeForResult( |
| 4304 | JSReceiver* receiver, |
| 4305 | LookupResult* lookup, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4306 | Name* name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4307 | bool continue_search) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4308 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4309 | if (IsAccessCheckNeeded()) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4310 | JSObject* this_obj = JSObject::cast(this); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4311 | Heap* heap = GetHeap(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4312 | if (!heap->isolate()->MayNamedAccess(this_obj, name, v8::ACCESS_HAS)) { |
| 4313 | return this_obj->GetPropertyAttributeWithFailedAccessCheck( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4314 | receiver, lookup, name, continue_search); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4315 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4316 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4317 | if (lookup->IsFound()) { |
| 4318 | switch (lookup->type()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4319 | case NORMAL: // fall through |
| 4320 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4321 | case CONSTANT: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4322 | case CALLBACKS: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4323 | return lookup->GetAttributes(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4324 | case HANDLER: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4325 | return JSProxy::cast(lookup->proxy())->GetPropertyAttributeWithHandler( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4326 | receiver, name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4327 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4328 | case INTERCEPTOR: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4329 | return lookup->holder()->GetPropertyAttributeWithInterceptor( |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4330 | JSObject::cast(receiver), name, continue_search); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4331 | case TRANSITION: |
| 4332 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4333 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4334 | } |
| 4335 | } |
| 4336 | return ABSENT; |
| 4337 | } |
| 4338 | |
| 4339 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4340 | PropertyAttributes JSReceiver::GetLocalPropertyAttribute(Name* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4341 | // Check whether the name is an array index. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4342 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4343 | if (IsJSObject() && name->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4344 | return GetLocalElementAttribute(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4345 | } |
| 4346 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4347 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 4348 | LocalLookup(name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4349 | return GetPropertyAttributeForResult(this, &lookup, name, false); |
| 4350 | } |
| 4351 | |
| 4352 | |
| 4353 | PropertyAttributes JSObject::GetElementAttributeWithReceiver( |
| 4354 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4355 | Isolate* isolate = GetIsolate(); |
| 4356 | |
| 4357 | // Check access rights if needed. |
| 4358 | if (IsAccessCheckNeeded()) { |
| 4359 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 4360 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 4361 | return ABSENT; |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | if (IsJSGlobalProxy()) { |
| 4366 | Object* proto = GetPrototype(); |
| 4367 | if (proto->IsNull()) return ABSENT; |
| 4368 | ASSERT(proto->IsJSGlobalObject()); |
| 4369 | return JSObject::cast(proto)->GetElementAttributeWithReceiver( |
| 4370 | receiver, index, continue_search); |
| 4371 | } |
| 4372 | |
| 4373 | // Check for lookup interceptor except when bootstrapping. |
| 4374 | if (HasIndexedInterceptor() && !isolate->bootstrapper()->IsActive()) { |
| 4375 | return GetElementAttributeWithInterceptor(receiver, index, continue_search); |
| 4376 | } |
| 4377 | |
| 4378 | return GetElementAttributeWithoutInterceptor( |
| 4379 | receiver, index, continue_search); |
| 4380 | } |
| 4381 | |
| 4382 | |
| 4383 | PropertyAttributes JSObject::GetElementAttributeWithInterceptor( |
| 4384 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4385 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4386 | HandleScope scope(isolate); |
| 4387 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4388 | // Make sure that the top context does not change when doing |
| 4389 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4390 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4391 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4392 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 4393 | Handle<JSReceiver> hreceiver(receiver); |
| 4394 | Handle<JSObject> holder(this); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 4395 | PropertyCallbackArguments args(isolate, interceptor->data(), receiver, this); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4396 | if (!interceptor->query()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4397 | v8::IndexedPropertyQueryCallback query = |
| 4398 | v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4399 | LOG(isolate, |
| 4400 | ApiIndexedPropertyAccess("interceptor-indexed-has", this, index)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4401 | v8::Handle<v8::Integer> result = args.Call(query, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4402 | if (!result.IsEmpty()) |
| 4403 | return static_cast<PropertyAttributes>(result->Int32Value()); |
| 4404 | } else if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4405 | v8::IndexedPropertyGetterCallback getter = |
| 4406 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4407 | LOG(isolate, |
| 4408 | ApiIndexedPropertyAccess("interceptor-indexed-get-has", this, index)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4409 | v8::Handle<v8::Value> result = args.Call(getter, index); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 4410 | if (!result.IsEmpty()) return NONE; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | return holder->GetElementAttributeWithoutInterceptor( |
| 4414 | *hreceiver, index, continue_search); |
| 4415 | } |
| 4416 | |
| 4417 | |
| 4418 | PropertyAttributes JSObject::GetElementAttributeWithoutInterceptor( |
| 4419 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4420 | PropertyAttributes attr = GetElementsAccessor()->GetAttributes( |
| 4421 | receiver, this, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4422 | if (attr != ABSENT) return attr; |
| 4423 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4424 | // Handle [] on String objects. |
| 4425 | if (IsStringObjectWithCharacterAt(index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4426 | return static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 4427 | } |
| 4428 | |
| 4429 | if (!continue_search) return ABSENT; |
| 4430 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4431 | Object* pt = GetPrototype(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4432 | if (pt->IsJSProxy()) { |
| 4433 | // 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] | 4434 | return JSProxy::cast(pt)->GetElementAttributeWithHandler(receiver, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4435 | } |
| 4436 | if (pt->IsNull()) return ABSENT; |
| 4437 | return JSObject::cast(pt)->GetElementAttributeWithReceiver( |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4438 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4442 | Handle<Map> NormalizedMapCache::Get(Handle<NormalizedMapCache> cache, |
| 4443 | Handle<JSObject> obj, |
| 4444 | PropertyNormalizationMode mode) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4445 | Map* fast = obj->map(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4446 | int index = fast->Hash() % kEntries; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4447 | Object* result = cache->get(index); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4448 | if (result->IsMap() && |
| 4449 | Map::cast(result)->EquivalentToForNormalization(fast, mode)) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4450 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4451 | if (FLAG_verify_heap) { |
| 4452 | Map::cast(result)->SharedMapVerify(); |
| 4453 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4454 | #endif |
| 4455 | #ifdef DEBUG |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4456 | if (FLAG_enable_slow_asserts) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4457 | // The cached map should match newly created normalized map bit-by-bit, |
| 4458 | // except for the code cache, which can contain some ics which can be |
| 4459 | // applied to the shared map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4460 | Object* fresh; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4461 | MaybeObject* maybe_fresh = |
| 4462 | fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP); |
| 4463 | if (maybe_fresh->ToObject(&fresh)) { |
| 4464 | ASSERT(memcmp(Map::cast(fresh)->address(), |
| 4465 | Map::cast(result)->address(), |
| 4466 | Map::kCodeCacheOffset) == 0); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4467 | STATIC_ASSERT(Map::kDependentCodeOffset == |
| 4468 | Map::kCodeCacheOffset + kPointerSize); |
| 4469 | int offset = Map::kDependentCodeOffset + kPointerSize; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4470 | ASSERT(memcmp(Map::cast(fresh)->address() + offset, |
| 4471 | Map::cast(result)->address() + offset, |
| 4472 | Map::kSize - offset) == 0); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4473 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4474 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4475 | #endif |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4476 | return handle(Map::cast(result)); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4477 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4478 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4479 | Isolate* isolate = cache->GetIsolate(); |
| 4480 | Handle<Map> map = Map::CopyNormalized(handle(fast), mode, |
| 4481 | SHARED_NORMALIZED_MAP); |
| 4482 | ASSERT(map->is_dictionary_map()); |
| 4483 | cache->set(index, *map); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4484 | isolate->counters()->normalized_maps()->Increment(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4485 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4486 | return map; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4490 | void NormalizedMapCache::Clear() { |
| 4491 | int entries = length(); |
| 4492 | for (int i = 0; i != entries; i++) { |
| 4493 | set_undefined(i); |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 4498 | void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object, |
| 4499 | Handle<Name> name, |
| 4500 | Handle<Code> code) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4501 | Handle<Map> map(object->map()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4502 | Map::UpdateCodeCache(map, name, code); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
| 4505 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4506 | void JSObject::NormalizeProperties(Handle<JSObject> object, |
| 4507 | PropertyNormalizationMode mode, |
| 4508 | int expected_additional_properties) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4509 | if (!object->HasFastProperties()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4510 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4511 | // The global object is always normalized. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4512 | ASSERT(!object->IsGlobalObject()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4513 | // JSGlobalProxy must never be normalized |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4514 | ASSERT(!object->IsJSGlobalProxy()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4515 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4516 | Isolate* isolate = object->GetIsolate(); |
| 4517 | HandleScope scope(isolate); |
| 4518 | Handle<Map> map(object->map()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4519 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4520 | // Allocate new content. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4521 | int real_size = map->NumberOfOwnDescriptors(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4522 | int property_count = real_size; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4523 | if (expected_additional_properties > 0) { |
| 4524 | property_count += expected_additional_properties; |
| 4525 | } else { |
| 4526 | property_count += 2; // Make space for two more properties. |
| 4527 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4528 | Handle<NameDictionary> dictionary = |
| 4529 | isolate->factory()->NewNameDictionary(property_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4530 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4531 | Handle<DescriptorArray> descs(map->instance_descriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4532 | for (int i = 0; i < real_size; i++) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4533 | PropertyDetails details = descs->GetDetails(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4534 | switch (details.type()) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4535 | case CONSTANT: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4536 | Handle<Name> key(descs->GetKey(i)); |
| 4537 | Handle<Object> value(descs->GetConstant(i), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4538 | PropertyDetails d = PropertyDetails( |
| 4539 | details.attributes(), NORMAL, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4540 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4541 | break; |
| 4542 | } |
| 4543 | case FIELD: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4544 | Handle<Name> key(descs->GetKey(i)); |
| 4545 | Handle<Object> value( |
| 4546 | object->RawFastPropertyAt(descs->GetFieldIndex(i)), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4547 | PropertyDetails d = |
| 4548 | PropertyDetails(details.attributes(), NORMAL, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4549 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4550 | break; |
| 4551 | } |
| 4552 | case CALLBACKS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4553 | Handle<Name> key(descs->GetKey(i)); |
| 4554 | Handle<Object> value(descs->GetCallbacksObject(i), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4555 | PropertyDetails d = PropertyDetails( |
| 4556 | details.attributes(), CALLBACKS, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4557 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4558 | break; |
| 4559 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4560 | case INTERCEPTOR: |
| 4561 | break; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4562 | case HANDLER: |
| 4563 | case NORMAL: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4564 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4565 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4566 | UNREACHABLE(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4567 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | // Copy the next enumeration index from instance descriptor. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4572 | dictionary->SetNextEnumerationIndex(real_size + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4573 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4574 | Handle<NormalizedMapCache> cache( |
| 4575 | isolate->context()->native_context()->normalized_map_cache()); |
| 4576 | Handle<Map> new_map = NormalizedMapCache::Get(cache, object, mode); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 4577 | ASSERT(new_map->is_dictionary_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4578 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4579 | // From here on we cannot fail and we shouldn't GC anymore. |
| 4580 | DisallowHeapAllocation no_allocation; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4581 | |
| 4582 | // Resize the object in the heap if necessary. |
| 4583 | int new_instance_size = new_map->instance_size(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4584 | int instance_size_delta = map->instance_size() - new_instance_size; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4585 | ASSERT(instance_size_delta >= 0); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4586 | isolate->heap()->CreateFillerObjectAt(object->address() + new_instance_size, |
| 4587 | instance_size_delta); |
| 4588 | if (Marking::IsBlack(Marking::MarkBitFrom(*object))) { |
| 4589 | MemoryChunk::IncrementLiveBytesFromMutator(object->address(), |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4590 | -instance_size_delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4593 | object->set_map(*new_map); |
| 4594 | map->NotifyLeafMapLayoutChange(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4595 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4596 | object->set_properties(*dictionary); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4597 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4598 | isolate->counters()->props_to_dictionary()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4599 | |
| 4600 | #ifdef DEBUG |
| 4601 | if (FLAG_trace_normalization) { |
| 4602 | PrintF("Object properties have been normalized:\n"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4603 | object->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4604 | } |
| 4605 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4609 | void JSObject::TransformToFastProperties(Handle<JSObject> object, |
| 4610 | int unused_property_fields) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4611 | if (object->HasFastProperties()) return; |
| 4612 | ASSERT(!object->IsGlobalObject()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4613 | CALL_HEAP_FUNCTION_VOID( |
| 4614 | object->GetIsolate(), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4615 | object->property_dictionary()->TransformPropertiesToFastFor( |
| 4616 | *object, unused_property_fields)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4620 | static MUST_USE_RESULT MaybeObject* CopyFastElementsToDictionary( |
| 4621 | Isolate* isolate, |
| 4622 | FixedArrayBase* array, |
| 4623 | int length, |
| 4624 | SeededNumberDictionary* dictionary) { |
| 4625 | Heap* heap = isolate->heap(); |
| 4626 | bool has_double_elements = array->IsFixedDoubleArray(); |
| 4627 | for (int i = 0; i < length; i++) { |
| 4628 | Object* value = NULL; |
| 4629 | if (has_double_elements) { |
| 4630 | FixedDoubleArray* double_array = FixedDoubleArray::cast(array); |
| 4631 | if (double_array->is_the_hole(i)) { |
| 4632 | value = isolate->heap()->the_hole_value(); |
| 4633 | } else { |
| 4634 | // Objects must be allocated in the old object space, since the |
| 4635 | // overall number of HeapNumbers needed for the conversion might |
| 4636 | // exceed the capacity of new space, and we would fail repeatedly |
| 4637 | // trying to convert the FixedDoubleArray. |
| 4638 | MaybeObject* maybe_value_object = |
| 4639 | heap->AllocateHeapNumber(double_array->get_scalar(i), TENURED); |
| 4640 | if (!maybe_value_object->ToObject(&value)) return maybe_value_object; |
| 4641 | } |
| 4642 | } else { |
| 4643 | value = FixedArray::cast(array)->get(i); |
| 4644 | } |
| 4645 | if (!value->IsTheHole()) { |
| 4646 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
| 4647 | MaybeObject* maybe_result = |
| 4648 | dictionary->AddNumberEntry(i, value, details); |
| 4649 | if (!maybe_result->To(&dictionary)) return maybe_result; |
| 4650 | } |
| 4651 | } |
| 4652 | return dictionary; |
| 4653 | } |
| 4654 | |
| 4655 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4656 | static Handle<SeededNumberDictionary> CopyFastElementsToDictionary( |
| 4657 | Handle<FixedArrayBase> array, |
| 4658 | int length, |
| 4659 | Handle<SeededNumberDictionary> dict) { |
| 4660 | Isolate* isolate = array->GetIsolate(); |
| 4661 | CALL_HEAP_FUNCTION(isolate, |
| 4662 | CopyFastElementsToDictionary( |
| 4663 | isolate, *array, length, *dict), |
| 4664 | SeededNumberDictionary); |
| 4665 | } |
| 4666 | |
| 4667 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4668 | Handle<SeededNumberDictionary> JSObject::NormalizeElements( |
| 4669 | Handle<JSObject> object) { |
| 4670 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 4671 | object->NormalizeElements(), |
| 4672 | SeededNumberDictionary); |
| 4673 | } |
| 4674 | |
| 4675 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4676 | MaybeObject* JSObject::NormalizeElements() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4677 | ASSERT(!HasExternalArrayElements()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 4678 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4679 | // Find the backing store. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4680 | FixedArrayBase* array = FixedArrayBase::cast(elements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4681 | Map* old_map = array->map(); |
| 4682 | bool is_arguments = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4683 | (old_map == old_map->GetHeap()->non_strict_arguments_elements_map()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4684 | if (is_arguments) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4685 | array = FixedArrayBase::cast(FixedArray::cast(array)->get(1)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4686 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4687 | if (array->IsDictionary()) return array; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4688 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4689 | ASSERT(HasFastSmiOrObjectElements() || |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4690 | HasFastDoubleElements() || |
| 4691 | HasFastArgumentsElements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4692 | // Compute the effective length and allocate a new backing store. |
| 4693 | int length = IsJSArray() |
| 4694 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 4695 | : array->length(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 4696 | int old_capacity = 0; |
| 4697 | int used_elements = 0; |
| 4698 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4699 | SeededNumberDictionary* dictionary; |
| 4700 | MaybeObject* maybe_dictionary = |
| 4701 | SeededNumberDictionary::Allocate(GetHeap(), used_elements); |
| 4702 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4703 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4704 | maybe_dictionary = CopyFastElementsToDictionary( |
| 4705 | GetIsolate(), array, length, dictionary); |
| 4706 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4707 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4708 | // Switch to using the dictionary as the backing storage for elements. |
| 4709 | if (is_arguments) { |
| 4710 | FixedArray::cast(elements())->set(1, dictionary); |
| 4711 | } else { |
| 4712 | // Set the new map first to satify the elements type assert in |
| 4713 | // set_elements(). |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4714 | Map* new_map; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4715 | MaybeObject* maybe = GetElementsTransitionMap(GetIsolate(), |
| 4716 | DICTIONARY_ELEMENTS); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4717 | if (!maybe->To(&new_map)) return maybe; |
| 4718 | set_map(new_map); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4719 | set_elements(dictionary); |
| 4720 | } |
| 4721 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4722 | old_map->GetHeap()->isolate()->counters()->elements_to_dictionary()-> |
| 4723 | Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4724 | |
| 4725 | #ifdef DEBUG |
| 4726 | if (FLAG_trace_normalization) { |
| 4727 | PrintF("Object elements have been normalized:\n"); |
| 4728 | Print(); |
| 4729 | } |
| 4730 | #endif |
| 4731 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4732 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
| 4733 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4737 | Smi* JSReceiver::GenerateIdentityHash() { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4738 | Isolate* isolate = GetIsolate(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4739 | |
| 4740 | int hash_value; |
| 4741 | int attempts = 0; |
| 4742 | do { |
| 4743 | // Generate a random 32-bit hash value but limit range to fit |
| 4744 | // within a smi. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4745 | hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4746 | attempts++; |
| 4747 | } while (hash_value == 0 && attempts < 30); |
| 4748 | hash_value = hash_value != 0 ? hash_value : 1; // never return 0 |
| 4749 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4750 | return Smi::FromInt(hash_value); |
| 4751 | } |
| 4752 | |
| 4753 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4754 | void JSObject::SetIdentityHash(Handle<JSObject> object, Smi* hash) { |
| 4755 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 4756 | object->SetHiddenProperty( |
| 4757 | object->GetHeap()->identity_hash_string(), hash)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4761 | int JSObject::GetIdentityHash(Handle<JSObject> object) { |
| 4762 | CALL_AND_RETRY_OR_DIE(object->GetIsolate(), |
| 4763 | object->GetIdentityHash(ALLOW_CREATION), |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 4764 | return Smi::cast(__object__)->value(), |
| 4765 | return 0); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4769 | MaybeObject* JSObject::GetIdentityHash(CreationFlag flag) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4770 | Object* stored_value = GetHiddenProperty(GetHeap()->identity_hash_string()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4771 | if (stored_value->IsSmi()) return stored_value; |
| 4772 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4773 | // Do not generate permanent identity hash code if not requested. |
| 4774 | if (flag == OMIT_CREATION) return GetHeap()->undefined_value(); |
| 4775 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4776 | Smi* hash = GenerateIdentityHash(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4777 | MaybeObject* result = SetHiddenProperty(GetHeap()->identity_hash_string(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4778 | hash); |
| 4779 | if (result->IsFailure()) return result; |
| 4780 | if (result->ToObjectUnchecked()->IsUndefined()) { |
| 4781 | // Trying to get hash of detached proxy. |
| 4782 | return Smi::FromInt(0); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4783 | } |
| 4784 | return hash; |
| 4785 | } |
| 4786 | |
| 4787 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4788 | Handle<Object> JSProxy::GetIdentityHash(Handle<JSProxy> proxy, |
| 4789 | CreationFlag flag) { |
| 4790 | CALL_HEAP_FUNCTION(proxy->GetIsolate(), proxy->GetIdentityHash(flag), Object); |
| 4791 | } |
| 4792 | |
| 4793 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4794 | MaybeObject* JSProxy::GetIdentityHash(CreationFlag flag) { |
| 4795 | Object* hash = this->hash(); |
| 4796 | if (!hash->IsSmi() && flag == ALLOW_CREATION) { |
| 4797 | hash = GenerateIdentityHash(); |
| 4798 | set_hash(hash); |
| 4799 | } |
| 4800 | return hash; |
| 4801 | } |
| 4802 | |
| 4803 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4804 | Object* JSObject::GetHiddenProperty(Name* key) { |
| 4805 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4806 | if (IsJSGlobalProxy()) { |
| 4807 | // For a proxy, use the prototype as target object. |
| 4808 | Object* proxy_parent = GetPrototype(); |
| 4809 | // If the proxy is detached, return undefined. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4810 | if (proxy_parent->IsNull()) return GetHeap()->the_hole_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4811 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4812 | return JSObject::cast(proxy_parent)->GetHiddenProperty(key); |
| 4813 | } |
| 4814 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4815 | MaybeObject* hidden_lookup = |
| 4816 | GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4817 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
| 4818 | |
| 4819 | if (inline_value->IsSmi()) { |
| 4820 | // Handle inline-stored identity hash. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4821 | if (key == GetHeap()->identity_hash_string()) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4822 | return inline_value; |
| 4823 | } else { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4824 | return GetHeap()->the_hole_value(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4825 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4826 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4827 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4828 | if (inline_value->IsUndefined()) return GetHeap()->the_hole_value(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4829 | |
| 4830 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
| 4831 | Object* entry = hashtable->Lookup(key); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4832 | return entry; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4836 | Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> obj, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4837 | Handle<Name> key, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4838 | Handle<Object> value) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4839 | CALL_HEAP_FUNCTION(obj->GetIsolate(), |
| 4840 | obj->SetHiddenProperty(*key, *value), |
| 4841 | Object); |
| 4842 | } |
| 4843 | |
| 4844 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4845 | MaybeObject* JSObject::SetHiddenProperty(Name* key, Object* value) { |
| 4846 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4847 | if (IsJSGlobalProxy()) { |
| 4848 | // For a proxy, use the prototype as target object. |
| 4849 | Object* proxy_parent = GetPrototype(); |
| 4850 | // If the proxy is detached, return undefined. |
| 4851 | if (proxy_parent->IsNull()) return GetHeap()->undefined_value(); |
| 4852 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4853 | return JSObject::cast(proxy_parent)->SetHiddenProperty(key, value); |
| 4854 | } |
| 4855 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4856 | MaybeObject* hidden_lookup = |
| 4857 | GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4858 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
| 4859 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4860 | // 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] | 4861 | if (value->IsSmi() && |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4862 | key == GetHeap()->identity_hash_string() && |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4863 | (inline_value->IsUndefined() || inline_value->IsSmi())) { |
| 4864 | return SetHiddenPropertiesHashTable(value); |
| 4865 | } |
| 4866 | |
| 4867 | hidden_lookup = GetHiddenPropertiesHashTable(CREATE_NEW_IF_ABSENT); |
| 4868 | ObjectHashTable* hashtable; |
| 4869 | if (!hidden_lookup->To(&hashtable)) return hidden_lookup; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4870 | |
| 4871 | // 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] | 4872 | MaybeObject* insert_result = hashtable->Put(key, value); |
| 4873 | ObjectHashTable* new_table; |
| 4874 | if (!insert_result->To(&new_table)) return insert_result; |
| 4875 | if (new_table != hashtable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4876 | // If adding the key expanded the dictionary (i.e., Add returned a new |
| 4877 | // dictionary), store it back to the object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4878 | MaybeObject* store_result = SetHiddenPropertiesHashTable(new_table); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4879 | if (store_result->IsFailure()) return store_result; |
| 4880 | } |
| 4881 | // Return this to mark success. |
| 4882 | return this; |
| 4883 | } |
| 4884 | |
| 4885 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4886 | void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) { |
| 4887 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4888 | ASSERT(key->IsUniqueName()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4889 | |
| 4890 | if (object->IsJSGlobalProxy()) { |
| 4891 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 4892 | if (proto->IsNull()) return; |
| 4893 | ASSERT(proto->IsJSGlobalObject()); |
| 4894 | return DeleteHiddenProperty(Handle<JSObject>::cast(proto), key); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4895 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4896 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4897 | MaybeObject* hidden_lookup = |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4898 | object->GetHiddenPropertiesHashTable(ONLY_RETURN_INLINE_VALUE); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4899 | Object* inline_value = hidden_lookup->ToObjectUnchecked(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4900 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4901 | // We never delete (inline-stored) identity hashes. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4902 | ASSERT(*key != isolate->heap()->identity_hash_string()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4903 | if (inline_value->IsUndefined() || inline_value->IsSmi()) return; |
| 4904 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4905 | Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value)); |
| 4906 | PutIntoObjectHashTable(hashtable, key, isolate->factory()->the_hole_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | |
| 4910 | bool JSObject::HasHiddenProperties() { |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4911 | return GetPropertyAttributePostInterceptor(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4912 | GetHeap()->hidden_string(), |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4913 | false) != ABSENT; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4917 | MaybeObject* JSObject::GetHiddenPropertiesHashTable( |
| 4918 | InitializeHiddenProperties init_option) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4919 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4920 | Object* inline_value; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4921 | if (HasFastProperties()) { |
| 4922 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4923 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4924 | // 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] | 4925 | // code zero) it will always occupy the first entry if present. |
| 4926 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4927 | if (descriptors->number_of_descriptors() > 0) { |
| 4928 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4929 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4930 | sorted_index < map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4931 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4932 | MaybeObject* maybe_value = this->FastPropertyAt( |
| 4933 | descriptors->GetDetails(sorted_index).representation(), |
| 4934 | descriptors->GetFieldIndex(sorted_index)); |
| 4935 | if (!maybe_value->To(&inline_value)) return maybe_value; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4936 | } else { |
| 4937 | inline_value = GetHeap()->undefined_value(); |
| 4938 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4939 | } else { |
| 4940 | inline_value = GetHeap()->undefined_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4941 | } |
| 4942 | } else { |
| 4943 | PropertyAttributes attributes; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4944 | // 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] | 4945 | // so we can be sure that GetLocalPropertyPostInterceptor returns a real |
| 4946 | // object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4947 | inline_value = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4948 | GetLocalPropertyPostInterceptor(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4949 | GetHeap()->hidden_string(), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4950 | &attributes)->ToObjectUnchecked(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4951 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4952 | |
| 4953 | if (init_option == ONLY_RETURN_INLINE_VALUE || |
| 4954 | inline_value->IsHashTable()) { |
| 4955 | return inline_value; |
| 4956 | } |
| 4957 | |
| 4958 | ObjectHashTable* hashtable; |
| 4959 | static const int kInitialCapacity = 4; |
| 4960 | MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 4961 | ObjectHashTable::Allocate(GetHeap(), |
| 4962 | kInitialCapacity, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4963 | ObjectHashTable::USE_CUSTOM_MINIMUM_CAPACITY); |
| 4964 | if (!maybe_obj->To<ObjectHashTable>(&hashtable)) return maybe_obj; |
| 4965 | |
| 4966 | if (inline_value->IsSmi()) { |
| 4967 | // We were storing the identity hash inline and now allocated an actual |
| 4968 | // dictionary. Put the identity hash into the new dictionary. |
| 4969 | MaybeObject* insert_result = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4970 | hashtable->Put(GetHeap()->identity_hash_string(), inline_value); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4971 | ObjectHashTable* new_table; |
| 4972 | if (!insert_result->To(&new_table)) return insert_result; |
| 4973 | // We expect no resizing for the first insert. |
| 4974 | ASSERT_EQ(hashtable, new_table); |
| 4975 | } |
| 4976 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4977 | MaybeObject* store_result = SetLocalPropertyIgnoreAttributesTrampoline( |
| 4978 | GetHeap()->hidden_string(), |
| 4979 | hashtable, |
| 4980 | DONT_ENUM, |
| 4981 | OPTIMAL_REPRESENTATION, |
| 4982 | ALLOW_AS_CONSTANT, |
| 4983 | OMIT_EXTENSIBILITY_CHECK); |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 4984 | if (store_result->IsFailure()) return store_result; |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4985 | return hashtable; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4989 | MaybeObject* JSObject::SetHiddenPropertiesHashTable(Object* value) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4990 | ASSERT(!IsJSGlobalProxy()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4991 | // We can store the identity hash inline iff there is no backing store |
| 4992 | // for hidden properties yet. |
| 4993 | ASSERT(HasHiddenProperties() != value->IsSmi()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4994 | if (HasFastProperties()) { |
| 4995 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4996 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4997 | // 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] | 4998 | // code zero) it will always occupy the first entry if present. |
| 4999 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5000 | if (descriptors->number_of_descriptors() > 0) { |
| 5001 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 5002 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5003 | sorted_index < map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5004 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5005 | FastPropertyAtPut(descriptors->GetFieldIndex(sorted_index), value); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5006 | return this; |
| 5007 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5008 | } |
| 5009 | } |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 5010 | MaybeObject* store_result = SetLocalPropertyIgnoreAttributesTrampoline( |
| 5011 | GetHeap()->hidden_string(), |
| 5012 | value, |
| 5013 | DONT_ENUM, |
| 5014 | OPTIMAL_REPRESENTATION, |
| 5015 | ALLOW_AS_CONSTANT, |
| 5016 | OMIT_EXTENSIBILITY_CHECK); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5017 | if (store_result->IsFailure()) return store_result; |
| 5018 | return this; |
| 5019 | } |
| 5020 | |
| 5021 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5022 | Handle<Object> JSObject::DeletePropertyPostInterceptor(Handle<JSObject> object, |
| 5023 | Handle<Name> name, |
| 5024 | DeleteMode mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5025 | // Check local property, ignore interceptor. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5026 | Isolate* isolate = object->GetIsolate(); |
| 5027 | LookupResult result(isolate); |
| 5028 | object->LocalLookupRealNamedProperty(*name, &result); |
| 5029 | if (!result.IsFound()) return isolate->factory()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5030 | |
| 5031 | // Normalize object if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5032 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5033 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5034 | return DeleteNormalizedProperty(object, name, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5038 | Handle<Object> JSObject::DeletePropertyWithInterceptor(Handle<JSObject> object, |
| 5039 | Handle<Name> name) { |
| 5040 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5041 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5042 | // TODO(rossberg): Support symbols in the API. |
| 5043 | if (name->IsSymbol()) return isolate->factory()->false_value(); |
| 5044 | |
| 5045 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5046 | if (!interceptor->deleter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 5047 | v8::NamedPropertyDeleterCallback deleter = |
| 5048 | v8::ToCData<v8::NamedPropertyDeleterCallback>(interceptor->deleter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5049 | LOG(isolate, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5050 | ApiNamedPropertyAccess("interceptor-named-delete", *object, *name)); |
| 5051 | PropertyCallbackArguments args( |
| 5052 | isolate, interceptor->data(), *object, *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5053 | v8::Handle<v8::Boolean> result = |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5054 | args.Call(deleter, v8::Utils::ToLocal(Handle<String>::cast(name))); |
| 5055 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5056 | if (!result.IsEmpty()) { |
| 5057 | ASSERT(result->IsBoolean()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 5058 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 5059 | result_internal->VerifyApiCallResultType(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5060 | // Rebox CustomArguments::kReturnValueOffset before returning. |
| 5061 | return handle(*result_internal, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5062 | } |
| 5063 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5064 | Handle<Object> result = |
| 5065 | DeletePropertyPostInterceptor(object, name, NORMAL_DELETION); |
| 5066 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5067 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5071 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 5072 | static Handle<Object> AccessorDelete(Handle<JSObject> object, |
| 5073 | uint32_t index, |
| 5074 | JSObject::DeleteMode mode) { |
| 5075 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 5076 | object->GetElementsAccessor()->Delete(*object, |
| 5077 | index, |
| 5078 | mode), |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5079 | Object); |
| 5080 | } |
| 5081 | |
| 5082 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5083 | Handle<Object> JSObject::DeleteElementWithInterceptor(Handle<JSObject> object, |
| 5084 | uint32_t index) { |
| 5085 | Isolate* isolate = object->GetIsolate(); |
| 5086 | Factory* factory = isolate->factory(); |
| 5087 | |
| 5088 | // Make sure that the top context does not change when doing |
| 5089 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 5090 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5091 | |
| 5092 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
| 5093 | if (interceptor->deleter()->IsUndefined()) return factory->false_value(); |
| 5094 | v8::IndexedPropertyDeleterCallback deleter = |
| 5095 | v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter()); |
| 5096 | LOG(isolate, |
| 5097 | ApiIndexedPropertyAccess("interceptor-indexed-delete", *object, index)); |
| 5098 | PropertyCallbackArguments args( |
| 5099 | isolate, interceptor->data(), *object, *object); |
| 5100 | v8::Handle<v8::Boolean> result = args.Call(deleter, index); |
| 5101 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5102 | if (!result.IsEmpty()) { |
| 5103 | ASSERT(result->IsBoolean()); |
| 5104 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 5105 | result_internal->VerifyApiCallResultType(); |
| 5106 | // Rebox CustomArguments::kReturnValueOffset before returning. |
| 5107 | return handle(*result_internal, isolate); |
| 5108 | } |
| 5109 | Handle<Object> delete_result = AccessorDelete(object, index, NORMAL_DELETION); |
| 5110 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5111 | return delete_result; |
| 5112 | } |
| 5113 | |
| 5114 | |
| 5115 | Handle<Object> JSObject::DeleteElement(Handle<JSObject> object, |
| 5116 | uint32_t index, |
| 5117 | DeleteMode mode) { |
| 5118 | Isolate* isolate = object->GetIsolate(); |
| 5119 | Factory* factory = isolate->factory(); |
| 5120 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5121 | // Check access rights if needed. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5122 | if (object->IsAccessCheckNeeded() && |
| 5123 | !isolate->MayIndexedAccess(*object, index, v8::ACCESS_DELETE)) { |
| 5124 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_DELETE); |
| 5125 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5126 | return factory->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5129 | if (object->IsStringObjectWithCharacterAt(index)) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5130 | if (mode == STRICT_DELETION) { |
| 5131 | // Deleting a non-configurable property in strict mode. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5132 | Handle<Object> name = factory->NewNumberFromUint(index); |
| 5133 | Handle<Object> args[2] = { name, object }; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5134 | Handle<Object> error = |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5135 | factory->NewTypeError("strict_delete_property", |
| 5136 | HandleVector(args, 2)); |
| 5137 | isolate->Throw(*error); |
| 5138 | return Handle<Object>(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5139 | } |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5140 | return factory->false_value(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5143 | if (object->IsJSGlobalProxy()) { |
| 5144 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5145 | if (proto->IsNull()) return factory->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5146 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5147 | return DeleteElement(Handle<JSObject>::cast(proto), index, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5150 | Handle<Object> old_value; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5151 | bool should_enqueue_change_record = false; |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5152 | if (FLAG_harmony_observation && object->map()->is_observed()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5153 | should_enqueue_change_record = HasLocalElement(object, index); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5154 | if (should_enqueue_change_record) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5155 | old_value = object->GetLocalElementAccessorPair(index) != NULL |
| 5156 | ? Handle<Object>::cast(factory->the_hole_value()) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 5157 | : Object::GetElement(isolate, object, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5158 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5161 | // Skip interceptor if forcing deletion. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5162 | Handle<Object> result; |
| 5163 | if (object->HasIndexedInterceptor() && mode != FORCE_DELETION) { |
| 5164 | result = DeleteElementWithInterceptor(object, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5165 | } else { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5166 | result = AccessorDelete(object, index, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5169 | if (should_enqueue_change_record && !HasLocalElement(object, index)) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5170 | Handle<String> name = factory->Uint32ToString(index); |
| 5171 | EnqueueChangeRecord(object, "deleted", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5174 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5178 | Handle<Object> JSObject::DeleteProperty(Handle<JSObject> object, |
| 5179 | Handle<Name> name, |
| 5180 | DeleteMode mode) { |
| 5181 | Isolate* isolate = object->GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5182 | // ECMA-262, 3rd, 8.6.2.5 |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5183 | ASSERT(name->IsName()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5184 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5185 | // Check access rights if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5186 | if (object->IsAccessCheckNeeded() && |
| 5187 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_DELETE)) { |
| 5188 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_DELETE); |
| 5189 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5190 | return isolate->factory()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5193 | if (object->IsJSGlobalProxy()) { |
| 5194 | Object* proto = object->GetPrototype(); |
| 5195 | if (proto->IsNull()) return isolate->factory()->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5196 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5197 | return JSGlobalObject::DeleteProperty( |
| 5198 | handle(JSGlobalObject::cast(proto)), name, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5199 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5200 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5201 | uint32_t index = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5202 | if (name->AsArrayIndex(&index)) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5203 | return DeleteElement(object, index, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | LookupResult lookup(isolate); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5207 | object->LocalLookup(*name, &lookup, true); |
| 5208 | if (!lookup.IsFound()) return isolate->factory()->true_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5209 | // Ignore attributes if forcing a deletion. |
| 5210 | if (lookup.IsDontDelete() && mode != FORCE_DELETION) { |
| 5211 | if (mode == STRICT_DELETION) { |
| 5212 | // Deleting a non-configurable property in strict mode. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5213 | Handle<Object> args[2] = { name, object }; |
| 5214 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 5215 | "strict_delete_property", HandleVector(args, ARRAY_SIZE(args))); |
| 5216 | isolate->Throw(*error); |
| 5217 | return Handle<Object>(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5218 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5219 | return isolate->factory()->false_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5222 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5223 | bool is_observed = FLAG_harmony_observation && object->map()->is_observed(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 5224 | if (is_observed && lookup.IsDataProperty()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5225 | old_value = Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5226 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5227 | Handle<Object> result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5228 | |
| 5229 | // Check for interceptor. |
| 5230 | if (lookup.IsInterceptor()) { |
| 5231 | // Skip interceptor if forcing a deletion. |
| 5232 | if (mode == FORCE_DELETION) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5233 | result = DeletePropertyPostInterceptor(object, name, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5234 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5235 | result = DeletePropertyWithInterceptor(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5236 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5237 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5238 | // Normalize object if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5239 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5240 | // Make sure the properties are normalized before removing the entry. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5241 | result = DeleteNormalizedProperty(object, name, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5242 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5243 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5244 | if (is_observed && !HasLocalProperty(object, name)) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5245 | EnqueueChangeRecord(object, "deleted", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5248 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5252 | Handle<Object> JSReceiver::DeleteElement(Handle<JSReceiver> object, |
| 5253 | uint32_t index, |
| 5254 | DeleteMode mode) { |
| 5255 | if (object->IsJSProxy()) { |
| 5256 | return JSProxy::DeleteElementWithHandler( |
| 5257 | Handle<JSProxy>::cast(object), index, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5258 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5259 | return JSObject::DeleteElement(Handle<JSObject>::cast(object), index, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5263 | Handle<Object> JSReceiver::DeleteProperty(Handle<JSReceiver> object, |
| 5264 | Handle<Name> name, |
| 5265 | DeleteMode mode) { |
| 5266 | if (object->IsJSProxy()) { |
| 5267 | return JSProxy::DeletePropertyWithHandler( |
| 5268 | Handle<JSProxy>::cast(object), name, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5269 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5270 | return JSObject::DeleteProperty(Handle<JSObject>::cast(object), name, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5274 | bool JSObject::ReferencesObjectFromElements(FixedArray* elements, |
| 5275 | ElementsKind kind, |
| 5276 | Object* object) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5277 | ASSERT(IsFastObjectElementsKind(kind) || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5278 | kind == DICTIONARY_ELEMENTS); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5279 | if (IsFastObjectElementsKind(kind)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5280 | int length = IsJSArray() |
| 5281 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 5282 | : elements->length(); |
| 5283 | for (int i = 0; i < length; ++i) { |
| 5284 | Object* element = elements->get(i); |
| 5285 | if (!element->IsTheHole() && element == object) return true; |
| 5286 | } |
| 5287 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5288 | Object* key = |
| 5289 | SeededNumberDictionary::cast(elements)->SlowReverseLookup(object); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5290 | if (!key->IsUndefined()) return true; |
| 5291 | } |
| 5292 | return false; |
| 5293 | } |
| 5294 | |
| 5295 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5296 | // Check whether this object references another object. |
| 5297 | bool JSObject::ReferencesObject(Object* obj) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5298 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5299 | Heap* heap = GetHeap(); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 5300 | DisallowHeapAllocation no_allocation; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5301 | |
| 5302 | // Is the object the constructor for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5303 | if (map_of_this->constructor() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5304 | return true; |
| 5305 | } |
| 5306 | |
| 5307 | // Is the object the prototype for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5308 | if (map_of_this->prototype() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5309 | return true; |
| 5310 | } |
| 5311 | |
| 5312 | // Check if the object is among the named properties. |
| 5313 | Object* key = SlowReverseLookup(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5314 | if (!key->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5315 | return true; |
| 5316 | } |
| 5317 | |
| 5318 | // Check if the object is among the indexed properties. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5319 | ElementsKind kind = GetElementsKind(); |
| 5320 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5321 | case EXTERNAL_PIXEL_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5322 | case EXTERNAL_BYTE_ELEMENTS: |
| 5323 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5324 | case EXTERNAL_SHORT_ELEMENTS: |
| 5325 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5326 | case EXTERNAL_INT_ELEMENTS: |
| 5327 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5328 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 5329 | case EXTERNAL_DOUBLE_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5330 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5331 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5332 | // Raw pixels and external arrays do not reference other |
| 5333 | // objects. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5334 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5335 | case FAST_SMI_ELEMENTS: |
| 5336 | case FAST_HOLEY_SMI_ELEMENTS: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5337 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5338 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5339 | case FAST_HOLEY_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5340 | case DICTIONARY_ELEMENTS: { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5341 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 5342 | if (ReferencesObjectFromElements(elements, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5343 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5344 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5345 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 5346 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 5347 | // Check the mapped parameters. |
| 5348 | int length = parameter_map->length(); |
| 5349 | for (int i = 2; i < length; ++i) { |
| 5350 | Object* value = parameter_map->get(i); |
| 5351 | if (!value->IsTheHole() && value == obj) return true; |
| 5352 | } |
| 5353 | // Check the arguments. |
| 5354 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5355 | kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : |
| 5356 | FAST_HOLEY_ELEMENTS; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5357 | if (ReferencesObjectFromElements(arguments, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5358 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5362 | // For functions check the context. |
| 5363 | if (IsJSFunction()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5364 | // Get the constructor function for arguments array. |
| 5365 | JSObject* arguments_boilerplate = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5366 | heap->isolate()->context()->native_context()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5367 | arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5368 | JSFunction* arguments_function = |
| 5369 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 5370 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5371 | // 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] | 5372 | JSFunction* f = JSFunction::cast(this); |
| 5373 | Context* context = f->context(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5374 | if (context->IsNativeContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5375 | return false; |
| 5376 | } |
| 5377 | |
| 5378 | // Check the non-special context slots. |
| 5379 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 5380 | // Only check JS objects. |
| 5381 | if (context->get(i)->IsJSObject()) { |
| 5382 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 5383 | // If it is an arguments array check the content. |
| 5384 | if (ctxobj->map()->constructor() == arguments_function) { |
| 5385 | if (ctxobj->ReferencesObject(obj)) { |
| 5386 | return true; |
| 5387 | } |
| 5388 | } else if (ctxobj == obj) { |
| 5389 | return true; |
| 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5394 | // Check the context extension (if any) if it can have references. |
| 5395 | if (context->has_extension() && !context->IsCatchContext()) { |
| 5396 | return JSObject::cast(context->extension())->ReferencesObject(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | // No references to object. |
| 5401 | return false; |
| 5402 | } |
| 5403 | |
| 5404 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5405 | Handle<Object> JSObject::PreventExtensions(Handle<JSObject> object) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5406 | Isolate* isolate = object->GetIsolate(); |
| 5407 | if (object->IsAccessCheckNeeded() && |
| 5408 | !isolate->MayNamedAccess(*object, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5409 | isolate->heap()->undefined_value(), |
| 5410 | v8::ACCESS_KEYS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5411 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5412 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5413 | return isolate->factory()->false_value(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5416 | if (object->IsJSGlobalProxy()) { |
| 5417 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5418 | if (proto->IsNull()) return object; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 5419 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5420 | return PreventExtensions(Handle<JSObject>::cast(proto)); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5423 | // It's not possible to seal objects with external array elements |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5424 | if (object->HasExternalArrayElements()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5425 | Handle<Object> error = |
| 5426 | isolate->factory()->NewTypeError( |
| 5427 | "cant_prevent_ext_external_array_elements", |
| 5428 | HandleVector(&object, 1)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5429 | isolate->Throw(*error); |
| 5430 | return Handle<Object>(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5433 | // If there are fast elements we normalize. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5434 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 5435 | ASSERT(object->HasDictionaryElements() || |
| 5436 | object->HasDictionaryArgumentsElements()); |
| 5437 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5438 | // Make sure that we never go back to fast case. |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5439 | dictionary->set_requires_slow_elements(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Do a map transition, other objects with this map may still |
| 5442 | // be extensible. |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5443 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5444 | Handle<Map> new_map = Map::Copy(handle(object->map())); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5445 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5446 | new_map->set_is_extensible(false); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5447 | object->set_map(*new_map); |
| 5448 | ASSERT(!object->map()->is_extensible()); |
| 5449 | return object; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5453 | template<typename Dictionary> |
| 5454 | static void FreezeDictionary(Dictionary* dictionary) { |
| 5455 | int capacity = dictionary->Capacity(); |
| 5456 | for (int i = 0; i < capacity; i++) { |
| 5457 | Object* k = dictionary->KeyAt(i); |
| 5458 | if (dictionary->IsKey(k)) { |
| 5459 | PropertyDetails details = dictionary->DetailsAt(i); |
| 5460 | int attrs = DONT_DELETE; |
| 5461 | // READ_ONLY is an invalid attribute for JS setters/getters. |
| 5462 | if (details.type() != CALLBACKS || |
| 5463 | !dictionary->ValueAt(i)->IsAccessorPair()) { |
| 5464 | attrs |= READ_ONLY; |
| 5465 | } |
| 5466 | details = details.CopyAddAttributes( |
| 5467 | static_cast<PropertyAttributes>(attrs)); |
| 5468 | dictionary->DetailsAtPut(i, details); |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5474 | Handle<Object> JSObject::Freeze(Handle<JSObject> object) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5475 | // Freezing non-strict arguments should be handled elsewhere. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5476 | ASSERT(!object->HasNonStrictArgumentsElements()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5477 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5478 | if (object->map()->is_frozen()) return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5479 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5480 | Isolate* isolate = object->GetIsolate(); |
| 5481 | if (object->IsAccessCheckNeeded() && |
| 5482 | !isolate->MayNamedAccess(*object, |
| 5483 | isolate->heap()->undefined_value(), |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5484 | v8::ACCESS_KEYS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5485 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5486 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5487 | return isolate->factory()->false_value(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5490 | if (object->IsJSGlobalProxy()) { |
| 5491 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5492 | if (proto->IsNull()) return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5493 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5494 | return Freeze(Handle<JSObject>::cast(proto)); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | // It's not possible to freeze objects with external array elements |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5498 | if (object->HasExternalArrayElements()) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5499 | Handle<Object> error = |
| 5500 | isolate->factory()->NewTypeError( |
| 5501 | "cant_prevent_ext_external_array_elements", |
| 5502 | HandleVector(&object, 1)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5503 | isolate->Throw(*error); |
| 5504 | return Handle<Object>(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5507 | Handle<SeededNumberDictionary> new_element_dictionary; |
| 5508 | if (!object->elements()->IsDictionary()) { |
| 5509 | int length = object->IsJSArray() |
| 5510 | ? Smi::cast(Handle<JSArray>::cast(object)->length())->value() |
| 5511 | : object->elements()->length(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5512 | if (length > 0) { |
| 5513 | int capacity = 0; |
| 5514 | int used = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5515 | object->GetElementsCapacityAndUsage(&capacity, &used); |
| 5516 | new_element_dictionary = |
| 5517 | isolate->factory()->NewSeededNumberDictionary(used); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5518 | |
| 5519 | // Move elements to a dictionary; avoid calling NormalizeElements to avoid |
| 5520 | // unnecessary transitions. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5521 | new_element_dictionary = CopyFastElementsToDictionary( |
| 5522 | handle(object->elements()), length, new_element_dictionary); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5523 | } else { |
| 5524 | // No existing elements, use a pre-allocated empty backing store |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5525 | new_element_dictionary = |
| 5526 | isolate->factory()->empty_slow_element_dictionary(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | LookupResult result(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5531 | Handle<Map> old_map(object->map()); |
| 5532 | old_map->LookupTransition(*object, isolate->heap()->frozen_symbol(), &result); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5533 | if (result.IsTransition()) { |
| 5534 | Map* transition_map = result.GetTransitionTarget(); |
| 5535 | ASSERT(transition_map->has_dictionary_elements()); |
| 5536 | ASSERT(transition_map->is_frozen()); |
| 5537 | ASSERT(!transition_map->is_extensible()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5538 | object->set_map(transition_map); |
| 5539 | } else if (object->HasFastProperties() && old_map->CanHaveMoreTransitions()) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5540 | // Create a new descriptor array with fully-frozen properties |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5541 | int num_descriptors = old_map->NumberOfOwnDescriptors(); |
| 5542 | Handle<DescriptorArray> new_descriptors = |
| 5543 | DescriptorArray::CopyUpToAddAttributes( |
| 5544 | handle(old_map->instance_descriptors()), num_descriptors, FROZEN); |
| 5545 | Handle<Map> new_map = Map::CopyReplaceDescriptors( |
| 5546 | old_map, new_descriptors, INSERT_TRANSITION, |
| 5547 | isolate->factory()->frozen_symbol()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5548 | new_map->freeze(); |
| 5549 | new_map->set_is_extensible(false); |
| 5550 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5551 | object->set_map(*new_map); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5552 | } else { |
| 5553 | // Slow path: need to normalize properties for safety |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5554 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5555 | |
| 5556 | // Create a new map, since other objects with this map may be extensible. |
| 5557 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5558 | Handle<Map> new_map = Map::Copy(handle(object->map())); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5559 | new_map->freeze(); |
| 5560 | new_map->set_is_extensible(false); |
| 5561 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5562 | object->set_map(*new_map); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5563 | |
| 5564 | // Freeze dictionary-mode properties |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5565 | FreezeDictionary(object->property_dictionary()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5568 | ASSERT(object->map()->has_dictionary_elements()); |
| 5569 | if (!new_element_dictionary.is_null()) { |
| 5570 | object->set_elements(*new_element_dictionary); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5571 | } |
| 5572 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5573 | if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 5574 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5575 | // Make sure we never go back to the fast case |
| 5576 | dictionary->set_requires_slow_elements(); |
| 5577 | // Freeze all elements in the dictionary |
| 5578 | FreezeDictionary(dictionary); |
| 5579 | } |
| 5580 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5581 | return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5585 | void JSObject::SetObserved(Handle<JSObject> object) { |
| 5586 | Isolate* isolate = object->GetIsolate(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5587 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5588 | if (object->map()->is_observed()) |
| 5589 | return; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5590 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5591 | if (!object->HasExternalArrayElements()) { |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5592 | // Go to dictionary mode, so that we don't skip map checks. |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5593 | NormalizeElements(object); |
| 5594 | ASSERT(!object->HasFastElements()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | LookupResult result(isolate); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5598 | object->map()->LookupTransition(*object, |
| 5599 | isolate->heap()->observed_symbol(), |
| 5600 | &result); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5601 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5602 | Handle<Map> new_map; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5603 | if (result.IsTransition()) { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5604 | new_map = handle(result.GetTransitionTarget()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5605 | ASSERT(new_map->is_observed()); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5606 | } else if (object->map()->CanHaveMoreTransitions()) { |
| 5607 | new_map = Map::CopyForObserved(handle(object->map())); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5608 | } else { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5609 | new_map = Map::Copy(handle(object->map())); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5610 | new_map->set_is_observed(true); |
| 5611 | } |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5612 | object->set_map(*new_map); |
| 5613 | } |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5614 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5615 | |
| 5616 | Handle<JSObject> JSObject::Copy(Handle<JSObject> object, |
| 5617 | Handle<AllocationSite> site) { |
| 5618 | Isolate* isolate = object->GetIsolate(); |
| 5619 | CALL_HEAP_FUNCTION(isolate, |
| 5620 | isolate->heap()->CopyJSObject(*object, *site), JSObject); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5624 | Handle<JSObject> JSObject::Copy(Handle<JSObject> object) { |
| 5625 | Isolate* isolate = object->GetIsolate(); |
| 5626 | CALL_HEAP_FUNCTION(isolate, |
| 5627 | isolate->heap()->CopyJSObject(*object), JSObject); |
| 5628 | } |
| 5629 | |
| 5630 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5631 | class JSObjectWalkVisitor { |
| 5632 | public: |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5633 | explicit JSObjectWalkVisitor(AllocationSiteContext* site_context) : |
| 5634 | site_context_(site_context) {} |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5635 | virtual ~JSObjectWalkVisitor() {} |
| 5636 | |
| 5637 | Handle<JSObject> Visit(Handle<JSObject> object) { |
| 5638 | return StructureWalk(object); |
| 5639 | } |
| 5640 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5641 | virtual bool is_copying() = 0; |
| 5642 | |
| 5643 | protected: |
| 5644 | Handle<JSObject> StructureWalk(Handle<JSObject> object); |
| 5645 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5646 | // The returned handle will be used for the object in all subsequent usages. |
| 5647 | // This allows VisitObject to make a copy of the object if desired. |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5648 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) = 0; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5649 | virtual Handle<JSObject> VisitElementOrProperty(Handle<JSObject> object, |
| 5650 | Handle<JSObject> value) = 0; |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5651 | |
| 5652 | AllocationSiteContext* site_context() { return site_context_; } |
| 5653 | |
| 5654 | private: |
| 5655 | AllocationSiteContext* site_context_; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5656 | }; |
| 5657 | |
| 5658 | |
| 5659 | class JSObjectCopyVisitor: public JSObjectWalkVisitor { |
| 5660 | public: |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5661 | explicit JSObjectCopyVisitor(AllocationSiteContext* site_context) |
| 5662 | : JSObjectWalkVisitor(site_context) {} |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5663 | |
| 5664 | virtual bool is_copying() V8_OVERRIDE { return true; } |
| 5665 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5666 | // The returned handle will be used for the object in all |
| 5667 | // subsequent usages. This allows VisitObject to make a copy |
| 5668 | // of the object if desired. |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5669 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) V8_OVERRIDE { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5670 | // Only create a memento if |
| 5671 | // 1) we have a JSArray, and |
| 5672 | // 2) the elements kind is palatable |
| 5673 | // 3) allow_mementos is true |
| 5674 | Handle<JSObject> copy; |
| 5675 | if (site_context()->activated() && |
| 5676 | AllocationSite::CanTrack(object->map()->instance_type()) && |
| 5677 | AllocationSite::GetMode(object->GetElementsKind()) == |
| 5678 | TRACK_ALLOCATION_SITE) { |
| 5679 | copy = JSObject::Copy(object, site_context()->current()); |
| 5680 | } else { |
| 5681 | copy = JSObject::Copy(object); |
| 5682 | } |
| 5683 | |
| 5684 | return copy; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | virtual Handle<JSObject> VisitElementOrProperty( |
| 5688 | Handle<JSObject> object, |
| 5689 | Handle<JSObject> value) V8_OVERRIDE { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5690 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 5691 | Handle<JSObject> copy_of_value = StructureWalk(value); |
| 5692 | site_context()->ExitScope(current_site, value); |
| 5693 | return copy_of_value; |
| 5694 | } |
| 5695 | }; |
| 5696 | |
| 5697 | |
| 5698 | class JSObjectCreateAllocationSitesVisitor: public JSObjectWalkVisitor { |
| 5699 | public: |
| 5700 | explicit JSObjectCreateAllocationSitesVisitor( |
| 5701 | AllocationSiteContext* site_context) |
| 5702 | : JSObjectWalkVisitor(site_context) {} |
| 5703 | |
| 5704 | virtual bool is_copying() V8_OVERRIDE { return false; } |
| 5705 | |
| 5706 | // The returned handle will be used for the object in all |
| 5707 | // subsequent usages. This allows VisitObject to make a copy |
| 5708 | // of the object if desired. |
| 5709 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) V8_OVERRIDE { |
| 5710 | return object; |
| 5711 | } |
| 5712 | |
| 5713 | virtual Handle<JSObject> VisitElementOrProperty( |
| 5714 | Handle<JSObject> object, |
| 5715 | Handle<JSObject> value) V8_OVERRIDE { |
| 5716 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 5717 | value = StructureWalk(value); |
| 5718 | site_context()->ExitScope(current_site, value); |
| 5719 | return value; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5720 | } |
| 5721 | }; |
| 5722 | |
| 5723 | |
| 5724 | Handle<JSObject> JSObjectWalkVisitor::StructureWalk(Handle<JSObject> object) { |
| 5725 | bool copying = is_copying(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5726 | Isolate* isolate = object->GetIsolate(); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5727 | StackLimitCheck check(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5728 | if (check.HasOverflowed()) { |
| 5729 | isolate->StackOverflow(); |
| 5730 | return Handle<JSObject>::null(); |
mvstanton@chromium.org | 5a4733b | 2013-09-16 07:33:24 +0000 | [diff] [blame] | 5731 | } |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5732 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5733 | if (object->map()->is_deprecated()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5734 | JSObject::MigrateInstance(object); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5735 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5736 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5737 | Handle<JSObject> copy = VisitObject(object); |
| 5738 | ASSERT(copying || copy.is_identical_to(object)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5739 | |
| 5740 | HandleScope scope(isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5741 | |
| 5742 | // Deep copy local properties. |
| 5743 | if (copy->HasFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5744 | Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5745 | int limit = copy->map()->NumberOfOwnDescriptors(); |
| 5746 | for (int i = 0; i < limit; i++) { |
| 5747 | PropertyDetails details = descriptors->GetDetails(i); |
| 5748 | if (details.type() != FIELD) continue; |
| 5749 | int index = descriptors->GetFieldIndex(i); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5750 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5751 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5752 | value = VisitElementOrProperty(copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5753 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, value, Handle<JSObject>()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5754 | } else { |
| 5755 | Representation representation = details.representation(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5756 | value = NewStorageFor(isolate, value, representation); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5757 | } |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5758 | if (copying) { |
| 5759 | copy->FastPropertyAtPut(index, *value); |
| 5760 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5761 | } |
| 5762 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5763 | Handle<FixedArray> names = |
| 5764 | isolate->factory()->NewFixedArray(copy->NumberOfLocalProperties()); |
| 5765 | copy->GetLocalPropertyNames(*names, 0); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5766 | for (int i = 0; i < names->length(); i++) { |
| 5767 | ASSERT(names->get(i)->IsString()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5768 | Handle<String> key_string(String::cast(names->get(i))); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5769 | PropertyAttributes attributes = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5770 | copy->GetLocalPropertyAttribute(*key_string); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5771 | // Only deep copy fields from the object literal expression. |
| 5772 | // In particular, don't try to copy the length attribute of |
| 5773 | // an array. |
| 5774 | if (attributes != NONE) continue; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5775 | Handle<Object> value( |
| 5776 | copy->GetProperty(*key_string, &attributes)->ToObjectUnchecked(), |
| 5777 | isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5778 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5779 | Handle<JSObject> result = VisitElementOrProperty( |
| 5780 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5781 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5782 | if (copying) { |
| 5783 | // Creating object copy for literals. No strict mode needed. |
| 5784 | CHECK_NOT_EMPTY_HANDLE(isolate, JSObject::SetProperty( |
| 5785 | copy, key_string, result, NONE, kNonStrictMode)); |
| 5786 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5787 | } |
| 5788 | } |
| 5789 | } |
| 5790 | |
| 5791 | // Deep copy local elements. |
| 5792 | // Pixel elements cannot be created using an object literal. |
| 5793 | ASSERT(!copy->HasExternalArrayElements()); |
| 5794 | switch (copy->GetElementsKind()) { |
| 5795 | case FAST_SMI_ELEMENTS: |
| 5796 | case FAST_ELEMENTS: |
| 5797 | case FAST_HOLEY_SMI_ELEMENTS: |
| 5798 | case FAST_HOLEY_ELEMENTS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5799 | Handle<FixedArray> elements(FixedArray::cast(copy->elements())); |
| 5800 | if (elements->map() == isolate->heap()->fixed_cow_array_map()) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5801 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 5802 | #ifdef DEBUG |
| 5803 | for (int i = 0; i < elements->length(); i++) { |
| 5804 | ASSERT(!elements->get(i)->IsJSObject()); |
| 5805 | } |
| 5806 | #endif |
| 5807 | } else { |
| 5808 | for (int i = 0; i < elements->length(); i++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5809 | Handle<Object> value(elements->get(i), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5810 | ASSERT(value->IsSmi() || |
| 5811 | value->IsTheHole() || |
| 5812 | (IsFastObjectElementsKind(copy->GetElementsKind()))); |
| 5813 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5814 | Handle<JSObject> result = VisitElementOrProperty( |
| 5815 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5816 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5817 | if (copying) { |
| 5818 | elements->set(i, *result); |
| 5819 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | break; |
| 5824 | } |
| 5825 | case DICTIONARY_ELEMENTS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5826 | Handle<SeededNumberDictionary> element_dictionary( |
| 5827 | copy->element_dictionary()); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5828 | int capacity = element_dictionary->Capacity(); |
| 5829 | for (int i = 0; i < capacity; i++) { |
| 5830 | Object* k = element_dictionary->KeyAt(i); |
| 5831 | if (element_dictionary->IsKey(k)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5832 | Handle<Object> value(element_dictionary->ValueAt(i), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5833 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5834 | Handle<JSObject> result = VisitElementOrProperty( |
| 5835 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5836 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5837 | if (copying) { |
| 5838 | element_dictionary->ValueAtPut(i, *result); |
| 5839 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5840 | } |
| 5841 | } |
| 5842 | } |
| 5843 | break; |
| 5844 | } |
| 5845 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 5846 | UNIMPLEMENTED(); |
| 5847 | break; |
| 5848 | case EXTERNAL_PIXEL_ELEMENTS: |
| 5849 | case EXTERNAL_BYTE_ELEMENTS: |
| 5850 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5851 | case EXTERNAL_SHORT_ELEMENTS: |
| 5852 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5853 | case EXTERNAL_INT_ELEMENTS: |
| 5854 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5855 | case EXTERNAL_FLOAT_ELEMENTS: |
| 5856 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 5857 | case FAST_DOUBLE_ELEMENTS: |
| 5858 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 5859 | // No contained objects, nothing to do. |
| 5860 | break; |
| 5861 | } |
| 5862 | return copy; |
| 5863 | } |
| 5864 | |
| 5865 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 5866 | Handle<JSObject> JSObject::DeepWalk(Handle<JSObject> object, |
| 5867 | AllocationSiteContext* site_context) { |
| 5868 | JSObjectCreateAllocationSitesVisitor v(site_context); |
| 5869 | Handle<JSObject> result = v.Visit(object); |
| 5870 | ASSERT(!v.is_copying() && |
| 5871 | (result.is_null() || result.is_identical_to(object))); |
| 5872 | return result; |
| 5873 | } |
| 5874 | |
| 5875 | |
| 5876 | Handle<JSObject> JSObject::DeepCopy(Handle<JSObject> object, |
| 5877 | AllocationSiteContext* site_context) { |
| 5878 | JSObjectCopyVisitor v(site_context); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5879 | Handle<JSObject> copy = v.Visit(object); |
| 5880 | ASSERT(v.is_copying() && !copy.is_identical_to(object)); |
| 5881 | return copy; |
| 5882 | } |
| 5883 | |
| 5884 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5885 | // Tests for the fast common case for property enumeration: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5886 | // - This object and all prototypes has an enum cache (which means that |
| 5887 | // 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] | 5888 | // - This object has no elements. |
| 5889 | // - No prototype has enumerable properties/elements. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5890 | bool JSReceiver::IsSimpleEnum() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5891 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5892 | for (Object* o = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5893 | o != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5894 | o = JSObject::cast(o)->GetPrototype()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5895 | if (!o->IsJSObject()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5896 | JSObject* curr = JSObject::cast(o); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5897 | int enum_length = curr->map()->EnumLength(); |
| 5898 | if (enum_length == Map::kInvalidEnumCache) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5899 | ASSERT(!curr->HasNamedInterceptor()); |
| 5900 | ASSERT(!curr->HasIndexedInterceptor()); |
| 5901 | ASSERT(!curr->IsAccessCheckNeeded()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5902 | if (curr->NumberOfEnumElements() > 0) return false; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5903 | if (curr != this && enum_length != 0) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5904 | } |
| 5905 | return true; |
| 5906 | } |
| 5907 | |
| 5908 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5909 | int Map::NumberOfDescribedProperties(DescriptorFlag which, |
| 5910 | PropertyAttributes filter) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5911 | int result = 0; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5912 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5913 | int limit = which == ALL_DESCRIPTORS |
| 5914 | ? descs->number_of_descriptors() |
| 5915 | : NumberOfOwnDescriptors(); |
| 5916 | for (int i = 0; i < limit; i++) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5917 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 5918 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 5919 | result++; |
| 5920 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5921 | } |
| 5922 | return result; |
| 5923 | } |
| 5924 | |
| 5925 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5926 | int Map::NextFreePropertyIndex() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5927 | int max_index = -1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5928 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5929 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5930 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5931 | if (descs->GetType(i) == FIELD) { |
| 5932 | int current_index = descs->GetFieldIndex(i); |
| 5933 | if (current_index > max_index) max_index = current_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5934 | } |
| 5935 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5936 | return max_index + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5937 | } |
| 5938 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5939 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5940 | AccessorDescriptor* Map::FindAccessor(Name* name) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5941 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5942 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 5943 | for (int i = 0; i < number_of_own_descriptors; i++) { |
| 5944 | if (descs->GetType(i) == CALLBACKS && name->Equals(descs->GetKey(i))) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5945 | return descs->GetCallbacks(i); |
| 5946 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5947 | } |
| 5948 | return NULL; |
| 5949 | } |
| 5950 | |
| 5951 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5952 | void JSReceiver::LocalLookup( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5953 | Name* name, LookupResult* result, bool search_hidden_prototypes) { |
| 5954 | ASSERT(name->IsName()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5955 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5956 | Heap* heap = GetHeap(); |
| 5957 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5958 | if (IsJSGlobalProxy()) { |
| 5959 | Object* proto = GetPrototype(); |
| 5960 | if (proto->IsNull()) return result->NotFound(); |
| 5961 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5962 | return JSReceiver::cast(proto)->LocalLookup( |
| 5963 | name, result, search_hidden_prototypes); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | if (IsJSProxy()) { |
| 5967 | result->HandlerResult(JSProxy::cast(this)); |
| 5968 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5969 | } |
| 5970 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5971 | // Do not use inline caching if the object is a non-global object |
| 5972 | // that requires access checks. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5973 | if (IsAccessCheckNeeded()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5974 | result->DisallowCaching(); |
| 5975 | } |
| 5976 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5977 | JSObject* js_object = JSObject::cast(this); |
| 5978 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5979 | // Check for lookup interceptor except when bootstrapping. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5980 | if (js_object->HasNamedInterceptor() && |
| 5981 | !heap->isolate()->bootstrapper()->IsActive()) { |
| 5982 | result->InterceptorResult(js_object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5983 | return; |
| 5984 | } |
| 5985 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5986 | js_object->LocalLookupRealNamedProperty(name, result); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5987 | if (result->IsFound() || !search_hidden_prototypes) return; |
| 5988 | |
| 5989 | Object* proto = js_object->GetPrototype(); |
| 5990 | if (!proto->IsJSReceiver()) return; |
| 5991 | JSReceiver* receiver = JSReceiver::cast(proto); |
| 5992 | if (receiver->map()->is_hidden_prototype()) { |
| 5993 | receiver->LocalLookup(name, result, search_hidden_prototypes); |
| 5994 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5998 | void JSReceiver::Lookup(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5999 | // Ecma-262 3rd 8.6.2.4 |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6000 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6001 | for (Object* current = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6002 | current != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6003 | current = JSObject::cast(current)->GetPrototype()) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 6004 | JSReceiver::cast(current)->LocalLookup(name, result, false); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6005 | if (result->IsFound()) return; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6006 | } |
| 6007 | result->NotFound(); |
| 6008 | } |
| 6009 | |
| 6010 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6011 | // Search object and its prototype chain for callback properties. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6012 | void JSObject::LookupCallbackProperty(Name* name, LookupResult* result) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6013 | Heap* heap = GetHeap(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6014 | for (Object* current = this; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 6015 | current != heap->null_value() && current->IsJSObject(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6016 | current = JSObject::cast(current)->GetPrototype()) { |
| 6017 | JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6018 | if (result->IsPropertyCallbacks()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6019 | } |
| 6020 | result->NotFound(); |
| 6021 | } |
| 6022 | |
| 6023 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6024 | // Try to update an accessor in an elements dictionary. Return true if the |
| 6025 | // update succeeded, and false otherwise. |
| 6026 | static bool UpdateGetterSetterInDictionary( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6027 | SeededNumberDictionary* dictionary, |
| 6028 | uint32_t index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6029 | Object* getter, |
| 6030 | Object* setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6031 | PropertyAttributes attributes) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6032 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6033 | if (entry != SeededNumberDictionary::kNotFound) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6034 | Object* result = dictionary->ValueAt(entry); |
| 6035 | PropertyDetails details = dictionary->DetailsAt(entry); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6036 | if (details.type() == CALLBACKS && result->IsAccessorPair()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6037 | ASSERT(!details.IsDontDelete()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6038 | if (details.attributes() != attributes) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6039 | dictionary->DetailsAtPut( |
| 6040 | entry, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6041 | PropertyDetails(attributes, CALLBACKS, index)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6042 | } |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6043 | AccessorPair::cast(result)->SetComponents(getter, setter); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6044 | return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6045 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6046 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6047 | return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6051 | void JSObject::DefineElementAccessor(Handle<JSObject> object, |
| 6052 | uint32_t index, |
| 6053 | Handle<Object> getter, |
| 6054 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6055 | PropertyAttributes attributes, |
| 6056 | v8::AccessControl access_control) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6057 | switch (object->GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6058 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6059 | case FAST_ELEMENTS: |
| 6060 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6061 | case FAST_HOLEY_SMI_ELEMENTS: |
| 6062 | case FAST_HOLEY_ELEMENTS: |
| 6063 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6064 | break; |
| 6065 | case EXTERNAL_PIXEL_ELEMENTS: |
| 6066 | case EXTERNAL_BYTE_ELEMENTS: |
| 6067 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6068 | case EXTERNAL_SHORT_ELEMENTS: |
| 6069 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6070 | case EXTERNAL_INT_ELEMENTS: |
| 6071 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6072 | case EXTERNAL_FLOAT_ELEMENTS: |
| 6073 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 6074 | // Ignore getters and setters on pixel and external array elements. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6075 | return; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6076 | case DICTIONARY_ELEMENTS: |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6077 | if (UpdateGetterSetterInDictionary(object->element_dictionary(), |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6078 | index, |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6079 | *getter, |
| 6080 | *setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6081 | attributes)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6082 | return; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6083 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6084 | break; |
| 6085 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 6086 | // Ascertain whether we have read-only properties or an existing |
| 6087 | // getter/setter pair in an arguments elements dictionary backing |
| 6088 | // store. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6089 | FixedArray* parameter_map = FixedArray::cast(object->elements()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6090 | uint32_t length = parameter_map->length(); |
| 6091 | Object* probe = |
| 6092 | index < (length - 2) ? parameter_map->get(index + 2) : NULL; |
| 6093 | if (probe == NULL || probe->IsTheHole()) { |
| 6094 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 6095 | if (arguments->IsDictionary()) { |
| 6096 | SeededNumberDictionary* dictionary = |
| 6097 | SeededNumberDictionary::cast(arguments); |
| 6098 | if (UpdateGetterSetterInDictionary(dictionary, |
| 6099 | index, |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6100 | *getter, |
| 6101 | *setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6102 | attributes)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6103 | return; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 6104 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6105 | } |
| 6106 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6107 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6108 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6111 | Isolate* isolate = object->GetIsolate(); |
| 6112 | Handle<AccessorPair> accessors = isolate->factory()->NewAccessorPair(); |
| 6113 | accessors->SetComponents(*getter, *setter); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6114 | accessors->set_access_flags(access_control); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6115 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6116 | SetElementCallback(object, index, accessors, attributes); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6120 | Handle<AccessorPair> JSObject::CreateAccessorPairFor(Handle<JSObject> object, |
| 6121 | Handle<Name> name) { |
| 6122 | Isolate* isolate = object->GetIsolate(); |
| 6123 | LookupResult result(isolate); |
| 6124 | object->LocalLookupRealNamedProperty(*name, &result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6125 | if (result.IsPropertyCallbacks()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6126 | // Note that the result can actually have IsDontDelete() == true when we |
| 6127 | // e.g. have to fall back to the slow case while adding a setter after |
| 6128 | // successfully reusing a map transition for a getter. Nevertheless, this is |
| 6129 | // OK, because the assertion only holds for the whole addition of both |
| 6130 | // accessors, not for the addition of each part. See first comment in |
| 6131 | // DefinePropertyAccessor below. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6132 | Object* obj = result.GetCallbackObject(); |
| 6133 | if (obj->IsAccessorPair()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6134 | return AccessorPair::Copy(handle(AccessorPair::cast(obj), isolate)); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6135 | } |
| 6136 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6137 | return isolate->factory()->NewAccessorPair(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6138 | } |
| 6139 | |
| 6140 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6141 | void JSObject::DefinePropertyAccessor(Handle<JSObject> object, |
| 6142 | Handle<Name> name, |
| 6143 | Handle<Object> getter, |
| 6144 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6145 | PropertyAttributes attributes, |
| 6146 | v8::AccessControl access_control) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6147 | // We could assert that the property is configurable here, but we would need |
| 6148 | // to do a lookup, which seems to be a bit of overkill. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6149 | bool only_attribute_changes = getter->IsNull() && setter->IsNull(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6150 | if (object->HasFastProperties() && !only_attribute_changes && |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6151 | access_control == v8::DEFAULT && |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6152 | (object->map()->NumberOfOwnDescriptors() < |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6153 | DescriptorArray::kMaxNumberOfDescriptors)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6154 | bool getterOk = getter->IsNull() || |
| 6155 | DefineFastAccessor(object, name, ACCESSOR_GETTER, getter, attributes); |
| 6156 | bool setterOk = !getterOk || setter->IsNull() || |
| 6157 | DefineFastAccessor(object, name, ACCESSOR_SETTER, setter, attributes); |
| 6158 | if (getterOk && setterOk) return; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6159 | } |
| 6160 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6161 | Handle<AccessorPair> accessors = CreateAccessorPairFor(object, name); |
| 6162 | accessors->SetComponents(*getter, *setter); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6163 | accessors->set_access_flags(access_control); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6164 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6165 | SetPropertyCallback(object, name, accessors, attributes); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6169 | bool JSObject::CanSetCallback(Name* name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6170 | ASSERT(!IsAccessCheckNeeded() || |
| 6171 | GetIsolate()->MayNamedAccess(this, name, v8::ACCESS_SET)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6172 | |
| 6173 | // Check if there is an API defined callback object which prohibits |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6174 | // callback overwriting in this object or its prototype chain. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6175 | // This mechanism is needed for instance in a browser setting, where |
| 6176 | // certain accessors such as window.location should not be allowed |
| 6177 | // to be overwritten because allowing overwriting could potentially |
| 6178 | // cause security problems. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6179 | LookupResult callback_result(GetIsolate()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6180 | LookupCallbackProperty(name, &callback_result); |
| 6181 | if (callback_result.IsFound()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6182 | Object* obj = callback_result.GetCallbackObject(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6183 | if (obj->IsAccessorInfo()) { |
| 6184 | return !AccessorInfo::cast(obj)->prohibits_overwriting(); |
| 6185 | } |
| 6186 | if (obj->IsAccessorPair()) { |
| 6187 | return !AccessorPair::cast(obj)->prohibits_overwriting(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6188 | } |
| 6189 | } |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6190 | return true; |
| 6191 | } |
| 6192 | |
| 6193 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6194 | void JSObject::SetElementCallback(Handle<JSObject> object, |
| 6195 | uint32_t index, |
| 6196 | Handle<Object> structure, |
| 6197 | PropertyAttributes attributes) { |
| 6198 | Heap* heap = object->GetHeap(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6199 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6200 | |
| 6201 | // Normalize elements to make this operation simple. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6202 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 6203 | ASSERT(object->HasDictionaryElements() || |
| 6204 | object->HasDictionaryArgumentsElements()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6205 | |
| 6206 | // Update the dictionary with the new CALLBACKS property. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6207 | dictionary = SeededNumberDictionary::Set(dictionary, index, structure, |
| 6208 | details); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6209 | dictionary->set_requires_slow_elements(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6210 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6211 | // Update the dictionary backing store on the object. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6212 | if (object->elements()->map() == heap->non_strict_arguments_elements_map()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6213 | // Also delete any parameter alias. |
| 6214 | // |
| 6215 | // TODO(kmillikin): when deleting the last parameter alias we could |
| 6216 | // switch to a direct backing store without the parameter map. This |
| 6217 | // would allow GC of the context. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6218 | FixedArray* parameter_map = FixedArray::cast(object->elements()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6219 | if (index < static_cast<uint32_t>(parameter_map->length()) - 2) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6220 | parameter_map->set(index + 2, heap->the_hole_value()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6221 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6222 | parameter_map->set(1, *dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6223 | } else { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6224 | object->set_elements(*dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6225 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
| 6228 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6229 | void JSObject::SetPropertyCallback(Handle<JSObject> object, |
| 6230 | Handle<Name> name, |
| 6231 | Handle<Object> structure, |
| 6232 | PropertyAttributes attributes) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6233 | // Normalize object to make this operation simple. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6234 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6235 | |
| 6236 | // For the global object allocate a new map to invalidate the global inline |
| 6237 | // caches which have a global property cell reference directly in the code. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6238 | if (object->IsGlobalObject()) { |
| 6239 | Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 6240 | ASSERT(new_map->is_dictionary_map()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6241 | object->set_map(*new_map); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6242 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6243 | // When running crankshaft, changing the map is not enough. We |
| 6244 | // need to deoptimize all functions that rely on this global |
| 6245 | // object. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6246 | Deoptimizer::DeoptimizeGlobalObject(*object); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | // Update the dictionary with the new CALLBACKS property. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6250 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6251 | SetNormalizedProperty(object, name, structure, details); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6254 | |
| 6255 | void JSObject::DefineAccessor(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6256 | Handle<Name> name, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6257 | Handle<Object> getter, |
| 6258 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6259 | PropertyAttributes attributes, |
| 6260 | v8::AccessControl access_control) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6261 | Isolate* isolate = object->GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6262 | // Check access rights if needed. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6263 | if (object->IsAccessCheckNeeded() && |
| 6264 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 6265 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 6266 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6267 | } |
| 6268 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6269 | if (object->IsJSGlobalProxy()) { |
| 6270 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 6271 | if (proto->IsNull()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6272 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6273 | DefineAccessor(Handle<JSObject>::cast(proto), |
| 6274 | name, |
| 6275 | getter, |
| 6276 | setter, |
| 6277 | attributes, |
| 6278 | access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6279 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6282 | // Make sure that the top context does not change when doing callbacks or |
| 6283 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6284 | AssertNoContextChange ncc(isolate); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6285 | |
| 6286 | // Try to flatten before operating on the string. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6287 | if (name->IsString()) String::cast(*name)->TryFlatten(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6288 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6289 | if (!object->CanSetCallback(*name)) return; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6290 | |
| 6291 | uint32_t index = 0; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6292 | bool is_element = name->AsArrayIndex(&index); |
| 6293 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 6294 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6295 | bool is_observed = FLAG_harmony_observation && object->map()->is_observed(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6296 | bool preexists = false; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 6297 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6298 | if (is_element) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6299 | preexists = HasLocalElement(object, index); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6300 | if (preexists && object->GetLocalElementAccessorPair(index) == NULL) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 6301 | old_value = Object::GetElement(isolate, object, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6302 | } |
| 6303 | } else { |
| 6304 | LookupResult lookup(isolate); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6305 | object->LocalLookup(*name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6306 | preexists = lookup.IsProperty(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 6307 | if (preexists && lookup.IsDataProperty()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6308 | old_value = Object::GetProperty(object, name); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 6309 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6310 | } |
| 6311 | } |
| 6312 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6313 | if (is_element) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6314 | DefineElementAccessor( |
| 6315 | object, index, getter, setter, attributes, access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6316 | } else { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6317 | DefinePropertyAccessor( |
| 6318 | object, name, getter, setter, attributes, access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6319 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6320 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 6321 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6322 | const char* type = preexists ? "reconfigured" : "new"; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6323 | EnqueueChangeRecord(object, type, name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6324 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6328 | static bool TryAccessorTransition(JSObject* self, |
| 6329 | Map* transitioned_map, |
| 6330 | int target_descriptor, |
| 6331 | AccessorComponent component, |
| 6332 | Object* accessor, |
| 6333 | PropertyAttributes attributes) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6334 | DescriptorArray* descs = transitioned_map->instance_descriptors(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6335 | PropertyDetails details = descs->GetDetails(target_descriptor); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6336 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6337 | // If the transition target was not callbacks, fall back to the slow case. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6338 | if (details.type() != CALLBACKS) return false; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6339 | Object* descriptor = descs->GetCallbacksObject(target_descriptor); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6340 | if (!descriptor->IsAccessorPair()) return false; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6341 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6342 | Object* target_accessor = AccessorPair::cast(descriptor)->get(component); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6343 | PropertyAttributes target_attributes = details.attributes(); |
| 6344 | |
| 6345 | // Reuse transition if adding same accessor with same attributes. |
| 6346 | if (target_accessor == accessor && target_attributes == attributes) { |
| 6347 | self->set_map(transitioned_map); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6348 | return true; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6351 | // If either not the same accessor, or not the same attributes, fall back to |
| 6352 | // the slow case. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6353 | return false; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6357 | static MaybeObject* CopyInsertDescriptor(Map* map, |
| 6358 | Name* name, |
| 6359 | AccessorPair* accessors, |
| 6360 | PropertyAttributes attributes) { |
| 6361 | CallbacksDescriptor new_accessors_desc(name, accessors, attributes); |
| 6362 | return map->CopyInsertDescriptor(&new_accessors_desc, INSERT_TRANSITION); |
| 6363 | } |
| 6364 | |
| 6365 | |
| 6366 | static Handle<Map> CopyInsertDescriptor(Handle<Map> map, |
| 6367 | Handle<Name> name, |
| 6368 | Handle<AccessorPair> accessors, |
| 6369 | PropertyAttributes attributes) { |
| 6370 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6371 | CopyInsertDescriptor(*map, *name, *accessors, attributes), |
| 6372 | Map); |
| 6373 | } |
| 6374 | |
| 6375 | |
| 6376 | bool JSObject::DefineFastAccessor(Handle<JSObject> object, |
| 6377 | Handle<Name> name, |
| 6378 | AccessorComponent component, |
| 6379 | Handle<Object> accessor, |
| 6380 | PropertyAttributes attributes) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6381 | ASSERT(accessor->IsSpecFunction() || accessor->IsUndefined()); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6382 | Isolate* isolate = object->GetIsolate(); |
| 6383 | LookupResult result(isolate); |
| 6384 | object->LocalLookup(*name, &result); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6385 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 6386 | if (result.IsFound() && !result.IsPropertyCallbacks()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6387 | return false; |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 6388 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6389 | |
| 6390 | // Return success if the same accessor with the same attributes already exist. |
| 6391 | AccessorPair* source_accessors = NULL; |
| 6392 | if (result.IsPropertyCallbacks()) { |
| 6393 | Object* callback_value = result.GetCallbackObject(); |
| 6394 | if (callback_value->IsAccessorPair()) { |
| 6395 | source_accessors = AccessorPair::cast(callback_value); |
| 6396 | Object* entry = source_accessors->get(component); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6397 | if (entry == *accessor && result.GetAttributes() == attributes) { |
| 6398 | return true; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6399 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6400 | } else { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6401 | return false; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6402 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6403 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6404 | int descriptor_number = result.GetDescriptorIndex(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6405 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6406 | object->map()->LookupTransition(*object, *name, &result); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6407 | |
| 6408 | if (result.IsFound()) { |
| 6409 | Map* target = result.GetTransitionTarget(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6410 | ASSERT(target->NumberOfOwnDescriptors() == |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6411 | object->map()->NumberOfOwnDescriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6412 | // This works since descriptors are sorted in order of addition. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6413 | ASSERT(object->map()->instance_descriptors()-> |
| 6414 | GetKey(descriptor_number) == *name); |
| 6415 | return TryAccessorTransition(*object, target, descriptor_number, |
| 6416 | component, *accessor, attributes); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6417 | } |
| 6418 | } else { |
| 6419 | // If not, lookup a transition. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6420 | object->map()->LookupTransition(*object, *name, &result); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6421 | |
| 6422 | // If there is a transition, try to follow it. |
| 6423 | if (result.IsFound()) { |
| 6424 | Map* target = result.GetTransitionTarget(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6425 | int descriptor_number = target->LastAdded(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6426 | ASSERT(target->instance_descriptors()->GetKey(descriptor_number) |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6427 | ->Equals(*name)); |
| 6428 | return TryAccessorTransition(*object, target, descriptor_number, |
| 6429 | component, *accessor, attributes); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6430 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6433 | // If there is no transition yet, add a transition to the a new accessor pair |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6434 | // containing the accessor. Allocate a new pair if there were no source |
| 6435 | // accessors. Otherwise, copy the pair and modify the accessor. |
| 6436 | Handle<AccessorPair> accessors = source_accessors != NULL |
| 6437 | ? AccessorPair::Copy(Handle<AccessorPair>(source_accessors)) |
| 6438 | : isolate->factory()->NewAccessorPair(); |
| 6439 | accessors->set(component, *accessor); |
| 6440 | Handle<Map> new_map = CopyInsertDescriptor(Handle<Map>(object->map()), |
| 6441 | name, accessors, attributes); |
| 6442 | object->set_map(*new_map); |
| 6443 | return true; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6447 | Handle<Object> JSObject::SetAccessor(Handle<JSObject> object, |
| 6448 | Handle<AccessorInfo> info) { |
| 6449 | Isolate* isolate = object->GetIsolate(); |
| 6450 | Factory* factory = isolate->factory(); |
| 6451 | Handle<Name> name(Name::cast(info->name())); |
| 6452 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6453 | // Check access rights if needed. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6454 | if (object->IsAccessCheckNeeded() && |
| 6455 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 6456 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 6457 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 6458 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6461 | if (object->IsJSGlobalProxy()) { |
| 6462 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 6463 | if (proto->IsNull()) return object; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6464 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6465 | return SetAccessor(Handle<JSObject>::cast(proto), info); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | // Make sure that the top context does not change when doing callbacks or |
| 6469 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6470 | AssertNoContextChange ncc(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6471 | |
| 6472 | // Try to flatten before operating on the string. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6473 | if (name->IsString()) FlattenString(Handle<String>::cast(name)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6474 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6475 | if (!object->CanSetCallback(*name)) return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6476 | |
| 6477 | uint32_t index = 0; |
| 6478 | bool is_element = name->AsArrayIndex(&index); |
| 6479 | |
| 6480 | if (is_element) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6481 | if (object->IsJSArray()) return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6482 | |
| 6483 | // Accessors overwrite previous callbacks (cf. with getters/setters). |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6484 | switch (object->GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6485 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6486 | case FAST_ELEMENTS: |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6487 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6488 | case FAST_HOLEY_SMI_ELEMENTS: |
| 6489 | case FAST_HOLEY_ELEMENTS: |
| 6490 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6491 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6492 | case EXTERNAL_PIXEL_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6493 | case EXTERNAL_BYTE_ELEMENTS: |
| 6494 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6495 | case EXTERNAL_SHORT_ELEMENTS: |
| 6496 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6497 | case EXTERNAL_INT_ELEMENTS: |
| 6498 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6499 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 6500 | case EXTERNAL_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6501 | // Ignore getters and setters on pixel and external array |
| 6502 | // elements. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6503 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6504 | case DICTIONARY_ELEMENTS: |
| 6505 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6506 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 6507 | UNIMPLEMENTED(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6508 | break; |
| 6509 | } |
| 6510 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6511 | SetElementCallback(object, index, info, info->property_attributes()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6512 | } else { |
| 6513 | // Lookup the name. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6514 | LookupResult result(isolate); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6515 | object->LocalLookup(*name, &result, true); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6516 | // ES5 forbids turning a property into an accessor if it's not |
| 6517 | // 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] | 6518 | if (result.IsFound() && (result.IsReadOnly() || result.IsDontDelete())) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6519 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6520 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6521 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6522 | SetPropertyCallback(object, name, info, info->property_attributes()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6523 | } |
| 6524 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6525 | return object; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6529 | Handle<Object> JSObject::GetAccessor(Handle<JSObject> object, |
| 6530 | Handle<Name> name, |
| 6531 | AccessorComponent component) { |
| 6532 | Isolate* isolate = object->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6533 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6534 | // Make sure that the top context does not change when doing callbacks or |
| 6535 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6536 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6537 | |
| 6538 | // Check access rights if needed. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6539 | if (object->IsAccessCheckNeeded() && |
| 6540 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_HAS)) { |
| 6541 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_HAS); |
| 6542 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 6543 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6546 | // Make the lookup and include prototypes. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6547 | uint32_t index = 0; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6548 | if (name->AsArrayIndex(&index)) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6549 | for (Handle<Object> obj = object; |
| 6550 | *obj != isolate->heap()->null_value(); |
| 6551 | obj = handle(JSReceiver::cast(*obj)->GetPrototype(), isolate)) { |
| 6552 | if (obj->IsJSObject() && JSObject::cast(*obj)->HasDictionaryElements()) { |
| 6553 | JSObject* js_object = JSObject::cast(*obj); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6554 | SeededNumberDictionary* dictionary = js_object->element_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6555 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6556 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6557 | Object* element = dictionary->ValueAt(entry); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 6558 | if (dictionary->DetailsAt(entry).type() == CALLBACKS && |
| 6559 | element->IsAccessorPair()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6560 | return handle(AccessorPair::cast(element)->GetComponent(component), |
| 6561 | isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6562 | } |
| 6563 | } |
| 6564 | } |
| 6565 | } |
| 6566 | } else { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6567 | for (Handle<Object> obj = object; |
| 6568 | *obj != isolate->heap()->null_value(); |
| 6569 | obj = handle(JSReceiver::cast(*obj)->GetPrototype(), isolate)) { |
| 6570 | LookupResult result(isolate); |
| 6571 | JSReceiver::cast(*obj)->LocalLookup(*name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6572 | if (result.IsFound()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6573 | if (result.IsReadOnly()) return isolate->factory()->undefined_value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6574 | if (result.IsPropertyCallbacks()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6575 | Object* obj = result.GetCallbackObject(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6576 | if (obj->IsAccessorPair()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6577 | return handle(AccessorPair::cast(obj)->GetComponent(component), |
| 6578 | isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6579 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6580 | } |
| 6581 | } |
| 6582 | } |
| 6583 | } |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6584 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | |
| 6588 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 6589 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6590 | int number_of_own_descriptors = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6591 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6592 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6593 | if (descs->GetType(i) == FIELD) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6594 | Object* property = RawFastPropertyAt(descs->GetFieldIndex(i)); |
| 6595 | if (FLAG_track_double_fields && |
| 6596 | descs->GetDetails(i).representation().IsDouble()) { |
| 6597 | ASSERT(property->IsHeapNumber()); |
| 6598 | if (value->IsNumber() && property->Number() == value->Number()) { |
| 6599 | return descs->GetKey(i); |
| 6600 | } |
| 6601 | } else if (property == value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6602 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6603 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 6604 | } else if (descs->GetType(i) == CONSTANT) { |
| 6605 | if (descs->GetConstant(i) == value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6606 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6607 | } |
| 6608 | } |
| 6609 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6610 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6611 | } else { |
| 6612 | return property_dictionary()->SlowReverseLookup(value); |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6617 | MaybeObject* Map::RawCopy(int instance_size) { |
| 6618 | Map* result; |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6619 | MaybeObject* maybe_result = |
| 6620 | GetHeap()->AllocateMap(instance_type(), instance_size); |
| 6621 | if (!maybe_result->To(&result)) return maybe_result; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6622 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6623 | result->set_prototype(prototype()); |
| 6624 | result->set_constructor(constructor()); |
| 6625 | result->set_bit_field(bit_field()); |
| 6626 | result->set_bit_field2(bit_field2()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6627 | int new_bit_field3 = bit_field3(); |
| 6628 | new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true); |
| 6629 | new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0); |
| 6630 | new_bit_field3 = EnumLengthBits::update(new_bit_field3, kInvalidEnumCache); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6631 | new_bit_field3 = Deprecated::update(new_bit_field3, false); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 6632 | new_bit_field3 = IsUnstable::update(new_bit_field3, false); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6633 | result->set_bit_field3(new_bit_field3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6634 | return result; |
| 6635 | } |
| 6636 | |
| 6637 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 6638 | Handle<Map> Map::CopyNormalized(Handle<Map> map, |
| 6639 | PropertyNormalizationMode mode, |
| 6640 | NormalizedMapSharingMode sharing) { |
| 6641 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6642 | map->CopyNormalized(mode, sharing), |
| 6643 | Map); |
| 6644 | } |
| 6645 | |
| 6646 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6647 | MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode, |
| 6648 | NormalizedMapSharingMode sharing) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6649 | int new_instance_size = instance_size(); |
| 6650 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
| 6651 | new_instance_size -= inobject_properties() * kPointerSize; |
| 6652 | } |
| 6653 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6654 | Map* result; |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6655 | MaybeObject* maybe_result = RawCopy(new_instance_size); |
| 6656 | if (!maybe_result->To(&result)) return maybe_result; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6657 | |
| 6658 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6659 | result->set_inobject_properties(inobject_properties()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6662 | result->set_is_shared(sharing == SHARED_NORMALIZED_MAP); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 6663 | result->set_dictionary_map(true); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 6664 | result->set_migration_target(false); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6665 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 6666 | #ifdef VERIFY_HEAP |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6667 | if (FLAG_verify_heap && result->is_shared()) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6668 | result->SharedMapVerify(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6669 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6670 | #endif |
| 6671 | |
| 6672 | return result; |
| 6673 | } |
| 6674 | |
| 6675 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 6676 | Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) { |
| 6677 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->CopyDropDescriptors(), Map); |
| 6678 | } |
| 6679 | |
| 6680 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6681 | MaybeObject* Map::CopyDropDescriptors() { |
| 6682 | Map* result; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6683 | MaybeObject* maybe_result = RawCopy(instance_size()); |
| 6684 | if (!maybe_result->To(&result)) return maybe_result; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6685 | |
| 6686 | // Please note instance_type and instance_size are set when allocated. |
| 6687 | result->set_inobject_properties(inobject_properties()); |
| 6688 | result->set_unused_property_fields(unused_property_fields()); |
| 6689 | |
| 6690 | result->set_pre_allocated_property_fields(pre_allocated_property_fields()); |
| 6691 | result->set_is_shared(false); |
| 6692 | result->ClearCodeCache(GetHeap()); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 6693 | NotifyLeafMapLayoutChange(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6694 | return result; |
| 6695 | } |
| 6696 | |
| 6697 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6698 | MaybeObject* Map::ShareDescriptor(DescriptorArray* descriptors, |
| 6699 | Descriptor* descriptor) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6700 | // Sanity check. This path is only to be taken if the map owns its descriptor |
| 6701 | // array, implying that its NumberOfOwnDescriptors equals the number of |
| 6702 | // descriptors in the descriptor array. |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6703 | ASSERT(NumberOfOwnDescriptors() == |
| 6704 | instance_descriptors()->number_of_descriptors()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 6705 | Map* result; |
| 6706 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6707 | if (!maybe_result->To(&result)) return maybe_result; |
| 6708 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6709 | Name* name = descriptor->GetKey(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6710 | |
| 6711 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6712 | MaybeObject* maybe_transitions = |
| 6713 | AddTransition(name, result, SIMPLE_TRANSITION); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6714 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6715 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6716 | int old_size = descriptors->number_of_descriptors(); |
| 6717 | |
| 6718 | DescriptorArray* new_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6719 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6720 | if (descriptors->NumberOfSlackDescriptors() > 0) { |
| 6721 | new_descriptors = descriptors; |
| 6722 | new_descriptors->Append(descriptor); |
| 6723 | } else { |
| 6724 | // Descriptor arrays grow by 50%. |
| 6725 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 6726 | GetIsolate(), old_size, old_size < 4 ? 1 : old_size / 2); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6727 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6728 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6729 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
| 6730 | |
| 6731 | // Copy the descriptors, inserting a descriptor. |
| 6732 | for (int i = 0; i < old_size; ++i) { |
| 6733 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 6734 | } |
| 6735 | |
| 6736 | new_descriptors->Append(descriptor, witness); |
| 6737 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6738 | if (old_size > 0) { |
| 6739 | // If the source descriptors had an enum cache we copy it. This ensures |
| 6740 | // that the maps to which we push the new descriptor array back can rely |
| 6741 | // on a cache always being available once it is set. If the map has more |
| 6742 | // enumerated descriptors than available in the original cache, the cache |
| 6743 | // will be lazily replaced by the extended cache when needed. |
| 6744 | if (descriptors->HasEnumCache()) { |
| 6745 | new_descriptors->CopyEnumCacheFrom(descriptors); |
| 6746 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6747 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6748 | Map* map; |
| 6749 | // Replace descriptors by new_descriptors in all maps that share it. |
| 6750 | for (Object* current = GetBackPointer(); |
| 6751 | !current->IsUndefined(); |
| 6752 | current = map->GetBackPointer()) { |
| 6753 | map = Map::cast(current); |
| 6754 | if (map->instance_descriptors() != descriptors) break; |
| 6755 | map->set_instance_descriptors(new_descriptors); |
| 6756 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6757 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6758 | set_instance_descriptors(new_descriptors); |
| 6759 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6760 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6761 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6762 | result->SetBackPointer(this); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6763 | result->InitializeDescriptors(new_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6764 | ASSERT(result->NumberOfOwnDescriptors() == NumberOfOwnDescriptors() + 1); |
| 6765 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6766 | set_transitions(transitions); |
| 6767 | set_owns_descriptors(false); |
| 6768 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6769 | return result; |
| 6770 | } |
| 6771 | |
| 6772 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6773 | Handle<Map> Map::CopyReplaceDescriptors(Handle<Map> map, |
| 6774 | Handle<DescriptorArray> descriptors, |
| 6775 | TransitionFlag flag, |
| 6776 | Handle<Name> name) { |
| 6777 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6778 | map->CopyReplaceDescriptors(*descriptors, flag, *name), |
| 6779 | Map); |
| 6780 | } |
| 6781 | |
| 6782 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6783 | MaybeObject* Map::CopyReplaceDescriptors(DescriptorArray* descriptors, |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6784 | TransitionFlag flag, |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 6785 | Name* name, |
| 6786 | SimpleTransitionFlag simple_flag) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6787 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6788 | |
| 6789 | Map* result; |
| 6790 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6791 | if (!maybe_result->To(&result)) return maybe_result; |
| 6792 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6793 | result->InitializeDescriptors(descriptors); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6794 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6795 | if (flag == INSERT_TRANSITION && CanHaveMoreTransitions()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6796 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6797 | MaybeObject* maybe_transitions = AddTransition(name, result, simple_flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6798 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6799 | set_transitions(transitions); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6800 | result->SetBackPointer(this); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 6801 | } else { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6802 | descriptors->InitializeRepresentations(Representation::Tagged()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6805 | return result; |
| 6806 | } |
| 6807 | |
| 6808 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6809 | Handle<Map> Map::CopyInstallDescriptors(Handle<Map> map, |
| 6810 | int new_descriptor, |
| 6811 | Handle<DescriptorArray> descriptors) { |
| 6812 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6813 | map->CopyInstallDescriptors(new_descriptor, *descriptors), |
| 6814 | Map); |
| 6815 | } |
| 6816 | |
| 6817 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6818 | // Since this method is used to rewrite an existing transition tree, it can |
| 6819 | // always insert transitions without checking. |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6820 | MaybeObject* Map::CopyInstallDescriptors(int new_descriptor, |
| 6821 | DescriptorArray* descriptors) { |
| 6822 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6823 | |
| 6824 | Map* result; |
| 6825 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6826 | if (!maybe_result->To(&result)) return maybe_result; |
| 6827 | |
| 6828 | result->InitializeDescriptors(descriptors); |
| 6829 | result->SetNumberOfOwnDescriptors(new_descriptor + 1); |
| 6830 | |
| 6831 | int unused_property_fields = this->unused_property_fields(); |
| 6832 | if (descriptors->GetDetails(new_descriptor).type() == FIELD) { |
| 6833 | unused_property_fields = this->unused_property_fields() - 1; |
| 6834 | if (unused_property_fields < 0) { |
| 6835 | unused_property_fields += JSObject::kFieldsAdded; |
| 6836 | } |
| 6837 | } |
| 6838 | |
| 6839 | result->set_unused_property_fields(unused_property_fields); |
| 6840 | result->set_owns_descriptors(false); |
| 6841 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6842 | Name* name = descriptors->GetKey(new_descriptor); |
| 6843 | TransitionArray* transitions; |
| 6844 | MaybeObject* maybe_transitions = |
| 6845 | AddTransition(name, result, SIMPLE_TRANSITION); |
| 6846 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6847 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6848 | set_transitions(transitions); |
| 6849 | result->SetBackPointer(this); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6850 | |
| 6851 | return result; |
| 6852 | } |
| 6853 | |
| 6854 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6855 | MaybeObject* Map::CopyAsElementsKind(ElementsKind kind, TransitionFlag flag) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6856 | if (flag == INSERT_TRANSITION) { |
| 6857 | ASSERT(!HasElementsTransition() || |
| 6858 | ((elements_transition_map()->elements_kind() == DICTIONARY_ELEMENTS || |
| 6859 | IsExternalArrayElementsKind( |
| 6860 | elements_transition_map()->elements_kind())) && |
| 6861 | (kind == DICTIONARY_ELEMENTS || |
| 6862 | IsExternalArrayElementsKind(kind)))); |
| 6863 | ASSERT(!IsFastElementsKind(kind) || |
| 6864 | IsMoreGeneralElementsKindTransition(elements_kind(), kind)); |
| 6865 | ASSERT(kind != elements_kind()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6866 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6867 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6868 | bool insert_transition = |
| 6869 | flag == INSERT_TRANSITION && !HasElementsTransition(); |
| 6870 | |
| 6871 | if (insert_transition && owns_descriptors()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6872 | // In case the map owned its own descriptors, share the descriptors and |
| 6873 | // transfer ownership to the new map. |
| 6874 | Map* new_map; |
| 6875 | MaybeObject* maybe_new_map = CopyDropDescriptors(); |
| 6876 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 6877 | |
| 6878 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6879 | if (added_elements->IsFailure()) return added_elements; |
| 6880 | |
| 6881 | new_map->set_elements_kind(kind); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6882 | new_map->InitializeDescriptors(instance_descriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6883 | new_map->SetBackPointer(this); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6884 | set_owns_descriptors(false); |
| 6885 | return new_map; |
| 6886 | } |
| 6887 | |
| 6888 | // In case the map did not own its own descriptors, a split is forced by |
| 6889 | // copying the map; creating a new descriptor array cell. |
| 6890 | // Create a new free-floating map only if we are not allowed to store it. |
| 6891 | Map* new_map; |
| 6892 | MaybeObject* maybe_new_map = Copy(); |
| 6893 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6894 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6895 | new_map->set_elements_kind(kind); |
| 6896 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6897 | if (insert_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6898 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6899 | if (added_elements->IsFailure()) return added_elements; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6900 | new_map->SetBackPointer(this); |
| 6901 | } |
| 6902 | |
| 6903 | return new_map; |
| 6904 | } |
| 6905 | |
| 6906 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 6907 | Handle<Map> Map::CopyForObserved(Handle<Map> map) { |
| 6908 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6909 | map->CopyForObserved(), |
| 6910 | Map); |
| 6911 | } |
| 6912 | |
| 6913 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6914 | MaybeObject* Map::CopyForObserved() { |
| 6915 | ASSERT(!is_observed()); |
| 6916 | |
| 6917 | // In case the map owned its own descriptors, share the descriptors and |
| 6918 | // transfer ownership to the new map. |
| 6919 | Map* new_map; |
| 6920 | MaybeObject* maybe_new_map; |
| 6921 | if (owns_descriptors()) { |
| 6922 | maybe_new_map = CopyDropDescriptors(); |
| 6923 | } else { |
| 6924 | maybe_new_map = Copy(); |
| 6925 | } |
| 6926 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 6927 | |
| 6928 | TransitionArray* transitions; |
| 6929 | MaybeObject* maybe_transitions = AddTransition(GetHeap()->observed_symbol(), |
| 6930 | new_map, |
| 6931 | FULL_TRANSITION); |
| 6932 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6933 | set_transitions(transitions); |
| 6934 | |
| 6935 | new_map->set_is_observed(true); |
| 6936 | |
| 6937 | if (owns_descriptors()) { |
| 6938 | new_map->InitializeDescriptors(instance_descriptors()); |
| 6939 | set_owns_descriptors(false); |
| 6940 | } |
| 6941 | |
| 6942 | new_map->SetBackPointer(this); |
| 6943 | return new_map; |
| 6944 | } |
| 6945 | |
| 6946 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6947 | MaybeObject* Map::CopyWithPreallocatedFieldDescriptors() { |
| 6948 | if (pre_allocated_property_fields() == 0) return CopyDropDescriptors(); |
| 6949 | |
| 6950 | // If the map has pre-allocated properties always start out with a descriptor |
| 6951 | // array describing these properties. |
| 6952 | ASSERT(constructor()->IsJSFunction()); |
| 6953 | JSFunction* ctor = JSFunction::cast(constructor()); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6954 | Map* map = ctor->initial_map(); |
| 6955 | DescriptorArray* descriptors = map->instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6956 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6957 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 6958 | DescriptorArray* new_descriptors; |
| 6959 | MaybeObject* maybe_descriptors = |
| 6960 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 6961 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6962 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 6963 | return CopyReplaceDescriptors(new_descriptors, OMIT_TRANSITION); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
| 6966 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6967 | Handle<Map> Map::Copy(Handle<Map> map) { |
| 6968 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->Copy(), Map); |
| 6969 | } |
| 6970 | |
| 6971 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6972 | MaybeObject* Map::Copy() { |
| 6973 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6974 | DescriptorArray* new_descriptors; |
| 6975 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 6976 | MaybeObject* maybe_descriptors = |
| 6977 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 6978 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6979 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 6980 | return CopyReplaceDescriptors(new_descriptors, OMIT_TRANSITION); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6981 | } |
| 6982 | |
| 6983 | |
| 6984 | MaybeObject* Map::CopyAddDescriptor(Descriptor* descriptor, |
| 6985 | TransitionFlag flag) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6986 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6987 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6988 | // Ensure the key is unique. |
| 6989 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6990 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 6991 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6992 | int old_size = NumberOfOwnDescriptors(); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 6993 | int new_size = old_size + 1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6994 | |
| 6995 | if (flag == INSERT_TRANSITION && |
| 6996 | owns_descriptors() && |
| 6997 | CanHaveMoreTransitions()) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6998 | return ShareDescriptor(descriptors, descriptor); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6999 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7000 | |
| 7001 | DescriptorArray* new_descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7002 | MaybeObject* maybe_descriptors = |
| 7003 | DescriptorArray::Allocate(GetIsolate(), old_size, 1); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7004 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 7005 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 7006 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7007 | |
| 7008 | // Copy the descriptors, inserting a descriptor. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7009 | for (int i = 0; i < old_size; ++i) { |
| 7010 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7011 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7012 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7013 | if (old_size != descriptors->number_of_descriptors()) { |
| 7014 | new_descriptors->SetNumberOfDescriptors(new_size); |
| 7015 | new_descriptors->Set(old_size, descriptor, witness); |
| 7016 | new_descriptors->Sort(); |
| 7017 | } else { |
| 7018 | new_descriptors->Append(descriptor, witness); |
| 7019 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7020 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7021 | Name* key = descriptor->GetKey(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7022 | return CopyReplaceDescriptors(new_descriptors, flag, key, SIMPLE_TRANSITION); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | |
| 7026 | MaybeObject* Map::CopyInsertDescriptor(Descriptor* descriptor, |
| 7027 | TransitionFlag flag) { |
| 7028 | DescriptorArray* old_descriptors = instance_descriptors(); |
| 7029 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7030 | // Ensure the key is unique. |
| 7031 | MaybeObject* maybe_result = descriptor->KeyToUniqueName(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7032 | if (maybe_result->IsFailure()) return maybe_result; |
| 7033 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7034 | // We replace the key if it is already present. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7035 | int index = old_descriptors->SearchWithCache(descriptor->GetKey(), this); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7036 | if (index != DescriptorArray::kNotFound) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 7037 | return CopyReplaceDescriptor(old_descriptors, descriptor, index, flag); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7038 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7039 | return CopyAddDescriptor(descriptor, flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7043 | Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes( |
| 7044 | Handle<DescriptorArray> desc, |
| 7045 | int enumeration_index, |
| 7046 | PropertyAttributes attributes) { |
| 7047 | CALL_HEAP_FUNCTION(desc->GetIsolate(), |
| 7048 | desc->CopyUpToAddAttributes(enumeration_index, attributes), |
| 7049 | DescriptorArray); |
| 7050 | } |
| 7051 | |
| 7052 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7053 | MaybeObject* DescriptorArray::CopyUpToAddAttributes( |
| 7054 | int enumeration_index, PropertyAttributes attributes) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7055 | if (enumeration_index == 0) return GetHeap()->empty_descriptor_array(); |
| 7056 | |
| 7057 | int size = enumeration_index; |
| 7058 | |
| 7059 | DescriptorArray* descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7060 | MaybeObject* maybe_descriptors = Allocate(GetIsolate(), size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7061 | if (!maybe_descriptors->To(&descriptors)) return maybe_descriptors; |
| 7062 | DescriptorArray::WhitenessWitness witness(descriptors); |
| 7063 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7064 | if (attributes != NONE) { |
| 7065 | for (int i = 0; i < size; ++i) { |
| 7066 | Object* value = GetValue(i); |
| 7067 | PropertyDetails details = GetDetails(i); |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7068 | int mask = DONT_DELETE | DONT_ENUM; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7069 | // READ_ONLY is an invalid attribute for JS setters/getters. |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7070 | if (details.type() != CALLBACKS || !value->IsAccessorPair()) { |
| 7071 | mask |= READ_ONLY; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7072 | } |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7073 | details = details.CopyAddAttributes( |
| 7074 | static_cast<PropertyAttributes>(attributes & mask)); |
| 7075 | Descriptor desc(GetKey(i), value, details); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7076 | descriptors->Set(i, &desc, witness); |
| 7077 | } |
| 7078 | } else { |
| 7079 | for (int i = 0; i < size; ++i) { |
| 7080 | descriptors->CopyFrom(i, this, i, witness); |
| 7081 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | if (number_of_descriptors() != enumeration_index) descriptors->Sort(); |
| 7085 | |
| 7086 | return descriptors; |
| 7087 | } |
| 7088 | |
| 7089 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 7090 | MaybeObject* Map::CopyReplaceDescriptor(DescriptorArray* descriptors, |
| 7091 | Descriptor* descriptor, |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7092 | int insertion_index, |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7093 | TransitionFlag flag) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7094 | // Ensure the key is unique. |
| 7095 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7096 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 7097 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7098 | Name* key = descriptor->GetKey(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7099 | ASSERT(key == descriptors->GetKey(insertion_index)); |
| 7100 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7101 | int new_size = NumberOfOwnDescriptors(); |
| 7102 | ASSERT(0 <= insertion_index && insertion_index < new_size); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 7103 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7104 | ASSERT_LT(insertion_index, new_size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7105 | |
| 7106 | DescriptorArray* new_descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7107 | MaybeObject* maybe_descriptors = |
| 7108 | DescriptorArray::Allocate(GetIsolate(), new_size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7109 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 7110 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7111 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7112 | for (int i = 0; i < new_size; ++i) { |
| 7113 | if (i == insertion_index) { |
| 7114 | new_descriptors->Set(i, descriptor, witness); |
| 7115 | } else { |
| 7116 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 7117 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7118 | } |
| 7119 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7120 | // Re-sort if descriptors were removed. |
| 7121 | if (new_size != descriptors->length()) new_descriptors->Sort(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7122 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7123 | SimpleTransitionFlag simple_flag = |
| 7124 | (insertion_index == descriptors->number_of_descriptors() - 1) |
| 7125 | ? SIMPLE_TRANSITION |
| 7126 | : FULL_TRANSITION; |
| 7127 | return CopyReplaceDescriptors(new_descriptors, flag, key, simple_flag); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7130 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7131 | void Map::UpdateCodeCache(Handle<Map> map, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7132 | Handle<Name> name, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7133 | Handle<Code> code) { |
| 7134 | Isolate* isolate = map->GetIsolate(); |
| 7135 | CALL_HEAP_FUNCTION_VOID(isolate, |
| 7136 | map->UpdateCodeCache(*name, *code)); |
| 7137 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7138 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7139 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7140 | MaybeObject* Map::UpdateCodeCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7141 | // Allocate the code cache if not present. |
| 7142 | if (code_cache()->IsFixedArray()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7143 | Object* result; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7144 | { MaybeObject* maybe_result = GetHeap()->AllocateCodeCache(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7145 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7146 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7147 | set_code_cache(result); |
| 7148 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7149 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7150 | // Update the code cache. |
| 7151 | return CodeCache::cast(code_cache())->Update(name, code); |
| 7152 | } |
| 7153 | |
| 7154 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7155 | Object* Map::FindInCodeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7156 | // Do a lookup if a code cache exists. |
| 7157 | if (!code_cache()->IsFixedArray()) { |
| 7158 | return CodeCache::cast(code_cache())->Lookup(name, flags); |
| 7159 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7160 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7161 | } |
| 7162 | } |
| 7163 | |
| 7164 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7165 | int Map::IndexInCodeCache(Object* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7166 | // Get the internal index if a code cache exists. |
| 7167 | if (!code_cache()->IsFixedArray()) { |
| 7168 | return CodeCache::cast(code_cache())->GetIndex(name, code); |
| 7169 | } |
| 7170 | return -1; |
| 7171 | } |
| 7172 | |
| 7173 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7174 | void Map::RemoveFromCodeCache(Name* name, Code* code, int index) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7175 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 7176 | // RemoveFromCodeCache so the code cache must be there. |
| 7177 | ASSERT(!code_cache()->IsFixedArray()); |
| 7178 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 7179 | } |
| 7180 | |
| 7181 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7182 | // An iterator over all map transitions in an descriptor array, reusing the map |
| 7183 | // field of the contens array while it is running. |
| 7184 | class IntrusiveMapTransitionIterator { |
| 7185 | public: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7186 | explicit IntrusiveMapTransitionIterator(TransitionArray* transition_array) |
| 7187 | : transition_array_(transition_array) { } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 7188 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7189 | void Start() { |
| 7190 | ASSERT(!IsIterating()); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7191 | *TransitionArrayHeader() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7192 | } |
| 7193 | |
| 7194 | bool IsIterating() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7195 | return (*TransitionArrayHeader())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | Map* Next() { |
| 7199 | ASSERT(IsIterating()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7200 | int index = Smi::cast(*TransitionArrayHeader())->value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7201 | int number_of_transitions = transition_array_->number_of_transitions(); |
| 7202 | while (index < number_of_transitions) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7203 | *TransitionArrayHeader() = Smi::FromInt(index + 1); |
| 7204 | return transition_array_->GetTarget(index); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7205 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7206 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7207 | *TransitionArrayHeader() = transition_array_->GetHeap()->fixed_array_map(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7208 | return NULL; |
| 7209 | } |
| 7210 | |
| 7211 | private: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7212 | Object** TransitionArrayHeader() { |
| 7213 | return HeapObject::RawField(transition_array_, TransitionArray::kMapOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7216 | TransitionArray* transition_array_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7217 | }; |
| 7218 | |
| 7219 | |
| 7220 | // An iterator over all prototype transitions, reusing the map field of the |
| 7221 | // underlying array while it is running. |
| 7222 | class IntrusivePrototypeTransitionIterator { |
| 7223 | public: |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7224 | explicit IntrusivePrototypeTransitionIterator(HeapObject* proto_trans) |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7225 | : proto_trans_(proto_trans) { } |
| 7226 | |
| 7227 | void Start() { |
| 7228 | ASSERT(!IsIterating()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7229 | *Header() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7230 | } |
| 7231 | |
| 7232 | bool IsIterating() { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7233 | return (*Header())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | Map* Next() { |
| 7237 | ASSERT(IsIterating()); |
| 7238 | int transitionNumber = Smi::cast(*Header())->value(); |
| 7239 | if (transitionNumber < NumberOfTransitions()) { |
| 7240 | *Header() = Smi::FromInt(transitionNumber + 1); |
| 7241 | return GetTransition(transitionNumber); |
| 7242 | } |
| 7243 | *Header() = proto_trans_->GetHeap()->fixed_array_map(); |
| 7244 | return NULL; |
| 7245 | } |
| 7246 | |
| 7247 | private: |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7248 | Object** Header() { |
| 7249 | return HeapObject::RawField(proto_trans_, FixedArray::kMapOffset); |
| 7250 | } |
| 7251 | |
| 7252 | int NumberOfTransitions() { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7253 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 7254 | Object* num = proto_trans->get(Map::kProtoTransitionNumberOfEntriesOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7255 | return Smi::cast(num)->value(); |
| 7256 | } |
| 7257 | |
| 7258 | Map* GetTransition(int transitionNumber) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7259 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 7260 | return Map::cast(proto_trans->get(IndexFor(transitionNumber))); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | int IndexFor(int transitionNumber) { |
| 7264 | return Map::kProtoTransitionHeaderSize + |
| 7265 | Map::kProtoTransitionMapOffset + |
| 7266 | transitionNumber * Map::kProtoTransitionElementsPerEntry; |
| 7267 | } |
| 7268 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7269 | HeapObject* proto_trans_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7270 | }; |
| 7271 | |
| 7272 | |
| 7273 | // To traverse the transition tree iteratively, we have to store two kinds of |
| 7274 | // information in a map: The parent map in the traversal and which children of a |
| 7275 | // node have already been visited. To do this without additional memory, we |
| 7276 | // temporarily reuse two maps with known values: |
| 7277 | // |
| 7278 | // (1) The map of the map temporarily holds the parent, and is restored to the |
| 7279 | // meta map afterwards. |
| 7280 | // |
| 7281 | // (2) The info which children have already been visited depends on which part |
| 7282 | // of the map we currently iterate: |
| 7283 | // |
| 7284 | // (a) If we currently follow normal map transitions, we temporarily store |
| 7285 | // the current index in the map of the FixedArray of the desciptor |
| 7286 | // array's contents, and restore it to the fixed array map afterwards. |
| 7287 | // Note that a single descriptor can have 0, 1, or 2 transitions. |
| 7288 | // |
| 7289 | // (b) If we currently follow prototype transitions, we temporarily store |
| 7290 | // the current index in the map of the FixedArray holding the prototype |
| 7291 | // transitions, and restore it to the fixed array map afterwards. |
| 7292 | // |
| 7293 | // Note that the child iterator is just a concatenation of two iterators: One |
| 7294 | // iterating over map transitions and one iterating over prototype transisitons. |
| 7295 | class TraversableMap : public Map { |
| 7296 | public: |
| 7297 | // Record the parent in the traversal within this map. Note that this destroys |
| 7298 | // this map's map! |
| 7299 | void SetParent(TraversableMap* parent) { set_map_no_write_barrier(parent); } |
| 7300 | |
| 7301 | // Reset the current map's map, returning the parent previously stored in it. |
| 7302 | TraversableMap* GetAndResetParent() { |
| 7303 | TraversableMap* old_parent = static_cast<TraversableMap*>(map()); |
| 7304 | set_map_no_write_barrier(GetHeap()->meta_map()); |
| 7305 | return old_parent; |
| 7306 | } |
| 7307 | |
| 7308 | // Start iterating over this map's children, possibly destroying a FixedArray |
| 7309 | // map (see explanation above). |
| 7310 | void ChildIteratorStart() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7311 | if (HasTransitionArray()) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7312 | if (HasPrototypeTransitions()) { |
| 7313 | IntrusivePrototypeTransitionIterator(GetPrototypeTransitions()).Start(); |
| 7314 | } |
| 7315 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7316 | IntrusiveMapTransitionIterator(transitions()).Start(); |
| 7317 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7318 | } |
| 7319 | |
| 7320 | // If we have an unvisited child map, return that one and advance. If we have |
| 7321 | // none, return NULL and reset any destroyed FixedArray maps. |
| 7322 | TraversableMap* ChildIteratorNext() { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7323 | TransitionArray* transition_array = unchecked_transition_array(); |
| 7324 | if (!transition_array->map()->IsSmi() && |
| 7325 | !transition_array->IsTransitionArray()) { |
| 7326 | return NULL; |
| 7327 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7328 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7329 | if (transition_array->HasPrototypeTransitions()) { |
| 7330 | HeapObject* proto_transitions = |
| 7331 | transition_array->UncheckedPrototypeTransitions(); |
| 7332 | IntrusivePrototypeTransitionIterator proto_iterator(proto_transitions); |
| 7333 | if (proto_iterator.IsIterating()) { |
| 7334 | Map* next = proto_iterator.Next(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7335 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 7336 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 7337 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7338 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7339 | IntrusiveMapTransitionIterator transition_iterator(transition_array); |
| 7340 | if (transition_iterator.IsIterating()) { |
| 7341 | Map* next = transition_iterator.Next(); |
| 7342 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 7343 | } |
| 7344 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7345 | return NULL; |
| 7346 | } |
| 7347 | }; |
| 7348 | |
| 7349 | |
| 7350 | // Traverse the transition tree in postorder without using the C++ stack by |
| 7351 | // doing pointer reversal. |
| 7352 | void Map::TraverseTransitionTree(TraverseCallback callback, void* data) { |
| 7353 | TraversableMap* current = static_cast<TraversableMap*>(this); |
| 7354 | current->ChildIteratorStart(); |
| 7355 | while (true) { |
| 7356 | TraversableMap* child = current->ChildIteratorNext(); |
| 7357 | if (child != NULL) { |
| 7358 | child->ChildIteratorStart(); |
| 7359 | child->SetParent(current); |
| 7360 | current = child; |
| 7361 | } else { |
| 7362 | TraversableMap* parent = current->GetAndResetParent(); |
| 7363 | callback(current, data); |
| 7364 | if (current == this) break; |
| 7365 | current = parent; |
| 7366 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7367 | } |
| 7368 | } |
| 7369 | |
| 7370 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7371 | MaybeObject* CodeCache::Update(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7372 | // The number of monomorphic stubs for normal load/store/call IC's can grow to |
| 7373 | // a large number and therefore they need to go into a hash table. They are |
| 7374 | // used to load global properties from cells. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7375 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7376 | // Make sure that a hash table is allocated for the normal load code cache. |
| 7377 | if (normal_type_cache()->IsUndefined()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7378 | Object* result; |
| 7379 | { MaybeObject* maybe_result = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7380 | CodeCacheHashTable::Allocate(GetHeap(), |
| 7381 | CodeCacheHashTable::kInitialSize); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7382 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7383 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7384 | set_normal_type_cache(result); |
| 7385 | } |
| 7386 | return UpdateNormalTypeCache(name, code); |
| 7387 | } else { |
| 7388 | ASSERT(default_cache()->IsFixedArray()); |
| 7389 | return UpdateDefaultCache(name, code); |
| 7390 | } |
| 7391 | } |
| 7392 | |
| 7393 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7394 | MaybeObject* CodeCache::UpdateDefaultCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7395 | // 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] | 7396 | // flags. This allows call constant stubs to overwrite call field |
| 7397 | // stubs, etc. |
| 7398 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 7399 | |
| 7400 | // First check whether we can update existing code cache without |
| 7401 | // extending it. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7402 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7403 | int length = cache->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7404 | int deleted_index = -1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7405 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7406 | Object* key = cache->get(i); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7407 | if (key->IsNull()) { |
| 7408 | if (deleted_index < 0) deleted_index = i; |
| 7409 | continue; |
| 7410 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7411 | if (key->IsUndefined()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7412 | if (deleted_index >= 0) i = deleted_index; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7413 | cache->set(i + kCodeCacheEntryNameOffset, name); |
| 7414 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7415 | return this; |
| 7416 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7417 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7418 | Code::Flags found = |
| 7419 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7420 | if (Code::RemoveTypeFromFlags(found) == flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7421 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7422 | return this; |
| 7423 | } |
| 7424 | } |
| 7425 | } |
| 7426 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7427 | // Reached the end of the code cache. If there were deleted |
| 7428 | // elements, reuse the space for the first of them. |
| 7429 | if (deleted_index >= 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7430 | cache->set(deleted_index + kCodeCacheEntryNameOffset, name); |
| 7431 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, code); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7432 | return this; |
| 7433 | } |
| 7434 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7435 | // Extend the code cache with some new entries (at least one). Must be a |
| 7436 | // multiple of the entry size. |
| 7437 | int new_length = length + ((length >> 1)) + kCodeCacheEntrySize; |
| 7438 | new_length = new_length - new_length % kCodeCacheEntrySize; |
| 7439 | ASSERT((new_length % kCodeCacheEntrySize) == 0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7440 | Object* result; |
| 7441 | { MaybeObject* maybe_result = cache->CopySize(new_length); |
| 7442 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7443 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7444 | |
| 7445 | // Add the (name, code) pair to the new cache. |
| 7446 | cache = FixedArray::cast(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7447 | cache->set(length + kCodeCacheEntryNameOffset, name); |
| 7448 | cache->set(length + kCodeCacheEntryCodeOffset, code); |
| 7449 | set_default_cache(cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7450 | return this; |
| 7451 | } |
| 7452 | |
| 7453 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7454 | MaybeObject* CodeCache::UpdateNormalTypeCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7455 | // Adding a new entry can cause a new cache to be allocated. |
| 7456 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7457 | Object* new_cache; |
| 7458 | { MaybeObject* maybe_new_cache = cache->Put(name, code); |
| 7459 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 7460 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7461 | set_normal_type_cache(new_cache); |
| 7462 | return this; |
| 7463 | } |
| 7464 | |
| 7465 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7466 | Object* CodeCache::Lookup(Name* name, Code::Flags flags) { |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7467 | flags = Code::RemoveTypeFromFlags(flags); |
| 7468 | Object* result = LookupDefaultCache(name, flags); |
| 7469 | if (result->IsCode()) return result; |
| 7470 | return LookupNormalTypeCache(name, flags); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
| 7473 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7474 | Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7475 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7476 | int length = cache->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7477 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 7478 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7479 | // Skip deleted elements. |
| 7480 | if (key->IsNull()) continue; |
| 7481 | if (key->IsUndefined()) return key; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7482 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7483 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7484 | if (Code::RemoveTypeFromFlags(code->flags()) == flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7485 | return code; |
| 7486 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7487 | } |
| 7488 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7489 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7490 | } |
| 7491 | |
| 7492 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7493 | Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7494 | if (!normal_type_cache()->IsUndefined()) { |
| 7495 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 7496 | return cache->Lookup(name, flags); |
| 7497 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7498 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7503 | int CodeCache::GetIndex(Object* name, Code* code) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7504 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7505 | if (normal_type_cache()->IsUndefined()) return -1; |
| 7506 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7507 | return cache->GetIndex(Name::cast(name), code->flags()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | FixedArray* array = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7511 | int len = array->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7512 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 7513 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7514 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 7515 | return -1; |
| 7516 | } |
| 7517 | |
| 7518 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7519 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7520 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7521 | ASSERT(!normal_type_cache()->IsUndefined()); |
| 7522 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7523 | ASSERT(cache->GetIndex(Name::cast(name), code->flags()) == index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7524 | cache->RemoveByIndex(index); |
| 7525 | } else { |
| 7526 | FixedArray* array = default_cache(); |
| 7527 | ASSERT(array->length() >= index && array->get(index)->IsCode()); |
| 7528 | // Use null instead of undefined for deleted elements to distinguish |
| 7529 | // deleted elements from unused elements. This distinction is used |
| 7530 | // when looking up in the cache and when updating the cache. |
| 7531 | ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
| 7532 | array->set_null(index - 1); // Name. |
| 7533 | array->set_null(index); // Code. |
| 7534 | } |
| 7535 | } |
| 7536 | |
| 7537 | |
| 7538 | // The key in the code cache hash table consists of the property name and the |
| 7539 | // code object. The actual match is on the name and the code flags. If a key |
| 7540 | // is created using the flags and not a code object it can only be used for |
| 7541 | // lookup not to create a new entry. |
| 7542 | class CodeCacheHashTableKey : public HashTableKey { |
| 7543 | public: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7544 | CodeCacheHashTableKey(Name* name, Code::Flags flags) |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7545 | : name_(name), flags_(flags), code_(NULL) { } |
| 7546 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7547 | CodeCacheHashTableKey(Name* name, Code* code) |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7548 | : name_(name), flags_(code->flags()), code_(code) { } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7549 | |
| 7550 | |
| 7551 | bool IsMatch(Object* other) { |
| 7552 | if (!other->IsFixedArray()) return false; |
| 7553 | FixedArray* pair = FixedArray::cast(other); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7554 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7555 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 7556 | if (flags != flags_) { |
| 7557 | return false; |
| 7558 | } |
| 7559 | return name_->Equals(name); |
| 7560 | } |
| 7561 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7562 | static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7563 | return name->Hash() ^ flags; |
| 7564 | } |
| 7565 | |
| 7566 | uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); } |
| 7567 | |
| 7568 | uint32_t HashForObject(Object* obj) { |
| 7569 | FixedArray* pair = FixedArray::cast(obj); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7570 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7571 | Code* code = Code::cast(pair->get(1)); |
| 7572 | return NameFlagsHashHelper(name, code->flags()); |
| 7573 | } |
| 7574 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7575 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7576 | ASSERT(code_ != NULL); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7577 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7578 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7579 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7580 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7581 | FixedArray* pair = FixedArray::cast(obj); |
| 7582 | pair->set(0, name_); |
| 7583 | pair->set(1, code_); |
| 7584 | return pair; |
| 7585 | } |
| 7586 | |
| 7587 | private: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7588 | Name* name_; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7589 | Code::Flags flags_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7590 | // TODO(jkummerow): We should be able to get by without this. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7591 | Code* code_; |
| 7592 | }; |
| 7593 | |
| 7594 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7595 | Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7596 | CodeCacheHashTableKey key(name, flags); |
| 7597 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7598 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7599 | return get(EntryToIndex(entry) + 1); |
| 7600 | } |
| 7601 | |
| 7602 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7603 | MaybeObject* CodeCacheHashTable::Put(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7604 | CodeCacheHashTableKey key(name, code); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7605 | Object* obj; |
| 7606 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7607 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7608 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7609 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7610 | // Don't use |this|, as the table might have grown. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7611 | CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj); |
| 7612 | |
| 7613 | int entry = cache->FindInsertionEntry(key.Hash()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7614 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7615 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7616 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 7617 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7618 | |
| 7619 | cache->set(EntryToIndex(entry), k); |
| 7620 | cache->set(EntryToIndex(entry) + 1, code); |
| 7621 | cache->ElementAdded(); |
| 7622 | return cache; |
| 7623 | } |
| 7624 | |
| 7625 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7626 | int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7627 | CodeCacheHashTableKey key(name, flags); |
| 7628 | int entry = FindEntry(&key); |
| 7629 | return (entry == kNotFound) ? -1 : entry; |
| 7630 | } |
| 7631 | |
| 7632 | |
| 7633 | void CodeCacheHashTable::RemoveByIndex(int index) { |
| 7634 | ASSERT(index >= 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7635 | Heap* heap = GetHeap(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7636 | set(EntryToIndex(index), heap->the_hole_value()); |
| 7637 | set(EntryToIndex(index) + 1, heap->the_hole_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7638 | ElementRemoved(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7642 | void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> cache, |
| 7643 | MapHandleList* maps, |
| 7644 | Code::Flags flags, |
| 7645 | Handle<Code> code) { |
| 7646 | Isolate* isolate = cache->GetIsolate(); |
| 7647 | CALL_HEAP_FUNCTION_VOID(isolate, cache->Update(maps, flags, *code)); |
| 7648 | } |
| 7649 | |
| 7650 | |
| 7651 | MaybeObject* PolymorphicCodeCache::Update(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7652 | Code::Flags flags, |
| 7653 | Code* code) { |
| 7654 | // Initialize cache if necessary. |
| 7655 | if (cache()->IsUndefined()) { |
| 7656 | Object* result; |
| 7657 | { MaybeObject* maybe_result = |
| 7658 | PolymorphicCodeCacheHashTable::Allocate( |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7659 | GetHeap(), |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7660 | PolymorphicCodeCacheHashTable::kInitialSize); |
| 7661 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7662 | } |
| 7663 | set_cache(result); |
| 7664 | } else { |
| 7665 | // This entry shouldn't be contained in the cache yet. |
| 7666 | ASSERT(PolymorphicCodeCacheHashTable::cast(cache()) |
| 7667 | ->Lookup(maps, flags)->IsUndefined()); |
| 7668 | } |
| 7669 | PolymorphicCodeCacheHashTable* hash_table = |
| 7670 | PolymorphicCodeCacheHashTable::cast(cache()); |
| 7671 | Object* new_cache; |
| 7672 | { MaybeObject* maybe_new_cache = hash_table->Put(maps, flags, code); |
| 7673 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 7674 | } |
| 7675 | set_cache(new_cache); |
| 7676 | return this; |
| 7677 | } |
| 7678 | |
| 7679 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7680 | Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps, |
| 7681 | Code::Flags flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7682 | if (!cache()->IsUndefined()) { |
| 7683 | PolymorphicCodeCacheHashTable* hash_table = |
| 7684 | PolymorphicCodeCacheHashTable::cast(cache()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 7685 | return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7686 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7687 | return GetIsolate()->factory()->undefined_value(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7688 | } |
| 7689 | } |
| 7690 | |
| 7691 | |
| 7692 | // Despite their name, object of this class are not stored in the actual |
| 7693 | // hash table; instead they're temporarily used for lookups. It is therefore |
| 7694 | // safe to have a weak (non-owning) pointer to a MapList as a member field. |
| 7695 | class PolymorphicCodeCacheHashTableKey : public HashTableKey { |
| 7696 | public: |
| 7697 | // Callers must ensure that |maps| outlives the newly constructed object. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7698 | PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags) |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7699 | : maps_(maps), |
| 7700 | code_flags_(code_flags) {} |
| 7701 | |
| 7702 | bool IsMatch(Object* other) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7703 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7704 | int other_flags; |
| 7705 | FromObject(other, &other_flags, &other_maps); |
| 7706 | if (code_flags_ != other_flags) return false; |
| 7707 | if (maps_->length() != other_maps.length()) return false; |
| 7708 | // Compare just the hashes first because it's faster. |
| 7709 | int this_hash = MapsHashHelper(maps_, code_flags_); |
| 7710 | int other_hash = MapsHashHelper(&other_maps, other_flags); |
| 7711 | if (this_hash != other_hash) return false; |
| 7712 | |
| 7713 | // Full comparison: for each map in maps_, look for an equivalent map in |
| 7714 | // other_maps. This implementation is slow, but probably good enough for |
| 7715 | // now because the lists are short (<= 4 elements currently). |
| 7716 | for (int i = 0; i < maps_->length(); ++i) { |
| 7717 | bool match_found = false; |
| 7718 | for (int j = 0; j < other_maps.length(); ++j) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7719 | if (*(maps_->at(i)) == *(other_maps.at(j))) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7720 | match_found = true; |
| 7721 | break; |
| 7722 | } |
| 7723 | } |
| 7724 | if (!match_found) return false; |
| 7725 | } |
| 7726 | return true; |
| 7727 | } |
| 7728 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7729 | static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7730 | uint32_t hash = code_flags; |
| 7731 | for (int i = 0; i < maps->length(); ++i) { |
| 7732 | hash ^= maps->at(i)->Hash(); |
| 7733 | } |
| 7734 | return hash; |
| 7735 | } |
| 7736 | |
| 7737 | uint32_t Hash() { |
| 7738 | return MapsHashHelper(maps_, code_flags_); |
| 7739 | } |
| 7740 | |
| 7741 | uint32_t HashForObject(Object* obj) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7742 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7743 | int other_flags; |
| 7744 | FromObject(obj, &other_flags, &other_maps); |
| 7745 | return MapsHashHelper(&other_maps, other_flags); |
| 7746 | } |
| 7747 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7748 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7749 | Object* obj; |
| 7750 | // The maps in |maps_| must be copied to a newly allocated FixedArray, |
| 7751 | // both because the referenced MapList is short-lived, and because C++ |
| 7752 | // objects can't be stored in the heap anyway. |
| 7753 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7754 | heap->AllocateUninitializedFixedArray(maps_->length() + 1); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7755 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7756 | } |
| 7757 | FixedArray* list = FixedArray::cast(obj); |
| 7758 | list->set(0, Smi::FromInt(code_flags_)); |
| 7759 | for (int i = 0; i < maps_->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7760 | list->set(i + 1, *maps_->at(i)); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7761 | } |
| 7762 | return list; |
| 7763 | } |
| 7764 | |
| 7765 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7766 | static MapHandleList* FromObject(Object* obj, |
| 7767 | int* code_flags, |
| 7768 | MapHandleList* maps) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7769 | FixedArray* list = FixedArray::cast(obj); |
| 7770 | maps->Rewind(0); |
| 7771 | *code_flags = Smi::cast(list->get(0))->value(); |
| 7772 | for (int i = 1; i < list->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7773 | maps->Add(Handle<Map>(Map::cast(list->get(i)))); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7774 | } |
| 7775 | return maps; |
| 7776 | } |
| 7777 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7778 | MapHandleList* maps_; // weak. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7779 | int code_flags_; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7780 | static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7781 | }; |
| 7782 | |
| 7783 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7784 | Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps, |
| 7785 | int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7786 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7787 | int entry = FindEntry(&key); |
| 7788 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
| 7789 | return get(EntryToIndex(entry) + 1); |
| 7790 | } |
| 7791 | |
| 7792 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7793 | MaybeObject* PolymorphicCodeCacheHashTable::Put(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7794 | int code_flags, |
| 7795 | Code* code) { |
| 7796 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7797 | Object* obj; |
| 7798 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7799 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7800 | } |
| 7801 | PolymorphicCodeCacheHashTable* cache = |
| 7802 | reinterpret_cast<PolymorphicCodeCacheHashTable*>(obj); |
| 7803 | int entry = cache->FindInsertionEntry(key.Hash()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7804 | { MaybeObject* maybe_obj = key.AsObject(GetHeap()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7805 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7806 | } |
| 7807 | cache->set(EntryToIndex(entry), obj); |
| 7808 | cache->set(EntryToIndex(entry) + 1, code); |
| 7809 | cache->ElementAdded(); |
| 7810 | return cache; |
| 7811 | } |
| 7812 | |
| 7813 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7814 | MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7815 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 7816 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7817 | accessor->AddElementsToFixedArray(array, array, this); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7818 | FixedArray* result; |
| 7819 | if (!maybe_result->To<FixedArray>(&result)) return maybe_result; |
| 7820 | #ifdef DEBUG |
| 7821 | if (FLAG_enable_slow_asserts) { |
| 7822 | for (int i = 0; i < result->length(); i++) { |
| 7823 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7824 | ASSERT(current->IsNumber() || current->IsName()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7825 | } |
| 7826 | } |
| 7827 | #endif |
| 7828 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7829 | } |
| 7830 | |
| 7831 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7832 | MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7833 | ElementsAccessor* accessor = ElementsAccessor::ForArray(other); |
| 7834 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7835 | accessor->AddElementsToFixedArray(NULL, NULL, this, other); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7836 | FixedArray* result; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 7837 | if (!maybe_result->To(&result)) return maybe_result; |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7838 | #ifdef DEBUG |
| 7839 | if (FLAG_enable_slow_asserts) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7840 | for (int i = 0; i < result->length(); i++) { |
| 7841 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7842 | ASSERT(current->IsNumber() || current->IsName()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7843 | } |
| 7844 | } |
| 7845 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7846 | return result; |
| 7847 | } |
| 7848 | |
| 7849 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 7850 | MaybeObject* FixedArray::CopySize(int new_length, PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7851 | Heap* heap = GetHeap(); |
| 7852 | if (new_length == 0) return heap->empty_fixed_array(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7853 | Object* obj; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 7854 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length, pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7855 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7856 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7857 | FixedArray* result = FixedArray::cast(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7858 | // Copy the content |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 7859 | DisallowHeapAllocation no_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7860 | int len = length(); |
| 7861 | if (new_length < len) len = new_length; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7862 | // We are taking the map from the old fixed array so the map is sure to |
| 7863 | // be an immortal immutable object. |
| 7864 | result->set_map_no_write_barrier(map()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7865 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7866 | for (int i = 0; i < len; i++) { |
| 7867 | result->set(i, get(i), mode); |
| 7868 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7869 | return result; |
| 7870 | } |
| 7871 | |
| 7872 | |
| 7873 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 7874 | DisallowHeapAllocation no_gc; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7875 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7876 | for (int index = 0; index < len; index++) { |
| 7877 | dest->set(dest_pos+index, get(pos+index), mode); |
| 7878 | } |
| 7879 | } |
| 7880 | |
| 7881 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7882 | #ifdef DEBUG |
| 7883 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 7884 | if (length() != other->length()) return false; |
| 7885 | for (int i = 0 ; i < length(); ++i) { |
| 7886 | if (get(i) != other->get(i)) return false; |
| 7887 | } |
| 7888 | return true; |
| 7889 | } |
| 7890 | #endif |
| 7891 | |
| 7892 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7893 | MaybeObject* DescriptorArray::Allocate(Isolate* isolate, |
| 7894 | int number_of_descriptors, |
| 7895 | int slack) { |
| 7896 | Heap* heap = isolate->heap(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7897 | // Do not use DescriptorArray::cast on incomplete object. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7898 | int size = number_of_descriptors + slack; |
| 7899 | if (size == 0) return heap->empty_descriptor_array(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7900 | FixedArray* result; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7901 | // Allocate the array of keys. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7902 | MaybeObject* maybe_array = heap->AllocateFixedArray(LengthFor(size)); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 7903 | if (!maybe_array->To(&result)) return maybe_array; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7904 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7905 | result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7906 | result->set(kEnumCacheIndex, Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7907 | return result; |
| 7908 | } |
| 7909 | |
| 7910 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7911 | void DescriptorArray::ClearEnumCache() { |
| 7912 | set(kEnumCacheIndex, Smi::FromInt(0)); |
| 7913 | } |
| 7914 | |
| 7915 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7916 | void DescriptorArray::SetEnumCache(FixedArray* bridge_storage, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7917 | FixedArray* new_cache, |
| 7918 | Object* new_index_cache) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7919 | ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7920 | ASSERT(new_index_cache->IsSmi() || new_index_cache->IsFixedArray()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 7921 | ASSERT(!IsEmpty()); |
| 7922 | ASSERT(!HasEnumCache() || new_cache->length() > GetEnumCache()->length()); |
| 7923 | FixedArray::cast(bridge_storage)-> |
| 7924 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 7925 | FixedArray::cast(bridge_storage)-> |
| 7926 | set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache); |
| 7927 | set(kEnumCacheIndex, bridge_storage); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7928 | } |
| 7929 | |
| 7930 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7931 | void DescriptorArray::CopyFrom(int dst_index, |
| 7932 | DescriptorArray* src, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7933 | int src_index, |
| 7934 | const WhitenessWitness& witness) { |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7935 | Object* value = src->GetValue(src_index); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 7936 | PropertyDetails details = src->GetDetails(src_index); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7937 | Descriptor desc(src->GetKey(src_index), value, details); |
| 7938 | Set(dst_index, &desc, witness); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7941 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7942 | Handle<DescriptorArray> DescriptorArray::Merge(Handle<DescriptorArray> desc, |
| 7943 | int verbatim, |
| 7944 | int valid, |
| 7945 | int new_size, |
| 7946 | int modify_index, |
| 7947 | StoreMode store_mode, |
| 7948 | Handle<DescriptorArray> other) { |
| 7949 | CALL_HEAP_FUNCTION(desc->GetIsolate(), |
| 7950 | desc->Merge(verbatim, valid, new_size, modify_index, |
| 7951 | store_mode, *other), |
| 7952 | DescriptorArray); |
| 7953 | } |
| 7954 | |
| 7955 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7956 | // 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] | 7957 | // partly) updated |this| descriptor array. |
| 7958 | // The method merges two descriptor array in three parts. Both descriptor arrays |
| 7959 | // are identical up to |verbatim|. They also overlap in keys up to |valid|. |
| 7960 | // Between |verbatim| and |valid|, the resulting descriptor type as well as the |
| 7961 | // representation are generalized from both |this| and |other|. Beyond |valid|, |
| 7962 | // the descriptors are copied verbatim from |other| up to |new_size|. |
| 7963 | // In case of incompatible types, the type and representation of |other| is |
| 7964 | // used. |
| 7965 | MaybeObject* DescriptorArray::Merge(int verbatim, |
| 7966 | int valid, |
| 7967 | int new_size, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 7968 | int modify_index, |
| 7969 | StoreMode store_mode, |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7970 | DescriptorArray* other) { |
| 7971 | ASSERT(verbatim <= valid); |
| 7972 | ASSERT(valid <= new_size); |
| 7973 | |
| 7974 | DescriptorArray* result; |
| 7975 | // Allocate a new descriptor array large enough to hold the required |
| 7976 | // descriptors, with minimally the exact same size as this descriptor array. |
| 7977 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7978 | GetIsolate(), new_size, |
| 7979 | Max(new_size, other->number_of_descriptors()) - new_size); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7980 | if (!maybe_descriptors->To(&result)) return maybe_descriptors; |
| 7981 | ASSERT(result->length() > length() || |
| 7982 | result->NumberOfSlackDescriptors() > 0 || |
| 7983 | result->number_of_descriptors() == other->number_of_descriptors()); |
| 7984 | ASSERT(result->number_of_descriptors() == new_size); |
| 7985 | |
| 7986 | DescriptorArray::WhitenessWitness witness(result); |
| 7987 | |
| 7988 | int descriptor; |
| 7989 | |
| 7990 | // 0 -> |verbatim| |
| 7991 | int current_offset = 0; |
| 7992 | for (descriptor = 0; descriptor < verbatim; descriptor++) { |
| 7993 | if (GetDetails(descriptor).type() == FIELD) current_offset++; |
hpayer@chromium.org | 2311a91 | 2013-08-28 13:39:38 +0000 | [diff] [blame] | 7994 | result->CopyFrom(descriptor, other, descriptor, witness); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
| 7997 | // |verbatim| -> |valid| |
| 7998 | for (; descriptor < valid; descriptor++) { |
| 7999 | Name* key = GetKey(descriptor); |
| 8000 | PropertyDetails details = GetDetails(descriptor); |
| 8001 | PropertyDetails other_details = other->GetDetails(descriptor); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8002 | |
| 8003 | if (details.type() == FIELD || other_details.type() == FIELD || |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 8004 | (store_mode == FORCE_FIELD && descriptor == modify_index) || |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 8005 | (details.type() == CONSTANT && |
| 8006 | other_details.type() == CONSTANT && |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8007 | GetValue(descriptor) != other->GetValue(descriptor))) { |
| 8008 | Representation representation = |
| 8009 | details.representation().generalize(other_details.representation()); |
| 8010 | FieldDescriptor d(key, |
| 8011 | current_offset++, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8012 | other_details.attributes(), |
| 8013 | representation); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8014 | result->Set(descriptor, &d, witness); |
| 8015 | } else { |
| 8016 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 8017 | } |
| 8018 | } |
| 8019 | |
| 8020 | // |valid| -> |new_size| |
| 8021 | for (; descriptor < new_size; descriptor++) { |
| 8022 | PropertyDetails details = other->GetDetails(descriptor); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 8023 | if (details.type() == FIELD || |
| 8024 | (store_mode == FORCE_FIELD && descriptor == modify_index)) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8025 | Name* key = other->GetKey(descriptor); |
| 8026 | FieldDescriptor d(key, |
| 8027 | current_offset++, |
| 8028 | details.attributes(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8029 | details.representation()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8030 | result->Set(descriptor, &d, witness); |
| 8031 | } else { |
| 8032 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 8033 | } |
| 8034 | } |
| 8035 | |
| 8036 | result->Sort(); |
| 8037 | return result; |
| 8038 | } |
| 8039 | |
| 8040 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8041 | // Checks whether a merge of |other| into |this| would return a copy of |this|. |
| 8042 | bool DescriptorArray::IsMoreGeneralThan(int verbatim, |
| 8043 | int valid, |
| 8044 | int new_size, |
| 8045 | DescriptorArray* other) { |
| 8046 | ASSERT(verbatim <= valid); |
| 8047 | ASSERT(valid <= new_size); |
| 8048 | if (valid != new_size) return false; |
| 8049 | |
| 8050 | for (int descriptor = verbatim; descriptor < valid; descriptor++) { |
| 8051 | PropertyDetails details = GetDetails(descriptor); |
| 8052 | PropertyDetails other_details = other->GetDetails(descriptor); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 8053 | if (!other_details.representation().fits_into(details.representation())) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8054 | return false; |
| 8055 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 8056 | if (details.type() == CONSTANT) { |
| 8057 | if (other_details.type() != CONSTANT) return false; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 8058 | if (GetValue(descriptor) != other->GetValue(descriptor)) return false; |
| 8059 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8060 | } |
| 8061 | |
| 8062 | return true; |
| 8063 | } |
| 8064 | |
| 8065 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 8066 | // We need the whiteness witness since sort will reshuffle the entries in the |
| 8067 | // descriptor array. If the descriptor array were to be black, the shuffling |
| 8068 | // would move a slot that was already recorded as pointing into an evacuation |
| 8069 | // candidate. This would result in missing updates upon evacuation. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8070 | void DescriptorArray::Sort() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8071 | // In-place heap sort. |
| 8072 | int len = number_of_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8073 | // Reset sorting since the descriptor array might contain invalid pointers. |
| 8074 | for (int i = 0; i < len; ++i) SetSortedKey(i, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8075 | // Bottom-up max-heap construction. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8076 | // Index of the last node with children |
| 8077 | const int max_parent_index = (len / 2) - 1; |
| 8078 | for (int i = max_parent_index; i >= 0; --i) { |
| 8079 | int parent_index = i; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8080 | const uint32_t parent_hash = GetSortedKey(i)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8081 | while (parent_index <= max_parent_index) { |
| 8082 | int child_index = 2 * parent_index + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8083 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8084 | if (child_index + 1 < len) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8085 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8086 | if (right_child_hash > child_hash) { |
| 8087 | child_index++; |
| 8088 | child_hash = right_child_hash; |
| 8089 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8090 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8091 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8092 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8093 | // Now element at child_index could be < its children. |
| 8094 | parent_index = child_index; // parent_hash remains correct. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8095 | } |
| 8096 | } |
| 8097 | |
| 8098 | // Extract elements and create sorted array. |
| 8099 | for (int i = len - 1; i > 0; --i) { |
| 8100 | // Put max element at the back of the array. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8101 | SwapSortedKeys(0, i); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8102 | // Shift down the new top element. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8103 | int parent_index = 0; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8104 | const uint32_t parent_hash = GetSortedKey(parent_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8105 | const int max_parent_index = (i / 2) - 1; |
| 8106 | while (parent_index <= max_parent_index) { |
| 8107 | int child_index = parent_index * 2 + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8108 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8109 | if (child_index + 1 < i) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8110 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8111 | if (right_child_hash > child_hash) { |
| 8112 | child_index++; |
| 8113 | child_hash = right_child_hash; |
| 8114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8115 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8116 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8117 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8118 | parent_index = child_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8119 | } |
| 8120 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8121 | ASSERT(IsSortedNoDuplicates()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8122 | } |
| 8123 | |
| 8124 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 8125 | Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) { |
| 8126 | Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair(); |
| 8127 | copy->set_getter(pair->getter()); |
| 8128 | copy->set_setter(pair->setter()); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 8129 | return copy; |
| 8130 | } |
| 8131 | |
| 8132 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 8133 | Object* AccessorPair::GetComponent(AccessorComponent component) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 8134 | Object* accessor = get(component); |
| 8135 | return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8139 | MaybeObject* DeoptimizationInputData::Allocate(Isolate* isolate, |
| 8140 | int deopt_entry_count, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8141 | PretenureFlag pretenure) { |
| 8142 | ASSERT(deopt_entry_count > 0); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8143 | return isolate->heap()->AllocateFixedArray(LengthFor(deopt_entry_count), |
| 8144 | pretenure); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8145 | } |
| 8146 | |
| 8147 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8148 | MaybeObject* DeoptimizationOutputData::Allocate(Isolate* isolate, |
| 8149 | int number_of_deopt_points, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8150 | PretenureFlag pretenure) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8151 | if (number_of_deopt_points == 0) return isolate->heap()->empty_fixed_array(); |
| 8152 | return isolate->heap()->AllocateFixedArray( |
| 8153 | LengthOfFixedArray(number_of_deopt_points), pretenure); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8154 | } |
| 8155 | |
| 8156 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8157 | #ifdef DEBUG |
| 8158 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 8159 | if (IsEmpty()) return other->IsEmpty(); |
| 8160 | if (other->IsEmpty()) return false; |
| 8161 | if (length() != other->length()) return false; |
| 8162 | for (int i = 0; i < length(); ++i) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 8163 | if (get(i) != other->get(i)) return false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8164 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 8165 | return true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8166 | } |
| 8167 | #endif |
| 8168 | |
| 8169 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 8170 | static bool IsIdentifier(UnicodeCache* cache, Name* name) { |
| 8171 | // Checks whether the buffer contains an identifier (no escape). |
| 8172 | if (!name->IsString()) return false; |
| 8173 | String* string = String::cast(name); |
| 8174 | if (string->length() == 0) return false; |
| 8175 | ConsStringIteratorOp op; |
| 8176 | StringCharacterStream stream(string, &op); |
| 8177 | if (!cache->IsIdentifierStart(stream.GetNext())) { |
| 8178 | return false; |
| 8179 | } |
| 8180 | while (stream.HasMore()) { |
| 8181 | if (!cache->IsIdentifierPart(stream.GetNext())) { |
| 8182 | return false; |
| 8183 | } |
| 8184 | } |
| 8185 | return true; |
| 8186 | } |
| 8187 | |
| 8188 | |
| 8189 | bool Name::IsCacheable(Isolate* isolate) { |
| 8190 | return IsSymbol() || |
| 8191 | IsIdentifier(isolate->unicode_cache(), this) || |
| 8192 | this == isolate->heap()->hidden_string(); |
| 8193 | } |
| 8194 | |
| 8195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8196 | bool String::LooksValid() { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8197 | if (!GetIsolate()->heap()->Contains(this)) return false; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8198 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8199 | } |
| 8200 | |
| 8201 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8202 | String::FlatContent String::GetFlatContent() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8203 | ASSERT(!AllowHeapAllocation::IsAllowed()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8204 | int length = this->length(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8205 | StringShape shape(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8206 | String* string = this; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8207 | int offset = 0; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8208 | if (shape.representation_tag() == kConsStringTag) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8209 | ConsString* cons = ConsString::cast(string); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8210 | if (cons->second()->length() != 0) { |
| 8211 | return FlatContent(); |
| 8212 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8213 | string = cons->first(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8214 | shape = StringShape(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8215 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8216 | if (shape.representation_tag() == kSlicedStringTag) { |
| 8217 | SlicedString* slice = SlicedString::cast(string); |
| 8218 | offset = slice->offset(); |
| 8219 | string = slice->parent(); |
| 8220 | shape = StringShape(string); |
| 8221 | ASSERT(shape.representation_tag() != kConsStringTag && |
| 8222 | shape.representation_tag() != kSlicedStringTag); |
| 8223 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8224 | if (shape.encoding_tag() == kOneByteStringTag) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8225 | const uint8_t* start; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8226 | if (shape.representation_tag() == kSeqStringTag) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8227 | start = SeqOneByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8228 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8229 | start = ExternalAsciiString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8230 | } |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8231 | return FlatContent(Vector<const uint8_t>(start + offset, length)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8232 | } else { |
| 8233 | ASSERT(shape.encoding_tag() == kTwoByteStringTag); |
| 8234 | const uc16* start; |
| 8235 | if (shape.representation_tag() == kSeqStringTag) { |
| 8236 | start = SeqTwoByteString::cast(string)->GetChars(); |
| 8237 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8238 | start = ExternalTwoByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8239 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8240 | return FlatContent(Vector<const uc16>(start + offset, length)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8241 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8242 | } |
| 8243 | |
| 8244 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8245 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 8246 | RobustnessFlag robust_flag, |
| 8247 | int offset, |
| 8248 | int length, |
| 8249 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8250 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8251 | return SmartArrayPointer<char>(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8252 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8253 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8254 | |
| 8255 | // Negative length means the to the end of the string. |
| 8256 | if (length < 0) length = kMaxInt - offset; |
| 8257 | |
| 8258 | // 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] | 8259 | Access<ConsStringIteratorOp> op( |
| 8260 | heap->isolate()->objects_string_iterator()); |
| 8261 | StringCharacterStream stream(this, op.value(), offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8262 | int character_position = offset; |
| 8263 | int utf8_bytes = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8264 | int last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8265 | while (stream.HasMore() && character_position++ < offset + length) { |
| 8266 | uint16_t character = stream.GetNext(); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8267 | utf8_bytes += unibrow::Utf8::Length(character, last); |
| 8268 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8269 | } |
| 8270 | |
| 8271 | if (length_return) { |
| 8272 | *length_return = utf8_bytes; |
| 8273 | } |
| 8274 | |
| 8275 | char* result = NewArray<char>(utf8_bytes + 1); |
| 8276 | |
| 8277 | // 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] | 8278 | stream.Reset(this, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8279 | character_position = offset; |
| 8280 | int utf8_byte_position = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8281 | last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8282 | while (stream.HasMore() && character_position++ < offset + length) { |
| 8283 | uint16_t character = stream.GetNext(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8284 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 8285 | character = ' '; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8286 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8287 | utf8_byte_position += |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8288 | unibrow::Utf8::Encode(result + utf8_byte_position, character, last); |
| 8289 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8290 | } |
| 8291 | result[utf8_byte_position] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8292 | return SmartArrayPointer<char>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8293 | } |
| 8294 | |
| 8295 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8296 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 8297 | RobustnessFlag robust_flag, |
| 8298 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8299 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 8300 | } |
| 8301 | |
| 8302 | |
| 8303 | const uc16* String::GetTwoByteData() { |
| 8304 | return GetTwoByteData(0); |
| 8305 | } |
| 8306 | |
| 8307 | |
| 8308 | const uc16* String::GetTwoByteData(unsigned start) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 8309 | ASSERT(!IsOneByteRepresentationUnderneath()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8310 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8311 | case kSeqStringTag: |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8312 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8313 | case kExternalStringTag: |
| 8314 | return ExternalTwoByteString::cast(this)-> |
| 8315 | ExternalTwoByteStringGetData(start); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8316 | case kSlicedStringTag: { |
| 8317 | SlicedString* slice = SlicedString::cast(this); |
| 8318 | return slice->parent()->GetTwoByteData(start + slice->offset()); |
| 8319 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8320 | case kConsStringTag: |
| 8321 | UNREACHABLE(); |
| 8322 | return NULL; |
| 8323 | } |
| 8324 | UNREACHABLE(); |
| 8325 | return NULL; |
| 8326 | } |
| 8327 | |
| 8328 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8329 | SmartArrayPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8330 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8331 | return SmartArrayPointer<uc16>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8332 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8333 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8334 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8335 | Access<ConsStringIteratorOp> op( |
| 8336 | heap->isolate()->objects_string_iterator()); |
| 8337 | StringCharacterStream stream(this, op.value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8338 | |
| 8339 | uc16* result = NewArray<uc16>(length() + 1); |
| 8340 | |
| 8341 | int i = 0; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8342 | while (stream.HasMore()) { |
| 8343 | uint16_t character = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8344 | result[i++] = character; |
| 8345 | } |
| 8346 | result[i] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8347 | return SmartArrayPointer<uc16>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8348 | } |
| 8349 | |
| 8350 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8351 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8352 | return reinterpret_cast<uc16*>( |
| 8353 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 8354 | } |
| 8355 | |
| 8356 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8357 | void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8358 | Relocatable* current = isolate->relocatable_top(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8359 | while (current != NULL) { |
| 8360 | current->PostGarbageCollection(); |
| 8361 | current = current->prev_; |
| 8362 | } |
| 8363 | } |
| 8364 | |
| 8365 | |
| 8366 | // Reserve space for statics needing saving and restoring. |
| 8367 | int Relocatable::ArchiveSpacePerThread() { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8368 | return sizeof(Relocatable*); // NOLINT |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
| 8371 | |
| 8372 | // Archive statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8373 | char* Relocatable::ArchiveState(Isolate* isolate, char* to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8374 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 8375 | isolate->set_relocatable_top(NULL); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8376 | return to + ArchiveSpacePerThread(); |
| 8377 | } |
| 8378 | |
| 8379 | |
| 8380 | // Restore statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8381 | char* Relocatable::RestoreState(Isolate* isolate, char* from) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8382 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8383 | return from + ArchiveSpacePerThread(); |
| 8384 | } |
| 8385 | |
| 8386 | |
| 8387 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 8388 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 8389 | Iterate(v, top); |
| 8390 | return thread_storage + ArchiveSpacePerThread(); |
| 8391 | } |
| 8392 | |
| 8393 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8394 | void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8395 | Iterate(v, isolate->relocatable_top()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8396 | } |
| 8397 | |
| 8398 | |
| 8399 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 8400 | Relocatable* current = top; |
| 8401 | while (current != NULL) { |
| 8402 | current->IterateInstance(v); |
| 8403 | current = current->prev_; |
| 8404 | } |
| 8405 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8406 | |
| 8407 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8408 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 8409 | : Relocatable(isolate), |
| 8410 | str_(str.location()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8411 | length_(str->length()) { |
| 8412 | PostGarbageCollection(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8413 | } |
| 8414 | |
| 8415 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8416 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 8417 | : Relocatable(isolate), |
| 8418 | str_(0), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8419 | is_ascii_(true), |
| 8420 | length_(input.length()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8421 | start_(input.start()) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8422 | |
| 8423 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8424 | void FlatStringReader::PostGarbageCollection() { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8425 | if (str_ == NULL) return; |
| 8426 | Handle<String> str(str_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8427 | ASSERT(str->IsFlat()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8428 | DisallowHeapAllocation no_gc; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 8429 | // This does not actually prevent the vector from being relocated later. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8430 | String::FlatContent content = str->GetFlatContent(); |
| 8431 | ASSERT(content.IsFlat()); |
| 8432 | is_ascii_ = content.IsAscii(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8433 | if (is_ascii_) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8434 | start_ = content.ToOneByteVector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8435 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8436 | start_ = content.ToUC16Vector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8437 | } |
| 8438 | } |
| 8439 | |
| 8440 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8441 | String* ConsStringIteratorOp::Operate(String* string, |
| 8442 | unsigned* offset_out, |
| 8443 | int32_t* type_out, |
| 8444 | unsigned* length_out) { |
| 8445 | ASSERT(string->IsConsString()); |
| 8446 | ConsString* cons_string = ConsString::cast(string); |
| 8447 | // Set up search data. |
| 8448 | root_ = cons_string; |
| 8449 | consumed_ = *offset_out; |
| 8450 | // Now search. |
| 8451 | return Search(offset_out, type_out, length_out); |
| 8452 | } |
| 8453 | |
| 8454 | |
| 8455 | String* ConsStringIteratorOp::Search(unsigned* offset_out, |
| 8456 | int32_t* type_out, |
| 8457 | unsigned* length_out) { |
| 8458 | ConsString* cons_string = root_; |
| 8459 | // Reset the stack, pushing the root string. |
| 8460 | depth_ = 1; |
| 8461 | maximum_depth_ = 1; |
| 8462 | frames_[0] = cons_string; |
| 8463 | const unsigned consumed = consumed_; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8464 | unsigned offset = 0; |
| 8465 | while (true) { |
| 8466 | // Loop until the string is found which contains the target offset. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8467 | String* string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8468 | unsigned length = string->length(); |
| 8469 | int32_t type; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8470 | if (consumed < offset + length) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8471 | // Target offset is in the left branch. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8472 | // Keep going if we're still in a ConString. |
| 8473 | type = string->map()->instance_type(); |
| 8474 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8475 | cons_string = ConsString::cast(string); |
| 8476 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8477 | continue; |
| 8478 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8479 | // Tell the stack we're done decending. |
| 8480 | AdjustMaximumDepth(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8481 | } else { |
| 8482 | // Descend right. |
| 8483 | // Update progress through the string. |
| 8484 | offset += length; |
| 8485 | // Keep going if we're still in a ConString. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8486 | string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8487 | type = string->map()->instance_type(); |
| 8488 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8489 | cons_string = ConsString::cast(string); |
| 8490 | PushRight(cons_string); |
| 8491 | // TODO(dcarney) Add back root optimization. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8492 | continue; |
| 8493 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8494 | // Need this to be updated for the current string. |
| 8495 | length = string->length(); |
| 8496 | // Account for the possibility of an empty right leaf. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8497 | // This happens only if we have asked for an offset outside the string. |
| 8498 | if (length == 0) { |
| 8499 | // Reset depth so future operations will return null immediately. |
| 8500 | Reset(); |
| 8501 | return NULL; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8502 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8503 | // Tell the stack we're done decending. |
| 8504 | AdjustMaximumDepth(); |
| 8505 | // Pop stack so next iteration is in correct place. |
| 8506 | Pop(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8507 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8508 | ASSERT(length != 0); |
| 8509 | // Adjust return values and exit. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8510 | consumed_ = offset + length; |
| 8511 | *offset_out = consumed - offset; |
| 8512 | *type_out = type; |
| 8513 | *length_out = length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8514 | return string; |
| 8515 | } |
| 8516 | UNREACHABLE(); |
| 8517 | return NULL; |
| 8518 | } |
| 8519 | |
| 8520 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8521 | String* ConsStringIteratorOp::NextLeaf(bool* blew_stack, |
| 8522 | int32_t* type_out, |
| 8523 | unsigned* length_out) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8524 | while (true) { |
| 8525 | // Tree traversal complete. |
| 8526 | if (depth_ == 0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8527 | *blew_stack = false; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8528 | return NULL; |
| 8529 | } |
| 8530 | // We've lost track of higher nodes. |
| 8531 | if (maximum_depth_ - depth_ == kStackSize) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8532 | *blew_stack = true; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8533 | return NULL; |
| 8534 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8535 | // Go right. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8536 | ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)]; |
| 8537 | String* string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8538 | int32_t type = string->map()->instance_type(); |
| 8539 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8540 | // Pop stack so next iteration is in correct place. |
| 8541 | Pop(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8542 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8543 | // Could be a flattened ConsString. |
| 8544 | if (length == 0) continue; |
| 8545 | *length_out = length; |
| 8546 | *type_out = type; |
| 8547 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8548 | return string; |
| 8549 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8550 | cons_string = ConsString::cast(string); |
| 8551 | // TODO(dcarney) Add back root optimization. |
| 8552 | PushRight(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8553 | // Need to traverse all the way left. |
| 8554 | while (true) { |
| 8555 | // Continue left. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8556 | string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8557 | type = string->map()->instance_type(); |
| 8558 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 8559 | AdjustMaximumDepth(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8560 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8561 | ASSERT(length != 0); |
| 8562 | *length_out = length; |
| 8563 | *type_out = type; |
| 8564 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8565 | return string; |
| 8566 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8567 | cons_string = ConsString::cast(string); |
| 8568 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8569 | } |
| 8570 | } |
| 8571 | UNREACHABLE(); |
| 8572 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8573 | } |
| 8574 | |
| 8575 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8576 | uint16_t ConsString::ConsStringGet(int index) { |
| 8577 | ASSERT(index >= 0 && index < this->length()); |
| 8578 | |
| 8579 | // Check for a flattened cons string |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8580 | if (second()->length() == 0) { |
| 8581 | String* left = first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8582 | return left->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8583 | } |
| 8584 | |
| 8585 | String* string = String::cast(this); |
| 8586 | |
| 8587 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8588 | if (StringShape(string).IsCons()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8589 | ConsString* cons_string = ConsString::cast(string); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8590 | String* left = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8591 | if (left->length() > index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8592 | string = left; |
| 8593 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8594 | index -= left->length(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8595 | string = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8596 | } |
| 8597 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8598 | return string->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8599 | } |
| 8600 | } |
| 8601 | |
| 8602 | UNREACHABLE(); |
| 8603 | return 0; |
| 8604 | } |
| 8605 | |
| 8606 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8607 | uint16_t SlicedString::SlicedStringGet(int index) { |
| 8608 | return parent()->Get(offset() + index); |
| 8609 | } |
| 8610 | |
| 8611 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8612 | template <typename sinkchar> |
| 8613 | void String::WriteToFlat(String* src, |
| 8614 | sinkchar* sink, |
| 8615 | int f, |
| 8616 | int t) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8617 | String* source = src; |
| 8618 | int from = f; |
| 8619 | int to = t; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8620 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8621 | ASSERT(0 <= from && from <= to && to <= source->length()); |
| 8622 | switch (StringShape(source).full_representation_tag()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8623 | case kOneByteStringTag | kExternalStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8624 | CopyChars(sink, |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8625 | ExternalAsciiString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8626 | to - from); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8627 | return; |
| 8628 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8629 | case kTwoByteStringTag | kExternalStringTag: { |
| 8630 | const uc16* data = |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8631 | ExternalTwoByteString::cast(source)->GetChars(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8632 | CopyChars(sink, |
| 8633 | data + from, |
| 8634 | to - from); |
| 8635 | return; |
| 8636 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8637 | case kOneByteStringTag | kSeqStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8638 | CopyChars(sink, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8639 | SeqOneByteString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8640 | to - from); |
| 8641 | return; |
| 8642 | } |
| 8643 | case kTwoByteStringTag | kSeqStringTag: { |
| 8644 | CopyChars(sink, |
| 8645 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 8646 | to - from); |
| 8647 | return; |
| 8648 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8649 | case kOneByteStringTag | kConsStringTag: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8650 | case kTwoByteStringTag | kConsStringTag: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8651 | ConsString* cons_string = ConsString::cast(source); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8652 | String* first = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8653 | int boundary = first->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8654 | if (to - boundary >= boundary - from) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8655 | // Right hand side is longer. Recurse over left. |
| 8656 | if (from < boundary) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8657 | WriteToFlat(first, sink, from, boundary); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8658 | sink += boundary - from; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8659 | from = 0; |
| 8660 | } else { |
| 8661 | from -= boundary; |
| 8662 | } |
| 8663 | to -= boundary; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8664 | source = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8665 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8666 | // Left hand side is longer. Recurse over right. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8667 | if (to > boundary) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8668 | String* second = cons_string->second(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8669 | // When repeatedly appending to a string, we get a cons string that |
| 8670 | // is unbalanced to the left, a list, essentially. We inline the |
| 8671 | // common case of sequential ascii right child. |
| 8672 | if (to - boundary == 1) { |
| 8673 | sink[boundary - from] = static_cast<sinkchar>(second->Get(0)); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8674 | } else if (second->IsSeqOneByteString()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8675 | CopyChars(sink + boundary - from, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8676 | SeqOneByteString::cast(second)->GetChars(), |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8677 | to - boundary); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8678 | } else { |
| 8679 | WriteToFlat(second, |
| 8680 | sink + boundary - from, |
| 8681 | 0, |
| 8682 | to - boundary); |
| 8683 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8684 | to = boundary; |
| 8685 | } |
| 8686 | source = first; |
| 8687 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8688 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8689 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8690 | case kOneByteStringTag | kSlicedStringTag: |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8691 | case kTwoByteStringTag | kSlicedStringTag: { |
| 8692 | SlicedString* slice = SlicedString::cast(source); |
| 8693 | unsigned offset = slice->offset(); |
| 8694 | WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 8695 | return; |
| 8696 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8697 | } |
| 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8702 | // Compares the contents of two strings by reading and comparing |
| 8703 | // int-sized blocks of characters. |
| 8704 | template <typename Char> |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8705 | static inline bool CompareRawStringContents(const Char* const a, |
| 8706 | const Char* const b, |
| 8707 | int length) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8708 | int i = 0; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8709 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8710 | // If this architecture isn't comfortable reading unaligned ints |
| 8711 | // then we have to check that the strings are aligned before |
| 8712 | // comparing them blockwise. |
| 8713 | const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8714 | uint32_t pa_addr = reinterpret_cast<uint32_t>(a); |
| 8715 | uint32_t pb_addr = reinterpret_cast<uint32_t>(b); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 8716 | if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8717 | #endif |
| 8718 | const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT |
| 8719 | int endpoint = length - kStepSize; |
| 8720 | // Compare blocks until we reach near the end of the string. |
| 8721 | for (; i <= endpoint; i += kStepSize) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8722 | uint32_t wa = *reinterpret_cast<const uint32_t*>(a + i); |
| 8723 | uint32_t wb = *reinterpret_cast<const uint32_t*>(b + i); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8724 | if (wa != wb) { |
| 8725 | return false; |
| 8726 | } |
| 8727 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8728 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8729 | } |
| 8730 | #endif |
| 8731 | // Compare the remaining characters that didn't fit into a block. |
| 8732 | for (; i < length; i++) { |
| 8733 | if (a[i] != b[i]) { |
| 8734 | return false; |
| 8735 | } |
| 8736 | } |
| 8737 | return true; |
| 8738 | } |
| 8739 | |
| 8740 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8741 | template<typename Chars1, typename Chars2> |
| 8742 | class RawStringComparator : public AllStatic { |
| 8743 | public: |
| 8744 | static inline bool compare(const Chars1* a, const Chars2* b, int len) { |
| 8745 | ASSERT(sizeof(Chars1) != sizeof(Chars2)); |
| 8746 | for (int i = 0; i < len; i++) { |
| 8747 | if (a[i] != b[i]) { |
| 8748 | return false; |
| 8749 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8750 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8751 | return true; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8752 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8753 | }; |
| 8754 | |
| 8755 | |
| 8756 | template<> |
| 8757 | class RawStringComparator<uint16_t, uint16_t> { |
| 8758 | public: |
| 8759 | static inline bool compare(const uint16_t* a, const uint16_t* b, int len) { |
| 8760 | return CompareRawStringContents(a, b, len); |
| 8761 | } |
| 8762 | }; |
| 8763 | |
| 8764 | |
| 8765 | template<> |
| 8766 | class RawStringComparator<uint8_t, uint8_t> { |
| 8767 | public: |
| 8768 | static inline bool compare(const uint8_t* a, const uint8_t* b, int len) { |
| 8769 | return CompareRawStringContents(a, b, len); |
| 8770 | } |
| 8771 | }; |
| 8772 | |
| 8773 | |
| 8774 | class StringComparator { |
| 8775 | class State { |
| 8776 | public: |
| 8777 | explicit inline State(ConsStringIteratorOp* op) |
| 8778 | : op_(op), is_one_byte_(true), length_(0), buffer8_(NULL) {} |
| 8779 | |
| 8780 | inline void Init(String* string, unsigned len) { |
| 8781 | op_->Reset(); |
| 8782 | int32_t type = string->map()->instance_type(); |
| 8783 | String::Visit(string, 0, *this, *op_, type, len); |
| 8784 | } |
| 8785 | |
| 8786 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 8787 | is_one_byte_ = true; |
| 8788 | buffer8_ = chars; |
| 8789 | length_ = length; |
| 8790 | } |
| 8791 | |
| 8792 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 8793 | is_one_byte_ = false; |
| 8794 | buffer16_ = chars; |
| 8795 | length_ = length; |
| 8796 | } |
| 8797 | |
| 8798 | void Advance(unsigned consumed) { |
| 8799 | ASSERT(consumed <= length_); |
| 8800 | // Still in buffer. |
| 8801 | if (length_ != consumed) { |
| 8802 | if (is_one_byte_) { |
| 8803 | buffer8_ += consumed; |
| 8804 | } else { |
| 8805 | buffer16_ += consumed; |
| 8806 | } |
| 8807 | length_ -= consumed; |
| 8808 | return; |
| 8809 | } |
| 8810 | // Advance state. |
| 8811 | ASSERT(op_->HasMore()); |
| 8812 | int32_t type = 0; |
| 8813 | unsigned length = 0; |
| 8814 | String* next = op_->ContinueOperation(&type, &length); |
| 8815 | ASSERT(next != NULL); |
| 8816 | ConsStringNullOp null_op; |
| 8817 | String::Visit(next, 0, *this, null_op, type, length); |
| 8818 | } |
| 8819 | |
| 8820 | ConsStringIteratorOp* const op_; |
| 8821 | bool is_one_byte_; |
| 8822 | unsigned length_; |
| 8823 | union { |
| 8824 | const uint8_t* buffer8_; |
| 8825 | const uint16_t* buffer16_; |
| 8826 | }; |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 8827 | |
| 8828 | private: |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8829 | DISALLOW_IMPLICIT_CONSTRUCTORS(State); |
| 8830 | }; |
| 8831 | |
| 8832 | public: |
| 8833 | inline StringComparator(ConsStringIteratorOp* op_1, |
| 8834 | ConsStringIteratorOp* op_2) |
| 8835 | : state_1_(op_1), |
| 8836 | state_2_(op_2) { |
| 8837 | } |
| 8838 | |
| 8839 | template<typename Chars1, typename Chars2> |
| 8840 | static inline bool Equals(State* state_1, State* state_2, unsigned to_check) { |
| 8841 | const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); |
| 8842 | const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); |
| 8843 | return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check); |
| 8844 | } |
| 8845 | |
| 8846 | bool Equals(unsigned length, String* string_1, String* string_2) { |
| 8847 | ASSERT(length != 0); |
| 8848 | state_1_.Init(string_1, length); |
| 8849 | state_2_.Init(string_2, length); |
| 8850 | while (true) { |
| 8851 | unsigned to_check = Min(state_1_.length_, state_2_.length_); |
| 8852 | ASSERT(to_check > 0 && to_check <= length); |
| 8853 | bool is_equal; |
| 8854 | if (state_1_.is_one_byte_) { |
| 8855 | if (state_2_.is_one_byte_) { |
| 8856 | is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8857 | } else { |
| 8858 | is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8859 | } |
| 8860 | } else { |
| 8861 | if (state_2_.is_one_byte_) { |
| 8862 | is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8863 | } else { |
| 8864 | is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8865 | } |
| 8866 | } |
| 8867 | // Looping done. |
| 8868 | if (!is_equal) return false; |
| 8869 | length -= to_check; |
| 8870 | // Exit condition. Strings are equal. |
| 8871 | if (length == 0) return true; |
| 8872 | state_1_.Advance(to_check); |
| 8873 | state_2_.Advance(to_check); |
| 8874 | } |
| 8875 | } |
| 8876 | |
| 8877 | private: |
| 8878 | State state_1_; |
| 8879 | State state_2_; |
| 8880 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringComparator); |
| 8881 | }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8882 | |
| 8883 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8884 | bool String::SlowEquals(String* other) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8885 | // Fast check: negative check with lengths. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8886 | int len = length(); |
| 8887 | if (len != other->length()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8888 | if (len == 0) return true; |
| 8889 | |
| 8890 | // Fast check: if hash code is computed for both strings |
| 8891 | // a fast negative check can be performed. |
| 8892 | if (HasHashCode() && other->HasHashCode()) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8893 | #ifdef DEBUG |
| 8894 | if (FLAG_enable_slow_asserts) { |
| 8895 | if (Hash() != other->Hash()) { |
| 8896 | bool found_difference = false; |
| 8897 | for (int i = 0; i < len; i++) { |
| 8898 | if (Get(i) != other->Get(i)) { |
| 8899 | found_difference = true; |
| 8900 | break; |
| 8901 | } |
| 8902 | } |
| 8903 | ASSERT(found_difference); |
| 8904 | } |
| 8905 | } |
| 8906 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8907 | if (Hash() != other->Hash()) return false; |
| 8908 | } |
| 8909 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 8910 | // We know the strings are both non-empty. Compare the first chars |
| 8911 | // before we try to flatten the strings. |
| 8912 | if (this->Get(0) != other->Get(0)) return false; |
| 8913 | |
| 8914 | String* lhs = this->TryFlattenGetString(); |
| 8915 | String* rhs = other->TryFlattenGetString(); |
| 8916 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8917 | // 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] | 8918 | if (StringShape(lhs).IsSequentialAscii() && |
| 8919 | StringShape(rhs).IsSequentialAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8920 | const uint8_t* str1 = SeqOneByteString::cast(lhs)->GetChars(); |
| 8921 | const uint8_t* str2 = SeqOneByteString::cast(rhs)->GetChars(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8922 | return CompareRawStringContents(str1, str2, len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8923 | } |
| 8924 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8925 | Isolate* isolate = GetIsolate(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8926 | StringComparator comparator(isolate->objects_string_compare_iterator_a(), |
| 8927 | isolate->objects_string_compare_iterator_b()); |
| 8928 | |
| 8929 | return comparator.Equals(static_cast<unsigned>(len), lhs, rhs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8930 | } |
| 8931 | |
| 8932 | |
| 8933 | bool String::MarkAsUndetectable() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 8934 | if (StringShape(this).IsInternalized()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8935 | |
| 8936 | Map* map = this->map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8937 | Heap* heap = GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8938 | if (map == heap->string_map()) { |
| 8939 | this->set_map(heap->undetectable_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8940 | return true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8941 | } else if (map == heap->ascii_string_map()) { |
| 8942 | this->set_map(heap->undetectable_ascii_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8943 | return true; |
| 8944 | } |
| 8945 | // Rest cannot be marked as undetectable |
| 8946 | return false; |
| 8947 | } |
| 8948 | |
| 8949 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 8950 | bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8951 | int slen = length(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8952 | // Can't check exact length equality, but we can check bounds. |
| 8953 | int str_len = str.length(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 8954 | if (!allow_prefix_match && |
| 8955 | (str_len < slen || |
| 8956 | str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8957 | return false; |
| 8958 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8959 | int i; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8960 | unsigned remaining_in_str = static_cast<unsigned>(str_len); |
| 8961 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start()); |
| 8962 | for (i = 0; i < slen && remaining_in_str > 0; i++) { |
| 8963 | unsigned cursor = 0; |
| 8964 | uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor); |
| 8965 | ASSERT(cursor > 0 && cursor <= remaining_in_str); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8966 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 8967 | if (i > slen - 1) return false; |
| 8968 | if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false; |
| 8969 | if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false; |
| 8970 | } else { |
| 8971 | if (Get(i) != r) return false; |
| 8972 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8973 | utf8_data += cursor; |
| 8974 | remaining_in_str -= cursor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8975 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 8976 | return (allow_prefix_match || i == slen) && remaining_in_str == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8977 | } |
| 8978 | |
| 8979 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8980 | bool String::IsOneByteEqualTo(Vector<const uint8_t> str) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 8981 | int slen = length(); |
| 8982 | if (str.length() != slen) return false; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8983 | DisallowHeapAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8984 | FlatContent content = GetFlatContent(); |
| 8985 | if (content.IsAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8986 | return CompareChars(content.ToOneByteVector().start(), |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8987 | str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 8988 | } |
| 8989 | for (int i = 0; i < slen; i++) { |
| 8990 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 8991 | } |
| 8992 | return true; |
| 8993 | } |
| 8994 | |
| 8995 | |
| 8996 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 8997 | int slen = length(); |
| 8998 | if (str.length() != slen) return false; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8999 | DisallowHeapAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9000 | FlatContent content = GetFlatContent(); |
| 9001 | if (content.IsTwoByte()) { |
| 9002 | return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 9003 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 9004 | for (int i = 0; i < slen; i++) { |
| 9005 | if (Get(i) != str[i]) return false; |
| 9006 | } |
| 9007 | return true; |
| 9008 | } |
| 9009 | |
| 9010 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9011 | class IteratingStringHasher: public StringHasher { |
| 9012 | public: |
| 9013 | static inline uint32_t Hash(String* string, uint32_t seed) { |
| 9014 | const unsigned len = static_cast<unsigned>(string->length()); |
| 9015 | IteratingStringHasher hasher(len, seed); |
| 9016 | if (hasher.has_trivial_hash()) { |
| 9017 | return hasher.GetHashField(); |
| 9018 | } |
| 9019 | int32_t type = string->map()->instance_type(); |
| 9020 | ConsStringNullOp null_op; |
| 9021 | String::Visit(string, 0, hasher, null_op, type, len); |
| 9022 | // Flat strings terminate immediately. |
| 9023 | if (hasher.consumed_ == len) { |
| 9024 | ASSERT(!string->IsConsString()); |
| 9025 | return hasher.GetHashField(); |
| 9026 | } |
| 9027 | ASSERT(string->IsConsString()); |
| 9028 | // This is a ConsString, iterate across it. |
| 9029 | ConsStringIteratorOp op; |
| 9030 | unsigned offset = 0; |
| 9031 | unsigned leaf_length = len; |
| 9032 | string = op.Operate(string, &offset, &type, &leaf_length); |
| 9033 | while (true) { |
| 9034 | ASSERT(hasher.consumed_ < len); |
| 9035 | String::Visit(string, 0, hasher, null_op, type, leaf_length); |
| 9036 | if (hasher.consumed_ == len) break; |
| 9037 | string = op.ContinueOperation(&type, &leaf_length); |
| 9038 | // This should be taken care of by the length check. |
| 9039 | ASSERT(string != NULL); |
| 9040 | } |
| 9041 | return hasher.GetHashField(); |
| 9042 | } |
| 9043 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 9044 | AddCharacters(chars, static_cast<int>(length)); |
| 9045 | consumed_ += length; |
| 9046 | } |
| 9047 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 9048 | AddCharacters(chars, static_cast<int>(length)); |
| 9049 | consumed_ += length; |
| 9050 | } |
| 9051 | |
| 9052 | private: |
| 9053 | inline IteratingStringHasher(int len, uint32_t seed) |
| 9054 | : StringHasher(len, seed), |
| 9055 | consumed_(0) {} |
| 9056 | unsigned consumed_; |
| 9057 | DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher); |
| 9058 | }; |
| 9059 | |
| 9060 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9061 | uint32_t String::ComputeAndSetHash() { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 9062 | // 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] | 9063 | ASSERT(!HasHashCode()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9064 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9065 | // Store the hash code in the object. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9066 | uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9067 | set_hash_field(field); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9068 | |
| 9069 | // Check the hash code is there. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9070 | ASSERT(HasHashCode()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 9071 | uint32_t result = field >> kHashShift; |
| 9072 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 9073 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9074 | } |
| 9075 | |
| 9076 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9077 | bool String::ComputeArrayIndex(uint32_t* index) { |
| 9078 | int length = this->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9079 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9080 | ConsStringIteratorOp op; |
| 9081 | StringCharacterStream stream(this, &op); |
| 9082 | uint16_t ch = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9083 | |
| 9084 | // If the string begins with a '0' character, it must only consist |
| 9085 | // of it to be a legal array index. |
| 9086 | if (ch == '0') { |
| 9087 | *index = 0; |
| 9088 | return length == 1; |
| 9089 | } |
| 9090 | |
| 9091 | // Convert string to uint32 array index; character by character. |
| 9092 | int d = ch - '0'; |
| 9093 | if (d < 0 || d > 9) return false; |
| 9094 | uint32_t result = d; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9095 | while (stream.HasMore()) { |
| 9096 | d = stream.GetNext() - '0'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9097 | if (d < 0 || d > 9) return false; |
| 9098 | // Check that the new result is below the 32 bit limit. |
| 9099 | if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 9100 | result = (result * 10) + d; |
| 9101 | } |
| 9102 | |
| 9103 | *index = result; |
| 9104 | return true; |
| 9105 | } |
| 9106 | |
| 9107 | |
| 9108 | bool String::SlowAsArrayIndex(uint32_t* index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9109 | if (length() <= kMaxCachedArrayIndexLength) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9110 | Hash(); // force computation of hash code |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9111 | uint32_t field = hash_field(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9112 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9113 | // Isolate the array index form the full hash field. |
| 9114 | *index = (kArrayIndexHashMask & field) >> kHashShift; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9115 | return true; |
| 9116 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9117 | return ComputeArrayIndex(index); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9118 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9119 | } |
| 9120 | |
| 9121 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9122 | Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) { |
| 9123 | int new_size, old_size; |
| 9124 | int old_length = string->length(); |
| 9125 | if (old_length <= new_length) return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9126 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9127 | if (string->IsSeqOneByteString()) { |
| 9128 | old_size = SeqOneByteString::SizeFor(old_length); |
| 9129 | new_size = SeqOneByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9130 | } else { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9131 | ASSERT(string->IsSeqTwoByteString()); |
| 9132 | old_size = SeqTwoByteString::SizeFor(old_length); |
| 9133 | new_size = SeqTwoByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9134 | } |
| 9135 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9136 | int delta = old_size - new_size; |
| 9137 | string->set_length(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9138 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9139 | Address start_of_string = string->address(); |
| 9140 | ASSERT_OBJECT_ALIGNED(start_of_string); |
| 9141 | ASSERT_OBJECT_ALIGNED(start_of_string + new_size); |
| 9142 | |
| 9143 | Heap* heap = string->GetHeap(); |
| 9144 | NewSpace* newspace = heap->new_space(); |
| 9145 | if (newspace->Contains(start_of_string) && |
| 9146 | newspace->top() == start_of_string + old_size) { |
| 9147 | // Last allocated object in new space. Simply lower allocation top. |
| 9148 | *(newspace->allocation_top_address()) = start_of_string + new_size; |
| 9149 | } else { |
| 9150 | // Sizes are pointer size aligned, so that we can use filler objects |
| 9151 | // that are a multiple of pointer size. |
| 9152 | heap->CreateFillerObjectAt(start_of_string + new_size, delta); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9153 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9154 | if (Marking::IsBlack(Marking::MarkBitFrom(start_of_string))) { |
| 9155 | MemoryChunk::IncrementLiveBytesFromMutator(start_of_string, -delta); |
| 9156 | } |
| 9157 | |
| 9158 | |
| 9159 | if (new_length == 0) return heap->isolate()->factory()->empty_string(); |
| 9160 | return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9161 | } |
| 9162 | |
| 9163 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 9164 | AllocationMemento* AllocationMemento::FindForJSObject(JSObject* object, |
| 9165 | bool in_GC) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9166 | // Currently, AllocationMemento objects are only allocated immediately |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9167 | // after JSArrays in NewSpace, and detecting whether a JSArray has one |
| 9168 | // involves carefully checking the object immediately after the JSArray |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9169 | // (if there is one) to see if it's an AllocationMemento. |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9170 | if (FLAG_track_allocation_sites && object->GetHeap()->InNewSpace(object)) { |
| 9171 | Address ptr_end = (reinterpret_cast<Address>(object) - kHeapObjectTag) + |
| 9172 | object->Size(); |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 9173 | Address top; |
| 9174 | if (in_GC) { |
| 9175 | top = object->GetHeap()->new_space()->FromSpacePageHigh(); |
| 9176 | } else { |
| 9177 | top = object->GetHeap()->NewSpaceTop(); |
| 9178 | } |
| 9179 | if ((ptr_end + AllocationMemento::kSize) <= top) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9180 | // There is room in newspace for allocation info. Do we have some? |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9181 | Map** possible_allocation_memento_map = |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9182 | reinterpret_cast<Map**>(ptr_end); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9183 | if (*possible_allocation_memento_map == |
| 9184 | object->GetHeap()->allocation_memento_map()) { |
| 9185 | AllocationMemento* memento = AllocationMemento::cast( |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 9186 | reinterpret_cast<Object*>(ptr_end + kHeapObjectTag)); |
hpayer@chromium.org | 27ce874 | 2013-09-19 09:59:01 +0000 | [diff] [blame] | 9187 | return memento; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9188 | } |
| 9189 | } |
| 9190 | } |
| 9191 | return NULL; |
| 9192 | } |
| 9193 | |
| 9194 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9195 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9196 | // For array indexes mix the length into the hash as an array index could |
| 9197 | // be zero. |
| 9198 | ASSERT(length > 0); |
| 9199 | ASSERT(length <= String::kMaxArrayIndexSize); |
| 9200 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 9201 | (1 << String::kArrayIndexValueBits)); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9202 | |
| 9203 | value <<= String::kHashShift; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9204 | value |= length << String::kArrayIndexHashLengthShift; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9205 | |
| 9206 | ASSERT((value & String::kIsNotArrayIndexMask) == 0); |
| 9207 | ASSERT((length > String::kMaxCachedArrayIndexLength) || |
| 9208 | (value & String::kContainsCachedArrayIndexMask) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9209 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9210 | } |
| 9211 | |
| 9212 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9213 | uint32_t StringHasher::GetHashField() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9214 | if (length_ <= String::kMaxHashCalcLength) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9215 | if (is_array_index_) { |
| 9216 | return MakeArrayIndexHash(array_index_, length_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9217 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9218 | return (GetHashCore(raw_running_hash_) << String::kHashShift) | |
| 9219 | String::kIsNotArrayIndexMask; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9220 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9221 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9222 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9223 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9224 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9225 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9226 | uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars, |
| 9227 | uint32_t seed, |
| 9228 | int* utf16_length_out) { |
| 9229 | int vector_length = chars.length(); |
| 9230 | // Handle some edge cases |
| 9231 | if (vector_length <= 1) { |
| 9232 | ASSERT(vector_length == 0 || |
| 9233 | static_cast<uint8_t>(chars.start()[0]) <= |
| 9234 | unibrow::Utf8::kMaxOneByteChar); |
| 9235 | *utf16_length_out = vector_length; |
| 9236 | return HashSequentialString(chars.start(), vector_length, seed); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9237 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9238 | // Start with a fake length which won't affect computation. |
| 9239 | // It will be updated later. |
| 9240 | StringHasher hasher(String::kMaxArrayIndexSize, seed); |
| 9241 | unsigned remaining = static_cast<unsigned>(vector_length); |
| 9242 | const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start()); |
| 9243 | int utf16_length = 0; |
| 9244 | bool is_index = true; |
| 9245 | ASSERT(hasher.is_array_index_); |
| 9246 | while (remaining > 0) { |
| 9247 | unsigned consumed = 0; |
| 9248 | uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed); |
| 9249 | ASSERT(consumed > 0 && consumed <= remaining); |
| 9250 | stream += consumed; |
| 9251 | remaining -= consumed; |
| 9252 | bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 9253 | utf16_length += is_two_characters ? 2 : 1; |
| 9254 | // No need to keep hashing. But we do need to calculate utf16_length. |
| 9255 | if (utf16_length > String::kMaxHashCalcLength) continue; |
| 9256 | if (is_two_characters) { |
| 9257 | uint16_t c1 = unibrow::Utf16::LeadSurrogate(c); |
| 9258 | uint16_t c2 = unibrow::Utf16::TrailSurrogate(c); |
| 9259 | hasher.AddCharacter(c1); |
| 9260 | hasher.AddCharacter(c2); |
| 9261 | if (is_index) is_index = hasher.UpdateIndex(c1); |
| 9262 | if (is_index) is_index = hasher.UpdateIndex(c2); |
| 9263 | } else { |
| 9264 | hasher.AddCharacter(c); |
| 9265 | if (is_index) is_index = hasher.UpdateIndex(c); |
| 9266 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9267 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9268 | *utf16_length_out = static_cast<int>(utf16_length); |
| 9269 | // Must set length here so that hash computation is correct. |
| 9270 | hasher.length_ = utf16_length; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9271 | return hasher.GetHashField(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9272 | } |
| 9273 | |
| 9274 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9275 | MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9276 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9277 | if (start == 0 && end == length()) return this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9278 | MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9279 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9280 | } |
| 9281 | |
| 9282 | |
| 9283 | void String::PrintOn(FILE* file) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9284 | int length = this->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9285 | for (int i = 0; i < length; i++) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 9286 | PrintF(file, "%c", Get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9287 | } |
| 9288 | } |
| 9289 | |
| 9290 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9291 | static void TrimEnumCache(Heap* heap, Map* map, DescriptorArray* descriptors) { |
| 9292 | int live_enum = map->EnumLength(); |
| 9293 | if (live_enum == Map::kInvalidEnumCache) { |
| 9294 | live_enum = map->NumberOfDescribedProperties(OWN_DESCRIPTORS, DONT_ENUM); |
| 9295 | } |
| 9296 | if (live_enum == 0) return descriptors->ClearEnumCache(); |
| 9297 | |
| 9298 | FixedArray* enum_cache = descriptors->GetEnumCache(); |
| 9299 | |
| 9300 | int to_trim = enum_cache->length() - live_enum; |
| 9301 | if (to_trim <= 0) return; |
| 9302 | RightTrimFixedArray<FROM_GC>(heap, descriptors->GetEnumCache(), to_trim); |
| 9303 | |
| 9304 | if (!descriptors->HasEnumIndicesCache()) return; |
| 9305 | FixedArray* enum_indices_cache = descriptors->GetEnumIndicesCache(); |
| 9306 | RightTrimFixedArray<FROM_GC>(heap, enum_indices_cache, to_trim); |
| 9307 | } |
| 9308 | |
| 9309 | |
| 9310 | static void TrimDescriptorArray(Heap* heap, |
| 9311 | Map* map, |
| 9312 | DescriptorArray* descriptors, |
| 9313 | int number_of_own_descriptors) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9314 | int number_of_descriptors = descriptors->number_of_descriptors_storage(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9315 | int to_trim = number_of_descriptors - number_of_own_descriptors; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9316 | if (to_trim == 0) return; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9317 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9318 | RightTrimFixedArray<FROM_GC>( |
| 9319 | heap, descriptors, to_trim * DescriptorArray::kDescriptorSize); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9320 | descriptors->SetNumberOfDescriptors(number_of_own_descriptors); |
| 9321 | |
| 9322 | if (descriptors->HasEnumCache()) TrimEnumCache(heap, map, descriptors); |
| 9323 | descriptors->Sort(); |
| 9324 | } |
| 9325 | |
| 9326 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9327 | // Clear a possible back pointer in case the transition leads to a dead map. |
| 9328 | // 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] | 9329 | static bool ClearBackPointer(Heap* heap, Map* target) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9330 | if (Marking::MarkBitFrom(target).Get()) return false; |
| 9331 | target->SetBackPointer(heap->undefined_value(), SKIP_WRITE_BARRIER); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9332 | return true; |
| 9333 | } |
| 9334 | |
| 9335 | |
| 9336 | // TODO(mstarzinger): This method should be moved into MarkCompactCollector, |
| 9337 | // because it cannot be called from outside the GC and we already have methods |
| 9338 | // depending on the transitions layout in the GC anyways. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 9339 | void Map::ClearNonLiveTransitions(Heap* heap) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9340 | // If there are no transitions to be cleared, return. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9341 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 9342 | // properly cleared. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9343 | if (!HasTransitionArray()) return; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9344 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9345 | TransitionArray* t = transitions(); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 9346 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9347 | |
| 9348 | int transition_index = 0; |
| 9349 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9350 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9351 | bool descriptors_owner_died = false; |
| 9352 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9353 | // Compact all live descriptors to the left. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9354 | for (int i = 0; i < t->number_of_transitions(); ++i) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9355 | Map* target = t->GetTarget(i); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9356 | if (ClearBackPointer(heap, target)) { |
| 9357 | if (target->instance_descriptors() == descriptors) { |
| 9358 | descriptors_owner_died = true; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9359 | } |
| 9360 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9361 | if (i != transition_index) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 9362 | Name* key = t->GetKey(i); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9363 | t->SetKey(transition_index, key); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9364 | Object** key_slot = t->GetKeySlot(transition_index); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9365 | collector->RecordSlot(key_slot, key_slot, key); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 9366 | // Target slots do not need to be recorded since maps are not compacted. |
| 9367 | t->SetTarget(transition_index, t->GetTarget(i)); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9368 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9369 | transition_index++; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9370 | } |
| 9371 | } |
| 9372 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 9373 | // If there are no transitions to be cleared, return. |
| 9374 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 9375 | // properly cleared. |
| 9376 | if (transition_index == t->number_of_transitions()) return; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9377 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9378 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 9379 | |
| 9380 | if (descriptors_owner_died) { |
| 9381 | if (number_of_own_descriptors > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9382 | TrimDescriptorArray(heap, this, descriptors, number_of_own_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9383 | ASSERT(descriptors->number_of_descriptors() == number_of_own_descriptors); |
| 9384 | } else { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9385 | ASSERT(descriptors == GetHeap()->empty_descriptor_array()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9386 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9387 | } |
| 9388 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9389 | int trim = t->number_of_transitions() - transition_index; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9390 | if (trim > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9391 | RightTrimFixedArray<FROM_GC>(heap, t, t->IsSimpleTransition() |
| 9392 | ? trim : trim * TransitionArray::kTransitionSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9393 | } |
| 9394 | } |
| 9395 | |
| 9396 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9397 | int Map::Hash() { |
| 9398 | // For performance reasons we only hash the 3 most variable fields of a map: |
| 9399 | // constructor, prototype and bit_field2. |
| 9400 | |
| 9401 | // Shift away the tag. |
| 9402 | int hash = (static_cast<uint32_t>( |
| 9403 | reinterpret_cast<uintptr_t>(constructor())) >> 2); |
| 9404 | |
| 9405 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 9406 | // when the two pointers are close (which is fairly common). |
| 9407 | // To avoid this we shift the prototype 4 bits relatively to the constructor. |
| 9408 | hash ^= (static_cast<uint32_t>( |
| 9409 | reinterpret_cast<uintptr_t>(prototype())) << 2); |
| 9410 | |
| 9411 | return hash ^ (hash >> 16) ^ bit_field2(); |
| 9412 | } |
| 9413 | |
| 9414 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9415 | static bool CheckEquivalent(Map* first, Map* second) { |
| 9416 | return |
| 9417 | first->constructor() == second->constructor() && |
| 9418 | first->prototype() == second->prototype() && |
| 9419 | first->instance_type() == second->instance_type() && |
| 9420 | first->bit_field() == second->bit_field() && |
| 9421 | first->bit_field2() == second->bit_field2() && |
| 9422 | first->is_observed() == second->is_observed() && |
| 9423 | first->function_with_prototype() == second->function_with_prototype(); |
| 9424 | } |
| 9425 | |
| 9426 | |
| 9427 | bool Map::EquivalentToForTransition(Map* other) { |
| 9428 | return CheckEquivalent(this, other); |
| 9429 | } |
| 9430 | |
| 9431 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9432 | bool Map::EquivalentToForNormalization(Map* other, |
| 9433 | PropertyNormalizationMode mode) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9434 | int properties = mode == CLEAR_INOBJECT_PROPERTIES |
| 9435 | ? 0 : other->inobject_properties(); |
| 9436 | return CheckEquivalent(this, other) && inobject_properties() == properties; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
| 9439 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 9440 | void ConstantPoolArray::ConstantPoolIterateBody(ObjectVisitor* v) { |
| 9441 | int first_ptr_offset = OffsetOfElementAt(first_ptr_index()); |
| 9442 | int last_ptr_offset = |
| 9443 | OffsetOfElementAt(first_ptr_index() + count_of_ptr_entries()); |
| 9444 | v->VisitPointers( |
| 9445 | HeapObject::RawField(this, first_ptr_offset), |
| 9446 | HeapObject::RawField(this, last_ptr_offset)); |
| 9447 | } |
| 9448 | |
| 9449 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 9450 | void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) { |
| 9451 | // Iterate over all fields in the body but take care in dealing with |
| 9452 | // the code entry. |
| 9453 | IteratePointers(v, kPropertiesOffset, kCodeEntryOffset); |
| 9454 | v->VisitCodeEntry(this->address() + kCodeEntryOffset); |
| 9455 | IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size); |
| 9456 | } |
| 9457 | |
| 9458 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9459 | void JSFunction::MarkForLazyRecompilation() { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9460 | ASSERT(is_compiled() || GetIsolate()->DebuggerHasBreakPoints()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9461 | ASSERT(!IsOptimized()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9462 | ASSERT(shared()->allows_lazy_compilation() || |
| 9463 | code()->optimizable()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 9464 | ASSERT(!shared()->is_generator()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9465 | set_code_no_write_barrier( |
| 9466 | GetIsolate()->builtins()->builtin(Builtins::kLazyRecompile)); |
| 9467 | // 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] | 9468 | } |
| 9469 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9470 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9471 | void JSFunction::MarkForConcurrentRecompilation() { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9472 | ASSERT(is_compiled() || GetIsolate()->DebuggerHasBreakPoints()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9473 | ASSERT(!IsOptimized()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 9474 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 9475 | ASSERT(!shared()->is_generator()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9476 | ASSERT(FLAG_concurrent_recompilation); |
| 9477 | if (FLAG_trace_concurrent_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9478 | PrintF(" ** Marking "); |
| 9479 | PrintName(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9480 | PrintF(" for concurrent recompilation.\n"); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9481 | } |
| 9482 | set_code_no_write_barrier( |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9483 | GetIsolate()->builtins()->builtin(Builtins::kConcurrentRecompile)); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9484 | // 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] | 9485 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9486 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9487 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9488 | void JSFunction::MarkInRecompileQueue() { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9489 | // We can only arrive here via the concurrent-recompilation builtin. If |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9490 | // break points were set, the code would point to the lazy-compile builtin. |
| 9491 | ASSERT(!GetIsolate()->DebuggerHasBreakPoints()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9492 | ASSERT(IsMarkedForConcurrentRecompilation() && !IsOptimized()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9493 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9494 | ASSERT(FLAG_concurrent_recompilation); |
| 9495 | if (FLAG_trace_concurrent_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9496 | PrintF(" ** Queueing "); |
| 9497 | PrintName(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9498 | PrintF(" for concurrent recompilation.\n"); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9499 | } |
| 9500 | set_code_no_write_barrier( |
| 9501 | GetIsolate()->builtins()->builtin(Builtins::kInRecompileQueue)); |
| 9502 | // No write barrier required, since the builtin is part of the root set. |
| 9503 | } |
| 9504 | |
| 9505 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9506 | static bool CompileLazyHelper(CompilationInfo* info, |
| 9507 | ClearExceptionFlag flag) { |
| 9508 | // Compile the source information to a code object. |
| 9509 | ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled()); |
| 9510 | ASSERT(!info->isolate()->has_pending_exception()); |
| 9511 | bool result = Compiler::CompileLazy(info); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9512 | ASSERT(result != info->isolate()->has_pending_exception()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9513 | if (!result && flag == CLEAR_EXCEPTION) { |
| 9514 | info->isolate()->clear_pending_exception(); |
| 9515 | } |
| 9516 | return result; |
| 9517 | } |
| 9518 | |
| 9519 | |
| 9520 | bool SharedFunctionInfo::CompileLazy(Handle<SharedFunctionInfo> shared, |
| 9521 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9522 | ASSERT(shared->allows_lazy_compilation_without_context()); |
| 9523 | CompilationInfoWithZone info(shared); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9524 | return CompileLazyHelper(&info, flag); |
| 9525 | } |
| 9526 | |
| 9527 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9528 | void SharedFunctionInfo::AddToOptimizedCodeMap( |
| 9529 | Handle<SharedFunctionInfo> shared, |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9530 | Handle<Context> native_context, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9531 | Handle<Code> code, |
| 9532 | Handle<FixedArray> literals) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9533 | CALL_HEAP_FUNCTION_VOID( |
| 9534 | shared->GetIsolate(), |
| 9535 | shared->AddToOptimizedCodeMap(*native_context, *code, *literals)); |
| 9536 | } |
| 9537 | |
| 9538 | |
| 9539 | MaybeObject* SharedFunctionInfo::AddToOptimizedCodeMap(Context* native_context, |
| 9540 | Code* code, |
| 9541 | FixedArray* literals) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9542 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9543 | ASSERT(native_context->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9544 | STATIC_ASSERT(kEntryLength == 3); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9545 | Heap* heap = GetHeap(); |
| 9546 | FixedArray* new_code_map; |
| 9547 | Object* value = optimized_code_map(); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9548 | if (value->IsSmi()) { |
| 9549 | // No optimized code map. |
| 9550 | ASSERT_EQ(0, Smi::cast(value)->value()); |
| 9551 | // Crate 3 entries per context {context, code, literals}. |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9552 | MaybeObject* maybe = heap->AllocateFixedArray(kInitialLength); |
| 9553 | if (!maybe->To(&new_code_map)) return maybe; |
| 9554 | new_code_map->set(kEntriesStart + 0, native_context); |
| 9555 | new_code_map->set(kEntriesStart + 1, code); |
| 9556 | new_code_map->set(kEntriesStart + 2, literals); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9557 | } else { |
| 9558 | // Copy old map and append one new entry. |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9559 | FixedArray* old_code_map = FixedArray::cast(value); |
| 9560 | ASSERT_EQ(-1, SearchOptimizedCodeMap(native_context)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9561 | int old_length = old_code_map->length(); |
| 9562 | int new_length = old_length + kEntryLength; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9563 | MaybeObject* maybe = old_code_map->CopySize(new_length); |
| 9564 | if (!maybe->To(&new_code_map)) return maybe; |
| 9565 | new_code_map->set(old_length + 0, native_context); |
| 9566 | new_code_map->set(old_length + 1, code); |
| 9567 | new_code_map->set(old_length + 2, literals); |
| 9568 | // Zap the old map for the sake of the heap verifier. |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 9569 | if (Heap::ShouldZapGarbage()) { |
| 9570 | Object** data = old_code_map->data_start(); |
| 9571 | MemsetPointer(data, heap->the_hole_value(), old_length); |
| 9572 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9573 | } |
| 9574 | #ifdef DEBUG |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9575 | for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9576 | ASSERT(new_code_map->get(i)->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9577 | ASSERT(new_code_map->get(i + 1)->IsCode()); |
| 9578 | ASSERT(Code::cast(new_code_map->get(i + 1))->kind() == |
| 9579 | Code::OPTIMIZED_FUNCTION); |
| 9580 | ASSERT(new_code_map->get(i + 2)->IsFixedArray()); |
| 9581 | } |
| 9582 | #endif |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9583 | set_optimized_code_map(new_code_map); |
| 9584 | return new_code_map; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9585 | } |
| 9586 | |
| 9587 | |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9588 | void SharedFunctionInfo::InstallFromOptimizedCodeMap(JSFunction* function, |
| 9589 | int index) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9590 | ASSERT(index > kEntriesStart); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9591 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9592 | if (!bound()) { |
| 9593 | FixedArray* cached_literals = FixedArray::cast(code_map->get(index + 1)); |
| 9594 | ASSERT(cached_literals != NULL); |
| 9595 | function->set_literals(cached_literals); |
| 9596 | } |
| 9597 | Code* code = Code::cast(code_map->get(index)); |
| 9598 | ASSERT(code != NULL); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9599 | ASSERT(function->context()->native_context() == code_map->get(index - 1)); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9600 | function->ReplaceCode(code); |
| 9601 | } |
| 9602 | |
| 9603 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9604 | void SharedFunctionInfo::ClearOptimizedCodeMap() { |
| 9605 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9606 | |
| 9607 | // If the next map link slot is already used then the function was |
| 9608 | // enqueued with code flushing and we remove it now. |
| 9609 | if (!code_map->get(kNextMapIndex)->IsUndefined()) { |
| 9610 | CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher(); |
| 9611 | flusher->EvictOptimizedCodeMap(this); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9612 | } |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9613 | |
| 9614 | ASSERT(code_map->get(kNextMapIndex)->IsUndefined()); |
| 9615 | set_optimized_code_map(Smi::FromInt(0)); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9616 | } |
| 9617 | |
| 9618 | |
| 9619 | void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code, |
| 9620 | const char* reason) { |
| 9621 | if (optimized_code_map()->IsSmi()) return; |
| 9622 | |
| 9623 | int i; |
| 9624 | bool removed_entry = false; |
| 9625 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9626 | for (i = kEntriesStart; i < code_map->length(); i += kEntryLength) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9627 | ASSERT(code_map->get(i)->IsNativeContext()); |
| 9628 | if (Code::cast(code_map->get(i + 1)) == optimized_code) { |
| 9629 | if (FLAG_trace_opt) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9630 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9631 | ShortPrint(); |
| 9632 | PrintF("]\n"); |
| 9633 | } |
| 9634 | removed_entry = true; |
| 9635 | break; |
| 9636 | } |
| 9637 | } |
| 9638 | while (i < (code_map->length() - kEntryLength)) { |
| 9639 | code_map->set(i, code_map->get(i + kEntryLength)); |
| 9640 | code_map->set(i + 1, code_map->get(i + 1 + kEntryLength)); |
| 9641 | code_map->set(i + 2, code_map->get(i + 2 + kEntryLength)); |
| 9642 | i += kEntryLength; |
| 9643 | } |
| 9644 | if (removed_entry) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9645 | // Always trim even when array is cleared because of heap verifier. |
| 9646 | RightTrimFixedArray<FROM_MUTATOR>(GetHeap(), code_map, kEntryLength); |
| 9647 | if (code_map->length() == kEntriesStart) { |
| 9648 | ClearOptimizedCodeMap(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9649 | } |
| 9650 | } |
| 9651 | } |
| 9652 | |
| 9653 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9654 | void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) { |
| 9655 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9656 | ASSERT(shrink_by % kEntryLength == 0); |
| 9657 | ASSERT(shrink_by <= code_map->length() - kEntriesStart); |
| 9658 | // Always trim even when array is cleared because of heap verifier. |
| 9659 | RightTrimFixedArray<FROM_GC>(GetHeap(), code_map, shrink_by); |
| 9660 | if (code_map->length() == kEntriesStart) { |
| 9661 | ClearOptimizedCodeMap(); |
| 9662 | } |
| 9663 | } |
| 9664 | |
| 9665 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9666 | bool JSFunction::CompileLazy(Handle<JSFunction> function, |
| 9667 | ClearExceptionFlag flag) { |
| 9668 | bool result = true; |
| 9669 | if (function->shared()->is_compiled()) { |
| 9670 | function->ReplaceCode(function->shared()->code()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9671 | } else { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9672 | ASSERT(function->shared()->allows_lazy_compilation()); |
| 9673 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9674 | result = CompileLazyHelper(&info, flag); |
| 9675 | ASSERT(!result || function->is_compiled()); |
| 9676 | } |
| 9677 | return result; |
| 9678 | } |
| 9679 | |
| 9680 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9681 | Handle<Code> JSFunction::CompileOsr(Handle<JSFunction> function, |
| 9682 | BailoutId osr_ast_id, |
| 9683 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9684 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9685 | info.SetOptimizing(osr_ast_id); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9686 | if (CompileLazyHelper(&info, flag)) { |
| 9687 | // TODO(titzer): don't install the OSR code. |
| 9688 | // ASSERT(function->code() != *info.code()); |
| 9689 | return info.code(); |
| 9690 | } else { |
| 9691 | return Handle<Code>::null(); |
| 9692 | } |
| 9693 | } |
| 9694 | |
| 9695 | |
| 9696 | bool JSFunction::CompileOptimized(Handle<JSFunction> function, |
| 9697 | ClearExceptionFlag flag) { |
| 9698 | CompilationInfoWithZone info(function); |
| 9699 | info.SetOptimizing(BailoutId::None()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9700 | return CompileLazyHelper(&info, flag); |
| 9701 | } |
| 9702 | |
| 9703 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9704 | bool JSFunction::EnsureCompiled(Handle<JSFunction> function, |
| 9705 | ClearExceptionFlag flag) { |
| 9706 | return function->is_compiled() || CompileLazy(function, flag); |
| 9707 | } |
| 9708 | |
| 9709 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9710 | bool JSFunction::IsInlineable() { |
| 9711 | if (IsBuiltin()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9712 | SharedFunctionInfo* shared_info = shared(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9713 | // Check that the function has a script associated with it. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9714 | if (!shared_info->script()->IsScript()) return false; |
| 9715 | if (shared_info->optimization_disabled()) return false; |
| 9716 | Code* code = shared_info->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9717 | if (code->kind() == Code::OPTIMIZED_FUNCTION) return true; |
| 9718 | // If we never ran this (unlikely) then lets try to optimize it. |
| 9719 | if (code->kind() != Code::FUNCTION) return true; |
| 9720 | return code->optimizable(); |
| 9721 | } |
| 9722 | |
| 9723 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9724 | void JSObject::OptimizeAsPrototype(Handle<JSObject> object) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9725 | if (object->IsGlobalObject()) return; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9726 | |
| 9727 | // Make sure prototypes are fast objects and their maps have the bit set |
| 9728 | // so they remain fast. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9729 | if (!object->HasFastProperties()) { |
| 9730 | TransformToFastProperties(object, 0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9731 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9732 | } |
| 9733 | |
| 9734 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9735 | static MUST_USE_RESULT MaybeObject* CacheInitialJSArrayMaps( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9736 | Context* native_context, Map* initial_map) { |
| 9737 | // Replace all of the cached initial array maps in the native context with |
| 9738 | // the appropriate transitioned elements kind maps. |
| 9739 | Heap* heap = native_context->GetHeap(); |
| 9740 | MaybeObject* maybe_maps = |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9741 | heap->AllocateFixedArrayWithHoles(kElementsKindCount, TENURED); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9742 | FixedArray* maps; |
| 9743 | if (!maybe_maps->To(&maps)) return maybe_maps; |
| 9744 | |
| 9745 | Map* current_map = initial_map; |
| 9746 | ElementsKind kind = current_map->elements_kind(); |
| 9747 | ASSERT(kind == GetInitialFastElementsKind()); |
| 9748 | maps->set(kind, current_map); |
| 9749 | for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1; |
| 9750 | i < kFastElementsKindCount; ++i) { |
| 9751 | Map* new_map; |
| 9752 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9753 | if (current_map->HasElementsTransition()) { |
| 9754 | new_map = current_map->elements_transition_map(); |
| 9755 | ASSERT(new_map->elements_kind() == next_kind); |
| 9756 | } else { |
| 9757 | MaybeObject* maybe_new_map = |
| 9758 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 9759 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 9760 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9761 | maps->set(next_kind, new_map); |
| 9762 | current_map = new_map; |
| 9763 | } |
| 9764 | native_context->set_js_array_maps(maps); |
| 9765 | return initial_map; |
| 9766 | } |
| 9767 | |
| 9768 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9769 | Handle<Object> CacheInitialJSArrayMaps(Handle<Context> native_context, |
| 9770 | Handle<Map> initial_map) { |
| 9771 | CALL_HEAP_FUNCTION(native_context->GetIsolate(), |
| 9772 | CacheInitialJSArrayMaps(*native_context, *initial_map), |
| 9773 | Object); |
| 9774 | } |
| 9775 | |
| 9776 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9777 | void JSFunction::SetInstancePrototype(Handle<JSFunction> function, |
| 9778 | Handle<Object> value) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9779 | ASSERT(value->IsJSReceiver()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9780 | |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 9781 | // First some logic for the map of the prototype to make sure it is in fast |
| 9782 | // mode. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9783 | if (value->IsJSObject()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9784 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9785 | } |
| 9786 | |
| 9787 | // Now some logic for the maps of the objects that are created by using this |
| 9788 | // function as a constructor. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9789 | if (function->has_initial_map()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9790 | // If the function has allocated the initial map replace it with a |
| 9791 | // copy containing the new prototype. Also complete any in-object |
| 9792 | // slack tracking that is in progress at this point because it is |
| 9793 | // still tracking the old copy. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9794 | if (function->shared()->IsInobjectSlackTrackingInProgress()) { |
| 9795 | function->shared()->CompleteInobjectSlackTracking(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9796 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9797 | Handle<Map> new_map = Map::Copy(handle(function->initial_map())); |
| 9798 | new_map->set_prototype(*value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9799 | |
| 9800 | // If the function is used as the global Array function, cache the |
| 9801 | // initial map (and transitioned versions) in the native context. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9802 | Context* native_context = function->context()->native_context(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9803 | Object* array_function = native_context->get(Context::ARRAY_FUNCTION_INDEX); |
| 9804 | if (array_function->IsJSFunction() && |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9805 | *function == JSFunction::cast(array_function)) { |
| 9806 | CacheInitialJSArrayMaps(handle(native_context), new_map); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9807 | } |
| 9808 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9809 | function->set_initial_map(*new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9810 | } else { |
| 9811 | // Put the value in the initial map field until an initial map is |
| 9812 | // needed. At that point, a new initial map is created and the |
| 9813 | // prototype is put into the initial map where it belongs. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9814 | function->set_prototype_or_initial_map(*value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9815 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9816 | function->GetHeap()->ClearInstanceofCache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9817 | } |
| 9818 | |
| 9819 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9820 | void JSFunction::SetPrototype(Handle<JSFunction> function, |
| 9821 | Handle<Object> value) { |
| 9822 | ASSERT(function->should_have_prototype()); |
| 9823 | Handle<Object> construct_prototype = value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9824 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9825 | // 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] | 9826 | // constructor field so it can be accessed. Also, set the prototype |
| 9827 | // used for constructing objects to the original object prototype. |
| 9828 | // See ECMA-262 13.2.2. |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9829 | if (!value->IsJSReceiver()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9830 | // 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] | 9831 | // Remove map transitions because they point to maps with a |
| 9832 | // different prototype. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9833 | Handle<Map> new_map = Map::Copy(handle(function->map())); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 9834 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9835 | function->set_map(*new_map); |
| 9836 | new_map->set_constructor(*value); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9837 | new_map->set_non_instance_prototype(true); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9838 | Isolate* isolate = new_map->GetIsolate(); |
| 9839 | construct_prototype = handle( |
| 9840 | isolate->context()->native_context()->initial_object_prototype(), |
| 9841 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9842 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9843 | function->map()->set_non_instance_prototype(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9846 | return SetInstancePrototype(function, construct_prototype); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9847 | } |
| 9848 | |
| 9849 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9850 | void JSFunction::RemovePrototype() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9851 | Context* native_context = context()->native_context(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9852 | Map* no_prototype_map = shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9853 | ? native_context->function_without_prototype_map() |
| 9854 | : native_context->strict_mode_function_without_prototype_map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9855 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9856 | if (map() == no_prototype_map) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9857 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9858 | ASSERT(map() == (shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9859 | ? native_context->function_map() |
| 9860 | : native_context->strict_mode_function_map())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9861 | |
| 9862 | set_map(no_prototype_map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9863 | 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] | 9864 | } |
| 9865 | |
| 9866 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9867 | void JSFunction::SetInstanceClassName(String* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9868 | shared()->set_instance_class_name(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
| 9871 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9872 | void JSFunction::PrintName(FILE* out) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9873 | SmartArrayPointer<char> name = shared()->DebugName()->ToCString(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9874 | PrintF(out, "%s", *name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9875 | } |
| 9876 | |
| 9877 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9878 | Context* JSFunction::NativeContextFromLiterals(FixedArray* literals) { |
| 9879 | return Context::cast(literals->get(JSFunction::kLiteralNativeContextIndex)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9880 | } |
| 9881 | |
| 9882 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9883 | // The filter is a pattern that matches function names in this way: |
| 9884 | // "*" all; the default |
| 9885 | // "-" all but the top-level function |
| 9886 | // "-name" all but the function "name" |
| 9887 | // "" only the top-level function |
| 9888 | // "name" only the function "name" |
| 9889 | // "name*" only functions starting with "name" |
| 9890 | bool JSFunction::PassesFilter(const char* raw_filter) { |
| 9891 | if (*raw_filter == '*') return true; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9892 | String* name = shared()->DebugName(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9893 | Vector<const char> filter = CStrVector(raw_filter); |
| 9894 | if (filter.length() == 0) return name->length() == 0; |
| 9895 | if (filter[0] != '-' && name->IsUtf8EqualTo(filter)) return true; |
| 9896 | if (filter[0] == '-' && |
| 9897 | !name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) { |
| 9898 | return true; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9899 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9900 | if (filter[filter.length() - 1] == '*' && |
| 9901 | name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) { |
| 9902 | return true; |
| 9903 | } |
| 9904 | return false; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9905 | } |
| 9906 | |
| 9907 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9908 | MaybeObject* Oddball::Initialize(Heap* heap, |
| 9909 | const char* to_string, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9910 | Object* to_number, |
| 9911 | byte kind) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9912 | String* internalized_to_string; |
| 9913 | { MaybeObject* maybe_string = |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9914 | heap->InternalizeUtf8String( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9915 | CStrVector(to_string)); |
| 9916 | if (!maybe_string->To(&internalized_to_string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9917 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9918 | set_to_string(internalized_to_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9919 | set_to_number(to_number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9920 | set_kind(kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9921 | return this; |
| 9922 | } |
| 9923 | |
| 9924 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 9925 | String* SharedFunctionInfo::DebugName() { |
| 9926 | Object* n = name(); |
| 9927 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 9928 | return String::cast(n); |
| 9929 | } |
| 9930 | |
| 9931 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9932 | bool SharedFunctionInfo::HasSourceCode() { |
| 9933 | return !script()->IsUndefined() && |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 9934 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9935 | } |
| 9936 | |
| 9937 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 9938 | Handle<Object> SharedFunctionInfo::GetSourceCode() { |
| 9939 | if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value(); |
| 9940 | Handle<String> source(String::cast(Script::cast(script())->source())); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 9941 | return GetIsolate()->factory()->NewSubString( |
| 9942 | source, start_position(), end_position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9943 | } |
| 9944 | |
| 9945 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9946 | int SharedFunctionInfo::SourceSize() { |
| 9947 | return end_position() - start_position(); |
| 9948 | } |
| 9949 | |
| 9950 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 9951 | int SharedFunctionInfo::CalculateInstanceSize() { |
| 9952 | int instance_size = |
| 9953 | JSObject::kHeaderSize + |
| 9954 | expected_nof_properties() * kPointerSize; |
| 9955 | if (instance_size > JSObject::kMaxInstanceSize) { |
| 9956 | instance_size = JSObject::kMaxInstanceSize; |
| 9957 | } |
| 9958 | return instance_size; |
| 9959 | } |
| 9960 | |
| 9961 | |
| 9962 | int SharedFunctionInfo::CalculateInObjectProperties() { |
| 9963 | return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize; |
| 9964 | } |
| 9965 | |
| 9966 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9967 | // Support function for printing the source code to a StringStream |
| 9968 | // without any allocation in the heap. |
| 9969 | void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator, |
| 9970 | int max_length) { |
| 9971 | // For some native functions there is no source. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9972 | if (!HasSourceCode()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9973 | accumulator->Add("<No Source>"); |
| 9974 | return; |
| 9975 | } |
| 9976 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9977 | // Get the source for the script which this function came from. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9978 | // Don't use String::cast because we don't want more assertion errors while |
| 9979 | // we are already creating a stack dump. |
| 9980 | String* script_source = |
| 9981 | reinterpret_cast<String*>(Script::cast(script())->source()); |
| 9982 | |
| 9983 | if (!script_source->LooksValid()) { |
| 9984 | accumulator->Add("<Invalid Source>"); |
| 9985 | return; |
| 9986 | } |
| 9987 | |
| 9988 | if (!is_toplevel()) { |
| 9989 | accumulator->Add("function "); |
| 9990 | Object* name = this->name(); |
| 9991 | if (name->IsString() && String::cast(name)->length() > 0) { |
| 9992 | accumulator->PrintName(name); |
| 9993 | } |
| 9994 | } |
| 9995 | |
| 9996 | int len = end_position() - start_position(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9997 | if (len <= max_length || max_length < 0) { |
| 9998 | accumulator->Put(script_source, start_position(), end_position()); |
| 9999 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10000 | accumulator->Put(script_source, |
| 10001 | start_position(), |
| 10002 | start_position() + max_length); |
| 10003 | accumulator->Add("...\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10004 | } |
| 10005 | } |
| 10006 | |
| 10007 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10008 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 10009 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 10010 | ByteArray* code_relocation = code->relocation_info(); |
| 10011 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 10012 | int length = code_relocation->length(); |
| 10013 | if (length != recompiled_relocation->length()) return false; |
| 10014 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 10015 | recompiled_relocation->GetDataStartAddress(), |
| 10016 | length); |
| 10017 | return compare == 0; |
| 10018 | } |
| 10019 | |
| 10020 | |
| 10021 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
| 10022 | ASSERT(!has_deoptimization_support()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10023 | DisallowHeapAllocation no_allocation; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10024 | Code* code = this->code(); |
| 10025 | if (IsCodeEquivalent(code, recompiled)) { |
| 10026 | // Copy the deoptimization data from the recompiled code. |
| 10027 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 10028 | code->set_has_deoptimization_support(true); |
| 10029 | } else { |
| 10030 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 10031 | // old code, we have to replace it. We should try to avoid this |
| 10032 | // altogether because it flushes valuable type feedback by |
| 10033 | // effectively resetting all IC state. |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 10034 | ReplaceCode(recompiled); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10035 | } |
| 10036 | ASSERT(has_deoptimization_support()); |
| 10037 | } |
| 10038 | |
| 10039 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 10040 | void SharedFunctionInfo::DisableOptimization(BailoutReason reason) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10041 | // Disable optimization for the shared function info and mark the |
| 10042 | // code as non-optimizable. The marker on the shared function info |
| 10043 | // is there because we flush non-optimized code thereby loosing the |
| 10044 | // non-optimizable information for the code. When the code is |
| 10045 | // regenerated and set on the shared function info it is marked as |
| 10046 | // non-optimizable if optimization is disabled for the shared |
| 10047 | // function info. |
| 10048 | set_optimization_disabled(true); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 10049 | set_bailout_reason(reason); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10050 | // Code should be the lazy compilation stub or else unoptimized. If the |
| 10051 | // latter, disable optimization for the code too. |
| 10052 | ASSERT(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN); |
| 10053 | if (code()->kind() == Code::FUNCTION) { |
| 10054 | code()->set_optimizable(false); |
| 10055 | } |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 10056 | PROFILE(GetIsolate(), |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 10057 | LogExistingFunction(Handle<SharedFunctionInfo>(this), |
| 10058 | Handle<Code>(code()))); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10059 | if (FLAG_trace_opt) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10060 | PrintF("[disabled optimization for "); |
| 10061 | ShortPrint(); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 10062 | PrintF(", reason: %s]\n", GetBailoutReason(reason)); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10063 | } |
| 10064 | } |
| 10065 | |
| 10066 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10067 | bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) { |
| 10068 | ASSERT(!id.IsNone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10069 | Code* unoptimized = code(); |
| 10070 | DeoptimizationOutputData* data = |
| 10071 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 10072 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 10073 | USE(ignore); |
| 10074 | return true; // Return true if there was no ASSERT. |
| 10075 | } |
| 10076 | |
| 10077 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10078 | void SharedFunctionInfo::StartInobjectSlackTracking(Map* map) { |
| 10079 | ASSERT(!IsInobjectSlackTrackingInProgress()); |
| 10080 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10081 | if (!FLAG_clever_optimizations) return; |
| 10082 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10083 | // Only initiate the tracking the first time. |
| 10084 | if (live_objects_may_exist()) return; |
| 10085 | set_live_objects_may_exist(true); |
| 10086 | |
| 10087 | // No tracking during the snapshot construction phase. |
| 10088 | if (Serializer::enabled()) return; |
| 10089 | |
| 10090 | if (map->unused_property_fields() == 0) return; |
| 10091 | |
| 10092 | // Nonzero counter is a leftover from the previous attempt interrupted |
| 10093 | // by GC, keep it. |
| 10094 | if (construction_count() == 0) { |
| 10095 | set_construction_count(kGenerousAllocationCount); |
| 10096 | } |
| 10097 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10098 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10099 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10100 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10101 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10102 | } |
| 10103 | |
| 10104 | |
| 10105 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 10106 | void SharedFunctionInfo::DetachInitialMap() { |
| 10107 | Map* map = reinterpret_cast<Map*>(initial_map()); |
| 10108 | |
| 10109 | // Make the map remember to restore the link if it survives the GC. |
| 10110 | map->set_bit_field2( |
| 10111 | map->bit_field2() | (1 << Map::kAttachedToSharedFunctionInfo)); |
| 10112 | |
| 10113 | // Undo state changes made by StartInobjectTracking (except the |
| 10114 | // construction_count). This way if the initial map does not survive the GC |
| 10115 | // then StartInobjectTracking will be called again the next time the |
| 10116 | // constructor is called. The countdown will continue and (possibly after |
| 10117 | // several more GCs) CompleteInobjectSlackTracking will eventually be called. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10118 | Heap* heap = map->GetHeap(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 10119 | set_initial_map(heap->undefined_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10120 | Builtins* builtins = heap->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10121 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10122 | *RawField(this, kConstructStubOffset)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10123 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10124 | // It is safe to clear the flag: it will be set again if the map is live. |
| 10125 | set_live_objects_may_exist(false); |
| 10126 | } |
| 10127 | |
| 10128 | |
| 10129 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 10130 | void SharedFunctionInfo::AttachInitialMap(Map* map) { |
| 10131 | map->set_bit_field2( |
| 10132 | map->bit_field2() & ~(1 << Map::kAttachedToSharedFunctionInfo)); |
| 10133 | |
| 10134 | // Resume inobject slack tracking. |
| 10135 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10136 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10137 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10138 | *RawField(this, kConstructStubOffset)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10139 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10140 | // The map survived the gc, so there may be objects referencing it. |
| 10141 | set_live_objects_may_exist(true); |
| 10142 | } |
| 10143 | |
| 10144 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10145 | void SharedFunctionInfo::ResetForNewContext(int new_ic_age) { |
| 10146 | code()->ClearInlineCaches(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10147 | set_ic_age(new_ic_age); |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 10148 | if (code()->kind() == Code::FUNCTION) { |
| 10149 | code()->set_profiler_ticks(0); |
| 10150 | if (optimization_disabled() && |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 10151 | opt_count() >= FLAG_max_opt_count) { |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 10152 | // Re-enable optimizations if they were disabled due to opt_count limit. |
| 10153 | set_optimization_disabled(false); |
| 10154 | code()->set_optimizable(true); |
| 10155 | } |
| 10156 | set_opt_count(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10157 | set_deopt_count(0); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10158 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10159 | } |
| 10160 | |
| 10161 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10162 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 10163 | int slack = map->unused_property_fields(); |
| 10164 | if (*reinterpret_cast<int*>(data) > slack) { |
| 10165 | *reinterpret_cast<int*>(data) = slack; |
| 10166 | } |
| 10167 | } |
| 10168 | |
| 10169 | |
| 10170 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 10171 | int slack = *reinterpret_cast<int*>(data); |
| 10172 | map->set_inobject_properties(map->inobject_properties() - slack); |
| 10173 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 10174 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
| 10175 | |
| 10176 | // Visitor id might depend on the instance size, recalculate it. |
| 10177 | map->set_visitor_id(StaticVisitorBase::GetVisitorId(map)); |
| 10178 | } |
| 10179 | |
| 10180 | |
| 10181 | void SharedFunctionInfo::CompleteInobjectSlackTracking() { |
| 10182 | ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress()); |
| 10183 | Map* map = Map::cast(initial_map()); |
| 10184 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10185 | Heap* heap = map->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10186 | set_initial_map(heap->undefined_value()); |
| 10187 | Builtins* builtins = heap->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10188 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10189 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10190 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10191 | |
| 10192 | int slack = map->unused_property_fields(); |
| 10193 | map->TraverseTransitionTree(&GetMinInobjectSlack, &slack); |
| 10194 | if (slack != 0) { |
| 10195 | // Resize the initial map and all maps in its transition tree. |
| 10196 | map->TraverseTransitionTree(&ShrinkInstanceSize, &slack); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10197 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10198 | // Give the correct expected_nof_properties to initial maps created later. |
| 10199 | ASSERT(expected_nof_properties() >= slack); |
| 10200 | set_expected_nof_properties(expected_nof_properties() - slack); |
| 10201 | } |
| 10202 | } |
| 10203 | |
| 10204 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10205 | int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context) { |
| 10206 | ASSERT(native_context->IsNativeContext()); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 10207 | if (!FLAG_cache_optimized_code) return -1; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10208 | Object* value = optimized_code_map(); |
| 10209 | if (!value->IsSmi()) { |
| 10210 | FixedArray* optimized_code_map = FixedArray::cast(value); |
| 10211 | int length = optimized_code_map->length(); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 10212 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10213 | if (optimized_code_map->get(i) == native_context) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10214 | return i + 1; |
| 10215 | } |
| 10216 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10217 | if (FLAG_trace_opt) { |
| 10218 | PrintF("[didn't find optimized code in optimized code map for "); |
| 10219 | ShortPrint(); |
| 10220 | PrintF("]\n"); |
| 10221 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10222 | } |
| 10223 | return -1; |
| 10224 | } |
| 10225 | |
| 10226 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 10227 | #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 10228 | const char* const VisitorSynchronization::kTags[ |
| 10229 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 10230 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 10231 | }; |
| 10232 | #undef DECLARE_TAG |
| 10233 | |
| 10234 | |
| 10235 | #define DECLARE_TAG(ignore1, ignore2, name) name, |
| 10236 | const char* const VisitorSynchronization::kTagNames[ |
| 10237 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 10238 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 10239 | }; |
| 10240 | #undef DECLARE_TAG |
| 10241 | |
| 10242 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10243 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10244 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10245 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 10246 | Object* old_target = target; |
| 10247 | VisitPointer(&target); |
| 10248 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10249 | } |
| 10250 | |
| 10251 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10252 | void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 10253 | ASSERT(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 10254 | Object* stub = rinfo->code_age_stub(); |
| 10255 | if (stub) { |
| 10256 | VisitPointer(&stub); |
| 10257 | } |
| 10258 | } |
| 10259 | |
| 10260 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 10261 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 10262 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 10263 | Object* old_code = code; |
| 10264 | VisitPointer(&code); |
| 10265 | if (code != old_code) { |
| 10266 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 10267 | } |
| 10268 | } |
| 10269 | |
| 10270 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10271 | void ObjectVisitor::VisitCell(RelocInfo* rinfo) { |
| 10272 | ASSERT(rinfo->rmode() == RelocInfo::CELL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10273 | Object* cell = rinfo->target_cell(); |
| 10274 | Object* old_cell = cell; |
| 10275 | VisitPointer(&cell); |
| 10276 | if (cell != old_cell) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10277 | rinfo->set_target_cell(reinterpret_cast<Cell*>(cell)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10278 | } |
| 10279 | } |
| 10280 | |
| 10281 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10282 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 10283 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 10284 | rinfo->IsPatchedReturnSequence()) || |
| 10285 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 10286 | rinfo->IsPatchedDebugBreakSlotSequence())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10287 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 10288 | Object* old_target = target; |
| 10289 | VisitPointer(&target); |
| 10290 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10291 | } |
| 10292 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10293 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10294 | void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) { |
| 10295 | ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
| 10296 | VisitPointer(rinfo->target_object_address()); |
| 10297 | } |
| 10298 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10299 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 10300 | void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) { |
| 10301 | Address* p = rinfo->target_reference_address(); |
| 10302 | VisitExternalReferences(p, p + 1); |
| 10303 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10304 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10305 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10306 | void Code::InvalidateRelocation() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10307 | set_relocation_info(GetHeap()->empty_byte_array()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10308 | } |
| 10309 | |
| 10310 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10311 | void Code::Relocate(intptr_t delta) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10312 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
| 10313 | it.rinfo()->apply(delta); |
| 10314 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 10315 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10316 | } |
| 10317 | |
| 10318 | |
| 10319 | void Code::CopyFrom(const CodeDesc& desc) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10320 | ASSERT(Marking::Color(this) == Marking::WHITE_OBJECT); |
| 10321 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10322 | // copy code |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 10323 | CopyBytes(instruction_start(), desc.buffer, |
| 10324 | static_cast<size_t>(desc.instr_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10325 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10326 | // copy reloc info |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 10327 | CopyBytes(relocation_start(), |
| 10328 | desc.buffer + desc.buffer_size - desc.reloc_size, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 10329 | static_cast<size_t>(desc.reloc_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10330 | |
| 10331 | // unbox handles and relocate |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 10332 | intptr_t delta = instruction_start() - desc.buffer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10333 | int mode_mask = RelocInfo::kCodeTargetMask | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10334 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10335 | RelocInfo::ModeMask(RelocInfo::CELL) | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10336 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10337 | RelocInfo::kApplyMask; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10338 | // Needed to find target_object and runtime_entry on X64 |
| 10339 | Assembler* origin = desc.origin; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10340 | AllowDeferredHandleDereference embedding_raw_address; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10341 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10342 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 10343 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 10344 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10345 | it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10346 | } else if (mode == RelocInfo::CELL) { |
| 10347 | Handle<Cell> cell = it.rinfo()->target_cell_handle(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10348 | it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10349 | } else if (RelocInfo::IsCodeTarget(mode)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10350 | // rewrite code handles in inline cache targets to direct |
| 10351 | // pointers to the first instruction in the code object |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 10352 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10353 | Code* code = Code::cast(*p); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10354 | it.rinfo()->set_target_address(code->instruction_start(), |
| 10355 | SKIP_WRITE_BARRIER); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10356 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
| 10357 | Address p = it.rinfo()->target_runtime_entry(origin); |
| 10358 | it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10359 | } else { |
| 10360 | it.rinfo()->apply(delta); |
| 10361 | } |
| 10362 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 10363 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10364 | } |
| 10365 | |
| 10366 | |
| 10367 | // Locate the source position which is closest to the address in the code. This |
| 10368 | // is using the source position information embedded in the relocation info. |
| 10369 | // The position returned is relative to the beginning of the script where the |
| 10370 | // source for this function is found. |
| 10371 | int Code::SourcePosition(Address pc) { |
| 10372 | int distance = kMaxInt; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10373 | int position = RelocInfo::kNoPosition; // Initially no position found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10374 | // Run through all the relocation info to find the best matching source |
| 10375 | // position. All the code needs to be considered as the sequence of the |
| 10376 | // instructions in the code does not necessarily follow the same order as the |
| 10377 | // source. |
| 10378 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 10379 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10380 | // Only look at positions after the current pc. |
| 10381 | if (it.rinfo()->pc() < pc) { |
| 10382 | // Get position and distance. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10383 | |
| 10384 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 10385 | int pos = static_cast<int>(it.rinfo()->data()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10386 | // If this position is closer than the current candidate or if it has the |
| 10387 | // same distance as the current candidate and the position is higher then |
| 10388 | // this position is the new candidate. |
| 10389 | if ((dist < distance) || |
| 10390 | (dist == distance && pos > position)) { |
| 10391 | position = pos; |
| 10392 | distance = dist; |
| 10393 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10394 | } |
| 10395 | it.next(); |
| 10396 | } |
| 10397 | return position; |
| 10398 | } |
| 10399 | |
| 10400 | |
| 10401 | // Same as Code::SourcePosition above except it only looks for statement |
| 10402 | // positions. |
| 10403 | int Code::SourceStatementPosition(Address pc) { |
| 10404 | // First find the position as close as possible using all position |
| 10405 | // information. |
| 10406 | int position = SourcePosition(pc); |
| 10407 | // Now find the closest statement position before the position. |
| 10408 | int statement_position = 0; |
| 10409 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 10410 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10411 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10412 | int p = static_cast<int>(it.rinfo()->data()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10413 | if (statement_position < p && p <= position) { |
| 10414 | statement_position = p; |
| 10415 | } |
| 10416 | } |
| 10417 | it.next(); |
| 10418 | } |
| 10419 | return statement_position; |
| 10420 | } |
| 10421 | |
| 10422 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10423 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10424 | SafepointTable table(this); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10425 | return table.FindEntry(pc); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10426 | } |
| 10427 | |
| 10428 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10429 | Object* Code::FindNthObject(int n, Map* match_map) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10430 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10431 | DisallowHeapAllocation no_allocation; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10432 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10433 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10434 | RelocInfo* info = it.rinfo(); |
| 10435 | Object* object = info->target_object(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10436 | if (object->IsHeapObject()) { |
| 10437 | if (HeapObject::cast(object)->map() == match_map) { |
| 10438 | if (--n == 0) return object; |
| 10439 | } |
| 10440 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10441 | } |
| 10442 | return NULL; |
| 10443 | } |
| 10444 | |
| 10445 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10446 | Map* Code::FindFirstMap() { |
| 10447 | Object* result = FindNthObject(1, GetHeap()->meta_map()); |
| 10448 | return (result != NULL) ? Map::cast(result) : NULL; |
| 10449 | } |
| 10450 | |
| 10451 | |
| 10452 | void Code::ReplaceNthObject(int n, |
| 10453 | Map* match_map, |
| 10454 | Object* replace_with) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10455 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10456 | DisallowHeapAllocation no_allocation; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10457 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10458 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10459 | RelocInfo* info = it.rinfo(); |
| 10460 | Object* object = info->target_object(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10461 | if (object->IsHeapObject()) { |
| 10462 | if (HeapObject::cast(object)->map() == match_map) { |
| 10463 | if (--n == 0) { |
| 10464 | info->set_target_object(replace_with); |
| 10465 | return; |
| 10466 | } |
| 10467 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10468 | } |
| 10469 | } |
| 10470 | UNREACHABLE(); |
| 10471 | } |
| 10472 | |
| 10473 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10474 | void Code::FindAllMaps(MapHandleList* maps) { |
| 10475 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10476 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10477 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10478 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10479 | RelocInfo* info = it.rinfo(); |
| 10480 | Object* object = info->target_object(); |
| 10481 | if (object->IsMap()) maps->Add(Handle<Map>(Map::cast(object))); |
| 10482 | } |
| 10483 | } |
| 10484 | |
| 10485 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10486 | void Code::ReplaceFirstMap(Map* replace_with) { |
| 10487 | ReplaceNthObject(1, GetHeap()->meta_map(), replace_with); |
| 10488 | } |
| 10489 | |
| 10490 | |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10491 | Code* Code::FindFirstHandler() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10492 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10493 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10494 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 10495 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10496 | RelocInfo* info = it.rinfo(); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10497 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 10498 | if (code->kind() == Code::HANDLER) return code; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10499 | } |
| 10500 | return NULL; |
| 10501 | } |
| 10502 | |
| 10503 | |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10504 | bool Code::FindHandlers(CodeHandleList* code_list, int length) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10505 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10506 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10507 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 10508 | int i = 0; |
| 10509 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10510 | if (i == length) return true; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10511 | RelocInfo* info = it.rinfo(); |
| 10512 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10513 | // IC stubs with handlers never contain non-handler code objects before |
| 10514 | // handler targets. |
| 10515 | if (code->kind() != Code::HANDLER) break; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10516 | code_list->Add(Handle<Code>(code)); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10517 | i++; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10518 | } |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10519 | return i == length; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10520 | } |
| 10521 | |
| 10522 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10523 | Name* Code::FindFirstName() { |
| 10524 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10525 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10526 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10527 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10528 | RelocInfo* info = it.rinfo(); |
| 10529 | Object* object = info->target_object(); |
| 10530 | if (object->IsName()) return Name::cast(object); |
| 10531 | } |
| 10532 | return NULL; |
| 10533 | } |
| 10534 | |
| 10535 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10536 | void Code::ReplaceNthCell(int n, Cell* replace_with) { |
| 10537 | ASSERT(is_inline_cache_stub()); |
| 10538 | DisallowHeapAllocation no_allocation; |
| 10539 | int mask = RelocInfo::ModeMask(RelocInfo::CELL); |
| 10540 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10541 | RelocInfo* info = it.rinfo(); |
| 10542 | if (--n == 0) { |
| 10543 | info->set_target_cell(replace_with); |
| 10544 | return; |
| 10545 | } |
| 10546 | } |
| 10547 | UNREACHABLE(); |
| 10548 | } |
| 10549 | |
| 10550 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10551 | void Code::ClearInlineCaches() { |
| 10552 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 10553 | RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) | |
| 10554 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID) | |
| 10555 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_CONTEXT); |
| 10556 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10557 | RelocInfo* info = it.rinfo(); |
| 10558 | Code* target(Code::GetCodeFromTargetAddress(info->target_address())); |
| 10559 | if (target->is_inline_cache_stub()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10560 | IC::Clear(this->GetIsolate(), info->pc()); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10561 | } |
| 10562 | } |
| 10563 | } |
| 10564 | |
| 10565 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10566 | void Code::ClearTypeFeedbackCells(Heap* heap) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10567 | if (kind() != FUNCTION) return; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10568 | Object* raw_info = type_feedback_info(); |
| 10569 | if (raw_info->IsTypeFeedbackInfo()) { |
| 10570 | TypeFeedbackCells* type_feedback_cells = |
| 10571 | TypeFeedbackInfo::cast(raw_info)->type_feedback_cells(); |
| 10572 | for (int i = 0; i < type_feedback_cells->CellCount(); i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10573 | Cell* cell = type_feedback_cells->GetCell(i); |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 10574 | // Don't clear AllocationSites |
| 10575 | Object* value = cell->value(); |
| 10576 | if (value == NULL || !value->IsAllocationSite()) { |
| 10577 | cell->set_value(TypeFeedbackCells::RawUninitializedSentinel(heap)); |
| 10578 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10579 | } |
| 10580 | } |
| 10581 | } |
| 10582 | |
| 10583 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10584 | BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) { |
| 10585 | DisallowHeapAllocation no_gc; |
| 10586 | ASSERT(kind() == FUNCTION); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10587 | BackEdgeTable back_edges(this, &no_gc); |
| 10588 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 10589 | if (back_edges.pc_offset(i) == pc_offset) return back_edges.ast_id(i); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10590 | } |
| 10591 | return BailoutId::None(); |
| 10592 | } |
| 10593 | |
| 10594 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10595 | void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) { |
| 10596 | PatchPlatformCodeAge(isolate, sequence, kNoAge, NO_MARKING_PARITY); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10597 | } |
| 10598 | |
| 10599 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10600 | void Code::MakeOlder(MarkingParity current_parity) { |
| 10601 | byte* sequence = FindCodeAgeSequence(); |
| 10602 | if (sequence != NULL) { |
| 10603 | Age age; |
| 10604 | MarkingParity code_parity; |
| 10605 | GetCodeAgeAndParity(sequence, &age, &code_parity); |
| 10606 | if (age != kLastCodeAge && code_parity != current_parity) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10607 | PatchPlatformCodeAge(GetIsolate(), |
| 10608 | sequence, |
| 10609 | static_cast<Age>(age + 1), |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10610 | current_parity); |
| 10611 | } |
| 10612 | } |
| 10613 | } |
| 10614 | |
| 10615 | |
| 10616 | bool Code::IsOld() { |
| 10617 | byte* sequence = FindCodeAgeSequence(); |
| 10618 | if (sequence == NULL) return false; |
| 10619 | Age age; |
| 10620 | MarkingParity parity; |
| 10621 | GetCodeAgeAndParity(sequence, &age, &parity); |
| 10622 | return age >= kSexagenarianCodeAge; |
| 10623 | } |
| 10624 | |
| 10625 | |
| 10626 | byte* Code::FindCodeAgeSequence() { |
| 10627 | return FLAG_age_code && |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10628 | prologue_offset() != kPrologueOffsetNotSet && |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10629 | (kind() == OPTIMIZED_FUNCTION || |
| 10630 | (kind() == FUNCTION && !has_debug_break_slots())) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10631 | ? instruction_start() + prologue_offset() |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10632 | : NULL; |
| 10633 | } |
| 10634 | |
| 10635 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10636 | int Code::GetAge() { |
| 10637 | byte* sequence = FindCodeAgeSequence(); |
| 10638 | if (sequence == NULL) { |
| 10639 | return Code::kNoAge; |
| 10640 | } |
| 10641 | Age age; |
| 10642 | MarkingParity parity; |
| 10643 | GetCodeAgeAndParity(sequence, &age, &parity); |
| 10644 | return age; |
| 10645 | } |
| 10646 | |
| 10647 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10648 | void Code::GetCodeAgeAndParity(Code* code, Age* age, |
| 10649 | MarkingParity* parity) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10650 | Isolate* isolate = code->GetIsolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10651 | Builtins* builtins = isolate->builtins(); |
| 10652 | Code* stub = NULL; |
| 10653 | #define HANDLE_CODE_AGE(AGE) \ |
| 10654 | stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \ |
| 10655 | if (code == stub) { \ |
| 10656 | *age = k##AGE##CodeAge; \ |
| 10657 | *parity = EVEN_MARKING_PARITY; \ |
| 10658 | return; \ |
| 10659 | } \ |
| 10660 | stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 10661 | if (code == stub) { \ |
| 10662 | *age = k##AGE##CodeAge; \ |
| 10663 | *parity = ODD_MARKING_PARITY; \ |
| 10664 | return; \ |
| 10665 | } |
| 10666 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10667 | #undef HANDLE_CODE_AGE |
| 10668 | UNREACHABLE(); |
| 10669 | } |
| 10670 | |
| 10671 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10672 | Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10673 | Builtins* builtins = isolate->builtins(); |
| 10674 | switch (age) { |
| 10675 | #define HANDLE_CODE_AGE(AGE) \ |
| 10676 | case k##AGE##CodeAge: { \ |
| 10677 | Code* stub = parity == EVEN_MARKING_PARITY \ |
| 10678 | ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \ |
| 10679 | : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 10680 | return stub; \ |
| 10681 | } |
| 10682 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10683 | #undef HANDLE_CODE_AGE |
| 10684 | default: |
| 10685 | UNREACHABLE(); |
| 10686 | break; |
| 10687 | } |
| 10688 | return NULL; |
| 10689 | } |
| 10690 | |
| 10691 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10692 | void Code::PrintDeoptLocation(int bailout_id) { |
| 10693 | const char* last_comment = NULL; |
| 10694 | int mask = RelocInfo::ModeMask(RelocInfo::COMMENT) |
| 10695 | | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY); |
| 10696 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10697 | RelocInfo* info = it.rinfo(); |
| 10698 | if (info->rmode() == RelocInfo::COMMENT) { |
| 10699 | last_comment = reinterpret_cast<const char*>(info->data()); |
danno@chromium.org | aefd607 | 2013-05-14 14:11:47 +0000 | [diff] [blame] | 10700 | } else if (last_comment != NULL) { |
| 10701 | if ((bailout_id == Deoptimizer::GetDeoptimizationId( |
| 10702 | GetIsolate(), info->target_address(), Deoptimizer::EAGER)) || |
| 10703 | (bailout_id == Deoptimizer::GetDeoptimizationId( |
| 10704 | GetIsolate(), info->target_address(), Deoptimizer::SOFT))) { |
| 10705 | CHECK(RelocInfo::IsRuntimeEntry(info->rmode())); |
| 10706 | PrintF(" %s\n", last_comment); |
| 10707 | return; |
| 10708 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10709 | } |
| 10710 | } |
| 10711 | } |
| 10712 | |
| 10713 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 10714 | bool Code::CanDeoptAt(Address pc) { |
| 10715 | DeoptimizationInputData* deopt_data = |
| 10716 | DeoptimizationInputData::cast(deoptimization_data()); |
| 10717 | Address code_start_address = instruction_start(); |
| 10718 | for (int i = 0; i < deopt_data->DeoptCount(); i++) { |
| 10719 | if (deopt_data->Pc(i)->value() == -1) continue; |
| 10720 | Address address = code_start_address + deopt_data->Pc(i)->value(); |
| 10721 | if (address == pc) return true; |
| 10722 | } |
| 10723 | return false; |
| 10724 | } |
| 10725 | |
| 10726 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10727 | // Identify kind of code. |
| 10728 | const char* Code::Kind2String(Kind kind) { |
| 10729 | switch (kind) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10730 | #define CASE(name) case name: return #name; |
| 10731 | CODE_KIND_LIST(CASE) |
| 10732 | #undef CASE |
| 10733 | case NUMBER_OF_KINDS: break; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10734 | } |
| 10735 | UNREACHABLE(); |
| 10736 | return NULL; |
| 10737 | } |
| 10738 | |
| 10739 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10740 | #ifdef ENABLE_DISASSEMBLER |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10741 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10742 | void DeoptimizationInputData::DeoptimizationInputDataPrint(FILE* out) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10743 | disasm::NameConverter converter; |
| 10744 | int deopt_count = DeoptCount(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10745 | PrintF(out, "Deoptimization Input Data (deopt points = %d)\n", deopt_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10746 | if (0 == deopt_count) return; |
| 10747 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10748 | 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] | 10749 | FLAG_print_code_verbose ? "commands" : ""); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10750 | for (int i = 0; i < deopt_count; i++) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10751 | PrintF(out, "%6d %6d %6d %6d", |
| 10752 | i, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10753 | AstId(i).ToInt(), |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10754 | ArgumentsStackHeight(i)->value(), |
| 10755 | Pc(i)->value()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10756 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10757 | if (!FLAG_print_code_verbose) { |
| 10758 | PrintF(out, "\n"); |
| 10759 | continue; |
| 10760 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10761 | // Print details of the frame translation. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10762 | int translation_index = TranslationIndex(i)->value(); |
| 10763 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 10764 | Translation::Opcode opcode = |
| 10765 | static_cast<Translation::Opcode>(iterator.Next()); |
| 10766 | ASSERT(Translation::BEGIN == opcode); |
| 10767 | int frame_count = iterator.Next(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10768 | int jsframe_count = iterator.Next(); |
| 10769 | PrintF(out, " %s {frame count=%d, js frame count=%d}\n", |
| 10770 | Translation::StringFor(opcode), |
| 10771 | frame_count, |
| 10772 | jsframe_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10773 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10774 | while (iterator.HasNext() && |
| 10775 | Translation::BEGIN != |
| 10776 | (opcode = static_cast<Translation::Opcode>(iterator.Next()))) { |
| 10777 | PrintF(out, "%24s %s ", "", Translation::StringFor(opcode)); |
| 10778 | |
| 10779 | switch (opcode) { |
| 10780 | case Translation::BEGIN: |
| 10781 | UNREACHABLE(); |
| 10782 | break; |
| 10783 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10784 | case Translation::JS_FRAME: { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10785 | int ast_id = iterator.Next(); |
| 10786 | int function_id = iterator.Next(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10787 | unsigned height = iterator.Next(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 10788 | PrintF(out, "{ast_id=%d, function=", ast_id); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10789 | if (function_id != Translation::kSelfLiteralId) { |
| 10790 | Object* function = LiteralArray()->get(function_id); |
| 10791 | JSFunction::cast(function)->PrintName(out); |
| 10792 | } else { |
| 10793 | PrintF(out, "<self>"); |
| 10794 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10795 | PrintF(out, ", height=%u}", height); |
| 10796 | break; |
| 10797 | } |
| 10798 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10799 | case Translation::COMPILED_STUB_FRAME: { |
| 10800 | Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next()); |
| 10801 | PrintF(out, "{kind=%d}", stub_kind); |
| 10802 | break; |
| 10803 | } |
| 10804 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10805 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
| 10806 | case Translation::CONSTRUCT_STUB_FRAME: { |
| 10807 | int function_id = iterator.Next(); |
| 10808 | JSFunction* function = |
| 10809 | JSFunction::cast(LiteralArray()->get(function_id)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10810 | unsigned height = iterator.Next(); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10811 | PrintF(out, "{function="); |
| 10812 | function->PrintName(out); |
| 10813 | PrintF(out, ", height=%u}", height); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10814 | break; |
| 10815 | } |
| 10816 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 10817 | case Translation::GETTER_STUB_FRAME: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10818 | case Translation::SETTER_STUB_FRAME: { |
| 10819 | int function_id = iterator.Next(); |
| 10820 | JSFunction* function = |
| 10821 | JSFunction::cast(LiteralArray()->get(function_id)); |
| 10822 | PrintF(out, "{function="); |
| 10823 | function->PrintName(out); |
| 10824 | PrintF(out, "}"); |
| 10825 | break; |
| 10826 | } |
| 10827 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10828 | case Translation::REGISTER: { |
| 10829 | int reg_code = iterator.Next(); |
| 10830 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10831 | break; |
| 10832 | } |
| 10833 | |
| 10834 | case Translation::INT32_REGISTER: { |
| 10835 | int reg_code = iterator.Next(); |
| 10836 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10837 | break; |
| 10838 | } |
| 10839 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10840 | case Translation::UINT32_REGISTER: { |
| 10841 | int reg_code = iterator.Next(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10842 | PrintF(out, "{input=%s (unsigned)}", |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10843 | converter.NameOfCPURegister(reg_code)); |
| 10844 | break; |
| 10845 | } |
| 10846 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10847 | case Translation::DOUBLE_REGISTER: { |
| 10848 | int reg_code = iterator.Next(); |
| 10849 | PrintF(out, "{input=%s}", |
| 10850 | DoubleRegister::AllocationIndexToString(reg_code)); |
| 10851 | break; |
| 10852 | } |
| 10853 | |
| 10854 | case Translation::STACK_SLOT: { |
| 10855 | int input_slot_index = iterator.Next(); |
| 10856 | PrintF(out, "{input=%d}", input_slot_index); |
| 10857 | break; |
| 10858 | } |
| 10859 | |
| 10860 | case Translation::INT32_STACK_SLOT: { |
| 10861 | int input_slot_index = iterator.Next(); |
| 10862 | PrintF(out, "{input=%d}", input_slot_index); |
| 10863 | break; |
| 10864 | } |
| 10865 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10866 | case Translation::UINT32_STACK_SLOT: { |
| 10867 | int input_slot_index = iterator.Next(); |
| 10868 | PrintF(out, "{input=%d (unsigned)}", input_slot_index); |
| 10869 | break; |
| 10870 | } |
| 10871 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10872 | case Translation::DOUBLE_STACK_SLOT: { |
| 10873 | int input_slot_index = iterator.Next(); |
| 10874 | PrintF(out, "{input=%d}", input_slot_index); |
| 10875 | break; |
| 10876 | } |
| 10877 | |
| 10878 | case Translation::LITERAL: { |
| 10879 | unsigned literal_index = iterator.Next(); |
| 10880 | PrintF(out, "{literal_id=%u}", literal_index); |
| 10881 | break; |
| 10882 | } |
| 10883 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 10884 | case Translation::DUPLICATED_OBJECT: { |
| 10885 | int object_index = iterator.Next(); |
| 10886 | PrintF(out, "{object_index=%d}", object_index); |
| 10887 | break; |
| 10888 | } |
| 10889 | |
| 10890 | case Translation::ARGUMENTS_OBJECT: |
| 10891 | case Translation::CAPTURED_OBJECT: { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10892 | int args_length = iterator.Next(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 10893 | PrintF(out, "{length=%d}", args_length); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10894 | break; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10895 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10896 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10897 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10898 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10899 | } |
| 10900 | } |
| 10901 | |
| 10902 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10903 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint(FILE* out) { |
| 10904 | PrintF(out, "Deoptimization Output Data (deopt points = %d)\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10905 | this->DeoptPoints()); |
| 10906 | if (this->DeoptPoints() == 0) return; |
| 10907 | |
| 10908 | PrintF("%6s %8s %s\n", "ast id", "pc", "state"); |
| 10909 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 10910 | int pc_and_state = this->PcAndState(i)->value(); |
| 10911 | PrintF("%6d %8d %s\n", |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10912 | this->AstId(i).ToInt(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10913 | FullCodeGenerator::PcField::decode(pc_and_state), |
| 10914 | FullCodeGenerator::State2String( |
| 10915 | FullCodeGenerator::StateField::decode(pc_and_state))); |
| 10916 | } |
| 10917 | } |
| 10918 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10919 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10920 | const char* Code::ICState2String(InlineCacheState state) { |
| 10921 | switch (state) { |
| 10922 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 10923 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 10924 | case MONOMORPHIC: return "MONOMORPHIC"; |
| 10925 | case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE"; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 10926 | case POLYMORPHIC: return "POLYMORPHIC"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10927 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 10928 | case GENERIC: return "GENERIC"; |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 10929 | case DEBUG_STUB: return "DEBUG_STUB"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 10930 | } |
| 10931 | UNREACHABLE(); |
| 10932 | return NULL; |
| 10933 | } |
| 10934 | |
| 10935 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10936 | const char* Code::StubType2String(StubType type) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10937 | switch (type) { |
| 10938 | case NORMAL: return "NORMAL"; |
| 10939 | case FIELD: return "FIELD"; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 10940 | case CONSTANT: return "CONSTANT"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10941 | case CALLBACKS: return "CALLBACKS"; |
| 10942 | case INTERCEPTOR: return "INTERCEPTOR"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10943 | case TRANSITION: return "TRANSITION"; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10944 | case NONEXISTENT: return "NONEXISTENT"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10945 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 10946 | UNREACHABLE(); // keep the compiler happy |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10947 | return NULL; |
| 10948 | } |
| 10949 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10950 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10951 | void Code::PrintExtraICState(FILE* out, Kind kind, ExtraICState extra) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10952 | PrintF(out, "extra_ic_state = "); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10953 | const char* name = NULL; |
| 10954 | switch (kind) { |
| 10955 | case CALL_IC: |
| 10956 | if (extra == STRING_INDEX_OUT_OF_BOUNDS) { |
| 10957 | name = "STRING_INDEX_OUT_OF_BOUNDS"; |
| 10958 | } |
| 10959 | break; |
| 10960 | case STORE_IC: |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10961 | case KEYED_STORE_IC: |
| 10962 | if (extra == kStrictMode) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10963 | name = "STRICT"; |
| 10964 | } |
| 10965 | break; |
| 10966 | default: |
| 10967 | break; |
| 10968 | } |
| 10969 | if (name != NULL) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10970 | PrintF(out, "%s\n", name); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10971 | } else { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10972 | PrintF(out, "%d\n", extra); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10973 | } |
| 10974 | } |
| 10975 | |
| 10976 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10977 | void Code::Disassemble(const char* name, FILE* out) { |
| 10978 | PrintF(out, "kind = %s\n", Kind2String(kind())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10979 | if (is_inline_cache_stub()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10980 | PrintF(out, "ic_state = %s\n", ICState2String(ic_state())); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10981 | PrintExtraICState(out, kind(), needs_extended_extra_ic_state(kind()) ? |
| 10982 | extended_extra_ic_state() : extra_ic_state()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10983 | if (ic_state() == MONOMORPHIC) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 10984 | PrintF(out, "type = %s\n", StubType2String(type())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10985 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10986 | if (is_call_stub() || is_keyed_call_stub()) { |
| 10987 | PrintF(out, "argc = %d\n", arguments_count()); |
| 10988 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 10989 | if (is_compare_ic_stub()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10990 | ASSERT(major_key() == CodeStub::CompareIC); |
| 10991 | CompareIC::State left_state, right_state, handler_state; |
| 10992 | Token::Value op; |
| 10993 | ICCompareStub::DecodeMinorKey(stub_info(), &left_state, &right_state, |
| 10994 | &handler_state, &op); |
| 10995 | PrintF(out, "compare_state = %s*%s -> %s\n", |
| 10996 | CompareIC::GetStateName(left_state), |
| 10997 | CompareIC::GetStateName(right_state), |
| 10998 | CompareIC::GetStateName(handler_state)); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10999 | PrintF(out, "compare_operation = %s\n", Token::Name(op)); |
| 11000 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11001 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11002 | if ((name != NULL) && (name[0] != '\0')) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11003 | PrintF(out, "name = %s\n", name); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11004 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11005 | if (kind() == OPTIMIZED_FUNCTION) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11006 | PrintF(out, "stack_slots = %d\n", stack_slots()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11007 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11008 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11009 | PrintF(out, "Instructions (size = %d)\n", instruction_size()); |
| 11010 | Disassembler::Decode(out, this); |
| 11011 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11012 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11013 | if (kind() == FUNCTION) { |
| 11014 | DeoptimizationOutputData* data = |
| 11015 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11016 | data->DeoptimizationOutputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11017 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 11018 | DeoptimizationInputData* data = |
| 11019 | DeoptimizationInputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11020 | data->DeoptimizationInputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11021 | } |
| 11022 | PrintF("\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11023 | |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 11024 | if (is_crankshafted()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11025 | SafepointTable table(this); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11026 | PrintF(out, "Safepoints (size = %u)\n", table.size()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11027 | for (unsigned i = 0; i < table.length(); i++) { |
| 11028 | unsigned pc_offset = table.GetPcOffset(i); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11029 | PrintF(out, "%p %4d ", (instruction_start() + pc_offset), pc_offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11030 | table.PrintEntry(i); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11031 | PrintF(out, " (sp -> fp)"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11032 | SafepointEntry entry = table.GetEntry(i); |
| 11033 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
| 11034 | PrintF(out, " %6d", entry.deoptimization_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11035 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11036 | PrintF(out, " <none>"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11037 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11038 | if (entry.argument_count() > 0) { |
| 11039 | PrintF(out, " argc: %d", entry.argument_count()); |
| 11040 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11041 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11042 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11043 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11044 | } else if (kind() == FUNCTION) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 11045 | unsigned offset = back_edge_table_offset(); |
| 11046 | // 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] | 11047 | // (due to alignment) the end of the instruction stream. |
| 11048 | if (static_cast<int>(offset) < instruction_size()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11049 | DisallowHeapAllocation no_gc; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11050 | BackEdgeTable back_edges(this, &no_gc); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11051 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11052 | PrintF(out, "Back edges (size = %u)\n", back_edges.length()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 11053 | PrintF(out, "ast_id pc_offset loop_depth\n"); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11054 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11055 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 11056 | PrintF(out, "%6d %9u %10u\n", back_edges.ast_id(i).ToInt(), |
| 11057 | back_edges.pc_offset(i), |
| 11058 | back_edges.loop_depth(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11059 | } |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11060 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11061 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11062 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 11063 | #ifdef OBJECT_PRINT |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 11064 | if (!type_feedback_info()->IsUndefined()) { |
| 11065 | TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(out); |
| 11066 | PrintF(out, "\n"); |
| 11067 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 11068 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11069 | } |
| 11070 | |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11071 | PrintF("RelocInfo (size = %d)\n", relocation_size()); |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 11072 | for (RelocIterator it(this); !it.done(); it.next()) { |
| 11073 | it.rinfo()->Print(GetIsolate(), out); |
| 11074 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11075 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11076 | } |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 11077 | #endif // ENABLE_DISASSEMBLER |
| 11078 | |
| 11079 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11080 | MaybeObject* JSObject::SetFastElementsCapacityAndLength( |
| 11081 | int capacity, |
| 11082 | int length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11083 | SetFastElementsCapacitySmiMode smi_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11084 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11085 | // 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] | 11086 | ASSERT(!HasExternalArrayElements()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 11087 | ASSERT(!map()->is_observed()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11088 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11089 | // Allocate a new fast elements backing store. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11090 | FixedArray* new_elements; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11091 | MaybeObject* maybe = heap->AllocateUninitializedFixedArray(capacity); |
| 11092 | if (!maybe->To(&new_elements)) return maybe; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11093 | |
jkummerow@chromium.org | f3eae90 | 2012-05-24 16:42:53 +0000 | [diff] [blame] | 11094 | ElementsKind elements_kind = GetElementsKind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11095 | ElementsKind new_elements_kind; |
| 11096 | // The resized array has FAST_*_SMI_ELEMENTS if the capacity mode forces it, |
| 11097 | // or if it's allowed and the old elements array contained only SMIs. |
| 11098 | bool has_fast_smi_elements = |
| 11099 | (smi_mode == kForceSmiElements) || |
| 11100 | ((smi_mode == kAllowSmiElements) && HasFastSmiElements()); |
| 11101 | if (has_fast_smi_elements) { |
| 11102 | if (IsHoleyElementsKind(elements_kind)) { |
| 11103 | new_elements_kind = FAST_HOLEY_SMI_ELEMENTS; |
| 11104 | } else { |
| 11105 | new_elements_kind = FAST_SMI_ELEMENTS; |
| 11106 | } |
| 11107 | } else { |
| 11108 | if (IsHoleyElementsKind(elements_kind)) { |
| 11109 | new_elements_kind = FAST_HOLEY_ELEMENTS; |
| 11110 | } else { |
| 11111 | new_elements_kind = FAST_ELEMENTS; |
| 11112 | } |
| 11113 | } |
| 11114 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11115 | ElementsAccessor* accessor = ElementsAccessor::ForKind(new_elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11116 | MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11117 | accessor->CopyElements(this, new_elements, elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11118 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11119 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11120 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11121 | Map* new_map = map(); |
| 11122 | if (new_elements_kind != elements_kind) { |
| 11123 | MaybeObject* maybe = |
| 11124 | GetElementsTransitionMap(GetIsolate(), new_elements_kind); |
| 11125 | if (!maybe->To(&new_map)) return maybe; |
| 11126 | } |
| 11127 | ValidateElements(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11128 | set_map_and_elements(new_map, new_elements); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11129 | |
| 11130 | // Transition through the allocation site as well if present. |
| 11131 | maybe_obj = UpdateAllocationSite(new_elements_kind); |
| 11132 | if (maybe_obj->IsFailure()) return maybe_obj; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11133 | } else { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11134 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11135 | parameter_map->set(1, new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11136 | } |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11137 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11138 | if (FLAG_trace_elements_transitions) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11139 | PrintElementsTransition(stdout, elements_kind, old_elements, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11140 | GetElementsKind(), new_elements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11141 | } |
| 11142 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11143 | if (IsJSArray()) { |
| 11144 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 11145 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11146 | return new_elements; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11147 | } |
| 11148 | |
| 11149 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 11150 | bool Code::IsWeakEmbeddedObject(Kind kind, Object* object) { |
| 11151 | if (kind != Code::OPTIMIZED_FUNCTION) return false; |
| 11152 | |
| 11153 | if (object->IsMap()) { |
| 11154 | return Map::cast(object)->CanTransition() && |
| 11155 | FLAG_collect_maps && |
| 11156 | FLAG_weak_embedded_maps_in_optimized_code; |
| 11157 | } |
| 11158 | |
| 11159 | if (object->IsJSObject()) { |
| 11160 | return FLAG_weak_embedded_objects_in_optimized_code; |
| 11161 | } |
| 11162 | |
| 11163 | return false; |
| 11164 | } |
| 11165 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11166 | MaybeObject* JSObject::SetFastDoubleElementsCapacityAndLength( |
| 11167 | int capacity, |
| 11168 | int length) { |
| 11169 | Heap* heap = GetHeap(); |
| 11170 | // We should never end in here with a pixel or external array. |
| 11171 | ASSERT(!HasExternalArrayElements()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 11172 | ASSERT(!map()->is_observed()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11173 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11174 | FixedArrayBase* elems; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11175 | { MaybeObject* maybe_obj = |
| 11176 | heap->AllocateUninitializedFixedDoubleArray(capacity); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11177 | if (!maybe_obj->To(&elems)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11178 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11179 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11180 | ElementsKind elements_kind = GetElementsKind(); |
| 11181 | ElementsKind new_elements_kind = elements_kind; |
| 11182 | if (IsHoleyElementsKind(elements_kind)) { |
| 11183 | new_elements_kind = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 11184 | } else { |
| 11185 | new_elements_kind = FAST_DOUBLE_ELEMENTS; |
| 11186 | } |
| 11187 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11188 | Map* new_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11189 | { MaybeObject* maybe_obj = |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11190 | GetElementsTransitionMap(heap->isolate(), new_elements_kind); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11191 | if (!maybe_obj->To(&new_map)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11192 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11193 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11194 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11195 | ElementsAccessor* accessor = ElementsAccessor::ForKind(FAST_DOUBLE_ELEMENTS); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11196 | { MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11197 | accessor->CopyElements(this, elems, elements_kind); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11198 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11199 | } |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11200 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11201 | ValidateElements(); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11202 | set_map_and_elements(new_map, elems); |
| 11203 | } else { |
| 11204 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
| 11205 | parameter_map->set(1, elems); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11206 | } |
| 11207 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11208 | if (FLAG_trace_elements_transitions) { |
| 11209 | PrintElementsTransition(stdout, elements_kind, old_elements, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11210 | GetElementsKind(), elems); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11211 | } |
| 11212 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11213 | if (IsJSArray()) { |
| 11214 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 11215 | } |
| 11216 | |
| 11217 | return this; |
| 11218 | } |
| 11219 | |
| 11220 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11221 | MaybeObject* JSArray::Initialize(int capacity, int length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11222 | ASSERT(capacity >= 0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11223 | return GetHeap()->AllocateJSArrayStorage(this, length, capacity, |
| 11224 | INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11225 | } |
| 11226 | |
| 11227 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11228 | void JSArray::Expand(int required_size) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11229 | GetIsolate()->factory()->SetElementsCapacityAndLength( |
| 11230 | Handle<JSArray>(this), required_size, required_size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11231 | } |
| 11232 | |
| 11233 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11234 | // Returns false if the passed-in index is marked non-configurable, |
| 11235 | // which will cause the ES5 truncation operation to halt, and thus |
| 11236 | // no further old values need be collected. |
| 11237 | static bool GetOldValue(Isolate* isolate, |
| 11238 | Handle<JSObject> object, |
| 11239 | uint32_t index, |
| 11240 | List<Handle<Object> >* old_values, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11241 | List<uint32_t>* indices) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11242 | PropertyAttributes attributes = object->GetLocalElementAttribute(index); |
| 11243 | ASSERT(attributes != ABSENT); |
| 11244 | if (attributes == DONT_DELETE) return false; |
| 11245 | old_values->Add(object->GetLocalElementAccessorPair(index) == NULL |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11246 | ? Object::GetElement(isolate, object, index) |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11247 | : Handle<Object>::cast(isolate->factory()->the_hole_value())); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11248 | indices->Add(index); |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11249 | return true; |
| 11250 | } |
| 11251 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11252 | static void EnqueueSpliceRecord(Handle<JSArray> object, |
| 11253 | uint32_t index, |
| 11254 | Handle<JSArray> deleted, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11255 | uint32_t add_count) { |
| 11256 | Isolate* isolate = object->GetIsolate(); |
| 11257 | HandleScope scope(isolate); |
| 11258 | Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11259 | Handle<Object> add_count_object = |
| 11260 | isolate->factory()->NewNumberFromUint(add_count); |
| 11261 | |
| 11262 | Handle<Object> args[] = |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11263 | { object, index_object, deleted, add_count_object }; |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11264 | |
| 11265 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11266 | Execution::Call(isolate, |
| 11267 | Handle<JSFunction>(isolate->observers_enqueue_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11268 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11269 | &threw); |
| 11270 | ASSERT(!threw); |
| 11271 | } |
| 11272 | |
| 11273 | |
| 11274 | static void BeginPerformSplice(Handle<JSArray> object) { |
| 11275 | Isolate* isolate = object->GetIsolate(); |
| 11276 | HandleScope scope(isolate); |
| 11277 | Handle<Object> args[] = { object }; |
| 11278 | |
| 11279 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11280 | Execution::Call(isolate, |
| 11281 | Handle<JSFunction>(isolate->observers_begin_perform_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11282 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11283 | &threw); |
| 11284 | ASSERT(!threw); |
| 11285 | } |
| 11286 | |
| 11287 | |
| 11288 | static void EndPerformSplice(Handle<JSArray> object) { |
| 11289 | Isolate* isolate = object->GetIsolate(); |
| 11290 | HandleScope scope(isolate); |
| 11291 | Handle<Object> args[] = { object }; |
| 11292 | |
| 11293 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11294 | Execution::Call(isolate, |
| 11295 | Handle<JSFunction>(isolate->observers_end_perform_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11296 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11297 | &threw); |
| 11298 | ASSERT(!threw); |
| 11299 | } |
| 11300 | |
| 11301 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 11302 | MaybeObject* JSArray::SetElementsLength(Object* len) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11303 | // 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] | 11304 | ASSERT(AllowsSetElementsLength()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11305 | if (!(FLAG_harmony_observation && map()->is_observed())) |
| 11306 | return GetElementsAccessor()->SetLength(this, len); |
| 11307 | |
| 11308 | Isolate* isolate = GetIsolate(); |
| 11309 | HandleScope scope(isolate); |
| 11310 | Handle<JSArray> self(this); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11311 | List<uint32_t> indices; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11312 | List<Handle<Object> > old_values; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11313 | Handle<Object> old_length_handle(self->length(), isolate); |
| 11314 | Handle<Object> new_length_handle(len, isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11315 | uint32_t old_length = 0; |
| 11316 | CHECK(old_length_handle->ToArrayIndex(&old_length)); |
| 11317 | uint32_t new_length = 0; |
| 11318 | if (!new_length_handle->ToArrayIndex(&new_length)) |
| 11319 | return Failure::InternalError(); |
| 11320 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11321 | // Observed arrays should always be in dictionary mode; |
| 11322 | // if they were in fast mode, the below is slower than necessary |
| 11323 | // as it iterates over the array backing store multiple times. |
| 11324 | ASSERT(self->HasDictionaryElements()); |
| 11325 | static const PropertyAttributes kNoAttrFilter = NONE; |
| 11326 | int num_elements = self->NumberOfLocalElements(kNoAttrFilter); |
| 11327 | if (num_elements > 0) { |
| 11328 | if (old_length == static_cast<uint32_t>(num_elements)) { |
| 11329 | // Simple case for arrays without holes. |
| 11330 | for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { |
| 11331 | if (!GetOldValue(isolate, self, i, &old_values, &indices)) break; |
| 11332 | } |
| 11333 | } else { |
| 11334 | // For sparse arrays, only iterate over existing elements. |
| 11335 | Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); |
| 11336 | self->GetLocalElementKeys(*keys, kNoAttrFilter); |
| 11337 | while (num_elements-- > 0) { |
| 11338 | uint32_t index = NumberToUint32(keys->get(num_elements)); |
| 11339 | if (index < new_length) break; |
| 11340 | if (!GetOldValue(isolate, self, index, &old_values, &indices)) break; |
| 11341 | } |
| 11342 | } |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11343 | } |
| 11344 | |
| 11345 | MaybeObject* result = |
| 11346 | self->GetElementsAccessor()->SetLength(*self, *new_length_handle); |
| 11347 | Handle<Object> hresult; |
| 11348 | if (!result->ToHandle(&hresult, isolate)) return result; |
| 11349 | |
| 11350 | CHECK(self->length()->ToArrayIndex(&new_length)); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11351 | if (old_length == new_length) return *hresult; |
| 11352 | |
| 11353 | BeginPerformSplice(self); |
| 11354 | |
| 11355 | for (int i = 0; i < indices.length(); ++i) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11356 | JSObject::EnqueueChangeRecord( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11357 | self, "deleted", isolate->factory()->Uint32ToString(indices[i]), |
| 11358 | old_values[i]); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11359 | } |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11360 | JSObject::EnqueueChangeRecord( |
| 11361 | self, "updated", isolate->factory()->length_string(), |
| 11362 | old_length_handle); |
| 11363 | |
| 11364 | EndPerformSplice(self); |
| 11365 | |
| 11366 | uint32_t index = Min(old_length, new_length); |
| 11367 | uint32_t add_count = new_length > old_length ? new_length - old_length : 0; |
| 11368 | uint32_t delete_count = new_length < old_length ? old_length - new_length : 0; |
| 11369 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11370 | if (delete_count > 0) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11371 | for (int i = indices.length() - 1; i >= 0; i--) { |
| 11372 | JSObject::SetElement(deleted, indices[i] - index, old_values[i], NONE, |
| 11373 | kNonStrictMode); |
| 11374 | } |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11375 | |
| 11376 | SetProperty(deleted, isolate->factory()->length_string(), |
| 11377 | isolate->factory()->NewNumberFromUint(delete_count), |
| 11378 | NONE, kNonStrictMode); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11379 | } |
| 11380 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11381 | EnqueueSpliceRecord(self, index, deleted, add_count); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11382 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11383 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11384 | } |
| 11385 | |
| 11386 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11387 | Handle<Map> Map::GetPrototypeTransition(Handle<Map> map, |
| 11388 | Handle<Object> prototype) { |
| 11389 | FixedArray* cache = map->GetPrototypeTransitions(); |
| 11390 | int number_of_transitions = map->NumberOfProtoTransitions(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11391 | const int proto_offset = |
| 11392 | kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset; |
| 11393 | const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset; |
| 11394 | const int step = kProtoTransitionElementsPerEntry; |
| 11395 | for (int i = 0; i < number_of_transitions; i++) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11396 | if (cache->get(proto_offset + i * step) == *prototype) { |
| 11397 | Object* result = cache->get(map_offset + i * step); |
| 11398 | return Handle<Map>(Map::cast(result)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11399 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11400 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11401 | return Handle<Map>(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11402 | } |
| 11403 | |
| 11404 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11405 | Handle<Map> Map::PutPrototypeTransition(Handle<Map> map, |
| 11406 | Handle<Object> prototype, |
| 11407 | Handle<Map> target_map) { |
| 11408 | ASSERT(target_map->IsMap()); |
| 11409 | ASSERT(HeapObject::cast(*prototype)->map()->IsMap()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11410 | // Don't cache prototype transition if this map is shared. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11411 | if (map->is_shared() || !FLAG_cache_prototype_transitions) return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11412 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11413 | const int step = kProtoTransitionElementsPerEntry; |
| 11414 | const int header = kProtoTransitionHeaderSize; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11415 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11416 | Handle<FixedArray> cache(map->GetPrototypeTransitions()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11417 | int capacity = (cache->length() - header) / step; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11418 | int transitions = map->NumberOfProtoTransitions() + 1; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11419 | |
| 11420 | if (transitions > capacity) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11421 | if (capacity > kMaxCachedPrototypeTransitions) return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11422 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11423 | // Grow array by factor 2 over and above what we need. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11424 | Factory* factory = map->GetIsolate()->factory(); |
| 11425 | cache = factory->CopySizeFixedArray(cache, transitions * 2 * step + header); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11426 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11427 | CALL_AND_RETRY_OR_DIE(map->GetIsolate(), |
| 11428 | map->SetPrototypeTransitions(*cache), |
| 11429 | break, |
| 11430 | return Handle<Map>()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11431 | } |
| 11432 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11433 | // Reload number of transitions as GC might shrink them. |
| 11434 | int last = map->NumberOfProtoTransitions(); |
| 11435 | int entry = header + last * step; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11436 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11437 | cache->set(entry + kProtoTransitionPrototypeOffset, *prototype); |
| 11438 | cache->set(entry + kProtoTransitionMapOffset, *target_map); |
| 11439 | map->SetNumberOfProtoTransitions(transitions); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11440 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11441 | return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11442 | } |
| 11443 | |
| 11444 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 11445 | void Map::ZapTransitions() { |
| 11446 | TransitionArray* transition_array = transitions(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 11447 | // TODO(mstarzinger): Temporarily use a slower version instead of the faster |
| 11448 | // MemsetPointer to investigate a crasher. Switch back to MemsetPointer. |
| 11449 | Object** data = transition_array->data_start(); |
| 11450 | Object* the_hole = GetHeap()->the_hole_value(); |
| 11451 | int length = transition_array->length(); |
| 11452 | for (int i = 0; i < length; i++) { |
| 11453 | data[i] = the_hole; |
| 11454 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
| 11457 | |
| 11458 | void Map::ZapPrototypeTransitions() { |
| 11459 | FixedArray* proto_transitions = GetPrototypeTransitions(); |
| 11460 | MemsetPointer(proto_transitions->data_start(), |
| 11461 | GetHeap()->the_hole_value(), |
| 11462 | proto_transitions->length()); |
| 11463 | } |
| 11464 | |
| 11465 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11466 | void Map::AddDependentCompilationInfo(DependentCode::DependencyGroup group, |
| 11467 | CompilationInfo* info) { |
| 11468 | Handle<DependentCode> dep(dependent_code()); |
| 11469 | Handle<DependentCode> codes = |
| 11470 | DependentCode::Insert(dep, group, info->object_wrapper()); |
| 11471 | if (*codes != dependent_code()) set_dependent_code(*codes); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11472 | info->dependencies(group)->Add(Handle<HeapObject>(this), info->zone()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11473 | } |
| 11474 | |
| 11475 | |
| 11476 | void Map::AddDependentCode(DependentCode::DependencyGroup group, |
| 11477 | Handle<Code> code) { |
| 11478 | Handle<DependentCode> codes = DependentCode::Insert( |
| 11479 | Handle<DependentCode>(dependent_code()), group, code); |
| 11480 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 11481 | } |
| 11482 | |
| 11483 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11484 | DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) { |
| 11485 | Recompute(entries); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 11486 | } |
| 11487 | |
| 11488 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11489 | void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) { |
| 11490 | start_indexes_[0] = 0; |
| 11491 | for (int g = 1; g <= kGroupCount; g++) { |
| 11492 | int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1)); |
| 11493 | start_indexes_[g] = start_indexes_[g - 1] + count; |
| 11494 | } |
| 11495 | } |
| 11496 | |
| 11497 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11498 | DependentCode* DependentCode::ForObject(Handle<HeapObject> object, |
| 11499 | DependencyGroup group) { |
| 11500 | AllowDeferredHandleDereference dependencies_are_safe; |
| 11501 | if (group == DependentCode::kPropertyCellChangedGroup) { |
| 11502 | return Handle<PropertyCell>::cast(object)->dependent_code(); |
| 11503 | } |
| 11504 | return Handle<Map>::cast(object)->dependent_code(); |
| 11505 | } |
| 11506 | |
| 11507 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11508 | Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries, |
| 11509 | DependencyGroup group, |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11510 | Handle<Object> object) { |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11511 | GroupStartIndexes starts(*entries); |
| 11512 | int start = starts.at(group); |
| 11513 | int end = starts.at(group + 1); |
| 11514 | int number_of_entries = starts.number_of_entries(); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11515 | if (start < end && entries->object_at(end - 1) == *object) { |
| 11516 | // Do not append the compilation info if it is already in the array. |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11517 | // It is sufficient to just check only the last element because |
| 11518 | // we process embedded maps of an optimized code in one batch. |
| 11519 | return entries; |
| 11520 | } |
| 11521 | if (entries->length() < kCodesStartIndex + number_of_entries + 1) { |
| 11522 | Factory* factory = entries->GetIsolate()->factory(); |
| 11523 | int capacity = kCodesStartIndex + number_of_entries + 1; |
| 11524 | if (capacity > 5) capacity = capacity * 5 / 4; |
| 11525 | Handle<DependentCode> new_entries = Handle<DependentCode>::cast( |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 11526 | factory->CopySizeFixedArray(entries, capacity, TENURED)); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11527 | // The number of codes can change after GC. |
| 11528 | starts.Recompute(*entries); |
| 11529 | start = starts.at(group); |
| 11530 | end = starts.at(group + 1); |
| 11531 | number_of_entries = starts.number_of_entries(); |
| 11532 | for (int i = 0; i < number_of_entries; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11533 | entries->clear_at(i); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11534 | } |
| 11535 | // If the old fixed array was empty, we need to reset counters of the |
| 11536 | // new array. |
| 11537 | if (number_of_entries == 0) { |
| 11538 | for (int g = 0; g < kGroupCount; g++) { |
| 11539 | new_entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0); |
| 11540 | } |
| 11541 | } |
| 11542 | entries = new_entries; |
| 11543 | } |
| 11544 | entries->ExtendGroup(group); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11545 | entries->set_object_at(end, *object); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11546 | entries->set_number_of_entries(group, end + 1 - start); |
| 11547 | return entries; |
| 11548 | } |
| 11549 | |
| 11550 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11551 | void DependentCode::UpdateToFinishedCode(DependencyGroup group, |
| 11552 | CompilationInfo* info, |
| 11553 | Code* code) { |
| 11554 | DisallowHeapAllocation no_gc; |
| 11555 | AllowDeferredHandleDereference get_object_wrapper; |
| 11556 | Foreign* info_wrapper = *info->object_wrapper(); |
| 11557 | GroupStartIndexes starts(this); |
| 11558 | int start = starts.at(group); |
| 11559 | int end = starts.at(group + 1); |
| 11560 | for (int i = start; i < end; i++) { |
| 11561 | if (object_at(i) == info_wrapper) { |
| 11562 | set_object_at(i, code); |
| 11563 | break; |
| 11564 | } |
| 11565 | } |
| 11566 | |
| 11567 | #ifdef DEBUG |
| 11568 | for (int i = start; i < end; i++) { |
| 11569 | ASSERT(is_code_at(i) || compilation_info_at(i) != info); |
| 11570 | } |
| 11571 | #endif |
| 11572 | } |
| 11573 | |
| 11574 | |
| 11575 | void DependentCode::RemoveCompilationInfo(DependentCode::DependencyGroup group, |
| 11576 | CompilationInfo* info) { |
| 11577 | DisallowHeapAllocation no_allocation; |
| 11578 | AllowDeferredHandleDereference get_object_wrapper; |
| 11579 | Foreign* info_wrapper = *info->object_wrapper(); |
| 11580 | GroupStartIndexes starts(this); |
| 11581 | int start = starts.at(group); |
| 11582 | int end = starts.at(group + 1); |
| 11583 | // Find compilation info wrapper. |
| 11584 | int info_pos = -1; |
| 11585 | for (int i = start; i < end; i++) { |
| 11586 | if (object_at(i) == info_wrapper) { |
| 11587 | info_pos = i; |
| 11588 | break; |
| 11589 | } |
| 11590 | } |
| 11591 | if (info_pos == -1) return; // Not found. |
| 11592 | int gap = info_pos; |
| 11593 | // Use the last of each group to fill the gap in the previous group. |
| 11594 | for (int i = group; i < kGroupCount; i++) { |
| 11595 | int last_of_group = starts.at(i + 1) - 1; |
| 11596 | ASSERT(last_of_group >= gap); |
| 11597 | if (last_of_group == gap) continue; |
| 11598 | copy(last_of_group, gap); |
| 11599 | gap = last_of_group; |
| 11600 | } |
| 11601 | ASSERT(gap == starts.number_of_entries() - 1); |
| 11602 | clear_at(gap); // Clear last gap. |
| 11603 | set_number_of_entries(group, end - start - 1); |
| 11604 | |
| 11605 | #ifdef DEBUG |
| 11606 | for (int i = start; i < end - 1; i++) { |
| 11607 | ASSERT(is_code_at(i) || compilation_info_at(i) != info); |
| 11608 | } |
| 11609 | #endif |
| 11610 | } |
| 11611 | |
| 11612 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11613 | bool DependentCode::Contains(DependencyGroup group, Code* code) { |
| 11614 | GroupStartIndexes starts(this); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 11615 | int start = starts.at(group); |
| 11616 | int end = starts.at(group + 1); |
| 11617 | for (int i = start; i < end; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11618 | if (object_at(i) == code) return true; |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 11619 | } |
| 11620 | return false; |
| 11621 | } |
| 11622 | |
| 11623 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11624 | void DependentCode::DeoptimizeDependentCodeGroup( |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 11625 | Isolate* isolate, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11626 | DependentCode::DependencyGroup group) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11627 | ASSERT(AllowCodeDependencyChange::IsAllowed()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 11628 | DisallowHeapAllocation no_allocation_scope; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11629 | DependentCode::GroupStartIndexes starts(this); |
| 11630 | int start = starts.at(group); |
| 11631 | int end = starts.at(group + 1); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11632 | int code_entries = starts.number_of_entries(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11633 | if (start == end) return; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 11634 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11635 | // Mark all the code that needs to be deoptimized. |
| 11636 | bool marked = false; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11637 | for (int i = start; i < end; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11638 | if (is_code_at(i)) { |
| 11639 | Code* code = code_at(i); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11640 | if (!code->marked_for_deoptimization()) { |
| 11641 | code->set_marked_for_deoptimization(true); |
| 11642 | marked = true; |
| 11643 | } |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11644 | } else { |
| 11645 | CompilationInfo* info = compilation_info_at(i); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11646 | info->AbortDueToDependencyChange(); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11647 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11648 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11649 | // Compact the array by moving all subsequent groups to fill in the new holes. |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11650 | for (int src = end, dst = start; src < code_entries; src++, dst++) { |
| 11651 | copy(src, dst); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11652 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11653 | // Now the holes are at the end of the array, zap them for heap-verifier. |
| 11654 | int removed = end - start; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11655 | for (int i = code_entries - removed; i < code_entries; i++) { |
| 11656 | clear_at(i); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11657 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11658 | set_number_of_entries(group, 0); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11659 | |
| 11660 | if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11661 | } |
| 11662 | |
| 11663 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11664 | Handle<Object> JSObject::SetPrototype(Handle<JSObject> object, |
| 11665 | Handle<Object> value, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 11666 | bool skip_hidden_prototypes) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11667 | #ifdef DEBUG |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11668 | int size = object->Size(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11669 | #endif |
| 11670 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11671 | Isolate* isolate = object->GetIsolate(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11672 | Heap* heap = isolate->heap(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11673 | // Silently ignore the change if value is not a JSObject or null. |
| 11674 | // SpiderMonkey behaves this way. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 11675 | if (!value->IsJSReceiver() && !value->IsNull()) return value; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11676 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11677 | // From 8.6.2 Object Internal Methods |
| 11678 | // ... |
| 11679 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 11680 | // [[Prototype]] internal properties of the object may not be modified. |
| 11681 | // ... |
| 11682 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 11683 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 11684 | // paragraph. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11685 | if (!object->map()->is_extensible()) { |
| 11686 | Handle<Object> args[] = { object }; |
| 11687 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 11688 | "non_extensible_proto", HandleVector(args, ARRAY_SIZE(args))); |
| 11689 | isolate->Throw(*error); |
| 11690 | return Handle<Object>(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11691 | } |
| 11692 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11693 | // Before we can set the prototype we need to be sure |
| 11694 | // prototype cycles are prevented. |
| 11695 | // It is sufficient to validate that the receiver is not in the new prototype |
| 11696 | // chain. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11697 | for (Object* pt = *value; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11698 | pt != heap->null_value(); |
| 11699 | pt = pt->GetPrototype(isolate)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11700 | if (JSReceiver::cast(pt) == *object) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11701 | // Cycle detected. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11702 | Handle<Object> error = isolate->factory()->NewError( |
| 11703 | "cyclic_proto", HandleVector<Object>(NULL, 0)); |
| 11704 | isolate->Throw(*error); |
| 11705 | return Handle<Object>(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11706 | } |
| 11707 | } |
| 11708 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11709 | Handle<JSObject> real_receiver = object; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11710 | |
| 11711 | if (skip_hidden_prototypes) { |
| 11712 | // Find the first object in the chain whose prototype object is not |
| 11713 | // hidden and set the new prototype on that object. |
| 11714 | Object* current_proto = real_receiver->GetPrototype(); |
| 11715 | while (current_proto->IsJSObject() && |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11716 | JSObject::cast(current_proto)->map()->is_hidden_prototype()) { |
| 11717 | real_receiver = handle(JSObject::cast(current_proto), isolate); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11718 | current_proto = current_proto->GetPrototype(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11719 | } |
| 11720 | } |
| 11721 | |
| 11722 | // Set the new prototype of the object. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11723 | Handle<Map> map(real_receiver->map()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11724 | |
| 11725 | // Nothing to do if prototype is already set. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11726 | if (map->prototype() == *value) return value; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11727 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 11728 | if (value->IsJSObject()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11729 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 11730 | } |
| 11731 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11732 | Handle<Map> new_map = Map::GetPrototypeTransition(map, value); |
| 11733 | if (new_map.is_null()) { |
| 11734 | new_map = Map::Copy(map); |
| 11735 | Map::PutPrototypeTransition(map, value, new_map); |
| 11736 | new_map->set_prototype(*value); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11737 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11738 | ASSERT(new_map->prototype() == *value); |
| 11739 | real_receiver->set_map(*new_map); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11740 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11741 | heap->ClearInstanceofCache(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11742 | ASSERT(size == object->Size()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11743 | return value; |
| 11744 | } |
| 11745 | |
| 11746 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11747 | MaybeObject* JSObject::EnsureCanContainElements(Arguments* args, |
| 11748 | uint32_t first_arg, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11749 | uint32_t arg_count, |
| 11750 | EnsureElementsMode mode) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11751 | // Elements in |Arguments| are ordered backwards (because they're on the |
| 11752 | // stack), but the method that's called here iterates over them in forward |
| 11753 | // direction. |
| 11754 | return EnsureCanContainElements( |
| 11755 | args->arguments() - first_arg - (arg_count - 1), |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11756 | arg_count, mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11757 | } |
| 11758 | |
| 11759 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 11760 | AccessorPair* JSObject::GetLocalPropertyAccessorPair(Name* name) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11761 | uint32_t index = 0; |
| 11762 | if (name->AsArrayIndex(&index)) { |
| 11763 | return GetLocalElementAccessorPair(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11764 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11765 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11766 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11767 | LocalLookupRealNamedProperty(name, &lookup); |
| 11768 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11769 | if (lookup.IsPropertyCallbacks() && |
| 11770 | lookup.GetCallbackObject()->IsAccessorPair()) { |
| 11771 | return AccessorPair::cast(lookup.GetCallbackObject()); |
| 11772 | } |
| 11773 | return NULL; |
| 11774 | } |
| 11775 | |
| 11776 | |
| 11777 | AccessorPair* JSObject::GetLocalElementAccessorPair(uint32_t index) { |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11778 | if (IsJSGlobalProxy()) { |
| 11779 | Object* proto = GetPrototype(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11780 | if (proto->IsNull()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11781 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11782 | return JSObject::cast(proto)->GetLocalElementAccessorPair(index); |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11783 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11784 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11785 | // Check for lookup interceptor. |
| 11786 | if (HasIndexedInterceptor()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11787 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11788 | return GetElementsAccessor()->GetAccessorPair(this, this, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11789 | } |
| 11790 | |
| 11791 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11792 | MaybeObject* JSObject::SetElementWithInterceptor(uint32_t index, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11793 | Object* value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11794 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11795 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11796 | bool check_prototype, |
| 11797 | SetPropertyMode set_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11798 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 11799 | HandleScope scope(isolate); |
| 11800 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11801 | // Make sure that the top context does not change when doing |
| 11802 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 11803 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 11804 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11805 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 11806 | Handle<JSObject> this_handle(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11807 | Handle<Object> value_handle(value, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11808 | if (!interceptor->setter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 11809 | v8::IndexedPropertySetterCallback setter = |
| 11810 | v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11811 | LOG(isolate, |
| 11812 | ApiIndexedPropertyAccess("interceptor-indexed-set", this, index)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11813 | PropertyCallbackArguments args(isolate, interceptor->data(), this, this); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11814 | v8::Handle<v8::Value> result = |
| 11815 | args.Call(setter, index, v8::Utils::ToLocal(value_handle)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11816 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11817 | if (!result.IsEmpty()) return *value_handle; |
| 11818 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11819 | MaybeObject* raw_result = |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11820 | this_handle->SetElementWithoutInterceptor(index, |
| 11821 | *value_handle, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11822 | attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11823 | strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 11824 | check_prototype, |
| 11825 | set_mode); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11826 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11827 | return raw_result; |
| 11828 | } |
| 11829 | |
| 11830 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11831 | MaybeObject* JSObject::GetElementWithCallback(Object* receiver, |
| 11832 | Object* structure, |
| 11833 | uint32_t index, |
| 11834 | Object* holder) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11835 | Isolate* isolate = GetIsolate(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 11836 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11837 | |
| 11838 | // api style callbacks. |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11839 | if (structure->IsExecutableAccessorInfo()) { |
| 11840 | Handle<ExecutableAccessorInfo> data( |
| 11841 | ExecutableAccessorInfo::cast(structure)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11842 | Object* fun_obj = data->getter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 11843 | v8::AccessorGetterCallback call_fun = |
| 11844 | v8::ToCData<v8::AccessorGetterCallback>(fun_obj); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 11845 | if (call_fun == NULL) return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11846 | HandleScope scope(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11847 | Handle<JSObject> self(JSObject::cast(receiver)); |
| 11848 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11849 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11850 | Handle<String> key = isolate->factory()->NumberToString(number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11851 | LOG(isolate, ApiNamedPropertyAccess("load", *self, *key)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11852 | PropertyCallbackArguments |
| 11853 | args(isolate, data->data(), *self, *holder_handle); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11854 | v8::Handle<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(key)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11855 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 11856 | if (result.IsEmpty()) return isolate->heap()->undefined_value(); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 11857 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 11858 | result_internal->VerifyApiCallResultType(); |
| 11859 | return *result_internal; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11860 | } |
| 11861 | |
| 11862 | // __defineGetter__ callback |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11863 | if (structure->IsAccessorPair()) { |
| 11864 | Object* getter = AccessorPair::cast(structure)->getter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11865 | if (getter->IsSpecFunction()) { |
| 11866 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 11867 | return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11868 | } |
| 11869 | // Getter is not a function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11870 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11871 | } |
| 11872 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11873 | if (structure->IsDeclaredAccessorInfo()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 11874 | return GetDeclaredAccessorProperty(receiver, |
| 11875 | DeclaredAccessorInfo::cast(structure), |
| 11876 | isolate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11877 | } |
| 11878 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11879 | UNREACHABLE(); |
| 11880 | return NULL; |
| 11881 | } |
| 11882 | |
| 11883 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11884 | Handle<Object> JSObject::SetElementWithCallback(Handle<JSObject> object, |
| 11885 | Handle<Object> structure, |
| 11886 | uint32_t index, |
| 11887 | Handle<Object> value, |
| 11888 | Handle<JSObject> holder, |
| 11889 | StrictModeFlag strict_mode) { |
| 11890 | Isolate* isolate = object->GetIsolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11891 | |
| 11892 | // We should never get here to initialize a const with the hole |
| 11893 | // value since a const declaration would conflict with the setter. |
| 11894 | ASSERT(!value->IsTheHole()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11895 | |
| 11896 | // 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] | 11897 | // data structure used to store the callbacks. Eventually foreign |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11898 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 11899 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11900 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11901 | if (structure->IsExecutableAccessorInfo()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11902 | // api style callbacks |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11903 | Handle<ExecutableAccessorInfo> data = |
| 11904 | Handle<ExecutableAccessorInfo>::cast(structure); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11905 | Object* call_obj = data->setter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 11906 | v8::AccessorSetterCallback call_fun = |
| 11907 | v8::ToCData<v8::AccessorSetterCallback>(call_obj); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11908 | if (call_fun == NULL) return value; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11909 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 11910 | Handle<String> key(isolate->factory()->NumberToString(number)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11911 | LOG(isolate, ApiNamedPropertyAccess("store", *object, *key)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11912 | PropertyCallbackArguments |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11913 | args(isolate, data->data(), *object, *holder); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11914 | args.Call(call_fun, |
| 11915 | v8::Utils::ToLocal(key), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11916 | v8::Utils::ToLocal(value)); |
| 11917 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 11918 | return value; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11919 | } |
| 11920 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 11921 | if (structure->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11922 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11923 | if (setter->IsSpecFunction()) { |
| 11924 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11925 | return SetPropertyWithDefinedSetter( |
| 11926 | object, Handle<JSReceiver>::cast(setter), value); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11927 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 11928 | if (strict_mode == kNonStrictMode) { |
| 11929 | return value; |
| 11930 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11931 | Handle<Object> key(isolate->factory()->NewNumberFromUint(index)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11932 | Handle<Object> args[2] = { key, holder }; |
| 11933 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 11934 | "no_setter_in_callback", HandleVector(args, 2)); |
| 11935 | isolate->Throw(*error); |
| 11936 | return Handle<Object>(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11937 | } |
| 11938 | } |
| 11939 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 11940 | // TODO(dcarney): Handle correctly. |
| 11941 | if (structure->IsDeclaredAccessorInfo()) return value; |
| 11942 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11943 | UNREACHABLE(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11944 | return Handle<Object>(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 11945 | } |
| 11946 | |
| 11947 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11948 | bool JSObject::HasFastArgumentsElements() { |
| 11949 | Heap* heap = GetHeap(); |
| 11950 | if (!elements()->IsFixedArray()) return false; |
| 11951 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 11952 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 11953 | return false; |
| 11954 | } |
| 11955 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 11956 | return !arguments->IsDictionary(); |
| 11957 | } |
| 11958 | |
| 11959 | |
| 11960 | bool JSObject::HasDictionaryArgumentsElements() { |
| 11961 | Heap* heap = GetHeap(); |
| 11962 | if (!elements()->IsFixedArray()) return false; |
| 11963 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 11964 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 11965 | return false; |
| 11966 | } |
| 11967 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 11968 | return arguments->IsDictionary(); |
| 11969 | } |
| 11970 | |
| 11971 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11972 | // Adding n elements in fast case is O(n*n). |
| 11973 | // Note: revisit design to have dual undefined values to capture absent |
| 11974 | // elements. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11975 | MaybeObject* JSObject::SetFastElement(uint32_t index, |
| 11976 | Object* value, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11977 | StrictModeFlag strict_mode, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 11978 | bool check_prototype) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11979 | ASSERT(HasFastSmiOrObjectElements() || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11980 | HasFastArgumentsElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11981 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 11982 | // Array optimizations rely on the prototype lookups of Array objects always |
| 11983 | // returning undefined. If there is a store to the initial prototype object, |
| 11984 | // make sure all of these optimizations are invalidated. |
| 11985 | Isolate* isolate(GetIsolate()); |
| 11986 | if (isolate->is_initial_object_prototype(this) || |
| 11987 | isolate->is_initial_array_prototype(this)) { |
| 11988 | HandleScope scope(GetIsolate()); |
| 11989 | map()->dependent_code()->DeoptimizeDependentCodeGroup( |
| 11990 | GetIsolate(), |
| 11991 | DependentCode::kElementsCantBeAddedGroup); |
| 11992 | } |
| 11993 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11994 | FixedArray* backing_store = FixedArray::cast(elements()); |
| 11995 | if (backing_store->map() == GetHeap()->non_strict_arguments_elements_map()) { |
| 11996 | backing_store = FixedArray::cast(backing_store->get(1)); |
| 11997 | } else { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11998 | MaybeObject* maybe = EnsureWritableFastElements(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11999 | if (!maybe->To(&backing_store)) return maybe; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12000 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12001 | uint32_t capacity = static_cast<uint32_t>(backing_store->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12002 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 12003 | if (check_prototype && |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12004 | (index >= capacity || backing_store->get(index)->IsTheHole())) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 12005 | bool found; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 12006 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index, |
| 12007 | value, |
| 12008 | &found, |
| 12009 | strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 12010 | if (found) return result; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 12011 | } |
| 12012 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12013 | uint32_t new_capacity = capacity; |
| 12014 | // Check if the length property of this object needs to be updated. |
| 12015 | uint32_t array_length = 0; |
| 12016 | bool must_update_array_length = false; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12017 | bool introduces_holes = true; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12018 | if (IsJSArray()) { |
| 12019 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12020 | introduces_holes = index > array_length; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12021 | if (index >= array_length) { |
| 12022 | must_update_array_length = true; |
| 12023 | array_length = index + 1; |
| 12024 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12025 | } else { |
| 12026 | introduces_holes = index >= capacity; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12027 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12028 | |
| 12029 | // If the array is growing, and it's not growth by a single element at the |
| 12030 | // end, make sure that the ElementsKind is HOLEY. |
| 12031 | ElementsKind elements_kind = GetElementsKind(); |
| 12032 | if (introduces_holes && |
| 12033 | IsFastElementsKind(elements_kind) && |
| 12034 | !IsFastHoleyElementsKind(elements_kind)) { |
| 12035 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
| 12036 | MaybeObject* maybe = TransitionElementsKind(transitioned_kind); |
| 12037 | if (maybe->IsFailure()) return maybe; |
| 12038 | } |
| 12039 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12040 | // Check if the capacity of the backing store needs to be increased, or if |
| 12041 | // a transition to slow elements is necessary. |
| 12042 | if (index >= capacity) { |
| 12043 | bool convert_to_slow = true; |
| 12044 | if ((index - capacity) < kMaxGap) { |
| 12045 | new_capacity = NewElementsCapacity(index + 1); |
| 12046 | ASSERT(new_capacity > index); |
| 12047 | if (!ShouldConvertToSlowElements(new_capacity)) { |
| 12048 | convert_to_slow = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12049 | } |
| 12050 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12051 | if (convert_to_slow) { |
| 12052 | MaybeObject* result = NormalizeElements(); |
| 12053 | if (result->IsFailure()) return result; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12054 | return SetDictionaryElement(index, value, NONE, strict_mode, |
| 12055 | check_prototype); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12056 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12057 | } |
| 12058 | // Convert to fast double elements if appropriate. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12059 | if (HasFastSmiElements() && !value->IsSmi() && value->IsNumber()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12060 | // Consider fixing the boilerplate as well if we have one. |
| 12061 | ElementsKind to_kind = IsHoleyElementsKind(elements_kind) |
| 12062 | ? FAST_HOLEY_DOUBLE_ELEMENTS |
| 12063 | : FAST_DOUBLE_ELEMENTS; |
| 12064 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12065 | MaybeObject* maybe_failure = UpdateAllocationSite(to_kind); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12066 | if (maybe_failure->IsFailure()) return maybe_failure; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12067 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12068 | MaybeObject* maybe = |
| 12069 | SetFastDoubleElementsCapacityAndLength(new_capacity, array_length); |
| 12070 | if (maybe->IsFailure()) return maybe; |
| 12071 | FixedDoubleArray::cast(elements())->set(index, value->Number()); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12072 | ValidateElements(); |
ager@chromium.org | bdf2f94 | 2008-10-17 07:23:00 +0000 | [diff] [blame] | 12073 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12074 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12075 | // Change elements kind from Smi-only to generic FAST if necessary. |
| 12076 | if (HasFastSmiElements() && !value->IsSmi()) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12077 | Map* new_map; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12078 | ElementsKind kind = HasFastHoleyElements() |
| 12079 | ? FAST_HOLEY_ELEMENTS |
| 12080 | : FAST_ELEMENTS; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12081 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12082 | MaybeObject* maybe_failure = UpdateAllocationSite(kind); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12083 | if (maybe_failure->IsFailure()) return maybe_failure; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12084 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12085 | MaybeObject* maybe_new_map = GetElementsTransitionMap(GetIsolate(), |
| 12086 | kind); |
| 12087 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 12088 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12089 | set_map(new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12090 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12091 | // Increase backing store capacity if that's been decided previously. |
| 12092 | if (new_capacity != capacity) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12093 | FixedArray* new_elements; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12094 | SetFastElementsCapacitySmiMode smi_mode = |
| 12095 | value->IsSmi() && HasFastSmiElements() |
| 12096 | ? kAllowSmiElements |
| 12097 | : kDontAllowSmiElements; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12098 | { MaybeObject* maybe = |
| 12099 | SetFastElementsCapacityAndLength(new_capacity, |
| 12100 | array_length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12101 | smi_mode); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12102 | if (!maybe->To(&new_elements)) return maybe; |
| 12103 | } |
| 12104 | new_elements->set(index, value); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12105 | ValidateElements(); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12106 | return value; |
| 12107 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12108 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12109 | // Finally, set the new element and length. |
| 12110 | ASSERT(elements()->IsFixedArray()); |
| 12111 | backing_store->set(index, value); |
| 12112 | if (must_update_array_length) { |
| 12113 | JSArray::cast(this)->set_length(Smi::FromInt(array_length)); |
| 12114 | } |
| 12115 | return value; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12116 | } |
| 12117 | |
| 12118 | |
| 12119 | MaybeObject* JSObject::SetDictionaryElement(uint32_t index, |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12120 | Object* value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12121 | PropertyAttributes attributes, |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12122 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12123 | bool check_prototype, |
| 12124 | SetPropertyMode set_mode) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12125 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
| 12126 | Isolate* isolate = GetIsolate(); |
| 12127 | Heap* heap = isolate->heap(); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12128 | Handle<JSObject> self(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 12129 | Handle<Object> value(value_raw, isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12130 | |
| 12131 | // Insert element in the dictionary. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12132 | Handle<FixedArray> elements(FixedArray::cast(this->elements())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12133 | bool is_arguments = |
| 12134 | (elements->map() == heap->non_strict_arguments_elements_map()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12135 | Handle<SeededNumberDictionary> dictionary(is_arguments |
| 12136 | ? SeededNumberDictionary::cast(elements->get(1)) |
| 12137 | : SeededNumberDictionary::cast(*elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12138 | |
| 12139 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12140 | if (entry != SeededNumberDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12141 | Handle<Object> element(dictionary->ValueAt(entry), isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12142 | PropertyDetails details = dictionary->DetailsAt(entry); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12143 | if (details.type() == CALLBACKS && set_mode == SET_PROPERTY) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12144 | Handle<Object> result = SetElementWithCallback(self, element, index, |
| 12145 | value, self, strict_mode); |
| 12146 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
| 12147 | return *result; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12148 | } else { |
| 12149 | dictionary->UpdateMaxNumberKey(index); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12150 | // 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] | 12151 | // is read-only (a declared const that has not been initialized). If a |
| 12152 | // value is being defined we skip attribute checks completely. |
| 12153 | if (set_mode == DEFINE_PROPERTY) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12154 | details = PropertyDetails( |
| 12155 | attributes, NORMAL, details.dictionary_index()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12156 | dictionary->DetailsAtPut(entry, details); |
| 12157 | } else if (details.IsReadOnly() && !element->IsTheHole()) { |
| 12158 | if (strict_mode == kNonStrictMode) { |
| 12159 | return isolate->heap()->undefined_value(); |
| 12160 | } else { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 12161 | Handle<Object> holder(this, isolate); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12162 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 12163 | Handle<Object> args[2] = { number, holder }; |
| 12164 | Handle<Object> error = |
| 12165 | isolate->factory()->NewTypeError("strict_read_only_property", |
| 12166 | HandleVector(args, 2)); |
| 12167 | return isolate->Throw(*error); |
| 12168 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12169 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12170 | // Elements of the arguments object in slow mode might be slow aliases. |
| 12171 | if (is_arguments && element->IsAliasedArgumentsEntry()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12172 | AliasedArgumentsEntry* entry = AliasedArgumentsEntry::cast(*element); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12173 | Context* context = Context::cast(elements->get(0)); |
| 12174 | int context_index = entry->aliased_context_slot(); |
| 12175 | ASSERT(!context->get(context_index)->IsTheHole()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12176 | context->set(context_index, *value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12177 | // For elements that are still writable we keep slow aliasing. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12178 | if (!details.IsReadOnly()) value = element; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12179 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12180 | dictionary->ValueAtPut(entry, *value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12181 | } |
| 12182 | } else { |
| 12183 | // 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] | 12184 | // Can cause GC! |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12185 | if (check_prototype) { |
| 12186 | bool found; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12187 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes( |
| 12188 | index, *value, &found, strict_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12189 | if (found) return result; |
| 12190 | } |
| 12191 | // When we set the is_extensible flag to false we always force the |
| 12192 | // element into dictionary mode (and force them to stay there). |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12193 | if (!self->map()->is_extensible()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12194 | if (strict_mode == kNonStrictMode) { |
| 12195 | return isolate->heap()->undefined_value(); |
| 12196 | } else { |
| 12197 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 12198 | Handle<String> name = isolate->factory()->NumberToString(number); |
| 12199 | Handle<Object> args[1] = { name }; |
| 12200 | Handle<Object> error = |
| 12201 | isolate->factory()->NewTypeError("object_not_extensible", |
| 12202 | HandleVector(args, 1)); |
| 12203 | return isolate->Throw(*error); |
| 12204 | } |
| 12205 | } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 12206 | FixedArrayBase* new_dictionary; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12207 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12208 | MaybeObject* maybe = dictionary->AddNumberEntry(index, *value, details); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12209 | if (!maybe->To(&new_dictionary)) return maybe; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12210 | if (*dictionary != SeededNumberDictionary::cast(new_dictionary)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12211 | if (is_arguments) { |
| 12212 | elements->set(1, new_dictionary); |
| 12213 | } else { |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12214 | self->set_elements(new_dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12215 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12216 | dictionary = |
| 12217 | handle(SeededNumberDictionary::cast(new_dictionary), isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12218 | } |
| 12219 | } |
| 12220 | |
| 12221 | // Update the array length if this JSObject is an array. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12222 | if (self->IsJSArray()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12223 | MaybeObject* result = |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12224 | JSArray::cast(*self)->JSArrayUpdateLengthFromIndex(index, *value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12225 | if (result->IsFailure()) return result; |
| 12226 | } |
| 12227 | |
| 12228 | // Attempt to put this object back in fast case. |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12229 | if (self->ShouldConvertToFastElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12230 | uint32_t new_length = 0; |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12231 | if (self->IsJSArray()) { |
| 12232 | CHECK(JSArray::cast(*self)->length()->ToArrayIndex(&new_length)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12233 | } else { |
| 12234 | new_length = dictionary->max_number_key() + 1; |
| 12235 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12236 | SetFastElementsCapacitySmiMode smi_mode = FLAG_smi_only_arrays |
| 12237 | ? kAllowSmiElements |
| 12238 | : kDontAllowSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12239 | bool has_smi_only_elements = false; |
| 12240 | bool should_convert_to_fast_double_elements = |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12241 | self->ShouldConvertToFastDoubleElements(&has_smi_only_elements); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12242 | if (has_smi_only_elements) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12243 | smi_mode = kForceSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12244 | } |
| 12245 | MaybeObject* result = should_convert_to_fast_double_elements |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12246 | ? self->SetFastDoubleElementsCapacityAndLength(new_length, new_length) |
| 12247 | : self->SetFastElementsCapacityAndLength( |
| 12248 | new_length, new_length, smi_mode); |
| 12249 | self->ValidateElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12250 | if (result->IsFailure()) return result; |
| 12251 | #ifdef DEBUG |
| 12252 | if (FLAG_trace_normalization) { |
| 12253 | PrintF("Object elements are fast case again:\n"); |
| 12254 | Print(); |
| 12255 | } |
| 12256 | #endif |
| 12257 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12258 | return *value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12259 | } |
| 12260 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 12261 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12262 | MUST_USE_RESULT MaybeObject* JSObject::SetFastDoubleElement( |
| 12263 | uint32_t index, |
| 12264 | Object* value, |
| 12265 | StrictModeFlag strict_mode, |
| 12266 | bool check_prototype) { |
| 12267 | ASSERT(HasFastDoubleElements()); |
| 12268 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 12269 | FixedArrayBase* base_elms = FixedArrayBase::cast(elements()); |
| 12270 | uint32_t elms_length = static_cast<uint32_t>(base_elms->length()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12271 | |
| 12272 | // If storing to an element that isn't in the array, pass the store request |
| 12273 | // up the prototype chain before storing in the receiver's elements. |
| 12274 | if (check_prototype && |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 12275 | (index >= elms_length || |
| 12276 | FixedDoubleArray::cast(base_elms)->is_the_hole(index))) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12277 | bool found; |
| 12278 | MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index, |
| 12279 | value, |
| 12280 | &found, |
| 12281 | strict_mode); |
| 12282 | if (found) return result; |
| 12283 | } |
| 12284 | |
| 12285 | // If the value object is not a heap number, switch to fast elements and try |
| 12286 | // again. |
| 12287 | bool value_is_smi = value->IsSmi(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12288 | bool introduces_holes = true; |
| 12289 | uint32_t length = elms_length; |
| 12290 | if (IsJSArray()) { |
| 12291 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
| 12292 | introduces_holes = index > length; |
| 12293 | } else { |
| 12294 | introduces_holes = index >= elms_length; |
| 12295 | } |
| 12296 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12297 | if (!value->IsNumber()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12298 | MaybeObject* maybe_obj = SetFastElementsCapacityAndLength( |
| 12299 | elms_length, |
| 12300 | length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12301 | kDontAllowSmiElements); |
| 12302 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 12303 | maybe_obj = SetFastElement(index, value, strict_mode, check_prototype); |
| 12304 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 12305 | ValidateElements(); |
| 12306 | return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12307 | } |
| 12308 | |
| 12309 | double double_value = value_is_smi |
| 12310 | ? static_cast<double>(Smi::cast(value)->value()) |
| 12311 | : HeapNumber::cast(value)->value(); |
| 12312 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12313 | // If the array is growing, and it's not growth by a single element at the |
| 12314 | // end, make sure that the ElementsKind is HOLEY. |
| 12315 | ElementsKind elements_kind = GetElementsKind(); |
| 12316 | if (introduces_holes && !IsFastHoleyElementsKind(elements_kind)) { |
| 12317 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
| 12318 | MaybeObject* maybe = TransitionElementsKind(transitioned_kind); |
| 12319 | if (maybe->IsFailure()) return maybe; |
| 12320 | } |
| 12321 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12322 | // Check whether there is extra space in the fixed array. |
| 12323 | if (index < elms_length) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 12324 | FixedDoubleArray* elms = FixedDoubleArray::cast(elements()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12325 | elms->set(index, double_value); |
| 12326 | if (IsJSArray()) { |
| 12327 | // Update the length of the array if needed. |
| 12328 | uint32_t array_length = 0; |
| 12329 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
| 12330 | if (index >= array_length) { |
| 12331 | JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
| 12332 | } |
| 12333 | } |
| 12334 | return value; |
| 12335 | } |
| 12336 | |
| 12337 | // Allow gap in fast case. |
| 12338 | if ((index - elms_length) < kMaxGap) { |
| 12339 | // Try allocating extra space. |
| 12340 | int new_capacity = NewElementsCapacity(index+1); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 12341 | if (!ShouldConvertToSlowElements(new_capacity)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12342 | ASSERT(static_cast<uint32_t>(new_capacity) > index); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12343 | MaybeObject* maybe_obj = |
| 12344 | SetFastDoubleElementsCapacityAndLength(new_capacity, index + 1); |
| 12345 | if (maybe_obj->IsFailure()) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12346 | FixedDoubleArray::cast(elements())->set(index, double_value); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12347 | ValidateElements(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12348 | return value; |
| 12349 | } |
| 12350 | } |
| 12351 | |
| 12352 | // Otherwise default to slow case. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12353 | ASSERT(HasFastDoubleElements()); |
| 12354 | ASSERT(map()->has_fast_double_elements()); |
| 12355 | ASSERT(elements()->IsFixedDoubleArray()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12356 | Object* obj; |
| 12357 | { MaybeObject* maybe_obj = NormalizeElements(); |
| 12358 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 12359 | } |
| 12360 | ASSERT(HasDictionaryElements()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12361 | return SetElement(index, value, NONE, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12362 | } |
| 12363 | |
| 12364 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 12365 | Handle<Object> JSReceiver::SetElement(Handle<JSReceiver> object, |
| 12366 | uint32_t index, |
| 12367 | Handle<Object> value, |
| 12368 | PropertyAttributes attributes, |
| 12369 | StrictModeFlag strict_mode) { |
| 12370 | if (object->IsJSProxy()) { |
| 12371 | return JSProxy::SetElementWithHandler( |
| 12372 | Handle<JSProxy>::cast(object), object, index, value, strict_mode); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12373 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 12374 | return JSObject::SetElement( |
| 12375 | Handle<JSObject>::cast(object), index, value, attributes, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12376 | } |
| 12377 | |
| 12378 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12379 | Handle<Object> JSObject::SetOwnElement(Handle<JSObject> object, |
| 12380 | uint32_t index, |
| 12381 | Handle<Object> value, |
| 12382 | StrictModeFlag strict_mode) { |
| 12383 | ASSERT(!object->HasExternalArrayElements()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12384 | CALL_HEAP_FUNCTION( |
| 12385 | object->GetIsolate(), |
| 12386 | object->SetElement(index, *value, NONE, strict_mode, false), |
| 12387 | Object); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12388 | } |
| 12389 | |
| 12390 | |
| 12391 | Handle<Object> JSObject::SetElement(Handle<JSObject> object, |
| 12392 | uint32_t index, |
| 12393 | Handle<Object> value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12394 | PropertyAttributes attr, |
| 12395 | StrictModeFlag strict_mode, |
| 12396 | SetPropertyMode set_mode) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12397 | if (object->HasExternalArrayElements()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 12398 | if (!value->IsNumber() && !value->IsUndefined()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12399 | bool has_exception; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12400 | Handle<Object> number = |
| 12401 | Execution::ToNumber(object->GetIsolate(), value, &has_exception); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12402 | if (has_exception) return Handle<Object>(); |
| 12403 | value = number; |
| 12404 | } |
| 12405 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12406 | CALL_HEAP_FUNCTION( |
| 12407 | object->GetIsolate(), |
| 12408 | object->SetElement(index, *value, attr, strict_mode, true, set_mode), |
| 12409 | Object); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12410 | } |
| 12411 | |
| 12412 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 12413 | MaybeObject* JSObject::SetElement(uint32_t index, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12414 | Object* value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12415 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12416 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12417 | bool check_prototype, |
| 12418 | SetPropertyMode set_mode) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12419 | Isolate* isolate = GetIsolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12420 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12421 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12422 | if (IsAccessCheckNeeded()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12423 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_SET)) { |
| 12424 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 12425 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12426 | return value_raw; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12427 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12428 | } |
| 12429 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12430 | if (IsJSGlobalProxy()) { |
| 12431 | Object* proto = GetPrototype(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12432 | if (proto->IsNull()) return value_raw; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12433 | ASSERT(proto->IsJSGlobalObject()); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12434 | return JSObject::cast(proto)->SetElement(index, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12435 | value_raw, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12436 | attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12437 | strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12438 | check_prototype, |
| 12439 | set_mode); |
| 12440 | } |
| 12441 | |
| 12442 | // Don't allow element properties to be redefined for external arrays. |
| 12443 | if (HasExternalArrayElements() && set_mode == DEFINE_PROPERTY) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12444 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12445 | Handle<Object> args[] = { handle(this, isolate), number }; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12446 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 12447 | "redef_external_array_element", HandleVector(args, ARRAY_SIZE(args))); |
| 12448 | return isolate->Throw(*error); |
| 12449 | } |
| 12450 | |
| 12451 | // Normalize the elements to enable attributes on the property. |
| 12452 | if ((attributes & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) { |
| 12453 | SeededNumberDictionary* dictionary; |
| 12454 | MaybeObject* maybe_object = NormalizeElements(); |
| 12455 | if (!maybe_object->To(&dictionary)) return maybe_object; |
| 12456 | // Make sure that we never go back to fast case. |
| 12457 | dictionary->set_requires_slow_elements(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12458 | } |
| 12459 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12460 | if (!(FLAG_harmony_observation && map()->is_observed())) { |
| 12461 | return HasIndexedInterceptor() |
| 12462 | ? SetElementWithInterceptor( |
| 12463 | index, value_raw, attributes, strict_mode, check_prototype, set_mode) |
| 12464 | : SetElementWithoutInterceptor( |
| 12465 | index, value_raw, attributes, strict_mode, check_prototype, set_mode); |
| 12466 | } |
| 12467 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12468 | // From here on, everything has to be handlified. |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12469 | Handle<JSObject> self(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 12470 | Handle<Object> value(value_raw, isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12471 | PropertyAttributes old_attributes = self->GetLocalElementAttribute(index); |
| 12472 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12473 | Handle<Object> old_length_handle; |
| 12474 | Handle<Object> new_length_handle; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12475 | |
| 12476 | if (old_attributes != ABSENT) { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 12477 | if (self->GetLocalElementAccessorPair(index) == NULL) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 12478 | old_value = Object::GetElement(isolate, self, index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12479 | } else if (self->IsJSArray()) { |
| 12480 | // Store old array length in case adding an element grows the array. |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12481 | old_length_handle = handle(Handle<JSArray>::cast(self)->length(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12482 | } |
| 12483 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12484 | // Check for lookup interceptor |
| 12485 | MaybeObject* result = self->HasIndexedInterceptor() |
| 12486 | ? self->SetElementWithInterceptor( |
| 12487 | index, *value, attributes, strict_mode, check_prototype, set_mode) |
| 12488 | : self->SetElementWithoutInterceptor( |
| 12489 | index, *value, attributes, strict_mode, check_prototype, set_mode); |
| 12490 | |
| 12491 | Handle<Object> hresult; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12492 | if (!result->ToHandle(&hresult, isolate)) return result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12493 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12494 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 12495 | PropertyAttributes new_attributes = self->GetLocalElementAttribute(index); |
| 12496 | if (old_attributes == ABSENT) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12497 | if (self->IsJSArray() && |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12498 | !old_length_handle->SameValue(Handle<JSArray>::cast(self)->length())) { |
| 12499 | new_length_handle = handle(Handle<JSArray>::cast(self)->length(), |
| 12500 | isolate); |
| 12501 | uint32_t old_length = 0; |
| 12502 | uint32_t new_length = 0; |
| 12503 | CHECK(old_length_handle->ToArrayIndex(&old_length)); |
| 12504 | CHECK(new_length_handle->ToArrayIndex(&new_length)); |
| 12505 | |
| 12506 | BeginPerformSplice(Handle<JSArray>::cast(self)); |
| 12507 | EnqueueChangeRecord(self, "new", name, old_value); |
| 12508 | EnqueueChangeRecord(self, "updated", isolate->factory()->length_string(), |
| 12509 | old_length_handle); |
| 12510 | EndPerformSplice(Handle<JSArray>::cast(self)); |
| 12511 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 12512 | EnqueueSpliceRecord(Handle<JSArray>::cast(self), old_length, deleted, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12513 | new_length - old_length); |
| 12514 | } else { |
| 12515 | EnqueueChangeRecord(self, "new", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12516 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12517 | } else if (old_value->IsTheHole()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12518 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12519 | } else { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 12520 | Handle<Object> new_value = Object::GetElement(isolate, self, index); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 12521 | bool value_changed = !old_value->SameValue(*new_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12522 | if (old_attributes != new_attributes) { |
| 12523 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
| 12524 | EnqueueChangeRecord(self, "reconfigured", name, old_value); |
| 12525 | } else if (value_changed) { |
| 12526 | EnqueueChangeRecord(self, "updated", name, old_value); |
| 12527 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12528 | } |
| 12529 | |
| 12530 | return *hresult; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12531 | } |
| 12532 | |
| 12533 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12534 | MaybeObject* JSObject::SetElementWithoutInterceptor(uint32_t index, |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 12535 | Object* value, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12536 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12537 | StrictModeFlag strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12538 | bool check_prototype, |
| 12539 | SetPropertyMode set_mode) { |
| 12540 | ASSERT(HasDictionaryElements() || |
| 12541 | HasDictionaryArgumentsElements() || |
| 12542 | (attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12543 | Isolate* isolate = GetIsolate(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12544 | if (FLAG_trace_external_array_abuse && |
| 12545 | IsExternalArrayElementsKind(GetElementsKind())) { |
| 12546 | CheckArrayAbuse(this, "external elements write", index); |
| 12547 | } |
| 12548 | if (FLAG_trace_js_array_abuse && |
| 12549 | !IsExternalArrayElementsKind(GetElementsKind())) { |
| 12550 | if (IsJSArray()) { |
| 12551 | CheckArrayAbuse(this, "elements write", index, true); |
| 12552 | } |
| 12553 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12554 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12555 | case FAST_SMI_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12556 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12557 | case FAST_HOLEY_SMI_ELEMENTS: |
| 12558 | case FAST_HOLEY_ELEMENTS: |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12559 | return SetFastElement(index, value, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12560 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12561 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12562 | return SetFastDoubleElement(index, value, strict_mode, check_prototype); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 12563 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 12564 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12565 | return pixels->SetValue(index, value); |
| 12566 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12567 | case EXTERNAL_BYTE_ELEMENTS: { |
| 12568 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 12569 | return array->SetValue(index, value); |
| 12570 | } |
| 12571 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 12572 | ExternalUnsignedByteArray* array = |
| 12573 | ExternalUnsignedByteArray::cast(elements()); |
| 12574 | return array->SetValue(index, value); |
| 12575 | } |
| 12576 | case EXTERNAL_SHORT_ELEMENTS: { |
| 12577 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 12578 | return array->SetValue(index, value); |
| 12579 | } |
| 12580 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 12581 | ExternalUnsignedShortArray* array = |
| 12582 | ExternalUnsignedShortArray::cast(elements()); |
| 12583 | return array->SetValue(index, value); |
| 12584 | } |
| 12585 | case EXTERNAL_INT_ELEMENTS: { |
| 12586 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 12587 | return array->SetValue(index, value); |
| 12588 | } |
| 12589 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 12590 | ExternalUnsignedIntArray* array = |
| 12591 | ExternalUnsignedIntArray::cast(elements()); |
| 12592 | return array->SetValue(index, value); |
| 12593 | } |
| 12594 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 12595 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 12596 | return array->SetValue(index, value); |
| 12597 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12598 | case EXTERNAL_DOUBLE_ELEMENTS: { |
| 12599 | ExternalDoubleArray* array = ExternalDoubleArray::cast(elements()); |
| 12600 | return array->SetValue(index, value); |
| 12601 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12602 | case DICTIONARY_ELEMENTS: |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12603 | return SetDictionaryElement(index, value, attr, strict_mode, |
| 12604 | check_prototype, set_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12605 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 12606 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 12607 | uint32_t length = parameter_map->length(); |
| 12608 | Object* probe = |
whesse@chromium.org | df8c03c | 2011-06-21 14:36:03 +0000 | [diff] [blame] | 12609 | (index < length - 2) ? parameter_map->get(index + 2) : NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12610 | if (probe != NULL && !probe->IsTheHole()) { |
| 12611 | Context* context = Context::cast(parameter_map->get(0)); |
| 12612 | int context_index = Smi::cast(probe)->value(); |
| 12613 | ASSERT(!context->get(context_index)->IsTheHole()); |
| 12614 | context->set(context_index, value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12615 | // Redefining attributes of an aliased element destroys fast aliasing. |
| 12616 | if (set_mode == SET_PROPERTY || attr == NONE) return value; |
| 12617 | parameter_map->set_the_hole(index + 2); |
| 12618 | // For elements that are still writable we re-establish slow aliasing. |
| 12619 | if ((attr & READ_ONLY) == 0) { |
| 12620 | MaybeObject* maybe_entry = |
| 12621 | isolate->heap()->AllocateAliasedArgumentsEntry(context_index); |
| 12622 | if (!maybe_entry->ToObject(&value)) return maybe_entry; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12623 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12624 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12625 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 12626 | if (arguments->IsDictionary()) { |
| 12627 | return SetDictionaryElement(index, value, attr, strict_mode, |
| 12628 | check_prototype, set_mode); |
| 12629 | } else { |
| 12630 | return SetFastElement(index, value, strict_mode, check_prototype); |
| 12631 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12632 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12633 | } |
| 12634 | // All possible cases have been handled above. Add a return to avoid the |
| 12635 | // complaints from the compiler. |
| 12636 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12637 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12638 | } |
| 12639 | |
| 12640 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12641 | void JSObject::TransitionElementsKind(Handle<JSObject> object, |
| 12642 | ElementsKind to_kind) { |
| 12643 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 12644 | object->TransitionElementsKind(to_kind)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12645 | } |
| 12646 | |
| 12647 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 12648 | bool AllocationSite::IsNestedSite() { |
| 12649 | ASSERT(FLAG_trace_track_allocation_sites); |
| 12650 | Object* current = GetHeap()->allocation_sites_list(); |
| 12651 | while (current != NULL && current->IsAllocationSite()) { |
| 12652 | AllocationSite* current_site = AllocationSite::cast(current); |
| 12653 | if (current_site->nested_site() == this) { |
| 12654 | return true; |
| 12655 | } |
| 12656 | current = current_site->weak_next(); |
| 12657 | } |
| 12658 | return false; |
| 12659 | } |
| 12660 | |
| 12661 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12662 | MaybeObject* JSObject::UpdateAllocationSite(ElementsKind to_kind) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12663 | if (!FLAG_track_allocation_sites || !IsJSArray()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12664 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12665 | } |
| 12666 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 12667 | AllocationMemento* memento = AllocationMemento::FindForJSObject(this); |
| 12668 | if (memento == NULL || !memento->IsValid()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12669 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12670 | } |
| 12671 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12672 | // Walk through to the Allocation Site |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 12673 | AllocationSite* site = memento->GetAllocationSite(); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 12674 | if (site->SitePointsToLiteral() && |
| 12675 | site->transition_info()->IsJSArray()) { |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12676 | JSArray* transition_info = JSArray::cast(site->transition_info()); |
| 12677 | ElementsKind kind = transition_info->GetElementsKind(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 12678 | // if kind is holey ensure that to_kind is as well. |
| 12679 | if (IsHoleyElementsKind(kind)) { |
| 12680 | to_kind = GetHoleyElementsKind(to_kind); |
| 12681 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12682 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12683 | // If the array is huge, it's not likely to be defined in a local |
| 12684 | // function, so we shouldn't make new instances of it very often. |
| 12685 | uint32_t length = 0; |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12686 | CHECK(transition_info->length()->ToArrayIndex(&length)); |
| 12687 | if (length <= AllocationSite::kMaximumArrayBytesToPretransition) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12688 | if (FLAG_trace_track_allocation_sites) { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 12689 | bool is_nested = site->IsNestedSite(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12690 | PrintF( |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 12691 | "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n", |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12692 | reinterpret_cast<void*>(this), |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 12693 | is_nested ? "(nested)" : "", |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12694 | ElementsKindToString(kind), |
| 12695 | ElementsKindToString(to_kind)); |
| 12696 | } |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12697 | return transition_info->TransitionElementsKind(to_kind); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12698 | } |
| 12699 | } |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12700 | } else { |
| 12701 | ElementsKind kind = site->GetElementsKind(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 12702 | // if kind is holey ensure that to_kind is as well. |
| 12703 | if (IsHoleyElementsKind(kind)) { |
| 12704 | to_kind = GetHoleyElementsKind(to_kind); |
| 12705 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12706 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12707 | if (FLAG_trace_track_allocation_sites) { |
| 12708 | PrintF("AllocationSite: JSArray %p site updated %s->%s\n", |
| 12709 | reinterpret_cast<void*>(this), |
| 12710 | ElementsKindToString(kind), |
| 12711 | ElementsKindToString(to_kind)); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12712 | } |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12713 | site->set_transition_info(Smi::FromInt(to_kind)); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12714 | } |
| 12715 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12716 | return this; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12717 | } |
| 12718 | |
| 12719 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12720 | MaybeObject* JSObject::TransitionElementsKind(ElementsKind to_kind) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12721 | ASSERT(!map()->is_observed()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12722 | ElementsKind from_kind = map()->elements_kind(); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12723 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12724 | if (IsFastHoleyElementsKind(from_kind)) { |
| 12725 | to_kind = GetHoleyElementsKind(to_kind); |
| 12726 | } |
| 12727 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 12728 | if (from_kind == to_kind) return this; |
| 12729 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12730 | MaybeObject* maybe_failure = UpdateAllocationSite(to_kind); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12731 | if (maybe_failure->IsFailure()) return maybe_failure; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12732 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12733 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12734 | if (elements() == isolate->heap()->empty_fixed_array() || |
| 12735 | (IsFastSmiOrObjectElementsKind(from_kind) && |
| 12736 | IsFastSmiOrObjectElementsKind(to_kind)) || |
| 12737 | (from_kind == FAST_DOUBLE_ELEMENTS && |
| 12738 | to_kind == FAST_HOLEY_DOUBLE_ELEMENTS)) { |
| 12739 | ASSERT(from_kind != TERMINAL_FAST_ELEMENTS_KIND); |
| 12740 | // No change is needed to the elements() buffer, the transition |
| 12741 | // only requires a map change. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12742 | MaybeObject* maybe_new_map = GetElementsTransitionMap(isolate, to_kind); |
| 12743 | Map* new_map; |
| 12744 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 12745 | set_map(new_map); |
| 12746 | if (FLAG_trace_elements_transitions) { |
| 12747 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 12748 | PrintElementsTransition(stdout, from_kind, elms, to_kind, elms); |
| 12749 | } |
| 12750 | return this; |
| 12751 | } |
| 12752 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12753 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 12754 | uint32_t capacity = static_cast<uint32_t>(elms->length()); |
| 12755 | uint32_t length = capacity; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12756 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12757 | if (IsJSArray()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12758 | Object* raw_length = JSArray::cast(this)->length(); |
| 12759 | if (raw_length->IsUndefined()) { |
| 12760 | // If length is undefined, then JSArray is being initialized and has no |
| 12761 | // elements, assume a length of zero. |
| 12762 | length = 0; |
| 12763 | } else { |
| 12764 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12765 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12766 | } |
| 12767 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12768 | if (IsFastSmiElementsKind(from_kind) && |
| 12769 | IsFastDoubleElementsKind(to_kind)) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12770 | MaybeObject* maybe_result = |
| 12771 | SetFastDoubleElementsCapacityAndLength(capacity, length); |
| 12772 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12773 | ValidateElements(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12774 | return this; |
| 12775 | } |
| 12776 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12777 | if (IsFastDoubleElementsKind(from_kind) && |
| 12778 | IsFastObjectElementsKind(to_kind)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12779 | MaybeObject* maybe_result = SetFastElementsCapacityAndLength( |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12780 | capacity, length, kDontAllowSmiElements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12781 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12782 | ValidateElements(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12783 | return this; |
| 12784 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12785 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12786 | // This method should never be called for any other case than the ones |
| 12787 | // handled above. |
| 12788 | UNREACHABLE(); |
| 12789 | return GetIsolate()->heap()->null_value(); |
| 12790 | } |
| 12791 | |
| 12792 | |
| 12793 | // static |
| 12794 | bool Map::IsValidElementsTransition(ElementsKind from_kind, |
| 12795 | ElementsKind to_kind) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12796 | // Transitions can't go backwards. |
| 12797 | if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) { |
| 12798 | return false; |
| 12799 | } |
| 12800 | |
| 12801 | // Transitions from HOLEY -> PACKED are not allowed. |
| 12802 | return !IsFastHoleyElementsKind(from_kind) || |
| 12803 | IsFastHoleyElementsKind(to_kind); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12804 | } |
| 12805 | |
| 12806 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12807 | MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, |
| 12808 | Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12809 | uint32_t old_len = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 12810 | CHECK(length()->ToArrayIndex(&old_len)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12811 | // Check to see if we need to update the length. For now, we make |
| 12812 | // sure that the length stays within 32-bits (unsigned). |
| 12813 | if (index >= old_len && index != 0xffffffff) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12814 | Object* len; |
| 12815 | { MaybeObject* maybe_len = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12816 | GetHeap()->NumberFromDouble(static_cast<double>(index) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12817 | if (!maybe_len->ToObject(&len)) return maybe_len; |
| 12818 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12819 | set_length(len); |
| 12820 | } |
| 12821 | return value; |
| 12822 | } |
| 12823 | |
| 12824 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 12825 | MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12826 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12827 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 12828 | HandleScope scope(isolate); |
| 12829 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12830 | // Make sure that the top context does not change when doing |
| 12831 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 12832 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 12833 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12834 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate); |
| 12835 | Handle<Object> this_handle(receiver, isolate); |
| 12836 | Handle<JSObject> holder_handle(this, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12837 | if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 12838 | v8::IndexedPropertyGetterCallback getter = |
| 12839 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12840 | LOG(isolate, |
| 12841 | ApiIndexedPropertyAccess("interceptor-indexed-get", this, index)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 12842 | PropertyCallbackArguments |
| 12843 | args(isolate, interceptor->data(), receiver, this); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12844 | v8::Handle<v8::Value> result = args.Call(getter, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12845 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 12846 | if (!result.IsEmpty()) { |
| 12847 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 12848 | result_internal->VerifyApiCallResultType(); |
| 12849 | return *result_internal; |
| 12850 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12851 | } |
| 12852 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12853 | Heap* heap = holder_handle->GetHeap(); |
| 12854 | ElementsAccessor* handler = holder_handle->GetElementsAccessor(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12855 | MaybeObject* raw_result = handler->Get(*this_handle, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 12856 | *holder_handle, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 12857 | index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12858 | if (raw_result != heap->the_hole_value()) return raw_result; |
| 12859 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12860 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12861 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12862 | Object* pt = holder_handle->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12863 | if (pt == heap->null_value()) return heap->undefined_value(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 12864 | return pt->GetElementWithReceiver(isolate, *this_handle, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12865 | } |
| 12866 | |
| 12867 | |
| 12868 | bool JSObject::HasDenseElements() { |
| 12869 | int capacity = 0; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12870 | int used = 0; |
| 12871 | GetElementsCapacityAndUsage(&capacity, &used); |
| 12872 | return (capacity == 0) || (used > (capacity / 2)); |
| 12873 | } |
| 12874 | |
| 12875 | |
| 12876 | void JSObject::GetElementsCapacityAndUsage(int* capacity, int* used) { |
| 12877 | *capacity = 0; |
| 12878 | *used = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12879 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12880 | FixedArrayBase* backing_store_base = FixedArrayBase::cast(elements()); |
| 12881 | FixedArray* backing_store = NULL; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12882 | switch (GetElementsKind()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12883 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12884 | backing_store_base = |
| 12885 | FixedArray::cast(FixedArray::cast(backing_store_base)->get(1)); |
| 12886 | backing_store = FixedArray::cast(backing_store_base); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12887 | if (backing_store->IsDictionary()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12888 | SeededNumberDictionary* dictionary = |
| 12889 | SeededNumberDictionary::cast(backing_store); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12890 | *capacity = dictionary->Capacity(); |
| 12891 | *used = dictionary->NumberOfElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12892 | break; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12893 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12894 | // Fall through. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12895 | case FAST_SMI_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12896 | case FAST_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12897 | if (IsJSArray()) { |
| 12898 | *capacity = backing_store_base->length(); |
| 12899 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 12900 | break; |
| 12901 | } |
| 12902 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12903 | case FAST_HOLEY_SMI_ELEMENTS: |
| 12904 | case FAST_HOLEY_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12905 | backing_store = FixedArray::cast(backing_store_base); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12906 | *capacity = backing_store->length(); |
| 12907 | for (int i = 0; i < *capacity; ++i) { |
| 12908 | if (!backing_store->get(i)->IsTheHole()) ++(*used); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12909 | } |
| 12910 | break; |
| 12911 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12912 | SeededNumberDictionary* dictionary = |
| 12913 | SeededNumberDictionary::cast(FixedArray::cast(elements())); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12914 | *capacity = dictionary->Capacity(); |
| 12915 | *used = dictionary->NumberOfElements(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12916 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12917 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12918 | case FAST_DOUBLE_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12919 | if (IsJSArray()) { |
| 12920 | *capacity = backing_store_base->length(); |
| 12921 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 12922 | break; |
| 12923 | } |
| 12924 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12925 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12926 | FixedDoubleArray* elms = FixedDoubleArray::cast(elements()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12927 | *capacity = elms->length(); |
| 12928 | for (int i = 0; i < *capacity; i++) { |
| 12929 | if (!elms->is_the_hole(i)) ++(*used); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12930 | } |
| 12931 | break; |
| 12932 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12933 | case EXTERNAL_BYTE_ELEMENTS: |
| 12934 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 12935 | case EXTERNAL_SHORT_ELEMENTS: |
| 12936 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 12937 | case EXTERNAL_INT_ELEMENTS: |
| 12938 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12939 | case EXTERNAL_FLOAT_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12940 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 12941 | case EXTERNAL_PIXEL_ELEMENTS: |
| 12942 | // External arrays are considered 100% used. |
| 12943 | ExternalArray* external_array = ExternalArray::cast(elements()); |
| 12944 | *capacity = external_array->length(); |
| 12945 | *used = external_array->length(); |
| 12946 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12947 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12948 | } |
| 12949 | |
| 12950 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12951 | bool JSObject::ShouldConvertToSlowElements(int new_capacity) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12952 | STATIC_ASSERT(kMaxUncheckedOldFastElementsLength <= |
| 12953 | kMaxUncheckedFastElementsLength); |
| 12954 | if (new_capacity <= kMaxUncheckedOldFastElementsLength || |
| 12955 | (new_capacity <= kMaxUncheckedFastElementsLength && |
| 12956 | GetHeap()->InNewSpace(this))) { |
| 12957 | return false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12958 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12959 | // If the fast-case backing storage takes up roughly three times as |
| 12960 | // much space (in machine words) as a dictionary backing storage |
| 12961 | // would, the object should have slow elements. |
| 12962 | int old_capacity = 0; |
| 12963 | int used_elements = 0; |
| 12964 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12965 | int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) * |
| 12966 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12967 | return 3 * dictionary_size <= new_capacity; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12968 | } |
| 12969 | |
| 12970 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12971 | bool JSObject::ShouldConvertToFastElements() { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12972 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12973 | // If the elements are sparse, we should not go back to fast case. |
| 12974 | if (!HasDenseElements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12975 | // An object requiring access checks is never allowed to have fast |
| 12976 | // elements. If it had fast elements we would skip security checks. |
| 12977 | if (IsAccessCheckNeeded()) return false; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 12978 | // Observed objects may not go to fast mode because they rely on map checks, |
| 12979 | // and for fast element accesses we sometimes check element kinds only. |
| 12980 | if (FLAG_harmony_observation && map()->is_observed()) return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12981 | |
| 12982 | FixedArray* elements = FixedArray::cast(this->elements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12983 | SeededNumberDictionary* dictionary = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12984 | if (elements->map() == GetHeap()->non_strict_arguments_elements_map()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12985 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12986 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12987 | dictionary = SeededNumberDictionary::cast(elements); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12988 | } |
| 12989 | // If an element has been added at a very high index in the elements |
| 12990 | // dictionary, we cannot go back to fast case. |
| 12991 | if (dictionary->requires_slow_elements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12992 | // If the dictionary backing storage takes up roughly half as much |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12993 | // space (in machine words) as a fast-case backing storage would, |
| 12994 | // the object should have fast elements. |
| 12995 | uint32_t array_size = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12996 | if (IsJSArray()) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12997 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12998 | } else { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 12999 | array_size = dictionary->max_number_key(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13000 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13001 | uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) * |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13002 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13003 | return 2 * dictionary_size >= array_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13004 | } |
| 13005 | |
| 13006 | |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13007 | bool JSObject::ShouldConvertToFastDoubleElements( |
| 13008 | bool* has_smi_only_elements) { |
| 13009 | *has_smi_only_elements = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13010 | if (FLAG_unbox_double_arrays) { |
| 13011 | ASSERT(HasDictionaryElements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13012 | SeededNumberDictionary* dictionary = |
| 13013 | SeededNumberDictionary::cast(elements()); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13014 | bool found_double = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13015 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 13016 | Object* key = dictionary->KeyAt(i); |
| 13017 | if (key->IsNumber()) { |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13018 | Object* value = dictionary->ValueAt(i); |
| 13019 | if (!value->IsNumber()) return false; |
| 13020 | if (!value->IsSmi()) { |
| 13021 | found_double = true; |
| 13022 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13023 | } |
| 13024 | } |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13025 | *has_smi_only_elements = !found_double; |
| 13026 | return found_double; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13027 | } else { |
| 13028 | return false; |
| 13029 | } |
| 13030 | } |
| 13031 | |
| 13032 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13033 | // Certain compilers request function template instantiation when they |
| 13034 | // see the definition of the other template functions in the |
| 13035 | // class. This requires us to have the template functions put |
| 13036 | // together, so even though this function belongs in objects-debug.cc, |
| 13037 | // we keep it here instead to satisfy certain compilers. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13038 | #ifdef OBJECT_PRINT |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13039 | template<typename Shape, typename Key> |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13040 | void Dictionary<Shape, Key>::Print(FILE* out) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13041 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 13042 | for (int i = 0; i < capacity; i++) { |
| 13043 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 13044 | if (HashTable<Shape, Key>::IsKey(k)) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13045 | PrintF(out, " "); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13046 | if (k->IsString()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13047 | String::cast(k)->StringPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13048 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13049 | k->ShortPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13050 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13051 | PrintF(out, ": "); |
| 13052 | ValueAt(i)->ShortPrint(out); |
| 13053 | PrintF(out, "\n"); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13054 | } |
| 13055 | } |
| 13056 | } |
| 13057 | #endif |
| 13058 | |
| 13059 | |
| 13060 | template<typename Shape, typename Key> |
| 13061 | void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13062 | int pos = 0; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13063 | int capacity = HashTable<Shape, Key>::Capacity(); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 13064 | DisallowHeapAllocation no_gc; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13065 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13066 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13067 | Object* k = Dictionary<Shape, Key>::KeyAt(i); |
| 13068 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 13069 | elements->set(pos++, ValueAt(i), mode); |
| 13070 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13071 | } |
| 13072 | ASSERT(pos == elements->length()); |
| 13073 | } |
| 13074 | |
| 13075 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13076 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
| 13077 | ASSERT(map()->has_named_interceptor()); |
| 13078 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13079 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13080 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13081 | constructor->shared()->get_api_func_data()->named_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13082 | return InterceptorInfo::cast(result); |
| 13083 | } |
| 13084 | |
| 13085 | |
| 13086 | InterceptorInfo* JSObject::GetIndexedInterceptor() { |
| 13087 | ASSERT(map()->has_indexed_interceptor()); |
| 13088 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13089 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13090 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13091 | constructor->shared()->get_api_func_data()->indexed_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13092 | return InterceptorInfo::cast(result); |
| 13093 | } |
| 13094 | |
| 13095 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13096 | Handle<Object> JSObject::GetPropertyPostInterceptor( |
| 13097 | Handle<JSObject> object, |
| 13098 | Handle<Object> receiver, |
| 13099 | Handle<Name> name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13100 | PropertyAttributes* attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13101 | // Check local property in holder, ignore interceptor. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13102 | Isolate* isolate = object->GetIsolate(); |
| 13103 | LookupResult lookup(isolate); |
| 13104 | object->LocalLookupRealNamedProperty(*name, &lookup); |
| 13105 | Handle<Object> result; |
| 13106 | if (lookup.IsFound()) { |
| 13107 | result = GetProperty(object, receiver, &lookup, name, attributes); |
| 13108 | } else { |
| 13109 | // Continue searching via the prototype chain. |
| 13110 | Handle<Object> prototype(object->GetPrototype(), isolate); |
| 13111 | *attributes = ABSENT; |
| 13112 | if (prototype->IsNull()) return isolate->factory()->undefined_value(); |
| 13113 | result = GetPropertyWithReceiver(prototype, receiver, name, attributes); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 13114 | } |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13115 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13116 | } |
| 13117 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13118 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13119 | MaybeObject* JSObject::GetLocalPropertyPostInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13120 | Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13121 | Name* name, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13122 | PropertyAttributes* attributes) { |
| 13123 | // Check local property in holder, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13124 | LookupResult result(GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13125 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13126 | if (result.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 13127 | return GetProperty(receiver, &result, name, attributes); |
| 13128 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13129 | return GetHeap()->undefined_value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13130 | } |
| 13131 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13132 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13133 | Handle<Object> JSObject::GetPropertyWithInterceptor( |
| 13134 | Handle<JSObject> object, |
| 13135 | Handle<Object> receiver, |
| 13136 | Handle<Name> name, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 13137 | PropertyAttributes* attributes) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13138 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13139 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13140 | // TODO(rossberg): Support symbols in the API. |
| 13141 | if (name->IsSymbol()) return isolate->factory()->undefined_value(); |
| 13142 | |
| 13143 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor(), isolate); |
| 13144 | Handle<String> name_string = Handle<String>::cast(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13145 | |
| 13146 | if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 13147 | v8::NamedPropertyGetterCallback getter = |
| 13148 | v8::ToCData<v8::NamedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13149 | LOG(isolate, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13150 | ApiNamedPropertyAccess("interceptor-named-get", *object, *name)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 13151 | PropertyCallbackArguments |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13152 | args(isolate, interceptor->data(), *receiver, *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13153 | v8::Handle<v8::Value> result = |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13154 | args.Call(getter, v8::Utils::ToLocal(name_string)); |
| 13155 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13156 | if (!result.IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13157 | *attributes = NONE; |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 13158 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 13159 | result_internal->VerifyApiCallResultType(); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13160 | // Rebox handle to escape this scope. |
| 13161 | return handle(*result_internal, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13162 | } |
| 13163 | } |
| 13164 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13165 | return GetPropertyPostInterceptor(object, receiver, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13166 | } |
| 13167 | |
| 13168 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 13169 | bool JSObject::HasRealNamedProperty(Isolate* isolate, Name* key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13170 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13171 | if (IsAccessCheckNeeded()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13172 | if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 13173 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13174 | return false; |
| 13175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13176 | } |
| 13177 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13178 | LookupResult result(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13179 | LocalLookupRealNamedProperty(key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13180 | return result.IsFound() && !result.IsInterceptor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13181 | } |
| 13182 | |
| 13183 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 13184 | bool JSObject::HasRealElementProperty(Isolate* isolate, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13185 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13186 | if (IsAccessCheckNeeded()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 13187 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 13188 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13189 | return false; |
| 13190 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13191 | } |
| 13192 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 13193 | if (IsJSGlobalProxy()) { |
| 13194 | Object* proto = GetPrototype(); |
| 13195 | if (proto->IsNull()) return false; |
| 13196 | ASSERT(proto->IsJSGlobalObject()); |
| 13197 | return JSObject::cast(proto)->HasRealElementProperty(isolate, index); |
| 13198 | } |
| 13199 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 13200 | return GetElementAttributeWithoutInterceptor(this, index, false) != ABSENT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13201 | } |
| 13202 | |
| 13203 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 13204 | bool JSObject::HasRealNamedCallbackProperty(Isolate* isolate, Name* key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13205 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13206 | if (IsAccessCheckNeeded()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13207 | if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 13208 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13209 | return false; |
| 13210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13211 | } |
| 13212 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13213 | LookupResult result(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13214 | LocalLookupRealNamedProperty(key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13215 | return result.IsPropertyCallbacks(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13216 | } |
| 13217 | |
| 13218 | |
| 13219 | int JSObject::NumberOfLocalProperties(PropertyAttributes filter) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13220 | if (HasFastProperties()) { |
| 13221 | Map* map = this->map(); |
| 13222 | if (filter == NONE) return map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13223 | if (filter & DONT_ENUM) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13224 | int result = map->EnumLength(); |
| 13225 | if (result != Map::kInvalidEnumCache) return result; |
| 13226 | } |
| 13227 | return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter); |
| 13228 | } |
| 13229 | return property_dictionary()->NumberOfElementsFilterAttributes(filter); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13230 | } |
| 13231 | |
| 13232 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13233 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13234 | Object* temp = get(i); |
| 13235 | set(i, get(j)); |
| 13236 | set(j, temp); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13237 | if (this != numbers) { |
| 13238 | temp = numbers->get(i); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 13239 | numbers->set(i, Smi::cast(numbers->get(j))); |
| 13240 | numbers->set(j, Smi::cast(temp)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13241 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13242 | } |
| 13243 | |
| 13244 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13245 | static void InsertionSortPairs(FixedArray* content, |
| 13246 | FixedArray* numbers, |
| 13247 | int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13248 | for (int i = 1; i < len; i++) { |
| 13249 | int j = i; |
| 13250 | while (j > 0 && |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13251 | (NumberToUint32(numbers->get(j - 1)) > |
| 13252 | NumberToUint32(numbers->get(j)))) { |
| 13253 | content->SwapPairs(numbers, j - 1, j); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13254 | j--; |
| 13255 | } |
| 13256 | } |
| 13257 | } |
| 13258 | |
| 13259 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13260 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13261 | // In-place heap sort. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13262 | ASSERT(content->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13263 | |
| 13264 | // Bottom-up max-heap construction. |
| 13265 | for (int i = 1; i < len; ++i) { |
| 13266 | int child_index = i; |
| 13267 | while (child_index > 0) { |
| 13268 | int parent_index = ((child_index + 1) >> 1) - 1; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13269 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 13270 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13271 | if (parent_value < child_value) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13272 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13273 | } else { |
| 13274 | break; |
| 13275 | } |
| 13276 | child_index = parent_index; |
| 13277 | } |
| 13278 | } |
| 13279 | |
| 13280 | // Extract elements and create sorted array. |
| 13281 | for (int i = len - 1; i > 0; --i) { |
| 13282 | // Put max element at the back of the array. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13283 | content->SwapPairs(numbers, 0, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13284 | // Sift down the new top element. |
| 13285 | int parent_index = 0; |
| 13286 | while (true) { |
| 13287 | int child_index = ((parent_index + 1) << 1) - 1; |
| 13288 | if (child_index >= i) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13289 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 13290 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 13291 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13292 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 13293 | if (parent_value > child1_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13294 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13295 | parent_index = child_index; |
| 13296 | } else { |
| 13297 | if (parent_value > child2_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13298 | content->SwapPairs(numbers, parent_index, child_index + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13299 | parent_index = child_index + 1; |
| 13300 | } |
| 13301 | } |
| 13302 | } |
| 13303 | } |
| 13304 | |
| 13305 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13306 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 13307 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
| 13308 | ASSERT(this->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13309 | // For small arrays, simply use insertion sort. |
| 13310 | if (len <= 10) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13311 | InsertionSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13312 | return; |
| 13313 | } |
| 13314 | // Check the range of indices. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13315 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 13316 | uint32_t max_index = min_index; |
| 13317 | uint32_t i; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13318 | for (i = 1; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13319 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 13320 | min_index = NumberToUint32(numbers->get(i)); |
| 13321 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 13322 | max_index = NumberToUint32(numbers->get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13323 | } |
| 13324 | } |
| 13325 | if (max_index - min_index + 1 == len) { |
| 13326 | // Indices form a contiguous range, unless there are duplicates. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13327 | // Do an in-place linear time sort assuming distinct numbers, but |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13328 | // avoid hanging in case they are not. |
| 13329 | for (i = 0; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13330 | uint32_t p; |
| 13331 | uint32_t j = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13332 | // While the current element at i is not at its correct position p, |
| 13333 | // swap the elements at these two positions. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13334 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13335 | j++ < len) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13336 | SwapPairs(numbers, i, p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13337 | } |
| 13338 | } |
| 13339 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13340 | HeapSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13341 | return; |
| 13342 | } |
| 13343 | } |
| 13344 | |
| 13345 | |
| 13346 | // Fill in the names of local properties into the supplied storage. The main |
| 13347 | // purpose of this function is to provide reflection information for the object |
| 13348 | // mirrors. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13349 | void JSObject::GetLocalPropertyNames( |
| 13350 | FixedArray* storage, int index, PropertyAttributes filter) { |
| 13351 | ASSERT(storage->length() >= (NumberOfLocalProperties(filter) - index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13352 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 13353 | int real_size = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 13354 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 13355 | for (int i = 0; i < real_size; i++) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13356 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 13357 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 13358 | storage->set(index++, descs->GetKey(i)); |
| 13359 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13360 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13361 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13362 | property_dictionary()->CopyKeysTo(storage, |
| 13363 | index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13364 | filter, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13365 | NameDictionary::UNSORTED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13366 | } |
| 13367 | } |
| 13368 | |
| 13369 | |
| 13370 | int JSObject::NumberOfLocalElements(PropertyAttributes filter) { |
| 13371 | return GetLocalElementKeys(NULL, filter); |
| 13372 | } |
| 13373 | |
| 13374 | |
| 13375 | int JSObject::NumberOfEnumElements() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13376 | // Fast case for objects with no elements. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13377 | if (!IsJSValue() && HasFastObjectElements()) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13378 | uint32_t length = IsJSArray() ? |
| 13379 | static_cast<uint32_t>( |
| 13380 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 13381 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 13382 | if (length == 0) return 0; |
| 13383 | } |
| 13384 | // Compute the number of enumerable elements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13385 | return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 13386 | } |
| 13387 | |
| 13388 | |
| 13389 | int JSObject::GetLocalElementKeys(FixedArray* storage, |
| 13390 | PropertyAttributes filter) { |
| 13391 | int counter = 0; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13392 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13393 | case FAST_SMI_ELEMENTS: |
| 13394 | case FAST_ELEMENTS: |
| 13395 | case FAST_HOLEY_SMI_ELEMENTS: |
| 13396 | case FAST_HOLEY_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13397 | int length = IsJSArray() ? |
| 13398 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 13399 | FixedArray::cast(elements())->length(); |
| 13400 | for (int i = 0; i < length; i++) { |
| 13401 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 13402 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13403 | storage->set(counter, Smi::FromInt(i)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13404 | } |
| 13405 | counter++; |
| 13406 | } |
| 13407 | } |
| 13408 | ASSERT(!storage || storage->length() >= counter); |
| 13409 | break; |
| 13410 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13411 | case FAST_DOUBLE_ELEMENTS: |
| 13412 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 13413 | int length = IsJSArray() ? |
| 13414 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 13415 | FixedDoubleArray::cast(elements())->length(); |
| 13416 | for (int i = 0; i < length; i++) { |
| 13417 | if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) { |
| 13418 | if (storage != NULL) { |
| 13419 | storage->set(counter, Smi::FromInt(i)); |
| 13420 | } |
| 13421 | counter++; |
| 13422 | } |
| 13423 | } |
| 13424 | ASSERT(!storage || storage->length() >= counter); |
| 13425 | break; |
| 13426 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 13427 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 13428 | int length = ExternalPixelArray::cast(elements())->length(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13429 | while (counter < length) { |
| 13430 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13431 | storage->set(counter, Smi::FromInt(counter)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13432 | } |
| 13433 | counter++; |
| 13434 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13435 | ASSERT(!storage || storage->length() >= counter); |
| 13436 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13437 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13438 | case EXTERNAL_BYTE_ELEMENTS: |
| 13439 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 13440 | case EXTERNAL_SHORT_ELEMENTS: |
| 13441 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 13442 | case EXTERNAL_INT_ELEMENTS: |
| 13443 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13444 | case EXTERNAL_FLOAT_ELEMENTS: |
| 13445 | case EXTERNAL_DOUBLE_ELEMENTS: { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13446 | int length = ExternalArray::cast(elements())->length(); |
| 13447 | while (counter < length) { |
| 13448 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13449 | storage->set(counter, Smi::FromInt(counter)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13450 | } |
| 13451 | counter++; |
| 13452 | } |
| 13453 | ASSERT(!storage || storage->length() >= counter); |
| 13454 | break; |
| 13455 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13456 | case DICTIONARY_ELEMENTS: { |
| 13457 | if (storage != NULL) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13458 | element_dictionary()->CopyKeysTo(storage, |
| 13459 | filter, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13460 | SeededNumberDictionary::SORTED); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13461 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13462 | counter += element_dictionary()->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13463 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13464 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13465 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 13466 | FixedArray* parameter_map = FixedArray::cast(elements()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13467 | int mapped_length = parameter_map->length() - 2; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13468 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 13469 | if (arguments->IsDictionary()) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13470 | // Copy the keys from arguments first, because Dictionary::CopyKeysTo |
| 13471 | // will insert in storage starting at index 0. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13472 | SeededNumberDictionary* dictionary = |
| 13473 | SeededNumberDictionary::cast(arguments); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13474 | if (storage != NULL) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13475 | dictionary->CopyKeysTo( |
| 13476 | storage, filter, SeededNumberDictionary::UNSORTED); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13477 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13478 | counter += dictionary->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13479 | for (int i = 0; i < mapped_length; ++i) { |
| 13480 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 13481 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13482 | ++counter; |
| 13483 | } |
| 13484 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13485 | if (storage != NULL) storage->SortPairs(storage, counter); |
| 13486 | |
| 13487 | } else { |
| 13488 | int backing_length = arguments->length(); |
| 13489 | int i = 0; |
| 13490 | for (; i < mapped_length; ++i) { |
| 13491 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 13492 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13493 | ++counter; |
| 13494 | } else if (i < backing_length && !arguments->get(i)->IsTheHole()) { |
| 13495 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13496 | ++counter; |
| 13497 | } |
| 13498 | } |
| 13499 | for (; i < backing_length; ++i) { |
| 13500 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13501 | ++counter; |
| 13502 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13503 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13504 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13505 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13506 | } |
| 13507 | |
| 13508 | if (this->IsJSValue()) { |
| 13509 | Object* val = JSValue::cast(this)->value(); |
| 13510 | if (val->IsString()) { |
| 13511 | String* str = String::cast(val); |
| 13512 | if (storage) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13513 | for (int i = 0; i < str->length(); i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13514 | storage->set(counter + i, Smi::FromInt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13515 | } |
| 13516 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13517 | counter += str->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13518 | } |
| 13519 | } |
| 13520 | ASSERT(!storage || storage->length() == counter); |
| 13521 | return counter; |
| 13522 | } |
| 13523 | |
| 13524 | |
| 13525 | int JSObject::GetEnumElementKeys(FixedArray* storage) { |
| 13526 | return GetLocalElementKeys(storage, |
| 13527 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 13528 | } |
| 13529 | |
| 13530 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13531 | // StringKey simply carries a string object as key. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13532 | class StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13533 | public: |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13534 | explicit StringKey(String* string) : |
| 13535 | string_(string), |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13536 | hash_(HashForObject(string)) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13537 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13538 | bool IsMatch(Object* string) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13539 | // We know that all entries in a hash table had their hash keys created. |
| 13540 | // Use that knowledge to have fast failure. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13541 | if (hash_ != HashForObject(string)) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13542 | return false; |
| 13543 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13544 | return string_->Equals(String::cast(string)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13545 | } |
| 13546 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13547 | uint32_t Hash() { return hash_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13548 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13549 | uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13550 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13551 | Object* AsObject(Heap* heap) { return string_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13552 | |
| 13553 | String* string_; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13554 | uint32_t hash_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13555 | }; |
| 13556 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13557 | |
| 13558 | // StringSharedKeys are used as keys in the eval cache. |
| 13559 | class StringSharedKey : public HashTableKey { |
| 13560 | public: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13561 | StringSharedKey(String* source, |
| 13562 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13563 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13564 | int scope_position) |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13565 | : source_(source), |
| 13566 | shared_(shared), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13567 | language_mode_(language_mode), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13568 | scope_position_(scope_position) { } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13569 | |
| 13570 | bool IsMatch(Object* other) { |
| 13571 | if (!other->IsFixedArray()) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13572 | FixedArray* other_array = FixedArray::cast(other); |
| 13573 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13574 | if (shared != shared_) return false; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13575 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 13576 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 13577 | language_unchecked == STRICT_MODE || |
| 13578 | language_unchecked == EXTENDED_MODE); |
| 13579 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
| 13580 | if (language_mode != language_mode_) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13581 | int scope_position = Smi::cast(other_array->get(3))->value(); |
| 13582 | if (scope_position != scope_position_) return false; |
| 13583 | String* source = String::cast(other_array->get(1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13584 | return source->Equals(source_); |
| 13585 | } |
| 13586 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13587 | static uint32_t StringSharedHashHelper(String* source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13588 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13589 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13590 | int scope_position) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13591 | uint32_t hash = source->Hash(); |
| 13592 | if (shared->HasSourceCode()) { |
| 13593 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 13594 | // code computation, we use a combination of the hash of the |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13595 | // script source code and the start position of the calling scope. |
| 13596 | // 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] | 13597 | // collection. |
| 13598 | Script* script = Script::cast(shared->script()); |
| 13599 | hash ^= String::cast(script->source())->Hash(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13600 | if (language_mode == STRICT_MODE) hash ^= 0x8000; |
| 13601 | if (language_mode == EXTENDED_MODE) hash ^= 0x0080; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13602 | hash += scope_position; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13603 | } |
| 13604 | return hash; |
| 13605 | } |
| 13606 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13607 | uint32_t Hash() { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13608 | return StringSharedHashHelper( |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13609 | source_, shared_, language_mode_, scope_position_); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13610 | } |
| 13611 | |
| 13612 | uint32_t HashForObject(Object* obj) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13613 | FixedArray* other_array = FixedArray::cast(obj); |
| 13614 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
| 13615 | String* source = String::cast(other_array->get(1)); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13616 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 13617 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 13618 | language_unchecked == STRICT_MODE || |
| 13619 | language_unchecked == EXTENDED_MODE); |
| 13620 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13621 | int scope_position = Smi::cast(other_array->get(3))->value(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13622 | return StringSharedHashHelper( |
| 13623 | source, shared, language_mode, scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13624 | } |
| 13625 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13626 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13627 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13628 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13629 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13630 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13631 | FixedArray* other_array = FixedArray::cast(obj); |
| 13632 | other_array->set(0, shared_); |
| 13633 | other_array->set(1, source_); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13634 | other_array->set(2, Smi::FromInt(language_mode_)); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13635 | other_array->set(3, Smi::FromInt(scope_position_)); |
| 13636 | return other_array; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13637 | } |
| 13638 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13639 | private: |
| 13640 | String* source_; |
| 13641 | SharedFunctionInfo* shared_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13642 | LanguageMode language_mode_; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13643 | int scope_position_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13644 | }; |
| 13645 | |
| 13646 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13647 | // RegExpKey carries the source and flags of a regular expression as key. |
| 13648 | class RegExpKey : public HashTableKey { |
| 13649 | public: |
| 13650 | RegExpKey(String* string, JSRegExp::Flags flags) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 13651 | : string_(string), |
| 13652 | flags_(Smi::FromInt(flags.value())) { } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13653 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 13654 | // Rather than storing the key in the hash table, a pointer to the |
| 13655 | // stored value is stored where the key should be. IsMatch then |
| 13656 | // compares the search key to the found object, rather than comparing |
| 13657 | // a key to a key. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13658 | bool IsMatch(Object* obj) { |
| 13659 | FixedArray* val = FixedArray::cast(obj); |
| 13660 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 13661 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 13662 | } |
| 13663 | |
| 13664 | uint32_t Hash() { return RegExpHash(string_, flags_); } |
| 13665 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13666 | Object* AsObject(Heap* heap) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13667 | // Plain hash maps, which is where regexp keys are used, don't |
| 13668 | // use this function. |
| 13669 | UNREACHABLE(); |
| 13670 | return NULL; |
| 13671 | } |
| 13672 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13673 | uint32_t HashForObject(Object* obj) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13674 | FixedArray* val = FixedArray::cast(obj); |
| 13675 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 13676 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 13677 | } |
| 13678 | |
| 13679 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 13680 | return string->Hash() + flags->value(); |
| 13681 | } |
| 13682 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13683 | String* string_; |
| 13684 | Smi* flags_; |
| 13685 | }; |
| 13686 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 13687 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13688 | // Utf8StringKey carries a vector of chars as key. |
| 13689 | class Utf8StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13690 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13691 | explicit Utf8StringKey(Vector<const char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13692 | : string_(string), hash_field_(0), seed_(seed) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13693 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13694 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13695 | return String::cast(string)->IsUtf8EqualTo(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13696 | } |
| 13697 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13698 | uint32_t Hash() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13699 | if (hash_field_ != 0) return hash_field_ >> String::kHashShift; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13700 | hash_field_ = StringHasher::ComputeUtf8Hash(string_, seed_, &chars_); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13701 | uint32_t result = hash_field_ >> String::kHashShift; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13702 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13703 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13704 | } |
| 13705 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13706 | uint32_t HashForObject(Object* other) { |
| 13707 | return String::cast(other)->Hash(); |
| 13708 | } |
| 13709 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13710 | MaybeObject* AsObject(Heap* heap) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13711 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13712 | return heap->AllocateInternalizedStringFromUtf8(string_, |
| 13713 | chars_, |
| 13714 | hash_field_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13715 | } |
| 13716 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13717 | Vector<const char> string_; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13718 | uint32_t hash_field_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13719 | 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] | 13720 | uint32_t seed_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13721 | }; |
| 13722 | |
| 13723 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13724 | template <typename Char> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13725 | class SequentialStringKey : public HashTableKey { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13726 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13727 | explicit SequentialStringKey(Vector<const Char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13728 | : string_(string), hash_field_(0), seed_(seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13729 | |
| 13730 | uint32_t Hash() { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13731 | hash_field_ = StringHasher::HashSequentialString<Char>(string_.start(), |
| 13732 | string_.length(), |
| 13733 | seed_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13734 | |
| 13735 | uint32_t result = hash_field_ >> String::kHashShift; |
| 13736 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13737 | return result; |
| 13738 | } |
| 13739 | |
| 13740 | |
| 13741 | uint32_t HashForObject(Object* other) { |
| 13742 | return String::cast(other)->Hash(); |
| 13743 | } |
| 13744 | |
| 13745 | Vector<const Char> string_; |
| 13746 | uint32_t hash_field_; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13747 | uint32_t seed_; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13748 | }; |
| 13749 | |
| 13750 | |
| 13751 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13752 | class OneByteStringKey : public SequentialStringKey<uint8_t> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13753 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13754 | OneByteStringKey(Vector<const uint8_t> str, uint32_t seed) |
| 13755 | : SequentialStringKey<uint8_t>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13756 | |
| 13757 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13758 | return String::cast(string)->IsOneByteEqualTo(string_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13759 | } |
| 13760 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13761 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13762 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13763 | return heap->AllocateOneByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13764 | } |
| 13765 | }; |
| 13766 | |
| 13767 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13768 | class SubStringOneByteStringKey : public HashTableKey { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13769 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13770 | explicit SubStringOneByteStringKey(Handle<SeqOneByteString> string, |
| 13771 | int from, |
| 13772 | int length) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 13773 | : string_(string), from_(from), length_(length) { } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13774 | |
| 13775 | uint32_t Hash() { |
| 13776 | ASSERT(length_ >= 0); |
| 13777 | ASSERT(from_ + length_ <= string_->length()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13778 | uint8_t* chars = string_->GetChars() + from_; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13779 | hash_field_ = StringHasher::HashSequentialString( |
| 13780 | chars, length_, string_->GetHeap()->HashSeed()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13781 | uint32_t result = hash_field_ >> String::kHashShift; |
| 13782 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13783 | return result; |
| 13784 | } |
| 13785 | |
| 13786 | |
| 13787 | uint32_t HashForObject(Object* other) { |
| 13788 | return String::cast(other)->Hash(); |
| 13789 | } |
| 13790 | |
| 13791 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13792 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
| 13793 | return String::cast(string)->IsOneByteEqualTo(chars); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13794 | } |
| 13795 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13796 | MaybeObject* AsObject(Heap* heap) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13797 | if (hash_field_ == 0) Hash(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13798 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13799 | return heap->AllocateOneByteInternalizedString(chars, hash_field_); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13800 | } |
| 13801 | |
| 13802 | private: |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 13803 | Handle<SeqOneByteString> string_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13804 | int from_; |
| 13805 | int length_; |
| 13806 | uint32_t hash_field_; |
| 13807 | }; |
| 13808 | |
| 13809 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13810 | class TwoByteStringKey : public SequentialStringKey<uc16> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13811 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13812 | explicit TwoByteStringKey(Vector<const uc16> str, uint32_t seed) |
| 13813 | : SequentialStringKey<uc16>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13814 | |
| 13815 | bool IsMatch(Object* string) { |
| 13816 | return String::cast(string)->IsTwoByteEqualTo(string_); |
| 13817 | } |
| 13818 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13819 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13820 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13821 | return heap->AllocateTwoByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13822 | } |
| 13823 | }; |
| 13824 | |
| 13825 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13826 | // InternalizedStringKey carries a string/internalized-string object as key. |
| 13827 | class InternalizedStringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13828 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13829 | explicit InternalizedStringKey(String* string) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13830 | : string_(string) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13831 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13832 | bool IsMatch(Object* string) { |
| 13833 | return String::cast(string)->Equals(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13834 | } |
| 13835 | |
| 13836 | uint32_t Hash() { return string_->Hash(); } |
| 13837 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13838 | uint32_t HashForObject(Object* other) { |
| 13839 | return String::cast(other)->Hash(); |
| 13840 | } |
| 13841 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13842 | MaybeObject* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13843 | // Attempt to flatten the string, so that internalized strings will most |
| 13844 | // often be flat strings. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 13845 | string_ = string_->TryFlattenGetString(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13846 | // Internalize the string if possible. |
| 13847 | Map* map = heap->InternalizedStringMapForString(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13848 | if (map != NULL) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 13849 | string_->set_map_no_write_barrier(map); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13850 | ASSERT(string_->IsInternalizedString()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13851 | return string_; |
| 13852 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13853 | // Otherwise allocate a new internalized string. |
| 13854 | return heap->AllocateInternalizedStringImpl( |
| 13855 | string_, string_->length(), string_->hash_field()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13856 | } |
| 13857 | |
| 13858 | static uint32_t StringHash(Object* obj) { |
| 13859 | return String::cast(obj)->Hash(); |
| 13860 | } |
| 13861 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13862 | String* string_; |
| 13863 | }; |
| 13864 | |
| 13865 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13866 | template<typename Shape, typename Key> |
| 13867 | void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13868 | IteratePointers(v, 0, kElementsStartOffset); |
| 13869 | } |
| 13870 | |
| 13871 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13872 | template<typename Shape, typename Key> |
| 13873 | void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13874 | IteratePointers(v, |
| 13875 | kElementsStartOffset, |
| 13876 | kHeaderSize + length() * kPointerSize); |
| 13877 | } |
| 13878 | |
| 13879 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13880 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13881 | MaybeObject* HashTable<Shape, Key>::Allocate(Heap* heap, |
| 13882 | int at_least_space_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13883 | MinimumCapacity capacity_option, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13884 | PretenureFlag pretenure) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 13885 | ASSERT(!capacity_option || IS_POWER_OF_TWO(at_least_space_for)); |
| 13886 | int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY) |
| 13887 | ? at_least_space_for |
| 13888 | : ComputeCapacity(at_least_space_for); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13889 | if (capacity > HashTable::kMaxCapacity) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13890 | return Failure::OutOfMemoryException(0x10); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13891 | } |
| 13892 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13893 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13894 | { MaybeObject* maybe_obj = |
| 13895 | heap-> AllocateHashTable(EntryToIndex(capacity), pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13896 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13897 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13898 | HashTable::cast(obj)->SetNumberOfElements(0); |
| 13899 | HashTable::cast(obj)->SetNumberOfDeletedElements(0); |
| 13900 | HashTable::cast(obj)->SetCapacity(capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13901 | return obj; |
| 13902 | } |
| 13903 | |
| 13904 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 13905 | // Find entry for key otherwise return kNotFound. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13906 | int NameDictionary::FindEntry(Name* key) { |
| 13907 | if (!key->IsUniqueName()) { |
| 13908 | return HashTable<NameDictionaryShape, Name*>::FindEntry(key); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13909 | } |
| 13910 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13911 | // Optimized for unique names. Knowledge of the key type allows: |
| 13912 | // 1. Move the check if the key is unique out of the loop. |
| 13913 | // 2. Avoid comparing hash codes in unique-to-unique comparison. |
| 13914 | // 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] | 13915 | // In case of positive result the dictionary key may be replaced by the |
| 13916 | // internalized string with minimal performance penalty. It gives a chance |
| 13917 | // to perform further lookups in code stubs (and significant performance |
| 13918 | // boost a certain style of code). |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13919 | |
| 13920 | // EnsureCapacity will guarantee the hash table is never full. |
| 13921 | uint32_t capacity = Capacity(); |
| 13922 | uint32_t entry = FirstProbe(key->Hash(), capacity); |
| 13923 | uint32_t count = 1; |
| 13924 | |
| 13925 | while (true) { |
| 13926 | int index = EntryToIndex(entry); |
| 13927 | Object* element = get(index); |
| 13928 | if (element->IsUndefined()) break; // Empty entry. |
| 13929 | if (key == element) return entry; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13930 | if (!element->IsUniqueName() && |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 13931 | !element->IsTheHole() && |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13932 | Name::cast(element)->Equals(key)) { |
| 13933 | // 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] | 13934 | // internalized string for faster further lookups. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13935 | set(index, key); |
| 13936 | return entry; |
| 13937 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13938 | ASSERT(element->IsTheHole() || !Name::cast(element)->Equals(key)); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 13939 | entry = NextProbe(entry, count++, capacity); |
| 13940 | } |
| 13941 | return kNotFound; |
| 13942 | } |
| 13943 | |
| 13944 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13945 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13946 | MaybeObject* HashTable<Shape, Key>::Rehash(HashTable* new_table, Key key) { |
| 13947 | ASSERT(NumberOfElements() < new_table->Capacity()); |
| 13948 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 13949 | DisallowHeapAllocation no_gc; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13950 | WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc); |
| 13951 | |
| 13952 | // Copy prefix to new array. |
| 13953 | for (int i = kPrefixStartIndex; |
| 13954 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 13955 | i++) { |
| 13956 | new_table->set(i, get(i), mode); |
| 13957 | } |
| 13958 | |
| 13959 | // Rehash the elements. |
| 13960 | int capacity = Capacity(); |
| 13961 | for (int i = 0; i < capacity; i++) { |
| 13962 | uint32_t from_index = EntryToIndex(i); |
| 13963 | Object* k = get(from_index); |
| 13964 | if (IsKey(k)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13965 | uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 13966 | uint32_t insertion_index = |
| 13967 | EntryToIndex(new_table->FindInsertionEntry(hash)); |
| 13968 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 13969 | new_table->set(insertion_index + j, get(from_index + j), mode); |
| 13970 | } |
| 13971 | } |
| 13972 | } |
| 13973 | new_table->SetNumberOfElements(NumberOfElements()); |
| 13974 | new_table->SetNumberOfDeletedElements(0); |
| 13975 | return new_table; |
| 13976 | } |
| 13977 | |
| 13978 | |
| 13979 | template<typename Shape, typename Key> |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 13980 | uint32_t HashTable<Shape, Key>::EntryForProbe(Key key, |
| 13981 | Object* k, |
| 13982 | int probe, |
| 13983 | uint32_t expected) { |
| 13984 | uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k); |
| 13985 | uint32_t capacity = Capacity(); |
| 13986 | uint32_t entry = FirstProbe(hash, capacity); |
| 13987 | for (int i = 1; i < probe; i++) { |
| 13988 | if (entry == expected) return expected; |
| 13989 | entry = NextProbe(entry, i, capacity); |
| 13990 | } |
| 13991 | return entry; |
| 13992 | } |
| 13993 | |
| 13994 | |
| 13995 | template<typename Shape, typename Key> |
| 13996 | void HashTable<Shape, Key>::Swap(uint32_t entry1, |
| 13997 | uint32_t entry2, |
| 13998 | WriteBarrierMode mode) { |
| 13999 | int index1 = EntryToIndex(entry1); |
| 14000 | int index2 = EntryToIndex(entry2); |
| 14001 | Object* temp[Shape::kEntrySize]; |
| 14002 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14003 | temp[j] = get(index1 + j); |
| 14004 | } |
| 14005 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14006 | set(index1 + j, get(index2 + j), mode); |
| 14007 | } |
| 14008 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14009 | set(index2 + j, temp[j], mode); |
| 14010 | } |
| 14011 | } |
| 14012 | |
| 14013 | |
| 14014 | template<typename Shape, typename Key> |
| 14015 | void HashTable<Shape, Key>::Rehash(Key key) { |
| 14016 | DisallowHeapAllocation no_gc; |
| 14017 | WriteBarrierMode mode = GetWriteBarrierMode(no_gc); |
| 14018 | uint32_t capacity = Capacity(); |
| 14019 | bool done = false; |
| 14020 | for (int probe = 1; !done; probe++) { |
| 14021 | // All elements at entries given by one of the first _probe_ probes |
| 14022 | // are placed correctly. Other elements might need to be moved. |
| 14023 | done = true; |
| 14024 | for (uint32_t current = 0; current < capacity; current++) { |
| 14025 | Object* current_key = get(EntryToIndex(current)); |
| 14026 | if (IsKey(current_key)) { |
| 14027 | uint32_t target = EntryForProbe(key, current_key, probe, current); |
| 14028 | if (current == target) continue; |
| 14029 | Object* target_key = get(EntryToIndex(target)); |
| 14030 | if (!IsKey(target_key) || |
| 14031 | EntryForProbe(key, target_key, probe, target) != target) { |
| 14032 | // Put the current element into the correct position. |
| 14033 | Swap(current, target, mode); |
| 14034 | // The other element will be processed on the next iteration. |
| 14035 | current--; |
| 14036 | } else { |
| 14037 | // The place for the current element is occupied. Leave the element |
| 14038 | // for the next probe. |
| 14039 | done = false; |
| 14040 | } |
| 14041 | } |
| 14042 | } |
| 14043 | } |
| 14044 | } |
| 14045 | |
| 14046 | |
| 14047 | template<typename Shape, typename Key> |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14048 | MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, |
| 14049 | Key key, |
| 14050 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14051 | int capacity = Capacity(); |
| 14052 | int nof = NumberOfElements() + n; |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 14053 | int nod = NumberOfDeletedElements(); |
| 14054 | // Return if: |
| 14055 | // 50% is still free after adding n elements and |
| 14056 | // at most 50% of the free elements are deleted elements. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14057 | if (nod <= (capacity - nof) >> 1) { |
| 14058 | int needed_free = nof >> 1; |
| 14059 | if (nof + needed_free <= capacity) return this; |
| 14060 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14061 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14062 | const int kMinCapacityForPretenure = 256; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14063 | bool should_pretenure = pretenure == TENURED || |
| 14064 | ((capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14065 | Object* obj; |
| 14066 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14067 | Allocate(GetHeap(), |
| 14068 | nof * 2, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14069 | USE_DEFAULT_MINIMUM_CAPACITY, |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14070 | should_pretenure ? TENURED : NOT_TENURED); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14071 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14072 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 14073 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14074 | return Rehash(HashTable::cast(obj), key); |
| 14075 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14076 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14077 | |
| 14078 | template<typename Shape, typename Key> |
| 14079 | MaybeObject* HashTable<Shape, Key>::Shrink(Key key) { |
| 14080 | int capacity = Capacity(); |
| 14081 | int nof = NumberOfElements(); |
| 14082 | |
| 14083 | // Shrink to fit the number of elements if only a quarter of the |
| 14084 | // capacity is filled with elements. |
| 14085 | if (nof > (capacity >> 2)) return this; |
| 14086 | // Allocate a new dictionary with room for at least the current |
| 14087 | // number of elements. The allocation method will make sure that |
| 14088 | // there is extra room in the dictionary for additions. Don't go |
| 14089 | // lower than room for 16 elements. |
| 14090 | int at_least_room_for = nof; |
| 14091 | if (at_least_room_for < 16) return this; |
| 14092 | |
| 14093 | const int kMinCapacityForPretenure = 256; |
| 14094 | bool pretenure = |
| 14095 | (at_least_room_for > kMinCapacityForPretenure) && |
| 14096 | !GetHeap()->InNewSpace(this); |
| 14097 | Object* obj; |
| 14098 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14099 | Allocate(GetHeap(), |
| 14100 | at_least_room_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14101 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 14102 | pretenure ? TENURED : NOT_TENURED); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14103 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14104 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14105 | |
| 14106 | return Rehash(HashTable::cast(obj), key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14107 | } |
| 14108 | |
| 14109 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14110 | template<typename Shape, typename Key> |
| 14111 | uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14112 | uint32_t capacity = Capacity(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14113 | uint32_t entry = FirstProbe(hash, capacity); |
| 14114 | uint32_t count = 1; |
| 14115 | // EnsureCapacity will guarantee the hash table is never full. |
| 14116 | while (true) { |
| 14117 | Object* element = KeyAt(entry); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14118 | if (element->IsUndefined() || element->IsTheHole()) break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14119 | entry = NextProbe(entry, count++, capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14120 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14121 | return entry; |
| 14122 | } |
| 14123 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14124 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14125 | // Force instantiation of template instances class. |
| 14126 | // Please note this list is compiler dependent. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14127 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14128 | template class HashTable<StringTableShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14129 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14130 | template class HashTable<CompilationCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14131 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14132 | template class HashTable<MapCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14133 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14134 | template class HashTable<ObjectHashTableShape<1>, Object*>; |
| 14135 | |
| 14136 | template class HashTable<ObjectHashTableShape<2>, Object*>; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14137 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14138 | template class HashTable<WeakHashTableShape<2>, Object*>; |
| 14139 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14140 | template class Dictionary<NameDictionaryShape, Name*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14141 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14142 | template class Dictionary<SeededNumberDictionaryShape, uint32_t>; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14143 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14144 | template class Dictionary<UnseededNumberDictionaryShape, uint32_t>; |
| 14145 | |
| 14146 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14147 | Allocate(Heap* heap, int at_least_space_for, PretenureFlag pretenure); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14148 | |
| 14149 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14150 | Allocate(Heap* heap, int at_least_space_for, PretenureFlag pretenure); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14151 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14152 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14153 | Allocate(Heap* heap, int n, PretenureFlag pretenure); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14154 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14155 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::AtPut( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14156 | uint32_t, Object*); |
| 14157 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14158 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14159 | AtPut(uint32_t, Object*); |
| 14160 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 14161 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14162 | SlowReverseLookup(Object* value); |
| 14163 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14164 | template Object* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14165 | SlowReverseLookup(Object* value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14166 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14167 | template Object* Dictionary<NameDictionaryShape, Name*>::SlowReverseLookup( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14168 | Object*); |
| 14169 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14170 | template void Dictionary<SeededNumberDictionaryShape, uint32_t>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14171 | FixedArray*, |
| 14172 | PropertyAttributes, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14173 | Dictionary<SeededNumberDictionaryShape, uint32_t>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14174 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14175 | template Object* Dictionary<NameDictionaryShape, Name*>::DeleteProperty( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14176 | int, JSObject::DeleteMode); |
| 14177 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14178 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14179 | DeleteProperty(int, JSObject::DeleteMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14180 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14181 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Shrink(Name* n); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14182 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14183 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Shrink( |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14184 | uint32_t); |
| 14185 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14186 | template void Dictionary<NameDictionaryShape, Name*>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14187 | FixedArray*, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 14188 | int, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 14189 | PropertyAttributes, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14190 | Dictionary<NameDictionaryShape, Name*>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14191 | |
| 14192 | template int |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14193 | Dictionary<NameDictionaryShape, Name*>::NumberOfElementsFilterAttributes( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14194 | PropertyAttributes); |
| 14195 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14196 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Add( |
| 14197 | Name*, Object*, PropertyDetails); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14198 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14199 | template MaybeObject* |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14200 | Dictionary<NameDictionaryShape, Name*>::GenerateNewEnumerationIndices(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14201 | |
| 14202 | template int |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14203 | Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14204 | NumberOfElementsFilterAttributes(PropertyAttributes); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14205 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14206 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Add( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14207 | uint32_t, Object*, PropertyDetails); |
| 14208 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14209 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::Add( |
| 14210 | uint32_t, Object*, PropertyDetails); |
| 14211 | |
| 14212 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14213 | EnsureCapacity(int, uint32_t); |
| 14214 | |
| 14215 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14216 | EnsureCapacity(int, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14217 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14218 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
| 14219 | EnsureCapacity(int, Name*); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14220 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14221 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14222 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
| 14223 | |
| 14224 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14225 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14226 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14227 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::AddEntry( |
| 14228 | Name*, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14229 | |
| 14230 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14231 | int Dictionary<SeededNumberDictionaryShape, uint32_t>::NumberOfEnumElements(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14232 | |
| 14233 | template |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14234 | int Dictionary<NameDictionaryShape, Name*>::NumberOfEnumElements(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14235 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14236 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14237 | int HashTable<SeededNumberDictionaryShape, uint32_t>::FindEntry(uint32_t); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14238 | |
| 14239 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14240 | // Collates undefined and unexisting elements below limit from position |
| 14241 | // zero of the elements. The object stays in Dictionary mode. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14242 | MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14243 | ASSERT(HasDictionaryElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14244 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 14245 | // or because we are not going to sort (and therefore compact) all of the |
| 14246 | // elements. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14247 | SeededNumberDictionary* dict = element_dictionary(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14248 | HeapNumber* result_double = NULL; |
| 14249 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14250 | // Allocate space for result before we start mutating the object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14251 | Object* new_double; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14252 | { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14253 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 14254 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14255 | result_double = HeapNumber::cast(new_double); |
| 14256 | } |
| 14257 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14258 | Object* obj; |
| 14259 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14260 | SeededNumberDictionary::Allocate(GetHeap(), dict->NumberOfElements()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14261 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14262 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14263 | SeededNumberDictionary* new_dict = SeededNumberDictionary::cast(obj); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14264 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14265 | DisallowHeapAllocation no_alloc; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14266 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14267 | uint32_t pos = 0; |
| 14268 | uint32_t undefs = 0; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14269 | int capacity = dict->Capacity(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14270 | for (int i = 0; i < capacity; i++) { |
| 14271 | Object* k = dict->KeyAt(i); |
| 14272 | if (dict->IsKey(k)) { |
| 14273 | ASSERT(k->IsNumber()); |
| 14274 | ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 14275 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 14276 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 14277 | Object* value = dict->ValueAt(i); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 14278 | PropertyDetails details = dict->DetailsAt(i); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 14279 | if (details.type() == CALLBACKS || details.IsReadOnly()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 14280 | // 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] | 14281 | // 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] | 14282 | return Smi::FromInt(-1); |
| 14283 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14284 | uint32_t key = NumberToUint32(k); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14285 | // In the following we assert that adding the entry to the new dictionary |
| 14286 | // does not cause GC. This is the case because we made sure to allocate |
| 14287 | // the dictionary big enough above, so it need not grow. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14288 | if (key < limit) { |
| 14289 | if (value->IsUndefined()) { |
| 14290 | undefs++; |
| 14291 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14292 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14293 | // Adding an entry with the key beyond smi-range requires |
| 14294 | // allocation. Bailout. |
| 14295 | return Smi::FromInt(-1); |
| 14296 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14297 | new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14298 | pos++; |
| 14299 | } |
| 14300 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14301 | if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14302 | // Adding an entry with the key beyond smi-range requires |
| 14303 | // allocation. Bailout. |
| 14304 | return Smi::FromInt(-1); |
| 14305 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14306 | new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14307 | } |
| 14308 | } |
| 14309 | } |
| 14310 | |
| 14311 | uint32_t result = pos; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14312 | PropertyDetails no_details = PropertyDetails(NONE, NORMAL, 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14313 | Heap* heap = GetHeap(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14314 | while (undefs > 0) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14315 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14316 | // Adding an entry with the key beyond smi-range requires |
| 14317 | // allocation. Bailout. |
| 14318 | return Smi::FromInt(-1); |
| 14319 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14320 | new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)-> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14321 | ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14322 | pos++; |
| 14323 | undefs--; |
| 14324 | } |
| 14325 | |
| 14326 | set_elements(new_dict); |
| 14327 | |
| 14328 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14329 | return Smi::FromInt(static_cast<int>(result)); |
| 14330 | } |
| 14331 | |
| 14332 | ASSERT_NE(NULL, result_double); |
| 14333 | result_double->set_value(static_cast<double>(result)); |
| 14334 | return result_double; |
| 14335 | } |
| 14336 | |
| 14337 | |
| 14338 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 14339 | // the start of the elements array. |
| 14340 | // If the object is in dictionary mode, it is converted to fast elements |
| 14341 | // mode. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14342 | MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14343 | Heap* heap = GetHeap(); |
| 14344 | |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 14345 | ASSERT(!map()->is_observed()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14346 | if (HasDictionaryElements()) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14347 | // Convert to fast elements containing only the existing properties. |
| 14348 | // Ordering is irrelevant, since we are going to sort anyway. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14349 | SeededNumberDictionary* dict = element_dictionary(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14350 | if (IsJSArray() || dict->requires_slow_elements() || |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 14351 | dict->max_number_key() >= limit) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14352 | return PrepareSlowElementsForSort(limit); |
| 14353 | } |
| 14354 | // Convert to fast elements. |
| 14355 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14356 | Object* obj; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 14357 | MaybeObject* maybe_obj = GetElementsTransitionMap(GetIsolate(), |
| 14358 | FAST_HOLEY_ELEMENTS); |
| 14359 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 14360 | Map* new_map = Map::cast(obj); |
| 14361 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14362 | PretenureFlag tenure = heap->InNewSpace(this) ? NOT_TENURED: TENURED; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14363 | Object* new_array; |
| 14364 | { MaybeObject* maybe_new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14365 | heap->AllocateFixedArray(dict->NumberOfElements(), tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14366 | if (!maybe_new_array->ToObject(&new_array)) return maybe_new_array; |
| 14367 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14368 | FixedArray* fast_elements = FixedArray::cast(new_array); |
| 14369 | dict->CopyValuesTo(fast_elements); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 14370 | ValidateElements(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 14371 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 14372 | set_map_and_elements(new_map, fast_elements); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 14373 | } else if (HasExternalArrayElements()) { |
| 14374 | // External arrays cannot have holes or undefined elements. |
| 14375 | return Smi::FromInt(ExternalArray::cast(elements())->length()); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14376 | } else if (!HasFastDoubleElements()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14377 | Object* obj; |
| 14378 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 14379 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14380 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14381 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 14382 | ASSERT(HasFastSmiOrObjectElements() || HasFastDoubleElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14383 | |
| 14384 | // Collect holes at the end, undefined before that and the rest at the |
| 14385 | // start, and return the number of non-hole, non-undefined values. |
| 14386 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14387 | FixedArrayBase* elements_base = FixedArrayBase::cast(this->elements()); |
| 14388 | uint32_t elements_length = static_cast<uint32_t>(elements_base->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14389 | if (limit > elements_length) { |
| 14390 | limit = elements_length ; |
| 14391 | } |
| 14392 | if (limit == 0) { |
| 14393 | return Smi::FromInt(0); |
| 14394 | } |
| 14395 | |
| 14396 | HeapNumber* result_double = NULL; |
| 14397 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14398 | // Pessimistically allocate space for return value before |
| 14399 | // we start mutating the array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14400 | Object* new_double; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14401 | { MaybeObject* maybe_new_double = heap->AllocateHeapNumber(0.0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14402 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 14403 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14404 | result_double = HeapNumber::cast(new_double); |
| 14405 | } |
| 14406 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14407 | uint32_t result = 0; |
| 14408 | if (elements_base->map() == heap->fixed_double_array_map()) { |
| 14409 | FixedDoubleArray* elements = FixedDoubleArray::cast(elements_base); |
| 14410 | // Split elements into defined and the_hole, in that order. |
| 14411 | unsigned int holes = limit; |
| 14412 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 14413 | // number of stores of non-undefined, non-the-hole values. |
| 14414 | for (unsigned int i = 0; i < holes; i++) { |
| 14415 | if (elements->is_the_hole(i)) { |
| 14416 | holes--; |
| 14417 | } else { |
| 14418 | continue; |
| 14419 | } |
| 14420 | // Position i needs to be filled. |
| 14421 | while (holes > i) { |
| 14422 | if (elements->is_the_hole(holes)) { |
| 14423 | holes--; |
| 14424 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 14425 | elements->set(i, elements->get_scalar(holes)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14426 | break; |
| 14427 | } |
| 14428 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14429 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14430 | result = holes; |
| 14431 | while (holes < limit) { |
| 14432 | elements->set_the_hole(holes); |
| 14433 | holes++; |
| 14434 | } |
| 14435 | } else { |
| 14436 | FixedArray* elements = FixedArray::cast(elements_base); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14437 | DisallowHeapAllocation no_gc; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14438 | |
| 14439 | // Split elements into defined, undefined and the_hole, in that order. Only |
| 14440 | // count locations for undefined and the hole, and fill them afterwards. |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14441 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14442 | unsigned int undefs = limit; |
| 14443 | unsigned int holes = limit; |
| 14444 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 14445 | // number of stores of non-undefined, non-the-hole values. |
| 14446 | for (unsigned int i = 0; i < undefs; i++) { |
| 14447 | Object* current = elements->get(i); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14448 | if (current->IsTheHole()) { |
| 14449 | holes--; |
| 14450 | undefs--; |
| 14451 | } else if (current->IsUndefined()) { |
| 14452 | undefs--; |
| 14453 | } else { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14454 | continue; |
| 14455 | } |
| 14456 | // Position i needs to be filled. |
| 14457 | while (undefs > i) { |
| 14458 | current = elements->get(undefs); |
| 14459 | if (current->IsTheHole()) { |
| 14460 | holes--; |
| 14461 | undefs--; |
| 14462 | } else if (current->IsUndefined()) { |
| 14463 | undefs--; |
| 14464 | } else { |
| 14465 | elements->set(i, current, write_barrier); |
| 14466 | break; |
| 14467 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14468 | } |
| 14469 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14470 | result = undefs; |
| 14471 | while (undefs < holes) { |
| 14472 | elements->set_undefined(undefs); |
| 14473 | undefs++; |
| 14474 | } |
| 14475 | while (holes < limit) { |
| 14476 | elements->set_the_hole(holes); |
| 14477 | holes++; |
| 14478 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14479 | } |
| 14480 | |
| 14481 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14482 | return Smi::FromInt(static_cast<int>(result)); |
| 14483 | } |
| 14484 | ASSERT_NE(NULL, result_double); |
| 14485 | result_double->set_value(static_cast<double>(result)); |
| 14486 | return result_double; |
| 14487 | } |
| 14488 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14489 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 14490 | ExternalArrayType JSTypedArray::type() { |
| 14491 | switch (elements()->map()->instance_type()) { |
| 14492 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 14493 | return kExternalByteArray; |
| 14494 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 14495 | return kExternalUnsignedByteArray; |
| 14496 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 14497 | return kExternalShortArray; |
| 14498 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 14499 | return kExternalUnsignedShortArray; |
| 14500 | case EXTERNAL_INT_ARRAY_TYPE: |
| 14501 | return kExternalIntArray; |
| 14502 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 14503 | return kExternalUnsignedIntArray; |
| 14504 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 14505 | return kExternalFloatArray; |
| 14506 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 14507 | return kExternalDoubleArray; |
| 14508 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 14509 | return kExternalPixelArray; |
| 14510 | default: |
| 14511 | return static_cast<ExternalArrayType>(-1); |
| 14512 | } |
| 14513 | } |
| 14514 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14515 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14516 | size_t JSTypedArray::element_size() { |
| 14517 | switch (elements()->map()->instance_type()) { |
| 14518 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 14519 | return 1; |
| 14520 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 14521 | return 1; |
| 14522 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 14523 | return 2; |
| 14524 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 14525 | return 2; |
| 14526 | case EXTERNAL_INT_ARRAY_TYPE: |
| 14527 | return 4; |
| 14528 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 14529 | return 4; |
| 14530 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 14531 | return 4; |
| 14532 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 14533 | return 8; |
| 14534 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 14535 | return 1; |
| 14536 | default: |
| 14537 | UNREACHABLE(); |
| 14538 | return 0; |
| 14539 | } |
| 14540 | } |
| 14541 | |
| 14542 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 14543 | Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14544 | uint8_t clamped_value = 0; |
| 14545 | if (index < static_cast<uint32_t>(length())) { |
| 14546 | if (value->IsSmi()) { |
| 14547 | int int_value = Smi::cast(value)->value(); |
| 14548 | if (int_value < 0) { |
| 14549 | clamped_value = 0; |
| 14550 | } else if (int_value > 255) { |
| 14551 | clamped_value = 255; |
| 14552 | } else { |
| 14553 | clamped_value = static_cast<uint8_t>(int_value); |
| 14554 | } |
| 14555 | } else if (value->IsHeapNumber()) { |
| 14556 | double double_value = HeapNumber::cast(value)->value(); |
| 14557 | if (!(double_value > 0)) { |
| 14558 | // NaN and less than zero clamp to zero. |
| 14559 | clamped_value = 0; |
| 14560 | } else if (double_value > 255) { |
| 14561 | // Greater than 255 clamp to 255. |
| 14562 | clamped_value = 255; |
| 14563 | } else { |
| 14564 | // Other doubles are rounded to the nearest integer. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14565 | clamped_value = static_cast<uint8_t>(lrint(double_value)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14566 | } |
| 14567 | } else { |
| 14568 | // Clamp undefined to zero (default). All other types have been |
| 14569 | // converted to a number type further up in the call chain. |
| 14570 | ASSERT(value->IsUndefined()); |
| 14571 | } |
| 14572 | set(index, clamped_value); |
| 14573 | } |
| 14574 | return Smi::FromInt(clamped_value); |
| 14575 | } |
| 14576 | |
| 14577 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14578 | template<typename ExternalArrayClass, typename ValueType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14579 | static MaybeObject* ExternalArrayIntSetter(Heap* heap, |
| 14580 | ExternalArrayClass* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14581 | uint32_t index, |
| 14582 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14583 | ValueType cast_value = 0; |
| 14584 | if (index < static_cast<uint32_t>(receiver->length())) { |
| 14585 | if (value->IsSmi()) { |
| 14586 | int int_value = Smi::cast(value)->value(); |
| 14587 | cast_value = static_cast<ValueType>(int_value); |
| 14588 | } else if (value->IsHeapNumber()) { |
| 14589 | double double_value = HeapNumber::cast(value)->value(); |
| 14590 | cast_value = static_cast<ValueType>(DoubleToInt32(double_value)); |
| 14591 | } else { |
| 14592 | // Clamp undefined to zero (default). All other types have been |
| 14593 | // converted to a number type further up in the call chain. |
| 14594 | ASSERT(value->IsUndefined()); |
| 14595 | } |
| 14596 | receiver->set(index, cast_value); |
| 14597 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14598 | return heap->NumberFromInt32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14599 | } |
| 14600 | |
| 14601 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14602 | MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14603 | return ExternalArrayIntSetter<ExternalByteArray, int8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14604 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14605 | } |
| 14606 | |
| 14607 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14608 | MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index, |
| 14609 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14610 | return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14611 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14612 | } |
| 14613 | |
| 14614 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14615 | MaybeObject* ExternalShortArray::SetValue(uint32_t index, |
| 14616 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14617 | return ExternalArrayIntSetter<ExternalShortArray, int16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14618 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14619 | } |
| 14620 | |
| 14621 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14622 | MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index, |
| 14623 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14624 | return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14625 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14626 | } |
| 14627 | |
| 14628 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14629 | MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14630 | return ExternalArrayIntSetter<ExternalIntArray, int32_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14631 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14632 | } |
| 14633 | |
| 14634 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14635 | MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14636 | uint32_t cast_value = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14637 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14638 | if (index < static_cast<uint32_t>(length())) { |
| 14639 | if (value->IsSmi()) { |
| 14640 | int int_value = Smi::cast(value)->value(); |
| 14641 | cast_value = static_cast<uint32_t>(int_value); |
| 14642 | } else if (value->IsHeapNumber()) { |
| 14643 | double double_value = HeapNumber::cast(value)->value(); |
| 14644 | cast_value = static_cast<uint32_t>(DoubleToUint32(double_value)); |
| 14645 | } else { |
| 14646 | // Clamp undefined to zero (default). All other types have been |
| 14647 | // converted to a number type further up in the call chain. |
| 14648 | ASSERT(value->IsUndefined()); |
| 14649 | } |
| 14650 | set(index, cast_value); |
| 14651 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14652 | return heap->NumberFromUint32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14653 | } |
| 14654 | |
| 14655 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14656 | MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14657 | float cast_value = static_cast<float>(OS::nan_value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14658 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14659 | if (index < static_cast<uint32_t>(length())) { |
| 14660 | if (value->IsSmi()) { |
| 14661 | int int_value = Smi::cast(value)->value(); |
| 14662 | cast_value = static_cast<float>(int_value); |
| 14663 | } else if (value->IsHeapNumber()) { |
| 14664 | double double_value = HeapNumber::cast(value)->value(); |
| 14665 | cast_value = static_cast<float>(double_value); |
| 14666 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14667 | // Clamp undefined to NaN (default). All other types have been |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14668 | // converted to a number type further up in the call chain. |
| 14669 | ASSERT(value->IsUndefined()); |
| 14670 | } |
| 14671 | set(index, cast_value); |
| 14672 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14673 | return heap->AllocateHeapNumber(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14674 | } |
| 14675 | |
| 14676 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14677 | MaybeObject* ExternalDoubleArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14678 | double double_value = OS::nan_value(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14679 | Heap* heap = GetHeap(); |
| 14680 | if (index < static_cast<uint32_t>(length())) { |
| 14681 | if (value->IsSmi()) { |
| 14682 | int int_value = Smi::cast(value)->value(); |
| 14683 | double_value = static_cast<double>(int_value); |
| 14684 | } else if (value->IsHeapNumber()) { |
| 14685 | double_value = HeapNumber::cast(value)->value(); |
| 14686 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14687 | // Clamp undefined to NaN (default). All other types have been |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14688 | // converted to a number type further up in the call chain. |
| 14689 | ASSERT(value->IsUndefined()); |
| 14690 | } |
| 14691 | set(index, double_value); |
| 14692 | } |
| 14693 | return heap->AllocateHeapNumber(double_value); |
| 14694 | } |
| 14695 | |
| 14696 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14697 | PropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14698 | ASSERT(!HasFastProperties()); |
| 14699 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14700 | return PropertyCell::cast(value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14701 | } |
| 14702 | |
| 14703 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14704 | Handle<PropertyCell> GlobalObject::EnsurePropertyCell( |
| 14705 | Handle<GlobalObject> global, |
| 14706 | Handle<Name> name) { |
| 14707 | ASSERT(!global->HasFastProperties()); |
| 14708 | int entry = global->property_dictionary()->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14709 | if (entry == NameDictionary::kNotFound) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14710 | Isolate* isolate = global->GetIsolate(); |
| 14711 | Handle<PropertyCell> cell = isolate->factory()->NewPropertyCell( |
| 14712 | isolate->factory()->the_hole_value()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14713 | PropertyDetails details(NONE, NORMAL, 0); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14714 | details = details.AsDeleted(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14715 | Handle<NameDictionary> dictionary = NameDictionaryAdd( |
| 14716 | handle(global->property_dictionary()), name, cell, details); |
| 14717 | global->set_properties(*dictionary); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14718 | return cell; |
| 14719 | } else { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14720 | Object* value = global->property_dictionary()->ValueAt(entry); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14721 | ASSERT(value->IsPropertyCell()); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14722 | return handle(PropertyCell::cast(value)); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14723 | } |
| 14724 | } |
| 14725 | |
| 14726 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14727 | MaybeObject* StringTable::LookupString(String* string, Object** s) { |
| 14728 | InternalizedStringKey key(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14729 | return LookupKey(&key, s); |
| 14730 | } |
| 14731 | |
| 14732 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14733 | // 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] | 14734 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 14735 | // string hash calculation loop here for speed. Doesn't work if the two |
| 14736 | // characters form a decimal integer, since such strings have a different hash |
| 14737 | // algorithm. |
| 14738 | class TwoCharHashTableKey : public HashTableKey { |
| 14739 | public: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14740 | TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed) |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14741 | : c1_(c1), c2_(c2) { |
| 14742 | // Char 1. |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 14743 | uint32_t hash = seed; |
| 14744 | hash += c1; |
| 14745 | hash += hash << 10; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14746 | hash ^= hash >> 6; |
| 14747 | // Char 2. |
| 14748 | hash += c2; |
| 14749 | hash += hash << 10; |
| 14750 | hash ^= hash >> 6; |
| 14751 | // GetHash. |
| 14752 | hash += hash << 3; |
| 14753 | hash ^= hash >> 11; |
| 14754 | hash += hash << 15; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 14755 | if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14756 | hash_ = hash; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14757 | #ifdef DEBUG |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14758 | // If this assert fails then we failed to reproduce the two-character |
| 14759 | // version of the string hashing algorithm above. One reason could be |
| 14760 | // that we were passed two digits as characters, since the hash |
| 14761 | // algorithm is different in that case. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14762 | uint16_t chars[2] = {c1, c2}; |
| 14763 | uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed); |
| 14764 | hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; |
| 14765 | 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] | 14766 | #endif |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14767 | } |
| 14768 | |
| 14769 | bool IsMatch(Object* o) { |
| 14770 | if (!o->IsString()) return false; |
| 14771 | String* other = String::cast(o); |
| 14772 | if (other->length() != 2) return false; |
| 14773 | if (other->Get(0) != c1_) return false; |
| 14774 | return other->Get(1) == c2_; |
| 14775 | } |
| 14776 | |
| 14777 | uint32_t Hash() { return hash_; } |
| 14778 | uint32_t HashForObject(Object* key) { |
| 14779 | if (!key->IsString()) return 0; |
| 14780 | return String::cast(key)->Hash(); |
| 14781 | } |
| 14782 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14783 | Object* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14784 | // The TwoCharHashTableKey is only used for looking in the string |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14785 | // table, not for adding to it. |
| 14786 | UNREACHABLE(); |
| 14787 | return NULL; |
| 14788 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 14789 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14790 | private: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14791 | uint16_t c1_; |
| 14792 | uint16_t c2_; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14793 | uint32_t hash_; |
| 14794 | }; |
| 14795 | |
| 14796 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14797 | bool StringTable::LookupStringIfExists(String* string, String** result) { |
| 14798 | InternalizedStringKey key(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 14799 | int entry = FindEntry(&key); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14800 | if (entry == kNotFound) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 14801 | return false; |
| 14802 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14803 | *result = String::cast(KeyAt(entry)); |
| 14804 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 14805 | return true; |
| 14806 | } |
| 14807 | } |
| 14808 | |
| 14809 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14810 | bool StringTable::LookupTwoCharsStringIfExists(uint16_t c1, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14811 | uint16_t c2, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14812 | String** result) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14813 | TwoCharHashTableKey key(c1, c2, GetHeap()->HashSeed()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14814 | int entry = FindEntry(&key); |
| 14815 | if (entry == kNotFound) { |
| 14816 | return false; |
| 14817 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14818 | *result = String::cast(KeyAt(entry)); |
| 14819 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14820 | return true; |
| 14821 | } |
| 14822 | } |
| 14823 | |
| 14824 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14825 | MaybeObject* StringTable::LookupUtf8String(Vector<const char> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14826 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14827 | Utf8StringKey key(str, GetHeap()->HashSeed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14828 | return LookupKey(&key, s); |
| 14829 | } |
| 14830 | |
| 14831 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14832 | MaybeObject* StringTable::LookupOneByteString(Vector<const uint8_t> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14833 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14834 | OneByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14835 | return LookupKey(&key, s); |
| 14836 | } |
| 14837 | |
| 14838 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14839 | MaybeObject* StringTable::LookupSubStringOneByteString( |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 14840 | Handle<SeqOneByteString> str, |
| 14841 | int from, |
| 14842 | int length, |
| 14843 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14844 | SubStringOneByteStringKey key(str, from, length); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 14845 | return LookupKey(&key, s); |
| 14846 | } |
| 14847 | |
| 14848 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14849 | MaybeObject* StringTable::LookupTwoByteString(Vector<const uc16> str, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14850 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14851 | TwoByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14852 | return LookupKey(&key, s); |
| 14853 | } |
| 14854 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14855 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14856 | MaybeObject* StringTable::LookupKey(HashTableKey* key, Object** s) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14857 | int entry = FindEntry(key); |
| 14858 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14859 | // String already in table. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14860 | if (entry != kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14861 | *s = KeyAt(entry); |
| 14862 | return this; |
| 14863 | } |
| 14864 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14865 | // Adding new string. Grow table if needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14866 | Object* obj; |
| 14867 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 14868 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14869 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14870 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14871 | // Create string object. |
| 14872 | Object* string; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14873 | { MaybeObject* maybe_string = key->AsObject(GetHeap()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14874 | if (!maybe_string->ToObject(&string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14876 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14877 | // If the string table grew as part of EnsureCapacity, obj is not |
| 14878 | // the current string table and therefore we cannot use |
| 14879 | // StringTable::cast here. |
| 14880 | StringTable* table = reinterpret_cast<StringTable*>(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14881 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14882 | // 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] | 14883 | entry = table->FindInsertionEntry(key->Hash()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14884 | table->set(EntryToIndex(entry), string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14885 | table->ElementAdded(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14886 | *s = string; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14887 | return table; |
| 14888 | } |
| 14889 | |
| 14890 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14891 | // The key for the script compilation cache is dependent on the mode flags, |
| 14892 | // because they change the global language mode and thus binding behaviour. |
| 14893 | // If flags change at some point, we must ensure that we do not hit the cache |
| 14894 | // for code compiled with different settings. |
| 14895 | static LanguageMode CurrentGlobalLanguageMode() { |
| 14896 | return FLAG_use_strict |
| 14897 | ? (FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE) |
| 14898 | : CLASSIC_MODE; |
| 14899 | } |
| 14900 | |
| 14901 | |
| 14902 | Object* CompilationCacheTable::Lookup(String* src, Context* context) { |
| 14903 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 14904 | StringSharedKey key(src, |
| 14905 | shared, |
| 14906 | CurrentGlobalLanguageMode(), |
| 14907 | RelocInfo::kNoPosition); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14908 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14909 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14910 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14911 | } |
| 14912 | |
| 14913 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 14914 | Object* CompilationCacheTable::LookupEval(String* src, |
| 14915 | Context* context, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 14916 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14917 | int scope_position) { |
| 14918 | StringSharedKey key(src, |
| 14919 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 14920 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14921 | scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14922 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14923 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14924 | return get(EntryToIndex(entry) + 1); |
| 14925 | } |
| 14926 | |
| 14927 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14928 | Object* CompilationCacheTable::LookupRegExp(String* src, |
| 14929 | JSRegExp::Flags flags) { |
| 14930 | RegExpKey key(src, flags); |
| 14931 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14932 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14933 | return get(EntryToIndex(entry) + 1); |
| 14934 | } |
| 14935 | |
| 14936 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14937 | MaybeObject* CompilationCacheTable::Put(String* src, |
| 14938 | Context* context, |
| 14939 | Object* value) { |
| 14940 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 14941 | StringSharedKey key(src, |
| 14942 | shared, |
| 14943 | CurrentGlobalLanguageMode(), |
| 14944 | RelocInfo::kNoPosition); |
| 14945 | CompilationCacheTable* cache; |
| 14946 | MaybeObject* maybe_cache = EnsureCapacity(1, &key); |
| 14947 | if (!maybe_cache->To(&cache)) return maybe_cache; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14948 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14949 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14950 | MaybeObject* maybe_k = key.AsObject(GetHeap()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14951 | if (!maybe_k->To(&k)) return maybe_k; |
| 14952 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14953 | int entry = cache->FindInsertionEntry(key.Hash()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 14954 | cache->set(EntryToIndex(entry), k); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14955 | cache->set(EntryToIndex(entry) + 1, value); |
| 14956 | cache->ElementAdded(); |
| 14957 | return cache; |
| 14958 | } |
| 14959 | |
| 14960 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14961 | MaybeObject* CompilationCacheTable::PutEval(String* src, |
| 14962 | Context* context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14963 | SharedFunctionInfo* value, |
| 14964 | int scope_position) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 14965 | StringSharedKey key(src, |
| 14966 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 14967 | value->language_mode(), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 14968 | scope_position); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14969 | Object* obj; |
| 14970 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 14971 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14972 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14973 | |
| 14974 | CompilationCacheTable* cache = |
| 14975 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14976 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14977 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14978 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14979 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14980 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 14981 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14982 | |
| 14983 | cache->set(EntryToIndex(entry), k); |
| 14984 | cache->set(EntryToIndex(entry) + 1, value); |
| 14985 | cache->ElementAdded(); |
| 14986 | return cache; |
| 14987 | } |
| 14988 | |
| 14989 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14990 | MaybeObject* CompilationCacheTable::PutRegExp(String* src, |
| 14991 | JSRegExp::Flags flags, |
| 14992 | FixedArray* value) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14993 | RegExpKey key(src, flags); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14994 | Object* obj; |
| 14995 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 14996 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14997 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 14998 | |
| 14999 | CompilationCacheTable* cache = |
| 15000 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15001 | int entry = cache->FindInsertionEntry(key.Hash()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 15002 | // We store the value in the key slot, and compare the search key |
| 15003 | // to the stored value with a custon IsMatch function during lookups. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15004 | cache->set(EntryToIndex(entry), value); |
| 15005 | cache->set(EntryToIndex(entry) + 1, value); |
| 15006 | cache->ElementAdded(); |
| 15007 | return cache; |
| 15008 | } |
| 15009 | |
| 15010 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15011 | void CompilationCacheTable::Remove(Object* value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15012 | Object* the_hole_value = GetHeap()->the_hole_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15013 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 15014 | int entry_index = EntryToIndex(entry); |
| 15015 | int value_index = entry_index + 1; |
| 15016 | if (get(value_index) == value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15017 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 15018 | NoWriteBarrierSet(this, value_index, the_hole_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15019 | ElementRemoved(); |
| 15020 | } |
| 15021 | } |
| 15022 | return; |
| 15023 | } |
| 15024 | |
| 15025 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15026 | // StringsKey used for HashTable where key is array of internalized strings. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15027 | class StringsKey : public HashTableKey { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15028 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15029 | explicit StringsKey(FixedArray* strings) : strings_(strings) { } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15030 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15031 | bool IsMatch(Object* strings) { |
| 15032 | FixedArray* o = FixedArray::cast(strings); |
| 15033 | int len = strings_->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15034 | if (o->length() != len) return false; |
| 15035 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15036 | if (o->get(i) != strings_->get(i)) return false; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15037 | } |
| 15038 | return true; |
| 15039 | } |
| 15040 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15041 | uint32_t Hash() { return HashForObject(strings_); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15042 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15043 | uint32_t HashForObject(Object* obj) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15044 | FixedArray* strings = FixedArray::cast(obj); |
| 15045 | int len = strings->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15046 | uint32_t hash = 0; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15047 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15048 | hash ^= String::cast(strings->get(i))->Hash(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15049 | } |
| 15050 | return hash; |
| 15051 | } |
| 15052 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15053 | Object* AsObject(Heap* heap) { return strings_; } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15054 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15055 | private: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15056 | FixedArray* strings_; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15057 | }; |
| 15058 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15059 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15060 | Object* MapCache::Lookup(FixedArray* array) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15061 | StringsKey key(array); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15062 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15063 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15064 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15065 | } |
| 15066 | |
| 15067 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15068 | MaybeObject* MapCache::Put(FixedArray* array, Map* value) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15069 | StringsKey key(array); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15070 | Object* obj; |
| 15071 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 15072 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15073 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15074 | |
| 15075 | MapCache* cache = reinterpret_cast<MapCache*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15076 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15077 | cache->set(EntryToIndex(entry), array); |
| 15078 | cache->set(EntryToIndex(entry) + 1, value); |
| 15079 | cache->ElementAdded(); |
| 15080 | return cache; |
| 15081 | } |
| 15082 | |
| 15083 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15084 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15085 | MaybeObject* Dictionary<Shape, Key>::Allocate(Heap* heap, |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 15086 | int at_least_space_for, |
| 15087 | PretenureFlag pretenure) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15088 | Object* obj; |
| 15089 | { MaybeObject* maybe_obj = |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 15090 | HashTable<Shape, Key>::Allocate( |
| 15091 | heap, |
| 15092 | at_least_space_for, |
| 15093 | HashTable<Shape, Key>::USE_DEFAULT_MINIMUM_CAPACITY, |
| 15094 | pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15095 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15096 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15097 | // Initialize the next enumeration index. |
| 15098 | Dictionary<Shape, Key>::cast(obj)-> |
| 15099 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15100 | return obj; |
| 15101 | } |
| 15102 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15103 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15104 | void NameDictionary::DoGenerateNewEnumerationIndices( |
| 15105 | Handle<NameDictionary> dictionary) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15106 | CALL_HEAP_FUNCTION_VOID(dictionary->GetIsolate(), |
| 15107 | dictionary->GenerateNewEnumerationIndices()); |
| 15108 | } |
| 15109 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15110 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15111 | MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15112 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15113 | int length = HashTable<Shape, Key>::NumberOfElements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15114 | |
| 15115 | // Allocate and initialize iteration order array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15116 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15117 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15118 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15119 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15120 | FixedArray* iteration_order = FixedArray::cast(obj); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15121 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15122 | iteration_order->set(i, Smi::FromInt(i)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15123 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15124 | |
| 15125 | // Allocate array with enumeration order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15126 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15127 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15128 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15129 | FixedArray* enumeration_order = FixedArray::cast(obj); |
| 15130 | |
| 15131 | // Fill the enumeration order array with property details. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15132 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15133 | int pos = 0; |
| 15134 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15135 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 15136 | int index = DetailsAt(i).dictionary_index(); |
| 15137 | enumeration_order->set(pos++, Smi::FromInt(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15138 | } |
| 15139 | } |
| 15140 | |
| 15141 | // Sort the arrays wrt. enumeration order. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 15142 | iteration_order->SortPairs(enumeration_order, enumeration_order->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15143 | |
| 15144 | // Overwrite the enumeration_order with the enumeration indices. |
| 15145 | for (int i = 0; i < length; i++) { |
| 15146 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 15147 | int enum_index = PropertyDetails::kInitialIndex + i; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15148 | enumeration_order->set(index, Smi::FromInt(enum_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15149 | } |
| 15150 | |
| 15151 | // Update the dictionary with new indices. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15152 | capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15153 | pos = 0; |
| 15154 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15155 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15156 | int enum_index = Smi::cast(enumeration_order->get(pos++))->value(); |
| 15157 | PropertyDetails details = DetailsAt(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15158 | PropertyDetails new_details = PropertyDetails( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15159 | details.attributes(), details.type(), enum_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15160 | DetailsAtPut(i, new_details); |
| 15161 | } |
| 15162 | } |
| 15163 | |
| 15164 | // Set the next enumeration index. |
| 15165 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
| 15166 | return this; |
| 15167 | } |
| 15168 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15169 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15170 | MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 15171 | // Check whether there are enough enumeration indices to add n elements. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15172 | if (Shape::kIsEnumerable && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15173 | !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) { |
| 15174 | // If not, we generate new indices for the properties. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15175 | Object* result; |
| 15176 | { MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 15177 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 15178 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15179 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15180 | return HashTable<Shape, Key>::EnsureCapacity(n, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15181 | } |
| 15182 | |
| 15183 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15184 | template<typename Shape, typename Key> |
| 15185 | Object* Dictionary<Shape, Key>::DeleteProperty(int entry, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 15186 | JSReceiver::DeleteMode mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15187 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15188 | PropertyDetails details = DetailsAt(entry); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 15189 | // Ignore attributes if forcing a deletion. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 15190 | if (details.IsDontDelete() && mode != JSReceiver::FORCE_DELETION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15191 | return heap->false_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 15192 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15193 | SetEntry(entry, heap->the_hole_value(), heap->the_hole_value()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15194 | HashTable<Shape, Key>::ElementRemoved(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15195 | return heap->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15196 | } |
| 15197 | |
| 15198 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15199 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 15200 | MaybeObject* Dictionary<Shape, Key>::Shrink(Key key) { |
| 15201 | return HashTable<Shape, Key>::Shrink(key); |
| 15202 | } |
| 15203 | |
| 15204 | |
| 15205 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15206 | MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15207 | int entry = this->FindEntry(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15208 | |
| 15209 | // If the entry is present set the value; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15210 | if (entry != Dictionary<Shape, Key>::kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15211 | ValueAtPut(entry, value); |
| 15212 | return this; |
| 15213 | } |
| 15214 | |
| 15215 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15216 | Object* obj; |
| 15217 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15218 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15219 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15220 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15221 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15222 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15223 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 15224 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15225 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15226 | |
| 15227 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 15228 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15229 | } |
| 15230 | |
| 15231 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15232 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15233 | MaybeObject* Dictionary<Shape, Key>::Add(Key key, |
| 15234 | Object* value, |
| 15235 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15236 | // Valdate key is absent. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15237 | SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15238 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15239 | Object* obj; |
| 15240 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15241 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15242 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15243 | |
| 15244 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 15245 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15246 | } |
| 15247 | |
| 15248 | |
| 15249 | // Add a key, value pair to the dictionary. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15250 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15251 | MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key, |
| 15252 | Object* value, |
| 15253 | PropertyDetails details, |
| 15254 | uint32_t hash) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15255 | // Compute the key object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15256 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15257 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15258 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 15259 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15260 | |
| 15261 | uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15262 | // Insert element at empty or deleted entry |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15263 | if (!details.IsDeleted() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 15264 | details.dictionary_index() == 0 && |
| 15265 | Shape::kIsEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15266 | // Assign an enumeration index to the property and update |
| 15267 | // SetNextEnumerationIndex. |
| 15268 | int index = NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15269 | details = PropertyDetails(details.attributes(), details.type(), index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15270 | SetNextEnumerationIndex(index + 1); |
| 15271 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15272 | SetEntry(entry, k, value, details); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15273 | ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber() || |
| 15274 | Dictionary<Shape, Key>::KeyAt(entry)->IsName())); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15275 | HashTable<Shape, Key>::ElementAdded(); |
| 15276 | return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15277 | } |
| 15278 | |
| 15279 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15280 | void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15281 | // If the dictionary requires slow elements an element has already |
| 15282 | // been added at a high index. |
| 15283 | if (requires_slow_elements()) return; |
| 15284 | // Check if this index is high enough that we should require slow |
| 15285 | // elements. |
| 15286 | if (key > kRequiresSlowElementsLimit) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 15287 | set_requires_slow_elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15288 | return; |
| 15289 | } |
| 15290 | // Update max key value. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15291 | Object* max_index_object = get(kMaxNumberKeyIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15292 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15293 | FixedArray::set(kMaxNumberKeyIndex, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15294 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15295 | } |
| 15296 | } |
| 15297 | |
| 15298 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15299 | MaybeObject* SeededNumberDictionary::AddNumberEntry(uint32_t key, |
| 15300 | Object* value, |
| 15301 | PropertyDetails details) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15302 | UpdateMaxNumberKey(key); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15303 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15304 | return Add(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15305 | } |
| 15306 | |
| 15307 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15308 | MaybeObject* UnseededNumberDictionary::AddNumberEntry(uint32_t key, |
| 15309 | Object* value) { |
| 15310 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15311 | return Add(key, value, PropertyDetails(NONE, NORMAL, 0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15312 | } |
| 15313 | |
| 15314 | |
| 15315 | MaybeObject* SeededNumberDictionary::AtNumberPut(uint32_t key, Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15316 | UpdateMaxNumberKey(key); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15317 | return AtPut(key, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15318 | } |
| 15319 | |
| 15320 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15321 | MaybeObject* UnseededNumberDictionary::AtNumberPut(uint32_t key, |
| 15322 | Object* value) { |
| 15323 | return AtPut(key, value); |
| 15324 | } |
| 15325 | |
| 15326 | |
| 15327 | Handle<SeededNumberDictionary> SeededNumberDictionary::Set( |
| 15328 | Handle<SeededNumberDictionary> dictionary, |
| 15329 | uint32_t index, |
| 15330 | Handle<Object> value, |
| 15331 | PropertyDetails details) { |
| 15332 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 15333 | dictionary->Set(index, *value, details), |
| 15334 | SeededNumberDictionary); |
| 15335 | } |
| 15336 | |
| 15337 | |
| 15338 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set( |
| 15339 | Handle<UnseededNumberDictionary> dictionary, |
| 15340 | uint32_t index, |
| 15341 | Handle<Object> value) { |
| 15342 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 15343 | dictionary->Set(index, *value), |
| 15344 | UnseededNumberDictionary); |
| 15345 | } |
| 15346 | |
| 15347 | |
| 15348 | MaybeObject* SeededNumberDictionary::Set(uint32_t key, |
| 15349 | Object* value, |
| 15350 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15351 | int entry = FindEntry(key); |
| 15352 | if (entry == kNotFound) return AddNumberEntry(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15353 | // Preserve enumeration index. |
| 15354 | details = PropertyDetails(details.attributes(), |
| 15355 | details.type(), |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15356 | DetailsAt(entry).dictionary_index()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15357 | MaybeObject* maybe_object_key = |
| 15358 | SeededNumberDictionaryShape::AsObject(GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15359 | Object* object_key; |
| 15360 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 15361 | SetEntry(entry, object_key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15362 | return this; |
| 15363 | } |
| 15364 | |
| 15365 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15366 | MaybeObject* UnseededNumberDictionary::Set(uint32_t key, |
| 15367 | Object* value) { |
| 15368 | int entry = FindEntry(key); |
| 15369 | if (entry == kNotFound) return AddNumberEntry(key, value); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15370 | MaybeObject* maybe_object_key = |
| 15371 | UnseededNumberDictionaryShape::AsObject(GetHeap(), key); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15372 | Object* object_key; |
| 15373 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
| 15374 | SetEntry(entry, object_key, value); |
| 15375 | return this; |
| 15376 | } |
| 15377 | |
| 15378 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 15379 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15380 | template<typename Shape, typename Key> |
| 15381 | int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes( |
| 15382 | PropertyAttributes filter) { |
| 15383 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15384 | int result = 0; |
| 15385 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15386 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15387 | if (HashTable<Shape, Key>::IsKey(k) && |
| 15388 | ((filter & SYMBOLIC) == 0 || !k->IsSymbol())) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15389 | PropertyDetails details = DetailsAt(i); |
| 15390 | if (details.IsDeleted()) continue; |
| 15391 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15392 | if ((attr & filter) == 0) result++; |
| 15393 | } |
| 15394 | } |
| 15395 | return result; |
| 15396 | } |
| 15397 | |
| 15398 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15399 | template<typename Shape, typename Key> |
| 15400 | int Dictionary<Shape, Key>::NumberOfEnumElements() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15401 | return NumberOfElementsFilterAttributes( |
| 15402 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 15403 | } |
| 15404 | |
| 15405 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15406 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15407 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 15408 | FixedArray* storage, |
| 15409 | PropertyAttributes filter, |
| 15410 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15411 | ASSERT(storage->length() >= NumberOfEnumElements()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15412 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15413 | int index = 0; |
| 15414 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15415 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15416 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15417 | PropertyDetails details = DetailsAt(i); |
| 15418 | if (details.IsDeleted()) continue; |
| 15419 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15420 | if ((attr & filter) == 0) storage->set(index++, k); |
| 15421 | } |
| 15422 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15423 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 15424 | storage->SortPairs(storage, index); |
| 15425 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15426 | ASSERT(storage->length() >= index); |
| 15427 | } |
| 15428 | |
| 15429 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15430 | FixedArray* NameDictionary::CopyEnumKeysTo(FixedArray* storage) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15431 | int length = storage->length(); |
| 15432 | ASSERT(length >= NumberOfEnumElements()); |
| 15433 | Heap* heap = GetHeap(); |
| 15434 | Object* undefined_value = heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15435 | int capacity = Capacity(); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15436 | int properties = 0; |
| 15437 | |
| 15438 | // Fill in the enumeration array by assigning enumerable keys at their |
| 15439 | // enumeration index. This will leave holes in the array if there are keys |
| 15440 | // that are deleted or not enumerable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15441 | for (int i = 0; i < capacity; i++) { |
| 15442 | Object* k = KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15443 | if (IsKey(k) && !k->IsSymbol()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15444 | PropertyDetails details = DetailsAt(i); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15445 | if (details.IsDeleted() || details.IsDontEnum()) continue; |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15446 | properties++; |
| 15447 | storage->set(details.dictionary_index() - 1, k); |
| 15448 | if (properties == length) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15449 | } |
| 15450 | } |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15451 | |
| 15452 | // There are holes in the enumeration array if less properties were assigned |
| 15453 | // than the length of the array. If so, crunch all the existing properties |
| 15454 | // together by shifting them to the left (maintaining the enumeration order), |
| 15455 | // and trimming of the right side of the array. |
| 15456 | if (properties < length) { |
| 15457 | if (properties == 0) return heap->empty_fixed_array(); |
| 15458 | properties = 0; |
| 15459 | for (int i = 0; i < length; ++i) { |
| 15460 | Object* value = storage->get(i); |
| 15461 | if (value != undefined_value) { |
| 15462 | storage->set(properties, value); |
| 15463 | ++properties; |
| 15464 | } |
| 15465 | } |
| 15466 | RightTrimFixedArray<FROM_MUTATOR>(heap, storage, length - properties); |
| 15467 | } |
| 15468 | return storage; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15469 | } |
| 15470 | |
| 15471 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15472 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15473 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 15474 | FixedArray* storage, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 15475 | int index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 15476 | PropertyAttributes filter, |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15477 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15478 | ASSERT(storage->length() >= NumberOfElementsFilterAttributes( |
| 15479 | static_cast<PropertyAttributes>(NONE))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15480 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15481 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15482 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15483 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15484 | PropertyDetails details = DetailsAt(i); |
| 15485 | if (details.IsDeleted()) continue; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 15486 | PropertyAttributes attr = details.attributes(); |
| 15487 | if ((attr & filter) == 0) storage->set(index++, k); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15488 | } |
| 15489 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15490 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 15491 | storage->SortPairs(storage, index); |
| 15492 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15493 | ASSERT(storage->length() >= index); |
| 15494 | } |
| 15495 | |
| 15496 | |
| 15497 | // Backwards lookup (slow). |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15498 | template<typename Shape, typename Key> |
| 15499 | Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) { |
| 15500 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15501 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15502 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15503 | if (Dictionary<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15504 | Object* e = ValueAt(i); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 15505 | if (e->IsPropertyCell()) { |
| 15506 | e = PropertyCell::cast(e)->value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15507 | } |
| 15508 | if (e == value) return k; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15509 | } |
| 15510 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15511 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15512 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15513 | } |
| 15514 | |
| 15515 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15516 | MaybeObject* NameDictionary::TransformPropertiesToFastFor( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15517 | JSObject* obj, int unused_property_fields) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15518 | // Make sure we preserve dictionary representation if there are too many |
| 15519 | // descriptors. |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15520 | int number_of_elements = NumberOfElements(); |
| 15521 | if (number_of_elements > DescriptorArray::kMaxNumberOfDescriptors) return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15522 | |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15523 | if (number_of_elements != NextEnumerationIndex()) { |
| 15524 | MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 15525 | if (maybe_result->IsFailure()) return maybe_result; |
| 15526 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15527 | |
| 15528 | int instance_descriptor_length = 0; |
| 15529 | int number_of_fields = 0; |
| 15530 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15531 | Heap* heap = GetHeap(); |
| 15532 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15533 | // Compute the length of the instance descriptor. |
| 15534 | int capacity = Capacity(); |
| 15535 | for (int i = 0; i < capacity; i++) { |
| 15536 | Object* k = KeyAt(i); |
| 15537 | if (IsKey(k)) { |
| 15538 | Object* value = ValueAt(i); |
| 15539 | PropertyType type = DetailsAt(i).type(); |
| 15540 | ASSERT(type != FIELD); |
| 15541 | instance_descriptor_length++; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 15542 | if (type == NORMAL && !value->IsJSFunction()) { |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 15543 | number_of_fields += 1; |
| 15544 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15545 | } |
| 15546 | } |
| 15547 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15548 | int inobject_props = obj->map()->inobject_properties(); |
| 15549 | |
| 15550 | // Allocate new map. |
| 15551 | Map* new_map; |
| 15552 | MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors(); |
| 15553 | 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] | 15554 | new_map->set_dictionary_map(false); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15555 | |
| 15556 | if (instance_descriptor_length == 0) { |
| 15557 | ASSERT_LE(unused_property_fields, inobject_props); |
| 15558 | // Transform the object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 15559 | new_map->set_unused_property_fields(inobject_props); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15560 | obj->set_map(new_map); |
| 15561 | obj->set_properties(heap->empty_fixed_array()); |
| 15562 | // Check that it really works. |
| 15563 | ASSERT(obj->HasFastProperties()); |
| 15564 | return obj; |
| 15565 | } |
| 15566 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15567 | // Allocate the instance descriptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15568 | DescriptorArray* descriptors; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15569 | MaybeObject* maybe_descriptors = |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15570 | DescriptorArray::Allocate(GetIsolate(), instance_descriptor_length); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15571 | if (!maybe_descriptors->To(&descriptors)) { |
| 15572 | return maybe_descriptors; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15573 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15574 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 15575 | DescriptorArray::WhitenessWitness witness(descriptors); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15576 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15577 | int number_of_allocated_fields = |
| 15578 | number_of_fields + unused_property_fields - inobject_props; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 15579 | if (number_of_allocated_fields < 0) { |
| 15580 | // There is enough inobject space for all fields (including unused). |
| 15581 | number_of_allocated_fields = 0; |
| 15582 | unused_property_fields = inobject_props - number_of_fields; |
| 15583 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15584 | |
| 15585 | // Allocate the fixed array for the fields. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15586 | FixedArray* fields; |
| 15587 | MaybeObject* maybe_fields = |
| 15588 | heap->AllocateFixedArray(number_of_allocated_fields); |
| 15589 | if (!maybe_fields->To(&fields)) return maybe_fields; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15590 | |
| 15591 | // Fill in the instance descriptor and the fields. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15592 | int current_offset = 0; |
| 15593 | for (int i = 0; i < capacity; i++) { |
| 15594 | Object* k = KeyAt(i); |
| 15595 | if (IsKey(k)) { |
| 15596 | Object* value = ValueAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15597 | Name* key; |
| 15598 | if (k->IsSymbol()) { |
| 15599 | key = Symbol::cast(k); |
| 15600 | } else { |
| 15601 | // Ensure the key is a unique name before writing into the |
| 15602 | // instance descriptor. |
| 15603 | MaybeObject* maybe_key = heap->InternalizeString(String::cast(k)); |
| 15604 | if (!maybe_key->To(&key)) return maybe_key; |
| 15605 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15606 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15607 | PropertyDetails details = DetailsAt(i); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15608 | int enumeration_index = details.dictionary_index(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15609 | PropertyType type = details.type(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15610 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 15611 | if (value->IsJSFunction()) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 15612 | ConstantDescriptor d(key, value, details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15613 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15614 | } else if (type == NORMAL) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15615 | if (current_offset < inobject_props) { |
| 15616 | obj->InObjectPropertyAtPut(current_offset, |
| 15617 | value, |
| 15618 | UPDATE_WRITE_BARRIER); |
| 15619 | } else { |
| 15620 | int offset = current_offset - inobject_props; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15621 | fields->set(offset, value); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15622 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15623 | FieldDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15624 | current_offset++, |
| 15625 | details.attributes(), |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15626 | // TODO(verwaest): value->OptimalRepresentation(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15627 | Representation::Tagged()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15628 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15629 | } else if (type == CALLBACKS) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15630 | CallbacksDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15631 | value, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15632 | details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15633 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15634 | } else { |
| 15635 | UNREACHABLE(); |
| 15636 | } |
| 15637 | } |
| 15638 | } |
| 15639 | ASSERT(current_offset == number_of_fields); |
| 15640 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15641 | descriptors->Sort(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15642 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 15643 | new_map->InitializeDescriptors(descriptors); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 15644 | new_map->set_unused_property_fields(unused_property_fields); |
| 15645 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15646 | // Transform the object. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 15647 | obj->set_map(new_map); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 15648 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15649 | obj->set_properties(fields); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15650 | ASSERT(obj->IsJSObject()); |
| 15651 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15652 | // Check that it really works. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15653 | ASSERT(obj->HasFastProperties()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15654 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15655 | return obj; |
| 15656 | } |
| 15657 | |
| 15658 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15659 | bool ObjectHashSet::Contains(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15660 | ASSERT(IsKey(key)); |
| 15661 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15662 | // 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] | 15663 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 15664 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return false; |
| 15665 | } |
| 15666 | return (FindEntry(key) != kNotFound); |
| 15667 | } |
| 15668 | |
| 15669 | |
| 15670 | MaybeObject* ObjectHashSet::Add(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15671 | ASSERT(IsKey(key)); |
| 15672 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15673 | // Make sure the key object has an identity hash code. |
| 15674 | int hash; |
| 15675 | { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION); |
| 15676 | if (maybe_hash->IsFailure()) return maybe_hash; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 15677 | ASSERT(key->GetHash(OMIT_CREATION) == maybe_hash); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15678 | hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value(); |
| 15679 | } |
| 15680 | int entry = FindEntry(key); |
| 15681 | |
| 15682 | // Check whether key is already present. |
| 15683 | if (entry != kNotFound) return this; |
| 15684 | |
| 15685 | // Check whether the hash set should be extended and add entry. |
| 15686 | Object* obj; |
| 15687 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15688 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15689 | } |
| 15690 | ObjectHashSet* table = ObjectHashSet::cast(obj); |
| 15691 | entry = table->FindInsertionEntry(hash); |
| 15692 | table->set(EntryToIndex(entry), key); |
| 15693 | table->ElementAdded(); |
| 15694 | return table; |
| 15695 | } |
| 15696 | |
| 15697 | |
| 15698 | MaybeObject* ObjectHashSet::Remove(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15699 | ASSERT(IsKey(key)); |
| 15700 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15701 | // If the object does not have an identity hash, it was never used as a key. |
| 15702 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 15703 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return this; |
| 15704 | } |
| 15705 | int entry = FindEntry(key); |
| 15706 | |
| 15707 | // Check whether key is actually present. |
| 15708 | if (entry == kNotFound) return this; |
| 15709 | |
| 15710 | // Remove entry and try to shrink this hash set. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15711 | set_the_hole(EntryToIndex(entry)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15712 | ElementRemoved(); |
| 15713 | return Shrink(key); |
| 15714 | } |
| 15715 | |
| 15716 | |
| 15717 | Object* ObjectHashTable::Lookup(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15718 | ASSERT(IsKey(key)); |
| 15719 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15720 | // If the object does not have an identity hash, it was never used as a key. |
| 15721 | { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION); |
| 15722 | if (maybe_hash->ToObjectUnchecked()->IsUndefined()) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 15723 | return GetHeap()->the_hole_value(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15724 | } |
| 15725 | } |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15726 | int entry = FindEntry(key); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 15727 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15728 | return get(EntryToIndex(entry) + 1); |
| 15729 | } |
| 15730 | |
| 15731 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15732 | MaybeObject* ObjectHashTable::Put(Object* key, Object* value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15733 | ASSERT(IsKey(key)); |
| 15734 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15735 | // Make sure the key object has an identity hash code. |
| 15736 | int hash; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15737 | { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15738 | if (maybe_hash->IsFailure()) return maybe_hash; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 15739 | ASSERT(key->GetHash(OMIT_CREATION) == maybe_hash); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15740 | hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value(); |
| 15741 | } |
| 15742 | int entry = FindEntry(key); |
| 15743 | |
| 15744 | // Check whether to perform removal operation. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 15745 | if (value->IsTheHole()) { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15746 | if (entry == kNotFound) return this; |
| 15747 | RemoveEntry(entry); |
| 15748 | return Shrink(key); |
| 15749 | } |
| 15750 | |
| 15751 | // Key is already in table, just overwrite value. |
| 15752 | if (entry != kNotFound) { |
| 15753 | set(EntryToIndex(entry) + 1, value); |
| 15754 | return this; |
| 15755 | } |
| 15756 | |
| 15757 | // Check whether the hash table should be extended. |
| 15758 | Object* obj; |
| 15759 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15760 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15761 | } |
| 15762 | ObjectHashTable* table = ObjectHashTable::cast(obj); |
| 15763 | table->AddEntry(table->FindInsertionEntry(hash), key, value); |
| 15764 | return table; |
| 15765 | } |
| 15766 | |
| 15767 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15768 | void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15769 | set(EntryToIndex(entry), key); |
| 15770 | set(EntryToIndex(entry) + 1, value); |
| 15771 | ElementAdded(); |
| 15772 | } |
| 15773 | |
| 15774 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15775 | void ObjectHashTable::RemoveEntry(int entry) { |
| 15776 | set_the_hole(EntryToIndex(entry)); |
| 15777 | set_the_hole(EntryToIndex(entry) + 1); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15778 | ElementRemoved(); |
| 15779 | } |
| 15780 | |
| 15781 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 15782 | Object* WeakHashTable::Lookup(Object* key) { |
| 15783 | ASSERT(IsKey(key)); |
| 15784 | int entry = FindEntry(key); |
| 15785 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
| 15786 | return get(EntryToValueIndex(entry)); |
| 15787 | } |
| 15788 | |
| 15789 | |
| 15790 | MaybeObject* WeakHashTable::Put(Object* key, Object* value) { |
| 15791 | ASSERT(IsKey(key)); |
| 15792 | int entry = FindEntry(key); |
| 15793 | // Key is already in table, just overwrite value. |
| 15794 | if (entry != kNotFound) { |
| 15795 | set(EntryToValueIndex(entry), value); |
| 15796 | return this; |
| 15797 | } |
| 15798 | |
| 15799 | // Check whether the hash table should be extended. |
| 15800 | Object* obj; |
| 15801 | { MaybeObject* maybe_obj = EnsureCapacity(1, key, TENURED); |
| 15802 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15803 | } |
| 15804 | WeakHashTable* table = WeakHashTable::cast(obj); |
| 15805 | table->AddEntry(table->FindInsertionEntry(Hash(key)), key, value); |
| 15806 | return table; |
| 15807 | } |
| 15808 | |
| 15809 | |
| 15810 | void WeakHashTable::AddEntry(int entry, Object* key, Object* value) { |
| 15811 | set(EntryToIndex(entry), key); |
| 15812 | set(EntryToValueIndex(entry), value); |
| 15813 | ElementAdded(); |
| 15814 | } |
| 15815 | |
| 15816 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15817 | DeclaredAccessorDescriptorIterator::DeclaredAccessorDescriptorIterator( |
| 15818 | DeclaredAccessorDescriptor* descriptor) |
| 15819 | : array_(descriptor->serialized_data()->GetDataStartAddress()), |
| 15820 | length_(descriptor->serialized_data()->length()), |
| 15821 | offset_(0) { |
| 15822 | } |
| 15823 | |
| 15824 | |
| 15825 | const DeclaredAccessorDescriptorData* |
| 15826 | DeclaredAccessorDescriptorIterator::Next() { |
| 15827 | ASSERT(offset_ < length_); |
| 15828 | uint8_t* ptr = &array_[offset_]; |
| 15829 | ASSERT(reinterpret_cast<uintptr_t>(ptr) % sizeof(uintptr_t) == 0); |
| 15830 | const DeclaredAccessorDescriptorData* data = |
| 15831 | reinterpret_cast<const DeclaredAccessorDescriptorData*>(ptr); |
| 15832 | offset_ += sizeof(*data); |
| 15833 | ASSERT(offset_ <= length_); |
| 15834 | return data; |
| 15835 | } |
| 15836 | |
| 15837 | |
| 15838 | Handle<DeclaredAccessorDescriptor> DeclaredAccessorDescriptor::Create( |
| 15839 | Isolate* isolate, |
| 15840 | const DeclaredAccessorDescriptorData& descriptor, |
| 15841 | Handle<DeclaredAccessorDescriptor> previous) { |
| 15842 | int previous_length = |
| 15843 | previous.is_null() ? 0 : previous->serialized_data()->length(); |
| 15844 | int length = sizeof(descriptor) + previous_length; |
| 15845 | Handle<ByteArray> serialized_descriptor = |
| 15846 | isolate->factory()->NewByteArray(length); |
| 15847 | Handle<DeclaredAccessorDescriptor> value = |
| 15848 | isolate->factory()->NewDeclaredAccessorDescriptor(); |
| 15849 | value->set_serialized_data(*serialized_descriptor); |
| 15850 | // Copy in the data. |
| 15851 | { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 15852 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15853 | uint8_t* array = serialized_descriptor->GetDataStartAddress(); |
| 15854 | if (previous_length != 0) { |
| 15855 | uint8_t* previous_array = |
| 15856 | previous->serialized_data()->GetDataStartAddress(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 15857 | OS::MemCopy(array, previous_array, previous_length); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15858 | array += previous_length; |
| 15859 | } |
| 15860 | ASSERT(reinterpret_cast<uintptr_t>(array) % sizeof(uintptr_t) == 0); |
| 15861 | DeclaredAccessorDescriptorData* data = |
| 15862 | reinterpret_cast<DeclaredAccessorDescriptorData*>(array); |
| 15863 | *data = descriptor; |
| 15864 | } |
| 15865 | return value; |
| 15866 | } |
| 15867 | |
| 15868 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 15869 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15870 | // Check if there is a break point at this code position. |
| 15871 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 15872 | // Get the break point info object for this code position. |
| 15873 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 15874 | |
| 15875 | // If there is no break point info object or no break points in the break |
| 15876 | // point info object there is no break point at this code position. |
| 15877 | if (break_point_info->IsUndefined()) return false; |
| 15878 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 15879 | } |
| 15880 | |
| 15881 | |
| 15882 | // Get the break point info object for this code position. |
| 15883 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 15884 | // Find the index of the break point info object for this code position. |
| 15885 | int index = GetBreakPointInfoIndex(code_position); |
| 15886 | |
| 15887 | // Return the break point info object if any. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15888 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15889 | return BreakPointInfo::cast(break_points()->get(index)); |
| 15890 | } |
| 15891 | |
| 15892 | |
| 15893 | // Clear a break point at the specified code position. |
| 15894 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 15895 | int code_position, |
| 15896 | Handle<Object> break_point_object) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 15897 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15898 | debug_info->GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15899 | if (break_point_info->IsUndefined()) return; |
| 15900 | BreakPointInfo::ClearBreakPoint( |
| 15901 | Handle<BreakPointInfo>::cast(break_point_info), |
| 15902 | break_point_object); |
| 15903 | } |
| 15904 | |
| 15905 | |
| 15906 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 15907 | int code_position, |
| 15908 | int source_position, |
| 15909 | int statement_position, |
| 15910 | Handle<Object> break_point_object) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15911 | Isolate* isolate = debug_info->GetIsolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 15912 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 15913 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15914 | if (!break_point_info->IsUndefined()) { |
| 15915 | BreakPointInfo::SetBreakPoint( |
| 15916 | Handle<BreakPointInfo>::cast(break_point_info), |
| 15917 | break_point_object); |
| 15918 | return; |
| 15919 | } |
| 15920 | |
| 15921 | // Adding a new break point for a code position which did not have any |
| 15922 | // break points before. Try to find a free slot. |
| 15923 | int index = kNoBreakPointInfo; |
| 15924 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 15925 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 15926 | index = i; |
| 15927 | break; |
| 15928 | } |
| 15929 | } |
| 15930 | if (index == kNoBreakPointInfo) { |
| 15931 | // No free slot - extend break point info array. |
| 15932 | Handle<FixedArray> old_break_points = |
| 15933 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15934 | Handle<FixedArray> new_break_points = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15935 | isolate->factory()->NewFixedArray( |
| 15936 | old_break_points->length() + |
| 15937 | Debug::kEstimatedNofBreakPointsInFunction); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 15938 | |
| 15939 | debug_info->set_break_points(*new_break_points); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15940 | for (int i = 0; i < old_break_points->length(); i++) { |
| 15941 | new_break_points->set(i, old_break_points->get(i)); |
| 15942 | } |
| 15943 | index = old_break_points->length(); |
| 15944 | } |
| 15945 | ASSERT(index != kNoBreakPointInfo); |
| 15946 | |
| 15947 | // Allocate new BreakPointInfo object and set the break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15948 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 15949 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15950 | new_break_point_info->set_code_position(Smi::FromInt(code_position)); |
| 15951 | new_break_point_info->set_source_position(Smi::FromInt(source_position)); |
| 15952 | new_break_point_info-> |
| 15953 | set_statement_position(Smi::FromInt(statement_position)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15954 | new_break_point_info->set_break_point_objects( |
| 15955 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15956 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 15957 | debug_info->break_points()->set(index, *new_break_point_info); |
| 15958 | } |
| 15959 | |
| 15960 | |
| 15961 | // Get the break point objects for a code position. |
| 15962 | Object* DebugInfo::GetBreakPointObjects(int code_position) { |
| 15963 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 15964 | if (break_point_info->IsUndefined()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15965 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15966 | } |
| 15967 | return BreakPointInfo::cast(break_point_info)->break_point_objects(); |
| 15968 | } |
| 15969 | |
| 15970 | |
| 15971 | // Get the total number of break points. |
| 15972 | int DebugInfo::GetBreakPointCount() { |
| 15973 | if (break_points()->IsUndefined()) return 0; |
| 15974 | int count = 0; |
| 15975 | for (int i = 0; i < break_points()->length(); i++) { |
| 15976 | if (!break_points()->get(i)->IsUndefined()) { |
| 15977 | BreakPointInfo* break_point_info = |
| 15978 | BreakPointInfo::cast(break_points()->get(i)); |
| 15979 | count += break_point_info->GetBreakPointCount(); |
| 15980 | } |
| 15981 | } |
| 15982 | return count; |
| 15983 | } |
| 15984 | |
| 15985 | |
| 15986 | Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 15987 | Handle<Object> break_point_object) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15988 | Heap* heap = debug_info->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15989 | if (debug_info->break_points()->IsUndefined()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15990 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 15991 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 15992 | Handle<BreakPointInfo> break_point_info = |
| 15993 | Handle<BreakPointInfo>(BreakPointInfo::cast( |
| 15994 | debug_info->break_points()->get(i))); |
| 15995 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 15996 | break_point_object)) { |
| 15997 | return *break_point_info; |
| 15998 | } |
| 15999 | } |
| 16000 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16001 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16002 | } |
| 16003 | |
| 16004 | |
| 16005 | // Find the index of the break point info object for the specified code |
| 16006 | // position. |
| 16007 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 16008 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 16009 | for (int i = 0; i < break_points()->length(); i++) { |
| 16010 | if (!break_points()->get(i)->IsUndefined()) { |
| 16011 | BreakPointInfo* break_point_info = |
| 16012 | BreakPointInfo::cast(break_points()->get(i)); |
| 16013 | if (break_point_info->code_position()->value() == code_position) { |
| 16014 | return i; |
| 16015 | } |
| 16016 | } |
| 16017 | } |
| 16018 | return kNoBreakPointInfo; |
| 16019 | } |
| 16020 | |
| 16021 | |
| 16022 | // Remove the specified break point object. |
| 16023 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 16024 | Handle<Object> break_point_object) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 16025 | Isolate* isolate = break_point_info->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16026 | // If there are no break points just ignore. |
| 16027 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 16028 | // If there is a single break point clear it if it is the same. |
| 16029 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 16030 | if (break_point_info->break_point_objects() == *break_point_object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16031 | break_point_info->set_break_point_objects( |
| 16032 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16033 | } |
| 16034 | return; |
| 16035 | } |
| 16036 | // If there are multiple break points shrink the array |
| 16037 | ASSERT(break_point_info->break_point_objects()->IsFixedArray()); |
| 16038 | Handle<FixedArray> old_array = |
| 16039 | Handle<FixedArray>( |
| 16040 | FixedArray::cast(break_point_info->break_point_objects())); |
| 16041 | Handle<FixedArray> new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16042 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16043 | int found_count = 0; |
| 16044 | for (int i = 0; i < old_array->length(); i++) { |
| 16045 | if (old_array->get(i) == *break_point_object) { |
| 16046 | ASSERT(found_count == 0); |
| 16047 | found_count++; |
| 16048 | } else { |
| 16049 | new_array->set(i - found_count, old_array->get(i)); |
| 16050 | } |
| 16051 | } |
| 16052 | // If the break point was found in the list change it. |
| 16053 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 16054 | } |
| 16055 | |
| 16056 | |
| 16057 | // Add the specified break point object. |
| 16058 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 16059 | Handle<Object> break_point_object) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16060 | Isolate* isolate = break_point_info->GetIsolate(); |
| 16061 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16062 | // If there was no break point objects before just set it. |
| 16063 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 16064 | break_point_info->set_break_point_objects(*break_point_object); |
| 16065 | return; |
| 16066 | } |
| 16067 | // If the break point object is the same as before just ignore. |
| 16068 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 16069 | // If there was one break point object before replace with array. |
| 16070 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16071 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16072 | array->set(0, break_point_info->break_point_objects()); |
| 16073 | array->set(1, *break_point_object); |
| 16074 | break_point_info->set_break_point_objects(*array); |
| 16075 | return; |
| 16076 | } |
| 16077 | // If there was more than one break point before extend array. |
| 16078 | Handle<FixedArray> old_array = |
| 16079 | Handle<FixedArray>( |
| 16080 | FixedArray::cast(break_point_info->break_point_objects())); |
| 16081 | Handle<FixedArray> new_array = |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16082 | isolate->factory()->NewFixedArray(old_array->length() + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16083 | for (int i = 0; i < old_array->length(); i++) { |
| 16084 | // If the break point was there before just ignore. |
| 16085 | if (old_array->get(i) == *break_point_object) return; |
| 16086 | new_array->set(i, old_array->get(i)); |
| 16087 | } |
| 16088 | // Add the new break point. |
| 16089 | new_array->set(old_array->length(), *break_point_object); |
| 16090 | break_point_info->set_break_point_objects(*new_array); |
| 16091 | } |
| 16092 | |
| 16093 | |
| 16094 | bool BreakPointInfo::HasBreakPointObject( |
| 16095 | Handle<BreakPointInfo> break_point_info, |
| 16096 | Handle<Object> break_point_object) { |
| 16097 | // No break point. |
| 16098 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 16099 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16100 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 16101 | return break_point_info->break_point_objects() == *break_point_object; |
| 16102 | } |
| 16103 | // Multiple break points. |
| 16104 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 16105 | for (int i = 0; i < array->length(); i++) { |
| 16106 | if (array->get(i) == *break_point_object) { |
| 16107 | return true; |
| 16108 | } |
| 16109 | } |
| 16110 | return false; |
| 16111 | } |
| 16112 | |
| 16113 | |
| 16114 | // Get the number of break points. |
| 16115 | int BreakPointInfo::GetBreakPointCount() { |
| 16116 | // No break point. |
| 16117 | if (break_point_objects()->IsUndefined()) return 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 16118 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16119 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 16120 | // Multiple break points. |
| 16121 | return FixedArray::cast(break_point_objects())->length(); |
| 16122 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 16123 | #endif // ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16124 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 16125 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 16126 | Object* JSDate::GetField(Object* object, Smi* index) { |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16127 | return JSDate::cast(object)->DoGetField( |
| 16128 | static_cast<FieldIndex>(index->value())); |
| 16129 | } |
| 16130 | |
| 16131 | |
| 16132 | Object* JSDate::DoGetField(FieldIndex index) { |
| 16133 | ASSERT(index != kDateValue); |
| 16134 | |
| 16135 | DateCache* date_cache = GetIsolate()->date_cache(); |
| 16136 | |
| 16137 | if (index < kFirstUncachedField) { |
| 16138 | Object* stamp = cache_stamp(); |
| 16139 | if (stamp != date_cache->stamp() && stamp->IsSmi()) { |
| 16140 | // Since the stamp is not NaN, the value is also not NaN. |
| 16141 | int64_t local_time_ms = |
| 16142 | date_cache->ToLocal(static_cast<int64_t>(value()->Number())); |
| 16143 | SetLocalFields(local_time_ms, date_cache); |
| 16144 | } |
| 16145 | switch (index) { |
| 16146 | case kYear: return year(); |
| 16147 | case kMonth: return month(); |
| 16148 | case kDay: return day(); |
| 16149 | case kWeekday: return weekday(); |
| 16150 | case kHour: return hour(); |
| 16151 | case kMinute: return min(); |
| 16152 | case kSecond: return sec(); |
| 16153 | default: UNREACHABLE(); |
| 16154 | } |
| 16155 | } |
| 16156 | |
| 16157 | if (index >= kFirstUTCField) { |
| 16158 | return GetUTCField(index, value()->Number(), date_cache); |
| 16159 | } |
| 16160 | |
| 16161 | double time = value()->Number(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 16162 | if (std::isnan(time)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16163 | |
| 16164 | int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time)); |
| 16165 | int days = DateCache::DaysFromTime(local_time_ms); |
| 16166 | |
| 16167 | if (index == kDays) return Smi::FromInt(days); |
| 16168 | |
| 16169 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 16170 | if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000); |
| 16171 | ASSERT(index == kTimeInDay); |
| 16172 | return Smi::FromInt(time_in_day_ms); |
| 16173 | } |
| 16174 | |
| 16175 | |
| 16176 | Object* JSDate::GetUTCField(FieldIndex index, |
| 16177 | double value, |
| 16178 | DateCache* date_cache) { |
| 16179 | ASSERT(index >= kFirstUTCField); |
| 16180 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 16181 | if (std::isnan(value)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16182 | |
| 16183 | int64_t time_ms = static_cast<int64_t>(value); |
| 16184 | |
| 16185 | if (index == kTimezoneOffset) { |
| 16186 | return Smi::FromInt(date_cache->TimezoneOffset(time_ms)); |
| 16187 | } |
| 16188 | |
| 16189 | int days = DateCache::DaysFromTime(time_ms); |
| 16190 | |
| 16191 | if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days)); |
| 16192 | |
| 16193 | if (index <= kDayUTC) { |
| 16194 | int year, month, day; |
| 16195 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 16196 | if (index == kYearUTC) return Smi::FromInt(year); |
| 16197 | if (index == kMonthUTC) return Smi::FromInt(month); |
| 16198 | ASSERT(index == kDayUTC); |
| 16199 | return Smi::FromInt(day); |
| 16200 | } |
| 16201 | |
| 16202 | int time_in_day_ms = DateCache::TimeInDay(time_ms, days); |
| 16203 | switch (index) { |
| 16204 | case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000)); |
| 16205 | case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60); |
| 16206 | case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60); |
| 16207 | case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000); |
| 16208 | case kDaysUTC: return Smi::FromInt(days); |
| 16209 | case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms); |
| 16210 | default: UNREACHABLE(); |
| 16211 | } |
| 16212 | |
| 16213 | UNREACHABLE(); |
| 16214 | return NULL; |
| 16215 | } |
| 16216 | |
| 16217 | |
| 16218 | void JSDate::SetValue(Object* value, bool is_value_nan) { |
| 16219 | set_value(value); |
| 16220 | if (is_value_nan) { |
| 16221 | HeapNumber* nan = GetIsolate()->heap()->nan_value(); |
| 16222 | set_cache_stamp(nan, SKIP_WRITE_BARRIER); |
| 16223 | set_year(nan, SKIP_WRITE_BARRIER); |
| 16224 | set_month(nan, SKIP_WRITE_BARRIER); |
| 16225 | set_day(nan, SKIP_WRITE_BARRIER); |
| 16226 | set_hour(nan, SKIP_WRITE_BARRIER); |
| 16227 | set_min(nan, SKIP_WRITE_BARRIER); |
| 16228 | set_sec(nan, SKIP_WRITE_BARRIER); |
| 16229 | set_weekday(nan, SKIP_WRITE_BARRIER); |
| 16230 | } else { |
| 16231 | set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER); |
| 16232 | } |
| 16233 | } |
| 16234 | |
| 16235 | |
| 16236 | void JSDate::SetLocalFields(int64_t local_time_ms, DateCache* date_cache) { |
| 16237 | int days = DateCache::DaysFromTime(local_time_ms); |
| 16238 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 16239 | int year, month, day; |
| 16240 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 16241 | int weekday = date_cache->Weekday(days); |
| 16242 | int hour = time_in_day_ms / (60 * 60 * 1000); |
| 16243 | int min = (time_in_day_ms / (60 * 1000)) % 60; |
| 16244 | int sec = (time_in_day_ms / 1000) % 60; |
| 16245 | set_cache_stamp(date_cache->stamp()); |
| 16246 | set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
| 16247 | set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
| 16248 | set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
| 16249 | set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
| 16250 | set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
| 16251 | set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
| 16252 | set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
| 16253 | } |
| 16254 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16255 | |
| 16256 | void JSArrayBuffer::Neuter() { |
| 16257 | ASSERT(is_external()); |
| 16258 | set_backing_store(NULL); |
| 16259 | set_byte_length(Smi::FromInt(0)); |
| 16260 | } |
| 16261 | |
| 16262 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16263 | void JSArrayBufferView::NeuterView() { |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16264 | set_byte_offset(Smi::FromInt(0)); |
| 16265 | set_byte_length(Smi::FromInt(0)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16266 | } |
| 16267 | |
| 16268 | |
| 16269 | void JSDataView::Neuter() { |
| 16270 | NeuterView(); |
| 16271 | } |
| 16272 | |
| 16273 | |
| 16274 | void JSTypedArray::Neuter() { |
| 16275 | NeuterView(); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16276 | set_length(Smi::FromInt(0)); |
| 16277 | set_elements(GetHeap()->EmptyExternalArrayForMap(map())); |
| 16278 | } |
| 16279 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16280 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 16281 | Type* PropertyCell::type() { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16282 | return static_cast<Type*>(type_raw()); |
| 16283 | } |
| 16284 | |
| 16285 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 16286 | void PropertyCell::set_type(Type* type, WriteBarrierMode ignored) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16287 | ASSERT(IsPropertyCell()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16288 | set_type_raw(type, ignored); |
| 16289 | } |
| 16290 | |
| 16291 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16292 | Type* PropertyCell::UpdateType(Handle<PropertyCell> cell, |
| 16293 | Handle<Object> value) { |
| 16294 | Isolate* isolate = cell->GetIsolate(); |
| 16295 | Handle<Type> old_type(cell->type(), isolate); |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 16296 | // TODO(2803): Do not track ConsString as constant because they cannot be |
| 16297 | // embedded into code. |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 16298 | Handle<Type> new_type(value->IsConsString() || value->IsTheHole() |
| 16299 | ? Type::Any() |
| 16300 | : Type::Constant(value, isolate), isolate); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16301 | |
| 16302 | if (new_type->Is(old_type)) { |
| 16303 | return *old_type; |
| 16304 | } |
| 16305 | |
| 16306 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 16307 | isolate, DependentCode::kPropertyCellChangedGroup); |
| 16308 | |
| 16309 | if (old_type->Is(Type::None()) || old_type->Is(Type::Undefined())) { |
| 16310 | return *new_type; |
| 16311 | } |
| 16312 | |
| 16313 | return Type::Any(); |
| 16314 | } |
| 16315 | |
| 16316 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16317 | void PropertyCell::SetValueInferType(Handle<PropertyCell> cell, |
| 16318 | Handle<Object> value, |
| 16319 | WriteBarrierMode mode) { |
| 16320 | CALL_HEAP_FUNCTION_VOID(cell->GetIsolate(), |
| 16321 | cell->SetValueInferType(*value, mode)); |
| 16322 | } |
| 16323 | |
| 16324 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16325 | MaybeObject* PropertyCell::SetValueInferType(Object* value, |
| 16326 | WriteBarrierMode ignored) { |
| 16327 | set_value(value, ignored); |
| 16328 | if (!Type::Any()->Is(type())) { |
| 16329 | IdempotentPointerToHandleCodeTrampoline trampoline(GetIsolate()); |
| 16330 | MaybeObject* maybe_type = trampoline.CallWithReturnValue( |
| 16331 | &PropertyCell::UpdateType, |
| 16332 | Handle<PropertyCell>(this), |
| 16333 | Handle<Object>(value, GetIsolate())); |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 16334 | Type* new_type = NULL; |
| 16335 | if (!maybe_type->To(&new_type)) return maybe_type; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16336 | set_type(new_type); |
| 16337 | } |
| 16338 | return value; |
| 16339 | } |
| 16340 | |
| 16341 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16342 | void PropertyCell::AddDependentCompilationInfo(CompilationInfo* info) { |
| 16343 | Handle<DependentCode> dep(dependent_code()); |
| 16344 | Handle<DependentCode> codes = |
| 16345 | DependentCode::Insert(dep, DependentCode::kPropertyCellChangedGroup, |
| 16346 | info->object_wrapper()); |
| 16347 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 16348 | info->dependencies(DependentCode::kPropertyCellChangedGroup)->Add( |
| 16349 | Handle<HeapObject>(this), info->zone()); |
| 16350 | } |
| 16351 | |
| 16352 | |
| 16353 | void PropertyCell::AddDependentCode(Handle<Code> code) { |
| 16354 | Handle<DependentCode> codes = DependentCode::Insert( |
| 16355 | Handle<DependentCode>(dependent_code()), |
| 16356 | DependentCode::kPropertyCellChangedGroup, code); |
| 16357 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 16358 | } |
| 16359 | |
| 16360 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 16361 | const char* GetBailoutReason(BailoutReason reason) { |
| 16362 | ASSERT(reason < kLastErrorMessage); |
| 16363 | #define ERROR_MESSAGES_TEXTS(C, T) T, |
| 16364 | static const char* error_messages_[] = { |
| 16365 | ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) |
| 16366 | }; |
| 16367 | #undef ERROR_MESSAGES_TEXTS |
| 16368 | return error_messages_[reason]; |
| 16369 | } |
| 16370 | |
| 16371 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16372 | } } // namespace v8::internal |