blob: c9984aa92f3d8c7620dbdfe762cbf06817235ed7 [file] [log] [blame]
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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 "accessors.h"
31#include "api.h"
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000032#include "arguments.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#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"
vegorov@chromium.org21b5e952010-11-23 10:24:40 +000040#include "string-search.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000041#include "stub-cache.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
ager@chromium.orgddb913d2009-01-27 10:01:48 +000048int HandleScope::NumberOfHandles() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 Isolate* isolate = Isolate::Current();
50 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
51 int n = impl->blocks()->length();
ager@chromium.orgddb913d2009-01-27 10:01:48 +000052 if (n == 0) return 0;
ager@chromium.orgc4c92722009-11-18 14:12:51 +000053 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054 (isolate->handle_scope_data()->next - impl->blocks()->last()));
ager@chromium.orgddb913d2009-01-27 10:01:48 +000055}
56
57
ager@chromium.org18ad94b2009-09-02 08:22:29 +000058Object** HandleScope::Extend() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000059 Isolate* isolate = Isolate::Current();
60 v8::ImplementationUtilities::HandleScopeData* current =
61 isolate->handle_scope_data();
ager@chromium.orgddb913d2009-01-27 10:01:48 +000062
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000063 Object** result = current->next;
64
65 ASSERT(result == current->limit);
ager@chromium.org3b45ab52009-03-19 22:21:34 +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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 if (current->level == 0) {
ager@chromium.org3b45ab52009-03-19 22:21:34 +000069 Utils::ReportApiFailure("v8::HandleScope::CreateHandle()",
70 "Cannot create a handle without a HandleScope");
71 return NULL;
72 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
ager@chromium.org3b45ab52009-03-19 22:21:34 +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.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000076 if (!impl->blocks()->is_empty()) {
77 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078 if (current->limit != limit) {
79 current->limit = limit;
80 ASSERT(limit - current->next < kHandleBlockSize);
ager@chromium.orgddb913d2009-01-27 10:01:48 +000081 }
82 }
83
ager@chromium.org3b45ab52009-03-19 22:21:34 +000084 // If we still haven't found a slot for the handle, we extend the
85 // current handle scope by allocating a new handle block.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 if (result == current->limit) {
ager@chromium.org3b45ab52009-03-19 22:21:34 +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.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000091 impl->blocks()->Add(result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 current->limit = &result[kHandleBlockSize];
ager@chromium.org3b45ab52009-03-19 22:21:34 +000093 }
94
ager@chromium.orgddb913d2009-01-27 10:01:48 +000095 return result;
96}
97
98
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099void 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);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000104}
105
106
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000107void HandleScope::ZapRange(Object** start, Object** end) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000108 ASSERT(end - start <= kHandleBlockSize);
109 for (Object** p = start; p != end; p++) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000110 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000111 }
112}
113
114
lrn@chromium.org303ada72010-10-27 09:33:13 +0000115Address HandleScope::current_level_address() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 return reinterpret_cast<Address>(
117 &Isolate::Current()->handle_scope_data()->level);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000118}
119
120
121Address HandleScope::current_next_address() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000122 return reinterpret_cast<Address>(
123 &Isolate::Current()->handle_scope_data()->next);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000124}
125
126
127Address HandleScope::current_limit_address() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 return reinterpret_cast<Address>(
129 &Isolate::Current()->handle_scope_data()->limit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000130}
131
132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content,
134 Handle<JSArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000135 CALL_HEAP_FUNCTION(content->GetIsolate(),
136 content->AddKeysFromJSArray(*array), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
141 Handle<FixedArray> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 CALL_HEAP_FUNCTION(first->GetIsolate(),
143 first->UnionOfKeys(*second), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144}
145
146
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000147Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 Handle<JSFunction> constructor,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000149 Handle<JSGlobalProxy> global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 CALL_HEAP_FUNCTION(
151 constructor->GetIsolate(),
152 constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global),
153 JSGlobalProxy);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154}
155
156
157void SetExpectedNofProperties(Handle<JSFunction> func, int nof) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000158 // If objects constructed from this function exist then changing
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000159 // 'estimated_nof_properties' is dangerous since the previous value might
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000160 // 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 func->shared()->set_expected_nof_properties(nof);
166 if (func->has_initial_map()) {
167 Handle<Map> new_initial_map =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 func->GetIsolate()->factory()->CopyMapDropTransitions(
169 Handle<Map>(func->initial_map()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 CALL_HEAP_FUNCTION_VOID(func->GetIsolate(),
178 func->SetPrototype(*value));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179}
180
181
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000182static int ExpectedNofPropertiesFromEstimate(int estimate) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000183 // 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.
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +0000193 return estimate + 8;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000194}
195
196
197void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
198 int estimate) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000199 // See the comment in SetExpectedNofProperties.
200 if (shared->live_objects_may_exist()) return;
201
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000202 shared->set_expected_nof_properties(
203 ExpectedNofPropertiesFromEstimate(estimate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204}
205
206
ager@chromium.org32912102009-01-16 10:38:43 +0000207void NormalizeProperties(Handle<JSObject> object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000208 PropertyNormalizationMode mode,
209 int expected_additional_properties) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 CALL_HEAP_FUNCTION_VOID(object->GetIsolate(),
211 object->NormalizeProperties(
212 mode,
213 expected_additional_properties));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214}
215
216
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000217Handle<NumberDictionary> NormalizeElements(Handle<JSObject> object) {
218 CALL_HEAP_FUNCTION(object->GetIsolate(),
219 object->NormalizeElements(),
220 NumberDictionary);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221}
222
223
224void TransformToFastProperties(Handle<JSObject> object,
225 int unused_property_fields) {
226 CALL_HEAP_FUNCTION_VOID(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 object->GetIsolate(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 object->TransformToFastProperties(unused_property_fields));
229}
230
231
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000232Handle<NumberDictionary> NumberDictionarySet(
233 Handle<NumberDictionary> dictionary,
234 uint32_t index,
235 Handle<Object> value,
236 PropertyDetails details) {
237 CALL_HEAP_FUNCTION(dictionary->GetIsolate(),
238 dictionary->Set(index, *value, details),
239 NumberDictionary);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000240}
241
242
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243void FlattenString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244 CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten());
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000245}
246
247
248Handle<String> FlattenGetString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249 CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
253Handle<Object> SetPrototype(Handle<JSFunction> function,
254 Handle<Object> prototype) {
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000255 ASSERT(function->should_have_prototype());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000256 CALL_HEAP_FUNCTION(function->GetIsolate(),
257 Accessors::FunctionSetPrototype(*function,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000258 *prototype,
259 NULL),
260 Object);
261}
262
263
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000264Handle<Object> SetProperty(Handle<JSReceiver> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 Handle<String> key,
266 Handle<Object> value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000267 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000268 StrictModeFlag strict_mode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269 CALL_HEAP_FUNCTION(object->GetIsolate(),
270 object->SetProperty(*key, *value, attributes, strict_mode),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000271 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272}
273
274
275Handle<Object> SetProperty(Handle<Object> object,
276 Handle<Object> key,
277 Handle<Object> value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000278 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000279 StrictModeFlag strict_mode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 Isolate* isolate = Isolate::Current();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000281 CALL_HEAP_FUNCTION(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 isolate,
283 Runtime::SetObjectProperty(
284 isolate, object, key, value, attributes, strict_mode),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000285 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286}
287
288
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000289Handle<Object> ForceSetProperty(Handle<JSObject> object,
290 Handle<Object> key,
291 Handle<Object> value,
292 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 Isolate* isolate = object->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000294 CALL_HEAP_FUNCTION(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000295 isolate,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000296 Runtime::ForceSetObjectProperty(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 isolate, object, key, value, attributes),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000298 Object);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000299}
300
301
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000302Handle<Object> SetNormalizedProperty(Handle<JSObject> object,
303 Handle<String> key,
304 Handle<Object> value,
305 PropertyDetails details) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306 CALL_HEAP_FUNCTION(object->GetIsolate(),
307 object->SetNormalizedProperty(*key, *value, details),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000308 Object);
309}
310
311
ager@chromium.orge2902be2009-06-08 12:21:35 +0000312Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
313 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000314 Isolate* isolate = object->GetIsolate();
315 CALL_HEAP_FUNCTION(isolate,
316 Runtime::ForceDeleteObjectProperty(isolate, object, key),
317 Object);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000318}
319
320
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000321Handle<Object> SetLocalPropertyIgnoreAttributes(
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000322 Handle<JSObject> object,
323 Handle<String> key,
324 Handle<Object> value,
325 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 CALL_HEAP_FUNCTION(
327 object->GetIsolate(),
328 object->SetLocalPropertyIgnoreAttributes(*key, *value, attributes),
329 Object);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000330}
331
ager@chromium.orge2902be2009-06-08 12:21:35 +0000332
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000333void SetLocalPropertyNoThrow(Handle<JSObject> object,
334 Handle<String> key,
335 Handle<Object> value,
336 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 Isolate* isolate = object->GetIsolate();
338 ASSERT(!isolate->has_pending_exception());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000339 CHECK(!SetLocalPropertyIgnoreAttributes(
340 object, key, value, attributes).is_null());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 CHECK(!isolate->has_pending_exception());
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000342}
343
344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000345Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
346 Handle<String> key,
347 Handle<Object> value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000348 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000349 StrictModeFlag strict_mode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 CALL_HEAP_FUNCTION(object->GetIsolate(),
351 object->SetPropertyWithInterceptor(*key,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 *value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000353 attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000354 strict_mode),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 Object);
356}
357
358
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000359Handle<Object> GetProperty(Handle<JSReceiver> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 const char* name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 Isolate* isolate = obj->GetIsolate();
362 Handle<String> str = isolate->factory()->LookupAsciiSymbol(name);
363 CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364}
365
366
367Handle<Object> GetProperty(Handle<Object> obj,
368 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 Isolate* isolate = Isolate::Current();
370 CALL_HEAP_FUNCTION(isolate,
371 Runtime::GetObjectProperty(isolate, obj, key), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372}
373
374
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000375Handle<Object> GetProperty(Handle<JSReceiver> obj,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000376 Handle<String> name,
377 LookupResult* result) {
378 PropertyAttributes attributes;
379 Isolate* isolate = Isolate::Current();
380 CALL_HEAP_FUNCTION(isolate,
381 obj->GetProperty(*obj, result, *name, &attributes),
382 Object);
383}
384
385
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000386Handle<Object> GetElement(Handle<Object> obj,
387 uint32_t index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 Isolate* isolate = Isolate::Current();
389 CALL_HEAP_FUNCTION(isolate, Runtime::GetElement(obj, index), Object);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000390}
391
392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
394 Handle<JSObject> holder,
395 Handle<String> name,
396 PropertyAttributes* attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397 Isolate* isolate = receiver->GetIsolate();
398 CALL_HEAP_FUNCTION(isolate,
399 holder->GetPropertyWithInterceptor(*receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 *name,
401 attributes),
402 Object);
403}
404
405
406Handle<Object> GetPrototype(Handle<Object> obj) {
407 Handle<Object> result(obj->GetPrototype());
408 return result;
409}
410
411
ager@chromium.org5c838252010-02-19 08:53:10 +0000412Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) {
413 const bool skip_hidden_prototypes = false;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 CALL_HEAP_FUNCTION(obj->GetIsolate(),
415 obj->SetPrototype(*value, skip_hidden_prototypes), Object);
416}
417
418
419Handle<Object> PreventExtensions(Handle<JSObject> object) {
420 CALL_HEAP_FUNCTION(object->GetIsolate(), object->PreventExtensions(), Object);
ager@chromium.org5c838252010-02-19 08:53:10 +0000421}
422
423
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000424Handle<Object> GetHiddenProperties(Handle<JSObject> obj,
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000425 JSObject::HiddenPropertiesFlag flag) {
426 CALL_HEAP_FUNCTION(obj->GetIsolate(),
427 obj->GetHiddenProperties(flag),
428 Object);
429}
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000430
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000431
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000432int GetIdentityHash(Handle<JSObject> obj) {
433 CALL_AND_RETRY(obj->GetIsolate(),
434 obj->GetIdentityHash(JSObject::ALLOW_CREATION),
435 return Smi::cast(__object__)->value(),
436 return 0);
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000437}
438
439
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440Handle<Object> DeleteElement(Handle<JSObject> obj,
441 uint32_t index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 CALL_HEAP_FUNCTION(obj->GetIsolate(),
443 obj->DeleteElement(index, JSObject::NORMAL_DELETION),
ager@chromium.orge2902be2009-06-08 12:21:35 +0000444 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
448Handle<Object> DeleteProperty(Handle<JSObject> obj,
449 Handle<String> prop) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 CALL_HEAP_FUNCTION(obj->GetIsolate(),
451 obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION),
ager@chromium.orge2902be2009-06-08 12:21:35 +0000452 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000453}
454
455
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000456Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000457 Isolate* isolate = Isolate::Current();
458 CALL_HEAP_FUNCTION(
459 isolate,
460 isolate->heap()->LookupSingleCharacterStringFromCode(index), Object);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000461}
462
463
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000464Handle<String> SubString(Handle<String> str,
465 int start,
466 int end,
467 PretenureFlag pretenure) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000468 CALL_HEAP_FUNCTION(str->GetIsolate(),
469 str->SubString(start, end, pretenure), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470}
471
472
473Handle<Object> SetElement(Handle<JSObject> object,
474 uint32_t index,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000475 Handle<Object> value,
476 StrictModeFlag strict_mode) {
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000477 if (object->HasExternalArrayElements()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000478 if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) {
479 bool has_exception;
480 Handle<Object> number = Execution::ToNumber(value, &has_exception);
481 if (has_exception) return Handle<Object>();
482 value = number;
483 }
484 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000485 CALL_HEAP_FUNCTION(object->GetIsolate(),
whesse@chromium.org7b260152011-06-20 15:33:18 +0000486 object->SetElement(index, *value, strict_mode, true),
487 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488}
489
490
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000491Handle<Object> SetOwnElement(Handle<JSObject> object,
492 uint32_t index,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000493 Handle<Object> value,
494 StrictModeFlag strict_mode) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000495 ASSERT(!object->HasExternalArrayElements());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000496 CALL_HEAP_FUNCTION(object->GetIsolate(),
497 object->SetElement(index, *value, strict_mode, false),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000498 Object);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000499}
500
501
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000502Handle<JSObject> Copy(Handle<JSObject> obj) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000503 Isolate* isolate = obj->GetIsolate();
504 CALL_HEAP_FUNCTION(isolate,
505 isolate->heap()->CopyJSObject(*obj), JSObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506}
507
508
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000509Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000510 CALL_HEAP_FUNCTION(obj->GetIsolate(), obj->DefineAccessor(*info), Object);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514// Wrappers for scripts are kept alive and cached in weak global
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000515// handles referred from foreign objects held by the scripts as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516// they are used. When they are not used anymore, the garbage
517// collector will call the weak callback on the global handle
518// associated with the wrapper and get rid of both the wrapper and the
519// handle.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000520static void ClearWrapperCache(Persistent<v8::Value> handle, void*) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 Handle<Object> cache = Utils::OpenHandle(*handle);
522 JSValue* wrapper = JSValue::cast(*cache);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000523 Foreign* foreign = Script::cast(wrapper->value())->wrapper();
524 ASSERT(foreign->address() == reinterpret_cast<Address>(cache.location()));
525 foreign->set_address(0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000526 Isolate* isolate = Isolate::Current();
527 isolate->global_handles()->Destroy(cache.location());
528 isolate->counters()->script_wrappers()->Decrement();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529}
530
531
532Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000533 if (script->wrapper()->address() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 // Return the script wrapper directly from the cache.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000535 return Handle<JSValue>(
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000536 reinterpret_cast<JSValue**>(script->wrapper()->address()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000538 Isolate* isolate = Isolate::Current();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 // Construct a new script wrapper.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000540 isolate->counters()->script_wrappers()->Increment();
541 Handle<JSFunction> constructor = isolate->script_function();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542 Handle<JSValue> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 result->set_value(*script);
545
546 // Create a new weak global handle and use it to cache the wrapper
547 // for future use. The cache will automatically be cleared by the
548 // garbage collector when it is not used anymore.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000549 Handle<Object> handle = isolate->global_handles()->Create(*result);
550 isolate->global_handles()->MakeWeak(handle.location(), NULL,
551 &ClearWrapperCache);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000552 script->wrapper()->set_address(reinterpret_cast<Address>(handle.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 return result;
554}
555
556
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000557// Init line_ends array with code positions of line ends inside script
558// source.
559void InitScriptLineEnds(Handle<Script> script) {
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000560 if (!script->line_ends()->IsUndefined()) return;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000562 Isolate* isolate = script->GetIsolate();
563
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000564 if (!script->source()->IsString()) {
565 ASSERT(script->source()->IsUndefined());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000567 script->set_line_ends(*empty);
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000568 ASSERT(script->line_ends()->IsFixedArray());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 return;
570 }
571
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000572 Handle<String> src(String::cast(script->source()), isolate);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000573
574 Handle<FixedArray> array = CalculateLineEnds(src, true);
575
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000576 if (*array != isolate->heap()->empty_fixed_array()) {
577 array->set_map(isolate->heap()->fixed_cow_array_map());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000578 }
579
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000580 script->set_line_ends(*array);
581 ASSERT(script->line_ends()->IsFixedArray());
582}
583
584
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000585template <typename SourceChar>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000586static void CalculateLineEnds(Isolate* isolate,
587 List<int>* line_ends,
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000588 Vector<const SourceChar> src,
589 bool with_last_line) {
590 const int src_len = src.length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000591 StringSearch<char, SourceChar> search(isolate, CStrVector("\n"));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000592
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000593 // Find and record line ends.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000594 int position = 0;
595 while (position != -1 && position < src_len) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000596 position = search.Search(src, position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000597 if (position != -1) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000598 line_ends->Add(position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000599 position++;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000600 } else if (with_last_line) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000601 // Even if the last line misses a line end, it is counted.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000602 line_ends->Add(src_len);
603 return;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000604 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000605 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000606}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000607
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000608
609Handle<FixedArray> CalculateLineEnds(Handle<String> src,
610 bool with_last_line) {
611 src = FlattenGetString(src);
612 // Rough estimate of line count based on a roughly estimated average
613 // length of (unpacked) code.
614 int line_count_estimate = src->length() >> 4;
615 List<int> line_ends(line_count_estimate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000616 Isolate* isolate = src->GetIsolate();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000617 {
618 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid.
619 // Dispatch on type of strings.
620 if (src->IsAsciiRepresentation()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000621 CalculateLineEnds(isolate,
622 &line_ends,
623 src->ToAsciiVector(),
624 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000625 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000626 CalculateLineEnds(isolate,
627 &line_ends,
628 src->ToUC16Vector(),
629 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000630 }
631 }
632 int line_count = line_ends.length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000633 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000634 for (int i = 0; i < line_count; i++) {
635 array->set(i, Smi::FromInt(line_ends[i]));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000636 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000637 return array;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000638}
639
640
641// Convert code position into line number.
642int GetScriptLineNumber(Handle<Script> script, int code_pos) {
643 InitScriptLineEnds(script);
644 AssertNoAllocation no_allocation;
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000646 const int line_ends_len = line_ends_array->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000648 if (!line_ends_len) return -1;
ager@chromium.org5c838252010-02-19 08:53:10 +0000649
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000650 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000651 return script->line_offset()->value();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000652 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000653
654 int left = 0;
655 int right = line_ends_len;
656 while (int half = (right - left) / 2) {
657 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
658 right -= half;
659 } else {
660 left += half;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000661 }
662 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000663 return right + script->line_offset()->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000664}
665
666
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000667int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
668 AssertNoAllocation no_allocation;
669 if (!script->line_ends()->IsUndefined()) {
670 return GetScriptLineNumber(script, code_pos);
671 }
672 // Slow mode: we do not have line_ends. We have to iterate through source.
673 if (!script->source()->IsString()) {
674 return -1;
675 }
676 String* source = String::cast(script->source());
677 int line = 0;
678 int len = source->length();
679 for (int pos = 0; pos < len; pos++) {
680 if (pos == code_pos) {
681 break;
682 }
683 if (source->Get(pos) == '\n') {
684 line++;
685 }
686 }
687 return line;
688}
689
690
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000691void CustomArguments::IterateInstance(ObjectVisitor* v) {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000692 v->VisitPointers(values_, values_ + ARRAY_SIZE(values_));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000693}
694
695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696// Compute the property keys from the interceptor.
697v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
698 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000699 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000701 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000702 v8::AccessorInfo info(args.end());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703 v8::Handle<v8::Array> result;
704 if (!interceptor->enumerator()->IsUndefined()) {
705 v8::NamedPropertyEnumerator enum_fun =
706 v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000707 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 {
709 // Leaving JavaScript.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000710 VMState state(isolate, EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 result = enum_fun(info);
712 }
713 }
714 return result;
715}
716
717
718// Compute the element keys from the interceptor.
719v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
720 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000721 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000723 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000724 v8::AccessorInfo info(args.end());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 v8::Handle<v8::Array> result;
726 if (!interceptor->enumerator()->IsUndefined()) {
727 v8::IndexedPropertyEnumerator enum_fun =
728 v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000729 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730 {
731 // Leaving JavaScript.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000732 VMState state(isolate, EXTERNAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 result = enum_fun(info);
734 }
735 }
736 return result;
737}
738
739
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000740static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
741 int len = array->length();
742 for (int i = 0; i < len; i++) {
743 Object* e = array->get(i);
744 if (!(e->IsString() || e->IsNumber())) return false;
745 }
746 return true;
747}
748
749
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000750Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object,
751 KeyCollectionType type) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000752 USE(ContainsOnlyValidKeys);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000753 Isolate* isolate = object->GetIsolate();
754 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
755 Handle<JSObject> arguments_boilerplate = Handle<JSObject>(
756 isolate->context()->global_context()->arguments_boilerplate(),
757 isolate);
758 Handle<JSFunction> arguments_function = Handle<JSFunction>(
759 JSFunction::cast(arguments_boilerplate->map()->constructor()),
760 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 // Only collect keys if access is permitted.
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000763 for (Handle<Object> p = object;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000764 *p != isolate->heap()->null_value();
765 p = Handle<Object>(p->GetPrototype(), isolate)) {
766 Handle<JSObject> current(JSObject::cast(*p), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000768 // Check access rights if required.
769 if (current->IsAccessCheckNeeded() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000770 !isolate->MayNamedAccess(*current,
771 isolate->heap()->undefined_value(),
772 v8::ACCESS_KEYS)) {
773 isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000774 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775 }
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000776
777 // Compute the element keys.
778 Handle<FixedArray> element_keys =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000779 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000780 current->GetEnumElementKeys(*element_keys);
781 content = UnionOfKeys(content, element_keys);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000782 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000783
784 // Add the element keys from the interceptor.
785 if (current->HasIndexedInterceptor()) {
786 v8::Handle<v8::Array> result =
787 GetKeysForIndexedInterceptor(object, current);
788 if (!result.IsEmpty())
789 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000790 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000791 }
792
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000793 // We can cache the computed property keys if access checks are
794 // not needed and no interceptors are involved.
795 //
796 // We do not use the cache if the object has elements and
797 // therefore it does not make sense to cache the property names
798 // for arguments objects. Arguments objects will always have
799 // elements.
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000800 // Wrapped strings have elements, but don't have an elements
801 // array or dictionary. So the fast inline test for whether to
802 // use the cache says yes, so we should not create a cache.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000803 bool cache_enum_keys =
804 ((current->map()->constructor() != *arguments_function) &&
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000805 !current->IsJSValue() &&
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000806 !current->IsAccessCheckNeeded() &&
807 !current->HasNamedInterceptor() &&
808 !current->HasIndexedInterceptor());
809 // Compute the property keys and cache them if possible.
810 content =
811 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000812 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000813
814 // Add the property keys from the interceptor.
815 if (current->HasNamedInterceptor()) {
816 v8::Handle<v8::Array> result =
817 GetKeysForNamedInterceptor(object, current);
818 if (!result.IsEmpty())
819 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000820 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000821 }
822
823 // If we only want local properties we bail out after the first
824 // iteration.
825 if (type == LOCAL_ONLY)
826 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 }
828 return content;
829}
830
831
832Handle<JSArray> GetKeysFor(Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000833 Isolate* isolate = object->GetIsolate();
834 isolate->counters()->for_in()->Increment();
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000835 Handle<FixedArray> elements = GetKeysInFixedArrayFor(object,
836 INCLUDE_PROTOS);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000837 return isolate->factory()->NewJSArrayWithElements(elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838}
839
840
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000841Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
842 bool cache_result) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 int index = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000844 Isolate* isolate = object->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 if (object->HasFastProperties()) {
846 if (object->map()->instance_descriptors()->HasEnumCache()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000847 isolate->counters()->enum_cache_hits()->Increment();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 DescriptorArray* desc = object->map()->instance_descriptors();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000849 return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()),
850 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 isolate->counters()->enum_cache_misses()->Increment();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853 int num_enum = object->NumberOfEnumProperties();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000854 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
855 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000856 Handle<DescriptorArray> descs =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000857 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000858 for (int i = 0; i < descs->number_of_descriptors(); i++) {
859 if (descs->IsProperty(i) && !descs->IsDontEnum(i)) {
860 (*storage)->set(index, descs->GetKey(i));
861 PropertyDetails details(descs->GetDetails(i));
862 (*sort_array)->set(index, Smi::FromInt(details.index()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 index++;
864 }
865 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000866 (*storage)->SortPairs(*sort_array, sort_array->length());
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000867 if (cache_result) {
868 Handle<FixedArray> bridge_storage =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000869 isolate->factory()->NewFixedArray(
870 DescriptorArray::kEnumCacheBridgeLength);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000871 DescriptorArray* desc = object->map()->instance_descriptors();
872 desc->SetEnumCache(*bridge_storage, *storage);
873 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874 ASSERT(storage->length() == index);
875 return storage;
876 } else {
877 int num_enum = object->NumberOfEnumProperties();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
879 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array);
881 return storage;
882 }
883}
884
885
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000886Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
887 Handle<JSObject> key,
888 Handle<Object> value) {
889 CALL_HEAP_FUNCTION(table->GetIsolate(),
890 table->Put(*key, *value),
891 ObjectHashTable);
892}
893
894
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000895bool EnsureCompiled(Handle<SharedFunctionInfo> shared,
896 ClearExceptionFlag flag) {
897 return shared->is_compiled() || CompileLazyShared(shared, flag);
898}
899
900
901static bool CompileLazyHelper(CompilationInfo* info,
902 ClearExceptionFlag flag) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // Compile the source information to a code object.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000904 ASSERT(info->IsOptimizing() || !info->shared_info()->is_compiled());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 ASSERT(!info->isolate()->has_pending_exception());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000906 bool result = Compiler::CompileLazy(info);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000907 ASSERT(result != Isolate::Current()->has_pending_exception());
908 if (!result && flag == CLEAR_EXCEPTION) {
909 info->isolate()->clear_pending_exception();
910 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 return result;
912}
913
914
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000915bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
916 ClearExceptionFlag flag) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000917 CompilationInfo info(shared);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000918 return CompileLazyHelper(&info, flag);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919}
920
921
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000922static bool CompileLazyFunction(Handle<JSFunction> function,
923 ClearExceptionFlag flag,
924 InLoopFlag in_loop_flag) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000925 bool result = true;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000926 if (function->shared()->is_compiled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000927 function->ReplaceCode(function->shared()->code());
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000928 function->shared()->set_code_age(0);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000929 } else {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000930 CompilationInfo info(function);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000931 if (in_loop_flag == IN_LOOP) info.MarkAsInLoop();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000932 result = CompileLazyHelper(&info, flag);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000933 ASSERT(!result || function->is_compiled());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000934 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000935 return result;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000936}
937
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000938
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000939bool CompileLazy(Handle<JSFunction> function,
940 ClearExceptionFlag flag) {
941 return CompileLazyFunction(function, flag, NOT_IN_LOOP);
942}
943
944
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000945bool CompileLazyInLoop(Handle<JSFunction> function,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000946 ClearExceptionFlag flag) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000947 return CompileLazyFunction(function, flag, IN_LOOP);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000948}
949
950
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000951bool CompileOptimized(Handle<JSFunction> function,
952 int osr_ast_id,
953 ClearExceptionFlag flag) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000954 CompilationInfo info(function);
955 info.SetOptimizing(osr_ast_id);
vegorov@chromium.org5d6c1f52011-02-28 13:13:38 +0000956 return CompileLazyHelper(&info, flag);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000957}
958
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959} } // namespace v8::internal