Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "api.h" |
| 31 | #include "arguments.h" |
| 32 | #include "bootstrapper.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 33 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "debug.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 35 | #include "deoptimizer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | #include "execution.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 37 | #include "full-codegen.h" |
| 38 | #include "hydrogen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | #include "objects-inl.h" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 40 | #include "objects-visiting.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | #include "macro-assembler.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 42 | #include "safepoint-table.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 43 | #include "scanner-base.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #include "scopeinfo.h" |
| 45 | #include "string-stream.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 46 | #include "utils.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 47 | #include "vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 50 | #include "disasm.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | #include "disassembler.h" |
| 52 | #endif |
| 53 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | namespace v8 { |
| 55 | namespace internal { |
| 56 | |
| 57 | // Getters and setters are stored in a fixed array property. These are |
| 58 | // constants for their indices. |
| 59 | const int kGetterIndex = 0; |
| 60 | const int kSetterIndex = 1; |
| 61 | |
| 62 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 63 | MUST_USE_RESULT static MaybeObject* CreateJSValue(JSFunction* constructor, |
| 64 | Object* value) { |
| 65 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 66 | { MaybeObject* maybe_result = |
| 67 | constructor->GetHeap()->AllocateJSObject(constructor); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 68 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 69 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | JSValue::cast(result)->set_value(value); |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 75 | MaybeObject* Object::ToObject(Context* global_context) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | if (IsNumber()) { |
| 77 | return CreateJSValue(global_context->number_function(), this); |
| 78 | } else if (IsBoolean()) { |
| 79 | return CreateJSValue(global_context->boolean_function(), this); |
| 80 | } else if (IsString()) { |
| 81 | return CreateJSValue(global_context->string_function(), this); |
| 82 | } |
| 83 | ASSERT(IsJSObject()); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 88 | MaybeObject* Object::ToObject() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | if (IsJSObject()) { |
| 90 | return this; |
| 91 | } else if (IsNumber()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 92 | Isolate* isolate = Isolate::Current(); |
| 93 | Context* global_context = isolate->context()->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | return CreateJSValue(global_context->number_function(), this); |
| 95 | } else if (IsBoolean()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 96 | Isolate* isolate = HeapObject::cast(this)->GetIsolate(); |
| 97 | Context* global_context = isolate->context()->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 98 | return CreateJSValue(global_context->boolean_function(), this); |
| 99 | } else if (IsString()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 100 | Isolate* isolate = HeapObject::cast(this)->GetIsolate(); |
| 101 | Context* global_context = isolate->context()->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 102 | return CreateJSValue(global_context->string_function(), this); |
| 103 | } |
| 104 | |
| 105 | // Throw a type error. |
| 106 | return Failure::InternalError(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | Object* Object::ToBoolean() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 111 | if (IsTrue()) return this; |
| 112 | if (IsFalse()) return this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | if (IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | return Isolate::Current()->heap()->ToBoolean(Smi::cast(this)->value() != 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 116 | HeapObject* heap_object = HeapObject::cast(this); |
| 117 | if (heap_object->IsUndefined() || heap_object->IsNull()) { |
| 118 | return heap_object->GetHeap()->false_value(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 119 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | // Undetectable object is false |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 121 | if (heap_object->IsUndetectableObject()) { |
| 122 | return heap_object->GetHeap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 123 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 124 | if (heap_object->IsString()) { |
| 125 | return heap_object->GetHeap()->ToBoolean( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 126 | String::cast(this)->length() != 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 127 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 128 | if (heap_object->IsHeapNumber()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | return HeapNumber::cast(this)->HeapNumberToBoolean(); |
| 130 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 131 | return heap_object->GetHeap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | void Object::Lookup(String* name, LookupResult* result) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | Object* holder = NULL; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 137 | if (IsSmi()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 138 | Heap* heap = Isolate::Current()->heap(); |
| 139 | Context* global_context = heap->isolate()->context()->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | holder = global_context->number_function()->instance_prototype(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 141 | } else { |
| 142 | HeapObject* heap_object = HeapObject::cast(this); |
| 143 | if (heap_object->IsJSObject()) { |
| 144 | return JSObject::cast(this)->Lookup(name, result); |
| 145 | } |
| 146 | Heap* heap = heap_object->GetHeap(); |
| 147 | if (heap_object->IsString()) { |
| 148 | Context* global_context = heap->isolate()->context()->global_context(); |
| 149 | holder = global_context->string_function()->instance_prototype(); |
| 150 | } else if (heap_object->IsHeapNumber()) { |
| 151 | Context* global_context = heap->isolate()->context()->global_context(); |
| 152 | holder = global_context->number_function()->instance_prototype(); |
| 153 | } else if (heap_object->IsBoolean()) { |
| 154 | Context* global_context = heap->isolate()->context()->global_context(); |
| 155 | holder = global_context->boolean_function()->instance_prototype(); |
| 156 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | } |
| 158 | ASSERT(holder != NULL); // Cannot handle null or undefined. |
| 159 | JSObject::cast(holder)->Lookup(name, result); |
| 160 | } |
| 161 | |
| 162 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 163 | MaybeObject* Object::GetPropertyWithReceiver(Object* receiver, |
| 164 | String* name, |
| 165 | PropertyAttributes* attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 166 | LookupResult result; |
| 167 | Lookup(name, &result); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 168 | MaybeObject* value = GetProperty(receiver, &result, name, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | ASSERT(*attributes <= ABSENT); |
| 170 | return value; |
| 171 | } |
| 172 | |
| 173 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 174 | MaybeObject* Object::GetPropertyWithCallback(Object* receiver, |
| 175 | Object* structure, |
| 176 | String* name, |
| 177 | Object* holder) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 178 | Isolate* isolate = name->GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | // To accommodate both the old and the new api we switch on the |
| 180 | // data structure used to store the callbacks. Eventually proxy |
| 181 | // callbacks should be phased out. |
| 182 | if (structure->IsProxy()) { |
| 183 | AccessorDescriptor* callback = |
| 184 | reinterpret_cast<AccessorDescriptor*>(Proxy::cast(structure)->proxy()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 185 | MaybeObject* value = (callback->getter)(receiver, callback->data); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 186 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 187 | return value; |
| 188 | } |
| 189 | |
| 190 | // api style callbacks. |
| 191 | if (structure->IsAccessorInfo()) { |
| 192 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 193 | Object* fun_obj = data->getter(); |
| 194 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
| 195 | HandleScope scope; |
| 196 | JSObject* self = JSObject::cast(receiver); |
| 197 | JSObject* holder_handle = JSObject::cast(holder); |
| 198 | Handle<String> key(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 199 | LOG(isolate, ApiNamedPropertyAccess("load", self, name)); |
| 200 | CustomArguments args(isolate, data->data(), self, holder_handle); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | v8::AccessorInfo info(args.end()); |
| 202 | v8::Handle<v8::Value> result; |
| 203 | { |
| 204 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 205 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 206 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 207 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 208 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 209 | if (result.IsEmpty()) { |
| 210 | return isolate->heap()->undefined_value(); |
| 211 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | return *v8::Utils::OpenHandle(*result); |
| 213 | } |
| 214 | |
| 215 | // __defineGetter__ callback |
| 216 | if (structure->IsFixedArray()) { |
| 217 | Object* getter = FixedArray::cast(structure)->get(kGetterIndex); |
| 218 | if (getter->IsJSFunction()) { |
| 219 | return Object::GetPropertyWithDefinedGetter(receiver, |
| 220 | JSFunction::cast(getter)); |
| 221 | } |
| 222 | // Getter is not a function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 223 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | UNREACHABLE(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 227 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 231 | MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver, |
| 232 | JSFunction* getter) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | HandleScope scope; |
| 234 | Handle<JSFunction> fun(JSFunction::cast(getter)); |
| 235 | Handle<Object> self(receiver); |
| 236 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 237 | Debug* debug = fun->GetHeap()->isolate()->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 238 | // Handle stepping into a getter if step into is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 239 | if (debug->StepInActive()) { |
| 240 | debug->HandleStepIn(fun, Handle<Object>::null(), 0, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | } |
| 242 | #endif |
| 243 | bool has_pending_exception; |
| 244 | Handle<Object> result = |
| 245 | Execution::Call(fun, self, 0, NULL, &has_pending_exception); |
| 246 | // Check for pending exception and return the result. |
| 247 | if (has_pending_exception) return Failure::Exception(); |
| 248 | return *result; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | // Only deal with CALLBACKS and INTERCEPTOR |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 253 | MaybeObject* JSObject::GetPropertyWithFailedAccessCheck( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 254 | Object* receiver, |
| 255 | LookupResult* result, |
| 256 | String* name, |
| 257 | PropertyAttributes* attributes) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 258 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 259 | switch (result->type()) { |
| 260 | case CALLBACKS: { |
| 261 | // Only allow API accessors. |
| 262 | Object* obj = result->GetCallbackObject(); |
| 263 | if (obj->IsAccessorInfo()) { |
| 264 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 265 | if (info->all_can_read()) { |
| 266 | *attributes = result->GetAttributes(); |
| 267 | return GetPropertyWithCallback(receiver, |
| 268 | result->GetCallbackObject(), |
| 269 | name, |
| 270 | result->holder()); |
| 271 | } |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | case NORMAL: |
| 276 | case FIELD: |
| 277 | case CONSTANT_FUNCTION: { |
| 278 | // Search ALL_CAN_READ accessors in prototype chain. |
| 279 | LookupResult r; |
| 280 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 281 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | return GetPropertyWithFailedAccessCheck(receiver, |
| 283 | &r, |
| 284 | name, |
| 285 | attributes); |
| 286 | } |
| 287 | break; |
| 288 | } |
| 289 | case INTERCEPTOR: { |
| 290 | // If the object has an interceptor, try real named properties. |
| 291 | // No access check in GetPropertyAttributeWithInterceptor. |
| 292 | LookupResult r; |
| 293 | result->holder()->LookupRealNamedProperty(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 294 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | return GetPropertyWithFailedAccessCheck(receiver, |
| 296 | &r, |
| 297 | name, |
| 298 | attributes); |
| 299 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | break; |
| 301 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 302 | default: |
| 303 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | // No accessible property found. |
| 308 | *attributes = ABSENT; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 309 | Heap* heap = name->GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 310 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_GET); |
| 311 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck( |
| 316 | Object* receiver, |
| 317 | LookupResult* result, |
| 318 | String* name, |
| 319 | bool continue_search) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 320 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 321 | switch (result->type()) { |
| 322 | case CALLBACKS: { |
| 323 | // Only allow API accessors. |
| 324 | Object* obj = result->GetCallbackObject(); |
| 325 | if (obj->IsAccessorInfo()) { |
| 326 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 327 | if (info->all_can_read()) { |
| 328 | return result->GetAttributes(); |
| 329 | } |
| 330 | } |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | case NORMAL: |
| 335 | case FIELD: |
| 336 | case CONSTANT_FUNCTION: { |
| 337 | if (!continue_search) break; |
| 338 | // Search ALL_CAN_READ accessors in prototype chain. |
| 339 | LookupResult r; |
| 340 | result->holder()->LookupRealNamedPropertyInPrototypes(name, &r); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 341 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 343 | &r, |
| 344 | name, |
| 345 | continue_search); |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | case INTERCEPTOR: { |
| 351 | // If the object has an interceptor, try real named properties. |
| 352 | // No access check in GetPropertyAttributeWithInterceptor. |
| 353 | LookupResult r; |
| 354 | if (continue_search) { |
| 355 | result->holder()->LookupRealNamedProperty(name, &r); |
| 356 | } else { |
| 357 | result->holder()->LocalLookupRealNamedProperty(name, &r); |
| 358 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 359 | if (r.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 360 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 361 | &r, |
| 362 | name, |
| 363 | continue_search); |
| 364 | } |
| 365 | break; |
| 366 | } |
| 367 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 368 | default: |
| 369 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 373 | GetHeap()->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | return ABSENT; |
| 375 | } |
| 376 | |
| 377 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | Object* JSObject::GetNormalizedProperty(LookupResult* result) { |
| 379 | ASSERT(!HasFastProperties()); |
| 380 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
| 381 | if (IsGlobalObject()) { |
| 382 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 383 | } |
| 384 | ASSERT(!value->IsJSGlobalPropertyCell()); |
| 385 | return value; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | Object* JSObject::SetNormalizedProperty(LookupResult* result, Object* value) { |
| 390 | ASSERT(!HasFastProperties()); |
| 391 | if (IsGlobalObject()) { |
| 392 | JSGlobalPropertyCell* cell = |
| 393 | JSGlobalPropertyCell::cast( |
| 394 | property_dictionary()->ValueAt(result->GetDictionaryEntry())); |
| 395 | cell->set_value(value); |
| 396 | } else { |
| 397 | property_dictionary()->ValueAtPut(result->GetDictionaryEntry(), value); |
| 398 | } |
| 399 | return value; |
| 400 | } |
| 401 | |
| 402 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 403 | MaybeObject* JSObject::SetNormalizedProperty(String* name, |
| 404 | Object* value, |
| 405 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | ASSERT(!HasFastProperties()); |
| 407 | int entry = property_dictionary()->FindEntry(name); |
| 408 | if (entry == StringDictionary::kNotFound) { |
| 409 | Object* store_value = value; |
| 410 | if (IsGlobalObject()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 411 | Heap* heap = name->GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 412 | MaybeObject* maybe_store_value = |
| 413 | heap->AllocateJSGlobalPropertyCell(value); |
| 414 | if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 415 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 416 | Object* dict; |
| 417 | { MaybeObject* maybe_dict = |
| 418 | property_dictionary()->Add(name, store_value, details); |
| 419 | if (!maybe_dict->ToObject(&dict)) return maybe_dict; |
| 420 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | set_properties(StringDictionary::cast(dict)); |
| 422 | return value; |
| 423 | } |
| 424 | // Preserve enumeration index. |
| 425 | details = PropertyDetails(details.attributes(), |
| 426 | details.type(), |
| 427 | property_dictionary()->DetailsAt(entry).index()); |
| 428 | if (IsGlobalObject()) { |
| 429 | JSGlobalPropertyCell* cell = |
| 430 | JSGlobalPropertyCell::cast(property_dictionary()->ValueAt(entry)); |
| 431 | cell->set_value(value); |
| 432 | // Please note we have to update the property details. |
| 433 | property_dictionary()->DetailsAtPut(entry, details); |
| 434 | } else { |
| 435 | property_dictionary()->SetEntry(entry, name, value, details); |
| 436 | } |
| 437 | return value; |
| 438 | } |
| 439 | |
| 440 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 441 | MaybeObject* JSObject::DeleteNormalizedProperty(String* name, DeleteMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | ASSERT(!HasFastProperties()); |
| 443 | StringDictionary* dictionary = property_dictionary(); |
| 444 | int entry = dictionary->FindEntry(name); |
| 445 | if (entry != StringDictionary::kNotFound) { |
| 446 | // If we have a global object set the cell to the hole. |
| 447 | if (IsGlobalObject()) { |
| 448 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 449 | if (details.IsDontDelete()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 450 | if (mode != FORCE_DELETION) return GetHeap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | // When forced to delete global properties, we have to make a |
| 452 | // map change to invalidate any ICs that think they can load |
| 453 | // from the DontDelete cell without checking if it contains |
| 454 | // the hole value. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 455 | Object* new_map; |
| 456 | { MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); |
| 457 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 458 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 459 | set_map(Map::cast(new_map)); |
| 460 | } |
| 461 | JSGlobalPropertyCell* cell = |
| 462 | JSGlobalPropertyCell::cast(dictionary->ValueAt(entry)); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 463 | cell->set_value(cell->heap()->the_hole_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 464 | dictionary->DetailsAtPut(entry, details.AsDeleted()); |
| 465 | } else { |
| 466 | return dictionary->DeleteProperty(entry, mode); |
| 467 | } |
| 468 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 469 | return GetHeap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | bool JSObject::IsDirty() { |
| 474 | Object* cons_obj = map()->constructor(); |
| 475 | if (!cons_obj->IsJSFunction()) |
| 476 | return true; |
| 477 | JSFunction* fun = JSFunction::cast(cons_obj); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 478 | if (!fun->shared()->IsApiFunction()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 479 | return true; |
| 480 | // If the object is fully fast case and has the same map it was |
| 481 | // created with then no changes can have been made to it. |
| 482 | return map() != fun->initial_map() |
| 483 | || !HasFastElements() |
| 484 | || !HasFastProperties(); |
| 485 | } |
| 486 | |
| 487 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 488 | MaybeObject* Object::GetProperty(Object* receiver, |
| 489 | LookupResult* result, |
| 490 | String* name, |
| 491 | PropertyAttributes* attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 492 | // Make sure that the top context does not change when doing |
| 493 | // callbacks or interceptor calls. |
| 494 | AssertNoContextChange ncc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | Heap* heap = name->GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | |
| 497 | // Traverse the prototype chain from the current object (this) to |
| 498 | // the holder and check for access rights. This avoid traversing the |
| 499 | // objects more than once in case of interceptors, because the |
| 500 | // holder will always be the interceptor holder and the search may |
| 501 | // only continue with a current object just after the interceptor |
| 502 | // holder in the prototype chain. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 503 | Object* last = result->IsProperty() ? result->holder() : heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 504 | for (Object* current = this; true; current = current->GetPrototype()) { |
| 505 | if (current->IsAccessCheckNeeded()) { |
| 506 | // Check if we're allowed to read from the current object. Note |
| 507 | // that even though we may not actually end up loading the named |
| 508 | // property from the current object, we still check that we have |
| 509 | // access to it. |
| 510 | JSObject* checked = JSObject::cast(current); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 511 | if (!heap->isolate()->MayNamedAccess(checked, name, v8::ACCESS_GET)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | return checked->GetPropertyWithFailedAccessCheck(receiver, |
| 513 | result, |
| 514 | name, |
| 515 | attributes); |
| 516 | } |
| 517 | } |
| 518 | // Stop traversing the chain once we reach the last object in the |
| 519 | // chain; either the holder of the result or null in case of an |
| 520 | // absent property. |
| 521 | if (current == last) break; |
| 522 | } |
| 523 | |
| 524 | if (!result->IsProperty()) { |
| 525 | *attributes = ABSENT; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 526 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | } |
| 528 | *attributes = result->GetAttributes(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | Object* value; |
| 530 | JSObject* holder = result->holder(); |
| 531 | switch (result->type()) { |
| 532 | case NORMAL: |
| 533 | value = holder->GetNormalizedProperty(result); |
| 534 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 535 | return value->IsTheHole() ? heap->undefined_value() : value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 536 | case FIELD: |
| 537 | value = holder->FastPropertyAt(result->GetFieldIndex()); |
| 538 | ASSERT(!value->IsTheHole() || result->IsReadOnly()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 539 | return value->IsTheHole() ? heap->undefined_value() : value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 540 | case CONSTANT_FUNCTION: |
| 541 | return result->GetConstantFunction(); |
| 542 | case CALLBACKS: |
| 543 | return GetPropertyWithCallback(receiver, |
| 544 | result->GetCallbackObject(), |
| 545 | name, |
| 546 | holder); |
| 547 | case INTERCEPTOR: { |
| 548 | JSObject* recvr = JSObject::cast(receiver); |
| 549 | return holder->GetPropertyWithInterceptor(recvr, name, attributes); |
| 550 | } |
| 551 | default: |
| 552 | UNREACHABLE(); |
| 553 | return NULL; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 558 | MaybeObject* Object::GetElementWithReceiver(Object* receiver, uint32_t index) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 559 | Object* holder = NULL; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 560 | if (IsSmi()) { |
| 561 | Context* global_context = Isolate::Current()->context()->global_context(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 562 | holder = global_context->number_function()->instance_prototype(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 563 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 564 | HeapObject* heap_object = HeapObject::cast(this); |
| 565 | |
| 566 | if (heap_object->IsJSObject()) { |
| 567 | return JSObject::cast(this)->GetElementWithReceiver(receiver, index); |
| 568 | } |
| 569 | Heap* heap = heap_object->GetHeap(); |
| 570 | Isolate* isolate = heap->isolate(); |
| 571 | |
| 572 | Context* global_context = isolate->context()->global_context(); |
| 573 | if (heap_object->IsString()) { |
| 574 | holder = global_context->string_function()->instance_prototype(); |
| 575 | } else if (heap_object->IsHeapNumber()) { |
| 576 | holder = global_context->number_function()->instance_prototype(); |
| 577 | } else if (heap_object->IsBoolean()) { |
| 578 | holder = global_context->boolean_function()->instance_prototype(); |
| 579 | } else { |
| 580 | // Undefined and null have no indexed properties. |
| 581 | ASSERT(heap_object->IsUndefined() || heap_object->IsNull()); |
| 582 | return heap->undefined_value(); |
| 583 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | return JSObject::cast(holder)->GetElementWithReceiver(receiver, index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
| 590 | Object* Object::GetPrototype() { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 591 | if (IsSmi()) { |
| 592 | Heap* heap = Isolate::Current()->heap(); |
| 593 | Context* context = heap->isolate()->context()->global_context(); |
| 594 | return context->number_function()->instance_prototype(); |
| 595 | } |
| 596 | |
| 597 | HeapObject* heap_object = HeapObject::cast(this); |
| 598 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 599 | // The object is either a number, a string, a boolean, or a real JS object. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 600 | if (heap_object->IsJSObject()) { |
| 601 | return JSObject::cast(this)->map()->prototype(); |
| 602 | } |
| 603 | Heap* heap = heap_object->GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 604 | Context* context = heap->isolate()->context()->global_context(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 605 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 606 | if (heap_object->IsHeapNumber()) { |
| 607 | return context->number_function()->instance_prototype(); |
| 608 | } |
| 609 | if (heap_object->IsString()) { |
| 610 | return context->string_function()->instance_prototype(); |
| 611 | } |
| 612 | if (heap_object->IsBoolean()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 613 | return context->boolean_function()->instance_prototype(); |
| 614 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | return heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 620 | void Object::ShortPrint(FILE* out) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 621 | HeapStringAllocator allocator; |
| 622 | StringStream accumulator(&allocator); |
| 623 | ShortPrint(&accumulator); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 624 | accumulator.OutputToFile(out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | void Object::ShortPrint(StringStream* accumulator) { |
| 629 | if (IsSmi()) { |
| 630 | Smi::cast(this)->SmiPrint(accumulator); |
| 631 | } else if (IsFailure()) { |
| 632 | Failure::cast(this)->FailurePrint(accumulator); |
| 633 | } else { |
| 634 | HeapObject::cast(this)->HeapObjectShortPrint(accumulator); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 639 | void Smi::SmiPrint(FILE* out) { |
| 640 | PrintF(out, "%d", value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
| 644 | void Smi::SmiPrint(StringStream* accumulator) { |
| 645 | accumulator->Add("%d", value()); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | void Failure::FailurePrint(StringStream* accumulator) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 650 | accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 654 | void Failure::FailurePrint(FILE* out) { |
| 655 | PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 659 | // Should a word be prefixed by 'a' or 'an' in order to read naturally in |
| 660 | // English? Returns false for non-ASCII or words that don't start with |
| 661 | // a capital letter. The a/an rule follows pronunciation in English. |
| 662 | // We don't use the BBC's overcorrect "an historic occasion" though if |
| 663 | // you speak a dialect you may well say "an 'istoric occasion". |
| 664 | static bool AnWord(String* str) { |
| 665 | if (str->length() == 0) return false; // A nothing. |
| 666 | int c0 = str->Get(0); |
| 667 | int c1 = str->length() > 1 ? str->Get(1) : 0; |
| 668 | if (c0 == 'U') { |
| 669 | if (c1 > 'Z') { |
| 670 | return true; // An Umpire, but a UTF8String, a U. |
| 671 | } |
| 672 | } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') { |
| 673 | return true; // An Ape, an ABCBook. |
| 674 | } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) && |
| 675 | (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' || |
| 676 | c0 == 'S' || c0 == 'X')) { |
| 677 | return true; // An MP3File, an M. |
| 678 | } |
| 679 | return false; |
| 680 | } |
| 681 | |
| 682 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 683 | MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 684 | #ifdef DEBUG |
| 685 | // Do not attempt to flatten in debug mode when allocation is not |
| 686 | // allowed. This is to avoid an assertion failure when allocating. |
| 687 | // Flattening strings is the only case where we always allow |
| 688 | // allocation because no GC is performed if the allocation fails. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 689 | if (!HEAP->IsAllocationAllowed()) return this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 690 | #endif |
| 691 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 692 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | switch (StringShape(this).representation_tag()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 694 | case kConsStringTag: { |
| 695 | ConsString* cs = ConsString::cast(this); |
| 696 | if (cs->second()->length() == 0) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 697 | return cs->first(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 698 | } |
| 699 | // There's little point in putting the flat string in new space if the |
| 700 | // cons string is in old space. It can never get GCed until there is |
| 701 | // an old space GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 702 | PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 703 | int len = length(); |
| 704 | Object* object; |
| 705 | String* result; |
| 706 | if (IsAsciiRepresentation()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 707 | { MaybeObject* maybe_object = heap->AllocateRawAsciiString(len, tenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 708 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 709 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 710 | result = String::cast(object); |
| 711 | String* first = cs->first(); |
| 712 | int first_length = first->length(); |
| 713 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 714 | WriteToFlat(first, dest, 0, first_length); |
| 715 | String* second = cs->second(); |
| 716 | WriteToFlat(second, |
| 717 | dest + first_length, |
| 718 | 0, |
| 719 | len - first_length); |
| 720 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 721 | { MaybeObject* maybe_object = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 722 | heap->AllocateRawTwoByteString(len, tenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 723 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 724 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 725 | result = String::cast(object); |
| 726 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 727 | String* first = cs->first(); |
| 728 | int first_length = first->length(); |
| 729 | WriteToFlat(first, dest, 0, first_length); |
| 730 | String* second = cs->second(); |
| 731 | WriteToFlat(second, |
| 732 | dest + first_length, |
| 733 | 0, |
| 734 | len - first_length); |
| 735 | } |
| 736 | cs->set_first(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 737 | cs->set_second(heap->empty_string()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 738 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 739 | } |
| 740 | default: |
| 741 | return this; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | |
| 746 | bool String::MakeExternal(v8::String::ExternalStringResource* resource) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 747 | // Externalizing twice leaks the external resource, so it's |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 748 | // prohibited by the API. |
| 749 | ASSERT(!this->IsExternalString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 750 | #ifdef DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 751 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 752 | // Assert that the resource and the string are equivalent. |
| 753 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 754 | ScopedVector<uc16> smart_chars(this->length()); |
| 755 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 756 | ASSERT(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 757 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 758 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 759 | } |
| 760 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 761 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 762 | int size = this->Size(); // Byte size of the original string. |
| 763 | if (size < ExternalString::kSize) { |
| 764 | // The string is too small to fit an external String in its place. This can |
| 765 | // only happen for zero length strings. |
| 766 | return false; |
| 767 | } |
| 768 | ASSERT(size >= ExternalString::kSize); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 769 | bool is_ascii = this->IsAsciiRepresentation(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | bool is_symbol = this->IsSymbol(); |
| 771 | int length = this->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 772 | int hash_field = this->hash_field(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | |
| 774 | // Morph the object to an external string by adjusting the map and |
| 775 | // reinitializing the fields. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 776 | this->set_map(is_ascii ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 777 | heap->external_string_with_ascii_data_map() : |
| 778 | heap->external_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 779 | ExternalTwoByteString* self = ExternalTwoByteString::cast(this); |
| 780 | self->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 781 | self->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 782 | self->set_resource(resource); |
| 783 | // Additionally make the object into an external symbol if the original string |
| 784 | // was a symbol to start with. |
| 785 | if (is_symbol) { |
| 786 | self->Hash(); // Force regeneration of the hash value. |
| 787 | // Now morph this external string into a external symbol. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 788 | this->set_map(is_ascii ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 789 | heap->external_symbol_with_ascii_data_map() : |
| 790 | heap->external_symbol_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Fill the remainder of the string with dead wood. |
| 794 | int new_size = this->Size(); // Byte size of the external String object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 795 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { |
| 801 | #ifdef DEBUG |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 802 | if (FLAG_enable_slow_asserts) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 803 | // Assert that the resource and the string are equivalent. |
| 804 | ASSERT(static_cast<size_t>(this->length()) == resource->length()); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 805 | ScopedVector<char> smart_chars(this->length()); |
| 806 | String::WriteToFlat(this, smart_chars.start(), 0, this->length()); |
| 807 | ASSERT(memcmp(smart_chars.start(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 808 | resource->data(), |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 809 | resource->length() * sizeof(smart_chars[0])) == 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 810 | } |
| 811 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 812 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 813 | int size = this->Size(); // Byte size of the original string. |
| 814 | if (size < ExternalString::kSize) { |
| 815 | // The string is too small to fit an external String in its place. This can |
| 816 | // only happen for zero length strings. |
| 817 | return false; |
| 818 | } |
| 819 | ASSERT(size >= ExternalString::kSize); |
| 820 | bool is_symbol = this->IsSymbol(); |
| 821 | int length = this->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 822 | int hash_field = this->hash_field(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 823 | |
| 824 | // Morph the object to an external string by adjusting the map and |
| 825 | // reinitializing the fields. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 826 | this->set_map(heap->external_ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 827 | ExternalAsciiString* self = ExternalAsciiString::cast(this); |
| 828 | self->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 829 | self->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 830 | self->set_resource(resource); |
| 831 | // Additionally make the object into an external symbol if the original string |
| 832 | // was a symbol to start with. |
| 833 | if (is_symbol) { |
| 834 | self->Hash(); // Force regeneration of the hash value. |
| 835 | // Now morph this external string into a external symbol. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 836 | this->set_map(heap->external_ascii_symbol_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // Fill the remainder of the string with dead wood. |
| 840 | int new_size = this->Size(); // Byte size of the external String object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 841 | heap->CreateFillerObjectAt(this->address() + new_size, size - new_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 842 | return true; |
| 843 | } |
| 844 | |
| 845 | |
| 846 | void String::StringShortPrint(StringStream* accumulator) { |
| 847 | int len = length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 848 | if (len > kMaxShortPrintLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 849 | accumulator->Add("<Very long string[%u]>", len); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | if (!LooksValid()) { |
| 854 | accumulator->Add("<Invalid String>"); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | StringInputBuffer buf(this); |
| 859 | |
| 860 | bool truncated = false; |
| 861 | if (len > kMaxShortPrintLength) { |
| 862 | len = kMaxShortPrintLength; |
| 863 | truncated = true; |
| 864 | } |
| 865 | bool ascii = true; |
| 866 | for (int i = 0; i < len; i++) { |
| 867 | int c = buf.GetNext(); |
| 868 | |
| 869 | if (c < 32 || c >= 127) { |
| 870 | ascii = false; |
| 871 | } |
| 872 | } |
| 873 | buf.Reset(this); |
| 874 | if (ascii) { |
| 875 | accumulator->Add("<String[%u]: ", length()); |
| 876 | for (int i = 0; i < len; i++) { |
| 877 | accumulator->Put(buf.GetNext()); |
| 878 | } |
| 879 | accumulator->Put('>'); |
| 880 | } else { |
| 881 | // Backslash indicates that the string contains control |
| 882 | // characters and that backslashes are therefore escaped. |
| 883 | accumulator->Add("<String[%u]\\: ", length()); |
| 884 | for (int i = 0; i < len; i++) { |
| 885 | int c = buf.GetNext(); |
| 886 | if (c == '\n') { |
| 887 | accumulator->Add("\\n"); |
| 888 | } else if (c == '\r') { |
| 889 | accumulator->Add("\\r"); |
| 890 | } else if (c == '\\') { |
| 891 | accumulator->Add("\\\\"); |
| 892 | } else if (c < 32 || c > 126) { |
| 893 | accumulator->Add("\\x%02x", c); |
| 894 | } else { |
| 895 | accumulator->Put(c); |
| 896 | } |
| 897 | } |
| 898 | if (truncated) { |
| 899 | accumulator->Put('.'); |
| 900 | accumulator->Put('.'); |
| 901 | accumulator->Put('.'); |
| 902 | } |
| 903 | accumulator->Put('>'); |
| 904 | } |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | |
| 909 | void JSObject::JSObjectShortPrint(StringStream* accumulator) { |
| 910 | switch (map()->instance_type()) { |
| 911 | case JS_ARRAY_TYPE: { |
| 912 | double length = JSArray::cast(this)->length()->Number(); |
| 913 | accumulator->Add("<JS array[%u]>", static_cast<uint32_t>(length)); |
| 914 | break; |
| 915 | } |
| 916 | case JS_REGEXP_TYPE: { |
| 917 | accumulator->Add("<JS RegExp>"); |
| 918 | break; |
| 919 | } |
| 920 | case JS_FUNCTION_TYPE: { |
| 921 | Object* fun_name = JSFunction::cast(this)->shared()->name(); |
| 922 | bool printed = false; |
| 923 | if (fun_name->IsString()) { |
| 924 | String* str = String::cast(fun_name); |
| 925 | if (str->length() > 0) { |
| 926 | accumulator->Add("<JS Function "); |
| 927 | accumulator->Put(str); |
| 928 | accumulator->Put('>'); |
| 929 | printed = true; |
| 930 | } |
| 931 | } |
| 932 | if (!printed) { |
| 933 | accumulator->Add("<JS Function>"); |
| 934 | } |
| 935 | break; |
| 936 | } |
| 937 | // All other JSObjects are rather similar to each other (JSObject, |
| 938 | // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue). |
| 939 | default: { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 940 | Map* map_of_this = map(); |
| 941 | Heap* heap = map_of_this->heap(); |
| 942 | Object* constructor = map_of_this->constructor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 943 | bool printed = false; |
| 944 | if (constructor->IsHeapObject() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 945 | !heap->Contains(HeapObject::cast(constructor))) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 946 | accumulator->Add("!!!INVALID CONSTRUCTOR!!!"); |
| 947 | } else { |
| 948 | bool global_object = IsJSGlobalProxy(); |
| 949 | if (constructor->IsJSFunction()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 950 | if (!heap->Contains(JSFunction::cast(constructor)->shared())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 951 | accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!"); |
| 952 | } else { |
| 953 | Object* constructor_name = |
| 954 | JSFunction::cast(constructor)->shared()->name(); |
| 955 | if (constructor_name->IsString()) { |
| 956 | String* str = String::cast(constructor_name); |
| 957 | if (str->length() > 0) { |
| 958 | bool vowel = AnWord(str); |
| 959 | accumulator->Add("<%sa%s ", |
| 960 | global_object ? "Global Object: " : "", |
| 961 | vowel ? "n" : ""); |
| 962 | accumulator->Put(str); |
| 963 | accumulator->Put('>'); |
| 964 | printed = true; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | if (!printed) { |
| 970 | accumulator->Add("<JS %sObject", global_object ? "Global " : ""); |
| 971 | } |
| 972 | } |
| 973 | if (IsJSValue()) { |
| 974 | accumulator->Add(" value = "); |
| 975 | JSValue::cast(this)->value()->ShortPrint(accumulator); |
| 976 | } |
| 977 | accumulator->Put('>'); |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | |
| 984 | void HeapObject::HeapObjectShortPrint(StringStream* accumulator) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 985 | // if (!HEAP->InNewSpace(this)) PrintF("*", this); |
| 986 | Heap* heap = GetHeap(); |
| 987 | if (!heap->Contains(this)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | accumulator->Add("!!!INVALID POINTER!!!"); |
| 989 | return; |
| 990 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 991 | if (!heap->Contains(map())) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 992 | accumulator->Add("!!!INVALID MAP!!!"); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | accumulator->Add("%p ", this); |
| 997 | |
| 998 | if (IsString()) { |
| 999 | String::cast(this)->StringShortPrint(accumulator); |
| 1000 | return; |
| 1001 | } |
| 1002 | if (IsJSObject()) { |
| 1003 | JSObject::cast(this)->JSObjectShortPrint(accumulator); |
| 1004 | return; |
| 1005 | } |
| 1006 | switch (map()->instance_type()) { |
| 1007 | case MAP_TYPE: |
| 1008 | accumulator->Add("<Map>"); |
| 1009 | break; |
| 1010 | case FIXED_ARRAY_TYPE: |
| 1011 | accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length()); |
| 1012 | break; |
| 1013 | case BYTE_ARRAY_TYPE: |
| 1014 | accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length()); |
| 1015 | break; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1016 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
| 1017 | accumulator->Add("<ExternalPixelArray[%u]>", |
| 1018 | ExternalPixelArray::cast(this)->length()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1019 | break; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1020 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1021 | accumulator->Add("<ExternalByteArray[%u]>", |
| 1022 | ExternalByteArray::cast(this)->length()); |
| 1023 | break; |
| 1024 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1025 | accumulator->Add("<ExternalUnsignedByteArray[%u]>", |
| 1026 | ExternalUnsignedByteArray::cast(this)->length()); |
| 1027 | break; |
| 1028 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1029 | accumulator->Add("<ExternalShortArray[%u]>", |
| 1030 | ExternalShortArray::cast(this)->length()); |
| 1031 | break; |
| 1032 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1033 | accumulator->Add("<ExternalUnsignedShortArray[%u]>", |
| 1034 | ExternalUnsignedShortArray::cast(this)->length()); |
| 1035 | break; |
| 1036 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1037 | accumulator->Add("<ExternalIntArray[%u]>", |
| 1038 | ExternalIntArray::cast(this)->length()); |
| 1039 | break; |
| 1040 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1041 | accumulator->Add("<ExternalUnsignedIntArray[%u]>", |
| 1042 | ExternalUnsignedIntArray::cast(this)->length()); |
| 1043 | break; |
| 1044 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
| 1045 | accumulator->Add("<ExternalFloatArray[%u]>", |
| 1046 | ExternalFloatArray::cast(this)->length()); |
| 1047 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1048 | case SHARED_FUNCTION_INFO_TYPE: |
| 1049 | accumulator->Add("<SharedFunctionInfo>"); |
| 1050 | break; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1051 | case JS_MESSAGE_OBJECT_TYPE: |
| 1052 | accumulator->Add("<JSMessageObject>"); |
| 1053 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1054 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1055 | case NAME##_TYPE: \ |
| 1056 | accumulator->Put('<'); \ |
| 1057 | accumulator->Add(#Name); \ |
| 1058 | accumulator->Put('>'); \ |
| 1059 | break; |
| 1060 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1061 | #undef MAKE_STRUCT_CASE |
| 1062 | case CODE_TYPE: |
| 1063 | accumulator->Add("<Code>"); |
| 1064 | break; |
| 1065 | case ODDBALL_TYPE: { |
| 1066 | if (IsUndefined()) |
| 1067 | accumulator->Add("<undefined>"); |
| 1068 | else if (IsTheHole()) |
| 1069 | accumulator->Add("<the hole>"); |
| 1070 | else if (IsNull()) |
| 1071 | accumulator->Add("<null>"); |
| 1072 | else if (IsTrue()) |
| 1073 | accumulator->Add("<true>"); |
| 1074 | else if (IsFalse()) |
| 1075 | accumulator->Add("<false>"); |
| 1076 | else |
| 1077 | accumulator->Add("<Odd Oddball>"); |
| 1078 | break; |
| 1079 | } |
| 1080 | case HEAP_NUMBER_TYPE: |
| 1081 | accumulator->Add("<Number: "); |
| 1082 | HeapNumber::cast(this)->HeapNumberPrint(accumulator); |
| 1083 | accumulator->Put('>'); |
| 1084 | break; |
| 1085 | case PROXY_TYPE: |
| 1086 | accumulator->Add("<Proxy>"); |
| 1087 | break; |
| 1088 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
| 1089 | accumulator->Add("Cell for "); |
| 1090 | JSGlobalPropertyCell::cast(this)->value()->ShortPrint(accumulator); |
| 1091 | break; |
| 1092 | default: |
| 1093 | accumulator->Add("<Other heap object (%d)>", map()->instance_type()); |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1099 | void HeapObject::Iterate(ObjectVisitor* v) { |
| 1100 | // Handle header |
| 1101 | IteratePointer(v, kMapOffset); |
| 1102 | // Handle object body |
| 1103 | Map* m = map(); |
| 1104 | IterateBody(m->instance_type(), SizeFromMap(m), v); |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | void HeapObject::IterateBody(InstanceType type, int object_size, |
| 1109 | ObjectVisitor* v) { |
| 1110 | // Avoiding <Type>::cast(this) because it accesses the map pointer field. |
| 1111 | // During GC, the map pointer field is encoded. |
| 1112 | if (type < FIRST_NONSTRING_TYPE) { |
| 1113 | switch (type & kStringRepresentationMask) { |
| 1114 | case kSeqStringTag: |
| 1115 | break; |
| 1116 | case kConsStringTag: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1117 | ConsString::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1118 | break; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1119 | case kExternalStringTag: |
| 1120 | if ((type & kStringEncodingMask) == kAsciiStringTag) { |
| 1121 | reinterpret_cast<ExternalAsciiString*>(this)-> |
| 1122 | ExternalAsciiStringIterateBody(v); |
| 1123 | } else { |
| 1124 | reinterpret_cast<ExternalTwoByteString*>(this)-> |
| 1125 | ExternalTwoByteStringIterateBody(v); |
| 1126 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | } |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | switch (type) { |
| 1133 | case FIXED_ARRAY_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1134 | FixedArray::BodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | break; |
| 1136 | case JS_OBJECT_TYPE: |
| 1137 | case JS_CONTEXT_EXTENSION_OBJECT_TYPE: |
| 1138 | case JS_VALUE_TYPE: |
| 1139 | case JS_ARRAY_TYPE: |
| 1140 | case JS_REGEXP_TYPE: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1141 | case JS_GLOBAL_PROXY_TYPE: |
| 1142 | case JS_GLOBAL_OBJECT_TYPE: |
| 1143 | case JS_BUILTINS_OBJECT_TYPE: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1144 | case JS_MESSAGE_OBJECT_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1145 | JSObject::BodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1146 | break; |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 1147 | case JS_FUNCTION_TYPE: |
| 1148 | reinterpret_cast<JSFunction*>(this) |
| 1149 | ->JSFunctionIterateBody(object_size, v); |
| 1150 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1151 | case ODDBALL_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1152 | Oddball::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1153 | break; |
| 1154 | case PROXY_TYPE: |
| 1155 | reinterpret_cast<Proxy*>(this)->ProxyIterateBody(v); |
| 1156 | break; |
| 1157 | case MAP_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1158 | Map::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | case CODE_TYPE: |
| 1161 | reinterpret_cast<Code*>(this)->CodeIterateBody(v); |
| 1162 | break; |
| 1163 | case JS_GLOBAL_PROPERTY_CELL_TYPE: |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1164 | JSGlobalPropertyCell::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | case HEAP_NUMBER_TYPE: |
| 1167 | case FILLER_TYPE: |
| 1168 | case BYTE_ARRAY_TYPE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1169 | case EXTERNAL_PIXEL_ARRAY_TYPE: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1170 | case EXTERNAL_BYTE_ARRAY_TYPE: |
| 1171 | case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE: |
| 1172 | case EXTERNAL_SHORT_ARRAY_TYPE: |
| 1173 | case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE: |
| 1174 | case EXTERNAL_INT_ARRAY_TYPE: |
| 1175 | case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE: |
| 1176 | case EXTERNAL_FLOAT_ARRAY_TYPE: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1177 | break; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1178 | case SHARED_FUNCTION_INFO_TYPE: |
| 1179 | SharedFunctionInfo::BodyDescriptor::IterateBody(this, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | break; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1181 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1182 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1183 | case NAME##_TYPE: |
| 1184 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1185 | #undef MAKE_STRUCT_CASE |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1186 | StructBodyDescriptor::IterateBody(this, object_size, v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | default: |
| 1189 | PrintF("Unknown type: %d\n", type); |
| 1190 | UNREACHABLE(); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1195 | Object* HeapNumber::HeapNumberToBoolean() { |
| 1196 | // NaN, +0, and -0 should return the false object |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1197 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 1198 | union IeeeDoubleLittleEndianArchType u; |
| 1199 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 1200 | union IeeeDoubleBigEndianArchType u; |
| 1201 | #endif |
| 1202 | u.d = value(); |
| 1203 | if (u.bits.exp == 2047) { |
| 1204 | // Detect NaN for IEEE double precision floating point. |
| 1205 | if ((u.bits.man_low | u.bits.man_high) != 0) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1206 | return GetHeap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1207 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1208 | if (u.bits.exp == 0) { |
| 1209 | // Detect +0, and -0 for IEEE double precision floating point. |
| 1210 | if ((u.bits.man_low | u.bits.man_high) == 0) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1211 | return GetHeap()->false_value(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1212 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1213 | return GetHeap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1217 | void HeapNumber::HeapNumberPrint(FILE* out) { |
| 1218 | PrintF(out, "%.16g", Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | void HeapNumber::HeapNumberPrint(StringStream* accumulator) { |
| 1223 | // The Windows version of vsnprintf can allocate when printing a %g string |
| 1224 | // into a buffer that may not be big enough. We don't want random memory |
| 1225 | // allocation when producing post-crash stack traces, so we print into a |
| 1226 | // buffer that is plenty big enough for any floating point number, then |
| 1227 | // print that using vsnprintf (which may truncate but never allocate if |
| 1228 | // there is no more space in the buffer). |
| 1229 | EmbeddedVector<char, 100> buffer; |
| 1230 | OS::SNPrintF(buffer, "%.16g", Number()); |
| 1231 | accumulator->Add("%s", buffer.start()); |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | String* JSObject::class_name() { |
| 1236 | if (IsJSFunction()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1237 | return GetHeap()->function_class_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1238 | } |
| 1239 | if (map()->constructor()->IsJSFunction()) { |
| 1240 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1241 | return String::cast(constructor->shared()->instance_class_name()); |
| 1242 | } |
| 1243 | // If the constructor is not present, return "Object". |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1244 | return GetHeap()->Object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | String* JSObject::constructor_name() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1249 | if (map()->constructor()->IsJSFunction()) { |
| 1250 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
| 1251 | String* name = String::cast(constructor->shared()->name()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1252 | if (name->length() > 0) return name; |
| 1253 | String* inferred_name = constructor->shared()->inferred_name(); |
| 1254 | if (inferred_name->length() > 0) return inferred_name; |
| 1255 | Object* proto = GetPrototype(); |
| 1256 | if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1257 | } |
| 1258 | // If the constructor is not present, return "Object". |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1259 | return GetHeap()->Object_symbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1263 | MaybeObject* JSObject::AddFastPropertyUsingMap(Map* new_map, |
| 1264 | String* name, |
| 1265 | Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1266 | int index = new_map->PropertyIndexFor(name); |
| 1267 | if (map()->unused_property_fields() == 0) { |
| 1268 | ASSERT(map()->unused_property_fields() == 0); |
| 1269 | int new_unused = new_map->unused_property_fields(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1270 | Object* values; |
| 1271 | { MaybeObject* maybe_values = |
| 1272 | properties()->CopySize(properties()->length() + new_unused + 1); |
| 1273 | if (!maybe_values->ToObject(&values)) return maybe_values; |
| 1274 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1275 | set_properties(FixedArray::cast(values)); |
| 1276 | } |
| 1277 | set_map(new_map); |
| 1278 | return FastPropertyAtPut(index, value); |
| 1279 | } |
| 1280 | |
| 1281 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1282 | static bool IsIdentifier(UnicodeCache* cache, |
| 1283 | unibrow::CharacterStream* buffer) { |
| 1284 | // Checks whether the buffer contains an identifier (no escape). |
| 1285 | if (!buffer->has_more()) return false; |
| 1286 | if (!cache->IsIdentifierStart(buffer->GetNext())) { |
| 1287 | return false; |
| 1288 | } |
| 1289 | while (buffer->has_more()) { |
| 1290 | if (!cache->IsIdentifierPart(buffer->GetNext())) { |
| 1291 | return false; |
| 1292 | } |
| 1293 | } |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1298 | MaybeObject* JSObject::AddFastProperty(String* name, |
| 1299 | Object* value, |
| 1300 | PropertyAttributes attributes) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1301 | ASSERT(!IsJSGlobalProxy()); |
| 1302 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1303 | // Normalize the object if the name is an actual string (not the |
| 1304 | // hidden symbols) and is not a real identifier. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1305 | Isolate* isolate = GetHeap()->isolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1306 | StringInputBuffer buffer(name); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1307 | if (!IsIdentifier(isolate->unicode_cache(), &buffer) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1308 | && name != isolate->heap()->hidden_symbol()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1309 | Object* obj; |
| 1310 | { MaybeObject* maybe_obj = |
| 1311 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1312 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1313 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1314 | return AddSlowProperty(name, value, attributes); |
| 1315 | } |
| 1316 | |
| 1317 | DescriptorArray* old_descriptors = map()->instance_descriptors(); |
| 1318 | // Compute the new index for new field. |
| 1319 | int index = map()->NextFreePropertyIndex(); |
| 1320 | |
| 1321 | // Allocate new instance descriptors with (name, index) added |
| 1322 | FieldDescriptor new_field(name, index, attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1323 | Object* new_descriptors; |
| 1324 | { MaybeObject* maybe_new_descriptors = |
| 1325 | old_descriptors->CopyInsert(&new_field, REMOVE_TRANSITIONS); |
| 1326 | if (!maybe_new_descriptors->ToObject(&new_descriptors)) { |
| 1327 | return maybe_new_descriptors; |
| 1328 | } |
| 1329 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1330 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1331 | // Only allow map transition if the object isn't the global object and there |
| 1332 | // is not a transition for the name, or there's a transition for the name but |
| 1333 | // it's unrelated to properties. |
| 1334 | int descriptor_index = old_descriptors->Search(name); |
| 1335 | |
| 1336 | // External array transitions are stored in the descriptor for property "", |
| 1337 | // which is not a identifier and should have forced a switch to slow |
| 1338 | // properties above. |
| 1339 | ASSERT(descriptor_index == DescriptorArray::kNotFound || |
| 1340 | old_descriptors->GetType(descriptor_index) != EXTERNAL_ARRAY_TRANSITION); |
| 1341 | bool can_insert_transition = descriptor_index == DescriptorArray::kNotFound || |
| 1342 | old_descriptors->GetType(descriptor_index) == EXTERNAL_ARRAY_TRANSITION; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1343 | bool allow_map_transition = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1344 | can_insert_transition && |
| 1345 | (isolate->context()->global_context()->object_function()->map() != map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1346 | |
| 1347 | ASSERT(index < map()->inobject_properties() || |
| 1348 | (index - map()->inobject_properties()) < properties()->length() || |
| 1349 | map()->unused_property_fields() == 0); |
| 1350 | // Allocate a new map for the object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1351 | Object* r; |
| 1352 | { MaybeObject* maybe_r = map()->CopyDropDescriptors(); |
| 1353 | if (!maybe_r->ToObject(&r)) return maybe_r; |
| 1354 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1355 | Map* new_map = Map::cast(r); |
| 1356 | if (allow_map_transition) { |
| 1357 | // Allocate new instance descriptors for the old map with map transition. |
| 1358 | MapTransitionDescriptor d(name, Map::cast(new_map), attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1359 | Object* r; |
| 1360 | { MaybeObject* maybe_r = old_descriptors->CopyInsert(&d, KEEP_TRANSITIONS); |
| 1361 | if (!maybe_r->ToObject(&r)) return maybe_r; |
| 1362 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1363 | old_descriptors = DescriptorArray::cast(r); |
| 1364 | } |
| 1365 | |
| 1366 | if (map()->unused_property_fields() == 0) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1367 | if (properties()->length() > MaxFastProperties()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1368 | Object* obj; |
| 1369 | { MaybeObject* maybe_obj = |
| 1370 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1371 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1372 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1373 | return AddSlowProperty(name, value, attributes); |
| 1374 | } |
| 1375 | // Make room for the new value |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1376 | Object* values; |
| 1377 | { MaybeObject* maybe_values = |
| 1378 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 1379 | if (!maybe_values->ToObject(&values)) return maybe_values; |
| 1380 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1381 | set_properties(FixedArray::cast(values)); |
| 1382 | new_map->set_unused_property_fields(kFieldsAdded - 1); |
| 1383 | } else { |
| 1384 | new_map->set_unused_property_fields(map()->unused_property_fields() - 1); |
| 1385 | } |
| 1386 | // We have now allocated all the necessary objects. |
| 1387 | // All the changes can be applied at once, so they are atomic. |
| 1388 | map()->set_instance_descriptors(old_descriptors); |
| 1389 | new_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1390 | set_map(new_map); |
| 1391 | return FastPropertyAtPut(index, value); |
| 1392 | } |
| 1393 | |
| 1394 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1395 | MaybeObject* JSObject::AddConstantFunctionProperty( |
| 1396 | String* name, |
| 1397 | JSFunction* function, |
| 1398 | PropertyAttributes attributes) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1399 | ASSERT(!GetHeap()->InNewSpace(function)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1400 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1401 | // Allocate new instance descriptors with (name, function) added |
| 1402 | ConstantFunctionDescriptor d(name, function, attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1403 | Object* new_descriptors; |
| 1404 | { MaybeObject* maybe_new_descriptors = |
| 1405 | map()->instance_descriptors()->CopyInsert(&d, REMOVE_TRANSITIONS); |
| 1406 | if (!maybe_new_descriptors->ToObject(&new_descriptors)) { |
| 1407 | return maybe_new_descriptors; |
| 1408 | } |
| 1409 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1410 | |
| 1411 | // Allocate a new map for the object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1412 | Object* new_map; |
| 1413 | { MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); |
| 1414 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1415 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1416 | |
| 1417 | DescriptorArray* descriptors = DescriptorArray::cast(new_descriptors); |
| 1418 | Map::cast(new_map)->set_instance_descriptors(descriptors); |
| 1419 | Map* old_map = map(); |
| 1420 | set_map(Map::cast(new_map)); |
| 1421 | |
| 1422 | // If the old map is the global object map (from new Object()), |
| 1423 | // then transitions are not added to it, so we are done. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1424 | Heap* heap = old_map->heap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1425 | if (old_map == heap->isolate()->context()->global_context()-> |
| 1426 | object_function()->map()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1427 | return function; |
| 1428 | } |
| 1429 | |
| 1430 | // Do not add CONSTANT_TRANSITIONS to global objects |
| 1431 | if (IsGlobalObject()) { |
| 1432 | return function; |
| 1433 | } |
| 1434 | |
| 1435 | // Add a CONSTANT_TRANSITION descriptor to the old map, |
| 1436 | // so future assignments to this property on other objects |
| 1437 | // of the same type will create a normal field, not a constant function. |
| 1438 | // Don't do this for special properties, with non-trival attributes. |
| 1439 | if (attributes != NONE) { |
| 1440 | return function; |
| 1441 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1442 | ConstTransitionDescriptor mark(name, Map::cast(new_map)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1443 | { MaybeObject* maybe_new_descriptors = |
| 1444 | old_map->instance_descriptors()->CopyInsert(&mark, KEEP_TRANSITIONS); |
| 1445 | if (!maybe_new_descriptors->ToObject(&new_descriptors)) { |
| 1446 | // We have accomplished the main goal, so return success. |
| 1447 | return function; |
| 1448 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1449 | } |
| 1450 | old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1451 | |
| 1452 | return function; |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | // Add property in slow mode |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1457 | MaybeObject* JSObject::AddSlowProperty(String* name, |
| 1458 | Object* value, |
| 1459 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1460 | ASSERT(!HasFastProperties()); |
| 1461 | StringDictionary* dict = property_dictionary(); |
| 1462 | Object* store_value = value; |
| 1463 | if (IsGlobalObject()) { |
| 1464 | // In case name is an orphaned property reuse the cell. |
| 1465 | int entry = dict->FindEntry(name); |
| 1466 | if (entry != StringDictionary::kNotFound) { |
| 1467 | store_value = dict->ValueAt(entry); |
| 1468 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
| 1469 | // Assign an enumeration index to the property and update |
| 1470 | // SetNextEnumerationIndex. |
| 1471 | int index = dict->NextEnumerationIndex(); |
| 1472 | PropertyDetails details = PropertyDetails(attributes, NORMAL, index); |
| 1473 | dict->SetNextEnumerationIndex(index + 1); |
| 1474 | dict->SetEntry(entry, name, store_value, details); |
| 1475 | return value; |
| 1476 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1477 | Heap* heap = GetHeap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1478 | { MaybeObject* maybe_store_value = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1479 | heap->AllocateJSGlobalPropertyCell(value); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1480 | if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value; |
| 1481 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1482 | JSGlobalPropertyCell::cast(store_value)->set_value(value); |
| 1483 | } |
| 1484 | PropertyDetails details = PropertyDetails(attributes, NORMAL); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1485 | Object* result; |
| 1486 | { MaybeObject* maybe_result = dict->Add(name, store_value, details); |
| 1487 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1488 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1489 | if (dict != result) set_properties(StringDictionary::cast(result)); |
| 1490 | return value; |
| 1491 | } |
| 1492 | |
| 1493 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1494 | MaybeObject* JSObject::AddProperty(String* name, |
| 1495 | Object* value, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1496 | PropertyAttributes attributes, |
| 1497 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1498 | ASSERT(!IsJSGlobalProxy()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1499 | Map* map_of_this = map(); |
| 1500 | Heap* heap = map_of_this->heap(); |
| 1501 | if (!map_of_this->is_extensible()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1502 | if (strict_mode == kNonStrictMode) { |
| 1503 | return heap->undefined_value(); |
| 1504 | } else { |
| 1505 | Handle<Object> args[1] = {Handle<String>(name)}; |
| 1506 | return heap->isolate()->Throw( |
| 1507 | *FACTORY->NewTypeError("object_not_extensible", |
| 1508 | HandleVector(args, 1))); |
| 1509 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1510 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1511 | if (HasFastProperties()) { |
| 1512 | // Ensure the descriptor array does not get too big. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1513 | if (map_of_this->instance_descriptors()->number_of_descriptors() < |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1514 | DescriptorArray::kMaxNumberOfDescriptors) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1515 | if (value->IsJSFunction() && !heap->InNewSpace(value)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1516 | return AddConstantFunctionProperty(name, |
| 1517 | JSFunction::cast(value), |
| 1518 | attributes); |
| 1519 | } else { |
| 1520 | return AddFastProperty(name, value, attributes); |
| 1521 | } |
| 1522 | } else { |
| 1523 | // Normalize the object to prevent very large instance descriptors. |
| 1524 | // This eliminates unwanted N^2 allocation and lookup behavior. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1525 | Object* obj; |
| 1526 | { MaybeObject* maybe_obj = |
| 1527 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1528 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1529 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | return AddSlowProperty(name, value, attributes); |
| 1533 | } |
| 1534 | |
| 1535 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1536 | MaybeObject* JSObject::SetPropertyPostInterceptor( |
| 1537 | String* name, |
| 1538 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1539 | PropertyAttributes attributes, |
| 1540 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1541 | // Check local property, ignore interceptor. |
| 1542 | LookupResult result; |
| 1543 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1544 | if (result.IsFound()) { |
| 1545 | // An existing property, a map transition or a null descriptor was |
| 1546 | // found. Use set property to handle all these cases. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1547 | return SetProperty(&result, name, value, attributes, strict_mode); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1548 | } |
| 1549 | // Add a new real property. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1550 | return AddProperty(name, value, attributes, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1554 | MaybeObject* JSObject::ReplaceSlowProperty(String* name, |
| 1555 | Object* value, |
| 1556 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1557 | StringDictionary* dictionary = property_dictionary(); |
| 1558 | int old_index = dictionary->FindEntry(name); |
| 1559 | int new_enumeration_index = 0; // 0 means "Use the next available index." |
| 1560 | if (old_index != -1) { |
| 1561 | // All calls to ReplaceSlowProperty have had all transitions removed. |
| 1562 | ASSERT(!dictionary->DetailsAt(old_index).IsTransition()); |
| 1563 | new_enumeration_index = dictionary->DetailsAt(old_index).index(); |
| 1564 | } |
| 1565 | |
| 1566 | PropertyDetails new_details(attributes, NORMAL, new_enumeration_index); |
| 1567 | return SetNormalizedProperty(name, value, new_details); |
| 1568 | } |
| 1569 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1570 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1571 | MaybeObject* JSObject::ConvertDescriptorToFieldAndMapTransition( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1572 | String* name, |
| 1573 | Object* new_value, |
| 1574 | PropertyAttributes attributes) { |
| 1575 | Map* old_map = map(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1576 | Object* result; |
| 1577 | { MaybeObject* maybe_result = |
| 1578 | ConvertDescriptorToField(name, new_value, attributes); |
| 1579 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1580 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1581 | // If we get to this point we have succeeded - do not return failure |
| 1582 | // after this point. Later stuff is optional. |
| 1583 | if (!HasFastProperties()) { |
| 1584 | return result; |
| 1585 | } |
| 1586 | // Do not add transitions to the map of "new Object()". |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1587 | if (map() == old_map->heap()->isolate()->context()->global_context()-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1588 | object_function()->map()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1589 | return result; |
| 1590 | } |
| 1591 | |
| 1592 | MapTransitionDescriptor transition(name, |
| 1593 | map(), |
| 1594 | attributes); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1595 | Object* new_descriptors; |
| 1596 | { MaybeObject* maybe_new_descriptors = old_map->instance_descriptors()-> |
| 1597 | CopyInsert(&transition, KEEP_TRANSITIONS); |
| 1598 | if (!maybe_new_descriptors->ToObject(&new_descriptors)) { |
| 1599 | return result; // Yes, return _result_. |
| 1600 | } |
| 1601 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1602 | old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors)); |
| 1603 | return result; |
| 1604 | } |
| 1605 | |
| 1606 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1607 | MaybeObject* JSObject::ConvertDescriptorToField(String* name, |
| 1608 | Object* new_value, |
| 1609 | PropertyAttributes attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1610 | if (map()->unused_property_fields() == 0 && |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1611 | properties()->length() > MaxFastProperties()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1612 | Object* obj; |
| 1613 | { MaybeObject* maybe_obj = |
| 1614 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 1615 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1616 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1617 | return ReplaceSlowProperty(name, new_value, attributes); |
| 1618 | } |
| 1619 | |
| 1620 | int index = map()->NextFreePropertyIndex(); |
| 1621 | FieldDescriptor new_field(name, index, attributes); |
| 1622 | // Make a new DescriptorArray replacing an entry with FieldDescriptor. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1623 | Object* descriptors_unchecked; |
| 1624 | { MaybeObject* maybe_descriptors_unchecked = map()->instance_descriptors()-> |
| 1625 | CopyInsert(&new_field, REMOVE_TRANSITIONS); |
| 1626 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 1627 | return maybe_descriptors_unchecked; |
| 1628 | } |
| 1629 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1630 | DescriptorArray* new_descriptors = |
| 1631 | DescriptorArray::cast(descriptors_unchecked); |
| 1632 | |
| 1633 | // Make a new map for the object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1634 | Object* new_map_unchecked; |
| 1635 | { MaybeObject* maybe_new_map_unchecked = map()->CopyDropDescriptors(); |
| 1636 | if (!maybe_new_map_unchecked->ToObject(&new_map_unchecked)) { |
| 1637 | return maybe_new_map_unchecked; |
| 1638 | } |
| 1639 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1640 | Map* new_map = Map::cast(new_map_unchecked); |
| 1641 | new_map->set_instance_descriptors(new_descriptors); |
| 1642 | |
| 1643 | // Make new properties array if necessary. |
| 1644 | FixedArray* new_properties = 0; // Will always be NULL or a valid pointer. |
| 1645 | int new_unused_property_fields = map()->unused_property_fields() - 1; |
| 1646 | if (map()->unused_property_fields() == 0) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1647 | new_unused_property_fields = kFieldsAdded - 1; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1648 | Object* new_properties_object; |
| 1649 | { MaybeObject* maybe_new_properties_object = |
| 1650 | properties()->CopySize(properties()->length() + kFieldsAdded); |
| 1651 | if (!maybe_new_properties_object->ToObject(&new_properties_object)) { |
| 1652 | return maybe_new_properties_object; |
| 1653 | } |
| 1654 | } |
| 1655 | new_properties = FixedArray::cast(new_properties_object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // Update pointers to commit changes. |
| 1659 | // Object points to the new map. |
| 1660 | new_map->set_unused_property_fields(new_unused_property_fields); |
| 1661 | set_map(new_map); |
| 1662 | if (new_properties) { |
| 1663 | set_properties(FixedArray::cast(new_properties)); |
| 1664 | } |
| 1665 | return FastPropertyAtPut(index, new_value); |
| 1666 | } |
| 1667 | |
| 1668 | |
| 1669 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1670 | MaybeObject* JSObject::SetPropertyWithInterceptor( |
| 1671 | String* name, |
| 1672 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1673 | PropertyAttributes attributes, |
| 1674 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1675 | Isolate* isolate = GetIsolate(); |
| 1676 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1677 | Handle<JSObject> this_handle(this); |
| 1678 | Handle<String> name_handle(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1679 | Handle<Object> value_handle(value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1680 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 1681 | if (!interceptor->setter()->IsUndefined()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1682 | LOG(isolate, ApiNamedPropertyAccess("interceptor-named-set", this, name)); |
| 1683 | CustomArguments args(isolate, interceptor->data(), this, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1684 | v8::AccessorInfo info(args.end()); |
| 1685 | v8::NamedPropertySetter setter = |
| 1686 | v8::ToCData<v8::NamedPropertySetter>(interceptor->setter()); |
| 1687 | v8::Handle<v8::Value> result; |
| 1688 | { |
| 1689 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1690 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1691 | Handle<Object> value_unhole(value->IsTheHole() ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1692 | isolate->heap()->undefined_value() : |
| 1693 | value, |
| 1694 | isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1695 | result = setter(v8::Utils::ToLocal(name_handle), |
| 1696 | v8::Utils::ToLocal(value_unhole), |
| 1697 | info); |
| 1698 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1699 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1700 | if (!result.IsEmpty()) return *value_handle; |
| 1701 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1702 | MaybeObject* raw_result = |
| 1703 | this_handle->SetPropertyPostInterceptor(*name_handle, |
| 1704 | *value_handle, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1705 | attributes, |
| 1706 | strict_mode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1707 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1708 | return raw_result; |
| 1709 | } |
| 1710 | |
| 1711 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1712 | MaybeObject* JSObject::SetProperty(String* name, |
| 1713 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1714 | PropertyAttributes attributes, |
| 1715 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1716 | LookupResult result; |
| 1717 | LocalLookup(name, &result); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 1718 | return SetProperty(&result, name, value, attributes, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1722 | MaybeObject* JSObject::SetPropertyWithCallback(Object* structure, |
| 1723 | String* name, |
| 1724 | Object* value, |
| 1725 | JSObject* holder) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1726 | Isolate* isolate = GetIsolate(); |
| 1727 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1728 | |
| 1729 | // We should never get here to initialize a const with the hole |
| 1730 | // value since a const declaration would conflict with the setter. |
| 1731 | ASSERT(!value->IsTheHole()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1732 | Handle<Object> value_handle(value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1733 | |
| 1734 | // To accommodate both the old and the new api we switch on the |
| 1735 | // data structure used to store the callbacks. Eventually proxy |
| 1736 | // callbacks should be phased out. |
| 1737 | if (structure->IsProxy()) { |
| 1738 | AccessorDescriptor* callback = |
| 1739 | reinterpret_cast<AccessorDescriptor*>(Proxy::cast(structure)->proxy()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1740 | MaybeObject* obj = (callback->setter)(this, value, callback->data); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1741 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1742 | if (obj->IsFailure()) return obj; |
| 1743 | return *value_handle; |
| 1744 | } |
| 1745 | |
| 1746 | if (structure->IsAccessorInfo()) { |
| 1747 | // api style callbacks |
| 1748 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 1749 | Object* call_obj = data->setter(); |
| 1750 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 1751 | if (call_fun == NULL) return value; |
| 1752 | Handle<String> key(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1753 | LOG(isolate, ApiNamedPropertyAccess("store", this, name)); |
| 1754 | CustomArguments args(isolate, data->data(), this, JSObject::cast(holder)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1755 | v8::AccessorInfo info(args.end()); |
| 1756 | { |
| 1757 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1758 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1759 | call_fun(v8::Utils::ToLocal(key), |
| 1760 | v8::Utils::ToLocal(value_handle), |
| 1761 | info); |
| 1762 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1763 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1764 | return *value_handle; |
| 1765 | } |
| 1766 | |
| 1767 | if (structure->IsFixedArray()) { |
| 1768 | Object* setter = FixedArray::cast(structure)->get(kSetterIndex); |
| 1769 | if (setter->IsJSFunction()) { |
| 1770 | return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); |
| 1771 | } else { |
| 1772 | Handle<String> key(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1773 | Handle<Object> holder_handle(holder, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1774 | Handle<Object> args[2] = { key, holder_handle }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1775 | return isolate->Throw( |
| 1776 | *isolate->factory()->NewTypeError("no_setter_in_callback", |
| 1777 | HandleVector(args, 2))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | UNREACHABLE(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1782 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1786 | MaybeObject* JSObject::SetPropertyWithDefinedSetter(JSFunction* setter, |
| 1787 | Object* value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1788 | Isolate* isolate = GetIsolate(); |
| 1789 | Handle<Object> value_handle(value, isolate); |
| 1790 | Handle<JSFunction> fun(JSFunction::cast(setter), isolate); |
| 1791 | Handle<JSObject> self(this, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1792 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1793 | Debug* debug = isolate->debug(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1794 | // Handle stepping into a setter if step into is active. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1795 | if (debug->StepInActive()) { |
| 1796 | debug->HandleStepIn(fun, Handle<Object>::null(), 0, false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1797 | } |
| 1798 | #endif |
| 1799 | bool has_pending_exception; |
| 1800 | Object** argv[] = { value_handle.location() }; |
| 1801 | Execution::Call(fun, self, 1, argv, &has_pending_exception); |
| 1802 | // Check for pending exception and return the result. |
| 1803 | if (has_pending_exception) return Failure::Exception(); |
| 1804 | return *value_handle; |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | void JSObject::LookupCallbackSetterInPrototypes(String* name, |
| 1809 | LookupResult* result) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1810 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1811 | for (Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1812 | pt != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1813 | pt = pt->GetPrototype()) { |
| 1814 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1815 | if (result->IsProperty()) { |
Ben Murdoch | 7d3e7fc | 2011-07-12 16:37:06 +0100 | [diff] [blame^] | 1816 | if (result->type() == CALLBACKS && !result->IsReadOnly()) return; |
| 1817 | // Found non-callback or read-only callback, stop looking. |
| 1818 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | result->NotFound(); |
| 1822 | } |
| 1823 | |
| 1824 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1825 | MaybeObject* JSObject::SetElementWithCallbackSetterInPrototypes(uint32_t index, |
| 1826 | Object* value, |
| 1827 | bool* found) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1828 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1829 | for (Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1830 | pt != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1831 | pt = pt->GetPrototype()) { |
| 1832 | if (!JSObject::cast(pt)->HasDictionaryElements()) { |
| 1833 | continue; |
| 1834 | } |
| 1835 | NumberDictionary* dictionary = JSObject::cast(pt)->element_dictionary(); |
| 1836 | int entry = dictionary->FindEntry(index); |
| 1837 | if (entry != NumberDictionary::kNotFound) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1838 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 1839 | if (details.type() == CALLBACKS) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1840 | *found = true; |
| 1841 | return SetElementWithCallback( |
| 1842 | dictionary->ValueAt(entry), index, value, JSObject::cast(pt)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | } |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1846 | *found = false; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1847 | return heap->the_hole_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | void JSObject::LookupInDescriptor(String* name, LookupResult* result) { |
| 1852 | DescriptorArray* descriptors = map()->instance_descriptors(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1853 | int number = descriptors->SearchWithCache(name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1854 | if (number != DescriptorArray::kNotFound) { |
| 1855 | result->DescriptorResult(this, descriptors->GetDetails(number), number); |
| 1856 | } else { |
| 1857 | result->NotFound(); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1862 | void Map::LookupInDescriptors(JSObject* holder, |
| 1863 | String* name, |
| 1864 | LookupResult* result) { |
| 1865 | DescriptorArray* descriptors = instance_descriptors(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1866 | DescriptorLookupCache* cache = heap()->isolate()->descriptor_lookup_cache(); |
| 1867 | int number = cache->Lookup(descriptors, name); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1868 | if (number == DescriptorLookupCache::kAbsent) { |
| 1869 | number = descriptors->Search(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1870 | cache->Update(descriptors, name, number); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1871 | } |
| 1872 | if (number != DescriptorArray::kNotFound) { |
| 1873 | result->DescriptorResult(holder, descriptors->GetDetails(number), number); |
| 1874 | } else { |
| 1875 | result->NotFound(); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1880 | MaybeObject* Map::GetExternalArrayElementsMap(ExternalArrayType array_type, |
| 1881 | bool safe_to_add_transition) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1882 | Heap* current_heap = heap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1883 | DescriptorArray* descriptors = instance_descriptors(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1884 | String* external_array_sentinel_name = current_heap->empty_symbol(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1885 | |
| 1886 | if (safe_to_add_transition) { |
| 1887 | // It's only safe to manipulate the descriptor array if it would be |
| 1888 | // safe to add a transition. |
| 1889 | |
| 1890 | ASSERT(!is_shared()); // no transitions can be added to shared maps. |
| 1891 | // Check if the external array transition already exists. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1892 | DescriptorLookupCache* cache = |
| 1893 | current_heap->isolate()->descriptor_lookup_cache(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1894 | int index = cache->Lookup(descriptors, external_array_sentinel_name); |
| 1895 | if (index == DescriptorLookupCache::kAbsent) { |
| 1896 | index = descriptors->Search(external_array_sentinel_name); |
| 1897 | cache->Update(descriptors, |
| 1898 | external_array_sentinel_name, |
| 1899 | index); |
| 1900 | } |
| 1901 | |
| 1902 | // If the transition already exists, check the type. If there is a match, |
| 1903 | // return it. |
| 1904 | if (index != DescriptorArray::kNotFound) { |
| 1905 | PropertyDetails details(PropertyDetails(descriptors->GetDetails(index))); |
| 1906 | if (details.type() == EXTERNAL_ARRAY_TRANSITION && |
| 1907 | details.array_type() == array_type) { |
| 1908 | return descriptors->GetValue(index); |
| 1909 | } else { |
| 1910 | safe_to_add_transition = false; |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | // No transition to an existing external array map. Make a new one. |
| 1916 | Object* obj; |
| 1917 | { MaybeObject* maybe_map = CopyDropTransitions(); |
| 1918 | if (!maybe_map->ToObject(&obj)) return maybe_map; |
| 1919 | } |
| 1920 | Map* new_map = Map::cast(obj); |
| 1921 | |
| 1922 | new_map->set_has_fast_elements(false); |
| 1923 | new_map->set_has_external_array_elements(true); |
| 1924 | GetIsolate()->counters()->map_to_external_array_elements()->Increment(); |
| 1925 | |
| 1926 | // Only remember the map transition if the object's map is NOT equal to the |
| 1927 | // global object_function's map and there is not an already existing |
| 1928 | // non-matching external array transition. |
| 1929 | bool allow_map_transition = |
| 1930 | safe_to_add_transition && |
| 1931 | (GetIsolate()->context()->global_context()->object_function()->map() != |
| 1932 | map()); |
| 1933 | if (allow_map_transition) { |
| 1934 | // Allocate new instance descriptors for the old map with map transition. |
| 1935 | ExternalArrayTransitionDescriptor desc(external_array_sentinel_name, |
| 1936 | Map::cast(new_map), |
| 1937 | array_type); |
| 1938 | Object* new_descriptors; |
| 1939 | MaybeObject* maybe_new_descriptors = descriptors->CopyInsert( |
| 1940 | &desc, |
| 1941 | KEEP_TRANSITIONS); |
| 1942 | if (!maybe_new_descriptors->ToObject(&new_descriptors)) { |
| 1943 | return maybe_new_descriptors; |
| 1944 | } |
| 1945 | descriptors = DescriptorArray::cast(new_descriptors); |
| 1946 | set_instance_descriptors(descriptors); |
| 1947 | } |
| 1948 | |
| 1949 | return new_map; |
| 1950 | } |
| 1951 | |
| 1952 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1953 | void JSObject::LocalLookupRealNamedProperty(String* name, |
| 1954 | LookupResult* result) { |
| 1955 | if (IsJSGlobalProxy()) { |
| 1956 | Object* proto = GetPrototype(); |
| 1957 | if (proto->IsNull()) return result->NotFound(); |
| 1958 | ASSERT(proto->IsJSGlobalObject()); |
| 1959 | return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result); |
| 1960 | } |
| 1961 | |
| 1962 | if (HasFastProperties()) { |
| 1963 | LookupInDescriptor(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1964 | if (result->IsFound()) { |
| 1965 | // A property, a map transition or a null descriptor was found. |
| 1966 | // We return all of these result types because |
| 1967 | // LocalLookupRealNamedProperty is used when setting properties |
| 1968 | // where map transitions and null descriptors are handled. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1969 | ASSERT(result->holder() == this && result->type() != NORMAL); |
| 1970 | // Disallow caching for uninitialized constants. These can only |
| 1971 | // occur as fields. |
| 1972 | if (result->IsReadOnly() && result->type() == FIELD && |
| 1973 | FastPropertyAt(result->GetFieldIndex())->IsTheHole()) { |
| 1974 | result->DisallowCaching(); |
| 1975 | } |
| 1976 | return; |
| 1977 | } |
| 1978 | } else { |
| 1979 | int entry = property_dictionary()->FindEntry(name); |
| 1980 | if (entry != StringDictionary::kNotFound) { |
| 1981 | Object* value = property_dictionary()->ValueAt(entry); |
| 1982 | if (IsGlobalObject()) { |
| 1983 | PropertyDetails d = property_dictionary()->DetailsAt(entry); |
| 1984 | if (d.IsDeleted()) { |
| 1985 | result->NotFound(); |
| 1986 | return; |
| 1987 | } |
| 1988 | value = JSGlobalPropertyCell::cast(value)->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1989 | } |
| 1990 | // Make sure to disallow caching for uninitialized constants |
| 1991 | // found in the dictionary-mode objects. |
| 1992 | if (value->IsTheHole()) result->DisallowCaching(); |
| 1993 | result->DictionaryResult(this, entry); |
| 1994 | return; |
| 1995 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1996 | } |
| 1997 | result->NotFound(); |
| 1998 | } |
| 1999 | |
| 2000 | |
| 2001 | void JSObject::LookupRealNamedProperty(String* name, LookupResult* result) { |
| 2002 | LocalLookupRealNamedProperty(name, result); |
| 2003 | if (result->IsProperty()) return; |
| 2004 | |
| 2005 | LookupRealNamedPropertyInPrototypes(name, result); |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | void JSObject::LookupRealNamedPropertyInPrototypes(String* name, |
| 2010 | LookupResult* result) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2011 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2012 | for (Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2013 | pt != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2014 | pt = JSObject::cast(pt)->GetPrototype()) { |
| 2015 | JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2016 | if (result->IsProperty() && (result->type() != INTERCEPTOR)) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2017 | } |
| 2018 | result->NotFound(); |
| 2019 | } |
| 2020 | |
| 2021 | |
| 2022 | // We only need to deal with CALLBACKS and INTERCEPTORS |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2023 | MaybeObject* JSObject::SetPropertyWithFailedAccessCheck(LookupResult* result, |
| 2024 | String* name, |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2025 | Object* value, |
| 2026 | bool check_prototype) { |
| 2027 | if (check_prototype && !result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2028 | LookupCallbackSetterInPrototypes(name, result); |
| 2029 | } |
| 2030 | |
| 2031 | if (result->IsProperty()) { |
| 2032 | if (!result->IsReadOnly()) { |
| 2033 | switch (result->type()) { |
| 2034 | case CALLBACKS: { |
| 2035 | Object* obj = result->GetCallbackObject(); |
| 2036 | if (obj->IsAccessorInfo()) { |
| 2037 | AccessorInfo* info = AccessorInfo::cast(obj); |
| 2038 | if (info->all_can_write()) { |
| 2039 | return SetPropertyWithCallback(result->GetCallbackObject(), |
| 2040 | name, |
| 2041 | value, |
| 2042 | result->holder()); |
| 2043 | } |
| 2044 | } |
| 2045 | break; |
| 2046 | } |
| 2047 | case INTERCEPTOR: { |
| 2048 | // Try lookup real named properties. Note that only property can be |
| 2049 | // set is callbacks marked as ALL_CAN_WRITE on the prototype chain. |
| 2050 | LookupResult r; |
| 2051 | LookupRealNamedProperty(name, &r); |
| 2052 | if (r.IsProperty()) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2053 | return SetPropertyWithFailedAccessCheck(&r, name, value, |
| 2054 | check_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2055 | } |
| 2056 | break; |
| 2057 | } |
| 2058 | default: { |
| 2059 | break; |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2065 | HandleScope scope; |
| 2066 | Handle<Object> value_handle(value); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2067 | Heap* heap = GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2068 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2069 | return *value_handle; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2073 | MaybeObject* JSObject::SetProperty(LookupResult* result, |
| 2074 | String* name, |
| 2075 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2076 | PropertyAttributes attributes, |
| 2077 | StrictModeFlag strict_mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2078 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2079 | // Make sure that the top context does not change when doing callbacks or |
| 2080 | // interceptor calls. |
| 2081 | AssertNoContextChange ncc; |
| 2082 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2083 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2084 | // dictionary. We make these short keys into symbols to avoid constantly |
| 2085 | // reallocating them. |
| 2086 | if (!name->IsSymbol() && name->length() <= 2) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2087 | Object* symbol_version; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2088 | { MaybeObject* maybe_symbol_version = heap->LookupSymbol(name); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2089 | if (maybe_symbol_version->ToObject(&symbol_version)) { |
| 2090 | name = String::cast(symbol_version); |
| 2091 | } |
| 2092 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2095 | // Check access rights if needed. |
| 2096 | if (IsAccessCheckNeeded() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2097 | && !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_SET)) { |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2098 | return SetPropertyWithFailedAccessCheck(result, name, value, true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | if (IsJSGlobalProxy()) { |
| 2102 | Object* proto = GetPrototype(); |
| 2103 | if (proto->IsNull()) return value; |
| 2104 | ASSERT(proto->IsJSGlobalObject()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2105 | return JSObject::cast(proto)->SetProperty( |
| 2106 | result, name, value, attributes, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | if (!result->IsProperty() && !IsJSContextExtensionObject()) { |
| 2110 | // We could not find a local property so let's check whether there is an |
| 2111 | // accessor that wants to handle the property. |
| 2112 | LookupResult accessor_result; |
| 2113 | LookupCallbackSetterInPrototypes(name, &accessor_result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2114 | if (accessor_result.IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2115 | return SetPropertyWithCallback(accessor_result.GetCallbackObject(), |
| 2116 | name, |
| 2117 | value, |
| 2118 | accessor_result.holder()); |
| 2119 | } |
| 2120 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2121 | if (!result->IsFound()) { |
| 2122 | // Neither properties nor transitions found. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2123 | return AddProperty(name, value, attributes, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2124 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2125 | if (result->IsReadOnly() && result->IsProperty()) { |
| 2126 | if (strict_mode == kStrictMode) { |
| 2127 | HandleScope scope; |
| 2128 | Handle<String> key(name); |
| 2129 | Handle<Object> holder(this); |
| 2130 | Handle<Object> args[2] = { key, holder }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2131 | return heap->isolate()->Throw(*heap->isolate()->factory()->NewTypeError( |
| 2132 | "strict_read_only_property", HandleVector(args, 2))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2133 | } else { |
| 2134 | return value; |
| 2135 | } |
| 2136 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2137 | // This is a real property that is not read-only, or it is a |
| 2138 | // transition or null descriptor and there are no setters in the prototypes. |
| 2139 | switch (result->type()) { |
| 2140 | case NORMAL: |
| 2141 | return SetNormalizedProperty(result, value); |
| 2142 | case FIELD: |
| 2143 | return FastPropertyAtPut(result->GetFieldIndex(), value); |
| 2144 | case MAP_TRANSITION: |
| 2145 | if (attributes == result->GetAttributes()) { |
| 2146 | // Only use map transition if the attributes match. |
| 2147 | return AddFastPropertyUsingMap(result->GetTransitionMap(), |
| 2148 | name, |
| 2149 | value); |
| 2150 | } |
| 2151 | return ConvertDescriptorToField(name, value, attributes); |
| 2152 | case CONSTANT_FUNCTION: |
| 2153 | // Only replace the function if necessary. |
| 2154 | if (value == result->GetConstantFunction()) return value; |
| 2155 | // Preserve the attributes of this existing property. |
| 2156 | attributes = result->GetAttributes(); |
| 2157 | return ConvertDescriptorToField(name, value, attributes); |
| 2158 | case CALLBACKS: |
| 2159 | return SetPropertyWithCallback(result->GetCallbackObject(), |
| 2160 | name, |
| 2161 | value, |
| 2162 | result->holder()); |
| 2163 | case INTERCEPTOR: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2164 | return SetPropertyWithInterceptor(name, value, attributes, strict_mode); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2165 | case CONSTANT_TRANSITION: { |
| 2166 | // If the same constant function is being added we can simply |
| 2167 | // transition to the target map. |
| 2168 | Map* target_map = result->GetTransitionMap(); |
| 2169 | DescriptorArray* target_descriptors = target_map->instance_descriptors(); |
| 2170 | int number = target_descriptors->SearchWithCache(name); |
| 2171 | ASSERT(number != DescriptorArray::kNotFound); |
| 2172 | ASSERT(target_descriptors->GetType(number) == CONSTANT_FUNCTION); |
| 2173 | JSFunction* function = |
| 2174 | JSFunction::cast(target_descriptors->GetValue(number)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2175 | ASSERT(!HEAP->InNewSpace(function)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2176 | if (value == function) { |
| 2177 | set_map(target_map); |
| 2178 | return value; |
| 2179 | } |
| 2180 | // Otherwise, replace with a MAP_TRANSITION to a new map with a |
| 2181 | // FIELD, even if the value is a constant function. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2182 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2183 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2184 | case NULL_DESCRIPTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2185 | case EXTERNAL_ARRAY_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2186 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 2187 | default: |
| 2188 | UNREACHABLE(); |
| 2189 | } |
| 2190 | UNREACHABLE(); |
| 2191 | return value; |
| 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | // Set a real local property, even if it is READ_ONLY. If the property is not |
| 2196 | // present, add it with attributes NONE. This code is an exact clone of |
| 2197 | // SetProperty, with the check for IsReadOnly and the check for a |
| 2198 | // callback setter removed. The two lines looking up the LookupResult |
| 2199 | // result are also added. If one of the functions is changed, the other |
| 2200 | // should be. |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2201 | MaybeObject* JSObject::SetLocalPropertyIgnoreAttributes( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2202 | String* name, |
| 2203 | Object* value, |
| 2204 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2205 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2206 | // Make sure that the top context does not change when doing callbacks or |
| 2207 | // interceptor calls. |
| 2208 | AssertNoContextChange ncc; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2209 | LookupResult result; |
| 2210 | LocalLookup(name, &result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2211 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2212 | if (IsAccessCheckNeeded()) { |
| 2213 | Heap* heap = GetHeap(); |
| 2214 | if (!heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 2215 | return SetPropertyWithFailedAccessCheck(&result, name, value, false); |
| 2216 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | if (IsJSGlobalProxy()) { |
| 2220 | Object* proto = GetPrototype(); |
| 2221 | if (proto->IsNull()) return value; |
| 2222 | ASSERT(proto->IsJSGlobalObject()); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2223 | return JSObject::cast(proto)->SetLocalPropertyIgnoreAttributes( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2224 | name, |
| 2225 | value, |
| 2226 | attributes); |
| 2227 | } |
| 2228 | |
| 2229 | // Check for accessor in prototype chain removed here in clone. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2230 | if (!result.IsFound()) { |
| 2231 | // Neither properties nor transitions found. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2232 | return AddProperty(name, value, attributes, kNonStrictMode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2233 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2234 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2235 | PropertyDetails details = PropertyDetails(attributes, NORMAL); |
| 2236 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2237 | // Check of IsReadOnly removed from here in clone. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2238 | switch (result.type()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2239 | case NORMAL: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2240 | return SetNormalizedProperty(name, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2241 | case FIELD: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2242 | return FastPropertyAtPut(result.GetFieldIndex(), value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2243 | case MAP_TRANSITION: |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2244 | if (attributes == result.GetAttributes()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2245 | // Only use map transition if the attributes match. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2246 | return AddFastPropertyUsingMap(result.GetTransitionMap(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2247 | name, |
| 2248 | value); |
| 2249 | } |
| 2250 | return ConvertDescriptorToField(name, value, attributes); |
| 2251 | case CONSTANT_FUNCTION: |
| 2252 | // Only replace the function if necessary. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2253 | if (value == result.GetConstantFunction()) return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2254 | // Preserve the attributes of this existing property. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2255 | attributes = result.GetAttributes(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2256 | return ConvertDescriptorToField(name, value, attributes); |
| 2257 | case CALLBACKS: |
| 2258 | case INTERCEPTOR: |
| 2259 | // Override callback in clone |
| 2260 | return ConvertDescriptorToField(name, value, attributes); |
| 2261 | case CONSTANT_TRANSITION: |
| 2262 | // Replace with a MAP_TRANSITION to a new map with a FIELD, even |
| 2263 | // if the value is a function. |
| 2264 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 2265 | case NULL_DESCRIPTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2266 | case EXTERNAL_ARRAY_TRANSITION: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2267 | return ConvertDescriptorToFieldAndMapTransition(name, value, attributes); |
| 2268 | default: |
| 2269 | UNREACHABLE(); |
| 2270 | } |
| 2271 | UNREACHABLE(); |
| 2272 | return value; |
| 2273 | } |
| 2274 | |
| 2275 | |
| 2276 | PropertyAttributes JSObject::GetPropertyAttributePostInterceptor( |
| 2277 | JSObject* receiver, |
| 2278 | String* name, |
| 2279 | bool continue_search) { |
| 2280 | // Check local property, ignore interceptor. |
| 2281 | LookupResult result; |
| 2282 | LocalLookupRealNamedProperty(name, &result); |
| 2283 | if (result.IsProperty()) return result.GetAttributes(); |
| 2284 | |
| 2285 | if (continue_search) { |
| 2286 | // Continue searching via the prototype chain. |
| 2287 | Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2288 | if (!pt->IsNull()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2289 | return JSObject::cast(pt)-> |
| 2290 | GetPropertyAttributeWithReceiver(receiver, name); |
| 2291 | } |
| 2292 | } |
| 2293 | return ABSENT; |
| 2294 | } |
| 2295 | |
| 2296 | |
| 2297 | PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor( |
| 2298 | JSObject* receiver, |
| 2299 | String* name, |
| 2300 | bool continue_search) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2301 | Isolate* isolate = GetIsolate(); |
| 2302 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2303 | // Make sure that the top context does not change when doing |
| 2304 | // callbacks or interceptor calls. |
| 2305 | AssertNoContextChange ncc; |
| 2306 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2307 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2308 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 2309 | Handle<JSObject> receiver_handle(receiver); |
| 2310 | Handle<JSObject> holder_handle(this); |
| 2311 | Handle<String> name_handle(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2312 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2313 | v8::AccessorInfo info(args.end()); |
| 2314 | if (!interceptor->query()->IsUndefined()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2315 | v8::NamedPropertyQuery query = |
| 2316 | v8::ToCData<v8::NamedPropertyQuery>(interceptor->query()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2317 | LOG(isolate, |
| 2318 | ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2319 | v8::Handle<v8::Integer> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2320 | { |
| 2321 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2322 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2323 | result = query(v8::Utils::ToLocal(name_handle), info); |
| 2324 | } |
| 2325 | if (!result.IsEmpty()) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2326 | ASSERT(result->IsInt32()); |
| 2327 | return static_cast<PropertyAttributes>(result->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2328 | } |
| 2329 | } else if (!interceptor->getter()->IsUndefined()) { |
| 2330 | v8::NamedPropertyGetter getter = |
| 2331 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2332 | LOG(isolate, |
| 2333 | ApiNamedPropertyAccess("interceptor-named-get-has", this, name)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2334 | v8::Handle<v8::Value> result; |
| 2335 | { |
| 2336 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2337 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2338 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 2339 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2340 | if (!result.IsEmpty()) return DONT_ENUM; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2341 | } |
| 2342 | return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle, |
| 2343 | *name_handle, |
| 2344 | continue_search); |
| 2345 | } |
| 2346 | |
| 2347 | |
| 2348 | PropertyAttributes JSObject::GetPropertyAttributeWithReceiver( |
| 2349 | JSObject* receiver, |
| 2350 | String* key) { |
| 2351 | uint32_t index = 0; |
| 2352 | if (key->AsArrayIndex(&index)) { |
| 2353 | if (HasElementWithReceiver(receiver, index)) return NONE; |
| 2354 | return ABSENT; |
| 2355 | } |
| 2356 | // Named property. |
| 2357 | LookupResult result; |
| 2358 | Lookup(key, &result); |
| 2359 | return GetPropertyAttribute(receiver, &result, key, true); |
| 2360 | } |
| 2361 | |
| 2362 | |
| 2363 | PropertyAttributes JSObject::GetPropertyAttribute(JSObject* receiver, |
| 2364 | LookupResult* result, |
| 2365 | String* name, |
| 2366 | bool continue_search) { |
| 2367 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2368 | if (IsAccessCheckNeeded()) { |
| 2369 | Heap* heap = GetHeap(); |
| 2370 | if (!heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_HAS)) { |
| 2371 | return GetPropertyAttributeWithFailedAccessCheck(receiver, |
| 2372 | result, |
| 2373 | name, |
| 2374 | continue_search); |
| 2375 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2376 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 2377 | if (result->IsProperty()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2378 | switch (result->type()) { |
| 2379 | case NORMAL: // fall through |
| 2380 | case FIELD: |
| 2381 | case CONSTANT_FUNCTION: |
| 2382 | case CALLBACKS: |
| 2383 | return result->GetAttributes(); |
| 2384 | case INTERCEPTOR: |
| 2385 | return result->holder()-> |
| 2386 | GetPropertyAttributeWithInterceptor(receiver, name, continue_search); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2387 | default: |
| 2388 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2389 | } |
| 2390 | } |
| 2391 | return ABSENT; |
| 2392 | } |
| 2393 | |
| 2394 | |
| 2395 | PropertyAttributes JSObject::GetLocalPropertyAttribute(String* name) { |
| 2396 | // Check whether the name is an array index. |
| 2397 | uint32_t index = 0; |
| 2398 | if (name->AsArrayIndex(&index)) { |
| 2399 | if (HasLocalElement(index)) return NONE; |
| 2400 | return ABSENT; |
| 2401 | } |
| 2402 | // Named property. |
| 2403 | LookupResult result; |
| 2404 | LocalLookup(name, &result); |
| 2405 | return GetPropertyAttribute(this, &result, name, false); |
| 2406 | } |
| 2407 | |
| 2408 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2409 | MaybeObject* NormalizedMapCache::Get(JSObject* obj, |
| 2410 | PropertyNormalizationMode mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2411 | Isolate* isolate = obj->GetIsolate(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2412 | Map* fast = obj->map(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2413 | int index = Hash(fast) % kEntries; |
| 2414 | Object* result = get(index); |
| 2415 | if (result->IsMap() && CheckHit(Map::cast(result), fast, mode)) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2416 | #ifdef DEBUG |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2417 | if (FLAG_enable_slow_asserts) { |
| 2418 | // The cached map should match newly created normalized map bit-by-bit. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2419 | Object* fresh; |
| 2420 | { MaybeObject* maybe_fresh = |
| 2421 | fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP); |
| 2422 | if (maybe_fresh->ToObject(&fresh)) { |
| 2423 | ASSERT(memcmp(Map::cast(fresh)->address(), |
| 2424 | Map::cast(result)->address(), |
| 2425 | Map::kSize) == 0); |
| 2426 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2427 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2428 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2429 | #endif |
| 2430 | return result; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2431 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2432 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2433 | { MaybeObject* maybe_result = |
| 2434 | fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP); |
| 2435 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2436 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2437 | set(index, result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2438 | isolate->counters()->normalized_maps()->Increment(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2439 | |
| 2440 | return result; |
| 2441 | } |
| 2442 | |
| 2443 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2444 | void NormalizedMapCache::Clear() { |
| 2445 | int entries = length(); |
| 2446 | for (int i = 0; i != entries; i++) { |
| 2447 | set_undefined(i); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | |
| 2452 | int NormalizedMapCache::Hash(Map* fast) { |
| 2453 | // For performance reasons we only hash the 3 most variable fields of a map: |
| 2454 | // constructor, prototype and bit_field2. |
| 2455 | |
| 2456 | // Shift away the tag. |
| 2457 | int hash = (static_cast<uint32_t>( |
| 2458 | reinterpret_cast<uintptr_t>(fast->constructor())) >> 2); |
| 2459 | |
| 2460 | // XOR-ing the prototype and constructor directly yields too many zero bits |
| 2461 | // when the two pointers are close (which is fairly common). |
| 2462 | // To avoid this we shift the prototype 4 bits relatively to the constructor. |
| 2463 | hash ^= (static_cast<uint32_t>( |
| 2464 | reinterpret_cast<uintptr_t>(fast->prototype())) << 2); |
| 2465 | |
| 2466 | return hash ^ (hash >> 16) ^ fast->bit_field2(); |
| 2467 | } |
| 2468 | |
| 2469 | |
| 2470 | bool NormalizedMapCache::CheckHit(Map* slow, |
| 2471 | Map* fast, |
| 2472 | PropertyNormalizationMode mode) { |
| 2473 | #ifdef DEBUG |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2474 | slow->SharedMapVerify(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2475 | #endif |
| 2476 | return |
| 2477 | slow->constructor() == fast->constructor() && |
| 2478 | slow->prototype() == fast->prototype() && |
| 2479 | slow->inobject_properties() == ((mode == CLEAR_INOBJECT_PROPERTIES) ? |
| 2480 | 0 : |
| 2481 | fast->inobject_properties()) && |
| 2482 | slow->instance_type() == fast->instance_type() && |
| 2483 | slow->bit_field() == fast->bit_field() && |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2484 | (slow->bit_field2() & ~(1<<Map::kIsShared)) == fast->bit_field2(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2488 | MaybeObject* JSObject::UpdateMapCodeCache(String* name, Code* code) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2489 | if (map()->is_shared()) { |
| 2490 | // Fast case maps are never marked as shared. |
| 2491 | ASSERT(!HasFastProperties()); |
| 2492 | // Replace the map with an identical copy that can be safely modified. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2493 | Object* obj; |
| 2494 | { MaybeObject* maybe_obj = map()->CopyNormalized(KEEP_INOBJECT_PROPERTIES, |
| 2495 | UNIQUE_NORMALIZED_MAP); |
| 2496 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2497 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2498 | GetIsolate()->counters()->normalized_maps()->Increment(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2499 | |
| 2500 | set_map(Map::cast(obj)); |
| 2501 | } |
| 2502 | return map()->UpdateCodeCache(name, code); |
| 2503 | } |
| 2504 | |
| 2505 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2506 | MaybeObject* JSObject::NormalizeProperties(PropertyNormalizationMode mode, |
| 2507 | int expected_additional_properties) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2508 | if (!HasFastProperties()) return this; |
| 2509 | |
| 2510 | // The global object is always normalized. |
| 2511 | ASSERT(!IsGlobalObject()); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2512 | // JSGlobalProxy must never be normalized |
| 2513 | ASSERT(!IsJSGlobalProxy()); |
| 2514 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2515 | Map* map_of_this = map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2516 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2517 | // Allocate new content. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2518 | int property_count = map_of_this->NumberOfDescribedProperties(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2519 | if (expected_additional_properties > 0) { |
| 2520 | property_count += expected_additional_properties; |
| 2521 | } else { |
| 2522 | property_count += 2; // Make space for two more properties. |
| 2523 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2524 | Object* obj; |
| 2525 | { MaybeObject* maybe_obj = |
| 2526 | StringDictionary::Allocate(property_count); |
| 2527 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2528 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2529 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 2530 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2531 | DescriptorArray* descs = map_of_this->instance_descriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2532 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2533 | PropertyDetails details(descs->GetDetails(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2534 | switch (details.type()) { |
| 2535 | case CONSTANT_FUNCTION: { |
| 2536 | PropertyDetails d = |
| 2537 | PropertyDetails(details.attributes(), NORMAL, details.index()); |
| 2538 | Object* value = descs->GetConstantFunction(i); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2539 | Object* result; |
| 2540 | { MaybeObject* maybe_result = |
| 2541 | dictionary->Add(descs->GetKey(i), value, d); |
| 2542 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2543 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2544 | dictionary = StringDictionary::cast(result); |
| 2545 | break; |
| 2546 | } |
| 2547 | case FIELD: { |
| 2548 | PropertyDetails d = |
| 2549 | PropertyDetails(details.attributes(), NORMAL, details.index()); |
| 2550 | Object* value = FastPropertyAt(descs->GetFieldIndex(i)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2551 | Object* result; |
| 2552 | { MaybeObject* maybe_result = |
| 2553 | dictionary->Add(descs->GetKey(i), value, d); |
| 2554 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2555 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2556 | dictionary = StringDictionary::cast(result); |
| 2557 | break; |
| 2558 | } |
| 2559 | case CALLBACKS: { |
| 2560 | PropertyDetails d = |
| 2561 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 2562 | Object* value = descs->GetCallbacksObject(i); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2563 | Object* result; |
| 2564 | { MaybeObject* maybe_result = |
| 2565 | dictionary->Add(descs->GetKey(i), value, d); |
| 2566 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2567 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2568 | dictionary = StringDictionary::cast(result); |
| 2569 | break; |
| 2570 | } |
| 2571 | case MAP_TRANSITION: |
| 2572 | case CONSTANT_TRANSITION: |
| 2573 | case NULL_DESCRIPTOR: |
| 2574 | case INTERCEPTOR: |
| 2575 | break; |
| 2576 | default: |
| 2577 | UNREACHABLE(); |
| 2578 | } |
| 2579 | } |
| 2580 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2581 | Heap* current_heap = map_of_this->heap(); |
| 2582 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2583 | // Copy the next enumeration index from instance descriptor. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2584 | int index = map_of_this->instance_descriptors()->NextEnumerationIndex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2585 | dictionary->SetNextEnumerationIndex(index); |
| 2586 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2587 | { MaybeObject* maybe_obj = |
| 2588 | current_heap->isolate()->context()->global_context()-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2589 | normalized_map_cache()->Get(this, mode); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2590 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2591 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2592 | Map* new_map = Map::cast(obj); |
| 2593 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2594 | // We have now successfully allocated all the necessary objects. |
| 2595 | // 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] | 2596 | |
| 2597 | // Resize the object in the heap if necessary. |
| 2598 | int new_instance_size = new_map->instance_size(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2599 | int instance_size_delta = map_of_this->instance_size() - new_instance_size; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2600 | ASSERT(instance_size_delta >= 0); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2601 | current_heap->CreateFillerObjectAt(this->address() + new_instance_size, |
| 2602 | instance_size_delta); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2603 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2604 | set_map(new_map); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2605 | new_map->set_instance_descriptors(current_heap->empty_descriptor_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2606 | |
| 2607 | set_properties(dictionary); |
| 2608 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2609 | current_heap->isolate()->counters()->props_to_dictionary()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2610 | |
| 2611 | #ifdef DEBUG |
| 2612 | if (FLAG_trace_normalization) { |
| 2613 | PrintF("Object properties have been normalized:\n"); |
| 2614 | Print(); |
| 2615 | } |
| 2616 | #endif |
| 2617 | return this; |
| 2618 | } |
| 2619 | |
| 2620 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2621 | MaybeObject* JSObject::TransformToFastProperties(int unused_property_fields) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2622 | if (HasFastProperties()) return this; |
| 2623 | ASSERT(!IsGlobalObject()); |
| 2624 | return property_dictionary()-> |
| 2625 | TransformPropertiesToFastFor(this, unused_property_fields); |
| 2626 | } |
| 2627 | |
| 2628 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2629 | MaybeObject* JSObject::NormalizeElements() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2630 | ASSERT(!HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2631 | if (HasDictionaryElements()) return this; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2632 | Map* old_map = map(); |
| 2633 | ASSERT(old_map->has_fast_elements()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2634 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2635 | Object* obj; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2636 | { MaybeObject* maybe_obj = old_map->GetSlowElementsMap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2637 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2638 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2639 | Map* new_map = Map::cast(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2640 | |
| 2641 | // Get number of entries. |
| 2642 | FixedArray* array = FixedArray::cast(elements()); |
| 2643 | |
| 2644 | // Compute the effective length. |
| 2645 | int length = IsJSArray() ? |
| 2646 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 2647 | array->length(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2648 | { MaybeObject* maybe_obj = NumberDictionary::Allocate(length); |
| 2649 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2650 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2651 | NumberDictionary* dictionary = NumberDictionary::cast(obj); |
| 2652 | // Copy entries. |
| 2653 | for (int i = 0; i < length; i++) { |
| 2654 | Object* value = array->get(i); |
| 2655 | if (!value->IsTheHole()) { |
| 2656 | PropertyDetails details = PropertyDetails(NONE, NORMAL); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2657 | Object* result; |
| 2658 | { MaybeObject* maybe_result = |
| 2659 | dictionary->AddNumberEntry(i, array->get(i), details); |
| 2660 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2661 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2662 | dictionary = NumberDictionary::cast(result); |
| 2663 | } |
| 2664 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2665 | // Switch to using the dictionary as the backing storage for |
| 2666 | // elements. Set the new map first to satify the elements type |
| 2667 | // assert in set_elements(). |
| 2668 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2669 | set_elements(dictionary); |
| 2670 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2671 | new_map->heap()->isolate()->counters()->elements_to_dictionary()-> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2672 | Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2673 | |
| 2674 | #ifdef DEBUG |
| 2675 | if (FLAG_trace_normalization) { |
| 2676 | PrintF("Object elements have been normalized:\n"); |
| 2677 | Print(); |
| 2678 | } |
| 2679 | #endif |
| 2680 | |
| 2681 | return this; |
| 2682 | } |
| 2683 | |
| 2684 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2685 | MaybeObject* JSObject::DeletePropertyPostInterceptor(String* name, |
| 2686 | DeleteMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2687 | // Check local property, ignore interceptor. |
| 2688 | LookupResult result; |
| 2689 | LocalLookupRealNamedProperty(name, &result); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2690 | if (!result.IsProperty()) return GetHeap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2691 | |
| 2692 | // Normalize object if needed. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2693 | Object* obj; |
| 2694 | { MaybeObject* maybe_obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2695 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2696 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2697 | |
| 2698 | return DeleteNormalizedProperty(name, mode); |
| 2699 | } |
| 2700 | |
| 2701 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2702 | MaybeObject* JSObject::DeletePropertyWithInterceptor(String* name) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2703 | Isolate* isolate = GetIsolate(); |
| 2704 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2705 | Handle<InterceptorInfo> interceptor(GetNamedInterceptor()); |
| 2706 | Handle<String> name_handle(name); |
| 2707 | Handle<JSObject> this_handle(this); |
| 2708 | if (!interceptor->deleter()->IsUndefined()) { |
| 2709 | v8::NamedPropertyDeleter deleter = |
| 2710 | v8::ToCData<v8::NamedPropertyDeleter>(interceptor->deleter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2711 | LOG(isolate, |
| 2712 | ApiNamedPropertyAccess("interceptor-named-delete", *this_handle, name)); |
| 2713 | CustomArguments args(isolate, interceptor->data(), this, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2714 | v8::AccessorInfo info(args.end()); |
| 2715 | v8::Handle<v8::Boolean> result; |
| 2716 | { |
| 2717 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2718 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2719 | result = deleter(v8::Utils::ToLocal(name_handle), info); |
| 2720 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2721 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2722 | if (!result.IsEmpty()) { |
| 2723 | ASSERT(result->IsBoolean()); |
| 2724 | return *v8::Utils::OpenHandle(*result); |
| 2725 | } |
| 2726 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2727 | MaybeObject* raw_result = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2728 | this_handle->DeletePropertyPostInterceptor(*name_handle, NORMAL_DELETION); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2729 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2730 | return raw_result; |
| 2731 | } |
| 2732 | |
| 2733 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2734 | MaybeObject* JSObject::DeleteElementPostInterceptor(uint32_t index, |
| 2735 | DeleteMode mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2736 | ASSERT(!HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2737 | switch (GetElementsKind()) { |
| 2738 | case FAST_ELEMENTS: { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2739 | Object* obj; |
| 2740 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 2741 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2742 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2743 | uint32_t length = IsJSArray() ? |
| 2744 | static_cast<uint32_t>(Smi::cast(JSArray::cast(this)->length())->value()) : |
| 2745 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 2746 | if (index < length) { |
| 2747 | FixedArray::cast(elements())->set_the_hole(index); |
| 2748 | } |
| 2749 | break; |
| 2750 | } |
| 2751 | case DICTIONARY_ELEMENTS: { |
| 2752 | NumberDictionary* dictionary = element_dictionary(); |
| 2753 | int entry = dictionary->FindEntry(index); |
| 2754 | if (entry != NumberDictionary::kNotFound) { |
| 2755 | return dictionary->DeleteProperty(entry, mode); |
| 2756 | } |
| 2757 | break; |
| 2758 | } |
| 2759 | default: |
| 2760 | UNREACHABLE(); |
| 2761 | break; |
| 2762 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2763 | return GetHeap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2767 | MaybeObject* JSObject::DeleteElementWithInterceptor(uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2768 | Isolate* isolate = GetIsolate(); |
| 2769 | Heap* heap = isolate->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2770 | // Make sure that the top context does not change when doing |
| 2771 | // callbacks or interceptor calls. |
| 2772 | AssertNoContextChange ncc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2773 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2774 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2775 | if (interceptor->deleter()->IsUndefined()) return heap->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2776 | v8::IndexedPropertyDeleter deleter = |
| 2777 | v8::ToCData<v8::IndexedPropertyDeleter>(interceptor->deleter()); |
| 2778 | Handle<JSObject> this_handle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2779 | LOG(isolate, |
| 2780 | ApiIndexedPropertyAccess("interceptor-indexed-delete", this, index)); |
| 2781 | CustomArguments args(isolate, interceptor->data(), this, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2782 | v8::AccessorInfo info(args.end()); |
| 2783 | v8::Handle<v8::Boolean> result; |
| 2784 | { |
| 2785 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2786 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2787 | result = deleter(index, info); |
| 2788 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2789 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2790 | if (!result.IsEmpty()) { |
| 2791 | ASSERT(result->IsBoolean()); |
| 2792 | return *v8::Utils::OpenHandle(*result); |
| 2793 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2794 | MaybeObject* raw_result = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2795 | this_handle->DeleteElementPostInterceptor(index, NORMAL_DELETION); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2796 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2797 | return raw_result; |
| 2798 | } |
| 2799 | |
| 2800 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2801 | MaybeObject* JSObject::DeleteElement(uint32_t index, DeleteMode mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2802 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2803 | // Check access rights if needed. |
| 2804 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2805 | !isolate->MayIndexedAccess(this, index, v8::ACCESS_DELETE)) { |
| 2806 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 2807 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | if (IsJSGlobalProxy()) { |
| 2811 | Object* proto = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2812 | if (proto->IsNull()) return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2813 | ASSERT(proto->IsJSGlobalObject()); |
| 2814 | return JSGlobalObject::cast(proto)->DeleteElement(index, mode); |
| 2815 | } |
| 2816 | |
| 2817 | if (HasIndexedInterceptor()) { |
| 2818 | // Skip interceptor if forcing deletion. |
| 2819 | if (mode == FORCE_DELETION) { |
| 2820 | return DeleteElementPostInterceptor(index, mode); |
| 2821 | } |
| 2822 | return DeleteElementWithInterceptor(index); |
| 2823 | } |
| 2824 | |
| 2825 | switch (GetElementsKind()) { |
| 2826 | case FAST_ELEMENTS: { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2827 | Object* obj; |
| 2828 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 2829 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2830 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2831 | uint32_t length = IsJSArray() ? |
| 2832 | static_cast<uint32_t>(Smi::cast(JSArray::cast(this)->length())->value()) : |
| 2833 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 2834 | if (index < length) { |
| 2835 | FixedArray::cast(elements())->set_the_hole(index); |
| 2836 | } |
| 2837 | break; |
| 2838 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2839 | case EXTERNAL_PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2840 | case EXTERNAL_BYTE_ELEMENTS: |
| 2841 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 2842 | case EXTERNAL_SHORT_ELEMENTS: |
| 2843 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 2844 | case EXTERNAL_INT_ELEMENTS: |
| 2845 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 2846 | case EXTERNAL_FLOAT_ELEMENTS: |
| 2847 | // Pixel and external array elements cannot be deleted. Just |
| 2848 | // silently ignore here. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2849 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2850 | case DICTIONARY_ELEMENTS: { |
| 2851 | NumberDictionary* dictionary = element_dictionary(); |
| 2852 | int entry = dictionary->FindEntry(index); |
| 2853 | if (entry != NumberDictionary::kNotFound) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2854 | Object* result = dictionary->DeleteProperty(entry, mode); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2855 | if (mode == STRICT_DELETION && result == |
| 2856 | isolate->heap()->false_value()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2857 | // In strict mode, deleting a non-configurable property throws |
| 2858 | // exception. dictionary->DeleteProperty will return false_value() |
| 2859 | // if a non-configurable property is being deleted. |
| 2860 | HandleScope scope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2861 | Handle<Object> i = isolate->factory()->NewNumberFromUint(index); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2862 | Handle<Object> args[2] = { i, Handle<Object>(this) }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2863 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 2864 | "strict_delete_property", HandleVector(args, 2))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2865 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2866 | } |
| 2867 | break; |
| 2868 | } |
| 2869 | default: |
| 2870 | UNREACHABLE(); |
| 2871 | break; |
| 2872 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2873 | return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2877 | MaybeObject* JSObject::DeleteProperty(String* name, DeleteMode mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2878 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2879 | // ECMA-262, 3rd, 8.6.2.5 |
| 2880 | ASSERT(name->IsString()); |
| 2881 | |
| 2882 | // Check access rights if needed. |
| 2883 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2884 | !isolate->MayNamedAccess(this, name, v8::ACCESS_DELETE)) { |
| 2885 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE); |
| 2886 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | if (IsJSGlobalProxy()) { |
| 2890 | Object* proto = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2891 | if (proto->IsNull()) return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2892 | ASSERT(proto->IsJSGlobalObject()); |
| 2893 | return JSGlobalObject::cast(proto)->DeleteProperty(name, mode); |
| 2894 | } |
| 2895 | |
| 2896 | uint32_t index = 0; |
| 2897 | if (name->AsArrayIndex(&index)) { |
| 2898 | return DeleteElement(index, mode); |
| 2899 | } else { |
| 2900 | LookupResult result; |
| 2901 | LocalLookup(name, &result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2902 | if (!result.IsProperty()) return isolate->heap()->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2903 | // Ignore attributes if forcing a deletion. |
| 2904 | if (result.IsDontDelete() && mode != FORCE_DELETION) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2905 | if (mode == STRICT_DELETION) { |
| 2906 | // Deleting a non-configurable property in strict mode. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2907 | HandleScope scope(isolate); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2908 | Handle<Object> args[2] = { Handle<Object>(name), Handle<Object>(this) }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2909 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 2910 | "strict_delete_property", HandleVector(args, 2))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2911 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2912 | return isolate->heap()->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2913 | } |
| 2914 | // Check for interceptor. |
| 2915 | if (result.type() == INTERCEPTOR) { |
| 2916 | // Skip interceptor if forcing a deletion. |
| 2917 | if (mode == FORCE_DELETION) { |
| 2918 | return DeletePropertyPostInterceptor(name, mode); |
| 2919 | } |
| 2920 | return DeletePropertyWithInterceptor(name); |
| 2921 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2922 | // Normalize object if needed. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2923 | Object* obj; |
| 2924 | { MaybeObject* maybe_obj = |
| 2925 | NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 2926 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2927 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2928 | // Make sure the properties are normalized before removing the entry. |
| 2929 | return DeleteNormalizedProperty(name, mode); |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | |
| 2934 | // Check whether this object references another object. |
| 2935 | bool JSObject::ReferencesObject(Object* obj) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2936 | Map* map_of_this = map(); |
| 2937 | Heap* heap = map_of_this->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2938 | AssertNoAllocation no_alloc; |
| 2939 | |
| 2940 | // Is the object the constructor for this object? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2941 | if (map_of_this->constructor() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2942 | return true; |
| 2943 | } |
| 2944 | |
| 2945 | // Is the object the prototype for this object? |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 2946 | if (map_of_this->prototype() == obj) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2947 | return true; |
| 2948 | } |
| 2949 | |
| 2950 | // Check if the object is among the named properties. |
| 2951 | Object* key = SlowReverseLookup(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2952 | if (!key->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2953 | return true; |
| 2954 | } |
| 2955 | |
| 2956 | // Check if the object is among the indexed properties. |
| 2957 | switch (GetElementsKind()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2958 | case EXTERNAL_PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2959 | case EXTERNAL_BYTE_ELEMENTS: |
| 2960 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 2961 | case EXTERNAL_SHORT_ELEMENTS: |
| 2962 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 2963 | case EXTERNAL_INT_ELEMENTS: |
| 2964 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 2965 | case EXTERNAL_FLOAT_ELEMENTS: |
| 2966 | // Raw pixels and external arrays do not reference other |
| 2967 | // objects. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2968 | break; |
| 2969 | case FAST_ELEMENTS: { |
| 2970 | int length = IsJSArray() ? |
| 2971 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 2972 | FixedArray::cast(elements())->length(); |
| 2973 | for (int i = 0; i < length; i++) { |
| 2974 | Object* element = FixedArray::cast(elements())->get(i); |
| 2975 | if (!element->IsTheHole() && element == obj) { |
| 2976 | return true; |
| 2977 | } |
| 2978 | } |
| 2979 | break; |
| 2980 | } |
| 2981 | case DICTIONARY_ELEMENTS: { |
| 2982 | key = element_dictionary()->SlowReverseLookup(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2983 | if (!key->IsUndefined()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2984 | return true; |
| 2985 | } |
| 2986 | break; |
| 2987 | } |
| 2988 | default: |
| 2989 | UNREACHABLE(); |
| 2990 | break; |
| 2991 | } |
| 2992 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2993 | // For functions check the context. |
| 2994 | if (IsJSFunction()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2995 | // Get the constructor function for arguments array. |
| 2996 | JSObject* arguments_boilerplate = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2997 | heap->isolate()->context()->global_context()-> |
| 2998 | arguments_boilerplate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2999 | JSFunction* arguments_function = |
| 3000 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 3001 | |
| 3002 | // Get the context and don't check if it is the global context. |
| 3003 | JSFunction* f = JSFunction::cast(this); |
| 3004 | Context* context = f->context(); |
| 3005 | if (context->IsGlobalContext()) { |
| 3006 | return false; |
| 3007 | } |
| 3008 | |
| 3009 | // Check the non-special context slots. |
| 3010 | for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) { |
| 3011 | // Only check JS objects. |
| 3012 | if (context->get(i)->IsJSObject()) { |
| 3013 | JSObject* ctxobj = JSObject::cast(context->get(i)); |
| 3014 | // If it is an arguments array check the content. |
| 3015 | if (ctxobj->map()->constructor() == arguments_function) { |
| 3016 | if (ctxobj->ReferencesObject(obj)) { |
| 3017 | return true; |
| 3018 | } |
| 3019 | } else if (ctxobj == obj) { |
| 3020 | return true; |
| 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | // Check the context extension if any. |
| 3026 | if (context->has_extension()) { |
| 3027 | return context->extension()->ReferencesObject(obj); |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | // No references to object. |
| 3032 | return false; |
| 3033 | } |
| 3034 | |
| 3035 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3036 | MaybeObject* JSObject::PreventExtensions() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3037 | Isolate* isolate = GetIsolate(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 3038 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3039 | !isolate->MayNamedAccess(this, |
| 3040 | isolate->heap()->undefined_value(), |
| 3041 | v8::ACCESS_KEYS)) { |
| 3042 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_KEYS); |
| 3043 | return isolate->heap()->false_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 3044 | } |
| 3045 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 3046 | if (IsJSGlobalProxy()) { |
| 3047 | Object* proto = GetPrototype(); |
| 3048 | if (proto->IsNull()) return this; |
| 3049 | ASSERT(proto->IsJSGlobalObject()); |
| 3050 | return JSObject::cast(proto)->PreventExtensions(); |
| 3051 | } |
| 3052 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3053 | // If there are fast elements we normalize. |
| 3054 | if (HasFastElements()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3055 | Object* ok; |
| 3056 | { MaybeObject* maybe_ok = NormalizeElements(); |
| 3057 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3058 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3059 | } |
| 3060 | // Make sure that we never go back to fast case. |
| 3061 | element_dictionary()->set_requires_slow_elements(); |
| 3062 | |
| 3063 | // Do a map transition, other objects with this map may still |
| 3064 | // be extensible. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3065 | Object* new_map; |
| 3066 | { MaybeObject* maybe_new_map = map()->CopyDropTransitions(); |
| 3067 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 3068 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3069 | Map::cast(new_map)->set_is_extensible(false); |
| 3070 | set_map(Map::cast(new_map)); |
| 3071 | ASSERT(!map()->is_extensible()); |
| 3072 | return new_map; |
| 3073 | } |
| 3074 | |
| 3075 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3076 | // Tests for the fast common case for property enumeration: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3077 | // - This object and all prototypes has an enum cache (which means that it has |
| 3078 | // no interceptors and needs no access checks). |
| 3079 | // - This object has no elements. |
| 3080 | // - No prototype has enumerable properties/elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3081 | bool JSObject::IsSimpleEnum() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3082 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3083 | for (Object* o = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3084 | o != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3085 | o = JSObject::cast(o)->GetPrototype()) { |
| 3086 | JSObject* curr = JSObject::cast(o); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3087 | if (!curr->map()->instance_descriptors()->HasEnumCache()) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3088 | ASSERT(!curr->HasNamedInterceptor()); |
| 3089 | ASSERT(!curr->HasIndexedInterceptor()); |
| 3090 | ASSERT(!curr->IsAccessCheckNeeded()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3091 | if (curr->NumberOfEnumElements() > 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3092 | if (curr != this) { |
| 3093 | FixedArray* curr_fixed_array = |
| 3094 | FixedArray::cast(curr->map()->instance_descriptors()->GetEnumCache()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3095 | if (curr_fixed_array->length() > 0) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | return true; |
| 3099 | } |
| 3100 | |
| 3101 | |
| 3102 | int Map::NumberOfDescribedProperties() { |
| 3103 | int result = 0; |
| 3104 | DescriptorArray* descs = instance_descriptors(); |
| 3105 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3106 | if (descs->IsProperty(i)) result++; |
| 3107 | } |
| 3108 | return result; |
| 3109 | } |
| 3110 | |
| 3111 | |
| 3112 | int Map::PropertyIndexFor(String* name) { |
| 3113 | DescriptorArray* descs = instance_descriptors(); |
| 3114 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3115 | if (name->Equals(descs->GetKey(i)) && !descs->IsNullDescriptor(i)) { |
| 3116 | return descs->GetFieldIndex(i); |
| 3117 | } |
| 3118 | } |
| 3119 | return -1; |
| 3120 | } |
| 3121 | |
| 3122 | |
| 3123 | int Map::NextFreePropertyIndex() { |
| 3124 | int max_index = -1; |
| 3125 | DescriptorArray* descs = instance_descriptors(); |
| 3126 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3127 | if (descs->GetType(i) == FIELD) { |
| 3128 | int current_index = descs->GetFieldIndex(i); |
| 3129 | if (current_index > max_index) max_index = current_index; |
| 3130 | } |
| 3131 | } |
| 3132 | return max_index + 1; |
| 3133 | } |
| 3134 | |
| 3135 | |
| 3136 | AccessorDescriptor* Map::FindAccessor(String* name) { |
| 3137 | DescriptorArray* descs = instance_descriptors(); |
| 3138 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3139 | if (name->Equals(descs->GetKey(i)) && descs->GetType(i) == CALLBACKS) { |
| 3140 | return descs->GetCallbacks(i); |
| 3141 | } |
| 3142 | } |
| 3143 | return NULL; |
| 3144 | } |
| 3145 | |
| 3146 | |
| 3147 | void JSObject::LocalLookup(String* name, LookupResult* result) { |
| 3148 | ASSERT(name->IsString()); |
| 3149 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3150 | Heap* heap = GetHeap(); |
| 3151 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3152 | if (IsJSGlobalProxy()) { |
| 3153 | Object* proto = GetPrototype(); |
| 3154 | if (proto->IsNull()) return result->NotFound(); |
| 3155 | ASSERT(proto->IsJSGlobalObject()); |
| 3156 | return JSObject::cast(proto)->LocalLookup(name, result); |
| 3157 | } |
| 3158 | |
| 3159 | // Do not use inline caching if the object is a non-global object |
| 3160 | // that requires access checks. |
| 3161 | if (!IsJSGlobalProxy() && IsAccessCheckNeeded()) { |
| 3162 | result->DisallowCaching(); |
| 3163 | } |
| 3164 | |
| 3165 | // Check __proto__ before interceptor. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3166 | if (name->Equals(heap->Proto_symbol()) && |
| 3167 | !IsJSContextExtensionObject()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3168 | result->ConstantResult(this); |
| 3169 | return; |
| 3170 | } |
| 3171 | |
| 3172 | // Check for lookup interceptor except when bootstrapping. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3173 | if (HasNamedInterceptor() && !heap->isolate()->bootstrapper()->IsActive()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3174 | result->InterceptorResult(this); |
| 3175 | return; |
| 3176 | } |
| 3177 | |
| 3178 | LocalLookupRealNamedProperty(name, result); |
| 3179 | } |
| 3180 | |
| 3181 | |
| 3182 | void JSObject::Lookup(String* name, LookupResult* result) { |
| 3183 | // Ecma-262 3rd 8.6.2.4 |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3184 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3185 | for (Object* current = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3186 | current != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3187 | current = JSObject::cast(current)->GetPrototype()) { |
| 3188 | JSObject::cast(current)->LocalLookup(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3189 | if (result->IsProperty()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3190 | } |
| 3191 | result->NotFound(); |
| 3192 | } |
| 3193 | |
| 3194 | |
| 3195 | // Search object and it's prototype chain for callback properties. |
| 3196 | void JSObject::LookupCallback(String* name, LookupResult* result) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3197 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3198 | for (Object* current = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3199 | current != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3200 | current = JSObject::cast(current)->GetPrototype()) { |
| 3201 | JSObject::cast(current)->LocalLookupRealNamedProperty(name, result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3202 | if (result->IsProperty() && result->type() == CALLBACKS) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3203 | } |
| 3204 | result->NotFound(); |
| 3205 | } |
| 3206 | |
| 3207 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3208 | MaybeObject* JSObject::DefineGetterSetter(String* name, |
| 3209 | PropertyAttributes attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3210 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3211 | // Make sure that the top context does not change when doing callbacks or |
| 3212 | // interceptor calls. |
| 3213 | AssertNoContextChange ncc; |
| 3214 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3215 | // Try to flatten before operating on the string. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3216 | name->TryFlatten(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3217 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3218 | if (!CanSetCallback(name)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3219 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3222 | uint32_t index = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3223 | bool is_element = name->AsArrayIndex(&index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3224 | |
| 3225 | if (is_element) { |
| 3226 | switch (GetElementsKind()) { |
| 3227 | case FAST_ELEMENTS: |
| 3228 | break; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3229 | case EXTERNAL_PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3230 | case EXTERNAL_BYTE_ELEMENTS: |
| 3231 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 3232 | case EXTERNAL_SHORT_ELEMENTS: |
| 3233 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 3234 | case EXTERNAL_INT_ELEMENTS: |
| 3235 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 3236 | case EXTERNAL_FLOAT_ELEMENTS: |
| 3237 | // Ignore getters and setters on pixel and external array |
| 3238 | // elements. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3239 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3240 | case DICTIONARY_ELEMENTS: { |
| 3241 | // Lookup the index. |
| 3242 | NumberDictionary* dictionary = element_dictionary(); |
| 3243 | int entry = dictionary->FindEntry(index); |
| 3244 | if (entry != NumberDictionary::kNotFound) { |
| 3245 | Object* result = dictionary->ValueAt(entry); |
| 3246 | PropertyDetails details = dictionary->DetailsAt(entry); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3247 | if (details.IsReadOnly()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3248 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3249 | if (result->IsFixedArray()) { |
| 3250 | return result; |
| 3251 | } |
| 3252 | // Otherwise allow to override it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | break; |
| 3256 | } |
| 3257 | default: |
| 3258 | UNREACHABLE(); |
| 3259 | break; |
| 3260 | } |
| 3261 | } else { |
| 3262 | // Lookup the name. |
| 3263 | LookupResult result; |
| 3264 | LocalLookup(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3265 | if (result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3266 | if (result.IsReadOnly()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3267 | if (result.type() == CALLBACKS) { |
| 3268 | Object* obj = result.GetCallbackObject(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3269 | // Need to preserve old getters/setters. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3270 | if (obj->IsFixedArray()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3271 | // Use set to update attributes. |
| 3272 | return SetPropertyCallback(name, obj, attributes); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3273 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3274 | } |
| 3275 | } |
| 3276 | } |
| 3277 | |
| 3278 | // Allocate the fixed array to hold getter and setter. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3279 | Object* structure; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3280 | { MaybeObject* maybe_structure = heap->AllocateFixedArray(2, TENURED); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3281 | if (!maybe_structure->ToObject(&structure)) return maybe_structure; |
| 3282 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3283 | |
| 3284 | if (is_element) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3285 | return SetElementCallback(index, structure, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3286 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3287 | return SetPropertyCallback(name, structure, attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3288 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | |
| 3292 | bool JSObject::CanSetCallback(String* name) { |
| 3293 | ASSERT(!IsAccessCheckNeeded() |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3294 | || Isolate::Current()->MayNamedAccess(this, name, v8::ACCESS_SET)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3295 | |
| 3296 | // Check if there is an API defined callback object which prohibits |
| 3297 | // callback overwriting in this object or it's prototype chain. |
| 3298 | // This mechanism is needed for instance in a browser setting, where |
| 3299 | // certain accessors such as window.location should not be allowed |
| 3300 | // to be overwritten because allowing overwriting could potentially |
| 3301 | // cause security problems. |
| 3302 | LookupResult callback_result; |
| 3303 | LookupCallback(name, &callback_result); |
| 3304 | if (callback_result.IsProperty()) { |
| 3305 | Object* obj = callback_result.GetCallbackObject(); |
| 3306 | if (obj->IsAccessorInfo() && |
| 3307 | AccessorInfo::cast(obj)->prohibits_overwriting()) { |
| 3308 | return false; |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | return true; |
| 3313 | } |
| 3314 | |
| 3315 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3316 | MaybeObject* JSObject::SetElementCallback(uint32_t index, |
| 3317 | Object* structure, |
| 3318 | PropertyAttributes attributes) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3319 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS); |
| 3320 | |
| 3321 | // Normalize elements to make this operation simple. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3322 | Object* ok; |
| 3323 | { MaybeObject* maybe_ok = NormalizeElements(); |
| 3324 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3325 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3326 | |
| 3327 | // Update the dictionary with the new CALLBACKS property. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3328 | Object* dict; |
| 3329 | { MaybeObject* maybe_dict = |
| 3330 | element_dictionary()->Set(index, structure, details); |
| 3331 | if (!maybe_dict->ToObject(&dict)) return maybe_dict; |
| 3332 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3333 | |
| 3334 | NumberDictionary* elements = NumberDictionary::cast(dict); |
| 3335 | elements->set_requires_slow_elements(); |
| 3336 | // Set the potential new dictionary on the object. |
| 3337 | set_elements(elements); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3338 | |
| 3339 | return structure; |
| 3340 | } |
| 3341 | |
| 3342 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3343 | MaybeObject* JSObject::SetPropertyCallback(String* name, |
| 3344 | Object* structure, |
| 3345 | PropertyAttributes attributes) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3346 | PropertyDetails details = PropertyDetails(attributes, CALLBACKS); |
| 3347 | |
| 3348 | bool convert_back_to_fast = HasFastProperties() && |
| 3349 | (map()->instance_descriptors()->number_of_descriptors() |
| 3350 | < DescriptorArray::kMaxNumberOfDescriptors); |
| 3351 | |
| 3352 | // Normalize object to make this operation simple. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3353 | Object* ok; |
| 3354 | { MaybeObject* maybe_ok = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0); |
| 3355 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3356 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3357 | |
| 3358 | // For the global object allocate a new map to invalidate the global inline |
| 3359 | // caches which have a global property cell reference directly in the code. |
| 3360 | if (IsGlobalObject()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3361 | Object* new_map; |
| 3362 | { MaybeObject* maybe_new_map = map()->CopyDropDescriptors(); |
| 3363 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 3364 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3365 | set_map(Map::cast(new_map)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3366 | // When running crankshaft, changing the map is not enough. We |
| 3367 | // need to deoptimize all functions that rely on this global |
| 3368 | // object. |
| 3369 | Deoptimizer::DeoptimizeGlobalObject(this); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | // Update the dictionary with the new CALLBACKS property. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3373 | Object* result; |
| 3374 | { MaybeObject* maybe_result = SetNormalizedProperty(name, structure, details); |
| 3375 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3376 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3377 | |
| 3378 | if (convert_back_to_fast) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3379 | { MaybeObject* maybe_ok = TransformToFastProperties(0); |
| 3380 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3381 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3382 | } |
| 3383 | return result; |
| 3384 | } |
| 3385 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3386 | MaybeObject* JSObject::DefineAccessor(String* name, |
| 3387 | bool is_getter, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3388 | Object* fun, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3389 | PropertyAttributes attributes) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3390 | ASSERT(fun->IsJSFunction() || fun->IsUndefined()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3391 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3392 | // Check access rights if needed. |
| 3393 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3394 | !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 3395 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 3396 | return isolate->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | if (IsJSGlobalProxy()) { |
| 3400 | Object* proto = GetPrototype(); |
| 3401 | if (proto->IsNull()) return this; |
| 3402 | ASSERT(proto->IsJSGlobalObject()); |
| 3403 | return JSObject::cast(proto)->DefineAccessor(name, is_getter, |
| 3404 | fun, attributes); |
| 3405 | } |
| 3406 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3407 | Object* array; |
| 3408 | { MaybeObject* maybe_array = DefineGetterSetter(name, attributes); |
| 3409 | if (!maybe_array->ToObject(&array)) return maybe_array; |
| 3410 | } |
| 3411 | if (array->IsUndefined()) return array; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3412 | FixedArray::cast(array)->set(is_getter ? 0 : 1, fun); |
| 3413 | return this; |
| 3414 | } |
| 3415 | |
| 3416 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3417 | MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3418 | Isolate* isolate = GetIsolate(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3419 | String* name = String::cast(info->name()); |
| 3420 | // Check access rights if needed. |
| 3421 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3422 | !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) { |
| 3423 | isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 3424 | return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | if (IsJSGlobalProxy()) { |
| 3428 | Object* proto = GetPrototype(); |
| 3429 | if (proto->IsNull()) return this; |
| 3430 | ASSERT(proto->IsJSGlobalObject()); |
| 3431 | return JSObject::cast(proto)->DefineAccessor(info); |
| 3432 | } |
| 3433 | |
| 3434 | // Make sure that the top context does not change when doing callbacks or |
| 3435 | // interceptor calls. |
| 3436 | AssertNoContextChange ncc; |
| 3437 | |
| 3438 | // Try to flatten before operating on the string. |
| 3439 | name->TryFlatten(); |
| 3440 | |
| 3441 | if (!CanSetCallback(name)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3442 | return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | uint32_t index = 0; |
| 3446 | bool is_element = name->AsArrayIndex(&index); |
| 3447 | |
| 3448 | if (is_element) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3449 | if (IsJSArray()) return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3450 | |
| 3451 | // Accessors overwrite previous callbacks (cf. with getters/setters). |
| 3452 | switch (GetElementsKind()) { |
| 3453 | case FAST_ELEMENTS: |
| 3454 | break; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3455 | case EXTERNAL_PIXEL_ELEMENTS: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3456 | case EXTERNAL_BYTE_ELEMENTS: |
| 3457 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 3458 | case EXTERNAL_SHORT_ELEMENTS: |
| 3459 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 3460 | case EXTERNAL_INT_ELEMENTS: |
| 3461 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 3462 | case EXTERNAL_FLOAT_ELEMENTS: |
| 3463 | // Ignore getters and setters on pixel and external array |
| 3464 | // elements. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3465 | return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3466 | case DICTIONARY_ELEMENTS: |
| 3467 | break; |
| 3468 | default: |
| 3469 | UNREACHABLE(); |
| 3470 | break; |
| 3471 | } |
| 3472 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3473 | Object* ok; |
| 3474 | { MaybeObject* maybe_ok = |
| 3475 | SetElementCallback(index, info, info->property_attributes()); |
| 3476 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3477 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3478 | } else { |
| 3479 | // Lookup the name. |
| 3480 | LookupResult result; |
| 3481 | LocalLookup(name, &result); |
| 3482 | // ES5 forbids turning a property into an accessor if it's not |
| 3483 | // configurable (that is IsDontDelete in ES3 and v8), see 8.6.1 (Table 5). |
| 3484 | if (result.IsProperty() && (result.IsReadOnly() || result.IsDontDelete())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3485 | return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3486 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3487 | Object* ok; |
| 3488 | { MaybeObject* maybe_ok = |
| 3489 | SetPropertyCallback(name, info, info->property_attributes()); |
| 3490 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3491 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | return this; |
| 3495 | } |
| 3496 | |
| 3497 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3498 | Object* JSObject::LookupAccessor(String* name, bool is_getter) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3499 | Heap* heap = GetHeap(); |
| 3500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3501 | // Make sure that the top context does not change when doing callbacks or |
| 3502 | // interceptor calls. |
| 3503 | AssertNoContextChange ncc; |
| 3504 | |
| 3505 | // Check access rights if needed. |
| 3506 | if (IsAccessCheckNeeded() && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3507 | !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_HAS)) { |
| 3508 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 3509 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | // Make the lookup and include prototypes. |
| 3513 | int accessor_index = is_getter ? kGetterIndex : kSetterIndex; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3514 | uint32_t index = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3515 | if (name->AsArrayIndex(&index)) { |
| 3516 | for (Object* obj = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3517 | obj != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3518 | obj = JSObject::cast(obj)->GetPrototype()) { |
| 3519 | JSObject* js_object = JSObject::cast(obj); |
| 3520 | if (js_object->HasDictionaryElements()) { |
| 3521 | NumberDictionary* dictionary = js_object->element_dictionary(); |
| 3522 | int entry = dictionary->FindEntry(index); |
| 3523 | if (entry != NumberDictionary::kNotFound) { |
| 3524 | Object* element = dictionary->ValueAt(entry); |
| 3525 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 3526 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3527 | if (element->IsFixedArray()) { |
| 3528 | return FixedArray::cast(element)->get(accessor_index); |
| 3529 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | } |
| 3534 | } else { |
| 3535 | for (Object* obj = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3536 | obj != heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3537 | obj = JSObject::cast(obj)->GetPrototype()) { |
| 3538 | LookupResult result; |
| 3539 | JSObject::cast(obj)->LocalLookup(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3540 | if (result.IsProperty()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3541 | if (result.IsReadOnly()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3542 | if (result.type() == CALLBACKS) { |
| 3543 | Object* obj = result.GetCallbackObject(); |
| 3544 | if (obj->IsFixedArray()) { |
| 3545 | return FixedArray::cast(obj)->get(accessor_index); |
| 3546 | } |
| 3547 | } |
| 3548 | } |
| 3549 | } |
| 3550 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3551 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | |
| 3555 | Object* JSObject::SlowReverseLookup(Object* value) { |
| 3556 | if (HasFastProperties()) { |
| 3557 | DescriptorArray* descs = map()->instance_descriptors(); |
| 3558 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3559 | if (descs->GetType(i) == FIELD) { |
| 3560 | if (FastPropertyAt(descs->GetFieldIndex(i)) == value) { |
| 3561 | return descs->GetKey(i); |
| 3562 | } |
| 3563 | } else if (descs->GetType(i) == CONSTANT_FUNCTION) { |
| 3564 | if (descs->GetConstantFunction(i) == value) { |
| 3565 | return descs->GetKey(i); |
| 3566 | } |
| 3567 | } |
| 3568 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3569 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3570 | } else { |
| 3571 | return property_dictionary()->SlowReverseLookup(value); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3576 | MaybeObject* Map::CopyDropDescriptors() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3577 | Heap* heap = GetHeap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3578 | Object* result; |
| 3579 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3580 | heap->AllocateMap(instance_type(), instance_size()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3581 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3582 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3583 | Map::cast(result)->set_prototype(prototype()); |
| 3584 | Map::cast(result)->set_constructor(constructor()); |
| 3585 | // Don't copy descriptors, so map transitions always remain a forest. |
| 3586 | // If we retained the same descriptors we would have two maps |
| 3587 | // pointing to the same transition which is bad because the garbage |
| 3588 | // collector relies on being able to reverse pointers from transitions |
| 3589 | // to maps. If properties need to be retained use CopyDropTransitions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3590 | Map::cast(result)->set_instance_descriptors( |
| 3591 | heap->empty_descriptor_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3592 | // Please note instance_type and instance_size are set when allocated. |
| 3593 | Map::cast(result)->set_inobject_properties(inobject_properties()); |
| 3594 | Map::cast(result)->set_unused_property_fields(unused_property_fields()); |
| 3595 | |
| 3596 | // If the map has pre-allocated properties always start out with a descriptor |
| 3597 | // array describing these properties. |
| 3598 | if (pre_allocated_property_fields() > 0) { |
| 3599 | ASSERT(constructor()->IsJSFunction()); |
| 3600 | JSFunction* ctor = JSFunction::cast(constructor()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3601 | Object* descriptors; |
| 3602 | { MaybeObject* maybe_descriptors = |
| 3603 | ctor->initial_map()->instance_descriptors()->RemoveTransitions(); |
| 3604 | if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors; |
| 3605 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3606 | Map::cast(result)->set_instance_descriptors( |
| 3607 | DescriptorArray::cast(descriptors)); |
| 3608 | Map::cast(result)->set_pre_allocated_property_fields( |
| 3609 | pre_allocated_property_fields()); |
| 3610 | } |
| 3611 | Map::cast(result)->set_bit_field(bit_field()); |
| 3612 | Map::cast(result)->set_bit_field2(bit_field2()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3613 | Map::cast(result)->set_is_shared(false); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3614 | Map::cast(result)->ClearCodeCache(heap); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3615 | return result; |
| 3616 | } |
| 3617 | |
| 3618 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3619 | MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode, |
| 3620 | NormalizedMapSharingMode sharing) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3621 | int new_instance_size = instance_size(); |
| 3622 | if (mode == CLEAR_INOBJECT_PROPERTIES) { |
| 3623 | new_instance_size -= inobject_properties() * kPointerSize; |
| 3624 | } |
| 3625 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3626 | Object* result; |
| 3627 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3628 | GetHeap()->AllocateMap(instance_type(), new_instance_size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3629 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3630 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3631 | |
| 3632 | if (mode != CLEAR_INOBJECT_PROPERTIES) { |
| 3633 | Map::cast(result)->set_inobject_properties(inobject_properties()); |
| 3634 | } |
| 3635 | |
| 3636 | Map::cast(result)->set_prototype(prototype()); |
| 3637 | Map::cast(result)->set_constructor(constructor()); |
| 3638 | |
| 3639 | Map::cast(result)->set_bit_field(bit_field()); |
| 3640 | Map::cast(result)->set_bit_field2(bit_field2()); |
| 3641 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3642 | Map::cast(result)->set_is_shared(sharing == SHARED_NORMALIZED_MAP); |
| 3643 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3644 | #ifdef DEBUG |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3645 | if (Map::cast(result)->is_shared()) { |
| 3646 | Map::cast(result)->SharedMapVerify(); |
| 3647 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3648 | #endif |
| 3649 | |
| 3650 | return result; |
| 3651 | } |
| 3652 | |
| 3653 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3654 | MaybeObject* Map::CopyDropTransitions() { |
| 3655 | Object* new_map; |
| 3656 | { MaybeObject* maybe_new_map = CopyDropDescriptors(); |
| 3657 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 3658 | } |
| 3659 | Object* descriptors; |
| 3660 | { MaybeObject* maybe_descriptors = |
| 3661 | instance_descriptors()->RemoveTransitions(); |
| 3662 | if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors; |
| 3663 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3664 | cast(new_map)->set_instance_descriptors(DescriptorArray::cast(descriptors)); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3665 | return new_map; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3669 | MaybeObject* Map::UpdateCodeCache(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3670 | // Allocate the code cache if not present. |
| 3671 | if (code_cache()->IsFixedArray()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3672 | Object* result; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3673 | { MaybeObject* maybe_result = code->heap()->AllocateCodeCache(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3674 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3675 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3676 | set_code_cache(result); |
| 3677 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3678 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3679 | // Update the code cache. |
| 3680 | return CodeCache::cast(code_cache())->Update(name, code); |
| 3681 | } |
| 3682 | |
| 3683 | |
| 3684 | Object* Map::FindInCodeCache(String* name, Code::Flags flags) { |
| 3685 | // Do a lookup if a code cache exists. |
| 3686 | if (!code_cache()->IsFixedArray()) { |
| 3687 | return CodeCache::cast(code_cache())->Lookup(name, flags); |
| 3688 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3689 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | |
| 3694 | int Map::IndexInCodeCache(Object* name, Code* code) { |
| 3695 | // Get the internal index if a code cache exists. |
| 3696 | if (!code_cache()->IsFixedArray()) { |
| 3697 | return CodeCache::cast(code_cache())->GetIndex(name, code); |
| 3698 | } |
| 3699 | return -1; |
| 3700 | } |
| 3701 | |
| 3702 | |
| 3703 | void Map::RemoveFromCodeCache(String* name, Code* code, int index) { |
| 3704 | // No GC is supposed to happen between a call to IndexInCodeCache and |
| 3705 | // RemoveFromCodeCache so the code cache must be there. |
| 3706 | ASSERT(!code_cache()->IsFixedArray()); |
| 3707 | CodeCache::cast(code_cache())->RemoveByIndex(name, code, index); |
| 3708 | } |
| 3709 | |
| 3710 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3711 | void Map::TraverseTransitionTree(TraverseCallback callback, void* data) { |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3712 | // Traverse the transition tree without using a stack. We do this by |
| 3713 | // reversing the pointers in the maps and descriptor arrays. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3714 | Map* current = this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3715 | Map* meta_map = heap()->meta_map(); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3716 | Object** map_or_index_field = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3717 | while (current != meta_map) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3718 | DescriptorArray* d = reinterpret_cast<DescriptorArray*>( |
| 3719 | *RawField(current, Map::kInstanceDescriptorsOffset)); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3720 | if (!d->IsEmpty()) { |
| 3721 | FixedArray* contents = reinterpret_cast<FixedArray*>( |
| 3722 | d->get(DescriptorArray::kContentArrayIndex)); |
| 3723 | map_or_index_field = RawField(contents, HeapObject::kMapOffset); |
| 3724 | Object* map_or_index = *map_or_index_field; |
| 3725 | bool map_done = true; // Controls a nested continue statement. |
| 3726 | for (int i = map_or_index->IsSmi() ? Smi::cast(map_or_index)->value() : 0; |
| 3727 | i < contents->length(); |
| 3728 | i += 2) { |
| 3729 | PropertyDetails details(Smi::cast(contents->get(i + 1))); |
| 3730 | if (details.IsTransition()) { |
| 3731 | // Found a map in the transition array. We record our progress in |
| 3732 | // the transition array by recording the current map in the map field |
| 3733 | // of the next map and recording the index in the transition array in |
| 3734 | // the map field of the array. |
| 3735 | Map* next = Map::cast(contents->get(i)); |
| 3736 | next->set_map(current); |
| 3737 | *map_or_index_field = Smi::FromInt(i + 2); |
| 3738 | current = next; |
| 3739 | map_done = false; |
| 3740 | break; |
| 3741 | } |
| 3742 | } |
| 3743 | if (!map_done) continue; |
Ben Murdoch | 7d3e7fc | 2011-07-12 16:37:06 +0100 | [diff] [blame^] | 3744 | } else { |
| 3745 | map_or_index_field = NULL; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3746 | } |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3747 | // That was the regular transitions, now for the prototype transitions. |
| 3748 | FixedArray* prototype_transitions = |
| 3749 | current->unchecked_prototype_transitions(); |
| 3750 | Object** proto_map_or_index_field = |
| 3751 | RawField(prototype_transitions, HeapObject::kMapOffset); |
| 3752 | Object* map_or_index = *proto_map_or_index_field; |
| 3753 | const int start = 2; |
| 3754 | int i = map_or_index->IsSmi() ? Smi::cast(map_or_index)->value() : start; |
| 3755 | if (i < prototype_transitions->length()) { |
| 3756 | // Found a map in the prototype transition array. Record progress in |
| 3757 | // an analogous way to the regular transitions array above. |
| 3758 | Object* perhaps_map = prototype_transitions->get(i); |
| 3759 | if (perhaps_map->IsMap()) { |
| 3760 | Map* next = Map::cast(perhaps_map); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3761 | next->set_map(current); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3762 | *proto_map_or_index_field = |
| 3763 | Smi::FromInt(i + 2); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3764 | current = next; |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3765 | continue; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3766 | } |
| 3767 | } |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 3768 | *proto_map_or_index_field = heap()->fixed_array_map(); |
| 3769 | if (map_or_index_field != NULL) { |
| 3770 | *map_or_index_field = heap()->fixed_array_map(); |
| 3771 | } |
| 3772 | |
| 3773 | // The callback expects a map to have a real map as its map, so we save |
| 3774 | // the map field, which is being used to track the traversal and put the |
| 3775 | // correct map (the meta_map) in place while we do the callback. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3776 | Map* prev = current->map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3777 | current->set_map(meta_map); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3778 | callback(current, data); |
| 3779 | current = prev; |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3784 | MaybeObject* CodeCache::Update(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3785 | ASSERT(code->ic_state() == MONOMORPHIC); |
| 3786 | |
| 3787 | // The number of monomorphic stubs for normal load/store/call IC's can grow to |
| 3788 | // a large number and therefore they need to go into a hash table. They are |
| 3789 | // used to load global properties from cells. |
| 3790 | if (code->type() == NORMAL) { |
| 3791 | // Make sure that a hash table is allocated for the normal load code cache. |
| 3792 | if (normal_type_cache()->IsUndefined()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3793 | Object* result; |
| 3794 | { MaybeObject* maybe_result = |
| 3795 | CodeCacheHashTable::Allocate(CodeCacheHashTable::kInitialSize); |
| 3796 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3797 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3798 | set_normal_type_cache(result); |
| 3799 | } |
| 3800 | return UpdateNormalTypeCache(name, code); |
| 3801 | } else { |
| 3802 | ASSERT(default_cache()->IsFixedArray()); |
| 3803 | return UpdateDefaultCache(name, code); |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3808 | MaybeObject* CodeCache::UpdateDefaultCache(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3809 | // 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] | 3810 | // flags. This allows call constant stubs to overwrite call field |
| 3811 | // stubs, etc. |
| 3812 | Code::Flags flags = Code::RemoveTypeFromFlags(code->flags()); |
| 3813 | |
| 3814 | // First check whether we can update existing code cache without |
| 3815 | // extending it. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3816 | FixedArray* cache = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3817 | int length = cache->length(); |
| 3818 | int deleted_index = -1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3819 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3820 | Object* key = cache->get(i); |
| 3821 | if (key->IsNull()) { |
| 3822 | if (deleted_index < 0) deleted_index = i; |
| 3823 | continue; |
| 3824 | } |
| 3825 | if (key->IsUndefined()) { |
| 3826 | if (deleted_index >= 0) i = deleted_index; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3827 | cache->set(i + kCodeCacheEntryNameOffset, name); |
| 3828 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3829 | return this; |
| 3830 | } |
| 3831 | if (name->Equals(String::cast(key))) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3832 | Code::Flags found = |
| 3833 | Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3834 | if (Code::RemoveTypeFromFlags(found) == flags) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3835 | cache->set(i + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3836 | return this; |
| 3837 | } |
| 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | // Reached the end of the code cache. If there were deleted |
| 3842 | // elements, reuse the space for the first of them. |
| 3843 | if (deleted_index >= 0) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3844 | cache->set(deleted_index + kCodeCacheEntryNameOffset, name); |
| 3845 | cache->set(deleted_index + kCodeCacheEntryCodeOffset, code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3846 | return this; |
| 3847 | } |
| 3848 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3849 | // Extend the code cache with some new entries (at least one). Must be a |
| 3850 | // multiple of the entry size. |
| 3851 | int new_length = length + ((length >> 1)) + kCodeCacheEntrySize; |
| 3852 | new_length = new_length - new_length % kCodeCacheEntrySize; |
| 3853 | ASSERT((new_length % kCodeCacheEntrySize) == 0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3854 | Object* result; |
| 3855 | { MaybeObject* maybe_result = cache->CopySize(new_length); |
| 3856 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3857 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3858 | |
| 3859 | // Add the (name, code) pair to the new cache. |
| 3860 | cache = FixedArray::cast(result); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3861 | cache->set(length + kCodeCacheEntryNameOffset, name); |
| 3862 | cache->set(length + kCodeCacheEntryCodeOffset, code); |
| 3863 | set_default_cache(cache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3864 | return this; |
| 3865 | } |
| 3866 | |
| 3867 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3868 | MaybeObject* CodeCache::UpdateNormalTypeCache(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3869 | // Adding a new entry can cause a new cache to be allocated. |
| 3870 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3871 | Object* new_cache; |
| 3872 | { MaybeObject* maybe_new_cache = cache->Put(name, code); |
| 3873 | if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache; |
| 3874 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3875 | set_normal_type_cache(new_cache); |
| 3876 | return this; |
| 3877 | } |
| 3878 | |
| 3879 | |
| 3880 | Object* CodeCache::Lookup(String* name, Code::Flags flags) { |
| 3881 | if (Code::ExtractTypeFromFlags(flags) == NORMAL) { |
| 3882 | return LookupNormalTypeCache(name, flags); |
| 3883 | } else { |
| 3884 | return LookupDefaultCache(name, flags); |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | |
| 3889 | Object* CodeCache::LookupDefaultCache(String* name, Code::Flags flags) { |
| 3890 | FixedArray* cache = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3891 | int length = cache->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3892 | for (int i = 0; i < length; i += kCodeCacheEntrySize) { |
| 3893 | Object* key = cache->get(i + kCodeCacheEntryNameOffset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3894 | // Skip deleted elements. |
| 3895 | if (key->IsNull()) continue; |
| 3896 | if (key->IsUndefined()) return key; |
| 3897 | if (name->Equals(String::cast(key))) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3898 | Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset)); |
| 3899 | if (code->flags() == flags) { |
| 3900 | return code; |
| 3901 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3904 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3908 | Object* CodeCache::LookupNormalTypeCache(String* name, Code::Flags flags) { |
| 3909 | if (!normal_type_cache()->IsUndefined()) { |
| 3910 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3911 | return cache->Lookup(name, flags); |
| 3912 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3913 | return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | |
| 3918 | int CodeCache::GetIndex(Object* name, Code* code) { |
| 3919 | if (code->type() == NORMAL) { |
| 3920 | if (normal_type_cache()->IsUndefined()) return -1; |
| 3921 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3922 | return cache->GetIndex(String::cast(name), code->flags()); |
| 3923 | } |
| 3924 | |
| 3925 | FixedArray* array = default_cache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3926 | int len = array->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3927 | for (int i = 0; i < len; i += kCodeCacheEntrySize) { |
| 3928 | if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3929 | } |
| 3930 | return -1; |
| 3931 | } |
| 3932 | |
| 3933 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3934 | void CodeCache::RemoveByIndex(Object* name, Code* code, int index) { |
| 3935 | if (code->type() == NORMAL) { |
| 3936 | ASSERT(!normal_type_cache()->IsUndefined()); |
| 3937 | CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache()); |
| 3938 | ASSERT(cache->GetIndex(String::cast(name), code->flags()) == index); |
| 3939 | cache->RemoveByIndex(index); |
| 3940 | } else { |
| 3941 | FixedArray* array = default_cache(); |
| 3942 | ASSERT(array->length() >= index && array->get(index)->IsCode()); |
| 3943 | // Use null instead of undefined for deleted elements to distinguish |
| 3944 | // deleted elements from unused elements. This distinction is used |
| 3945 | // when looking up in the cache and when updating the cache. |
| 3946 | ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset); |
| 3947 | array->set_null(index - 1); // Name. |
| 3948 | array->set_null(index); // Code. |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | // The key in the code cache hash table consists of the property name and the |
| 3954 | // code object. The actual match is on the name and the code flags. If a key |
| 3955 | // is created using the flags and not a code object it can only be used for |
| 3956 | // lookup not to create a new entry. |
| 3957 | class CodeCacheHashTableKey : public HashTableKey { |
| 3958 | public: |
| 3959 | CodeCacheHashTableKey(String* name, Code::Flags flags) |
| 3960 | : name_(name), flags_(flags), code_(NULL) { } |
| 3961 | |
| 3962 | CodeCacheHashTableKey(String* name, Code* code) |
| 3963 | : name_(name), |
| 3964 | flags_(code->flags()), |
| 3965 | code_(code) { } |
| 3966 | |
| 3967 | |
| 3968 | bool IsMatch(Object* other) { |
| 3969 | if (!other->IsFixedArray()) return false; |
| 3970 | FixedArray* pair = FixedArray::cast(other); |
| 3971 | String* name = String::cast(pair->get(0)); |
| 3972 | Code::Flags flags = Code::cast(pair->get(1))->flags(); |
| 3973 | if (flags != flags_) { |
| 3974 | return false; |
| 3975 | } |
| 3976 | return name_->Equals(name); |
| 3977 | } |
| 3978 | |
| 3979 | static uint32_t NameFlagsHashHelper(String* name, Code::Flags flags) { |
| 3980 | return name->Hash() ^ flags; |
| 3981 | } |
| 3982 | |
| 3983 | uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); } |
| 3984 | |
| 3985 | uint32_t HashForObject(Object* obj) { |
| 3986 | FixedArray* pair = FixedArray::cast(obj); |
| 3987 | String* name = String::cast(pair->get(0)); |
| 3988 | Code* code = Code::cast(pair->get(1)); |
| 3989 | return NameFlagsHashHelper(name, code->flags()); |
| 3990 | } |
| 3991 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3992 | MUST_USE_RESULT MaybeObject* AsObject() { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3993 | ASSERT(code_ != NULL); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3994 | Object* obj; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3995 | { MaybeObject* maybe_obj = code_->heap()->AllocateFixedArray(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3996 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3997 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3998 | FixedArray* pair = FixedArray::cast(obj); |
| 3999 | pair->set(0, name_); |
| 4000 | pair->set(1, code_); |
| 4001 | return pair; |
| 4002 | } |
| 4003 | |
| 4004 | private: |
| 4005 | String* name_; |
| 4006 | Code::Flags flags_; |
| 4007 | Code* code_; |
| 4008 | }; |
| 4009 | |
| 4010 | |
| 4011 | Object* CodeCacheHashTable::Lookup(String* name, Code::Flags flags) { |
| 4012 | CodeCacheHashTableKey key(name, flags); |
| 4013 | int entry = FindEntry(&key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4014 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4015 | return get(EntryToIndex(entry) + 1); |
| 4016 | } |
| 4017 | |
| 4018 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4019 | MaybeObject* CodeCacheHashTable::Put(String* name, Code* code) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4020 | CodeCacheHashTableKey key(name, code); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4021 | Object* obj; |
| 4022 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 4023 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4024 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4025 | |
| 4026 | // Don't use this, as the table might have grown. |
| 4027 | CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj); |
| 4028 | |
| 4029 | int entry = cache->FindInsertionEntry(key.Hash()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4030 | Object* k; |
| 4031 | { MaybeObject* maybe_k = key.AsObject(); |
| 4032 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 4033 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4034 | |
| 4035 | cache->set(EntryToIndex(entry), k); |
| 4036 | cache->set(EntryToIndex(entry) + 1, code); |
| 4037 | cache->ElementAdded(); |
| 4038 | return cache; |
| 4039 | } |
| 4040 | |
| 4041 | |
| 4042 | int CodeCacheHashTable::GetIndex(String* name, Code::Flags flags) { |
| 4043 | CodeCacheHashTableKey key(name, flags); |
| 4044 | int entry = FindEntry(&key); |
| 4045 | return (entry == kNotFound) ? -1 : entry; |
| 4046 | } |
| 4047 | |
| 4048 | |
| 4049 | void CodeCacheHashTable::RemoveByIndex(int index) { |
| 4050 | ASSERT(index >= 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4051 | Heap* heap = GetHeap(); |
| 4052 | set(EntryToIndex(index), heap->null_value()); |
| 4053 | set(EntryToIndex(index) + 1, heap->null_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4054 | ElementRemoved(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4058 | static bool HasKey(FixedArray* array, Object* key) { |
| 4059 | int len0 = array->length(); |
| 4060 | for (int i = 0; i < len0; i++) { |
| 4061 | Object* element = array->get(i); |
| 4062 | if (element->IsSmi() && key->IsSmi() && (element == key)) return true; |
| 4063 | if (element->IsString() && |
| 4064 | key->IsString() && String::cast(element)->Equals(String::cast(key))) { |
| 4065 | return true; |
| 4066 | } |
| 4067 | } |
| 4068 | return false; |
| 4069 | } |
| 4070 | |
| 4071 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4072 | MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4073 | ASSERT(!array->HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4074 | switch (array->GetElementsKind()) { |
| 4075 | case JSObject::FAST_ELEMENTS: |
| 4076 | return UnionOfKeys(FixedArray::cast(array->elements())); |
| 4077 | case JSObject::DICTIONARY_ELEMENTS: { |
| 4078 | NumberDictionary* dict = array->element_dictionary(); |
| 4079 | int size = dict->NumberOfElements(); |
| 4080 | |
| 4081 | // Allocate a temporary fixed array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4082 | Object* object; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4083 | { MaybeObject* maybe_object = GetHeap()->AllocateFixedArray(size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4084 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 4085 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4086 | FixedArray* key_array = FixedArray::cast(object); |
| 4087 | |
| 4088 | int capacity = dict->Capacity(); |
| 4089 | int pos = 0; |
| 4090 | // Copy the elements from the JSArray to the temporary fixed array. |
| 4091 | for (int i = 0; i < capacity; i++) { |
| 4092 | if (dict->IsKey(dict->KeyAt(i))) { |
| 4093 | key_array->set(pos++, dict->ValueAt(i)); |
| 4094 | } |
| 4095 | } |
| 4096 | // Compute the union of this and the temporary fixed array. |
| 4097 | return UnionOfKeys(key_array); |
| 4098 | } |
| 4099 | default: |
| 4100 | UNREACHABLE(); |
| 4101 | } |
| 4102 | UNREACHABLE(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4103 | return GetHeap()->null_value(); // Failure case needs to "return" a value. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4107 | MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4108 | int len0 = length(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4109 | #ifdef DEBUG |
| 4110 | if (FLAG_enable_slow_asserts) { |
| 4111 | for (int i = 0; i < len0; i++) { |
| 4112 | ASSERT(get(i)->IsString() || get(i)->IsNumber()); |
| 4113 | } |
| 4114 | } |
| 4115 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4116 | int len1 = other->length(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4117 | // Optimize if 'other' is empty. |
| 4118 | // We cannot optimize if 'this' is empty, as other may have holes |
| 4119 | // or non keys. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4120 | if (len1 == 0) return this; |
| 4121 | |
| 4122 | // Compute how many elements are not in this. |
| 4123 | int extra = 0; |
| 4124 | for (int y = 0; y < len1; y++) { |
| 4125 | Object* value = other->get(y); |
| 4126 | if (!value->IsTheHole() && !HasKey(this, value)) extra++; |
| 4127 | } |
| 4128 | |
| 4129 | if (extra == 0) return this; |
| 4130 | |
| 4131 | // Allocate the result |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4132 | Object* obj; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4133 | { MaybeObject* maybe_obj = GetHeap()->AllocateFixedArray(len0 + extra); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4134 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4135 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4136 | // Fill in the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4137 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4138 | FixedArray* result = FixedArray::cast(obj); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4139 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4140 | for (int i = 0; i < len0; i++) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4141 | Object* e = get(i); |
| 4142 | ASSERT(e->IsString() || e->IsNumber()); |
| 4143 | result->set(i, e, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4144 | } |
| 4145 | // Fill in the extra keys. |
| 4146 | int index = 0; |
| 4147 | for (int y = 0; y < len1; y++) { |
| 4148 | Object* value = other->get(y); |
| 4149 | if (!value->IsTheHole() && !HasKey(this, value)) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4150 | Object* e = other->get(y); |
| 4151 | ASSERT(e->IsString() || e->IsNumber()); |
| 4152 | result->set(len0 + index, e, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4153 | index++; |
| 4154 | } |
| 4155 | } |
| 4156 | ASSERT(extra == index); |
| 4157 | return result; |
| 4158 | } |
| 4159 | |
| 4160 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4161 | MaybeObject* FixedArray::CopySize(int new_length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4162 | Heap* heap = GetHeap(); |
| 4163 | if (new_length == 0) return heap->empty_fixed_array(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4164 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4165 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4166 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4167 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4168 | FixedArray* result = FixedArray::cast(obj); |
| 4169 | // Copy the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4170 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4171 | int len = length(); |
| 4172 | if (new_length < len) len = new_length; |
| 4173 | result->set_map(map()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4174 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4175 | for (int i = 0; i < len; i++) { |
| 4176 | result->set(i, get(i), mode); |
| 4177 | } |
| 4178 | return result; |
| 4179 | } |
| 4180 | |
| 4181 | |
| 4182 | void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4183 | AssertNoAllocation no_gc; |
| 4184 | WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4185 | for (int index = 0; index < len; index++) { |
| 4186 | dest->set(dest_pos+index, get(pos+index), mode); |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | #ifdef DEBUG |
| 4192 | bool FixedArray::IsEqualTo(FixedArray* other) { |
| 4193 | if (length() != other->length()) return false; |
| 4194 | for (int i = 0 ; i < length(); ++i) { |
| 4195 | if (get(i) != other->get(i)) return false; |
| 4196 | } |
| 4197 | return true; |
| 4198 | } |
| 4199 | #endif |
| 4200 | |
| 4201 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4202 | MaybeObject* DescriptorArray::Allocate(int number_of_descriptors) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4203 | Heap* heap = Isolate::Current()->heap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4204 | if (number_of_descriptors == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4205 | return heap->empty_descriptor_array(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4206 | } |
| 4207 | // Allocate the array of keys. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4208 | Object* array; |
| 4209 | { MaybeObject* maybe_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4210 | heap->AllocateFixedArray(ToKeyIndex(number_of_descriptors)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4211 | if (!maybe_array->ToObject(&array)) return maybe_array; |
| 4212 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4213 | // Do not use DescriptorArray::cast on incomplete object. |
| 4214 | FixedArray* result = FixedArray::cast(array); |
| 4215 | |
| 4216 | // Allocate the content array and set it in the descriptor array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4217 | { MaybeObject* maybe_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4218 | heap->AllocateFixedArray(number_of_descriptors << 1); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4219 | if (!maybe_array->ToObject(&array)) return maybe_array; |
| 4220 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4221 | result->set(kContentArrayIndex, array); |
| 4222 | result->set(kEnumerationIndexIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4223 | Smi::FromInt(PropertyDetails::kInitialIndex)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | return result; |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | void DescriptorArray::SetEnumCache(FixedArray* bridge_storage, |
| 4229 | FixedArray* new_cache) { |
| 4230 | ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength); |
| 4231 | if (HasEnumCache()) { |
| 4232 | FixedArray::cast(get(kEnumerationIndexIndex))-> |
| 4233 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 4234 | } else { |
| 4235 | if (IsEmpty()) return; // Do nothing for empty descriptor array. |
| 4236 | FixedArray::cast(bridge_storage)-> |
| 4237 | set(kEnumCacheBridgeCacheIndex, new_cache); |
| 4238 | fast_set(FixedArray::cast(bridge_storage), |
| 4239 | kEnumCacheBridgeEnumIndex, |
| 4240 | get(kEnumerationIndexIndex)); |
| 4241 | set(kEnumerationIndexIndex, bridge_storage); |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4246 | MaybeObject* DescriptorArray::CopyInsert(Descriptor* descriptor, |
| 4247 | TransitionFlag transition_flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4248 | // Transitions are only kept when inserting another transition. |
| 4249 | // This precondition is not required by this function's implementation, but |
| 4250 | // is currently required by the semantics of maps, so we check it. |
| 4251 | // Conversely, we filter after replacing, so replacing a transition and |
| 4252 | // removing all other transitions is not supported. |
| 4253 | bool remove_transitions = transition_flag == REMOVE_TRANSITIONS; |
| 4254 | ASSERT(remove_transitions == !descriptor->GetDetails().IsTransition()); |
| 4255 | ASSERT(descriptor->GetDetails().type() != NULL_DESCRIPTOR); |
| 4256 | |
| 4257 | // Ensure the key is a symbol. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4258 | Object* result; |
| 4259 | { MaybeObject* maybe_result = descriptor->KeyToSymbol(); |
| 4260 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4261 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4262 | |
| 4263 | int transitions = 0; |
| 4264 | int null_descriptors = 0; |
| 4265 | if (remove_transitions) { |
| 4266 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 4267 | if (IsTransition(i)) transitions++; |
| 4268 | if (IsNullDescriptor(i)) null_descriptors++; |
| 4269 | } |
| 4270 | } else { |
| 4271 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 4272 | if (IsNullDescriptor(i)) null_descriptors++; |
| 4273 | } |
| 4274 | } |
| 4275 | int new_size = number_of_descriptors() - transitions - null_descriptors; |
| 4276 | |
| 4277 | // If key is in descriptor, we replace it in-place when filtering. |
| 4278 | // Count a null descriptor for key as inserted, not replaced. |
| 4279 | int index = Search(descriptor->GetKey()); |
| 4280 | const bool inserting = (index == kNotFound); |
| 4281 | const bool replacing = !inserting; |
| 4282 | bool keep_enumeration_index = false; |
| 4283 | if (inserting) { |
| 4284 | ++new_size; |
| 4285 | } |
| 4286 | if (replacing) { |
| 4287 | // We are replacing an existing descriptor. We keep the enumeration |
| 4288 | // index of a visible property. |
| 4289 | PropertyType t = PropertyDetails(GetDetails(index)).type(); |
| 4290 | if (t == CONSTANT_FUNCTION || |
| 4291 | t == FIELD || |
| 4292 | t == CALLBACKS || |
| 4293 | t == INTERCEPTOR) { |
| 4294 | keep_enumeration_index = true; |
| 4295 | } else if (remove_transitions) { |
| 4296 | // Replaced descriptor has been counted as removed if it is |
| 4297 | // a transition that will be replaced. Adjust count in this case. |
| 4298 | ++new_size; |
| 4299 | } |
| 4300 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4301 | { MaybeObject* maybe_result = Allocate(new_size); |
| 4302 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4303 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4304 | DescriptorArray* new_descriptors = DescriptorArray::cast(result); |
| 4305 | // Set the enumeration index in the descriptors and set the enumeration index |
| 4306 | // in the result. |
| 4307 | int enumeration_index = NextEnumerationIndex(); |
| 4308 | if (!descriptor->GetDetails().IsTransition()) { |
| 4309 | if (keep_enumeration_index) { |
| 4310 | descriptor->SetEnumerationIndex( |
| 4311 | PropertyDetails(GetDetails(index)).index()); |
| 4312 | } else { |
| 4313 | descriptor->SetEnumerationIndex(enumeration_index); |
| 4314 | ++enumeration_index; |
| 4315 | } |
| 4316 | } |
| 4317 | new_descriptors->SetNextEnumerationIndex(enumeration_index); |
| 4318 | |
| 4319 | // Copy the descriptors, filtering out transitions and null descriptors, |
| 4320 | // and inserting or replacing a descriptor. |
| 4321 | uint32_t descriptor_hash = descriptor->GetKey()->Hash(); |
| 4322 | int from_index = 0; |
| 4323 | int to_index = 0; |
| 4324 | |
| 4325 | for (; from_index < number_of_descriptors(); from_index++) { |
| 4326 | String* key = GetKey(from_index); |
| 4327 | if (key->Hash() > descriptor_hash || key == descriptor->GetKey()) { |
| 4328 | break; |
| 4329 | } |
| 4330 | if (IsNullDescriptor(from_index)) continue; |
| 4331 | if (remove_transitions && IsTransition(from_index)) continue; |
| 4332 | new_descriptors->CopyFrom(to_index++, this, from_index); |
| 4333 | } |
| 4334 | |
| 4335 | new_descriptors->Set(to_index++, descriptor); |
| 4336 | if (replacing) from_index++; |
| 4337 | |
| 4338 | for (; from_index < number_of_descriptors(); from_index++) { |
| 4339 | if (IsNullDescriptor(from_index)) continue; |
| 4340 | if (remove_transitions && IsTransition(from_index)) continue; |
| 4341 | new_descriptors->CopyFrom(to_index++, this, from_index); |
| 4342 | } |
| 4343 | |
| 4344 | ASSERT(to_index == new_descriptors->number_of_descriptors()); |
| 4345 | SLOW_ASSERT(new_descriptors->IsSortedNoDuplicates()); |
| 4346 | |
| 4347 | return new_descriptors; |
| 4348 | } |
| 4349 | |
| 4350 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4351 | MaybeObject* DescriptorArray::RemoveTransitions() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4352 | // Remove all transitions and null descriptors. Return a copy of the array |
| 4353 | // with all transitions removed, or a Failure object if the new array could |
| 4354 | // not be allocated. |
| 4355 | |
| 4356 | // Compute the size of the map transition entries to be removed. |
| 4357 | int num_removed = 0; |
| 4358 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 4359 | if (!IsProperty(i)) num_removed++; |
| 4360 | } |
| 4361 | |
| 4362 | // Allocate the new descriptor array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4363 | Object* result; |
| 4364 | { MaybeObject* maybe_result = Allocate(number_of_descriptors() - num_removed); |
| 4365 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4366 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4367 | DescriptorArray* new_descriptors = DescriptorArray::cast(result); |
| 4368 | |
| 4369 | // Copy the content. |
| 4370 | int next_descriptor = 0; |
| 4371 | for (int i = 0; i < number_of_descriptors(); i++) { |
| 4372 | if (IsProperty(i)) new_descriptors->CopyFrom(next_descriptor++, this, i); |
| 4373 | } |
| 4374 | ASSERT(next_descriptor == new_descriptors->number_of_descriptors()); |
| 4375 | |
| 4376 | return new_descriptors; |
| 4377 | } |
| 4378 | |
| 4379 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4380 | void DescriptorArray::SortUnchecked() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4381 | // In-place heap sort. |
| 4382 | int len = number_of_descriptors(); |
| 4383 | |
| 4384 | // Bottom-up max-heap construction. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4385 | // Index of the last node with children |
| 4386 | const int max_parent_index = (len / 2) - 1; |
| 4387 | for (int i = max_parent_index; i >= 0; --i) { |
| 4388 | int parent_index = i; |
| 4389 | const uint32_t parent_hash = GetKey(i)->Hash(); |
| 4390 | while (parent_index <= max_parent_index) { |
| 4391 | int child_index = 2 * parent_index + 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4392 | uint32_t child_hash = GetKey(child_index)->Hash(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4393 | if (child_index + 1 < len) { |
| 4394 | uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
| 4395 | if (right_child_hash > child_hash) { |
| 4396 | child_index++; |
| 4397 | child_hash = right_child_hash; |
| 4398 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4399 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4400 | if (child_hash <= parent_hash) break; |
| 4401 | Swap(parent_index, child_index); |
| 4402 | // Now element at child_index could be < its children. |
| 4403 | parent_index = child_index; // parent_hash remains correct. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | // Extract elements and create sorted array. |
| 4408 | for (int i = len - 1; i > 0; --i) { |
| 4409 | // Put max element at the back of the array. |
| 4410 | Swap(0, i); |
| 4411 | // Sift down the new top element. |
| 4412 | int parent_index = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4413 | const uint32_t parent_hash = GetKey(parent_index)->Hash(); |
| 4414 | const int max_parent_index = (i / 2) - 1; |
| 4415 | while (parent_index <= max_parent_index) { |
| 4416 | int child_index = parent_index * 2 + 1; |
| 4417 | uint32_t child_hash = GetKey(child_index)->Hash(); |
| 4418 | if (child_index + 1 < i) { |
| 4419 | uint32_t right_child_hash = GetKey(child_index + 1)->Hash(); |
| 4420 | if (right_child_hash > child_hash) { |
| 4421 | child_index++; |
| 4422 | child_hash = right_child_hash; |
| 4423 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4424 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4425 | if (child_hash <= parent_hash) break; |
| 4426 | Swap(parent_index, child_index); |
| 4427 | parent_index = child_index; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4428 | } |
| 4429 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4430 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4431 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4432 | |
| 4433 | void DescriptorArray::Sort() { |
| 4434 | SortUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4435 | SLOW_ASSERT(IsSortedNoDuplicates()); |
| 4436 | } |
| 4437 | |
| 4438 | |
| 4439 | int DescriptorArray::BinarySearch(String* name, int low, int high) { |
| 4440 | uint32_t hash = name->Hash(); |
| 4441 | |
| 4442 | while (low <= high) { |
| 4443 | int mid = (low + high) / 2; |
| 4444 | String* mid_name = GetKey(mid); |
| 4445 | uint32_t mid_hash = mid_name->Hash(); |
| 4446 | |
| 4447 | if (mid_hash > hash) { |
| 4448 | high = mid - 1; |
| 4449 | continue; |
| 4450 | } |
| 4451 | if (mid_hash < hash) { |
| 4452 | low = mid + 1; |
| 4453 | continue; |
| 4454 | } |
| 4455 | // Found an element with the same hash-code. |
| 4456 | ASSERT(hash == mid_hash); |
| 4457 | // There might be more, so we find the first one and |
| 4458 | // check them all to see if we have a match. |
| 4459 | if (name == mid_name && !is_null_descriptor(mid)) return mid; |
| 4460 | while ((mid > low) && (GetKey(mid - 1)->Hash() == hash)) mid--; |
| 4461 | for (; (mid <= high) && (GetKey(mid)->Hash() == hash); mid++) { |
| 4462 | if (GetKey(mid)->Equals(name) && !is_null_descriptor(mid)) return mid; |
| 4463 | } |
| 4464 | break; |
| 4465 | } |
| 4466 | return kNotFound; |
| 4467 | } |
| 4468 | |
| 4469 | |
| 4470 | int DescriptorArray::LinearSearch(String* name, int len) { |
| 4471 | uint32_t hash = name->Hash(); |
| 4472 | for (int number = 0; number < len; number++) { |
| 4473 | String* entry = GetKey(number); |
| 4474 | if ((entry->Hash() == hash) && |
| 4475 | name->Equals(entry) && |
| 4476 | !is_null_descriptor(number)) { |
| 4477 | return number; |
| 4478 | } |
| 4479 | } |
| 4480 | return kNotFound; |
| 4481 | } |
| 4482 | |
| 4483 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4484 | MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count, |
| 4485 | PretenureFlag pretenure) { |
| 4486 | ASSERT(deopt_entry_count > 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4487 | return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4488 | pretenure); |
| 4489 | } |
| 4490 | |
| 4491 | |
| 4492 | MaybeObject* DeoptimizationOutputData::Allocate(int number_of_deopt_points, |
| 4493 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4494 | if (number_of_deopt_points == 0) return HEAP->empty_fixed_array(); |
| 4495 | return HEAP->AllocateFixedArray(LengthOfFixedArray(number_of_deopt_points), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4496 | pretenure); |
| 4497 | } |
| 4498 | |
| 4499 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4500 | #ifdef DEBUG |
| 4501 | bool DescriptorArray::IsEqualTo(DescriptorArray* other) { |
| 4502 | if (IsEmpty()) return other->IsEmpty(); |
| 4503 | if (other->IsEmpty()) return false; |
| 4504 | if (length() != other->length()) return false; |
| 4505 | for (int i = 0; i < length(); ++i) { |
| 4506 | if (get(i) != other->get(i) && i != kContentArrayIndex) return false; |
| 4507 | } |
| 4508 | return GetContentArray()->IsEqualTo(other->GetContentArray()); |
| 4509 | } |
| 4510 | #endif |
| 4511 | |
| 4512 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4513 | bool String::LooksValid() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4514 | if (!Isolate::Current()->heap()->Contains(this)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4515 | return true; |
| 4516 | } |
| 4517 | |
| 4518 | |
| 4519 | int String::Utf8Length() { |
| 4520 | if (IsAsciiRepresentation()) return length(); |
| 4521 | // Attempt to flatten before accessing the string. It probably |
| 4522 | // doesn't make Utf8Length faster, but it is very likely that |
| 4523 | // the string will be accessed later (for example by WriteUtf8) |
| 4524 | // so it's still a good idea. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4525 | Heap* heap = GetHeap(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4526 | TryFlatten(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4527 | Access<StringInputBuffer> buffer( |
| 4528 | heap->isolate()->objects_string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4529 | buffer->Reset(0, this); |
| 4530 | int result = 0; |
| 4531 | while (buffer->has_more()) |
| 4532 | result += unibrow::Utf8::Length(buffer->GetNext()); |
| 4533 | return result; |
| 4534 | } |
| 4535 | |
| 4536 | |
| 4537 | Vector<const char> String::ToAsciiVector() { |
| 4538 | ASSERT(IsAsciiRepresentation()); |
| 4539 | ASSERT(IsFlat()); |
| 4540 | |
| 4541 | int offset = 0; |
| 4542 | int length = this->length(); |
| 4543 | StringRepresentationTag string_tag = StringShape(this).representation_tag(); |
| 4544 | String* string = this; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4545 | if (string_tag == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4546 | ConsString* cons = ConsString::cast(string); |
| 4547 | ASSERT(cons->second()->length() == 0); |
| 4548 | string = cons->first(); |
| 4549 | string_tag = StringShape(string).representation_tag(); |
| 4550 | } |
| 4551 | if (string_tag == kSeqStringTag) { |
| 4552 | SeqAsciiString* seq = SeqAsciiString::cast(string); |
| 4553 | char* start = seq->GetChars(); |
| 4554 | return Vector<const char>(start + offset, length); |
| 4555 | } |
| 4556 | ASSERT(string_tag == kExternalStringTag); |
| 4557 | ExternalAsciiString* ext = ExternalAsciiString::cast(string); |
| 4558 | const char* start = ext->resource()->data(); |
| 4559 | return Vector<const char>(start + offset, length); |
| 4560 | } |
| 4561 | |
| 4562 | |
| 4563 | Vector<const uc16> String::ToUC16Vector() { |
| 4564 | ASSERT(IsTwoByteRepresentation()); |
| 4565 | ASSERT(IsFlat()); |
| 4566 | |
| 4567 | int offset = 0; |
| 4568 | int length = this->length(); |
| 4569 | StringRepresentationTag string_tag = StringShape(this).representation_tag(); |
| 4570 | String* string = this; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4571 | if (string_tag == kConsStringTag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4572 | ConsString* cons = ConsString::cast(string); |
| 4573 | ASSERT(cons->second()->length() == 0); |
| 4574 | string = cons->first(); |
| 4575 | string_tag = StringShape(string).representation_tag(); |
| 4576 | } |
| 4577 | if (string_tag == kSeqStringTag) { |
| 4578 | SeqTwoByteString* seq = SeqTwoByteString::cast(string); |
| 4579 | return Vector<const uc16>(seq->GetChars() + offset, length); |
| 4580 | } |
| 4581 | ASSERT(string_tag == kExternalStringTag); |
| 4582 | ExternalTwoByteString* ext = ExternalTwoByteString::cast(string); |
| 4583 | const uc16* start = |
| 4584 | reinterpret_cast<const uc16*>(ext->resource()->data()); |
| 4585 | return Vector<const uc16>(start + offset, length); |
| 4586 | } |
| 4587 | |
| 4588 | |
| 4589 | SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 4590 | RobustnessFlag robust_flag, |
| 4591 | int offset, |
| 4592 | int length, |
| 4593 | int* length_return) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4594 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
| 4595 | return SmartPointer<char>(NULL); |
| 4596 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4597 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4598 | |
| 4599 | // Negative length means the to the end of the string. |
| 4600 | if (length < 0) length = kMaxInt - offset; |
| 4601 | |
| 4602 | // Compute the size of the UTF-8 string. Start at the specified offset. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4603 | Access<StringInputBuffer> buffer( |
| 4604 | heap->isolate()->objects_string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4605 | buffer->Reset(offset, this); |
| 4606 | int character_position = offset; |
| 4607 | int utf8_bytes = 0; |
| 4608 | while (buffer->has_more()) { |
| 4609 | uint16_t character = buffer->GetNext(); |
| 4610 | if (character_position < offset + length) { |
| 4611 | utf8_bytes += unibrow::Utf8::Length(character); |
| 4612 | } |
| 4613 | character_position++; |
| 4614 | } |
| 4615 | |
| 4616 | if (length_return) { |
| 4617 | *length_return = utf8_bytes; |
| 4618 | } |
| 4619 | |
| 4620 | char* result = NewArray<char>(utf8_bytes + 1); |
| 4621 | |
| 4622 | // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset. |
| 4623 | buffer->Rewind(); |
| 4624 | buffer->Seek(offset); |
| 4625 | character_position = offset; |
| 4626 | int utf8_byte_position = 0; |
| 4627 | while (buffer->has_more()) { |
| 4628 | uint16_t character = buffer->GetNext(); |
| 4629 | if (character_position < offset + length) { |
| 4630 | if (allow_nulls == DISALLOW_NULLS && character == 0) { |
| 4631 | character = ' '; |
| 4632 | } |
| 4633 | utf8_byte_position += |
| 4634 | unibrow::Utf8::Encode(result + utf8_byte_position, character); |
| 4635 | } |
| 4636 | character_position++; |
| 4637 | } |
| 4638 | result[utf8_byte_position] = 0; |
| 4639 | return SmartPointer<char>(result); |
| 4640 | } |
| 4641 | |
| 4642 | |
| 4643 | SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls, |
| 4644 | RobustnessFlag robust_flag, |
| 4645 | int* length_return) { |
| 4646 | return ToCString(allow_nulls, robust_flag, 0, -1, length_return); |
| 4647 | } |
| 4648 | |
| 4649 | |
| 4650 | const uc16* String::GetTwoByteData() { |
| 4651 | return GetTwoByteData(0); |
| 4652 | } |
| 4653 | |
| 4654 | |
| 4655 | const uc16* String::GetTwoByteData(unsigned start) { |
| 4656 | ASSERT(!IsAsciiRepresentation()); |
| 4657 | switch (StringShape(this).representation_tag()) { |
| 4658 | case kSeqStringTag: |
| 4659 | return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start); |
| 4660 | case kExternalStringTag: |
| 4661 | return ExternalTwoByteString::cast(this)-> |
| 4662 | ExternalTwoByteStringGetData(start); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4663 | case kConsStringTag: |
| 4664 | UNREACHABLE(); |
| 4665 | return NULL; |
| 4666 | } |
| 4667 | UNREACHABLE(); |
| 4668 | return NULL; |
| 4669 | } |
| 4670 | |
| 4671 | |
| 4672 | SmartPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4673 | if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) { |
| 4674 | return SmartPointer<uc16>(); |
| 4675 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4676 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4677 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4678 | Access<StringInputBuffer> buffer( |
| 4679 | heap->isolate()->objects_string_input_buffer()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4680 | buffer->Reset(this); |
| 4681 | |
| 4682 | uc16* result = NewArray<uc16>(length() + 1); |
| 4683 | |
| 4684 | int i = 0; |
| 4685 | while (buffer->has_more()) { |
| 4686 | uint16_t character = buffer->GetNext(); |
| 4687 | result[i++] = character; |
| 4688 | } |
| 4689 | result[i] = 0; |
| 4690 | return SmartPointer<uc16>(result); |
| 4691 | } |
| 4692 | |
| 4693 | |
| 4694 | const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) { |
| 4695 | return reinterpret_cast<uc16*>( |
| 4696 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start; |
| 4697 | } |
| 4698 | |
| 4699 | |
| 4700 | void SeqTwoByteString::SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 4701 | unsigned* offset_ptr, |
| 4702 | unsigned max_chars) { |
| 4703 | unsigned chars_read = 0; |
| 4704 | unsigned offset = *offset_ptr; |
| 4705 | while (chars_read < max_chars) { |
| 4706 | uint16_t c = *reinterpret_cast<uint16_t*>( |
| 4707 | reinterpret_cast<char*>(this) - |
| 4708 | kHeapObjectTag + kHeaderSize + offset * kShortSize); |
| 4709 | if (c <= kMaxAsciiCharCode) { |
| 4710 | // Fast case for ASCII characters. Cursor is an input output argument. |
| 4711 | if (!unibrow::CharacterStream::EncodeAsciiCharacter(c, |
| 4712 | rbb->util_buffer, |
| 4713 | rbb->capacity, |
| 4714 | rbb->cursor)) { |
| 4715 | break; |
| 4716 | } |
| 4717 | } else { |
| 4718 | if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c, |
| 4719 | rbb->util_buffer, |
| 4720 | rbb->capacity, |
| 4721 | rbb->cursor)) { |
| 4722 | break; |
| 4723 | } |
| 4724 | } |
| 4725 | offset++; |
| 4726 | chars_read++; |
| 4727 | } |
| 4728 | *offset_ptr = offset; |
| 4729 | rbb->remaining += chars_read; |
| 4730 | } |
| 4731 | |
| 4732 | |
| 4733 | const unibrow::byte* SeqAsciiString::SeqAsciiStringReadBlock( |
| 4734 | unsigned* remaining, |
| 4735 | unsigned* offset_ptr, |
| 4736 | unsigned max_chars) { |
| 4737 | const unibrow::byte* b = reinterpret_cast<unibrow::byte*>(this) - |
| 4738 | kHeapObjectTag + kHeaderSize + *offset_ptr * kCharSize; |
| 4739 | *remaining = max_chars; |
| 4740 | *offset_ptr += max_chars; |
| 4741 | return b; |
| 4742 | } |
| 4743 | |
| 4744 | |
| 4745 | // This will iterate unless the block of string data spans two 'halves' of |
| 4746 | // a ConsString, in which case it will recurse. Since the block of string |
| 4747 | // data to be read has a maximum size this limits the maximum recursion |
| 4748 | // depth to something sane. Since C++ does not have tail call recursion |
| 4749 | // elimination, the iteration must be explicit. Since this is not an |
| 4750 | // -IntoBuffer method it can delegate to one of the efficient |
| 4751 | // *AsciiStringReadBlock routines. |
| 4752 | const unibrow::byte* ConsString::ConsStringReadBlock(ReadBlockBuffer* rbb, |
| 4753 | unsigned* offset_ptr, |
| 4754 | unsigned max_chars) { |
| 4755 | ConsString* current = this; |
| 4756 | unsigned offset = *offset_ptr; |
| 4757 | int offset_correction = 0; |
| 4758 | |
| 4759 | while (true) { |
| 4760 | String* left = current->first(); |
| 4761 | unsigned left_length = (unsigned)left->length(); |
| 4762 | if (left_length > offset && |
| 4763 | (max_chars <= left_length - offset || |
| 4764 | (rbb->capacity <= left_length - offset && |
| 4765 | (max_chars = left_length - offset, true)))) { // comma operator! |
| 4766 | // Left hand side only - iterate unless we have reached the bottom of |
| 4767 | // the cons tree. The assignment on the left of the comma operator is |
| 4768 | // in order to make use of the fact that the -IntoBuffer routines can |
| 4769 | // produce at most 'capacity' characters. This enables us to postpone |
| 4770 | // the point where we switch to the -IntoBuffer routines (below) in order |
| 4771 | // to maximize the chances of delegating a big chunk of work to the |
| 4772 | // efficient *AsciiStringReadBlock routines. |
| 4773 | if (StringShape(left).IsCons()) { |
| 4774 | current = ConsString::cast(left); |
| 4775 | continue; |
| 4776 | } else { |
| 4777 | const unibrow::byte* answer = |
| 4778 | String::ReadBlock(left, rbb, &offset, max_chars); |
| 4779 | *offset_ptr = offset + offset_correction; |
| 4780 | return answer; |
| 4781 | } |
| 4782 | } else if (left_length <= offset) { |
| 4783 | // Right hand side only - iterate unless we have reached the bottom of |
| 4784 | // the cons tree. |
| 4785 | String* right = current->second(); |
| 4786 | offset -= left_length; |
| 4787 | offset_correction += left_length; |
| 4788 | if (StringShape(right).IsCons()) { |
| 4789 | current = ConsString::cast(right); |
| 4790 | continue; |
| 4791 | } else { |
| 4792 | const unibrow::byte* answer = |
| 4793 | String::ReadBlock(right, rbb, &offset, max_chars); |
| 4794 | *offset_ptr = offset + offset_correction; |
| 4795 | return answer; |
| 4796 | } |
| 4797 | } else { |
| 4798 | // The block to be read spans two sides of the ConsString, so we call the |
| 4799 | // -IntoBuffer version, which will recurse. The -IntoBuffer methods |
| 4800 | // are able to assemble data from several part strings because they use |
| 4801 | // the util_buffer to store their data and never return direct pointers |
| 4802 | // to their storage. We don't try to read more than the buffer capacity |
| 4803 | // here or we can get too much recursion. |
| 4804 | ASSERT(rbb->remaining == 0); |
| 4805 | ASSERT(rbb->cursor == 0); |
| 4806 | current->ConsStringReadBlockIntoBuffer( |
| 4807 | rbb, |
| 4808 | &offset, |
| 4809 | max_chars > rbb->capacity ? rbb->capacity : max_chars); |
| 4810 | *offset_ptr = offset + offset_correction; |
| 4811 | return rbb->util_buffer; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | |
| 4816 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4817 | uint16_t ExternalAsciiString::ExternalAsciiStringGet(int index) { |
| 4818 | ASSERT(index >= 0 && index < length()); |
| 4819 | return resource()->data()[index]; |
| 4820 | } |
| 4821 | |
| 4822 | |
| 4823 | const unibrow::byte* ExternalAsciiString::ExternalAsciiStringReadBlock( |
| 4824 | unsigned* remaining, |
| 4825 | unsigned* offset_ptr, |
| 4826 | unsigned max_chars) { |
| 4827 | // Cast const char* to unibrow::byte* (signedness difference). |
| 4828 | const unibrow::byte* b = |
| 4829 | reinterpret_cast<const unibrow::byte*>(resource()->data()) + *offset_ptr; |
| 4830 | *remaining = max_chars; |
| 4831 | *offset_ptr += max_chars; |
| 4832 | return b; |
| 4833 | } |
| 4834 | |
| 4835 | |
| 4836 | const uc16* ExternalTwoByteString::ExternalTwoByteStringGetData( |
| 4837 | unsigned start) { |
| 4838 | return resource()->data() + start; |
| 4839 | } |
| 4840 | |
| 4841 | |
| 4842 | uint16_t ExternalTwoByteString::ExternalTwoByteStringGet(int index) { |
| 4843 | ASSERT(index >= 0 && index < length()); |
| 4844 | return resource()->data()[index]; |
| 4845 | } |
| 4846 | |
| 4847 | |
| 4848 | void ExternalTwoByteString::ExternalTwoByteStringReadBlockIntoBuffer( |
| 4849 | ReadBlockBuffer* rbb, |
| 4850 | unsigned* offset_ptr, |
| 4851 | unsigned max_chars) { |
| 4852 | unsigned chars_read = 0; |
| 4853 | unsigned offset = *offset_ptr; |
| 4854 | const uint16_t* data = resource()->data(); |
| 4855 | while (chars_read < max_chars) { |
| 4856 | uint16_t c = data[offset]; |
| 4857 | if (c <= kMaxAsciiCharCode) { |
| 4858 | // Fast case for ASCII characters. Cursor is an input output argument. |
| 4859 | if (!unibrow::CharacterStream::EncodeAsciiCharacter(c, |
| 4860 | rbb->util_buffer, |
| 4861 | rbb->capacity, |
| 4862 | rbb->cursor)) |
| 4863 | break; |
| 4864 | } else { |
| 4865 | if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c, |
| 4866 | rbb->util_buffer, |
| 4867 | rbb->capacity, |
| 4868 | rbb->cursor)) |
| 4869 | break; |
| 4870 | } |
| 4871 | offset++; |
| 4872 | chars_read++; |
| 4873 | } |
| 4874 | *offset_ptr = offset; |
| 4875 | rbb->remaining += chars_read; |
| 4876 | } |
| 4877 | |
| 4878 | |
| 4879 | void SeqAsciiString::SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 4880 | unsigned* offset_ptr, |
| 4881 | unsigned max_chars) { |
| 4882 | unsigned capacity = rbb->capacity - rbb->cursor; |
| 4883 | if (max_chars > capacity) max_chars = capacity; |
| 4884 | memcpy(rbb->util_buffer + rbb->cursor, |
| 4885 | reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize + |
| 4886 | *offset_ptr * kCharSize, |
| 4887 | max_chars); |
| 4888 | rbb->remaining += max_chars; |
| 4889 | *offset_ptr += max_chars; |
| 4890 | rbb->cursor += max_chars; |
| 4891 | } |
| 4892 | |
| 4893 | |
| 4894 | void ExternalAsciiString::ExternalAsciiStringReadBlockIntoBuffer( |
| 4895 | ReadBlockBuffer* rbb, |
| 4896 | unsigned* offset_ptr, |
| 4897 | unsigned max_chars) { |
| 4898 | unsigned capacity = rbb->capacity - rbb->cursor; |
| 4899 | if (max_chars > capacity) max_chars = capacity; |
| 4900 | memcpy(rbb->util_buffer + rbb->cursor, |
| 4901 | resource()->data() + *offset_ptr, |
| 4902 | max_chars); |
| 4903 | rbb->remaining += max_chars; |
| 4904 | *offset_ptr += max_chars; |
| 4905 | rbb->cursor += max_chars; |
| 4906 | } |
| 4907 | |
| 4908 | |
| 4909 | // This method determines the type of string involved and then copies |
| 4910 | // a whole chunk of characters into a buffer, or returns a pointer to a buffer |
| 4911 | // where they can be found. The pointer is not necessarily valid across a GC |
| 4912 | // (see AsciiStringReadBlock). |
| 4913 | const unibrow::byte* String::ReadBlock(String* input, |
| 4914 | ReadBlockBuffer* rbb, |
| 4915 | unsigned* offset_ptr, |
| 4916 | unsigned max_chars) { |
| 4917 | ASSERT(*offset_ptr <= static_cast<unsigned>(input->length())); |
| 4918 | if (max_chars == 0) { |
| 4919 | rbb->remaining = 0; |
| 4920 | return NULL; |
| 4921 | } |
| 4922 | switch (StringShape(input).representation_tag()) { |
| 4923 | case kSeqStringTag: |
| 4924 | if (input->IsAsciiRepresentation()) { |
| 4925 | SeqAsciiString* str = SeqAsciiString::cast(input); |
| 4926 | return str->SeqAsciiStringReadBlock(&rbb->remaining, |
| 4927 | offset_ptr, |
| 4928 | max_chars); |
| 4929 | } else { |
| 4930 | SeqTwoByteString* str = SeqTwoByteString::cast(input); |
| 4931 | str->SeqTwoByteStringReadBlockIntoBuffer(rbb, |
| 4932 | offset_ptr, |
| 4933 | max_chars); |
| 4934 | return rbb->util_buffer; |
| 4935 | } |
| 4936 | case kConsStringTag: |
| 4937 | return ConsString::cast(input)->ConsStringReadBlock(rbb, |
| 4938 | offset_ptr, |
| 4939 | max_chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4940 | case kExternalStringTag: |
| 4941 | if (input->IsAsciiRepresentation()) { |
| 4942 | return ExternalAsciiString::cast(input)->ExternalAsciiStringReadBlock( |
| 4943 | &rbb->remaining, |
| 4944 | offset_ptr, |
| 4945 | max_chars); |
| 4946 | } else { |
| 4947 | ExternalTwoByteString::cast(input)-> |
| 4948 | ExternalTwoByteStringReadBlockIntoBuffer(rbb, |
| 4949 | offset_ptr, |
| 4950 | max_chars); |
| 4951 | return rbb->util_buffer; |
| 4952 | } |
| 4953 | default: |
| 4954 | break; |
| 4955 | } |
| 4956 | |
| 4957 | UNREACHABLE(); |
| 4958 | return 0; |
| 4959 | } |
| 4960 | |
| 4961 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4962 | void Relocatable::PostGarbageCollectionProcessing() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4963 | Isolate* isolate = Isolate::Current(); |
| 4964 | Relocatable* current = isolate->relocatable_top(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4965 | while (current != NULL) { |
| 4966 | current->PostGarbageCollection(); |
| 4967 | current = current->prev_; |
| 4968 | } |
| 4969 | } |
| 4970 | |
| 4971 | |
| 4972 | // Reserve space for statics needing saving and restoring. |
| 4973 | int Relocatable::ArchiveSpacePerThread() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4974 | return sizeof(Isolate::Current()->relocatable_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | |
| 4978 | // Archive statics that are thread local. |
| 4979 | char* Relocatable::ArchiveState(char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4980 | Isolate* isolate = Isolate::Current(); |
| 4981 | *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top(); |
| 4982 | isolate->set_relocatable_top(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4983 | return to + ArchiveSpacePerThread(); |
| 4984 | } |
| 4985 | |
| 4986 | |
| 4987 | // Restore statics that are thread local. |
| 4988 | char* Relocatable::RestoreState(char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4989 | Isolate* isolate = Isolate::Current(); |
| 4990 | isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4991 | return from + ArchiveSpacePerThread(); |
| 4992 | } |
| 4993 | |
| 4994 | |
| 4995 | char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 4996 | Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage); |
| 4997 | Iterate(v, top); |
| 4998 | return thread_storage + ArchiveSpacePerThread(); |
| 4999 | } |
| 5000 | |
| 5001 | |
| 5002 | void Relocatable::Iterate(ObjectVisitor* v) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5003 | Isolate* isolate = Isolate::Current(); |
| 5004 | Iterate(v, isolate->relocatable_top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | |
| 5008 | void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) { |
| 5009 | Relocatable* current = top; |
| 5010 | while (current != NULL) { |
| 5011 | current->IterateInstance(v); |
| 5012 | current = current->prev_; |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5017 | FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str) |
| 5018 | : Relocatable(isolate), |
| 5019 | str_(str.location()), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5020 | length_(str->length()) { |
| 5021 | PostGarbageCollection(); |
| 5022 | } |
| 5023 | |
| 5024 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5025 | FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input) |
| 5026 | : Relocatable(isolate), |
| 5027 | str_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5028 | is_ascii_(true), |
| 5029 | length_(input.length()), |
| 5030 | start_(input.start()) { } |
| 5031 | |
| 5032 | |
| 5033 | void FlatStringReader::PostGarbageCollection() { |
| 5034 | if (str_ == NULL) return; |
| 5035 | Handle<String> str(str_); |
| 5036 | ASSERT(str->IsFlat()); |
| 5037 | is_ascii_ = str->IsAsciiRepresentation(); |
| 5038 | if (is_ascii_) { |
| 5039 | start_ = str->ToAsciiVector().start(); |
| 5040 | } else { |
| 5041 | start_ = str->ToUC16Vector().start(); |
| 5042 | } |
| 5043 | } |
| 5044 | |
| 5045 | |
| 5046 | void StringInputBuffer::Seek(unsigned pos) { |
| 5047 | Reset(pos, input_); |
| 5048 | } |
| 5049 | |
| 5050 | |
| 5051 | void SafeStringInputBuffer::Seek(unsigned pos) { |
| 5052 | Reset(pos, input_); |
| 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | // This method determines the type of string involved and then copies |
| 5057 | // a whole chunk of characters into a buffer. It can be used with strings |
| 5058 | // that have been glued together to form a ConsString and which must cooperate |
| 5059 | // to fill up a buffer. |
| 5060 | void String::ReadBlockIntoBuffer(String* input, |
| 5061 | ReadBlockBuffer* rbb, |
| 5062 | unsigned* offset_ptr, |
| 5063 | unsigned max_chars) { |
| 5064 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 5065 | if (max_chars == 0) return; |
| 5066 | |
| 5067 | switch (StringShape(input).representation_tag()) { |
| 5068 | case kSeqStringTag: |
| 5069 | if (input->IsAsciiRepresentation()) { |
| 5070 | SeqAsciiString::cast(input)->SeqAsciiStringReadBlockIntoBuffer(rbb, |
| 5071 | offset_ptr, |
| 5072 | max_chars); |
| 5073 | return; |
| 5074 | } else { |
| 5075 | SeqTwoByteString::cast(input)->SeqTwoByteStringReadBlockIntoBuffer(rbb, |
| 5076 | offset_ptr, |
| 5077 | max_chars); |
| 5078 | return; |
| 5079 | } |
| 5080 | case kConsStringTag: |
| 5081 | ConsString::cast(input)->ConsStringReadBlockIntoBuffer(rbb, |
| 5082 | offset_ptr, |
| 5083 | max_chars); |
| 5084 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5085 | case kExternalStringTag: |
| 5086 | if (input->IsAsciiRepresentation()) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5087 | ExternalAsciiString::cast(input)-> |
| 5088 | ExternalAsciiStringReadBlockIntoBuffer(rbb, offset_ptr, max_chars); |
| 5089 | } else { |
| 5090 | ExternalTwoByteString::cast(input)-> |
| 5091 | ExternalTwoByteStringReadBlockIntoBuffer(rbb, |
| 5092 | offset_ptr, |
| 5093 | max_chars); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5094 | } |
| 5095 | return; |
| 5096 | default: |
| 5097 | break; |
| 5098 | } |
| 5099 | |
| 5100 | UNREACHABLE(); |
| 5101 | return; |
| 5102 | } |
| 5103 | |
| 5104 | |
| 5105 | const unibrow::byte* String::ReadBlock(String* input, |
| 5106 | unibrow::byte* util_buffer, |
| 5107 | unsigned capacity, |
| 5108 | unsigned* remaining, |
| 5109 | unsigned* offset_ptr) { |
| 5110 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 5111 | unsigned chars = input->length() - *offset_ptr; |
| 5112 | ReadBlockBuffer rbb(util_buffer, 0, capacity, 0); |
| 5113 | const unibrow::byte* answer = ReadBlock(input, &rbb, offset_ptr, chars); |
| 5114 | ASSERT(rbb.remaining <= static_cast<unsigned>(input->length())); |
| 5115 | *remaining = rbb.remaining; |
| 5116 | return answer; |
| 5117 | } |
| 5118 | |
| 5119 | |
| 5120 | const unibrow::byte* String::ReadBlock(String** raw_input, |
| 5121 | unibrow::byte* util_buffer, |
| 5122 | unsigned capacity, |
| 5123 | unsigned* remaining, |
| 5124 | unsigned* offset_ptr) { |
| 5125 | Handle<String> input(raw_input); |
| 5126 | ASSERT(*offset_ptr <= (unsigned)input->length()); |
| 5127 | unsigned chars = input->length() - *offset_ptr; |
| 5128 | if (chars > capacity) chars = capacity; |
| 5129 | ReadBlockBuffer rbb(util_buffer, 0, capacity, 0); |
| 5130 | ReadBlockIntoBuffer(*input, &rbb, offset_ptr, chars); |
| 5131 | ASSERT(rbb.remaining <= static_cast<unsigned>(input->length())); |
| 5132 | *remaining = rbb.remaining; |
| 5133 | return rbb.util_buffer; |
| 5134 | } |
| 5135 | |
| 5136 | |
| 5137 | // This will iterate unless the block of string data spans two 'halves' of |
| 5138 | // a ConsString, in which case it will recurse. Since the block of string |
| 5139 | // data to be read has a maximum size this limits the maximum recursion |
| 5140 | // depth to something sane. Since C++ does not have tail call recursion |
| 5141 | // elimination, the iteration must be explicit. |
| 5142 | void ConsString::ConsStringReadBlockIntoBuffer(ReadBlockBuffer* rbb, |
| 5143 | unsigned* offset_ptr, |
| 5144 | unsigned max_chars) { |
| 5145 | ConsString* current = this; |
| 5146 | unsigned offset = *offset_ptr; |
| 5147 | int offset_correction = 0; |
| 5148 | |
| 5149 | while (true) { |
| 5150 | String* left = current->first(); |
| 5151 | unsigned left_length = (unsigned)left->length(); |
| 5152 | if (left_length > offset && |
| 5153 | max_chars <= left_length - offset) { |
| 5154 | // Left hand side only - iterate unless we have reached the bottom of |
| 5155 | // the cons tree. |
| 5156 | if (StringShape(left).IsCons()) { |
| 5157 | current = ConsString::cast(left); |
| 5158 | continue; |
| 5159 | } else { |
| 5160 | String::ReadBlockIntoBuffer(left, rbb, &offset, max_chars); |
| 5161 | *offset_ptr = offset + offset_correction; |
| 5162 | return; |
| 5163 | } |
| 5164 | } else if (left_length <= offset) { |
| 5165 | // Right hand side only - iterate unless we have reached the bottom of |
| 5166 | // the cons tree. |
| 5167 | offset -= left_length; |
| 5168 | offset_correction += left_length; |
| 5169 | String* right = current->second(); |
| 5170 | if (StringShape(right).IsCons()) { |
| 5171 | current = ConsString::cast(right); |
| 5172 | continue; |
| 5173 | } else { |
| 5174 | String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars); |
| 5175 | *offset_ptr = offset + offset_correction; |
| 5176 | return; |
| 5177 | } |
| 5178 | } else { |
| 5179 | // The block to be read spans two sides of the ConsString, so we recurse. |
| 5180 | // First recurse on the left. |
| 5181 | max_chars -= left_length - offset; |
| 5182 | String::ReadBlockIntoBuffer(left, rbb, &offset, left_length - offset); |
| 5183 | // We may have reached the max or there may not have been enough space |
| 5184 | // in the buffer for the characters in the left hand side. |
| 5185 | if (offset == left_length) { |
| 5186 | // Recurse on the right. |
| 5187 | String* right = String::cast(current->second()); |
| 5188 | offset -= left_length; |
| 5189 | offset_correction += left_length; |
| 5190 | String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars); |
| 5191 | } |
| 5192 | *offset_ptr = offset + offset_correction; |
| 5193 | return; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5199 | uint16_t ConsString::ConsStringGet(int index) { |
| 5200 | ASSERT(index >= 0 && index < this->length()); |
| 5201 | |
| 5202 | // Check for a flattened cons string |
| 5203 | if (second()->length() == 0) { |
| 5204 | String* left = first(); |
| 5205 | return left->Get(index); |
| 5206 | } |
| 5207 | |
| 5208 | String* string = String::cast(this); |
| 5209 | |
| 5210 | while (true) { |
| 5211 | if (StringShape(string).IsCons()) { |
| 5212 | ConsString* cons_string = ConsString::cast(string); |
| 5213 | String* left = cons_string->first(); |
| 5214 | if (left->length() > index) { |
| 5215 | string = left; |
| 5216 | } else { |
| 5217 | index -= left->length(); |
| 5218 | string = cons_string->second(); |
| 5219 | } |
| 5220 | } else { |
| 5221 | return string->Get(index); |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | UNREACHABLE(); |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
| 5229 | |
| 5230 | template <typename sinkchar> |
| 5231 | void String::WriteToFlat(String* src, |
| 5232 | sinkchar* sink, |
| 5233 | int f, |
| 5234 | int t) { |
| 5235 | String* source = src; |
| 5236 | int from = f; |
| 5237 | int to = t; |
| 5238 | while (true) { |
| 5239 | ASSERT(0 <= from && from <= to && to <= source->length()); |
| 5240 | switch (StringShape(source).full_representation_tag()) { |
| 5241 | case kAsciiStringTag | kExternalStringTag: { |
| 5242 | CopyChars(sink, |
| 5243 | ExternalAsciiString::cast(source)->resource()->data() + from, |
| 5244 | to - from); |
| 5245 | return; |
| 5246 | } |
| 5247 | case kTwoByteStringTag | kExternalStringTag: { |
| 5248 | const uc16* data = |
| 5249 | ExternalTwoByteString::cast(source)->resource()->data(); |
| 5250 | CopyChars(sink, |
| 5251 | data + from, |
| 5252 | to - from); |
| 5253 | return; |
| 5254 | } |
| 5255 | case kAsciiStringTag | kSeqStringTag: { |
| 5256 | CopyChars(sink, |
| 5257 | SeqAsciiString::cast(source)->GetChars() + from, |
| 5258 | to - from); |
| 5259 | return; |
| 5260 | } |
| 5261 | case kTwoByteStringTag | kSeqStringTag: { |
| 5262 | CopyChars(sink, |
| 5263 | SeqTwoByteString::cast(source)->GetChars() + from, |
| 5264 | to - from); |
| 5265 | return; |
| 5266 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5267 | case kAsciiStringTag | kConsStringTag: |
| 5268 | case kTwoByteStringTag | kConsStringTag: { |
| 5269 | ConsString* cons_string = ConsString::cast(source); |
| 5270 | String* first = cons_string->first(); |
| 5271 | int boundary = first->length(); |
| 5272 | if (to - boundary >= boundary - from) { |
| 5273 | // Right hand side is longer. Recurse over left. |
| 5274 | if (from < boundary) { |
| 5275 | WriteToFlat(first, sink, from, boundary); |
| 5276 | sink += boundary - from; |
| 5277 | from = 0; |
| 5278 | } else { |
| 5279 | from -= boundary; |
| 5280 | } |
| 5281 | to -= boundary; |
| 5282 | source = cons_string->second(); |
| 5283 | } else { |
| 5284 | // Left hand side is longer. Recurse over right. |
| 5285 | if (to > boundary) { |
| 5286 | String* second = cons_string->second(); |
| 5287 | WriteToFlat(second, |
| 5288 | sink + boundary - from, |
| 5289 | 0, |
| 5290 | to - boundary); |
| 5291 | to = boundary; |
| 5292 | } |
| 5293 | source = first; |
| 5294 | } |
| 5295 | break; |
| 5296 | } |
| 5297 | } |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5302 | template <typename IteratorA, typename IteratorB> |
| 5303 | static inline bool CompareStringContents(IteratorA* ia, IteratorB* ib) { |
| 5304 | // General slow case check. We know that the ia and ib iterators |
| 5305 | // have the same length. |
| 5306 | while (ia->has_more()) { |
| 5307 | uc32 ca = ia->GetNext(); |
| 5308 | uc32 cb = ib->GetNext(); |
| 5309 | if (ca != cb) |
| 5310 | return false; |
| 5311 | } |
| 5312 | return true; |
| 5313 | } |
| 5314 | |
| 5315 | |
| 5316 | // Compares the contents of two strings by reading and comparing |
| 5317 | // int-sized blocks of characters. |
| 5318 | template <typename Char> |
| 5319 | static inline bool CompareRawStringContents(Vector<Char> a, Vector<Char> b) { |
| 5320 | int length = a.length(); |
| 5321 | ASSERT_EQ(length, b.length()); |
| 5322 | const Char* pa = a.start(); |
| 5323 | const Char* pb = b.start(); |
| 5324 | int i = 0; |
| 5325 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
| 5326 | // If this architecture isn't comfortable reading unaligned ints |
| 5327 | // then we have to check that the strings are aligned before |
| 5328 | // comparing them blockwise. |
| 5329 | const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT |
| 5330 | uint32_t pa_addr = reinterpret_cast<uint32_t>(pa); |
| 5331 | uint32_t pb_addr = reinterpret_cast<uint32_t>(pb); |
| 5332 | if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) { |
| 5333 | #endif |
| 5334 | const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT |
| 5335 | int endpoint = length - kStepSize; |
| 5336 | // Compare blocks until we reach near the end of the string. |
| 5337 | for (; i <= endpoint; i += kStepSize) { |
| 5338 | uint32_t wa = *reinterpret_cast<const uint32_t*>(pa + i); |
| 5339 | uint32_t wb = *reinterpret_cast<const uint32_t*>(pb + i); |
| 5340 | if (wa != wb) { |
| 5341 | return false; |
| 5342 | } |
| 5343 | } |
| 5344 | #ifndef V8_HOST_CAN_READ_UNALIGNED |
| 5345 | } |
| 5346 | #endif |
| 5347 | // Compare the remaining characters that didn't fit into a block. |
| 5348 | for (; i < length; i++) { |
| 5349 | if (a[i] != b[i]) { |
| 5350 | return false; |
| 5351 | } |
| 5352 | } |
| 5353 | return true; |
| 5354 | } |
| 5355 | |
| 5356 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5357 | template <typename IteratorA> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5358 | static inline bool CompareStringContentsPartial(Isolate* isolate, |
| 5359 | IteratorA* ia, |
| 5360 | String* b) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5361 | if (b->IsFlat()) { |
| 5362 | if (b->IsAsciiRepresentation()) { |
| 5363 | VectorIterator<char> ib(b->ToAsciiVector()); |
| 5364 | return CompareStringContents(ia, &ib); |
| 5365 | } else { |
| 5366 | VectorIterator<uc16> ib(b->ToUC16Vector()); |
| 5367 | return CompareStringContents(ia, &ib); |
| 5368 | } |
| 5369 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5370 | isolate->objects_string_compare_buffer_b()->Reset(0, b); |
| 5371 | return CompareStringContents(ia, |
| 5372 | isolate->objects_string_compare_buffer_b()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5377 | bool String::SlowEquals(String* other) { |
| 5378 | // Fast check: negative check with lengths. |
| 5379 | int len = length(); |
| 5380 | if (len != other->length()) return false; |
| 5381 | if (len == 0) return true; |
| 5382 | |
| 5383 | // Fast check: if hash code is computed for both strings |
| 5384 | // a fast negative check can be performed. |
| 5385 | if (HasHashCode() && other->HasHashCode()) { |
| 5386 | if (Hash() != other->Hash()) return false; |
| 5387 | } |
| 5388 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5389 | // We know the strings are both non-empty. Compare the first chars |
| 5390 | // before we try to flatten the strings. |
| 5391 | if (this->Get(0) != other->Get(0)) return false; |
| 5392 | |
| 5393 | String* lhs = this->TryFlattenGetString(); |
| 5394 | String* rhs = other->TryFlattenGetString(); |
| 5395 | |
| 5396 | if (StringShape(lhs).IsSequentialAscii() && |
| 5397 | StringShape(rhs).IsSequentialAscii()) { |
| 5398 | const char* str1 = SeqAsciiString::cast(lhs)->GetChars(); |
| 5399 | const char* str2 = SeqAsciiString::cast(rhs)->GetChars(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5400 | return CompareRawStringContents(Vector<const char>(str1, len), |
| 5401 | Vector<const char>(str2, len)); |
| 5402 | } |
| 5403 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5404 | Isolate* isolate = GetIsolate(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5405 | if (lhs->IsFlat()) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5406 | if (lhs->IsAsciiRepresentation()) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5407 | Vector<const char> vec1 = lhs->ToAsciiVector(); |
| 5408 | if (rhs->IsFlat()) { |
| 5409 | if (rhs->IsAsciiRepresentation()) { |
| 5410 | Vector<const char> vec2 = rhs->ToAsciiVector(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5411 | return CompareRawStringContents(vec1, vec2); |
| 5412 | } else { |
| 5413 | VectorIterator<char> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5414 | VectorIterator<uc16> ib(rhs->ToUC16Vector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5415 | return CompareStringContents(&buf1, &ib); |
| 5416 | } |
| 5417 | } else { |
| 5418 | VectorIterator<char> buf1(vec1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5419 | isolate->objects_string_compare_buffer_b()->Reset(0, rhs); |
| 5420 | return CompareStringContents(&buf1, |
| 5421 | isolate->objects_string_compare_buffer_b()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5422 | } |
| 5423 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5424 | Vector<const uc16> vec1 = lhs->ToUC16Vector(); |
| 5425 | if (rhs->IsFlat()) { |
| 5426 | if (rhs->IsAsciiRepresentation()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5427 | VectorIterator<uc16> buf1(vec1); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5428 | VectorIterator<char> ib(rhs->ToAsciiVector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5429 | return CompareStringContents(&buf1, &ib); |
| 5430 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5431 | Vector<const uc16> vec2(rhs->ToUC16Vector()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5432 | return CompareRawStringContents(vec1, vec2); |
| 5433 | } |
| 5434 | } else { |
| 5435 | VectorIterator<uc16> buf1(vec1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5436 | isolate->objects_string_compare_buffer_b()->Reset(0, rhs); |
| 5437 | return CompareStringContents(&buf1, |
| 5438 | isolate->objects_string_compare_buffer_b()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5442 | isolate->objects_string_compare_buffer_a()->Reset(0, lhs); |
| 5443 | return CompareStringContentsPartial(isolate, |
| 5444 | isolate->objects_string_compare_buffer_a(), rhs); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | |
| 5449 | bool String::MarkAsUndetectable() { |
| 5450 | if (StringShape(this).IsSymbol()) return false; |
| 5451 | |
| 5452 | Map* map = this->map(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5453 | Heap* heap = map->heap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5454 | if (map == heap->string_map()) { |
| 5455 | this->set_map(heap->undetectable_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5456 | return true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5457 | } else if (map == heap->ascii_string_map()) { |
| 5458 | this->set_map(heap->undetectable_ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5459 | return true; |
| 5460 | } |
| 5461 | // Rest cannot be marked as undetectable |
| 5462 | return false; |
| 5463 | } |
| 5464 | |
| 5465 | |
| 5466 | bool String::IsEqualTo(Vector<const char> str) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5467 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5468 | int slen = length(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5469 | Access<UnicodeCache::Utf8Decoder> |
| 5470 | decoder(isolate->unicode_cache()->utf8_decoder()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5471 | decoder->Reset(str.start(), str.length()); |
| 5472 | int i; |
| 5473 | for (i = 0; i < slen && decoder->has_more(); i++) { |
| 5474 | uc32 r = decoder->GetNext(); |
| 5475 | if (Get(i) != r) return false; |
| 5476 | } |
| 5477 | return i == slen && !decoder->has_more(); |
| 5478 | } |
| 5479 | |
| 5480 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5481 | bool String::IsAsciiEqualTo(Vector<const char> str) { |
| 5482 | int slen = length(); |
| 5483 | if (str.length() != slen) return false; |
| 5484 | for (int i = 0; i < slen; i++) { |
| 5485 | if (Get(i) != static_cast<uint16_t>(str[i])) return false; |
| 5486 | } |
| 5487 | return true; |
| 5488 | } |
| 5489 | |
| 5490 | |
| 5491 | bool String::IsTwoByteEqualTo(Vector<const uc16> str) { |
| 5492 | int slen = length(); |
| 5493 | if (str.length() != slen) return false; |
| 5494 | for (int i = 0; i < slen; i++) { |
| 5495 | if (Get(i) != str[i]) return false; |
| 5496 | } |
| 5497 | return true; |
| 5498 | } |
| 5499 | |
| 5500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5501 | uint32_t String::ComputeAndSetHash() { |
| 5502 | // Should only be called if hash code has not yet been computed. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5503 | ASSERT(!HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5504 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5505 | const int len = length(); |
| 5506 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5507 | // Compute the hash code. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5508 | uint32_t field = 0; |
| 5509 | if (StringShape(this).IsSequentialAscii()) { |
| 5510 | field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(), len); |
| 5511 | } else if (StringShape(this).IsSequentialTwoByte()) { |
| 5512 | field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(), len); |
| 5513 | } else { |
| 5514 | StringInputBuffer buffer(this); |
| 5515 | field = ComputeHashField(&buffer, len); |
| 5516 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5517 | |
| 5518 | // Store the hash code in the object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5519 | set_hash_field(field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5520 | |
| 5521 | // Check the hash code is there. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5522 | ASSERT(HasHashCode()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5523 | uint32_t result = field >> kHashShift; |
| 5524 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 5525 | return result; |
| 5526 | } |
| 5527 | |
| 5528 | |
| 5529 | bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer, |
| 5530 | uint32_t* index, |
| 5531 | int length) { |
| 5532 | if (length == 0 || length > kMaxArrayIndexSize) return false; |
| 5533 | uc32 ch = buffer->GetNext(); |
| 5534 | |
| 5535 | // If the string begins with a '0' character, it must only consist |
| 5536 | // of it to be a legal array index. |
| 5537 | if (ch == '0') { |
| 5538 | *index = 0; |
| 5539 | return length == 1; |
| 5540 | } |
| 5541 | |
| 5542 | // Convert string to uint32 array index; character by character. |
| 5543 | int d = ch - '0'; |
| 5544 | if (d < 0 || d > 9) return false; |
| 5545 | uint32_t result = d; |
| 5546 | while (buffer->has_more()) { |
| 5547 | d = buffer->GetNext() - '0'; |
| 5548 | if (d < 0 || d > 9) return false; |
| 5549 | // Check that the new result is below the 32 bit limit. |
| 5550 | if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 5551 | result = (result * 10) + d; |
| 5552 | } |
| 5553 | |
| 5554 | *index = result; |
| 5555 | return true; |
| 5556 | } |
| 5557 | |
| 5558 | |
| 5559 | bool String::SlowAsArrayIndex(uint32_t* index) { |
| 5560 | if (length() <= kMaxCachedArrayIndexLength) { |
| 5561 | Hash(); // force computation of hash code |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5562 | uint32_t field = hash_field(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5563 | if ((field & kIsNotArrayIndexMask) != 0) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5564 | // Isolate the array index form the full hash field. |
| 5565 | *index = (kArrayIndexHashMask & field) >> kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5566 | return true; |
| 5567 | } else { |
| 5568 | StringInputBuffer buffer(this); |
| 5569 | return ComputeArrayIndex(&buffer, index, length()); |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5574 | uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5575 | // For array indexes mix the length into the hash as an array index could |
| 5576 | // be zero. |
| 5577 | ASSERT(length > 0); |
| 5578 | ASSERT(length <= String::kMaxArrayIndexSize); |
| 5579 | ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) < |
| 5580 | (1 << String::kArrayIndexValueBits)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5581 | |
| 5582 | value <<= String::kHashShift; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5583 | value |= length << String::kArrayIndexHashLengthShift; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5584 | |
| 5585 | ASSERT((value & String::kIsNotArrayIndexMask) == 0); |
| 5586 | ASSERT((length > String::kMaxCachedArrayIndexLength) || |
| 5587 | (value & String::kContainsCachedArrayIndexMask) == 0); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5588 | return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | |
| 5592 | uint32_t StringHasher::GetHashField() { |
| 5593 | ASSERT(is_valid()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5594 | if (length_ <= String::kMaxHashCalcLength) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5595 | if (is_array_index()) { |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 5596 | return MakeArrayIndexHash(array_index(), length_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5597 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5598 | return (GetHash() << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5599 | } else { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5600 | return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5601 | } |
| 5602 | } |
| 5603 | |
| 5604 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5605 | uint32_t String::ComputeHashField(unibrow::CharacterStream* buffer, |
| 5606 | int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5607 | StringHasher hasher(length); |
| 5608 | |
| 5609 | // Very long strings have a trivial hash that doesn't inspect the |
| 5610 | // string contents. |
| 5611 | if (hasher.has_trivial_hash()) { |
| 5612 | return hasher.GetHashField(); |
| 5613 | } |
| 5614 | |
| 5615 | // Do the iterative array index computation as long as there is a |
| 5616 | // chance this is an array index. |
| 5617 | while (buffer->has_more() && hasher.is_array_index()) { |
| 5618 | hasher.AddCharacter(buffer->GetNext()); |
| 5619 | } |
| 5620 | |
| 5621 | // Process the remaining characters without updating the array |
| 5622 | // index. |
| 5623 | while (buffer->has_more()) { |
| 5624 | hasher.AddCharacterNoIndex(buffer->GetNext()); |
| 5625 | } |
| 5626 | |
| 5627 | return hasher.GetHashField(); |
| 5628 | } |
| 5629 | |
| 5630 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5631 | MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5632 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5633 | if (start == 0 && end == length()) return this; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5634 | MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5635 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | |
| 5639 | void String::PrintOn(FILE* file) { |
| 5640 | int length = this->length(); |
| 5641 | for (int i = 0; i < length; i++) { |
| 5642 | fprintf(file, "%c", Get(i)); |
| 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | |
| 5647 | void Map::CreateBackPointers() { |
| 5648 | DescriptorArray* descriptors = instance_descriptors(); |
| 5649 | for (int i = 0; i < descriptors->number_of_descriptors(); i++) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5650 | if (descriptors->GetType(i) == MAP_TRANSITION || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5651 | descriptors->GetType(i) == EXTERNAL_ARRAY_TRANSITION || |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5652 | descriptors->GetType(i) == CONSTANT_TRANSITION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5653 | // Get target. |
| 5654 | Map* target = Map::cast(descriptors->GetValue(i)); |
| 5655 | #ifdef DEBUG |
| 5656 | // Verify target. |
| 5657 | Object* source_prototype = prototype(); |
| 5658 | Object* target_prototype = target->prototype(); |
| 5659 | ASSERT(source_prototype->IsJSObject() || |
| 5660 | source_prototype->IsMap() || |
| 5661 | source_prototype->IsNull()); |
| 5662 | ASSERT(target_prototype->IsJSObject() || |
| 5663 | target_prototype->IsNull()); |
| 5664 | ASSERT(source_prototype->IsMap() || |
| 5665 | source_prototype == target_prototype); |
| 5666 | #endif |
| 5667 | // Point target back to source. set_prototype() will not let us set |
| 5668 | // the prototype to a map, as we do here. |
| 5669 | *RawField(target, kPrototypeOffset) = this; |
| 5670 | } |
| 5671 | } |
| 5672 | } |
| 5673 | |
| 5674 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5675 | void Map::ClearNonLiveTransitions(Heap* heap, Object* real_prototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5676 | // Live DescriptorArray objects will be marked, so we must use |
| 5677 | // low-level accessors to get and modify their data. |
| 5678 | DescriptorArray* d = reinterpret_cast<DescriptorArray*>( |
| 5679 | *RawField(this, Map::kInstanceDescriptorsOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5680 | if (d == heap->raw_unchecked_empty_descriptor_array()) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5681 | Smi* NullDescriptorDetails = |
| 5682 | PropertyDetails(NONE, NULL_DESCRIPTOR).AsSmi(); |
| 5683 | FixedArray* contents = reinterpret_cast<FixedArray*>( |
| 5684 | d->get(DescriptorArray::kContentArrayIndex)); |
| 5685 | ASSERT(contents->length() >= 2); |
| 5686 | for (int i = 0; i < contents->length(); i += 2) { |
| 5687 | // If the pair (value, details) is a map transition, |
| 5688 | // check if the target is live. If not, null the descriptor. |
| 5689 | // Also drop the back pointer for that map transition, so that this |
| 5690 | // map is not reached again by following a back pointer from a |
| 5691 | // non-live object. |
| 5692 | PropertyDetails details(Smi::cast(contents->get(i + 1))); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5693 | if (details.type() == MAP_TRANSITION || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5694 | details.type() == EXTERNAL_ARRAY_TRANSITION || |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5695 | details.type() == CONSTANT_TRANSITION) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5696 | Map* target = reinterpret_cast<Map*>(contents->get(i)); |
| 5697 | ASSERT(target->IsHeapObject()); |
| 5698 | if (!target->IsMarked()) { |
| 5699 | ASSERT(target->IsMap()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5700 | contents->set_unchecked(i + 1, NullDescriptorDetails); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5701 | contents->set_null_unchecked(heap, i); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5702 | ASSERT(target->prototype() == this || |
| 5703 | target->prototype() == real_prototype); |
| 5704 | // Getter prototype() is read-only, set_prototype() has side effects. |
| 5705 | *RawField(target, Map::kPrototypeOffset) = real_prototype; |
| 5706 | } |
| 5707 | } |
| 5708 | } |
| 5709 | } |
| 5710 | |
| 5711 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5712 | void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) { |
| 5713 | // Iterate over all fields in the body but take care in dealing with |
| 5714 | // the code entry. |
| 5715 | IteratePointers(v, kPropertiesOffset, kCodeEntryOffset); |
| 5716 | v->VisitCodeEntry(this->address() + kCodeEntryOffset); |
| 5717 | IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size); |
| 5718 | } |
| 5719 | |
| 5720 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5721 | void JSFunction::MarkForLazyRecompilation() { |
| 5722 | ASSERT(is_compiled() && !IsOptimized()); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 5723 | ASSERT(shared()->allows_lazy_compilation() || |
| 5724 | code()->optimizable()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5725 | Builtins* builtins = GetIsolate()->builtins(); |
| 5726 | ReplaceCode(builtins->builtin(Builtins::kLazyRecompile)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | |
| 5730 | uint32_t JSFunction::SourceHash() { |
| 5731 | uint32_t hash = 0; |
| 5732 | Object* script = shared()->script(); |
| 5733 | if (!script->IsUndefined()) { |
| 5734 | Object* source = Script::cast(script)->source(); |
| 5735 | if (source->IsUndefined()) hash = String::cast(source)->Hash(); |
| 5736 | } |
| 5737 | hash ^= ComputeIntegerHash(shared()->start_position_and_type()); |
| 5738 | hash += ComputeIntegerHash(shared()->end_position()); |
| 5739 | return hash; |
| 5740 | } |
| 5741 | |
| 5742 | |
| 5743 | bool JSFunction::IsInlineable() { |
| 5744 | if (IsBuiltin()) return false; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5745 | SharedFunctionInfo* shared_info = shared(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5746 | // Check that the function has a script associated with it. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5747 | if (!shared_info->script()->IsScript()) return false; |
| 5748 | if (shared_info->optimization_disabled()) return false; |
| 5749 | Code* code = shared_info->code(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5750 | if (code->kind() == Code::OPTIMIZED_FUNCTION) return true; |
| 5751 | // If we never ran this (unlikely) then lets try to optimize it. |
| 5752 | if (code->kind() != Code::FUNCTION) return true; |
| 5753 | return code->optimizable(); |
| 5754 | } |
| 5755 | |
| 5756 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5757 | Object* JSFunction::SetInstancePrototype(Object* value) { |
| 5758 | ASSERT(value->IsJSObject()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5759 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5760 | if (has_initial_map()) { |
| 5761 | initial_map()->set_prototype(value); |
| 5762 | } else { |
| 5763 | // Put the value in the initial map field until an initial map is |
| 5764 | // needed. At that point, a new initial map is created and the |
| 5765 | // prototype is put into the initial map where it belongs. |
| 5766 | set_prototype_or_initial_map(value); |
| 5767 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5768 | heap->ClearInstanceofCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5769 | return value; |
| 5770 | } |
| 5771 | |
| 5772 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5773 | MaybeObject* JSFunction::SetPrototype(Object* value) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5774 | ASSERT(should_have_prototype()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5775 | Object* construct_prototype = value; |
| 5776 | |
| 5777 | // If the value is not a JSObject, store the value in the map's |
| 5778 | // constructor field so it can be accessed. Also, set the prototype |
| 5779 | // used for constructing objects to the original object prototype. |
| 5780 | // See ECMA-262 13.2.2. |
| 5781 | if (!value->IsJSObject()) { |
| 5782 | // Copy the map so this does not affect unrelated functions. |
| 5783 | // Remove map transitions because they point to maps with a |
| 5784 | // different prototype. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5785 | Object* new_object; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5786 | { MaybeObject* maybe_new_map = map()->CopyDropTransitions(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5787 | if (!maybe_new_map->ToObject(&new_object)) return maybe_new_map; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5788 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5789 | Map* new_map = Map::cast(new_object); |
| 5790 | Heap* heap = new_map->heap(); |
| 5791 | set_map(new_map); |
| 5792 | new_map->set_constructor(value); |
| 5793 | new_map->set_non_instance_prototype(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5794 | construct_prototype = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5795 | heap->isolate()->context()->global_context()-> |
| 5796 | initial_object_prototype(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5797 | } else { |
| 5798 | map()->set_non_instance_prototype(false); |
| 5799 | } |
| 5800 | |
| 5801 | return SetInstancePrototype(construct_prototype); |
| 5802 | } |
| 5803 | |
| 5804 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5805 | Object* JSFunction::RemovePrototype() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5806 | Context* global_context = context()->global_context(); |
| 5807 | Map* no_prototype_map = shared()->strict_mode() |
| 5808 | ? global_context->strict_mode_function_without_prototype_map() |
| 5809 | : global_context->function_without_prototype_map(); |
| 5810 | |
| 5811 | if (map() == no_prototype_map) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5812 | // Be idempotent. |
| 5813 | return this; |
| 5814 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5815 | |
| 5816 | ASSERT(!shared()->strict_mode() || |
| 5817 | map() == global_context->strict_mode_function_map()); |
| 5818 | ASSERT(shared()->strict_mode() || map() == global_context->function_map()); |
| 5819 | |
| 5820 | set_map(no_prototype_map); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5821 | set_prototype_or_initial_map(no_prototype_map->heap()->the_hole_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5822 | return this; |
| 5823 | } |
| 5824 | |
| 5825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5826 | Object* JSFunction::SetInstanceClassName(String* name) { |
| 5827 | shared()->set_instance_class_name(name); |
| 5828 | return this; |
| 5829 | } |
| 5830 | |
| 5831 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5832 | void JSFunction::PrintName(FILE* out) { |
| 5833 | SmartPointer<char> name = shared()->DebugName()->ToCString(); |
| 5834 | PrintF(out, "%s", *name); |
| 5835 | } |
| 5836 | |
| 5837 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5838 | Context* JSFunction::GlobalContextFromLiterals(FixedArray* literals) { |
| 5839 | return Context::cast(literals->get(JSFunction::kLiteralGlobalContextIndex)); |
| 5840 | } |
| 5841 | |
| 5842 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5843 | MaybeObject* Oddball::Initialize(const char* to_string, |
| 5844 | Object* to_number, |
| 5845 | byte kind) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5846 | Object* symbol; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5847 | { MaybeObject* maybe_symbol = |
| 5848 | Isolate::Current()->heap()->LookupAsciiSymbol(to_string); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5849 | if (!maybe_symbol->ToObject(&symbol)) return maybe_symbol; |
| 5850 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5851 | set_to_string(String::cast(symbol)); |
| 5852 | set_to_number(to_number); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5853 | set_kind(kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5854 | return this; |
| 5855 | } |
| 5856 | |
| 5857 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5858 | String* SharedFunctionInfo::DebugName() { |
| 5859 | Object* n = name(); |
| 5860 | if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name(); |
| 5861 | return String::cast(n); |
| 5862 | } |
| 5863 | |
| 5864 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5865 | bool SharedFunctionInfo::HasSourceCode() { |
| 5866 | return !script()->IsUndefined() && |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5867 | !reinterpret_cast<Script*>(script())->source()->IsUndefined(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | |
| 5871 | Object* SharedFunctionInfo::GetSourceCode() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5872 | Isolate* isolate = GetIsolate(); |
| 5873 | if (!HasSourceCode()) return isolate->heap()->undefined_value(); |
| 5874 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5875 | Object* source = Script::cast(script())->source(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5876 | return *SubString(Handle<String>(String::cast(source), isolate), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5877 | start_position(), end_position()); |
| 5878 | } |
| 5879 | |
| 5880 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5881 | int SharedFunctionInfo::SourceSize() { |
| 5882 | return end_position() - start_position(); |
| 5883 | } |
| 5884 | |
| 5885 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5886 | int SharedFunctionInfo::CalculateInstanceSize() { |
| 5887 | int instance_size = |
| 5888 | JSObject::kHeaderSize + |
| 5889 | expected_nof_properties() * kPointerSize; |
| 5890 | if (instance_size > JSObject::kMaxInstanceSize) { |
| 5891 | instance_size = JSObject::kMaxInstanceSize; |
| 5892 | } |
| 5893 | return instance_size; |
| 5894 | } |
| 5895 | |
| 5896 | |
| 5897 | int SharedFunctionInfo::CalculateInObjectProperties() { |
| 5898 | return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize; |
| 5899 | } |
| 5900 | |
| 5901 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5902 | bool SharedFunctionInfo::CanGenerateInlineConstructor(Object* prototype) { |
| 5903 | // Check the basic conditions for generating inline constructor code. |
| 5904 | if (!FLAG_inline_new |
| 5905 | || !has_only_simple_this_property_assignments() |
| 5906 | || this_property_assignments_count() == 0) { |
| 5907 | return false; |
| 5908 | } |
| 5909 | |
| 5910 | // If the prototype is null inline constructors cause no problems. |
| 5911 | if (!prototype->IsJSObject()) { |
| 5912 | ASSERT(prototype->IsNull()); |
| 5913 | return true; |
| 5914 | } |
| 5915 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 5916 | Heap* heap = GetHeap(); |
| 5917 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5918 | // Traverse the proposed prototype chain looking for setters for properties of |
| 5919 | // the same names as are set by the inline constructor. |
| 5920 | for (Object* obj = prototype; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5921 | obj != heap->null_value(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5922 | obj = obj->GetPrototype()) { |
| 5923 | JSObject* js_object = JSObject::cast(obj); |
| 5924 | for (int i = 0; i < this_property_assignments_count(); i++) { |
| 5925 | LookupResult result; |
| 5926 | String* name = GetThisPropertyAssignmentName(i); |
| 5927 | js_object->LocalLookupRealNamedProperty(name, &result); |
| 5928 | if (result.IsProperty() && result.type() == CALLBACKS) { |
| 5929 | return false; |
| 5930 | } |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | return true; |
| 5935 | } |
| 5936 | |
| 5937 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 5938 | void SharedFunctionInfo::ForbidInlineConstructor() { |
| 5939 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
| 5940 | kHasOnlySimpleThisPropertyAssignments, |
| 5941 | false)); |
| 5942 | } |
| 5943 | |
| 5944 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5945 | void SharedFunctionInfo::SetThisPropertyAssignmentsInfo( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5946 | bool only_simple_this_property_assignments, |
| 5947 | FixedArray* assignments) { |
| 5948 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5949 | kHasOnlySimpleThisPropertyAssignments, |
| 5950 | only_simple_this_property_assignments)); |
| 5951 | set_this_property_assignments(assignments); |
| 5952 | set_this_property_assignments_count(assignments->length() / 3); |
| 5953 | } |
| 5954 | |
| 5955 | |
| 5956 | void SharedFunctionInfo::ClearThisPropertyAssignmentsInfo() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5957 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5958 | set_compiler_hints(BooleanBit::set(compiler_hints(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5959 | kHasOnlySimpleThisPropertyAssignments, |
| 5960 | false)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5961 | set_this_property_assignments(heap->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5962 | set_this_property_assignments_count(0); |
| 5963 | } |
| 5964 | |
| 5965 | |
| 5966 | String* SharedFunctionInfo::GetThisPropertyAssignmentName(int index) { |
| 5967 | Object* obj = this_property_assignments(); |
| 5968 | ASSERT(obj->IsFixedArray()); |
| 5969 | ASSERT(index < this_property_assignments_count()); |
| 5970 | obj = FixedArray::cast(obj)->get(index * 3); |
| 5971 | ASSERT(obj->IsString()); |
| 5972 | return String::cast(obj); |
| 5973 | } |
| 5974 | |
| 5975 | |
| 5976 | bool SharedFunctionInfo::IsThisPropertyAssignmentArgument(int index) { |
| 5977 | Object* obj = this_property_assignments(); |
| 5978 | ASSERT(obj->IsFixedArray()); |
| 5979 | ASSERT(index < this_property_assignments_count()); |
| 5980 | obj = FixedArray::cast(obj)->get(index * 3 + 1); |
| 5981 | return Smi::cast(obj)->value() != -1; |
| 5982 | } |
| 5983 | |
| 5984 | |
| 5985 | int SharedFunctionInfo::GetThisPropertyAssignmentArgument(int index) { |
| 5986 | ASSERT(IsThisPropertyAssignmentArgument(index)); |
| 5987 | Object* obj = |
| 5988 | FixedArray::cast(this_property_assignments())->get(index * 3 + 1); |
| 5989 | return Smi::cast(obj)->value(); |
| 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | Object* SharedFunctionInfo::GetThisPropertyAssignmentConstant(int index) { |
| 5994 | ASSERT(!IsThisPropertyAssignmentArgument(index)); |
| 5995 | Object* obj = |
| 5996 | FixedArray::cast(this_property_assignments())->get(index * 3 + 2); |
| 5997 | return obj; |
| 5998 | } |
| 5999 | |
| 6000 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6001 | // Support function for printing the source code to a StringStream |
| 6002 | // without any allocation in the heap. |
| 6003 | void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator, |
| 6004 | int max_length) { |
| 6005 | // For some native functions there is no source. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6006 | if (!HasSourceCode()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6007 | accumulator->Add("<No Source>"); |
| 6008 | return; |
| 6009 | } |
| 6010 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6011 | // Get the source for the script which this function came from. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6012 | // Don't use String::cast because we don't want more assertion errors while |
| 6013 | // we are already creating a stack dump. |
| 6014 | String* script_source = |
| 6015 | reinterpret_cast<String*>(Script::cast(script())->source()); |
| 6016 | |
| 6017 | if (!script_source->LooksValid()) { |
| 6018 | accumulator->Add("<Invalid Source>"); |
| 6019 | return; |
| 6020 | } |
| 6021 | |
| 6022 | if (!is_toplevel()) { |
| 6023 | accumulator->Add("function "); |
| 6024 | Object* name = this->name(); |
| 6025 | if (name->IsString() && String::cast(name)->length() > 0) { |
| 6026 | accumulator->PrintName(name); |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | int len = end_position() - start_position(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6031 | if (len <= max_length || max_length < 0) { |
| 6032 | accumulator->Put(script_source, start_position(), end_position()); |
| 6033 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6034 | accumulator->Put(script_source, |
| 6035 | start_position(), |
| 6036 | start_position() + max_length); |
| 6037 | accumulator->Add("...\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6038 | } |
| 6039 | } |
| 6040 | |
| 6041 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6042 | static bool IsCodeEquivalent(Code* code, Code* recompiled) { |
| 6043 | if (code->instruction_size() != recompiled->instruction_size()) return false; |
| 6044 | ByteArray* code_relocation = code->relocation_info(); |
| 6045 | ByteArray* recompiled_relocation = recompiled->relocation_info(); |
| 6046 | int length = code_relocation->length(); |
| 6047 | if (length != recompiled_relocation->length()) return false; |
| 6048 | int compare = memcmp(code_relocation->GetDataStartAddress(), |
| 6049 | recompiled_relocation->GetDataStartAddress(), |
| 6050 | length); |
| 6051 | return compare == 0; |
| 6052 | } |
| 6053 | |
| 6054 | |
| 6055 | void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) { |
| 6056 | ASSERT(!has_deoptimization_support()); |
| 6057 | AssertNoAllocation no_allocation; |
| 6058 | Code* code = this->code(); |
| 6059 | if (IsCodeEquivalent(code, recompiled)) { |
| 6060 | // Copy the deoptimization data from the recompiled code. |
| 6061 | code->set_deoptimization_data(recompiled->deoptimization_data()); |
| 6062 | code->set_has_deoptimization_support(true); |
| 6063 | } else { |
| 6064 | // TODO(3025757): In case the recompiled isn't equivalent to the |
| 6065 | // old code, we have to replace it. We should try to avoid this |
| 6066 | // altogether because it flushes valuable type feedback by |
| 6067 | // effectively resetting all IC state. |
| 6068 | set_code(recompiled); |
| 6069 | } |
| 6070 | ASSERT(has_deoptimization_support()); |
| 6071 | } |
| 6072 | |
| 6073 | |
| 6074 | bool SharedFunctionInfo::VerifyBailoutId(int id) { |
| 6075 | // TODO(srdjan): debugging ARM crashes in hydrogen. OK to disable while |
| 6076 | // we are always bailing out on ARM. |
| 6077 | |
| 6078 | ASSERT(id != AstNode::kNoNumber); |
| 6079 | Code* unoptimized = code(); |
| 6080 | DeoptimizationOutputData* data = |
| 6081 | DeoptimizationOutputData::cast(unoptimized->deoptimization_data()); |
| 6082 | unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this); |
| 6083 | USE(ignore); |
| 6084 | return true; // Return true if there was no ASSERT. |
| 6085 | } |
| 6086 | |
| 6087 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6088 | void SharedFunctionInfo::StartInobjectSlackTracking(Map* map) { |
| 6089 | ASSERT(!IsInobjectSlackTrackingInProgress()); |
| 6090 | |
| 6091 | // Only initiate the tracking the first time. |
| 6092 | if (live_objects_may_exist()) return; |
| 6093 | set_live_objects_may_exist(true); |
| 6094 | |
| 6095 | // No tracking during the snapshot construction phase. |
| 6096 | if (Serializer::enabled()) return; |
| 6097 | |
| 6098 | if (map->unused_property_fields() == 0) return; |
| 6099 | |
| 6100 | // Nonzero counter is a leftover from the previous attempt interrupted |
| 6101 | // by GC, keep it. |
| 6102 | if (construction_count() == 0) { |
| 6103 | set_construction_count(kGenerousAllocationCount); |
| 6104 | } |
| 6105 | set_initial_map(map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6106 | Builtins* builtins = map->heap()->isolate()->builtins(); |
| 6107 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6108 | construct_stub()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6109 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | |
| 6113 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 6114 | void SharedFunctionInfo::DetachInitialMap() { |
| 6115 | Map* map = reinterpret_cast<Map*>(initial_map()); |
| 6116 | |
| 6117 | // Make the map remember to restore the link if it survives the GC. |
| 6118 | map->set_bit_field2( |
| 6119 | map->bit_field2() | (1 << Map::kAttachedToSharedFunctionInfo)); |
| 6120 | |
| 6121 | // Undo state changes made by StartInobjectTracking (except the |
| 6122 | // construction_count). This way if the initial map does not survive the GC |
| 6123 | // then StartInobjectTracking will be called again the next time the |
| 6124 | // constructor is called. The countdown will continue and (possibly after |
| 6125 | // several more GCs) CompleteInobjectSlackTracking will eventually be called. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6126 | set_initial_map(map->heap()->raw_unchecked_undefined_value()); |
| 6127 | Builtins* builtins = map->heap()->isolate()->builtins(); |
| 6128 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6129 | *RawField(this, kConstructStubOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6130 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6131 | // It is safe to clear the flag: it will be set again if the map is live. |
| 6132 | set_live_objects_may_exist(false); |
| 6133 | } |
| 6134 | |
| 6135 | |
| 6136 | // Called from GC, hence reinterpret_cast and unchecked accessors. |
| 6137 | void SharedFunctionInfo::AttachInitialMap(Map* map) { |
| 6138 | map->set_bit_field2( |
| 6139 | map->bit_field2() & ~(1 << Map::kAttachedToSharedFunctionInfo)); |
| 6140 | |
| 6141 | // Resume inobject slack tracking. |
| 6142 | set_initial_map(map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6143 | Builtins* builtins = map->heap()->isolate()->builtins(); |
| 6144 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric), |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6145 | *RawField(this, kConstructStubOffset)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6146 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6147 | // The map survived the gc, so there may be objects referencing it. |
| 6148 | set_live_objects_may_exist(true); |
| 6149 | } |
| 6150 | |
| 6151 | |
| 6152 | static void GetMinInobjectSlack(Map* map, void* data) { |
| 6153 | int slack = map->unused_property_fields(); |
| 6154 | if (*reinterpret_cast<int*>(data) > slack) { |
| 6155 | *reinterpret_cast<int*>(data) = slack; |
| 6156 | } |
| 6157 | } |
| 6158 | |
| 6159 | |
| 6160 | static void ShrinkInstanceSize(Map* map, void* data) { |
| 6161 | int slack = *reinterpret_cast<int*>(data); |
| 6162 | map->set_inobject_properties(map->inobject_properties() - slack); |
| 6163 | map->set_unused_property_fields(map->unused_property_fields() - slack); |
| 6164 | map->set_instance_size(map->instance_size() - slack * kPointerSize); |
| 6165 | |
| 6166 | // Visitor id might depend on the instance size, recalculate it. |
| 6167 | map->set_visitor_id(StaticVisitorBase::GetVisitorId(map)); |
| 6168 | } |
| 6169 | |
| 6170 | |
| 6171 | void SharedFunctionInfo::CompleteInobjectSlackTracking() { |
| 6172 | ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress()); |
| 6173 | Map* map = Map::cast(initial_map()); |
| 6174 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6175 | Heap* heap = map->heap(); |
| 6176 | set_initial_map(heap->undefined_value()); |
| 6177 | Builtins* builtins = heap->isolate()->builtins(); |
| 6178 | ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown), |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6179 | construct_stub()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6180 | set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 6181 | |
| 6182 | int slack = map->unused_property_fields(); |
| 6183 | map->TraverseTransitionTree(&GetMinInobjectSlack, &slack); |
| 6184 | if (slack != 0) { |
| 6185 | // Resize the initial map and all maps in its transition tree. |
| 6186 | map->TraverseTransitionTree(&ShrinkInstanceSize, &slack); |
| 6187 | // Give the correct expected_nof_properties to initial maps created later. |
| 6188 | ASSERT(expected_nof_properties() >= slack); |
| 6189 | set_expected_nof_properties(expected_nof_properties() - slack); |
| 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6194 | void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) { |
| 6195 | ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode())); |
| 6196 | Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address()); |
| 6197 | Object* old_target = target; |
| 6198 | VisitPointer(&target); |
| 6199 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 6200 | } |
| 6201 | |
| 6202 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 6203 | void ObjectVisitor::VisitCodeEntry(Address entry_address) { |
| 6204 | Object* code = Code::GetObjectFromEntryAddress(entry_address); |
| 6205 | Object* old_code = code; |
| 6206 | VisitPointer(&code); |
| 6207 | if (code != old_code) { |
| 6208 | Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry(); |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6213 | void ObjectVisitor::VisitGlobalPropertyCell(RelocInfo* rinfo) { |
| 6214 | ASSERT(rinfo->rmode() == RelocInfo::GLOBAL_PROPERTY_CELL); |
| 6215 | Object* cell = rinfo->target_cell(); |
| 6216 | Object* old_cell = cell; |
| 6217 | VisitPointer(&cell); |
| 6218 | if (cell != old_cell) { |
| 6219 | rinfo->set_target_cell(reinterpret_cast<JSGlobalPropertyCell*>(cell)); |
| 6220 | } |
| 6221 | } |
| 6222 | |
| 6223 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6224 | void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6225 | ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) && |
| 6226 | rinfo->IsPatchedReturnSequence()) || |
| 6227 | (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) && |
| 6228 | rinfo->IsPatchedDebugBreakSlotSequence())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6229 | Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address()); |
| 6230 | Object* old_target = target; |
| 6231 | VisitPointer(&target); |
| 6232 | CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target. |
| 6233 | } |
| 6234 | |
| 6235 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6236 | void Code::InvalidateRelocation() { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6237 | set_relocation_info(heap()->empty_byte_array()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6241 | void Code::Relocate(intptr_t delta) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6242 | for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) { |
| 6243 | it.rinfo()->apply(delta); |
| 6244 | } |
| 6245 | CPU::FlushICache(instruction_start(), instruction_size()); |
| 6246 | } |
| 6247 | |
| 6248 | |
| 6249 | void Code::CopyFrom(const CodeDesc& desc) { |
| 6250 | // copy code |
| 6251 | memmove(instruction_start(), desc.buffer, desc.instr_size); |
| 6252 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6253 | // copy reloc info |
| 6254 | memmove(relocation_start(), |
| 6255 | desc.buffer + desc.buffer_size - desc.reloc_size, |
| 6256 | desc.reloc_size); |
| 6257 | |
| 6258 | // unbox handles and relocate |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6259 | intptr_t delta = instruction_start() - desc.buffer; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6260 | int mode_mask = RelocInfo::kCodeTargetMask | |
| 6261 | RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6262 | RelocInfo::ModeMask(RelocInfo::GLOBAL_PROPERTY_CELL) | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6263 | RelocInfo::kApplyMask; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6264 | Assembler* origin = desc.origin; // Needed to find target_object on X64. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6265 | for (RelocIterator it(this, mode_mask); !it.done(); it.next()) { |
| 6266 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 6267 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6268 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6269 | it.rinfo()->set_target_object(*p); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6270 | } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6271 | Handle<JSGlobalPropertyCell> cell = it.rinfo()->target_cell_handle(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6272 | it.rinfo()->set_target_cell(*cell); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6273 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 6274 | // rewrite code handles in inline cache targets to direct |
| 6275 | // pointers to the first instruction in the code object |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6276 | Handle<Object> p = it.rinfo()->target_object_handle(origin); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6277 | Code* code = Code::cast(*p); |
| 6278 | it.rinfo()->set_target_address(code->instruction_start()); |
| 6279 | } else { |
| 6280 | it.rinfo()->apply(delta); |
| 6281 | } |
| 6282 | } |
| 6283 | CPU::FlushICache(instruction_start(), instruction_size()); |
| 6284 | } |
| 6285 | |
| 6286 | |
| 6287 | // Locate the source position which is closest to the address in the code. This |
| 6288 | // is using the source position information embedded in the relocation info. |
| 6289 | // The position returned is relative to the beginning of the script where the |
| 6290 | // source for this function is found. |
| 6291 | int Code::SourcePosition(Address pc) { |
| 6292 | int distance = kMaxInt; |
| 6293 | int position = RelocInfo::kNoPosition; // Initially no position found. |
| 6294 | // Run through all the relocation info to find the best matching source |
| 6295 | // position. All the code needs to be considered as the sequence of the |
| 6296 | // instructions in the code does not necessarily follow the same order as the |
| 6297 | // source. |
| 6298 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 6299 | while (!it.done()) { |
| 6300 | // Only look at positions after the current pc. |
| 6301 | if (it.rinfo()->pc() < pc) { |
| 6302 | // Get position and distance. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6303 | |
| 6304 | int dist = static_cast<int>(pc - it.rinfo()->pc()); |
| 6305 | int pos = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6306 | // If this position is closer than the current candidate or if it has the |
| 6307 | // same distance as the current candidate and the position is higher then |
| 6308 | // this position is the new candidate. |
| 6309 | if ((dist < distance) || |
| 6310 | (dist == distance && pos > position)) { |
| 6311 | position = pos; |
| 6312 | distance = dist; |
| 6313 | } |
| 6314 | } |
| 6315 | it.next(); |
| 6316 | } |
| 6317 | return position; |
| 6318 | } |
| 6319 | |
| 6320 | |
| 6321 | // Same as Code::SourcePosition above except it only looks for statement |
| 6322 | // positions. |
| 6323 | int Code::SourceStatementPosition(Address pc) { |
| 6324 | // First find the position as close as possible using all position |
| 6325 | // information. |
| 6326 | int position = SourcePosition(pc); |
| 6327 | // Now find the closest statement position before the position. |
| 6328 | int statement_position = 0; |
| 6329 | RelocIterator it(this, RelocInfo::kPositionMask); |
| 6330 | while (!it.done()) { |
| 6331 | if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6332 | int p = static_cast<int>(it.rinfo()->data()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6333 | if (statement_position < p && p <= position) { |
| 6334 | statement_position = p; |
| 6335 | } |
| 6336 | } |
| 6337 | it.next(); |
| 6338 | } |
| 6339 | return statement_position; |
| 6340 | } |
| 6341 | |
| 6342 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 6343 | SafepointEntry Code::GetSafepointEntry(Address pc) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6344 | SafepointTable table(this); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 6345 | return table.FindEntry(pc); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6346 | } |
| 6347 | |
| 6348 | |
| 6349 | void Code::SetNoStackCheckTable() { |
| 6350 | // Indicate the absence of a stack-check table by a table start after the |
| 6351 | // end of the instructions. Table start must be aligned, so round up. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6352 | set_stack_check_table_offset(RoundUp(instruction_size(), kIntSize)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6353 | } |
| 6354 | |
| 6355 | |
| 6356 | Map* Code::FindFirstMap() { |
| 6357 | ASSERT(is_inline_cache_stub()); |
| 6358 | AssertNoAllocation no_allocation; |
| 6359 | int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT); |
| 6360 | for (RelocIterator it(this, mask); !it.done(); it.next()) { |
| 6361 | RelocInfo* info = it.rinfo(); |
| 6362 | Object* object = info->target_object(); |
| 6363 | if (object->IsMap()) return Map::cast(object); |
| 6364 | } |
| 6365 | return NULL; |
| 6366 | } |
| 6367 | |
| 6368 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6369 | #ifdef ENABLE_DISASSEMBLER |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6370 | |
| 6371 | #ifdef OBJECT_PRINT |
| 6372 | |
| 6373 | void DeoptimizationInputData::DeoptimizationInputDataPrint(FILE* out) { |
| 6374 | disasm::NameConverter converter; |
| 6375 | int deopt_count = DeoptCount(); |
| 6376 | PrintF(out, "Deoptimization Input Data (deopt points = %d)\n", deopt_count); |
| 6377 | if (0 == deopt_count) return; |
| 6378 | |
| 6379 | PrintF(out, "%6s %6s %6s %12s\n", "index", "ast id", "argc", "commands"); |
| 6380 | for (int i = 0; i < deopt_count; i++) { |
| 6381 | int command_count = 0; |
| 6382 | PrintF(out, "%6d %6d %6d", |
| 6383 | i, AstId(i)->value(), ArgumentsStackHeight(i)->value()); |
| 6384 | int translation_index = TranslationIndex(i)->value(); |
| 6385 | TranslationIterator iterator(TranslationByteArray(), translation_index); |
| 6386 | Translation::Opcode opcode = |
| 6387 | static_cast<Translation::Opcode>(iterator.Next()); |
| 6388 | ASSERT(Translation::BEGIN == opcode); |
| 6389 | int frame_count = iterator.Next(); |
| 6390 | if (FLAG_print_code_verbose) { |
| 6391 | PrintF(out, " %s {count=%d}\n", Translation::StringFor(opcode), |
| 6392 | frame_count); |
| 6393 | } |
| 6394 | |
| 6395 | for (int i = 0; i < frame_count; ++i) { |
| 6396 | opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 6397 | ASSERT(Translation::FRAME == opcode); |
| 6398 | int ast_id = iterator.Next(); |
| 6399 | int function_id = iterator.Next(); |
| 6400 | JSFunction* function = |
| 6401 | JSFunction::cast(LiteralArray()->get(function_id)); |
| 6402 | unsigned height = iterator.Next(); |
| 6403 | if (FLAG_print_code_verbose) { |
| 6404 | PrintF(out, "%24s %s {ast_id=%d, function=", |
| 6405 | "", Translation::StringFor(opcode), ast_id); |
| 6406 | function->PrintName(out); |
| 6407 | PrintF(out, ", height=%u}\n", height); |
| 6408 | } |
| 6409 | |
| 6410 | // Size of translation is height plus all incoming arguments including |
| 6411 | // receiver. |
| 6412 | int size = height + function->shared()->formal_parameter_count() + 1; |
| 6413 | command_count += size; |
| 6414 | for (int j = 0; j < size; ++j) { |
| 6415 | opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 6416 | if (FLAG_print_code_verbose) { |
| 6417 | PrintF(out, "%24s %s ", "", Translation::StringFor(opcode)); |
| 6418 | } |
| 6419 | |
| 6420 | if (opcode == Translation::DUPLICATE) { |
| 6421 | opcode = static_cast<Translation::Opcode>(iterator.Next()); |
| 6422 | if (FLAG_print_code_verbose) { |
| 6423 | PrintF(out, "%s ", Translation::StringFor(opcode)); |
| 6424 | } |
| 6425 | --j; // Two commands share the same frame index. |
| 6426 | } |
| 6427 | |
| 6428 | switch (opcode) { |
| 6429 | case Translation::BEGIN: |
| 6430 | case Translation::FRAME: |
| 6431 | case Translation::DUPLICATE: |
| 6432 | UNREACHABLE(); |
| 6433 | break; |
| 6434 | |
| 6435 | case Translation::REGISTER: { |
| 6436 | int reg_code = iterator.Next(); |
| 6437 | if (FLAG_print_code_verbose) { |
| 6438 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 6439 | } |
| 6440 | break; |
| 6441 | } |
| 6442 | |
| 6443 | case Translation::INT32_REGISTER: { |
| 6444 | int reg_code = iterator.Next(); |
| 6445 | if (FLAG_print_code_verbose) { |
| 6446 | PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code)); |
| 6447 | } |
| 6448 | break; |
| 6449 | } |
| 6450 | |
| 6451 | case Translation::DOUBLE_REGISTER: { |
| 6452 | int reg_code = iterator.Next(); |
| 6453 | if (FLAG_print_code_verbose) { |
| 6454 | PrintF(out, "{input=%s}", |
| 6455 | DoubleRegister::AllocationIndexToString(reg_code)); |
| 6456 | } |
| 6457 | break; |
| 6458 | } |
| 6459 | |
| 6460 | case Translation::STACK_SLOT: { |
| 6461 | int input_slot_index = iterator.Next(); |
| 6462 | if (FLAG_print_code_verbose) { |
| 6463 | PrintF(out, "{input=%d}", input_slot_index); |
| 6464 | } |
| 6465 | break; |
| 6466 | } |
| 6467 | |
| 6468 | case Translation::INT32_STACK_SLOT: { |
| 6469 | int input_slot_index = iterator.Next(); |
| 6470 | if (FLAG_print_code_verbose) { |
| 6471 | PrintF(out, "{input=%d}", input_slot_index); |
| 6472 | } |
| 6473 | break; |
| 6474 | } |
| 6475 | |
| 6476 | case Translation::DOUBLE_STACK_SLOT: { |
| 6477 | int input_slot_index = iterator.Next(); |
| 6478 | if (FLAG_print_code_verbose) { |
| 6479 | PrintF(out, "{input=%d}", input_slot_index); |
| 6480 | } |
| 6481 | break; |
| 6482 | } |
| 6483 | |
| 6484 | case Translation::LITERAL: { |
| 6485 | unsigned literal_index = iterator.Next(); |
| 6486 | if (FLAG_print_code_verbose) { |
| 6487 | PrintF(out, "{literal_id=%u}", literal_index); |
| 6488 | } |
| 6489 | break; |
| 6490 | } |
| 6491 | |
| 6492 | case Translation::ARGUMENTS_OBJECT: |
| 6493 | break; |
| 6494 | } |
| 6495 | if (FLAG_print_code_verbose) PrintF(out, "\n"); |
| 6496 | } |
| 6497 | } |
| 6498 | if (!FLAG_print_code_verbose) PrintF(out, " %12d\n", command_count); |
| 6499 | } |
| 6500 | } |
| 6501 | |
| 6502 | |
| 6503 | void DeoptimizationOutputData::DeoptimizationOutputDataPrint(FILE* out) { |
| 6504 | PrintF(out, "Deoptimization Output Data (deopt points = %d)\n", |
| 6505 | this->DeoptPoints()); |
| 6506 | if (this->DeoptPoints() == 0) return; |
| 6507 | |
| 6508 | PrintF("%6s %8s %s\n", "ast id", "pc", "state"); |
| 6509 | for (int i = 0; i < this->DeoptPoints(); i++) { |
| 6510 | int pc_and_state = this->PcAndState(i)->value(); |
| 6511 | PrintF("%6d %8d %s\n", |
| 6512 | this->AstId(i)->value(), |
| 6513 | FullCodeGenerator::PcField::decode(pc_and_state), |
| 6514 | FullCodeGenerator::State2String( |
| 6515 | FullCodeGenerator::StateField::decode(pc_and_state))); |
| 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | #endif |
| 6520 | |
| 6521 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6522 | // Identify kind of code. |
| 6523 | const char* Code::Kind2String(Kind kind) { |
| 6524 | switch (kind) { |
| 6525 | case FUNCTION: return "FUNCTION"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6526 | case OPTIMIZED_FUNCTION: return "OPTIMIZED_FUNCTION"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6527 | case STUB: return "STUB"; |
| 6528 | case BUILTIN: return "BUILTIN"; |
| 6529 | case LOAD_IC: return "LOAD_IC"; |
| 6530 | case KEYED_LOAD_IC: return "KEYED_LOAD_IC"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6531 | case KEYED_EXTERNAL_ARRAY_LOAD_IC: return "KEYED_EXTERNAL_ARRAY_LOAD_IC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6532 | case STORE_IC: return "STORE_IC"; |
| 6533 | case KEYED_STORE_IC: return "KEYED_STORE_IC"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6534 | case KEYED_EXTERNAL_ARRAY_STORE_IC: return "KEYED_EXTERNAL_ARRAY_STORE_IC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6535 | case CALL_IC: return "CALL_IC"; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6536 | case KEYED_CALL_IC: return "KEYED_CALL_IC"; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6537 | case TYPE_RECORDING_BINARY_OP_IC: return "TYPE_RECORDING_BINARY_OP_IC"; |
| 6538 | case COMPARE_IC: return "COMPARE_IC"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6539 | } |
| 6540 | UNREACHABLE(); |
| 6541 | return NULL; |
| 6542 | } |
| 6543 | |
| 6544 | |
| 6545 | const char* Code::ICState2String(InlineCacheState state) { |
| 6546 | switch (state) { |
| 6547 | case UNINITIALIZED: return "UNINITIALIZED"; |
| 6548 | case PREMONOMORPHIC: return "PREMONOMORPHIC"; |
| 6549 | case MONOMORPHIC: return "MONOMORPHIC"; |
| 6550 | case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE"; |
| 6551 | case MEGAMORPHIC: return "MEGAMORPHIC"; |
| 6552 | case DEBUG_BREAK: return "DEBUG_BREAK"; |
| 6553 | case DEBUG_PREPARE_STEP_IN: return "DEBUG_PREPARE_STEP_IN"; |
| 6554 | } |
| 6555 | UNREACHABLE(); |
| 6556 | return NULL; |
| 6557 | } |
| 6558 | |
| 6559 | |
| 6560 | const char* Code::PropertyType2String(PropertyType type) { |
| 6561 | switch (type) { |
| 6562 | case NORMAL: return "NORMAL"; |
| 6563 | case FIELD: return "FIELD"; |
| 6564 | case CONSTANT_FUNCTION: return "CONSTANT_FUNCTION"; |
| 6565 | case CALLBACKS: return "CALLBACKS"; |
| 6566 | case INTERCEPTOR: return "INTERCEPTOR"; |
| 6567 | case MAP_TRANSITION: return "MAP_TRANSITION"; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6568 | case EXTERNAL_ARRAY_TRANSITION: return "EXTERNAL_ARRAY_TRANSITION"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6569 | case CONSTANT_TRANSITION: return "CONSTANT_TRANSITION"; |
| 6570 | case NULL_DESCRIPTOR: return "NULL_DESCRIPTOR"; |
| 6571 | } |
| 6572 | UNREACHABLE(); |
| 6573 | return NULL; |
| 6574 | } |
| 6575 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6576 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6577 | void Code::PrintExtraICState(FILE* out, Kind kind, ExtraICState extra) { |
| 6578 | const char* name = NULL; |
| 6579 | switch (kind) { |
| 6580 | case CALL_IC: |
| 6581 | if (extra == STRING_INDEX_OUT_OF_BOUNDS) { |
| 6582 | name = "STRING_INDEX_OUT_OF_BOUNDS"; |
| 6583 | } |
| 6584 | break; |
| 6585 | case STORE_IC: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6586 | case KEYED_STORE_IC: |
| 6587 | if (extra == kStrictMode) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6588 | name = "STRICT"; |
| 6589 | } |
| 6590 | break; |
| 6591 | default: |
| 6592 | break; |
| 6593 | } |
| 6594 | if (name != NULL) { |
| 6595 | PrintF(out, "extra_ic_state = %s\n", name); |
| 6596 | } else { |
| 6597 | PrintF(out, "etra_ic_state = %d\n", extra); |
| 6598 | } |
| 6599 | } |
| 6600 | |
| 6601 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6602 | void Code::Disassemble(const char* name, FILE* out) { |
| 6603 | PrintF(out, "kind = %s\n", Kind2String(kind())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6604 | if (is_inline_cache_stub()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6605 | PrintF(out, "ic_state = %s\n", ICState2String(ic_state())); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6606 | PrintExtraICState(out, kind(), extra_ic_state()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6607 | PrintF(out, "ic_in_loop = %d\n", ic_in_loop() == IN_LOOP); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6608 | if (ic_state() == MONOMORPHIC) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6609 | PrintF(out, "type = %s\n", PropertyType2String(type())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | if ((name != NULL) && (name[0] != '\0')) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6613 | PrintF(out, "name = %s\n", name); |
| 6614 | } |
| 6615 | if (kind() == OPTIMIZED_FUNCTION) { |
| 6616 | PrintF(out, "stack_slots = %d\n", stack_slots()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6619 | PrintF(out, "Instructions (size = %d)\n", instruction_size()); |
| 6620 | Disassembler::Decode(out, this); |
| 6621 | PrintF(out, "\n"); |
| 6622 | |
| 6623 | #ifdef DEBUG |
| 6624 | if (kind() == FUNCTION) { |
| 6625 | DeoptimizationOutputData* data = |
| 6626 | DeoptimizationOutputData::cast(this->deoptimization_data()); |
| 6627 | data->DeoptimizationOutputDataPrint(out); |
| 6628 | } else if (kind() == OPTIMIZED_FUNCTION) { |
| 6629 | DeoptimizationInputData* data = |
| 6630 | DeoptimizationInputData::cast(this->deoptimization_data()); |
| 6631 | data->DeoptimizationInputDataPrint(out); |
| 6632 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6633 | PrintF("\n"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6634 | #endif |
| 6635 | |
| 6636 | if (kind() == OPTIMIZED_FUNCTION) { |
| 6637 | SafepointTable table(this); |
| 6638 | PrintF(out, "Safepoints (size = %u)\n", table.size()); |
| 6639 | for (unsigned i = 0; i < table.length(); i++) { |
| 6640 | unsigned pc_offset = table.GetPcOffset(i); |
| 6641 | PrintF(out, "%p %4d ", (instruction_start() + pc_offset), pc_offset); |
| 6642 | table.PrintEntry(i); |
| 6643 | PrintF(out, " (sp -> fp)"); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 6644 | SafepointEntry entry = table.GetEntry(i); |
| 6645 | if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) { |
| 6646 | PrintF(out, " %6d", entry.deoptimization_index()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6647 | } else { |
| 6648 | PrintF(out, " <none>"); |
| 6649 | } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 6650 | if (entry.argument_count() > 0) { |
| 6651 | PrintF(out, " argc: %d", entry.argument_count()); |
| 6652 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6653 | PrintF(out, "\n"); |
| 6654 | } |
| 6655 | PrintF(out, "\n"); |
| 6656 | } else if (kind() == FUNCTION) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 6657 | unsigned offset = stack_check_table_offset(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6658 | // If there is no stack check table, the "table start" will at or after |
| 6659 | // (due to alignment) the end of the instruction stream. |
| 6660 | if (static_cast<int>(offset) < instruction_size()) { |
| 6661 | unsigned* address = |
| 6662 | reinterpret_cast<unsigned*>(instruction_start() + offset); |
| 6663 | unsigned length = address[0]; |
| 6664 | PrintF(out, "Stack checks (size = %u)\n", length); |
| 6665 | PrintF(out, "ast_id pc_offset\n"); |
| 6666 | for (unsigned i = 0; i < length; ++i) { |
| 6667 | unsigned index = (2 * i) + 1; |
| 6668 | PrintF(out, "%6u %9u\n", address[index], address[index + 1]); |
| 6669 | } |
| 6670 | PrintF(out, "\n"); |
| 6671 | } |
| 6672 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6673 | |
| 6674 | PrintF("RelocInfo (size = %d)\n", relocation_size()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6675 | for (RelocIterator it(this); !it.done(); it.next()) it.rinfo()->Print(out); |
| 6676 | PrintF(out, "\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6677 | } |
| 6678 | #endif // ENABLE_DISASSEMBLER |
| 6679 | |
| 6680 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6681 | MaybeObject* JSObject::SetFastElementsCapacityAndLength(int capacity, |
| 6682 | int length) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6683 | Heap* heap = GetHeap(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6684 | // We should never end in here with a pixel or external array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6685 | ASSERT(!HasExternalArrayElements()); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6686 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6687 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6688 | { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(capacity); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6689 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6690 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6691 | FixedArray* elems = FixedArray::cast(obj); |
| 6692 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6693 | { MaybeObject* maybe_obj = map()->GetFastElementsMap(); |
| 6694 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6695 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6696 | Map* new_map = Map::cast(obj); |
| 6697 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6698 | AssertNoAllocation no_gc; |
| 6699 | WriteBarrierMode mode = elems->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6700 | switch (GetElementsKind()) { |
| 6701 | case FAST_ELEMENTS: { |
| 6702 | FixedArray* old_elements = FixedArray::cast(elements()); |
| 6703 | uint32_t old_length = static_cast<uint32_t>(old_elements->length()); |
| 6704 | // Fill out the new array with this content and array holes. |
| 6705 | for (uint32_t i = 0; i < old_length; i++) { |
| 6706 | elems->set(i, old_elements->get(i), mode); |
| 6707 | } |
| 6708 | break; |
| 6709 | } |
| 6710 | case DICTIONARY_ELEMENTS: { |
| 6711 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 6712 | for (int i = 0; i < dictionary->Capacity(); i++) { |
| 6713 | Object* key = dictionary->KeyAt(i); |
| 6714 | if (key->IsNumber()) { |
| 6715 | uint32_t entry = static_cast<uint32_t>(key->Number()); |
| 6716 | elems->set(entry, dictionary->ValueAt(i), mode); |
| 6717 | } |
| 6718 | } |
| 6719 | break; |
| 6720 | } |
| 6721 | default: |
| 6722 | UNREACHABLE(); |
| 6723 | break; |
| 6724 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6725 | |
| 6726 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6727 | set_elements(elems); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6728 | |
| 6729 | if (IsJSArray()) { |
| 6730 | JSArray::cast(this)->set_length(Smi::FromInt(length)); |
| 6731 | } |
| 6732 | |
| 6733 | return this; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6734 | } |
| 6735 | |
| 6736 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6737 | MaybeObject* JSObject::SetSlowElements(Object* len) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6738 | // We should never end in here with a pixel or external array. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6739 | ASSERT(!HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6740 | |
| 6741 | uint32_t new_length = static_cast<uint32_t>(len->Number()); |
| 6742 | |
| 6743 | switch (GetElementsKind()) { |
| 6744 | case FAST_ELEMENTS: { |
| 6745 | // Make sure we never try to shrink dense arrays into sparse arrays. |
| 6746 | ASSERT(static_cast<uint32_t>(FixedArray::cast(elements())->length()) <= |
| 6747 | new_length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6748 | Object* obj; |
| 6749 | { MaybeObject* maybe_obj = NormalizeElements(); |
| 6750 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6751 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6752 | |
| 6753 | // Update length for JSArrays. |
| 6754 | if (IsJSArray()) JSArray::cast(this)->set_length(len); |
| 6755 | break; |
| 6756 | } |
| 6757 | case DICTIONARY_ELEMENTS: { |
| 6758 | if (IsJSArray()) { |
| 6759 | uint32_t old_length = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6760 | static_cast<uint32_t>(JSArray::cast(this)->length()->Number()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6761 | element_dictionary()->RemoveNumberEntries(new_length, old_length), |
| 6762 | JSArray::cast(this)->set_length(len); |
| 6763 | } |
| 6764 | break; |
| 6765 | } |
| 6766 | default: |
| 6767 | UNREACHABLE(); |
| 6768 | break; |
| 6769 | } |
| 6770 | return this; |
| 6771 | } |
| 6772 | |
| 6773 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6774 | MaybeObject* JSArray::Initialize(int capacity) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6775 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6776 | ASSERT(capacity >= 0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6777 | set_length(Smi::FromInt(0)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6778 | FixedArray* new_elements; |
| 6779 | if (capacity == 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6780 | new_elements = heap->empty_fixed_array(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6781 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6782 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6783 | { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(capacity); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6784 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6785 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6786 | new_elements = FixedArray::cast(obj); |
| 6787 | } |
| 6788 | set_elements(new_elements); |
| 6789 | return this; |
| 6790 | } |
| 6791 | |
| 6792 | |
| 6793 | void JSArray::Expand(int required_size) { |
| 6794 | Handle<JSArray> self(this); |
| 6795 | Handle<FixedArray> old_backing(FixedArray::cast(elements())); |
| 6796 | int old_size = old_backing->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 6797 | int new_size = required_size > old_size ? required_size : old_size; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6798 | Handle<FixedArray> new_backing = FACTORY->NewFixedArray(new_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6799 | // Can't use this any more now because we may have had a GC! |
| 6800 | for (int i = 0; i < old_size; i++) new_backing->set(i, old_backing->get(i)); |
| 6801 | self->SetContent(*new_backing); |
| 6802 | } |
| 6803 | |
| 6804 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6805 | static Failure* ArrayLengthRangeError(Heap* heap) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6806 | HandleScope scope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6807 | return heap->isolate()->Throw( |
| 6808 | *FACTORY->NewRangeError("invalid_array_length", |
| 6809 | HandleVector<Object>(NULL, 0))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6813 | MaybeObject* JSObject::SetElementsLength(Object* len) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 6814 | // We should never end in here with a pixel or external array. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6815 | ASSERT(AllowsSetElementsLength()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6816 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6817 | MaybeObject* maybe_smi_length = len->ToSmi(); |
| 6818 | Object* smi_length = Smi::FromInt(0); |
| 6819 | if (maybe_smi_length->ToObject(&smi_length) && smi_length->IsSmi()) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 6820 | const int value = Smi::cast(smi_length)->value(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6821 | if (value < 0) return ArrayLengthRangeError(GetHeap()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6822 | switch (GetElementsKind()) { |
| 6823 | case FAST_ELEMENTS: { |
| 6824 | int old_capacity = FixedArray::cast(elements())->length(); |
| 6825 | if (value <= old_capacity) { |
| 6826 | if (IsJSArray()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6827 | Object* obj; |
| 6828 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 6829 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6830 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6831 | int old_length = FastD2I(JSArray::cast(this)->length()->Number()); |
| 6832 | // NOTE: We may be able to optimize this by removing the |
| 6833 | // last part of the elements backing storage array and |
| 6834 | // setting the capacity to the new size. |
| 6835 | for (int i = value; i < old_length; i++) { |
| 6836 | FixedArray::cast(elements())->set_the_hole(i); |
| 6837 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6838 | JSArray::cast(this)->set_length(Smi::cast(smi_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6839 | } |
| 6840 | return this; |
| 6841 | } |
| 6842 | int min = NewElementsCapacity(old_capacity); |
| 6843 | int new_capacity = value > min ? value : min; |
| 6844 | if (new_capacity <= kMaxFastElementsLength || |
| 6845 | !ShouldConvertToSlowElements(new_capacity)) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6846 | Object* obj; |
| 6847 | { MaybeObject* maybe_obj = |
| 6848 | SetFastElementsCapacityAndLength(new_capacity, value); |
| 6849 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6850 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6851 | return this; |
| 6852 | } |
| 6853 | break; |
| 6854 | } |
| 6855 | case DICTIONARY_ELEMENTS: { |
| 6856 | if (IsJSArray()) { |
| 6857 | if (value == 0) { |
| 6858 | // If the length of a slow array is reset to zero, we clear |
| 6859 | // the array and flush backing storage. This has the added |
| 6860 | // benefit that the array returns to fast mode. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6861 | Object* obj; |
| 6862 | { MaybeObject* maybe_obj = ResetElements(); |
| 6863 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6864 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6865 | } else { |
| 6866 | // Remove deleted elements. |
| 6867 | uint32_t old_length = |
| 6868 | static_cast<uint32_t>(JSArray::cast(this)->length()->Number()); |
| 6869 | element_dictionary()->RemoveNumberEntries(value, old_length); |
| 6870 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6871 | JSArray::cast(this)->set_length(Smi::cast(smi_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6872 | } |
| 6873 | return this; |
| 6874 | } |
| 6875 | default: |
| 6876 | UNREACHABLE(); |
| 6877 | break; |
| 6878 | } |
| 6879 | } |
| 6880 | |
| 6881 | // General slow case. |
| 6882 | if (len->IsNumber()) { |
| 6883 | uint32_t length; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 6884 | if (len->ToArrayIndex(&length)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6885 | return SetSlowElements(len); |
| 6886 | } else { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6887 | return ArrayLengthRangeError(GetHeap()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6888 | } |
| 6889 | } |
| 6890 | |
| 6891 | // len is not a number so make the array size one and |
| 6892 | // set only element to len. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6893 | Object* obj; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6894 | { MaybeObject* maybe_obj = GetHeap()->AllocateFixedArray(1); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6895 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6896 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6897 | FixedArray::cast(obj)->set(0, len); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 6898 | if (IsJSArray()) JSArray::cast(this)->set_length(Smi::FromInt(1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6899 | set_elements(FixedArray::cast(obj)); |
| 6900 | return this; |
| 6901 | } |
| 6902 | |
| 6903 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 6904 | Object* Map::GetPrototypeTransition(Object* prototype) { |
| 6905 | FixedArray* cache = prototype_transitions(); |
| 6906 | int capacity = cache->length(); |
| 6907 | if (capacity == 0) return NULL; |
| 6908 | int finger = Smi::cast(cache->get(0))->value(); |
| 6909 | for (int i = 1; i < finger; i += 2) { |
| 6910 | if (cache->get(i) == prototype) return cache->get(i + 1); |
| 6911 | } |
| 6912 | return NULL; |
| 6913 | } |
| 6914 | |
| 6915 | |
| 6916 | MaybeObject* Map::PutPrototypeTransition(Object* prototype, Map* map) { |
| 6917 | // Don't cache prototype transition if this map is shared. |
| 6918 | if (is_shared() || !FLAG_cache_prototype_transitions) return this; |
| 6919 | |
| 6920 | FixedArray* cache = prototype_transitions(); |
| 6921 | |
| 6922 | int capacity = cache->length(); |
| 6923 | |
| 6924 | int finger = (capacity == 0) ? 1 : Smi::cast(cache->get(0))->value(); |
| 6925 | |
| 6926 | if (finger >= capacity) { |
| 6927 | if (capacity > kMaxCachedPrototypeTransitions) return this; |
| 6928 | |
| 6929 | FixedArray* new_cache; |
| 6930 | { MaybeObject* maybe_cache = heap()->AllocateFixedArray(finger * 2 + 1); |
| 6931 | if (!maybe_cache->To<FixedArray>(&new_cache)) return maybe_cache; |
| 6932 | } |
| 6933 | |
| 6934 | for (int i = 1; i < capacity; i++) new_cache->set(i, cache->get(i)); |
| 6935 | cache = new_cache; |
| 6936 | set_prototype_transitions(cache); |
| 6937 | } |
| 6938 | |
| 6939 | cache->set(finger, prototype); |
| 6940 | cache->set(finger + 1, map); |
| 6941 | cache->set(0, Smi::FromInt(finger + 2)); |
| 6942 | |
| 6943 | return cache; |
| 6944 | } |
| 6945 | |
| 6946 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 6947 | MaybeObject* JSObject::SetPrototype(Object* value, |
| 6948 | bool skip_hidden_prototypes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6949 | Heap* heap = GetHeap(); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6950 | // Silently ignore the change if value is not a JSObject or null. |
| 6951 | // SpiderMonkey behaves this way. |
| 6952 | if (!value->IsJSObject() && !value->IsNull()) return value; |
| 6953 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 6954 | // From 8.6.2 Object Internal Methods |
| 6955 | // ... |
| 6956 | // In addition, if [[Extensible]] is false the value of the [[Class]] and |
| 6957 | // [[Prototype]] internal properties of the object may not be modified. |
| 6958 | // ... |
| 6959 | // Implementation specific extensions that modify [[Class]], [[Prototype]] |
| 6960 | // or [[Extensible]] must not violate the invariants defined in the preceding |
| 6961 | // paragraph. |
| 6962 | if (!this->map()->is_extensible()) { |
| 6963 | HandleScope scope; |
| 6964 | Handle<Object> handle(this, heap->isolate()); |
| 6965 | return heap->isolate()->Throw( |
| 6966 | *FACTORY->NewTypeError("non_extensible_proto", |
| 6967 | HandleVector<Object>(&handle, 1))); |
| 6968 | } |
| 6969 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6970 | // Before we can set the prototype we need to be sure |
| 6971 | // prototype cycles are prevented. |
| 6972 | // It is sufficient to validate that the receiver is not in the new prototype |
| 6973 | // chain. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6974 | for (Object* pt = value; pt != heap->null_value(); pt = pt->GetPrototype()) { |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6975 | if (JSObject::cast(pt) == this) { |
| 6976 | // Cycle detected. |
| 6977 | HandleScope scope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6978 | return heap->isolate()->Throw( |
| 6979 | *FACTORY->NewError("cyclic_proto", HandleVector<Object>(NULL, 0))); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6980 | } |
| 6981 | } |
| 6982 | |
| 6983 | JSObject* real_receiver = this; |
| 6984 | |
| 6985 | if (skip_hidden_prototypes) { |
| 6986 | // Find the first object in the chain whose prototype object is not |
| 6987 | // hidden and set the new prototype on that object. |
| 6988 | Object* current_proto = real_receiver->GetPrototype(); |
| 6989 | while (current_proto->IsJSObject() && |
| 6990 | JSObject::cast(current_proto)->map()->is_hidden_prototype()) { |
| 6991 | real_receiver = JSObject::cast(current_proto); |
| 6992 | current_proto = current_proto->GetPrototype(); |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | // Set the new prototype of the object. |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 6997 | Map* map = real_receiver->map(); |
| 6998 | |
| 6999 | // Nothing to do if prototype is already set. |
| 7000 | if (map->prototype() == value) return value; |
| 7001 | |
| 7002 | Object* new_map = map->GetPrototypeTransition(value); |
| 7003 | if (new_map == NULL) { |
| 7004 | { MaybeObject* maybe_new_map = map->CopyDropTransitions(); |
| 7005 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 7006 | } |
| 7007 | |
| 7008 | { MaybeObject* maybe_new_cache = |
| 7009 | map->PutPrototypeTransition(value, Map::cast(new_map)); |
| 7010 | if (maybe_new_cache->IsFailure()) return maybe_new_cache; |
| 7011 | } |
| 7012 | |
| 7013 | Map::cast(new_map)->set_prototype(value); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7014 | } |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 7015 | ASSERT(Map::cast(new_map)->prototype() == value); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7016 | real_receiver->set_map(Map::cast(new_map)); |
| 7017 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7018 | heap->ClearInstanceofCache(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 7019 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 7020 | return value; |
| 7021 | } |
| 7022 | |
| 7023 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7024 | bool JSObject::HasElementPostInterceptor(JSObject* receiver, uint32_t index) { |
| 7025 | switch (GetElementsKind()) { |
| 7026 | case FAST_ELEMENTS: { |
| 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 ((index < length) && |
| 7032 | !FixedArray::cast(elements())->get(index)->IsTheHole()) { |
| 7033 | return true; |
| 7034 | } |
| 7035 | break; |
| 7036 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7037 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 7038 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7039 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 7040 | return true; |
| 7041 | } |
| 7042 | break; |
| 7043 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7044 | case EXTERNAL_BYTE_ELEMENTS: |
| 7045 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7046 | case EXTERNAL_SHORT_ELEMENTS: |
| 7047 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7048 | case EXTERNAL_INT_ELEMENTS: |
| 7049 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7050 | case EXTERNAL_FLOAT_ELEMENTS: { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7051 | ExternalArray* array = ExternalArray::cast(elements()); |
| 7052 | if (index < static_cast<uint32_t>(array->length())) { |
| 7053 | return true; |
| 7054 | } |
| 7055 | break; |
| 7056 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7057 | case DICTIONARY_ELEMENTS: { |
| 7058 | if (element_dictionary()->FindEntry(index) |
| 7059 | != NumberDictionary::kNotFound) { |
| 7060 | return true; |
| 7061 | } |
| 7062 | break; |
| 7063 | } |
| 7064 | default: |
| 7065 | UNREACHABLE(); |
| 7066 | break; |
| 7067 | } |
| 7068 | |
| 7069 | // Handle [] on String objects. |
| 7070 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 7071 | |
| 7072 | Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7073 | if (pt->IsNull()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7074 | return JSObject::cast(pt)->HasElementWithReceiver(receiver, index); |
| 7075 | } |
| 7076 | |
| 7077 | |
| 7078 | bool JSObject::HasElementWithInterceptor(JSObject* receiver, uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7079 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7080 | // Make sure that the top context does not change when doing |
| 7081 | // callbacks or interceptor calls. |
| 7082 | AssertNoContextChange ncc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7083 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7084 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 7085 | Handle<JSObject> receiver_handle(receiver); |
| 7086 | Handle<JSObject> holder_handle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7087 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7088 | v8::AccessorInfo info(args.end()); |
| 7089 | if (!interceptor->query()->IsUndefined()) { |
| 7090 | v8::IndexedPropertyQuery query = |
| 7091 | v8::ToCData<v8::IndexedPropertyQuery>(interceptor->query()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7092 | LOG(isolate, |
| 7093 | ApiIndexedPropertyAccess("interceptor-indexed-has", this, index)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7094 | v8::Handle<v8::Integer> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7095 | { |
| 7096 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7097 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7098 | result = query(index, info); |
| 7099 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7100 | if (!result.IsEmpty()) { |
| 7101 | ASSERT(result->IsInt32()); |
| 7102 | return true; // absence of property is signaled by empty handle. |
| 7103 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7104 | } else if (!interceptor->getter()->IsUndefined()) { |
| 7105 | v8::IndexedPropertyGetter getter = |
| 7106 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7107 | LOG(isolate, |
| 7108 | ApiIndexedPropertyAccess("interceptor-indexed-has-get", this, index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7109 | v8::Handle<v8::Value> result; |
| 7110 | { |
| 7111 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7112 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7113 | result = getter(index, info); |
| 7114 | } |
| 7115 | if (!result.IsEmpty()) return true; |
| 7116 | } |
| 7117 | return holder_handle->HasElementPostInterceptor(*receiver_handle, index); |
| 7118 | } |
| 7119 | |
| 7120 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7121 | JSObject::LocalElementType JSObject::HasLocalElement(uint32_t index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7122 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7123 | if (IsAccessCheckNeeded()) { |
| 7124 | Heap* heap = GetHeap(); |
| 7125 | if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 7126 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 7127 | return UNDEFINED_ELEMENT; |
| 7128 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7131 | if (IsJSGlobalProxy()) { |
| 7132 | Object* proto = GetPrototype(); |
| 7133 | if (proto->IsNull()) return UNDEFINED_ELEMENT; |
| 7134 | ASSERT(proto->IsJSGlobalObject()); |
| 7135 | return JSObject::cast(proto)->HasLocalElement(index); |
| 7136 | } |
| 7137 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7138 | // Check for lookup interceptor |
| 7139 | if (HasIndexedInterceptor()) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7140 | return HasElementWithInterceptor(this, index) ? INTERCEPTED_ELEMENT |
| 7141 | : UNDEFINED_ELEMENT; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | // Handle [] on String objects. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7145 | if (this->IsStringObjectWithCharacterAt(index)) { |
| 7146 | return STRING_CHARACTER_ELEMENT; |
| 7147 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7148 | |
| 7149 | switch (GetElementsKind()) { |
| 7150 | case FAST_ELEMENTS: { |
| 7151 | uint32_t length = IsJSArray() ? |
| 7152 | static_cast<uint32_t> |
| 7153 | (Smi::cast(JSArray::cast(this)->length())->value()) : |
| 7154 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7155 | if ((index < length) && |
| 7156 | !FixedArray::cast(elements())->get(index)->IsTheHole()) { |
| 7157 | return FAST_ELEMENT; |
| 7158 | } |
| 7159 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7160 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7161 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 7162 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7163 | if (index < static_cast<uint32_t>(pixels->length())) return FAST_ELEMENT; |
| 7164 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7165 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7166 | case EXTERNAL_BYTE_ELEMENTS: |
| 7167 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7168 | case EXTERNAL_SHORT_ELEMENTS: |
| 7169 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7170 | case EXTERNAL_INT_ELEMENTS: |
| 7171 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7172 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7173 | ExternalArray* array = ExternalArray::cast(elements()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7174 | if (index < static_cast<uint32_t>(array->length())) return FAST_ELEMENT; |
| 7175 | break; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7176 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7177 | case DICTIONARY_ELEMENTS: { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7178 | if (element_dictionary()->FindEntry(index) != |
| 7179 | NumberDictionary::kNotFound) { |
| 7180 | return DICTIONARY_ELEMENT; |
| 7181 | } |
| 7182 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7183 | } |
| 7184 | default: |
| 7185 | UNREACHABLE(); |
| 7186 | break; |
| 7187 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 7188 | |
| 7189 | return UNDEFINED_ELEMENT; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7190 | } |
| 7191 | |
| 7192 | |
| 7193 | bool JSObject::HasElementWithReceiver(JSObject* receiver, uint32_t index) { |
| 7194 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7195 | if (IsAccessCheckNeeded()) { |
| 7196 | Heap* heap = GetHeap(); |
| 7197 | if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 7198 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 7199 | return false; |
| 7200 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | // Check for lookup interceptor |
| 7204 | if (HasIndexedInterceptor()) { |
| 7205 | return HasElementWithInterceptor(receiver, index); |
| 7206 | } |
| 7207 | |
| 7208 | switch (GetElementsKind()) { |
| 7209 | case FAST_ELEMENTS: { |
| 7210 | uint32_t length = IsJSArray() ? |
| 7211 | static_cast<uint32_t> |
| 7212 | (Smi::cast(JSArray::cast(this)->length())->value()) : |
| 7213 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 7214 | if ((index < length) && |
| 7215 | !FixedArray::cast(elements())->get(index)->IsTheHole()) return true; |
| 7216 | break; |
| 7217 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7218 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 7219 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7220 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 7221 | return true; |
| 7222 | } |
| 7223 | break; |
| 7224 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7225 | case EXTERNAL_BYTE_ELEMENTS: |
| 7226 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7227 | case EXTERNAL_SHORT_ELEMENTS: |
| 7228 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7229 | case EXTERNAL_INT_ELEMENTS: |
| 7230 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7231 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7232 | ExternalArray* array = ExternalArray::cast(elements()); |
| 7233 | if (index < static_cast<uint32_t>(array->length())) { |
| 7234 | return true; |
| 7235 | } |
| 7236 | break; |
| 7237 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7238 | case DICTIONARY_ELEMENTS: { |
| 7239 | if (element_dictionary()->FindEntry(index) |
| 7240 | != NumberDictionary::kNotFound) { |
| 7241 | return true; |
| 7242 | } |
| 7243 | break; |
| 7244 | } |
| 7245 | default: |
| 7246 | UNREACHABLE(); |
| 7247 | break; |
| 7248 | } |
| 7249 | |
| 7250 | // Handle [] on String objects. |
| 7251 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 7252 | |
| 7253 | Object* pt = GetPrototype(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7254 | if (pt->IsNull()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7255 | return JSObject::cast(pt)->HasElementWithReceiver(receiver, index); |
| 7256 | } |
| 7257 | |
| 7258 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7259 | MaybeObject* JSObject::SetElementWithInterceptor(uint32_t index, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7260 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7261 | StrictModeFlag strict_mode, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7262 | bool check_prototype) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7263 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7264 | // Make sure that the top context does not change when doing |
| 7265 | // callbacks or interceptor calls. |
| 7266 | AssertNoContextChange ncc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7267 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7268 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor()); |
| 7269 | Handle<JSObject> this_handle(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7270 | Handle<Object> value_handle(value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7271 | if (!interceptor->setter()->IsUndefined()) { |
| 7272 | v8::IndexedPropertySetter setter = |
| 7273 | v8::ToCData<v8::IndexedPropertySetter>(interceptor->setter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7274 | LOG(isolate, |
| 7275 | ApiIndexedPropertyAccess("interceptor-indexed-set", this, index)); |
| 7276 | CustomArguments args(isolate, interceptor->data(), this, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7277 | v8::AccessorInfo info(args.end()); |
| 7278 | v8::Handle<v8::Value> result; |
| 7279 | { |
| 7280 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7281 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7282 | result = setter(index, v8::Utils::ToLocal(value_handle), info); |
| 7283 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7284 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7285 | if (!result.IsEmpty()) return *value_handle; |
| 7286 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7287 | MaybeObject* raw_result = |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7288 | this_handle->SetElementWithoutInterceptor(index, |
| 7289 | *value_handle, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7290 | strict_mode, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7291 | check_prototype); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7292 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7293 | return raw_result; |
| 7294 | } |
| 7295 | |
| 7296 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7297 | MaybeObject* JSObject::GetElementWithCallback(Object* receiver, |
| 7298 | Object* structure, |
| 7299 | uint32_t index, |
| 7300 | Object* holder) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7301 | Isolate* isolate = GetIsolate(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7302 | ASSERT(!structure->IsProxy()); |
| 7303 | |
| 7304 | // api style callbacks. |
| 7305 | if (structure->IsAccessorInfo()) { |
| 7306 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 7307 | Object* fun_obj = data->getter(); |
| 7308 | v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7309 | HandleScope scope(isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7310 | Handle<JSObject> self(JSObject::cast(receiver)); |
| 7311 | Handle<JSObject> holder_handle(JSObject::cast(holder)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7312 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 7313 | Handle<String> key(isolate->factory()->NumberToString(number)); |
| 7314 | LOG(isolate, ApiNamedPropertyAccess("load", *self, *key)); |
| 7315 | CustomArguments args(isolate, data->data(), *self, *holder_handle); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7316 | v8::AccessorInfo info(args.end()); |
| 7317 | v8::Handle<v8::Value> result; |
| 7318 | { |
| 7319 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7320 | VMState state(isolate, EXTERNAL); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7321 | result = call_fun(v8::Utils::ToLocal(key), info); |
| 7322 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7323 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
| 7324 | if (result.IsEmpty()) return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7325 | return *v8::Utils::OpenHandle(*result); |
| 7326 | } |
| 7327 | |
| 7328 | // __defineGetter__ callback |
| 7329 | if (structure->IsFixedArray()) { |
| 7330 | Object* getter = FixedArray::cast(structure)->get(kGetterIndex); |
| 7331 | if (getter->IsJSFunction()) { |
| 7332 | return Object::GetPropertyWithDefinedGetter(receiver, |
| 7333 | JSFunction::cast(getter)); |
| 7334 | } |
| 7335 | // Getter is not a function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7336 | return isolate->heap()->undefined_value(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7337 | } |
| 7338 | |
| 7339 | UNREACHABLE(); |
| 7340 | return NULL; |
| 7341 | } |
| 7342 | |
| 7343 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7344 | MaybeObject* JSObject::SetElementWithCallback(Object* structure, |
| 7345 | uint32_t index, |
| 7346 | Object* value, |
| 7347 | JSObject* holder) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7348 | Isolate* isolate = GetIsolate(); |
| 7349 | HandleScope scope(isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7350 | |
| 7351 | // We should never get here to initialize a const with the hole |
| 7352 | // value since a const declaration would conflict with the setter. |
| 7353 | ASSERT(!value->IsTheHole()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7354 | Handle<Object> value_handle(value, isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7355 | |
| 7356 | // To accommodate both the old and the new api we switch on the |
| 7357 | // data structure used to store the callbacks. Eventually proxy |
| 7358 | // callbacks should be phased out. |
| 7359 | ASSERT(!structure->IsProxy()); |
| 7360 | |
| 7361 | if (structure->IsAccessorInfo()) { |
| 7362 | // api style callbacks |
| 7363 | AccessorInfo* data = AccessorInfo::cast(structure); |
| 7364 | Object* call_obj = data->setter(); |
| 7365 | v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj); |
| 7366 | if (call_fun == NULL) return value; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7367 | Handle<Object> number = isolate->factory()->NewNumberFromUint(index); |
| 7368 | Handle<String> key(isolate->factory()->NumberToString(number)); |
| 7369 | LOG(isolate, ApiNamedPropertyAccess("store", this, *key)); |
| 7370 | CustomArguments args(isolate, data->data(), this, JSObject::cast(holder)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7371 | v8::AccessorInfo info(args.end()); |
| 7372 | { |
| 7373 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7374 | VMState state(isolate, EXTERNAL); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7375 | call_fun(v8::Utils::ToLocal(key), |
| 7376 | v8::Utils::ToLocal(value_handle), |
| 7377 | info); |
| 7378 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7379 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7380 | return *value_handle; |
| 7381 | } |
| 7382 | |
| 7383 | if (structure->IsFixedArray()) { |
| 7384 | Object* setter = FixedArray::cast(structure)->get(kSetterIndex); |
| 7385 | if (setter->IsJSFunction()) { |
| 7386 | return SetPropertyWithDefinedSetter(JSFunction::cast(setter), value); |
| 7387 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7388 | Handle<Object> holder_handle(holder, isolate); |
| 7389 | Handle<Object> key(isolate->factory()->NewNumberFromUint(index)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7390 | Handle<Object> args[2] = { key, holder_handle }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7391 | return isolate->Throw( |
| 7392 | *isolate->factory()->NewTypeError("no_setter_in_callback", |
| 7393 | HandleVector(args, 2))); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7394 | } |
| 7395 | } |
| 7396 | |
| 7397 | UNREACHABLE(); |
| 7398 | return NULL; |
| 7399 | } |
| 7400 | |
| 7401 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7402 | // Adding n elements in fast case is O(n*n). |
| 7403 | // Note: revisit design to have dual undefined values to capture absent |
| 7404 | // elements. |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7405 | MaybeObject* JSObject::SetFastElement(uint32_t index, |
| 7406 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7407 | StrictModeFlag strict_mode, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7408 | bool check_prototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7409 | ASSERT(HasFastElements()); |
| 7410 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7411 | Object* elms_obj; |
| 7412 | { MaybeObject* maybe_elms_obj = EnsureWritableFastElements(); |
| 7413 | if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj; |
| 7414 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7415 | FixedArray* elms = FixedArray::cast(elms_obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7416 | uint32_t elms_length = static_cast<uint32_t>(elms->length()); |
| 7417 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7418 | if (check_prototype && |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7419 | (index >= elms_length || elms->get(index)->IsTheHole())) { |
| 7420 | bool found; |
| 7421 | MaybeObject* result = |
| 7422 | SetElementWithCallbackSetterInPrototypes(index, value, &found); |
| 7423 | if (found) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7424 | } |
| 7425 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7426 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7427 | // Check whether there is extra space in fixed array.. |
| 7428 | if (index < elms_length) { |
| 7429 | elms->set(index, value); |
| 7430 | if (IsJSArray()) { |
| 7431 | // Update the length of the array if needed. |
| 7432 | uint32_t array_length = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7433 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7434 | if (index >= array_length) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 7435 | JSArray::cast(this)->set_length(Smi::FromInt(index + 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7436 | } |
| 7437 | } |
| 7438 | return value; |
| 7439 | } |
| 7440 | |
| 7441 | // Allow gap in fast case. |
| 7442 | if ((index - elms_length) < kMaxGap) { |
| 7443 | // Try allocating extra space. |
| 7444 | int new_capacity = NewElementsCapacity(index+1); |
| 7445 | if (new_capacity <= kMaxFastElementsLength || |
| 7446 | !ShouldConvertToSlowElements(new_capacity)) { |
| 7447 | ASSERT(static_cast<uint32_t>(new_capacity) > index); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7448 | Object* obj; |
| 7449 | { MaybeObject* maybe_obj = |
| 7450 | SetFastElementsCapacityAndLength(new_capacity, index + 1); |
| 7451 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7452 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7453 | FixedArray::cast(elements())->set(index, value); |
| 7454 | return value; |
| 7455 | } |
| 7456 | } |
| 7457 | |
| 7458 | // Otherwise default to slow case. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7459 | Object* obj; |
| 7460 | { MaybeObject* maybe_obj = NormalizeElements(); |
| 7461 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7462 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7463 | ASSERT(HasDictionaryElements()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7464 | return SetElement(index, value, strict_mode, check_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7465 | } |
| 7466 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 7467 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7468 | MaybeObject* JSObject::SetElement(uint32_t index, |
| 7469 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7470 | StrictModeFlag strict_mode, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7471 | bool check_prototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7472 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7473 | if (IsAccessCheckNeeded()) { |
| 7474 | Heap* heap = GetHeap(); |
| 7475 | if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_SET)) { |
| 7476 | HandleScope scope; |
| 7477 | Handle<Object> value_handle(value); |
| 7478 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_SET); |
| 7479 | return *value_handle; |
| 7480 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7481 | } |
| 7482 | |
| 7483 | if (IsJSGlobalProxy()) { |
| 7484 | Object* proto = GetPrototype(); |
| 7485 | if (proto->IsNull()) return value; |
| 7486 | ASSERT(proto->IsJSGlobalObject()); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7487 | return JSObject::cast(proto)->SetElement(index, |
| 7488 | value, |
| 7489 | strict_mode, |
| 7490 | check_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7491 | } |
| 7492 | |
| 7493 | // Check for lookup interceptor |
| 7494 | if (HasIndexedInterceptor()) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7495 | return SetElementWithInterceptor(index, |
| 7496 | value, |
| 7497 | strict_mode, |
| 7498 | check_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7499 | } |
| 7500 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7501 | return SetElementWithoutInterceptor(index, |
| 7502 | value, |
| 7503 | strict_mode, |
| 7504 | check_prototype); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7508 | MaybeObject* JSObject::SetElementWithoutInterceptor(uint32_t index, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7509 | Object* value, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7510 | StrictModeFlag strict_mode, |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 7511 | bool check_prototype) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7512 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7513 | switch (GetElementsKind()) { |
| 7514 | case FAST_ELEMENTS: |
| 7515 | // Fast case. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7516 | return SetFastElement(index, value, strict_mode, check_prototype); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7517 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 7518 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7519 | return pixels->SetValue(index, value); |
| 7520 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7521 | case EXTERNAL_BYTE_ELEMENTS: { |
| 7522 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 7523 | return array->SetValue(index, value); |
| 7524 | } |
| 7525 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 7526 | ExternalUnsignedByteArray* array = |
| 7527 | ExternalUnsignedByteArray::cast(elements()); |
| 7528 | return array->SetValue(index, value); |
| 7529 | } |
| 7530 | case EXTERNAL_SHORT_ELEMENTS: { |
| 7531 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 7532 | return array->SetValue(index, value); |
| 7533 | } |
| 7534 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 7535 | ExternalUnsignedShortArray* array = |
| 7536 | ExternalUnsignedShortArray::cast(elements()); |
| 7537 | return array->SetValue(index, value); |
| 7538 | } |
| 7539 | case EXTERNAL_INT_ELEMENTS: { |
| 7540 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 7541 | return array->SetValue(index, value); |
| 7542 | } |
| 7543 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 7544 | ExternalUnsignedIntArray* array = |
| 7545 | ExternalUnsignedIntArray::cast(elements()); |
| 7546 | return array->SetValue(index, value); |
| 7547 | } |
| 7548 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7549 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 7550 | return array->SetValue(index, value); |
| 7551 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7552 | case DICTIONARY_ELEMENTS: { |
| 7553 | // Insert element in the dictionary. |
| 7554 | FixedArray* elms = FixedArray::cast(elements()); |
| 7555 | NumberDictionary* dictionary = NumberDictionary::cast(elms); |
| 7556 | |
| 7557 | int entry = dictionary->FindEntry(index); |
| 7558 | if (entry != NumberDictionary::kNotFound) { |
| 7559 | Object* element = dictionary->ValueAt(entry); |
| 7560 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 7561 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7562 | return SetElementWithCallback(element, index, value, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7563 | } else { |
| 7564 | dictionary->UpdateMaxNumberKey(index); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7565 | // If put fails instrict mode, throw exception. |
| 7566 | if (!dictionary->ValueAtPut(entry, value) && |
| 7567 | strict_mode == kStrictMode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7568 | Handle<Object> number(isolate->factory()->NewNumberFromUint(index)); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7569 | Handle<Object> holder(this); |
| 7570 | Handle<Object> args[2] = { number, holder }; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7571 | return isolate->Throw( |
| 7572 | *isolate->factory()->NewTypeError("strict_read_only_property", |
| 7573 | HandleVector(args, 2))); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7574 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7575 | } |
| 7576 | } else { |
| 7577 | // Index not already used. Look for an accessor in the prototype chain. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7578 | if (check_prototype) { |
| 7579 | bool found; |
| 7580 | MaybeObject* result = |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7581 | // Strict mode not needed. No-setter case already handled. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 7582 | SetElementWithCallbackSetterInPrototypes(index, value, &found); |
| 7583 | if (found) return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7584 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7585 | // When we set the is_extensible flag to false we always force |
| 7586 | // the element into dictionary mode (and force them to stay there). |
| 7587 | if (!map()->is_extensible()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7588 | if (strict_mode == kNonStrictMode) { |
| 7589 | return isolate->heap()->undefined_value(); |
| 7590 | } else { |
| 7591 | Handle<Object> number(isolate->factory()->NewNumberFromUint(index)); |
| 7592 | Handle<String> index_string( |
| 7593 | isolate->factory()->NumberToString(number)); |
| 7594 | Handle<Object> args[1] = { index_string }; |
| 7595 | return isolate->Throw( |
| 7596 | *isolate->factory()->NewTypeError("object_not_extensible", |
| 7597 | HandleVector(args, 1))); |
| 7598 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 7599 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7600 | Object* result; |
| 7601 | { MaybeObject* maybe_result = dictionary->AtNumberPut(index, value); |
| 7602 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7603 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7604 | if (elms != FixedArray::cast(result)) { |
| 7605 | set_elements(FixedArray::cast(result)); |
| 7606 | } |
| 7607 | } |
| 7608 | |
| 7609 | // Update the array length if this JSObject is an array. |
| 7610 | if (IsJSArray()) { |
| 7611 | JSArray* array = JSArray::cast(this); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7612 | Object* return_value; |
| 7613 | { MaybeObject* maybe_return_value = |
| 7614 | array->JSArrayUpdateLengthFromIndex(index, value); |
| 7615 | if (!maybe_return_value->ToObject(&return_value)) { |
| 7616 | return maybe_return_value; |
| 7617 | } |
| 7618 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7619 | } |
| 7620 | |
| 7621 | // Attempt to put this object back in fast case. |
| 7622 | if (ShouldConvertToFastElements()) { |
| 7623 | uint32_t new_length = 0; |
| 7624 | if (IsJSArray()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7625 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7626 | } else { |
| 7627 | new_length = NumberDictionary::cast(elements())->max_number_key() + 1; |
| 7628 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7629 | Object* obj; |
| 7630 | { MaybeObject* maybe_obj = |
| 7631 | SetFastElementsCapacityAndLength(new_length, new_length); |
| 7632 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7633 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7634 | #ifdef DEBUG |
| 7635 | if (FLAG_trace_normalization) { |
| 7636 | PrintF("Object elements are fast case again:\n"); |
| 7637 | Print(); |
| 7638 | } |
| 7639 | #endif |
| 7640 | } |
| 7641 | |
| 7642 | return value; |
| 7643 | } |
| 7644 | default: |
| 7645 | UNREACHABLE(); |
| 7646 | break; |
| 7647 | } |
| 7648 | // All possible cases have been handled above. Add a return to avoid the |
| 7649 | // complaints from the compiler. |
| 7650 | UNREACHABLE(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7651 | return isolate->heap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7655 | MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index, |
| 7656 | Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7657 | uint32_t old_len = 0; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7658 | CHECK(length()->ToArrayIndex(&old_len)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7659 | // Check to see if we need to update the length. For now, we make |
| 7660 | // sure that the length stays within 32-bits (unsigned). |
| 7661 | if (index >= old_len && index != 0xffffffff) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7662 | Object* len; |
| 7663 | { MaybeObject* maybe_len = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7664 | GetHeap()->NumberFromDouble(static_cast<double>(index) + 1); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7665 | if (!maybe_len->ToObject(&len)) return maybe_len; |
| 7666 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7667 | set_length(len); |
| 7668 | } |
| 7669 | return value; |
| 7670 | } |
| 7671 | |
| 7672 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7673 | MaybeObject* JSObject::GetElementPostInterceptor(Object* receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7674 | uint32_t index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7675 | // Get element works for both JSObject and JSArray since |
| 7676 | // JSArray::length cannot change. |
| 7677 | switch (GetElementsKind()) { |
| 7678 | case FAST_ELEMENTS: { |
| 7679 | FixedArray* elms = FixedArray::cast(elements()); |
| 7680 | if (index < static_cast<uint32_t>(elms->length())) { |
| 7681 | Object* value = elms->get(index); |
| 7682 | if (!value->IsTheHole()) return value; |
| 7683 | } |
| 7684 | break; |
| 7685 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7686 | case EXTERNAL_PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7687 | case EXTERNAL_BYTE_ELEMENTS: |
| 7688 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7689 | case EXTERNAL_SHORT_ELEMENTS: |
| 7690 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7691 | case EXTERNAL_INT_ELEMENTS: |
| 7692 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7693 | case EXTERNAL_FLOAT_ELEMENTS: { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7694 | MaybeObject* maybe_value = GetExternalElement(index); |
| 7695 | Object* value; |
| 7696 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 7697 | if (!value->IsUndefined()) return value; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7698 | break; |
| 7699 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7700 | case DICTIONARY_ELEMENTS: { |
| 7701 | NumberDictionary* dictionary = element_dictionary(); |
| 7702 | int entry = dictionary->FindEntry(index); |
| 7703 | if (entry != NumberDictionary::kNotFound) { |
| 7704 | Object* element = dictionary->ValueAt(entry); |
| 7705 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 7706 | if (details.type() == CALLBACKS) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 7707 | return GetElementWithCallback(receiver, |
| 7708 | element, |
| 7709 | index, |
| 7710 | this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7711 | } |
| 7712 | return element; |
| 7713 | } |
| 7714 | break; |
| 7715 | } |
| 7716 | default: |
| 7717 | UNREACHABLE(); |
| 7718 | break; |
| 7719 | } |
| 7720 | |
| 7721 | // Continue searching via the prototype chain. |
| 7722 | Object* pt = GetPrototype(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7723 | if (pt->IsNull()) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7724 | return pt->GetElementWithReceiver(receiver, index); |
| 7725 | } |
| 7726 | |
| 7727 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7728 | MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7729 | uint32_t index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7730 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7731 | // Make sure that the top context does not change when doing |
| 7732 | // callbacks or interceptor calls. |
| 7733 | AssertNoContextChange ncc; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7734 | HandleScope scope(isolate); |
| 7735 | Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate); |
| 7736 | Handle<Object> this_handle(receiver, isolate); |
| 7737 | Handle<JSObject> holder_handle(this, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7738 | if (!interceptor->getter()->IsUndefined()) { |
| 7739 | v8::IndexedPropertyGetter getter = |
| 7740 | v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7741 | LOG(isolate, |
| 7742 | ApiIndexedPropertyAccess("interceptor-indexed-get", this, index)); |
| 7743 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7744 | v8::AccessorInfo info(args.end()); |
| 7745 | v8::Handle<v8::Value> result; |
| 7746 | { |
| 7747 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7748 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7749 | result = getter(index, info); |
| 7750 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7751 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7752 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 7753 | } |
| 7754 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7755 | MaybeObject* raw_result = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7756 | holder_handle->GetElementPostInterceptor(*this_handle, index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7757 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7758 | return raw_result; |
| 7759 | } |
| 7760 | |
| 7761 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7762 | MaybeObject* JSObject::GetElementWithReceiver(Object* receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 7763 | uint32_t index) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7764 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7765 | if (IsAccessCheckNeeded()) { |
| 7766 | Heap* heap = GetHeap(); |
| 7767 | if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_GET)) { |
| 7768 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_GET); |
| 7769 | return heap->undefined_value(); |
| 7770 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7771 | } |
| 7772 | |
| 7773 | if (HasIndexedInterceptor()) { |
| 7774 | return GetElementWithInterceptor(receiver, index); |
| 7775 | } |
| 7776 | |
| 7777 | // Get element works for both JSObject and JSArray since |
| 7778 | // JSArray::length cannot change. |
| 7779 | switch (GetElementsKind()) { |
| 7780 | case FAST_ELEMENTS: { |
| 7781 | FixedArray* elms = FixedArray::cast(elements()); |
| 7782 | if (index < static_cast<uint32_t>(elms->length())) { |
| 7783 | Object* value = elms->get(index); |
| 7784 | if (!value->IsTheHole()) return value; |
| 7785 | } |
| 7786 | break; |
| 7787 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7788 | case EXTERNAL_PIXEL_ELEMENTS: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7789 | case EXTERNAL_BYTE_ELEMENTS: |
| 7790 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7791 | case EXTERNAL_SHORT_ELEMENTS: |
| 7792 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7793 | case EXTERNAL_INT_ELEMENTS: |
| 7794 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7795 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7796 | MaybeObject* maybe_value = GetExternalElement(index); |
| 7797 | Object* value; |
| 7798 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 7799 | if (!value->IsUndefined()) return value; |
| 7800 | break; |
| 7801 | } |
| 7802 | case DICTIONARY_ELEMENTS: { |
| 7803 | NumberDictionary* dictionary = element_dictionary(); |
| 7804 | int entry = dictionary->FindEntry(index); |
| 7805 | if (entry != NumberDictionary::kNotFound) { |
| 7806 | Object* element = dictionary->ValueAt(entry); |
| 7807 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 7808 | if (details.type() == CALLBACKS) { |
| 7809 | return GetElementWithCallback(receiver, |
| 7810 | element, |
| 7811 | index, |
| 7812 | this); |
| 7813 | } |
| 7814 | return element; |
| 7815 | } |
| 7816 | break; |
| 7817 | } |
| 7818 | } |
| 7819 | |
| 7820 | Object* pt = GetPrototype(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 7821 | Heap* heap = GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7822 | if (pt == heap->null_value()) return heap->undefined_value(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7823 | return pt->GetElementWithReceiver(receiver, index); |
| 7824 | } |
| 7825 | |
| 7826 | |
| 7827 | MaybeObject* JSObject::GetExternalElement(uint32_t index) { |
| 7828 | // Get element works for both JSObject and JSArray since |
| 7829 | // JSArray::length cannot change. |
| 7830 | switch (GetElementsKind()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7831 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 7832 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7833 | if (index < static_cast<uint32_t>(pixels->length())) { |
| 7834 | uint8_t value = pixels->get(index); |
| 7835 | return Smi::FromInt(value); |
| 7836 | } |
| 7837 | break; |
| 7838 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7839 | case EXTERNAL_BYTE_ELEMENTS: { |
| 7840 | ExternalByteArray* array = ExternalByteArray::cast(elements()); |
| 7841 | if (index < static_cast<uint32_t>(array->length())) { |
| 7842 | int8_t value = array->get(index); |
| 7843 | return Smi::FromInt(value); |
| 7844 | } |
| 7845 | break; |
| 7846 | } |
| 7847 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 7848 | ExternalUnsignedByteArray* array = |
| 7849 | ExternalUnsignedByteArray::cast(elements()); |
| 7850 | if (index < static_cast<uint32_t>(array->length())) { |
| 7851 | uint8_t value = array->get(index); |
| 7852 | return Smi::FromInt(value); |
| 7853 | } |
| 7854 | break; |
| 7855 | } |
| 7856 | case EXTERNAL_SHORT_ELEMENTS: { |
| 7857 | ExternalShortArray* array = ExternalShortArray::cast(elements()); |
| 7858 | if (index < static_cast<uint32_t>(array->length())) { |
| 7859 | int16_t value = array->get(index); |
| 7860 | return Smi::FromInt(value); |
| 7861 | } |
| 7862 | break; |
| 7863 | } |
| 7864 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 7865 | ExternalUnsignedShortArray* array = |
| 7866 | ExternalUnsignedShortArray::cast(elements()); |
| 7867 | if (index < static_cast<uint32_t>(array->length())) { |
| 7868 | uint16_t value = array->get(index); |
| 7869 | return Smi::FromInt(value); |
| 7870 | } |
| 7871 | break; |
| 7872 | } |
| 7873 | case EXTERNAL_INT_ELEMENTS: { |
| 7874 | ExternalIntArray* array = ExternalIntArray::cast(elements()); |
| 7875 | if (index < static_cast<uint32_t>(array->length())) { |
| 7876 | int32_t value = array->get(index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7877 | return GetHeap()->NumberFromInt32(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7878 | } |
| 7879 | break; |
| 7880 | } |
| 7881 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 7882 | ExternalUnsignedIntArray* array = |
| 7883 | ExternalUnsignedIntArray::cast(elements()); |
| 7884 | if (index < static_cast<uint32_t>(array->length())) { |
| 7885 | uint32_t value = array->get(index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7886 | return GetHeap()->NumberFromUint32(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7887 | } |
| 7888 | break; |
| 7889 | } |
| 7890 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 7891 | ExternalFloatArray* array = ExternalFloatArray::cast(elements()); |
| 7892 | if (index < static_cast<uint32_t>(array->length())) { |
| 7893 | float value = array->get(index); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7894 | return GetHeap()->AllocateHeapNumber(value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7895 | } |
| 7896 | break; |
| 7897 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 7898 | case FAST_ELEMENTS: |
| 7899 | case DICTIONARY_ELEMENTS: |
| 7900 | UNREACHABLE(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7901 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7902 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7903 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7904 | } |
| 7905 | |
| 7906 | |
| 7907 | bool JSObject::HasDenseElements() { |
| 7908 | int capacity = 0; |
| 7909 | int number_of_elements = 0; |
| 7910 | |
| 7911 | switch (GetElementsKind()) { |
| 7912 | case FAST_ELEMENTS: { |
| 7913 | FixedArray* elms = FixedArray::cast(elements()); |
| 7914 | capacity = elms->length(); |
| 7915 | for (int i = 0; i < capacity; i++) { |
| 7916 | if (!elms->get(i)->IsTheHole()) number_of_elements++; |
| 7917 | } |
| 7918 | break; |
| 7919 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 7920 | case EXTERNAL_PIXEL_ELEMENTS: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 7921 | case EXTERNAL_BYTE_ELEMENTS: |
| 7922 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 7923 | case EXTERNAL_SHORT_ELEMENTS: |
| 7924 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 7925 | case EXTERNAL_INT_ELEMENTS: |
| 7926 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 7927 | case EXTERNAL_FLOAT_ELEMENTS: { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7928 | return true; |
| 7929 | } |
| 7930 | case DICTIONARY_ELEMENTS: { |
| 7931 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 7932 | capacity = dictionary->Capacity(); |
| 7933 | number_of_elements = dictionary->NumberOfElements(); |
| 7934 | break; |
| 7935 | } |
| 7936 | default: |
| 7937 | UNREACHABLE(); |
| 7938 | break; |
| 7939 | } |
| 7940 | |
| 7941 | if (capacity == 0) return true; |
| 7942 | return (number_of_elements > (capacity / 2)); |
| 7943 | } |
| 7944 | |
| 7945 | |
| 7946 | bool JSObject::ShouldConvertToSlowElements(int new_capacity) { |
| 7947 | ASSERT(HasFastElements()); |
| 7948 | // Keep the array in fast case if the current backing storage is |
| 7949 | // almost filled and if the new capacity is no more than twice the |
| 7950 | // old capacity. |
| 7951 | int elements_length = FixedArray::cast(elements())->length(); |
| 7952 | return !HasDenseElements() || ((new_capacity / 2) > elements_length); |
| 7953 | } |
| 7954 | |
| 7955 | |
| 7956 | bool JSObject::ShouldConvertToFastElements() { |
| 7957 | ASSERT(HasDictionaryElements()); |
| 7958 | NumberDictionary* dictionary = NumberDictionary::cast(elements()); |
| 7959 | // If the elements are sparse, we should not go back to fast case. |
| 7960 | if (!HasDenseElements()) return false; |
| 7961 | // If an element has been added at a very high index in the elements |
| 7962 | // dictionary, we cannot go back to fast case. |
| 7963 | if (dictionary->requires_slow_elements()) return false; |
| 7964 | // An object requiring access checks is never allowed to have fast |
| 7965 | // elements. If it had fast elements we would skip security checks. |
| 7966 | if (IsAccessCheckNeeded()) return false; |
| 7967 | // If the dictionary backing storage takes up roughly half as much |
| 7968 | // space as a fast-case backing storage would the array should have |
| 7969 | // fast elements. |
| 7970 | uint32_t length = 0; |
| 7971 | if (IsJSArray()) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 7972 | CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7973 | } else { |
| 7974 | length = dictionary->max_number_key(); |
| 7975 | } |
| 7976 | return static_cast<uint32_t>(dictionary->Capacity()) >= |
| 7977 | (length / (2 * NumberDictionary::kEntrySize)); |
| 7978 | } |
| 7979 | |
| 7980 | |
| 7981 | // Certain compilers request function template instantiation when they |
| 7982 | // see the definition of the other template functions in the |
| 7983 | // class. This requires us to have the template functions put |
| 7984 | // together, so even though this function belongs in objects-debug.cc, |
| 7985 | // we keep it here instead to satisfy certain compilers. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7986 | #ifdef OBJECT_PRINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7987 | template<typename Shape, typename Key> |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7988 | void Dictionary<Shape, Key>::Print(FILE* out) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7989 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 7990 | for (int i = 0; i < capacity; i++) { |
| 7991 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 7992 | if (HashTable<Shape, Key>::IsKey(k)) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7993 | PrintF(out, " "); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7994 | if (k->IsString()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7995 | String::cast(k)->StringPrint(out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7996 | } else { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7997 | k->ShortPrint(out); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 7998 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 7999 | PrintF(out, ": "); |
| 8000 | ValueAt(i)->ShortPrint(out); |
| 8001 | PrintF(out, "\n"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8002 | } |
| 8003 | } |
| 8004 | } |
| 8005 | #endif |
| 8006 | |
| 8007 | |
| 8008 | template<typename Shape, typename Key> |
| 8009 | void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) { |
| 8010 | int pos = 0; |
| 8011 | int capacity = HashTable<Shape, Key>::Capacity(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8012 | AssertNoAllocation no_gc; |
| 8013 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8014 | for (int i = 0; i < capacity; i++) { |
| 8015 | Object* k = Dictionary<Shape, Key>::KeyAt(i); |
| 8016 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 8017 | elements->set(pos++, ValueAt(i), mode); |
| 8018 | } |
| 8019 | } |
| 8020 | ASSERT(pos == elements->length()); |
| 8021 | } |
| 8022 | |
| 8023 | |
| 8024 | InterceptorInfo* JSObject::GetNamedInterceptor() { |
| 8025 | ASSERT(map()->has_named_interceptor()); |
| 8026 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8027 | ASSERT(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8028 | Object* result = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8029 | constructor->shared()->get_api_func_data()->named_property_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8030 | return InterceptorInfo::cast(result); |
| 8031 | } |
| 8032 | |
| 8033 | |
| 8034 | InterceptorInfo* JSObject::GetIndexedInterceptor() { |
| 8035 | ASSERT(map()->has_indexed_interceptor()); |
| 8036 | JSFunction* constructor = JSFunction::cast(map()->constructor()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8037 | ASSERT(constructor->shared()->IsApiFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8038 | Object* result = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8039 | constructor->shared()->get_api_func_data()->indexed_property_handler(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8040 | return InterceptorInfo::cast(result); |
| 8041 | } |
| 8042 | |
| 8043 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8044 | MaybeObject* JSObject::GetPropertyPostInterceptor( |
| 8045 | JSObject* receiver, |
| 8046 | String* name, |
| 8047 | PropertyAttributes* attributes) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8048 | // Check local property in holder, ignore interceptor. |
| 8049 | LookupResult result; |
| 8050 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8051 | if (result.IsProperty()) { |
| 8052 | return GetProperty(receiver, &result, name, attributes); |
| 8053 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8054 | // Continue searching via the prototype chain. |
| 8055 | Object* pt = GetPrototype(); |
| 8056 | *attributes = ABSENT; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8057 | if (pt->IsNull()) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8058 | return pt->GetPropertyWithReceiver(receiver, name, attributes); |
| 8059 | } |
| 8060 | |
| 8061 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8062 | MaybeObject* JSObject::GetLocalPropertyPostInterceptor( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8063 | JSObject* receiver, |
| 8064 | String* name, |
| 8065 | PropertyAttributes* attributes) { |
| 8066 | // Check local property in holder, ignore interceptor. |
| 8067 | LookupResult result; |
| 8068 | LocalLookupRealNamedProperty(name, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8069 | if (result.IsProperty()) { |
| 8070 | return GetProperty(receiver, &result, name, attributes); |
| 8071 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8072 | return GetHeap()->undefined_value(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
| 8075 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8076 | MaybeObject* JSObject::GetPropertyWithInterceptor( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8077 | JSObject* receiver, |
| 8078 | String* name, |
| 8079 | PropertyAttributes* attributes) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8080 | Isolate* isolate = GetIsolate(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8081 | InterceptorInfo* interceptor = GetNamedInterceptor(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8082 | HandleScope scope(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8083 | Handle<JSObject> receiver_handle(receiver); |
| 8084 | Handle<JSObject> holder_handle(this); |
| 8085 | Handle<String> name_handle(name); |
| 8086 | |
| 8087 | if (!interceptor->getter()->IsUndefined()) { |
| 8088 | v8::NamedPropertyGetter getter = |
| 8089 | v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8090 | LOG(isolate, |
| 8091 | ApiNamedPropertyAccess("interceptor-named-get", *holder_handle, name)); |
| 8092 | CustomArguments args(isolate, interceptor->data(), receiver, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8093 | v8::AccessorInfo info(args.end()); |
| 8094 | v8::Handle<v8::Value> result; |
| 8095 | { |
| 8096 | // Leaving JavaScript. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8097 | VMState state(isolate, EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8098 | result = getter(v8::Utils::ToLocal(name_handle), info); |
| 8099 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8100 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8101 | if (!result.IsEmpty()) { |
| 8102 | *attributes = NONE; |
| 8103 | return *v8::Utils::OpenHandle(*result); |
| 8104 | } |
| 8105 | } |
| 8106 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8107 | MaybeObject* result = holder_handle->GetPropertyPostInterceptor( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8108 | *receiver_handle, |
| 8109 | *name_handle, |
| 8110 | attributes); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8111 | RETURN_IF_SCHEDULED_EXCEPTION(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8112 | return result; |
| 8113 | } |
| 8114 | |
| 8115 | |
| 8116 | bool JSObject::HasRealNamedProperty(String* key) { |
| 8117 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8118 | if (IsAccessCheckNeeded()) { |
| 8119 | Heap* heap = GetHeap(); |
| 8120 | if (!heap->isolate()->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 8121 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 8122 | return false; |
| 8123 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8124 | } |
| 8125 | |
| 8126 | LookupResult result; |
| 8127 | LocalLookupRealNamedProperty(key, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8128 | return result.IsProperty() && (result.type() != INTERCEPTOR); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8129 | } |
| 8130 | |
| 8131 | |
| 8132 | bool JSObject::HasRealElementProperty(uint32_t index) { |
| 8133 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8134 | if (IsAccessCheckNeeded()) { |
| 8135 | Heap* heap = GetHeap(); |
| 8136 | if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) { |
| 8137 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 8138 | return false; |
| 8139 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8140 | } |
| 8141 | |
| 8142 | // Handle [] on String objects. |
| 8143 | if (this->IsStringObjectWithCharacterAt(index)) return true; |
| 8144 | |
| 8145 | switch (GetElementsKind()) { |
| 8146 | case FAST_ELEMENTS: { |
| 8147 | uint32_t length = IsJSArray() ? |
| 8148 | static_cast<uint32_t>( |
| 8149 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 8150 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 8151 | return (index < length) && |
| 8152 | !FixedArray::cast(elements())->get(index)->IsTheHole(); |
| 8153 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8154 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 8155 | ExternalPixelArray* pixels = ExternalPixelArray::cast(elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8156 | return index < static_cast<uint32_t>(pixels->length()); |
| 8157 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8158 | case EXTERNAL_BYTE_ELEMENTS: |
| 8159 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 8160 | case EXTERNAL_SHORT_ELEMENTS: |
| 8161 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 8162 | case EXTERNAL_INT_ELEMENTS: |
| 8163 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 8164 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 8165 | ExternalArray* array = ExternalArray::cast(elements()); |
| 8166 | return index < static_cast<uint32_t>(array->length()); |
| 8167 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8168 | case DICTIONARY_ELEMENTS: { |
| 8169 | return element_dictionary()->FindEntry(index) |
| 8170 | != NumberDictionary::kNotFound; |
| 8171 | } |
| 8172 | default: |
| 8173 | UNREACHABLE(); |
| 8174 | break; |
| 8175 | } |
| 8176 | // All possibilities have been handled above already. |
| 8177 | UNREACHABLE(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8178 | return GetHeap()->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8179 | } |
| 8180 | |
| 8181 | |
| 8182 | bool JSObject::HasRealNamedCallbackProperty(String* key) { |
| 8183 | // Check access rights if needed. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8184 | if (IsAccessCheckNeeded()) { |
| 8185 | Heap* heap = GetHeap(); |
| 8186 | if (!heap->isolate()->MayNamedAccess(this, key, v8::ACCESS_HAS)) { |
| 8187 | heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS); |
| 8188 | return false; |
| 8189 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
| 8192 | LookupResult result; |
| 8193 | LocalLookupRealNamedProperty(key, &result); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 8194 | return result.IsProperty() && (result.type() == CALLBACKS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8195 | } |
| 8196 | |
| 8197 | |
| 8198 | int JSObject::NumberOfLocalProperties(PropertyAttributes filter) { |
| 8199 | if (HasFastProperties()) { |
| 8200 | DescriptorArray* descs = map()->instance_descriptors(); |
| 8201 | int result = 0; |
| 8202 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8203 | PropertyDetails details(descs->GetDetails(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8204 | if (details.IsProperty() && (details.attributes() & filter) == 0) { |
| 8205 | result++; |
| 8206 | } |
| 8207 | } |
| 8208 | return result; |
| 8209 | } else { |
| 8210 | return property_dictionary()->NumberOfElementsFilterAttributes(filter); |
| 8211 | } |
| 8212 | } |
| 8213 | |
| 8214 | |
| 8215 | int JSObject::NumberOfEnumProperties() { |
| 8216 | return NumberOfLocalProperties(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 8217 | } |
| 8218 | |
| 8219 | |
| 8220 | void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) { |
| 8221 | Object* temp = get(i); |
| 8222 | set(i, get(j)); |
| 8223 | set(j, temp); |
| 8224 | if (this != numbers) { |
| 8225 | temp = numbers->get(i); |
| 8226 | numbers->set(i, numbers->get(j)); |
| 8227 | numbers->set(j, temp); |
| 8228 | } |
| 8229 | } |
| 8230 | |
| 8231 | |
| 8232 | static void InsertionSortPairs(FixedArray* content, |
| 8233 | FixedArray* numbers, |
| 8234 | int len) { |
| 8235 | for (int i = 1; i < len; i++) { |
| 8236 | int j = i; |
| 8237 | while (j > 0 && |
| 8238 | (NumberToUint32(numbers->get(j - 1)) > |
| 8239 | NumberToUint32(numbers->get(j)))) { |
| 8240 | content->SwapPairs(numbers, j - 1, j); |
| 8241 | j--; |
| 8242 | } |
| 8243 | } |
| 8244 | } |
| 8245 | |
| 8246 | |
| 8247 | void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) { |
| 8248 | // In-place heap sort. |
| 8249 | ASSERT(content->length() == numbers->length()); |
| 8250 | |
| 8251 | // Bottom-up max-heap construction. |
| 8252 | for (int i = 1; i < len; ++i) { |
| 8253 | int child_index = i; |
| 8254 | while (child_index > 0) { |
| 8255 | int parent_index = ((child_index + 1) >> 1) - 1; |
| 8256 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 8257 | uint32_t child_value = NumberToUint32(numbers->get(child_index)); |
| 8258 | if (parent_value < child_value) { |
| 8259 | content->SwapPairs(numbers, parent_index, child_index); |
| 8260 | } else { |
| 8261 | break; |
| 8262 | } |
| 8263 | child_index = parent_index; |
| 8264 | } |
| 8265 | } |
| 8266 | |
| 8267 | // Extract elements and create sorted array. |
| 8268 | for (int i = len - 1; i > 0; --i) { |
| 8269 | // Put max element at the back of the array. |
| 8270 | content->SwapPairs(numbers, 0, i); |
| 8271 | // Sift down the new top element. |
| 8272 | int parent_index = 0; |
| 8273 | while (true) { |
| 8274 | int child_index = ((parent_index + 1) << 1) - 1; |
| 8275 | if (child_index >= i) break; |
| 8276 | uint32_t child1_value = NumberToUint32(numbers->get(child_index)); |
| 8277 | uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1)); |
| 8278 | uint32_t parent_value = NumberToUint32(numbers->get(parent_index)); |
| 8279 | if (child_index + 1 >= i || child1_value > child2_value) { |
| 8280 | if (parent_value > child1_value) break; |
| 8281 | content->SwapPairs(numbers, parent_index, child_index); |
| 8282 | parent_index = child_index; |
| 8283 | } else { |
| 8284 | if (parent_value > child2_value) break; |
| 8285 | content->SwapPairs(numbers, parent_index, child_index + 1); |
| 8286 | parent_index = child_index + 1; |
| 8287 | } |
| 8288 | } |
| 8289 | } |
| 8290 | } |
| 8291 | |
| 8292 | |
| 8293 | // Sort this array and the numbers as pairs wrt. the (distinct) numbers. |
| 8294 | void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) { |
| 8295 | ASSERT(this->length() == numbers->length()); |
| 8296 | // For small arrays, simply use insertion sort. |
| 8297 | if (len <= 10) { |
| 8298 | InsertionSortPairs(this, numbers, len); |
| 8299 | return; |
| 8300 | } |
| 8301 | // Check the range of indices. |
| 8302 | uint32_t min_index = NumberToUint32(numbers->get(0)); |
| 8303 | uint32_t max_index = min_index; |
| 8304 | uint32_t i; |
| 8305 | for (i = 1; i < len; i++) { |
| 8306 | if (NumberToUint32(numbers->get(i)) < min_index) { |
| 8307 | min_index = NumberToUint32(numbers->get(i)); |
| 8308 | } else if (NumberToUint32(numbers->get(i)) > max_index) { |
| 8309 | max_index = NumberToUint32(numbers->get(i)); |
| 8310 | } |
| 8311 | } |
| 8312 | if (max_index - min_index + 1 == len) { |
| 8313 | // Indices form a contiguous range, unless there are duplicates. |
| 8314 | // Do an in-place linear time sort assuming distinct numbers, but |
| 8315 | // avoid hanging in case they are not. |
| 8316 | for (i = 0; i < len; i++) { |
| 8317 | uint32_t p; |
| 8318 | uint32_t j = 0; |
| 8319 | // While the current element at i is not at its correct position p, |
| 8320 | // swap the elements at these two positions. |
| 8321 | while ((p = NumberToUint32(numbers->get(i)) - min_index) != i && |
| 8322 | j++ < len) { |
| 8323 | SwapPairs(numbers, i, p); |
| 8324 | } |
| 8325 | } |
| 8326 | } else { |
| 8327 | HeapSortPairs(this, numbers, len); |
| 8328 | return; |
| 8329 | } |
| 8330 | } |
| 8331 | |
| 8332 | |
| 8333 | // Fill in the names of local properties into the supplied storage. The main |
| 8334 | // purpose of this function is to provide reflection information for the object |
| 8335 | // mirrors. |
| 8336 | void JSObject::GetLocalPropertyNames(FixedArray* storage, int index) { |
| 8337 | ASSERT(storage->length() >= (NumberOfLocalProperties(NONE) - index)); |
| 8338 | if (HasFastProperties()) { |
| 8339 | DescriptorArray* descs = map()->instance_descriptors(); |
| 8340 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 8341 | if (descs->IsProperty(i)) storage->set(index++, descs->GetKey(i)); |
| 8342 | } |
| 8343 | ASSERT(storage->length() >= index); |
| 8344 | } else { |
| 8345 | property_dictionary()->CopyKeysTo(storage); |
| 8346 | } |
| 8347 | } |
| 8348 | |
| 8349 | |
| 8350 | int JSObject::NumberOfLocalElements(PropertyAttributes filter) { |
| 8351 | return GetLocalElementKeys(NULL, filter); |
| 8352 | } |
| 8353 | |
| 8354 | |
| 8355 | int JSObject::NumberOfEnumElements() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8356 | // Fast case for objects with no elements. |
| 8357 | if (!IsJSValue() && HasFastElements()) { |
| 8358 | uint32_t length = IsJSArray() ? |
| 8359 | static_cast<uint32_t>( |
| 8360 | Smi::cast(JSArray::cast(this)->length())->value()) : |
| 8361 | static_cast<uint32_t>(FixedArray::cast(elements())->length()); |
| 8362 | if (length == 0) return 0; |
| 8363 | } |
| 8364 | // Compute the number of enumerable elements. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8365 | return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM)); |
| 8366 | } |
| 8367 | |
| 8368 | |
| 8369 | int JSObject::GetLocalElementKeys(FixedArray* storage, |
| 8370 | PropertyAttributes filter) { |
| 8371 | int counter = 0; |
| 8372 | switch (GetElementsKind()) { |
| 8373 | case FAST_ELEMENTS: { |
| 8374 | int length = IsJSArray() ? |
| 8375 | Smi::cast(JSArray::cast(this)->length())->value() : |
| 8376 | FixedArray::cast(elements())->length(); |
| 8377 | for (int i = 0; i < length; i++) { |
| 8378 | if (!FixedArray::cast(elements())->get(i)->IsTheHole()) { |
| 8379 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8380 | storage->set(counter, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8381 | } |
| 8382 | counter++; |
| 8383 | } |
| 8384 | } |
| 8385 | ASSERT(!storage || storage->length() >= counter); |
| 8386 | break; |
| 8387 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8388 | case EXTERNAL_PIXEL_ELEMENTS: { |
| 8389 | int length = ExternalPixelArray::cast(elements())->length(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8390 | while (counter < length) { |
| 8391 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8392 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8393 | } |
| 8394 | counter++; |
| 8395 | } |
| 8396 | ASSERT(!storage || storage->length() >= counter); |
| 8397 | break; |
| 8398 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8399 | case EXTERNAL_BYTE_ELEMENTS: |
| 8400 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 8401 | case EXTERNAL_SHORT_ELEMENTS: |
| 8402 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 8403 | case EXTERNAL_INT_ELEMENTS: |
| 8404 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 8405 | case EXTERNAL_FLOAT_ELEMENTS: { |
| 8406 | int length = ExternalArray::cast(elements())->length(); |
| 8407 | while (counter < length) { |
| 8408 | if (storage != NULL) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8409 | storage->set(counter, Smi::FromInt(counter)); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8410 | } |
| 8411 | counter++; |
| 8412 | } |
| 8413 | ASSERT(!storage || storage->length() >= counter); |
| 8414 | break; |
| 8415 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8416 | case DICTIONARY_ELEMENTS: { |
| 8417 | if (storage != NULL) { |
| 8418 | element_dictionary()->CopyKeysTo(storage, filter); |
| 8419 | } |
| 8420 | counter = element_dictionary()->NumberOfElementsFilterAttributes(filter); |
| 8421 | break; |
| 8422 | } |
| 8423 | default: |
| 8424 | UNREACHABLE(); |
| 8425 | break; |
| 8426 | } |
| 8427 | |
| 8428 | if (this->IsJSValue()) { |
| 8429 | Object* val = JSValue::cast(this)->value(); |
| 8430 | if (val->IsString()) { |
| 8431 | String* str = String::cast(val); |
| 8432 | if (storage) { |
| 8433 | for (int i = 0; i < str->length(); i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8434 | storage->set(counter + i, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8435 | } |
| 8436 | } |
| 8437 | counter += str->length(); |
| 8438 | } |
| 8439 | } |
| 8440 | ASSERT(!storage || storage->length() == counter); |
| 8441 | return counter; |
| 8442 | } |
| 8443 | |
| 8444 | |
| 8445 | int JSObject::GetEnumElementKeys(FixedArray* storage) { |
| 8446 | return GetLocalElementKeys(storage, |
| 8447 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 8448 | } |
| 8449 | |
| 8450 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8451 | // StringKey simply carries a string object as key. |
| 8452 | class StringKey : public HashTableKey { |
| 8453 | public: |
| 8454 | explicit StringKey(String* string) : |
| 8455 | string_(string), |
| 8456 | hash_(HashForObject(string)) { } |
| 8457 | |
| 8458 | bool IsMatch(Object* string) { |
| 8459 | // We know that all entries in a hash table had their hash keys created. |
| 8460 | // Use that knowledge to have fast failure. |
| 8461 | if (hash_ != HashForObject(string)) { |
| 8462 | return false; |
| 8463 | } |
| 8464 | return string_->Equals(String::cast(string)); |
| 8465 | } |
| 8466 | |
| 8467 | uint32_t Hash() { return hash_; } |
| 8468 | |
| 8469 | uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); } |
| 8470 | |
| 8471 | Object* AsObject() { return string_; } |
| 8472 | |
| 8473 | String* string_; |
| 8474 | uint32_t hash_; |
| 8475 | }; |
| 8476 | |
| 8477 | |
| 8478 | // StringSharedKeys are used as keys in the eval cache. |
| 8479 | class StringSharedKey : public HashTableKey { |
| 8480 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8481 | StringSharedKey(String* source, |
| 8482 | SharedFunctionInfo* shared, |
| 8483 | StrictModeFlag strict_mode) |
| 8484 | : source_(source), |
| 8485 | shared_(shared), |
| 8486 | strict_mode_(strict_mode) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8487 | |
| 8488 | bool IsMatch(Object* other) { |
| 8489 | if (!other->IsFixedArray()) return false; |
| 8490 | FixedArray* pair = FixedArray::cast(other); |
| 8491 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(pair->get(0)); |
| 8492 | if (shared != shared_) return false; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8493 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>( |
| 8494 | Smi::cast(pair->get(2))->value()); |
| 8495 | if (strict_mode != strict_mode_) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8496 | String* source = String::cast(pair->get(1)); |
| 8497 | return source->Equals(source_); |
| 8498 | } |
| 8499 | |
| 8500 | static uint32_t StringSharedHashHelper(String* source, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8501 | SharedFunctionInfo* shared, |
| 8502 | StrictModeFlag strict_mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8503 | uint32_t hash = source->Hash(); |
| 8504 | if (shared->HasSourceCode()) { |
| 8505 | // Instead of using the SharedFunctionInfo pointer in the hash |
| 8506 | // code computation, we use a combination of the hash of the |
| 8507 | // script source code and the start and end positions. We do |
| 8508 | // this to ensure that the cache entries can survive garbage |
| 8509 | // collection. |
| 8510 | Script* script = Script::cast(shared->script()); |
| 8511 | hash ^= String::cast(script->source())->Hash(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8512 | if (strict_mode == kStrictMode) hash ^= 0x8000; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8513 | hash += shared->start_position(); |
| 8514 | } |
| 8515 | return hash; |
| 8516 | } |
| 8517 | |
| 8518 | uint32_t Hash() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8519 | return StringSharedHashHelper(source_, shared_, strict_mode_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8520 | } |
| 8521 | |
| 8522 | uint32_t HashForObject(Object* obj) { |
| 8523 | FixedArray* pair = FixedArray::cast(obj); |
| 8524 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(pair->get(0)); |
| 8525 | String* source = String::cast(pair->get(1)); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8526 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>( |
| 8527 | Smi::cast(pair->get(2))->value()); |
| 8528 | return StringSharedHashHelper(source, shared, strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8529 | } |
| 8530 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8531 | MUST_USE_RESULT MaybeObject* AsObject() { |
| 8532 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8533 | { MaybeObject* maybe_obj = source_->GetHeap()->AllocateFixedArray(3); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8534 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8535 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8536 | FixedArray* pair = FixedArray::cast(obj); |
| 8537 | pair->set(0, shared_); |
| 8538 | pair->set(1, source_); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8539 | pair->set(2, Smi::FromInt(strict_mode_)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8540 | return pair; |
| 8541 | } |
| 8542 | |
| 8543 | private: |
| 8544 | String* source_; |
| 8545 | SharedFunctionInfo* shared_; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 8546 | StrictModeFlag strict_mode_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8547 | }; |
| 8548 | |
| 8549 | |
| 8550 | // RegExpKey carries the source and flags of a regular expression as key. |
| 8551 | class RegExpKey : public HashTableKey { |
| 8552 | public: |
| 8553 | RegExpKey(String* string, JSRegExp::Flags flags) |
| 8554 | : string_(string), |
| 8555 | flags_(Smi::FromInt(flags.value())) { } |
| 8556 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 8557 | // Rather than storing the key in the hash table, a pointer to the |
| 8558 | // stored value is stored where the key should be. IsMatch then |
| 8559 | // compares the search key to the found object, rather than comparing |
| 8560 | // a key to a key. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8561 | bool IsMatch(Object* obj) { |
| 8562 | FixedArray* val = FixedArray::cast(obj); |
| 8563 | return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex))) |
| 8564 | && (flags_ == val->get(JSRegExp::kFlagsIndex)); |
| 8565 | } |
| 8566 | |
| 8567 | uint32_t Hash() { return RegExpHash(string_, flags_); } |
| 8568 | |
| 8569 | Object* AsObject() { |
| 8570 | // Plain hash maps, which is where regexp keys are used, don't |
| 8571 | // use this function. |
| 8572 | UNREACHABLE(); |
| 8573 | return NULL; |
| 8574 | } |
| 8575 | |
| 8576 | uint32_t HashForObject(Object* obj) { |
| 8577 | FixedArray* val = FixedArray::cast(obj); |
| 8578 | return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)), |
| 8579 | Smi::cast(val->get(JSRegExp::kFlagsIndex))); |
| 8580 | } |
| 8581 | |
| 8582 | static uint32_t RegExpHash(String* string, Smi* flags) { |
| 8583 | return string->Hash() + flags->value(); |
| 8584 | } |
| 8585 | |
| 8586 | String* string_; |
| 8587 | Smi* flags_; |
| 8588 | }; |
| 8589 | |
| 8590 | // Utf8SymbolKey carries a vector of chars as key. |
| 8591 | class Utf8SymbolKey : public HashTableKey { |
| 8592 | public: |
| 8593 | explicit Utf8SymbolKey(Vector<const char> string) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8594 | : string_(string), hash_field_(0) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8595 | |
| 8596 | bool IsMatch(Object* string) { |
| 8597 | return String::cast(string)->IsEqualTo(string_); |
| 8598 | } |
| 8599 | |
| 8600 | uint32_t Hash() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8601 | if (hash_field_ != 0) return hash_field_ >> String::kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8602 | unibrow::Utf8InputBuffer<> buffer(string_.start(), |
| 8603 | static_cast<unsigned>(string_.length())); |
| 8604 | chars_ = buffer.Length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8605 | hash_field_ = String::ComputeHashField(&buffer, chars_); |
| 8606 | uint32_t result = hash_field_ >> String::kHashShift; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8607 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 8608 | return result; |
| 8609 | } |
| 8610 | |
| 8611 | uint32_t HashForObject(Object* other) { |
| 8612 | return String::cast(other)->Hash(); |
| 8613 | } |
| 8614 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8615 | MaybeObject* AsObject() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8616 | if (hash_field_ == 0) Hash(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8617 | return Isolate::Current()->heap()->AllocateSymbol( |
| 8618 | string_, chars_, hash_field_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8619 | } |
| 8620 | |
| 8621 | Vector<const char> string_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8622 | uint32_t hash_field_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8623 | int chars_; // Caches the number of characters when computing the hash code. |
| 8624 | }; |
| 8625 | |
| 8626 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 8627 | template <typename Char> |
| 8628 | class SequentialSymbolKey : public HashTableKey { |
| 8629 | public: |
| 8630 | explicit SequentialSymbolKey(Vector<const Char> string) |
| 8631 | : string_(string), hash_field_(0) { } |
| 8632 | |
| 8633 | uint32_t Hash() { |
| 8634 | StringHasher hasher(string_.length()); |
| 8635 | |
| 8636 | // Very long strings have a trivial hash that doesn't inspect the |
| 8637 | // string contents. |
| 8638 | if (hasher.has_trivial_hash()) { |
| 8639 | hash_field_ = hasher.GetHashField(); |
| 8640 | } else { |
| 8641 | int i = 0; |
| 8642 | // Do the iterative array index computation as long as there is a |
| 8643 | // chance this is an array index. |
| 8644 | while (i < string_.length() && hasher.is_array_index()) { |
| 8645 | hasher.AddCharacter(static_cast<uc32>(string_[i])); |
| 8646 | i++; |
| 8647 | } |
| 8648 | |
| 8649 | // Process the remaining characters without updating the array |
| 8650 | // index. |
| 8651 | while (i < string_.length()) { |
| 8652 | hasher.AddCharacterNoIndex(static_cast<uc32>(string_[i])); |
| 8653 | i++; |
| 8654 | } |
| 8655 | hash_field_ = hasher.GetHashField(); |
| 8656 | } |
| 8657 | |
| 8658 | uint32_t result = hash_field_ >> String::kHashShift; |
| 8659 | ASSERT(result != 0); // Ensure that the hash value of 0 is never computed. |
| 8660 | return result; |
| 8661 | } |
| 8662 | |
| 8663 | |
| 8664 | uint32_t HashForObject(Object* other) { |
| 8665 | return String::cast(other)->Hash(); |
| 8666 | } |
| 8667 | |
| 8668 | Vector<const Char> string_; |
| 8669 | uint32_t hash_field_; |
| 8670 | }; |
| 8671 | |
| 8672 | |
| 8673 | |
| 8674 | class AsciiSymbolKey : public SequentialSymbolKey<char> { |
| 8675 | public: |
| 8676 | explicit AsciiSymbolKey(Vector<const char> str) |
| 8677 | : SequentialSymbolKey<char>(str) { } |
| 8678 | |
| 8679 | bool IsMatch(Object* string) { |
| 8680 | return String::cast(string)->IsAsciiEqualTo(string_); |
| 8681 | } |
| 8682 | |
| 8683 | MaybeObject* AsObject() { |
| 8684 | if (hash_field_ == 0) Hash(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8685 | return HEAP->AllocateAsciiSymbol(string_, hash_field_); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 8686 | } |
| 8687 | }; |
| 8688 | |
| 8689 | |
| 8690 | class TwoByteSymbolKey : public SequentialSymbolKey<uc16> { |
| 8691 | public: |
| 8692 | explicit TwoByteSymbolKey(Vector<const uc16> str) |
| 8693 | : SequentialSymbolKey<uc16>(str) { } |
| 8694 | |
| 8695 | bool IsMatch(Object* string) { |
| 8696 | return String::cast(string)->IsTwoByteEqualTo(string_); |
| 8697 | } |
| 8698 | |
| 8699 | MaybeObject* AsObject() { |
| 8700 | if (hash_field_ == 0) Hash(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8701 | return HEAP->AllocateTwoByteSymbol(string_, hash_field_); |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 8702 | } |
| 8703 | }; |
| 8704 | |
| 8705 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8706 | // SymbolKey carries a string/symbol object as key. |
| 8707 | class SymbolKey : public HashTableKey { |
| 8708 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8709 | explicit SymbolKey(String* string) |
| 8710 | : string_(string) { } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8711 | |
| 8712 | bool IsMatch(Object* string) { |
| 8713 | return String::cast(string)->Equals(string_); |
| 8714 | } |
| 8715 | |
| 8716 | uint32_t Hash() { return string_->Hash(); } |
| 8717 | |
| 8718 | uint32_t HashForObject(Object* other) { |
| 8719 | return String::cast(other)->Hash(); |
| 8720 | } |
| 8721 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8722 | MaybeObject* AsObject() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 8723 | // Attempt to flatten the string, so that symbols will most often |
| 8724 | // be flat strings. |
| 8725 | string_ = string_->TryFlattenGetString(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8726 | Heap* heap = string_->GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8727 | // Transform string to symbol if possible. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8728 | Map* map = heap->SymbolMapForString(string_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8729 | if (map != NULL) { |
| 8730 | string_->set_map(map); |
| 8731 | ASSERT(string_->IsSymbol()); |
| 8732 | return string_; |
| 8733 | } |
| 8734 | // Otherwise allocate a new symbol. |
| 8735 | StringInputBuffer buffer(string_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8736 | return heap->AllocateInternalSymbol(&buffer, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8737 | string_->length(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 8738 | string_->hash_field()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8739 | } |
| 8740 | |
| 8741 | static uint32_t StringHash(Object* obj) { |
| 8742 | return String::cast(obj)->Hash(); |
| 8743 | } |
| 8744 | |
| 8745 | String* string_; |
| 8746 | }; |
| 8747 | |
| 8748 | |
| 8749 | template<typename Shape, typename Key> |
| 8750 | void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) { |
| 8751 | IteratePointers(v, 0, kElementsStartOffset); |
| 8752 | } |
| 8753 | |
| 8754 | |
| 8755 | template<typename Shape, typename Key> |
| 8756 | void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) { |
| 8757 | IteratePointers(v, |
| 8758 | kElementsStartOffset, |
| 8759 | kHeaderSize + length() * kPointerSize); |
| 8760 | } |
| 8761 | |
| 8762 | |
| 8763 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8764 | MaybeObject* HashTable<Shape, Key>::Allocate(int at_least_space_for, |
| 8765 | PretenureFlag pretenure) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8766 | const int kMinCapacity = 32; |
| 8767 | int capacity = RoundUpToPowerOf2(at_least_space_for * 2); |
| 8768 | if (capacity < kMinCapacity) { |
| 8769 | capacity = kMinCapacity; // Guarantee min capacity. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8770 | } else if (capacity > HashTable::kMaxCapacity) { |
| 8771 | return Failure::OutOfMemoryException(); |
| 8772 | } |
| 8773 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8774 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 8775 | { MaybeObject* maybe_obj = Isolate::Current()->heap()-> |
| 8776 | AllocateHashTable(EntryToIndex(capacity), pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8777 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8778 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8779 | HashTable::cast(obj)->SetNumberOfElements(0); |
| 8780 | HashTable::cast(obj)->SetNumberOfDeletedElements(0); |
| 8781 | HashTable::cast(obj)->SetCapacity(capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8782 | return obj; |
| 8783 | } |
| 8784 | |
| 8785 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8786 | // Find entry for key otherwise return kNotFound. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 8787 | int StringDictionary::FindEntry(String* key) { |
| 8788 | if (!key->IsSymbol()) { |
| 8789 | return HashTable<StringDictionaryShape, String*>::FindEntry(key); |
| 8790 | } |
| 8791 | |
| 8792 | // Optimized for symbol key. Knowledge of the key type allows: |
| 8793 | // 1. Move the check if the key is a symbol out of the loop. |
| 8794 | // 2. Avoid comparing hash codes in symbol to symbol comparision. |
| 8795 | // 3. Detect a case when a dictionary key is not a symbol but the key is. |
| 8796 | // In case of positive result the dictionary key may be replaced by |
| 8797 | // the symbol with minimal performance penalty. It gives a chance to |
| 8798 | // perform further lookups in code stubs (and significant performance boost |
| 8799 | // a certain style of code). |
| 8800 | |
| 8801 | // EnsureCapacity will guarantee the hash table is never full. |
| 8802 | uint32_t capacity = Capacity(); |
| 8803 | uint32_t entry = FirstProbe(key->Hash(), capacity); |
| 8804 | uint32_t count = 1; |
| 8805 | |
| 8806 | while (true) { |
| 8807 | int index = EntryToIndex(entry); |
| 8808 | Object* element = get(index); |
| 8809 | if (element->IsUndefined()) break; // Empty entry. |
| 8810 | if (key == element) return entry; |
| 8811 | if (!element->IsSymbol() && |
| 8812 | !element->IsNull() && |
| 8813 | String::cast(element)->Equals(key)) { |
| 8814 | // Replace a non-symbol key by the equivalent symbol for faster further |
| 8815 | // lookups. |
| 8816 | set(index, key); |
| 8817 | return entry; |
| 8818 | } |
| 8819 | ASSERT(element->IsNull() || !String::cast(element)->Equals(key)); |
| 8820 | entry = NextProbe(entry, count++, capacity); |
| 8821 | } |
| 8822 | return kNotFound; |
| 8823 | } |
| 8824 | |
| 8825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8826 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8827 | MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, Key key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8828 | int capacity = Capacity(); |
| 8829 | int nof = NumberOfElements() + n; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8830 | int nod = NumberOfDeletedElements(); |
| 8831 | // Return if: |
| 8832 | // 50% is still free after adding n elements and |
| 8833 | // at most 50% of the free elements are deleted elements. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8834 | if (nod <= (capacity - nof) >> 1) { |
| 8835 | int needed_free = nof >> 1; |
| 8836 | if (nof + needed_free <= capacity) return this; |
| 8837 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8838 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8839 | const int kMinCapacityForPretenure = 256; |
| 8840 | bool pretenure = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8841 | (capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8842 | Object* obj; |
| 8843 | { MaybeObject* maybe_obj = |
| 8844 | Allocate(nof * 2, pretenure ? TENURED : NOT_TENURED); |
| 8845 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8846 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8847 | |
| 8848 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8849 | HashTable* table = HashTable::cast(obj); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 8850 | WriteBarrierMode mode = table->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8851 | |
| 8852 | // Copy prefix to new array. |
| 8853 | for (int i = kPrefixStartIndex; |
| 8854 | i < kPrefixStartIndex + Shape::kPrefixSize; |
| 8855 | i++) { |
| 8856 | table->set(i, get(i), mode); |
| 8857 | } |
| 8858 | // Rehash the elements. |
| 8859 | for (int i = 0; i < capacity; i++) { |
| 8860 | uint32_t from_index = EntryToIndex(i); |
| 8861 | Object* k = get(from_index); |
| 8862 | if (IsKey(k)) { |
| 8863 | uint32_t hash = Shape::HashForObject(key, k); |
| 8864 | uint32_t insertion_index = |
| 8865 | EntryToIndex(table->FindInsertionEntry(hash)); |
| 8866 | for (int j = 0; j < Shape::kEntrySize; j++) { |
| 8867 | table->set(insertion_index + j, get(from_index + j), mode); |
| 8868 | } |
| 8869 | } |
| 8870 | } |
| 8871 | table->SetNumberOfElements(NumberOfElements()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8872 | table->SetNumberOfDeletedElements(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8873 | return table; |
| 8874 | } |
| 8875 | |
| 8876 | |
| 8877 | template<typename Shape, typename Key> |
| 8878 | uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) { |
| 8879 | uint32_t capacity = Capacity(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8880 | uint32_t entry = FirstProbe(hash, capacity); |
| 8881 | uint32_t count = 1; |
| 8882 | // EnsureCapacity will guarantee the hash table is never full. |
| 8883 | while (true) { |
| 8884 | Object* element = KeyAt(entry); |
| 8885 | if (element->IsUndefined() || element->IsNull()) break; |
| 8886 | entry = NextProbe(entry, count++, capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8887 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8888 | return entry; |
| 8889 | } |
| 8890 | |
| 8891 | // Force instantiation of template instances class. |
| 8892 | // Please note this list is compiler dependent. |
| 8893 | |
| 8894 | template class HashTable<SymbolTableShape, HashTableKey*>; |
| 8895 | |
| 8896 | template class HashTable<CompilationCacheShape, HashTableKey*>; |
| 8897 | |
| 8898 | template class HashTable<MapCacheShape, HashTableKey*>; |
| 8899 | |
| 8900 | template class Dictionary<StringDictionaryShape, String*>; |
| 8901 | |
| 8902 | template class Dictionary<NumberDictionaryShape, uint32_t>; |
| 8903 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8904 | template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::Allocate( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8905 | int); |
| 8906 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8907 | template MaybeObject* Dictionary<StringDictionaryShape, String*>::Allocate( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8908 | int); |
| 8909 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8910 | template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::AtPut( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8911 | uint32_t, Object*); |
| 8912 | |
| 8913 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::SlowReverseLookup( |
| 8914 | Object*); |
| 8915 | |
| 8916 | template Object* Dictionary<StringDictionaryShape, String*>::SlowReverseLookup( |
| 8917 | Object*); |
| 8918 | |
| 8919 | template void Dictionary<NumberDictionaryShape, uint32_t>::CopyKeysTo( |
| 8920 | FixedArray*, PropertyAttributes); |
| 8921 | |
| 8922 | template Object* Dictionary<StringDictionaryShape, String*>::DeleteProperty( |
| 8923 | int, JSObject::DeleteMode); |
| 8924 | |
| 8925 | template Object* Dictionary<NumberDictionaryShape, uint32_t>::DeleteProperty( |
| 8926 | int, JSObject::DeleteMode); |
| 8927 | |
| 8928 | template void Dictionary<StringDictionaryShape, String*>::CopyKeysTo( |
| 8929 | FixedArray*); |
| 8930 | |
| 8931 | template int |
| 8932 | Dictionary<StringDictionaryShape, String*>::NumberOfElementsFilterAttributes( |
| 8933 | PropertyAttributes); |
| 8934 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8935 | template MaybeObject* Dictionary<StringDictionaryShape, String*>::Add( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8936 | String*, Object*, PropertyDetails); |
| 8937 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8938 | template MaybeObject* |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8939 | Dictionary<StringDictionaryShape, String*>::GenerateNewEnumerationIndices(); |
| 8940 | |
| 8941 | template int |
| 8942 | Dictionary<NumberDictionaryShape, uint32_t>::NumberOfElementsFilterAttributes( |
| 8943 | PropertyAttributes); |
| 8944 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8945 | template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::Add( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8946 | uint32_t, Object*, PropertyDetails); |
| 8947 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8948 | template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>:: |
| 8949 | EnsureCapacity(int, uint32_t); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8950 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8951 | template MaybeObject* Dictionary<StringDictionaryShape, String*>:: |
| 8952 | EnsureCapacity(int, String*); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8953 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8954 | template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::AddEntry( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8955 | uint32_t, Object*, PropertyDetails, uint32_t); |
| 8956 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8957 | template MaybeObject* Dictionary<StringDictionaryShape, String*>::AddEntry( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8958 | String*, Object*, PropertyDetails, uint32_t); |
| 8959 | |
| 8960 | template |
| 8961 | int Dictionary<NumberDictionaryShape, uint32_t>::NumberOfEnumElements(); |
| 8962 | |
| 8963 | template |
| 8964 | int Dictionary<StringDictionaryShape, String*>::NumberOfEnumElements(); |
| 8965 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 8966 | template |
| 8967 | int HashTable<NumberDictionaryShape, uint32_t>::FindEntry(uint32_t); |
| 8968 | |
| 8969 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8970 | // Collates undefined and unexisting elements below limit from position |
| 8971 | // zero of the elements. The object stays in Dictionary mode. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8972 | MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8973 | ASSERT(HasDictionaryElements()); |
| 8974 | // Must stay in dictionary mode, either because of requires_slow_elements, |
| 8975 | // or because we are not going to sort (and therefore compact) all of the |
| 8976 | // elements. |
| 8977 | NumberDictionary* dict = element_dictionary(); |
| 8978 | HeapNumber* result_double = NULL; |
| 8979 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 8980 | // Allocate space for result before we start mutating the object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8981 | Object* new_double; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 8982 | { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8983 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 8984 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8985 | result_double = HeapNumber::cast(new_double); |
| 8986 | } |
| 8987 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 8988 | Object* obj; |
| 8989 | { MaybeObject* maybe_obj = |
| 8990 | NumberDictionary::Allocate(dict->NumberOfElements()); |
| 8991 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8992 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8993 | NumberDictionary* new_dict = NumberDictionary::cast(obj); |
| 8994 | |
| 8995 | AssertNoAllocation no_alloc; |
| 8996 | |
| 8997 | uint32_t pos = 0; |
| 8998 | uint32_t undefs = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8999 | int capacity = dict->Capacity(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9000 | for (int i = 0; i < capacity; i++) { |
| 9001 | Object* k = dict->KeyAt(i); |
| 9002 | if (dict->IsKey(k)) { |
| 9003 | ASSERT(k->IsNumber()); |
| 9004 | ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0); |
| 9005 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0); |
| 9006 | ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32); |
| 9007 | Object* value = dict->ValueAt(i); |
| 9008 | PropertyDetails details = dict->DetailsAt(i); |
| 9009 | if (details.type() == CALLBACKS) { |
| 9010 | // Bail out and do the sorting of undefineds and array holes in JS. |
| 9011 | return Smi::FromInt(-1); |
| 9012 | } |
| 9013 | uint32_t key = NumberToUint32(k); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9014 | // In the following we assert that adding the entry to the new dictionary |
| 9015 | // does not cause GC. This is the case because we made sure to allocate |
| 9016 | // the dictionary big enough above, so it need not grow. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9017 | if (key < limit) { |
| 9018 | if (value->IsUndefined()) { |
| 9019 | undefs++; |
| 9020 | } else { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9021 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9022 | // Adding an entry with the key beyond smi-range requires |
| 9023 | // allocation. Bailout. |
| 9024 | return Smi::FromInt(-1); |
| 9025 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9026 | new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9027 | pos++; |
| 9028 | } |
| 9029 | } else { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9030 | if (key > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9031 | // Adding an entry with the key beyond smi-range requires |
| 9032 | // allocation. Bailout. |
| 9033 | return Smi::FromInt(-1); |
| 9034 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9035 | new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9036 | } |
| 9037 | } |
| 9038 | } |
| 9039 | |
| 9040 | uint32_t result = pos; |
| 9041 | PropertyDetails no_details = PropertyDetails(NONE, NORMAL); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9042 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9043 | while (undefs > 0) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9044 | if (pos > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9045 | // Adding an entry with the key beyond smi-range requires |
| 9046 | // allocation. Bailout. |
| 9047 | return Smi::FromInt(-1); |
| 9048 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9049 | new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)-> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9050 | ToObjectUnchecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9051 | pos++; |
| 9052 | undefs--; |
| 9053 | } |
| 9054 | |
| 9055 | set_elements(new_dict); |
| 9056 | |
| 9057 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9058 | return Smi::FromInt(static_cast<int>(result)); |
| 9059 | } |
| 9060 | |
| 9061 | ASSERT_NE(NULL, result_double); |
| 9062 | result_double->set_value(static_cast<double>(result)); |
| 9063 | return result_double; |
| 9064 | } |
| 9065 | |
| 9066 | |
| 9067 | // Collects all defined (non-hole) and non-undefined (array) elements at |
| 9068 | // the start of the elements array. |
| 9069 | // If the object is in dictionary mode, it is converted to fast elements |
| 9070 | // mode. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9071 | MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9072 | ASSERT(!HasExternalArrayElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9073 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9074 | Heap* heap = GetHeap(); |
| 9075 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9076 | if (HasDictionaryElements()) { |
| 9077 | // Convert to fast elements containing only the existing properties. |
| 9078 | // Ordering is irrelevant, since we are going to sort anyway. |
| 9079 | NumberDictionary* dict = element_dictionary(); |
| 9080 | if (IsJSArray() || dict->requires_slow_elements() || |
| 9081 | dict->max_number_key() >= limit) { |
| 9082 | return PrepareSlowElementsForSort(limit); |
| 9083 | } |
| 9084 | // Convert to fast elements. |
| 9085 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9086 | Object* obj; |
| 9087 | { MaybeObject* maybe_obj = map()->GetFastElementsMap(); |
| 9088 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9089 | } |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9090 | Map* new_map = Map::cast(obj); |
| 9091 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9092 | PretenureFlag tenure = heap->InNewSpace(this) ? NOT_TENURED: TENURED; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9093 | Object* new_array; |
| 9094 | { MaybeObject* maybe_new_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9095 | heap->AllocateFixedArray(dict->NumberOfElements(), tenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9096 | if (!maybe_new_array->ToObject(&new_array)) return maybe_new_array; |
| 9097 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9098 | FixedArray* fast_elements = FixedArray::cast(new_array); |
| 9099 | dict->CopyValuesTo(fast_elements); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 9100 | |
| 9101 | set_map(new_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9102 | set_elements(fast_elements); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 9103 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9104 | Object* obj; |
| 9105 | { MaybeObject* maybe_obj = EnsureWritableFastElements(); |
| 9106 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9107 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9108 | } |
| 9109 | ASSERT(HasFastElements()); |
| 9110 | |
| 9111 | // Collect holes at the end, undefined before that and the rest at the |
| 9112 | // start, and return the number of non-hole, non-undefined values. |
| 9113 | |
| 9114 | FixedArray* elements = FixedArray::cast(this->elements()); |
| 9115 | uint32_t elements_length = static_cast<uint32_t>(elements->length()); |
| 9116 | if (limit > elements_length) { |
| 9117 | limit = elements_length ; |
| 9118 | } |
| 9119 | if (limit == 0) { |
| 9120 | return Smi::FromInt(0); |
| 9121 | } |
| 9122 | |
| 9123 | HeapNumber* result_double = NULL; |
| 9124 | if (limit > static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9125 | // Pessimistically allocate space for return value before |
| 9126 | // we start mutating the array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9127 | Object* new_double; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9128 | { MaybeObject* maybe_new_double = heap->AllocateHeapNumber(0.0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9129 | if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double; |
| 9130 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9131 | result_double = HeapNumber::cast(new_double); |
| 9132 | } |
| 9133 | |
| 9134 | AssertNoAllocation no_alloc; |
| 9135 | |
| 9136 | // Split elements into defined, undefined and the_hole, in that order. |
| 9137 | // Only count locations for undefined and the hole, and fill them afterwards. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9138 | WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9139 | unsigned int undefs = limit; |
| 9140 | unsigned int holes = limit; |
| 9141 | // Assume most arrays contain no holes and undefined values, so minimize the |
| 9142 | // number of stores of non-undefined, non-the-hole values. |
| 9143 | for (unsigned int i = 0; i < undefs; i++) { |
| 9144 | Object* current = elements->get(i); |
| 9145 | if (current->IsTheHole()) { |
| 9146 | holes--; |
| 9147 | undefs--; |
| 9148 | } else if (current->IsUndefined()) { |
| 9149 | undefs--; |
| 9150 | } else { |
| 9151 | continue; |
| 9152 | } |
| 9153 | // Position i needs to be filled. |
| 9154 | while (undefs > i) { |
| 9155 | current = elements->get(undefs); |
| 9156 | if (current->IsTheHole()) { |
| 9157 | holes--; |
| 9158 | undefs--; |
| 9159 | } else if (current->IsUndefined()) { |
| 9160 | undefs--; |
| 9161 | } else { |
| 9162 | elements->set(i, current, write_barrier); |
| 9163 | break; |
| 9164 | } |
| 9165 | } |
| 9166 | } |
| 9167 | uint32_t result = undefs; |
| 9168 | while (undefs < holes) { |
| 9169 | elements->set_undefined(undefs); |
| 9170 | undefs++; |
| 9171 | } |
| 9172 | while (holes < limit) { |
| 9173 | elements->set_the_hole(holes); |
| 9174 | holes++; |
| 9175 | } |
| 9176 | |
| 9177 | if (result <= static_cast<uint32_t>(Smi::kMaxValue)) { |
| 9178 | return Smi::FromInt(static_cast<int>(result)); |
| 9179 | } |
| 9180 | ASSERT_NE(NULL, result_double); |
| 9181 | result_double->set_value(static_cast<double>(result)); |
| 9182 | return result_double; |
| 9183 | } |
| 9184 | |
| 9185 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9186 | Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9187 | uint8_t clamped_value = 0; |
| 9188 | if (index < static_cast<uint32_t>(length())) { |
| 9189 | if (value->IsSmi()) { |
| 9190 | int int_value = Smi::cast(value)->value(); |
| 9191 | if (int_value < 0) { |
| 9192 | clamped_value = 0; |
| 9193 | } else if (int_value > 255) { |
| 9194 | clamped_value = 255; |
| 9195 | } else { |
| 9196 | clamped_value = static_cast<uint8_t>(int_value); |
| 9197 | } |
| 9198 | } else if (value->IsHeapNumber()) { |
| 9199 | double double_value = HeapNumber::cast(value)->value(); |
| 9200 | if (!(double_value > 0)) { |
| 9201 | // NaN and less than zero clamp to zero. |
| 9202 | clamped_value = 0; |
| 9203 | } else if (double_value > 255) { |
| 9204 | // Greater than 255 clamp to 255. |
| 9205 | clamped_value = 255; |
| 9206 | } else { |
| 9207 | // Other doubles are rounded to the nearest integer. |
| 9208 | clamped_value = static_cast<uint8_t>(double_value + 0.5); |
| 9209 | } |
| 9210 | } else { |
| 9211 | // Clamp undefined to zero (default). All other types have been |
| 9212 | // converted to a number type further up in the call chain. |
| 9213 | ASSERT(value->IsUndefined()); |
| 9214 | } |
| 9215 | set(index, clamped_value); |
| 9216 | } |
| 9217 | return Smi::FromInt(clamped_value); |
| 9218 | } |
| 9219 | |
| 9220 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9221 | template<typename ExternalArrayClass, typename ValueType> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9222 | static MaybeObject* ExternalArrayIntSetter(Heap* heap, |
| 9223 | ExternalArrayClass* receiver, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9224 | uint32_t index, |
| 9225 | Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9226 | ValueType cast_value = 0; |
| 9227 | if (index < static_cast<uint32_t>(receiver->length())) { |
| 9228 | if (value->IsSmi()) { |
| 9229 | int int_value = Smi::cast(value)->value(); |
| 9230 | cast_value = static_cast<ValueType>(int_value); |
| 9231 | } else if (value->IsHeapNumber()) { |
| 9232 | double double_value = HeapNumber::cast(value)->value(); |
| 9233 | cast_value = static_cast<ValueType>(DoubleToInt32(double_value)); |
| 9234 | } else { |
| 9235 | // Clamp undefined to zero (default). All other types have been |
| 9236 | // converted to a number type further up in the call chain. |
| 9237 | ASSERT(value->IsUndefined()); |
| 9238 | } |
| 9239 | receiver->set(index, cast_value); |
| 9240 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9241 | return heap->NumberFromInt32(cast_value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9242 | } |
| 9243 | |
| 9244 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9245 | MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9246 | return ExternalArrayIntSetter<ExternalByteArray, int8_t> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9247 | (GetHeap(), this, index, value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9248 | } |
| 9249 | |
| 9250 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9251 | MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index, |
| 9252 | Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9253 | return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9254 | (GetHeap(), this, index, value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9255 | } |
| 9256 | |
| 9257 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9258 | MaybeObject* ExternalShortArray::SetValue(uint32_t index, |
| 9259 | Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9260 | return ExternalArrayIntSetter<ExternalShortArray, int16_t> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9261 | (GetHeap(), this, index, value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9262 | } |
| 9263 | |
| 9264 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9265 | MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index, |
| 9266 | Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9267 | return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9268 | (GetHeap(), this, index, value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9269 | } |
| 9270 | |
| 9271 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9272 | MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9273 | return ExternalArrayIntSetter<ExternalIntArray, int32_t> |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9274 | (GetHeap(), this, index, value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9275 | } |
| 9276 | |
| 9277 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9278 | MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9279 | uint32_t cast_value = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9280 | Heap* heap = GetHeap(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9281 | if (index < static_cast<uint32_t>(length())) { |
| 9282 | if (value->IsSmi()) { |
| 9283 | int int_value = Smi::cast(value)->value(); |
| 9284 | cast_value = static_cast<uint32_t>(int_value); |
| 9285 | } else if (value->IsHeapNumber()) { |
| 9286 | double double_value = HeapNumber::cast(value)->value(); |
| 9287 | cast_value = static_cast<uint32_t>(DoubleToUint32(double_value)); |
| 9288 | } else { |
| 9289 | // Clamp undefined to zero (default). All other types have been |
| 9290 | // converted to a number type further up in the call chain. |
| 9291 | ASSERT(value->IsUndefined()); |
| 9292 | } |
| 9293 | set(index, cast_value); |
| 9294 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9295 | return heap->NumberFromUint32(cast_value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9299 | MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9300 | float cast_value = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9301 | Heap* heap = GetHeap(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9302 | if (index < static_cast<uint32_t>(length())) { |
| 9303 | if (value->IsSmi()) { |
| 9304 | int int_value = Smi::cast(value)->value(); |
| 9305 | cast_value = static_cast<float>(int_value); |
| 9306 | } else if (value->IsHeapNumber()) { |
| 9307 | double double_value = HeapNumber::cast(value)->value(); |
| 9308 | cast_value = static_cast<float>(double_value); |
| 9309 | } else { |
| 9310 | // Clamp undefined to zero (default). All other types have been |
| 9311 | // converted to a number type further up in the call chain. |
| 9312 | ASSERT(value->IsUndefined()); |
| 9313 | } |
| 9314 | set(index, cast_value); |
| 9315 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9316 | return heap->AllocateHeapNumber(cast_value); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9317 | } |
| 9318 | |
| 9319 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9320 | JSGlobalPropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9321 | ASSERT(!HasFastProperties()); |
| 9322 | Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9323 | return JSGlobalPropertyCell::cast(value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9324 | } |
| 9325 | |
| 9326 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9327 | MaybeObject* GlobalObject::EnsurePropertyCell(String* name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9328 | ASSERT(!HasFastProperties()); |
| 9329 | int entry = property_dictionary()->FindEntry(name); |
| 9330 | if (entry == StringDictionary::kNotFound) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9331 | Heap* heap = GetHeap(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9332 | Object* cell; |
| 9333 | { MaybeObject* maybe_cell = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9334 | heap->AllocateJSGlobalPropertyCell(heap->the_hole_value()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9335 | if (!maybe_cell->ToObject(&cell)) return maybe_cell; |
| 9336 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9337 | PropertyDetails details(NONE, NORMAL); |
| 9338 | details = details.AsDeleted(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9339 | Object* dictionary; |
| 9340 | { MaybeObject* maybe_dictionary = |
| 9341 | property_dictionary()->Add(name, cell, details); |
| 9342 | if (!maybe_dictionary->ToObject(&dictionary)) return maybe_dictionary; |
| 9343 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9344 | set_properties(StringDictionary::cast(dictionary)); |
| 9345 | return cell; |
| 9346 | } else { |
| 9347 | Object* value = property_dictionary()->ValueAt(entry); |
| 9348 | ASSERT(value->IsJSGlobalPropertyCell()); |
| 9349 | return value; |
| 9350 | } |
| 9351 | } |
| 9352 | |
| 9353 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9354 | MaybeObject* SymbolTable::LookupString(String* string, Object** s) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9355 | SymbolKey key(string); |
| 9356 | return LookupKey(&key, s); |
| 9357 | } |
| 9358 | |
| 9359 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9360 | // This class is used for looking up two character strings in the symbol table. |
| 9361 | // If we don't have a hit we don't want to waste much time so we unroll the |
| 9362 | // string hash calculation loop here for speed. Doesn't work if the two |
| 9363 | // characters form a decimal integer, since such strings have a different hash |
| 9364 | // algorithm. |
| 9365 | class TwoCharHashTableKey : public HashTableKey { |
| 9366 | public: |
| 9367 | TwoCharHashTableKey(uint32_t c1, uint32_t c2) |
| 9368 | : c1_(c1), c2_(c2) { |
| 9369 | // Char 1. |
| 9370 | uint32_t hash = c1 + (c1 << 10); |
| 9371 | hash ^= hash >> 6; |
| 9372 | // Char 2. |
| 9373 | hash += c2; |
| 9374 | hash += hash << 10; |
| 9375 | hash ^= hash >> 6; |
| 9376 | // GetHash. |
| 9377 | hash += hash << 3; |
| 9378 | hash ^= hash >> 11; |
| 9379 | hash += hash << 15; |
| 9380 | if (hash == 0) hash = 27; |
| 9381 | #ifdef DEBUG |
| 9382 | StringHasher hasher(2); |
| 9383 | hasher.AddCharacter(c1); |
| 9384 | hasher.AddCharacter(c2); |
| 9385 | // If this assert fails then we failed to reproduce the two-character |
| 9386 | // version of the string hashing algorithm above. One reason could be |
| 9387 | // that we were passed two digits as characters, since the hash |
| 9388 | // algorithm is different in that case. |
| 9389 | ASSERT_EQ(static_cast<int>(hasher.GetHash()), static_cast<int>(hash)); |
| 9390 | #endif |
| 9391 | hash_ = hash; |
| 9392 | } |
| 9393 | |
| 9394 | bool IsMatch(Object* o) { |
| 9395 | if (!o->IsString()) return false; |
| 9396 | String* other = String::cast(o); |
| 9397 | if (other->length() != 2) return false; |
| 9398 | if (other->Get(0) != c1_) return false; |
| 9399 | return other->Get(1) == c2_; |
| 9400 | } |
| 9401 | |
| 9402 | uint32_t Hash() { return hash_; } |
| 9403 | uint32_t HashForObject(Object* key) { |
| 9404 | if (!key->IsString()) return 0; |
| 9405 | return String::cast(key)->Hash(); |
| 9406 | } |
| 9407 | |
| 9408 | Object* AsObject() { |
| 9409 | // The TwoCharHashTableKey is only used for looking in the symbol |
| 9410 | // table, not for adding to it. |
| 9411 | UNREACHABLE(); |
| 9412 | return NULL; |
| 9413 | } |
| 9414 | private: |
| 9415 | uint32_t c1_; |
| 9416 | uint32_t c2_; |
| 9417 | uint32_t hash_; |
| 9418 | }; |
| 9419 | |
| 9420 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9421 | bool SymbolTable::LookupSymbolIfExists(String* string, String** symbol) { |
| 9422 | SymbolKey key(string); |
| 9423 | int entry = FindEntry(&key); |
| 9424 | if (entry == kNotFound) { |
| 9425 | return false; |
| 9426 | } else { |
| 9427 | String* result = String::cast(KeyAt(entry)); |
| 9428 | ASSERT(StringShape(result).IsSymbol()); |
| 9429 | *symbol = result; |
| 9430 | return true; |
| 9431 | } |
| 9432 | } |
| 9433 | |
| 9434 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 9435 | bool SymbolTable::LookupTwoCharsSymbolIfExists(uint32_t c1, |
| 9436 | uint32_t c2, |
| 9437 | String** symbol) { |
| 9438 | TwoCharHashTableKey key(c1, c2); |
| 9439 | int entry = FindEntry(&key); |
| 9440 | if (entry == kNotFound) { |
| 9441 | return false; |
| 9442 | } else { |
| 9443 | String* result = String::cast(KeyAt(entry)); |
| 9444 | ASSERT(StringShape(result).IsSymbol()); |
| 9445 | *symbol = result; |
| 9446 | return true; |
| 9447 | } |
| 9448 | } |
| 9449 | |
| 9450 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9451 | MaybeObject* SymbolTable::LookupSymbol(Vector<const char> str, Object** s) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9452 | Utf8SymbolKey key(str); |
| 9453 | return LookupKey(&key, s); |
| 9454 | } |
| 9455 | |
| 9456 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 9457 | MaybeObject* SymbolTable::LookupAsciiSymbol(Vector<const char> str, |
| 9458 | Object** s) { |
| 9459 | AsciiSymbolKey key(str); |
| 9460 | return LookupKey(&key, s); |
| 9461 | } |
| 9462 | |
| 9463 | |
| 9464 | MaybeObject* SymbolTable::LookupTwoByteSymbol(Vector<const uc16> str, |
| 9465 | Object** s) { |
| 9466 | TwoByteSymbolKey key(str); |
| 9467 | return LookupKey(&key, s); |
| 9468 | } |
| 9469 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9470 | MaybeObject* SymbolTable::LookupKey(HashTableKey* key, Object** s) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9471 | int entry = FindEntry(key); |
| 9472 | |
| 9473 | // Symbol already in table. |
| 9474 | if (entry != kNotFound) { |
| 9475 | *s = KeyAt(entry); |
| 9476 | return this; |
| 9477 | } |
| 9478 | |
| 9479 | // Adding new symbol. Grow table if needed. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9480 | Object* obj; |
| 9481 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 9482 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9483 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9484 | |
| 9485 | // Create symbol object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9486 | Object* symbol; |
| 9487 | { MaybeObject* maybe_symbol = key->AsObject(); |
| 9488 | if (!maybe_symbol->ToObject(&symbol)) return maybe_symbol; |
| 9489 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9490 | |
| 9491 | // If the symbol table grew as part of EnsureCapacity, obj is not |
| 9492 | // the current symbol table and therefore we cannot use |
| 9493 | // SymbolTable::cast here. |
| 9494 | SymbolTable* table = reinterpret_cast<SymbolTable*>(obj); |
| 9495 | |
| 9496 | // Add the new symbol and return it along with the symbol table. |
| 9497 | entry = table->FindInsertionEntry(key->Hash()); |
| 9498 | table->set(EntryToIndex(entry), symbol); |
| 9499 | table->ElementAdded(); |
| 9500 | *s = symbol; |
| 9501 | return table; |
| 9502 | } |
| 9503 | |
| 9504 | |
| 9505 | Object* CompilationCacheTable::Lookup(String* src) { |
| 9506 | StringKey key(src); |
| 9507 | int entry = FindEntry(&key); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9508 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9509 | return get(EntryToIndex(entry) + 1); |
| 9510 | } |
| 9511 | |
| 9512 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9513 | Object* CompilationCacheTable::LookupEval(String* src, |
| 9514 | Context* context, |
| 9515 | StrictModeFlag strict_mode) { |
| 9516 | StringSharedKey key(src, context->closure()->shared(), strict_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9517 | int entry = FindEntry(&key); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9518 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9519 | return get(EntryToIndex(entry) + 1); |
| 9520 | } |
| 9521 | |
| 9522 | |
| 9523 | Object* CompilationCacheTable::LookupRegExp(String* src, |
| 9524 | JSRegExp::Flags flags) { |
| 9525 | RegExpKey key(src, flags); |
| 9526 | int entry = FindEntry(&key); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9527 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9528 | return get(EntryToIndex(entry) + 1); |
| 9529 | } |
| 9530 | |
| 9531 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9532 | MaybeObject* CompilationCacheTable::Put(String* src, Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9533 | StringKey key(src); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9534 | Object* obj; |
| 9535 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 9536 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9537 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9538 | |
| 9539 | CompilationCacheTable* cache = |
| 9540 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 9541 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 9542 | cache->set(EntryToIndex(entry), src); |
| 9543 | cache->set(EntryToIndex(entry) + 1, value); |
| 9544 | cache->ElementAdded(); |
| 9545 | return cache; |
| 9546 | } |
| 9547 | |
| 9548 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9549 | MaybeObject* CompilationCacheTable::PutEval(String* src, |
| 9550 | Context* context, |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 9551 | SharedFunctionInfo* value) { |
| 9552 | StringSharedKey key(src, |
| 9553 | context->closure()->shared(), |
| 9554 | value->strict_mode() ? kStrictMode : kNonStrictMode); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9555 | Object* obj; |
| 9556 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 9557 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9558 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9559 | |
| 9560 | CompilationCacheTable* cache = |
| 9561 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 9562 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 9563 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9564 | Object* k; |
| 9565 | { MaybeObject* maybe_k = key.AsObject(); |
| 9566 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 9567 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9568 | |
| 9569 | cache->set(EntryToIndex(entry), k); |
| 9570 | cache->set(EntryToIndex(entry) + 1, value); |
| 9571 | cache->ElementAdded(); |
| 9572 | return cache; |
| 9573 | } |
| 9574 | |
| 9575 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9576 | MaybeObject* CompilationCacheTable::PutRegExp(String* src, |
| 9577 | JSRegExp::Flags flags, |
| 9578 | FixedArray* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9579 | RegExpKey key(src, flags); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9580 | Object* obj; |
| 9581 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 9582 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9583 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9584 | |
| 9585 | CompilationCacheTable* cache = |
| 9586 | reinterpret_cast<CompilationCacheTable*>(obj); |
| 9587 | int entry = cache->FindInsertionEntry(key.Hash()); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 9588 | // We store the value in the key slot, and compare the search key |
| 9589 | // to the stored value with a custon IsMatch function during lookups. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9590 | cache->set(EntryToIndex(entry), value); |
| 9591 | cache->set(EntryToIndex(entry) + 1, value); |
| 9592 | cache->ElementAdded(); |
| 9593 | return cache; |
| 9594 | } |
| 9595 | |
| 9596 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9597 | void CompilationCacheTable::Remove(Object* value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9598 | Object* null_value = GetHeap()->null_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9599 | for (int entry = 0, size = Capacity(); entry < size; entry++) { |
| 9600 | int entry_index = EntryToIndex(entry); |
| 9601 | int value_index = entry_index + 1; |
| 9602 | if (get(value_index) == value) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9603 | fast_set(this, entry_index, null_value); |
| 9604 | fast_set(this, value_index, null_value); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 9605 | ElementRemoved(); |
| 9606 | } |
| 9607 | } |
| 9608 | return; |
| 9609 | } |
| 9610 | |
| 9611 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9612 | // SymbolsKey used for HashTable where key is array of symbols. |
| 9613 | class SymbolsKey : public HashTableKey { |
| 9614 | public: |
| 9615 | explicit SymbolsKey(FixedArray* symbols) : symbols_(symbols) { } |
| 9616 | |
| 9617 | bool IsMatch(Object* symbols) { |
| 9618 | FixedArray* o = FixedArray::cast(symbols); |
| 9619 | int len = symbols_->length(); |
| 9620 | if (o->length() != len) return false; |
| 9621 | for (int i = 0; i < len; i++) { |
| 9622 | if (o->get(i) != symbols_->get(i)) return false; |
| 9623 | } |
| 9624 | return true; |
| 9625 | } |
| 9626 | |
| 9627 | uint32_t Hash() { return HashForObject(symbols_); } |
| 9628 | |
| 9629 | uint32_t HashForObject(Object* obj) { |
| 9630 | FixedArray* symbols = FixedArray::cast(obj); |
| 9631 | int len = symbols->length(); |
| 9632 | uint32_t hash = 0; |
| 9633 | for (int i = 0; i < len; i++) { |
| 9634 | hash ^= String::cast(symbols->get(i))->Hash(); |
| 9635 | } |
| 9636 | return hash; |
| 9637 | } |
| 9638 | |
| 9639 | Object* AsObject() { return symbols_; } |
| 9640 | |
| 9641 | private: |
| 9642 | FixedArray* symbols_; |
| 9643 | }; |
| 9644 | |
| 9645 | |
| 9646 | Object* MapCache::Lookup(FixedArray* array) { |
| 9647 | SymbolsKey key(array); |
| 9648 | int entry = FindEntry(&key); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9649 | if (entry == kNotFound) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9650 | return get(EntryToIndex(entry) + 1); |
| 9651 | } |
| 9652 | |
| 9653 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9654 | MaybeObject* MapCache::Put(FixedArray* array, Map* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9655 | SymbolsKey key(array); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9656 | Object* obj; |
| 9657 | { MaybeObject* maybe_obj = EnsureCapacity(1, &key); |
| 9658 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9659 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9660 | |
| 9661 | MapCache* cache = reinterpret_cast<MapCache*>(obj); |
| 9662 | int entry = cache->FindInsertionEntry(key.Hash()); |
| 9663 | cache->set(EntryToIndex(entry), array); |
| 9664 | cache->set(EntryToIndex(entry) + 1, value); |
| 9665 | cache->ElementAdded(); |
| 9666 | return cache; |
| 9667 | } |
| 9668 | |
| 9669 | |
| 9670 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9671 | MaybeObject* Dictionary<Shape, Key>::Allocate(int at_least_space_for) { |
| 9672 | Object* obj; |
| 9673 | { MaybeObject* maybe_obj = |
| 9674 | HashTable<Shape, Key>::Allocate(at_least_space_for); |
| 9675 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9676 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9677 | // Initialize the next enumeration index. |
| 9678 | Dictionary<Shape, Key>::cast(obj)-> |
| 9679 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9680 | return obj; |
| 9681 | } |
| 9682 | |
| 9683 | |
| 9684 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9685 | MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9686 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9687 | int length = HashTable<Shape, Key>::NumberOfElements(); |
| 9688 | |
| 9689 | // Allocate and initialize iteration order array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9690 | Object* obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9691 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9692 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9693 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9694 | FixedArray* iteration_order = FixedArray::cast(obj); |
| 9695 | for (int i = 0; i < length; i++) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9696 | iteration_order->set(i, Smi::FromInt(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9697 | } |
| 9698 | |
| 9699 | // Allocate array with enumeration order. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9700 | { MaybeObject* maybe_obj = heap->AllocateFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9701 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9702 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9703 | FixedArray* enumeration_order = FixedArray::cast(obj); |
| 9704 | |
| 9705 | // Fill the enumeration order array with property details. |
| 9706 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 9707 | int pos = 0; |
| 9708 | for (int i = 0; i < capacity; i++) { |
| 9709 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9710 | enumeration_order->set(pos++, Smi::FromInt(DetailsAt(i).index())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9711 | } |
| 9712 | } |
| 9713 | |
| 9714 | // Sort the arrays wrt. enumeration order. |
| 9715 | iteration_order->SortPairs(enumeration_order, enumeration_order->length()); |
| 9716 | |
| 9717 | // Overwrite the enumeration_order with the enumeration indices. |
| 9718 | for (int i = 0; i < length; i++) { |
| 9719 | int index = Smi::cast(iteration_order->get(i))->value(); |
| 9720 | int enum_index = PropertyDetails::kInitialIndex + i; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9721 | enumeration_order->set(index, Smi::FromInt(enum_index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9722 | } |
| 9723 | |
| 9724 | // Update the dictionary with new indices. |
| 9725 | capacity = HashTable<Shape, Key>::Capacity(); |
| 9726 | pos = 0; |
| 9727 | for (int i = 0; i < capacity; i++) { |
| 9728 | if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) { |
| 9729 | int enum_index = Smi::cast(enumeration_order->get(pos++))->value(); |
| 9730 | PropertyDetails details = DetailsAt(i); |
| 9731 | PropertyDetails new_details = |
| 9732 | PropertyDetails(details.attributes(), details.type(), enum_index); |
| 9733 | DetailsAtPut(i, new_details); |
| 9734 | } |
| 9735 | } |
| 9736 | |
| 9737 | // Set the next enumeration index. |
| 9738 | SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length); |
| 9739 | return this; |
| 9740 | } |
| 9741 | |
| 9742 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9743 | MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9744 | // Check whether there are enough enumeration indices to add n elements. |
| 9745 | if (Shape::kIsEnumerable && |
| 9746 | !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) { |
| 9747 | // If not, we generate new indices for the properties. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9748 | Object* result; |
| 9749 | { MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 9750 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 9751 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9752 | } |
| 9753 | return HashTable<Shape, Key>::EnsureCapacity(n, key); |
| 9754 | } |
| 9755 | |
| 9756 | |
| 9757 | void NumberDictionary::RemoveNumberEntries(uint32_t from, uint32_t to) { |
| 9758 | // Do nothing if the interval [from, to) is empty. |
| 9759 | if (from >= to) return; |
| 9760 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9761 | Heap* heap = GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9762 | int removed_entries = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9763 | Object* sentinel = heap->null_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9764 | int capacity = Capacity(); |
| 9765 | for (int i = 0; i < capacity; i++) { |
| 9766 | Object* key = KeyAt(i); |
| 9767 | if (key->IsNumber()) { |
| 9768 | uint32_t number = static_cast<uint32_t>(key->Number()); |
| 9769 | if (from <= number && number < to) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9770 | SetEntry(i, sentinel, sentinel); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9771 | removed_entries++; |
| 9772 | } |
| 9773 | } |
| 9774 | } |
| 9775 | |
| 9776 | // Update the number of elements. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 9777 | ElementsRemoved(removed_entries); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9778 | } |
| 9779 | |
| 9780 | |
| 9781 | template<typename Shape, typename Key> |
| 9782 | Object* Dictionary<Shape, Key>::DeleteProperty(int entry, |
| 9783 | JSObject::DeleteMode mode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9784 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9785 | PropertyDetails details = DetailsAt(entry); |
| 9786 | // Ignore attributes if forcing a deletion. |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 9787 | if (details.IsDontDelete() && mode != JSObject::FORCE_DELETION) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9788 | return heap->false_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9789 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 9790 | SetEntry(entry, heap->null_value(), heap->null_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9791 | HashTable<Shape, Key>::ElementRemoved(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 9792 | return heap->true_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9793 | } |
| 9794 | |
| 9795 | |
| 9796 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9797 | MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9798 | int entry = this->FindEntry(key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9799 | |
| 9800 | // If the entry is present set the value; |
| 9801 | if (entry != Dictionary<Shape, Key>::kNotFound) { |
| 9802 | ValueAtPut(entry, value); |
| 9803 | return this; |
| 9804 | } |
| 9805 | |
| 9806 | // Check whether the dictionary should be extended. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9807 | Object* obj; |
| 9808 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 9809 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9810 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9811 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9812 | Object* k; |
| 9813 | { MaybeObject* maybe_k = Shape::AsObject(key); |
| 9814 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 9815 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9816 | PropertyDetails details = PropertyDetails(NONE, NORMAL); |
| 9817 | return Dictionary<Shape, Key>::cast(obj)-> |
| 9818 | AddEntry(key, value, details, Shape::Hash(key)); |
| 9819 | } |
| 9820 | |
| 9821 | |
| 9822 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9823 | MaybeObject* Dictionary<Shape, Key>::Add(Key key, |
| 9824 | Object* value, |
| 9825 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9826 | // Valdate key is absent. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9827 | SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9828 | // Check whether the dictionary should be extended. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9829 | Object* obj; |
| 9830 | { MaybeObject* maybe_obj = EnsureCapacity(1, key); |
| 9831 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9832 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9833 | return Dictionary<Shape, Key>::cast(obj)-> |
| 9834 | AddEntry(key, value, details, Shape::Hash(key)); |
| 9835 | } |
| 9836 | |
| 9837 | |
| 9838 | // Add a key, value pair to the dictionary. |
| 9839 | template<typename Shape, typename Key> |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9840 | MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key, |
| 9841 | Object* value, |
| 9842 | PropertyDetails details, |
| 9843 | uint32_t hash) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9844 | // Compute the key object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9845 | Object* k; |
| 9846 | { MaybeObject* maybe_k = Shape::AsObject(key); |
| 9847 | if (!maybe_k->ToObject(&k)) return maybe_k; |
| 9848 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9849 | |
| 9850 | uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash); |
| 9851 | // Insert element at empty or deleted entry |
| 9852 | if (!details.IsDeleted() && details.index() == 0 && Shape::kIsEnumerable) { |
| 9853 | // Assign an enumeration index to the property and update |
| 9854 | // SetNextEnumerationIndex. |
| 9855 | int index = NextEnumerationIndex(); |
| 9856 | details = PropertyDetails(details.attributes(), details.type(), index); |
| 9857 | SetNextEnumerationIndex(index + 1); |
| 9858 | } |
| 9859 | SetEntry(entry, k, value, details); |
| 9860 | ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber() |
| 9861 | || Dictionary<Shape, Key>::KeyAt(entry)->IsString())); |
| 9862 | HashTable<Shape, Key>::ElementAdded(); |
| 9863 | return this; |
| 9864 | } |
| 9865 | |
| 9866 | |
| 9867 | void NumberDictionary::UpdateMaxNumberKey(uint32_t key) { |
| 9868 | // If the dictionary requires slow elements an element has already |
| 9869 | // been added at a high index. |
| 9870 | if (requires_slow_elements()) return; |
| 9871 | // Check if this index is high enough that we should require slow |
| 9872 | // elements. |
| 9873 | if (key > kRequiresSlowElementsLimit) { |
| 9874 | set_requires_slow_elements(); |
| 9875 | return; |
| 9876 | } |
| 9877 | // Update max key value. |
| 9878 | Object* max_index_object = get(kMaxNumberKeyIndex); |
| 9879 | if (!max_index_object->IsSmi() || max_number_key() < key) { |
| 9880 | FixedArray::set(kMaxNumberKeyIndex, |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9881 | Smi::FromInt(key << kRequiresSlowElementsTagSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9882 | } |
| 9883 | } |
| 9884 | |
| 9885 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9886 | MaybeObject* NumberDictionary::AddNumberEntry(uint32_t key, |
| 9887 | Object* value, |
| 9888 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9889 | UpdateMaxNumberKey(key); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9890 | SLOW_ASSERT(this->FindEntry(key) == kNotFound); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9891 | return Add(key, value, details); |
| 9892 | } |
| 9893 | |
| 9894 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9895 | MaybeObject* NumberDictionary::AtNumberPut(uint32_t key, Object* value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9896 | UpdateMaxNumberKey(key); |
| 9897 | return AtPut(key, value); |
| 9898 | } |
| 9899 | |
| 9900 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9901 | MaybeObject* NumberDictionary::Set(uint32_t key, |
| 9902 | Object* value, |
| 9903 | PropertyDetails details) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9904 | int entry = FindEntry(key); |
| 9905 | if (entry == kNotFound) return AddNumberEntry(key, value, details); |
| 9906 | // Preserve enumeration index. |
| 9907 | details = PropertyDetails(details.attributes(), |
| 9908 | details.type(), |
| 9909 | DetailsAt(entry).index()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 9910 | MaybeObject* maybe_object_key = NumberDictionaryShape::AsObject(key); |
| 9911 | Object* object_key; |
| 9912 | if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 9913 | SetEntry(entry, object_key, value, details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9914 | return this; |
| 9915 | } |
| 9916 | |
| 9917 | |
| 9918 | |
| 9919 | template<typename Shape, typename Key> |
| 9920 | int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes( |
| 9921 | PropertyAttributes filter) { |
| 9922 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 9923 | int result = 0; |
| 9924 | for (int i = 0; i < capacity; i++) { |
| 9925 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 9926 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 9927 | PropertyDetails details = DetailsAt(i); |
| 9928 | if (details.IsDeleted()) continue; |
| 9929 | PropertyAttributes attr = details.attributes(); |
| 9930 | if ((attr & filter) == 0) result++; |
| 9931 | } |
| 9932 | } |
| 9933 | return result; |
| 9934 | } |
| 9935 | |
| 9936 | |
| 9937 | template<typename Shape, typename Key> |
| 9938 | int Dictionary<Shape, Key>::NumberOfEnumElements() { |
| 9939 | return NumberOfElementsFilterAttributes( |
| 9940 | static_cast<PropertyAttributes>(DONT_ENUM)); |
| 9941 | } |
| 9942 | |
| 9943 | |
| 9944 | template<typename Shape, typename Key> |
| 9945 | void Dictionary<Shape, Key>::CopyKeysTo(FixedArray* storage, |
| 9946 | PropertyAttributes filter) { |
| 9947 | ASSERT(storage->length() >= NumberOfEnumElements()); |
| 9948 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 9949 | int index = 0; |
| 9950 | for (int i = 0; i < capacity; i++) { |
| 9951 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 9952 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 9953 | PropertyDetails details = DetailsAt(i); |
| 9954 | if (details.IsDeleted()) continue; |
| 9955 | PropertyAttributes attr = details.attributes(); |
| 9956 | if ((attr & filter) == 0) storage->set(index++, k); |
| 9957 | } |
| 9958 | } |
| 9959 | storage->SortPairs(storage, index); |
| 9960 | ASSERT(storage->length() >= index); |
| 9961 | } |
| 9962 | |
| 9963 | |
| 9964 | void StringDictionary::CopyEnumKeysTo(FixedArray* storage, |
| 9965 | FixedArray* sort_array) { |
| 9966 | ASSERT(storage->length() >= NumberOfEnumElements()); |
| 9967 | int capacity = Capacity(); |
| 9968 | int index = 0; |
| 9969 | for (int i = 0; i < capacity; i++) { |
| 9970 | Object* k = KeyAt(i); |
| 9971 | if (IsKey(k)) { |
| 9972 | PropertyDetails details = DetailsAt(i); |
| 9973 | if (details.IsDeleted() || details.IsDontEnum()) continue; |
| 9974 | storage->set(index, k); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 9975 | sort_array->set(index, Smi::FromInt(details.index())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 9976 | index++; |
| 9977 | } |
| 9978 | } |
| 9979 | storage->SortPairs(sort_array, sort_array->length()); |
| 9980 | ASSERT(storage->length() >= index); |
| 9981 | } |
| 9982 | |
| 9983 | |
| 9984 | template<typename Shape, typename Key> |
| 9985 | void Dictionary<Shape, Key>::CopyKeysTo(FixedArray* storage) { |
| 9986 | ASSERT(storage->length() >= NumberOfElementsFilterAttributes( |
| 9987 | static_cast<PropertyAttributes>(NONE))); |
| 9988 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 9989 | int index = 0; |
| 9990 | for (int i = 0; i < capacity; i++) { |
| 9991 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 9992 | if (HashTable<Shape, Key>::IsKey(k)) { |
| 9993 | PropertyDetails details = DetailsAt(i); |
| 9994 | if (details.IsDeleted()) continue; |
| 9995 | storage->set(index++, k); |
| 9996 | } |
| 9997 | } |
| 9998 | ASSERT(storage->length() >= index); |
| 9999 | } |
| 10000 | |
| 10001 | |
| 10002 | // Backwards lookup (slow). |
| 10003 | template<typename Shape, typename Key> |
| 10004 | Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) { |
| 10005 | int capacity = HashTable<Shape, Key>::Capacity(); |
| 10006 | for (int i = 0; i < capacity; i++) { |
| 10007 | Object* k = HashTable<Shape, Key>::KeyAt(i); |
| 10008 | if (Dictionary<Shape, Key>::IsKey(k)) { |
| 10009 | Object* e = ValueAt(i); |
| 10010 | if (e->IsJSGlobalPropertyCell()) { |
| 10011 | e = JSGlobalPropertyCell::cast(e)->value(); |
| 10012 | } |
| 10013 | if (e == value) return k; |
| 10014 | } |
| 10015 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10016 | Heap* heap = Dictionary<Shape, Key>::GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10017 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10018 | } |
| 10019 | |
| 10020 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10021 | MaybeObject* StringDictionary::TransformPropertiesToFastFor( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10022 | JSObject* obj, int unused_property_fields) { |
| 10023 | // Make sure we preserve dictionary representation if there are too many |
| 10024 | // descriptors. |
| 10025 | if (NumberOfElements() > DescriptorArray::kMaxNumberOfDescriptors) return obj; |
| 10026 | |
| 10027 | // Figure out if it is necessary to generate new enumeration indices. |
| 10028 | int max_enumeration_index = |
| 10029 | NextEnumerationIndex() + |
| 10030 | (DescriptorArray::kMaxNumberOfDescriptors - |
| 10031 | NumberOfElements()); |
| 10032 | if (!PropertyDetails::IsValidIndex(max_enumeration_index)) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10033 | Object* result; |
| 10034 | { MaybeObject* maybe_result = GenerateNewEnumerationIndices(); |
| 10035 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10036 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10037 | } |
| 10038 | |
| 10039 | int instance_descriptor_length = 0; |
| 10040 | int number_of_fields = 0; |
| 10041 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10042 | Heap* heap = GetHeap(); |
| 10043 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10044 | // Compute the length of the instance descriptor. |
| 10045 | int capacity = Capacity(); |
| 10046 | for (int i = 0; i < capacity; i++) { |
| 10047 | Object* k = KeyAt(i); |
| 10048 | if (IsKey(k)) { |
| 10049 | Object* value = ValueAt(i); |
| 10050 | PropertyType type = DetailsAt(i).type(); |
| 10051 | ASSERT(type != FIELD); |
| 10052 | instance_descriptor_length++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10053 | if (type == NORMAL && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10054 | (!value->IsJSFunction() || heap->InNewSpace(value))) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 10055 | number_of_fields += 1; |
| 10056 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10057 | } |
| 10058 | } |
| 10059 | |
| 10060 | // Allocate the instance descriptor. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10061 | Object* descriptors_unchecked; |
| 10062 | { MaybeObject* maybe_descriptors_unchecked = |
| 10063 | DescriptorArray::Allocate(instance_descriptor_length); |
| 10064 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 10065 | return maybe_descriptors_unchecked; |
| 10066 | } |
| 10067 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10068 | DescriptorArray* descriptors = DescriptorArray::cast(descriptors_unchecked); |
| 10069 | |
| 10070 | int inobject_props = obj->map()->inobject_properties(); |
| 10071 | int number_of_allocated_fields = |
| 10072 | number_of_fields + unused_property_fields - inobject_props; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 10073 | if (number_of_allocated_fields < 0) { |
| 10074 | // There is enough inobject space for all fields (including unused). |
| 10075 | number_of_allocated_fields = 0; |
| 10076 | unused_property_fields = inobject_props - number_of_fields; |
| 10077 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10078 | |
| 10079 | // Allocate the fixed array for the fields. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10080 | Object* fields; |
| 10081 | { MaybeObject* maybe_fields = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10082 | heap->AllocateFixedArray(number_of_allocated_fields); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10083 | if (!maybe_fields->ToObject(&fields)) return maybe_fields; |
| 10084 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10085 | |
| 10086 | // Fill in the instance descriptor and the fields. |
| 10087 | int next_descriptor = 0; |
| 10088 | int current_offset = 0; |
| 10089 | for (int i = 0; i < capacity; i++) { |
| 10090 | Object* k = KeyAt(i); |
| 10091 | if (IsKey(k)) { |
| 10092 | Object* value = ValueAt(i); |
| 10093 | // Ensure the key is a symbol before writing into the instance descriptor. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10094 | Object* key; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10095 | { MaybeObject* maybe_key = heap->LookupSymbol(String::cast(k)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10096 | if (!maybe_key->ToObject(&key)) return maybe_key; |
| 10097 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10098 | PropertyDetails details = DetailsAt(i); |
| 10099 | PropertyType type = details.type(); |
| 10100 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10101 | if (value->IsJSFunction() && !heap->InNewSpace(value)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10102 | ConstantFunctionDescriptor d(String::cast(key), |
| 10103 | JSFunction::cast(value), |
| 10104 | details.attributes(), |
| 10105 | details.index()); |
| 10106 | descriptors->Set(next_descriptor++, &d); |
| 10107 | } else if (type == NORMAL) { |
| 10108 | if (current_offset < inobject_props) { |
| 10109 | obj->InObjectPropertyAtPut(current_offset, |
| 10110 | value, |
| 10111 | UPDATE_WRITE_BARRIER); |
| 10112 | } else { |
| 10113 | int offset = current_offset - inobject_props; |
| 10114 | FixedArray::cast(fields)->set(offset, value); |
| 10115 | } |
| 10116 | FieldDescriptor d(String::cast(key), |
| 10117 | current_offset++, |
| 10118 | details.attributes(), |
| 10119 | details.index()); |
| 10120 | descriptors->Set(next_descriptor++, &d); |
| 10121 | } else if (type == CALLBACKS) { |
| 10122 | CallbacksDescriptor d(String::cast(key), |
| 10123 | value, |
| 10124 | details.attributes(), |
| 10125 | details.index()); |
| 10126 | descriptors->Set(next_descriptor++, &d); |
| 10127 | } else { |
| 10128 | UNREACHABLE(); |
| 10129 | } |
| 10130 | } |
| 10131 | } |
| 10132 | ASSERT(current_offset == number_of_fields); |
| 10133 | |
| 10134 | descriptors->Sort(); |
| 10135 | // Allocate new map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 10136 | Object* new_map; |
| 10137 | { MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors(); |
| 10138 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 10139 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10140 | |
| 10141 | // Transform the object. |
| 10142 | obj->set_map(Map::cast(new_map)); |
| 10143 | obj->map()->set_instance_descriptors(descriptors); |
| 10144 | obj->map()->set_unused_property_fields(unused_property_fields); |
| 10145 | |
| 10146 | obj->set_properties(FixedArray::cast(fields)); |
| 10147 | ASSERT(obj->IsJSObject()); |
| 10148 | |
| 10149 | descriptors->SetNextEnumerationIndex(NextEnumerationIndex()); |
| 10150 | // Check that it really works. |
| 10151 | ASSERT(obj->HasFastProperties()); |
| 10152 | |
| 10153 | return obj; |
| 10154 | } |
| 10155 | |
| 10156 | |
| 10157 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 10158 | // Check if there is a break point at this code position. |
| 10159 | bool DebugInfo::HasBreakPoint(int code_position) { |
| 10160 | // Get the break point info object for this code position. |
| 10161 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 10162 | |
| 10163 | // If there is no break point info object or no break points in the break |
| 10164 | // point info object there is no break point at this code position. |
| 10165 | if (break_point_info->IsUndefined()) return false; |
| 10166 | return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0; |
| 10167 | } |
| 10168 | |
| 10169 | |
| 10170 | // Get the break point info object for this code position. |
| 10171 | Object* DebugInfo::GetBreakPointInfo(int code_position) { |
| 10172 | // Find the index of the break point info object for this code position. |
| 10173 | int index = GetBreakPointInfoIndex(code_position); |
| 10174 | |
| 10175 | // Return the break point info object if any. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10176 | if (index == kNoBreakPointInfo) return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10177 | return BreakPointInfo::cast(break_points()->get(index)); |
| 10178 | } |
| 10179 | |
| 10180 | |
| 10181 | // Clear a break point at the specified code position. |
| 10182 | void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 10183 | int code_position, |
| 10184 | Handle<Object> break_point_object) { |
| 10185 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position)); |
| 10186 | if (break_point_info->IsUndefined()) return; |
| 10187 | BreakPointInfo::ClearBreakPoint( |
| 10188 | Handle<BreakPointInfo>::cast(break_point_info), |
| 10189 | break_point_object); |
| 10190 | } |
| 10191 | |
| 10192 | |
| 10193 | void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info, |
| 10194 | int code_position, |
| 10195 | int source_position, |
| 10196 | int statement_position, |
| 10197 | Handle<Object> break_point_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10198 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10199 | Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position)); |
| 10200 | if (!break_point_info->IsUndefined()) { |
| 10201 | BreakPointInfo::SetBreakPoint( |
| 10202 | Handle<BreakPointInfo>::cast(break_point_info), |
| 10203 | break_point_object); |
| 10204 | return; |
| 10205 | } |
| 10206 | |
| 10207 | // Adding a new break point for a code position which did not have any |
| 10208 | // break points before. Try to find a free slot. |
| 10209 | int index = kNoBreakPointInfo; |
| 10210 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 10211 | if (debug_info->break_points()->get(i)->IsUndefined()) { |
| 10212 | index = i; |
| 10213 | break; |
| 10214 | } |
| 10215 | } |
| 10216 | if (index == kNoBreakPointInfo) { |
| 10217 | // No free slot - extend break point info array. |
| 10218 | Handle<FixedArray> old_break_points = |
| 10219 | Handle<FixedArray>(FixedArray::cast(debug_info->break_points())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10220 | Handle<FixedArray> new_break_points = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10221 | isolate->factory()->NewFixedArray( |
| 10222 | old_break_points->length() + |
| 10223 | Debug::kEstimatedNofBreakPointsInFunction); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 10224 | |
| 10225 | debug_info->set_break_points(*new_break_points); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10226 | for (int i = 0; i < old_break_points->length(); i++) { |
| 10227 | new_break_points->set(i, old_break_points->get(i)); |
| 10228 | } |
| 10229 | index = old_break_points->length(); |
| 10230 | } |
| 10231 | ASSERT(index != kNoBreakPointInfo); |
| 10232 | |
| 10233 | // Allocate new BreakPointInfo object and set the break point. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10234 | Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast( |
| 10235 | isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10236 | new_break_point_info->set_code_position(Smi::FromInt(code_position)); |
| 10237 | new_break_point_info->set_source_position(Smi::FromInt(source_position)); |
| 10238 | new_break_point_info-> |
| 10239 | set_statement_position(Smi::FromInt(statement_position)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10240 | new_break_point_info->set_break_point_objects( |
| 10241 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10242 | BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object); |
| 10243 | debug_info->break_points()->set(index, *new_break_point_info); |
| 10244 | } |
| 10245 | |
| 10246 | |
| 10247 | // Get the break point objects for a code position. |
| 10248 | Object* DebugInfo::GetBreakPointObjects(int code_position) { |
| 10249 | Object* break_point_info = GetBreakPointInfo(code_position); |
| 10250 | if (break_point_info->IsUndefined()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10251 | return GetHeap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10252 | } |
| 10253 | return BreakPointInfo::cast(break_point_info)->break_point_objects(); |
| 10254 | } |
| 10255 | |
| 10256 | |
| 10257 | // Get the total number of break points. |
| 10258 | int DebugInfo::GetBreakPointCount() { |
| 10259 | if (break_points()->IsUndefined()) return 0; |
| 10260 | int count = 0; |
| 10261 | for (int i = 0; i < break_points()->length(); i++) { |
| 10262 | if (!break_points()->get(i)->IsUndefined()) { |
| 10263 | BreakPointInfo* break_point_info = |
| 10264 | BreakPointInfo::cast(break_points()->get(i)); |
| 10265 | count += break_point_info->GetBreakPointCount(); |
| 10266 | } |
| 10267 | } |
| 10268 | return count; |
| 10269 | } |
| 10270 | |
| 10271 | |
| 10272 | Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 10273 | Handle<Object> break_point_object) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 10274 | Heap* heap = debug_info->GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10275 | if (debug_info->break_points()->IsUndefined()) return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10276 | for (int i = 0; i < debug_info->break_points()->length(); i++) { |
| 10277 | if (!debug_info->break_points()->get(i)->IsUndefined()) { |
| 10278 | Handle<BreakPointInfo> break_point_info = |
| 10279 | Handle<BreakPointInfo>(BreakPointInfo::cast( |
| 10280 | debug_info->break_points()->get(i))); |
| 10281 | if (BreakPointInfo::HasBreakPointObject(break_point_info, |
| 10282 | break_point_object)) { |
| 10283 | return *break_point_info; |
| 10284 | } |
| 10285 | } |
| 10286 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10287 | return heap->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10288 | } |
| 10289 | |
| 10290 | |
| 10291 | // Find the index of the break point info object for the specified code |
| 10292 | // position. |
| 10293 | int DebugInfo::GetBreakPointInfoIndex(int code_position) { |
| 10294 | if (break_points()->IsUndefined()) return kNoBreakPointInfo; |
| 10295 | for (int i = 0; i < break_points()->length(); i++) { |
| 10296 | if (!break_points()->get(i)->IsUndefined()) { |
| 10297 | BreakPointInfo* break_point_info = |
| 10298 | BreakPointInfo::cast(break_points()->get(i)); |
| 10299 | if (break_point_info->code_position()->value() == code_position) { |
| 10300 | return i; |
| 10301 | } |
| 10302 | } |
| 10303 | } |
| 10304 | return kNoBreakPointInfo; |
| 10305 | } |
| 10306 | |
| 10307 | |
| 10308 | // Remove the specified break point object. |
| 10309 | void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 10310 | Handle<Object> break_point_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10311 | Isolate* isolate = Isolate::Current(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10312 | // If there are no break points just ignore. |
| 10313 | if (break_point_info->break_point_objects()->IsUndefined()) return; |
| 10314 | // If there is a single break point clear it if it is the same. |
| 10315 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 10316 | if (break_point_info->break_point_objects() == *break_point_object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10317 | break_point_info->set_break_point_objects( |
| 10318 | isolate->heap()->undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10319 | } |
| 10320 | return; |
| 10321 | } |
| 10322 | // If there are multiple break points shrink the array |
| 10323 | ASSERT(break_point_info->break_point_objects()->IsFixedArray()); |
| 10324 | Handle<FixedArray> old_array = |
| 10325 | Handle<FixedArray>( |
| 10326 | FixedArray::cast(break_point_info->break_point_objects())); |
| 10327 | Handle<FixedArray> new_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10328 | isolate->factory()->NewFixedArray(old_array->length() - 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10329 | int found_count = 0; |
| 10330 | for (int i = 0; i < old_array->length(); i++) { |
| 10331 | if (old_array->get(i) == *break_point_object) { |
| 10332 | ASSERT(found_count == 0); |
| 10333 | found_count++; |
| 10334 | } else { |
| 10335 | new_array->set(i - found_count, old_array->get(i)); |
| 10336 | } |
| 10337 | } |
| 10338 | // If the break point was found in the list change it. |
| 10339 | if (found_count > 0) break_point_info->set_break_point_objects(*new_array); |
| 10340 | } |
| 10341 | |
| 10342 | |
| 10343 | // Add the specified break point object. |
| 10344 | void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info, |
| 10345 | Handle<Object> break_point_object) { |
| 10346 | // If there was no break point objects before just set it. |
| 10347 | if (break_point_info->break_point_objects()->IsUndefined()) { |
| 10348 | break_point_info->set_break_point_objects(*break_point_object); |
| 10349 | return; |
| 10350 | } |
| 10351 | // If the break point object is the same as before just ignore. |
| 10352 | if (break_point_info->break_point_objects() == *break_point_object) return; |
| 10353 | // If there was one break point object before replace with array. |
| 10354 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10355 | Handle<FixedArray> array = FACTORY->NewFixedArray(2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10356 | array->set(0, break_point_info->break_point_objects()); |
| 10357 | array->set(1, *break_point_object); |
| 10358 | break_point_info->set_break_point_objects(*array); |
| 10359 | return; |
| 10360 | } |
| 10361 | // If there was more than one break point before extend array. |
| 10362 | Handle<FixedArray> old_array = |
| 10363 | Handle<FixedArray>( |
| 10364 | FixedArray::cast(break_point_info->break_point_objects())); |
| 10365 | Handle<FixedArray> new_array = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 10366 | FACTORY->NewFixedArray(old_array->length() + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10367 | for (int i = 0; i < old_array->length(); i++) { |
| 10368 | // If the break point was there before just ignore. |
| 10369 | if (old_array->get(i) == *break_point_object) return; |
| 10370 | new_array->set(i, old_array->get(i)); |
| 10371 | } |
| 10372 | // Add the new break point. |
| 10373 | new_array->set(old_array->length(), *break_point_object); |
| 10374 | break_point_info->set_break_point_objects(*new_array); |
| 10375 | } |
| 10376 | |
| 10377 | |
| 10378 | bool BreakPointInfo::HasBreakPointObject( |
| 10379 | Handle<BreakPointInfo> break_point_info, |
| 10380 | Handle<Object> break_point_object) { |
| 10381 | // No break point. |
| 10382 | if (break_point_info->break_point_objects()->IsUndefined()) return false; |
| 10383 | // Single beak point. |
| 10384 | if (!break_point_info->break_point_objects()->IsFixedArray()) { |
| 10385 | return break_point_info->break_point_objects() == *break_point_object; |
| 10386 | } |
| 10387 | // Multiple break points. |
| 10388 | FixedArray* array = FixedArray::cast(break_point_info->break_point_objects()); |
| 10389 | for (int i = 0; i < array->length(); i++) { |
| 10390 | if (array->get(i) == *break_point_object) { |
| 10391 | return true; |
| 10392 | } |
| 10393 | } |
| 10394 | return false; |
| 10395 | } |
| 10396 | |
| 10397 | |
| 10398 | // Get the number of break points. |
| 10399 | int BreakPointInfo::GetBreakPointCount() { |
| 10400 | // No break point. |
| 10401 | if (break_point_objects()->IsUndefined()) return 0; |
| 10402 | // Single beak point. |
| 10403 | if (!break_point_objects()->IsFixedArray()) return 1; |
| 10404 | // Multiple break points. |
| 10405 | return FixedArray::cast(break_point_objects())->length(); |
| 10406 | } |
| 10407 | #endif |
| 10408 | |
| 10409 | |
| 10410 | } } // namespace v8::internal |