Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 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 | |
| 30 | #include "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "debug.h" |
| 34 | #include "execution.h" |
| 35 | #include "objects-inl.h" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 36 | #include "objects-visiting.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "macro-assembler.h" |
| 38 | #include "scanner.h" |
| 39 | #include "scopeinfo.h" |
| 40 | #include "string-stream.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 41 | #include "utils.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef ENABLE_DISASSEMBLER |
| 44 | #include "disassembler.h" |
| 45 | #endif |
| 46 | |
| 47 | |
| 48 | namespace v8 { |
| 49 | namespace internal { |
| 50 | |
| 51 | // Getters and setters are stored in a fixed array property. These are |
| 52 | // constants for their indices. |
| 53 | const int kGetterIndex = 0; |
| 54 | const int kSetterIndex = 1; |
| 55 | |
| 56 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 57 | MUST_USE_RESULT static Object* CreateJSValue(JSFunction* constructor, |
| 58 | Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 59 | Object* result = Heap::AllocateJSObject(constructor); |
| 60 | if (result->IsFailure()) return result; |
| 61 | JSValue::cast(result)->set_value(value); |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | Object* Object::ToObject(Context* global_context) { |
| 67 | if (IsNumber()) { |
| 68 | return CreateJSValue(global_context->number_function(), this); |
| 69 | } else if (IsBoolean()) { |
| 70 | return CreateJSValue(global_context->boolean_function(), this); |
| 71 | } else if (IsString()) { |
| 72 | return CreateJSValue(global_context->string_function(), this); |
| 73 | } |
| 74 | ASSERT(IsJSObject()); |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | Object* Object::ToObject() { |
| 80 | Context* global_context = Top::context()->global_context(); |
| 81 | if (IsJSObject()) { |
| 82 | return this; |
| 83 | } else if (IsNumber()) { |
| 84 | return CreateJSValue(global_context->number_function(), this); |
| 85 | } else if (IsBoolean()) { |
| 86 | return CreateJSValue(global_context->boolean_function(), this); |
| 87 | } else if (IsString()) { |
| 88 | return CreateJSValue(global_context->string_function(), this); |
| 89 | } |
| 90 | |
| 91 | // Throw a type error. |
| 92 | return Failure::InternalError(); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | Object* Object::ToBoolean() { |
| 97 | if (IsTrue()) return Heap::true_value(); |
| 98 | if (IsFalse()) return Heap::false_value(); |
| 99 | if (IsSmi()) { |
| 100 | return Heap::ToBoolean(Smi::cast(this)->value() != 0); |
| 101 | } |
| 102 | if (IsUndefined() || IsNull()) return Heap::false_value(); |
| 103 | // Undetectable object is false |
| 104 | if (IsUndetectableObject()) { |
| 105 | return Heap::false_value(); |
| 106 | } |
| 107 | if (IsString()) { |
| 108 | return Heap::ToBoolean(String::cast(this)->length() != 0); |
| 109 | } |
| 110 | if (IsHeapNumber()) { |
| 111 | return HeapNumber::cast(this)->HeapNumberToBoolean(); |
| 112 | } |
| 113 | return Heap::true_value(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void Object::Lookup(String* name, LookupResult* result) { |
| 118 | if (IsJSObject()) return JSObject::cast(this)->Lookup(name, result); |
| 119 | Object* holder = NULL; |
| 120 | Context* global_context = Top::context()->global_context(); |
| 121 | if (IsString()) { |
| 122 | holder = global_context->string_function()->instance_prototype(); |
| 123 | } else if (IsNumber()) { |
| 124 | holder = global_context->number_function()->instance_prototype(); |
| 125 | } else if (IsBoolean()) { |
| 126 | holder = global_context->boolean_function()->instance_prototype(); |
| 127 | } |
| 128 | ASSERT(holder != NULL); // Cannot handle null or undefined. |
| 129 | JSObject::cast(holder)->Lookup(name, result); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | Object* Object::GetPropertyWithReceiver(Object* receiver, |
| 134 | String* name, |
| 135 | PropertyAttributes* attributes) { |
| 136 | LookupResult result; |
| 137 | Lookup(name, &result); |
| 138 | Object* value = GetProperty(receiver, &result, name, attributes); |
| 139 | ASSERT(*attributes <= ABSENT); |
| 140 | return value; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | Object* Object::GetPropertyWithCallback(Object* receiver, |
| 145 | Object* structure, |
| 146 | String* name, |
| 147 | Object* holder) { |
| 148 | // To accommodate both the old and the new api we switch on the |
| 149 | // data structure used to store the callbacks. Eventually proxy |
| 150 | // callbacks should be phased out. |
| 151 | if (structure->IsProxy()) { |
| 152 | AccessorDescriptor* callback = |
| 153 | reinterpret_cast<AccessorDescriptor*>(Proxy::cast(structure)->proxy()); |
| 154 | Object* value = (callback->getter)(receiver, callback->data); |
| 155 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 156 | return value; |
| 157 | } |
| 158 | |
| 159 | // api style callbacks. |
| 160 | if (structure->IsAccessorInfo()) { |
| 161 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 162 | Object* fun_obj = data->getter(); |
| 163 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
| 164 | HandleScope scope; |
| 165 | JSObject* self = JSObject::cast(receiver); |
| 166 | JSObject* holder_handle = JSObject::cast(holder); |
| 167 | Handle<String> key(name); |
| 168 | LOG(ApiNamedPropertyAccess("load", self, name)); |
| 169 | CustomArguments args(data->data(), self, holder_handle); |
| 170 | v8::AccessorInfo info(args.end()); |
| 171 | v8::Handle<v8::Value> result; |
| 172 | { |
| 173 | // Leaving JavaScript. |
| 174 | VMState state(EXTERNAL); |
| 175 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 176 | } |
| 177 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 178 | if (result.IsEmpty()) return Heap::undefined_value(); |
| 179 | return *v8::Utils::OpenHandle(*result); |
| 180 | } |
| 181 | |
| 182 | // __defineGetter__ callback |
| 183 | if (structure->IsFixedArray()) { |
| 184 | Object* getter = FixedArray::cast(structure)->get(kGetterIndex); |
| 185 | if (getter->IsJSFunction()) { |
| 186 | return Object::GetPropertyWithDefinedGetter(receiver, |
| 187 | JSFunction::cast(getter)); |
| 188 | } |
| 189 | // Getter is not a function. |
| 190 | return Heap::undefined_value(); |
| 191 | } |
| 192 | |
| 193 | UNREACHABLE(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 194 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
| 198 | Object* Object::GetPropertyWithDefinedGetter(Object* receiver, |
| 199 | JSFunction* getter) { |
| 200 | HandleScope scope; |
| 201 | Handle<JSFunction> fun(JSFunction::cast(getter)); |
| 202 | Handle<Object> self(receiver); |
| 203 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 204 | // Handle stepping into a getter if step into is active. |
| 205 | if (Debug::StepInActive()) { |
| 206 | Debug::HandleStepIn(fun, Handle<Object>::null(), 0, false); |
| 207 | } |
| 208 | #endif |
| 209 | bool has_pending_exception; |
| 210 | Handle<Object> result = |
| 211 | Execution::Call(fun, self, 0, NULL, &has_pending_exception); |
| 212 | // Check for pending exception and return the result. |
| 213 | if (has_pending_exception) return Failure::Exception(); |
| 214 | return *result; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | // Only deal with CALLBACKS and INTERCEPTOR |
| 219 | Object* JSObject::GetPropertyWithFailedAccessCheck( |
| 220 | Object* receiver, |
| 221 | LookupResult* result, |
| 222 | String* name, |
| 223 | PropertyAttributes* attributes) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 224 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | switch (result->type()) { |
| 226 | case CALLBACKS: { |
| 227 | // Only allow API accessors. |
| 228 | Object* obj = result->GetCallbackObject(); |
| 229 | if (obj->IsAccessorInfo()) { |
| 230 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 231 | if (info->all_can_read()) { |
| 232 | *attributes = result->GetAttributes(); |
| 233 | return GetPropertyWithCallback(receiver, |
| 234 | result->GetCallbackObject(), |
| 235 | name, |
| 236 | result->holder()); |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | case NORMAL: |
| 242 | case FIELD: |
| 243 | case CONSTANT_FUNCTION: { |
| 244 | // Search ALL_CAN_READ accessors in prototype chain. |
| 245 | LookupResult r; |
| 246 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 247 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | return GetPropertyWithFailedAccessCheck(receiver, |
| 249 | &r, |
| 250 | name, |
| 251 | attributes); |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | case INTERCEPTOR: { |
| 256 | // If the object has an interceptor, try real named properties. |
| 257 | // No access check in GetPropertyAttributeWithInterceptor. |
| 258 | LookupResult r; |
| 259 | result->holder()->LookupRealNamedProperty(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 260 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 261 | return GetPropertyWithFailedAccessCheck(receiver, |
| 262 | &r, |
| 263 | name, |
| 264 | attributes); |
| 265 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 268 | default: |
| 269 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | // No accessible property found. |
| 274 | *attributes = ABSENT; |
| 275 | Top::ReportFailedAccessCheck(this, v8::ACCESS_GET); |
| 276 | return Heap::undefined_value(); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck( |
| 281 | Object* receiver, |
| 282 | LookupResult* result, |
| 283 | String* name, |
| 284 | bool continue_search) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 285 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | switch (result->type()) { |
| 287 | case CALLBACKS: { |
| 288 | // Only allow API accessors. |
| 289 | Object* obj = result->GetCallbackObject(); |
| 290 | if (obj->IsAccessorInfo()) { |
| 291 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 292 | if (info->all_can_read()) { |
| 293 | return result->GetAttributes(); |
| 294 | } |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | case NORMAL: |
| 300 | case FIELD: |
| 301 | case CONSTANT_FUNCTION: { |
| 302 | if (!continue_search) break; |
| 303 | // Search ALL_CAN_READ accessors in prototype chain. |
| 304 | LookupResult r; |
| 305 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 306 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 307 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 308 | &r, |
| 309 | name, |
| 310 | continue_search); |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | case INTERCEPTOR: { |
| 316 | // If the object has an interceptor, try real named properties. |
| 317 | // No access check in GetPropertyAttributeWithInterceptor. |
| 318 | LookupResult r; |
| 319 | if (continue_search) { |
| 320 | result->holder()->LookupRealNamedProperty(name, &r); |
| 321 | } else { |
| 322 | result->holder()->LocalLookupRealNamedProperty(name, &r); |
| 323 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 324 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 326 | &r, |
| 327 | name, |
| 328 | continue_search); |
| 329 | } |
| 330 | break; |
| 331 | } |
| 332 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 333 | default: |
| 334 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 339 | return ABSENT; |
| 340 | } |
| 341 | |
| 342 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 343 | Object* JSObject::GetNormalizedProperty(LookupResult* result) { |
| 344 | ASSERT(!HasFastProperties()); |
| 345 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 346 | if (IsGlobalObject()) { |
| 347 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 348 | } |
| 349 | ASSERT(!value->IsJSGlobalPropertyCell()); |
| 350 | return value; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | Object* JSObject::SetNormalizedProperty(LookupResult* result, Object* value) { |
| 355 | ASSERT(!HasFastProperties()); |
| 356 | if (IsGlobalObject()) { |
| 357 | JSGlobalPropertyCell* cell = |
| 358 | JSGlobalPropertyCell::cast( |
| 359 | property_dictionary()->ValueAt(result->GetDictionaryEntry())); |
| 360 | cell->set_value(value); |
| 361 | } else { |
| 362 | property_dictionary()->ValueAtPut(result->GetDictionaryEntry(), value); |
| 363 | } |
| 364 | return value; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | Object* JSObject::SetNormalizedProperty(String* name, |
| 369 | Object* value, |
| 370 | PropertyDetails details) { |
| 371 | ASSERT(!HasFastProperties()); |
| 372 | int entry = property_dictionary()->FindEntry(name); |
| 373 | if (entry == StringDictionary::kNotFound) { |
| 374 | Object* store_value = value; |
| 375 | if (IsGlobalObject()) { |
| 376 | store_value = Heap::AllocateJSGlobalPropertyCell(value); |
| 377 | if (store_value->IsFailure()) return store_value; |
| 378 | } |
| 379 | Object* dict = property_dictionary()->Add(name, store_value, details); |
| 380 | if (dict->IsFailure()) return dict; |
| 381 | set_properties(StringDictionary::cast(dict)); |
| 382 | return value; |
| 383 | } |
| 384 | // Preserve enumeration index. |
| 385 | details = PropertyDetails(details.attributes(), |
| 386 | details.type(), |
| 387 | property_dictionary()->DetailsAt(entry).index()); |
| 388 | if (IsGlobalObject()) { |
| 389 | JSGlobalPropertyCell* cell = |
| 390 | JSGlobalPropertyCell::cast(property_dictionary()->ValueAt(entry)); |
| 391 | cell->set_value(value); |
| 392 | // Please note we have to update the property details. |
| 393 | property_dictionary()->DetailsAtPut(entry, details); |
| 394 | } else { |
| 395 | property_dictionary()->SetEntry(entry, name, value, details); |
| 396 | } |
| 397 | return value; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | Object* JSObject::DeleteNormalizedProperty(String* name, DeleteMode mode) { |
| 402 | ASSERT(!HasFastProperties()); |
| 403 | StringDictionary* dictionary = property_dictionary(); |
| 404 | int entry = dictionary->FindEntry(name); |
| 405 | if (entry != StringDictionary::kNotFound) { |
| 406 | // If we have a global object set the cell to the hole. |
| 407 | if (IsGlobalObject()) { |
| 408 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 409 | if (details.IsDontDelete()) { |
| 410 | if (mode != FORCE_DELETION) return Heap::false_value(); |
| 411 | // When forced to delete global properties, we have to make a |
| 412 | // map change to invalidate any ICs that think they can load |
| 413 | // from the DontDelete cell without checking if it contains |
| 414 | // the hole value. |
| 415 | Object* new_map = map()->CopyDropDescriptors(); |
| 416 | if (new_map->IsFailure()) return new_map; |
| 417 | set_map(Map::cast(new_map)); |
| 418 | } |
| 419 | JSGlobalPropertyCell* cell = |
| 420 | JSGlobalPropertyCell::cast(dictionary->ValueAt(entry)); |
| 421 | cell->set_value(Heap::the_hole_value()); |
| 422 | dictionary->DetailsAtPut(entry, details.AsDeleted()); |
| 423 | } else { |
| 424 | return dictionary->DeleteProperty(entry, mode); |
| 425 | } |
| 426 | } |
| 427 | return Heap::true_value(); |
| 428 | } |
| 429 | |
| 430 | |
| 431 | bool JSObject::IsDirty() { |
| 432 | Object* cons_obj = map()->constructor(); |
| 433 | if (!cons_obj->IsJSFunction()) |
| 434 | return true; |
| 435 | JSFunction* fun = JSFunction::cast(cons_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 436 | if (!fun->shared()->IsApiFunction()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | return true; |
| 438 | // If the object is fully fast case and has the same map it was |
| 439 | // created with then no changes can have been made to it. |
| 440 | return map() != fun->initial_map() |
| 441 | || !HasFastElements() |
| 442 | || !HasFastProperties(); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | Object* Object::GetProperty(Object* receiver, |
| 447 | LookupResult* result, |
| 448 | String* name, |
| 449 | PropertyAttributes* attributes) { |
| 450 | // Make sure that the top context does not change when doing |
| 451 | // callbacks or interceptor calls. |
| 452 | AssertNoContextChange ncc; |
| 453 | |
| 454 | // Traverse the prototype chain from the current object (this) to |
| 455 | // the holder and check for access rights. This avoid traversing the |
| 456 | // objects more than once in case of interceptors, because the |
| 457 | // holder will always be the interceptor holder and the search may |
| 458 | // only continue with a current object just after the interceptor |
| 459 | // holder in the prototype chain. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 460 | Object* last = result->IsProperty() ? result->holder() : Heap::null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 461 | for (Object* current = this; true; current = current->GetPrototype()) { |
| 462 | if (current->IsAccessCheckNeeded()) { |
| 463 | // Check if we're allowed to read from the current object. Note |
| 464 | // that even though we may not actually end up loading the named |
| 465 | // property from the current object, we still check that we have |
| 466 | // access to it. |
| 467 | JSObject* checked = JSObject::cast(current); |
| 468 | if (!Top::MayNamedAccess(checked, name, v8::ACCESS_GET)) { |
| 469 | return checked->GetPropertyWithFailedAccessCheck(receiver, |
| 470 | result, |
| 471 | name, |
| 472 | attributes); |
| 473 | } |
| 474 | } |
| 475 | // Stop traversing the chain once we reach the last object in the |
| 476 | // chain; either the holder of the result or null in case of an |
| 477 | // absent property. |
| 478 | if (current == last) break; |
| 479 | } |
| 480 | |
| 481 | if (!result->IsProperty()) { |
| 482 | *attributes = ABSENT; |
| 483 | return Heap::undefined_value(); |
| 484 | } |
| 485 | *attributes = result->GetAttributes(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | Object* value; |
| 487 | JSObject* holder = result->holder(); |
| 488 | switch (result->type()) { |
| 489 | case NORMAL: |
| 490 | value = holder->GetNormalizedProperty(result); |
| 491 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
| 492 | return value->IsTheHole() ? Heap::undefined_value() : value; |
| 493 | case FIELD: |
| 494 | value = holder->FastPropertyAt(result->GetFieldIndex()); |
| 495 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
| 496 | return value->IsTheHole() ? Heap::undefined_value() : value; |
| 497 | case CONSTANT_FUNCTION: |
| 498 | return result->GetConstantFunction(); |
| 499 | case CALLBACKS: |
| 500 | return GetPropertyWithCallback(receiver, |
| 501 | result->GetCallbackObject(), |
| 502 | name, |
| 503 | holder); |
| 504 | case INTERCEPTOR: { |
| 505 | JSObject* recvr = JSObject::cast(receiver); |
| 506 | return holder->GetPropertyWithInterceptor(recvr, name, attributes); |
| 507 | } |
| 508 | default: |
| 509 | UNREACHABLE(); |
| 510 | return NULL; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | |
| 515 | Object* Object::GetElementWithReceiver(Object* receiver, uint32_t index) { |
| 516 | // Non-JS objects do not have integer indexed properties. |
| 517 | if (!IsJSObject()) return Heap::undefined_value(); |
| 518 | return JSObject::cast(this)->GetElementWithReceiver(JSObject::cast(receiver), |
| 519 | index); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | Object* Object::GetPrototype() { |
| 524 | // The object is either a number, a string, a boolean, or a real JS object. |
| 525 | if (IsJSObject()) return JSObject::cast(this)->map()->prototype(); |
| 526 | Context* context = Top::context()->global_context(); |
| 527 | |
| 528 | if (IsNumber()) return context->number_function()->instance_prototype(); |
| 529 | if (IsString()) return context->string_function()->instance_prototype(); |
| 530 | if (IsBoolean()) { |
| 531 | return context->boolean_function()->instance_prototype(); |
| 532 | } else { |
| 533 | return Heap::null_value(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | void Object::ShortPrint() { |
| 539 | HeapStringAllocator allocator; |
| 540 | StringStream accumulator(&allocator); |
| 541 | ShortPrint(&accumulator); |
| 542 | accumulator.OutputToStdOut(); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | void Object::ShortPrint(StringStream* accumulator) { |
| 547 | if (IsSmi()) { |
| 548 | Smi::cast(this)->SmiPrint(accumulator); |
| 549 | } else if (IsFailure()) { |
| 550 | Failure::cast(this)->FailurePrint(accumulator); |
| 551 | } else { |
| 552 | HeapObject::cast(this)->HeapObjectShortPrint(accumulator); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | |
| 557 | void Smi::SmiPrint() { |
| 558 | PrintF("%d", value()); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | void Smi::SmiPrint(StringStream* accumulator) { |
| 563 | accumulator->Add("%d", value()); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void Failure::FailurePrint(StringStream* accumulator) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 568 | accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | void Failure::FailurePrint() { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 573 | PrintF("Failure(%p)", reinterpret_cast<void*>(value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
| 577 | Failure* Failure::RetryAfterGC(int requested_bytes, AllocationSpace space) { |
| 578 | ASSERT((space & ~kSpaceTagMask) == 0); |
| 579 | // TODO(X64): Stop using Smi validation for non-smi checks, even if they |
| 580 | // happen to be identical at the moment. |
| 581 | |
| 582 | int requested = requested_bytes >> kObjectAlignmentBits; |
| 583 | int value = (requested << kSpaceTagSize) | space; |
| 584 | // We can't very well allocate a heap number in this situation, and if the |
| 585 | // requested memory is so large it seems reasonable to say that this is an |
| 586 | // out of memory situation. This fixes a crash in |
| 587 | // js1_5/Regress/regress-303213.js. |
| 588 | if (value >> kSpaceTagSize != requested || |
| 589 | !Smi::IsValid(value) || |
| 590 | value != ((value << kFailureTypeTagSize) >> kFailureTypeTagSize) || |
| 591 | !Smi::IsValid(value << kFailureTypeTagSize)) { |
| 592 | Top::context()->mark_out_of_memory(); |
| 593 | return Failure::OutOfMemoryException(); |
| 594 | } |
| 595 | return Construct(RETRY_AFTER_GC, value); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 600 | // English? Returns false for non-ASCII or words that don't start with |
| 601 | // a capital letter. The a/an rule follows pronunciation in English. |
| 602 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 603 | // you speak a dialect you may well say "an 'istoric occasion". |
| 604 | static bool AnWord(String* str) { |
| 605 | if (str->length() == 0) return false; // A nothing. |
| 606 | int c0 = str->Get(0); |
| 607 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
| 608 | if (c0 == 'U') { |
| 609 | if (c1 > 'Z') { |
| 610 | return true; // An Umpire, but a UTF8String, a U. |
| 611 | } |
| 612 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
| 613 | return true; // An Ape, an ABCBook. |
| 614 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 615 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 616 | c0 == 'S' || c0 == 'X')) { |
| 617 | return true; // An MP3File, an M. |
| 618 | } |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 623 | Object* String::SlowTryFlatten(PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 624 | #ifdef DEBUG |
| 625 | // Do not attempt to flatten in debug mode when allocation is not |
| 626 | // allowed. This is to avoid an assertion failure when allocating. |
| 627 | // Flattening strings is the only case where we always allow |
| 628 | // allocation because no GC is performed if the allocation fails. |
| 629 | if (!Heap::IsAllocationAllowed()) return this; |
| 630 | #endif |
| 631 | |
| 632 | switch (StringShape(this).representation_tag()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 633 | case kConsStringTag: { |
| 634 | ConsString* cs = ConsString::cast(this); |
| 635 | if (cs->second()->length() == 0) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 636 | return cs->first(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | } |
| 638 | // There's little point in putting the flat string in new space if the |
| 639 | // cons string is in old space. It can never get GCed until there is |
| 640 | // an old space GC. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 641 | PretenureFlag tenure = Heap::InNewSpace(this) ? pretenure : TENURED; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 642 | int len = length(); |
| 643 | Object* object; |
| 644 | String* result; |
| 645 | if (IsAsciiRepresentation()) { |
| 646 | object = Heap::AllocateRawAsciiString(len, tenure); |
| 647 | if (object->IsFailure()) return object; |
| 648 | result = String::cast(object); |
| 649 | String* first = cs->first(); |
| 650 | int first_length = first->length(); |
| 651 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 652 | WriteToFlat(first, dest, 0, first_length); |
| 653 | String* second = cs->second(); |
| 654 | WriteToFlat(second, |
| 655 | dest + first_length, |
| 656 | 0, |
| 657 | len - first_length); |
| 658 | } else { |
| 659 | object = Heap::AllocateRawTwoByteString(len, tenure); |
| 660 | if (object->IsFailure()) return object; |
| 661 | result = String::cast(object); |
| 662 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 663 | String* first = cs->first(); |
| 664 | int first_length = first->length(); |
| 665 | WriteToFlat(first, dest, 0, first_length); |
| 666 | String* second = cs->second(); |
| 667 | WriteToFlat(second, |
| 668 | dest + first_length, |
| 669 | 0, |
| 670 | len - first_length); |
| 671 | } |
| 672 | cs->set_first(result); |
| 673 | cs->set_second(Heap::empty_string()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 674 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | } |
| 676 | default: |
| 677 | return this; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | |
| 682 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 683 | // Externalizing twice leaks the external resource, so it's |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 684 | // prohibited by the API. |
| 685 | ASSERT(!this->IsExternalString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 686 | #ifdef DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 687 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 688 | // Assert that the resource and the string are equivalent. |
| 689 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 690 | ScopedVector<uc16> smart_chars(this->length()); |
| 691 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 692 | ASSERT(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 694 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 695 | } |
| 696 | #endif // DEBUG |
| 697 | |
| 698 | int size = this->Size(); // Byte size of the original string. |
| 699 | if (size < ExternalString::kSize) { |
| 700 | // The string is too small to fit an external String in its place. This can |
| 701 | // only happen for zero length strings. |
| 702 | return false; |
| 703 | } |
| 704 | ASSERT(size >= ExternalString::kSize); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 705 | bool is_ascii = this->IsAsciiRepresentation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 706 | bool is_symbol = this->IsSymbol(); |
| 707 | int length = this->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 708 | int hash_field = this->hash_field(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 709 | |
| 710 | // Morph the object to an external string by adjusting the map and |
| 711 | // reinitializing the fields. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 712 | this->set_map(is_ascii ? |
| 713 | Heap::external_string_with_ascii_data_map() : |
| 714 | Heap::external_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 715 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 716 | self->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 717 | self->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 718 | self->set_resource(resource); |
| 719 | // Additionally make the object into an external symbol if the original string |
| 720 | // was a symbol to start with. |
| 721 | if (is_symbol) { |
| 722 | self->Hash(); // Force regeneration of the hash value. |
| 723 | // Now morph this external string into a external symbol. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 724 | this->set_map(is_ascii ? |
| 725 | Heap::external_symbol_with_ascii_data_map() : |
| 726 | Heap::external_symbol_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // Fill the remainder of the string with dead wood. |
| 730 | int new_size = this->Size(); // Byte size of the external String object. |
| 731 | Heap::CreateFillerObjectAt(this->address() + new_size, size - new_size); |
| 732 | return true; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { |
| 737 | #ifdef DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 738 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 739 | // Assert that the resource and the string are equivalent. |
| 740 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 741 | ScopedVector<char> smart_chars(this->length()); |
| 742 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 743 | ASSERT(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 744 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 745 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 746 | } |
| 747 | #endif // DEBUG |
| 748 | |
| 749 | int size = this->Size(); // Byte size of the original string. |
| 750 | if (size < ExternalString::kSize) { |
| 751 | // The string is too small to fit an external String in its place. This can |
| 752 | // only happen for zero length strings. |
| 753 | return false; |
| 754 | } |
| 755 | ASSERT(size >= ExternalString::kSize); |
| 756 | bool is_symbol = this->IsSymbol(); |
| 757 | int length = this->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 758 | int hash_field = this->hash_field(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 759 | |
| 760 | // Morph the object to an external string by adjusting the map and |
| 761 | // reinitializing the fields. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 762 | this->set_map(Heap::external_ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 763 | ExternalAsciiString* self = ExternalAsciiString::cast(this); |
| 764 | self->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 765 | self->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 766 | self->set_resource(resource); |
| 767 | // Additionally make the object into an external symbol if the original string |
| 768 | // was a symbol to start with. |
| 769 | if (is_symbol) { |
| 770 | self->Hash(); // Force regeneration of the hash value. |
| 771 | // Now morph this external string into a external symbol. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 772 | this->set_map(Heap::external_ascii_symbol_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Fill the remainder of the string with dead wood. |
| 776 | int new_size = this->Size(); // Byte size of the external String object. |
| 777 | Heap::CreateFillerObjectAt(this->address() + new_size, size - new_size); |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | void String::StringShortPrint(StringStream* accumulator) { |
| 783 | int len = length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 784 | if (len > kMaxShortPrintLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 785 | accumulator->Add("<Very long string[%u]>", len); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | if (!LooksValid()) { |
| 790 | accumulator->Add("<Invalid String>"); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | StringInputBuffer buf(this); |
| 795 | |
| 796 | bool truncated = false; |
| 797 | if (len > kMaxShortPrintLength) { |
| 798 | len = kMaxShortPrintLength; |
| 799 | truncated = true; |
| 800 | } |
| 801 | bool ascii = true; |
| 802 | for (int i = 0; i < len; i++) { |
| 803 | int c = buf.GetNext(); |
| 804 | |
| 805 | if (c < 32 || c >= 127) { |
| 806 | ascii = false; |
| 807 | } |
| 808 | } |
| 809 | buf.Reset(this); |
| 810 | if (ascii) { |
| 811 | accumulator->Add("<String[%u]: ", length()); |
| 812 | for (int i = 0; i < len; i++) { |
| 813 | accumulator->Put(buf.GetNext()); |
| 814 | } |
| 815 | accumulator->Put('>'); |
| 816 | } else { |
| 817 | // Backslash indicates that the string contains control |
| 818 | // characters and that backslashes are therefore escaped. |
| 819 | accumulator->Add("<String[%u]\\: ", length()); |
| 820 | for (int i = 0; i < len; i++) { |
| 821 | int c = buf.GetNext(); |
| 822 | if (c == '\n') { |
| 823 | accumulator->Add("\\n"); |
| 824 | } else if (c == '\r') { |
| 825 | accumulator->Add("\\r"); |
| 826 | } else if (c == '\\') { |
| 827 | accumulator->Add("\\\\"); |
| 828 | } else if (c < 32 || c > 126) { |
| 829 | accumulator->Add("\\x%02x", c); |
| 830 | } else { |
| 831 | accumulator->Put(c); |
| 832 | } |
| 833 | } |
| 834 | if (truncated) { |
| 835 | accumulator->Put('.'); |
| 836 | accumulator->Put('.'); |
| 837 | accumulator->Put('.'); |
| 838 | } |
| 839 | accumulator->Put('>'); |
| 840 | } |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 846 | switch (map()->instance_type()) { |
| 847 | case JS_ARRAY_TYPE: { |
| 848 | double length = JSArray::cast(this)->length()->Number(); |
| 849 | accumulator->Add("<JS array[%u]>", static_cast<uint32_t>(length)); |
| 850 | break; |
| 851 | } |
| 852 | case JS_REGEXP_TYPE: { |
| 853 | accumulator->Add("<JS RegExp>"); |
| 854 | break; |
| 855 | } |
| 856 | case JS_FUNCTION_TYPE: { |
| 857 | Object* fun_name = JSFunction::cast(this)->shared()->name(); |
| 858 | bool printed = false; |
| 859 | if (fun_name->IsString()) { |
| 860 | String* str = String::cast(fun_name); |
| 861 | if (str->length() > 0) { |
| 862 | accumulator->Add("<JS Function "); |
| 863 | accumulator->Put(str); |
| 864 | accumulator->Put('>'); |
| 865 | printed = true; |
| 866 | } |
| 867 | } |
| 868 | if (!printed) { |
| 869 | accumulator->Add("<JS Function>"); |
| 870 | } |
| 871 | break; |
| 872 | } |
| 873 | // All other JSObjects are rather similar to each other (JSObject, |
| 874 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
| 875 | default: { |
| 876 | Object* constructor = map()->constructor(); |
| 877 | bool printed = false; |
| 878 | if (constructor->IsHeapObject() && |
| 879 | !Heap::Contains(HeapObject::cast(constructor))) { |
| 880 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 881 | } else { |
| 882 | bool global_object = IsJSGlobalProxy(); |
| 883 | if (constructor->IsJSFunction()) { |
| 884 | if (!Heap::Contains(JSFunction::cast(constructor)->shared())) { |
| 885 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 886 | } else { |
| 887 | Object* constructor_name = |
| 888 | JSFunction::cast(constructor)->shared()->name(); |
| 889 | if (constructor_name->IsString()) { |
| 890 | String* str = String::cast(constructor_name); |
| 891 | if (str->length() > 0) { |
| 892 | bool vowel = AnWord(str); |
| 893 | accumulator->Add("<%sa%s ", |
| 894 | global_object ? "Global Object: " : "", |
| 895 | vowel ? "n" : ""); |
| 896 | accumulator->Put(str); |
| 897 | accumulator->Put('>'); |
| 898 | printed = true; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | if (!printed) { |
| 904 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 905 | } |
| 906 | } |
| 907 | if (IsJSValue()) { |
| 908 | accumulator->Add(" value = "); |
| 909 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 910 | } |
| 911 | accumulator->Put('>'); |
| 912 | break; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | |
| 918 | void HeapObject::HeapObjectShortPrint(StringStream* accumulator) { |
| 919 | // if (!Heap::InNewSpace(this)) PrintF("*", this); |
| 920 | if (!Heap::Contains(this)) { |
| 921 | accumulator->Add("!!!INVALID POINTER!!!"); |
| 922 | return; |
| 923 | } |
| 924 | if (!Heap::Contains(map())) { |
| 925 | accumulator->Add("!!!INVALID MAP!!!"); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | accumulator->Add("%p ", this); |
| 930 | |
| 931 | if (IsString()) { |
| 932 | String::cast(this)->StringShortPrint(accumulator); |
| 933 | return; |
| 934 | } |
| 935 | if (IsJSObject()) { |
| 936 | JSObject::cast(this)->JSObjectShortPrint(accumulator); |
| 937 | return; |
| 938 | } |
| 939 | switch (map()->instance_type()) { |
| 940 | case MAP_TYPE: |
| 941 | accumulator->Add("<Map>"); |
| 942 | break; |
| 943 | case FIXED_ARRAY_TYPE: |
| 944 | accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length()); |
| 945 | break; |
| 946 | case BYTE_ARRAY_TYPE: |
| 947 | accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length()); |
| 948 | break; |
| 949 | case PIXEL_ARRAY_TYPE: |
| 950 | accumulator->Add("<PixelArray[%u]>", PixelArray::cast(this)->length()); |
| 951 | break; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 952 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 953 | accumulator->Add("<ExternalByteArray[%u]>", |
| 954 | ExternalByteArray::cast(this)->length()); |
| 955 | break; |
| 956 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 957 | accumulator->Add("<ExternalUnsignedByteArray[%u]>", |
| 958 | ExternalUnsignedByteArray::cast(this)->length()); |
| 959 | break; |
| 960 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 961 | accumulator->Add("<ExternalShortArray[%u]>", |
| 962 | ExternalShortArray::cast(this)->length()); |
| 963 | break; |
| 964 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 965 | accumulator->Add("<ExternalUnsignedShortArray[%u]>", |
| 966 | ExternalUnsignedShortArray::cast(this)->length()); |
| 967 | break; |
| 968 | case EXTERNAL_INT_ARRAY_TYPE: |
| 969 | accumulator->Add("<ExternalIntArray[%u]>", |
| 970 | ExternalIntArray::cast(this)->length()); |
| 971 | break; |
| 972 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 973 | accumulator->Add("<ExternalUnsignedIntArray[%u]>", |
| 974 | ExternalUnsignedIntArray::cast(this)->length()); |
| 975 | break; |
| 976 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 977 | accumulator->Add("<ExternalFloatArray[%u]>", |
| 978 | ExternalFloatArray::cast(this)->length()); |
| 979 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 980 | case SHARED_FUNCTION_INFO_TYPE: |
| 981 | accumulator->Add("<SharedFunctionInfo>"); |
| 982 | break; |
| 983 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 984 | case NAME##_TYPE: \ |
| 985 | accumulator->Put('<'); \ |
| 986 | accumulator->Add(#Name); \ |
| 987 | accumulator->Put('>'); \ |
| 988 | break; |
| 989 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 990 | #undef MAKE_STRUCT_CASE |
| 991 | case CODE_TYPE: |
| 992 | accumulator->Add("<Code>"); |
| 993 | break; |
| 994 | case ODDBALL_TYPE: { |
| 995 | if (IsUndefined()) |
| 996 | accumulator->Add("<undefined>"); |
| 997 | else if (IsTheHole()) |
| 998 | accumulator->Add("<the hole>"); |
| 999 | else if (IsNull()) |
| 1000 | accumulator->Add("<null>"); |
| 1001 | else if (IsTrue()) |
| 1002 | accumulator->Add("<true>"); |
| 1003 | else if (IsFalse()) |
| 1004 | accumulator->Add("<false>"); |
| 1005 | else |
| 1006 | accumulator->Add("<Odd Oddball>"); |
| 1007 | break; |
| 1008 | } |
| 1009 | case HEAP_NUMBER_TYPE: |
| 1010 | accumulator->Add("<Number: "); |
| 1011 | HeapNumber::cast(this)->HeapNumberPrint(accumulator); |
| 1012 | accumulator->Put('>'); |
| 1013 | break; |
| 1014 | case PROXY_TYPE: |
| 1015 | accumulator->Add("<Proxy>"); |
| 1016 | break; |
| 1017 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
| 1018 | accumulator->Add("Cell for "); |
| 1019 | JSGlobalPropertyCell::cast(this)->value()->ShortPrint(accumulator); |
| 1020 | break; |
| 1021 | default: |
| 1022 | accumulator->Add("<Other heap object (%d)>", map()->instance_type()); |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1028 | void HeapObject::Iterate(ObjectVisitor* v) { |
| 1029 | // Handle header |
| 1030 | IteratePointer(v, kMapOffset); |
| 1031 | // Handle object body |
| 1032 | Map* m = map(); |
| 1033 | IterateBody(m->instance_type(), SizeFromMap(m), v); |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 1038 | ObjectVisitor* v) { |
| 1039 | // Avoiding <Type>::cast(this) because it accesses the map pointer field. |
| 1040 | // During GC, the map pointer field is encoded. |
| 1041 | if (type < FIRST_NONSTRING_TYPE) { |
| 1042 | switch (type & kStringRepresentationMask) { |
| 1043 | case kSeqStringTag: |
| 1044 | break; |
| 1045 | case kConsStringTag: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1046 | ConsString::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1047 | break; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1048 | case kExternalStringTag: |
| 1049 | if ((type & kStringEncodingMask) == kAsciiStringTag) { |
| 1050 | reinterpret_cast<ExternalAsciiString*>(this)-> |
| 1051 | ExternalAsciiStringIterateBody(v); |
| 1052 | } else { |
| 1053 | reinterpret_cast<ExternalTwoByteString*>(this)-> |
| 1054 | ExternalTwoByteStringIterateBody(v); |
| 1055 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | } |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | switch (type) { |
| 1062 | case FIXED_ARRAY_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1063 | FixedArray::BodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | case JS_OBJECT_TYPE: |
| 1066 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
| 1067 | case JS_VALUE_TYPE: |
| 1068 | case JS_ARRAY_TYPE: |
| 1069 | case JS_REGEXP_TYPE: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1070 | case JS_GLOBAL_PROXY_TYPE: |
| 1071 | case JS_GLOBAL_OBJECT_TYPE: |
| 1072 | case JS_BUILTINS_OBJECT_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1073 | JSObject::BodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1074 | break; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1075 | case JS_FUNCTION_TYPE: |
| 1076 | reinterpret_cast<JSFunction*>(this) |
| 1077 | ->JSFunctionIterateBody(object_size, v); |
| 1078 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1079 | case ODDBALL_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1080 | Oddball::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | case PROXY_TYPE: |
| 1083 | reinterpret_cast<Proxy*>(this)->ProxyIterateBody(v); |
| 1084 | break; |
| 1085 | case MAP_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1086 | Map::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | case CODE_TYPE: |
| 1089 | reinterpret_cast<Code*>(this)->CodeIterateBody(v); |
| 1090 | break; |
| 1091 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1092 | JSGlobalPropertyCell::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | case HEAP_NUMBER_TYPE: |
| 1095 | case FILLER_TYPE: |
| 1096 | case BYTE_ARRAY_TYPE: |
| 1097 | case PIXEL_ARRAY_TYPE: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1098 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1099 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1100 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1101 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1102 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1103 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1104 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1105 | break; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1106 | case SHARED_FUNCTION_INFO_TYPE: |
| 1107 | SharedFunctionInfo::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1108 | break; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1109 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1110 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1111 | case NAME##_TYPE: |
| 1112 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1113 | #undef MAKE_STRUCT_CASE |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1114 | StructBodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1115 | break; |
| 1116 | default: |
| 1117 | PrintF("Unknown type: %d\n", type); |
| 1118 | UNREACHABLE(); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1123 | Object* HeapNumber::HeapNumberToBoolean() { |
| 1124 | // NaN, +0, and -0 should return the false object |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1125 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1126 | union IeeeDoubleLittleEndianArchType u; |
| 1127 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 1128 | union IeeeDoubleBigEndianArchType u; |
| 1129 | #endif |
| 1130 | u.d = value(); |
| 1131 | if (u.bits.exp == 2047) { |
| 1132 | // Detect NaN for IEEE double precision floating point. |
| 1133 | if ((u.bits.man_low | u.bits.man_high) != 0) |
| 1134 | return Heap::false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1136 | if (u.bits.exp == 0) { |
| 1137 | // Detect +0, and -0 for IEEE double precision floating point. |
| 1138 | if ((u.bits.man_low | u.bits.man_high) == 0) |
| 1139 | return Heap::false_value(); |
| 1140 | } |
| 1141 | return Heap::true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | void HeapNumber::HeapNumberPrint() { |
| 1146 | PrintF("%.16g", Number()); |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | void HeapNumber::HeapNumberPrint(StringStream* accumulator) { |
| 1151 | // The Windows version of vsnprintf can allocate when printing a %g string |
| 1152 | // into a buffer that may not be big enough. We don't want random memory |
| 1153 | // allocation when producing post-crash stack traces, so we print into a |
| 1154 | // buffer that is plenty big enough for any floating point number, then |
| 1155 | // print that using vsnprintf (which may truncate but never allocate if |
| 1156 | // there is no more space in the buffer). |
| 1157 | EmbeddedVector<char, 100> buffer; |
| 1158 | OS::SNPrintF(buffer, "%.16g", Number()); |
| 1159 | accumulator->Add("%s", buffer.start()); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | String* JSObject::class_name() { |
| 1164 | if (IsJSFunction()) { |
| 1165 | return Heap::function_class_symbol(); |
| 1166 | } |
| 1167 | if (map()->constructor()->IsJSFunction()) { |
| 1168 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1169 | return String::cast(constructor->shared()->instance_class_name()); |
| 1170 | } |
| 1171 | // If the constructor is not present, return "Object". |
| 1172 | return Heap::Object_symbol(); |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | String* JSObject::constructor_name() { |
| 1177 | if (IsJSFunction()) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1178 | return Heap::closure_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1179 | } |
| 1180 | if (map()->constructor()->IsJSFunction()) { |
| 1181 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1182 | String* name = String::cast(constructor->shared()->name()); |
| 1183 | return name->length() > 0 ? name : constructor->shared()->inferred_name(); |
| 1184 | } |
| 1185 | // If the constructor is not present, return "Object". |
| 1186 | return Heap::Object_symbol(); |
| 1187 | } |
| 1188 | |
| 1189 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1190 | Object* JSObject::AddFastPropertyUsingMap(Map* new_map, |
| 1191 | String* name, |
| 1192 | Object* value) { |
| 1193 | int index = new_map->PropertyIndexFor(name); |
| 1194 | if (map()->unused_property_fields() == 0) { |
| 1195 | ASSERT(map()->unused_property_fields() == 0); |
| 1196 | int new_unused = new_map->unused_property_fields(); |
| 1197 | Object* values = |
| 1198 | properties()->CopySize(properties()->length() + new_unused + 1); |
| 1199 | if (values->IsFailure()) return values; |
| 1200 | set_properties(FixedArray::cast(values)); |
| 1201 | } |
| 1202 | set_map(new_map); |
| 1203 | return FastPropertyAtPut(index, value); |
| 1204 | } |
| 1205 | |
| 1206 | |
| 1207 | Object* JSObject::AddFastProperty(String* name, |
| 1208 | Object* value, |
| 1209 | PropertyAttributes attributes) { |
| 1210 | // Normalize the object if the name is an actual string (not the |
| 1211 | // hidden symbols) and is not a real identifier. |
| 1212 | StringInputBuffer buffer(name); |
| 1213 | if (!Scanner::IsIdentifier(&buffer) && name != Heap::hidden_symbol()) { |
| 1214 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1215 | if (obj->IsFailure()) return obj; |
| 1216 | return AddSlowProperty(name, value, attributes); |
| 1217 | } |
| 1218 | |
| 1219 | DescriptorArray* old_descriptors = map()->instance_descriptors(); |
| 1220 | // Compute the new index for new field. |
| 1221 | int index = map()->NextFreePropertyIndex(); |
| 1222 | |
| 1223 | // Allocate new instance descriptors with (name, index) added |
| 1224 | FieldDescriptor new_field(name, index, attributes); |
| 1225 | Object* new_descriptors = |
| 1226 | old_descriptors->CopyInsert(&new_field, REMOVE_TRANSITIONS); |
| 1227 | if (new_descriptors->IsFailure()) return new_descriptors; |
| 1228 | |
| 1229 | // Only allow map transition if the object's map is NOT equal to the |
| 1230 | // global object_function's map and there is not a transition for name. |
| 1231 | bool allow_map_transition = |
| 1232 | !old_descriptors->Contains(name) && |
| 1233 | (Top::context()->global_context()->object_function()->map() != map()); |
| 1234 | |
| 1235 | ASSERT(index < map()->inobject_properties() || |
| 1236 | (index - map()->inobject_properties()) < properties()->length() || |
| 1237 | map()->unused_property_fields() == 0); |
| 1238 | // Allocate a new map for the object. |
| 1239 | Object* r = map()->CopyDropDescriptors(); |
| 1240 | if (r->IsFailure()) return r; |
| 1241 | Map* new_map = Map::cast(r); |
| 1242 | if (allow_map_transition) { |
| 1243 | // Allocate new instance descriptors for the old map with map transition. |
| 1244 | MapTransitionDescriptor d(name, Map::cast(new_map), attributes); |
| 1245 | Object* r = old_descriptors->CopyInsert(&d, KEEP_TRANSITIONS); |
| 1246 | if (r->IsFailure()) return r; |
| 1247 | old_descriptors = DescriptorArray::cast(r); |
| 1248 | } |
| 1249 | |
| 1250 | if (map()->unused_property_fields() == 0) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1251 | if (properties()->length() > MaxFastProperties()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1252 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1253 | if (obj->IsFailure()) return obj; |
| 1254 | return AddSlowProperty(name, value, attributes); |
| 1255 | } |
| 1256 | // Make room for the new value |
| 1257 | Object* values = |
| 1258 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 1259 | if (values->IsFailure()) return values; |
| 1260 | set_properties(FixedArray::cast(values)); |
| 1261 | new_map->set_unused_property_fields(kFieldsAdded - 1); |
| 1262 | } else { |
| 1263 | new_map->set_unused_property_fields(map()->unused_property_fields() - 1); |
| 1264 | } |
| 1265 | // We have now allocated all the necessary objects. |
| 1266 | // All the changes can be applied at once, so they are atomic. |
| 1267 | map()->set_instance_descriptors(old_descriptors); |
| 1268 | new_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1269 | set_map(new_map); |
| 1270 | return FastPropertyAtPut(index, value); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | Object* JSObject::AddConstantFunctionProperty(String* name, |
| 1275 | JSFunction* function, |
| 1276 | PropertyAttributes attributes) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1277 | ASSERT(!Heap::InNewSpace(function)); |
| 1278 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1279 | // Allocate new instance descriptors with (name, function) added |
| 1280 | ConstantFunctionDescriptor d(name, function, attributes); |
| 1281 | Object* new_descriptors = |
| 1282 | map()->instance_descriptors()->CopyInsert(&d, REMOVE_TRANSITIONS); |
| 1283 | if (new_descriptors->IsFailure()) return new_descriptors; |
| 1284 | |
| 1285 | // Allocate a new map for the object. |
| 1286 | Object* new_map = map()->CopyDropDescriptors(); |
| 1287 | if (new_map->IsFailure()) return new_map; |
| 1288 | |
| 1289 | DescriptorArray* descriptors = DescriptorArray::cast(new_descriptors); |
| 1290 | Map::cast(new_map)->set_instance_descriptors(descriptors); |
| 1291 | Map* old_map = map(); |
| 1292 | set_map(Map::cast(new_map)); |
| 1293 | |
| 1294 | // If the old map is the global object map (from new Object()), |
| 1295 | // then transitions are not added to it, so we are done. |
| 1296 | if (old_map == Top::context()->global_context()->object_function()->map()) { |
| 1297 | return function; |
| 1298 | } |
| 1299 | |
| 1300 | // Do not add CONSTANT_TRANSITIONS to global objects |
| 1301 | if (IsGlobalObject()) { |
| 1302 | return function; |
| 1303 | } |
| 1304 | |
| 1305 | // Add a CONSTANT_TRANSITION descriptor to the old map, |
| 1306 | // so future assignments to this property on other objects |
| 1307 | // of the same type will create a normal field, not a constant function. |
| 1308 | // Don't do this for special properties, with non-trival attributes. |
| 1309 | if (attributes != NONE) { |
| 1310 | return function; |
| 1311 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1312 | ConstTransitionDescriptor mark(name, Map::cast(new_map)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1313 | new_descriptors = |
| 1314 | old_map->instance_descriptors()->CopyInsert(&mark, KEEP_TRANSITIONS); |
| 1315 | if (new_descriptors->IsFailure()) { |
| 1316 | return function; // We have accomplished the main goal, so return success. |
| 1317 | } |
| 1318 | old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1319 | |
| 1320 | return function; |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | // Add property in slow mode |
| 1325 | Object* JSObject::AddSlowProperty(String* name, |
| 1326 | Object* value, |
| 1327 | PropertyAttributes attributes) { |
| 1328 | ASSERT(!HasFastProperties()); |
| 1329 | StringDictionary* dict = property_dictionary(); |
| 1330 | Object* store_value = value; |
| 1331 | if (IsGlobalObject()) { |
| 1332 | // In case name is an orphaned property reuse the cell. |
| 1333 | int entry = dict->FindEntry(name); |
| 1334 | if (entry != StringDictionary::kNotFound) { |
| 1335 | store_value = dict->ValueAt(entry); |
| 1336 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
| 1337 | // Assign an enumeration index to the property and update |
| 1338 | // SetNextEnumerationIndex. |
| 1339 | int index = dict->NextEnumerationIndex(); |
| 1340 | PropertyDetails details = PropertyDetails(attributes, NORMAL, index); |
| 1341 | dict->SetNextEnumerationIndex(index + 1); |
| 1342 | dict->SetEntry(entry, name, store_value, details); |
| 1343 | return value; |
| 1344 | } |
| 1345 | store_value = Heap::AllocateJSGlobalPropertyCell(value); |
| 1346 | if (store_value->IsFailure()) return store_value; |
| 1347 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
| 1348 | } |
| 1349 | PropertyDetails details = PropertyDetails(attributes, NORMAL); |
| 1350 | Object* result = dict->Add(name, store_value, details); |
| 1351 | if (result->IsFailure()) return result; |
| 1352 | if (dict != result) set_properties(StringDictionary::cast(result)); |
| 1353 | return value; |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | Object* JSObject::AddProperty(String* name, |
| 1358 | Object* value, |
| 1359 | PropertyAttributes attributes) { |
| 1360 | ASSERT(!IsJSGlobalProxy()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1361 | if (!map()->is_extensible()) { |
| 1362 | Handle<Object> args[1] = {Handle<String>(name)}; |
| 1363 | return Top::Throw(*Factory::NewTypeError("object_not_extensible", |
| 1364 | HandleVector(args, 1))); |
| 1365 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1366 | if (HasFastProperties()) { |
| 1367 | // Ensure the descriptor array does not get too big. |
| 1368 | if (map()->instance_descriptors()->number_of_descriptors() < |
| 1369 | DescriptorArray::kMaxNumberOfDescriptors) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1370 | if (value->IsJSFunction() && !Heap::InNewSpace(value)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1371 | return AddConstantFunctionProperty(name, |
| 1372 | JSFunction::cast(value), |
| 1373 | attributes); |
| 1374 | } else { |
| 1375 | return AddFastProperty(name, value, attributes); |
| 1376 | } |
| 1377 | } else { |
| 1378 | // Normalize the object to prevent very large instance descriptors. |
| 1379 | // This eliminates unwanted N^2 allocation and lookup behavior. |
| 1380 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1381 | if (obj->IsFailure()) return obj; |
| 1382 | } |
| 1383 | } |
| 1384 | return AddSlowProperty(name, value, attributes); |
| 1385 | } |
| 1386 | |
| 1387 | |
| 1388 | Object* JSObject::SetPropertyPostInterceptor(String* name, |
| 1389 | Object* value, |
| 1390 | PropertyAttributes attributes) { |
| 1391 | // Check local property, ignore interceptor. |
| 1392 | LookupResult result; |
| 1393 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1394 | if (result.IsFound()) { |
| 1395 | // An existing property, a map transition or a null descriptor was |
| 1396 | // found. Use set property to handle all these cases. |
| 1397 | return SetProperty(&result, name, value, attributes); |
| 1398 | } |
| 1399 | // Add a new real property. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1400 | return AddProperty(name, value, attributes); |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | Object* JSObject::ReplaceSlowProperty(String* name, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1405 | Object* value, |
| 1406 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1407 | StringDictionary* dictionary = property_dictionary(); |
| 1408 | int old_index = dictionary->FindEntry(name); |
| 1409 | int new_enumeration_index = 0; // 0 means "Use the next available index." |
| 1410 | if (old_index != -1) { |
| 1411 | // All calls to ReplaceSlowProperty have had all transitions removed. |
| 1412 | ASSERT(!dictionary->DetailsAt(old_index).IsTransition()); |
| 1413 | new_enumeration_index = dictionary->DetailsAt(old_index).index(); |
| 1414 | } |
| 1415 | |
| 1416 | PropertyDetails new_details(attributes, NORMAL, new_enumeration_index); |
| 1417 | return SetNormalizedProperty(name, value, new_details); |
| 1418 | } |
| 1419 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1420 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1421 | Object* JSObject::ConvertDescriptorToFieldAndMapTransition( |
| 1422 | String* name, |
| 1423 | Object* new_value, |
| 1424 | PropertyAttributes attributes) { |
| 1425 | Map* old_map = map(); |
| 1426 | Object* result = ConvertDescriptorToField(name, new_value, attributes); |
| 1427 | if (result->IsFailure()) return result; |
| 1428 | // If we get to this point we have succeeded - do not return failure |
| 1429 | // after this point. Later stuff is optional. |
| 1430 | if (!HasFastProperties()) { |
| 1431 | return result; |
| 1432 | } |
| 1433 | // Do not add transitions to the map of "new Object()". |
| 1434 | if (map() == Top::context()->global_context()->object_function()->map()) { |
| 1435 | return result; |
| 1436 | } |
| 1437 | |
| 1438 | MapTransitionDescriptor transition(name, |
| 1439 | map(), |
| 1440 | attributes); |
| 1441 | Object* new_descriptors = |
| 1442 | old_map->instance_descriptors()-> |
| 1443 | CopyInsert(&transition, KEEP_TRANSITIONS); |
| 1444 | if (new_descriptors->IsFailure()) return result; // Yes, return _result_. |
| 1445 | old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1446 | return result; |
| 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | Object* JSObject::ConvertDescriptorToField(String* name, |
| 1451 | Object* new_value, |
| 1452 | PropertyAttributes attributes) { |
| 1453 | if (map()->unused_property_fields() == 0 && |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1454 | properties()->length() > MaxFastProperties()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1455 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1456 | if (obj->IsFailure()) return obj; |
| 1457 | return ReplaceSlowProperty(name, new_value, attributes); |
| 1458 | } |
| 1459 | |
| 1460 | int index = map()->NextFreePropertyIndex(); |
| 1461 | FieldDescriptor new_field(name, index, attributes); |
| 1462 | // Make a new DescriptorArray replacing an entry with FieldDescriptor. |
| 1463 | Object* descriptors_unchecked = map()->instance_descriptors()-> |
| 1464 | CopyInsert(&new_field, REMOVE_TRANSITIONS); |
| 1465 | if (descriptors_unchecked->IsFailure()) return descriptors_unchecked; |
| 1466 | DescriptorArray* new_descriptors = |
| 1467 | DescriptorArray::cast(descriptors_unchecked); |
| 1468 | |
| 1469 | // Make a new map for the object. |
| 1470 | Object* new_map_unchecked = map()->CopyDropDescriptors(); |
| 1471 | if (new_map_unchecked->IsFailure()) return new_map_unchecked; |
| 1472 | Map* new_map = Map::cast(new_map_unchecked); |
| 1473 | new_map->set_instance_descriptors(new_descriptors); |
| 1474 | |
| 1475 | // Make new properties array if necessary. |
| 1476 | FixedArray* new_properties = 0; // Will always be NULL or a valid pointer. |
| 1477 | int new_unused_property_fields = map()->unused_property_fields() - 1; |
| 1478 | if (map()->unused_property_fields() == 0) { |
| 1479 | new_unused_property_fields = kFieldsAdded - 1; |
| 1480 | Object* new_properties_unchecked = |
| 1481 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 1482 | if (new_properties_unchecked->IsFailure()) return new_properties_unchecked; |
| 1483 | new_properties = FixedArray::cast(new_properties_unchecked); |
| 1484 | } |
| 1485 | |
| 1486 | // Update pointers to commit changes. |
| 1487 | // Object points to the new map. |
| 1488 | new_map->set_unused_property_fields(new_unused_property_fields); |
| 1489 | set_map(new_map); |
| 1490 | if (new_properties) { |
| 1491 | set_properties(FixedArray::cast(new_properties)); |
| 1492 | } |
| 1493 | return FastPropertyAtPut(index, new_value); |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | Object* JSObject::SetPropertyWithInterceptor(String* name, |
| 1499 | Object* value, |
| 1500 | PropertyAttributes attributes) { |
| 1501 | HandleScope scope; |
| 1502 | Handle<JSObject> this_handle(this); |
| 1503 | Handle<String> name_handle(name); |
| 1504 | Handle<Object> value_handle(value); |
| 1505 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 1506 | if (!interceptor->setter()->IsUndefined()) { |
| 1507 | LOG(ApiNamedPropertyAccess("interceptor-named-set", this, name)); |
| 1508 | CustomArguments args(interceptor->data(), this, this); |
| 1509 | v8::AccessorInfo info(args.end()); |
| 1510 | v8::NamedPropertySetter setter = |
| 1511 | v8::ToCData<v8::NamedPropertySetter>(interceptor->setter()); |
| 1512 | v8::Handle<v8::Value> result; |
| 1513 | { |
| 1514 | // Leaving JavaScript. |
| 1515 | VMState state(EXTERNAL); |
| 1516 | Handle<Object> value_unhole(value->IsTheHole() ? |
| 1517 | Heap::undefined_value() : |
| 1518 | value); |
| 1519 | result = setter(v8::Utils::ToLocal(name_handle), |
| 1520 | v8::Utils::ToLocal(value_unhole), |
| 1521 | info); |
| 1522 | } |
| 1523 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 1524 | if (!result.IsEmpty()) return *value_handle; |
| 1525 | } |
| 1526 | Object* raw_result = this_handle->SetPropertyPostInterceptor(*name_handle, |
| 1527 | *value_handle, |
| 1528 | attributes); |
| 1529 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 1530 | return raw_result; |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | Object* JSObject::SetProperty(String* name, |
| 1535 | Object* value, |
| 1536 | PropertyAttributes attributes) { |
| 1537 | LookupResult result; |
| 1538 | LocalLookup(name, &result); |
| 1539 | return SetProperty(&result, name, value, attributes); |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | Object* JSObject::SetPropertyWithCallback(Object* structure, |
| 1544 | String* name, |
| 1545 | Object* value, |
| 1546 | JSObject* holder) { |
| 1547 | HandleScope scope; |
| 1548 | |
| 1549 | // We should never get here to initialize a const with the hole |
| 1550 | // value since a const declaration would conflict with the setter. |
| 1551 | ASSERT(!value->IsTheHole()); |
| 1552 | Handle<Object> value_handle(value); |
| 1553 | |
| 1554 | // To accommodate both the old and the new api we switch on the |
| 1555 | // data structure used to store the callbacks. Eventually proxy |
| 1556 | // callbacks should be phased out. |
| 1557 | if (structure->IsProxy()) { |
| 1558 | AccessorDescriptor* callback = |
| 1559 | reinterpret_cast<AccessorDescriptor*>(Proxy::cast(structure)->proxy()); |
| 1560 | Object* obj = (callback->setter)(this, value, callback->data); |
| 1561 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 1562 | if (obj->IsFailure()) return obj; |
| 1563 | return *value_handle; |
| 1564 | } |
| 1565 | |
| 1566 | if (structure->IsAccessorInfo()) { |
| 1567 | // api style callbacks |
| 1568 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 1569 | Object* call_obj = data->setter(); |
| 1570 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 1571 | if (call_fun == NULL) return value; |
| 1572 | Handle<String> key(name); |
| 1573 | LOG(ApiNamedPropertyAccess("store", this, name)); |
| 1574 | CustomArguments args(data->data(), this, JSObject::cast(holder)); |
| 1575 | v8::AccessorInfo info(args.end()); |
| 1576 | { |
| 1577 | // Leaving JavaScript. |
| 1578 | VMState state(EXTERNAL); |
| 1579 | call_fun(v8::Utils::ToLocal(key), |
| 1580 | v8::Utils::ToLocal(value_handle), |
| 1581 | info); |
| 1582 | } |
| 1583 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 1584 | return *value_handle; |
| 1585 | } |
| 1586 | |
| 1587 | if (structure->IsFixedArray()) { |
| 1588 | Object* setter = FixedArray::cast(structure)->get(kSetterIndex); |
| 1589 | if (setter->IsJSFunction()) { |
| 1590 | return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); |
| 1591 | } else { |
| 1592 | Handle<String> key(name); |
| 1593 | Handle<Object> holder_handle(holder); |
| 1594 | Handle<Object> args[2] = { key, holder_handle }; |
| 1595 | return Top::Throw(*Factory::NewTypeError("no_setter_in_callback", |
| 1596 | HandleVector(args, 2))); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | UNREACHABLE(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1601 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | Object* JSObject::SetPropertyWithDefinedSetter(JSFunction* setter, |
| 1606 | Object* value) { |
| 1607 | Handle<Object> value_handle(value); |
| 1608 | Handle<JSFunction> fun(JSFunction::cast(setter)); |
| 1609 | Handle<JSObject> self(this); |
| 1610 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1611 | // Handle stepping into a setter if step into is active. |
| 1612 | if (Debug::StepInActive()) { |
| 1613 | Debug::HandleStepIn(fun, Handle<Object>::null(), 0, false); |
| 1614 | } |
| 1615 | #endif |
| 1616 | bool has_pending_exception; |
| 1617 | Object** argv[] = { value_handle.location() }; |
| 1618 | Execution::Call(fun, self, 1, argv, &has_pending_exception); |
| 1619 | // Check for pending exception and return the result. |
| 1620 | if (has_pending_exception) return Failure::Exception(); |
| 1621 | return *value_handle; |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | void JSObject::LookupCallbackSetterInPrototypes(String* name, |
| 1626 | LookupResult* result) { |
| 1627 | for (Object* pt = GetPrototype(); |
| 1628 | pt != Heap::null_value(); |
| 1629 | pt = pt->GetPrototype()) { |
| 1630 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1631 | if (result->IsProperty()) { |
| 1632 | if (result->IsReadOnly()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1633 | result->NotFound(); |
| 1634 | return; |
| 1635 | } |
| 1636 | if (result->type() == CALLBACKS) { |
| 1637 | return; |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | result->NotFound(); |
| 1642 | } |
| 1643 | |
| 1644 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1645 | bool JSObject::SetElementWithCallbackSetterInPrototypes(uint32_t index, |
| 1646 | Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1647 | for (Object* pt = GetPrototype(); |
| 1648 | pt != Heap::null_value(); |
| 1649 | pt = pt->GetPrototype()) { |
| 1650 | if (!JSObject::cast(pt)->HasDictionaryElements()) { |
| 1651 | continue; |
| 1652 | } |
| 1653 | NumberDictionary* dictionary = JSObject::cast(pt)->element_dictionary(); |
| 1654 | int entry = dictionary->FindEntry(index); |
| 1655 | if (entry != NumberDictionary::kNotFound) { |
| 1656 | Object* element = dictionary->ValueAt(entry); |
| 1657 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 1658 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1659 | SetElementWithCallback(element, index, value, JSObject::cast(pt)); |
| 1660 | return true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1664 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | |
| 1668 | void JSObject::LookupInDescriptor(String* name, LookupResult* result) { |
| 1669 | DescriptorArray* descriptors = map()->instance_descriptors(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1670 | int number = descriptors->SearchWithCache(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1671 | if (number != DescriptorArray::kNotFound) { |
| 1672 | result->DescriptorResult(this, descriptors->GetDetails(number), number); |
| 1673 | } else { |
| 1674 | result->NotFound(); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | |
| 1679 | void JSObject::LocalLookupRealNamedProperty(String* name, |
| 1680 | LookupResult* result) { |
| 1681 | if (IsJSGlobalProxy()) { |
| 1682 | Object* proto = GetPrototype(); |
| 1683 | if (proto->IsNull()) return result->NotFound(); |
| 1684 | ASSERT(proto->IsJSGlobalObject()); |
| 1685 | return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result); |
| 1686 | } |
| 1687 | |
| 1688 | if (HasFastProperties()) { |
| 1689 | LookupInDescriptor(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1690 | if (result->IsFound()) { |
| 1691 | // A property, a map transition or a null descriptor was found. |
| 1692 | // We return all of these result types because |
| 1693 | // LocalLookupRealNamedProperty is used when setting properties |
| 1694 | // where map transitions and null descriptors are handled. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1695 | ASSERT(result->holder() == this && result->type() != NORMAL); |
| 1696 | // Disallow caching for uninitialized constants. These can only |
| 1697 | // occur as fields. |
| 1698 | if (result->IsReadOnly() && result->type() == FIELD && |
| 1699 | FastPropertyAt(result->GetFieldIndex())->IsTheHole()) { |
| 1700 | result->DisallowCaching(); |
| 1701 | } |
| 1702 | return; |
| 1703 | } |
| 1704 | } else { |
| 1705 | int entry = property_dictionary()->FindEntry(name); |
| 1706 | if (entry != StringDictionary::kNotFound) { |
| 1707 | Object* value = property_dictionary()->ValueAt(entry); |
| 1708 | if (IsGlobalObject()) { |
| 1709 | PropertyDetails d = property_dictionary()->DetailsAt(entry); |
| 1710 | if (d.IsDeleted()) { |
| 1711 | result->NotFound(); |
| 1712 | return; |
| 1713 | } |
| 1714 | value = JSGlobalPropertyCell::cast(value)->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1715 | } |
| 1716 | // Make sure to disallow caching for uninitialized constants |
| 1717 | // found in the dictionary-mode objects. |
| 1718 | if (value->IsTheHole()) result->DisallowCaching(); |
| 1719 | result->DictionaryResult(this, entry); |
| 1720 | return; |
| 1721 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1722 | } |
| 1723 | result->NotFound(); |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | void JSObject::LookupRealNamedProperty(String* name, LookupResult* result) { |
| 1728 | LocalLookupRealNamedProperty(name, result); |
| 1729 | if (result->IsProperty()) return; |
| 1730 | |
| 1731 | LookupRealNamedPropertyInPrototypes(name, result); |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | void JSObject::LookupRealNamedPropertyInPrototypes(String* name, |
| 1736 | LookupResult* result) { |
| 1737 | for (Object* pt = GetPrototype(); |
| 1738 | pt != Heap::null_value(); |
| 1739 | pt = JSObject::cast(pt)->GetPrototype()) { |
| 1740 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1741 | if (result->IsProperty() && (result->type() != INTERCEPTOR)) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1742 | } |
| 1743 | result->NotFound(); |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | // We only need to deal with CALLBACKS and INTERCEPTORS |
| 1748 | Object* JSObject::SetPropertyWithFailedAccessCheck(LookupResult* result, |
| 1749 | String* name, |
| 1750 | Object* value) { |
| 1751 | if (!result->IsProperty()) { |
| 1752 | LookupCallbackSetterInPrototypes(name, result); |
| 1753 | } |
| 1754 | |
| 1755 | if (result->IsProperty()) { |
| 1756 | if (!result->IsReadOnly()) { |
| 1757 | switch (result->type()) { |
| 1758 | case CALLBACKS: { |
| 1759 | Object* obj = result->GetCallbackObject(); |
| 1760 | if (obj->IsAccessorInfo()) { |
| 1761 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 1762 | if (info->all_can_write()) { |
| 1763 | return SetPropertyWithCallback(result->GetCallbackObject(), |
| 1764 | name, |
| 1765 | value, |
| 1766 | result->holder()); |
| 1767 | } |
| 1768 | } |
| 1769 | break; |
| 1770 | } |
| 1771 | case INTERCEPTOR: { |
| 1772 | // Try lookup real named properties. Note that only property can be |
| 1773 | // set is callbacks marked as ALL_CAN_WRITE on the prototype chain. |
| 1774 | LookupResult r; |
| 1775 | LookupRealNamedProperty(name, &r); |
| 1776 | if (r.IsProperty()) { |
| 1777 | return SetPropertyWithFailedAccessCheck(&r, name, value); |
| 1778 | } |
| 1779 | break; |
| 1780 | } |
| 1781 | default: { |
| 1782 | break; |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1788 | HandleScope scope; |
| 1789 | Handle<Object> value_handle(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1790 | Top::ReportFailedAccessCheck(this, v8::ACCESS_SET); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1791 | return *value_handle; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | Object* JSObject::SetProperty(LookupResult* result, |
| 1796 | String* name, |
| 1797 | Object* value, |
| 1798 | PropertyAttributes attributes) { |
| 1799 | // Make sure that the top context does not change when doing callbacks or |
| 1800 | // interceptor calls. |
| 1801 | AssertNoContextChange ncc; |
| 1802 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1803 | // Optimization for 2-byte strings often used as keys in a decompression |
| 1804 | // dictionary. We make these short keys into symbols to avoid constantly |
| 1805 | // reallocating them. |
| 1806 | if (!name->IsSymbol() && name->length() <= 2) { |
| 1807 | Object* symbol_version = Heap::LookupSymbol(name); |
| 1808 | if (!symbol_version->IsFailure()) name = String::cast(symbol_version); |
| 1809 | } |
| 1810 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1811 | // Check access rights if needed. |
| 1812 | if (IsAccessCheckNeeded() |
| 1813 | && !Top::MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 1814 | return SetPropertyWithFailedAccessCheck(result, name, value); |
| 1815 | } |
| 1816 | |
| 1817 | if (IsJSGlobalProxy()) { |
| 1818 | Object* proto = GetPrototype(); |
| 1819 | if (proto->IsNull()) return value; |
| 1820 | ASSERT(proto->IsJSGlobalObject()); |
| 1821 | return JSObject::cast(proto)->SetProperty(result, name, value, attributes); |
| 1822 | } |
| 1823 | |
| 1824 | if (!result->IsProperty() && !IsJSContextExtensionObject()) { |
| 1825 | // We could not find a local property so let's check whether there is an |
| 1826 | // accessor that wants to handle the property. |
| 1827 | LookupResult accessor_result; |
| 1828 | LookupCallbackSetterInPrototypes(name, &accessor_result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1829 | if (accessor_result.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1830 | return SetPropertyWithCallback(accessor_result.GetCallbackObject(), |
| 1831 | name, |
| 1832 | value, |
| 1833 | accessor_result.holder()); |
| 1834 | } |
| 1835 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1836 | if (!result->IsFound()) { |
| 1837 | // Neither properties nor transitions found. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1838 | return AddProperty(name, value, attributes); |
| 1839 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1840 | if (result->IsReadOnly() && result->IsProperty()) return value; |
| 1841 | // This is a real property that is not read-only, or it is a |
| 1842 | // transition or null descriptor and there are no setters in the prototypes. |
| 1843 | switch (result->type()) { |
| 1844 | case NORMAL: |
| 1845 | return SetNormalizedProperty(result, value); |
| 1846 | case FIELD: |
| 1847 | return FastPropertyAtPut(result->GetFieldIndex(), value); |
| 1848 | case MAP_TRANSITION: |
| 1849 | if (attributes == result->GetAttributes()) { |
| 1850 | // Only use map transition if the attributes match. |
| 1851 | return AddFastPropertyUsingMap(result->GetTransitionMap(), |
| 1852 | name, |
| 1853 | value); |
| 1854 | } |
| 1855 | return ConvertDescriptorToField(name, value, attributes); |
| 1856 | case CONSTANT_FUNCTION: |
| 1857 | // Only replace the function if necessary. |
| 1858 | if (value == result->GetConstantFunction()) return value; |
| 1859 | // Preserve the attributes of this existing property. |
| 1860 | attributes = result->GetAttributes(); |
| 1861 | return ConvertDescriptorToField(name, value, attributes); |
| 1862 | case CALLBACKS: |
| 1863 | return SetPropertyWithCallback(result->GetCallbackObject(), |
| 1864 | name, |
| 1865 | value, |
| 1866 | result->holder()); |
| 1867 | case INTERCEPTOR: |
| 1868 | return SetPropertyWithInterceptor(name, value, attributes); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1869 | case CONSTANT_TRANSITION: { |
| 1870 | // If the same constant function is being added we can simply |
| 1871 | // transition to the target map. |
| 1872 | Map* target_map = result->GetTransitionMap(); |
| 1873 | DescriptorArray* target_descriptors = target_map->instance_descriptors(); |
| 1874 | int number = target_descriptors->SearchWithCache(name); |
| 1875 | ASSERT(number != DescriptorArray::kNotFound); |
| 1876 | ASSERT(target_descriptors->GetType(number) == CONSTANT_FUNCTION); |
| 1877 | JSFunction* function = |
| 1878 | JSFunction::cast(target_descriptors->GetValue(number)); |
| 1879 | ASSERT(!Heap::InNewSpace(function)); |
| 1880 | if (value == function) { |
| 1881 | set_map(target_map); |
| 1882 | return value; |
| 1883 | } |
| 1884 | // Otherwise, replace with a MAP_TRANSITION to a new map with a |
| 1885 | // FIELD, even if the value is a constant function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1886 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1887 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1888 | case NULL_DESCRIPTOR: |
| 1889 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 1890 | default: |
| 1891 | UNREACHABLE(); |
| 1892 | } |
| 1893 | UNREACHABLE(); |
| 1894 | return value; |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | // Set a real local property, even if it is READ_ONLY. If the property is not |
| 1899 | // present, add it with attributes NONE. This code is an exact clone of |
| 1900 | // SetProperty, with the check for IsReadOnly and the check for a |
| 1901 | // callback setter removed. The two lines looking up the LookupResult |
| 1902 | // result are also added. If one of the functions is changed, the other |
| 1903 | // should be. |
| 1904 | Object* JSObject::IgnoreAttributesAndSetLocalProperty( |
| 1905 | String* name, |
| 1906 | Object* value, |
| 1907 | PropertyAttributes attributes) { |
| 1908 | // Make sure that the top context does not change when doing callbacks or |
| 1909 | // interceptor calls. |
| 1910 | AssertNoContextChange ncc; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1911 | LookupResult result; |
| 1912 | LocalLookup(name, &result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1913 | // Check access rights if needed. |
| 1914 | if (IsAccessCheckNeeded() |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1915 | && !Top::MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 1916 | return SetPropertyWithFailedAccessCheck(&result, name, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | if (IsJSGlobalProxy()) { |
| 1920 | Object* proto = GetPrototype(); |
| 1921 | if (proto->IsNull()) return value; |
| 1922 | ASSERT(proto->IsJSGlobalObject()); |
| 1923 | return JSObject::cast(proto)->IgnoreAttributesAndSetLocalProperty( |
| 1924 | name, |
| 1925 | value, |
| 1926 | attributes); |
| 1927 | } |
| 1928 | |
| 1929 | // Check for accessor in prototype chain removed here in clone. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1930 | if (!result.IsFound()) { |
| 1931 | // Neither properties nor transitions found. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | return AddProperty(name, value, attributes); |
| 1933 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1934 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1935 | PropertyDetails details = PropertyDetails(attributes, NORMAL); |
| 1936 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1937 | // Check of IsReadOnly removed from here in clone. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1938 | switch (result.type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1939 | case NORMAL: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1940 | return SetNormalizedProperty(name, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1941 | case FIELD: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1942 | return FastPropertyAtPut(result.GetFieldIndex(), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1943 | case MAP_TRANSITION: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1944 | if (attributes == result.GetAttributes()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1945 | // Only use map transition if the attributes match. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1946 | return AddFastPropertyUsingMap(result.GetTransitionMap(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1947 | name, |
| 1948 | value); |
| 1949 | } |
| 1950 | return ConvertDescriptorToField(name, value, attributes); |
| 1951 | case CONSTANT_FUNCTION: |
| 1952 | // Only replace the function if necessary. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1953 | if (value == result.GetConstantFunction()) return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1954 | // Preserve the attributes of this existing property. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1955 | attributes = result.GetAttributes(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1956 | return ConvertDescriptorToField(name, value, attributes); |
| 1957 | case CALLBACKS: |
| 1958 | case INTERCEPTOR: |
| 1959 | // Override callback in clone |
| 1960 | return ConvertDescriptorToField(name, value, attributes); |
| 1961 | case CONSTANT_TRANSITION: |
| 1962 | // Replace with a MAP_TRANSITION to a new map with a FIELD, even |
| 1963 | // if the value is a function. |
| 1964 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 1965 | case NULL_DESCRIPTOR: |
| 1966 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 1967 | default: |
| 1968 | UNREACHABLE(); |
| 1969 | } |
| 1970 | UNREACHABLE(); |
| 1971 | return value; |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | PropertyAttributes JSObject::GetPropertyAttributePostInterceptor( |
| 1976 | JSObject* receiver, |
| 1977 | String* name, |
| 1978 | bool continue_search) { |
| 1979 | // Check local property, ignore interceptor. |
| 1980 | LookupResult result; |
| 1981 | LocalLookupRealNamedProperty(name, &result); |
| 1982 | if (result.IsProperty()) return result.GetAttributes(); |
| 1983 | |
| 1984 | if (continue_search) { |
| 1985 | // Continue searching via the prototype chain. |
| 1986 | Object* pt = GetPrototype(); |
| 1987 | if (pt != Heap::null_value()) { |
| 1988 | return JSObject::cast(pt)-> |
| 1989 | GetPropertyAttributeWithReceiver(receiver, name); |
| 1990 | } |
| 1991 | } |
| 1992 | return ABSENT; |
| 1993 | } |
| 1994 | |
| 1995 | |
| 1996 | PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor( |
| 1997 | JSObject* receiver, |
| 1998 | String* name, |
| 1999 | bool continue_search) { |
| 2000 | // Make sure that the top context does not change when doing |
| 2001 | // callbacks or interceptor calls. |
| 2002 | AssertNoContextChange ncc; |
| 2003 | |
| 2004 | HandleScope scope; |
| 2005 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 2006 | Handle<JSObject> receiver_handle(receiver); |
| 2007 | Handle<JSObject> holder_handle(this); |
| 2008 | Handle<String> name_handle(name); |
| 2009 | CustomArguments args(interceptor->data(), receiver, this); |
| 2010 | v8::AccessorInfo info(args.end()); |
| 2011 | if (!interceptor->query()->IsUndefined()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2012 | v8::NamedPropertyQuery query = |
| 2013 | v8::ToCData<v8::NamedPropertyQuery>(interceptor->query()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2014 | LOG(ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2015 | v8::Handle<v8::Integer> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2016 | { |
| 2017 | // Leaving JavaScript. |
| 2018 | VMState state(EXTERNAL); |
| 2019 | result = query(v8::Utils::ToLocal(name_handle), info); |
| 2020 | } |
| 2021 | if (!result.IsEmpty()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2022 | ASSERT(result->IsInt32()); |
| 2023 | return static_cast<PropertyAttributes>(result->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2024 | } |
| 2025 | } else if (!interceptor->getter()->IsUndefined()) { |
| 2026 | v8::NamedPropertyGetter getter = |
| 2027 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
| 2028 | LOG(ApiNamedPropertyAccess("interceptor-named-get-has", this, name)); |
| 2029 | v8::Handle<v8::Value> result; |
| 2030 | { |
| 2031 | // Leaving JavaScript. |
| 2032 | VMState state(EXTERNAL); |
| 2033 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 2034 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2035 | if (!result.IsEmpty()) return DONT_ENUM; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2036 | } |
| 2037 | return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle, |
| 2038 | *name_handle, |
| 2039 | continue_search); |
| 2040 | } |
| 2041 | |
| 2042 | |
| 2043 | PropertyAttributes JSObject::GetPropertyAttributeWithReceiver( |
| 2044 | JSObject* receiver, |
| 2045 | String* key) { |
| 2046 | uint32_t index = 0; |
| 2047 | if (key->AsArrayIndex(&index)) { |
| 2048 | if (HasElementWithReceiver(receiver, index)) return NONE; |
| 2049 | return ABSENT; |
| 2050 | } |
| 2051 | // Named property. |
| 2052 | LookupResult result; |
| 2053 | Lookup(key, &result); |
| 2054 | return GetPropertyAttribute(receiver, &result, key, true); |
| 2055 | } |
| 2056 | |
| 2057 | |
| 2058 | PropertyAttributes JSObject::GetPropertyAttribute(JSObject* receiver, |
| 2059 | LookupResult* result, |
| 2060 | String* name, |
| 2061 | bool continue_search) { |
| 2062 | // Check access rights if needed. |
| 2063 | if (IsAccessCheckNeeded() && |
| 2064 | !Top::MayNamedAccess(this, name, v8::ACCESS_HAS)) { |
| 2065 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 2066 | result, |
| 2067 | name, |
| 2068 | continue_search); |
| 2069 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2070 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2071 | switch (result->type()) { |
| 2072 | case NORMAL: // fall through |
| 2073 | case FIELD: |
| 2074 | case CONSTANT_FUNCTION: |
| 2075 | case CALLBACKS: |
| 2076 | return result->GetAttributes(); |
| 2077 | case INTERCEPTOR: |
| 2078 | return result->holder()-> |
| 2079 | GetPropertyAttributeWithInterceptor(receiver, name, continue_search); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2080 | default: |
| 2081 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | return ABSENT; |
| 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | PropertyAttributes JSObject::GetLocalPropertyAttribute(String* name) { |
| 2089 | // Check whether the name is an array index. |
| 2090 | uint32_t index = 0; |
| 2091 | if (name->AsArrayIndex(&index)) { |
| 2092 | if (HasLocalElement(index)) return NONE; |
| 2093 | return ABSENT; |
| 2094 | } |
| 2095 | // Named property. |
| 2096 | LookupResult result; |
| 2097 | LocalLookup(name, &result); |
| 2098 | return GetPropertyAttribute(this, &result, name, false); |
| 2099 | } |
| 2100 | |
| 2101 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 2102 | bool NormalizedMapCache::IsCacheable(JSObject* object) { |
| 2103 | // Caching for global objects is not worth it (there are too few of them). |
| 2104 | return !object->IsGlobalObject(); |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | Object* NormalizedMapCache::Get(JSObject* obj, PropertyNormalizationMode mode) { |
| 2109 | Object* result; |
| 2110 | |
| 2111 | Map* fast = obj->map(); |
| 2112 | if (!IsCacheable(obj)) { |
| 2113 | result = fast->CopyNormalized(mode); |
| 2114 | if (result->IsFailure()) return result; |
| 2115 | } else { |
| 2116 | int index = Hash(fast) % kEntries; |
| 2117 | result = get(index); |
| 2118 | |
| 2119 | if (result->IsMap() && CheckHit(Map::cast(result), fast, mode)) { |
| 2120 | #ifdef DEBUG |
| 2121 | if (FLAG_enable_slow_asserts) { |
| 2122 | // Make sure that the new slow map has exactly the same hash as the |
| 2123 | // original fast map. This way we can use hash to check if a slow map |
| 2124 | // is already in the hash (see Contains method). |
| 2125 | ASSERT(Hash(fast) == Hash(Map::cast(result))); |
| 2126 | // The cached map should match newly created normalized map bit-by-bit. |
| 2127 | Object* fresh = fast->CopyNormalized(mode); |
| 2128 | if (!fresh->IsFailure()) { |
| 2129 | // Copy the unused byte so that the assertion below works. |
| 2130 | Map::cast(fresh)->address()[Map::kUnusedOffset] = |
| 2131 | Map::cast(result)->address()[Map::kUnusedOffset]; |
| 2132 | ASSERT(memcmp(Map::cast(fresh)->address(), |
| 2133 | Map::cast(result)->address(), |
| 2134 | Map::kSize) == 0); |
| 2135 | } |
| 2136 | } |
| 2137 | #endif |
| 2138 | return result; |
| 2139 | } |
| 2140 | |
| 2141 | result = fast->CopyNormalized(mode); |
| 2142 | if (result->IsFailure()) return result; |
| 2143 | set(index, result); |
| 2144 | } |
| 2145 | Counters::normalized_maps.Increment(); |
| 2146 | |
| 2147 | return result; |
| 2148 | } |
| 2149 | |
| 2150 | |
| 2151 | bool NormalizedMapCache::Contains(Map* map) { |
| 2152 | // If the map is present in the cache it can only be at one place: |
| 2153 | // at the index calculated from the hash. We assume that a slow map has the |
| 2154 | // same hash as a fast map it has been generated from. |
| 2155 | int index = Hash(map) % kEntries; |
| 2156 | return get(index) == map; |
| 2157 | } |
| 2158 | |
| 2159 | |
| 2160 | void NormalizedMapCache::Clear() { |
| 2161 | int entries = length(); |
| 2162 | for (int i = 0; i != entries; i++) { |
| 2163 | set_undefined(i); |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | int NormalizedMapCache::Hash(Map* fast) { |
| 2169 | // For performance reasons we only hash the 3 most variable fields of a map: |
| 2170 | // constructor, prototype and bit_field2. |
| 2171 | |
| 2172 | // Shift away the tag. |
| 2173 | int hash = (static_cast<uint32_t>( |
| 2174 | reinterpret_cast<uintptr_t>(fast->constructor())) >> 2); |
| 2175 | |
| 2176 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 2177 | // when the two pointers are close (which is fairly common). |
| 2178 | // To avoid this we shift the prototype 4 bits relatively to the constructor. |
| 2179 | hash ^= (static_cast<uint32_t>( |
| 2180 | reinterpret_cast<uintptr_t>(fast->prototype())) << 2); |
| 2181 | |
| 2182 | return hash ^ (hash >> 16) ^ fast->bit_field2(); |
| 2183 | } |
| 2184 | |
| 2185 | |
| 2186 | bool NormalizedMapCache::CheckHit(Map* slow, |
| 2187 | Map* fast, |
| 2188 | PropertyNormalizationMode mode) { |
| 2189 | #ifdef DEBUG |
| 2190 | slow->NormalizedMapVerify(); |
| 2191 | #endif |
| 2192 | return |
| 2193 | slow->constructor() == fast->constructor() && |
| 2194 | slow->prototype() == fast->prototype() && |
| 2195 | slow->inobject_properties() == ((mode == CLEAR_INOBJECT_PROPERTIES) ? |
| 2196 | 0 : |
| 2197 | fast->inobject_properties()) && |
| 2198 | slow->instance_type() == fast->instance_type() && |
| 2199 | slow->bit_field() == fast->bit_field() && |
| 2200 | slow->bit_field2() == fast->bit_field2(); |
| 2201 | } |
| 2202 | |
| 2203 | |
| 2204 | Object* JSObject::UpdateMapCodeCache(String* name, Code* code) { |
| 2205 | if (!HasFastProperties() && |
| 2206 | NormalizedMapCache::IsCacheable(this) && |
| 2207 | Top::context()->global_context()->normalized_map_cache()-> |
| 2208 | Contains(map())) { |
| 2209 | // Replace the map with the identical copy that can be safely modified. |
| 2210 | Object* obj = map()->CopyNormalized(KEEP_INOBJECT_PROPERTIES); |
| 2211 | if (obj->IsFailure()) return obj; |
| 2212 | Counters::normalized_maps.Increment(); |
| 2213 | |
| 2214 | set_map(Map::cast(obj)); |
| 2215 | } |
| 2216 | return map()->UpdateCodeCache(name, code); |
| 2217 | } |
| 2218 | |
| 2219 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2220 | Object* JSObject::NormalizeProperties(PropertyNormalizationMode mode, |
| 2221 | int expected_additional_properties) { |
| 2222 | if (!HasFastProperties()) return this; |
| 2223 | |
| 2224 | // The global object is always normalized. |
| 2225 | ASSERT(!IsGlobalObject()); |
| 2226 | |
| 2227 | // Allocate new content. |
| 2228 | int property_count = map()->NumberOfDescribedProperties(); |
| 2229 | if (expected_additional_properties > 0) { |
| 2230 | property_count += expected_additional_properties; |
| 2231 | } else { |
| 2232 | property_count += 2; // Make space for two more properties. |
| 2233 | } |
| 2234 | Object* obj = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2235 | StringDictionary::Allocate(property_count); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2236 | if (obj->IsFailure()) return obj; |
| 2237 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 2238 | |
| 2239 | DescriptorArray* descs = map()->instance_descriptors(); |
| 2240 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2241 | PropertyDetails details = descs->GetDetails(i); |
| 2242 | switch (details.type()) { |
| 2243 | case CONSTANT_FUNCTION: { |
| 2244 | PropertyDetails d = |
| 2245 | PropertyDetails(details.attributes(), NORMAL, details.index()); |
| 2246 | Object* value = descs->GetConstantFunction(i); |
| 2247 | Object* result = dictionary->Add(descs->GetKey(i), value, d); |
| 2248 | if (result->IsFailure()) return result; |
| 2249 | dictionary = StringDictionary::cast(result); |
| 2250 | break; |
| 2251 | } |
| 2252 | case FIELD: { |
| 2253 | PropertyDetails d = |
| 2254 | PropertyDetails(details.attributes(), NORMAL, details.index()); |
| 2255 | Object* value = FastPropertyAt(descs->GetFieldIndex(i)); |
| 2256 | Object* result = dictionary->Add(descs->GetKey(i), value, d); |
| 2257 | if (result->IsFailure()) return result; |
| 2258 | dictionary = StringDictionary::cast(result); |
| 2259 | break; |
| 2260 | } |
| 2261 | case CALLBACKS: { |
| 2262 | PropertyDetails d = |
| 2263 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 2264 | Object* value = descs->GetCallbacksObject(i); |
| 2265 | Object* result = dictionary->Add(descs->GetKey(i), value, d); |
| 2266 | if (result->IsFailure()) return result; |
| 2267 | dictionary = StringDictionary::cast(result); |
| 2268 | break; |
| 2269 | } |
| 2270 | case MAP_TRANSITION: |
| 2271 | case CONSTANT_TRANSITION: |
| 2272 | case NULL_DESCRIPTOR: |
| 2273 | case INTERCEPTOR: |
| 2274 | break; |
| 2275 | default: |
| 2276 | UNREACHABLE(); |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | // Copy the next enumeration index from instance descriptor. |
| 2281 | int index = map()->instance_descriptors()->NextEnumerationIndex(); |
| 2282 | dictionary->SetNextEnumerationIndex(index); |
| 2283 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 2284 | obj = Top::context()->global_context()-> |
| 2285 | normalized_map_cache()->Get(this, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2286 | if (obj->IsFailure()) return obj; |
| 2287 | Map* new_map = Map::cast(obj); |
| 2288 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2289 | // We have now successfully allocated all the necessary objects. |
| 2290 | // Changes can now be made with the guarantee that all of them take effect. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 2291 | |
| 2292 | // Resize the object in the heap if necessary. |
| 2293 | int new_instance_size = new_map->instance_size(); |
| 2294 | int instance_size_delta = map()->instance_size() - new_instance_size; |
| 2295 | ASSERT(instance_size_delta >= 0); |
| 2296 | Heap::CreateFillerObjectAt(this->address() + new_instance_size, |
| 2297 | instance_size_delta); |
| 2298 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2299 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2300 | |
| 2301 | set_properties(dictionary); |
| 2302 | |
| 2303 | Counters::props_to_dictionary.Increment(); |
| 2304 | |
| 2305 | #ifdef DEBUG |
| 2306 | if (FLAG_trace_normalization) { |
| 2307 | PrintF("Object properties have been normalized:\n"); |
| 2308 | Print(); |
| 2309 | } |
| 2310 | #endif |
| 2311 | return this; |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | Object* JSObject::TransformToFastProperties(int unused_property_fields) { |
| 2316 | if (HasFastProperties()) return this; |
| 2317 | ASSERT(!IsGlobalObject()); |
| 2318 | return property_dictionary()-> |
| 2319 | TransformPropertiesToFastFor(this, unused_property_fields); |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | Object* JSObject::NormalizeElements() { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2324 | ASSERT(!HasPixelElements() && !HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2325 | if (HasDictionaryElements()) return this; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2326 | ASSERT(map()->has_fast_elements()); |
| 2327 | |
| 2328 | Object* obj = map()->GetSlowElementsMap(); |
| 2329 | if (obj->IsFailure()) return obj; |
| 2330 | Map* new_map = Map::cast(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Get number of entries. |
| 2333 | FixedArray* array = FixedArray::cast(elements()); |
| 2334 | |
| 2335 | // Compute the effective length. |
| 2336 | int length = IsJSArray() ? |
| 2337 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 2338 | array->length(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2339 | obj = NumberDictionary::Allocate(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2340 | if (obj->IsFailure()) return obj; |
| 2341 | NumberDictionary* dictionary = NumberDictionary::cast(obj); |
| 2342 | // Copy entries. |
| 2343 | for (int i = 0; i < length; i++) { |
| 2344 | Object* value = array->get(i); |
| 2345 | if (!value->IsTheHole()) { |
| 2346 | PropertyDetails details = PropertyDetails(NONE, NORMAL); |
| 2347 | Object* result = dictionary->AddNumberEntry(i, array->get(i), details); |
| 2348 | if (result->IsFailure()) return result; |
| 2349 | dictionary = NumberDictionary::cast(result); |
| 2350 | } |
| 2351 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2352 | // Switch to using the dictionary as the backing storage for |
| 2353 | // elements. Set the new map first to satify the elements type |
| 2354 | // assert in set_elements(). |
| 2355 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2356 | set_elements(dictionary); |
| 2357 | |
| 2358 | Counters::elements_to_dictionary.Increment(); |
| 2359 | |
| 2360 | #ifdef DEBUG |
| 2361 | if (FLAG_trace_normalization) { |
| 2362 | PrintF("Object elements have been normalized:\n"); |
| 2363 | Print(); |
| 2364 | } |
| 2365 | #endif |
| 2366 | |
| 2367 | return this; |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | Object* JSObject::DeletePropertyPostInterceptor(String* name, DeleteMode mode) { |
| 2372 | // Check local property, ignore interceptor. |
| 2373 | LookupResult result; |
| 2374 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2375 | if (!result.IsProperty()) return Heap::true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | |
| 2377 | // Normalize object if needed. |
| 2378 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2379 | if (obj->IsFailure()) return obj; |
| 2380 | |
| 2381 | return DeleteNormalizedProperty(name, mode); |
| 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | Object* JSObject::DeletePropertyWithInterceptor(String* name) { |
| 2386 | HandleScope scope; |
| 2387 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 2388 | Handle<String> name_handle(name); |
| 2389 | Handle<JSObject> this_handle(this); |
| 2390 | if (!interceptor->deleter()->IsUndefined()) { |
| 2391 | v8::NamedPropertyDeleter deleter = |
| 2392 | v8::ToCData<v8::NamedPropertyDeleter>(interceptor->deleter()); |
| 2393 | LOG(ApiNamedPropertyAccess("interceptor-named-delete", *this_handle, name)); |
| 2394 | CustomArguments args(interceptor->data(), this, this); |
| 2395 | v8::AccessorInfo info(args.end()); |
| 2396 | v8::Handle<v8::Boolean> result; |
| 2397 | { |
| 2398 | // Leaving JavaScript. |
| 2399 | VMState state(EXTERNAL); |
| 2400 | result = deleter(v8::Utils::ToLocal(name_handle), info); |
| 2401 | } |
| 2402 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 2403 | if (!result.IsEmpty()) { |
| 2404 | ASSERT(result->IsBoolean()); |
| 2405 | return *v8::Utils::OpenHandle(*result); |
| 2406 | } |
| 2407 | } |
| 2408 | Object* raw_result = |
| 2409 | this_handle->DeletePropertyPostInterceptor(*name_handle, NORMAL_DELETION); |
| 2410 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 2411 | return raw_result; |
| 2412 | } |
| 2413 | |
| 2414 | |
| 2415 | Object* JSObject::DeleteElementPostInterceptor(uint32_t index, |
| 2416 | DeleteMode mode) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2417 | ASSERT(!HasPixelElements() && !HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2418 | switch (GetElementsKind()) { |
| 2419 | case FAST_ELEMENTS: { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2420 | Object* obj = EnsureWritableFastElements(); |
| 2421 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2422 | uint32_t length = IsJSArray() ? |
| 2423 | static_cast<uint32_t>(Smi::cast(JSArray::cast(this)->length())->value()) : |
| 2424 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 2425 | if (index < length) { |
| 2426 | FixedArray::cast(elements())->set_the_hole(index); |
| 2427 | } |
| 2428 | break; |
| 2429 | } |
| 2430 | case DICTIONARY_ELEMENTS: { |
| 2431 | NumberDictionary* dictionary = element_dictionary(); |
| 2432 | int entry = dictionary->FindEntry(index); |
| 2433 | if (entry != NumberDictionary::kNotFound) { |
| 2434 | return dictionary->DeleteProperty(entry, mode); |
| 2435 | } |
| 2436 | break; |
| 2437 | } |
| 2438 | default: |
| 2439 | UNREACHABLE(); |
| 2440 | break; |
| 2441 | } |
| 2442 | return Heap::true_value(); |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | Object* JSObject::DeleteElementWithInterceptor(uint32_t index) { |
| 2447 | // Make sure that the top context does not change when doing |
| 2448 | // callbacks or interceptor calls. |
| 2449 | AssertNoContextChange ncc; |
| 2450 | HandleScope scope; |
| 2451 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 2452 | if (interceptor->deleter()->IsUndefined()) return Heap::false_value(); |
| 2453 | v8::IndexedPropertyDeleter deleter = |
| 2454 | v8::ToCData<v8::IndexedPropertyDeleter>(interceptor->deleter()); |
| 2455 | Handle<JSObject> this_handle(this); |
| 2456 | LOG(ApiIndexedPropertyAccess("interceptor-indexed-delete", this, index)); |
| 2457 | CustomArguments args(interceptor->data(), this, this); |
| 2458 | v8::AccessorInfo info(args.end()); |
| 2459 | v8::Handle<v8::Boolean> result; |
| 2460 | { |
| 2461 | // Leaving JavaScript. |
| 2462 | VMState state(EXTERNAL); |
| 2463 | result = deleter(index, info); |
| 2464 | } |
| 2465 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 2466 | if (!result.IsEmpty()) { |
| 2467 | ASSERT(result->IsBoolean()); |
| 2468 | return *v8::Utils::OpenHandle(*result); |
| 2469 | } |
| 2470 | Object* raw_result = |
| 2471 | this_handle->DeleteElementPostInterceptor(index, NORMAL_DELETION); |
| 2472 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 2473 | return raw_result; |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | Object* JSObject::DeleteElement(uint32_t index, DeleteMode mode) { |
| 2478 | // Check access rights if needed. |
| 2479 | if (IsAccessCheckNeeded() && |
| 2480 | !Top::MayIndexedAccess(this, index, v8::ACCESS_DELETE)) { |
| 2481 | Top::ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 2482 | return Heap::false_value(); |
| 2483 | } |
| 2484 | |
| 2485 | if (IsJSGlobalProxy()) { |
| 2486 | Object* proto = GetPrototype(); |
| 2487 | if (proto->IsNull()) return Heap::false_value(); |
| 2488 | ASSERT(proto->IsJSGlobalObject()); |
| 2489 | return JSGlobalObject::cast(proto)->DeleteElement(index, mode); |
| 2490 | } |
| 2491 | |
| 2492 | if (HasIndexedInterceptor()) { |
| 2493 | // Skip interceptor if forcing deletion. |
| 2494 | if (mode == FORCE_DELETION) { |
| 2495 | return DeleteElementPostInterceptor(index, mode); |
| 2496 | } |
| 2497 | return DeleteElementWithInterceptor(index); |
| 2498 | } |
| 2499 | |
| 2500 | switch (GetElementsKind()) { |
| 2501 | case FAST_ELEMENTS: { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2502 | Object* obj = EnsureWritableFastElements(); |
| 2503 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2504 | uint32_t length = IsJSArray() ? |
| 2505 | static_cast<uint32_t>(Smi::cast(JSArray::cast(this)->length())->value()) : |
| 2506 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 2507 | if (index < length) { |
| 2508 | FixedArray::cast(elements())->set_the_hole(index); |
| 2509 | } |
| 2510 | break; |
| 2511 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2512 | case PIXEL_ELEMENTS: |
| 2513 | case EXTERNAL_BYTE_ELEMENTS: |
| 2514 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 2515 | case EXTERNAL_SHORT_ELEMENTS: |
| 2516 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 2517 | case EXTERNAL_INT_ELEMENTS: |
| 2518 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 2519 | case EXTERNAL_FLOAT_ELEMENTS: |
| 2520 | // Pixel and external array elements cannot be deleted. Just |
| 2521 | // silently ignore here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2522 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2523 | case DICTIONARY_ELEMENTS: { |
| 2524 | NumberDictionary* dictionary = element_dictionary(); |
| 2525 | int entry = dictionary->FindEntry(index); |
| 2526 | if (entry != NumberDictionary::kNotFound) { |
| 2527 | return dictionary->DeleteProperty(entry, mode); |
| 2528 | } |
| 2529 | break; |
| 2530 | } |
| 2531 | default: |
| 2532 | UNREACHABLE(); |
| 2533 | break; |
| 2534 | } |
| 2535 | return Heap::true_value(); |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | Object* JSObject::DeleteProperty(String* name, DeleteMode mode) { |
| 2540 | // ECMA-262, 3rd, 8.6.2.5 |
| 2541 | ASSERT(name->IsString()); |
| 2542 | |
| 2543 | // Check access rights if needed. |
| 2544 | if (IsAccessCheckNeeded() && |
| 2545 | !Top::MayNamedAccess(this, name, v8::ACCESS_DELETE)) { |
| 2546 | Top::ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 2547 | return Heap::false_value(); |
| 2548 | } |
| 2549 | |
| 2550 | if (IsJSGlobalProxy()) { |
| 2551 | Object* proto = GetPrototype(); |
| 2552 | if (proto->IsNull()) return Heap::false_value(); |
| 2553 | ASSERT(proto->IsJSGlobalObject()); |
| 2554 | return JSGlobalObject::cast(proto)->DeleteProperty(name, mode); |
| 2555 | } |
| 2556 | |
| 2557 | uint32_t index = 0; |
| 2558 | if (name->AsArrayIndex(&index)) { |
| 2559 | return DeleteElement(index, mode); |
| 2560 | } else { |
| 2561 | LookupResult result; |
| 2562 | LocalLookup(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2563 | if (!result.IsProperty()) return Heap::true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2564 | // Ignore attributes if forcing a deletion. |
| 2565 | if (result.IsDontDelete() && mode != FORCE_DELETION) { |
| 2566 | return Heap::false_value(); |
| 2567 | } |
| 2568 | // Check for interceptor. |
| 2569 | if (result.type() == INTERCEPTOR) { |
| 2570 | // Skip interceptor if forcing a deletion. |
| 2571 | if (mode == FORCE_DELETION) { |
| 2572 | return DeletePropertyPostInterceptor(name, mode); |
| 2573 | } |
| 2574 | return DeletePropertyWithInterceptor(name); |
| 2575 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2576 | // Normalize object if needed. |
| 2577 | Object* obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2578 | if (obj->IsFailure()) return obj; |
| 2579 | // Make sure the properties are normalized before removing the entry. |
| 2580 | return DeleteNormalizedProperty(name, mode); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | |
| 2585 | // Check whether this object references another object. |
| 2586 | bool JSObject::ReferencesObject(Object* obj) { |
| 2587 | AssertNoAllocation no_alloc; |
| 2588 | |
| 2589 | // Is the object the constructor for this object? |
| 2590 | if (map()->constructor() == obj) { |
| 2591 | return true; |
| 2592 | } |
| 2593 | |
| 2594 | // Is the object the prototype for this object? |
| 2595 | if (map()->prototype() == obj) { |
| 2596 | return true; |
| 2597 | } |
| 2598 | |
| 2599 | // Check if the object is among the named properties. |
| 2600 | Object* key = SlowReverseLookup(obj); |
| 2601 | if (key != Heap::undefined_value()) { |
| 2602 | return true; |
| 2603 | } |
| 2604 | |
| 2605 | // Check if the object is among the indexed properties. |
| 2606 | switch (GetElementsKind()) { |
| 2607 | case PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2608 | case EXTERNAL_BYTE_ELEMENTS: |
| 2609 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 2610 | case EXTERNAL_SHORT_ELEMENTS: |
| 2611 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 2612 | case EXTERNAL_INT_ELEMENTS: |
| 2613 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 2614 | case EXTERNAL_FLOAT_ELEMENTS: |
| 2615 | // Raw pixels and external arrays do not reference other |
| 2616 | // objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | case FAST_ELEMENTS: { |
| 2619 | int length = IsJSArray() ? |
| 2620 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 2621 | FixedArray::cast(elements())->length(); |
| 2622 | for (int i = 0; i < length; i++) { |
| 2623 | Object* element = FixedArray::cast(elements())->get(i); |
| 2624 | if (!element->IsTheHole() && element == obj) { |
| 2625 | return true; |
| 2626 | } |
| 2627 | } |
| 2628 | break; |
| 2629 | } |
| 2630 | case DICTIONARY_ELEMENTS: { |
| 2631 | key = element_dictionary()->SlowReverseLookup(obj); |
| 2632 | if (key != Heap::undefined_value()) { |
| 2633 | return true; |
| 2634 | } |
| 2635 | break; |
| 2636 | } |
| 2637 | default: |
| 2638 | UNREACHABLE(); |
| 2639 | break; |
| 2640 | } |
| 2641 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2642 | // For functions check the context. |
| 2643 | if (IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2644 | // Get the constructor function for arguments array. |
| 2645 | JSObject* arguments_boilerplate = |
| 2646 | Top::context()->global_context()->arguments_boilerplate(); |
| 2647 | JSFunction* arguments_function = |
| 2648 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 2649 | |
| 2650 | // Get the context and don't check if it is the global context. |
| 2651 | JSFunction* f = JSFunction::cast(this); |
| 2652 | Context* context = f->context(); |
| 2653 | if (context->IsGlobalContext()) { |
| 2654 | return false; |
| 2655 | } |
| 2656 | |
| 2657 | // Check the non-special context slots. |
| 2658 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 2659 | // Only check JS objects. |
| 2660 | if (context->get(i)->IsJSObject()) { |
| 2661 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 2662 | // If it is an arguments array check the content. |
| 2663 | if (ctxobj->map()->constructor() == arguments_function) { |
| 2664 | if (ctxobj->ReferencesObject(obj)) { |
| 2665 | return true; |
| 2666 | } |
| 2667 | } else if (ctxobj == obj) { |
| 2668 | return true; |
| 2669 | } |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | // Check the context extension if any. |
| 2674 | if (context->has_extension()) { |
| 2675 | return context->extension()->ReferencesObject(obj); |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | // No references to object. |
| 2680 | return false; |
| 2681 | } |
| 2682 | |
| 2683 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2684 | Object* JSObject::PreventExtensions() { |
| 2685 | // If there are fast elements we normalize. |
| 2686 | if (HasFastElements()) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 2687 | Object* ok = NormalizeElements(); |
| 2688 | if (ok->IsFailure()) return ok; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2689 | } |
| 2690 | // Make sure that we never go back to fast case. |
| 2691 | element_dictionary()->set_requires_slow_elements(); |
| 2692 | |
| 2693 | // Do a map transition, other objects with this map may still |
| 2694 | // be extensible. |
| 2695 | Object* new_map = map()->CopyDropTransitions(); |
| 2696 | if (new_map->IsFailure()) return new_map; |
| 2697 | Map::cast(new_map)->set_is_extensible(false); |
| 2698 | set_map(Map::cast(new_map)); |
| 2699 | ASSERT(!map()->is_extensible()); |
| 2700 | return new_map; |
| 2701 | } |
| 2702 | |
| 2703 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2704 | // Tests for the fast common case for property enumeration: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2705 | // - This object and all prototypes has an enum cache (which means that it has |
| 2706 | // no interceptors and needs no access checks). |
| 2707 | // - This object has no elements. |
| 2708 | // - No prototype has enumerable properties/elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2709 | bool JSObject::IsSimpleEnum() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2710 | for (Object* o = this; |
| 2711 | o != Heap::null_value(); |
| 2712 | o = JSObject::cast(o)->GetPrototype()) { |
| 2713 | JSObject* curr = JSObject::cast(o); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2714 | if (!curr->map()->instance_descriptors()->HasEnumCache()) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2715 | ASSERT(!curr->HasNamedInterceptor()); |
| 2716 | ASSERT(!curr->HasIndexedInterceptor()); |
| 2717 | ASSERT(!curr->IsAccessCheckNeeded()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2718 | if (curr->NumberOfEnumElements() > 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2719 | if (curr != this) { |
| 2720 | FixedArray* curr_fixed_array = |
| 2721 | FixedArray::cast(curr->map()->instance_descriptors()->GetEnumCache()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2722 | if (curr_fixed_array->length() > 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | return true; |
| 2726 | } |
| 2727 | |
| 2728 | |
| 2729 | int Map::NumberOfDescribedProperties() { |
| 2730 | int result = 0; |
| 2731 | DescriptorArray* descs = instance_descriptors(); |
| 2732 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2733 | if (descs->IsProperty(i)) result++; |
| 2734 | } |
| 2735 | return result; |
| 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | int Map::PropertyIndexFor(String* name) { |
| 2740 | DescriptorArray* descs = instance_descriptors(); |
| 2741 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2742 | if (name->Equals(descs->GetKey(i)) && !descs->IsNullDescriptor(i)) { |
| 2743 | return descs->GetFieldIndex(i); |
| 2744 | } |
| 2745 | } |
| 2746 | return -1; |
| 2747 | } |
| 2748 | |
| 2749 | |
| 2750 | int Map::NextFreePropertyIndex() { |
| 2751 | int max_index = -1; |
| 2752 | DescriptorArray* descs = instance_descriptors(); |
| 2753 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2754 | if (descs->GetType(i) == FIELD) { |
| 2755 | int current_index = descs->GetFieldIndex(i); |
| 2756 | if (current_index > max_index) max_index = current_index; |
| 2757 | } |
| 2758 | } |
| 2759 | return max_index + 1; |
| 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | AccessorDescriptor* Map::FindAccessor(String* name) { |
| 2764 | DescriptorArray* descs = instance_descriptors(); |
| 2765 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2766 | if (name->Equals(descs->GetKey(i)) && descs->GetType(i) == CALLBACKS) { |
| 2767 | return descs->GetCallbacks(i); |
| 2768 | } |
| 2769 | } |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | |
| 2773 | |
| 2774 | void JSObject::LocalLookup(String* name, LookupResult* result) { |
| 2775 | ASSERT(name->IsString()); |
| 2776 | |
| 2777 | if (IsJSGlobalProxy()) { |
| 2778 | Object* proto = GetPrototype(); |
| 2779 | if (proto->IsNull()) return result->NotFound(); |
| 2780 | ASSERT(proto->IsJSGlobalObject()); |
| 2781 | return JSObject::cast(proto)->LocalLookup(name, result); |
| 2782 | } |
| 2783 | |
| 2784 | // Do not use inline caching if the object is a non-global object |
| 2785 | // that requires access checks. |
| 2786 | if (!IsJSGlobalProxy() && IsAccessCheckNeeded()) { |
| 2787 | result->DisallowCaching(); |
| 2788 | } |
| 2789 | |
| 2790 | // Check __proto__ before interceptor. |
| 2791 | if (name->Equals(Heap::Proto_symbol()) && !IsJSContextExtensionObject()) { |
| 2792 | result->ConstantResult(this); |
| 2793 | return; |
| 2794 | } |
| 2795 | |
| 2796 | // Check for lookup interceptor except when bootstrapping. |
| 2797 | if (HasNamedInterceptor() && !Bootstrapper::IsActive()) { |
| 2798 | result->InterceptorResult(this); |
| 2799 | return; |
| 2800 | } |
| 2801 | |
| 2802 | LocalLookupRealNamedProperty(name, result); |
| 2803 | } |
| 2804 | |
| 2805 | |
| 2806 | void JSObject::Lookup(String* name, LookupResult* result) { |
| 2807 | // Ecma-262 3rd 8.6.2.4 |
| 2808 | for (Object* current = this; |
| 2809 | current != Heap::null_value(); |
| 2810 | current = JSObject::cast(current)->GetPrototype()) { |
| 2811 | JSObject::cast(current)->LocalLookup(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2812 | if (result->IsProperty()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2813 | } |
| 2814 | result->NotFound(); |
| 2815 | } |
| 2816 | |
| 2817 | |
| 2818 | // Search object and it's prototype chain for callback properties. |
| 2819 | void JSObject::LookupCallback(String* name, LookupResult* result) { |
| 2820 | for (Object* current = this; |
| 2821 | current != Heap::null_value(); |
| 2822 | current = JSObject::cast(current)->GetPrototype()) { |
| 2823 | JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2824 | if (result->IsProperty() && result->type() == CALLBACKS) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2825 | } |
| 2826 | result->NotFound(); |
| 2827 | } |
| 2828 | |
| 2829 | |
| 2830 | Object* JSObject::DefineGetterSetter(String* name, |
| 2831 | PropertyAttributes attributes) { |
| 2832 | // Make sure that the top context does not change when doing callbacks or |
| 2833 | // interceptor calls. |
| 2834 | AssertNoContextChange ncc; |
| 2835 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2836 | // Try to flatten before operating on the string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2837 | name->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2838 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2839 | if (!CanSetCallback(name)) { |
| 2840 | return Heap::undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2843 | uint32_t index = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2844 | bool is_element = name->AsArrayIndex(&index); |
| 2845 | if (is_element && IsJSArray()) return Heap::undefined_value(); |
| 2846 | |
| 2847 | if (is_element) { |
| 2848 | switch (GetElementsKind()) { |
| 2849 | case FAST_ELEMENTS: |
| 2850 | break; |
| 2851 | case PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2852 | case EXTERNAL_BYTE_ELEMENTS: |
| 2853 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 2854 | case EXTERNAL_SHORT_ELEMENTS: |
| 2855 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 2856 | case EXTERNAL_INT_ELEMENTS: |
| 2857 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 2858 | case EXTERNAL_FLOAT_ELEMENTS: |
| 2859 | // Ignore getters and setters on pixel and external array |
| 2860 | // elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2861 | return Heap::undefined_value(); |
| 2862 | case DICTIONARY_ELEMENTS: { |
| 2863 | // Lookup the index. |
| 2864 | NumberDictionary* dictionary = element_dictionary(); |
| 2865 | int entry = dictionary->FindEntry(index); |
| 2866 | if (entry != NumberDictionary::kNotFound) { |
| 2867 | Object* result = dictionary->ValueAt(entry); |
| 2868 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 2869 | if (details.IsReadOnly()) return Heap::undefined_value(); |
| 2870 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2871 | if (result->IsFixedArray()) { |
| 2872 | return result; |
| 2873 | } |
| 2874 | // Otherwise allow to override it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2875 | } |
| 2876 | } |
| 2877 | break; |
| 2878 | } |
| 2879 | default: |
| 2880 | UNREACHABLE(); |
| 2881 | break; |
| 2882 | } |
| 2883 | } else { |
| 2884 | // Lookup the name. |
| 2885 | LookupResult result; |
| 2886 | LocalLookup(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2887 | if (result.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2888 | if (result.IsReadOnly()) return Heap::undefined_value(); |
| 2889 | if (result.type() == CALLBACKS) { |
| 2890 | Object* obj = result.GetCallbackObject(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2891 | // Need to preserve old getters/setters. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2892 | if (obj->IsFixedArray()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2893 | // Use set to update attributes. |
| 2894 | return SetPropertyCallback(name, obj, attributes); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 2895 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | // Allocate the fixed array to hold getter and setter. |
| 2901 | Object* structure = Heap::AllocateFixedArray(2, TENURED); |
| 2902 | if (structure->IsFailure()) return structure; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2903 | |
| 2904 | if (is_element) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2905 | return SetElementCallback(index, structure, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2906 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2907 | return SetPropertyCallback(name, structure, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2908 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | |
| 2912 | bool JSObject::CanSetCallback(String* name) { |
| 2913 | ASSERT(!IsAccessCheckNeeded() |
| 2914 | || Top::MayNamedAccess(this, name, v8::ACCESS_SET)); |
| 2915 | |
| 2916 | // Check if there is an API defined callback object which prohibits |
| 2917 | // callback overwriting in this object or it's prototype chain. |
| 2918 | // This mechanism is needed for instance in a browser setting, where |
| 2919 | // certain accessors such as window.location should not be allowed |
| 2920 | // to be overwritten because allowing overwriting could potentially |
| 2921 | // cause security problems. |
| 2922 | LookupResult callback_result; |
| 2923 | LookupCallback(name, &callback_result); |
| 2924 | if (callback_result.IsProperty()) { |
| 2925 | Object* obj = callback_result.GetCallbackObject(); |
| 2926 | if (obj->IsAccessorInfo() && |
| 2927 | AccessorInfo::cast(obj)->prohibits_overwriting()) { |
| 2928 | return false; |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | return true; |
| 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | Object* JSObject::SetElementCallback(uint32_t index, |
| 2937 | Object* structure, |
| 2938 | PropertyAttributes attributes) { |
| 2939 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS); |
| 2940 | |
| 2941 | // Normalize elements to make this operation simple. |
| 2942 | Object* ok = NormalizeElements(); |
| 2943 | if (ok->IsFailure()) return ok; |
| 2944 | |
| 2945 | // Update the dictionary with the new CALLBACKS property. |
| 2946 | Object* dict = |
| 2947 | element_dictionary()->Set(index, structure, details); |
| 2948 | if (dict->IsFailure()) return dict; |
| 2949 | |
| 2950 | NumberDictionary* elements = NumberDictionary::cast(dict); |
| 2951 | elements->set_requires_slow_elements(); |
| 2952 | // Set the potential new dictionary on the object. |
| 2953 | set_elements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2954 | |
| 2955 | return structure; |
| 2956 | } |
| 2957 | |
| 2958 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2959 | Object* JSObject::SetPropertyCallback(String* name, |
| 2960 | Object* structure, |
| 2961 | PropertyAttributes attributes) { |
| 2962 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS); |
| 2963 | |
| 2964 | bool convert_back_to_fast = HasFastProperties() && |
| 2965 | (map()->instance_descriptors()->number_of_descriptors() |
| 2966 | < DescriptorArray::kMaxNumberOfDescriptors); |
| 2967 | |
| 2968 | // Normalize object to make this operation simple. |
| 2969 | Object* ok = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2970 | if (ok->IsFailure()) return ok; |
| 2971 | |
| 2972 | // For the global object allocate a new map to invalidate the global inline |
| 2973 | // caches which have a global property cell reference directly in the code. |
| 2974 | if (IsGlobalObject()) { |
| 2975 | Object* new_map = map()->CopyDropDescriptors(); |
| 2976 | if (new_map->IsFailure()) return new_map; |
| 2977 | set_map(Map::cast(new_map)); |
| 2978 | } |
| 2979 | |
| 2980 | // Update the dictionary with the new CALLBACKS property. |
| 2981 | Object* result = SetNormalizedProperty(name, structure, details); |
| 2982 | if (result->IsFailure()) return result; |
| 2983 | |
| 2984 | if (convert_back_to_fast) { |
| 2985 | ok = TransformToFastProperties(0); |
| 2986 | if (ok->IsFailure()) return ok; |
| 2987 | } |
| 2988 | return result; |
| 2989 | } |
| 2990 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2991 | Object* JSObject::DefineAccessor(String* name, bool is_getter, JSFunction* fun, |
| 2992 | PropertyAttributes attributes) { |
| 2993 | // Check access rights if needed. |
| 2994 | if (IsAccessCheckNeeded() && |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2995 | !Top::MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 2996 | Top::ReportFailedAccessCheck(this, v8::ACCESS_SET); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2997 | return Heap::undefined_value(); |
| 2998 | } |
| 2999 | |
| 3000 | if (IsJSGlobalProxy()) { |
| 3001 | Object* proto = GetPrototype(); |
| 3002 | if (proto->IsNull()) return this; |
| 3003 | ASSERT(proto->IsJSGlobalObject()); |
| 3004 | return JSObject::cast(proto)->DefineAccessor(name, is_getter, |
| 3005 | fun, attributes); |
| 3006 | } |
| 3007 | |
| 3008 | Object* array = DefineGetterSetter(name, attributes); |
| 3009 | if (array->IsFailure() || array->IsUndefined()) return array; |
| 3010 | FixedArray::cast(array)->set(is_getter ? 0 : 1, fun); |
| 3011 | return this; |
| 3012 | } |
| 3013 | |
| 3014 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3015 | Object* JSObject::DefineAccessor(AccessorInfo* info) { |
| 3016 | String* name = String::cast(info->name()); |
| 3017 | // Check access rights if needed. |
| 3018 | if (IsAccessCheckNeeded() && |
| 3019 | !Top::MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 3020 | Top::ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 3021 | return Heap::undefined_value(); |
| 3022 | } |
| 3023 | |
| 3024 | if (IsJSGlobalProxy()) { |
| 3025 | Object* proto = GetPrototype(); |
| 3026 | if (proto->IsNull()) return this; |
| 3027 | ASSERT(proto->IsJSGlobalObject()); |
| 3028 | return JSObject::cast(proto)->DefineAccessor(info); |
| 3029 | } |
| 3030 | |
| 3031 | // Make sure that the top context does not change when doing callbacks or |
| 3032 | // interceptor calls. |
| 3033 | AssertNoContextChange ncc; |
| 3034 | |
| 3035 | // Try to flatten before operating on the string. |
| 3036 | name->TryFlatten(); |
| 3037 | |
| 3038 | if (!CanSetCallback(name)) { |
| 3039 | return Heap::undefined_value(); |
| 3040 | } |
| 3041 | |
| 3042 | uint32_t index = 0; |
| 3043 | bool is_element = name->AsArrayIndex(&index); |
| 3044 | |
| 3045 | if (is_element) { |
| 3046 | if (IsJSArray()) return Heap::undefined_value(); |
| 3047 | |
| 3048 | // Accessors overwrite previous callbacks (cf. with getters/setters). |
| 3049 | switch (GetElementsKind()) { |
| 3050 | case FAST_ELEMENTS: |
| 3051 | break; |
| 3052 | case PIXEL_ELEMENTS: |
| 3053 | case EXTERNAL_BYTE_ELEMENTS: |
| 3054 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 3055 | case EXTERNAL_SHORT_ELEMENTS: |
| 3056 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 3057 | case EXTERNAL_INT_ELEMENTS: |
| 3058 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 3059 | case EXTERNAL_FLOAT_ELEMENTS: |
| 3060 | // Ignore getters and setters on pixel and external array |
| 3061 | // elements. |
| 3062 | return Heap::undefined_value(); |
| 3063 | case DICTIONARY_ELEMENTS: |
| 3064 | break; |
| 3065 | default: |
| 3066 | UNREACHABLE(); |
| 3067 | break; |
| 3068 | } |
| 3069 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 3070 | Object* ok = SetElementCallback(index, info, info->property_attributes()); |
| 3071 | if (ok->IsFailure()) return ok; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3072 | } else { |
| 3073 | // Lookup the name. |
| 3074 | LookupResult result; |
| 3075 | LocalLookup(name, &result); |
| 3076 | // ES5 forbids turning a property into an accessor if it's not |
| 3077 | // configurable (that is IsDontDelete in ES3 and v8), see 8.6.1 (Table 5). |
| 3078 | if (result.IsProperty() && (result.IsReadOnly() || result.IsDontDelete())) { |
| 3079 | return Heap::undefined_value(); |
| 3080 | } |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 3081 | Object* ok = SetPropertyCallback(name, info, info->property_attributes()); |
| 3082 | if (ok->IsFailure()) return ok; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | return this; |
| 3086 | } |
| 3087 | |
| 3088 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3089 | Object* JSObject::LookupAccessor(String* name, bool is_getter) { |
| 3090 | // Make sure that the top context does not change when doing callbacks or |
| 3091 | // interceptor calls. |
| 3092 | AssertNoContextChange ncc; |
| 3093 | |
| 3094 | // Check access rights if needed. |
| 3095 | if (IsAccessCheckNeeded() && |
| 3096 | !Top::MayNamedAccess(this, name, v8::ACCESS_HAS)) { |
| 3097 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 3098 | return Heap::undefined_value(); |
| 3099 | } |
| 3100 | |
| 3101 | // Make the lookup and include prototypes. |
| 3102 | int accessor_index = is_getter ? kGetterIndex : kSetterIndex; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3103 | uint32_t index = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3104 | if (name->AsArrayIndex(&index)) { |
| 3105 | for (Object* obj = this; |
| 3106 | obj != Heap::null_value(); |
| 3107 | obj = JSObject::cast(obj)->GetPrototype()) { |
| 3108 | JSObject* js_object = JSObject::cast(obj); |
| 3109 | if (js_object->HasDictionaryElements()) { |
| 3110 | NumberDictionary* dictionary = js_object->element_dictionary(); |
| 3111 | int entry = dictionary->FindEntry(index); |
| 3112 | if (entry != NumberDictionary::kNotFound) { |
| 3113 | Object* element = dictionary->ValueAt(entry); |
| 3114 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 3115 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3116 | if (element->IsFixedArray()) { |
| 3117 | return FixedArray::cast(element)->get(accessor_index); |
| 3118 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3119 | } |
| 3120 | } |
| 3121 | } |
| 3122 | } |
| 3123 | } else { |
| 3124 | for (Object* obj = this; |
| 3125 | obj != Heap::null_value(); |
| 3126 | obj = JSObject::cast(obj)->GetPrototype()) { |
| 3127 | LookupResult result; |
| 3128 | JSObject::cast(obj)->LocalLookup(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3129 | if (result.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3130 | if (result.IsReadOnly()) return Heap::undefined_value(); |
| 3131 | if (result.type() == CALLBACKS) { |
| 3132 | Object* obj = result.GetCallbackObject(); |
| 3133 | if (obj->IsFixedArray()) { |
| 3134 | return FixedArray::cast(obj)->get(accessor_index); |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | return Heap::undefined_value(); |
| 3141 | } |
| 3142 | |
| 3143 | |
| 3144 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 3145 | if (HasFastProperties()) { |
| 3146 | DescriptorArray* descs = map()->instance_descriptors(); |
| 3147 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3148 | if (descs->GetType(i) == FIELD) { |
| 3149 | if (FastPropertyAt(descs->GetFieldIndex(i)) == value) { |
| 3150 | return descs->GetKey(i); |
| 3151 | } |
| 3152 | } else if (descs->GetType(i) == CONSTANT_FUNCTION) { |
| 3153 | if (descs->GetConstantFunction(i) == value) { |
| 3154 | return descs->GetKey(i); |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | return Heap::undefined_value(); |
| 3159 | } else { |
| 3160 | return property_dictionary()->SlowReverseLookup(value); |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | |
| 3165 | Object* Map::CopyDropDescriptors() { |
| 3166 | Object* result = Heap::AllocateMap(instance_type(), instance_size()); |
| 3167 | if (result->IsFailure()) return result; |
| 3168 | Map::cast(result)->set_prototype(prototype()); |
| 3169 | Map::cast(result)->set_constructor(constructor()); |
| 3170 | // Don't copy descriptors, so map transitions always remain a forest. |
| 3171 | // If we retained the same descriptors we would have two maps |
| 3172 | // pointing to the same transition which is bad because the garbage |
| 3173 | // collector relies on being able to reverse pointers from transitions |
| 3174 | // to maps. If properties need to be retained use CopyDropTransitions. |
| 3175 | Map::cast(result)->set_instance_descriptors(Heap::empty_descriptor_array()); |
| 3176 | // Please note instance_type and instance_size are set when allocated. |
| 3177 | Map::cast(result)->set_inobject_properties(inobject_properties()); |
| 3178 | Map::cast(result)->set_unused_property_fields(unused_property_fields()); |
| 3179 | |
| 3180 | // If the map has pre-allocated properties always start out with a descriptor |
| 3181 | // array describing these properties. |
| 3182 | if (pre_allocated_property_fields() > 0) { |
| 3183 | ASSERT(constructor()->IsJSFunction()); |
| 3184 | JSFunction* ctor = JSFunction::cast(constructor()); |
| 3185 | Object* descriptors = |
| 3186 | ctor->initial_map()->instance_descriptors()->RemoveTransitions(); |
| 3187 | if (descriptors->IsFailure()) return descriptors; |
| 3188 | Map::cast(result)->set_instance_descriptors( |
| 3189 | DescriptorArray::cast(descriptors)); |
| 3190 | Map::cast(result)->set_pre_allocated_property_fields( |
| 3191 | pre_allocated_property_fields()); |
| 3192 | } |
| 3193 | Map::cast(result)->set_bit_field(bit_field()); |
| 3194 | Map::cast(result)->set_bit_field2(bit_field2()); |
| 3195 | Map::cast(result)->ClearCodeCache(); |
| 3196 | return result; |
| 3197 | } |
| 3198 | |
| 3199 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 3200 | Object* Map::CopyNormalized(PropertyNormalizationMode mode) { |
| 3201 | int new_instance_size = instance_size(); |
| 3202 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
| 3203 | new_instance_size -= inobject_properties() * kPointerSize; |
| 3204 | } |
| 3205 | |
| 3206 | Object* result = Heap::AllocateMap(instance_type(), new_instance_size); |
| 3207 | if (result->IsFailure()) return result; |
| 3208 | |
| 3209 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
| 3210 | Map::cast(result)->set_inobject_properties(inobject_properties()); |
| 3211 | } |
| 3212 | |
| 3213 | Map::cast(result)->set_prototype(prototype()); |
| 3214 | Map::cast(result)->set_constructor(constructor()); |
| 3215 | |
| 3216 | Map::cast(result)->set_bit_field(bit_field()); |
| 3217 | Map::cast(result)->set_bit_field2(bit_field2()); |
| 3218 | |
| 3219 | #ifdef DEBUG |
| 3220 | Map::cast(result)->NormalizedMapVerify(); |
| 3221 | #endif |
| 3222 | |
| 3223 | return result; |
| 3224 | } |
| 3225 | |
| 3226 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3227 | Object* Map::CopyDropTransitions() { |
| 3228 | Object* new_map = CopyDropDescriptors(); |
| 3229 | if (new_map->IsFailure()) return new_map; |
| 3230 | Object* descriptors = instance_descriptors()->RemoveTransitions(); |
| 3231 | if (descriptors->IsFailure()) return descriptors; |
| 3232 | cast(new_map)->set_instance_descriptors(DescriptorArray::cast(descriptors)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3233 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | |
| 3237 | Object* Map::UpdateCodeCache(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3238 | // Allocate the code cache if not present. |
| 3239 | if (code_cache()->IsFixedArray()) { |
| 3240 | Object* result = Heap::AllocateCodeCache(); |
| 3241 | if (result->IsFailure()) return result; |
| 3242 | set_code_cache(result); |
| 3243 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3244 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3245 | // Update the code cache. |
| 3246 | return CodeCache::cast(code_cache())->Update(name, code); |
| 3247 | } |
| 3248 | |
| 3249 | |
| 3250 | Object* Map::FindInCodeCache(String* name, Code::Flags flags) { |
| 3251 | // Do a lookup if a code cache exists. |
| 3252 | if (!code_cache()->IsFixedArray()) { |
| 3253 | return CodeCache::cast(code_cache())->Lookup(name, flags); |
| 3254 | } else { |
| 3255 | return Heap::undefined_value(); |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | |
| 3260 | int Map::IndexInCodeCache(Object* name, Code* code) { |
| 3261 | // Get the internal index if a code cache exists. |
| 3262 | if (!code_cache()->IsFixedArray()) { |
| 3263 | return CodeCache::cast(code_cache())->GetIndex(name, code); |
| 3264 | } |
| 3265 | return -1; |
| 3266 | } |
| 3267 | |
| 3268 | |
| 3269 | void Map::RemoveFromCodeCache(String* name, Code* code, int index) { |
| 3270 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 3271 | // RemoveFromCodeCache so the code cache must be there. |
| 3272 | ASSERT(!code_cache()->IsFixedArray()); |
| 3273 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 3274 | } |
| 3275 | |
| 3276 | |
| 3277 | Object* CodeCache::Update(String* name, Code* code) { |
| 3278 | ASSERT(code->ic_state() == MONOMORPHIC); |
| 3279 | |
| 3280 | // The number of monomorphic stubs for normal load/store/call IC's can grow to |
| 3281 | // a large number and therefore they need to go into a hash table. They are |
| 3282 | // used to load global properties from cells. |
| 3283 | if (code->type() == NORMAL) { |
| 3284 | // Make sure that a hash table is allocated for the normal load code cache. |
| 3285 | if (normal_type_cache()->IsUndefined()) { |
| 3286 | Object* result = |
| 3287 | CodeCacheHashTable::Allocate(CodeCacheHashTable::kInitialSize); |
| 3288 | if (result->IsFailure()) return result; |
| 3289 | set_normal_type_cache(result); |
| 3290 | } |
| 3291 | return UpdateNormalTypeCache(name, code); |
| 3292 | } else { |
| 3293 | ASSERT(default_cache()->IsFixedArray()); |
| 3294 | return UpdateDefaultCache(name, code); |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | |
| 3299 | Object* CodeCache::UpdateDefaultCache(String* name, Code* code) { |
| 3300 | // When updating the default code cache we disregard the type encoded in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3301 | // flags. This allows call constant stubs to overwrite call field |
| 3302 | // stubs, etc. |
| 3303 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 3304 | |
| 3305 | // First check whether we can update existing code cache without |
| 3306 | // extending it. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3307 | FixedArray* cache = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3308 | int length = cache->length(); |
| 3309 | int deleted_index = -1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3310 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3311 | Object* key = cache->get(i); |
| 3312 | if (key->IsNull()) { |
| 3313 | if (deleted_index < 0) deleted_index = i; |
| 3314 | continue; |
| 3315 | } |
| 3316 | if (key->IsUndefined()) { |
| 3317 | if (deleted_index >= 0) i = deleted_index; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3318 | cache->set(i + kCodeCacheEntryNameOffset, name); |
| 3319 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3320 | return this; |
| 3321 | } |
| 3322 | if (name->Equals(String::cast(key))) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3323 | Code::Flags found = |
| 3324 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3325 | if (Code::RemoveTypeFromFlags(found) == flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3326 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3327 | return this; |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | // Reached the end of the code cache. If there were deleted |
| 3333 | // elements, reuse the space for the first of them. |
| 3334 | if (deleted_index >= 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3335 | cache->set(deleted_index + kCodeCacheEntryNameOffset, name); |
| 3336 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3337 | return this; |
| 3338 | } |
| 3339 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3340 | // Extend the code cache with some new entries (at least one). Must be a |
| 3341 | // multiple of the entry size. |
| 3342 | int new_length = length + ((length >> 1)) + kCodeCacheEntrySize; |
| 3343 | new_length = new_length - new_length % kCodeCacheEntrySize; |
| 3344 | ASSERT((new_length % kCodeCacheEntrySize) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3345 | Object* result = cache->CopySize(new_length); |
| 3346 | if (result->IsFailure()) return result; |
| 3347 | |
| 3348 | // Add the (name, code) pair to the new cache. |
| 3349 | cache = FixedArray::cast(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3350 | cache->set(length + kCodeCacheEntryNameOffset, name); |
| 3351 | cache->set(length + kCodeCacheEntryCodeOffset, code); |
| 3352 | set_default_cache(cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3353 | return this; |
| 3354 | } |
| 3355 | |
| 3356 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3357 | Object* CodeCache::UpdateNormalTypeCache(String* name, Code* code) { |
| 3358 | // Adding a new entry can cause a new cache to be allocated. |
| 3359 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3360 | Object* new_cache = cache->Put(name, code); |
| 3361 | if (new_cache->IsFailure()) return new_cache; |
| 3362 | set_normal_type_cache(new_cache); |
| 3363 | return this; |
| 3364 | } |
| 3365 | |
| 3366 | |
| 3367 | Object* CodeCache::Lookup(String* name, Code::Flags flags) { |
| 3368 | if (Code::ExtractTypeFromFlags(flags) == NORMAL) { |
| 3369 | return LookupNormalTypeCache(name, flags); |
| 3370 | } else { |
| 3371 | return LookupDefaultCache(name, flags); |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | |
| 3376 | Object* CodeCache::LookupDefaultCache(String* name, Code::Flags flags) { |
| 3377 | FixedArray* cache = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3378 | int length = cache->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3379 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 3380 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3381 | // Skip deleted elements. |
| 3382 | if (key->IsNull()) continue; |
| 3383 | if (key->IsUndefined()) return key; |
| 3384 | if (name->Equals(String::cast(key))) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3385 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
| 3386 | if (code->flags() == flags) { |
| 3387 | return code; |
| 3388 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | return Heap::undefined_value(); |
| 3392 | } |
| 3393 | |
| 3394 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3395 | Object* CodeCache::LookupNormalTypeCache(String* name, Code::Flags flags) { |
| 3396 | if (!normal_type_cache()->IsUndefined()) { |
| 3397 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3398 | return cache->Lookup(name, flags); |
| 3399 | } else { |
| 3400 | return Heap::undefined_value(); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | int CodeCache::GetIndex(Object* name, Code* code) { |
| 3406 | if (code->type() == NORMAL) { |
| 3407 | if (normal_type_cache()->IsUndefined()) return -1; |
| 3408 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3409 | return cache->GetIndex(String::cast(name), code->flags()); |
| 3410 | } |
| 3411 | |
| 3412 | FixedArray* array = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3413 | int len = array->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3414 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 3415 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3416 | } |
| 3417 | return -1; |
| 3418 | } |
| 3419 | |
| 3420 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3421 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
| 3422 | if (code->type() == NORMAL) { |
| 3423 | ASSERT(!normal_type_cache()->IsUndefined()); |
| 3424 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3425 | ASSERT(cache->GetIndex(String::cast(name), code->flags()) == index); |
| 3426 | cache->RemoveByIndex(index); |
| 3427 | } else { |
| 3428 | FixedArray* array = default_cache(); |
| 3429 | ASSERT(array->length() >= index && array->get(index)->IsCode()); |
| 3430 | // Use null instead of undefined for deleted elements to distinguish |
| 3431 | // deleted elements from unused elements. This distinction is used |
| 3432 | // when looking up in the cache and when updating the cache. |
| 3433 | ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
| 3434 | array->set_null(index - 1); // Name. |
| 3435 | array->set_null(index); // Code. |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | // The key in the code cache hash table consists of the property name and the |
| 3441 | // code object. The actual match is on the name and the code flags. If a key |
| 3442 | // is created using the flags and not a code object it can only be used for |
| 3443 | // lookup not to create a new entry. |
| 3444 | class CodeCacheHashTableKey : public HashTableKey { |
| 3445 | public: |
| 3446 | CodeCacheHashTableKey(String* name, Code::Flags flags) |
| 3447 | : name_(name), flags_(flags), code_(NULL) { } |
| 3448 | |
| 3449 | CodeCacheHashTableKey(String* name, Code* code) |
| 3450 | : name_(name), |
| 3451 | flags_(code->flags()), |
| 3452 | code_(code) { } |
| 3453 | |
| 3454 | |
| 3455 | bool IsMatch(Object* other) { |
| 3456 | if (!other->IsFixedArray()) return false; |
| 3457 | FixedArray* pair = FixedArray::cast(other); |
| 3458 | String* name = String::cast(pair->get(0)); |
| 3459 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 3460 | if (flags != flags_) { |
| 3461 | return false; |
| 3462 | } |
| 3463 | return name_->Equals(name); |
| 3464 | } |
| 3465 | |
| 3466 | static uint32_t NameFlagsHashHelper(String* name, Code::Flags flags) { |
| 3467 | return name->Hash() ^ flags; |
| 3468 | } |
| 3469 | |
| 3470 | uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); } |
| 3471 | |
| 3472 | uint32_t HashForObject(Object* obj) { |
| 3473 | FixedArray* pair = FixedArray::cast(obj); |
| 3474 | String* name = String::cast(pair->get(0)); |
| 3475 | Code* code = Code::cast(pair->get(1)); |
| 3476 | return NameFlagsHashHelper(name, code->flags()); |
| 3477 | } |
| 3478 | |
| 3479 | Object* AsObject() { |
| 3480 | ASSERT(code_ != NULL); |
| 3481 | Object* obj = Heap::AllocateFixedArray(2); |
| 3482 | if (obj->IsFailure()) return obj; |
| 3483 | FixedArray* pair = FixedArray::cast(obj); |
| 3484 | pair->set(0, name_); |
| 3485 | pair->set(1, code_); |
| 3486 | return pair; |
| 3487 | } |
| 3488 | |
| 3489 | private: |
| 3490 | String* name_; |
| 3491 | Code::Flags flags_; |
| 3492 | Code* code_; |
| 3493 | }; |
| 3494 | |
| 3495 | |
| 3496 | Object* CodeCacheHashTable::Lookup(String* name, Code::Flags flags) { |
| 3497 | CodeCacheHashTableKey key(name, flags); |
| 3498 | int entry = FindEntry(&key); |
| 3499 | if (entry == kNotFound) return Heap::undefined_value(); |
| 3500 | return get(EntryToIndex(entry) + 1); |
| 3501 | } |
| 3502 | |
| 3503 | |
| 3504 | Object* CodeCacheHashTable::Put(String* name, Code* code) { |
| 3505 | CodeCacheHashTableKey key(name, code); |
| 3506 | Object* obj = EnsureCapacity(1, &key); |
| 3507 | if (obj->IsFailure()) return obj; |
| 3508 | |
| 3509 | // Don't use this, as the table might have grown. |
| 3510 | CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj); |
| 3511 | |
| 3512 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 3513 | Object* k = key.AsObject(); |
| 3514 | if (k->IsFailure()) return k; |
| 3515 | |
| 3516 | cache->set(EntryToIndex(entry), k); |
| 3517 | cache->set(EntryToIndex(entry) + 1, code); |
| 3518 | cache->ElementAdded(); |
| 3519 | return cache; |
| 3520 | } |
| 3521 | |
| 3522 | |
| 3523 | int CodeCacheHashTable::GetIndex(String* name, Code::Flags flags) { |
| 3524 | CodeCacheHashTableKey key(name, flags); |
| 3525 | int entry = FindEntry(&key); |
| 3526 | return (entry == kNotFound) ? -1 : entry; |
| 3527 | } |
| 3528 | |
| 3529 | |
| 3530 | void CodeCacheHashTable::RemoveByIndex(int index) { |
| 3531 | ASSERT(index >= 0); |
| 3532 | set(EntryToIndex(index), Heap::null_value()); |
| 3533 | set(EntryToIndex(index) + 1, Heap::null_value()); |
| 3534 | ElementRemoved(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3538 | static bool HasKey(FixedArray* array, Object* key) { |
| 3539 | int len0 = array->length(); |
| 3540 | for (int i = 0; i < len0; i++) { |
| 3541 | Object* element = array->get(i); |
| 3542 | if (element->IsSmi() && key->IsSmi() && (element == key)) return true; |
| 3543 | if (element->IsString() && |
| 3544 | key->IsString() && String::cast(element)->Equals(String::cast(key))) { |
| 3545 | return true; |
| 3546 | } |
| 3547 | } |
| 3548 | return false; |
| 3549 | } |
| 3550 | |
| 3551 | |
| 3552 | Object* FixedArray::AddKeysFromJSArray(JSArray* array) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3553 | ASSERT(!array->HasPixelElements() && !array->HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3554 | switch (array->GetElementsKind()) { |
| 3555 | case JSObject::FAST_ELEMENTS: |
| 3556 | return UnionOfKeys(FixedArray::cast(array->elements())); |
| 3557 | case JSObject::DICTIONARY_ELEMENTS: { |
| 3558 | NumberDictionary* dict = array->element_dictionary(); |
| 3559 | int size = dict->NumberOfElements(); |
| 3560 | |
| 3561 | // Allocate a temporary fixed array. |
| 3562 | Object* object = Heap::AllocateFixedArray(size); |
| 3563 | if (object->IsFailure()) return object; |
| 3564 | FixedArray* key_array = FixedArray::cast(object); |
| 3565 | |
| 3566 | int capacity = dict->Capacity(); |
| 3567 | int pos = 0; |
| 3568 | // Copy the elements from the JSArray to the temporary fixed array. |
| 3569 | for (int i = 0; i < capacity; i++) { |
| 3570 | if (dict->IsKey(dict->KeyAt(i))) { |
| 3571 | key_array->set(pos++, dict->ValueAt(i)); |
| 3572 | } |
| 3573 | } |
| 3574 | // Compute the union of this and the temporary fixed array. |
| 3575 | return UnionOfKeys(key_array); |
| 3576 | } |
| 3577 | default: |
| 3578 | UNREACHABLE(); |
| 3579 | } |
| 3580 | UNREACHABLE(); |
| 3581 | return Heap::null_value(); // Failure case needs to "return" a value. |
| 3582 | } |
| 3583 | |
| 3584 | |
| 3585 | Object* FixedArray::UnionOfKeys(FixedArray* other) { |
| 3586 | int len0 = length(); |
| 3587 | int len1 = other->length(); |
| 3588 | // Optimize if either is empty. |
| 3589 | if (len0 == 0) return other; |
| 3590 | if (len1 == 0) return this; |
| 3591 | |
| 3592 | // Compute how many elements are not in this. |
| 3593 | int extra = 0; |
| 3594 | for (int y = 0; y < len1; y++) { |
| 3595 | Object* value = other->get(y); |
| 3596 | if (!value->IsTheHole() && !HasKey(this, value)) extra++; |
| 3597 | } |
| 3598 | |
| 3599 | if (extra == 0) return this; |
| 3600 | |
| 3601 | // Allocate the result |
| 3602 | Object* obj = Heap::AllocateFixedArray(len0 + extra); |
| 3603 | if (obj->IsFailure()) return obj; |
| 3604 | // Fill in the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3605 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3606 | FixedArray* result = FixedArray::cast(obj); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3607 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3608 | for (int i = 0; i < len0; i++) { |
| 3609 | result->set(i, get(i), mode); |
| 3610 | } |
| 3611 | // Fill in the extra keys. |
| 3612 | int index = 0; |
| 3613 | for (int y = 0; y < len1; y++) { |
| 3614 | Object* value = other->get(y); |
| 3615 | if (!value->IsTheHole() && !HasKey(this, value)) { |
| 3616 | result->set(len0 + index, other->get(y), mode); |
| 3617 | index++; |
| 3618 | } |
| 3619 | } |
| 3620 | ASSERT(extra == index); |
| 3621 | return result; |
| 3622 | } |
| 3623 | |
| 3624 | |
| 3625 | Object* FixedArray::CopySize(int new_length) { |
| 3626 | if (new_length == 0) return Heap::empty_fixed_array(); |
| 3627 | Object* obj = Heap::AllocateFixedArray(new_length); |
| 3628 | if (obj->IsFailure()) return obj; |
| 3629 | FixedArray* result = FixedArray::cast(obj); |
| 3630 | // Copy the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3631 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3632 | int len = length(); |
| 3633 | if (new_length < len) len = new_length; |
| 3634 | result->set_map(map()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3635 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3636 | for (int i = 0; i < len; i++) { |
| 3637 | result->set(i, get(i), mode); |
| 3638 | } |
| 3639 | return result; |
| 3640 | } |
| 3641 | |
| 3642 | |
| 3643 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3644 | AssertNoAllocation no_gc; |
| 3645 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3646 | for (int index = 0; index < len; index++) { |
| 3647 | dest->set(dest_pos+index, get(pos+index), mode); |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | |
| 3652 | #ifdef DEBUG |
| 3653 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 3654 | if (length() != other->length()) return false; |
| 3655 | for (int i = 0 ; i < length(); ++i) { |
| 3656 | if (get(i) != other->get(i)) return false; |
| 3657 | } |
| 3658 | return true; |
| 3659 | } |
| 3660 | #endif |
| 3661 | |
| 3662 | |
| 3663 | Object* DescriptorArray::Allocate(int number_of_descriptors) { |
| 3664 | if (number_of_descriptors == 0) { |
| 3665 | return Heap::empty_descriptor_array(); |
| 3666 | } |
| 3667 | // Allocate the array of keys. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3668 | Object* array = |
| 3669 | Heap::AllocateFixedArray(ToKeyIndex(number_of_descriptors)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3670 | if (array->IsFailure()) return array; |
| 3671 | // Do not use DescriptorArray::cast on incomplete object. |
| 3672 | FixedArray* result = FixedArray::cast(array); |
| 3673 | |
| 3674 | // Allocate the content array and set it in the descriptor array. |
| 3675 | array = Heap::AllocateFixedArray(number_of_descriptors << 1); |
| 3676 | if (array->IsFailure()) return array; |
| 3677 | result->set(kContentArrayIndex, array); |
| 3678 | result->set(kEnumerationIndexIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3679 | Smi::FromInt(PropertyDetails::kInitialIndex)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3680 | return result; |
| 3681 | } |
| 3682 | |
| 3683 | |
| 3684 | void DescriptorArray::SetEnumCache(FixedArray* bridge_storage, |
| 3685 | FixedArray* new_cache) { |
| 3686 | ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength); |
| 3687 | if (HasEnumCache()) { |
| 3688 | FixedArray::cast(get(kEnumerationIndexIndex))-> |
| 3689 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 3690 | } else { |
| 3691 | if (IsEmpty()) return; // Do nothing for empty descriptor array. |
| 3692 | FixedArray::cast(bridge_storage)-> |
| 3693 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 3694 | fast_set(FixedArray::cast(bridge_storage), |
| 3695 | kEnumCacheBridgeEnumIndex, |
| 3696 | get(kEnumerationIndexIndex)); |
| 3697 | set(kEnumerationIndexIndex, bridge_storage); |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | |
| 3702 | Object* DescriptorArray::CopyInsert(Descriptor* descriptor, |
| 3703 | TransitionFlag transition_flag) { |
| 3704 | // Transitions are only kept when inserting another transition. |
| 3705 | // This precondition is not required by this function's implementation, but |
| 3706 | // is currently required by the semantics of maps, so we check it. |
| 3707 | // Conversely, we filter after replacing, so replacing a transition and |
| 3708 | // removing all other transitions is not supported. |
| 3709 | bool remove_transitions = transition_flag == REMOVE_TRANSITIONS; |
| 3710 | ASSERT(remove_transitions == !descriptor->GetDetails().IsTransition()); |
| 3711 | ASSERT(descriptor->GetDetails().type() != NULL_DESCRIPTOR); |
| 3712 | |
| 3713 | // Ensure the key is a symbol. |
| 3714 | Object* result = descriptor->KeyToSymbol(); |
| 3715 | if (result->IsFailure()) return result; |
| 3716 | |
| 3717 | int transitions = 0; |
| 3718 | int null_descriptors = 0; |
| 3719 | if (remove_transitions) { |
| 3720 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 3721 | if (IsTransition(i)) transitions++; |
| 3722 | if (IsNullDescriptor(i)) null_descriptors++; |
| 3723 | } |
| 3724 | } else { |
| 3725 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 3726 | if (IsNullDescriptor(i)) null_descriptors++; |
| 3727 | } |
| 3728 | } |
| 3729 | int new_size = number_of_descriptors() - transitions - null_descriptors; |
| 3730 | |
| 3731 | // If key is in descriptor, we replace it in-place when filtering. |
| 3732 | // Count a null descriptor for key as inserted, not replaced. |
| 3733 | int index = Search(descriptor->GetKey()); |
| 3734 | const bool inserting = (index == kNotFound); |
| 3735 | const bool replacing = !inserting; |
| 3736 | bool keep_enumeration_index = false; |
| 3737 | if (inserting) { |
| 3738 | ++new_size; |
| 3739 | } |
| 3740 | if (replacing) { |
| 3741 | // We are replacing an existing descriptor. We keep the enumeration |
| 3742 | // index of a visible property. |
| 3743 | PropertyType t = PropertyDetails(GetDetails(index)).type(); |
| 3744 | if (t == CONSTANT_FUNCTION || |
| 3745 | t == FIELD || |
| 3746 | t == CALLBACKS || |
| 3747 | t == INTERCEPTOR) { |
| 3748 | keep_enumeration_index = true; |
| 3749 | } else if (remove_transitions) { |
| 3750 | // Replaced descriptor has been counted as removed if it is |
| 3751 | // a transition that will be replaced. Adjust count in this case. |
| 3752 | ++new_size; |
| 3753 | } |
| 3754 | } |
| 3755 | result = Allocate(new_size); |
| 3756 | if (result->IsFailure()) return result; |
| 3757 | DescriptorArray* new_descriptors = DescriptorArray::cast(result); |
| 3758 | // Set the enumeration index in the descriptors and set the enumeration index |
| 3759 | // in the result. |
| 3760 | int enumeration_index = NextEnumerationIndex(); |
| 3761 | if (!descriptor->GetDetails().IsTransition()) { |
| 3762 | if (keep_enumeration_index) { |
| 3763 | descriptor->SetEnumerationIndex( |
| 3764 | PropertyDetails(GetDetails(index)).index()); |
| 3765 | } else { |
| 3766 | descriptor->SetEnumerationIndex(enumeration_index); |
| 3767 | ++enumeration_index; |
| 3768 | } |
| 3769 | } |
| 3770 | new_descriptors->SetNextEnumerationIndex(enumeration_index); |
| 3771 | |
| 3772 | // Copy the descriptors, filtering out transitions and null descriptors, |
| 3773 | // and inserting or replacing a descriptor. |
| 3774 | uint32_t descriptor_hash = descriptor->GetKey()->Hash(); |
| 3775 | int from_index = 0; |
| 3776 | int to_index = 0; |
| 3777 | |
| 3778 | for (; from_index < number_of_descriptors(); from_index++) { |
| 3779 | String* key = GetKey(from_index); |
| 3780 | if (key->Hash() > descriptor_hash || key == descriptor->GetKey()) { |
| 3781 | break; |
| 3782 | } |
| 3783 | if (IsNullDescriptor(from_index)) continue; |
| 3784 | if (remove_transitions && IsTransition(from_index)) continue; |
| 3785 | new_descriptors->CopyFrom(to_index++, this, from_index); |
| 3786 | } |
| 3787 | |
| 3788 | new_descriptors->Set(to_index++, descriptor); |
| 3789 | if (replacing) from_index++; |
| 3790 | |
| 3791 | for (; from_index < number_of_descriptors(); from_index++) { |
| 3792 | if (IsNullDescriptor(from_index)) continue; |
| 3793 | if (remove_transitions && IsTransition(from_index)) continue; |
| 3794 | new_descriptors->CopyFrom(to_index++, this, from_index); |
| 3795 | } |
| 3796 | |
| 3797 | ASSERT(to_index == new_descriptors->number_of_descriptors()); |
| 3798 | SLOW_ASSERT(new_descriptors->IsSortedNoDuplicates()); |
| 3799 | |
| 3800 | return new_descriptors; |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | Object* DescriptorArray::RemoveTransitions() { |
| 3805 | // Remove all transitions and null descriptors. Return a copy of the array |
| 3806 | // with all transitions removed, or a Failure object if the new array could |
| 3807 | // not be allocated. |
| 3808 | |
| 3809 | // Compute the size of the map transition entries to be removed. |
| 3810 | int num_removed = 0; |
| 3811 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 3812 | if (!IsProperty(i)) num_removed++; |
| 3813 | } |
| 3814 | |
| 3815 | // Allocate the new descriptor array. |
| 3816 | Object* result = Allocate(number_of_descriptors() - num_removed); |
| 3817 | if (result->IsFailure()) return result; |
| 3818 | DescriptorArray* new_descriptors = DescriptorArray::cast(result); |
| 3819 | |
| 3820 | // Copy the content. |
| 3821 | int next_descriptor = 0; |
| 3822 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 3823 | if (IsProperty(i)) new_descriptors->CopyFrom(next_descriptor++, this, i); |
| 3824 | } |
| 3825 | ASSERT(next_descriptor == new_descriptors->number_of_descriptors()); |
| 3826 | |
| 3827 | return new_descriptors; |
| 3828 | } |
| 3829 | |
| 3830 | |
| 3831 | void DescriptorArray::Sort() { |
| 3832 | // In-place heap sort. |
| 3833 | int len = number_of_descriptors(); |
| 3834 | |
| 3835 | // Bottom-up max-heap construction. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3836 | // Index of the last node with children |
| 3837 | const int max_parent_index = (len / 2) - 1; |
| 3838 | for (int i = max_parent_index; i >= 0; --i) { |
| 3839 | int parent_index = i; |
| 3840 | const uint32_t parent_hash = GetKey(i)->Hash(); |
| 3841 | while (parent_index <= max_parent_index) { |
| 3842 | int child_index = 2 * parent_index + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3843 | uint32_t child_hash = GetKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3844 | if (child_index + 1 < len) { |
| 3845 | uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
| 3846 | if (right_child_hash > child_hash) { |
| 3847 | child_index++; |
| 3848 | child_hash = right_child_hash; |
| 3849 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3850 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3851 | if (child_hash <= parent_hash) break; |
| 3852 | Swap(parent_index, child_index); |
| 3853 | // Now element at child_index could be < its children. |
| 3854 | parent_index = child_index; // parent_hash remains correct. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | // Extract elements and create sorted array. |
| 3859 | for (int i = len - 1; i > 0; --i) { |
| 3860 | // Put max element at the back of the array. |
| 3861 | Swap(0, i); |
| 3862 | // Sift down the new top element. |
| 3863 | int parent_index = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3864 | const uint32_t parent_hash = GetKey(parent_index)->Hash(); |
| 3865 | const int max_parent_index = (i / 2) - 1; |
| 3866 | while (parent_index <= max_parent_index) { |
| 3867 | int child_index = parent_index * 2 + 1; |
| 3868 | uint32_t child_hash = GetKey(child_index)->Hash(); |
| 3869 | if (child_index + 1 < i) { |
| 3870 | uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
| 3871 | if (right_child_hash > child_hash) { |
| 3872 | child_index++; |
| 3873 | child_hash = right_child_hash; |
| 3874 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3875 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3876 | if (child_hash <= parent_hash) break; |
| 3877 | Swap(parent_index, child_index); |
| 3878 | parent_index = child_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | SLOW_ASSERT(IsSortedNoDuplicates()); |
| 3883 | } |
| 3884 | |
| 3885 | |
| 3886 | int DescriptorArray::BinarySearch(String* name, int low, int high) { |
| 3887 | uint32_t hash = name->Hash(); |
| 3888 | |
| 3889 | while (low <= high) { |
| 3890 | int mid = (low + high) / 2; |
| 3891 | String* mid_name = GetKey(mid); |
| 3892 | uint32_t mid_hash = mid_name->Hash(); |
| 3893 | |
| 3894 | if (mid_hash > hash) { |
| 3895 | high = mid - 1; |
| 3896 | continue; |
| 3897 | } |
| 3898 | if (mid_hash < hash) { |
| 3899 | low = mid + 1; |
| 3900 | continue; |
| 3901 | } |
| 3902 | // Found an element with the same hash-code. |
| 3903 | ASSERT(hash == mid_hash); |
| 3904 | // There might be more, so we find the first one and |
| 3905 | // check them all to see if we have a match. |
| 3906 | if (name == mid_name && !is_null_descriptor(mid)) return mid; |
| 3907 | while ((mid > low) && (GetKey(mid - 1)->Hash() == hash)) mid--; |
| 3908 | for (; (mid <= high) && (GetKey(mid)->Hash() == hash); mid++) { |
| 3909 | if (GetKey(mid)->Equals(name) && !is_null_descriptor(mid)) return mid; |
| 3910 | } |
| 3911 | break; |
| 3912 | } |
| 3913 | return kNotFound; |
| 3914 | } |
| 3915 | |
| 3916 | |
| 3917 | int DescriptorArray::LinearSearch(String* name, int len) { |
| 3918 | uint32_t hash = name->Hash(); |
| 3919 | for (int number = 0; number < len; number++) { |
| 3920 | String* entry = GetKey(number); |
| 3921 | if ((entry->Hash() == hash) && |
| 3922 | name->Equals(entry) && |
| 3923 | !is_null_descriptor(number)) { |
| 3924 | return number; |
| 3925 | } |
| 3926 | } |
| 3927 | return kNotFound; |
| 3928 | } |
| 3929 | |
| 3930 | |
| 3931 | #ifdef DEBUG |
| 3932 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 3933 | if (IsEmpty()) return other->IsEmpty(); |
| 3934 | if (other->IsEmpty()) return false; |
| 3935 | if (length() != other->length()) return false; |
| 3936 | for (int i = 0; i < length(); ++i) { |
| 3937 | if (get(i) != other->get(i) && i != kContentArrayIndex) return false; |
| 3938 | } |
| 3939 | return GetContentArray()->IsEqualTo(other->GetContentArray()); |
| 3940 | } |
| 3941 | #endif |
| 3942 | |
| 3943 | |
| 3944 | static StaticResource<StringInputBuffer> string_input_buffer; |
| 3945 | |
| 3946 | |
| 3947 | bool String::LooksValid() { |
| 3948 | if (!Heap::Contains(this)) return false; |
| 3949 | return true; |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | int String::Utf8Length() { |
| 3954 | if (IsAsciiRepresentation()) return length(); |
| 3955 | // Attempt to flatten before accessing the string. It probably |
| 3956 | // doesn't make Utf8Length faster, but it is very likely that |
| 3957 | // the string will be accessed later (for example by WriteUtf8) |
| 3958 | // so it's still a good idea. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3959 | TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3960 | Access<StringInputBuffer> buffer(&string_input_buffer); |
| 3961 | buffer->Reset(0, this); |
| 3962 | int result = 0; |
| 3963 | while (buffer->has_more()) |
| 3964 | result += unibrow::Utf8::Length(buffer->GetNext()); |
| 3965 | return result; |
| 3966 | } |
| 3967 | |
| 3968 | |
| 3969 | Vector<const char> String::ToAsciiVector() { |
| 3970 | ASSERT(IsAsciiRepresentation()); |
| 3971 | ASSERT(IsFlat()); |
| 3972 | |
| 3973 | int offset = 0; |
| 3974 | int length = this->length(); |
| 3975 | StringRepresentationTag string_tag = StringShape(this).representation_tag(); |
| 3976 | String* string = this; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3977 | if (string_tag == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3978 | ConsString* cons = ConsString::cast(string); |
| 3979 | ASSERT(cons->second()->length() == 0); |
| 3980 | string = cons->first(); |
| 3981 | string_tag = StringShape(string).representation_tag(); |
| 3982 | } |
| 3983 | if (string_tag == kSeqStringTag) { |
| 3984 | SeqAsciiString* seq = SeqAsciiString::cast(string); |
| 3985 | char* start = seq->GetChars(); |
| 3986 | return Vector<const char>(start + offset, length); |
| 3987 | } |
| 3988 | ASSERT(string_tag == kExternalStringTag); |
| 3989 | ExternalAsciiString* ext = ExternalAsciiString::cast(string); |
| 3990 | const char* start = ext->resource()->data(); |
| 3991 | return Vector<const char>(start + offset, length); |
| 3992 | } |
| 3993 | |
| 3994 | |
| 3995 | Vector<const uc16> String::ToUC16Vector() { |
| 3996 | ASSERT(IsTwoByteRepresentation()); |
| 3997 | ASSERT(IsFlat()); |
| 3998 | |
| 3999 | int offset = 0; |
| 4000 | int length = this->length(); |
| 4001 | StringRepresentationTag string_tag = StringShape(this).representation_tag(); |
| 4002 | String* string = this; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4003 | if (string_tag == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4004 | ConsString* cons = ConsString::cast(string); |
| 4005 | ASSERT(cons->second()->length() == 0); |
| 4006 | string = cons->first(); |
| 4007 | string_tag = StringShape(string).representation_tag(); |
| 4008 | } |
| 4009 | if (string_tag == kSeqStringTag) { |
| 4010 | SeqTwoByteString* seq = SeqTwoByteString::cast(string); |
| 4011 | return Vector<const uc16>(seq->GetChars() + offset, length); |
| 4012 | } |
| 4013 | ASSERT(string_tag == kExternalStringTag); |
| 4014 | ExternalTwoByteString* ext = ExternalTwoByteString::cast(string); |
| 4015 | const uc16* start = |
| 4016 | reinterpret_cast<const uc16*>(ext->resource()->data()); |
| 4017 | return Vector<const uc16>(start + offset, length); |
| 4018 | } |
| 4019 | |
| 4020 | |
| 4021 | SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 4022 | RobustnessFlag robust_flag, |
| 4023 | int offset, |
| 4024 | int length, |
| 4025 | int* length_return) { |
| 4026 | ASSERT(NativeAllocationChecker::allocation_allowed()); |
| 4027 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
| 4028 | return SmartPointer<char>(NULL); |
| 4029 | } |
| 4030 | |
| 4031 | // Negative length means the to the end of the string. |
| 4032 | if (length < 0) length = kMaxInt - offset; |
| 4033 | |
| 4034 | // Compute the size of the UTF-8 string. Start at the specified offset. |
| 4035 | Access<StringInputBuffer> buffer(&string_input_buffer); |
| 4036 | buffer->Reset(offset, this); |
| 4037 | int character_position = offset; |
| 4038 | int utf8_bytes = 0; |
| 4039 | while (buffer->has_more()) { |
| 4040 | uint16_t character = buffer->GetNext(); |
| 4041 | if (character_position < offset + length) { |
| 4042 | utf8_bytes += unibrow::Utf8::Length(character); |
| 4043 | } |
| 4044 | character_position++; |
| 4045 | } |
| 4046 | |
| 4047 | if (length_return) { |
| 4048 | *length_return = utf8_bytes; |
| 4049 | } |
| 4050 | |
| 4051 | char* result = NewArray<char>(utf8_bytes + 1); |
| 4052 | |
| 4053 | // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset. |
| 4054 | buffer->Rewind(); |
| 4055 | buffer->Seek(offset); |
| 4056 | character_position = offset; |
| 4057 | int utf8_byte_position = 0; |
| 4058 | while (buffer->has_more()) { |
| 4059 | uint16_t character = buffer->GetNext(); |
| 4060 | if (character_position < offset + length) { |
| 4061 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 4062 | character = ' '; |
| 4063 | } |
| 4064 | utf8_byte_position += |
| 4065 | unibrow::Utf8::Encode(result + utf8_byte_position, character); |
| 4066 | } |
| 4067 | character_position++; |
| 4068 | } |
| 4069 | result[utf8_byte_position] = 0; |
| 4070 | return SmartPointer<char>(result); |
| 4071 | } |
| 4072 | |
| 4073 | |
| 4074 | SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 4075 | RobustnessFlag robust_flag, |
| 4076 | int* length_return) { |
| 4077 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 4078 | } |
| 4079 | |
| 4080 | |
| 4081 | const uc16* String::GetTwoByteData() { |
| 4082 | return GetTwoByteData(0); |
| 4083 | } |
| 4084 | |
| 4085 | |
| 4086 | const uc16* String::GetTwoByteData(unsigned start) { |
| 4087 | ASSERT(!IsAsciiRepresentation()); |
| 4088 | switch (StringShape(this).representation_tag()) { |
| 4089 | case kSeqStringTag: |
| 4090 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
| 4091 | case kExternalStringTag: |
| 4092 | return ExternalTwoByteString::cast(this)-> |
| 4093 | ExternalTwoByteStringGetData(start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4094 | case kConsStringTag: |
| 4095 | UNREACHABLE(); |
| 4096 | return NULL; |
| 4097 | } |
| 4098 | UNREACHABLE(); |
| 4099 | return NULL; |
| 4100 | } |
| 4101 | |
| 4102 | |
| 4103 | SmartPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) { |
| 4104 | ASSERT(NativeAllocationChecker::allocation_allowed()); |
| 4105 | |
| 4106 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
| 4107 | return SmartPointer<uc16>(); |
| 4108 | } |
| 4109 | |
| 4110 | Access<StringInputBuffer> buffer(&string_input_buffer); |
| 4111 | buffer->Reset(this); |
| 4112 | |
| 4113 | uc16* result = NewArray<uc16>(length() + 1); |
| 4114 | |
| 4115 | int i = 0; |
| 4116 | while (buffer->has_more()) { |
| 4117 | uint16_t character = buffer->GetNext(); |
| 4118 | result[i++] = character; |
| 4119 | } |
| 4120 | result[i] = 0; |
| 4121 | return SmartPointer<uc16>(result); |
| 4122 | } |
| 4123 | |
| 4124 | |
| 4125 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
| 4126 | return reinterpret_cast<uc16*>( |
| 4127 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 4128 | } |
| 4129 | |
| 4130 | |
| 4131 | void SeqTwoByteString::SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 4132 | unsigned* offset_ptr, |
| 4133 | unsigned max_chars) { |
| 4134 | unsigned chars_read = 0; |
| 4135 | unsigned offset = *offset_ptr; |
| 4136 | while (chars_read < max_chars) { |
| 4137 | uint16_t c = *reinterpret_cast<uint16_t*>( |
| 4138 | reinterpret_cast<char*>(this) - |
| 4139 | kHeapObjectTag + kHeaderSize + offset * kShortSize); |
| 4140 | if (c <= kMaxAsciiCharCode) { |
| 4141 | // Fast case for ASCII characters. Cursor is an input output argument. |
| 4142 | if (!unibrow::CharacterStream::EncodeAsciiCharacter(c, |
| 4143 | rbb->util_buffer, |
| 4144 | rbb->capacity, |
| 4145 | rbb->cursor)) { |
| 4146 | break; |
| 4147 | } |
| 4148 | } else { |
| 4149 | if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c, |
| 4150 | rbb->util_buffer, |
| 4151 | rbb->capacity, |
| 4152 | rbb->cursor)) { |
| 4153 | break; |
| 4154 | } |
| 4155 | } |
| 4156 | offset++; |
| 4157 | chars_read++; |
| 4158 | } |
| 4159 | *offset_ptr = offset; |
| 4160 | rbb->remaining += chars_read; |
| 4161 | } |
| 4162 | |
| 4163 | |
| 4164 | const unibrow::byte* SeqAsciiString::SeqAsciiStringReadBlock( |
| 4165 | unsigned* remaining, |
| 4166 | unsigned* offset_ptr, |
| 4167 | unsigned max_chars) { |
| 4168 | const unibrow::byte* b = reinterpret_cast<unibrow::byte*>(this) - |
| 4169 | kHeapObjectTag + kHeaderSize + *offset_ptr * kCharSize; |
| 4170 | *remaining = max_chars; |
| 4171 | *offset_ptr += max_chars; |
| 4172 | return b; |
| 4173 | } |
| 4174 | |
| 4175 | |
| 4176 | // This will iterate unless the block of string data spans two 'halves' of |
| 4177 | // a ConsString, in which case it will recurse. Since the block of string |
| 4178 | // data to be read has a maximum size this limits the maximum recursion |
| 4179 | // depth to something sane. Since C++ does not have tail call recursion |
| 4180 | // elimination, the iteration must be explicit. Since this is not an |
| 4181 | // -IntoBuffer method it can delegate to one of the efficient |
| 4182 | // *AsciiStringReadBlock routines. |
| 4183 | const unibrow::byte* ConsString::ConsStringReadBlock(ReadBlockBuffer* rbb, |
| 4184 | unsigned* offset_ptr, |
| 4185 | unsigned max_chars) { |
| 4186 | ConsString* current = this; |
| 4187 | unsigned offset = *offset_ptr; |
| 4188 | int offset_correction = 0; |
| 4189 | |
| 4190 | while (true) { |
| 4191 | String* left = current->first(); |
| 4192 | unsigned left_length = (unsigned)left->length(); |
| 4193 | if (left_length > offset && |
| 4194 | (max_chars <= left_length - offset || |
| 4195 | (rbb->capacity <= left_length - offset && |
| 4196 | (max_chars = left_length - offset, true)))) { // comma operator! |
| 4197 | // Left hand side only - iterate unless we have reached the bottom of |
| 4198 | // the cons tree. The assignment on the left of the comma operator is |
| 4199 | // in order to make use of the fact that the -IntoBuffer routines can |
| 4200 | // produce at most 'capacity' characters. This enables us to postpone |
| 4201 | // the point where we switch to the -IntoBuffer routines (below) in order |
| 4202 | // to maximize the chances of delegating a big chunk of work to the |
| 4203 | // efficient *AsciiStringReadBlock routines. |
| 4204 | if (StringShape(left).IsCons()) { |
| 4205 | current = ConsString::cast(left); |
| 4206 | continue; |
| 4207 | } else { |
| 4208 | const unibrow::byte* answer = |
| 4209 | String::ReadBlock(left, rbb, &offset, max_chars); |
| 4210 | *offset_ptr = offset + offset_correction; |
| 4211 | return answer; |
| 4212 | } |
| 4213 | } else if (left_length <= offset) { |
| 4214 | // Right hand side only - iterate unless we have reached the bottom of |
| 4215 | // the cons tree. |
| 4216 | String* right = current->second(); |
| 4217 | offset -= left_length; |
| 4218 | offset_correction += left_length; |
| 4219 | if (StringShape(right).IsCons()) { |
| 4220 | current = ConsString::cast(right); |
| 4221 | continue; |
| 4222 | } else { |
| 4223 | const unibrow::byte* answer = |
| 4224 | String::ReadBlock(right, rbb, &offset, max_chars); |
| 4225 | *offset_ptr = offset + offset_correction; |
| 4226 | return answer; |
| 4227 | } |
| 4228 | } else { |
| 4229 | // The block to be read spans two sides of the ConsString, so we call the |
| 4230 | // -IntoBuffer version, which will recurse. The -IntoBuffer methods |
| 4231 | // are able to assemble data from several part strings because they use |
| 4232 | // the util_buffer to store their data and never return direct pointers |
| 4233 | // to their storage. We don't try to read more than the buffer capacity |
| 4234 | // here or we can get too much recursion. |
| 4235 | ASSERT(rbb->remaining == 0); |
| 4236 | ASSERT(rbb->cursor == 0); |
| 4237 | current->ConsStringReadBlockIntoBuffer( |
| 4238 | rbb, |
| 4239 | &offset, |
| 4240 | max_chars > rbb->capacity ? rbb->capacity : max_chars); |
| 4241 | *offset_ptr = offset + offset_correction; |
| 4242 | return rbb->util_buffer; |
| 4243 | } |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4248 | uint16_t ExternalAsciiString::ExternalAsciiStringGet(int index) { |
| 4249 | ASSERT(index >= 0 && index < length()); |
| 4250 | return resource()->data()[index]; |
| 4251 | } |
| 4252 | |
| 4253 | |
| 4254 | const unibrow::byte* ExternalAsciiString::ExternalAsciiStringReadBlock( |
| 4255 | unsigned* remaining, |
| 4256 | unsigned* offset_ptr, |
| 4257 | unsigned max_chars) { |
| 4258 | // Cast const char* to unibrow::byte* (signedness difference). |
| 4259 | const unibrow::byte* b = |
| 4260 | reinterpret_cast<const unibrow::byte*>(resource()->data()) + *offset_ptr; |
| 4261 | *remaining = max_chars; |
| 4262 | *offset_ptr += max_chars; |
| 4263 | return b; |
| 4264 | } |
| 4265 | |
| 4266 | |
| 4267 | const uc16* ExternalTwoByteString::ExternalTwoByteStringGetData( |
| 4268 | unsigned start) { |
| 4269 | return resource()->data() + start; |
| 4270 | } |
| 4271 | |
| 4272 | |
| 4273 | uint16_t ExternalTwoByteString::ExternalTwoByteStringGet(int index) { |
| 4274 | ASSERT(index >= 0 && index < length()); |
| 4275 | return resource()->data()[index]; |
| 4276 | } |
| 4277 | |
| 4278 | |
| 4279 | void ExternalTwoByteString::ExternalTwoByteStringReadBlockIntoBuffer( |
| 4280 | ReadBlockBuffer* rbb, |
| 4281 | unsigned* offset_ptr, |
| 4282 | unsigned max_chars) { |
| 4283 | unsigned chars_read = 0; |
| 4284 | unsigned offset = *offset_ptr; |
| 4285 | const uint16_t* data = resource()->data(); |
| 4286 | while (chars_read < max_chars) { |
| 4287 | uint16_t c = data[offset]; |
| 4288 | if (c <= kMaxAsciiCharCode) { |
| 4289 | // Fast case for ASCII characters. Cursor is an input output argument. |
| 4290 | if (!unibrow::CharacterStream::EncodeAsciiCharacter(c, |
| 4291 | rbb->util_buffer, |
| 4292 | rbb->capacity, |
| 4293 | rbb->cursor)) |
| 4294 | break; |
| 4295 | } else { |
| 4296 | if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c, |
| 4297 | rbb->util_buffer, |
| 4298 | rbb->capacity, |
| 4299 | rbb->cursor)) |
| 4300 | break; |
| 4301 | } |
| 4302 | offset++; |
| 4303 | chars_read++; |
| 4304 | } |
| 4305 | *offset_ptr = offset; |
| 4306 | rbb->remaining += chars_read; |
| 4307 | } |
| 4308 | |
| 4309 | |
| 4310 | void SeqAsciiString::SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 4311 | unsigned* offset_ptr, |
| 4312 | unsigned max_chars) { |
| 4313 | unsigned capacity = rbb->capacity - rbb->cursor; |
| 4314 | if (max_chars > capacity) max_chars = capacity; |
| 4315 | memcpy(rbb->util_buffer + rbb->cursor, |
| 4316 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize + |
| 4317 | *offset_ptr * kCharSize, |
| 4318 | max_chars); |
| 4319 | rbb->remaining += max_chars; |
| 4320 | *offset_ptr += max_chars; |
| 4321 | rbb->cursor += max_chars; |
| 4322 | } |
| 4323 | |
| 4324 | |
| 4325 | void ExternalAsciiString::ExternalAsciiStringReadBlockIntoBuffer( |
| 4326 | ReadBlockBuffer* rbb, |
| 4327 | unsigned* offset_ptr, |
| 4328 | unsigned max_chars) { |
| 4329 | unsigned capacity = rbb->capacity - rbb->cursor; |
| 4330 | if (max_chars > capacity) max_chars = capacity; |
| 4331 | memcpy(rbb->util_buffer + rbb->cursor, |
| 4332 | resource()->data() + *offset_ptr, |
| 4333 | max_chars); |
| 4334 | rbb->remaining += max_chars; |
| 4335 | *offset_ptr += max_chars; |
| 4336 | rbb->cursor += max_chars; |
| 4337 | } |
| 4338 | |
| 4339 | |
| 4340 | // This method determines the type of string involved and then copies |
| 4341 | // a whole chunk of characters into a buffer, or returns a pointer to a buffer |
| 4342 | // where they can be found. The pointer is not necessarily valid across a GC |
| 4343 | // (see AsciiStringReadBlock). |
| 4344 | const unibrow::byte* String::ReadBlock(String* input, |
| 4345 | ReadBlockBuffer* rbb, |
| 4346 | unsigned* offset_ptr, |
| 4347 | unsigned max_chars) { |
| 4348 | ASSERT(*offset_ptr <= static_cast<unsigned>(input->length())); |
| 4349 | if (max_chars == 0) { |
| 4350 | rbb->remaining = 0; |
| 4351 | return NULL; |
| 4352 | } |
| 4353 | switch (StringShape(input).representation_tag()) { |
| 4354 | case kSeqStringTag: |
| 4355 | if (input->IsAsciiRepresentation()) { |
| 4356 | SeqAsciiString* str = SeqAsciiString::cast(input); |
| 4357 | return str->SeqAsciiStringReadBlock(&rbb->remaining, |
| 4358 | offset_ptr, |
| 4359 | max_chars); |
| 4360 | } else { |
| 4361 | SeqTwoByteString* str = SeqTwoByteString::cast(input); |
| 4362 | str->SeqTwoByteStringReadBlockIntoBuffer(rbb, |
| 4363 | offset_ptr, |
| 4364 | max_chars); |
| 4365 | return rbb->util_buffer; |
| 4366 | } |
| 4367 | case kConsStringTag: |
| 4368 | return ConsString::cast(input)->ConsStringReadBlock(rbb, |
| 4369 | offset_ptr, |
| 4370 | max_chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4371 | case kExternalStringTag: |
| 4372 | if (input->IsAsciiRepresentation()) { |
| 4373 | return ExternalAsciiString::cast(input)->ExternalAsciiStringReadBlock( |
| 4374 | &rbb->remaining, |
| 4375 | offset_ptr, |
| 4376 | max_chars); |
| 4377 | } else { |
| 4378 | ExternalTwoByteString::cast(input)-> |
| 4379 | ExternalTwoByteStringReadBlockIntoBuffer(rbb, |
| 4380 | offset_ptr, |
| 4381 | max_chars); |
| 4382 | return rbb->util_buffer; |
| 4383 | } |
| 4384 | default: |
| 4385 | break; |
| 4386 | } |
| 4387 | |
| 4388 | UNREACHABLE(); |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
| 4392 | |
| 4393 | Relocatable* Relocatable::top_ = NULL; |
| 4394 | |
| 4395 | |
| 4396 | void Relocatable::PostGarbageCollectionProcessing() { |
| 4397 | Relocatable* current = top_; |
| 4398 | while (current != NULL) { |
| 4399 | current->PostGarbageCollection(); |
| 4400 | current = current->prev_; |
| 4401 | } |
| 4402 | } |
| 4403 | |
| 4404 | |
| 4405 | // Reserve space for statics needing saving and restoring. |
| 4406 | int Relocatable::ArchiveSpacePerThread() { |
| 4407 | return sizeof(top_); |
| 4408 | } |
| 4409 | |
| 4410 | |
| 4411 | // Archive statics that are thread local. |
| 4412 | char* Relocatable::ArchiveState(char* to) { |
| 4413 | *reinterpret_cast<Relocatable**>(to) = top_; |
| 4414 | top_ = NULL; |
| 4415 | return to + ArchiveSpacePerThread(); |
| 4416 | } |
| 4417 | |
| 4418 | |
| 4419 | // Restore statics that are thread local. |
| 4420 | char* Relocatable::RestoreState(char* from) { |
| 4421 | top_ = *reinterpret_cast<Relocatable**>(from); |
| 4422 | return from + ArchiveSpacePerThread(); |
| 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 4427 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 4428 | Iterate(v, top); |
| 4429 | return thread_storage + ArchiveSpacePerThread(); |
| 4430 | } |
| 4431 | |
| 4432 | |
| 4433 | void Relocatable::Iterate(ObjectVisitor* v) { |
| 4434 | Iterate(v, top_); |
| 4435 | } |
| 4436 | |
| 4437 | |
| 4438 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 4439 | Relocatable* current = top; |
| 4440 | while (current != NULL) { |
| 4441 | current->IterateInstance(v); |
| 4442 | current = current->prev_; |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | |
| 4447 | FlatStringReader::FlatStringReader(Handle<String> str) |
| 4448 | : str_(str.location()), |
| 4449 | length_(str->length()) { |
| 4450 | PostGarbageCollection(); |
| 4451 | } |
| 4452 | |
| 4453 | |
| 4454 | FlatStringReader::FlatStringReader(Vector<const char> input) |
| 4455 | : str_(0), |
| 4456 | is_ascii_(true), |
| 4457 | length_(input.length()), |
| 4458 | start_(input.start()) { } |
| 4459 | |
| 4460 | |
| 4461 | void FlatStringReader::PostGarbageCollection() { |
| 4462 | if (str_ == NULL) return; |
| 4463 | Handle<String> str(str_); |
| 4464 | ASSERT(str->IsFlat()); |
| 4465 | is_ascii_ = str->IsAsciiRepresentation(); |
| 4466 | if (is_ascii_) { |
| 4467 | start_ = str->ToAsciiVector().start(); |
| 4468 | } else { |
| 4469 | start_ = str->ToUC16Vector().start(); |
| 4470 | } |
| 4471 | } |
| 4472 | |
| 4473 | |
| 4474 | void StringInputBuffer::Seek(unsigned pos) { |
| 4475 | Reset(pos, input_); |
| 4476 | } |
| 4477 | |
| 4478 | |
| 4479 | void SafeStringInputBuffer::Seek(unsigned pos) { |
| 4480 | Reset(pos, input_); |
| 4481 | } |
| 4482 | |
| 4483 | |
| 4484 | // This method determines the type of string involved and then copies |
| 4485 | // a whole chunk of characters into a buffer. It can be used with strings |
| 4486 | // that have been glued together to form a ConsString and which must cooperate |
| 4487 | // to fill up a buffer. |
| 4488 | void String::ReadBlockIntoBuffer(String* input, |
| 4489 | ReadBlockBuffer* rbb, |
| 4490 | unsigned* offset_ptr, |
| 4491 | unsigned max_chars) { |
| 4492 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 4493 | if (max_chars == 0) return; |
| 4494 | |
| 4495 | switch (StringShape(input).representation_tag()) { |
| 4496 | case kSeqStringTag: |
| 4497 | if (input->IsAsciiRepresentation()) { |
| 4498 | SeqAsciiString::cast(input)->SeqAsciiStringReadBlockIntoBuffer(rbb, |
| 4499 | offset_ptr, |
| 4500 | max_chars); |
| 4501 | return; |
| 4502 | } else { |
| 4503 | SeqTwoByteString::cast(input)->SeqTwoByteStringReadBlockIntoBuffer(rbb, |
| 4504 | offset_ptr, |
| 4505 | max_chars); |
| 4506 | return; |
| 4507 | } |
| 4508 | case kConsStringTag: |
| 4509 | ConsString::cast(input)->ConsStringReadBlockIntoBuffer(rbb, |
| 4510 | offset_ptr, |
| 4511 | max_chars); |
| 4512 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4513 | case kExternalStringTag: |
| 4514 | if (input->IsAsciiRepresentation()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4515 | ExternalAsciiString::cast(input)-> |
| 4516 | ExternalAsciiStringReadBlockIntoBuffer(rbb, offset_ptr, max_chars); |
| 4517 | } else { |
| 4518 | ExternalTwoByteString::cast(input)-> |
| 4519 | ExternalTwoByteStringReadBlockIntoBuffer(rbb, |
| 4520 | offset_ptr, |
| 4521 | max_chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4522 | } |
| 4523 | return; |
| 4524 | default: |
| 4525 | break; |
| 4526 | } |
| 4527 | |
| 4528 | UNREACHABLE(); |
| 4529 | return; |
| 4530 | } |
| 4531 | |
| 4532 | |
| 4533 | const unibrow::byte* String::ReadBlock(String* input, |
| 4534 | unibrow::byte* util_buffer, |
| 4535 | unsigned capacity, |
| 4536 | unsigned* remaining, |
| 4537 | unsigned* offset_ptr) { |
| 4538 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 4539 | unsigned chars = input->length() - *offset_ptr; |
| 4540 | ReadBlockBuffer rbb(util_buffer, 0, capacity, 0); |
| 4541 | const unibrow::byte* answer = ReadBlock(input, &rbb, offset_ptr, chars); |
| 4542 | ASSERT(rbb.remaining <= static_cast<unsigned>(input->length())); |
| 4543 | *remaining = rbb.remaining; |
| 4544 | return answer; |
| 4545 | } |
| 4546 | |
| 4547 | |
| 4548 | const unibrow::byte* String::ReadBlock(String** raw_input, |
| 4549 | unibrow::byte* util_buffer, |
| 4550 | unsigned capacity, |
| 4551 | unsigned* remaining, |
| 4552 | unsigned* offset_ptr) { |
| 4553 | Handle<String> input(raw_input); |
| 4554 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 4555 | unsigned chars = input->length() - *offset_ptr; |
| 4556 | if (chars > capacity) chars = capacity; |
| 4557 | ReadBlockBuffer rbb(util_buffer, 0, capacity, 0); |
| 4558 | ReadBlockIntoBuffer(*input, &rbb, offset_ptr, chars); |
| 4559 | ASSERT(rbb.remaining <= static_cast<unsigned>(input->length())); |
| 4560 | *remaining = rbb.remaining; |
| 4561 | return rbb.util_buffer; |
| 4562 | } |
| 4563 | |
| 4564 | |
| 4565 | // This will iterate unless the block of string data spans two 'halves' of |
| 4566 | // a ConsString, in which case it will recurse. Since the block of string |
| 4567 | // data to be read has a maximum size this limits the maximum recursion |
| 4568 | // depth to something sane. Since C++ does not have tail call recursion |
| 4569 | // elimination, the iteration must be explicit. |
| 4570 | void ConsString::ConsStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 4571 | unsigned* offset_ptr, |
| 4572 | unsigned max_chars) { |
| 4573 | ConsString* current = this; |
| 4574 | unsigned offset = *offset_ptr; |
| 4575 | int offset_correction = 0; |
| 4576 | |
| 4577 | while (true) { |
| 4578 | String* left = current->first(); |
| 4579 | unsigned left_length = (unsigned)left->length(); |
| 4580 | if (left_length > offset && |
| 4581 | max_chars <= left_length - offset) { |
| 4582 | // Left hand side only - iterate unless we have reached the bottom of |
| 4583 | // the cons tree. |
| 4584 | if (StringShape(left).IsCons()) { |
| 4585 | current = ConsString::cast(left); |
| 4586 | continue; |
| 4587 | } else { |
| 4588 | String::ReadBlockIntoBuffer(left, rbb, &offset, max_chars); |
| 4589 | *offset_ptr = offset + offset_correction; |
| 4590 | return; |
| 4591 | } |
| 4592 | } else if (left_length <= offset) { |
| 4593 | // Right hand side only - iterate unless we have reached the bottom of |
| 4594 | // the cons tree. |
| 4595 | offset -= left_length; |
| 4596 | offset_correction += left_length; |
| 4597 | String* right = current->second(); |
| 4598 | if (StringShape(right).IsCons()) { |
| 4599 | current = ConsString::cast(right); |
| 4600 | continue; |
| 4601 | } else { |
| 4602 | String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars); |
| 4603 | *offset_ptr = offset + offset_correction; |
| 4604 | return; |
| 4605 | } |
| 4606 | } else { |
| 4607 | // The block to be read spans two sides of the ConsString, so we recurse. |
| 4608 | // First recurse on the left. |
| 4609 | max_chars -= left_length - offset; |
| 4610 | String::ReadBlockIntoBuffer(left, rbb, &offset, left_length - offset); |
| 4611 | // We may have reached the max or there may not have been enough space |
| 4612 | // in the buffer for the characters in the left hand side. |
| 4613 | if (offset == left_length) { |
| 4614 | // Recurse on the right. |
| 4615 | String* right = String::cast(current->second()); |
| 4616 | offset -= left_length; |
| 4617 | offset_correction += left_length; |
| 4618 | String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars); |
| 4619 | } |
| 4620 | *offset_ptr = offset + offset_correction; |
| 4621 | return; |
| 4622 | } |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4627 | uint16_t ConsString::ConsStringGet(int index) { |
| 4628 | ASSERT(index >= 0 && index < this->length()); |
| 4629 | |
| 4630 | // Check for a flattened cons string |
| 4631 | if (second()->length() == 0) { |
| 4632 | String* left = first(); |
| 4633 | return left->Get(index); |
| 4634 | } |
| 4635 | |
| 4636 | String* string = String::cast(this); |
| 4637 | |
| 4638 | while (true) { |
| 4639 | if (StringShape(string).IsCons()) { |
| 4640 | ConsString* cons_string = ConsString::cast(string); |
| 4641 | String* left = cons_string->first(); |
| 4642 | if (left->length() > index) { |
| 4643 | string = left; |
| 4644 | } else { |
| 4645 | index -= left->length(); |
| 4646 | string = cons_string->second(); |
| 4647 | } |
| 4648 | } else { |
| 4649 | return string->Get(index); |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | UNREACHABLE(); |
| 4654 | return 0; |
| 4655 | } |
| 4656 | |
| 4657 | |
| 4658 | template <typename sinkchar> |
| 4659 | void String::WriteToFlat(String* src, |
| 4660 | sinkchar* sink, |
| 4661 | int f, |
| 4662 | int t) { |
| 4663 | String* source = src; |
| 4664 | int from = f; |
| 4665 | int to = t; |
| 4666 | while (true) { |
| 4667 | ASSERT(0 <= from && from <= to && to <= source->length()); |
| 4668 | switch (StringShape(source).full_representation_tag()) { |
| 4669 | case kAsciiStringTag | kExternalStringTag: { |
| 4670 | CopyChars(sink, |
| 4671 | ExternalAsciiString::cast(source)->resource()->data() + from, |
| 4672 | to - from); |
| 4673 | return; |
| 4674 | } |
| 4675 | case kTwoByteStringTag | kExternalStringTag: { |
| 4676 | const uc16* data = |
| 4677 | ExternalTwoByteString::cast(source)->resource()->data(); |
| 4678 | CopyChars(sink, |
| 4679 | data + from, |
| 4680 | to - from); |
| 4681 | return; |
| 4682 | } |
| 4683 | case kAsciiStringTag | kSeqStringTag: { |
| 4684 | CopyChars(sink, |
| 4685 | SeqAsciiString::cast(source)->GetChars() + from, |
| 4686 | to - from); |
| 4687 | return; |
| 4688 | } |
| 4689 | case kTwoByteStringTag | kSeqStringTag: { |
| 4690 | CopyChars(sink, |
| 4691 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 4692 | to - from); |
| 4693 | return; |
| 4694 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4695 | case kAsciiStringTag | kConsStringTag: |
| 4696 | case kTwoByteStringTag | kConsStringTag: { |
| 4697 | ConsString* cons_string = ConsString::cast(source); |
| 4698 | String* first = cons_string->first(); |
| 4699 | int boundary = first->length(); |
| 4700 | if (to - boundary >= boundary - from) { |
| 4701 | // Right hand side is longer. Recurse over left. |
| 4702 | if (from < boundary) { |
| 4703 | WriteToFlat(first, sink, from, boundary); |
| 4704 | sink += boundary - from; |
| 4705 | from = 0; |
| 4706 | } else { |
| 4707 | from -= boundary; |
| 4708 | } |
| 4709 | to -= boundary; |
| 4710 | source = cons_string->second(); |
| 4711 | } else { |
| 4712 | // Left hand side is longer. Recurse over right. |
| 4713 | if (to > boundary) { |
| 4714 | String* second = cons_string->second(); |
| 4715 | WriteToFlat(second, |
| 4716 | sink + boundary - from, |
| 4717 | 0, |
| 4718 | to - boundary); |
| 4719 | to = boundary; |
| 4720 | } |
| 4721 | source = first; |
| 4722 | } |
| 4723 | break; |
| 4724 | } |
| 4725 | } |
| 4726 | } |
| 4727 | } |
| 4728 | |
| 4729 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4730 | template <typename IteratorA, typename IteratorB> |
| 4731 | static inline bool CompareStringContents(IteratorA* ia, IteratorB* ib) { |
| 4732 | // General slow case check. We know that the ia and ib iterators |
| 4733 | // have the same length. |
| 4734 | while (ia->has_more()) { |
| 4735 | uc32 ca = ia->GetNext(); |
| 4736 | uc32 cb = ib->GetNext(); |
| 4737 | if (ca != cb) |
| 4738 | return false; |
| 4739 | } |
| 4740 | return true; |
| 4741 | } |
| 4742 | |
| 4743 | |
| 4744 | // Compares the contents of two strings by reading and comparing |
| 4745 | // int-sized blocks of characters. |
| 4746 | template <typename Char> |
| 4747 | static inline bool CompareRawStringContents(Vector<Char> a, Vector<Char> b) { |
| 4748 | int length = a.length(); |
| 4749 | ASSERT_EQ(length, b.length()); |
| 4750 | const Char* pa = a.start(); |
| 4751 | const Char* pb = b.start(); |
| 4752 | int i = 0; |
| 4753 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
| 4754 | // If this architecture isn't comfortable reading unaligned ints |
| 4755 | // then we have to check that the strings are aligned before |
| 4756 | // comparing them blockwise. |
| 4757 | const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT |
| 4758 | uint32_t pa_addr = reinterpret_cast<uint32_t>(pa); |
| 4759 | uint32_t pb_addr = reinterpret_cast<uint32_t>(pb); |
| 4760 | if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) { |
| 4761 | #endif |
| 4762 | const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT |
| 4763 | int endpoint = length - kStepSize; |
| 4764 | // Compare blocks until we reach near the end of the string. |
| 4765 | for (; i <= endpoint; i += kStepSize) { |
| 4766 | uint32_t wa = *reinterpret_cast<const uint32_t*>(pa + i); |
| 4767 | uint32_t wb = *reinterpret_cast<const uint32_t*>(pb + i); |
| 4768 | if (wa != wb) { |
| 4769 | return false; |
| 4770 | } |
| 4771 | } |
| 4772 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
| 4773 | } |
| 4774 | #endif |
| 4775 | // Compare the remaining characters that didn't fit into a block. |
| 4776 | for (; i < length; i++) { |
| 4777 | if (a[i] != b[i]) { |
| 4778 | return false; |
| 4779 | } |
| 4780 | } |
| 4781 | return true; |
| 4782 | } |
| 4783 | |
| 4784 | |
| 4785 | static StringInputBuffer string_compare_buffer_b; |
| 4786 | |
| 4787 | |
| 4788 | template <typename IteratorA> |
| 4789 | static inline bool CompareStringContentsPartial(IteratorA* ia, String* b) { |
| 4790 | if (b->IsFlat()) { |
| 4791 | if (b->IsAsciiRepresentation()) { |
| 4792 | VectorIterator<char> ib(b->ToAsciiVector()); |
| 4793 | return CompareStringContents(ia, &ib); |
| 4794 | } else { |
| 4795 | VectorIterator<uc16> ib(b->ToUC16Vector()); |
| 4796 | return CompareStringContents(ia, &ib); |
| 4797 | } |
| 4798 | } else { |
| 4799 | string_compare_buffer_b.Reset(0, b); |
| 4800 | return CompareStringContents(ia, &string_compare_buffer_b); |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | |
| 4805 | static StringInputBuffer string_compare_buffer_a; |
| 4806 | |
| 4807 | |
| 4808 | bool String::SlowEquals(String* other) { |
| 4809 | // Fast check: negative check with lengths. |
| 4810 | int len = length(); |
| 4811 | if (len != other->length()) return false; |
| 4812 | if (len == 0) return true; |
| 4813 | |
| 4814 | // Fast check: if hash code is computed for both strings |
| 4815 | // a fast negative check can be performed. |
| 4816 | if (HasHashCode() && other->HasHashCode()) { |
| 4817 | if (Hash() != other->Hash()) return false; |
| 4818 | } |
| 4819 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4820 | // We know the strings are both non-empty. Compare the first chars |
| 4821 | // before we try to flatten the strings. |
| 4822 | if (this->Get(0) != other->Get(0)) return false; |
| 4823 | |
| 4824 | String* lhs = this->TryFlattenGetString(); |
| 4825 | String* rhs = other->TryFlattenGetString(); |
| 4826 | |
| 4827 | if (StringShape(lhs).IsSequentialAscii() && |
| 4828 | StringShape(rhs).IsSequentialAscii()) { |
| 4829 | const char* str1 = SeqAsciiString::cast(lhs)->GetChars(); |
| 4830 | const char* str2 = SeqAsciiString::cast(rhs)->GetChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4831 | return CompareRawStringContents(Vector<const char>(str1, len), |
| 4832 | Vector<const char>(str2, len)); |
| 4833 | } |
| 4834 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4835 | if (lhs->IsFlat()) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4836 | if (lhs->IsAsciiRepresentation()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4837 | Vector<const char> vec1 = lhs->ToAsciiVector(); |
| 4838 | if (rhs->IsFlat()) { |
| 4839 | if (rhs->IsAsciiRepresentation()) { |
| 4840 | Vector<const char> vec2 = rhs->ToAsciiVector(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4841 | return CompareRawStringContents(vec1, vec2); |
| 4842 | } else { |
| 4843 | VectorIterator<char> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4844 | VectorIterator<uc16> ib(rhs->ToUC16Vector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4845 | return CompareStringContents(&buf1, &ib); |
| 4846 | } |
| 4847 | } else { |
| 4848 | VectorIterator<char> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4849 | string_compare_buffer_b.Reset(0, rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4850 | return CompareStringContents(&buf1, &string_compare_buffer_b); |
| 4851 | } |
| 4852 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4853 | Vector<const uc16> vec1 = lhs->ToUC16Vector(); |
| 4854 | if (rhs->IsFlat()) { |
| 4855 | if (rhs->IsAsciiRepresentation()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4856 | VectorIterator<uc16> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4857 | VectorIterator<char> ib(rhs->ToAsciiVector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4858 | return CompareStringContents(&buf1, &ib); |
| 4859 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4860 | Vector<const uc16> vec2(rhs->ToUC16Vector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4861 | return CompareRawStringContents(vec1, vec2); |
| 4862 | } |
| 4863 | } else { |
| 4864 | VectorIterator<uc16> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4865 | string_compare_buffer_b.Reset(0, rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4866 | return CompareStringContents(&buf1, &string_compare_buffer_b); |
| 4867 | } |
| 4868 | } |
| 4869 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4870 | string_compare_buffer_a.Reset(0, lhs); |
| 4871 | return CompareStringContentsPartial(&string_compare_buffer_a, rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | |
| 4876 | bool String::MarkAsUndetectable() { |
| 4877 | if (StringShape(this).IsSymbol()) return false; |
| 4878 | |
| 4879 | Map* map = this->map(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4880 | if (map == Heap::string_map()) { |
| 4881 | this->set_map(Heap::undetectable_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4882 | return true; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4883 | } else if (map == Heap::ascii_string_map()) { |
| 4884 | this->set_map(Heap::undetectable_ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4885 | return true; |
| 4886 | } |
| 4887 | // Rest cannot be marked as undetectable |
| 4888 | return false; |
| 4889 | } |
| 4890 | |
| 4891 | |
| 4892 | bool String::IsEqualTo(Vector<const char> str) { |
| 4893 | int slen = length(); |
| 4894 | Access<Scanner::Utf8Decoder> decoder(Scanner::utf8_decoder()); |
| 4895 | decoder->Reset(str.start(), str.length()); |
| 4896 | int i; |
| 4897 | for (i = 0; i < slen && decoder->has_more(); i++) { |
| 4898 | uc32 r = decoder->GetNext(); |
| 4899 | if (Get(i) != r) return false; |
| 4900 | } |
| 4901 | return i == slen && !decoder->has_more(); |
| 4902 | } |
| 4903 | |
| 4904 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4905 | template <typename schar> |
| 4906 | static inline uint32_t HashSequentialString(const schar* chars, int length) { |
| 4907 | StringHasher hasher(length); |
| 4908 | if (!hasher.has_trivial_hash()) { |
| 4909 | int i; |
| 4910 | for (i = 0; hasher.is_array_index() && (i < length); i++) { |
| 4911 | hasher.AddCharacter(chars[i]); |
| 4912 | } |
| 4913 | for (; i < length; i++) { |
| 4914 | hasher.AddCharacterNoIndex(chars[i]); |
| 4915 | } |
| 4916 | } |
| 4917 | return hasher.GetHashField(); |
| 4918 | } |
| 4919 | |
| 4920 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4921 | uint32_t String::ComputeAndSetHash() { |
| 4922 | // Should only be called if hash code has not yet been computed. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4923 | ASSERT(!HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4924 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4925 | const int len = length(); |
| 4926 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4927 | // Compute the hash code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4928 | uint32_t field = 0; |
| 4929 | if (StringShape(this).IsSequentialAscii()) { |
| 4930 | field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); |
| 4931 | } else if (StringShape(this).IsSequentialTwoByte()) { |
| 4932 | field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); |
| 4933 | } else { |
| 4934 | StringInputBuffer buffer(this); |
| 4935 | field = ComputeHashField(&buffer, len); |
| 4936 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4937 | |
| 4938 | // Store the hash code in the object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4939 | set_hash_field(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4940 | |
| 4941 | // Check the hash code is there. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4942 | ASSERT(HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4943 | uint32_t result = field >> kHashShift; |
| 4944 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 4945 | return result; |
| 4946 | } |
| 4947 | |
| 4948 | |
| 4949 | bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, |
| 4950 | uint32_t* index, |
| 4951 | int length) { |
| 4952 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
| 4953 | uc32 ch = buffer->GetNext(); |
| 4954 | |
| 4955 | // If the string begins with a '0' character, it must only consist |
| 4956 | // of it to be a legal array index. |
| 4957 | if (ch == '0') { |
| 4958 | *index = 0; |
| 4959 | return length == 1; |
| 4960 | } |
| 4961 | |
| 4962 | // Convert string to uint32 array index; character by character. |
| 4963 | int d = ch - '0'; |
| 4964 | if (d < 0 || d > 9) return false; |
| 4965 | uint32_t result = d; |
| 4966 | while (buffer->has_more()) { |
| 4967 | d = buffer->GetNext() - '0'; |
| 4968 | if (d < 0 || d > 9) return false; |
| 4969 | // Check that the new result is below the 32 bit limit. |
| 4970 | if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 4971 | result = (result * 10) + d; |
| 4972 | } |
| 4973 | |
| 4974 | *index = result; |
| 4975 | return true; |
| 4976 | } |
| 4977 | |
| 4978 | |
| 4979 | bool String::SlowAsArrayIndex(uint32_t* index) { |
| 4980 | if (length() <= kMaxCachedArrayIndexLength) { |
| 4981 | Hash(); // force computation of hash code |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4982 | uint32_t field = hash_field(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4983 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4984 | // Isolate the array index form the full hash field. |
| 4985 | *index = (kArrayIndexHashMask & field) >> kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4986 | return true; |
| 4987 | } else { |
| 4988 | StringInputBuffer buffer(this); |
| 4989 | return ComputeArrayIndex(&buffer, index, length()); |
| 4990 | } |
| 4991 | } |
| 4992 | |
| 4993 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 4994 | uint32_t StringHasher::MakeCachedArrayIndex(uint32_t value, int length) { |
| 4995 | value <<= String::kHashShift; |
| 4996 | // For array indexes mix the length into the hash as an array index could |
| 4997 | // be zero. |
| 4998 | ASSERT(length > 0); |
| 4999 | ASSERT(length <= String::kMaxArrayIndexSize); |
| 5000 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 5001 | (1 << String::kArrayIndexValueBits)); |
| 5002 | ASSERT(String::kMaxArrayIndexSize < (1 << String::kArrayIndexValueBits)); |
| 5003 | value &= ~String::kIsNotArrayIndexMask; |
| 5004 | value |= length << String::kArrayIndexHashLengthShift; |
| 5005 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | |
| 5009 | uint32_t StringHasher::GetHashField() { |
| 5010 | ASSERT(is_valid()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5011 | if (length_ <= String::kMaxHashCalcLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5012 | if (is_array_index()) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 5013 | return MakeCachedArrayIndex(array_index(), length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5014 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 5015 | return (GetHash() << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5016 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame^] | 5017 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5022 | uint32_t String::ComputeHashField(unibrow::CharacterStream* buffer, |
| 5023 | int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5024 | StringHasher hasher(length); |
| 5025 | |
| 5026 | // Very long strings have a trivial hash that doesn't inspect the |
| 5027 | // string contents. |
| 5028 | if (hasher.has_trivial_hash()) { |
| 5029 | return hasher.GetHashField(); |
| 5030 | } |
| 5031 | |
| 5032 | // Do the iterative array index computation as long as there is a |
| 5033 | // chance this is an array index. |
| 5034 | while (buffer->has_more() && hasher.is_array_index()) { |
| 5035 | hasher.AddCharacter(buffer->GetNext()); |
| 5036 | } |
| 5037 | |
| 5038 | // Process the remaining characters without updating the array |
| 5039 | // index. |
| 5040 | while (buffer->has_more()) { |
| 5041 | hasher.AddCharacterNoIndex(buffer->GetNext()); |
| 5042 | } |
| 5043 | |
| 5044 | return hasher.GetHashField(); |
| 5045 | } |
| 5046 | |
| 5047 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5048 | Object* String::SubString(int start, int end, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5049 | if (start == 0 && end == length()) return this; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5050 | Object* result = Heap::AllocateSubString(this, start, end, pretenure); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5051 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | |
| 5055 | void String::PrintOn(FILE* file) { |
| 5056 | int length = this->length(); |
| 5057 | for (int i = 0; i < length; i++) { |
| 5058 | fprintf(file, "%c", Get(i)); |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | |
| 5063 | void Map::CreateBackPointers() { |
| 5064 | DescriptorArray* descriptors = instance_descriptors(); |
| 5065 | for (int i = 0; i < descriptors->number_of_descriptors(); i++) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5066 | if (descriptors->GetType(i) == MAP_TRANSITION || |
| 5067 | descriptors->GetType(i) == CONSTANT_TRANSITION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5068 | // Get target. |
| 5069 | Map* target = Map::cast(descriptors->GetValue(i)); |
| 5070 | #ifdef DEBUG |
| 5071 | // Verify target. |
| 5072 | Object* source_prototype = prototype(); |
| 5073 | Object* target_prototype = target->prototype(); |
| 5074 | ASSERT(source_prototype->IsJSObject() || |
| 5075 | source_prototype->IsMap() || |
| 5076 | source_prototype->IsNull()); |
| 5077 | ASSERT(target_prototype->IsJSObject() || |
| 5078 | target_prototype->IsNull()); |
| 5079 | ASSERT(source_prototype->IsMap() || |
| 5080 | source_prototype == target_prototype); |
| 5081 | #endif |
| 5082 | // Point target back to source. set_prototype() will not let us set |
| 5083 | // the prototype to a map, as we do here. |
| 5084 | *RawField(target, kPrototypeOffset) = this; |
| 5085 | } |
| 5086 | } |
| 5087 | } |
| 5088 | |
| 5089 | |
| 5090 | void Map::ClearNonLiveTransitions(Object* real_prototype) { |
| 5091 | // Live DescriptorArray objects will be marked, so we must use |
| 5092 | // low-level accessors to get and modify their data. |
| 5093 | DescriptorArray* d = reinterpret_cast<DescriptorArray*>( |
| 5094 | *RawField(this, Map::kInstanceDescriptorsOffset)); |
| 5095 | if (d == Heap::raw_unchecked_empty_descriptor_array()) return; |
| 5096 | Smi* NullDescriptorDetails = |
| 5097 | PropertyDetails(NONE, NULL_DESCRIPTOR).AsSmi(); |
| 5098 | FixedArray* contents = reinterpret_cast<FixedArray*>( |
| 5099 | d->get(DescriptorArray::kContentArrayIndex)); |
| 5100 | ASSERT(contents->length() >= 2); |
| 5101 | for (int i = 0; i < contents->length(); i += 2) { |
| 5102 | // If the pair (value, details) is a map transition, |
| 5103 | // check if the target is live. If not, null the descriptor. |
| 5104 | // Also drop the back pointer for that map transition, so that this |
| 5105 | // map is not reached again by following a back pointer from a |
| 5106 | // non-live object. |
| 5107 | PropertyDetails details(Smi::cast(contents->get(i + 1))); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5108 | if (details.type() == MAP_TRANSITION || |
| 5109 | details.type() == CONSTANT_TRANSITION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5110 | Map* target = reinterpret_cast<Map*>(contents->get(i)); |
| 5111 | ASSERT(target->IsHeapObject()); |
| 5112 | if (!target->IsMarked()) { |
| 5113 | ASSERT(target->IsMap()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5114 | contents->set_unchecked(i + 1, NullDescriptorDetails); |
| 5115 | contents->set_null_unchecked(i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5116 | ASSERT(target->prototype() == this || |
| 5117 | target->prototype() == real_prototype); |
| 5118 | // Getter prototype() is read-only, set_prototype() has side effects. |
| 5119 | *RawField(target, Map::kPrototypeOffset) = real_prototype; |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5126 | void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) { |
| 5127 | // Iterate over all fields in the body but take care in dealing with |
| 5128 | // the code entry. |
| 5129 | IteratePointers(v, kPropertiesOffset, kCodeEntryOffset); |
| 5130 | v->VisitCodeEntry(this->address() + kCodeEntryOffset); |
| 5131 | IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size); |
| 5132 | } |
| 5133 | |
| 5134 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | Object* JSFunction::SetInstancePrototype(Object* value) { |
| 5136 | ASSERT(value->IsJSObject()); |
| 5137 | |
| 5138 | if (has_initial_map()) { |
| 5139 | initial_map()->set_prototype(value); |
| 5140 | } else { |
| 5141 | // Put the value in the initial map field until an initial map is |
| 5142 | // needed. At that point, a new initial map is created and the |
| 5143 | // prototype is put into the initial map where it belongs. |
| 5144 | set_prototype_or_initial_map(value); |
| 5145 | } |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5146 | Heap::ClearInstanceofCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5147 | return value; |
| 5148 | } |
| 5149 | |
| 5150 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5151 | Object* JSFunction::SetPrototype(Object* value) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5152 | ASSERT(should_have_prototype()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5153 | Object* construct_prototype = value; |
| 5154 | |
| 5155 | // If the value is not a JSObject, store the value in the map's |
| 5156 | // constructor field so it can be accessed. Also, set the prototype |
| 5157 | // used for constructing objects to the original object prototype. |
| 5158 | // See ECMA-262 13.2.2. |
| 5159 | if (!value->IsJSObject()) { |
| 5160 | // Copy the map so this does not affect unrelated functions. |
| 5161 | // Remove map transitions because they point to maps with a |
| 5162 | // different prototype. |
| 5163 | Object* new_map = map()->CopyDropTransitions(); |
| 5164 | if (new_map->IsFailure()) return new_map; |
| 5165 | set_map(Map::cast(new_map)); |
| 5166 | map()->set_constructor(value); |
| 5167 | map()->set_non_instance_prototype(true); |
| 5168 | construct_prototype = |
| 5169 | Top::context()->global_context()->initial_object_prototype(); |
| 5170 | } else { |
| 5171 | map()->set_non_instance_prototype(false); |
| 5172 | } |
| 5173 | |
| 5174 | return SetInstancePrototype(construct_prototype); |
| 5175 | } |
| 5176 | |
| 5177 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5178 | Object* JSFunction::RemovePrototype() { |
| 5179 | ASSERT(map() == context()->global_context()->function_map()); |
| 5180 | set_map(context()->global_context()->function_without_prototype_map()); |
| 5181 | set_prototype_or_initial_map(Heap::the_hole_value()); |
| 5182 | return this; |
| 5183 | } |
| 5184 | |
| 5185 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5186 | Object* JSFunction::SetInstanceClassName(String* name) { |
| 5187 | shared()->set_instance_class_name(name); |
| 5188 | return this; |
| 5189 | } |
| 5190 | |
| 5191 | |
| 5192 | Context* JSFunction::GlobalContextFromLiterals(FixedArray* literals) { |
| 5193 | return Context::cast(literals->get(JSFunction::kLiteralGlobalContextIndex)); |
| 5194 | } |
| 5195 | |
| 5196 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5197 | Object* Oddball::Initialize(const char* to_string, Object* to_number) { |
| 5198 | Object* symbol = Heap::LookupAsciiSymbol(to_string); |
| 5199 | if (symbol->IsFailure()) return symbol; |
| 5200 | set_to_string(String::cast(symbol)); |
| 5201 | set_to_number(to_number); |
| 5202 | return this; |
| 5203 | } |
| 5204 | |
| 5205 | |
| 5206 | bool SharedFunctionInfo::HasSourceCode() { |
| 5207 | return !script()->IsUndefined() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5208 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | |
| 5212 | Object* SharedFunctionInfo::GetSourceCode() { |
| 5213 | HandleScope scope; |
| 5214 | if (script()->IsUndefined()) return Heap::undefined_value(); |
| 5215 | Object* source = Script::cast(script())->source(); |
| 5216 | if (source->IsUndefined()) return Heap::undefined_value(); |
| 5217 | return *SubString(Handle<String>(String::cast(source)), |
| 5218 | start_position(), end_position()); |
| 5219 | } |
| 5220 | |
| 5221 | |
| 5222 | int SharedFunctionInfo::CalculateInstanceSize() { |
| 5223 | int instance_size = |
| 5224 | JSObject::kHeaderSize + |
| 5225 | expected_nof_properties() * kPointerSize; |
| 5226 | if (instance_size > JSObject::kMaxInstanceSize) { |
| 5227 | instance_size = JSObject::kMaxInstanceSize; |
| 5228 | } |
| 5229 | return instance_size; |
| 5230 | } |
| 5231 | |
| 5232 | |
| 5233 | int SharedFunctionInfo::CalculateInObjectProperties() { |
| 5234 | return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize; |
| 5235 | } |
| 5236 | |
| 5237 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5238 | bool SharedFunctionInfo::CanGenerateInlineConstructor(Object* prototype) { |
| 5239 | // Check the basic conditions for generating inline constructor code. |
| 5240 | if (!FLAG_inline_new |
| 5241 | || !has_only_simple_this_property_assignments() |
| 5242 | || this_property_assignments_count() == 0) { |
| 5243 | return false; |
| 5244 | } |
| 5245 | |
| 5246 | // If the prototype is null inline constructors cause no problems. |
| 5247 | if (!prototype->IsJSObject()) { |
| 5248 | ASSERT(prototype->IsNull()); |
| 5249 | return true; |
| 5250 | } |
| 5251 | |
| 5252 | // Traverse the proposed prototype chain looking for setters for properties of |
| 5253 | // the same names as are set by the inline constructor. |
| 5254 | for (Object* obj = prototype; |
| 5255 | obj != Heap::null_value(); |
| 5256 | obj = obj->GetPrototype()) { |
| 5257 | JSObject* js_object = JSObject::cast(obj); |
| 5258 | for (int i = 0; i < this_property_assignments_count(); i++) { |
| 5259 | LookupResult result; |
| 5260 | String* name = GetThisPropertyAssignmentName(i); |
| 5261 | js_object->LocalLookupRealNamedProperty(name, &result); |
| 5262 | if (result.IsProperty() && result.type() == CALLBACKS) { |
| 5263 | return false; |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | |
| 5268 | return true; |
| 5269 | } |
| 5270 | |
| 5271 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5272 | void SharedFunctionInfo::SetThisPropertyAssignmentsInfo( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5273 | bool only_simple_this_property_assignments, |
| 5274 | FixedArray* assignments) { |
| 5275 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5276 | kHasOnlySimpleThisPropertyAssignments, |
| 5277 | only_simple_this_property_assignments)); |
| 5278 | set_this_property_assignments(assignments); |
| 5279 | set_this_property_assignments_count(assignments->length() / 3); |
| 5280 | } |
| 5281 | |
| 5282 | |
| 5283 | void SharedFunctionInfo::ClearThisPropertyAssignmentsInfo() { |
| 5284 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5285 | kHasOnlySimpleThisPropertyAssignments, |
| 5286 | false)); |
| 5287 | set_this_property_assignments(Heap::undefined_value()); |
| 5288 | set_this_property_assignments_count(0); |
| 5289 | } |
| 5290 | |
| 5291 | |
| 5292 | String* SharedFunctionInfo::GetThisPropertyAssignmentName(int index) { |
| 5293 | Object* obj = this_property_assignments(); |
| 5294 | ASSERT(obj->IsFixedArray()); |
| 5295 | ASSERT(index < this_property_assignments_count()); |
| 5296 | obj = FixedArray::cast(obj)->get(index * 3); |
| 5297 | ASSERT(obj->IsString()); |
| 5298 | return String::cast(obj); |
| 5299 | } |
| 5300 | |
| 5301 | |
| 5302 | bool SharedFunctionInfo::IsThisPropertyAssignmentArgument(int index) { |
| 5303 | Object* obj = this_property_assignments(); |
| 5304 | ASSERT(obj->IsFixedArray()); |
| 5305 | ASSERT(index < this_property_assignments_count()); |
| 5306 | obj = FixedArray::cast(obj)->get(index * 3 + 1); |
| 5307 | return Smi::cast(obj)->value() != -1; |
| 5308 | } |
| 5309 | |
| 5310 | |
| 5311 | int SharedFunctionInfo::GetThisPropertyAssignmentArgument(int index) { |
| 5312 | ASSERT(IsThisPropertyAssignmentArgument(index)); |
| 5313 | Object* obj = |
| 5314 | FixedArray::cast(this_property_assignments())->get(index * 3 + 1); |
| 5315 | return Smi::cast(obj)->value(); |
| 5316 | } |
| 5317 | |
| 5318 | |
| 5319 | Object* SharedFunctionInfo::GetThisPropertyAssignmentConstant(int index) { |
| 5320 | ASSERT(!IsThisPropertyAssignmentArgument(index)); |
| 5321 | Object* obj = |
| 5322 | FixedArray::cast(this_property_assignments())->get(index * 3 + 2); |
| 5323 | return obj; |
| 5324 | } |
| 5325 | |
| 5326 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5327 | // Support function for printing the source code to a StringStream |
| 5328 | // without any allocation in the heap. |
| 5329 | void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator, |
| 5330 | int max_length) { |
| 5331 | // For some native functions there is no source. |
| 5332 | if (script()->IsUndefined() || |
| 5333 | Script::cast(script())->source()->IsUndefined()) { |
| 5334 | accumulator->Add("<No Source>"); |
| 5335 | return; |
| 5336 | } |
| 5337 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5338 | // Get the source for the script which this function came from. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5339 | // Don't use String::cast because we don't want more assertion errors while |
| 5340 | // we are already creating a stack dump. |
| 5341 | String* script_source = |
| 5342 | reinterpret_cast<String*>(Script::cast(script())->source()); |
| 5343 | |
| 5344 | if (!script_source->LooksValid()) { |
| 5345 | accumulator->Add("<Invalid Source>"); |
| 5346 | return; |
| 5347 | } |
| 5348 | |
| 5349 | if (!is_toplevel()) { |
| 5350 | accumulator->Add("function "); |
| 5351 | Object* name = this->name(); |
| 5352 | if (name->IsString() && String::cast(name)->length() > 0) { |
| 5353 | accumulator->PrintName(name); |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | int len = end_position() - start_position(); |
| 5358 | if (len > max_length) { |
| 5359 | accumulator->Put(script_source, |
| 5360 | start_position(), |
| 5361 | start_position() + max_length); |
| 5362 | accumulator->Add("...\n"); |
| 5363 | } else { |
| 5364 | accumulator->Put(script_source, start_position(), end_position()); |
| 5365 | } |
| 5366 | } |
| 5367 | |
| 5368 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5369 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
| 5370 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
| 5371 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 5372 | Object* old_target = target; |
| 5373 | VisitPointer(&target); |
| 5374 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 5375 | } |
| 5376 | |
| 5377 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5378 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 5379 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 5380 | Object* old_code = code; |
| 5381 | VisitPointer(&code); |
| 5382 | if (code != old_code) { |
| 5383 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 5384 | } |
| 5385 | } |
| 5386 | |
| 5387 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5388 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5389 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 5390 | rinfo->IsPatchedReturnSequence()) || |
| 5391 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 5392 | rinfo->IsPatchedDebugBreakSlotSequence())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5393 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 5394 | Object* old_target = target; |
| 5395 | VisitPointer(&target); |
| 5396 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 5397 | } |
| 5398 | |
| 5399 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5400 | void Code::Relocate(intptr_t delta) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5401 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
| 5402 | it.rinfo()->apply(delta); |
| 5403 | } |
| 5404 | CPU::FlushICache(instruction_start(), instruction_size()); |
| 5405 | } |
| 5406 | |
| 5407 | |
| 5408 | void Code::CopyFrom(const CodeDesc& desc) { |
| 5409 | // copy code |
| 5410 | memmove(instruction_start(), desc.buffer, desc.instr_size); |
| 5411 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5412 | // copy reloc info |
| 5413 | memmove(relocation_start(), |
| 5414 | desc.buffer + desc.buffer_size - desc.reloc_size, |
| 5415 | desc.reloc_size); |
| 5416 | |
| 5417 | // unbox handles and relocate |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5418 | intptr_t delta = instruction_start() - desc.buffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5419 | int mode_mask = RelocInfo::kCodeTargetMask | |
| 5420 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
| 5421 | RelocInfo::kApplyMask; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5422 | Assembler* origin = desc.origin; // Needed to find target_object on X64. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5423 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 5424 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 5425 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5426 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5427 | it.rinfo()->set_target_object(*p); |
| 5428 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 5429 | // rewrite code handles in inline cache targets to direct |
| 5430 | // pointers to the first instruction in the code object |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5431 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5432 | Code* code = Code::cast(*p); |
| 5433 | it.rinfo()->set_target_address(code->instruction_start()); |
| 5434 | } else { |
| 5435 | it.rinfo()->apply(delta); |
| 5436 | } |
| 5437 | } |
| 5438 | CPU::FlushICache(instruction_start(), instruction_size()); |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | // Locate the source position which is closest to the address in the code. This |
| 5443 | // is using the source position information embedded in the relocation info. |
| 5444 | // The position returned is relative to the beginning of the script where the |
| 5445 | // source for this function is found. |
| 5446 | int Code::SourcePosition(Address pc) { |
| 5447 | int distance = kMaxInt; |
| 5448 | int position = RelocInfo::kNoPosition; // Initially no position found. |
| 5449 | // Run through all the relocation info to find the best matching source |
| 5450 | // position. All the code needs to be considered as the sequence of the |
| 5451 | // instructions in the code does not necessarily follow the same order as the |
| 5452 | // source. |
| 5453 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 5454 | while (!it.done()) { |
| 5455 | // Only look at positions after the current pc. |
| 5456 | if (it.rinfo()->pc() < pc) { |
| 5457 | // Get position and distance. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5458 | |
| 5459 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 5460 | int pos = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5461 | // If this position is closer than the current candidate or if it has the |
| 5462 | // same distance as the current candidate and the position is higher then |
| 5463 | // this position is the new candidate. |
| 5464 | if ((dist < distance) || |
| 5465 | (dist == distance && pos > position)) { |
| 5466 | position = pos; |
| 5467 | distance = dist; |
| 5468 | } |
| 5469 | } |
| 5470 | it.next(); |
| 5471 | } |
| 5472 | return position; |
| 5473 | } |
| 5474 | |
| 5475 | |
| 5476 | // Same as Code::SourcePosition above except it only looks for statement |
| 5477 | // positions. |
| 5478 | int Code::SourceStatementPosition(Address pc) { |
| 5479 | // First find the position as close as possible using all position |
| 5480 | // information. |
| 5481 | int position = SourcePosition(pc); |
| 5482 | // Now find the closest statement position before the position. |
| 5483 | int statement_position = 0; |
| 5484 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 5485 | while (!it.done()) { |
| 5486 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5487 | int p = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5488 | if (statement_position < p && p <= position) { |
| 5489 | statement_position = p; |
| 5490 | } |
| 5491 | } |
| 5492 | it.next(); |
| 5493 | } |
| 5494 | return statement_position; |
| 5495 | } |
| 5496 | |
| 5497 | |
| 5498 | #ifdef ENABLE_DISASSEMBLER |
| 5499 | // Identify kind of code. |
| 5500 | const char* Code::Kind2String(Kind kind) { |
| 5501 | switch (kind) { |
| 5502 | case FUNCTION: return "FUNCTION"; |
| 5503 | case STUB: return "STUB"; |
| 5504 | case BUILTIN: return "BUILTIN"; |
| 5505 | case LOAD_IC: return "LOAD_IC"; |
| 5506 | case KEYED_LOAD_IC: return "KEYED_LOAD_IC"; |
| 5507 | case STORE_IC: return "STORE_IC"; |
| 5508 | case KEYED_STORE_IC: return "KEYED_STORE_IC"; |
| 5509 | case CALL_IC: return "CALL_IC"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5510 | case KEYED_CALL_IC: return "KEYED_CALL_IC"; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5511 | case BINARY_OP_IC: return "BINARY_OP_IC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5512 | } |
| 5513 | UNREACHABLE(); |
| 5514 | return NULL; |
| 5515 | } |
| 5516 | |
| 5517 | |
| 5518 | const char* Code::ICState2String(InlineCacheState state) { |
| 5519 | switch (state) { |
| 5520 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 5521 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 5522 | case MONOMORPHIC: return "MONOMORPHIC"; |
| 5523 | case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE"; |
| 5524 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
| 5525 | case DEBUG_BREAK: return "DEBUG_BREAK"; |
| 5526 | case DEBUG_PREPARE_STEP_IN: return "DEBUG_PREPARE_STEP_IN"; |
| 5527 | } |
| 5528 | UNREACHABLE(); |
| 5529 | return NULL; |
| 5530 | } |
| 5531 | |
| 5532 | |
| 5533 | const char* Code::PropertyType2String(PropertyType type) { |
| 5534 | switch (type) { |
| 5535 | case NORMAL: return "NORMAL"; |
| 5536 | case FIELD: return "FIELD"; |
| 5537 | case CONSTANT_FUNCTION: return "CONSTANT_FUNCTION"; |
| 5538 | case CALLBACKS: return "CALLBACKS"; |
| 5539 | case INTERCEPTOR: return "INTERCEPTOR"; |
| 5540 | case MAP_TRANSITION: return "MAP_TRANSITION"; |
| 5541 | case CONSTANT_TRANSITION: return "CONSTANT_TRANSITION"; |
| 5542 | case NULL_DESCRIPTOR: return "NULL_DESCRIPTOR"; |
| 5543 | } |
| 5544 | UNREACHABLE(); |
| 5545 | return NULL; |
| 5546 | } |
| 5547 | |
| 5548 | void Code::Disassemble(const char* name) { |
| 5549 | PrintF("kind = %s\n", Kind2String(kind())); |
| 5550 | if (is_inline_cache_stub()) { |
| 5551 | PrintF("ic_state = %s\n", ICState2String(ic_state())); |
| 5552 | PrintF("ic_in_loop = %d\n", ic_in_loop() == IN_LOOP); |
| 5553 | if (ic_state() == MONOMORPHIC) { |
| 5554 | PrintF("type = %s\n", PropertyType2String(type())); |
| 5555 | } |
| 5556 | } |
| 5557 | if ((name != NULL) && (name[0] != '\0')) { |
| 5558 | PrintF("name = %s\n", name); |
| 5559 | } |
| 5560 | |
| 5561 | PrintF("Instructions (size = %d)\n", instruction_size()); |
| 5562 | Disassembler::Decode(NULL, this); |
| 5563 | PrintF("\n"); |
| 5564 | |
| 5565 | PrintF("RelocInfo (size = %d)\n", relocation_size()); |
| 5566 | for (RelocIterator it(this); !it.done(); it.next()) |
| 5567 | it.rinfo()->Print(); |
| 5568 | PrintF("\n"); |
| 5569 | } |
| 5570 | #endif // ENABLE_DISASSEMBLER |
| 5571 | |
| 5572 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5573 | Object* JSObject::SetFastElementsCapacityAndLength(int capacity, int length) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5574 | // We should never end in here with a pixel or external array. |
| 5575 | ASSERT(!HasPixelElements() && !HasExternalArrayElements()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5576 | |
| 5577 | Object* obj = Heap::AllocateFixedArrayWithHoles(capacity); |
| 5578 | if (obj->IsFailure()) return obj; |
| 5579 | FixedArray* elems = FixedArray::cast(obj); |
| 5580 | |
| 5581 | obj = map()->GetFastElementsMap(); |
| 5582 | if (obj->IsFailure()) return obj; |
| 5583 | Map* new_map = Map::cast(obj); |
| 5584 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5585 | AssertNoAllocation no_gc; |
| 5586 | WriteBarrierMode mode = elems->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5587 | switch (GetElementsKind()) { |
| 5588 | case FAST_ELEMENTS: { |
| 5589 | FixedArray* old_elements = FixedArray::cast(elements()); |
| 5590 | uint32_t old_length = static_cast<uint32_t>(old_elements->length()); |
| 5591 | // Fill out the new array with this content and array holes. |
| 5592 | for (uint32_t i = 0; i < old_length; i++) { |
| 5593 | elems->set(i, old_elements->get(i), mode); |
| 5594 | } |
| 5595 | break; |
| 5596 | } |
| 5597 | case DICTIONARY_ELEMENTS: { |
| 5598 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 5599 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 5600 | Object* key = dictionary->KeyAt(i); |
| 5601 | if (key->IsNumber()) { |
| 5602 | uint32_t entry = static_cast<uint32_t>(key->Number()); |
| 5603 | elems->set(entry, dictionary->ValueAt(i), mode); |
| 5604 | } |
| 5605 | } |
| 5606 | break; |
| 5607 | } |
| 5608 | default: |
| 5609 | UNREACHABLE(); |
| 5610 | break; |
| 5611 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5612 | |
| 5613 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5614 | set_elements(elems); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5615 | |
| 5616 | if (IsJSArray()) { |
| 5617 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 5618 | } |
| 5619 | |
| 5620 | return this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | |
| 5624 | Object* JSObject::SetSlowElements(Object* len) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5625 | // We should never end in here with a pixel or external array. |
| 5626 | ASSERT(!HasPixelElements() && !HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5627 | |
| 5628 | uint32_t new_length = static_cast<uint32_t>(len->Number()); |
| 5629 | |
| 5630 | switch (GetElementsKind()) { |
| 5631 | case FAST_ELEMENTS: { |
| 5632 | // Make sure we never try to shrink dense arrays into sparse arrays. |
| 5633 | ASSERT(static_cast<uint32_t>(FixedArray::cast(elements())->length()) <= |
| 5634 | new_length); |
| 5635 | Object* obj = NormalizeElements(); |
| 5636 | if (obj->IsFailure()) return obj; |
| 5637 | |
| 5638 | // Update length for JSArrays. |
| 5639 | if (IsJSArray()) JSArray::cast(this)->set_length(len); |
| 5640 | break; |
| 5641 | } |
| 5642 | case DICTIONARY_ELEMENTS: { |
| 5643 | if (IsJSArray()) { |
| 5644 | uint32_t old_length = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5645 | static_cast<uint32_t>(JSArray::cast(this)->length()->Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5646 | element_dictionary()->RemoveNumberEntries(new_length, old_length), |
| 5647 | JSArray::cast(this)->set_length(len); |
| 5648 | } |
| 5649 | break; |
| 5650 | } |
| 5651 | default: |
| 5652 | UNREACHABLE(); |
| 5653 | break; |
| 5654 | } |
| 5655 | return this; |
| 5656 | } |
| 5657 | |
| 5658 | |
| 5659 | Object* JSArray::Initialize(int capacity) { |
| 5660 | ASSERT(capacity >= 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5661 | set_length(Smi::FromInt(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5662 | FixedArray* new_elements; |
| 5663 | if (capacity == 0) { |
| 5664 | new_elements = Heap::empty_fixed_array(); |
| 5665 | } else { |
| 5666 | Object* obj = Heap::AllocateFixedArrayWithHoles(capacity); |
| 5667 | if (obj->IsFailure()) return obj; |
| 5668 | new_elements = FixedArray::cast(obj); |
| 5669 | } |
| 5670 | set_elements(new_elements); |
| 5671 | return this; |
| 5672 | } |
| 5673 | |
| 5674 | |
| 5675 | void JSArray::Expand(int required_size) { |
| 5676 | Handle<JSArray> self(this); |
| 5677 | Handle<FixedArray> old_backing(FixedArray::cast(elements())); |
| 5678 | int old_size = old_backing->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5679 | int new_size = required_size > old_size ? required_size : old_size; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5680 | Handle<FixedArray> new_backing = Factory::NewFixedArray(new_size); |
| 5681 | // Can't use this any more now because we may have had a GC! |
| 5682 | for (int i = 0; i < old_size; i++) new_backing->set(i, old_backing->get(i)); |
| 5683 | self->SetContent(*new_backing); |
| 5684 | } |
| 5685 | |
| 5686 | |
| 5687 | // Computes the new capacity when expanding the elements of a JSObject. |
| 5688 | static int NewElementsCapacity(int old_capacity) { |
| 5689 | // (old_capacity + 50%) + 16 |
| 5690 | return old_capacity + (old_capacity >> 1) + 16; |
| 5691 | } |
| 5692 | |
| 5693 | |
| 5694 | static Object* ArrayLengthRangeError() { |
| 5695 | HandleScope scope; |
| 5696 | return Top::Throw(*Factory::NewRangeError("invalid_array_length", |
| 5697 | HandleVector<Object>(NULL, 0))); |
| 5698 | } |
| 5699 | |
| 5700 | |
| 5701 | Object* JSObject::SetElementsLength(Object* len) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5702 | // We should never end in here with a pixel or external array. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5703 | ASSERT(AllowsSetElementsLength()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5704 | |
| 5705 | Object* smi_length = len->ToSmi(); |
| 5706 | if (smi_length->IsSmi()) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5707 | const int value = Smi::cast(smi_length)->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5708 | if (value < 0) return ArrayLengthRangeError(); |
| 5709 | switch (GetElementsKind()) { |
| 5710 | case FAST_ELEMENTS: { |
| 5711 | int old_capacity = FixedArray::cast(elements())->length(); |
| 5712 | if (value <= old_capacity) { |
| 5713 | if (IsJSArray()) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5714 | Object* obj = EnsureWritableFastElements(); |
| 5715 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5716 | int old_length = FastD2I(JSArray::cast(this)->length()->Number()); |
| 5717 | // NOTE: We may be able to optimize this by removing the |
| 5718 | // last part of the elements backing storage array and |
| 5719 | // setting the capacity to the new size. |
| 5720 | for (int i = value; i < old_length; i++) { |
| 5721 | FixedArray::cast(elements())->set_the_hole(i); |
| 5722 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5723 | JSArray::cast(this)->set_length(Smi::cast(smi_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5724 | } |
| 5725 | return this; |
| 5726 | } |
| 5727 | int min = NewElementsCapacity(old_capacity); |
| 5728 | int new_capacity = value > min ? value : min; |
| 5729 | if (new_capacity <= kMaxFastElementsLength || |
| 5730 | !ShouldConvertToSlowElements(new_capacity)) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5731 | Object* obj = SetFastElementsCapacityAndLength(new_capacity, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5732 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5733 | return this; |
| 5734 | } |
| 5735 | break; |
| 5736 | } |
| 5737 | case DICTIONARY_ELEMENTS: { |
| 5738 | if (IsJSArray()) { |
| 5739 | if (value == 0) { |
| 5740 | // If the length of a slow array is reset to zero, we clear |
| 5741 | // the array and flush backing storage. This has the added |
| 5742 | // benefit that the array returns to fast mode. |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 5743 | Object* obj = ResetElements(); |
| 5744 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5745 | } else { |
| 5746 | // Remove deleted elements. |
| 5747 | uint32_t old_length = |
| 5748 | static_cast<uint32_t>(JSArray::cast(this)->length()->Number()); |
| 5749 | element_dictionary()->RemoveNumberEntries(value, old_length); |
| 5750 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5751 | JSArray::cast(this)->set_length(Smi::cast(smi_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5752 | } |
| 5753 | return this; |
| 5754 | } |
| 5755 | default: |
| 5756 | UNREACHABLE(); |
| 5757 | break; |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | // General slow case. |
| 5762 | if (len->IsNumber()) { |
| 5763 | uint32_t length; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5764 | if (len->ToArrayIndex(&length)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5765 | return SetSlowElements(len); |
| 5766 | } else { |
| 5767 | return ArrayLengthRangeError(); |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | // len is not a number so make the array size one and |
| 5772 | // set only element to len. |
| 5773 | Object* obj = Heap::AllocateFixedArray(1); |
| 5774 | if (obj->IsFailure()) return obj; |
| 5775 | FixedArray::cast(obj)->set(0, len); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5776 | if (IsJSArray()) JSArray::cast(this)->set_length(Smi::FromInt(1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5777 | set_elements(FixedArray::cast(obj)); |
| 5778 | return this; |
| 5779 | } |
| 5780 | |
| 5781 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5782 | Object* JSObject::SetPrototype(Object* value, |
| 5783 | bool skip_hidden_prototypes) { |
| 5784 | // Silently ignore the change if value is not a JSObject or null. |
| 5785 | // SpiderMonkey behaves this way. |
| 5786 | if (!value->IsJSObject() && !value->IsNull()) return value; |
| 5787 | |
| 5788 | // Before we can set the prototype we need to be sure |
| 5789 | // prototype cycles are prevented. |
| 5790 | // It is sufficient to validate that the receiver is not in the new prototype |
| 5791 | // chain. |
| 5792 | for (Object* pt = value; pt != Heap::null_value(); pt = pt->GetPrototype()) { |
| 5793 | if (JSObject::cast(pt) == this) { |
| 5794 | // Cycle detected. |
| 5795 | HandleScope scope; |
| 5796 | return Top::Throw(*Factory::NewError("cyclic_proto", |
| 5797 | HandleVector<Object>(NULL, 0))); |
| 5798 | } |
| 5799 | } |
| 5800 | |
| 5801 | JSObject* real_receiver = this; |
| 5802 | |
| 5803 | if (skip_hidden_prototypes) { |
| 5804 | // Find the first object in the chain whose prototype object is not |
| 5805 | // hidden and set the new prototype on that object. |
| 5806 | Object* current_proto = real_receiver->GetPrototype(); |
| 5807 | while (current_proto->IsJSObject() && |
| 5808 | JSObject::cast(current_proto)->map()->is_hidden_prototype()) { |
| 5809 | real_receiver = JSObject::cast(current_proto); |
| 5810 | current_proto = current_proto->GetPrototype(); |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | // Set the new prototype of the object. |
| 5815 | Object* new_map = real_receiver->map()->CopyDropTransitions(); |
| 5816 | if (new_map->IsFailure()) return new_map; |
| 5817 | Map::cast(new_map)->set_prototype(value); |
| 5818 | real_receiver->set_map(Map::cast(new_map)); |
| 5819 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 5820 | Heap::ClearInstanceofCache(); |
| 5821 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5822 | return value; |
| 5823 | } |
| 5824 | |
| 5825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5826 | bool JSObject::HasElementPostInterceptor(JSObject* receiver, uint32_t index) { |
| 5827 | switch (GetElementsKind()) { |
| 5828 | case FAST_ELEMENTS: { |
| 5829 | uint32_t length = IsJSArray() ? |
| 5830 | static_cast<uint32_t> |
| 5831 | (Smi::cast(JSArray::cast(this)->length())->value()) : |
| 5832 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 5833 | if ((index < length) && |
| 5834 | !FixedArray::cast(elements())->get(index)->IsTheHole()) { |
| 5835 | return true; |
| 5836 | } |
| 5837 | break; |
| 5838 | } |
| 5839 | case PIXEL_ELEMENTS: { |
| 5840 | // TODO(iposva): Add testcase. |
| 5841 | PixelArray* pixels = PixelArray::cast(elements()); |
| 5842 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 5843 | return true; |
| 5844 | } |
| 5845 | break; |
| 5846 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5847 | case EXTERNAL_BYTE_ELEMENTS: |
| 5848 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5849 | case EXTERNAL_SHORT_ELEMENTS: |
| 5850 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5851 | case EXTERNAL_INT_ELEMENTS: |
| 5852 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5853 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 5854 | // TODO(kbr): Add testcase. |
| 5855 | ExternalArray* array = ExternalArray::cast(elements()); |
| 5856 | if (index < static_cast<uint32_t>(array->length())) { |
| 5857 | return true; |
| 5858 | } |
| 5859 | break; |
| 5860 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5861 | case DICTIONARY_ELEMENTS: { |
| 5862 | if (element_dictionary()->FindEntry(index) |
| 5863 | != NumberDictionary::kNotFound) { |
| 5864 | return true; |
| 5865 | } |
| 5866 | break; |
| 5867 | } |
| 5868 | default: |
| 5869 | UNREACHABLE(); |
| 5870 | break; |
| 5871 | } |
| 5872 | |
| 5873 | // Handle [] on String objects. |
| 5874 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 5875 | |
| 5876 | Object* pt = GetPrototype(); |
| 5877 | if (pt == Heap::null_value()) return false; |
| 5878 | return JSObject::cast(pt)->HasElementWithReceiver(receiver, index); |
| 5879 | } |
| 5880 | |
| 5881 | |
| 5882 | bool JSObject::HasElementWithInterceptor(JSObject* receiver, uint32_t index) { |
| 5883 | // Make sure that the top context does not change when doing |
| 5884 | // callbacks or interceptor calls. |
| 5885 | AssertNoContextChange ncc; |
| 5886 | HandleScope scope; |
| 5887 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 5888 | Handle<JSObject> receiver_handle(receiver); |
| 5889 | Handle<JSObject> holder_handle(this); |
| 5890 | CustomArguments args(interceptor->data(), receiver, this); |
| 5891 | v8::AccessorInfo info(args.end()); |
| 5892 | if (!interceptor->query()->IsUndefined()) { |
| 5893 | v8::IndexedPropertyQuery query = |
| 5894 | v8::ToCData<v8::IndexedPropertyQuery>(interceptor->query()); |
| 5895 | LOG(ApiIndexedPropertyAccess("interceptor-indexed-has", this, index)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5896 | v8::Handle<v8::Integer> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5897 | { |
| 5898 | // Leaving JavaScript. |
| 5899 | VMState state(EXTERNAL); |
| 5900 | result = query(index, info); |
| 5901 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5902 | if (!result.IsEmpty()) { |
| 5903 | ASSERT(result->IsInt32()); |
| 5904 | return true; // absence of property is signaled by empty handle. |
| 5905 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5906 | } else if (!interceptor->getter()->IsUndefined()) { |
| 5907 | v8::IndexedPropertyGetter getter = |
| 5908 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
| 5909 | LOG(ApiIndexedPropertyAccess("interceptor-indexed-has-get", this, index)); |
| 5910 | v8::Handle<v8::Value> result; |
| 5911 | { |
| 5912 | // Leaving JavaScript. |
| 5913 | VMState state(EXTERNAL); |
| 5914 | result = getter(index, info); |
| 5915 | } |
| 5916 | if (!result.IsEmpty()) return true; |
| 5917 | } |
| 5918 | return holder_handle->HasElementPostInterceptor(*receiver_handle, index); |
| 5919 | } |
| 5920 | |
| 5921 | |
| 5922 | bool JSObject::HasLocalElement(uint32_t index) { |
| 5923 | // Check access rights if needed. |
| 5924 | if (IsAccessCheckNeeded() && |
| 5925 | !Top::MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 5926 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 5927 | return false; |
| 5928 | } |
| 5929 | |
| 5930 | // Check for lookup interceptor |
| 5931 | if (HasIndexedInterceptor()) { |
| 5932 | return HasElementWithInterceptor(this, index); |
| 5933 | } |
| 5934 | |
| 5935 | // Handle [] on String objects. |
| 5936 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 5937 | |
| 5938 | switch (GetElementsKind()) { |
| 5939 | case FAST_ELEMENTS: { |
| 5940 | uint32_t length = IsJSArray() ? |
| 5941 | static_cast<uint32_t> |
| 5942 | (Smi::cast(JSArray::cast(this)->length())->value()) : |
| 5943 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 5944 | return (index < length) && |
| 5945 | !FixedArray::cast(elements())->get(index)->IsTheHole(); |
| 5946 | } |
| 5947 | case PIXEL_ELEMENTS: { |
| 5948 | PixelArray* pixels = PixelArray::cast(elements()); |
| 5949 | return (index < static_cast<uint32_t>(pixels->length())); |
| 5950 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5951 | case EXTERNAL_BYTE_ELEMENTS: |
| 5952 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 5953 | case EXTERNAL_SHORT_ELEMENTS: |
| 5954 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 5955 | case EXTERNAL_INT_ELEMENTS: |
| 5956 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 5957 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 5958 | ExternalArray* array = ExternalArray::cast(elements()); |
| 5959 | return (index < static_cast<uint32_t>(array->length())); |
| 5960 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5961 | case DICTIONARY_ELEMENTS: { |
| 5962 | return element_dictionary()->FindEntry(index) |
| 5963 | != NumberDictionary::kNotFound; |
| 5964 | } |
| 5965 | default: |
| 5966 | UNREACHABLE(); |
| 5967 | break; |
| 5968 | } |
| 5969 | UNREACHABLE(); |
| 5970 | return Heap::null_value(); |
| 5971 | } |
| 5972 | |
| 5973 | |
| 5974 | bool JSObject::HasElementWithReceiver(JSObject* receiver, uint32_t index) { |
| 5975 | // Check access rights if needed. |
| 5976 | if (IsAccessCheckNeeded() && |
| 5977 | !Top::MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 5978 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 5979 | return false; |
| 5980 | } |
| 5981 | |
| 5982 | // Check for lookup interceptor |
| 5983 | if (HasIndexedInterceptor()) { |
| 5984 | return HasElementWithInterceptor(receiver, index); |
| 5985 | } |
| 5986 | |
| 5987 | switch (GetElementsKind()) { |
| 5988 | case FAST_ELEMENTS: { |
| 5989 | uint32_t length = IsJSArray() ? |
| 5990 | static_cast<uint32_t> |
| 5991 | (Smi::cast(JSArray::cast(this)->length())->value()) : |
| 5992 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 5993 | if ((index < length) && |
| 5994 | !FixedArray::cast(elements())->get(index)->IsTheHole()) return true; |
| 5995 | break; |
| 5996 | } |
| 5997 | case PIXEL_ELEMENTS: { |
| 5998 | PixelArray* pixels = PixelArray::cast(elements()); |
| 5999 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 6000 | return true; |
| 6001 | } |
| 6002 | break; |
| 6003 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6004 | case EXTERNAL_BYTE_ELEMENTS: |
| 6005 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6006 | case EXTERNAL_SHORT_ELEMENTS: |
| 6007 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6008 | case EXTERNAL_INT_ELEMENTS: |
| 6009 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6010 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 6011 | ExternalArray* array = ExternalArray::cast(elements()); |
| 6012 | if (index < static_cast<uint32_t>(array->length())) { |
| 6013 | return true; |
| 6014 | } |
| 6015 | break; |
| 6016 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6017 | case DICTIONARY_ELEMENTS: { |
| 6018 | if (element_dictionary()->FindEntry(index) |
| 6019 | != NumberDictionary::kNotFound) { |
| 6020 | return true; |
| 6021 | } |
| 6022 | break; |
| 6023 | } |
| 6024 | default: |
| 6025 | UNREACHABLE(); |
| 6026 | break; |
| 6027 | } |
| 6028 | |
| 6029 | // Handle [] on String objects. |
| 6030 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 6031 | |
| 6032 | Object* pt = GetPrototype(); |
| 6033 | if (pt == Heap::null_value()) return false; |
| 6034 | return JSObject::cast(pt)->HasElementWithReceiver(receiver, index); |
| 6035 | } |
| 6036 | |
| 6037 | |
| 6038 | Object* JSObject::SetElementWithInterceptor(uint32_t index, Object* value) { |
| 6039 | // Make sure that the top context does not change when doing |
| 6040 | // callbacks or interceptor calls. |
| 6041 | AssertNoContextChange ncc; |
| 6042 | HandleScope scope; |
| 6043 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 6044 | Handle<JSObject> this_handle(this); |
| 6045 | Handle<Object> value_handle(value); |
| 6046 | if (!interceptor->setter()->IsUndefined()) { |
| 6047 | v8::IndexedPropertySetter setter = |
| 6048 | v8::ToCData<v8::IndexedPropertySetter>(interceptor->setter()); |
| 6049 | LOG(ApiIndexedPropertyAccess("interceptor-indexed-set", this, index)); |
| 6050 | CustomArguments args(interceptor->data(), this, this); |
| 6051 | v8::AccessorInfo info(args.end()); |
| 6052 | v8::Handle<v8::Value> result; |
| 6053 | { |
| 6054 | // Leaving JavaScript. |
| 6055 | VMState state(EXTERNAL); |
| 6056 | result = setter(index, v8::Utils::ToLocal(value_handle), info); |
| 6057 | } |
| 6058 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6059 | if (!result.IsEmpty()) return *value_handle; |
| 6060 | } |
| 6061 | Object* raw_result = |
| 6062 | this_handle->SetElementWithoutInterceptor(index, *value_handle); |
| 6063 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6064 | return raw_result; |
| 6065 | } |
| 6066 | |
| 6067 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6068 | Object* JSObject::GetElementWithCallback(Object* receiver, |
| 6069 | Object* structure, |
| 6070 | uint32_t index, |
| 6071 | Object* holder) { |
| 6072 | ASSERT(!structure->IsProxy()); |
| 6073 | |
| 6074 | // api style callbacks. |
| 6075 | if (structure->IsAccessorInfo()) { |
| 6076 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 6077 | Object* fun_obj = data->getter(); |
| 6078 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
| 6079 | HandleScope scope; |
| 6080 | Handle<JSObject> self(JSObject::cast(receiver)); |
| 6081 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
| 6082 | Handle<Object> number = Factory::NewNumberFromUint(index); |
| 6083 | Handle<String> key(Factory::NumberToString(number)); |
| 6084 | LOG(ApiNamedPropertyAccess("load", *self, *key)); |
| 6085 | CustomArguments args(data->data(), *self, *holder_handle); |
| 6086 | v8::AccessorInfo info(args.end()); |
| 6087 | v8::Handle<v8::Value> result; |
| 6088 | { |
| 6089 | // Leaving JavaScript. |
| 6090 | VMState state(EXTERNAL); |
| 6091 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 6092 | } |
| 6093 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6094 | if (result.IsEmpty()) return Heap::undefined_value(); |
| 6095 | return *v8::Utils::OpenHandle(*result); |
| 6096 | } |
| 6097 | |
| 6098 | // __defineGetter__ callback |
| 6099 | if (structure->IsFixedArray()) { |
| 6100 | Object* getter = FixedArray::cast(structure)->get(kGetterIndex); |
| 6101 | if (getter->IsJSFunction()) { |
| 6102 | return Object::GetPropertyWithDefinedGetter(receiver, |
| 6103 | JSFunction::cast(getter)); |
| 6104 | } |
| 6105 | // Getter is not a function. |
| 6106 | return Heap::undefined_value(); |
| 6107 | } |
| 6108 | |
| 6109 | UNREACHABLE(); |
| 6110 | return NULL; |
| 6111 | } |
| 6112 | |
| 6113 | |
| 6114 | Object* JSObject::SetElementWithCallback(Object* structure, |
| 6115 | uint32_t index, |
| 6116 | Object* value, |
| 6117 | JSObject* holder) { |
| 6118 | HandleScope scope; |
| 6119 | |
| 6120 | // We should never get here to initialize a const with the hole |
| 6121 | // value since a const declaration would conflict with the setter. |
| 6122 | ASSERT(!value->IsTheHole()); |
| 6123 | Handle<Object> value_handle(value); |
| 6124 | |
| 6125 | // To accommodate both the old and the new api we switch on the |
| 6126 | // data structure used to store the callbacks. Eventually proxy |
| 6127 | // callbacks should be phased out. |
| 6128 | ASSERT(!structure->IsProxy()); |
| 6129 | |
| 6130 | if (structure->IsAccessorInfo()) { |
| 6131 | // api style callbacks |
| 6132 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 6133 | Object* call_obj = data->setter(); |
| 6134 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 6135 | if (call_fun == NULL) return value; |
| 6136 | Handle<Object> number = Factory::NewNumberFromUint(index); |
| 6137 | Handle<String> key(Factory::NumberToString(number)); |
| 6138 | LOG(ApiNamedPropertyAccess("store", this, *key)); |
| 6139 | CustomArguments args(data->data(), this, JSObject::cast(holder)); |
| 6140 | v8::AccessorInfo info(args.end()); |
| 6141 | { |
| 6142 | // Leaving JavaScript. |
| 6143 | VMState state(EXTERNAL); |
| 6144 | call_fun(v8::Utils::ToLocal(key), |
| 6145 | v8::Utils::ToLocal(value_handle), |
| 6146 | info); |
| 6147 | } |
| 6148 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6149 | return *value_handle; |
| 6150 | } |
| 6151 | |
| 6152 | if (structure->IsFixedArray()) { |
| 6153 | Object* setter = FixedArray::cast(structure)->get(kSetterIndex); |
| 6154 | if (setter->IsJSFunction()) { |
| 6155 | return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); |
| 6156 | } else { |
| 6157 | Handle<Object> holder_handle(holder); |
| 6158 | Handle<Object> key(Factory::NewNumberFromUint(index)); |
| 6159 | Handle<Object> args[2] = { key, holder_handle }; |
| 6160 | return Top::Throw(*Factory::NewTypeError("no_setter_in_callback", |
| 6161 | HandleVector(args, 2))); |
| 6162 | } |
| 6163 | } |
| 6164 | |
| 6165 | UNREACHABLE(); |
| 6166 | return NULL; |
| 6167 | } |
| 6168 | |
| 6169 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6170 | // Adding n elements in fast case is O(n*n). |
| 6171 | // Note: revisit design to have dual undefined values to capture absent |
| 6172 | // elements. |
| 6173 | Object* JSObject::SetFastElement(uint32_t index, Object* value) { |
| 6174 | ASSERT(HasFastElements()); |
| 6175 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 6176 | Object* elms_obj = EnsureWritableFastElements(); |
| 6177 | if (elms_obj->IsFailure()) return elms_obj; |
| 6178 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6179 | uint32_t elms_length = static_cast<uint32_t>(elms->length()); |
| 6180 | |
| 6181 | if (!IsJSArray() && (index >= elms_length || elms->get(index)->IsTheHole())) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6182 | if (SetElementWithCallbackSetterInPrototypes(index, value)) { |
| 6183 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6184 | } |
| 6185 | } |
| 6186 | |
| 6187 | // Check whether there is extra space in fixed array.. |
| 6188 | if (index < elms_length) { |
| 6189 | elms->set(index, value); |
| 6190 | if (IsJSArray()) { |
| 6191 | // Update the length of the array if needed. |
| 6192 | uint32_t array_length = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6193 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6194 | if (index >= array_length) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6195 | JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6196 | } |
| 6197 | } |
| 6198 | return value; |
| 6199 | } |
| 6200 | |
| 6201 | // Allow gap in fast case. |
| 6202 | if ((index - elms_length) < kMaxGap) { |
| 6203 | // Try allocating extra space. |
| 6204 | int new_capacity = NewElementsCapacity(index+1); |
| 6205 | if (new_capacity <= kMaxFastElementsLength || |
| 6206 | !ShouldConvertToSlowElements(new_capacity)) { |
| 6207 | ASSERT(static_cast<uint32_t>(new_capacity) > index); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6208 | Object* obj = SetFastElementsCapacityAndLength(new_capacity, index + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6209 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6210 | FixedArray::cast(elements())->set(index, value); |
| 6211 | return value; |
| 6212 | } |
| 6213 | } |
| 6214 | |
| 6215 | // Otherwise default to slow case. |
| 6216 | Object* obj = NormalizeElements(); |
| 6217 | if (obj->IsFailure()) return obj; |
| 6218 | ASSERT(HasDictionaryElements()); |
| 6219 | return SetElement(index, value); |
| 6220 | } |
| 6221 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 6222 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6223 | Object* JSObject::SetElement(uint32_t index, Object* value) { |
| 6224 | // Check access rights if needed. |
| 6225 | if (IsAccessCheckNeeded() && |
| 6226 | !Top::MayIndexedAccess(this, index, v8::ACCESS_SET)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 6227 | HandleScope scope; |
| 6228 | Handle<Object> value_handle(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6229 | Top::ReportFailedAccessCheck(this, v8::ACCESS_SET); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 6230 | return *value_handle; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | if (IsJSGlobalProxy()) { |
| 6234 | Object* proto = GetPrototype(); |
| 6235 | if (proto->IsNull()) return value; |
| 6236 | ASSERT(proto->IsJSGlobalObject()); |
| 6237 | return JSObject::cast(proto)->SetElement(index, value); |
| 6238 | } |
| 6239 | |
| 6240 | // Check for lookup interceptor |
| 6241 | if (HasIndexedInterceptor()) { |
| 6242 | return SetElementWithInterceptor(index, value); |
| 6243 | } |
| 6244 | |
| 6245 | return SetElementWithoutInterceptor(index, value); |
| 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | Object* JSObject::SetElementWithoutInterceptor(uint32_t index, Object* value) { |
| 6250 | switch (GetElementsKind()) { |
| 6251 | case FAST_ELEMENTS: |
| 6252 | // Fast case. |
| 6253 | return SetFastElement(index, value); |
| 6254 | case PIXEL_ELEMENTS: { |
| 6255 | PixelArray* pixels = PixelArray::cast(elements()); |
| 6256 | return pixels->SetValue(index, value); |
| 6257 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6258 | case EXTERNAL_BYTE_ELEMENTS: { |
| 6259 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 6260 | return array->SetValue(index, value); |
| 6261 | } |
| 6262 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 6263 | ExternalUnsignedByteArray* array = |
| 6264 | ExternalUnsignedByteArray::cast(elements()); |
| 6265 | return array->SetValue(index, value); |
| 6266 | } |
| 6267 | case EXTERNAL_SHORT_ELEMENTS: { |
| 6268 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 6269 | return array->SetValue(index, value); |
| 6270 | } |
| 6271 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 6272 | ExternalUnsignedShortArray* array = |
| 6273 | ExternalUnsignedShortArray::cast(elements()); |
| 6274 | return array->SetValue(index, value); |
| 6275 | } |
| 6276 | case EXTERNAL_INT_ELEMENTS: { |
| 6277 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 6278 | return array->SetValue(index, value); |
| 6279 | } |
| 6280 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 6281 | ExternalUnsignedIntArray* array = |
| 6282 | ExternalUnsignedIntArray::cast(elements()); |
| 6283 | return array->SetValue(index, value); |
| 6284 | } |
| 6285 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 6286 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 6287 | return array->SetValue(index, value); |
| 6288 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6289 | case DICTIONARY_ELEMENTS: { |
| 6290 | // Insert element in the dictionary. |
| 6291 | FixedArray* elms = FixedArray::cast(elements()); |
| 6292 | NumberDictionary* dictionary = NumberDictionary::cast(elms); |
| 6293 | |
| 6294 | int entry = dictionary->FindEntry(index); |
| 6295 | if (entry != NumberDictionary::kNotFound) { |
| 6296 | Object* element = dictionary->ValueAt(entry); |
| 6297 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 6298 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6299 | return SetElementWithCallback(element, index, value, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6300 | } else { |
| 6301 | dictionary->UpdateMaxNumberKey(index); |
| 6302 | dictionary->ValueAtPut(entry, value); |
| 6303 | } |
| 6304 | } else { |
| 6305 | // Index not already used. Look for an accessor in the prototype chain. |
| 6306 | if (!IsJSArray()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6307 | if (SetElementWithCallbackSetterInPrototypes(index, value)) { |
| 6308 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6309 | } |
| 6310 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6311 | // When we set the is_extensible flag to false we always force |
| 6312 | // the element into dictionary mode (and force them to stay there). |
| 6313 | if (!map()->is_extensible()) { |
| 6314 | Handle<Object> number(Heap::NumberFromUint32(index)); |
| 6315 | Handle<String> index_string(Factory::NumberToString(number)); |
| 6316 | Handle<Object> args[1] = { index_string }; |
| 6317 | return Top::Throw(*Factory::NewTypeError("object_not_extensible", |
| 6318 | HandleVector(args, 1))); |
| 6319 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6320 | Object* result = dictionary->AtNumberPut(index, value); |
| 6321 | if (result->IsFailure()) return result; |
| 6322 | if (elms != FixedArray::cast(result)) { |
| 6323 | set_elements(FixedArray::cast(result)); |
| 6324 | } |
| 6325 | } |
| 6326 | |
| 6327 | // Update the array length if this JSObject is an array. |
| 6328 | if (IsJSArray()) { |
| 6329 | JSArray* array = JSArray::cast(this); |
| 6330 | Object* return_value = array->JSArrayUpdateLengthFromIndex(index, |
| 6331 | value); |
| 6332 | if (return_value->IsFailure()) return return_value; |
| 6333 | } |
| 6334 | |
| 6335 | // Attempt to put this object back in fast case. |
| 6336 | if (ShouldConvertToFastElements()) { |
| 6337 | uint32_t new_length = 0; |
| 6338 | if (IsJSArray()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6339 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6340 | } else { |
| 6341 | new_length = NumberDictionary::cast(elements())->max_number_key() + 1; |
| 6342 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6343 | Object* obj = SetFastElementsCapacityAndLength(new_length, new_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6344 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6345 | #ifdef DEBUG |
| 6346 | if (FLAG_trace_normalization) { |
| 6347 | PrintF("Object elements are fast case again:\n"); |
| 6348 | Print(); |
| 6349 | } |
| 6350 | #endif |
| 6351 | } |
| 6352 | |
| 6353 | return value; |
| 6354 | } |
| 6355 | default: |
| 6356 | UNREACHABLE(); |
| 6357 | break; |
| 6358 | } |
| 6359 | // All possible cases have been handled above. Add a return to avoid the |
| 6360 | // complaints from the compiler. |
| 6361 | UNREACHABLE(); |
| 6362 | return Heap::null_value(); |
| 6363 | } |
| 6364 | |
| 6365 | |
| 6366 | Object* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, Object* value) { |
| 6367 | uint32_t old_len = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6368 | CHECK(length()->ToArrayIndex(&old_len)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6369 | // Check to see if we need to update the length. For now, we make |
| 6370 | // sure that the length stays within 32-bits (unsigned). |
| 6371 | if (index >= old_len && index != 0xffffffff) { |
| 6372 | Object* len = |
| 6373 | Heap::NumberFromDouble(static_cast<double>(index) + 1); |
| 6374 | if (len->IsFailure()) return len; |
| 6375 | set_length(len); |
| 6376 | } |
| 6377 | return value; |
| 6378 | } |
| 6379 | |
| 6380 | |
| 6381 | Object* JSObject::GetElementPostInterceptor(JSObject* receiver, |
| 6382 | uint32_t index) { |
| 6383 | // Get element works for both JSObject and JSArray since |
| 6384 | // JSArray::length cannot change. |
| 6385 | switch (GetElementsKind()) { |
| 6386 | case FAST_ELEMENTS: { |
| 6387 | FixedArray* elms = FixedArray::cast(elements()); |
| 6388 | if (index < static_cast<uint32_t>(elms->length())) { |
| 6389 | Object* value = elms->get(index); |
| 6390 | if (!value->IsTheHole()) return value; |
| 6391 | } |
| 6392 | break; |
| 6393 | } |
| 6394 | case PIXEL_ELEMENTS: { |
| 6395 | // TODO(iposva): Add testcase and implement. |
| 6396 | UNIMPLEMENTED(); |
| 6397 | break; |
| 6398 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6399 | case EXTERNAL_BYTE_ELEMENTS: |
| 6400 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6401 | case EXTERNAL_SHORT_ELEMENTS: |
| 6402 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6403 | case EXTERNAL_INT_ELEMENTS: |
| 6404 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6405 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 6406 | // TODO(kbr): Add testcase and implement. |
| 6407 | UNIMPLEMENTED(); |
| 6408 | break; |
| 6409 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6410 | case DICTIONARY_ELEMENTS: { |
| 6411 | NumberDictionary* dictionary = element_dictionary(); |
| 6412 | int entry = dictionary->FindEntry(index); |
| 6413 | if (entry != NumberDictionary::kNotFound) { |
| 6414 | Object* element = dictionary->ValueAt(entry); |
| 6415 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 6416 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6417 | return GetElementWithCallback(receiver, |
| 6418 | element, |
| 6419 | index, |
| 6420 | this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6421 | } |
| 6422 | return element; |
| 6423 | } |
| 6424 | break; |
| 6425 | } |
| 6426 | default: |
| 6427 | UNREACHABLE(); |
| 6428 | break; |
| 6429 | } |
| 6430 | |
| 6431 | // Continue searching via the prototype chain. |
| 6432 | Object* pt = GetPrototype(); |
| 6433 | if (pt == Heap::null_value()) return Heap::undefined_value(); |
| 6434 | return pt->GetElementWithReceiver(receiver, index); |
| 6435 | } |
| 6436 | |
| 6437 | |
| 6438 | Object* JSObject::GetElementWithInterceptor(JSObject* receiver, |
| 6439 | uint32_t index) { |
| 6440 | // Make sure that the top context does not change when doing |
| 6441 | // callbacks or interceptor calls. |
| 6442 | AssertNoContextChange ncc; |
| 6443 | HandleScope scope; |
| 6444 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 6445 | Handle<JSObject> this_handle(receiver); |
| 6446 | Handle<JSObject> holder_handle(this); |
| 6447 | |
| 6448 | if (!interceptor->getter()->IsUndefined()) { |
| 6449 | v8::IndexedPropertyGetter getter = |
| 6450 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
| 6451 | LOG(ApiIndexedPropertyAccess("interceptor-indexed-get", this, index)); |
| 6452 | CustomArguments args(interceptor->data(), receiver, this); |
| 6453 | v8::AccessorInfo info(args.end()); |
| 6454 | v8::Handle<v8::Value> result; |
| 6455 | { |
| 6456 | // Leaving JavaScript. |
| 6457 | VMState state(EXTERNAL); |
| 6458 | result = getter(index, info); |
| 6459 | } |
| 6460 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6461 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 6462 | } |
| 6463 | |
| 6464 | Object* raw_result = |
| 6465 | holder_handle->GetElementPostInterceptor(*this_handle, index); |
| 6466 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6467 | return raw_result; |
| 6468 | } |
| 6469 | |
| 6470 | |
| 6471 | Object* JSObject::GetElementWithReceiver(JSObject* receiver, uint32_t index) { |
| 6472 | // Check access rights if needed. |
| 6473 | if (IsAccessCheckNeeded() && |
| 6474 | !Top::MayIndexedAccess(this, index, v8::ACCESS_GET)) { |
| 6475 | Top::ReportFailedAccessCheck(this, v8::ACCESS_GET); |
| 6476 | return Heap::undefined_value(); |
| 6477 | } |
| 6478 | |
| 6479 | if (HasIndexedInterceptor()) { |
| 6480 | return GetElementWithInterceptor(receiver, index); |
| 6481 | } |
| 6482 | |
| 6483 | // Get element works for both JSObject and JSArray since |
| 6484 | // JSArray::length cannot change. |
| 6485 | switch (GetElementsKind()) { |
| 6486 | case FAST_ELEMENTS: { |
| 6487 | FixedArray* elms = FixedArray::cast(elements()); |
| 6488 | if (index < static_cast<uint32_t>(elms->length())) { |
| 6489 | Object* value = elms->get(index); |
| 6490 | if (!value->IsTheHole()) return value; |
| 6491 | } |
| 6492 | break; |
| 6493 | } |
| 6494 | case PIXEL_ELEMENTS: { |
| 6495 | PixelArray* pixels = PixelArray::cast(elements()); |
| 6496 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 6497 | uint8_t value = pixels->get(index); |
| 6498 | return Smi::FromInt(value); |
| 6499 | } |
| 6500 | break; |
| 6501 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6502 | case EXTERNAL_BYTE_ELEMENTS: { |
| 6503 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 6504 | if (index < static_cast<uint32_t>(array->length())) { |
| 6505 | int8_t value = array->get(index); |
| 6506 | return Smi::FromInt(value); |
| 6507 | } |
| 6508 | break; |
| 6509 | } |
| 6510 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 6511 | ExternalUnsignedByteArray* array = |
| 6512 | ExternalUnsignedByteArray::cast(elements()); |
| 6513 | if (index < static_cast<uint32_t>(array->length())) { |
| 6514 | uint8_t value = array->get(index); |
| 6515 | return Smi::FromInt(value); |
| 6516 | } |
| 6517 | break; |
| 6518 | } |
| 6519 | case EXTERNAL_SHORT_ELEMENTS: { |
| 6520 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 6521 | if (index < static_cast<uint32_t>(array->length())) { |
| 6522 | int16_t value = array->get(index); |
| 6523 | return Smi::FromInt(value); |
| 6524 | } |
| 6525 | break; |
| 6526 | } |
| 6527 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 6528 | ExternalUnsignedShortArray* array = |
| 6529 | ExternalUnsignedShortArray::cast(elements()); |
| 6530 | if (index < static_cast<uint32_t>(array->length())) { |
| 6531 | uint16_t value = array->get(index); |
| 6532 | return Smi::FromInt(value); |
| 6533 | } |
| 6534 | break; |
| 6535 | } |
| 6536 | case EXTERNAL_INT_ELEMENTS: { |
| 6537 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 6538 | if (index < static_cast<uint32_t>(array->length())) { |
| 6539 | int32_t value = array->get(index); |
| 6540 | return Heap::NumberFromInt32(value); |
| 6541 | } |
| 6542 | break; |
| 6543 | } |
| 6544 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 6545 | ExternalUnsignedIntArray* array = |
| 6546 | ExternalUnsignedIntArray::cast(elements()); |
| 6547 | if (index < static_cast<uint32_t>(array->length())) { |
| 6548 | uint32_t value = array->get(index); |
| 6549 | return Heap::NumberFromUint32(value); |
| 6550 | } |
| 6551 | break; |
| 6552 | } |
| 6553 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 6554 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 6555 | if (index < static_cast<uint32_t>(array->length())) { |
| 6556 | float value = array->get(index); |
| 6557 | return Heap::AllocateHeapNumber(value); |
| 6558 | } |
| 6559 | break; |
| 6560 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6561 | case DICTIONARY_ELEMENTS: { |
| 6562 | NumberDictionary* dictionary = element_dictionary(); |
| 6563 | int entry = dictionary->FindEntry(index); |
| 6564 | if (entry != NumberDictionary::kNotFound) { |
| 6565 | Object* element = dictionary->ValueAt(entry); |
| 6566 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 6567 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6568 | return GetElementWithCallback(receiver, |
| 6569 | element, |
| 6570 | index, |
| 6571 | this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6572 | } |
| 6573 | return element; |
| 6574 | } |
| 6575 | break; |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | Object* pt = GetPrototype(); |
| 6580 | if (pt == Heap::null_value()) return Heap::undefined_value(); |
| 6581 | return pt->GetElementWithReceiver(receiver, index); |
| 6582 | } |
| 6583 | |
| 6584 | |
| 6585 | bool JSObject::HasDenseElements() { |
| 6586 | int capacity = 0; |
| 6587 | int number_of_elements = 0; |
| 6588 | |
| 6589 | switch (GetElementsKind()) { |
| 6590 | case FAST_ELEMENTS: { |
| 6591 | FixedArray* elms = FixedArray::cast(elements()); |
| 6592 | capacity = elms->length(); |
| 6593 | for (int i = 0; i < capacity; i++) { |
| 6594 | if (!elms->get(i)->IsTheHole()) number_of_elements++; |
| 6595 | } |
| 6596 | break; |
| 6597 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6598 | case PIXEL_ELEMENTS: |
| 6599 | case EXTERNAL_BYTE_ELEMENTS: |
| 6600 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6601 | case EXTERNAL_SHORT_ELEMENTS: |
| 6602 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6603 | case EXTERNAL_INT_ELEMENTS: |
| 6604 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6605 | case EXTERNAL_FLOAT_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6606 | return true; |
| 6607 | } |
| 6608 | case DICTIONARY_ELEMENTS: { |
| 6609 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 6610 | capacity = dictionary->Capacity(); |
| 6611 | number_of_elements = dictionary->NumberOfElements(); |
| 6612 | break; |
| 6613 | } |
| 6614 | default: |
| 6615 | UNREACHABLE(); |
| 6616 | break; |
| 6617 | } |
| 6618 | |
| 6619 | if (capacity == 0) return true; |
| 6620 | return (number_of_elements > (capacity / 2)); |
| 6621 | } |
| 6622 | |
| 6623 | |
| 6624 | bool JSObject::ShouldConvertToSlowElements(int new_capacity) { |
| 6625 | ASSERT(HasFastElements()); |
| 6626 | // Keep the array in fast case if the current backing storage is |
| 6627 | // almost filled and if the new capacity is no more than twice the |
| 6628 | // old capacity. |
| 6629 | int elements_length = FixedArray::cast(elements())->length(); |
| 6630 | return !HasDenseElements() || ((new_capacity / 2) > elements_length); |
| 6631 | } |
| 6632 | |
| 6633 | |
| 6634 | bool JSObject::ShouldConvertToFastElements() { |
| 6635 | ASSERT(HasDictionaryElements()); |
| 6636 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 6637 | // If the elements are sparse, we should not go back to fast case. |
| 6638 | if (!HasDenseElements()) return false; |
| 6639 | // If an element has been added at a very high index in the elements |
| 6640 | // dictionary, we cannot go back to fast case. |
| 6641 | if (dictionary->requires_slow_elements()) return false; |
| 6642 | // An object requiring access checks is never allowed to have fast |
| 6643 | // elements. If it had fast elements we would skip security checks. |
| 6644 | if (IsAccessCheckNeeded()) return false; |
| 6645 | // If the dictionary backing storage takes up roughly half as much |
| 6646 | // space as a fast-case backing storage would the array should have |
| 6647 | // fast elements. |
| 6648 | uint32_t length = 0; |
| 6649 | if (IsJSArray()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6650 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6651 | } else { |
| 6652 | length = dictionary->max_number_key(); |
| 6653 | } |
| 6654 | return static_cast<uint32_t>(dictionary->Capacity()) >= |
| 6655 | (length / (2 * NumberDictionary::kEntrySize)); |
| 6656 | } |
| 6657 | |
| 6658 | |
| 6659 | // Certain compilers request function template instantiation when they |
| 6660 | // see the definition of the other template functions in the |
| 6661 | // class. This requires us to have the template functions put |
| 6662 | // together, so even though this function belongs in objects-debug.cc, |
| 6663 | // we keep it here instead to satisfy certain compilers. |
| 6664 | #ifdef DEBUG |
| 6665 | template<typename Shape, typename Key> |
| 6666 | void Dictionary<Shape, Key>::Print() { |
| 6667 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 6668 | for (int i = 0; i < capacity; i++) { |
| 6669 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 6670 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 6671 | PrintF(" "); |
| 6672 | if (k->IsString()) { |
| 6673 | String::cast(k)->StringPrint(); |
| 6674 | } else { |
| 6675 | k->ShortPrint(); |
| 6676 | } |
| 6677 | PrintF(": "); |
| 6678 | ValueAt(i)->ShortPrint(); |
| 6679 | PrintF("\n"); |
| 6680 | } |
| 6681 | } |
| 6682 | } |
| 6683 | #endif |
| 6684 | |
| 6685 | |
| 6686 | template<typename Shape, typename Key> |
| 6687 | void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) { |
| 6688 | int pos = 0; |
| 6689 | int capacity = HashTable<Shape, Key>::Capacity(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6690 | AssertNoAllocation no_gc; |
| 6691 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6692 | for (int i = 0; i < capacity; i++) { |
| 6693 | Object* k = Dictionary<Shape, Key>::KeyAt(i); |
| 6694 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 6695 | elements->set(pos++, ValueAt(i), mode); |
| 6696 | } |
| 6697 | } |
| 6698 | ASSERT(pos == elements->length()); |
| 6699 | } |
| 6700 | |
| 6701 | |
| 6702 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
| 6703 | ASSERT(map()->has_named_interceptor()); |
| 6704 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6705 | ASSERT(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6706 | Object* result = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6707 | constructor->shared()->get_api_func_data()->named_property_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6708 | return InterceptorInfo::cast(result); |
| 6709 | } |
| 6710 | |
| 6711 | |
| 6712 | InterceptorInfo* JSObject::GetIndexedInterceptor() { |
| 6713 | ASSERT(map()->has_indexed_interceptor()); |
| 6714 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6715 | ASSERT(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6716 | Object* result = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6717 | constructor->shared()->get_api_func_data()->indexed_property_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6718 | return InterceptorInfo::cast(result); |
| 6719 | } |
| 6720 | |
| 6721 | |
| 6722 | Object* JSObject::GetPropertyPostInterceptor(JSObject* receiver, |
| 6723 | String* name, |
| 6724 | PropertyAttributes* attributes) { |
| 6725 | // Check local property in holder, ignore interceptor. |
| 6726 | LookupResult result; |
| 6727 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6728 | if (result.IsProperty()) { |
| 6729 | return GetProperty(receiver, &result, name, attributes); |
| 6730 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6731 | // Continue searching via the prototype chain. |
| 6732 | Object* pt = GetPrototype(); |
| 6733 | *attributes = ABSENT; |
| 6734 | if (pt == Heap::null_value()) return Heap::undefined_value(); |
| 6735 | return pt->GetPropertyWithReceiver(receiver, name, attributes); |
| 6736 | } |
| 6737 | |
| 6738 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6739 | Object* JSObject::GetLocalPropertyPostInterceptor( |
| 6740 | JSObject* receiver, |
| 6741 | String* name, |
| 6742 | PropertyAttributes* attributes) { |
| 6743 | // Check local property in holder, ignore interceptor. |
| 6744 | LookupResult result; |
| 6745 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6746 | if (result.IsProperty()) { |
| 6747 | return GetProperty(receiver, &result, name, attributes); |
| 6748 | } |
| 6749 | return Heap::undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6753 | Object* JSObject::GetPropertyWithInterceptor( |
| 6754 | JSObject* receiver, |
| 6755 | String* name, |
| 6756 | PropertyAttributes* attributes) { |
| 6757 | InterceptorInfo* interceptor = GetNamedInterceptor(); |
| 6758 | HandleScope scope; |
| 6759 | Handle<JSObject> receiver_handle(receiver); |
| 6760 | Handle<JSObject> holder_handle(this); |
| 6761 | Handle<String> name_handle(name); |
| 6762 | |
| 6763 | if (!interceptor->getter()->IsUndefined()) { |
| 6764 | v8::NamedPropertyGetter getter = |
| 6765 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
| 6766 | LOG(ApiNamedPropertyAccess("interceptor-named-get", *holder_handle, name)); |
| 6767 | CustomArguments args(interceptor->data(), receiver, this); |
| 6768 | v8::AccessorInfo info(args.end()); |
| 6769 | v8::Handle<v8::Value> result; |
| 6770 | { |
| 6771 | // Leaving JavaScript. |
| 6772 | VMState state(EXTERNAL); |
| 6773 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 6774 | } |
| 6775 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6776 | if (!result.IsEmpty()) { |
| 6777 | *attributes = NONE; |
| 6778 | return *v8::Utils::OpenHandle(*result); |
| 6779 | } |
| 6780 | } |
| 6781 | |
| 6782 | Object* result = holder_handle->GetPropertyPostInterceptor( |
| 6783 | *receiver_handle, |
| 6784 | *name_handle, |
| 6785 | attributes); |
| 6786 | RETURN_IF_SCHEDULED_EXCEPTION(); |
| 6787 | return result; |
| 6788 | } |
| 6789 | |
| 6790 | |
| 6791 | bool JSObject::HasRealNamedProperty(String* key) { |
| 6792 | // Check access rights if needed. |
| 6793 | if (IsAccessCheckNeeded() && |
| 6794 | !Top::MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 6795 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 6796 | return false; |
| 6797 | } |
| 6798 | |
| 6799 | LookupResult result; |
| 6800 | LocalLookupRealNamedProperty(key, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6801 | return result.IsProperty() && (result.type() != INTERCEPTOR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | |
| 6805 | bool JSObject::HasRealElementProperty(uint32_t index) { |
| 6806 | // Check access rights if needed. |
| 6807 | if (IsAccessCheckNeeded() && |
| 6808 | !Top::MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 6809 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 6810 | return false; |
| 6811 | } |
| 6812 | |
| 6813 | // Handle [] on String objects. |
| 6814 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 6815 | |
| 6816 | switch (GetElementsKind()) { |
| 6817 | case FAST_ELEMENTS: { |
| 6818 | uint32_t length = IsJSArray() ? |
| 6819 | static_cast<uint32_t>( |
| 6820 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 6821 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 6822 | return (index < length) && |
| 6823 | !FixedArray::cast(elements())->get(index)->IsTheHole(); |
| 6824 | } |
| 6825 | case PIXEL_ELEMENTS: { |
| 6826 | PixelArray* pixels = PixelArray::cast(elements()); |
| 6827 | return index < static_cast<uint32_t>(pixels->length()); |
| 6828 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6829 | case EXTERNAL_BYTE_ELEMENTS: |
| 6830 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 6831 | case EXTERNAL_SHORT_ELEMENTS: |
| 6832 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 6833 | case EXTERNAL_INT_ELEMENTS: |
| 6834 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 6835 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 6836 | ExternalArray* array = ExternalArray::cast(elements()); |
| 6837 | return index < static_cast<uint32_t>(array->length()); |
| 6838 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6839 | case DICTIONARY_ELEMENTS: { |
| 6840 | return element_dictionary()->FindEntry(index) |
| 6841 | != NumberDictionary::kNotFound; |
| 6842 | } |
| 6843 | default: |
| 6844 | UNREACHABLE(); |
| 6845 | break; |
| 6846 | } |
| 6847 | // All possibilities have been handled above already. |
| 6848 | UNREACHABLE(); |
| 6849 | return Heap::null_value(); |
| 6850 | } |
| 6851 | |
| 6852 | |
| 6853 | bool JSObject::HasRealNamedCallbackProperty(String* key) { |
| 6854 | // Check access rights if needed. |
| 6855 | if (IsAccessCheckNeeded() && |
| 6856 | !Top::MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 6857 | Top::ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 6858 | return false; |
| 6859 | } |
| 6860 | |
| 6861 | LookupResult result; |
| 6862 | LocalLookupRealNamedProperty(key, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6863 | return result.IsProperty() && (result.type() == CALLBACKS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6864 | } |
| 6865 | |
| 6866 | |
| 6867 | int JSObject::NumberOfLocalProperties(PropertyAttributes filter) { |
| 6868 | if (HasFastProperties()) { |
| 6869 | DescriptorArray* descs = map()->instance_descriptors(); |
| 6870 | int result = 0; |
| 6871 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 6872 | PropertyDetails details = descs->GetDetails(i); |
| 6873 | if (details.IsProperty() && (details.attributes() & filter) == 0) { |
| 6874 | result++; |
| 6875 | } |
| 6876 | } |
| 6877 | return result; |
| 6878 | } else { |
| 6879 | return property_dictionary()->NumberOfElementsFilterAttributes(filter); |
| 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | |
| 6884 | int JSObject::NumberOfEnumProperties() { |
| 6885 | return NumberOfLocalProperties(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 6886 | } |
| 6887 | |
| 6888 | |
| 6889 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
| 6890 | Object* temp = get(i); |
| 6891 | set(i, get(j)); |
| 6892 | set(j, temp); |
| 6893 | if (this != numbers) { |
| 6894 | temp = numbers->get(i); |
| 6895 | numbers->set(i, numbers->get(j)); |
| 6896 | numbers->set(j, temp); |
| 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | |
| 6901 | static void InsertionSortPairs(FixedArray* content, |
| 6902 | FixedArray* numbers, |
| 6903 | int len) { |
| 6904 | for (int i = 1; i < len; i++) { |
| 6905 | int j = i; |
| 6906 | while (j > 0 && |
| 6907 | (NumberToUint32(numbers->get(j - 1)) > |
| 6908 | NumberToUint32(numbers->get(j)))) { |
| 6909 | content->SwapPairs(numbers, j - 1, j); |
| 6910 | j--; |
| 6911 | } |
| 6912 | } |
| 6913 | } |
| 6914 | |
| 6915 | |
| 6916 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
| 6917 | // In-place heap sort. |
| 6918 | ASSERT(content->length() == numbers->length()); |
| 6919 | |
| 6920 | // Bottom-up max-heap construction. |
| 6921 | for (int i = 1; i < len; ++i) { |
| 6922 | int child_index = i; |
| 6923 | while (child_index > 0) { |
| 6924 | int parent_index = ((child_index + 1) >> 1) - 1; |
| 6925 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 6926 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
| 6927 | if (parent_value < child_value) { |
| 6928 | content->SwapPairs(numbers, parent_index, child_index); |
| 6929 | } else { |
| 6930 | break; |
| 6931 | } |
| 6932 | child_index = parent_index; |
| 6933 | } |
| 6934 | } |
| 6935 | |
| 6936 | // Extract elements and create sorted array. |
| 6937 | for (int i = len - 1; i > 0; --i) { |
| 6938 | // Put max element at the back of the array. |
| 6939 | content->SwapPairs(numbers, 0, i); |
| 6940 | // Sift down the new top element. |
| 6941 | int parent_index = 0; |
| 6942 | while (true) { |
| 6943 | int child_index = ((parent_index + 1) << 1) - 1; |
| 6944 | if (child_index >= i) break; |
| 6945 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 6946 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 6947 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 6948 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 6949 | if (parent_value > child1_value) break; |
| 6950 | content->SwapPairs(numbers, parent_index, child_index); |
| 6951 | parent_index = child_index; |
| 6952 | } else { |
| 6953 | if (parent_value > child2_value) break; |
| 6954 | content->SwapPairs(numbers, parent_index, child_index + 1); |
| 6955 | parent_index = child_index + 1; |
| 6956 | } |
| 6957 | } |
| 6958 | } |
| 6959 | } |
| 6960 | |
| 6961 | |
| 6962 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 6963 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
| 6964 | ASSERT(this->length() == numbers->length()); |
| 6965 | // For small arrays, simply use insertion sort. |
| 6966 | if (len <= 10) { |
| 6967 | InsertionSortPairs(this, numbers, len); |
| 6968 | return; |
| 6969 | } |
| 6970 | // Check the range of indices. |
| 6971 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 6972 | uint32_t max_index = min_index; |
| 6973 | uint32_t i; |
| 6974 | for (i = 1; i < len; i++) { |
| 6975 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 6976 | min_index = NumberToUint32(numbers->get(i)); |
| 6977 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 6978 | max_index = NumberToUint32(numbers->get(i)); |
| 6979 | } |
| 6980 | } |
| 6981 | if (max_index - min_index + 1 == len) { |
| 6982 | // Indices form a contiguous range, unless there are duplicates. |
| 6983 | // Do an in-place linear time sort assuming distinct numbers, but |
| 6984 | // avoid hanging in case they are not. |
| 6985 | for (i = 0; i < len; i++) { |
| 6986 | uint32_t p; |
| 6987 | uint32_t j = 0; |
| 6988 | // While the current element at i is not at its correct position p, |
| 6989 | // swap the elements at these two positions. |
| 6990 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
| 6991 | j++ < len) { |
| 6992 | SwapPairs(numbers, i, p); |
| 6993 | } |
| 6994 | } |
| 6995 | } else { |
| 6996 | HeapSortPairs(this, numbers, len); |
| 6997 | return; |
| 6998 | } |
| 6999 | } |
| 7000 | |
| 7001 | |
| 7002 | // Fill in the names of local properties into the supplied storage. The main |
| 7003 | // purpose of this function is to provide reflection information for the object |
| 7004 | // mirrors. |
| 7005 | void JSObject::GetLocalPropertyNames(FixedArray* storage, int index) { |
| 7006 | ASSERT(storage->length() >= (NumberOfLocalProperties(NONE) - index)); |
| 7007 | if (HasFastProperties()) { |
| 7008 | DescriptorArray* descs = map()->instance_descriptors(); |
| 7009 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 7010 | if (descs->IsProperty(i)) storage->set(index++, descs->GetKey(i)); |
| 7011 | } |
| 7012 | ASSERT(storage->length() >= index); |
| 7013 | } else { |
| 7014 | property_dictionary()->CopyKeysTo(storage); |
| 7015 | } |
| 7016 | } |
| 7017 | |
| 7018 | |
| 7019 | int JSObject::NumberOfLocalElements(PropertyAttributes filter) { |
| 7020 | return GetLocalElementKeys(NULL, filter); |
| 7021 | } |
| 7022 | |
| 7023 | |
| 7024 | int JSObject::NumberOfEnumElements() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7025 | // Fast case for objects with no elements. |
| 7026 | if (!IsJSValue() && HasFastElements()) { |
| 7027 | uint32_t length = IsJSArray() ? |
| 7028 | static_cast<uint32_t>( |
| 7029 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 7030 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 7031 | if (length == 0) return 0; |
| 7032 | } |
| 7033 | // Compute the number of enumerable elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7034 | return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 7035 | } |
| 7036 | |
| 7037 | |
| 7038 | int JSObject::GetLocalElementKeys(FixedArray* storage, |
| 7039 | PropertyAttributes filter) { |
| 7040 | int counter = 0; |
| 7041 | switch (GetElementsKind()) { |
| 7042 | case FAST_ELEMENTS: { |
| 7043 | int length = IsJSArray() ? |
| 7044 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 7045 | FixedArray::cast(elements())->length(); |
| 7046 | for (int i = 0; i < length; i++) { |
| 7047 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 7048 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7049 | storage->set(counter, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7050 | } |
| 7051 | counter++; |
| 7052 | } |
| 7053 | } |
| 7054 | ASSERT(!storage || storage->length() >= counter); |
| 7055 | break; |
| 7056 | } |
| 7057 | case PIXEL_ELEMENTS: { |
| 7058 | int length = PixelArray::cast(elements())->length(); |
| 7059 | while (counter < length) { |
| 7060 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7061 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7062 | } |
| 7063 | counter++; |
| 7064 | } |
| 7065 | ASSERT(!storage || storage->length() >= counter); |
| 7066 | break; |
| 7067 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7068 | case EXTERNAL_BYTE_ELEMENTS: |
| 7069 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7070 | case EXTERNAL_SHORT_ELEMENTS: |
| 7071 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7072 | case EXTERNAL_INT_ELEMENTS: |
| 7073 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7074 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7075 | int length = ExternalArray::cast(elements())->length(); |
| 7076 | while (counter < length) { |
| 7077 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7078 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7079 | } |
| 7080 | counter++; |
| 7081 | } |
| 7082 | ASSERT(!storage || storage->length() >= counter); |
| 7083 | break; |
| 7084 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7085 | case DICTIONARY_ELEMENTS: { |
| 7086 | if (storage != NULL) { |
| 7087 | element_dictionary()->CopyKeysTo(storage, filter); |
| 7088 | } |
| 7089 | counter = element_dictionary()->NumberOfElementsFilterAttributes(filter); |
| 7090 | break; |
| 7091 | } |
| 7092 | default: |
| 7093 | UNREACHABLE(); |
| 7094 | break; |
| 7095 | } |
| 7096 | |
| 7097 | if (this->IsJSValue()) { |
| 7098 | Object* val = JSValue::cast(this)->value(); |
| 7099 | if (val->IsString()) { |
| 7100 | String* str = String::cast(val); |
| 7101 | if (storage) { |
| 7102 | for (int i = 0; i < str->length(); i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7103 | storage->set(counter + i, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7104 | } |
| 7105 | } |
| 7106 | counter += str->length(); |
| 7107 | } |
| 7108 | } |
| 7109 | ASSERT(!storage || storage->length() == counter); |
| 7110 | return counter; |
| 7111 | } |
| 7112 | |
| 7113 | |
| 7114 | int JSObject::GetEnumElementKeys(FixedArray* storage) { |
| 7115 | return GetLocalElementKeys(storage, |
| 7116 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 7117 | } |
| 7118 | |
| 7119 | |
| 7120 | bool NumberDictionaryShape::IsMatch(uint32_t key, Object* other) { |
| 7121 | ASSERT(other->IsNumber()); |
| 7122 | return key == static_cast<uint32_t>(other->Number()); |
| 7123 | } |
| 7124 | |
| 7125 | |
| 7126 | uint32_t NumberDictionaryShape::Hash(uint32_t key) { |
| 7127 | return ComputeIntegerHash(key); |
| 7128 | } |
| 7129 | |
| 7130 | |
| 7131 | uint32_t NumberDictionaryShape::HashForObject(uint32_t key, Object* other) { |
| 7132 | ASSERT(other->IsNumber()); |
| 7133 | return ComputeIntegerHash(static_cast<uint32_t>(other->Number())); |
| 7134 | } |
| 7135 | |
| 7136 | |
| 7137 | Object* NumberDictionaryShape::AsObject(uint32_t key) { |
| 7138 | return Heap::NumberFromUint32(key); |
| 7139 | } |
| 7140 | |
| 7141 | |
| 7142 | bool StringDictionaryShape::IsMatch(String* key, Object* other) { |
| 7143 | // We know that all entries in a hash table had their hash keys created. |
| 7144 | // Use that knowledge to have fast failure. |
| 7145 | if (key->Hash() != String::cast(other)->Hash()) return false; |
| 7146 | return key->Equals(String::cast(other)); |
| 7147 | } |
| 7148 | |
| 7149 | |
| 7150 | uint32_t StringDictionaryShape::Hash(String* key) { |
| 7151 | return key->Hash(); |
| 7152 | } |
| 7153 | |
| 7154 | |
| 7155 | uint32_t StringDictionaryShape::HashForObject(String* key, Object* other) { |
| 7156 | return String::cast(other)->Hash(); |
| 7157 | } |
| 7158 | |
| 7159 | |
| 7160 | Object* StringDictionaryShape::AsObject(String* key) { |
| 7161 | return key; |
| 7162 | } |
| 7163 | |
| 7164 | |
| 7165 | // StringKey simply carries a string object as key. |
| 7166 | class StringKey : public HashTableKey { |
| 7167 | public: |
| 7168 | explicit StringKey(String* string) : |
| 7169 | string_(string), |
| 7170 | hash_(HashForObject(string)) { } |
| 7171 | |
| 7172 | bool IsMatch(Object* string) { |
| 7173 | // We know that all entries in a hash table had their hash keys created. |
| 7174 | // Use that knowledge to have fast failure. |
| 7175 | if (hash_ != HashForObject(string)) { |
| 7176 | return false; |
| 7177 | } |
| 7178 | return string_->Equals(String::cast(string)); |
| 7179 | } |
| 7180 | |
| 7181 | uint32_t Hash() { return hash_; } |
| 7182 | |
| 7183 | uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); } |
| 7184 | |
| 7185 | Object* AsObject() { return string_; } |
| 7186 | |
| 7187 | String* string_; |
| 7188 | uint32_t hash_; |
| 7189 | }; |
| 7190 | |
| 7191 | |
| 7192 | // StringSharedKeys are used as keys in the eval cache. |
| 7193 | class StringSharedKey : public HashTableKey { |
| 7194 | public: |
| 7195 | StringSharedKey(String* source, SharedFunctionInfo* shared) |
| 7196 | : source_(source), shared_(shared) { } |
| 7197 | |
| 7198 | bool IsMatch(Object* other) { |
| 7199 | if (!other->IsFixedArray()) return false; |
| 7200 | FixedArray* pair = FixedArray::cast(other); |
| 7201 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(pair->get(0)); |
| 7202 | if (shared != shared_) return false; |
| 7203 | String* source = String::cast(pair->get(1)); |
| 7204 | return source->Equals(source_); |
| 7205 | } |
| 7206 | |
| 7207 | static uint32_t StringSharedHashHelper(String* source, |
| 7208 | SharedFunctionInfo* shared) { |
| 7209 | uint32_t hash = source->Hash(); |
| 7210 | if (shared->HasSourceCode()) { |
| 7211 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 7212 | // code computation, we use a combination of the hash of the |
| 7213 | // script source code and the start and end positions. We do |
| 7214 | // this to ensure that the cache entries can survive garbage |
| 7215 | // collection. |
| 7216 | Script* script = Script::cast(shared->script()); |
| 7217 | hash ^= String::cast(script->source())->Hash(); |
| 7218 | hash += shared->start_position(); |
| 7219 | } |
| 7220 | return hash; |
| 7221 | } |
| 7222 | |
| 7223 | uint32_t Hash() { |
| 7224 | return StringSharedHashHelper(source_, shared_); |
| 7225 | } |
| 7226 | |
| 7227 | uint32_t HashForObject(Object* obj) { |
| 7228 | FixedArray* pair = FixedArray::cast(obj); |
| 7229 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(pair->get(0)); |
| 7230 | String* source = String::cast(pair->get(1)); |
| 7231 | return StringSharedHashHelper(source, shared); |
| 7232 | } |
| 7233 | |
| 7234 | Object* AsObject() { |
| 7235 | Object* obj = Heap::AllocateFixedArray(2); |
| 7236 | if (obj->IsFailure()) return obj; |
| 7237 | FixedArray* pair = FixedArray::cast(obj); |
| 7238 | pair->set(0, shared_); |
| 7239 | pair->set(1, source_); |
| 7240 | return pair; |
| 7241 | } |
| 7242 | |
| 7243 | private: |
| 7244 | String* source_; |
| 7245 | SharedFunctionInfo* shared_; |
| 7246 | }; |
| 7247 | |
| 7248 | |
| 7249 | // RegExpKey carries the source and flags of a regular expression as key. |
| 7250 | class RegExpKey : public HashTableKey { |
| 7251 | public: |
| 7252 | RegExpKey(String* string, JSRegExp::Flags flags) |
| 7253 | : string_(string), |
| 7254 | flags_(Smi::FromInt(flags.value())) { } |
| 7255 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7256 | // Rather than storing the key in the hash table, a pointer to the |
| 7257 | // stored value is stored where the key should be. IsMatch then |
| 7258 | // compares the search key to the found object, rather than comparing |
| 7259 | // a key to a key. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7260 | bool IsMatch(Object* obj) { |
| 7261 | FixedArray* val = FixedArray::cast(obj); |
| 7262 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 7263 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 7264 | } |
| 7265 | |
| 7266 | uint32_t Hash() { return RegExpHash(string_, flags_); } |
| 7267 | |
| 7268 | Object* AsObject() { |
| 7269 | // Plain hash maps, which is where regexp keys are used, don't |
| 7270 | // use this function. |
| 7271 | UNREACHABLE(); |
| 7272 | return NULL; |
| 7273 | } |
| 7274 | |
| 7275 | uint32_t HashForObject(Object* obj) { |
| 7276 | FixedArray* val = FixedArray::cast(obj); |
| 7277 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 7278 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 7279 | } |
| 7280 | |
| 7281 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 7282 | return string->Hash() + flags->value(); |
| 7283 | } |
| 7284 | |
| 7285 | String* string_; |
| 7286 | Smi* flags_; |
| 7287 | }; |
| 7288 | |
| 7289 | // Utf8SymbolKey carries a vector of chars as key. |
| 7290 | class Utf8SymbolKey : public HashTableKey { |
| 7291 | public: |
| 7292 | explicit Utf8SymbolKey(Vector<const char> string) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7293 | : string_(string), hash_field_(0) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7294 | |
| 7295 | bool IsMatch(Object* string) { |
| 7296 | return String::cast(string)->IsEqualTo(string_); |
| 7297 | } |
| 7298 | |
| 7299 | uint32_t Hash() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7300 | if (hash_field_ != 0) return hash_field_ >> String::kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7301 | unibrow::Utf8InputBuffer<> buffer(string_.start(), |
| 7302 | static_cast<unsigned>(string_.length())); |
| 7303 | chars_ = buffer.Length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7304 | hash_field_ = String::ComputeHashField(&buffer, chars_); |
| 7305 | uint32_t result = hash_field_ >> String::kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7306 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 7307 | return result; |
| 7308 | } |
| 7309 | |
| 7310 | uint32_t HashForObject(Object* other) { |
| 7311 | return String::cast(other)->Hash(); |
| 7312 | } |
| 7313 | |
| 7314 | Object* AsObject() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7315 | if (hash_field_ == 0) Hash(); |
| 7316 | return Heap::AllocateSymbol(string_, chars_, hash_field_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7317 | } |
| 7318 | |
| 7319 | Vector<const char> string_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7320 | uint32_t hash_field_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7321 | int chars_; // Caches the number of characters when computing the hash code. |
| 7322 | }; |
| 7323 | |
| 7324 | |
| 7325 | // SymbolKey carries a string/symbol object as key. |
| 7326 | class SymbolKey : public HashTableKey { |
| 7327 | public: |
| 7328 | explicit SymbolKey(String* string) : string_(string) { } |
| 7329 | |
| 7330 | bool IsMatch(Object* string) { |
| 7331 | return String::cast(string)->Equals(string_); |
| 7332 | } |
| 7333 | |
| 7334 | uint32_t Hash() { return string_->Hash(); } |
| 7335 | |
| 7336 | uint32_t HashForObject(Object* other) { |
| 7337 | return String::cast(other)->Hash(); |
| 7338 | } |
| 7339 | |
| 7340 | Object* AsObject() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7341 | // Attempt to flatten the string, so that symbols will most often |
| 7342 | // be flat strings. |
| 7343 | string_ = string_->TryFlattenGetString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7344 | // Transform string to symbol if possible. |
| 7345 | Map* map = Heap::SymbolMapForString(string_); |
| 7346 | if (map != NULL) { |
| 7347 | string_->set_map(map); |
| 7348 | ASSERT(string_->IsSymbol()); |
| 7349 | return string_; |
| 7350 | } |
| 7351 | // Otherwise allocate a new symbol. |
| 7352 | StringInputBuffer buffer(string_); |
| 7353 | return Heap::AllocateInternalSymbol(&buffer, |
| 7354 | string_->length(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7355 | string_->hash_field()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7356 | } |
| 7357 | |
| 7358 | static uint32_t StringHash(Object* obj) { |
| 7359 | return String::cast(obj)->Hash(); |
| 7360 | } |
| 7361 | |
| 7362 | String* string_; |
| 7363 | }; |
| 7364 | |
| 7365 | |
| 7366 | template<typename Shape, typename Key> |
| 7367 | void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
| 7368 | IteratePointers(v, 0, kElementsStartOffset); |
| 7369 | } |
| 7370 | |
| 7371 | |
| 7372 | template<typename Shape, typename Key> |
| 7373 | void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) { |
| 7374 | IteratePointers(v, |
| 7375 | kElementsStartOffset, |
| 7376 | kHeaderSize + length() * kPointerSize); |
| 7377 | } |
| 7378 | |
| 7379 | |
| 7380 | template<typename Shape, typename Key> |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7381 | Object* HashTable<Shape, Key>::Allocate(int at_least_space_for, |
| 7382 | PretenureFlag pretenure) { |
| 7383 | const int kMinCapacity = 32; |
| 7384 | int capacity = RoundUpToPowerOf2(at_least_space_for * 2); |
| 7385 | if (capacity < kMinCapacity) { |
| 7386 | capacity = kMinCapacity; // Guarantee min capacity. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7387 | } else if (capacity > HashTable::kMaxCapacity) { |
| 7388 | return Failure::OutOfMemoryException(); |
| 7389 | } |
| 7390 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7391 | Object* obj = Heap::AllocateHashTable(EntryToIndex(capacity), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7392 | if (!obj->IsFailure()) { |
| 7393 | HashTable::cast(obj)->SetNumberOfElements(0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7394 | HashTable::cast(obj)->SetNumberOfDeletedElements(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7395 | HashTable::cast(obj)->SetCapacity(capacity); |
| 7396 | } |
| 7397 | return obj; |
| 7398 | } |
| 7399 | |
| 7400 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7401 | // Find entry for key otherwise return kNotFound. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7402 | template<typename Shape, typename Key> |
| 7403 | int HashTable<Shape, Key>::FindEntry(Key key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7404 | uint32_t capacity = Capacity(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7405 | uint32_t entry = FirstProbe(Shape::Hash(key), capacity); |
| 7406 | uint32_t count = 1; |
| 7407 | // EnsureCapacity will guarantee the hash table is never full. |
| 7408 | while (true) { |
| 7409 | Object* element = KeyAt(entry); |
| 7410 | if (element->IsUndefined()) break; // Empty entry. |
| 7411 | if (!element->IsNull() && Shape::IsMatch(key, element)) return entry; |
| 7412 | entry = NextProbe(entry, count++, capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7413 | } |
| 7414 | return kNotFound; |
| 7415 | } |
| 7416 | |
| 7417 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 7418 | // Find entry for key otherwise return kNotFound. |
| 7419 | int StringDictionary::FindEntry(String* key) { |
| 7420 | if (!key->IsSymbol()) { |
| 7421 | return HashTable<StringDictionaryShape, String*>::FindEntry(key); |
| 7422 | } |
| 7423 | |
| 7424 | // Optimized for symbol key. Knowledge of the key type allows: |
| 7425 | // 1. Move the check if the key is a symbol out of the loop. |
| 7426 | // 2. Avoid comparing hash codes in symbol to symbol comparision. |
| 7427 | // 3. Detect a case when a dictionary key is not a symbol but the key is. |
| 7428 | // In case of positive result the dictionary key may be replaced by |
| 7429 | // the symbol with minimal performance penalty. It gives a chance to |
| 7430 | // perform further lookups in code stubs (and significant performance boost |
| 7431 | // a certain style of code). |
| 7432 | |
| 7433 | // EnsureCapacity will guarantee the hash table is never full. |
| 7434 | uint32_t capacity = Capacity(); |
| 7435 | uint32_t entry = FirstProbe(key->Hash(), capacity); |
| 7436 | uint32_t count = 1; |
| 7437 | |
| 7438 | while (true) { |
| 7439 | int index = EntryToIndex(entry); |
| 7440 | Object* element = get(index); |
| 7441 | if (element->IsUndefined()) break; // Empty entry. |
| 7442 | if (key == element) return entry; |
| 7443 | if (!element->IsSymbol() && |
| 7444 | !element->IsNull() && |
| 7445 | String::cast(element)->Equals(key)) { |
| 7446 | // Replace a non-symbol key by the equivalent symbol for faster further |
| 7447 | // lookups. |
| 7448 | set(index, key); |
| 7449 | return entry; |
| 7450 | } |
| 7451 | ASSERT(element->IsNull() || !String::cast(element)->Equals(key)); |
| 7452 | entry = NextProbe(entry, count++, capacity); |
| 7453 | } |
| 7454 | return kNotFound; |
| 7455 | } |
| 7456 | |
| 7457 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7458 | template<typename Shape, typename Key> |
| 7459 | Object* HashTable<Shape, Key>::EnsureCapacity(int n, Key key) { |
| 7460 | int capacity = Capacity(); |
| 7461 | int nof = NumberOfElements() + n; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7462 | int nod = NumberOfDeletedElements(); |
| 7463 | // Return if: |
| 7464 | // 50% is still free after adding n elements and |
| 7465 | // at most 50% of the free elements are deleted elements. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7466 | if (nod <= (capacity - nof) >> 1) { |
| 7467 | int needed_free = nof >> 1; |
| 7468 | if (nof + needed_free <= capacity) return this; |
| 7469 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7470 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7471 | const int kMinCapacityForPretenure = 256; |
| 7472 | bool pretenure = |
| 7473 | (capacity > kMinCapacityForPretenure) && !Heap::InNewSpace(this); |
| 7474 | Object* obj = Allocate(nof * 2, pretenure ? TENURED : NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7475 | if (obj->IsFailure()) return obj; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7476 | |
| 7477 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7478 | HashTable* table = HashTable::cast(obj); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7479 | WriteBarrierMode mode = table->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7480 | |
| 7481 | // Copy prefix to new array. |
| 7482 | for (int i = kPrefixStartIndex; |
| 7483 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 7484 | i++) { |
| 7485 | table->set(i, get(i), mode); |
| 7486 | } |
| 7487 | // Rehash the elements. |
| 7488 | for (int i = 0; i < capacity; i++) { |
| 7489 | uint32_t from_index = EntryToIndex(i); |
| 7490 | Object* k = get(from_index); |
| 7491 | if (IsKey(k)) { |
| 7492 | uint32_t hash = Shape::HashForObject(key, k); |
| 7493 | uint32_t insertion_index = |
| 7494 | EntryToIndex(table->FindInsertionEntry(hash)); |
| 7495 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 7496 | table->set(insertion_index + j, get(from_index + j), mode); |
| 7497 | } |
| 7498 | } |
| 7499 | } |
| 7500 | table->SetNumberOfElements(NumberOfElements()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7501 | table->SetNumberOfDeletedElements(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7502 | return table; |
| 7503 | } |
| 7504 | |
| 7505 | |
| 7506 | template<typename Shape, typename Key> |
| 7507 | uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) { |
| 7508 | uint32_t capacity = Capacity(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7509 | uint32_t entry = FirstProbe(hash, capacity); |
| 7510 | uint32_t count = 1; |
| 7511 | // EnsureCapacity will guarantee the hash table is never full. |
| 7512 | while (true) { |
| 7513 | Object* element = KeyAt(entry); |
| 7514 | if (element->IsUndefined() || element->IsNull()) break; |
| 7515 | entry = NextProbe(entry, count++, capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7516 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7517 | return entry; |
| 7518 | } |
| 7519 | |
| 7520 | // Force instantiation of template instances class. |
| 7521 | // Please note this list is compiler dependent. |
| 7522 | |
| 7523 | template class HashTable<SymbolTableShape, HashTableKey*>; |
| 7524 | |
| 7525 | template class HashTable<CompilationCacheShape, HashTableKey*>; |
| 7526 | |
| 7527 | template class HashTable<MapCacheShape, HashTableKey*>; |
| 7528 | |
| 7529 | template class Dictionary<StringDictionaryShape, String*>; |
| 7530 | |
| 7531 | template class Dictionary<NumberDictionaryShape, uint32_t>; |
| 7532 | |
| 7533 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::Allocate( |
| 7534 | int); |
| 7535 | |
| 7536 | template Object* Dictionary<StringDictionaryShape, String*>::Allocate( |
| 7537 | int); |
| 7538 | |
| 7539 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::AtPut( |
| 7540 | uint32_t, Object*); |
| 7541 | |
| 7542 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::SlowReverseLookup( |
| 7543 | Object*); |
| 7544 | |
| 7545 | template Object* Dictionary<StringDictionaryShape, String*>::SlowReverseLookup( |
| 7546 | Object*); |
| 7547 | |
| 7548 | template void Dictionary<NumberDictionaryShape, uint32_t>::CopyKeysTo( |
| 7549 | FixedArray*, PropertyAttributes); |
| 7550 | |
| 7551 | template Object* Dictionary<StringDictionaryShape, String*>::DeleteProperty( |
| 7552 | int, JSObject::DeleteMode); |
| 7553 | |
| 7554 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::DeleteProperty( |
| 7555 | int, JSObject::DeleteMode); |
| 7556 | |
| 7557 | template void Dictionary<StringDictionaryShape, String*>::CopyKeysTo( |
| 7558 | FixedArray*); |
| 7559 | |
| 7560 | template int |
| 7561 | Dictionary<StringDictionaryShape, String*>::NumberOfElementsFilterAttributes( |
| 7562 | PropertyAttributes); |
| 7563 | |
| 7564 | template Object* Dictionary<StringDictionaryShape, String*>::Add( |
| 7565 | String*, Object*, PropertyDetails); |
| 7566 | |
| 7567 | template Object* |
| 7568 | Dictionary<StringDictionaryShape, String*>::GenerateNewEnumerationIndices(); |
| 7569 | |
| 7570 | template int |
| 7571 | Dictionary<NumberDictionaryShape, uint32_t>::NumberOfElementsFilterAttributes( |
| 7572 | PropertyAttributes); |
| 7573 | |
| 7574 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::Add( |
| 7575 | uint32_t, Object*, PropertyDetails); |
| 7576 | |
| 7577 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::EnsureCapacity( |
| 7578 | int, uint32_t); |
| 7579 | |
| 7580 | template Object* Dictionary<StringDictionaryShape, String*>::EnsureCapacity( |
| 7581 | int, String*); |
| 7582 | |
| 7583 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::AddEntry( |
| 7584 | uint32_t, Object*, PropertyDetails, uint32_t); |
| 7585 | |
| 7586 | template Object* Dictionary<StringDictionaryShape, String*>::AddEntry( |
| 7587 | String*, Object*, PropertyDetails, uint32_t); |
| 7588 | |
| 7589 | template |
| 7590 | int Dictionary<NumberDictionaryShape, uint32_t>::NumberOfEnumElements(); |
| 7591 | |
| 7592 | template |
| 7593 | int Dictionary<StringDictionaryShape, String*>::NumberOfEnumElements(); |
| 7594 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 7595 | template |
| 7596 | int HashTable<NumberDictionaryShape, uint32_t>::FindEntry(uint32_t); |
| 7597 | |
| 7598 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7599 | // Collates undefined and unexisting elements below limit from position |
| 7600 | // zero of the elements. The object stays in Dictionary mode. |
| 7601 | Object* JSObject::PrepareSlowElementsForSort(uint32_t limit) { |
| 7602 | ASSERT(HasDictionaryElements()); |
| 7603 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 7604 | // or because we are not going to sort (and therefore compact) all of the |
| 7605 | // elements. |
| 7606 | NumberDictionary* dict = element_dictionary(); |
| 7607 | HeapNumber* result_double = NULL; |
| 7608 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 7609 | // Allocate space for result before we start mutating the object. |
| 7610 | Object* new_double = Heap::AllocateHeapNumber(0.0); |
| 7611 | if (new_double->IsFailure()) return new_double; |
| 7612 | result_double = HeapNumber::cast(new_double); |
| 7613 | } |
| 7614 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7615 | Object* obj = NumberDictionary::Allocate(dict->NumberOfElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7616 | if (obj->IsFailure()) return obj; |
| 7617 | NumberDictionary* new_dict = NumberDictionary::cast(obj); |
| 7618 | |
| 7619 | AssertNoAllocation no_alloc; |
| 7620 | |
| 7621 | uint32_t pos = 0; |
| 7622 | uint32_t undefs = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 7623 | int capacity = dict->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7624 | for (int i = 0; i < capacity; i++) { |
| 7625 | Object* k = dict->KeyAt(i); |
| 7626 | if (dict->IsKey(k)) { |
| 7627 | ASSERT(k->IsNumber()); |
| 7628 | ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 7629 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 7630 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 7631 | Object* value = dict->ValueAt(i); |
| 7632 | PropertyDetails details = dict->DetailsAt(i); |
| 7633 | if (details.type() == CALLBACKS) { |
| 7634 | // Bail out and do the sorting of undefineds and array holes in JS. |
| 7635 | return Smi::FromInt(-1); |
| 7636 | } |
| 7637 | uint32_t key = NumberToUint32(k); |
| 7638 | if (key < limit) { |
| 7639 | if (value->IsUndefined()) { |
| 7640 | undefs++; |
| 7641 | } else { |
| 7642 | new_dict->AddNumberEntry(pos, value, details); |
| 7643 | pos++; |
| 7644 | } |
| 7645 | } else { |
| 7646 | new_dict->AddNumberEntry(key, value, details); |
| 7647 | } |
| 7648 | } |
| 7649 | } |
| 7650 | |
| 7651 | uint32_t result = pos; |
| 7652 | PropertyDetails no_details = PropertyDetails(NONE, NORMAL); |
| 7653 | while (undefs > 0) { |
| 7654 | new_dict->AddNumberEntry(pos, Heap::undefined_value(), no_details); |
| 7655 | pos++; |
| 7656 | undefs--; |
| 7657 | } |
| 7658 | |
| 7659 | set_elements(new_dict); |
| 7660 | |
| 7661 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 7662 | return Smi::FromInt(static_cast<int>(result)); |
| 7663 | } |
| 7664 | |
| 7665 | ASSERT_NE(NULL, result_double); |
| 7666 | result_double->set_value(static_cast<double>(result)); |
| 7667 | return result_double; |
| 7668 | } |
| 7669 | |
| 7670 | |
| 7671 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 7672 | // the start of the elements array. |
| 7673 | // If the object is in dictionary mode, it is converted to fast elements |
| 7674 | // mode. |
| 7675 | Object* JSObject::PrepareElementsForSort(uint32_t limit) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7676 | ASSERT(!HasPixelElements() && !HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7677 | |
| 7678 | if (HasDictionaryElements()) { |
| 7679 | // Convert to fast elements containing only the existing properties. |
| 7680 | // Ordering is irrelevant, since we are going to sort anyway. |
| 7681 | NumberDictionary* dict = element_dictionary(); |
| 7682 | if (IsJSArray() || dict->requires_slow_elements() || |
| 7683 | dict->max_number_key() >= limit) { |
| 7684 | return PrepareSlowElementsForSort(limit); |
| 7685 | } |
| 7686 | // Convert to fast elements. |
| 7687 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7688 | Object* obj = map()->GetFastElementsMap(); |
| 7689 | if (obj->IsFailure()) return obj; |
| 7690 | Map* new_map = Map::cast(obj); |
| 7691 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7692 | PretenureFlag tenure = Heap::InNewSpace(this) ? NOT_TENURED: TENURED; |
| 7693 | Object* new_array = |
| 7694 | Heap::AllocateFixedArray(dict->NumberOfElements(), tenure); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7695 | if (new_array->IsFailure()) return new_array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7696 | FixedArray* fast_elements = FixedArray::cast(new_array); |
| 7697 | dict->CopyValuesTo(fast_elements); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7698 | |
| 7699 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7700 | set_elements(fast_elements); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7701 | } else { |
| 7702 | Object* obj = EnsureWritableFastElements(); |
| 7703 | if (obj->IsFailure()) return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7704 | } |
| 7705 | ASSERT(HasFastElements()); |
| 7706 | |
| 7707 | // Collect holes at the end, undefined before that and the rest at the |
| 7708 | // start, and return the number of non-hole, non-undefined values. |
| 7709 | |
| 7710 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 7711 | uint32_t elements_length = static_cast<uint32_t>(elements->length()); |
| 7712 | if (limit > elements_length) { |
| 7713 | limit = elements_length ; |
| 7714 | } |
| 7715 | if (limit == 0) { |
| 7716 | return Smi::FromInt(0); |
| 7717 | } |
| 7718 | |
| 7719 | HeapNumber* result_double = NULL; |
| 7720 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 7721 | // Pessimistically allocate space for return value before |
| 7722 | // we start mutating the array. |
| 7723 | Object* new_double = Heap::AllocateHeapNumber(0.0); |
| 7724 | if (new_double->IsFailure()) return new_double; |
| 7725 | result_double = HeapNumber::cast(new_double); |
| 7726 | } |
| 7727 | |
| 7728 | AssertNoAllocation no_alloc; |
| 7729 | |
| 7730 | // Split elements into defined, undefined and the_hole, in that order. |
| 7731 | // Only count locations for undefined and the hole, and fill them afterwards. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7732 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7733 | unsigned int undefs = limit; |
| 7734 | unsigned int holes = limit; |
| 7735 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 7736 | // number of stores of non-undefined, non-the-hole values. |
| 7737 | for (unsigned int i = 0; i < undefs; i++) { |
| 7738 | Object* current = elements->get(i); |
| 7739 | if (current->IsTheHole()) { |
| 7740 | holes--; |
| 7741 | undefs--; |
| 7742 | } else if (current->IsUndefined()) { |
| 7743 | undefs--; |
| 7744 | } else { |
| 7745 | continue; |
| 7746 | } |
| 7747 | // Position i needs to be filled. |
| 7748 | while (undefs > i) { |
| 7749 | current = elements->get(undefs); |
| 7750 | if (current->IsTheHole()) { |
| 7751 | holes--; |
| 7752 | undefs--; |
| 7753 | } else if (current->IsUndefined()) { |
| 7754 | undefs--; |
| 7755 | } else { |
| 7756 | elements->set(i, current, write_barrier); |
| 7757 | break; |
| 7758 | } |
| 7759 | } |
| 7760 | } |
| 7761 | uint32_t result = undefs; |
| 7762 | while (undefs < holes) { |
| 7763 | elements->set_undefined(undefs); |
| 7764 | undefs++; |
| 7765 | } |
| 7766 | while (holes < limit) { |
| 7767 | elements->set_the_hole(holes); |
| 7768 | holes++; |
| 7769 | } |
| 7770 | |
| 7771 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 7772 | return Smi::FromInt(static_cast<int>(result)); |
| 7773 | } |
| 7774 | ASSERT_NE(NULL, result_double); |
| 7775 | result_double->set_value(static_cast<double>(result)); |
| 7776 | return result_double; |
| 7777 | } |
| 7778 | |
| 7779 | |
| 7780 | Object* PixelArray::SetValue(uint32_t index, Object* value) { |
| 7781 | uint8_t clamped_value = 0; |
| 7782 | if (index < static_cast<uint32_t>(length())) { |
| 7783 | if (value->IsSmi()) { |
| 7784 | int int_value = Smi::cast(value)->value(); |
| 7785 | if (int_value < 0) { |
| 7786 | clamped_value = 0; |
| 7787 | } else if (int_value > 255) { |
| 7788 | clamped_value = 255; |
| 7789 | } else { |
| 7790 | clamped_value = static_cast<uint8_t>(int_value); |
| 7791 | } |
| 7792 | } else if (value->IsHeapNumber()) { |
| 7793 | double double_value = HeapNumber::cast(value)->value(); |
| 7794 | if (!(double_value > 0)) { |
| 7795 | // NaN and less than zero clamp to zero. |
| 7796 | clamped_value = 0; |
| 7797 | } else if (double_value > 255) { |
| 7798 | // Greater than 255 clamp to 255. |
| 7799 | clamped_value = 255; |
| 7800 | } else { |
| 7801 | // Other doubles are rounded to the nearest integer. |
| 7802 | clamped_value = static_cast<uint8_t>(double_value + 0.5); |
| 7803 | } |
| 7804 | } else { |
| 7805 | // Clamp undefined to zero (default). All other types have been |
| 7806 | // converted to a number type further up in the call chain. |
| 7807 | ASSERT(value->IsUndefined()); |
| 7808 | } |
| 7809 | set(index, clamped_value); |
| 7810 | } |
| 7811 | return Smi::FromInt(clamped_value); |
| 7812 | } |
| 7813 | |
| 7814 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7815 | template<typename ExternalArrayClass, typename ValueType> |
| 7816 | static Object* ExternalArrayIntSetter(ExternalArrayClass* receiver, |
| 7817 | uint32_t index, |
| 7818 | Object* value) { |
| 7819 | ValueType cast_value = 0; |
| 7820 | if (index < static_cast<uint32_t>(receiver->length())) { |
| 7821 | if (value->IsSmi()) { |
| 7822 | int int_value = Smi::cast(value)->value(); |
| 7823 | cast_value = static_cast<ValueType>(int_value); |
| 7824 | } else if (value->IsHeapNumber()) { |
| 7825 | double double_value = HeapNumber::cast(value)->value(); |
| 7826 | cast_value = static_cast<ValueType>(DoubleToInt32(double_value)); |
| 7827 | } else { |
| 7828 | // Clamp undefined to zero (default). All other types have been |
| 7829 | // converted to a number type further up in the call chain. |
| 7830 | ASSERT(value->IsUndefined()); |
| 7831 | } |
| 7832 | receiver->set(index, cast_value); |
| 7833 | } |
| 7834 | return Heap::NumberFromInt32(cast_value); |
| 7835 | } |
| 7836 | |
| 7837 | |
| 7838 | Object* ExternalByteArray::SetValue(uint32_t index, Object* value) { |
| 7839 | return ExternalArrayIntSetter<ExternalByteArray, int8_t> |
| 7840 | (this, index, value); |
| 7841 | } |
| 7842 | |
| 7843 | |
| 7844 | Object* ExternalUnsignedByteArray::SetValue(uint32_t index, Object* value) { |
| 7845 | return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t> |
| 7846 | (this, index, value); |
| 7847 | } |
| 7848 | |
| 7849 | |
| 7850 | Object* ExternalShortArray::SetValue(uint32_t index, Object* value) { |
| 7851 | return ExternalArrayIntSetter<ExternalShortArray, int16_t> |
| 7852 | (this, index, value); |
| 7853 | } |
| 7854 | |
| 7855 | |
| 7856 | Object* ExternalUnsignedShortArray::SetValue(uint32_t index, Object* value) { |
| 7857 | return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t> |
| 7858 | (this, index, value); |
| 7859 | } |
| 7860 | |
| 7861 | |
| 7862 | Object* ExternalIntArray::SetValue(uint32_t index, Object* value) { |
| 7863 | return ExternalArrayIntSetter<ExternalIntArray, int32_t> |
| 7864 | (this, index, value); |
| 7865 | } |
| 7866 | |
| 7867 | |
| 7868 | Object* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) { |
| 7869 | uint32_t cast_value = 0; |
| 7870 | if (index < static_cast<uint32_t>(length())) { |
| 7871 | if (value->IsSmi()) { |
| 7872 | int int_value = Smi::cast(value)->value(); |
| 7873 | cast_value = static_cast<uint32_t>(int_value); |
| 7874 | } else if (value->IsHeapNumber()) { |
| 7875 | double double_value = HeapNumber::cast(value)->value(); |
| 7876 | cast_value = static_cast<uint32_t>(DoubleToUint32(double_value)); |
| 7877 | } else { |
| 7878 | // Clamp undefined to zero (default). All other types have been |
| 7879 | // converted to a number type further up in the call chain. |
| 7880 | ASSERT(value->IsUndefined()); |
| 7881 | } |
| 7882 | set(index, cast_value); |
| 7883 | } |
| 7884 | return Heap::NumberFromUint32(cast_value); |
| 7885 | } |
| 7886 | |
| 7887 | |
| 7888 | Object* ExternalFloatArray::SetValue(uint32_t index, Object* value) { |
| 7889 | float cast_value = 0; |
| 7890 | if (index < static_cast<uint32_t>(length())) { |
| 7891 | if (value->IsSmi()) { |
| 7892 | int int_value = Smi::cast(value)->value(); |
| 7893 | cast_value = static_cast<float>(int_value); |
| 7894 | } else if (value->IsHeapNumber()) { |
| 7895 | double double_value = HeapNumber::cast(value)->value(); |
| 7896 | cast_value = static_cast<float>(double_value); |
| 7897 | } else { |
| 7898 | // Clamp undefined to zero (default). All other types have been |
| 7899 | // converted to a number type further up in the call chain. |
| 7900 | ASSERT(value->IsUndefined()); |
| 7901 | } |
| 7902 | set(index, cast_value); |
| 7903 | } |
| 7904 | return Heap::AllocateHeapNumber(cast_value); |
| 7905 | } |
| 7906 | |
| 7907 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7908 | Object* GlobalObject::GetPropertyCell(LookupResult* result) { |
| 7909 | ASSERT(!HasFastProperties()); |
| 7910 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 7911 | ASSERT(value->IsJSGlobalPropertyCell()); |
| 7912 | return value; |
| 7913 | } |
| 7914 | |
| 7915 | |
| 7916 | Object* GlobalObject::EnsurePropertyCell(String* name) { |
| 7917 | ASSERT(!HasFastProperties()); |
| 7918 | int entry = property_dictionary()->FindEntry(name); |
| 7919 | if (entry == StringDictionary::kNotFound) { |
| 7920 | Object* cell = Heap::AllocateJSGlobalPropertyCell(Heap::the_hole_value()); |
| 7921 | if (cell->IsFailure()) return cell; |
| 7922 | PropertyDetails details(NONE, NORMAL); |
| 7923 | details = details.AsDeleted(); |
| 7924 | Object* dictionary = property_dictionary()->Add(name, cell, details); |
| 7925 | if (dictionary->IsFailure()) return dictionary; |
| 7926 | set_properties(StringDictionary::cast(dictionary)); |
| 7927 | return cell; |
| 7928 | } else { |
| 7929 | Object* value = property_dictionary()->ValueAt(entry); |
| 7930 | ASSERT(value->IsJSGlobalPropertyCell()); |
| 7931 | return value; |
| 7932 | } |
| 7933 | } |
| 7934 | |
| 7935 | |
| 7936 | Object* SymbolTable::LookupString(String* string, Object** s) { |
| 7937 | SymbolKey key(string); |
| 7938 | return LookupKey(&key, s); |
| 7939 | } |
| 7940 | |
| 7941 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 7942 | // This class is used for looking up two character strings in the symbol table. |
| 7943 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 7944 | // string hash calculation loop here for speed. Doesn't work if the two |
| 7945 | // characters form a decimal integer, since such strings have a different hash |
| 7946 | // algorithm. |
| 7947 | class TwoCharHashTableKey : public HashTableKey { |
| 7948 | public: |
| 7949 | TwoCharHashTableKey(uint32_t c1, uint32_t c2) |
| 7950 | : c1_(c1), c2_(c2) { |
| 7951 | // Char 1. |
| 7952 | uint32_t hash = c1 + (c1 << 10); |
| 7953 | hash ^= hash >> 6; |
| 7954 | // Char 2. |
| 7955 | hash += c2; |
| 7956 | hash += hash << 10; |
| 7957 | hash ^= hash >> 6; |
| 7958 | // GetHash. |
| 7959 | hash += hash << 3; |
| 7960 | hash ^= hash >> 11; |
| 7961 | hash += hash << 15; |
| 7962 | if (hash == 0) hash = 27; |
| 7963 | #ifdef DEBUG |
| 7964 | StringHasher hasher(2); |
| 7965 | hasher.AddCharacter(c1); |
| 7966 | hasher.AddCharacter(c2); |
| 7967 | // If this assert fails then we failed to reproduce the two-character |
| 7968 | // version of the string hashing algorithm above. One reason could be |
| 7969 | // that we were passed two digits as characters, since the hash |
| 7970 | // algorithm is different in that case. |
| 7971 | ASSERT_EQ(static_cast<int>(hasher.GetHash()), static_cast<int>(hash)); |
| 7972 | #endif |
| 7973 | hash_ = hash; |
| 7974 | } |
| 7975 | |
| 7976 | bool IsMatch(Object* o) { |
| 7977 | if (!o->IsString()) return false; |
| 7978 | String* other = String::cast(o); |
| 7979 | if (other->length() != 2) return false; |
| 7980 | if (other->Get(0) != c1_) return false; |
| 7981 | return other->Get(1) == c2_; |
| 7982 | } |
| 7983 | |
| 7984 | uint32_t Hash() { return hash_; } |
| 7985 | uint32_t HashForObject(Object* key) { |
| 7986 | if (!key->IsString()) return 0; |
| 7987 | return String::cast(key)->Hash(); |
| 7988 | } |
| 7989 | |
| 7990 | Object* AsObject() { |
| 7991 | // The TwoCharHashTableKey is only used for looking in the symbol |
| 7992 | // table, not for adding to it. |
| 7993 | UNREACHABLE(); |
| 7994 | return NULL; |
| 7995 | } |
| 7996 | private: |
| 7997 | uint32_t c1_; |
| 7998 | uint32_t c2_; |
| 7999 | uint32_t hash_; |
| 8000 | }; |
| 8001 | |
| 8002 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8003 | bool SymbolTable::LookupSymbolIfExists(String* string, String** symbol) { |
| 8004 | SymbolKey key(string); |
| 8005 | int entry = FindEntry(&key); |
| 8006 | if (entry == kNotFound) { |
| 8007 | return false; |
| 8008 | } else { |
| 8009 | String* result = String::cast(KeyAt(entry)); |
| 8010 | ASSERT(StringShape(result).IsSymbol()); |
| 8011 | *symbol = result; |
| 8012 | return true; |
| 8013 | } |
| 8014 | } |
| 8015 | |
| 8016 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8017 | bool SymbolTable::LookupTwoCharsSymbolIfExists(uint32_t c1, |
| 8018 | uint32_t c2, |
| 8019 | String** symbol) { |
| 8020 | TwoCharHashTableKey key(c1, c2); |
| 8021 | int entry = FindEntry(&key); |
| 8022 | if (entry == kNotFound) { |
| 8023 | return false; |
| 8024 | } else { |
| 8025 | String* result = String::cast(KeyAt(entry)); |
| 8026 | ASSERT(StringShape(result).IsSymbol()); |
| 8027 | *symbol = result; |
| 8028 | return true; |
| 8029 | } |
| 8030 | } |
| 8031 | |
| 8032 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8033 | Object* SymbolTable::LookupSymbol(Vector<const char> str, Object** s) { |
| 8034 | Utf8SymbolKey key(str); |
| 8035 | return LookupKey(&key, s); |
| 8036 | } |
| 8037 | |
| 8038 | |
| 8039 | Object* SymbolTable::LookupKey(HashTableKey* key, Object** s) { |
| 8040 | int entry = FindEntry(key); |
| 8041 | |
| 8042 | // Symbol already in table. |
| 8043 | if (entry != kNotFound) { |
| 8044 | *s = KeyAt(entry); |
| 8045 | return this; |
| 8046 | } |
| 8047 | |
| 8048 | // Adding new symbol. Grow table if needed. |
| 8049 | Object* obj = EnsureCapacity(1, key); |
| 8050 | if (obj->IsFailure()) return obj; |
| 8051 | |
| 8052 | // Create symbol object. |
| 8053 | Object* symbol = key->AsObject(); |
| 8054 | if (symbol->IsFailure()) return symbol; |
| 8055 | |
| 8056 | // If the symbol table grew as part of EnsureCapacity, obj is not |
| 8057 | // the current symbol table and therefore we cannot use |
| 8058 | // SymbolTable::cast here. |
| 8059 | SymbolTable* table = reinterpret_cast<SymbolTable*>(obj); |
| 8060 | |
| 8061 | // Add the new symbol and return it along with the symbol table. |
| 8062 | entry = table->FindInsertionEntry(key->Hash()); |
| 8063 | table->set(EntryToIndex(entry), symbol); |
| 8064 | table->ElementAdded(); |
| 8065 | *s = symbol; |
| 8066 | return table; |
| 8067 | } |
| 8068 | |
| 8069 | |
| 8070 | Object* CompilationCacheTable::Lookup(String* src) { |
| 8071 | StringKey key(src); |
| 8072 | int entry = FindEntry(&key); |
| 8073 | if (entry == kNotFound) return Heap::undefined_value(); |
| 8074 | return get(EntryToIndex(entry) + 1); |
| 8075 | } |
| 8076 | |
| 8077 | |
| 8078 | Object* CompilationCacheTable::LookupEval(String* src, Context* context) { |
| 8079 | StringSharedKey key(src, context->closure()->shared()); |
| 8080 | int entry = FindEntry(&key); |
| 8081 | if (entry == kNotFound) return Heap::undefined_value(); |
| 8082 | return get(EntryToIndex(entry) + 1); |
| 8083 | } |
| 8084 | |
| 8085 | |
| 8086 | Object* CompilationCacheTable::LookupRegExp(String* src, |
| 8087 | JSRegExp::Flags flags) { |
| 8088 | RegExpKey key(src, flags); |
| 8089 | int entry = FindEntry(&key); |
| 8090 | if (entry == kNotFound) return Heap::undefined_value(); |
| 8091 | return get(EntryToIndex(entry) + 1); |
| 8092 | } |
| 8093 | |
| 8094 | |
| 8095 | Object* CompilationCacheTable::Put(String* src, Object* value) { |
| 8096 | StringKey key(src); |
| 8097 | Object* obj = EnsureCapacity(1, &key); |
| 8098 | if (obj->IsFailure()) return obj; |
| 8099 | |
| 8100 | CompilationCacheTable* cache = |
| 8101 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 8102 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 8103 | cache->set(EntryToIndex(entry), src); |
| 8104 | cache->set(EntryToIndex(entry) + 1, value); |
| 8105 | cache->ElementAdded(); |
| 8106 | return cache; |
| 8107 | } |
| 8108 | |
| 8109 | |
| 8110 | Object* CompilationCacheTable::PutEval(String* src, |
| 8111 | Context* context, |
| 8112 | Object* value) { |
| 8113 | StringSharedKey key(src, context->closure()->shared()); |
| 8114 | Object* obj = EnsureCapacity(1, &key); |
| 8115 | if (obj->IsFailure()) return obj; |
| 8116 | |
| 8117 | CompilationCacheTable* cache = |
| 8118 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 8119 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 8120 | |
| 8121 | Object* k = key.AsObject(); |
| 8122 | if (k->IsFailure()) return k; |
| 8123 | |
| 8124 | cache->set(EntryToIndex(entry), k); |
| 8125 | cache->set(EntryToIndex(entry) + 1, value); |
| 8126 | cache->ElementAdded(); |
| 8127 | return cache; |
| 8128 | } |
| 8129 | |
| 8130 | |
| 8131 | Object* CompilationCacheTable::PutRegExp(String* src, |
| 8132 | JSRegExp::Flags flags, |
| 8133 | FixedArray* value) { |
| 8134 | RegExpKey key(src, flags); |
| 8135 | Object* obj = EnsureCapacity(1, &key); |
| 8136 | if (obj->IsFailure()) return obj; |
| 8137 | |
| 8138 | CompilationCacheTable* cache = |
| 8139 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 8140 | int entry = cache->FindInsertionEntry(key.Hash()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8141 | // We store the value in the key slot, and compare the search key |
| 8142 | // to the stored value with a custon IsMatch function during lookups. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8143 | cache->set(EntryToIndex(entry), value); |
| 8144 | cache->set(EntryToIndex(entry) + 1, value); |
| 8145 | cache->ElementAdded(); |
| 8146 | return cache; |
| 8147 | } |
| 8148 | |
| 8149 | |
| 8150 | // SymbolsKey used for HashTable where key is array of symbols. |
| 8151 | class SymbolsKey : public HashTableKey { |
| 8152 | public: |
| 8153 | explicit SymbolsKey(FixedArray* symbols) : symbols_(symbols) { } |
| 8154 | |
| 8155 | bool IsMatch(Object* symbols) { |
| 8156 | FixedArray* o = FixedArray::cast(symbols); |
| 8157 | int len = symbols_->length(); |
| 8158 | if (o->length() != len) return false; |
| 8159 | for (int i = 0; i < len; i++) { |
| 8160 | if (o->get(i) != symbols_->get(i)) return false; |
| 8161 | } |
| 8162 | return true; |
| 8163 | } |
| 8164 | |
| 8165 | uint32_t Hash() { return HashForObject(symbols_); } |
| 8166 | |
| 8167 | uint32_t HashForObject(Object* obj) { |
| 8168 | FixedArray* symbols = FixedArray::cast(obj); |
| 8169 | int len = symbols->length(); |
| 8170 | uint32_t hash = 0; |
| 8171 | for (int i = 0; i < len; i++) { |
| 8172 | hash ^= String::cast(symbols->get(i))->Hash(); |
| 8173 | } |
| 8174 | return hash; |
| 8175 | } |
| 8176 | |
| 8177 | Object* AsObject() { return symbols_; } |
| 8178 | |
| 8179 | private: |
| 8180 | FixedArray* symbols_; |
| 8181 | }; |
| 8182 | |
| 8183 | |
| 8184 | Object* MapCache::Lookup(FixedArray* array) { |
| 8185 | SymbolsKey key(array); |
| 8186 | int entry = FindEntry(&key); |
| 8187 | if (entry == kNotFound) return Heap::undefined_value(); |
| 8188 | return get(EntryToIndex(entry) + 1); |
| 8189 | } |
| 8190 | |
| 8191 | |
| 8192 | Object* MapCache::Put(FixedArray* array, Map* value) { |
| 8193 | SymbolsKey key(array); |
| 8194 | Object* obj = EnsureCapacity(1, &key); |
| 8195 | if (obj->IsFailure()) return obj; |
| 8196 | |
| 8197 | MapCache* cache = reinterpret_cast<MapCache*>(obj); |
| 8198 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 8199 | cache->set(EntryToIndex(entry), array); |
| 8200 | cache->set(EntryToIndex(entry) + 1, value); |
| 8201 | cache->ElementAdded(); |
| 8202 | return cache; |
| 8203 | } |
| 8204 | |
| 8205 | |
| 8206 | template<typename Shape, typename Key> |
| 8207 | Object* Dictionary<Shape, Key>::Allocate(int at_least_space_for) { |
| 8208 | Object* obj = HashTable<Shape, Key>::Allocate(at_least_space_for); |
| 8209 | // Initialize the next enumeration index. |
| 8210 | if (!obj->IsFailure()) { |
| 8211 | Dictionary<Shape, Key>::cast(obj)-> |
| 8212 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
| 8213 | } |
| 8214 | return obj; |
| 8215 | } |
| 8216 | |
| 8217 | |
| 8218 | template<typename Shape, typename Key> |
| 8219 | Object* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() { |
| 8220 | int length = HashTable<Shape, Key>::NumberOfElements(); |
| 8221 | |
| 8222 | // Allocate and initialize iteration order array. |
| 8223 | Object* obj = Heap::AllocateFixedArray(length); |
| 8224 | if (obj->IsFailure()) return obj; |
| 8225 | FixedArray* iteration_order = FixedArray::cast(obj); |
| 8226 | for (int i = 0; i < length; i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8227 | iteration_order->set(i, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8228 | } |
| 8229 | |
| 8230 | // Allocate array with enumeration order. |
| 8231 | obj = Heap::AllocateFixedArray(length); |
| 8232 | if (obj->IsFailure()) return obj; |
| 8233 | FixedArray* enumeration_order = FixedArray::cast(obj); |
| 8234 | |
| 8235 | // Fill the enumeration order array with property details. |
| 8236 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 8237 | int pos = 0; |
| 8238 | for (int i = 0; i < capacity; i++) { |
| 8239 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8240 | enumeration_order->set(pos++, Smi::FromInt(DetailsAt(i).index())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8241 | } |
| 8242 | } |
| 8243 | |
| 8244 | // Sort the arrays wrt. enumeration order. |
| 8245 | iteration_order->SortPairs(enumeration_order, enumeration_order->length()); |
| 8246 | |
| 8247 | // Overwrite the enumeration_order with the enumeration indices. |
| 8248 | for (int i = 0; i < length; i++) { |
| 8249 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 8250 | int enum_index = PropertyDetails::kInitialIndex + i; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8251 | enumeration_order->set(index, Smi::FromInt(enum_index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8252 | } |
| 8253 | |
| 8254 | // Update the dictionary with new indices. |
| 8255 | capacity = HashTable<Shape, Key>::Capacity(); |
| 8256 | pos = 0; |
| 8257 | for (int i = 0; i < capacity; i++) { |
| 8258 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
| 8259 | int enum_index = Smi::cast(enumeration_order->get(pos++))->value(); |
| 8260 | PropertyDetails details = DetailsAt(i); |
| 8261 | PropertyDetails new_details = |
| 8262 | PropertyDetails(details.attributes(), details.type(), enum_index); |
| 8263 | DetailsAtPut(i, new_details); |
| 8264 | } |
| 8265 | } |
| 8266 | |
| 8267 | // Set the next enumeration index. |
| 8268 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
| 8269 | return this; |
| 8270 | } |
| 8271 | |
| 8272 | template<typename Shape, typename Key> |
| 8273 | Object* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) { |
| 8274 | // Check whether there are enough enumeration indices to add n elements. |
| 8275 | if (Shape::kIsEnumerable && |
| 8276 | !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) { |
| 8277 | // If not, we generate new indices for the properties. |
| 8278 | Object* result = GenerateNewEnumerationIndices(); |
| 8279 | if (result->IsFailure()) return result; |
| 8280 | } |
| 8281 | return HashTable<Shape, Key>::EnsureCapacity(n, key); |
| 8282 | } |
| 8283 | |
| 8284 | |
| 8285 | void NumberDictionary::RemoveNumberEntries(uint32_t from, uint32_t to) { |
| 8286 | // Do nothing if the interval [from, to) is empty. |
| 8287 | if (from >= to) return; |
| 8288 | |
| 8289 | int removed_entries = 0; |
| 8290 | Object* sentinel = Heap::null_value(); |
| 8291 | int capacity = Capacity(); |
| 8292 | for (int i = 0; i < capacity; i++) { |
| 8293 | Object* key = KeyAt(i); |
| 8294 | if (key->IsNumber()) { |
| 8295 | uint32_t number = static_cast<uint32_t>(key->Number()); |
| 8296 | if (from <= number && number < to) { |
| 8297 | SetEntry(i, sentinel, sentinel, Smi::FromInt(0)); |
| 8298 | removed_entries++; |
| 8299 | } |
| 8300 | } |
| 8301 | } |
| 8302 | |
| 8303 | // Update the number of elements. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8304 | ElementsRemoved(removed_entries); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
| 8307 | |
| 8308 | template<typename Shape, typename Key> |
| 8309 | Object* Dictionary<Shape, Key>::DeleteProperty(int entry, |
| 8310 | JSObject::DeleteMode mode) { |
| 8311 | PropertyDetails details = DetailsAt(entry); |
| 8312 | // Ignore attributes if forcing a deletion. |
| 8313 | if (details.IsDontDelete() && mode == JSObject::NORMAL_DELETION) { |
| 8314 | return Heap::false_value(); |
| 8315 | } |
| 8316 | SetEntry(entry, Heap::null_value(), Heap::null_value(), Smi::FromInt(0)); |
| 8317 | HashTable<Shape, Key>::ElementRemoved(); |
| 8318 | return Heap::true_value(); |
| 8319 | } |
| 8320 | |
| 8321 | |
| 8322 | template<typename Shape, typename Key> |
| 8323 | Object* Dictionary<Shape, Key>::AtPut(Key key, Object* value) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8324 | int entry = this->FindEntry(key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8325 | |
| 8326 | // If the entry is present set the value; |
| 8327 | if (entry != Dictionary<Shape, Key>::kNotFound) { |
| 8328 | ValueAtPut(entry, value); |
| 8329 | return this; |
| 8330 | } |
| 8331 | |
| 8332 | // Check whether the dictionary should be extended. |
| 8333 | Object* obj = EnsureCapacity(1, key); |
| 8334 | if (obj->IsFailure()) return obj; |
| 8335 | |
| 8336 | Object* k = Shape::AsObject(key); |
| 8337 | if (k->IsFailure()) return k; |
| 8338 | PropertyDetails details = PropertyDetails(NONE, NORMAL); |
| 8339 | return Dictionary<Shape, Key>::cast(obj)-> |
| 8340 | AddEntry(key, value, details, Shape::Hash(key)); |
| 8341 | } |
| 8342 | |
| 8343 | |
| 8344 | template<typename Shape, typename Key> |
| 8345 | Object* Dictionary<Shape, Key>::Add(Key key, |
| 8346 | Object* value, |
| 8347 | PropertyDetails details) { |
| 8348 | // Valdate key is absent. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8349 | SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8350 | // Check whether the dictionary should be extended. |
| 8351 | Object* obj = EnsureCapacity(1, key); |
| 8352 | if (obj->IsFailure()) return obj; |
| 8353 | return Dictionary<Shape, Key>::cast(obj)-> |
| 8354 | AddEntry(key, value, details, Shape::Hash(key)); |
| 8355 | } |
| 8356 | |
| 8357 | |
| 8358 | // Add a key, value pair to the dictionary. |
| 8359 | template<typename Shape, typename Key> |
| 8360 | Object* Dictionary<Shape, Key>::AddEntry(Key key, |
| 8361 | Object* value, |
| 8362 | PropertyDetails details, |
| 8363 | uint32_t hash) { |
| 8364 | // Compute the key object. |
| 8365 | Object* k = Shape::AsObject(key); |
| 8366 | if (k->IsFailure()) return k; |
| 8367 | |
| 8368 | uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash); |
| 8369 | // Insert element at empty or deleted entry |
| 8370 | if (!details.IsDeleted() && details.index() == 0 && Shape::kIsEnumerable) { |
| 8371 | // Assign an enumeration index to the property and update |
| 8372 | // SetNextEnumerationIndex. |
| 8373 | int index = NextEnumerationIndex(); |
| 8374 | details = PropertyDetails(details.attributes(), details.type(), index); |
| 8375 | SetNextEnumerationIndex(index + 1); |
| 8376 | } |
| 8377 | SetEntry(entry, k, value, details); |
| 8378 | ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber() |
| 8379 | || Dictionary<Shape, Key>::KeyAt(entry)->IsString())); |
| 8380 | HashTable<Shape, Key>::ElementAdded(); |
| 8381 | return this; |
| 8382 | } |
| 8383 | |
| 8384 | |
| 8385 | void NumberDictionary::UpdateMaxNumberKey(uint32_t key) { |
| 8386 | // If the dictionary requires slow elements an element has already |
| 8387 | // been added at a high index. |
| 8388 | if (requires_slow_elements()) return; |
| 8389 | // Check if this index is high enough that we should require slow |
| 8390 | // elements. |
| 8391 | if (key > kRequiresSlowElementsLimit) { |
| 8392 | set_requires_slow_elements(); |
| 8393 | return; |
| 8394 | } |
| 8395 | // Update max key value. |
| 8396 | Object* max_index_object = get(kMaxNumberKeyIndex); |
| 8397 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
| 8398 | FixedArray::set(kMaxNumberKeyIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8399 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8400 | } |
| 8401 | } |
| 8402 | |
| 8403 | |
| 8404 | Object* NumberDictionary::AddNumberEntry(uint32_t key, |
| 8405 | Object* value, |
| 8406 | PropertyDetails details) { |
| 8407 | UpdateMaxNumberKey(key); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 8408 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8409 | return Add(key, value, details); |
| 8410 | } |
| 8411 | |
| 8412 | |
| 8413 | Object* NumberDictionary::AtNumberPut(uint32_t key, Object* value) { |
| 8414 | UpdateMaxNumberKey(key); |
| 8415 | return AtPut(key, value); |
| 8416 | } |
| 8417 | |
| 8418 | |
| 8419 | Object* NumberDictionary::Set(uint32_t key, |
| 8420 | Object* value, |
| 8421 | PropertyDetails details) { |
| 8422 | int entry = FindEntry(key); |
| 8423 | if (entry == kNotFound) return AddNumberEntry(key, value, details); |
| 8424 | // Preserve enumeration index. |
| 8425 | details = PropertyDetails(details.attributes(), |
| 8426 | details.type(), |
| 8427 | DetailsAt(entry).index()); |
| 8428 | SetEntry(entry, NumberDictionaryShape::AsObject(key), value, details); |
| 8429 | return this; |
| 8430 | } |
| 8431 | |
| 8432 | |
| 8433 | |
| 8434 | template<typename Shape, typename Key> |
| 8435 | int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes( |
| 8436 | PropertyAttributes filter) { |
| 8437 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 8438 | int result = 0; |
| 8439 | for (int i = 0; i < capacity; i++) { |
| 8440 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 8441 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 8442 | PropertyDetails details = DetailsAt(i); |
| 8443 | if (details.IsDeleted()) continue; |
| 8444 | PropertyAttributes attr = details.attributes(); |
| 8445 | if ((attr & filter) == 0) result++; |
| 8446 | } |
| 8447 | } |
| 8448 | return result; |
| 8449 | } |
| 8450 | |
| 8451 | |
| 8452 | template<typename Shape, typename Key> |
| 8453 | int Dictionary<Shape, Key>::NumberOfEnumElements() { |
| 8454 | return NumberOfElementsFilterAttributes( |
| 8455 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 8456 | } |
| 8457 | |
| 8458 | |
| 8459 | template<typename Shape, typename Key> |
| 8460 | void Dictionary<Shape, Key>::CopyKeysTo(FixedArray* storage, |
| 8461 | PropertyAttributes filter) { |
| 8462 | ASSERT(storage->length() >= NumberOfEnumElements()); |
| 8463 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 8464 | int index = 0; |
| 8465 | for (int i = 0; i < capacity; i++) { |
| 8466 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 8467 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 8468 | PropertyDetails details = DetailsAt(i); |
| 8469 | if (details.IsDeleted()) continue; |
| 8470 | PropertyAttributes attr = details.attributes(); |
| 8471 | if ((attr & filter) == 0) storage->set(index++, k); |
| 8472 | } |
| 8473 | } |
| 8474 | storage->SortPairs(storage, index); |
| 8475 | ASSERT(storage->length() >= index); |
| 8476 | } |
| 8477 | |
| 8478 | |
| 8479 | void StringDictionary::CopyEnumKeysTo(FixedArray* storage, |
| 8480 | FixedArray* sort_array) { |
| 8481 | ASSERT(storage->length() >= NumberOfEnumElements()); |
| 8482 | int capacity = Capacity(); |
| 8483 | int index = 0; |
| 8484 | for (int i = 0; i < capacity; i++) { |
| 8485 | Object* k = KeyAt(i); |
| 8486 | if (IsKey(k)) { |
| 8487 | PropertyDetails details = DetailsAt(i); |
| 8488 | if (details.IsDeleted() || details.IsDontEnum()) continue; |
| 8489 | storage->set(index, k); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8490 | sort_array->set(index, Smi::FromInt(details.index())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8491 | index++; |
| 8492 | } |
| 8493 | } |
| 8494 | storage->SortPairs(sort_array, sort_array->length()); |
| 8495 | ASSERT(storage->length() >= index); |
| 8496 | } |
| 8497 | |
| 8498 | |
| 8499 | template<typename Shape, typename Key> |
| 8500 | void Dictionary<Shape, Key>::CopyKeysTo(FixedArray* storage) { |
| 8501 | ASSERT(storage->length() >= NumberOfElementsFilterAttributes( |
| 8502 | static_cast<PropertyAttributes>(NONE))); |
| 8503 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 8504 | int index = 0; |
| 8505 | for (int i = 0; i < capacity; i++) { |
| 8506 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 8507 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 8508 | PropertyDetails details = DetailsAt(i); |
| 8509 | if (details.IsDeleted()) continue; |
| 8510 | storage->set(index++, k); |
| 8511 | } |
| 8512 | } |
| 8513 | ASSERT(storage->length() >= index); |
| 8514 | } |
| 8515 | |
| 8516 | |
| 8517 | // Backwards lookup (slow). |
| 8518 | template<typename Shape, typename Key> |
| 8519 | Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) { |
| 8520 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 8521 | for (int i = 0; i < capacity; i++) { |
| 8522 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 8523 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 8524 | Object* e = ValueAt(i); |
| 8525 | if (e->IsJSGlobalPropertyCell()) { |
| 8526 | e = JSGlobalPropertyCell::cast(e)->value(); |
| 8527 | } |
| 8528 | if (e == value) return k; |
| 8529 | } |
| 8530 | } |
| 8531 | return Heap::undefined_value(); |
| 8532 | } |
| 8533 | |
| 8534 | |
| 8535 | Object* StringDictionary::TransformPropertiesToFastFor( |
| 8536 | JSObject* obj, int unused_property_fields) { |
| 8537 | // Make sure we preserve dictionary representation if there are too many |
| 8538 | // descriptors. |
| 8539 | if (NumberOfElements() > DescriptorArray::kMaxNumberOfDescriptors) return obj; |
| 8540 | |
| 8541 | // Figure out if it is necessary to generate new enumeration indices. |
| 8542 | int max_enumeration_index = |
| 8543 | NextEnumerationIndex() + |
| 8544 | (DescriptorArray::kMaxNumberOfDescriptors - |
| 8545 | NumberOfElements()); |
| 8546 | if (!PropertyDetails::IsValidIndex(max_enumeration_index)) { |
| 8547 | Object* result = GenerateNewEnumerationIndices(); |
| 8548 | if (result->IsFailure()) return result; |
| 8549 | } |
| 8550 | |
| 8551 | int instance_descriptor_length = 0; |
| 8552 | int number_of_fields = 0; |
| 8553 | |
| 8554 | // Compute the length of the instance descriptor. |
| 8555 | int capacity = Capacity(); |
| 8556 | for (int i = 0; i < capacity; i++) { |
| 8557 | Object* k = KeyAt(i); |
| 8558 | if (IsKey(k)) { |
| 8559 | Object* value = ValueAt(i); |
| 8560 | PropertyType type = DetailsAt(i).type(); |
| 8561 | ASSERT(type != FIELD); |
| 8562 | instance_descriptor_length++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8563 | if (type == NORMAL && |
| 8564 | (!value->IsJSFunction() || Heap::InNewSpace(value))) { |
| 8565 | number_of_fields += 1; |
| 8566 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8567 | } |
| 8568 | } |
| 8569 | |
| 8570 | // Allocate the instance descriptor. |
| 8571 | Object* descriptors_unchecked = |
| 8572 | DescriptorArray::Allocate(instance_descriptor_length); |
| 8573 | if (descriptors_unchecked->IsFailure()) return descriptors_unchecked; |
| 8574 | DescriptorArray* descriptors = DescriptorArray::cast(descriptors_unchecked); |
| 8575 | |
| 8576 | int inobject_props = obj->map()->inobject_properties(); |
| 8577 | int number_of_allocated_fields = |
| 8578 | number_of_fields + unused_property_fields - inobject_props; |
| 8579 | |
| 8580 | // Allocate the fixed array for the fields. |
| 8581 | Object* fields = Heap::AllocateFixedArray(number_of_allocated_fields); |
| 8582 | if (fields->IsFailure()) return fields; |
| 8583 | |
| 8584 | // Fill in the instance descriptor and the fields. |
| 8585 | int next_descriptor = 0; |
| 8586 | int current_offset = 0; |
| 8587 | for (int i = 0; i < capacity; i++) { |
| 8588 | Object* k = KeyAt(i); |
| 8589 | if (IsKey(k)) { |
| 8590 | Object* value = ValueAt(i); |
| 8591 | // Ensure the key is a symbol before writing into the instance descriptor. |
| 8592 | Object* key = Heap::LookupSymbol(String::cast(k)); |
| 8593 | if (key->IsFailure()) return key; |
| 8594 | PropertyDetails details = DetailsAt(i); |
| 8595 | PropertyType type = details.type(); |
| 8596 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8597 | if (value->IsJSFunction() && !Heap::InNewSpace(value)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8598 | ConstantFunctionDescriptor d(String::cast(key), |
| 8599 | JSFunction::cast(value), |
| 8600 | details.attributes(), |
| 8601 | details.index()); |
| 8602 | descriptors->Set(next_descriptor++, &d); |
| 8603 | } else if (type == NORMAL) { |
| 8604 | if (current_offset < inobject_props) { |
| 8605 | obj->InObjectPropertyAtPut(current_offset, |
| 8606 | value, |
| 8607 | UPDATE_WRITE_BARRIER); |
| 8608 | } else { |
| 8609 | int offset = current_offset - inobject_props; |
| 8610 | FixedArray::cast(fields)->set(offset, value); |
| 8611 | } |
| 8612 | FieldDescriptor d(String::cast(key), |
| 8613 | current_offset++, |
| 8614 | details.attributes(), |
| 8615 | details.index()); |
| 8616 | descriptors->Set(next_descriptor++, &d); |
| 8617 | } else if (type == CALLBACKS) { |
| 8618 | CallbacksDescriptor d(String::cast(key), |
| 8619 | value, |
| 8620 | details.attributes(), |
| 8621 | details.index()); |
| 8622 | descriptors->Set(next_descriptor++, &d); |
| 8623 | } else { |
| 8624 | UNREACHABLE(); |
| 8625 | } |
| 8626 | } |
| 8627 | } |
| 8628 | ASSERT(current_offset == number_of_fields); |
| 8629 | |
| 8630 | descriptors->Sort(); |
| 8631 | // Allocate new map. |
| 8632 | Object* new_map = obj->map()->CopyDropDescriptors(); |
| 8633 | if (new_map->IsFailure()) return new_map; |
| 8634 | |
| 8635 | // Transform the object. |
| 8636 | obj->set_map(Map::cast(new_map)); |
| 8637 | obj->map()->set_instance_descriptors(descriptors); |
| 8638 | obj->map()->set_unused_property_fields(unused_property_fields); |
| 8639 | |
| 8640 | obj->set_properties(FixedArray::cast(fields)); |
| 8641 | ASSERT(obj->IsJSObject()); |
| 8642 | |
| 8643 | descriptors->SetNextEnumerationIndex(NextEnumerationIndex()); |
| 8644 | // Check that it really works. |
| 8645 | ASSERT(obj->HasFastProperties()); |
| 8646 | |
| 8647 | return obj; |
| 8648 | } |
| 8649 | |
| 8650 | |
| 8651 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 8652 | // Check if there is a break point at this code position. |
| 8653 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 8654 | // Get the break point info object for this code position. |
| 8655 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 8656 | |
| 8657 | // If there is no break point info object or no break points in the break |
| 8658 | // point info object there is no break point at this code position. |
| 8659 | if (break_point_info->IsUndefined()) return false; |
| 8660 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 8661 | } |
| 8662 | |
| 8663 | |
| 8664 | // Get the break point info object for this code position. |
| 8665 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 8666 | // Find the index of the break point info object for this code position. |
| 8667 | int index = GetBreakPointInfoIndex(code_position); |
| 8668 | |
| 8669 | // Return the break point info object if any. |
| 8670 | if (index == kNoBreakPointInfo) return Heap::undefined_value(); |
| 8671 | return BreakPointInfo::cast(break_points()->get(index)); |
| 8672 | } |
| 8673 | |
| 8674 | |
| 8675 | // Clear a break point at the specified code position. |
| 8676 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 8677 | int code_position, |
| 8678 | Handle<Object> break_point_object) { |
| 8679 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position)); |
| 8680 | if (break_point_info->IsUndefined()) return; |
| 8681 | BreakPointInfo::ClearBreakPoint( |
| 8682 | Handle<BreakPointInfo>::cast(break_point_info), |
| 8683 | break_point_object); |
| 8684 | } |
| 8685 | |
| 8686 | |
| 8687 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 8688 | int code_position, |
| 8689 | int source_position, |
| 8690 | int statement_position, |
| 8691 | Handle<Object> break_point_object) { |
| 8692 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position)); |
| 8693 | if (!break_point_info->IsUndefined()) { |
| 8694 | BreakPointInfo::SetBreakPoint( |
| 8695 | Handle<BreakPointInfo>::cast(break_point_info), |
| 8696 | break_point_object); |
| 8697 | return; |
| 8698 | } |
| 8699 | |
| 8700 | // Adding a new break point for a code position which did not have any |
| 8701 | // break points before. Try to find a free slot. |
| 8702 | int index = kNoBreakPointInfo; |
| 8703 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 8704 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 8705 | index = i; |
| 8706 | break; |
| 8707 | } |
| 8708 | } |
| 8709 | if (index == kNoBreakPointInfo) { |
| 8710 | // No free slot - extend break point info array. |
| 8711 | Handle<FixedArray> old_break_points = |
| 8712 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
| 8713 | debug_info->set_break_points(*Factory::NewFixedArray( |
| 8714 | old_break_points->length() + |
| 8715 | Debug::kEstimatedNofBreakPointsInFunction)); |
| 8716 | Handle<FixedArray> new_break_points = |
| 8717 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
| 8718 | for (int i = 0; i < old_break_points->length(); i++) { |
| 8719 | new_break_points->set(i, old_break_points->get(i)); |
| 8720 | } |
| 8721 | index = old_break_points->length(); |
| 8722 | } |
| 8723 | ASSERT(index != kNoBreakPointInfo); |
| 8724 | |
| 8725 | // Allocate new BreakPointInfo object and set the break point. |
| 8726 | Handle<BreakPointInfo> new_break_point_info = |
| 8727 | Handle<BreakPointInfo>::cast(Factory::NewStruct(BREAK_POINT_INFO_TYPE)); |
| 8728 | new_break_point_info->set_code_position(Smi::FromInt(code_position)); |
| 8729 | new_break_point_info->set_source_position(Smi::FromInt(source_position)); |
| 8730 | new_break_point_info-> |
| 8731 | set_statement_position(Smi::FromInt(statement_position)); |
| 8732 | new_break_point_info->set_break_point_objects(Heap::undefined_value()); |
| 8733 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 8734 | debug_info->break_points()->set(index, *new_break_point_info); |
| 8735 | } |
| 8736 | |
| 8737 | |
| 8738 | // Get the break point objects for a code position. |
| 8739 | Object* DebugInfo::GetBreakPointObjects(int code_position) { |
| 8740 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 8741 | if (break_point_info->IsUndefined()) { |
| 8742 | return Heap::undefined_value(); |
| 8743 | } |
| 8744 | return BreakPointInfo::cast(break_point_info)->break_point_objects(); |
| 8745 | } |
| 8746 | |
| 8747 | |
| 8748 | // Get the total number of break points. |
| 8749 | int DebugInfo::GetBreakPointCount() { |
| 8750 | if (break_points()->IsUndefined()) return 0; |
| 8751 | int count = 0; |
| 8752 | for (int i = 0; i < break_points()->length(); i++) { |
| 8753 | if (!break_points()->get(i)->IsUndefined()) { |
| 8754 | BreakPointInfo* break_point_info = |
| 8755 | BreakPointInfo::cast(break_points()->get(i)); |
| 8756 | count += break_point_info->GetBreakPointCount(); |
| 8757 | } |
| 8758 | } |
| 8759 | return count; |
| 8760 | } |
| 8761 | |
| 8762 | |
| 8763 | Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 8764 | Handle<Object> break_point_object) { |
| 8765 | if (debug_info->break_points()->IsUndefined()) return Heap::undefined_value(); |
| 8766 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 8767 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 8768 | Handle<BreakPointInfo> break_point_info = |
| 8769 | Handle<BreakPointInfo>(BreakPointInfo::cast( |
| 8770 | debug_info->break_points()->get(i))); |
| 8771 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 8772 | break_point_object)) { |
| 8773 | return *break_point_info; |
| 8774 | } |
| 8775 | } |
| 8776 | } |
| 8777 | return Heap::undefined_value(); |
| 8778 | } |
| 8779 | |
| 8780 | |
| 8781 | // Find the index of the break point info object for the specified code |
| 8782 | // position. |
| 8783 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 8784 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 8785 | for (int i = 0; i < break_points()->length(); i++) { |
| 8786 | if (!break_points()->get(i)->IsUndefined()) { |
| 8787 | BreakPointInfo* break_point_info = |
| 8788 | BreakPointInfo::cast(break_points()->get(i)); |
| 8789 | if (break_point_info->code_position()->value() == code_position) { |
| 8790 | return i; |
| 8791 | } |
| 8792 | } |
| 8793 | } |
| 8794 | return kNoBreakPointInfo; |
| 8795 | } |
| 8796 | |
| 8797 | |
| 8798 | // Remove the specified break point object. |
| 8799 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 8800 | Handle<Object> break_point_object) { |
| 8801 | // If there are no break points just ignore. |
| 8802 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 8803 | // If there is a single break point clear it if it is the same. |
| 8804 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 8805 | if (break_point_info->break_point_objects() == *break_point_object) { |
| 8806 | break_point_info->set_break_point_objects(Heap::undefined_value()); |
| 8807 | } |
| 8808 | return; |
| 8809 | } |
| 8810 | // If there are multiple break points shrink the array |
| 8811 | ASSERT(break_point_info->break_point_objects()->IsFixedArray()); |
| 8812 | Handle<FixedArray> old_array = |
| 8813 | Handle<FixedArray>( |
| 8814 | FixedArray::cast(break_point_info->break_point_objects())); |
| 8815 | Handle<FixedArray> new_array = |
| 8816 | Factory::NewFixedArray(old_array->length() - 1); |
| 8817 | int found_count = 0; |
| 8818 | for (int i = 0; i < old_array->length(); i++) { |
| 8819 | if (old_array->get(i) == *break_point_object) { |
| 8820 | ASSERT(found_count == 0); |
| 8821 | found_count++; |
| 8822 | } else { |
| 8823 | new_array->set(i - found_count, old_array->get(i)); |
| 8824 | } |
| 8825 | } |
| 8826 | // If the break point was found in the list change it. |
| 8827 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 8828 | } |
| 8829 | |
| 8830 | |
| 8831 | // Add the specified break point object. |
| 8832 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 8833 | Handle<Object> break_point_object) { |
| 8834 | // If there was no break point objects before just set it. |
| 8835 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 8836 | break_point_info->set_break_point_objects(*break_point_object); |
| 8837 | return; |
| 8838 | } |
| 8839 | // If the break point object is the same as before just ignore. |
| 8840 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 8841 | // If there was one break point object before replace with array. |
| 8842 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 8843 | Handle<FixedArray> array = Factory::NewFixedArray(2); |
| 8844 | array->set(0, break_point_info->break_point_objects()); |
| 8845 | array->set(1, *break_point_object); |
| 8846 | break_point_info->set_break_point_objects(*array); |
| 8847 | return; |
| 8848 | } |
| 8849 | // If there was more than one break point before extend array. |
| 8850 | Handle<FixedArray> old_array = |
| 8851 | Handle<FixedArray>( |
| 8852 | FixedArray::cast(break_point_info->break_point_objects())); |
| 8853 | Handle<FixedArray> new_array = |
| 8854 | Factory::NewFixedArray(old_array->length() + 1); |
| 8855 | for (int i = 0; i < old_array->length(); i++) { |
| 8856 | // If the break point was there before just ignore. |
| 8857 | if (old_array->get(i) == *break_point_object) return; |
| 8858 | new_array->set(i, old_array->get(i)); |
| 8859 | } |
| 8860 | // Add the new break point. |
| 8861 | new_array->set(old_array->length(), *break_point_object); |
| 8862 | break_point_info->set_break_point_objects(*new_array); |
| 8863 | } |
| 8864 | |
| 8865 | |
| 8866 | bool BreakPointInfo::HasBreakPointObject( |
| 8867 | Handle<BreakPointInfo> break_point_info, |
| 8868 | Handle<Object> break_point_object) { |
| 8869 | // No break point. |
| 8870 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
| 8871 | // Single beak point. |
| 8872 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 8873 | return break_point_info->break_point_objects() == *break_point_object; |
| 8874 | } |
| 8875 | // Multiple break points. |
| 8876 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 8877 | for (int i = 0; i < array->length(); i++) { |
| 8878 | if (array->get(i) == *break_point_object) { |
| 8879 | return true; |
| 8880 | } |
| 8881 | } |
| 8882 | return false; |
| 8883 | } |
| 8884 | |
| 8885 | |
| 8886 | // Get the number of break points. |
| 8887 | int BreakPointInfo::GetBreakPointCount() { |
| 8888 | // No break point. |
| 8889 | if (break_point_objects()->IsUndefined()) return 0; |
| 8890 | // Single beak point. |
| 8891 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 8892 | // Multiple break points. |
| 8893 | return FixedArray::cast(break_point_objects())->length(); |
| 8894 | } |
| 8895 | #endif |
| 8896 | |
| 8897 | |
| 8898 | } } // namespace v8::internal |