blob: b03b642c682b445f858bbe11e4d97ab660fb3c9b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "arguments.h"
33#include "bootstrapper.h"
34#include "compiler.h"
35#include "debug.h"
36#include "execution.h"
37#include "global-handles.h"
38#include "natives.h"
39#include "runtime.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080040#include "string-search.h"
Steve Blockd0582a62009-12-15 09:54:21 +000041#include "stub-cache.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
47
Steve Blocka7e24c12009-10-30 11:49:00 +000048int HandleScope::NumberOfHandles() {
Steve Block44f0eee2011-05-26 01:26:41 +010049 Isolate* isolate = Isolate::Current();
50 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
51 int n = impl->blocks()->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000052 if (n == 0) return 0;
Steve Blockd0582a62009-12-15 09:54:21 +000053 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
Steve Block44f0eee2011-05-26 01:26:41 +010054 (isolate->handle_scope_data()->next - impl->blocks()->last()));
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
58Object** HandleScope::Extend() {
Steve Block44f0eee2011-05-26 01:26:41 +010059 Isolate* isolate = Isolate::Current();
60 v8::ImplementationUtilities::HandleScopeData* current =
61 isolate->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +000062
Steve Block44f0eee2011-05-26 01:26:41 +010063 Object** result = current->next;
64
65 ASSERT(result == current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000066 // Make sure there's at least one scope on the stack and that the
67 // top of the scope stack isn't a barrier.
Steve Block44f0eee2011-05-26 01:26:41 +010068 if (current->level == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +000069 Utils::ReportApiFailure("v8::HandleScope::CreateHandle()",
70 "Cannot create a handle without a HandleScope");
71 return NULL;
72 }
Steve Block44f0eee2011-05-26 01:26:41 +010073 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
Steve Blocka7e24c12009-10-30 11:49:00 +000074 // If there's more room in the last block, we use that. This is used
75 // for fast creation of scopes after scope barriers.
76 if (!impl->blocks()->is_empty()) {
77 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
Steve Block44f0eee2011-05-26 01:26:41 +010078 if (current->limit != limit) {
79 current->limit = limit;
80 ASSERT(limit - current->next < kHandleBlockSize);
Steve Blocka7e24c12009-10-30 11:49:00 +000081 }
82 }
83
84 // If we still haven't found a slot for the handle, we extend the
85 // current handle scope by allocating a new handle block.
Steve Block44f0eee2011-05-26 01:26:41 +010086 if (result == current->limit) {
Steve Blocka7e24c12009-10-30 11:49:00 +000087 // If there's a spare block, use it for growing the current scope.
88 result = impl->GetSpareOrNewBlock();
89 // Add the extension to the global list of blocks, but count the
90 // extension as part of the current scope.
91 impl->blocks()->Add(result);
Steve Block44f0eee2011-05-26 01:26:41 +010092 current->limit = &result[kHandleBlockSize];
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
94
95 return result;
96}
97
98
Steve Block44f0eee2011-05-26 01:26:41 +010099void HandleScope::DeleteExtensions(Isolate* isolate) {
100 ASSERT(isolate == Isolate::Current());
101 v8::ImplementationUtilities::HandleScopeData* current =
102 isolate->handle_scope_data();
103 isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000104}
105
106
107void HandleScope::ZapRange(Object** start, Object** end) {
John Reck59135872010-11-02 12:39:01 -0700108 ASSERT(end - start <= kHandleBlockSize);
109 for (Object** p = start; p != end; p++) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
111 }
112}
113
114
John Reck59135872010-11-02 12:39:01 -0700115Address HandleScope::current_level_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 return reinterpret_cast<Address>(
117 &Isolate::Current()->handle_scope_data()->level);
Steve Blockd0582a62009-12-15 09:54:21 +0000118}
119
120
121Address HandleScope::current_next_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100122 return reinterpret_cast<Address>(
123 &Isolate::Current()->handle_scope_data()->next);
Steve Blockd0582a62009-12-15 09:54:21 +0000124}
125
126
127Address HandleScope::current_limit_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100128 return reinterpret_cast<Address>(
129 &Isolate::Current()->handle_scope_data()->limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000130}
131
132
Steve Blocka7e24c12009-10-30 11:49:00 +0000133Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content,
134 Handle<JSArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100135 CALL_HEAP_FUNCTION(content->GetIsolate(),
136 content->AddKeysFromJSArray(*array), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137}
138
139
140Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
141 Handle<FixedArray> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100142 CALL_HEAP_FUNCTION(first->GetIsolate(),
143 first->UnionOfKeys(*second), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000144}
145
146
147Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
148 Handle<JSFunction> constructor,
149 Handle<JSGlobalProxy> global) {
Steve Block44f0eee2011-05-26 01:26:41 +0100150 CALL_HEAP_FUNCTION(
151 constructor->GetIsolate(),
152 constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global),
153 JSGlobalProxy);
Steve Blocka7e24c12009-10-30 11:49:00 +0000154}
155
156
157void SetExpectedNofProperties(Handle<JSFunction> func, int nof) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100158 // If objects constructed from this function exist then changing
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800159 // 'estimated_nof_properties' is dangerous since the previous value might
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100160 // have been compiled into the fast construct stub. More over, the inobject
161 // slack tracking logic might have adjusted the previous value, so even
162 // passing the same value is risky.
163 if (func->shared()->live_objects_may_exist()) return;
164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 func->shared()->set_expected_nof_properties(nof);
166 if (func->has_initial_map()) {
167 Handle<Map> new_initial_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100168 func->GetIsolate()->factory()->CopyMapDropTransitions(
169 Handle<Map>(func->initial_map()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 new_initial_map->set_unused_property_fields(nof);
171 func->set_initial_map(*new_initial_map);
172 }
173}
174
175
176void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100177 CALL_HEAP_FUNCTION_VOID(func->GetIsolate(),
178 func->SetPrototype(*value));
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181
182static int ExpectedNofPropertiesFromEstimate(int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100183 // If no properties are added in the constructor, they are more likely
184 // to be added later.
185 if (estimate == 0) estimate = 2;
186
187 // We do not shrink objects that go into a snapshot (yet), so we adjust
188 // the estimate conservatively.
189 if (Serializer::enabled()) return estimate + 2;
190
191 // Inobject slack tracking will reclaim redundant inobject space later,
192 // so we can afford to adjust the estimate generously.
Ben Murdochf87a2032010-10-22 12:50:53 +0100193 return estimate + 8;
Steve Blocka7e24c12009-10-30 11:49:00 +0000194}
195
196
197void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
198 int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100199 // See the comment in SetExpectedNofProperties.
200 if (shared->live_objects_may_exist()) return;
201
Steve Blocka7e24c12009-10-30 11:49:00 +0000202 shared->set_expected_nof_properties(
203 ExpectedNofPropertiesFromEstimate(estimate));
204}
205
206
Steve Blocka7e24c12009-10-30 11:49:00 +0000207void NormalizeProperties(Handle<JSObject> object,
208 PropertyNormalizationMode mode,
209 int expected_additional_properties) {
Steve Block44f0eee2011-05-26 01:26:41 +0100210 CALL_HEAP_FUNCTION_VOID(object->GetIsolate(),
211 object->NormalizeProperties(
212 mode,
213 expected_additional_properties));
Steve Blocka7e24c12009-10-30 11:49:00 +0000214}
215
216
217void NormalizeElements(Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100218 CALL_HEAP_FUNCTION_VOID(object->GetIsolate(),
219 object->NormalizeElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000220}
221
222
223void TransformToFastProperties(Handle<JSObject> object,
224 int unused_property_fields) {
225 CALL_HEAP_FUNCTION_VOID(
Steve Block44f0eee2011-05-26 01:26:41 +0100226 object->GetIsolate(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 object->TransformToFastProperties(unused_property_fields));
228}
229
230
John Reck59135872010-11-02 12:39:01 -0700231void NumberDictionarySet(Handle<NumberDictionary> dictionary,
232 uint32_t index,
233 Handle<Object> value,
234 PropertyDetails details) {
Steve Block44f0eee2011-05-26 01:26:41 +0100235 CALL_HEAP_FUNCTION_VOID(dictionary->GetIsolate(),
236 dictionary->Set(index, *value, details));
John Reck59135872010-11-02 12:39:01 -0700237}
238
239
Steve Blocka7e24c12009-10-30 11:49:00 +0000240void FlattenString(Handle<String> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100241 CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten());
Steve Block8defd9f2010-07-08 12:39:36 +0100242}
243
244
245Handle<String> FlattenGetString(Handle<String> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100246 CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000247}
248
249
250Handle<Object> SetPrototype(Handle<JSFunction> function,
251 Handle<Object> prototype) {
Steve Block6ded16b2010-05-10 14:33:55 +0100252 ASSERT(function->should_have_prototype());
Steve Block44f0eee2011-05-26 01:26:41 +0100253 CALL_HEAP_FUNCTION(function->GetIsolate(),
254 Accessors::FunctionSetPrototype(*function,
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 *prototype,
256 NULL),
257 Object);
258}
259
260
261Handle<Object> SetProperty(Handle<JSObject> object,
262 Handle<String> key,
263 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100264 PropertyAttributes attributes,
265 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100266 CALL_HEAP_FUNCTION(object->GetIsolate(),
267 object->SetProperty(*key, *value, attributes, strict_mode),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100268 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000269}
270
271
272Handle<Object> SetProperty(Handle<Object> object,
273 Handle<Object> key,
274 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100275 PropertyAttributes attributes,
276 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100277 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 CALL_HEAP_FUNCTION(
Steve Block44f0eee2011-05-26 01:26:41 +0100279 isolate,
280 Runtime::SetObjectProperty(
281 isolate, object, key, value, attributes, strict_mode),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100282 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000283}
284
285
286Handle<Object> ForceSetProperty(Handle<JSObject> object,
287 Handle<Object> key,
288 Handle<Object> value,
289 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100290 Isolate* isolate = object->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000291 CALL_HEAP_FUNCTION(
Steve Block44f0eee2011-05-26 01:26:41 +0100292 isolate,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100293 Runtime::ForceSetObjectProperty(
Steve Block44f0eee2011-05-26 01:26:41 +0100294 isolate, object, key, value, attributes),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100295 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000296}
297
298
Andrei Popescu31002712010-02-23 13:46:05 +0000299Handle<Object> SetNormalizedProperty(Handle<JSObject> object,
300 Handle<String> key,
301 Handle<Object> value,
302 PropertyDetails details) {
Steve Block44f0eee2011-05-26 01:26:41 +0100303 CALL_HEAP_FUNCTION(object->GetIsolate(),
304 object->SetNormalizedProperty(*key, *value, details),
Andrei Popescu31002712010-02-23 13:46:05 +0000305 Object);
306}
307
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
310 Handle<Object> key) {
Steve Block44f0eee2011-05-26 01:26:41 +0100311 Isolate* isolate = object->GetIsolate();
312 CALL_HEAP_FUNCTION(isolate,
313 Runtime::ForceDeleteObjectProperty(isolate, object, key),
314 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000315}
316
317
Ben Murdoch086aeea2011-05-13 15:57:08 +0100318Handle<Object> SetLocalPropertyIgnoreAttributes(
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 Handle<JSObject> object,
320 Handle<String> key,
321 Handle<Object> value,
322 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100323 CALL_HEAP_FUNCTION(
324 object->GetIsolate(),
325 object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes),
326 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000327}
328
329
Steve Block1e0659c2011-05-24 12:43:12 +0100330void SetLocalPropertyNoThrow(Handle<JSObject> object,
331 Handle<String> key,
332 Handle<Object> value,
333 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100334 Isolate* isolate = object->GetIsolate();
335 ASSERT(!isolate->has_pending_exception());
Steve Block1e0659c2011-05-24 12:43:12 +0100336 CHECK(!SetLocalPropertyIgnoreAttributes(
337 object, key, value, attributes).is_null());
Steve Block44f0eee2011-05-26 01:26:41 +0100338 CHECK(!isolate->has_pending_exception());
Steve Block1e0659c2011-05-24 12:43:12 +0100339}
340
341
Steve Blocka7e24c12009-10-30 11:49:00 +0000342Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
343 Handle<String> key,
344 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100345 PropertyAttributes attributes,
346 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100347 CALL_HEAP_FUNCTION(object->GetIsolate(),
348 object->SetPropertyWithInterceptor(*key,
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 *value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100350 attributes,
351 strict_mode),
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 Object);
353}
354
355
356Handle<Object> GetProperty(Handle<JSObject> obj,
357 const char* name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100358 Isolate* isolate = obj->GetIsolate();
359 Handle<String> str = isolate->factory()->LookupAsciiSymbol(name);
360 CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361}
362
363
364Handle<Object> GetProperty(Handle<Object> obj,
Ben Murdoch257744e2011-11-30 15:57:28 +0000365 const char* name,
366 LookupResult* result) {
367 Isolate* isolate = Isolate::Current();
368 Handle<String> str = isolate->factory()->LookupAsciiSymbol(name);
369 PropertyAttributes attributes;
370 CALL_HEAP_FUNCTION(
371 isolate, obj->GetProperty(*obj, result, *str, &attributes), Object);
372}
373
374
375Handle<Object> GetProperty(Handle<Object> obj,
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 Handle<Object> key) {
Steve Block44f0eee2011-05-26 01:26:41 +0100377 Isolate* isolate = Isolate::Current();
378 CALL_HEAP_FUNCTION(isolate,
379 Runtime::GetObjectProperty(isolate, obj, key), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000380}
381
382
Ben Murdoch8b112d22011-06-08 16:22:53 +0100383Handle<Object> GetProperty(Handle<JSObject> obj,
384 Handle<String> name,
385 LookupResult* result) {
386 PropertyAttributes attributes;
387 Isolate* isolate = Isolate::Current();
388 CALL_HEAP_FUNCTION(isolate,
389 obj->GetProperty(*obj, result, *name, &attributes),
390 Object);
391}
392
393
Steve Block6ded16b2010-05-10 14:33:55 +0100394Handle<Object> GetElement(Handle<Object> obj,
395 uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +0100396 Isolate* isolate = Isolate::Current();
397 CALL_HEAP_FUNCTION(isolate, Runtime::GetElement(obj, index), Object);
Steve Block6ded16b2010-05-10 14:33:55 +0100398}
399
400
Steve Blocka7e24c12009-10-30 11:49:00 +0000401Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
402 Handle<JSObject> holder,
403 Handle<String> name,
404 PropertyAttributes* attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100405 Isolate* isolate = receiver->GetIsolate();
406 CALL_HEAP_FUNCTION(isolate,
407 holder->GetPropertyWithInterceptor(*receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 *name,
409 attributes),
410 Object);
411}
412
413
414Handle<Object> GetPrototype(Handle<Object> obj) {
415 Handle<Object> result(obj->GetPrototype());
416 return result;
417}
418
419
Andrei Popescu402d9372010-02-26 13:31:12 +0000420Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) {
421 const bool skip_hidden_prototypes = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100422 CALL_HEAP_FUNCTION(obj->GetIsolate(),
423 obj->SetPrototype(*value, skip_hidden_prototypes), Object);
424}
425
426
427Handle<Object> PreventExtensions(Handle<JSObject> object) {
428 CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object);
Andrei Popescu402d9372010-02-26 13:31:12 +0000429}
430
431
Steve Blocka7e24c12009-10-30 11:49:00 +0000432Handle<Object> GetHiddenProperties(Handle<JSObject> obj,
433 bool create_if_needed) {
Steve Block44f0eee2011-05-26 01:26:41 +0100434 Isolate* isolate = obj->GetIsolate();
Steve Blockd0582a62009-12-15 09:54:21 +0000435 Object* holder = obj->BypassGlobalProxy();
Steve Block44f0eee2011-05-26 01:26:41 +0100436 if (holder->IsUndefined()) return isolate->factory()->undefined_value();
437 obj = Handle<JSObject>(JSObject::cast(holder), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000438
439 if (obj->HasFastProperties()) {
440 // If the object has fast properties, check whether the first slot
441 // in the descriptor array matches the hidden symbol. Since the
442 // hidden symbols hash code is zero (and no other string has hash
443 // code zero) it will always occupy the first entry if present.
444 DescriptorArray* descriptors = obj->map()->instance_descriptors();
445 if ((descriptors->number_of_descriptors() > 0) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100446 (descriptors->GetKey(0) == isolate->heap()->hidden_symbol()) &&
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 descriptors->IsProperty(0)) {
448 ASSERT(descriptors->GetType(0) == FIELD);
Steve Block44f0eee2011-05-26 01:26:41 +0100449 return Handle<Object>(obj->FastPropertyAt(descriptors->GetFieldIndex(0)),
450 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 }
452 }
453
454 // Only attempt to find the hidden properties in the local object and not
455 // in the prototype chain. Note that HasLocalProperty() can cause a GC in
456 // the general case in the presence of interceptors.
Steve Blockd0582a62009-12-15 09:54:21 +0000457 if (!obj->HasHiddenPropertiesObject()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000458 // Hidden properties object not found. Allocate a new hidden properties
459 // object if requested. Otherwise return the undefined value.
460 if (create_if_needed) {
Steve Block44f0eee2011-05-26 01:26:41 +0100461 Handle<Object> hidden_obj =
462 isolate->factory()->NewJSObject(isolate->object_function());
463 CALL_HEAP_FUNCTION(isolate,
464 obj->SetHiddenPropertiesObject(*hidden_obj), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100466 return isolate->factory()->undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 }
468 }
Steve Block44f0eee2011-05-26 01:26:41 +0100469 return Handle<Object>(obj->GetHiddenPropertiesObject(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470}
471
472
473Handle<Object> DeleteElement(Handle<JSObject> obj,
474 uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +0100475 CALL_HEAP_FUNCTION(obj->GetIsolate(),
476 obj->DeleteElement(index, JSObject::NORMAL_DELETION),
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 Object);
478}
479
480
481Handle<Object> DeleteProperty(Handle<JSObject> obj,
482 Handle<String> prop) {
Steve Block44f0eee2011-05-26 01:26:41 +0100483 CALL_HEAP_FUNCTION(obj->GetIsolate(),
484 obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION),
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 Object);
486}
487
488
489Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +0100490 Isolate* isolate = Isolate::Current();
491 CALL_HEAP_FUNCTION(
492 isolate,
493 isolate->heap()->LookupSingleCharacterStringFromCode(index), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000494}
495
496
Steve Block6ded16b2010-05-10 14:33:55 +0100497Handle<String> SubString(Handle<String> str,
498 int start,
499 int end,
500 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100501 CALL_HEAP_FUNCTION(str->GetIsolate(),
502 str->SubString(start, end, pretenure), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000503}
504
505
506Handle<Object> SetElement(Handle<JSObject> object,
507 uint32_t index,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100508 Handle<Object> value,
509 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100510 if (object->HasExternalArrayElements()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) {
512 bool has_exception;
513 Handle<Object> number = Execution::ToNumber(value, &has_exception);
514 if (has_exception) return Handle<Object>();
515 value = number;
516 }
517 }
Steve Block44f0eee2011-05-26 01:26:41 +0100518 CALL_HEAP_FUNCTION(object->GetIsolate(),
519 object->SetElement(index, *value, strict_mode), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000520}
521
522
Ben Murdoch086aeea2011-05-13 15:57:08 +0100523Handle<Object> SetOwnElement(Handle<JSObject> object,
524 uint32_t index,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100525 Handle<Object> value,
526 StrictModeFlag strict_mode) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100527 ASSERT(!object->HasExternalArrayElements());
Steve Block44f0eee2011-05-26 01:26:41 +0100528 CALL_HEAP_FUNCTION(object->GetIsolate(),
529 object->SetElement(index, *value, strict_mode, false),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100530 Object);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100531}
532
533
Steve Blocka7e24c12009-10-30 11:49:00 +0000534Handle<JSObject> Copy(Handle<JSObject> obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100535 Isolate* isolate = obj->GetIsolate();
536 CALL_HEAP_FUNCTION(isolate,
537 isolate->heap()->CopyJSObject(*obj), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}
539
540
Leon Clarkef7060e22010-06-03 12:02:55 +0100541Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100542 CALL_HEAP_FUNCTION(obj->GetIsolate(), obj->DefineAccessor(*info), Object);
Leon Clarkef7060e22010-06-03 12:02:55 +0100543}
544
545
Steve Blocka7e24c12009-10-30 11:49:00 +0000546// Wrappers for scripts are kept alive and cached in weak global
Ben Murdoch257744e2011-11-30 15:57:28 +0000547// handles referred from foreign objects held by the scripts as long as
Steve Blocka7e24c12009-10-30 11:49:00 +0000548// they are used. When they are not used anymore, the garbage
549// collector will call the weak callback on the global handle
550// associated with the wrapper and get rid of both the wrapper and the
551// handle.
552static void ClearWrapperCache(Persistent<v8::Value> handle, void*) {
553#ifdef ENABLE_HEAP_PROTECTION
554 // Weak reference callbacks are called as if from outside V8. We
555 // need to reeenter to unprotect the heap.
556 VMState state(OTHER);
557#endif
558 Handle<Object> cache = Utils::OpenHandle(*handle);
559 JSValue* wrapper = JSValue::cast(*cache);
Ben Murdoch257744e2011-11-30 15:57:28 +0000560 Foreign* foreign = Script::cast(wrapper->value())->wrapper();
561 ASSERT(foreign->address() == reinterpret_cast<Address>(cache.location()));
562 foreign->set_address(0);
Steve Block44f0eee2011-05-26 01:26:41 +0100563 Isolate* isolate = Isolate::Current();
564 isolate->global_handles()->Destroy(cache.location());
565 isolate->counters()->script_wrappers()->Decrement();
Steve Blocka7e24c12009-10-30 11:49:00 +0000566}
567
568
569Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000570 if (script->wrapper()->address() != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 // Return the script wrapper directly from the cache.
572 return Handle<JSValue>(
Ben Murdoch257744e2011-11-30 15:57:28 +0000573 reinterpret_cast<JSValue**>(script->wrapper()->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 }
Steve Block44f0eee2011-05-26 01:26:41 +0100575 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 // Construct a new script wrapper.
Steve Block44f0eee2011-05-26 01:26:41 +0100577 isolate->counters()->script_wrappers()->Increment();
578 Handle<JSFunction> constructor = isolate->script_function();
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 Handle<JSValue> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100580 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
Steve Blocka7e24c12009-10-30 11:49:00 +0000581 result->set_value(*script);
582
583 // Create a new weak global handle and use it to cache the wrapper
584 // for future use. The cache will automatically be cleared by the
585 // garbage collector when it is not used anymore.
Steve Block44f0eee2011-05-26 01:26:41 +0100586 Handle<Object> handle = isolate->global_handles()->Create(*result);
587 isolate->global_handles()->MakeWeak(handle.location(), NULL,
588 &ClearWrapperCache);
Ben Murdoch257744e2011-11-30 15:57:28 +0000589 script->wrapper()->set_address(reinterpret_cast<Address>(handle.location()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 return result;
591}
592
593
594// Init line_ends array with code positions of line ends inside script
595// source.
596void InitScriptLineEnds(Handle<Script> script) {
597 if (!script->line_ends()->IsUndefined()) return;
598
Steve Block44f0eee2011-05-26 01:26:41 +0100599 Isolate* isolate = script->GetIsolate();
600
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 if (!script->source()->IsString()) {
602 ASSERT(script->source()->IsUndefined());
Steve Block44f0eee2011-05-26 01:26:41 +0100603 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100604 script->set_line_ends(*empty);
Steve Blockd0582a62009-12-15 09:54:21 +0000605 ASSERT(script->line_ends()->IsFixedArray());
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 return;
607 }
608
Steve Block44f0eee2011-05-26 01:26:41 +0100609 Handle<String> src(String::cast(script->source()), isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100610
611 Handle<FixedArray> array = CalculateLineEnds(src, true);
612
Steve Block44f0eee2011-05-26 01:26:41 +0100613 if (*array != isolate->heap()->empty_fixed_array()) {
614 array->set_map(isolate->heap()->fixed_cow_array_map());
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800615 }
616
Steve Block6ded16b2010-05-10 14:33:55 +0100617 script->set_line_ends(*array);
618 ASSERT(script->line_ends()->IsFixedArray());
619}
620
621
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800622template <typename SourceChar>
Steve Block44f0eee2011-05-26 01:26:41 +0100623static void CalculateLineEnds(Isolate* isolate,
624 List<int>* line_ends,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800625 Vector<const SourceChar> src,
626 bool with_last_line) {
627 const int src_len = src.length();
Steve Block44f0eee2011-05-26 01:26:41 +0100628 StringSearch<char, SourceChar> search(isolate, CStrVector("\n"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000629
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800630 // Find and record line ends.
Steve Blocka7e24c12009-10-30 11:49:00 +0000631 int position = 0;
632 while (position != -1 && position < src_len) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800633 position = search.Search(src, position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 if (position != -1) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800635 line_ends->Add(position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 position++;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800637 } else if (with_last_line) {
Steve Block6ded16b2010-05-10 14:33:55 +0100638 // Even if the last line misses a line end, it is counted.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800639 line_ends->Add(src_len);
640 return;
Steve Block6ded16b2010-05-10 14:33:55 +0100641 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000642 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800643}
Steve Blocka7e24c12009-10-30 11:49:00 +0000644
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800645
646Handle<FixedArray> CalculateLineEnds(Handle<String> src,
647 bool with_last_line) {
648 src = FlattenGetString(src);
649 // Rough estimate of line count based on a roughly estimated average
650 // length of (unpacked) code.
651 int line_count_estimate = src->length() >> 4;
652 List<int> line_ends(line_count_estimate);
Steve Block44f0eee2011-05-26 01:26:41 +0100653 Isolate* isolate = src->GetIsolate();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800654 {
655 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid.
656 // Dispatch on type of strings.
657 if (src->IsAsciiRepresentation()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100658 CalculateLineEnds(isolate,
659 &line_ends,
660 src->ToAsciiVector(),
661 with_last_line);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800662 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100663 CalculateLineEnds(isolate,
664 &line_ends,
665 src->ToUC16Vector(),
666 with_last_line);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800667 }
668 }
669 int line_count = line_ends.length();
Steve Block44f0eee2011-05-26 01:26:41 +0100670 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800671 for (int i = 0; i < line_count; i++) {
672 array->set(i, Smi::FromInt(line_ends[i]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 }
Steve Block6ded16b2010-05-10 14:33:55 +0100674 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000675}
676
677
678// Convert code position into line number.
679int GetScriptLineNumber(Handle<Script> script, int code_pos) {
680 InitScriptLineEnds(script);
681 AssertNoAllocation no_allocation;
Andrei Popescu402d9372010-02-26 13:31:12 +0000682 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
Steve Blockd0582a62009-12-15 09:54:21 +0000683 const int line_ends_len = line_ends_array->length();
Steve Blocka7e24c12009-10-30 11:49:00 +0000684
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800685 if (!line_ends_len) return -1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000686
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800687 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000688 return script->line_offset()->value();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800689 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000690
691 int left = 0;
692 int right = line_ends_len;
693 while (int half = (right - left) / 2) {
694 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
695 right -= half;
696 } else {
697 left += half;
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 }
699 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000700 return right + script->line_offset()->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000701}
702
703
Steve Block6ded16b2010-05-10 14:33:55 +0100704int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
705 AssertNoAllocation no_allocation;
706 if (!script->line_ends()->IsUndefined()) {
707 return GetScriptLineNumber(script, code_pos);
708 }
709 // Slow mode: we do not have line_ends. We have to iterate through source.
710 if (!script->source()->IsString()) {
711 return -1;
712 }
713 String* source = String::cast(script->source());
714 int line = 0;
715 int len = source->length();
716 for (int pos = 0; pos < len; pos++) {
717 if (pos == code_pos) {
718 break;
719 }
720 if (source->Get(pos) == '\n') {
721 line++;
722 }
723 }
724 return line;
725}
726
727
Steve Blocka7e24c12009-10-30 11:49:00 +0000728void CustomArguments::IterateInstance(ObjectVisitor* v) {
Steve Block6ded16b2010-05-10 14:33:55 +0100729 v->VisitPointers(values_, values_ + ARRAY_SIZE(values_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000730}
731
732
733// Compute the property keys from the interceptor.
734v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
735 Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100736 Isolate* isolate = receiver->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000737 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +0100738 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000739 v8::AccessorInfo info(args.end());
740 v8::Handle<v8::Array> result;
741 if (!interceptor->enumerator()->IsUndefined()) {
742 v8::NamedPropertyEnumerator enum_fun =
743 v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator());
Steve Block44f0eee2011-05-26 01:26:41 +0100744 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
Steve Blocka7e24c12009-10-30 11:49:00 +0000745 {
746 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100747 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000748 result = enum_fun(info);
749 }
750 }
751 return result;
752}
753
754
755// Compute the element keys from the interceptor.
756v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
757 Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100758 Isolate* isolate = receiver->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +0100760 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 v8::AccessorInfo info(args.end());
762 v8::Handle<v8::Array> result;
763 if (!interceptor->enumerator()->IsUndefined()) {
764 v8::IndexedPropertyEnumerator enum_fun =
765 v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator());
Steve Block44f0eee2011-05-26 01:26:41 +0100766 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 {
768 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100769 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000770 result = enum_fun(info);
771 }
772 }
773 return result;
774}
775
776
Ben Murdochf87a2032010-10-22 12:50:53 +0100777static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
778 int len = array->length();
779 for (int i = 0; i < len; i++) {
780 Object* e = array->get(i);
781 if (!(e->IsString() || e->IsNumber())) return false;
782 }
783 return true;
784}
785
786
Steve Blocka7e24c12009-10-30 11:49:00 +0000787Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object,
788 KeyCollectionType type) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100789 USE(ContainsOnlyValidKeys);
Steve Block44f0eee2011-05-26 01:26:41 +0100790 Isolate* isolate = object->GetIsolate();
791 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
792 Handle<JSObject> arguments_boilerplate = Handle<JSObject>(
793 isolate->context()->global_context()->arguments_boilerplate(),
794 isolate);
795 Handle<JSFunction> arguments_function = Handle<JSFunction>(
796 JSFunction::cast(arguments_boilerplate->map()->constructor()),
797 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000798
799 // Only collect keys if access is permitted.
800 for (Handle<Object> p = object;
Steve Block44f0eee2011-05-26 01:26:41 +0100801 *p != isolate->heap()->null_value();
802 p = Handle<Object>(p->GetPrototype(), isolate)) {
803 Handle<JSObject> current(JSObject::cast(*p), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000804
805 // Check access rights if required.
806 if (current->IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100807 !isolate->MayNamedAccess(*current,
808 isolate->heap()->undefined_value(),
809 v8::ACCESS_KEYS)) {
810 isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 break;
812 }
813
814 // Compute the element keys.
815 Handle<FixedArray> element_keys =
Steve Block44f0eee2011-05-26 01:26:41 +0100816 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 current->GetEnumElementKeys(*element_keys);
818 content = UnionOfKeys(content, element_keys);
Ben Murdochf87a2032010-10-22 12:50:53 +0100819 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000820
821 // Add the element keys from the interceptor.
822 if (current->HasIndexedInterceptor()) {
823 v8::Handle<v8::Array> result =
824 GetKeysForIndexedInterceptor(object, current);
825 if (!result.IsEmpty())
826 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100827 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 }
829
Steve Blockd0582a62009-12-15 09:54:21 +0000830 // We can cache the computed property keys if access checks are
831 // not needed and no interceptors are involved.
832 //
833 // We do not use the cache if the object has elements and
834 // therefore it does not make sense to cache the property names
835 // for arguments objects. Arguments objects will always have
836 // elements.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100837 // Wrapped strings have elements, but don't have an elements
838 // array or dictionary. So the fast inline test for whether to
839 // use the cache says yes, so we should not create a cache.
Steve Blockd0582a62009-12-15 09:54:21 +0000840 bool cache_enum_keys =
841 ((current->map()->constructor() != *arguments_function) &&
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100842 !current->IsJSValue() &&
Steve Blockd0582a62009-12-15 09:54:21 +0000843 !current->IsAccessCheckNeeded() &&
844 !current->HasNamedInterceptor() &&
845 !current->HasIndexedInterceptor());
846 // Compute the property keys and cache them if possible.
847 content =
848 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
Ben Murdochf87a2032010-10-22 12:50:53 +0100849 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000850
851 // Add the property keys from the interceptor.
852 if (current->HasNamedInterceptor()) {
853 v8::Handle<v8::Array> result =
854 GetKeysForNamedInterceptor(object, current);
855 if (!result.IsEmpty())
856 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100857 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 }
859
860 // If we only want local properties we bail out after the first
861 // iteration.
862 if (type == LOCAL_ONLY)
863 break;
864 }
865 return content;
866}
867
868
869Handle<JSArray> GetKeysFor(Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100870 Isolate* isolate = object->GetIsolate();
871 isolate->counters()->for_in()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000872 Handle<FixedArray> elements = GetKeysInFixedArrayFor(object,
873 INCLUDE_PROTOS);
Steve Block44f0eee2011-05-26 01:26:41 +0100874 return isolate->factory()->NewJSArrayWithElements(elements);
Steve Blocka7e24c12009-10-30 11:49:00 +0000875}
876
877
Steve Blockd0582a62009-12-15 09:54:21 +0000878Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
879 bool cache_result) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000880 int index = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100881 Isolate* isolate = object->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000882 if (object->HasFastProperties()) {
883 if (object->map()->instance_descriptors()->HasEnumCache()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100884 isolate->counters()->enum_cache_hits()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 DescriptorArray* desc = object->map()->instance_descriptors();
Steve Block44f0eee2011-05-26 01:26:41 +0100886 return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()),
887 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 }
Steve Block44f0eee2011-05-26 01:26:41 +0100889 isolate->counters()->enum_cache_misses()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000890 int num_enum = object->NumberOfEnumProperties();
Steve Block44f0eee2011-05-26 01:26:41 +0100891 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
892 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
Steve Blocka7e24c12009-10-30 11:49:00 +0000893 Handle<DescriptorArray> descs =
Steve Block44f0eee2011-05-26 01:26:41 +0100894 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000895 for (int i = 0; i < descs->number_of_descriptors(); i++) {
896 if (descs->IsProperty(i) && !descs->IsDontEnum(i)) {
897 (*storage)->set(index, descs->GetKey(i));
898 PropertyDetails details(descs->GetDetails(i));
899 (*sort_array)->set(index, Smi::FromInt(details.index()));
900 index++;
901 }
902 }
903 (*storage)->SortPairs(*sort_array, sort_array->length());
Steve Blockd0582a62009-12-15 09:54:21 +0000904 if (cache_result) {
905 Handle<FixedArray> bridge_storage =
Steve Block44f0eee2011-05-26 01:26:41 +0100906 isolate->factory()->NewFixedArray(
907 DescriptorArray::kEnumCacheBridgeLength);
Steve Blockd0582a62009-12-15 09:54:21 +0000908 DescriptorArray* desc = object->map()->instance_descriptors();
909 desc->SetEnumCache(*bridge_storage, *storage);
910 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000911 ASSERT(storage->length() == index);
912 return storage;
913 } else {
914 int num_enum = object->NumberOfEnumProperties();
Steve Block44f0eee2011-05-26 01:26:41 +0100915 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
916 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array);
918 return storage;
919 }
920}
921
922
Leon Clarke4515c472010-02-03 11:58:03 +0000923bool EnsureCompiled(Handle<SharedFunctionInfo> shared,
924 ClearExceptionFlag flag) {
925 return shared->is_compiled() || CompileLazyShared(shared, flag);
926}
927
928
929static bool CompileLazyHelper(CompilationInfo* info,
930 ClearExceptionFlag flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 // Compile the source information to a code object.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100932 ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled());
Steve Block44f0eee2011-05-26 01:26:41 +0100933 ASSERT(!info->isolate()->has_pending_exception());
Leon Clarke4515c472010-02-03 11:58:03 +0000934 bool result = Compiler::CompileLazy(info);
Steve Block44f0eee2011-05-26 01:26:41 +0100935 ASSERT(result != Isolate::Current()->has_pending_exception());
936 if (!result && flag == CLEAR_EXCEPTION) {
937 info->isolate()->clear_pending_exception();
938 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 return result;
940}
941
942
Leon Clarke4515c472010-02-03 11:58:03 +0000943bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
944 ClearExceptionFlag flag) {
Andrei Popescu31002712010-02-23 13:46:05 +0000945 CompilationInfo info(shared);
Leon Clarke4515c472010-02-03 11:58:03 +0000946 return CompileLazyHelper(&info, flag);
947}
948
949
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100950static bool CompileLazyFunction(Handle<JSFunction> function,
951 ClearExceptionFlag flag,
952 InLoopFlag in_loop_flag) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100953 bool result = true;
Iain Merrick75681382010-08-19 15:07:18 +0100954 if (function->shared()->is_compiled()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100955 function->ReplaceCode(function->shared()->code());
Iain Merrick75681382010-08-19 15:07:18 +0100956 function->shared()->set_code_age(0);
Iain Merrick75681382010-08-19 15:07:18 +0100957 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +0100958 CompilationInfo info(function);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100959 if (in_loop_flag == IN_LOOP) info.MarkAsInLoop();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100960 result = CompileLazyHelper(&info, flag);
Ben Murdochf87a2032010-10-22 12:50:53 +0100961 ASSERT(!result || function->is_compiled());
Iain Merrick75681382010-08-19 15:07:18 +0100962 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100963 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000964}
965
966
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100967bool CompileLazy(Handle<JSFunction> function,
968 ClearExceptionFlag flag) {
969 return CompileLazyFunction(function, flag, NOT_IN_LOOP);
970}
971
972
Leon Clarke4515c472010-02-03 11:58:03 +0000973bool CompileLazyInLoop(Handle<JSFunction> function,
Leon Clarke4515c472010-02-03 11:58:03 +0000974 ClearExceptionFlag flag) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100975 return CompileLazyFunction(function, flag, IN_LOOP);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100976}
977
978
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100979bool CompileOptimized(Handle<JSFunction> function,
980 int osr_ast_id,
981 ClearExceptionFlag flag) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100982 CompilationInfo info(function);
983 info.SetOptimizing(osr_ast_id);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100984 return CompileLazyHelper(&info, flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000985}
986
Steve Blocka7e24c12009-10-30 11:49:00 +0000987} } // namespace v8::internal