blob: 8491d5887682d4fb8cdf5f00a1d451d8e23fdd91 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Murdochb0fe1622011-05-05 13:52:32 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "execution.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010037#include "full-codegen.h"
38#include "hydrogen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "objects-inl.h"
Iain Merrick75681382010-08-19 15:07:18 +010040#include "objects-visiting.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042#include "safepoint-table.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080043#include "scanner-base.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044#include "scopeinfo.h"
45#include "string-stream.h"
Steve Blockd0582a62009-12-15 09:54:21 +000046#include "utils.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010047#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +010050#include "disasm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000051#include "disassembler.h"
52#endif
53
Steve Blocka7e24c12009-10-30 11:49:00 +000054namespace v8 {
55namespace internal {
56
57// Getters and setters are stored in a fixed array property. These are
58// constants for their indices.
59const int kGetterIndex = 0;
60const int kSetterIndex = 1;
61
62
John Reck59135872010-11-02 12:39:01 -070063MUST_USE_RESULT static MaybeObject* CreateJSValue(JSFunction* constructor,
64 Object* value) {
65 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +010066 { MaybeObject* maybe_result =
67 constructor->GetHeap()->AllocateJSObject(constructor);
John Reck59135872010-11-02 12:39:01 -070068 if (!maybe_result->ToObject(&result)) return maybe_result;
69 }
Steve Blocka7e24c12009-10-30 11:49:00 +000070 JSValue::cast(result)->set_value(value);
71 return result;
72}
73
74
John Reck59135872010-11-02 12:39:01 -070075MaybeObject* Object::ToObject(Context* global_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +000076 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 Reck59135872010-11-02 12:39:01 -070088MaybeObject* Object::ToObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +000089 if (IsJSObject()) {
90 return this;
91 } else if (IsNumber()) {
Steve Block44f0eee2011-05-26 01:26:41 +010092 Isolate* isolate = Isolate::Current();
93 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +000094 return CreateJSValue(global_context->number_function(), this);
95 } else if (IsBoolean()) {
Steve Block44f0eee2011-05-26 01:26:41 +010096 Isolate* isolate = HeapObject::cast(this)->GetIsolate();
97 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +000098 return CreateJSValue(global_context->boolean_function(), this);
99 } else if (IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100100 Isolate* isolate = HeapObject::cast(this)->GetIsolate();
101 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 return CreateJSValue(global_context->string_function(), this);
103 }
104
105 // Throw a type error.
106 return Failure::InternalError();
107}
108
109
110Object* Object::ToBoolean() {
Steve Block44f0eee2011-05-26 01:26:41 +0100111 if (IsTrue()) return this;
112 if (IsFalse()) return this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 if (IsSmi()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100114 return Isolate::Current()->heap()->ToBoolean(Smi::cast(this)->value() != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100116 HeapObject* heap_object = HeapObject::cast(this);
117 if (heap_object->IsUndefined() || heap_object->IsNull()) {
118 return heap_object->GetHeap()->false_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100119 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 // Undetectable object is false
Ben Murdoch8b112d22011-06-08 16:22:53 +0100121 if (heap_object->IsUndetectableObject()) {
122 return heap_object->GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100124 if (heap_object->IsString()) {
125 return heap_object->GetHeap()->ToBoolean(
Steve Block44f0eee2011-05-26 01:26:41 +0100126 String::cast(this)->length() != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100128 if (heap_object->IsHeapNumber()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 return HeapNumber::cast(this)->HeapNumberToBoolean();
130 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100131 return heap_object->GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000132}
133
134
135void Object::Lookup(String* name, LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 Object* holder = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100137 if (IsSmi()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100138 Heap* heap = Isolate::Current()->heap();
139 Context* global_context = heap->isolate()->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000140 holder = global_context->number_function()->instance_prototype();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100141 } 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 Blocka7e24c12009-10-30 11:49:00 +0000157 }
158 ASSERT(holder != NULL); // Cannot handle null or undefined.
159 JSObject::cast(holder)->Lookup(name, result);
160}
161
162
John Reck59135872010-11-02 12:39:01 -0700163MaybeObject* Object::GetPropertyWithReceiver(Object* receiver,
164 String* name,
165 PropertyAttributes* attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 LookupResult result;
167 Lookup(name, &result);
John Reck59135872010-11-02 12:39:01 -0700168 MaybeObject* value = GetProperty(receiver, &result, name, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 ASSERT(*attributes <= ABSENT);
170 return value;
171}
172
173
John Reck59135872010-11-02 12:39:01 -0700174MaybeObject* Object::GetPropertyWithCallback(Object* receiver,
175 Object* structure,
176 String* name,
177 Object* holder) {
Steve Block44f0eee2011-05-26 01:26:41 +0100178 Isolate* isolate = name->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 // 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 Reck59135872010-11-02 12:39:01 -0700185 MaybeObject* value = (callback->getter)(receiver, callback->data);
Steve Block44f0eee2011-05-26 01:26:41 +0100186 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 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 Block44f0eee2011-05-26 01:26:41 +0100199 LOG(isolate, ApiNamedPropertyAccess("load", self, name));
200 CustomArguments args(isolate, data->data(), self, holder_handle);
Steve Blocka7e24c12009-10-30 11:49:00 +0000201 v8::AccessorInfo info(args.end());
202 v8::Handle<v8::Value> result;
203 {
204 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100205 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 result = call_fun(v8::Utils::ToLocal(key), info);
207 }
Steve Block44f0eee2011-05-26 01:26:41 +0100208 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
209 if (result.IsEmpty()) {
210 return isolate->heap()->undefined_value();
211 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000212 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 Block44f0eee2011-05-26 01:26:41 +0100223 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 }
225
226 UNREACHABLE();
Leon Clarkef7060e22010-06-03 12:02:55 +0100227 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000228}
229
230
John Reck59135872010-11-02 12:39:01 -0700231MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver,
232 JSFunction* getter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000233 HandleScope scope;
234 Handle<JSFunction> fun(JSFunction::cast(getter));
235 Handle<Object> self(receiver);
236#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100237 Debug* debug = fun->GetHeap()->isolate()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 // Handle stepping into a getter if step into is active.
Steve Block44f0eee2011-05-26 01:26:41 +0100239 if (debug->StepInActive()) {
240 debug->HandleStepIn(fun, Handle<Object>::null(), 0, false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 }
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 Reck59135872010-11-02 12:39:01 -0700253MaybeObject* JSObject::GetPropertyWithFailedAccessCheck(
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 Object* receiver,
255 LookupResult* result,
256 String* name,
257 PropertyAttributes* attributes) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000258 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 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 Popescu402d9372010-02-26 13:31:12 +0000281 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 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 Popescu402d9372010-02-26 13:31:12 +0000294 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 return GetPropertyWithFailedAccessCheck(receiver,
296 &r,
297 name,
298 attributes);
299 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 break;
301 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000302 default:
303 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 }
305 }
306
307 // No accessible property found.
308 *attributes = ABSENT;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100309 Heap* heap = name->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100310 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_GET);
311 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000312}
313
314
315PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck(
316 Object* receiver,
317 LookupResult* result,
318 String* name,
319 bool continue_search) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000320 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 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 Popescu402d9372010-02-26 13:31:12 +0000341 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 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 Popescu402d9372010-02-26 13:31:12 +0000359 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000360 return GetPropertyAttributeWithFailedAccessCheck(receiver,
361 &r,
362 name,
363 continue_search);
364 }
365 break;
366 }
367
Andrei Popescu402d9372010-02-26 13:31:12 +0000368 default:
369 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 }
371 }
372
Ben Murdoch8b112d22011-06-08 16:22:53 +0100373 GetHeap()->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 return ABSENT;
375}
376
377
Steve Blocka7e24c12009-10-30 11:49:00 +0000378Object* 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
389Object* 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 Reck59135872010-11-02 12:39:01 -0700403MaybeObject* JSObject::SetNormalizedProperty(String* name,
404 Object* value,
405 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 ASSERT(!HasFastProperties());
407 int entry = property_dictionary()->FindEntry(name);
408 if (entry == StringDictionary::kNotFound) {
409 Object* store_value = value;
410 if (IsGlobalObject()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100411 Heap* heap = name->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100412 MaybeObject* maybe_store_value =
413 heap->AllocateJSGlobalPropertyCell(value);
414 if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 }
John Reck59135872010-11-02 12:39:01 -0700416 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 Blocka7e24c12009-10-30 11:49:00 +0000421 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 Reck59135872010-11-02 12:39:01 -0700441MaybeObject* JSObject::DeleteNormalizedProperty(String* name, DeleteMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000442 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 Murdoch8b112d22011-06-08 16:22:53 +0100450 if (mode != FORCE_DELETION) return GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // 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 Reck59135872010-11-02 12:39:01 -0700455 Object* new_map;
456 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
457 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
458 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 set_map(Map::cast(new_map));
460 }
461 JSGlobalPropertyCell* cell =
462 JSGlobalPropertyCell::cast(dictionary->ValueAt(entry));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100463 cell->set_value(cell->heap()->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000464 dictionary->DetailsAtPut(entry, details.AsDeleted());
465 } else {
466 return dictionary->DeleteProperty(entry, mode);
467 }
468 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100469 return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000470}
471
472
473bool JSObject::IsDirty() {
474 Object* cons_obj = map()->constructor();
475 if (!cons_obj->IsJSFunction())
476 return true;
477 JSFunction* fun = JSFunction::cast(cons_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100478 if (!fun->shared()->IsApiFunction())
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 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 Reck59135872010-11-02 12:39:01 -0700488MaybeObject* Object::GetProperty(Object* receiver,
489 LookupResult* result,
490 String* name,
491 PropertyAttributes* attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 // Make sure that the top context does not change when doing
493 // callbacks or interceptor calls.
494 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +0100495 Heap* heap = name->GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000496
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 Block44f0eee2011-05-26 01:26:41 +0100503 Object* last = result->IsProperty() ? result->holder() : heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 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 Block44f0eee2011-05-26 01:26:41 +0100511 if (!heap->isolate()->MayNamedAccess(checked, name, v8::ACCESS_GET)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 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 Block44f0eee2011-05-26 01:26:41 +0100526 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 }
528 *attributes = result->GetAttributes();
Steve Blocka7e24c12009-10-30 11:49:00 +0000529 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 Block44f0eee2011-05-26 01:26:41 +0100535 return value->IsTheHole() ? heap->undefined_value() : value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 case FIELD:
537 value = holder->FastPropertyAt(result->GetFieldIndex());
538 ASSERT(!value->IsTheHole() || result->IsReadOnly());
Steve Block44f0eee2011-05-26 01:26:41 +0100539 return value->IsTheHole() ? heap->undefined_value() : value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 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 Reck59135872010-11-02 12:39:01 -0700558MaybeObject* Object::GetElementWithReceiver(Object* receiver, uint32_t index) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100559 Object* holder = NULL;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100560 if (IsSmi()) {
561 Context* global_context = Isolate::Current()->context()->global_context();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100562 holder = global_context->number_function()->instance_prototype();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100563 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100564 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 Murdoche0cee9b2011-05-25 10:26:03 +0100584 }
585
586 return JSObject::cast(holder)->GetElementWithReceiver(receiver, index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000587}
588
589
590Object* Object::GetPrototype() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100591 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 Blocka7e24c12009-10-30 11:49:00 +0000599 // The object is either a number, a string, a boolean, or a real JS object.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100600 if (heap_object->IsJSObject()) {
601 return JSObject::cast(this)->map()->prototype();
602 }
603 Heap* heap = heap_object->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100604 Context* context = heap->isolate()->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000605
Ben Murdoch8b112d22011-06-08 16:22:53 +0100606 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 Blocka7e24c12009-10-30 11:49:00 +0000613 return context->boolean_function()->instance_prototype();
614 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100615 return heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000616 }
617}
618
619
Ben Murdochb0fe1622011-05-05 13:52:32 +0100620void Object::ShortPrint(FILE* out) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 HeapStringAllocator allocator;
622 StringStream accumulator(&allocator);
623 ShortPrint(&accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 accumulator.OutputToFile(out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000625}
626
627
628void 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 Murdochb0fe1622011-05-05 13:52:32 +0100639void Smi::SmiPrint(FILE* out) {
640 PrintF(out, "%d", value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000641}
642
643
644void Smi::SmiPrint(StringStream* accumulator) {
645 accumulator->Add("%d", value());
646}
647
648
649void Failure::FailurePrint(StringStream* accumulator) {
Steve Block3ce2e202009-11-05 08:53:23 +0000650 accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000651}
652
653
Ben Murdochb0fe1622011-05-05 13:52:32 +0100654void Failure::FailurePrint(FILE* out) {
655 PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000656}
657
658
Steve Blocka7e24c12009-10-30 11:49:00 +0000659// 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".
664static 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 Reck59135872010-11-02 12:39:01 -0700683MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000684#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 Block44f0eee2011-05-26 01:26:41 +0100689 if (!HEAP->IsAllocationAllowed()) return this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000690#endif
691
Steve Block44f0eee2011-05-26 01:26:41 +0100692 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 switch (StringShape(this).representation_tag()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 case kConsStringTag: {
695 ConsString* cs = ConsString::cast(this);
696 if (cs->second()->length() == 0) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100697 return cs->first();
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 }
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 Block44f0eee2011-05-26 01:26:41 +0100702 PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED;
Steve Blocka7e24c12009-10-30 11:49:00 +0000703 int len = length();
704 Object* object;
705 String* result;
706 if (IsAsciiRepresentation()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100707 { MaybeObject* maybe_object = heap->AllocateRawAsciiString(len, tenure);
John Reck59135872010-11-02 12:39:01 -0700708 if (!maybe_object->ToObject(&object)) return maybe_object;
709 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 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 Reck59135872010-11-02 12:39:01 -0700721 { MaybeObject* maybe_object =
Steve Block44f0eee2011-05-26 01:26:41 +0100722 heap->AllocateRawTwoByteString(len, tenure);
John Reck59135872010-11-02 12:39:01 -0700723 if (!maybe_object->ToObject(&object)) return maybe_object;
724 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000725 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 Block44f0eee2011-05-26 01:26:41 +0100737 cs->set_second(heap->empty_string());
Leon Clarkef7060e22010-06-03 12:02:55 +0100738 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 }
740 default:
741 return this;
742 }
743}
744
745
746bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Block8defd9f2010-07-08 12:39:36 +0100747 // Externalizing twice leaks the external resource, so it's
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100748 // prohibited by the API.
749 ASSERT(!this->IsExternalString());
Steve Blocka7e24c12009-10-30 11:49:00 +0000750#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000751 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 // Assert that the resource and the string are equivalent.
753 ASSERT(static_cast<size_t>(this->length()) == resource->length());
Kristian Monsen25f61362010-05-21 11:50:48 +0100754 ScopedVector<uc16> smart_chars(this->length());
755 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
756 ASSERT(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000757 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +0100758 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 }
760#endif // DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100761 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000762 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 Monsen9dcf7e22010-06-28 14:14:28 +0100769 bool is_ascii = this->IsAsciiRepresentation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 bool is_symbol = this->IsSymbol();
771 int length = this->length();
Steve Blockd0582a62009-12-15 09:54:21 +0000772 int hash_field = this->hash_field();
Steve Blocka7e24c12009-10-30 11:49:00 +0000773
774 // Morph the object to an external string by adjusting the map and
775 // reinitializing the fields.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100776 this->set_map(is_ascii ?
Steve Block44f0eee2011-05-26 01:26:41 +0100777 heap->external_string_with_ascii_data_map() :
778 heap->external_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000779 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
780 self->set_length(length);
Steve Blockd0582a62009-12-15 09:54:21 +0000781 self->set_hash_field(hash_field);
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 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 Monsen9dcf7e22010-06-28 14:14:28 +0100788 this->set_map(is_ascii ?
Steve Block44f0eee2011-05-26 01:26:41 +0100789 heap->external_symbol_with_ascii_data_map() :
790 heap->external_symbol_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000791 }
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 Block44f0eee2011-05-26 01:26:41 +0100795 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 return true;
797}
798
799
800bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) {
801#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000802 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000803 // Assert that the resource and the string are equivalent.
804 ASSERT(static_cast<size_t>(this->length()) == resource->length());
Kristian Monsen25f61362010-05-21 11:50:48 +0100805 ScopedVector<char> smart_chars(this->length());
806 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
807 ASSERT(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000808 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +0100809 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 }
811#endif // DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100812 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000813 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 Blockd0582a62009-12-15 09:54:21 +0000822 int hash_field = this->hash_field();
Steve Blocka7e24c12009-10-30 11:49:00 +0000823
824 // Morph the object to an external string by adjusting the map and
825 // reinitializing the fields.
Steve Block44f0eee2011-05-26 01:26:41 +0100826 this->set_map(heap->external_ascii_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000827 ExternalAsciiString* self = ExternalAsciiString::cast(this);
828 self->set_length(length);
Steve Blockd0582a62009-12-15 09:54:21 +0000829 self->set_hash_field(hash_field);
Steve Blocka7e24c12009-10-30 11:49:00 +0000830 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 Block44f0eee2011-05-26 01:26:41 +0100836 this->set_map(heap->external_ascii_symbol_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000837 }
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 Block44f0eee2011-05-26 01:26:41 +0100841 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 return true;
843}
844
845
846void String::StringShortPrint(StringStream* accumulator) {
847 int len = length();
Steve Blockd0582a62009-12-15 09:54:21 +0000848 if (len > kMaxShortPrintLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000849 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
909void 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 Murdoch8b112d22011-06-08 16:22:53 +0100940 Map* map_of_this = map();
941 Heap* heap = map_of_this->heap();
942 Object* constructor = map_of_this->constructor();
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 bool printed = false;
944 if (constructor->IsHeapObject() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100945 !heap->Contains(HeapObject::cast(constructor))) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000946 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
947 } else {
948 bool global_object = IsJSGlobalProxy();
949 if (constructor->IsJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100950 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 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
984void HeapObject::HeapObjectShortPrint(StringStream* accumulator) {
Steve Block44f0eee2011-05-26 01:26:41 +0100985 // if (!HEAP->InNewSpace(this)) PrintF("*", this);
986 Heap* heap = GetHeap();
987 if (!heap->Contains(this)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000988 accumulator->Add("!!!INVALID POINTER!!!");
989 return;
990 }
Steve Block44f0eee2011-05-26 01:26:41 +0100991 if (!heap->Contains(map())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000992 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 Block44f0eee2011-05-26 01:26:41 +01001016 case EXTERNAL_PIXEL_ARRAY_TYPE:
1017 accumulator->Add("<ExternalPixelArray[%u]>",
1018 ExternalPixelArray::cast(this)->length());
Steve Blocka7e24c12009-10-30 11:49:00 +00001019 break;
Steve Block3ce2e202009-11-05 08:53:23 +00001020 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 Blocka7e24c12009-10-30 11:49:00 +00001048 case SHARED_FUNCTION_INFO_TYPE:
1049 accumulator->Add("<SharedFunctionInfo>");
1050 break;
Steve Block1e0659c2011-05-24 12:43:12 +01001051 case JS_MESSAGE_OBJECT_TYPE:
1052 accumulator->Add("<JSMessageObject>");
1053 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001054#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 Blocka7e24c12009-10-30 11:49:00 +00001099void 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
1108void 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 Merrick75681382010-08-19 15:07:18 +01001117 ConsString::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001118 break;
Steve Blockd0582a62009-12-15 09:54:21 +00001119 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 Blocka7e24c12009-10-30 11:49:00 +00001127 break;
1128 }
1129 return;
1130 }
1131
1132 switch (type) {
1133 case FIXED_ARRAY_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001134 FixedArray::BodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001135 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 Blocka7e24c12009-10-30 11:49:00 +00001141 case JS_GLOBAL_PROXY_TYPE:
1142 case JS_GLOBAL_OBJECT_TYPE:
1143 case JS_BUILTINS_OBJECT_TYPE:
Steve Block1e0659c2011-05-24 12:43:12 +01001144 case JS_MESSAGE_OBJECT_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001145 JSObject::BodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 break;
Steve Block791712a2010-08-27 10:21:07 +01001147 case JS_FUNCTION_TYPE:
1148 reinterpret_cast<JSFunction*>(this)
1149 ->JSFunctionIterateBody(object_size, v);
1150 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001151 case ODDBALL_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001152 Oddball::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001153 break;
1154 case PROXY_TYPE:
1155 reinterpret_cast<Proxy*>(this)->ProxyIterateBody(v);
1156 break;
1157 case MAP_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001158 Map::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 break;
1160 case CODE_TYPE:
1161 reinterpret_cast<Code*>(this)->CodeIterateBody(v);
1162 break;
1163 case JS_GLOBAL_PROPERTY_CELL_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001164 JSGlobalPropertyCell::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001165 break;
1166 case HEAP_NUMBER_TYPE:
1167 case FILLER_TYPE:
1168 case BYTE_ARRAY_TYPE:
Steve Block44f0eee2011-05-26 01:26:41 +01001169 case EXTERNAL_PIXEL_ARRAY_TYPE:
Steve Block3ce2e202009-11-05 08:53:23 +00001170 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 Blocka7e24c12009-10-30 11:49:00 +00001177 break;
Iain Merrick75681382010-08-19 15:07:18 +01001178 case SHARED_FUNCTION_INFO_TYPE:
1179 SharedFunctionInfo::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001180 break;
Iain Merrick75681382010-08-19 15:07:18 +01001181
Steve Blocka7e24c12009-10-30 11:49:00 +00001182#define MAKE_STRUCT_CASE(NAME, Name, name) \
1183 case NAME##_TYPE:
1184 STRUCT_LIST(MAKE_STRUCT_CASE)
1185#undef MAKE_STRUCT_CASE
Iain Merrick75681382010-08-19 15:07:18 +01001186 StructBodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001187 break;
1188 default:
1189 PrintF("Unknown type: %d\n", type);
1190 UNREACHABLE();
1191 }
1192}
1193
1194
Steve Blocka7e24c12009-10-30 11:49:00 +00001195Object* HeapNumber::HeapNumberToBoolean() {
1196 // NaN, +0, and -0 should return the false object
Iain Merrick75681382010-08-19 15:07:18 +01001197#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 Block44f0eee2011-05-26 01:26:41 +01001206 return GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 }
Iain Merrick75681382010-08-19 15:07:18 +01001208 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 Block44f0eee2011-05-26 01:26:41 +01001211 return GetHeap()->false_value();
Iain Merrick75681382010-08-19 15:07:18 +01001212 }
Steve Block44f0eee2011-05-26 01:26:41 +01001213 return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001214}
1215
1216
Ben Murdochb0fe1622011-05-05 13:52:32 +01001217void HeapNumber::HeapNumberPrint(FILE* out) {
1218 PrintF(out, "%.16g", Number());
Steve Blocka7e24c12009-10-30 11:49:00 +00001219}
1220
1221
1222void 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
1235String* JSObject::class_name() {
1236 if (IsJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001237 return GetHeap()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001238 }
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 Block44f0eee2011-05-26 01:26:41 +01001244 return GetHeap()->Object_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001245}
1246
1247
1248String* JSObject::constructor_name() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001249 if (map()->constructor()->IsJSFunction()) {
1250 JSFunction* constructor = JSFunction::cast(map()->constructor());
1251 String* name = String::cast(constructor->shared()->name());
Ben Murdochf87a2032010-10-22 12:50:53 +01001252 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 Blocka7e24c12009-10-30 11:49:00 +00001257 }
1258 // If the constructor is not present, return "Object".
Steve Block44f0eee2011-05-26 01:26:41 +01001259 return GetHeap()->Object_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001260}
1261
1262
John Reck59135872010-11-02 12:39:01 -07001263MaybeObject* JSObject::AddFastPropertyUsingMap(Map* new_map,
1264 String* name,
1265 Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001266 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 Reck59135872010-11-02 12:39:01 -07001270 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 Blocka7e24c12009-10-30 11:49:00 +00001275 set_properties(FixedArray::cast(values));
1276 }
1277 set_map(new_map);
1278 return FastPropertyAtPut(index, value);
1279}
1280
1281
Ben Murdoch8b112d22011-06-08 16:22:53 +01001282static 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 Reck59135872010-11-02 12:39:01 -07001298MaybeObject* JSObject::AddFastProperty(String* name,
1299 Object* value,
1300 PropertyAttributes attributes) {
Steve Block1e0659c2011-05-24 12:43:12 +01001301 ASSERT(!IsJSGlobalProxy());
1302
Steve Blocka7e24c12009-10-30 11:49:00 +00001303 // Normalize the object if the name is an actual string (not the
1304 // hidden symbols) and is not a real identifier.
Steve Block44f0eee2011-05-26 01:26:41 +01001305 Isolate* isolate = GetHeap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001306 StringInputBuffer buffer(name);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001307 if (!IsIdentifier(isolate->unicode_cache(), &buffer)
Steve Block44f0eee2011-05-26 01:26:41 +01001308 && name != isolate->heap()->hidden_symbol()) {
John Reck59135872010-11-02 12:39:01 -07001309 Object* obj;
1310 { MaybeObject* maybe_obj =
1311 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1312 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1313 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001314 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 Reck59135872010-11-02 12:39:01 -07001323 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 Blocka7e24c12009-10-30 11:49:00 +00001330
Steve Block44f0eee2011-05-26 01:26:41 +01001331 // 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 Blocka7e24c12009-10-30 11:49:00 +00001343 bool allow_map_transition =
Steve Block44f0eee2011-05-26 01:26:41 +01001344 can_insert_transition &&
1345 (isolate->context()->global_context()->object_function()->map() != map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001346
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 Reck59135872010-11-02 12:39:01 -07001351 Object* r;
1352 { MaybeObject* maybe_r = map()->CopyDropDescriptors();
1353 if (!maybe_r->ToObject(&r)) return maybe_r;
1354 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001355 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 Reck59135872010-11-02 12:39:01 -07001359 Object* r;
1360 { MaybeObject* maybe_r = old_descriptors->CopyInsert(&d, KEEP_TRANSITIONS);
1361 if (!maybe_r->ToObject(&r)) return maybe_r;
1362 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001363 old_descriptors = DescriptorArray::cast(r);
1364 }
1365
1366 if (map()->unused_property_fields() == 0) {
Steve Block8defd9f2010-07-08 12:39:36 +01001367 if (properties()->length() > MaxFastProperties()) {
John Reck59135872010-11-02 12:39:01 -07001368 Object* obj;
1369 { MaybeObject* maybe_obj =
1370 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1371 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1372 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001373 return AddSlowProperty(name, value, attributes);
1374 }
1375 // Make room for the new value
John Reck59135872010-11-02 12:39:01 -07001376 Object* values;
1377 { MaybeObject* maybe_values =
1378 properties()->CopySize(properties()->length() + kFieldsAdded);
1379 if (!maybe_values->ToObject(&values)) return maybe_values;
1380 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001381 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 Reck59135872010-11-02 12:39:01 -07001395MaybeObject* JSObject::AddConstantFunctionProperty(
1396 String* name,
1397 JSFunction* function,
1398 PropertyAttributes attributes) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001399 ASSERT(!GetHeap()->InNewSpace(function));
Leon Clarkee46be812010-01-19 14:06:41 +00001400
Steve Blocka7e24c12009-10-30 11:49:00 +00001401 // Allocate new instance descriptors with (name, function) added
1402 ConstantFunctionDescriptor d(name, function, attributes);
John Reck59135872010-11-02 12:39:01 -07001403 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 Blocka7e24c12009-10-30 11:49:00 +00001410
1411 // Allocate a new map for the object.
John Reck59135872010-11-02 12:39:01 -07001412 Object* new_map;
1413 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
1414 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
1415 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001416
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 Murdoch8b112d22011-06-08 16:22:53 +01001424 Heap* heap = old_map->heap();
Steve Block44f0eee2011-05-26 01:26:41 +01001425 if (old_map == heap->isolate()->context()->global_context()->
1426 object_function()->map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001427 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 Merrick75681382010-08-19 15:07:18 +01001442 ConstTransitionDescriptor mark(name, Map::cast(new_map));
John Reck59135872010-11-02 12:39:01 -07001443 { 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 Blocka7e24c12009-10-30 11:49:00 +00001449 }
1450 old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors));
1451
1452 return function;
1453}
1454
1455
1456// Add property in slow mode
John Reck59135872010-11-02 12:39:01 -07001457MaybeObject* JSObject::AddSlowProperty(String* name,
1458 Object* value,
1459 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001460 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 Murdoch8b112d22011-06-08 16:22:53 +01001477 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -07001478 { MaybeObject* maybe_store_value =
Steve Block44f0eee2011-05-26 01:26:41 +01001479 heap->AllocateJSGlobalPropertyCell(value);
John Reck59135872010-11-02 12:39:01 -07001480 if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value;
1481 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001482 JSGlobalPropertyCell::cast(store_value)->set_value(value);
1483 }
1484 PropertyDetails details = PropertyDetails(attributes, NORMAL);
John Reck59135872010-11-02 12:39:01 -07001485 Object* result;
1486 { MaybeObject* maybe_result = dict->Add(name, store_value, details);
1487 if (!maybe_result->ToObject(&result)) return maybe_result;
1488 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001489 if (dict != result) set_properties(StringDictionary::cast(result));
1490 return value;
1491}
1492
1493
John Reck59135872010-11-02 12:39:01 -07001494MaybeObject* JSObject::AddProperty(String* name,
1495 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001496 PropertyAttributes attributes,
1497 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001498 ASSERT(!IsJSGlobalProxy());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001499 Map* map_of_this = map();
1500 Heap* heap = map_of_this->heap();
1501 if (!map_of_this->is_extensible()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001502 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 Block8defd9f2010-07-08 12:39:36 +01001510 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001511 if (HasFastProperties()) {
1512 // Ensure the descriptor array does not get too big.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001513 if (map_of_this->instance_descriptors()->number_of_descriptors() <
Steve Blocka7e24c12009-10-30 11:49:00 +00001514 DescriptorArray::kMaxNumberOfDescriptors) {
Steve Block44f0eee2011-05-26 01:26:41 +01001515 if (value->IsJSFunction() && !heap->InNewSpace(value)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001516 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 Reck59135872010-11-02 12:39:01 -07001525 Object* obj;
1526 { MaybeObject* maybe_obj =
1527 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1528 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1529 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 }
1531 }
1532 return AddSlowProperty(name, value, attributes);
1533}
1534
1535
John Reck59135872010-11-02 12:39:01 -07001536MaybeObject* JSObject::SetPropertyPostInterceptor(
1537 String* name,
1538 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001539 PropertyAttributes attributes,
1540 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001541 // Check local property, ignore interceptor.
1542 LookupResult result;
1543 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001544 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 Murdoche0cee9b2011-05-25 10:26:03 +01001547 return SetProperty(&result, name, value, attributes, strict_mode);
Andrei Popescu402d9372010-02-26 13:31:12 +00001548 }
1549 // Add a new real property.
Steve Block44f0eee2011-05-26 01:26:41 +01001550 return AddProperty(name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001551}
1552
1553
John Reck59135872010-11-02 12:39:01 -07001554MaybeObject* JSObject::ReplaceSlowProperty(String* name,
1555 Object* value,
1556 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 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 Blockd0582a62009-12-15 09:54:21 +00001570
John Reck59135872010-11-02 12:39:01 -07001571MaybeObject* JSObject::ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001572 String* name,
1573 Object* new_value,
1574 PropertyAttributes attributes) {
1575 Map* old_map = map();
John Reck59135872010-11-02 12:39:01 -07001576 Object* result;
1577 { MaybeObject* maybe_result =
1578 ConvertDescriptorToField(name, new_value, attributes);
1579 if (!maybe_result->ToObject(&result)) return maybe_result;
1580 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001581 // 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 Murdoch8b112d22011-06-08 16:22:53 +01001587 if (map() == old_map->heap()->isolate()->context()->global_context()->
Steve Block44f0eee2011-05-26 01:26:41 +01001588 object_function()->map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001589 return result;
1590 }
1591
1592 MapTransitionDescriptor transition(name,
1593 map(),
1594 attributes);
John Reck59135872010-11-02 12:39:01 -07001595 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 Blocka7e24c12009-10-30 11:49:00 +00001602 old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors));
1603 return result;
1604}
1605
1606
John Reck59135872010-11-02 12:39:01 -07001607MaybeObject* JSObject::ConvertDescriptorToField(String* name,
1608 Object* new_value,
1609 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001610 if (map()->unused_property_fields() == 0 &&
Steve Block8defd9f2010-07-08 12:39:36 +01001611 properties()->length() > MaxFastProperties()) {
John Reck59135872010-11-02 12:39:01 -07001612 Object* obj;
1613 { MaybeObject* maybe_obj =
1614 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1615 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1616 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001617 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 Reck59135872010-11-02 12:39:01 -07001623 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 Blocka7e24c12009-10-30 11:49:00 +00001630 DescriptorArray* new_descriptors =
1631 DescriptorArray::cast(descriptors_unchecked);
1632
1633 // Make a new map for the object.
John Reck59135872010-11-02 12:39:01 -07001634 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 Blocka7e24c12009-10-30 11:49:00 +00001640 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 Monsen0d5e1162010-09-30 15:31:59 +01001647 new_unused_property_fields = kFieldsAdded - 1;
John Reck59135872010-11-02 12:39:01 -07001648 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 Blocka7e24c12009-10-30 11:49:00 +00001656 }
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 Reck59135872010-11-02 12:39:01 -07001670MaybeObject* JSObject::SetPropertyWithInterceptor(
1671 String* name,
1672 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001673 PropertyAttributes attributes,
1674 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01001675 Isolate* isolate = GetIsolate();
1676 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001677 Handle<JSObject> this_handle(this);
1678 Handle<String> name_handle(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001679 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001680 Handle<InterceptorInfo> interceptor(GetNamedInterceptor());
1681 if (!interceptor->setter()->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001682 LOG(isolate, ApiNamedPropertyAccess("interceptor-named-set", this, name));
1683 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001684 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 Block44f0eee2011-05-26 01:26:41 +01001690 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001691 Handle<Object> value_unhole(value->IsTheHole() ?
Steve Block44f0eee2011-05-26 01:26:41 +01001692 isolate->heap()->undefined_value() :
1693 value,
1694 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001695 result = setter(v8::Utils::ToLocal(name_handle),
1696 v8::Utils::ToLocal(value_unhole),
1697 info);
1698 }
Steve Block44f0eee2011-05-26 01:26:41 +01001699 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001700 if (!result.IsEmpty()) return *value_handle;
1701 }
John Reck59135872010-11-02 12:39:01 -07001702 MaybeObject* raw_result =
1703 this_handle->SetPropertyPostInterceptor(*name_handle,
1704 *value_handle,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001705 attributes,
1706 strict_mode);
Steve Block44f0eee2011-05-26 01:26:41 +01001707 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001708 return raw_result;
1709}
1710
1711
John Reck59135872010-11-02 12:39:01 -07001712MaybeObject* JSObject::SetProperty(String* name,
1713 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001714 PropertyAttributes attributes,
1715 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001716 LookupResult result;
1717 LocalLookup(name, &result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001718 return SetProperty(&result, name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001719}
1720
1721
John Reck59135872010-11-02 12:39:01 -07001722MaybeObject* JSObject::SetPropertyWithCallback(Object* structure,
1723 String* name,
1724 Object* value,
1725 JSObject* holder) {
Steve Block44f0eee2011-05-26 01:26:41 +01001726 Isolate* isolate = GetIsolate();
1727 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001728
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 Block44f0eee2011-05-26 01:26:41 +01001732 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001733
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 Reck59135872010-11-02 12:39:01 -07001740 MaybeObject* obj = (callback->setter)(this, value, callback->data);
Steve Block44f0eee2011-05-26 01:26:41 +01001741 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001742 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 Block44f0eee2011-05-26 01:26:41 +01001753 LOG(isolate, ApiNamedPropertyAccess("store", this, name));
1754 CustomArguments args(isolate, data->data(), this, JSObject::cast(holder));
Steve Blocka7e24c12009-10-30 11:49:00 +00001755 v8::AccessorInfo info(args.end());
1756 {
1757 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001758 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001759 call_fun(v8::Utils::ToLocal(key),
1760 v8::Utils::ToLocal(value_handle),
1761 info);
1762 }
Steve Block44f0eee2011-05-26 01:26:41 +01001763 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 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 Block44f0eee2011-05-26 01:26:41 +01001773 Handle<Object> holder_handle(holder, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001774 Handle<Object> args[2] = { key, holder_handle };
Steve Block44f0eee2011-05-26 01:26:41 +01001775 return isolate->Throw(
1776 *isolate->factory()->NewTypeError("no_setter_in_callback",
1777 HandleVector(args, 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001778 }
1779 }
1780
1781 UNREACHABLE();
Leon Clarkef7060e22010-06-03 12:02:55 +01001782 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00001783}
1784
1785
John Reck59135872010-11-02 12:39:01 -07001786MaybeObject* JSObject::SetPropertyWithDefinedSetter(JSFunction* setter,
1787 Object* value) {
Steve Block44f0eee2011-05-26 01:26:41 +01001788 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 Blocka7e24c12009-10-30 11:49:00 +00001792#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001793 Debug* debug = isolate->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00001794 // Handle stepping into a setter if step into is active.
Steve Block44f0eee2011-05-26 01:26:41 +01001795 if (debug->StepInActive()) {
1796 debug->HandleStepIn(fun, Handle<Object>::null(), 0, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00001797 }
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
1808void JSObject::LookupCallbackSetterInPrototypes(String* name,
1809 LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01001810 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001811 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01001812 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001813 pt = pt->GetPrototype()) {
1814 JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001815 if (result->IsProperty()) {
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001816 if (result->type() == CALLBACKS && !result->IsReadOnly()) return;
1817 // Found non-callback or read-only callback, stop looking.
1818 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001819 }
1820 }
1821 result->NotFound();
1822}
1823
1824
Steve Block1e0659c2011-05-24 12:43:12 +01001825MaybeObject* JSObject::SetElementWithCallbackSetterInPrototypes(uint32_t index,
1826 Object* value,
1827 bool* found) {
Steve Block44f0eee2011-05-26 01:26:41 +01001828 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00001829 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01001830 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001831 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 Blocka7e24c12009-10-30 11:49:00 +00001838 PropertyDetails details = dictionary->DetailsAt(entry);
1839 if (details.type() == CALLBACKS) {
Steve Block1e0659c2011-05-24 12:43:12 +01001840 *found = true;
1841 return SetElementWithCallback(
1842 dictionary->ValueAt(entry), index, value, JSObject::cast(pt));
Steve Blocka7e24c12009-10-30 11:49:00 +00001843 }
1844 }
1845 }
Steve Block1e0659c2011-05-24 12:43:12 +01001846 *found = false;
Steve Block44f0eee2011-05-26 01:26:41 +01001847 return heap->the_hole_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001848}
1849
1850
1851void JSObject::LookupInDescriptor(String* name, LookupResult* result) {
1852 DescriptorArray* descriptors = map()->instance_descriptors();
Iain Merrick75681382010-08-19 15:07:18 +01001853 int number = descriptors->SearchWithCache(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001854 if (number != DescriptorArray::kNotFound) {
1855 result->DescriptorResult(this, descriptors->GetDetails(number), number);
1856 } else {
1857 result->NotFound();
1858 }
1859}
1860
1861
Ben Murdochb0fe1622011-05-05 13:52:32 +01001862void Map::LookupInDescriptors(JSObject* holder,
1863 String* name,
1864 LookupResult* result) {
1865 DescriptorArray* descriptors = instance_descriptors();
Steve Block44f0eee2011-05-26 01:26:41 +01001866 DescriptorLookupCache* cache = heap()->isolate()->descriptor_lookup_cache();
1867 int number = cache->Lookup(descriptors, name);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001868 if (number == DescriptorLookupCache::kAbsent) {
1869 number = descriptors->Search(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001870 cache->Update(descriptors, name, number);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001871 }
1872 if (number != DescriptorArray::kNotFound) {
1873 result->DescriptorResult(holder, descriptors->GetDetails(number), number);
1874 } else {
1875 result->NotFound();
1876 }
1877}
1878
1879
Steve Block44f0eee2011-05-26 01:26:41 +01001880MaybeObject* Map::GetExternalArrayElementsMap(ExternalArrayType array_type,
1881 bool safe_to_add_transition) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001882 Heap* current_heap = heap();
Steve Block44f0eee2011-05-26 01:26:41 +01001883 DescriptorArray* descriptors = instance_descriptors();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001884 String* external_array_sentinel_name = current_heap->empty_symbol();
Steve Block44f0eee2011-05-26 01:26:41 +01001885
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 Murdoch8b112d22011-06-08 16:22:53 +01001892 DescriptorLookupCache* cache =
1893 current_heap->isolate()->descriptor_lookup_cache();
Steve Block44f0eee2011-05-26 01:26:41 +01001894 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 Blocka7e24c12009-10-30 11:49:00 +00001953void 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 Popescu402d9372010-02-26 13:31:12 +00001964 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 Blocka7e24c12009-10-30 11:49:00 +00001969 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 Blocka7e24c12009-10-30 11:49:00 +00001989 }
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 Blocka7e24c12009-10-30 11:49:00 +00001996 }
1997 result->NotFound();
1998}
1999
2000
2001void JSObject::LookupRealNamedProperty(String* name, LookupResult* result) {
2002 LocalLookupRealNamedProperty(name, result);
2003 if (result->IsProperty()) return;
2004
2005 LookupRealNamedPropertyInPrototypes(name, result);
2006}
2007
2008
2009void JSObject::LookupRealNamedPropertyInPrototypes(String* name,
2010 LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01002011 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002012 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002013 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 pt = JSObject::cast(pt)->GetPrototype()) {
2015 JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002016 if (result->IsProperty() && (result->type() != INTERCEPTOR)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002017 }
2018 result->NotFound();
2019}
2020
2021
2022// We only need to deal with CALLBACKS and INTERCEPTORS
John Reck59135872010-11-02 12:39:01 -07002023MaybeObject* JSObject::SetPropertyWithFailedAccessCheck(LookupResult* result,
2024 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01002025 Object* value,
2026 bool check_prototype) {
2027 if (check_prototype && !result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002028 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 Murdoch086aeea2011-05-13 15:57:08 +01002053 return SetPropertyWithFailedAccessCheck(&r, name, value,
2054 check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00002055 }
2056 break;
2057 }
2058 default: {
2059 break;
2060 }
2061 }
2062 }
2063 }
2064
Iain Merrick75681382010-08-19 15:07:18 +01002065 HandleScope scope;
2066 Handle<Object> value_handle(value);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002067 Heap* heap = GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01002068 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_SET);
Iain Merrick75681382010-08-19 15:07:18 +01002069 return *value_handle;
Steve Blocka7e24c12009-10-30 11:49:00 +00002070}
2071
2072
John Reck59135872010-11-02 12:39:01 -07002073MaybeObject* JSObject::SetProperty(LookupResult* result,
2074 String* name,
2075 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002076 PropertyAttributes attributes,
2077 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002078 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002079 // Make sure that the top context does not change when doing callbacks or
2080 // interceptor calls.
2081 AssertNoContextChange ncc;
2082
Steve Blockd0582a62009-12-15 09:54:21 +00002083 // 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 Reck59135872010-11-02 12:39:01 -07002087 Object* symbol_version;
Steve Block44f0eee2011-05-26 01:26:41 +01002088 { MaybeObject* maybe_symbol_version = heap->LookupSymbol(name);
John Reck59135872010-11-02 12:39:01 -07002089 if (maybe_symbol_version->ToObject(&symbol_version)) {
2090 name = String::cast(symbol_version);
2091 }
2092 }
Steve Blockd0582a62009-12-15 09:54:21 +00002093 }
2094
Steve Blocka7e24c12009-10-30 11:49:00 +00002095 // Check access rights if needed.
2096 if (IsAccessCheckNeeded()
Steve Block44f0eee2011-05-26 01:26:41 +01002097 && !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_SET)) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002098 return SetPropertyWithFailedAccessCheck(result, name, value, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00002099 }
2100
2101 if (IsJSGlobalProxy()) {
2102 Object* proto = GetPrototype();
2103 if (proto->IsNull()) return value;
2104 ASSERT(proto->IsJSGlobalObject());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002105 return JSObject::cast(proto)->SetProperty(
2106 result, name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002107 }
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 Popescu402d9372010-02-26 13:31:12 +00002114 if (accessor_result.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002115 return SetPropertyWithCallback(accessor_result.GetCallbackObject(),
2116 name,
2117 value,
2118 accessor_result.holder());
2119 }
2120 }
Andrei Popescu402d9372010-02-26 13:31:12 +00002121 if (!result->IsFound()) {
2122 // Neither properties nor transitions found.
Steve Block44f0eee2011-05-26 01:26:41 +01002123 return AddProperty(name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002124 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002125 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 Block44f0eee2011-05-26 01:26:41 +01002131 return heap->isolate()->Throw(*heap->isolate()->factory()->NewTypeError(
2132 "strict_read_only_property", HandleVector(args, 2)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002133 } else {
2134 return value;
2135 }
2136 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 // 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 Murdoche0cee9b2011-05-25 10:26:03 +01002164 return SetPropertyWithInterceptor(name, value, attributes, strict_mode);
Iain Merrick75681382010-08-19 15:07:18 +01002165 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 Block44f0eee2011-05-26 01:26:41 +01002175 ASSERT(!HEAP->InNewSpace(function));
Iain Merrick75681382010-08-19 15:07:18 +01002176 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 Blocka7e24c12009-10-30 11:49:00 +00002182 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
Iain Merrick75681382010-08-19 15:07:18 +01002183 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002184 case NULL_DESCRIPTOR:
Steve Block44f0eee2011-05-26 01:26:41 +01002185 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 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 Murdoch086aeea2011-05-13 15:57:08 +01002201MaybeObject* JSObject::SetLocalPropertyIgnoreAttributes(
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 String* name,
2203 Object* value,
2204 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01002205
Steve Blocka7e24c12009-10-30 11:49:00 +00002206 // Make sure that the top context does not change when doing callbacks or
2207 // interceptor calls.
2208 AssertNoContextChange ncc;
Andrei Popescu402d9372010-02-26 13:31:12 +00002209 LookupResult result;
2210 LocalLookup(name, &result);
Steve Blocka7e24c12009-10-30 11:49:00 +00002211 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002212 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 Blocka7e24c12009-10-30 11:49:00 +00002217 }
2218
2219 if (IsJSGlobalProxy()) {
2220 Object* proto = GetPrototype();
2221 if (proto->IsNull()) return value;
2222 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch086aeea2011-05-13 15:57:08 +01002223 return JSObject::cast(proto)->SetLocalPropertyIgnoreAttributes(
Steve Blocka7e24c12009-10-30 11:49:00 +00002224 name,
2225 value,
2226 attributes);
2227 }
2228
2229 // Check for accessor in prototype chain removed here in clone.
Andrei Popescu402d9372010-02-26 13:31:12 +00002230 if (!result.IsFound()) {
2231 // Neither properties nor transitions found.
Steve Block44f0eee2011-05-26 01:26:41 +01002232 return AddProperty(name, value, attributes, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002233 }
Steve Block6ded16b2010-05-10 14:33:55 +01002234
Andrei Popescu402d9372010-02-26 13:31:12 +00002235 PropertyDetails details = PropertyDetails(attributes, NORMAL);
2236
Steve Blocka7e24c12009-10-30 11:49:00 +00002237 // Check of IsReadOnly removed from here in clone.
Andrei Popescu402d9372010-02-26 13:31:12 +00002238 switch (result.type()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002239 case NORMAL:
Andrei Popescu402d9372010-02-26 13:31:12 +00002240 return SetNormalizedProperty(name, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002241 case FIELD:
Andrei Popescu402d9372010-02-26 13:31:12 +00002242 return FastPropertyAtPut(result.GetFieldIndex(), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002243 case MAP_TRANSITION:
Andrei Popescu402d9372010-02-26 13:31:12 +00002244 if (attributes == result.GetAttributes()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002245 // Only use map transition if the attributes match.
Andrei Popescu402d9372010-02-26 13:31:12 +00002246 return AddFastPropertyUsingMap(result.GetTransitionMap(),
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 name,
2248 value);
2249 }
2250 return ConvertDescriptorToField(name, value, attributes);
2251 case CONSTANT_FUNCTION:
2252 // Only replace the function if necessary.
Andrei Popescu402d9372010-02-26 13:31:12 +00002253 if (value == result.GetConstantFunction()) return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00002254 // Preserve the attributes of this existing property.
Andrei Popescu402d9372010-02-26 13:31:12 +00002255 attributes = result.GetAttributes();
Steve Blocka7e24c12009-10-30 11:49:00 +00002256 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 Block44f0eee2011-05-26 01:26:41 +01002266 case EXTERNAL_ARRAY_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002267 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
2268 default:
2269 UNREACHABLE();
2270 }
2271 UNREACHABLE();
2272 return value;
2273}
2274
2275
2276PropertyAttributes 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 Block44f0eee2011-05-26 01:26:41 +01002288 if (!pt->IsNull()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002289 return JSObject::cast(pt)->
2290 GetPropertyAttributeWithReceiver(receiver, name);
2291 }
2292 }
2293 return ABSENT;
2294}
2295
2296
2297PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor(
2298 JSObject* receiver,
2299 String* name,
2300 bool continue_search) {
Steve Block44f0eee2011-05-26 01:26:41 +01002301 Isolate* isolate = GetIsolate();
2302
Steve Blocka7e24c12009-10-30 11:49:00 +00002303 // Make sure that the top context does not change when doing
2304 // callbacks or interceptor calls.
2305 AssertNoContextChange ncc;
2306
Steve Block44f0eee2011-05-26 01:26:41 +01002307 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002308 Handle<InterceptorInfo> interceptor(GetNamedInterceptor());
2309 Handle<JSObject> receiver_handle(receiver);
2310 Handle<JSObject> holder_handle(this);
2311 Handle<String> name_handle(name);
Steve Block44f0eee2011-05-26 01:26:41 +01002312 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002313 v8::AccessorInfo info(args.end());
2314 if (!interceptor->query()->IsUndefined()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002315 v8::NamedPropertyQuery query =
2316 v8::ToCData<v8::NamedPropertyQuery>(interceptor->query());
Steve Block44f0eee2011-05-26 01:26:41 +01002317 LOG(isolate,
2318 ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002319 v8::Handle<v8::Integer> result;
Steve Blocka7e24c12009-10-30 11:49:00 +00002320 {
2321 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002322 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00002323 result = query(v8::Utils::ToLocal(name_handle), info);
2324 }
2325 if (!result.IsEmpty()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002326 ASSERT(result->IsInt32());
2327 return static_cast<PropertyAttributes>(result->Int32Value());
Steve Blocka7e24c12009-10-30 11:49:00 +00002328 }
2329 } else if (!interceptor->getter()->IsUndefined()) {
2330 v8::NamedPropertyGetter getter =
2331 v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01002332 LOG(isolate,
2333 ApiNamedPropertyAccess("interceptor-named-get-has", this, name));
Steve Blocka7e24c12009-10-30 11:49:00 +00002334 v8::Handle<v8::Value> result;
2335 {
2336 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002337 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00002338 result = getter(v8::Utils::ToLocal(name_handle), info);
2339 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002340 if (!result.IsEmpty()) return DONT_ENUM;
Steve Blocka7e24c12009-10-30 11:49:00 +00002341 }
2342 return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle,
2343 *name_handle,
2344 continue_search);
2345}
2346
2347
2348PropertyAttributes 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
2363PropertyAttributes JSObject::GetPropertyAttribute(JSObject* receiver,
2364 LookupResult* result,
2365 String* name,
2366 bool continue_search) {
2367 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002368 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 Blocka7e24c12009-10-30 11:49:00 +00002376 }
Andrei Popescu402d9372010-02-26 13:31:12 +00002377 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002378 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 Blocka7e24c12009-10-30 11:49:00 +00002387 default:
2388 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00002389 }
2390 }
2391 return ABSENT;
2392}
2393
2394
2395PropertyAttributes 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 Reck59135872010-11-02 12:39:01 -07002409MaybeObject* NormalizedMapCache::Get(JSObject* obj,
2410 PropertyNormalizationMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002411 Isolate* isolate = obj->GetIsolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002412 Map* fast = obj->map();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002413 int index = Hash(fast) % kEntries;
2414 Object* result = get(index);
2415 if (result->IsMap() && CheckHit(Map::cast(result), fast, mode)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002416#ifdef DEBUG
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002417 if (FLAG_enable_slow_asserts) {
2418 // The cached map should match newly created normalized map bit-by-bit.
John Reck59135872010-11-02 12:39:01 -07002419 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 Monsen80d68ea2010-09-08 11:05:35 +01002427 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002428 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002429#endif
2430 return result;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002431 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002432
John Reck59135872010-11-02 12:39:01 -07002433 { MaybeObject* maybe_result =
2434 fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP);
2435 if (!maybe_result->ToObject(&result)) return maybe_result;
2436 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002437 set(index, result);
Steve Block44f0eee2011-05-26 01:26:41 +01002438 isolate->counters()->normalized_maps()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002439
2440 return result;
2441}
2442
2443
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002444void NormalizedMapCache::Clear() {
2445 int entries = length();
2446 for (int i = 0; i != entries; i++) {
2447 set_undefined(i);
2448 }
2449}
2450
2451
2452int 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
2470bool NormalizedMapCache::CheckHit(Map* slow,
2471 Map* fast,
2472 PropertyNormalizationMode mode) {
2473#ifdef DEBUG
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002474 slow->SharedMapVerify();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002475#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 Monsen0d5e1162010-09-30 15:31:59 +01002484 (slow->bit_field2() & ~(1<<Map::kIsShared)) == fast->bit_field2();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002485}
2486
2487
John Reck59135872010-11-02 12:39:01 -07002488MaybeObject* JSObject::UpdateMapCodeCache(String* name, Code* code) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002489 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 Reck59135872010-11-02 12:39:01 -07002493 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 Block44f0eee2011-05-26 01:26:41 +01002498 GetIsolate()->counters()->normalized_maps()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002499
2500 set_map(Map::cast(obj));
2501 }
2502 return map()->UpdateCodeCache(name, code);
2503}
2504
2505
John Reck59135872010-11-02 12:39:01 -07002506MaybeObject* JSObject::NormalizeProperties(PropertyNormalizationMode mode,
2507 int expected_additional_properties) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002508 if (!HasFastProperties()) return this;
2509
2510 // The global object is always normalized.
2511 ASSERT(!IsGlobalObject());
Steve Block1e0659c2011-05-24 12:43:12 +01002512 // JSGlobalProxy must never be normalized
2513 ASSERT(!IsJSGlobalProxy());
2514
Ben Murdoch8b112d22011-06-08 16:22:53 +01002515 Map* map_of_this = map();
Steve Block44f0eee2011-05-26 01:26:41 +01002516
Steve Blocka7e24c12009-10-30 11:49:00 +00002517 // Allocate new content.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002518 int property_count = map_of_this->NumberOfDescribedProperties();
Steve Blocka7e24c12009-10-30 11:49:00 +00002519 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 Reck59135872010-11-02 12:39:01 -07002524 Object* obj;
2525 { MaybeObject* maybe_obj =
2526 StringDictionary::Allocate(property_count);
2527 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2528 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002529 StringDictionary* dictionary = StringDictionary::cast(obj);
2530
Ben Murdoch8b112d22011-06-08 16:22:53 +01002531 DescriptorArray* descs = map_of_this->instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00002532 for (int i = 0; i < descs->number_of_descriptors(); i++) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002533 PropertyDetails details(descs->GetDetails(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00002534 switch (details.type()) {
2535 case CONSTANT_FUNCTION: {
2536 PropertyDetails d =
2537 PropertyDetails(details.attributes(), NORMAL, details.index());
2538 Object* value = descs->GetConstantFunction(i);
John Reck59135872010-11-02 12:39:01 -07002539 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 Blocka7e24c12009-10-30 11:49:00 +00002544 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 Reck59135872010-11-02 12:39:01 -07002551 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 Blocka7e24c12009-10-30 11:49:00 +00002556 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 Reck59135872010-11-02 12:39:01 -07002563 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 Blocka7e24c12009-10-30 11:49:00 +00002568 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 Murdoch8b112d22011-06-08 16:22:53 +01002581 Heap* current_heap = map_of_this->heap();
2582
Steve Blocka7e24c12009-10-30 11:49:00 +00002583 // Copy the next enumeration index from instance descriptor.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002584 int index = map_of_this->instance_descriptors()->NextEnumerationIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00002585 dictionary->SetNextEnumerationIndex(index);
2586
Ben Murdoch8b112d22011-06-08 16:22:53 +01002587 { MaybeObject* maybe_obj =
2588 current_heap->isolate()->context()->global_context()->
Steve Block44f0eee2011-05-26 01:26:41 +01002589 normalized_map_cache()->Get(this, mode);
John Reck59135872010-11-02 12:39:01 -07002590 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2591 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002592 Map* new_map = Map::cast(obj);
2593
Steve Blocka7e24c12009-10-30 11:49:00 +00002594 // 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 Monsen80d68ea2010-09-08 11:05:35 +01002596
2597 // Resize the object in the heap if necessary.
2598 int new_instance_size = new_map->instance_size();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002599 int instance_size_delta = map_of_this->instance_size() - new_instance_size;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002600 ASSERT(instance_size_delta >= 0);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002601 current_heap->CreateFillerObjectAt(this->address() + new_instance_size,
2602 instance_size_delta);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002603
Steve Blocka7e24c12009-10-30 11:49:00 +00002604 set_map(new_map);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002605 new_map->set_instance_descriptors(current_heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +00002606
2607 set_properties(dictionary);
2608
Ben Murdoch8b112d22011-06-08 16:22:53 +01002609 current_heap->isolate()->counters()->props_to_dictionary()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +00002610
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 Reck59135872010-11-02 12:39:01 -07002621MaybeObject* JSObject::TransformToFastProperties(int unused_property_fields) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002622 if (HasFastProperties()) return this;
2623 ASSERT(!IsGlobalObject());
2624 return property_dictionary()->
2625 TransformPropertiesToFastFor(this, unused_property_fields);
2626}
2627
2628
John Reck59135872010-11-02 12:39:01 -07002629MaybeObject* JSObject::NormalizeElements() {
Steve Block44f0eee2011-05-26 01:26:41 +01002630 ASSERT(!HasExternalArrayElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00002631 if (HasDictionaryElements()) return this;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002632 Map* old_map = map();
2633 ASSERT(old_map->has_fast_elements());
Steve Block8defd9f2010-07-08 12:39:36 +01002634
John Reck59135872010-11-02 12:39:01 -07002635 Object* obj;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002636 { MaybeObject* maybe_obj = old_map->GetSlowElementsMap();
John Reck59135872010-11-02 12:39:01 -07002637 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2638 }
Steve Block8defd9f2010-07-08 12:39:36 +01002639 Map* new_map = Map::cast(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002640
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 Reck59135872010-11-02 12:39:01 -07002648 { MaybeObject* maybe_obj = NumberDictionary::Allocate(length);
2649 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2650 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002651 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 Reck59135872010-11-02 12:39:01 -07002657 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 Blocka7e24c12009-10-30 11:49:00 +00002662 dictionary = NumberDictionary::cast(result);
2663 }
2664 }
Steve Block8defd9f2010-07-08 12:39:36 +01002665 // 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 Blocka7e24c12009-10-30 11:49:00 +00002669 set_elements(dictionary);
2670
Ben Murdoch8b112d22011-06-08 16:22:53 +01002671 new_map->heap()->isolate()->counters()->elements_to_dictionary()->
Steve Block44f0eee2011-05-26 01:26:41 +01002672 Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +00002673
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 Reck59135872010-11-02 12:39:01 -07002685MaybeObject* JSObject::DeletePropertyPostInterceptor(String* name,
2686 DeleteMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002687 // Check local property, ignore interceptor.
2688 LookupResult result;
2689 LocalLookupRealNamedProperty(name, &result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002690 if (!result.IsProperty()) return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002691
2692 // Normalize object if needed.
John Reck59135872010-11-02 12:39:01 -07002693 Object* obj;
2694 { MaybeObject* maybe_obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
2695 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2696 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002697
2698 return DeleteNormalizedProperty(name, mode);
2699}
2700
2701
John Reck59135872010-11-02 12:39:01 -07002702MaybeObject* JSObject::DeletePropertyWithInterceptor(String* name) {
Steve Block44f0eee2011-05-26 01:26:41 +01002703 Isolate* isolate = GetIsolate();
2704 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002705 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 Block44f0eee2011-05-26 01:26:41 +01002711 LOG(isolate,
2712 ApiNamedPropertyAccess("interceptor-named-delete", *this_handle, name));
2713 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002714 v8::AccessorInfo info(args.end());
2715 v8::Handle<v8::Boolean> result;
2716 {
2717 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002718 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00002719 result = deleter(v8::Utils::ToLocal(name_handle), info);
2720 }
Steve Block44f0eee2011-05-26 01:26:41 +01002721 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002722 if (!result.IsEmpty()) {
2723 ASSERT(result->IsBoolean());
2724 return *v8::Utils::OpenHandle(*result);
2725 }
2726 }
John Reck59135872010-11-02 12:39:01 -07002727 MaybeObject* raw_result =
Steve Blocka7e24c12009-10-30 11:49:00 +00002728 this_handle->DeletePropertyPostInterceptor(*name_handle, NORMAL_DELETION);
Steve Block44f0eee2011-05-26 01:26:41 +01002729 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002730 return raw_result;
2731}
2732
2733
John Reck59135872010-11-02 12:39:01 -07002734MaybeObject* JSObject::DeleteElementPostInterceptor(uint32_t index,
2735 DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002736 ASSERT(!HasExternalArrayElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00002737 switch (GetElementsKind()) {
2738 case FAST_ELEMENTS: {
John Reck59135872010-11-02 12:39:01 -07002739 Object* obj;
2740 { MaybeObject* maybe_obj = EnsureWritableFastElements();
2741 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2742 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002743 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 Murdoch8b112d22011-06-08 16:22:53 +01002763 return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002764}
2765
2766
John Reck59135872010-11-02 12:39:01 -07002767MaybeObject* JSObject::DeleteElementWithInterceptor(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01002768 Isolate* isolate = GetIsolate();
2769 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002770 // Make sure that the top context does not change when doing
2771 // callbacks or interceptor calls.
2772 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01002773 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002774 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +01002775 if (interceptor->deleter()->IsUndefined()) return heap->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002776 v8::IndexedPropertyDeleter deleter =
2777 v8::ToCData<v8::IndexedPropertyDeleter>(interceptor->deleter());
2778 Handle<JSObject> this_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01002779 LOG(isolate,
2780 ApiIndexedPropertyAccess("interceptor-indexed-delete", this, index));
2781 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00002782 v8::AccessorInfo info(args.end());
2783 v8::Handle<v8::Boolean> result;
2784 {
2785 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002786 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00002787 result = deleter(index, info);
2788 }
Steve Block44f0eee2011-05-26 01:26:41 +01002789 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002790 if (!result.IsEmpty()) {
2791 ASSERT(result->IsBoolean());
2792 return *v8::Utils::OpenHandle(*result);
2793 }
John Reck59135872010-11-02 12:39:01 -07002794 MaybeObject* raw_result =
Steve Blocka7e24c12009-10-30 11:49:00 +00002795 this_handle->DeleteElementPostInterceptor(index, NORMAL_DELETION);
Steve Block44f0eee2011-05-26 01:26:41 +01002796 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002797 return raw_result;
2798}
2799
2800
John Reck59135872010-11-02 12:39:01 -07002801MaybeObject* JSObject::DeleteElement(uint32_t index, DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002802 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002803 // Check access rights if needed.
2804 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01002805 !isolate->MayIndexedAccess(this, index, v8::ACCESS_DELETE)) {
2806 isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE);
2807 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002808 }
2809
2810 if (IsJSGlobalProxy()) {
2811 Object* proto = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002812 if (proto->IsNull()) return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002813 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 Reck59135872010-11-02 12:39:01 -07002827 Object* obj;
2828 { MaybeObject* maybe_obj = EnsureWritableFastElements();
2829 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2830 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002831 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 Block44f0eee2011-05-26 01:26:41 +01002839 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00002840 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 Blocka7e24c12009-10-30 11:49:00 +00002849 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00002850 case DICTIONARY_ELEMENTS: {
2851 NumberDictionary* dictionary = element_dictionary();
2852 int entry = dictionary->FindEntry(index);
2853 if (entry != NumberDictionary::kNotFound) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002854 Object* result = dictionary->DeleteProperty(entry, mode);
Steve Block44f0eee2011-05-26 01:26:41 +01002855 if (mode == STRICT_DELETION && result ==
2856 isolate->heap()->false_value()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002857 // 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 Block44f0eee2011-05-26 01:26:41 +01002861 Handle<Object> i = isolate->factory()->NewNumberFromUint(index);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002862 Handle<Object> args[2] = { i, Handle<Object>(this) };
Steve Block44f0eee2011-05-26 01:26:41 +01002863 return isolate->Throw(*isolate->factory()->NewTypeError(
2864 "strict_delete_property", HandleVector(args, 2)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002865 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002866 }
2867 break;
2868 }
2869 default:
2870 UNREACHABLE();
2871 break;
2872 }
Steve Block44f0eee2011-05-26 01:26:41 +01002873 return isolate->heap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002874}
2875
2876
John Reck59135872010-11-02 12:39:01 -07002877MaybeObject* JSObject::DeleteProperty(String* name, DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002878 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002879 // ECMA-262, 3rd, 8.6.2.5
2880 ASSERT(name->IsString());
2881
2882 // Check access rights if needed.
2883 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01002884 !isolate->MayNamedAccess(this, name, v8::ACCESS_DELETE)) {
2885 isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE);
2886 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002887 }
2888
2889 if (IsJSGlobalProxy()) {
2890 Object* proto = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002891 if (proto->IsNull()) return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002892 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 Block44f0eee2011-05-26 01:26:41 +01002902 if (!result.IsProperty()) return isolate->heap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002903 // Ignore attributes if forcing a deletion.
2904 if (result.IsDontDelete() && mode != FORCE_DELETION) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002905 if (mode == STRICT_DELETION) {
2906 // Deleting a non-configurable property in strict mode.
Steve Block44f0eee2011-05-26 01:26:41 +01002907 HandleScope scope(isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002908 Handle<Object> args[2] = { Handle<Object>(name), Handle<Object>(this) };
Steve Block44f0eee2011-05-26 01:26:41 +01002909 return isolate->Throw(*isolate->factory()->NewTypeError(
2910 "strict_delete_property", HandleVector(args, 2)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002911 }
Steve Block44f0eee2011-05-26 01:26:41 +01002912 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002913 }
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 Blocka7e24c12009-10-30 11:49:00 +00002922 // Normalize object if needed.
John Reck59135872010-11-02 12:39:01 -07002923 Object* obj;
2924 { MaybeObject* maybe_obj =
2925 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
2926 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
2927 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002928 // 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.
2935bool JSObject::ReferencesObject(Object* obj) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002936 Map* map_of_this = map();
2937 Heap* heap = map_of_this->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002938 AssertNoAllocation no_alloc;
2939
2940 // Is the object the constructor for this object?
Ben Murdoch8b112d22011-06-08 16:22:53 +01002941 if (map_of_this->constructor() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002942 return true;
2943 }
2944
2945 // Is the object the prototype for this object?
Ben Murdoch8b112d22011-06-08 16:22:53 +01002946 if (map_of_this->prototype() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002947 return true;
2948 }
2949
2950 // Check if the object is among the named properties.
2951 Object* key = SlowReverseLookup(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01002952 if (!key->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002953 return true;
2954 }
2955
2956 // Check if the object is among the indexed properties.
2957 switch (GetElementsKind()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002958 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00002959 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 Blocka7e24c12009-10-30 11:49:00 +00002968 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 Block44f0eee2011-05-26 01:26:41 +01002983 if (!key->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002984 return true;
2985 }
2986 break;
2987 }
2988 default:
2989 UNREACHABLE();
2990 break;
2991 }
2992
Steve Block6ded16b2010-05-10 14:33:55 +01002993 // For functions check the context.
2994 if (IsJSFunction()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002995 // Get the constructor function for arguments array.
2996 JSObject* arguments_boilerplate =
Steve Block44f0eee2011-05-26 01:26:41 +01002997 heap->isolate()->context()->global_context()->
2998 arguments_boilerplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00002999 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 Reck59135872010-11-02 12:39:01 -07003036MaybeObject* JSObject::PreventExtensions() {
Steve Block44f0eee2011-05-26 01:26:41 +01003037 Isolate* isolate = GetIsolate();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003038 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01003039 !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 Murdoche0cee9b2011-05-25 10:26:03 +01003044 }
3045
Steve Block1e0659c2011-05-24 12:43:12 +01003046 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 Block8defd9f2010-07-08 12:39:36 +01003053 // If there are fast elements we normalize.
3054 if (HasFastElements()) {
John Reck59135872010-11-02 12:39:01 -07003055 Object* ok;
3056 { MaybeObject* maybe_ok = NormalizeElements();
3057 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3058 }
Steve Block8defd9f2010-07-08 12:39:36 +01003059 }
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 Reck59135872010-11-02 12:39:01 -07003065 Object* new_map;
3066 { MaybeObject* maybe_new_map = map()->CopyDropTransitions();
3067 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
3068 }
Steve Block8defd9f2010-07-08 12:39:36 +01003069 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 Blocka7e24c12009-10-30 11:49:00 +00003076// Tests for the fast common case for property enumeration:
Steve Blockd0582a62009-12-15 09:54:21 +00003077// - 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 Blocka7e24c12009-10-30 11:49:00 +00003081bool JSObject::IsSimpleEnum() {
Steve Block44f0eee2011-05-26 01:26:41 +01003082 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003083 for (Object* o = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003084 o != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003085 o = JSObject::cast(o)->GetPrototype()) {
3086 JSObject* curr = JSObject::cast(o);
Steve Blocka7e24c12009-10-30 11:49:00 +00003087 if (!curr->map()->instance_descriptors()->HasEnumCache()) return false;
Steve Blockd0582a62009-12-15 09:54:21 +00003088 ASSERT(!curr->HasNamedInterceptor());
3089 ASSERT(!curr->HasIndexedInterceptor());
3090 ASSERT(!curr->IsAccessCheckNeeded());
Steve Blocka7e24c12009-10-30 11:49:00 +00003091 if (curr->NumberOfEnumElements() > 0) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003092 if (curr != this) {
3093 FixedArray* curr_fixed_array =
3094 FixedArray::cast(curr->map()->instance_descriptors()->GetEnumCache());
Steve Blockd0582a62009-12-15 09:54:21 +00003095 if (curr_fixed_array->length() > 0) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00003096 }
3097 }
3098 return true;
3099}
3100
3101
3102int 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
3112int 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
3123int 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
3136AccessorDescriptor* 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
3147void JSObject::LocalLookup(String* name, LookupResult* result) {
3148 ASSERT(name->IsString());
3149
Steve Block44f0eee2011-05-26 01:26:41 +01003150 Heap* heap = GetHeap();
3151
Steve Blocka7e24c12009-10-30 11:49:00 +00003152 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 Block44f0eee2011-05-26 01:26:41 +01003166 if (name->Equals(heap->Proto_symbol()) &&
3167 !IsJSContextExtensionObject()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003168 result->ConstantResult(this);
3169 return;
3170 }
3171
3172 // Check for lookup interceptor except when bootstrapping.
Steve Block44f0eee2011-05-26 01:26:41 +01003173 if (HasNamedInterceptor() && !heap->isolate()->bootstrapper()->IsActive()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003174 result->InterceptorResult(this);
3175 return;
3176 }
3177
3178 LocalLookupRealNamedProperty(name, result);
3179}
3180
3181
3182void JSObject::Lookup(String* name, LookupResult* result) {
3183 // Ecma-262 3rd 8.6.2.4
Steve Block44f0eee2011-05-26 01:26:41 +01003184 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003185 for (Object* current = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003186 current != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003187 current = JSObject::cast(current)->GetPrototype()) {
3188 JSObject::cast(current)->LocalLookup(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00003189 if (result->IsProperty()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00003190 }
3191 result->NotFound();
3192}
3193
3194
3195// Search object and it's prototype chain for callback properties.
3196void JSObject::LookupCallback(String* name, LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01003197 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003198 for (Object* current = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003199 current != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003200 current = JSObject::cast(current)->GetPrototype()) {
3201 JSObject::cast(current)->LocalLookupRealNamedProperty(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00003202 if (result->IsProperty() && result->type() == CALLBACKS) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00003203 }
3204 result->NotFound();
3205}
3206
3207
John Reck59135872010-11-02 12:39:01 -07003208MaybeObject* JSObject::DefineGetterSetter(String* name,
3209 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01003210 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003211 // Make sure that the top context does not change when doing callbacks or
3212 // interceptor calls.
3213 AssertNoContextChange ncc;
3214
Steve Blocka7e24c12009-10-30 11:49:00 +00003215 // Try to flatten before operating on the string.
Steve Block6ded16b2010-05-10 14:33:55 +01003216 name->TryFlatten();
Steve Blocka7e24c12009-10-30 11:49:00 +00003217
Leon Clarkef7060e22010-06-03 12:02:55 +01003218 if (!CanSetCallback(name)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003219 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003220 }
3221
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003222 uint32_t index = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003223 bool is_element = name->AsArrayIndex(&index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003224
3225 if (is_element) {
3226 switch (GetElementsKind()) {
3227 case FAST_ELEMENTS:
3228 break;
Steve Block44f0eee2011-05-26 01:26:41 +01003229 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00003230 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 Block44f0eee2011-05-26 01:26:41 +01003239 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003240 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 Block44f0eee2011-05-26 01:26:41 +01003247 if (details.IsReadOnly()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003248 if (details.type() == CALLBACKS) {
Leon Clarkef7060e22010-06-03 12:02:55 +01003249 if (result->IsFixedArray()) {
3250 return result;
3251 }
3252 // Otherwise allow to override it.
Steve Blocka7e24c12009-10-30 11:49:00 +00003253 }
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 Popescu402d9372010-02-26 13:31:12 +00003265 if (result.IsProperty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003266 if (result.IsReadOnly()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003267 if (result.type() == CALLBACKS) {
3268 Object* obj = result.GetCallbackObject();
Leon Clarkef7060e22010-06-03 12:02:55 +01003269 // Need to preserve old getters/setters.
Leon Clarked91b9f72010-01-27 17:25:45 +00003270 if (obj->IsFixedArray()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01003271 // Use set to update attributes.
3272 return SetPropertyCallback(name, obj, attributes);
Leon Clarked91b9f72010-01-27 17:25:45 +00003273 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003274 }
3275 }
3276 }
3277
3278 // Allocate the fixed array to hold getter and setter.
John Reck59135872010-11-02 12:39:01 -07003279 Object* structure;
Steve Block44f0eee2011-05-26 01:26:41 +01003280 { MaybeObject* maybe_structure = heap->AllocateFixedArray(2, TENURED);
John Reck59135872010-11-02 12:39:01 -07003281 if (!maybe_structure->ToObject(&structure)) return maybe_structure;
3282 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003283
3284 if (is_element) {
Leon Clarkef7060e22010-06-03 12:02:55 +01003285 return SetElementCallback(index, structure, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00003286 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01003287 return SetPropertyCallback(name, structure, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00003288 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003289}
3290
3291
3292bool JSObject::CanSetCallback(String* name) {
3293 ASSERT(!IsAccessCheckNeeded()
Steve Block44f0eee2011-05-26 01:26:41 +01003294 || Isolate::Current()->MayNamedAccess(this, name, v8::ACCESS_SET));
Leon Clarkef7060e22010-06-03 12:02:55 +01003295
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 Reck59135872010-11-02 12:39:01 -07003316MaybeObject* JSObject::SetElementCallback(uint32_t index,
3317 Object* structure,
3318 PropertyAttributes attributes) {
Leon Clarkef7060e22010-06-03 12:02:55 +01003319 PropertyDetails details = PropertyDetails(attributes, CALLBACKS);
3320
3321 // Normalize elements to make this operation simple.
John Reck59135872010-11-02 12:39:01 -07003322 Object* ok;
3323 { MaybeObject* maybe_ok = NormalizeElements();
3324 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3325 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003326
3327 // Update the dictionary with the new CALLBACKS property.
John Reck59135872010-11-02 12:39:01 -07003328 Object* dict;
3329 { MaybeObject* maybe_dict =
3330 element_dictionary()->Set(index, structure, details);
3331 if (!maybe_dict->ToObject(&dict)) return maybe_dict;
3332 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003333
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 Blocka7e24c12009-10-30 11:49:00 +00003338
3339 return structure;
3340}
3341
3342
John Reck59135872010-11-02 12:39:01 -07003343MaybeObject* JSObject::SetPropertyCallback(String* name,
3344 Object* structure,
3345 PropertyAttributes attributes) {
Leon Clarkef7060e22010-06-03 12:02:55 +01003346 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 Reck59135872010-11-02 12:39:01 -07003353 Object* ok;
3354 { MaybeObject* maybe_ok = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
3355 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3356 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003357
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 Reck59135872010-11-02 12:39:01 -07003361 Object* new_map;
3362 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
3363 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
3364 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003365 set_map(Map::cast(new_map));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003366 // 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 Clarkef7060e22010-06-03 12:02:55 +01003370 }
3371
3372 // Update the dictionary with the new CALLBACKS property.
John Reck59135872010-11-02 12:39:01 -07003373 Object* result;
3374 { MaybeObject* maybe_result = SetNormalizedProperty(name, structure, details);
3375 if (!maybe_result->ToObject(&result)) return maybe_result;
3376 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003377
3378 if (convert_back_to_fast) {
John Reck59135872010-11-02 12:39:01 -07003379 { MaybeObject* maybe_ok = TransformToFastProperties(0);
3380 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3381 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003382 }
3383 return result;
3384}
3385
John Reck59135872010-11-02 12:39:01 -07003386MaybeObject* JSObject::DefineAccessor(String* name,
3387 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003388 Object* fun,
John Reck59135872010-11-02 12:39:01 -07003389 PropertyAttributes attributes) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01003390 ASSERT(fun->IsJSFunction() || fun->IsUndefined());
Steve Block44f0eee2011-05-26 01:26:41 +01003391 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00003392 // Check access rights if needed.
3393 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01003394 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
3395 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
3396 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003397 }
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 Reck59135872010-11-02 12:39:01 -07003407 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 Blocka7e24c12009-10-30 11:49:00 +00003412 FixedArray::cast(array)->set(is_getter ? 0 : 1, fun);
3413 return this;
3414}
3415
3416
John Reck59135872010-11-02 12:39:01 -07003417MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +01003418 Isolate* isolate = GetIsolate();
Leon Clarkef7060e22010-06-03 12:02:55 +01003419 String* name = String::cast(info->name());
3420 // Check access rights if needed.
3421 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01003422 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
3423 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
3424 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01003425 }
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 Block44f0eee2011-05-26 01:26:41 +01003442 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01003443 }
3444
3445 uint32_t index = 0;
3446 bool is_element = name->AsArrayIndex(&index);
3447
3448 if (is_element) {
Steve Block44f0eee2011-05-26 01:26:41 +01003449 if (IsJSArray()) return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01003450
3451 // Accessors overwrite previous callbacks (cf. with getters/setters).
3452 switch (GetElementsKind()) {
3453 case FAST_ELEMENTS:
3454 break;
Steve Block44f0eee2011-05-26 01:26:41 +01003455 case EXTERNAL_PIXEL_ELEMENTS:
Leon Clarkef7060e22010-06-03 12:02:55 +01003456 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 Block44f0eee2011-05-26 01:26:41 +01003465 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01003466 case DICTIONARY_ELEMENTS:
3467 break;
3468 default:
3469 UNREACHABLE();
3470 break;
3471 }
3472
John Reck59135872010-11-02 12:39:01 -07003473 Object* ok;
3474 { MaybeObject* maybe_ok =
3475 SetElementCallback(index, info, info->property_attributes());
3476 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3477 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003478 } 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 Block44f0eee2011-05-26 01:26:41 +01003485 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01003486 }
John Reck59135872010-11-02 12:39:01 -07003487 Object* ok;
3488 { MaybeObject* maybe_ok =
3489 SetPropertyCallback(name, info, info->property_attributes());
3490 if (!maybe_ok->ToObject(&ok)) return maybe_ok;
3491 }
Leon Clarkef7060e22010-06-03 12:02:55 +01003492 }
3493
3494 return this;
3495}
3496
3497
Steve Blocka7e24c12009-10-30 11:49:00 +00003498Object* JSObject::LookupAccessor(String* name, bool is_getter) {
Steve Block44f0eee2011-05-26 01:26:41 +01003499 Heap* heap = GetHeap();
3500
Steve Blocka7e24c12009-10-30 11:49:00 +00003501 // 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 Block44f0eee2011-05-26 01:26:41 +01003507 !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_HAS)) {
3508 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
3509 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003510 }
3511
3512 // Make the lookup and include prototypes.
3513 int accessor_index = is_getter ? kGetterIndex : kSetterIndex;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003514 uint32_t index = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003515 if (name->AsArrayIndex(&index)) {
3516 for (Object* obj = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003517 obj != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003518 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 Clarkef7060e22010-06-03 12:02:55 +01003527 if (element->IsFixedArray()) {
3528 return FixedArray::cast(element)->get(accessor_index);
3529 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003530 }
3531 }
3532 }
3533 }
3534 } else {
3535 for (Object* obj = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003536 obj != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003537 obj = JSObject::cast(obj)->GetPrototype()) {
3538 LookupResult result;
3539 JSObject::cast(obj)->LocalLookup(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00003540 if (result.IsProperty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01003541 if (result.IsReadOnly()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003542 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 Block44f0eee2011-05-26 01:26:41 +01003551 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003552}
3553
3554
3555Object* 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 Murdoch8b112d22011-06-08 16:22:53 +01003569 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003570 } else {
3571 return property_dictionary()->SlowReverseLookup(value);
3572 }
3573}
3574
3575
John Reck59135872010-11-02 12:39:01 -07003576MaybeObject* Map::CopyDropDescriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01003577 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -07003578 Object* result;
3579 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01003580 heap->AllocateMap(instance_type(), instance_size());
John Reck59135872010-11-02 12:39:01 -07003581 if (!maybe_result->ToObject(&result)) return maybe_result;
3582 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003583 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 Block44f0eee2011-05-26 01:26:41 +01003590 Map::cast(result)->set_instance_descriptors(
3591 heap->empty_descriptor_array());
Steve Blocka7e24c12009-10-30 11:49:00 +00003592 // 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 Reck59135872010-11-02 12:39:01 -07003601 Object* descriptors;
3602 { MaybeObject* maybe_descriptors =
3603 ctor->initial_map()->instance_descriptors()->RemoveTransitions();
3604 if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors;
3605 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003606 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 Monsen0d5e1162010-09-30 15:31:59 +01003613 Map::cast(result)->set_is_shared(false);
Steve Block44f0eee2011-05-26 01:26:41 +01003614 Map::cast(result)->ClearCodeCache(heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003615 return result;
3616}
3617
3618
John Reck59135872010-11-02 12:39:01 -07003619MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode,
3620 NormalizedMapSharingMode sharing) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003621 int new_instance_size = instance_size();
3622 if (mode == CLEAR_INOBJECT_PROPERTIES) {
3623 new_instance_size -= inobject_properties() * kPointerSize;
3624 }
3625
John Reck59135872010-11-02 12:39:01 -07003626 Object* result;
3627 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01003628 GetHeap()->AllocateMap(instance_type(), new_instance_size);
John Reck59135872010-11-02 12:39:01 -07003629 if (!maybe_result->ToObject(&result)) return maybe_result;
3630 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003631
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 Monsen0d5e1162010-09-30 15:31:59 +01003642 Map::cast(result)->set_is_shared(sharing == SHARED_NORMALIZED_MAP);
3643
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003644#ifdef DEBUG
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003645 if (Map::cast(result)->is_shared()) {
3646 Map::cast(result)->SharedMapVerify();
3647 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003648#endif
3649
3650 return result;
3651}
3652
3653
John Reck59135872010-11-02 12:39:01 -07003654MaybeObject* 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 Blocka7e24c12009-10-30 11:49:00 +00003664 cast(new_map)->set_instance_descriptors(DescriptorArray::cast(descriptors));
Steve Block8defd9f2010-07-08 12:39:36 +01003665 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00003666}
3667
3668
John Reck59135872010-11-02 12:39:01 -07003669MaybeObject* Map::UpdateCodeCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01003670 // Allocate the code cache if not present.
3671 if (code_cache()->IsFixedArray()) {
John Reck59135872010-11-02 12:39:01 -07003672 Object* result;
Ben Murdoch8b112d22011-06-08 16:22:53 +01003673 { MaybeObject* maybe_result = code->heap()->AllocateCodeCache();
John Reck59135872010-11-02 12:39:01 -07003674 if (!maybe_result->ToObject(&result)) return maybe_result;
3675 }
Steve Block6ded16b2010-05-10 14:33:55 +01003676 set_code_cache(result);
3677 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003678
Steve Block6ded16b2010-05-10 14:33:55 +01003679 // Update the code cache.
3680 return CodeCache::cast(code_cache())->Update(name, code);
3681}
3682
3683
3684Object* 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 Block44f0eee2011-05-26 01:26:41 +01003689 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01003690 }
3691}
3692
3693
3694int 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
3703void 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 Monsen0d5e1162010-09-30 15:31:59 +01003711void Map::TraverseTransitionTree(TraverseCallback callback, void* data) {
Steve Block053d10c2011-06-13 19:13:29 +01003712 // Traverse the transition tree without using a stack. We do this by
3713 // reversing the pointers in the maps and descriptor arrays.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003714 Map* current = this;
Steve Block44f0eee2011-05-26 01:26:41 +01003715 Map* meta_map = heap()->meta_map();
Steve Block053d10c2011-06-13 19:13:29 +01003716 Object** map_or_index_field = NULL;
Steve Block44f0eee2011-05-26 01:26:41 +01003717 while (current != meta_map) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003718 DescriptorArray* d = reinterpret_cast<DescriptorArray*>(
3719 *RawField(current, Map::kInstanceDescriptorsOffset));
Steve Block053d10c2011-06-13 19:13:29 +01003720 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 Murdoch7d3e7fc2011-07-12 16:37:06 +01003744 } else {
3745 map_or_index_field = NULL;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003746 }
Steve Block053d10c2011-06-13 19:13:29 +01003747 // 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 Monsen0d5e1162010-09-30 15:31:59 +01003761 next->set_map(current);
Steve Block053d10c2011-06-13 19:13:29 +01003762 *proto_map_or_index_field =
3763 Smi::FromInt(i + 2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003764 current = next;
Steve Block053d10c2011-06-13 19:13:29 +01003765 continue;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003766 }
3767 }
Steve Block053d10c2011-06-13 19:13:29 +01003768 *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 Monsen0d5e1162010-09-30 15:31:59 +01003776 Map* prev = current->map();
Steve Block44f0eee2011-05-26 01:26:41 +01003777 current->set_map(meta_map);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003778 callback(current, data);
3779 current = prev;
3780 }
3781}
3782
3783
John Reck59135872010-11-02 12:39:01 -07003784MaybeObject* CodeCache::Update(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01003785 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 Reck59135872010-11-02 12:39:01 -07003793 Object* result;
3794 { MaybeObject* maybe_result =
3795 CodeCacheHashTable::Allocate(CodeCacheHashTable::kInitialSize);
3796 if (!maybe_result->ToObject(&result)) return maybe_result;
3797 }
Steve Block6ded16b2010-05-10 14:33:55 +01003798 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 Reck59135872010-11-02 12:39:01 -07003808MaybeObject* CodeCache::UpdateDefaultCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01003809 // When updating the default code cache we disregard the type encoded in the
Steve Blocka7e24c12009-10-30 11:49:00 +00003810 // 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 Block6ded16b2010-05-10 14:33:55 +01003816 FixedArray* cache = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +00003817 int length = cache->length();
3818 int deleted_index = -1;
Steve Block6ded16b2010-05-10 14:33:55 +01003819 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003820 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 Block6ded16b2010-05-10 14:33:55 +01003827 cache->set(i + kCodeCacheEntryNameOffset, name);
3828 cache->set(i + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003829 return this;
3830 }
3831 if (name->Equals(String::cast(key))) {
Steve Block6ded16b2010-05-10 14:33:55 +01003832 Code::Flags found =
3833 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
Steve Blocka7e24c12009-10-30 11:49:00 +00003834 if (Code::RemoveTypeFromFlags(found) == flags) {
Steve Block6ded16b2010-05-10 14:33:55 +01003835 cache->set(i + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003836 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 Block6ded16b2010-05-10 14:33:55 +01003844 cache->set(deleted_index + kCodeCacheEntryNameOffset, name);
3845 cache->set(deleted_index + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003846 return this;
3847 }
3848
Steve Block6ded16b2010-05-10 14:33:55 +01003849 // 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 Reck59135872010-11-02 12:39:01 -07003854 Object* result;
3855 { MaybeObject* maybe_result = cache->CopySize(new_length);
3856 if (!maybe_result->ToObject(&result)) return maybe_result;
3857 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003858
3859 // Add the (name, code) pair to the new cache.
3860 cache = FixedArray::cast(result);
Steve Block6ded16b2010-05-10 14:33:55 +01003861 cache->set(length + kCodeCacheEntryNameOffset, name);
3862 cache->set(length + kCodeCacheEntryCodeOffset, code);
3863 set_default_cache(cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00003864 return this;
3865}
3866
3867
John Reck59135872010-11-02 12:39:01 -07003868MaybeObject* CodeCache::UpdateNormalTypeCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01003869 // Adding a new entry can cause a new cache to be allocated.
3870 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
John Reck59135872010-11-02 12:39:01 -07003871 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 Block6ded16b2010-05-10 14:33:55 +01003875 set_normal_type_cache(new_cache);
3876 return this;
3877}
3878
3879
3880Object* 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
3889Object* CodeCache::LookupDefaultCache(String* name, Code::Flags flags) {
3890 FixedArray* cache = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +00003891 int length = cache->length();
Steve Block6ded16b2010-05-10 14:33:55 +01003892 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
3893 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00003894 // Skip deleted elements.
3895 if (key->IsNull()) continue;
3896 if (key->IsUndefined()) return key;
3897 if (name->Equals(String::cast(key))) {
Steve Block6ded16b2010-05-10 14:33:55 +01003898 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
3899 if (code->flags() == flags) {
3900 return code;
3901 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003902 }
3903 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003904 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003905}
3906
3907
Steve Block6ded16b2010-05-10 14:33:55 +01003908Object* 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 Block44f0eee2011-05-26 01:26:41 +01003913 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01003914 }
3915}
3916
3917
3918int 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 Blocka7e24c12009-10-30 11:49:00 +00003926 int len = array->length();
Steve Block6ded16b2010-05-10 14:33:55 +01003927 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
3928 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003929 }
3930 return -1;
3931}
3932
3933
Steve Block6ded16b2010-05-10 14:33:55 +01003934void 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.
3957class 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 Reck59135872010-11-02 12:39:01 -07003992 MUST_USE_RESULT MaybeObject* AsObject() {
Steve Block6ded16b2010-05-10 14:33:55 +01003993 ASSERT(code_ != NULL);
John Reck59135872010-11-02 12:39:01 -07003994 Object* obj;
Ben Murdoch8b112d22011-06-08 16:22:53 +01003995 { MaybeObject* maybe_obj = code_->heap()->AllocateFixedArray(2);
John Reck59135872010-11-02 12:39:01 -07003996 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3997 }
Steve Block6ded16b2010-05-10 14:33:55 +01003998 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
4011Object* CodeCacheHashTable::Lookup(String* name, Code::Flags flags) {
4012 CodeCacheHashTableKey key(name, flags);
4013 int entry = FindEntry(&key);
Steve Block44f0eee2011-05-26 01:26:41 +01004014 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01004015 return get(EntryToIndex(entry) + 1);
4016}
4017
4018
John Reck59135872010-11-02 12:39:01 -07004019MaybeObject* CodeCacheHashTable::Put(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01004020 CodeCacheHashTableKey key(name, code);
John Reck59135872010-11-02 12:39:01 -07004021 Object* obj;
4022 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
4023 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4024 }
Steve Block6ded16b2010-05-10 14:33:55 +01004025
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 Reck59135872010-11-02 12:39:01 -07004030 Object* k;
4031 { MaybeObject* maybe_k = key.AsObject();
4032 if (!maybe_k->ToObject(&k)) return maybe_k;
4033 }
Steve Block6ded16b2010-05-10 14:33:55 +01004034
4035 cache->set(EntryToIndex(entry), k);
4036 cache->set(EntryToIndex(entry) + 1, code);
4037 cache->ElementAdded();
4038 return cache;
4039}
4040
4041
4042int 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
4049void CodeCacheHashTable::RemoveByIndex(int index) {
4050 ASSERT(index >= 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004051 Heap* heap = GetHeap();
4052 set(EntryToIndex(index), heap->null_value());
4053 set(EntryToIndex(index) + 1, heap->null_value());
Steve Block6ded16b2010-05-10 14:33:55 +01004054 ElementRemoved();
Steve Blocka7e24c12009-10-30 11:49:00 +00004055}
4056
4057
Steve Blocka7e24c12009-10-30 11:49:00 +00004058static 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 Reck59135872010-11-02 12:39:01 -07004072MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) {
Steve Block44f0eee2011-05-26 01:26:41 +01004073 ASSERT(!array->HasExternalArrayElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00004074 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 Reck59135872010-11-02 12:39:01 -07004082 Object* object;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004083 { MaybeObject* maybe_object = GetHeap()->AllocateFixedArray(size);
John Reck59135872010-11-02 12:39:01 -07004084 if (!maybe_object->ToObject(&object)) return maybe_object;
4085 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004086 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 Murdoch8b112d22011-06-08 16:22:53 +01004103 return GetHeap()->null_value(); // Failure case needs to "return" a value.
Steve Blocka7e24c12009-10-30 11:49:00 +00004104}
4105
4106
John Reck59135872010-11-02 12:39:01 -07004107MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004108 int len0 = length();
Ben Murdochf87a2032010-10-22 12:50:53 +01004109#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 Blocka7e24c12009-10-30 11:49:00 +00004116 int len1 = other->length();
Ben Murdochf87a2032010-10-22 12:50:53 +01004117 // Optimize if 'other' is empty.
4118 // We cannot optimize if 'this' is empty, as other may have holes
4119 // or non keys.
Steve Blocka7e24c12009-10-30 11:49:00 +00004120 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 Reck59135872010-11-02 12:39:01 -07004132 Object* obj;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004133 { MaybeObject* maybe_obj = GetHeap()->AllocateFixedArray(len0 + extra);
John Reck59135872010-11-02 12:39:01 -07004134 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4135 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004136 // Fill in the content
Leon Clarke4515c472010-02-03 11:58:03 +00004137 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +00004138 FixedArray* result = FixedArray::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +00004139 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00004140 for (int i = 0; i < len0; i++) {
Ben Murdochf87a2032010-10-22 12:50:53 +01004141 Object* e = get(i);
4142 ASSERT(e->IsString() || e->IsNumber());
4143 result->set(i, e, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00004144 }
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 Murdochf87a2032010-10-22 12:50:53 +01004150 Object* e = other->get(y);
4151 ASSERT(e->IsString() || e->IsNumber());
4152 result->set(len0 + index, e, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00004153 index++;
4154 }
4155 }
4156 ASSERT(extra == index);
4157 return result;
4158}
4159
4160
John Reck59135872010-11-02 12:39:01 -07004161MaybeObject* FixedArray::CopySize(int new_length) {
Steve Block44f0eee2011-05-26 01:26:41 +01004162 Heap* heap = GetHeap();
4163 if (new_length == 0) return heap->empty_fixed_array();
John Reck59135872010-11-02 12:39:01 -07004164 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01004165 { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length);
John Reck59135872010-11-02 12:39:01 -07004166 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4167 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004168 FixedArray* result = FixedArray::cast(obj);
4169 // Copy the content
Leon Clarke4515c472010-02-03 11:58:03 +00004170 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +00004171 int len = length();
4172 if (new_length < len) len = new_length;
4173 result->set_map(map());
Leon Clarke4515c472010-02-03 11:58:03 +00004174 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00004175 for (int i = 0; i < len; i++) {
4176 result->set(i, get(i), mode);
4177 }
4178 return result;
4179}
4180
4181
4182void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
Leon Clarke4515c472010-02-03 11:58:03 +00004183 AssertNoAllocation no_gc;
4184 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00004185 for (int index = 0; index < len; index++) {
4186 dest->set(dest_pos+index, get(pos+index), mode);
4187 }
4188}
4189
4190
4191#ifdef DEBUG
4192bool 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 Reck59135872010-11-02 12:39:01 -07004202MaybeObject* DescriptorArray::Allocate(int number_of_descriptors) {
Steve Block44f0eee2011-05-26 01:26:41 +01004203 Heap* heap = Isolate::Current()->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004204 if (number_of_descriptors == 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01004205 return heap->empty_descriptor_array();
Steve Blocka7e24c12009-10-30 11:49:00 +00004206 }
4207 // Allocate the array of keys.
John Reck59135872010-11-02 12:39:01 -07004208 Object* array;
4209 { MaybeObject* maybe_array =
Steve Block44f0eee2011-05-26 01:26:41 +01004210 heap->AllocateFixedArray(ToKeyIndex(number_of_descriptors));
John Reck59135872010-11-02 12:39:01 -07004211 if (!maybe_array->ToObject(&array)) return maybe_array;
4212 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004213 // 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 Reck59135872010-11-02 12:39:01 -07004217 { MaybeObject* maybe_array =
Steve Block44f0eee2011-05-26 01:26:41 +01004218 heap->AllocateFixedArray(number_of_descriptors << 1);
John Reck59135872010-11-02 12:39:01 -07004219 if (!maybe_array->ToObject(&array)) return maybe_array;
4220 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004221 result->set(kContentArrayIndex, array);
4222 result->set(kEnumerationIndexIndex,
Leon Clarke4515c472010-02-03 11:58:03 +00004223 Smi::FromInt(PropertyDetails::kInitialIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +00004224 return result;
4225}
4226
4227
4228void 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 Reck59135872010-11-02 12:39:01 -07004246MaybeObject* DescriptorArray::CopyInsert(Descriptor* descriptor,
4247 TransitionFlag transition_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004248 // 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 Reck59135872010-11-02 12:39:01 -07004258 Object* result;
4259 { MaybeObject* maybe_result = descriptor->KeyToSymbol();
4260 if (!maybe_result->ToObject(&result)) return maybe_result;
4261 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004262
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 Reck59135872010-11-02 12:39:01 -07004301 { MaybeObject* maybe_result = Allocate(new_size);
4302 if (!maybe_result->ToObject(&result)) return maybe_result;
4303 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004304 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 Reck59135872010-11-02 12:39:01 -07004351MaybeObject* DescriptorArray::RemoveTransitions() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004352 // 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 Reck59135872010-11-02 12:39:01 -07004363 Object* result;
4364 { MaybeObject* maybe_result = Allocate(number_of_descriptors() - num_removed);
4365 if (!maybe_result->ToObject(&result)) return maybe_result;
4366 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004367 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 Monsen0d5e1162010-09-30 15:31:59 +01004380void DescriptorArray::SortUnchecked() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004381 // In-place heap sort.
4382 int len = number_of_descriptors();
4383
4384 // Bottom-up max-heap construction.
Steve Block6ded16b2010-05-10 14:33:55 +01004385 // 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 Blocka7e24c12009-10-30 11:49:00 +00004392 uint32_t child_hash = GetKey(child_index)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +01004393 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 Blocka7e24c12009-10-30 11:49:00 +00004399 }
Steve Block6ded16b2010-05-10 14:33:55 +01004400 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 Blocka7e24c12009-10-30 11:49:00 +00004404 }
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 Block6ded16b2010-05-10 14:33:55 +01004413 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 Blocka7e24c12009-10-30 11:49:00 +00004424 }
Steve Block6ded16b2010-05-10 14:33:55 +01004425 if (child_hash <= parent_hash) break;
4426 Swap(parent_index, child_index);
4427 parent_index = child_index;
Steve Blocka7e24c12009-10-30 11:49:00 +00004428 }
4429 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004430}
Steve Blocka7e24c12009-10-30 11:49:00 +00004431
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004432
4433void DescriptorArray::Sort() {
4434 SortUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00004435 SLOW_ASSERT(IsSortedNoDuplicates());
4436}
4437
4438
4439int 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
4470int 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 Murdochb0fe1622011-05-05 13:52:32 +01004484MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count,
4485 PretenureFlag pretenure) {
4486 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004487 return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count),
Ben Murdochb0fe1622011-05-05 13:52:32 +01004488 pretenure);
4489}
4490
4491
4492MaybeObject* DeoptimizationOutputData::Allocate(int number_of_deopt_points,
4493 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01004494 if (number_of_deopt_points == 0) return HEAP->empty_fixed_array();
4495 return HEAP->AllocateFixedArray(LengthOfFixedArray(number_of_deopt_points),
Ben Murdochb0fe1622011-05-05 13:52:32 +01004496 pretenure);
4497}
4498
4499
Steve Blocka7e24c12009-10-30 11:49:00 +00004500#ifdef DEBUG
4501bool 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 Blocka7e24c12009-10-30 11:49:00 +00004513bool String::LooksValid() {
Steve Block44f0eee2011-05-26 01:26:41 +01004514 if (!Isolate::Current()->heap()->Contains(this)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00004515 return true;
4516}
4517
4518
4519int 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 Block44f0eee2011-05-26 01:26:41 +01004525 Heap* heap = GetHeap();
Steve Block6ded16b2010-05-10 14:33:55 +01004526 TryFlatten();
Steve Block44f0eee2011-05-26 01:26:41 +01004527 Access<StringInputBuffer> buffer(
4528 heap->isolate()->objects_string_input_buffer());
Steve Blocka7e24c12009-10-30 11:49:00 +00004529 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
4537Vector<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 Blockd0582a62009-12-15 09:54:21 +00004545 if (string_tag == kConsStringTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004546 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
4563Vector<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 Blockd0582a62009-12-15 09:54:21 +00004571 if (string_tag == kConsStringTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004572 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
4589SmartPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
4590 RobustnessFlag robust_flag,
4591 int offset,
4592 int length,
4593 int* length_return) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004594 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
4595 return SmartPointer<char>(NULL);
4596 }
Steve Block44f0eee2011-05-26 01:26:41 +01004597 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004598
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 Block44f0eee2011-05-26 01:26:41 +01004603 Access<StringInputBuffer> buffer(
4604 heap->isolate()->objects_string_input_buffer());
Steve Blocka7e24c12009-10-30 11:49:00 +00004605 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
4643SmartPointer<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
4650const uc16* String::GetTwoByteData() {
4651 return GetTwoByteData(0);
4652}
4653
4654
4655const 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 Blocka7e24c12009-10-30 11:49:00 +00004663 case kConsStringTag:
4664 UNREACHABLE();
4665 return NULL;
4666 }
4667 UNREACHABLE();
4668 return NULL;
4669}
4670
4671
4672SmartPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004673 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
4674 return SmartPointer<uc16>();
4675 }
Steve Block44f0eee2011-05-26 01:26:41 +01004676 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004677
Steve Block44f0eee2011-05-26 01:26:41 +01004678 Access<StringInputBuffer> buffer(
4679 heap->isolate()->objects_string_input_buffer());
Steve Blocka7e24c12009-10-30 11:49:00 +00004680 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
4694const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
4695 return reinterpret_cast<uc16*>(
4696 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
4697}
4698
4699
4700void 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
4733const 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.
4752const 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 Blocka7e24c12009-10-30 11:49:00 +00004817uint16_t ExternalAsciiString::ExternalAsciiStringGet(int index) {
4818 ASSERT(index >= 0 && index < length());
4819 return resource()->data()[index];
4820}
4821
4822
4823const 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
4836const uc16* ExternalTwoByteString::ExternalTwoByteStringGetData(
4837 unsigned start) {
4838 return resource()->data() + start;
4839}
4840
4841
4842uint16_t ExternalTwoByteString::ExternalTwoByteStringGet(int index) {
4843 ASSERT(index >= 0 && index < length());
4844 return resource()->data()[index];
4845}
4846
4847
4848void 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
4879void 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
4894void 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).
4913const 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 Blocka7e24c12009-10-30 11:49:00 +00004940 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 Blocka7e24c12009-10-30 11:49:00 +00004962void Relocatable::PostGarbageCollectionProcessing() {
Steve Block44f0eee2011-05-26 01:26:41 +01004963 Isolate* isolate = Isolate::Current();
4964 Relocatable* current = isolate->relocatable_top();
Steve Blocka7e24c12009-10-30 11:49:00 +00004965 while (current != NULL) {
4966 current->PostGarbageCollection();
4967 current = current->prev_;
4968 }
4969}
4970
4971
4972// Reserve space for statics needing saving and restoring.
4973int Relocatable::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01004974 return sizeof(Isolate::Current()->relocatable_top());
Steve Blocka7e24c12009-10-30 11:49:00 +00004975}
4976
4977
4978// Archive statics that are thread local.
4979char* Relocatable::ArchiveState(char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01004980 Isolate* isolate = Isolate::Current();
4981 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
4982 isolate->set_relocatable_top(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00004983 return to + ArchiveSpacePerThread();
4984}
4985
4986
4987// Restore statics that are thread local.
4988char* Relocatable::RestoreState(char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01004989 Isolate* isolate = Isolate::Current();
4990 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
Steve Blocka7e24c12009-10-30 11:49:00 +00004991 return from + ArchiveSpacePerThread();
4992}
4993
4994
4995char* 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
5002void Relocatable::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +01005003 Isolate* isolate = Isolate::Current();
5004 Iterate(v, isolate->relocatable_top());
Steve Blocka7e24c12009-10-30 11:49:00 +00005005}
5006
5007
5008void 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 Block44f0eee2011-05-26 01:26:41 +01005017FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
5018 : Relocatable(isolate),
5019 str_(str.location()),
Steve Blocka7e24c12009-10-30 11:49:00 +00005020 length_(str->length()) {
5021 PostGarbageCollection();
5022}
5023
5024
Steve Block44f0eee2011-05-26 01:26:41 +01005025FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
5026 : Relocatable(isolate),
5027 str_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +00005028 is_ascii_(true),
5029 length_(input.length()),
5030 start_(input.start()) { }
5031
5032
5033void 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
5046void StringInputBuffer::Seek(unsigned pos) {
5047 Reset(pos, input_);
5048}
5049
5050
5051void 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.
5060void 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 Blocka7e24c12009-10-30 11:49:00 +00005085 case kExternalStringTag:
5086 if (input->IsAsciiRepresentation()) {
Steve Blockd0582a62009-12-15 09:54:21 +00005087 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 Blocka7e24c12009-10-30 11:49:00 +00005094 }
5095 return;
5096 default:
5097 break;
5098 }
5099
5100 UNREACHABLE();
5101 return;
5102}
5103
5104
5105const 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
5120const 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.
5142void 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 Blocka7e24c12009-10-30 11:49:00 +00005199uint16_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
5230template <typename sinkchar>
5231void 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 Blocka7e24c12009-10-30 11:49:00 +00005267 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 Blocka7e24c12009-10-30 11:49:00 +00005302template <typename IteratorA, typename IteratorB>
5303static 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.
5318template <typename Char>
5319static 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 Blocka7e24c12009-10-30 11:49:00 +00005357template <typename IteratorA>
Steve Block44f0eee2011-05-26 01:26:41 +01005358static inline bool CompareStringContentsPartial(Isolate* isolate,
5359 IteratorA* ia,
5360 String* b) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005361 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 Block44f0eee2011-05-26 01:26:41 +01005370 isolate->objects_string_compare_buffer_b()->Reset(0, b);
5371 return CompareStringContents(ia,
5372 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00005373 }
5374}
5375
5376
Steve Blocka7e24c12009-10-30 11:49:00 +00005377bool 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 Clarkef7060e22010-06-03 12:02:55 +01005389 // 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 Blocka7e24c12009-10-30 11:49:00 +00005400 return CompareRawStringContents(Vector<const char>(str1, len),
5401 Vector<const char>(str2, len));
5402 }
5403
Steve Block44f0eee2011-05-26 01:26:41 +01005404 Isolate* isolate = GetIsolate();
Leon Clarkef7060e22010-06-03 12:02:55 +01005405 if (lhs->IsFlat()) {
Ben Murdochbb769b22010-08-11 14:56:33 +01005406 if (lhs->IsAsciiRepresentation()) {
Leon Clarkef7060e22010-06-03 12:02:55 +01005407 Vector<const char> vec1 = lhs->ToAsciiVector();
5408 if (rhs->IsFlat()) {
5409 if (rhs->IsAsciiRepresentation()) {
5410 Vector<const char> vec2 = rhs->ToAsciiVector();
Steve Blocka7e24c12009-10-30 11:49:00 +00005411 return CompareRawStringContents(vec1, vec2);
5412 } else {
5413 VectorIterator<char> buf1(vec1);
Leon Clarkef7060e22010-06-03 12:02:55 +01005414 VectorIterator<uc16> ib(rhs->ToUC16Vector());
Steve Blocka7e24c12009-10-30 11:49:00 +00005415 return CompareStringContents(&buf1, &ib);
5416 }
5417 } else {
5418 VectorIterator<char> buf1(vec1);
Steve Block44f0eee2011-05-26 01:26:41 +01005419 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
5420 return CompareStringContents(&buf1,
5421 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00005422 }
5423 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01005424 Vector<const uc16> vec1 = lhs->ToUC16Vector();
5425 if (rhs->IsFlat()) {
5426 if (rhs->IsAsciiRepresentation()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005427 VectorIterator<uc16> buf1(vec1);
Leon Clarkef7060e22010-06-03 12:02:55 +01005428 VectorIterator<char> ib(rhs->ToAsciiVector());
Steve Blocka7e24c12009-10-30 11:49:00 +00005429 return CompareStringContents(&buf1, &ib);
5430 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +01005431 Vector<const uc16> vec2(rhs->ToUC16Vector());
Steve Blocka7e24c12009-10-30 11:49:00 +00005432 return CompareRawStringContents(vec1, vec2);
5433 }
5434 } else {
5435 VectorIterator<uc16> buf1(vec1);
Steve Block44f0eee2011-05-26 01:26:41 +01005436 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
5437 return CompareStringContents(&buf1,
5438 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00005439 }
5440 }
5441 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005442 isolate->objects_string_compare_buffer_a()->Reset(0, lhs);
5443 return CompareStringContentsPartial(isolate,
5444 isolate->objects_string_compare_buffer_a(), rhs);
Steve Blocka7e24c12009-10-30 11:49:00 +00005445 }
5446}
5447
5448
5449bool String::MarkAsUndetectable() {
5450 if (StringShape(this).IsSymbol()) return false;
5451
5452 Map* map = this->map();
Ben Murdoch8b112d22011-06-08 16:22:53 +01005453 Heap* heap = map->heap();
Steve Block44f0eee2011-05-26 01:26:41 +01005454 if (map == heap->string_map()) {
5455 this->set_map(heap->undetectable_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00005456 return true;
Steve Block44f0eee2011-05-26 01:26:41 +01005457 } else if (map == heap->ascii_string_map()) {
5458 this->set_map(heap->undetectable_ascii_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00005459 return true;
5460 }
5461 // Rest cannot be marked as undetectable
5462 return false;
5463}
5464
5465
5466bool String::IsEqualTo(Vector<const char> str) {
Steve Block44f0eee2011-05-26 01:26:41 +01005467 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00005468 int slen = length();
Ben Murdoch8b112d22011-06-08 16:22:53 +01005469 Access<UnicodeCache::Utf8Decoder>
5470 decoder(isolate->unicode_cache()->utf8_decoder());
Steve Blocka7e24c12009-10-30 11:49:00 +00005471 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 Block9fac8402011-05-12 15:51:54 +01005481bool 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
5491bool 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 Blocka7e24c12009-10-30 11:49:00 +00005501uint32_t String::ComputeAndSetHash() {
5502 // Should only be called if hash code has not yet been computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005503 ASSERT(!HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00005504
Steve Block6ded16b2010-05-10 14:33:55 +01005505 const int len = length();
5506
Steve Blocka7e24c12009-10-30 11:49:00 +00005507 // Compute the hash code.
Steve Block6ded16b2010-05-10 14:33:55 +01005508 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 Blocka7e24c12009-10-30 11:49:00 +00005517
5518 // Store the hash code in the object.
Steve Blockd0582a62009-12-15 09:54:21 +00005519 set_hash_field(field);
Steve Blocka7e24c12009-10-30 11:49:00 +00005520
5521 // Check the hash code is there.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005522 ASSERT(HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00005523 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
5529bool 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
5559bool String::SlowAsArrayIndex(uint32_t* index) {
5560 if (length() <= kMaxCachedArrayIndexLength) {
5561 Hash(); // force computation of hash code
Steve Blockd0582a62009-12-15 09:54:21 +00005562 uint32_t field = hash_field();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005563 if ((field & kIsNotArrayIndexMask) != 0) return false;
Steve Blockd0582a62009-12-15 09:54:21 +00005564 // Isolate the array index form the full hash field.
5565 *index = (kArrayIndexHashMask & field) >> kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +00005566 return true;
5567 } else {
5568 StringInputBuffer buffer(this);
5569 return ComputeArrayIndex(&buffer, index, length());
5570 }
5571}
5572
5573
Iain Merrick9ac36c92010-09-13 15:29:50 +01005574uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005575 // 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 Merrick9ac36c92010-09-13 15:29:50 +01005581
5582 value <<= String::kHashShift;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005583 value |= length << String::kArrayIndexHashLengthShift;
Iain Merrick9ac36c92010-09-13 15:29:50 +01005584
5585 ASSERT((value & String::kIsNotArrayIndexMask) == 0);
5586 ASSERT((length > String::kMaxCachedArrayIndexLength) ||
5587 (value & String::kContainsCachedArrayIndexMask) == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005588 return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00005589}
5590
5591
5592uint32_t StringHasher::GetHashField() {
5593 ASSERT(is_valid());
Steve Blockd0582a62009-12-15 09:54:21 +00005594 if (length_ <= String::kMaxHashCalcLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005595 if (is_array_index()) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005596 return MakeArrayIndexHash(array_index(), length_);
Steve Blocka7e24c12009-10-30 11:49:00 +00005597 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005598 return (GetHash() << String::kHashShift) | String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00005599 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005600 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00005601 }
5602}
5603
5604
Steve Blockd0582a62009-12-15 09:54:21 +00005605uint32_t String::ComputeHashField(unibrow::CharacterStream* buffer,
5606 int length) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005607 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 Reck59135872010-11-02 12:39:01 -07005631MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01005632 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00005633 if (start == 0 && end == length()) return this;
Steve Block44f0eee2011-05-26 01:26:41 +01005634 MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure);
Steve Blockd0582a62009-12-15 09:54:21 +00005635 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00005636}
5637
5638
5639void 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
5647void Map::CreateBackPointers() {
5648 DescriptorArray* descriptors = instance_descriptors();
5649 for (int i = 0; i < descriptors->number_of_descriptors(); i++) {
Iain Merrick75681382010-08-19 15:07:18 +01005650 if (descriptors->GetType(i) == MAP_TRANSITION ||
Steve Block44f0eee2011-05-26 01:26:41 +01005651 descriptors->GetType(i) == EXTERNAL_ARRAY_TRANSITION ||
Iain Merrick75681382010-08-19 15:07:18 +01005652 descriptors->GetType(i) == CONSTANT_TRANSITION) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005653 // 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 Block44f0eee2011-05-26 01:26:41 +01005675void Map::ClearNonLiveTransitions(Heap* heap, Object* real_prototype) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005676 // 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 Block44f0eee2011-05-26 01:26:41 +01005680 if (d == heap->raw_unchecked_empty_descriptor_array()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00005681 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 Merrick75681382010-08-19 15:07:18 +01005693 if (details.type() == MAP_TRANSITION ||
Steve Block44f0eee2011-05-26 01:26:41 +01005694 details.type() == EXTERNAL_ARRAY_TRANSITION ||
Iain Merrick75681382010-08-19 15:07:18 +01005695 details.type() == CONSTANT_TRANSITION) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005696 Map* target = reinterpret_cast<Map*>(contents->get(i));
5697 ASSERT(target->IsHeapObject());
5698 if (!target->IsMarked()) {
5699 ASSERT(target->IsMap());
Iain Merrick75681382010-08-19 15:07:18 +01005700 contents->set_unchecked(i + 1, NullDescriptorDetails);
Steve Block44f0eee2011-05-26 01:26:41 +01005701 contents->set_null_unchecked(heap, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00005702 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 Block791712a2010-08-27 10:21:07 +01005712void 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 Murdochb0fe1622011-05-05 13:52:32 +01005721void JSFunction::MarkForLazyRecompilation() {
5722 ASSERT(is_compiled() && !IsOptimized());
Ben Murdochb8e0da22011-05-16 14:20:40 +01005723 ASSERT(shared()->allows_lazy_compilation() ||
5724 code()->optimizable());
Steve Block44f0eee2011-05-26 01:26:41 +01005725 Builtins* builtins = GetIsolate()->builtins();
5726 ReplaceCode(builtins->builtin(Builtins::kLazyRecompile));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005727}
5728
5729
5730uint32_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
5743bool JSFunction::IsInlineable() {
5744 if (IsBuiltin()) return false;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005745 SharedFunctionInfo* shared_info = shared();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005746 // Check that the function has a script associated with it.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005747 if (!shared_info->script()->IsScript()) return false;
5748 if (shared_info->optimization_disabled()) return false;
5749 Code* code = shared_info->code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005750 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 Blocka7e24c12009-10-30 11:49:00 +00005757Object* JSFunction::SetInstancePrototype(Object* value) {
5758 ASSERT(value->IsJSObject());
Steve Block44f0eee2011-05-26 01:26:41 +01005759 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00005760 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 Block44f0eee2011-05-26 01:26:41 +01005768 heap->ClearInstanceofCache();
Steve Blocka7e24c12009-10-30 11:49:00 +00005769 return value;
5770}
5771
5772
John Reck59135872010-11-02 12:39:01 -07005773MaybeObject* JSFunction::SetPrototype(Object* value) {
Steve Block6ded16b2010-05-10 14:33:55 +01005774 ASSERT(should_have_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00005775 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 Murdoch8b112d22011-06-08 16:22:53 +01005785 Object* new_object;
John Reck59135872010-11-02 12:39:01 -07005786 { MaybeObject* maybe_new_map = map()->CopyDropTransitions();
Ben Murdoch8b112d22011-06-08 16:22:53 +01005787 if (!maybe_new_map->ToObject(&new_object)) return maybe_new_map;
John Reck59135872010-11-02 12:39:01 -07005788 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01005789 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 Blocka7e24c12009-10-30 11:49:00 +00005794 construct_prototype =
Steve Block44f0eee2011-05-26 01:26:41 +01005795 heap->isolate()->context()->global_context()->
5796 initial_object_prototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00005797 } else {
5798 map()->set_non_instance_prototype(false);
5799 }
5800
5801 return SetInstancePrototype(construct_prototype);
5802}
5803
5804
Steve Block6ded16b2010-05-10 14:33:55 +01005805Object* JSFunction::RemovePrototype() {
Steve Block44f0eee2011-05-26 01:26:41 +01005806 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 Murdoche0cee9b2011-05-25 10:26:03 +01005812 // Be idempotent.
5813 return this;
5814 }
Steve Block44f0eee2011-05-26 01:26:41 +01005815
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 Murdoch8b112d22011-06-08 16:22:53 +01005821 set_prototype_or_initial_map(no_prototype_map->heap()->the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +01005822 return this;
5823}
5824
5825
Steve Blocka7e24c12009-10-30 11:49:00 +00005826Object* JSFunction::SetInstanceClassName(String* name) {
5827 shared()->set_instance_class_name(name);
5828 return this;
5829}
5830
5831
Ben Murdochb0fe1622011-05-05 13:52:32 +01005832void JSFunction::PrintName(FILE* out) {
5833 SmartPointer<char> name = shared()->DebugName()->ToCString();
5834 PrintF(out, "%s", *name);
5835}
5836
5837
Steve Blocka7e24c12009-10-30 11:49:00 +00005838Context* JSFunction::GlobalContextFromLiterals(FixedArray* literals) {
5839 return Context::cast(literals->get(JSFunction::kLiteralGlobalContextIndex));
5840}
5841
5842
Steve Block44f0eee2011-05-26 01:26:41 +01005843MaybeObject* Oddball::Initialize(const char* to_string,
5844 Object* to_number,
5845 byte kind) {
John Reck59135872010-11-02 12:39:01 -07005846 Object* symbol;
Steve Block44f0eee2011-05-26 01:26:41 +01005847 { MaybeObject* maybe_symbol =
5848 Isolate::Current()->heap()->LookupAsciiSymbol(to_string);
John Reck59135872010-11-02 12:39:01 -07005849 if (!maybe_symbol->ToObject(&symbol)) return maybe_symbol;
5850 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005851 set_to_string(String::cast(symbol));
5852 set_to_number(to_number);
Steve Block44f0eee2011-05-26 01:26:41 +01005853 set_kind(kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00005854 return this;
5855}
5856
5857
Ben Murdochf87a2032010-10-22 12:50:53 +01005858String* 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 Blocka7e24c12009-10-30 11:49:00 +00005865bool SharedFunctionInfo::HasSourceCode() {
5866 return !script()->IsUndefined() &&
Iain Merrick75681382010-08-19 15:07:18 +01005867 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +00005868}
5869
5870
5871Object* SharedFunctionInfo::GetSourceCode() {
Steve Block44f0eee2011-05-26 01:26:41 +01005872 Isolate* isolate = GetIsolate();
5873 if (!HasSourceCode()) return isolate->heap()->undefined_value();
5874 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00005875 Object* source = Script::cast(script())->source();
Steve Block44f0eee2011-05-26 01:26:41 +01005876 return *SubString(Handle<String>(String::cast(source), isolate),
Steve Blocka7e24c12009-10-30 11:49:00 +00005877 start_position(), end_position());
5878}
5879
5880
Ben Murdochb0fe1622011-05-05 13:52:32 +01005881int SharedFunctionInfo::SourceSize() {
5882 return end_position() - start_position();
5883}
5884
5885
Steve Blocka7e24c12009-10-30 11:49:00 +00005886int 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
5897int SharedFunctionInfo::CalculateInObjectProperties() {
5898 return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize;
5899}
5900
5901
Andrei Popescu402d9372010-02-26 13:31:12 +00005902bool 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 Murdoch8b112d22011-06-08 16:22:53 +01005916 Heap* heap = GetHeap();
5917
Andrei Popescu402d9372010-02-26 13:31:12 +00005918 // 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 Block44f0eee2011-05-26 01:26:41 +01005921 obj != heap->null_value();
Andrei Popescu402d9372010-02-26 13:31:12 +00005922 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 Monsen0d5e1162010-09-30 15:31:59 +01005938void SharedFunctionInfo::ForbidInlineConstructor() {
5939 set_compiler_hints(BooleanBit::set(compiler_hints(),
5940 kHasOnlySimpleThisPropertyAssignments,
5941 false));
5942}
5943
5944
Steve Blocka7e24c12009-10-30 11:49:00 +00005945void SharedFunctionInfo::SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00005946 bool only_simple_this_property_assignments,
5947 FixedArray* assignments) {
5948 set_compiler_hints(BooleanBit::set(compiler_hints(),
Steve Blocka7e24c12009-10-30 11:49:00 +00005949 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
5956void SharedFunctionInfo::ClearThisPropertyAssignmentsInfo() {
Steve Block44f0eee2011-05-26 01:26:41 +01005957 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00005958 set_compiler_hints(BooleanBit::set(compiler_hints(),
Steve Blocka7e24c12009-10-30 11:49:00 +00005959 kHasOnlySimpleThisPropertyAssignments,
5960 false));
Steve Block44f0eee2011-05-26 01:26:41 +01005961 set_this_property_assignments(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00005962 set_this_property_assignments_count(0);
5963}
5964
5965
5966String* 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
5976bool 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
5985int 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
5993Object* 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 Blocka7e24c12009-10-30 11:49:00 +00006001// Support function for printing the source code to a StringStream
6002// without any allocation in the heap.
6003void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator,
6004 int max_length) {
6005 // For some native functions there is no source.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006006 if (!HasSourceCode()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006007 accumulator->Add("<No Source>");
6008 return;
6009 }
6010
Steve Blockd0582a62009-12-15 09:54:21 +00006011 // Get the source for the script which this function came from.
Steve Blocka7e24c12009-10-30 11:49:00 +00006012 // 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 Murdochb0fe1622011-05-05 13:52:32 +01006031 if (len <= max_length || max_length < 0) {
6032 accumulator->Put(script_source, start_position(), end_position());
6033 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00006034 accumulator->Put(script_source,
6035 start_position(),
6036 start_position() + max_length);
6037 accumulator->Add("...\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00006038 }
6039}
6040
6041
Ben Murdochb0fe1622011-05-05 13:52:32 +01006042static 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
6055void 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
6074bool 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 Monsen0d5e1162010-09-30 15:31:59 +01006088void 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 Block44f0eee2011-05-26 01:26:41 +01006106 Builtins* builtins = map->heap()->isolate()->builtins();
6107 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006108 construct_stub());
Steve Block44f0eee2011-05-26 01:26:41 +01006109 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006110}
6111
6112
6113// Called from GC, hence reinterpret_cast and unchecked accessors.
6114void 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 Block44f0eee2011-05-26 01:26:41 +01006126 set_initial_map(map->heap()->raw_unchecked_undefined_value());
6127 Builtins* builtins = map->heap()->isolate()->builtins();
6128 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006129 *RawField(this, kConstructStubOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01006130 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006131 // 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.
6137void 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 Block44f0eee2011-05-26 01:26:41 +01006143 Builtins* builtins = map->heap()->isolate()->builtins();
6144 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006145 *RawField(this, kConstructStubOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01006146 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006147 // The map survived the gc, so there may be objects referencing it.
6148 set_live_objects_may_exist(true);
6149}
6150
6151
6152static 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
6160static 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
6171void SharedFunctionInfo::CompleteInobjectSlackTracking() {
6172 ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress());
6173 Map* map = Map::cast(initial_map());
6174
Steve Block44f0eee2011-05-26 01:26:41 +01006175 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 Monsen0d5e1162010-09-30 15:31:59 +01006179 construct_stub());
Steve Block44f0eee2011-05-26 01:26:41 +01006180 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006181
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 Blocka7e24c12009-10-30 11:49:00 +00006194void 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 Block791712a2010-08-27 10:21:07 +01006203void 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 Murdochb0fe1622011-05-05 13:52:32 +01006213void 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 Blocka7e24c12009-10-30 11:49:00 +00006224void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006225 ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) &&
6226 rinfo->IsPatchedReturnSequence()) ||
6227 (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
6228 rinfo->IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006229 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 Murdochb0fe1622011-05-05 13:52:32 +01006236void Code::InvalidateRelocation() {
Ben Murdoch8b112d22011-06-08 16:22:53 +01006237 set_relocation_info(heap()->empty_byte_array());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006238}
6239
6240
Steve Blockd0582a62009-12-15 09:54:21 +00006241void Code::Relocate(intptr_t delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006242 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
6249void Code::CopyFrom(const CodeDesc& desc) {
6250 // copy code
6251 memmove(instruction_start(), desc.buffer, desc.instr_size);
6252
Steve Blocka7e24c12009-10-30 11:49:00 +00006253 // 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 Block3ce2e202009-11-05 08:53:23 +00006259 intptr_t delta = instruction_start() - desc.buffer;
Steve Blocka7e24c12009-10-30 11:49:00 +00006260 int mode_mask = RelocInfo::kCodeTargetMask |
6261 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
Ben Murdochb0fe1622011-05-05 13:52:32 +01006262 RelocInfo::ModeMask(RelocInfo::GLOBAL_PROPERTY_CELL) |
Steve Blocka7e24c12009-10-30 11:49:00 +00006263 RelocInfo::kApplyMask;
Steve Block3ce2e202009-11-05 08:53:23 +00006264 Assembler* origin = desc.origin; // Needed to find target_object on X64.
Steve Blocka7e24c12009-10-30 11:49:00 +00006265 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
6266 RelocInfo::Mode mode = it.rinfo()->rmode();
6267 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Steve Block3ce2e202009-11-05 08:53:23 +00006268 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Steve Blocka7e24c12009-10-30 11:49:00 +00006269 it.rinfo()->set_target_object(*p);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006270 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
Steve Block1e0659c2011-05-24 12:43:12 +01006271 Handle<JSGlobalPropertyCell> cell = it.rinfo()->target_cell_handle();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006272 it.rinfo()->set_target_cell(*cell);
Steve Blocka7e24c12009-10-30 11:49:00 +00006273 } 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 Block3ce2e202009-11-05 08:53:23 +00006276 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Steve Blocka7e24c12009-10-30 11:49:00 +00006277 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.
6291int 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 Blockd0582a62009-12-15 09:54:21 +00006303
6304 int dist = static_cast<int>(pc - it.rinfo()->pc());
6305 int pos = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +00006306 // 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.
6323int 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 Blockd0582a62009-12-15 09:54:21 +00006332 int p = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +00006333 if (statement_position < p && p <= position) {
6334 statement_position = p;
6335 }
6336 }
6337 it.next();
6338 }
6339 return statement_position;
6340}
6341
6342
Ben Murdochb8e0da22011-05-16 14:20:40 +01006343SafepointEntry Code::GetSafepointEntry(Address pc) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006344 SafepointTable table(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01006345 return table.FindEntry(pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006346}
6347
6348
6349void 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 Block1e0659c2011-05-24 12:43:12 +01006352 set_stack_check_table_offset(RoundUp(instruction_size(), kIntSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006353}
6354
6355
6356Map* 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 Blocka7e24c12009-10-30 11:49:00 +00006369#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +01006370
6371#ifdef OBJECT_PRINT
6372
6373void 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
6503void 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 Blocka7e24c12009-10-30 11:49:00 +00006522// Identify kind of code.
6523const char* Code::Kind2String(Kind kind) {
6524 switch (kind) {
6525 case FUNCTION: return "FUNCTION";
Ben Murdochb0fe1622011-05-05 13:52:32 +01006526 case OPTIMIZED_FUNCTION: return "OPTIMIZED_FUNCTION";
Steve Blocka7e24c12009-10-30 11:49:00 +00006527 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 Block44f0eee2011-05-26 01:26:41 +01006531 case KEYED_EXTERNAL_ARRAY_LOAD_IC: return "KEYED_EXTERNAL_ARRAY_LOAD_IC";
Steve Blocka7e24c12009-10-30 11:49:00 +00006532 case STORE_IC: return "STORE_IC";
6533 case KEYED_STORE_IC: return "KEYED_STORE_IC";
Steve Block44f0eee2011-05-26 01:26:41 +01006534 case KEYED_EXTERNAL_ARRAY_STORE_IC: return "KEYED_EXTERNAL_ARRAY_STORE_IC";
Steve Blocka7e24c12009-10-30 11:49:00 +00006535 case CALL_IC: return "CALL_IC";
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006536 case KEYED_CALL_IC: return "KEYED_CALL_IC";
Ben Murdochb0fe1622011-05-05 13:52:32 +01006537 case TYPE_RECORDING_BINARY_OP_IC: return "TYPE_RECORDING_BINARY_OP_IC";
6538 case COMPARE_IC: return "COMPARE_IC";
Steve Blocka7e24c12009-10-30 11:49:00 +00006539 }
6540 UNREACHABLE();
6541 return NULL;
6542}
6543
6544
6545const 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
6560const 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 Block44f0eee2011-05-26 01:26:41 +01006568 case EXTERNAL_ARRAY_TRANSITION: return "EXTERNAL_ARRAY_TRANSITION";
Steve Blocka7e24c12009-10-30 11:49:00 +00006569 case CONSTANT_TRANSITION: return "CONSTANT_TRANSITION";
6570 case NULL_DESCRIPTOR: return "NULL_DESCRIPTOR";
6571 }
6572 UNREACHABLE();
6573 return NULL;
6574}
6575
Ben Murdochb0fe1622011-05-05 13:52:32 +01006576
Steve Block1e0659c2011-05-24 12:43:12 +01006577void 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 Murdoche0cee9b2011-05-25 10:26:03 +01006586 case KEYED_STORE_IC:
6587 if (extra == kStrictMode) {
Steve Block1e0659c2011-05-24 12:43:12 +01006588 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 Murdochb0fe1622011-05-05 13:52:32 +01006602void Code::Disassemble(const char* name, FILE* out) {
6603 PrintF(out, "kind = %s\n", Kind2String(kind()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006604 if (is_inline_cache_stub()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006605 PrintF(out, "ic_state = %s\n", ICState2String(ic_state()));
Steve Block1e0659c2011-05-24 12:43:12 +01006606 PrintExtraICState(out, kind(), extra_ic_state());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006607 PrintF(out, "ic_in_loop = %d\n", ic_in_loop() == IN_LOOP);
Steve Blocka7e24c12009-10-30 11:49:00 +00006608 if (ic_state() == MONOMORPHIC) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006609 PrintF(out, "type = %s\n", PropertyType2String(type()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006610 }
6611 }
6612 if ((name != NULL) && (name[0] != '\0')) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006613 PrintF(out, "name = %s\n", name);
6614 }
6615 if (kind() == OPTIMIZED_FUNCTION) {
6616 PrintF(out, "stack_slots = %d\n", stack_slots());
Steve Blocka7e24c12009-10-30 11:49:00 +00006617 }
6618
Ben Murdochb0fe1622011-05-05 13:52:32 +01006619 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 Blocka7e24c12009-10-30 11:49:00 +00006633 PrintF("\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +01006634#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 Murdochb8e0da22011-05-16 14:20:40 +01006644 SafepointEntry entry = table.GetEntry(i);
6645 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
6646 PrintF(out, " %6d", entry.deoptimization_index());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006647 } else {
6648 PrintF(out, " <none>");
6649 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01006650 if (entry.argument_count() > 0) {
6651 PrintF(out, " argc: %d", entry.argument_count());
6652 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01006653 PrintF(out, "\n");
6654 }
6655 PrintF(out, "\n");
6656 } else if (kind() == FUNCTION) {
Steve Block1e0659c2011-05-24 12:43:12 +01006657 unsigned offset = stack_check_table_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006658 // 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 Blocka7e24c12009-10-30 11:49:00 +00006673
6674 PrintF("RelocInfo (size = %d)\n", relocation_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006675 for (RelocIterator it(this); !it.done(); it.next()) it.rinfo()->Print(out);
6676 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00006677}
6678#endif // ENABLE_DISASSEMBLER
6679
6680
John Reck59135872010-11-02 12:39:01 -07006681MaybeObject* JSObject::SetFastElementsCapacityAndLength(int capacity,
6682 int length) {
Steve Block44f0eee2011-05-26 01:26:41 +01006683 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +00006684 // We should never end in here with a pixel or external array.
Steve Block44f0eee2011-05-26 01:26:41 +01006685 ASSERT(!HasExternalArrayElements());
Steve Block8defd9f2010-07-08 12:39:36 +01006686
John Reck59135872010-11-02 12:39:01 -07006687 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01006688 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(capacity);
John Reck59135872010-11-02 12:39:01 -07006689 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6690 }
Steve Block8defd9f2010-07-08 12:39:36 +01006691 FixedArray* elems = FixedArray::cast(obj);
6692
John Reck59135872010-11-02 12:39:01 -07006693 { MaybeObject* maybe_obj = map()->GetFastElementsMap();
6694 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6695 }
Steve Block8defd9f2010-07-08 12:39:36 +01006696 Map* new_map = Map::cast(obj);
6697
Leon Clarke4515c472010-02-03 11:58:03 +00006698 AssertNoAllocation no_gc;
6699 WriteBarrierMode mode = elems->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00006700 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 Block8defd9f2010-07-08 12:39:36 +01006725
6726 set_map(new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00006727 set_elements(elems);
Steve Block8defd9f2010-07-08 12:39:36 +01006728
6729 if (IsJSArray()) {
6730 JSArray::cast(this)->set_length(Smi::FromInt(length));
6731 }
6732
6733 return this;
Steve Blocka7e24c12009-10-30 11:49:00 +00006734}
6735
6736
John Reck59135872010-11-02 12:39:01 -07006737MaybeObject* JSObject::SetSlowElements(Object* len) {
Steve Block3ce2e202009-11-05 08:53:23 +00006738 // We should never end in here with a pixel or external array.
Steve Block44f0eee2011-05-26 01:26:41 +01006739 ASSERT(!HasExternalArrayElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00006740
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 Reck59135872010-11-02 12:39:01 -07006748 Object* obj;
6749 { MaybeObject* maybe_obj = NormalizeElements();
6750 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6751 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006752
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 Block6ded16b2010-05-10 14:33:55 +01006760 static_cast<uint32_t>(JSArray::cast(this)->length()->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +00006761 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 Reck59135872010-11-02 12:39:01 -07006774MaybeObject* JSArray::Initialize(int capacity) {
Steve Block44f0eee2011-05-26 01:26:41 +01006775 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00006776 ASSERT(capacity >= 0);
Leon Clarke4515c472010-02-03 11:58:03 +00006777 set_length(Smi::FromInt(0));
Steve Blocka7e24c12009-10-30 11:49:00 +00006778 FixedArray* new_elements;
6779 if (capacity == 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01006780 new_elements = heap->empty_fixed_array();
Steve Blocka7e24c12009-10-30 11:49:00 +00006781 } else {
John Reck59135872010-11-02 12:39:01 -07006782 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01006783 { MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(capacity);
John Reck59135872010-11-02 12:39:01 -07006784 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6785 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006786 new_elements = FixedArray::cast(obj);
6787 }
6788 set_elements(new_elements);
6789 return this;
6790}
6791
6792
6793void 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 Blockd0582a62009-12-15 09:54:21 +00006797 int new_size = required_size > old_size ? required_size : old_size;
Steve Block44f0eee2011-05-26 01:26:41 +01006798 Handle<FixedArray> new_backing = FACTORY->NewFixedArray(new_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00006799 // 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 Block44f0eee2011-05-26 01:26:41 +01006805static Failure* ArrayLengthRangeError(Heap* heap) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006806 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01006807 return heap->isolate()->Throw(
6808 *FACTORY->NewRangeError("invalid_array_length",
6809 HandleVector<Object>(NULL, 0)));
Steve Blocka7e24c12009-10-30 11:49:00 +00006810}
6811
6812
John Reck59135872010-11-02 12:39:01 -07006813MaybeObject* JSObject::SetElementsLength(Object* len) {
Steve Block3ce2e202009-11-05 08:53:23 +00006814 // We should never end in here with a pixel or external array.
Steve Block6ded16b2010-05-10 14:33:55 +01006815 ASSERT(AllowsSetElementsLength());
Steve Blocka7e24c12009-10-30 11:49:00 +00006816
John Reck59135872010-11-02 12:39:01 -07006817 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 Block8defd9f2010-07-08 12:39:36 +01006820 const int value = Smi::cast(smi_length)->value();
Ben Murdoch8b112d22011-06-08 16:22:53 +01006821 if (value < 0) return ArrayLengthRangeError(GetHeap());
Steve Blocka7e24c12009-10-30 11:49:00 +00006822 switch (GetElementsKind()) {
6823 case FAST_ELEMENTS: {
6824 int old_capacity = FixedArray::cast(elements())->length();
6825 if (value <= old_capacity) {
6826 if (IsJSArray()) {
John Reck59135872010-11-02 12:39:01 -07006827 Object* obj;
6828 { MaybeObject* maybe_obj = EnsureWritableFastElements();
6829 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6830 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006831 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 Clarke4515c472010-02-03 11:58:03 +00006838 JSArray::cast(this)->set_length(Smi::cast(smi_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00006839 }
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 Reck59135872010-11-02 12:39:01 -07006846 Object* obj;
6847 { MaybeObject* maybe_obj =
6848 SetFastElementsCapacityAndLength(new_capacity, value);
6849 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6850 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006851 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 Reck59135872010-11-02 12:39:01 -07006861 Object* obj;
6862 { MaybeObject* maybe_obj = ResetElements();
6863 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6864 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006865 } 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 Clarke4515c472010-02-03 11:58:03 +00006871 JSArray::cast(this)->set_length(Smi::cast(smi_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00006872 }
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 Murdoch7f4d5bd2010-06-15 11:15:29 +01006884 if (len->ToArrayIndex(&length)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006885 return SetSlowElements(len);
6886 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01006887 return ArrayLengthRangeError(GetHeap());
Steve Blocka7e24c12009-10-30 11:49:00 +00006888 }
6889 }
6890
6891 // len is not a number so make the array size one and
6892 // set only element to len.
John Reck59135872010-11-02 12:39:01 -07006893 Object* obj;
Ben Murdoch8b112d22011-06-08 16:22:53 +01006894 { MaybeObject* maybe_obj = GetHeap()->AllocateFixedArray(1);
John Reck59135872010-11-02 12:39:01 -07006895 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
6896 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006897 FixedArray::cast(obj)->set(0, len);
Leon Clarke4515c472010-02-03 11:58:03 +00006898 if (IsJSArray()) JSArray::cast(this)->set_length(Smi::FromInt(1));
Steve Blocka7e24c12009-10-30 11:49:00 +00006899 set_elements(FixedArray::cast(obj));
6900 return this;
6901}
6902
6903
Steve Block053d10c2011-06-13 19:13:29 +01006904Object* 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
6916MaybeObject* 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 Reck59135872010-11-02 12:39:01 -07006947MaybeObject* JSObject::SetPrototype(Object* value,
6948 bool skip_hidden_prototypes) {
Steve Block44f0eee2011-05-26 01:26:41 +01006949 Heap* heap = GetHeap();
Andrei Popescu402d9372010-02-26 13:31:12 +00006950 // 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 Murdoch8b112d22011-06-08 16:22:53 +01006954 // 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 Popescu402d9372010-02-26 13:31:12 +00006970 // 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 Block44f0eee2011-05-26 01:26:41 +01006974 for (Object* pt = value; pt != heap->null_value(); pt = pt->GetPrototype()) {
Andrei Popescu402d9372010-02-26 13:31:12 +00006975 if (JSObject::cast(pt) == this) {
6976 // Cycle detected.
6977 HandleScope scope;
Steve Block44f0eee2011-05-26 01:26:41 +01006978 return heap->isolate()->Throw(
6979 *FACTORY->NewError("cyclic_proto", HandleVector<Object>(NULL, 0)));
Andrei Popescu402d9372010-02-26 13:31:12 +00006980 }
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 Block053d10c2011-06-13 19:13:29 +01006997 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 Reck59135872010-11-02 12:39:01 -07007014 }
Steve Block053d10c2011-06-13 19:13:29 +01007015 ASSERT(Map::cast(new_map)->prototype() == value);
Andrei Popescu402d9372010-02-26 13:31:12 +00007016 real_receiver->set_map(Map::cast(new_map));
7017
Steve Block44f0eee2011-05-26 01:26:41 +01007018 heap->ClearInstanceofCache();
Kristian Monsen25f61362010-05-21 11:50:48 +01007019
Andrei Popescu402d9372010-02-26 13:31:12 +00007020 return value;
7021}
7022
7023
Steve Blocka7e24c12009-10-30 11:49:00 +00007024bool 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 Block44f0eee2011-05-26 01:26:41 +01007037 case EXTERNAL_PIXEL_ELEMENTS: {
7038 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00007039 if (index < static_cast<uint32_t>(pixels->length())) {
7040 return true;
7041 }
7042 break;
7043 }
Steve Block3ce2e202009-11-05 08:53:23 +00007044 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 Block3ce2e202009-11-05 08:53:23 +00007051 ExternalArray* array = ExternalArray::cast(elements());
7052 if (index < static_cast<uint32_t>(array->length())) {
7053 return true;
7054 }
7055 break;
7056 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007057 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 Block44f0eee2011-05-26 01:26:41 +01007073 if (pt->IsNull()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00007074 return JSObject::cast(pt)->HasElementWithReceiver(receiver, index);
7075}
7076
7077
7078bool JSObject::HasElementWithInterceptor(JSObject* receiver, uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01007079 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007080 // Make sure that the top context does not change when doing
7081 // callbacks or interceptor calls.
7082 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01007083 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007084 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
7085 Handle<JSObject> receiver_handle(receiver);
7086 Handle<JSObject> holder_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01007087 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007088 v8::AccessorInfo info(args.end());
7089 if (!interceptor->query()->IsUndefined()) {
7090 v8::IndexedPropertyQuery query =
7091 v8::ToCData<v8::IndexedPropertyQuery>(interceptor->query());
Steve Block44f0eee2011-05-26 01:26:41 +01007092 LOG(isolate,
7093 ApiIndexedPropertyAccess("interceptor-indexed-has", this, index));
Iain Merrick75681382010-08-19 15:07:18 +01007094 v8::Handle<v8::Integer> result;
Steve Blocka7e24c12009-10-30 11:49:00 +00007095 {
7096 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007097 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00007098 result = query(index, info);
7099 }
Iain Merrick75681382010-08-19 15:07:18 +01007100 if (!result.IsEmpty()) {
7101 ASSERT(result->IsInt32());
7102 return true; // absence of property is signaled by empty handle.
7103 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007104 } else if (!interceptor->getter()->IsUndefined()) {
7105 v8::IndexedPropertyGetter getter =
7106 v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01007107 LOG(isolate,
7108 ApiIndexedPropertyAccess("interceptor-indexed-has-get", this, index));
Steve Blocka7e24c12009-10-30 11:49:00 +00007109 v8::Handle<v8::Value> result;
7110 {
7111 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007112 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00007113 result = getter(index, info);
7114 }
7115 if (!result.IsEmpty()) return true;
7116 }
7117 return holder_handle->HasElementPostInterceptor(*receiver_handle, index);
7118}
7119
7120
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007121JSObject::LocalElementType JSObject::HasLocalElement(uint32_t index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007122 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01007123 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 Blocka7e24c12009-10-30 11:49:00 +00007129 }
7130
Steve Block1e0659c2011-05-24 12:43:12 +01007131 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 Blocka7e24c12009-10-30 11:49:00 +00007138 // Check for lookup interceptor
7139 if (HasIndexedInterceptor()) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007140 return HasElementWithInterceptor(this, index) ? INTERCEPTED_ELEMENT
7141 : UNDEFINED_ELEMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +00007142 }
7143
7144 // Handle [] on String objects.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007145 if (this->IsStringObjectWithCharacterAt(index)) {
7146 return STRING_CHARACTER_ELEMENT;
7147 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007148
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 Monsen0d5e1162010-09-30 15:31:59 +01007155 if ((index < length) &&
7156 !FixedArray::cast(elements())->get(index)->IsTheHole()) {
7157 return FAST_ELEMENT;
7158 }
7159 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007160 }
Steve Block44f0eee2011-05-26 01:26:41 +01007161 case EXTERNAL_PIXEL_ELEMENTS: {
7162 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007163 if (index < static_cast<uint32_t>(pixels->length())) return FAST_ELEMENT;
7164 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007165 }
Steve Block3ce2e202009-11-05 08:53:23 +00007166 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 Monsen0d5e1162010-09-30 15:31:59 +01007174 if (index < static_cast<uint32_t>(array->length())) return FAST_ELEMENT;
7175 break;
Steve Block3ce2e202009-11-05 08:53:23 +00007176 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007177 case DICTIONARY_ELEMENTS: {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007178 if (element_dictionary()->FindEntry(index) !=
7179 NumberDictionary::kNotFound) {
7180 return DICTIONARY_ELEMENT;
7181 }
7182 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007183 }
7184 default:
7185 UNREACHABLE();
7186 break;
7187 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007188
7189 return UNDEFINED_ELEMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +00007190}
7191
7192
7193bool JSObject::HasElementWithReceiver(JSObject* receiver, uint32_t index) {
7194 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01007195 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 Blocka7e24c12009-10-30 11:49:00 +00007201 }
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 Block44f0eee2011-05-26 01:26:41 +01007218 case EXTERNAL_PIXEL_ELEMENTS: {
7219 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00007220 if (index < static_cast<uint32_t>(pixels->length())) {
7221 return true;
7222 }
7223 break;
7224 }
Steve Block3ce2e202009-11-05 08:53:23 +00007225 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 Blocka7e24c12009-10-30 11:49:00 +00007238 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 Block44f0eee2011-05-26 01:26:41 +01007254 if (pt->IsNull()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00007255 return JSObject::cast(pt)->HasElementWithReceiver(receiver, index);
7256}
7257
7258
John Reck59135872010-11-02 12:39:01 -07007259MaybeObject* JSObject::SetElementWithInterceptor(uint32_t index,
Steve Block9fac8402011-05-12 15:51:54 +01007260 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007261 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01007262 bool check_prototype) {
Steve Block44f0eee2011-05-26 01:26:41 +01007263 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007264 // Make sure that the top context does not change when doing
7265 // callbacks or interceptor calls.
7266 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01007267 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007268 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
7269 Handle<JSObject> this_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01007270 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007271 if (!interceptor->setter()->IsUndefined()) {
7272 v8::IndexedPropertySetter setter =
7273 v8::ToCData<v8::IndexedPropertySetter>(interceptor->setter());
Steve Block44f0eee2011-05-26 01:26:41 +01007274 LOG(isolate,
7275 ApiIndexedPropertyAccess("interceptor-indexed-set", this, index));
7276 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007277 v8::AccessorInfo info(args.end());
7278 v8::Handle<v8::Value> result;
7279 {
7280 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007281 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00007282 result = setter(index, v8::Utils::ToLocal(value_handle), info);
7283 }
Steve Block44f0eee2011-05-26 01:26:41 +01007284 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007285 if (!result.IsEmpty()) return *value_handle;
7286 }
John Reck59135872010-11-02 12:39:01 -07007287 MaybeObject* raw_result =
Steve Block9fac8402011-05-12 15:51:54 +01007288 this_handle->SetElementWithoutInterceptor(index,
7289 *value_handle,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007290 strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01007291 check_prototype);
Steve Block44f0eee2011-05-26 01:26:41 +01007292 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007293 return raw_result;
7294}
7295
7296
John Reck59135872010-11-02 12:39:01 -07007297MaybeObject* JSObject::GetElementWithCallback(Object* receiver,
7298 Object* structure,
7299 uint32_t index,
7300 Object* holder) {
Steve Block44f0eee2011-05-26 01:26:41 +01007301 Isolate* isolate = GetIsolate();
Leon Clarkef7060e22010-06-03 12:02:55 +01007302 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 Block44f0eee2011-05-26 01:26:41 +01007309 HandleScope scope(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01007310 Handle<JSObject> self(JSObject::cast(receiver));
7311 Handle<JSObject> holder_handle(JSObject::cast(holder));
Steve Block44f0eee2011-05-26 01:26:41 +01007312 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 Clarkef7060e22010-06-03 12:02:55 +01007316 v8::AccessorInfo info(args.end());
7317 v8::Handle<v8::Value> result;
7318 {
7319 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007320 VMState state(isolate, EXTERNAL);
Leon Clarkef7060e22010-06-03 12:02:55 +01007321 result = call_fun(v8::Utils::ToLocal(key), info);
7322 }
Steve Block44f0eee2011-05-26 01:26:41 +01007323 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
7324 if (result.IsEmpty()) return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01007325 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 Block44f0eee2011-05-26 01:26:41 +01007336 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01007337 }
7338
7339 UNREACHABLE();
7340 return NULL;
7341}
7342
7343
John Reck59135872010-11-02 12:39:01 -07007344MaybeObject* JSObject::SetElementWithCallback(Object* structure,
7345 uint32_t index,
7346 Object* value,
7347 JSObject* holder) {
Steve Block44f0eee2011-05-26 01:26:41 +01007348 Isolate* isolate = GetIsolate();
7349 HandleScope scope(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01007350
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 Block44f0eee2011-05-26 01:26:41 +01007354 Handle<Object> value_handle(value, isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01007355
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 Block44f0eee2011-05-26 01:26:41 +01007367 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 Clarkef7060e22010-06-03 12:02:55 +01007371 v8::AccessorInfo info(args.end());
7372 {
7373 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007374 VMState state(isolate, EXTERNAL);
Leon Clarkef7060e22010-06-03 12:02:55 +01007375 call_fun(v8::Utils::ToLocal(key),
7376 v8::Utils::ToLocal(value_handle),
7377 info);
7378 }
Steve Block44f0eee2011-05-26 01:26:41 +01007379 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01007380 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 Block44f0eee2011-05-26 01:26:41 +01007388 Handle<Object> holder_handle(holder, isolate);
7389 Handle<Object> key(isolate->factory()->NewNumberFromUint(index));
Leon Clarkef7060e22010-06-03 12:02:55 +01007390 Handle<Object> args[2] = { key, holder_handle };
Steve Block44f0eee2011-05-26 01:26:41 +01007391 return isolate->Throw(
7392 *isolate->factory()->NewTypeError("no_setter_in_callback",
7393 HandleVector(args, 2)));
Leon Clarkef7060e22010-06-03 12:02:55 +01007394 }
7395 }
7396
7397 UNREACHABLE();
7398 return NULL;
7399}
7400
7401
Steve Blocka7e24c12009-10-30 11:49:00 +00007402// 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 Block9fac8402011-05-12 15:51:54 +01007405MaybeObject* JSObject::SetFastElement(uint32_t index,
7406 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007407 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01007408 bool check_prototype) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007409 ASSERT(HasFastElements());
7410
John Reck59135872010-11-02 12:39:01 -07007411 Object* elms_obj;
7412 { MaybeObject* maybe_elms_obj = EnsureWritableFastElements();
7413 if (!maybe_elms_obj->ToObject(&elms_obj)) return maybe_elms_obj;
7414 }
Iain Merrick75681382010-08-19 15:07:18 +01007415 FixedArray* elms = FixedArray::cast(elms_obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00007416 uint32_t elms_length = static_cast<uint32_t>(elms->length());
7417
Steve Block9fac8402011-05-12 15:51:54 +01007418 if (check_prototype &&
Steve Block1e0659c2011-05-24 12:43:12 +01007419 (index >= elms_length || elms->get(index)->IsTheHole())) {
7420 bool found;
7421 MaybeObject* result =
7422 SetElementWithCallbackSetterInPrototypes(index, value, &found);
7423 if (found) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00007424 }
7425
Steve Block9fac8402011-05-12 15:51:54 +01007426
Steve Blocka7e24c12009-10-30 11:49:00 +00007427 // 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 Murdoch7f4d5bd2010-06-15 11:15:29 +01007433 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00007434 if (index >= array_length) {
Leon Clarke4515c472010-02-03 11:58:03 +00007435 JSArray::cast(this)->set_length(Smi::FromInt(index + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00007436 }
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 Reck59135872010-11-02 12:39:01 -07007448 Object* obj;
7449 { MaybeObject* maybe_obj =
7450 SetFastElementsCapacityAndLength(new_capacity, index + 1);
7451 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
7452 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007453 FixedArray::cast(elements())->set(index, value);
7454 return value;
7455 }
7456 }
7457
7458 // Otherwise default to slow case.
John Reck59135872010-11-02 12:39:01 -07007459 Object* obj;
7460 { MaybeObject* maybe_obj = NormalizeElements();
7461 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
7462 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007463 ASSERT(HasDictionaryElements());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007464 return SetElement(index, value, strict_mode, check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00007465}
7466
Iain Merrick75681382010-08-19 15:07:18 +01007467
Steve Block9fac8402011-05-12 15:51:54 +01007468MaybeObject* JSObject::SetElement(uint32_t index,
7469 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007470 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01007471 bool check_prototype) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007472 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01007473 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 Blocka7e24c12009-10-30 11:49:00 +00007481 }
7482
7483 if (IsJSGlobalProxy()) {
7484 Object* proto = GetPrototype();
7485 if (proto->IsNull()) return value;
7486 ASSERT(proto->IsJSGlobalObject());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007487 return JSObject::cast(proto)->SetElement(index,
7488 value,
7489 strict_mode,
7490 check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00007491 }
7492
7493 // Check for lookup interceptor
7494 if (HasIndexedInterceptor()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007495 return SetElementWithInterceptor(index,
7496 value,
7497 strict_mode,
7498 check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00007499 }
7500
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007501 return SetElementWithoutInterceptor(index,
7502 value,
7503 strict_mode,
7504 check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00007505}
7506
7507
John Reck59135872010-11-02 12:39:01 -07007508MaybeObject* JSObject::SetElementWithoutInterceptor(uint32_t index,
Steve Block9fac8402011-05-12 15:51:54 +01007509 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007510 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01007511 bool check_prototype) {
Steve Block44f0eee2011-05-26 01:26:41 +01007512 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007513 switch (GetElementsKind()) {
7514 case FAST_ELEMENTS:
7515 // Fast case.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007516 return SetFastElement(index, value, strict_mode, check_prototype);
Steve Block44f0eee2011-05-26 01:26:41 +01007517 case EXTERNAL_PIXEL_ELEMENTS: {
7518 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00007519 return pixels->SetValue(index, value);
7520 }
Steve Block3ce2e202009-11-05 08:53:23 +00007521 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 Blocka7e24c12009-10-30 11:49:00 +00007552 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 Clarkef7060e22010-06-03 12:02:55 +01007562 return SetElementWithCallback(element, index, value, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007563 } else {
7564 dictionary->UpdateMaxNumberKey(index);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007565 // If put fails instrict mode, throw exception.
7566 if (!dictionary->ValueAtPut(entry, value) &&
7567 strict_mode == kStrictMode) {
Steve Block44f0eee2011-05-26 01:26:41 +01007568 Handle<Object> number(isolate->factory()->NewNumberFromUint(index));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007569 Handle<Object> holder(this);
7570 Handle<Object> args[2] = { number, holder };
Steve Block44f0eee2011-05-26 01:26:41 +01007571 return isolate->Throw(
7572 *isolate->factory()->NewTypeError("strict_read_only_property",
7573 HandleVector(args, 2)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007574 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007575 }
7576 } else {
7577 // Index not already used. Look for an accessor in the prototype chain.
Steve Block1e0659c2011-05-24 12:43:12 +01007578 if (check_prototype) {
7579 bool found;
7580 MaybeObject* result =
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007581 // Strict mode not needed. No-setter case already handled.
Steve Block1e0659c2011-05-24 12:43:12 +01007582 SetElementWithCallbackSetterInPrototypes(index, value, &found);
7583 if (found) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00007584 }
Steve Block8defd9f2010-07-08 12:39:36 +01007585 // 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 Block44f0eee2011-05-26 01:26:41 +01007588 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 Block8defd9f2010-07-08 12:39:36 +01007599 }
John Reck59135872010-11-02 12:39:01 -07007600 Object* result;
7601 { MaybeObject* maybe_result = dictionary->AtNumberPut(index, value);
7602 if (!maybe_result->ToObject(&result)) return maybe_result;
7603 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007604 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 Reck59135872010-11-02 12:39:01 -07007612 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 Blocka7e24c12009-10-30 11:49:00 +00007619 }
7620
7621 // Attempt to put this object back in fast case.
7622 if (ShouldConvertToFastElements()) {
7623 uint32_t new_length = 0;
7624 if (IsJSArray()) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007625 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00007626 } else {
7627 new_length = NumberDictionary::cast(elements())->max_number_key() + 1;
7628 }
John Reck59135872010-11-02 12:39:01 -07007629 Object* obj;
7630 { MaybeObject* maybe_obj =
7631 SetFastElementsCapacityAndLength(new_length, new_length);
7632 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
7633 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007634#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 Block44f0eee2011-05-26 01:26:41 +01007651 return isolate->heap()->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007652}
7653
7654
John Reck59135872010-11-02 12:39:01 -07007655MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index,
7656 Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007657 uint32_t old_len = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007658 CHECK(length()->ToArrayIndex(&old_len));
Steve Blocka7e24c12009-10-30 11:49:00 +00007659 // 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 Reck59135872010-11-02 12:39:01 -07007662 Object* len;
7663 { MaybeObject* maybe_len =
Steve Block44f0eee2011-05-26 01:26:41 +01007664 GetHeap()->NumberFromDouble(static_cast<double>(index) + 1);
John Reck59135872010-11-02 12:39:01 -07007665 if (!maybe_len->ToObject(&len)) return maybe_len;
7666 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007667 set_length(len);
7668 }
7669 return value;
7670}
7671
7672
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007673MaybeObject* JSObject::GetElementPostInterceptor(Object* receiver,
John Reck59135872010-11-02 12:39:01 -07007674 uint32_t index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007675 // 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 Block44f0eee2011-05-26 01:26:41 +01007686 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00007687 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 Murdoche0cee9b2011-05-25 10:26:03 +01007694 MaybeObject* maybe_value = GetExternalElement(index);
7695 Object* value;
7696 if (!maybe_value->ToObject(&value)) return maybe_value;
7697 if (!value->IsUndefined()) return value;
Steve Block3ce2e202009-11-05 08:53:23 +00007698 break;
7699 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007700 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 Clarkef7060e22010-06-03 12:02:55 +01007707 return GetElementWithCallback(receiver,
7708 element,
7709 index,
7710 this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007711 }
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 Murdoch8b112d22011-06-08 16:22:53 +01007723 if (pt->IsNull()) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007724 return pt->GetElementWithReceiver(receiver, index);
7725}
7726
7727
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007728MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver,
John Reck59135872010-11-02 12:39:01 -07007729 uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01007730 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007731 // Make sure that the top context does not change when doing
7732 // callbacks or interceptor calls.
7733 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01007734 HandleScope scope(isolate);
7735 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate);
7736 Handle<Object> this_handle(receiver, isolate);
7737 Handle<JSObject> holder_handle(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007738 if (!interceptor->getter()->IsUndefined()) {
7739 v8::IndexedPropertyGetter getter =
7740 v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01007741 LOG(isolate,
7742 ApiIndexedPropertyAccess("interceptor-indexed-get", this, index));
7743 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00007744 v8::AccessorInfo info(args.end());
7745 v8::Handle<v8::Value> result;
7746 {
7747 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01007748 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00007749 result = getter(index, info);
7750 }
Steve Block44f0eee2011-05-26 01:26:41 +01007751 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007752 if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result);
7753 }
7754
John Reck59135872010-11-02 12:39:01 -07007755 MaybeObject* raw_result =
Steve Blocka7e24c12009-10-30 11:49:00 +00007756 holder_handle->GetElementPostInterceptor(*this_handle, index);
Steve Block44f0eee2011-05-26 01:26:41 +01007757 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007758 return raw_result;
7759}
7760
7761
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007762MaybeObject* JSObject::GetElementWithReceiver(Object* receiver,
John Reck59135872010-11-02 12:39:01 -07007763 uint32_t index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007764 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01007765 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 Blocka7e24c12009-10-30 11:49:00 +00007771 }
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 Block44f0eee2011-05-26 01:26:41 +01007788 case EXTERNAL_PIXEL_ELEMENTS:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007789 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 Murdoch8b112d22011-06-08 16:22:53 +01007821 Heap* heap = GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01007822 if (pt == heap->null_value()) return heap->undefined_value();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007823 return pt->GetElementWithReceiver(receiver, index);
7824}
7825
7826
7827MaybeObject* JSObject::GetExternalElement(uint32_t index) {
7828 // Get element works for both JSObject and JSArray since
7829 // JSArray::length cannot change.
7830 switch (GetElementsKind()) {
Steve Block44f0eee2011-05-26 01:26:41 +01007831 case EXTERNAL_PIXEL_ELEMENTS: {
7832 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00007833 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 Block3ce2e202009-11-05 08:53:23 +00007839 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 Block44f0eee2011-05-26 01:26:41 +01007877 return GetHeap()->NumberFromInt32(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007878 }
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 Block44f0eee2011-05-26 01:26:41 +01007886 return GetHeap()->NumberFromUint32(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007887 }
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 Block44f0eee2011-05-26 01:26:41 +01007894 return GetHeap()->AllocateHeapNumber(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007895 }
7896 break;
7897 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007898 case FAST_ELEMENTS:
7899 case DICTIONARY_ELEMENTS:
7900 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00007901 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007902 }
Steve Block44f0eee2011-05-26 01:26:41 +01007903 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00007904}
7905
7906
7907bool 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 Block44f0eee2011-05-26 01:26:41 +01007920 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00007921 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 Blocka7e24c12009-10-30 11:49:00 +00007928 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
7946bool 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
7956bool 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 Murdoch7f4d5bd2010-06-15 11:15:29 +01007972 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length));
Steve Blocka7e24c12009-10-30 11:49:00 +00007973 } 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 Murdochb0fe1622011-05-05 13:52:32 +01007986#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00007987template<typename Shape, typename Key>
Ben Murdochb0fe1622011-05-05 13:52:32 +01007988void Dictionary<Shape, Key>::Print(FILE* out) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007989 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 Murdochb0fe1622011-05-05 13:52:32 +01007993 PrintF(out, " ");
Steve Blocka7e24c12009-10-30 11:49:00 +00007994 if (k->IsString()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01007995 String::cast(k)->StringPrint(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00007996 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01007997 k->ShortPrint(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00007998 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01007999 PrintF(out, ": ");
8000 ValueAt(i)->ShortPrint(out);
8001 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00008002 }
8003 }
8004}
8005#endif
8006
8007
8008template<typename Shape, typename Key>
8009void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) {
8010 int pos = 0;
8011 int capacity = HashTable<Shape, Key>::Capacity();
Leon Clarke4515c472010-02-03 11:58:03 +00008012 AssertNoAllocation no_gc;
8013 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00008014 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
8024InterceptorInfo* JSObject::GetNamedInterceptor() {
8025 ASSERT(map()->has_named_interceptor());
8026 JSFunction* constructor = JSFunction::cast(map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01008027 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00008028 Object* result =
Steve Block6ded16b2010-05-10 14:33:55 +01008029 constructor->shared()->get_api_func_data()->named_property_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00008030 return InterceptorInfo::cast(result);
8031}
8032
8033
8034InterceptorInfo* JSObject::GetIndexedInterceptor() {
8035 ASSERT(map()->has_indexed_interceptor());
8036 JSFunction* constructor = JSFunction::cast(map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01008037 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00008038 Object* result =
Steve Block6ded16b2010-05-10 14:33:55 +01008039 constructor->shared()->get_api_func_data()->indexed_property_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00008040 return InterceptorInfo::cast(result);
8041}
8042
8043
John Reck59135872010-11-02 12:39:01 -07008044MaybeObject* JSObject::GetPropertyPostInterceptor(
8045 JSObject* receiver,
8046 String* name,
8047 PropertyAttributes* attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008048 // Check local property in holder, ignore interceptor.
8049 LookupResult result;
8050 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00008051 if (result.IsProperty()) {
8052 return GetProperty(receiver, &result, name, attributes);
8053 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008054 // Continue searching via the prototype chain.
8055 Object* pt = GetPrototype();
8056 *attributes = ABSENT;
Ben Murdoch8b112d22011-06-08 16:22:53 +01008057 if (pt->IsNull()) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00008058 return pt->GetPropertyWithReceiver(receiver, name, attributes);
8059}
8060
8061
John Reck59135872010-11-02 12:39:01 -07008062MaybeObject* JSObject::GetLocalPropertyPostInterceptor(
Steve Blockd0582a62009-12-15 09:54:21 +00008063 JSObject* receiver,
8064 String* name,
8065 PropertyAttributes* attributes) {
8066 // Check local property in holder, ignore interceptor.
8067 LookupResult result;
8068 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00008069 if (result.IsProperty()) {
8070 return GetProperty(receiver, &result, name, attributes);
8071 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01008072 return GetHeap()->undefined_value();
Steve Blockd0582a62009-12-15 09:54:21 +00008073}
8074
8075
John Reck59135872010-11-02 12:39:01 -07008076MaybeObject* JSObject::GetPropertyWithInterceptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00008077 JSObject* receiver,
8078 String* name,
8079 PropertyAttributes* attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01008080 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00008081 InterceptorInfo* interceptor = GetNamedInterceptor();
Steve Block44f0eee2011-05-26 01:26:41 +01008082 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008083 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 Block44f0eee2011-05-26 01:26:41 +01008090 LOG(isolate,
8091 ApiNamedPropertyAccess("interceptor-named-get", *holder_handle, name));
8092 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008093 v8::AccessorInfo info(args.end());
8094 v8::Handle<v8::Value> result;
8095 {
8096 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01008097 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00008098 result = getter(v8::Utils::ToLocal(name_handle), info);
8099 }
Steve Block44f0eee2011-05-26 01:26:41 +01008100 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008101 if (!result.IsEmpty()) {
8102 *attributes = NONE;
8103 return *v8::Utils::OpenHandle(*result);
8104 }
8105 }
8106
John Reck59135872010-11-02 12:39:01 -07008107 MaybeObject* result = holder_handle->GetPropertyPostInterceptor(
Steve Blocka7e24c12009-10-30 11:49:00 +00008108 *receiver_handle,
8109 *name_handle,
8110 attributes);
Steve Block44f0eee2011-05-26 01:26:41 +01008111 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008112 return result;
8113}
8114
8115
8116bool JSObject::HasRealNamedProperty(String* key) {
8117 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01008118 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 Blocka7e24c12009-10-30 11:49:00 +00008124 }
8125
8126 LookupResult result;
8127 LocalLookupRealNamedProperty(key, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00008128 return result.IsProperty() && (result.type() != INTERCEPTOR);
Steve Blocka7e24c12009-10-30 11:49:00 +00008129}
8130
8131
8132bool JSObject::HasRealElementProperty(uint32_t index) {
8133 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01008134 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 Blocka7e24c12009-10-30 11:49:00 +00008140 }
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 Block44f0eee2011-05-26 01:26:41 +01008154 case EXTERNAL_PIXEL_ELEMENTS: {
8155 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00008156 return index < static_cast<uint32_t>(pixels->length());
8157 }
Steve Block3ce2e202009-11-05 08:53:23 +00008158 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 Blocka7e24c12009-10-30 11:49:00 +00008168 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 Murdoch8b112d22011-06-08 16:22:53 +01008178 return GetHeap()->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00008179}
8180
8181
8182bool JSObject::HasRealNamedCallbackProperty(String* key) {
8183 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01008184 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 Blocka7e24c12009-10-30 11:49:00 +00008190 }
8191
8192 LookupResult result;
8193 LocalLookupRealNamedProperty(key, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00008194 return result.IsProperty() && (result.type() == CALLBACKS);
Steve Blocka7e24c12009-10-30 11:49:00 +00008195}
8196
8197
8198int 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 Murdoch8b112d22011-06-08 16:22:53 +01008203 PropertyDetails details(descs->GetDetails(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00008204 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
8215int JSObject::NumberOfEnumProperties() {
8216 return NumberOfLocalProperties(static_cast<PropertyAttributes>(DONT_ENUM));
8217}
8218
8219
8220void 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
8232static 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
8247void 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.
8294void 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.
8336void 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
8350int JSObject::NumberOfLocalElements(PropertyAttributes filter) {
8351 return GetLocalElementKeys(NULL, filter);
8352}
8353
8354
8355int JSObject::NumberOfEnumElements() {
Steve Blockd0582a62009-12-15 09:54:21 +00008356 // 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 Blocka7e24c12009-10-30 11:49:00 +00008365 return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM));
8366}
8367
8368
8369int 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 Clarke4515c472010-02-03 11:58:03 +00008380 storage->set(counter, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00008381 }
8382 counter++;
8383 }
8384 }
8385 ASSERT(!storage || storage->length() >= counter);
8386 break;
8387 }
Steve Block44f0eee2011-05-26 01:26:41 +01008388 case EXTERNAL_PIXEL_ELEMENTS: {
8389 int length = ExternalPixelArray::cast(elements())->length();
Steve Blocka7e24c12009-10-30 11:49:00 +00008390 while (counter < length) {
8391 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +00008392 storage->set(counter, Smi::FromInt(counter));
Steve Blocka7e24c12009-10-30 11:49:00 +00008393 }
8394 counter++;
8395 }
8396 ASSERT(!storage || storage->length() >= counter);
8397 break;
8398 }
Steve Block3ce2e202009-11-05 08:53:23 +00008399 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 Clarke4515c472010-02-03 11:58:03 +00008409 storage->set(counter, Smi::FromInt(counter));
Steve Block3ce2e202009-11-05 08:53:23 +00008410 }
8411 counter++;
8412 }
8413 ASSERT(!storage || storage->length() >= counter);
8414 break;
8415 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008416 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 Clarke4515c472010-02-03 11:58:03 +00008434 storage->set(counter + i, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00008435 }
8436 }
8437 counter += str->length();
8438 }
8439 }
8440 ASSERT(!storage || storage->length() == counter);
8441 return counter;
8442}
8443
8444
8445int JSObject::GetEnumElementKeys(FixedArray* storage) {
8446 return GetLocalElementKeys(storage,
8447 static_cast<PropertyAttributes>(DONT_ENUM));
8448}
8449
8450
Steve Blocka7e24c12009-10-30 11:49:00 +00008451// StringKey simply carries a string object as key.
8452class 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.
8479class StringSharedKey : public HashTableKey {
8480 public:
Steve Block1e0659c2011-05-24 12:43:12 +01008481 StringSharedKey(String* source,
8482 SharedFunctionInfo* shared,
8483 StrictModeFlag strict_mode)
8484 : source_(source),
8485 shared_(shared),
8486 strict_mode_(strict_mode) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00008487
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 Block1e0659c2011-05-24 12:43:12 +01008493 StrictModeFlag strict_mode = static_cast<StrictModeFlag>(
8494 Smi::cast(pair->get(2))->value());
8495 if (strict_mode != strict_mode_) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00008496 String* source = String::cast(pair->get(1));
8497 return source->Equals(source_);
8498 }
8499
8500 static uint32_t StringSharedHashHelper(String* source,
Steve Block1e0659c2011-05-24 12:43:12 +01008501 SharedFunctionInfo* shared,
8502 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008503 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 Block1e0659c2011-05-24 12:43:12 +01008512 if (strict_mode == kStrictMode) hash ^= 0x8000;
Steve Blocka7e24c12009-10-30 11:49:00 +00008513 hash += shared->start_position();
8514 }
8515 return hash;
8516 }
8517
8518 uint32_t Hash() {
Steve Block1e0659c2011-05-24 12:43:12 +01008519 return StringSharedHashHelper(source_, shared_, strict_mode_);
Steve Blocka7e24c12009-10-30 11:49:00 +00008520 }
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 Block1e0659c2011-05-24 12:43:12 +01008526 StrictModeFlag strict_mode = static_cast<StrictModeFlag>(
8527 Smi::cast(pair->get(2))->value());
8528 return StringSharedHashHelper(source, shared, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00008529 }
8530
John Reck59135872010-11-02 12:39:01 -07008531 MUST_USE_RESULT MaybeObject* AsObject() {
8532 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01008533 { MaybeObject* maybe_obj = source_->GetHeap()->AllocateFixedArray(3);
John Reck59135872010-11-02 12:39:01 -07008534 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
8535 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008536 FixedArray* pair = FixedArray::cast(obj);
8537 pair->set(0, shared_);
8538 pair->set(1, source_);
Steve Block1e0659c2011-05-24 12:43:12 +01008539 pair->set(2, Smi::FromInt(strict_mode_));
Steve Blocka7e24c12009-10-30 11:49:00 +00008540 return pair;
8541 }
8542
8543 private:
8544 String* source_;
8545 SharedFunctionInfo* shared_;
Steve Block1e0659c2011-05-24 12:43:12 +01008546 StrictModeFlag strict_mode_;
Steve Blocka7e24c12009-10-30 11:49:00 +00008547};
8548
8549
8550// RegExpKey carries the source and flags of a regular expression as key.
8551class RegExpKey : public HashTableKey {
8552 public:
8553 RegExpKey(String* string, JSRegExp::Flags flags)
8554 : string_(string),
8555 flags_(Smi::FromInt(flags.value())) { }
8556
Steve Block3ce2e202009-11-05 08:53:23 +00008557 // 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 Blocka7e24c12009-10-30 11:49:00 +00008561 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.
8591class Utf8SymbolKey : public HashTableKey {
8592 public:
8593 explicit Utf8SymbolKey(Vector<const char> string)
Steve Blockd0582a62009-12-15 09:54:21 +00008594 : string_(string), hash_field_(0) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00008595
8596 bool IsMatch(Object* string) {
8597 return String::cast(string)->IsEqualTo(string_);
8598 }
8599
8600 uint32_t Hash() {
Steve Blockd0582a62009-12-15 09:54:21 +00008601 if (hash_field_ != 0) return hash_field_ >> String::kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +00008602 unibrow::Utf8InputBuffer<> buffer(string_.start(),
8603 static_cast<unsigned>(string_.length()));
8604 chars_ = buffer.Length();
Steve Blockd0582a62009-12-15 09:54:21 +00008605 hash_field_ = String::ComputeHashField(&buffer, chars_);
8606 uint32_t result = hash_field_ >> String::kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +00008607 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 Reck59135872010-11-02 12:39:01 -07008615 MaybeObject* AsObject() {
Steve Blockd0582a62009-12-15 09:54:21 +00008616 if (hash_field_ == 0) Hash();
Steve Block44f0eee2011-05-26 01:26:41 +01008617 return Isolate::Current()->heap()->AllocateSymbol(
8618 string_, chars_, hash_field_);
Steve Blocka7e24c12009-10-30 11:49:00 +00008619 }
8620
8621 Vector<const char> string_;
Steve Blockd0582a62009-12-15 09:54:21 +00008622 uint32_t hash_field_;
Steve Blocka7e24c12009-10-30 11:49:00 +00008623 int chars_; // Caches the number of characters when computing the hash code.
8624};
8625
8626
Steve Block9fac8402011-05-12 15:51:54 +01008627template <typename Char>
8628class 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
8674class 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 Block44f0eee2011-05-26 01:26:41 +01008685 return HEAP->AllocateAsciiSymbol(string_, hash_field_);
Steve Block9fac8402011-05-12 15:51:54 +01008686 }
8687};
8688
8689
8690class 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 Block44f0eee2011-05-26 01:26:41 +01008701 return HEAP->AllocateTwoByteSymbol(string_, hash_field_);
Steve Block9fac8402011-05-12 15:51:54 +01008702 }
8703};
8704
8705
Steve Blocka7e24c12009-10-30 11:49:00 +00008706// SymbolKey carries a string/symbol object as key.
8707class SymbolKey : public HashTableKey {
8708 public:
Steve Block44f0eee2011-05-26 01:26:41 +01008709 explicit SymbolKey(String* string)
8710 : string_(string) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00008711
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 Reck59135872010-11-02 12:39:01 -07008722 MaybeObject* AsObject() {
Leon Clarkef7060e22010-06-03 12:02:55 +01008723 // Attempt to flatten the string, so that symbols will most often
8724 // be flat strings.
8725 string_ = string_->TryFlattenGetString();
Steve Block44f0eee2011-05-26 01:26:41 +01008726 Heap* heap = string_->GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00008727 // Transform string to symbol if possible.
Steve Block44f0eee2011-05-26 01:26:41 +01008728 Map* map = heap->SymbolMapForString(string_);
Steve Blocka7e24c12009-10-30 11:49:00 +00008729 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 Block44f0eee2011-05-26 01:26:41 +01008736 return heap->AllocateInternalSymbol(&buffer,
Steve Blocka7e24c12009-10-30 11:49:00 +00008737 string_->length(),
Steve Blockd0582a62009-12-15 09:54:21 +00008738 string_->hash_field());
Steve Blocka7e24c12009-10-30 11:49:00 +00008739 }
8740
8741 static uint32_t StringHash(Object* obj) {
8742 return String::cast(obj)->Hash();
8743 }
8744
8745 String* string_;
8746};
8747
8748
8749template<typename Shape, typename Key>
8750void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) {
8751 IteratePointers(v, 0, kElementsStartOffset);
8752}
8753
8754
8755template<typename Shape, typename Key>
8756void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) {
8757 IteratePointers(v,
8758 kElementsStartOffset,
8759 kHeaderSize + length() * kPointerSize);
8760}
8761
8762
8763template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07008764MaybeObject* HashTable<Shape, Key>::Allocate(int at_least_space_for,
8765 PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +01008766 const int kMinCapacity = 32;
8767 int capacity = RoundUpToPowerOf2(at_least_space_for * 2);
8768 if (capacity < kMinCapacity) {
8769 capacity = kMinCapacity; // Guarantee min capacity.
Leon Clarkee46be812010-01-19 14:06:41 +00008770 } else if (capacity > HashTable::kMaxCapacity) {
8771 return Failure::OutOfMemoryException();
8772 }
8773
John Reck59135872010-11-02 12:39:01 -07008774 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01008775 { MaybeObject* maybe_obj = Isolate::Current()->heap()->
8776 AllocateHashTable(EntryToIndex(capacity), pretenure);
John Reck59135872010-11-02 12:39:01 -07008777 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00008778 }
John Reck59135872010-11-02 12:39:01 -07008779 HashTable::cast(obj)->SetNumberOfElements(0);
8780 HashTable::cast(obj)->SetNumberOfDeletedElements(0);
8781 HashTable::cast(obj)->SetCapacity(capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00008782 return obj;
8783}
8784
8785
Leon Clarkee46be812010-01-19 14:06:41 +00008786// Find entry for key otherwise return kNotFound.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01008787int 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 Blocka7e24c12009-10-30 11:49:00 +00008826template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07008827MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, Key key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008828 int capacity = Capacity();
8829 int nof = NumberOfElements() + n;
Leon Clarkee46be812010-01-19 14:06:41 +00008830 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 Block6ded16b2010-05-10 14:33:55 +01008834 if (nod <= (capacity - nof) >> 1) {
8835 int needed_free = nof >> 1;
8836 if (nof + needed_free <= capacity) return this;
8837 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008838
Steve Block6ded16b2010-05-10 14:33:55 +01008839 const int kMinCapacityForPretenure = 256;
8840 bool pretenure =
Ben Murdoch8b112d22011-06-08 16:22:53 +01008841 (capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this);
John Reck59135872010-11-02 12:39:01 -07008842 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 Clarke4515c472010-02-03 11:58:03 +00008847
8848 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +00008849 HashTable* table = HashTable::cast(obj);
Leon Clarke4515c472010-02-03 11:58:03 +00008850 WriteBarrierMode mode = table->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00008851
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 Clarkee46be812010-01-19 14:06:41 +00008872 table->SetNumberOfDeletedElements(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00008873 return table;
8874}
8875
8876
8877template<typename Shape, typename Key>
8878uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) {
8879 uint32_t capacity = Capacity();
Leon Clarkee46be812010-01-19 14:06:41 +00008880 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 Blocka7e24c12009-10-30 11:49:00 +00008887 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008888 return entry;
8889}
8890
8891// Force instantiation of template instances class.
8892// Please note this list is compiler dependent.
8893
8894template class HashTable<SymbolTableShape, HashTableKey*>;
8895
8896template class HashTable<CompilationCacheShape, HashTableKey*>;
8897
8898template class HashTable<MapCacheShape, HashTableKey*>;
8899
8900template class Dictionary<StringDictionaryShape, String*>;
8901
8902template class Dictionary<NumberDictionaryShape, uint32_t>;
8903
John Reck59135872010-11-02 12:39:01 -07008904template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::Allocate(
Steve Blocka7e24c12009-10-30 11:49:00 +00008905 int);
8906
John Reck59135872010-11-02 12:39:01 -07008907template MaybeObject* Dictionary<StringDictionaryShape, String*>::Allocate(
Steve Blocka7e24c12009-10-30 11:49:00 +00008908 int);
8909
John Reck59135872010-11-02 12:39:01 -07008910template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::AtPut(
Steve Blocka7e24c12009-10-30 11:49:00 +00008911 uint32_t, Object*);
8912
8913template Object* Dictionary<NumberDictionaryShape, uint32_t>::SlowReverseLookup(
8914 Object*);
8915
8916template Object* Dictionary<StringDictionaryShape, String*>::SlowReverseLookup(
8917 Object*);
8918
8919template void Dictionary<NumberDictionaryShape, uint32_t>::CopyKeysTo(
8920 FixedArray*, PropertyAttributes);
8921
8922template Object* Dictionary<StringDictionaryShape, String*>::DeleteProperty(
8923 int, JSObject::DeleteMode);
8924
8925template Object* Dictionary<NumberDictionaryShape, uint32_t>::DeleteProperty(
8926 int, JSObject::DeleteMode);
8927
8928template void Dictionary<StringDictionaryShape, String*>::CopyKeysTo(
8929 FixedArray*);
8930
8931template int
8932Dictionary<StringDictionaryShape, String*>::NumberOfElementsFilterAttributes(
8933 PropertyAttributes);
8934
John Reck59135872010-11-02 12:39:01 -07008935template MaybeObject* Dictionary<StringDictionaryShape, String*>::Add(
Steve Blocka7e24c12009-10-30 11:49:00 +00008936 String*, Object*, PropertyDetails);
8937
John Reck59135872010-11-02 12:39:01 -07008938template MaybeObject*
Steve Blocka7e24c12009-10-30 11:49:00 +00008939Dictionary<StringDictionaryShape, String*>::GenerateNewEnumerationIndices();
8940
8941template int
8942Dictionary<NumberDictionaryShape, uint32_t>::NumberOfElementsFilterAttributes(
8943 PropertyAttributes);
8944
John Reck59135872010-11-02 12:39:01 -07008945template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::Add(
Steve Blocka7e24c12009-10-30 11:49:00 +00008946 uint32_t, Object*, PropertyDetails);
8947
John Reck59135872010-11-02 12:39:01 -07008948template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::
8949 EnsureCapacity(int, uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +00008950
John Reck59135872010-11-02 12:39:01 -07008951template MaybeObject* Dictionary<StringDictionaryShape, String*>::
8952 EnsureCapacity(int, String*);
Steve Blocka7e24c12009-10-30 11:49:00 +00008953
John Reck59135872010-11-02 12:39:01 -07008954template MaybeObject* Dictionary<NumberDictionaryShape, uint32_t>::AddEntry(
Steve Blocka7e24c12009-10-30 11:49:00 +00008955 uint32_t, Object*, PropertyDetails, uint32_t);
8956
John Reck59135872010-11-02 12:39:01 -07008957template MaybeObject* Dictionary<StringDictionaryShape, String*>::AddEntry(
Steve Blocka7e24c12009-10-30 11:49:00 +00008958 String*, Object*, PropertyDetails, uint32_t);
8959
8960template
8961int Dictionary<NumberDictionaryShape, uint32_t>::NumberOfEnumElements();
8962
8963template
8964int Dictionary<StringDictionaryShape, String*>::NumberOfEnumElements();
8965
Leon Clarkee46be812010-01-19 14:06:41 +00008966template
8967int HashTable<NumberDictionaryShape, uint32_t>::FindEntry(uint32_t);
8968
8969
Steve Blocka7e24c12009-10-30 11:49:00 +00008970// Collates undefined and unexisting elements below limit from position
8971// zero of the elements. The object stays in Dictionary mode.
John Reck59135872010-11-02 12:39:01 -07008972MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008973 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 Reck59135872010-11-02 12:39:01 -07008981 Object* new_double;
Ben Murdoch8b112d22011-06-08 16:22:53 +01008982 { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0);
John Reck59135872010-11-02 12:39:01 -07008983 if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double;
8984 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008985 result_double = HeapNumber::cast(new_double);
8986 }
8987
John Reck59135872010-11-02 12:39:01 -07008988 Object* obj;
8989 { MaybeObject* maybe_obj =
8990 NumberDictionary::Allocate(dict->NumberOfElements());
8991 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
8992 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008993 NumberDictionary* new_dict = NumberDictionary::cast(obj);
8994
8995 AssertNoAllocation no_alloc;
8996
8997 uint32_t pos = 0;
8998 uint32_t undefs = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01008999 int capacity = dict->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +00009000 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 Reck59135872010-11-02 12:39:01 -07009014 // 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 Blocka7e24c12009-10-30 11:49:00 +00009017 if (key < limit) {
9018 if (value->IsUndefined()) {
9019 undefs++;
9020 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01009021 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 Reck59135872010-11-02 12:39:01 -07009026 new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00009027 pos++;
9028 }
9029 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01009030 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 Reck59135872010-11-02 12:39:01 -07009035 new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00009036 }
9037 }
9038 }
9039
9040 uint32_t result = pos;
9041 PropertyDetails no_details = PropertyDetails(NONE, NORMAL);
Ben Murdoch8b112d22011-06-08 16:22:53 +01009042 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00009043 while (undefs > 0) {
Steve Block1e0659c2011-05-24 12:43:12 +01009044 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 Block44f0eee2011-05-26 01:26:41 +01009049 new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)->
John Reck59135872010-11-02 12:39:01 -07009050 ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +00009051 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 Reck59135872010-11-02 12:39:01 -07009071MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) {
Steve Block44f0eee2011-05-26 01:26:41 +01009072 ASSERT(!HasExternalArrayElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00009073
Ben Murdoch8b112d22011-06-08 16:22:53 +01009074 Heap* heap = GetHeap();
9075
Steve Blocka7e24c12009-10-30 11:49:00 +00009076 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 Reck59135872010-11-02 12:39:01 -07009086 Object* obj;
9087 { MaybeObject* maybe_obj = map()->GetFastElementsMap();
9088 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9089 }
Steve Block8defd9f2010-07-08 12:39:36 +01009090 Map* new_map = Map::cast(obj);
9091
Steve Block44f0eee2011-05-26 01:26:41 +01009092 PretenureFlag tenure = heap->InNewSpace(this) ? NOT_TENURED: TENURED;
John Reck59135872010-11-02 12:39:01 -07009093 Object* new_array;
9094 { MaybeObject* maybe_new_array =
Steve Block44f0eee2011-05-26 01:26:41 +01009095 heap->AllocateFixedArray(dict->NumberOfElements(), tenure);
John Reck59135872010-11-02 12:39:01 -07009096 if (!maybe_new_array->ToObject(&new_array)) return maybe_new_array;
9097 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009098 FixedArray* fast_elements = FixedArray::cast(new_array);
9099 dict->CopyValuesTo(fast_elements);
Steve Block8defd9f2010-07-08 12:39:36 +01009100
9101 set_map(new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00009102 set_elements(fast_elements);
Iain Merrick75681382010-08-19 15:07:18 +01009103 } else {
John Reck59135872010-11-02 12:39:01 -07009104 Object* obj;
9105 { MaybeObject* maybe_obj = EnsureWritableFastElements();
9106 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9107 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009108 }
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 Reck59135872010-11-02 12:39:01 -07009127 Object* new_double;
Steve Block44f0eee2011-05-26 01:26:41 +01009128 { MaybeObject* maybe_new_double = heap->AllocateHeapNumber(0.0);
John Reck59135872010-11-02 12:39:01 -07009129 if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double;
9130 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009131 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 Clarke4515c472010-02-03 11:58:03 +00009138 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc);
Steve Blocka7e24c12009-10-30 11:49:00 +00009139 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 Block44f0eee2011-05-26 01:26:41 +01009186Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009187 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 Block3ce2e202009-11-05 08:53:23 +00009221template<typename ExternalArrayClass, typename ValueType>
Steve Block44f0eee2011-05-26 01:26:41 +01009222static MaybeObject* ExternalArrayIntSetter(Heap* heap,
9223 ExternalArrayClass* receiver,
John Reck59135872010-11-02 12:39:01 -07009224 uint32_t index,
9225 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009226 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 Block44f0eee2011-05-26 01:26:41 +01009241 return heap->NumberFromInt32(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +00009242}
9243
9244
John Reck59135872010-11-02 12:39:01 -07009245MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009246 return ExternalArrayIntSetter<ExternalByteArray, int8_t>
Steve Block44f0eee2011-05-26 01:26:41 +01009247 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +00009248}
9249
9250
John Reck59135872010-11-02 12:39:01 -07009251MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index,
9252 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009253 return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t>
Steve Block44f0eee2011-05-26 01:26:41 +01009254 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +00009255}
9256
9257
John Reck59135872010-11-02 12:39:01 -07009258MaybeObject* ExternalShortArray::SetValue(uint32_t index,
9259 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009260 return ExternalArrayIntSetter<ExternalShortArray, int16_t>
Steve Block44f0eee2011-05-26 01:26:41 +01009261 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +00009262}
9263
9264
John Reck59135872010-11-02 12:39:01 -07009265MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index,
9266 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009267 return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t>
Steve Block44f0eee2011-05-26 01:26:41 +01009268 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +00009269}
9270
9271
John Reck59135872010-11-02 12:39:01 -07009272MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009273 return ExternalArrayIntSetter<ExternalIntArray, int32_t>
Steve Block44f0eee2011-05-26 01:26:41 +01009274 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +00009275}
9276
9277
John Reck59135872010-11-02 12:39:01 -07009278MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009279 uint32_t cast_value = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01009280 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +00009281 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 Block44f0eee2011-05-26 01:26:41 +01009295 return heap->NumberFromUint32(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +00009296}
9297
9298
John Reck59135872010-11-02 12:39:01 -07009299MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00009300 float cast_value = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01009301 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +00009302 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 Block44f0eee2011-05-26 01:26:41 +01009316 return heap->AllocateHeapNumber(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +00009317}
9318
9319
Ben Murdochb0fe1622011-05-05 13:52:32 +01009320JSGlobalPropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009321 ASSERT(!HasFastProperties());
9322 Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry());
Ben Murdochb0fe1622011-05-05 13:52:32 +01009323 return JSGlobalPropertyCell::cast(value);
Steve Blocka7e24c12009-10-30 11:49:00 +00009324}
9325
9326
John Reck59135872010-11-02 12:39:01 -07009327MaybeObject* GlobalObject::EnsurePropertyCell(String* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009328 ASSERT(!HasFastProperties());
9329 int entry = property_dictionary()->FindEntry(name);
9330 if (entry == StringDictionary::kNotFound) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01009331 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -07009332 Object* cell;
9333 { MaybeObject* maybe_cell =
Steve Block44f0eee2011-05-26 01:26:41 +01009334 heap->AllocateJSGlobalPropertyCell(heap->the_hole_value());
John Reck59135872010-11-02 12:39:01 -07009335 if (!maybe_cell->ToObject(&cell)) return maybe_cell;
9336 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009337 PropertyDetails details(NONE, NORMAL);
9338 details = details.AsDeleted();
John Reck59135872010-11-02 12:39:01 -07009339 Object* dictionary;
9340 { MaybeObject* maybe_dictionary =
9341 property_dictionary()->Add(name, cell, details);
9342 if (!maybe_dictionary->ToObject(&dictionary)) return maybe_dictionary;
9343 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009344 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 Reck59135872010-11-02 12:39:01 -07009354MaybeObject* SymbolTable::LookupString(String* string, Object** s) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009355 SymbolKey key(string);
9356 return LookupKey(&key, s);
9357}
9358
9359
Steve Blockd0582a62009-12-15 09:54:21 +00009360// 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.
9365class 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 Blocka7e24c12009-10-30 11:49:00 +00009421bool 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 Blockd0582a62009-12-15 09:54:21 +00009435bool 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 Reck59135872010-11-02 12:39:01 -07009451MaybeObject* SymbolTable::LookupSymbol(Vector<const char> str, Object** s) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009452 Utf8SymbolKey key(str);
9453 return LookupKey(&key, s);
9454}
9455
9456
Steve Block9fac8402011-05-12 15:51:54 +01009457MaybeObject* SymbolTable::LookupAsciiSymbol(Vector<const char> str,
9458 Object** s) {
9459 AsciiSymbolKey key(str);
9460 return LookupKey(&key, s);
9461}
9462
9463
9464MaybeObject* SymbolTable::LookupTwoByteSymbol(Vector<const uc16> str,
9465 Object** s) {
9466 TwoByteSymbolKey key(str);
9467 return LookupKey(&key, s);
9468}
9469
John Reck59135872010-11-02 12:39:01 -07009470MaybeObject* SymbolTable::LookupKey(HashTableKey* key, Object** s) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009471 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 Reck59135872010-11-02 12:39:01 -07009480 Object* obj;
9481 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
9482 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9483 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009484
9485 // Create symbol object.
John Reck59135872010-11-02 12:39:01 -07009486 Object* symbol;
9487 { MaybeObject* maybe_symbol = key->AsObject();
9488 if (!maybe_symbol->ToObject(&symbol)) return maybe_symbol;
9489 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009490
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
9505Object* CompilationCacheTable::Lookup(String* src) {
9506 StringKey key(src);
9507 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +01009508 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009509 return get(EntryToIndex(entry) + 1);
9510}
9511
9512
Steve Block1e0659c2011-05-24 12:43:12 +01009513Object* CompilationCacheTable::LookupEval(String* src,
9514 Context* context,
9515 StrictModeFlag strict_mode) {
9516 StringSharedKey key(src, context->closure()->shared(), strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00009517 int entry = FindEntry(&key);
Steve Block44f0eee2011-05-26 01:26:41 +01009518 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009519 return get(EntryToIndex(entry) + 1);
9520}
9521
9522
9523Object* CompilationCacheTable::LookupRegExp(String* src,
9524 JSRegExp::Flags flags) {
9525 RegExpKey key(src, flags);
9526 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +01009527 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009528 return get(EntryToIndex(entry) + 1);
9529}
9530
9531
John Reck59135872010-11-02 12:39:01 -07009532MaybeObject* CompilationCacheTable::Put(String* src, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009533 StringKey key(src);
John Reck59135872010-11-02 12:39:01 -07009534 Object* obj;
9535 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
9536 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9537 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009538
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 Reck59135872010-11-02 12:39:01 -07009549MaybeObject* CompilationCacheTable::PutEval(String* src,
9550 Context* context,
Steve Block1e0659c2011-05-24 12:43:12 +01009551 SharedFunctionInfo* value) {
9552 StringSharedKey key(src,
9553 context->closure()->shared(),
9554 value->strict_mode() ? kStrictMode : kNonStrictMode);
John Reck59135872010-11-02 12:39:01 -07009555 Object* obj;
9556 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
9557 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9558 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009559
9560 CompilationCacheTable* cache =
9561 reinterpret_cast<CompilationCacheTable*>(obj);
9562 int entry = cache->FindInsertionEntry(key.Hash());
9563
John Reck59135872010-11-02 12:39:01 -07009564 Object* k;
9565 { MaybeObject* maybe_k = key.AsObject();
9566 if (!maybe_k->ToObject(&k)) return maybe_k;
9567 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009568
9569 cache->set(EntryToIndex(entry), k);
9570 cache->set(EntryToIndex(entry) + 1, value);
9571 cache->ElementAdded();
9572 return cache;
9573}
9574
9575
John Reck59135872010-11-02 12:39:01 -07009576MaybeObject* CompilationCacheTable::PutRegExp(String* src,
9577 JSRegExp::Flags flags,
9578 FixedArray* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009579 RegExpKey key(src, flags);
John Reck59135872010-11-02 12:39:01 -07009580 Object* obj;
9581 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
9582 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9583 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009584
9585 CompilationCacheTable* cache =
9586 reinterpret_cast<CompilationCacheTable*>(obj);
9587 int entry = cache->FindInsertionEntry(key.Hash());
Steve Block3ce2e202009-11-05 08:53:23 +00009588 // 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 Blocka7e24c12009-10-30 11:49:00 +00009590 cache->set(EntryToIndex(entry), value);
9591 cache->set(EntryToIndex(entry) + 1, value);
9592 cache->ElementAdded();
9593 return cache;
9594}
9595
9596
Ben Murdochb0fe1622011-05-05 13:52:32 +01009597void CompilationCacheTable::Remove(Object* value) {
Steve Block44f0eee2011-05-26 01:26:41 +01009598 Object* null_value = GetHeap()->null_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +01009599 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 Block44f0eee2011-05-26 01:26:41 +01009603 fast_set(this, entry_index, null_value);
9604 fast_set(this, value_index, null_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01009605 ElementRemoved();
9606 }
9607 }
9608 return;
9609}
9610
9611
Steve Blocka7e24c12009-10-30 11:49:00 +00009612// SymbolsKey used for HashTable where key is array of symbols.
9613class 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
9646Object* MapCache::Lookup(FixedArray* array) {
9647 SymbolsKey key(array);
9648 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +01009649 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009650 return get(EntryToIndex(entry) + 1);
9651}
9652
9653
John Reck59135872010-11-02 12:39:01 -07009654MaybeObject* MapCache::Put(FixedArray* array, Map* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009655 SymbolsKey key(array);
John Reck59135872010-11-02 12:39:01 -07009656 Object* obj;
9657 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
9658 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9659 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009660
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
9670template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009671MaybeObject* 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 Blocka7e24c12009-10-30 11:49:00 +00009676 }
John Reck59135872010-11-02 12:39:01 -07009677 // Initialize the next enumeration index.
9678 Dictionary<Shape, Key>::cast(obj)->
9679 SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +00009680 return obj;
9681}
9682
9683
9684template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009685MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() {
Steve Block44f0eee2011-05-26 01:26:41 +01009686 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00009687 int length = HashTable<Shape, Key>::NumberOfElements();
9688
9689 // Allocate and initialize iteration order array.
John Reck59135872010-11-02 12:39:01 -07009690 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01009691 { MaybeObject* maybe_obj = heap->AllocateFixedArray(length);
John Reck59135872010-11-02 12:39:01 -07009692 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9693 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009694 FixedArray* iteration_order = FixedArray::cast(obj);
9695 for (int i = 0; i < length; i++) {
Leon Clarke4515c472010-02-03 11:58:03 +00009696 iteration_order->set(i, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00009697 }
9698
9699 // Allocate array with enumeration order.
Steve Block44f0eee2011-05-26 01:26:41 +01009700 { MaybeObject* maybe_obj = heap->AllocateFixedArray(length);
John Reck59135872010-11-02 12:39:01 -07009701 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9702 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009703 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 Clarke4515c472010-02-03 11:58:03 +00009710 enumeration_order->set(pos++, Smi::FromInt(DetailsAt(i).index()));
Steve Blocka7e24c12009-10-30 11:49:00 +00009711 }
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 Clarke4515c472010-02-03 11:58:03 +00009721 enumeration_order->set(index, Smi::FromInt(enum_index));
Steve Blocka7e24c12009-10-30 11:49:00 +00009722 }
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
9742template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009743MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009744 // 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 Reck59135872010-11-02 12:39:01 -07009748 Object* result;
9749 { MaybeObject* maybe_result = GenerateNewEnumerationIndices();
9750 if (!maybe_result->ToObject(&result)) return maybe_result;
9751 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009752 }
9753 return HashTable<Shape, Key>::EnsureCapacity(n, key);
9754}
9755
9756
9757void 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 Block44f0eee2011-05-26 01:26:41 +01009761 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00009762 int removed_entries = 0;
Steve Block44f0eee2011-05-26 01:26:41 +01009763 Object* sentinel = heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009764 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 Murdoch8b112d22011-06-08 16:22:53 +01009770 SetEntry(i, sentinel, sentinel);
Steve Blocka7e24c12009-10-30 11:49:00 +00009771 removed_entries++;
9772 }
9773 }
9774 }
9775
9776 // Update the number of elements.
Leon Clarkee46be812010-01-19 14:06:41 +00009777 ElementsRemoved(removed_entries);
Steve Blocka7e24c12009-10-30 11:49:00 +00009778}
9779
9780
9781template<typename Shape, typename Key>
9782Object* Dictionary<Shape, Key>::DeleteProperty(int entry,
9783 JSObject::DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01009784 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00009785 PropertyDetails details = DetailsAt(entry);
9786 // Ignore attributes if forcing a deletion.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009787 if (details.IsDontDelete() && mode != JSObject::FORCE_DELETION) {
Steve Block44f0eee2011-05-26 01:26:41 +01009788 return heap->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009789 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01009790 SetEntry(entry, heap->null_value(), heap->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00009791 HashTable<Shape, Key>::ElementRemoved();
Steve Block44f0eee2011-05-26 01:26:41 +01009792 return heap->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009793}
9794
9795
9796template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009797MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01009798 int entry = this->FindEntry(key);
Steve Blocka7e24c12009-10-30 11:49:00 +00009799
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 Reck59135872010-11-02 12:39:01 -07009807 Object* obj;
9808 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
9809 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9810 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009811
John Reck59135872010-11-02 12:39:01 -07009812 Object* k;
9813 { MaybeObject* maybe_k = Shape::AsObject(key);
9814 if (!maybe_k->ToObject(&k)) return maybe_k;
9815 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009816 PropertyDetails details = PropertyDetails(NONE, NORMAL);
9817 return Dictionary<Shape, Key>::cast(obj)->
9818 AddEntry(key, value, details, Shape::Hash(key));
9819}
9820
9821
9822template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009823MaybeObject* Dictionary<Shape, Key>::Add(Key key,
9824 Object* value,
9825 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009826 // Valdate key is absent.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01009827 SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound));
Steve Blocka7e24c12009-10-30 11:49:00 +00009828 // Check whether the dictionary should be extended.
John Reck59135872010-11-02 12:39:01 -07009829 Object* obj;
9830 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
9831 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9832 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009833 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.
9839template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -07009840MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key,
9841 Object* value,
9842 PropertyDetails details,
9843 uint32_t hash) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009844 // Compute the key object.
John Reck59135872010-11-02 12:39:01 -07009845 Object* k;
9846 { MaybeObject* maybe_k = Shape::AsObject(key);
9847 if (!maybe_k->ToObject(&k)) return maybe_k;
9848 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009849
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
9867void 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 Clarke4515c472010-02-03 11:58:03 +00009881 Smi::FromInt(key << kRequiresSlowElementsTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00009882 }
9883}
9884
9885
John Reck59135872010-11-02 12:39:01 -07009886MaybeObject* NumberDictionary::AddNumberEntry(uint32_t key,
9887 Object* value,
9888 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009889 UpdateMaxNumberKey(key);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01009890 SLOW_ASSERT(this->FindEntry(key) == kNotFound);
Steve Blocka7e24c12009-10-30 11:49:00 +00009891 return Add(key, value, details);
9892}
9893
9894
John Reck59135872010-11-02 12:39:01 -07009895MaybeObject* NumberDictionary::AtNumberPut(uint32_t key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009896 UpdateMaxNumberKey(key);
9897 return AtPut(key, value);
9898}
9899
9900
John Reck59135872010-11-02 12:39:01 -07009901MaybeObject* NumberDictionary::Set(uint32_t key,
9902 Object* value,
9903 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009904 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 Reck59135872010-11-02 12:39:01 -07009910 MaybeObject* maybe_object_key = NumberDictionaryShape::AsObject(key);
9911 Object* object_key;
9912 if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key;
Ben Murdochf87a2032010-10-22 12:50:53 +01009913 SetEntry(entry, object_key, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +00009914 return this;
9915}
9916
9917
9918
9919template<typename Shape, typename Key>
9920int 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
9937template<typename Shape, typename Key>
9938int Dictionary<Shape, Key>::NumberOfEnumElements() {
9939 return NumberOfElementsFilterAttributes(
9940 static_cast<PropertyAttributes>(DONT_ENUM));
9941}
9942
9943
9944template<typename Shape, typename Key>
9945void 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
9964void 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 Clarke4515c472010-02-03 11:58:03 +00009975 sort_array->set(index, Smi::FromInt(details.index()));
Steve Blocka7e24c12009-10-30 11:49:00 +00009976 index++;
9977 }
9978 }
9979 storage->SortPairs(sort_array, sort_array->length());
9980 ASSERT(storage->length() >= index);
9981}
9982
9983
9984template<typename Shape, typename Key>
9985void 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).
10003template<typename Shape, typename Key>
10004Object* 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 Murdoch8b112d22011-06-08 16:22:53 +010010016 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +010010017 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010018}
10019
10020
John Reck59135872010-11-02 12:39:01 -070010021MaybeObject* StringDictionary::TransformPropertiesToFastFor(
Steve Blocka7e24c12009-10-30 11:49:00 +000010022 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 Reck59135872010-11-02 12:39:01 -070010033 Object* result;
10034 { MaybeObject* maybe_result = GenerateNewEnumerationIndices();
10035 if (!maybe_result->ToObject(&result)) return maybe_result;
10036 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010037 }
10038
10039 int instance_descriptor_length = 0;
10040 int number_of_fields = 0;
10041
Ben Murdoch8b112d22011-06-08 16:22:53 +010010042 Heap* heap = GetHeap();
10043
Steve Blocka7e24c12009-10-30 11:49:00 +000010044 // 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 Clarkee46be812010-01-19 14:06:41 +000010053 if (type == NORMAL &&
Steve Block44f0eee2011-05-26 01:26:41 +010010054 (!value->IsJSFunction() || heap->InNewSpace(value))) {
Leon Clarkee46be812010-01-19 14:06:41 +000010055 number_of_fields += 1;
10056 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010057 }
10058 }
10059
10060 // Allocate the instance descriptor.
John Reck59135872010-11-02 12:39:01 -070010061 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 Blocka7e24c12009-10-30 11:49:00 +000010068 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 Murdochf87a2032010-10-22 12:50:53 +010010073 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 Blocka7e24c12009-10-30 11:49:00 +000010078
10079 // Allocate the fixed array for the fields.
John Reck59135872010-11-02 12:39:01 -070010080 Object* fields;
10081 { MaybeObject* maybe_fields =
Steve Block44f0eee2011-05-26 01:26:41 +010010082 heap->AllocateFixedArray(number_of_allocated_fields);
John Reck59135872010-11-02 12:39:01 -070010083 if (!maybe_fields->ToObject(&fields)) return maybe_fields;
10084 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010085
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 Reck59135872010-11-02 12:39:01 -070010094 Object* key;
Steve Block44f0eee2011-05-26 01:26:41 +010010095 { MaybeObject* maybe_key = heap->LookupSymbol(String::cast(k));
John Reck59135872010-11-02 12:39:01 -070010096 if (!maybe_key->ToObject(&key)) return maybe_key;
10097 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010098 PropertyDetails details = DetailsAt(i);
10099 PropertyType type = details.type();
10100
Steve Block44f0eee2011-05-26 01:26:41 +010010101 if (value->IsJSFunction() && !heap->InNewSpace(value)) {
Steve Blocka7e24c12009-10-30 11:49:00 +000010102 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 Reck59135872010-11-02 12:39:01 -070010136 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 Blocka7e24c12009-10-30 11:49:00 +000010140
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.
10159bool 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.
10171Object* 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 Murdoch8b112d22011-06-08 16:22:53 +010010176 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010177 return BreakPointInfo::cast(break_points()->get(index));
10178}
10179
10180
10181// Clear a break point at the specified code position.
10182void 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
10193void 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 Block44f0eee2011-05-26 01:26:41 +010010198 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +000010199 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 Blocka7e24c12009-10-30 11:49:00 +000010220 Handle<FixedArray> new_break_points =
Steve Block44f0eee2011-05-26 01:26:41 +010010221 isolate->factory()->NewFixedArray(
10222 old_break_points->length() +
10223 Debug::kEstimatedNofBreakPointsInFunction);
Kristian Monsen0d5e1162010-09-30 15:31:59 +010010224
10225 debug_info->set_break_points(*new_break_points);
Steve Blocka7e24c12009-10-30 11:49:00 +000010226 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 Block44f0eee2011-05-26 01:26:41 +010010234 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
10235 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +000010236 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 Block44f0eee2011-05-26 01:26:41 +010010240 new_break_point_info->set_break_point_objects(
10241 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000010242 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.
10248Object* DebugInfo::GetBreakPointObjects(int code_position) {
10249 Object* break_point_info = GetBreakPointInfo(code_position);
10250 if (break_point_info->IsUndefined()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010010251 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010252 }
10253 return BreakPointInfo::cast(break_point_info)->break_point_objects();
10254}
10255
10256
10257// Get the total number of break points.
10258int 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
10272Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info,
10273 Handle<Object> break_point_object) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010010274 Heap* heap = debug_info->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +010010275 if (debug_info->break_points()->IsUndefined()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010276 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 Block44f0eee2011-05-26 01:26:41 +010010287 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010288}
10289
10290
10291// Find the index of the break point info object for the specified code
10292// position.
10293int 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.
10309void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
10310 Handle<Object> break_point_object) {
Steve Block44f0eee2011-05-26 01:26:41 +010010311 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +000010312 // 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 Block44f0eee2011-05-26 01:26:41 +010010317 break_point_info->set_break_point_objects(
10318 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000010319 }
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 Block44f0eee2011-05-26 01:26:41 +010010328 isolate->factory()->NewFixedArray(old_array->length() - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000010329 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.
10344void 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 Block44f0eee2011-05-26 01:26:41 +010010355 Handle<FixedArray> array = FACTORY->NewFixedArray(2);
Steve Blocka7e24c12009-10-30 11:49:00 +000010356 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 Block44f0eee2011-05-26 01:26:41 +010010366 FACTORY->NewFixedArray(old_array->length() + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000010367 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
10378bool 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.
10399int 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