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 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 346 | Handle<FixedArray> JSObject::EnsureWritableFastElements( |
| 347 | Handle<JSObject> object) { |
| 348 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 349 | object->EnsureWritableFastElements(), |
| 350 | FixedArray); |
| 351 | } |
| 352 | |
| 353 | |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 354 | Handle<Object> JSObject::GetPropertyWithCallback(Handle<JSObject> object, |
| 355 | Handle<Object> receiver, |
| 356 | Handle<Object> structure, |
| 357 | Handle<Name> name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 358 | Isolate* isolate = name->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | // 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] | 360 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 362 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 364 | reinterpret_cast<AccessorDescriptor*>( |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 365 | Handle<Foreign>::cast(structure)->foreign_address()); |
| 366 | CALL_HEAP_FUNCTION(isolate, |
| 367 | (callback->getter)(isolate, *receiver, callback->data), |
| 368 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // api style callbacks. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 372 | if (structure->IsAccessorInfo()) { |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 373 | Handle<AccessorInfo> accessor_info = Handle<AccessorInfo>::cast(structure); |
| 374 | if (!accessor_info->IsCompatibleReceiver(*receiver)) { |
| 375 | Handle<Object> args[2] = { name, receiver }; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 376 | Handle<Object> error = |
| 377 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 378 | HandleVector(args, |
| 379 | ARRAY_SIZE(args))); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 380 | isolate->Throw(*error); |
| 381 | return Handle<Object>::null(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 382 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 383 | // TODO(rossberg): Handling symbols in the API requires changing the API, |
| 384 | // so we do not support it for now. |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 385 | if (name->IsSymbol()) return isolate->factory()->undefined_value(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 386 | if (structure->IsDeclaredAccessorInfo()) { |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 387 | CALL_HEAP_FUNCTION( |
| 388 | isolate, |
| 389 | GetDeclaredAccessorProperty(*receiver, |
| 390 | DeclaredAccessorInfo::cast(*structure), |
| 391 | isolate), |
| 392 | Object); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 393 | } |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 394 | |
| 395 | Handle<ExecutableAccessorInfo> data = |
| 396 | Handle<ExecutableAccessorInfo>::cast(structure); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 397 | v8::AccessorGetterCallback call_fun = |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 398 | v8::ToCData<v8::AccessorGetterCallback>(data->getter()); |
| 399 | if (call_fun == NULL) return isolate->factory()->undefined_value(); |
| 400 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 401 | HandleScope scope(isolate); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 402 | Handle<JSObject> self = Handle<JSObject>::cast(receiver); |
| 403 | Handle<String> key = Handle<String>::cast(name); |
| 404 | LOG(isolate, ApiNamedPropertyAccess("load", *self, *name)); |
| 405 | PropertyCallbackArguments args(isolate, data->data(), *self, *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 406 | v8::Handle<v8::Value> result = |
| 407 | args.Call(call_fun, v8::Utils::ToLocal(key)); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 408 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 409 | if (result.IsEmpty()) { |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 410 | return isolate->factory()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 411 | } |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 412 | Handle<Object> return_value = v8::Utils::OpenHandle(*result); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 413 | return_value->VerifyApiCallResultType(); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 414 | return scope.CloseAndEscape(return_value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | // __defineGetter__ callback |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 418 | Handle<Object> getter(Handle<AccessorPair>::cast(structure)->getter(), |
| 419 | isolate); |
| 420 | if (getter->IsSpecFunction()) { |
| 421 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 422 | CALL_HEAP_FUNCTION( |
| 423 | isolate, |
| 424 | object->GetPropertyWithDefinedGetter(*receiver, |
| 425 | JSReceiver::cast(*getter)), |
| 426 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 427 | } |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 428 | // Getter is not a function. |
| 429 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 433 | MaybeObject* JSProxy::GetPropertyWithHandler(Object* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 434 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 435 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 436 | HandleScope scope(isolate); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 437 | Handle<Object> receiver(receiver_raw, isolate); |
| 438 | Handle<Object> name(name_raw, isolate); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 439 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 440 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 441 | if (name->IsSymbol()) return isolate->heap()->undefined_value(); |
| 442 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 443 | Handle<Object> args[] = { receiver, name }; |
| 444 | Handle<Object> result = CallTrap( |
| 445 | "get", isolate->derived_get_trap(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 446 | if (isolate->has_pending_exception()) return Failure::Exception(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 447 | |
| 448 | return *result; |
| 449 | } |
| 450 | |
| 451 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 452 | Handle<Object> Object::GetProperty(Handle<Object> object, |
| 453 | Handle<Name> name) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 454 | // TODO(rossberg): The index test should not be here but in the GetProperty |
| 455 | // method (or somewhere else entirely). Needs more global clean-up. |
| 456 | uint32_t index; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 457 | Isolate* isolate = name->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 458 | if (name->AsArrayIndex(&index)) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 459 | return GetElement(isolate, object, index); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 460 | CALL_HEAP_FUNCTION(isolate, object->GetProperty(*name), Object); |
| 461 | } |
| 462 | |
| 463 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 464 | Handle<Object> Object::GetElement(Isolate* isolate, |
| 465 | Handle<Object> object, |
| 466 | uint32_t index) { |
| 467 | CALL_HEAP_FUNCTION(isolate, object->GetElement(isolate, index), Object); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 471 | MaybeObject* JSProxy::GetElementWithHandler(Object* receiver, |
| 472 | uint32_t index) { |
| 473 | String* name; |
| 474 | MaybeObject* maybe = GetHeap()->Uint32ToString(index); |
| 475 | if (!maybe->To<String>(&name)) return maybe; |
| 476 | return GetPropertyWithHandler(receiver, name); |
| 477 | } |
| 478 | |
| 479 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 480 | Handle<Object> JSProxy::SetElementWithHandler(Handle<JSProxy> proxy, |
| 481 | Handle<JSReceiver> receiver, |
| 482 | uint32_t index, |
| 483 | Handle<Object> value, |
| 484 | StrictModeFlag strict_mode) { |
| 485 | Isolate* isolate = proxy->GetIsolate(); |
| 486 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 487 | return SetPropertyWithHandler( |
| 488 | proxy, receiver, name, value, NONE, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 492 | bool JSProxy::HasElementWithHandler(Handle<JSProxy> proxy, uint32_t index) { |
| 493 | Isolate* isolate = proxy->GetIsolate(); |
| 494 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
| 495 | return HasPropertyWithHandler(proxy, name); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 499 | MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 500 | JSReceiver* getter) { |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 501 | Isolate* isolate = getter->GetIsolate(); |
| 502 | HandleScope scope(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 503 | Handle<JSReceiver> fun(getter); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 504 | Handle<Object> self(receiver, isolate); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 505 | #ifdef ENABLE_DEBUGGER_SUPPORT |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 506 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 507 | // Handle stepping into a getter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 508 | // TODO(rossberg): should this apply to getters that are function proxies? |
| 509 | if (debug->StepInActive() && fun->IsJSFunction()) { |
| 510 | debug->HandleStepIn( |
| 511 | Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 512 | } |
| 513 | #endif |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 514 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 515 | bool has_pending_exception; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 516 | Handle<Object> result = Execution::Call( |
| 517 | isolate, fun, self, 0, NULL, &has_pending_exception, true); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 518 | // Check for pending exception and return the result. |
| 519 | if (has_pending_exception) return Failure::Exception(); |
| 520 | return *result; |
| 521 | } |
| 522 | |
| 523 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 524 | // Only deal with CALLBACKS and INTERCEPTOR |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 525 | Handle<Object> JSObject::GetPropertyWithFailedAccessCheck( |
| 526 | Handle<JSObject> object, |
| 527 | Handle<Object> receiver, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 528 | LookupResult* result, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 529 | Handle<Name> name, |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 530 | PropertyAttributes* attributes) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 531 | Isolate* isolate = name->GetIsolate(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 532 | if (result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 533 | switch (result->type()) { |
| 534 | case CALLBACKS: { |
| 535 | // Only allow API accessors. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 536 | Handle<Object> callback_obj(result->GetCallbackObject(), isolate); |
| 537 | if (callback_obj->IsAccessorInfo()) { |
| 538 | if (!AccessorInfo::cast(*callback_obj)->all_can_read()) break; |
| 539 | *attributes = result->GetAttributes(); |
| 540 | // Fall through to GetPropertyWithCallback. |
| 541 | } else if (callback_obj->IsAccessorPair()) { |
| 542 | if (!AccessorPair::cast(*callback_obj)->all_can_read()) break; |
| 543 | // Fall through to GetPropertyWithCallback. |
| 544 | } else { |
| 545 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 546 | } |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 547 | Handle<JSObject> holder(result->holder(), isolate); |
| 548 | return GetPropertyWithCallback(holder, receiver, callback_obj, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 549 | } |
| 550 | case NORMAL: |
| 551 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 552 | case CONSTANT: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 553 | // Search ALL_CAN_READ accessors in prototype chain. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 554 | LookupResult r(isolate); |
| 555 | result->holder()->LookupRealNamedPropertyInPrototypes(*name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 556 | if (r.IsProperty()) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 557 | return GetPropertyWithFailedAccessCheck( |
| 558 | object, receiver, &r, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | } |
| 560 | break; |
| 561 | } |
| 562 | case INTERCEPTOR: { |
| 563 | // If the object has an interceptor, try real named properties. |
| 564 | // No access check in GetPropertyAttributeWithInterceptor. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 565 | LookupResult r(isolate); |
| 566 | result->holder()->LookupRealNamedProperty(*name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 567 | if (r.IsProperty()) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 568 | return GetPropertyWithFailedAccessCheck( |
| 569 | object, receiver, &r, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 570 | } |
ager@chromium.org | 9ed6c32 | 2010-02-19 12:24:05 +0000 | [diff] [blame] | 571 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 572 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 573 | default: |
| 574 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 578 | // No accessible property found. |
| 579 | *attributes = ABSENT; |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 580 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_GET); |
| 581 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 582 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 586 | PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck( |
| 587 | Object* receiver, |
| 588 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 589 | Name* name, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 590 | bool continue_search) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 591 | if (result->IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 592 | switch (result->type()) { |
| 593 | case CALLBACKS: { |
| 594 | // Only allow API accessors. |
| 595 | Object* obj = result->GetCallbackObject(); |
| 596 | if (obj->IsAccessorInfo()) { |
| 597 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 598 | if (info->all_can_read()) { |
| 599 | return result->GetAttributes(); |
| 600 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 601 | } else if (obj->IsAccessorPair()) { |
| 602 | AccessorPair* pair = AccessorPair::cast(obj); |
| 603 | if (pair->all_can_read()) { |
| 604 | return result->GetAttributes(); |
| 605 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 606 | } |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | case NORMAL: |
| 611 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 612 | case CONSTANT: { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 613 | if (!continue_search) break; |
| 614 | // Search ALL_CAN_READ accessors in prototype chain. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 615 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 616 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 617 | if (r.IsProperty()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 618 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 619 | &r, |
| 620 | name, |
| 621 | continue_search); |
| 622 | } |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | case INTERCEPTOR: { |
| 627 | // If the object has an interceptor, try real named properties. |
| 628 | // No access check in GetPropertyAttributeWithInterceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 629 | LookupResult r(GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 630 | if (continue_search) { |
| 631 | result->holder()->LookupRealNamedProperty(name, &r); |
| 632 | } else { |
| 633 | result->holder()->LocalLookupRealNamedProperty(name, &r); |
| 634 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 635 | if (!r.IsFound()) break; |
| 636 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 637 | &r, |
| 638 | name, |
| 639 | continue_search); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 640 | } |
| 641 | |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 642 | case HANDLER: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 643 | case TRANSITION: |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 644 | case NONEXISTENT: |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 645 | UNREACHABLE(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 649 | GetIsolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 650 | return ABSENT; |
| 651 | } |
| 652 | |
| 653 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 654 | Object* JSObject::GetNormalizedProperty(LookupResult* result) { |
| 655 | ASSERT(!HasFastProperties()); |
| 656 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 657 | if (IsGlobalObject()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 658 | value = PropertyCell::cast(value)->value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 659 | } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 660 | ASSERT(!value->IsPropertyCell() && !value->IsCell()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 661 | return value; |
| 662 | } |
| 663 | |
| 664 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 665 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 666 | LookupResult* result, |
| 667 | Handle<Object> value) { |
| 668 | ASSERT(!object->HasFastProperties()); |
| 669 | NameDictionary* property_dictionary = object->property_dictionary(); |
| 670 | if (object->IsGlobalObject()) { |
| 671 | Handle<PropertyCell> cell(PropertyCell::cast( |
| 672 | property_dictionary->ValueAt(result->GetDictionaryEntry()))); |
| 673 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 674 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 675 | property_dictionary->ValueAtPut(result->GetDictionaryEntry(), *value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 676 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 680 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 681 | static Handle<NameDictionary> NameDictionaryAdd(Handle<NameDictionary> dict, |
| 682 | Handle<Name> name, |
| 683 | Handle<Object> value, |
| 684 | PropertyDetails details) { |
| 685 | CALL_HEAP_FUNCTION(dict->GetIsolate(), |
| 686 | dict->Add(*name, *value, details), |
| 687 | NameDictionary); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 691 | void JSObject::SetNormalizedProperty(Handle<JSObject> object, |
| 692 | Handle<Name> name, |
| 693 | Handle<Object> value, |
| 694 | PropertyDetails details) { |
| 695 | ASSERT(!object->HasFastProperties()); |
| 696 | Handle<NameDictionary> property_dictionary(object->property_dictionary()); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 697 | |
| 698 | if (!name->IsUniqueName()) { |
| 699 | name = object->GetIsolate()->factory()->InternalizedStringFromString( |
| 700 | Handle<String>::cast(name)); |
| 701 | } |
| 702 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 703 | int entry = property_dictionary->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 704 | if (entry == NameDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 705 | Handle<Object> store_value = value; |
| 706 | if (object->IsGlobalObject()) { |
| 707 | store_value = object->GetIsolate()->factory()->NewPropertyCell(value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 708 | } |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 709 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 710 | property_dictionary = |
| 711 | NameDictionaryAdd(property_dictionary, name, store_value, details); |
| 712 | object->set_properties(*property_dictionary); |
| 713 | return; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 714 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 715 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 716 | PropertyDetails original_details = property_dictionary->DetailsAt(entry); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 717 | int enumeration_index; |
| 718 | // Preserve the enumeration index unless the property was deleted. |
| 719 | if (original_details.IsDeleted()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 720 | enumeration_index = property_dictionary->NextEnumerationIndex(); |
| 721 | property_dictionary->SetNextEnumerationIndex(enumeration_index + 1); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 722 | } else { |
| 723 | enumeration_index = original_details.dictionary_index(); |
| 724 | ASSERT(enumeration_index > 0); |
| 725 | } |
| 726 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 727 | details = PropertyDetails( |
| 728 | details.attributes(), details.type(), enumeration_index); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 729 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 730 | if (object->IsGlobalObject()) { |
| 731 | Handle<PropertyCell> cell( |
| 732 | PropertyCell::cast(property_dictionary->ValueAt(entry))); |
| 733 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 734 | // Please note we have to update the property details. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 735 | property_dictionary->DetailsAtPut(entry, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 736 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 737 | property_dictionary->SetEntry(entry, *name, *value, details); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 738 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 742 | // TODO(mstarzinger): Temporary wrapper until target is handlified. |
| 743 | Handle<NameDictionary> NameDictionaryShrink(Handle<NameDictionary> dict, |
| 744 | Handle<Name> name) { |
| 745 | CALL_HEAP_FUNCTION(dict->GetIsolate(), dict->Shrink(*name), NameDictionary); |
| 746 | } |
| 747 | |
| 748 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 749 | Handle<Object> JSObject::DeleteNormalizedProperty(Handle<JSObject> object, |
| 750 | Handle<Name> name, |
| 751 | DeleteMode mode) { |
| 752 | ASSERT(!object->HasFastProperties()); |
| 753 | Isolate* isolate = object->GetIsolate(); |
| 754 | Handle<NameDictionary> dictionary(object->property_dictionary()); |
| 755 | int entry = dictionary->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 756 | if (entry != NameDictionary::kNotFound) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 757 | // 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] | 758 | if (object->IsGlobalObject()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 759 | PropertyDetails details = dictionary->DetailsAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 760 | if (details.IsDontDelete()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 761 | if (mode != FORCE_DELETION) return isolate->factory()->false_value(); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 762 | // When forced to delete global properties, we have to make a |
| 763 | // map change to invalidate any ICs that think they can load |
| 764 | // from the DontDelete cell without checking if it contains |
| 765 | // the hole value. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 766 | Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 767 | ASSERT(new_map->is_dictionary_map()); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 768 | object->set_map(*new_map); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 769 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 770 | Handle<PropertyCell> cell(PropertyCell::cast(dictionary->ValueAt(entry))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 771 | Handle<Object> value = isolate->factory()->the_hole_value(); |
| 772 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 773 | dictionary->DetailsAtPut(entry, details.AsDeleted()); |
| 774 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 775 | Handle<Object> deleted(dictionary->DeleteProperty(entry, mode), isolate); |
| 776 | if (*deleted == isolate->heap()->true_value()) { |
| 777 | Handle<NameDictionary> new_properties = |
| 778 | NameDictionaryShrink(dictionary, name); |
| 779 | object->set_properties(*new_properties); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 780 | } |
| 781 | return deleted; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 784 | return isolate->factory()->true_value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 788 | bool JSObject::IsDirty() { |
| 789 | Object* cons_obj = map()->constructor(); |
| 790 | if (!cons_obj->IsJSFunction()) |
| 791 | return true; |
| 792 | JSFunction* fun = JSFunction::cast(cons_obj); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 793 | if (!fun->shared()->IsApiFunction()) |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 794 | return true; |
| 795 | // If the object is fully fast case and has the same map it was |
| 796 | // created with then no changes can have been made to it. |
| 797 | return map() != fun->initial_map() |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 798 | || !HasFastObjectElements() |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 799 | || !HasFastProperties(); |
| 800 | } |
| 801 | |
| 802 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 803 | Handle<Object> Object::GetProperty(Handle<Object> object, |
| 804 | Handle<Object> receiver, |
| 805 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 806 | Handle<Name> key, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 807 | PropertyAttributes* attributes) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 808 | Isolate* isolate = result->isolate(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 809 | CALL_HEAP_FUNCTION( |
| 810 | isolate, |
| 811 | object->GetProperty(*receiver, result, *key, attributes), |
| 812 | Object); |
| 813 | } |
| 814 | |
| 815 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 816 | MaybeObject* Object::GetPropertyOrFail(Handle<Object> object, |
| 817 | Handle<Object> receiver, |
| 818 | LookupResult* result, |
| 819 | Handle<Name> key, |
| 820 | PropertyAttributes* attributes) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 821 | Isolate* isolate = result->isolate(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 822 | CALL_HEAP_FUNCTION_PASS_EXCEPTION( |
| 823 | isolate, |
| 824 | object->GetProperty(*receiver, result, *key, attributes)); |
| 825 | } |
| 826 | |
| 827 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 828 | // TODO(yangguo): handlify this and get rid of. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 829 | MaybeObject* Object::GetProperty(Object* receiver, |
| 830 | LookupResult* result, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 831 | Name* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 832 | PropertyAttributes* attributes) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 833 | Isolate* isolate = name->GetIsolate(); |
| 834 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 835 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 836 | #ifdef DEBUG |
| 837 | // TODO(mstarzinger): Only because of the AssertNoContextChange, drop as soon |
| 838 | // as this method has been fully handlified. |
| 839 | HandleScope scope(isolate); |
| 840 | #endif |
| 841 | |
| 842 | // Make sure that the top context does not change when doing |
| 843 | // callbacks or interceptor calls. |
| 844 | AssertNoContextChange ncc(isolate); |
| 845 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 846 | // Traverse the prototype chain from the current object (this) to |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 847 | // the holder and check for access rights. This avoids traversing the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 848 | // objects more than once in case of interceptors, because the |
| 849 | // holder will always be the interceptor holder and the search may |
| 850 | // only continue with a current object just after the interceptor |
| 851 | // holder in the prototype chain. |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 852 | // Proxy handlers do not use the proxy's prototype, so we can skip this. |
| 853 | if (!result->IsHandler()) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 854 | Object* last = result->IsProperty() |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 855 | ? result->holder() |
| 856 | : Object::cast(heap->null_value()); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 857 | ASSERT(this != this->GetPrototype(isolate)); |
| 858 | for (Object* current = this; |
| 859 | true; |
| 860 | current = current->GetPrototype(isolate)) { |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 861 | if (current->IsAccessCheckNeeded()) { |
| 862 | // Check if we're allowed to read from the current object. Note |
| 863 | // that even though we may not actually end up loading the named |
| 864 | // property from the current object, we still check that we have |
| 865 | // access to it. |
| 866 | JSObject* checked = JSObject::cast(current); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 867 | if (!isolate->MayNamedAccess(checked, name, v8::ACCESS_GET)) { |
| 868 | HandleScope scope(isolate); |
| 869 | Handle<Object> value = JSObject::GetPropertyWithFailedAccessCheck( |
| 870 | handle(checked, isolate), |
| 871 | handle(receiver, isolate), |
| 872 | result, |
| 873 | handle(name, isolate), |
| 874 | attributes); |
| 875 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
| 876 | return *value; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 877 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 878 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 879 | // Stop traversing the chain once we reach the last object in the |
| 880 | // chain; either the holder of the result or null in case of an |
| 881 | // absent property. |
| 882 | if (current == last) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | } |
| 885 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 886 | if (!result->IsProperty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 887 | *attributes = ABSENT; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 888 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 889 | } |
| 890 | *attributes = result->GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 891 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 892 | switch (result->type()) { |
| 893 | case NORMAL: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 894 | value = result->holder()->GetNormalizedProperty(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 896 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 897 | case FIELD: { |
| 898 | MaybeObject* maybe_result = result->holder()->FastPropertyAt( |
| 899 | result->representation(), |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 900 | result->GetFieldIndex().field_index()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 901 | if (!maybe_result->To(&value)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 902 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 903 | return value->IsTheHole() ? heap->undefined_value() : value; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 904 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 905 | case CONSTANT: |
| 906 | return result->GetConstant(); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 907 | case CALLBACKS: { |
| 908 | HandleScope scope(isolate); |
| 909 | Handle<Object> value = JSObject::GetPropertyWithCallback( |
| 910 | handle(result->holder(), isolate), |
| 911 | handle(receiver, isolate), |
| 912 | handle(result->GetCallbackObject(), isolate), |
| 913 | handle(name, isolate)); |
| 914 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
| 915 | return *value; |
| 916 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 917 | case HANDLER: |
| 918 | return result->proxy()->GetPropertyWithHandler(receiver, name); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 919 | case INTERCEPTOR: { |
| 920 | HandleScope scope(isolate); |
| 921 | Handle<Object> value = JSObject::GetPropertyWithInterceptor( |
| 922 | handle(result->holder(), isolate), |
| 923 | handle(receiver, isolate), |
| 924 | handle(name, isolate), |
| 925 | attributes); |
| 926 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
| 927 | return *value; |
| 928 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 929 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 930 | case NONEXISTENT: |
| 931 | UNREACHABLE(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 932 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 933 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 934 | UNREACHABLE(); |
| 935 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 939 | MaybeObject* Object::GetElementWithReceiver(Isolate* isolate, |
| 940 | Object* receiver, |
| 941 | uint32_t index) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 942 | Heap* heap = isolate->heap(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 943 | Object* holder = this; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 944 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 945 | // Iterate up the prototype chain until an element is found or the null |
| 946 | // prototype is encountered. |
| 947 | for (holder = this; |
| 948 | holder != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 949 | holder = holder->GetPrototype(isolate)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 950 | if (!holder->IsJSObject()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 951 | Context* native_context = isolate->context()->native_context(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 952 | if (holder->IsNumber()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 953 | holder = native_context->number_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 954 | } else if (holder->IsString()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 955 | holder = native_context->string_function()->instance_prototype(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 956 | } else if (holder->IsSymbol()) { |
| 957 | holder = native_context->symbol_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 958 | } else if (holder->IsBoolean()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 959 | holder = native_context->boolean_function()->instance_prototype(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 960 | } else if (holder->IsJSProxy()) { |
| 961 | return JSProxy::cast(holder)->GetElementWithHandler(receiver, index); |
| 962 | } else { |
| 963 | // Undefined and null have no indexed properties. |
| 964 | ASSERT(holder->IsUndefined() || holder->IsNull()); |
| 965 | return heap->undefined_value(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | // Inline the case for JSObjects. Doing so significantly improves the |
| 970 | // performance of fetching elements where checking the prototype chain is |
| 971 | // necessary. |
| 972 | JSObject* js_object = JSObject::cast(holder); |
| 973 | |
| 974 | // Check access rights if needed. |
| 975 | if (js_object->IsAccessCheckNeeded()) { |
| 976 | Isolate* isolate = heap->isolate(); |
| 977 | if (!isolate->MayIndexedAccess(js_object, index, v8::ACCESS_GET)) { |
| 978 | isolate->ReportFailedAccessCheck(js_object, v8::ACCESS_GET); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 979 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 980 | return heap->undefined_value(); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | if (js_object->HasIndexedInterceptor()) { |
| 985 | return js_object->GetElementWithInterceptor(receiver, index); |
| 986 | } |
| 987 | |
| 988 | if (js_object->elements() != heap->empty_fixed_array()) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 989 | MaybeObject* result = js_object->GetElementsAccessor()->Get( |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 990 | receiver, js_object, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 991 | if (result != heap->the_hole_value()) return result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 992 | } |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 993 | } |
| 994 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 995 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 999 | Object* Object::GetPrototype(Isolate* isolate) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1000 | if (IsSmi()) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1001 | Context* context = isolate->context()->native_context(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1002 | return context->number_function()->instance_prototype(); |
| 1003 | } |
| 1004 | |
| 1005 | HeapObject* heap_object = HeapObject::cast(this); |
| 1006 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1007 | // The object is either a number, a string, a boolean, |
| 1008 | // a real JS object, or a Harmony proxy. |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 1009 | if (heap_object->IsJSReceiver()) { |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1010 | return heap_object->map()->prototype(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1011 | } |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1012 | Context* context = isolate->context()->native_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1013 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1014 | if (heap_object->IsHeapNumber()) { |
| 1015 | return context->number_function()->instance_prototype(); |
| 1016 | } |
| 1017 | if (heap_object->IsString()) { |
| 1018 | return context->string_function()->instance_prototype(); |
| 1019 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1020 | if (heap_object->IsSymbol()) { |
| 1021 | return context->symbol_function()->instance_prototype(); |
| 1022 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1023 | if (heap_object->IsBoolean()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1024 | return context->boolean_function()->instance_prototype(); |
| 1025 | } else { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 1026 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 1031 | Map* Object::GetMarkerMap(Isolate* isolate) { |
| 1032 | if (IsSmi()) return isolate->heap()->heap_number_map(); |
| 1033 | return HeapObject::cast(this)->map(); |
| 1034 | } |
| 1035 | |
| 1036 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 1037 | Object* Object::GetHash() { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1038 | // 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] | 1039 | // a real JS object, or a Harmony proxy. |
| 1040 | if (IsNumber()) { |
| 1041 | uint32_t hash = ComputeLongHash(double_to_uint64(Number())); |
| 1042 | return Smi::FromInt(hash & Smi::kMaxValue); |
| 1043 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1044 | if (IsName()) { |
| 1045 | uint32_t hash = Name::cast(this)->Hash(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1046 | return Smi::FromInt(hash); |
| 1047 | } |
| 1048 | if (IsOddball()) { |
| 1049 | uint32_t hash = Oddball::cast(this)->to_string()->Hash(); |
| 1050 | return Smi::FromInt(hash); |
| 1051 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1052 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 1053 | ASSERT(IsJSReceiver()); |
| 1054 | return JSReceiver::cast(this)->GetIdentityHash(); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | Handle<Object> Object::GetOrCreateHash(Handle<Object> object, |
| 1059 | Isolate* isolate) { |
| 1060 | Handle<Object> hash(object->GetHash(), isolate); |
| 1061 | if (hash->IsSmi()) |
| 1062 | return hash; |
| 1063 | |
| 1064 | ASSERT(object->IsJSReceiver()); |
| 1065 | return JSReceiver::GetOrCreateIdentityHash(Handle<JSReceiver>::cast(object)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | bool Object::SameValue(Object* other) { |
| 1070 | if (other == this) return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1071 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 1072 | // 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] | 1073 | // a real JS object, or a Harmony proxy. |
| 1074 | if (IsNumber() && other->IsNumber()) { |
| 1075 | double this_value = Number(); |
| 1076 | double other_value = other->Number(); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 1077 | bool equal = this_value == other_value; |
| 1078 | // SameValue(NaN, NaN) is true. |
| 1079 | if (!equal) return std::isnan(this_value) && std::isnan(other_value); |
| 1080 | // SameValue(0.0, -0.0) is false. |
| 1081 | return (this_value != 0) || ((1 / this_value) == (1 / other_value)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1082 | } |
| 1083 | if (IsString() && other->IsString()) { |
| 1084 | return String::cast(this)->Equals(String::cast(other)); |
| 1085 | } |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1090 | void Object::ShortPrint(FILE* out) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1091 | HeapStringAllocator allocator; |
| 1092 | StringStream accumulator(&allocator); |
| 1093 | ShortPrint(&accumulator); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1094 | accumulator.OutputToFile(out); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | void Object::ShortPrint(StringStream* accumulator) { |
| 1099 | if (IsSmi()) { |
| 1100 | Smi::cast(this)->SmiPrint(accumulator); |
| 1101 | } else if (IsFailure()) { |
| 1102 | Failure::cast(this)->FailurePrint(accumulator); |
| 1103 | } else { |
| 1104 | HeapObject::cast(this)->HeapObjectShortPrint(accumulator); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1109 | void Smi::SmiPrint(FILE* out) { |
| 1110 | PrintF(out, "%d", value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | void Smi::SmiPrint(StringStream* accumulator) { |
| 1115 | accumulator->Add("%d", value()); |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | void Failure::FailurePrint(StringStream* accumulator) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1120 | accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1124 | void Failure::FailurePrint(FILE* out) { |
| 1125 | PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1129 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 1130 | // English? Returns false for non-ASCII or words that don't start with |
| 1131 | // a capital letter. The a/an rule follows pronunciation in English. |
| 1132 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 1133 | // you speak a dialect you may well say "an 'istoric occasion". |
| 1134 | static bool AnWord(String* str) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1135 | if (str->length() == 0) return false; // A nothing. |
| 1136 | int c0 = str->Get(0); |
| 1137 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1138 | if (c0 == 'U') { |
| 1139 | if (c1 > 'Z') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1140 | return true; // An Umpire, but a UTF8String, a U. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1143 | return true; // An Ape, an ABCBook. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1144 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 1145 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 1146 | c0 == 'S' || c0 == 'X')) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1147 | return true; // An MP3File, an M. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1148 | } |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1153 | MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1154 | #ifdef DEBUG |
| 1155 | // Do not attempt to flatten in debug mode when allocation is not |
| 1156 | // allowed. This is to avoid an assertion failure when allocating. |
| 1157 | // Flattening strings is the only case where we always allow |
| 1158 | // allocation because no GC is performed if the allocation fails. |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 1159 | if (!AllowHeapAllocation::IsAllowed()) return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | #endif |
| 1161 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1162 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1163 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | case kConsStringTag: { |
| 1165 | ConsString* cs = ConsString::cast(this); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1166 | if (cs->second()->length() == 0) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1167 | return cs->first(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | } |
| 1169 | // There's little point in putting the flat string in new space if the |
| 1170 | // cons string is in old space. It can never get GCed until there is |
| 1171 | // an old space GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1172 | PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1173 | int len = length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1174 | Object* object; |
| 1175 | String* result; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1176 | if (IsOneByteRepresentation()) { |
| 1177 | { MaybeObject* maybe_object = |
| 1178 | heap->AllocateRawOneByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1179 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1180 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1181 | result = String::cast(object); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1182 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1183 | int first_length = first->length(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 1184 | uint8_t* dest = SeqOneByteString::cast(result)->GetChars(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1185 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1186 | String* second = cs->second(); |
| 1187 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1188 | dest + first_length, |
| 1189 | 0, |
| 1190 | len - first_length); |
| 1191 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1192 | { MaybeObject* maybe_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1193 | heap->AllocateRawTwoByteString(len, tenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1194 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 1195 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1196 | result = String::cast(object); |
| 1197 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1198 | String* first = cs->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1199 | int first_length = first->length(); |
| 1200 | WriteToFlat(first, dest, 0, first_length); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1201 | String* second = cs->second(); |
| 1202 | WriteToFlat(second, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1203 | dest + first_length, |
| 1204 | 0, |
| 1205 | len - first_length); |
| 1206 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1207 | cs->set_first(result); |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 1208 | cs->set_second(heap->empty_string(), SKIP_WRITE_BARRIER); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1209 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | } |
| 1211 | default: |
| 1212 | return this; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1217 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1218 | // Externalizing twice leaks the external resource, so it's |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1219 | // prohibited by the API. |
| 1220 | ASSERT(!this->IsExternalString()); |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 1221 | #ifdef ENABLE_SLOW_ASSERTS |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1222 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1223 | // Assert that the resource and the string are equivalent. |
| 1224 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1225 | ScopedVector<uc16> smart_chars(this->length()); |
| 1226 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1227 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1228 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1229 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1230 | } |
| 1231 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1232 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1233 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1234 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1237 | bool is_ascii = this->IsOneByteRepresentation(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1238 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1239 | |
| 1240 | // Morph the object to an external string by adjusting the map and |
| 1241 | // reinitializing the fields. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1242 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1243 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1244 | is_internalized |
| 1245 | ? (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1246 | ? heap->external_internalized_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1247 | : heap->external_internalized_string_map()) |
| 1248 | : (is_ascii |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1249 | ? heap->external_string_with_one_byte_data_map() |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1250 | : heap->external_string_map())); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1251 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1252 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1253 | is_internalized |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1254 | ? (is_ascii |
| 1255 | ? heap-> |
| 1256 | short_external_internalized_string_with_one_byte_data_map() |
| 1257 | : heap->short_external_internalized_string_map()) |
| 1258 | : (is_ascii |
| 1259 | ? heap->short_external_string_with_one_byte_data_map() |
| 1260 | : heap->short_external_string_map())); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1261 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1262 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 1263 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1264 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1265 | |
| 1266 | // Fill the remainder of the string with dead wood. |
| 1267 | 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] | 1268 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1269 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1270 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1271 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1272 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 1278 | #ifdef ENABLE_SLOW_ASSERTS |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1279 | if (FLAG_enable_slow_asserts) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1280 | // Assert that the resource and the string are equivalent. |
| 1281 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 1282 | if (this->IsTwoByteRepresentation()) { |
| 1283 | ScopedVector<uint16_t> smart_chars(this->length()); |
| 1284 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1285 | ASSERT(String::IsOneByte(smart_chars.start(), this->length())); |
| 1286 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1287 | ScopedVector<char> smart_chars(this->length()); |
| 1288 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 1289 | ASSERT(memcmp(smart_chars.start(), |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1290 | resource->data(), |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1291 | resource->length() * sizeof(smart_chars[0])) == 0); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1292 | } |
| 1293 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1294 | Heap* heap = GetHeap(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1295 | int size = this->Size(); // Byte size of the original string. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1296 | if (size < ExternalString::kShortSize) { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1297 | return false; |
| 1298 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1299 | bool is_internalized = this->IsInternalizedString(); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1300 | |
| 1301 | // 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] | 1302 | // reinitializing the fields. Use short version if space is limited. |
| 1303 | if (size >= ExternalString::kSize) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1304 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1305 | is_internalized ? heap->external_ascii_internalized_string_map() |
| 1306 | : heap->external_ascii_string_map()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1307 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1308 | this->set_map_no_write_barrier( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1309 | is_internalized ? heap->short_external_ascii_internalized_string_map() |
| 1310 | : heap->short_external_ascii_string_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1311 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1312 | ExternalAsciiString* self = ExternalAsciiString::cast(this); |
| 1313 | self->set_resource(resource); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1314 | if (is_internalized) self->Hash(); // Force regeneration of the hash value. |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1315 | |
| 1316 | // Fill the remainder of the string with dead wood. |
| 1317 | 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] | 1318 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1319 | if (Marking::IsBlack(Marking::MarkBitFrom(this))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1320 | MemoryChunk::IncrementLiveBytesFromMutator(this->address(), |
| 1321 | new_size - size); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1322 | } |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1327 | void String::StringShortPrint(StringStream* accumulator) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1328 | int len = length(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1329 | if (len > kMaxShortPrintLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1330 | accumulator->Add("<Very long string[%u]>", len); |
| 1331 | return; |
| 1332 | } |
| 1333 | |
| 1334 | if (!LooksValid()) { |
| 1335 | accumulator->Add("<Invalid String>"); |
| 1336 | return; |
| 1337 | } |
| 1338 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1339 | ConsStringIteratorOp op; |
| 1340 | StringCharacterStream stream(this, &op); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1341 | |
| 1342 | bool truncated = false; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1343 | if (len > kMaxShortPrintLength) { |
| 1344 | len = kMaxShortPrintLength; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | truncated = true; |
| 1346 | } |
| 1347 | bool ascii = true; |
| 1348 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1349 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | |
| 1351 | if (c < 32 || c >= 127) { |
| 1352 | ascii = false; |
| 1353 | } |
| 1354 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1355 | stream.Reset(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1356 | if (ascii) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1357 | accumulator->Add("<String[%u]: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1359 | accumulator->Put(static_cast<char>(stream.GetNext())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1360 | } |
| 1361 | accumulator->Put('>'); |
| 1362 | } else { |
| 1363 | // Backslash indicates that the string contains control |
| 1364 | // characters and that backslashes are therefore escaped. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1365 | accumulator->Add("<String[%u]\\: ", length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1366 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1367 | uint16_t c = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1368 | if (c == '\n') { |
| 1369 | accumulator->Add("\\n"); |
| 1370 | } else if (c == '\r') { |
| 1371 | accumulator->Add("\\r"); |
| 1372 | } else if (c == '\\') { |
| 1373 | accumulator->Add("\\\\"); |
| 1374 | } else if (c < 32 || c > 126) { |
| 1375 | accumulator->Add("\\x%02x", c); |
| 1376 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 1377 | accumulator->Put(static_cast<char>(c)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | if (truncated) { |
| 1381 | accumulator->Put('.'); |
| 1382 | accumulator->Put('.'); |
| 1383 | accumulator->Put('.'); |
| 1384 | } |
| 1385 | accumulator->Put('>'); |
| 1386 | } |
| 1387 | return; |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 1392 | switch (map()->instance_type()) { |
| 1393 | case JS_ARRAY_TYPE: { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 1394 | double length = JSArray::cast(this)->length()->IsUndefined() |
| 1395 | ? 0 |
| 1396 | : JSArray::cast(this)->length()->Number(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1397 | accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1400 | case JS_WEAK_MAP_TYPE: { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1401 | accumulator->Add("<JS WeakMap>"); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1402 | break; |
| 1403 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1404 | case JS_WEAK_SET_TYPE: { |
| 1405 | accumulator->Add("<JS WeakSet>"); |
| 1406 | break; |
| 1407 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1408 | case JS_REGEXP_TYPE: { |
| 1409 | accumulator->Add("<JS RegExp>"); |
| 1410 | break; |
| 1411 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1412 | case JS_FUNCTION_TYPE: { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1413 | JSFunction* function = JSFunction::cast(this); |
| 1414 | Object* fun_name = function->shared()->DebugName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1415 | bool printed = false; |
| 1416 | if (fun_name->IsString()) { |
| 1417 | String* str = String::cast(fun_name); |
| 1418 | if (str->length() > 0) { |
| 1419 | accumulator->Add("<JS Function "); |
| 1420 | accumulator->Put(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1421 | printed = true; |
| 1422 | } |
| 1423 | } |
| 1424 | if (!printed) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1425 | accumulator->Add("<JS Function"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1426 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1427 | accumulator->Add(" (SharedFunctionInfo %p)", |
| 1428 | reinterpret_cast<void*>(function->shared())); |
| 1429 | accumulator->Put('>'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1430 | break; |
| 1431 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1432 | case JS_GENERATOR_OBJECT_TYPE: { |
| 1433 | accumulator->Add("<JS Generator>"); |
| 1434 | break; |
| 1435 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1436 | case JS_MODULE_TYPE: { |
| 1437 | accumulator->Add("<JS Module>"); |
| 1438 | break; |
| 1439 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1440 | // All other JSObjects are rather similar to each other (JSObject, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1441 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1442 | default: { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1443 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1444 | Heap* heap = GetHeap(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1445 | Object* constructor = map_of_this->constructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1446 | bool printed = false; |
| 1447 | if (constructor->IsHeapObject() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1448 | !heap->Contains(HeapObject::cast(constructor))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 1450 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1451 | bool global_object = IsJSGlobalProxy(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1452 | if (constructor->IsJSFunction()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1453 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1454 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 1455 | } else { |
| 1456 | Object* constructor_name = |
| 1457 | JSFunction::cast(constructor)->shared()->name(); |
| 1458 | if (constructor_name->IsString()) { |
| 1459 | String* str = String::cast(constructor_name); |
| 1460 | if (str->length() > 0) { |
| 1461 | bool vowel = AnWord(str); |
| 1462 | accumulator->Add("<%sa%s ", |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1463 | global_object ? "Global Object: " : "", |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | vowel ? "n" : ""); |
| 1465 | accumulator->Put(str); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1466 | accumulator->Add(" with %smap %p", |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1467 | map_of_this->is_deprecated() ? "deprecated " : "", |
| 1468 | map_of_this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | printed = true; |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | if (!printed) { |
| 1475 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 1476 | } |
| 1477 | } |
| 1478 | if (IsJSValue()) { |
| 1479 | accumulator->Add(" value = "); |
| 1480 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 1481 | } |
| 1482 | accumulator->Put('>'); |
| 1483 | break; |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1489 | void JSObject::PrintElementsTransition( |
| 1490 | FILE* file, ElementsKind from_kind, FixedArrayBase* from_elements, |
| 1491 | ElementsKind to_kind, FixedArrayBase* to_elements) { |
| 1492 | if (from_kind != to_kind) { |
| 1493 | PrintF(file, "elements transition ["); |
| 1494 | PrintElementsKind(file, from_kind); |
| 1495 | PrintF(file, " -> "); |
| 1496 | PrintElementsKind(file, to_kind); |
| 1497 | PrintF(file, "] in "); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 1498 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1499 | PrintF(file, " for "); |
| 1500 | ShortPrint(file); |
| 1501 | PrintF(file, " from "); |
| 1502 | from_elements->ShortPrint(file); |
| 1503 | PrintF(file, " to "); |
| 1504 | to_elements->ShortPrint(file); |
| 1505 | PrintF(file, "\n"); |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1510 | void Map::PrintGeneralization(FILE* file, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1511 | const char* reason, |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1512 | int modify_index, |
| 1513 | int split, |
| 1514 | int descriptors, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1515 | bool constant_to_field, |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1516 | Representation old_representation, |
| 1517 | Representation new_representation) { |
| 1518 | PrintF(file, "[generalizing "); |
| 1519 | constructor_name()->PrintOn(file); |
| 1520 | PrintF(file, "] "); |
| 1521 | String::cast(instance_descriptors()->GetKey(modify_index))->PrintOn(file); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 1522 | if (constant_to_field) { |
| 1523 | PrintF(file, ":c->f"); |
| 1524 | } else { |
| 1525 | PrintF(file, ":%s->%s", |
| 1526 | old_representation.Mnemonic(), |
| 1527 | new_representation.Mnemonic()); |
| 1528 | } |
| 1529 | PrintF(file, " ("); |
| 1530 | if (strlen(reason) > 0) { |
| 1531 | PrintF(file, "%s", reason); |
| 1532 | } else { |
| 1533 | PrintF(file, "+%i maps", descriptors - split); |
| 1534 | } |
| 1535 | PrintF(file, ") ["); |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1536 | JavaScriptFrame::PrintTop(GetIsolate(), file, false, true); |
| 1537 | PrintF(file, "]\n"); |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | void JSObject::PrintInstanceMigration(FILE* file, |
| 1542 | Map* original_map, |
| 1543 | Map* new_map) { |
| 1544 | PrintF(file, "[migrating "); |
| 1545 | map()->constructor_name()->PrintOn(file); |
| 1546 | PrintF(file, "] "); |
| 1547 | DescriptorArray* o = original_map->instance_descriptors(); |
| 1548 | DescriptorArray* n = new_map->instance_descriptors(); |
| 1549 | for (int i = 0; i < original_map->NumberOfOwnDescriptors(); i++) { |
| 1550 | Representation o_r = o->GetDetails(i).representation(); |
| 1551 | Representation n_r = n->GetDetails(i).representation(); |
| 1552 | if (!o_r.Equals(n_r)) { |
| 1553 | String::cast(o->GetKey(i))->PrintOn(file); |
| 1554 | PrintF(file, ":%s->%s ", o_r.Mnemonic(), n_r.Mnemonic()); |
| 1555 | } else if (o->GetDetails(i).type() == CONSTANT && |
| 1556 | n->GetDetails(i).type() == FIELD) { |
| 1557 | Name* name = o->GetKey(i); |
| 1558 | if (name->IsString()) { |
| 1559 | String::cast(name)->PrintOn(file); |
| 1560 | } else { |
| 1561 | PrintF(file, "???"); |
| 1562 | } |
| 1563 | PrintF(file, " "); |
| 1564 | } |
| 1565 | } |
| 1566 | PrintF(file, "\n"); |
| 1567 | } |
| 1568 | |
| 1569 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1570 | void HeapObject::HeapObjectShortPrint(StringStream* accumulator) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1571 | Heap* heap = GetHeap(); |
| 1572 | if (!heap->Contains(this)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1573 | accumulator->Add("!!!INVALID POINTER!!!"); |
| 1574 | return; |
| 1575 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1576 | if (!heap->Contains(map())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | accumulator->Add("!!!INVALID MAP!!!"); |
| 1578 | return; |
| 1579 | } |
| 1580 | |
| 1581 | accumulator->Add("%p ", this); |
| 1582 | |
| 1583 | if (IsString()) { |
| 1584 | String::cast(this)->StringShortPrint(accumulator); |
| 1585 | return; |
| 1586 | } |
| 1587 | if (IsJSObject()) { |
| 1588 | JSObject::cast(this)->JSObjectShortPrint(accumulator); |
| 1589 | return; |
| 1590 | } |
| 1591 | switch (map()->instance_type()) { |
| 1592 | case MAP_TYPE: |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1593 | accumulator->Add("<Map(elements=%u)>", Map::cast(this)->elements_kind()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | case FIXED_ARRAY_TYPE: |
| 1596 | accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length()); |
| 1597 | break; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1598 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1599 | accumulator->Add("<FixedDoubleArray[%u]>", |
| 1600 | FixedDoubleArray::cast(this)->length()); |
| 1601 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1602 | case BYTE_ARRAY_TYPE: |
| 1603 | accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length()); |
| 1604 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1605 | case FREE_SPACE_TYPE: |
| 1606 | accumulator->Add("<FreeSpace[%u]>", FreeSpace::cast(this)->Size()); |
| 1607 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1608 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 1609 | accumulator->Add("<ExternalPixelArray[%u]>", |
| 1610 | ExternalPixelArray::cast(this)->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1611 | break; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1612 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1613 | accumulator->Add("<ExternalByteArray[%u]>", |
| 1614 | ExternalByteArray::cast(this)->length()); |
| 1615 | break; |
| 1616 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1617 | accumulator->Add("<ExternalUnsignedByteArray[%u]>", |
| 1618 | ExternalUnsignedByteArray::cast(this)->length()); |
| 1619 | break; |
| 1620 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1621 | accumulator->Add("<ExternalShortArray[%u]>", |
| 1622 | ExternalShortArray::cast(this)->length()); |
| 1623 | break; |
| 1624 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1625 | accumulator->Add("<ExternalUnsignedShortArray[%u]>", |
| 1626 | ExternalUnsignedShortArray::cast(this)->length()); |
| 1627 | break; |
| 1628 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1629 | accumulator->Add("<ExternalIntArray[%u]>", |
| 1630 | ExternalIntArray::cast(this)->length()); |
| 1631 | break; |
| 1632 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1633 | accumulator->Add("<ExternalUnsignedIntArray[%u]>", |
| 1634 | ExternalUnsignedIntArray::cast(this)->length()); |
| 1635 | break; |
| 1636 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 1637 | accumulator->Add("<ExternalFloatArray[%u]>", |
| 1638 | ExternalFloatArray::cast(this)->length()); |
| 1639 | break; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1640 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 1641 | accumulator->Add("<ExternalDoubleArray[%u]>", |
| 1642 | ExternalDoubleArray::cast(this)->length()); |
| 1643 | break; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1644 | case SHARED_FUNCTION_INFO_TYPE: { |
| 1645 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(this); |
| 1646 | SmartArrayPointer<char> debug_name = |
| 1647 | shared->DebugName()->ToCString(); |
| 1648 | if (debug_name[0] != 0) { |
| 1649 | accumulator->Add("<SharedFunctionInfo %s>", *debug_name); |
| 1650 | } else { |
| 1651 | accumulator->Add("<SharedFunctionInfo>"); |
| 1652 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1653 | break; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1654 | } |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1655 | case JS_MESSAGE_OBJECT_TYPE: |
| 1656 | accumulator->Add("<JSMessageObject>"); |
| 1657 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1658 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1659 | case NAME##_TYPE: \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1660 | accumulator->Put('<'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1661 | accumulator->Add(#Name); \ |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1662 | accumulator->Put('>'); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1663 | break; |
| 1664 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1665 | #undef MAKE_STRUCT_CASE |
| 1666 | case CODE_TYPE: |
| 1667 | accumulator->Add("<Code>"); |
| 1668 | break; |
| 1669 | case ODDBALL_TYPE: { |
| 1670 | if (IsUndefined()) |
| 1671 | accumulator->Add("<undefined>"); |
| 1672 | else if (IsTheHole()) |
| 1673 | accumulator->Add("<the hole>"); |
| 1674 | else if (IsNull()) |
| 1675 | accumulator->Add("<null>"); |
| 1676 | else if (IsTrue()) |
| 1677 | accumulator->Add("<true>"); |
| 1678 | else if (IsFalse()) |
| 1679 | accumulator->Add("<false>"); |
| 1680 | else |
| 1681 | accumulator->Add("<Odd Oddball>"); |
| 1682 | break; |
| 1683 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1684 | case SYMBOL_TYPE: { |
| 1685 | Symbol* symbol = Symbol::cast(this); |
| 1686 | accumulator->Add("<Symbol: %d", symbol->Hash()); |
| 1687 | if (!symbol->name()->IsUndefined()) { |
| 1688 | accumulator->Add(" "); |
| 1689 | String::cast(symbol->name())->StringShortPrint(accumulator); |
| 1690 | } |
| 1691 | accumulator->Add(">"); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1692 | break; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1693 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1694 | case HEAP_NUMBER_TYPE: |
| 1695 | accumulator->Add("<Number: "); |
| 1696 | HeapNumber::cast(this)->HeapNumberPrint(accumulator); |
| 1697 | accumulator->Put('>'); |
| 1698 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1699 | case JS_PROXY_TYPE: |
| 1700 | accumulator->Add("<JSProxy>"); |
| 1701 | break; |
| 1702 | case JS_FUNCTION_PROXY_TYPE: |
| 1703 | accumulator->Add("<JSFunctionProxy>"); |
| 1704 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1705 | case FOREIGN_TYPE: |
| 1706 | accumulator->Add("<Foreign>"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1707 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1708 | case CELL_TYPE: |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1709 | accumulator->Add("Cell for "); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1710 | Cell::cast(this)->value()->ShortPrint(accumulator); |
| 1711 | break; |
| 1712 | case PROPERTY_CELL_TYPE: |
| 1713 | accumulator->Add("PropertyCell for "); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1714 | PropertyCell::cast(this)->value()->ShortPrint(accumulator); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1715 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1716 | default: |
| 1717 | accumulator->Add("<Other heap object (%d)>", map()->instance_type()); |
| 1718 | break; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1723 | void HeapObject::Iterate(ObjectVisitor* v) { |
| 1724 | // Handle header |
| 1725 | IteratePointer(v, kMapOffset); |
| 1726 | // Handle object body |
| 1727 | Map* m = map(); |
| 1728 | IterateBody(m->instance_type(), SizeFromMap(m), v); |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 1733 | ObjectVisitor* v) { |
| 1734 | // Avoiding <Type>::cast(this) because it accesses the map pointer field. |
| 1735 | // During GC, the map pointer field is encoded. |
| 1736 | if (type < FIRST_NONSTRING_TYPE) { |
| 1737 | switch (type & kStringRepresentationMask) { |
| 1738 | case kSeqStringTag: |
| 1739 | break; |
| 1740 | case kConsStringTag: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1741 | ConsString::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1742 | break; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1743 | case kSlicedStringTag: |
| 1744 | SlicedString::BodyDescriptor::IterateBody(this, v); |
| 1745 | break; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1746 | case kExternalStringTag: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 1747 | if ((type & kStringEncodingMask) == kOneByteStringTag) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1748 | reinterpret_cast<ExternalAsciiString*>(this)-> |
| 1749 | ExternalAsciiStringIterateBody(v); |
| 1750 | } else { |
| 1751 | reinterpret_cast<ExternalTwoByteString*>(this)-> |
| 1752 | ExternalTwoByteStringIterateBody(v); |
| 1753 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1754 | break; |
| 1755 | } |
| 1756 | return; |
| 1757 | } |
| 1758 | |
| 1759 | switch (type) { |
| 1760 | case FIXED_ARRAY_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1761 | FixedArray::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | break; |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1763 | case CONSTANT_POOL_ARRAY_TYPE: |
| 1764 | reinterpret_cast<ConstantPoolArray*>(this)->ConstantPoolIterateBody(v); |
| 1765 | break; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1766 | case FIXED_DOUBLE_ARRAY_TYPE: |
| 1767 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1768 | case JS_OBJECT_TYPE: |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1769 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1770 | case JS_GENERATOR_OBJECT_TYPE: |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1771 | case JS_MODULE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1772 | case JS_VALUE_TYPE: |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1773 | case JS_DATE_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1774 | case JS_ARRAY_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1775 | case JS_ARRAY_BUFFER_TYPE: |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1776 | case JS_TYPED_ARRAY_TYPE: |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1777 | case JS_DATA_VIEW_TYPE: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1778 | case JS_SET_TYPE: |
| 1779 | case JS_MAP_TYPE: |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1780 | case JS_WEAK_MAP_TYPE: |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1781 | case JS_WEAK_SET_TYPE: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1782 | case JS_REGEXP_TYPE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1783 | case JS_GLOBAL_PROXY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | case JS_GLOBAL_OBJECT_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1785 | case JS_BUILTINS_OBJECT_TYPE: |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1786 | case JS_MESSAGE_OBJECT_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1787 | JSObject::BodyDescriptor::IterateBody(this, object_size, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1788 | break; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1789 | case JS_FUNCTION_TYPE: |
| 1790 | reinterpret_cast<JSFunction*>(this) |
| 1791 | ->JSFunctionIterateBody(object_size, v); |
| 1792 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1793 | case ODDBALL_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1794 | Oddball::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1795 | break; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1796 | case JS_PROXY_TYPE: |
| 1797 | JSProxy::BodyDescriptor::IterateBody(this, v); |
| 1798 | break; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 1799 | case JS_FUNCTION_PROXY_TYPE: |
| 1800 | JSFunctionProxy::BodyDescriptor::IterateBody(this, v); |
| 1801 | break; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1802 | case FOREIGN_TYPE: |
| 1803 | reinterpret_cast<Foreign*>(this)->ForeignIterateBody(v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1804 | break; |
| 1805 | case MAP_TYPE: |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1806 | Map::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | case CODE_TYPE: |
| 1809 | reinterpret_cast<Code*>(this)->CodeIterateBody(v); |
| 1810 | break; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1811 | case CELL_TYPE: |
| 1812 | Cell::BodyDescriptor::IterateBody(this, v); |
| 1813 | break; |
| 1814 | case PROPERTY_CELL_TYPE: |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1815 | PropertyCell::BodyDescriptor::IterateBody(this, v); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1816 | break; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1817 | case SYMBOL_TYPE: |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 1818 | Symbol::BodyDescriptor::IterateBody(this, v); |
| 1819 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1820 | case HEAP_NUMBER_TYPE: |
| 1821 | case FILLER_TYPE: |
| 1822 | case BYTE_ARRAY_TYPE: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1823 | case FREE_SPACE_TYPE: |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1824 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1825 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1826 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1827 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1828 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1829 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1830 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1831 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 1832 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1833 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1834 | case SHARED_FUNCTION_INFO_TYPE: { |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 1835 | SharedFunctionInfo::BodyDescriptor::IterateBody(this, v); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1836 | break; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1837 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1838 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1839 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1840 | case NAME##_TYPE: |
| 1841 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1842 | #undef MAKE_STRUCT_CASE |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1843 | if (type == ALLOCATION_SITE_TYPE) { |
| 1844 | AllocationSite::BodyDescriptor::IterateBody(this, v); |
| 1845 | } else { |
| 1846 | StructBodyDescriptor::IterateBody(this, object_size, v); |
| 1847 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | default: |
| 1850 | PrintF("Unknown type: %d\n", type); |
| 1851 | UNREACHABLE(); |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1856 | bool HeapNumber::HeapNumberBooleanValue() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1857 | // NaN, +0, and -0 should return the false object |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1858 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1859 | union IeeeDoubleLittleEndianArchType u; |
| 1860 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 1861 | union IeeeDoubleBigEndianArchType u; |
| 1862 | #endif |
| 1863 | u.d = value(); |
| 1864 | if (u.bits.exp == 2047) { |
| 1865 | // Detect NaN for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1866 | 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] | 1867 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1868 | if (u.bits.exp == 0) { |
| 1869 | // Detect +0, and -0 for IEEE double precision floating point. |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1870 | 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] | 1871 | } |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 1872 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1876 | void HeapNumber::HeapNumberPrint(FILE* out) { |
| 1877 | PrintF(out, "%.16g", Number()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | |
| 1881 | void HeapNumber::HeapNumberPrint(StringStream* accumulator) { |
| 1882 | // The Windows version of vsnprintf can allocate when printing a %g string |
| 1883 | // into a buffer that may not be big enough. We don't want random memory |
| 1884 | // allocation when producing post-crash stack traces, so we print into a |
| 1885 | // buffer that is plenty big enough for any floating point number, then |
| 1886 | // print that using vsnprintf (which may truncate but never allocate if |
| 1887 | // there is no more space in the buffer). |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1888 | EmbeddedVector<char, 100> buffer; |
| 1889 | OS::SNPrintF(buffer, "%.16g", Number()); |
| 1890 | accumulator->Add("%s", buffer.start()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1894 | String* JSReceiver::class_name() { |
| 1895 | if (IsJSFunction() && IsJSFunctionProxy()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1896 | return GetHeap()->function_class_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1897 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1898 | if (map()->constructor()->IsJSFunction()) { |
| 1899 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1900 | return String::cast(constructor->shared()->instance_class_name()); |
| 1901 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1902 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1903 | return GetHeap()->Object_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1907 | String* Map::constructor_name() { |
| 1908 | if (constructor()->IsJSFunction()) { |
| 1909 | JSFunction* constructor = JSFunction::cast(this->constructor()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1910 | String* name = String::cast(constructor->shared()->name()); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1911 | if (name->length() > 0) return name; |
| 1912 | String* inferred_name = constructor->shared()->inferred_name(); |
| 1913 | if (inferred_name->length() > 0) return inferred_name; |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1914 | Object* proto = prototype(); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1915 | if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1916 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1917 | // TODO(rossberg): what about proxies? |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1918 | // If the constructor is not present, return "Object". |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1919 | return GetHeap()->Object_string(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 1923 | String* JSReceiver::constructor_name() { |
| 1924 | return map()->constructor_name(); |
| 1925 | } |
| 1926 | |
| 1927 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1928 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 1929 | static Handle<Object> NewStorageFor(Isolate* isolate, |
| 1930 | Handle<Object> object, |
| 1931 | Representation representation) { |
| 1932 | Heap* heap = isolate->heap(); |
| 1933 | CALL_HEAP_FUNCTION(isolate, |
| 1934 | object->AllocateNewStorageFor(heap, representation), |
| 1935 | Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1939 | void JSObject::AddFastPropertyUsingMap(Handle<JSObject> object, |
| 1940 | Handle<Map> new_map, |
| 1941 | Handle<Name> name, |
| 1942 | Handle<Object> value, |
| 1943 | int field_index, |
| 1944 | Representation representation) { |
| 1945 | Isolate* isolate = object->GetIsolate(); |
| 1946 | |
| 1947 | // This method is used to transition to a field. If we are transitioning to a |
| 1948 | // double field, allocate new storage. |
| 1949 | Handle<Object> storage = NewStorageFor(isolate, value, representation); |
| 1950 | |
| 1951 | if (object->map()->unused_property_fields() == 0) { |
| 1952 | int new_unused = new_map->unused_property_fields(); |
| 1953 | Handle<FixedArray> properties(object->properties()); |
| 1954 | Handle<FixedArray> values = isolate->factory()->CopySizeFixedArray( |
| 1955 | properties, properties->length() + new_unused + 1); |
| 1956 | object->set_properties(*values); |
| 1957 | } |
| 1958 | |
| 1959 | object->set_map(*new_map); |
| 1960 | object->FastPropertyAtPut(field_index, *storage); |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | static MaybeObject* CopyAddFieldDescriptor(Map* map, |
| 1965 | Name* name, |
| 1966 | int index, |
| 1967 | PropertyAttributes attributes, |
| 1968 | Representation representation, |
| 1969 | TransitionFlag flag) { |
| 1970 | Map* new_map; |
| 1971 | FieldDescriptor new_field_desc(name, index, attributes, representation); |
| 1972 | MaybeObject* maybe_map = map->CopyAddDescriptor(&new_field_desc, flag); |
| 1973 | if (!maybe_map->To(&new_map)) return maybe_map; |
| 1974 | int unused_property_fields = map->unused_property_fields() - 1; |
| 1975 | if (unused_property_fields < 0) { |
| 1976 | unused_property_fields += JSObject::kFieldsAdded; |
| 1977 | } |
| 1978 | new_map->set_unused_property_fields(unused_property_fields); |
| 1979 | return new_map; |
| 1980 | } |
| 1981 | |
| 1982 | |
| 1983 | static Handle<Map> CopyAddFieldDescriptor(Handle<Map> map, |
| 1984 | Handle<Name> name, |
| 1985 | int index, |
| 1986 | PropertyAttributes attributes, |
| 1987 | Representation representation, |
| 1988 | TransitionFlag flag) { |
| 1989 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 1990 | CopyAddFieldDescriptor( |
| 1991 | *map, *name, index, attributes, representation, flag), |
| 1992 | Map); |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | void JSObject::AddFastProperty(Handle<JSObject> object, |
| 1997 | Handle<Name> name, |
| 1998 | Handle<Object> value, |
| 1999 | PropertyAttributes attributes, |
| 2000 | StoreFromKeyed store_mode, |
| 2001 | ValueType value_type, |
| 2002 | TransitionFlag flag) { |
| 2003 | ASSERT(!object->IsJSGlobalProxy()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 2004 | ASSERT(DescriptorArray::kNotFound == |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2005 | object->map()->instance_descriptors()->Search( |
| 2006 | *name, object->map()->NumberOfOwnDescriptors())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 2007 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2008 | // 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] | 2009 | // hidden strings) and is not a real identifier. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 2010 | // Normalize the object if it will have too many fast properties. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2011 | Isolate* isolate = object->GetIsolate(); |
| 2012 | if (!name->IsCacheable(isolate) || |
| 2013 | object->TooManyFastProperties(store_mode)) { |
| 2014 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
| 2015 | AddSlowProperty(object, name, value, attributes); |
| 2016 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | // Compute the new index for new field. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2020 | int index = object->map()->NextFreePropertyIndex(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | |
| 2022 | // Allocate new instance descriptors with (name, index) added |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2023 | if (object->IsJSContextExtensionObject()) value_type = FORCE_TAGGED; |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2024 | Representation representation = value->OptimalRepresentation(value_type); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2025 | Handle<Map> new_map = CopyAddFieldDescriptor( |
| 2026 | handle(object->map()), name, index, attributes, representation, flag); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2027 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2028 | AddFastPropertyUsingMap(object, new_map, name, value, index, representation); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2032 | static MaybeObject* CopyAddConstantDescriptor(Map* map, |
| 2033 | Name* name, |
| 2034 | Object* value, |
| 2035 | PropertyAttributes attributes, |
| 2036 | TransitionFlag flag) { |
| 2037 | ConstantDescriptor new_constant_desc(name, value, attributes); |
| 2038 | return map->CopyAddDescriptor(&new_constant_desc, flag); |
| 2039 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 2040 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2041 | |
| 2042 | static Handle<Map> CopyAddConstantDescriptor(Handle<Map> map, |
| 2043 | Handle<Name> name, |
| 2044 | Handle<Object> value, |
| 2045 | PropertyAttributes attributes, |
| 2046 | TransitionFlag flag) { |
| 2047 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 2048 | CopyAddConstantDescriptor( |
| 2049 | *map, *name, *value, attributes, flag), |
| 2050 | Map); |
| 2051 | } |
| 2052 | |
| 2053 | |
| 2054 | void JSObject::AddConstantProperty(Handle<JSObject> object, |
| 2055 | Handle<Name> name, |
| 2056 | Handle<Object> constant, |
| 2057 | PropertyAttributes attributes, |
| 2058 | TransitionFlag initial_flag) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2059 | TransitionFlag flag = |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2060 | // Do not add transitions to global objects. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2061 | (object->IsGlobalObject() || |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2062 | // Don't add transitions to special properties with non-trivial |
| 2063 | // attributes. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2064 | attributes != NONE) |
| 2065 | ? OMIT_TRANSITION |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2066 | : initial_flag; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2067 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2068 | // Allocate new instance descriptors with (name, constant) added. |
| 2069 | Handle<Map> new_map = CopyAddConstantDescriptor( |
| 2070 | handle(object->map()), name, constant, attributes, flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2071 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2072 | object->set_map(*new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2076 | void JSObject::AddSlowProperty(Handle<JSObject> object, |
| 2077 | Handle<Name> name, |
| 2078 | Handle<Object> value, |
| 2079 | PropertyAttributes attributes) { |
| 2080 | ASSERT(!object->HasFastProperties()); |
| 2081 | Isolate* isolate = object->GetIsolate(); |
| 2082 | Handle<NameDictionary> dict(object->property_dictionary()); |
| 2083 | if (object->IsGlobalObject()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2084 | // In case name is an orphaned property reuse the cell. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2085 | int entry = dict->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2086 | if (entry != NameDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2087 | Handle<PropertyCell> cell(PropertyCell::cast(dict->ValueAt(entry))); |
| 2088 | PropertyCell::SetValueInferType(cell, value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2089 | // Assign an enumeration index to the property and update |
| 2090 | // SetNextEnumerationIndex. |
| 2091 | int index = dict->NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2092 | PropertyDetails details = PropertyDetails(attributes, NORMAL, index); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2093 | dict->SetNextEnumerationIndex(index + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2094 | dict->SetEntry(entry, *name, *cell, details); |
| 2095 | return; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2096 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2097 | Handle<PropertyCell> cell = isolate->factory()->NewPropertyCell(value); |
| 2098 | PropertyCell::SetValueInferType(cell, value); |
| 2099 | value = cell; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2100 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2101 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2102 | Handle<NameDictionary> result = NameDictionaryAdd(dict, name, value, details); |
| 2103 | if (*dict != *result) object->set_properties(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2107 | Handle<Object> JSObject::AddProperty(Handle<JSObject> object, |
| 2108 | Handle<Name> name, |
| 2109 | Handle<Object> value, |
| 2110 | PropertyAttributes attributes, |
| 2111 | StrictModeFlag strict_mode, |
| 2112 | JSReceiver::StoreFromKeyed store_mode, |
| 2113 | ExtensibilityCheck extensibility_check, |
| 2114 | ValueType value_type, |
| 2115 | StoreMode mode, |
| 2116 | TransitionFlag transition_flag) { |
| 2117 | ASSERT(!object->IsJSGlobalProxy()); |
| 2118 | Isolate* isolate = object->GetIsolate(); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 2119 | |
| 2120 | if (!name->IsUniqueName()) { |
| 2121 | name = isolate->factory()->InternalizedStringFromString( |
| 2122 | Handle<String>::cast(name)); |
| 2123 | } |
| 2124 | |
mstarzinger@chromium.org | ccab367 | 2012-06-14 15:01:16 +0000 | [diff] [blame] | 2125 | if (extensibility_check == PERFORM_EXTENSIBILITY_CHECK && |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2126 | !object->map()->is_extensible()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2127 | if (strict_mode == kNonStrictMode) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 2128 | return value; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2129 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2130 | Handle<Object> args[1] = { name }; |
| 2131 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 2132 | "object_not_extensible", HandleVector(args, ARRAY_SIZE(args))); |
| 2133 | isolate->Throw(*error); |
| 2134 | return Handle<Object>(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 2135 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2136 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2137 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2138 | if (object->HasFastProperties()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2139 | // Ensure the descriptor array does not get too big. |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 2140 | if (object->map()->NumberOfOwnDescriptors() <= kMaxNumberOfDescriptors) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2141 | // TODO(verwaest): Support other constants. |
| 2142 | // if (mode == ALLOW_AS_CONSTANT && |
| 2143 | // !value->IsTheHole() && |
| 2144 | // !value->IsConsString()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2145 | if (value->IsJSFunction()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2146 | AddConstantProperty(object, name, value, attributes, transition_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2148 | AddFastProperty(object, name, value, attributes, store_mode, |
| 2149 | value_type, transition_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } else { |
| 2152 | // Normalize the object to prevent very large instance descriptors. |
| 2153 | // This eliminates unwanted N^2 allocation and lookup behavior. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2154 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
| 2155 | AddSlowProperty(object, name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2157 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2158 | AddSlowProperty(object, name, value, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2159 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2160 | |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 2161 | if (FLAG_harmony_observation && |
| 2162 | object->map()->is_observed() && |
| 2163 | *name != isolate->heap()->hidden_string()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2164 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 2165 | EnqueueChangeRecord(object, "add", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2168 | return value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | void JSObject::EnqueueChangeRecord(Handle<JSObject> object, |
| 2173 | const char* type_str, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2174 | Handle<Name> name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2175 | Handle<Object> old_value) { |
| 2176 | Isolate* isolate = object->GetIsolate(); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 2177 | HandleScope scope(isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 2178 | Handle<String> type = isolate->factory()->InternalizeUtf8String(type_str); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 2179 | if (object->IsJSGlobalObject()) { |
| 2180 | object = handle(JSGlobalObject::cast(*object)->global_receiver(), isolate); |
| 2181 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2182 | Handle<Object> args[] = { type, object, name, old_value }; |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 2183 | int argc = name.is_null() ? 2 : old_value->IsTheHole() ? 3 : 4; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2184 | bool threw; |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 2185 | |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2186 | Execution::Call(isolate, |
| 2187 | Handle<JSFunction>(isolate->observers_notify_change()), |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 2188 | isolate->factory()->undefined_value(), |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 2189 | argc, args, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2190 | &threw); |
| 2191 | ASSERT(!threw); |
| 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | void JSObject::DeliverChangeRecords(Isolate* isolate) { |
| 2196 | ASSERT(isolate->observer_delivery_pending()); |
| 2197 | bool threw = false; |
| 2198 | Execution::Call( |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2199 | isolate, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 2200 | isolate->observers_deliver_changes(), |
| 2201 | isolate->factory()->undefined_value(), |
| 2202 | 0, |
| 2203 | NULL, |
| 2204 | &threw); |
| 2205 | ASSERT(!threw); |
| 2206 | isolate->set_observer_delivery_pending(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2210 | Handle<Object> JSObject::SetPropertyPostInterceptor( |
| 2211 | Handle<JSObject> object, |
| 2212 | Handle<Name> name, |
| 2213 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2214 | PropertyAttributes attributes, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2215 | StrictModeFlag strict_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2216 | // Check local property, ignore interceptor. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2217 | LookupResult result(object->GetIsolate()); |
| 2218 | object->LocalLookupRealNamedProperty(*name, &result); |
| 2219 | if (!result.IsFound()) { |
| 2220 | object->map()->LookupTransition(*object, *name, &result); |
| 2221 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2222 | if (result.IsFound()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2223 | // An existing property or a map transition was found. Use set property to |
| 2224 | // handle all these cases. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2225 | return SetPropertyForResult(object, &result, name, value, attributes, |
| 2226 | strict_mode, MAY_BE_STORE_FROM_KEYED); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2227 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2228 | bool done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2229 | Handle<Object> result_object = SetPropertyViaPrototypes( |
| 2230 | object, name, value, attributes, strict_mode, &done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2231 | if (done) return result_object; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2232 | // Add a new real property. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2233 | return AddProperty(object, name, value, attributes, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2237 | static void ReplaceSlowProperty(Handle<JSObject> object, |
| 2238 | Handle<Name> name, |
| 2239 | Handle<Object> value, |
| 2240 | PropertyAttributes attributes) { |
| 2241 | NameDictionary* dictionary = object->property_dictionary(); |
| 2242 | int old_index = dictionary->FindEntry(*name); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2243 | int new_enumeration_index = 0; // 0 means "Use the next available index." |
| 2244 | if (old_index != -1) { |
| 2245 | // All calls to ReplaceSlowProperty have had all transitions removed. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 2246 | new_enumeration_index = dictionary->DetailsAt(old_index).dictionary_index(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2247 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2248 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2249 | PropertyDetails new_details(attributes, NORMAL, new_enumeration_index); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2250 | JSObject::SetNormalizedProperty(object, name, value, new_details); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2253 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2254 | const char* Representation::Mnemonic() const { |
| 2255 | switch (kind_) { |
| 2256 | case kNone: return "v"; |
| 2257 | case kTagged: return "t"; |
| 2258 | case kSmi: return "s"; |
| 2259 | case kDouble: return "d"; |
| 2260 | case kInteger32: return "i"; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2261 | case kHeapObject: return "h"; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2262 | case kExternal: return "x"; |
| 2263 | default: |
| 2264 | UNREACHABLE(); |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | |
| 2270 | enum RightTrimMode { FROM_GC, FROM_MUTATOR }; |
| 2271 | |
| 2272 | |
| 2273 | static void ZapEndOfFixedArray(Address new_end, int to_trim) { |
| 2274 | // If we are doing a big trim in old space then we zap the space. |
| 2275 | Object** zap = reinterpret_cast<Object**>(new_end); |
| 2276 | zap++; // Header of filler must be at least one word so skip that. |
| 2277 | for (int i = 1; i < to_trim; i++) { |
| 2278 | *zap++ = Smi::FromInt(0); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | template<RightTrimMode trim_mode> |
| 2284 | static void RightTrimFixedArray(Heap* heap, FixedArray* elms, int to_trim) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 2285 | ASSERT(elms->map() != heap->fixed_cow_array_map()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2286 | // 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] | 2287 | ASSERT(!heap->lo_space()->Contains(elms)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2288 | |
| 2289 | const int len = elms->length(); |
| 2290 | |
| 2291 | ASSERT(to_trim < len); |
| 2292 | |
| 2293 | Address new_end = elms->address() + FixedArray::SizeFor(len - to_trim); |
| 2294 | |
| 2295 | if (trim_mode != FROM_GC || Heap::ShouldZapGarbage()) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2296 | ZapEndOfFixedArray(new_end, to_trim); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | int size_delta = to_trim * kPointerSize; |
| 2300 | |
| 2301 | // Technically in new space this write might be omitted (except for |
| 2302 | // debug mode which iterates through the heap), but to play safer |
| 2303 | // we still do it. |
| 2304 | heap->CreateFillerObjectAt(new_end, size_delta); |
| 2305 | |
| 2306 | elms->set_length(len - to_trim); |
| 2307 | |
| 2308 | // Maintain marking consistency for IncrementalMarking. |
| 2309 | if (Marking::IsBlack(Marking::MarkBitFrom(elms))) { |
| 2310 | if (trim_mode == FROM_GC) { |
| 2311 | MemoryChunk::IncrementLiveBytesFromGC(elms->address(), -size_delta); |
| 2312 | } else { |
| 2313 | MemoryChunk::IncrementLiveBytesFromMutator(elms->address(), -size_delta); |
| 2314 | } |
| 2315 | } |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 2316 | |
| 2317 | // The array may not be moved during GC, |
| 2318 | // and size has to be adjusted nevertheless. |
| 2319 | HeapProfiler* profiler = heap->isolate()->heap_profiler(); |
| 2320 | if (profiler->is_tracking_allocations()) { |
| 2321 | profiler->UpdateObjectSizeEvent(elms->address(), elms->Size()); |
| 2322 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2326 | bool Map::InstancesNeedRewriting(Map* target, |
| 2327 | int target_number_of_fields, |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2328 | int target_inobject, |
| 2329 | int target_unused) { |
| 2330 | // If fields were added (or removed), rewrite the instance. |
| 2331 | int number_of_fields = NumberOfFields(); |
| 2332 | ASSERT(target_number_of_fields >= number_of_fields); |
| 2333 | if (target_number_of_fields != number_of_fields) return true; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2334 | |
| 2335 | if (FLAG_track_double_fields) { |
| 2336 | // If smi descriptors were replaced by double descriptors, rewrite. |
| 2337 | DescriptorArray* old_desc = instance_descriptors(); |
| 2338 | DescriptorArray* new_desc = target->instance_descriptors(); |
| 2339 | int limit = NumberOfOwnDescriptors(); |
| 2340 | for (int i = 0; i < limit; i++) { |
| 2341 | if (new_desc->GetDetails(i).representation().IsDouble() && |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2342 | !old_desc->GetDetails(i).representation().IsDouble()) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2343 | return true; |
| 2344 | } |
| 2345 | } |
| 2346 | } |
| 2347 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2348 | // If no fields were added, and no inobject properties were removed, setting |
| 2349 | // the map is sufficient. |
| 2350 | if (target_inobject == inobject_properties()) return false; |
| 2351 | // In-object slack tracking may have reduced the object size of the new map. |
| 2352 | // In that case, succeed if all existing fields were inobject, and they still |
| 2353 | // fit within the new inobject size. |
| 2354 | ASSERT(target_inobject < inobject_properties()); |
| 2355 | if (target_number_of_fields <= target_inobject) { |
| 2356 | ASSERT(target_number_of_fields + target_unused == target_inobject); |
| 2357 | return false; |
| 2358 | } |
| 2359 | // Otherwise, properties will need to be moved to the backing store. |
| 2360 | return true; |
| 2361 | } |
| 2362 | |
| 2363 | |
| 2364 | // To migrate an instance to a map: |
| 2365 | // - First check whether the instance needs to be rewritten. If not, simply |
| 2366 | // change the map. |
| 2367 | // - Otherwise, allocate a fixed array large enough to hold all fields, in |
| 2368 | // addition to unused space. |
| 2369 | // - Copy all existing properties in, in the following order: backing store |
| 2370 | // properties, unused fields, inobject properties. |
| 2371 | // - If all allocation succeeded, commit the state atomically: |
| 2372 | // * Copy inobject properties from the backing store back into the object. |
| 2373 | // * Trim the difference in instance size of the object. This also cleanly |
| 2374 | // frees inobject properties that moved to the backing store. |
| 2375 | // * If there are properties left in the backing store, trim of the space used |
| 2376 | // to temporarily store the inobject properties. |
| 2377 | // * If there are properties left in the backing store, install the backing |
| 2378 | // store. |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2379 | void JSObject::MigrateToMap(Handle<JSObject> object, Handle<Map> new_map) { |
| 2380 | Isolate* isolate = object->GetIsolate(); |
| 2381 | Handle<Map> old_map(object->map()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2382 | int number_of_fields = new_map->NumberOfFields(); |
| 2383 | int inobject = new_map->inobject_properties(); |
| 2384 | int unused = new_map->unused_property_fields(); |
| 2385 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2386 | // Nothing to do if no functions were converted to fields and no smis were |
| 2387 | // converted to doubles. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2388 | if (!old_map->InstancesNeedRewriting( |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2389 | *new_map, number_of_fields, inobject, unused)) { |
| 2390 | object->set_map(*new_map); |
| 2391 | return; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | int total_size = number_of_fields + unused; |
| 2395 | int external = total_size - inobject; |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2396 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(total_size); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2397 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2398 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
| 2399 | Handle<DescriptorArray> new_descriptors(new_map->instance_descriptors()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2400 | int descriptors = new_map->NumberOfOwnDescriptors(); |
| 2401 | |
| 2402 | for (int i = 0; i < descriptors; i++) { |
| 2403 | PropertyDetails details = new_descriptors->GetDetails(i); |
| 2404 | if (details.type() != FIELD) continue; |
| 2405 | PropertyDetails old_details = old_descriptors->GetDetails(i); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2406 | if (old_details.type() == CALLBACKS) { |
| 2407 | ASSERT(details.representation().IsTagged()); |
| 2408 | continue; |
| 2409 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 2410 | ASSERT(old_details.type() == CONSTANT || |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2411 | old_details.type() == FIELD); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2412 | Object* raw_value = old_details.type() == CONSTANT |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2413 | ? old_descriptors->GetValue(i) |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2414 | : object->RawFastPropertyAt(old_descriptors->GetFieldIndex(i)); |
| 2415 | Handle<Object> value(raw_value, isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2416 | if (FLAG_track_double_fields && |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2417 | !old_details.representation().IsDouble() && |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2418 | details.representation().IsDouble()) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2419 | if (old_details.representation().IsNone()) { |
| 2420 | value = handle(Smi::FromInt(0), isolate); |
| 2421 | } |
| 2422 | value = NewStorageFor(isolate, value, details.representation()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2423 | } |
| 2424 | ASSERT(!(FLAG_track_double_fields && |
| 2425 | details.representation().IsDouble() && |
| 2426 | value->IsSmi())); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2427 | int target_index = new_descriptors->GetFieldIndex(i) - inobject; |
| 2428 | if (target_index < 0) target_index += total_size; |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2429 | array->set(target_index, *value); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2432 | // From here on we cannot fail and we shouldn't GC anymore. |
| 2433 | DisallowHeapAllocation no_allocation; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2434 | |
| 2435 | // Copy (real) inobject properties. If necessary, stop at number_of_fields to |
| 2436 | // avoid overwriting |one_pointer_filler_map|. |
| 2437 | int limit = Min(inobject, number_of_fields); |
| 2438 | for (int i = 0; i < limit; i++) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2439 | object->FastPropertyAtPut(i, array->get(external + i)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | // Create filler object past the new instance size. |
| 2443 | int new_instance_size = new_map->instance_size(); |
| 2444 | int instance_size_delta = old_map->instance_size() - new_instance_size; |
| 2445 | ASSERT(instance_size_delta >= 0); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2446 | Address address = object->address() + new_instance_size; |
| 2447 | isolate->heap()->CreateFillerObjectAt(address, instance_size_delta); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2448 | |
| 2449 | // If there are properties in the new backing store, trim it to the correct |
| 2450 | // size and install the backing store into the object. |
| 2451 | if (external > 0) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2452 | RightTrimFixedArray<FROM_MUTATOR>(isolate->heap(), *array, inobject); |
| 2453 | object->set_properties(*array); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 2456 | object->set_map(*new_map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 2460 | Handle<TransitionArray> Map::AddTransition(Handle<Map> map, |
| 2461 | Handle<Name> key, |
| 2462 | Handle<Map> target, |
| 2463 | SimpleTransitionFlag flag) { |
| 2464 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 2465 | map->AddTransition(*key, *target, flag), |
| 2466 | TransitionArray); |
| 2467 | } |
| 2468 | |
| 2469 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2470 | void JSObject::GeneralizeFieldRepresentation(Handle<JSObject> object, |
| 2471 | int modify_index, |
| 2472 | Representation new_representation, |
| 2473 | StoreMode store_mode) { |
| 2474 | Handle<Map> new_map = Map::GeneralizeRepresentation( |
| 2475 | handle(object->map()), modify_index, new_representation, store_mode); |
| 2476 | if (object->map() == *new_map) return; |
| 2477 | return MigrateToMap(object, new_map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | |
| 2481 | int Map::NumberOfFields() { |
| 2482 | DescriptorArray* descriptors = instance_descriptors(); |
| 2483 | int result = 0; |
| 2484 | for (int i = 0; i < NumberOfOwnDescriptors(); i++) { |
| 2485 | if (descriptors->GetDetails(i).type() == FIELD) result++; |
| 2486 | } |
| 2487 | return result; |
| 2488 | } |
| 2489 | |
| 2490 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2491 | Handle<Map> Map::CopyGeneralizeAllRepresentations(Handle<Map> map, |
| 2492 | int modify_index, |
| 2493 | StoreMode store_mode, |
| 2494 | PropertyAttributes attributes, |
| 2495 | const char* reason) { |
| 2496 | Handle<Map> new_map = Copy(map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2497 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2498 | DescriptorArray* descriptors = new_map->instance_descriptors(); |
| 2499 | descriptors->InitializeRepresentations(Representation::Tagged()); |
| 2500 | |
| 2501 | // Unless the instance is being migrated, ensure that modify_index is a field. |
| 2502 | PropertyDetails details = descriptors->GetDetails(modify_index); |
| 2503 | if (store_mode == FORCE_FIELD && details.type() != FIELD) { |
| 2504 | FieldDescriptor d(descriptors->GetKey(modify_index), |
| 2505 | new_map->NumberOfFields(), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2506 | attributes, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2507 | Representation::Tagged()); |
| 2508 | d.SetSortedKeyIndex(details.pointer()); |
| 2509 | descriptors->Set(modify_index, &d); |
| 2510 | int unused_property_fields = new_map->unused_property_fields() - 1; |
| 2511 | if (unused_property_fields < 0) { |
| 2512 | unused_property_fields += JSObject::kFieldsAdded; |
| 2513 | } |
| 2514 | new_map->set_unused_property_fields(unused_property_fields); |
| 2515 | } |
| 2516 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2517 | if (FLAG_trace_generalization) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2518 | map->PrintGeneralization(stdout, reason, modify_index, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2519 | new_map->NumberOfOwnDescriptors(), |
| 2520 | new_map->NumberOfOwnDescriptors(), |
| 2521 | details.type() == CONSTANT && store_mode == FORCE_FIELD, |
| 2522 | Representation::Tagged(), Representation::Tagged()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2523 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2524 | return new_map; |
| 2525 | } |
| 2526 | |
| 2527 | |
| 2528 | void Map::DeprecateTransitionTree() { |
| 2529 | if (!FLAG_track_fields) return; |
| 2530 | if (is_deprecated()) return; |
| 2531 | if (HasTransitionArray()) { |
| 2532 | TransitionArray* transitions = this->transitions(); |
| 2533 | for (int i = 0; i < transitions->number_of_transitions(); i++) { |
| 2534 | transitions->GetTarget(i)->DeprecateTransitionTree(); |
| 2535 | } |
| 2536 | } |
| 2537 | deprecate(); |
| 2538 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 2539 | GetIsolate(), DependentCode::kTransitionGroup); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 2540 | NotifyLeafMapLayoutChange(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | |
| 2544 | // Invalidates a transition target at |key|, and installs |new_descriptors| over |
| 2545 | // the current instance_descriptors to ensure proper sharing of descriptor |
| 2546 | // arrays. |
| 2547 | void Map::DeprecateTarget(Name* key, DescriptorArray* new_descriptors) { |
| 2548 | if (HasTransitionArray()) { |
| 2549 | TransitionArray* transitions = this->transitions(); |
| 2550 | int transition = transitions->Search(key); |
| 2551 | if (transition != TransitionArray::kNotFound) { |
| 2552 | transitions->GetTarget(transition)->DeprecateTransitionTree(); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | // Don't overwrite the empty descriptor array. |
| 2557 | if (NumberOfOwnDescriptors() == 0) return; |
| 2558 | |
| 2559 | DescriptorArray* to_replace = instance_descriptors(); |
| 2560 | Map* current = this; |
| 2561 | while (current->instance_descriptors() == to_replace) { |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 2562 | current->SetEnumLength(kInvalidEnumCacheSentinel); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2563 | current->set_instance_descriptors(new_descriptors); |
| 2564 | Object* next = current->GetBackPointer(); |
| 2565 | if (next->IsUndefined()) break; |
| 2566 | current = Map::cast(next); |
| 2567 | } |
| 2568 | |
| 2569 | set_owns_descriptors(false); |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | Map* Map::FindRootMap() { |
| 2574 | Map* result = this; |
| 2575 | while (true) { |
| 2576 | Object* back = result->GetBackPointer(); |
| 2577 | if (back->IsUndefined()) return result; |
| 2578 | result = Map::cast(back); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2583 | // Returns NULL if the updated map is incompatible. |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2584 | Map* Map::FindUpdatedMap(int verbatim, |
| 2585 | int length, |
| 2586 | DescriptorArray* descriptors) { |
| 2587 | // This can only be called on roots of transition trees. |
| 2588 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2589 | |
| 2590 | Map* current = this; |
| 2591 | |
| 2592 | for (int i = verbatim; i < length; i++) { |
| 2593 | if (!current->HasTransitionArray()) break; |
| 2594 | Name* name = descriptors->GetKey(i); |
| 2595 | TransitionArray* transitions = current->transitions(); |
| 2596 | int transition = transitions->Search(name); |
| 2597 | if (transition == TransitionArray::kNotFound) break; |
| 2598 | current = transitions->GetTarget(transition); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2599 | PropertyDetails details = descriptors->GetDetails(i); |
| 2600 | PropertyDetails target_details = |
| 2601 | current->instance_descriptors()->GetDetails(i); |
| 2602 | if (details.attributes() != target_details.attributes()) return NULL; |
| 2603 | if (details.type() == CALLBACKS) { |
| 2604 | if (target_details.type() != CALLBACKS) return NULL; |
| 2605 | if (descriptors->GetValue(i) != |
| 2606 | current->instance_descriptors()->GetValue(i)) { |
| 2607 | return NULL; |
| 2608 | } |
| 2609 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | return current; |
| 2613 | } |
| 2614 | |
| 2615 | |
| 2616 | Map* Map::FindLastMatchMap(int verbatim, |
| 2617 | int length, |
| 2618 | DescriptorArray* descriptors) { |
| 2619 | // This can only be called on roots of transition trees. |
| 2620 | ASSERT(GetBackPointer()->IsUndefined()); |
| 2621 | |
| 2622 | Map* current = this; |
| 2623 | |
| 2624 | for (int i = verbatim; i < length; i++) { |
| 2625 | if (!current->HasTransitionArray()) break; |
| 2626 | Name* name = descriptors->GetKey(i); |
| 2627 | TransitionArray* transitions = current->transitions(); |
| 2628 | int transition = transitions->Search(name); |
| 2629 | if (transition == TransitionArray::kNotFound) break; |
| 2630 | |
| 2631 | Map* next = transitions->GetTarget(transition); |
| 2632 | DescriptorArray* next_descriptors = next->instance_descriptors(); |
| 2633 | |
| 2634 | if (next_descriptors->GetValue(i) != descriptors->GetValue(i)) break; |
| 2635 | |
| 2636 | PropertyDetails details = descriptors->GetDetails(i); |
| 2637 | PropertyDetails next_details = next_descriptors->GetDetails(i); |
| 2638 | if (details.type() != next_details.type()) break; |
| 2639 | if (details.attributes() != next_details.attributes()) break; |
| 2640 | if (!details.representation().Equals(next_details.representation())) break; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2641 | |
| 2642 | current = next; |
| 2643 | } |
| 2644 | return current; |
| 2645 | } |
| 2646 | |
| 2647 | |
| 2648 | // Generalize the representation of the descriptor at |modify_index|. |
| 2649 | // This method rewrites the transition tree to reflect the new change. To avoid |
| 2650 | // high degrees over polymorphism, and to stabilize quickly, on every rewrite |
| 2651 | // the new type is deduced by merging the current type with any potential new |
| 2652 | // (partial) version of the type in the transition tree. |
| 2653 | // To do this, on each rewrite: |
| 2654 | // - Search the root of the transition tree using FindRootMap. |
| 2655 | // - Find |updated|, the newest matching version of this map using |
| 2656 | // FindUpdatedMap. This uses the keys in the own map's descriptor array to |
| 2657 | // walk the transition tree. |
| 2658 | // - Merge/generalize the descriptor array of the current map and |updated|. |
| 2659 | // - Generalize the |modify_index| descriptor using |new_representation|. |
| 2660 | // - Walk the tree again starting from the root towards |updated|. Stop at |
| 2661 | // |split_map|, the first map who's descriptor array does not match the merged |
| 2662 | // descriptor array. |
| 2663 | // - If |updated| == |split_map|, |updated| is in the expected state. Return it. |
| 2664 | // - Otherwise, invalidate the outdated transition target from |updated|, and |
| 2665 | // 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] | 2666 | Handle<Map> Map::GeneralizeRepresentation(Handle<Map> old_map, |
| 2667 | int modify_index, |
| 2668 | Representation new_representation, |
| 2669 | StoreMode store_mode) { |
| 2670 | Handle<DescriptorArray> old_descriptors(old_map->instance_descriptors()); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2671 | PropertyDetails old_details = old_descriptors->GetDetails(modify_index); |
| 2672 | Representation old_representation = old_details.representation(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2673 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2674 | // It's fine to transition from None to anything but double without any |
| 2675 | // modification to the object, because the default uninitialized value for |
| 2676 | // representation None can be overwritten by both smi and tagged values. |
| 2677 | // Doubles, however, would require a box allocation. |
| 2678 | if (old_representation.IsNone() && |
| 2679 | !new_representation.IsNone() && |
| 2680 | !new_representation.IsDouble()) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2681 | old_descriptors->SetRepresentation(modify_index, new_representation); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2682 | return old_map; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | int descriptors = old_map->NumberOfOwnDescriptors(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2686 | Handle<Map> root_map(old_map->FindRootMap()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2687 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2688 | // Check the state of the root map. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2689 | if (!old_map->EquivalentToForTransition(*root_map)) { |
| 2690 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 2691 | old_details.attributes(), "not equivalent"); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | int verbatim = root_map->NumberOfOwnDescriptors(); |
| 2695 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2696 | if (store_mode != ALLOW_AS_CONSTANT && modify_index < verbatim) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2697 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 2698 | old_details.attributes(), "root modification"); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2701 | Map* raw_updated = root_map->FindUpdatedMap( |
| 2702 | verbatim, descriptors, *old_descriptors); |
| 2703 | if (raw_updated == NULL) { |
| 2704 | return CopyGeneralizeAllRepresentations(old_map, modify_index, store_mode, |
| 2705 | old_details.attributes(), "incompatible"); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2706 | } |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 2707 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2708 | Handle<Map> updated(raw_updated); |
| 2709 | Handle<DescriptorArray> updated_descriptors(updated->instance_descriptors()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2710 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2711 | int valid = updated->NumberOfOwnDescriptors(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2712 | |
| 2713 | // Directly change the map if the target map is more general. Ensure that the |
| 2714 | // 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] | 2715 | if (updated_descriptors->IsMoreGeneralThan( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2716 | verbatim, valid, descriptors, *old_descriptors) && |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2717 | (store_mode == ALLOW_AS_CONSTANT || |
| 2718 | updated_descriptors->GetDetails(modify_index).type() == FIELD)) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2719 | Representation updated_representation = |
| 2720 | updated_descriptors->GetDetails(modify_index).representation(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2721 | if (new_representation.fits_into(updated_representation)) return updated; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2724 | Handle<DescriptorArray> new_descriptors = DescriptorArray::Merge( |
| 2725 | updated_descriptors, verbatim, valid, descriptors, modify_index, |
| 2726 | store_mode, old_descriptors); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2727 | ASSERT(store_mode == ALLOW_AS_CONSTANT || |
| 2728 | new_descriptors->GetDetails(modify_index).type() == FIELD); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2729 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2730 | old_representation = |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2731 | new_descriptors->GetDetails(modify_index).representation(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2732 | Representation updated_representation = |
| 2733 | new_representation.generalize(old_representation); |
| 2734 | if (!updated_representation.Equals(old_representation)) { |
| 2735 | new_descriptors->SetRepresentation(modify_index, updated_representation); |
| 2736 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2737 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2738 | Handle<Map> split_map(root_map->FindLastMatchMap( |
| 2739 | verbatim, descriptors, *new_descriptors)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2740 | |
| 2741 | int split_descriptors = split_map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2742 | // This is shadowed by |updated_descriptors| being more general than |
| 2743 | // |old_descriptors|. |
| 2744 | ASSERT(descriptors != split_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2745 | |
| 2746 | int descriptor = split_descriptors; |
| 2747 | split_map->DeprecateTarget( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2748 | old_descriptors->GetKey(descriptor), *new_descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2749 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2750 | if (FLAG_trace_generalization) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2751 | old_map->PrintGeneralization( |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 2752 | stdout, "", modify_index, descriptor, descriptors, |
| 2753 | old_descriptors->GetDetails(modify_index).type() == CONSTANT && |
| 2754 | store_mode == FORCE_FIELD, |
| 2755 | old_representation, updated_representation); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2758 | // Add missing transitions. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2759 | Handle<Map> new_map = split_map; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2760 | for (; descriptor < descriptors; descriptor++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2761 | new_map = Map::CopyInstallDescriptors(new_map, descriptor, new_descriptors); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 2762 | new_map->set_migration_target(true); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | new_map->set_owns_descriptors(true); |
| 2766 | return new_map; |
| 2767 | } |
| 2768 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2769 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 2770 | // Generalize the representation of all FIELD descriptors. |
| 2771 | Handle<Map> Map::GeneralizeAllFieldRepresentations( |
| 2772 | Handle<Map> map, |
| 2773 | Representation new_representation) { |
| 2774 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 2775 | for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
| 2776 | PropertyDetails details = descriptors->GetDetails(i); |
| 2777 | if (details.type() == FIELD) { |
| 2778 | map = GeneralizeRepresentation(map, i, new_representation, FORCE_FIELD); |
| 2779 | } |
| 2780 | } |
| 2781 | return map; |
| 2782 | } |
| 2783 | |
| 2784 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2785 | Map* Map::CurrentMapForDeprecated() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 2786 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 2787 | if (!is_deprecated()) return this; |
| 2788 | |
| 2789 | DescriptorArray* old_descriptors = instance_descriptors(); |
| 2790 | |
| 2791 | int descriptors = NumberOfOwnDescriptors(); |
| 2792 | Map* root_map = FindRootMap(); |
| 2793 | |
| 2794 | // Check the state of the root map. |
| 2795 | if (!EquivalentToForTransition(root_map)) return NULL; |
| 2796 | int verbatim = root_map->NumberOfOwnDescriptors(); |
| 2797 | |
| 2798 | Map* updated = root_map->FindUpdatedMap( |
| 2799 | verbatim, descriptors, old_descriptors); |
| 2800 | if (updated == NULL) return NULL; |
| 2801 | |
| 2802 | DescriptorArray* updated_descriptors = updated->instance_descriptors(); |
| 2803 | int valid = updated->NumberOfOwnDescriptors(); |
| 2804 | if (!updated_descriptors->IsMoreGeneralThan( |
| 2805 | verbatim, valid, descriptors, old_descriptors)) { |
| 2806 | return NULL; |
| 2807 | } |
| 2808 | |
| 2809 | return updated; |
| 2810 | } |
| 2811 | |
| 2812 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2813 | Handle<Object> JSObject::SetPropertyWithInterceptor( |
| 2814 | Handle<JSObject> object, |
| 2815 | Handle<Name> name, |
| 2816 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2817 | PropertyAttributes attributes, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 2818 | StrictModeFlag strict_mode) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2819 | // TODO(rossberg): Support symbols in the API. |
| 2820 | if (name->IsSymbol()) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2821 | Isolate* isolate = object->GetIsolate(); |
| 2822 | Handle<String> name_string = Handle<String>::cast(name); |
| 2823 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2824 | if (!interceptor->setter()->IsUndefined()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2825 | LOG(isolate, |
| 2826 | ApiNamedPropertyAccess("interceptor-named-set", *object, *name)); |
| 2827 | PropertyCallbackArguments args( |
| 2828 | isolate, interceptor->data(), *object, *object); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 2829 | v8::NamedPropertySetterCallback setter = |
| 2830 | v8::ToCData<v8::NamedPropertySetterCallback>(interceptor->setter()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2831 | Handle<Object> value_unhole = value->IsTheHole() |
| 2832 | ? Handle<Object>(isolate->factory()->undefined_value()) : value; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2833 | v8::Handle<v8::Value> result = args.Call(setter, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2834 | v8::Utils::ToLocal(name_string), |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2835 | v8::Utils::ToLocal(value_unhole)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2836 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2837 | if (!result.IsEmpty()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2838 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2839 | Handle<Object> result = |
| 2840 | SetPropertyPostInterceptor(object, name, value, attributes, strict_mode); |
| 2841 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2842 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2846 | Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2847 | Handle<Name> name, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2848 | Handle<Object> value, |
| 2849 | PropertyAttributes attributes, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2850 | StrictModeFlag strict_mode, |
| 2851 | StoreFromKeyed store_mode) { |
| 2852 | LookupResult result(object->GetIsolate()); |
| 2853 | object->LocalLookup(*name, &result, true); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2854 | if (!result.IsFound()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2855 | object->map()->LookupTransition(JSObject::cast(*object), *name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 2856 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2857 | return SetProperty(object, &result, name, value, attributes, strict_mode, |
| 2858 | store_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2862 | Handle<Object> JSObject::SetPropertyWithCallback(Handle<JSObject> object, |
| 2863 | Handle<Object> structure, |
| 2864 | Handle<Name> name, |
| 2865 | Handle<Object> value, |
| 2866 | Handle<JSObject> holder, |
| 2867 | StrictModeFlag strict_mode) { |
| 2868 | Isolate* isolate = object->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2869 | |
| 2870 | // We should never get here to initialize a const with the hole |
| 2871 | // value since a const declaration would conflict with the setter. |
| 2872 | ASSERT(!value->IsTheHole()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2873 | |
| 2874 | // 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] | 2875 | // data structure used to store the callbacks. Eventually foreign |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2876 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2877 | if (structure->IsForeign()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2878 | AccessorDescriptor* callback = |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 2879 | reinterpret_cast<AccessorDescriptor*>( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2880 | Handle<Foreign>::cast(structure)->foreign_address()); |
| 2881 | CALL_AND_RETRY_OR_DIE(isolate, |
| 2882 | (callback->setter)( |
| 2883 | isolate, *object, *value, callback->data), |
| 2884 | break, |
| 2885 | return Handle<Object>()); |
| 2886 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2887 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2890 | if (structure->IsExecutableAccessorInfo()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2891 | // api style callbacks |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2892 | ExecutableAccessorInfo* data = ExecutableAccessorInfo::cast(*structure); |
| 2893 | if (!data->IsCompatibleReceiver(*object)) { |
| 2894 | Handle<Object> args[2] = { name, object }; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2895 | Handle<Object> error = |
| 2896 | isolate->factory()->NewTypeError("incompatible_method_receiver", |
| 2897 | HandleVector(args, |
| 2898 | ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2899 | isolate->Throw(*error); |
| 2900 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 2901 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 2902 | // TODO(rossberg): Support symbols in the API. |
| 2903 | if (name->IsSymbol()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2904 | Object* call_obj = data->setter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 2905 | v8::AccessorSetterCallback call_fun = |
| 2906 | v8::ToCData<v8::AccessorSetterCallback>(call_obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2907 | if (call_fun == NULL) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2908 | Handle<String> key = Handle<String>::cast(name); |
| 2909 | LOG(isolate, ApiNamedPropertyAccess("store", *object, *name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2910 | PropertyCallbackArguments args( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2911 | isolate, data->data(), *object, JSObject::cast(*holder)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2912 | args.Call(call_fun, |
| 2913 | v8::Utils::ToLocal(key), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2914 | v8::Utils::ToLocal(value)); |
| 2915 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 2916 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2919 | if (structure->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2920 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2921 | if (setter->IsSpecFunction()) { |
| 2922 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2923 | return SetPropertyWithDefinedSetter( |
| 2924 | object, Handle<JSReceiver>::cast(setter), value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2925 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2926 | if (strict_mode == kNonStrictMode) { |
| 2927 | return value; |
| 2928 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2929 | Handle<Object> args[2] = { name, holder }; |
| 2930 | Handle<Object> error = |
| 2931 | isolate->factory()->NewTypeError("no_setter_in_callback", |
| 2932 | HandleVector(args, 2)); |
| 2933 | isolate->Throw(*error); |
| 2934 | return Handle<Object>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2935 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 2938 | // TODO(dcarney): Handle correctly. |
| 2939 | if (structure->IsDeclaredAccessorInfo()) { |
| 2940 | return value; |
| 2941 | } |
| 2942 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | UNREACHABLE(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2944 | return Handle<Object>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2948 | Handle<Object> JSReceiver::SetPropertyWithDefinedSetter( |
| 2949 | Handle<JSReceiver> object, |
| 2950 | Handle<JSReceiver> setter, |
| 2951 | Handle<Object> value) { |
| 2952 | Isolate* isolate = object->GetIsolate(); |
| 2953 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2954 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2955 | Debug* debug = isolate->debug(); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2956 | // Handle stepping into a setter if step into is active. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2957 | // TODO(rossberg): should this apply to getters that are function proxies? |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2958 | if (debug->StepInActive() && setter->IsJSFunction()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2959 | debug->HandleStepIn( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2960 | Handle<JSFunction>::cast(setter), Handle<Object>::null(), 0, false); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 2961 | } |
| 2962 | #endif |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2963 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2964 | bool has_pending_exception; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2965 | Handle<Object> argv[] = { value }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 2966 | Execution::Call( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2967 | isolate, setter, object, ARRAY_SIZE(argv), argv, &has_pending_exception); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2968 | // Check for pending exception and return the result. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2969 | if (has_pending_exception) return Handle<Object>(); |
| 2970 | return value; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 2973 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 2974 | Handle<Object> JSObject::SetElementWithCallbackSetterInPrototypes( |
| 2975 | Handle<JSObject> object, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2976 | uint32_t index, |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 2977 | Handle<Object> value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 2978 | bool* found, |
| 2979 | StrictModeFlag strict_mode) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 2980 | Isolate *isolate = object->GetIsolate(); |
| 2981 | for (Handle<Object> proto = handle(object->GetPrototype(), isolate); |
| 2982 | !proto->IsNull(); |
| 2983 | proto = handle(proto->GetPrototype(isolate), isolate)) { |
| 2984 | if (proto->IsJSProxy()) { |
| 2985 | return JSProxy::SetPropertyViaPrototypesWithHandler( |
| 2986 | Handle<JSProxy>::cast(proto), |
| 2987 | object, |
| 2988 | isolate->factory()->Uint32ToString(index), // name |
| 2989 | value, |
| 2990 | NONE, |
| 2991 | strict_mode, |
| 2992 | found); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2993 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 2994 | Handle<JSObject> js_proto = Handle<JSObject>::cast(proto); |
| 2995 | if (!js_proto->HasDictionaryElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 2996 | continue; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2997 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 2998 | Handle<SeededNumberDictionary> dictionary(js_proto->element_dictionary()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 2999 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 3000 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3001 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 3002 | if (details.type() == CALLBACKS) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3003 | *found = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3004 | Handle<Object> structure(dictionary->ValueAt(entry), isolate); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 3005 | return SetElementWithCallback(object, structure, index, value, js_proto, |
| 3006 | strict_mode); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3007 | } |
| 3008 | } |
| 3009 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3010 | *found = false; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 3011 | return isolate->factory()->the_hole_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3014 | |
| 3015 | Handle<Object> JSObject::SetPropertyViaPrototypes(Handle<JSObject> object, |
| 3016 | Handle<Name> name, |
| 3017 | Handle<Object> value, |
| 3018 | PropertyAttributes attributes, |
| 3019 | StrictModeFlag strict_mode, |
| 3020 | bool* done) { |
| 3021 | Isolate* isolate = object->GetIsolate(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3022 | |
| 3023 | *done = false; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 3024 | // 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] | 3025 | // accessor that wants to handle the property, or whether the property is |
| 3026 | // read-only on the prototype chain. |
| 3027 | LookupResult result(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3028 | object->LookupRealNamedPropertyInPrototypes(*name, &result); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3029 | if (result.IsFound()) { |
| 3030 | switch (result.type()) { |
| 3031 | case NORMAL: |
| 3032 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 3033 | case CONSTANT: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3034 | *done = result.IsReadOnly(); |
| 3035 | break; |
| 3036 | case INTERCEPTOR: { |
| 3037 | PropertyAttributes attr = |
| 3038 | result.holder()->GetPropertyAttributeWithInterceptor( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3039 | *object, *name, true); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3040 | *done = !!(attr & READ_ONLY); |
| 3041 | break; |
| 3042 | } |
| 3043 | case CALLBACKS: { |
| 3044 | if (!FLAG_es5_readonly && result.IsReadOnly()) break; |
| 3045 | *done = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3046 | Handle<Object> callback_object(result.GetCallbackObject(), isolate); |
| 3047 | return SetPropertyWithCallback(object, callback_object, name, value, |
| 3048 | handle(result.holder()), strict_mode); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3049 | } |
| 3050 | case HANDLER: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3051 | Handle<JSProxy> proxy(result.proxy()); |
| 3052 | return JSProxy::SetPropertyViaPrototypesWithHandler( |
| 3053 | proxy, object, name, value, attributes, strict_mode, done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3054 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3055 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 3056 | case NONEXISTENT: |
| 3057 | UNREACHABLE(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3058 | break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3059 | } |
| 3060 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3061 | |
| 3062 | // If we get here with *done true, we have encountered a read-only property. |
| 3063 | if (!FLAG_es5_readonly) *done = false; |
| 3064 | if (*done) { |
| 3065 | if (strict_mode == kNonStrictMode) return value; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3066 | Handle<Object> args[] = { name, object }; |
| 3067 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3068 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
| 3069 | isolate->Throw(*error); |
| 3070 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3071 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3072 | return isolate->factory()->the_hole_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3075 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3076 | void Map::EnsureDescriptorSlack(Handle<Map> map, int slack) { |
| 3077 | Handle<DescriptorArray> descriptors(map->instance_descriptors()); |
| 3078 | if (slack <= descriptors->NumberOfSlackDescriptors()) return; |
| 3079 | int number_of_descriptors = descriptors->number_of_descriptors(); |
| 3080 | Isolate* isolate = map->GetIsolate(); |
| 3081 | Handle<DescriptorArray> new_descriptors = |
| 3082 | isolate->factory()->NewDescriptorArray(number_of_descriptors, slack); |
| 3083 | DescriptorArray::WhitenessWitness witness(*new_descriptors); |
| 3084 | |
| 3085 | for (int i = 0; i < number_of_descriptors; ++i) { |
| 3086 | new_descriptors->CopyFrom(i, *descriptors, i, witness); |
| 3087 | } |
| 3088 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3089 | map->set_instance_descriptors(*new_descriptors); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3093 | template<class T> |
| 3094 | static int AppendUniqueCallbacks(NeanderArray* callbacks, |
| 3095 | Handle<typename T::Array> array, |
| 3096 | int valid_descriptors) { |
| 3097 | int nof_callbacks = callbacks->length(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3098 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3099 | Isolate* isolate = array->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3100 | // Ensure the keys are unique names before writing them into the |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3101 | // 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] | 3102 | // temporarily put the heap in an invalid state while appending descriptors. |
| 3103 | for (int i = 0; i < nof_callbacks; ++i) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3104 | Handle<AccessorInfo> entry(AccessorInfo::cast(callbacks->get(i))); |
| 3105 | if (entry->name()->IsUniqueName()) continue; |
| 3106 | Handle<String> key = |
| 3107 | isolate->factory()->InternalizedStringFromString( |
| 3108 | Handle<String>(String::cast(entry->name()))); |
| 3109 | entry->set_name(*key); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3112 | // Fill in new callback descriptors. Process the callbacks from |
| 3113 | // back to front so that the last callback with a given name takes |
| 3114 | // precedence over previously added callbacks with that name. |
| 3115 | for (int i = nof_callbacks - 1; i >= 0; i--) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3116 | AccessorInfo* entry = AccessorInfo::cast(callbacks->get(i)); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3117 | Name* key = Name::cast(entry->name()); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3118 | // Check if a descriptor with this name already exists before writing. |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3119 | if (!T::Contains(key, entry, valid_descriptors, array)) { |
| 3120 | T::Insert(key, entry, valid_descriptors, array); |
| 3121 | valid_descriptors++; |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3125 | return valid_descriptors; |
| 3126 | } |
| 3127 | |
| 3128 | struct DescriptorArrayAppender { |
| 3129 | typedef DescriptorArray Array; |
| 3130 | static bool Contains(Name* key, |
| 3131 | AccessorInfo* entry, |
| 3132 | int valid_descriptors, |
| 3133 | Handle<DescriptorArray> array) { |
| 3134 | return array->Search(key, valid_descriptors) != DescriptorArray::kNotFound; |
| 3135 | } |
| 3136 | static void Insert(Name* key, |
| 3137 | AccessorInfo* entry, |
| 3138 | int valid_descriptors, |
| 3139 | Handle<DescriptorArray> array) { |
| 3140 | CallbacksDescriptor desc(key, entry, entry->property_attributes()); |
| 3141 | array->Append(&desc); |
| 3142 | } |
| 3143 | }; |
| 3144 | |
| 3145 | |
| 3146 | struct FixedArrayAppender { |
| 3147 | typedef FixedArray Array; |
| 3148 | static bool Contains(Name* key, |
| 3149 | AccessorInfo* entry, |
| 3150 | int valid_descriptors, |
| 3151 | Handle<FixedArray> array) { |
| 3152 | for (int i = 0; i < valid_descriptors; i++) { |
| 3153 | if (key == AccessorInfo::cast(array->get(i))->name()) return true; |
| 3154 | } |
| 3155 | return false; |
| 3156 | } |
| 3157 | static void Insert(Name* key, |
| 3158 | AccessorInfo* entry, |
| 3159 | int valid_descriptors, |
| 3160 | Handle<FixedArray> array) { |
| 3161 | array->set(valid_descriptors, entry); |
| 3162 | } |
| 3163 | }; |
| 3164 | |
| 3165 | |
| 3166 | void Map::AppendCallbackDescriptors(Handle<Map> map, |
| 3167 | Handle<Object> descriptors) { |
| 3168 | int nof = map->NumberOfOwnDescriptors(); |
| 3169 | Handle<DescriptorArray> array(map->instance_descriptors()); |
| 3170 | NeanderArray callbacks(descriptors); |
| 3171 | ASSERT(array->NumberOfSlackDescriptors() >= callbacks.length()); |
| 3172 | nof = AppendUniqueCallbacks<DescriptorArrayAppender>(&callbacks, array, nof); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 3173 | map->SetNumberOfOwnDescriptors(nof); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 3177 | int AccessorInfo::AppendUnique(Handle<Object> descriptors, |
| 3178 | Handle<FixedArray> array, |
| 3179 | int valid_descriptors) { |
| 3180 | NeanderArray callbacks(descriptors); |
| 3181 | ASSERT(array->length() >= callbacks.length() + valid_descriptors); |
| 3182 | return AppendUniqueCallbacks<FixedArrayAppender>(&callbacks, |
| 3183 | array, |
| 3184 | valid_descriptors); |
| 3185 | } |
| 3186 | |
| 3187 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3188 | static bool ContainsMap(MapHandleList* maps, Handle<Map> map) { |
| 3189 | ASSERT(!map.is_null()); |
| 3190 | for (int i = 0; i < maps->length(); ++i) { |
| 3191 | if (!maps->at(i).is_null() && maps->at(i).is_identical_to(map)) return true; |
| 3192 | } |
| 3193 | return false; |
| 3194 | } |
| 3195 | |
| 3196 | |
| 3197 | template <class T> |
| 3198 | static Handle<T> MaybeNull(T* p) { |
| 3199 | if (p == NULL) return Handle<T>::null(); |
| 3200 | return Handle<T>(p); |
| 3201 | } |
| 3202 | |
| 3203 | |
| 3204 | Handle<Map> Map::FindTransitionedMap(MapHandleList* candidates) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3205 | ElementsKind kind = elements_kind(); |
| 3206 | Handle<Map> transitioned_map = Handle<Map>::null(); |
| 3207 | Handle<Map> current_map(this); |
| 3208 | bool packed = IsFastPackedElementsKind(kind); |
| 3209 | if (IsTransitionableFastElementsKind(kind)) { |
| 3210 | while (CanTransitionToMoreGeneralFastElementsKind(kind, false)) { |
| 3211 | kind = GetNextMoreGeneralFastElementsKind(kind, false); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3212 | Handle<Map> maybe_transitioned_map = |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3213 | MaybeNull(current_map->LookupElementsTransitionMap(kind)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3214 | if (maybe_transitioned_map.is_null()) break; |
| 3215 | if (ContainsMap(candidates, maybe_transitioned_map) && |
| 3216 | (packed || !IsFastPackedElementsKind(kind))) { |
| 3217 | transitioned_map = maybe_transitioned_map; |
| 3218 | if (!IsFastPackedElementsKind(kind)) packed = false; |
| 3219 | } |
| 3220 | current_map = maybe_transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3221 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3222 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3223 | return transitioned_map; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 3226 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3227 | static Map* FindClosestElementsTransition(Map* map, ElementsKind to_kind) { |
| 3228 | Map* current_map = map; |
| 3229 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3230 | int to_index = IsFastElementsKind(to_kind) |
| 3231 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 3232 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 3233 | |
| 3234 | ASSERT(index <= to_index); |
| 3235 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3236 | for (; index < to_index; ++index) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3237 | if (!current_map->HasElementsTransition()) return current_map; |
| 3238 | current_map = current_map->elements_transition_map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3239 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3240 | if (!IsFastElementsKind(to_kind) && current_map->HasElementsTransition()) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3241 | Map* next_map = current_map->elements_transition_map(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3242 | if (next_map->elements_kind() == to_kind) return next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3243 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3244 | ASSERT(IsFastElementsKind(to_kind) |
| 3245 | ? current_map->elements_kind() == to_kind |
| 3246 | : current_map->elements_kind() == TERMINAL_FAST_ELEMENTS_KIND); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3247 | return current_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
| 3250 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3251 | Map* Map::LookupElementsTransitionMap(ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3252 | Map* to_map = FindClosestElementsTransition(this, to_kind); |
| 3253 | if (to_map->elements_kind() == to_kind) return to_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3254 | return NULL; |
| 3255 | } |
| 3256 | |
| 3257 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3258 | bool Map::IsMapInArrayPrototypeChain() { |
| 3259 | Isolate* isolate = GetIsolate(); |
| 3260 | if (isolate->initial_array_prototype()->map() == this) { |
| 3261 | return true; |
| 3262 | } |
| 3263 | |
| 3264 | if (isolate->initial_object_prototype()->map() == this) { |
| 3265 | return true; |
| 3266 | } |
| 3267 | |
| 3268 | return false; |
| 3269 | } |
| 3270 | |
| 3271 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3272 | static MaybeObject* AddMissingElementsTransitions(Map* map, |
| 3273 | ElementsKind to_kind) { |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3274 | ASSERT(IsFastElementsKind(map->elements_kind())); |
| 3275 | int index = GetSequenceIndexFromFastElementsKind(map->elements_kind()); |
| 3276 | int to_index = IsFastElementsKind(to_kind) |
| 3277 | ? GetSequenceIndexFromFastElementsKind(to_kind) |
| 3278 | : GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND); |
| 3279 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3280 | ASSERT(index <= to_index); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3281 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3282 | Map* current_map = map; |
| 3283 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3284 | for (; index < to_index; ++index) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3285 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(index + 1); |
| 3286 | MaybeObject* maybe_next_map = |
| 3287 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 3288 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3291 | // In case we are exiting the fast elements kind system, just add the map in |
| 3292 | // the end. |
| 3293 | if (!IsFastElementsKind(to_kind)) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3294 | MaybeObject* maybe_next_map = |
| 3295 | current_map->CopyAsElementsKind(to_kind, INSERT_TRANSITION); |
| 3296 | if (!maybe_next_map->To(¤t_map)) return maybe_next_map; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3299 | ASSERT(current_map->elements_kind() == to_kind); |
| 3300 | return current_map; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3304 | Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object, |
| 3305 | ElementsKind to_kind) { |
| 3306 | Isolate* isolate = object->GetIsolate(); |
| 3307 | CALL_HEAP_FUNCTION(isolate, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3308 | object->GetElementsTransitionMap(isolate, to_kind), |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3309 | Map); |
| 3310 | } |
| 3311 | |
| 3312 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 3313 | MaybeObject* JSObject::GetElementsTransitionMapSlow(ElementsKind to_kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3314 | Map* start_map = map(); |
| 3315 | ElementsKind from_kind = start_map->elements_kind(); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 3316 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3317 | if (from_kind == to_kind) { |
| 3318 | return start_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3321 | bool allow_store_transition = |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 3322 | // 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] | 3323 | // non-matching element transition. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3324 | !start_map->IsUndefined() && !start_map->is_shared() && |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 3325 | IsFastElementsKind(from_kind); |
| 3326 | |
| 3327 | // Only store fast element maps in ascending generality. |
| 3328 | if (IsFastElementsKind(to_kind)) { |
| 3329 | allow_store_transition &= |
| 3330 | IsTransitionableFastElementsKind(from_kind) && |
| 3331 | IsMoreGeneralElementsKindTransition(from_kind, to_kind); |
| 3332 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3333 | |
| 3334 | if (!allow_store_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3335 | return start_map->CopyAsElementsKind(to_kind, OMIT_TRANSITION); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3336 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3337 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3338 | return start_map->AsElementsKind(to_kind); |
| 3339 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3340 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3341 | |
| 3342 | MaybeObject* Map::AsElementsKind(ElementsKind kind) { |
| 3343 | Map* closest_map = FindClosestElementsTransition(this, kind); |
| 3344 | |
| 3345 | if (closest_map->elements_kind() == kind) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3346 | return closest_map; |
| 3347 | } |
| 3348 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3349 | return AddMissingElementsTransitions(closest_map, kind); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3353 | void JSObject::LocalLookupRealNamedProperty(Name* name, LookupResult* result) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3354 | if (IsJSGlobalProxy()) { |
| 3355 | Object* proto = GetPrototype(); |
| 3356 | if (proto->IsNull()) return result->NotFound(); |
| 3357 | ASSERT(proto->IsJSGlobalObject()); |
| 3358 | return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result); |
| 3359 | } |
| 3360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3361 | if (HasFastProperties()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3362 | map()->LookupDescriptor(this, name, result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3363 | // A property or a map transition was found. We return all of these result |
| 3364 | // types because LocalLookupRealNamedProperty is used when setting |
| 3365 | // properties where map transitions are handled. |
| 3366 | ASSERT(!result->IsFound() || |
| 3367 | (result->holder() == this && result->IsFastPropertyType())); |
| 3368 | // Disallow caching for uninitialized constants. These can only |
| 3369 | // occur as fields. |
| 3370 | if (result->IsField() && |
| 3371 | result->IsReadOnly() && |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3372 | RawFastPropertyAt(result->GetFieldIndex().field_index())->IsTheHole()) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3373 | result->DisallowCaching(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3374 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3375 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3376 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3377 | |
| 3378 | int entry = property_dictionary()->FindEntry(name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3379 | if (entry != NameDictionary::kNotFound) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3380 | Object* value = property_dictionary()->ValueAt(entry); |
| 3381 | if (IsGlobalObject()) { |
| 3382 | PropertyDetails d = property_dictionary()->DetailsAt(entry); |
| 3383 | if (d.IsDeleted()) { |
| 3384 | result->NotFound(); |
| 3385 | return; |
| 3386 | } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 3387 | value = PropertyCell::cast(value)->value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 3388 | } |
| 3389 | // Make sure to disallow caching for uninitialized constants |
| 3390 | // found in the dictionary-mode objects. |
| 3391 | if (value->IsTheHole()) result->DisallowCaching(); |
| 3392 | result->DictionaryResult(this, entry); |
| 3393 | return; |
| 3394 | } |
| 3395 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3396 | result->NotFound(); |
| 3397 | } |
| 3398 | |
| 3399 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3400 | void JSObject::LookupRealNamedProperty(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3401 | LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3402 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3403 | |
| 3404 | LookupRealNamedPropertyInPrototypes(name, result); |
| 3405 | } |
| 3406 | |
| 3407 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3408 | void JSObject::LookupRealNamedPropertyInPrototypes(Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3409 | LookupResult* result) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3410 | Isolate* isolate = GetIsolate(); |
| 3411 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3412 | for (Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3413 | pt != heap->null_value(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3414 | pt = pt->GetPrototype(isolate)) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3415 | if (pt->IsJSProxy()) { |
| 3416 | return result->HandlerResult(JSProxy::cast(pt)); |
| 3417 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3418 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 3419 | ASSERT(!(result->IsFound() && result->type() == INTERCEPTOR)); |
| 3420 | if (result->IsFound()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3421 | } |
| 3422 | result->NotFound(); |
| 3423 | } |
| 3424 | |
| 3425 | |
| 3426 | // We only need to deal with CALLBACKS and INTERCEPTORS |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3427 | Handle<Object> JSObject::SetPropertyWithFailedAccessCheck( |
| 3428 | Handle<JSObject> object, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3429 | LookupResult* result, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3430 | Handle<Name> name, |
| 3431 | Handle<Object> value, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3432 | bool check_prototype, |
| 3433 | StrictModeFlag strict_mode) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3434 | if (check_prototype && !result->IsProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3435 | object->LookupRealNamedPropertyInPrototypes(*name, result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | if (result->IsProperty()) { |
| 3439 | if (!result->IsReadOnly()) { |
| 3440 | switch (result->type()) { |
| 3441 | case CALLBACKS: { |
| 3442 | Object* obj = result->GetCallbackObject(); |
| 3443 | if (obj->IsAccessorInfo()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3444 | Handle<AccessorInfo> info(AccessorInfo::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3445 | if (info->all_can_write()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3446 | return SetPropertyWithCallback(object, |
| 3447 | info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3448 | name, |
| 3449 | value, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3450 | handle(result->holder()), |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3451 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3452 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3453 | } else if (obj->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3454 | Handle<AccessorPair> pair(AccessorPair::cast(obj)); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3455 | if (pair->all_can_read()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3456 | return SetPropertyWithCallback(object, |
| 3457 | pair, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3458 | name, |
| 3459 | value, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3460 | handle(result->holder()), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3461 | strict_mode); |
| 3462 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3463 | } |
| 3464 | break; |
| 3465 | } |
| 3466 | case INTERCEPTOR: { |
| 3467 | // Try lookup real named properties. Note that only property can be |
| 3468 | // 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] | 3469 | LookupResult r(object->GetIsolate()); |
| 3470 | object->LookupRealNamedProperty(*name, &r); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3471 | if (r.IsProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3472 | return SetPropertyWithFailedAccessCheck(object, |
| 3473 | &r, |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3474 | name, |
| 3475 | value, |
| 3476 | check_prototype, |
| 3477 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3478 | } |
| 3479 | break; |
| 3480 | } |
| 3481 | default: { |
| 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | } |
| 3486 | } |
| 3487 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3488 | Isolate* isolate = object->GetIsolate(); |
| 3489 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 3490 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 3491 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3495 | Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object, |
| 3496 | LookupResult* result, |
| 3497 | Handle<Name> key, |
| 3498 | Handle<Object> value, |
| 3499 | PropertyAttributes attributes, |
| 3500 | StrictModeFlag strict_mode, |
| 3501 | StoreFromKeyed store_mode) { |
yangguo@chromium.org | de0db00 | 2012-06-22 13:44:28 +0000 | [diff] [blame] | 3502 | if (result->IsHandler()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3503 | return JSProxy::SetPropertyWithHandler(handle(result->proxy()), |
| 3504 | object, key, value, attributes, strict_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3505 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3506 | return JSObject::SetPropertyForResult(Handle<JSObject>::cast(object), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3507 | result, key, value, attributes, strict_mode, store_mode); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3512 | bool JSProxy::HasPropertyWithHandler(Handle<JSProxy> proxy, Handle<Name> name) { |
| 3513 | Isolate* isolate = proxy->GetIsolate(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +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()) return false; |
| 3517 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3518 | Handle<Object> args[] = { name }; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3519 | Handle<Object> result = proxy->CallTrap( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3520 | "has", isolate->derived_has_trap(), ARRAY_SIZE(args), args); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3521 | if (isolate->has_pending_exception()) return false; |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3522 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3523 | return result->BooleanValue(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3527 | Handle<Object> JSProxy::SetPropertyWithHandler(Handle<JSProxy> proxy, |
| 3528 | Handle<JSReceiver> receiver, |
| 3529 | Handle<Name> name, |
| 3530 | Handle<Object> value, |
| 3531 | PropertyAttributes attributes, |
| 3532 | StrictModeFlag strict_mode) { |
| 3533 | Isolate* isolate = proxy->GetIsolate(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3534 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3535 | // 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] | 3536 | if (name->IsSymbol()) return value; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3537 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3538 | Handle<Object> args[] = { receiver, name, value }; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3539 | proxy->CallTrap("set", isolate->derived_set_trap(), ARRAY_SIZE(args), args); |
| 3540 | if (isolate->has_pending_exception()) return Handle<Object>(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3541 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3542 | return value; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3546 | Handle<Object> JSProxy::SetPropertyViaPrototypesWithHandler( |
| 3547 | Handle<JSProxy> proxy, |
| 3548 | Handle<JSReceiver> receiver, |
| 3549 | Handle<Name> name, |
| 3550 | Handle<Object> value, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3551 | PropertyAttributes attributes, |
| 3552 | StrictModeFlag strict_mode, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3553 | bool* done) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3554 | Isolate* isolate = proxy->GetIsolate(); |
| 3555 | Handle<Object> handler(proxy->handler(), isolate); // Trap might morph proxy. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3556 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3557 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3558 | if (name->IsSymbol()) { |
| 3559 | *done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3560 | return isolate->factory()->the_hole_value(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3563 | *done = true; // except where redefined... |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3564 | Handle<Object> args[] = { name }; |
| 3565 | Handle<Object> result = proxy->CallTrap( |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3566 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3567 | if (isolate->has_pending_exception()) return Handle<Object>(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3568 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3569 | if (result->IsUndefined()) { |
| 3570 | *done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3571 | return isolate->factory()->the_hole_value(); |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3574 | // Emulate [[GetProperty]] semantics for proxies. |
| 3575 | bool has_pending_exception; |
| 3576 | Handle<Object> argv[] = { result }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3577 | Handle<Object> desc = Execution::Call( |
| 3578 | isolate, isolate->to_complete_property_descriptor(), result, |
| 3579 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3580 | if (has_pending_exception) return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3581 | |
| 3582 | // [[GetProperty]] requires to check that all properties are configurable. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3583 | Handle<String> configurable_name = |
| 3584 | isolate->factory()->InternalizeOneByteString( |
| 3585 | STATIC_ASCII_VECTOR("configurable_")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3586 | Handle<Object> configurable( |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3587 | v8::internal::GetProperty(isolate, desc, configurable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3588 | ASSERT(!isolate->has_pending_exception()); |
| 3589 | ASSERT(configurable->IsTrue() || configurable->IsFalse()); |
| 3590 | if (configurable->IsFalse()) { |
| 3591 | Handle<String> trap = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3592 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3593 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3594 | Handle<Object> args[] = { handler, trap, name }; |
| 3595 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3596 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3597 | isolate->Throw(*error); |
| 3598 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3599 | } |
| 3600 | ASSERT(configurable->IsTrue()); |
| 3601 | |
| 3602 | // Check for DataDescriptor. |
| 3603 | Handle<String> hasWritable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3604 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3605 | STATIC_ASCII_VECTOR("hasWritable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3606 | Handle<Object> hasWritable( |
| 3607 | v8::internal::GetProperty(isolate, desc, hasWritable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3608 | ASSERT(!isolate->has_pending_exception()); |
| 3609 | ASSERT(hasWritable->IsTrue() || hasWritable->IsFalse()); |
| 3610 | if (hasWritable->IsTrue()) { |
| 3611 | Handle<String> writable_name = |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3612 | isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3613 | STATIC_ASCII_VECTOR("writable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3614 | Handle<Object> writable( |
| 3615 | v8::internal::GetProperty(isolate, desc, writable_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3616 | ASSERT(!isolate->has_pending_exception()); |
| 3617 | ASSERT(writable->IsTrue() || writable->IsFalse()); |
| 3618 | *done = writable->IsFalse(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3619 | if (!*done) return isolate->factory()->the_hole_value(); |
| 3620 | if (strict_mode == kNonStrictMode) return value; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3621 | Handle<Object> args[] = { name, receiver }; |
| 3622 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3623 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3624 | isolate->Throw(*error); |
| 3625 | return Handle<Object>(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | // We have an AccessorDescriptor. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3629 | Handle<String> set_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3630 | STATIC_ASCII_VECTOR("set_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3631 | Handle<Object> setter(v8::internal::GetProperty(isolate, desc, set_name)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3632 | ASSERT(!isolate->has_pending_exception()); |
| 3633 | if (!setter->IsUndefined()) { |
| 3634 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3635 | return SetPropertyWithDefinedSetter( |
| 3636 | receiver, Handle<JSReceiver>::cast(setter), value); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3639 | if (strict_mode == kNonStrictMode) return value; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 3640 | Handle<Object> args2[] = { name, proxy }; |
| 3641 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3642 | "no_setter_in_callback", HandleVector(args2, ARRAY_SIZE(args2))); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3643 | isolate->Throw(*error); |
| 3644 | return Handle<Object>(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3648 | Handle<Object> JSProxy::DeletePropertyWithHandler( |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3649 | Handle<JSProxy> proxy, Handle<Name> name, DeleteMode mode) { |
| 3650 | Isolate* isolate = proxy->GetIsolate(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3651 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3652 | // 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] | 3653 | if (name->IsSymbol()) return isolate->factory()->false_value(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3654 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3655 | Handle<Object> args[] = { name }; |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3656 | Handle<Object> result = proxy->CallTrap( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3657 | "delete", Handle<Object>(), ARRAY_SIZE(args), args); |
| 3658 | if (isolate->has_pending_exception()) return Handle<Object>(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3659 | |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3660 | bool result_bool = result->BooleanValue(); |
| 3661 | if (mode == STRICT_DELETION && !result_bool) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3662 | Handle<Object> handler(proxy->handler(), isolate); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3663 | Handle<String> trap_name = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3664 | STATIC_ASCII_VECTOR("delete")); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 3665 | Handle<Object> args[] = { handler, trap_name }; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3666 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3667 | "handler_failed", HandleVector(args, ARRAY_SIZE(args))); |
| 3668 | isolate->Throw(*error); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3669 | return Handle<Object>(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3670 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3671 | return isolate->factory()->ToBoolean(result_bool); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3675 | Handle<Object> JSProxy::DeleteElementWithHandler( |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3676 | Handle<JSProxy> proxy, uint32_t index, DeleteMode mode) { |
| 3677 | Isolate* isolate = proxy->GetIsolate(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3678 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3679 | return JSProxy::DeletePropertyWithHandler(proxy, name, mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | |
| 3683 | MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler( |
| 3684 | JSReceiver* receiver_raw, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 3685 | Name* name_raw) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3686 | Isolate* isolate = GetIsolate(); |
| 3687 | HandleScope scope(isolate); |
| 3688 | Handle<JSProxy> proxy(this); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3689 | Handle<Object> handler(this->handler(), isolate); // Trap might morph proxy. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3690 | Handle<JSReceiver> receiver(receiver_raw); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3691 | Handle<Object> name(name_raw, isolate); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3692 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 3693 | // TODO(rossberg): adjust once there is a story for symbols vs proxies. |
| 3694 | if (name->IsSymbol()) return ABSENT; |
| 3695 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3696 | Handle<Object> args[] = { name }; |
| 3697 | Handle<Object> result = CallTrap( |
| 3698 | "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3699 | if (isolate->has_pending_exception()) return NONE; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3700 | |
| 3701 | if (result->IsUndefined()) return ABSENT; |
| 3702 | |
| 3703 | bool has_pending_exception; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3704 | Handle<Object> argv[] = { result }; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3705 | Handle<Object> desc = Execution::Call( |
| 3706 | isolate, isolate->to_complete_property_descriptor(), result, |
| 3707 | ARRAY_SIZE(argv), argv, &has_pending_exception); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3708 | if (has_pending_exception) return NONE; |
| 3709 | |
| 3710 | // Convert result to PropertyAttributes. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3711 | Handle<String> enum_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3712 | STATIC_ASCII_VECTOR("enumerable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3713 | Handle<Object> enumerable(v8::internal::GetProperty(isolate, desc, enum_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3714 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3715 | Handle<String> conf_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3716 | STATIC_ASCII_VECTOR("configurable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3717 | Handle<Object> configurable(v8::internal::GetProperty(isolate, desc, conf_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3718 | if (isolate->has_pending_exception()) return NONE; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3719 | Handle<String> writ_n = isolate->factory()->InternalizeOneByteString( |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3720 | STATIC_ASCII_VECTOR("writable_")); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3721 | Handle<Object> writable(v8::internal::GetProperty(isolate, desc, writ_n)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3722 | if (isolate->has_pending_exception()) return NONE; |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 3723 | if (!writable->BooleanValue()) { |
| 3724 | Handle<String> set_n = isolate->factory()->InternalizeOneByteString( |
| 3725 | STATIC_ASCII_VECTOR("set_")); |
| 3726 | Handle<Object> setter(v8::internal::GetProperty(isolate, desc, set_n)); |
| 3727 | if (isolate->has_pending_exception()) return NONE; |
| 3728 | writable = isolate->factory()->ToBoolean(!setter->IsUndefined()); |
| 3729 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3730 | |
| 3731 | if (configurable->IsFalse()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3732 | Handle<String> trap = isolate->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 3733 | STATIC_ASCII_VECTOR("getPropertyDescriptor")); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 3734 | Handle<Object> args[] = { handler, trap, name }; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3735 | Handle<Object> error = isolate->factory()->NewTypeError( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3736 | "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args))); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3737 | isolate->Throw(*error); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3738 | return NONE; |
| 3739 | } |
| 3740 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3741 | int attributes = NONE; |
svenpanne@chromium.org | 9faefa4 | 2013-03-08 13:13:16 +0000 | [diff] [blame] | 3742 | if (!enumerable->BooleanValue()) attributes |= DONT_ENUM; |
| 3743 | if (!configurable->BooleanValue()) attributes |= DONT_DELETE; |
| 3744 | if (!writable->BooleanValue()) attributes |= READ_ONLY; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3745 | return static_cast<PropertyAttributes>(attributes); |
| 3746 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3747 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3748 | |
| 3749 | MUST_USE_RESULT PropertyAttributes JSProxy::GetElementAttributeWithHandler( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3750 | JSReceiver* receiver_raw, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3751 | uint32_t index) { |
| 3752 | Isolate* isolate = GetIsolate(); |
| 3753 | HandleScope scope(isolate); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3754 | Handle<JSProxy> proxy(this); |
| 3755 | Handle<JSReceiver> receiver(receiver_raw); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3756 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3757 | return proxy->GetPropertyAttributeWithHandler(*receiver, *name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3761 | void JSProxy::Fix(Handle<JSProxy> proxy) { |
| 3762 | Isolate* isolate = proxy->GetIsolate(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3763 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3764 | // Save identity hash. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 3765 | Handle<Object> hash(proxy->GetIdentityHash(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3766 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3767 | if (proxy->IsJSFunctionProxy()) { |
| 3768 | isolate->factory()->BecomeJSFunction(proxy); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3769 | // Code will be set on the JavaScript side. |
| 3770 | } else { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3771 | isolate->factory()->BecomeJSObject(proxy); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 3772 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3773 | ASSERT(proxy->IsJSObject()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3774 | |
| 3775 | // Inherit identity, if it was present. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 3776 | if (hash->IsSmi()) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 3777 | JSObject::SetIdentityHash(Handle<JSObject>::cast(proxy), |
| 3778 | Handle<Smi>::cast(hash)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3779 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 3783 | MUST_USE_RESULT Handle<Object> JSProxy::CallTrap(const char* name, |
| 3784 | Handle<Object> derived, |
| 3785 | int argc, |
| 3786 | Handle<Object> argv[]) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3787 | Isolate* isolate = GetIsolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3788 | Handle<Object> handler(this->handler(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3789 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 3790 | Handle<String> trap_name = isolate->factory()->InternalizeUtf8String(name); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 3791 | Handle<Object> trap(v8::internal::GetProperty(isolate, handler, trap_name)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3792 | if (isolate->has_pending_exception()) return trap; |
| 3793 | |
| 3794 | if (trap->IsUndefined()) { |
| 3795 | if (derived.is_null()) { |
| 3796 | Handle<Object> args[] = { handler, trap_name }; |
| 3797 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 3798 | "handler_trap_missing", HandleVector(args, ARRAY_SIZE(args))); |
| 3799 | isolate->Throw(*error); |
| 3800 | return Handle<Object>(); |
| 3801 | } |
| 3802 | trap = Handle<Object>(derived); |
| 3803 | } |
| 3804 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3805 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 3806 | return Execution::Call(isolate, trap, handler, argc, argv, &threw); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3809 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3810 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 3811 | static Handle<Map> MapAsElementsKind(Handle<Map> map, ElementsKind kind) { |
| 3812 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->AsElementsKind(kind), Map); |
| 3813 | } |
| 3814 | |
| 3815 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3816 | void JSObject::AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3817 | ASSERT(object->map()->inobject_properties() == map->inobject_properties()); |
| 3818 | ElementsKind obj_kind = object->map()->elements_kind(); |
| 3819 | ElementsKind map_kind = map->elements_kind(); |
| 3820 | if (map_kind != obj_kind) { |
| 3821 | ElementsKind to_kind = map_kind; |
| 3822 | if (IsMoreGeneralElementsKindTransition(map_kind, obj_kind) || |
| 3823 | IsDictionaryElementsKind(obj_kind)) { |
| 3824 | to_kind = obj_kind; |
| 3825 | } |
| 3826 | if (IsDictionaryElementsKind(to_kind)) { |
| 3827 | NormalizeElements(object); |
| 3828 | } else { |
| 3829 | TransitionElementsKind(object, to_kind); |
| 3830 | } |
| 3831 | map = MapAsElementsKind(map, to_kind); |
| 3832 | } |
| 3833 | int total_size = |
| 3834 | map->NumberOfOwnDescriptors() + map->unused_property_fields(); |
| 3835 | int out_of_object = total_size - map->inobject_properties(); |
| 3836 | if (out_of_object != object->properties()->length()) { |
| 3837 | Isolate* isolate = object->GetIsolate(); |
| 3838 | Handle<FixedArray> new_properties = isolate->factory()->CopySizeFixedArray( |
| 3839 | handle(object->properties()), out_of_object); |
| 3840 | object->set_properties(*new_properties); |
| 3841 | } |
| 3842 | object->set_map(*map); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3846 | void JSObject::MigrateInstance(Handle<JSObject> object) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3847 | // Converting any field to the most specific type will cause the |
| 3848 | // GeneralizeFieldRepresentation algorithm to create the most general existing |
| 3849 | // transition that matches the object. This achieves what is needed. |
| 3850 | Handle<Map> original_map(object->map()); |
| 3851 | GeneralizeFieldRepresentation( |
| 3852 | object, 0, Representation::None(), ALLOW_AS_CONSTANT); |
| 3853 | if (FLAG_trace_migration) { |
| 3854 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 3855 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 3859 | Handle<Object> JSObject::TryMigrateInstance(Handle<JSObject> object) { |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 3860 | Map* new_map = object->map()->CurrentMapForDeprecated(); |
| 3861 | if (new_map == NULL) return Handle<Object>(); |
| 3862 | Handle<Map> original_map(object->map()); |
| 3863 | JSObject::MigrateToMap(object, handle(new_map)); |
| 3864 | if (FLAG_trace_migration) { |
| 3865 | object->PrintInstanceMigration(stdout, *original_map, object->map()); |
| 3866 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3867 | return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3871 | Handle<Object> JSObject::SetPropertyUsingTransition( |
| 3872 | Handle<JSObject> object, |
| 3873 | LookupResult* lookup, |
| 3874 | Handle<Name> name, |
| 3875 | Handle<Object> value, |
| 3876 | PropertyAttributes attributes) { |
| 3877 | Handle<Map> transition_map(lookup->GetTransitionTarget()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3878 | int descriptor = transition_map->LastAdded(); |
| 3879 | |
| 3880 | DescriptorArray* descriptors = transition_map->instance_descriptors(); |
| 3881 | PropertyDetails details = descriptors->GetDetails(descriptor); |
| 3882 | |
| 3883 | if (details.type() == CALLBACKS || attributes != details.attributes()) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3884 | // AddProperty will either normalize the object, or create a new fast copy |
| 3885 | // of the map. If we get a fast copy of the map, all field representations |
| 3886 | // will be tagged since the transition is omitted. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3887 | return JSObject::AddProperty( |
| 3888 | object, name, value, attributes, kNonStrictMode, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3889 | JSReceiver::CERTAINLY_NOT_STORE_FROM_KEYED, |
| 3890 | JSReceiver::OMIT_EXTENSIBILITY_CHECK, |
| 3891 | JSObject::FORCE_TAGGED, FORCE_FIELD, OMIT_TRANSITION); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | // Keep the target CONSTANT if the same value is stored. |
| 3895 | // TODO(verwaest): Also support keeping the placeholder |
| 3896 | // (value->IsUninitialized) as constant. |
| 3897 | if (details.type() == CONSTANT && |
| 3898 | descriptors->GetValue(descriptor) == *value) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3899 | object->set_map(*transition_map); |
| 3900 | return value; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | Representation representation = details.representation(); |
| 3904 | |
| 3905 | if (!value->FitsRepresentation(representation) || |
| 3906 | details.type() == CONSTANT) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3907 | transition_map = Map::GeneralizeRepresentation(transition_map, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3908 | descriptor, value->OptimalRepresentation(), FORCE_FIELD); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3909 | Object* back = transition_map->GetBackPointer(); |
| 3910 | if (back->IsMap()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3911 | MigrateToMap(object, handle(Map::cast(back))); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3912 | } |
| 3913 | descriptors = transition_map->instance_descriptors(); |
| 3914 | representation = descriptors->GetDetails(descriptor).representation(); |
| 3915 | } |
| 3916 | |
| 3917 | int field_index = descriptors->GetFieldIndex(descriptor); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3918 | AddFastPropertyUsingMap( |
| 3919 | object, transition_map, name, value, field_index, representation); |
| 3920 | return value; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3924 | static void SetPropertyToField(LookupResult* lookup, |
| 3925 | Handle<Name> name, |
| 3926 | Handle<Object> value) { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3927 | Representation representation = lookup->representation(); |
| 3928 | if (!value->FitsRepresentation(representation) || |
| 3929 | lookup->type() == CONSTANT) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3930 | JSObject::GeneralizeFieldRepresentation(handle(lookup->holder()), |
| 3931 | lookup->GetDescriptorIndex(), |
| 3932 | value->OptimalRepresentation(), |
| 3933 | FORCE_FIELD); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3934 | DescriptorArray* desc = lookup->holder()->map()->instance_descriptors(); |
| 3935 | int descriptor = lookup->GetDescriptorIndex(); |
| 3936 | representation = desc->GetDetails(descriptor).representation(); |
| 3937 | } |
| 3938 | |
| 3939 | if (FLAG_track_double_fields && representation.IsDouble()) { |
| 3940 | HeapNumber* storage = HeapNumber::cast(lookup->holder()->RawFastPropertyAt( |
| 3941 | lookup->GetFieldIndex().field_index())); |
| 3942 | storage->set_value(value->Number()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3943 | return; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | lookup->holder()->FastPropertyAtPut( |
| 3947 | lookup->GetFieldIndex().field_index(), *value); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3951 | static void ConvertAndSetLocalProperty(LookupResult* lookup, |
| 3952 | Handle<Name> name, |
| 3953 | Handle<Object> value, |
| 3954 | PropertyAttributes attributes) { |
| 3955 | Handle<JSObject> object(lookup->holder()); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3956 | if (object->TooManyFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3957 | JSObject::NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | if (!object->HasFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3961 | ReplaceSlowProperty(object, name, value, attributes); |
| 3962 | return; |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | int descriptor_index = lookup->GetDescriptorIndex(); |
| 3966 | if (lookup->GetAttributes() == attributes) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3967 | JSObject::GeneralizeFieldRepresentation( |
| 3968 | object, descriptor_index, Representation::Tagged(), FORCE_FIELD); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3969 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3970 | Handle<Map> old_map(object->map()); |
| 3971 | Handle<Map> new_map = Map::CopyGeneralizeAllRepresentations(old_map, |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3972 | descriptor_index, FORCE_FIELD, attributes, "attributes mismatch"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3973 | JSObject::MigrateToMap(object, new_map); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | DescriptorArray* descriptors = object->map()->instance_descriptors(); |
| 3977 | int index = descriptors->GetDetails(descriptor_index).field_index(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3978 | object->FastPropertyAtPut(index, *value); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3982 | static void SetPropertyToFieldWithAttributes(LookupResult* lookup, |
| 3983 | Handle<Name> name, |
| 3984 | Handle<Object> value, |
| 3985 | PropertyAttributes attributes) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3986 | if (lookup->GetAttributes() == attributes) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3987 | if (value->IsUninitialized()) return; |
| 3988 | SetPropertyToField(lookup, name, value); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3989 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3990 | ConvertAndSetLocalProperty(lookup, name, value, attributes); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3995 | Handle<Object> JSObject::SetPropertyForResult(Handle<JSObject> object, |
| 3996 | LookupResult* lookup, |
| 3997 | Handle<Name> name, |
| 3998 | Handle<Object> value, |
| 3999 | PropertyAttributes attributes, |
| 4000 | StrictModeFlag strict_mode, |
| 4001 | StoreFromKeyed store_mode) { |
| 4002 | Isolate* isolate = object->GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4003 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4004 | // Make sure that the top context does not change when doing callbacks or |
| 4005 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4006 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4007 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4008 | // 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] | 4009 | // dictionary. We internalize these short keys to avoid constantly |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 4010 | // reallocating them. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4011 | if (name->IsString() && !name->IsInternalizedString() && |
| 4012 | Handle<String>::cast(name)->length() <= 2) { |
| 4013 | name = isolate->factory()->InternalizeString(Handle<String>::cast(name)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4015 | |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 4016 | // Check access rights if needed. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4017 | if (object->IsAccessCheckNeeded()) { |
| 4018 | if (!isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 4019 | return SetPropertyWithFailedAccessCheck(object, lookup, name, value, |
| 4020 | true, strict_mode); |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 4021 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4024 | if (object->IsJSGlobalProxy()) { |
| 4025 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 4026 | if (proto->IsNull()) return value; |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 4027 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4028 | return SetPropertyForResult(Handle<JSObject>::cast(proto), |
| 4029 | lookup, name, value, attributes, strict_mode, store_mode); |
hpayer@chromium.org | 2f877ac | 2013-09-18 15:29:06 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4032 | ASSERT(!lookup->IsFound() || lookup->holder() == *object || |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 4033 | lookup->holder()->map()->is_hidden_prototype()); |
| 4034 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4035 | if (!lookup->IsProperty() && !object->IsJSContextExtensionObject()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4036 | bool done = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4037 | Handle<Object> result_object = SetPropertyViaPrototypes( |
| 4038 | object, name, value, attributes, strict_mode, &done); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4039 | if (done) return result_object; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4040 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4041 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4042 | if (!lookup->IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4043 | // Neither properties nor transitions found. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4044 | return AddProperty( |
| 4045 | object, name, value, attributes, strict_mode, store_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4046 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4047 | |
| 4048 | if (lookup->IsProperty() && lookup->IsReadOnly()) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4049 | if (strict_mode == kStrictMode) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4050 | Handle<Object> args[] = { name, object }; |
| 4051 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 4052 | "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))); |
| 4053 | isolate->Throw(*error); |
| 4054 | return Handle<Object>(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4055 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4056 | return value; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4057 | } |
| 4058 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4059 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4060 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 4061 | bool is_observed = FLAG_harmony_observation && |
| 4062 | object->map()->is_observed() && |
| 4063 | *name != isolate->heap()->hidden_string(); |
| 4064 | if (is_observed && lookup->IsDataProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4065 | old_value = Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4068 | // This is a real property that is not read-only, or it is a |
| 4069 | // 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] | 4070 | Handle<Object> result = value; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4071 | switch (lookup->type()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4072 | case NORMAL: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4073 | SetNormalizedProperty(handle(lookup->holder()), lookup, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4074 | break; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4075 | case FIELD: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4076 | SetPropertyToField(lookup, name, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4077 | break; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4078 | case CONSTANT: |
| 4079 | // Only replace the constant if necessary. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4080 | if (*value == lookup->GetConstant()) return value; |
| 4081 | SetPropertyToField(lookup, name, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4082 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4083 | case CALLBACKS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4084 | Handle<Object> callback_object(lookup->GetCallbackObject(), isolate); |
| 4085 | return SetPropertyWithCallback(object, callback_object, name, value, |
| 4086 | handle(lookup->holder()), strict_mode); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4087 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4088 | case INTERCEPTOR: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4089 | result = SetPropertyWithInterceptor(handle(lookup->holder()), name, value, |
| 4090 | attributes, strict_mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4091 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4092 | case TRANSITION: |
| 4093 | result = SetPropertyUsingTransition(handle(lookup->holder()), lookup, |
| 4094 | name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4095 | break; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4096 | case HANDLER: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4097 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4098 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4099 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4100 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4101 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<Object>()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4102 | |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 4103 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4104 | if (lookup->IsTransition()) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4105 | EnqueueChangeRecord(object, "add", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4106 | } else { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 4107 | LookupResult new_lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4108 | object->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4109 | if (new_lookup.IsDataProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4110 | Handle<Object> new_value = Object::GetProperty(object, name); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4111 | if (!new_value->SameValue(*old_value)) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4112 | EnqueueChangeRecord(object, "update", name, old_value); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4113 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
| 4116 | } |
| 4117 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4118 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | |
| 4122 | // 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] | 4123 | // present, add it with attributes NONE. This code is an exact clone of |
| 4124 | // SetProperty, with the check for IsReadOnly and the check for a |
| 4125 | // callback setter removed. The two lines looking up the LookupResult |
| 4126 | // result are also added. If one of the functions is changed, the other |
| 4127 | // should be. |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 4128 | // Note that this method cannot be used to set the prototype of a function |
| 4129 | // because ConvertDescriptorToField() which is called in "case CALLBACKS:" |
| 4130 | // doesn't handle function prototypes correctly. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4131 | Handle<Object> JSObject::SetLocalPropertyIgnoreAttributes( |
| 4132 | Handle<JSObject> object, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4133 | Handle<Name> name, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4134 | Handle<Object> value, |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 4135 | PropertyAttributes attributes, |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4136 | ValueType value_type, |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4137 | StoreMode mode, |
| 4138 | ExtensibilityCheck extensibility_check) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4139 | Isolate* isolate = object->GetIsolate(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4140 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4141 | // Make sure that the top context does not change when doing callbacks or |
| 4142 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4143 | AssertNoContextChange ncc(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4144 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4145 | LookupResult lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4146 | object->LocalLookup(*name, &lookup, true); |
| 4147 | if (!lookup.IsFound()) { |
| 4148 | object->map()->LookupTransition(*object, *name, &lookup); |
| 4149 | } |
| 4150 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4151 | // Check access rights if needed. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4152 | if (object->IsAccessCheckNeeded()) { |
| 4153 | if (!isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 4154 | return SetPropertyWithFailedAccessCheck(object, &lookup, name, value, |
| 4155 | false, kNonStrictMode); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4156 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4157 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4158 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4159 | if (object->IsJSGlobalProxy()) { |
| 4160 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 4161 | if (proto->IsNull()) return value; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4162 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4163 | return SetLocalPropertyIgnoreAttributes(Handle<JSObject>::cast(proto), |
| 4164 | name, value, attributes, value_type, mode, extensibility_check); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4167 | if (lookup.IsFound() && |
| 4168 | (lookup.type() == INTERCEPTOR || lookup.type() == CALLBACKS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4169 | object->LocalLookupRealNamedProperty(*name, &lookup); |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4172 | // Check for accessor in prototype chain removed here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4173 | if (!lookup.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4174 | // Neither properties nor transitions found. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4175 | return AddProperty(object, name, value, attributes, kNonStrictMode, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4176 | MAY_BE_STORE_FROM_KEYED, extensibility_check, value_type, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4179 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4180 | PropertyAttributes old_attributes = ABSENT; |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 4181 | bool is_observed = FLAG_harmony_observation && |
| 4182 | object->map()->is_observed() && |
| 4183 | *name != isolate->heap()->hidden_string(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 4184 | if (is_observed && lookup.IsProperty()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 4185 | if (lookup.IsDataProperty()) old_value = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4186 | Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4187 | old_attributes = lookup.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4188 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 4189 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4190 | // Check of IsReadOnly removed from here in clone. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4191 | switch (lookup.type()) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4192 | case NORMAL: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4193 | ReplaceSlowProperty(object, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4194 | break; |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4195 | case FIELD: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4196 | SetPropertyToFieldWithAttributes(&lookup, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4197 | break; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4198 | case CONSTANT: |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 4199 | // Only replace the constant if necessary. |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4200 | if (lookup.GetAttributes() != attributes || |
| 4201 | *value != lookup.GetConstant()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4202 | SetPropertyToFieldWithAttributes(&lookup, name, value, attributes); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 4203 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4204 | break; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4205 | case CALLBACKS: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4206 | ConvertAndSetLocalProperty(&lookup, name, value, attributes); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4207 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4208 | case TRANSITION: { |
| 4209 | Handle<Object> result = SetPropertyUsingTransition( |
| 4210 | handle(lookup.holder()), &lookup, name, value, attributes); |
| 4211 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<Object>()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4212 | break; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4213 | } |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4214 | case NONEXISTENT: |
mstarzinger@chromium.org | 1f410f9 | 2013-08-29 08:13:16 +0000 | [diff] [blame] | 4215 | case HANDLER: |
| 4216 | case INTERCEPTOR: |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4217 | UNREACHABLE(); |
| 4218 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4219 | |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 4220 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4221 | if (lookup.IsTransition()) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4222 | EnqueueChangeRecord(object, "add", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4223 | } else if (old_value->IsTheHole()) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4224 | EnqueueChangeRecord(object, "reconfigure", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4225 | } else { |
| 4226 | LookupResult new_lookup(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4227 | object->LocalLookup(*name, &new_lookup, true); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4228 | bool value_changed = false; |
| 4229 | if (new_lookup.IsDataProperty()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4230 | Handle<Object> new_value = Object::GetProperty(object, name); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 4231 | value_changed = !old_value->SameValue(*new_value); |
| 4232 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4233 | if (new_lookup.GetAttributes() != old_attributes) { |
| 4234 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4235 | EnqueueChangeRecord(object, "reconfigure", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 4236 | } else if (value_changed) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 4237 | EnqueueChangeRecord(object, "update", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4242 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | |
| 4246 | PropertyAttributes JSObject::GetPropertyAttributePostInterceptor( |
| 4247 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4248 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4249 | bool continue_search) { |
| 4250 | // Check local property, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4251 | LookupResult result(GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4252 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 4253 | if (result.IsFound()) return result.GetAttributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4254 | |
| 4255 | if (continue_search) { |
| 4256 | // Continue searching via the prototype chain. |
| 4257 | Object* pt = GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4258 | if (!pt->IsNull()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4259 | return JSObject::cast(pt)-> |
| 4260 | GetPropertyAttributeWithReceiver(receiver, name); |
| 4261 | } |
| 4262 | } |
| 4263 | return ABSENT; |
| 4264 | } |
| 4265 | |
| 4266 | |
| 4267 | PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor( |
| 4268 | JSObject* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4269 | Name* name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4270 | bool continue_search) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4271 | // TODO(rossberg): Support symbols in the API. |
| 4272 | if (name->IsSymbol()) return ABSENT; |
| 4273 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4274 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4275 | HandleScope scope(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4276 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4277 | // Make sure that the top context does not change when doing |
| 4278 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4279 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4280 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4281 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 4282 | Handle<JSObject> receiver_handle(receiver); |
| 4283 | Handle<JSObject> holder_handle(this); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4284 | Handle<String> name_handle(String::cast(name)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 4285 | PropertyCallbackArguments args(isolate, interceptor->data(), receiver, this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4286 | if (!interceptor->query()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4287 | v8::NamedPropertyQueryCallback query = |
| 4288 | v8::ToCData<v8::NamedPropertyQueryCallback>(interceptor->query()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4289 | LOG(isolate, |
| 4290 | ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4291 | v8::Handle<v8::Integer> result = |
| 4292 | args.Call(query, v8::Utils::ToLocal(name_handle)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4293 | if (!result.IsEmpty()) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4294 | ASSERT(result->IsInt32()); |
| 4295 | return static_cast<PropertyAttributes>(result->Int32Value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4296 | } |
| 4297 | } else if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4298 | v8::NamedPropertyGetterCallback getter = |
| 4299 | v8::ToCData<v8::NamedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4300 | LOG(isolate, |
| 4301 | ApiNamedPropertyAccess("interceptor-named-get-has", this, name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4302 | v8::Handle<v8::Value> result = |
| 4303 | args.Call(getter, v8::Utils::ToLocal(name_handle)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4304 | if (!result.IsEmpty()) return DONT_ENUM; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4305 | } |
| 4306 | return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle, |
| 4307 | *name_handle, |
| 4308 | continue_search); |
| 4309 | } |
| 4310 | |
| 4311 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4312 | PropertyAttributes JSReceiver::GetPropertyAttributeWithReceiver( |
| 4313 | JSReceiver* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4314 | Name* key) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4315 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4316 | if (IsJSObject() && key->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4317 | return JSObject::cast(this)->GetElementAttributeWithReceiver( |
| 4318 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4319 | } |
| 4320 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4321 | LookupResult lookup(GetIsolate()); |
| 4322 | Lookup(key, &lookup); |
| 4323 | return GetPropertyAttributeForResult(receiver, &lookup, key, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4327 | PropertyAttributes JSReceiver::GetPropertyAttributeForResult( |
| 4328 | JSReceiver* receiver, |
| 4329 | LookupResult* lookup, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4330 | Name* name, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4331 | bool continue_search) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4332 | // Check access rights if needed. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4333 | if (IsAccessCheckNeeded()) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4334 | JSObject* this_obj = JSObject::cast(this); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4335 | Heap* heap = GetHeap(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4336 | if (!heap->isolate()->MayNamedAccess(this_obj, name, v8::ACCESS_HAS)) { |
| 4337 | return this_obj->GetPropertyAttributeWithFailedAccessCheck( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4338 | receiver, lookup, name, continue_search); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4339 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4340 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4341 | if (lookup->IsFound()) { |
| 4342 | switch (lookup->type()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4343 | case NORMAL: // fall through |
| 4344 | case FIELD: |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4345 | case CONSTANT: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4346 | case CALLBACKS: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4347 | return lookup->GetAttributes(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4348 | case HANDLER: { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4349 | return JSProxy::cast(lookup->proxy())->GetPropertyAttributeWithHandler( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4350 | receiver, name); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4351 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4352 | case INTERCEPTOR: |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4353 | return lookup->holder()->GetPropertyAttributeWithInterceptor( |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4354 | JSObject::cast(receiver), name, continue_search); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4355 | case TRANSITION: |
| 4356 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4357 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | return ABSENT; |
| 4361 | } |
| 4362 | |
| 4363 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4364 | PropertyAttributes JSReceiver::GetLocalPropertyAttribute(Name* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4365 | // Check whether the name is an array index. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4366 | uint32_t index = 0; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4367 | if (IsJSObject() && name->AsArrayIndex(&index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4368 | return GetLocalElementAttribute(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4369 | } |
| 4370 | // Named property. |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4371 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 4372 | LocalLookup(name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4373 | return GetPropertyAttributeForResult(this, &lookup, name, false); |
| 4374 | } |
| 4375 | |
| 4376 | |
| 4377 | PropertyAttributes JSObject::GetElementAttributeWithReceiver( |
| 4378 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4379 | Isolate* isolate = GetIsolate(); |
| 4380 | |
| 4381 | // Check access rights if needed. |
| 4382 | if (IsAccessCheckNeeded()) { |
| 4383 | if (!isolate->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 4384 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 4385 | return ABSENT; |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | if (IsJSGlobalProxy()) { |
| 4390 | Object* proto = GetPrototype(); |
| 4391 | if (proto->IsNull()) return ABSENT; |
| 4392 | ASSERT(proto->IsJSGlobalObject()); |
| 4393 | return JSObject::cast(proto)->GetElementAttributeWithReceiver( |
| 4394 | receiver, index, continue_search); |
| 4395 | } |
| 4396 | |
| 4397 | // Check for lookup interceptor except when bootstrapping. |
| 4398 | if (HasIndexedInterceptor() && !isolate->bootstrapper()->IsActive()) { |
| 4399 | return GetElementAttributeWithInterceptor(receiver, index, continue_search); |
| 4400 | } |
| 4401 | |
| 4402 | return GetElementAttributeWithoutInterceptor( |
| 4403 | receiver, index, continue_search); |
| 4404 | } |
| 4405 | |
| 4406 | |
| 4407 | PropertyAttributes JSObject::GetElementAttributeWithInterceptor( |
| 4408 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
| 4409 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4410 | HandleScope scope(isolate); |
| 4411 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4412 | // Make sure that the top context does not change when doing |
| 4413 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 4414 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 4415 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4416 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 4417 | Handle<JSReceiver> hreceiver(receiver); |
| 4418 | Handle<JSObject> holder(this); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 4419 | PropertyCallbackArguments args(isolate, interceptor->data(), receiver, this); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4420 | if (!interceptor->query()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4421 | v8::IndexedPropertyQueryCallback query = |
| 4422 | v8::ToCData<v8::IndexedPropertyQueryCallback>(interceptor->query()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4423 | LOG(isolate, |
| 4424 | ApiIndexedPropertyAccess("interceptor-indexed-has", this, index)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4425 | v8::Handle<v8::Integer> result = args.Call(query, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4426 | if (!result.IsEmpty()) |
| 4427 | return static_cast<PropertyAttributes>(result->Int32Value()); |
| 4428 | } else if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 4429 | v8::IndexedPropertyGetterCallback getter = |
| 4430 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4431 | LOG(isolate, |
| 4432 | ApiIndexedPropertyAccess("interceptor-indexed-get-has", this, index)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4433 | v8::Handle<v8::Value> result = args.Call(getter, index); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 4434 | if (!result.IsEmpty()) return NONE; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | return holder->GetElementAttributeWithoutInterceptor( |
| 4438 | *hreceiver, index, continue_search); |
| 4439 | } |
| 4440 | |
| 4441 | |
| 4442 | PropertyAttributes JSObject::GetElementAttributeWithoutInterceptor( |
| 4443 | JSReceiver* receiver, uint32_t index, bool continue_search) { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4444 | PropertyAttributes attr = GetElementsAccessor()->GetAttributes( |
| 4445 | receiver, this, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4446 | if (attr != ABSENT) return attr; |
| 4447 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4448 | // Handle [] on String objects. |
| 4449 | if (IsStringObjectWithCharacterAt(index)) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4450 | return static_cast<PropertyAttributes>(READ_ONLY | DONT_DELETE); |
| 4451 | } |
| 4452 | |
| 4453 | if (!continue_search) return ABSENT; |
| 4454 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4455 | Object* pt = GetPrototype(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4456 | if (pt->IsJSProxy()) { |
| 4457 | // 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] | 4458 | return JSProxy::cast(pt)->GetElementAttributeWithHandler(receiver, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 4459 | } |
| 4460 | if (pt->IsNull()) return ABSENT; |
| 4461 | return JSObject::cast(pt)->GetElementAttributeWithReceiver( |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 4462 | receiver, index, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4466 | Handle<Map> NormalizedMapCache::Get(Handle<NormalizedMapCache> cache, |
| 4467 | Handle<JSObject> obj, |
| 4468 | PropertyNormalizationMode mode) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4469 | int index = obj->map()->Hash() % kEntries; |
| 4470 | Handle<Object> result = handle(cache->get(index), cache->GetIsolate()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4471 | if (result->IsMap() && |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4472 | Handle<Map>::cast(result)->EquivalentToForNormalization(obj->map(), |
| 4473 | mode)) { |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4474 | #ifdef VERIFY_HEAP |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4475 | if (FLAG_verify_heap) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4476 | Handle<Map>::cast(result)->SharedMapVerify(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4477 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 4478 | #endif |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 4479 | #ifdef ENABLE_SLOW_ASSERTS |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4480 | if (FLAG_enable_slow_asserts) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 4481 | // The cached map should match newly created normalized map bit-by-bit, |
| 4482 | // except for the code cache, which can contain some ics which can be |
| 4483 | // applied to the shared map. |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4484 | Handle<Map> fresh = Map::CopyNormalized(handle(obj->map()), mode, |
| 4485 | SHARED_NORMALIZED_MAP); |
| 4486 | |
| 4487 | ASSERT(memcmp(fresh->address(), |
| 4488 | Handle<Map>::cast(result)->address(), |
| 4489 | Map::kCodeCacheOffset) == 0); |
| 4490 | STATIC_ASSERT(Map::kDependentCodeOffset == |
| 4491 | Map::kCodeCacheOffset + kPointerSize); |
| 4492 | int offset = Map::kDependentCodeOffset + kPointerSize; |
| 4493 | ASSERT(memcmp(fresh->address() + offset, |
| 4494 | Handle<Map>::cast(result)->address() + offset, |
| 4495 | Map::kSize - offset) == 0); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4496 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4497 | #endif |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4498 | return Handle<Map>::cast(result); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4499 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 4500 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4501 | Isolate* isolate = cache->GetIsolate(); |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 4502 | Handle<Map> map = Map::CopyNormalized(handle(obj->map()), mode, |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4503 | SHARED_NORMALIZED_MAP); |
| 4504 | ASSERT(map->is_dictionary_map()); |
| 4505 | cache->set(index, *map); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 4506 | isolate->counters()->normalized_maps()->Increment(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4507 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4508 | return map; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4512 | void NormalizedMapCache::Clear() { |
| 4513 | int entries = length(); |
| 4514 | for (int i = 0; i != entries; i++) { |
| 4515 | set_undefined(i); |
| 4516 | } |
| 4517 | } |
| 4518 | |
| 4519 | |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 4520 | void HeapObject::UpdateMapCodeCache(Handle<HeapObject> object, |
| 4521 | Handle<Name> name, |
| 4522 | Handle<Code> code) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4523 | Handle<Map> map(object->map()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4524 | Map::UpdateCodeCache(map, name, code); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4528 | void JSObject::NormalizeProperties(Handle<JSObject> object, |
| 4529 | PropertyNormalizationMode mode, |
| 4530 | int expected_additional_properties) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4531 | if (!object->HasFastProperties()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4532 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4533 | // The global object is always normalized. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4534 | ASSERT(!object->IsGlobalObject()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4535 | // JSGlobalProxy must never be normalized |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4536 | ASSERT(!object->IsJSGlobalProxy()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4537 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4538 | Isolate* isolate = object->GetIsolate(); |
| 4539 | HandleScope scope(isolate); |
| 4540 | Handle<Map> map(object->map()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4541 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4542 | // Allocate new content. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4543 | int real_size = map->NumberOfOwnDescriptors(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 4544 | int property_count = real_size; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 4545 | if (expected_additional_properties > 0) { |
| 4546 | property_count += expected_additional_properties; |
| 4547 | } else { |
| 4548 | property_count += 2; // Make space for two more properties. |
| 4549 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4550 | Handle<NameDictionary> dictionary = |
| 4551 | isolate->factory()->NewNameDictionary(property_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4552 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4553 | Handle<DescriptorArray> descs(map->instance_descriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4554 | for (int i = 0; i < real_size; i++) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 4555 | PropertyDetails details = descs->GetDetails(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4556 | switch (details.type()) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 4557 | case CONSTANT: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4558 | Handle<Name> key(descs->GetKey(i)); |
| 4559 | Handle<Object> value(descs->GetConstant(i), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4560 | PropertyDetails d = PropertyDetails( |
| 4561 | details.attributes(), NORMAL, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4562 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | case FIELD: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4566 | Handle<Name> key(descs->GetKey(i)); |
| 4567 | Handle<Object> value( |
| 4568 | object->RawFastPropertyAt(descs->GetFieldIndex(i)), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4569 | PropertyDetails d = |
| 4570 | PropertyDetails(details.attributes(), NORMAL, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4571 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4572 | break; |
| 4573 | } |
| 4574 | case CALLBACKS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4575 | Handle<Name> key(descs->GetKey(i)); |
| 4576 | Handle<Object> value(descs->GetCallbacksObject(i), isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4577 | PropertyDetails d = PropertyDetails( |
| 4578 | details.attributes(), CALLBACKS, i + 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4579 | dictionary = NameDictionaryAdd(dictionary, key, value, d); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4580 | break; |
| 4581 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4582 | case INTERCEPTOR: |
| 4583 | break; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4584 | case HANDLER: |
| 4585 | case NORMAL: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 4586 | case TRANSITION: |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 4587 | case NONEXISTENT: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4588 | UNREACHABLE(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 4589 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | // Copy the next enumeration index from instance descriptor. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4594 | dictionary->SetNextEnumerationIndex(real_size + 1); |
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 | Handle<NormalizedMapCache> cache( |
| 4597 | isolate->context()->native_context()->normalized_map_cache()); |
| 4598 | Handle<Map> new_map = NormalizedMapCache::Get(cache, object, mode); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 4599 | ASSERT(new_map->is_dictionary_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4600 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4601 | // From here on we cannot fail and we shouldn't GC anymore. |
| 4602 | DisallowHeapAllocation no_allocation; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4603 | |
| 4604 | // Resize the object in the heap if necessary. |
| 4605 | int new_instance_size = new_map->instance_size(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4606 | int instance_size_delta = map->instance_size() - new_instance_size; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 4607 | ASSERT(instance_size_delta >= 0); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4608 | isolate->heap()->CreateFillerObjectAt(object->address() + new_instance_size, |
| 4609 | instance_size_delta); |
| 4610 | if (Marking::IsBlack(Marking::MarkBitFrom(*object))) { |
| 4611 | MemoryChunk::IncrementLiveBytesFromMutator(object->address(), |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 4612 | -instance_size_delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4615 | object->set_map(*new_map); |
| 4616 | map->NotifyLeafMapLayoutChange(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4617 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4618 | object->set_properties(*dictionary); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4619 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4620 | isolate->counters()->props_to_dictionary()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4621 | |
| 4622 | #ifdef DEBUG |
| 4623 | if (FLAG_trace_normalization) { |
| 4624 | PrintF("Object properties have been normalized:\n"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4625 | object->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4626 | } |
| 4627 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4631 | void JSObject::TransformToFastProperties(Handle<JSObject> object, |
| 4632 | int unused_property_fields) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4633 | if (object->HasFastProperties()) return; |
| 4634 | ASSERT(!object->IsGlobalObject()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4635 | CALL_HEAP_FUNCTION_VOID( |
| 4636 | object->GetIsolate(), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4637 | object->property_dictionary()->TransformPropertiesToFastFor( |
| 4638 | *object, unused_property_fields)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4642 | static MUST_USE_RESULT MaybeObject* CopyFastElementsToDictionary( |
| 4643 | Isolate* isolate, |
| 4644 | FixedArrayBase* array, |
| 4645 | int length, |
| 4646 | SeededNumberDictionary* dictionary) { |
| 4647 | Heap* heap = isolate->heap(); |
| 4648 | bool has_double_elements = array->IsFixedDoubleArray(); |
| 4649 | for (int i = 0; i < length; i++) { |
| 4650 | Object* value = NULL; |
| 4651 | if (has_double_elements) { |
| 4652 | FixedDoubleArray* double_array = FixedDoubleArray::cast(array); |
| 4653 | if (double_array->is_the_hole(i)) { |
| 4654 | value = isolate->heap()->the_hole_value(); |
| 4655 | } else { |
| 4656 | // Objects must be allocated in the old object space, since the |
| 4657 | // overall number of HeapNumbers needed for the conversion might |
| 4658 | // exceed the capacity of new space, and we would fail repeatedly |
| 4659 | // trying to convert the FixedDoubleArray. |
| 4660 | MaybeObject* maybe_value_object = |
| 4661 | heap->AllocateHeapNumber(double_array->get_scalar(i), TENURED); |
| 4662 | if (!maybe_value_object->ToObject(&value)) return maybe_value_object; |
| 4663 | } |
| 4664 | } else { |
| 4665 | value = FixedArray::cast(array)->get(i); |
| 4666 | } |
| 4667 | if (!value->IsTheHole()) { |
| 4668 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
| 4669 | MaybeObject* maybe_result = |
| 4670 | dictionary->AddNumberEntry(i, value, details); |
| 4671 | if (!maybe_result->To(&dictionary)) return maybe_result; |
| 4672 | } |
| 4673 | } |
| 4674 | return dictionary; |
| 4675 | } |
| 4676 | |
| 4677 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4678 | static Handle<SeededNumberDictionary> CopyFastElementsToDictionary( |
| 4679 | Handle<FixedArrayBase> array, |
| 4680 | int length, |
| 4681 | Handle<SeededNumberDictionary> dict) { |
| 4682 | Isolate* isolate = array->GetIsolate(); |
| 4683 | CALL_HEAP_FUNCTION(isolate, |
| 4684 | CopyFastElementsToDictionary( |
| 4685 | isolate, *array, length, *dict), |
| 4686 | SeededNumberDictionary); |
| 4687 | } |
| 4688 | |
| 4689 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4690 | Handle<SeededNumberDictionary> JSObject::NormalizeElements( |
| 4691 | Handle<JSObject> object) { |
| 4692 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 4693 | object->NormalizeElements(), |
| 4694 | SeededNumberDictionary); |
| 4695 | } |
| 4696 | |
| 4697 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4698 | MaybeObject* JSObject::NormalizeElements() { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4699 | ASSERT(!HasExternalArrayElements()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 4700 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4701 | // Find the backing store. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4702 | FixedArrayBase* array = FixedArrayBase::cast(elements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4703 | Map* old_map = array->map(); |
| 4704 | bool is_arguments = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4705 | (old_map == old_map->GetHeap()->non_strict_arguments_elements_map()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4706 | if (is_arguments) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4707 | array = FixedArrayBase::cast(FixedArray::cast(array)->get(1)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4708 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4709 | if (array->IsDictionary()) return array; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4710 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 4711 | ASSERT(HasFastSmiOrObjectElements() || |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4712 | HasFastDoubleElements() || |
| 4713 | HasFastArgumentsElements()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4714 | // Compute the effective length and allocate a new backing store. |
| 4715 | int length = IsJSArray() |
| 4716 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 4717 | : array->length(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 4718 | int old_capacity = 0; |
| 4719 | int used_elements = 0; |
| 4720 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4721 | SeededNumberDictionary* dictionary; |
| 4722 | MaybeObject* maybe_dictionary = |
| 4723 | SeededNumberDictionary::Allocate(GetHeap(), used_elements); |
| 4724 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4725 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4726 | maybe_dictionary = CopyFastElementsToDictionary( |
| 4727 | GetIsolate(), array, length, dictionary); |
| 4728 | if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4729 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4730 | // Switch to using the dictionary as the backing storage for elements. |
| 4731 | if (is_arguments) { |
| 4732 | FixedArray::cast(elements())->set(1, dictionary); |
| 4733 | } else { |
| 4734 | // Set the new map first to satify the elements type assert in |
| 4735 | // set_elements(). |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4736 | Map* new_map; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 4737 | MaybeObject* maybe = GetElementsTransitionMap(GetIsolate(), |
| 4738 | DICTIONARY_ELEMENTS); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 4739 | if (!maybe->To(&new_map)) return maybe; |
| 4740 | set_map(new_map); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4741 | set_elements(dictionary); |
| 4742 | } |
| 4743 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4744 | old_map->GetHeap()->isolate()->counters()->elements_to_dictionary()-> |
| 4745 | Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4746 | |
| 4747 | #ifdef DEBUG |
| 4748 | if (FLAG_trace_normalization) { |
| 4749 | PrintF("Object elements have been normalized:\n"); |
| 4750 | Print(); |
| 4751 | } |
| 4752 | #endif |
| 4753 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4754 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
| 4755 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4759 | Smi* JSReceiver::GenerateIdentityHash() { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4760 | Isolate* isolate = GetIsolate(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4761 | |
| 4762 | int hash_value; |
| 4763 | int attempts = 0; |
| 4764 | do { |
| 4765 | // Generate a random 32-bit hash value but limit range to fit |
| 4766 | // within a smi. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4767 | hash_value = isolate->random_number_generator()->NextInt() & Smi::kMaxValue; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4768 | attempts++; |
| 4769 | } while (hash_value == 0 && attempts < 30); |
| 4770 | hash_value = hash_value != 0 ? hash_value : 1; // never return 0 |
| 4771 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4772 | return Smi::FromInt(hash_value); |
| 4773 | } |
| 4774 | |
| 4775 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4776 | void JSObject::SetIdentityHash(Handle<JSObject> object, Handle<Smi> hash) { |
| 4777 | Isolate* isolate = object->GetIsolate(); |
| 4778 | SetHiddenProperty(object, isolate->factory()->identity_hash_string(), hash); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4782 | Object* JSObject::GetIdentityHash() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4783 | Object* stored_value = GetHiddenProperty(GetHeap()->identity_hash_string()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4784 | return stored_value->IsSmi() ? stored_value : GetHeap()->undefined_value(); |
| 4785 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4786 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4787 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4788 | Handle<Object> JSObject::GetOrCreateIdentityHash(Handle<JSObject> object) { |
| 4789 | Handle<Object> hash(object->GetIdentityHash(), object->GetIsolate()); |
| 4790 | if (hash->IsSmi()) |
| 4791 | return hash; |
| 4792 | |
| 4793 | Isolate* isolate = object->GetIsolate(); |
| 4794 | |
| 4795 | hash = handle(object->GenerateIdentityHash(), isolate); |
| 4796 | Handle<Object> result = SetHiddenProperty(object, |
| 4797 | isolate->factory()->identity_hash_string(), hash); |
| 4798 | |
| 4799 | if (result->IsUndefined()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4800 | // Trying to get hash of detached proxy. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4801 | return handle(Smi::FromInt(0), isolate); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4802 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4803 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4804 | return hash; |
| 4805 | } |
| 4806 | |
| 4807 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4808 | Object* JSProxy::GetIdentityHash() { |
| 4809 | return this->hash(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4813 | Handle<Object> JSProxy::GetOrCreateIdentityHash(Handle<JSProxy> proxy) { |
| 4814 | Isolate* isolate = proxy->GetIsolate(); |
| 4815 | |
| 4816 | Handle<Object> hash(proxy->GetIdentityHash(), isolate); |
| 4817 | if (hash->IsSmi()) |
| 4818 | return hash; |
| 4819 | |
| 4820 | hash = handle(proxy->GenerateIdentityHash(), isolate); |
| 4821 | proxy->set_hash(*hash); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4822 | return hash; |
| 4823 | } |
| 4824 | |
| 4825 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4826 | Object* JSObject::GetHiddenProperty(Name* key) { |
| 4827 | ASSERT(key->IsUniqueName()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4828 | if (IsJSGlobalProxy()) { |
| 4829 | // For a proxy, use the prototype as target object. |
| 4830 | Object* proxy_parent = GetPrototype(); |
| 4831 | // If the proxy is detached, return undefined. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4832 | if (proxy_parent->IsNull()) return GetHeap()->the_hole_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4833 | ASSERT(proxy_parent->IsJSGlobalObject()); |
| 4834 | return JSObject::cast(proxy_parent)->GetHiddenProperty(key); |
| 4835 | } |
| 4836 | ASSERT(!IsJSGlobalProxy()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4837 | Object* inline_value = GetHiddenPropertiesHashTable(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4838 | |
| 4839 | if (inline_value->IsSmi()) { |
| 4840 | // Handle inline-stored identity hash. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4841 | if (key == GetHeap()->identity_hash_string()) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4842 | return inline_value; |
| 4843 | } else { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4844 | return GetHeap()->the_hole_value(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4845 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4846 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4847 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4848 | if (inline_value->IsUndefined()) return GetHeap()->the_hole_value(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4849 | |
| 4850 | ObjectHashTable* hashtable = ObjectHashTable::cast(inline_value); |
| 4851 | Object* entry = hashtable->Lookup(key); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4852 | return entry; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4856 | Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4857 | Handle<Name> key, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4858 | Handle<Object> value) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4859 | Isolate* isolate = object->GetIsolate(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4860 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4861 | ASSERT(key->IsUniqueName()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4862 | if (object->IsJSGlobalProxy()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4863 | // For a proxy, use the prototype as target object. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4864 | Handle<Object> proxy_parent(object->GetPrototype(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4865 | // If the proxy is detached, return undefined. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4866 | if (proxy_parent->IsNull()) return isolate->factory()->undefined_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4867 | ASSERT(proxy_parent->IsJSGlobalObject()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4868 | return SetHiddenProperty(Handle<JSObject>::cast(proxy_parent), key, value); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4869 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4870 | ASSERT(!object->IsJSGlobalProxy()); |
| 4871 | |
| 4872 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4873 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4874 | // 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] | 4875 | if (value->IsSmi() && |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4876 | *key == *isolate->factory()->identity_hash_string() && |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4877 | (inline_value->IsUndefined() || inline_value->IsSmi())) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4878 | return JSObject::SetHiddenPropertiesHashTable(object, value); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4881 | Handle<ObjectHashTable> hashtable = |
| 4882 | GetOrCreateHiddenPropertiesHashtable(object); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4883 | |
| 4884 | // If it was found, check if the key is already in the dictionary. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4885 | Handle<ObjectHashTable> new_table = ObjectHashTable::Put(hashtable, key, |
| 4886 | value); |
| 4887 | if (*new_table != *hashtable) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4888 | // If adding the key expanded the dictionary (i.e., Add returned a new |
| 4889 | // dictionary), store it back to the object. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4890 | SetHiddenPropertiesHashTable(object, new_table); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4891 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4892 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4893 | // Return this to mark success. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4894 | return object; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4898 | void JSObject::DeleteHiddenProperty(Handle<JSObject> object, Handle<Name> key) { |
| 4899 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4900 | ASSERT(key->IsUniqueName()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4901 | |
| 4902 | if (object->IsJSGlobalProxy()) { |
| 4903 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 4904 | if (proto->IsNull()) return; |
| 4905 | ASSERT(proto->IsJSGlobalObject()); |
| 4906 | return DeleteHiddenProperty(Handle<JSObject>::cast(proto), key); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4907 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4908 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4909 | Object* inline_value = object->GetHiddenPropertiesHashTable(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4910 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4911 | // We never delete (inline-stored) identity hashes. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4912 | ASSERT(*key != *isolate->factory()->identity_hash_string()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4913 | if (inline_value->IsUndefined() || inline_value->IsSmi()) return; |
| 4914 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 4915 | Handle<ObjectHashTable> hashtable(ObjectHashTable::cast(inline_value)); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4916 | ObjectHashTable::Put(hashtable, key, isolate->factory()->the_hole_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | |
| 4920 | bool JSObject::HasHiddenProperties() { |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4921 | return GetPropertyAttributePostInterceptor(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4922 | GetHeap()->hidden_string(), |
erik.corry@gmail.com | b99cabf | 2011-10-07 09:45:13 +0000 | [diff] [blame] | 4923 | false) != ABSENT; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4927 | Object* JSObject::GetHiddenPropertiesHashTable() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4928 | ASSERT(!IsJSGlobalProxy()); |
| 4929 | if (HasFastProperties()) { |
| 4930 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4931 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 4932 | // 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] | 4933 | // code zero) it will always occupy the first entry if present. |
| 4934 | DescriptorArray* descriptors = this->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4935 | if (descriptors->number_of_descriptors() > 0) { |
| 4936 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4937 | if (descriptors->GetKey(sorted_index) == GetHeap()->hidden_string() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 4938 | sorted_index < map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4939 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4940 | ASSERT(descriptors->GetDetails(sorted_index).representation(). |
| 4941 | IsCompatibleForLoad(Representation::Tagged())); |
| 4942 | return this->RawFastPropertyAt( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 4943 | descriptors->GetFieldIndex(sorted_index)); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4944 | } else { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4945 | return GetHeap()->undefined_value(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 4946 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4947 | } else { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4948 | return GetHeap()->undefined_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4949 | } |
| 4950 | } else { |
| 4951 | PropertyAttributes attributes; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 4952 | // 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] | 4953 | // so we can be sure that GetLocalPropertyPostInterceptor returns a real |
| 4954 | // object. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4955 | return GetLocalPropertyPostInterceptor(this, |
| 4956 | GetHeap()->hidden_string(), |
| 4957 | &attributes)->ToObjectUnchecked(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4958 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4959 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4960 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4961 | Handle<ObjectHashTable> JSObject::GetOrCreateHiddenPropertiesHashtable( |
| 4962 | Handle<JSObject> object) { |
| 4963 | Isolate* isolate = object->GetIsolate(); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4964 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4965 | static const int kInitialCapacity = 4; |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4966 | Handle<Object> inline_value(object->GetHiddenPropertiesHashTable(), isolate); |
| 4967 | if (inline_value->IsHashTable()) { |
| 4968 | return Handle<ObjectHashTable>::cast(inline_value); |
| 4969 | } |
| 4970 | |
| 4971 | Handle<ObjectHashTable> hashtable = isolate->factory()->NewObjectHashTable( |
| 4972 | kInitialCapacity, |
| 4973 | USE_CUSTOM_MINIMUM_CAPACITY); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4974 | |
| 4975 | if (inline_value->IsSmi()) { |
| 4976 | // We were storing the identity hash inline and now allocated an actual |
| 4977 | // dictionary. Put the identity hash into the new dictionary. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4978 | hashtable = ObjectHashTable::Put(hashtable, |
| 4979 | isolate->factory()->identity_hash_string(), |
| 4980 | inline_value); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4983 | JSObject::SetLocalPropertyIgnoreAttributes( |
| 4984 | object, |
| 4985 | isolate->factory()->hidden_string(), |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 4986 | hashtable, |
| 4987 | DONT_ENUM, |
| 4988 | OPTIMAL_REPRESENTATION, |
| 4989 | ALLOW_AS_CONSTANT, |
| 4990 | OMIT_EXTENSIBILITY_CHECK); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4991 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 4992 | return hashtable; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 4996 | Handle<Object> JSObject::SetHiddenPropertiesHashTable(Handle<JSObject> object, |
| 4997 | Handle<Object> value) { |
| 4998 | ASSERT(!object->IsJSGlobalProxy()); |
| 4999 | |
| 5000 | Isolate* isolate = object->GetIsolate(); |
| 5001 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 5002 | // We can store the identity hash inline iff there is no backing store |
| 5003 | // for hidden properties yet. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5004 | ASSERT(object->HasHiddenProperties() != value->IsSmi()); |
| 5005 | if (object->HasFastProperties()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5006 | // If the object has fast properties, check whether the first slot |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 5007 | // in the descriptor array matches the hidden string. Since the |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5008 | // 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] | 5009 | // code zero) it will always occupy the first entry if present. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5010 | DescriptorArray* descriptors = object->map()->instance_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5011 | if (descriptors->number_of_descriptors() > 0) { |
| 5012 | int sorted_index = descriptors->GetSortedKeyIndex(0); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5013 | if (descriptors->GetKey(sorted_index) == isolate->heap()->hidden_string() |
| 5014 | && sorted_index < object->map()->NumberOfOwnDescriptors()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5015 | ASSERT(descriptors->GetType(sorted_index) == FIELD); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5016 | object->FastPropertyAtPut(descriptors->GetFieldIndex(sorted_index), |
| 5017 | *value); |
| 5018 | return object; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5019 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5020 | } |
| 5021 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5022 | |
| 5023 | SetLocalPropertyIgnoreAttributes(object, |
| 5024 | isolate->factory()->hidden_string(), |
| 5025 | value, |
| 5026 | DONT_ENUM, |
| 5027 | OPTIMAL_REPRESENTATION, |
| 5028 | ALLOW_AS_CONSTANT, |
| 5029 | OMIT_EXTENSIBILITY_CHECK); |
| 5030 | return object; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5034 | Handle<Object> JSObject::DeletePropertyPostInterceptor(Handle<JSObject> object, |
| 5035 | Handle<Name> name, |
| 5036 | DeleteMode mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5037 | // Check local property, ignore interceptor. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5038 | Isolate* isolate = object->GetIsolate(); |
| 5039 | LookupResult result(isolate); |
| 5040 | object->LocalLookupRealNamedProperty(*name, &result); |
| 5041 | if (!result.IsFound()) return isolate->factory()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5042 | |
| 5043 | // Normalize object if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5044 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5045 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5046 | return DeleteNormalizedProperty(object, name, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
| 5049 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5050 | Handle<Object> JSObject::DeletePropertyWithInterceptor(Handle<JSObject> object, |
| 5051 | Handle<Name> name) { |
| 5052 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5053 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5054 | // TODO(rossberg): Support symbols in the API. |
| 5055 | if (name->IsSymbol()) return isolate->factory()->false_value(); |
| 5056 | |
| 5057 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5058 | if (!interceptor->deleter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 5059 | v8::NamedPropertyDeleterCallback deleter = |
| 5060 | v8::ToCData<v8::NamedPropertyDeleterCallback>(interceptor->deleter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5061 | LOG(isolate, |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5062 | ApiNamedPropertyAccess("interceptor-named-delete", *object, *name)); |
| 5063 | PropertyCallbackArguments args( |
| 5064 | isolate, interceptor->data(), *object, *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5065 | v8::Handle<v8::Boolean> result = |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5066 | args.Call(deleter, v8::Utils::ToLocal(Handle<String>::cast(name))); |
| 5067 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5068 | if (!result.IsEmpty()) { |
| 5069 | ASSERT(result->IsBoolean()); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 5070 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 5071 | result_internal->VerifyApiCallResultType(); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5072 | // Rebox CustomArguments::kReturnValueOffset before returning. |
| 5073 | return handle(*result_internal, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5074 | } |
| 5075 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5076 | Handle<Object> result = |
| 5077 | DeletePropertyPostInterceptor(object, name, NORMAL_DELETION); |
| 5078 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5079 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5083 | // TODO(mstarzinger): Temporary wrapper until handlified. |
| 5084 | static Handle<Object> AccessorDelete(Handle<JSObject> object, |
| 5085 | uint32_t index, |
| 5086 | JSObject::DeleteMode mode) { |
| 5087 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 5088 | object->GetElementsAccessor()->Delete(*object, |
| 5089 | index, |
| 5090 | mode), |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5091 | Object); |
| 5092 | } |
| 5093 | |
| 5094 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5095 | Handle<Object> JSObject::DeleteElementWithInterceptor(Handle<JSObject> object, |
| 5096 | uint32_t index) { |
| 5097 | Isolate* isolate = object->GetIsolate(); |
| 5098 | Factory* factory = isolate->factory(); |
| 5099 | |
| 5100 | // Make sure that the top context does not change when doing |
| 5101 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 5102 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5103 | |
| 5104 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
| 5105 | if (interceptor->deleter()->IsUndefined()) return factory->false_value(); |
| 5106 | v8::IndexedPropertyDeleterCallback deleter = |
| 5107 | v8::ToCData<v8::IndexedPropertyDeleterCallback>(interceptor->deleter()); |
| 5108 | LOG(isolate, |
| 5109 | ApiIndexedPropertyAccess("interceptor-indexed-delete", *object, index)); |
| 5110 | PropertyCallbackArguments args( |
| 5111 | isolate, interceptor->data(), *object, *object); |
| 5112 | v8::Handle<v8::Boolean> result = args.Call(deleter, index); |
| 5113 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5114 | if (!result.IsEmpty()) { |
| 5115 | ASSERT(result->IsBoolean()); |
| 5116 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 5117 | result_internal->VerifyApiCallResultType(); |
| 5118 | // Rebox CustomArguments::kReturnValueOffset before returning. |
| 5119 | return handle(*result_internal, isolate); |
| 5120 | } |
| 5121 | Handle<Object> delete_result = AccessorDelete(object, index, NORMAL_DELETION); |
| 5122 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5123 | return delete_result; |
| 5124 | } |
| 5125 | |
| 5126 | |
| 5127 | Handle<Object> JSObject::DeleteElement(Handle<JSObject> object, |
| 5128 | uint32_t index, |
| 5129 | DeleteMode mode) { |
| 5130 | Isolate* isolate = object->GetIsolate(); |
| 5131 | Factory* factory = isolate->factory(); |
| 5132 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5133 | // Check access rights if needed. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5134 | if (object->IsAccessCheckNeeded() && |
| 5135 | !isolate->MayIndexedAccess(*object, index, v8::ACCESS_DELETE)) { |
| 5136 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_DELETE); |
| 5137 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5138 | return factory->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5141 | if (object->IsStringObjectWithCharacterAt(index)) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5142 | if (mode == STRICT_DELETION) { |
| 5143 | // Deleting a non-configurable property in strict mode. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5144 | Handle<Object> name = factory->NewNumberFromUint(index); |
| 5145 | Handle<Object> args[2] = { name, object }; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5146 | Handle<Object> error = |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5147 | factory->NewTypeError("strict_delete_property", |
| 5148 | HandleVector(args, 2)); |
| 5149 | isolate->Throw(*error); |
| 5150 | return Handle<Object>(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5151 | } |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5152 | return factory->false_value(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5155 | if (object->IsJSGlobalProxy()) { |
| 5156 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5157 | if (proto->IsNull()) return factory->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5158 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5159 | return DeleteElement(Handle<JSObject>::cast(proto), index, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5160 | } |
| 5161 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 5162 | Handle<Object> old_value; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5163 | bool should_enqueue_change_record = false; |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5164 | if (FLAG_harmony_observation && object->map()->is_observed()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5165 | should_enqueue_change_record = HasLocalElement(object, index); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 5166 | if (should_enqueue_change_record) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5167 | old_value = object->GetLocalElementAccessorPair(index) != NULL |
| 5168 | ? Handle<Object>::cast(factory->the_hole_value()) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 5169 | : Object::GetElement(isolate, object, index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5170 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5173 | // Skip interceptor if forcing deletion. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5174 | Handle<Object> result; |
| 5175 | if (object->HasIndexedInterceptor() && mode != FORCE_DELETION) { |
| 5176 | result = DeleteElementWithInterceptor(object, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5177 | } else { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5178 | result = AccessorDelete(object, index, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5181 | if (should_enqueue_change_record && !HasLocalElement(object, index)) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5182 | Handle<String> name = factory->Uint32ToString(index); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 5183 | EnqueueChangeRecord(object, "delete", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 5186 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5190 | Handle<Object> JSObject::DeleteProperty(Handle<JSObject> object, |
| 5191 | Handle<Name> name, |
| 5192 | DeleteMode mode) { |
| 5193 | Isolate* isolate = object->GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5194 | // ECMA-262, 3rd, 8.6.2.5 |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5195 | ASSERT(name->IsName()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5196 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5197 | // Check access rights if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5198 | if (object->IsAccessCheckNeeded() && |
| 5199 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_DELETE)) { |
| 5200 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_DELETE); |
| 5201 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5202 | return isolate->factory()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5205 | if (object->IsJSGlobalProxy()) { |
| 5206 | Object* proto = object->GetPrototype(); |
| 5207 | if (proto->IsNull()) return isolate->factory()->false_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5208 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5209 | return JSGlobalObject::DeleteProperty( |
| 5210 | handle(JSGlobalObject::cast(proto)), name, mode); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5211 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5212 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 5213 | uint32_t index = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5214 | if (name->AsArrayIndex(&index)) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5215 | return DeleteElement(object, index, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | LookupResult lookup(isolate); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5219 | object->LocalLookup(*name, &lookup, true); |
| 5220 | if (!lookup.IsFound()) return isolate->factory()->true_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5221 | // Ignore attributes if forcing a deletion. |
| 5222 | if (lookup.IsDontDelete() && mode != FORCE_DELETION) { |
| 5223 | if (mode == STRICT_DELETION) { |
| 5224 | // Deleting a non-configurable property in strict mode. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5225 | Handle<Object> args[2] = { name, object }; |
| 5226 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 5227 | "strict_delete_property", HandleVector(args, ARRAY_SIZE(args))); |
| 5228 | isolate->Throw(*error); |
| 5229 | return Handle<Object>(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5230 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5231 | return isolate->factory()->false_value(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 5234 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 5235 | bool is_observed = FLAG_harmony_observation && |
| 5236 | object->map()->is_observed() && |
| 5237 | *name != isolate->heap()->hidden_string(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 5238 | if (is_observed && lookup.IsDataProperty()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5239 | old_value = Object::GetProperty(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5240 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5241 | Handle<Object> result; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5242 | |
| 5243 | // Check for interceptor. |
| 5244 | if (lookup.IsInterceptor()) { |
| 5245 | // Skip interceptor if forcing a deletion. |
| 5246 | if (mode == FORCE_DELETION) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5247 | result = DeletePropertyPostInterceptor(object, name, mode); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5248 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5249 | result = DeletePropertyWithInterceptor(object, name); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5251 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5252 | // Normalize object if needed. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5253 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5254 | // Make sure the properties are normalized before removing the entry. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5255 | result = DeleteNormalizedProperty(object, name, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5256 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5257 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5258 | if (is_observed && !HasLocalProperty(object, name)) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 5259 | EnqueueChangeRecord(object, "delete", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5262 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5266 | Handle<Object> JSReceiver::DeleteElement(Handle<JSReceiver> object, |
| 5267 | uint32_t index, |
| 5268 | DeleteMode mode) { |
| 5269 | if (object->IsJSProxy()) { |
| 5270 | return JSProxy::DeleteElementWithHandler( |
| 5271 | Handle<JSProxy>::cast(object), index, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5272 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5273 | return JSObject::DeleteElement(Handle<JSObject>::cast(object), index, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5277 | Handle<Object> JSReceiver::DeleteProperty(Handle<JSReceiver> object, |
| 5278 | Handle<Name> name, |
| 5279 | DeleteMode mode) { |
| 5280 | if (object->IsJSProxy()) { |
| 5281 | return JSProxy::DeletePropertyWithHandler( |
| 5282 | Handle<JSProxy>::cast(object), name, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5283 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 5284 | return JSObject::DeleteProperty(Handle<JSObject>::cast(object), name, mode); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5288 | bool JSObject::ReferencesObjectFromElements(FixedArray* elements, |
| 5289 | ElementsKind kind, |
| 5290 | Object* object) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5291 | ASSERT(IsFastObjectElementsKind(kind) || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5292 | kind == DICTIONARY_ELEMENTS); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5293 | if (IsFastObjectElementsKind(kind)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5294 | int length = IsJSArray() |
| 5295 | ? Smi::cast(JSArray::cast(this)->length())->value() |
| 5296 | : elements->length(); |
| 5297 | for (int i = 0; i < length; ++i) { |
| 5298 | Object* element = elements->get(i); |
| 5299 | if (!element->IsTheHole() && element == object) return true; |
| 5300 | } |
| 5301 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5302 | Object* key = |
| 5303 | SeededNumberDictionary::cast(elements)->SlowReverseLookup(object); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5304 | if (!key->IsUndefined()) return true; |
| 5305 | } |
| 5306 | return false; |
| 5307 | } |
| 5308 | |
| 5309 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5310 | // Check whether this object references another object. |
| 5311 | bool JSObject::ReferencesObject(Object* obj) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5312 | Map* map_of_this = map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5313 | Heap* heap = GetHeap(); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 5314 | DisallowHeapAllocation no_allocation; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5315 | |
| 5316 | // Is the object the constructor for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5317 | if (map_of_this->constructor() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5318 | return true; |
| 5319 | } |
| 5320 | |
| 5321 | // Is the object the prototype for this object? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5322 | if (map_of_this->prototype() == obj) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5323 | return true; |
| 5324 | } |
| 5325 | |
| 5326 | // Check if the object is among the named properties. |
| 5327 | Object* key = SlowReverseLookup(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5328 | if (!key->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5329 | return true; |
| 5330 | } |
| 5331 | |
| 5332 | // Check if the object is among the indexed properties. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5333 | ElementsKind kind = GetElementsKind(); |
| 5334 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5335 | case EXTERNAL_PIXEL_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5336 | case EXTERNAL_BYTE_ELEMENTS: |
| 5337 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5338 | case EXTERNAL_SHORT_ELEMENTS: |
| 5339 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5340 | case EXTERNAL_INT_ELEMENTS: |
| 5341 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5342 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 5343 | case EXTERNAL_DOUBLE_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5344 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5345 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5346 | // Raw pixels and external arrays do not reference other |
| 5347 | // objects. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5348 | break; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5349 | case FAST_SMI_ELEMENTS: |
| 5350 | case FAST_HOLEY_SMI_ELEMENTS: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5351 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5352 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5353 | case FAST_HOLEY_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5354 | case DICTIONARY_ELEMENTS: { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5355 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 5356 | if (ReferencesObjectFromElements(elements, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5357 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5358 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5359 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 5360 | FixedArray* parameter_map = FixedArray::cast(elements()); |
| 5361 | // Check the mapped parameters. |
| 5362 | int length = parameter_map->length(); |
| 5363 | for (int i = 2; i < length; ++i) { |
| 5364 | Object* value = parameter_map->get(i); |
| 5365 | if (!value->IsTheHole() && value == obj) return true; |
| 5366 | } |
| 5367 | // Check the arguments. |
| 5368 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 5369 | kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : |
| 5370 | FAST_HOLEY_ELEMENTS; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5371 | if (ReferencesObjectFromElements(arguments, kind, obj)) return true; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 5372 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 5373 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5376 | // For functions check the context. |
| 5377 | if (IsJSFunction()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5378 | // Get the constructor function for arguments array. |
| 5379 | JSObject* arguments_boilerplate = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5380 | heap->isolate()->context()->native_context()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5381 | arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5382 | JSFunction* arguments_function = |
| 5383 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 5384 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5385 | // 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] | 5386 | JSFunction* f = JSFunction::cast(this); |
| 5387 | Context* context = f->context(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5388 | if (context->IsNativeContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5389 | return false; |
| 5390 | } |
| 5391 | |
| 5392 | // Check the non-special context slots. |
| 5393 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 5394 | // Only check JS objects. |
| 5395 | if (context->get(i)->IsJSObject()) { |
| 5396 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 5397 | // If it is an arguments array check the content. |
| 5398 | if (ctxobj->map()->constructor() == arguments_function) { |
| 5399 | if (ctxobj->ReferencesObject(obj)) { |
| 5400 | return true; |
| 5401 | } |
| 5402 | } else if (ctxobj == obj) { |
| 5403 | return true; |
| 5404 | } |
| 5405 | } |
| 5406 | } |
| 5407 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5408 | // Check the context extension (if any) if it can have references. |
| 5409 | if (context->has_extension() && !context->IsCatchContext()) { |
| 5410 | return JSObject::cast(context->extension())->ReferencesObject(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | // No references to object. |
| 5415 | return false; |
| 5416 | } |
| 5417 | |
| 5418 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5419 | Handle<Object> JSObject::PreventExtensions(Handle<JSObject> object) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5420 | Isolate* isolate = object->GetIsolate(); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5421 | |
| 5422 | if (!object->map()->is_extensible()) return object; |
| 5423 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5424 | if (object->IsAccessCheckNeeded() && |
| 5425 | !isolate->MayNamedAccess(*object, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5426 | isolate->heap()->undefined_value(), |
| 5427 | v8::ACCESS_KEYS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5428 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5429 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5430 | return isolate->factory()->false_value(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5433 | if (object->IsJSGlobalProxy()) { |
| 5434 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5435 | if (proto->IsNull()) return object; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 5436 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5437 | return PreventExtensions(Handle<JSObject>::cast(proto)); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5440 | // It's not possible to seal objects with external array elements |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5441 | if (object->HasExternalArrayElements()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5442 | Handle<Object> error = |
| 5443 | isolate->factory()->NewTypeError( |
| 5444 | "cant_prevent_ext_external_array_elements", |
| 5445 | HandleVector(&object, 1)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5446 | isolate->Throw(*error); |
| 5447 | return Handle<Object>(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5450 | // If there are fast elements we normalize. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5451 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 5452 | ASSERT(object->HasDictionaryElements() || |
| 5453 | object->HasDictionaryArgumentsElements()); |
| 5454 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5455 | // Make sure that we never go back to fast case. |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5456 | dictionary->set_requires_slow_elements(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Do a map transition, other objects with this map may still |
| 5459 | // be extensible. |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5460 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5461 | Handle<Map> new_map = Map::Copy(handle(object->map())); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 5462 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 5463 | new_map->set_is_extensible(false); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5464 | object->set_map(*new_map); |
| 5465 | ASSERT(!object->map()->is_extensible()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5466 | |
| 5467 | if (FLAG_harmony_observation && object->map()->is_observed()) { |
| 5468 | EnqueueChangeRecord(object, "preventExtensions", Handle<Name>(), |
| 5469 | isolate->factory()->the_hole_value()); |
| 5470 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5471 | return object; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 5472 | } |
| 5473 | |
| 5474 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5475 | template<typename Dictionary> |
| 5476 | static void FreezeDictionary(Dictionary* dictionary) { |
| 5477 | int capacity = dictionary->Capacity(); |
| 5478 | for (int i = 0; i < capacity; i++) { |
| 5479 | Object* k = dictionary->KeyAt(i); |
| 5480 | if (dictionary->IsKey(k)) { |
| 5481 | PropertyDetails details = dictionary->DetailsAt(i); |
| 5482 | int attrs = DONT_DELETE; |
| 5483 | // READ_ONLY is an invalid attribute for JS setters/getters. |
| 5484 | if (details.type() != CALLBACKS || |
| 5485 | !dictionary->ValueAt(i)->IsAccessorPair()) { |
| 5486 | attrs |= READ_ONLY; |
| 5487 | } |
| 5488 | details = details.CopyAddAttributes( |
| 5489 | static_cast<PropertyAttributes>(attrs)); |
| 5490 | dictionary->DetailsAtPut(i, details); |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5496 | Handle<Object> JSObject::Freeze(Handle<JSObject> object) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5497 | // Freezing non-strict arguments should be handled elsewhere. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5498 | ASSERT(!object->HasNonStrictArgumentsElements()); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5499 | ASSERT(!object->map()->is_observed()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5500 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5501 | if (object->map()->is_frozen()) return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5502 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5503 | Isolate* isolate = object->GetIsolate(); |
| 5504 | if (object->IsAccessCheckNeeded() && |
| 5505 | !isolate->MayNamedAccess(*object, |
| 5506 | isolate->heap()->undefined_value(), |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5507 | v8::ACCESS_KEYS)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5508 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5509 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 5510 | return isolate->factory()->false_value(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5513 | if (object->IsJSGlobalProxy()) { |
| 5514 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 5515 | if (proto->IsNull()) return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5516 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5517 | return Freeze(Handle<JSObject>::cast(proto)); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | // It's not possible to freeze objects with external array elements |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5521 | if (object->HasExternalArrayElements()) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5522 | Handle<Object> error = |
| 5523 | isolate->factory()->NewTypeError( |
| 5524 | "cant_prevent_ext_external_array_elements", |
| 5525 | HandleVector(&object, 1)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5526 | isolate->Throw(*error); |
| 5527 | return Handle<Object>(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5530 | Handle<SeededNumberDictionary> new_element_dictionary; |
| 5531 | if (!object->elements()->IsDictionary()) { |
| 5532 | int length = object->IsJSArray() |
| 5533 | ? Smi::cast(Handle<JSArray>::cast(object)->length())->value() |
| 5534 | : object->elements()->length(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5535 | if (length > 0) { |
| 5536 | int capacity = 0; |
| 5537 | int used = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5538 | object->GetElementsCapacityAndUsage(&capacity, &used); |
| 5539 | new_element_dictionary = |
| 5540 | isolate->factory()->NewSeededNumberDictionary(used); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5541 | |
| 5542 | // Move elements to a dictionary; avoid calling NormalizeElements to avoid |
| 5543 | // unnecessary transitions. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5544 | new_element_dictionary = CopyFastElementsToDictionary( |
| 5545 | handle(object->elements()), length, new_element_dictionary); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5546 | } else { |
| 5547 | // No existing elements, use a pre-allocated empty backing store |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5548 | new_element_dictionary = |
| 5549 | isolate->factory()->empty_slow_element_dictionary(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | LookupResult result(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5554 | Handle<Map> old_map(object->map()); |
| 5555 | old_map->LookupTransition(*object, isolate->heap()->frozen_symbol(), &result); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5556 | if (result.IsTransition()) { |
| 5557 | Map* transition_map = result.GetTransitionTarget(); |
| 5558 | ASSERT(transition_map->has_dictionary_elements()); |
| 5559 | ASSERT(transition_map->is_frozen()); |
| 5560 | ASSERT(!transition_map->is_extensible()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5561 | object->set_map(transition_map); |
| 5562 | } else if (object->HasFastProperties() && old_map->CanHaveMoreTransitions()) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5563 | // Create a new descriptor array with fully-frozen properties |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5564 | int num_descriptors = old_map->NumberOfOwnDescriptors(); |
| 5565 | Handle<DescriptorArray> new_descriptors = |
| 5566 | DescriptorArray::CopyUpToAddAttributes( |
| 5567 | handle(old_map->instance_descriptors()), num_descriptors, FROZEN); |
| 5568 | Handle<Map> new_map = Map::CopyReplaceDescriptors( |
| 5569 | old_map, new_descriptors, INSERT_TRANSITION, |
| 5570 | isolate->factory()->frozen_symbol()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5571 | new_map->freeze(); |
| 5572 | new_map->set_is_extensible(false); |
| 5573 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5574 | object->set_map(*new_map); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5575 | } else { |
| 5576 | // Slow path: need to normalize properties for safety |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5577 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5578 | |
| 5579 | // Create a new map, since other objects with this map may be extensible. |
| 5580 | // TODO(adamk): Extend the NormalizedMapCache to handle non-extensible maps. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5581 | Handle<Map> new_map = Map::Copy(handle(object->map())); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5582 | new_map->freeze(); |
| 5583 | new_map->set_is_extensible(false); |
| 5584 | new_map->set_elements_kind(DICTIONARY_ELEMENTS); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5585 | object->set_map(*new_map); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5586 | |
| 5587 | // Freeze dictionary-mode properties |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5588 | FreezeDictionary(object->property_dictionary()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5591 | ASSERT(object->map()->has_dictionary_elements()); |
| 5592 | if (!new_element_dictionary.is_null()) { |
| 5593 | object->set_elements(*new_element_dictionary); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5596 | if (object->elements() != isolate->heap()->empty_slow_element_dictionary()) { |
| 5597 | SeededNumberDictionary* dictionary = object->element_dictionary(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5598 | // Make sure we never go back to the fast case |
| 5599 | dictionary->set_requires_slow_elements(); |
| 5600 | // Freeze all elements in the dictionary |
| 5601 | FreezeDictionary(dictionary); |
| 5602 | } |
| 5603 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5604 | return object; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5608 | void JSObject::SetObserved(Handle<JSObject> object) { |
| 5609 | Isolate* isolate = object->GetIsolate(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5610 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5611 | if (object->map()->is_observed()) |
| 5612 | return; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5613 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5614 | LookupResult result(isolate); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5615 | object->map()->LookupTransition(*object, |
| 5616 | isolate->heap()->observed_symbol(), |
| 5617 | &result); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5618 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5619 | Handle<Map> new_map; |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5620 | if (result.IsTransition()) { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5621 | new_map = handle(result.GetTransitionTarget()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5622 | ASSERT(new_map->is_observed()); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5623 | } else if (object->map()->CanHaveMoreTransitions()) { |
| 5624 | new_map = Map::CopyForObserved(handle(object->map())); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5625 | } else { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5626 | new_map = Map::Copy(handle(object->map())); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 5627 | new_map->set_is_observed(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5628 | } |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5629 | object->set_map(*new_map); |
| 5630 | } |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5631 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5632 | |
| 5633 | Handle<JSObject> JSObject::Copy(Handle<JSObject> object, |
| 5634 | Handle<AllocationSite> site) { |
| 5635 | Isolate* isolate = object->GetIsolate(); |
| 5636 | CALL_HEAP_FUNCTION(isolate, |
| 5637 | isolate->heap()->CopyJSObject(*object, *site), JSObject); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 5638 | } |
| 5639 | |
| 5640 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5641 | Handle<JSObject> JSObject::Copy(Handle<JSObject> object) { |
| 5642 | Isolate* isolate = object->GetIsolate(); |
| 5643 | CALL_HEAP_FUNCTION(isolate, |
| 5644 | isolate->heap()->CopyJSObject(*object), JSObject); |
| 5645 | } |
| 5646 | |
| 5647 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5648 | class JSObjectWalkVisitor { |
| 5649 | public: |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5650 | explicit JSObjectWalkVisitor(AllocationSiteContext* site_context) : |
| 5651 | site_context_(site_context) {} |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5652 | virtual ~JSObjectWalkVisitor() {} |
| 5653 | |
| 5654 | Handle<JSObject> Visit(Handle<JSObject> object) { |
| 5655 | return StructureWalk(object); |
| 5656 | } |
| 5657 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5658 | virtual bool is_copying() = 0; |
| 5659 | |
| 5660 | protected: |
| 5661 | Handle<JSObject> StructureWalk(Handle<JSObject> object); |
| 5662 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5663 | // The returned handle will be used for the object in all subsequent usages. |
| 5664 | // 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] | 5665 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) = 0; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5666 | virtual Handle<JSObject> VisitElementOrProperty(Handle<JSObject> object, |
| 5667 | Handle<JSObject> value) = 0; |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5668 | |
| 5669 | AllocationSiteContext* site_context() { return site_context_; } |
| 5670 | |
| 5671 | private: |
| 5672 | AllocationSiteContext* site_context_; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5673 | }; |
| 5674 | |
| 5675 | |
| 5676 | class JSObjectCopyVisitor: public JSObjectWalkVisitor { |
| 5677 | public: |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5678 | explicit JSObjectCopyVisitor(AllocationSiteContext* site_context) |
| 5679 | : JSObjectWalkVisitor(site_context) {} |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5680 | |
| 5681 | virtual bool is_copying() V8_OVERRIDE { return true; } |
| 5682 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5683 | // The returned handle will be used for the object in all |
| 5684 | // subsequent usages. This allows VisitObject to make a copy |
| 5685 | // of the object if desired. |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5686 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) V8_OVERRIDE { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5687 | // Only create a memento if |
| 5688 | // 1) we have a JSArray, and |
| 5689 | // 2) the elements kind is palatable |
| 5690 | // 3) allow_mementos is true |
| 5691 | Handle<JSObject> copy; |
| 5692 | if (site_context()->activated() && |
| 5693 | AllocationSite::CanTrack(object->map()->instance_type()) && |
| 5694 | AllocationSite::GetMode(object->GetElementsKind()) == |
| 5695 | TRACK_ALLOCATION_SITE) { |
| 5696 | copy = JSObject::Copy(object, site_context()->current()); |
| 5697 | } else { |
| 5698 | copy = JSObject::Copy(object); |
| 5699 | } |
| 5700 | |
| 5701 | return copy; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | virtual Handle<JSObject> VisitElementOrProperty( |
| 5705 | Handle<JSObject> object, |
| 5706 | Handle<JSObject> value) V8_OVERRIDE { |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5707 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 5708 | Handle<JSObject> copy_of_value = StructureWalk(value); |
| 5709 | site_context()->ExitScope(current_site, value); |
| 5710 | return copy_of_value; |
| 5711 | } |
| 5712 | }; |
| 5713 | |
| 5714 | |
| 5715 | class JSObjectCreateAllocationSitesVisitor: public JSObjectWalkVisitor { |
| 5716 | public: |
| 5717 | explicit JSObjectCreateAllocationSitesVisitor( |
| 5718 | AllocationSiteContext* site_context) |
| 5719 | : JSObjectWalkVisitor(site_context) {} |
| 5720 | |
| 5721 | virtual bool is_copying() V8_OVERRIDE { return false; } |
| 5722 | |
| 5723 | // The returned handle will be used for the object in all |
| 5724 | // subsequent usages. This allows VisitObject to make a copy |
| 5725 | // of the object if desired. |
| 5726 | virtual Handle<JSObject> VisitObject(Handle<JSObject> object) V8_OVERRIDE { |
| 5727 | return object; |
| 5728 | } |
| 5729 | |
| 5730 | virtual Handle<JSObject> VisitElementOrProperty( |
| 5731 | Handle<JSObject> object, |
| 5732 | Handle<JSObject> value) V8_OVERRIDE { |
| 5733 | Handle<AllocationSite> current_site = site_context()->EnterNewScope(); |
| 5734 | value = StructureWalk(value); |
| 5735 | site_context()->ExitScope(current_site, value); |
| 5736 | return value; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5737 | } |
| 5738 | }; |
| 5739 | |
| 5740 | |
| 5741 | Handle<JSObject> JSObjectWalkVisitor::StructureWalk(Handle<JSObject> object) { |
| 5742 | bool copying = is_copying(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5743 | Isolate* isolate = object->GetIsolate(); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5744 | StackLimitCheck check(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5745 | if (check.HasOverflowed()) { |
| 5746 | isolate->StackOverflow(); |
| 5747 | return Handle<JSObject>::null(); |
mvstanton@chromium.org | 5a4733b | 2013-09-16 07:33:24 +0000 | [diff] [blame] | 5748 | } |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5749 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5750 | if (object->map()->is_deprecated()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5751 | JSObject::MigrateInstance(object); |
hpayer@chromium.org | 4f626d1 | 2013-09-18 07:47:45 +0000 | [diff] [blame] | 5752 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5753 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5754 | Handle<JSObject> copy = VisitObject(object); |
| 5755 | ASSERT(copying || copy.is_identical_to(object)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5756 | |
| 5757 | HandleScope scope(isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5758 | |
| 5759 | // Deep copy local properties. |
| 5760 | if (copy->HasFastProperties()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5761 | Handle<DescriptorArray> descriptors(copy->map()->instance_descriptors()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5762 | int limit = copy->map()->NumberOfOwnDescriptors(); |
| 5763 | for (int i = 0; i < limit; i++) { |
| 5764 | PropertyDetails details = descriptors->GetDetails(i); |
| 5765 | if (details.type() != FIELD) continue; |
| 5766 | int index = descriptors->GetFieldIndex(i); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5767 | Handle<Object> value(object->RawFastPropertyAt(index), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5768 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5769 | value = VisitElementOrProperty(copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5770 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, value, Handle<JSObject>()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5771 | } else { |
| 5772 | Representation representation = details.representation(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5773 | value = NewStorageFor(isolate, value, representation); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5774 | } |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5775 | if (copying) { |
| 5776 | copy->FastPropertyAtPut(index, *value); |
| 5777 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5778 | } |
| 5779 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5780 | Handle<FixedArray> names = |
| 5781 | isolate->factory()->NewFixedArray(copy->NumberOfLocalProperties()); |
| 5782 | copy->GetLocalPropertyNames(*names, 0); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5783 | for (int i = 0; i < names->length(); i++) { |
| 5784 | ASSERT(names->get(i)->IsString()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5785 | Handle<String> key_string(String::cast(names->get(i))); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5786 | PropertyAttributes attributes = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5787 | copy->GetLocalPropertyAttribute(*key_string); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5788 | // Only deep copy fields from the object literal expression. |
| 5789 | // In particular, don't try to copy the length attribute of |
| 5790 | // an array. |
| 5791 | if (attributes != NONE) continue; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5792 | Handle<Object> value( |
| 5793 | copy->GetProperty(*key_string, &attributes)->ToObjectUnchecked(), |
| 5794 | isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5795 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5796 | Handle<JSObject> result = VisitElementOrProperty( |
| 5797 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5798 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5799 | if (copying) { |
| 5800 | // Creating object copy for literals. No strict mode needed. |
| 5801 | CHECK_NOT_EMPTY_HANDLE(isolate, JSObject::SetProperty( |
| 5802 | copy, key_string, result, NONE, kNonStrictMode)); |
| 5803 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5804 | } |
| 5805 | } |
| 5806 | } |
| 5807 | |
| 5808 | // Deep copy local elements. |
| 5809 | // Pixel elements cannot be created using an object literal. |
| 5810 | ASSERT(!copy->HasExternalArrayElements()); |
| 5811 | switch (copy->GetElementsKind()) { |
| 5812 | case FAST_SMI_ELEMENTS: |
| 5813 | case FAST_ELEMENTS: |
| 5814 | case FAST_HOLEY_SMI_ELEMENTS: |
| 5815 | case FAST_HOLEY_ELEMENTS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5816 | Handle<FixedArray> elements(FixedArray::cast(copy->elements())); |
| 5817 | if (elements->map() == isolate->heap()->fixed_cow_array_map()) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 5818 | if (copying) { |
| 5819 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 5820 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5821 | #ifdef DEBUG |
| 5822 | for (int i = 0; i < elements->length(); i++) { |
| 5823 | ASSERT(!elements->get(i)->IsJSObject()); |
| 5824 | } |
| 5825 | #endif |
| 5826 | } else { |
| 5827 | for (int i = 0; i < elements->length(); i++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5828 | Handle<Object> value(elements->get(i), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5829 | ASSERT(value->IsSmi() || |
| 5830 | value->IsTheHole() || |
| 5831 | (IsFastObjectElementsKind(copy->GetElementsKind()))); |
| 5832 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5833 | Handle<JSObject> result = VisitElementOrProperty( |
| 5834 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5835 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5836 | if (copying) { |
| 5837 | elements->set(i, *result); |
| 5838 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5839 | } |
| 5840 | } |
| 5841 | } |
| 5842 | break; |
| 5843 | } |
| 5844 | case DICTIONARY_ELEMENTS: { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5845 | Handle<SeededNumberDictionary> element_dictionary( |
| 5846 | copy->element_dictionary()); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5847 | int capacity = element_dictionary->Capacity(); |
| 5848 | for (int i = 0; i < capacity; i++) { |
| 5849 | Object* k = element_dictionary->KeyAt(i); |
| 5850 | if (element_dictionary->IsKey(k)) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5851 | Handle<Object> value(element_dictionary->ValueAt(i), isolate); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5852 | if (value->IsJSObject()) { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5853 | Handle<JSObject> result = VisitElementOrProperty( |
| 5854 | copy, Handle<JSObject>::cast(value)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5855 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<JSObject>()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5856 | if (copying) { |
| 5857 | element_dictionary->ValueAtPut(i, *result); |
| 5858 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 5859 | } |
| 5860 | } |
| 5861 | } |
| 5862 | break; |
| 5863 | } |
| 5864 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 5865 | UNIMPLEMENTED(); |
| 5866 | break; |
| 5867 | case EXTERNAL_PIXEL_ELEMENTS: |
| 5868 | case EXTERNAL_BYTE_ELEMENTS: |
| 5869 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5870 | case EXTERNAL_SHORT_ELEMENTS: |
| 5871 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5872 | case EXTERNAL_INT_ELEMENTS: |
| 5873 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5874 | case EXTERNAL_FLOAT_ELEMENTS: |
| 5875 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 5876 | case FAST_DOUBLE_ELEMENTS: |
| 5877 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 5878 | // No contained objects, nothing to do. |
| 5879 | break; |
| 5880 | } |
| 5881 | return copy; |
| 5882 | } |
| 5883 | |
| 5884 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 5885 | Handle<JSObject> JSObject::DeepWalk(Handle<JSObject> object, |
| 5886 | AllocationSiteContext* site_context) { |
| 5887 | JSObjectCreateAllocationSitesVisitor v(site_context); |
| 5888 | Handle<JSObject> result = v.Visit(object); |
| 5889 | ASSERT(!v.is_copying() && |
| 5890 | (result.is_null() || result.is_identical_to(object))); |
| 5891 | return result; |
| 5892 | } |
| 5893 | |
| 5894 | |
| 5895 | Handle<JSObject> JSObject::DeepCopy(Handle<JSObject> object, |
| 5896 | AllocationSiteContext* site_context) { |
| 5897 | JSObjectCopyVisitor v(site_context); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 5898 | Handle<JSObject> copy = v.Visit(object); |
| 5899 | ASSERT(v.is_copying() && !copy.is_identical_to(object)); |
| 5900 | return copy; |
| 5901 | } |
| 5902 | |
| 5903 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5904 | // Tests for the fast common case for property enumeration: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5905 | // - This object and all prototypes has an enum cache (which means that |
| 5906 | // 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] | 5907 | // - This object has no elements. |
| 5908 | // - No prototype has enumerable properties/elements. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5909 | bool JSReceiver::IsSimpleEnum() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5910 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5911 | for (Object* o = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5912 | o != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5913 | o = JSObject::cast(o)->GetPrototype()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5914 | if (!o->IsJSObject()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5915 | JSObject* curr = JSObject::cast(o); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5916 | int enum_length = curr->map()->EnumLength(); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 5917 | if (enum_length == kInvalidEnumCacheSentinel) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 5918 | ASSERT(!curr->HasNamedInterceptor()); |
| 5919 | ASSERT(!curr->HasIndexedInterceptor()); |
| 5920 | ASSERT(!curr->IsAccessCheckNeeded()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5921 | if (curr->NumberOfEnumElements() > 0) return false; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 5922 | if (curr != this && enum_length != 0) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5923 | } |
| 5924 | return true; |
| 5925 | } |
| 5926 | |
| 5927 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5928 | int Map::NumberOfDescribedProperties(DescriptorFlag which, |
| 5929 | PropertyAttributes filter) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5930 | int result = 0; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5931 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5932 | int limit = which == ALL_DESCRIPTORS |
| 5933 | ? descs->number_of_descriptors() |
| 5934 | : NumberOfOwnDescriptors(); |
| 5935 | for (int i = 0; i < limit; i++) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5936 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 5937 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 5938 | result++; |
| 5939 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5940 | } |
| 5941 | return result; |
| 5942 | } |
| 5943 | |
| 5944 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5945 | int Map::NextFreePropertyIndex() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5946 | int max_index = -1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5947 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5948 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5949 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5950 | if (descs->GetType(i) == FIELD) { |
| 5951 | int current_index = descs->GetFieldIndex(i); |
| 5952 | if (current_index > max_index) max_index = current_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5953 | } |
| 5954 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5955 | return max_index + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5956 | } |
| 5957 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5958 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5959 | AccessorDescriptor* Map::FindAccessor(Name* name) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5960 | DescriptorArray* descs = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 5961 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 5962 | for (int i = 0; i < number_of_own_descriptors; i++) { |
| 5963 | if (descs->GetType(i) == CALLBACKS && name->Equals(descs->GetKey(i))) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 5964 | return descs->GetCallbacks(i); |
| 5965 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5966 | } |
| 5967 | return NULL; |
| 5968 | } |
| 5969 | |
| 5970 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5971 | void JSReceiver::LocalLookup( |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 5972 | Name* name, LookupResult* result, bool search_hidden_prototypes) { |
| 5973 | ASSERT(name->IsName()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5974 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5975 | Heap* heap = GetHeap(); |
| 5976 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5977 | if (IsJSGlobalProxy()) { |
| 5978 | Object* proto = GetPrototype(); |
| 5979 | if (proto->IsNull()) return result->NotFound(); |
| 5980 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 5981 | return JSReceiver::cast(proto)->LocalLookup( |
| 5982 | name, result, search_hidden_prototypes); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | if (IsJSProxy()) { |
| 5986 | result->HandlerResult(JSProxy::cast(this)); |
| 5987 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5988 | } |
| 5989 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5990 | // Do not use inline caching if the object is a non-global object |
| 5991 | // that requires access checks. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5992 | if (IsAccessCheckNeeded()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5993 | result->DisallowCaching(); |
| 5994 | } |
| 5995 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5996 | JSObject* js_object = JSObject::cast(this); |
| 5997 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5998 | // Check for lookup interceptor except when bootstrapping. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5999 | if (js_object->HasNamedInterceptor() && |
| 6000 | !heap->isolate()->bootstrapper()->IsActive()) { |
| 6001 | result->InterceptorResult(js_object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6002 | return; |
| 6003 | } |
| 6004 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6005 | js_object->LocalLookupRealNamedProperty(name, result); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 6006 | if (result->IsFound() || !search_hidden_prototypes) return; |
| 6007 | |
| 6008 | Object* proto = js_object->GetPrototype(); |
| 6009 | if (!proto->IsJSReceiver()) return; |
| 6010 | JSReceiver* receiver = JSReceiver::cast(proto); |
| 6011 | if (receiver->map()->is_hidden_prototype()) { |
| 6012 | receiver->LocalLookup(name, result, search_hidden_prototypes); |
| 6013 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6017 | void JSReceiver::Lookup(Name* name, LookupResult* result) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6018 | // Ecma-262 3rd 8.6.2.4 |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6019 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6020 | for (Object* current = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6021 | current != heap->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6022 | current = JSObject::cast(current)->GetPrototype()) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 6023 | JSReceiver::cast(current)->LocalLookup(name, result, false); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6024 | if (result->IsFound()) return; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6025 | } |
| 6026 | result->NotFound(); |
| 6027 | } |
| 6028 | |
| 6029 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6030 | // Search object and its prototype chain for callback properties. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6031 | void JSObject::LookupCallbackProperty(Name* name, LookupResult* result) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6032 | Heap* heap = GetHeap(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6033 | for (Object* current = this; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 6034 | current != heap->null_value() && current->IsJSObject(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6035 | current = JSObject::cast(current)->GetPrototype()) { |
| 6036 | JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6037 | if (result->IsPropertyCallbacks()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6038 | } |
| 6039 | result->NotFound(); |
| 6040 | } |
| 6041 | |
| 6042 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6043 | // Try to update an accessor in an elements dictionary. Return true if the |
| 6044 | // update succeeded, and false otherwise. |
| 6045 | static bool UpdateGetterSetterInDictionary( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6046 | SeededNumberDictionary* dictionary, |
| 6047 | uint32_t index, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6048 | Object* getter, |
| 6049 | Object* setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6050 | PropertyAttributes attributes) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6051 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6052 | if (entry != SeededNumberDictionary::kNotFound) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6053 | Object* result = dictionary->ValueAt(entry); |
| 6054 | PropertyDetails details = dictionary->DetailsAt(entry); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6055 | if (details.type() == CALLBACKS && result->IsAccessorPair()) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6056 | ASSERT(!details.IsDontDelete()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6057 | if (details.attributes() != attributes) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6058 | dictionary->DetailsAtPut( |
| 6059 | entry, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6060 | PropertyDetails(attributes, CALLBACKS, index)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6061 | } |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6062 | AccessorPair::cast(result)->SetComponents(getter, setter); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6063 | return true; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6064 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6065 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6066 | return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6070 | void JSObject::DefineElementAccessor(Handle<JSObject> object, |
| 6071 | uint32_t index, |
| 6072 | Handle<Object> getter, |
| 6073 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6074 | PropertyAttributes attributes, |
| 6075 | v8::AccessControl access_control) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6076 | switch (object->GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6077 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6078 | case FAST_ELEMENTS: |
| 6079 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6080 | case FAST_HOLEY_SMI_ELEMENTS: |
| 6081 | case FAST_HOLEY_ELEMENTS: |
| 6082 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6083 | break; |
| 6084 | case EXTERNAL_PIXEL_ELEMENTS: |
| 6085 | case EXTERNAL_BYTE_ELEMENTS: |
| 6086 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6087 | case EXTERNAL_SHORT_ELEMENTS: |
| 6088 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6089 | case EXTERNAL_INT_ELEMENTS: |
| 6090 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6091 | case EXTERNAL_FLOAT_ELEMENTS: |
| 6092 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 6093 | // Ignore getters and setters on pixel and external array elements. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6094 | return; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6095 | case DICTIONARY_ELEMENTS: |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6096 | if (UpdateGetterSetterInDictionary(object->element_dictionary(), |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6097 | index, |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6098 | *getter, |
| 6099 | *setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6100 | attributes)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6101 | return; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6102 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6103 | break; |
| 6104 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 6105 | // Ascertain whether we have read-only properties or an existing |
| 6106 | // getter/setter pair in an arguments elements dictionary backing |
| 6107 | // store. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6108 | FixedArray* parameter_map = FixedArray::cast(object->elements()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6109 | uint32_t length = parameter_map->length(); |
| 6110 | Object* probe = |
| 6111 | index < (length - 2) ? parameter_map->get(index + 2) : NULL; |
| 6112 | if (probe == NULL || probe->IsTheHole()) { |
| 6113 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 6114 | if (arguments->IsDictionary()) { |
| 6115 | SeededNumberDictionary* dictionary = |
| 6116 | SeededNumberDictionary::cast(arguments); |
| 6117 | if (UpdateGetterSetterInDictionary(dictionary, |
| 6118 | index, |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6119 | *getter, |
| 6120 | *setter, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6121 | attributes)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6122 | return; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 6123 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6124 | } |
| 6125 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6126 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6127 | } |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6128 | } |
| 6129 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6130 | Isolate* isolate = object->GetIsolate(); |
| 6131 | Handle<AccessorPair> accessors = isolate->factory()->NewAccessorPair(); |
| 6132 | accessors->SetComponents(*getter, *setter); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6133 | accessors->set_access_flags(access_control); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6134 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6135 | SetElementCallback(object, index, accessors, attributes); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6139 | Handle<AccessorPair> JSObject::CreateAccessorPairFor(Handle<JSObject> object, |
| 6140 | Handle<Name> name) { |
| 6141 | Isolate* isolate = object->GetIsolate(); |
| 6142 | LookupResult result(isolate); |
| 6143 | object->LocalLookupRealNamedProperty(*name, &result); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6144 | if (result.IsPropertyCallbacks()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6145 | // Note that the result can actually have IsDontDelete() == true when we |
| 6146 | // e.g. have to fall back to the slow case while adding a setter after |
| 6147 | // successfully reusing a map transition for a getter. Nevertheless, this is |
| 6148 | // OK, because the assertion only holds for the whole addition of both |
| 6149 | // accessors, not for the addition of each part. See first comment in |
| 6150 | // DefinePropertyAccessor below. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6151 | Object* obj = result.GetCallbackObject(); |
| 6152 | if (obj->IsAccessorPair()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6153 | return AccessorPair::Copy(handle(AccessorPair::cast(obj), isolate)); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6154 | } |
| 6155 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6156 | return isolate->factory()->NewAccessorPair(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6160 | void JSObject::DefinePropertyAccessor(Handle<JSObject> object, |
| 6161 | Handle<Name> name, |
| 6162 | Handle<Object> getter, |
| 6163 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6164 | PropertyAttributes attributes, |
| 6165 | v8::AccessControl access_control) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6166 | // We could assert that the property is configurable here, but we would need |
| 6167 | // 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] | 6168 | bool only_attribute_changes = getter->IsNull() && setter->IsNull(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6169 | if (object->HasFastProperties() && !only_attribute_changes && |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6170 | access_control == v8::DEFAULT && |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 6171 | (object->map()->NumberOfOwnDescriptors() <= kMaxNumberOfDescriptors)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6172 | bool getterOk = getter->IsNull() || |
| 6173 | DefineFastAccessor(object, name, ACCESSOR_GETTER, getter, attributes); |
| 6174 | bool setterOk = !getterOk || setter->IsNull() || |
| 6175 | DefineFastAccessor(object, name, ACCESSOR_SETTER, setter, attributes); |
| 6176 | if (getterOk && setterOk) return; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6179 | Handle<AccessorPair> accessors = CreateAccessorPairFor(object, name); |
| 6180 | accessors->SetComponents(*getter, *setter); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6181 | accessors->set_access_flags(access_control); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6182 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6183 | SetPropertyCallback(object, name, accessors, attributes); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6187 | bool JSObject::CanSetCallback(Name* name) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6188 | ASSERT(!IsAccessCheckNeeded() || |
| 6189 | GetIsolate()->MayNamedAccess(this, name, v8::ACCESS_SET)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6190 | |
| 6191 | // Check if there is an API defined callback object which prohibits |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6192 | // callback overwriting in this object or its prototype chain. |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6193 | // This mechanism is needed for instance in a browser setting, where |
| 6194 | // certain accessors such as window.location should not be allowed |
| 6195 | // to be overwritten because allowing overwriting could potentially |
| 6196 | // cause security problems. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6197 | LookupResult callback_result(GetIsolate()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6198 | LookupCallbackProperty(name, &callback_result); |
| 6199 | if (callback_result.IsFound()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6200 | Object* obj = callback_result.GetCallbackObject(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6201 | if (obj->IsAccessorInfo()) { |
| 6202 | return !AccessorInfo::cast(obj)->prohibits_overwriting(); |
| 6203 | } |
| 6204 | if (obj->IsAccessorPair()) { |
| 6205 | return !AccessorPair::cast(obj)->prohibits_overwriting(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6206 | } |
| 6207 | } |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6208 | return true; |
| 6209 | } |
| 6210 | |
| 6211 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 6212 | bool Map::DictionaryElementsInPrototypeChainOnly() { |
| 6213 | Heap* heap = GetHeap(); |
| 6214 | |
| 6215 | if (IsDictionaryElementsKind(elements_kind())) { |
| 6216 | return false; |
| 6217 | } |
| 6218 | |
| 6219 | for (Object* prototype = this->prototype(); |
| 6220 | prototype != heap->null_value(); |
| 6221 | prototype = prototype->GetPrototype(GetIsolate())) { |
| 6222 | if (prototype->IsJSProxy()) { |
| 6223 | // Be conservative, don't walk into proxies. |
| 6224 | return true; |
| 6225 | } |
| 6226 | |
| 6227 | if (IsDictionaryElementsKind( |
| 6228 | JSObject::cast(prototype)->map()->elements_kind())) { |
| 6229 | return true; |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | return false; |
| 6234 | } |
| 6235 | |
| 6236 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6237 | void JSObject::SetElementCallback(Handle<JSObject> object, |
| 6238 | uint32_t index, |
| 6239 | Handle<Object> structure, |
| 6240 | PropertyAttributes attributes) { |
| 6241 | Heap* heap = object->GetHeap(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6242 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6243 | |
| 6244 | // Normalize elements to make this operation simple. |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 6245 | bool had_dictionary_elements = object->HasDictionaryElements(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6246 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
| 6247 | ASSERT(object->HasDictionaryElements() || |
| 6248 | object->HasDictionaryArgumentsElements()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6249 | // Update the dictionary with the new CALLBACKS property. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6250 | dictionary = SeededNumberDictionary::Set(dictionary, index, structure, |
| 6251 | details); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6252 | dictionary->set_requires_slow_elements(); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6253 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6254 | // Update the dictionary backing store on the object. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6255 | if (object->elements()->map() == heap->non_strict_arguments_elements_map()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6256 | // Also delete any parameter alias. |
| 6257 | // |
| 6258 | // TODO(kmillikin): when deleting the last parameter alias we could |
| 6259 | // switch to a direct backing store without the parameter map. This |
| 6260 | // would allow GC of the context. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6261 | FixedArray* parameter_map = FixedArray::cast(object->elements()); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6262 | if (index < static_cast<uint32_t>(parameter_map->length()) - 2) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6263 | parameter_map->set(index + 2, heap->the_hole_value()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6264 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6265 | parameter_map->set(1, *dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6266 | } else { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6267 | object->set_elements(*dictionary); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 6268 | |
| 6269 | if (!had_dictionary_elements) { |
| 6270 | // KeyedStoreICs (at least the non-generic ones) need a reset. |
| 6271 | heap->ClearAllICsByKind(Code::KEYED_STORE_IC); |
| 6272 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6273 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
| 6276 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6277 | void JSObject::SetPropertyCallback(Handle<JSObject> object, |
| 6278 | Handle<Name> name, |
| 6279 | Handle<Object> structure, |
| 6280 | PropertyAttributes attributes) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6281 | // Normalize object to make this operation simple. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6282 | NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6283 | |
| 6284 | // For the global object allocate a new map to invalidate the global inline |
| 6285 | // 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] | 6286 | if (object->IsGlobalObject()) { |
| 6287 | Handle<Map> new_map = Map::CopyDropDescriptors(handle(object->map())); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 6288 | ASSERT(new_map->is_dictionary_map()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6289 | object->set_map(*new_map); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6290 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6291 | // When running crankshaft, changing the map is not enough. We |
| 6292 | // need to deoptimize all functions that rely on this global |
| 6293 | // object. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6294 | Deoptimizer::DeoptimizeGlobalObject(*object); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | // Update the dictionary with the new CALLBACKS property. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6298 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS, 0); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6299 | SetNormalizedProperty(object, name, structure, details); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6302 | |
| 6303 | void JSObject::DefineAccessor(Handle<JSObject> object, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6304 | Handle<Name> name, |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 6305 | Handle<Object> getter, |
| 6306 | Handle<Object> setter, |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6307 | PropertyAttributes attributes, |
| 6308 | v8::AccessControl access_control) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6309 | Isolate* isolate = object->GetIsolate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6310 | // Check access rights if needed. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6311 | if (object->IsAccessCheckNeeded() && |
| 6312 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 6313 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 6314 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6317 | if (object->IsJSGlobalProxy()) { |
| 6318 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 6319 | if (proto->IsNull()) return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6320 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6321 | DefineAccessor(Handle<JSObject>::cast(proto), |
| 6322 | name, |
| 6323 | getter, |
| 6324 | setter, |
| 6325 | attributes, |
| 6326 | access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6327 | return; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6330 | // Make sure that the top context does not change when doing callbacks or |
| 6331 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6332 | AssertNoContextChange ncc(isolate); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6333 | |
| 6334 | // Try to flatten before operating on the string. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6335 | if (name->IsString()) String::cast(*name)->TryFlatten(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6336 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6337 | if (!object->CanSetCallback(*name)) return; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 6338 | |
| 6339 | uint32_t index = 0; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6340 | bool is_element = name->AsArrayIndex(&index); |
| 6341 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 6342 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
mstarzinger@chromium.org | 6900838 | 2013-10-18 10:34:25 +0000 | [diff] [blame] | 6343 | bool is_observed = FLAG_harmony_observation && |
| 6344 | object->map()->is_observed() && |
| 6345 | *name != isolate->heap()->hidden_string(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6346 | bool preexists = false; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 6347 | if (is_observed) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6348 | if (is_element) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6349 | preexists = HasLocalElement(object, index); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6350 | if (preexists && object->GetLocalElementAccessorPair(index) == NULL) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 6351 | old_value = Object::GetElement(isolate, object, index); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6352 | } |
| 6353 | } else { |
| 6354 | LookupResult lookup(isolate); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6355 | object->LocalLookup(*name, &lookup, true); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6356 | preexists = lookup.IsProperty(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 6357 | if (preexists && lookup.IsDataProperty()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6358 | old_value = Object::GetProperty(object, name); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 6359 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6360 | } |
| 6361 | } |
| 6362 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6363 | if (is_element) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6364 | DefineElementAccessor( |
| 6365 | object, index, getter, setter, attributes, access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6366 | } else { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 6367 | DefinePropertyAccessor( |
| 6368 | object, name, getter, setter, attributes, access_control); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6369 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6370 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 6371 | if (is_observed) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 6372 | const char* type = preexists ? "reconfigure" : "add"; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6373 | EnqueueChangeRecord(object, type, name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6374 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6378 | static bool TryAccessorTransition(JSObject* self, |
| 6379 | Map* transitioned_map, |
| 6380 | int target_descriptor, |
| 6381 | AccessorComponent component, |
| 6382 | Object* accessor, |
| 6383 | PropertyAttributes attributes) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6384 | DescriptorArray* descs = transitioned_map->instance_descriptors(); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6385 | PropertyDetails details = descs->GetDetails(target_descriptor); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6386 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6387 | // 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] | 6388 | if (details.type() != CALLBACKS) return false; |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6389 | Object* descriptor = descs->GetCallbacksObject(target_descriptor); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6390 | if (!descriptor->IsAccessorPair()) return false; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6391 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6392 | Object* target_accessor = AccessorPair::cast(descriptor)->get(component); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6393 | PropertyAttributes target_attributes = details.attributes(); |
| 6394 | |
| 6395 | // Reuse transition if adding same accessor with same attributes. |
| 6396 | if (target_accessor == accessor && target_attributes == attributes) { |
| 6397 | self->set_map(transitioned_map); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6398 | return true; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6399 | } |
| 6400 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6401 | // If either not the same accessor, or not the same attributes, fall back to |
| 6402 | // the slow case. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6403 | return false; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6407 | static MaybeObject* CopyInsertDescriptor(Map* map, |
| 6408 | Name* name, |
| 6409 | AccessorPair* accessors, |
| 6410 | PropertyAttributes attributes) { |
| 6411 | CallbacksDescriptor new_accessors_desc(name, accessors, attributes); |
| 6412 | return map->CopyInsertDescriptor(&new_accessors_desc, INSERT_TRANSITION); |
| 6413 | } |
| 6414 | |
| 6415 | |
| 6416 | static Handle<Map> CopyInsertDescriptor(Handle<Map> map, |
| 6417 | Handle<Name> name, |
| 6418 | Handle<AccessorPair> accessors, |
| 6419 | PropertyAttributes attributes) { |
| 6420 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6421 | CopyInsertDescriptor(*map, *name, *accessors, attributes), |
| 6422 | Map); |
| 6423 | } |
| 6424 | |
| 6425 | |
| 6426 | bool JSObject::DefineFastAccessor(Handle<JSObject> object, |
| 6427 | Handle<Name> name, |
| 6428 | AccessorComponent component, |
| 6429 | Handle<Object> accessor, |
| 6430 | PropertyAttributes attributes) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6431 | ASSERT(accessor->IsSpecFunction() || accessor->IsUndefined()); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6432 | Isolate* isolate = object->GetIsolate(); |
| 6433 | LookupResult result(isolate); |
| 6434 | object->LocalLookup(*name, &result); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6435 | |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 6436 | if (result.IsFound() && !result.IsPropertyCallbacks()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6437 | return false; |
ulan@chromium.org | 32d7dba | 2013-04-24 10:59:06 +0000 | [diff] [blame] | 6438 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6439 | |
| 6440 | // Return success if the same accessor with the same attributes already exist. |
| 6441 | AccessorPair* source_accessors = NULL; |
| 6442 | if (result.IsPropertyCallbacks()) { |
| 6443 | Object* callback_value = result.GetCallbackObject(); |
| 6444 | if (callback_value->IsAccessorPair()) { |
| 6445 | source_accessors = AccessorPair::cast(callback_value); |
| 6446 | Object* entry = source_accessors->get(component); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6447 | if (entry == *accessor && result.GetAttributes() == attributes) { |
| 6448 | return true; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6449 | } |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6450 | } else { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6451 | return false; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6452 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6453 | |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6454 | int descriptor_number = result.GetDescriptorIndex(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6455 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6456 | object->map()->LookupTransition(*object, *name, &result); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6457 | |
| 6458 | if (result.IsFound()) { |
| 6459 | Map* target = result.GetTransitionTarget(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6460 | ASSERT(target->NumberOfOwnDescriptors() == |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6461 | object->map()->NumberOfOwnDescriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6462 | // This works since descriptors are sorted in order of addition. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6463 | ASSERT(object->map()->instance_descriptors()-> |
| 6464 | GetKey(descriptor_number) == *name); |
| 6465 | return TryAccessorTransition(*object, target, descriptor_number, |
| 6466 | component, *accessor, attributes); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6467 | } |
| 6468 | } else { |
| 6469 | // If not, lookup a transition. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6470 | object->map()->LookupTransition(*object, *name, &result); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6471 | |
| 6472 | // If there is a transition, try to follow it. |
| 6473 | if (result.IsFound()) { |
| 6474 | Map* target = result.GetTransitionTarget(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 6475 | int descriptor_number = target->LastAdded(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6476 | ASSERT(target->instance_descriptors()->GetKey(descriptor_number) |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6477 | ->Equals(*name)); |
| 6478 | return TryAccessorTransition(*object, target, descriptor_number, |
| 6479 | component, *accessor, attributes); |
verwaest@chromium.org | 178fb15 | 2012-07-18 11:21:48 +0000 | [diff] [blame] | 6480 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6481 | } |
| 6482 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6483 | // 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] | 6484 | // containing the accessor. Allocate a new pair if there were no source |
| 6485 | // accessors. Otherwise, copy the pair and modify the accessor. |
| 6486 | Handle<AccessorPair> accessors = source_accessors != NULL |
| 6487 | ? AccessorPair::Copy(Handle<AccessorPair>(source_accessors)) |
| 6488 | : isolate->factory()->NewAccessorPair(); |
| 6489 | accessors->set(component, *accessor); |
| 6490 | Handle<Map> new_map = CopyInsertDescriptor(Handle<Map>(object->map()), |
| 6491 | name, accessors, attributes); |
| 6492 | object->set_map(*new_map); |
| 6493 | return true; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6494 | } |
| 6495 | |
| 6496 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6497 | Handle<Object> JSObject::SetAccessor(Handle<JSObject> object, |
| 6498 | Handle<AccessorInfo> info) { |
| 6499 | Isolate* isolate = object->GetIsolate(); |
| 6500 | Factory* factory = isolate->factory(); |
| 6501 | Handle<Name> name(Name::cast(info->name())); |
| 6502 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6503 | // Check access rights if needed. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6504 | if (object->IsAccessCheckNeeded() && |
| 6505 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_SET)) { |
| 6506 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 6507 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 6508 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6511 | if (object->IsJSGlobalProxy()) { |
| 6512 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 6513 | if (proto->IsNull()) return object; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6514 | ASSERT(proto->IsJSGlobalObject()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6515 | return SetAccessor(Handle<JSObject>::cast(proto), info); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6516 | } |
| 6517 | |
| 6518 | // Make sure that the top context does not change when doing callbacks or |
| 6519 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6520 | AssertNoContextChange ncc(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6521 | |
| 6522 | // Try to flatten before operating on the string. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6523 | if (name->IsString()) FlattenString(Handle<String>::cast(name)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6524 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6525 | if (!object->CanSetCallback(*name)) return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6526 | |
| 6527 | uint32_t index = 0; |
| 6528 | bool is_element = name->AsArrayIndex(&index); |
| 6529 | |
| 6530 | if (is_element) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6531 | if (object->IsJSArray()) return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6532 | |
| 6533 | // Accessors overwrite previous callbacks (cf. with getters/setters). |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6534 | switch (object->GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6535 | case FAST_SMI_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6536 | case FAST_ELEMENTS: |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 6537 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 6538 | case FAST_HOLEY_SMI_ELEMENTS: |
| 6539 | case FAST_HOLEY_ELEMENTS: |
| 6540 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6541 | break; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 6542 | case EXTERNAL_PIXEL_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6543 | case EXTERNAL_BYTE_ELEMENTS: |
| 6544 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6545 | case EXTERNAL_SHORT_ELEMENTS: |
| 6546 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6547 | case EXTERNAL_INT_ELEMENTS: |
| 6548 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6549 | case EXTERNAL_FLOAT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 6550 | case EXTERNAL_DOUBLE_ELEMENTS: |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6551 | // Ignore getters and setters on pixel and external array |
| 6552 | // elements. |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6553 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6554 | case DICTIONARY_ELEMENTS: |
| 6555 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6556 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 6557 | UNIMPLEMENTED(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6558 | break; |
| 6559 | } |
| 6560 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6561 | SetElementCallback(object, index, info, info->property_attributes()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6562 | } else { |
| 6563 | // Lookup the name. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 6564 | LookupResult result(isolate); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6565 | object->LocalLookup(*name, &result, true); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6566 | // ES5 forbids turning a property into an accessor if it's not |
| 6567 | // 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] | 6568 | if (result.IsFound() && (result.IsReadOnly() || result.IsDontDelete())) { |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6569 | return factory->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6570 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6571 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6572 | SetPropertyCallback(object, name, info, info->property_attributes()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 6575 | return object; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 6576 | } |
| 6577 | |
| 6578 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6579 | Handle<Object> JSObject::GetAccessor(Handle<JSObject> object, |
| 6580 | Handle<Name> name, |
| 6581 | AccessorComponent component) { |
| 6582 | Isolate* isolate = object->GetIsolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6583 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6584 | // Make sure that the top context does not change when doing callbacks or |
| 6585 | // interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6586 | AssertNoContextChange ncc(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6587 | |
| 6588 | // Check access rights if needed. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6589 | if (object->IsAccessCheckNeeded() && |
| 6590 | !isolate->MayNamedAccess(*object, *name, v8::ACCESS_HAS)) { |
| 6591 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_HAS); |
| 6592 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 6593 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6596 | // Make the lookup and include prototypes. |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 6597 | uint32_t index = 0; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6598 | if (name->AsArrayIndex(&index)) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6599 | for (Handle<Object> obj = object; |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 6600 | !obj->IsNull(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6601 | obj = handle(JSReceiver::cast(*obj)->GetPrototype(), isolate)) { |
| 6602 | if (obj->IsJSObject() && JSObject::cast(*obj)->HasDictionaryElements()) { |
| 6603 | JSObject* js_object = JSObject::cast(*obj); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6604 | SeededNumberDictionary* dictionary = js_object->element_dictionary(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 6605 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6606 | if (entry != SeededNumberDictionary::kNotFound) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6607 | Object* element = dictionary->ValueAt(entry); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 6608 | if (dictionary->DetailsAt(entry).type() == CALLBACKS && |
| 6609 | element->IsAccessorPair()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6610 | return handle(AccessorPair::cast(element)->GetComponent(component), |
| 6611 | isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6612 | } |
| 6613 | } |
| 6614 | } |
| 6615 | } |
| 6616 | } else { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6617 | for (Handle<Object> obj = object; |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 6618 | !obj->IsNull(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6619 | obj = handle(JSReceiver::cast(*obj)->GetPrototype(), isolate)) { |
| 6620 | LookupResult result(isolate); |
| 6621 | JSReceiver::cast(*obj)->LocalLookup(*name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6622 | if (result.IsFound()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6623 | if (result.IsReadOnly()) return isolate->factory()->undefined_value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 6624 | if (result.IsPropertyCallbacks()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6625 | Object* obj = result.GetCallbackObject(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 6626 | if (obj->IsAccessorPair()) { |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6627 | return handle(AccessorPair::cast(obj)->GetComponent(component), |
| 6628 | isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6629 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6630 | } |
| 6631 | } |
| 6632 | } |
| 6633 | } |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 6634 | return isolate->factory()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | |
| 6638 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 6639 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6640 | int number_of_own_descriptors = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6641 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6642 | for (int i = 0; i < number_of_own_descriptors; i++) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6643 | if (descs->GetType(i) == FIELD) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6644 | Object* property = RawFastPropertyAt(descs->GetFieldIndex(i)); |
| 6645 | if (FLAG_track_double_fields && |
| 6646 | descs->GetDetails(i).representation().IsDouble()) { |
| 6647 | ASSERT(property->IsHeapNumber()); |
| 6648 | if (value->IsNumber() && property->Number() == value->Number()) { |
| 6649 | return descs->GetKey(i); |
| 6650 | } |
| 6651 | } else if (property == value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6652 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6653 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 6654 | } else if (descs->GetType(i) == CONSTANT) { |
| 6655 | if (descs->GetConstant(i) == value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 6656 | return descs->GetKey(i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6657 | } |
| 6658 | } |
| 6659 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6660 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6661 | } else { |
| 6662 | return property_dictionary()->SlowReverseLookup(value); |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6667 | Handle<Map> Map::RawCopy(Handle<Map> map, |
| 6668 | int instance_size) { |
| 6669 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6670 | map->RawCopy(instance_size), |
| 6671 | Map); |
| 6672 | } |
| 6673 | |
| 6674 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6675 | MaybeObject* Map::RawCopy(int instance_size) { |
| 6676 | Map* result; |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6677 | MaybeObject* maybe_result = |
| 6678 | GetHeap()->AllocateMap(instance_type(), instance_size); |
| 6679 | if (!maybe_result->To(&result)) return maybe_result; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6680 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6681 | result->set_prototype(prototype()); |
| 6682 | result->set_constructor(constructor()); |
| 6683 | result->set_bit_field(bit_field()); |
| 6684 | result->set_bit_field2(bit_field2()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6685 | int new_bit_field3 = bit_field3(); |
| 6686 | new_bit_field3 = OwnsDescriptors::update(new_bit_field3, true); |
| 6687 | new_bit_field3 = NumberOfOwnDescriptorsBits::update(new_bit_field3, 0); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 6688 | new_bit_field3 = EnumLengthBits::update(new_bit_field3, |
| 6689 | kInvalidEnumCacheSentinel); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6690 | new_bit_field3 = Deprecated::update(new_bit_field3, false); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 6691 | new_bit_field3 = IsUnstable::update(new_bit_field3, false); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6692 | result->set_bit_field3(new_bit_field3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6693 | return result; |
| 6694 | } |
| 6695 | |
| 6696 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 6697 | Handle<Map> Map::CopyNormalized(Handle<Map> map, |
| 6698 | PropertyNormalizationMode mode, |
| 6699 | NormalizedMapSharingMode sharing) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6700 | int new_instance_size = map->instance_size(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6701 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6702 | new_instance_size -= map->inobject_properties() * kPointerSize; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6703 | } |
| 6704 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6705 | Handle<Map> result = Map::RawCopy(map, new_instance_size); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6706 | |
| 6707 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6708 | result->set_inobject_properties(map->inobject_properties()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6711 | result->set_is_shared(sharing == SHARED_NORMALIZED_MAP); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 6712 | result->set_dictionary_map(true); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 6713 | result->set_migration_target(false); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6714 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 6715 | #ifdef VERIFY_HEAP |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6716 | if (FLAG_verify_heap && result->is_shared()) { |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6717 | result->SharedMapVerify(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 6718 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 6719 | #endif |
| 6720 | |
| 6721 | return result; |
| 6722 | } |
| 6723 | |
| 6724 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 6725 | Handle<Map> Map::CopyDropDescriptors(Handle<Map> map) { |
| 6726 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->CopyDropDescriptors(), Map); |
| 6727 | } |
| 6728 | |
| 6729 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6730 | MaybeObject* Map::CopyDropDescriptors() { |
| 6731 | Map* result; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6732 | MaybeObject* maybe_result = RawCopy(instance_size()); |
| 6733 | if (!maybe_result->To(&result)) return maybe_result; |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6734 | |
| 6735 | // Please note instance_type and instance_size are set when allocated. |
| 6736 | result->set_inobject_properties(inobject_properties()); |
| 6737 | result->set_unused_property_fields(unused_property_fields()); |
| 6738 | |
| 6739 | result->set_pre_allocated_property_fields(pre_allocated_property_fields()); |
| 6740 | result->set_is_shared(false); |
| 6741 | result->ClearCodeCache(GetHeap()); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 6742 | NotifyLeafMapLayoutChange(); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6743 | return result; |
| 6744 | } |
| 6745 | |
| 6746 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6747 | MaybeObject* Map::ShareDescriptor(DescriptorArray* descriptors, |
| 6748 | Descriptor* descriptor) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6749 | // Sanity check. This path is only to be taken if the map owns its descriptor |
| 6750 | // array, implying that its NumberOfOwnDescriptors equals the number of |
| 6751 | // descriptors in the descriptor array. |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6752 | ASSERT(NumberOfOwnDescriptors() == |
| 6753 | instance_descriptors()->number_of_descriptors()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 6754 | Map* result; |
| 6755 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6756 | if (!maybe_result->To(&result)) return maybe_result; |
| 6757 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 6758 | Name* name = descriptor->GetKey(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6759 | |
| 6760 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6761 | MaybeObject* maybe_transitions = |
| 6762 | AddTransition(name, result, SIMPLE_TRANSITION); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6763 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
| 6764 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6765 | int old_size = descriptors->number_of_descriptors(); |
| 6766 | |
| 6767 | DescriptorArray* new_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6768 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6769 | if (descriptors->NumberOfSlackDescriptors() > 0) { |
| 6770 | new_descriptors = descriptors; |
| 6771 | new_descriptors->Append(descriptor); |
| 6772 | } else { |
| 6773 | // Descriptor arrays grow by 50%. |
| 6774 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 6775 | GetIsolate(), old_size, old_size < 4 ? 1 : old_size / 2); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6776 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6777 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6778 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
| 6779 | |
| 6780 | // Copy the descriptors, inserting a descriptor. |
| 6781 | for (int i = 0; i < old_size; ++i) { |
| 6782 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 6783 | } |
| 6784 | |
| 6785 | new_descriptors->Append(descriptor, witness); |
| 6786 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6787 | if (old_size > 0) { |
| 6788 | // If the source descriptors had an enum cache we copy it. This ensures |
| 6789 | // that the maps to which we push the new descriptor array back can rely |
| 6790 | // on a cache always being available once it is set. If the map has more |
| 6791 | // enumerated descriptors than available in the original cache, the cache |
| 6792 | // will be lazily replaced by the extended cache when needed. |
| 6793 | if (descriptors->HasEnumCache()) { |
| 6794 | new_descriptors->CopyEnumCacheFrom(descriptors); |
| 6795 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6796 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6797 | Map* map; |
| 6798 | // Replace descriptors by new_descriptors in all maps that share it. |
| 6799 | for (Object* current = GetBackPointer(); |
| 6800 | !current->IsUndefined(); |
| 6801 | current = map->GetBackPointer()) { |
| 6802 | map = Map::cast(current); |
| 6803 | if (map->instance_descriptors() != descriptors) break; |
| 6804 | map->set_instance_descriptors(new_descriptors); |
| 6805 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6806 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6807 | set_instance_descriptors(new_descriptors); |
| 6808 | } |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 6809 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6810 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6811 | result->SetBackPointer(this); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6812 | result->InitializeDescriptors(new_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6813 | ASSERT(result->NumberOfOwnDescriptors() == NumberOfOwnDescriptors() + 1); |
| 6814 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6815 | set_transitions(transitions); |
| 6816 | set_owns_descriptors(false); |
| 6817 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6818 | return result; |
| 6819 | } |
| 6820 | |
| 6821 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6822 | Handle<Map> Map::CopyReplaceDescriptors(Handle<Map> map, |
| 6823 | Handle<DescriptorArray> descriptors, |
| 6824 | TransitionFlag flag, |
| 6825 | Handle<Name> name) { |
| 6826 | CALL_HEAP_FUNCTION(map->GetIsolate(), |
| 6827 | map->CopyReplaceDescriptors(*descriptors, flag, *name), |
| 6828 | Map); |
| 6829 | } |
| 6830 | |
| 6831 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6832 | MaybeObject* Map::CopyReplaceDescriptors(DescriptorArray* descriptors, |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6833 | TransitionFlag flag, |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 6834 | Name* name, |
| 6835 | SimpleTransitionFlag simple_flag) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6836 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6837 | |
| 6838 | Map* result; |
| 6839 | MaybeObject* maybe_result = CopyDropDescriptors(); |
| 6840 | if (!maybe_result->To(&result)) return maybe_result; |
| 6841 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6842 | result->InitializeDescriptors(descriptors); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6843 | |
verwaest@chromium.org | b6d052d | 2012-07-27 08:03:27 +0000 | [diff] [blame] | 6844 | if (flag == INSERT_TRANSITION && CanHaveMoreTransitions()) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6845 | TransitionArray* transitions; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 6846 | MaybeObject* maybe_transitions = AddTransition(name, result, simple_flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6847 | if (!maybe_transitions->To(&transitions)) return maybe_transitions; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6848 | set_transitions(transitions); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6849 | result->SetBackPointer(this); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 6850 | } else { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6851 | descriptors->InitializeRepresentations(Representation::Tagged()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6854 | return result; |
| 6855 | } |
| 6856 | |
| 6857 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6858 | // Since this method is used to rewrite an existing transition tree, it can |
| 6859 | // always insert transitions without checking. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6860 | Handle<Map> Map::CopyInstallDescriptors(Handle<Map> map, |
| 6861 | int new_descriptor, |
| 6862 | Handle<DescriptorArray> descriptors) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6863 | ASSERT(descriptors->IsSortedNoDuplicates()); |
| 6864 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6865 | Handle<Map> result = Map::CopyDropDescriptors(map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6866 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6867 | result->InitializeDescriptors(*descriptors); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6868 | result->SetNumberOfOwnDescriptors(new_descriptor + 1); |
| 6869 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6870 | int unused_property_fields = map->unused_property_fields(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6871 | if (descriptors->GetDetails(new_descriptor).type() == FIELD) { |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6872 | unused_property_fields = map->unused_property_fields() - 1; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6873 | if (unused_property_fields < 0) { |
| 6874 | unused_property_fields += JSObject::kFieldsAdded; |
| 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | result->set_unused_property_fields(unused_property_fields); |
| 6879 | result->set_owns_descriptors(false); |
| 6880 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6881 | Handle<Name> name = handle(descriptors->GetKey(new_descriptor)); |
| 6882 | Handle<TransitionArray> transitions = Map::AddTransition(map, name, result, |
| 6883 | SIMPLE_TRANSITION); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6884 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 6885 | map->set_transitions(*transitions); |
| 6886 | result->SetBackPointer(*map); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 6887 | |
| 6888 | return result; |
| 6889 | } |
| 6890 | |
| 6891 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6892 | MaybeObject* Map::CopyAsElementsKind(ElementsKind kind, TransitionFlag flag) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6893 | if (flag == INSERT_TRANSITION) { |
| 6894 | ASSERT(!HasElementsTransition() || |
| 6895 | ((elements_transition_map()->elements_kind() == DICTIONARY_ELEMENTS || |
| 6896 | IsExternalArrayElementsKind( |
| 6897 | elements_transition_map()->elements_kind())) && |
| 6898 | (kind == DICTIONARY_ELEMENTS || |
| 6899 | IsExternalArrayElementsKind(kind)))); |
| 6900 | ASSERT(!IsFastElementsKind(kind) || |
| 6901 | IsMoreGeneralElementsKindTransition(elements_kind(), kind)); |
| 6902 | ASSERT(kind != elements_kind()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6903 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6904 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6905 | bool insert_transition = |
| 6906 | flag == INSERT_TRANSITION && !HasElementsTransition(); |
| 6907 | |
| 6908 | if (insert_transition && owns_descriptors()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6909 | // In case the map owned its own descriptors, share the descriptors and |
| 6910 | // transfer ownership to the new map. |
| 6911 | Map* new_map; |
| 6912 | MaybeObject* maybe_new_map = CopyDropDescriptors(); |
| 6913 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 6914 | |
| 6915 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6916 | if (added_elements->IsFailure()) return added_elements; |
| 6917 | |
| 6918 | new_map->set_elements_kind(kind); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6919 | new_map->InitializeDescriptors(instance_descriptors()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6920 | new_map->SetBackPointer(this); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6921 | set_owns_descriptors(false); |
| 6922 | return new_map; |
| 6923 | } |
| 6924 | |
| 6925 | // In case the map did not own its own descriptors, a split is forced by |
| 6926 | // copying the map; creating a new descriptor array cell. |
| 6927 | // Create a new free-floating map only if we are not allowed to store it. |
| 6928 | Map* new_map; |
| 6929 | MaybeObject* maybe_new_map = Copy(); |
| 6930 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6931 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6932 | new_map->set_elements_kind(kind); |
| 6933 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 6934 | if (insert_transition) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6935 | MaybeObject* added_elements = set_elements_transition_map(new_map); |
| 6936 | if (added_elements->IsFailure()) return added_elements; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6937 | new_map->SetBackPointer(this); |
| 6938 | } |
| 6939 | |
| 6940 | return new_map; |
| 6941 | } |
| 6942 | |
| 6943 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 6944 | Handle<Map> Map::CopyForObserved(Handle<Map> map) { |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6945 | ASSERT(!map->is_observed()); |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 6946 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6947 | Isolate* isolate = map->GetIsolate(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6948 | |
| 6949 | // In case the map owned its own descriptors, share the descriptors and |
| 6950 | // transfer ownership to the new map. |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6951 | Handle<Map> new_map; |
| 6952 | if (map->owns_descriptors()) { |
| 6953 | new_map = Map::CopyDropDescriptors(map); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6954 | } else { |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6955 | new_map = Map::Copy(map); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6956 | } |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6957 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6958 | Handle<TransitionArray> transitions = |
| 6959 | Map::AddTransition(map, isolate->factory()->observed_symbol(), new_map, |
| 6960 | FULL_TRANSITION); |
| 6961 | |
| 6962 | map->set_transitions(*transitions); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6963 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 6964 | new_map->set_is_observed(); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6965 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6966 | if (map->owns_descriptors()) { |
| 6967 | new_map->InitializeDescriptors(map->instance_descriptors()); |
| 6968 | map->set_owns_descriptors(false); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 6971 | new_map->SetBackPointer(*map); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 6972 | return new_map; |
| 6973 | } |
| 6974 | |
| 6975 | |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6976 | MaybeObject* Map::CopyWithPreallocatedFieldDescriptors() { |
| 6977 | if (pre_allocated_property_fields() == 0) return CopyDropDescriptors(); |
| 6978 | |
| 6979 | // If the map has pre-allocated properties always start out with a descriptor |
| 6980 | // array describing these properties. |
| 6981 | ASSERT(constructor()->IsJSFunction()); |
| 6982 | JSFunction* ctor = JSFunction::cast(constructor()); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 6983 | Map* map = ctor->initial_map(); |
| 6984 | DescriptorArray* descriptors = map->instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 6985 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 6986 | int number_of_own_descriptors = map->NumberOfOwnDescriptors(); |
| 6987 | DescriptorArray* new_descriptors; |
| 6988 | MaybeObject* maybe_descriptors = |
| 6989 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 6990 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 6991 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 6992 | return CopyReplaceDescriptors(new_descriptors, OMIT_TRANSITION); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 6996 | Handle<Map> Map::Copy(Handle<Map> map) { |
| 6997 | CALL_HEAP_FUNCTION(map->GetIsolate(), map->Copy(), Map); |
| 6998 | } |
| 6999 | |
| 7000 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7001 | MaybeObject* Map::Copy() { |
| 7002 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7003 | DescriptorArray* new_descriptors; |
| 7004 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 7005 | MaybeObject* maybe_descriptors = |
| 7006 | descriptors->CopyUpTo(number_of_own_descriptors); |
| 7007 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 7008 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7009 | return CopyReplaceDescriptors(new_descriptors, OMIT_TRANSITION); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | |
| 7013 | MaybeObject* Map::CopyAddDescriptor(Descriptor* descriptor, |
| 7014 | TransitionFlag flag) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7015 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7016 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7017 | // Ensure the key is unique. |
| 7018 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7019 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 7020 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7021 | int old_size = NumberOfOwnDescriptors(); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 7022 | int new_size = old_size + 1; |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7023 | |
| 7024 | if (flag == INSERT_TRANSITION && |
| 7025 | owns_descriptors() && |
| 7026 | CanHaveMoreTransitions()) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 7027 | return ShareDescriptor(descriptors, descriptor); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7028 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7029 | |
| 7030 | DescriptorArray* new_descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7031 | MaybeObject* maybe_descriptors = |
| 7032 | DescriptorArray::Allocate(GetIsolate(), old_size, 1); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7033 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
| 7034 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 7035 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7036 | |
| 7037 | // Copy the descriptors, inserting a descriptor. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7038 | for (int i = 0; i < old_size; ++i) { |
| 7039 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7040 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7041 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7042 | if (old_size != descriptors->number_of_descriptors()) { |
| 7043 | new_descriptors->SetNumberOfDescriptors(new_size); |
| 7044 | new_descriptors->Set(old_size, descriptor, witness); |
| 7045 | new_descriptors->Sort(); |
| 7046 | } else { |
| 7047 | new_descriptors->Append(descriptor, witness); |
| 7048 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7049 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7050 | Name* key = descriptor->GetKey(); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7051 | return CopyReplaceDescriptors(new_descriptors, flag, key, SIMPLE_TRANSITION); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | |
| 7055 | MaybeObject* Map::CopyInsertDescriptor(Descriptor* descriptor, |
| 7056 | TransitionFlag flag) { |
| 7057 | DescriptorArray* old_descriptors = instance_descriptors(); |
| 7058 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7059 | // Ensure the key is unique. |
| 7060 | MaybeObject* maybe_result = descriptor->KeyToUniqueName(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7061 | if (maybe_result->IsFailure()) return maybe_result; |
| 7062 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7063 | // We replace the key if it is already present. |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7064 | int index = old_descriptors->SearchWithCache(descriptor->GetKey(), this); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7065 | if (index != DescriptorArray::kNotFound) { |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 7066 | return CopyReplaceDescriptor(old_descriptors, descriptor, index, flag); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 7067 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7068 | return CopyAddDescriptor(descriptor, flag); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7072 | Handle<DescriptorArray> DescriptorArray::CopyUpToAddAttributes( |
| 7073 | Handle<DescriptorArray> desc, |
| 7074 | int enumeration_index, |
| 7075 | PropertyAttributes attributes) { |
| 7076 | CALL_HEAP_FUNCTION(desc->GetIsolate(), |
| 7077 | desc->CopyUpToAddAttributes(enumeration_index, attributes), |
| 7078 | DescriptorArray); |
| 7079 | } |
| 7080 | |
| 7081 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7082 | MaybeObject* DescriptorArray::CopyUpToAddAttributes( |
| 7083 | int enumeration_index, PropertyAttributes attributes) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7084 | if (enumeration_index == 0) return GetHeap()->empty_descriptor_array(); |
| 7085 | |
| 7086 | int size = enumeration_index; |
| 7087 | |
| 7088 | DescriptorArray* descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7089 | MaybeObject* maybe_descriptors = Allocate(GetIsolate(), size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7090 | if (!maybe_descriptors->To(&descriptors)) return maybe_descriptors; |
| 7091 | DescriptorArray::WhitenessWitness witness(descriptors); |
| 7092 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7093 | if (attributes != NONE) { |
| 7094 | for (int i = 0; i < size; ++i) { |
| 7095 | Object* value = GetValue(i); |
| 7096 | PropertyDetails details = GetDetails(i); |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7097 | int mask = DONT_DELETE | DONT_ENUM; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7098 | // READ_ONLY is an invalid attribute for JS setters/getters. |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7099 | if (details.type() != CALLBACKS || !value->IsAccessorPair()) { |
| 7100 | mask |= READ_ONLY; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7101 | } |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 7102 | details = details.CopyAddAttributes( |
| 7103 | static_cast<PropertyAttributes>(attributes & mask)); |
| 7104 | Descriptor desc(GetKey(i), value, details); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7105 | descriptors->Set(i, &desc, witness); |
| 7106 | } |
| 7107 | } else { |
| 7108 | for (int i = 0; i < size; ++i) { |
| 7109 | descriptors->CopyFrom(i, this, i, witness); |
| 7110 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | if (number_of_descriptors() != enumeration_index) descriptors->Sort(); |
| 7114 | |
| 7115 | return descriptors; |
| 7116 | } |
| 7117 | |
| 7118 | |
jkummerow@chromium.org | c195667 | 2012-10-11 15:57:38 +0000 | [diff] [blame] | 7119 | MaybeObject* Map::CopyReplaceDescriptor(DescriptorArray* descriptors, |
| 7120 | Descriptor* descriptor, |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7121 | int insertion_index, |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7122 | TransitionFlag flag) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7123 | // Ensure the key is unique. |
| 7124 | MaybeObject* maybe_failure = descriptor->KeyToUniqueName(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7125 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 7126 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7127 | Name* key = descriptor->GetKey(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7128 | ASSERT(key == descriptors->GetKey(insertion_index)); |
| 7129 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7130 | int new_size = NumberOfOwnDescriptors(); |
| 7131 | ASSERT(0 <= insertion_index && insertion_index < new_size); |
verwaest@chromium.org | 652f4fa | 2012-10-08 08:48:51 +0000 | [diff] [blame] | 7132 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7133 | ASSERT_LT(insertion_index, new_size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7134 | |
| 7135 | DescriptorArray* new_descriptors; |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7136 | MaybeObject* maybe_descriptors = |
| 7137 | DescriptorArray::Allocate(GetIsolate(), new_size); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7138 | if (!maybe_descriptors->To(&new_descriptors)) return maybe_descriptors; |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 7139 | DescriptorArray::WhitenessWitness witness(new_descriptors); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7140 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7141 | for (int i = 0; i < new_size; ++i) { |
| 7142 | if (i == insertion_index) { |
| 7143 | new_descriptors->Set(i, descriptor, witness); |
| 7144 | } else { |
| 7145 | new_descriptors->CopyFrom(i, descriptors, i, witness); |
| 7146 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7147 | } |
| 7148 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7149 | // Re-sort if descriptors were removed. |
| 7150 | if (new_size != descriptors->length()) new_descriptors->Sort(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 7151 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 7152 | SimpleTransitionFlag simple_flag = |
| 7153 | (insertion_index == descriptors->number_of_descriptors() - 1) |
| 7154 | ? SIMPLE_TRANSITION |
| 7155 | : FULL_TRANSITION; |
| 7156 | return CopyReplaceDescriptors(new_descriptors, flag, key, simple_flag); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7157 | } |
| 7158 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7159 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7160 | void Map::UpdateCodeCache(Handle<Map> map, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7161 | Handle<Name> name, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7162 | Handle<Code> code) { |
| 7163 | Isolate* isolate = map->GetIsolate(); |
| 7164 | CALL_HEAP_FUNCTION_VOID(isolate, |
| 7165 | map->UpdateCodeCache(*name, *code)); |
| 7166 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7167 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7168 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7169 | MaybeObject* Map::UpdateCodeCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7170 | // Allocate the code cache if not present. |
| 7171 | if (code_cache()->IsFixedArray()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7172 | Object* result; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 7173 | { MaybeObject* maybe_result = GetHeap()->AllocateCodeCache(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7174 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7175 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7176 | set_code_cache(result); |
| 7177 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7178 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7179 | // Update the code cache. |
| 7180 | return CodeCache::cast(code_cache())->Update(name, code); |
| 7181 | } |
| 7182 | |
| 7183 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7184 | Object* Map::FindInCodeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7185 | // Do a lookup if a code cache exists. |
| 7186 | if (!code_cache()->IsFixedArray()) { |
| 7187 | return CodeCache::cast(code_cache())->Lookup(name, flags); |
| 7188 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7189 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7190 | } |
| 7191 | } |
| 7192 | |
| 7193 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7194 | int Map::IndexInCodeCache(Object* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7195 | // Get the internal index if a code cache exists. |
| 7196 | if (!code_cache()->IsFixedArray()) { |
| 7197 | return CodeCache::cast(code_cache())->GetIndex(name, code); |
| 7198 | } |
| 7199 | return -1; |
| 7200 | } |
| 7201 | |
| 7202 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7203 | void Map::RemoveFromCodeCache(Name* name, Code* code, int index) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7204 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 7205 | // RemoveFromCodeCache so the code cache must be there. |
| 7206 | ASSERT(!code_cache()->IsFixedArray()); |
| 7207 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 7208 | } |
| 7209 | |
| 7210 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7211 | // An iterator over all map transitions in an descriptor array, reusing the map |
| 7212 | // field of the contens array while it is running. |
| 7213 | class IntrusiveMapTransitionIterator { |
| 7214 | public: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7215 | explicit IntrusiveMapTransitionIterator(TransitionArray* transition_array) |
| 7216 | : transition_array_(transition_array) { } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 7217 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7218 | void Start() { |
| 7219 | ASSERT(!IsIterating()); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7220 | *TransitionArrayHeader() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
| 7223 | bool IsIterating() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7224 | return (*TransitionArrayHeader())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7225 | } |
| 7226 | |
| 7227 | Map* Next() { |
| 7228 | ASSERT(IsIterating()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7229 | int index = Smi::cast(*TransitionArrayHeader())->value(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7230 | int number_of_transitions = transition_array_->number_of_transitions(); |
| 7231 | while (index < number_of_transitions) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7232 | *TransitionArrayHeader() = Smi::FromInt(index + 1); |
| 7233 | return transition_array_->GetTarget(index); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7234 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7235 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7236 | *TransitionArrayHeader() = transition_array_->GetHeap()->fixed_array_map(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7237 | return NULL; |
| 7238 | } |
| 7239 | |
| 7240 | private: |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7241 | Object** TransitionArrayHeader() { |
| 7242 | return HeapObject::RawField(transition_array_, TransitionArray::kMapOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7245 | TransitionArray* transition_array_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7246 | }; |
| 7247 | |
| 7248 | |
| 7249 | // An iterator over all prototype transitions, reusing the map field of the |
| 7250 | // underlying array while it is running. |
| 7251 | class IntrusivePrototypeTransitionIterator { |
| 7252 | public: |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7253 | explicit IntrusivePrototypeTransitionIterator(HeapObject* proto_trans) |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7254 | : proto_trans_(proto_trans) { } |
| 7255 | |
| 7256 | void Start() { |
| 7257 | ASSERT(!IsIterating()); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7258 | *Header() = Smi::FromInt(0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7259 | } |
| 7260 | |
| 7261 | bool IsIterating() { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7262 | return (*Header())->IsSmi(); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | Map* Next() { |
| 7266 | ASSERT(IsIterating()); |
| 7267 | int transitionNumber = Smi::cast(*Header())->value(); |
| 7268 | if (transitionNumber < NumberOfTransitions()) { |
| 7269 | *Header() = Smi::FromInt(transitionNumber + 1); |
| 7270 | return GetTransition(transitionNumber); |
| 7271 | } |
| 7272 | *Header() = proto_trans_->GetHeap()->fixed_array_map(); |
| 7273 | return NULL; |
| 7274 | } |
| 7275 | |
| 7276 | private: |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7277 | Object** Header() { |
| 7278 | return HeapObject::RawField(proto_trans_, FixedArray::kMapOffset); |
| 7279 | } |
| 7280 | |
| 7281 | int NumberOfTransitions() { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7282 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 7283 | Object* num = proto_trans->get(Map::kProtoTransitionNumberOfEntriesOffset); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7284 | return Smi::cast(num)->value(); |
| 7285 | } |
| 7286 | |
| 7287 | Map* GetTransition(int transitionNumber) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7288 | FixedArray* proto_trans = reinterpret_cast<FixedArray*>(proto_trans_); |
| 7289 | return Map::cast(proto_trans->get(IndexFor(transitionNumber))); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7290 | } |
| 7291 | |
| 7292 | int IndexFor(int transitionNumber) { |
| 7293 | return Map::kProtoTransitionHeaderSize + |
| 7294 | Map::kProtoTransitionMapOffset + |
| 7295 | transitionNumber * Map::kProtoTransitionElementsPerEntry; |
| 7296 | } |
| 7297 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 7298 | HeapObject* proto_trans_; |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7299 | }; |
| 7300 | |
| 7301 | |
| 7302 | // To traverse the transition tree iteratively, we have to store two kinds of |
| 7303 | // information in a map: The parent map in the traversal and which children of a |
| 7304 | // node have already been visited. To do this without additional memory, we |
| 7305 | // temporarily reuse two maps with known values: |
| 7306 | // |
| 7307 | // (1) The map of the map temporarily holds the parent, and is restored to the |
| 7308 | // meta map afterwards. |
| 7309 | // |
| 7310 | // (2) The info which children have already been visited depends on which part |
| 7311 | // of the map we currently iterate: |
| 7312 | // |
| 7313 | // (a) If we currently follow normal map transitions, we temporarily store |
| 7314 | // the current index in the map of the FixedArray of the desciptor |
| 7315 | // array's contents, and restore it to the fixed array map afterwards. |
| 7316 | // Note that a single descriptor can have 0, 1, or 2 transitions. |
| 7317 | // |
| 7318 | // (b) If we currently follow prototype transitions, we temporarily store |
| 7319 | // the current index in the map of the FixedArray holding the prototype |
| 7320 | // transitions, and restore it to the fixed array map afterwards. |
| 7321 | // |
| 7322 | // Note that the child iterator is just a concatenation of two iterators: One |
| 7323 | // iterating over map transitions and one iterating over prototype transisitons. |
| 7324 | class TraversableMap : public Map { |
| 7325 | public: |
| 7326 | // Record the parent in the traversal within this map. Note that this destroys |
| 7327 | // this map's map! |
| 7328 | void SetParent(TraversableMap* parent) { set_map_no_write_barrier(parent); } |
| 7329 | |
| 7330 | // Reset the current map's map, returning the parent previously stored in it. |
| 7331 | TraversableMap* GetAndResetParent() { |
| 7332 | TraversableMap* old_parent = static_cast<TraversableMap*>(map()); |
| 7333 | set_map_no_write_barrier(GetHeap()->meta_map()); |
| 7334 | return old_parent; |
| 7335 | } |
| 7336 | |
| 7337 | // Start iterating over this map's children, possibly destroying a FixedArray |
| 7338 | // map (see explanation above). |
| 7339 | void ChildIteratorStart() { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7340 | if (HasTransitionArray()) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7341 | if (HasPrototypeTransitions()) { |
| 7342 | IntrusivePrototypeTransitionIterator(GetPrototypeTransitions()).Start(); |
| 7343 | } |
| 7344 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7345 | IntrusiveMapTransitionIterator(transitions()).Start(); |
| 7346 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7347 | } |
| 7348 | |
| 7349 | // If we have an unvisited child map, return that one and advance. If we have |
| 7350 | // none, return NULL and reset any destroyed FixedArray maps. |
| 7351 | TraversableMap* ChildIteratorNext() { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7352 | TransitionArray* transition_array = unchecked_transition_array(); |
| 7353 | if (!transition_array->map()->IsSmi() && |
| 7354 | !transition_array->IsTransitionArray()) { |
| 7355 | return NULL; |
| 7356 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7357 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7358 | if (transition_array->HasPrototypeTransitions()) { |
| 7359 | HeapObject* proto_transitions = |
| 7360 | transition_array->UncheckedPrototypeTransitions(); |
| 7361 | IntrusivePrototypeTransitionIterator proto_iterator(proto_transitions); |
| 7362 | if (proto_iterator.IsIterating()) { |
| 7363 | Map* next = proto_iterator.Next(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 7364 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 7365 | } |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 7366 | } |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 7367 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 7368 | IntrusiveMapTransitionIterator transition_iterator(transition_array); |
| 7369 | if (transition_iterator.IsIterating()) { |
| 7370 | Map* next = transition_iterator.Next(); |
| 7371 | if (next != NULL) return static_cast<TraversableMap*>(next); |
| 7372 | } |
| 7373 | |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 7374 | return NULL; |
| 7375 | } |
| 7376 | }; |
| 7377 | |
| 7378 | |
| 7379 | // Traverse the transition tree in postorder without using the C++ stack by |
| 7380 | // doing pointer reversal. |
| 7381 | void Map::TraverseTransitionTree(TraverseCallback callback, void* data) { |
| 7382 | TraversableMap* current = static_cast<TraversableMap*>(this); |
| 7383 | current->ChildIteratorStart(); |
| 7384 | while (true) { |
| 7385 | TraversableMap* child = current->ChildIteratorNext(); |
| 7386 | if (child != NULL) { |
| 7387 | child->ChildIteratorStart(); |
| 7388 | child->SetParent(current); |
| 7389 | current = child; |
| 7390 | } else { |
| 7391 | TraversableMap* parent = current->GetAndResetParent(); |
| 7392 | callback(current, data); |
| 7393 | if (current == this) break; |
| 7394 | current = parent; |
| 7395 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7396 | } |
| 7397 | } |
| 7398 | |
| 7399 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7400 | MaybeObject* CodeCache::Update(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7401 | // The number of monomorphic stubs for normal load/store/call IC's can grow to |
| 7402 | // a large number and therefore they need to go into a hash table. They are |
| 7403 | // used to load global properties from cells. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7404 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7405 | // Make sure that a hash table is allocated for the normal load code cache. |
| 7406 | if (normal_type_cache()->IsUndefined()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7407 | Object* result; |
| 7408 | { MaybeObject* maybe_result = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7409 | CodeCacheHashTable::Allocate(GetHeap(), |
| 7410 | CodeCacheHashTable::kInitialSize); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7411 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7412 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7413 | set_normal_type_cache(result); |
| 7414 | } |
| 7415 | return UpdateNormalTypeCache(name, code); |
| 7416 | } else { |
| 7417 | ASSERT(default_cache()->IsFixedArray()); |
| 7418 | return UpdateDefaultCache(name, code); |
| 7419 | } |
| 7420 | } |
| 7421 | |
| 7422 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7423 | MaybeObject* CodeCache::UpdateDefaultCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7424 | // 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] | 7425 | // flags. This allows call constant stubs to overwrite call field |
| 7426 | // stubs, etc. |
| 7427 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 7428 | |
| 7429 | // First check whether we can update existing code cache without |
| 7430 | // extending it. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7431 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7432 | int length = cache->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7433 | int deleted_index = -1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7434 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7435 | Object* key = cache->get(i); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7436 | if (key->IsNull()) { |
| 7437 | if (deleted_index < 0) deleted_index = i; |
| 7438 | continue; |
| 7439 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7440 | if (key->IsUndefined()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7441 | if (deleted_index >= 0) i = deleted_index; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7442 | cache->set(i + kCodeCacheEntryNameOffset, name); |
| 7443 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7444 | return this; |
| 7445 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7446 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7447 | Code::Flags found = |
| 7448 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7449 | if (Code::RemoveTypeFromFlags(found) == flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7450 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7451 | return this; |
| 7452 | } |
| 7453 | } |
| 7454 | } |
| 7455 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7456 | // Reached the end of the code cache. If there were deleted |
| 7457 | // elements, reuse the space for the first of them. |
| 7458 | if (deleted_index >= 0) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7459 | cache->set(deleted_index + kCodeCacheEntryNameOffset, name); |
| 7460 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, code); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7461 | return this; |
| 7462 | } |
| 7463 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7464 | // Extend the code cache with some new entries (at least one). Must be a |
| 7465 | // multiple of the entry size. |
| 7466 | int new_length = length + ((length >> 1)) + kCodeCacheEntrySize; |
| 7467 | new_length = new_length - new_length % kCodeCacheEntrySize; |
| 7468 | ASSERT((new_length % kCodeCacheEntrySize) == 0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7469 | Object* result; |
| 7470 | { MaybeObject* maybe_result = cache->CopySize(new_length); |
| 7471 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7472 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7473 | |
| 7474 | // Add the (name, code) pair to the new cache. |
| 7475 | cache = FixedArray::cast(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7476 | cache->set(length + kCodeCacheEntryNameOffset, name); |
| 7477 | cache->set(length + kCodeCacheEntryCodeOffset, code); |
| 7478 | set_default_cache(cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7479 | return this; |
| 7480 | } |
| 7481 | |
| 7482 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7483 | MaybeObject* CodeCache::UpdateNormalTypeCache(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7484 | // Adding a new entry can cause a new cache to be allocated. |
| 7485 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7486 | Object* new_cache; |
| 7487 | { MaybeObject* maybe_new_cache = cache->Put(name, code); |
| 7488 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 7489 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7490 | set_normal_type_cache(new_cache); |
| 7491 | return this; |
| 7492 | } |
| 7493 | |
| 7494 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7495 | Object* CodeCache::Lookup(Name* name, Code::Flags flags) { |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7496 | flags = Code::RemoveTypeFromFlags(flags); |
| 7497 | Object* result = LookupDefaultCache(name, flags); |
| 7498 | if (result->IsCode()) return result; |
| 7499 | return LookupNormalTypeCache(name, flags); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7500 | } |
| 7501 | |
| 7502 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7503 | Object* CodeCache::LookupDefaultCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7504 | FixedArray* cache = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7505 | int length = cache->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7506 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 7507 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 7508 | // Skip deleted elements. |
| 7509 | if (key->IsNull()) continue; |
| 7510 | if (key->IsUndefined()) return key; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7511 | if (name->Equals(Name::cast(key))) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7512 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7513 | if (Code::RemoveTypeFromFlags(code->flags()) == flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7514 | return code; |
| 7515 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7516 | } |
| 7517 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7518 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7522 | Object* CodeCache::LookupNormalTypeCache(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7523 | if (!normal_type_cache()->IsUndefined()) { |
| 7524 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 7525 | return cache->Lookup(name, flags); |
| 7526 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7527 | return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7528 | } |
| 7529 | } |
| 7530 | |
| 7531 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7532 | int CodeCache::GetIndex(Object* name, Code* code) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7533 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7534 | if (normal_type_cache()->IsUndefined()) return -1; |
| 7535 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7536 | return cache->GetIndex(Name::cast(name), code->flags()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
| 7539 | FixedArray* array = default_cache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7540 | int len = array->length(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7541 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 7542 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7543 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 7544 | return -1; |
| 7545 | } |
| 7546 | |
| 7547 | |
sgjesse@chromium.org | 99a37fa | 2010-03-11 09:23:46 +0000 | [diff] [blame] | 7548 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 7549 | if (code->type() == Code::NORMAL) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7550 | ASSERT(!normal_type_cache()->IsUndefined()); |
| 7551 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7552 | ASSERT(cache->GetIndex(Name::cast(name), code->flags()) == index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7553 | cache->RemoveByIndex(index); |
| 7554 | } else { |
| 7555 | FixedArray* array = default_cache(); |
| 7556 | ASSERT(array->length() >= index && array->get(index)->IsCode()); |
| 7557 | // Use null instead of undefined for deleted elements to distinguish |
| 7558 | // deleted elements from unused elements. This distinction is used |
| 7559 | // when looking up in the cache and when updating the cache. |
| 7560 | ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
| 7561 | array->set_null(index - 1); // Name. |
| 7562 | array->set_null(index); // Code. |
| 7563 | } |
| 7564 | } |
| 7565 | |
| 7566 | |
| 7567 | // The key in the code cache hash table consists of the property name and the |
| 7568 | // code object. The actual match is on the name and the code flags. If a key |
| 7569 | // is created using the flags and not a code object it can only be used for |
| 7570 | // lookup not to create a new entry. |
| 7571 | class CodeCacheHashTableKey : public HashTableKey { |
| 7572 | public: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7573 | CodeCacheHashTableKey(Name* name, Code::Flags flags) |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7574 | : name_(name), flags_(flags), code_(NULL) { } |
| 7575 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7576 | CodeCacheHashTableKey(Name* name, Code* code) |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 7577 | : name_(name), flags_(code->flags()), code_(code) { } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7578 | |
| 7579 | |
| 7580 | bool IsMatch(Object* other) { |
| 7581 | if (!other->IsFixedArray()) return false; |
| 7582 | FixedArray* pair = FixedArray::cast(other); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7583 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7584 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 7585 | if (flags != flags_) { |
| 7586 | return false; |
| 7587 | } |
| 7588 | return name_->Equals(name); |
| 7589 | } |
| 7590 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7591 | static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7592 | return name->Hash() ^ flags; |
| 7593 | } |
| 7594 | |
| 7595 | uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); } |
| 7596 | |
| 7597 | uint32_t HashForObject(Object* obj) { |
| 7598 | FixedArray* pair = FixedArray::cast(obj); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7599 | Name* name = Name::cast(pair->get(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7600 | Code* code = Code::cast(pair->get(1)); |
| 7601 | return NameFlagsHashHelper(name, code->flags()); |
| 7602 | } |
| 7603 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7604 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7605 | ASSERT(code_ != NULL); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7606 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7607 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7608 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7609 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7610 | FixedArray* pair = FixedArray::cast(obj); |
| 7611 | pair->set(0, name_); |
| 7612 | pair->set(1, code_); |
| 7613 | return pair; |
| 7614 | } |
| 7615 | |
| 7616 | private: |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7617 | Name* name_; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7618 | Code::Flags flags_; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7619 | // TODO(jkummerow): We should be able to get by without this. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7620 | Code* code_; |
| 7621 | }; |
| 7622 | |
| 7623 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7624 | Object* CodeCacheHashTable::Lookup(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7625 | CodeCacheHashTableKey key(name, flags); |
| 7626 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7627 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7628 | return get(EntryToIndex(entry) + 1); |
| 7629 | } |
| 7630 | |
| 7631 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7632 | MaybeObject* CodeCacheHashTable::Put(Name* name, Code* code) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7633 | CodeCacheHashTableKey key(name, code); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7634 | Object* obj; |
| 7635 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7636 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7637 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7638 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7639 | // Don't use |this|, as the table might have grown. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7640 | CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj); |
| 7641 | |
| 7642 | int entry = cache->FindInsertionEntry(key.Hash()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7643 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7644 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7645 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 7646 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7647 | |
| 7648 | cache->set(EntryToIndex(entry), k); |
| 7649 | cache->set(EntryToIndex(entry) + 1, code); |
| 7650 | cache->ElementAdded(); |
| 7651 | return cache; |
| 7652 | } |
| 7653 | |
| 7654 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7655 | int CodeCacheHashTable::GetIndex(Name* name, Code::Flags flags) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7656 | CodeCacheHashTableKey key(name, flags); |
| 7657 | int entry = FindEntry(&key); |
| 7658 | return (entry == kNotFound) ? -1 : entry; |
| 7659 | } |
| 7660 | |
| 7661 | |
| 7662 | void CodeCacheHashTable::RemoveByIndex(int index) { |
| 7663 | ASSERT(index >= 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7664 | Heap* heap = GetHeap(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7665 | set(EntryToIndex(index), heap->the_hole_value()); |
| 7666 | set(EntryToIndex(index) + 1, heap->the_hole_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7667 | ElementRemoved(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7671 | void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> cache, |
| 7672 | MapHandleList* maps, |
| 7673 | Code::Flags flags, |
| 7674 | Handle<Code> code) { |
| 7675 | Isolate* isolate = cache->GetIsolate(); |
| 7676 | CALL_HEAP_FUNCTION_VOID(isolate, cache->Update(maps, flags, *code)); |
| 7677 | } |
| 7678 | |
| 7679 | |
| 7680 | MaybeObject* PolymorphicCodeCache::Update(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7681 | Code::Flags flags, |
| 7682 | Code* code) { |
| 7683 | // Initialize cache if necessary. |
| 7684 | if (cache()->IsUndefined()) { |
| 7685 | Object* result; |
| 7686 | { MaybeObject* maybe_result = |
| 7687 | PolymorphicCodeCacheHashTable::Allocate( |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7688 | GetHeap(), |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7689 | PolymorphicCodeCacheHashTable::kInitialSize); |
| 7690 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7691 | } |
| 7692 | set_cache(result); |
| 7693 | } else { |
| 7694 | // This entry shouldn't be contained in the cache yet. |
| 7695 | ASSERT(PolymorphicCodeCacheHashTable::cast(cache()) |
| 7696 | ->Lookup(maps, flags)->IsUndefined()); |
| 7697 | } |
| 7698 | PolymorphicCodeCacheHashTable* hash_table = |
| 7699 | PolymorphicCodeCacheHashTable::cast(cache()); |
| 7700 | Object* new_cache; |
| 7701 | { MaybeObject* maybe_new_cache = hash_table->Put(maps, flags, code); |
| 7702 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 7703 | } |
| 7704 | set_cache(new_cache); |
| 7705 | return this; |
| 7706 | } |
| 7707 | |
| 7708 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7709 | Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps, |
| 7710 | Code::Flags flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7711 | if (!cache()->IsUndefined()) { |
| 7712 | PolymorphicCodeCacheHashTable* hash_table = |
| 7713 | PolymorphicCodeCacheHashTable::cast(cache()); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 7714 | return Handle<Object>(hash_table->Lookup(maps, flags), GetIsolate()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7715 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7716 | return GetIsolate()->factory()->undefined_value(); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7717 | } |
| 7718 | } |
| 7719 | |
| 7720 | |
| 7721 | // Despite their name, object of this class are not stored in the actual |
| 7722 | // hash table; instead they're temporarily used for lookups. It is therefore |
| 7723 | // safe to have a weak (non-owning) pointer to a MapList as a member field. |
| 7724 | class PolymorphicCodeCacheHashTableKey : public HashTableKey { |
| 7725 | public: |
| 7726 | // Callers must ensure that |maps| outlives the newly constructed object. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7727 | PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags) |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7728 | : maps_(maps), |
| 7729 | code_flags_(code_flags) {} |
| 7730 | |
| 7731 | bool IsMatch(Object* other) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7732 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7733 | int other_flags; |
| 7734 | FromObject(other, &other_flags, &other_maps); |
| 7735 | if (code_flags_ != other_flags) return false; |
| 7736 | if (maps_->length() != other_maps.length()) return false; |
| 7737 | // Compare just the hashes first because it's faster. |
| 7738 | int this_hash = MapsHashHelper(maps_, code_flags_); |
| 7739 | int other_hash = MapsHashHelper(&other_maps, other_flags); |
| 7740 | if (this_hash != other_hash) return false; |
| 7741 | |
| 7742 | // Full comparison: for each map in maps_, look for an equivalent map in |
| 7743 | // other_maps. This implementation is slow, but probably good enough for |
| 7744 | // now because the lists are short (<= 4 elements currently). |
| 7745 | for (int i = 0; i < maps_->length(); ++i) { |
| 7746 | bool match_found = false; |
| 7747 | for (int j = 0; j < other_maps.length(); ++j) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 7748 | if (*(maps_->at(i)) == *(other_maps.at(j))) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7749 | match_found = true; |
| 7750 | break; |
| 7751 | } |
| 7752 | } |
| 7753 | if (!match_found) return false; |
| 7754 | } |
| 7755 | return true; |
| 7756 | } |
| 7757 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7758 | static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7759 | uint32_t hash = code_flags; |
| 7760 | for (int i = 0; i < maps->length(); ++i) { |
| 7761 | hash ^= maps->at(i)->Hash(); |
| 7762 | } |
| 7763 | return hash; |
| 7764 | } |
| 7765 | |
| 7766 | uint32_t Hash() { |
| 7767 | return MapsHashHelper(maps_, code_flags_); |
| 7768 | } |
| 7769 | |
| 7770 | uint32_t HashForObject(Object* obj) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7771 | MapHandleList other_maps(kDefaultListAllocationSize); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7772 | int other_flags; |
| 7773 | FromObject(obj, &other_flags, &other_maps); |
| 7774 | return MapsHashHelper(&other_maps, other_flags); |
| 7775 | } |
| 7776 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7777 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7778 | Object* obj; |
| 7779 | // The maps in |maps_| must be copied to a newly allocated FixedArray, |
| 7780 | // both because the referenced MapList is short-lived, and because C++ |
| 7781 | // objects can't be stored in the heap anyway. |
| 7782 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7783 | heap->AllocateUninitializedFixedArray(maps_->length() + 1); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7784 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7785 | } |
| 7786 | FixedArray* list = FixedArray::cast(obj); |
| 7787 | list->set(0, Smi::FromInt(code_flags_)); |
| 7788 | for (int i = 0; i < maps_->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7789 | list->set(i + 1, *maps_->at(i)); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7790 | } |
| 7791 | return list; |
| 7792 | } |
| 7793 | |
| 7794 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7795 | static MapHandleList* FromObject(Object* obj, |
| 7796 | int* code_flags, |
| 7797 | MapHandleList* maps) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7798 | FixedArray* list = FixedArray::cast(obj); |
| 7799 | maps->Rewind(0); |
| 7800 | *code_flags = Smi::cast(list->get(0))->value(); |
| 7801 | for (int i = 1; i < list->length(); ++i) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7802 | maps->Add(Handle<Map>(Map::cast(list->get(i)))); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7803 | } |
| 7804 | return maps; |
| 7805 | } |
| 7806 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7807 | MapHandleList* maps_; // weak. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7808 | int code_flags_; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7809 | static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7810 | }; |
| 7811 | |
| 7812 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7813 | Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps, |
| 7814 | int code_flags) { |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7815 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7816 | int entry = FindEntry(&key); |
| 7817 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
| 7818 | return get(EntryToIndex(entry) + 1); |
| 7819 | } |
| 7820 | |
| 7821 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7822 | MaybeObject* PolymorphicCodeCacheHashTable::Put(MapHandleList* maps, |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7823 | int code_flags, |
| 7824 | Code* code) { |
| 7825 | PolymorphicCodeCacheHashTableKey key(maps, code_flags); |
| 7826 | Object* obj; |
| 7827 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 7828 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7829 | } |
| 7830 | PolymorphicCodeCacheHashTable* cache = |
| 7831 | reinterpret_cast<PolymorphicCodeCacheHashTable*>(obj); |
| 7832 | int entry = cache->FindInsertionEntry(key.Hash()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 7833 | { MaybeObject* maybe_obj = key.AsObject(GetHeap()); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 7834 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7835 | } |
| 7836 | cache->set(EntryToIndex(entry), obj); |
| 7837 | cache->set(EntryToIndex(entry) + 1, code); |
| 7838 | cache->ElementAdded(); |
| 7839 | return cache; |
| 7840 | } |
| 7841 | |
| 7842 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7843 | MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7844 | ElementsAccessor* accessor = array->GetElementsAccessor(); |
| 7845 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7846 | accessor->AddElementsToFixedArray(array, array, this); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7847 | FixedArray* result; |
| 7848 | if (!maybe_result->To<FixedArray>(&result)) return maybe_result; |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 7849 | #ifdef ENABLE_SLOW_ASSERTS |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7850 | if (FLAG_enable_slow_asserts) { |
| 7851 | for (int i = 0; i < result->length(); i++) { |
| 7852 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7853 | ASSERT(current->IsNumber() || current->IsName()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 7854 | } |
| 7855 | } |
| 7856 | #endif |
| 7857 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7858 | } |
| 7859 | |
| 7860 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7861 | MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7862 | ElementsAccessor* accessor = ElementsAccessor::ForArray(other); |
| 7863 | MaybeObject* maybe_result = |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 7864 | accessor->AddElementsToFixedArray(NULL, NULL, this, other); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7865 | FixedArray* result; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 7866 | if (!maybe_result->To(&result)) return maybe_result; |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 7867 | #ifdef ENABLE_SLOW_ASSERTS |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7868 | if (FLAG_enable_slow_asserts) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 7869 | for (int i = 0; i < result->length(); i++) { |
| 7870 | Object* current = result->get(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 7871 | ASSERT(current->IsNumber() || current->IsName()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 7872 | } |
| 7873 | } |
| 7874 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7875 | return result; |
| 7876 | } |
| 7877 | |
| 7878 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 7879 | MaybeObject* FixedArray::CopySize(int new_length, PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7880 | Heap* heap = GetHeap(); |
| 7881 | if (new_length == 0) return heap->empty_fixed_array(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7882 | Object* obj; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 7883 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length, pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7884 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7885 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7886 | FixedArray* result = FixedArray::cast(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7887 | // Copy the content |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 7888 | DisallowHeapAllocation no_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7889 | int len = length(); |
| 7890 | if (new_length < len) len = new_length; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7891 | // We are taking the map from the old fixed array so the map is sure to |
| 7892 | // be an immortal immutable object. |
| 7893 | result->set_map_no_write_barrier(map()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7894 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7895 | for (int i = 0; i < len; i++) { |
| 7896 | result->set(i, get(i), mode); |
| 7897 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7898 | return result; |
| 7899 | } |
| 7900 | |
| 7901 | |
| 7902 | 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] | 7903 | DisallowHeapAllocation no_gc; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7904 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7905 | for (int index = 0; index < len; index++) { |
| 7906 | dest->set(dest_pos+index, get(pos+index), mode); |
| 7907 | } |
| 7908 | } |
| 7909 | |
| 7910 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7911 | #ifdef DEBUG |
| 7912 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 7913 | if (length() != other->length()) return false; |
| 7914 | for (int i = 0 ; i < length(); ++i) { |
| 7915 | if (get(i) != other->get(i)) return false; |
| 7916 | } |
| 7917 | return true; |
| 7918 | } |
| 7919 | #endif |
| 7920 | |
| 7921 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 7922 | MaybeObject* DescriptorArray::Allocate(Isolate* isolate, |
| 7923 | int number_of_descriptors, |
| 7924 | int slack) { |
| 7925 | Heap* heap = isolate->heap(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7926 | // Do not use DescriptorArray::cast on incomplete object. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7927 | int size = number_of_descriptors + slack; |
| 7928 | if (size == 0) return heap->empty_descriptor_array(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 7929 | FixedArray* result; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7930 | // Allocate the array of keys. |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7931 | MaybeObject* maybe_array = heap->AllocateFixedArray(LengthFor(size)); |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 7932 | if (!maybe_array->To(&result)) return maybe_array; |
verwaest@chromium.org | 5091559 | 2012-06-18 12:15:44 +0000 | [diff] [blame] | 7933 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 7934 | result->set(kDescriptorLengthIndex, Smi::FromInt(number_of_descriptors)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7935 | result->set(kEnumCacheIndex, Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7936 | return result; |
| 7937 | } |
| 7938 | |
| 7939 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 7940 | void DescriptorArray::ClearEnumCache() { |
| 7941 | set(kEnumCacheIndex, Smi::FromInt(0)); |
| 7942 | } |
| 7943 | |
| 7944 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7945 | void DescriptorArray::SetEnumCache(FixedArray* bridge_storage, |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7946 | FixedArray* new_cache, |
| 7947 | Object* new_index_cache) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7948 | ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 7949 | ASSERT(new_index_cache->IsSmi() || new_index_cache->IsFixedArray()); |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 7950 | ASSERT(!IsEmpty()); |
| 7951 | ASSERT(!HasEnumCache() || new_cache->length() > GetEnumCache()->length()); |
| 7952 | FixedArray::cast(bridge_storage)-> |
| 7953 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 7954 | FixedArray::cast(bridge_storage)-> |
| 7955 | set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache); |
| 7956 | set(kEnumCacheIndex, bridge_storage); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7957 | } |
| 7958 | |
| 7959 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 7960 | void DescriptorArray::CopyFrom(int dst_index, |
| 7961 | DescriptorArray* src, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7962 | int src_index, |
| 7963 | const WhitenessWitness& witness) { |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7964 | Object* value = src->GetValue(src_index); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 7965 | PropertyDetails details = src->GetDetails(src_index); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7966 | Descriptor desc(src->GetKey(src_index), value, details); |
| 7967 | Set(dst_index, &desc, witness); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 7968 | } |
| 7969 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7970 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7971 | Handle<DescriptorArray> DescriptorArray::Merge(Handle<DescriptorArray> desc, |
| 7972 | int verbatim, |
| 7973 | int valid, |
| 7974 | int new_size, |
| 7975 | int modify_index, |
| 7976 | StoreMode store_mode, |
| 7977 | Handle<DescriptorArray> other) { |
| 7978 | CALL_HEAP_FUNCTION(desc->GetIsolate(), |
| 7979 | desc->Merge(verbatim, valid, new_size, modify_index, |
| 7980 | store_mode, *other), |
| 7981 | DescriptorArray); |
| 7982 | } |
| 7983 | |
| 7984 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7985 | // 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] | 7986 | // partly) updated |this| descriptor array. |
| 7987 | // The method merges two descriptor array in three parts. Both descriptor arrays |
| 7988 | // are identical up to |verbatim|. They also overlap in keys up to |valid|. |
| 7989 | // Between |verbatim| and |valid|, the resulting descriptor type as well as the |
| 7990 | // representation are generalized from both |this| and |other|. Beyond |valid|, |
| 7991 | // the descriptors are copied verbatim from |other| up to |new_size|. |
| 7992 | // In case of incompatible types, the type and representation of |other| is |
| 7993 | // used. |
| 7994 | MaybeObject* DescriptorArray::Merge(int verbatim, |
| 7995 | int valid, |
| 7996 | int new_size, |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 7997 | int modify_index, |
| 7998 | StoreMode store_mode, |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 7999 | DescriptorArray* other) { |
| 8000 | ASSERT(verbatim <= valid); |
| 8001 | ASSERT(valid <= new_size); |
| 8002 | |
| 8003 | DescriptorArray* result; |
| 8004 | // Allocate a new descriptor array large enough to hold the required |
| 8005 | // descriptors, with minimally the exact same size as this descriptor array. |
| 8006 | MaybeObject* maybe_descriptors = DescriptorArray::Allocate( |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8007 | GetIsolate(), new_size, |
| 8008 | Max(new_size, other->number_of_descriptors()) - new_size); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8009 | if (!maybe_descriptors->To(&result)) return maybe_descriptors; |
| 8010 | ASSERT(result->length() > length() || |
| 8011 | result->NumberOfSlackDescriptors() > 0 || |
| 8012 | result->number_of_descriptors() == other->number_of_descriptors()); |
| 8013 | ASSERT(result->number_of_descriptors() == new_size); |
| 8014 | |
| 8015 | DescriptorArray::WhitenessWitness witness(result); |
| 8016 | |
| 8017 | int descriptor; |
| 8018 | |
| 8019 | // 0 -> |verbatim| |
| 8020 | int current_offset = 0; |
| 8021 | for (descriptor = 0; descriptor < verbatim; descriptor++) { |
| 8022 | if (GetDetails(descriptor).type() == FIELD) current_offset++; |
hpayer@chromium.org | 2311a91 | 2013-08-28 13:39:38 +0000 | [diff] [blame] | 8023 | result->CopyFrom(descriptor, other, descriptor, witness); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8024 | } |
| 8025 | |
| 8026 | // |verbatim| -> |valid| |
| 8027 | for (; descriptor < valid; descriptor++) { |
| 8028 | Name* key = GetKey(descriptor); |
| 8029 | PropertyDetails details = GetDetails(descriptor); |
| 8030 | PropertyDetails other_details = other->GetDetails(descriptor); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8031 | |
| 8032 | if (details.type() == FIELD || other_details.type() == FIELD || |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 8033 | (store_mode == FORCE_FIELD && descriptor == modify_index) || |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 8034 | (details.type() == CONSTANT && |
| 8035 | other_details.type() == CONSTANT && |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8036 | GetValue(descriptor) != other->GetValue(descriptor))) { |
| 8037 | Representation representation = |
| 8038 | details.representation().generalize(other_details.representation()); |
| 8039 | FieldDescriptor d(key, |
| 8040 | current_offset++, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8041 | other_details.attributes(), |
| 8042 | representation); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8043 | result->Set(descriptor, &d, witness); |
| 8044 | } else { |
| 8045 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 8046 | } |
| 8047 | } |
| 8048 | |
| 8049 | // |valid| -> |new_size| |
| 8050 | for (; descriptor < new_size; descriptor++) { |
| 8051 | PropertyDetails details = other->GetDetails(descriptor); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 8052 | if (details.type() == FIELD || |
| 8053 | (store_mode == FORCE_FIELD && descriptor == modify_index)) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8054 | Name* key = other->GetKey(descriptor); |
| 8055 | FieldDescriptor d(key, |
| 8056 | current_offset++, |
| 8057 | details.attributes(), |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8058 | details.representation()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 8059 | result->Set(descriptor, &d, witness); |
| 8060 | } else { |
| 8061 | result->CopyFrom(descriptor, other, descriptor, witness); |
| 8062 | } |
| 8063 | } |
| 8064 | |
| 8065 | result->Sort(); |
| 8066 | return result; |
| 8067 | } |
| 8068 | |
| 8069 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8070 | // Checks whether a merge of |other| into |this| would return a copy of |this|. |
| 8071 | bool DescriptorArray::IsMoreGeneralThan(int verbatim, |
| 8072 | int valid, |
| 8073 | int new_size, |
| 8074 | DescriptorArray* other) { |
| 8075 | ASSERT(verbatim <= valid); |
| 8076 | ASSERT(valid <= new_size); |
| 8077 | if (valid != new_size) return false; |
| 8078 | |
| 8079 | for (int descriptor = verbatim; descriptor < valid; descriptor++) { |
| 8080 | PropertyDetails details = GetDetails(descriptor); |
| 8081 | PropertyDetails other_details = other->GetDetails(descriptor); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 8082 | if (!other_details.representation().fits_into(details.representation())) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8083 | return false; |
| 8084 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 8085 | if (details.type() == CONSTANT) { |
| 8086 | if (other_details.type() != CONSTANT) return false; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 8087 | if (GetValue(descriptor) != other->GetValue(descriptor)) return false; |
| 8088 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | return true; |
| 8092 | } |
| 8093 | |
| 8094 | |
verwaest@chromium.org | 3714139 | 2012-05-31 13:27:02 +0000 | [diff] [blame] | 8095 | // We need the whiteness witness since sort will reshuffle the entries in the |
| 8096 | // descriptor array. If the descriptor array were to be black, the shuffling |
| 8097 | // would move a slot that was already recorded as pointing into an evacuation |
| 8098 | // candidate. This would result in missing updates upon evacuation. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8099 | void DescriptorArray::Sort() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8100 | // In-place heap sort. |
| 8101 | int len = number_of_descriptors(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8102 | // Reset sorting since the descriptor array might contain invalid pointers. |
| 8103 | for (int i = 0; i < len; ++i) SetSortedKey(i, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8104 | // Bottom-up max-heap construction. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8105 | // Index of the last node with children |
| 8106 | const int max_parent_index = (len / 2) - 1; |
| 8107 | for (int i = max_parent_index; i >= 0; --i) { |
| 8108 | int parent_index = i; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8109 | const uint32_t parent_hash = GetSortedKey(i)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8110 | while (parent_index <= max_parent_index) { |
| 8111 | int child_index = 2 * parent_index + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8112 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8113 | if (child_index + 1 < len) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8114 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8115 | if (right_child_hash > child_hash) { |
| 8116 | child_index++; |
| 8117 | child_hash = right_child_hash; |
| 8118 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8119 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8120 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8121 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8122 | // Now element at child_index could be < its children. |
| 8123 | parent_index = child_index; // parent_hash remains correct. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8124 | } |
| 8125 | } |
| 8126 | |
| 8127 | // Extract elements and create sorted array. |
| 8128 | for (int i = len - 1; i > 0; --i) { |
| 8129 | // Put max element at the back of the array. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8130 | SwapSortedKeys(0, i); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8131 | // Shift down the new top element. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8132 | int parent_index = 0; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8133 | const uint32_t parent_hash = GetSortedKey(parent_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8134 | const int max_parent_index = (i / 2) - 1; |
| 8135 | while (parent_index <= max_parent_index) { |
| 8136 | int child_index = parent_index * 2 + 1; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8137 | uint32_t child_hash = GetSortedKey(child_index)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8138 | if (child_index + 1 < i) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8139 | uint32_t right_child_hash = GetSortedKey(child_index + 1)->Hash(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8140 | if (right_child_hash > child_hash) { |
| 8141 | child_index++; |
| 8142 | child_hash = right_child_hash; |
| 8143 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8144 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8145 | if (child_hash <= parent_hash) break; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 8146 | SwapSortedKeys(parent_index, child_index); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8147 | parent_index = child_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8148 | } |
| 8149 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 8150 | ASSERT(IsSortedNoDuplicates()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 8154 | Handle<AccessorPair> AccessorPair::Copy(Handle<AccessorPair> pair) { |
| 8155 | Handle<AccessorPair> copy = pair->GetIsolate()->factory()->NewAccessorPair(); |
| 8156 | copy->set_getter(pair->getter()); |
| 8157 | copy->set_setter(pair->setter()); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 8158 | return copy; |
| 8159 | } |
| 8160 | |
| 8161 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 8162 | Object* AccessorPair::GetComponent(AccessorComponent component) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 8163 | Object* accessor = get(component); |
| 8164 | return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8168 | MaybeObject* DeoptimizationInputData::Allocate(Isolate* isolate, |
| 8169 | int deopt_entry_count, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8170 | PretenureFlag pretenure) { |
| 8171 | ASSERT(deopt_entry_count > 0); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8172 | return isolate->heap()->AllocateFixedArray(LengthFor(deopt_entry_count), |
| 8173 | pretenure); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
| 8176 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8177 | MaybeObject* DeoptimizationOutputData::Allocate(Isolate* isolate, |
| 8178 | int number_of_deopt_points, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8179 | PretenureFlag pretenure) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 8180 | if (number_of_deopt_points == 0) return isolate->heap()->empty_fixed_array(); |
| 8181 | return isolate->heap()->AllocateFixedArray( |
| 8182 | LengthOfFixedArray(number_of_deopt_points), pretenure); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8183 | } |
| 8184 | |
| 8185 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8186 | #ifdef DEBUG |
| 8187 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 8188 | if (IsEmpty()) return other->IsEmpty(); |
| 8189 | if (other->IsEmpty()) return false; |
| 8190 | if (length() != other->length()) return false; |
| 8191 | for (int i = 0; i < length(); ++i) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 8192 | if (get(i) != other->get(i)) return false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8193 | } |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 8194 | return true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8195 | } |
| 8196 | #endif |
| 8197 | |
| 8198 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 8199 | static bool IsIdentifier(UnicodeCache* cache, Name* name) { |
| 8200 | // Checks whether the buffer contains an identifier (no escape). |
| 8201 | if (!name->IsString()) return false; |
| 8202 | String* string = String::cast(name); |
| 8203 | if (string->length() == 0) return false; |
| 8204 | ConsStringIteratorOp op; |
| 8205 | StringCharacterStream stream(string, &op); |
| 8206 | if (!cache->IsIdentifierStart(stream.GetNext())) { |
| 8207 | return false; |
| 8208 | } |
| 8209 | while (stream.HasMore()) { |
| 8210 | if (!cache->IsIdentifierPart(stream.GetNext())) { |
| 8211 | return false; |
| 8212 | } |
| 8213 | } |
| 8214 | return true; |
| 8215 | } |
| 8216 | |
| 8217 | |
| 8218 | bool Name::IsCacheable(Isolate* isolate) { |
| 8219 | return IsSymbol() || |
| 8220 | IsIdentifier(isolate->unicode_cache(), this) || |
| 8221 | this == isolate->heap()->hidden_string(); |
| 8222 | } |
| 8223 | |
| 8224 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8225 | bool String::LooksValid() { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8226 | if (!GetIsolate()->heap()->Contains(this)) return false; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8227 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
| 8230 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8231 | String::FlatContent String::GetFlatContent() { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8232 | ASSERT(!AllowHeapAllocation::IsAllowed()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8233 | int length = this->length(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8234 | StringShape shape(this); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8235 | String* string = this; |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8236 | int offset = 0; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8237 | if (shape.representation_tag() == kConsStringTag) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8238 | ConsString* cons = ConsString::cast(string); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8239 | if (cons->second()->length() != 0) { |
| 8240 | return FlatContent(); |
| 8241 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8242 | string = cons->first(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8243 | shape = StringShape(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8244 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8245 | if (shape.representation_tag() == kSlicedStringTag) { |
| 8246 | SlicedString* slice = SlicedString::cast(string); |
| 8247 | offset = slice->offset(); |
| 8248 | string = slice->parent(); |
| 8249 | shape = StringShape(string); |
| 8250 | ASSERT(shape.representation_tag() != kConsStringTag && |
| 8251 | shape.representation_tag() != kSlicedStringTag); |
| 8252 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8253 | if (shape.encoding_tag() == kOneByteStringTag) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8254 | const uint8_t* start; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8255 | if (shape.representation_tag() == kSeqStringTag) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8256 | start = SeqOneByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8257 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8258 | start = ExternalAsciiString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8259 | } |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8260 | return FlatContent(Vector<const uint8_t>(start + offset, length)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8261 | } else { |
| 8262 | ASSERT(shape.encoding_tag() == kTwoByteStringTag); |
| 8263 | const uc16* start; |
| 8264 | if (shape.representation_tag() == kSeqStringTag) { |
| 8265 | start = SeqTwoByteString::cast(string)->GetChars(); |
| 8266 | } else { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8267 | start = ExternalTwoByteString::cast(string)->GetChars(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8268 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8269 | return FlatContent(Vector<const uc16>(start + offset, length)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8270 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8274 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 8275 | RobustnessFlag robust_flag, |
| 8276 | int offset, |
| 8277 | int length, |
| 8278 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8279 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8280 | return SmartArrayPointer<char>(NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8281 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8282 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8283 | |
| 8284 | // Negative length means the to the end of the string. |
| 8285 | if (length < 0) length = kMaxInt - offset; |
| 8286 | |
| 8287 | // 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] | 8288 | Access<ConsStringIteratorOp> op( |
| 8289 | heap->isolate()->objects_string_iterator()); |
| 8290 | StringCharacterStream stream(this, op.value(), offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8291 | int character_position = offset; |
| 8292 | int utf8_bytes = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8293 | int last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8294 | while (stream.HasMore() && character_position++ < offset + length) { |
| 8295 | uint16_t character = stream.GetNext(); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8296 | utf8_bytes += unibrow::Utf8::Length(character, last); |
| 8297 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
| 8300 | if (length_return) { |
| 8301 | *length_return = utf8_bytes; |
| 8302 | } |
| 8303 | |
| 8304 | char* result = NewArray<char>(utf8_bytes + 1); |
| 8305 | |
| 8306 | // 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] | 8307 | stream.Reset(this, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8308 | character_position = offset; |
| 8309 | int utf8_byte_position = 0; |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8310 | last = unibrow::Utf16::kNoPreviousCharacter; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8311 | while (stream.HasMore() && character_position++ < offset + length) { |
| 8312 | uint16_t character = stream.GetNext(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8313 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 8314 | character = ' '; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8315 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8316 | utf8_byte_position += |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8317 | unibrow::Utf8::Encode(result + utf8_byte_position, character, last); |
| 8318 | last = character; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8319 | } |
| 8320 | result[utf8_byte_position] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8321 | return SmartArrayPointer<char>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8322 | } |
| 8323 | |
| 8324 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8325 | SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 8326 | RobustnessFlag robust_flag, |
| 8327 | int* length_return) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8328 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 8329 | } |
| 8330 | |
| 8331 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8332 | const uc16* String::GetTwoByteData(unsigned start) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 8333 | ASSERT(!IsOneByteRepresentationUnderneath()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8334 | switch (StringShape(this).representation_tag()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8335 | case kSeqStringTag: |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8336 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8337 | case kExternalStringTag: |
| 8338 | return ExternalTwoByteString::cast(this)-> |
| 8339 | ExternalTwoByteStringGetData(start); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8340 | case kSlicedStringTag: { |
| 8341 | SlicedString* slice = SlicedString::cast(this); |
| 8342 | return slice->parent()->GetTwoByteData(start + slice->offset()); |
| 8343 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8344 | case kConsStringTag: |
| 8345 | UNREACHABLE(); |
| 8346 | return NULL; |
| 8347 | } |
| 8348 | UNREACHABLE(); |
| 8349 | return NULL; |
| 8350 | } |
| 8351 | |
| 8352 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8353 | SmartArrayPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8354 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8355 | return SmartArrayPointer<uc16>(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8356 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8357 | Heap* heap = GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8358 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8359 | Access<ConsStringIteratorOp> op( |
| 8360 | heap->isolate()->objects_string_iterator()); |
| 8361 | StringCharacterStream stream(this, op.value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8362 | |
| 8363 | uc16* result = NewArray<uc16>(length() + 1); |
| 8364 | |
| 8365 | int i = 0; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8366 | while (stream.HasMore()) { |
| 8367 | uint16_t character = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8368 | result[i++] = character; |
| 8369 | } |
| 8370 | result[i] = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8371 | return SmartArrayPointer<uc16>(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8372 | } |
| 8373 | |
| 8374 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8375 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8376 | return reinterpret_cast<uc16*>( |
| 8377 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 8378 | } |
| 8379 | |
| 8380 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8381 | void Relocatable::PostGarbageCollectionProcessing(Isolate* isolate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8382 | Relocatable* current = isolate->relocatable_top(); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8383 | while (current != NULL) { |
| 8384 | current->PostGarbageCollection(); |
| 8385 | current = current->prev_; |
| 8386 | } |
| 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | // Reserve space for statics needing saving and restoring. |
| 8391 | int Relocatable::ArchiveSpacePerThread() { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8392 | return sizeof(Relocatable*); // NOLINT |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8393 | } |
| 8394 | |
| 8395 | |
| 8396 | // Archive statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8397 | char* Relocatable::ArchiveState(Isolate* isolate, char* to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8398 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 8399 | isolate->set_relocatable_top(NULL); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8400 | return to + ArchiveSpacePerThread(); |
| 8401 | } |
| 8402 | |
| 8403 | |
| 8404 | // Restore statics that are thread local. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8405 | char* Relocatable::RestoreState(Isolate* isolate, char* from) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8406 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8407 | return from + ArchiveSpacePerThread(); |
| 8408 | } |
| 8409 | |
| 8410 | |
| 8411 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 8412 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 8413 | Iterate(v, top); |
| 8414 | return thread_storage + ArchiveSpacePerThread(); |
| 8415 | } |
| 8416 | |
| 8417 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 8418 | void Relocatable::Iterate(Isolate* isolate, ObjectVisitor* v) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8419 | Iterate(v, isolate->relocatable_top()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8420 | } |
| 8421 | |
| 8422 | |
| 8423 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 8424 | Relocatable* current = top; |
| 8425 | while (current != NULL) { |
| 8426 | current->IterateInstance(v); |
| 8427 | current = current->prev_; |
| 8428 | } |
| 8429 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8430 | |
| 8431 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8432 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 8433 | : Relocatable(isolate), |
| 8434 | str_(str.location()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8435 | length_(str->length()) { |
| 8436 | PostGarbageCollection(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8440 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 8441 | : Relocatable(isolate), |
| 8442 | str_(0), |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8443 | is_ascii_(true), |
| 8444 | length_(input.length()), |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8445 | start_(input.start()) { } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8446 | |
| 8447 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 8448 | void FlatStringReader::PostGarbageCollection() { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8449 | if (str_ == NULL) return; |
| 8450 | Handle<String> str(str_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8451 | ASSERT(str->IsFlat()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 8452 | DisallowHeapAllocation no_gc; |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 8453 | // This does not actually prevent the vector from being relocated later. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8454 | String::FlatContent content = str->GetFlatContent(); |
| 8455 | ASSERT(content.IsFlat()); |
| 8456 | is_ascii_ = content.IsAscii(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8457 | if (is_ascii_) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8458 | start_ = content.ToOneByteVector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8459 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 8460 | start_ = content.ToUC16Vector().start(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8461 | } |
| 8462 | } |
| 8463 | |
| 8464 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8465 | String* ConsStringIteratorOp::Operate(String* string, |
| 8466 | unsigned* offset_out, |
| 8467 | int32_t* type_out, |
| 8468 | unsigned* length_out) { |
| 8469 | ASSERT(string->IsConsString()); |
| 8470 | ConsString* cons_string = ConsString::cast(string); |
| 8471 | // Set up search data. |
| 8472 | root_ = cons_string; |
| 8473 | consumed_ = *offset_out; |
| 8474 | // Now search. |
| 8475 | return Search(offset_out, type_out, length_out); |
| 8476 | } |
| 8477 | |
| 8478 | |
| 8479 | String* ConsStringIteratorOp::Search(unsigned* offset_out, |
| 8480 | int32_t* type_out, |
| 8481 | unsigned* length_out) { |
| 8482 | ConsString* cons_string = root_; |
| 8483 | // Reset the stack, pushing the root string. |
| 8484 | depth_ = 1; |
| 8485 | maximum_depth_ = 1; |
| 8486 | frames_[0] = cons_string; |
| 8487 | const unsigned consumed = consumed_; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8488 | unsigned offset = 0; |
| 8489 | while (true) { |
| 8490 | // Loop until the string is found which contains the target offset. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8491 | String* string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8492 | unsigned length = string->length(); |
| 8493 | int32_t type; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8494 | if (consumed < offset + length) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8495 | // Target offset is in the left branch. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8496 | // Keep going if we're still in a ConString. |
| 8497 | type = string->map()->instance_type(); |
| 8498 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8499 | cons_string = ConsString::cast(string); |
| 8500 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8501 | continue; |
| 8502 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8503 | // Tell the stack we're done decending. |
| 8504 | AdjustMaximumDepth(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8505 | } else { |
| 8506 | // Descend right. |
| 8507 | // Update progress through the string. |
| 8508 | offset += length; |
| 8509 | // Keep going if we're still in a ConString. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8510 | string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8511 | type = string->map()->instance_type(); |
| 8512 | if ((type & kStringRepresentationMask) == kConsStringTag) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8513 | cons_string = ConsString::cast(string); |
| 8514 | PushRight(cons_string); |
| 8515 | // TODO(dcarney) Add back root optimization. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8516 | continue; |
| 8517 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8518 | // Need this to be updated for the current string. |
| 8519 | length = string->length(); |
| 8520 | // Account for the possibility of an empty right leaf. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8521 | // This happens only if we have asked for an offset outside the string. |
| 8522 | if (length == 0) { |
| 8523 | // Reset depth so future operations will return null immediately. |
| 8524 | Reset(); |
| 8525 | return NULL; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8526 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8527 | // Tell the stack we're done decending. |
| 8528 | AdjustMaximumDepth(); |
| 8529 | // Pop stack so next iteration is in correct place. |
| 8530 | Pop(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8531 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8532 | ASSERT(length != 0); |
| 8533 | // Adjust return values and exit. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8534 | consumed_ = offset + length; |
| 8535 | *offset_out = consumed - offset; |
| 8536 | *type_out = type; |
| 8537 | *length_out = length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8538 | return string; |
| 8539 | } |
| 8540 | UNREACHABLE(); |
| 8541 | return NULL; |
| 8542 | } |
| 8543 | |
| 8544 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8545 | String* ConsStringIteratorOp::NextLeaf(bool* blew_stack, |
| 8546 | int32_t* type_out, |
| 8547 | unsigned* length_out) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8548 | while (true) { |
| 8549 | // Tree traversal complete. |
| 8550 | if (depth_ == 0) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8551 | *blew_stack = false; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8552 | return NULL; |
| 8553 | } |
| 8554 | // We've lost track of higher nodes. |
| 8555 | if (maximum_depth_ - depth_ == kStackSize) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8556 | *blew_stack = true; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8557 | return NULL; |
| 8558 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8559 | // Go right. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8560 | ConsString* cons_string = frames_[OffsetForDepth(depth_ - 1)]; |
| 8561 | String* string = cons_string->second(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8562 | int32_t type = string->map()->instance_type(); |
| 8563 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8564 | // Pop stack so next iteration is in correct place. |
| 8565 | Pop(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8566 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8567 | // Could be a flattened ConsString. |
| 8568 | if (length == 0) continue; |
| 8569 | *length_out = length; |
| 8570 | *type_out = type; |
| 8571 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8572 | return string; |
| 8573 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8574 | cons_string = ConsString::cast(string); |
| 8575 | // TODO(dcarney) Add back root optimization. |
| 8576 | PushRight(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8577 | // Need to traverse all the way left. |
| 8578 | while (true) { |
| 8579 | // Continue left. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8580 | string = cons_string->first(); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8581 | type = string->map()->instance_type(); |
| 8582 | if ((type & kStringRepresentationMask) != kConsStringTag) { |
| 8583 | AdjustMaximumDepth(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8584 | unsigned length = static_cast<unsigned>(string->length()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8585 | ASSERT(length != 0); |
| 8586 | *length_out = length; |
| 8587 | *type_out = type; |
| 8588 | consumed_ += length; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8589 | return string; |
| 8590 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8591 | cons_string = ConsString::cast(string); |
| 8592 | PushLeft(cons_string); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 8593 | } |
| 8594 | } |
| 8595 | UNREACHABLE(); |
| 8596 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8597 | } |
| 8598 | |
| 8599 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8600 | uint16_t ConsString::ConsStringGet(int index) { |
| 8601 | ASSERT(index >= 0 && index < this->length()); |
| 8602 | |
| 8603 | // Check for a flattened cons string |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8604 | if (second()->length() == 0) { |
| 8605 | String* left = first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8606 | return left->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8607 | } |
| 8608 | |
| 8609 | String* string = String::cast(this); |
| 8610 | |
| 8611 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8612 | if (StringShape(string).IsCons()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8613 | ConsString* cons_string = ConsString::cast(string); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8614 | String* left = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8615 | if (left->length() > index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8616 | string = left; |
| 8617 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8618 | index -= left->length(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8619 | string = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8620 | } |
| 8621 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8622 | return string->Get(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8623 | } |
| 8624 | } |
| 8625 | |
| 8626 | UNREACHABLE(); |
| 8627 | return 0; |
| 8628 | } |
| 8629 | |
| 8630 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8631 | uint16_t SlicedString::SlicedStringGet(int index) { |
| 8632 | return parent()->Get(offset() + index); |
| 8633 | } |
| 8634 | |
| 8635 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8636 | template <typename sinkchar> |
| 8637 | void String::WriteToFlat(String* src, |
| 8638 | sinkchar* sink, |
| 8639 | int f, |
| 8640 | int t) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8641 | String* source = src; |
| 8642 | int from = f; |
| 8643 | int to = t; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8644 | while (true) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8645 | ASSERT(0 <= from && from <= to && to <= source->length()); |
| 8646 | switch (StringShape(source).full_representation_tag()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8647 | case kOneByteStringTag | kExternalStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8648 | CopyChars(sink, |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8649 | ExternalAsciiString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8650 | to - from); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8651 | return; |
| 8652 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8653 | case kTwoByteStringTag | kExternalStringTag: { |
| 8654 | const uc16* data = |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 8655 | ExternalTwoByteString::cast(source)->GetChars(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8656 | CopyChars(sink, |
| 8657 | data + from, |
| 8658 | to - from); |
| 8659 | return; |
| 8660 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8661 | case kOneByteStringTag | kSeqStringTag: { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8662 | CopyChars(sink, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8663 | SeqOneByteString::cast(source)->GetChars() + from, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8664 | to - from); |
| 8665 | return; |
| 8666 | } |
| 8667 | case kTwoByteStringTag | kSeqStringTag: { |
| 8668 | CopyChars(sink, |
| 8669 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 8670 | to - from); |
| 8671 | return; |
| 8672 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8673 | case kOneByteStringTag | kConsStringTag: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8674 | case kTwoByteStringTag | kConsStringTag: { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8675 | ConsString* cons_string = ConsString::cast(source); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8676 | String* first = cons_string->first(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8677 | int boundary = first->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8678 | if (to - boundary >= boundary - from) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8679 | // Right hand side is longer. Recurse over left. |
| 8680 | if (from < boundary) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8681 | WriteToFlat(first, sink, from, boundary); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8682 | sink += boundary - from; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8683 | from = 0; |
| 8684 | } else { |
| 8685 | from -= boundary; |
| 8686 | } |
| 8687 | to -= boundary; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8688 | source = cons_string->second(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8689 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8690 | // Left hand side is longer. Recurse over right. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8691 | if (to > boundary) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8692 | String* second = cons_string->second(); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8693 | // When repeatedly appending to a string, we get a cons string that |
| 8694 | // is unbalanced to the left, a list, essentially. We inline the |
| 8695 | // common case of sequential ascii right child. |
| 8696 | if (to - boundary == 1) { |
| 8697 | sink[boundary - from] = static_cast<sinkchar>(second->Get(0)); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8698 | } else if (second->IsSeqOneByteString()) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8699 | CopyChars(sink + boundary - from, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 8700 | SeqOneByteString::cast(second)->GetChars(), |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8701 | to - boundary); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 8702 | } else { |
| 8703 | WriteToFlat(second, |
| 8704 | sink + boundary - from, |
| 8705 | 0, |
| 8706 | to - boundary); |
| 8707 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8708 | to = boundary; |
| 8709 | } |
| 8710 | source = first; |
| 8711 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8712 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8713 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 8714 | case kOneByteStringTag | kSlicedStringTag: |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 8715 | case kTwoByteStringTag | kSlicedStringTag: { |
| 8716 | SlicedString* slice = SlicedString::cast(source); |
| 8717 | unsigned offset = slice->offset(); |
| 8718 | WriteToFlat(slice->parent(), sink, from + offset, to + offset); |
| 8719 | return; |
| 8720 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8721 | } |
| 8722 | } |
| 8723 | } |
| 8724 | |
| 8725 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8726 | // Compares the contents of two strings by reading and comparing |
| 8727 | // int-sized blocks of characters. |
| 8728 | template <typename Char> |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8729 | static inline bool CompareRawStringContents(const Char* const a, |
| 8730 | const Char* const b, |
| 8731 | int length) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8732 | int i = 0; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8733 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8734 | // If this architecture isn't comfortable reading unaligned ints |
| 8735 | // then we have to check that the strings are aligned before |
| 8736 | // comparing them blockwise. |
| 8737 | const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8738 | uint32_t pa_addr = reinterpret_cast<uint32_t>(a); |
| 8739 | uint32_t pb_addr = reinterpret_cast<uint32_t>(b); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 8740 | if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8741 | #endif |
| 8742 | const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT |
| 8743 | int endpoint = length - kStepSize; |
| 8744 | // Compare blocks until we reach near the end of the string. |
| 8745 | for (; i <= endpoint; i += kStepSize) { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8746 | uint32_t wa = *reinterpret_cast<const uint32_t*>(a + i); |
| 8747 | uint32_t wb = *reinterpret_cast<const uint32_t*>(b + i); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8748 | if (wa != wb) { |
| 8749 | return false; |
| 8750 | } |
| 8751 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 8752 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8753 | } |
| 8754 | #endif |
| 8755 | // Compare the remaining characters that didn't fit into a block. |
| 8756 | for (; i < length; i++) { |
| 8757 | if (a[i] != b[i]) { |
| 8758 | return false; |
| 8759 | } |
| 8760 | } |
| 8761 | return true; |
| 8762 | } |
| 8763 | |
| 8764 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8765 | template<typename Chars1, typename Chars2> |
| 8766 | class RawStringComparator : public AllStatic { |
| 8767 | public: |
| 8768 | static inline bool compare(const Chars1* a, const Chars2* b, int len) { |
| 8769 | ASSERT(sizeof(Chars1) != sizeof(Chars2)); |
| 8770 | for (int i = 0; i < len; i++) { |
| 8771 | if (a[i] != b[i]) { |
| 8772 | return false; |
| 8773 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8774 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8775 | return true; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8776 | } |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8777 | }; |
| 8778 | |
| 8779 | |
| 8780 | template<> |
| 8781 | class RawStringComparator<uint16_t, uint16_t> { |
| 8782 | public: |
| 8783 | static inline bool compare(const uint16_t* a, const uint16_t* b, int len) { |
| 8784 | return CompareRawStringContents(a, b, len); |
| 8785 | } |
| 8786 | }; |
| 8787 | |
| 8788 | |
| 8789 | template<> |
| 8790 | class RawStringComparator<uint8_t, uint8_t> { |
| 8791 | public: |
| 8792 | static inline bool compare(const uint8_t* a, const uint8_t* b, int len) { |
| 8793 | return CompareRawStringContents(a, b, len); |
| 8794 | } |
| 8795 | }; |
| 8796 | |
| 8797 | |
| 8798 | class StringComparator { |
| 8799 | class State { |
| 8800 | public: |
| 8801 | explicit inline State(ConsStringIteratorOp* op) |
| 8802 | : op_(op), is_one_byte_(true), length_(0), buffer8_(NULL) {} |
| 8803 | |
| 8804 | inline void Init(String* string, unsigned len) { |
| 8805 | op_->Reset(); |
| 8806 | int32_t type = string->map()->instance_type(); |
| 8807 | String::Visit(string, 0, *this, *op_, type, len); |
| 8808 | } |
| 8809 | |
| 8810 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 8811 | is_one_byte_ = true; |
| 8812 | buffer8_ = chars; |
| 8813 | length_ = length; |
| 8814 | } |
| 8815 | |
| 8816 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 8817 | is_one_byte_ = false; |
| 8818 | buffer16_ = chars; |
| 8819 | length_ = length; |
| 8820 | } |
| 8821 | |
| 8822 | void Advance(unsigned consumed) { |
| 8823 | ASSERT(consumed <= length_); |
| 8824 | // Still in buffer. |
| 8825 | if (length_ != consumed) { |
| 8826 | if (is_one_byte_) { |
| 8827 | buffer8_ += consumed; |
| 8828 | } else { |
| 8829 | buffer16_ += consumed; |
| 8830 | } |
| 8831 | length_ -= consumed; |
| 8832 | return; |
| 8833 | } |
| 8834 | // Advance state. |
| 8835 | ASSERT(op_->HasMore()); |
| 8836 | int32_t type = 0; |
| 8837 | unsigned length = 0; |
| 8838 | String* next = op_->ContinueOperation(&type, &length); |
| 8839 | ASSERT(next != NULL); |
| 8840 | ConsStringNullOp null_op; |
| 8841 | String::Visit(next, 0, *this, null_op, type, length); |
| 8842 | } |
| 8843 | |
| 8844 | ConsStringIteratorOp* const op_; |
| 8845 | bool is_one_byte_; |
| 8846 | unsigned length_; |
| 8847 | union { |
| 8848 | const uint8_t* buffer8_; |
| 8849 | const uint16_t* buffer16_; |
| 8850 | }; |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 8851 | |
| 8852 | private: |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8853 | DISALLOW_IMPLICIT_CONSTRUCTORS(State); |
| 8854 | }; |
| 8855 | |
| 8856 | public: |
| 8857 | inline StringComparator(ConsStringIteratorOp* op_1, |
| 8858 | ConsStringIteratorOp* op_2) |
| 8859 | : state_1_(op_1), |
| 8860 | state_2_(op_2) { |
| 8861 | } |
| 8862 | |
| 8863 | template<typename Chars1, typename Chars2> |
| 8864 | static inline bool Equals(State* state_1, State* state_2, unsigned to_check) { |
| 8865 | const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_); |
| 8866 | const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_); |
| 8867 | return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check); |
| 8868 | } |
| 8869 | |
| 8870 | bool Equals(unsigned length, String* string_1, String* string_2) { |
| 8871 | ASSERT(length != 0); |
| 8872 | state_1_.Init(string_1, length); |
| 8873 | state_2_.Init(string_2, length); |
| 8874 | while (true) { |
| 8875 | unsigned to_check = Min(state_1_.length_, state_2_.length_); |
| 8876 | ASSERT(to_check > 0 && to_check <= length); |
| 8877 | bool is_equal; |
| 8878 | if (state_1_.is_one_byte_) { |
| 8879 | if (state_2_.is_one_byte_) { |
| 8880 | is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8881 | } else { |
| 8882 | is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8883 | } |
| 8884 | } else { |
| 8885 | if (state_2_.is_one_byte_) { |
| 8886 | is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check); |
| 8887 | } else { |
| 8888 | is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check); |
| 8889 | } |
| 8890 | } |
| 8891 | // Looping done. |
| 8892 | if (!is_equal) return false; |
| 8893 | length -= to_check; |
| 8894 | // Exit condition. Strings are equal. |
| 8895 | if (length == 0) return true; |
| 8896 | state_1_.Advance(to_check); |
| 8897 | state_2_.Advance(to_check); |
| 8898 | } |
| 8899 | } |
| 8900 | |
| 8901 | private: |
| 8902 | State state_1_; |
| 8903 | State state_2_; |
| 8904 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringComparator); |
| 8905 | }; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 8906 | |
| 8907 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8908 | bool String::SlowEquals(String* other) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8909 | // Fast check: negative check with lengths. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8910 | int len = length(); |
| 8911 | if (len != other->length()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8912 | if (len == 0) return true; |
| 8913 | |
| 8914 | // Fast check: if hash code is computed for both strings |
| 8915 | // a fast negative check can be performed. |
| 8916 | if (HasHashCode() && other->HasHashCode()) { |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 8917 | #ifdef ENABLE_SLOW_ASSERTS |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8918 | if (FLAG_enable_slow_asserts) { |
| 8919 | if (Hash() != other->Hash()) { |
| 8920 | bool found_difference = false; |
| 8921 | for (int i = 0; i < len; i++) { |
| 8922 | if (Get(i) != other->Get(i)) { |
| 8923 | found_difference = true; |
| 8924 | break; |
| 8925 | } |
| 8926 | } |
| 8927 | ASSERT(found_difference); |
| 8928 | } |
| 8929 | } |
| 8930 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8931 | if (Hash() != other->Hash()) return false; |
| 8932 | } |
| 8933 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 8934 | // We know the strings are both non-empty. Compare the first chars |
| 8935 | // before we try to flatten the strings. |
| 8936 | if (this->Get(0) != other->Get(0)) return false; |
| 8937 | |
| 8938 | String* lhs = this->TryFlattenGetString(); |
| 8939 | String* rhs = other->TryFlattenGetString(); |
| 8940 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8941 | // 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] | 8942 | if (StringShape(lhs).IsSequentialAscii() && |
| 8943 | StringShape(rhs).IsSequentialAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 8944 | const uint8_t* str1 = SeqOneByteString::cast(lhs)->GetChars(); |
| 8945 | const uint8_t* str2 = SeqOneByteString::cast(rhs)->GetChars(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8946 | return CompareRawStringContents(str1, str2, len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8949 | Isolate* isolate = GetIsolate(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 8950 | StringComparator comparator(isolate->objects_string_compare_iterator_a(), |
| 8951 | isolate->objects_string_compare_iterator_b()); |
| 8952 | |
| 8953 | return comparator.Equals(static_cast<unsigned>(len), lhs, rhs); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8954 | } |
| 8955 | |
| 8956 | |
| 8957 | bool String::MarkAsUndetectable() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 8958 | if (StringShape(this).IsInternalized()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8959 | |
| 8960 | Map* map = this->map(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8961 | Heap* heap = GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8962 | if (map == heap->string_map()) { |
| 8963 | this->set_map(heap->undetectable_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8964 | return true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8965 | } else if (map == heap->ascii_string_map()) { |
| 8966 | this->set_map(heap->undetectable_ascii_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8967 | return true; |
| 8968 | } |
| 8969 | // Rest cannot be marked as undetectable |
| 8970 | return false; |
| 8971 | } |
| 8972 | |
| 8973 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 8974 | bool String::IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8975 | int slen = length(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8976 | // Can't check exact length equality, but we can check bounds. |
| 8977 | int str_len = str.length(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 8978 | if (!allow_prefix_match && |
| 8979 | (str_len < slen || |
| 8980 | str_len > slen*static_cast<int>(unibrow::Utf8::kMaxEncodedSize))) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8981 | return false; |
| 8982 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8983 | int i; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8984 | unsigned remaining_in_str = static_cast<unsigned>(str_len); |
| 8985 | const uint8_t* utf8_data = reinterpret_cast<const uint8_t*>(str.start()); |
| 8986 | for (i = 0; i < slen && remaining_in_str > 0; i++) { |
| 8987 | unsigned cursor = 0; |
| 8988 | uint32_t r = unibrow::Utf8::ValueOf(utf8_data, remaining_in_str, &cursor); |
| 8989 | ASSERT(cursor > 0 && cursor <= remaining_in_str); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 8990 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 8991 | if (i > slen - 1) return false; |
| 8992 | if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false; |
| 8993 | if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false; |
| 8994 | } else { |
| 8995 | if (Get(i) != r) return false; |
| 8996 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 8997 | utf8_data += cursor; |
| 8998 | remaining_in_str -= cursor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8999 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9000 | return (allow_prefix_match || i == slen) && remaining_in_str == 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9001 | } |
| 9002 | |
| 9003 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9004 | bool String::IsOneByteEqualTo(Vector<const uint8_t> str) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 9005 | int slen = length(); |
| 9006 | if (str.length() != slen) return false; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 9007 | DisallowHeapAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9008 | FlatContent content = GetFlatContent(); |
| 9009 | if (content.IsAscii()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9010 | return CompareChars(content.ToOneByteVector().start(), |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9011 | str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 9012 | } |
| 9013 | for (int i = 0; i < slen; i++) { |
| 9014 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 9015 | } |
| 9016 | return true; |
| 9017 | } |
| 9018 | |
| 9019 | |
| 9020 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 9021 | int slen = length(); |
| 9022 | if (str.length() != slen) return false; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 9023 | DisallowHeapAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9024 | FlatContent content = GetFlatContent(); |
| 9025 | if (content.IsTwoByte()) { |
| 9026 | return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 9027 | } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 9028 | for (int i = 0; i < slen; i++) { |
| 9029 | if (Get(i) != str[i]) return false; |
| 9030 | } |
| 9031 | return true; |
| 9032 | } |
| 9033 | |
| 9034 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9035 | class IteratingStringHasher: public StringHasher { |
| 9036 | public: |
| 9037 | static inline uint32_t Hash(String* string, uint32_t seed) { |
| 9038 | const unsigned len = static_cast<unsigned>(string->length()); |
| 9039 | IteratingStringHasher hasher(len, seed); |
| 9040 | if (hasher.has_trivial_hash()) { |
| 9041 | return hasher.GetHashField(); |
| 9042 | } |
| 9043 | int32_t type = string->map()->instance_type(); |
| 9044 | ConsStringNullOp null_op; |
| 9045 | String::Visit(string, 0, hasher, null_op, type, len); |
| 9046 | // Flat strings terminate immediately. |
| 9047 | if (hasher.consumed_ == len) { |
| 9048 | ASSERT(!string->IsConsString()); |
| 9049 | return hasher.GetHashField(); |
| 9050 | } |
| 9051 | ASSERT(string->IsConsString()); |
| 9052 | // This is a ConsString, iterate across it. |
| 9053 | ConsStringIteratorOp op; |
| 9054 | unsigned offset = 0; |
| 9055 | unsigned leaf_length = len; |
| 9056 | string = op.Operate(string, &offset, &type, &leaf_length); |
| 9057 | while (true) { |
| 9058 | ASSERT(hasher.consumed_ < len); |
| 9059 | String::Visit(string, 0, hasher, null_op, type, leaf_length); |
| 9060 | if (hasher.consumed_ == len) break; |
| 9061 | string = op.ContinueOperation(&type, &leaf_length); |
| 9062 | // This should be taken care of by the length check. |
| 9063 | ASSERT(string != NULL); |
| 9064 | } |
| 9065 | return hasher.GetHashField(); |
| 9066 | } |
| 9067 | inline void VisitOneByteString(const uint8_t* chars, unsigned length) { |
| 9068 | AddCharacters(chars, static_cast<int>(length)); |
| 9069 | consumed_ += length; |
| 9070 | } |
| 9071 | inline void VisitTwoByteString(const uint16_t* chars, unsigned length) { |
| 9072 | AddCharacters(chars, static_cast<int>(length)); |
| 9073 | consumed_ += length; |
| 9074 | } |
| 9075 | |
| 9076 | private: |
| 9077 | inline IteratingStringHasher(int len, uint32_t seed) |
| 9078 | : StringHasher(len, seed), |
| 9079 | consumed_(0) {} |
| 9080 | unsigned consumed_; |
| 9081 | DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher); |
| 9082 | }; |
| 9083 | |
| 9084 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9085 | uint32_t String::ComputeAndSetHash() { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 9086 | // 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] | 9087 | ASSERT(!HasHashCode()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9088 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9089 | // Store the hash code in the object. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9090 | uint32_t field = IteratingStringHasher::Hash(this, GetHeap()->HashSeed()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9091 | set_hash_field(field); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9092 | |
| 9093 | // Check the hash code is there. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9094 | ASSERT(HasHashCode()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 9095 | uint32_t result = field >> kHashShift; |
| 9096 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 9097 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9098 | } |
| 9099 | |
| 9100 | |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9101 | bool String::ComputeArrayIndex(uint32_t* index) { |
| 9102 | int length = this->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9103 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9104 | ConsStringIteratorOp op; |
| 9105 | StringCharacterStream stream(this, &op); |
| 9106 | uint16_t ch = stream.GetNext(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9107 | |
| 9108 | // If the string begins with a '0' character, it must only consist |
| 9109 | // of it to be a legal array index. |
| 9110 | if (ch == '0') { |
| 9111 | *index = 0; |
| 9112 | return length == 1; |
| 9113 | } |
| 9114 | |
| 9115 | // Convert string to uint32 array index; character by character. |
| 9116 | int d = ch - '0'; |
| 9117 | if (d < 0 || d > 9) return false; |
| 9118 | uint32_t result = d; |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9119 | while (stream.HasMore()) { |
| 9120 | d = stream.GetNext() - '0'; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9121 | if (d < 0 || d > 9) return false; |
| 9122 | // Check that the new result is below the 32 bit limit. |
| 9123 | if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 9124 | result = (result * 10) + d; |
| 9125 | } |
| 9126 | |
| 9127 | *index = result; |
| 9128 | return true; |
| 9129 | } |
| 9130 | |
| 9131 | |
| 9132 | bool String::SlowAsArrayIndex(uint32_t* index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9133 | if (length() <= kMaxCachedArrayIndexLength) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9134 | Hash(); // force computation of hash code |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9135 | uint32_t field = hash_field(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9136 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9137 | // Isolate the array index form the full hash field. |
| 9138 | *index = (kArrayIndexHashMask & field) >> kHashShift; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9139 | return true; |
| 9140 | } else { |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 9141 | return ComputeArrayIndex(index); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9142 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9146 | Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) { |
| 9147 | int new_size, old_size; |
| 9148 | int old_length = string->length(); |
| 9149 | if (old_length <= new_length) return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9150 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9151 | if (string->IsSeqOneByteString()) { |
| 9152 | old_size = SeqOneByteString::SizeFor(old_length); |
| 9153 | new_size = SeqOneByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9154 | } else { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9155 | ASSERT(string->IsSeqTwoByteString()); |
| 9156 | old_size = SeqTwoByteString::SizeFor(old_length); |
| 9157 | new_size = SeqTwoByteString::SizeFor(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9158 | } |
| 9159 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9160 | int delta = old_size - new_size; |
| 9161 | string->set_length(new_length); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9162 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9163 | Address start_of_string = string->address(); |
| 9164 | ASSERT_OBJECT_ALIGNED(start_of_string); |
| 9165 | ASSERT_OBJECT_ALIGNED(start_of_string + new_size); |
| 9166 | |
| 9167 | Heap* heap = string->GetHeap(); |
| 9168 | NewSpace* newspace = heap->new_space(); |
| 9169 | if (newspace->Contains(start_of_string) && |
| 9170 | newspace->top() == start_of_string + old_size) { |
| 9171 | // Last allocated object in new space. Simply lower allocation top. |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 9172 | newspace->set_top(start_of_string + new_size); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9173 | } else { |
| 9174 | // Sizes are pointer size aligned, so that we can use filler objects |
| 9175 | // that are a multiple of pointer size. |
| 9176 | heap->CreateFillerObjectAt(start_of_string + new_size, delta); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9177 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9178 | if (Marking::IsBlack(Marking::MarkBitFrom(start_of_string))) { |
| 9179 | MemoryChunk::IncrementLiveBytesFromMutator(start_of_string, -delta); |
| 9180 | } |
| 9181 | |
| 9182 | |
| 9183 | if (new_length == 0) return heap->isolate()->factory()->empty_string(); |
| 9184 | return string; |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 9185 | } |
| 9186 | |
| 9187 | |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 9188 | AllocationMemento* AllocationMemento::FindForJSObject(JSObject* object, |
| 9189 | bool in_GC) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9190 | // Currently, AllocationMemento objects are only allocated immediately |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9191 | // after JSArrays in NewSpace, and detecting whether a JSArray has one |
| 9192 | // involves carefully checking the object immediately after the JSArray |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9193 | // (if there is one) to see if it's an AllocationMemento. |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9194 | if (FLAG_track_allocation_sites && object->GetHeap()->InNewSpace(object)) { |
| 9195 | Address ptr_end = (reinterpret_cast<Address>(object) - kHeapObjectTag) + |
| 9196 | object->Size(); |
jkummerow@chromium.org | d8a3a14 | 2013-10-03 12:15:05 +0000 | [diff] [blame] | 9197 | Address top; |
| 9198 | if (in_GC) { |
| 9199 | top = object->GetHeap()->new_space()->FromSpacePageHigh(); |
| 9200 | } else { |
| 9201 | top = object->GetHeap()->NewSpaceTop(); |
| 9202 | } |
| 9203 | if ((ptr_end + AllocationMemento::kSize) <= top) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9204 | // 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] | 9205 | Map** possible_allocation_memento_map = |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9206 | reinterpret_cast<Map**>(ptr_end); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9207 | if (*possible_allocation_memento_map == |
| 9208 | object->GetHeap()->allocation_memento_map()) { |
| 9209 | AllocationMemento* memento = AllocationMemento::cast( |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 9210 | reinterpret_cast<Object*>(ptr_end + kHeapObjectTag)); |
hpayer@chromium.org | 27ce874 | 2013-09-19 09:59:01 +0000 | [diff] [blame] | 9211 | return memento; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 9212 | } |
| 9213 | } |
| 9214 | } |
| 9215 | return NULL; |
| 9216 | } |
| 9217 | |
| 9218 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9219 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9220 | // For array indexes mix the length into the hash as an array index could |
| 9221 | // be zero. |
| 9222 | ASSERT(length > 0); |
| 9223 | ASSERT(length <= String::kMaxArrayIndexSize); |
| 9224 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 9225 | (1 << String::kArrayIndexValueBits)); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9226 | |
| 9227 | value <<= String::kHashShift; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9228 | value |= length << String::kArrayIndexHashLengthShift; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 9229 | |
| 9230 | ASSERT((value & String::kIsNotArrayIndexMask) == 0); |
| 9231 | ASSERT((length > String::kMaxCachedArrayIndexLength) || |
| 9232 | (value & String::kContainsCachedArrayIndexMask) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9233 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9234 | } |
| 9235 | |
| 9236 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9237 | uint32_t StringHasher::GetHashField() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9238 | if (length_ <= String::kMaxHashCalcLength) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9239 | if (is_array_index_) { |
| 9240 | return MakeArrayIndexHash(array_index_, length_); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9241 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9242 | return (GetHashCore(raw_running_hash_) << String::kHashShift) | |
| 9243 | String::kIsNotArrayIndexMask; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9244 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 9245 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9246 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9247 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9248 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9249 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9250 | uint32_t StringHasher::ComputeUtf8Hash(Vector<const char> chars, |
| 9251 | uint32_t seed, |
| 9252 | int* utf16_length_out) { |
| 9253 | int vector_length = chars.length(); |
| 9254 | // Handle some edge cases |
| 9255 | if (vector_length <= 1) { |
| 9256 | ASSERT(vector_length == 0 || |
| 9257 | static_cast<uint8_t>(chars.start()[0]) <= |
| 9258 | unibrow::Utf8::kMaxOneByteChar); |
| 9259 | *utf16_length_out = vector_length; |
| 9260 | return HashSequentialString(chars.start(), vector_length, seed); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9261 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9262 | // Start with a fake length which won't affect computation. |
| 9263 | // It will be updated later. |
| 9264 | StringHasher hasher(String::kMaxArrayIndexSize, seed); |
| 9265 | unsigned remaining = static_cast<unsigned>(vector_length); |
| 9266 | const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start()); |
| 9267 | int utf16_length = 0; |
| 9268 | bool is_index = true; |
| 9269 | ASSERT(hasher.is_array_index_); |
| 9270 | while (remaining > 0) { |
| 9271 | unsigned consumed = 0; |
| 9272 | uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed); |
| 9273 | ASSERT(consumed > 0 && consumed <= remaining); |
| 9274 | stream += consumed; |
| 9275 | remaining -= consumed; |
| 9276 | bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode; |
| 9277 | utf16_length += is_two_characters ? 2 : 1; |
| 9278 | // No need to keep hashing. But we do need to calculate utf16_length. |
| 9279 | if (utf16_length > String::kMaxHashCalcLength) continue; |
| 9280 | if (is_two_characters) { |
| 9281 | uint16_t c1 = unibrow::Utf16::LeadSurrogate(c); |
| 9282 | uint16_t c2 = unibrow::Utf16::TrailSurrogate(c); |
| 9283 | hasher.AddCharacter(c1); |
| 9284 | hasher.AddCharacter(c2); |
| 9285 | if (is_index) is_index = hasher.UpdateIndex(c1); |
| 9286 | if (is_index) is_index = hasher.UpdateIndex(c2); |
| 9287 | } else { |
| 9288 | hasher.AddCharacter(c); |
| 9289 | if (is_index) is_index = hasher.UpdateIndex(c); |
| 9290 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9291 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9292 | *utf16_length_out = static_cast<int>(utf16_length); |
| 9293 | // Must set length here so that hash computation is correct. |
| 9294 | hasher.length_ = utf16_length; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 9295 | return hasher.GetHashField(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9299 | MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9300 | Heap* heap = GetHeap(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9301 | if (start == 0 && end == length()) return this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9302 | MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9303 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9304 | } |
| 9305 | |
| 9306 | |
| 9307 | void String::PrintOn(FILE* file) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9308 | int length = this->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9309 | for (int i = 0; i < length; i++) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 9310 | PrintF(file, "%c", Get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9311 | } |
| 9312 | } |
| 9313 | |
| 9314 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9315 | static void TrimEnumCache(Heap* heap, Map* map, DescriptorArray* descriptors) { |
| 9316 | int live_enum = map->EnumLength(); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 9317 | if (live_enum == kInvalidEnumCacheSentinel) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9318 | live_enum = map->NumberOfDescribedProperties(OWN_DESCRIPTORS, DONT_ENUM); |
| 9319 | } |
| 9320 | if (live_enum == 0) return descriptors->ClearEnumCache(); |
| 9321 | |
| 9322 | FixedArray* enum_cache = descriptors->GetEnumCache(); |
| 9323 | |
| 9324 | int to_trim = enum_cache->length() - live_enum; |
| 9325 | if (to_trim <= 0) return; |
| 9326 | RightTrimFixedArray<FROM_GC>(heap, descriptors->GetEnumCache(), to_trim); |
| 9327 | |
| 9328 | if (!descriptors->HasEnumIndicesCache()) return; |
| 9329 | FixedArray* enum_indices_cache = descriptors->GetEnumIndicesCache(); |
| 9330 | RightTrimFixedArray<FROM_GC>(heap, enum_indices_cache, to_trim); |
| 9331 | } |
| 9332 | |
| 9333 | |
| 9334 | static void TrimDescriptorArray(Heap* heap, |
| 9335 | Map* map, |
| 9336 | DescriptorArray* descriptors, |
| 9337 | int number_of_own_descriptors) { |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9338 | int number_of_descriptors = descriptors->number_of_descriptors_storage(); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9339 | int to_trim = number_of_descriptors - number_of_own_descriptors; |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9340 | if (to_trim == 0) return; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9341 | |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 9342 | RightTrimFixedArray<FROM_GC>( |
| 9343 | heap, descriptors, to_trim * DescriptorArray::kDescriptorSize); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9344 | descriptors->SetNumberOfDescriptors(number_of_own_descriptors); |
| 9345 | |
| 9346 | if (descriptors->HasEnumCache()) TrimEnumCache(heap, map, descriptors); |
| 9347 | descriptors->Sort(); |
| 9348 | } |
| 9349 | |
| 9350 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9351 | // Clear a possible back pointer in case the transition leads to a dead map. |
| 9352 | // 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] | 9353 | static bool ClearBackPointer(Heap* heap, Map* target) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9354 | if (Marking::MarkBitFrom(target).Get()) return false; |
| 9355 | target->SetBackPointer(heap->undefined_value(), SKIP_WRITE_BARRIER); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9356 | return true; |
| 9357 | } |
| 9358 | |
| 9359 | |
| 9360 | // TODO(mstarzinger): This method should be moved into MarkCompactCollector, |
| 9361 | // because it cannot be called from outside the GC and we already have methods |
| 9362 | // depending on the transitions layout in the GC anyways. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 9363 | void Map::ClearNonLiveTransitions(Heap* heap) { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9364 | // If there are no transitions to be cleared, return. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9365 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 9366 | // properly cleared. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9367 | if (!HasTransitionArray()) return; |
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 | TransitionArray* t = transitions(); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 9370 | MarkCompactCollector* collector = heap->mark_compact_collector(); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9371 | |
| 9372 | int transition_index = 0; |
| 9373 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9374 | DescriptorArray* descriptors = instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9375 | bool descriptors_owner_died = false; |
| 9376 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9377 | // Compact all live descriptors to the left. |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9378 | for (int i = 0; i < t->number_of_transitions(); ++i) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9379 | Map* target = t->GetTarget(i); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9380 | if (ClearBackPointer(heap, target)) { |
| 9381 | if (target->instance_descriptors() == descriptors) { |
| 9382 | descriptors_owner_died = true; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9383 | } |
| 9384 | } else { |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9385 | if (i != transition_index) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 9386 | Name* key = t->GetKey(i); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9387 | t->SetKey(transition_index, key); |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9388 | Object** key_slot = t->GetKeySlot(transition_index); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9389 | collector->RecordSlot(key_slot, key_slot, key); |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 9390 | // Target slots do not need to be recorded since maps are not compacted. |
| 9391 | t->SetTarget(transition_index, t->GetTarget(i)); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9392 | } |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9393 | transition_index++; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9394 | } |
| 9395 | } |
| 9396 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 9397 | // If there are no transitions to be cleared, return. |
| 9398 | // TODO(verwaest) Should be an assert, otherwise back pointers are not |
| 9399 | // properly cleared. |
| 9400 | if (transition_index == t->number_of_transitions()) return; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9401 | |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9402 | int number_of_own_descriptors = NumberOfOwnDescriptors(); |
| 9403 | |
| 9404 | if (descriptors_owner_died) { |
| 9405 | if (number_of_own_descriptors > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9406 | TrimDescriptorArray(heap, this, descriptors, number_of_own_descriptors); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9407 | ASSERT(descriptors->number_of_descriptors() == number_of_own_descriptors); |
mstarzinger@chromium.org | 2ed0d02 | 2013-10-17 08:06:21 +0000 | [diff] [blame] | 9408 | set_owns_descriptors(true); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9409 | } else { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 9410 | ASSERT(descriptors == GetHeap()->empty_descriptor_array()); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9411 | } |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 9412 | } |
| 9413 | |
yangguo@chromium.org | 99aa490 | 2012-07-06 16:21:55 +0000 | [diff] [blame] | 9414 | int trim = t->number_of_transitions() - transition_index; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 9415 | if (trim > 0) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 9416 | RightTrimFixedArray<FROM_GC>(heap, t, t->IsSimpleTransition() |
| 9417 | ? trim : trim * TransitionArray::kTransitionSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9418 | } |
| 9419 | } |
| 9420 | |
| 9421 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9422 | int Map::Hash() { |
| 9423 | // For performance reasons we only hash the 3 most variable fields of a map: |
| 9424 | // constructor, prototype and bit_field2. |
| 9425 | |
| 9426 | // Shift away the tag. |
| 9427 | int hash = (static_cast<uint32_t>( |
| 9428 | reinterpret_cast<uintptr_t>(constructor())) >> 2); |
| 9429 | |
| 9430 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 9431 | // when the two pointers are close (which is fairly common). |
| 9432 | // To avoid this we shift the prototype 4 bits relatively to the constructor. |
| 9433 | hash ^= (static_cast<uint32_t>( |
| 9434 | reinterpret_cast<uintptr_t>(prototype())) << 2); |
| 9435 | |
| 9436 | return hash ^ (hash >> 16) ^ bit_field2(); |
| 9437 | } |
| 9438 | |
| 9439 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9440 | static bool CheckEquivalent(Map* first, Map* second) { |
| 9441 | return |
| 9442 | first->constructor() == second->constructor() && |
| 9443 | first->prototype() == second->prototype() && |
| 9444 | first->instance_type() == second->instance_type() && |
| 9445 | first->bit_field() == second->bit_field() && |
| 9446 | first->bit_field2() == second->bit_field2() && |
| 9447 | first->is_observed() == second->is_observed() && |
| 9448 | first->function_with_prototype() == second->function_with_prototype(); |
| 9449 | } |
| 9450 | |
| 9451 | |
| 9452 | bool Map::EquivalentToForTransition(Map* other) { |
| 9453 | return CheckEquivalent(this, other); |
| 9454 | } |
| 9455 | |
| 9456 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9457 | bool Map::EquivalentToForNormalization(Map* other, |
| 9458 | PropertyNormalizationMode mode) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9459 | int properties = mode == CLEAR_INOBJECT_PROPERTIES |
| 9460 | ? 0 : other->inobject_properties(); |
| 9461 | return CheckEquivalent(this, other) && inobject_properties() == properties; |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9462 | } |
| 9463 | |
| 9464 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 9465 | void ConstantPoolArray::ConstantPoolIterateBody(ObjectVisitor* v) { |
| 9466 | int first_ptr_offset = OffsetOfElementAt(first_ptr_index()); |
| 9467 | int last_ptr_offset = |
| 9468 | OffsetOfElementAt(first_ptr_index() + count_of_ptr_entries()); |
| 9469 | v->VisitPointers( |
| 9470 | HeapObject::RawField(this, first_ptr_offset), |
| 9471 | HeapObject::RawField(this, last_ptr_offset)); |
| 9472 | } |
| 9473 | |
| 9474 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 9475 | void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) { |
| 9476 | // Iterate over all fields in the body but take care in dealing with |
| 9477 | // the code entry. |
| 9478 | IteratePointers(v, kPropertiesOffset, kCodeEntryOffset); |
| 9479 | v->VisitCodeEntry(this->address() + kCodeEntryOffset); |
| 9480 | IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size); |
| 9481 | } |
| 9482 | |
| 9483 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9484 | void JSFunction::MarkForLazyRecompilation() { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9485 | ASSERT(is_compiled() || GetIsolate()->DebuggerHasBreakPoints()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9486 | ASSERT(!IsOptimized()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 9487 | ASSERT(shared()->allows_lazy_compilation() || |
| 9488 | code()->optimizable()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 9489 | ASSERT(!shared()->is_generator()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9490 | set_code_no_write_barrier( |
| 9491 | GetIsolate()->builtins()->builtin(Builtins::kLazyRecompile)); |
| 9492 | // 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] | 9493 | } |
| 9494 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9495 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9496 | void JSFunction::MarkForConcurrentRecompilation() { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9497 | ASSERT(is_compiled() || GetIsolate()->DebuggerHasBreakPoints()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9498 | ASSERT(!IsOptimized()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 9499 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 9500 | ASSERT(!shared()->is_generator()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9501 | ASSERT(FLAG_concurrent_recompilation); |
| 9502 | if (FLAG_trace_concurrent_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9503 | PrintF(" ** Marking "); |
| 9504 | PrintName(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9505 | PrintF(" for concurrent recompilation.\n"); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9506 | } |
| 9507 | set_code_no_write_barrier( |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9508 | GetIsolate()->builtins()->builtin(Builtins::kConcurrentRecompile)); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9509 | // 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] | 9510 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9511 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9512 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9513 | void JSFunction::MarkInRecompileQueue() { |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9514 | // We can only arrive here via the concurrent-recompilation builtin. If |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9515 | // break points were set, the code would point to the lazy-compile builtin. |
| 9516 | ASSERT(!GetIsolate()->DebuggerHasBreakPoints()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9517 | ASSERT(IsMarkedForConcurrentRecompilation() && !IsOptimized()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9518 | ASSERT(shared()->allows_lazy_compilation() || code()->optimizable()); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9519 | ASSERT(FLAG_concurrent_recompilation); |
| 9520 | if (FLAG_trace_concurrent_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9521 | PrintF(" ** Queueing "); |
| 9522 | PrintName(); |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 9523 | PrintF(" for concurrent recompilation.\n"); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 9524 | } |
| 9525 | set_code_no_write_barrier( |
| 9526 | GetIsolate()->builtins()->builtin(Builtins::kInRecompileQueue)); |
| 9527 | // No write barrier required, since the builtin is part of the root set. |
| 9528 | } |
| 9529 | |
| 9530 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9531 | static bool CompileLazyHelper(CompilationInfo* info, |
| 9532 | ClearExceptionFlag flag) { |
| 9533 | // Compile the source information to a code object. |
| 9534 | ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled()); |
| 9535 | ASSERT(!info->isolate()->has_pending_exception()); |
| 9536 | bool result = Compiler::CompileLazy(info); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9537 | ASSERT(result != info->isolate()->has_pending_exception()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9538 | if (!result && flag == CLEAR_EXCEPTION) { |
| 9539 | info->isolate()->clear_pending_exception(); |
| 9540 | } |
| 9541 | return result; |
| 9542 | } |
| 9543 | |
| 9544 | |
| 9545 | bool SharedFunctionInfo::CompileLazy(Handle<SharedFunctionInfo> shared, |
| 9546 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9547 | ASSERT(shared->allows_lazy_compilation_without_context()); |
| 9548 | CompilationInfoWithZone info(shared); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9549 | return CompileLazyHelper(&info, flag); |
| 9550 | } |
| 9551 | |
| 9552 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9553 | void SharedFunctionInfo::AddToOptimizedCodeMap( |
| 9554 | Handle<SharedFunctionInfo> shared, |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9555 | Handle<Context> native_context, |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9556 | Handle<Code> code, |
| 9557 | Handle<FixedArray> literals) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9558 | CALL_HEAP_FUNCTION_VOID( |
| 9559 | shared->GetIsolate(), |
| 9560 | shared->AddToOptimizedCodeMap(*native_context, *code, *literals)); |
| 9561 | } |
| 9562 | |
| 9563 | |
| 9564 | MaybeObject* SharedFunctionInfo::AddToOptimizedCodeMap(Context* native_context, |
| 9565 | Code* code, |
| 9566 | FixedArray* literals) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9567 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9568 | ASSERT(native_context->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9569 | STATIC_ASSERT(kEntryLength == 3); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9570 | Heap* heap = GetHeap(); |
| 9571 | FixedArray* new_code_map; |
| 9572 | Object* value = optimized_code_map(); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9573 | if (value->IsSmi()) { |
| 9574 | // No optimized code map. |
| 9575 | ASSERT_EQ(0, Smi::cast(value)->value()); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 9576 | // Create 3 entries per context {context, code, literals}. |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9577 | MaybeObject* maybe = heap->AllocateFixedArray(kInitialLength); |
| 9578 | if (!maybe->To(&new_code_map)) return maybe; |
| 9579 | new_code_map->set(kEntriesStart + 0, native_context); |
| 9580 | new_code_map->set(kEntriesStart + 1, code); |
| 9581 | new_code_map->set(kEntriesStart + 2, literals); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9582 | } else { |
| 9583 | // Copy old map and append one new entry. |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9584 | FixedArray* old_code_map = FixedArray::cast(value); |
| 9585 | ASSERT_EQ(-1, SearchOptimizedCodeMap(native_context)); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9586 | int old_length = old_code_map->length(); |
| 9587 | int new_length = old_length + kEntryLength; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9588 | MaybeObject* maybe = old_code_map->CopySize(new_length); |
| 9589 | if (!maybe->To(&new_code_map)) return maybe; |
| 9590 | new_code_map->set(old_length + 0, native_context); |
| 9591 | new_code_map->set(old_length + 1, code); |
| 9592 | new_code_map->set(old_length + 2, literals); |
| 9593 | // Zap the old map for the sake of the heap verifier. |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 9594 | if (Heap::ShouldZapGarbage()) { |
| 9595 | Object** data = old_code_map->data_start(); |
| 9596 | MemsetPointer(data, heap->the_hole_value(), old_length); |
| 9597 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9598 | } |
| 9599 | #ifdef DEBUG |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9600 | for (int i = kEntriesStart; i < new_code_map->length(); i += kEntryLength) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9601 | ASSERT(new_code_map->get(i)->IsNativeContext()); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9602 | ASSERT(new_code_map->get(i + 1)->IsCode()); |
| 9603 | ASSERT(Code::cast(new_code_map->get(i + 1))->kind() == |
| 9604 | Code::OPTIMIZED_FUNCTION); |
| 9605 | ASSERT(new_code_map->get(i + 2)->IsFixedArray()); |
| 9606 | } |
| 9607 | #endif |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9608 | set_optimized_code_map(new_code_map); |
| 9609 | return new_code_map; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9610 | } |
| 9611 | |
| 9612 | |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9613 | void SharedFunctionInfo::InstallFromOptimizedCodeMap(JSFunction* function, |
| 9614 | int index) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9615 | ASSERT(index > kEntriesStart); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9616 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9617 | if (!bound()) { |
| 9618 | FixedArray* cached_literals = FixedArray::cast(code_map->get(index + 1)); |
| 9619 | ASSERT(cached_literals != NULL); |
| 9620 | function->set_literals(cached_literals); |
| 9621 | } |
| 9622 | Code* code = Code::cast(code_map->get(index)); |
| 9623 | ASSERT(code != NULL); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9624 | ASSERT(function->context()->native_context() == code_map->get(index - 1)); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 9625 | function->ReplaceCode(code); |
| 9626 | } |
| 9627 | |
| 9628 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9629 | void SharedFunctionInfo::ClearOptimizedCodeMap() { |
| 9630 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9631 | |
| 9632 | // If the next map link slot is already used then the function was |
| 9633 | // enqueued with code flushing and we remove it now. |
| 9634 | if (!code_map->get(kNextMapIndex)->IsUndefined()) { |
| 9635 | CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher(); |
| 9636 | flusher->EvictOptimizedCodeMap(this); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9637 | } |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9638 | |
| 9639 | ASSERT(code_map->get(kNextMapIndex)->IsUndefined()); |
| 9640 | set_optimized_code_map(Smi::FromInt(0)); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
| 9643 | |
| 9644 | void SharedFunctionInfo::EvictFromOptimizedCodeMap(Code* optimized_code, |
| 9645 | const char* reason) { |
| 9646 | if (optimized_code_map()->IsSmi()) return; |
| 9647 | |
| 9648 | int i; |
| 9649 | bool removed_entry = false; |
| 9650 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9651 | for (i = kEntriesStart; i < code_map->length(); i += kEntryLength) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9652 | ASSERT(code_map->get(i)->IsNativeContext()); |
| 9653 | if (Code::cast(code_map->get(i + 1)) == optimized_code) { |
| 9654 | if (FLAG_trace_opt) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9655 | PrintF("[evicting entry from optimizing code map (%s) for ", reason); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9656 | ShortPrint(); |
| 9657 | PrintF("]\n"); |
| 9658 | } |
| 9659 | removed_entry = true; |
| 9660 | break; |
| 9661 | } |
| 9662 | } |
| 9663 | while (i < (code_map->length() - kEntryLength)) { |
| 9664 | code_map->set(i, code_map->get(i + kEntryLength)); |
| 9665 | code_map->set(i + 1, code_map->get(i + 1 + kEntryLength)); |
| 9666 | code_map->set(i + 2, code_map->get(i + 2 + kEntryLength)); |
| 9667 | i += kEntryLength; |
| 9668 | } |
| 9669 | if (removed_entry) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9670 | // Always trim even when array is cleared because of heap verifier. |
| 9671 | RightTrimFixedArray<FROM_MUTATOR>(GetHeap(), code_map, kEntryLength); |
| 9672 | if (code_map->length() == kEntriesStart) { |
| 9673 | ClearOptimizedCodeMap(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9674 | } |
| 9675 | } |
| 9676 | } |
| 9677 | |
| 9678 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 9679 | void SharedFunctionInfo::TrimOptimizedCodeMap(int shrink_by) { |
| 9680 | FixedArray* code_map = FixedArray::cast(optimized_code_map()); |
| 9681 | ASSERT(shrink_by % kEntryLength == 0); |
| 9682 | ASSERT(shrink_by <= code_map->length() - kEntriesStart); |
| 9683 | // Always trim even when array is cleared because of heap verifier. |
| 9684 | RightTrimFixedArray<FROM_GC>(GetHeap(), code_map, shrink_by); |
| 9685 | if (code_map->length() == kEntriesStart) { |
| 9686 | ClearOptimizedCodeMap(); |
| 9687 | } |
| 9688 | } |
| 9689 | |
| 9690 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9691 | bool JSFunction::CompileLazy(Handle<JSFunction> function, |
| 9692 | ClearExceptionFlag flag) { |
| 9693 | bool result = true; |
| 9694 | if (function->shared()->is_compiled()) { |
| 9695 | function->ReplaceCode(function->shared()->code()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9696 | } else { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9697 | ASSERT(function->shared()->allows_lazy_compilation()); |
| 9698 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9699 | result = CompileLazyHelper(&info, flag); |
| 9700 | ASSERT(!result || function->is_compiled()); |
| 9701 | } |
| 9702 | return result; |
| 9703 | } |
| 9704 | |
| 9705 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9706 | Handle<Code> JSFunction::CompileOsr(Handle<JSFunction> function, |
| 9707 | BailoutId osr_ast_id, |
| 9708 | ClearExceptionFlag flag) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9709 | CompilationInfoWithZone info(function); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9710 | info.SetOptimizing(osr_ast_id); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9711 | if (CompileLazyHelper(&info, flag)) { |
| 9712 | // TODO(titzer): don't install the OSR code. |
| 9713 | // ASSERT(function->code() != *info.code()); |
| 9714 | return info.code(); |
| 9715 | } else { |
| 9716 | return Handle<Code>::null(); |
| 9717 | } |
| 9718 | } |
| 9719 | |
| 9720 | |
| 9721 | bool JSFunction::CompileOptimized(Handle<JSFunction> function, |
| 9722 | ClearExceptionFlag flag) { |
| 9723 | CompilationInfoWithZone info(function); |
| 9724 | info.SetOptimizing(BailoutId::None()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9725 | return CompileLazyHelper(&info, flag); |
| 9726 | } |
| 9727 | |
| 9728 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 9729 | bool JSFunction::EnsureCompiled(Handle<JSFunction> function, |
| 9730 | ClearExceptionFlag flag) { |
| 9731 | return function->is_compiled() || CompileLazy(function, flag); |
| 9732 | } |
| 9733 | |
| 9734 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 9735 | void JSObject::OptimizeAsPrototype(Handle<JSObject> object) { |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9736 | if (object->IsGlobalObject()) return; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9737 | |
| 9738 | // Make sure prototypes are fast objects and their maps have the bit set |
| 9739 | // so they remain fast. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 9740 | if (!object->HasFastProperties()) { |
| 9741 | TransformToFastProperties(object, 0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9742 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9743 | } |
| 9744 | |
| 9745 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9746 | static MUST_USE_RESULT MaybeObject* CacheInitialJSArrayMaps( |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9747 | Context* native_context, Map* initial_map) { |
| 9748 | // Replace all of the cached initial array maps in the native context with |
| 9749 | // the appropriate transitioned elements kind maps. |
| 9750 | Heap* heap = native_context->GetHeap(); |
| 9751 | MaybeObject* maybe_maps = |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9752 | heap->AllocateFixedArrayWithHoles(kElementsKindCount, TENURED); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9753 | FixedArray* maps; |
| 9754 | if (!maybe_maps->To(&maps)) return maybe_maps; |
| 9755 | |
| 9756 | Map* current_map = initial_map; |
| 9757 | ElementsKind kind = current_map->elements_kind(); |
| 9758 | ASSERT(kind == GetInitialFastElementsKind()); |
| 9759 | maps->set(kind, current_map); |
| 9760 | for (int i = GetSequenceIndexFromFastElementsKind(kind) + 1; |
| 9761 | i < kFastElementsKindCount; ++i) { |
| 9762 | Map* new_map; |
| 9763 | ElementsKind next_kind = GetFastElementsKindFromSequenceIndex(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 9764 | if (current_map->HasElementsTransition()) { |
| 9765 | new_map = current_map->elements_transition_map(); |
| 9766 | ASSERT(new_map->elements_kind() == next_kind); |
| 9767 | } else { |
| 9768 | MaybeObject* maybe_new_map = |
| 9769 | current_map->CopyAsElementsKind(next_kind, INSERT_TRANSITION); |
| 9770 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 9771 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9772 | maps->set(next_kind, new_map); |
| 9773 | current_map = new_map; |
| 9774 | } |
| 9775 | native_context->set_js_array_maps(maps); |
| 9776 | return initial_map; |
| 9777 | } |
| 9778 | |
| 9779 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 9780 | Handle<Object> CacheInitialJSArrayMaps(Handle<Context> native_context, |
| 9781 | Handle<Map> initial_map) { |
| 9782 | CALL_HEAP_FUNCTION(native_context->GetIsolate(), |
| 9783 | CacheInitialJSArrayMaps(*native_context, *initial_map), |
| 9784 | Object); |
| 9785 | } |
| 9786 | |
| 9787 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9788 | void JSFunction::SetInstancePrototype(Handle<JSFunction> function, |
| 9789 | Handle<Object> value) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9790 | ASSERT(value->IsJSReceiver()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9791 | |
ulan@chromium.org | f6a0c41 | 2012-06-15 12:31:06 +0000 | [diff] [blame] | 9792 | // First some logic for the map of the prototype to make sure it is in fast |
| 9793 | // mode. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9794 | if (value->IsJSObject()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9795 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
| 9798 | // Now some logic for the maps of the objects that are created by using this |
| 9799 | // function as a constructor. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9800 | if (function->has_initial_map()) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9801 | // If the function has allocated the initial map replace it with a |
| 9802 | // copy containing the new prototype. Also complete any in-object |
| 9803 | // slack tracking that is in progress at this point because it is |
| 9804 | // still tracking the old copy. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9805 | if (function->shared()->IsInobjectSlackTrackingInProgress()) { |
| 9806 | function->shared()->CompleteInobjectSlackTracking(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9807 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9808 | Handle<Map> new_map = Map::Copy(handle(function->initial_map())); |
| 9809 | new_map->set_prototype(*value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9810 | |
| 9811 | // If the function is used as the global Array function, cache the |
| 9812 | // initial map (and transitioned versions) in the native context. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9813 | Context* native_context = function->context()->native_context(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9814 | Object* array_function = native_context->get(Context::ARRAY_FUNCTION_INDEX); |
| 9815 | if (array_function->IsJSFunction() && |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9816 | *function == JSFunction::cast(array_function)) { |
| 9817 | CacheInitialJSArrayMaps(handle(native_context), new_map); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 9818 | } |
| 9819 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9820 | function->set_initial_map(*new_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9821 | } else { |
| 9822 | // Put the value in the initial map field until an initial map is |
| 9823 | // needed. At that point, a new initial map is created and the |
| 9824 | // prototype is put into the initial map where it belongs. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9825 | function->set_prototype_or_initial_map(*value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9826 | } |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9827 | function->GetHeap()->ClearInstanceofCache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9828 | } |
| 9829 | |
| 9830 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9831 | void JSFunction::SetPrototype(Handle<JSFunction> function, |
| 9832 | Handle<Object> value) { |
| 9833 | ASSERT(function->should_have_prototype()); |
| 9834 | Handle<Object> construct_prototype = value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9835 | |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9836 | // 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] | 9837 | // constructor field so it can be accessed. Also, set the prototype |
| 9838 | // used for constructing objects to the original object prototype. |
| 9839 | // See ECMA-262 13.2.2. |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 9840 | if (!value->IsJSReceiver()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9841 | // 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] | 9842 | // Remove map transitions because they point to maps with a |
| 9843 | // different prototype. |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9844 | Handle<Map> new_map = Map::Copy(handle(function->map())); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 9845 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9846 | function->set_map(*new_map); |
| 9847 | new_map->set_constructor(*value); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9848 | new_map->set_non_instance_prototype(true); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9849 | Isolate* isolate = new_map->GetIsolate(); |
| 9850 | construct_prototype = handle( |
| 9851 | isolate->context()->native_context()->initial_object_prototype(), |
| 9852 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9853 | } else { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9854 | function->map()->set_non_instance_prototype(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9855 | } |
| 9856 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 9857 | return SetInstancePrototype(function, construct_prototype); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9858 | } |
| 9859 | |
| 9860 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9861 | void JSFunction::RemovePrototype() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9862 | Context* native_context = context()->native_context(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9863 | Map* no_prototype_map = shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9864 | ? native_context->function_without_prototype_map() |
| 9865 | : native_context->strict_mode_function_without_prototype_map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9866 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9867 | if (map() == no_prototype_map) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9868 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9869 | ASSERT(map() == (shared()->is_classic_mode() |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9870 | ? native_context->function_map() |
| 9871 | : native_context->strict_mode_function_map())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9872 | |
| 9873 | set_map(no_prototype_map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9874 | 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] | 9875 | } |
| 9876 | |
| 9877 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 9878 | void JSFunction::EnsureHasInitialMap(Handle<JSFunction> function) { |
| 9879 | if (function->has_initial_map()) return; |
| 9880 | Isolate* isolate = function->GetIsolate(); |
| 9881 | |
| 9882 | // First create a new map with the size and number of in-object properties |
| 9883 | // suggested by the function. |
| 9884 | InstanceType instance_type; |
| 9885 | int instance_size; |
| 9886 | int in_object_properties; |
| 9887 | if (function->shared()->is_generator()) { |
| 9888 | instance_type = JS_GENERATOR_OBJECT_TYPE; |
| 9889 | instance_size = JSGeneratorObject::kSize; |
| 9890 | in_object_properties = 0; |
| 9891 | } else { |
| 9892 | instance_type = JS_OBJECT_TYPE; |
| 9893 | instance_size = function->shared()->CalculateInstanceSize(); |
| 9894 | in_object_properties = function->shared()->CalculateInObjectProperties(); |
| 9895 | } |
| 9896 | Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size); |
| 9897 | |
| 9898 | // Fetch or allocate prototype. |
| 9899 | Handle<Object> prototype; |
| 9900 | if (function->has_instance_prototype()) { |
| 9901 | prototype = handle(function->instance_prototype(), isolate); |
| 9902 | } else { |
| 9903 | prototype = isolate->factory()->NewFunctionPrototype(function); |
| 9904 | } |
| 9905 | map->set_inobject_properties(in_object_properties); |
| 9906 | map->set_unused_property_fields(in_object_properties); |
| 9907 | map->set_prototype(*prototype); |
| 9908 | ASSERT(map->has_fast_object_elements()); |
| 9909 | |
| 9910 | if (!function->shared()->is_generator()) { |
| 9911 | function->shared()->StartInobjectSlackTracking(*map); |
| 9912 | } |
| 9913 | |
| 9914 | // Finally link initial map and constructor function. |
| 9915 | function->set_initial_map(*map); |
| 9916 | map->set_constructor(*function); |
| 9917 | } |
| 9918 | |
| 9919 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 9920 | void JSFunction::SetInstanceClassName(String* name) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9921 | shared()->set_instance_class_name(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9922 | } |
| 9923 | |
| 9924 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9925 | void JSFunction::PrintName(FILE* out) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9926 | SmartArrayPointer<char> name = shared()->DebugName()->ToCString(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 9927 | PrintF(out, "%s", *name); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9928 | } |
| 9929 | |
| 9930 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 9931 | Context* JSFunction::NativeContextFromLiterals(FixedArray* literals) { |
| 9932 | return Context::cast(literals->get(JSFunction::kLiteralNativeContextIndex)); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9933 | } |
| 9934 | |
| 9935 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9936 | // The filter is a pattern that matches function names in this way: |
| 9937 | // "*" all; the default |
| 9938 | // "-" all but the top-level function |
| 9939 | // "-name" all but the function "name" |
| 9940 | // "" only the top-level function |
| 9941 | // "name" only the function "name" |
| 9942 | // "name*" only functions starting with "name" |
| 9943 | bool JSFunction::PassesFilter(const char* raw_filter) { |
| 9944 | if (*raw_filter == '*') return true; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9945 | String* name = shared()->DebugName(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9946 | Vector<const char> filter = CStrVector(raw_filter); |
| 9947 | if (filter.length() == 0) return name->length() == 0; |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 9948 | if (filter[0] == '-') { |
| 9949 | if (filter.length() == 1) { |
| 9950 | return (name->length() != 0); |
| 9951 | } else if (!name->IsUtf8EqualTo(filter.SubVector(1, filter.length()))) { |
| 9952 | return true; |
| 9953 | } |
| 9954 | } else if (name->IsUtf8EqualTo(filter)) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9955 | return true; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9956 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 9957 | if (filter[filter.length() - 1] == '*' && |
| 9958 | name->IsUtf8EqualTo(filter.SubVector(0, filter.length() - 1), true)) { |
| 9959 | return true; |
| 9960 | } |
| 9961 | return false; |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9962 | } |
| 9963 | |
| 9964 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9965 | MaybeObject* Oddball::Initialize(Heap* heap, |
| 9966 | const char* to_string, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9967 | Object* to_number, |
| 9968 | byte kind) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9969 | String* internalized_to_string; |
| 9970 | { MaybeObject* maybe_string = |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 9971 | heap->InternalizeUtf8String( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9972 | CStrVector(to_string)); |
| 9973 | if (!maybe_string->To(&internalized_to_string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9974 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 9975 | set_to_string(internalized_to_string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9976 | set_to_number(to_number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9977 | set_kind(kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9978 | return this; |
| 9979 | } |
| 9980 | |
| 9981 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 9982 | String* SharedFunctionInfo::DebugName() { |
| 9983 | Object* n = name(); |
| 9984 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 9985 | return String::cast(n); |
| 9986 | } |
| 9987 | |
| 9988 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9989 | bool SharedFunctionInfo::HasSourceCode() { |
| 9990 | return !script()->IsUndefined() && |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 9991 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9992 | } |
| 9993 | |
| 9994 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 9995 | Handle<Object> SharedFunctionInfo::GetSourceCode() { |
| 9996 | if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value(); |
| 9997 | Handle<String> source(String::cast(Script::cast(script())->source())); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 9998 | return GetIsolate()->factory()->NewSubString( |
| 9999 | source, start_position(), end_position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10000 | } |
| 10001 | |
| 10002 | |
machenbach@chromium.org | 0cc0950 | 2013-11-13 12:20:55 +0000 | [diff] [blame] | 10003 | bool SharedFunctionInfo::IsInlineable() { |
| 10004 | // Check that the function has a script associated with it. |
| 10005 | if (!script()->IsScript()) return false; |
| 10006 | if (optimization_disabled()) return false; |
| 10007 | // If we never ran this (unlikely) then lets try to optimize it. |
| 10008 | if (code()->kind() != Code::FUNCTION) return true; |
| 10009 | return code()->optimizable(); |
| 10010 | } |
| 10011 | |
| 10012 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10013 | int SharedFunctionInfo::SourceSize() { |
| 10014 | return end_position() - start_position(); |
| 10015 | } |
| 10016 | |
| 10017 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 10018 | int SharedFunctionInfo::CalculateInstanceSize() { |
| 10019 | int instance_size = |
| 10020 | JSObject::kHeaderSize + |
| 10021 | expected_nof_properties() * kPointerSize; |
| 10022 | if (instance_size > JSObject::kMaxInstanceSize) { |
| 10023 | instance_size = JSObject::kMaxInstanceSize; |
| 10024 | } |
| 10025 | return instance_size; |
| 10026 | } |
| 10027 | |
| 10028 | |
| 10029 | int SharedFunctionInfo::CalculateInObjectProperties() { |
| 10030 | return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize; |
| 10031 | } |
| 10032 | |
| 10033 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10034 | // Support function for printing the source code to a StringStream |
| 10035 | // without any allocation in the heap. |
| 10036 | void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator, |
| 10037 | int max_length) { |
| 10038 | // For some native functions there is no source. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10039 | if (!HasSourceCode()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10040 | accumulator->Add("<No Source>"); |
| 10041 | return; |
| 10042 | } |
| 10043 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10044 | // Get the source for the script which this function came from. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10045 | // Don't use String::cast because we don't want more assertion errors while |
| 10046 | // we are already creating a stack dump. |
| 10047 | String* script_source = |
| 10048 | reinterpret_cast<String*>(Script::cast(script())->source()); |
| 10049 | |
| 10050 | if (!script_source->LooksValid()) { |
| 10051 | accumulator->Add("<Invalid Source>"); |
| 10052 | return; |
| 10053 | } |
| 10054 | |
| 10055 | if (!is_toplevel()) { |
| 10056 | accumulator->Add("function "); |
| 10057 | Object* name = this->name(); |
| 10058 | if (name->IsString() && String::cast(name)->length() > 0) { |
| 10059 | accumulator->PrintName(name); |
| 10060 | } |
| 10061 | } |
| 10062 | |
| 10063 | int len = end_position() - start_position(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10064 | if (len <= max_length || max_length < 0) { |
| 10065 | accumulator->Put(script_source, start_position(), end_position()); |
| 10066 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10067 | accumulator->Put(script_source, |
| 10068 | start_position(), |
| 10069 | start_position() + max_length); |
| 10070 | accumulator->Add("...\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10071 | } |
| 10072 | } |
| 10073 | |
| 10074 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10075 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 10076 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 10077 | ByteArray* code_relocation = code->relocation_info(); |
| 10078 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 10079 | int length = code_relocation->length(); |
| 10080 | if (length != recompiled_relocation->length()) return false; |
| 10081 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 10082 | recompiled_relocation->GetDataStartAddress(), |
| 10083 | length); |
| 10084 | return compare == 0; |
| 10085 | } |
| 10086 | |
| 10087 | |
| 10088 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
| 10089 | ASSERT(!has_deoptimization_support()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10090 | DisallowHeapAllocation no_allocation; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10091 | Code* code = this->code(); |
| 10092 | if (IsCodeEquivalent(code, recompiled)) { |
| 10093 | // Copy the deoptimization data from the recompiled code. |
| 10094 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 10095 | code->set_has_deoptimization_support(true); |
| 10096 | } else { |
| 10097 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 10098 | // old code, we have to replace it. We should try to avoid this |
| 10099 | // altogether because it flushes valuable type feedback by |
| 10100 | // effectively resetting all IC state. |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 10101 | ReplaceCode(recompiled); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10102 | } |
| 10103 | ASSERT(has_deoptimization_support()); |
| 10104 | } |
| 10105 | |
| 10106 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 10107 | void SharedFunctionInfo::DisableOptimization(BailoutReason reason) { |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10108 | // Disable optimization for the shared function info and mark the |
| 10109 | // code as non-optimizable. The marker on the shared function info |
| 10110 | // is there because we flush non-optimized code thereby loosing the |
| 10111 | // non-optimizable information for the code. When the code is |
| 10112 | // regenerated and set on the shared function info it is marked as |
| 10113 | // non-optimizable if optimization is disabled for the shared |
| 10114 | // function info. |
| 10115 | set_optimization_disabled(true); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 10116 | set_bailout_reason(reason); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10117 | // Code should be the lazy compilation stub or else unoptimized. If the |
| 10118 | // latter, disable optimization for the code too. |
| 10119 | ASSERT(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN); |
| 10120 | if (code()->kind() == Code::FUNCTION) { |
| 10121 | code()->set_optimizable(false); |
| 10122 | } |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 10123 | PROFILE(GetIsolate(), |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 10124 | LogExistingFunction(Handle<SharedFunctionInfo>(this), |
| 10125 | Handle<Code>(code()))); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10126 | if (FLAG_trace_opt) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10127 | PrintF("[disabled optimization for "); |
| 10128 | ShortPrint(); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 10129 | PrintF(", reason: %s]\n", GetBailoutReason(reason)); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10130 | } |
| 10131 | } |
| 10132 | |
| 10133 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10134 | bool SharedFunctionInfo::VerifyBailoutId(BailoutId id) { |
| 10135 | ASSERT(!id.IsNone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10136 | Code* unoptimized = code(); |
| 10137 | DeoptimizationOutputData* data = |
| 10138 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 10139 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 10140 | USE(ignore); |
| 10141 | return true; // Return true if there was no ASSERT. |
| 10142 | } |
| 10143 | |
| 10144 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10145 | void SharedFunctionInfo::StartInobjectSlackTracking(Map* map) { |
| 10146 | ASSERT(!IsInobjectSlackTrackingInProgress()); |
| 10147 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10148 | if (!FLAG_clever_optimizations) return; |
| 10149 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10150 | // Only initiate the tracking the first time. |
| 10151 | if (live_objects_may_exist()) return; |
| 10152 | set_live_objects_may_exist(true); |
| 10153 | |
| 10154 | // No tracking during the snapshot construction phase. |
| 10155 | if (Serializer::enabled()) return; |
| 10156 | |
| 10157 | if (map->unused_property_fields() == 0) return; |
| 10158 | |
| 10159 | // Nonzero counter is a leftover from the previous attempt interrupted |
| 10160 | // by GC, keep it. |
| 10161 | if (construction_count() == 0) { |
| 10162 | set_construction_count(kGenerousAllocationCount); |
| 10163 | } |
| 10164 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10165 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10166 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10167 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10168 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10169 | } |
| 10170 | |
| 10171 | |
| 10172 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 10173 | void SharedFunctionInfo::DetachInitialMap() { |
| 10174 | Map* map = reinterpret_cast<Map*>(initial_map()); |
| 10175 | |
| 10176 | // Make the map remember to restore the link if it survives the GC. |
| 10177 | map->set_bit_field2( |
| 10178 | map->bit_field2() | (1 << Map::kAttachedToSharedFunctionInfo)); |
| 10179 | |
| 10180 | // Undo state changes made by StartInobjectTracking (except the |
| 10181 | // construction_count). This way if the initial map does not survive the GC |
| 10182 | // then StartInobjectTracking will be called again the next time the |
| 10183 | // constructor is called. The countdown will continue and (possibly after |
| 10184 | // several more GCs) CompleteInobjectSlackTracking will eventually be called. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10185 | Heap* heap = map->GetHeap(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 10186 | set_initial_map(heap->undefined_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 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 | *RawField(this, kConstructStubOffset)); |
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 | // It is safe to clear the flag: it will be set again if the map is live. |
| 10192 | set_live_objects_may_exist(false); |
| 10193 | } |
| 10194 | |
| 10195 | |
| 10196 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 10197 | void SharedFunctionInfo::AttachInitialMap(Map* map) { |
| 10198 | map->set_bit_field2( |
| 10199 | map->bit_field2() & ~(1 << Map::kAttachedToSharedFunctionInfo)); |
| 10200 | |
| 10201 | // Resume inobject slack tracking. |
| 10202 | set_initial_map(map); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10203 | Builtins* builtins = map->GetHeap()->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10204 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10205 | *RawField(this, kConstructStubOffset)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10206 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10207 | // The map survived the gc, so there may be objects referencing it. |
| 10208 | set_live_objects_may_exist(true); |
| 10209 | } |
| 10210 | |
| 10211 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10212 | void SharedFunctionInfo::ResetForNewContext(int new_ic_age) { |
| 10213 | code()->ClearInlineCaches(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10214 | set_ic_age(new_ic_age); |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 10215 | if (code()->kind() == Code::FUNCTION) { |
| 10216 | code()->set_profiler_ticks(0); |
| 10217 | if (optimization_disabled() && |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 10218 | opt_count() >= FLAG_max_opt_count) { |
ulan@chromium.org | 6b9c2a0 | 2012-04-02 14:52:24 +0000 | [diff] [blame] | 10219 | // Re-enable optimizations if they were disabled due to opt_count limit. |
| 10220 | set_optimization_disabled(false); |
| 10221 | code()->set_optimizable(true); |
| 10222 | } |
| 10223 | set_opt_count(0); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 10224 | set_deopt_count(0); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10225 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10226 | } |
| 10227 | |
| 10228 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10229 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 10230 | int slack = map->unused_property_fields(); |
| 10231 | if (*reinterpret_cast<int*>(data) > slack) { |
| 10232 | *reinterpret_cast<int*>(data) = slack; |
| 10233 | } |
| 10234 | } |
| 10235 | |
| 10236 | |
| 10237 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 10238 | int slack = *reinterpret_cast<int*>(data); |
| 10239 | map->set_inobject_properties(map->inobject_properties() - slack); |
| 10240 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 10241 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
| 10242 | |
| 10243 | // Visitor id might depend on the instance size, recalculate it. |
| 10244 | map->set_visitor_id(StaticVisitorBase::GetVisitorId(map)); |
| 10245 | } |
| 10246 | |
| 10247 | |
| 10248 | void SharedFunctionInfo::CompleteInobjectSlackTracking() { |
| 10249 | ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress()); |
| 10250 | Map* map = Map::cast(initial_map()); |
| 10251 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10252 | Heap* heap = map->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10253 | set_initial_map(heap->undefined_value()); |
| 10254 | Builtins* builtins = heap->isolate()->builtins(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10255 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10256 | construct_stub()); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 10257 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10258 | |
| 10259 | int slack = map->unused_property_fields(); |
| 10260 | map->TraverseTransitionTree(&GetMinInobjectSlack, &slack); |
| 10261 | if (slack != 0) { |
| 10262 | // Resize the initial map and all maps in its transition tree. |
| 10263 | map->TraverseTransitionTree(&ShrinkInstanceSize, &slack); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 10264 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10265 | // Give the correct expected_nof_properties to initial maps created later. |
| 10266 | ASSERT(expected_nof_properties() >= slack); |
| 10267 | set_expected_nof_properties(expected_nof_properties() - slack); |
| 10268 | } |
| 10269 | } |
| 10270 | |
| 10271 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10272 | int SharedFunctionInfo::SearchOptimizedCodeMap(Context* native_context) { |
| 10273 | ASSERT(native_context->IsNativeContext()); |
ulan@chromium.org | d9e468a | 2012-06-25 09:47:40 +0000 | [diff] [blame] | 10274 | if (!FLAG_cache_optimized_code) return -1; |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10275 | Object* value = optimized_code_map(); |
| 10276 | if (!value->IsSmi()) { |
| 10277 | FixedArray* optimized_code_map = FixedArray::cast(value); |
| 10278 | int length = optimized_code_map->length(); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 10279 | for (int i = kEntriesStart; i < length; i += kEntryLength) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10280 | if (optimized_code_map->get(i) == native_context) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10281 | return i + 1; |
| 10282 | } |
| 10283 | } |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10284 | if (FLAG_trace_opt) { |
| 10285 | PrintF("[didn't find optimized code in optimized code map for "); |
| 10286 | ShortPrint(); |
| 10287 | PrintF("]\n"); |
| 10288 | } |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10289 | } |
| 10290 | return -1; |
| 10291 | } |
| 10292 | |
| 10293 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 10294 | #define DECLARE_TAG(ignore1, name, ignore2) name, |
| 10295 | const char* const VisitorSynchronization::kTags[ |
| 10296 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 10297 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 10298 | }; |
| 10299 | #undef DECLARE_TAG |
| 10300 | |
| 10301 | |
| 10302 | #define DECLARE_TAG(ignore1, ignore2, name) name, |
| 10303 | const char* const VisitorSynchronization::kTagNames[ |
| 10304 | VisitorSynchronization::kNumberOfSyncTags] = { |
| 10305 | VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG) |
| 10306 | }; |
| 10307 | #undef DECLARE_TAG |
| 10308 | |
| 10309 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10310 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10311 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10312 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 10313 | Object* old_target = target; |
| 10314 | VisitPointer(&target); |
| 10315 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10316 | } |
| 10317 | |
| 10318 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10319 | void ObjectVisitor::VisitCodeAgeSequence(RelocInfo* rinfo) { |
| 10320 | ASSERT(RelocInfo::IsCodeAgeSequence(rinfo->rmode())); |
| 10321 | Object* stub = rinfo->code_age_stub(); |
| 10322 | if (stub) { |
| 10323 | VisitPointer(&stub); |
| 10324 | } |
| 10325 | } |
| 10326 | |
| 10327 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 10328 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 10329 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 10330 | Object* old_code = code; |
| 10331 | VisitPointer(&code); |
| 10332 | if (code != old_code) { |
| 10333 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 10334 | } |
| 10335 | } |
| 10336 | |
| 10337 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10338 | void ObjectVisitor::VisitCell(RelocInfo* rinfo) { |
| 10339 | ASSERT(rinfo->rmode() == RelocInfo::CELL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10340 | Object* cell = rinfo->target_cell(); |
| 10341 | Object* old_cell = cell; |
| 10342 | VisitPointer(&cell); |
| 10343 | if (cell != old_cell) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10344 | rinfo->set_target_cell(reinterpret_cast<Cell*>(cell)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10345 | } |
| 10346 | } |
| 10347 | |
| 10348 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10349 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 10350 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 10351 | rinfo->IsPatchedReturnSequence()) || |
| 10352 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 10353 | rinfo->IsPatchedDebugBreakSlotSequence())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10354 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 10355 | Object* old_target = target; |
| 10356 | VisitPointer(&target); |
| 10357 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10358 | } |
| 10359 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10360 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10361 | void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) { |
| 10362 | ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10363 | Object* p = rinfo->target_object(); |
| 10364 | VisitPointer(&p); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10365 | } |
| 10366 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10367 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 10368 | void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10369 | Address p = rinfo->target_reference(); |
| 10370 | VisitExternalReference(&p); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 10371 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 10372 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 10373 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10374 | void Code::InvalidateRelocation() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10375 | set_relocation_info(GetHeap()->empty_byte_array()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10376 | } |
| 10377 | |
| 10378 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10379 | void Code::Relocate(intptr_t delta) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10380 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
| 10381 | it.rinfo()->apply(delta); |
| 10382 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 10383 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10384 | } |
| 10385 | |
| 10386 | |
| 10387 | void Code::CopyFrom(const CodeDesc& desc) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10388 | ASSERT(Marking::Color(this) == Marking::WHITE_OBJECT); |
| 10389 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10390 | // copy code |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 10391 | CopyBytes(instruction_start(), desc.buffer, |
| 10392 | static_cast<size_t>(desc.instr_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10393 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10394 | // copy reloc info |
danno@chromium.org | c99cd48 | 2013-03-21 15:26:42 +0000 | [diff] [blame] | 10395 | CopyBytes(relocation_start(), |
| 10396 | desc.buffer + desc.buffer_size - desc.reloc_size, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 10397 | static_cast<size_t>(desc.reloc_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10398 | |
| 10399 | // unbox handles and relocate |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 10400 | intptr_t delta = instruction_start() - desc.buffer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10401 | int mode_mask = RelocInfo::kCodeTargetMask | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10402 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10403 | RelocInfo::ModeMask(RelocInfo::CELL) | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10404 | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10405 | RelocInfo::kApplyMask; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10406 | // Needed to find target_object and runtime_entry on X64 |
| 10407 | Assembler* origin = desc.origin; |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10408 | AllowDeferredHandleDereference embedding_raw_address; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10409 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10410 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 10411 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 10412 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10413 | it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10414 | } else if (mode == RelocInfo::CELL) { |
| 10415 | Handle<Cell> cell = it.rinfo()->target_cell_handle(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10416 | it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10417 | } else if (RelocInfo::IsCodeTarget(mode)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10418 | // rewrite code handles in inline cache targets to direct |
| 10419 | // pointers to the first instruction in the code object |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 10420 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10421 | Code* code = Code::cast(*p); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10422 | it.rinfo()->set_target_address(code->instruction_start(), |
| 10423 | SKIP_WRITE_BARRIER); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 10424 | } else if (RelocInfo::IsRuntimeEntry(mode)) { |
| 10425 | Address p = it.rinfo()->target_runtime_entry(origin); |
| 10426 | it.rinfo()->set_target_runtime_entry(p, SKIP_WRITE_BARRIER); |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10427 | } else if (mode == RelocInfo::CODE_AGE_SEQUENCE) { |
| 10428 | Handle<Object> p = it.rinfo()->code_age_stub_handle(origin); |
| 10429 | Code* code = Code::cast(*p); |
| 10430 | it.rinfo()->set_code_age_stub(code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10431 | } else { |
| 10432 | it.rinfo()->apply(delta); |
| 10433 | } |
| 10434 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 10435 | CPU::FlushICache(instruction_start(), instruction_size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10436 | } |
| 10437 | |
| 10438 | |
| 10439 | // Locate the source position which is closest to the address in the code. This |
| 10440 | // is using the source position information embedded in the relocation info. |
| 10441 | // The position returned is relative to the beginning of the script where the |
| 10442 | // source for this function is found. |
| 10443 | int Code::SourcePosition(Address pc) { |
| 10444 | int distance = kMaxInt; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10445 | int position = RelocInfo::kNoPosition; // Initially no position found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10446 | // Run through all the relocation info to find the best matching source |
| 10447 | // position. All the code needs to be considered as the sequence of the |
| 10448 | // instructions in the code does not necessarily follow the same order as the |
| 10449 | // source. |
| 10450 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 10451 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10452 | // Only look at positions after the current pc. |
| 10453 | if (it.rinfo()->pc() < pc) { |
| 10454 | // Get position and distance. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10455 | |
| 10456 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 10457 | int pos = static_cast<int>(it.rinfo()->data()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10458 | // If this position is closer than the current candidate or if it has the |
| 10459 | // same distance as the current candidate and the position is higher then |
| 10460 | // this position is the new candidate. |
| 10461 | if ((dist < distance) || |
| 10462 | (dist == distance && pos > position)) { |
| 10463 | position = pos; |
| 10464 | distance = dist; |
| 10465 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10466 | } |
| 10467 | it.next(); |
| 10468 | } |
| 10469 | return position; |
| 10470 | } |
| 10471 | |
| 10472 | |
| 10473 | // Same as Code::SourcePosition above except it only looks for statement |
| 10474 | // positions. |
| 10475 | int Code::SourceStatementPosition(Address pc) { |
| 10476 | // First find the position as close as possible using all position |
| 10477 | // information. |
| 10478 | int position = SourcePosition(pc); |
| 10479 | // Now find the closest statement position before the position. |
| 10480 | int statement_position = 0; |
| 10481 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 10482 | while (!it.done()) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10483 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 10484 | int p = static_cast<int>(it.rinfo()->data()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10485 | if (statement_position < p && p <= position) { |
| 10486 | statement_position = p; |
| 10487 | } |
| 10488 | } |
| 10489 | it.next(); |
| 10490 | } |
| 10491 | return statement_position; |
| 10492 | } |
| 10493 | |
| 10494 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10495 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10496 | SafepointTable table(this); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 10497 | return table.FindEntry(pc); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10498 | } |
| 10499 | |
| 10500 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10501 | Object* Code::FindNthObject(int n, Map* match_map) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10502 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10503 | DisallowHeapAllocation no_allocation; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10504 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10505 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10506 | RelocInfo* info = it.rinfo(); |
| 10507 | Object* object = info->target_object(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10508 | if (object->IsHeapObject()) { |
| 10509 | if (HeapObject::cast(object)->map() == match_map) { |
| 10510 | if (--n == 0) return object; |
| 10511 | } |
| 10512 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10513 | } |
| 10514 | return NULL; |
| 10515 | } |
| 10516 | |
| 10517 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10518 | Map* Code::FindFirstMap() { |
| 10519 | Object* result = FindNthObject(1, GetHeap()->meta_map()); |
| 10520 | return (result != NULL) ? Map::cast(result) : NULL; |
| 10521 | } |
| 10522 | |
| 10523 | |
| 10524 | void Code::ReplaceNthObject(int n, |
| 10525 | Map* match_map, |
| 10526 | Object* replace_with) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 10527 | ASSERT(is_inline_cache_stub() || is_handler()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10528 | DisallowHeapAllocation no_allocation; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10529 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10530 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10531 | RelocInfo* info = it.rinfo(); |
| 10532 | Object* object = info->target_object(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10533 | if (object->IsHeapObject()) { |
| 10534 | if (HeapObject::cast(object)->map() == match_map) { |
| 10535 | if (--n == 0) { |
| 10536 | info->set_target_object(replace_with); |
| 10537 | return; |
| 10538 | } |
| 10539 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 10540 | } |
| 10541 | } |
| 10542 | UNREACHABLE(); |
| 10543 | } |
| 10544 | |
| 10545 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10546 | void Code::FindAllMaps(MapHandleList* maps) { |
| 10547 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10548 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10549 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10550 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10551 | RelocInfo* info = it.rinfo(); |
| 10552 | Object* object = info->target_object(); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 10553 | if (object->IsMap()) maps->Add(handle(Map::cast(object))); |
| 10554 | } |
| 10555 | } |
| 10556 | |
| 10557 | |
| 10558 | void Code::FindAllTypes(TypeHandleList* types) { |
| 10559 | ASSERT(is_inline_cache_stub()); |
| 10560 | DisallowHeapAllocation no_allocation; |
| 10561 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10562 | Isolate* isolate = GetIsolate(); |
| 10563 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10564 | RelocInfo* info = it.rinfo(); |
| 10565 | Object* object = info->target_object(); |
| 10566 | if (object->IsMap()) { |
| 10567 | Handle<Map> map(Map::cast(object)); |
| 10568 | types->Add(handle(IC::MapToType(map), isolate)); |
| 10569 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10570 | } |
| 10571 | } |
| 10572 | |
| 10573 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10574 | void Code::ReplaceFirstMap(Map* replace_with) { |
| 10575 | ReplaceNthObject(1, GetHeap()->meta_map(), replace_with); |
| 10576 | } |
| 10577 | |
| 10578 | |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10579 | Code* Code::FindFirstHandler() { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10580 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10581 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10582 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 10583 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10584 | RelocInfo* info = it.rinfo(); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10585 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
| 10586 | if (code->kind() == Code::HANDLER) return code; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10587 | } |
| 10588 | return NULL; |
| 10589 | } |
| 10590 | |
| 10591 | |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10592 | bool Code::FindHandlers(CodeHandleList* code_list, int length) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10593 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10594 | DisallowHeapAllocation no_allocation; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10595 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET); |
| 10596 | int i = 0; |
| 10597 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10598 | if (i == length) return true; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10599 | RelocInfo* info = it.rinfo(); |
| 10600 | Code* code = Code::GetCodeFromTargetAddress(info->target_address()); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10601 | // IC stubs with handlers never contain non-handler code objects before |
| 10602 | // handler targets. |
| 10603 | if (code->kind() != Code::HANDLER) break; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10604 | code_list->Add(Handle<Code>(code)); |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10605 | i++; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10606 | } |
jkummerow@chromium.org | 32aa03c | 2013-10-01 08:21:50 +0000 | [diff] [blame] | 10607 | return i == length; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 10608 | } |
| 10609 | |
| 10610 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10611 | Name* Code::FindFirstName() { |
| 10612 | ASSERT(is_inline_cache_stub()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 10613 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10614 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 10615 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10616 | RelocInfo* info = it.rinfo(); |
| 10617 | Object* object = info->target_object(); |
| 10618 | if (object->IsName()) return Name::cast(object); |
| 10619 | } |
| 10620 | return NULL; |
| 10621 | } |
| 10622 | |
| 10623 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10624 | void Code::ReplaceNthCell(int n, Cell* replace_with) { |
| 10625 | ASSERT(is_inline_cache_stub()); |
| 10626 | DisallowHeapAllocation no_allocation; |
| 10627 | int mask = RelocInfo::ModeMask(RelocInfo::CELL); |
| 10628 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10629 | RelocInfo* info = it.rinfo(); |
| 10630 | if (--n == 0) { |
| 10631 | info->set_target_cell(replace_with); |
| 10632 | return; |
| 10633 | } |
| 10634 | } |
| 10635 | UNREACHABLE(); |
| 10636 | } |
| 10637 | |
| 10638 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10639 | void Code::ClearInlineCaches() { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 10640 | ClearInlineCaches(NULL); |
| 10641 | } |
| 10642 | |
| 10643 | |
| 10644 | void Code::ClearInlineCaches(Code::Kind kind) { |
| 10645 | ClearInlineCaches(&kind); |
| 10646 | } |
| 10647 | |
| 10648 | |
| 10649 | void Code::ClearInlineCaches(Code::Kind* kind) { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10650 | int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) | |
| 10651 | RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) | |
| 10652 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID) | |
| 10653 | RelocInfo::ModeMask(RelocInfo::CODE_TARGET_CONTEXT); |
| 10654 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10655 | RelocInfo* info = it.rinfo(); |
| 10656 | Code* target(Code::GetCodeFromTargetAddress(info->target_address())); |
| 10657 | if (target->is_inline_cache_stub()) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 10658 | if (kind == NULL || *kind == target->kind()) { |
| 10659 | IC::Clear(this->GetIsolate(), info->pc()); |
| 10660 | } |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 10661 | } |
| 10662 | } |
| 10663 | } |
| 10664 | |
| 10665 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10666 | void Code::ClearTypeFeedbackCells(Heap* heap) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 10667 | if (kind() != FUNCTION) return; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10668 | Object* raw_info = type_feedback_info(); |
| 10669 | if (raw_info->IsTypeFeedbackInfo()) { |
| 10670 | TypeFeedbackCells* type_feedback_cells = |
| 10671 | TypeFeedbackInfo::cast(raw_info)->type_feedback_cells(); |
| 10672 | for (int i = 0; i < type_feedback_cells->CellCount(); i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10673 | Cell* cell = type_feedback_cells->GetCell(i); |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 10674 | // Don't clear AllocationSites |
| 10675 | Object* value = cell->value(); |
| 10676 | if (value == NULL || !value->IsAllocationSite()) { |
| 10677 | cell->set_value(TypeFeedbackCells::RawUninitializedSentinel(heap)); |
| 10678 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 10679 | } |
| 10680 | } |
| 10681 | } |
| 10682 | |
| 10683 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10684 | BailoutId Code::TranslatePcOffsetToAstId(uint32_t pc_offset) { |
| 10685 | DisallowHeapAllocation no_gc; |
| 10686 | ASSERT(kind() == FUNCTION); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10687 | BackEdgeTable back_edges(this, &no_gc); |
| 10688 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 10689 | 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] | 10690 | } |
| 10691 | return BailoutId::None(); |
| 10692 | } |
| 10693 | |
| 10694 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10695 | void Code::MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate) { |
bmeurer@chromium.org | e94b5ff | 2013-10-25 09:22:31 +0000 | [diff] [blame] | 10696 | PatchPlatformCodeAge(isolate, sequence, kNoAgeCodeAge, NO_MARKING_PARITY); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10697 | } |
| 10698 | |
| 10699 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10700 | void Code::MarkCodeAsExecuted(byte* sequence, Isolate* isolate) { |
| 10701 | PatchPlatformCodeAge(isolate, sequence, kExecutedOnceCodeAge, |
| 10702 | NO_MARKING_PARITY); |
| 10703 | } |
| 10704 | |
| 10705 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10706 | static Code::Age EffectiveAge(Code::Age age) { |
| 10707 | if (age == Code::kNotExecutedCodeAge) { |
| 10708 | // Treat that's never been executed as old immediately. |
| 10709 | age = Code::kIsOldCodeAge; |
| 10710 | } else if (age == Code::kExecutedOnceCodeAge) { |
| 10711 | // Pre-age code that has only been executed once. |
| 10712 | age = Code::kPreAgedCodeAge; |
| 10713 | } |
| 10714 | return age; |
| 10715 | } |
| 10716 | |
| 10717 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10718 | void Code::MakeOlder(MarkingParity current_parity) { |
| 10719 | byte* sequence = FindCodeAgeSequence(); |
| 10720 | if (sequence != NULL) { |
| 10721 | Age age; |
| 10722 | MarkingParity code_parity; |
| 10723 | GetCodeAgeAndParity(sequence, &age, &code_parity); |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10724 | age = EffectiveAge(age); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10725 | if (age != kLastCodeAge && code_parity != current_parity) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10726 | PatchPlatformCodeAge(GetIsolate(), |
| 10727 | sequence, |
| 10728 | static_cast<Age>(age + 1), |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10729 | current_parity); |
| 10730 | } |
| 10731 | } |
| 10732 | } |
| 10733 | |
| 10734 | |
| 10735 | bool Code::IsOld() { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10736 | return GetAge() >= kIsOldCodeAge; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
| 10739 | |
| 10740 | byte* Code::FindCodeAgeSequence() { |
| 10741 | return FLAG_age_code && |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10742 | prologue_offset() != Code::kPrologueOffsetNotSet && |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10743 | (kind() == OPTIMIZED_FUNCTION || |
| 10744 | (kind() == FUNCTION && !has_debug_break_slots())) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 10745 | ? instruction_start() + prologue_offset() |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10746 | : NULL; |
| 10747 | } |
| 10748 | |
| 10749 | |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10750 | Code::Age Code::GetAge() { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10751 | return EffectiveAge(GetRawAge()); |
| 10752 | } |
| 10753 | |
| 10754 | |
| 10755 | Code::Age Code::GetRawAge() { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10756 | byte* sequence = FindCodeAgeSequence(); |
| 10757 | if (sequence == NULL) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10758 | return kNoAgeCodeAge; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 10759 | } |
| 10760 | Age age; |
| 10761 | MarkingParity parity; |
| 10762 | GetCodeAgeAndParity(sequence, &age, &parity); |
| 10763 | return age; |
| 10764 | } |
| 10765 | |
| 10766 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10767 | void Code::GetCodeAgeAndParity(Code* code, Age* age, |
| 10768 | MarkingParity* parity) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 10769 | Isolate* isolate = code->GetIsolate(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10770 | Builtins* builtins = isolate->builtins(); |
| 10771 | Code* stub = NULL; |
| 10772 | #define HANDLE_CODE_AGE(AGE) \ |
| 10773 | stub = *builtins->Make##AGE##CodeYoungAgainEvenMarking(); \ |
| 10774 | if (code == stub) { \ |
| 10775 | *age = k##AGE##CodeAge; \ |
| 10776 | *parity = EVEN_MARKING_PARITY; \ |
| 10777 | return; \ |
| 10778 | } \ |
| 10779 | stub = *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 10780 | if (code == stub) { \ |
| 10781 | *age = k##AGE##CodeAge; \ |
| 10782 | *parity = ODD_MARKING_PARITY; \ |
| 10783 | return; \ |
| 10784 | } |
| 10785 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10786 | #undef HANDLE_CODE_AGE |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10787 | stub = *builtins->MarkCodeAsExecutedOnce(); |
| 10788 | if (code == stub) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10789 | *age = kNotExecutedCodeAge; |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10790 | *parity = NO_MARKING_PARITY; |
| 10791 | return; |
| 10792 | } |
| 10793 | stub = *builtins->MarkCodeAsExecutedTwice(); |
| 10794 | if (code == stub) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 10795 | *age = kExecutedOnceCodeAge; |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10796 | *parity = NO_MARKING_PARITY; |
| 10797 | return; |
| 10798 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10799 | UNREACHABLE(); |
| 10800 | } |
| 10801 | |
| 10802 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10803 | Code* Code::GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity) { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10804 | Builtins* builtins = isolate->builtins(); |
| 10805 | switch (age) { |
| 10806 | #define HANDLE_CODE_AGE(AGE) \ |
| 10807 | case k##AGE##CodeAge: { \ |
| 10808 | Code* stub = parity == EVEN_MARKING_PARITY \ |
| 10809 | ? *builtins->Make##AGE##CodeYoungAgainEvenMarking() \ |
| 10810 | : *builtins->Make##AGE##CodeYoungAgainOddMarking(); \ |
| 10811 | return stub; \ |
| 10812 | } |
| 10813 | CODE_AGE_LIST(HANDLE_CODE_AGE) |
| 10814 | #undef HANDLE_CODE_AGE |
bmeurer@chromium.org | c9913f0 | 2013-10-24 06:31:36 +0000 | [diff] [blame] | 10815 | case kNotExecutedCodeAge: { |
| 10816 | ASSERT(parity == NO_MARKING_PARITY); |
| 10817 | return *builtins->MarkCodeAsExecutedOnce(); |
| 10818 | } |
| 10819 | case kExecutedOnceCodeAge: { |
| 10820 | ASSERT(parity == NO_MARKING_PARITY); |
| 10821 | return *builtins->MarkCodeAsExecutedTwice(); |
| 10822 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 10823 | default: |
| 10824 | UNREACHABLE(); |
| 10825 | break; |
| 10826 | } |
| 10827 | return NULL; |
| 10828 | } |
| 10829 | |
| 10830 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 10831 | void Code::PrintDeoptLocation(FILE* out, int bailout_id) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10832 | const char* last_comment = NULL; |
| 10833 | int mask = RelocInfo::ModeMask(RelocInfo::COMMENT) |
| 10834 | | RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY); |
| 10835 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 10836 | RelocInfo* info = it.rinfo(); |
| 10837 | if (info->rmode() == RelocInfo::COMMENT) { |
| 10838 | last_comment = reinterpret_cast<const char*>(info->data()); |
danno@chromium.org | aefd607 | 2013-05-14 14:11:47 +0000 | [diff] [blame] | 10839 | } else if (last_comment != NULL) { |
| 10840 | if ((bailout_id == Deoptimizer::GetDeoptimizationId( |
| 10841 | GetIsolate(), info->target_address(), Deoptimizer::EAGER)) || |
| 10842 | (bailout_id == Deoptimizer::GetDeoptimizationId( |
| 10843 | GetIsolate(), info->target_address(), Deoptimizer::SOFT))) { |
| 10844 | CHECK(RelocInfo::IsRuntimeEntry(info->rmode())); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 10845 | PrintF(out, " %s\n", last_comment); |
danno@chromium.org | aefd607 | 2013-05-14 14:11:47 +0000 | [diff] [blame] | 10846 | return; |
| 10847 | } |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10848 | } |
| 10849 | } |
| 10850 | } |
| 10851 | |
| 10852 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 10853 | bool Code::CanDeoptAt(Address pc) { |
| 10854 | DeoptimizationInputData* deopt_data = |
| 10855 | DeoptimizationInputData::cast(deoptimization_data()); |
| 10856 | Address code_start_address = instruction_start(); |
| 10857 | for (int i = 0; i < deopt_data->DeoptCount(); i++) { |
| 10858 | if (deopt_data->Pc(i)->value() == -1) continue; |
| 10859 | Address address = code_start_address + deopt_data->Pc(i)->value(); |
| 10860 | if (address == pc) return true; |
| 10861 | } |
| 10862 | return false; |
| 10863 | } |
| 10864 | |
| 10865 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10866 | // Identify kind of code. |
| 10867 | const char* Code::Kind2String(Kind kind) { |
| 10868 | switch (kind) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10869 | #define CASE(name) case name: return #name; |
| 10870 | CODE_KIND_LIST(CASE) |
| 10871 | #undef CASE |
| 10872 | case NUMBER_OF_KINDS: break; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10873 | } |
| 10874 | UNREACHABLE(); |
| 10875 | return NULL; |
| 10876 | } |
| 10877 | |
| 10878 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10879 | #ifdef ENABLE_DISASSEMBLER |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10880 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10881 | void DeoptimizationInputData::DeoptimizationInputDataPrint(FILE* out) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10882 | disasm::NameConverter converter; |
| 10883 | int deopt_count = DeoptCount(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 10884 | PrintF(out, "Deoptimization Input Data (deopt points = %d)\n", deopt_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10885 | if (0 == deopt_count) return; |
| 10886 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10887 | 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] | 10888 | FLAG_print_code_verbose ? "commands" : ""); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10889 | for (int i = 0; i < deopt_count; i++) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10890 | PrintF(out, "%6d %6d %6d %6d", |
| 10891 | i, |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 10892 | AstId(i).ToInt(), |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 10893 | ArgumentsStackHeight(i)->value(), |
| 10894 | Pc(i)->value()); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10895 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10896 | if (!FLAG_print_code_verbose) { |
| 10897 | PrintF(out, "\n"); |
| 10898 | continue; |
| 10899 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10900 | // Print details of the frame translation. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10901 | int translation_index = TranslationIndex(i)->value(); |
| 10902 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 10903 | Translation::Opcode opcode = |
| 10904 | static_cast<Translation::Opcode>(iterator.Next()); |
| 10905 | ASSERT(Translation::BEGIN == opcode); |
| 10906 | int frame_count = iterator.Next(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10907 | int jsframe_count = iterator.Next(); |
| 10908 | PrintF(out, " %s {frame count=%d, js frame count=%d}\n", |
| 10909 | Translation::StringFor(opcode), |
| 10910 | frame_count, |
| 10911 | jsframe_count); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10912 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10913 | while (iterator.HasNext() && |
| 10914 | Translation::BEGIN != |
| 10915 | (opcode = static_cast<Translation::Opcode>(iterator.Next()))) { |
| 10916 | PrintF(out, "%24s %s ", "", Translation::StringFor(opcode)); |
| 10917 | |
| 10918 | switch (opcode) { |
| 10919 | case Translation::BEGIN: |
| 10920 | UNREACHABLE(); |
| 10921 | break; |
| 10922 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10923 | case Translation::JS_FRAME: { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10924 | int ast_id = iterator.Next(); |
| 10925 | int function_id = iterator.Next(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10926 | unsigned height = iterator.Next(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 10927 | PrintF(out, "{ast_id=%d, function=", ast_id); |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 10928 | if (function_id != Translation::kSelfLiteralId) { |
| 10929 | Object* function = LiteralArray()->get(function_id); |
| 10930 | JSFunction::cast(function)->PrintName(out); |
| 10931 | } else { |
| 10932 | PrintF(out, "<self>"); |
| 10933 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10934 | PrintF(out, ", height=%u}", height); |
| 10935 | break; |
| 10936 | } |
| 10937 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 10938 | case Translation::COMPILED_STUB_FRAME: { |
| 10939 | Code::Kind stub_kind = static_cast<Code::Kind>(iterator.Next()); |
| 10940 | PrintF(out, "{kind=%d}", stub_kind); |
| 10941 | break; |
| 10942 | } |
| 10943 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10944 | case Translation::ARGUMENTS_ADAPTOR_FRAME: |
| 10945 | case Translation::CONSTRUCT_STUB_FRAME: { |
| 10946 | int function_id = iterator.Next(); |
| 10947 | JSFunction* function = |
| 10948 | JSFunction::cast(LiteralArray()->get(function_id)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10949 | unsigned height = iterator.Next(); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 10950 | PrintF(out, "{function="); |
| 10951 | function->PrintName(out); |
| 10952 | PrintF(out, ", height=%u}", height); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10953 | break; |
| 10954 | } |
| 10955 | |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 10956 | case Translation::GETTER_STUB_FRAME: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10957 | case Translation::SETTER_STUB_FRAME: { |
| 10958 | int function_id = iterator.Next(); |
| 10959 | JSFunction* function = |
| 10960 | JSFunction::cast(LiteralArray()->get(function_id)); |
| 10961 | PrintF(out, "{function="); |
| 10962 | function->PrintName(out); |
| 10963 | PrintF(out, "}"); |
| 10964 | break; |
| 10965 | } |
| 10966 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10967 | case Translation::REGISTER: { |
| 10968 | int reg_code = iterator.Next(); |
| 10969 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10970 | break; |
| 10971 | } |
| 10972 | |
| 10973 | case Translation::INT32_REGISTER: { |
| 10974 | int reg_code = iterator.Next(); |
| 10975 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 10976 | break; |
| 10977 | } |
| 10978 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10979 | case Translation::UINT32_REGISTER: { |
| 10980 | int reg_code = iterator.Next(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 10981 | PrintF(out, "{input=%s (unsigned)}", |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 10982 | converter.NameOfCPURegister(reg_code)); |
| 10983 | break; |
| 10984 | } |
| 10985 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10986 | case Translation::DOUBLE_REGISTER: { |
| 10987 | int reg_code = iterator.Next(); |
| 10988 | PrintF(out, "{input=%s}", |
| 10989 | DoubleRegister::AllocationIndexToString(reg_code)); |
| 10990 | break; |
| 10991 | } |
| 10992 | |
| 10993 | case Translation::STACK_SLOT: { |
| 10994 | int input_slot_index = iterator.Next(); |
| 10995 | PrintF(out, "{input=%d}", input_slot_index); |
| 10996 | break; |
| 10997 | } |
| 10998 | |
| 10999 | case Translation::INT32_STACK_SLOT: { |
| 11000 | int input_slot_index = iterator.Next(); |
| 11001 | PrintF(out, "{input=%d}", input_slot_index); |
| 11002 | break; |
| 11003 | } |
| 11004 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 11005 | case Translation::UINT32_STACK_SLOT: { |
| 11006 | int input_slot_index = iterator.Next(); |
| 11007 | PrintF(out, "{input=%d (unsigned)}", input_slot_index); |
| 11008 | break; |
| 11009 | } |
| 11010 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11011 | case Translation::DOUBLE_STACK_SLOT: { |
| 11012 | int input_slot_index = iterator.Next(); |
| 11013 | PrintF(out, "{input=%d}", input_slot_index); |
| 11014 | break; |
| 11015 | } |
| 11016 | |
| 11017 | case Translation::LITERAL: { |
| 11018 | unsigned literal_index = iterator.Next(); |
| 11019 | PrintF(out, "{literal_id=%u}", literal_index); |
| 11020 | break; |
| 11021 | } |
| 11022 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11023 | case Translation::DUPLICATED_OBJECT: { |
| 11024 | int object_index = iterator.Next(); |
| 11025 | PrintF(out, "{object_index=%d}", object_index); |
| 11026 | break; |
| 11027 | } |
| 11028 | |
| 11029 | case Translation::ARGUMENTS_OBJECT: |
| 11030 | case Translation::CAPTURED_OBJECT: { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 11031 | int args_length = iterator.Next(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 11032 | PrintF(out, "{length=%d}", args_length); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11033 | break; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 11034 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11035 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11036 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11037 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11038 | } |
| 11039 | } |
| 11040 | |
| 11041 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11042 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint(FILE* out) { |
| 11043 | PrintF(out, "Deoptimization Output Data (deopt points = %d)\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11044 | this->DeoptPoints()); |
| 11045 | if (this->DeoptPoints() == 0) return; |
| 11046 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11047 | PrintF(out, "%6s %8s %s\n", "ast id", "pc", "state"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11048 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 11049 | int pc_and_state = this->PcAndState(i)->value(); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11050 | PrintF(out, "%6d %8d %s\n", |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 11051 | this->AstId(i).ToInt(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11052 | FullCodeGenerator::PcField::decode(pc_and_state), |
| 11053 | FullCodeGenerator::State2String( |
| 11054 | FullCodeGenerator::StateField::decode(pc_and_state))); |
| 11055 | } |
| 11056 | } |
| 11057 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11058 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 11059 | const char* Code::ICState2String(InlineCacheState state) { |
| 11060 | switch (state) { |
| 11061 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 11062 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 11063 | case MONOMORPHIC: return "MONOMORPHIC"; |
| 11064 | case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE"; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 11065 | case POLYMORPHIC: return "POLYMORPHIC"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 11066 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11067 | case GENERIC: return "GENERIC"; |
yangguo@chromium.org | 9768bf1 | 2013-01-11 14:51:07 +0000 | [diff] [blame] | 11068 | case DEBUG_STUB: return "DEBUG_STUB"; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 11069 | } |
| 11070 | UNREACHABLE(); |
| 11071 | return NULL; |
| 11072 | } |
| 11073 | |
| 11074 | |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 11075 | const char* Code::StubType2String(StubType type) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11076 | switch (type) { |
| 11077 | case NORMAL: return "NORMAL"; |
| 11078 | case FIELD: return "FIELD"; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 11079 | case CONSTANT: return "CONSTANT"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11080 | case CALLBACKS: return "CALLBACKS"; |
| 11081 | case INTERCEPTOR: return "INTERCEPTOR"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11082 | case TRANSITION: return "TRANSITION"; |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 11083 | case NONEXISTENT: return "NONEXISTENT"; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11084 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 11085 | UNREACHABLE(); // keep the compiler happy |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11086 | return NULL; |
| 11087 | } |
| 11088 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11089 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11090 | void Code::PrintExtraICState(FILE* out, Kind kind, ExtraICState extra) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11091 | PrintF(out, "extra_ic_state = "); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11092 | const char* name = NULL; |
| 11093 | switch (kind) { |
| 11094 | case CALL_IC: |
| 11095 | if (extra == STRING_INDEX_OUT_OF_BOUNDS) { |
| 11096 | name = "STRING_INDEX_OUT_OF_BOUNDS"; |
| 11097 | } |
| 11098 | break; |
| 11099 | case STORE_IC: |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11100 | case KEYED_STORE_IC: |
| 11101 | if (extra == kStrictMode) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11102 | name = "STRICT"; |
| 11103 | } |
| 11104 | break; |
| 11105 | default: |
| 11106 | break; |
| 11107 | } |
| 11108 | if (name != NULL) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11109 | PrintF(out, "%s\n", name); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11110 | } else { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11111 | PrintF(out, "%d\n", extra); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11112 | } |
| 11113 | } |
| 11114 | |
| 11115 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11116 | void Code::Disassemble(const char* name, FILE* out) { |
| 11117 | PrintF(out, "kind = %s\n", Kind2String(kind())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11118 | if (is_inline_cache_stub()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11119 | PrintF(out, "ic_state = %s\n", ICState2String(ic_state())); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11120 | PrintExtraICState(out, kind(), needs_extended_extra_ic_state(kind()) ? |
| 11121 | extended_extra_ic_state() : extra_ic_state()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11122 | if (ic_state() == MONOMORPHIC) { |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 11123 | PrintF(out, "type = %s\n", StubType2String(type())); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11124 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11125 | if (is_call_stub() || is_keyed_call_stub()) { |
| 11126 | PrintF(out, "argc = %d\n", arguments_count()); |
| 11127 | } |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 11128 | if (is_compare_ic_stub()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11129 | ASSERT(major_key() == CodeStub::CompareIC); |
| 11130 | CompareIC::State left_state, right_state, handler_state; |
| 11131 | Token::Value op; |
| 11132 | ICCompareStub::DecodeMinorKey(stub_info(), &left_state, &right_state, |
| 11133 | &handler_state, &op); |
| 11134 | PrintF(out, "compare_state = %s*%s -> %s\n", |
| 11135 | CompareIC::GetStateName(left_state), |
| 11136 | CompareIC::GetStateName(right_state), |
| 11137 | CompareIC::GetStateName(handler_state)); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11138 | PrintF(out, "compare_operation = %s\n", Token::Name(op)); |
| 11139 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11140 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11141 | if ((name != NULL) && (name[0] != '\0')) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11142 | PrintF(out, "name = %s\n", name); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11143 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11144 | if (kind() == OPTIMIZED_FUNCTION) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11145 | PrintF(out, "stack_slots = %d\n", stack_slots()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11146 | } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11147 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11148 | PrintF(out, "Instructions (size = %d)\n", instruction_size()); |
| 11149 | Disassembler::Decode(out, this); |
| 11150 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11151 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11152 | if (kind() == FUNCTION) { |
| 11153 | DeoptimizationOutputData* data = |
| 11154 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11155 | data->DeoptimizationOutputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11156 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 11157 | DeoptimizationInputData* data = |
| 11158 | DeoptimizationInputData::cast(this->deoptimization_data()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11159 | data->DeoptimizationInputDataPrint(out); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11160 | } |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11161 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11162 | |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 11163 | if (is_crankshafted()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11164 | SafepointTable table(this); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11165 | PrintF(out, "Safepoints (size = %u)\n", table.size()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11166 | for (unsigned i = 0; i < table.length(); i++) { |
| 11167 | unsigned pc_offset = table.GetPcOffset(i); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11168 | PrintF(out, "%p %4d ", (instruction_start() + pc_offset), pc_offset); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11169 | table.PrintEntry(i, out); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11170 | PrintF(out, " (sp -> fp)"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11171 | SafepointEntry entry = table.GetEntry(i); |
| 11172 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
| 11173 | PrintF(out, " %6d", entry.deoptimization_index()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11174 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11175 | PrintF(out, " <none>"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11176 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11177 | if (entry.argument_count() > 0) { |
| 11178 | PrintF(out, " argc: %d", entry.argument_count()); |
| 11179 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11180 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11181 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11182 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11183 | } else if (kind() == FUNCTION) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 11184 | unsigned offset = back_edge_table_offset(); |
| 11185 | // 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] | 11186 | // (due to alignment) the end of the instruction stream. |
| 11187 | if (static_cast<int>(offset) < instruction_size()) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11188 | DisallowHeapAllocation no_gc; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11189 | BackEdgeTable back_edges(this, &no_gc); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11190 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11191 | PrintF(out, "Back edges (size = %u)\n", back_edges.length()); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 11192 | PrintF(out, "ast_id pc_offset loop_depth\n"); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11193 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11194 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 11195 | PrintF(out, "%6d %9u %10u\n", back_edges.ast_id(i).ToInt(), |
| 11196 | back_edges.pc_offset(i), |
| 11197 | back_edges.loop_depth(i)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11198 | } |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11199 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11200 | PrintF(out, "\n"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11201 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 11202 | #ifdef OBJECT_PRINT |
ulan@chromium.org | ea52b5f | 2012-07-30 13:05:33 +0000 | [diff] [blame] | 11203 | if (!type_feedback_info()->IsUndefined()) { |
| 11204 | TypeFeedbackInfo::cast(type_feedback_info())->TypeFeedbackInfoPrint(out); |
| 11205 | PrintF(out, "\n"); |
| 11206 | } |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 11207 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11208 | } |
| 11209 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11210 | PrintF(out, "RelocInfo (size = %d)\n", relocation_size()); |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 11211 | for (RelocIterator it(this); !it.done(); it.next()) { |
| 11212 | it.rinfo()->Print(GetIsolate(), out); |
| 11213 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 11214 | PrintF(out, "\n"); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11215 | } |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 11216 | #endif // ENABLE_DISASSEMBLER |
| 11217 | |
| 11218 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11219 | Handle<FixedArray> JSObject::SetFastElementsCapacityAndLength( |
| 11220 | Handle<JSObject> object, |
| 11221 | int capacity, |
| 11222 | int length, |
| 11223 | SetFastElementsCapacitySmiMode smi_mode) { |
| 11224 | CALL_HEAP_FUNCTION( |
| 11225 | object->GetIsolate(), |
| 11226 | object->SetFastElementsCapacityAndLength(capacity, length, smi_mode), |
| 11227 | FixedArray); |
| 11228 | } |
| 11229 | |
| 11230 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11231 | MaybeObject* JSObject::SetFastElementsCapacityAndLength( |
| 11232 | int capacity, |
| 11233 | int length, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11234 | SetFastElementsCapacitySmiMode smi_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11235 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11236 | // 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] | 11237 | ASSERT(!HasExternalArrayElements()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11238 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11239 | // Allocate a new fast elements backing store. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11240 | FixedArray* new_elements; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11241 | MaybeObject* maybe = heap->AllocateUninitializedFixedArray(capacity); |
| 11242 | if (!maybe->To(&new_elements)) return maybe; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11243 | |
jkummerow@chromium.org | f3eae90 | 2012-05-24 16:42:53 +0000 | [diff] [blame] | 11244 | ElementsKind elements_kind = GetElementsKind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11245 | ElementsKind new_elements_kind; |
| 11246 | // The resized array has FAST_*_SMI_ELEMENTS if the capacity mode forces it, |
| 11247 | // or if it's allowed and the old elements array contained only SMIs. |
| 11248 | bool has_fast_smi_elements = |
| 11249 | (smi_mode == kForceSmiElements) || |
| 11250 | ((smi_mode == kAllowSmiElements) && HasFastSmiElements()); |
| 11251 | if (has_fast_smi_elements) { |
| 11252 | if (IsHoleyElementsKind(elements_kind)) { |
| 11253 | new_elements_kind = FAST_HOLEY_SMI_ELEMENTS; |
| 11254 | } else { |
| 11255 | new_elements_kind = FAST_SMI_ELEMENTS; |
| 11256 | } |
| 11257 | } else { |
| 11258 | if (IsHoleyElementsKind(elements_kind)) { |
| 11259 | new_elements_kind = FAST_HOLEY_ELEMENTS; |
| 11260 | } else { |
| 11261 | new_elements_kind = FAST_ELEMENTS; |
| 11262 | } |
| 11263 | } |
| 11264 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11265 | ElementsAccessor* accessor = ElementsAccessor::ForKind(new_elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11266 | MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11267 | accessor->CopyElements(this, new_elements, elements_kind); |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 11268 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11269 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11270 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11271 | Map* new_map = map(); |
| 11272 | if (new_elements_kind != elements_kind) { |
| 11273 | MaybeObject* maybe = |
| 11274 | GetElementsTransitionMap(GetIsolate(), new_elements_kind); |
| 11275 | if (!maybe->To(&new_map)) return maybe; |
| 11276 | } |
| 11277 | ValidateElements(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11278 | set_map_and_elements(new_map, new_elements); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11279 | |
| 11280 | // Transition through the allocation site as well if present. |
| 11281 | maybe_obj = UpdateAllocationSite(new_elements_kind); |
| 11282 | if (maybe_obj->IsFailure()) return maybe_obj; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11283 | } else { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11284 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 11285 | parameter_map->set(1, new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11286 | } |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11287 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11288 | if (FLAG_trace_elements_transitions) { |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11289 | PrintElementsTransition(stdout, elements_kind, old_elements, |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 11290 | GetElementsKind(), new_elements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11291 | } |
| 11292 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 11293 | if (IsJSArray()) { |
| 11294 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 11295 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11296 | return new_elements; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11297 | } |
| 11298 | |
| 11299 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 11300 | bool Code::IsWeakEmbeddedObject(Kind kind, Object* object) { |
| 11301 | if (kind != Code::OPTIMIZED_FUNCTION) return false; |
| 11302 | |
| 11303 | if (object->IsMap()) { |
| 11304 | return Map::cast(object)->CanTransition() && |
| 11305 | FLAG_collect_maps && |
| 11306 | FLAG_weak_embedded_maps_in_optimized_code; |
| 11307 | } |
| 11308 | |
| 11309 | if (object->IsJSObject()) { |
| 11310 | return FLAG_weak_embedded_objects_in_optimized_code; |
| 11311 | } |
| 11312 | |
| 11313 | return false; |
| 11314 | } |
| 11315 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11316 | |
| 11317 | void JSObject::SetFastDoubleElementsCapacityAndLength(Handle<JSObject> object, |
| 11318 | int capacity, |
| 11319 | int length) { |
| 11320 | CALL_HEAP_FUNCTION_VOID( |
| 11321 | object->GetIsolate(), |
| 11322 | object->SetFastDoubleElementsCapacityAndLength(capacity, length)); |
| 11323 | } |
| 11324 | |
| 11325 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11326 | MaybeObject* JSObject::SetFastDoubleElementsCapacityAndLength( |
| 11327 | int capacity, |
| 11328 | int length) { |
| 11329 | Heap* heap = GetHeap(); |
| 11330 | // We should never end in here with a pixel or external array. |
| 11331 | ASSERT(!HasExternalArrayElements()); |
| 11332 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11333 | FixedArrayBase* elems; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11334 | { MaybeObject* maybe_obj = |
| 11335 | heap->AllocateUninitializedFixedDoubleArray(capacity); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11336 | if (!maybe_obj->To(&elems)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11337 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11338 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11339 | ElementsKind elements_kind = GetElementsKind(); |
| 11340 | ElementsKind new_elements_kind = elements_kind; |
| 11341 | if (IsHoleyElementsKind(elements_kind)) { |
| 11342 | new_elements_kind = FAST_HOLEY_DOUBLE_ELEMENTS; |
| 11343 | } else { |
| 11344 | new_elements_kind = FAST_DOUBLE_ELEMENTS; |
| 11345 | } |
| 11346 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11347 | Map* new_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11348 | { MaybeObject* maybe_obj = |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11349 | GetElementsTransitionMap(heap->isolate(), new_elements_kind); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 11350 | if (!maybe_obj->To(&new_map)) return maybe_obj; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11351 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11352 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11353 | FixedArrayBase* old_elements = elements(); |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11354 | ElementsAccessor* accessor = ElementsAccessor::ForKind(FAST_DOUBLE_ELEMENTS); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11355 | { MaybeObject* maybe_obj = |
yangguo@chromium.org | 4cd70b4 | 2013-01-04 08:57:54 +0000 | [diff] [blame] | 11356 | accessor->CopyElements(this, elems, elements_kind); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 11357 | if (maybe_obj->IsFailure()) return maybe_obj; |
| 11358 | } |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11359 | if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11360 | ValidateElements(); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 11361 | set_map_and_elements(new_map, elems); |
| 11362 | } else { |
| 11363 | FixedArray* parameter_map = FixedArray::cast(old_elements); |
| 11364 | parameter_map->set(1, elems); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11365 | } |
| 11366 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11367 | if (FLAG_trace_elements_transitions) { |
| 11368 | PrintElementsTransition(stdout, elements_kind, old_elements, |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 11369 | GetElementsKind(), elems); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11370 | } |
| 11371 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11372 | if (IsJSArray()) { |
| 11373 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 11374 | } |
| 11375 | |
| 11376 | return this; |
| 11377 | } |
| 11378 | |
| 11379 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11380 | MaybeObject* JSArray::Initialize(int capacity, int length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11381 | ASSERT(capacity >= 0); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 11382 | return GetHeap()->AllocateJSArrayStorage(this, length, capacity, |
| 11383 | INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11384 | } |
| 11385 | |
| 11386 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11387 | void JSArray::Expand(int required_size) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11388 | GetIsolate()->factory()->SetElementsCapacityAndLength( |
| 11389 | Handle<JSArray>(this), required_size, required_size); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 11390 | } |
| 11391 | |
| 11392 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11393 | // Returns false if the passed-in index is marked non-configurable, |
| 11394 | // which will cause the ES5 truncation operation to halt, and thus |
| 11395 | // no further old values need be collected. |
| 11396 | static bool GetOldValue(Isolate* isolate, |
| 11397 | Handle<JSObject> object, |
| 11398 | uint32_t index, |
| 11399 | List<Handle<Object> >* old_values, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11400 | List<uint32_t>* indices) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11401 | PropertyAttributes attributes = object->GetLocalElementAttribute(index); |
| 11402 | ASSERT(attributes != ABSENT); |
| 11403 | if (attributes == DONT_DELETE) return false; |
| 11404 | old_values->Add(object->GetLocalElementAccessorPair(index) == NULL |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11405 | ? Object::GetElement(isolate, object, index) |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11406 | : Handle<Object>::cast(isolate->factory()->the_hole_value())); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11407 | indices->Add(index); |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11408 | return true; |
| 11409 | } |
| 11410 | |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11411 | static void EnqueueSpliceRecord(Handle<JSArray> object, |
| 11412 | uint32_t index, |
| 11413 | Handle<JSArray> deleted, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11414 | uint32_t add_count) { |
| 11415 | Isolate* isolate = object->GetIsolate(); |
| 11416 | HandleScope scope(isolate); |
| 11417 | Handle<Object> index_object = isolate->factory()->NewNumberFromUint(index); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11418 | Handle<Object> add_count_object = |
| 11419 | isolate->factory()->NewNumberFromUint(add_count); |
| 11420 | |
| 11421 | Handle<Object> args[] = |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11422 | { object, index_object, deleted, add_count_object }; |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11423 | |
| 11424 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11425 | Execution::Call(isolate, |
| 11426 | Handle<JSFunction>(isolate->observers_enqueue_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11427 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11428 | &threw); |
| 11429 | ASSERT(!threw); |
| 11430 | } |
| 11431 | |
| 11432 | |
| 11433 | static void BeginPerformSplice(Handle<JSArray> object) { |
| 11434 | Isolate* isolate = object->GetIsolate(); |
| 11435 | HandleScope scope(isolate); |
| 11436 | Handle<Object> args[] = { object }; |
| 11437 | |
| 11438 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11439 | Execution::Call(isolate, |
| 11440 | Handle<JSFunction>(isolate->observers_begin_perform_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11441 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11442 | &threw); |
| 11443 | ASSERT(!threw); |
| 11444 | } |
| 11445 | |
| 11446 | |
| 11447 | static void EndPerformSplice(Handle<JSArray> object) { |
| 11448 | Isolate* isolate = object->GetIsolate(); |
| 11449 | HandleScope scope(isolate); |
| 11450 | Handle<Object> args[] = { object }; |
| 11451 | |
| 11452 | bool threw; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 11453 | Execution::Call(isolate, |
| 11454 | Handle<JSFunction>(isolate->observers_end_perform_splice()), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11455 | isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, |
| 11456 | &threw); |
| 11457 | ASSERT(!threw); |
| 11458 | } |
| 11459 | |
| 11460 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 11461 | MaybeObject* JSArray::SetElementsLength(Object* len) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11462 | // 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] | 11463 | ASSERT(AllowsSetElementsLength()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11464 | if (!(FLAG_harmony_observation && map()->is_observed())) |
| 11465 | return GetElementsAccessor()->SetLength(this, len); |
| 11466 | |
| 11467 | Isolate* isolate = GetIsolate(); |
| 11468 | HandleScope scope(isolate); |
| 11469 | Handle<JSArray> self(this); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11470 | List<uint32_t> indices; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11471 | List<Handle<Object> > old_values; |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11472 | Handle<Object> old_length_handle(self->length(), isolate); |
| 11473 | Handle<Object> new_length_handle(len, isolate); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11474 | uint32_t old_length = 0; |
| 11475 | CHECK(old_length_handle->ToArrayIndex(&old_length)); |
| 11476 | uint32_t new_length = 0; |
| 11477 | if (!new_length_handle->ToArrayIndex(&new_length)) |
| 11478 | return Failure::InternalError(); |
| 11479 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11480 | static const PropertyAttributes kNoAttrFilter = NONE; |
| 11481 | int num_elements = self->NumberOfLocalElements(kNoAttrFilter); |
| 11482 | if (num_elements > 0) { |
| 11483 | if (old_length == static_cast<uint32_t>(num_elements)) { |
| 11484 | // Simple case for arrays without holes. |
| 11485 | for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { |
| 11486 | if (!GetOldValue(isolate, self, i, &old_values, &indices)) break; |
| 11487 | } |
| 11488 | } else { |
| 11489 | // For sparse arrays, only iterate over existing elements. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11490 | // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over |
| 11491 | // the to-be-removed indices twice. |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 11492 | Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); |
| 11493 | self->GetLocalElementKeys(*keys, kNoAttrFilter); |
| 11494 | while (num_elements-- > 0) { |
| 11495 | uint32_t index = NumberToUint32(keys->get(num_elements)); |
| 11496 | if (index < new_length) break; |
| 11497 | if (!GetOldValue(isolate, self, index, &old_values, &indices)) break; |
| 11498 | } |
| 11499 | } |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11500 | } |
| 11501 | |
| 11502 | MaybeObject* result = |
| 11503 | self->GetElementsAccessor()->SetLength(*self, *new_length_handle); |
| 11504 | Handle<Object> hresult; |
| 11505 | if (!result->ToHandle(&hresult, isolate)) return result; |
| 11506 | |
| 11507 | CHECK(self->length()->ToArrayIndex(&new_length)); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11508 | if (old_length == new_length) return *hresult; |
| 11509 | |
| 11510 | BeginPerformSplice(self); |
| 11511 | |
| 11512 | for (int i = 0; i < indices.length(); ++i) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11513 | JSObject::EnqueueChangeRecord( |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11514 | self, "delete", isolate->factory()->Uint32ToString(indices[i]), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11515 | old_values[i]); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11516 | } |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11517 | JSObject::EnqueueChangeRecord( |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11518 | self, "update", isolate->factory()->length_string(), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11519 | old_length_handle); |
| 11520 | |
| 11521 | EndPerformSplice(self); |
| 11522 | |
| 11523 | uint32_t index = Min(old_length, new_length); |
| 11524 | uint32_t add_count = new_length > old_length ? new_length - old_length : 0; |
| 11525 | uint32_t delete_count = new_length < old_length ? old_length - new_length : 0; |
| 11526 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11527 | if (delete_count > 0) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11528 | for (int i = indices.length() - 1; i >= 0; i--) { |
| 11529 | JSObject::SetElement(deleted, indices[i] - index, old_values[i], NONE, |
| 11530 | kNonStrictMode); |
| 11531 | } |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11532 | |
| 11533 | SetProperty(deleted, isolate->factory()->length_string(), |
| 11534 | isolate->factory()->NewNumberFromUint(delete_count), |
| 11535 | NONE, kNonStrictMode); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11536 | } |
| 11537 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 11538 | EnqueueSpliceRecord(self, index, deleted, add_count); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 11539 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11540 | return *hresult; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11541 | } |
| 11542 | |
| 11543 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11544 | Handle<Map> Map::GetPrototypeTransition(Handle<Map> map, |
| 11545 | Handle<Object> prototype) { |
| 11546 | FixedArray* cache = map->GetPrototypeTransitions(); |
| 11547 | int number_of_transitions = map->NumberOfProtoTransitions(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11548 | const int proto_offset = |
| 11549 | kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset; |
| 11550 | const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset; |
| 11551 | const int step = kProtoTransitionElementsPerEntry; |
| 11552 | for (int i = 0; i < number_of_transitions; i++) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11553 | if (cache->get(proto_offset + i * step) == *prototype) { |
| 11554 | Object* result = cache->get(map_offset + i * step); |
| 11555 | return Handle<Map>(Map::cast(result)); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11556 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11557 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11558 | return Handle<Map>(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11559 | } |
| 11560 | |
| 11561 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11562 | Handle<Map> Map::PutPrototypeTransition(Handle<Map> map, |
| 11563 | Handle<Object> prototype, |
| 11564 | Handle<Map> target_map) { |
| 11565 | ASSERT(target_map->IsMap()); |
| 11566 | ASSERT(HeapObject::cast(*prototype)->map()->IsMap()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11567 | // Don't cache prototype transition if this map is shared. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11568 | if (map->is_shared() || !FLAG_cache_prototype_transitions) return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11569 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11570 | const int step = kProtoTransitionElementsPerEntry; |
| 11571 | const int header = kProtoTransitionHeaderSize; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11572 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11573 | Handle<FixedArray> cache(map->GetPrototypeTransitions()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11574 | int capacity = (cache->length() - header) / step; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11575 | int transitions = map->NumberOfProtoTransitions() + 1; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11576 | |
| 11577 | if (transitions > capacity) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11578 | if (capacity > kMaxCachedPrototypeTransitions) return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11579 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11580 | // Grow array by factor 2 over and above what we need. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11581 | Factory* factory = map->GetIsolate()->factory(); |
| 11582 | cache = factory->CopySizeFixedArray(cache, transitions * 2 * step + header); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11583 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11584 | CALL_AND_RETRY_OR_DIE(map->GetIsolate(), |
| 11585 | map->SetPrototypeTransitions(*cache), |
| 11586 | break, |
| 11587 | return Handle<Map>()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11588 | } |
| 11589 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11590 | // Reload number of transitions as GC might shrink them. |
| 11591 | int last = map->NumberOfProtoTransitions(); |
| 11592 | int entry = header + last * step; |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11593 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11594 | cache->set(entry + kProtoTransitionPrototypeOffset, *prototype); |
| 11595 | cache->set(entry + kProtoTransitionMapOffset, *target_map); |
| 11596 | map->SetNumberOfProtoTransitions(transitions); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11597 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11598 | return map; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11599 | } |
| 11600 | |
| 11601 | |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 11602 | void Map::ZapTransitions() { |
| 11603 | TransitionArray* transition_array = transitions(); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 11604 | // TODO(mstarzinger): Temporarily use a slower version instead of the faster |
| 11605 | // MemsetPointer to investigate a crasher. Switch back to MemsetPointer. |
| 11606 | Object** data = transition_array->data_start(); |
| 11607 | Object* the_hole = GetHeap()->the_hole_value(); |
| 11608 | int length = transition_array->length(); |
| 11609 | for (int i = 0; i < length; i++) { |
| 11610 | data[i] = the_hole; |
| 11611 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 11612 | } |
| 11613 | |
| 11614 | |
| 11615 | void Map::ZapPrototypeTransitions() { |
| 11616 | FixedArray* proto_transitions = GetPrototypeTransitions(); |
| 11617 | MemsetPointer(proto_transitions->data_start(), |
| 11618 | GetHeap()->the_hole_value(), |
| 11619 | proto_transitions->length()); |
| 11620 | } |
| 11621 | |
| 11622 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11623 | void Map::AddDependentCompilationInfo(DependentCode::DependencyGroup group, |
| 11624 | CompilationInfo* info) { |
| 11625 | Handle<DependentCode> dep(dependent_code()); |
| 11626 | Handle<DependentCode> codes = |
| 11627 | DependentCode::Insert(dep, group, info->object_wrapper()); |
| 11628 | if (*codes != dependent_code()) set_dependent_code(*codes); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11629 | info->dependencies(group)->Add(Handle<HeapObject>(this), info->zone()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11630 | } |
| 11631 | |
| 11632 | |
| 11633 | void Map::AddDependentCode(DependentCode::DependencyGroup group, |
| 11634 | Handle<Code> code) { |
| 11635 | Handle<DependentCode> codes = DependentCode::Insert( |
| 11636 | Handle<DependentCode>(dependent_code()), group, code); |
| 11637 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 11638 | } |
| 11639 | |
| 11640 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11641 | DependentCode::GroupStartIndexes::GroupStartIndexes(DependentCode* entries) { |
| 11642 | Recompute(entries); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 11643 | } |
| 11644 | |
| 11645 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11646 | void DependentCode::GroupStartIndexes::Recompute(DependentCode* entries) { |
| 11647 | start_indexes_[0] = 0; |
| 11648 | for (int g = 1; g <= kGroupCount; g++) { |
| 11649 | int count = entries->number_of_entries(static_cast<DependencyGroup>(g - 1)); |
| 11650 | start_indexes_[g] = start_indexes_[g - 1] + count; |
| 11651 | } |
| 11652 | } |
| 11653 | |
| 11654 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11655 | DependentCode* DependentCode::ForObject(Handle<HeapObject> object, |
| 11656 | DependencyGroup group) { |
| 11657 | AllowDeferredHandleDereference dependencies_are_safe; |
| 11658 | if (group == DependentCode::kPropertyCellChangedGroup) { |
| 11659 | return Handle<PropertyCell>::cast(object)->dependent_code(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11660 | } else if (group == DependentCode::kAllocationSiteTenuringChangedGroup || |
| 11661 | group == DependentCode::kAllocationSiteTransitionChangedGroup) { |
| 11662 | return Handle<AllocationSite>::cast(object)->dependent_code(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11663 | } |
| 11664 | return Handle<Map>::cast(object)->dependent_code(); |
| 11665 | } |
| 11666 | |
| 11667 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11668 | Handle<DependentCode> DependentCode::Insert(Handle<DependentCode> entries, |
| 11669 | DependencyGroup group, |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11670 | Handle<Object> object) { |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11671 | GroupStartIndexes starts(*entries); |
| 11672 | int start = starts.at(group); |
| 11673 | int end = starts.at(group + 1); |
| 11674 | int number_of_entries = starts.number_of_entries(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11675 | // Check for existing entry to avoid duplicates. |
| 11676 | for (int i = start; i < end; i++) { |
| 11677 | if (entries->object_at(i) == *object) return entries; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11678 | } |
| 11679 | if (entries->length() < kCodesStartIndex + number_of_entries + 1) { |
| 11680 | Factory* factory = entries->GetIsolate()->factory(); |
| 11681 | int capacity = kCodesStartIndex + number_of_entries + 1; |
| 11682 | if (capacity > 5) capacity = capacity * 5 / 4; |
| 11683 | Handle<DependentCode> new_entries = Handle<DependentCode>::cast( |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 11684 | factory->CopySizeFixedArray(entries, capacity, TENURED)); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11685 | // The number of codes can change after GC. |
| 11686 | starts.Recompute(*entries); |
| 11687 | start = starts.at(group); |
| 11688 | end = starts.at(group + 1); |
| 11689 | number_of_entries = starts.number_of_entries(); |
| 11690 | for (int i = 0; i < number_of_entries; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11691 | entries->clear_at(i); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11692 | } |
| 11693 | // If the old fixed array was empty, we need to reset counters of the |
| 11694 | // new array. |
| 11695 | if (number_of_entries == 0) { |
| 11696 | for (int g = 0; g < kGroupCount; g++) { |
| 11697 | new_entries->set_number_of_entries(static_cast<DependencyGroup>(g), 0); |
| 11698 | } |
| 11699 | } |
| 11700 | entries = new_entries; |
| 11701 | } |
| 11702 | entries->ExtendGroup(group); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11703 | entries->set_object_at(end, *object); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11704 | entries->set_number_of_entries(group, end + 1 - start); |
| 11705 | return entries; |
| 11706 | } |
| 11707 | |
| 11708 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11709 | void DependentCode::UpdateToFinishedCode(DependencyGroup group, |
| 11710 | CompilationInfo* info, |
| 11711 | Code* code) { |
| 11712 | DisallowHeapAllocation no_gc; |
| 11713 | AllowDeferredHandleDereference get_object_wrapper; |
| 11714 | Foreign* info_wrapper = *info->object_wrapper(); |
| 11715 | GroupStartIndexes starts(this); |
| 11716 | int start = starts.at(group); |
| 11717 | int end = starts.at(group + 1); |
| 11718 | for (int i = start; i < end; i++) { |
| 11719 | if (object_at(i) == info_wrapper) { |
| 11720 | set_object_at(i, code); |
| 11721 | break; |
| 11722 | } |
| 11723 | } |
| 11724 | |
| 11725 | #ifdef DEBUG |
| 11726 | for (int i = start; i < end; i++) { |
| 11727 | ASSERT(is_code_at(i) || compilation_info_at(i) != info); |
| 11728 | } |
| 11729 | #endif |
| 11730 | } |
| 11731 | |
| 11732 | |
| 11733 | void DependentCode::RemoveCompilationInfo(DependentCode::DependencyGroup group, |
| 11734 | CompilationInfo* info) { |
| 11735 | DisallowHeapAllocation no_allocation; |
| 11736 | AllowDeferredHandleDereference get_object_wrapper; |
| 11737 | Foreign* info_wrapper = *info->object_wrapper(); |
| 11738 | GroupStartIndexes starts(this); |
| 11739 | int start = starts.at(group); |
| 11740 | int end = starts.at(group + 1); |
| 11741 | // Find compilation info wrapper. |
| 11742 | int info_pos = -1; |
| 11743 | for (int i = start; i < end; i++) { |
| 11744 | if (object_at(i) == info_wrapper) { |
| 11745 | info_pos = i; |
| 11746 | break; |
| 11747 | } |
| 11748 | } |
| 11749 | if (info_pos == -1) return; // Not found. |
| 11750 | int gap = info_pos; |
| 11751 | // Use the last of each group to fill the gap in the previous group. |
| 11752 | for (int i = group; i < kGroupCount; i++) { |
| 11753 | int last_of_group = starts.at(i + 1) - 1; |
| 11754 | ASSERT(last_of_group >= gap); |
| 11755 | if (last_of_group == gap) continue; |
| 11756 | copy(last_of_group, gap); |
| 11757 | gap = last_of_group; |
| 11758 | } |
| 11759 | ASSERT(gap == starts.number_of_entries() - 1); |
| 11760 | clear_at(gap); // Clear last gap. |
| 11761 | set_number_of_entries(group, end - start - 1); |
| 11762 | |
| 11763 | #ifdef DEBUG |
| 11764 | for (int i = start; i < end - 1; i++) { |
| 11765 | ASSERT(is_code_at(i) || compilation_info_at(i) != info); |
| 11766 | } |
| 11767 | #endif |
| 11768 | } |
| 11769 | |
| 11770 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11771 | bool DependentCode::Contains(DependencyGroup group, Code* code) { |
| 11772 | GroupStartIndexes starts(this); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 11773 | int start = starts.at(group); |
| 11774 | int end = starts.at(group + 1); |
| 11775 | for (int i = start; i < end; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11776 | if (object_at(i) == code) return true; |
danno@chromium.org | 94b0d6f | 2013-02-04 13:33:20 +0000 | [diff] [blame] | 11777 | } |
| 11778 | return false; |
| 11779 | } |
| 11780 | |
| 11781 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11782 | void DependentCode::DeoptimizeDependentCodeGroup( |
svenpanne@chromium.org | 876cca8 | 2013-03-18 14:43:20 +0000 | [diff] [blame] | 11783 | Isolate* isolate, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11784 | DependentCode::DependencyGroup group) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 11785 | ASSERT(AllowCodeDependencyChange::IsAllowed()); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 11786 | DisallowHeapAllocation no_allocation_scope; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11787 | DependentCode::GroupStartIndexes starts(this); |
| 11788 | int start = starts.at(group); |
| 11789 | int end = starts.at(group + 1); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11790 | int code_entries = starts.number_of_entries(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11791 | if (start == end) return; |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 11792 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11793 | // Mark all the code that needs to be deoptimized. |
| 11794 | bool marked = false; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11795 | for (int i = start; i < end; i++) { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11796 | if (is_code_at(i)) { |
| 11797 | Code* code = code_at(i); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11798 | if (!code->marked_for_deoptimization()) { |
| 11799 | code->set_marked_for_deoptimization(true); |
| 11800 | marked = true; |
| 11801 | } |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11802 | } else { |
| 11803 | CompilationInfo* info = compilation_info_at(i); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11804 | info->AbortDueToDependencyChange(); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11805 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11806 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11807 | // 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] | 11808 | for (int src = end, dst = start; src < code_entries; src++, dst++) { |
| 11809 | copy(src, dst); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11810 | } |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11811 | // Now the holes are at the end of the array, zap them for heap-verifier. |
| 11812 | int removed = end - start; |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 11813 | for (int i = code_entries - removed; i < code_entries; i++) { |
| 11814 | clear_at(i); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 11815 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11816 | set_number_of_entries(group, 0); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 11817 | |
| 11818 | if (marked) Deoptimizer::DeoptimizeMarkedCode(isolate); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 11819 | } |
| 11820 | |
| 11821 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11822 | Handle<Object> JSObject::SetPrototype(Handle<JSObject> object, |
| 11823 | Handle<Object> value, |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 11824 | bool skip_hidden_prototypes) { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11825 | #ifdef DEBUG |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11826 | int size = object->Size(); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 11827 | #endif |
| 11828 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11829 | Isolate* isolate = object->GetIsolate(); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11830 | Heap* heap = isolate->heap(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11831 | // Silently ignore the change if value is not a JSObject or null. |
| 11832 | // SpiderMonkey behaves this way. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 11833 | if (!value->IsJSReceiver() && !value->IsNull()) return value; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11834 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11835 | // From 8.6.2 Object Internal Methods |
| 11836 | // ... |
| 11837 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 11838 | // [[Prototype]] internal properties of the object may not be modified. |
| 11839 | // ... |
| 11840 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 11841 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 11842 | // paragraph. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11843 | if (!object->map()->is_extensible()) { |
| 11844 | Handle<Object> args[] = { object }; |
| 11845 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 11846 | "non_extensible_proto", HandleVector(args, ARRAY_SIZE(args))); |
| 11847 | isolate->Throw(*error); |
| 11848 | return Handle<Object>(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11849 | } |
| 11850 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11851 | // Before we can set the prototype we need to be sure |
| 11852 | // prototype cycles are prevented. |
| 11853 | // It is sufficient to validate that the receiver is not in the new prototype |
| 11854 | // chain. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11855 | for (Object* pt = *value; |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11856 | pt != heap->null_value(); |
| 11857 | pt = pt->GetPrototype(isolate)) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11858 | if (JSReceiver::cast(pt) == *object) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11859 | // Cycle detected. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11860 | Handle<Object> error = isolate->factory()->NewError( |
| 11861 | "cyclic_proto", HandleVector<Object>(NULL, 0)); |
| 11862 | isolate->Throw(*error); |
| 11863 | return Handle<Object>(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11864 | } |
| 11865 | } |
| 11866 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11867 | bool dictionary_elements_in_chain = |
| 11868 | object->map()->DictionaryElementsInPrototypeChainOnly(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11869 | Handle<JSObject> real_receiver = object; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11870 | |
| 11871 | if (skip_hidden_prototypes) { |
| 11872 | // Find the first object in the chain whose prototype object is not |
| 11873 | // hidden and set the new prototype on that object. |
| 11874 | Object* current_proto = real_receiver->GetPrototype(); |
| 11875 | while (current_proto->IsJSObject() && |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11876 | JSObject::cast(current_proto)->map()->is_hidden_prototype()) { |
| 11877 | real_receiver = handle(JSObject::cast(current_proto), isolate); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 11878 | current_proto = current_proto->GetPrototype(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11879 | } |
| 11880 | } |
| 11881 | |
| 11882 | // Set the new prototype of the object. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11883 | Handle<Map> map(real_receiver->map()); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11884 | |
| 11885 | // Nothing to do if prototype is already set. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11886 | if (map->prototype() == *value) return value; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 11887 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 11888 | if (value->IsJSObject()) { |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11889 | JSObject::OptimizeAsPrototype(Handle<JSObject>::cast(value)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 11890 | } |
| 11891 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11892 | Handle<Map> new_map = Map::GetPrototypeTransition(map, value); |
| 11893 | if (new_map.is_null()) { |
| 11894 | new_map = Map::Copy(map); |
| 11895 | Map::PutPrototypeTransition(map, value, new_map); |
| 11896 | new_map->set_prototype(*value); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11897 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11898 | ASSERT(new_map->prototype() == *value); |
| 11899 | real_receiver->set_map(*new_map); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11900 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 11901 | if (!dictionary_elements_in_chain && |
| 11902 | new_map->DictionaryElementsInPrototypeChainOnly()) { |
| 11903 | // If the prototype chain didn't previously have element callbacks, then |
| 11904 | // KeyedStoreICs need to be cleared to ensure any that involve this |
| 11905 | // map go generic. |
| 11906 | object->GetHeap()->ClearAllICsByKind(Code::KEYED_STORE_IC); |
| 11907 | } |
| 11908 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11909 | heap->ClearInstanceofCache(); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 11910 | ASSERT(size == object->Size()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11911 | return value; |
| 11912 | } |
| 11913 | |
| 11914 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11915 | MaybeObject* JSObject::EnsureCanContainElements(Arguments* args, |
| 11916 | uint32_t first_arg, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11917 | uint32_t arg_count, |
| 11918 | EnsureElementsMode mode) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11919 | // Elements in |Arguments| are ordered backwards (because they're on the |
| 11920 | // stack), but the method that's called here iterates over them in forward |
| 11921 | // direction. |
| 11922 | return EnsureCanContainElements( |
| 11923 | args->arguments() - first_arg - (arg_count - 1), |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 11924 | arg_count, mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11925 | } |
| 11926 | |
| 11927 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 11928 | AccessorPair* JSObject::GetLocalPropertyAccessorPair(Name* name) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11929 | uint32_t index = 0; |
| 11930 | if (name->AsArrayIndex(&index)) { |
| 11931 | return GetLocalElementAccessorPair(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11932 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11933 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11934 | LookupResult lookup(GetIsolate()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11935 | LocalLookupRealNamedProperty(name, &lookup); |
| 11936 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 11937 | if (lookup.IsPropertyCallbacks() && |
| 11938 | lookup.GetCallbackObject()->IsAccessorPair()) { |
| 11939 | return AccessorPair::cast(lookup.GetCallbackObject()); |
| 11940 | } |
| 11941 | return NULL; |
| 11942 | } |
| 11943 | |
| 11944 | |
| 11945 | AccessorPair* JSObject::GetLocalElementAccessorPair(uint32_t index) { |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11946 | if (IsJSGlobalProxy()) { |
| 11947 | Object* proto = GetPrototype(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11948 | if (proto->IsNull()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11949 | ASSERT(proto->IsJSGlobalObject()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11950 | return JSObject::cast(proto)->GetLocalElementAccessorPair(index); |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11951 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11952 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11953 | // Check for lookup interceptor. |
| 11954 | if (HasIndexedInterceptor()) return NULL; |
mstarzinger@chromium.org | f4f9499 | 2012-11-19 15:39:24 +0000 | [diff] [blame] | 11955 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 11956 | return GetElementsAccessor()->GetAccessorPair(this, this, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11957 | } |
| 11958 | |
| 11959 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11960 | Handle<Object> JSObject::SetElementWithInterceptor( |
| 11961 | Handle<JSObject> object, |
| 11962 | uint32_t index, |
| 11963 | Handle<Object> value, |
| 11964 | PropertyAttributes attributes, |
| 11965 | StrictModeFlag strict_mode, |
| 11966 | bool check_prototype, |
| 11967 | SetPropertyMode set_mode) { |
| 11968 | Isolate* isolate = object->GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 11969 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11970 | // Make sure that the top context does not change when doing |
| 11971 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 11972 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 11973 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11974 | Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11975 | if (!interceptor->setter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 11976 | v8::IndexedPropertySetterCallback setter = |
| 11977 | v8::ToCData<v8::IndexedPropertySetterCallback>(interceptor->setter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11978 | LOG(isolate, |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11979 | ApiIndexedPropertyAccess("interceptor-indexed-set", *object, index)); |
| 11980 | PropertyCallbackArguments args(isolate, interceptor->data(), *object, |
| 11981 | *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11982 | v8::Handle<v8::Value> result = |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11983 | args.Call(setter, index, v8::Utils::ToLocal(value)); |
| 11984 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 11985 | if (!result.IsEmpty()) return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11986 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 11987 | |
| 11988 | return SetElementWithoutInterceptor(object, index, value, attributes, |
| 11989 | strict_mode, |
| 11990 | check_prototype, |
| 11991 | set_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11992 | } |
| 11993 | |
| 11994 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11995 | MaybeObject* JSObject::GetElementWithCallback(Object* receiver, |
| 11996 | Object* structure, |
| 11997 | uint32_t index, |
| 11998 | Object* holder) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11999 | Isolate* isolate = GetIsolate(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 12000 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12001 | |
| 12002 | // api style callbacks. |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12003 | if (structure->IsExecutableAccessorInfo()) { |
| 12004 | Handle<ExecutableAccessorInfo> data( |
| 12005 | ExecutableAccessorInfo::cast(structure)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12006 | Object* fun_obj = data->getter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 12007 | v8::AccessorGetterCallback call_fun = |
| 12008 | v8::ToCData<v8::AccessorGetterCallback>(fun_obj); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 12009 | if (call_fun == NULL) return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12010 | HandleScope scope(isolate); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12011 | Handle<JSObject> self(JSObject::cast(receiver)); |
| 12012 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12013 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12014 | Handle<String> key = isolate->factory()->NumberToString(number); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12015 | LOG(isolate, ApiNamedPropertyAccess("load", *self, *key)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 12016 | PropertyCallbackArguments |
| 12017 | args(isolate, data->data(), *self, *holder_handle); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12018 | 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] | 12019 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 12020 | if (result.IsEmpty()) return isolate->heap()->undefined_value(); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 12021 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 12022 | result_internal->VerifyApiCallResultType(); |
| 12023 | return *result_internal; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12024 | } |
| 12025 | |
| 12026 | // __defineGetter__ callback |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12027 | if (structure->IsAccessorPair()) { |
| 12028 | Object* getter = AccessorPair::cast(structure)->getter(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12029 | if (getter->IsSpecFunction()) { |
| 12030 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
| 12031 | return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12032 | } |
| 12033 | // Getter is not a function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12034 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12035 | } |
| 12036 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12037 | if (structure->IsDeclaredAccessorInfo()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 12038 | return GetDeclaredAccessorProperty(receiver, |
| 12039 | DeclaredAccessorInfo::cast(structure), |
| 12040 | isolate); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12041 | } |
| 12042 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12043 | UNREACHABLE(); |
| 12044 | return NULL; |
| 12045 | } |
| 12046 | |
| 12047 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12048 | Handle<Object> JSObject::SetElementWithCallback(Handle<JSObject> object, |
| 12049 | Handle<Object> structure, |
| 12050 | uint32_t index, |
| 12051 | Handle<Object> value, |
| 12052 | Handle<JSObject> holder, |
| 12053 | StrictModeFlag strict_mode) { |
| 12054 | Isolate* isolate = object->GetIsolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12055 | |
| 12056 | // We should never get here to initialize a const with the hole |
| 12057 | // value since a const declaration would conflict with the setter. |
| 12058 | ASSERT(!value->IsTheHole()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12059 | |
| 12060 | // 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] | 12061 | // data structure used to store the callbacks. Eventually foreign |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12062 | // callbacks should be phased out. |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 12063 | ASSERT(!structure->IsForeign()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12064 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12065 | if (structure->IsExecutableAccessorInfo()) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12066 | // api style callbacks |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12067 | Handle<ExecutableAccessorInfo> data = |
| 12068 | Handle<ExecutableAccessorInfo>::cast(structure); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12069 | Object* call_obj = data->setter(); |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 12070 | v8::AccessorSetterCallback call_fun = |
| 12071 | v8::ToCData<v8::AccessorSetterCallback>(call_obj); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12072 | if (call_fun == NULL) return value; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12073 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 12074 | Handle<String> key(isolate->factory()->NumberToString(number)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12075 | LOG(isolate, ApiNamedPropertyAccess("store", *object, *key)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 12076 | PropertyCallbackArguments |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12077 | args(isolate, data->data(), *object, *holder); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12078 | args.Call(call_fun, |
| 12079 | v8::Utils::ToLocal(key), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12080 | v8::Utils::ToLocal(value)); |
| 12081 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 12082 | return value; |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12083 | } |
| 12084 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12085 | if (structure->IsAccessorPair()) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12086 | Handle<Object> setter(AccessorPair::cast(*structure)->setter(), isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12087 | if (setter->IsSpecFunction()) { |
| 12088 | // TODO(rossberg): nicer would be to cast to some JSCallable here... |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12089 | return SetPropertyWithDefinedSetter( |
| 12090 | object, Handle<JSReceiver>::cast(setter), value); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12091 | } else { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 12092 | if (strict_mode == kNonStrictMode) { |
| 12093 | return value; |
| 12094 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12095 | Handle<Object> key(isolate->factory()->NewNumberFromUint(index)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12096 | Handle<Object> args[2] = { key, holder }; |
| 12097 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 12098 | "no_setter_in_callback", HandleVector(args, 2)); |
| 12099 | isolate->Throw(*error); |
| 12100 | return Handle<Object>(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12101 | } |
| 12102 | } |
| 12103 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12104 | // TODO(dcarney): Handle correctly. |
| 12105 | if (structure->IsDeclaredAccessorInfo()) return value; |
| 12106 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12107 | UNREACHABLE(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12108 | return Handle<Object>(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 12109 | } |
| 12110 | |
| 12111 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12112 | bool JSObject::HasFastArgumentsElements() { |
| 12113 | Heap* heap = GetHeap(); |
| 12114 | if (!elements()->IsFixedArray()) return false; |
| 12115 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 12116 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 12117 | return false; |
| 12118 | } |
| 12119 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 12120 | return !arguments->IsDictionary(); |
| 12121 | } |
| 12122 | |
| 12123 | |
| 12124 | bool JSObject::HasDictionaryArgumentsElements() { |
| 12125 | Heap* heap = GetHeap(); |
| 12126 | if (!elements()->IsFixedArray()) return false; |
| 12127 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 12128 | if (elements->map() != heap->non_strict_arguments_elements_map()) { |
| 12129 | return false; |
| 12130 | } |
| 12131 | FixedArray* arguments = FixedArray::cast(elements->get(1)); |
| 12132 | return arguments->IsDictionary(); |
| 12133 | } |
| 12134 | |
| 12135 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12136 | // Adding n elements in fast case is O(n*n). |
| 12137 | // Note: revisit design to have dual undefined values to capture absent |
| 12138 | // elements. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12139 | Handle<Object> JSObject::SetFastElement(Handle<JSObject> object, |
| 12140 | uint32_t index, |
| 12141 | Handle<Object> value, |
| 12142 | StrictModeFlag strict_mode, |
| 12143 | bool check_prototype) { |
| 12144 | ASSERT(object->HasFastSmiOrObjectElements() || |
| 12145 | object->HasFastArgumentsElements()); |
| 12146 | |
| 12147 | Isolate* isolate = object->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12148 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 12149 | // Array optimizations rely on the prototype lookups of Array objects always |
| 12150 | // returning undefined. If there is a store to the initial prototype object, |
| 12151 | // make sure all of these optimizations are invalidated. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12152 | if (isolate->is_initial_object_prototype(*object) || |
| 12153 | isolate->is_initial_array_prototype(*object)) { |
| 12154 | object->map()->dependent_code()->DeoptimizeDependentCodeGroup(isolate, |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 12155 | DependentCode::kElementsCantBeAddedGroup); |
| 12156 | } |
| 12157 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12158 | Handle<FixedArray> backing_store(FixedArray::cast(object->elements())); |
| 12159 | if (backing_store->map() == |
| 12160 | isolate->heap()->non_strict_arguments_elements_map()) { |
| 12161 | backing_store = handle(FixedArray::cast(backing_store->get(1))); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12162 | } else { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12163 | backing_store = EnsureWritableFastElements(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12164 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12165 | uint32_t capacity = static_cast<uint32_t>(backing_store->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12166 | |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 12167 | if (check_prototype && |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12168 | (index >= capacity || backing_store->get(index)->IsTheHole())) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 12169 | bool found; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12170 | Handle<Object> result = SetElementWithCallbackSetterInPrototypes( |
| 12171 | object, index, value, &found, strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 12172 | if (found) return result; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 12173 | } |
| 12174 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12175 | uint32_t new_capacity = capacity; |
| 12176 | // Check if the length property of this object needs to be updated. |
| 12177 | uint32_t array_length = 0; |
| 12178 | bool must_update_array_length = false; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12179 | bool introduces_holes = true; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12180 | if (object->IsJSArray()) { |
| 12181 | CHECK(Handle<JSArray>::cast(object)->length()->ToArrayIndex(&array_length)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12182 | introduces_holes = index > array_length; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12183 | if (index >= array_length) { |
| 12184 | must_update_array_length = true; |
| 12185 | array_length = index + 1; |
| 12186 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12187 | } else { |
| 12188 | introduces_holes = index >= capacity; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12189 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12190 | |
| 12191 | // If the array is growing, and it's not growth by a single element at the |
| 12192 | // end, make sure that the ElementsKind is HOLEY. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12193 | ElementsKind elements_kind = object->GetElementsKind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12194 | if (introduces_holes && |
| 12195 | IsFastElementsKind(elements_kind) && |
| 12196 | !IsFastHoleyElementsKind(elements_kind)) { |
| 12197 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12198 | TransitionElementsKind(object, transitioned_kind); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12199 | } |
| 12200 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12201 | // Check if the capacity of the backing store needs to be increased, or if |
| 12202 | // a transition to slow elements is necessary. |
| 12203 | if (index >= capacity) { |
| 12204 | bool convert_to_slow = true; |
| 12205 | if ((index - capacity) < kMaxGap) { |
| 12206 | new_capacity = NewElementsCapacity(index + 1); |
| 12207 | ASSERT(new_capacity > index); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12208 | if (!object->ShouldConvertToSlowElements(new_capacity)) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12209 | convert_to_slow = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12210 | } |
| 12211 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12212 | if (convert_to_slow) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12213 | NormalizeElements(object); |
| 12214 | return SetDictionaryElement(object, index, value, NONE, strict_mode, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12215 | check_prototype); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12216 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12217 | } |
| 12218 | // Convert to fast double elements if appropriate. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12219 | if (object->HasFastSmiElements() && !value->IsSmi() && value->IsNumber()) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12220 | // Consider fixing the boilerplate as well if we have one. |
| 12221 | ElementsKind to_kind = IsHoleyElementsKind(elements_kind) |
| 12222 | ? FAST_HOLEY_DOUBLE_ELEMENTS |
| 12223 | : FAST_DOUBLE_ELEMENTS; |
| 12224 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12225 | UpdateAllocationSite(object, to_kind); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12226 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12227 | SetFastDoubleElementsCapacityAndLength(object, new_capacity, array_length); |
| 12228 | FixedDoubleArray::cast(object->elements())->set(index, value->Number()); |
| 12229 | object->ValidateElements(); |
ager@chromium.org | bdf2f94 | 2008-10-17 07:23:00 +0000 | [diff] [blame] | 12230 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12231 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12232 | // Change elements kind from Smi-only to generic FAST if necessary. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12233 | if (object->HasFastSmiElements() && !value->IsSmi()) { |
| 12234 | ElementsKind kind = object->HasFastHoleyElements() |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12235 | ? FAST_HOLEY_ELEMENTS |
| 12236 | : FAST_ELEMENTS; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12237 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12238 | UpdateAllocationSite(object, kind); |
| 12239 | Handle<Map> new_map = GetElementsTransitionMap(object, kind); |
| 12240 | object->set_map(*new_map); |
| 12241 | ASSERT(IsFastObjectElementsKind(object->GetElementsKind())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12242 | } |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12243 | // Increase backing store capacity if that's been decided previously. |
| 12244 | if (new_capacity != capacity) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12245 | SetFastElementsCapacitySmiMode smi_mode = |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12246 | value->IsSmi() && object->HasFastSmiElements() |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12247 | ? kAllowSmiElements |
| 12248 | : kDontAllowSmiElements; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12249 | Handle<FixedArray> new_elements = |
| 12250 | SetFastElementsCapacityAndLength(object, new_capacity, array_length, |
| 12251 | smi_mode); |
| 12252 | new_elements->set(index, *value); |
| 12253 | object->ValidateElements(); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12254 | return value; |
| 12255 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12256 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12257 | // Finally, set the new element and length. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12258 | ASSERT(object->elements()->IsFixedArray()); |
| 12259 | backing_store->set(index, *value); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12260 | if (must_update_array_length) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12261 | Handle<JSArray>::cast(object)->set_length(Smi::FromInt(array_length)); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12262 | } |
| 12263 | return value; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12264 | } |
| 12265 | |
| 12266 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12267 | Handle<Object> JSObject::SetDictionaryElement(Handle<JSObject> object, |
| 12268 | uint32_t index, |
| 12269 | Handle<Object> value, |
| 12270 | PropertyAttributes attributes, |
| 12271 | StrictModeFlag strict_mode, |
| 12272 | bool check_prototype, |
| 12273 | SetPropertyMode set_mode) { |
| 12274 | ASSERT(object->HasDictionaryElements() || |
| 12275 | object->HasDictionaryArgumentsElements()); |
| 12276 | Isolate* isolate = object->GetIsolate(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12277 | |
| 12278 | // Insert element in the dictionary. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12279 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12280 | bool is_arguments = |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12281 | (elements->map() == isolate->heap()->non_strict_arguments_elements_map()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12282 | Handle<SeededNumberDictionary> dictionary(is_arguments |
| 12283 | ? SeededNumberDictionary::cast(elements->get(1)) |
| 12284 | : SeededNumberDictionary::cast(*elements)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12285 | |
| 12286 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12287 | if (entry != SeededNumberDictionary::kNotFound) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12288 | Handle<Object> element(dictionary->ValueAt(entry), isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12289 | PropertyDetails details = dictionary->DetailsAt(entry); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12290 | if (details.type() == CALLBACKS && set_mode == SET_PROPERTY) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12291 | return SetElementWithCallback(object, element, index, value, object, |
| 12292 | strict_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12293 | } else { |
| 12294 | dictionary->UpdateMaxNumberKey(index); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12295 | // 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] | 12296 | // is read-only (a declared const that has not been initialized). If a |
| 12297 | // value is being defined we skip attribute checks completely. |
| 12298 | if (set_mode == DEFINE_PROPERTY) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12299 | details = PropertyDetails( |
| 12300 | attributes, NORMAL, details.dictionary_index()); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12301 | dictionary->DetailsAtPut(entry, details); |
| 12302 | } else if (details.IsReadOnly() && !element->IsTheHole()) { |
| 12303 | if (strict_mode == kNonStrictMode) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12304 | return isolate->factory()->undefined_value(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12305 | } else { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12306 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12307 | Handle<Object> args[2] = { number, object }; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12308 | Handle<Object> error = |
| 12309 | isolate->factory()->NewTypeError("strict_read_only_property", |
| 12310 | HandleVector(args, 2)); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12311 | isolate->Throw(*error); |
| 12312 | return Handle<Object>(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12313 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12314 | } |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12315 | // Elements of the arguments object in slow mode might be slow aliases. |
| 12316 | if (is_arguments && element->IsAliasedArgumentsEntry()) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12317 | Handle<AliasedArgumentsEntry> entry = |
| 12318 | Handle<AliasedArgumentsEntry>::cast(element); |
| 12319 | Handle<Context> context(Context::cast(elements->get(0))); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12320 | int context_index = entry->aliased_context_slot(); |
| 12321 | ASSERT(!context->get(context_index)->IsTheHole()); |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12322 | context->set(context_index, *value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12323 | // For elements that are still writable we keep slow aliasing. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12324 | if (!details.IsReadOnly()) value = element; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12325 | } |
jkummerow@chromium.org | 5323a9c | 2012-12-10 19:00:50 +0000 | [diff] [blame] | 12326 | dictionary->ValueAtPut(entry, *value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12327 | } |
| 12328 | } else { |
| 12329 | // 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] | 12330 | // Can cause GC! |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12331 | if (check_prototype) { |
| 12332 | bool found; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12333 | Handle<Object> result = SetElementWithCallbackSetterInPrototypes(object, |
| 12334 | index, value, &found, strict_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12335 | if (found) return result; |
| 12336 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12337 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12338 | // When we set the is_extensible flag to false we always force the |
| 12339 | // element into dictionary mode (and force them to stay there). |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12340 | if (!object->map()->is_extensible()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12341 | if (strict_mode == kNonStrictMode) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12342 | return isolate->factory()->undefined_value(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12343 | } else { |
| 12344 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 12345 | Handle<String> name = isolate->factory()->NumberToString(number); |
| 12346 | Handle<Object> args[1] = { name }; |
| 12347 | Handle<Object> error = |
| 12348 | isolate->factory()->NewTypeError("object_not_extensible", |
| 12349 | HandleVector(args, 1)); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12350 | isolate->Throw(*error); |
| 12351 | return Handle<Object>(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12352 | } |
| 12353 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12354 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12355 | PropertyDetails details = PropertyDetails(attributes, NORMAL, 0); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12356 | Handle<SeededNumberDictionary> new_dictionary = |
| 12357 | SeededNumberDictionary::AddNumberEntry(dictionary, index, value, |
| 12358 | details); |
| 12359 | if (*dictionary != *new_dictionary) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12360 | if (is_arguments) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12361 | elements->set(1, *new_dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12362 | } else { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12363 | object->set_elements(*new_dictionary); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12364 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12365 | dictionary = new_dictionary; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12366 | } |
| 12367 | } |
| 12368 | |
| 12369 | // Update the array length if this JSObject is an array. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12370 | if (object->IsJSArray()) { |
| 12371 | JSArray::JSArrayUpdateLengthFromIndex(Handle<JSArray>::cast(object), index, |
| 12372 | value); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12373 | } |
| 12374 | |
| 12375 | // Attempt to put this object back in fast case. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12376 | if (object->ShouldConvertToFastElements()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12377 | uint32_t new_length = 0; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12378 | if (object->IsJSArray()) { |
| 12379 | CHECK(Handle<JSArray>::cast(object)->length()->ToArrayIndex(&new_length)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12380 | } else { |
| 12381 | new_length = dictionary->max_number_key() + 1; |
| 12382 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12383 | SetFastElementsCapacitySmiMode smi_mode = FLAG_smi_only_arrays |
| 12384 | ? kAllowSmiElements |
| 12385 | : kDontAllowSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12386 | bool has_smi_only_elements = false; |
| 12387 | bool should_convert_to_fast_double_elements = |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12388 | object->ShouldConvertToFastDoubleElements(&has_smi_only_elements); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12389 | if (has_smi_only_elements) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12390 | smi_mode = kForceSmiElements; |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 12391 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12392 | |
| 12393 | if (should_convert_to_fast_double_elements) { |
| 12394 | SetFastDoubleElementsCapacityAndLength(object, new_length, new_length); |
| 12395 | } else { |
| 12396 | SetFastElementsCapacityAndLength(object, new_length, new_length, |
| 12397 | smi_mode); |
| 12398 | } |
| 12399 | object->ValidateElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12400 | #ifdef DEBUG |
| 12401 | if (FLAG_trace_normalization) { |
| 12402 | PrintF("Object elements are fast case again:\n"); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12403 | object->Print(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12404 | } |
| 12405 | #endif |
| 12406 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12407 | return value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12408 | } |
| 12409 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12410 | Handle<Object> JSObject::SetFastDoubleElement( |
| 12411 | Handle<JSObject> object, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12412 | uint32_t index, |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12413 | Handle<Object> value, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12414 | StrictModeFlag strict_mode, |
| 12415 | bool check_prototype) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12416 | ASSERT(object->HasFastDoubleElements()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12417 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12418 | Handle<FixedArrayBase> base_elms(FixedArrayBase::cast(object->elements())); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 12419 | uint32_t elms_length = static_cast<uint32_t>(base_elms->length()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12420 | |
| 12421 | // If storing to an element that isn't in the array, pass the store request |
| 12422 | // up the prototype chain before storing in the receiver's elements. |
| 12423 | if (check_prototype && |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 12424 | (index >= elms_length || |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12425 | Handle<FixedDoubleArray>::cast(base_elms)->is_the_hole(index))) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12426 | bool found; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12427 | Handle<Object> result = SetElementWithCallbackSetterInPrototypes(object, |
| 12428 | index, value, &found, strict_mode); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12429 | if (found) return result; |
| 12430 | } |
| 12431 | |
| 12432 | // If the value object is not a heap number, switch to fast elements and try |
| 12433 | // again. |
| 12434 | bool value_is_smi = value->IsSmi(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12435 | bool introduces_holes = true; |
| 12436 | uint32_t length = elms_length; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12437 | if (object->IsJSArray()) { |
| 12438 | CHECK(Handle<JSArray>::cast(object)->length()->ToArrayIndex(&length)); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12439 | introduces_holes = index > length; |
| 12440 | } else { |
| 12441 | introduces_holes = index >= elms_length; |
| 12442 | } |
| 12443 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12444 | if (!value->IsNumber()) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12445 | SetFastElementsCapacityAndLength(object, elms_length, length, |
| 12446 | kDontAllowSmiElements); |
| 12447 | Handle<Object> result = SetFastElement(object, index, value, strict_mode, |
| 12448 | check_prototype); |
| 12449 | RETURN_IF_EMPTY_HANDLE_VALUE(object->GetIsolate(), result, |
| 12450 | Handle<Object>()); |
| 12451 | object->ValidateElements(); |
| 12452 | return result; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12453 | } |
| 12454 | |
| 12455 | double double_value = value_is_smi |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12456 | ? static_cast<double>(Handle<Smi>::cast(value)->value()) |
| 12457 | : Handle<HeapNumber>::cast(value)->value(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12458 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12459 | // If the array is growing, and it's not growth by a single element at the |
| 12460 | // end, make sure that the ElementsKind is HOLEY. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12461 | ElementsKind elements_kind = object->GetElementsKind(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12462 | if (introduces_holes && !IsFastHoleyElementsKind(elements_kind)) { |
| 12463 | ElementsKind transitioned_kind = GetHoleyElementsKind(elements_kind); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12464 | TransitionElementsKind(object, transitioned_kind); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12465 | } |
| 12466 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12467 | // Check whether there is extra space in the fixed array. |
| 12468 | if (index < elms_length) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12469 | Handle<FixedDoubleArray> elms(FixedDoubleArray::cast(object->elements())); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12470 | elms->set(index, double_value); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12471 | if (object->IsJSArray()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12472 | // Update the length of the array if needed. |
| 12473 | uint32_t array_length = 0; |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12474 | CHECK( |
| 12475 | Handle<JSArray>::cast(object)->length()->ToArrayIndex(&array_length)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12476 | if (index >= array_length) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12477 | Handle<JSArray>::cast(object)->set_length(Smi::FromInt(index + 1)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12478 | } |
| 12479 | } |
| 12480 | return value; |
| 12481 | } |
| 12482 | |
| 12483 | // Allow gap in fast case. |
| 12484 | if ((index - elms_length) < kMaxGap) { |
| 12485 | // Try allocating extra space. |
| 12486 | int new_capacity = NewElementsCapacity(index+1); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12487 | if (!object->ShouldConvertToSlowElements(new_capacity)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12488 | ASSERT(static_cast<uint32_t>(new_capacity) > index); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12489 | SetFastDoubleElementsCapacityAndLength(object, new_capacity, index + 1); |
| 12490 | FixedDoubleArray::cast(object->elements())->set(index, double_value); |
| 12491 | object->ValidateElements(); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12492 | return value; |
| 12493 | } |
| 12494 | } |
| 12495 | |
| 12496 | // Otherwise default to slow case. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12497 | ASSERT(object->HasFastDoubleElements()); |
| 12498 | ASSERT(object->map()->has_fast_double_elements()); |
| 12499 | ASSERT(object->elements()->IsFixedDoubleArray()); |
| 12500 | |
| 12501 | NormalizeElements(object); |
| 12502 | ASSERT(object->HasDictionaryElements()); |
| 12503 | return SetElement(object, index, value, NONE, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12504 | } |
| 12505 | |
| 12506 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 12507 | Handle<Object> JSReceiver::SetElement(Handle<JSReceiver> object, |
| 12508 | uint32_t index, |
| 12509 | Handle<Object> value, |
| 12510 | PropertyAttributes attributes, |
| 12511 | StrictModeFlag strict_mode) { |
| 12512 | if (object->IsJSProxy()) { |
| 12513 | return JSProxy::SetElementWithHandler( |
| 12514 | Handle<JSProxy>::cast(object), object, index, value, strict_mode); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12515 | } |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 12516 | return JSObject::SetElement( |
| 12517 | Handle<JSObject>::cast(object), index, value, attributes, strict_mode); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12518 | } |
| 12519 | |
| 12520 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12521 | Handle<Object> JSObject::SetOwnElement(Handle<JSObject> object, |
| 12522 | uint32_t index, |
| 12523 | Handle<Object> value, |
| 12524 | StrictModeFlag strict_mode) { |
| 12525 | ASSERT(!object->HasExternalArrayElements()); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12526 | return JSObject::SetElement(object, index, value, NONE, strict_mode, false); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12527 | } |
| 12528 | |
| 12529 | |
| 12530 | Handle<Object> JSObject::SetElement(Handle<JSObject> object, |
| 12531 | uint32_t index, |
| 12532 | Handle<Object> value, |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12533 | PropertyAttributes attributes, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12534 | StrictModeFlag strict_mode, |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 12535 | bool check_prototype, |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12536 | SetPropertyMode set_mode) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12537 | Isolate* isolate = object->GetIsolate(); |
| 12538 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12539 | if (object->HasExternalArrayElements()) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 12540 | if (!value->IsNumber() && !value->IsUndefined()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12541 | bool has_exception; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12542 | Handle<Object> number = |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12543 | Execution::ToNumber(isolate, value, &has_exception); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12544 | if (has_exception) return Handle<Object>(); |
| 12545 | value = number; |
| 12546 | } |
| 12547 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12548 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12549 | // Check access rights if needed. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12550 | if (object->IsAccessCheckNeeded()) { |
| 12551 | if (!isolate->MayIndexedAccess(*object, index, v8::ACCESS_SET)) { |
| 12552 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_SET); |
| 12553 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
| 12554 | return value; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12555 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12556 | } |
| 12557 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12558 | if (object->IsJSGlobalProxy()) { |
| 12559 | Handle<Object> proto(object->GetPrototype(), isolate); |
| 12560 | if (proto->IsNull()) return value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12561 | ASSERT(proto->IsJSGlobalObject()); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12562 | return SetElement(Handle<JSObject>::cast(proto), index, value, attributes, |
| 12563 | strict_mode, |
| 12564 | check_prototype, |
| 12565 | set_mode); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12566 | } |
| 12567 | |
| 12568 | // Don't allow element properties to be redefined for external arrays. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12569 | if (object->HasExternalArrayElements() && set_mode == DEFINE_PROPERTY) { |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12570 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12571 | Handle<Object> args[] = { object, number }; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12572 | Handle<Object> error = isolate->factory()->NewTypeError( |
| 12573 | "redef_external_array_element", HandleVector(args, ARRAY_SIZE(args))); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12574 | isolate->Throw(*error); |
| 12575 | return Handle<Object>(); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12576 | } |
| 12577 | |
| 12578 | // Normalize the elements to enable attributes on the property. |
| 12579 | if ((attributes & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12580 | Handle<SeededNumberDictionary> dictionary = NormalizeElements(object); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12581 | // Make sure that we never go back to fast case. |
| 12582 | dictionary->set_requires_slow_elements(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 12583 | } |
| 12584 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12585 | if (!(FLAG_harmony_observation && object->map()->is_observed())) { |
| 12586 | return object->HasIndexedInterceptor() |
| 12587 | ? SetElementWithInterceptor(object, index, value, attributes, strict_mode, |
| 12588 | check_prototype, |
| 12589 | set_mode) |
| 12590 | : SetElementWithoutInterceptor(object, index, value, attributes, |
| 12591 | strict_mode, |
| 12592 | check_prototype, |
| 12593 | set_mode); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12594 | } |
| 12595 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12596 | PropertyAttributes old_attributes = object->GetLocalElementAttribute(index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12597 | Handle<Object> old_value = isolate->factory()->the_hole_value(); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12598 | Handle<Object> old_length_handle; |
| 12599 | Handle<Object> new_length_handle; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12600 | |
| 12601 | if (old_attributes != ABSENT) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12602 | if (object->GetLocalElementAccessorPair(index) == NULL) |
| 12603 | old_value = Object::GetElement(isolate, object, index); |
| 12604 | } else if (object->IsJSArray()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12605 | // Store old array length in case adding an element grows the array. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12606 | old_length_handle = handle(Handle<JSArray>::cast(object)->length(), |
| 12607 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12608 | } |
| 12609 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12610 | // Check for lookup interceptor |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12611 | Handle<Object> result = object->HasIndexedInterceptor() |
| 12612 | ? SetElementWithInterceptor(object, index, value, attributes, strict_mode, |
| 12613 | check_prototype, |
| 12614 | set_mode) |
| 12615 | : SetElementWithoutInterceptor(object, index, value, attributes, |
| 12616 | strict_mode, |
| 12617 | check_prototype, |
| 12618 | set_mode); |
| 12619 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, result, Handle<Object>()); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12620 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12621 | Handle<String> name = isolate->factory()->Uint32ToString(index); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12622 | PropertyAttributes new_attributes = object->GetLocalElementAttribute(index); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 12623 | if (old_attributes == ABSENT) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12624 | if (object->IsJSArray() && |
| 12625 | !old_length_handle->SameValue( |
| 12626 | Handle<JSArray>::cast(object)->length())) { |
| 12627 | new_length_handle = handle(Handle<JSArray>::cast(object)->length(), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12628 | isolate); |
| 12629 | uint32_t old_length = 0; |
| 12630 | uint32_t new_length = 0; |
| 12631 | CHECK(old_length_handle->ToArrayIndex(&old_length)); |
| 12632 | CHECK(new_length_handle->ToArrayIndex(&new_length)); |
| 12633 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12634 | BeginPerformSplice(Handle<JSArray>::cast(object)); |
| 12635 | EnqueueChangeRecord(object, "add", name, old_value); |
| 12636 | EnqueueChangeRecord(object, "update", isolate->factory()->length_string(), |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12637 | old_length_handle); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12638 | EndPerformSplice(Handle<JSArray>::cast(object)); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12639 | Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12640 | EnqueueSpliceRecord(Handle<JSArray>::cast(object), old_length, deleted, |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 12641 | new_length - old_length); |
| 12642 | } else { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12643 | EnqueueChangeRecord(object, "add", name, old_value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12644 | } |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12645 | } else if (old_value->IsTheHole()) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12646 | EnqueueChangeRecord(object, "reconfigure", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12647 | } else { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12648 | Handle<Object> new_value = Object::GetElement(isolate, object, index); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 12649 | bool value_changed = !old_value->SameValue(*new_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12650 | if (old_attributes != new_attributes) { |
| 12651 | if (!value_changed) old_value = isolate->factory()->the_hole_value(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12652 | EnqueueChangeRecord(object, "reconfigure", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12653 | } else if (value_changed) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12654 | EnqueueChangeRecord(object, "update", name, old_value); |
mstarzinger@chromium.org | e3b8d0f | 2013-02-01 09:06:41 +0000 | [diff] [blame] | 12655 | } |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 12656 | } |
| 12657 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12658 | return result; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12659 | } |
| 12660 | |
| 12661 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12662 | Handle<Object> JSObject::SetElementWithoutInterceptor( |
| 12663 | Handle<JSObject> object, |
| 12664 | uint32_t index, |
| 12665 | Handle<Object> value, |
| 12666 | PropertyAttributes attributes, |
| 12667 | StrictModeFlag strict_mode, |
| 12668 | bool check_prototype, |
| 12669 | SetPropertyMode set_mode) { |
| 12670 | ASSERT(object->HasDictionaryElements() || |
| 12671 | object->HasDictionaryArgumentsElements() || |
| 12672 | (attributes & (DONT_DELETE | DONT_ENUM | READ_ONLY)) == 0); |
| 12673 | Isolate* isolate = object->GetIsolate(); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12674 | if (FLAG_trace_external_array_abuse && |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12675 | IsExternalArrayElementsKind(object->GetElementsKind())) { |
| 12676 | CheckArrayAbuse(*object, "external elements write", index); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12677 | } |
| 12678 | if (FLAG_trace_js_array_abuse && |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12679 | !IsExternalArrayElementsKind(object->GetElementsKind())) { |
| 12680 | if (object->IsJSArray()) { |
| 12681 | CheckArrayAbuse(*object, "elements write", index, true); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 12682 | } |
| 12683 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12684 | switch (object->GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12685 | case FAST_SMI_ELEMENTS: |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12686 | case FAST_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12687 | case FAST_HOLEY_SMI_ELEMENTS: |
| 12688 | case FAST_HOLEY_ELEMENTS: |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12689 | return SetFastElement(object, index, value, strict_mode, check_prototype); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12690 | case FAST_DOUBLE_ELEMENTS: |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12691 | case FAST_HOLEY_DOUBLE_ELEMENTS: |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12692 | return SetFastDoubleElement(object, index, value, strict_mode, |
| 12693 | check_prototype); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 12694 | case EXTERNAL_PIXEL_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12695 | ExternalPixelArray* pixels = ExternalPixelArray::cast(object->elements()); |
| 12696 | return handle(pixels->SetValue(index, *value), isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12697 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12698 | case EXTERNAL_BYTE_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12699 | Handle<ExternalByteArray> array( |
| 12700 | ExternalByteArray::cast(object->elements())); |
| 12701 | return ExternalByteArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12702 | } |
| 12703 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12704 | Handle<ExternalUnsignedByteArray> array( |
| 12705 | ExternalUnsignedByteArray::cast(object->elements())); |
| 12706 | return ExternalUnsignedByteArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12707 | } |
| 12708 | case EXTERNAL_SHORT_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12709 | Handle<ExternalShortArray> array(ExternalShortArray::cast( |
| 12710 | object->elements())); |
| 12711 | return ExternalShortArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12712 | } |
| 12713 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12714 | Handle<ExternalUnsignedShortArray> array( |
| 12715 | ExternalUnsignedShortArray::cast(object->elements())); |
| 12716 | return ExternalUnsignedShortArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12717 | } |
| 12718 | case EXTERNAL_INT_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12719 | Handle<ExternalIntArray> array( |
| 12720 | ExternalIntArray::cast(object->elements())); |
| 12721 | return ExternalIntArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12722 | } |
| 12723 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12724 | Handle<ExternalUnsignedIntArray> array( |
| 12725 | ExternalUnsignedIntArray::cast(object->elements())); |
| 12726 | return ExternalUnsignedIntArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12727 | } |
| 12728 | case EXTERNAL_FLOAT_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12729 | Handle<ExternalFloatArray> array( |
| 12730 | ExternalFloatArray::cast(object->elements())); |
| 12731 | return ExternalFloatArray::SetValue(array, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12732 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12733 | case EXTERNAL_DOUBLE_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12734 | Handle<ExternalDoubleArray> array( |
| 12735 | ExternalDoubleArray::cast(object->elements())); |
| 12736 | return ExternalDoubleArray::SetValue(array, index, value); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 12737 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12738 | case DICTIONARY_ELEMENTS: |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12739 | return SetDictionaryElement(object, index, value, attributes, strict_mode, |
| 12740 | check_prototype, |
| 12741 | set_mode); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12742 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12743 | Handle<FixedArray> parameter_map(FixedArray::cast(object->elements())); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12744 | uint32_t length = parameter_map->length(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12745 | Handle<Object> probe = index < length - 2 ? |
| 12746 | Handle<Object>(parameter_map->get(index + 2), isolate) : |
| 12747 | Handle<Object>(); |
| 12748 | if (!probe.is_null() && !probe->IsTheHole()) { |
| 12749 | Handle<Context> context(Context::cast(parameter_map->get(0))); |
| 12750 | int context_index = Handle<Smi>::cast(probe)->value(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 12751 | ASSERT(!context->get(context_index)->IsTheHole()); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12752 | context->set(context_index, *value); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12753 | // Redefining attributes of an aliased element destroys fast aliasing. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12754 | if (set_mode == SET_PROPERTY || attributes == NONE) return value; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12755 | parameter_map->set_the_hole(index + 2); |
| 12756 | // For elements that are still writable we re-establish slow aliasing. |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12757 | if ((attributes & READ_ONLY) == 0) { |
| 12758 | value = Handle<Object>::cast( |
| 12759 | isolate->factory()->NewAliasedArgumentsEntry(context_index)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12760 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12761 | } |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12762 | Handle<FixedArray> arguments(FixedArray::cast(parameter_map->get(1))); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12763 | if (arguments->IsDictionary()) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12764 | return SetDictionaryElement(object, index, value, attributes, |
| 12765 | strict_mode, |
| 12766 | check_prototype, |
| 12767 | set_mode); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12768 | } else { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12769 | return SetFastElement(object, index, value, strict_mode, |
| 12770 | check_prototype); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 12771 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12772 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12773 | } |
| 12774 | // All possible cases have been handled above. Add a return to avoid the |
| 12775 | // complaints from the compiler. |
| 12776 | UNREACHABLE(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12777 | return isolate->factory()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12778 | } |
| 12779 | |
| 12780 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12781 | void JSObject::TransitionElementsKind(Handle<JSObject> object, |
| 12782 | ElementsKind to_kind) { |
| 12783 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 12784 | object->TransitionElementsKind(to_kind)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12785 | } |
| 12786 | |
| 12787 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 12788 | bool AllocationSite::IsNestedSite() { |
| 12789 | ASSERT(FLAG_trace_track_allocation_sites); |
| 12790 | Object* current = GetHeap()->allocation_sites_list(); |
| 12791 | while (current != NULL && current->IsAllocationSite()) { |
| 12792 | AllocationSite* current_site = AllocationSite::cast(current); |
| 12793 | if (current_site->nested_site() == this) { |
| 12794 | return true; |
| 12795 | } |
| 12796 | current = current_site->weak_next(); |
| 12797 | } |
| 12798 | return false; |
| 12799 | } |
| 12800 | |
| 12801 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12802 | MaybeObject* AllocationSite::DigestTransitionFeedback(ElementsKind to_kind) { |
| 12803 | Isolate* isolate = GetIsolate(); |
| 12804 | |
| 12805 | if (SitePointsToLiteral() && transition_info()->IsJSArray()) { |
| 12806 | JSArray* transition_info = JSArray::cast(this->transition_info()); |
| 12807 | ElementsKind kind = transition_info->GetElementsKind(); |
| 12808 | // if kind is holey ensure that to_kind is as well. |
| 12809 | if (IsHoleyElementsKind(kind)) { |
| 12810 | to_kind = GetHoleyElementsKind(to_kind); |
| 12811 | } |
| 12812 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 12813 | // If the array is huge, it's not likely to be defined in a local |
| 12814 | // function, so we shouldn't make new instances of it very often. |
| 12815 | uint32_t length = 0; |
| 12816 | CHECK(transition_info->length()->ToArrayIndex(&length)); |
| 12817 | if (length <= kMaximumArrayBytesToPretransition) { |
| 12818 | if (FLAG_trace_track_allocation_sites) { |
| 12819 | bool is_nested = IsNestedSite(); |
| 12820 | PrintF( |
| 12821 | "AllocationSite: JSArray %p boilerplate %s updated %s->%s\n", |
| 12822 | reinterpret_cast<void*>(this), |
| 12823 | is_nested ? "(nested)" : "", |
| 12824 | ElementsKindToString(kind), |
| 12825 | ElementsKindToString(to_kind)); |
| 12826 | } |
| 12827 | MaybeObject* result = transition_info->TransitionElementsKind(to_kind); |
| 12828 | if (result->IsFailure()) return result; |
| 12829 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 12830 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 12831 | } |
| 12832 | } |
| 12833 | } else { |
| 12834 | ElementsKind kind = GetElementsKind(); |
| 12835 | // if kind is holey ensure that to_kind is as well. |
| 12836 | if (IsHoleyElementsKind(kind)) { |
| 12837 | to_kind = GetHoleyElementsKind(to_kind); |
| 12838 | } |
| 12839 | if (IsMoreGeneralElementsKindTransition(kind, to_kind)) { |
| 12840 | if (FLAG_trace_track_allocation_sites) { |
| 12841 | PrintF("AllocationSite: JSArray %p site updated %s->%s\n", |
| 12842 | reinterpret_cast<void*>(this), |
| 12843 | ElementsKindToString(kind), |
| 12844 | ElementsKindToString(to_kind)); |
| 12845 | } |
| 12846 | SetElementsKind(to_kind); |
| 12847 | dependent_code()->DeoptimizeDependentCodeGroup( |
| 12848 | isolate, DependentCode::kAllocationSiteTransitionChangedGroup); |
| 12849 | } |
| 12850 | } |
| 12851 | return this; |
| 12852 | } |
| 12853 | |
| 12854 | |
| 12855 | void AllocationSite::AddDependentCompilationInfo(Reason reason, |
| 12856 | CompilationInfo* info) { |
| 12857 | DependentCode::DependencyGroup group = ToDependencyGroup(reason); |
| 12858 | Handle<DependentCode> dep(dependent_code()); |
| 12859 | Handle<DependentCode> codes = |
| 12860 | DependentCode::Insert(dep, group, info->object_wrapper()); |
| 12861 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 12862 | info->dependencies(group)->Add(Handle<HeapObject>(this), info->zone()); |
| 12863 | } |
| 12864 | |
| 12865 | |
| 12866 | void AllocationSite::AddDependentCode(Reason reason, Handle<Code> code) { |
| 12867 | DependentCode::DependencyGroup group = ToDependencyGroup(reason); |
| 12868 | Handle<DependentCode> codes = DependentCode::Insert( |
| 12869 | Handle<DependentCode>(dependent_code()), group, code); |
| 12870 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 12871 | } |
| 12872 | |
| 12873 | |
| 12874 | void JSObject::UpdateAllocationSite(Handle<JSObject> object, |
| 12875 | ElementsKind to_kind) { |
| 12876 | CALL_HEAP_FUNCTION_VOID(object->GetIsolate(), |
| 12877 | object->UpdateAllocationSite(to_kind)); |
| 12878 | } |
| 12879 | |
| 12880 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12881 | MaybeObject* JSObject::UpdateAllocationSite(ElementsKind to_kind) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12882 | if (!FLAG_track_allocation_sites || !IsJSArray()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12883 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12884 | } |
| 12885 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 12886 | AllocationMemento* memento = AllocationMemento::FindForJSObject(this); |
| 12887 | if (memento == NULL || !memento->IsValid()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 12888 | return this; |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 12889 | } |
| 12890 | |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 12891 | // Walk through to the Allocation Site |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 12892 | AllocationSite* site = memento->GetAllocationSite(); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12893 | return site->DigestTransitionFeedback(to_kind); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12894 | } |
| 12895 | |
| 12896 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12897 | MaybeObject* JSObject::TransitionElementsKind(ElementsKind to_kind) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12898 | ElementsKind from_kind = map()->elements_kind(); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12899 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12900 | if (IsFastHoleyElementsKind(from_kind)) { |
| 12901 | to_kind = GetHoleyElementsKind(to_kind); |
| 12902 | } |
| 12903 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 12904 | if (from_kind == to_kind) return this; |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 12905 | // Don't update the site if to_kind isn't fast |
| 12906 | if (IsFastElementsKind(to_kind)) { |
| 12907 | MaybeObject* maybe_failure = UpdateAllocationSite(to_kind); |
| 12908 | if (maybe_failure->IsFailure()) return maybe_failure; |
| 12909 | } |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 12910 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12911 | Isolate* isolate = GetIsolate(); |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12912 | if (elements() == isolate->heap()->empty_fixed_array() || |
| 12913 | (IsFastSmiOrObjectElementsKind(from_kind) && |
| 12914 | IsFastSmiOrObjectElementsKind(to_kind)) || |
| 12915 | (from_kind == FAST_DOUBLE_ELEMENTS && |
| 12916 | to_kind == FAST_HOLEY_DOUBLE_ELEMENTS)) { |
| 12917 | ASSERT(from_kind != TERMINAL_FAST_ELEMENTS_KIND); |
| 12918 | // No change is needed to the elements() buffer, the transition |
| 12919 | // only requires a map change. |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 12920 | MaybeObject* maybe_new_map = GetElementsTransitionMap(isolate, to_kind); |
| 12921 | Map* new_map; |
| 12922 | if (!maybe_new_map->To(&new_map)) return maybe_new_map; |
| 12923 | set_map(new_map); |
| 12924 | if (FLAG_trace_elements_transitions) { |
| 12925 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 12926 | PrintElementsTransition(stdout, from_kind, elms, to_kind, elms); |
| 12927 | } |
| 12928 | return this; |
| 12929 | } |
| 12930 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12931 | FixedArrayBase* elms = FixedArrayBase::cast(elements()); |
| 12932 | uint32_t capacity = static_cast<uint32_t>(elms->length()); |
| 12933 | uint32_t length = capacity; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12934 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12935 | if (IsJSArray()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12936 | Object* raw_length = JSArray::cast(this)->length(); |
| 12937 | if (raw_length->IsUndefined()) { |
| 12938 | // If length is undefined, then JSArray is being initialized and has no |
| 12939 | // elements, assume a length of zero. |
| 12940 | length = 0; |
| 12941 | } else { |
| 12942 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12943 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12944 | } |
| 12945 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12946 | if (IsFastSmiElementsKind(from_kind) && |
| 12947 | IsFastDoubleElementsKind(to_kind)) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12948 | MaybeObject* maybe_result = |
| 12949 | SetFastDoubleElementsCapacityAndLength(capacity, length); |
| 12950 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12951 | ValidateElements(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12952 | return this; |
| 12953 | } |
| 12954 | |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12955 | if (IsFastDoubleElementsKind(from_kind) && |
| 12956 | IsFastObjectElementsKind(to_kind)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12957 | MaybeObject* maybe_result = SetFastElementsCapacityAndLength( |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12958 | capacity, length, kDontAllowSmiElements); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12959 | if (maybe_result->IsFailure()) return maybe_result; |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12960 | ValidateElements(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12961 | return this; |
| 12962 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 12963 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12964 | // This method should never be called for any other case than the ones |
| 12965 | // handled above. |
| 12966 | UNREACHABLE(); |
| 12967 | return GetIsolate()->heap()->null_value(); |
| 12968 | } |
| 12969 | |
| 12970 | |
| 12971 | // static |
| 12972 | bool Map::IsValidElementsTransition(ElementsKind from_kind, |
| 12973 | ElementsKind to_kind) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 12974 | // Transitions can't go backwards. |
| 12975 | if (!IsMoreGeneralElementsKindTransition(from_kind, to_kind)) { |
| 12976 | return false; |
| 12977 | } |
| 12978 | |
| 12979 | // Transitions from HOLEY -> PACKED are not allowed. |
| 12980 | return !IsFastHoleyElementsKind(from_kind) || |
| 12981 | IsFastHoleyElementsKind(to_kind); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12982 | } |
| 12983 | |
| 12984 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 12985 | void JSArray::JSArrayUpdateLengthFromIndex(Handle<JSArray> array, |
| 12986 | uint32_t index, |
| 12987 | Handle<Object> value) { |
| 12988 | CALL_HEAP_FUNCTION_VOID(array->GetIsolate(), |
| 12989 | array->JSArrayUpdateLengthFromIndex(index, *value)); |
| 12990 | } |
| 12991 | |
| 12992 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12993 | MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, |
| 12994 | Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12995 | uint32_t old_len = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 12996 | CHECK(length()->ToArrayIndex(&old_len)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12997 | // Check to see if we need to update the length. For now, we make |
| 12998 | // sure that the length stays within 32-bits (unsigned). |
| 12999 | if (index >= old_len && index != 0xffffffff) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13000 | Object* len; |
| 13001 | { MaybeObject* maybe_len = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13002 | GetHeap()->NumberFromDouble(static_cast<double>(index) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13003 | if (!maybe_len->ToObject(&len)) return maybe_len; |
| 13004 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13005 | set_length(len); |
| 13006 | } |
| 13007 | return value; |
| 13008 | } |
| 13009 | |
| 13010 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 13011 | MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13012 | uint32_t index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13013 | Isolate* isolate = GetIsolate(); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 13014 | HandleScope scope(isolate); |
| 13015 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13016 | // Make sure that the top context does not change when doing |
| 13017 | // callbacks or interceptor calls. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 13018 | AssertNoContextChange ncc(isolate); |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 13019 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13020 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate); |
| 13021 | Handle<Object> this_handle(receiver, isolate); |
| 13022 | Handle<JSObject> holder_handle(this, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13023 | if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 13024 | v8::IndexedPropertyGetterCallback getter = |
| 13025 | v8::ToCData<v8::IndexedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13026 | LOG(isolate, |
| 13027 | ApiIndexedPropertyAccess("interceptor-indexed-get", this, index)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 13028 | PropertyCallbackArguments |
| 13029 | args(isolate, interceptor->data(), receiver, this); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13030 | v8::Handle<v8::Value> result = args.Call(getter, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13031 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 13032 | if (!result.IsEmpty()) { |
| 13033 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 13034 | result_internal->VerifyApiCallResultType(); |
| 13035 | return *result_internal; |
| 13036 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13037 | } |
| 13038 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13039 | Heap* heap = holder_handle->GetHeap(); |
| 13040 | ElementsAccessor* handler = holder_handle->GetElementsAccessor(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 13041 | MaybeObject* raw_result = handler->Get(*this_handle, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 13042 | *holder_handle, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 13043 | index); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13044 | if (raw_result != heap->the_hole_value()) return raw_result; |
| 13045 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13046 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13047 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13048 | Object* pt = holder_handle->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13049 | if (pt == heap->null_value()) return heap->undefined_value(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 13050 | return pt->GetElementWithReceiver(isolate, *this_handle, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13051 | } |
| 13052 | |
| 13053 | |
| 13054 | bool JSObject::HasDenseElements() { |
| 13055 | int capacity = 0; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13056 | int used = 0; |
| 13057 | GetElementsCapacityAndUsage(&capacity, &used); |
| 13058 | return (capacity == 0) || (used > (capacity / 2)); |
| 13059 | } |
| 13060 | |
| 13061 | |
| 13062 | void JSObject::GetElementsCapacityAndUsage(int* capacity, int* used) { |
| 13063 | *capacity = 0; |
| 13064 | *used = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13065 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 13066 | FixedArrayBase* backing_store_base = FixedArrayBase::cast(elements()); |
| 13067 | FixedArray* backing_store = NULL; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13068 | switch (GetElementsKind()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13069 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 13070 | backing_store_base = |
| 13071 | FixedArray::cast(FixedArray::cast(backing_store_base)->get(1)); |
| 13072 | backing_store = FixedArray::cast(backing_store_base); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13073 | if (backing_store->IsDictionary()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13074 | SeededNumberDictionary* dictionary = |
| 13075 | SeededNumberDictionary::cast(backing_store); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13076 | *capacity = dictionary->Capacity(); |
| 13077 | *used = dictionary->NumberOfElements(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13078 | break; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13079 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13080 | // Fall through. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13081 | case FAST_SMI_ELEMENTS: |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13082 | case FAST_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 13083 | if (IsJSArray()) { |
| 13084 | *capacity = backing_store_base->length(); |
| 13085 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 13086 | break; |
| 13087 | } |
| 13088 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13089 | case FAST_HOLEY_SMI_ELEMENTS: |
| 13090 | case FAST_HOLEY_ELEMENTS: |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 13091 | backing_store = FixedArray::cast(backing_store_base); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13092 | *capacity = backing_store->length(); |
| 13093 | for (int i = 0; i < *capacity; ++i) { |
| 13094 | if (!backing_store->get(i)->IsTheHole()) ++(*used); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13095 | } |
| 13096 | break; |
| 13097 | case DICTIONARY_ELEMENTS: { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 13098 | SeededNumberDictionary* dictionary = element_dictionary(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13099 | *capacity = dictionary->Capacity(); |
| 13100 | *used = dictionary->NumberOfElements(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13101 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13102 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13103 | case FAST_DOUBLE_ELEMENTS: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 13104 | if (IsJSArray()) { |
| 13105 | *capacity = backing_store_base->length(); |
| 13106 | *used = Smi::cast(JSArray::cast(this)->length())->value(); |
| 13107 | break; |
| 13108 | } |
| 13109 | // Fall through if packing is not guaranteed. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13110 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13111 | FixedDoubleArray* elms = FixedDoubleArray::cast(elements()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13112 | *capacity = elms->length(); |
| 13113 | for (int i = 0; i < *capacity; i++) { |
| 13114 | if (!elms->is_the_hole(i)) ++(*used); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13115 | } |
| 13116 | break; |
| 13117 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13118 | case EXTERNAL_BYTE_ELEMENTS: |
| 13119 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 13120 | case EXTERNAL_SHORT_ELEMENTS: |
| 13121 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 13122 | case EXTERNAL_INT_ELEMENTS: |
| 13123 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13124 | case EXTERNAL_FLOAT_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13125 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 13126 | case EXTERNAL_PIXEL_ELEMENTS: |
| 13127 | // External arrays are considered 100% used. |
| 13128 | ExternalArray* external_array = ExternalArray::cast(elements()); |
| 13129 | *capacity = external_array->length(); |
| 13130 | *used = external_array->length(); |
| 13131 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13132 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13133 | } |
| 13134 | |
| 13135 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13136 | bool JSObject::ShouldConvertToSlowElements(int new_capacity) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13137 | STATIC_ASSERT(kMaxUncheckedOldFastElementsLength <= |
| 13138 | kMaxUncheckedFastElementsLength); |
| 13139 | if (new_capacity <= kMaxUncheckedOldFastElementsLength || |
| 13140 | (new_capacity <= kMaxUncheckedFastElementsLength && |
| 13141 | GetHeap()->InNewSpace(this))) { |
| 13142 | return false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13143 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13144 | // If the fast-case backing storage takes up roughly three times as |
| 13145 | // much space (in machine words) as a dictionary backing storage |
| 13146 | // would, the object should have slow elements. |
| 13147 | int old_capacity = 0; |
| 13148 | int used_elements = 0; |
| 13149 | GetElementsCapacityAndUsage(&old_capacity, &used_elements); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13150 | int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) * |
| 13151 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13152 | return 3 * dictionary_size <= new_capacity; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13153 | } |
| 13154 | |
| 13155 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13156 | bool JSObject::ShouldConvertToFastElements() { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13157 | ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13158 | // If the elements are sparse, we should not go back to fast case. |
| 13159 | if (!HasDenseElements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13160 | // An object requiring access checks is never allowed to have fast |
| 13161 | // elements. If it had fast elements we would skip security checks. |
| 13162 | if (IsAccessCheckNeeded()) return false; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13163 | // Observed objects may not go to fast mode because they rely on map checks, |
| 13164 | // and for fast element accesses we sometimes check element kinds only. |
| 13165 | if (FLAG_harmony_observation && map()->is_observed()) return false; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13166 | |
| 13167 | FixedArray* elements = FixedArray::cast(this->elements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13168 | SeededNumberDictionary* dictionary = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13169 | if (elements->map() == GetHeap()->non_strict_arguments_elements_map()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13170 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13171 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13172 | dictionary = SeededNumberDictionary::cast(elements); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13173 | } |
| 13174 | // If an element has been added at a very high index in the elements |
| 13175 | // dictionary, we cannot go back to fast case. |
| 13176 | if (dictionary->requires_slow_elements()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13177 | // If the dictionary backing storage takes up roughly half as much |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13178 | // space (in machine words) as a fast-case backing storage would, |
| 13179 | // the object should have fast elements. |
| 13180 | uint32_t array_size = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13181 | if (IsJSArray()) { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13182 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13183 | } else { |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13184 | array_size = dictionary->max_number_key(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13185 | } |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13186 | uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) * |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13187 | SeededNumberDictionary::kEntrySize; |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 13188 | return 2 * dictionary_size >= array_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13189 | } |
| 13190 | |
| 13191 | |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13192 | bool JSObject::ShouldConvertToFastDoubleElements( |
| 13193 | bool* has_smi_only_elements) { |
| 13194 | *has_smi_only_elements = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13195 | if (FLAG_unbox_double_arrays) { |
| 13196 | ASSERT(HasDictionaryElements()); |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 13197 | SeededNumberDictionary* dictionary = element_dictionary(); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13198 | bool found_double = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13199 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 13200 | Object* key = dictionary->KeyAt(i); |
| 13201 | if (key->IsNumber()) { |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13202 | Object* value = dictionary->ValueAt(i); |
| 13203 | if (!value->IsNumber()) return false; |
| 13204 | if (!value->IsSmi()) { |
| 13205 | found_double = true; |
| 13206 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13207 | } |
| 13208 | } |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 13209 | *has_smi_only_elements = !found_double; |
| 13210 | return found_double; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13211 | } else { |
| 13212 | return false; |
| 13213 | } |
| 13214 | } |
| 13215 | |
| 13216 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13217 | // Certain compilers request function template instantiation when they |
| 13218 | // see the definition of the other template functions in the |
| 13219 | // class. This requires us to have the template functions put |
| 13220 | // together, so even though this function belongs in objects-debug.cc, |
| 13221 | // we keep it here instead to satisfy certain compilers. |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13222 | #ifdef OBJECT_PRINT |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13223 | template<typename Shape, typename Key> |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13224 | void Dictionary<Shape, Key>::Print(FILE* out) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13225 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 13226 | for (int i = 0; i < capacity; i++) { |
| 13227 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 13228 | if (HashTable<Shape, Key>::IsKey(k)) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13229 | PrintF(out, " "); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13230 | if (k->IsString()) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13231 | String::cast(k)->StringPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13232 | } else { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13233 | k->ShortPrint(out); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13234 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 13235 | PrintF(out, ": "); |
| 13236 | ValueAt(i)->ShortPrint(out); |
| 13237 | PrintF(out, "\n"); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13238 | } |
| 13239 | } |
| 13240 | } |
| 13241 | #endif |
| 13242 | |
| 13243 | |
| 13244 | template<typename Shape, typename Key> |
| 13245 | void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13246 | int pos = 0; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13247 | int capacity = HashTable<Shape, Key>::Capacity(); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 13248 | DisallowHeapAllocation no_gc; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13249 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13250 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13251 | Object* k = Dictionary<Shape, Key>::KeyAt(i); |
| 13252 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 13253 | elements->set(pos++, ValueAt(i), mode); |
| 13254 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13255 | } |
| 13256 | ASSERT(pos == elements->length()); |
| 13257 | } |
| 13258 | |
| 13259 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13260 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
| 13261 | ASSERT(map()->has_named_interceptor()); |
| 13262 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13263 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13264 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13265 | constructor->shared()->get_api_func_data()->named_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13266 | return InterceptorInfo::cast(result); |
| 13267 | } |
| 13268 | |
| 13269 | |
| 13270 | InterceptorInfo* JSObject::GetIndexedInterceptor() { |
| 13271 | ASSERT(map()->has_indexed_interceptor()); |
| 13272 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13273 | ASSERT(constructor->shared()->IsApiFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13274 | Object* result = |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13275 | constructor->shared()->get_api_func_data()->indexed_property_handler(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13276 | return InterceptorInfo::cast(result); |
| 13277 | } |
| 13278 | |
| 13279 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13280 | Handle<Object> JSObject::GetPropertyPostInterceptor( |
| 13281 | Handle<JSObject> object, |
| 13282 | Handle<Object> receiver, |
| 13283 | Handle<Name> name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13284 | PropertyAttributes* attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13285 | // Check local property in holder, ignore interceptor. |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13286 | Isolate* isolate = object->GetIsolate(); |
| 13287 | LookupResult lookup(isolate); |
| 13288 | object->LocalLookupRealNamedProperty(*name, &lookup); |
| 13289 | Handle<Object> result; |
| 13290 | if (lookup.IsFound()) { |
| 13291 | result = GetProperty(object, receiver, &lookup, name, attributes); |
| 13292 | } else { |
| 13293 | // Continue searching via the prototype chain. |
| 13294 | Handle<Object> prototype(object->GetPrototype(), isolate); |
| 13295 | *attributes = ABSENT; |
| 13296 | if (prototype->IsNull()) return isolate->factory()->undefined_value(); |
| 13297 | result = GetPropertyWithReceiver(prototype, receiver, name, attributes); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 13298 | } |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13299 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13300 | } |
| 13301 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13302 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13303 | MaybeObject* JSObject::GetLocalPropertyPostInterceptor( |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13304 | Object* receiver, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13305 | Name* name, |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13306 | PropertyAttributes* attributes) { |
| 13307 | // Check local property in holder, ignore interceptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13308 | LookupResult result(GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13309 | LocalLookupRealNamedProperty(name, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13310 | if (result.IsFound()) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 13311 | return GetProperty(receiver, &result, name, attributes); |
| 13312 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13313 | return GetHeap()->undefined_value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 13314 | } |
| 13315 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13316 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13317 | Handle<Object> JSObject::GetPropertyWithInterceptor( |
| 13318 | Handle<JSObject> object, |
| 13319 | Handle<Object> receiver, |
| 13320 | Handle<Name> name, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 13321 | PropertyAttributes* attributes) { |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13322 | Isolate* isolate = object->GetIsolate(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13323 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13324 | // TODO(rossberg): Support symbols in the API. |
| 13325 | if (name->IsSymbol()) return isolate->factory()->undefined_value(); |
| 13326 | |
| 13327 | Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor(), isolate); |
| 13328 | Handle<String> name_string = Handle<String>::cast(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13329 | |
| 13330 | if (!interceptor->getter()->IsUndefined()) { |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 13331 | v8::NamedPropertyGetterCallback getter = |
| 13332 | v8::ToCData<v8::NamedPropertyGetterCallback>(interceptor->getter()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13333 | LOG(isolate, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13334 | ApiNamedPropertyAccess("interceptor-named-get", *object, *name)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 13335 | PropertyCallbackArguments |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13336 | args(isolate, interceptor->data(), *receiver, *object); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13337 | v8::Handle<v8::Value> result = |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13338 | args.Call(getter, v8::Utils::ToLocal(name_string)); |
| 13339 | RETURN_HANDLE_IF_SCHEDULED_EXCEPTION(isolate, Object); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13340 | if (!result.IsEmpty()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13341 | *attributes = NONE; |
mstarzinger@chromium.org | de88679 | 2012-09-11 13:22:37 +0000 | [diff] [blame] | 13342 | Handle<Object> result_internal = v8::Utils::OpenHandle(*result); |
| 13343 | result_internal->VerifyApiCallResultType(); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13344 | // Rebox handle to escape this scope. |
| 13345 | return handle(*result_internal, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13346 | } |
| 13347 | } |
| 13348 | |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 13349 | return GetPropertyPostInterceptor(object, receiver, name, attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13350 | } |
| 13351 | |
| 13352 | |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13353 | bool JSObject::HasRealNamedProperty(Handle<JSObject> object, |
| 13354 | Handle<Name> key) { |
| 13355 | Isolate* isolate = object->GetIsolate(); |
| 13356 | SealHandleScope shs(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13357 | // Check access rights if needed. |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13358 | if (object->IsAccessCheckNeeded()) { |
| 13359 | if (!isolate->MayNamedAccess(*object, *key, v8::ACCESS_HAS)) { |
| 13360 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13361 | return false; |
| 13362 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13363 | } |
| 13364 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13365 | LookupResult result(isolate); |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13366 | object->LocalLookupRealNamedProperty(*key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13367 | return result.IsFound() && !result.IsInterceptor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13368 | } |
| 13369 | |
| 13370 | |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13371 | bool JSObject::HasRealElementProperty(Handle<JSObject> object, uint32_t index) { |
| 13372 | Isolate* isolate = object->GetIsolate(); |
| 13373 | SealHandleScope shs(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13374 | // Check access rights if needed. |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13375 | if (object->IsAccessCheckNeeded()) { |
| 13376 | if (!isolate->MayIndexedAccess(*object, index, v8::ACCESS_HAS)) { |
| 13377 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13378 | return false; |
| 13379 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13380 | } |
| 13381 | |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13382 | if (object->IsJSGlobalProxy()) { |
| 13383 | HandleScope scope(isolate); |
| 13384 | Handle<Object> proto(object->GetPrototype(), isolate); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 13385 | if (proto->IsNull()) return false; |
| 13386 | ASSERT(proto->IsJSGlobalObject()); |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13387 | return HasRealElementProperty(Handle<JSObject>::cast(proto), index); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 13388 | } |
| 13389 | |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13390 | return object->GetElementAttributeWithoutInterceptor( |
| 13391 | *object, index, false) != ABSENT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13392 | } |
| 13393 | |
| 13394 | |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13395 | bool JSObject::HasRealNamedCallbackProperty(Handle<JSObject> object, |
| 13396 | Handle<Name> key) { |
| 13397 | Isolate* isolate = object->GetIsolate(); |
| 13398 | SealHandleScope shs(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13399 | // Check access rights if needed. |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13400 | if (object->IsAccessCheckNeeded()) { |
| 13401 | if (!isolate->MayNamedAccess(*object, *key, v8::ACCESS_HAS)) { |
| 13402 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_HAS); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13403 | return false; |
| 13404 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13405 | } |
| 13406 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13407 | LookupResult result(isolate); |
bmeurer@chromium.org | 0fdb2a6 | 2013-10-21 07:19:36 +0000 | [diff] [blame] | 13408 | object->LocalLookupRealNamedProperty(*key, &result); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13409 | return result.IsPropertyCallbacks(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13410 | } |
| 13411 | |
| 13412 | |
| 13413 | int JSObject::NumberOfLocalProperties(PropertyAttributes filter) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13414 | if (HasFastProperties()) { |
| 13415 | Map* map = this->map(); |
| 13416 | if (filter == NONE) return map->NumberOfOwnDescriptors(); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13417 | if (filter & DONT_ENUM) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13418 | int result = map->EnumLength(); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 13419 | if (result != kInvalidEnumCacheSentinel) return result; |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 13420 | } |
| 13421 | return map->NumberOfDescribedProperties(OWN_DESCRIPTORS, filter); |
| 13422 | } |
| 13423 | return property_dictionary()->NumberOfElementsFilterAttributes(filter); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13424 | } |
| 13425 | |
| 13426 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13427 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13428 | Object* temp = get(i); |
| 13429 | set(i, get(j)); |
| 13430 | set(j, temp); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13431 | if (this != numbers) { |
| 13432 | temp = numbers->get(i); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 13433 | numbers->set(i, Smi::cast(numbers->get(j))); |
| 13434 | numbers->set(j, Smi::cast(temp)); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13435 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13436 | } |
| 13437 | |
| 13438 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13439 | static void InsertionSortPairs(FixedArray* content, |
| 13440 | FixedArray* numbers, |
| 13441 | int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13442 | for (int i = 1; i < len; i++) { |
| 13443 | int j = i; |
| 13444 | while (j > 0 && |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13445 | (NumberToUint32(numbers->get(j - 1)) > |
| 13446 | NumberToUint32(numbers->get(j)))) { |
| 13447 | content->SwapPairs(numbers, j - 1, j); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13448 | j--; |
| 13449 | } |
| 13450 | } |
| 13451 | } |
| 13452 | |
| 13453 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13454 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13455 | // In-place heap sort. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13456 | ASSERT(content->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13457 | |
| 13458 | // Bottom-up max-heap construction. |
| 13459 | for (int i = 1; i < len; ++i) { |
| 13460 | int child_index = i; |
| 13461 | while (child_index > 0) { |
| 13462 | int parent_index = ((child_index + 1) >> 1) - 1; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13463 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 13464 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13465 | if (parent_value < child_value) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13466 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13467 | } else { |
| 13468 | break; |
| 13469 | } |
| 13470 | child_index = parent_index; |
| 13471 | } |
| 13472 | } |
| 13473 | |
| 13474 | // Extract elements and create sorted array. |
| 13475 | for (int i = len - 1; i > 0; --i) { |
| 13476 | // Put max element at the back of the array. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13477 | content->SwapPairs(numbers, 0, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13478 | // Sift down the new top element. |
| 13479 | int parent_index = 0; |
| 13480 | while (true) { |
| 13481 | int child_index = ((parent_index + 1) << 1) - 1; |
| 13482 | if (child_index >= i) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13483 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 13484 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 13485 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13486 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 13487 | if (parent_value > child1_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13488 | content->SwapPairs(numbers, parent_index, child_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13489 | parent_index = child_index; |
| 13490 | } else { |
| 13491 | if (parent_value > child2_value) break; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13492 | content->SwapPairs(numbers, parent_index, child_index + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13493 | parent_index = child_index + 1; |
| 13494 | } |
| 13495 | } |
| 13496 | } |
| 13497 | } |
| 13498 | |
| 13499 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13500 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 13501 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
| 13502 | ASSERT(this->length() == numbers->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13503 | // For small arrays, simply use insertion sort. |
| 13504 | if (len <= 10) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13505 | InsertionSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13506 | return; |
| 13507 | } |
| 13508 | // Check the range of indices. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13509 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 13510 | uint32_t max_index = min_index; |
| 13511 | uint32_t i; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13512 | for (i = 1; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13513 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 13514 | min_index = NumberToUint32(numbers->get(i)); |
| 13515 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 13516 | max_index = NumberToUint32(numbers->get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13517 | } |
| 13518 | } |
| 13519 | if (max_index - min_index + 1 == len) { |
| 13520 | // Indices form a contiguous range, unless there are duplicates. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13521 | // Do an in-place linear time sort assuming distinct numbers, but |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13522 | // avoid hanging in case they are not. |
| 13523 | for (i = 0; i < len; i++) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13524 | uint32_t p; |
| 13525 | uint32_t j = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13526 | // While the current element at i is not at its correct position p, |
| 13527 | // swap the elements at these two positions. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13528 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13529 | j++ < len) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13530 | SwapPairs(numbers, i, p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13531 | } |
| 13532 | } |
| 13533 | } else { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13534 | HeapSortPairs(this, numbers, len); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13535 | return; |
| 13536 | } |
| 13537 | } |
| 13538 | |
| 13539 | |
| 13540 | // Fill in the names of local properties into the supplied storage. The main |
| 13541 | // purpose of this function is to provide reflection information for the object |
| 13542 | // mirrors. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13543 | void JSObject::GetLocalPropertyNames( |
| 13544 | FixedArray* storage, int index, PropertyAttributes filter) { |
| 13545 | ASSERT(storage->length() >= (NumberOfLocalProperties(filter) - index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13546 | if (HasFastProperties()) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 13547 | int real_size = map()->NumberOfOwnDescriptors(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 13548 | DescriptorArray* descs = map()->instance_descriptors(); |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 13549 | for (int i = 0; i < real_size; i++) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13550 | if ((descs->GetDetails(i).attributes() & filter) == 0 && |
| 13551 | ((filter & SYMBOLIC) == 0 || !descs->GetKey(i)->IsSymbol())) { |
| 13552 | storage->set(index++, descs->GetKey(i)); |
| 13553 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13554 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13555 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13556 | property_dictionary()->CopyKeysTo(storage, |
| 13557 | index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 13558 | filter, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 13559 | NameDictionary::UNSORTED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13560 | } |
| 13561 | } |
| 13562 | |
| 13563 | |
| 13564 | int JSObject::NumberOfLocalElements(PropertyAttributes filter) { |
| 13565 | return GetLocalElementKeys(NULL, filter); |
| 13566 | } |
| 13567 | |
| 13568 | |
| 13569 | int JSObject::NumberOfEnumElements() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13570 | // Fast case for objects with no elements. |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13571 | if (!IsJSValue() && HasFastObjectElements()) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13572 | uint32_t length = IsJSArray() ? |
| 13573 | static_cast<uint32_t>( |
| 13574 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 13575 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 13576 | if (length == 0) return 0; |
| 13577 | } |
| 13578 | // Compute the number of enumerable elements. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13579 | return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 13580 | } |
| 13581 | |
| 13582 | |
| 13583 | int JSObject::GetLocalElementKeys(FixedArray* storage, |
| 13584 | PropertyAttributes filter) { |
| 13585 | int counter = 0; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13586 | switch (GetElementsKind()) { |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13587 | case FAST_SMI_ELEMENTS: |
| 13588 | case FAST_ELEMENTS: |
| 13589 | case FAST_HOLEY_SMI_ELEMENTS: |
| 13590 | case FAST_HOLEY_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13591 | int length = IsJSArray() ? |
| 13592 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 13593 | FixedArray::cast(elements())->length(); |
| 13594 | for (int i = 0; i < length; i++) { |
| 13595 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 13596 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13597 | storage->set(counter, Smi::FromInt(i)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13598 | } |
| 13599 | counter++; |
| 13600 | } |
| 13601 | } |
| 13602 | ASSERT(!storage || storage->length() >= counter); |
| 13603 | break; |
| 13604 | } |
svenpanne@chromium.org | 830d30c | 2012-05-29 13:20:14 +0000 | [diff] [blame] | 13605 | case FAST_DOUBLE_ELEMENTS: |
| 13606 | case FAST_HOLEY_DOUBLE_ELEMENTS: { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 13607 | int length = IsJSArray() ? |
| 13608 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 13609 | FixedDoubleArray::cast(elements())->length(); |
| 13610 | for (int i = 0; i < length; i++) { |
| 13611 | if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) { |
| 13612 | if (storage != NULL) { |
| 13613 | storage->set(counter, Smi::FromInt(i)); |
| 13614 | } |
| 13615 | counter++; |
| 13616 | } |
| 13617 | } |
| 13618 | ASSERT(!storage || storage->length() >= counter); |
| 13619 | break; |
| 13620 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 13621 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 13622 | int length = ExternalPixelArray::cast(elements())->length(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13623 | while (counter < length) { |
| 13624 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13625 | storage->set(counter, Smi::FromInt(counter)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13626 | } |
| 13627 | counter++; |
| 13628 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13629 | ASSERT(!storage || storage->length() >= counter); |
| 13630 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13631 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13632 | case EXTERNAL_BYTE_ELEMENTS: |
| 13633 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 13634 | case EXTERNAL_SHORT_ELEMENTS: |
| 13635 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 13636 | case EXTERNAL_INT_ELEMENTS: |
| 13637 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 13638 | case EXTERNAL_FLOAT_ELEMENTS: |
| 13639 | case EXTERNAL_DOUBLE_ELEMENTS: { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13640 | int length = ExternalArray::cast(elements())->length(); |
| 13641 | while (counter < length) { |
| 13642 | if (storage != NULL) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13643 | storage->set(counter, Smi::FromInt(counter)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13644 | } |
| 13645 | counter++; |
| 13646 | } |
| 13647 | ASSERT(!storage || storage->length() >= counter); |
| 13648 | break; |
| 13649 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13650 | case DICTIONARY_ELEMENTS: { |
| 13651 | if (storage != NULL) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13652 | element_dictionary()->CopyKeysTo(storage, |
| 13653 | filter, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13654 | SeededNumberDictionary::SORTED); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13655 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13656 | counter += element_dictionary()->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13657 | break; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13658 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13659 | case NON_STRICT_ARGUMENTS_ELEMENTS: { |
| 13660 | FixedArray* parameter_map = FixedArray::cast(elements()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13661 | int mapped_length = parameter_map->length() - 2; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13662 | FixedArray* arguments = FixedArray::cast(parameter_map->get(1)); |
| 13663 | if (arguments->IsDictionary()) { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13664 | // Copy the keys from arguments first, because Dictionary::CopyKeysTo |
| 13665 | // will insert in storage starting at index 0. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13666 | SeededNumberDictionary* dictionary = |
| 13667 | SeededNumberDictionary::cast(arguments); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13668 | if (storage != NULL) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 13669 | dictionary->CopyKeysTo( |
| 13670 | storage, filter, SeededNumberDictionary::UNSORTED); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13671 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13672 | counter += dictionary->NumberOfElementsFilterAttributes(filter); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13673 | for (int i = 0; i < mapped_length; ++i) { |
| 13674 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 13675 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13676 | ++counter; |
| 13677 | } |
| 13678 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 13679 | if (storage != NULL) storage->SortPairs(storage, counter); |
| 13680 | |
| 13681 | } else { |
| 13682 | int backing_length = arguments->length(); |
| 13683 | int i = 0; |
| 13684 | for (; i < mapped_length; ++i) { |
| 13685 | if (!parameter_map->get(i + 2)->IsTheHole()) { |
| 13686 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13687 | ++counter; |
| 13688 | } else if (i < backing_length && !arguments->get(i)->IsTheHole()) { |
| 13689 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13690 | ++counter; |
| 13691 | } |
| 13692 | } |
| 13693 | for (; i < backing_length; ++i) { |
| 13694 | if (storage != NULL) storage->set(counter, Smi::FromInt(i)); |
| 13695 | ++counter; |
| 13696 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13697 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13698 | break; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 13699 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13700 | } |
| 13701 | |
| 13702 | if (this->IsJSValue()) { |
| 13703 | Object* val = JSValue::cast(this)->value(); |
| 13704 | if (val->IsString()) { |
| 13705 | String* str = String::cast(val); |
| 13706 | if (storage) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13707 | for (int i = 0; i < str->length(); i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13708 | storage->set(counter + i, Smi::FromInt(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13709 | } |
| 13710 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13711 | counter += str->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13712 | } |
| 13713 | } |
| 13714 | ASSERT(!storage || storage->length() == counter); |
| 13715 | return counter; |
| 13716 | } |
| 13717 | |
| 13718 | |
| 13719 | int JSObject::GetEnumElementKeys(FixedArray* storage) { |
| 13720 | return GetLocalElementKeys(storage, |
| 13721 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 13722 | } |
| 13723 | |
| 13724 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13725 | // StringKey simply carries a string object as key. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13726 | class StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13727 | public: |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13728 | explicit StringKey(String* string) : |
| 13729 | string_(string), |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13730 | hash_(HashForObject(string)) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13731 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13732 | bool IsMatch(Object* string) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13733 | // We know that all entries in a hash table had their hash keys created. |
| 13734 | // Use that knowledge to have fast failure. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13735 | if (hash_ != HashForObject(string)) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13736 | return false; |
| 13737 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13738 | return string_->Equals(String::cast(string)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13739 | } |
| 13740 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13741 | uint32_t Hash() { return hash_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13742 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13743 | uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13744 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13745 | Object* AsObject(Heap* heap) { return string_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13746 | |
| 13747 | String* string_; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13748 | uint32_t hash_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13749 | }; |
| 13750 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13751 | |
| 13752 | // StringSharedKeys are used as keys in the eval cache. |
| 13753 | class StringSharedKey : public HashTableKey { |
| 13754 | public: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13755 | StringSharedKey(String* source, |
| 13756 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13757 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13758 | int scope_position) |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13759 | : source_(source), |
| 13760 | shared_(shared), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13761 | language_mode_(language_mode), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13762 | scope_position_(scope_position) { } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13763 | |
| 13764 | bool IsMatch(Object* other) { |
| 13765 | if (!other->IsFixedArray()) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13766 | FixedArray* other_array = FixedArray::cast(other); |
| 13767 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13768 | if (shared != shared_) return false; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13769 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 13770 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 13771 | language_unchecked == STRICT_MODE || |
| 13772 | language_unchecked == EXTENDED_MODE); |
| 13773 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
| 13774 | if (language_mode != language_mode_) return false; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13775 | int scope_position = Smi::cast(other_array->get(3))->value(); |
| 13776 | if (scope_position != scope_position_) return false; |
| 13777 | String* source = String::cast(other_array->get(1)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13778 | return source->Equals(source_); |
| 13779 | } |
| 13780 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13781 | static uint32_t StringSharedHashHelper(String* source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 13782 | SharedFunctionInfo* shared, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13783 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13784 | int scope_position) { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13785 | uint32_t hash = source->Hash(); |
| 13786 | if (shared->HasSourceCode()) { |
| 13787 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 13788 | // code computation, we use a combination of the hash of the |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13789 | // script source code and the start position of the calling scope. |
| 13790 | // 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] | 13791 | // collection. |
| 13792 | Script* script = Script::cast(shared->script()); |
| 13793 | hash ^= String::cast(script->source())->Hash(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13794 | if (language_mode == STRICT_MODE) hash ^= 0x8000; |
| 13795 | if (language_mode == EXTENDED_MODE) hash ^= 0x0080; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13796 | hash += scope_position; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13797 | } |
| 13798 | return hash; |
| 13799 | } |
| 13800 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13801 | uint32_t Hash() { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13802 | return StringSharedHashHelper( |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13803 | source_, shared_, language_mode_, scope_position_); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13804 | } |
| 13805 | |
| 13806 | uint32_t HashForObject(Object* obj) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13807 | FixedArray* other_array = FixedArray::cast(obj); |
| 13808 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0)); |
| 13809 | String* source = String::cast(other_array->get(1)); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13810 | int language_unchecked = Smi::cast(other_array->get(2))->value(); |
| 13811 | ASSERT(language_unchecked == CLASSIC_MODE || |
| 13812 | language_unchecked == STRICT_MODE || |
| 13813 | language_unchecked == EXTENDED_MODE); |
| 13814 | LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13815 | int scope_position = Smi::cast(other_array->get(3))->value(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13816 | return StringSharedHashHelper( |
| 13817 | source, shared, language_mode, scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13818 | } |
| 13819 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13820 | MUST_USE_RESULT MaybeObject* AsObject(Heap* heap) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13821 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13822 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13823 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 13824 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13825 | FixedArray* other_array = FixedArray::cast(obj); |
| 13826 | other_array->set(0, shared_); |
| 13827 | other_array->set(1, source_); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13828 | other_array->set(2, Smi::FromInt(language_mode_)); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13829 | other_array->set(3, Smi::FromInt(scope_position_)); |
| 13830 | return other_array; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13831 | } |
| 13832 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13833 | private: |
| 13834 | String* source_; |
| 13835 | SharedFunctionInfo* shared_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 13836 | LanguageMode language_mode_; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 13837 | int scope_position_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13838 | }; |
| 13839 | |
| 13840 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13841 | // RegExpKey carries the source and flags of a regular expression as key. |
| 13842 | class RegExpKey : public HashTableKey { |
| 13843 | public: |
| 13844 | RegExpKey(String* string, JSRegExp::Flags flags) |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 13845 | : string_(string), |
| 13846 | flags_(Smi::FromInt(flags.value())) { } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13847 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 13848 | // Rather than storing the key in the hash table, a pointer to the |
| 13849 | // stored value is stored where the key should be. IsMatch then |
| 13850 | // compares the search key to the found object, rather than comparing |
| 13851 | // a key to a key. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13852 | bool IsMatch(Object* obj) { |
| 13853 | FixedArray* val = FixedArray::cast(obj); |
| 13854 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 13855 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 13856 | } |
| 13857 | |
| 13858 | uint32_t Hash() { return RegExpHash(string_, flags_); } |
| 13859 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13860 | Object* AsObject(Heap* heap) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13861 | // Plain hash maps, which is where regexp keys are used, don't |
| 13862 | // use this function. |
| 13863 | UNREACHABLE(); |
| 13864 | return NULL; |
| 13865 | } |
| 13866 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13867 | uint32_t HashForObject(Object* obj) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13868 | FixedArray* val = FixedArray::cast(obj); |
| 13869 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 13870 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 13871 | } |
| 13872 | |
| 13873 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 13874 | return string->Hash() + flags->value(); |
| 13875 | } |
| 13876 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 13877 | String* string_; |
| 13878 | Smi* flags_; |
| 13879 | }; |
| 13880 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 13881 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13882 | // Utf8StringKey carries a vector of chars as key. |
| 13883 | class Utf8StringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13884 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13885 | explicit Utf8StringKey(Vector<const char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13886 | : string_(string), hash_field_(0), seed_(seed) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13887 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13888 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13889 | return String::cast(string)->IsUtf8EqualTo(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13890 | } |
| 13891 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13892 | uint32_t Hash() { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13893 | if (hash_field_ != 0) return hash_field_ >> String::kHashShift; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13894 | hash_field_ = StringHasher::ComputeUtf8Hash(string_, seed_, &chars_); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13895 | uint32_t result = hash_field_ >> String::kHashShift; |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 13896 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13897 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13898 | } |
| 13899 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13900 | uint32_t HashForObject(Object* other) { |
| 13901 | return String::cast(other)->Hash(); |
| 13902 | } |
| 13903 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13904 | MaybeObject* AsObject(Heap* heap) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13905 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13906 | return heap->AllocateInternalizedStringFromUtf8(string_, |
| 13907 | chars_, |
| 13908 | hash_field_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13909 | } |
| 13910 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13911 | Vector<const char> string_; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13912 | uint32_t hash_field_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13913 | 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] | 13914 | uint32_t seed_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13915 | }; |
| 13916 | |
| 13917 | |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13918 | template <typename Char> |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13919 | class SequentialStringKey : public HashTableKey { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13920 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13921 | explicit SequentialStringKey(Vector<const Char> string, uint32_t seed) |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13922 | : string_(string), hash_field_(0), seed_(seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13923 | |
| 13924 | uint32_t Hash() { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13925 | hash_field_ = StringHasher::HashSequentialString<Char>(string_.start(), |
| 13926 | string_.length(), |
| 13927 | seed_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13928 | |
| 13929 | uint32_t result = hash_field_ >> String::kHashShift; |
| 13930 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13931 | return result; |
| 13932 | } |
| 13933 | |
| 13934 | |
| 13935 | uint32_t HashForObject(Object* other) { |
| 13936 | return String::cast(other)->Hash(); |
| 13937 | } |
| 13938 | |
| 13939 | Vector<const Char> string_; |
| 13940 | uint32_t hash_field_; |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 13941 | uint32_t seed_; |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13942 | }; |
| 13943 | |
| 13944 | |
| 13945 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13946 | class OneByteStringKey : public SequentialStringKey<uint8_t> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13947 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13948 | OneByteStringKey(Vector<const uint8_t> str, uint32_t seed) |
| 13949 | : SequentialStringKey<uint8_t>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13950 | |
| 13951 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13952 | return String::cast(string)->IsOneByteEqualTo(string_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13953 | } |
| 13954 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13955 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13956 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13957 | return heap->AllocateOneByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 13958 | } |
| 13959 | }; |
| 13960 | |
| 13961 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13962 | class SubStringOneByteStringKey : public HashTableKey { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13963 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 13964 | explicit SubStringOneByteStringKey(Handle<SeqOneByteString> string, |
| 13965 | int from, |
| 13966 | int length) |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 13967 | : string_(string), from_(from), length_(length) { } |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13968 | |
| 13969 | uint32_t Hash() { |
| 13970 | ASSERT(length_ >= 0); |
| 13971 | ASSERT(from_ + length_ <= string_->length()); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13972 | uint8_t* chars = string_->GetChars() + from_; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 13973 | hash_field_ = StringHasher::HashSequentialString( |
| 13974 | chars, length_, string_->GetHeap()->HashSeed()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13975 | uint32_t result = hash_field_ >> String::kHashShift; |
| 13976 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 13977 | return result; |
| 13978 | } |
| 13979 | |
| 13980 | |
| 13981 | uint32_t HashForObject(Object* other) { |
| 13982 | return String::cast(other)->Hash(); |
| 13983 | } |
| 13984 | |
| 13985 | bool IsMatch(Object* string) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13986 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
| 13987 | return String::cast(string)->IsOneByteEqualTo(chars); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13988 | } |
| 13989 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13990 | MaybeObject* AsObject(Heap* heap) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13991 | if (hash_field_ == 0) Hash(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 13992 | Vector<const uint8_t> chars(string_->GetChars() + from_, length_); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 13993 | return heap->AllocateOneByteInternalizedString(chars, hash_field_); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13994 | } |
| 13995 | |
| 13996 | private: |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 13997 | Handle<SeqOneByteString> string_; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 13998 | int from_; |
| 13999 | int length_; |
| 14000 | uint32_t hash_field_; |
| 14001 | }; |
| 14002 | |
| 14003 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14004 | class TwoByteStringKey : public SequentialStringKey<uc16> { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14005 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14006 | explicit TwoByteStringKey(Vector<const uc16> str, uint32_t seed) |
| 14007 | : SequentialStringKey<uc16>(str, seed) { } |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14008 | |
| 14009 | bool IsMatch(Object* string) { |
| 14010 | return String::cast(string)->IsTwoByteEqualTo(string_); |
| 14011 | } |
| 14012 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14013 | MaybeObject* AsObject(Heap* heap) { |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14014 | if (hash_field_ == 0) Hash(); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14015 | return heap->AllocateTwoByteInternalizedString(string_, hash_field_); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 14016 | } |
| 14017 | }; |
| 14018 | |
| 14019 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14020 | // InternalizedStringKey carries a string/internalized-string object as key. |
| 14021 | class InternalizedStringKey : public HashTableKey { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14022 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14023 | explicit InternalizedStringKey(String* string) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14024 | : string_(string) { } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14025 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 14026 | bool IsMatch(Object* string) { |
| 14027 | return String::cast(string)->Equals(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14028 | } |
| 14029 | |
| 14030 | uint32_t Hash() { return string_->Hash(); } |
| 14031 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14032 | uint32_t HashForObject(Object* other) { |
| 14033 | return String::cast(other)->Hash(); |
| 14034 | } |
| 14035 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14036 | MaybeObject* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14037 | // Attempt to flatten the string, so that internalized strings will most |
| 14038 | // often be flat strings. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 14039 | string_ = string_->TryFlattenGetString(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14040 | // Internalize the string if possible. |
| 14041 | Map* map = heap->InternalizedStringMapForString(string_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14042 | if (map != NULL) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 14043 | string_->set_map_no_write_barrier(map); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14044 | ASSERT(string_->IsInternalizedString()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14045 | return string_; |
| 14046 | } |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14047 | // Otherwise allocate a new internalized string. |
| 14048 | return heap->AllocateInternalizedStringImpl( |
| 14049 | string_, string_->length(), string_->hash_field()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14050 | } |
| 14051 | |
| 14052 | static uint32_t StringHash(Object* obj) { |
| 14053 | return String::cast(obj)->Hash(); |
| 14054 | } |
| 14055 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14056 | String* string_; |
| 14057 | }; |
| 14058 | |
| 14059 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14060 | template<typename Shape, typename Key> |
| 14061 | void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14062 | IteratePointers(v, 0, kElementsStartOffset); |
| 14063 | } |
| 14064 | |
| 14065 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14066 | template<typename Shape, typename Key> |
| 14067 | void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14068 | IteratePointers(v, |
| 14069 | kElementsStartOffset, |
| 14070 | kHeaderSize + length() * kPointerSize); |
| 14071 | } |
| 14072 | |
| 14073 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14074 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14075 | MaybeObject* HashTable<Shape, Key>::Allocate(Heap* heap, |
| 14076 | int at_least_space_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14077 | MinimumCapacity capacity_option, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14078 | PretenureFlag pretenure) { |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14079 | ASSERT(!capacity_option || IS_POWER_OF_TWO(at_least_space_for)); |
| 14080 | int capacity = (capacity_option == USE_CUSTOM_MINIMUM_CAPACITY) |
| 14081 | ? at_least_space_for |
| 14082 | : ComputeCapacity(at_least_space_for); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 14083 | if (capacity > HashTable::kMaxCapacity) { |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 14084 | return Failure::OutOfMemoryException(0x10); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14085 | } |
| 14086 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14087 | Object* obj; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14088 | { MaybeObject* maybe_obj = |
| 14089 | heap-> AllocateHashTable(EntryToIndex(capacity), pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14090 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14091 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14092 | HashTable::cast(obj)->SetNumberOfElements(0); |
| 14093 | HashTable::cast(obj)->SetNumberOfDeletedElements(0); |
| 14094 | HashTable::cast(obj)->SetCapacity(capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14095 | return obj; |
| 14096 | } |
| 14097 | |
| 14098 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14099 | // Find entry for key otherwise return kNotFound. |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14100 | int NameDictionary::FindEntry(Name* key) { |
| 14101 | if (!key->IsUniqueName()) { |
| 14102 | return HashTable<NameDictionaryShape, Name*>::FindEntry(key); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 14103 | } |
| 14104 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14105 | // Optimized for unique names. Knowledge of the key type allows: |
| 14106 | // 1. Move the check if the key is unique out of the loop. |
| 14107 | // 2. Avoid comparing hash codes in unique-to-unique comparison. |
| 14108 | // 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] | 14109 | // In case of positive result the dictionary key may be replaced by the |
| 14110 | // internalized string with minimal performance penalty. It gives a chance |
| 14111 | // to perform further lookups in code stubs (and significant performance |
| 14112 | // boost a certain style of code). |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 14113 | |
| 14114 | // EnsureCapacity will guarantee the hash table is never full. |
| 14115 | uint32_t capacity = Capacity(); |
| 14116 | uint32_t entry = FirstProbe(key->Hash(), capacity); |
| 14117 | uint32_t count = 1; |
| 14118 | |
| 14119 | while (true) { |
| 14120 | int index = EntryToIndex(entry); |
| 14121 | Object* element = get(index); |
| 14122 | if (element->IsUndefined()) break; // Empty entry. |
| 14123 | if (key == element) return entry; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14124 | if (!element->IsUniqueName() && |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14125 | !element->IsTheHole() && |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14126 | Name::cast(element)->Equals(key)) { |
| 14127 | // 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] | 14128 | // internalized string for faster further lookups. |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 14129 | set(index, key); |
| 14130 | return entry; |
| 14131 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14132 | ASSERT(element->IsTheHole() || !Name::cast(element)->Equals(key)); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 14133 | entry = NextProbe(entry, count++, capacity); |
| 14134 | } |
| 14135 | return kNotFound; |
| 14136 | } |
| 14137 | |
| 14138 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14139 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14140 | MaybeObject* HashTable<Shape, Key>::Rehash(HashTable* new_table, Key key) { |
| 14141 | ASSERT(NumberOfElements() < new_table->Capacity()); |
| 14142 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14143 | DisallowHeapAllocation no_gc; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14144 | WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc); |
| 14145 | |
| 14146 | // Copy prefix to new array. |
| 14147 | for (int i = kPrefixStartIndex; |
| 14148 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 14149 | i++) { |
| 14150 | new_table->set(i, get(i), mode); |
| 14151 | } |
| 14152 | |
| 14153 | // Rehash the elements. |
| 14154 | int capacity = Capacity(); |
| 14155 | for (int i = 0; i < capacity; i++) { |
| 14156 | uint32_t from_index = EntryToIndex(i); |
| 14157 | Object* k = get(from_index); |
| 14158 | if (IsKey(k)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14159 | uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14160 | uint32_t insertion_index = |
| 14161 | EntryToIndex(new_table->FindInsertionEntry(hash)); |
| 14162 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14163 | new_table->set(insertion_index + j, get(from_index + j), mode); |
| 14164 | } |
| 14165 | } |
| 14166 | } |
| 14167 | new_table->SetNumberOfElements(NumberOfElements()); |
| 14168 | new_table->SetNumberOfDeletedElements(0); |
| 14169 | return new_table; |
| 14170 | } |
| 14171 | |
| 14172 | |
| 14173 | template<typename Shape, typename Key> |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 14174 | uint32_t HashTable<Shape, Key>::EntryForProbe(Key key, |
| 14175 | Object* k, |
| 14176 | int probe, |
| 14177 | uint32_t expected) { |
| 14178 | uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k); |
| 14179 | uint32_t capacity = Capacity(); |
| 14180 | uint32_t entry = FirstProbe(hash, capacity); |
| 14181 | for (int i = 1; i < probe; i++) { |
| 14182 | if (entry == expected) return expected; |
| 14183 | entry = NextProbe(entry, i, capacity); |
| 14184 | } |
| 14185 | return entry; |
| 14186 | } |
| 14187 | |
| 14188 | |
| 14189 | template<typename Shape, typename Key> |
| 14190 | void HashTable<Shape, Key>::Swap(uint32_t entry1, |
| 14191 | uint32_t entry2, |
| 14192 | WriteBarrierMode mode) { |
| 14193 | int index1 = EntryToIndex(entry1); |
| 14194 | int index2 = EntryToIndex(entry2); |
| 14195 | Object* temp[Shape::kEntrySize]; |
| 14196 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14197 | temp[j] = get(index1 + j); |
| 14198 | } |
| 14199 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14200 | set(index1 + j, get(index2 + j), mode); |
| 14201 | } |
| 14202 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 14203 | set(index2 + j, temp[j], mode); |
| 14204 | } |
| 14205 | } |
| 14206 | |
| 14207 | |
| 14208 | template<typename Shape, typename Key> |
| 14209 | void HashTable<Shape, Key>::Rehash(Key key) { |
| 14210 | DisallowHeapAllocation no_gc; |
| 14211 | WriteBarrierMode mode = GetWriteBarrierMode(no_gc); |
| 14212 | uint32_t capacity = Capacity(); |
| 14213 | bool done = false; |
| 14214 | for (int probe = 1; !done; probe++) { |
| 14215 | // All elements at entries given by one of the first _probe_ probes |
| 14216 | // are placed correctly. Other elements might need to be moved. |
| 14217 | done = true; |
| 14218 | for (uint32_t current = 0; current < capacity; current++) { |
| 14219 | Object* current_key = get(EntryToIndex(current)); |
| 14220 | if (IsKey(current_key)) { |
| 14221 | uint32_t target = EntryForProbe(key, current_key, probe, current); |
| 14222 | if (current == target) continue; |
| 14223 | Object* target_key = get(EntryToIndex(target)); |
| 14224 | if (!IsKey(target_key) || |
| 14225 | EntryForProbe(key, target_key, probe, target) != target) { |
| 14226 | // Put the current element into the correct position. |
| 14227 | Swap(current, target, mode); |
| 14228 | // The other element will be processed on the next iteration. |
| 14229 | current--; |
| 14230 | } else { |
| 14231 | // The place for the current element is occupied. Leave the element |
| 14232 | // for the next probe. |
| 14233 | done = false; |
| 14234 | } |
| 14235 | } |
| 14236 | } |
| 14237 | } |
| 14238 | } |
| 14239 | |
| 14240 | |
| 14241 | template<typename Shape, typename Key> |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14242 | MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, |
| 14243 | Key key, |
| 14244 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14245 | int capacity = Capacity(); |
| 14246 | int nof = NumberOfElements() + n; |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 14247 | int nod = NumberOfDeletedElements(); |
| 14248 | // Return if: |
| 14249 | // 50% is still free after adding n elements and |
| 14250 | // at most 50% of the free elements are deleted elements. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14251 | if (nod <= (capacity - nof) >> 1) { |
| 14252 | int needed_free = nof >> 1; |
| 14253 | if (nof + needed_free <= capacity) return this; |
| 14254 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14255 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14256 | const int kMinCapacityForPretenure = 256; |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14257 | bool should_pretenure = pretenure == TENURED || |
| 14258 | ((capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14259 | Object* obj; |
| 14260 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14261 | Allocate(GetHeap(), |
| 14262 | nof * 2, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14263 | USE_DEFAULT_MINIMUM_CAPACITY, |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14264 | should_pretenure ? TENURED : NOT_TENURED); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14265 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14266 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 14267 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14268 | return Rehash(HashTable::cast(obj), key); |
| 14269 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14270 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14271 | |
| 14272 | template<typename Shape, typename Key> |
| 14273 | MaybeObject* HashTable<Shape, Key>::Shrink(Key key) { |
| 14274 | int capacity = Capacity(); |
| 14275 | int nof = NumberOfElements(); |
| 14276 | |
| 14277 | // Shrink to fit the number of elements if only a quarter of the |
| 14278 | // capacity is filled with elements. |
| 14279 | if (nof > (capacity >> 2)) return this; |
| 14280 | // Allocate a new dictionary with room for at least the current |
| 14281 | // number of elements. The allocation method will make sure that |
| 14282 | // there is extra room in the dictionary for additions. Don't go |
| 14283 | // lower than room for 16 elements. |
| 14284 | int at_least_room_for = nof; |
| 14285 | if (at_least_room_for < 16) return this; |
| 14286 | |
| 14287 | const int kMinCapacityForPretenure = 256; |
| 14288 | bool pretenure = |
| 14289 | (at_least_room_for > kMinCapacityForPretenure) && |
| 14290 | !GetHeap()->InNewSpace(this); |
| 14291 | Object* obj; |
| 14292 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14293 | Allocate(GetHeap(), |
| 14294 | at_least_room_for, |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 14295 | USE_DEFAULT_MINIMUM_CAPACITY, |
| 14296 | pretenure ? TENURED : NOT_TENURED); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14297 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14298 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14299 | |
| 14300 | return Rehash(HashTable::cast(obj), key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14301 | } |
| 14302 | |
| 14303 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14304 | template<typename Shape, typename Key> |
| 14305 | uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14306 | uint32_t capacity = Capacity(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14307 | uint32_t entry = FirstProbe(hash, capacity); |
| 14308 | uint32_t count = 1; |
| 14309 | // EnsureCapacity will guarantee the hash table is never full. |
| 14310 | while (true) { |
| 14311 | Object* element = KeyAt(entry); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 14312 | if (element->IsUndefined() || element->IsTheHole()) break; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14313 | entry = NextProbe(entry, count++, capacity); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14314 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14315 | return entry; |
| 14316 | } |
| 14317 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14318 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14319 | // Force instantiation of template instances class. |
| 14320 | // Please note this list is compiler dependent. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14321 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14322 | template class HashTable<StringTableShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14323 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14324 | template class HashTable<CompilationCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14325 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14326 | template class HashTable<MapCacheShape, HashTableKey*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14327 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14328 | template class HashTable<ObjectHashTableShape<1>, Object*>; |
| 14329 | |
| 14330 | template class HashTable<ObjectHashTableShape<2>, Object*>; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 14331 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 14332 | template class HashTable<WeakHashTableShape<2>, Object*>; |
| 14333 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14334 | template class Dictionary<NameDictionaryShape, Name*>; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14335 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14336 | template class Dictionary<SeededNumberDictionaryShape, uint32_t>; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14337 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14338 | template class Dictionary<UnseededNumberDictionaryShape, uint32_t>; |
| 14339 | |
| 14340 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14341 | Allocate(Heap* heap, int at_least_space_for, PretenureFlag pretenure); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14342 | |
| 14343 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14344 | Allocate(Heap* heap, int at_least_space_for, PretenureFlag pretenure); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14345 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14346 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 14347 | Allocate(Heap* heap, int n, PretenureFlag pretenure); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14348 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14349 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::AtPut( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14350 | uint32_t, Object*); |
| 14351 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14352 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14353 | AtPut(uint32_t, Object*); |
| 14354 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 14355 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14356 | SlowReverseLookup(Object* value); |
| 14357 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14358 | template Object* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14359 | SlowReverseLookup(Object* value); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14360 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14361 | template Object* Dictionary<NameDictionaryShape, Name*>::SlowReverseLookup( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14362 | Object*); |
| 14363 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14364 | template void Dictionary<SeededNumberDictionaryShape, uint32_t>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14365 | FixedArray*, |
| 14366 | PropertyAttributes, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14367 | Dictionary<SeededNumberDictionaryShape, uint32_t>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14368 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14369 | template Object* Dictionary<NameDictionaryShape, Name*>::DeleteProperty( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14370 | int, JSObject::DeleteMode); |
| 14371 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14372 | template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14373 | DeleteProperty(int, JSObject::DeleteMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14374 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14375 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Shrink(Name* n); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14376 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14377 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Shrink( |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 14378 | uint32_t); |
| 14379 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14380 | template void Dictionary<NameDictionaryShape, Name*>::CopyKeysTo( |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 14381 | FixedArray*, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 14382 | int, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 14383 | PropertyAttributes, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14384 | Dictionary<NameDictionaryShape, Name*>::SortMode); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14385 | |
| 14386 | template int |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14387 | Dictionary<NameDictionaryShape, Name*>::NumberOfElementsFilterAttributes( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14388 | PropertyAttributes); |
| 14389 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14390 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::Add( |
| 14391 | Name*, Object*, PropertyDetails); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14392 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14393 | template MaybeObject* |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14394 | Dictionary<NameDictionaryShape, Name*>::GenerateNewEnumerationIndices(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14395 | |
| 14396 | template int |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14397 | Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14398 | NumberOfElementsFilterAttributes(PropertyAttributes); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14399 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14400 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Add( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14401 | uint32_t, Object*, PropertyDetails); |
| 14402 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14403 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::Add( |
| 14404 | uint32_t, Object*, PropertyDetails); |
| 14405 | |
| 14406 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14407 | EnsureCapacity(int, uint32_t); |
| 14408 | |
| 14409 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14410 | EnsureCapacity(int, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14411 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14412 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>:: |
| 14413 | EnsureCapacity(int, Name*); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14414 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14415 | template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>:: |
| 14416 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
| 14417 | |
| 14418 | template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>:: |
| 14419 | AddEntry(uint32_t, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14420 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14421 | template MaybeObject* Dictionary<NameDictionaryShape, Name*>::AddEntry( |
| 14422 | Name*, Object*, PropertyDetails, uint32_t); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14423 | |
| 14424 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14425 | int Dictionary<SeededNumberDictionaryShape, uint32_t>::NumberOfEnumElements(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 14426 | |
| 14427 | template |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14428 | int Dictionary<NameDictionaryShape, Name*>::NumberOfEnumElements(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 14429 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14430 | template |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14431 | int HashTable<SeededNumberDictionaryShape, uint32_t>::FindEntry(uint32_t); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14432 | |
| 14433 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14434 | Handle<Object> JSObject::PrepareSlowElementsForSort( |
| 14435 | Handle<JSObject> object, uint32_t limit) { |
| 14436 | CALL_HEAP_FUNCTION(object->GetIsolate(), |
| 14437 | object->PrepareSlowElementsForSort(limit), |
| 14438 | Object); |
| 14439 | } |
| 14440 | |
| 14441 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14442 | // Collates undefined and unexisting elements below limit from position |
| 14443 | // zero of the elements. The object stays in Dictionary mode. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14444 | MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14445 | ASSERT(HasDictionaryElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14446 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 14447 | // or because we are not going to sort (and therefore compact) all of the |
| 14448 | // elements. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14449 | SeededNumberDictionary* dict = element_dictionary(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14450 | HeapNumber* result_double = NULL; |
| 14451 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14452 | // Allocate space for result before we start mutating the object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14453 | Object* new_double; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14454 | { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14455 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 14456 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14457 | result_double = HeapNumber::cast(new_double); |
| 14458 | } |
| 14459 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14460 | Object* obj; |
| 14461 | { MaybeObject* maybe_obj = |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 14462 | SeededNumberDictionary::Allocate(GetHeap(), dict->NumberOfElements()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14463 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 14464 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 14465 | SeededNumberDictionary* new_dict = SeededNumberDictionary::cast(obj); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14466 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14467 | DisallowHeapAllocation no_alloc; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14468 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14469 | uint32_t pos = 0; |
| 14470 | uint32_t undefs = 0; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14471 | int capacity = dict->Capacity(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14472 | for (int i = 0; i < capacity; i++) { |
| 14473 | Object* k = dict->KeyAt(i); |
| 14474 | if (dict->IsKey(k)) { |
| 14475 | ASSERT(k->IsNumber()); |
| 14476 | ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 14477 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 14478 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 14479 | Object* value = dict->ValueAt(i); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 14480 | PropertyDetails details = dict->DetailsAt(i); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 14481 | if (details.type() == CALLBACKS || details.IsReadOnly()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 14482 | // 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] | 14483 | // 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] | 14484 | return Smi::FromInt(-1); |
| 14485 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14486 | uint32_t key = NumberToUint32(k); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14487 | // In the following we assert that adding the entry to the new dictionary |
| 14488 | // does not cause GC. This is the case because we made sure to allocate |
| 14489 | // the dictionary big enough above, so it need not grow. |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14490 | if (key < limit) { |
| 14491 | if (value->IsUndefined()) { |
| 14492 | undefs++; |
| 14493 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14494 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14495 | // Adding an entry with the key beyond smi-range requires |
| 14496 | // allocation. Bailout. |
| 14497 | return Smi::FromInt(-1); |
| 14498 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14499 | new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14500 | pos++; |
| 14501 | } |
| 14502 | } else { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14503 | if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14504 | // Adding an entry with the key beyond smi-range requires |
| 14505 | // allocation. Bailout. |
| 14506 | return Smi::FromInt(-1); |
| 14507 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14508 | new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14509 | } |
| 14510 | } |
| 14511 | } |
| 14512 | |
| 14513 | uint32_t result = pos; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14514 | PropertyDetails no_details = PropertyDetails(NONE, NORMAL, 0); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14515 | Heap* heap = GetHeap(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14516 | while (undefs > 0) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 14517 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14518 | // Adding an entry with the key beyond smi-range requires |
| 14519 | // allocation. Bailout. |
| 14520 | return Smi::FromInt(-1); |
| 14521 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14522 | new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)-> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14523 | ToObjectUnchecked(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14524 | pos++; |
| 14525 | undefs--; |
| 14526 | } |
| 14527 | |
| 14528 | set_elements(new_dict); |
| 14529 | |
| 14530 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 14531 | return Smi::FromInt(static_cast<int>(result)); |
| 14532 | } |
| 14533 | |
| 14534 | ASSERT_NE(NULL, result_double); |
| 14535 | result_double->set_value(static_cast<double>(result)); |
| 14536 | return result_double; |
| 14537 | } |
| 14538 | |
| 14539 | |
| 14540 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 14541 | // the start of the elements array. |
| 14542 | // If the object is in dictionary mode, it is converted to fast elements |
| 14543 | // mode. |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14544 | Handle<Object> JSObject::PrepareElementsForSort(Handle<JSObject> object, |
| 14545 | uint32_t limit) { |
| 14546 | Isolate* isolate = object->GetIsolate(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 14547 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14548 | ASSERT(!object->map()->is_observed()); |
| 14549 | if (object->HasDictionaryElements()) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14550 | // Convert to fast elements containing only the existing properties. |
| 14551 | // Ordering is irrelevant, since we are going to sort anyway. |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14552 | Handle<SeededNumberDictionary> dict(object->element_dictionary()); |
| 14553 | if (object->IsJSArray() || dict->requires_slow_elements() || |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 14554 | dict->max_number_key() >= limit) { |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14555 | return JSObject::PrepareSlowElementsForSort(object, limit); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14556 | } |
| 14557 | // Convert to fast elements. |
| 14558 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14559 | Handle<Map> new_map = |
| 14560 | JSObject::GetElementsTransitionMap(object, FAST_HOLEY_ELEMENTS); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 14561 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14562 | PretenureFlag tenure = isolate->heap()->InNewSpace(*object) ? |
| 14563 | NOT_TENURED: TENURED; |
| 14564 | Handle<FixedArray> fast_elements = |
| 14565 | isolate->factory()->NewFixedArray(dict->NumberOfElements(), tenure); |
| 14566 | dict->CopyValuesTo(*fast_elements); |
| 14567 | object->ValidateElements(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 14568 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14569 | object->set_map_and_elements(*new_map, *fast_elements); |
| 14570 | } else if (object->HasExternalArrayElements()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 14571 | // External arrays cannot have holes or undefined elements. |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14572 | return handle(Smi::FromInt( |
| 14573 | ExternalArray::cast(object->elements())->length()), isolate); |
| 14574 | } else if (!object->HasFastDoubleElements()) { |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14575 | EnsureWritableFastElements(object); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14576 | } |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14577 | ASSERT(object->HasFastSmiOrObjectElements() || |
| 14578 | object->HasFastDoubleElements()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14579 | |
| 14580 | // Collect holes at the end, undefined before that and the rest at the |
| 14581 | // start, and return the number of non-hole, non-undefined values. |
| 14582 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14583 | Handle<FixedArrayBase> elements_base(object->elements()); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14584 | uint32_t elements_length = static_cast<uint32_t>(elements_base->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14585 | if (limit > elements_length) { |
| 14586 | limit = elements_length ; |
| 14587 | } |
| 14588 | if (limit == 0) { |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14589 | return handle(Smi::FromInt(0), isolate); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14590 | } |
| 14591 | |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14592 | uint32_t result = 0; |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14593 | if (elements_base->map() == isolate->heap()->fixed_double_array_map()) { |
| 14594 | FixedDoubleArray* elements = FixedDoubleArray::cast(*elements_base); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14595 | // Split elements into defined and the_hole, in that order. |
| 14596 | unsigned int holes = limit; |
| 14597 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 14598 | // number of stores of non-undefined, non-the-hole values. |
| 14599 | for (unsigned int i = 0; i < holes; i++) { |
| 14600 | if (elements->is_the_hole(i)) { |
| 14601 | holes--; |
| 14602 | } else { |
| 14603 | continue; |
| 14604 | } |
| 14605 | // Position i needs to be filled. |
| 14606 | while (holes > i) { |
| 14607 | if (elements->is_the_hole(holes)) { |
| 14608 | holes--; |
| 14609 | } else { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 14610 | elements->set(i, elements->get_scalar(holes)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14611 | break; |
| 14612 | } |
| 14613 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14614 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14615 | result = holes; |
| 14616 | while (holes < limit) { |
| 14617 | elements->set_the_hole(holes); |
| 14618 | holes++; |
| 14619 | } |
| 14620 | } else { |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14621 | FixedArray* elements = FixedArray::cast(*elements_base); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14622 | DisallowHeapAllocation no_gc; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14623 | |
| 14624 | // Split elements into defined, undefined and the_hole, in that order. Only |
| 14625 | // count locations for undefined and the hole, and fill them afterwards. |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 14626 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_gc); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14627 | unsigned int undefs = limit; |
| 14628 | unsigned int holes = limit; |
| 14629 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 14630 | // number of stores of non-undefined, non-the-hole values. |
| 14631 | for (unsigned int i = 0; i < undefs; i++) { |
| 14632 | Object* current = elements->get(i); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14633 | if (current->IsTheHole()) { |
| 14634 | holes--; |
| 14635 | undefs--; |
| 14636 | } else if (current->IsUndefined()) { |
| 14637 | undefs--; |
| 14638 | } else { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14639 | continue; |
| 14640 | } |
| 14641 | // Position i needs to be filled. |
| 14642 | while (undefs > i) { |
| 14643 | current = elements->get(undefs); |
| 14644 | if (current->IsTheHole()) { |
| 14645 | holes--; |
| 14646 | undefs--; |
| 14647 | } else if (current->IsUndefined()) { |
| 14648 | undefs--; |
| 14649 | } else { |
| 14650 | elements->set(i, current, write_barrier); |
| 14651 | break; |
| 14652 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14653 | } |
| 14654 | } |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14655 | result = undefs; |
| 14656 | while (undefs < holes) { |
| 14657 | elements->set_undefined(undefs); |
| 14658 | undefs++; |
| 14659 | } |
| 14660 | while (holes < limit) { |
| 14661 | elements->set_the_hole(holes); |
| 14662 | holes++; |
| 14663 | } |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14664 | } |
| 14665 | |
jkummerow@chromium.org | ef33a54 | 2013-10-28 18:03:37 +0000 | [diff] [blame] | 14666 | return isolate->factory()->NewNumberFromUint(result); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14667 | } |
| 14668 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14669 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 14670 | ExternalArrayType JSTypedArray::type() { |
| 14671 | switch (elements()->map()->instance_type()) { |
| 14672 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 14673 | return kExternalByteArray; |
| 14674 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 14675 | return kExternalUnsignedByteArray; |
| 14676 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 14677 | return kExternalShortArray; |
| 14678 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 14679 | return kExternalUnsignedShortArray; |
| 14680 | case EXTERNAL_INT_ARRAY_TYPE: |
| 14681 | return kExternalIntArray; |
| 14682 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 14683 | return kExternalUnsignedIntArray; |
| 14684 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 14685 | return kExternalFloatArray; |
| 14686 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 14687 | return kExternalDoubleArray; |
| 14688 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 14689 | return kExternalPixelArray; |
| 14690 | default: |
| 14691 | return static_cast<ExternalArrayType>(-1); |
| 14692 | } |
| 14693 | } |
| 14694 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14695 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14696 | size_t JSTypedArray::element_size() { |
| 14697 | switch (elements()->map()->instance_type()) { |
| 14698 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 14699 | return 1; |
| 14700 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 14701 | return 1; |
| 14702 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 14703 | return 2; |
| 14704 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 14705 | return 2; |
| 14706 | case EXTERNAL_INT_ARRAY_TYPE: |
| 14707 | return 4; |
| 14708 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 14709 | return 4; |
| 14710 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 14711 | return 4; |
| 14712 | case EXTERNAL_DOUBLE_ARRAY_TYPE: |
| 14713 | return 8; |
| 14714 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 14715 | return 1; |
| 14716 | default: |
| 14717 | UNREACHABLE(); |
| 14718 | return 0; |
| 14719 | } |
| 14720 | } |
| 14721 | |
| 14722 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 14723 | Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14724 | uint8_t clamped_value = 0; |
| 14725 | if (index < static_cast<uint32_t>(length())) { |
| 14726 | if (value->IsSmi()) { |
| 14727 | int int_value = Smi::cast(value)->value(); |
| 14728 | if (int_value < 0) { |
| 14729 | clamped_value = 0; |
| 14730 | } else if (int_value > 255) { |
| 14731 | clamped_value = 255; |
| 14732 | } else { |
| 14733 | clamped_value = static_cast<uint8_t>(int_value); |
| 14734 | } |
| 14735 | } else if (value->IsHeapNumber()) { |
| 14736 | double double_value = HeapNumber::cast(value)->value(); |
| 14737 | if (!(double_value > 0)) { |
| 14738 | // NaN and less than zero clamp to zero. |
| 14739 | clamped_value = 0; |
| 14740 | } else if (double_value > 255) { |
| 14741 | // Greater than 255 clamp to 255. |
| 14742 | clamped_value = 255; |
| 14743 | } else { |
| 14744 | // Other doubles are rounded to the nearest integer. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 14745 | clamped_value = static_cast<uint8_t>(lrint(double_value)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 14746 | } |
| 14747 | } else { |
| 14748 | // Clamp undefined to zero (default). All other types have been |
| 14749 | // converted to a number type further up in the call chain. |
| 14750 | ASSERT(value->IsUndefined()); |
| 14751 | } |
| 14752 | set(index, clamped_value); |
| 14753 | } |
| 14754 | return Smi::FromInt(clamped_value); |
| 14755 | } |
| 14756 | |
| 14757 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14758 | template<typename ExternalArrayClass, typename ValueType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14759 | static MaybeObject* ExternalArrayIntSetter(Heap* heap, |
| 14760 | ExternalArrayClass* receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14761 | uint32_t index, |
| 14762 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14763 | ValueType cast_value = 0; |
| 14764 | if (index < static_cast<uint32_t>(receiver->length())) { |
| 14765 | if (value->IsSmi()) { |
| 14766 | int int_value = Smi::cast(value)->value(); |
| 14767 | cast_value = static_cast<ValueType>(int_value); |
| 14768 | } else if (value->IsHeapNumber()) { |
| 14769 | double double_value = HeapNumber::cast(value)->value(); |
| 14770 | cast_value = static_cast<ValueType>(DoubleToInt32(double_value)); |
| 14771 | } else { |
| 14772 | // Clamp undefined to zero (default). All other types have been |
| 14773 | // converted to a number type further up in the call chain. |
| 14774 | ASSERT(value->IsUndefined()); |
| 14775 | } |
| 14776 | receiver->set(index, cast_value); |
| 14777 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14778 | return heap->NumberFromInt32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14779 | } |
| 14780 | |
| 14781 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14782 | Handle<Object> ExternalByteArray::SetValue(Handle<ExternalByteArray> array, |
| 14783 | uint32_t index, |
| 14784 | Handle<Object> value) { |
| 14785 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14786 | array->SetValue(index, *value), |
| 14787 | Object); |
| 14788 | } |
| 14789 | |
| 14790 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14791 | MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14792 | return ExternalArrayIntSetter<ExternalByteArray, int8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14793 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14794 | } |
| 14795 | |
| 14796 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14797 | Handle<Object> ExternalUnsignedByteArray::SetValue( |
| 14798 | Handle<ExternalUnsignedByteArray> array, |
| 14799 | uint32_t index, |
| 14800 | Handle<Object> value) { |
| 14801 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14802 | array->SetValue(index, *value), |
| 14803 | Object); |
| 14804 | } |
| 14805 | |
| 14806 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14807 | MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index, |
| 14808 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14809 | return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14810 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14811 | } |
| 14812 | |
| 14813 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14814 | Handle<Object> ExternalShortArray::SetValue( |
| 14815 | Handle<ExternalShortArray> array, |
| 14816 | uint32_t index, |
| 14817 | Handle<Object> value) { |
| 14818 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14819 | array->SetValue(index, *value), |
| 14820 | Object); |
| 14821 | } |
| 14822 | |
| 14823 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14824 | MaybeObject* ExternalShortArray::SetValue(uint32_t index, |
| 14825 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14826 | return ExternalArrayIntSetter<ExternalShortArray, int16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14827 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14828 | } |
| 14829 | |
| 14830 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14831 | Handle<Object> ExternalUnsignedShortArray::SetValue( |
| 14832 | Handle<ExternalUnsignedShortArray> array, |
| 14833 | uint32_t index, |
| 14834 | Handle<Object> value) { |
| 14835 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14836 | array->SetValue(index, *value), |
| 14837 | Object); |
| 14838 | } |
| 14839 | |
| 14840 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14841 | MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index, |
| 14842 | Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14843 | return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14844 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14845 | } |
| 14846 | |
| 14847 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14848 | Handle<Object> ExternalIntArray::SetValue(Handle<ExternalIntArray> array, |
| 14849 | uint32_t index, |
| 14850 | Handle<Object> value) { |
| 14851 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14852 | array->SetValue(index, *value), |
| 14853 | Object); |
| 14854 | } |
| 14855 | |
| 14856 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14857 | MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14858 | return ExternalArrayIntSetter<ExternalIntArray, int32_t> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14859 | (GetHeap(), this, index, value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14860 | } |
| 14861 | |
| 14862 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14863 | Handle<Object> ExternalUnsignedIntArray::SetValue( |
| 14864 | Handle<ExternalUnsignedIntArray> array, |
| 14865 | uint32_t index, |
| 14866 | Handle<Object> value) { |
| 14867 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14868 | array->SetValue(index, *value), |
| 14869 | Object); |
| 14870 | } |
| 14871 | |
| 14872 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14873 | MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14874 | uint32_t cast_value = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14875 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14876 | if (index < static_cast<uint32_t>(length())) { |
| 14877 | if (value->IsSmi()) { |
| 14878 | int int_value = Smi::cast(value)->value(); |
| 14879 | cast_value = static_cast<uint32_t>(int_value); |
| 14880 | } else if (value->IsHeapNumber()) { |
| 14881 | double double_value = HeapNumber::cast(value)->value(); |
| 14882 | cast_value = static_cast<uint32_t>(DoubleToUint32(double_value)); |
| 14883 | } else { |
| 14884 | // Clamp undefined to zero (default). All other types have been |
| 14885 | // converted to a number type further up in the call chain. |
| 14886 | ASSERT(value->IsUndefined()); |
| 14887 | } |
| 14888 | set(index, cast_value); |
| 14889 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14890 | return heap->NumberFromUint32(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14891 | } |
| 14892 | |
| 14893 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14894 | Handle<Object> ExternalFloatArray::SetValue(Handle<ExternalFloatArray> array, |
| 14895 | uint32_t index, |
| 14896 | Handle<Object> value) { |
| 14897 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14898 | array->SetValue(index, *value), |
| 14899 | Object); |
| 14900 | } |
| 14901 | |
| 14902 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 14903 | MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14904 | float cast_value = static_cast<float>(OS::nan_value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14905 | Heap* heap = GetHeap(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14906 | if (index < static_cast<uint32_t>(length())) { |
| 14907 | if (value->IsSmi()) { |
| 14908 | int int_value = Smi::cast(value)->value(); |
| 14909 | cast_value = static_cast<float>(int_value); |
| 14910 | } else if (value->IsHeapNumber()) { |
| 14911 | double double_value = HeapNumber::cast(value)->value(); |
| 14912 | cast_value = static_cast<float>(double_value); |
| 14913 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14914 | // Clamp undefined to NaN (default). All other types have been |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14915 | // converted to a number type further up in the call chain. |
| 14916 | ASSERT(value->IsUndefined()); |
| 14917 | } |
| 14918 | set(index, cast_value); |
| 14919 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 14920 | return heap->AllocateHeapNumber(cast_value); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 14921 | } |
| 14922 | |
| 14923 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 14924 | Handle<Object> ExternalDoubleArray::SetValue(Handle<ExternalDoubleArray> array, |
| 14925 | uint32_t index, |
| 14926 | Handle<Object> value) { |
| 14927 | CALL_HEAP_FUNCTION(array->GetIsolate(), |
| 14928 | array->SetValue(index, *value), |
| 14929 | Object); |
| 14930 | } |
| 14931 | |
| 14932 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14933 | MaybeObject* ExternalDoubleArray::SetValue(uint32_t index, Object* value) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14934 | double double_value = OS::nan_value(); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14935 | Heap* heap = GetHeap(); |
| 14936 | if (index < static_cast<uint32_t>(length())) { |
| 14937 | if (value->IsSmi()) { |
| 14938 | int int_value = Smi::cast(value)->value(); |
| 14939 | double_value = static_cast<double>(int_value); |
| 14940 | } else if (value->IsHeapNumber()) { |
| 14941 | double_value = HeapNumber::cast(value)->value(); |
| 14942 | } else { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 14943 | // Clamp undefined to NaN (default). All other types have been |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 14944 | // converted to a number type further up in the call chain. |
| 14945 | ASSERT(value->IsUndefined()); |
| 14946 | } |
| 14947 | set(index, double_value); |
| 14948 | } |
| 14949 | return heap->AllocateHeapNumber(double_value); |
| 14950 | } |
| 14951 | |
| 14952 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14953 | PropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14954 | ASSERT(!HasFastProperties()); |
| 14955 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14956 | return PropertyCell::cast(value); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 14957 | } |
| 14958 | |
| 14959 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 14960 | Handle<PropertyCell> JSGlobalObject::EnsurePropertyCell( |
| 14961 | Handle<JSGlobalObject> global, |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14962 | Handle<Name> name) { |
| 14963 | ASSERT(!global->HasFastProperties()); |
| 14964 | int entry = global->property_dictionary()->FindEntry(*name); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 14965 | if (entry == NameDictionary::kNotFound) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14966 | Isolate* isolate = global->GetIsolate(); |
| 14967 | Handle<PropertyCell> cell = isolate->factory()->NewPropertyCell( |
| 14968 | isolate->factory()->the_hole_value()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14969 | PropertyDetails details(NONE, NORMAL, 0); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14970 | details = details.AsDeleted(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14971 | Handle<NameDictionary> dictionary = NameDictionaryAdd( |
| 14972 | handle(global->property_dictionary()), name, cell, details); |
| 14973 | global->set_properties(*dictionary); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14974 | return cell; |
| 14975 | } else { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14976 | Object* value = global->property_dictionary()->ValueAt(entry); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 14977 | ASSERT(value->IsPropertyCell()); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14978 | return handle(PropertyCell::cast(value)); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 14979 | } |
| 14980 | } |
| 14981 | |
| 14982 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14983 | MaybeObject* StringTable::LookupString(String* string, Object** s) { |
| 14984 | InternalizedStringKey key(string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 14985 | return LookupKey(&key, s); |
| 14986 | } |
| 14987 | |
| 14988 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14989 | // 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] | 14990 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 14991 | // string hash calculation loop here for speed. Doesn't work if the two |
| 14992 | // characters form a decimal integer, since such strings have a different hash |
| 14993 | // algorithm. |
| 14994 | class TwoCharHashTableKey : public HashTableKey { |
| 14995 | public: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 14996 | TwoCharHashTableKey(uint16_t c1, uint16_t c2, uint32_t seed) |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 14997 | : c1_(c1), c2_(c2) { |
| 14998 | // Char 1. |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 14999 | uint32_t hash = seed; |
| 15000 | hash += c1; |
| 15001 | hash += hash << 10; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15002 | hash ^= hash >> 6; |
| 15003 | // Char 2. |
| 15004 | hash += c2; |
| 15005 | hash += hash << 10; |
| 15006 | hash ^= hash >> 6; |
| 15007 | // GetHash. |
| 15008 | hash += hash << 3; |
| 15009 | hash ^= hash >> 11; |
| 15010 | hash += hash << 15; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 15011 | if ((hash & String::kHashBitMask) == 0) hash = StringHasher::kZeroHash; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15012 | hash_ = hash; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15013 | #ifdef DEBUG |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15014 | // If this assert fails then we failed to reproduce the two-character |
| 15015 | // version of the string hashing algorithm above. One reason could be |
| 15016 | // that we were passed two digits as characters, since the hash |
| 15017 | // algorithm is different in that case. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15018 | uint16_t chars[2] = {c1, c2}; |
| 15019 | uint32_t check_hash = StringHasher::HashSequentialString(chars, 2, seed); |
| 15020 | hash = (hash << String::kHashShift) | String::kIsNotArrayIndexMask; |
| 15021 | 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] | 15022 | #endif |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15023 | } |
| 15024 | |
| 15025 | bool IsMatch(Object* o) { |
| 15026 | if (!o->IsString()) return false; |
| 15027 | String* other = String::cast(o); |
| 15028 | if (other->length() != 2) return false; |
| 15029 | if (other->Get(0) != c1_) return false; |
| 15030 | return other->Get(1) == c2_; |
| 15031 | } |
| 15032 | |
| 15033 | uint32_t Hash() { return hash_; } |
| 15034 | uint32_t HashForObject(Object* key) { |
| 15035 | if (!key->IsString()) return 0; |
| 15036 | return String::cast(key)->Hash(); |
| 15037 | } |
| 15038 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15039 | Object* AsObject(Heap* heap) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15040 | // The TwoCharHashTableKey is only used for looking in the string |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15041 | // table, not for adding to it. |
| 15042 | UNREACHABLE(); |
| 15043 | return NULL; |
| 15044 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 15045 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15046 | private: |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15047 | uint16_t c1_; |
| 15048 | uint16_t c2_; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15049 | uint32_t hash_; |
| 15050 | }; |
| 15051 | |
| 15052 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15053 | bool StringTable::LookupStringIfExists(String* string, String** result) { |
| 15054 | InternalizedStringKey key(string); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 15055 | int entry = FindEntry(&key); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15056 | if (entry == kNotFound) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 15057 | return false; |
| 15058 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15059 | *result = String::cast(KeyAt(entry)); |
| 15060 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 15061 | return true; |
| 15062 | } |
| 15063 | } |
| 15064 | |
| 15065 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15066 | bool StringTable::LookupTwoCharsStringIfExists(uint16_t c1, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15067 | uint16_t c2, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15068 | String** result) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15069 | TwoCharHashTableKey key(c1, c2, GetHeap()->HashSeed()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15070 | int entry = FindEntry(&key); |
| 15071 | if (entry == kNotFound) { |
| 15072 | return false; |
| 15073 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15074 | *result = String::cast(KeyAt(entry)); |
| 15075 | ASSERT(StringShape(*result).IsInternalized()); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 15076 | return true; |
| 15077 | } |
| 15078 | } |
| 15079 | |
| 15080 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15081 | MaybeObject* StringTable::LookupUtf8String(Vector<const char> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15082 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15083 | Utf8StringKey key(str, GetHeap()->HashSeed()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15084 | return LookupKey(&key, s); |
| 15085 | } |
| 15086 | |
| 15087 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15088 | MaybeObject* StringTable::LookupOneByteString(Vector<const uint8_t> str, |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 15089 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15090 | OneByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 15091 | return LookupKey(&key, s); |
| 15092 | } |
| 15093 | |
| 15094 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15095 | MaybeObject* StringTable::LookupSubStringOneByteString( |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 15096 | Handle<SeqOneByteString> str, |
| 15097 | int from, |
| 15098 | int length, |
| 15099 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15100 | SubStringOneByteStringKey key(str, from, length); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 15101 | return LookupKey(&key, s); |
| 15102 | } |
| 15103 | |
| 15104 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15105 | MaybeObject* StringTable::LookupTwoByteString(Vector<const uc16> str, |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 15106 | Object** s) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15107 | TwoByteStringKey key(str, GetHeap()->HashSeed()); |
fschneider@chromium.org | 9e3e0b6 | 2011-01-03 10:16:46 +0000 | [diff] [blame] | 15108 | return LookupKey(&key, s); |
| 15109 | } |
| 15110 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 15111 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15112 | MaybeObject* StringTable::LookupKey(HashTableKey* key, Object** s) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15113 | int entry = FindEntry(key); |
| 15114 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15115 | // String already in table. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15116 | if (entry != kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15117 | *s = KeyAt(entry); |
| 15118 | return this; |
| 15119 | } |
| 15120 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15121 | // Adding new string. Grow table if needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15122 | Object* obj; |
| 15123 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15124 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15125 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15126 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15127 | // Create string object. |
| 15128 | Object* string; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15129 | { MaybeObject* maybe_string = key->AsObject(GetHeap()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15130 | if (!maybe_string->ToObject(&string)) return maybe_string; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15131 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15132 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15133 | // If the string table grew as part of EnsureCapacity, obj is not |
| 15134 | // the current string table and therefore we cannot use |
| 15135 | // StringTable::cast here. |
| 15136 | StringTable* table = reinterpret_cast<StringTable*>(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15137 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15138 | // 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] | 15139 | entry = table->FindInsertionEntry(key->Hash()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15140 | table->set(EntryToIndex(entry), string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15141 | table->ElementAdded(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15142 | *s = string; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15143 | return table; |
| 15144 | } |
| 15145 | |
| 15146 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 15147 | // The key for the script compilation cache is dependent on the mode flags, |
| 15148 | // because they change the global language mode and thus binding behaviour. |
| 15149 | // If flags change at some point, we must ensure that we do not hit the cache |
| 15150 | // for code compiled with different settings. |
| 15151 | static LanguageMode CurrentGlobalLanguageMode() { |
| 15152 | return FLAG_use_strict |
| 15153 | ? (FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE) |
| 15154 | : CLASSIC_MODE; |
| 15155 | } |
| 15156 | |
| 15157 | |
| 15158 | Object* CompilationCacheTable::Lookup(String* src, Context* context) { |
| 15159 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 15160 | StringSharedKey key(src, |
| 15161 | shared, |
| 15162 | CurrentGlobalLanguageMode(), |
| 15163 | RelocInfo::kNoPosition); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 15164 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15165 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15166 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 15167 | } |
| 15168 | |
| 15169 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 15170 | Object* CompilationCacheTable::LookupEval(String* src, |
| 15171 | Context* context, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 15172 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 15173 | int scope_position) { |
| 15174 | StringSharedKey key(src, |
| 15175 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 15176 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 15177 | scope_position); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 15178 | int entry = FindEntry(&key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15179 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 15180 | return get(EntryToIndex(entry) + 1); |
| 15181 | } |
| 15182 | |
| 15183 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15184 | Object* CompilationCacheTable::LookupRegExp(String* src, |
| 15185 | JSRegExp::Flags flags) { |
| 15186 | RegExpKey key(src, flags); |
| 15187 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15188 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15189 | return get(EntryToIndex(entry) + 1); |
| 15190 | } |
| 15191 | |
| 15192 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 15193 | MaybeObject* CompilationCacheTable::Put(String* src, |
| 15194 | Context* context, |
| 15195 | Object* value) { |
| 15196 | SharedFunctionInfo* shared = context->closure()->shared(); |
| 15197 | StringSharedKey key(src, |
| 15198 | shared, |
| 15199 | CurrentGlobalLanguageMode(), |
| 15200 | RelocInfo::kNoPosition); |
| 15201 | CompilationCacheTable* cache; |
| 15202 | MaybeObject* maybe_cache = EnsureCapacity(1, &key); |
| 15203 | if (!maybe_cache->To(&cache)) return maybe_cache; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 15204 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 15205 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15206 | MaybeObject* maybe_k = key.AsObject(GetHeap()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 15207 | if (!maybe_k->To(&k)) return maybe_k; |
| 15208 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15209 | int entry = cache->FindInsertionEntry(key.Hash()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 15210 | cache->set(EntryToIndex(entry), k); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 15211 | cache->set(EntryToIndex(entry) + 1, value); |
| 15212 | cache->ElementAdded(); |
| 15213 | return cache; |
| 15214 | } |
| 15215 | |
| 15216 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15217 | MaybeObject* CompilationCacheTable::PutEval(String* src, |
| 15218 | Context* context, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 15219 | SharedFunctionInfo* value, |
| 15220 | int scope_position) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 15221 | StringSharedKey key(src, |
| 15222 | context->closure()->shared(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 15223 | value->language_mode(), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 15224 | scope_position); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15225 | Object* obj; |
| 15226 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 15227 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15228 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 15229 | |
| 15230 | CompilationCacheTable* cache = |
| 15231 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15232 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 15233 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15234 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15235 | { MaybeObject* maybe_k = key.AsObject(GetHeap()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15236 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 15237 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 15238 | |
| 15239 | cache->set(EntryToIndex(entry), k); |
| 15240 | cache->set(EntryToIndex(entry) + 1, value); |
| 15241 | cache->ElementAdded(); |
| 15242 | return cache; |
| 15243 | } |
| 15244 | |
| 15245 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15246 | MaybeObject* CompilationCacheTable::PutRegExp(String* src, |
| 15247 | JSRegExp::Flags flags, |
| 15248 | FixedArray* value) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15249 | RegExpKey key(src, flags); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15250 | Object* obj; |
| 15251 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 15252 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15253 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15254 | |
| 15255 | CompilationCacheTable* cache = |
| 15256 | reinterpret_cast<CompilationCacheTable*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15257 | int entry = cache->FindInsertionEntry(key.Hash()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 15258 | // We store the value in the key slot, and compare the search key |
| 15259 | // to the stored value with a custon IsMatch function during lookups. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 15260 | cache->set(EntryToIndex(entry), value); |
| 15261 | cache->set(EntryToIndex(entry) + 1, value); |
| 15262 | cache->ElementAdded(); |
| 15263 | return cache; |
| 15264 | } |
| 15265 | |
| 15266 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15267 | void CompilationCacheTable::Remove(Object* value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15268 | Object* the_hole_value = GetHeap()->the_hole_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15269 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 15270 | int entry_index = EntryToIndex(entry); |
| 15271 | int value_index = entry_index + 1; |
| 15272 | if (get(value_index) == value) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15273 | NoWriteBarrierSet(this, entry_index, the_hole_value); |
| 15274 | NoWriteBarrierSet(this, value_index, the_hole_value); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 15275 | ElementRemoved(); |
| 15276 | } |
| 15277 | } |
| 15278 | return; |
| 15279 | } |
| 15280 | |
| 15281 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15282 | // StringsKey used for HashTable where key is array of internalized strings. |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15283 | class StringsKey : public HashTableKey { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15284 | public: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15285 | explicit StringsKey(FixedArray* strings) : strings_(strings) { } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15286 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15287 | bool IsMatch(Object* strings) { |
| 15288 | FixedArray* o = FixedArray::cast(strings); |
| 15289 | int len = strings_->length(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15290 | if (o->length() != len) return false; |
| 15291 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15292 | if (o->get(i) != strings_->get(i)) return false; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15293 | } |
| 15294 | return true; |
| 15295 | } |
| 15296 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15297 | uint32_t Hash() { return HashForObject(strings_); } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15298 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15299 | uint32_t HashForObject(Object* obj) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15300 | FixedArray* strings = FixedArray::cast(obj); |
| 15301 | int len = strings->length(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15302 | uint32_t hash = 0; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15303 | for (int i = 0; i < len; i++) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15304 | hash ^= String::cast(strings->get(i))->Hash(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15305 | } |
| 15306 | return hash; |
| 15307 | } |
| 15308 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15309 | Object* AsObject(Heap* heap) { return strings_; } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15310 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15311 | private: |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15312 | FixedArray* strings_; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15313 | }; |
| 15314 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15315 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15316 | Object* MapCache::Lookup(FixedArray* array) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15317 | StringsKey key(array); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15318 | int entry = FindEntry(&key); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15319 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15320 | return get(EntryToIndex(entry) + 1); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15321 | } |
| 15322 | |
| 15323 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15324 | MaybeObject* MapCache::Put(FixedArray* array, Map* value) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 15325 | StringsKey key(array); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15326 | Object* obj; |
| 15327 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 15328 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15329 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15330 | |
| 15331 | MapCache* cache = reinterpret_cast<MapCache*>(obj); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15332 | int entry = cache->FindInsertionEntry(key.Hash()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 15333 | cache->set(EntryToIndex(entry), array); |
| 15334 | cache->set(EntryToIndex(entry) + 1, value); |
| 15335 | cache->ElementAdded(); |
| 15336 | return cache; |
| 15337 | } |
| 15338 | |
| 15339 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15340 | template<typename Shape, typename Key> |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15341 | MaybeObject* Dictionary<Shape, Key>::Allocate(Heap* heap, |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 15342 | int at_least_space_for, |
| 15343 | PretenureFlag pretenure) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15344 | Object* obj; |
| 15345 | { MaybeObject* maybe_obj = |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 15346 | HashTable<Shape, Key>::Allocate( |
| 15347 | heap, |
| 15348 | at_least_space_for, |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15349 | USE_DEFAULT_MINIMUM_CAPACITY, |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 15350 | pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15351 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15352 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15353 | // Initialize the next enumeration index. |
| 15354 | Dictionary<Shape, Key>::cast(obj)-> |
| 15355 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15356 | return obj; |
| 15357 | } |
| 15358 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15359 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15360 | void NameDictionary::DoGenerateNewEnumerationIndices( |
| 15361 | Handle<NameDictionary> dictionary) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15362 | CALL_HEAP_FUNCTION_VOID(dictionary->GetIsolate(), |
| 15363 | dictionary->GenerateNewEnumerationIndices()); |
| 15364 | } |
| 15365 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15366 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15367 | MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15368 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15369 | int length = HashTable<Shape, Key>::NumberOfElements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15370 | |
| 15371 | // Allocate and initialize iteration order array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15372 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15373 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15374 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15375 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15376 | FixedArray* iteration_order = FixedArray::cast(obj); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15377 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15378 | iteration_order->set(i, Smi::FromInt(i)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15379 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15380 | |
| 15381 | // Allocate array with enumeration order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15382 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15383 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15384 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15385 | FixedArray* enumeration_order = FixedArray::cast(obj); |
| 15386 | |
| 15387 | // Fill the enumeration order array with property details. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15388 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15389 | int pos = 0; |
| 15390 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15391 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 15392 | int index = DetailsAt(i).dictionary_index(); |
| 15393 | enumeration_order->set(pos++, Smi::FromInt(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15394 | } |
| 15395 | } |
| 15396 | |
| 15397 | // Sort the arrays wrt. enumeration order. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 15398 | iteration_order->SortPairs(enumeration_order, enumeration_order->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15399 | |
| 15400 | // Overwrite the enumeration_order with the enumeration indices. |
| 15401 | for (int i = 0; i < length; i++) { |
| 15402 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 15403 | int enum_index = PropertyDetails::kInitialIndex + i; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15404 | enumeration_order->set(index, Smi::FromInt(enum_index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15405 | } |
| 15406 | |
| 15407 | // Update the dictionary with new indices. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15408 | capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15409 | pos = 0; |
| 15410 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15411 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15412 | int enum_index = Smi::cast(enumeration_order->get(pos++))->value(); |
| 15413 | PropertyDetails details = DetailsAt(i); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15414 | PropertyDetails new_details = PropertyDetails( |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15415 | details.attributes(), details.type(), enum_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15416 | DetailsAtPut(i, new_details); |
| 15417 | } |
| 15418 | } |
| 15419 | |
| 15420 | // Set the next enumeration index. |
| 15421 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
| 15422 | return this; |
| 15423 | } |
| 15424 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15425 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15426 | MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 15427 | // Check whether there are enough enumeration indices to add n elements. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15428 | if (Shape::kIsEnumerable && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15429 | !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) { |
| 15430 | // If not, we generate new indices for the properties. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15431 | Object* result; |
| 15432 | { MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 15433 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 15434 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15435 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15436 | return HashTable<Shape, Key>::EnsureCapacity(n, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15437 | } |
| 15438 | |
| 15439 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15440 | template<typename Shape, typename Key> |
| 15441 | Object* Dictionary<Shape, Key>::DeleteProperty(int entry, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 15442 | JSReceiver::DeleteMode mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15443 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15444 | PropertyDetails details = DetailsAt(entry); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 15445 | // Ignore attributes if forcing a deletion. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 15446 | if (details.IsDontDelete() && mode != JSReceiver::FORCE_DELETION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15447 | return heap->false_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 15448 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15449 | SetEntry(entry, heap->the_hole_value(), heap->the_hole_value()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15450 | HashTable<Shape, Key>::ElementRemoved(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15451 | return heap->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15452 | } |
| 15453 | |
| 15454 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15455 | template<typename Shape, typename Key> |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 15456 | MaybeObject* Dictionary<Shape, Key>::Shrink(Key key) { |
| 15457 | return HashTable<Shape, Key>::Shrink(key); |
| 15458 | } |
| 15459 | |
| 15460 | |
| 15461 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15462 | MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15463 | int entry = this->FindEntry(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15464 | |
| 15465 | // If the entry is present set the value; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15466 | if (entry != Dictionary<Shape, Key>::kNotFound) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15467 | ValueAtPut(entry, value); |
| 15468 | return this; |
| 15469 | } |
| 15470 | |
| 15471 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15472 | Object* obj; |
| 15473 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15474 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15475 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15476 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15477 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15478 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15479 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 15480 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15481 | PropertyDetails details = PropertyDetails(NONE, NORMAL, 0); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15482 | |
| 15483 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 15484 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15485 | } |
| 15486 | |
| 15487 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15488 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15489 | MaybeObject* Dictionary<Shape, Key>::Add(Key key, |
| 15490 | Object* value, |
| 15491 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15492 | // Valdate key is absent. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15493 | SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15494 | // Check whether the dictionary should be extended. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15495 | Object* obj; |
| 15496 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 15497 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 15498 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15499 | |
| 15500 | return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details, |
| 15501 | Dictionary<Shape, Key>::Hash(key)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15502 | } |
| 15503 | |
| 15504 | |
| 15505 | // Add a key, value pair to the dictionary. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15506 | template<typename Shape, typename Key> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15507 | MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key, |
| 15508 | Object* value, |
| 15509 | PropertyDetails details, |
| 15510 | uint32_t hash) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15511 | // Compute the key object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15512 | Object* k; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15513 | { MaybeObject* maybe_k = Shape::AsObject(this->GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15514 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 15515 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15516 | |
| 15517 | uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15518 | // Insert element at empty or deleted entry |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15519 | if (!details.IsDeleted() && |
verwaest@chromium.org | 06ab2ec | 2012-10-09 17:00:13 +0000 | [diff] [blame] | 15520 | details.dictionary_index() == 0 && |
| 15521 | Shape::kIsEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15522 | // Assign an enumeration index to the property and update |
| 15523 | // SetNextEnumerationIndex. |
| 15524 | int index = NextEnumerationIndex(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15525 | details = PropertyDetails(details.attributes(), details.type(), index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15526 | SetNextEnumerationIndex(index + 1); |
| 15527 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15528 | SetEntry(entry, k, value, details); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15529 | ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber() || |
| 15530 | Dictionary<Shape, Key>::KeyAt(entry)->IsName())); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15531 | HashTable<Shape, Key>::ElementAdded(); |
| 15532 | return this; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15533 | } |
| 15534 | |
| 15535 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15536 | void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15537 | // If the dictionary requires slow elements an element has already |
| 15538 | // been added at a high index. |
| 15539 | if (requires_slow_elements()) return; |
| 15540 | // Check if this index is high enough that we should require slow |
| 15541 | // elements. |
| 15542 | if (key > kRequiresSlowElementsLimit) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 15543 | set_requires_slow_elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15544 | return; |
| 15545 | } |
| 15546 | // Update max key value. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 15547 | Object* max_index_object = get(kMaxNumberKeyIndex); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15548 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15549 | FixedArray::set(kMaxNumberKeyIndex, |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 15550 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15551 | } |
| 15552 | } |
| 15553 | |
machenbach@chromium.org | b5be0a9 | 2013-11-15 10:32:41 +0000 | [diff] [blame] | 15554 | Handle<SeededNumberDictionary> SeededNumberDictionary::AddNumberEntry( |
| 15555 | Handle<SeededNumberDictionary> dictionary, |
| 15556 | uint32_t key, |
| 15557 | Handle<Object> value, |
| 15558 | PropertyDetails details) { |
| 15559 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 15560 | dictionary->AddNumberEntry(key, *value, details), |
| 15561 | SeededNumberDictionary); |
| 15562 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15563 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15564 | MaybeObject* SeededNumberDictionary::AddNumberEntry(uint32_t key, |
| 15565 | Object* value, |
| 15566 | PropertyDetails details) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15567 | UpdateMaxNumberKey(key); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 15568 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15569 | return Add(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15570 | } |
| 15571 | |
| 15572 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15573 | MaybeObject* UnseededNumberDictionary::AddNumberEntry(uint32_t key, |
| 15574 | Object* value) { |
| 15575 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15576 | return Add(key, value, PropertyDetails(NONE, NORMAL, 0)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15577 | } |
| 15578 | |
| 15579 | |
| 15580 | MaybeObject* SeededNumberDictionary::AtNumberPut(uint32_t key, Object* value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15581 | UpdateMaxNumberKey(key); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15582 | return AtPut(key, value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15583 | } |
| 15584 | |
| 15585 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15586 | MaybeObject* UnseededNumberDictionary::AtNumberPut(uint32_t key, |
| 15587 | Object* value) { |
| 15588 | return AtPut(key, value); |
| 15589 | } |
| 15590 | |
| 15591 | |
| 15592 | Handle<SeededNumberDictionary> SeededNumberDictionary::Set( |
| 15593 | Handle<SeededNumberDictionary> dictionary, |
| 15594 | uint32_t index, |
| 15595 | Handle<Object> value, |
| 15596 | PropertyDetails details) { |
| 15597 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 15598 | dictionary->Set(index, *value, details), |
| 15599 | SeededNumberDictionary); |
| 15600 | } |
| 15601 | |
| 15602 | |
| 15603 | Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set( |
| 15604 | Handle<UnseededNumberDictionary> dictionary, |
| 15605 | uint32_t index, |
| 15606 | Handle<Object> value) { |
| 15607 | CALL_HEAP_FUNCTION(dictionary->GetIsolate(), |
| 15608 | dictionary->Set(index, *value), |
| 15609 | UnseededNumberDictionary); |
| 15610 | } |
| 15611 | |
| 15612 | |
| 15613 | MaybeObject* SeededNumberDictionary::Set(uint32_t key, |
| 15614 | Object* value, |
| 15615 | PropertyDetails details) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15616 | int entry = FindEntry(key); |
| 15617 | if (entry == kNotFound) return AddNumberEntry(key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15618 | // Preserve enumeration index. |
| 15619 | details = PropertyDetails(details.attributes(), |
| 15620 | details.type(), |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15621 | DetailsAt(entry).dictionary_index()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15622 | MaybeObject* maybe_object_key = |
| 15623 | SeededNumberDictionaryShape::AsObject(GetHeap(), key); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15624 | Object* object_key; |
| 15625 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 15626 | SetEntry(entry, object_key, value, details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15627 | return this; |
| 15628 | } |
| 15629 | |
| 15630 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15631 | MaybeObject* UnseededNumberDictionary::Set(uint32_t key, |
| 15632 | Object* value) { |
| 15633 | int entry = FindEntry(key); |
| 15634 | if (entry == kNotFound) return AddNumberEntry(key, value); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 15635 | MaybeObject* maybe_object_key = |
| 15636 | UnseededNumberDictionaryShape::AsObject(GetHeap(), key); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15637 | Object* object_key; |
| 15638 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
| 15639 | SetEntry(entry, object_key, value); |
| 15640 | return this; |
| 15641 | } |
| 15642 | |
| 15643 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 15644 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15645 | template<typename Shape, typename Key> |
| 15646 | int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes( |
| 15647 | PropertyAttributes filter) { |
| 15648 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15649 | int result = 0; |
| 15650 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15651 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15652 | if (HashTable<Shape, Key>::IsKey(k) && |
| 15653 | ((filter & SYMBOLIC) == 0 || !k->IsSymbol())) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15654 | PropertyDetails details = DetailsAt(i); |
| 15655 | if (details.IsDeleted()) continue; |
| 15656 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15657 | if ((attr & filter) == 0) result++; |
| 15658 | } |
| 15659 | } |
| 15660 | return result; |
| 15661 | } |
| 15662 | |
| 15663 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15664 | template<typename Shape, typename Key> |
| 15665 | int Dictionary<Shape, Key>::NumberOfEnumElements() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15666 | return NumberOfElementsFilterAttributes( |
| 15667 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 15668 | } |
| 15669 | |
| 15670 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15671 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15672 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 15673 | FixedArray* storage, |
| 15674 | PropertyAttributes filter, |
| 15675 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15676 | ASSERT(storage->length() >= NumberOfEnumElements()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15677 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15678 | int index = 0; |
| 15679 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15680 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15681 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15682 | PropertyDetails details = DetailsAt(i); |
| 15683 | if (details.IsDeleted()) continue; |
| 15684 | PropertyAttributes attr = details.attributes(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15685 | if ((attr & filter) == 0) storage->set(index++, k); |
| 15686 | } |
| 15687 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15688 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 15689 | storage->SortPairs(storage, index); |
| 15690 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15691 | ASSERT(storage->length() >= index); |
| 15692 | } |
| 15693 | |
| 15694 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15695 | FixedArray* NameDictionary::CopyEnumKeysTo(FixedArray* storage) { |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15696 | int length = storage->length(); |
| 15697 | ASSERT(length >= NumberOfEnumElements()); |
| 15698 | Heap* heap = GetHeap(); |
| 15699 | Object* undefined_value = heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15700 | int capacity = Capacity(); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15701 | int properties = 0; |
| 15702 | |
| 15703 | // Fill in the enumeration array by assigning enumerable keys at their |
| 15704 | // enumeration index. This will leave holes in the array if there are keys |
| 15705 | // that are deleted or not enumerable. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15706 | for (int i = 0; i < capacity; i++) { |
| 15707 | Object* k = KeyAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15708 | if (IsKey(k) && !k->IsSymbol()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15709 | PropertyDetails details = DetailsAt(i); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15710 | if (details.IsDeleted() || details.IsDontEnum()) continue; |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15711 | properties++; |
| 15712 | storage->set(details.dictionary_index() - 1, k); |
| 15713 | if (properties == length) break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15714 | } |
| 15715 | } |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15716 | |
| 15717 | // There are holes in the enumeration array if less properties were assigned |
| 15718 | // than the length of the array. If so, crunch all the existing properties |
| 15719 | // together by shifting them to the left (maintaining the enumeration order), |
| 15720 | // and trimming of the right side of the array. |
| 15721 | if (properties < length) { |
| 15722 | if (properties == 0) return heap->empty_fixed_array(); |
| 15723 | properties = 0; |
| 15724 | for (int i = 0; i < length; ++i) { |
| 15725 | Object* value = storage->get(i); |
| 15726 | if (value != undefined_value) { |
| 15727 | storage->set(properties, value); |
| 15728 | ++properties; |
| 15729 | } |
| 15730 | } |
| 15731 | RightTrimFixedArray<FROM_MUTATOR>(heap, storage, length - properties); |
| 15732 | } |
| 15733 | return storage; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15734 | } |
| 15735 | |
| 15736 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15737 | template<typename Shape, typename Key> |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15738 | void Dictionary<Shape, Key>::CopyKeysTo( |
| 15739 | FixedArray* storage, |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 15740 | int index, |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 15741 | PropertyAttributes filter, |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15742 | typename Dictionary<Shape, Key>::SortMode sort_mode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15743 | ASSERT(storage->length() >= NumberOfElementsFilterAttributes( |
| 15744 | static_cast<PropertyAttributes>(NONE))); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15745 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15746 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15747 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15748 | if (HashTable<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 15749 | PropertyDetails details = DetailsAt(i); |
| 15750 | if (details.IsDeleted()) continue; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 15751 | PropertyAttributes attr = details.attributes(); |
| 15752 | if ((attr & filter) == 0) storage->set(index++, k); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15753 | } |
| 15754 | } |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 15755 | if (sort_mode == Dictionary<Shape, Key>::SORTED) { |
| 15756 | storage->SortPairs(storage, index); |
| 15757 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15758 | ASSERT(storage->length() >= index); |
| 15759 | } |
| 15760 | |
| 15761 | |
| 15762 | // Backwards lookup (slow). |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15763 | template<typename Shape, typename Key> |
| 15764 | Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) { |
| 15765 | int capacity = HashTable<Shape, Key>::Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15766 | for (int i = 0; i < capacity; i++) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15767 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 15768 | if (Dictionary<Shape, Key>::IsKey(k)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15769 | Object* e = ValueAt(i); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 15770 | if (e->IsPropertyCell()) { |
| 15771 | e = PropertyCell::cast(e)->value(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15772 | } |
| 15773 | if (e == value) return k; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15774 | } |
| 15775 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15776 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15777 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15778 | } |
| 15779 | |
| 15780 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15781 | MaybeObject* NameDictionary::TransformPropertiesToFastFor( |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 15782 | JSObject* obj, int unused_property_fields) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15783 | // Make sure we preserve dictionary representation if there are too many |
| 15784 | // descriptors. |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15785 | int number_of_elements = NumberOfElements(); |
machenbach@chromium.org | af9cfcb | 2013-11-19 11:05:18 +0000 | [diff] [blame^] | 15786 | if (number_of_elements > kMaxNumberOfDescriptors) return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15787 | |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 15788 | if (number_of_elements != NextEnumerationIndex()) { |
| 15789 | MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 15790 | if (maybe_result->IsFailure()) return maybe_result; |
| 15791 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15792 | |
| 15793 | int instance_descriptor_length = 0; |
| 15794 | int number_of_fields = 0; |
| 15795 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 15796 | Heap* heap = GetHeap(); |
| 15797 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15798 | // Compute the length of the instance descriptor. |
| 15799 | int capacity = Capacity(); |
| 15800 | for (int i = 0; i < capacity; i++) { |
| 15801 | Object* k = KeyAt(i); |
| 15802 | if (IsKey(k)) { |
| 15803 | Object* value = ValueAt(i); |
| 15804 | PropertyType type = DetailsAt(i).type(); |
| 15805 | ASSERT(type != FIELD); |
| 15806 | instance_descriptor_length++; |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 15807 | if (type == NORMAL && !value->IsJSFunction()) { |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 15808 | number_of_fields += 1; |
| 15809 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15810 | } |
| 15811 | } |
| 15812 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15813 | int inobject_props = obj->map()->inobject_properties(); |
| 15814 | |
| 15815 | // Allocate new map. |
| 15816 | Map* new_map; |
| 15817 | MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors(); |
| 15818 | 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] | 15819 | new_map->set_dictionary_map(false); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15820 | |
| 15821 | if (instance_descriptor_length == 0) { |
| 15822 | ASSERT_LE(unused_property_fields, inobject_props); |
| 15823 | // Transform the object. |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 15824 | new_map->set_unused_property_fields(inobject_props); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 15825 | obj->set_map(new_map); |
| 15826 | obj->set_properties(heap->empty_fixed_array()); |
| 15827 | // Check that it really works. |
| 15828 | ASSERT(obj->HasFastProperties()); |
| 15829 | return obj; |
| 15830 | } |
| 15831 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15832 | // Allocate the instance descriptor. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15833 | DescriptorArray* descriptors; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15834 | MaybeObject* maybe_descriptors = |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15835 | DescriptorArray::Allocate(GetIsolate(), instance_descriptor_length); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15836 | if (!maybe_descriptors->To(&descriptors)) { |
| 15837 | return maybe_descriptors; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 15838 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15839 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 15840 | DescriptorArray::WhitenessWitness witness(descriptors); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15841 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15842 | int number_of_allocated_fields = |
| 15843 | number_of_fields + unused_property_fields - inobject_props; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 15844 | if (number_of_allocated_fields < 0) { |
| 15845 | // There is enough inobject space for all fields (including unused). |
| 15846 | number_of_allocated_fields = 0; |
| 15847 | unused_property_fields = inobject_props - number_of_fields; |
| 15848 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15849 | |
| 15850 | // Allocate the fixed array for the fields. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15851 | FixedArray* fields; |
| 15852 | MaybeObject* maybe_fields = |
| 15853 | heap->AllocateFixedArray(number_of_allocated_fields); |
| 15854 | if (!maybe_fields->To(&fields)) return maybe_fields; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15855 | |
| 15856 | // Fill in the instance descriptor and the fields. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15857 | int current_offset = 0; |
| 15858 | for (int i = 0; i < capacity; i++) { |
| 15859 | Object* k = KeyAt(i); |
| 15860 | if (IsKey(k)) { |
| 15861 | Object* value = ValueAt(i); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 15862 | Name* key; |
| 15863 | if (k->IsSymbol()) { |
| 15864 | key = Symbol::cast(k); |
| 15865 | } else { |
| 15866 | // Ensure the key is a unique name before writing into the |
| 15867 | // instance descriptor. |
| 15868 | MaybeObject* maybe_key = heap->InternalizeString(String::cast(k)); |
| 15869 | if (!maybe_key->To(&key)) return maybe_key; |
| 15870 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15871 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15872 | PropertyDetails details = DetailsAt(i); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15873 | int enumeration_index = details.dictionary_index(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15874 | PropertyType type = details.type(); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15875 | |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 15876 | if (value->IsJSFunction()) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 15877 | ConstantDescriptor d(key, value, details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15878 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15879 | } else if (type == NORMAL) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15880 | if (current_offset < inobject_props) { |
| 15881 | obj->InObjectPropertyAtPut(current_offset, |
| 15882 | value, |
| 15883 | UPDATE_WRITE_BARRIER); |
| 15884 | } else { |
| 15885 | int offset = current_offset - inobject_props; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15886 | fields->set(offset, value); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15887 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15888 | FieldDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15889 | current_offset++, |
| 15890 | details.attributes(), |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15891 | // TODO(verwaest): value->OptimalRepresentation(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15892 | Representation::Tagged()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15893 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15894 | } else if (type == CALLBACKS) { |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15895 | CallbacksDescriptor d(key, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15896 | value, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15897 | details.attributes()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15898 | descriptors->Set(enumeration_index - 1, &d, witness); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15899 | } else { |
| 15900 | UNREACHABLE(); |
| 15901 | } |
| 15902 | } |
| 15903 | } |
| 15904 | ASSERT(current_offset == number_of_fields); |
| 15905 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 15906 | descriptors->Sort(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15907 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 15908 | new_map->InitializeDescriptors(descriptors); |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 15909 | new_map->set_unused_property_fields(unused_property_fields); |
| 15910 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15911 | // Transform the object. |
rossberg@chromium.org | 657d53b | 2012-07-12 11:06:03 +0000 | [diff] [blame] | 15912 | obj->set_map(new_map); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 15913 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 15914 | obj->set_properties(fields); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15915 | ASSERT(obj->IsJSObject()); |
| 15916 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15917 | // Check that it really works. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15918 | ASSERT(obj->HasFastProperties()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 15919 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 15920 | return obj; |
| 15921 | } |
| 15922 | |
| 15923 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15924 | Handle<ObjectHashSet> ObjectHashSet::EnsureCapacity( |
| 15925 | Handle<ObjectHashSet> table, |
| 15926 | int n, |
| 15927 | Handle<Object> key, |
| 15928 | PretenureFlag pretenure) { |
| 15929 | Handle<HashTable<ObjectHashTableShape<1>, Object*> > table_base = table; |
| 15930 | CALL_HEAP_FUNCTION(table_base->GetIsolate(), |
| 15931 | table_base->EnsureCapacity(n, *key, pretenure), |
| 15932 | ObjectHashSet); |
| 15933 | } |
| 15934 | |
| 15935 | |
| 15936 | Handle<ObjectHashSet> ObjectHashSet::Shrink(Handle<ObjectHashSet> table, |
| 15937 | Handle<Object> key) { |
| 15938 | Handle<HashTable<ObjectHashTableShape<1>, Object*> > table_base = table; |
| 15939 | CALL_HEAP_FUNCTION(table_base->GetIsolate(), |
| 15940 | table_base->Shrink(*key), |
| 15941 | ObjectHashSet); |
| 15942 | } |
| 15943 | |
| 15944 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15945 | bool ObjectHashSet::Contains(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15946 | ASSERT(IsKey(key)); |
| 15947 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 15948 | // If the object does not have an identity hash, it was never used as a key. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15949 | Object* hash = key->GetHash(); |
| 15950 | if (hash->IsUndefined()) return false; |
| 15951 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15952 | return (FindEntry(key) != kNotFound); |
| 15953 | } |
| 15954 | |
| 15955 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15956 | Handle<ObjectHashSet> ObjectHashSet::Add(Handle<ObjectHashSet> table, |
| 15957 | Handle<Object> key) { |
| 15958 | ASSERT(table->IsKey(*key)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15959 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15960 | // Make sure the key object has an identity hash code. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15961 | Handle<Object> object_hash = Object::GetOrCreateHash(key, |
| 15962 | table->GetIsolate()); |
| 15963 | |
| 15964 | int entry = table->FindEntry(*key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15965 | |
| 15966 | // Check whether key is already present. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15967 | if (entry != kNotFound) return table; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15968 | |
| 15969 | // Check whether the hash set should be extended and add entry. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15970 | Handle<ObjectHashSet> new_table = |
| 15971 | ObjectHashSet::EnsureCapacity(table, 1, key); |
| 15972 | entry = new_table->FindInsertionEntry(Smi::cast(*object_hash)->value()); |
| 15973 | new_table->set(EntryToIndex(entry), *key); |
| 15974 | new_table->ElementAdded(); |
| 15975 | return new_table; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15976 | } |
| 15977 | |
| 15978 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15979 | Handle<ObjectHashSet> ObjectHashSet::Remove(Handle<ObjectHashSet> table, |
| 15980 | Handle<Object> key) { |
| 15981 | ASSERT(table->IsKey(*key)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 15982 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15983 | // If the object does not have an identity hash, it was never used as a key. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15984 | if (key->GetHash()->IsUndefined()) return table; |
| 15985 | |
| 15986 | int entry = table->FindEntry(*key); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15987 | |
| 15988 | // Check whether key is actually present. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15989 | if (entry == kNotFound) return table; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 15990 | |
| 15991 | // Remove entry and try to shrink this hash set. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 15992 | table->set_the_hole(EntryToIndex(entry)); |
| 15993 | table->ElementRemoved(); |
| 15994 | |
| 15995 | return ObjectHashSet::Shrink(table, key); |
| 15996 | } |
| 15997 | |
| 15998 | |
| 15999 | Handle<ObjectHashTable> ObjectHashTable::EnsureCapacity( |
| 16000 | Handle<ObjectHashTable> table, |
| 16001 | int n, |
| 16002 | Handle<Object> key, |
| 16003 | PretenureFlag pretenure) { |
| 16004 | Handle<HashTable<ObjectHashTableShape<2>, Object*> > table_base = table; |
| 16005 | CALL_HEAP_FUNCTION(table_base->GetIsolate(), |
| 16006 | table_base->EnsureCapacity(n, *key, pretenure), |
| 16007 | ObjectHashTable); |
| 16008 | } |
| 16009 | |
| 16010 | |
| 16011 | Handle<ObjectHashTable> ObjectHashTable::Shrink( |
| 16012 | Handle<ObjectHashTable> table, Handle<Object> key) { |
| 16013 | Handle<HashTable<ObjectHashTableShape<2>, Object*> > table_base = table; |
| 16014 | CALL_HEAP_FUNCTION(table_base->GetIsolate(), |
| 16015 | table_base->Shrink(*key), |
| 16016 | ObjectHashTable); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 16017 | } |
| 16018 | |
| 16019 | |
| 16020 | Object* ObjectHashTable::Lookup(Object* key) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 16021 | ASSERT(IsKey(key)); |
| 16022 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 16023 | // If the object does not have an identity hash, it was never used as a key. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16024 | Object* hash = key->GetHash(); |
| 16025 | if (hash->IsUndefined()) { |
| 16026 | return GetHeap()->the_hole_value(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 16027 | } |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16028 | int entry = FindEntry(key); |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 16029 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16030 | return get(EntryToIndex(entry) + 1); |
| 16031 | } |
| 16032 | |
| 16033 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16034 | Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table, |
| 16035 | Handle<Object> key, |
| 16036 | Handle<Object> value) { |
| 16037 | ASSERT(table->IsKey(*key)); |
| 16038 | |
| 16039 | Isolate* isolate = table->GetIsolate(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 16040 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16041 | // Make sure the key object has an identity hash code. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16042 | Handle<Object> hash = Object::GetOrCreateHash(key, isolate); |
| 16043 | |
| 16044 | int entry = table->FindEntry(*key); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16045 | |
| 16046 | // Check whether to perform removal operation. |
jkummerow@chromium.org | 7a6fc81 | 2012-06-27 11:12:38 +0000 | [diff] [blame] | 16047 | if (value->IsTheHole()) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16048 | if (entry == kNotFound) return table; |
| 16049 | table->RemoveEntry(entry); |
| 16050 | return Shrink(table, key); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16051 | } |
| 16052 | |
| 16053 | // Key is already in table, just overwrite value. |
| 16054 | if (entry != kNotFound) { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16055 | table->set(EntryToIndex(entry) + 1, *value); |
| 16056 | return table; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16057 | } |
| 16058 | |
| 16059 | // Check whether the hash table should be extended. |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 16060 | table = EnsureCapacity(table, 1, key); |
| 16061 | table->AddEntry(table->FindInsertionEntry(Handle<Smi>::cast(hash)->value()), |
| 16062 | *key, |
| 16063 | *value); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16064 | return table; |
| 16065 | } |
| 16066 | |
| 16067 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 16068 | void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) { |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16069 | set(EntryToIndex(entry), key); |
| 16070 | set(EntryToIndex(entry) + 1, value); |
| 16071 | ElementAdded(); |
| 16072 | } |
| 16073 | |
| 16074 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 16075 | void ObjectHashTable::RemoveEntry(int entry) { |
| 16076 | set_the_hole(EntryToIndex(entry)); |
| 16077 | set_the_hole(EntryToIndex(entry) + 1); |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 16078 | ElementRemoved(); |
| 16079 | } |
| 16080 | |
| 16081 | |
jkummerow@chromium.org | 25b0e21 | 2013-10-04 15:38:52 +0000 | [diff] [blame] | 16082 | Object* WeakHashTable::Lookup(Object* key) { |
| 16083 | ASSERT(IsKey(key)); |
| 16084 | int entry = FindEntry(key); |
| 16085 | if (entry == kNotFound) return GetHeap()->the_hole_value(); |
| 16086 | return get(EntryToValueIndex(entry)); |
| 16087 | } |
| 16088 | |
| 16089 | |
| 16090 | MaybeObject* WeakHashTable::Put(Object* key, Object* value) { |
| 16091 | ASSERT(IsKey(key)); |
| 16092 | int entry = FindEntry(key); |
| 16093 | // Key is already in table, just overwrite value. |
| 16094 | if (entry != kNotFound) { |
| 16095 | set(EntryToValueIndex(entry), value); |
| 16096 | return this; |
| 16097 | } |
| 16098 | |
| 16099 | // Check whether the hash table should be extended. |
| 16100 | Object* obj; |
| 16101 | { MaybeObject* maybe_obj = EnsureCapacity(1, key, TENURED); |
| 16102 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 16103 | } |
| 16104 | WeakHashTable* table = WeakHashTable::cast(obj); |
| 16105 | table->AddEntry(table->FindInsertionEntry(Hash(key)), key, value); |
| 16106 | return table; |
| 16107 | } |
| 16108 | |
| 16109 | |
| 16110 | void WeakHashTable::AddEntry(int entry, Object* key, Object* value) { |
| 16111 | set(EntryToIndex(entry), key); |
| 16112 | set(EntryToValueIndex(entry), value); |
| 16113 | ElementAdded(); |
| 16114 | } |
| 16115 | |
| 16116 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 16117 | DeclaredAccessorDescriptorIterator::DeclaredAccessorDescriptorIterator( |
| 16118 | DeclaredAccessorDescriptor* descriptor) |
| 16119 | : array_(descriptor->serialized_data()->GetDataStartAddress()), |
| 16120 | length_(descriptor->serialized_data()->length()), |
| 16121 | offset_(0) { |
| 16122 | } |
| 16123 | |
| 16124 | |
| 16125 | const DeclaredAccessorDescriptorData* |
| 16126 | DeclaredAccessorDescriptorIterator::Next() { |
| 16127 | ASSERT(offset_ < length_); |
| 16128 | uint8_t* ptr = &array_[offset_]; |
| 16129 | ASSERT(reinterpret_cast<uintptr_t>(ptr) % sizeof(uintptr_t) == 0); |
| 16130 | const DeclaredAccessorDescriptorData* data = |
| 16131 | reinterpret_cast<const DeclaredAccessorDescriptorData*>(ptr); |
| 16132 | offset_ += sizeof(*data); |
| 16133 | ASSERT(offset_ <= length_); |
| 16134 | return data; |
| 16135 | } |
| 16136 | |
| 16137 | |
| 16138 | Handle<DeclaredAccessorDescriptor> DeclaredAccessorDescriptor::Create( |
| 16139 | Isolate* isolate, |
| 16140 | const DeclaredAccessorDescriptorData& descriptor, |
| 16141 | Handle<DeclaredAccessorDescriptor> previous) { |
| 16142 | int previous_length = |
| 16143 | previous.is_null() ? 0 : previous->serialized_data()->length(); |
| 16144 | int length = sizeof(descriptor) + previous_length; |
| 16145 | Handle<ByteArray> serialized_descriptor = |
| 16146 | isolate->factory()->NewByteArray(length); |
| 16147 | Handle<DeclaredAccessorDescriptor> value = |
| 16148 | isolate->factory()->NewDeclaredAccessorDescriptor(); |
| 16149 | value->set_serialized_data(*serialized_descriptor); |
| 16150 | // Copy in the data. |
| 16151 | { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 16152 | DisallowHeapAllocation no_allocation; |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 16153 | uint8_t* array = serialized_descriptor->GetDataStartAddress(); |
| 16154 | if (previous_length != 0) { |
| 16155 | uint8_t* previous_array = |
| 16156 | previous->serialized_data()->GetDataStartAddress(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 16157 | OS::MemCopy(array, previous_array, previous_length); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 16158 | array += previous_length; |
| 16159 | } |
| 16160 | ASSERT(reinterpret_cast<uintptr_t>(array) % sizeof(uintptr_t) == 0); |
| 16161 | DeclaredAccessorDescriptorData* data = |
| 16162 | reinterpret_cast<DeclaredAccessorDescriptorData*>(array); |
| 16163 | *data = descriptor; |
| 16164 | } |
| 16165 | return value; |
| 16166 | } |
| 16167 | |
| 16168 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 16169 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16170 | // Check if there is a break point at this code position. |
| 16171 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 16172 | // Get the break point info object for this code position. |
| 16173 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 16174 | |
| 16175 | // If there is no break point info object or no break points in the break |
| 16176 | // point info object there is no break point at this code position. |
| 16177 | if (break_point_info->IsUndefined()) return false; |
| 16178 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 16179 | } |
| 16180 | |
| 16181 | |
| 16182 | // Get the break point info object for this code position. |
| 16183 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 16184 | // Find the index of the break point info object for this code position. |
| 16185 | int index = GetBreakPointInfoIndex(code_position); |
| 16186 | |
| 16187 | // Return the break point info object if any. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 16188 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16189 | return BreakPointInfo::cast(break_points()->get(index)); |
| 16190 | } |
| 16191 | |
| 16192 | |
| 16193 | // Clear a break point at the specified code position. |
| 16194 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 16195 | int code_position, |
| 16196 | Handle<Object> break_point_object) { |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 16197 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 16198 | debug_info->GetIsolate()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16199 | if (break_point_info->IsUndefined()) return; |
| 16200 | BreakPointInfo::ClearBreakPoint( |
| 16201 | Handle<BreakPointInfo>::cast(break_point_info), |
| 16202 | break_point_object); |
| 16203 | } |
| 16204 | |
| 16205 | |
| 16206 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 16207 | int code_position, |
| 16208 | int source_position, |
| 16209 | int statement_position, |
| 16210 | Handle<Object> break_point_object) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 16211 | Isolate* isolate = debug_info->GetIsolate(); |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 16212 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position), |
| 16213 | isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16214 | if (!break_point_info->IsUndefined()) { |
| 16215 | BreakPointInfo::SetBreakPoint( |
| 16216 | Handle<BreakPointInfo>::cast(break_point_info), |
| 16217 | break_point_object); |
| 16218 | return; |
| 16219 | } |
| 16220 | |
| 16221 | // Adding a new break point for a code position which did not have any |
| 16222 | // break points before. Try to find a free slot. |
| 16223 | int index = kNoBreakPointInfo; |
| 16224 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 16225 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 16226 | index = i; |
| 16227 | break; |
| 16228 | } |
| 16229 | } |
| 16230 | if (index == kNoBreakPointInfo) { |
| 16231 | // No free slot - extend break point info array. |
| 16232 | Handle<FixedArray> old_break_points = |
| 16233 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16234 | Handle<FixedArray> new_break_points = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16235 | isolate->factory()->NewFixedArray( |
| 16236 | old_break_points->length() + |
| 16237 | Debug::kEstimatedNofBreakPointsInFunction); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 16238 | |
| 16239 | debug_info->set_break_points(*new_break_points); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16240 | for (int i = 0; i < old_break_points->length(); i++) { |
| 16241 | new_break_points->set(i, old_break_points->get(i)); |
| 16242 | } |
| 16243 | index = old_break_points->length(); |
| 16244 | } |
| 16245 | ASSERT(index != kNoBreakPointInfo); |
| 16246 | |
| 16247 | // Allocate new BreakPointInfo object and set the break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16248 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 16249 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16250 | new_break_point_info->set_code_position(Smi::FromInt(code_position)); |
| 16251 | new_break_point_info->set_source_position(Smi::FromInt(source_position)); |
| 16252 | new_break_point_info-> |
| 16253 | set_statement_position(Smi::FromInt(statement_position)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16254 | new_break_point_info->set_break_point_objects( |
| 16255 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16256 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 16257 | debug_info->break_points()->set(index, *new_break_point_info); |
| 16258 | } |
| 16259 | |
| 16260 | |
| 16261 | // Get the break point objects for a code position. |
| 16262 | Object* DebugInfo::GetBreakPointObjects(int code_position) { |
| 16263 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 16264 | if (break_point_info->IsUndefined()) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 16265 | return GetHeap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16266 | } |
| 16267 | return BreakPointInfo::cast(break_point_info)->break_point_objects(); |
| 16268 | } |
| 16269 | |
| 16270 | |
| 16271 | // Get the total number of break points. |
| 16272 | int DebugInfo::GetBreakPointCount() { |
| 16273 | if (break_points()->IsUndefined()) return 0; |
| 16274 | int count = 0; |
| 16275 | for (int i = 0; i < break_points()->length(); i++) { |
| 16276 | if (!break_points()->get(i)->IsUndefined()) { |
| 16277 | BreakPointInfo* break_point_info = |
| 16278 | BreakPointInfo::cast(break_points()->get(i)); |
| 16279 | count += break_point_info->GetBreakPointCount(); |
| 16280 | } |
| 16281 | } |
| 16282 | return count; |
| 16283 | } |
| 16284 | |
| 16285 | |
| 16286 | Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 16287 | Handle<Object> break_point_object) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 16288 | Heap* heap = debug_info->GetHeap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16289 | if (debug_info->break_points()->IsUndefined()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16290 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 16291 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 16292 | Handle<BreakPointInfo> break_point_info = |
| 16293 | Handle<BreakPointInfo>(BreakPointInfo::cast( |
| 16294 | debug_info->break_points()->get(i))); |
| 16295 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 16296 | break_point_object)) { |
| 16297 | return *break_point_info; |
| 16298 | } |
| 16299 | } |
| 16300 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16301 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16302 | } |
| 16303 | |
| 16304 | |
| 16305 | // Find the index of the break point info object for the specified code |
| 16306 | // position. |
| 16307 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 16308 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 16309 | for (int i = 0; i < break_points()->length(); i++) { |
| 16310 | if (!break_points()->get(i)->IsUndefined()) { |
| 16311 | BreakPointInfo* break_point_info = |
| 16312 | BreakPointInfo::cast(break_points()->get(i)); |
| 16313 | if (break_point_info->code_position()->value() == code_position) { |
| 16314 | return i; |
| 16315 | } |
| 16316 | } |
| 16317 | } |
| 16318 | return kNoBreakPointInfo; |
| 16319 | } |
| 16320 | |
| 16321 | |
| 16322 | // Remove the specified break point object. |
| 16323 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 16324 | Handle<Object> break_point_object) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 16325 | Isolate* isolate = break_point_info->GetIsolate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16326 | // If there are no break points just ignore. |
| 16327 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 16328 | // If there is a single break point clear it if it is the same. |
| 16329 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 16330 | if (break_point_info->break_point_objects() == *break_point_object) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16331 | break_point_info->set_break_point_objects( |
| 16332 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16333 | } |
| 16334 | return; |
| 16335 | } |
| 16336 | // If there are multiple break points shrink the array |
| 16337 | ASSERT(break_point_info->break_point_objects()->IsFixedArray()); |
| 16338 | Handle<FixedArray> old_array = |
| 16339 | Handle<FixedArray>( |
| 16340 | FixedArray::cast(break_point_info->break_point_objects())); |
| 16341 | Handle<FixedArray> new_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16342 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16343 | int found_count = 0; |
| 16344 | for (int i = 0; i < old_array->length(); i++) { |
| 16345 | if (old_array->get(i) == *break_point_object) { |
| 16346 | ASSERT(found_count == 0); |
| 16347 | found_count++; |
| 16348 | } else { |
| 16349 | new_array->set(i - found_count, old_array->get(i)); |
| 16350 | } |
| 16351 | } |
| 16352 | // If the break point was found in the list change it. |
| 16353 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 16354 | } |
| 16355 | |
| 16356 | |
| 16357 | // Add the specified break point object. |
| 16358 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 16359 | Handle<Object> break_point_object) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16360 | Isolate* isolate = break_point_info->GetIsolate(); |
| 16361 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16362 | // If there was no break point objects before just set it. |
| 16363 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 16364 | break_point_info->set_break_point_objects(*break_point_object); |
| 16365 | return; |
| 16366 | } |
| 16367 | // If the break point object is the same as before just ignore. |
| 16368 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 16369 | // If there was one break point object before replace with array. |
| 16370 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16371 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16372 | array->set(0, break_point_info->break_point_objects()); |
| 16373 | array->set(1, *break_point_object); |
| 16374 | break_point_info->set_break_point_objects(*array); |
| 16375 | return; |
| 16376 | } |
| 16377 | // If there was more than one break point before extend array. |
| 16378 | Handle<FixedArray> old_array = |
| 16379 | Handle<FixedArray>( |
| 16380 | FixedArray::cast(break_point_info->break_point_objects())); |
| 16381 | Handle<FixedArray> new_array = |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16382 | isolate->factory()->NewFixedArray(old_array->length() + 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16383 | for (int i = 0; i < old_array->length(); i++) { |
| 16384 | // If the break point was there before just ignore. |
| 16385 | if (old_array->get(i) == *break_point_object) return; |
| 16386 | new_array->set(i, old_array->get(i)); |
| 16387 | } |
| 16388 | // Add the new break point. |
| 16389 | new_array->set(old_array->length(), *break_point_object); |
| 16390 | break_point_info->set_break_point_objects(*new_array); |
| 16391 | } |
| 16392 | |
| 16393 | |
| 16394 | bool BreakPointInfo::HasBreakPointObject( |
| 16395 | Handle<BreakPointInfo> break_point_info, |
| 16396 | Handle<Object> break_point_object) { |
| 16397 | // No break point. |
| 16398 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 16399 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16400 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 16401 | return break_point_info->break_point_objects() == *break_point_object; |
| 16402 | } |
| 16403 | // Multiple break points. |
| 16404 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 16405 | for (int i = 0; i < array->length(); i++) { |
| 16406 | if (array->get(i) == *break_point_object) { |
| 16407 | return true; |
| 16408 | } |
| 16409 | } |
| 16410 | return false; |
| 16411 | } |
| 16412 | |
| 16413 | |
| 16414 | // Get the number of break points. |
| 16415 | int BreakPointInfo::GetBreakPointCount() { |
| 16416 | // No break point. |
| 16417 | if (break_point_objects()->IsUndefined()) return 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 16418 | // Single break point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16419 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 16420 | // Multiple break points. |
| 16421 | return FixedArray::cast(break_point_objects())->length(); |
| 16422 | } |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 16423 | #endif // ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16424 | |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 16425 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 16426 | Object* JSDate::GetField(Object* object, Smi* index) { |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16427 | return JSDate::cast(object)->DoGetField( |
| 16428 | static_cast<FieldIndex>(index->value())); |
| 16429 | } |
| 16430 | |
| 16431 | |
| 16432 | Object* JSDate::DoGetField(FieldIndex index) { |
| 16433 | ASSERT(index != kDateValue); |
| 16434 | |
| 16435 | DateCache* date_cache = GetIsolate()->date_cache(); |
| 16436 | |
| 16437 | if (index < kFirstUncachedField) { |
| 16438 | Object* stamp = cache_stamp(); |
| 16439 | if (stamp != date_cache->stamp() && stamp->IsSmi()) { |
| 16440 | // Since the stamp is not NaN, the value is also not NaN. |
| 16441 | int64_t local_time_ms = |
| 16442 | date_cache->ToLocal(static_cast<int64_t>(value()->Number())); |
| 16443 | SetLocalFields(local_time_ms, date_cache); |
| 16444 | } |
| 16445 | switch (index) { |
| 16446 | case kYear: return year(); |
| 16447 | case kMonth: return month(); |
| 16448 | case kDay: return day(); |
| 16449 | case kWeekday: return weekday(); |
| 16450 | case kHour: return hour(); |
| 16451 | case kMinute: return min(); |
| 16452 | case kSecond: return sec(); |
| 16453 | default: UNREACHABLE(); |
| 16454 | } |
| 16455 | } |
| 16456 | |
| 16457 | if (index >= kFirstUTCField) { |
| 16458 | return GetUTCField(index, value()->Number(), date_cache); |
| 16459 | } |
| 16460 | |
| 16461 | double time = value()->Number(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 16462 | if (std::isnan(time)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16463 | |
| 16464 | int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time)); |
| 16465 | int days = DateCache::DaysFromTime(local_time_ms); |
| 16466 | |
| 16467 | if (index == kDays) return Smi::FromInt(days); |
| 16468 | |
| 16469 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 16470 | if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000); |
| 16471 | ASSERT(index == kTimeInDay); |
| 16472 | return Smi::FromInt(time_in_day_ms); |
| 16473 | } |
| 16474 | |
| 16475 | |
| 16476 | Object* JSDate::GetUTCField(FieldIndex index, |
| 16477 | double value, |
| 16478 | DateCache* date_cache) { |
| 16479 | ASSERT(index >= kFirstUTCField); |
| 16480 | |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 16481 | if (std::isnan(value)) return GetIsolate()->heap()->nan_value(); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 16482 | |
| 16483 | int64_t time_ms = static_cast<int64_t>(value); |
| 16484 | |
| 16485 | if (index == kTimezoneOffset) { |
| 16486 | return Smi::FromInt(date_cache->TimezoneOffset(time_ms)); |
| 16487 | } |
| 16488 | |
| 16489 | int days = DateCache::DaysFromTime(time_ms); |
| 16490 | |
| 16491 | if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days)); |
| 16492 | |
| 16493 | if (index <= kDayUTC) { |
| 16494 | int year, month, day; |
| 16495 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 16496 | if (index == kYearUTC) return Smi::FromInt(year); |
| 16497 | if (index == kMonthUTC) return Smi::FromInt(month); |
| 16498 | ASSERT(index == kDayUTC); |
| 16499 | return Smi::FromInt(day); |
| 16500 | } |
| 16501 | |
| 16502 | int time_in_day_ms = DateCache::TimeInDay(time_ms, days); |
| 16503 | switch (index) { |
| 16504 | case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000)); |
| 16505 | case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60); |
| 16506 | case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60); |
| 16507 | case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000); |
| 16508 | case kDaysUTC: return Smi::FromInt(days); |
| 16509 | case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms); |
| 16510 | default: UNREACHABLE(); |
| 16511 | } |
| 16512 | |
| 16513 | UNREACHABLE(); |
| 16514 | return NULL; |
| 16515 | } |
| 16516 | |
| 16517 | |
| 16518 | void JSDate::SetValue(Object* value, bool is_value_nan) { |
| 16519 | set_value(value); |
| 16520 | if (is_value_nan) { |
| 16521 | HeapNumber* nan = GetIsolate()->heap()->nan_value(); |
| 16522 | set_cache_stamp(nan, SKIP_WRITE_BARRIER); |
| 16523 | set_year(nan, SKIP_WRITE_BARRIER); |
| 16524 | set_month(nan, SKIP_WRITE_BARRIER); |
| 16525 | set_day(nan, SKIP_WRITE_BARRIER); |
| 16526 | set_hour(nan, SKIP_WRITE_BARRIER); |
| 16527 | set_min(nan, SKIP_WRITE_BARRIER); |
| 16528 | set_sec(nan, SKIP_WRITE_BARRIER); |
| 16529 | set_weekday(nan, SKIP_WRITE_BARRIER); |
| 16530 | } else { |
| 16531 | set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER); |
| 16532 | } |
| 16533 | } |
| 16534 | |
| 16535 | |
| 16536 | void JSDate::SetLocalFields(int64_t local_time_ms, DateCache* date_cache) { |
| 16537 | int days = DateCache::DaysFromTime(local_time_ms); |
| 16538 | int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days); |
| 16539 | int year, month, day; |
| 16540 | date_cache->YearMonthDayFromDays(days, &year, &month, &day); |
| 16541 | int weekday = date_cache->Weekday(days); |
| 16542 | int hour = time_in_day_ms / (60 * 60 * 1000); |
| 16543 | int min = (time_in_day_ms / (60 * 1000)) % 60; |
| 16544 | int sec = (time_in_day_ms / 1000) % 60; |
| 16545 | set_cache_stamp(date_cache->stamp()); |
| 16546 | set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
| 16547 | set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
| 16548 | set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
| 16549 | set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
| 16550 | set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
| 16551 | set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
| 16552 | set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
| 16553 | } |
| 16554 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16555 | |
| 16556 | void JSArrayBuffer::Neuter() { |
| 16557 | ASSERT(is_external()); |
| 16558 | set_backing_store(NULL); |
| 16559 | set_byte_length(Smi::FromInt(0)); |
| 16560 | } |
| 16561 | |
| 16562 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16563 | void JSArrayBufferView::NeuterView() { |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16564 | set_byte_offset(Smi::FromInt(0)); |
| 16565 | set_byte_length(Smi::FromInt(0)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16566 | } |
| 16567 | |
| 16568 | |
| 16569 | void JSDataView::Neuter() { |
| 16570 | NeuterView(); |
| 16571 | } |
| 16572 | |
| 16573 | |
| 16574 | void JSTypedArray::Neuter() { |
| 16575 | NeuterView(); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 16576 | set_length(Smi::FromInt(0)); |
| 16577 | set_elements(GetHeap()->EmptyExternalArrayForMap(map())); |
| 16578 | } |
| 16579 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16580 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 16581 | Type* PropertyCell::type() { |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16582 | return static_cast<Type*>(type_raw()); |
| 16583 | } |
| 16584 | |
| 16585 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 16586 | void PropertyCell::set_type(Type* type, WriteBarrierMode ignored) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16587 | ASSERT(IsPropertyCell()); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 16588 | set_type_raw(type, ignored); |
| 16589 | } |
| 16590 | |
| 16591 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 16592 | Handle<Type> PropertyCell::UpdatedType(Handle<PropertyCell> cell, |
| 16593 | Handle<Object> value) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16594 | Isolate* isolate = cell->GetIsolate(); |
| 16595 | Handle<Type> old_type(cell->type(), isolate); |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 16596 | // TODO(2803): Do not track ConsString as constant because they cannot be |
| 16597 | // embedded into code. |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 16598 | Handle<Type> new_type(value->IsConsString() || value->IsTheHole() |
| 16599 | ? Type::Any() |
| 16600 | : Type::Constant(value, isolate), isolate); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16601 | |
| 16602 | if (new_type->Is(old_type)) { |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 16603 | return old_type; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16604 | } |
| 16605 | |
| 16606 | cell->dependent_code()->DeoptimizeDependentCodeGroup( |
| 16607 | isolate, DependentCode::kPropertyCellChangedGroup); |
| 16608 | |
| 16609 | if (old_type->Is(Type::None()) || old_type->Is(Type::Undefined())) { |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 16610 | return new_type; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16611 | } |
| 16612 | |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 16613 | return handle(Type::Any(), isolate); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16614 | } |
| 16615 | |
| 16616 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16617 | void PropertyCell::SetValueInferType(Handle<PropertyCell> cell, |
bmeurer@chromium.org | 71f9fca | 2013-10-22 08:00:09 +0000 | [diff] [blame] | 16618 | Handle<Object> value) { |
| 16619 | cell->set_value(*value); |
| 16620 | if (!Type::Any()->Is(cell->type())) { |
bmeurer@chromium.org | db78329 | 2013-10-23 06:51:00 +0000 | [diff] [blame] | 16621 | Handle<Type> new_type = UpdatedType(cell, value); |
| 16622 | cell->set_type(*new_type); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16623 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16624 | } |
| 16625 | |
| 16626 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16627 | void PropertyCell::AddDependentCompilationInfo(CompilationInfo* info) { |
| 16628 | Handle<DependentCode> dep(dependent_code()); |
| 16629 | Handle<DependentCode> codes = |
| 16630 | DependentCode::Insert(dep, DependentCode::kPropertyCellChangedGroup, |
| 16631 | info->object_wrapper()); |
| 16632 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 16633 | info->dependencies(DependentCode::kPropertyCellChangedGroup)->Add( |
| 16634 | Handle<HeapObject>(this), info->zone()); |
| 16635 | } |
| 16636 | |
| 16637 | |
| 16638 | void PropertyCell::AddDependentCode(Handle<Code> code) { |
| 16639 | Handle<DependentCode> codes = DependentCode::Insert( |
| 16640 | Handle<DependentCode>(dependent_code()), |
| 16641 | DependentCode::kPropertyCellChangedGroup, code); |
| 16642 | if (*codes != dependent_code()) set_dependent_code(*codes); |
| 16643 | } |
| 16644 | |
| 16645 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 16646 | const char* GetBailoutReason(BailoutReason reason) { |
| 16647 | ASSERT(reason < kLastErrorMessage); |
| 16648 | #define ERROR_MESSAGES_TEXTS(C, T) T, |
| 16649 | static const char* error_messages_[] = { |
| 16650 | ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) |
| 16651 | }; |
| 16652 | #undef ERROR_MESSAGES_TEXTS |
| 16653 | return error_messages_[reason]; |
| 16654 | } |
| 16655 | |
| 16656 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 16657 | } } // namespace v8::internal |