blob: 208c75b8ac19df3054c0125efbca4fa7e1b1c2c0 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 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"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010036#include "date.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000037#include "elements.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038#include "execution.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010039#include "full-codegen.h"
40#include "hydrogen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "objects-inl.h"
Iain Merrick75681382010-08-19 15:07:18 +010042#include "objects-visiting.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010043#include "objects-visiting-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044#include "macro-assembler.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045#include "mark-compact.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010046#include "safepoint-table.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000047#include "string-stream.h"
Steve Blockd0582a62009-12-15 09:54:21 +000048#include "utils.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010049#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000050
51#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +010052#include "disasm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000053#include "disassembler.h"
54#endif
55
Steve Blocka7e24c12009-10-30 11:49:00 +000056namespace v8 {
57namespace internal {
58
Ben Murdoch3ef787d2012-04-12 10:51:47 +010059void PrintElementsKind(FILE* out, ElementsKind kind) {
60 ElementsAccessor* accessor = ElementsAccessor::ForKind(kind);
61 PrintF(out, "%s", accessor->name());
62}
63
Steve Blocka7e24c12009-10-30 11:49:00 +000064
John Reck59135872010-11-02 12:39:01 -070065MUST_USE_RESULT static MaybeObject* CreateJSValue(JSFunction* constructor,
66 Object* value) {
67 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +010068 { MaybeObject* maybe_result =
69 constructor->GetHeap()->AllocateJSObject(constructor);
John Reck59135872010-11-02 12:39:01 -070070 if (!maybe_result->ToObject(&result)) return maybe_result;
71 }
Steve Blocka7e24c12009-10-30 11:49:00 +000072 JSValue::cast(result)->set_value(value);
73 return result;
74}
75
76
John Reck59135872010-11-02 12:39:01 -070077MaybeObject* Object::ToObject(Context* global_context) {
Steve Blocka7e24c12009-10-30 11:49:00 +000078 if (IsNumber()) {
79 return CreateJSValue(global_context->number_function(), this);
80 } else if (IsBoolean()) {
81 return CreateJSValue(global_context->boolean_function(), this);
82 } else if (IsString()) {
83 return CreateJSValue(global_context->string_function(), this);
84 }
85 ASSERT(IsJSObject());
86 return this;
87}
88
89
John Reck59135872010-11-02 12:39:01 -070090MaybeObject* Object::ToObject() {
Ben Murdoch589d6972011-11-30 16:04:58 +000091 if (IsJSReceiver()) {
Steve Blocka7e24c12009-10-30 11:49:00 +000092 return this;
93 } else if (IsNumber()) {
Steve Block44f0eee2011-05-26 01:26:41 +010094 Isolate* isolate = Isolate::Current();
95 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +000096 return CreateJSValue(global_context->number_function(), this);
97 } else if (IsBoolean()) {
Steve Block44f0eee2011-05-26 01:26:41 +010098 Isolate* isolate = HeapObject::cast(this)->GetIsolate();
99 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 return CreateJSValue(global_context->boolean_function(), this);
101 } else if (IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100102 Isolate* isolate = HeapObject::cast(this)->GetIsolate();
103 Context* global_context = isolate->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000104 return CreateJSValue(global_context->string_function(), this);
105 }
106
107 // Throw a type error.
108 return Failure::InternalError();
109}
110
111
112Object* Object::ToBoolean() {
Steve Block44f0eee2011-05-26 01:26:41 +0100113 if (IsTrue()) return this;
114 if (IsFalse()) return this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 if (IsSmi()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 return Isolate::Current()->heap()->ToBoolean(Smi::cast(this)->value() != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100118 HeapObject* heap_object = HeapObject::cast(this);
119 if (heap_object->IsUndefined() || heap_object->IsNull()) {
120 return heap_object->GetHeap()->false_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100121 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 // Undetectable object is false
Ben Murdoch8b112d22011-06-08 16:22:53 +0100123 if (heap_object->IsUndetectableObject()) {
124 return heap_object->GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100126 if (heap_object->IsString()) {
127 return heap_object->GetHeap()->ToBoolean(
Steve Block44f0eee2011-05-26 01:26:41 +0100128 String::cast(this)->length() != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100130 if (heap_object->IsHeapNumber()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 return HeapNumber::cast(this)->HeapNumberToBoolean();
132 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100133 return heap_object->GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000134}
135
136
137void Object::Lookup(String* name, LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 Object* holder = NULL;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100139 if (IsJSReceiver()) {
140 holder = this;
Ben Murdoch85b71792012-04-11 18:30:58 +0100141 } else {
Ben Murdoch85b71792012-04-11 18:30:58 +0100142 Context* global_context = Isolate::Current()->context()->global_context();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143 if (IsNumber()) {
Ben Murdoch85b71792012-04-11 18:30:58 +0100144 holder = global_context->number_function()->instance_prototype();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100145 } else if (IsString()) {
146 holder = global_context->string_function()->instance_prototype();
147 } else if (IsBoolean()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100148 holder = global_context->boolean_function()->instance_prototype();
149 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000150 }
151 ASSERT(holder != NULL); // Cannot handle null or undefined.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100152 JSReceiver::cast(holder)->Lookup(name, result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000153}
154
155
John Reck59135872010-11-02 12:39:01 -0700156MaybeObject* Object::GetPropertyWithReceiver(Object* receiver,
157 String* name,
158 PropertyAttributes* attributes) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100159 LookupResult result(name->GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 Lookup(name, &result);
John Reck59135872010-11-02 12:39:01 -0700161 MaybeObject* value = GetProperty(receiver, &result, name, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 ASSERT(*attributes <= ABSENT);
163 return value;
164}
165
166
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100167MaybeObject* JSObject::GetPropertyWithCallback(Object* receiver,
168 Object* structure,
169 String* name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100170 Isolate* isolate = name->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 // To accommodate both the old and the new api we switch on the
Ben Murdoch257744e2011-11-30 15:57:28 +0000172 // data structure used to store the callbacks. Eventually foreign
Steve Blocka7e24c12009-10-30 11:49:00 +0000173 // callbacks should be phased out.
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 if (structure->IsForeign()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000175 AccessorDescriptor* callback =
Ben Murdoch257744e2011-11-30 15:57:28 +0000176 reinterpret_cast<AccessorDescriptor*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100177 Foreign::cast(structure)->foreign_address());
John Reck59135872010-11-02 12:39:01 -0700178 MaybeObject* value = (callback->getter)(receiver, callback->data);
Steve Block44f0eee2011-05-26 01:26:41 +0100179 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 return value;
181 }
182
183 // api style callbacks.
184 if (structure->IsAccessorInfo()) {
185 AccessorInfo* data = AccessorInfo::cast(structure);
186 Object* fun_obj = data->getter();
187 v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 JSObject* self = JSObject::cast(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 Handle<String> key(name);
Steve Block44f0eee2011-05-26 01:26:41 +0100191 LOG(isolate, ApiNamedPropertyAccess("load", self, name));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100192 CustomArguments args(isolate, data->data(), self, this);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 v8::AccessorInfo info(args.end());
194 v8::Handle<v8::Value> result;
195 {
196 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100197 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 result = call_fun(v8::Utils::ToLocal(key), info);
199 }
Steve Block44f0eee2011-05-26 01:26:41 +0100200 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
201 if (result.IsEmpty()) {
202 return isolate->heap()->undefined_value();
203 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 return *v8::Utils::OpenHandle(*result);
205 }
206
207 // __defineGetter__ callback
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208 if (structure->IsAccessorPair()) {
209 Object* getter = AccessorPair::cast(structure)->getter();
210 if (getter->IsSpecFunction()) {
211 // TODO(rossberg): nicer would be to cast to some JSCallable here...
212 return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter));
Steve Blocka7e24c12009-10-30 11:49:00 +0000213 }
214 // Getter is not a function.
Steve Block44f0eee2011-05-26 01:26:41 +0100215 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000216 }
217
218 UNREACHABLE();
Leon Clarkef7060e22010-06-03 12:02:55 +0100219 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000220}
221
222
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100223MaybeObject* JSProxy::GetPropertyWithHandler(Object* receiver_raw,
224 String* name_raw) {
225 Isolate* isolate = GetIsolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000226 HandleScope scope(isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +0000227 Handle<Object> receiver(receiver_raw);
228 Handle<Object> name(name_raw);
Ben Murdoch257744e2011-11-30 15:57:28 +0000229
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100230 Handle<Object> args[] = { receiver, name };
231 Handle<Object> result = CallTrap(
232 "get", isolate->derived_get_trap(), ARRAY_SIZE(args), args);
Ben Murdoch589d6972011-11-30 16:04:58 +0000233 if (isolate->has_pending_exception()) return Failure::Exception();
Ben Murdoch257744e2011-11-30 15:57:28 +0000234
235 return *result;
236}
237
238
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239Handle<Object> Object::GetElement(Handle<Object> object, uint32_t index) {
240 Isolate* isolate = object->IsHeapObject()
241 ? Handle<HeapObject>::cast(object)->GetIsolate()
242 : Isolate::Current();
243 CALL_HEAP_FUNCTION(isolate, object->GetElement(index), Object);
244}
245
246
247MaybeObject* JSProxy::GetElementWithHandler(Object* receiver,
248 uint32_t index) {
249 String* name;
250 MaybeObject* maybe = GetHeap()->Uint32ToString(index);
251 if (!maybe->To<String>(&name)) return maybe;
252 return GetPropertyWithHandler(receiver, name);
253}
254
255
256MaybeObject* JSProxy::SetElementWithHandler(uint32_t index,
257 Object* value,
258 StrictModeFlag strict_mode) {
259 String* name;
260 MaybeObject* maybe = GetHeap()->Uint32ToString(index);
261 if (!maybe->To<String>(&name)) return maybe;
262 return SetPropertyWithHandler(name, value, NONE, strict_mode);
263}
264
265
266bool JSProxy::HasElementWithHandler(uint32_t index) {
267 String* name;
268 MaybeObject* maybe = GetHeap()->Uint32ToString(index);
269 if (!maybe->To<String>(&name)) return maybe;
270 return HasPropertyWithHandler(name);
271}
272
273
John Reck59135872010-11-02 12:39:01 -0700274MaybeObject* Object::GetPropertyWithDefinedGetter(Object* receiver,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100275 JSReceiver* getter) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 HandleScope scope;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100277 Handle<JSReceiver> fun(getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 Handle<Object> self(receiver);
279#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100280 Debug* debug = fun->GetHeap()->isolate()->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 // Handle stepping into a getter if step into is active.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100282 // TODO(rossberg): should this apply to getters that are function proxies?
283 if (debug->StepInActive() && fun->IsJSFunction()) {
284 debug->HandleStepIn(
285 Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 }
287#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100288
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 bool has_pending_exception;
290 Handle<Object> result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100291 Execution::Call(fun, self, 0, NULL, &has_pending_exception, true);
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 // Check for pending exception and return the result.
293 if (has_pending_exception) return Failure::Exception();
294 return *result;
295}
296
297
298// Only deal with CALLBACKS and INTERCEPTOR
John Reck59135872010-11-02 12:39:01 -0700299MaybeObject* JSObject::GetPropertyWithFailedAccessCheck(
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 Object* receiver,
301 LookupResult* result,
302 String* name,
303 PropertyAttributes* attributes) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000304 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 switch (result->type()) {
306 case CALLBACKS: {
307 // Only allow API accessors.
308 Object* obj = result->GetCallbackObject();
309 if (obj->IsAccessorInfo()) {
310 AccessorInfo* info = AccessorInfo::cast(obj);
311 if (info->all_can_read()) {
312 *attributes = result->GetAttributes();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100313 return result->holder()->GetPropertyWithCallback(
314 receiver, result->GetCallbackObject(), name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 }
316 }
317 break;
318 }
319 case NORMAL:
320 case FIELD:
321 case CONSTANT_FUNCTION: {
322 // Search ALL_CAN_READ accessors in prototype chain.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100323 LookupResult r(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 result->holder()->LookupRealNamedPropertyInPrototypes(name, &r);
Andrei Popescu402d9372010-02-26 13:31:12 +0000325 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000326 return GetPropertyWithFailedAccessCheck(receiver,
327 &r,
328 name,
329 attributes);
330 }
331 break;
332 }
333 case INTERCEPTOR: {
334 // If the object has an interceptor, try real named properties.
335 // No access check in GetPropertyAttributeWithInterceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100336 LookupResult r(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 result->holder()->LookupRealNamedProperty(name, &r);
Andrei Popescu402d9372010-02-26 13:31:12 +0000338 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 return GetPropertyWithFailedAccessCheck(receiver,
340 &r,
341 name,
342 attributes);
343 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 break;
345 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000346 default:
347 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 }
349 }
350
351 // No accessible property found.
352 *attributes = ABSENT;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100353 Heap* heap = name->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100354 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_GET);
355 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000356}
357
358
359PropertyAttributes JSObject::GetPropertyAttributeWithFailedAccessCheck(
360 Object* receiver,
361 LookupResult* result,
362 String* name,
363 bool continue_search) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000364 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 switch (result->type()) {
366 case CALLBACKS: {
367 // Only allow API accessors.
368 Object* obj = result->GetCallbackObject();
369 if (obj->IsAccessorInfo()) {
370 AccessorInfo* info = AccessorInfo::cast(obj);
371 if (info->all_can_read()) {
372 return result->GetAttributes();
373 }
374 }
375 break;
376 }
377
378 case NORMAL:
379 case FIELD:
380 case CONSTANT_FUNCTION: {
381 if (!continue_search) break;
382 // Search ALL_CAN_READ accessors in prototype chain.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100383 LookupResult r(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 result->holder()->LookupRealNamedPropertyInPrototypes(name, &r);
Andrei Popescu402d9372010-02-26 13:31:12 +0000385 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 return GetPropertyAttributeWithFailedAccessCheck(receiver,
387 &r,
388 name,
389 continue_search);
390 }
391 break;
392 }
393
394 case INTERCEPTOR: {
395 // If the object has an interceptor, try real named properties.
396 // No access check in GetPropertyAttributeWithInterceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100397 LookupResult r(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000398 if (continue_search) {
399 result->holder()->LookupRealNamedProperty(name, &r);
400 } else {
401 result->holder()->LocalLookupRealNamedProperty(name, &r);
402 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000403 if (r.IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 return GetPropertyAttributeWithFailedAccessCheck(receiver,
405 &r,
406 name,
407 continue_search);
408 }
409 break;
410 }
411
Andrei Popescu402d9372010-02-26 13:31:12 +0000412 default:
413 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000414 }
415 }
416
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100417 GetIsolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000418 return ABSENT;
419}
420
421
Steve Blocka7e24c12009-10-30 11:49:00 +0000422Object* JSObject::GetNormalizedProperty(LookupResult* result) {
423 ASSERT(!HasFastProperties());
424 Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry());
425 if (IsGlobalObject()) {
426 value = JSGlobalPropertyCell::cast(value)->value();
427 }
428 ASSERT(!value->IsJSGlobalPropertyCell());
429 return value;
430}
431
432
433Object* JSObject::SetNormalizedProperty(LookupResult* result, Object* value) {
434 ASSERT(!HasFastProperties());
435 if (IsGlobalObject()) {
436 JSGlobalPropertyCell* cell =
437 JSGlobalPropertyCell::cast(
438 property_dictionary()->ValueAt(result->GetDictionaryEntry()));
439 cell->set_value(value);
440 } else {
441 property_dictionary()->ValueAtPut(result->GetDictionaryEntry(), value);
442 }
443 return value;
444}
445
446
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100447Handle<Object> JSObject::SetNormalizedProperty(Handle<JSObject> object,
448 Handle<String> key,
449 Handle<Object> value,
450 PropertyDetails details) {
451 CALL_HEAP_FUNCTION(object->GetIsolate(),
452 object->SetNormalizedProperty(*key, *value, details),
453 Object);
454}
455
456
John Reck59135872010-11-02 12:39:01 -0700457MaybeObject* JSObject::SetNormalizedProperty(String* name,
458 Object* value,
459 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 ASSERT(!HasFastProperties());
461 int entry = property_dictionary()->FindEntry(name);
462 if (entry == StringDictionary::kNotFound) {
463 Object* store_value = value;
464 if (IsGlobalObject()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100465 Heap* heap = name->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100466 MaybeObject* maybe_store_value =
467 heap->AllocateJSGlobalPropertyCell(value);
468 if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000469 }
John Reck59135872010-11-02 12:39:01 -0700470 Object* dict;
471 { MaybeObject* maybe_dict =
472 property_dictionary()->Add(name, store_value, details);
473 if (!maybe_dict->ToObject(&dict)) return maybe_dict;
474 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 set_properties(StringDictionary::cast(dict));
476 return value;
477 }
478 // Preserve enumeration index.
479 details = PropertyDetails(details.attributes(),
480 details.type(),
481 property_dictionary()->DetailsAt(entry).index());
482 if (IsGlobalObject()) {
483 JSGlobalPropertyCell* cell =
484 JSGlobalPropertyCell::cast(property_dictionary()->ValueAt(entry));
485 cell->set_value(value);
486 // Please note we have to update the property details.
487 property_dictionary()->DetailsAtPut(entry, details);
488 } else {
489 property_dictionary()->SetEntry(entry, name, value, details);
490 }
491 return value;
492}
493
494
John Reck59135872010-11-02 12:39:01 -0700495MaybeObject* JSObject::DeleteNormalizedProperty(String* name, DeleteMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000496 ASSERT(!HasFastProperties());
497 StringDictionary* dictionary = property_dictionary();
498 int entry = dictionary->FindEntry(name);
499 if (entry != StringDictionary::kNotFound) {
500 // If we have a global object set the cell to the hole.
501 if (IsGlobalObject()) {
502 PropertyDetails details = dictionary->DetailsAt(entry);
503 if (details.IsDontDelete()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100504 if (mode != FORCE_DELETION) return GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000505 // When forced to delete global properties, we have to make a
506 // map change to invalidate any ICs that think they can load
507 // from the DontDelete cell without checking if it contains
508 // the hole value.
John Reck59135872010-11-02 12:39:01 -0700509 Object* new_map;
510 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
511 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
512 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 set_map(Map::cast(new_map));
514 }
515 JSGlobalPropertyCell* cell =
516 JSGlobalPropertyCell::cast(dictionary->ValueAt(entry));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100517 cell->set_value(cell->GetHeap()->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 dictionary->DetailsAtPut(entry, details.AsDeleted());
519 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000520 Object* deleted = dictionary->DeleteProperty(entry, mode);
521 if (deleted == GetHeap()->true_value()) {
522 FixedArray* new_properties = NULL;
523 MaybeObject* maybe_properties = dictionary->Shrink(name);
524 if (!maybe_properties->To(&new_properties)) {
525 return maybe_properties;
526 }
527 set_properties(new_properties);
528 }
529 return deleted;
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 }
531 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100532 return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000533}
534
535
536bool JSObject::IsDirty() {
537 Object* cons_obj = map()->constructor();
538 if (!cons_obj->IsJSFunction())
539 return true;
540 JSFunction* fun = JSFunction::cast(cons_obj);
Steve Block6ded16b2010-05-10 14:33:55 +0100541 if (!fun->shared()->IsApiFunction())
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 return true;
543 // If the object is fully fast case and has the same map it was
544 // created with then no changes can have been made to it.
545 return map() != fun->initial_map()
546 || !HasFastElements()
547 || !HasFastProperties();
548}
549
550
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100551Handle<Object> Object::GetProperty(Handle<Object> object,
552 Handle<Object> receiver,
553 LookupResult* result,
554 Handle<String> key,
555 PropertyAttributes* attributes) {
556 Isolate* isolate = object->IsHeapObject()
557 ? Handle<HeapObject>::cast(object)->GetIsolate()
558 : Isolate::Current();
559 CALL_HEAP_FUNCTION(
560 isolate,
561 object->GetProperty(*receiver, result, *key, attributes),
562 Object);
563}
564
565
John Reck59135872010-11-02 12:39:01 -0700566MaybeObject* Object::GetProperty(Object* receiver,
567 LookupResult* result,
568 String* name,
569 PropertyAttributes* attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 // Make sure that the top context does not change when doing
571 // callbacks or interceptor calls.
572 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +0100573 Heap* heap = name->GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
575 // Traverse the prototype chain from the current object (this) to
Ben Murdoch257744e2011-11-30 15:57:28 +0000576 // the holder and check for access rights. This avoids traversing the
Steve Blocka7e24c12009-10-30 11:49:00 +0000577 // objects more than once in case of interceptors, because the
578 // holder will always be the interceptor holder and the search may
579 // only continue with a current object just after the interceptor
580 // holder in the prototype chain.
Ben Murdoch257744e2011-11-30 15:57:28 +0000581 // Proxy handlers do not use the proxy's prototype, so we can skip this.
582 if (!result->IsHandler()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100583 Object* last = result->IsProperty()
584 ? result->holder()
585 : Object::cast(heap->null_value());
Ben Murdoch257744e2011-11-30 15:57:28 +0000586 ASSERT(this != this->GetPrototype());
587 for (Object* current = this; true; current = current->GetPrototype()) {
588 if (current->IsAccessCheckNeeded()) {
589 // Check if we're allowed to read from the current object. Note
590 // that even though we may not actually end up loading the named
591 // property from the current object, we still check that we have
592 // access to it.
593 JSObject* checked = JSObject::cast(current);
594 if (!heap->isolate()->MayNamedAccess(checked, name, v8::ACCESS_GET)) {
595 return checked->GetPropertyWithFailedAccessCheck(receiver,
596 result,
597 name,
598 attributes);
599 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000600 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000601 // Stop traversing the chain once we reach the last object in the
602 // chain; either the holder of the result or null in case of an
603 // absent property.
604 if (current == last) break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 }
607
608 if (!result->IsProperty()) {
609 *attributes = ABSENT;
Steve Block44f0eee2011-05-26 01:26:41 +0100610 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 }
612 *attributes = result->GetAttributes();
Steve Blocka7e24c12009-10-30 11:49:00 +0000613 Object* value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 switch (result->type()) {
615 case NORMAL:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100616 value = result->holder()->GetNormalizedProperty(result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 ASSERT(!value->IsTheHole() || result->IsReadOnly());
Steve Block44f0eee2011-05-26 01:26:41 +0100618 return value->IsTheHole() ? heap->undefined_value() : value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000619 case FIELD:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100620 value = result->holder()->FastPropertyAt(result->GetFieldIndex());
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 ASSERT(!value->IsTheHole() || result->IsReadOnly());
Steve Block44f0eee2011-05-26 01:26:41 +0100622 return value->IsTheHole() ? heap->undefined_value() : value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000623 case CONSTANT_FUNCTION:
624 return result->GetConstantFunction();
625 case CALLBACKS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100626 return result->holder()->GetPropertyWithCallback(
627 receiver, result->GetCallbackObject(), name);
628 case HANDLER:
629 return result->proxy()->GetPropertyWithHandler(receiver, name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 case INTERCEPTOR: {
631 JSObject* recvr = JSObject::cast(receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100632 return result->holder()->GetPropertyWithInterceptor(
633 recvr, name, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000635 case MAP_TRANSITION:
Ben Murdoch589d6972011-11-30 16:04:58 +0000636 case ELEMENTS_TRANSITION:
Ben Murdoch257744e2011-11-30 15:57:28 +0000637 case CONSTANT_TRANSITION:
638 case NULL_DESCRIPTOR:
639 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000640 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000641 UNREACHABLE();
642 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000643}
644
645
John Reck59135872010-11-02 12:39:01 -0700646MaybeObject* Object::GetElementWithReceiver(Object* receiver, uint32_t index) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000647 Heap* heap = IsSmi()
648 ? Isolate::Current()->heap()
649 : HeapObject::cast(this)->GetHeap();
650 Object* holder = this;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100651
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000652 // Iterate up the prototype chain until an element is found or the null
653 // prototype is encountered.
654 for (holder = this;
655 holder != heap->null_value();
656 holder = holder->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100657 if (!holder->IsJSObject()) {
658 Isolate* isolate = heap->isolate();
659 Context* global_context = isolate->context()->global_context();
660 if (holder->IsNumber()) {
661 holder = global_context->number_function()->instance_prototype();
662 } else if (holder->IsString()) {
663 holder = global_context->string_function()->instance_prototype();
664 } else if (holder->IsBoolean()) {
665 holder = global_context->boolean_function()->instance_prototype();
666 } else if (holder->IsJSProxy()) {
667 return JSProxy::cast(holder)->GetElementWithHandler(receiver, index);
668 } else {
669 // Undefined and null have no indexed properties.
670 ASSERT(holder->IsUndefined() || holder->IsNull());
671 return heap->undefined_value();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000672 }
673 }
674
675 // Inline the case for JSObjects. Doing so significantly improves the
676 // performance of fetching elements where checking the prototype chain is
677 // necessary.
678 JSObject* js_object = JSObject::cast(holder);
679
680 // Check access rights if needed.
681 if (js_object->IsAccessCheckNeeded()) {
682 Isolate* isolate = heap->isolate();
683 if (!isolate->MayIndexedAccess(js_object, index, v8::ACCESS_GET)) {
684 isolate->ReportFailedAccessCheck(js_object, v8::ACCESS_GET);
685 return heap->undefined_value();
686 }
687 }
688
689 if (js_object->HasIndexedInterceptor()) {
690 return js_object->GetElementWithInterceptor(receiver, index);
691 }
692
693 if (js_object->elements() != heap->empty_fixed_array()) {
694 MaybeObject* result = js_object->GetElementsAccessor()->Get(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100695 receiver, js_object, index);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000696 if (result != heap->the_hole_value()) return result;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100697 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100698 }
699
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000700 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000701}
702
703
704Object* Object::GetPrototype() {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100705 if (IsSmi()) {
706 Heap* heap = Isolate::Current()->heap();
707 Context* context = heap->isolate()->context()->global_context();
708 return context->number_function()->instance_prototype();
709 }
710
711 HeapObject* heap_object = HeapObject::cast(this);
712
Ben Murdoch257744e2011-11-30 15:57:28 +0000713 // The object is either a number, a string, a boolean,
714 // a real JS object, or a Harmony proxy.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000715 if (heap_object->IsJSReceiver()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000716 return heap_object->map()->prototype();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100717 }
718 Heap* heap = heap_object->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +0100719 Context* context = heap->isolate()->context()->global_context();
Steve Blocka7e24c12009-10-30 11:49:00 +0000720
Ben Murdoch8b112d22011-06-08 16:22:53 +0100721 if (heap_object->IsHeapNumber()) {
722 return context->number_function()->instance_prototype();
723 }
724 if (heap_object->IsString()) {
725 return context->string_function()->instance_prototype();
726 }
727 if (heap_object->IsBoolean()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000728 return context->boolean_function()->instance_prototype();
729 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100730 return heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 }
732}
733
734
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100735MaybeObject* Object::GetHash(CreationFlag flag) {
736 // The object is either a number, a string, an odd-ball,
737 // a real JS object, or a Harmony proxy.
738 if (IsNumber()) {
739 uint32_t hash = ComputeLongHash(double_to_uint64(Number()));
740 return Smi::FromInt(hash & Smi::kMaxValue);
741 }
742 if (IsString()) {
743 uint32_t hash = String::cast(this)->Hash();
744 return Smi::FromInt(hash);
745 }
746 if (IsOddball()) {
747 uint32_t hash = Oddball::cast(this)->to_string()->Hash();
748 return Smi::FromInt(hash);
749 }
750 if (IsJSReceiver()) {
751 return JSReceiver::cast(this)->GetIdentityHash(flag);
752 }
753
754 UNREACHABLE();
755 return Smi::FromInt(0);
756}
757
758
759bool Object::SameValue(Object* other) {
760 if (other == this) return true;
761 if (!IsHeapObject() || !other->IsHeapObject()) return false;
762
763 // The object is either a number, a string, an odd-ball,
764 // a real JS object, or a Harmony proxy.
765 if (IsNumber() && other->IsNumber()) {
766 double this_value = Number();
767 double other_value = other->Number();
768 return (this_value == other_value) ||
769 (isnan(this_value) && isnan(other_value));
770 }
771 if (IsString() && other->IsString()) {
772 return String::cast(this)->Equals(String::cast(other));
773 }
774 return false;
775}
776
777
Ben Murdochb0fe1622011-05-05 13:52:32 +0100778void Object::ShortPrint(FILE* out) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000779 HeapStringAllocator allocator;
780 StringStream accumulator(&allocator);
781 ShortPrint(&accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100782 accumulator.OutputToFile(out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000783}
784
785
786void Object::ShortPrint(StringStream* accumulator) {
787 if (IsSmi()) {
788 Smi::cast(this)->SmiPrint(accumulator);
789 } else if (IsFailure()) {
790 Failure::cast(this)->FailurePrint(accumulator);
791 } else {
792 HeapObject::cast(this)->HeapObjectShortPrint(accumulator);
793 }
794}
795
796
Ben Murdochb0fe1622011-05-05 13:52:32 +0100797void Smi::SmiPrint(FILE* out) {
798 PrintF(out, "%d", value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000799}
800
801
802void Smi::SmiPrint(StringStream* accumulator) {
803 accumulator->Add("%d", value());
804}
805
806
807void Failure::FailurePrint(StringStream* accumulator) {
Steve Block3ce2e202009-11-05 08:53:23 +0000808 accumulator->Add("Failure(%p)", reinterpret_cast<void*>(value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000809}
810
811
Ben Murdochb0fe1622011-05-05 13:52:32 +0100812void Failure::FailurePrint(FILE* out) {
813 PrintF(out, "Failure(%p)", reinterpret_cast<void*>(value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000814}
815
816
Steve Blocka7e24c12009-10-30 11:49:00 +0000817// Should a word be prefixed by 'a' or 'an' in order to read naturally in
818// English? Returns false for non-ASCII or words that don't start with
819// a capital letter. The a/an rule follows pronunciation in English.
820// We don't use the BBC's overcorrect "an historic occasion" though if
821// you speak a dialect you may well say "an 'istoric occasion".
822static bool AnWord(String* str) {
823 if (str->length() == 0) return false; // A nothing.
824 int c0 = str->Get(0);
825 int c1 = str->length() > 1 ? str->Get(1) : 0;
826 if (c0 == 'U') {
827 if (c1 > 'Z') {
828 return true; // An Umpire, but a UTF8String, a U.
829 }
830 } else if (c0 == 'A' || c0 == 'E' || c0 == 'I' || c0 == 'O') {
831 return true; // An Ape, an ABCBook.
832 } else if ((c1 == 0 || (c1 >= 'A' && c1 <= 'Z')) &&
833 (c0 == 'F' || c0 == 'H' || c0 == 'M' || c0 == 'N' || c0 == 'R' ||
834 c0 == 'S' || c0 == 'X')) {
835 return true; // An MP3File, an M.
836 }
837 return false;
838}
839
840
John Reck59135872010-11-02 12:39:01 -0700841MaybeObject* String::SlowTryFlatten(PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000842#ifdef DEBUG
843 // Do not attempt to flatten in debug mode when allocation is not
844 // allowed. This is to avoid an assertion failure when allocating.
845 // Flattening strings is the only case where we always allow
846 // allocation because no GC is performed if the allocation fails.
Steve Block44f0eee2011-05-26 01:26:41 +0100847 if (!HEAP->IsAllocationAllowed()) return this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000848#endif
849
Steve Block44f0eee2011-05-26 01:26:41 +0100850 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 switch (StringShape(this).representation_tag()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000852 case kConsStringTag: {
853 ConsString* cs = ConsString::cast(this);
854 if (cs->second()->length() == 0) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100855 return cs->first();
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 }
857 // There's little point in putting the flat string in new space if the
858 // cons string is in old space. It can never get GCed until there is
859 // an old space GC.
Steve Block44f0eee2011-05-26 01:26:41 +0100860 PretenureFlag tenure = heap->InNewSpace(this) ? pretenure : TENURED;
Steve Blocka7e24c12009-10-30 11:49:00 +0000861 int len = length();
862 Object* object;
863 String* result;
864 if (IsAsciiRepresentation()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100865 { MaybeObject* maybe_object = heap->AllocateRawAsciiString(len, tenure);
John Reck59135872010-11-02 12:39:01 -0700866 if (!maybe_object->ToObject(&object)) return maybe_object;
867 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000868 result = String::cast(object);
869 String* first = cs->first();
870 int first_length = first->length();
871 char* dest = SeqAsciiString::cast(result)->GetChars();
872 WriteToFlat(first, dest, 0, first_length);
873 String* second = cs->second();
874 WriteToFlat(second,
875 dest + first_length,
876 0,
877 len - first_length);
878 } else {
John Reck59135872010-11-02 12:39:01 -0700879 { MaybeObject* maybe_object =
Steve Block44f0eee2011-05-26 01:26:41 +0100880 heap->AllocateRawTwoByteString(len, tenure);
John Reck59135872010-11-02 12:39:01 -0700881 if (!maybe_object->ToObject(&object)) return maybe_object;
882 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 result = String::cast(object);
884 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
885 String* first = cs->first();
886 int first_length = first->length();
887 WriteToFlat(first, dest, 0, first_length);
888 String* second = cs->second();
889 WriteToFlat(second,
890 dest + first_length,
891 0,
892 len - first_length);
893 }
894 cs->set_first(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100895 cs->set_second(heap->empty_string(), SKIP_WRITE_BARRIER);
Leon Clarkef7060e22010-06-03 12:02:55 +0100896 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 }
898 default:
899 return this;
900 }
901}
902
903
904bool String::MakeExternal(v8::String::ExternalStringResource* resource) {
Steve Block8defd9f2010-07-08 12:39:36 +0100905 // Externalizing twice leaks the external resource, so it's
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100906 // prohibited by the API.
907 ASSERT(!this->IsExternalString());
Steve Blocka7e24c12009-10-30 11:49:00 +0000908#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000909 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000910 // Assert that the resource and the string are equivalent.
911 ASSERT(static_cast<size_t>(this->length()) == resource->length());
Kristian Monsen25f61362010-05-21 11:50:48 +0100912 ScopedVector<uc16> smart_chars(this->length());
913 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
914 ASSERT(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000915 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +0100916 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 }
918#endif // DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100919 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000920 int size = this->Size(); // Byte size of the original string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100921 if (size < ExternalString::kShortSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000922 return false;
923 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100924 bool is_ascii = this->IsAsciiRepresentation();
Steve Blocka7e24c12009-10-30 11:49:00 +0000925 bool is_symbol = this->IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000926
927 // Morph the object to an external string by adjusting the map and
928 // reinitializing the fields.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100929 if (size >= ExternalString::kSize) {
930 this->set_map_no_write_barrier(
931 is_symbol
932 ? (is_ascii ? heap->external_symbol_with_ascii_data_map()
933 : heap->external_symbol_map())
934 : (is_ascii ? heap->external_string_with_ascii_data_map()
935 : heap->external_string_map()));
936 } else {
937 this->set_map_no_write_barrier(
938 is_symbol
939 ? (is_ascii ? heap->short_external_symbol_with_ascii_data_map()
940 : heap->short_external_symbol_map())
941 : (is_ascii ? heap->short_external_string_with_ascii_data_map()
942 : heap->short_external_string_map()));
Ben Murdoch85b71792012-04-11 18:30:58 +0100943 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100944 ExternalTwoByteString* self = ExternalTwoByteString::cast(this);
945 self->set_resource(resource);
946 if (is_symbol) self->Hash(); // Force regeneration of the hash value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000947
948 // Fill the remainder of the string with dead wood.
949 int new_size = this->Size(); // Byte size of the external String object.
Steve Block44f0eee2011-05-26 01:26:41 +0100950 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100951 if (Marking::IsBlack(Marking::MarkBitFrom(this))) {
952 MemoryChunk::IncrementLiveBytesFromMutator(this->address(),
953 new_size - size);
954 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000955 return true;
956}
957
958
959bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) {
960#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +0000961 if (FLAG_enable_slow_asserts) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000962 // Assert that the resource and the string are equivalent.
963 ASSERT(static_cast<size_t>(this->length()) == resource->length());
Kristian Monsen25f61362010-05-21 11:50:48 +0100964 ScopedVector<char> smart_chars(this->length());
965 String::WriteToFlat(this, smart_chars.start(), 0, this->length());
966 ASSERT(memcmp(smart_chars.start(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000967 resource->data(),
Kristian Monsen25f61362010-05-21 11:50:48 +0100968 resource->length() * sizeof(smart_chars[0])) == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000969 }
970#endif // DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100971 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 int size = this->Size(); // Byte size of the original string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100973 if (size < ExternalString::kShortSize) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000974 return false;
975 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000976 bool is_symbol = this->IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000977
978 // Morph the object to an external string by adjusting the map and
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100979 // reinitializing the fields. Use short version if space is limited.
980 if (size >= ExternalString::kSize) {
981 this->set_map_no_write_barrier(
982 is_symbol ? heap->external_ascii_symbol_map()
983 : heap->external_ascii_string_map());
984 } else {
985 this->set_map_no_write_barrier(
986 is_symbol ? heap->short_external_ascii_symbol_map()
987 : heap->short_external_ascii_string_map());
Ben Murdoch85b71792012-04-11 18:30:58 +0100988 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100989 ExternalAsciiString* self = ExternalAsciiString::cast(this);
990 self->set_resource(resource);
991 if (is_symbol) self->Hash(); // Force regeneration of the hash value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000992
993 // Fill the remainder of the string with dead wood.
994 int new_size = this->Size(); // Byte size of the external String object.
Steve Block44f0eee2011-05-26 01:26:41 +0100995 heap->CreateFillerObjectAt(this->address() + new_size, size - new_size);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100996 if (Marking::IsBlack(Marking::MarkBitFrom(this))) {
997 MemoryChunk::IncrementLiveBytesFromMutator(this->address(),
998 new_size - size);
999 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 return true;
1001}
1002
1003
1004void String::StringShortPrint(StringStream* accumulator) {
1005 int len = length();
Steve Blockd0582a62009-12-15 09:54:21 +00001006 if (len > kMaxShortPrintLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001007 accumulator->Add("<Very long string[%u]>", len);
1008 return;
1009 }
1010
1011 if (!LooksValid()) {
1012 accumulator->Add("<Invalid String>");
1013 return;
1014 }
1015
1016 StringInputBuffer buf(this);
1017
1018 bool truncated = false;
1019 if (len > kMaxShortPrintLength) {
1020 len = kMaxShortPrintLength;
1021 truncated = true;
1022 }
1023 bool ascii = true;
1024 for (int i = 0; i < len; i++) {
1025 int c = buf.GetNext();
1026
1027 if (c < 32 || c >= 127) {
1028 ascii = false;
1029 }
1030 }
1031 buf.Reset(this);
1032 if (ascii) {
1033 accumulator->Add("<String[%u]: ", length());
1034 for (int i = 0; i < len; i++) {
1035 accumulator->Put(buf.GetNext());
1036 }
1037 accumulator->Put('>');
1038 } else {
1039 // Backslash indicates that the string contains control
1040 // characters and that backslashes are therefore escaped.
1041 accumulator->Add("<String[%u]\\: ", length());
1042 for (int i = 0; i < len; i++) {
1043 int c = buf.GetNext();
1044 if (c == '\n') {
1045 accumulator->Add("\\n");
1046 } else if (c == '\r') {
1047 accumulator->Add("\\r");
1048 } else if (c == '\\') {
1049 accumulator->Add("\\\\");
1050 } else if (c < 32 || c > 126) {
1051 accumulator->Add("\\x%02x", c);
1052 } else {
1053 accumulator->Put(c);
1054 }
1055 }
1056 if (truncated) {
1057 accumulator->Put('.');
1058 accumulator->Put('.');
1059 accumulator->Put('.');
1060 }
1061 accumulator->Put('>');
1062 }
1063 return;
1064}
1065
1066
1067void JSObject::JSObjectShortPrint(StringStream* accumulator) {
1068 switch (map()->instance_type()) {
1069 case JS_ARRAY_TYPE: {
1070 double length = JSArray::cast(this)->length()->Number();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001071 accumulator->Add("<JS Array[%u]>", static_cast<uint32_t>(length));
Steve Blocka7e24c12009-10-30 11:49:00 +00001072 break;
1073 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001074 case JS_WEAK_MAP_TYPE: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001075 accumulator->Add("<JS WeakMap>");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001076 break;
1077 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 case JS_REGEXP_TYPE: {
1079 accumulator->Add("<JS RegExp>");
1080 break;
1081 }
1082 case JS_FUNCTION_TYPE: {
1083 Object* fun_name = JSFunction::cast(this)->shared()->name();
1084 bool printed = false;
1085 if (fun_name->IsString()) {
1086 String* str = String::cast(fun_name);
1087 if (str->length() > 0) {
1088 accumulator->Add("<JS Function ");
1089 accumulator->Put(str);
1090 accumulator->Put('>');
1091 printed = true;
1092 }
1093 }
1094 if (!printed) {
1095 accumulator->Add("<JS Function>");
1096 }
1097 break;
1098 }
1099 // All other JSObjects are rather similar to each other (JSObject,
1100 // JSGlobalProxy, JSGlobalObject, JSUndetectableObject, JSValue).
1101 default: {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001102 Map* map_of_this = map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001103 Heap* heap = GetHeap();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001104 Object* constructor = map_of_this->constructor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001105 bool printed = false;
1106 if (constructor->IsHeapObject() &&
Steve Block44f0eee2011-05-26 01:26:41 +01001107 !heap->Contains(HeapObject::cast(constructor))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001108 accumulator->Add("!!!INVALID CONSTRUCTOR!!!");
1109 } else {
1110 bool global_object = IsJSGlobalProxy();
1111 if (constructor->IsJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001112 if (!heap->Contains(JSFunction::cast(constructor)->shared())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 accumulator->Add("!!!INVALID SHARED ON CONSTRUCTOR!!!");
1114 } else {
1115 Object* constructor_name =
1116 JSFunction::cast(constructor)->shared()->name();
1117 if (constructor_name->IsString()) {
1118 String* str = String::cast(constructor_name);
1119 if (str->length() > 0) {
1120 bool vowel = AnWord(str);
1121 accumulator->Add("<%sa%s ",
1122 global_object ? "Global Object: " : "",
1123 vowel ? "n" : "");
1124 accumulator->Put(str);
Steve Blocka7e24c12009-10-30 11:49:00 +00001125 printed = true;
1126 }
1127 }
1128 }
1129 }
1130 if (!printed) {
1131 accumulator->Add("<JS %sObject", global_object ? "Global " : "");
1132 }
1133 }
1134 if (IsJSValue()) {
1135 accumulator->Add(" value = ");
1136 JSValue::cast(this)->value()->ShortPrint(accumulator);
1137 }
1138 accumulator->Put('>');
1139 break;
1140 }
1141 }
1142}
1143
1144
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001145void JSObject::PrintElementsTransition(
1146 FILE* file, ElementsKind from_kind, FixedArrayBase* from_elements,
1147 ElementsKind to_kind, FixedArrayBase* to_elements) {
1148 if (from_kind != to_kind) {
1149 PrintF(file, "elements transition [");
1150 PrintElementsKind(file, from_kind);
1151 PrintF(file, " -> ");
1152 PrintElementsKind(file, to_kind);
1153 PrintF(file, "] in ");
1154 JavaScriptFrame::PrintTop(file, false, true);
1155 PrintF(file, " for ");
1156 ShortPrint(file);
1157 PrintF(file, " from ");
1158 from_elements->ShortPrint(file);
1159 PrintF(file, " to ");
1160 to_elements->ShortPrint(file);
1161 PrintF(file, "\n");
1162 }
1163}
1164
1165
Steve Blocka7e24c12009-10-30 11:49:00 +00001166void HeapObject::HeapObjectShortPrint(StringStream* accumulator) {
Steve Block44f0eee2011-05-26 01:26:41 +01001167 Heap* heap = GetHeap();
1168 if (!heap->Contains(this)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001169 accumulator->Add("!!!INVALID POINTER!!!");
1170 return;
1171 }
Steve Block44f0eee2011-05-26 01:26:41 +01001172 if (!heap->Contains(map())) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 accumulator->Add("!!!INVALID MAP!!!");
1174 return;
1175 }
1176
1177 accumulator->Add("%p ", this);
1178
1179 if (IsString()) {
1180 String::cast(this)->StringShortPrint(accumulator);
1181 return;
1182 }
1183 if (IsJSObject()) {
1184 JSObject::cast(this)->JSObjectShortPrint(accumulator);
1185 return;
1186 }
1187 switch (map()->instance_type()) {
1188 case MAP_TYPE:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001189 accumulator->Add("<Map(elements=%u)>", Map::cast(this)->elements_kind());
Steve Blocka7e24c12009-10-30 11:49:00 +00001190 break;
1191 case FIXED_ARRAY_TYPE:
1192 accumulator->Add("<FixedArray[%u]>", FixedArray::cast(this)->length());
1193 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001194 case FIXED_DOUBLE_ARRAY_TYPE:
1195 accumulator->Add("<FixedDoubleArray[%u]>",
1196 FixedDoubleArray::cast(this)->length());
1197 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 case BYTE_ARRAY_TYPE:
1199 accumulator->Add("<ByteArray[%u]>", ByteArray::cast(this)->length());
1200 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001201 case FREE_SPACE_TYPE:
1202 accumulator->Add("<FreeSpace[%u]>", FreeSpace::cast(this)->Size());
1203 break;
Steve Block44f0eee2011-05-26 01:26:41 +01001204 case EXTERNAL_PIXEL_ARRAY_TYPE:
1205 accumulator->Add("<ExternalPixelArray[%u]>",
1206 ExternalPixelArray::cast(this)->length());
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 break;
Steve Block3ce2e202009-11-05 08:53:23 +00001208 case EXTERNAL_BYTE_ARRAY_TYPE:
1209 accumulator->Add("<ExternalByteArray[%u]>",
1210 ExternalByteArray::cast(this)->length());
1211 break;
1212 case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
1213 accumulator->Add("<ExternalUnsignedByteArray[%u]>",
1214 ExternalUnsignedByteArray::cast(this)->length());
1215 break;
1216 case EXTERNAL_SHORT_ARRAY_TYPE:
1217 accumulator->Add("<ExternalShortArray[%u]>",
1218 ExternalShortArray::cast(this)->length());
1219 break;
1220 case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
1221 accumulator->Add("<ExternalUnsignedShortArray[%u]>",
1222 ExternalUnsignedShortArray::cast(this)->length());
1223 break;
1224 case EXTERNAL_INT_ARRAY_TYPE:
1225 accumulator->Add("<ExternalIntArray[%u]>",
1226 ExternalIntArray::cast(this)->length());
1227 break;
1228 case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
1229 accumulator->Add("<ExternalUnsignedIntArray[%u]>",
1230 ExternalUnsignedIntArray::cast(this)->length());
1231 break;
1232 case EXTERNAL_FLOAT_ARRAY_TYPE:
1233 accumulator->Add("<ExternalFloatArray[%u]>",
1234 ExternalFloatArray::cast(this)->length());
1235 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001236 case EXTERNAL_DOUBLE_ARRAY_TYPE:
1237 accumulator->Add("<ExternalDoubleArray[%u]>",
1238 ExternalDoubleArray::cast(this)->length());
1239 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001240 case SHARED_FUNCTION_INFO_TYPE:
1241 accumulator->Add("<SharedFunctionInfo>");
1242 break;
Steve Block1e0659c2011-05-24 12:43:12 +01001243 case JS_MESSAGE_OBJECT_TYPE:
1244 accumulator->Add("<JSMessageObject>");
1245 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001246#define MAKE_STRUCT_CASE(NAME, Name, name) \
1247 case NAME##_TYPE: \
1248 accumulator->Put('<'); \
1249 accumulator->Add(#Name); \
1250 accumulator->Put('>'); \
1251 break;
1252 STRUCT_LIST(MAKE_STRUCT_CASE)
1253#undef MAKE_STRUCT_CASE
1254 case CODE_TYPE:
1255 accumulator->Add("<Code>");
1256 break;
1257 case ODDBALL_TYPE: {
1258 if (IsUndefined())
1259 accumulator->Add("<undefined>");
1260 else if (IsTheHole())
1261 accumulator->Add("<the hole>");
1262 else if (IsNull())
1263 accumulator->Add("<null>");
1264 else if (IsTrue())
1265 accumulator->Add("<true>");
1266 else if (IsFalse())
1267 accumulator->Add("<false>");
1268 else
1269 accumulator->Add("<Odd Oddball>");
1270 break;
1271 }
1272 case HEAP_NUMBER_TYPE:
1273 accumulator->Add("<Number: ");
1274 HeapNumber::cast(this)->HeapNumberPrint(accumulator);
1275 accumulator->Put('>');
1276 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00001277 case JS_PROXY_TYPE:
1278 accumulator->Add("<JSProxy>");
1279 break;
1280 case JS_FUNCTION_PROXY_TYPE:
1281 accumulator->Add("<JSFunctionProxy>");
1282 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001283 case FOREIGN_TYPE:
1284 accumulator->Add("<Foreign>");
Steve Blocka7e24c12009-10-30 11:49:00 +00001285 break;
1286 case JS_GLOBAL_PROPERTY_CELL_TYPE:
1287 accumulator->Add("Cell for ");
1288 JSGlobalPropertyCell::cast(this)->value()->ShortPrint(accumulator);
1289 break;
1290 default:
1291 accumulator->Add("<Other heap object (%d)>", map()->instance_type());
1292 break;
1293 }
1294}
1295
1296
Steve Blocka7e24c12009-10-30 11:49:00 +00001297void HeapObject::Iterate(ObjectVisitor* v) {
1298 // Handle header
1299 IteratePointer(v, kMapOffset);
1300 // Handle object body
1301 Map* m = map();
1302 IterateBody(m->instance_type(), SizeFromMap(m), v);
1303}
1304
1305
1306void HeapObject::IterateBody(InstanceType type, int object_size,
1307 ObjectVisitor* v) {
1308 // Avoiding <Type>::cast(this) because it accesses the map pointer field.
1309 // During GC, the map pointer field is encoded.
1310 if (type < FIRST_NONSTRING_TYPE) {
1311 switch (type & kStringRepresentationMask) {
1312 case kSeqStringTag:
1313 break;
1314 case kConsStringTag:
Iain Merrick75681382010-08-19 15:07:18 +01001315 ConsString::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 break;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001317 case kSlicedStringTag:
1318 SlicedString::BodyDescriptor::IterateBody(this, v);
1319 break;
Steve Blockd0582a62009-12-15 09:54:21 +00001320 case kExternalStringTag:
1321 if ((type & kStringEncodingMask) == kAsciiStringTag) {
1322 reinterpret_cast<ExternalAsciiString*>(this)->
1323 ExternalAsciiStringIterateBody(v);
1324 } else {
1325 reinterpret_cast<ExternalTwoByteString*>(this)->
1326 ExternalTwoByteStringIterateBody(v);
1327 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 break;
1329 }
1330 return;
1331 }
1332
1333 switch (type) {
1334 case FIXED_ARRAY_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001335 FixedArray::BodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001336 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001337 case FIXED_DOUBLE_ARRAY_TYPE:
1338 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 case JS_OBJECT_TYPE:
1340 case JS_CONTEXT_EXTENSION_OBJECT_TYPE:
1341 case JS_VALUE_TYPE:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001342 case JS_DATE_TYPE:
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 case JS_ARRAY_TYPE:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001344 case JS_SET_TYPE:
1345 case JS_MAP_TYPE:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001346 case JS_WEAK_MAP_TYPE:
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 case JS_REGEXP_TYPE:
Steve Blocka7e24c12009-10-30 11:49:00 +00001348 case JS_GLOBAL_PROXY_TYPE:
1349 case JS_GLOBAL_OBJECT_TYPE:
1350 case JS_BUILTINS_OBJECT_TYPE:
Steve Block1e0659c2011-05-24 12:43:12 +01001351 case JS_MESSAGE_OBJECT_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001352 JSObject::BodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001353 break;
Steve Block791712a2010-08-27 10:21:07 +01001354 case JS_FUNCTION_TYPE:
1355 reinterpret_cast<JSFunction*>(this)
1356 ->JSFunctionIterateBody(object_size, v);
1357 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001358 case ODDBALL_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001359 Oddball::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001360 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001361 case JS_PROXY_TYPE:
1362 JSProxy::BodyDescriptor::IterateBody(this, v);
1363 break;
Ben Murdoch589d6972011-11-30 16:04:58 +00001364 case JS_FUNCTION_PROXY_TYPE:
1365 JSFunctionProxy::BodyDescriptor::IterateBody(this, v);
1366 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001367 case FOREIGN_TYPE:
1368 reinterpret_cast<Foreign*>(this)->ForeignIterateBody(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001369 break;
1370 case MAP_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001371 Map::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001372 break;
1373 case CODE_TYPE:
1374 reinterpret_cast<Code*>(this)->CodeIterateBody(v);
1375 break;
1376 case JS_GLOBAL_PROPERTY_CELL_TYPE:
Iain Merrick75681382010-08-19 15:07:18 +01001377 JSGlobalPropertyCell::BodyDescriptor::IterateBody(this, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 break;
1379 case HEAP_NUMBER_TYPE:
1380 case FILLER_TYPE:
1381 case BYTE_ARRAY_TYPE:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001382 case FREE_SPACE_TYPE:
Steve Block44f0eee2011-05-26 01:26:41 +01001383 case EXTERNAL_PIXEL_ARRAY_TYPE:
Steve Block3ce2e202009-11-05 08:53:23 +00001384 case EXTERNAL_BYTE_ARRAY_TYPE:
1385 case EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE:
1386 case EXTERNAL_SHORT_ARRAY_TYPE:
1387 case EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE:
1388 case EXTERNAL_INT_ARRAY_TYPE:
1389 case EXTERNAL_UNSIGNED_INT_ARRAY_TYPE:
1390 case EXTERNAL_FLOAT_ARRAY_TYPE:
Ben Murdoch257744e2011-11-30 15:57:28 +00001391 case EXTERNAL_DOUBLE_ARRAY_TYPE:
Steve Blocka7e24c12009-10-30 11:49:00 +00001392 break;
Ben Murdoch8f9999f2012-04-23 10:39:17 +01001393 case SHARED_FUNCTION_INFO_TYPE: {
1394 SharedFunctionInfo* shared = reinterpret_cast<SharedFunctionInfo*>(this);
1395 shared->SharedFunctionInfoIterateBody(v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001396 break;
Ben Murdoch8f9999f2012-04-23 10:39:17 +01001397 }
Iain Merrick75681382010-08-19 15:07:18 +01001398
Steve Blocka7e24c12009-10-30 11:49:00 +00001399#define MAKE_STRUCT_CASE(NAME, Name, name) \
1400 case NAME##_TYPE:
1401 STRUCT_LIST(MAKE_STRUCT_CASE)
1402#undef MAKE_STRUCT_CASE
Iain Merrick75681382010-08-19 15:07:18 +01001403 StructBodyDescriptor::IterateBody(this, object_size, v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001404 break;
1405 default:
1406 PrintF("Unknown type: %d\n", type);
1407 UNREACHABLE();
1408 }
1409}
1410
1411
Steve Blocka7e24c12009-10-30 11:49:00 +00001412Object* HeapNumber::HeapNumberToBoolean() {
1413 // NaN, +0, and -0 should return the false object
Iain Merrick75681382010-08-19 15:07:18 +01001414#if __BYTE_ORDER == __LITTLE_ENDIAN
1415 union IeeeDoubleLittleEndianArchType u;
1416#elif __BYTE_ORDER == __BIG_ENDIAN
1417 union IeeeDoubleBigEndianArchType u;
1418#endif
1419 u.d = value();
1420 if (u.bits.exp == 2047) {
1421 // Detect NaN for IEEE double precision floating point.
1422 if ((u.bits.man_low | u.bits.man_high) != 0)
Steve Block44f0eee2011-05-26 01:26:41 +01001423 return GetHeap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001424 }
Iain Merrick75681382010-08-19 15:07:18 +01001425 if (u.bits.exp == 0) {
1426 // Detect +0, and -0 for IEEE double precision floating point.
1427 if ((u.bits.man_low | u.bits.man_high) == 0)
Steve Block44f0eee2011-05-26 01:26:41 +01001428 return GetHeap()->false_value();
Iain Merrick75681382010-08-19 15:07:18 +01001429 }
Steve Block44f0eee2011-05-26 01:26:41 +01001430 return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00001431}
1432
1433
Ben Murdochb0fe1622011-05-05 13:52:32 +01001434void HeapNumber::HeapNumberPrint(FILE* out) {
1435 PrintF(out, "%.16g", Number());
Steve Blocka7e24c12009-10-30 11:49:00 +00001436}
1437
1438
1439void HeapNumber::HeapNumberPrint(StringStream* accumulator) {
1440 // The Windows version of vsnprintf can allocate when printing a %g string
1441 // into a buffer that may not be big enough. We don't want random memory
1442 // allocation when producing post-crash stack traces, so we print into a
1443 // buffer that is plenty big enough for any floating point number, then
1444 // print that using vsnprintf (which may truncate but never allocate if
1445 // there is no more space in the buffer).
1446 EmbeddedVector<char, 100> buffer;
1447 OS::SNPrintF(buffer, "%.16g", Number());
1448 accumulator->Add("%s", buffer.start());
1449}
1450
1451
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001452String* JSReceiver::class_name() {
1453 if (IsJSFunction() && IsJSFunctionProxy()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001454 return GetHeap()->function_class_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001455 }
1456 if (map()->constructor()->IsJSFunction()) {
1457 JSFunction* constructor = JSFunction::cast(map()->constructor());
1458 return String::cast(constructor->shared()->instance_class_name());
1459 }
1460 // If the constructor is not present, return "Object".
Steve Block44f0eee2011-05-26 01:26:41 +01001461 return GetHeap()->Object_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001462}
1463
1464
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001465String* JSReceiver::constructor_name() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001466 if (map()->constructor()->IsJSFunction()) {
1467 JSFunction* constructor = JSFunction::cast(map()->constructor());
1468 String* name = String::cast(constructor->shared()->name());
Ben Murdochf87a2032010-10-22 12:50:53 +01001469 if (name->length() > 0) return name;
1470 String* inferred_name = constructor->shared()->inferred_name();
1471 if (inferred_name->length() > 0) return inferred_name;
1472 Object* proto = GetPrototype();
1473 if (proto->IsJSObject()) return JSObject::cast(proto)->constructor_name();
Steve Blocka7e24c12009-10-30 11:49:00 +00001474 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001475 // TODO(rossberg): what about proxies?
Steve Blocka7e24c12009-10-30 11:49:00 +00001476 // If the constructor is not present, return "Object".
Steve Block44f0eee2011-05-26 01:26:41 +01001477 return GetHeap()->Object_symbol();
Steve Blocka7e24c12009-10-30 11:49:00 +00001478}
1479
1480
John Reck59135872010-11-02 12:39:01 -07001481MaybeObject* JSObject::AddFastPropertyUsingMap(Map* new_map,
1482 String* name,
1483 Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001484 int index = new_map->PropertyIndexFor(name);
1485 if (map()->unused_property_fields() == 0) {
1486 ASSERT(map()->unused_property_fields() == 0);
1487 int new_unused = new_map->unused_property_fields();
John Reck59135872010-11-02 12:39:01 -07001488 Object* values;
1489 { MaybeObject* maybe_values =
1490 properties()->CopySize(properties()->length() + new_unused + 1);
1491 if (!maybe_values->ToObject(&values)) return maybe_values;
1492 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 set_properties(FixedArray::cast(values));
1494 }
1495 set_map(new_map);
1496 return FastPropertyAtPut(index, value);
1497}
1498
1499
Ben Murdoch8b112d22011-06-08 16:22:53 +01001500static bool IsIdentifier(UnicodeCache* cache,
1501 unibrow::CharacterStream* buffer) {
1502 // Checks whether the buffer contains an identifier (no escape).
1503 if (!buffer->has_more()) return false;
1504 if (!cache->IsIdentifierStart(buffer->GetNext())) {
1505 return false;
1506 }
1507 while (buffer->has_more()) {
1508 if (!cache->IsIdentifierPart(buffer->GetNext())) {
1509 return false;
1510 }
1511 }
1512 return true;
1513}
1514
1515
John Reck59135872010-11-02 12:39:01 -07001516MaybeObject* JSObject::AddFastProperty(String* name,
1517 Object* value,
1518 PropertyAttributes attributes) {
Steve Block1e0659c2011-05-24 12:43:12 +01001519 ASSERT(!IsJSGlobalProxy());
1520
Steve Blocka7e24c12009-10-30 11:49:00 +00001521 // Normalize the object if the name is an actual string (not the
1522 // hidden symbols) and is not a real identifier.
Steve Block44f0eee2011-05-26 01:26:41 +01001523 Isolate* isolate = GetHeap()->isolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00001524 StringInputBuffer buffer(name);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001525 if (!IsIdentifier(isolate->unicode_cache(), &buffer)
Steve Block44f0eee2011-05-26 01:26:41 +01001526 && name != isolate->heap()->hidden_symbol()) {
John Reck59135872010-11-02 12:39:01 -07001527 Object* obj;
1528 { MaybeObject* maybe_obj =
1529 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1530 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1531 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001532 return AddSlowProperty(name, value, attributes);
1533 }
1534
1535 DescriptorArray* old_descriptors = map()->instance_descriptors();
1536 // Compute the new index for new field.
1537 int index = map()->NextFreePropertyIndex();
1538
1539 // Allocate new instance descriptors with (name, index) added
1540 FieldDescriptor new_field(name, index, attributes);
John Reck59135872010-11-02 12:39:01 -07001541 Object* new_descriptors;
1542 { MaybeObject* maybe_new_descriptors =
1543 old_descriptors->CopyInsert(&new_field, REMOVE_TRANSITIONS);
1544 if (!maybe_new_descriptors->ToObject(&new_descriptors)) {
1545 return maybe_new_descriptors;
1546 }
1547 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001548
Steve Block44f0eee2011-05-26 01:26:41 +01001549 // Only allow map transition if the object isn't the global object and there
1550 // is not a transition for the name, or there's a transition for the name but
1551 // it's unrelated to properties.
1552 int descriptor_index = old_descriptors->Search(name);
1553
Ben Murdoch589d6972011-11-30 16:04:58 +00001554 // Element transitions are stored in the descriptor for property "", which is
1555 // not a identifier and should have forced a switch to slow properties above.
Steve Block44f0eee2011-05-26 01:26:41 +01001556 ASSERT(descriptor_index == DescriptorArray::kNotFound ||
Ben Murdoch589d6972011-11-30 16:04:58 +00001557 old_descriptors->GetType(descriptor_index) != ELEMENTS_TRANSITION);
Steve Block44f0eee2011-05-26 01:26:41 +01001558 bool can_insert_transition = descriptor_index == DescriptorArray::kNotFound ||
Ben Murdoch589d6972011-11-30 16:04:58 +00001559 old_descriptors->GetType(descriptor_index) == ELEMENTS_TRANSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +00001560 bool allow_map_transition =
Steve Block44f0eee2011-05-26 01:26:41 +01001561 can_insert_transition &&
1562 (isolate->context()->global_context()->object_function()->map() != map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001563
1564 ASSERT(index < map()->inobject_properties() ||
1565 (index - map()->inobject_properties()) < properties()->length() ||
1566 map()->unused_property_fields() == 0);
1567 // Allocate a new map for the object.
John Reck59135872010-11-02 12:39:01 -07001568 Object* r;
1569 { MaybeObject* maybe_r = map()->CopyDropDescriptors();
1570 if (!maybe_r->ToObject(&r)) return maybe_r;
1571 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001572 Map* new_map = Map::cast(r);
1573 if (allow_map_transition) {
1574 // Allocate new instance descriptors for the old map with map transition.
1575 MapTransitionDescriptor d(name, Map::cast(new_map), attributes);
John Reck59135872010-11-02 12:39:01 -07001576 Object* r;
1577 { MaybeObject* maybe_r = old_descriptors->CopyInsert(&d, KEEP_TRANSITIONS);
1578 if (!maybe_r->ToObject(&r)) return maybe_r;
1579 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001580 old_descriptors = DescriptorArray::cast(r);
1581 }
1582
1583 if (map()->unused_property_fields() == 0) {
Steve Block8defd9f2010-07-08 12:39:36 +01001584 if (properties()->length() > MaxFastProperties()) {
John Reck59135872010-11-02 12:39:01 -07001585 Object* obj;
1586 { MaybeObject* maybe_obj =
1587 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1588 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1589 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001590 return AddSlowProperty(name, value, attributes);
1591 }
1592 // Make room for the new value
John Reck59135872010-11-02 12:39:01 -07001593 Object* values;
1594 { MaybeObject* maybe_values =
1595 properties()->CopySize(properties()->length() + kFieldsAdded);
1596 if (!maybe_values->ToObject(&values)) return maybe_values;
1597 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001598 set_properties(FixedArray::cast(values));
1599 new_map->set_unused_property_fields(kFieldsAdded - 1);
1600 } else {
1601 new_map->set_unused_property_fields(map()->unused_property_fields() - 1);
1602 }
1603 // We have now allocated all the necessary objects.
1604 // All the changes can be applied at once, so they are atomic.
1605 map()->set_instance_descriptors(old_descriptors);
1606 new_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors));
1607 set_map(new_map);
1608 return FastPropertyAtPut(index, value);
1609}
1610
1611
John Reck59135872010-11-02 12:39:01 -07001612MaybeObject* JSObject::AddConstantFunctionProperty(
1613 String* name,
1614 JSFunction* function,
1615 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001616 // Allocate new instance descriptors with (name, function) added
1617 ConstantFunctionDescriptor d(name, function, attributes);
John Reck59135872010-11-02 12:39:01 -07001618 Object* new_descriptors;
1619 { MaybeObject* maybe_new_descriptors =
1620 map()->instance_descriptors()->CopyInsert(&d, REMOVE_TRANSITIONS);
1621 if (!maybe_new_descriptors->ToObject(&new_descriptors)) {
1622 return maybe_new_descriptors;
1623 }
1624 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001625
1626 // Allocate a new map for the object.
John Reck59135872010-11-02 12:39:01 -07001627 Object* new_map;
1628 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
1629 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
1630 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001631
1632 DescriptorArray* descriptors = DescriptorArray::cast(new_descriptors);
1633 Map::cast(new_map)->set_instance_descriptors(descriptors);
1634 Map* old_map = map();
1635 set_map(Map::cast(new_map));
1636
1637 // If the old map is the global object map (from new Object()),
1638 // then transitions are not added to it, so we are done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001639 Heap* heap = GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01001640 if (old_map == heap->isolate()->context()->global_context()->
1641 object_function()->map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001642 return function;
1643 }
1644
1645 // Do not add CONSTANT_TRANSITIONS to global objects
1646 if (IsGlobalObject()) {
1647 return function;
1648 }
1649
1650 // Add a CONSTANT_TRANSITION descriptor to the old map,
1651 // so future assignments to this property on other objects
1652 // of the same type will create a normal field, not a constant function.
1653 // Don't do this for special properties, with non-trival attributes.
1654 if (attributes != NONE) {
1655 return function;
1656 }
Iain Merrick75681382010-08-19 15:07:18 +01001657 ConstTransitionDescriptor mark(name, Map::cast(new_map));
John Reck59135872010-11-02 12:39:01 -07001658 { MaybeObject* maybe_new_descriptors =
1659 old_map->instance_descriptors()->CopyInsert(&mark, KEEP_TRANSITIONS);
1660 if (!maybe_new_descriptors->ToObject(&new_descriptors)) {
1661 // We have accomplished the main goal, so return success.
1662 return function;
1663 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001664 }
1665 old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors));
1666
1667 return function;
1668}
1669
1670
1671// Add property in slow mode
John Reck59135872010-11-02 12:39:01 -07001672MaybeObject* JSObject::AddSlowProperty(String* name,
1673 Object* value,
1674 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001675 ASSERT(!HasFastProperties());
1676 StringDictionary* dict = property_dictionary();
1677 Object* store_value = value;
1678 if (IsGlobalObject()) {
1679 // In case name is an orphaned property reuse the cell.
1680 int entry = dict->FindEntry(name);
1681 if (entry != StringDictionary::kNotFound) {
1682 store_value = dict->ValueAt(entry);
1683 JSGlobalPropertyCell::cast(store_value)->set_value(value);
1684 // Assign an enumeration index to the property and update
1685 // SetNextEnumerationIndex.
1686 int index = dict->NextEnumerationIndex();
1687 PropertyDetails details = PropertyDetails(attributes, NORMAL, index);
1688 dict->SetNextEnumerationIndex(index + 1);
1689 dict->SetEntry(entry, name, store_value, details);
1690 return value;
1691 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001692 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -07001693 { MaybeObject* maybe_store_value =
Steve Block44f0eee2011-05-26 01:26:41 +01001694 heap->AllocateJSGlobalPropertyCell(value);
John Reck59135872010-11-02 12:39:01 -07001695 if (!maybe_store_value->ToObject(&store_value)) return maybe_store_value;
1696 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001697 JSGlobalPropertyCell::cast(store_value)->set_value(value);
1698 }
1699 PropertyDetails details = PropertyDetails(attributes, NORMAL);
John Reck59135872010-11-02 12:39:01 -07001700 Object* result;
1701 { MaybeObject* maybe_result = dict->Add(name, store_value, details);
1702 if (!maybe_result->ToObject(&result)) return maybe_result;
1703 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001704 if (dict != result) set_properties(StringDictionary::cast(result));
1705 return value;
1706}
1707
1708
John Reck59135872010-11-02 12:39:01 -07001709MaybeObject* JSObject::AddProperty(String* name,
1710 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001711 PropertyAttributes attributes,
1712 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 ASSERT(!IsJSGlobalProxy());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001714 Map* map_of_this = map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001715 Heap* heap = GetHeap();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001716 if (!map_of_this->is_extensible()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001717 if (strict_mode == kNonStrictMode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001718 return value;
Steve Block44f0eee2011-05-26 01:26:41 +01001719 } else {
1720 Handle<Object> args[1] = {Handle<String>(name)};
1721 return heap->isolate()->Throw(
1722 *FACTORY->NewTypeError("object_not_extensible",
1723 HandleVector(args, 1)));
1724 }
Steve Block8defd9f2010-07-08 12:39:36 +01001725 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001726 if (HasFastProperties()) {
1727 // Ensure the descriptor array does not get too big.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001728 if (map_of_this->instance_descriptors()->number_of_descriptors() <
Steve Blocka7e24c12009-10-30 11:49:00 +00001729 DescriptorArray::kMaxNumberOfDescriptors) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001730 if (value->IsJSFunction()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001731 return AddConstantFunctionProperty(name,
1732 JSFunction::cast(value),
1733 attributes);
1734 } else {
1735 return AddFastProperty(name, value, attributes);
1736 }
1737 } else {
1738 // Normalize the object to prevent very large instance descriptors.
1739 // This eliminates unwanted N^2 allocation and lookup behavior.
John Reck59135872010-11-02 12:39:01 -07001740 Object* obj;
1741 { MaybeObject* maybe_obj =
1742 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1743 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1744 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001745 }
1746 }
1747 return AddSlowProperty(name, value, attributes);
1748}
1749
1750
John Reck59135872010-11-02 12:39:01 -07001751MaybeObject* JSObject::SetPropertyPostInterceptor(
1752 String* name,
1753 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001754 PropertyAttributes attributes,
1755 StrictModeFlag strict_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001756 // Check local property, ignore interceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001757 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001758 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00001759 if (result.IsFound()) {
1760 // An existing property, a map transition or a null descriptor was
1761 // found. Use set property to handle all these cases.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001762 return SetProperty(&result, name, value, attributes, strict_mode);
Andrei Popescu402d9372010-02-26 13:31:12 +00001763 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001764 bool found = false;
1765 MaybeObject* result_object;
1766 result_object = SetPropertyWithCallbackSetterInPrototypes(name,
1767 value,
1768 attributes,
1769 &found,
1770 strict_mode);
1771 if (found) return result_object;
Andrei Popescu402d9372010-02-26 13:31:12 +00001772 // Add a new real property.
Steve Block44f0eee2011-05-26 01:26:41 +01001773 return AddProperty(name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001774}
1775
1776
John Reck59135872010-11-02 12:39:01 -07001777MaybeObject* JSObject::ReplaceSlowProperty(String* name,
1778 Object* value,
1779 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001780 StringDictionary* dictionary = property_dictionary();
1781 int old_index = dictionary->FindEntry(name);
1782 int new_enumeration_index = 0; // 0 means "Use the next available index."
1783 if (old_index != -1) {
1784 // All calls to ReplaceSlowProperty have had all transitions removed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001785 ASSERT(!dictionary->ContainsTransition(old_index));
Steve Blocka7e24c12009-10-30 11:49:00 +00001786 new_enumeration_index = dictionary->DetailsAt(old_index).index();
1787 }
1788
1789 PropertyDetails new_details(attributes, NORMAL, new_enumeration_index);
1790 return SetNormalizedProperty(name, value, new_details);
1791}
1792
Steve Blockd0582a62009-12-15 09:54:21 +00001793
John Reck59135872010-11-02 12:39:01 -07001794MaybeObject* JSObject::ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001795 String* name,
1796 Object* new_value,
1797 PropertyAttributes attributes) {
1798 Map* old_map = map();
John Reck59135872010-11-02 12:39:01 -07001799 Object* result;
1800 { MaybeObject* maybe_result =
1801 ConvertDescriptorToField(name, new_value, attributes);
1802 if (!maybe_result->ToObject(&result)) return maybe_result;
1803 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001804 // If we get to this point we have succeeded - do not return failure
1805 // after this point. Later stuff is optional.
1806 if (!HasFastProperties()) {
1807 return result;
1808 }
1809 // Do not add transitions to the map of "new Object()".
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001810 if (map() == GetIsolate()->context()->global_context()->
Steve Block44f0eee2011-05-26 01:26:41 +01001811 object_function()->map()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001812 return result;
1813 }
1814
1815 MapTransitionDescriptor transition(name,
1816 map(),
1817 attributes);
John Reck59135872010-11-02 12:39:01 -07001818 Object* new_descriptors;
1819 { MaybeObject* maybe_new_descriptors = old_map->instance_descriptors()->
1820 CopyInsert(&transition, KEEP_TRANSITIONS);
1821 if (!maybe_new_descriptors->ToObject(&new_descriptors)) {
1822 return result; // Yes, return _result_.
1823 }
1824 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001825 old_map->set_instance_descriptors(DescriptorArray::cast(new_descriptors));
1826 return result;
1827}
1828
1829
John Reck59135872010-11-02 12:39:01 -07001830MaybeObject* JSObject::ConvertDescriptorToField(String* name,
1831 Object* new_value,
1832 PropertyAttributes attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001833 if (map()->unused_property_fields() == 0 &&
Steve Block8defd9f2010-07-08 12:39:36 +01001834 properties()->length() > MaxFastProperties()) {
John Reck59135872010-11-02 12:39:01 -07001835 Object* obj;
1836 { MaybeObject* maybe_obj =
1837 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
1838 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1839 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001840 return ReplaceSlowProperty(name, new_value, attributes);
1841 }
1842
1843 int index = map()->NextFreePropertyIndex();
1844 FieldDescriptor new_field(name, index, attributes);
1845 // Make a new DescriptorArray replacing an entry with FieldDescriptor.
John Reck59135872010-11-02 12:39:01 -07001846 Object* descriptors_unchecked;
1847 { MaybeObject* maybe_descriptors_unchecked = map()->instance_descriptors()->
1848 CopyInsert(&new_field, REMOVE_TRANSITIONS);
1849 if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) {
1850 return maybe_descriptors_unchecked;
1851 }
1852 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001853 DescriptorArray* new_descriptors =
1854 DescriptorArray::cast(descriptors_unchecked);
1855
1856 // Make a new map for the object.
John Reck59135872010-11-02 12:39:01 -07001857 Object* new_map_unchecked;
1858 { MaybeObject* maybe_new_map_unchecked = map()->CopyDropDescriptors();
1859 if (!maybe_new_map_unchecked->ToObject(&new_map_unchecked)) {
1860 return maybe_new_map_unchecked;
1861 }
1862 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001863 Map* new_map = Map::cast(new_map_unchecked);
1864 new_map->set_instance_descriptors(new_descriptors);
1865
1866 // Make new properties array if necessary.
1867 FixedArray* new_properties = 0; // Will always be NULL or a valid pointer.
1868 int new_unused_property_fields = map()->unused_property_fields() - 1;
1869 if (map()->unused_property_fields() == 0) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001870 new_unused_property_fields = kFieldsAdded - 1;
John Reck59135872010-11-02 12:39:01 -07001871 Object* new_properties_object;
1872 { MaybeObject* maybe_new_properties_object =
1873 properties()->CopySize(properties()->length() + kFieldsAdded);
1874 if (!maybe_new_properties_object->ToObject(&new_properties_object)) {
1875 return maybe_new_properties_object;
1876 }
1877 }
1878 new_properties = FixedArray::cast(new_properties_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001879 }
1880
1881 // Update pointers to commit changes.
1882 // Object points to the new map.
1883 new_map->set_unused_property_fields(new_unused_property_fields);
1884 set_map(new_map);
1885 if (new_properties) {
1886 set_properties(FixedArray::cast(new_properties));
1887 }
1888 return FastPropertyAtPut(index, new_value);
1889}
1890
1891
1892
John Reck59135872010-11-02 12:39:01 -07001893MaybeObject* JSObject::SetPropertyWithInterceptor(
1894 String* name,
1895 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001896 PropertyAttributes attributes,
1897 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01001898 Isolate* isolate = GetIsolate();
1899 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001900 Handle<JSObject> this_handle(this);
1901 Handle<String> name_handle(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001902 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001903 Handle<InterceptorInfo> interceptor(GetNamedInterceptor());
1904 if (!interceptor->setter()->IsUndefined()) {
Steve Block44f0eee2011-05-26 01:26:41 +01001905 LOG(isolate, ApiNamedPropertyAccess("interceptor-named-set", this, name));
1906 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001907 v8::AccessorInfo info(args.end());
1908 v8::NamedPropertySetter setter =
1909 v8::ToCData<v8::NamedPropertySetter>(interceptor->setter());
1910 v8::Handle<v8::Value> result;
1911 {
1912 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001913 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001914 Handle<Object> value_unhole(value->IsTheHole() ?
Steve Block44f0eee2011-05-26 01:26:41 +01001915 isolate->heap()->undefined_value() :
1916 value,
1917 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001918 result = setter(v8::Utils::ToLocal(name_handle),
1919 v8::Utils::ToLocal(value_unhole),
1920 info);
1921 }
Steve Block44f0eee2011-05-26 01:26:41 +01001922 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001923 if (!result.IsEmpty()) return *value_handle;
1924 }
John Reck59135872010-11-02 12:39:01 -07001925 MaybeObject* raw_result =
1926 this_handle->SetPropertyPostInterceptor(*name_handle,
1927 *value_handle,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001928 attributes,
1929 strict_mode);
Steve Block44f0eee2011-05-26 01:26:41 +01001930 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001931 return raw_result;
1932}
1933
1934
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001935Handle<Object> JSReceiver::SetProperty(Handle<JSReceiver> object,
1936 Handle<String> key,
1937 Handle<Object> value,
1938 PropertyAttributes attributes,
1939 StrictModeFlag strict_mode) {
1940 CALL_HEAP_FUNCTION(object->GetIsolate(),
1941 object->SetProperty(*key, *value, attributes, strict_mode),
1942 Object);
1943}
1944
1945
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001946MaybeObject* JSReceiver::SetProperty(String* name,
1947 Object* value,
1948 PropertyAttributes attributes,
1949 StrictModeFlag strict_mode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001950 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00001951 LocalLookup(name, &result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001952 return SetProperty(&result, name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001953}
1954
1955
John Reck59135872010-11-02 12:39:01 -07001956MaybeObject* JSObject::SetPropertyWithCallback(Object* structure,
1957 String* name,
1958 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001959 JSObject* holder,
1960 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01001961 Isolate* isolate = GetIsolate();
1962 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001963
1964 // We should never get here to initialize a const with the hole
1965 // value since a const declaration would conflict with the setter.
1966 ASSERT(!value->IsTheHole());
Steve Block44f0eee2011-05-26 01:26:41 +01001967 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001968
1969 // To accommodate both the old and the new api we switch on the
Ben Murdoch257744e2011-11-30 15:57:28 +00001970 // data structure used to store the callbacks. Eventually foreign
Steve Blocka7e24c12009-10-30 11:49:00 +00001971 // callbacks should be phased out.
Ben Murdoch257744e2011-11-30 15:57:28 +00001972 if (structure->IsForeign()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001973 AccessorDescriptor* callback =
Ben Murdoch257744e2011-11-30 15:57:28 +00001974 reinterpret_cast<AccessorDescriptor*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001975 Foreign::cast(structure)->foreign_address());
John Reck59135872010-11-02 12:39:01 -07001976 MaybeObject* obj = (callback->setter)(this, value, callback->data);
Steve Block44f0eee2011-05-26 01:26:41 +01001977 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001978 if (obj->IsFailure()) return obj;
1979 return *value_handle;
1980 }
1981
1982 if (structure->IsAccessorInfo()) {
1983 // api style callbacks
1984 AccessorInfo* data = AccessorInfo::cast(structure);
1985 Object* call_obj = data->setter();
1986 v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj);
1987 if (call_fun == NULL) return value;
1988 Handle<String> key(name);
Steve Block44f0eee2011-05-26 01:26:41 +01001989 LOG(isolate, ApiNamedPropertyAccess("store", this, name));
1990 CustomArguments args(isolate, data->data(), this, JSObject::cast(holder));
Steve Blocka7e24c12009-10-30 11:49:00 +00001991 v8::AccessorInfo info(args.end());
1992 {
1993 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01001994 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001995 call_fun(v8::Utils::ToLocal(key),
1996 v8::Utils::ToLocal(value_handle),
1997 info);
1998 }
Steve Block44f0eee2011-05-26 01:26:41 +01001999 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002000 return *value_handle;
2001 }
2002
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002003 if (structure->IsAccessorPair()) {
2004 Object* setter = AccessorPair::cast(structure)->setter();
2005 if (setter->IsSpecFunction()) {
2006 // TODO(rossberg): nicer would be to cast to some JSCallable here...
2007 return SetPropertyWithDefinedSetter(JSReceiver::cast(setter), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002008 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002009 if (strict_mode == kNonStrictMode) {
2010 return value;
2011 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002012 Handle<String> key(name);
Steve Block44f0eee2011-05-26 01:26:41 +01002013 Handle<Object> holder_handle(holder, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 Handle<Object> args[2] = { key, holder_handle };
Steve Block44f0eee2011-05-26 01:26:41 +01002015 return isolate->Throw(
2016 *isolate->factory()->NewTypeError("no_setter_in_callback",
2017 HandleVector(args, 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002018 }
2019 }
2020
2021 UNREACHABLE();
Leon Clarkef7060e22010-06-03 12:02:55 +01002022 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00002023}
2024
2025
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002026MaybeObject* JSReceiver::SetPropertyWithDefinedSetter(JSReceiver* setter,
2027 Object* value) {
Steve Block44f0eee2011-05-26 01:26:41 +01002028 Isolate* isolate = GetIsolate();
2029 Handle<Object> value_handle(value, isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002030 Handle<JSReceiver> fun(setter, isolate);
2031 Handle<JSReceiver> self(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002032#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01002033 Debug* debug = isolate->debug();
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 // Handle stepping into a setter if step into is active.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002035 // TODO(rossberg): should this apply to getters that are function proxies?
2036 if (debug->StepInActive() && fun->IsJSFunction()) {
2037 debug->HandleStepIn(
2038 Handle<JSFunction>::cast(fun), Handle<Object>::null(), 0, false);
Steve Blocka7e24c12009-10-30 11:49:00 +00002039 }
2040#endif
2041 bool has_pending_exception;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002042 Handle<Object> argv[] = { value_handle };
2043 Execution::Call(fun, self, ARRAY_SIZE(argv), argv, &has_pending_exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00002044 // Check for pending exception and return the result.
2045 if (has_pending_exception) return Failure::Exception();
2046 return *value_handle;
2047}
2048
2049
2050void JSObject::LookupCallbackSetterInPrototypes(String* name,
2051 LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01002052 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002053 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002054 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002055 pt = pt->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002056 if (pt->IsJSProxy()) {
2057 return result->HandlerResult(JSProxy::cast(pt));
2058 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002059 JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002060 if (result->IsProperty()) {
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01002061 if (result->type() == CALLBACKS && !result->IsReadOnly()) return;
2062 // Found non-callback or read-only callback, stop looking.
2063 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00002064 }
2065 }
2066 result->NotFound();
2067}
2068
2069
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002070MaybeObject* JSObject::SetElementWithCallbackSetterInPrototypes(
2071 uint32_t index,
2072 Object* value,
2073 bool* found,
2074 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002075 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002076 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002077 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002078 pt = pt->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002079 if (pt->IsJSProxy()) {
2080 String* name;
2081 MaybeObject* maybe = GetHeap()->Uint32ToString(index);
2082 if (!maybe->To<String>(&name)) {
2083 *found = true; // Force abort
2084 return maybe;
2085 }
2086 return JSProxy::cast(pt)->SetPropertyWithHandlerIfDefiningSetter(
2087 name, value, NONE, strict_mode, found);
2088 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002089 if (!JSObject::cast(pt)->HasDictionaryElements()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002090 continue;
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002092 SeededNumberDictionary* dictionary =
2093 JSObject::cast(pt)->element_dictionary();
Steve Blocka7e24c12009-10-30 11:49:00 +00002094 int entry = dictionary->FindEntry(index);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002095 if (entry != SeededNumberDictionary::kNotFound) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002096 PropertyDetails details = dictionary->DetailsAt(entry);
2097 if (details.type() == CALLBACKS) {
Steve Block1e0659c2011-05-24 12:43:12 +01002098 *found = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002099 return SetElementWithCallback(dictionary->ValueAt(entry),
2100 index,
2101 value,
2102 JSObject::cast(pt),
2103 strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002104 }
2105 }
2106 }
Steve Block1e0659c2011-05-24 12:43:12 +01002107 *found = false;
Steve Block44f0eee2011-05-26 01:26:41 +01002108 return heap->the_hole_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002109}
2110
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002111MaybeObject* JSObject::SetPropertyWithCallbackSetterInPrototypes(
2112 String* name,
2113 Object* value,
2114 PropertyAttributes attributes,
2115 bool* found,
2116 StrictModeFlag strict_mode) {
2117 Heap* heap = GetHeap();
2118 // We could not find a local property so let's check whether there is an
2119 // accessor that wants to handle the property.
2120 LookupResult accessor_result(heap->isolate());
2121 LookupCallbackSetterInPrototypes(name, &accessor_result);
2122 if (accessor_result.IsFound()) {
2123 *found = true;
2124 if (accessor_result.type() == CALLBACKS) {
2125 return SetPropertyWithCallback(accessor_result.GetCallbackObject(),
2126 name,
2127 value,
2128 accessor_result.holder(),
2129 strict_mode);
2130 } else if (accessor_result.type() == HANDLER) {
2131 // There is a proxy in the prototype chain. Invoke its
2132 // getPropertyDescriptor trap.
2133 bool found = false;
2134 // SetPropertyWithHandlerIfDefiningSetter can cause GC,
2135 // make sure to use the handlified references after calling
2136 // the function.
2137 Handle<JSObject> self(this);
2138 Handle<String> hname(name);
2139 Handle<Object> hvalue(value);
2140 MaybeObject* result =
2141 accessor_result.proxy()->SetPropertyWithHandlerIfDefiningSetter(
2142 name, value, attributes, strict_mode, &found);
2143 if (found) return result;
2144 // The proxy does not define the property as an accessor.
2145 // Consequently, it has no effect on setting the receiver.
2146 return self->AddProperty(*hname, *hvalue, attributes, strict_mode);
2147 }
2148 }
2149 *found = false;
2150 return heap->the_hole_value();
2151}
2152
Steve Blocka7e24c12009-10-30 11:49:00 +00002153
2154void JSObject::LookupInDescriptor(String* name, LookupResult* result) {
2155 DescriptorArray* descriptors = map()->instance_descriptors();
Iain Merrick75681382010-08-19 15:07:18 +01002156 int number = descriptors->SearchWithCache(name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002157 if (number != DescriptorArray::kNotFound) {
2158 result->DescriptorResult(this, descriptors->GetDetails(number), number);
2159 } else {
2160 result->NotFound();
2161 }
2162}
2163
2164
Ben Murdochb0fe1622011-05-05 13:52:32 +01002165void Map::LookupInDescriptors(JSObject* holder,
2166 String* name,
2167 LookupResult* result) {
2168 DescriptorArray* descriptors = instance_descriptors();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002169 DescriptorLookupCache* cache =
2170 GetHeap()->isolate()->descriptor_lookup_cache();
Steve Block44f0eee2011-05-26 01:26:41 +01002171 int number = cache->Lookup(descriptors, name);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002172 if (number == DescriptorLookupCache::kAbsent) {
2173 number = descriptors->Search(name);
Steve Block44f0eee2011-05-26 01:26:41 +01002174 cache->Update(descriptors, name, number);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002175 }
2176 if (number != DescriptorArray::kNotFound) {
2177 result->DescriptorResult(holder, descriptors->GetDetails(number), number);
2178 } else {
2179 result->NotFound();
2180 }
2181}
2182
2183
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002184static bool ContainsMap(MapHandleList* maps, Handle<Map> map) {
2185 ASSERT(!map.is_null());
2186 for (int i = 0; i < maps->length(); ++i) {
2187 if (!maps->at(i).is_null() && maps->at(i).is_identical_to(map)) return true;
2188 }
2189 return false;
2190}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002191
Ben Murdoch85b71792012-04-11 18:30:58 +01002192
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002193template <class T>
2194static Handle<T> MaybeNull(T* p) {
2195 if (p == NULL) return Handle<T>::null();
2196 return Handle<T>(p);
2197}
2198
2199
2200Handle<Map> Map::FindTransitionedMap(MapHandleList* candidates) {
2201 ElementsKind elms_kind = elements_kind();
2202 if (elms_kind == FAST_DOUBLE_ELEMENTS) {
2203 bool dummy = true;
2204 Handle<Map> fast_map =
2205 MaybeNull(LookupElementsTransitionMap(FAST_ELEMENTS, &dummy));
2206 if (!fast_map.is_null() && ContainsMap(candidates, fast_map)) {
2207 return fast_map;
Ben Murdoch85b71792012-04-11 18:30:58 +01002208 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002209 return Handle<Map>::null();
2210 }
2211 if (elms_kind == FAST_SMI_ONLY_ELEMENTS) {
2212 bool dummy = true;
2213 Handle<Map> double_map =
2214 MaybeNull(LookupElementsTransitionMap(FAST_DOUBLE_ELEMENTS, &dummy));
2215 // In the current implementation, if the DOUBLE map doesn't exist, the
2216 // FAST map can't exist either.
2217 if (double_map.is_null()) return Handle<Map>::null();
2218 Handle<Map> fast_map =
2219 MaybeNull(double_map->LookupElementsTransitionMap(FAST_ELEMENTS,
2220 &dummy));
2221 if (!fast_map.is_null() && ContainsMap(candidates, fast_map)) {
2222 return fast_map;
2223 }
2224 if (ContainsMap(candidates, double_map)) return double_map;
2225 }
2226 return Handle<Map>::null();
2227}
Ben Murdoch85b71792012-04-11 18:30:58 +01002228
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002229static Map* GetElementsTransitionMapFromDescriptor(Object* descriptor_contents,
2230 ElementsKind elements_kind) {
2231 if (descriptor_contents->IsMap()) {
2232 Map* map = Map::cast(descriptor_contents);
2233 if (map->elements_kind() == elements_kind) {
2234 return map;
2235 }
2236 return NULL;
2237 }
2238
2239 FixedArray* map_array = FixedArray::cast(descriptor_contents);
2240 for (int i = 0; i < map_array->length(); ++i) {
2241 Object* current = map_array->get(i);
2242 // Skip undefined slots, they are sentinels for reclaimed maps.
2243 if (!current->IsUndefined()) {
2244 Map* current_map = Map::cast(map_array->get(i));
2245 if (current_map->elements_kind() == elements_kind) {
2246 return current_map;
Ben Murdoch85b71792012-04-11 18:30:58 +01002247 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002248 }
2249 }
2250
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002251 return NULL;
2252}
2253
2254
2255static MaybeObject* AddElementsTransitionMapToDescriptor(
2256 Object* descriptor_contents,
2257 Map* new_map) {
2258 // Nothing was in the descriptor for an ELEMENTS_TRANSITION,
2259 // simply add the map.
2260 if (descriptor_contents == NULL) {
2261 return new_map;
2262 }
2263
2264 // There was already a map in the descriptor, create a 2-element FixedArray
2265 // to contain the existing map plus the new one.
2266 FixedArray* new_array;
2267 Heap* heap = new_map->GetHeap();
2268 if (descriptor_contents->IsMap()) {
2269 // Must tenure, DescriptorArray expects no new-space objects.
2270 MaybeObject* maybe_new_array = heap->AllocateFixedArray(2, TENURED);
2271 if (!maybe_new_array->To<FixedArray>(&new_array)) {
2272 return maybe_new_array;
2273 }
2274 new_array->set(0, descriptor_contents);
2275 new_array->set(1, new_map);
2276 return new_array;
2277 }
2278
2279 // The descriptor already contained a list of maps for different ElementKinds
2280 // of ELEMENTS_TRANSITION, first check the existing array for an undefined
2281 // slot, and if that's not available, create a FixedArray to hold the existing
2282 // maps plus the new one and fill it in.
2283 FixedArray* array = FixedArray::cast(descriptor_contents);
2284 for (int i = 0; i < array->length(); ++i) {
2285 if (array->get(i)->IsUndefined()) {
2286 array->set(i, new_map);
2287 return array;
2288 }
2289 }
2290
2291 // Must tenure, DescriptorArray expects no new-space objects.
2292 MaybeObject* maybe_new_array =
2293 heap->AllocateFixedArray(array->length() + 1, TENURED);
2294 if (!maybe_new_array->To<FixedArray>(&new_array)) {
2295 return maybe_new_array;
2296 }
2297 int i = 0;
2298 while (i < array->length()) {
2299 new_array->set(i, array->get(i));
2300 ++i;
2301 }
2302 new_array->set(i, new_map);
2303 return new_array;
2304}
2305
2306
2307String* Map::elements_transition_sentinel_name() {
2308 return GetHeap()->empty_symbol();
2309}
2310
2311
2312Object* Map::GetDescriptorContents(String* sentinel_name,
2313 bool* safe_to_add_transition) {
2314 // Get the cached index for the descriptors lookup, or find and cache it.
2315 DescriptorArray* descriptors = instance_descriptors();
2316 DescriptorLookupCache* cache = GetIsolate()->descriptor_lookup_cache();
2317 int index = cache->Lookup(descriptors, sentinel_name);
2318 if (index == DescriptorLookupCache::kAbsent) {
2319 index = descriptors->Search(sentinel_name);
2320 cache->Update(descriptors, sentinel_name, index);
2321 }
2322 // If the transition already exists, return its descriptor.
2323 if (index != DescriptorArray::kNotFound) {
2324 PropertyDetails details(descriptors->GetDetails(index));
2325 if (details.type() == ELEMENTS_TRANSITION) {
2326 return descriptors->GetValue(index);
2327 } else {
2328 if (safe_to_add_transition != NULL) {
2329 *safe_to_add_transition = false;
2330 }
2331 }
2332 }
2333 return NULL;
2334}
2335
2336
2337Map* Map::LookupElementsTransitionMap(ElementsKind elements_kind,
2338 bool* safe_to_add_transition) {
2339 // Special case: indirect SMI->FAST transition (cf. comment in
2340 // AddElementsTransition()).
2341 if (this->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2342 elements_kind == FAST_ELEMENTS) {
2343 Map* double_map = this->LookupElementsTransitionMap(FAST_DOUBLE_ELEMENTS,
2344 safe_to_add_transition);
2345 if (double_map == NULL) return double_map;
2346 return double_map->LookupElementsTransitionMap(FAST_ELEMENTS,
2347 safe_to_add_transition);
2348 }
2349 Object* descriptor_contents = GetDescriptorContents(
2350 elements_transition_sentinel_name(), safe_to_add_transition);
2351 if (descriptor_contents != NULL) {
2352 Map* maybe_transition_map =
2353 GetElementsTransitionMapFromDescriptor(descriptor_contents,
2354 elements_kind);
2355 ASSERT(maybe_transition_map == NULL || maybe_transition_map->IsMap());
2356 return maybe_transition_map;
2357 }
2358 return NULL;
2359}
2360
2361
2362MaybeObject* Map::AddElementsTransition(ElementsKind elements_kind,
2363 Map* transitioned_map) {
2364 // The map transition graph should be a tree, therefore the transition
2365 // from SMI to FAST elements is not done directly, but by going through
2366 // DOUBLE elements first.
2367 if (this->elements_kind() == FAST_SMI_ONLY_ELEMENTS &&
2368 elements_kind == FAST_ELEMENTS) {
2369 bool safe_to_add = true;
2370 Map* double_map = this->LookupElementsTransitionMap(
2371 FAST_DOUBLE_ELEMENTS, &safe_to_add);
2372 // This method is only called when safe_to_add_transition has been found
2373 // to be true earlier.
2374 ASSERT(safe_to_add);
2375
2376 if (double_map == NULL) {
2377 MaybeObject* maybe_map = this->CopyDropTransitions();
2378 if (!maybe_map->To(&double_map)) return maybe_map;
2379 double_map->set_elements_kind(FAST_DOUBLE_ELEMENTS);
2380 MaybeObject* maybe_double_transition = this->AddElementsTransition(
2381 FAST_DOUBLE_ELEMENTS, double_map);
2382 if (maybe_double_transition->IsFailure()) return maybe_double_transition;
2383 }
2384 return double_map->AddElementsTransition(FAST_ELEMENTS, transitioned_map);
2385 }
2386
2387 bool safe_to_add_transition = true;
2388 Object* descriptor_contents = GetDescriptorContents(
2389 elements_transition_sentinel_name(), &safe_to_add_transition);
2390 // This method is only called when safe_to_add_transition has been found
2391 // to be true earlier.
2392 ASSERT(safe_to_add_transition);
2393 MaybeObject* maybe_new_contents =
2394 AddElementsTransitionMapToDescriptor(descriptor_contents,
2395 transitioned_map);
2396 Object* new_contents;
2397 if (!maybe_new_contents->ToObject(&new_contents)) {
2398 return maybe_new_contents;
2399 }
2400
2401 ElementsTransitionDescriptor desc(elements_transition_sentinel_name(),
2402 new_contents);
2403 Object* new_descriptors;
2404 MaybeObject* maybe_new_descriptors =
2405 instance_descriptors()->CopyInsert(&desc, KEEP_TRANSITIONS);
2406 if (!maybe_new_descriptors->ToObject(&new_descriptors)) {
2407 return maybe_new_descriptors;
2408 }
2409 set_instance_descriptors(DescriptorArray::cast(new_descriptors));
2410 return this;
2411}
2412
2413
2414Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
2415 ElementsKind to_kind) {
2416 Isolate* isolate = object->GetIsolate();
2417 CALL_HEAP_FUNCTION(isolate,
2418 object->GetElementsTransitionMap(isolate, to_kind),
2419 Map);
2420}
2421
2422
2423MaybeObject* JSObject::GetElementsTransitionMapSlow(ElementsKind to_kind) {
2424 Map* current_map = map();
2425 ElementsKind from_kind = current_map->elements_kind();
2426
2427 if (from_kind == to_kind) return current_map;
2428
2429 // Only objects with FastProperties can have DescriptorArrays and can track
2430 // element-related maps. Also don't add descriptors to maps that are shared.
2431 bool safe_to_add_transition = HasFastProperties() &&
2432 !current_map->IsUndefined() &&
2433 !current_map->is_shared();
2434
2435 // Prevent long chains of DICTIONARY -> FAST_ELEMENTS maps caused by objects
2436 // with elements that switch back and forth between dictionary and fast
2437 // element mode.
2438 if (from_kind == DICTIONARY_ELEMENTS && to_kind == FAST_ELEMENTS) {
2439 safe_to_add_transition = false;
2440 }
2441
2442 if (safe_to_add_transition) {
2443 // It's only safe to manipulate the descriptor array if it would be
2444 // safe to add a transition.
2445 Map* maybe_transition_map = current_map->LookupElementsTransitionMap(
2446 to_kind, &safe_to_add_transition);
2447 if (maybe_transition_map != NULL) {
2448 return maybe_transition_map;
2449 }
2450 }
2451
2452 Map* new_map = NULL;
2453
Ben Murdoch589d6972011-11-30 16:04:58 +00002454 // No transition to an existing map for the given ElementsKind. Make a new
2455 // one.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002456 { MaybeObject* maybe_map = current_map->CopyDropTransitions();
2457 if (!maybe_map->To(&new_map)) return maybe_map;
Steve Block44f0eee2011-05-26 01:26:41 +01002458 }
Steve Block44f0eee2011-05-26 01:26:41 +01002459
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002460 new_map->set_elements_kind(to_kind);
Steve Block44f0eee2011-05-26 01:26:41 +01002461
2462 // Only remember the map transition if the object's map is NOT equal to the
2463 // global object_function's map and there is not an already existing
Ben Murdoch589d6972011-11-30 16:04:58 +00002464 // non-matching element transition.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002465 Context* global_context = GetIsolate()->context()->global_context();
2466 bool allow_map_transition = safe_to_add_transition &&
2467 (global_context->object_function()->map() != map());
Steve Block44f0eee2011-05-26 01:26:41 +01002468 if (allow_map_transition) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002469 MaybeObject* maybe_transition =
2470 current_map->AddElementsTransition(to_kind, new_map);
2471 if (maybe_transition->IsFailure()) return maybe_transition;
Steve Block44f0eee2011-05-26 01:26:41 +01002472 }
Steve Block44f0eee2011-05-26 01:26:41 +01002473 return new_map;
2474}
2475
2476
Steve Blocka7e24c12009-10-30 11:49:00 +00002477void JSObject::LocalLookupRealNamedProperty(String* name,
2478 LookupResult* result) {
2479 if (IsJSGlobalProxy()) {
2480 Object* proto = GetPrototype();
2481 if (proto->IsNull()) return result->NotFound();
2482 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002483 // A GlobalProxy's prototype should always be a proper JSObject.
Steve Blocka7e24c12009-10-30 11:49:00 +00002484 return JSObject::cast(proto)->LocalLookupRealNamedProperty(name, result);
2485 }
2486
2487 if (HasFastProperties()) {
2488 LookupInDescriptor(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002489 if (result->IsFound()) {
2490 // A property, a map transition or a null descriptor was found.
2491 // We return all of these result types because
2492 // LocalLookupRealNamedProperty is used when setting properties
2493 // where map transitions and null descriptors are handled.
Steve Blocka7e24c12009-10-30 11:49:00 +00002494 ASSERT(result->holder() == this && result->type() != NORMAL);
2495 // Disallow caching for uninitialized constants. These can only
2496 // occur as fields.
2497 if (result->IsReadOnly() && result->type() == FIELD &&
2498 FastPropertyAt(result->GetFieldIndex())->IsTheHole()) {
2499 result->DisallowCaching();
2500 }
2501 return;
2502 }
2503 } else {
2504 int entry = property_dictionary()->FindEntry(name);
2505 if (entry != StringDictionary::kNotFound) {
2506 Object* value = property_dictionary()->ValueAt(entry);
2507 if (IsGlobalObject()) {
2508 PropertyDetails d = property_dictionary()->DetailsAt(entry);
2509 if (d.IsDeleted()) {
2510 result->NotFound();
2511 return;
2512 }
2513 value = JSGlobalPropertyCell::cast(value)->value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002514 }
2515 // Make sure to disallow caching for uninitialized constants
2516 // found in the dictionary-mode objects.
2517 if (value->IsTheHole()) result->DisallowCaching();
2518 result->DictionaryResult(this, entry);
2519 return;
2520 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002521 }
2522 result->NotFound();
2523}
2524
2525
2526void JSObject::LookupRealNamedProperty(String* name, LookupResult* result) {
2527 LocalLookupRealNamedProperty(name, result);
2528 if (result->IsProperty()) return;
2529
2530 LookupRealNamedPropertyInPrototypes(name, result);
2531}
2532
2533
2534void JSObject::LookupRealNamedPropertyInPrototypes(String* name,
2535 LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01002536 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002537 for (Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01002538 pt != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002539 pt = JSObject::cast(pt)->GetPrototype()) {
2540 JSObject::cast(pt)->LocalLookupRealNamedProperty(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00002541 if (result->IsProperty() && (result->type() != INTERCEPTOR)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00002542 }
2543 result->NotFound();
2544}
2545
2546
2547// We only need to deal with CALLBACKS and INTERCEPTORS
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002548MaybeObject* JSObject::SetPropertyWithFailedAccessCheck(
2549 LookupResult* result,
2550 String* name,
2551 Object* value,
2552 bool check_prototype,
2553 StrictModeFlag strict_mode) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01002554 if (check_prototype && !result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002555 LookupCallbackSetterInPrototypes(name, result);
2556 }
2557
2558 if (result->IsProperty()) {
2559 if (!result->IsReadOnly()) {
2560 switch (result->type()) {
2561 case CALLBACKS: {
2562 Object* obj = result->GetCallbackObject();
2563 if (obj->IsAccessorInfo()) {
2564 AccessorInfo* info = AccessorInfo::cast(obj);
2565 if (info->all_can_write()) {
2566 return SetPropertyWithCallback(result->GetCallbackObject(),
2567 name,
2568 value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002569 result->holder(),
2570 strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002571 }
2572 }
2573 break;
2574 }
2575 case INTERCEPTOR: {
2576 // Try lookup real named properties. Note that only property can be
2577 // set is callbacks marked as ALL_CAN_WRITE on the prototype chain.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002578 LookupResult r(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00002579 LookupRealNamedProperty(name, &r);
2580 if (r.IsProperty()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002581 return SetPropertyWithFailedAccessCheck(&r,
2582 name,
2583 value,
2584 check_prototype,
2585 strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 }
2587 break;
2588 }
2589 default: {
2590 break;
2591 }
2592 }
2593 }
2594 }
2595
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002596 Isolate* isolate = GetIsolate();
2597 HandleScope scope(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002598 Handle<Object> value_handle(value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002599 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
Iain Merrick75681382010-08-19 15:07:18 +01002600 return *value_handle;
Steve Blocka7e24c12009-10-30 11:49:00 +00002601}
2602
2603
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002604MaybeObject* JSReceiver::SetProperty(LookupResult* result,
2605 String* key,
2606 Object* value,
2607 PropertyAttributes attributes,
2608 StrictModeFlag strict_mode) {
2609 if (result->IsFound() && result->type() == HANDLER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002610 return result->proxy()->SetPropertyWithHandler(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002611 key, value, attributes, strict_mode);
2612 } else {
2613 return JSObject::cast(this)->SetPropertyForResult(
2614 result, key, value, attributes, strict_mode);
2615 }
2616}
2617
2618
2619bool JSProxy::HasPropertyWithHandler(String* name_raw) {
2620 Isolate* isolate = GetIsolate();
2621 HandleScope scope(isolate);
2622 Handle<Object> receiver(this);
2623 Handle<Object> name(name_raw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002624
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002625 Handle<Object> args[] = { name };
2626 Handle<Object> result = CallTrap(
2627 "has", isolate->derived_has_trap(), ARRAY_SIZE(args), args);
Ben Murdoch589d6972011-11-30 16:04:58 +00002628 if (isolate->has_pending_exception()) return Failure::Exception();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002629
2630 return result->ToBoolean()->IsTrue();
2631}
2632
2633
2634MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyWithHandler(
2635 String* name_raw,
2636 Object* value_raw,
2637 PropertyAttributes attributes,
2638 StrictModeFlag strict_mode) {
2639 Isolate* isolate = GetIsolate();
2640 HandleScope scope(isolate);
2641 Handle<Object> receiver(this);
2642 Handle<Object> name(name_raw);
2643 Handle<Object> value(value_raw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002644
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002645 Handle<Object> args[] = { receiver, name, value };
2646 CallTrap("set", isolate->derived_set_trap(), ARRAY_SIZE(args), args);
Ben Murdoch589d6972011-11-30 16:04:58 +00002647 if (isolate->has_pending_exception()) return Failure::Exception();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002648
2649 return *value;
2650}
2651
2652
2653MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyWithHandlerIfDefiningSetter(
2654 String* name_raw,
2655 Object* value_raw,
2656 PropertyAttributes attributes,
2657 StrictModeFlag strict_mode,
2658 bool* found) {
2659 *found = true; // except where defined otherwise...
2660 Isolate* isolate = GetHeap()->isolate();
2661 Handle<JSProxy> proxy(this);
2662 Handle<Object> handler(this->handler()); // Trap might morph proxy.
2663 Handle<String> name(name_raw);
2664 Handle<Object> value(value_raw);
2665 Handle<Object> args[] = { name };
2666 Handle<Object> result = proxy->CallTrap(
2667 "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args);
2668 if (isolate->has_pending_exception()) return Failure::Exception();
2669
2670 if (!result->IsUndefined()) {
2671 // The proxy handler cares about this property.
2672 // Check whether it is virtualized as an accessor.
2673 // Emulate [[GetProperty]] semantics for proxies.
2674 bool has_pending_exception;
2675 Handle<Object> argv[] = { result };
2676 Handle<Object> desc =
2677 Execution::Call(isolate->to_complete_property_descriptor(), result,
2678 ARRAY_SIZE(argv), argv, &has_pending_exception);
2679 if (has_pending_exception) return Failure::Exception();
2680
2681 Handle<String> conf_name =
2682 isolate->factory()->LookupAsciiSymbol("configurable_");
2683 Handle<Object> configurable(v8::internal::GetProperty(desc, conf_name));
2684 ASSERT(!isolate->has_pending_exception());
2685 if (configurable->IsFalse()) {
2686 Handle<String> trap =
2687 isolate->factory()->LookupAsciiSymbol("getPropertyDescriptor");
2688 Handle<Object> args[] = { handler, trap, name };
2689 Handle<Object> error = isolate->factory()->NewTypeError(
2690 "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args)));
2691 return isolate->Throw(*error);
2692 }
2693 ASSERT(configurable->IsTrue());
2694
2695 // Check for AccessorDescriptor.
2696 Handle<String> set_name = isolate->factory()->LookupAsciiSymbol("set_");
2697 Handle<Object> setter(v8::internal::GetProperty(desc, set_name));
2698 ASSERT(!isolate->has_pending_exception());
2699 if (!setter->IsUndefined()) {
2700 // We have a setter -- invoke it.
2701 // TODO(rossberg): nicer would be to cast to some JSCallable here...
2702 return proxy->SetPropertyWithDefinedSetter(
2703 JSReceiver::cast(*setter), *value);
2704 } else {
2705 Handle<String> get_name = isolate->factory()->LookupAsciiSymbol("get_");
2706 Handle<Object> getter(v8::internal::GetProperty(desc, get_name));
2707 ASSERT(!isolate->has_pending_exception());
2708 if (!getter->IsUndefined()) {
2709 // We have a getter but no setter -- the property may not be
2710 // written. In strict mode, throw an error.
2711 if (strict_mode == kNonStrictMode) return *value;
2712 Handle<Object> args[] = { name, proxy };
2713 Handle<Object> error = isolate->factory()->NewTypeError(
2714 "no_setter_in_callback", HandleVector(args, ARRAY_SIZE(args)));
2715 return isolate->Throw(*error);
2716 }
2717 }
2718 // Fall-through.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002719 }
2720
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002721 // The proxy does not define the property as an accessor.
2722 *found = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002723 return *value;
2724}
2725
2726
2727MUST_USE_RESULT MaybeObject* JSProxy::DeletePropertyWithHandler(
2728 String* name_raw, DeleteMode mode) {
2729 Isolate* isolate = GetIsolate();
2730 HandleScope scope(isolate);
2731 Handle<Object> receiver(this);
2732 Handle<Object> name(name_raw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002733
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002734 Handle<Object> args[] = { name };
2735 Handle<Object> result = CallTrap(
2736 "delete", Handle<Object>(), ARRAY_SIZE(args), args);
Ben Murdoch589d6972011-11-30 16:04:58 +00002737 if (isolate->has_pending_exception()) return Failure::Exception();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002738
2739 Object* bool_result = result->ToBoolean();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002740 if (mode == STRICT_DELETION && bool_result == GetHeap()->false_value()) {
2741 Handle<String> trap_name = isolate->factory()->LookupAsciiSymbol("delete");
2742 Handle<Object> args[] = { Handle<Object>(handler()), trap_name };
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002743 Handle<Object> error = isolate->factory()->NewTypeError(
2744 "handler_failed", HandleVector(args, ARRAY_SIZE(args)));
2745 isolate->Throw(*error);
2746 return Failure::Exception();
2747 }
2748 return bool_result;
2749}
2750
2751
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002752MUST_USE_RESULT MaybeObject* JSProxy::DeleteElementWithHandler(
2753 uint32_t index,
2754 DeleteMode mode) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002755 Isolate* isolate = GetIsolate();
2756 HandleScope scope(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002757 Handle<String> name = isolate->factory()->Uint32ToString(index);
2758 return JSProxy::DeletePropertyWithHandler(*name, mode);
2759}
2760
2761
2762MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler(
2763 JSReceiver* receiver_raw,
2764 String* name_raw) {
2765 Isolate* isolate = GetIsolate();
2766 HandleScope scope(isolate);
2767 Handle<JSProxy> proxy(this);
2768 Handle<Object> handler(this->handler()); // Trap might morph proxy.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002769 Handle<JSReceiver> receiver(receiver_raw);
2770 Handle<Object> name(name_raw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002771
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002772 Handle<Object> args[] = { name };
2773 Handle<Object> result = CallTrap(
2774 "getPropertyDescriptor", Handle<Object>(), ARRAY_SIZE(args), args);
Ben Murdoch589d6972011-11-30 16:04:58 +00002775 if (isolate->has_pending_exception()) return NONE;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002776
2777 if (result->IsUndefined()) return ABSENT;
2778
2779 bool has_pending_exception;
2780 Handle<Object> argv[] = { result };
2781 Handle<Object> desc =
2782 Execution::Call(isolate->to_complete_property_descriptor(), result,
2783 ARRAY_SIZE(argv), argv, &has_pending_exception);
2784 if (has_pending_exception) return NONE;
2785
2786 // Convert result to PropertyAttributes.
2787 Handle<String> enum_n = isolate->factory()->LookupAsciiSymbol("enumerable");
2788 Handle<Object> enumerable(v8::internal::GetProperty(desc, enum_n));
2789 if (isolate->has_pending_exception()) return NONE;
2790 Handle<String> conf_n = isolate->factory()->LookupAsciiSymbol("configurable");
2791 Handle<Object> configurable(v8::internal::GetProperty(desc, conf_n));
2792 if (isolate->has_pending_exception()) return NONE;
2793 Handle<String> writ_n = isolate->factory()->LookupAsciiSymbol("writable");
2794 Handle<Object> writable(v8::internal::GetProperty(desc, writ_n));
2795 if (isolate->has_pending_exception()) return NONE;
2796
2797 if (configurable->IsFalse()) {
2798 Handle<String> trap =
2799 isolate->factory()->LookupAsciiSymbol("getPropertyDescriptor");
2800 Handle<Object> args[] = { handler, trap, name };
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002801 Handle<Object> error = isolate->factory()->NewTypeError(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002802 "proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002803 isolate->Throw(*error);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002804 return NONE;
2805 }
2806
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002807 int attributes = NONE;
2808 if (enumerable->ToBoolean()->IsFalse()) attributes |= DONT_ENUM;
2809 if (configurable->ToBoolean()->IsFalse()) attributes |= DONT_DELETE;
2810 if (writable->ToBoolean()->IsFalse()) attributes |= READ_ONLY;
2811 return static_cast<PropertyAttributes>(attributes);
2812}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002813
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002814
2815MUST_USE_RESULT PropertyAttributes JSProxy::GetElementAttributeWithHandler(
2816 JSReceiver* receiver,
2817 uint32_t index) {
2818 Isolate* isolate = GetIsolate();
2819 HandleScope scope(isolate);
2820 Handle<String> name = isolate->factory()->Uint32ToString(index);
2821 return GetPropertyAttributeWithHandler(receiver, *name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002822}
2823
2824
2825void JSProxy::Fix() {
2826 Isolate* isolate = GetIsolate();
2827 HandleScope scope(isolate);
2828 Handle<JSProxy> self(this);
2829
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002830 // Save identity hash.
2831 MaybeObject* maybe_hash = GetIdentityHash(OMIT_CREATION);
2832
Ben Murdoch589d6972011-11-30 16:04:58 +00002833 if (IsJSFunctionProxy()) {
2834 isolate->factory()->BecomeJSFunction(self);
2835 // Code will be set on the JavaScript side.
2836 } else {
2837 isolate->factory()->BecomeJSObject(self);
2838 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002839 ASSERT(self->IsJSObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002840
2841 // Inherit identity, if it was present.
2842 Object* hash;
2843 if (maybe_hash->To<Object>(&hash) && hash->IsSmi()) {
2844 Handle<JSObject> new_self(JSObject::cast(*self));
2845 isolate->factory()->SetIdentityHash(new_self, hash);
2846 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002847}
2848
2849
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002850MUST_USE_RESULT Handle<Object> JSProxy::CallTrap(const char* name,
2851 Handle<Object> derived,
2852 int argc,
2853 Handle<Object> argv[]) {
2854 Isolate* isolate = GetIsolate();
2855 Handle<Object> handler(this->handler());
2856
2857 Handle<String> trap_name = isolate->factory()->LookupAsciiSymbol(name);
2858 Handle<Object> trap(v8::internal::GetProperty(handler, trap_name));
2859 if (isolate->has_pending_exception()) return trap;
2860
2861 if (trap->IsUndefined()) {
2862 if (derived.is_null()) {
2863 Handle<Object> args[] = { handler, trap_name };
2864 Handle<Object> error = isolate->factory()->NewTypeError(
2865 "handler_trap_missing", HandleVector(args, ARRAY_SIZE(args)));
2866 isolate->Throw(*error);
2867 return Handle<Object>();
2868 }
2869 trap = Handle<Object>(derived);
2870 }
2871
2872 bool threw;
2873 return Execution::Call(trap, handler, argc, argv, &threw);
2874}
2875
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002876
2877MaybeObject* JSObject::SetPropertyForResult(LookupResult* result,
2878 String* name,
2879 Object* value,
2880 PropertyAttributes attributes,
2881 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01002882 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00002883 // Make sure that the top context does not change when doing callbacks or
2884 // interceptor calls.
2885 AssertNoContextChange ncc;
2886
Steve Blockd0582a62009-12-15 09:54:21 +00002887 // Optimization for 2-byte strings often used as keys in a decompression
2888 // dictionary. We make these short keys into symbols to avoid constantly
2889 // reallocating them.
2890 if (!name->IsSymbol() && name->length() <= 2) {
John Reck59135872010-11-02 12:39:01 -07002891 Object* symbol_version;
Steve Block44f0eee2011-05-26 01:26:41 +01002892 { MaybeObject* maybe_symbol_version = heap->LookupSymbol(name);
John Reck59135872010-11-02 12:39:01 -07002893 if (maybe_symbol_version->ToObject(&symbol_version)) {
2894 name = String::cast(symbol_version);
2895 }
2896 }
Steve Blockd0582a62009-12-15 09:54:21 +00002897 }
2898
Steve Blocka7e24c12009-10-30 11:49:00 +00002899 // Check access rights if needed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002900 if (IsAccessCheckNeeded()) {
2901 if (!heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_SET)) {
2902 return SetPropertyWithFailedAccessCheck(
2903 result, name, value, true, strict_mode);
2904 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002905 }
2906
2907 if (IsJSGlobalProxy()) {
2908 Object* proto = GetPrototype();
2909 if (proto->IsNull()) return value;
2910 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002911 return JSObject::cast(proto)->SetPropertyForResult(
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002912 result, name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002913 }
2914
2915 if (!result->IsProperty() && !IsJSContextExtensionObject()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002916 bool found = false;
2917 MaybeObject* result_object;
2918 result_object = SetPropertyWithCallbackSetterInPrototypes(name,
2919 value,
2920 attributes,
2921 &found,
2922 strict_mode);
2923 if (found) return result_object;
Steve Blocka7e24c12009-10-30 11:49:00 +00002924 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002925
2926 // At this point, no GC should have happened, as this would invalidate
2927 // 'result', which we cannot handlify!
2928
Andrei Popescu402d9372010-02-26 13:31:12 +00002929 if (!result->IsFound()) {
2930 // Neither properties nor transitions found.
Steve Block44f0eee2011-05-26 01:26:41 +01002931 return AddProperty(name, value, attributes, strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002932 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002933 if (result->IsReadOnly() && result->IsProperty()) {
2934 if (strict_mode == kStrictMode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002935 Handle<JSObject> self(this);
2936 Handle<String> hname(name);
2937 Handle<Object> args[] = { hname, self };
Steve Block44f0eee2011-05-26 01:26:41 +01002938 return heap->isolate()->Throw(*heap->isolate()->factory()->NewTypeError(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002939 "strict_read_only_property", HandleVector(args, ARRAY_SIZE(args))));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002940 } else {
2941 return value;
2942 }
2943 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002944 // This is a real property that is not read-only, or it is a
2945 // transition or null descriptor and there are no setters in the prototypes.
2946 switch (result->type()) {
2947 case NORMAL:
2948 return SetNormalizedProperty(result, value);
2949 case FIELD:
2950 return FastPropertyAtPut(result->GetFieldIndex(), value);
2951 case MAP_TRANSITION:
2952 if (attributes == result->GetAttributes()) {
2953 // Only use map transition if the attributes match.
2954 return AddFastPropertyUsingMap(result->GetTransitionMap(),
2955 name,
2956 value);
2957 }
2958 return ConvertDescriptorToField(name, value, attributes);
2959 case CONSTANT_FUNCTION:
2960 // Only replace the function if necessary.
2961 if (value == result->GetConstantFunction()) return value;
2962 // Preserve the attributes of this existing property.
2963 attributes = result->GetAttributes();
2964 return ConvertDescriptorToField(name, value, attributes);
2965 case CALLBACKS:
2966 return SetPropertyWithCallback(result->GetCallbackObject(),
2967 name,
2968 value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002969 result->holder(),
2970 strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002971 case INTERCEPTOR:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002972 return SetPropertyWithInterceptor(name, value, attributes, strict_mode);
Iain Merrick75681382010-08-19 15:07:18 +01002973 case CONSTANT_TRANSITION: {
2974 // If the same constant function is being added we can simply
2975 // transition to the target map.
2976 Map* target_map = result->GetTransitionMap();
2977 DescriptorArray* target_descriptors = target_map->instance_descriptors();
2978 int number = target_descriptors->SearchWithCache(name);
2979 ASSERT(number != DescriptorArray::kNotFound);
2980 ASSERT(target_descriptors->GetType(number) == CONSTANT_FUNCTION);
2981 JSFunction* function =
2982 JSFunction::cast(target_descriptors->GetValue(number));
Iain Merrick75681382010-08-19 15:07:18 +01002983 if (value == function) {
2984 set_map(target_map);
2985 return value;
2986 }
2987 // Otherwise, replace with a MAP_TRANSITION to a new map with a
2988 // FIELD, even if the value is a constant function.
Steve Blocka7e24c12009-10-30 11:49:00 +00002989 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
Iain Merrick75681382010-08-19 15:07:18 +01002990 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002991 case NULL_DESCRIPTOR:
Ben Murdoch589d6972011-11-30 16:04:58 +00002992 case ELEMENTS_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00002993 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002994 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00002995 UNREACHABLE();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002996 return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00002997 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002998 UNREACHABLE(); // keep the compiler happy
Steve Blocka7e24c12009-10-30 11:49:00 +00002999 return value;
3000}
3001
3002
3003// Set a real local property, even if it is READ_ONLY. If the property is not
3004// present, add it with attributes NONE. This code is an exact clone of
3005// SetProperty, with the check for IsReadOnly and the check for a
3006// callback setter removed. The two lines looking up the LookupResult
3007// result are also added. If one of the functions is changed, the other
3008// should be.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003009// Note that this method cannot be used to set the prototype of a function
3010// because ConvertDescriptorToField() which is called in "case CALLBACKS:"
3011// doesn't handle function prototypes correctly.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003012Handle<Object> JSObject::SetLocalPropertyIgnoreAttributes(
3013 Handle<JSObject> object,
3014 Handle<String> key,
3015 Handle<Object> value,
3016 PropertyAttributes attributes) {
3017 CALL_HEAP_FUNCTION(
3018 object->GetIsolate(),
3019 object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes),
3020 Object);
3021}
3022
3023
Ben Murdoch086aeea2011-05-13 15:57:08 +01003024MaybeObject* JSObject::SetLocalPropertyIgnoreAttributes(
Steve Blocka7e24c12009-10-30 11:49:00 +00003025 String* name,
3026 Object* value,
3027 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01003028
Steve Blocka7e24c12009-10-30 11:49:00 +00003029 // Make sure that the top context does not change when doing callbacks or
3030 // interceptor calls.
3031 AssertNoContextChange ncc;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003032 Isolate* isolate = GetIsolate();
3033 LookupResult result(isolate);
Andrei Popescu402d9372010-02-26 13:31:12 +00003034 LocalLookup(name, &result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003035 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003036 if (IsAccessCheckNeeded()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003037 if (!isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003038 return SetPropertyWithFailedAccessCheck(&result,
3039 name,
3040 value,
3041 false,
3042 kNonStrictMode);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003043 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003044 }
3045
3046 if (IsJSGlobalProxy()) {
3047 Object* proto = GetPrototype();
3048 if (proto->IsNull()) return value;
3049 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch086aeea2011-05-13 15:57:08 +01003050 return JSObject::cast(proto)->SetLocalPropertyIgnoreAttributes(
Steve Blocka7e24c12009-10-30 11:49:00 +00003051 name,
3052 value,
3053 attributes);
3054 }
3055
3056 // Check for accessor in prototype chain removed here in clone.
Andrei Popescu402d9372010-02-26 13:31:12 +00003057 if (!result.IsFound()) {
3058 // Neither properties nor transitions found.
Steve Block44f0eee2011-05-26 01:26:41 +01003059 return AddProperty(name, value, attributes, kNonStrictMode);
Steve Blocka7e24c12009-10-30 11:49:00 +00003060 }
Steve Block6ded16b2010-05-10 14:33:55 +01003061
Andrei Popescu402d9372010-02-26 13:31:12 +00003062 PropertyDetails details = PropertyDetails(attributes, NORMAL);
3063
Steve Blocka7e24c12009-10-30 11:49:00 +00003064 // Check of IsReadOnly removed from here in clone.
Andrei Popescu402d9372010-02-26 13:31:12 +00003065 switch (result.type()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003066 case NORMAL:
Andrei Popescu402d9372010-02-26 13:31:12 +00003067 return SetNormalizedProperty(name, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +00003068 case FIELD:
Andrei Popescu402d9372010-02-26 13:31:12 +00003069 return FastPropertyAtPut(result.GetFieldIndex(), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003070 case MAP_TRANSITION:
Andrei Popescu402d9372010-02-26 13:31:12 +00003071 if (attributes == result.GetAttributes()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003072 // Only use map transition if the attributes match.
Andrei Popescu402d9372010-02-26 13:31:12 +00003073 return AddFastPropertyUsingMap(result.GetTransitionMap(),
Steve Blocka7e24c12009-10-30 11:49:00 +00003074 name,
3075 value);
3076 }
3077 return ConvertDescriptorToField(name, value, attributes);
3078 case CONSTANT_FUNCTION:
3079 // Only replace the function if necessary.
Andrei Popescu402d9372010-02-26 13:31:12 +00003080 if (value == result.GetConstantFunction()) return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00003081 // Preserve the attributes of this existing property.
Andrei Popescu402d9372010-02-26 13:31:12 +00003082 attributes = result.GetAttributes();
Steve Blocka7e24c12009-10-30 11:49:00 +00003083 return ConvertDescriptorToField(name, value, attributes);
3084 case CALLBACKS:
3085 case INTERCEPTOR:
3086 // Override callback in clone
3087 return ConvertDescriptorToField(name, value, attributes);
3088 case CONSTANT_TRANSITION:
3089 // Replace with a MAP_TRANSITION to a new map with a FIELD, even
3090 // if the value is a function.
3091 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
3092 case NULL_DESCRIPTOR:
Ben Murdoch589d6972011-11-30 16:04:58 +00003093 case ELEMENTS_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00003094 return ConvertDescriptorToFieldAndMapTransition(name, value, attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003095 case HANDLER:
Steve Blocka7e24c12009-10-30 11:49:00 +00003096 UNREACHABLE();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003097 return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00003098 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003099 UNREACHABLE(); // keep the compiler happy
Steve Blocka7e24c12009-10-30 11:49:00 +00003100 return value;
3101}
3102
3103
3104PropertyAttributes JSObject::GetPropertyAttributePostInterceptor(
3105 JSObject* receiver,
3106 String* name,
3107 bool continue_search) {
3108 // Check local property, ignore interceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003109 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003110 LocalLookupRealNamedProperty(name, &result);
3111 if (result.IsProperty()) return result.GetAttributes();
3112
3113 if (continue_search) {
3114 // Continue searching via the prototype chain.
3115 Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01003116 if (!pt->IsNull()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003117 return JSObject::cast(pt)->
3118 GetPropertyAttributeWithReceiver(receiver, name);
3119 }
3120 }
3121 return ABSENT;
3122}
3123
3124
3125PropertyAttributes JSObject::GetPropertyAttributeWithInterceptor(
3126 JSObject* receiver,
3127 String* name,
3128 bool continue_search) {
Steve Block44f0eee2011-05-26 01:26:41 +01003129 Isolate* isolate = GetIsolate();
3130
Steve Blocka7e24c12009-10-30 11:49:00 +00003131 // Make sure that the top context does not change when doing
3132 // callbacks or interceptor calls.
3133 AssertNoContextChange ncc;
3134
Steve Block44f0eee2011-05-26 01:26:41 +01003135 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003136 Handle<InterceptorInfo> interceptor(GetNamedInterceptor());
3137 Handle<JSObject> receiver_handle(receiver);
3138 Handle<JSObject> holder_handle(this);
3139 Handle<String> name_handle(name);
Steve Block44f0eee2011-05-26 01:26:41 +01003140 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003141 v8::AccessorInfo info(args.end());
3142 if (!interceptor->query()->IsUndefined()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003143 v8::NamedPropertyQuery query =
3144 v8::ToCData<v8::NamedPropertyQuery>(interceptor->query());
Steve Block44f0eee2011-05-26 01:26:41 +01003145 LOG(isolate,
3146 ApiNamedPropertyAccess("interceptor-named-has", *holder_handle, name));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003147 v8::Handle<v8::Integer> result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003148 {
3149 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01003150 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003151 result = query(v8::Utils::ToLocal(name_handle), info);
3152 }
3153 if (!result.IsEmpty()) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003154 ASSERT(result->IsInt32());
3155 return static_cast<PropertyAttributes>(result->Int32Value());
Steve Blocka7e24c12009-10-30 11:49:00 +00003156 }
3157 } else if (!interceptor->getter()->IsUndefined()) {
3158 v8::NamedPropertyGetter getter =
3159 v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01003160 LOG(isolate,
3161 ApiNamedPropertyAccess("interceptor-named-get-has", this, name));
Steve Blocka7e24c12009-10-30 11:49:00 +00003162 v8::Handle<v8::Value> result;
3163 {
3164 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01003165 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003166 result = getter(v8::Utils::ToLocal(name_handle), info);
3167 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003168 if (!result.IsEmpty()) return DONT_ENUM;
Steve Blocka7e24c12009-10-30 11:49:00 +00003169 }
3170 return holder_handle->GetPropertyAttributePostInterceptor(*receiver_handle,
3171 *name_handle,
3172 continue_search);
3173}
3174
3175
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003176PropertyAttributes JSReceiver::GetPropertyAttributeWithReceiver(
3177 JSReceiver* receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +00003178 String* key) {
3179 uint32_t index = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003180 if (IsJSObject() && key->AsArrayIndex(&index)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003181 return JSObject::cast(this)->HasElementWithReceiver(receiver, index)
3182 ? NONE : ABSENT;
Steve Blocka7e24c12009-10-30 11:49:00 +00003183 }
3184 // Named property.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003185 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003186 Lookup(key, &result);
3187 return GetPropertyAttribute(receiver, &result, key, true);
3188}
3189
3190
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003191PropertyAttributes JSReceiver::GetPropertyAttribute(JSReceiver* receiver,
3192 LookupResult* result,
3193 String* name,
3194 bool continue_search) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003195 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003196 if (IsAccessCheckNeeded()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003197 JSObject* this_obj = JSObject::cast(this);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003198 Heap* heap = GetHeap();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003199 if (!heap->isolate()->MayNamedAccess(this_obj, name, v8::ACCESS_HAS)) {
3200 return this_obj->GetPropertyAttributeWithFailedAccessCheck(
3201 receiver, result, name, continue_search);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003202 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003203 }
Andrei Popescu402d9372010-02-26 13:31:12 +00003204 if (result->IsProperty()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003205 switch (result->type()) {
3206 case NORMAL: // fall through
3207 case FIELD:
3208 case CONSTANT_FUNCTION:
3209 case CALLBACKS:
3210 return result->GetAttributes();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003211 case HANDLER: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003212 return JSProxy::cast(result->proxy())->GetPropertyAttributeWithHandler(
3213 receiver, name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003214 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003215 case INTERCEPTOR:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003216 return result->holder()->GetPropertyAttributeWithInterceptor(
3217 JSObject::cast(receiver), name, continue_search);
Steve Blocka7e24c12009-10-30 11:49:00 +00003218 default:
3219 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00003220 }
3221 }
3222 return ABSENT;
3223}
3224
3225
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003226PropertyAttributes JSReceiver::GetLocalPropertyAttribute(String* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003227 // Check whether the name is an array index.
3228 uint32_t index = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003229 if (IsJSObject() && name->AsArrayIndex(&index)) {
3230 if (JSObject::cast(this)->HasLocalElement(index)) return NONE;
Steve Blocka7e24c12009-10-30 11:49:00 +00003231 return ABSENT;
3232 }
3233 // Named property.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003234 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003235 LocalLookup(name, &result);
3236 return GetPropertyAttribute(this, &result, name, false);
3237}
3238
3239
John Reck59135872010-11-02 12:39:01 -07003240MaybeObject* NormalizedMapCache::Get(JSObject* obj,
3241 PropertyNormalizationMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01003242 Isolate* isolate = obj->GetIsolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003243 Map* fast = obj->map();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003244 int index = fast->Hash() % kEntries;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003245 Object* result = get(index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003246 if (result->IsMap() &&
3247 Map::cast(result)->EquivalentToForNormalization(fast, mode)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003248#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003249 if (FLAG_verify_heap) {
3250 Map::cast(result)->SharedMapVerify();
3251 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003252 if (FLAG_enable_slow_asserts) {
3253 // The cached map should match newly created normalized map bit-by-bit.
John Reck59135872010-11-02 12:39:01 -07003254 Object* fresh;
3255 { MaybeObject* maybe_fresh =
3256 fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP);
3257 if (maybe_fresh->ToObject(&fresh)) {
3258 ASSERT(memcmp(Map::cast(fresh)->address(),
3259 Map::cast(result)->address(),
3260 Map::kSize) == 0);
3261 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003262 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003263 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003264#endif
3265 return result;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003266 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003267
John Reck59135872010-11-02 12:39:01 -07003268 { MaybeObject* maybe_result =
3269 fast->CopyNormalized(mode, SHARED_NORMALIZED_MAP);
3270 if (!maybe_result->ToObject(&result)) return maybe_result;
3271 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003272 set(index, result);
Steve Block44f0eee2011-05-26 01:26:41 +01003273 isolate->counters()->normalized_maps()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003274
3275 return result;
3276}
3277
3278
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003279void NormalizedMapCache::Clear() {
3280 int entries = length();
3281 for (int i = 0; i != entries; i++) {
3282 set_undefined(i);
3283 }
3284}
3285
3286
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003287void JSObject::UpdateMapCodeCache(Handle<JSObject> object,
3288 Handle<String> name,
3289 Handle<Code> code) {
3290 Isolate* isolate = object->GetIsolate();
3291 CALL_HEAP_FUNCTION_VOID(isolate,
3292 object->UpdateMapCodeCache(*name, *code));
3293}
3294
3295
John Reck59135872010-11-02 12:39:01 -07003296MaybeObject* JSObject::UpdateMapCodeCache(String* name, Code* code) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003297 if (map()->is_shared()) {
3298 // Fast case maps are never marked as shared.
3299 ASSERT(!HasFastProperties());
3300 // Replace the map with an identical copy that can be safely modified.
John Reck59135872010-11-02 12:39:01 -07003301 Object* obj;
3302 { MaybeObject* maybe_obj = map()->CopyNormalized(KEEP_INOBJECT_PROPERTIES,
3303 UNIQUE_NORMALIZED_MAP);
3304 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3305 }
Steve Block44f0eee2011-05-26 01:26:41 +01003306 GetIsolate()->counters()->normalized_maps()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003307
3308 set_map(Map::cast(obj));
3309 }
3310 return map()->UpdateCodeCache(name, code);
3311}
3312
3313
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003314void JSObject::NormalizeProperties(Handle<JSObject> object,
3315 PropertyNormalizationMode mode,
3316 int expected_additional_properties) {
3317 CALL_HEAP_FUNCTION_VOID(object->GetIsolate(),
3318 object->NormalizeProperties(
3319 mode, expected_additional_properties));
3320}
3321
3322
John Reck59135872010-11-02 12:39:01 -07003323MaybeObject* JSObject::NormalizeProperties(PropertyNormalizationMode mode,
3324 int expected_additional_properties) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003325 if (!HasFastProperties()) return this;
3326
3327 // The global object is always normalized.
3328 ASSERT(!IsGlobalObject());
Steve Block1e0659c2011-05-24 12:43:12 +01003329 // JSGlobalProxy must never be normalized
3330 ASSERT(!IsJSGlobalProxy());
3331
Ben Murdoch8b112d22011-06-08 16:22:53 +01003332 Map* map_of_this = map();
Steve Block44f0eee2011-05-26 01:26:41 +01003333
Steve Blocka7e24c12009-10-30 11:49:00 +00003334 // Allocate new content.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003335 int property_count = map_of_this->NumberOfDescribedProperties();
Steve Blocka7e24c12009-10-30 11:49:00 +00003336 if (expected_additional_properties > 0) {
3337 property_count += expected_additional_properties;
3338 } else {
3339 property_count += 2; // Make space for two more properties.
3340 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003341 StringDictionary* dictionary;
3342 { MaybeObject* maybe_dictionary = StringDictionary::Allocate(property_count);
3343 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
John Reck59135872010-11-02 12:39:01 -07003344 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003345
Ben Murdoch8b112d22011-06-08 16:22:53 +01003346 DescriptorArray* descs = map_of_this->instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00003347 for (int i = 0; i < descs->number_of_descriptors(); i++) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003348 PropertyDetails details(descs->GetDetails(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00003349 switch (details.type()) {
3350 case CONSTANT_FUNCTION: {
3351 PropertyDetails d =
3352 PropertyDetails(details.attributes(), NORMAL, details.index());
3353 Object* value = descs->GetConstantFunction(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003354 MaybeObject* maybe_dictionary =
3355 dictionary->Add(descs->GetKey(i), value, d);
3356 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +00003357 break;
3358 }
3359 case FIELD: {
3360 PropertyDetails d =
3361 PropertyDetails(details.attributes(), NORMAL, details.index());
3362 Object* value = FastPropertyAt(descs->GetFieldIndex(i));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003363 MaybeObject* maybe_dictionary =
3364 dictionary->Add(descs->GetKey(i), value, d);
3365 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +00003366 break;
3367 }
3368 case CALLBACKS: {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003369 if (!descs->IsProperty(i)) break;
Steve Blocka7e24c12009-10-30 11:49:00 +00003370 Object* value = descs->GetCallbacksObject(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003371 if (value->IsAccessorPair()) {
3372 MaybeObject* maybe_copy =
3373 AccessorPair::cast(value)->CopyWithoutTransitions();
3374 if (!maybe_copy->To(&value)) return maybe_copy;
John Reck59135872010-11-02 12:39:01 -07003375 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003376 MaybeObject* maybe_dictionary =
3377 dictionary->Add(descs->GetKey(i), value, details);
3378 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +00003379 break;
3380 }
3381 case MAP_TRANSITION:
3382 case CONSTANT_TRANSITION:
3383 case NULL_DESCRIPTOR:
3384 case INTERCEPTOR:
Ben Murdoch589d6972011-11-30 16:04:58 +00003385 case ELEMENTS_TRANSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +00003386 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003387 case HANDLER:
3388 case NORMAL:
Steve Blocka7e24c12009-10-30 11:49:00 +00003389 UNREACHABLE();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003390 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00003391 }
3392 }
3393
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003394 Heap* current_heap = GetHeap();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003395
Steve Blocka7e24c12009-10-30 11:49:00 +00003396 // Copy the next enumeration index from instance descriptor.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003397 int index = map_of_this->instance_descriptors()->NextEnumerationIndex();
Steve Blocka7e24c12009-10-30 11:49:00 +00003398 dictionary->SetNextEnumerationIndex(index);
3399
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003400 Map* new_map;
3401 { MaybeObject* maybe_map =
Ben Murdoch8b112d22011-06-08 16:22:53 +01003402 current_heap->isolate()->context()->global_context()->
Steve Block44f0eee2011-05-26 01:26:41 +01003403 normalized_map_cache()->Get(this, mode);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003404 if (!maybe_map->To(&new_map)) return maybe_map;
John Reck59135872010-11-02 12:39:01 -07003405 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003406
Steve Blocka7e24c12009-10-30 11:49:00 +00003407 // We have now successfully allocated all the necessary objects.
3408 // Changes can now be made with the guarantee that all of them take effect.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003409
3410 // Resize the object in the heap if necessary.
3411 int new_instance_size = new_map->instance_size();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003412 int instance_size_delta = map_of_this->instance_size() - new_instance_size;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003413 ASSERT(instance_size_delta >= 0);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003414 current_heap->CreateFillerObjectAt(this->address() + new_instance_size,
3415 instance_size_delta);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003416 if (Marking::IsBlack(Marking::MarkBitFrom(this))) {
3417 MemoryChunk::IncrementLiveBytesFromMutator(this->address(),
3418 -instance_size_delta);
3419 }
3420
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003421
Steve Blocka7e24c12009-10-30 11:49:00 +00003422 set_map(new_map);
Ben Murdoch257744e2011-11-30 15:57:28 +00003423 new_map->clear_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00003424
3425 set_properties(dictionary);
3426
Ben Murdoch8b112d22011-06-08 16:22:53 +01003427 current_heap->isolate()->counters()->props_to_dictionary()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +00003428
3429#ifdef DEBUG
3430 if (FLAG_trace_normalization) {
3431 PrintF("Object properties have been normalized:\n");
3432 Print();
3433 }
3434#endif
3435 return this;
3436}
3437
3438
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003439void JSObject::TransformToFastProperties(Handle<JSObject> object,
3440 int unused_property_fields) {
3441 CALL_HEAP_FUNCTION_VOID(
3442 object->GetIsolate(),
3443 object->TransformToFastProperties(unused_property_fields));
3444}
3445
3446
John Reck59135872010-11-02 12:39:01 -07003447MaybeObject* JSObject::TransformToFastProperties(int unused_property_fields) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003448 if (HasFastProperties()) return this;
3449 ASSERT(!IsGlobalObject());
3450 return property_dictionary()->
3451 TransformPropertiesToFastFor(this, unused_property_fields);
3452}
3453
3454
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003455Handle<SeededNumberDictionary> JSObject::NormalizeElements(
3456 Handle<JSObject> object) {
3457 CALL_HEAP_FUNCTION(object->GetIsolate(),
3458 object->NormalizeElements(),
3459 SeededNumberDictionary);
3460}
3461
3462
John Reck59135872010-11-02 12:39:01 -07003463MaybeObject* JSObject::NormalizeElements() {
Steve Block44f0eee2011-05-26 01:26:41 +01003464 ASSERT(!HasExternalArrayElements());
Steve Block8defd9f2010-07-08 12:39:36 +01003465
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003466 // Find the backing store.
3467 FixedArrayBase* array = FixedArrayBase::cast(elements());
3468 Map* old_map = array->map();
3469 bool is_arguments =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003470 (old_map == old_map->GetHeap()->non_strict_arguments_elements_map());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003471 if (is_arguments) {
3472 array = FixedArrayBase::cast(FixedArray::cast(array)->get(1));
John Reck59135872010-11-02 12:39:01 -07003473 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003474 if (array->IsDictionary()) return array;
Steve Blocka7e24c12009-10-30 11:49:00 +00003475
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003476 ASSERT(HasFastElements() ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003477 HasFastSmiOnlyElements() ||
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003478 HasFastDoubleElements() ||
3479 HasFastArgumentsElements());
3480 // Compute the effective length and allocate a new backing store.
3481 int length = IsJSArray()
3482 ? Smi::cast(JSArray::cast(this)->length())->value()
3483 : array->length();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003484 int old_capacity = 0;
3485 int used_elements = 0;
3486 GetElementsCapacityAndUsage(&old_capacity, &used_elements);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003487 SeededNumberDictionary* dictionary = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003488 { Object* object;
Ben Murdochc7cc0282012-03-05 14:35:55 +00003489 MaybeObject* maybe = SeededNumberDictionary::Allocate(used_elements);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003490 if (!maybe->ToObject(&object)) return maybe;
Ben Murdochc7cc0282012-03-05 14:35:55 +00003491 dictionary = SeededNumberDictionary::cast(object);
John Reck59135872010-11-02 12:39:01 -07003492 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003493
3494 // Copy the elements to the new backing store.
3495 bool has_double_elements = array->IsFixedDoubleArray();
Steve Blocka7e24c12009-10-30 11:49:00 +00003496 for (int i = 0; i < length; i++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003497 Object* value = NULL;
3498 if (has_double_elements) {
3499 FixedDoubleArray* double_array = FixedDoubleArray::cast(array);
3500 if (double_array->is_the_hole(i)) {
3501 value = GetIsolate()->heap()->the_hole_value();
3502 } else {
3503 // Objects must be allocated in the old object space, since the
3504 // overall number of HeapNumbers needed for the conversion might
3505 // exceed the capacity of new space, and we would fail repeatedly
3506 // trying to convert the FixedDoubleArray.
3507 MaybeObject* maybe_value_object =
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003508 GetHeap()->AllocateHeapNumber(double_array->get_scalar(i), TENURED);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003509 if (!maybe_value_object->ToObject(&value)) return maybe_value_object;
John Reck59135872010-11-02 12:39:01 -07003510 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003511 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003512 ASSERT(old_map->has_fast_elements() ||
3513 old_map->has_fast_smi_only_elements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003514 value = FixedArray::cast(array)->get(i);
3515 }
3516 PropertyDetails details = PropertyDetails(NONE, NORMAL);
3517 if (!value->IsTheHole()) {
3518 Object* result;
3519 MaybeObject* maybe_result =
3520 dictionary->AddNumberEntry(i, value, details);
3521 if (!maybe_result->ToObject(&result)) return maybe_result;
Ben Murdochc7cc0282012-03-05 14:35:55 +00003522 dictionary = SeededNumberDictionary::cast(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003523 }
3524 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003525
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003526 // Switch to using the dictionary as the backing storage for elements.
3527 if (is_arguments) {
3528 FixedArray::cast(elements())->set(1, dictionary);
3529 } else {
3530 // Set the new map first to satify the elements type assert in
3531 // set_elements().
3532 Object* new_map;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003533 MaybeObject* maybe = GetElementsTransitionMap(GetIsolate(),
3534 DICTIONARY_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003535 if (!maybe->ToObject(&new_map)) return maybe;
3536 set_map(Map::cast(new_map));
3537 set_elements(dictionary);
3538 }
3539
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003540 old_map->GetHeap()->isolate()->counters()->elements_to_dictionary()->
3541 Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +00003542
3543#ifdef DEBUG
3544 if (FLAG_trace_normalization) {
3545 PrintF("Object elements have been normalized:\n");
3546 Print();
3547 }
3548#endif
3549
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003550 ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements());
3551 return dictionary;
Steve Blocka7e24c12009-10-30 11:49:00 +00003552}
3553
3554
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003555Smi* JSReceiver::GenerateIdentityHash() {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003556 Isolate* isolate = GetIsolate();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003557
3558 int hash_value;
3559 int attempts = 0;
3560 do {
3561 // Generate a random 32-bit hash value but limit range to fit
3562 // within a smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003563 hash_value = V8::RandomPrivate(isolate) & Smi::kMaxValue;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003564 attempts++;
3565 } while (hash_value == 0 && attempts < 30);
3566 hash_value = hash_value != 0 ? hash_value : 1; // never return 0
3567
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003568 return Smi::FromInt(hash_value);
3569}
3570
3571
3572MaybeObject* JSObject::SetIdentityHash(Object* hash, CreationFlag flag) {
3573 MaybeObject* maybe = SetHiddenProperty(GetHeap()->identity_hash_symbol(),
3574 hash);
3575 if (maybe->IsFailure()) return maybe;
3576 return this;
3577}
3578
3579
3580int JSObject::GetIdentityHash(Handle<JSObject> obj) {
3581 CALL_AND_RETRY(obj->GetIsolate(),
3582 obj->GetIdentityHash(ALLOW_CREATION),
3583 return Smi::cast(__object__)->value(),
3584 return 0);
3585}
3586
3587
3588MaybeObject* JSObject::GetIdentityHash(CreationFlag flag) {
3589 Object* stored_value = GetHiddenProperty(GetHeap()->identity_hash_symbol());
3590 if (stored_value->IsSmi()) return stored_value;
3591
3592 // Do not generate permanent identity hash code if not requested.
3593 if (flag == OMIT_CREATION) return GetHeap()->undefined_value();
3594
3595 Smi* hash = GenerateIdentityHash();
3596 MaybeObject* result = SetHiddenProperty(GetHeap()->identity_hash_symbol(),
3597 hash);
3598 if (result->IsFailure()) return result;
3599 if (result->ToObjectUnchecked()->IsUndefined()) {
3600 // Trying to get hash of detached proxy.
3601 return Smi::FromInt(0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003602 }
3603 return hash;
3604}
3605
3606
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003607MaybeObject* JSProxy::GetIdentityHash(CreationFlag flag) {
3608 Object* hash = this->hash();
3609 if (!hash->IsSmi() && flag == ALLOW_CREATION) {
3610 hash = GenerateIdentityHash();
3611 set_hash(hash);
3612 }
3613 return hash;
3614}
3615
3616
3617Object* JSObject::GetHiddenProperty(String* key) {
3618 if (IsJSGlobalProxy()) {
3619 // For a proxy, use the prototype as target object.
3620 Object* proxy_parent = GetPrototype();
3621 // If the proxy is detached, return undefined.
3622 if (proxy_parent->IsNull()) return GetHeap()->undefined_value();
3623 ASSERT(proxy_parent->IsJSGlobalObject());
3624 return JSObject::cast(proxy_parent)->GetHiddenProperty(key);
3625 }
3626 ASSERT(!IsJSGlobalProxy());
3627 MaybeObject* hidden_lookup = GetHiddenPropertiesDictionary(false);
3628 ASSERT(!hidden_lookup->IsFailure()); // No failure when passing false as arg.
3629 if (hidden_lookup->ToObjectUnchecked()->IsUndefined()) {
3630 return GetHeap()->undefined_value();
3631 }
3632 StringDictionary* dictionary =
3633 StringDictionary::cast(hidden_lookup->ToObjectUnchecked());
3634 int entry = dictionary->FindEntry(key);
3635 if (entry == StringDictionary::kNotFound) return GetHeap()->undefined_value();
3636 return dictionary->ValueAt(entry);
3637}
3638
3639
3640Handle<Object> JSObject::SetHiddenProperty(Handle<JSObject> obj,
3641 Handle<String> key,
3642 Handle<Object> value) {
3643 CALL_HEAP_FUNCTION(obj->GetIsolate(),
3644 obj->SetHiddenProperty(*key, *value),
3645 Object);
3646}
3647
3648
3649MaybeObject* JSObject::SetHiddenProperty(String* key, Object* value) {
3650 if (IsJSGlobalProxy()) {
3651 // For a proxy, use the prototype as target object.
3652 Object* proxy_parent = GetPrototype();
3653 // If the proxy is detached, return undefined.
3654 if (proxy_parent->IsNull()) return GetHeap()->undefined_value();
3655 ASSERT(proxy_parent->IsJSGlobalObject());
3656 return JSObject::cast(proxy_parent)->SetHiddenProperty(key, value);
3657 }
3658 ASSERT(!IsJSGlobalProxy());
3659 MaybeObject* hidden_lookup = GetHiddenPropertiesDictionary(true);
3660 StringDictionary* dictionary;
3661 if (!hidden_lookup->To<StringDictionary>(&dictionary)) return hidden_lookup;
3662
3663 // If it was found, check if the key is already in the dictionary.
3664 int entry = dictionary->FindEntry(key);
3665 if (entry != StringDictionary::kNotFound) {
3666 // If key was found, just update the value.
3667 dictionary->ValueAtPut(entry, value);
3668 return this;
3669 }
3670 // Key was not already in the dictionary, so add the entry.
3671 MaybeObject* insert_result = dictionary->Add(key,
3672 value,
3673 PropertyDetails(NONE, NORMAL));
3674 StringDictionary* new_dict;
3675 if (!insert_result->To<StringDictionary>(&new_dict)) return insert_result;
3676 if (new_dict != dictionary) {
3677 // If adding the key expanded the dictionary (i.e., Add returned a new
3678 // dictionary), store it back to the object.
3679 MaybeObject* store_result = SetHiddenPropertiesDictionary(new_dict);
3680 if (store_result->IsFailure()) return store_result;
3681 }
3682 // Return this to mark success.
3683 return this;
3684}
3685
3686
3687void JSObject::DeleteHiddenProperty(String* key) {
3688 if (IsJSGlobalProxy()) {
3689 // For a proxy, use the prototype as target object.
3690 Object* proxy_parent = GetPrototype();
3691 // If the proxy is detached, return immediately.
3692 if (proxy_parent->IsNull()) return;
3693 ASSERT(proxy_parent->IsJSGlobalObject());
3694 JSObject::cast(proxy_parent)->DeleteHiddenProperty(key);
3695 return;
3696 }
3697 MaybeObject* hidden_lookup = GetHiddenPropertiesDictionary(false);
3698 ASSERT(!hidden_lookup->IsFailure()); // No failure when passing false as arg.
3699 if (hidden_lookup->ToObjectUnchecked()->IsUndefined()) return;
3700 StringDictionary* dictionary =
3701 StringDictionary::cast(hidden_lookup->ToObjectUnchecked());
3702 int entry = dictionary->FindEntry(key);
3703 if (entry == StringDictionary::kNotFound) {
3704 // Key wasn't in dictionary. Deletion is a success.
3705 return;
3706 }
3707 // Key was in the dictionary. Remove it.
3708 dictionary->DeleteProperty(entry, JSReceiver::FORCE_DELETION);
3709}
3710
3711
3712bool JSObject::HasHiddenProperties() {
3713 return GetPropertyAttributePostInterceptor(this,
3714 GetHeap()->hidden_symbol(),
3715 false) != ABSENT;
3716}
3717
3718
3719MaybeObject* JSObject::GetHiddenPropertiesDictionary(bool create_if_absent) {
3720 ASSERT(!IsJSGlobalProxy());
3721 if (HasFastProperties()) {
3722 // If the object has fast properties, check whether the first slot
3723 // in the descriptor array matches the hidden symbol. Since the
3724 // hidden symbols hash code is zero (and no other string has hash
3725 // code zero) it will always occupy the first entry if present.
3726 DescriptorArray* descriptors = this->map()->instance_descriptors();
3727 if ((descriptors->number_of_descriptors() > 0) &&
3728 (descriptors->GetKey(0) == GetHeap()->hidden_symbol())) {
3729 if (descriptors->GetType(0) == FIELD) {
3730 Object* hidden_store =
3731 this->FastPropertyAt(descriptors->GetFieldIndex(0));
3732 return StringDictionary::cast(hidden_store);
3733 } else {
3734 ASSERT(descriptors->GetType(0) == NULL_DESCRIPTOR ||
3735 descriptors->GetType(0) == MAP_TRANSITION);
3736 }
3737 }
3738 } else {
3739 PropertyAttributes attributes;
3740 // You can't install a getter on a property indexed by the hidden symbol,
3741 // so we can be sure that GetLocalPropertyPostInterceptor returns a real
3742 // object.
3743 Object* lookup =
3744 GetLocalPropertyPostInterceptor(this,
3745 GetHeap()->hidden_symbol(),
3746 &attributes)->ToObjectUnchecked();
3747 if (!lookup->IsUndefined()) {
3748 return StringDictionary::cast(lookup);
3749 }
3750 }
3751 if (!create_if_absent) return GetHeap()->undefined_value();
3752 const int kInitialSize = 5;
3753 MaybeObject* dict_alloc = StringDictionary::Allocate(kInitialSize);
3754 StringDictionary* dictionary;
3755 if (!dict_alloc->To<StringDictionary>(&dictionary)) return dict_alloc;
3756 MaybeObject* store_result =
3757 SetPropertyPostInterceptor(GetHeap()->hidden_symbol(),
3758 dictionary,
3759 DONT_ENUM,
3760 kNonStrictMode);
3761 if (store_result->IsFailure()) return store_result;
3762 return dictionary;
3763}
3764
3765
3766MaybeObject* JSObject::SetHiddenPropertiesDictionary(
3767 StringDictionary* dictionary) {
3768 ASSERT(!IsJSGlobalProxy());
3769 ASSERT(HasHiddenProperties());
3770 if (HasFastProperties()) {
3771 // If the object has fast properties, check whether the first slot
3772 // in the descriptor array matches the hidden symbol. Since the
3773 // hidden symbols hash code is zero (and no other string has hash
3774 // code zero) it will always occupy the first entry if present.
3775 DescriptorArray* descriptors = this->map()->instance_descriptors();
3776 if ((descriptors->number_of_descriptors() > 0) &&
3777 (descriptors->GetKey(0) == GetHeap()->hidden_symbol())) {
3778 if (descriptors->GetType(0) == FIELD) {
3779 this->FastPropertyAtPut(descriptors->GetFieldIndex(0), dictionary);
3780 return this;
3781 } else {
3782 ASSERT(descriptors->GetType(0) == NULL_DESCRIPTOR ||
3783 descriptors->GetType(0) == MAP_TRANSITION);
3784 }
3785 }
3786 }
3787 MaybeObject* store_result =
3788 SetPropertyPostInterceptor(GetHeap()->hidden_symbol(),
3789 dictionary,
3790 DONT_ENUM,
3791 kNonStrictMode);
3792 if (store_result->IsFailure()) return store_result;
3793 return this;
3794}
3795
3796
John Reck59135872010-11-02 12:39:01 -07003797MaybeObject* JSObject::DeletePropertyPostInterceptor(String* name,
3798 DeleteMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003799 // Check local property, ignore interceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003800 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00003801 LocalLookupRealNamedProperty(name, &result);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003802 if (!result.IsProperty()) return GetHeap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003803
3804 // Normalize object if needed.
John Reck59135872010-11-02 12:39:01 -07003805 Object* obj;
3806 { MaybeObject* maybe_obj = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
3807 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3808 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003809
3810 return DeleteNormalizedProperty(name, mode);
3811}
3812
3813
John Reck59135872010-11-02 12:39:01 -07003814MaybeObject* JSObject::DeletePropertyWithInterceptor(String* name) {
Steve Block44f0eee2011-05-26 01:26:41 +01003815 Isolate* isolate = GetIsolate();
3816 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003817 Handle<InterceptorInfo> interceptor(GetNamedInterceptor());
3818 Handle<String> name_handle(name);
3819 Handle<JSObject> this_handle(this);
3820 if (!interceptor->deleter()->IsUndefined()) {
3821 v8::NamedPropertyDeleter deleter =
3822 v8::ToCData<v8::NamedPropertyDeleter>(interceptor->deleter());
Steve Block44f0eee2011-05-26 01:26:41 +01003823 LOG(isolate,
3824 ApiNamedPropertyAccess("interceptor-named-delete", *this_handle, name));
3825 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003826 v8::AccessorInfo info(args.end());
3827 v8::Handle<v8::Boolean> result;
3828 {
3829 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01003830 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003831 result = deleter(v8::Utils::ToLocal(name_handle), info);
3832 }
Steve Block44f0eee2011-05-26 01:26:41 +01003833 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003834 if (!result.IsEmpty()) {
3835 ASSERT(result->IsBoolean());
3836 return *v8::Utils::OpenHandle(*result);
3837 }
3838 }
John Reck59135872010-11-02 12:39:01 -07003839 MaybeObject* raw_result =
Steve Blocka7e24c12009-10-30 11:49:00 +00003840 this_handle->DeletePropertyPostInterceptor(*name_handle, NORMAL_DELETION);
Steve Block44f0eee2011-05-26 01:26:41 +01003841 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003842 return raw_result;
3843}
3844
3845
John Reck59135872010-11-02 12:39:01 -07003846MaybeObject* JSObject::DeleteElementWithInterceptor(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01003847 Isolate* isolate = GetIsolate();
3848 Heap* heap = isolate->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003849 // Make sure that the top context does not change when doing
3850 // callbacks or interceptor calls.
3851 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01003852 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003853 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +01003854 if (interceptor->deleter()->IsUndefined()) return heap->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003855 v8::IndexedPropertyDeleter deleter =
3856 v8::ToCData<v8::IndexedPropertyDeleter>(interceptor->deleter());
3857 Handle<JSObject> this_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01003858 LOG(isolate,
3859 ApiIndexedPropertyAccess("interceptor-indexed-delete", this, index));
3860 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00003861 v8::AccessorInfo info(args.end());
3862 v8::Handle<v8::Boolean> result;
3863 {
3864 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01003865 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00003866 result = deleter(index, info);
3867 }
Steve Block44f0eee2011-05-26 01:26:41 +01003868 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003869 if (!result.IsEmpty()) {
3870 ASSERT(result->IsBoolean());
3871 return *v8::Utils::OpenHandle(*result);
3872 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003873 MaybeObject* raw_result = this_handle->GetElementsAccessor()->Delete(
3874 *this_handle,
3875 index,
3876 NORMAL_DELETION);
Steve Block44f0eee2011-05-26 01:26:41 +01003877 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003878 return raw_result;
3879}
3880
3881
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003882Handle<Object> JSObject::DeleteElement(Handle<JSObject> obj,
3883 uint32_t index) {
3884 CALL_HEAP_FUNCTION(obj->GetIsolate(),
3885 obj->DeleteElement(index, JSObject::NORMAL_DELETION),
3886 Object);
3887}
3888
3889
John Reck59135872010-11-02 12:39:01 -07003890MaybeObject* JSObject::DeleteElement(uint32_t index, DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01003891 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00003892 // Check access rights if needed.
3893 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01003894 !isolate->MayIndexedAccess(this, index, v8::ACCESS_DELETE)) {
3895 isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE);
3896 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003897 }
3898
3899 if (IsJSGlobalProxy()) {
3900 Object* proto = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01003901 if (proto->IsNull()) return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003902 ASSERT(proto->IsJSGlobalObject());
3903 return JSGlobalObject::cast(proto)->DeleteElement(index, mode);
3904 }
3905
3906 if (HasIndexedInterceptor()) {
3907 // Skip interceptor if forcing deletion.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003908 if (mode != FORCE_DELETION) {
3909 return DeleteElementWithInterceptor(index);
3910 }
3911 mode = JSReceiver::FORCE_DELETION;
Steve Blocka7e24c12009-10-30 11:49:00 +00003912 }
3913
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003914 return GetElementsAccessor()->Delete(this, index, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00003915}
3916
3917
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003918Handle<Object> JSObject::DeleteProperty(Handle<JSObject> obj,
3919 Handle<String> prop) {
3920 CALL_HEAP_FUNCTION(obj->GetIsolate(),
3921 obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION),
3922 Object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003923}
3924
3925
John Reck59135872010-11-02 12:39:01 -07003926MaybeObject* JSObject::DeleteProperty(String* name, DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01003927 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00003928 // ECMA-262, 3rd, 8.6.2.5
3929 ASSERT(name->IsString());
3930
3931 // Check access rights if needed.
3932 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01003933 !isolate->MayNamedAccess(this, name, v8::ACCESS_DELETE)) {
3934 isolate->ReportFailedAccessCheck(this, v8::ACCESS_DELETE);
3935 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003936 }
3937
3938 if (IsJSGlobalProxy()) {
3939 Object* proto = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01003940 if (proto->IsNull()) return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003941 ASSERT(proto->IsJSGlobalObject());
3942 return JSGlobalObject::cast(proto)->DeleteProperty(name, mode);
3943 }
3944
3945 uint32_t index = 0;
3946 if (name->AsArrayIndex(&index)) {
3947 return DeleteElement(index, mode);
3948 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003949 LookupResult result(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00003950 LocalLookup(name, &result);
Steve Block44f0eee2011-05-26 01:26:41 +01003951 if (!result.IsProperty()) return isolate->heap()->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003952 // Ignore attributes if forcing a deletion.
3953 if (result.IsDontDelete() && mode != FORCE_DELETION) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003954 if (mode == STRICT_DELETION) {
3955 // Deleting a non-configurable property in strict mode.
Steve Block44f0eee2011-05-26 01:26:41 +01003956 HandleScope scope(isolate);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003957 Handle<Object> args[2] = { Handle<Object>(name), Handle<Object>(this) };
Steve Block44f0eee2011-05-26 01:26:41 +01003958 return isolate->Throw(*isolate->factory()->NewTypeError(
3959 "strict_delete_property", HandleVector(args, 2)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003960 }
Steve Block44f0eee2011-05-26 01:26:41 +01003961 return isolate->heap()->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003962 }
3963 // Check for interceptor.
3964 if (result.type() == INTERCEPTOR) {
3965 // Skip interceptor if forcing a deletion.
3966 if (mode == FORCE_DELETION) {
3967 return DeletePropertyPostInterceptor(name, mode);
3968 }
3969 return DeletePropertyWithInterceptor(name);
3970 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003971 // Normalize object if needed.
John Reck59135872010-11-02 12:39:01 -07003972 Object* obj;
3973 { MaybeObject* maybe_obj =
3974 NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
3975 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3976 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003977 // Make sure the properties are normalized before removing the entry.
3978 return DeleteNormalizedProperty(name, mode);
3979 }
3980}
3981
3982
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003983MaybeObject* JSReceiver::DeleteElement(uint32_t index, DeleteMode mode) {
3984 if (IsJSProxy()) {
3985 return JSProxy::cast(this)->DeleteElementWithHandler(index, mode);
3986 }
3987 return JSObject::cast(this)->DeleteElement(index, mode);
3988}
3989
3990
3991MaybeObject* JSReceiver::DeleteProperty(String* name, DeleteMode mode) {
3992 if (IsJSProxy()) {
3993 return JSProxy::cast(this)->DeletePropertyWithHandler(name, mode);
3994 }
3995 return JSObject::cast(this)->DeleteProperty(name, mode);
3996}
3997
3998
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003999bool JSObject::ReferencesObjectFromElements(FixedArray* elements,
4000 ElementsKind kind,
4001 Object* object) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004002 ASSERT(kind == FAST_ELEMENTS ||
4003 kind == DICTIONARY_ELEMENTS);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004004 if (kind == FAST_ELEMENTS) {
4005 int length = IsJSArray()
4006 ? Smi::cast(JSArray::cast(this)->length())->value()
4007 : elements->length();
4008 for (int i = 0; i < length; ++i) {
4009 Object* element = elements->get(i);
4010 if (!element->IsTheHole() && element == object) return true;
4011 }
4012 } else {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004013 Object* key =
4014 SeededNumberDictionary::cast(elements)->SlowReverseLookup(object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004015 if (!key->IsUndefined()) return true;
4016 }
4017 return false;
4018}
4019
4020
Steve Blocka7e24c12009-10-30 11:49:00 +00004021// Check whether this object references another object.
4022bool JSObject::ReferencesObject(Object* obj) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01004023 Map* map_of_this = map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004024 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004025 AssertNoAllocation no_alloc;
4026
4027 // Is the object the constructor for this object?
Ben Murdoch8b112d22011-06-08 16:22:53 +01004028 if (map_of_this->constructor() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004029 return true;
4030 }
4031
4032 // Is the object the prototype for this object?
Ben Murdoch8b112d22011-06-08 16:22:53 +01004033 if (map_of_this->prototype() == obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004034 return true;
4035 }
4036
4037 // Check if the object is among the named properties.
4038 Object* key = SlowReverseLookup(obj);
Steve Block44f0eee2011-05-26 01:26:41 +01004039 if (!key->IsUndefined()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004040 return true;
4041 }
4042
4043 // Check if the object is among the indexed properties.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004044 ElementsKind kind = GetElementsKind();
4045 switch (kind) {
Steve Block44f0eee2011-05-26 01:26:41 +01004046 case EXTERNAL_PIXEL_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00004047 case EXTERNAL_BYTE_ELEMENTS:
4048 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
4049 case EXTERNAL_SHORT_ELEMENTS:
4050 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
4051 case EXTERNAL_INT_ELEMENTS:
4052 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
4053 case EXTERNAL_FLOAT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004054 case EXTERNAL_DOUBLE_ELEMENTS:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004055 case FAST_DOUBLE_ELEMENTS:
Steve Block3ce2e202009-11-05 08:53:23 +00004056 // Raw pixels and external arrays do not reference other
4057 // objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00004058 break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004059 case FAST_SMI_ONLY_ELEMENTS:
4060 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004061 case FAST_ELEMENTS:
Steve Blocka7e24c12009-10-30 11:49:00 +00004062 case DICTIONARY_ELEMENTS: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004063 FixedArray* elements = FixedArray::cast(this->elements());
4064 if (ReferencesObjectFromElements(elements, kind, obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00004065 break;
4066 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004067 case NON_STRICT_ARGUMENTS_ELEMENTS: {
4068 FixedArray* parameter_map = FixedArray::cast(elements());
4069 // Check the mapped parameters.
4070 int length = parameter_map->length();
4071 for (int i = 2; i < length; ++i) {
4072 Object* value = parameter_map->get(i);
4073 if (!value->IsTheHole() && value == obj) return true;
4074 }
4075 // Check the arguments.
4076 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
4077 kind = arguments->IsDictionary() ? DICTIONARY_ELEMENTS : FAST_ELEMENTS;
4078 if (ReferencesObjectFromElements(arguments, kind, obj)) return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00004079 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004080 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004081 }
4082
Steve Block6ded16b2010-05-10 14:33:55 +01004083 // For functions check the context.
4084 if (IsJSFunction()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004085 // Get the constructor function for arguments array.
4086 JSObject* arguments_boilerplate =
Steve Block44f0eee2011-05-26 01:26:41 +01004087 heap->isolate()->context()->global_context()->
4088 arguments_boilerplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004089 JSFunction* arguments_function =
4090 JSFunction::cast(arguments_boilerplate->map()->constructor());
4091
4092 // Get the context and don't check if it is the global context.
4093 JSFunction* f = JSFunction::cast(this);
4094 Context* context = f->context();
4095 if (context->IsGlobalContext()) {
4096 return false;
4097 }
4098
4099 // Check the non-special context slots.
4100 for (int i = Context::MIN_CONTEXT_SLOTS; i < context->length(); i++) {
4101 // Only check JS objects.
4102 if (context->get(i)->IsJSObject()) {
4103 JSObject* ctxobj = JSObject::cast(context->get(i));
4104 // If it is an arguments array check the content.
4105 if (ctxobj->map()->constructor() == arguments_function) {
4106 if (ctxobj->ReferencesObject(obj)) {
4107 return true;
4108 }
4109 } else if (ctxobj == obj) {
4110 return true;
4111 }
4112 }
4113 }
4114
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004115 // Check the context extension (if any) if it can have references.
4116 if (context->has_extension() && !context->IsCatchContext()) {
4117 return JSObject::cast(context->extension())->ReferencesObject(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004118 }
4119 }
4120
4121 // No references to object.
4122 return false;
4123}
4124
4125
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004126Handle<Object> JSObject::PreventExtensions(Handle<JSObject> object) {
4127 CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object);
4128}
4129
4130
John Reck59135872010-11-02 12:39:01 -07004131MaybeObject* JSObject::PreventExtensions() {
Steve Block44f0eee2011-05-26 01:26:41 +01004132 Isolate* isolate = GetIsolate();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004133 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01004134 !isolate->MayNamedAccess(this,
4135 isolate->heap()->undefined_value(),
4136 v8::ACCESS_KEYS)) {
4137 isolate->ReportFailedAccessCheck(this, v8::ACCESS_KEYS);
4138 return isolate->heap()->false_value();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004139 }
4140
Steve Block1e0659c2011-05-24 12:43:12 +01004141 if (IsJSGlobalProxy()) {
4142 Object* proto = GetPrototype();
4143 if (proto->IsNull()) return this;
4144 ASSERT(proto->IsJSGlobalObject());
4145 return JSObject::cast(proto)->PreventExtensions();
4146 }
4147
Ben Murdoch257744e2011-11-30 15:57:28 +00004148 // It's not possible to seal objects with external array elements
4149 if (HasExternalArrayElements()) {
4150 HandleScope scope(isolate);
4151 Handle<Object> object(this);
4152 Handle<Object> error =
4153 isolate->factory()->NewTypeError(
4154 "cant_prevent_ext_external_array_elements",
4155 HandleVector(&object, 1));
4156 return isolate->Throw(*error);
4157 }
4158
Steve Block8defd9f2010-07-08 12:39:36 +01004159 // If there are fast elements we normalize.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004160 SeededNumberDictionary* dictionary = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004161 { MaybeObject* maybe = NormalizeElements();
Ben Murdochc7cc0282012-03-05 14:35:55 +00004162 if (!maybe->To<SeededNumberDictionary>(&dictionary)) return maybe;
Steve Block8defd9f2010-07-08 12:39:36 +01004163 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004164 ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements());
Steve Block8defd9f2010-07-08 12:39:36 +01004165 // Make sure that we never go back to fast case.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004166 dictionary->set_requires_slow_elements();
Steve Block8defd9f2010-07-08 12:39:36 +01004167
4168 // Do a map transition, other objects with this map may still
4169 // be extensible.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004170 Map* new_map;
4171 { MaybeObject* maybe = map()->CopyDropTransitions();
4172 if (!maybe->To<Map>(&new_map)) return maybe;
John Reck59135872010-11-02 12:39:01 -07004173 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004174 new_map->set_is_extensible(false);
4175 set_map(new_map);
Steve Block8defd9f2010-07-08 12:39:36 +01004176 ASSERT(!map()->is_extensible());
4177 return new_map;
4178}
4179
4180
Steve Blocka7e24c12009-10-30 11:49:00 +00004181// Tests for the fast common case for property enumeration:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004182// - This object and all prototypes has an enum cache (which means that
4183// it is no proxy, has no interceptors and needs no access checks).
Steve Blockd0582a62009-12-15 09:54:21 +00004184// - This object has no elements.
4185// - No prototype has enumerable properties/elements.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004186bool JSReceiver::IsSimpleEnum() {
Steve Block44f0eee2011-05-26 01:26:41 +01004187 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004188 for (Object* o = this;
Steve Block44f0eee2011-05-26 01:26:41 +01004189 o != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004190 o = JSObject::cast(o)->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004191 if (!o->IsJSObject()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00004192 JSObject* curr = JSObject::cast(o);
Steve Blocka7e24c12009-10-30 11:49:00 +00004193 if (!curr->map()->instance_descriptors()->HasEnumCache()) return false;
Steve Blockd0582a62009-12-15 09:54:21 +00004194 ASSERT(!curr->HasNamedInterceptor());
4195 ASSERT(!curr->HasIndexedInterceptor());
4196 ASSERT(!curr->IsAccessCheckNeeded());
Steve Blocka7e24c12009-10-30 11:49:00 +00004197 if (curr->NumberOfEnumElements() > 0) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00004198 if (curr != this) {
4199 FixedArray* curr_fixed_array =
4200 FixedArray::cast(curr->map()->instance_descriptors()->GetEnumCache());
Steve Blockd0582a62009-12-15 09:54:21 +00004201 if (curr_fixed_array->length() > 0) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00004202 }
4203 }
4204 return true;
4205}
4206
4207
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004208int Map::NumberOfDescribedProperties(PropertyAttributes filter) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004209 int result = 0;
4210 DescriptorArray* descs = instance_descriptors();
4211 for (int i = 0; i < descs->number_of_descriptors(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004212 PropertyDetails details(descs->GetDetails(i));
4213 if (descs->IsProperty(i) && (details.attributes() & filter) == 0) {
4214 result++;
4215 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004216 }
4217 return result;
4218}
4219
4220
4221int Map::PropertyIndexFor(String* name) {
4222 DescriptorArray* descs = instance_descriptors();
4223 for (int i = 0; i < descs->number_of_descriptors(); i++) {
4224 if (name->Equals(descs->GetKey(i)) && !descs->IsNullDescriptor(i)) {
4225 return descs->GetFieldIndex(i);
4226 }
4227 }
4228 return -1;
4229}
4230
4231
4232int Map::NextFreePropertyIndex() {
4233 int max_index = -1;
4234 DescriptorArray* descs = instance_descriptors();
4235 for (int i = 0; i < descs->number_of_descriptors(); i++) {
4236 if (descs->GetType(i) == FIELD) {
4237 int current_index = descs->GetFieldIndex(i);
4238 if (current_index > max_index) max_index = current_index;
4239 }
4240 }
4241 return max_index + 1;
4242}
4243
4244
4245AccessorDescriptor* Map::FindAccessor(String* name) {
4246 DescriptorArray* descs = instance_descriptors();
4247 for (int i = 0; i < descs->number_of_descriptors(); i++) {
4248 if (name->Equals(descs->GetKey(i)) && descs->GetType(i) == CALLBACKS) {
4249 return descs->GetCallbacks(i);
4250 }
4251 }
4252 return NULL;
4253}
4254
4255
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004256void JSReceiver::LocalLookup(String* name, LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004257 ASSERT(name->IsString());
4258
Steve Block44f0eee2011-05-26 01:26:41 +01004259 Heap* heap = GetHeap();
4260
Steve Blocka7e24c12009-10-30 11:49:00 +00004261 if (IsJSGlobalProxy()) {
4262 Object* proto = GetPrototype();
4263 if (proto->IsNull()) return result->NotFound();
4264 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004265 return JSReceiver::cast(proto)->LocalLookup(name, result);
4266 }
4267
4268 if (IsJSProxy()) {
4269 result->HandlerResult(JSProxy::cast(this));
4270 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00004271 }
4272
4273 // Do not use inline caching if the object is a non-global object
4274 // that requires access checks.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004275 if (IsAccessCheckNeeded()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004276 result->DisallowCaching();
4277 }
4278
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004279 JSObject* js_object = JSObject::cast(this);
4280
Steve Blocka7e24c12009-10-30 11:49:00 +00004281 // Check __proto__ before interceptor.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004282 if (name->Equals(heap->Proto_symbol()) && !IsJSContextExtensionObject()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004283 result->ConstantResult(js_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004284 return;
4285 }
4286
4287 // Check for lookup interceptor except when bootstrapping.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004288 if (js_object->HasNamedInterceptor() &&
4289 !heap->isolate()->bootstrapper()->IsActive()) {
4290 result->InterceptorResult(js_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004291 return;
4292 }
4293
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004294 js_object->LocalLookupRealNamedProperty(name, result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004295}
4296
4297
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004298void JSReceiver::Lookup(String* name, LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004299 // Ecma-262 3rd 8.6.2.4
Steve Block44f0eee2011-05-26 01:26:41 +01004300 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004301 for (Object* current = this;
Steve Block44f0eee2011-05-26 01:26:41 +01004302 current != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004303 current = JSObject::cast(current)->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004304 JSReceiver::cast(current)->LocalLookup(name, result);
Andrei Popescu402d9372010-02-26 13:31:12 +00004305 if (result->IsProperty()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00004306 }
4307 result->NotFound();
4308}
4309
4310
4311// Search object and it's prototype chain for callback properties.
4312void JSObject::LookupCallback(String* name, LookupResult* result) {
Steve Block44f0eee2011-05-26 01:26:41 +01004313 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00004314 for (Object* current = this;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004315 current != heap->null_value() && current->IsJSObject();
Steve Blocka7e24c12009-10-30 11:49:00 +00004316 current = JSObject::cast(current)->GetPrototype()) {
4317 JSObject::cast(current)->LocalLookupRealNamedProperty(name, result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004318 if (result->IsFound() && result->type() == CALLBACKS) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00004319 }
4320 result->NotFound();
4321}
4322
4323
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004324// Try to update an accessor in an elements dictionary. Return true if the
4325// update succeeded, and false otherwise.
4326static bool UpdateGetterSetterInDictionary(
4327 SeededNumberDictionary* dictionary,
4328 uint32_t index,
4329 Object* getter,
4330 Object* setter,
4331 PropertyAttributes attributes) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004332 int entry = dictionary->FindEntry(index);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004333 if (entry != SeededNumberDictionary::kNotFound) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004334 Object* result = dictionary->ValueAt(entry);
4335 PropertyDetails details = dictionary->DetailsAt(entry);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004336 if (details.type() == CALLBACKS && result->IsAccessorPair()) {
4337 ASSERT(!details.IsDontDelete());
4338 if (details.attributes() != attributes) {
4339 dictionary->DetailsAtPut(entry,
4340 PropertyDetails(attributes, CALLBACKS, index));
4341 }
4342 AccessorPair::cast(result)->SetComponents(getter, setter);
4343 return true;
4344 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004345 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004346 return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004347}
4348
4349
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004350MaybeObject* JSObject::DefineElementAccessor(uint32_t index,
4351 Object* getter,
4352 Object* setter,
4353 PropertyAttributes attributes) {
4354 switch (GetElementsKind()) {
4355 case FAST_SMI_ONLY_ELEMENTS:
4356 case FAST_ELEMENTS:
4357 case FAST_DOUBLE_ELEMENTS:
4358 break;
4359 case EXTERNAL_PIXEL_ELEMENTS:
4360 case EXTERNAL_BYTE_ELEMENTS:
4361 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
4362 case EXTERNAL_SHORT_ELEMENTS:
4363 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
4364 case EXTERNAL_INT_ELEMENTS:
4365 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
4366 case EXTERNAL_FLOAT_ELEMENTS:
4367 case EXTERNAL_DOUBLE_ELEMENTS:
4368 // Ignore getters and setters on pixel and external array elements.
4369 return GetHeap()->undefined_value();
4370 case DICTIONARY_ELEMENTS:
4371 if (UpdateGetterSetterInDictionary(element_dictionary(),
4372 index,
4373 getter,
4374 setter,
4375 attributes)) {
4376 return GetHeap()->undefined_value();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004377 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004378 break;
4379 case NON_STRICT_ARGUMENTS_ELEMENTS: {
4380 // Ascertain whether we have read-only properties or an existing
4381 // getter/setter pair in an arguments elements dictionary backing
4382 // store.
4383 FixedArray* parameter_map = FixedArray::cast(elements());
4384 uint32_t length = parameter_map->length();
4385 Object* probe =
4386 index < (length - 2) ? parameter_map->get(index + 2) : NULL;
4387 if (probe == NULL || probe->IsTheHole()) {
4388 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
4389 if (arguments->IsDictionary()) {
4390 SeededNumberDictionary* dictionary =
4391 SeededNumberDictionary::cast(arguments);
4392 if (UpdateGetterSetterInDictionary(dictionary,
4393 index,
4394 getter,
4395 setter,
4396 attributes)) {
4397 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004398 }
4399 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004400 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004401 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00004402 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004403 }
4404
4405 AccessorPair* accessors;
4406 { MaybeObject* maybe_accessors = GetHeap()->AllocateAccessorPair();
4407 if (!maybe_accessors->To(&accessors)) return maybe_accessors;
4408 }
4409 accessors->SetComponents(getter, setter);
4410
4411 return SetElementCallback(index, accessors, attributes);
4412}
4413
4414
4415MaybeObject* JSObject::DefinePropertyAccessor(String* name,
4416 Object* getter,
4417 Object* setter,
4418 PropertyAttributes attributes) {
4419 // Lookup the name.
4420 LookupResult result(GetHeap()->isolate());
4421 LocalLookupRealNamedProperty(name, &result);
4422 if (result.IsFound()) {
4423 if (result.type() == CALLBACKS) {
4424 ASSERT(!result.IsDontDelete());
4425 Object* obj = result.GetCallbackObject();
4426 // Need to preserve old getters/setters.
4427 if (obj->IsAccessorPair()) {
4428 AccessorPair* copy;
4429 { MaybeObject* maybe_copy =
4430 AccessorPair::cast(obj)->CopyWithoutTransitions();
4431 if (!maybe_copy->To(&copy)) return maybe_copy;
Leon Clarked91b9f72010-01-27 17:25:45 +00004432 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004433 copy->SetComponents(getter, setter);
4434 // Use set to update attributes.
4435 return SetPropertyCallback(name, copy, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00004436 }
4437 }
4438 }
4439
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004440 AccessorPair* accessors;
4441 { MaybeObject* maybe_accessors = GetHeap()->AllocateAccessorPair();
4442 if (!maybe_accessors->To(&accessors)) return maybe_accessors;
John Reck59135872010-11-02 12:39:01 -07004443 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004444 accessors->SetComponents(getter, setter);
Steve Blocka7e24c12009-10-30 11:49:00 +00004445
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004446 return SetPropertyCallback(name, accessors, attributes);
Leon Clarkef7060e22010-06-03 12:02:55 +01004447}
4448
4449
4450bool JSObject::CanSetCallback(String* name) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004451 ASSERT(!IsAccessCheckNeeded() ||
4452 GetIsolate()->MayNamedAccess(this, name, v8::ACCESS_SET));
Leon Clarkef7060e22010-06-03 12:02:55 +01004453
4454 // Check if there is an API defined callback object which prohibits
4455 // callback overwriting in this object or it's prototype chain.
4456 // This mechanism is needed for instance in a browser setting, where
4457 // certain accessors such as window.location should not be allowed
4458 // to be overwritten because allowing overwriting could potentially
4459 // cause security problems.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004460 LookupResult callback_result(GetIsolate());
Leon Clarkef7060e22010-06-03 12:02:55 +01004461 LookupCallback(name, &callback_result);
4462 if (callback_result.IsProperty()) {
4463 Object* obj = callback_result.GetCallbackObject();
4464 if (obj->IsAccessorInfo() &&
4465 AccessorInfo::cast(obj)->prohibits_overwriting()) {
4466 return false;
4467 }
4468 }
4469
4470 return true;
4471}
4472
4473
John Reck59135872010-11-02 12:39:01 -07004474MaybeObject* JSObject::SetElementCallback(uint32_t index,
4475 Object* structure,
4476 PropertyAttributes attributes) {
Leon Clarkef7060e22010-06-03 12:02:55 +01004477 PropertyDetails details = PropertyDetails(attributes, CALLBACKS);
4478
4479 // Normalize elements to make this operation simple.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004480 SeededNumberDictionary* dictionary;
4481 { MaybeObject* maybe_dictionary = NormalizeElements();
4482 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
John Reck59135872010-11-02 12:39:01 -07004483 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004484 ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements());
Leon Clarkef7060e22010-06-03 12:02:55 +01004485
4486 // Update the dictionary with the new CALLBACKS property.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004487 { MaybeObject* maybe_dictionary = dictionary->Set(index, structure, details);
4488 if (!maybe_dictionary->To(&dictionary)) return maybe_dictionary;
John Reck59135872010-11-02 12:39:01 -07004489 }
Leon Clarkef7060e22010-06-03 12:02:55 +01004490
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004491 dictionary->set_requires_slow_elements();
4492 // Update the dictionary backing store on the object.
4493 if (elements()->map() == GetHeap()->non_strict_arguments_elements_map()) {
4494 // Also delete any parameter alias.
4495 //
4496 // TODO(kmillikin): when deleting the last parameter alias we could
4497 // switch to a direct backing store without the parameter map. This
4498 // would allow GC of the context.
4499 FixedArray* parameter_map = FixedArray::cast(elements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004500 if (index < static_cast<uint32_t>(parameter_map->length()) - 2) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004501 parameter_map->set(index + 2, GetHeap()->the_hole_value());
4502 }
4503 parameter_map->set(1, dictionary);
4504 } else {
4505 set_elements(dictionary);
4506 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004507
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004508 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004509}
4510
4511
John Reck59135872010-11-02 12:39:01 -07004512MaybeObject* JSObject::SetPropertyCallback(String* name,
4513 Object* structure,
4514 PropertyAttributes attributes) {
Leon Clarkef7060e22010-06-03 12:02:55 +01004515 // Normalize object to make this operation simple.
John Reck59135872010-11-02 12:39:01 -07004516 { MaybeObject* maybe_ok = NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004517 if (maybe_ok->IsFailure()) return maybe_ok;
John Reck59135872010-11-02 12:39:01 -07004518 }
Leon Clarkef7060e22010-06-03 12:02:55 +01004519
4520 // For the global object allocate a new map to invalidate the global inline
4521 // caches which have a global property cell reference directly in the code.
4522 if (IsGlobalObject()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004523 Map* new_map;
John Reck59135872010-11-02 12:39:01 -07004524 { MaybeObject* maybe_new_map = map()->CopyDropDescriptors();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004525 if (!maybe_new_map->To(&new_map)) return maybe_new_map;
John Reck59135872010-11-02 12:39:01 -07004526 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004527 set_map(new_map);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004528 // When running crankshaft, changing the map is not enough. We
4529 // need to deoptimize all functions that rely on this global
4530 // object.
4531 Deoptimizer::DeoptimizeGlobalObject(this);
Leon Clarkef7060e22010-06-03 12:02:55 +01004532 }
4533
4534 // Update the dictionary with the new CALLBACKS property.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004535 PropertyDetails details = PropertyDetails(attributes, CALLBACKS);
4536 { MaybeObject* maybe_ok = SetNormalizedProperty(name, structure, details);
4537 if (maybe_ok->IsFailure()) return maybe_ok;
John Reck59135872010-11-02 12:39:01 -07004538 }
Leon Clarkef7060e22010-06-03 12:02:55 +01004539
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004540 return GetHeap()->undefined_value();
4541}
4542
4543
4544void JSObject::DefineAccessor(Handle<JSObject> object,
4545 Handle<String> name,
4546 Handle<Object> getter,
4547 Handle<Object> setter,
4548 PropertyAttributes attributes) {
4549 CALL_HEAP_FUNCTION_VOID(
4550 object->GetIsolate(),
4551 object->DefineAccessor(*name, *getter, *setter, attributes));
Leon Clarkef7060e22010-06-03 12:02:55 +01004552}
4553
John Reck59135872010-11-02 12:39:01 -07004554MaybeObject* JSObject::DefineAccessor(String* name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004555 Object* getter,
4556 Object* setter,
John Reck59135872010-11-02 12:39:01 -07004557 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +01004558 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004559 // Check access rights if needed.
4560 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01004561 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
4562 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
4563 return isolate->heap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004564 }
4565
4566 if (IsJSGlobalProxy()) {
4567 Object* proto = GetPrototype();
4568 if (proto->IsNull()) return this;
4569 ASSERT(proto->IsJSGlobalObject());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004570 return JSObject::cast(proto)->DefineAccessor(
4571 name, getter, setter, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00004572 }
4573
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004574 // Make sure that the top context does not change when doing callbacks or
4575 // interceptor calls.
4576 AssertNoContextChange ncc;
4577
4578 // Try to flatten before operating on the string.
4579 name->TryFlatten();
4580
4581 if (!CanSetCallback(name)) return isolate->heap()->undefined_value();
4582
4583 uint32_t index = 0;
4584 return name->AsArrayIndex(&index) ?
4585 DefineElementAccessor(index, getter, setter, attributes) :
4586 DefinePropertyAccessor(name, getter, setter, attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00004587}
4588
4589
John Reck59135872010-11-02 12:39:01 -07004590MaybeObject* JSObject::DefineAccessor(AccessorInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +01004591 Isolate* isolate = GetIsolate();
Leon Clarkef7060e22010-06-03 12:02:55 +01004592 String* name = String::cast(info->name());
4593 // Check access rights if needed.
4594 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01004595 !isolate->MayNamedAccess(this, name, v8::ACCESS_SET)) {
4596 isolate->ReportFailedAccessCheck(this, v8::ACCESS_SET);
4597 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01004598 }
4599
4600 if (IsJSGlobalProxy()) {
4601 Object* proto = GetPrototype();
4602 if (proto->IsNull()) return this;
4603 ASSERT(proto->IsJSGlobalObject());
4604 return JSObject::cast(proto)->DefineAccessor(info);
4605 }
4606
4607 // Make sure that the top context does not change when doing callbacks or
4608 // interceptor calls.
4609 AssertNoContextChange ncc;
4610
4611 // Try to flatten before operating on the string.
4612 name->TryFlatten();
4613
4614 if (!CanSetCallback(name)) {
Steve Block44f0eee2011-05-26 01:26:41 +01004615 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01004616 }
4617
4618 uint32_t index = 0;
4619 bool is_element = name->AsArrayIndex(&index);
4620
4621 if (is_element) {
Steve Block44f0eee2011-05-26 01:26:41 +01004622 if (IsJSArray()) return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01004623
4624 // Accessors overwrite previous callbacks (cf. with getters/setters).
4625 switch (GetElementsKind()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004626 case FAST_SMI_ONLY_ELEMENTS:
Leon Clarkef7060e22010-06-03 12:02:55 +01004627 case FAST_ELEMENTS:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004628 case FAST_DOUBLE_ELEMENTS:
Leon Clarkef7060e22010-06-03 12:02:55 +01004629 break;
Steve Block44f0eee2011-05-26 01:26:41 +01004630 case EXTERNAL_PIXEL_ELEMENTS:
Leon Clarkef7060e22010-06-03 12:02:55 +01004631 case EXTERNAL_BYTE_ELEMENTS:
4632 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
4633 case EXTERNAL_SHORT_ELEMENTS:
4634 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
4635 case EXTERNAL_INT_ELEMENTS:
4636 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
4637 case EXTERNAL_FLOAT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00004638 case EXTERNAL_DOUBLE_ELEMENTS:
Leon Clarkef7060e22010-06-03 12:02:55 +01004639 // Ignore getters and setters on pixel and external array
4640 // elements.
Steve Block44f0eee2011-05-26 01:26:41 +01004641 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01004642 case DICTIONARY_ELEMENTS:
4643 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004644 case NON_STRICT_ARGUMENTS_ELEMENTS:
4645 UNIMPLEMENTED();
Leon Clarkef7060e22010-06-03 12:02:55 +01004646 break;
4647 }
4648
John Reck59135872010-11-02 12:39:01 -07004649 { MaybeObject* maybe_ok =
4650 SetElementCallback(index, info, info->property_attributes());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004651 if (maybe_ok->IsFailure()) return maybe_ok;
John Reck59135872010-11-02 12:39:01 -07004652 }
Leon Clarkef7060e22010-06-03 12:02:55 +01004653 } else {
4654 // Lookup the name.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004655 LookupResult result(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01004656 LocalLookup(name, &result);
4657 // ES5 forbids turning a property into an accessor if it's not
4658 // configurable (that is IsDontDelete in ES3 and v8), see 8.6.1 (Table 5).
4659 if (result.IsProperty() && (result.IsReadOnly() || result.IsDontDelete())) {
Steve Block44f0eee2011-05-26 01:26:41 +01004660 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01004661 }
John Reck59135872010-11-02 12:39:01 -07004662 { MaybeObject* maybe_ok =
4663 SetPropertyCallback(name, info, info->property_attributes());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004664 if (maybe_ok->IsFailure()) return maybe_ok;
John Reck59135872010-11-02 12:39:01 -07004665 }
Leon Clarkef7060e22010-06-03 12:02:55 +01004666 }
4667
4668 return this;
4669}
4670
4671
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004672Object* JSObject::LookupAccessor(String* name, AccessorComponent component) {
Steve Block44f0eee2011-05-26 01:26:41 +01004673 Heap* heap = GetHeap();
4674
Steve Blocka7e24c12009-10-30 11:49:00 +00004675 // Make sure that the top context does not change when doing callbacks or
4676 // interceptor calls.
4677 AssertNoContextChange ncc;
4678
4679 // Check access rights if needed.
4680 if (IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +01004681 !heap->isolate()->MayNamedAccess(this, name, v8::ACCESS_HAS)) {
4682 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
4683 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004684 }
4685
4686 // Make the lookup and include prototypes.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004687 uint32_t index = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004688 if (name->AsArrayIndex(&index)) {
4689 for (Object* obj = this;
Steve Block44f0eee2011-05-26 01:26:41 +01004690 obj != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004691 obj = JSObject::cast(obj)->GetPrototype()) {
4692 JSObject* js_object = JSObject::cast(obj);
4693 if (js_object->HasDictionaryElements()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004694 SeededNumberDictionary* dictionary = js_object->element_dictionary();
Steve Blocka7e24c12009-10-30 11:49:00 +00004695 int entry = dictionary->FindEntry(index);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004696 if (entry != SeededNumberDictionary::kNotFound) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004697 Object* element = dictionary->ValueAt(entry);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004698 if (dictionary->DetailsAt(entry).type() == CALLBACKS &&
4699 element->IsAccessorPair()) {
4700 return AccessorPair::cast(element)->GetComponent(component);
Steve Blocka7e24c12009-10-30 11:49:00 +00004701 }
4702 }
4703 }
4704 }
4705 } else {
4706 for (Object* obj = this;
Steve Block44f0eee2011-05-26 01:26:41 +01004707 obj != heap->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004708 obj = JSObject::cast(obj)->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004709 LookupResult result(heap->isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00004710 JSObject::cast(obj)->LocalLookup(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +00004711 if (result.IsProperty()) {
Steve Block44f0eee2011-05-26 01:26:41 +01004712 if (result.IsReadOnly()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004713 if (result.type() == CALLBACKS) {
4714 Object* obj = result.GetCallbackObject();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004715 if (obj->IsAccessorPair()) {
4716 return AccessorPair::cast(obj)->GetComponent(component);
Steve Blocka7e24c12009-10-30 11:49:00 +00004717 }
4718 }
4719 }
4720 }
4721 }
Steve Block44f0eee2011-05-26 01:26:41 +01004722 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004723}
4724
4725
4726Object* JSObject::SlowReverseLookup(Object* value) {
4727 if (HasFastProperties()) {
4728 DescriptorArray* descs = map()->instance_descriptors();
4729 for (int i = 0; i < descs->number_of_descriptors(); i++) {
4730 if (descs->GetType(i) == FIELD) {
4731 if (FastPropertyAt(descs->GetFieldIndex(i)) == value) {
4732 return descs->GetKey(i);
4733 }
4734 } else if (descs->GetType(i) == CONSTANT_FUNCTION) {
4735 if (descs->GetConstantFunction(i) == value) {
4736 return descs->GetKey(i);
4737 }
4738 }
4739 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004740 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00004741 } else {
4742 return property_dictionary()->SlowReverseLookup(value);
4743 }
4744}
4745
4746
John Reck59135872010-11-02 12:39:01 -07004747MaybeObject* Map::CopyDropDescriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01004748 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -07004749 Object* result;
4750 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01004751 heap->AllocateMap(instance_type(), instance_size());
John Reck59135872010-11-02 12:39:01 -07004752 if (!maybe_result->ToObject(&result)) return maybe_result;
4753 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004754 Map::cast(result)->set_prototype(prototype());
4755 Map::cast(result)->set_constructor(constructor());
4756 // Don't copy descriptors, so map transitions always remain a forest.
4757 // If we retained the same descriptors we would have two maps
4758 // pointing to the same transition which is bad because the garbage
4759 // collector relies on being able to reverse pointers from transitions
4760 // to maps. If properties need to be retained use CopyDropTransitions.
Ben Murdoch257744e2011-11-30 15:57:28 +00004761 Map::cast(result)->clear_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00004762 // Please note instance_type and instance_size are set when allocated.
4763 Map::cast(result)->set_inobject_properties(inobject_properties());
4764 Map::cast(result)->set_unused_property_fields(unused_property_fields());
4765
4766 // If the map has pre-allocated properties always start out with a descriptor
4767 // array describing these properties.
4768 if (pre_allocated_property_fields() > 0) {
4769 ASSERT(constructor()->IsJSFunction());
4770 JSFunction* ctor = JSFunction::cast(constructor());
John Reck59135872010-11-02 12:39:01 -07004771 Object* descriptors;
4772 { MaybeObject* maybe_descriptors =
4773 ctor->initial_map()->instance_descriptors()->RemoveTransitions();
4774 if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors;
4775 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004776 Map::cast(result)->set_instance_descriptors(
4777 DescriptorArray::cast(descriptors));
4778 Map::cast(result)->set_pre_allocated_property_fields(
4779 pre_allocated_property_fields());
4780 }
4781 Map::cast(result)->set_bit_field(bit_field());
4782 Map::cast(result)->set_bit_field2(bit_field2());
Ben Murdoch257744e2011-11-30 15:57:28 +00004783 Map::cast(result)->set_bit_field3(bit_field3());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004784 Map::cast(result)->set_is_shared(false);
Steve Block44f0eee2011-05-26 01:26:41 +01004785 Map::cast(result)->ClearCodeCache(heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00004786 return result;
4787}
4788
4789
John Reck59135872010-11-02 12:39:01 -07004790MaybeObject* Map::CopyNormalized(PropertyNormalizationMode mode,
4791 NormalizedMapSharingMode sharing) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004792 int new_instance_size = instance_size();
4793 if (mode == CLEAR_INOBJECT_PROPERTIES) {
4794 new_instance_size -= inobject_properties() * kPointerSize;
4795 }
4796
John Reck59135872010-11-02 12:39:01 -07004797 Object* result;
4798 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01004799 GetHeap()->AllocateMap(instance_type(), new_instance_size);
John Reck59135872010-11-02 12:39:01 -07004800 if (!maybe_result->ToObject(&result)) return maybe_result;
4801 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004802
4803 if (mode != CLEAR_INOBJECT_PROPERTIES) {
4804 Map::cast(result)->set_inobject_properties(inobject_properties());
4805 }
4806
4807 Map::cast(result)->set_prototype(prototype());
4808 Map::cast(result)->set_constructor(constructor());
4809
4810 Map::cast(result)->set_bit_field(bit_field());
4811 Map::cast(result)->set_bit_field2(bit_field2());
Ben Murdoch257744e2011-11-30 15:57:28 +00004812 Map::cast(result)->set_bit_field3(bit_field3());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004813
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004814 Map::cast(result)->set_is_shared(sharing == SHARED_NORMALIZED_MAP);
4815
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004816#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004817 if (FLAG_verify_heap && Map::cast(result)->is_shared()) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004818 Map::cast(result)->SharedMapVerify();
4819 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004820#endif
4821
4822 return result;
4823}
4824
4825
John Reck59135872010-11-02 12:39:01 -07004826MaybeObject* Map::CopyDropTransitions() {
4827 Object* new_map;
4828 { MaybeObject* maybe_new_map = CopyDropDescriptors();
4829 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
4830 }
4831 Object* descriptors;
4832 { MaybeObject* maybe_descriptors =
4833 instance_descriptors()->RemoveTransitions();
4834 if (!maybe_descriptors->ToObject(&descriptors)) return maybe_descriptors;
4835 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004836 cast(new_map)->set_instance_descriptors(DescriptorArray::cast(descriptors));
Steve Block8defd9f2010-07-08 12:39:36 +01004837 return new_map;
Steve Blocka7e24c12009-10-30 11:49:00 +00004838}
4839
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004840void Map::UpdateCodeCache(Handle<Map> map,
4841 Handle<String> name,
4842 Handle<Code> code) {
4843 Isolate* isolate = map->GetIsolate();
4844 CALL_HEAP_FUNCTION_VOID(isolate,
4845 map->UpdateCodeCache(*name, *code));
4846}
Steve Blocka7e24c12009-10-30 11:49:00 +00004847
John Reck59135872010-11-02 12:39:01 -07004848MaybeObject* Map::UpdateCodeCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01004849 // Allocate the code cache if not present.
4850 if (code_cache()->IsFixedArray()) {
John Reck59135872010-11-02 12:39:01 -07004851 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004852 { MaybeObject* maybe_result = GetHeap()->AllocateCodeCache();
John Reck59135872010-11-02 12:39:01 -07004853 if (!maybe_result->ToObject(&result)) return maybe_result;
4854 }
Steve Block6ded16b2010-05-10 14:33:55 +01004855 set_code_cache(result);
4856 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004857
Steve Block6ded16b2010-05-10 14:33:55 +01004858 // Update the code cache.
4859 return CodeCache::cast(code_cache())->Update(name, code);
4860}
4861
4862
4863Object* Map::FindInCodeCache(String* name, Code::Flags flags) {
4864 // Do a lookup if a code cache exists.
4865 if (!code_cache()->IsFixedArray()) {
4866 return CodeCache::cast(code_cache())->Lookup(name, flags);
4867 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004868 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01004869 }
4870}
4871
4872
4873int Map::IndexInCodeCache(Object* name, Code* code) {
4874 // Get the internal index if a code cache exists.
4875 if (!code_cache()->IsFixedArray()) {
4876 return CodeCache::cast(code_cache())->GetIndex(name, code);
4877 }
4878 return -1;
4879}
4880
4881
4882void Map::RemoveFromCodeCache(String* name, Code* code, int index) {
4883 // No GC is supposed to happen between a call to IndexInCodeCache and
4884 // RemoveFromCodeCache so the code cache must be there.
4885 ASSERT(!code_cache()->IsFixedArray());
4886 CodeCache::cast(code_cache())->RemoveByIndex(name, code, index);
4887}
4888
4889
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004890// An iterator over all map transitions in an descriptor array, reusing the map
4891// field of the contens array while it is running.
4892class IntrusiveMapTransitionIterator {
4893 public:
4894 explicit IntrusiveMapTransitionIterator(DescriptorArray* descriptor_array)
4895 : descriptor_array_(descriptor_array) { }
4896
4897 void Start() {
4898 ASSERT(!IsIterating());
4899 if (HasContentArray()) *ContentHeader() = Smi::FromInt(0);
4900 }
4901
4902 bool IsIterating() {
4903 return HasContentArray() && (*ContentHeader())->IsSmi();
4904 }
4905
4906 Map* Next() {
4907 ASSERT(IsIterating());
4908 FixedArray* contents = ContentArray();
4909 // Attention, tricky index manipulation ahead: Every entry in the contents
4910 // array consists of a value/details pair, so the index is typically even.
4911 // An exception is made for CALLBACKS entries: An even index means we look
4912 // at its getter, and an odd index means we look at its setter.
4913 int index = Smi::cast(*ContentHeader())->value();
4914 while (index < contents->length()) {
4915 PropertyDetails details(Smi::cast(contents->get(index | 1)));
4916 switch (details.type()) {
4917 case MAP_TRANSITION:
4918 case CONSTANT_TRANSITION:
4919 case ELEMENTS_TRANSITION:
4920 // We definitely have a map transition.
4921 *ContentHeader() = Smi::FromInt(index + 2);
4922 return static_cast<Map*>(contents->get(index));
4923 case CALLBACKS: {
4924 // We might have a map transition in a getter or in a setter.
4925 AccessorPair* accessors =
4926 static_cast<AccessorPair*>(contents->get(index & ~1));
4927 Object* accessor =
4928 ((index & 1) == 0) ? accessors->getter() : accessors->setter();
4929 index++;
4930 if (accessor->IsMap()) {
4931 *ContentHeader() = Smi::FromInt(index);
4932 return static_cast<Map*>(accessor);
4933 }
Steve Block053d10c2011-06-13 19:13:29 +01004934 break;
4935 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004936 case NORMAL:
4937 case FIELD:
4938 case CONSTANT_FUNCTION:
4939 case HANDLER:
4940 case INTERCEPTOR:
4941 case NULL_DESCRIPTOR:
4942 // We definitely have no map transition.
4943 index += 2;
4944 break;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004945 }
4946 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004947 *ContentHeader() = descriptor_array_->GetHeap()->fixed_array_map();
4948 return NULL;
4949 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00004950
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004951 private:
4952 bool HasContentArray() {
4953 return descriptor_array_-> length() > DescriptorArray::kContentArrayIndex;
4954 }
4955
4956 FixedArray* ContentArray() {
4957 Object* array = descriptor_array_->get(DescriptorArray::kContentArrayIndex);
4958 return static_cast<FixedArray*>(array);
4959 }
4960
4961 Object** ContentHeader() {
4962 return HeapObject::RawField(ContentArray(), DescriptorArray::kMapOffset);
4963 }
4964
4965 DescriptorArray* descriptor_array_;
4966};
4967
4968
4969// An iterator over all prototype transitions, reusing the map field of the
4970// underlying array while it is running.
4971class IntrusivePrototypeTransitionIterator {
4972 public:
4973 explicit IntrusivePrototypeTransitionIterator(FixedArray* proto_trans)
4974 : proto_trans_(proto_trans) { }
4975
4976 void Start() {
4977 ASSERT(!IsIterating());
4978 if (HasTransitions()) *Header() = Smi::FromInt(0);
4979 }
4980
4981 bool IsIterating() {
4982 return HasTransitions() && (*Header())->IsSmi();
4983 }
4984
4985 Map* Next() {
4986 ASSERT(IsIterating());
4987 int transitionNumber = Smi::cast(*Header())->value();
4988 if (transitionNumber < NumberOfTransitions()) {
4989 *Header() = Smi::FromInt(transitionNumber + 1);
4990 return GetTransition(transitionNumber);
4991 }
4992 *Header() = proto_trans_->GetHeap()->fixed_array_map();
4993 return NULL;
4994 }
4995
4996 private:
4997 bool HasTransitions() {
4998 return proto_trans_->length() >= Map::kProtoTransitionHeaderSize;
4999 }
5000
5001 Object** Header() {
5002 return HeapObject::RawField(proto_trans_, FixedArray::kMapOffset);
5003 }
5004
5005 int NumberOfTransitions() {
5006 Object* num = proto_trans_->get(Map::kProtoTransitionNumberOfEntriesOffset);
5007 return Smi::cast(num)->value();
5008 }
5009
5010 Map* GetTransition(int transitionNumber) {
5011 return Map::cast(proto_trans_->get(IndexFor(transitionNumber)));
5012 }
5013
5014 int IndexFor(int transitionNumber) {
5015 return Map::kProtoTransitionHeaderSize +
5016 Map::kProtoTransitionMapOffset +
5017 transitionNumber * Map::kProtoTransitionElementsPerEntry;
5018 }
5019
5020 FixedArray* proto_trans_;
5021};
5022
5023
5024// To traverse the transition tree iteratively, we have to store two kinds of
5025// information in a map: The parent map in the traversal and which children of a
5026// node have already been visited. To do this without additional memory, we
5027// temporarily reuse two maps with known values:
5028//
5029// (1) The map of the map temporarily holds the parent, and is restored to the
5030// meta map afterwards.
5031//
5032// (2) The info which children have already been visited depends on which part
5033// of the map we currently iterate:
5034//
5035// (a) If we currently follow normal map transitions, we temporarily store
5036// the current index in the map of the FixedArray of the desciptor
5037// array's contents, and restore it to the fixed array map afterwards.
5038// Note that a single descriptor can have 0, 1, or 2 transitions.
5039//
5040// (b) If we currently follow prototype transitions, we temporarily store
5041// the current index in the map of the FixedArray holding the prototype
5042// transitions, and restore it to the fixed array map afterwards.
5043//
5044// Note that the child iterator is just a concatenation of two iterators: One
5045// iterating over map transitions and one iterating over prototype transisitons.
5046class TraversableMap : public Map {
5047 public:
5048 // Record the parent in the traversal within this map. Note that this destroys
5049 // this map's map!
5050 void SetParent(TraversableMap* parent) { set_map_no_write_barrier(parent); }
5051
5052 // Reset the current map's map, returning the parent previously stored in it.
5053 TraversableMap* GetAndResetParent() {
5054 TraversableMap* old_parent = static_cast<TraversableMap*>(map());
5055 set_map_no_write_barrier(GetHeap()->meta_map());
5056 return old_parent;
5057 }
5058
5059 // Start iterating over this map's children, possibly destroying a FixedArray
5060 // map (see explanation above).
5061 void ChildIteratorStart() {
5062 IntrusiveMapTransitionIterator(instance_descriptors()).Start();
5063 IntrusivePrototypeTransitionIterator(
5064 unchecked_prototype_transitions()).Start();
5065 }
5066
5067 // If we have an unvisited child map, return that one and advance. If we have
5068 // none, return NULL and reset any destroyed FixedArray maps.
5069 TraversableMap* ChildIteratorNext() {
5070 IntrusiveMapTransitionIterator descriptor_iterator(instance_descriptors());
5071 if (descriptor_iterator.IsIterating()) {
5072 Map* next = descriptor_iterator.Next();
5073 if (next != NULL) return static_cast<TraversableMap*>(next);
5074 }
5075 IntrusivePrototypeTransitionIterator
5076 proto_iterator(unchecked_prototype_transitions());
5077 if (proto_iterator.IsIterating()) {
5078 Map* next = proto_iterator.Next();
5079 if (next != NULL) return static_cast<TraversableMap*>(next);
5080 }
5081 return NULL;
5082 }
5083};
5084
5085
5086// Traverse the transition tree in postorder without using the C++ stack by
5087// doing pointer reversal.
5088void Map::TraverseTransitionTree(TraverseCallback callback, void* data) {
5089 TraversableMap* current = static_cast<TraversableMap*>(this);
5090 current->ChildIteratorStart();
5091 while (true) {
5092 TraversableMap* child = current->ChildIteratorNext();
5093 if (child != NULL) {
5094 child->ChildIteratorStart();
5095 child->SetParent(current);
5096 current = child;
5097 } else {
5098 TraversableMap* parent = current->GetAndResetParent();
5099 callback(current, data);
5100 if (current == this) break;
5101 current = parent;
5102 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005103 }
5104}
5105
5106
John Reck59135872010-11-02 12:39:01 -07005107MaybeObject* CodeCache::Update(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01005108 // The number of monomorphic stubs for normal load/store/call IC's can grow to
5109 // a large number and therefore they need to go into a hash table. They are
5110 // used to load global properties from cells.
5111 if (code->type() == NORMAL) {
5112 // Make sure that a hash table is allocated for the normal load code cache.
5113 if (normal_type_cache()->IsUndefined()) {
John Reck59135872010-11-02 12:39:01 -07005114 Object* result;
5115 { MaybeObject* maybe_result =
5116 CodeCacheHashTable::Allocate(CodeCacheHashTable::kInitialSize);
5117 if (!maybe_result->ToObject(&result)) return maybe_result;
5118 }
Steve Block6ded16b2010-05-10 14:33:55 +01005119 set_normal_type_cache(result);
5120 }
5121 return UpdateNormalTypeCache(name, code);
5122 } else {
5123 ASSERT(default_cache()->IsFixedArray());
5124 return UpdateDefaultCache(name, code);
5125 }
5126}
5127
5128
John Reck59135872010-11-02 12:39:01 -07005129MaybeObject* CodeCache::UpdateDefaultCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01005130 // When updating the default code cache we disregard the type encoded in the
Steve Blocka7e24c12009-10-30 11:49:00 +00005131 // flags. This allows call constant stubs to overwrite call field
5132 // stubs, etc.
5133 Code::Flags flags = Code::RemoveTypeFromFlags(code->flags());
5134
5135 // First check whether we can update existing code cache without
5136 // extending it.
Steve Block6ded16b2010-05-10 14:33:55 +01005137 FixedArray* cache = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +00005138 int length = cache->length();
5139 int deleted_index = -1;
Steve Block6ded16b2010-05-10 14:33:55 +01005140 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005141 Object* key = cache->get(i);
5142 if (key->IsNull()) {
5143 if (deleted_index < 0) deleted_index = i;
5144 continue;
5145 }
5146 if (key->IsUndefined()) {
5147 if (deleted_index >= 0) i = deleted_index;
Steve Block6ded16b2010-05-10 14:33:55 +01005148 cache->set(i + kCodeCacheEntryNameOffset, name);
5149 cache->set(i + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005150 return this;
5151 }
5152 if (name->Equals(String::cast(key))) {
Steve Block6ded16b2010-05-10 14:33:55 +01005153 Code::Flags found =
5154 Code::cast(cache->get(i + kCodeCacheEntryCodeOffset))->flags();
Steve Blocka7e24c12009-10-30 11:49:00 +00005155 if (Code::RemoveTypeFromFlags(found) == flags) {
Steve Block6ded16b2010-05-10 14:33:55 +01005156 cache->set(i + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005157 return this;
5158 }
5159 }
5160 }
5161
5162 // Reached the end of the code cache. If there were deleted
5163 // elements, reuse the space for the first of them.
5164 if (deleted_index >= 0) {
Steve Block6ded16b2010-05-10 14:33:55 +01005165 cache->set(deleted_index + kCodeCacheEntryNameOffset, name);
5166 cache->set(deleted_index + kCodeCacheEntryCodeOffset, code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005167 return this;
5168 }
5169
Steve Block6ded16b2010-05-10 14:33:55 +01005170 // Extend the code cache with some new entries (at least one). Must be a
5171 // multiple of the entry size.
5172 int new_length = length + ((length >> 1)) + kCodeCacheEntrySize;
5173 new_length = new_length - new_length % kCodeCacheEntrySize;
5174 ASSERT((new_length % kCodeCacheEntrySize) == 0);
John Reck59135872010-11-02 12:39:01 -07005175 Object* result;
5176 { MaybeObject* maybe_result = cache->CopySize(new_length);
5177 if (!maybe_result->ToObject(&result)) return maybe_result;
5178 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005179
5180 // Add the (name, code) pair to the new cache.
5181 cache = FixedArray::cast(result);
Steve Block6ded16b2010-05-10 14:33:55 +01005182 cache->set(length + kCodeCacheEntryNameOffset, name);
5183 cache->set(length + kCodeCacheEntryCodeOffset, code);
5184 set_default_cache(cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00005185 return this;
5186}
5187
5188
John Reck59135872010-11-02 12:39:01 -07005189MaybeObject* CodeCache::UpdateNormalTypeCache(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01005190 // Adding a new entry can cause a new cache to be allocated.
5191 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
John Reck59135872010-11-02 12:39:01 -07005192 Object* new_cache;
5193 { MaybeObject* maybe_new_cache = cache->Put(name, code);
5194 if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache;
5195 }
Steve Block6ded16b2010-05-10 14:33:55 +01005196 set_normal_type_cache(new_cache);
5197 return this;
5198}
5199
5200
5201Object* CodeCache::Lookup(String* name, Code::Flags flags) {
5202 if (Code::ExtractTypeFromFlags(flags) == NORMAL) {
5203 return LookupNormalTypeCache(name, flags);
5204 } else {
5205 return LookupDefaultCache(name, flags);
5206 }
5207}
5208
5209
5210Object* CodeCache::LookupDefaultCache(String* name, Code::Flags flags) {
5211 FixedArray* cache = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +00005212 int length = cache->length();
Steve Block6ded16b2010-05-10 14:33:55 +01005213 for (int i = 0; i < length; i += kCodeCacheEntrySize) {
5214 Object* key = cache->get(i + kCodeCacheEntryNameOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00005215 // Skip deleted elements.
5216 if (key->IsNull()) continue;
5217 if (key->IsUndefined()) return key;
5218 if (name->Equals(String::cast(key))) {
Steve Block6ded16b2010-05-10 14:33:55 +01005219 Code* code = Code::cast(cache->get(i + kCodeCacheEntryCodeOffset));
5220 if (code->flags() == flags) {
5221 return code;
5222 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005223 }
5224 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01005225 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00005226}
5227
5228
Steve Block6ded16b2010-05-10 14:33:55 +01005229Object* CodeCache::LookupNormalTypeCache(String* name, Code::Flags flags) {
5230 if (!normal_type_cache()->IsUndefined()) {
5231 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
5232 return cache->Lookup(name, flags);
5233 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005234 return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01005235 }
5236}
5237
5238
5239int CodeCache::GetIndex(Object* name, Code* code) {
5240 if (code->type() == NORMAL) {
5241 if (normal_type_cache()->IsUndefined()) return -1;
5242 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
5243 return cache->GetIndex(String::cast(name), code->flags());
5244 }
5245
5246 FixedArray* array = default_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +00005247 int len = array->length();
Steve Block6ded16b2010-05-10 14:33:55 +01005248 for (int i = 0; i < len; i += kCodeCacheEntrySize) {
5249 if (array->get(i + kCodeCacheEntryCodeOffset) == code) return i + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00005250 }
5251 return -1;
5252}
5253
5254
Steve Block6ded16b2010-05-10 14:33:55 +01005255void CodeCache::RemoveByIndex(Object* name, Code* code, int index) {
5256 if (code->type() == NORMAL) {
5257 ASSERT(!normal_type_cache()->IsUndefined());
5258 CodeCacheHashTable* cache = CodeCacheHashTable::cast(normal_type_cache());
5259 ASSERT(cache->GetIndex(String::cast(name), code->flags()) == index);
5260 cache->RemoveByIndex(index);
5261 } else {
5262 FixedArray* array = default_cache();
5263 ASSERT(array->length() >= index && array->get(index)->IsCode());
5264 // Use null instead of undefined for deleted elements to distinguish
5265 // deleted elements from unused elements. This distinction is used
5266 // when looking up in the cache and when updating the cache.
5267 ASSERT_EQ(1, kCodeCacheEntryCodeOffset - kCodeCacheEntryNameOffset);
5268 array->set_null(index - 1); // Name.
5269 array->set_null(index); // Code.
5270 }
5271}
5272
5273
5274// The key in the code cache hash table consists of the property name and the
5275// code object. The actual match is on the name and the code flags. If a key
5276// is created using the flags and not a code object it can only be used for
5277// lookup not to create a new entry.
5278class CodeCacheHashTableKey : public HashTableKey {
5279 public:
5280 CodeCacheHashTableKey(String* name, Code::Flags flags)
5281 : name_(name), flags_(flags), code_(NULL) { }
5282
5283 CodeCacheHashTableKey(String* name, Code* code)
5284 : name_(name),
5285 flags_(code->flags()),
5286 code_(code) { }
5287
5288
5289 bool IsMatch(Object* other) {
5290 if (!other->IsFixedArray()) return false;
5291 FixedArray* pair = FixedArray::cast(other);
5292 String* name = String::cast(pair->get(0));
5293 Code::Flags flags = Code::cast(pair->get(1))->flags();
5294 if (flags != flags_) {
5295 return false;
5296 }
5297 return name_->Equals(name);
5298 }
5299
5300 static uint32_t NameFlagsHashHelper(String* name, Code::Flags flags) {
5301 return name->Hash() ^ flags;
5302 }
5303
5304 uint32_t Hash() { return NameFlagsHashHelper(name_, flags_); }
5305
5306 uint32_t HashForObject(Object* obj) {
5307 FixedArray* pair = FixedArray::cast(obj);
5308 String* name = String::cast(pair->get(0));
5309 Code* code = Code::cast(pair->get(1));
5310 return NameFlagsHashHelper(name, code->flags());
5311 }
5312
John Reck59135872010-11-02 12:39:01 -07005313 MUST_USE_RESULT MaybeObject* AsObject() {
Steve Block6ded16b2010-05-10 14:33:55 +01005314 ASSERT(code_ != NULL);
John Reck59135872010-11-02 12:39:01 -07005315 Object* obj;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005316 { MaybeObject* maybe_obj = code_->GetHeap()->AllocateFixedArray(2);
John Reck59135872010-11-02 12:39:01 -07005317 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5318 }
Steve Block6ded16b2010-05-10 14:33:55 +01005319 FixedArray* pair = FixedArray::cast(obj);
5320 pair->set(0, name_);
5321 pair->set(1, code_);
5322 return pair;
5323 }
5324
5325 private:
5326 String* name_;
5327 Code::Flags flags_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005328 // TODO(jkummerow): We should be able to get by without this.
Steve Block6ded16b2010-05-10 14:33:55 +01005329 Code* code_;
5330};
5331
5332
5333Object* CodeCacheHashTable::Lookup(String* name, Code::Flags flags) {
5334 CodeCacheHashTableKey key(name, flags);
5335 int entry = FindEntry(&key);
Steve Block44f0eee2011-05-26 01:26:41 +01005336 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Block6ded16b2010-05-10 14:33:55 +01005337 return get(EntryToIndex(entry) + 1);
5338}
5339
5340
John Reck59135872010-11-02 12:39:01 -07005341MaybeObject* CodeCacheHashTable::Put(String* name, Code* code) {
Steve Block6ded16b2010-05-10 14:33:55 +01005342 CodeCacheHashTableKey key(name, code);
John Reck59135872010-11-02 12:39:01 -07005343 Object* obj;
5344 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
5345 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5346 }
Steve Block6ded16b2010-05-10 14:33:55 +01005347
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005348 // Don't use |this|, as the table might have grown.
Steve Block6ded16b2010-05-10 14:33:55 +01005349 CodeCacheHashTable* cache = reinterpret_cast<CodeCacheHashTable*>(obj);
5350
5351 int entry = cache->FindInsertionEntry(key.Hash());
John Reck59135872010-11-02 12:39:01 -07005352 Object* k;
5353 { MaybeObject* maybe_k = key.AsObject();
5354 if (!maybe_k->ToObject(&k)) return maybe_k;
5355 }
Steve Block6ded16b2010-05-10 14:33:55 +01005356
5357 cache->set(EntryToIndex(entry), k);
5358 cache->set(EntryToIndex(entry) + 1, code);
5359 cache->ElementAdded();
5360 return cache;
5361}
5362
5363
5364int CodeCacheHashTable::GetIndex(String* name, Code::Flags flags) {
5365 CodeCacheHashTableKey key(name, flags);
5366 int entry = FindEntry(&key);
5367 return (entry == kNotFound) ? -1 : entry;
5368}
5369
5370
5371void CodeCacheHashTable::RemoveByIndex(int index) {
5372 ASSERT(index >= 0);
Steve Block44f0eee2011-05-26 01:26:41 +01005373 Heap* heap = GetHeap();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005374 set(EntryToIndex(index), heap->the_hole_value());
5375 set(EntryToIndex(index) + 1, heap->the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +01005376 ElementRemoved();
Steve Blocka7e24c12009-10-30 11:49:00 +00005377}
5378
5379
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005380void PolymorphicCodeCache::Update(Handle<PolymorphicCodeCache> cache,
5381 MapHandleList* maps,
5382 Code::Flags flags,
5383 Handle<Code> code) {
5384 Isolate* isolate = cache->GetIsolate();
5385 CALL_HEAP_FUNCTION_VOID(isolate, cache->Update(maps, flags, *code));
5386}
5387
5388
5389MaybeObject* PolymorphicCodeCache::Update(MapHandleList* maps,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005390 Code::Flags flags,
5391 Code* code) {
5392 // Initialize cache if necessary.
5393 if (cache()->IsUndefined()) {
5394 Object* result;
5395 { MaybeObject* maybe_result =
5396 PolymorphicCodeCacheHashTable::Allocate(
5397 PolymorphicCodeCacheHashTable::kInitialSize);
5398 if (!maybe_result->ToObject(&result)) return maybe_result;
5399 }
5400 set_cache(result);
5401 } else {
5402 // This entry shouldn't be contained in the cache yet.
5403 ASSERT(PolymorphicCodeCacheHashTable::cast(cache())
5404 ->Lookup(maps, flags)->IsUndefined());
5405 }
5406 PolymorphicCodeCacheHashTable* hash_table =
5407 PolymorphicCodeCacheHashTable::cast(cache());
5408 Object* new_cache;
5409 { MaybeObject* maybe_new_cache = hash_table->Put(maps, flags, code);
5410 if (!maybe_new_cache->ToObject(&new_cache)) return maybe_new_cache;
5411 }
5412 set_cache(new_cache);
5413 return this;
5414}
5415
5416
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005417Handle<Object> PolymorphicCodeCache::Lookup(MapHandleList* maps,
5418 Code::Flags flags) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005419 if (!cache()->IsUndefined()) {
5420 PolymorphicCodeCacheHashTable* hash_table =
5421 PolymorphicCodeCacheHashTable::cast(cache());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005422 return Handle<Object>(hash_table->Lookup(maps, flags));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005423 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005424 return GetIsolate()->factory()->undefined_value();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005425 }
5426}
5427
5428
5429// Despite their name, object of this class are not stored in the actual
5430// hash table; instead they're temporarily used for lookups. It is therefore
5431// safe to have a weak (non-owning) pointer to a MapList as a member field.
5432class PolymorphicCodeCacheHashTableKey : public HashTableKey {
5433 public:
5434 // Callers must ensure that |maps| outlives the newly constructed object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005435 PolymorphicCodeCacheHashTableKey(MapHandleList* maps, int code_flags)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005436 : maps_(maps),
5437 code_flags_(code_flags) {}
5438
5439 bool IsMatch(Object* other) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005440 MapHandleList other_maps(kDefaultListAllocationSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005441 int other_flags;
5442 FromObject(other, &other_flags, &other_maps);
5443 if (code_flags_ != other_flags) return false;
5444 if (maps_->length() != other_maps.length()) return false;
5445 // Compare just the hashes first because it's faster.
5446 int this_hash = MapsHashHelper(maps_, code_flags_);
5447 int other_hash = MapsHashHelper(&other_maps, other_flags);
5448 if (this_hash != other_hash) return false;
5449
5450 // Full comparison: for each map in maps_, look for an equivalent map in
5451 // other_maps. This implementation is slow, but probably good enough for
5452 // now because the lists are short (<= 4 elements currently).
5453 for (int i = 0; i < maps_->length(); ++i) {
5454 bool match_found = false;
5455 for (int j = 0; j < other_maps.length(); ++j) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005456 if (*(maps_->at(i)) == *(other_maps.at(j))) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005457 match_found = true;
5458 break;
5459 }
5460 }
5461 if (!match_found) return false;
5462 }
5463 return true;
5464 }
5465
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005466 static uint32_t MapsHashHelper(MapHandleList* maps, int code_flags) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005467 uint32_t hash = code_flags;
5468 for (int i = 0; i < maps->length(); ++i) {
5469 hash ^= maps->at(i)->Hash();
5470 }
5471 return hash;
5472 }
5473
5474 uint32_t Hash() {
5475 return MapsHashHelper(maps_, code_flags_);
5476 }
5477
5478 uint32_t HashForObject(Object* obj) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005479 MapHandleList other_maps(kDefaultListAllocationSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005480 int other_flags;
5481 FromObject(obj, &other_flags, &other_maps);
5482 return MapsHashHelper(&other_maps, other_flags);
5483 }
5484
5485 MUST_USE_RESULT MaybeObject* AsObject() {
5486 Object* obj;
5487 // The maps in |maps_| must be copied to a newly allocated FixedArray,
5488 // both because the referenced MapList is short-lived, and because C++
5489 // objects can't be stored in the heap anyway.
5490 { MaybeObject* maybe_obj =
5491 HEAP->AllocateUninitializedFixedArray(maps_->length() + 1);
5492 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5493 }
5494 FixedArray* list = FixedArray::cast(obj);
5495 list->set(0, Smi::FromInt(code_flags_));
5496 for (int i = 0; i < maps_->length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005497 list->set(i + 1, *maps_->at(i));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005498 }
5499 return list;
5500 }
5501
5502 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005503 static MapHandleList* FromObject(Object* obj,
5504 int* code_flags,
5505 MapHandleList* maps) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005506 FixedArray* list = FixedArray::cast(obj);
5507 maps->Rewind(0);
5508 *code_flags = Smi::cast(list->get(0))->value();
5509 for (int i = 1; i < list->length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005510 maps->Add(Handle<Map>(Map::cast(list->get(i))));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005511 }
5512 return maps;
5513 }
5514
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005515 MapHandleList* maps_; // weak.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005516 int code_flags_;
5517 static const int kDefaultListAllocationSize = kMaxKeyedPolymorphism + 1;
5518};
5519
5520
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005521Object* PolymorphicCodeCacheHashTable::Lookup(MapHandleList* maps,
5522 int code_flags) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005523 PolymorphicCodeCacheHashTableKey key(maps, code_flags);
5524 int entry = FindEntry(&key);
5525 if (entry == kNotFound) return GetHeap()->undefined_value();
5526 return get(EntryToIndex(entry) + 1);
5527}
5528
5529
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005530MaybeObject* PolymorphicCodeCacheHashTable::Put(MapHandleList* maps,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005531 int code_flags,
5532 Code* code) {
5533 PolymorphicCodeCacheHashTableKey key(maps, code_flags);
5534 Object* obj;
5535 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
5536 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5537 }
5538 PolymorphicCodeCacheHashTable* cache =
5539 reinterpret_cast<PolymorphicCodeCacheHashTable*>(obj);
5540 int entry = cache->FindInsertionEntry(key.Hash());
5541 { MaybeObject* maybe_obj = key.AsObject();
5542 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5543 }
5544 cache->set(EntryToIndex(entry), obj);
5545 cache->set(EntryToIndex(entry) + 1, code);
5546 cache->ElementAdded();
5547 return cache;
5548}
5549
5550
John Reck59135872010-11-02 12:39:01 -07005551MaybeObject* FixedArray::AddKeysFromJSArray(JSArray* array) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005552 ElementsAccessor* accessor = array->GetElementsAccessor();
5553 MaybeObject* maybe_result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005554 accessor->AddElementsToFixedArray(array, array, this);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005555 FixedArray* result;
5556 if (!maybe_result->To<FixedArray>(&result)) return maybe_result;
5557#ifdef DEBUG
5558 if (FLAG_enable_slow_asserts) {
5559 for (int i = 0; i < result->length(); i++) {
5560 Object* current = result->get(i);
5561 ASSERT(current->IsNumber() || current->IsString());
Steve Blocka7e24c12009-10-30 11:49:00 +00005562 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005563 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005564#endif
5565 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00005566}
5567
5568
John Reck59135872010-11-02 12:39:01 -07005569MaybeObject* FixedArray::UnionOfKeys(FixedArray* other) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005570 ElementsAccessor* accessor = ElementsAccessor::ForArray(other);
5571 MaybeObject* maybe_result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005572 accessor->AddElementsToFixedArray(NULL, NULL, this, other);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005573 FixedArray* result;
5574 if (!maybe_result->To<FixedArray>(&result)) return maybe_result;
Ben Murdochf87a2032010-10-22 12:50:53 +01005575#ifdef DEBUG
5576 if (FLAG_enable_slow_asserts) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005577 for (int i = 0; i < result->length(); i++) {
5578 Object* current = result->get(i);
5579 ASSERT(current->IsNumber() || current->IsString());
Ben Murdochf87a2032010-10-22 12:50:53 +01005580 }
5581 }
5582#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005583 return result;
5584}
5585
5586
John Reck59135872010-11-02 12:39:01 -07005587MaybeObject* FixedArray::CopySize(int new_length) {
Steve Block44f0eee2011-05-26 01:26:41 +01005588 Heap* heap = GetHeap();
5589 if (new_length == 0) return heap->empty_fixed_array();
John Reck59135872010-11-02 12:39:01 -07005590 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +01005591 { MaybeObject* maybe_obj = heap->AllocateFixedArray(new_length);
John Reck59135872010-11-02 12:39:01 -07005592 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
5593 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005594 FixedArray* result = FixedArray::cast(obj);
5595 // Copy the content
Leon Clarke4515c472010-02-03 11:58:03 +00005596 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +00005597 int len = length();
5598 if (new_length < len) len = new_length;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005599 // We are taking the map from the old fixed array so the map is sure to
5600 // be an immortal immutable object.
5601 result->set_map_no_write_barrier(map());
Leon Clarke4515c472010-02-03 11:58:03 +00005602 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00005603 for (int i = 0; i < len; i++) {
5604 result->set(i, get(i), mode);
5605 }
5606 return result;
5607}
5608
5609
5610void FixedArray::CopyTo(int pos, FixedArray* dest, int dest_pos, int len) {
Leon Clarke4515c472010-02-03 11:58:03 +00005611 AssertNoAllocation no_gc;
5612 WriteBarrierMode mode = dest->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00005613 for (int index = 0; index < len; index++) {
5614 dest->set(dest_pos+index, get(pos+index), mode);
5615 }
5616}
5617
5618
5619#ifdef DEBUG
5620bool FixedArray::IsEqualTo(FixedArray* other) {
5621 if (length() != other->length()) return false;
5622 for (int i = 0 ; i < length(); ++i) {
5623 if (get(i) != other->get(i)) return false;
5624 }
5625 return true;
5626}
5627#endif
5628
5629
John Reck59135872010-11-02 12:39:01 -07005630MaybeObject* DescriptorArray::Allocate(int number_of_descriptors) {
Steve Block44f0eee2011-05-26 01:26:41 +01005631 Heap* heap = Isolate::Current()->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +00005632 if (number_of_descriptors == 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01005633 return heap->empty_descriptor_array();
Steve Blocka7e24c12009-10-30 11:49:00 +00005634 }
5635 // Allocate the array of keys.
John Reck59135872010-11-02 12:39:01 -07005636 Object* array;
5637 { MaybeObject* maybe_array =
Steve Block44f0eee2011-05-26 01:26:41 +01005638 heap->AllocateFixedArray(ToKeyIndex(number_of_descriptors));
John Reck59135872010-11-02 12:39:01 -07005639 if (!maybe_array->ToObject(&array)) return maybe_array;
5640 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005641 // Do not use DescriptorArray::cast on incomplete object.
5642 FixedArray* result = FixedArray::cast(array);
5643
5644 // Allocate the content array and set it in the descriptor array.
John Reck59135872010-11-02 12:39:01 -07005645 { MaybeObject* maybe_array =
Steve Block44f0eee2011-05-26 01:26:41 +01005646 heap->AllocateFixedArray(number_of_descriptors << 1);
John Reck59135872010-11-02 12:39:01 -07005647 if (!maybe_array->ToObject(&array)) return maybe_array;
5648 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005649 result->set(kBitField3StorageIndex, Smi::FromInt(0));
Steve Blocka7e24c12009-10-30 11:49:00 +00005650 result->set(kContentArrayIndex, array);
5651 result->set(kEnumerationIndexIndex,
Leon Clarke4515c472010-02-03 11:58:03 +00005652 Smi::FromInt(PropertyDetails::kInitialIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +00005653 return result;
5654}
5655
5656
5657void DescriptorArray::SetEnumCache(FixedArray* bridge_storage,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005658 FixedArray* new_cache,
5659 Object* new_index_cache) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005660 ASSERT(bridge_storage->length() >= kEnumCacheBridgeLength);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005661 ASSERT(new_index_cache->IsSmi() || new_index_cache->IsFixedArray());
Steve Blocka7e24c12009-10-30 11:49:00 +00005662 if (HasEnumCache()) {
5663 FixedArray::cast(get(kEnumerationIndexIndex))->
5664 set(kEnumCacheBridgeCacheIndex, new_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005665 FixedArray::cast(get(kEnumerationIndexIndex))->
5666 set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00005667 } else {
5668 if (IsEmpty()) return; // Do nothing for empty descriptor array.
5669 FixedArray::cast(bridge_storage)->
5670 set(kEnumCacheBridgeCacheIndex, new_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005671 FixedArray::cast(bridge_storage)->
5672 set(kEnumCacheBridgeIndicesCacheIndex, new_index_cache);
5673 NoWriteBarrierSet(FixedArray::cast(bridge_storage),
5674 kEnumCacheBridgeEnumIndex,
5675 get(kEnumerationIndexIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +00005676 set(kEnumerationIndexIndex, bridge_storage);
5677 }
5678}
5679
5680
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005681static bool InsertionPointFound(String* key1, String* key2) {
5682 return key1->Hash() > key2->Hash() || key1 == key2;
5683}
5684
5685
5686void DescriptorArray::CopyFrom(Handle<DescriptorArray> dst,
5687 int dst_index,
5688 Handle<DescriptorArray> src,
5689 int src_index,
5690 const WhitenessWitness& witness) {
5691 CALL_HEAP_FUNCTION_VOID(dst->GetIsolate(),
5692 dst->CopyFrom(dst_index, *src, src_index, witness));
5693}
5694
5695
5696MaybeObject* DescriptorArray::CopyFrom(int dst_index,
5697 DescriptorArray* src,
5698 int src_index,
5699 const WhitenessWitness& witness) {
5700 Object* value = src->GetValue(src_index);
5701 PropertyDetails details(src->GetDetails(src_index));
5702 if (details.type() == CALLBACKS && value->IsAccessorPair()) {
5703 MaybeObject* maybe_copy =
5704 AccessorPair::cast(value)->CopyWithoutTransitions();
5705 if (!maybe_copy->To(&value)) return maybe_copy;
5706 }
5707 Descriptor desc(src->GetKey(src_index), value, details);
5708 Set(dst_index, &desc, witness);
5709 return this;
5710}
5711
5712
John Reck59135872010-11-02 12:39:01 -07005713MaybeObject* DescriptorArray::CopyInsert(Descriptor* descriptor,
5714 TransitionFlag transition_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005715 // Transitions are only kept when inserting another transition.
5716 // This precondition is not required by this function's implementation, but
5717 // is currently required by the semantics of maps, so we check it.
5718 // Conversely, we filter after replacing, so replacing a transition and
5719 // removing all other transitions is not supported.
5720 bool remove_transitions = transition_flag == REMOVE_TRANSITIONS;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005721 ASSERT(remove_transitions == !descriptor->ContainsTransition());
Steve Blocka7e24c12009-10-30 11:49:00 +00005722 ASSERT(descriptor->GetDetails().type() != NULL_DESCRIPTOR);
5723
5724 // Ensure the key is a symbol.
John Reck59135872010-11-02 12:39:01 -07005725 { MaybeObject* maybe_result = descriptor->KeyToSymbol();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005726 if (maybe_result->IsFailure()) return maybe_result;
John Reck59135872010-11-02 12:39:01 -07005727 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005728
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005729 int new_size = 0;
5730 for (int i = 0; i < number_of_descriptors(); i++) {
5731 if (IsNullDescriptor(i)) continue;
5732 if (remove_transitions && IsTransitionOnly(i)) continue;
5733 new_size++;
Steve Blocka7e24c12009-10-30 11:49:00 +00005734 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005735
5736 // If key is in descriptor, we replace it in-place when filtering.
5737 // Count a null descriptor for key as inserted, not replaced.
5738 int index = Search(descriptor->GetKey());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005739 const bool replacing = (index != kNotFound);
Steve Blocka7e24c12009-10-30 11:49:00 +00005740 bool keep_enumeration_index = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005741 if (replacing) {
5742 // We are replacing an existing descriptor. We keep the enumeration
5743 // index of a visible property.
5744 PropertyType t = PropertyDetails(GetDetails(index)).type();
5745 if (t == CONSTANT_FUNCTION ||
5746 t == FIELD ||
5747 t == CALLBACKS ||
5748 t == INTERCEPTOR) {
5749 keep_enumeration_index = true;
5750 } else if (remove_transitions) {
5751 // Replaced descriptor has been counted as removed if it is
5752 // a transition that will be replaced. Adjust count in this case.
5753 ++new_size;
5754 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005755 } else {
5756 ++new_size;
Steve Blocka7e24c12009-10-30 11:49:00 +00005757 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005758
5759 DescriptorArray* new_descriptors;
John Reck59135872010-11-02 12:39:01 -07005760 { MaybeObject* maybe_result = Allocate(new_size);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005761 if (!maybe_result->To(&new_descriptors)) return maybe_result;
John Reck59135872010-11-02 12:39:01 -07005762 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005763
5764 DescriptorArray::WhitenessWitness witness(new_descriptors);
5765
Steve Blocka7e24c12009-10-30 11:49:00 +00005766 // Set the enumeration index in the descriptors and set the enumeration index
5767 // in the result.
5768 int enumeration_index = NextEnumerationIndex();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005769 if (!descriptor->ContainsTransition()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005770 if (keep_enumeration_index) {
5771 descriptor->SetEnumerationIndex(
5772 PropertyDetails(GetDetails(index)).index());
5773 } else {
5774 descriptor->SetEnumerationIndex(enumeration_index);
5775 ++enumeration_index;
5776 }
5777 }
5778 new_descriptors->SetNextEnumerationIndex(enumeration_index);
5779
5780 // Copy the descriptors, filtering out transitions and null descriptors,
5781 // and inserting or replacing a descriptor.
Ben Murdoch85b71792012-04-11 18:30:58 +01005782 int to_index = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005783 int insertion_index = -1;
5784 int from_index = 0;
5785 while (from_index < number_of_descriptors()) {
5786 if (insertion_index < 0 &&
5787 InsertionPointFound(GetKey(from_index), descriptor->GetKey())) {
5788 insertion_index = to_index++;
5789 if (replacing) from_index++;
5790 } else {
5791 if (!(IsNullDescriptor(from_index) ||
5792 (remove_transitions && IsTransitionOnly(from_index)))) {
5793 MaybeObject* copy_result =
5794 new_descriptors->CopyFrom(to_index++, this, from_index, witness);
5795 if (copy_result->IsFailure()) return copy_result;
5796 }
5797 from_index++;
Steve Blocka7e24c12009-10-30 11:49:00 +00005798 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005799 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005800 if (insertion_index < 0) insertion_index = to_index++;
5801 new_descriptors->Set(insertion_index, descriptor, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +00005802
5803 ASSERT(to_index == new_descriptors->number_of_descriptors());
5804 SLOW_ASSERT(new_descriptors->IsSortedNoDuplicates());
5805
5806 return new_descriptors;
5807}
5808
5809
John Reck59135872010-11-02 12:39:01 -07005810MaybeObject* DescriptorArray::RemoveTransitions() {
Ben Murdoch85b71792012-04-11 18:30:58 +01005811 // Allocate the new descriptor array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005812 int new_number_of_descriptors = 0;
5813 for (int i = 0; i < number_of_descriptors(); i++) {
5814 if (IsProperty(i)) new_number_of_descriptors++;
Ben Murdoch85b71792012-04-11 18:30:58 +01005815 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005816 DescriptorArray* new_descriptors;
5817 { MaybeObject* maybe_result = Allocate(new_number_of_descriptors);
5818 if (!maybe_result->To(&new_descriptors)) return maybe_result;
5819 }
Ben Murdoch85b71792012-04-11 18:30:58 +01005820
Steve Blocka7e24c12009-10-30 11:49:00 +00005821 // Copy the content.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005822 DescriptorArray::WhitenessWitness witness(new_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00005823 int next_descriptor = 0;
5824 for (int i = 0; i < number_of_descriptors(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005825 if (IsProperty(i)) {
5826 MaybeObject* copy_result =
5827 new_descriptors->CopyFrom(next_descriptor++, this, i, witness);
5828 if (copy_result->IsFailure()) return copy_result;
5829 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005830 }
5831 ASSERT(next_descriptor == new_descriptors->number_of_descriptors());
5832
5833 return new_descriptors;
5834}
5835
5836
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005837void DescriptorArray::SortUnchecked(const WhitenessWitness& witness) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005838 // In-place heap sort.
5839 int len = number_of_descriptors();
5840
5841 // Bottom-up max-heap construction.
Steve Block6ded16b2010-05-10 14:33:55 +01005842 // Index of the last node with children
5843 const int max_parent_index = (len / 2) - 1;
5844 for (int i = max_parent_index; i >= 0; --i) {
5845 int parent_index = i;
5846 const uint32_t parent_hash = GetKey(i)->Hash();
5847 while (parent_index <= max_parent_index) {
5848 int child_index = 2 * parent_index + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00005849 uint32_t child_hash = GetKey(child_index)->Hash();
Steve Block6ded16b2010-05-10 14:33:55 +01005850 if (child_index + 1 < len) {
5851 uint32_t right_child_hash = GetKey(child_index + 1)->Hash();
5852 if (right_child_hash > child_hash) {
5853 child_index++;
5854 child_hash = right_child_hash;
5855 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005856 }
Steve Block6ded16b2010-05-10 14:33:55 +01005857 if (child_hash <= parent_hash) break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005858 NoIncrementalWriteBarrierSwapDescriptors(parent_index, child_index);
Steve Block6ded16b2010-05-10 14:33:55 +01005859 // Now element at child_index could be < its children.
5860 parent_index = child_index; // parent_hash remains correct.
Steve Blocka7e24c12009-10-30 11:49:00 +00005861 }
5862 }
5863
5864 // Extract elements and create sorted array.
5865 for (int i = len - 1; i > 0; --i) {
5866 // Put max element at the back of the array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005867 NoIncrementalWriteBarrierSwapDescriptors(0, i);
5868 // Shift down the new top element.
Steve Blocka7e24c12009-10-30 11:49:00 +00005869 int parent_index = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01005870 const uint32_t parent_hash = GetKey(parent_index)->Hash();
5871 const int max_parent_index = (i / 2) - 1;
5872 while (parent_index <= max_parent_index) {
5873 int child_index = parent_index * 2 + 1;
5874 uint32_t child_hash = GetKey(child_index)->Hash();
5875 if (child_index + 1 < i) {
5876 uint32_t right_child_hash = GetKey(child_index + 1)->Hash();
5877 if (right_child_hash > child_hash) {
5878 child_index++;
5879 child_hash = right_child_hash;
5880 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005881 }
Steve Block6ded16b2010-05-10 14:33:55 +01005882 if (child_hash <= parent_hash) break;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005883 NoIncrementalWriteBarrierSwapDescriptors(parent_index, child_index);
Steve Block6ded16b2010-05-10 14:33:55 +01005884 parent_index = child_index;
Steve Blocka7e24c12009-10-30 11:49:00 +00005885 }
5886 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005887}
Steve Blocka7e24c12009-10-30 11:49:00 +00005888
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005889
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005890void DescriptorArray::Sort(const WhitenessWitness& witness) {
5891 SortUnchecked(witness);
Steve Blocka7e24c12009-10-30 11:49:00 +00005892 SLOW_ASSERT(IsSortedNoDuplicates());
5893}
5894
5895
5896int DescriptorArray::BinarySearch(String* name, int low, int high) {
5897 uint32_t hash = name->Hash();
5898
5899 while (low <= high) {
5900 int mid = (low + high) / 2;
5901 String* mid_name = GetKey(mid);
5902 uint32_t mid_hash = mid_name->Hash();
5903
5904 if (mid_hash > hash) {
5905 high = mid - 1;
5906 continue;
5907 }
5908 if (mid_hash < hash) {
5909 low = mid + 1;
5910 continue;
5911 }
5912 // Found an element with the same hash-code.
5913 ASSERT(hash == mid_hash);
5914 // There might be more, so we find the first one and
5915 // check them all to see if we have a match.
5916 if (name == mid_name && !is_null_descriptor(mid)) return mid;
5917 while ((mid > low) && (GetKey(mid - 1)->Hash() == hash)) mid--;
5918 for (; (mid <= high) && (GetKey(mid)->Hash() == hash); mid++) {
5919 if (GetKey(mid)->Equals(name) && !is_null_descriptor(mid)) return mid;
5920 }
5921 break;
5922 }
5923 return kNotFound;
5924}
5925
5926
5927int DescriptorArray::LinearSearch(String* name, int len) {
5928 uint32_t hash = name->Hash();
5929 for (int number = 0; number < len; number++) {
5930 String* entry = GetKey(number);
5931 if ((entry->Hash() == hash) &&
5932 name->Equals(entry) &&
5933 !is_null_descriptor(number)) {
5934 return number;
5935 }
5936 }
5937 return kNotFound;
5938}
5939
5940
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005941MaybeObject* AccessorPair::CopyWithoutTransitions() {
5942 Heap* heap = GetHeap();
5943 AccessorPair* copy;
5944 { MaybeObject* maybe_copy = heap->AllocateAccessorPair();
5945 if (!maybe_copy->To(&copy)) return maybe_copy;
5946 }
5947 copy->set_getter(getter()->IsMap() ? heap->the_hole_value() : getter());
5948 copy->set_setter(setter()->IsMap() ? heap->the_hole_value() : setter());
5949 return copy;
5950}
5951
5952
5953Object* AccessorPair::GetComponent(AccessorComponent component) {
5954 Object* accessor = (component == ACCESSOR_GETTER) ? getter() : setter();
5955 return accessor->IsTheHole() ? GetHeap()->undefined_value() : accessor;
5956}
5957
5958
Ben Murdochb0fe1622011-05-05 13:52:32 +01005959MaybeObject* DeoptimizationInputData::Allocate(int deopt_entry_count,
5960 PretenureFlag pretenure) {
5961 ASSERT(deopt_entry_count > 0);
Steve Block44f0eee2011-05-26 01:26:41 +01005962 return HEAP->AllocateFixedArray(LengthFor(deopt_entry_count),
Ben Murdochb0fe1622011-05-05 13:52:32 +01005963 pretenure);
5964}
5965
5966
5967MaybeObject* DeoptimizationOutputData::Allocate(int number_of_deopt_points,
5968 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01005969 if (number_of_deopt_points == 0) return HEAP->empty_fixed_array();
5970 return HEAP->AllocateFixedArray(LengthOfFixedArray(number_of_deopt_points),
Ben Murdochb0fe1622011-05-05 13:52:32 +01005971 pretenure);
5972}
5973
5974
Steve Blocka7e24c12009-10-30 11:49:00 +00005975#ifdef DEBUG
5976bool DescriptorArray::IsEqualTo(DescriptorArray* other) {
5977 if (IsEmpty()) return other->IsEmpty();
5978 if (other->IsEmpty()) return false;
5979 if (length() != other->length()) return false;
5980 for (int i = 0; i < length(); ++i) {
5981 if (get(i) != other->get(i) && i != kContentArrayIndex) return false;
5982 }
5983 return GetContentArray()->IsEqualTo(other->GetContentArray());
5984}
5985#endif
5986
5987
Steve Blocka7e24c12009-10-30 11:49:00 +00005988bool String::LooksValid() {
Steve Block44f0eee2011-05-26 01:26:41 +01005989 if (!Isolate::Current()->heap()->Contains(this)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005990 return true;
5991}
5992
5993
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005994String::FlatContent String::GetFlatContent() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005995 int length = this->length();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005996 StringShape shape(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00005997 String* string = this;
Steve Blocka7e24c12009-10-30 11:49:00 +00005998 int offset = 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005999 if (shape.representation_tag() == kConsStringTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006000 ConsString* cons = ConsString::cast(string);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006001 if (cons->second()->length() != 0) {
6002 return FlatContent();
6003 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006004 string = cons->first();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006005 shape = StringShape(string);
Steve Blocka7e24c12009-10-30 11:49:00 +00006006 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006007 if (shape.representation_tag() == kSlicedStringTag) {
6008 SlicedString* slice = SlicedString::cast(string);
6009 offset = slice->offset();
6010 string = slice->parent();
6011 shape = StringShape(string);
6012 ASSERT(shape.representation_tag() != kConsStringTag &&
6013 shape.representation_tag() != kSlicedStringTag);
Steve Blocka7e24c12009-10-30 11:49:00 +00006014 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006015 if (shape.encoding_tag() == kAsciiStringTag) {
6016 const char* start;
6017 if (shape.representation_tag() == kSeqStringTag) {
6018 start = SeqAsciiString::cast(string)->GetChars();
6019 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006020 start = ExternalAsciiString::cast(string)->GetChars();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006021 }
6022 return FlatContent(Vector<const char>(start + offset, length));
6023 } else {
6024 ASSERT(shape.encoding_tag() == kTwoByteStringTag);
6025 const uc16* start;
6026 if (shape.representation_tag() == kSeqStringTag) {
6027 start = SeqTwoByteString::cast(string)->GetChars();
6028 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006029 start = ExternalTwoByteString::cast(string)->GetChars();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006030 }
6031 return FlatContent(Vector<const uc16>(start + offset, length));
6032 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006033}
6034
6035
Ben Murdoch589d6972011-11-30 16:04:58 +00006036SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
6037 RobustnessFlag robust_flag,
6038 int offset,
6039 int length,
6040 int* length_return) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006041 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
Ben Murdoch589d6972011-11-30 16:04:58 +00006042 return SmartArrayPointer<char>(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006043 }
Steve Block44f0eee2011-05-26 01:26:41 +01006044 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00006045
6046 // Negative length means the to the end of the string.
6047 if (length < 0) length = kMaxInt - offset;
6048
6049 // Compute the size of the UTF-8 string. Start at the specified offset.
Steve Block44f0eee2011-05-26 01:26:41 +01006050 Access<StringInputBuffer> buffer(
6051 heap->isolate()->objects_string_input_buffer());
Steve Blocka7e24c12009-10-30 11:49:00 +00006052 buffer->Reset(offset, this);
6053 int character_position = offset;
6054 int utf8_bytes = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006055 int last = unibrow::Utf16::kNoPreviousCharacter;
6056 while (buffer->has_more() && character_position++ < offset + length) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006057 uint16_t character = buffer->GetNext();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006058 utf8_bytes += unibrow::Utf8::Length(character, last);
6059 last = character;
Steve Blocka7e24c12009-10-30 11:49:00 +00006060 }
6061
6062 if (length_return) {
6063 *length_return = utf8_bytes;
6064 }
6065
6066 char* result = NewArray<char>(utf8_bytes + 1);
6067
6068 // Convert the UTF-16 string to a UTF-8 buffer. Start at the specified offset.
6069 buffer->Rewind();
6070 buffer->Seek(offset);
6071 character_position = offset;
6072 int utf8_byte_position = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006073 last = unibrow::Utf16::kNoPreviousCharacter;
6074 while (buffer->has_more() && character_position++ < offset + length) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006075 uint16_t character = buffer->GetNext();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006076 if (allow_nulls == DISALLOW_NULLS && character == 0) {
6077 character = ' ';
Steve Blocka7e24c12009-10-30 11:49:00 +00006078 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006079 utf8_byte_position +=
6080 unibrow::Utf8::Encode(result + utf8_byte_position, character, last);
6081 last = character;
Steve Blocka7e24c12009-10-30 11:49:00 +00006082 }
6083 result[utf8_byte_position] = 0;
Ben Murdoch589d6972011-11-30 16:04:58 +00006084 return SmartArrayPointer<char>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00006085}
6086
6087
Ben Murdoch589d6972011-11-30 16:04:58 +00006088SmartArrayPointer<char> String::ToCString(AllowNullsFlag allow_nulls,
6089 RobustnessFlag robust_flag,
6090 int* length_return) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006091 return ToCString(allow_nulls, robust_flag, 0, -1, length_return);
6092}
6093
6094
6095const uc16* String::GetTwoByteData() {
6096 return GetTwoByteData(0);
6097}
6098
6099
6100const uc16* String::GetTwoByteData(unsigned start) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006101 ASSERT(!IsAsciiRepresentationUnderneath());
Steve Blocka7e24c12009-10-30 11:49:00 +00006102 switch (StringShape(this).representation_tag()) {
6103 case kSeqStringTag:
6104 return SeqTwoByteString::cast(this)->SeqTwoByteStringGetData(start);
6105 case kExternalStringTag:
6106 return ExternalTwoByteString::cast(this)->
6107 ExternalTwoByteStringGetData(start);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006108 case kSlicedStringTag: {
6109 SlicedString* slice = SlicedString::cast(this);
6110 return slice->parent()->GetTwoByteData(start + slice->offset());
6111 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006112 case kConsStringTag:
6113 UNREACHABLE();
6114 return NULL;
6115 }
6116 UNREACHABLE();
6117 return NULL;
6118}
6119
6120
Ben Murdoch589d6972011-11-30 16:04:58 +00006121SmartArrayPointer<uc16> String::ToWideCString(RobustnessFlag robust_flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006122 if (robust_flag == ROBUST_STRING_TRAVERSAL && !LooksValid()) {
Ben Murdoch589d6972011-11-30 16:04:58 +00006123 return SmartArrayPointer<uc16>();
Steve Blocka7e24c12009-10-30 11:49:00 +00006124 }
Steve Block44f0eee2011-05-26 01:26:41 +01006125 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00006126
Steve Block44f0eee2011-05-26 01:26:41 +01006127 Access<StringInputBuffer> buffer(
6128 heap->isolate()->objects_string_input_buffer());
Steve Blocka7e24c12009-10-30 11:49:00 +00006129 buffer->Reset(this);
6130
6131 uc16* result = NewArray<uc16>(length() + 1);
6132
6133 int i = 0;
6134 while (buffer->has_more()) {
6135 uint16_t character = buffer->GetNext();
6136 result[i++] = character;
6137 }
6138 result[i] = 0;
Ben Murdoch589d6972011-11-30 16:04:58 +00006139 return SmartArrayPointer<uc16>(result);
Steve Blocka7e24c12009-10-30 11:49:00 +00006140}
6141
6142
6143const uc16* SeqTwoByteString::SeqTwoByteStringGetData(unsigned start) {
6144 return reinterpret_cast<uc16*>(
6145 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize) + start;
6146}
6147
6148
6149void SeqTwoByteString::SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* rbb,
6150 unsigned* offset_ptr,
6151 unsigned max_chars) {
6152 unsigned chars_read = 0;
6153 unsigned offset = *offset_ptr;
6154 while (chars_read < max_chars) {
6155 uint16_t c = *reinterpret_cast<uint16_t*>(
6156 reinterpret_cast<char*>(this) -
6157 kHeapObjectTag + kHeaderSize + offset * kShortSize);
6158 if (c <= kMaxAsciiCharCode) {
6159 // Fast case for ASCII characters. Cursor is an input output argument.
6160 if (!unibrow::CharacterStream::EncodeAsciiCharacter(c,
6161 rbb->util_buffer,
6162 rbb->capacity,
6163 rbb->cursor)) {
6164 break;
6165 }
6166 } else {
6167 if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c,
6168 rbb->util_buffer,
6169 rbb->capacity,
6170 rbb->cursor)) {
6171 break;
6172 }
6173 }
6174 offset++;
6175 chars_read++;
6176 }
6177 *offset_ptr = offset;
6178 rbb->remaining += chars_read;
6179}
6180
6181
6182const unibrow::byte* SeqAsciiString::SeqAsciiStringReadBlock(
6183 unsigned* remaining,
6184 unsigned* offset_ptr,
6185 unsigned max_chars) {
6186 const unibrow::byte* b = reinterpret_cast<unibrow::byte*>(this) -
6187 kHeapObjectTag + kHeaderSize + *offset_ptr * kCharSize;
6188 *remaining = max_chars;
6189 *offset_ptr += max_chars;
6190 return b;
6191}
6192
6193
6194// This will iterate unless the block of string data spans two 'halves' of
6195// a ConsString, in which case it will recurse. Since the block of string
6196// data to be read has a maximum size this limits the maximum recursion
6197// depth to something sane. Since C++ does not have tail call recursion
6198// elimination, the iteration must be explicit. Since this is not an
6199// -IntoBuffer method it can delegate to one of the efficient
6200// *AsciiStringReadBlock routines.
6201const unibrow::byte* ConsString::ConsStringReadBlock(ReadBlockBuffer* rbb,
6202 unsigned* offset_ptr,
6203 unsigned max_chars) {
6204 ConsString* current = this;
6205 unsigned offset = *offset_ptr;
6206 int offset_correction = 0;
6207
6208 while (true) {
6209 String* left = current->first();
6210 unsigned left_length = (unsigned)left->length();
6211 if (left_length > offset &&
6212 (max_chars <= left_length - offset ||
6213 (rbb->capacity <= left_length - offset &&
6214 (max_chars = left_length - offset, true)))) { // comma operator!
6215 // Left hand side only - iterate unless we have reached the bottom of
6216 // the cons tree. The assignment on the left of the comma operator is
6217 // in order to make use of the fact that the -IntoBuffer routines can
6218 // produce at most 'capacity' characters. This enables us to postpone
6219 // the point where we switch to the -IntoBuffer routines (below) in order
6220 // to maximize the chances of delegating a big chunk of work to the
6221 // efficient *AsciiStringReadBlock routines.
6222 if (StringShape(left).IsCons()) {
6223 current = ConsString::cast(left);
6224 continue;
6225 } else {
6226 const unibrow::byte* answer =
6227 String::ReadBlock(left, rbb, &offset, max_chars);
6228 *offset_ptr = offset + offset_correction;
6229 return answer;
6230 }
6231 } else if (left_length <= offset) {
6232 // Right hand side only - iterate unless we have reached the bottom of
6233 // the cons tree.
6234 String* right = current->second();
6235 offset -= left_length;
6236 offset_correction += left_length;
6237 if (StringShape(right).IsCons()) {
6238 current = ConsString::cast(right);
6239 continue;
6240 } else {
6241 const unibrow::byte* answer =
6242 String::ReadBlock(right, rbb, &offset, max_chars);
6243 *offset_ptr = offset + offset_correction;
6244 return answer;
6245 }
6246 } else {
6247 // The block to be read spans two sides of the ConsString, so we call the
6248 // -IntoBuffer version, which will recurse. The -IntoBuffer methods
6249 // are able to assemble data from several part strings because they use
6250 // the util_buffer to store their data and never return direct pointers
6251 // to their storage. We don't try to read more than the buffer capacity
6252 // here or we can get too much recursion.
6253 ASSERT(rbb->remaining == 0);
6254 ASSERT(rbb->cursor == 0);
6255 current->ConsStringReadBlockIntoBuffer(
6256 rbb,
6257 &offset,
6258 max_chars > rbb->capacity ? rbb->capacity : max_chars);
6259 *offset_ptr = offset + offset_correction;
6260 return rbb->util_buffer;
6261 }
6262 }
6263}
6264
6265
Steve Blocka7e24c12009-10-30 11:49:00 +00006266const unibrow::byte* ExternalAsciiString::ExternalAsciiStringReadBlock(
6267 unsigned* remaining,
6268 unsigned* offset_ptr,
6269 unsigned max_chars) {
6270 // Cast const char* to unibrow::byte* (signedness difference).
6271 const unibrow::byte* b =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006272 reinterpret_cast<const unibrow::byte*>(GetChars()) + *offset_ptr;
Steve Blocka7e24c12009-10-30 11:49:00 +00006273 *remaining = max_chars;
6274 *offset_ptr += max_chars;
6275 return b;
6276}
6277
6278
Steve Blocka7e24c12009-10-30 11:49:00 +00006279void ExternalTwoByteString::ExternalTwoByteStringReadBlockIntoBuffer(
6280 ReadBlockBuffer* rbb,
6281 unsigned* offset_ptr,
6282 unsigned max_chars) {
6283 unsigned chars_read = 0;
6284 unsigned offset = *offset_ptr;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006285 const uint16_t* data = GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00006286 while (chars_read < max_chars) {
6287 uint16_t c = data[offset];
6288 if (c <= kMaxAsciiCharCode) {
6289 // Fast case for ASCII characters. Cursor is an input output argument.
6290 if (!unibrow::CharacterStream::EncodeAsciiCharacter(c,
6291 rbb->util_buffer,
6292 rbb->capacity,
6293 rbb->cursor))
6294 break;
6295 } else {
6296 if (!unibrow::CharacterStream::EncodeNonAsciiCharacter(c,
6297 rbb->util_buffer,
6298 rbb->capacity,
6299 rbb->cursor))
6300 break;
6301 }
6302 offset++;
6303 chars_read++;
6304 }
6305 *offset_ptr = offset;
6306 rbb->remaining += chars_read;
6307}
6308
6309
6310void SeqAsciiString::SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* rbb,
6311 unsigned* offset_ptr,
6312 unsigned max_chars) {
6313 unsigned capacity = rbb->capacity - rbb->cursor;
6314 if (max_chars > capacity) max_chars = capacity;
6315 memcpy(rbb->util_buffer + rbb->cursor,
6316 reinterpret_cast<char*>(this) - kHeapObjectTag + kHeaderSize +
6317 *offset_ptr * kCharSize,
6318 max_chars);
6319 rbb->remaining += max_chars;
6320 *offset_ptr += max_chars;
6321 rbb->cursor += max_chars;
6322}
6323
6324
6325void ExternalAsciiString::ExternalAsciiStringReadBlockIntoBuffer(
6326 ReadBlockBuffer* rbb,
6327 unsigned* offset_ptr,
6328 unsigned max_chars) {
6329 unsigned capacity = rbb->capacity - rbb->cursor;
6330 if (max_chars > capacity) max_chars = capacity;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006331 memcpy(rbb->util_buffer + rbb->cursor, GetChars() + *offset_ptr, max_chars);
Steve Blocka7e24c12009-10-30 11:49:00 +00006332 rbb->remaining += max_chars;
6333 *offset_ptr += max_chars;
6334 rbb->cursor += max_chars;
6335}
6336
6337
6338// This method determines the type of string involved and then copies
6339// a whole chunk of characters into a buffer, or returns a pointer to a buffer
6340// where they can be found. The pointer is not necessarily valid across a GC
6341// (see AsciiStringReadBlock).
6342const unibrow::byte* String::ReadBlock(String* input,
6343 ReadBlockBuffer* rbb,
6344 unsigned* offset_ptr,
6345 unsigned max_chars) {
6346 ASSERT(*offset_ptr <= static_cast<unsigned>(input->length()));
6347 if (max_chars == 0) {
6348 rbb->remaining = 0;
6349 return NULL;
6350 }
6351 switch (StringShape(input).representation_tag()) {
6352 case kSeqStringTag:
6353 if (input->IsAsciiRepresentation()) {
6354 SeqAsciiString* str = SeqAsciiString::cast(input);
6355 return str->SeqAsciiStringReadBlock(&rbb->remaining,
6356 offset_ptr,
6357 max_chars);
6358 } else {
6359 SeqTwoByteString* str = SeqTwoByteString::cast(input);
6360 str->SeqTwoByteStringReadBlockIntoBuffer(rbb,
6361 offset_ptr,
6362 max_chars);
6363 return rbb->util_buffer;
6364 }
6365 case kConsStringTag:
6366 return ConsString::cast(input)->ConsStringReadBlock(rbb,
6367 offset_ptr,
6368 max_chars);
Steve Blocka7e24c12009-10-30 11:49:00 +00006369 case kExternalStringTag:
6370 if (input->IsAsciiRepresentation()) {
6371 return ExternalAsciiString::cast(input)->ExternalAsciiStringReadBlock(
6372 &rbb->remaining,
6373 offset_ptr,
6374 max_chars);
6375 } else {
6376 ExternalTwoByteString::cast(input)->
6377 ExternalTwoByteStringReadBlockIntoBuffer(rbb,
6378 offset_ptr,
6379 max_chars);
6380 return rbb->util_buffer;
6381 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006382 case kSlicedStringTag:
6383 return SlicedString::cast(input)->SlicedStringReadBlock(rbb,
6384 offset_ptr,
6385 max_chars);
Steve Blocka7e24c12009-10-30 11:49:00 +00006386 default:
6387 break;
6388 }
6389
6390 UNREACHABLE();
6391 return 0;
6392}
6393
6394
Steve Blocka7e24c12009-10-30 11:49:00 +00006395void Relocatable::PostGarbageCollectionProcessing() {
Steve Block44f0eee2011-05-26 01:26:41 +01006396 Isolate* isolate = Isolate::Current();
6397 Relocatable* current = isolate->relocatable_top();
Steve Blocka7e24c12009-10-30 11:49:00 +00006398 while (current != NULL) {
6399 current->PostGarbageCollection();
6400 current = current->prev_;
6401 }
6402}
6403
6404
6405// Reserve space for statics needing saving and restoring.
6406int Relocatable::ArchiveSpacePerThread() {
Steve Block44f0eee2011-05-26 01:26:41 +01006407 return sizeof(Isolate::Current()->relocatable_top());
Steve Blocka7e24c12009-10-30 11:49:00 +00006408}
6409
6410
6411// Archive statics that are thread local.
Ben Murdoch257744e2011-11-30 15:57:28 +00006412char* Relocatable::ArchiveState(Isolate* isolate, char* to) {
Steve Block44f0eee2011-05-26 01:26:41 +01006413 *reinterpret_cast<Relocatable**>(to) = isolate->relocatable_top();
6414 isolate->set_relocatable_top(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006415 return to + ArchiveSpacePerThread();
6416}
6417
6418
6419// Restore statics that are thread local.
Ben Murdoch257744e2011-11-30 15:57:28 +00006420char* Relocatable::RestoreState(Isolate* isolate, char* from) {
Steve Block44f0eee2011-05-26 01:26:41 +01006421 isolate->set_relocatable_top(*reinterpret_cast<Relocatable**>(from));
Steve Blocka7e24c12009-10-30 11:49:00 +00006422 return from + ArchiveSpacePerThread();
6423}
6424
6425
6426char* Relocatable::Iterate(ObjectVisitor* v, char* thread_storage) {
6427 Relocatable* top = *reinterpret_cast<Relocatable**>(thread_storage);
6428 Iterate(v, top);
6429 return thread_storage + ArchiveSpacePerThread();
6430}
6431
6432
6433void Relocatable::Iterate(ObjectVisitor* v) {
Steve Block44f0eee2011-05-26 01:26:41 +01006434 Isolate* isolate = Isolate::Current();
6435 Iterate(v, isolate->relocatable_top());
Steve Blocka7e24c12009-10-30 11:49:00 +00006436}
6437
6438
6439void Relocatable::Iterate(ObjectVisitor* v, Relocatable* top) {
6440 Relocatable* current = top;
6441 while (current != NULL) {
6442 current->IterateInstance(v);
6443 current = current->prev_;
6444 }
6445}
6446
6447
Steve Block44f0eee2011-05-26 01:26:41 +01006448FlatStringReader::FlatStringReader(Isolate* isolate, Handle<String> str)
6449 : Relocatable(isolate),
6450 str_(str.location()),
Steve Blocka7e24c12009-10-30 11:49:00 +00006451 length_(str->length()) {
6452 PostGarbageCollection();
6453}
6454
6455
Steve Block44f0eee2011-05-26 01:26:41 +01006456FlatStringReader::FlatStringReader(Isolate* isolate, Vector<const char> input)
6457 : Relocatable(isolate),
6458 str_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +00006459 is_ascii_(true),
6460 length_(input.length()),
6461 start_(input.start()) { }
6462
6463
6464void FlatStringReader::PostGarbageCollection() {
6465 if (str_ == NULL) return;
6466 Handle<String> str(str_);
6467 ASSERT(str->IsFlat());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006468 String::FlatContent content = str->GetFlatContent();
6469 ASSERT(content.IsFlat());
6470 is_ascii_ = content.IsAscii();
Steve Blocka7e24c12009-10-30 11:49:00 +00006471 if (is_ascii_) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006472 start_ = content.ToAsciiVector().start();
Steve Blocka7e24c12009-10-30 11:49:00 +00006473 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006474 start_ = content.ToUC16Vector().start();
Steve Blocka7e24c12009-10-30 11:49:00 +00006475 }
6476}
6477
6478
6479void StringInputBuffer::Seek(unsigned pos) {
6480 Reset(pos, input_);
6481}
6482
6483
6484void SafeStringInputBuffer::Seek(unsigned pos) {
6485 Reset(pos, input_);
6486}
6487
6488
6489// This method determines the type of string involved and then copies
6490// a whole chunk of characters into a buffer. It can be used with strings
6491// that have been glued together to form a ConsString and which must cooperate
6492// to fill up a buffer.
6493void String::ReadBlockIntoBuffer(String* input,
6494 ReadBlockBuffer* rbb,
6495 unsigned* offset_ptr,
6496 unsigned max_chars) {
6497 ASSERT(*offset_ptr <= (unsigned)input->length());
6498 if (max_chars == 0) return;
6499
6500 switch (StringShape(input).representation_tag()) {
6501 case kSeqStringTag:
6502 if (input->IsAsciiRepresentation()) {
6503 SeqAsciiString::cast(input)->SeqAsciiStringReadBlockIntoBuffer(rbb,
6504 offset_ptr,
6505 max_chars);
6506 return;
6507 } else {
6508 SeqTwoByteString::cast(input)->SeqTwoByteStringReadBlockIntoBuffer(rbb,
6509 offset_ptr,
6510 max_chars);
6511 return;
6512 }
6513 case kConsStringTag:
6514 ConsString::cast(input)->ConsStringReadBlockIntoBuffer(rbb,
6515 offset_ptr,
6516 max_chars);
6517 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00006518 case kExternalStringTag:
6519 if (input->IsAsciiRepresentation()) {
Steve Blockd0582a62009-12-15 09:54:21 +00006520 ExternalAsciiString::cast(input)->
6521 ExternalAsciiStringReadBlockIntoBuffer(rbb, offset_ptr, max_chars);
6522 } else {
6523 ExternalTwoByteString::cast(input)->
6524 ExternalTwoByteStringReadBlockIntoBuffer(rbb,
6525 offset_ptr,
6526 max_chars);
Steve Blocka7e24c12009-10-30 11:49:00 +00006527 }
6528 return;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006529 case kSlicedStringTag:
6530 SlicedString::cast(input)->SlicedStringReadBlockIntoBuffer(rbb,
6531 offset_ptr,
6532 max_chars);
6533 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00006534 default:
6535 break;
6536 }
6537
6538 UNREACHABLE();
6539 return;
6540}
6541
6542
6543const unibrow::byte* String::ReadBlock(String* input,
6544 unibrow::byte* util_buffer,
6545 unsigned capacity,
6546 unsigned* remaining,
6547 unsigned* offset_ptr) {
6548 ASSERT(*offset_ptr <= (unsigned)input->length());
6549 unsigned chars = input->length() - *offset_ptr;
6550 ReadBlockBuffer rbb(util_buffer, 0, capacity, 0);
6551 const unibrow::byte* answer = ReadBlock(input, &rbb, offset_ptr, chars);
6552 ASSERT(rbb.remaining <= static_cast<unsigned>(input->length()));
6553 *remaining = rbb.remaining;
6554 return answer;
6555}
6556
6557
6558const unibrow::byte* String::ReadBlock(String** raw_input,
6559 unibrow::byte* util_buffer,
6560 unsigned capacity,
6561 unsigned* remaining,
6562 unsigned* offset_ptr) {
6563 Handle<String> input(raw_input);
6564 ASSERT(*offset_ptr <= (unsigned)input->length());
6565 unsigned chars = input->length() - *offset_ptr;
6566 if (chars > capacity) chars = capacity;
6567 ReadBlockBuffer rbb(util_buffer, 0, capacity, 0);
6568 ReadBlockIntoBuffer(*input, &rbb, offset_ptr, chars);
6569 ASSERT(rbb.remaining <= static_cast<unsigned>(input->length()));
6570 *remaining = rbb.remaining;
6571 return rbb.util_buffer;
6572}
6573
6574
6575// This will iterate unless the block of string data spans two 'halves' of
6576// a ConsString, in which case it will recurse. Since the block of string
6577// data to be read has a maximum size this limits the maximum recursion
6578// depth to something sane. Since C++ does not have tail call recursion
6579// elimination, the iteration must be explicit.
6580void ConsString::ConsStringReadBlockIntoBuffer(ReadBlockBuffer* rbb,
6581 unsigned* offset_ptr,
6582 unsigned max_chars) {
6583 ConsString* current = this;
6584 unsigned offset = *offset_ptr;
6585 int offset_correction = 0;
6586
6587 while (true) {
6588 String* left = current->first();
6589 unsigned left_length = (unsigned)left->length();
6590 if (left_length > offset &&
6591 max_chars <= left_length - offset) {
6592 // Left hand side only - iterate unless we have reached the bottom of
6593 // the cons tree.
6594 if (StringShape(left).IsCons()) {
6595 current = ConsString::cast(left);
6596 continue;
6597 } else {
6598 String::ReadBlockIntoBuffer(left, rbb, &offset, max_chars);
6599 *offset_ptr = offset + offset_correction;
6600 return;
6601 }
6602 } else if (left_length <= offset) {
6603 // Right hand side only - iterate unless we have reached the bottom of
6604 // the cons tree.
6605 offset -= left_length;
6606 offset_correction += left_length;
6607 String* right = current->second();
6608 if (StringShape(right).IsCons()) {
6609 current = ConsString::cast(right);
6610 continue;
6611 } else {
6612 String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars);
6613 *offset_ptr = offset + offset_correction;
6614 return;
6615 }
6616 } else {
6617 // The block to be read spans two sides of the ConsString, so we recurse.
6618 // First recurse on the left.
6619 max_chars -= left_length - offset;
6620 String::ReadBlockIntoBuffer(left, rbb, &offset, left_length - offset);
6621 // We may have reached the max or there may not have been enough space
6622 // in the buffer for the characters in the left hand side.
6623 if (offset == left_length) {
6624 // Recurse on the right.
6625 String* right = String::cast(current->second());
6626 offset -= left_length;
6627 offset_correction += left_length;
6628 String::ReadBlockIntoBuffer(right, rbb, &offset, max_chars);
6629 }
6630 *offset_ptr = offset + offset_correction;
6631 return;
6632 }
6633 }
6634}
6635
6636
Steve Blocka7e24c12009-10-30 11:49:00 +00006637uint16_t ConsString::ConsStringGet(int index) {
6638 ASSERT(index >= 0 && index < this->length());
6639
6640 // Check for a flattened cons string
6641 if (second()->length() == 0) {
6642 String* left = first();
6643 return left->Get(index);
6644 }
6645
6646 String* string = String::cast(this);
6647
6648 while (true) {
6649 if (StringShape(string).IsCons()) {
6650 ConsString* cons_string = ConsString::cast(string);
6651 String* left = cons_string->first();
6652 if (left->length() > index) {
6653 string = left;
6654 } else {
6655 index -= left->length();
6656 string = cons_string->second();
6657 }
6658 } else {
6659 return string->Get(index);
6660 }
6661 }
6662
6663 UNREACHABLE();
6664 return 0;
6665}
6666
6667
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006668uint16_t SlicedString::SlicedStringGet(int index) {
6669 return parent()->Get(offset() + index);
6670}
6671
6672
6673const unibrow::byte* SlicedString::SlicedStringReadBlock(
6674 ReadBlockBuffer* buffer, unsigned* offset_ptr, unsigned chars) {
6675 unsigned offset = this->offset();
6676 *offset_ptr += offset;
6677 const unibrow::byte* answer = String::ReadBlock(String::cast(parent()),
6678 buffer, offset_ptr, chars);
6679 *offset_ptr -= offset;
6680 return answer;
6681}
6682
6683
6684void SlicedString::SlicedStringReadBlockIntoBuffer(
6685 ReadBlockBuffer* buffer, unsigned* offset_ptr, unsigned chars) {
6686 unsigned offset = this->offset();
6687 *offset_ptr += offset;
6688 String::ReadBlockIntoBuffer(String::cast(parent()),
6689 buffer, offset_ptr, chars);
6690 *offset_ptr -= offset;
6691}
6692
Steve Blocka7e24c12009-10-30 11:49:00 +00006693template <typename sinkchar>
6694void String::WriteToFlat(String* src,
6695 sinkchar* sink,
6696 int f,
6697 int t) {
6698 String* source = src;
6699 int from = f;
6700 int to = t;
6701 while (true) {
6702 ASSERT(0 <= from && from <= to && to <= source->length());
6703 switch (StringShape(source).full_representation_tag()) {
6704 case kAsciiStringTag | kExternalStringTag: {
6705 CopyChars(sink,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006706 ExternalAsciiString::cast(source)->GetChars() + from,
Steve Blocka7e24c12009-10-30 11:49:00 +00006707 to - from);
6708 return;
6709 }
6710 case kTwoByteStringTag | kExternalStringTag: {
6711 const uc16* data =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006712 ExternalTwoByteString::cast(source)->GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00006713 CopyChars(sink,
6714 data + from,
6715 to - from);
6716 return;
6717 }
6718 case kAsciiStringTag | kSeqStringTag: {
6719 CopyChars(sink,
6720 SeqAsciiString::cast(source)->GetChars() + from,
6721 to - from);
6722 return;
6723 }
6724 case kTwoByteStringTag | kSeqStringTag: {
6725 CopyChars(sink,
6726 SeqTwoByteString::cast(source)->GetChars() + from,
6727 to - from);
6728 return;
6729 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006730 case kAsciiStringTag | kConsStringTag:
6731 case kTwoByteStringTag | kConsStringTag: {
6732 ConsString* cons_string = ConsString::cast(source);
6733 String* first = cons_string->first();
6734 int boundary = first->length();
6735 if (to - boundary >= boundary - from) {
6736 // Right hand side is longer. Recurse over left.
6737 if (from < boundary) {
6738 WriteToFlat(first, sink, from, boundary);
6739 sink += boundary - from;
6740 from = 0;
6741 } else {
6742 from -= boundary;
6743 }
6744 to -= boundary;
6745 source = cons_string->second();
6746 } else {
6747 // Left hand side is longer. Recurse over right.
6748 if (to > boundary) {
6749 String* second = cons_string->second();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006750 // When repeatedly appending to a string, we get a cons string that
6751 // is unbalanced to the left, a list, essentially. We inline the
6752 // common case of sequential ascii right child.
6753 if (to - boundary == 1) {
6754 sink[boundary - from] = static_cast<sinkchar>(second->Get(0));
6755 } else if (second->IsSeqAsciiString()) {
6756 CopyChars(sink + boundary - from,
6757 SeqAsciiString::cast(second)->GetChars(),
Steve Blocka7e24c12009-10-30 11:49:00 +00006758 to - boundary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006759 } else {
6760 WriteToFlat(second,
6761 sink + boundary - from,
6762 0,
6763 to - boundary);
6764 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006765 to = boundary;
6766 }
6767 source = first;
6768 }
6769 break;
6770 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006771 case kAsciiStringTag | kSlicedStringTag:
6772 case kTwoByteStringTag | kSlicedStringTag: {
6773 SlicedString* slice = SlicedString::cast(source);
6774 unsigned offset = slice->offset();
6775 WriteToFlat(slice->parent(), sink, from + offset, to + offset);
6776 return;
6777 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006778 }
6779 }
6780}
6781
6782
Steve Blocka7e24c12009-10-30 11:49:00 +00006783template <typename IteratorA, typename IteratorB>
6784static inline bool CompareStringContents(IteratorA* ia, IteratorB* ib) {
6785 // General slow case check. We know that the ia and ib iterators
6786 // have the same length.
6787 while (ia->has_more()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006788 uint32_t ca = ia->GetNext();
6789 uint32_t cb = ib->GetNext();
6790 ASSERT(ca <= unibrow::Utf16::kMaxNonSurrogateCharCode);
6791 ASSERT(cb <= unibrow::Utf16::kMaxNonSurrogateCharCode);
Steve Blocka7e24c12009-10-30 11:49:00 +00006792 if (ca != cb)
6793 return false;
6794 }
6795 return true;
6796}
6797
6798
6799// Compares the contents of two strings by reading and comparing
6800// int-sized blocks of characters.
6801template <typename Char>
6802static inline bool CompareRawStringContents(Vector<Char> a, Vector<Char> b) {
6803 int length = a.length();
6804 ASSERT_EQ(length, b.length());
6805 const Char* pa = a.start();
6806 const Char* pb = b.start();
6807 int i = 0;
6808#ifndef V8_HOST_CAN_READ_UNALIGNED
6809 // If this architecture isn't comfortable reading unaligned ints
6810 // then we have to check that the strings are aligned before
6811 // comparing them blockwise.
6812 const int kAlignmentMask = sizeof(uint32_t) - 1; // NOLINT
6813 uint32_t pa_addr = reinterpret_cast<uint32_t>(pa);
6814 uint32_t pb_addr = reinterpret_cast<uint32_t>(pb);
6815 if (((pa_addr & kAlignmentMask) | (pb_addr & kAlignmentMask)) == 0) {
6816#endif
6817 const int kStepSize = sizeof(int) / sizeof(Char); // NOLINT
6818 int endpoint = length - kStepSize;
6819 // Compare blocks until we reach near the end of the string.
6820 for (; i <= endpoint; i += kStepSize) {
6821 uint32_t wa = *reinterpret_cast<const uint32_t*>(pa + i);
6822 uint32_t wb = *reinterpret_cast<const uint32_t*>(pb + i);
6823 if (wa != wb) {
6824 return false;
6825 }
6826 }
6827#ifndef V8_HOST_CAN_READ_UNALIGNED
6828 }
6829#endif
6830 // Compare the remaining characters that didn't fit into a block.
6831 for (; i < length; i++) {
6832 if (a[i] != b[i]) {
6833 return false;
6834 }
6835 }
6836 return true;
6837}
6838
6839
Steve Blocka7e24c12009-10-30 11:49:00 +00006840template <typename IteratorA>
Steve Block44f0eee2011-05-26 01:26:41 +01006841static inline bool CompareStringContentsPartial(Isolate* isolate,
6842 IteratorA* ia,
6843 String* b) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006844 String::FlatContent content = b->GetFlatContent();
6845 if (content.IsFlat()) {
6846 if (content.IsAscii()) {
6847 VectorIterator<char> ib(content.ToAsciiVector());
Steve Blocka7e24c12009-10-30 11:49:00 +00006848 return CompareStringContents(ia, &ib);
6849 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006850 VectorIterator<uc16> ib(content.ToUC16Vector());
Steve Blocka7e24c12009-10-30 11:49:00 +00006851 return CompareStringContents(ia, &ib);
6852 }
6853 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01006854 isolate->objects_string_compare_buffer_b()->Reset(0, b);
6855 return CompareStringContents(ia,
6856 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00006857 }
6858}
6859
6860
Steve Blocka7e24c12009-10-30 11:49:00 +00006861bool String::SlowEquals(String* other) {
6862 // Fast check: negative check with lengths.
6863 int len = length();
6864 if (len != other->length()) return false;
6865 if (len == 0) return true;
6866
6867 // Fast check: if hash code is computed for both strings
6868 // a fast negative check can be performed.
6869 if (HasHashCode() && other->HasHashCode()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00006870#ifdef DEBUG
6871 if (FLAG_enable_slow_asserts) {
6872 if (Hash() != other->Hash()) {
6873 bool found_difference = false;
6874 for (int i = 0; i < len; i++) {
6875 if (Get(i) != other->Get(i)) {
6876 found_difference = true;
6877 break;
6878 }
6879 }
6880 ASSERT(found_difference);
6881 }
6882 }
6883#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006884 if (Hash() != other->Hash()) return false;
6885 }
6886
Leon Clarkef7060e22010-06-03 12:02:55 +01006887 // We know the strings are both non-empty. Compare the first chars
6888 // before we try to flatten the strings.
6889 if (this->Get(0) != other->Get(0)) return false;
6890
6891 String* lhs = this->TryFlattenGetString();
6892 String* rhs = other->TryFlattenGetString();
6893
6894 if (StringShape(lhs).IsSequentialAscii() &&
6895 StringShape(rhs).IsSequentialAscii()) {
6896 const char* str1 = SeqAsciiString::cast(lhs)->GetChars();
6897 const char* str2 = SeqAsciiString::cast(rhs)->GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00006898 return CompareRawStringContents(Vector<const char>(str1, len),
6899 Vector<const char>(str2, len));
6900 }
6901
Steve Block44f0eee2011-05-26 01:26:41 +01006902 Isolate* isolate = GetIsolate();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006903 String::FlatContent lhs_content = lhs->GetFlatContent();
6904 String::FlatContent rhs_content = rhs->GetFlatContent();
6905 if (lhs_content.IsFlat()) {
6906 if (lhs_content.IsAscii()) {
6907 Vector<const char> vec1 = lhs_content.ToAsciiVector();
6908 if (rhs_content.IsFlat()) {
6909 if (rhs_content.IsAscii()) {
6910 Vector<const char> vec2 = rhs_content.ToAsciiVector();
Steve Blocka7e24c12009-10-30 11:49:00 +00006911 return CompareRawStringContents(vec1, vec2);
6912 } else {
6913 VectorIterator<char> buf1(vec1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006914 VectorIterator<uc16> ib(rhs_content.ToUC16Vector());
Steve Blocka7e24c12009-10-30 11:49:00 +00006915 return CompareStringContents(&buf1, &ib);
6916 }
6917 } else {
6918 VectorIterator<char> buf1(vec1);
Steve Block44f0eee2011-05-26 01:26:41 +01006919 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
6920 return CompareStringContents(&buf1,
6921 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00006922 }
6923 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006924 Vector<const uc16> vec1 = lhs_content.ToUC16Vector();
6925 if (rhs_content.IsFlat()) {
6926 if (rhs_content.IsAscii()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006927 VectorIterator<uc16> buf1(vec1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006928 VectorIterator<char> ib(rhs_content.ToAsciiVector());
Steve Blocka7e24c12009-10-30 11:49:00 +00006929 return CompareStringContents(&buf1, &ib);
6930 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006931 Vector<const uc16> vec2(rhs_content.ToUC16Vector());
Steve Blocka7e24c12009-10-30 11:49:00 +00006932 return CompareRawStringContents(vec1, vec2);
6933 }
6934 } else {
6935 VectorIterator<uc16> buf1(vec1);
Steve Block44f0eee2011-05-26 01:26:41 +01006936 isolate->objects_string_compare_buffer_b()->Reset(0, rhs);
6937 return CompareStringContents(&buf1,
6938 isolate->objects_string_compare_buffer_b());
Steve Blocka7e24c12009-10-30 11:49:00 +00006939 }
6940 }
6941 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01006942 isolate->objects_string_compare_buffer_a()->Reset(0, lhs);
6943 return CompareStringContentsPartial(isolate,
6944 isolate->objects_string_compare_buffer_a(), rhs);
Steve Blocka7e24c12009-10-30 11:49:00 +00006945 }
6946}
6947
6948
6949bool String::MarkAsUndetectable() {
6950 if (StringShape(this).IsSymbol()) return false;
6951
6952 Map* map = this->map();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006953 Heap* heap = GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01006954 if (map == heap->string_map()) {
6955 this->set_map(heap->undetectable_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00006956 return true;
Steve Block44f0eee2011-05-26 01:26:41 +01006957 } else if (map == heap->ascii_string_map()) {
6958 this->set_map(heap->undetectable_ascii_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00006959 return true;
6960 }
6961 // Rest cannot be marked as undetectable
6962 return false;
6963}
6964
6965
6966bool String::IsEqualTo(Vector<const char> str) {
Steve Block44f0eee2011-05-26 01:26:41 +01006967 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00006968 int slen = length();
Ben Murdoch8b112d22011-06-08 16:22:53 +01006969 Access<UnicodeCache::Utf8Decoder>
6970 decoder(isolate->unicode_cache()->utf8_decoder());
Steve Blocka7e24c12009-10-30 11:49:00 +00006971 decoder->Reset(str.start(), str.length());
6972 int i;
6973 for (i = 0; i < slen && decoder->has_more(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006974 uint32_t r = decoder->GetNext();
6975 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
6976 if (i > slen - 1) return false;
6977 if (Get(i++) != unibrow::Utf16::LeadSurrogate(r)) return false;
6978 if (Get(i) != unibrow::Utf16::TrailSurrogate(r)) return false;
6979 } else {
6980 if (Get(i) != r) return false;
6981 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006982 }
6983 return i == slen && !decoder->has_more();
6984}
6985
6986
Steve Block9fac8402011-05-12 15:51:54 +01006987bool String::IsAsciiEqualTo(Vector<const char> str) {
6988 int slen = length();
6989 if (str.length() != slen) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006990 FlatContent content = GetFlatContent();
6991 if (content.IsAscii()) {
6992 return CompareChars(content.ToAsciiVector().start(),
6993 str.start(), slen) == 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006994 }
6995 for (int i = 0; i < slen; i++) {
6996 if (Get(i) != static_cast<uint16_t>(str[i])) return false;
Steve Block9fac8402011-05-12 15:51:54 +01006997 }
6998 return true;
6999}
7000
7001
7002bool String::IsTwoByteEqualTo(Vector<const uc16> str) {
7003 int slen = length();
7004 if (str.length() != slen) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007005 FlatContent content = GetFlatContent();
7006 if (content.IsTwoByte()) {
7007 return CompareChars(content.ToUC16Vector().start(), str.start(), slen) == 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007008 }
Steve Block9fac8402011-05-12 15:51:54 +01007009 for (int i = 0; i < slen; i++) {
7010 if (Get(i) != str[i]) return false;
7011 }
7012 return true;
7013}
7014
7015
Steve Blocka7e24c12009-10-30 11:49:00 +00007016uint32_t String::ComputeAndSetHash() {
7017 // Should only be called if hash code has not yet been computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007018 ASSERT(!HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00007019
Steve Block6ded16b2010-05-10 14:33:55 +01007020 const int len = length();
7021
Steve Blocka7e24c12009-10-30 11:49:00 +00007022 // Compute the hash code.
Steve Block6ded16b2010-05-10 14:33:55 +01007023 uint32_t field = 0;
7024 if (StringShape(this).IsSequentialAscii()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00007025 field = HashSequentialString(SeqAsciiString::cast(this)->GetChars(),
7026 len,
7027 GetHeap()->HashSeed());
Steve Block6ded16b2010-05-10 14:33:55 +01007028 } else if (StringShape(this).IsSequentialTwoByte()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00007029 field = HashSequentialString(SeqTwoByteString::cast(this)->GetChars(),
7030 len,
7031 GetHeap()->HashSeed());
Steve Block6ded16b2010-05-10 14:33:55 +01007032 } else {
7033 StringInputBuffer buffer(this);
Ben Murdochc7cc0282012-03-05 14:35:55 +00007034 field = ComputeHashField(&buffer, len, GetHeap()->HashSeed());
Steve Block6ded16b2010-05-10 14:33:55 +01007035 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007036
7037 // Store the hash code in the object.
Steve Blockd0582a62009-12-15 09:54:21 +00007038 set_hash_field(field);
Steve Blocka7e24c12009-10-30 11:49:00 +00007039
7040 // Check the hash code is there.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007041 ASSERT(HasHashCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00007042 uint32_t result = field >> kHashShift;
7043 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
7044 return result;
7045}
7046
7047
7048bool String::ComputeArrayIndex(unibrow::CharacterStream* buffer,
7049 uint32_t* index,
7050 int length) {
7051 if (length == 0 || length > kMaxArrayIndexSize) return false;
7052 uc32 ch = buffer->GetNext();
7053
7054 // If the string begins with a '0' character, it must only consist
7055 // of it to be a legal array index.
7056 if (ch == '0') {
7057 *index = 0;
7058 return length == 1;
7059 }
7060
7061 // Convert string to uint32 array index; character by character.
7062 int d = ch - '0';
7063 if (d < 0 || d > 9) return false;
7064 uint32_t result = d;
7065 while (buffer->has_more()) {
7066 d = buffer->GetNext() - '0';
7067 if (d < 0 || d > 9) return false;
7068 // Check that the new result is below the 32 bit limit.
7069 if (result > 429496729U - ((d > 5) ? 1 : 0)) return false;
7070 result = (result * 10) + d;
7071 }
7072
7073 *index = result;
7074 return true;
7075}
7076
7077
7078bool String::SlowAsArrayIndex(uint32_t* index) {
7079 if (length() <= kMaxCachedArrayIndexLength) {
7080 Hash(); // force computation of hash code
Steve Blockd0582a62009-12-15 09:54:21 +00007081 uint32_t field = hash_field();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007082 if ((field & kIsNotArrayIndexMask) != 0) return false;
Steve Blockd0582a62009-12-15 09:54:21 +00007083 // Isolate the array index form the full hash field.
7084 *index = (kArrayIndexHashMask & field) >> kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +00007085 return true;
7086 } else {
7087 StringInputBuffer buffer(this);
7088 return ComputeArrayIndex(&buffer, index, length());
7089 }
7090}
7091
7092
Iain Merrick9ac36c92010-09-13 15:29:50 +01007093uint32_t StringHasher::MakeArrayIndexHash(uint32_t value, int length) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007094 // For array indexes mix the length into the hash as an array index could
7095 // be zero.
7096 ASSERT(length > 0);
7097 ASSERT(length <= String::kMaxArrayIndexSize);
7098 ASSERT(TenToThe(String::kMaxCachedArrayIndexLength) <
7099 (1 << String::kArrayIndexValueBits));
Iain Merrick9ac36c92010-09-13 15:29:50 +01007100
7101 value <<= String::kHashShift;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007102 value |= length << String::kArrayIndexHashLengthShift;
Iain Merrick9ac36c92010-09-13 15:29:50 +01007103
7104 ASSERT((value & String::kIsNotArrayIndexMask) == 0);
7105 ASSERT((length > String::kMaxCachedArrayIndexLength) ||
7106 (value & String::kContainsCachedArrayIndexMask) == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007107 return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00007108}
7109
7110
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007111void StringHasher::AddSurrogatePair(uc32 c) {
7112 uint16_t lead = unibrow::Utf16::LeadSurrogate(c);
7113 AddCharacter(lead);
7114 uint16_t trail = unibrow::Utf16::TrailSurrogate(c);
7115 AddCharacter(trail);
7116}
7117
7118
7119void StringHasher::AddSurrogatePairNoIndex(uc32 c) {
7120 uint16_t lead = unibrow::Utf16::LeadSurrogate(c);
7121 AddCharacterNoIndex(lead);
7122 uint16_t trail = unibrow::Utf16::TrailSurrogate(c);
7123 AddCharacterNoIndex(trail);
7124}
7125
7126
Steve Blocka7e24c12009-10-30 11:49:00 +00007127uint32_t StringHasher::GetHashField() {
7128 ASSERT(is_valid());
Steve Blockd0582a62009-12-15 09:54:21 +00007129 if (length_ <= String::kMaxHashCalcLength) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007130 if (is_array_index()) {
Iain Merrick9ac36c92010-09-13 15:29:50 +01007131 return MakeArrayIndexHash(array_index(), length_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007132 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007133 return (GetHash() << String::kHashShift) | String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00007134 } else {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007135 return (length_ << String::kHashShift) | String::kIsNotArrayIndexMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00007136 }
7137}
7138
7139
Steve Blockd0582a62009-12-15 09:54:21 +00007140uint32_t String::ComputeHashField(unibrow::CharacterStream* buffer,
Ben Murdochc7cc0282012-03-05 14:35:55 +00007141 int length,
7142 uint32_t seed) {
7143 StringHasher hasher(length, seed);
Steve Blocka7e24c12009-10-30 11:49:00 +00007144
7145 // Very long strings have a trivial hash that doesn't inspect the
7146 // string contents.
7147 if (hasher.has_trivial_hash()) {
7148 return hasher.GetHashField();
7149 }
7150
7151 // Do the iterative array index computation as long as there is a
7152 // chance this is an array index.
7153 while (buffer->has_more() && hasher.is_array_index()) {
7154 hasher.AddCharacter(buffer->GetNext());
7155 }
7156
7157 // Process the remaining characters without updating the array
7158 // index.
7159 while (buffer->has_more()) {
7160 hasher.AddCharacterNoIndex(buffer->GetNext());
7161 }
7162
7163 return hasher.GetHashField();
7164}
7165
7166
John Reck59135872010-11-02 12:39:01 -07007167MaybeObject* String::SubString(int start, int end, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01007168 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00007169 if (start == 0 && end == length()) return this;
Steve Block44f0eee2011-05-26 01:26:41 +01007170 MaybeObject* result = heap->AllocateSubString(this, start, end, pretenure);
Steve Blockd0582a62009-12-15 09:54:21 +00007171 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00007172}
7173
7174
7175void String::PrintOn(FILE* file) {
7176 int length = this->length();
7177 for (int i = 0; i < length; i++) {
7178 fprintf(file, "%c", Get(i));
7179 }
7180}
7181
7182
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007183void Map::CreateOneBackPointer(Object* transition_target) {
7184 if (!transition_target->IsMap()) return;
7185 Map* target = Map::cast(transition_target);
7186#ifdef DEBUG
7187 // Verify target.
7188 Object* source_prototype = prototype();
7189 Object* target_prototype = target->prototype();
7190 ASSERT(source_prototype->IsJSReceiver() ||
7191 source_prototype->IsMap() ||
7192 source_prototype->IsNull());
7193 ASSERT(target_prototype->IsJSReceiver() ||
7194 target_prototype->IsNull());
7195 ASSERT(source_prototype->IsMap() ||
7196 source_prototype == target_prototype);
7197#endif
7198 // Point target back to source. set_prototype() will not let us set
7199 // the prototype to a map, as we do here.
7200 *RawField(target, kPrototypeOffset) = this;
7201}
7202
7203
Steve Blocka7e24c12009-10-30 11:49:00 +00007204void Map::CreateBackPointers() {
7205 DescriptorArray* descriptors = instance_descriptors();
7206 for (int i = 0; i < descriptors->number_of_descriptors(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007207 switch (descriptors->GetType(i)) {
7208 case MAP_TRANSITION:
7209 case CONSTANT_TRANSITION:
7210 CreateOneBackPointer(descriptors->GetValue(i));
7211 break;
7212 case ELEMENTS_TRANSITION: {
7213 Object* object = descriptors->GetValue(i);
7214 if (object->IsMap()) {
7215 CreateOneBackPointer(object);
7216 } else {
7217 FixedArray* array = FixedArray::cast(object);
7218 for (int i = 0; i < array->length(); ++i) {
7219 CreateOneBackPointer(array->get(i));
7220 }
7221 }
7222 break;
7223 }
7224 case CALLBACKS: {
7225 Object* object = descriptors->GetValue(i);
7226 if (object->IsAccessorPair()) {
7227 AccessorPair* accessors = AccessorPair::cast(object);
7228 CreateOneBackPointer(accessors->getter());
7229 CreateOneBackPointer(accessors->setter());
7230 }
7231 break;
7232 }
7233 case NORMAL:
7234 case FIELD:
7235 case CONSTANT_FUNCTION:
7236 case HANDLER:
7237 case INTERCEPTOR:
7238 case NULL_DESCRIPTOR:
7239 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007240 }
7241 }
7242}
7243
7244
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007245bool Map::RestoreOneBackPointer(Object* object,
7246 Object* real_prototype,
7247 bool* keep_entry) {
7248 if (!object->IsMap()) return false;
7249 Map* map = Map::cast(object);
7250 if (Marking::MarkBitFrom(map).Get()) {
7251 *keep_entry = true;
7252 return false;
7253 }
7254 ASSERT(map->prototype() == this || map->prototype() == real_prototype);
7255 // Getter prototype() is read-only, set_prototype() has side effects.
7256 *RawField(map, Map::kPrototypeOffset) = real_prototype;
7257 return true;
7258}
7259
7260
Steve Block44f0eee2011-05-26 01:26:41 +01007261void Map::ClearNonLiveTransitions(Heap* heap, Object* real_prototype) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007262 DescriptorArray* d = DescriptorArray::cast(
Ben Murdoch257744e2011-11-30 15:57:28 +00007263 *RawField(this, Map::kInstanceDescriptorsOrBitField3Offset));
7264 if (d->IsEmpty()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00007265 Smi* NullDescriptorDetails =
7266 PropertyDetails(NONE, NULL_DESCRIPTOR).AsSmi();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007267 FixedArray* contents = FixedArray::cast(
Steve Blocka7e24c12009-10-30 11:49:00 +00007268 d->get(DescriptorArray::kContentArrayIndex));
7269 ASSERT(contents->length() >= 2);
7270 for (int i = 0; i < contents->length(); i += 2) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007271 // If the pair (value, details) is a map transition, check if the target is
7272 // live. If not, null the descriptor. Also drop the back pointer for that
7273 // map transition, so that this map is not reached again by following a back
7274 // pointer from a non-live object.
7275 bool keep_entry = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00007276 PropertyDetails details(Smi::cast(contents->get(i + 1)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007277 switch (details.type()) {
7278 case MAP_TRANSITION:
7279 case CONSTANT_TRANSITION:
7280 RestoreOneBackPointer(contents->get(i), real_prototype, &keep_entry);
7281 break;
7282 case ELEMENTS_TRANSITION: {
7283 Object* object = contents->get(i);
7284 if (object->IsMap()) {
7285 RestoreOneBackPointer(object, real_prototype, &keep_entry);
7286 } else {
7287 FixedArray* array = FixedArray::cast(object);
7288 for (int j = 0; j < array->length(); ++j) {
7289 if (RestoreOneBackPointer(array->get(j),
7290 real_prototype,
7291 &keep_entry)) {
7292 array->set_undefined(j);
7293 }
7294 }
7295 }
7296 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00007297 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007298 case CALLBACKS: {
7299 Object* object = contents->get(i);
7300 if (object->IsAccessorPair()) {
7301 AccessorPair* accessors = AccessorPair::cast(object);
7302 if (RestoreOneBackPointer(accessors->getter(),
7303 real_prototype,
7304 &keep_entry)) {
7305 accessors->set_getter(heap->the_hole_value());
7306 }
7307 if (RestoreOneBackPointer(accessors->setter(),
7308 real_prototype,
7309 &keep_entry)) {
7310 accessors->set_setter(heap->the_hole_value());
7311 }
7312 } else {
7313 keep_entry = true;
7314 }
7315 break;
7316 }
7317 case NORMAL:
7318 case FIELD:
7319 case CONSTANT_FUNCTION:
7320 case HANDLER:
7321 case INTERCEPTOR:
7322 case NULL_DESCRIPTOR:
7323 keep_entry = true;
7324 break;
7325 }
7326 // Make sure that an entry containing only dead transitions gets collected.
7327 // What we *really* want to do here is removing this entry completely, but
7328 // for technical reasons we can't do this, so we zero it out instead.
7329 if (!keep_entry) {
7330 contents->set_unchecked(i + 1, NullDescriptorDetails);
7331 contents->set_null_unchecked(heap, i);
Steve Blocka7e24c12009-10-30 11:49:00 +00007332 }
7333 }
7334}
7335
7336
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007337int Map::Hash() {
7338 // For performance reasons we only hash the 3 most variable fields of a map:
7339 // constructor, prototype and bit_field2.
7340
7341 // Shift away the tag.
7342 int hash = (static_cast<uint32_t>(
7343 reinterpret_cast<uintptr_t>(constructor())) >> 2);
7344
7345 // XOR-ing the prototype and constructor directly yields too many zero bits
7346 // when the two pointers are close (which is fairly common).
7347 // To avoid this we shift the prototype 4 bits relatively to the constructor.
7348 hash ^= (static_cast<uint32_t>(
7349 reinterpret_cast<uintptr_t>(prototype())) << 2);
7350
7351 return hash ^ (hash >> 16) ^ bit_field2();
7352}
7353
7354
7355bool Map::EquivalentToForNormalization(Map* other,
7356 PropertyNormalizationMode mode) {
7357 return
7358 constructor() == other->constructor() &&
7359 prototype() == other->prototype() &&
7360 inobject_properties() == ((mode == CLEAR_INOBJECT_PROPERTIES) ?
7361 0 :
7362 other->inobject_properties()) &&
7363 instance_type() == other->instance_type() &&
7364 bit_field() == other->bit_field() &&
7365 bit_field2() == other->bit_field2() &&
7366 (bit_field3() & ~(1<<Map::kIsShared)) ==
7367 (other->bit_field3() & ~(1<<Map::kIsShared));
7368}
7369
7370
Steve Block791712a2010-08-27 10:21:07 +01007371void JSFunction::JSFunctionIterateBody(int object_size, ObjectVisitor* v) {
7372 // Iterate over all fields in the body but take care in dealing with
7373 // the code entry.
7374 IteratePointers(v, kPropertiesOffset, kCodeEntryOffset);
7375 v->VisitCodeEntry(this->address() + kCodeEntryOffset);
7376 IteratePointers(v, kCodeEntryOffset + kPointerSize, object_size);
7377}
7378
7379
Ben Murdochb0fe1622011-05-05 13:52:32 +01007380void JSFunction::MarkForLazyRecompilation() {
7381 ASSERT(is_compiled() && !IsOptimized());
Ben Murdochb8e0da22011-05-16 14:20:40 +01007382 ASSERT(shared()->allows_lazy_compilation() ||
7383 code()->optimizable());
Steve Block44f0eee2011-05-26 01:26:41 +01007384 Builtins* builtins = GetIsolate()->builtins();
7385 ReplaceCode(builtins->builtin(Builtins::kLazyRecompile));
Ben Murdochb0fe1622011-05-05 13:52:32 +01007386}
7387
7388
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007389bool SharedFunctionInfo::EnsureCompiled(Handle<SharedFunctionInfo> shared,
7390 ClearExceptionFlag flag) {
7391 return shared->is_compiled() || CompileLazy(shared, flag);
7392}
7393
7394
7395static bool CompileLazyHelper(CompilationInfo* info,
7396 ClearExceptionFlag flag) {
7397 // Compile the source information to a code object.
7398 ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled());
7399 ASSERT(!info->isolate()->has_pending_exception());
7400 bool result = Compiler::CompileLazy(info);
7401 ASSERT(result != Isolate::Current()->has_pending_exception());
7402 if (!result && flag == CLEAR_EXCEPTION) {
7403 info->isolate()->clear_pending_exception();
7404 }
7405 return result;
7406}
7407
7408
7409bool SharedFunctionInfo::CompileLazy(Handle<SharedFunctionInfo> shared,
7410 ClearExceptionFlag flag) {
7411 CompilationInfo info(shared);
7412 return CompileLazyHelper(&info, flag);
7413}
7414
7415
7416bool JSFunction::CompileLazy(Handle<JSFunction> function,
7417 ClearExceptionFlag flag) {
7418 bool result = true;
7419 if (function->shared()->is_compiled()) {
7420 function->ReplaceCode(function->shared()->code());
7421 function->shared()->set_code_age(0);
7422 } else {
7423 CompilationInfo info(function);
7424 result = CompileLazyHelper(&info, flag);
7425 ASSERT(!result || function->is_compiled());
7426 }
7427 return result;
7428}
7429
7430
7431bool JSFunction::CompileOptimized(Handle<JSFunction> function,
7432 int osr_ast_id,
7433 ClearExceptionFlag flag) {
7434 CompilationInfo info(function);
7435 info.SetOptimizing(osr_ast_id);
7436 return CompileLazyHelper(&info, flag);
7437}
7438
7439
Ben Murdochb0fe1622011-05-05 13:52:32 +01007440bool JSFunction::IsInlineable() {
7441 if (IsBuiltin()) return false;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007442 SharedFunctionInfo* shared_info = shared();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007443 // Check that the function has a script associated with it.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007444 if (!shared_info->script()->IsScript()) return false;
7445 if (shared_info->optimization_disabled()) return false;
7446 Code* code = shared_info->code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007447 if (code->kind() == Code::OPTIMIZED_FUNCTION) return true;
7448 // If we never ran this (unlikely) then lets try to optimize it.
7449 if (code->kind() != Code::FUNCTION) return true;
7450 return code->optimizable();
7451}
7452
7453
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007454MaybeObject* JSFunction::SetInstancePrototype(Object* value) {
7455 ASSERT(value->IsJSReceiver());
Steve Block44f0eee2011-05-26 01:26:41 +01007456 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00007457 if (has_initial_map()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007458 // If the function has allocated the initial map
7459 // replace it with a copy containing the new prototype.
7460 Map* new_map;
7461 MaybeObject* maybe_new_map = initial_map()->CopyDropTransitions();
7462 if (!maybe_new_map->To(&new_map)) return maybe_new_map;
7463 new_map->set_prototype(value);
7464 MaybeObject* maybe_object =
7465 set_initial_map_and_cache_transitions(new_map);
7466 if (maybe_object->IsFailure()) return maybe_object;
Steve Blocka7e24c12009-10-30 11:49:00 +00007467 } else {
7468 // Put the value in the initial map field until an initial map is
7469 // needed. At that point, a new initial map is created and the
7470 // prototype is put into the initial map where it belongs.
7471 set_prototype_or_initial_map(value);
7472 }
Steve Block44f0eee2011-05-26 01:26:41 +01007473 heap->ClearInstanceofCache();
Steve Blocka7e24c12009-10-30 11:49:00 +00007474 return value;
7475}
7476
7477
John Reck59135872010-11-02 12:39:01 -07007478MaybeObject* JSFunction::SetPrototype(Object* value) {
Steve Block6ded16b2010-05-10 14:33:55 +01007479 ASSERT(should_have_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +00007480 Object* construct_prototype = value;
7481
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007482 // If the value is not a JSReceiver, store the value in the map's
Steve Blocka7e24c12009-10-30 11:49:00 +00007483 // constructor field so it can be accessed. Also, set the prototype
7484 // used for constructing objects to the original object prototype.
7485 // See ECMA-262 13.2.2.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007486 if (!value->IsJSReceiver()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007487 // Copy the map so this does not affect unrelated functions.
7488 // Remove map transitions because they point to maps with a
7489 // different prototype.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007490 Map* new_map;
John Reck59135872010-11-02 12:39:01 -07007491 { MaybeObject* maybe_new_map = map()->CopyDropTransitions();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007492 if (!maybe_new_map->To(&new_map)) return maybe_new_map;
John Reck59135872010-11-02 12:39:01 -07007493 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007494 Heap* heap = new_map->GetHeap();
Ben Murdoch8b112d22011-06-08 16:22:53 +01007495 set_map(new_map);
7496 new_map->set_constructor(value);
7497 new_map->set_non_instance_prototype(true);
Steve Blocka7e24c12009-10-30 11:49:00 +00007498 construct_prototype =
Steve Block44f0eee2011-05-26 01:26:41 +01007499 heap->isolate()->context()->global_context()->
7500 initial_object_prototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00007501 } else {
7502 map()->set_non_instance_prototype(false);
7503 }
7504
7505 return SetInstancePrototype(construct_prototype);
7506}
7507
7508
Steve Block6ded16b2010-05-10 14:33:55 +01007509Object* JSFunction::RemovePrototype() {
Steve Block44f0eee2011-05-26 01:26:41 +01007510 Context* global_context = context()->global_context();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007511 Map* no_prototype_map = shared()->is_classic_mode()
7512 ? global_context->function_without_prototype_map()
7513 : global_context->strict_mode_function_without_prototype_map();
Steve Block44f0eee2011-05-26 01:26:41 +01007514
7515 if (map() == no_prototype_map) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007516 // Be idempotent.
7517 return this;
7518 }
Steve Block44f0eee2011-05-26 01:26:41 +01007519
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007520 ASSERT(map() == (shared()->is_classic_mode()
7521 ? global_context->function_map()
7522 : global_context->strict_mode_function_map()));
Steve Block44f0eee2011-05-26 01:26:41 +01007523
7524 set_map(no_prototype_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007525 set_prototype_or_initial_map(no_prototype_map->GetHeap()->the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +01007526 return this;
7527}
7528
7529
Steve Blocka7e24c12009-10-30 11:49:00 +00007530Object* JSFunction::SetInstanceClassName(String* name) {
7531 shared()->set_instance_class_name(name);
7532 return this;
7533}
7534
7535
Ben Murdochb0fe1622011-05-05 13:52:32 +01007536void JSFunction::PrintName(FILE* out) {
Ben Murdoch589d6972011-11-30 16:04:58 +00007537 SmartArrayPointer<char> name = shared()->DebugName()->ToCString();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007538 PrintF(out, "%s", *name);
7539}
7540
7541
Steve Blocka7e24c12009-10-30 11:49:00 +00007542Context* JSFunction::GlobalContextFromLiterals(FixedArray* literals) {
7543 return Context::cast(literals->get(JSFunction::kLiteralGlobalContextIndex));
7544}
7545
7546
Steve Block44f0eee2011-05-26 01:26:41 +01007547MaybeObject* Oddball::Initialize(const char* to_string,
7548 Object* to_number,
7549 byte kind) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007550 String* symbol;
Steve Block44f0eee2011-05-26 01:26:41 +01007551 { MaybeObject* maybe_symbol =
7552 Isolate::Current()->heap()->LookupAsciiSymbol(to_string);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007553 if (!maybe_symbol->To(&symbol)) return maybe_symbol;
John Reck59135872010-11-02 12:39:01 -07007554 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007555 set_to_string(symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00007556 set_to_number(to_number);
Steve Block44f0eee2011-05-26 01:26:41 +01007557 set_kind(kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00007558 return this;
7559}
7560
7561
Ben Murdochf87a2032010-10-22 12:50:53 +01007562String* SharedFunctionInfo::DebugName() {
7563 Object* n = name();
7564 if (!n->IsString() || String::cast(n)->length() == 0) return inferred_name();
7565 return String::cast(n);
7566}
7567
7568
Steve Blocka7e24c12009-10-30 11:49:00 +00007569bool SharedFunctionInfo::HasSourceCode() {
7570 return !script()->IsUndefined() &&
Iain Merrick75681382010-08-19 15:07:18 +01007571 !reinterpret_cast<Script*>(script())->source()->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +00007572}
7573
7574
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007575Handle<Object> SharedFunctionInfo::GetSourceCode() {
7576 if (!HasSourceCode()) return GetIsolate()->factory()->undefined_value();
7577 Handle<String> source(String::cast(Script::cast(script())->source()));
7578 return SubString(source, start_position(), end_position());
Steve Blocka7e24c12009-10-30 11:49:00 +00007579}
7580
7581
Ben Murdochb0fe1622011-05-05 13:52:32 +01007582int SharedFunctionInfo::SourceSize() {
7583 return end_position() - start_position();
7584}
7585
7586
Steve Blocka7e24c12009-10-30 11:49:00 +00007587int SharedFunctionInfo::CalculateInstanceSize() {
7588 int instance_size =
7589 JSObject::kHeaderSize +
7590 expected_nof_properties() * kPointerSize;
7591 if (instance_size > JSObject::kMaxInstanceSize) {
7592 instance_size = JSObject::kMaxInstanceSize;
7593 }
7594 return instance_size;
7595}
7596
7597
7598int SharedFunctionInfo::CalculateInObjectProperties() {
7599 return (CalculateInstanceSize() - JSObject::kHeaderSize) / kPointerSize;
7600}
7601
7602
Andrei Popescu402d9372010-02-26 13:31:12 +00007603bool SharedFunctionInfo::CanGenerateInlineConstructor(Object* prototype) {
7604 // Check the basic conditions for generating inline constructor code.
7605 if (!FLAG_inline_new
7606 || !has_only_simple_this_property_assignments()
7607 || this_property_assignments_count() == 0) {
7608 return false;
7609 }
7610
7611 // If the prototype is null inline constructors cause no problems.
7612 if (!prototype->IsJSObject()) {
7613 ASSERT(prototype->IsNull());
7614 return true;
7615 }
7616
Ben Murdoch8b112d22011-06-08 16:22:53 +01007617 Heap* heap = GetHeap();
7618
Andrei Popescu402d9372010-02-26 13:31:12 +00007619 // Traverse the proposed prototype chain looking for setters for properties of
7620 // the same names as are set by the inline constructor.
7621 for (Object* obj = prototype;
Steve Block44f0eee2011-05-26 01:26:41 +01007622 obj != heap->null_value();
Andrei Popescu402d9372010-02-26 13:31:12 +00007623 obj = obj->GetPrototype()) {
7624 JSObject* js_object = JSObject::cast(obj);
7625 for (int i = 0; i < this_property_assignments_count(); i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007626 LookupResult result(heap->isolate());
Andrei Popescu402d9372010-02-26 13:31:12 +00007627 String* name = GetThisPropertyAssignmentName(i);
7628 js_object->LocalLookupRealNamedProperty(name, &result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007629 if (result.IsFound() && result.type() == CALLBACKS) {
Andrei Popescu402d9372010-02-26 13:31:12 +00007630 return false;
7631 }
7632 }
7633 }
7634
7635 return true;
7636}
7637
7638
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007639void SharedFunctionInfo::ForbidInlineConstructor() {
7640 set_compiler_hints(BooleanBit::set(compiler_hints(),
7641 kHasOnlySimpleThisPropertyAssignments,
7642 false));
7643}
7644
7645
Steve Blocka7e24c12009-10-30 11:49:00 +00007646void SharedFunctionInfo::SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00007647 bool only_simple_this_property_assignments,
7648 FixedArray* assignments) {
7649 set_compiler_hints(BooleanBit::set(compiler_hints(),
Steve Blocka7e24c12009-10-30 11:49:00 +00007650 kHasOnlySimpleThisPropertyAssignments,
7651 only_simple_this_property_assignments));
7652 set_this_property_assignments(assignments);
7653 set_this_property_assignments_count(assignments->length() / 3);
7654}
7655
7656
7657void SharedFunctionInfo::ClearThisPropertyAssignmentsInfo() {
Steve Block44f0eee2011-05-26 01:26:41 +01007658 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00007659 set_compiler_hints(BooleanBit::set(compiler_hints(),
Steve Blocka7e24c12009-10-30 11:49:00 +00007660 kHasOnlySimpleThisPropertyAssignments,
7661 false));
Steve Block44f0eee2011-05-26 01:26:41 +01007662 set_this_property_assignments(heap->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00007663 set_this_property_assignments_count(0);
7664}
7665
7666
7667String* SharedFunctionInfo::GetThisPropertyAssignmentName(int index) {
7668 Object* obj = this_property_assignments();
7669 ASSERT(obj->IsFixedArray());
7670 ASSERT(index < this_property_assignments_count());
7671 obj = FixedArray::cast(obj)->get(index * 3);
7672 ASSERT(obj->IsString());
7673 return String::cast(obj);
7674}
7675
7676
7677bool SharedFunctionInfo::IsThisPropertyAssignmentArgument(int index) {
7678 Object* obj = this_property_assignments();
7679 ASSERT(obj->IsFixedArray());
7680 ASSERT(index < this_property_assignments_count());
7681 obj = FixedArray::cast(obj)->get(index * 3 + 1);
7682 return Smi::cast(obj)->value() != -1;
7683}
7684
7685
7686int SharedFunctionInfo::GetThisPropertyAssignmentArgument(int index) {
7687 ASSERT(IsThisPropertyAssignmentArgument(index));
7688 Object* obj =
7689 FixedArray::cast(this_property_assignments())->get(index * 3 + 1);
7690 return Smi::cast(obj)->value();
7691}
7692
7693
7694Object* SharedFunctionInfo::GetThisPropertyAssignmentConstant(int index) {
7695 ASSERT(!IsThisPropertyAssignmentArgument(index));
7696 Object* obj =
7697 FixedArray::cast(this_property_assignments())->get(index * 3 + 2);
7698 return obj;
7699}
7700
7701
Steve Blocka7e24c12009-10-30 11:49:00 +00007702// Support function for printing the source code to a StringStream
7703// without any allocation in the heap.
7704void SharedFunctionInfo::SourceCodePrint(StringStream* accumulator,
7705 int max_length) {
7706 // For some native functions there is no source.
Ben Murdochb0fe1622011-05-05 13:52:32 +01007707 if (!HasSourceCode()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007708 accumulator->Add("<No Source>");
7709 return;
7710 }
7711
Steve Blockd0582a62009-12-15 09:54:21 +00007712 // Get the source for the script which this function came from.
Steve Blocka7e24c12009-10-30 11:49:00 +00007713 // Don't use String::cast because we don't want more assertion errors while
7714 // we are already creating a stack dump.
7715 String* script_source =
7716 reinterpret_cast<String*>(Script::cast(script())->source());
7717
7718 if (!script_source->LooksValid()) {
7719 accumulator->Add("<Invalid Source>");
7720 return;
7721 }
7722
7723 if (!is_toplevel()) {
7724 accumulator->Add("function ");
7725 Object* name = this->name();
7726 if (name->IsString() && String::cast(name)->length() > 0) {
7727 accumulator->PrintName(name);
7728 }
7729 }
7730
7731 int len = end_position() - start_position();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007732 if (len <= max_length || max_length < 0) {
7733 accumulator->Put(script_source, start_position(), end_position());
7734 } else {
Steve Blocka7e24c12009-10-30 11:49:00 +00007735 accumulator->Put(script_source,
7736 start_position(),
7737 start_position() + max_length);
7738 accumulator->Add("...\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00007739 }
7740}
7741
7742
Ben Murdochb0fe1622011-05-05 13:52:32 +01007743static bool IsCodeEquivalent(Code* code, Code* recompiled) {
7744 if (code->instruction_size() != recompiled->instruction_size()) return false;
7745 ByteArray* code_relocation = code->relocation_info();
7746 ByteArray* recompiled_relocation = recompiled->relocation_info();
7747 int length = code_relocation->length();
7748 if (length != recompiled_relocation->length()) return false;
7749 int compare = memcmp(code_relocation->GetDataStartAddress(),
7750 recompiled_relocation->GetDataStartAddress(),
7751 length);
7752 return compare == 0;
7753}
7754
7755
7756void SharedFunctionInfo::EnableDeoptimizationSupport(Code* recompiled) {
7757 ASSERT(!has_deoptimization_support());
7758 AssertNoAllocation no_allocation;
7759 Code* code = this->code();
7760 if (IsCodeEquivalent(code, recompiled)) {
7761 // Copy the deoptimization data from the recompiled code.
7762 code->set_deoptimization_data(recompiled->deoptimization_data());
7763 code->set_has_deoptimization_support(true);
7764 } else {
7765 // TODO(3025757): In case the recompiled isn't equivalent to the
7766 // old code, we have to replace it. We should try to avoid this
7767 // altogether because it flushes valuable type feedback by
7768 // effectively resetting all IC state.
7769 set_code(recompiled);
7770 }
7771 ASSERT(has_deoptimization_support());
7772}
7773
7774
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007775void SharedFunctionInfo::DisableOptimization() {
Ben Murdoch257744e2011-11-30 15:57:28 +00007776 // Disable optimization for the shared function info and mark the
7777 // code as non-optimizable. The marker on the shared function info
7778 // is there because we flush non-optimized code thereby loosing the
7779 // non-optimizable information for the code. When the code is
7780 // regenerated and set on the shared function info it is marked as
7781 // non-optimizable if optimization is disabled for the shared
7782 // function info.
7783 set_optimization_disabled(true);
7784 // Code should be the lazy compilation stub or else unoptimized. If the
7785 // latter, disable optimization for the code too.
7786 ASSERT(code()->kind() == Code::FUNCTION || code()->kind() == Code::BUILTIN);
7787 if (code()->kind() == Code::FUNCTION) {
7788 code()->set_optimizable(false);
7789 }
7790 if (FLAG_trace_opt) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007791 PrintF("[disabled optimization for %s]\n", *DebugName()->ToCString());
Ben Murdoch257744e2011-11-30 15:57:28 +00007792 }
7793}
7794
7795
Ben Murdochb0fe1622011-05-05 13:52:32 +01007796bool SharedFunctionInfo::VerifyBailoutId(int id) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01007797 ASSERT(id != AstNode::kNoNumber);
7798 Code* unoptimized = code();
7799 DeoptimizationOutputData* data =
7800 DeoptimizationOutputData::cast(unoptimized->deoptimization_data());
7801 unsigned ignore = Deoptimizer::GetOutputInfo(data, id, this);
7802 USE(ignore);
7803 return true; // Return true if there was no ASSERT.
7804}
7805
7806
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007807void SharedFunctionInfo::StartInobjectSlackTracking(Map* map) {
7808 ASSERT(!IsInobjectSlackTrackingInProgress());
7809
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007810 if (!FLAG_clever_optimizations) return;
7811
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007812 // Only initiate the tracking the first time.
7813 if (live_objects_may_exist()) return;
7814 set_live_objects_may_exist(true);
7815
7816 // No tracking during the snapshot construction phase.
7817 if (Serializer::enabled()) return;
7818
7819 if (map->unused_property_fields() == 0) return;
7820
7821 // Nonzero counter is a leftover from the previous attempt interrupted
7822 // by GC, keep it.
7823 if (construction_count() == 0) {
7824 set_construction_count(kGenerousAllocationCount);
7825 }
7826 set_initial_map(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007827 Builtins* builtins = map->GetHeap()->isolate()->builtins();
Steve Block44f0eee2011-05-26 01:26:41 +01007828 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007829 construct_stub());
Steve Block44f0eee2011-05-26 01:26:41 +01007830 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007831}
7832
7833
7834// Called from GC, hence reinterpret_cast and unchecked accessors.
7835void SharedFunctionInfo::DetachInitialMap() {
7836 Map* map = reinterpret_cast<Map*>(initial_map());
7837
7838 // Make the map remember to restore the link if it survives the GC.
7839 map->set_bit_field2(
7840 map->bit_field2() | (1 << Map::kAttachedToSharedFunctionInfo));
7841
7842 // Undo state changes made by StartInobjectTracking (except the
7843 // construction_count). This way if the initial map does not survive the GC
7844 // then StartInobjectTracking will be called again the next time the
7845 // constructor is called. The countdown will continue and (possibly after
7846 // several more GCs) CompleteInobjectSlackTracking will eventually be called.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007847 Heap* heap = map->GetHeap();
7848 set_initial_map(heap->raw_unchecked_undefined_value());
7849 Builtins* builtins = heap->isolate()->builtins();
Steve Block44f0eee2011-05-26 01:26:41 +01007850 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007851 *RawField(this, kConstructStubOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01007852 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007853 // It is safe to clear the flag: it will be set again if the map is live.
7854 set_live_objects_may_exist(false);
7855}
7856
7857
7858// Called from GC, hence reinterpret_cast and unchecked accessors.
7859void SharedFunctionInfo::AttachInitialMap(Map* map) {
7860 map->set_bit_field2(
7861 map->bit_field2() & ~(1 << Map::kAttachedToSharedFunctionInfo));
7862
7863 // Resume inobject slack tracking.
7864 set_initial_map(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007865 Builtins* builtins = map->GetHeap()->isolate()->builtins();
Steve Block44f0eee2011-05-26 01:26:41 +01007866 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubGeneric),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007867 *RawField(this, kConstructStubOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01007868 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubCountdown));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007869 // The map survived the gc, so there may be objects referencing it.
7870 set_live_objects_may_exist(true);
7871}
7872
7873
Ben Murdoch8f9999f2012-04-23 10:39:17 +01007874void SharedFunctionInfo::ResetForNewContext(int new_ic_age) {
7875 code()->ClearInlineCaches();
7876 set_ic_age(new_ic_age);
7877 if (code()->kind() == Code::FUNCTION) {
7878 code()->set_profiler_ticks(0);
7879 if (optimization_disabled() &&
7880 opt_count() >= Compiler::kDefaultMaxOptCount) {
7881 // Re-enable optimizations if they were disabled due to opt_count limit.
7882 set_optimization_disabled(false);
7883 code()->set_optimizable(true);
7884 }
7885 set_opt_count(0);
7886 }
7887}
7888
7889
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007890static void GetMinInobjectSlack(Map* map, void* data) {
7891 int slack = map->unused_property_fields();
7892 if (*reinterpret_cast<int*>(data) > slack) {
7893 *reinterpret_cast<int*>(data) = slack;
7894 }
7895}
7896
7897
7898static void ShrinkInstanceSize(Map* map, void* data) {
7899 int slack = *reinterpret_cast<int*>(data);
7900 map->set_inobject_properties(map->inobject_properties() - slack);
7901 map->set_unused_property_fields(map->unused_property_fields() - slack);
7902 map->set_instance_size(map->instance_size() - slack * kPointerSize);
7903
7904 // Visitor id might depend on the instance size, recalculate it.
7905 map->set_visitor_id(StaticVisitorBase::GetVisitorId(map));
7906}
7907
7908
7909void SharedFunctionInfo::CompleteInobjectSlackTracking() {
7910 ASSERT(live_objects_may_exist() && IsInobjectSlackTrackingInProgress());
7911 Map* map = Map::cast(initial_map());
7912
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007913 Heap* heap = map->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01007914 set_initial_map(heap->undefined_value());
7915 Builtins* builtins = heap->isolate()->builtins();
7916 ASSERT_EQ(builtins->builtin(Builtins::kJSConstructStubCountdown),
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007917 construct_stub());
Steve Block44f0eee2011-05-26 01:26:41 +01007918 set_construct_stub(builtins->builtin(Builtins::kJSConstructStubGeneric));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007919
7920 int slack = map->unused_property_fields();
7921 map->TraverseTransitionTree(&GetMinInobjectSlack, &slack);
7922 if (slack != 0) {
7923 // Resize the initial map and all maps in its transition tree.
7924 map->TraverseTransitionTree(&ShrinkInstanceSize, &slack);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007925
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007926 // Give the correct expected_nof_properties to initial maps created later.
7927 ASSERT(expected_nof_properties() >= slack);
7928 set_expected_nof_properties(expected_nof_properties() - slack);
7929 }
7930}
7931
7932
Ben Murdoch8f9999f2012-04-23 10:39:17 +01007933void SharedFunctionInfo::SharedFunctionInfoIterateBody(ObjectVisitor* v) {
7934 v->VisitSharedFunctionInfo(this);
7935 SharedFunctionInfo::BodyDescriptor::IterateBody(this, v);
7936}
7937
7938
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007939#define DECLARE_TAG(ignore1, name, ignore2) name,
7940const char* const VisitorSynchronization::kTags[
7941 VisitorSynchronization::kNumberOfSyncTags] = {
7942 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
7943};
7944#undef DECLARE_TAG
7945
7946
7947#define DECLARE_TAG(ignore1, ignore2, name) name,
7948const char* const VisitorSynchronization::kTagNames[
7949 VisitorSynchronization::kNumberOfSyncTags] = {
7950 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_TAG)
7951};
7952#undef DECLARE_TAG
7953
7954
Steve Blocka7e24c12009-10-30 11:49:00 +00007955void ObjectVisitor::VisitCodeTarget(RelocInfo* rinfo) {
7956 ASSERT(RelocInfo::IsCodeTarget(rinfo->rmode()));
7957 Object* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
7958 Object* old_target = target;
7959 VisitPointer(&target);
7960 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
7961}
7962
7963
Steve Block791712a2010-08-27 10:21:07 +01007964void ObjectVisitor::VisitCodeEntry(Address entry_address) {
7965 Object* code = Code::GetObjectFromEntryAddress(entry_address);
7966 Object* old_code = code;
7967 VisitPointer(&code);
7968 if (code != old_code) {
7969 Memory::Address_at(entry_address) = reinterpret_cast<Code*>(code)->entry();
7970 }
7971}
7972
7973
Ben Murdochb0fe1622011-05-05 13:52:32 +01007974void ObjectVisitor::VisitGlobalPropertyCell(RelocInfo* rinfo) {
7975 ASSERT(rinfo->rmode() == RelocInfo::GLOBAL_PROPERTY_CELL);
7976 Object* cell = rinfo->target_cell();
7977 Object* old_cell = cell;
7978 VisitPointer(&cell);
7979 if (cell != old_cell) {
7980 rinfo->set_target_cell(reinterpret_cast<JSGlobalPropertyCell*>(cell));
7981 }
7982}
7983
7984
Steve Blocka7e24c12009-10-30 11:49:00 +00007985void ObjectVisitor::VisitDebugTarget(RelocInfo* rinfo) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007986 ASSERT((RelocInfo::IsJSReturn(rinfo->rmode()) &&
7987 rinfo->IsPatchedReturnSequence()) ||
7988 (RelocInfo::IsDebugBreakSlot(rinfo->rmode()) &&
7989 rinfo->IsPatchedDebugBreakSlotSequence()));
Steve Blocka7e24c12009-10-30 11:49:00 +00007990 Object* target = Code::GetCodeFromTargetAddress(rinfo->call_address());
7991 Object* old_target = target;
7992 VisitPointer(&target);
7993 CHECK_EQ(target, old_target); // VisitPointer doesn't change Code* *target.
7994}
7995
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007996void ObjectVisitor::VisitEmbeddedPointer(RelocInfo* rinfo) {
7997 ASSERT(rinfo->rmode() == RelocInfo::EMBEDDED_OBJECT);
7998 VisitPointer(rinfo->target_object_address());
7999}
8000
8001void ObjectVisitor::VisitExternalReference(RelocInfo* rinfo) {
8002 Address* p = rinfo->target_reference_address();
8003 VisitExternalReferences(p, p + 1);
8004}
8005
Ben Murdochb0fe1622011-05-05 13:52:32 +01008006void Code::InvalidateRelocation() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008007 set_relocation_info(GetHeap()->empty_byte_array());
Ben Murdochb0fe1622011-05-05 13:52:32 +01008008}
8009
8010
Steve Blockd0582a62009-12-15 09:54:21 +00008011void Code::Relocate(intptr_t delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008012 for (RelocIterator it(this, RelocInfo::kApplyMask); !it.done(); it.next()) {
8013 it.rinfo()->apply(delta);
8014 }
8015 CPU::FlushICache(instruction_start(), instruction_size());
8016}
8017
8018
8019void Code::CopyFrom(const CodeDesc& desc) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008020 ASSERT(Marking::Color(this) == Marking::WHITE_OBJECT);
8021
Steve Blocka7e24c12009-10-30 11:49:00 +00008022 // copy code
8023 memmove(instruction_start(), desc.buffer, desc.instr_size);
8024
Steve Blocka7e24c12009-10-30 11:49:00 +00008025 // copy reloc info
8026 memmove(relocation_start(),
8027 desc.buffer + desc.buffer_size - desc.reloc_size,
8028 desc.reloc_size);
8029
8030 // unbox handles and relocate
Steve Block3ce2e202009-11-05 08:53:23 +00008031 intptr_t delta = instruction_start() - desc.buffer;
Steve Blocka7e24c12009-10-30 11:49:00 +00008032 int mode_mask = RelocInfo::kCodeTargetMask |
8033 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
Ben Murdochb0fe1622011-05-05 13:52:32 +01008034 RelocInfo::ModeMask(RelocInfo::GLOBAL_PROPERTY_CELL) |
Steve Blocka7e24c12009-10-30 11:49:00 +00008035 RelocInfo::kApplyMask;
Steve Block3ce2e202009-11-05 08:53:23 +00008036 Assembler* origin = desc.origin; // Needed to find target_object on X64.
Steve Blocka7e24c12009-10-30 11:49:00 +00008037 for (RelocIterator it(this, mode_mask); !it.done(); it.next()) {
8038 RelocInfo::Mode mode = it.rinfo()->rmode();
8039 if (mode == RelocInfo::EMBEDDED_OBJECT) {
Steve Block3ce2e202009-11-05 08:53:23 +00008040 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008041 it.rinfo()->set_target_object(*p, SKIP_WRITE_BARRIER);
Ben Murdochb0fe1622011-05-05 13:52:32 +01008042 } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008043 Handle<JSGlobalPropertyCell> cell = it.rinfo()->target_cell_handle();
8044 it.rinfo()->set_target_cell(*cell, SKIP_WRITE_BARRIER);
Steve Blocka7e24c12009-10-30 11:49:00 +00008045 } else if (RelocInfo::IsCodeTarget(mode)) {
8046 // rewrite code handles in inline cache targets to direct
8047 // pointers to the first instruction in the code object
Steve Block3ce2e202009-11-05 08:53:23 +00008048 Handle<Object> p = it.rinfo()->target_object_handle(origin);
Steve Blocka7e24c12009-10-30 11:49:00 +00008049 Code* code = Code::cast(*p);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008050 it.rinfo()->set_target_address(code->instruction_start(),
8051 SKIP_WRITE_BARRIER);
Steve Blocka7e24c12009-10-30 11:49:00 +00008052 } else {
8053 it.rinfo()->apply(delta);
8054 }
8055 }
8056 CPU::FlushICache(instruction_start(), instruction_size());
8057}
8058
8059
8060// Locate the source position which is closest to the address in the code. This
8061// is using the source position information embedded in the relocation info.
8062// The position returned is relative to the beginning of the script where the
8063// source for this function is found.
8064int Code::SourcePosition(Address pc) {
8065 int distance = kMaxInt;
8066 int position = RelocInfo::kNoPosition; // Initially no position found.
8067 // Run through all the relocation info to find the best matching source
8068 // position. All the code needs to be considered as the sequence of the
8069 // instructions in the code does not necessarily follow the same order as the
8070 // source.
8071 RelocIterator it(this, RelocInfo::kPositionMask);
8072 while (!it.done()) {
8073 // Only look at positions after the current pc.
8074 if (it.rinfo()->pc() < pc) {
8075 // Get position and distance.
Steve Blockd0582a62009-12-15 09:54:21 +00008076
8077 int dist = static_cast<int>(pc - it.rinfo()->pc());
8078 int pos = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +00008079 // If this position is closer than the current candidate or if it has the
8080 // same distance as the current candidate and the position is higher then
8081 // this position is the new candidate.
8082 if ((dist < distance) ||
8083 (dist == distance && pos > position)) {
8084 position = pos;
8085 distance = dist;
8086 }
8087 }
8088 it.next();
8089 }
8090 return position;
8091}
8092
8093
8094// Same as Code::SourcePosition above except it only looks for statement
8095// positions.
8096int Code::SourceStatementPosition(Address pc) {
8097 // First find the position as close as possible using all position
8098 // information.
8099 int position = SourcePosition(pc);
8100 // Now find the closest statement position before the position.
8101 int statement_position = 0;
8102 RelocIterator it(this, RelocInfo::kPositionMask);
8103 while (!it.done()) {
8104 if (RelocInfo::IsStatementPosition(it.rinfo()->rmode())) {
Steve Blockd0582a62009-12-15 09:54:21 +00008105 int p = static_cast<int>(it.rinfo()->data());
Steve Blocka7e24c12009-10-30 11:49:00 +00008106 if (statement_position < p && p <= position) {
8107 statement_position = p;
8108 }
8109 }
8110 it.next();
8111 }
8112 return statement_position;
8113}
8114
8115
Ben Murdochb8e0da22011-05-16 14:20:40 +01008116SafepointEntry Code::GetSafepointEntry(Address pc) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008117 SafepointTable table(this);
Ben Murdochb8e0da22011-05-16 14:20:40 +01008118 return table.FindEntry(pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01008119}
8120
8121
8122void Code::SetNoStackCheckTable() {
8123 // Indicate the absence of a stack-check table by a table start after the
8124 // end of the instructions. Table start must be aligned, so round up.
Steve Block1e0659c2011-05-24 12:43:12 +01008125 set_stack_check_table_offset(RoundUp(instruction_size(), kIntSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01008126}
8127
8128
8129Map* Code::FindFirstMap() {
8130 ASSERT(is_inline_cache_stub());
8131 AssertNoAllocation no_allocation;
8132 int mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT);
8133 for (RelocIterator it(this, mask); !it.done(); it.next()) {
8134 RelocInfo* info = it.rinfo();
8135 Object* object = info->target_object();
8136 if (object->IsMap()) return Map::cast(object);
8137 }
8138 return NULL;
8139}
8140
8141
Ben Murdoch8f9999f2012-04-23 10:39:17 +01008142void Code::ClearInlineCaches() {
8143 int mask = RelocInfo::ModeMask(RelocInfo::CODE_TARGET) |
8144 RelocInfo::ModeMask(RelocInfo::CONSTRUCT_CALL) |
8145 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_WITH_ID) |
8146 RelocInfo::ModeMask(RelocInfo::CODE_TARGET_CONTEXT);
8147 for (RelocIterator it(this, mask); !it.done(); it.next()) {
8148 RelocInfo* info = it.rinfo();
8149 Code* target(Code::GetCodeFromTargetAddress(info->target_address()));
8150 if (target->is_inline_cache_stub()) {
8151 IC::Clear(info->pc());
8152 }
8153 }
8154}
8155
8156
Steve Blocka7e24c12009-10-30 11:49:00 +00008157#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +01008158
Ben Murdochb0fe1622011-05-05 13:52:32 +01008159void DeoptimizationInputData::DeoptimizationInputDataPrint(FILE* out) {
8160 disasm::NameConverter converter;
8161 int deopt_count = DeoptCount();
8162 PrintF(out, "Deoptimization Input Data (deopt points = %d)\n", deopt_count);
8163 if (0 == deopt_count) return;
8164
Ben Murdoch2b4ba112012-01-20 14:57:15 +00008165 PrintF(out, "%6s %6s %6s %6s %12s\n", "index", "ast id", "argc", "pc",
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008166 FLAG_print_code_verbose ? "commands" : "");
Ben Murdochb0fe1622011-05-05 13:52:32 +01008167 for (int i = 0; i < deopt_count; i++) {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00008168 PrintF(out, "%6d %6d %6d %6d",
8169 i,
8170 AstId(i)->value(),
8171 ArgumentsStackHeight(i)->value(),
8172 Pc(i)->value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008173
8174 if (!FLAG_print_code_verbose) {
8175 PrintF(out, "\n");
8176 continue;
8177 }
8178 // Print details of the frame translation.
Ben Murdochb0fe1622011-05-05 13:52:32 +01008179 int translation_index = TranslationIndex(i)->value();
8180 TranslationIterator iterator(TranslationByteArray(), translation_index);
8181 Translation::Opcode opcode =
8182 static_cast<Translation::Opcode>(iterator.Next());
8183 ASSERT(Translation::BEGIN == opcode);
8184 int frame_count = iterator.Next();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008185 int jsframe_count = iterator.Next();
8186 PrintF(out, " %s {frame count=%d, js frame count=%d}\n",
8187 Translation::StringFor(opcode),
8188 frame_count,
8189 jsframe_count);
Ben Murdochb0fe1622011-05-05 13:52:32 +01008190
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008191 while (iterator.HasNext() &&
8192 Translation::BEGIN !=
8193 (opcode = static_cast<Translation::Opcode>(iterator.Next()))) {
8194 PrintF(out, "%24s %s ", "", Translation::StringFor(opcode));
8195
8196 switch (opcode) {
8197 case Translation::BEGIN:
8198 UNREACHABLE();
8199 break;
8200
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008201 case Translation::JS_FRAME: {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008202 int ast_id = iterator.Next();
8203 int function_id = iterator.Next();
8204 JSFunction* function =
8205 JSFunction::cast(LiteralArray()->get(function_id));
8206 unsigned height = iterator.Next();
Ben Murdoch589d6972011-11-30 16:04:58 +00008207 PrintF(out, "{ast_id=%d, function=", ast_id);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008208 function->PrintName(out);
8209 PrintF(out, ", height=%u}", height);
8210 break;
8211 }
8212
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008213 case Translation::ARGUMENTS_ADAPTOR_FRAME:
8214 case Translation::CONSTRUCT_STUB_FRAME: {
8215 int function_id = iterator.Next();
8216 JSFunction* function =
8217 JSFunction::cast(LiteralArray()->get(function_id));
8218 unsigned height = iterator.Next();
8219 PrintF(out, "{function=");
8220 function->PrintName(out);
8221 PrintF(out, ", height=%u}", height);
8222 break;
8223 }
8224
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008225 case Translation::DUPLICATE:
8226 break;
8227
8228 case Translation::REGISTER: {
8229 int reg_code = iterator.Next();
8230 PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code));
8231 break;
8232 }
8233
8234 case Translation::INT32_REGISTER: {
8235 int reg_code = iterator.Next();
8236 PrintF(out, "{input=%s}", converter.NameOfCPURegister(reg_code));
8237 break;
8238 }
8239
8240 case Translation::DOUBLE_REGISTER: {
8241 int reg_code = iterator.Next();
8242 PrintF(out, "{input=%s}",
8243 DoubleRegister::AllocationIndexToString(reg_code));
8244 break;
8245 }
8246
8247 case Translation::STACK_SLOT: {
8248 int input_slot_index = iterator.Next();
8249 PrintF(out, "{input=%d}", input_slot_index);
8250 break;
8251 }
8252
8253 case Translation::INT32_STACK_SLOT: {
8254 int input_slot_index = iterator.Next();
8255 PrintF(out, "{input=%d}", input_slot_index);
8256 break;
8257 }
8258
8259 case Translation::DOUBLE_STACK_SLOT: {
8260 int input_slot_index = iterator.Next();
8261 PrintF(out, "{input=%d}", input_slot_index);
8262 break;
8263 }
8264
8265 case Translation::LITERAL: {
8266 unsigned literal_index = iterator.Next();
8267 PrintF(out, "{literal_id=%u}", literal_index);
8268 break;
8269 }
8270
8271 case Translation::ARGUMENTS_OBJECT:
8272 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01008273 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008274 PrintF(out, "\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +01008275 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01008276 }
8277}
8278
8279
8280void DeoptimizationOutputData::DeoptimizationOutputDataPrint(FILE* out) {
8281 PrintF(out, "Deoptimization Output Data (deopt points = %d)\n",
8282 this->DeoptPoints());
8283 if (this->DeoptPoints() == 0) return;
8284
8285 PrintF("%6s %8s %s\n", "ast id", "pc", "state");
8286 for (int i = 0; i < this->DeoptPoints(); i++) {
8287 int pc_and_state = this->PcAndState(i)->value();
8288 PrintF("%6d %8d %s\n",
8289 this->AstId(i)->value(),
8290 FullCodeGenerator::PcField::decode(pc_and_state),
8291 FullCodeGenerator::State2String(
8292 FullCodeGenerator::StateField::decode(pc_and_state)));
8293 }
8294}
8295
Ben Murdochb0fe1622011-05-05 13:52:32 +01008296
Steve Blocka7e24c12009-10-30 11:49:00 +00008297// Identify kind of code.
8298const char* Code::Kind2String(Kind kind) {
8299 switch (kind) {
8300 case FUNCTION: return "FUNCTION";
Ben Murdochb0fe1622011-05-05 13:52:32 +01008301 case OPTIMIZED_FUNCTION: return "OPTIMIZED_FUNCTION";
Steve Blocka7e24c12009-10-30 11:49:00 +00008302 case STUB: return "STUB";
8303 case BUILTIN: return "BUILTIN";
8304 case LOAD_IC: return "LOAD_IC";
8305 case KEYED_LOAD_IC: return "KEYED_LOAD_IC";
8306 case STORE_IC: return "STORE_IC";
8307 case KEYED_STORE_IC: return "KEYED_STORE_IC";
8308 case CALL_IC: return "CALL_IC";
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01008309 case KEYED_CALL_IC: return "KEYED_CALL_IC";
Ben Murdoch257744e2011-11-30 15:57:28 +00008310 case UNARY_OP_IC: return "UNARY_OP_IC";
8311 case BINARY_OP_IC: return "BINARY_OP_IC";
Ben Murdochb0fe1622011-05-05 13:52:32 +01008312 case COMPARE_IC: return "COMPARE_IC";
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008313 case TO_BOOLEAN_IC: return "TO_BOOLEAN_IC";
Steve Blocka7e24c12009-10-30 11:49:00 +00008314 }
8315 UNREACHABLE();
8316 return NULL;
8317}
8318
8319
8320const char* Code::ICState2String(InlineCacheState state) {
8321 switch (state) {
8322 case UNINITIALIZED: return "UNINITIALIZED";
8323 case PREMONOMORPHIC: return "PREMONOMORPHIC";
8324 case MONOMORPHIC: return "MONOMORPHIC";
8325 case MONOMORPHIC_PROTOTYPE_FAILURE: return "MONOMORPHIC_PROTOTYPE_FAILURE";
8326 case MEGAMORPHIC: return "MEGAMORPHIC";
8327 case DEBUG_BREAK: return "DEBUG_BREAK";
8328 case DEBUG_PREPARE_STEP_IN: return "DEBUG_PREPARE_STEP_IN";
8329 }
8330 UNREACHABLE();
8331 return NULL;
8332}
8333
8334
8335const char* Code::PropertyType2String(PropertyType type) {
8336 switch (type) {
8337 case NORMAL: return "NORMAL";
8338 case FIELD: return "FIELD";
8339 case CONSTANT_FUNCTION: return "CONSTANT_FUNCTION";
8340 case CALLBACKS: return "CALLBACKS";
Ben Murdoch257744e2011-11-30 15:57:28 +00008341 case HANDLER: return "HANDLER";
Steve Blocka7e24c12009-10-30 11:49:00 +00008342 case INTERCEPTOR: return "INTERCEPTOR";
8343 case MAP_TRANSITION: return "MAP_TRANSITION";
Ben Murdoch589d6972011-11-30 16:04:58 +00008344 case ELEMENTS_TRANSITION: return "ELEMENTS_TRANSITION";
Steve Blocka7e24c12009-10-30 11:49:00 +00008345 case CONSTANT_TRANSITION: return "CONSTANT_TRANSITION";
8346 case NULL_DESCRIPTOR: return "NULL_DESCRIPTOR";
8347 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008348 UNREACHABLE(); // keep the compiler happy
Steve Blocka7e24c12009-10-30 11:49:00 +00008349 return NULL;
8350}
8351
Ben Murdochb0fe1622011-05-05 13:52:32 +01008352
Steve Block1e0659c2011-05-24 12:43:12 +01008353void Code::PrintExtraICState(FILE* out, Kind kind, ExtraICState extra) {
8354 const char* name = NULL;
8355 switch (kind) {
8356 case CALL_IC:
8357 if (extra == STRING_INDEX_OUT_OF_BOUNDS) {
8358 name = "STRING_INDEX_OUT_OF_BOUNDS";
8359 }
8360 break;
8361 case STORE_IC:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01008362 case KEYED_STORE_IC:
8363 if (extra == kStrictMode) {
Steve Block1e0659c2011-05-24 12:43:12 +01008364 name = "STRICT";
8365 }
8366 break;
8367 default:
8368 break;
8369 }
8370 if (name != NULL) {
8371 PrintF(out, "extra_ic_state = %s\n", name);
8372 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008373 PrintF(out, "extra_ic_state = %d\n", extra);
Steve Block1e0659c2011-05-24 12:43:12 +01008374 }
8375}
8376
8377
Ben Murdochb0fe1622011-05-05 13:52:32 +01008378void Code::Disassemble(const char* name, FILE* out) {
8379 PrintF(out, "kind = %s\n", Kind2String(kind()));
Steve Blocka7e24c12009-10-30 11:49:00 +00008380 if (is_inline_cache_stub()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008381 PrintF(out, "ic_state = %s\n", ICState2String(ic_state()));
Steve Block1e0659c2011-05-24 12:43:12 +01008382 PrintExtraICState(out, kind(), extra_ic_state());
Steve Blocka7e24c12009-10-30 11:49:00 +00008383 if (ic_state() == MONOMORPHIC) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008384 PrintF(out, "type = %s\n", PropertyType2String(type()));
Steve Blocka7e24c12009-10-30 11:49:00 +00008385 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008386 if (is_call_stub() || is_keyed_call_stub()) {
8387 PrintF(out, "argc = %d\n", arguments_count());
8388 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008389 }
8390 if ((name != NULL) && (name[0] != '\0')) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01008391 PrintF(out, "name = %s\n", name);
8392 }
8393 if (kind() == OPTIMIZED_FUNCTION) {
8394 PrintF(out, "stack_slots = %d\n", stack_slots());
Steve Blocka7e24c12009-10-30 11:49:00 +00008395 }
8396
Ben Murdochb0fe1622011-05-05 13:52:32 +01008397 PrintF(out, "Instructions (size = %d)\n", instruction_size());
8398 Disassembler::Decode(out, this);
8399 PrintF(out, "\n");
8400
Ben Murdochb0fe1622011-05-05 13:52:32 +01008401 if (kind() == FUNCTION) {
8402 DeoptimizationOutputData* data =
8403 DeoptimizationOutputData::cast(this->deoptimization_data());
8404 data->DeoptimizationOutputDataPrint(out);
8405 } else if (kind() == OPTIMIZED_FUNCTION) {
8406 DeoptimizationInputData* data =
8407 DeoptimizationInputData::cast(this->deoptimization_data());
8408 data->DeoptimizationInputDataPrint(out);
8409 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008410 PrintF("\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +01008411
8412 if (kind() == OPTIMIZED_FUNCTION) {
8413 SafepointTable table(this);
8414 PrintF(out, "Safepoints (size = %u)\n", table.size());
8415 for (unsigned i = 0; i < table.length(); i++) {
8416 unsigned pc_offset = table.GetPcOffset(i);
8417 PrintF(out, "%p %4d ", (instruction_start() + pc_offset), pc_offset);
8418 table.PrintEntry(i);
8419 PrintF(out, " (sp -> fp)");
Ben Murdochb8e0da22011-05-16 14:20:40 +01008420 SafepointEntry entry = table.GetEntry(i);
8421 if (entry.deoptimization_index() != Safepoint::kNoDeoptimizationIndex) {
8422 PrintF(out, " %6d", entry.deoptimization_index());
Ben Murdochb0fe1622011-05-05 13:52:32 +01008423 } else {
8424 PrintF(out, " <none>");
8425 }
Ben Murdochb8e0da22011-05-16 14:20:40 +01008426 if (entry.argument_count() > 0) {
8427 PrintF(out, " argc: %d", entry.argument_count());
8428 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01008429 PrintF(out, "\n");
8430 }
8431 PrintF(out, "\n");
8432 } else if (kind() == FUNCTION) {
Steve Block1e0659c2011-05-24 12:43:12 +01008433 unsigned offset = stack_check_table_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +01008434 // If there is no stack check table, the "table start" will at or after
8435 // (due to alignment) the end of the instruction stream.
8436 if (static_cast<int>(offset) < instruction_size()) {
8437 unsigned* address =
8438 reinterpret_cast<unsigned*>(instruction_start() + offset);
8439 unsigned length = address[0];
8440 PrintF(out, "Stack checks (size = %u)\n", length);
8441 PrintF(out, "ast_id pc_offset\n");
8442 for (unsigned i = 0; i < length; ++i) {
8443 unsigned index = (2 * i) + 1;
8444 PrintF(out, "%6u %9u\n", address[index], address[index + 1]);
8445 }
8446 PrintF(out, "\n");
8447 }
8448 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008449
8450 PrintF("RelocInfo (size = %d)\n", relocation_size());
Ben Murdochb0fe1622011-05-05 13:52:32 +01008451 for (RelocIterator it(this); !it.done(); it.next()) it.rinfo()->Print(out);
8452 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00008453}
8454#endif // ENABLE_DISASSEMBLER
8455
8456
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008457MaybeObject* JSObject::SetFastElementsCapacityAndLength(
8458 int capacity,
8459 int length,
8460 SetFastElementsCapacityMode set_capacity_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01008461 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +00008462 // We should never end in here with a pixel or external array.
Steve Block44f0eee2011-05-26 01:26:41 +01008463 ASSERT(!HasExternalArrayElements());
Steve Block8defd9f2010-07-08 12:39:36 +01008464
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008465 // Allocate a new fast elements backing store.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008466 FixedArray* new_elements;
8467 { MaybeObject* maybe = heap->AllocateFixedArrayWithHoles(capacity);
8468 if (!maybe->To(&new_elements)) return maybe;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008469 }
8470
8471 // Find the new map to use for this object if there is a map change.
8472 Map* new_map = NULL;
8473 if (elements()->map() != heap->non_strict_arguments_elements_map()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008474 // The resized array has FAST_SMI_ONLY_ELEMENTS if the capacity mode forces
8475 // it, or if it's allowed and the old elements array contained only SMIs.
8476 bool has_fast_smi_only_elements =
8477 (set_capacity_mode == kForceSmiOnlyElements) ||
8478 ((set_capacity_mode == kAllowSmiOnlyElements) &&
8479 (elements()->map()->has_fast_smi_only_elements() ||
8480 elements() == heap->empty_fixed_array()));
8481 ElementsKind elements_kind = has_fast_smi_only_elements
8482 ? FAST_SMI_ONLY_ELEMENTS
8483 : FAST_ELEMENTS;
8484 MaybeObject* maybe = GetElementsTransitionMap(GetIsolate(), elements_kind);
8485 if (!maybe->To(&new_map)) return maybe;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008486 }
8487
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008488 FixedArrayBase* old_elements = elements();
8489 ElementsKind elements_kind = GetElementsKind();
8490 ElementsAccessor* accessor = ElementsAccessor::ForKind(elements_kind);
8491 ElementsKind to_kind = (elements_kind == FAST_SMI_ONLY_ELEMENTS)
8492 ? FAST_SMI_ONLY_ELEMENTS
8493 : FAST_ELEMENTS;
8494 // int copy_size = Min(old_elements_raw->length(), new_elements->length());
8495 accessor->CopyElements(this, new_elements, to_kind);
8496 if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) {
8497 set_map_and_elements(new_map, new_elements);
8498 } else {
8499 FixedArray* parameter_map = FixedArray::cast(old_elements);
8500 parameter_map->set(1, new_elements);
8501 }
8502
8503 if (FLAG_trace_elements_transitions) {
8504 PrintElementsTransition(stdout, elements_kind, old_elements,
8505 GetElementsKind(), new_elements);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00008506 }
8507
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008508 // Update the length if necessary.
8509 if (IsJSArray()) {
8510 JSArray::cast(this)->set_length(Smi::FromInt(length));
8511 }
8512
8513 return new_elements;
8514}
8515
8516
8517MaybeObject* JSObject::SetFastDoubleElementsCapacityAndLength(
8518 int capacity,
8519 int length) {
8520 Heap* heap = GetHeap();
8521 // We should never end in here with a pixel or external array.
8522 ASSERT(!HasExternalArrayElements());
8523
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008524 FixedDoubleArray* elems;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008525 { MaybeObject* maybe_obj =
8526 heap->AllocateUninitializedFixedDoubleArray(capacity);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008527 if (!maybe_obj->To(&elems)) return maybe_obj;
John Reck59135872010-11-02 12:39:01 -07008528 }
Steve Block8defd9f2010-07-08 12:39:36 +01008529
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008530 Map* new_map;
8531 { MaybeObject* maybe_obj =
8532 GetElementsTransitionMap(heap->isolate(), FAST_DOUBLE_ELEMENTS);
8533 if (!maybe_obj->To(&new_map)) return maybe_obj;
8534 }
8535
8536 FixedArrayBase* old_elements = elements();
8537 ElementsKind elements_kind = GetElementsKind();
8538 ElementsAccessor* accessor = ElementsAccessor::ForKind(elements_kind);
8539 accessor->CopyElements(this, elems, FAST_DOUBLE_ELEMENTS);
8540 if (elements_kind != NON_STRICT_ARGUMENTS_ELEMENTS) {
8541 set_map_and_elements(new_map, elems);
8542 } else {
8543 FixedArray* parameter_map = FixedArray::cast(old_elements);
8544 parameter_map->set(1, elems);
8545 }
8546
8547 if (FLAG_trace_elements_transitions) {
8548 PrintElementsTransition(stdout, elements_kind, old_elements,
8549 FAST_DOUBLE_ELEMENTS, elems);
8550 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00008551
Steve Block8defd9f2010-07-08 12:39:36 +01008552 if (IsJSArray()) {
8553 JSArray::cast(this)->set_length(Smi::FromInt(length));
8554 }
8555
8556 return this;
Steve Blocka7e24c12009-10-30 11:49:00 +00008557}
8558
8559
John Reck59135872010-11-02 12:39:01 -07008560MaybeObject* JSArray::Initialize(int capacity) {
Steve Block44f0eee2011-05-26 01:26:41 +01008561 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +00008562 ASSERT(capacity >= 0);
Leon Clarke4515c472010-02-03 11:58:03 +00008563 set_length(Smi::FromInt(0));
Steve Blocka7e24c12009-10-30 11:49:00 +00008564 FixedArray* new_elements;
8565 if (capacity == 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01008566 new_elements = heap->empty_fixed_array();
Steve Blocka7e24c12009-10-30 11:49:00 +00008567 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008568 MaybeObject* maybe_obj = heap->AllocateFixedArrayWithHoles(capacity);
8569 if (!maybe_obj->To(&new_elements)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00008570 }
8571 set_elements(new_elements);
8572 return this;
8573}
8574
8575
8576void JSArray::Expand(int required_size) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008577 GetIsolate()->factory()->SetElementsCapacityAndLength(
8578 Handle<JSArray>(this), required_size, required_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00008579}
8580
8581
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008582MaybeObject* JSArray::SetElementsLength(Object* len) {
Steve Block3ce2e202009-11-05 08:53:23 +00008583 // We should never end in here with a pixel or external array.
Steve Block6ded16b2010-05-10 14:33:55 +01008584 ASSERT(AllowsSetElementsLength());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008585 return GetElementsAccessor()->SetLength(this, len);
Steve Blocka7e24c12009-10-30 11:49:00 +00008586}
8587
8588
Steve Block053d10c2011-06-13 19:13:29 +01008589Object* Map::GetPrototypeTransition(Object* prototype) {
8590 FixedArray* cache = prototype_transitions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008591 int number_of_transitions = NumberOfProtoTransitions();
8592 const int proto_offset =
8593 kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset;
8594 const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset;
8595 const int step = kProtoTransitionElementsPerEntry;
8596 for (int i = 0; i < number_of_transitions; i++) {
8597 if (cache->get(proto_offset + i * step) == prototype) {
8598 Object* map = cache->get(map_offset + i * step);
8599 ASSERT(map->IsMap());
8600 return map;
8601 }
Steve Block053d10c2011-06-13 19:13:29 +01008602 }
8603 return NULL;
8604}
8605
8606
8607MaybeObject* Map::PutPrototypeTransition(Object* prototype, Map* map) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008608 ASSERT(map->IsMap());
8609 ASSERT(HeapObject::cast(prototype)->map()->IsMap());
Steve Block053d10c2011-06-13 19:13:29 +01008610 // Don't cache prototype transition if this map is shared.
8611 if (is_shared() || !FLAG_cache_prototype_transitions) return this;
8612
8613 FixedArray* cache = prototype_transitions();
8614
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008615 const int step = kProtoTransitionElementsPerEntry;
8616 const int header = kProtoTransitionHeaderSize;
Steve Block053d10c2011-06-13 19:13:29 +01008617
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008618 int capacity = (cache->length() - header) / step;
Steve Block053d10c2011-06-13 19:13:29 +01008619
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008620 int transitions = NumberOfProtoTransitions() + 1;
8621
8622 if (transitions > capacity) {
Steve Block053d10c2011-06-13 19:13:29 +01008623 if (capacity > kMaxCachedPrototypeTransitions) return this;
8624
8625 FixedArray* new_cache;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008626 // Grow array by factor 2 over and above what we need.
8627 { MaybeObject* maybe_cache =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008628 GetHeap()->AllocateFixedArray(transitions * 2 * step + header);
8629 if (!maybe_cache->To(&new_cache)) return maybe_cache;
Steve Block053d10c2011-06-13 19:13:29 +01008630 }
8631
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008632 for (int i = 0; i < capacity * step; i++) {
8633 new_cache->set(i + header, cache->get(i + header));
8634 }
Steve Block053d10c2011-06-13 19:13:29 +01008635 cache = new_cache;
8636 set_prototype_transitions(cache);
8637 }
8638
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008639 int last = transitions - 1;
8640
8641 cache->set(header + last * step + kProtoTransitionPrototypeOffset, prototype);
8642 cache->set(header + last * step + kProtoTransitionMapOffset, map);
8643 SetNumberOfProtoTransitions(transitions);
Steve Block053d10c2011-06-13 19:13:29 +01008644
8645 return cache;
8646}
8647
8648
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008649MaybeObject* JSReceiver::SetPrototype(Object* value,
8650 bool skip_hidden_prototypes) {
8651#ifdef DEBUG
8652 int size = Size();
8653#endif
8654
Steve Block44f0eee2011-05-26 01:26:41 +01008655 Heap* heap = GetHeap();
Andrei Popescu402d9372010-02-26 13:31:12 +00008656 // Silently ignore the change if value is not a JSObject or null.
8657 // SpiderMonkey behaves this way.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008658 if (!value->IsJSReceiver() && !value->IsNull()) return value;
Andrei Popescu402d9372010-02-26 13:31:12 +00008659
Ben Murdoch8b112d22011-06-08 16:22:53 +01008660 // From 8.6.2 Object Internal Methods
8661 // ...
8662 // In addition, if [[Extensible]] is false the value of the [[Class]] and
8663 // [[Prototype]] internal properties of the object may not be modified.
8664 // ...
8665 // Implementation specific extensions that modify [[Class]], [[Prototype]]
8666 // or [[Extensible]] must not violate the invariants defined in the preceding
8667 // paragraph.
8668 if (!this->map()->is_extensible()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008669 HandleScope scope(heap->isolate());
Ben Murdoch8b112d22011-06-08 16:22:53 +01008670 Handle<Object> handle(this, heap->isolate());
8671 return heap->isolate()->Throw(
8672 *FACTORY->NewTypeError("non_extensible_proto",
8673 HandleVector<Object>(&handle, 1)));
8674 }
8675
Andrei Popescu402d9372010-02-26 13:31:12 +00008676 // Before we can set the prototype we need to be sure
8677 // prototype cycles are prevented.
8678 // It is sufficient to validate that the receiver is not in the new prototype
8679 // chain.
Steve Block44f0eee2011-05-26 01:26:41 +01008680 for (Object* pt = value; pt != heap->null_value(); pt = pt->GetPrototype()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008681 if (JSReceiver::cast(pt) == this) {
Andrei Popescu402d9372010-02-26 13:31:12 +00008682 // Cycle detected.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008683 HandleScope scope(heap->isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01008684 return heap->isolate()->Throw(
8685 *FACTORY->NewError("cyclic_proto", HandleVector<Object>(NULL, 0)));
Andrei Popescu402d9372010-02-26 13:31:12 +00008686 }
8687 }
8688
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008689 JSReceiver* real_receiver = this;
Andrei Popescu402d9372010-02-26 13:31:12 +00008690
8691 if (skip_hidden_prototypes) {
8692 // Find the first object in the chain whose prototype object is not
8693 // hidden and set the new prototype on that object.
8694 Object* current_proto = real_receiver->GetPrototype();
8695 while (current_proto->IsJSObject() &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008696 JSReceiver::cast(current_proto)->map()->is_hidden_prototype()) {
8697 real_receiver = JSReceiver::cast(current_proto);
Andrei Popescu402d9372010-02-26 13:31:12 +00008698 current_proto = current_proto->GetPrototype();
8699 }
8700 }
8701
8702 // Set the new prototype of the object.
Steve Block053d10c2011-06-13 19:13:29 +01008703 Map* map = real_receiver->map();
8704
8705 // Nothing to do if prototype is already set.
8706 if (map->prototype() == value) return value;
8707
8708 Object* new_map = map->GetPrototypeTransition(value);
8709 if (new_map == NULL) {
8710 { MaybeObject* maybe_new_map = map->CopyDropTransitions();
8711 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
8712 }
8713
8714 { MaybeObject* maybe_new_cache =
8715 map->PutPrototypeTransition(value, Map::cast(new_map));
8716 if (maybe_new_cache->IsFailure()) return maybe_new_cache;
8717 }
8718
8719 Map::cast(new_map)->set_prototype(value);
John Reck59135872010-11-02 12:39:01 -07008720 }
Steve Block053d10c2011-06-13 19:13:29 +01008721 ASSERT(Map::cast(new_map)->prototype() == value);
Andrei Popescu402d9372010-02-26 13:31:12 +00008722 real_receiver->set_map(Map::cast(new_map));
8723
Steve Block44f0eee2011-05-26 01:26:41 +01008724 heap->ClearInstanceofCache();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008725 ASSERT(size == Size());
Andrei Popescu402d9372010-02-26 13:31:12 +00008726 return value;
8727}
8728
8729
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008730MaybeObject* JSObject::EnsureCanContainElements(Arguments* args,
8731 uint32_t first_arg,
8732 uint32_t arg_count,
8733 EnsureElementsMode mode) {
8734 // Elements in |Arguments| are ordered backwards (because they're on the
8735 // stack), but the method that's called here iterates over them in forward
8736 // direction.
8737 return EnsureCanContainElements(
8738 args->arguments() - first_arg - (arg_count - 1),
8739 arg_count, mode);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00008740}
8741
8742
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008743bool JSObject::HasElementWithInterceptor(JSReceiver* receiver, uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01008744 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00008745 // Make sure that the top context does not change when doing
8746 // callbacks or interceptor calls.
8747 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01008748 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008749 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008750 Handle<JSReceiver> receiver_handle(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00008751 Handle<JSObject> holder_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01008752 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008753 v8::AccessorInfo info(args.end());
8754 if (!interceptor->query()->IsUndefined()) {
8755 v8::IndexedPropertyQuery query =
8756 v8::ToCData<v8::IndexedPropertyQuery>(interceptor->query());
Steve Block44f0eee2011-05-26 01:26:41 +01008757 LOG(isolate,
8758 ApiIndexedPropertyAccess("interceptor-indexed-has", this, index));
Iain Merrick75681382010-08-19 15:07:18 +01008759 v8::Handle<v8::Integer> result;
Steve Blocka7e24c12009-10-30 11:49:00 +00008760 {
8761 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01008762 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00008763 result = query(index, info);
8764 }
Iain Merrick75681382010-08-19 15:07:18 +01008765 if (!result.IsEmpty()) {
8766 ASSERT(result->IsInt32());
8767 return true; // absence of property is signaled by empty handle.
8768 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008769 } else if (!interceptor->getter()->IsUndefined()) {
8770 v8::IndexedPropertyGetter getter =
8771 v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01008772 LOG(isolate,
8773 ApiIndexedPropertyAccess("interceptor-indexed-has-get", this, index));
Steve Blocka7e24c12009-10-30 11:49:00 +00008774 v8::Handle<v8::Value> result;
8775 {
8776 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01008777 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00008778 result = getter(index, info);
8779 }
8780 if (!result.IsEmpty()) return true;
8781 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008782
8783 if (holder_handle->GetElementsAccessor()->HasElement(
8784 *receiver_handle, *holder_handle, index)) {
8785 return true;
8786 }
8787
8788 if (holder_handle->IsStringObjectWithCharacterAt(index)) return true;
8789 Object* pt = holder_handle->GetPrototype();
8790 if (pt->IsJSProxy()) {
8791 // We need to follow the spec and simulate a call to [[GetOwnProperty]].
8792 return JSProxy::cast(pt)->GetElementAttributeWithHandler(
8793 receiver, index) != ABSENT;
8794 }
8795 if (pt->IsNull()) return false;
8796 return JSObject::cast(pt)->HasElementWithReceiver(*receiver_handle, index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008797}
8798
8799
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008800JSObject::LocalElementType JSObject::HasLocalElement(uint32_t index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008801 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01008802 if (IsAccessCheckNeeded()) {
8803 Heap* heap = GetHeap();
8804 if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) {
8805 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
8806 return UNDEFINED_ELEMENT;
8807 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008808 }
8809
Steve Block1e0659c2011-05-24 12:43:12 +01008810 if (IsJSGlobalProxy()) {
8811 Object* proto = GetPrototype();
8812 if (proto->IsNull()) return UNDEFINED_ELEMENT;
8813 ASSERT(proto->IsJSGlobalObject());
8814 return JSObject::cast(proto)->HasLocalElement(index);
8815 }
8816
Steve Blocka7e24c12009-10-30 11:49:00 +00008817 // Check for lookup interceptor
8818 if (HasIndexedInterceptor()) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008819 return HasElementWithInterceptor(this, index) ? INTERCEPTED_ELEMENT
8820 : UNDEFINED_ELEMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +00008821 }
8822
8823 // Handle [] on String objects.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008824 if (this->IsStringObjectWithCharacterAt(index)) {
8825 return STRING_CHARACTER_ELEMENT;
8826 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008827
8828 switch (GetElementsKind()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008829 case FAST_SMI_ONLY_ELEMENTS:
Steve Blocka7e24c12009-10-30 11:49:00 +00008830 case FAST_ELEMENTS: {
8831 uint32_t length = IsJSArray() ?
8832 static_cast<uint32_t>
8833 (Smi::cast(JSArray::cast(this)->length())->value()) :
8834 static_cast<uint32_t>(FixedArray::cast(elements())->length());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008835 if ((index < length) &&
8836 !FixedArray::cast(elements())->get(index)->IsTheHole()) {
8837 return FAST_ELEMENT;
8838 }
8839 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00008840 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008841 case FAST_DOUBLE_ELEMENTS: {
8842 uint32_t length = IsJSArray() ?
8843 static_cast<uint32_t>
8844 (Smi::cast(JSArray::cast(this)->length())->value()) :
8845 static_cast<uint32_t>(FixedDoubleArray::cast(elements())->length());
8846 if ((index < length) &&
8847 !FixedDoubleArray::cast(elements())->is_the_hole(index)) {
8848 return FAST_ELEMENT;
8849 }
8850 break;
8851 }
Steve Block44f0eee2011-05-26 01:26:41 +01008852 case EXTERNAL_PIXEL_ELEMENTS: {
8853 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008854 if (index < static_cast<uint32_t>(pixels->length())) return FAST_ELEMENT;
8855 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00008856 }
Steve Block3ce2e202009-11-05 08:53:23 +00008857 case EXTERNAL_BYTE_ELEMENTS:
8858 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
8859 case EXTERNAL_SHORT_ELEMENTS:
8860 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
8861 case EXTERNAL_INT_ELEMENTS:
8862 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00008863 case EXTERNAL_FLOAT_ELEMENTS:
8864 case EXTERNAL_DOUBLE_ELEMENTS: {
Steve Block3ce2e202009-11-05 08:53:23 +00008865 ExternalArray* array = ExternalArray::cast(elements());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008866 if (index < static_cast<uint32_t>(array->length())) return FAST_ELEMENT;
8867 break;
Steve Block3ce2e202009-11-05 08:53:23 +00008868 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008869 case DICTIONARY_ELEMENTS: {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008870 if (element_dictionary()->FindEntry(index) !=
Ben Murdochc7cc0282012-03-05 14:35:55 +00008871 SeededNumberDictionary::kNotFound) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008872 return DICTIONARY_ELEMENT;
8873 }
8874 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00008875 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008876 case NON_STRICT_ARGUMENTS_ELEMENTS: {
8877 // Aliased parameters and non-aliased elements in a fast backing store
8878 // behave as FAST_ELEMENT. Non-aliased elements in a dictionary
8879 // backing store behave as DICTIONARY_ELEMENT.
8880 FixedArray* parameter_map = FixedArray::cast(elements());
8881 uint32_t length = parameter_map->length();
8882 Object* probe =
8883 index < (length - 2) ? parameter_map->get(index + 2) : NULL;
8884 if (probe != NULL && !probe->IsTheHole()) return FAST_ELEMENT;
8885 // If not aliased, check the arguments.
8886 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
8887 if (arguments->IsDictionary()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00008888 SeededNumberDictionary* dictionary =
8889 SeededNumberDictionary::cast(arguments);
8890 if (dictionary->FindEntry(index) != SeededNumberDictionary::kNotFound) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008891 return DICTIONARY_ELEMENT;
8892 }
8893 } else {
8894 length = arguments->length();
8895 probe = (index < length) ? arguments->get(index) : NULL;
8896 if (probe != NULL && !probe->IsTheHole()) return FAST_ELEMENT;
8897 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008898 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008899 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008900 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01008901
8902 return UNDEFINED_ELEMENT;
Steve Blocka7e24c12009-10-30 11:49:00 +00008903}
8904
8905
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008906bool JSObject::HasElementWithReceiver(JSReceiver* receiver, uint32_t index) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008907 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01008908 if (IsAccessCheckNeeded()) {
8909 Heap* heap = GetHeap();
8910 if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) {
8911 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
8912 return false;
8913 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008914 }
8915
8916 // Check for lookup interceptor
8917 if (HasIndexedInterceptor()) {
8918 return HasElementWithInterceptor(receiver, index);
8919 }
8920
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008921 ElementsAccessor* accessor = GetElementsAccessor();
8922 if (accessor->HasElement(receiver, this, index)) {
8923 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00008924 }
8925
8926 // Handle [] on String objects.
8927 if (this->IsStringObjectWithCharacterAt(index)) return true;
8928
8929 Object* pt = GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01008930 if (pt->IsNull()) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008931 if (pt->IsJSProxy()) {
8932 // We need to follow the spec and simulate a call to [[GetOwnProperty]].
8933 return JSProxy::cast(pt)->GetElementAttributeWithHandler(
8934 receiver, index) != ABSENT;
8935 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008936 return JSObject::cast(pt)->HasElementWithReceiver(receiver, index);
8937}
8938
8939
John Reck59135872010-11-02 12:39:01 -07008940MaybeObject* JSObject::SetElementWithInterceptor(uint32_t index,
Steve Block9fac8402011-05-12 15:51:54 +01008941 Object* value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008942 PropertyAttributes attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01008943 StrictModeFlag strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008944 bool check_prototype,
8945 SetPropertyMode set_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01008946 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00008947 // Make sure that the top context does not change when doing
8948 // callbacks or interceptor calls.
8949 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01008950 HandleScope scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008951 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor());
8952 Handle<JSObject> this_handle(this);
Steve Block44f0eee2011-05-26 01:26:41 +01008953 Handle<Object> value_handle(value, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008954 if (!interceptor->setter()->IsUndefined()) {
8955 v8::IndexedPropertySetter setter =
8956 v8::ToCData<v8::IndexedPropertySetter>(interceptor->setter());
Steve Block44f0eee2011-05-26 01:26:41 +01008957 LOG(isolate,
8958 ApiIndexedPropertyAccess("interceptor-indexed-set", this, index));
8959 CustomArguments args(isolate, interceptor->data(), this, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008960 v8::AccessorInfo info(args.end());
8961 v8::Handle<v8::Value> result;
8962 {
8963 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01008964 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00008965 result = setter(index, v8::Utils::ToLocal(value_handle), info);
8966 }
Steve Block44f0eee2011-05-26 01:26:41 +01008967 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008968 if (!result.IsEmpty()) return *value_handle;
8969 }
John Reck59135872010-11-02 12:39:01 -07008970 MaybeObject* raw_result =
Steve Block9fac8402011-05-12 15:51:54 +01008971 this_handle->SetElementWithoutInterceptor(index,
8972 *value_handle,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008973 attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01008974 strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008975 check_prototype,
8976 set_mode);
Steve Block44f0eee2011-05-26 01:26:41 +01008977 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00008978 return raw_result;
8979}
8980
8981
John Reck59135872010-11-02 12:39:01 -07008982MaybeObject* JSObject::GetElementWithCallback(Object* receiver,
8983 Object* structure,
8984 uint32_t index,
8985 Object* holder) {
Steve Block44f0eee2011-05-26 01:26:41 +01008986 Isolate* isolate = GetIsolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00008987 ASSERT(!structure->IsForeign());
Leon Clarkef7060e22010-06-03 12:02:55 +01008988
8989 // api style callbacks.
8990 if (structure->IsAccessorInfo()) {
Ben Murdoch257744e2011-11-30 15:57:28 +00008991 Handle<AccessorInfo> data(AccessorInfo::cast(structure));
Leon Clarkef7060e22010-06-03 12:02:55 +01008992 Object* fun_obj = data->getter();
8993 v8::AccessorGetter call_fun = v8::ToCData<v8::AccessorGetter>(fun_obj);
Steve Block44f0eee2011-05-26 01:26:41 +01008994 HandleScope scope(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01008995 Handle<JSObject> self(JSObject::cast(receiver));
8996 Handle<JSObject> holder_handle(JSObject::cast(holder));
Steve Block44f0eee2011-05-26 01:26:41 +01008997 Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008998 Handle<String> key = isolate->factory()->NumberToString(number);
Steve Block44f0eee2011-05-26 01:26:41 +01008999 LOG(isolate, ApiNamedPropertyAccess("load", *self, *key));
9000 CustomArguments args(isolate, data->data(), *self, *holder_handle);
Leon Clarkef7060e22010-06-03 12:02:55 +01009001 v8::AccessorInfo info(args.end());
9002 v8::Handle<v8::Value> result;
9003 {
9004 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01009005 VMState state(isolate, EXTERNAL);
Leon Clarkef7060e22010-06-03 12:02:55 +01009006 result = call_fun(v8::Utils::ToLocal(key), info);
9007 }
Steve Block44f0eee2011-05-26 01:26:41 +01009008 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
9009 if (result.IsEmpty()) return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01009010 return *v8::Utils::OpenHandle(*result);
9011 }
9012
9013 // __defineGetter__ callback
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009014 if (structure->IsAccessorPair()) {
9015 Object* getter = AccessorPair::cast(structure)->getter();
9016 if (getter->IsSpecFunction()) {
9017 // TODO(rossberg): nicer would be to cast to some JSCallable here...
9018 return GetPropertyWithDefinedGetter(receiver, JSReceiver::cast(getter));
Leon Clarkef7060e22010-06-03 12:02:55 +01009019 }
9020 // Getter is not a function.
Steve Block44f0eee2011-05-26 01:26:41 +01009021 return isolate->heap()->undefined_value();
Leon Clarkef7060e22010-06-03 12:02:55 +01009022 }
9023
9024 UNREACHABLE();
9025 return NULL;
9026}
9027
9028
John Reck59135872010-11-02 12:39:01 -07009029MaybeObject* JSObject::SetElementWithCallback(Object* structure,
9030 uint32_t index,
9031 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009032 JSObject* holder,
9033 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +01009034 Isolate* isolate = GetIsolate();
9035 HandleScope scope(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01009036
9037 // We should never get here to initialize a const with the hole
9038 // value since a const declaration would conflict with the setter.
9039 ASSERT(!value->IsTheHole());
Steve Block44f0eee2011-05-26 01:26:41 +01009040 Handle<Object> value_handle(value, isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01009041
9042 // To accommodate both the old and the new api we switch on the
Ben Murdoch257744e2011-11-30 15:57:28 +00009043 // data structure used to store the callbacks. Eventually foreign
Leon Clarkef7060e22010-06-03 12:02:55 +01009044 // callbacks should be phased out.
Ben Murdoch257744e2011-11-30 15:57:28 +00009045 ASSERT(!structure->IsForeign());
Leon Clarkef7060e22010-06-03 12:02:55 +01009046
9047 if (structure->IsAccessorInfo()) {
9048 // api style callbacks
Ben Murdoch257744e2011-11-30 15:57:28 +00009049 Handle<JSObject> self(this);
9050 Handle<JSObject> holder_handle(JSObject::cast(holder));
9051 Handle<AccessorInfo> data(AccessorInfo::cast(structure));
Leon Clarkef7060e22010-06-03 12:02:55 +01009052 Object* call_obj = data->setter();
9053 v8::AccessorSetter call_fun = v8::ToCData<v8::AccessorSetter>(call_obj);
9054 if (call_fun == NULL) return value;
Steve Block44f0eee2011-05-26 01:26:41 +01009055 Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
9056 Handle<String> key(isolate->factory()->NumberToString(number));
Ben Murdoch257744e2011-11-30 15:57:28 +00009057 LOG(isolate, ApiNamedPropertyAccess("store", *self, *key));
9058 CustomArguments args(isolate, data->data(), *self, *holder_handle);
Leon Clarkef7060e22010-06-03 12:02:55 +01009059 v8::AccessorInfo info(args.end());
9060 {
9061 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01009062 VMState state(isolate, EXTERNAL);
Leon Clarkef7060e22010-06-03 12:02:55 +01009063 call_fun(v8::Utils::ToLocal(key),
9064 v8::Utils::ToLocal(value_handle),
9065 info);
9066 }
Steve Block44f0eee2011-05-26 01:26:41 +01009067 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Leon Clarkef7060e22010-06-03 12:02:55 +01009068 return *value_handle;
9069 }
9070
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009071 if (structure->IsAccessorPair()) {
9072 Handle<Object> setter(AccessorPair::cast(structure)->setter());
9073 if (setter->IsSpecFunction()) {
9074 // TODO(rossberg): nicer would be to cast to some JSCallable here...
9075 return SetPropertyWithDefinedSetter(JSReceiver::cast(*setter), value);
Leon Clarkef7060e22010-06-03 12:02:55 +01009076 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009077 if (strict_mode == kNonStrictMode) {
9078 return value;
9079 }
Steve Block44f0eee2011-05-26 01:26:41 +01009080 Handle<Object> holder_handle(holder, isolate);
9081 Handle<Object> key(isolate->factory()->NewNumberFromUint(index));
Leon Clarkef7060e22010-06-03 12:02:55 +01009082 Handle<Object> args[2] = { key, holder_handle };
Steve Block44f0eee2011-05-26 01:26:41 +01009083 return isolate->Throw(
9084 *isolate->factory()->NewTypeError("no_setter_in_callback",
9085 HandleVector(args, 2)));
Leon Clarkef7060e22010-06-03 12:02:55 +01009086 }
9087 }
9088
9089 UNREACHABLE();
9090 return NULL;
9091}
9092
9093
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009094bool JSObject::HasFastArgumentsElements() {
9095 Heap* heap = GetHeap();
9096 if (!elements()->IsFixedArray()) return false;
9097 FixedArray* elements = FixedArray::cast(this->elements());
9098 if (elements->map() != heap->non_strict_arguments_elements_map()) {
9099 return false;
9100 }
9101 FixedArray* arguments = FixedArray::cast(elements->get(1));
9102 return !arguments->IsDictionary();
9103}
9104
9105
9106bool JSObject::HasDictionaryArgumentsElements() {
9107 Heap* heap = GetHeap();
9108 if (!elements()->IsFixedArray()) return false;
9109 FixedArray* elements = FixedArray::cast(this->elements());
9110 if (elements->map() != heap->non_strict_arguments_elements_map()) {
9111 return false;
9112 }
9113 FixedArray* arguments = FixedArray::cast(elements->get(1));
9114 return arguments->IsDictionary();
9115}
9116
9117
Steve Blocka7e24c12009-10-30 11:49:00 +00009118// Adding n elements in fast case is O(n*n).
9119// Note: revisit design to have dual undefined values to capture absent
9120// elements.
Steve Block9fac8402011-05-12 15:51:54 +01009121MaybeObject* JSObject::SetFastElement(uint32_t index,
9122 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009123 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01009124 bool check_prototype) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009125 ASSERT(HasFastTypeElements() ||
9126 HasFastArgumentsElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00009127
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009128 FixedArray* backing_store = FixedArray::cast(elements());
9129 if (backing_store->map() == GetHeap()->non_strict_arguments_elements_map()) {
9130 backing_store = FixedArray::cast(backing_store->get(1));
9131 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009132 MaybeObject* maybe = EnsureWritableFastElements();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009133 if (!maybe->To(&backing_store)) return maybe;
John Reck59135872010-11-02 12:39:01 -07009134 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009135 uint32_t capacity = static_cast<uint32_t>(backing_store->length());
Steve Blocka7e24c12009-10-30 11:49:00 +00009136
Steve Block9fac8402011-05-12 15:51:54 +01009137 if (check_prototype &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009138 (index >= capacity || backing_store->get(index)->IsTheHole())) {
Steve Block1e0659c2011-05-24 12:43:12 +01009139 bool found;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009140 MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index,
9141 value,
9142 &found,
9143 strict_mode);
Steve Block1e0659c2011-05-24 12:43:12 +01009144 if (found) return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00009145 }
9146
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009147 uint32_t new_capacity = capacity;
9148 // Check if the length property of this object needs to be updated.
9149 uint32_t array_length = 0;
9150 bool must_update_array_length = false;
9151 if (IsJSArray()) {
9152 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length));
9153 if (index >= array_length) {
9154 must_update_array_length = true;
9155 array_length = index + 1;
9156 }
9157 }
9158 // Check if the capacity of the backing store needs to be increased, or if
9159 // a transition to slow elements is necessary.
9160 if (index >= capacity) {
9161 bool convert_to_slow = true;
9162 if ((index - capacity) < kMaxGap) {
9163 new_capacity = NewElementsCapacity(index + 1);
9164 ASSERT(new_capacity > index);
9165 if (!ShouldConvertToSlowElements(new_capacity)) {
9166 convert_to_slow = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009167 }
9168 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009169 if (convert_to_slow) {
9170 MaybeObject* result = NormalizeElements();
9171 if (result->IsFailure()) return result;
9172 return SetDictionaryElement(index, value, NONE, strict_mode,
9173 check_prototype);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00009174 }
9175 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009176 // Convert to fast double elements if appropriate.
9177 if (HasFastSmiOnlyElements() && !value->IsSmi() && value->IsNumber()) {
9178 MaybeObject* maybe =
9179 SetFastDoubleElementsCapacityAndLength(new_capacity, array_length);
9180 if (maybe->IsFailure()) return maybe;
9181 FixedDoubleArray::cast(elements())->set(index, value->Number());
9182 return value;
9183 }
9184 // Change elements kind from SMI_ONLY to generic FAST if necessary.
9185 if (HasFastSmiOnlyElements() && !value->IsSmi()) {
9186 Map* new_map;
9187 { MaybeObject* maybe_new_map = GetElementsTransitionMap(GetIsolate(),
9188 FAST_ELEMENTS);
9189 if (!maybe_new_map->To(&new_map)) return maybe_new_map;
9190 }
9191 set_map(new_map);
9192 if (FLAG_trace_elements_transitions) {
9193 PrintElementsTransition(stdout, FAST_SMI_ONLY_ELEMENTS, elements(),
9194 FAST_ELEMENTS, elements());
9195 }
9196 }
9197 // Increase backing store capacity if that's been decided previously.
9198 if (new_capacity != capacity) {
9199 FixedArray* new_elements;
9200 SetFastElementsCapacityMode set_capacity_mode =
9201 value->IsSmi() && HasFastSmiOnlyElements()
9202 ? kAllowSmiOnlyElements
9203 : kDontAllowSmiOnlyElements;
9204 { MaybeObject* maybe =
9205 SetFastElementsCapacityAndLength(new_capacity,
9206 array_length,
9207 set_capacity_mode);
9208 if (!maybe->To(&new_elements)) return maybe;
9209 }
9210 new_elements->set(index, value);
9211 return value;
9212 }
9213 // Finally, set the new element and length.
9214 ASSERT(elements()->IsFixedArray());
9215 backing_store->set(index, value);
9216 if (must_update_array_length) {
9217 JSArray::cast(this)->set_length(Smi::FromInt(array_length));
9218 }
9219 return value;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009220}
9221
9222
9223MaybeObject* JSObject::SetDictionaryElement(uint32_t index,
9224 Object* value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009225 PropertyAttributes attributes,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009226 StrictModeFlag strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009227 bool check_prototype,
9228 SetPropertyMode set_mode) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009229 ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements());
9230 Isolate* isolate = GetIsolate();
9231 Heap* heap = isolate->heap();
9232
9233 // Insert element in the dictionary.
9234 FixedArray* elements = FixedArray::cast(this->elements());
9235 bool is_arguments =
9236 (elements->map() == heap->non_strict_arguments_elements_map());
Ben Murdochc7cc0282012-03-05 14:35:55 +00009237 SeededNumberDictionary* dictionary = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009238 if (is_arguments) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009239 dictionary = SeededNumberDictionary::cast(elements->get(1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009240 } else {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009241 dictionary = SeededNumberDictionary::cast(elements);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009242 }
9243
9244 int entry = dictionary->FindEntry(index);
Ben Murdochc7cc0282012-03-05 14:35:55 +00009245 if (entry != SeededNumberDictionary::kNotFound) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009246 Object* element = dictionary->ValueAt(entry);
9247 PropertyDetails details = dictionary->DetailsAt(entry);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009248 if (details.type() == CALLBACKS && set_mode == SET_PROPERTY) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009249 return SetElementWithCallback(element, index, value, this, strict_mode);
9250 } else {
9251 dictionary->UpdateMaxNumberKey(index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009252 // If a value has not been initialized we allow writing to it even if it
9253 // is read-only (a declared const that has not been initialized). If a
9254 // value is being defined we skip attribute checks completely.
9255 if (set_mode == DEFINE_PROPERTY) {
9256 details = PropertyDetails(attributes, NORMAL, details.index());
9257 dictionary->DetailsAtPut(entry, details);
9258 } else if (details.IsReadOnly() && !element->IsTheHole()) {
9259 if (strict_mode == kNonStrictMode) {
9260 return isolate->heap()->undefined_value();
9261 } else {
9262 Handle<Object> holder(this);
9263 Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
9264 Handle<Object> args[2] = { number, holder };
9265 Handle<Object> error =
9266 isolate->factory()->NewTypeError("strict_read_only_property",
9267 HandleVector(args, 2));
9268 return isolate->Throw(*error);
9269 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009270 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009271 // Elements of the arguments object in slow mode might be slow aliases.
9272 if (is_arguments && element->IsAliasedArgumentsEntry()) {
9273 AliasedArgumentsEntry* entry = AliasedArgumentsEntry::cast(element);
9274 Context* context = Context::cast(elements->get(0));
9275 int context_index = entry->aliased_context_slot();
9276 ASSERT(!context->get(context_index)->IsTheHole());
9277 context->set(context_index, value);
9278 // For elements that are still writable we keep slow aliasing.
9279 if (!details.IsReadOnly()) value = element;
9280 }
9281 dictionary->ValueAtPut(entry, value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009282 }
9283 } else {
9284 // Index not already used. Look for an accessor in the prototype chain.
9285 if (check_prototype) {
9286 bool found;
9287 MaybeObject* result =
9288 SetElementWithCallbackSetterInPrototypes(
9289 index, value, &found, strict_mode);
9290 if (found) return result;
9291 }
9292 // When we set the is_extensible flag to false we always force the
9293 // element into dictionary mode (and force them to stay there).
9294 if (!map()->is_extensible()) {
9295 if (strict_mode == kNonStrictMode) {
9296 return isolate->heap()->undefined_value();
9297 } else {
9298 Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
9299 Handle<String> name = isolate->factory()->NumberToString(number);
9300 Handle<Object> args[1] = { name };
9301 Handle<Object> error =
9302 isolate->factory()->NewTypeError("object_not_extensible",
9303 HandleVector(args, 1));
9304 return isolate->Throw(*error);
9305 }
9306 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009307 FixedArrayBase* new_dictionary;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009308 PropertyDetails details = PropertyDetails(attributes, NORMAL);
9309 MaybeObject* maybe = dictionary->AddNumberEntry(index, value, details);
9310 if (!maybe->To(&new_dictionary)) return maybe;
Ben Murdochc7cc0282012-03-05 14:35:55 +00009311 if (dictionary != SeededNumberDictionary::cast(new_dictionary)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009312 if (is_arguments) {
9313 elements->set(1, new_dictionary);
9314 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009315 set_elements(new_dictionary);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009316 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00009317 dictionary = SeededNumberDictionary::cast(new_dictionary);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009318 }
9319 }
9320
9321 // Update the array length if this JSObject is an array.
9322 if (IsJSArray()) {
9323 MaybeObject* result =
9324 JSArray::cast(this)->JSArrayUpdateLengthFromIndex(index, value);
9325 if (result->IsFailure()) return result;
9326 }
9327
9328 // Attempt to put this object back in fast case.
9329 if (ShouldConvertToFastElements()) {
9330 uint32_t new_length = 0;
9331 if (IsJSArray()) {
9332 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&new_length));
9333 } else {
9334 new_length = dictionary->max_number_key() + 1;
9335 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009336 SetFastElementsCapacityMode set_capacity_mode = FLAG_smi_only_arrays
9337 ? kAllowSmiOnlyElements
9338 : kDontAllowSmiOnlyElements;
9339 bool has_smi_only_elements = false;
9340 bool should_convert_to_fast_double_elements =
9341 ShouldConvertToFastDoubleElements(&has_smi_only_elements);
9342 if (has_smi_only_elements) {
9343 set_capacity_mode = kForceSmiOnlyElements;
9344 }
9345 MaybeObject* result = should_convert_to_fast_double_elements
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009346 ? SetFastDoubleElementsCapacityAndLength(new_length, new_length)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009347 : SetFastElementsCapacityAndLength(new_length,
9348 new_length,
9349 set_capacity_mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009350 if (result->IsFailure()) return result;
9351#ifdef DEBUG
9352 if (FLAG_trace_normalization) {
9353 PrintF("Object elements are fast case again:\n");
9354 Print();
9355 }
9356#endif
9357 }
9358 return value;
9359}
9360
9361
9362MUST_USE_RESULT MaybeObject* JSObject::SetFastDoubleElement(
9363 uint32_t index,
9364 Object* value,
9365 StrictModeFlag strict_mode,
9366 bool check_prototype) {
9367 ASSERT(HasFastDoubleElements());
9368
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009369 FixedArrayBase* base_elms = FixedArrayBase::cast(elements());
9370 uint32_t elms_length = static_cast<uint32_t>(base_elms->length());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009371
9372 // If storing to an element that isn't in the array, pass the store request
9373 // up the prototype chain before storing in the receiver's elements.
9374 if (check_prototype &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009375 (index >= elms_length ||
9376 FixedDoubleArray::cast(base_elms)->is_the_hole(index))) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009377 bool found;
9378 MaybeObject* result = SetElementWithCallbackSetterInPrototypes(index,
9379 value,
9380 &found,
9381 strict_mode);
9382 if (found) return result;
9383 }
9384
9385 // If the value object is not a heap number, switch to fast elements and try
9386 // again.
9387 bool value_is_smi = value->IsSmi();
9388 if (!value->IsNumber()) {
9389 Object* obj;
9390 uint32_t length = elms_length;
9391 if (IsJSArray()) {
9392 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length));
9393 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009394 MaybeObject* maybe_obj = SetFastElementsCapacityAndLength(
9395 elms_length,
9396 length,
9397 kDontAllowSmiOnlyElements);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009398 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009399 return SetFastElement(index,
9400 value,
9401 strict_mode,
9402 check_prototype);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009403 }
9404
9405 double double_value = value_is_smi
9406 ? static_cast<double>(Smi::cast(value)->value())
9407 : HeapNumber::cast(value)->value();
9408
9409 // Check whether there is extra space in the fixed array.
Steve Blocka7e24c12009-10-30 11:49:00 +00009410 if (index < elms_length) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009411 FixedDoubleArray* elms = FixedDoubleArray::cast(elements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009412 elms->set(index, double_value);
Steve Blocka7e24c12009-10-30 11:49:00 +00009413 if (IsJSArray()) {
9414 // Update the length of the array if needed.
9415 uint32_t array_length = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01009416 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_length));
Steve Blocka7e24c12009-10-30 11:49:00 +00009417 if (index >= array_length) {
Leon Clarke4515c472010-02-03 11:58:03 +00009418 JSArray::cast(this)->set_length(Smi::FromInt(index + 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00009419 }
9420 }
9421 return value;
9422 }
9423
9424 // Allow gap in fast case.
9425 if ((index - elms_length) < kMaxGap) {
9426 // Try allocating extra space.
9427 int new_capacity = NewElementsCapacity(index+1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009428 if (!ShouldConvertToSlowElements(new_capacity)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009429 ASSERT(static_cast<uint32_t>(new_capacity) > index);
John Reck59135872010-11-02 12:39:01 -07009430 Object* obj;
9431 { MaybeObject* maybe_obj =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009432 SetFastDoubleElementsCapacityAndLength(new_capacity,
9433 index + 1);
John Reck59135872010-11-02 12:39:01 -07009434 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9435 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009436 FixedDoubleArray::cast(elements())->set(index, double_value);
Steve Blocka7e24c12009-10-30 11:49:00 +00009437 return value;
9438 }
9439 }
9440
9441 // Otherwise default to slow case.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009442 ASSERT(HasFastDoubleElements());
9443 ASSERT(map()->has_fast_double_elements());
9444 ASSERT(elements()->IsFixedDoubleArray());
John Reck59135872010-11-02 12:39:01 -07009445 Object* obj;
9446 { MaybeObject* maybe_obj = NormalizeElements();
9447 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
9448 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009449 ASSERT(HasDictionaryElements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009450 return SetElement(index, value, NONE, strict_mode, check_prototype);
9451}
9452
9453
9454MaybeObject* JSReceiver::SetElement(uint32_t index,
9455 Object* value,
9456 PropertyAttributes attributes,
9457 StrictModeFlag strict_mode,
9458 bool check_proto) {
9459 if (IsJSProxy()) {
9460 return JSProxy::cast(this)->SetElementWithHandler(
9461 index, value, strict_mode);
9462 } else {
9463 return JSObject::cast(this)->SetElement(
9464 index, value, attributes, strict_mode, check_proto);
9465 }
9466}
9467
9468
9469Handle<Object> JSObject::SetOwnElement(Handle<JSObject> object,
9470 uint32_t index,
9471 Handle<Object> value,
9472 StrictModeFlag strict_mode) {
9473 ASSERT(!object->HasExternalArrayElements());
9474 CALL_HEAP_FUNCTION(
9475 object->GetIsolate(),
9476 object->SetElement(index, *value, NONE, strict_mode, false),
9477 Object);
9478}
9479
9480
9481Handle<Object> JSObject::SetElement(Handle<JSObject> object,
9482 uint32_t index,
9483 Handle<Object> value,
9484 PropertyAttributes attr,
9485 StrictModeFlag strict_mode,
9486 SetPropertyMode set_mode) {
9487 if (object->HasExternalArrayElements()) {
9488 if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) {
9489 bool has_exception;
9490 Handle<Object> number = Execution::ToNumber(value, &has_exception);
9491 if (has_exception) return Handle<Object>();
9492 value = number;
9493 }
9494 }
9495 CALL_HEAP_FUNCTION(
9496 object->GetIsolate(),
9497 object->SetElement(index, *value, attr, strict_mode, true, set_mode),
9498 Object);
Ben Murdochc7cc0282012-03-05 14:35:55 +00009499}
9500
9501
Steve Block9fac8402011-05-12 15:51:54 +01009502MaybeObject* JSObject::SetElement(uint32_t index,
9503 Object* value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009504 PropertyAttributes attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009505 StrictModeFlag strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009506 bool check_prototype,
9507 SetPropertyMode set_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009508 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +01009509 if (IsAccessCheckNeeded()) {
9510 Heap* heap = GetHeap();
9511 if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_SET)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009512 HandleScope scope(heap->isolate());
Ben Murdoch8b112d22011-06-08 16:22:53 +01009513 Handle<Object> value_handle(value);
9514 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_SET);
9515 return *value_handle;
9516 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009517 }
9518
9519 if (IsJSGlobalProxy()) {
9520 Object* proto = GetPrototype();
9521 if (proto->IsNull()) return value;
9522 ASSERT(proto->IsJSGlobalObject());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009523 return JSObject::cast(proto)->SetElement(index,
9524 value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009525 attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009526 strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009527 check_prototype,
9528 set_mode);
9529 }
9530
9531 // Don't allow element properties to be redefined for external arrays.
9532 if (HasExternalArrayElements() && set_mode == DEFINE_PROPERTY) {
9533 Isolate* isolate = GetHeap()->isolate();
9534 Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
9535 Handle<Object> args[] = { Handle<Object>(this), number };
9536 Handle<Object> error = isolate->factory()->NewTypeError(
9537 "redef_external_array_element", HandleVector(args, ARRAY_SIZE(args)));
9538 return isolate->Throw(*error);
9539 }
9540
9541 // Normalize the elements to enable attributes on the property.
9542 if ((attributes & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) {
9543 SeededNumberDictionary* dictionary;
9544 MaybeObject* maybe_object = NormalizeElements();
9545 if (!maybe_object->To(&dictionary)) return maybe_object;
9546 // Make sure that we never go back to fast case.
9547 dictionary->set_requires_slow_elements();
Steve Blocka7e24c12009-10-30 11:49:00 +00009548 }
9549
9550 // Check for lookup interceptor
9551 if (HasIndexedInterceptor()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009552 return SetElementWithInterceptor(index,
9553 value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009554 attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009555 strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009556 check_prototype,
9557 set_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00009558 }
9559
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009560 return SetElementWithoutInterceptor(index,
9561 value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009562 attributes,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009563 strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009564 check_prototype,
9565 set_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00009566}
9567
9568
John Reck59135872010-11-02 12:39:01 -07009569MaybeObject* JSObject::SetElementWithoutInterceptor(uint32_t index,
Steve Block9fac8402011-05-12 15:51:54 +01009570 Object* value,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009571 PropertyAttributes attr,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009572 StrictModeFlag strict_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009573 bool check_prototype,
9574 SetPropertyMode set_mode) {
9575 ASSERT(HasDictionaryElements() ||
9576 HasDictionaryArgumentsElements() ||
9577 (attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) == 0);
Steve Block44f0eee2011-05-26 01:26:41 +01009578 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00009579 switch (GetElementsKind()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009580 case FAST_SMI_ONLY_ELEMENTS:
Steve Blocka7e24c12009-10-30 11:49:00 +00009581 case FAST_ELEMENTS:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009582 return SetFastElement(index, value, strict_mode, check_prototype);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009583 case FAST_DOUBLE_ELEMENTS:
9584 return SetFastDoubleElement(index, value, strict_mode, check_prototype);
Steve Block44f0eee2011-05-26 01:26:41 +01009585 case EXTERNAL_PIXEL_ELEMENTS: {
9586 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00009587 return pixels->SetValue(index, value);
9588 }
Steve Block3ce2e202009-11-05 08:53:23 +00009589 case EXTERNAL_BYTE_ELEMENTS: {
9590 ExternalByteArray* array = ExternalByteArray::cast(elements());
9591 return array->SetValue(index, value);
9592 }
9593 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: {
9594 ExternalUnsignedByteArray* array =
9595 ExternalUnsignedByteArray::cast(elements());
9596 return array->SetValue(index, value);
9597 }
9598 case EXTERNAL_SHORT_ELEMENTS: {
9599 ExternalShortArray* array = ExternalShortArray::cast(elements());
9600 return array->SetValue(index, value);
9601 }
9602 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: {
9603 ExternalUnsignedShortArray* array =
9604 ExternalUnsignedShortArray::cast(elements());
9605 return array->SetValue(index, value);
9606 }
9607 case EXTERNAL_INT_ELEMENTS: {
9608 ExternalIntArray* array = ExternalIntArray::cast(elements());
9609 return array->SetValue(index, value);
9610 }
9611 case EXTERNAL_UNSIGNED_INT_ELEMENTS: {
9612 ExternalUnsignedIntArray* array =
9613 ExternalUnsignedIntArray::cast(elements());
9614 return array->SetValue(index, value);
9615 }
9616 case EXTERNAL_FLOAT_ELEMENTS: {
9617 ExternalFloatArray* array = ExternalFloatArray::cast(elements());
9618 return array->SetValue(index, value);
9619 }
Ben Murdoch257744e2011-11-30 15:57:28 +00009620 case EXTERNAL_DOUBLE_ELEMENTS: {
9621 ExternalDoubleArray* array = ExternalDoubleArray::cast(elements());
9622 return array->SetValue(index, value);
9623 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009624 case DICTIONARY_ELEMENTS:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009625 return SetDictionaryElement(index, value, attr, strict_mode,
9626 check_prototype, set_mode);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009627 case NON_STRICT_ARGUMENTS_ELEMENTS: {
9628 FixedArray* parameter_map = FixedArray::cast(elements());
9629 uint32_t length = parameter_map->length();
9630 Object* probe =
9631 (index < length - 2) ? parameter_map->get(index + 2) : NULL;
9632 if (probe != NULL && !probe->IsTheHole()) {
9633 Context* context = Context::cast(parameter_map->get(0));
9634 int context_index = Smi::cast(probe)->value();
9635 ASSERT(!context->get(context_index)->IsTheHole());
9636 context->set(context_index, value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009637 // Redefining attributes of an aliased element destroys fast aliasing.
9638 if (set_mode == SET_PROPERTY || attr == NONE) return value;
9639 parameter_map->set_the_hole(index + 2);
9640 // For elements that are still writable we re-establish slow aliasing.
9641 if ((attr & READ_ONLY) == 0) {
9642 MaybeObject* maybe_entry =
9643 isolate->heap()->AllocateAliasedArgumentsEntry(context_index);
9644 if (!maybe_entry->ToObject(&value)) return maybe_entry;
Ben Murdoch85b71792012-04-11 18:30:58 +01009645 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01009646 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009647 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
9648 if (arguments->IsDictionary()) {
9649 return SetDictionaryElement(index, value, attr, strict_mode,
9650 check_prototype, set_mode);
9651 } else {
9652 return SetFastElement(index, value, strict_mode, check_prototype);
9653 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009654 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009655 }
9656 // All possible cases have been handled above. Add a return to avoid the
9657 // complaints from the compiler.
9658 UNREACHABLE();
Steve Block44f0eee2011-05-26 01:26:41 +01009659 return isolate->heap()->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00009660}
9661
9662
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009663Handle<Object> JSObject::TransitionElementsKind(Handle<JSObject> object,
9664 ElementsKind to_kind) {
9665 CALL_HEAP_FUNCTION(object->GetIsolate(),
9666 object->TransitionElementsKind(to_kind),
9667 Object);
9668}
9669
9670
9671MaybeObject* JSObject::TransitionElementsKind(ElementsKind to_kind) {
9672 ElementsKind from_kind = map()->elements_kind();
9673
9674 Isolate* isolate = GetIsolate();
9675 if (from_kind == FAST_SMI_ONLY_ELEMENTS &&
9676 (to_kind == FAST_ELEMENTS ||
9677 elements() == isolate->heap()->empty_fixed_array())) {
9678 MaybeObject* maybe_new_map = GetElementsTransitionMap(isolate, to_kind);
9679 Map* new_map;
9680 if (!maybe_new_map->To(&new_map)) return maybe_new_map;
9681 set_map(new_map);
9682 if (FLAG_trace_elements_transitions) {
9683 FixedArrayBase* elms = FixedArrayBase::cast(elements());
9684 PrintElementsTransition(stdout, from_kind, elms, to_kind, elms);
9685 }
9686 return this;
9687 }
9688
9689 FixedArrayBase* elms = FixedArrayBase::cast(elements());
9690 uint32_t capacity = static_cast<uint32_t>(elms->length());
9691 uint32_t length = capacity;
9692
9693 if (IsJSArray()) {
9694 Object* raw_length = JSArray::cast(this)->length();
9695 if (raw_length->IsUndefined()) {
9696 // If length is undefined, then JSArray is being initialized and has no
9697 // elements, assume a length of zero.
9698 length = 0;
9699 } else {
9700 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&length));
9701 }
9702 }
9703
9704 if (from_kind == FAST_SMI_ONLY_ELEMENTS &&
9705 to_kind == FAST_DOUBLE_ELEMENTS) {
9706 MaybeObject* maybe_result =
9707 SetFastDoubleElementsCapacityAndLength(capacity, length);
9708 if (maybe_result->IsFailure()) return maybe_result;
9709 return this;
9710 }
9711
9712 if (from_kind == FAST_DOUBLE_ELEMENTS && to_kind == FAST_ELEMENTS) {
9713 MaybeObject* maybe_result = SetFastElementsCapacityAndLength(
9714 capacity, length, kDontAllowSmiOnlyElements);
9715 if (maybe_result->IsFailure()) return maybe_result;
9716 return this;
9717 }
9718
9719 // This method should never be called for any other case than the ones
9720 // handled above.
9721 UNREACHABLE();
9722 return GetIsolate()->heap()->null_value();
9723}
9724
9725
9726// static
9727bool Map::IsValidElementsTransition(ElementsKind from_kind,
9728 ElementsKind to_kind) {
9729 return
9730 (from_kind == FAST_SMI_ONLY_ELEMENTS &&
9731 (to_kind == FAST_DOUBLE_ELEMENTS || to_kind == FAST_ELEMENTS)) ||
9732 (from_kind == FAST_DOUBLE_ELEMENTS && to_kind == FAST_ELEMENTS);
9733}
9734
9735
John Reck59135872010-11-02 12:39:01 -07009736MaybeObject* JSArray::JSArrayUpdateLengthFromIndex(uint32_t index,
9737 Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009738 uint32_t old_len = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01009739 CHECK(length()->ToArrayIndex(&old_len));
Steve Blocka7e24c12009-10-30 11:49:00 +00009740 // Check to see if we need to update the length. For now, we make
9741 // sure that the length stays within 32-bits (unsigned).
9742 if (index >= old_len && index != 0xffffffff) {
John Reck59135872010-11-02 12:39:01 -07009743 Object* len;
9744 { MaybeObject* maybe_len =
Steve Block44f0eee2011-05-26 01:26:41 +01009745 GetHeap()->NumberFromDouble(static_cast<double>(index) + 1);
John Reck59135872010-11-02 12:39:01 -07009746 if (!maybe_len->ToObject(&len)) return maybe_len;
9747 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009748 set_length(len);
9749 }
9750 return value;
9751}
9752
9753
Ben Murdoche0cee9b2011-05-25 10:26:03 +01009754MaybeObject* JSObject::GetElementWithInterceptor(Object* receiver,
John Reck59135872010-11-02 12:39:01 -07009755 uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +01009756 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00009757 // Make sure that the top context does not change when doing
9758 // callbacks or interceptor calls.
9759 AssertNoContextChange ncc;
Steve Block44f0eee2011-05-26 01:26:41 +01009760 HandleScope scope(isolate);
9761 Handle<InterceptorInfo> interceptor(GetIndexedInterceptor(), isolate);
9762 Handle<Object> this_handle(receiver, isolate);
9763 Handle<JSObject> holder_handle(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00009764 if (!interceptor->getter()->IsUndefined()) {
9765 v8::IndexedPropertyGetter getter =
9766 v8::ToCData<v8::IndexedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +01009767 LOG(isolate,
9768 ApiIndexedPropertyAccess("interceptor-indexed-get", this, index));
9769 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00009770 v8::AccessorInfo info(args.end());
9771 v8::Handle<v8::Value> result;
9772 {
9773 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01009774 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00009775 result = getter(index, info);
9776 }
Steve Block44f0eee2011-05-26 01:26:41 +01009777 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00009778 if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result);
9779 }
9780
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009781 Heap* heap = holder_handle->GetHeap();
9782 ElementsAccessor* handler = holder_handle->GetElementsAccessor();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009783 MaybeObject* raw_result = handler->Get(*this_handle,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009784 *holder_handle,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009785 index);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009786 if (raw_result != heap->the_hole_value()) return raw_result;
9787
Steve Block44f0eee2011-05-26 01:26:41 +01009788 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00009789
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009790 Object* pt = holder_handle->GetPrototype();
Steve Block44f0eee2011-05-26 01:26:41 +01009791 if (pt == heap->null_value()) return heap->undefined_value();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009792 return pt->GetElementWithReceiver(*this_handle, index);
Steve Blocka7e24c12009-10-30 11:49:00 +00009793}
9794
9795
9796bool JSObject::HasDenseElements() {
9797 int capacity = 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009798 int used = 0;
9799 GetElementsCapacityAndUsage(&capacity, &used);
9800 return (capacity == 0) || (used > (capacity / 2));
9801}
9802
9803
9804void JSObject::GetElementsCapacityAndUsage(int* capacity, int* used) {
9805 *capacity = 0;
9806 *used = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00009807
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009808 FixedArrayBase* backing_store_base = FixedArrayBase::cast(elements());
9809 FixedArray* backing_store = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00009810 switch (GetElementsKind()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009811 case NON_STRICT_ARGUMENTS_ELEMENTS:
9812 backing_store_base =
9813 FixedArray::cast(FixedArray::cast(backing_store_base)->get(1));
9814 backing_store = FixedArray::cast(backing_store_base);
9815 if (backing_store->IsDictionary()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009816 SeededNumberDictionary* dictionary =
9817 SeededNumberDictionary::cast(backing_store);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009818 *capacity = dictionary->Capacity();
9819 *used = dictionary->NumberOfElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009820 break;
9821 }
9822 // Fall through.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009823 case FAST_SMI_ONLY_ELEMENTS:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009824 case FAST_ELEMENTS:
9825 backing_store = FixedArray::cast(backing_store_base);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009826 *capacity = backing_store->length();
9827 for (int i = 0; i < *capacity; ++i) {
9828 if (!backing_store->get(i)->IsTheHole()) ++(*used);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009829 }
9830 break;
9831 case DICTIONARY_ELEMENTS: {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009832 SeededNumberDictionary* dictionary =
9833 SeededNumberDictionary::cast(FixedArray::cast(elements()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009834 *capacity = dictionary->Capacity();
9835 *used = dictionary->NumberOfElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009836 break;
9837 }
9838 case FAST_DOUBLE_ELEMENTS: {
9839 FixedDoubleArray* elms = FixedDoubleArray::cast(elements());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009840 *capacity = elms->length();
9841 for (int i = 0; i < *capacity; i++) {
9842 if (!elms->is_the_hole(i)) ++(*used);
Steve Blocka7e24c12009-10-30 11:49:00 +00009843 }
9844 break;
9845 }
Steve Block3ce2e202009-11-05 08:53:23 +00009846 case EXTERNAL_BYTE_ELEMENTS:
9847 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
9848 case EXTERNAL_SHORT_ELEMENTS:
9849 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
9850 case EXTERNAL_INT_ELEMENTS:
9851 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +00009852 case EXTERNAL_FLOAT_ELEMENTS:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009853 case EXTERNAL_DOUBLE_ELEMENTS:
9854 case EXTERNAL_PIXEL_ELEMENTS:
9855 // External arrays are considered 100% used.
9856 ExternalArray* external_array = ExternalArray::cast(elements());
9857 *capacity = external_array->length();
9858 *used = external_array->length();
9859 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00009860 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009861}
9862
9863
9864bool JSObject::ShouldConvertToSlowElements(int new_capacity) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009865 STATIC_ASSERT(kMaxUncheckedOldFastElementsLength <=
9866 kMaxUncheckedFastElementsLength);
9867 if (new_capacity <= kMaxUncheckedOldFastElementsLength ||
9868 (new_capacity <= kMaxUncheckedFastElementsLength &&
9869 GetHeap()->InNewSpace(this))) {
9870 return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009871 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009872 // If the fast-case backing storage takes up roughly three times as
9873 // much space (in machine words) as a dictionary backing storage
9874 // would, the object should have slow elements.
9875 int old_capacity = 0;
9876 int used_elements = 0;
9877 GetElementsCapacityAndUsage(&old_capacity, &used_elements);
Ben Murdochc7cc0282012-03-05 14:35:55 +00009878 int dictionary_size = SeededNumberDictionary::ComputeCapacity(used_elements) *
9879 SeededNumberDictionary::kEntrySize;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009880 return 3 * dictionary_size <= new_capacity;
Steve Blocka7e24c12009-10-30 11:49:00 +00009881}
9882
9883
9884bool JSObject::ShouldConvertToFastElements() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009885 ASSERT(HasDictionaryElements() || HasDictionaryArgumentsElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00009886 // If the elements are sparse, we should not go back to fast case.
9887 if (!HasDenseElements()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00009888 // An object requiring access checks is never allowed to have fast
9889 // elements. If it had fast elements we would skip security checks.
9890 if (IsAccessCheckNeeded()) return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009891
9892 FixedArray* elements = FixedArray::cast(this->elements());
Ben Murdochc7cc0282012-03-05 14:35:55 +00009893 SeededNumberDictionary* dictionary = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009894 if (elements->map() == GetHeap()->non_strict_arguments_elements_map()) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009895 dictionary = SeededNumberDictionary::cast(elements->get(1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009896 } else {
Ben Murdochc7cc0282012-03-05 14:35:55 +00009897 dictionary = SeededNumberDictionary::cast(elements);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009898 }
9899 // If an element has been added at a very high index in the elements
9900 // dictionary, we cannot go back to fast case.
9901 if (dictionary->requires_slow_elements()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00009902 // If the dictionary backing storage takes up roughly half as much
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009903 // space (in machine words) as a fast-case backing storage would,
9904 // the object should have fast elements.
9905 uint32_t array_size = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00009906 if (IsJSArray()) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009907 CHECK(JSArray::cast(this)->length()->ToArrayIndex(&array_size));
Steve Blocka7e24c12009-10-30 11:49:00 +00009908 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009909 array_size = dictionary->max_number_key();
Steve Blocka7e24c12009-10-30 11:49:00 +00009910 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009911 uint32_t dictionary_size = static_cast<uint32_t>(dictionary->Capacity()) *
Ben Murdochc7cc0282012-03-05 14:35:55 +00009912 SeededNumberDictionary::kEntrySize;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009913 return 2 * dictionary_size >= array_size;
Steve Blocka7e24c12009-10-30 11:49:00 +00009914}
9915
9916
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009917bool JSObject::ShouldConvertToFastDoubleElements(
9918 bool* has_smi_only_elements) {
9919 *has_smi_only_elements = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009920 if (FLAG_unbox_double_arrays) {
9921 ASSERT(HasDictionaryElements());
Ben Murdochc7cc0282012-03-05 14:35:55 +00009922 SeededNumberDictionary* dictionary =
9923 SeededNumberDictionary::cast(elements());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009924 bool found_double = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009925 for (int i = 0; i < dictionary->Capacity(); i++) {
9926 Object* key = dictionary->KeyAt(i);
9927 if (key->IsNumber()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009928 Object* value = dictionary->ValueAt(i);
9929 if (!value->IsNumber()) return false;
9930 if (!value->IsSmi()) {
9931 found_double = true;
9932 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009933 }
9934 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009935 *has_smi_only_elements = !found_double;
9936 return found_double;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009937 } else {
9938 return false;
9939 }
9940}
9941
9942
Steve Blocka7e24c12009-10-30 11:49:00 +00009943// Certain compilers request function template instantiation when they
9944// see the definition of the other template functions in the
9945// class. This requires us to have the template functions put
9946// together, so even though this function belongs in objects-debug.cc,
9947// we keep it here instead to satisfy certain compilers.
Ben Murdochb0fe1622011-05-05 13:52:32 +01009948#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00009949template<typename Shape, typename Key>
Ben Murdochb0fe1622011-05-05 13:52:32 +01009950void Dictionary<Shape, Key>::Print(FILE* out) {
Steve Blocka7e24c12009-10-30 11:49:00 +00009951 int capacity = HashTable<Shape, Key>::Capacity();
9952 for (int i = 0; i < capacity; i++) {
9953 Object* k = HashTable<Shape, Key>::KeyAt(i);
9954 if (HashTable<Shape, Key>::IsKey(k)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01009955 PrintF(out, " ");
Steve Blocka7e24c12009-10-30 11:49:00 +00009956 if (k->IsString()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01009957 String::cast(k)->StringPrint(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00009958 } else {
Ben Murdochb0fe1622011-05-05 13:52:32 +01009959 k->ShortPrint(out);
Steve Blocka7e24c12009-10-30 11:49:00 +00009960 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01009961 PrintF(out, ": ");
9962 ValueAt(i)->ShortPrint(out);
9963 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +00009964 }
9965 }
9966}
9967#endif
9968
9969
9970template<typename Shape, typename Key>
9971void Dictionary<Shape, Key>::CopyValuesTo(FixedArray* elements) {
9972 int pos = 0;
9973 int capacity = HashTable<Shape, Key>::Capacity();
Leon Clarke4515c472010-02-03 11:58:03 +00009974 AssertNoAllocation no_gc;
9975 WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00009976 for (int i = 0; i < capacity; i++) {
9977 Object* k = Dictionary<Shape, Key>::KeyAt(i);
9978 if (Dictionary<Shape, Key>::IsKey(k)) {
9979 elements->set(pos++, ValueAt(i), mode);
9980 }
9981 }
9982 ASSERT(pos == elements->length());
9983}
9984
9985
9986InterceptorInfo* JSObject::GetNamedInterceptor() {
9987 ASSERT(map()->has_named_interceptor());
9988 JSFunction* constructor = JSFunction::cast(map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01009989 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00009990 Object* result =
Steve Block6ded16b2010-05-10 14:33:55 +01009991 constructor->shared()->get_api_func_data()->named_property_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +00009992 return InterceptorInfo::cast(result);
9993}
9994
9995
9996InterceptorInfo* JSObject::GetIndexedInterceptor() {
9997 ASSERT(map()->has_indexed_interceptor());
9998 JSFunction* constructor = JSFunction::cast(map()->constructor());
Steve Block6ded16b2010-05-10 14:33:55 +01009999 ASSERT(constructor->shared()->IsApiFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +000010000 Object* result =
Steve Block6ded16b2010-05-10 14:33:55 +010010001 constructor->shared()->get_api_func_data()->indexed_property_handler();
Steve Blocka7e24c12009-10-30 11:49:00 +000010002 return InterceptorInfo::cast(result);
10003}
10004
10005
John Reck59135872010-11-02 12:39:01 -070010006MaybeObject* JSObject::GetPropertyPostInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010007 JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -070010008 String* name,
10009 PropertyAttributes* attributes) {
Steve Blocka7e24c12009-10-30 11:49:00 +000010010 // Check local property in holder, ignore interceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010011 LookupResult result(GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +000010012 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +000010013 if (result.IsProperty()) {
10014 return GetProperty(receiver, &result, name, attributes);
10015 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010016 // Continue searching via the prototype chain.
10017 Object* pt = GetPrototype();
10018 *attributes = ABSENT;
Ben Murdoch8b112d22011-06-08 16:22:53 +010010019 if (pt->IsNull()) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010020 return pt->GetPropertyWithReceiver(receiver, name, attributes);
10021}
10022
10023
John Reck59135872010-11-02 12:39:01 -070010024MaybeObject* JSObject::GetLocalPropertyPostInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010025 JSReceiver* receiver,
Steve Blockd0582a62009-12-15 09:54:21 +000010026 String* name,
10027 PropertyAttributes* attributes) {
10028 // Check local property in holder, ignore interceptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010029 LookupResult result(GetIsolate());
Steve Blockd0582a62009-12-15 09:54:21 +000010030 LocalLookupRealNamedProperty(name, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +000010031 if (result.IsProperty()) {
10032 return GetProperty(receiver, &result, name, attributes);
10033 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010010034 return GetHeap()->undefined_value();
Steve Blockd0582a62009-12-15 09:54:21 +000010035}
10036
10037
John Reck59135872010-11-02 12:39:01 -070010038MaybeObject* JSObject::GetPropertyWithInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010039 JSReceiver* receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +000010040 String* name,
10041 PropertyAttributes* attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +010010042 Isolate* isolate = GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +000010043 InterceptorInfo* interceptor = GetNamedInterceptor();
Steve Block44f0eee2011-05-26 01:26:41 +010010044 HandleScope scope(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010045 Handle<JSReceiver> receiver_handle(receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +000010046 Handle<JSObject> holder_handle(this);
10047 Handle<String> name_handle(name);
10048
10049 if (!interceptor->getter()->IsUndefined()) {
10050 v8::NamedPropertyGetter getter =
10051 v8::ToCData<v8::NamedPropertyGetter>(interceptor->getter());
Steve Block44f0eee2011-05-26 01:26:41 +010010052 LOG(isolate,
10053 ApiNamedPropertyAccess("interceptor-named-get", *holder_handle, name));
10054 CustomArguments args(isolate, interceptor->data(), receiver, this);
Steve Blocka7e24c12009-10-30 11:49:00 +000010055 v8::AccessorInfo info(args.end());
10056 v8::Handle<v8::Value> result;
10057 {
10058 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +010010059 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +000010060 result = getter(v8::Utils::ToLocal(name_handle), info);
10061 }
Steve Block44f0eee2011-05-26 01:26:41 +010010062 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000010063 if (!result.IsEmpty()) {
10064 *attributes = NONE;
10065 return *v8::Utils::OpenHandle(*result);
10066 }
10067 }
10068
John Reck59135872010-11-02 12:39:01 -070010069 MaybeObject* result = holder_handle->GetPropertyPostInterceptor(
Steve Blocka7e24c12009-10-30 11:49:00 +000010070 *receiver_handle,
10071 *name_handle,
10072 attributes);
Steve Block44f0eee2011-05-26 01:26:41 +010010073 RETURN_IF_SCHEDULED_EXCEPTION(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000010074 return result;
10075}
10076
10077
10078bool JSObject::HasRealNamedProperty(String* key) {
10079 // Check access rights if needed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010080 Isolate* isolate = GetIsolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +010010081 if (IsAccessCheckNeeded()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010082 if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) {
10083 isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
Ben Murdoch8b112d22011-06-08 16:22:53 +010010084 return false;
10085 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010086 }
10087
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010088 LookupResult result(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000010089 LocalLookupRealNamedProperty(key, &result);
Andrei Popescu402d9372010-02-26 13:31:12 +000010090 return result.IsProperty() && (result.type() != INTERCEPTOR);
Steve Blocka7e24c12009-10-30 11:49:00 +000010091}
10092
10093
10094bool JSObject::HasRealElementProperty(uint32_t index) {
10095 // Check access rights if needed.
Ben Murdoch8b112d22011-06-08 16:22:53 +010010096 if (IsAccessCheckNeeded()) {
10097 Heap* heap = GetHeap();
10098 if (!heap->isolate()->MayIndexedAccess(this, index, v8::ACCESS_HAS)) {
10099 heap->isolate()->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
10100 return false;
10101 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010102 }
10103
10104 // Handle [] on String objects.
10105 if (this->IsStringObjectWithCharacterAt(index)) return true;
10106
10107 switch (GetElementsKind()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010108 case FAST_SMI_ONLY_ELEMENTS:
Steve Blocka7e24c12009-10-30 11:49:00 +000010109 case FAST_ELEMENTS: {
10110 uint32_t length = IsJSArray() ?
10111 static_cast<uint32_t>(
10112 Smi::cast(JSArray::cast(this)->length())->value()) :
10113 static_cast<uint32_t>(FixedArray::cast(elements())->length());
10114 return (index < length) &&
10115 !FixedArray::cast(elements())->get(index)->IsTheHole();
10116 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010117 case FAST_DOUBLE_ELEMENTS: {
10118 uint32_t length = IsJSArray() ?
10119 static_cast<uint32_t>(
10120 Smi::cast(JSArray::cast(this)->length())->value()) :
10121 static_cast<uint32_t>(FixedDoubleArray::cast(elements())->length());
10122 return (index < length) &&
10123 !FixedDoubleArray::cast(elements())->is_the_hole(index);
10124 break;
10125 }
Steve Block44f0eee2011-05-26 01:26:41 +010010126 case EXTERNAL_PIXEL_ELEMENTS: {
10127 ExternalPixelArray* pixels = ExternalPixelArray::cast(elements());
Steve Blocka7e24c12009-10-30 11:49:00 +000010128 return index < static_cast<uint32_t>(pixels->length());
10129 }
Steve Block3ce2e202009-11-05 08:53:23 +000010130 case EXTERNAL_BYTE_ELEMENTS:
10131 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
10132 case EXTERNAL_SHORT_ELEMENTS:
10133 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
10134 case EXTERNAL_INT_ELEMENTS:
10135 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +000010136 case EXTERNAL_FLOAT_ELEMENTS:
10137 case EXTERNAL_DOUBLE_ELEMENTS: {
Steve Block3ce2e202009-11-05 08:53:23 +000010138 ExternalArray* array = ExternalArray::cast(elements());
10139 return index < static_cast<uint32_t>(array->length());
10140 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010141 case DICTIONARY_ELEMENTS: {
10142 return element_dictionary()->FindEntry(index)
Ben Murdochc7cc0282012-03-05 14:35:55 +000010143 != SeededNumberDictionary::kNotFound;
Steve Blocka7e24c12009-10-30 11:49:00 +000010144 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010145 case NON_STRICT_ARGUMENTS_ELEMENTS:
10146 UNIMPLEMENTED();
Steve Blocka7e24c12009-10-30 11:49:00 +000010147 break;
10148 }
10149 // All possibilities have been handled above already.
10150 UNREACHABLE();
Ben Murdoch8b112d22011-06-08 16:22:53 +010010151 return GetHeap()->null_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000010152}
10153
10154
10155bool JSObject::HasRealNamedCallbackProperty(String* key) {
10156 // Check access rights if needed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010157 Isolate* isolate = GetIsolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +010010158 if (IsAccessCheckNeeded()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010159 if (!isolate->MayNamedAccess(this, key, v8::ACCESS_HAS)) {
10160 isolate->ReportFailedAccessCheck(this, v8::ACCESS_HAS);
Ben Murdoch8b112d22011-06-08 16:22:53 +010010161 return false;
10162 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010163 }
10164
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010165 LookupResult result(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000010166 LocalLookupRealNamedProperty(key, &result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010167 return result.IsFound() && (result.type() == CALLBACKS);
Steve Blocka7e24c12009-10-30 11:49:00 +000010168}
10169
10170
10171int JSObject::NumberOfLocalProperties(PropertyAttributes filter) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010172 return HasFastProperties() ?
10173 map()->NumberOfDescribedProperties(filter) :
10174 property_dictionary()->NumberOfElementsFilterAttributes(filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000010175}
10176
10177
10178void FixedArray::SwapPairs(FixedArray* numbers, int i, int j) {
10179 Object* temp = get(i);
10180 set(i, get(j));
10181 set(j, temp);
10182 if (this != numbers) {
10183 temp = numbers->get(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010184 numbers->set(i, Smi::cast(numbers->get(j)));
10185 numbers->set(j, Smi::cast(temp));
Steve Blocka7e24c12009-10-30 11:49:00 +000010186 }
10187}
10188
10189
10190static void InsertionSortPairs(FixedArray* content,
10191 FixedArray* numbers,
10192 int len) {
10193 for (int i = 1; i < len; i++) {
10194 int j = i;
10195 while (j > 0 &&
10196 (NumberToUint32(numbers->get(j - 1)) >
10197 NumberToUint32(numbers->get(j)))) {
10198 content->SwapPairs(numbers, j - 1, j);
10199 j--;
10200 }
10201 }
10202}
10203
10204
10205void HeapSortPairs(FixedArray* content, FixedArray* numbers, int len) {
10206 // In-place heap sort.
10207 ASSERT(content->length() == numbers->length());
10208
10209 // Bottom-up max-heap construction.
10210 for (int i = 1; i < len; ++i) {
10211 int child_index = i;
10212 while (child_index > 0) {
10213 int parent_index = ((child_index + 1) >> 1) - 1;
10214 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
10215 uint32_t child_value = NumberToUint32(numbers->get(child_index));
10216 if (parent_value < child_value) {
10217 content->SwapPairs(numbers, parent_index, child_index);
10218 } else {
10219 break;
10220 }
10221 child_index = parent_index;
10222 }
10223 }
10224
10225 // Extract elements and create sorted array.
10226 for (int i = len - 1; i > 0; --i) {
10227 // Put max element at the back of the array.
10228 content->SwapPairs(numbers, 0, i);
10229 // Sift down the new top element.
10230 int parent_index = 0;
10231 while (true) {
10232 int child_index = ((parent_index + 1) << 1) - 1;
10233 if (child_index >= i) break;
10234 uint32_t child1_value = NumberToUint32(numbers->get(child_index));
10235 uint32_t child2_value = NumberToUint32(numbers->get(child_index + 1));
10236 uint32_t parent_value = NumberToUint32(numbers->get(parent_index));
10237 if (child_index + 1 >= i || child1_value > child2_value) {
10238 if (parent_value > child1_value) break;
10239 content->SwapPairs(numbers, parent_index, child_index);
10240 parent_index = child_index;
10241 } else {
10242 if (parent_value > child2_value) break;
10243 content->SwapPairs(numbers, parent_index, child_index + 1);
10244 parent_index = child_index + 1;
10245 }
10246 }
10247 }
10248}
10249
10250
10251// Sort this array and the numbers as pairs wrt. the (distinct) numbers.
10252void FixedArray::SortPairs(FixedArray* numbers, uint32_t len) {
10253 ASSERT(this->length() == numbers->length());
10254 // For small arrays, simply use insertion sort.
10255 if (len <= 10) {
10256 InsertionSortPairs(this, numbers, len);
10257 return;
10258 }
10259 // Check the range of indices.
10260 uint32_t min_index = NumberToUint32(numbers->get(0));
10261 uint32_t max_index = min_index;
10262 uint32_t i;
10263 for (i = 1; i < len; i++) {
10264 if (NumberToUint32(numbers->get(i)) < min_index) {
10265 min_index = NumberToUint32(numbers->get(i));
10266 } else if (NumberToUint32(numbers->get(i)) > max_index) {
10267 max_index = NumberToUint32(numbers->get(i));
10268 }
10269 }
10270 if (max_index - min_index + 1 == len) {
10271 // Indices form a contiguous range, unless there are duplicates.
10272 // Do an in-place linear time sort assuming distinct numbers, but
10273 // avoid hanging in case they are not.
10274 for (i = 0; i < len; i++) {
10275 uint32_t p;
10276 uint32_t j = 0;
10277 // While the current element at i is not at its correct position p,
10278 // swap the elements at these two positions.
10279 while ((p = NumberToUint32(numbers->get(i)) - min_index) != i &&
10280 j++ < len) {
10281 SwapPairs(numbers, i, p);
10282 }
10283 }
10284 } else {
10285 HeapSortPairs(this, numbers, len);
10286 return;
10287 }
10288}
10289
10290
10291// Fill in the names of local properties into the supplied storage. The main
10292// purpose of this function is to provide reflection information for the object
10293// mirrors.
10294void JSObject::GetLocalPropertyNames(FixedArray* storage, int index) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010295 ASSERT(storage->length() >= (NumberOfLocalProperties() - index));
Steve Blocka7e24c12009-10-30 11:49:00 +000010296 if (HasFastProperties()) {
10297 DescriptorArray* descs = map()->instance_descriptors();
10298 for (int i = 0; i < descs->number_of_descriptors(); i++) {
10299 if (descs->IsProperty(i)) storage->set(index++, descs->GetKey(i));
10300 }
10301 ASSERT(storage->length() >= index);
10302 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010303 property_dictionary()->CopyKeysTo(storage,
10304 index,
10305 StringDictionary::UNSORTED);
Steve Blocka7e24c12009-10-30 11:49:00 +000010306 }
10307}
10308
10309
10310int JSObject::NumberOfLocalElements(PropertyAttributes filter) {
10311 return GetLocalElementKeys(NULL, filter);
10312}
10313
10314
10315int JSObject::NumberOfEnumElements() {
Steve Blockd0582a62009-12-15 09:54:21 +000010316 // Fast case for objects with no elements.
10317 if (!IsJSValue() && HasFastElements()) {
10318 uint32_t length = IsJSArray() ?
10319 static_cast<uint32_t>(
10320 Smi::cast(JSArray::cast(this)->length())->value()) :
10321 static_cast<uint32_t>(FixedArray::cast(elements())->length());
10322 if (length == 0) return 0;
10323 }
10324 // Compute the number of enumerable elements.
Steve Blocka7e24c12009-10-30 11:49:00 +000010325 return NumberOfLocalElements(static_cast<PropertyAttributes>(DONT_ENUM));
10326}
10327
10328
10329int JSObject::GetLocalElementKeys(FixedArray* storage,
10330 PropertyAttributes filter) {
10331 int counter = 0;
10332 switch (GetElementsKind()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010333 case FAST_SMI_ONLY_ELEMENTS:
Steve Blocka7e24c12009-10-30 11:49:00 +000010334 case FAST_ELEMENTS: {
10335 int length = IsJSArray() ?
10336 Smi::cast(JSArray::cast(this)->length())->value() :
10337 FixedArray::cast(elements())->length();
10338 for (int i = 0; i < length; i++) {
10339 if (!FixedArray::cast(elements())->get(i)->IsTheHole()) {
10340 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +000010341 storage->set(counter, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +000010342 }
10343 counter++;
10344 }
10345 }
10346 ASSERT(!storage || storage->length() >= counter);
10347 break;
10348 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010349 case FAST_DOUBLE_ELEMENTS: {
10350 int length = IsJSArray() ?
10351 Smi::cast(JSArray::cast(this)->length())->value() :
10352 FixedDoubleArray::cast(elements())->length();
10353 for (int i = 0; i < length; i++) {
10354 if (!FixedDoubleArray::cast(elements())->is_the_hole(i)) {
10355 if (storage != NULL) {
10356 storage->set(counter, Smi::FromInt(i));
10357 }
10358 counter++;
10359 }
10360 }
10361 ASSERT(!storage || storage->length() >= counter);
10362 break;
10363 }
Steve Block44f0eee2011-05-26 01:26:41 +010010364 case EXTERNAL_PIXEL_ELEMENTS: {
10365 int length = ExternalPixelArray::cast(elements())->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000010366 while (counter < length) {
10367 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +000010368 storage->set(counter, Smi::FromInt(counter));
Steve Blocka7e24c12009-10-30 11:49:00 +000010369 }
10370 counter++;
10371 }
10372 ASSERT(!storage || storage->length() >= counter);
10373 break;
10374 }
Steve Block3ce2e202009-11-05 08:53:23 +000010375 case EXTERNAL_BYTE_ELEMENTS:
10376 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
10377 case EXTERNAL_SHORT_ELEMENTS:
10378 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
10379 case EXTERNAL_INT_ELEMENTS:
10380 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
Ben Murdoch257744e2011-11-30 15:57:28 +000010381 case EXTERNAL_FLOAT_ELEMENTS:
10382 case EXTERNAL_DOUBLE_ELEMENTS: {
Steve Block3ce2e202009-11-05 08:53:23 +000010383 int length = ExternalArray::cast(elements())->length();
10384 while (counter < length) {
10385 if (storage != NULL) {
Leon Clarke4515c472010-02-03 11:58:03 +000010386 storage->set(counter, Smi::FromInt(counter));
Steve Block3ce2e202009-11-05 08:53:23 +000010387 }
10388 counter++;
10389 }
10390 ASSERT(!storage || storage->length() >= counter);
10391 break;
10392 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010393 case DICTIONARY_ELEMENTS: {
10394 if (storage != NULL) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010395 element_dictionary()->CopyKeysTo(storage,
10396 filter,
Ben Murdochc7cc0282012-03-05 14:35:55 +000010397 SeededNumberDictionary::SORTED);
Steve Blocka7e24c12009-10-30 11:49:00 +000010398 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010399 counter += element_dictionary()->NumberOfElementsFilterAttributes(filter);
Steve Blocka7e24c12009-10-30 11:49:00 +000010400 break;
10401 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010402 case NON_STRICT_ARGUMENTS_ELEMENTS: {
10403 FixedArray* parameter_map = FixedArray::cast(elements());
10404 int mapped_length = parameter_map->length() - 2;
10405 FixedArray* arguments = FixedArray::cast(parameter_map->get(1));
10406 if (arguments->IsDictionary()) {
10407 // Copy the keys from arguments first, because Dictionary::CopyKeysTo
10408 // will insert in storage starting at index 0.
Ben Murdochc7cc0282012-03-05 14:35:55 +000010409 SeededNumberDictionary* dictionary =
10410 SeededNumberDictionary::cast(arguments);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010411 if (storage != NULL) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000010412 dictionary->CopyKeysTo(
10413 storage, filter, SeededNumberDictionary::UNSORTED);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010414 }
10415 counter += dictionary->NumberOfElementsFilterAttributes(filter);
10416 for (int i = 0; i < mapped_length; ++i) {
10417 if (!parameter_map->get(i + 2)->IsTheHole()) {
10418 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
10419 ++counter;
10420 }
10421 }
10422 if (storage != NULL) storage->SortPairs(storage, counter);
10423
10424 } else {
10425 int backing_length = arguments->length();
10426 int i = 0;
10427 for (; i < mapped_length; ++i) {
10428 if (!parameter_map->get(i + 2)->IsTheHole()) {
10429 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
10430 ++counter;
10431 } else if (i < backing_length && !arguments->get(i)->IsTheHole()) {
10432 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
10433 ++counter;
10434 }
10435 }
10436 for (; i < backing_length; ++i) {
10437 if (storage != NULL) storage->set(counter, Smi::FromInt(i));
10438 ++counter;
10439 }
10440 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010441 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010442 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010443 }
10444
10445 if (this->IsJSValue()) {
10446 Object* val = JSValue::cast(this)->value();
10447 if (val->IsString()) {
10448 String* str = String::cast(val);
10449 if (storage) {
10450 for (int i = 0; i < str->length(); i++) {
Leon Clarke4515c472010-02-03 11:58:03 +000010451 storage->set(counter + i, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +000010452 }
10453 }
10454 counter += str->length();
10455 }
10456 }
10457 ASSERT(!storage || storage->length() == counter);
10458 return counter;
10459}
10460
10461
10462int JSObject::GetEnumElementKeys(FixedArray* storage) {
10463 return GetLocalElementKeys(storage,
10464 static_cast<PropertyAttributes>(DONT_ENUM));
10465}
10466
10467
Steve Blocka7e24c12009-10-30 11:49:00 +000010468// StringKey simply carries a string object as key.
10469class StringKey : public HashTableKey {
10470 public:
10471 explicit StringKey(String* string) :
10472 string_(string),
10473 hash_(HashForObject(string)) { }
10474
10475 bool IsMatch(Object* string) {
10476 // We know that all entries in a hash table had their hash keys created.
10477 // Use that knowledge to have fast failure.
10478 if (hash_ != HashForObject(string)) {
10479 return false;
10480 }
10481 return string_->Equals(String::cast(string));
10482 }
10483
10484 uint32_t Hash() { return hash_; }
10485
10486 uint32_t HashForObject(Object* other) { return String::cast(other)->Hash(); }
10487
10488 Object* AsObject() { return string_; }
10489
10490 String* string_;
10491 uint32_t hash_;
10492};
10493
10494
10495// StringSharedKeys are used as keys in the eval cache.
10496class StringSharedKey : public HashTableKey {
10497 public:
Steve Block1e0659c2011-05-24 12:43:12 +010010498 StringSharedKey(String* source,
10499 SharedFunctionInfo* shared,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010500 LanguageMode language_mode,
10501 int scope_position)
Steve Block1e0659c2011-05-24 12:43:12 +010010502 : source_(source),
10503 shared_(shared),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010504 language_mode_(language_mode),
10505 scope_position_(scope_position) { }
Steve Blocka7e24c12009-10-30 11:49:00 +000010506
10507 bool IsMatch(Object* other) {
10508 if (!other->IsFixedArray()) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010509 FixedArray* other_array = FixedArray::cast(other);
10510 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
Steve Blocka7e24c12009-10-30 11:49:00 +000010511 if (shared != shared_) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010512 int language_unchecked = Smi::cast(other_array->get(2))->value();
10513 ASSERT(language_unchecked == CLASSIC_MODE ||
10514 language_unchecked == STRICT_MODE ||
10515 language_unchecked == EXTENDED_MODE);
10516 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
10517 if (language_mode != language_mode_) return false;
10518 int scope_position = Smi::cast(other_array->get(3))->value();
10519 if (scope_position != scope_position_) return false;
10520 String* source = String::cast(other_array->get(1));
Steve Blocka7e24c12009-10-30 11:49:00 +000010521 return source->Equals(source_);
10522 }
10523
10524 static uint32_t StringSharedHashHelper(String* source,
Steve Block1e0659c2011-05-24 12:43:12 +010010525 SharedFunctionInfo* shared,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010526 LanguageMode language_mode,
10527 int scope_position) {
Steve Blocka7e24c12009-10-30 11:49:00 +000010528 uint32_t hash = source->Hash();
10529 if (shared->HasSourceCode()) {
10530 // Instead of using the SharedFunctionInfo pointer in the hash
10531 // code computation, we use a combination of the hash of the
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010532 // script source code and the start position of the calling scope.
10533 // We do this to ensure that the cache entries can survive garbage
Steve Blocka7e24c12009-10-30 11:49:00 +000010534 // collection.
10535 Script* script = Script::cast(shared->script());
10536 hash ^= String::cast(script->source())->Hash();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010537 if (language_mode == STRICT_MODE) hash ^= 0x8000;
10538 if (language_mode == EXTENDED_MODE) hash ^= 0x0080;
10539 hash += scope_position;
Steve Blocka7e24c12009-10-30 11:49:00 +000010540 }
10541 return hash;
10542 }
10543
10544 uint32_t Hash() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010545 return StringSharedHashHelper(
10546 source_, shared_, language_mode_, scope_position_);
Steve Blocka7e24c12009-10-30 11:49:00 +000010547 }
10548
10549 uint32_t HashForObject(Object* obj) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010550 FixedArray* other_array = FixedArray::cast(obj);
10551 SharedFunctionInfo* shared = SharedFunctionInfo::cast(other_array->get(0));
10552 String* source = String::cast(other_array->get(1));
10553 int language_unchecked = Smi::cast(other_array->get(2))->value();
10554 ASSERT(language_unchecked == CLASSIC_MODE ||
10555 language_unchecked == STRICT_MODE ||
10556 language_unchecked == EXTENDED_MODE);
10557 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
10558 int scope_position = Smi::cast(other_array->get(3))->value();
10559 return StringSharedHashHelper(
10560 source, shared, language_mode, scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000010561 }
10562
John Reck59135872010-11-02 12:39:01 -070010563 MUST_USE_RESULT MaybeObject* AsObject() {
10564 Object* obj;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010565 { MaybeObject* maybe_obj = source_->GetHeap()->AllocateFixedArray(4);
John Reck59135872010-11-02 12:39:01 -070010566 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
10567 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010568 FixedArray* other_array = FixedArray::cast(obj);
10569 other_array->set(0, shared_);
10570 other_array->set(1, source_);
10571 other_array->set(2, Smi::FromInt(language_mode_));
10572 other_array->set(3, Smi::FromInt(scope_position_));
10573 return other_array;
Steve Blocka7e24c12009-10-30 11:49:00 +000010574 }
10575
10576 private:
10577 String* source_;
10578 SharedFunctionInfo* shared_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010579 LanguageMode language_mode_;
10580 int scope_position_;
Steve Blocka7e24c12009-10-30 11:49:00 +000010581};
10582
10583
10584// RegExpKey carries the source and flags of a regular expression as key.
10585class RegExpKey : public HashTableKey {
10586 public:
10587 RegExpKey(String* string, JSRegExp::Flags flags)
10588 : string_(string),
10589 flags_(Smi::FromInt(flags.value())) { }
10590
Steve Block3ce2e202009-11-05 08:53:23 +000010591 // Rather than storing the key in the hash table, a pointer to the
10592 // stored value is stored where the key should be. IsMatch then
10593 // compares the search key to the found object, rather than comparing
10594 // a key to a key.
Steve Blocka7e24c12009-10-30 11:49:00 +000010595 bool IsMatch(Object* obj) {
10596 FixedArray* val = FixedArray::cast(obj);
10597 return string_->Equals(String::cast(val->get(JSRegExp::kSourceIndex)))
10598 && (flags_ == val->get(JSRegExp::kFlagsIndex));
10599 }
10600
10601 uint32_t Hash() { return RegExpHash(string_, flags_); }
10602
10603 Object* AsObject() {
10604 // Plain hash maps, which is where regexp keys are used, don't
10605 // use this function.
10606 UNREACHABLE();
10607 return NULL;
10608 }
10609
10610 uint32_t HashForObject(Object* obj) {
10611 FixedArray* val = FixedArray::cast(obj);
10612 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
10613 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
10614 }
10615
10616 static uint32_t RegExpHash(String* string, Smi* flags) {
10617 return string->Hash() + flags->value();
10618 }
10619
10620 String* string_;
10621 Smi* flags_;
10622};
10623
10624// Utf8SymbolKey carries a vector of chars as key.
10625class Utf8SymbolKey : public HashTableKey {
10626 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +000010627 explicit Utf8SymbolKey(Vector<const char> string, uint32_t seed)
10628 : string_(string), hash_field_(0), seed_(seed) { }
Steve Blocka7e24c12009-10-30 11:49:00 +000010629
10630 bool IsMatch(Object* string) {
10631 return String::cast(string)->IsEqualTo(string_);
10632 }
10633
10634 uint32_t Hash() {
Steve Blockd0582a62009-12-15 09:54:21 +000010635 if (hash_field_ != 0) return hash_field_ >> String::kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +000010636 unibrow::Utf8InputBuffer<> buffer(string_.start(),
10637 static_cast<unsigned>(string_.length()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010638 chars_ = buffer.Utf16Length();
Ben Murdochc7cc0282012-03-05 14:35:55 +000010639 hash_field_ = String::ComputeHashField(&buffer, chars_, seed_);
Steve Blockd0582a62009-12-15 09:54:21 +000010640 uint32_t result = hash_field_ >> String::kHashShift;
Steve Blocka7e24c12009-10-30 11:49:00 +000010641 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
10642 return result;
10643 }
10644
10645 uint32_t HashForObject(Object* other) {
10646 return String::cast(other)->Hash();
10647 }
10648
John Reck59135872010-11-02 12:39:01 -070010649 MaybeObject* AsObject() {
Steve Blockd0582a62009-12-15 09:54:21 +000010650 if (hash_field_ == 0) Hash();
Steve Block44f0eee2011-05-26 01:26:41 +010010651 return Isolate::Current()->heap()->AllocateSymbol(
10652 string_, chars_, hash_field_);
Steve Blocka7e24c12009-10-30 11:49:00 +000010653 }
10654
10655 Vector<const char> string_;
Steve Blockd0582a62009-12-15 09:54:21 +000010656 uint32_t hash_field_;
Steve Blocka7e24c12009-10-30 11:49:00 +000010657 int chars_; // Caches the number of characters when computing the hash code.
Ben Murdochc7cc0282012-03-05 14:35:55 +000010658 uint32_t seed_;
Steve Blocka7e24c12009-10-30 11:49:00 +000010659};
10660
10661
Steve Block9fac8402011-05-12 15:51:54 +010010662template <typename Char>
10663class SequentialSymbolKey : public HashTableKey {
10664 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +000010665 explicit SequentialSymbolKey(Vector<const Char> string, uint32_t seed)
10666 : string_(string), hash_field_(0), seed_(seed) { }
Steve Block9fac8402011-05-12 15:51:54 +010010667
10668 uint32_t Hash() {
Ben Murdochc7cc0282012-03-05 14:35:55 +000010669 StringHasher hasher(string_.length(), seed_);
Steve Block9fac8402011-05-12 15:51:54 +010010670
10671 // Very long strings have a trivial hash that doesn't inspect the
10672 // string contents.
10673 if (hasher.has_trivial_hash()) {
10674 hash_field_ = hasher.GetHashField();
10675 } else {
10676 int i = 0;
10677 // Do the iterative array index computation as long as there is a
10678 // chance this is an array index.
10679 while (i < string_.length() && hasher.is_array_index()) {
10680 hasher.AddCharacter(static_cast<uc32>(string_[i]));
10681 i++;
10682 }
10683
10684 // Process the remaining characters without updating the array
10685 // index.
10686 while (i < string_.length()) {
10687 hasher.AddCharacterNoIndex(static_cast<uc32>(string_[i]));
10688 i++;
10689 }
10690 hash_field_ = hasher.GetHashField();
10691 }
10692
10693 uint32_t result = hash_field_ >> String::kHashShift;
10694 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
10695 return result;
10696 }
10697
10698
10699 uint32_t HashForObject(Object* other) {
10700 return String::cast(other)->Hash();
10701 }
10702
10703 Vector<const Char> string_;
10704 uint32_t hash_field_;
Ben Murdochc7cc0282012-03-05 14:35:55 +000010705 uint32_t seed_;
Steve Block9fac8402011-05-12 15:51:54 +010010706};
10707
10708
10709
10710class AsciiSymbolKey : public SequentialSymbolKey<char> {
10711 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +000010712 AsciiSymbolKey(Vector<const char> str, uint32_t seed)
10713 : SequentialSymbolKey<char>(str, seed) { }
Steve Block9fac8402011-05-12 15:51:54 +010010714
10715 bool IsMatch(Object* string) {
10716 return String::cast(string)->IsAsciiEqualTo(string_);
10717 }
10718
10719 MaybeObject* AsObject() {
10720 if (hash_field_ == 0) Hash();
Steve Block44f0eee2011-05-26 01:26:41 +010010721 return HEAP->AllocateAsciiSymbol(string_, hash_field_);
Steve Block9fac8402011-05-12 15:51:54 +010010722 }
10723};
10724
10725
Ben Murdoch257744e2011-11-30 15:57:28 +000010726class SubStringAsciiSymbolKey : public HashTableKey {
10727 public:
10728 explicit SubStringAsciiSymbolKey(Handle<SeqAsciiString> string,
10729 int from,
Ben Murdochc7cc0282012-03-05 14:35:55 +000010730 int length,
10731 uint32_t seed)
10732 : string_(string), from_(from), length_(length), seed_(seed) { }
Ben Murdoch257744e2011-11-30 15:57:28 +000010733
10734 uint32_t Hash() {
10735 ASSERT(length_ >= 0);
10736 ASSERT(from_ + length_ <= string_->length());
Ben Murdochc7cc0282012-03-05 14:35:55 +000010737 StringHasher hasher(length_, string_->GetHeap()->HashSeed());
Ben Murdoch257744e2011-11-30 15:57:28 +000010738
10739 // Very long strings have a trivial hash that doesn't inspect the
10740 // string contents.
10741 if (hasher.has_trivial_hash()) {
10742 hash_field_ = hasher.GetHashField();
10743 } else {
10744 int i = 0;
10745 // Do the iterative array index computation as long as there is a
10746 // chance this is an array index.
10747 while (i < length_ && hasher.is_array_index()) {
10748 hasher.AddCharacter(static_cast<uc32>(
10749 string_->SeqAsciiStringGet(i + from_)));
10750 i++;
10751 }
10752
10753 // Process the remaining characters without updating the array
10754 // index.
10755 while (i < length_) {
10756 hasher.AddCharacterNoIndex(static_cast<uc32>(
10757 string_->SeqAsciiStringGet(i + from_)));
10758 i++;
10759 }
10760 hash_field_ = hasher.GetHashField();
10761 }
10762
10763 uint32_t result = hash_field_ >> String::kHashShift;
10764 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
10765 return result;
10766 }
10767
10768
10769 uint32_t HashForObject(Object* other) {
10770 return String::cast(other)->Hash();
10771 }
10772
10773 bool IsMatch(Object* string) {
10774 Vector<const char> chars(string_->GetChars() + from_, length_);
10775 return String::cast(string)->IsAsciiEqualTo(chars);
10776 }
10777
10778 MaybeObject* AsObject() {
10779 if (hash_field_ == 0) Hash();
10780 Vector<const char> chars(string_->GetChars() + from_, length_);
10781 return HEAP->AllocateAsciiSymbol(chars, hash_field_);
10782 }
10783
10784 private:
10785 Handle<SeqAsciiString> string_;
10786 int from_;
10787 int length_;
10788 uint32_t hash_field_;
Ben Murdochc7cc0282012-03-05 14:35:55 +000010789 uint32_t seed_;
Ben Murdoch257744e2011-11-30 15:57:28 +000010790};
10791
10792
Steve Block9fac8402011-05-12 15:51:54 +010010793class TwoByteSymbolKey : public SequentialSymbolKey<uc16> {
10794 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +000010795 explicit TwoByteSymbolKey(Vector<const uc16> str, uint32_t seed)
10796 : SequentialSymbolKey<uc16>(str, seed) { }
Steve Block9fac8402011-05-12 15:51:54 +010010797
10798 bool IsMatch(Object* string) {
10799 return String::cast(string)->IsTwoByteEqualTo(string_);
10800 }
10801
10802 MaybeObject* AsObject() {
10803 if (hash_field_ == 0) Hash();
Steve Block44f0eee2011-05-26 01:26:41 +010010804 return HEAP->AllocateTwoByteSymbol(string_, hash_field_);
Steve Block9fac8402011-05-12 15:51:54 +010010805 }
10806};
10807
10808
Steve Blocka7e24c12009-10-30 11:49:00 +000010809// SymbolKey carries a string/symbol object as key.
10810class SymbolKey : public HashTableKey {
10811 public:
Steve Block44f0eee2011-05-26 01:26:41 +010010812 explicit SymbolKey(String* string)
10813 : string_(string) { }
Steve Blocka7e24c12009-10-30 11:49:00 +000010814
10815 bool IsMatch(Object* string) {
10816 return String::cast(string)->Equals(string_);
10817 }
10818
10819 uint32_t Hash() { return string_->Hash(); }
10820
10821 uint32_t HashForObject(Object* other) {
10822 return String::cast(other)->Hash();
10823 }
10824
John Reck59135872010-11-02 12:39:01 -070010825 MaybeObject* AsObject() {
Leon Clarkef7060e22010-06-03 12:02:55 +010010826 // Attempt to flatten the string, so that symbols will most often
10827 // be flat strings.
10828 string_ = string_->TryFlattenGetString();
Steve Block44f0eee2011-05-26 01:26:41 +010010829 Heap* heap = string_->GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +000010830 // Transform string to symbol if possible.
Steve Block44f0eee2011-05-26 01:26:41 +010010831 Map* map = heap->SymbolMapForString(string_);
Steve Blocka7e24c12009-10-30 11:49:00 +000010832 if (map != NULL) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010833 string_->set_map_no_write_barrier(map);
Steve Blocka7e24c12009-10-30 11:49:00 +000010834 ASSERT(string_->IsSymbol());
10835 return string_;
10836 }
10837 // Otherwise allocate a new symbol.
10838 StringInputBuffer buffer(string_);
Steve Block44f0eee2011-05-26 01:26:41 +010010839 return heap->AllocateInternalSymbol(&buffer,
Steve Blocka7e24c12009-10-30 11:49:00 +000010840 string_->length(),
Steve Blockd0582a62009-12-15 09:54:21 +000010841 string_->hash_field());
Steve Blocka7e24c12009-10-30 11:49:00 +000010842 }
10843
10844 static uint32_t StringHash(Object* obj) {
10845 return String::cast(obj)->Hash();
10846 }
10847
10848 String* string_;
10849};
10850
10851
10852template<typename Shape, typename Key>
10853void HashTable<Shape, Key>::IteratePrefix(ObjectVisitor* v) {
10854 IteratePointers(v, 0, kElementsStartOffset);
10855}
10856
10857
10858template<typename Shape, typename Key>
10859void HashTable<Shape, Key>::IterateElements(ObjectVisitor* v) {
10860 IteratePointers(v,
10861 kElementsStartOffset,
10862 kHeaderSize + length() * kPointerSize);
10863}
10864
10865
10866template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070010867MaybeObject* HashTable<Shape, Key>::Allocate(int at_least_space_for,
10868 PretenureFlag pretenure) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010869 int capacity = ComputeCapacity(at_least_space_for);
10870 if (capacity > HashTable::kMaxCapacity) {
Leon Clarkee46be812010-01-19 14:06:41 +000010871 return Failure::OutOfMemoryException();
10872 }
10873
John Reck59135872010-11-02 12:39:01 -070010874 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +010010875 { MaybeObject* maybe_obj = Isolate::Current()->heap()->
10876 AllocateHashTable(EntryToIndex(capacity), pretenure);
John Reck59135872010-11-02 12:39:01 -070010877 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +000010878 }
John Reck59135872010-11-02 12:39:01 -070010879 HashTable::cast(obj)->SetNumberOfElements(0);
10880 HashTable::cast(obj)->SetNumberOfDeletedElements(0);
10881 HashTable::cast(obj)->SetCapacity(capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +000010882 return obj;
10883}
10884
10885
Leon Clarkee46be812010-01-19 14:06:41 +000010886// Find entry for key otherwise return kNotFound.
Ben Murdoch3bec4d22010-07-22 14:51:16 +010010887int StringDictionary::FindEntry(String* key) {
10888 if (!key->IsSymbol()) {
10889 return HashTable<StringDictionaryShape, String*>::FindEntry(key);
10890 }
10891
10892 // Optimized for symbol key. Knowledge of the key type allows:
10893 // 1. Move the check if the key is a symbol out of the loop.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010894 // 2. Avoid comparing hash codes in symbol to symbol comparison.
Ben Murdoch3bec4d22010-07-22 14:51:16 +010010895 // 3. Detect a case when a dictionary key is not a symbol but the key is.
10896 // In case of positive result the dictionary key may be replaced by
10897 // the symbol with minimal performance penalty. It gives a chance to
10898 // perform further lookups in code stubs (and significant performance boost
10899 // a certain style of code).
10900
10901 // EnsureCapacity will guarantee the hash table is never full.
10902 uint32_t capacity = Capacity();
10903 uint32_t entry = FirstProbe(key->Hash(), capacity);
10904 uint32_t count = 1;
10905
10906 while (true) {
10907 int index = EntryToIndex(entry);
10908 Object* element = get(index);
10909 if (element->IsUndefined()) break; // Empty entry.
10910 if (key == element) return entry;
10911 if (!element->IsSymbol() &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010912 !element->IsTheHole() &&
Ben Murdoch3bec4d22010-07-22 14:51:16 +010010913 String::cast(element)->Equals(key)) {
10914 // Replace a non-symbol key by the equivalent symbol for faster further
10915 // lookups.
10916 set(index, key);
10917 return entry;
10918 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010919 ASSERT(element->IsTheHole() || !String::cast(element)->Equals(key));
Ben Murdoch3bec4d22010-07-22 14:51:16 +010010920 entry = NextProbe(entry, count++, capacity);
10921 }
10922 return kNotFound;
10923}
10924
10925
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010926bool StringDictionary::ContainsTransition(int entry) {
10927 switch (DetailsAt(entry).type()) {
10928 case MAP_TRANSITION:
10929 case CONSTANT_TRANSITION:
10930 case ELEMENTS_TRANSITION:
10931 return true;
10932 case CALLBACKS: {
10933 Object* value = ValueAt(entry);
10934 if (!value->IsAccessorPair()) return false;
10935 AccessorPair* accessors = AccessorPair::cast(value);
10936 return accessors->getter()->IsMap() || accessors->setter()->IsMap();
10937 }
10938 case NORMAL:
10939 case FIELD:
10940 case CONSTANT_FUNCTION:
10941 case HANDLER:
10942 case INTERCEPTOR:
10943 case NULL_DESCRIPTOR:
10944 return false;
10945 }
10946 UNREACHABLE(); // Keep the compiler happy.
10947 return false;
10948}
10949
10950
Steve Blocka7e24c12009-10-30 11:49:00 +000010951template<typename Shape, typename Key>
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010952MaybeObject* HashTable<Shape, Key>::Rehash(HashTable* new_table, Key key) {
10953 ASSERT(NumberOfElements() < new_table->Capacity());
10954
10955 AssertNoAllocation no_gc;
10956 WriteBarrierMode mode = new_table->GetWriteBarrierMode(no_gc);
10957
10958 // Copy prefix to new array.
10959 for (int i = kPrefixStartIndex;
10960 i < kPrefixStartIndex + Shape::kPrefixSize;
10961 i++) {
10962 new_table->set(i, get(i), mode);
10963 }
10964
10965 // Rehash the elements.
10966 int capacity = Capacity();
10967 for (int i = 0; i < capacity; i++) {
10968 uint32_t from_index = EntryToIndex(i);
10969 Object* k = get(from_index);
10970 if (IsKey(k)) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000010971 uint32_t hash = HashTable<Shape, Key>::HashForObject(key, k);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000010972 uint32_t insertion_index =
10973 EntryToIndex(new_table->FindInsertionEntry(hash));
10974 for (int j = 0; j < Shape::kEntrySize; j++) {
10975 new_table->set(insertion_index + j, get(from_index + j), mode);
10976 }
10977 }
10978 }
10979 new_table->SetNumberOfElements(NumberOfElements());
10980 new_table->SetNumberOfDeletedElements(0);
10981 return new_table;
10982}
10983
10984
10985template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070010986MaybeObject* HashTable<Shape, Key>::EnsureCapacity(int n, Key key) {
Steve Blocka7e24c12009-10-30 11:49:00 +000010987 int capacity = Capacity();
10988 int nof = NumberOfElements() + n;
Leon Clarkee46be812010-01-19 14:06:41 +000010989 int nod = NumberOfDeletedElements();
10990 // Return if:
10991 // 50% is still free after adding n elements and
10992 // at most 50% of the free elements are deleted elements.
Steve Block6ded16b2010-05-10 14:33:55 +010010993 if (nod <= (capacity - nof) >> 1) {
10994 int needed_free = nof >> 1;
10995 if (nof + needed_free <= capacity) return this;
10996 }
Steve Blocka7e24c12009-10-30 11:49:00 +000010997
Steve Block6ded16b2010-05-10 14:33:55 +010010998 const int kMinCapacityForPretenure = 256;
10999 bool pretenure =
Ben Murdoch8b112d22011-06-08 16:22:53 +010011000 (capacity > kMinCapacityForPretenure) && !GetHeap()->InNewSpace(this);
John Reck59135872010-11-02 12:39:01 -070011001 Object* obj;
11002 { MaybeObject* maybe_obj =
11003 Allocate(nof * 2, pretenure ? TENURED : NOT_TENURED);
11004 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11005 }
Leon Clarke4515c472010-02-03 11:58:03 +000011006
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011007 return Rehash(HashTable::cast(obj), key);
11008}
Steve Blocka7e24c12009-10-30 11:49:00 +000011009
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011010
11011template<typename Shape, typename Key>
11012MaybeObject* HashTable<Shape, Key>::Shrink(Key key) {
11013 int capacity = Capacity();
11014 int nof = NumberOfElements();
11015
11016 // Shrink to fit the number of elements if only a quarter of the
11017 // capacity is filled with elements.
11018 if (nof > (capacity >> 2)) return this;
11019 // Allocate a new dictionary with room for at least the current
11020 // number of elements. The allocation method will make sure that
11021 // there is extra room in the dictionary for additions. Don't go
11022 // lower than room for 16 elements.
11023 int at_least_room_for = nof;
11024 if (at_least_room_for < 16) return this;
11025
11026 const int kMinCapacityForPretenure = 256;
11027 bool pretenure =
11028 (at_least_room_for > kMinCapacityForPretenure) &&
11029 !GetHeap()->InNewSpace(this);
11030 Object* obj;
11031 { MaybeObject* maybe_obj =
11032 Allocate(at_least_room_for, pretenure ? TENURED : NOT_TENURED);
11033 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +000011034 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011035
11036 return Rehash(HashTable::cast(obj), key);
Steve Blocka7e24c12009-10-30 11:49:00 +000011037}
11038
11039
11040template<typename Shape, typename Key>
11041uint32_t HashTable<Shape, Key>::FindInsertionEntry(uint32_t hash) {
11042 uint32_t capacity = Capacity();
Leon Clarkee46be812010-01-19 14:06:41 +000011043 uint32_t entry = FirstProbe(hash, capacity);
11044 uint32_t count = 1;
11045 // EnsureCapacity will guarantee the hash table is never full.
11046 while (true) {
11047 Object* element = KeyAt(entry);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011048 if (element->IsUndefined() || element->IsTheHole()) break;
Leon Clarkee46be812010-01-19 14:06:41 +000011049 entry = NextProbe(entry, count++, capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +000011050 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011051 return entry;
11052}
11053
11054// Force instantiation of template instances class.
11055// Please note this list is compiler dependent.
11056
11057template class HashTable<SymbolTableShape, HashTableKey*>;
11058
11059template class HashTable<CompilationCacheShape, HashTableKey*>;
11060
11061template class HashTable<MapCacheShape, HashTableKey*>;
11062
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011063template class HashTable<ObjectHashTableShape<1>, Object*>;
11064
11065template class HashTable<ObjectHashTableShape<2>, Object*>;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011066
Steve Blocka7e24c12009-10-30 11:49:00 +000011067template class Dictionary<StringDictionaryShape, String*>;
11068
Ben Murdochc7cc0282012-03-05 14:35:55 +000011069template class Dictionary<SeededNumberDictionaryShape, uint32_t>;
Steve Blocka7e24c12009-10-30 11:49:00 +000011070
Ben Murdochc7cc0282012-03-05 14:35:55 +000011071template class Dictionary<UnseededNumberDictionaryShape, uint32_t>;
11072
11073template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::
11074 Allocate(int at_least_space_for);
11075
11076template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::
11077 Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +000011078
John Reck59135872010-11-02 12:39:01 -070011079template MaybeObject* Dictionary<StringDictionaryShape, String*>::Allocate(
Steve Blocka7e24c12009-10-30 11:49:00 +000011080 int);
11081
Ben Murdochc7cc0282012-03-05 14:35:55 +000011082template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::AtPut(
Steve Blocka7e24c12009-10-30 11:49:00 +000011083 uint32_t, Object*);
11084
Ben Murdochc7cc0282012-03-05 14:35:55 +000011085template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::
11086 AtPut(uint32_t, Object*);
11087
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011088template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>::
11089 SlowReverseLookup(Object* value);
11090
Ben Murdochc7cc0282012-03-05 14:35:55 +000011091template Object* Dictionary<UnseededNumberDictionaryShape, uint32_t>::
11092 SlowReverseLookup(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +000011093
11094template Object* Dictionary<StringDictionaryShape, String*>::SlowReverseLookup(
11095 Object*);
11096
Ben Murdochc7cc0282012-03-05 14:35:55 +000011097template void Dictionary<SeededNumberDictionaryShape, uint32_t>::CopyKeysTo(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011098 FixedArray*,
11099 PropertyAttributes,
Ben Murdochc7cc0282012-03-05 14:35:55 +000011100 Dictionary<SeededNumberDictionaryShape, uint32_t>::SortMode);
Steve Blocka7e24c12009-10-30 11:49:00 +000011101
11102template Object* Dictionary<StringDictionaryShape, String*>::DeleteProperty(
11103 int, JSObject::DeleteMode);
11104
Ben Murdochc7cc0282012-03-05 14:35:55 +000011105template Object* Dictionary<SeededNumberDictionaryShape, uint32_t>::
11106 DeleteProperty(int, JSObject::DeleteMode);
Steve Blocka7e24c12009-10-30 11:49:00 +000011107
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011108template MaybeObject* Dictionary<StringDictionaryShape, String*>::Shrink(
11109 String*);
11110
Ben Murdochc7cc0282012-03-05 14:35:55 +000011111template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Shrink(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011112 uint32_t);
11113
Steve Blocka7e24c12009-10-30 11:49:00 +000011114template void Dictionary<StringDictionaryShape, String*>::CopyKeysTo(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011115 FixedArray*,
11116 int,
11117 Dictionary<StringDictionaryShape, String*>::SortMode);
Steve Blocka7e24c12009-10-30 11:49:00 +000011118
11119template int
11120Dictionary<StringDictionaryShape, String*>::NumberOfElementsFilterAttributes(
11121 PropertyAttributes);
11122
John Reck59135872010-11-02 12:39:01 -070011123template MaybeObject* Dictionary<StringDictionaryShape, String*>::Add(
Steve Blocka7e24c12009-10-30 11:49:00 +000011124 String*, Object*, PropertyDetails);
11125
John Reck59135872010-11-02 12:39:01 -070011126template MaybeObject*
Steve Blocka7e24c12009-10-30 11:49:00 +000011127Dictionary<StringDictionaryShape, String*>::GenerateNewEnumerationIndices();
11128
11129template int
Ben Murdochc7cc0282012-03-05 14:35:55 +000011130Dictionary<SeededNumberDictionaryShape, uint32_t>::
11131 NumberOfElementsFilterAttributes(PropertyAttributes);
Steve Blocka7e24c12009-10-30 11:49:00 +000011132
Ben Murdochc7cc0282012-03-05 14:35:55 +000011133template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::Add(
Steve Blocka7e24c12009-10-30 11:49:00 +000011134 uint32_t, Object*, PropertyDetails);
11135
Ben Murdochc7cc0282012-03-05 14:35:55 +000011136template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::Add(
11137 uint32_t, Object*, PropertyDetails);
11138
11139template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::
11140 EnsureCapacity(int, uint32_t);
11141
11142template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::
John Reck59135872010-11-02 12:39:01 -070011143 EnsureCapacity(int, uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +000011144
John Reck59135872010-11-02 12:39:01 -070011145template MaybeObject* Dictionary<StringDictionaryShape, String*>::
11146 EnsureCapacity(int, String*);
Steve Blocka7e24c12009-10-30 11:49:00 +000011147
Ben Murdochc7cc0282012-03-05 14:35:55 +000011148template MaybeObject* Dictionary<SeededNumberDictionaryShape, uint32_t>::
11149 AddEntry(uint32_t, Object*, PropertyDetails, uint32_t);
11150
11151template MaybeObject* Dictionary<UnseededNumberDictionaryShape, uint32_t>::
11152 AddEntry(uint32_t, Object*, PropertyDetails, uint32_t);
Steve Blocka7e24c12009-10-30 11:49:00 +000011153
John Reck59135872010-11-02 12:39:01 -070011154template MaybeObject* Dictionary<StringDictionaryShape, String*>::AddEntry(
Steve Blocka7e24c12009-10-30 11:49:00 +000011155 String*, Object*, PropertyDetails, uint32_t);
11156
11157template
Ben Murdochc7cc0282012-03-05 14:35:55 +000011158int Dictionary<SeededNumberDictionaryShape, uint32_t>::NumberOfEnumElements();
Steve Blocka7e24c12009-10-30 11:49:00 +000011159
11160template
11161int Dictionary<StringDictionaryShape, String*>::NumberOfEnumElements();
11162
Leon Clarkee46be812010-01-19 14:06:41 +000011163template
Ben Murdochc7cc0282012-03-05 14:35:55 +000011164int HashTable<SeededNumberDictionaryShape, uint32_t>::FindEntry(uint32_t);
Leon Clarkee46be812010-01-19 14:06:41 +000011165
11166
Steve Blocka7e24c12009-10-30 11:49:00 +000011167// Collates undefined and unexisting elements below limit from position
11168// zero of the elements. The object stays in Dictionary mode.
John Reck59135872010-11-02 12:39:01 -070011169MaybeObject* JSObject::PrepareSlowElementsForSort(uint32_t limit) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011170 ASSERT(HasDictionaryElements());
11171 // Must stay in dictionary mode, either because of requires_slow_elements,
11172 // or because we are not going to sort (and therefore compact) all of the
11173 // elements.
Ben Murdochc7cc0282012-03-05 14:35:55 +000011174 SeededNumberDictionary* dict = element_dictionary();
Steve Blocka7e24c12009-10-30 11:49:00 +000011175 HeapNumber* result_double = NULL;
11176 if (limit > static_cast<uint32_t>(Smi::kMaxValue)) {
11177 // Allocate space for result before we start mutating the object.
John Reck59135872010-11-02 12:39:01 -070011178 Object* new_double;
Ben Murdoch8b112d22011-06-08 16:22:53 +010011179 { MaybeObject* maybe_new_double = GetHeap()->AllocateHeapNumber(0.0);
John Reck59135872010-11-02 12:39:01 -070011180 if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double;
11181 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011182 result_double = HeapNumber::cast(new_double);
11183 }
11184
John Reck59135872010-11-02 12:39:01 -070011185 Object* obj;
11186 { MaybeObject* maybe_obj =
Ben Murdochc7cc0282012-03-05 14:35:55 +000011187 SeededNumberDictionary::Allocate(dict->NumberOfElements());
John Reck59135872010-11-02 12:39:01 -070011188 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11189 }
Ben Murdochc7cc0282012-03-05 14:35:55 +000011190 SeededNumberDictionary* new_dict = SeededNumberDictionary::cast(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000011191
11192 AssertNoAllocation no_alloc;
11193
11194 uint32_t pos = 0;
11195 uint32_t undefs = 0;
Steve Block6ded16b2010-05-10 14:33:55 +010011196 int capacity = dict->Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000011197 for (int i = 0; i < capacity; i++) {
11198 Object* k = dict->KeyAt(i);
11199 if (dict->IsKey(k)) {
11200 ASSERT(k->IsNumber());
11201 ASSERT(!k->IsSmi() || Smi::cast(k)->value() >= 0);
11202 ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() >= 0);
11203 ASSERT(!k->IsHeapNumber() || HeapNumber::cast(k)->value() <= kMaxUInt32);
11204 Object* value = dict->ValueAt(i);
11205 PropertyDetails details = dict->DetailsAt(i);
11206 if (details.type() == CALLBACKS) {
11207 // Bail out and do the sorting of undefineds and array holes in JS.
11208 return Smi::FromInt(-1);
11209 }
11210 uint32_t key = NumberToUint32(k);
John Reck59135872010-11-02 12:39:01 -070011211 // In the following we assert that adding the entry to the new dictionary
11212 // does not cause GC. This is the case because we made sure to allocate
11213 // the dictionary big enough above, so it need not grow.
Steve Blocka7e24c12009-10-30 11:49:00 +000011214 if (key < limit) {
11215 if (value->IsUndefined()) {
11216 undefs++;
11217 } else {
Steve Block1e0659c2011-05-24 12:43:12 +010011218 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
11219 // Adding an entry with the key beyond smi-range requires
11220 // allocation. Bailout.
11221 return Smi::FromInt(-1);
11222 }
John Reck59135872010-11-02 12:39:01 -070011223 new_dict->AddNumberEntry(pos, value, details)->ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000011224 pos++;
11225 }
11226 } else {
Steve Block1e0659c2011-05-24 12:43:12 +010011227 if (key > static_cast<uint32_t>(Smi::kMaxValue)) {
11228 // Adding an entry with the key beyond smi-range requires
11229 // allocation. Bailout.
11230 return Smi::FromInt(-1);
11231 }
John Reck59135872010-11-02 12:39:01 -070011232 new_dict->AddNumberEntry(key, value, details)->ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000011233 }
11234 }
11235 }
11236
11237 uint32_t result = pos;
11238 PropertyDetails no_details = PropertyDetails(NONE, NORMAL);
Ben Murdoch8b112d22011-06-08 16:22:53 +010011239 Heap* heap = GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +000011240 while (undefs > 0) {
Steve Block1e0659c2011-05-24 12:43:12 +010011241 if (pos > static_cast<uint32_t>(Smi::kMaxValue)) {
11242 // Adding an entry with the key beyond smi-range requires
11243 // allocation. Bailout.
11244 return Smi::FromInt(-1);
11245 }
Steve Block44f0eee2011-05-26 01:26:41 +010011246 new_dict->AddNumberEntry(pos, heap->undefined_value(), no_details)->
John Reck59135872010-11-02 12:39:01 -070011247 ToObjectUnchecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000011248 pos++;
11249 undefs--;
11250 }
11251
11252 set_elements(new_dict);
11253
11254 if (result <= static_cast<uint32_t>(Smi::kMaxValue)) {
11255 return Smi::FromInt(static_cast<int>(result));
11256 }
11257
11258 ASSERT_NE(NULL, result_double);
11259 result_double->set_value(static_cast<double>(result));
11260 return result_double;
11261}
11262
11263
11264// Collects all defined (non-hole) and non-undefined (array) elements at
11265// the start of the elements array.
11266// If the object is in dictionary mode, it is converted to fast elements
11267// mode.
John Reck59135872010-11-02 12:39:01 -070011268MaybeObject* JSObject::PrepareElementsForSort(uint32_t limit) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010011269 Heap* heap = GetHeap();
11270
Steve Blocka7e24c12009-10-30 11:49:00 +000011271 if (HasDictionaryElements()) {
11272 // Convert to fast elements containing only the existing properties.
11273 // Ordering is irrelevant, since we are going to sort anyway.
Ben Murdochc7cc0282012-03-05 14:35:55 +000011274 SeededNumberDictionary* dict = element_dictionary();
Steve Blocka7e24c12009-10-30 11:49:00 +000011275 if (IsJSArray() || dict->requires_slow_elements() ||
11276 dict->max_number_key() >= limit) {
11277 return PrepareSlowElementsForSort(limit);
11278 }
11279 // Convert to fast elements.
11280
John Reck59135872010-11-02 12:39:01 -070011281 Object* obj;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011282 { MaybeObject* maybe_obj = GetElementsTransitionMap(GetIsolate(),
11283 FAST_ELEMENTS);
John Reck59135872010-11-02 12:39:01 -070011284 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11285 }
Steve Block8defd9f2010-07-08 12:39:36 +010011286 Map* new_map = Map::cast(obj);
11287
Steve Block44f0eee2011-05-26 01:26:41 +010011288 PretenureFlag tenure = heap->InNewSpace(this) ? NOT_TENURED: TENURED;
John Reck59135872010-11-02 12:39:01 -070011289 Object* new_array;
11290 { MaybeObject* maybe_new_array =
Steve Block44f0eee2011-05-26 01:26:41 +010011291 heap->AllocateFixedArray(dict->NumberOfElements(), tenure);
John Reck59135872010-11-02 12:39:01 -070011292 if (!maybe_new_array->ToObject(&new_array)) return maybe_new_array;
11293 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011294 FixedArray* fast_elements = FixedArray::cast(new_array);
11295 dict->CopyValuesTo(fast_elements);
Steve Block8defd9f2010-07-08 12:39:36 +010011296
11297 set_map(new_map);
Steve Blocka7e24c12009-10-30 11:49:00 +000011298 set_elements(fast_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011299 } else if (HasExternalArrayElements()) {
11300 // External arrays cannot have holes or undefined elements.
11301 return Smi::FromInt(ExternalArray::cast(elements())->length());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011302 } else if (!HasFastDoubleElements()) {
John Reck59135872010-11-02 12:39:01 -070011303 Object* obj;
11304 { MaybeObject* maybe_obj = EnsureWritableFastElements();
11305 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11306 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011307 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011308 ASSERT(HasFastTypeElements() || HasFastDoubleElements());
Steve Blocka7e24c12009-10-30 11:49:00 +000011309
11310 // Collect holes at the end, undefined before that and the rest at the
11311 // start, and return the number of non-hole, non-undefined values.
11312
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011313 FixedArrayBase* elements_base = FixedArrayBase::cast(this->elements());
11314 uint32_t elements_length = static_cast<uint32_t>(elements_base->length());
Steve Blocka7e24c12009-10-30 11:49:00 +000011315 if (limit > elements_length) {
11316 limit = elements_length ;
11317 }
11318 if (limit == 0) {
11319 return Smi::FromInt(0);
11320 }
11321
11322 HeapNumber* result_double = NULL;
11323 if (limit > static_cast<uint32_t>(Smi::kMaxValue)) {
11324 // Pessimistically allocate space for return value before
11325 // we start mutating the array.
John Reck59135872010-11-02 12:39:01 -070011326 Object* new_double;
Steve Block44f0eee2011-05-26 01:26:41 +010011327 { MaybeObject* maybe_new_double = heap->AllocateHeapNumber(0.0);
John Reck59135872010-11-02 12:39:01 -070011328 if (!maybe_new_double->ToObject(&new_double)) return maybe_new_double;
11329 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011330 result_double = HeapNumber::cast(new_double);
11331 }
11332
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011333 uint32_t result = 0;
11334 if (elements_base->map() == heap->fixed_double_array_map()) {
11335 FixedDoubleArray* elements = FixedDoubleArray::cast(elements_base);
11336 // Split elements into defined and the_hole, in that order.
11337 unsigned int holes = limit;
11338 // Assume most arrays contain no holes and undefined values, so minimize the
11339 // number of stores of non-undefined, non-the-hole values.
11340 for (unsigned int i = 0; i < holes; i++) {
11341 if (elements->is_the_hole(i)) {
11342 holes--;
11343 } else {
11344 continue;
11345 }
11346 // Position i needs to be filled.
11347 while (holes > i) {
11348 if (elements->is_the_hole(holes)) {
11349 holes--;
11350 } else {
11351 elements->set(i, elements->get_scalar(holes));
11352 break;
11353 }
11354 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011355 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011356 result = holes;
11357 while (holes < limit) {
11358 elements->set_the_hole(holes);
11359 holes++;
11360 }
11361 } else {
11362 FixedArray* elements = FixedArray::cast(elements_base);
11363 AssertNoAllocation no_alloc;
11364
11365 // Split elements into defined, undefined and the_hole, in that order. Only
11366 // count locations for undefined and the hole, and fill them afterwards.
11367 WriteBarrierMode write_barrier = elements->GetWriteBarrierMode(no_alloc);
11368 unsigned int undefs = limit;
11369 unsigned int holes = limit;
11370 // Assume most arrays contain no holes and undefined values, so minimize the
11371 // number of stores of non-undefined, non-the-hole values.
11372 for (unsigned int i = 0; i < undefs; i++) {
11373 Object* current = elements->get(i);
Steve Blocka7e24c12009-10-30 11:49:00 +000011374 if (current->IsTheHole()) {
11375 holes--;
11376 undefs--;
11377 } else if (current->IsUndefined()) {
11378 undefs--;
11379 } else {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011380 continue;
11381 }
11382 // Position i needs to be filled.
11383 while (undefs > i) {
11384 current = elements->get(undefs);
11385 if (current->IsTheHole()) {
11386 holes--;
11387 undefs--;
11388 } else if (current->IsUndefined()) {
11389 undefs--;
11390 } else {
11391 elements->set(i, current, write_barrier);
11392 break;
11393 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011394 }
11395 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000011396 result = undefs;
11397 while (undefs < holes) {
11398 elements->set_undefined(undefs);
11399 undefs++;
11400 }
11401 while (holes < limit) {
11402 elements->set_the_hole(holes);
11403 holes++;
11404 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011405 }
11406
11407 if (result <= static_cast<uint32_t>(Smi::kMaxValue)) {
11408 return Smi::FromInt(static_cast<int>(result));
11409 }
11410 ASSERT_NE(NULL, result_double);
11411 result_double->set_value(static_cast<double>(result));
11412 return result_double;
11413}
11414
11415
Steve Block44f0eee2011-05-26 01:26:41 +010011416Object* ExternalPixelArray::SetValue(uint32_t index, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011417 uint8_t clamped_value = 0;
11418 if (index < static_cast<uint32_t>(length())) {
11419 if (value->IsSmi()) {
11420 int int_value = Smi::cast(value)->value();
11421 if (int_value < 0) {
11422 clamped_value = 0;
11423 } else if (int_value > 255) {
11424 clamped_value = 255;
11425 } else {
11426 clamped_value = static_cast<uint8_t>(int_value);
11427 }
11428 } else if (value->IsHeapNumber()) {
11429 double double_value = HeapNumber::cast(value)->value();
11430 if (!(double_value > 0)) {
11431 // NaN and less than zero clamp to zero.
11432 clamped_value = 0;
11433 } else if (double_value > 255) {
11434 // Greater than 255 clamp to 255.
11435 clamped_value = 255;
11436 } else {
11437 // Other doubles are rounded to the nearest integer.
11438 clamped_value = static_cast<uint8_t>(double_value + 0.5);
11439 }
11440 } else {
11441 // Clamp undefined to zero (default). All other types have been
11442 // converted to a number type further up in the call chain.
11443 ASSERT(value->IsUndefined());
11444 }
11445 set(index, clamped_value);
11446 }
11447 return Smi::FromInt(clamped_value);
11448}
11449
11450
Steve Block3ce2e202009-11-05 08:53:23 +000011451template<typename ExternalArrayClass, typename ValueType>
Steve Block44f0eee2011-05-26 01:26:41 +010011452static MaybeObject* ExternalArrayIntSetter(Heap* heap,
11453 ExternalArrayClass* receiver,
John Reck59135872010-11-02 12:39:01 -070011454 uint32_t index,
11455 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011456 ValueType cast_value = 0;
11457 if (index < static_cast<uint32_t>(receiver->length())) {
11458 if (value->IsSmi()) {
11459 int int_value = Smi::cast(value)->value();
11460 cast_value = static_cast<ValueType>(int_value);
11461 } else if (value->IsHeapNumber()) {
11462 double double_value = HeapNumber::cast(value)->value();
11463 cast_value = static_cast<ValueType>(DoubleToInt32(double_value));
11464 } else {
11465 // Clamp undefined to zero (default). All other types have been
11466 // converted to a number type further up in the call chain.
11467 ASSERT(value->IsUndefined());
11468 }
11469 receiver->set(index, cast_value);
11470 }
Steve Block44f0eee2011-05-26 01:26:41 +010011471 return heap->NumberFromInt32(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +000011472}
11473
11474
John Reck59135872010-11-02 12:39:01 -070011475MaybeObject* ExternalByteArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011476 return ExternalArrayIntSetter<ExternalByteArray, int8_t>
Steve Block44f0eee2011-05-26 01:26:41 +010011477 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +000011478}
11479
11480
John Reck59135872010-11-02 12:39:01 -070011481MaybeObject* ExternalUnsignedByteArray::SetValue(uint32_t index,
11482 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011483 return ExternalArrayIntSetter<ExternalUnsignedByteArray, uint8_t>
Steve Block44f0eee2011-05-26 01:26:41 +010011484 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +000011485}
11486
11487
John Reck59135872010-11-02 12:39:01 -070011488MaybeObject* ExternalShortArray::SetValue(uint32_t index,
11489 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011490 return ExternalArrayIntSetter<ExternalShortArray, int16_t>
Steve Block44f0eee2011-05-26 01:26:41 +010011491 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +000011492}
11493
11494
John Reck59135872010-11-02 12:39:01 -070011495MaybeObject* ExternalUnsignedShortArray::SetValue(uint32_t index,
11496 Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011497 return ExternalArrayIntSetter<ExternalUnsignedShortArray, uint16_t>
Steve Block44f0eee2011-05-26 01:26:41 +010011498 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +000011499}
11500
11501
John Reck59135872010-11-02 12:39:01 -070011502MaybeObject* ExternalIntArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011503 return ExternalArrayIntSetter<ExternalIntArray, int32_t>
Steve Block44f0eee2011-05-26 01:26:41 +010011504 (GetHeap(), this, index, value);
Steve Block3ce2e202009-11-05 08:53:23 +000011505}
11506
11507
John Reck59135872010-11-02 12:39:01 -070011508MaybeObject* ExternalUnsignedIntArray::SetValue(uint32_t index, Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +000011509 uint32_t cast_value = 0;
Steve Block44f0eee2011-05-26 01:26:41 +010011510 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +000011511 if (index < static_cast<uint32_t>(length())) {
11512 if (value->IsSmi()) {
11513 int int_value = Smi::cast(value)->value();
11514 cast_value = static_cast<uint32_t>(int_value);
11515 } else if (value->IsHeapNumber()) {
11516 double double_value = HeapNumber::cast(value)->value();
11517 cast_value = static_cast<uint32_t>(DoubleToUint32(double_value));
11518 } else {
11519 // Clamp undefined to zero (default). All other types have been
11520 // converted to a number type further up in the call chain.
11521 ASSERT(value->IsUndefined());
11522 }
11523 set(index, cast_value);
11524 }
Steve Block44f0eee2011-05-26 01:26:41 +010011525 return heap->NumberFromUint32(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +000011526}
11527
11528
John Reck59135872010-11-02 12:39:01 -070011529MaybeObject* ExternalFloatArray::SetValue(uint32_t index, Object* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011530 float cast_value = static_cast<float>(OS::nan_value());
Steve Block44f0eee2011-05-26 01:26:41 +010011531 Heap* heap = GetHeap();
Steve Block3ce2e202009-11-05 08:53:23 +000011532 if (index < static_cast<uint32_t>(length())) {
11533 if (value->IsSmi()) {
11534 int int_value = Smi::cast(value)->value();
11535 cast_value = static_cast<float>(int_value);
11536 } else if (value->IsHeapNumber()) {
11537 double double_value = HeapNumber::cast(value)->value();
11538 cast_value = static_cast<float>(double_value);
11539 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011540 // Clamp undefined to NaN (default). All other types have been
Steve Block3ce2e202009-11-05 08:53:23 +000011541 // converted to a number type further up in the call chain.
11542 ASSERT(value->IsUndefined());
11543 }
11544 set(index, cast_value);
11545 }
Steve Block44f0eee2011-05-26 01:26:41 +010011546 return heap->AllocateHeapNumber(cast_value);
Steve Block3ce2e202009-11-05 08:53:23 +000011547}
11548
11549
Ben Murdoch257744e2011-11-30 15:57:28 +000011550MaybeObject* ExternalDoubleArray::SetValue(uint32_t index, Object* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011551 double double_value = OS::nan_value();
Ben Murdoch257744e2011-11-30 15:57:28 +000011552 Heap* heap = GetHeap();
11553 if (index < static_cast<uint32_t>(length())) {
11554 if (value->IsSmi()) {
11555 int int_value = Smi::cast(value)->value();
11556 double_value = static_cast<double>(int_value);
11557 } else if (value->IsHeapNumber()) {
11558 double_value = HeapNumber::cast(value)->value();
11559 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011560 // Clamp undefined to NaN (default). All other types have been
Ben Murdoch257744e2011-11-30 15:57:28 +000011561 // converted to a number type further up in the call chain.
11562 ASSERT(value->IsUndefined());
11563 }
11564 set(index, double_value);
11565 }
11566 return heap->AllocateHeapNumber(double_value);
11567}
11568
11569
Ben Murdochb0fe1622011-05-05 13:52:32 +010011570JSGlobalPropertyCell* GlobalObject::GetPropertyCell(LookupResult* result) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011571 ASSERT(!HasFastProperties());
11572 Object* value = property_dictionary()->ValueAt(result->GetDictionaryEntry());
Ben Murdochb0fe1622011-05-05 13:52:32 +010011573 return JSGlobalPropertyCell::cast(value);
Steve Blocka7e24c12009-10-30 11:49:00 +000011574}
11575
11576
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011577Handle<JSGlobalPropertyCell> GlobalObject::EnsurePropertyCell(
11578 Handle<GlobalObject> global,
11579 Handle<String> name) {
11580 Isolate* isolate = global->GetIsolate();
11581 CALL_HEAP_FUNCTION(isolate,
11582 global->EnsurePropertyCell(*name),
11583 JSGlobalPropertyCell);
11584}
11585
11586
John Reck59135872010-11-02 12:39:01 -070011587MaybeObject* GlobalObject::EnsurePropertyCell(String* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011588 ASSERT(!HasFastProperties());
11589 int entry = property_dictionary()->FindEntry(name);
11590 if (entry == StringDictionary::kNotFound) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010011591 Heap* heap = GetHeap();
John Reck59135872010-11-02 12:39:01 -070011592 Object* cell;
11593 { MaybeObject* maybe_cell =
Steve Block44f0eee2011-05-26 01:26:41 +010011594 heap->AllocateJSGlobalPropertyCell(heap->the_hole_value());
John Reck59135872010-11-02 12:39:01 -070011595 if (!maybe_cell->ToObject(&cell)) return maybe_cell;
11596 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011597 PropertyDetails details(NONE, NORMAL);
11598 details = details.AsDeleted();
John Reck59135872010-11-02 12:39:01 -070011599 Object* dictionary;
11600 { MaybeObject* maybe_dictionary =
11601 property_dictionary()->Add(name, cell, details);
11602 if (!maybe_dictionary->ToObject(&dictionary)) return maybe_dictionary;
11603 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011604 set_properties(StringDictionary::cast(dictionary));
11605 return cell;
11606 } else {
11607 Object* value = property_dictionary()->ValueAt(entry);
11608 ASSERT(value->IsJSGlobalPropertyCell());
11609 return value;
11610 }
11611}
11612
11613
John Reck59135872010-11-02 12:39:01 -070011614MaybeObject* SymbolTable::LookupString(String* string, Object** s) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011615 SymbolKey key(string);
11616 return LookupKey(&key, s);
11617}
11618
11619
Steve Blockd0582a62009-12-15 09:54:21 +000011620// This class is used for looking up two character strings in the symbol table.
11621// If we don't have a hit we don't want to waste much time so we unroll the
11622// string hash calculation loop here for speed. Doesn't work if the two
11623// characters form a decimal integer, since such strings have a different hash
11624// algorithm.
11625class TwoCharHashTableKey : public HashTableKey {
11626 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +000011627 TwoCharHashTableKey(uint32_t c1, uint32_t c2, uint32_t seed)
Steve Blockd0582a62009-12-15 09:54:21 +000011628 : c1_(c1), c2_(c2) {
11629 // Char 1.
Ben Murdochc7cc0282012-03-05 14:35:55 +000011630 uint32_t hash = seed;
11631 hash += c1;
11632 hash += hash << 10;
Steve Blockd0582a62009-12-15 09:54:21 +000011633 hash ^= hash >> 6;
11634 // Char 2.
11635 hash += c2;
11636 hash += hash << 10;
11637 hash ^= hash >> 6;
11638 // GetHash.
11639 hash += hash << 3;
11640 hash ^= hash >> 11;
11641 hash += hash << 15;
Ben Murdochc7cc0282012-03-05 14:35:55 +000011642 if ((hash & String::kHashBitMask) == 0) hash = String::kZeroHash;
Steve Blockd0582a62009-12-15 09:54:21 +000011643#ifdef DEBUG
Ben Murdochc7cc0282012-03-05 14:35:55 +000011644 StringHasher hasher(2, seed);
Steve Blockd0582a62009-12-15 09:54:21 +000011645 hasher.AddCharacter(c1);
11646 hasher.AddCharacter(c2);
11647 // If this assert fails then we failed to reproduce the two-character
11648 // version of the string hashing algorithm above. One reason could be
11649 // that we were passed two digits as characters, since the hash
11650 // algorithm is different in that case.
11651 ASSERT_EQ(static_cast<int>(hasher.GetHash()), static_cast<int>(hash));
11652#endif
11653 hash_ = hash;
11654 }
11655
11656 bool IsMatch(Object* o) {
11657 if (!o->IsString()) return false;
11658 String* other = String::cast(o);
11659 if (other->length() != 2) return false;
11660 if (other->Get(0) != c1_) return false;
11661 return other->Get(1) == c2_;
11662 }
11663
11664 uint32_t Hash() { return hash_; }
11665 uint32_t HashForObject(Object* key) {
11666 if (!key->IsString()) return 0;
11667 return String::cast(key)->Hash();
11668 }
11669
11670 Object* AsObject() {
11671 // The TwoCharHashTableKey is only used for looking in the symbol
11672 // table, not for adding to it.
11673 UNREACHABLE();
11674 return NULL;
11675 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011676
Steve Blockd0582a62009-12-15 09:54:21 +000011677 private:
11678 uint32_t c1_;
11679 uint32_t c2_;
11680 uint32_t hash_;
11681};
11682
11683
Steve Blocka7e24c12009-10-30 11:49:00 +000011684bool SymbolTable::LookupSymbolIfExists(String* string, String** symbol) {
11685 SymbolKey key(string);
11686 int entry = FindEntry(&key);
11687 if (entry == kNotFound) {
11688 return false;
11689 } else {
11690 String* result = String::cast(KeyAt(entry));
11691 ASSERT(StringShape(result).IsSymbol());
11692 *symbol = result;
11693 return true;
11694 }
11695}
11696
11697
Steve Blockd0582a62009-12-15 09:54:21 +000011698bool SymbolTable::LookupTwoCharsSymbolIfExists(uint32_t c1,
11699 uint32_t c2,
11700 String** symbol) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000011701 TwoCharHashTableKey key(c1, c2, GetHeap()->HashSeed());
Steve Blockd0582a62009-12-15 09:54:21 +000011702 int entry = FindEntry(&key);
11703 if (entry == kNotFound) {
11704 return false;
11705 } else {
11706 String* result = String::cast(KeyAt(entry));
11707 ASSERT(StringShape(result).IsSymbol());
11708 *symbol = result;
11709 return true;
11710 }
11711}
11712
11713
Ben Murdochc7cc0282012-03-05 14:35:55 +000011714MaybeObject* SymbolTable::LookupSymbol(Vector<const char> str,
11715 Object** s) {
11716 Utf8SymbolKey key(str, GetHeap()->HashSeed());
Steve Blocka7e24c12009-10-30 11:49:00 +000011717 return LookupKey(&key, s);
11718}
11719
11720
Steve Block9fac8402011-05-12 15:51:54 +010011721MaybeObject* SymbolTable::LookupAsciiSymbol(Vector<const char> str,
11722 Object** s) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000011723 AsciiSymbolKey key(str, GetHeap()->HashSeed());
Steve Block9fac8402011-05-12 15:51:54 +010011724 return LookupKey(&key, s);
11725}
11726
11727
Ben Murdoch257744e2011-11-30 15:57:28 +000011728MaybeObject* SymbolTable::LookupSubStringAsciiSymbol(Handle<SeqAsciiString> str,
11729 int from,
11730 int length,
11731 Object** s) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000011732 SubStringAsciiSymbolKey key(str, from, length, GetHeap()->HashSeed());
Ben Murdoch257744e2011-11-30 15:57:28 +000011733 return LookupKey(&key, s);
11734}
11735
11736
Steve Block9fac8402011-05-12 15:51:54 +010011737MaybeObject* SymbolTable::LookupTwoByteSymbol(Vector<const uc16> str,
11738 Object** s) {
Ben Murdochc7cc0282012-03-05 14:35:55 +000011739 TwoByteSymbolKey key(str, GetHeap()->HashSeed());
Steve Block9fac8402011-05-12 15:51:54 +010011740 return LookupKey(&key, s);
11741}
11742
John Reck59135872010-11-02 12:39:01 -070011743MaybeObject* SymbolTable::LookupKey(HashTableKey* key, Object** s) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011744 int entry = FindEntry(key);
11745
11746 // Symbol already in table.
11747 if (entry != kNotFound) {
11748 *s = KeyAt(entry);
11749 return this;
11750 }
11751
11752 // Adding new symbol. Grow table if needed.
John Reck59135872010-11-02 12:39:01 -070011753 Object* obj;
11754 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
11755 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11756 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011757
11758 // Create symbol object.
John Reck59135872010-11-02 12:39:01 -070011759 Object* symbol;
11760 { MaybeObject* maybe_symbol = key->AsObject();
11761 if (!maybe_symbol->ToObject(&symbol)) return maybe_symbol;
11762 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011763
11764 // If the symbol table grew as part of EnsureCapacity, obj is not
11765 // the current symbol table and therefore we cannot use
11766 // SymbolTable::cast here.
11767 SymbolTable* table = reinterpret_cast<SymbolTable*>(obj);
11768
11769 // Add the new symbol and return it along with the symbol table.
11770 entry = table->FindInsertionEntry(key->Hash());
11771 table->set(EntryToIndex(entry), symbol);
11772 table->ElementAdded();
11773 *s = symbol;
11774 return table;
11775}
11776
11777
11778Object* CompilationCacheTable::Lookup(String* src) {
11779 StringKey key(src);
11780 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +010011781 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000011782 return get(EntryToIndex(entry) + 1);
11783}
11784
11785
Steve Block1e0659c2011-05-24 12:43:12 +010011786Object* CompilationCacheTable::LookupEval(String* src,
11787 Context* context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011788 LanguageMode language_mode,
11789 int scope_position) {
11790 StringSharedKey key(src,
11791 context->closure()->shared(),
11792 language_mode,
11793 scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +000011794 int entry = FindEntry(&key);
Steve Block44f0eee2011-05-26 01:26:41 +010011795 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000011796 return get(EntryToIndex(entry) + 1);
11797}
11798
11799
11800Object* CompilationCacheTable::LookupRegExp(String* src,
11801 JSRegExp::Flags flags) {
11802 RegExpKey key(src, flags);
11803 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +010011804 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000011805 return get(EntryToIndex(entry) + 1);
11806}
11807
11808
John Reck59135872010-11-02 12:39:01 -070011809MaybeObject* CompilationCacheTable::Put(String* src, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011810 StringKey key(src);
John Reck59135872010-11-02 12:39:01 -070011811 Object* obj;
11812 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
11813 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11814 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011815
11816 CompilationCacheTable* cache =
11817 reinterpret_cast<CompilationCacheTable*>(obj);
11818 int entry = cache->FindInsertionEntry(key.Hash());
11819 cache->set(EntryToIndex(entry), src);
11820 cache->set(EntryToIndex(entry) + 1, value);
11821 cache->ElementAdded();
11822 return cache;
11823}
11824
11825
John Reck59135872010-11-02 12:39:01 -070011826MaybeObject* CompilationCacheTable::PutEval(String* src,
11827 Context* context,
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011828 SharedFunctionInfo* value,
11829 int scope_position) {
Steve Block1e0659c2011-05-24 12:43:12 +010011830 StringSharedKey key(src,
11831 context->closure()->shared(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011832 value->language_mode(),
11833 scope_position);
John Reck59135872010-11-02 12:39:01 -070011834 Object* obj;
11835 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
11836 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11837 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011838
11839 CompilationCacheTable* cache =
11840 reinterpret_cast<CompilationCacheTable*>(obj);
11841 int entry = cache->FindInsertionEntry(key.Hash());
11842
John Reck59135872010-11-02 12:39:01 -070011843 Object* k;
11844 { MaybeObject* maybe_k = key.AsObject();
11845 if (!maybe_k->ToObject(&k)) return maybe_k;
11846 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011847
11848 cache->set(EntryToIndex(entry), k);
11849 cache->set(EntryToIndex(entry) + 1, value);
11850 cache->ElementAdded();
11851 return cache;
11852}
11853
11854
John Reck59135872010-11-02 12:39:01 -070011855MaybeObject* CompilationCacheTable::PutRegExp(String* src,
11856 JSRegExp::Flags flags,
11857 FixedArray* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011858 RegExpKey key(src, flags);
John Reck59135872010-11-02 12:39:01 -070011859 Object* obj;
11860 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
11861 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11862 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011863
11864 CompilationCacheTable* cache =
11865 reinterpret_cast<CompilationCacheTable*>(obj);
11866 int entry = cache->FindInsertionEntry(key.Hash());
Steve Block3ce2e202009-11-05 08:53:23 +000011867 // We store the value in the key slot, and compare the search key
11868 // to the stored value with a custon IsMatch function during lookups.
Steve Blocka7e24c12009-10-30 11:49:00 +000011869 cache->set(EntryToIndex(entry), value);
11870 cache->set(EntryToIndex(entry) + 1, value);
11871 cache->ElementAdded();
11872 return cache;
11873}
11874
11875
Ben Murdochb0fe1622011-05-05 13:52:32 +010011876void CompilationCacheTable::Remove(Object* value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011877 Object* the_hole_value = GetHeap()->the_hole_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +010011878 for (int entry = 0, size = Capacity(); entry < size; entry++) {
11879 int entry_index = EntryToIndex(entry);
11880 int value_index = entry_index + 1;
11881 if (get(value_index) == value) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010011882 NoWriteBarrierSet(this, entry_index, the_hole_value);
11883 NoWriteBarrierSet(this, value_index, the_hole_value);
Ben Murdochb0fe1622011-05-05 13:52:32 +010011884 ElementRemoved();
11885 }
11886 }
11887 return;
11888}
11889
11890
Steve Blocka7e24c12009-10-30 11:49:00 +000011891// SymbolsKey used for HashTable where key is array of symbols.
11892class SymbolsKey : public HashTableKey {
11893 public:
11894 explicit SymbolsKey(FixedArray* symbols) : symbols_(symbols) { }
11895
11896 bool IsMatch(Object* symbols) {
11897 FixedArray* o = FixedArray::cast(symbols);
11898 int len = symbols_->length();
11899 if (o->length() != len) return false;
11900 for (int i = 0; i < len; i++) {
11901 if (o->get(i) != symbols_->get(i)) return false;
11902 }
11903 return true;
11904 }
11905
11906 uint32_t Hash() { return HashForObject(symbols_); }
11907
11908 uint32_t HashForObject(Object* obj) {
11909 FixedArray* symbols = FixedArray::cast(obj);
11910 int len = symbols->length();
11911 uint32_t hash = 0;
11912 for (int i = 0; i < len; i++) {
11913 hash ^= String::cast(symbols->get(i))->Hash();
11914 }
11915 return hash;
11916 }
11917
11918 Object* AsObject() { return symbols_; }
11919
11920 private:
11921 FixedArray* symbols_;
11922};
11923
11924
11925Object* MapCache::Lookup(FixedArray* array) {
11926 SymbolsKey key(array);
11927 int entry = FindEntry(&key);
Ben Murdoch8b112d22011-06-08 16:22:53 +010011928 if (entry == kNotFound) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000011929 return get(EntryToIndex(entry) + 1);
11930}
11931
11932
John Reck59135872010-11-02 12:39:01 -070011933MaybeObject* MapCache::Put(FixedArray* array, Map* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000011934 SymbolsKey key(array);
John Reck59135872010-11-02 12:39:01 -070011935 Object* obj;
11936 { MaybeObject* maybe_obj = EnsureCapacity(1, &key);
11937 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11938 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011939
11940 MapCache* cache = reinterpret_cast<MapCache*>(obj);
11941 int entry = cache->FindInsertionEntry(key.Hash());
11942 cache->set(EntryToIndex(entry), array);
11943 cache->set(EntryToIndex(entry) + 1, value);
11944 cache->ElementAdded();
11945 return cache;
11946}
11947
11948
11949template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070011950MaybeObject* Dictionary<Shape, Key>::Allocate(int at_least_space_for) {
11951 Object* obj;
11952 { MaybeObject* maybe_obj =
11953 HashTable<Shape, Key>::Allocate(at_least_space_for);
11954 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
Steve Blocka7e24c12009-10-30 11:49:00 +000011955 }
John Reck59135872010-11-02 12:39:01 -070011956 // Initialize the next enumeration index.
11957 Dictionary<Shape, Key>::cast(obj)->
11958 SetNextEnumerationIndex(PropertyDetails::kInitialIndex);
Steve Blocka7e24c12009-10-30 11:49:00 +000011959 return obj;
11960}
11961
11962
11963template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070011964MaybeObject* Dictionary<Shape, Key>::GenerateNewEnumerationIndices() {
Steve Block44f0eee2011-05-26 01:26:41 +010011965 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +000011966 int length = HashTable<Shape, Key>::NumberOfElements();
11967
11968 // Allocate and initialize iteration order array.
John Reck59135872010-11-02 12:39:01 -070011969 Object* obj;
Steve Block44f0eee2011-05-26 01:26:41 +010011970 { MaybeObject* maybe_obj = heap->AllocateFixedArray(length);
John Reck59135872010-11-02 12:39:01 -070011971 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11972 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011973 FixedArray* iteration_order = FixedArray::cast(obj);
11974 for (int i = 0; i < length; i++) {
Leon Clarke4515c472010-02-03 11:58:03 +000011975 iteration_order->set(i, Smi::FromInt(i));
Steve Blocka7e24c12009-10-30 11:49:00 +000011976 }
11977
11978 // Allocate array with enumeration order.
Steve Block44f0eee2011-05-26 01:26:41 +010011979 { MaybeObject* maybe_obj = heap->AllocateFixedArray(length);
John Reck59135872010-11-02 12:39:01 -070011980 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
11981 }
Steve Blocka7e24c12009-10-30 11:49:00 +000011982 FixedArray* enumeration_order = FixedArray::cast(obj);
11983
11984 // Fill the enumeration order array with property details.
11985 int capacity = HashTable<Shape, Key>::Capacity();
11986 int pos = 0;
11987 for (int i = 0; i < capacity; i++) {
11988 if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) {
Leon Clarke4515c472010-02-03 11:58:03 +000011989 enumeration_order->set(pos++, Smi::FromInt(DetailsAt(i).index()));
Steve Blocka7e24c12009-10-30 11:49:00 +000011990 }
11991 }
11992
11993 // Sort the arrays wrt. enumeration order.
11994 iteration_order->SortPairs(enumeration_order, enumeration_order->length());
11995
11996 // Overwrite the enumeration_order with the enumeration indices.
11997 for (int i = 0; i < length; i++) {
11998 int index = Smi::cast(iteration_order->get(i))->value();
11999 int enum_index = PropertyDetails::kInitialIndex + i;
Leon Clarke4515c472010-02-03 11:58:03 +000012000 enumeration_order->set(index, Smi::FromInt(enum_index));
Steve Blocka7e24c12009-10-30 11:49:00 +000012001 }
12002
12003 // Update the dictionary with new indices.
12004 capacity = HashTable<Shape, Key>::Capacity();
12005 pos = 0;
12006 for (int i = 0; i < capacity; i++) {
12007 if (Dictionary<Shape, Key>::IsKey(Dictionary<Shape, Key>::KeyAt(i))) {
12008 int enum_index = Smi::cast(enumeration_order->get(pos++))->value();
12009 PropertyDetails details = DetailsAt(i);
12010 PropertyDetails new_details =
12011 PropertyDetails(details.attributes(), details.type(), enum_index);
12012 DetailsAtPut(i, new_details);
12013 }
12014 }
12015
12016 // Set the next enumeration index.
12017 SetNextEnumerationIndex(PropertyDetails::kInitialIndex+length);
12018 return this;
12019}
12020
12021template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070012022MaybeObject* Dictionary<Shape, Key>::EnsureCapacity(int n, Key key) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012023 // Check whether there are enough enumeration indices to add n elements.
12024 if (Shape::kIsEnumerable &&
12025 !PropertyDetails::IsValidIndex(NextEnumerationIndex() + n)) {
12026 // If not, we generate new indices for the properties.
John Reck59135872010-11-02 12:39:01 -070012027 Object* result;
12028 { MaybeObject* maybe_result = GenerateNewEnumerationIndices();
12029 if (!maybe_result->ToObject(&result)) return maybe_result;
12030 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012031 }
12032 return HashTable<Shape, Key>::EnsureCapacity(n, key);
12033}
12034
12035
Steve Blocka7e24c12009-10-30 11:49:00 +000012036template<typename Shape, typename Key>
12037Object* Dictionary<Shape, Key>::DeleteProperty(int entry,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012038 JSReceiver::DeleteMode mode) {
Steve Block44f0eee2011-05-26 01:26:41 +010012039 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Blocka7e24c12009-10-30 11:49:00 +000012040 PropertyDetails details = DetailsAt(entry);
12041 // Ignore attributes if forcing a deletion.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012042 if (details.IsDontDelete() && mode != JSReceiver::FORCE_DELETION) {
Steve Block44f0eee2011-05-26 01:26:41 +010012043 return heap->false_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012044 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012045 SetEntry(entry, heap->the_hole_value(), heap->the_hole_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000012046 HashTable<Shape, Key>::ElementRemoved();
Steve Block44f0eee2011-05-26 01:26:41 +010012047 return heap->true_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012048}
12049
12050
12051template<typename Shape, typename Key>
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012052MaybeObject* Dictionary<Shape, Key>::Shrink(Key key) {
12053 return HashTable<Shape, Key>::Shrink(key);
12054}
12055
12056
12057template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070012058MaybeObject* Dictionary<Shape, Key>::AtPut(Key key, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010012059 int entry = this->FindEntry(key);
Steve Blocka7e24c12009-10-30 11:49:00 +000012060
12061 // If the entry is present set the value;
12062 if (entry != Dictionary<Shape, Key>::kNotFound) {
12063 ValueAtPut(entry, value);
12064 return this;
12065 }
12066
12067 // Check whether the dictionary should be extended.
John Reck59135872010-11-02 12:39:01 -070012068 Object* obj;
12069 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
12070 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
12071 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012072
John Reck59135872010-11-02 12:39:01 -070012073 Object* k;
12074 { MaybeObject* maybe_k = Shape::AsObject(key);
12075 if (!maybe_k->ToObject(&k)) return maybe_k;
12076 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012077 PropertyDetails details = PropertyDetails(NONE, NORMAL);
Ben Murdochc7cc0282012-03-05 14:35:55 +000012078
12079 return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details,
12080 Dictionary<Shape, Key>::Hash(key));
Steve Blocka7e24c12009-10-30 11:49:00 +000012081}
12082
12083
12084template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070012085MaybeObject* Dictionary<Shape, Key>::Add(Key key,
12086 Object* value,
12087 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012088 // Valdate key is absent.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010012089 SLOW_ASSERT((this->FindEntry(key) == Dictionary<Shape, Key>::kNotFound));
Steve Blocka7e24c12009-10-30 11:49:00 +000012090 // Check whether the dictionary should be extended.
John Reck59135872010-11-02 12:39:01 -070012091 Object* obj;
12092 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
12093 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
12094 }
Ben Murdochc7cc0282012-03-05 14:35:55 +000012095
12096 return Dictionary<Shape, Key>::cast(obj)->AddEntry(key, value, details,
12097 Dictionary<Shape, Key>::Hash(key));
Steve Blocka7e24c12009-10-30 11:49:00 +000012098}
12099
12100
12101// Add a key, value pair to the dictionary.
12102template<typename Shape, typename Key>
John Reck59135872010-11-02 12:39:01 -070012103MaybeObject* Dictionary<Shape, Key>::AddEntry(Key key,
12104 Object* value,
12105 PropertyDetails details,
12106 uint32_t hash) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012107 // Compute the key object.
John Reck59135872010-11-02 12:39:01 -070012108 Object* k;
12109 { MaybeObject* maybe_k = Shape::AsObject(key);
12110 if (!maybe_k->ToObject(&k)) return maybe_k;
12111 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012112
12113 uint32_t entry = Dictionary<Shape, Key>::FindInsertionEntry(hash);
12114 // Insert element at empty or deleted entry
12115 if (!details.IsDeleted() && details.index() == 0 && Shape::kIsEnumerable) {
12116 // Assign an enumeration index to the property and update
12117 // SetNextEnumerationIndex.
12118 int index = NextEnumerationIndex();
12119 details = PropertyDetails(details.attributes(), details.type(), index);
12120 SetNextEnumerationIndex(index + 1);
12121 }
12122 SetEntry(entry, k, value, details);
12123 ASSERT((Dictionary<Shape, Key>::KeyAt(entry)->IsNumber()
12124 || Dictionary<Shape, Key>::KeyAt(entry)->IsString()));
12125 HashTable<Shape, Key>::ElementAdded();
12126 return this;
12127}
12128
12129
Ben Murdochc7cc0282012-03-05 14:35:55 +000012130void SeededNumberDictionary::UpdateMaxNumberKey(uint32_t key) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012131 // If the dictionary requires slow elements an element has already
12132 // been added at a high index.
12133 if (requires_slow_elements()) return;
12134 // Check if this index is high enough that we should require slow
12135 // elements.
12136 if (key > kRequiresSlowElementsLimit) {
12137 set_requires_slow_elements();
12138 return;
12139 }
12140 // Update max key value.
12141 Object* max_index_object = get(kMaxNumberKeyIndex);
12142 if (!max_index_object->IsSmi() || max_number_key() < key) {
12143 FixedArray::set(kMaxNumberKeyIndex,
Leon Clarke4515c472010-02-03 11:58:03 +000012144 Smi::FromInt(key << kRequiresSlowElementsTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +000012145 }
12146}
12147
12148
Ben Murdochc7cc0282012-03-05 14:35:55 +000012149MaybeObject* SeededNumberDictionary::AddNumberEntry(uint32_t key,
12150 Object* value,
12151 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012152 UpdateMaxNumberKey(key);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010012153 SLOW_ASSERT(this->FindEntry(key) == kNotFound);
Steve Blocka7e24c12009-10-30 11:49:00 +000012154 return Add(key, value, details);
12155}
12156
12157
Ben Murdochc7cc0282012-03-05 14:35:55 +000012158MaybeObject* UnseededNumberDictionary::AddNumberEntry(uint32_t key,
12159 Object* value) {
12160 SLOW_ASSERT(this->FindEntry(key) == kNotFound);
12161 return Add(key, value, PropertyDetails(NONE, NORMAL));
12162}
12163
12164
12165MaybeObject* SeededNumberDictionary::AtNumberPut(uint32_t key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012166 UpdateMaxNumberKey(key);
12167 return AtPut(key, value);
12168}
12169
12170
Ben Murdochc7cc0282012-03-05 14:35:55 +000012171MaybeObject* UnseededNumberDictionary::AtNumberPut(uint32_t key,
12172 Object* value) {
12173 return AtPut(key, value);
12174}
12175
12176
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012177Handle<SeededNumberDictionary> SeededNumberDictionary::Set(
12178 Handle<SeededNumberDictionary> dictionary,
12179 uint32_t index,
12180 Handle<Object> value,
12181 PropertyDetails details) {
12182 CALL_HEAP_FUNCTION(dictionary->GetIsolate(),
12183 dictionary->Set(index, *value, details),
12184 SeededNumberDictionary);
12185}
12186
12187
12188Handle<UnseededNumberDictionary> UnseededNumberDictionary::Set(
12189 Handle<UnseededNumberDictionary> dictionary,
12190 uint32_t index,
12191 Handle<Object> value) {
12192 CALL_HEAP_FUNCTION(dictionary->GetIsolate(),
12193 dictionary->Set(index, *value),
12194 UnseededNumberDictionary);
12195}
12196
12197
Ben Murdochc7cc0282012-03-05 14:35:55 +000012198MaybeObject* SeededNumberDictionary::Set(uint32_t key,
12199 Object* value,
12200 PropertyDetails details) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012201 int entry = FindEntry(key);
12202 if (entry == kNotFound) return AddNumberEntry(key, value, details);
12203 // Preserve enumeration index.
12204 details = PropertyDetails(details.attributes(),
12205 details.type(),
12206 DetailsAt(entry).index());
Ben Murdochc7cc0282012-03-05 14:35:55 +000012207 MaybeObject* maybe_object_key = SeededNumberDictionaryShape::AsObject(key);
John Reck59135872010-11-02 12:39:01 -070012208 Object* object_key;
12209 if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key;
Ben Murdochf87a2032010-10-22 12:50:53 +010012210 SetEntry(entry, object_key, value, details);
Steve Blocka7e24c12009-10-30 11:49:00 +000012211 return this;
12212}
12213
12214
Ben Murdochc7cc0282012-03-05 14:35:55 +000012215MaybeObject* UnseededNumberDictionary::Set(uint32_t key,
12216 Object* value) {
12217 int entry = FindEntry(key);
12218 if (entry == kNotFound) return AddNumberEntry(key, value);
12219 MaybeObject* maybe_object_key = UnseededNumberDictionaryShape::AsObject(key);
12220 Object* object_key;
12221 if (!maybe_object_key->ToObject(&object_key)) return maybe_object_key;
12222 SetEntry(entry, object_key, value);
12223 return this;
12224}
12225
12226
Steve Blocka7e24c12009-10-30 11:49:00 +000012227
12228template<typename Shape, typename Key>
12229int Dictionary<Shape, Key>::NumberOfElementsFilterAttributes(
12230 PropertyAttributes filter) {
12231 int capacity = HashTable<Shape, Key>::Capacity();
12232 int result = 0;
12233 for (int i = 0; i < capacity; i++) {
12234 Object* k = HashTable<Shape, Key>::KeyAt(i);
12235 if (HashTable<Shape, Key>::IsKey(k)) {
12236 PropertyDetails details = DetailsAt(i);
12237 if (details.IsDeleted()) continue;
12238 PropertyAttributes attr = details.attributes();
12239 if ((attr & filter) == 0) result++;
12240 }
12241 }
12242 return result;
12243}
12244
12245
12246template<typename Shape, typename Key>
12247int Dictionary<Shape, Key>::NumberOfEnumElements() {
12248 return NumberOfElementsFilterAttributes(
12249 static_cast<PropertyAttributes>(DONT_ENUM));
12250}
12251
12252
12253template<typename Shape, typename Key>
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012254void Dictionary<Shape, Key>::CopyKeysTo(
12255 FixedArray* storage,
12256 PropertyAttributes filter,
12257 typename Dictionary<Shape, Key>::SortMode sort_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012258 ASSERT(storage->length() >= NumberOfEnumElements());
12259 int capacity = HashTable<Shape, Key>::Capacity();
12260 int index = 0;
12261 for (int i = 0; i < capacity; i++) {
12262 Object* k = HashTable<Shape, Key>::KeyAt(i);
12263 if (HashTable<Shape, Key>::IsKey(k)) {
12264 PropertyDetails details = DetailsAt(i);
12265 if (details.IsDeleted()) continue;
12266 PropertyAttributes attr = details.attributes();
12267 if ((attr & filter) == 0) storage->set(index++, k);
12268 }
12269 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012270 if (sort_mode == Dictionary<Shape, Key>::SORTED) {
12271 storage->SortPairs(storage, index);
12272 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012273 ASSERT(storage->length() >= index);
12274}
12275
12276
12277void StringDictionary::CopyEnumKeysTo(FixedArray* storage,
12278 FixedArray* sort_array) {
12279 ASSERT(storage->length() >= NumberOfEnumElements());
12280 int capacity = Capacity();
12281 int index = 0;
12282 for (int i = 0; i < capacity; i++) {
12283 Object* k = KeyAt(i);
12284 if (IsKey(k)) {
12285 PropertyDetails details = DetailsAt(i);
12286 if (details.IsDeleted() || details.IsDontEnum()) continue;
12287 storage->set(index, k);
Leon Clarke4515c472010-02-03 11:58:03 +000012288 sort_array->set(index, Smi::FromInt(details.index()));
Steve Blocka7e24c12009-10-30 11:49:00 +000012289 index++;
12290 }
12291 }
12292 storage->SortPairs(sort_array, sort_array->length());
12293 ASSERT(storage->length() >= index);
12294}
12295
12296
12297template<typename Shape, typename Key>
Ben Murdoch6d7cb002011-08-04 19:25:22 +010012298void Dictionary<Shape, Key>::CopyKeysTo(
Ben Murdoch257744e2011-11-30 15:57:28 +000012299 FixedArray* storage,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012300 int index,
12301 typename Dictionary<Shape, Key>::SortMode sort_mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012302 ASSERT(storage->length() >= NumberOfElementsFilterAttributes(
12303 static_cast<PropertyAttributes>(NONE)));
12304 int capacity = HashTable<Shape, Key>::Capacity();
Steve Blocka7e24c12009-10-30 11:49:00 +000012305 for (int i = 0; i < capacity; i++) {
12306 Object* k = HashTable<Shape, Key>::KeyAt(i);
12307 if (HashTable<Shape, Key>::IsKey(k)) {
12308 PropertyDetails details = DetailsAt(i);
12309 if (details.IsDeleted()) continue;
12310 storage->set(index++, k);
12311 }
12312 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000012313 if (sort_mode == Dictionary<Shape, Key>::SORTED) {
12314 storage->SortPairs(storage, index);
12315 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012316 ASSERT(storage->length() >= index);
12317}
12318
12319
12320// Backwards lookup (slow).
12321template<typename Shape, typename Key>
12322Object* Dictionary<Shape, Key>::SlowReverseLookup(Object* value) {
12323 int capacity = HashTable<Shape, Key>::Capacity();
12324 for (int i = 0; i < capacity; i++) {
12325 Object* k = HashTable<Shape, Key>::KeyAt(i);
12326 if (Dictionary<Shape, Key>::IsKey(k)) {
12327 Object* e = ValueAt(i);
12328 if (e->IsJSGlobalPropertyCell()) {
12329 e = JSGlobalPropertyCell::cast(e)->value();
12330 }
12331 if (e == value) return k;
12332 }
12333 }
Ben Murdoch8b112d22011-06-08 16:22:53 +010012334 Heap* heap = Dictionary<Shape, Key>::GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +010012335 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012336}
12337
12338
John Reck59135872010-11-02 12:39:01 -070012339MaybeObject* StringDictionary::TransformPropertiesToFastFor(
Steve Blocka7e24c12009-10-30 11:49:00 +000012340 JSObject* obj, int unused_property_fields) {
12341 // Make sure we preserve dictionary representation if there are too many
12342 // descriptors.
12343 if (NumberOfElements() > DescriptorArray::kMaxNumberOfDescriptors) return obj;
12344
12345 // Figure out if it is necessary to generate new enumeration indices.
12346 int max_enumeration_index =
12347 NextEnumerationIndex() +
12348 (DescriptorArray::kMaxNumberOfDescriptors -
12349 NumberOfElements());
12350 if (!PropertyDetails::IsValidIndex(max_enumeration_index)) {
John Reck59135872010-11-02 12:39:01 -070012351 Object* result;
12352 { MaybeObject* maybe_result = GenerateNewEnumerationIndices();
12353 if (!maybe_result->ToObject(&result)) return maybe_result;
12354 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012355 }
12356
12357 int instance_descriptor_length = 0;
12358 int number_of_fields = 0;
12359
Ben Murdoch8b112d22011-06-08 16:22:53 +010012360 Heap* heap = GetHeap();
12361
Steve Blocka7e24c12009-10-30 11:49:00 +000012362 // Compute the length of the instance descriptor.
12363 int capacity = Capacity();
12364 for (int i = 0; i < capacity; i++) {
12365 Object* k = KeyAt(i);
12366 if (IsKey(k)) {
12367 Object* value = ValueAt(i);
12368 PropertyType type = DetailsAt(i).type();
12369 ASSERT(type != FIELD);
12370 instance_descriptor_length++;
Leon Clarkee46be812010-01-19 14:06:41 +000012371 if (type == NORMAL &&
Steve Block44f0eee2011-05-26 01:26:41 +010012372 (!value->IsJSFunction() || heap->InNewSpace(value))) {
Leon Clarkee46be812010-01-19 14:06:41 +000012373 number_of_fields += 1;
12374 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012375 }
12376 }
12377
12378 // Allocate the instance descriptor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012379 DescriptorArray* descriptors;
12380 { MaybeObject* maybe_descriptors =
John Reck59135872010-11-02 12:39:01 -070012381 DescriptorArray::Allocate(instance_descriptor_length);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012382 if (!maybe_descriptors->To<DescriptorArray>(&descriptors)) {
12383 return maybe_descriptors;
John Reck59135872010-11-02 12:39:01 -070012384 }
12385 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012386
12387 DescriptorArray::WhitenessWitness witness(descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +000012388
12389 int inobject_props = obj->map()->inobject_properties();
12390 int number_of_allocated_fields =
12391 number_of_fields + unused_property_fields - inobject_props;
Ben Murdochf87a2032010-10-22 12:50:53 +010012392 if (number_of_allocated_fields < 0) {
12393 // There is enough inobject space for all fields (including unused).
12394 number_of_allocated_fields = 0;
12395 unused_property_fields = inobject_props - number_of_fields;
12396 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012397
12398 // Allocate the fixed array for the fields.
John Reck59135872010-11-02 12:39:01 -070012399 Object* fields;
12400 { MaybeObject* maybe_fields =
Steve Block44f0eee2011-05-26 01:26:41 +010012401 heap->AllocateFixedArray(number_of_allocated_fields);
John Reck59135872010-11-02 12:39:01 -070012402 if (!maybe_fields->ToObject(&fields)) return maybe_fields;
12403 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012404
12405 // Fill in the instance descriptor and the fields.
12406 int next_descriptor = 0;
12407 int current_offset = 0;
12408 for (int i = 0; i < capacity; i++) {
12409 Object* k = KeyAt(i);
12410 if (IsKey(k)) {
12411 Object* value = ValueAt(i);
12412 // Ensure the key is a symbol before writing into the instance descriptor.
John Reck59135872010-11-02 12:39:01 -070012413 Object* key;
Steve Block44f0eee2011-05-26 01:26:41 +010012414 { MaybeObject* maybe_key = heap->LookupSymbol(String::cast(k));
John Reck59135872010-11-02 12:39:01 -070012415 if (!maybe_key->ToObject(&key)) return maybe_key;
12416 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012417 PropertyDetails details = DetailsAt(i);
12418 PropertyType type = details.type();
12419
Steve Block44f0eee2011-05-26 01:26:41 +010012420 if (value->IsJSFunction() && !heap->InNewSpace(value)) {
Steve Blocka7e24c12009-10-30 11:49:00 +000012421 ConstantFunctionDescriptor d(String::cast(key),
12422 JSFunction::cast(value),
12423 details.attributes(),
12424 details.index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012425 descriptors->Set(next_descriptor++, &d, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +000012426 } else if (type == NORMAL) {
12427 if (current_offset < inobject_props) {
12428 obj->InObjectPropertyAtPut(current_offset,
12429 value,
12430 UPDATE_WRITE_BARRIER);
12431 } else {
12432 int offset = current_offset - inobject_props;
12433 FixedArray::cast(fields)->set(offset, value);
12434 }
12435 FieldDescriptor d(String::cast(key),
12436 current_offset++,
12437 details.attributes(),
12438 details.index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012439 descriptors->Set(next_descriptor++, &d, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +000012440 } else if (type == CALLBACKS) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012441 if (value->IsAccessorPair()) {
12442 MaybeObject* maybe_copy =
12443 AccessorPair::cast(value)->CopyWithoutTransitions();
12444 if (!maybe_copy->To(&value)) return maybe_copy;
12445 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012446 CallbacksDescriptor d(String::cast(key),
12447 value,
12448 details.attributes(),
12449 details.index());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012450 descriptors->Set(next_descriptor++, &d, witness);
Steve Blocka7e24c12009-10-30 11:49:00 +000012451 } else {
12452 UNREACHABLE();
12453 }
12454 }
12455 }
12456 ASSERT(current_offset == number_of_fields);
12457
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012458 descriptors->Sort(witness);
Steve Blocka7e24c12009-10-30 11:49:00 +000012459 // Allocate new map.
John Reck59135872010-11-02 12:39:01 -070012460 Object* new_map;
12461 { MaybeObject* maybe_new_map = obj->map()->CopyDropDescriptors();
12462 if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map;
12463 }
Steve Blocka7e24c12009-10-30 11:49:00 +000012464
12465 // Transform the object.
12466 obj->set_map(Map::cast(new_map));
12467 obj->map()->set_instance_descriptors(descriptors);
12468 obj->map()->set_unused_property_fields(unused_property_fields);
12469
12470 obj->set_properties(FixedArray::cast(fields));
12471 ASSERT(obj->IsJSObject());
12472
12473 descriptors->SetNextEnumerationIndex(NextEnumerationIndex());
12474 // Check that it really works.
12475 ASSERT(obj->HasFastProperties());
12476
12477 return obj;
12478}
12479
12480
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012481bool ObjectHashSet::Contains(Object* key) {
12482 ASSERT(IsKey(key));
12483
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012484 // If the object does not have an identity hash, it was never used as a key.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012485 { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION);
12486 if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return false;
12487 }
12488 return (FindEntry(key) != kNotFound);
12489}
12490
12491
12492MaybeObject* ObjectHashSet::Add(Object* key) {
12493 ASSERT(IsKey(key));
12494
12495 // Make sure the key object has an identity hash code.
12496 int hash;
12497 { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION);
12498 if (maybe_hash->IsFailure()) return maybe_hash;
12499 hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value();
12500 }
12501 int entry = FindEntry(key);
12502
12503 // Check whether key is already present.
12504 if (entry != kNotFound) return this;
12505
12506 // Check whether the hash set should be extended and add entry.
12507 Object* obj;
12508 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
12509 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
12510 }
12511 ObjectHashSet* table = ObjectHashSet::cast(obj);
12512 entry = table->FindInsertionEntry(hash);
12513 table->set(EntryToIndex(entry), key);
12514 table->ElementAdded();
12515 return table;
12516}
12517
12518
12519MaybeObject* ObjectHashSet::Remove(Object* key) {
12520 ASSERT(IsKey(key));
12521
12522 // If the object does not have an identity hash, it was never used as a key.
12523 { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION);
12524 if (maybe_hash->ToObjectUnchecked()->IsUndefined()) return this;
12525 }
12526 int entry = FindEntry(key);
12527
12528 // Check whether key is actually present.
12529 if (entry == kNotFound) return this;
12530
12531 // Remove entry and try to shrink this hash set.
12532 set_the_hole(EntryToIndex(entry));
12533 ElementRemoved();
12534 return Shrink(key);
12535}
12536
12537
12538Object* ObjectHashTable::Lookup(Object* key) {
12539 ASSERT(IsKey(key));
12540
12541 // If the object does not have an identity hash, it was never used as a key.
12542 { MaybeObject* maybe_hash = key->GetHash(OMIT_CREATION);
12543 if (maybe_hash->ToObjectUnchecked()->IsUndefined()) {
12544 return GetHeap()->undefined_value();
12545 }
12546 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012547 int entry = FindEntry(key);
12548 if (entry == kNotFound) return GetHeap()->undefined_value();
12549 return get(EntryToIndex(entry) + 1);
12550}
12551
12552
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012553MaybeObject* ObjectHashTable::Put(Object* key, Object* value) {
12554 ASSERT(IsKey(key));
12555
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012556 // Make sure the key object has an identity hash code.
12557 int hash;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012558 { MaybeObject* maybe_hash = key->GetHash(ALLOW_CREATION);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012559 if (maybe_hash->IsFailure()) return maybe_hash;
12560 hash = Smi::cast(maybe_hash->ToObjectUnchecked())->value();
12561 }
12562 int entry = FindEntry(key);
12563
12564 // Check whether to perform removal operation.
12565 if (value->IsUndefined()) {
12566 if (entry == kNotFound) return this;
12567 RemoveEntry(entry);
12568 return Shrink(key);
12569 }
12570
12571 // Key is already in table, just overwrite value.
12572 if (entry != kNotFound) {
12573 set(EntryToIndex(entry) + 1, value);
12574 return this;
12575 }
12576
12577 // Check whether the hash table should be extended.
12578 Object* obj;
12579 { MaybeObject* maybe_obj = EnsureCapacity(1, key);
12580 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
12581 }
12582 ObjectHashTable* table = ObjectHashTable::cast(obj);
12583 table->AddEntry(table->FindInsertionEntry(hash), key, value);
12584 return table;
12585}
12586
12587
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012588void ObjectHashTable::AddEntry(int entry, Object* key, Object* value) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012589 set(EntryToIndex(entry), key);
12590 set(EntryToIndex(entry) + 1, value);
12591 ElementAdded();
12592}
12593
12594
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012595void ObjectHashTable::RemoveEntry(int entry) {
12596 set_the_hole(EntryToIndex(entry));
12597 set_the_hole(EntryToIndex(entry) + 1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012598 ElementRemoved();
12599}
12600
12601
Steve Blocka7e24c12009-10-30 11:49:00 +000012602#ifdef ENABLE_DEBUGGER_SUPPORT
12603// Check if there is a break point at this code position.
12604bool DebugInfo::HasBreakPoint(int code_position) {
12605 // Get the break point info object for this code position.
12606 Object* break_point_info = GetBreakPointInfo(code_position);
12607
12608 // If there is no break point info object or no break points in the break
12609 // point info object there is no break point at this code position.
12610 if (break_point_info->IsUndefined()) return false;
12611 return BreakPointInfo::cast(break_point_info)->GetBreakPointCount() > 0;
12612}
12613
12614
12615// Get the break point info object for this code position.
12616Object* DebugInfo::GetBreakPointInfo(int code_position) {
12617 // Find the index of the break point info object for this code position.
12618 int index = GetBreakPointInfoIndex(code_position);
12619
12620 // Return the break point info object if any.
Ben Murdoch8b112d22011-06-08 16:22:53 +010012621 if (index == kNoBreakPointInfo) return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012622 return BreakPointInfo::cast(break_points()->get(index));
12623}
12624
12625
12626// Clear a break point at the specified code position.
12627void DebugInfo::ClearBreakPoint(Handle<DebugInfo> debug_info,
12628 int code_position,
12629 Handle<Object> break_point_object) {
12630 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position));
12631 if (break_point_info->IsUndefined()) return;
12632 BreakPointInfo::ClearBreakPoint(
12633 Handle<BreakPointInfo>::cast(break_point_info),
12634 break_point_object);
12635}
12636
12637
12638void DebugInfo::SetBreakPoint(Handle<DebugInfo> debug_info,
12639 int code_position,
12640 int source_position,
12641 int statement_position,
12642 Handle<Object> break_point_object) {
Steve Block44f0eee2011-05-26 01:26:41 +010012643 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +000012644 Handle<Object> break_point_info(debug_info->GetBreakPointInfo(code_position));
12645 if (!break_point_info->IsUndefined()) {
12646 BreakPointInfo::SetBreakPoint(
12647 Handle<BreakPointInfo>::cast(break_point_info),
12648 break_point_object);
12649 return;
12650 }
12651
12652 // Adding a new break point for a code position which did not have any
12653 // break points before. Try to find a free slot.
12654 int index = kNoBreakPointInfo;
12655 for (int i = 0; i < debug_info->break_points()->length(); i++) {
12656 if (debug_info->break_points()->get(i)->IsUndefined()) {
12657 index = i;
12658 break;
12659 }
12660 }
12661 if (index == kNoBreakPointInfo) {
12662 // No free slot - extend break point info array.
12663 Handle<FixedArray> old_break_points =
12664 Handle<FixedArray>(FixedArray::cast(debug_info->break_points()));
Steve Blocka7e24c12009-10-30 11:49:00 +000012665 Handle<FixedArray> new_break_points =
Steve Block44f0eee2011-05-26 01:26:41 +010012666 isolate->factory()->NewFixedArray(
12667 old_break_points->length() +
12668 Debug::kEstimatedNofBreakPointsInFunction);
Kristian Monsen0d5e1162010-09-30 15:31:59 +010012669
12670 debug_info->set_break_points(*new_break_points);
Steve Blocka7e24c12009-10-30 11:49:00 +000012671 for (int i = 0; i < old_break_points->length(); i++) {
12672 new_break_points->set(i, old_break_points->get(i));
12673 }
12674 index = old_break_points->length();
12675 }
12676 ASSERT(index != kNoBreakPointInfo);
12677
12678 // Allocate new BreakPointInfo object and set the break point.
Steve Block44f0eee2011-05-26 01:26:41 +010012679 Handle<BreakPointInfo> new_break_point_info = Handle<BreakPointInfo>::cast(
12680 isolate->factory()->NewStruct(BREAK_POINT_INFO_TYPE));
Steve Blocka7e24c12009-10-30 11:49:00 +000012681 new_break_point_info->set_code_position(Smi::FromInt(code_position));
12682 new_break_point_info->set_source_position(Smi::FromInt(source_position));
12683 new_break_point_info->
12684 set_statement_position(Smi::FromInt(statement_position));
Steve Block44f0eee2011-05-26 01:26:41 +010012685 new_break_point_info->set_break_point_objects(
12686 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000012687 BreakPointInfo::SetBreakPoint(new_break_point_info, break_point_object);
12688 debug_info->break_points()->set(index, *new_break_point_info);
12689}
12690
12691
12692// Get the break point objects for a code position.
12693Object* DebugInfo::GetBreakPointObjects(int code_position) {
12694 Object* break_point_info = GetBreakPointInfo(code_position);
12695 if (break_point_info->IsUndefined()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010012696 return GetHeap()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012697 }
12698 return BreakPointInfo::cast(break_point_info)->break_point_objects();
12699}
12700
12701
12702// Get the total number of break points.
12703int DebugInfo::GetBreakPointCount() {
12704 if (break_points()->IsUndefined()) return 0;
12705 int count = 0;
12706 for (int i = 0; i < break_points()->length(); i++) {
12707 if (!break_points()->get(i)->IsUndefined()) {
12708 BreakPointInfo* break_point_info =
12709 BreakPointInfo::cast(break_points()->get(i));
12710 count += break_point_info->GetBreakPointCount();
12711 }
12712 }
12713 return count;
12714}
12715
12716
12717Object* DebugInfo::FindBreakPointInfo(Handle<DebugInfo> debug_info,
12718 Handle<Object> break_point_object) {
Ben Murdoch8b112d22011-06-08 16:22:53 +010012719 Heap* heap = debug_info->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +010012720 if (debug_info->break_points()->IsUndefined()) return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012721 for (int i = 0; i < debug_info->break_points()->length(); i++) {
12722 if (!debug_info->break_points()->get(i)->IsUndefined()) {
12723 Handle<BreakPointInfo> break_point_info =
12724 Handle<BreakPointInfo>(BreakPointInfo::cast(
12725 debug_info->break_points()->get(i)));
12726 if (BreakPointInfo::HasBreakPointObject(break_point_info,
12727 break_point_object)) {
12728 return *break_point_info;
12729 }
12730 }
12731 }
Steve Block44f0eee2011-05-26 01:26:41 +010012732 return heap->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +000012733}
12734
12735
12736// Find the index of the break point info object for the specified code
12737// position.
12738int DebugInfo::GetBreakPointInfoIndex(int code_position) {
12739 if (break_points()->IsUndefined()) return kNoBreakPointInfo;
12740 for (int i = 0; i < break_points()->length(); i++) {
12741 if (!break_points()->get(i)->IsUndefined()) {
12742 BreakPointInfo* break_point_info =
12743 BreakPointInfo::cast(break_points()->get(i));
12744 if (break_point_info->code_position()->value() == code_position) {
12745 return i;
12746 }
12747 }
12748 }
12749 return kNoBreakPointInfo;
12750}
12751
12752
12753// Remove the specified break point object.
12754void BreakPointInfo::ClearBreakPoint(Handle<BreakPointInfo> break_point_info,
12755 Handle<Object> break_point_object) {
Steve Block44f0eee2011-05-26 01:26:41 +010012756 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +000012757 // If there are no break points just ignore.
12758 if (break_point_info->break_point_objects()->IsUndefined()) return;
12759 // If there is a single break point clear it if it is the same.
12760 if (!break_point_info->break_point_objects()->IsFixedArray()) {
12761 if (break_point_info->break_point_objects() == *break_point_object) {
Steve Block44f0eee2011-05-26 01:26:41 +010012762 break_point_info->set_break_point_objects(
12763 isolate->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +000012764 }
12765 return;
12766 }
12767 // If there are multiple break points shrink the array
12768 ASSERT(break_point_info->break_point_objects()->IsFixedArray());
12769 Handle<FixedArray> old_array =
12770 Handle<FixedArray>(
12771 FixedArray::cast(break_point_info->break_point_objects()));
12772 Handle<FixedArray> new_array =
Steve Block44f0eee2011-05-26 01:26:41 +010012773 isolate->factory()->NewFixedArray(old_array->length() - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000012774 int found_count = 0;
12775 for (int i = 0; i < old_array->length(); i++) {
12776 if (old_array->get(i) == *break_point_object) {
12777 ASSERT(found_count == 0);
12778 found_count++;
12779 } else {
12780 new_array->set(i - found_count, old_array->get(i));
12781 }
12782 }
12783 // If the break point was found in the list change it.
12784 if (found_count > 0) break_point_info->set_break_point_objects(*new_array);
12785}
12786
12787
12788// Add the specified break point object.
12789void BreakPointInfo::SetBreakPoint(Handle<BreakPointInfo> break_point_info,
12790 Handle<Object> break_point_object) {
12791 // If there was no break point objects before just set it.
12792 if (break_point_info->break_point_objects()->IsUndefined()) {
12793 break_point_info->set_break_point_objects(*break_point_object);
12794 return;
12795 }
12796 // If the break point object is the same as before just ignore.
12797 if (break_point_info->break_point_objects() == *break_point_object) return;
12798 // If there was one break point object before replace with array.
12799 if (!break_point_info->break_point_objects()->IsFixedArray()) {
Steve Block44f0eee2011-05-26 01:26:41 +010012800 Handle<FixedArray> array = FACTORY->NewFixedArray(2);
Steve Blocka7e24c12009-10-30 11:49:00 +000012801 array->set(0, break_point_info->break_point_objects());
12802 array->set(1, *break_point_object);
12803 break_point_info->set_break_point_objects(*array);
12804 return;
12805 }
12806 // If there was more than one break point before extend array.
12807 Handle<FixedArray> old_array =
12808 Handle<FixedArray>(
12809 FixedArray::cast(break_point_info->break_point_objects()));
12810 Handle<FixedArray> new_array =
Steve Block44f0eee2011-05-26 01:26:41 +010012811 FACTORY->NewFixedArray(old_array->length() + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +000012812 for (int i = 0; i < old_array->length(); i++) {
12813 // If the break point was there before just ignore.
12814 if (old_array->get(i) == *break_point_object) return;
12815 new_array->set(i, old_array->get(i));
12816 }
12817 // Add the new break point.
12818 new_array->set(old_array->length(), *break_point_object);
12819 break_point_info->set_break_point_objects(*new_array);
12820}
12821
12822
12823bool BreakPointInfo::HasBreakPointObject(
12824 Handle<BreakPointInfo> break_point_info,
12825 Handle<Object> break_point_object) {
12826 // No break point.
12827 if (break_point_info->break_point_objects()->IsUndefined()) return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012828 // Single break point.
Steve Blocka7e24c12009-10-30 11:49:00 +000012829 if (!break_point_info->break_point_objects()->IsFixedArray()) {
12830 return break_point_info->break_point_objects() == *break_point_object;
12831 }
12832 // Multiple break points.
12833 FixedArray* array = FixedArray::cast(break_point_info->break_point_objects());
12834 for (int i = 0; i < array->length(); i++) {
12835 if (array->get(i) == *break_point_object) {
12836 return true;
12837 }
12838 }
12839 return false;
12840}
12841
12842
12843// Get the number of break points.
12844int BreakPointInfo::GetBreakPointCount() {
12845 // No break point.
12846 if (break_point_objects()->IsUndefined()) return 0;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000012847 // Single break point.
Steve Blocka7e24c12009-10-30 11:49:00 +000012848 if (!break_point_objects()->IsFixedArray()) return 1;
12849 // Multiple break points.
12850 return FixedArray::cast(break_point_objects())->length();
12851}
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012852#endif // ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000012853
12854
Ben Murdoch3ef787d2012-04-12 10:51:47 +010012855MaybeObject* JSDate::GetField(Object* object, Smi* index) {
12856 return JSDate::cast(object)->DoGetField(
12857 static_cast<FieldIndex>(index->value()));
12858}
12859
12860
12861Object* JSDate::DoGetField(FieldIndex index) {
12862 ASSERT(index != kDateValue);
12863
12864 DateCache* date_cache = GetIsolate()->date_cache();
12865
12866 if (index < kFirstUncachedField) {
12867 Object* stamp = cache_stamp();
12868 if (stamp != date_cache->stamp() && stamp->IsSmi()) {
12869 // Since the stamp is not NaN, the value is also not NaN.
12870 int64_t local_time_ms =
12871 date_cache->ToLocal(static_cast<int64_t>(value()->Number()));
12872 SetLocalFields(local_time_ms, date_cache);
12873 }
12874 switch (index) {
12875 case kYear: return year();
12876 case kMonth: return month();
12877 case kDay: return day();
12878 case kWeekday: return weekday();
12879 case kHour: return hour();
12880 case kMinute: return min();
12881 case kSecond: return sec();
12882 default: UNREACHABLE();
12883 }
12884 }
12885
12886 if (index >= kFirstUTCField) {
12887 return GetUTCField(index, value()->Number(), date_cache);
12888 }
12889
12890 double time = value()->Number();
12891 if (isnan(time)) return GetIsolate()->heap()->nan_value();
12892
12893 int64_t local_time_ms = date_cache->ToLocal(static_cast<int64_t>(time));
12894 int days = DateCache::DaysFromTime(local_time_ms);
12895
12896 if (index == kDays) return Smi::FromInt(days);
12897
12898 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
12899 if (index == kMillisecond) return Smi::FromInt(time_in_day_ms % 1000);
12900 ASSERT(index == kTimeInDay);
12901 return Smi::FromInt(time_in_day_ms);
12902}
12903
12904
12905Object* JSDate::GetUTCField(FieldIndex index,
12906 double value,
12907 DateCache* date_cache) {
12908 ASSERT(index >= kFirstUTCField);
12909
12910 if (isnan(value)) return GetIsolate()->heap()->nan_value();
12911
12912 int64_t time_ms = static_cast<int64_t>(value);
12913
12914 if (index == kTimezoneOffset) {
12915 return Smi::FromInt(date_cache->TimezoneOffset(time_ms));
12916 }
12917
12918 int days = DateCache::DaysFromTime(time_ms);
12919
12920 if (index == kWeekdayUTC) return Smi::FromInt(date_cache->Weekday(days));
12921
12922 if (index <= kDayUTC) {
12923 int year, month, day;
12924 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
12925 if (index == kYearUTC) return Smi::FromInt(year);
12926 if (index == kMonthUTC) return Smi::FromInt(month);
12927 ASSERT(index == kDayUTC);
12928 return Smi::FromInt(day);
12929 }
12930
12931 int time_in_day_ms = DateCache::TimeInDay(time_ms, days);
12932 switch (index) {
12933 case kHourUTC: return Smi::FromInt(time_in_day_ms / (60 * 60 * 1000));
12934 case kMinuteUTC: return Smi::FromInt((time_in_day_ms / (60 * 1000)) % 60);
12935 case kSecondUTC: return Smi::FromInt((time_in_day_ms / 1000) % 60);
12936 case kMillisecondUTC: return Smi::FromInt(time_in_day_ms % 1000);
12937 case kDaysUTC: return Smi::FromInt(days);
12938 case kTimeInDayUTC: return Smi::FromInt(time_in_day_ms);
12939 default: UNREACHABLE();
12940 }
12941
12942 UNREACHABLE();
12943 return NULL;
12944}
12945
12946
12947void JSDate::SetValue(Object* value, bool is_value_nan) {
12948 set_value(value);
12949 if (is_value_nan) {
12950 HeapNumber* nan = GetIsolate()->heap()->nan_value();
12951 set_cache_stamp(nan, SKIP_WRITE_BARRIER);
12952 set_year(nan, SKIP_WRITE_BARRIER);
12953 set_month(nan, SKIP_WRITE_BARRIER);
12954 set_day(nan, SKIP_WRITE_BARRIER);
12955 set_hour(nan, SKIP_WRITE_BARRIER);
12956 set_min(nan, SKIP_WRITE_BARRIER);
12957 set_sec(nan, SKIP_WRITE_BARRIER);
12958 set_weekday(nan, SKIP_WRITE_BARRIER);
12959 } else {
12960 set_cache_stamp(Smi::FromInt(DateCache::kInvalidStamp), SKIP_WRITE_BARRIER);
12961 }
12962}
12963
12964
12965void JSDate::SetLocalFields(int64_t local_time_ms, DateCache* date_cache) {
12966 int days = DateCache::DaysFromTime(local_time_ms);
12967 int time_in_day_ms = DateCache::TimeInDay(local_time_ms, days);
12968 int year, month, day;
12969 date_cache->YearMonthDayFromDays(days, &year, &month, &day);
12970 int weekday = date_cache->Weekday(days);
12971 int hour = time_in_day_ms / (60 * 60 * 1000);
12972 int min = (time_in_day_ms / (60 * 1000)) % 60;
12973 int sec = (time_in_day_ms / 1000) % 60;
12974 set_cache_stamp(date_cache->stamp());
12975 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER);
12976 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER);
12977 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER);
12978 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER);
12979 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER);
12980 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER);
12981 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER);
12982}
12983
Steve Blocka7e24c12009-10-30 11:49:00 +000012984} } // namespace v8::internal