blob: 20fe116dde996afe8fd1a8b8ff4ce89c82c9cd76 [file] [log] [blame]
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00001// Copyright 2012 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
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000048int HandleScope::NumberOfHandles(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
50 int n = impl->blocks()->length();
ager@chromium.orgddb913d2009-01-27 10:01:48 +000051 if (n == 0) return 0;
ager@chromium.orgc4c92722009-11-18 14:12:51 +000052 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000053 (isolate->handle_scope_data()->next - impl->blocks()->last()));
ager@chromium.orgddb913d2009-01-27 10:01:48 +000054}
55
56
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000057Object** HandleScope::Extend(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000058 v8::ImplementationUtilities::HandleScopeData* current =
59 isolate->handle_scope_data();
ager@chromium.orgddb913d2009-01-27 10:01:48 +000060
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000061 Object** result = current->next;
62
63 ASSERT(result == current->limit);
ager@chromium.org3b45ab52009-03-19 22:21:34 +000064 // Make sure there's at least one scope on the stack and that the
65 // top of the scope stack isn't a barrier.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000066 if (current->level == 0) {
ager@chromium.org3b45ab52009-03-19 22:21:34 +000067 Utils::ReportApiFailure("v8::HandleScope::CreateHandle()",
68 "Cannot create a handle without a HandleScope");
69 return NULL;
70 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000071 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
ager@chromium.org3b45ab52009-03-19 22:21:34 +000072 // If there's more room in the last block, we use that. This is used
73 // for fast creation of scopes after scope barriers.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000074 if (!impl->blocks()->is_empty()) {
75 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000076 if (current->limit != limit) {
77 current->limit = limit;
78 ASSERT(limit - current->next < kHandleBlockSize);
ager@chromium.orgddb913d2009-01-27 10:01:48 +000079 }
80 }
81
ager@chromium.org3b45ab52009-03-19 22:21:34 +000082 // If we still haven't found a slot for the handle, we extend the
83 // current handle scope by allocating a new handle block.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084 if (result == current->limit) {
ager@chromium.org3b45ab52009-03-19 22:21:34 +000085 // If there's a spare block, use it for growing the current scope.
86 result = impl->GetSpareOrNewBlock();
87 // Add the extension to the global list of blocks, but count the
88 // extension as part of the current scope.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000089 impl->blocks()->Add(result);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 current->limit = &result[kHandleBlockSize];
ager@chromium.org3b45ab52009-03-19 22:21:34 +000091 }
92
ager@chromium.orgddb913d2009-01-27 10:01:48 +000093 return result;
94}
95
96
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097void HandleScope::DeleteExtensions(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000098 v8::ImplementationUtilities::HandleScopeData* current =
99 isolate->handle_scope_data();
100 isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000101}
102
103
jkummerow@chromium.orgc3669762013-09-30 13:42:25 +0000104#ifdef ENABLE_HANDLE_ZAPPING
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000105void HandleScope::ZapRange(Object** start, Object** end) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000106 ASSERT(end - start <= kHandleBlockSize);
107 for (Object** p = start; p != end; p++) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000108 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000109 }
110}
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000111#endif
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000112
113
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000114Address HandleScope::current_level_address(Isolate* isolate) {
115 return reinterpret_cast<Address>(&isolate->handle_scope_data()->level);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000116}
117
118
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000119Address HandleScope::current_next_address(Isolate* isolate) {
120 return reinterpret_cast<Address>(&isolate->handle_scope_data()->next);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121}
122
123
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000124Address HandleScope::current_limit_address(Isolate* isolate) {
125 return reinterpret_cast<Address>(&isolate->handle_scope_data()->limit);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126}
127
128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content,
130 Handle<JSArray> array) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131 CALL_HEAP_FUNCTION(content->GetIsolate(),
132 content->AddKeysFromJSArray(*array), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135
136Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
137 Handle<FixedArray> second) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000138 CALL_HEAP_FUNCTION(first->GetIsolate(),
139 first->UnionOfKeys(*second), FixedArray);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140}
141
142
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000143Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 Handle<JSFunction> constructor,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000145 Handle<JSGlobalProxy> global) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 CALL_HEAP_FUNCTION(
147 constructor->GetIsolate(),
148 constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global),
149 JSGlobalProxy);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150}
151
152
153void SetExpectedNofProperties(Handle<JSFunction> func, int nof) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000154 // If objects constructed from this function exist then changing
ager@chromium.orgbeb25712010-11-29 08:02:25 +0000155 // 'estimated_nof_properties' is dangerous since the previous value might
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000156 // have been compiled into the fast construct stub. More over, the inobject
157 // slack tracking logic might have adjusted the previous value, so even
158 // passing the same value is risky.
159 if (func->shared()->live_objects_may_exist()) return;
160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 func->shared()->set_expected_nof_properties(nof);
162 if (func->has_initial_map()) {
163 Handle<Map> new_initial_map =
verwaest@chromium.org753aee42012-07-17 16:15:42 +0000164 func->GetIsolate()->factory()->CopyMap(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000165 Handle<Map>(func->initial_map()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 new_initial_map->set_unused_property_fields(nof);
167 func->set_initial_map(*new_initial_map);
168 }
169}
170
171
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000172static int ExpectedNofPropertiesFromEstimate(int estimate) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000173 // If no properties are added in the constructor, they are more likely
174 // to be added later.
175 if (estimate == 0) estimate = 2;
176
177 // We do not shrink objects that go into a snapshot (yet), so we adjust
178 // the estimate conservatively.
179 if (Serializer::enabled()) return estimate + 2;
180
181 // Inobject slack tracking will reclaim redundant inobject space later,
182 // so we can afford to adjust the estimate generously.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000183 if (FLAG_clever_optimizations) {
184 return estimate + 8;
185 } else {
186 return estimate + 3;
187 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000188}
189
190
191void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
192 int estimate) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000193 // See the comment in SetExpectedNofProperties.
194 if (shared->live_objects_may_exist()) return;
195
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000196 shared->set_expected_nof_properties(
197 ExpectedNofPropertiesFromEstimate(estimate));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198}
199
200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201void FlattenString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten());
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000203}
204
205
206Handle<String> FlattenGetString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000207 CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208}
209
210
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000211Handle<Object> SetProperty(Isolate* isolate,
212 Handle<Object> object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 Handle<Object> key,
214 Handle<Object> value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000215 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000216 StrictModeFlag strict_mode) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000217 CALL_HEAP_FUNCTION(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218 isolate,
219 Runtime::SetObjectProperty(
220 isolate, object, key, value, attributes, strict_mode),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000221 Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222}
223
224
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000225Handle<Object> ForceSetProperty(Handle<JSObject> object,
226 Handle<Object> key,
227 Handle<Object> value,
228 PropertyAttributes attributes) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000229 Isolate* isolate = object->GetIsolate();
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000230 CALL_HEAP_FUNCTION(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000231 isolate,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000232 Runtime::ForceSetObjectProperty(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000233 isolate, object, key, value, attributes),
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000234 Object);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000235}
236
237
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000238Handle<Object> DeleteProperty(Handle<JSObject> object, Handle<Object> key) {
239 Isolate* isolate = object->GetIsolate();
240 CALL_HEAP_FUNCTION(isolate,
241 Runtime::DeleteObjectProperty(
242 isolate, object, key, JSReceiver::NORMAL_DELETION),
243 Object);
244}
245
246
ager@chromium.orge2902be2009-06-08 12:21:35 +0000247Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
248 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249 Isolate* isolate = object->GetIsolate();
250 CALL_HEAP_FUNCTION(isolate,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000251 Runtime::DeleteObjectProperty(
252 isolate, object, key, JSReceiver::FORCE_DELETION),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 Object);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000254}
255
256
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000257Handle<Object> HasProperty(Handle<JSReceiver> obj, Handle<Object> key) {
258 Isolate* isolate = obj->GetIsolate();
259 CALL_HEAP_FUNCTION(isolate,
260 Runtime::HasObjectProperty(isolate, obj, key), Object);
261}
262
263
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000264Handle<Object> GetProperty(Handle<JSReceiver> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 const char* name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 Isolate* isolate = obj->GetIsolate();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000267 Handle<String> str = isolate->factory()->InternalizeUtf8String(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000268 CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269}
270
271
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000272Handle<Object> GetProperty(Isolate* isolate,
273 Handle<Object> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275 CALL_HEAP_FUNCTION(isolate,
276 Runtime::GetObjectProperty(isolate, obj, key), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277}
278
279
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000280Handle<Object> LookupSingleCharacterStringFromCode(Isolate* isolate,
281 uint32_t index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000282 CALL_HEAP_FUNCTION(
283 isolate,
284 isolate->heap()->LookupSingleCharacterStringFromCode(index), Object);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000285}
286
287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288// Wrappers for scripts are kept alive and cached in weak global
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000289// handles referred from foreign objects held by the scripts as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290// they are used. When they are not used anymore, the garbage
291// collector will call the weak callback on the global handle
292// associated with the wrapper and get rid of both the wrapper and the
293// handle.
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000294static void ClearWrapperCache(v8::Isolate* v8_isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000295 Persistent<v8::Value>* handle,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000296 void*) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000297 Handle<Object> cache = Utils::OpenPersistent(handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 JSValue* wrapper = JSValue::cast(*cache);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000299 Foreign* foreign = Script::cast(wrapper->value())->wrapper();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000300 ASSERT(foreign->foreign_address() ==
301 reinterpret_cast<Address>(cache.location()));
302 foreign->set_foreign_address(0);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000303 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 isolate->global_handles()->Destroy(cache.location());
305 isolate->counters()->script_wrappers()->Decrement();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306}
307
308
309Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000310 if (script->wrapper()->foreign_address() != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 // Return the script wrapper directly from the cache.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000312 return Handle<JSValue>(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000313 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000315 Isolate* isolate = script->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 // Construct a new script wrapper.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000317 isolate->counters()->script_wrappers()->Increment();
318 Handle<JSFunction> constructor = isolate->script_function();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319 Handle<JSValue> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000320 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000321
322 // The allocation might have triggered a GC, which could have called this
323 // function recursively, and a wrapper has already been created and cached.
324 // In that case, simply return the cached wrapper.
325 if (script->wrapper()->foreign_address() != NULL) {
326 return Handle<JSValue>(
327 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address()));
328 }
329
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330 result->set_value(*script);
331
332 // Create a new weak global handle and use it to cache the wrapper
333 // for future use. The cache will automatically be cleared by the
334 // garbage collector when it is not used anymore.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 Handle<Object> handle = isolate->global_handles()->Create(*result);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000336 isolate->global_handles()->MakeWeak(handle.location(),
337 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338 &ClearWrapperCache);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000339 script->wrapper()->set_foreign_address(
340 reinterpret_cast<Address>(handle.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 return result;
342}
343
344
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000345// Init line_ends array with code positions of line ends inside script
346// source.
347void InitScriptLineEnds(Handle<Script> script) {
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000348 if (!script->line_ends()->IsUndefined()) return;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000349
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 Isolate* isolate = script->GetIsolate();
351
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000352 if (!script->source()->IsString()) {
353 ASSERT(script->source()->IsUndefined());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000354 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000355 script->set_line_ends(*empty);
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000356 ASSERT(script->line_ends()->IsFixedArray());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000357 return;
358 }
359
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 Handle<String> src(String::cast(script->source()), isolate);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000361
362 Handle<FixedArray> array = CalculateLineEnds(src, true);
363
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 if (*array != isolate->heap()->empty_fixed_array()) {
365 array->set_map(isolate->heap()->fixed_cow_array_map());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000366 }
367
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000368 script->set_line_ends(*array);
369 ASSERT(script->line_ends()->IsFixedArray());
370}
371
372
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000373template <typename SourceChar>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374static void CalculateLineEnds(Isolate* isolate,
375 List<int>* line_ends,
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000376 Vector<const SourceChar> src,
377 bool with_last_line) {
378 const int src_len = src.length();
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000379 StringSearch<uint8_t, SourceChar> search(isolate, STATIC_ASCII_VECTOR("\n"));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000380
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000381 // Find and record line ends.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000382 int position = 0;
383 while (position != -1 && position < src_len) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000384 position = search.Search(src, position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000385 if (position != -1) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000386 line_ends->Add(position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000387 position++;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000388 } else if (with_last_line) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000389 // Even if the last line misses a line end, it is counted.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000390 line_ends->Add(src_len);
391 return;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000392 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000393 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000394}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000395
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000396
397Handle<FixedArray> CalculateLineEnds(Handle<String> src,
398 bool with_last_line) {
399 src = FlattenGetString(src);
400 // Rough estimate of line count based on a roughly estimated average
401 // length of (unpacked) code.
402 int line_count_estimate = src->length() >> 4;
403 List<int> line_ends(line_count_estimate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 Isolate* isolate = src->GetIsolate();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000405 {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000406 DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000407 // Dispatch on type of strings.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +0000408 String::FlatContent content = src->GetFlatContent();
409 ASSERT(content.IsFlat());
410 if (content.IsAscii()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411 CalculateLineEnds(isolate,
412 &line_ends,
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000413 content.ToOneByteVector(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000415 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 CalculateLineEnds(isolate,
417 &line_ends,
ricow@chromium.orgddd545c2011-08-24 12:02:41 +0000418 content.ToUC16Vector(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000419 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000420 }
421 }
422 int line_count = line_ends.length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000424 for (int i = 0; i < line_count; i++) {
425 array->set(i, Smi::FromInt(line_ends[i]));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000426 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000427 return array;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000428}
429
430
431// Convert code position into line number.
432int GetScriptLineNumber(Handle<Script> script, int code_pos) {
433 InitScriptLineEnds(script);
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000434 DisallowHeapAllocation no_allocation;
ager@chromium.org5c838252010-02-19 08:53:10 +0000435 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000436 const int line_ends_len = line_ends_array->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000437
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000438 if (!line_ends_len) return -1;
ager@chromium.org5c838252010-02-19 08:53:10 +0000439
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000440 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000441 return script->line_offset()->value();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000442 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000443
444 int left = 0;
445 int right = line_ends_len;
446 while (int half = (right - left) / 2) {
447 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
448 right -= half;
449 } else {
450 left += half;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000451 }
452 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000453 return right + script->line_offset()->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454}
455
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000456
danno@chromium.orgc612e022011-11-10 11:38:15 +0000457// Convert code position into column number.
458int GetScriptColumnNumber(Handle<Script> script, int code_pos) {
459 int line_number = GetScriptLineNumber(script, code_pos);
460 if (line_number == -1) return -1;
461
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000462 DisallowHeapAllocation no_allocation;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000463 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
464 line_number = line_number - script->line_offset()->value();
465 if (line_number == 0) return code_pos + script->column_offset()->value();
466 int prev_line_end_pos =
467 Smi::cast(line_ends_array->get(line_number - 1))->value();
468 return code_pos - (prev_line_end_pos + 1);
469}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000470
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000471
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000472int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000473 DisallowHeapAllocation no_allocation;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000474 if (!script->line_ends()->IsUndefined()) {
475 return GetScriptLineNumber(script, code_pos);
476 }
477 // Slow mode: we do not have line_ends. We have to iterate through source.
478 if (!script->source()->IsString()) {
479 return -1;
480 }
481 String* source = String::cast(script->source());
482 int line = 0;
483 int len = source->length();
484 for (int pos = 0; pos < len; pos++) {
485 if (pos == code_pos) {
486 break;
487 }
488 if (source->Get(pos) == '\n') {
489 line++;
490 }
491 }
492 return line;
493}
494
495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000496// Compute the property keys from the interceptor.
ulan@chromium.org750145a2013-03-07 15:14:13 +0000497// TODO(rossberg): support symbols in API, and filter here if needed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000498v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000500 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000502 PropertyCallbackArguments
503 args(isolate, interceptor->data(), *receiver, *object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 v8::Handle<v8::Array> result;
505 if (!interceptor->enumerator()->IsUndefined()) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000506 v8::NamedPropertyEnumeratorCallback enum_fun =
507 v8::ToCData<v8::NamedPropertyEnumeratorCallback>(
508 interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000510 result = args.Call(enum_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000512#if ENABLE_EXTRA_CHECKS
513 CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject());
514#endif
ulan@chromium.org837a67e2013-06-11 15:39:48 +0000515 return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate),
516 result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517}
518
519
520// Compute the element keys from the interceptor.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000521v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000525 PropertyCallbackArguments
526 args(isolate, interceptor->data(), *receiver, *object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 v8::Handle<v8::Array> result;
528 if (!interceptor->enumerator()->IsUndefined()) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000529 v8::IndexedPropertyEnumeratorCallback enum_fun =
530 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>(
531 interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000533 result = args.Call(enum_fun);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000534#if ENABLE_EXTRA_CHECKS
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000535 CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject());
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000536#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 }
ulan@chromium.org837a67e2013-06-11 15:39:48 +0000538 return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate),
539 result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540}
541
542
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000543Handle<Object> GetScriptNameOrSourceURL(Handle<Script> script) {
544 Isolate* isolate = script->GetIsolate();
545 Handle<String> name_or_source_url_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000546 isolate->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000547 STATIC_ASCII_VECTOR("nameOrSourceURL"));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000548 Handle<JSValue> script_wrapper = GetScriptWrapper(script);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000549 Handle<Object> property = GetProperty(isolate,
550 script_wrapper,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000551 name_or_source_url_key);
552 ASSERT(property->IsJSFunction());
553 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
554 bool caught_exception;
555 Handle<Object> result = Execution::TryCall(method, script_wrapper, 0,
556 NULL, &caught_exception);
557 if (caught_exception) {
558 result = isolate->factory()->undefined_value();
559 }
560 return result;
561}
562
563
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000564static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
565 int len = array->length();
566 for (int i = 0; i < len; i++) {
567 Object* e = array->get(i);
568 if (!(e->IsString() || e->IsNumber())) return false;
569 }
570 return true;
571}
572
573
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000574Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
575 KeyCollectionType type,
576 bool* threw) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000577 USE(ContainsOnlyValidKeys);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000578 Isolate* isolate = object->GetIsolate();
579 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
580 Handle<JSObject> arguments_boilerplate = Handle<JSObject>(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000581 isolate->context()->native_context()->arguments_boilerplate(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000582 isolate);
583 Handle<JSFunction> arguments_function = Handle<JSFunction>(
584 JSFunction::cast(arguments_boilerplate->map()->constructor()),
585 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 // Only collect keys if access is permitted.
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000588 for (Handle<Object> p = object;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 *p != isolate->heap()->null_value();
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000590 p = Handle<Object>(p->GetPrototype(isolate), isolate)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000591 if (p->IsJSProxy()) {
592 Handle<JSProxy> proxy(JSProxy::cast(*p), isolate);
593 Handle<Object> args[] = { proxy };
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000594 Handle<Object> names = Execution::Call(isolate,
595 isolate->proxy_enumerate(),
596 object,
597 ARRAY_SIZE(args),
598 args,
599 threw);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000600 if (*threw) return content;
601 content = AddKeysFromJSArray(content, Handle<JSArray>::cast(names));
602 break;
603 }
604
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000605 Handle<JSObject> current(JSObject::cast(*p), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000607 // Check access rights if required.
608 if (current->IsAccessCheckNeeded() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000609 !isolate->MayNamedAccess(*current,
610 isolate->heap()->undefined_value(),
611 v8::ACCESS_KEYS)) {
612 isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
danno@chromium.org169691d2013-07-15 08:01:13 +0000613 if (isolate->has_scheduled_exception()) {
614 isolate->PromoteScheduledException();
615 *threw = true;
616 }
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000617 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618 }
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000619
620 // Compute the element keys.
621 Handle<FixedArray> element_keys =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000623 current->GetEnumElementKeys(*element_keys);
624 content = UnionOfKeys(content, element_keys);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000625 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000626
627 // Add the element keys from the interceptor.
628 if (current->HasIndexedInterceptor()) {
629 v8::Handle<v8::Array> result =
630 GetKeysForIndexedInterceptor(object, current);
631 if (!result.IsEmpty())
632 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000633 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000634 }
635
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000636 // We can cache the computed property keys if access checks are
637 // not needed and no interceptors are involved.
638 //
639 // We do not use the cache if the object has elements and
640 // therefore it does not make sense to cache the property names
641 // for arguments objects. Arguments objects will always have
642 // elements.
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000643 // Wrapped strings have elements, but don't have an elements
644 // array or dictionary. So the fast inline test for whether to
645 // use the cache says yes, so we should not create a cache.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000646 bool cache_enum_keys =
647 ((current->map()->constructor() != *arguments_function) &&
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000648 !current->IsJSValue() &&
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000649 !current->IsAccessCheckNeeded() &&
650 !current->HasNamedInterceptor() &&
651 !current->HasIndexedInterceptor());
652 // Compute the property keys and cache them if possible.
653 content =
654 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000655 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000656
657 // Add the property keys from the interceptor.
658 if (current->HasNamedInterceptor()) {
659 v8::Handle<v8::Array> result =
660 GetKeysForNamedInterceptor(object, current);
661 if (!result.IsEmpty())
662 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000663 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000664 }
665
666 // If we only want local properties we bail out after the first
667 // iteration.
668 if (type == LOCAL_ONLY)
669 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 }
671 return content;
672}
673
674
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000675Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000676 Isolate* isolate = object->GetIsolate();
677 isolate->counters()->for_in()->Increment();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000678 Handle<FixedArray> elements =
679 GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000680 return isolate->factory()->NewJSArrayWithElements(elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681}
682
683
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000684Handle<FixedArray> ReduceFixedArrayTo(Handle<FixedArray> array, int length) {
685 ASSERT(array->length() >= length);
686 if (array->length() == length) return array;
687
688 Handle<FixedArray> new_array =
689 array->GetIsolate()->factory()->NewFixedArray(length);
690 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i));
691 return new_array;
692}
693
694
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000695Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
696 bool cache_result) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000697 Isolate* isolate = object->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 if (object->HasFastProperties()) {
699 if (object->map()->instance_descriptors()->HasEnumCache()) {
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000700 int own_property_count = object->map()->EnumLength();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000701 // If we have an enum cache, but the enum length of the given map is set
702 // to kInvalidEnumCache, this means that the map itself has never used the
703 // present enum cache. The first step to using the cache is to set the
704 // enum length of the map by counting the number of own descriptors that
ulan@chromium.org750145a2013-03-07 15:14:13 +0000705 // are not DONT_ENUM or SYMBOLIC.
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000706 if (own_property_count == Map::kInvalidEnumCache) {
707 own_property_count = object->map()->NumberOfDescribedProperties(
ulan@chromium.org750145a2013-03-07 15:14:13 +0000708 OWN_DESCRIPTORS, DONT_SHOW);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000709
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000710 if (cache_result) object->map()->SetEnumLength(own_property_count);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000711 }
712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 DescriptorArray* desc = object->map()->instance_descriptors();
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000714 Handle<FixedArray> keys(desc->GetEnumCache(), isolate);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000715
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000716 // In case the number of properties required in the enum are actually
717 // present, we can reuse the enum cache. Otherwise, this means that the
718 // enum cache was generated for a previous (smaller) version of the
719 // Descriptor Array. In that case we regenerate the enum cache.
720 if (own_property_count <= keys->length()) {
721 isolate->counters()->enum_cache_hits()->Increment();
722 return ReduceFixedArrayTo(keys, own_property_count);
723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724 }
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000725
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000726 Handle<Map> map(object->map());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000727
728 if (map->instance_descriptors()->IsEmpty()) {
729 isolate->counters()->enum_cache_hits()->Increment();
730 if (cache_result) map->SetEnumLength(0);
731 return isolate->factory()->empty_fixed_array();
732 }
733
734 isolate->counters()->enum_cache_misses()->Increment();
ulan@chromium.org750145a2013-03-07 15:14:13 +0000735 int num_enum = map->NumberOfDescribedProperties(ALL_DESCRIPTORS, DONT_SHOW);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000736
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000738 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(num_enum);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000739
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000740 Handle<DescriptorArray> descs =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000741 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000742
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000743 int real_size = map->NumberOfOwnDescriptors();
744 int enum_size = 0;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000745 int index = 0;
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000746
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000747 for (int i = 0; i < descs->number_of_descriptors(); i++) {
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000748 PropertyDetails details = descs->GetDetails(i);
ulan@chromium.org750145a2013-03-07 15:14:13 +0000749 Object* key = descs->GetKey(i);
750 if (!(details.IsDontEnum() || key->IsSymbol())) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000751 if (i < real_size) ++enum_size;
ulan@chromium.org750145a2013-03-07 15:14:13 +0000752 storage->set(index, key);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000753 if (!indices.is_null()) {
754 if (details.type() != FIELD) {
755 indices = Handle<FixedArray>();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000756 } else {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000757 int field_index = descs->GetFieldIndex(i);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000758 if (field_index >= map->inobject_properties()) {
759 field_index = -(field_index - map->inobject_properties() + 1);
760 }
761 indices->set(index, Smi::FromInt(field_index));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000762 }
763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 index++;
765 }
766 }
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000767 ASSERT(index == storage->length());
768
769 Handle<FixedArray> bridge_storage =
770 isolate->factory()->NewFixedArray(
771 DescriptorArray::kEnumCacheBridgeLength);
772 DescriptorArray* desc = object->map()->instance_descriptors();
773 desc->SetEnumCache(*bridge_storage,
774 *storage,
775 indices.is_null() ? Object::cast(Smi::FromInt(0))
776 : Object::cast(*indices));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000777 if (cache_result) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000778 object->map()->SetEnumLength(enum_size);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000779 }
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000780
781 return ReduceFixedArrayTo(storage, enum_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000782 } else {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000783 Handle<NameDictionary> dictionary(object->property_dictionary());
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000784
785 int length = dictionary->NumberOfElements();
786 if (length == 0) {
787 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
788 }
789
790 // The enumeration array is generated by allocating an array big enough to
791 // hold all properties that have been seen, whether they are are deleted or
792 // not. Subsequently all visible properties are added to the array. If some
793 // properties were not visible, the array is trimmed so it only contains
794 // visible properties. This improves over adding elements and sorting by
795 // index by having linear complexity rather than n*log(n).
796
797 // By comparing the monotonous NextEnumerationIndex to the NumberOfElements,
798 // we can predict the number of holes in the final array. If there will be
799 // more than 50% holes, regenerate the enumeration indices to reduce the
800 // number of holes to a minimum. This avoids allocating a large array if
801 // many properties were added but subsequently deleted.
802 int next_enumeration = dictionary->NextEnumerationIndex();
803 if (!object->IsGlobalObject() && next_enumeration > (length * 3) / 2) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000804 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000805 next_enumeration = dictionary->NextEnumerationIndex();
806 }
807
808 Handle<FixedArray> storage =
809 isolate->factory()->NewFixedArray(next_enumeration);
810
811 storage = Handle<FixedArray>(dictionary->CopyEnumKeysTo(*storage));
ulan@chromium.org750145a2013-03-07 15:14:13 +0000812 ASSERT(storage->length() == object->NumberOfLocalProperties(DONT_SHOW));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 return storage;
814 }
815}
816
817
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000818Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
819 Handle<Object> key) {
820 CALL_HEAP_FUNCTION(table->GetIsolate(),
821 table->Add(*key),
822 ObjectHashSet);
823}
824
825
826Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
827 Handle<Object> key) {
828 CALL_HEAP_FUNCTION(table->GetIsolate(),
829 table->Remove(*key),
830 ObjectHashSet);
831}
832
833
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000834Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000835 Handle<Object> key,
vegorov@chromium.org7943d462011-08-01 11:41:52 +0000836 Handle<Object> value) {
837 CALL_HEAP_FUNCTION(table->GetIsolate(),
838 table->Put(*key, *value),
839 ObjectHashTable);
840}
841
842
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000843DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
844 : impl_(isolate->handle_scope_implementer()) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000845 impl_->BeginDeferredScope();
846 v8::ImplementationUtilities::HandleScopeData* data =
847 impl_->isolate()->handle_scope_data();
848 Object** new_next = impl_->GetSpareOrNewBlock();
849 Object** new_limit = &new_next[kHandleBlockSize];
850 ASSERT(data->limit == &impl_->blocks()->last()[kHandleBlockSize]);
851 impl_->blocks()->Add(new_next);
852
853#ifdef DEBUG
854 prev_level_ = data->level;
855#endif
856 data->level++;
857 prev_limit_ = data->limit;
858 prev_next_ = data->next;
859 data->next = new_next;
860 data->limit = new_limit;
861}
862
863
864DeferredHandleScope::~DeferredHandleScope() {
865 impl_->isolate()->handle_scope_data()->level--;
866 ASSERT(handles_detached_);
867 ASSERT(impl_->isolate()->handle_scope_data()->level == prev_level_);
868}
869
870
871DeferredHandles* DeferredHandleScope::Detach() {
872 DeferredHandles* deferred = impl_->Detach(prev_limit_);
873 v8::ImplementationUtilities::HandleScopeData* data =
874 impl_->isolate()->handle_scope_data();
875 data->next = prev_next_;
876 data->limit = prev_limit_;
877#ifdef DEBUG
878 handles_detached_ = true;
879#endif
880 return deferred;
881}
882
883
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +0000884void AddWeakObjectToCodeDependency(Heap* heap,
885 Handle<Object> object,
886 Handle<Code> code) {
887 heap->EnsureWeakObjectToCodeTable();
888 Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(*object));
889 dep = DependentCode::Insert(dep, DependentCode::kWeaklyEmbeddedGroup, code);
890 CALL_HEAP_FUNCTION_VOID(heap->isolate(),
891 heap->AddWeakObjectToCodeDependency(*object, *dep));
892}
893
894
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895} } // namespace v8::internal