blob: 2d414022e09203e8a1034ee24fd55ca49f699063 [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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153void FlattenString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000154 CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten());
lrn@chromium.org32d961d2010-06-30 09:09:34 +0000155}
156
157
158Handle<String> FlattenGetString(Handle<String> string) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000159 CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160}
161
162
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000163Handle<Object> ForceSetProperty(Handle<JSObject> object,
164 Handle<Object> key,
165 Handle<Object> value,
166 PropertyAttributes attributes) {
machenbach@chromium.orge8412be2013-11-08 10:23:52 +0000167 return Runtime::ForceSetObjectProperty(object->GetIsolate(), object, key,
168 value, attributes);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000169}
170
171
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000172Handle<Object> DeleteProperty(Handle<JSObject> object, Handle<Object> key) {
173 Isolate* isolate = object->GetIsolate();
174 CALL_HEAP_FUNCTION(isolate,
175 Runtime::DeleteObjectProperty(
176 isolate, object, key, JSReceiver::NORMAL_DELETION),
177 Object);
178}
179
180
ager@chromium.orge2902be2009-06-08 12:21:35 +0000181Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
182 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000183 Isolate* isolate = object->GetIsolate();
184 CALL_HEAP_FUNCTION(isolate,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000185 Runtime::DeleteObjectProperty(
186 isolate, object, key, JSReceiver::FORCE_DELETION),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000187 Object);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000188}
189
190
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000191Handle<Object> HasProperty(Handle<JSReceiver> obj, Handle<Object> key) {
192 Isolate* isolate = obj->GetIsolate();
193 CALL_HEAP_FUNCTION(isolate,
194 Runtime::HasObjectProperty(isolate, obj, key), Object);
195}
196
197
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000198Handle<Object> GetProperty(Handle<JSReceiver> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 const char* name) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 Isolate* isolate = obj->GetIsolate();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000201 Handle<String> str = isolate->factory()->InternalizeUtf8String(name);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203}
204
205
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000206Handle<Object> GetProperty(Isolate* isolate,
207 Handle<Object> obj,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 Handle<Object> key) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000209 CALL_HEAP_FUNCTION(isolate,
210 Runtime::GetObjectProperty(isolate, obj, key), Object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211}
212
213
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000214Handle<Object> LookupSingleCharacterStringFromCode(Isolate* isolate,
215 uint32_t index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 CALL_HEAP_FUNCTION(
217 isolate,
218 isolate->heap()->LookupSingleCharacterStringFromCode(index), Object);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000219}
220
221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222// Wrappers for scripts are kept alive and cached in weak global
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000223// handles referred from foreign objects held by the scripts as long as
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224// they are used. When they are not used anymore, the garbage
225// collector will call the weak callback on the global handle
226// associated with the wrapper and get rid of both the wrapper and the
227// handle.
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000228static void ClearWrapperCache(v8::Isolate* v8_isolate,
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000229 Persistent<v8::Value>* handle,
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000230 void*) {
danno@chromium.orgf95d4b92013-06-13 14:40:17 +0000231 Handle<Object> cache = Utils::OpenPersistent(handle);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232 JSValue* wrapper = JSValue::cast(*cache);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000233 Foreign* foreign = Script::cast(wrapper->value())->wrapper();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000234 ASSERT(foreign->foreign_address() ==
235 reinterpret_cast<Address>(cache.location()));
236 foreign->set_foreign_address(0);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000237 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238 isolate->global_handles()->Destroy(cache.location());
239 isolate->counters()->script_wrappers()->Decrement();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240}
241
242
243Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000244 if (script->wrapper()->foreign_address() != NULL) {
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000245 // Return a handle for the existing script wrapper from the cache.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000246 return Handle<JSValue>(
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000247 *reinterpret_cast<JSValue**>(script->wrapper()->foreign_address()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 }
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000249 Isolate* isolate = script->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 // Construct a new script wrapper.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000251 isolate->counters()->script_wrappers()->Increment();
252 Handle<JSFunction> constructor = isolate->script_function();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253 Handle<JSValue> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000255
256 // The allocation might have triggered a GC, which could have called this
257 // function recursively, and a wrapper has already been created and cached.
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000258 // In that case, simply return a handle for the cached wrapper.
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000259 if (script->wrapper()->foreign_address() != NULL) {
260 return Handle<JSValue>(
machenbach@chromium.org935a7792013-11-12 09:05:18 +0000261 *reinterpret_cast<JSValue**>(script->wrapper()->foreign_address()));
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000262 }
263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 result->set_value(*script);
265
266 // Create a new weak global handle and use it to cache the wrapper
267 // for future use. The cache will automatically be cleared by the
268 // garbage collector when it is not used anymore.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269 Handle<Object> handle = isolate->global_handles()->Create(*result);
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000270 isolate->global_handles()->MakeWeak(handle.location(),
271 NULL,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000272 &ClearWrapperCache);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000273 script->wrapper()->set_foreign_address(
274 reinterpret_cast<Address>(handle.location()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 return result;
276}
277
278
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000279// Init line_ends array with code positions of line ends inside script
280// source.
281void InitScriptLineEnds(Handle<Script> script) {
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000282 if (!script->line_ends()->IsUndefined()) return;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000283
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 Isolate* isolate = script->GetIsolate();
285
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000286 if (!script->source()->IsString()) {
287 ASSERT(script->source()->IsUndefined());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000289 script->set_line_ends(*empty);
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +0000290 ASSERT(script->line_ends()->IsFixedArray());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000291 return;
292 }
293
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000294 Handle<String> src(String::cast(script->source()), isolate);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000295
296 Handle<FixedArray> array = CalculateLineEnds(src, true);
297
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298 if (*array != isolate->heap()->empty_fixed_array()) {
299 array->set_map(isolate->heap()->fixed_cow_array_map());
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +0000300 }
301
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000302 script->set_line_ends(*array);
303 ASSERT(script->line_ends()->IsFixedArray());
304}
305
306
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000307template <typename SourceChar>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000308static void CalculateLineEnds(Isolate* isolate,
309 List<int>* line_ends,
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000310 Vector<const SourceChar> src,
311 bool with_last_line) {
312 const int src_len = src.length();
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000313 StringSearch<uint8_t, SourceChar> search(isolate, STATIC_ASCII_VECTOR("\n"));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000315 // Find and record line ends.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 int position = 0;
317 while (position != -1 && position < src_len) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000318 position = search.Search(src, position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319 if (position != -1) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000320 line_ends->Add(position);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000321 position++;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000322 } else if (with_last_line) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000323 // Even if the last line misses a line end, it is counted.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000324 line_ends->Add(src_len);
325 return;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000326 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000327 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000328}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000330
331Handle<FixedArray> CalculateLineEnds(Handle<String> src,
332 bool with_last_line) {
333 src = FlattenGetString(src);
334 // Rough estimate of line count based on a roughly estimated average
335 // length of (unpacked) code.
336 int line_count_estimate = src->length() >> 4;
337 List<int> line_ends(line_count_estimate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338 Isolate* isolate = src->GetIsolate();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000339 {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000340 DisallowHeapAllocation no_allocation; // ensure vectors stay valid.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000341 // Dispatch on type of strings.
ricow@chromium.orgddd545c2011-08-24 12:02:41 +0000342 String::FlatContent content = src->GetFlatContent();
343 ASSERT(content.IsFlat());
344 if (content.IsAscii()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000345 CalculateLineEnds(isolate,
346 &line_ends,
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000347 content.ToOneByteVector(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000348 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000349 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 CalculateLineEnds(isolate,
351 &line_ends,
ricow@chromium.orgddd545c2011-08-24 12:02:41 +0000352 content.ToUC16Vector(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 with_last_line);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000354 }
355 }
356 int line_count = line_ends.length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000357 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000358 for (int i = 0; i < line_count; i++) {
359 array->set(i, Smi::FromInt(line_ends[i]));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000360 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000361 return array;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000362}
363
364
365// Convert code position into line number.
366int GetScriptLineNumber(Handle<Script> script, int code_pos) {
367 InitScriptLineEnds(script);
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000368 DisallowHeapAllocation no_allocation;
ager@chromium.org5c838252010-02-19 08:53:10 +0000369 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000370 const int line_ends_len = line_ends_array->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000371
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000372 if (!line_ends_len) return -1;
ager@chromium.org5c838252010-02-19 08:53:10 +0000373
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000374 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000375 return script->line_offset()->value();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000376 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000377
378 int left = 0;
379 int right = line_ends_len;
380 while (int half = (right - left) / 2) {
381 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
382 right -= half;
383 } else {
384 left += half;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000385 }
386 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000387 return right + script->line_offset()->value();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000388}
389
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000390
danno@chromium.orgc612e022011-11-10 11:38:15 +0000391// Convert code position into column number.
392int GetScriptColumnNumber(Handle<Script> script, int code_pos) {
393 int line_number = GetScriptLineNumber(script, code_pos);
394 if (line_number == -1) return -1;
395
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000396 DisallowHeapAllocation no_allocation;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000397 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
398 line_number = line_number - script->line_offset()->value();
399 if (line_number == 0) return code_pos + script->column_offset()->value();
400 int prev_line_end_pos =
401 Smi::cast(line_ends_array->get(line_number - 1))->value();
402 return code_pos - (prev_line_end_pos + 1);
403}
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000404
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000405
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000406int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000407 DisallowHeapAllocation no_allocation;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000408 if (!script->line_ends()->IsUndefined()) {
409 return GetScriptLineNumber(script, code_pos);
410 }
411 // Slow mode: we do not have line_ends. We have to iterate through source.
412 if (!script->source()->IsString()) {
413 return -1;
414 }
415 String* source = String::cast(script->source());
416 int line = 0;
417 int len = source->length();
418 for (int pos = 0; pos < len; pos++) {
419 if (pos == code_pos) {
420 break;
421 }
422 if (source->Get(pos) == '\n') {
423 line++;
424 }
425 }
426 return line;
427}
428
429
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430// Compute the property keys from the interceptor.
ulan@chromium.org750145a2013-03-07 15:14:13 +0000431// TODO(rossberg): support symbols in API, and filter here if needed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000432v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000436 PropertyCallbackArguments
437 args(isolate, interceptor->data(), *receiver, *object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438 v8::Handle<v8::Array> result;
439 if (!interceptor->enumerator()->IsUndefined()) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000440 v8::NamedPropertyEnumeratorCallback enum_fun =
441 v8::ToCData<v8::NamedPropertyEnumeratorCallback>(
442 interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000444 result = args.Call(enum_fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 }
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000446#if ENABLE_EXTRA_CHECKS
447 CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject());
448#endif
ulan@chromium.org837a67e2013-06-11 15:39:48 +0000449 return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate),
450 result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000451}
452
453
454// Compute the element keys from the interceptor.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000455v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456 Handle<JSObject> object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000457 Isolate* isolate = receiver->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
ulan@chromium.orgbf9432e2013-05-22 14:05:23 +0000459 PropertyCallbackArguments
460 args(isolate, interceptor->data(), *receiver, *object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 v8::Handle<v8::Array> result;
462 if (!interceptor->enumerator()->IsUndefined()) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000463 v8::IndexedPropertyEnumeratorCallback enum_fun =
464 v8::ToCData<v8::IndexedPropertyEnumeratorCallback>(
465 interceptor->enumerator());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000467 result = args.Call(enum_fun);
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000468#if ENABLE_EXTRA_CHECKS
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000469 CHECK(result.IsEmpty() || v8::Utils::OpenHandle(*result)->IsJSObject());
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000470#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471 }
ulan@chromium.org837a67e2013-06-11 15:39:48 +0000472 return v8::Local<v8::Array>::New(reinterpret_cast<v8::Isolate*>(isolate),
473 result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474}
475
476
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000477Handle<Object> GetScriptNameOrSourceURL(Handle<Script> script) {
478 Isolate* isolate = script->GetIsolate();
479 Handle<String> name_or_source_url_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000480 isolate->factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000481 STATIC_ASCII_VECTOR("nameOrSourceURL"));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000482 Handle<JSValue> script_wrapper = GetScriptWrapper(script);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000483 Handle<Object> property = GetProperty(isolate,
484 script_wrapper,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000485 name_or_source_url_key);
486 ASSERT(property->IsJSFunction());
487 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
488 bool caught_exception;
489 Handle<Object> result = Execution::TryCall(method, script_wrapper, 0,
490 NULL, &caught_exception);
491 if (caught_exception) {
492 result = isolate->factory()->undefined_value();
493 }
494 return result;
495}
496
497
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000498static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
499 int len = array->length();
500 for (int i = 0; i < len; i++) {
501 Object* e = array->get(i);
502 if (!(e->IsString() || e->IsNumber())) return false;
503 }
504 return true;
505}
506
507
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000508Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
509 KeyCollectionType type,
510 bool* threw) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000511 USE(ContainsOnlyValidKeys);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000512 Isolate* isolate = object->GetIsolate();
513 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
514 Handle<JSObject> arguments_boilerplate = Handle<JSObject>(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000515 isolate->context()->native_context()->arguments_boilerplate(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 isolate);
517 Handle<JSFunction> arguments_function = Handle<JSFunction>(
518 JSFunction::cast(arguments_boilerplate->map()->constructor()),
519 isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 // Only collect keys if access is permitted.
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000522 for (Handle<Object> p = object;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 *p != isolate->heap()->null_value();
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000524 p = Handle<Object>(p->GetPrototype(isolate), isolate)) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000525 if (p->IsJSProxy()) {
526 Handle<JSProxy> proxy(JSProxy::cast(*p), isolate);
527 Handle<Object> args[] = { proxy };
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000528 Handle<Object> names = Execution::Call(isolate,
529 isolate->proxy_enumerate(),
530 object,
531 ARRAY_SIZE(args),
532 args,
533 threw);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000534 if (*threw) return content;
535 content = AddKeysFromJSArray(content, Handle<JSArray>::cast(names));
536 break;
537 }
538
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000539 Handle<JSObject> current(JSObject::cast(*p), isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000541 // Check access rights if required.
542 if (current->IsAccessCheckNeeded() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 !isolate->MayNamedAccess(*current,
544 isolate->heap()->undefined_value(),
545 v8::ACCESS_KEYS)) {
546 isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
danno@chromium.org169691d2013-07-15 08:01:13 +0000547 if (isolate->has_scheduled_exception()) {
548 isolate->PromoteScheduledException();
549 *threw = true;
550 }
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000551 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 }
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000553
554 // Compute the element keys.
555 Handle<FixedArray> element_keys =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000557 current->GetEnumElementKeys(*element_keys);
558 content = UnionOfKeys(content, element_keys);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000559 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000560
561 // Add the element keys from the interceptor.
562 if (current->HasIndexedInterceptor()) {
563 v8::Handle<v8::Array> result =
564 GetKeysForIndexedInterceptor(object, current);
565 if (!result.IsEmpty())
566 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000567 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000568 }
569
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000570 // We can cache the computed property keys if access checks are
571 // not needed and no interceptors are involved.
572 //
573 // We do not use the cache if the object has elements and
574 // therefore it does not make sense to cache the property names
575 // for arguments objects. Arguments objects will always have
576 // elements.
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000577 // Wrapped strings have elements, but don't have an elements
578 // array or dictionary. So the fast inline test for whether to
579 // use the cache says yes, so we should not create a cache.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000580 bool cache_enum_keys =
581 ((current->map()->constructor() != *arguments_function) &&
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000582 !current->IsJSValue() &&
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000583 !current->IsAccessCheckNeeded() &&
584 !current->HasNamedInterceptor() &&
585 !current->HasIndexedInterceptor());
586 // Compute the property keys and cache them if possible.
587 content =
588 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000589 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000590
591 // Add the property keys from the interceptor.
592 if (current->HasNamedInterceptor()) {
593 v8::Handle<v8::Array> result =
594 GetKeysForNamedInterceptor(object, current);
595 if (!result.IsEmpty())
596 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000597 ASSERT(ContainsOnlyValidKeys(content));
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000598 }
599
600 // If we only want local properties we bail out after the first
601 // iteration.
602 if (type == LOCAL_ONLY)
603 break;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 }
605 return content;
606}
607
608
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000609Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 Isolate* isolate = object->GetIsolate();
611 isolate->counters()->for_in()->Increment();
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000612 Handle<FixedArray> elements =
613 GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 return isolate->factory()->NewJSArrayWithElements(elements);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615}
616
617
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000618Handle<FixedArray> ReduceFixedArrayTo(Handle<FixedArray> array, int length) {
619 ASSERT(array->length() >= length);
620 if (array->length() == length) return array;
621
622 Handle<FixedArray> new_array =
623 array->GetIsolate()->factory()->NewFixedArray(length);
624 for (int i = 0; i < length; ++i) new_array->set(i, array->get(i));
625 return new_array;
626}
627
628
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000629Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
630 bool cache_result) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000631 Isolate* isolate = object->GetIsolate();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 if (object->HasFastProperties()) {
633 if (object->map()->instance_descriptors()->HasEnumCache()) {
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000634 int own_property_count = object->map()->EnumLength();
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000635 // If we have an enum cache, but the enum length of the given map is set
636 // to kInvalidEnumCache, this means that the map itself has never used the
637 // present enum cache. The first step to using the cache is to set the
638 // enum length of the map by counting the number of own descriptors that
ulan@chromium.org750145a2013-03-07 15:14:13 +0000639 // are not DONT_ENUM or SYMBOLIC.
machenbach@chromium.orgaf9cfcb2013-11-19 11:05:18 +0000640 if (own_property_count == kInvalidEnumCacheSentinel) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000641 own_property_count = object->map()->NumberOfDescribedProperties(
ulan@chromium.org750145a2013-03-07 15:14:13 +0000642 OWN_DESCRIPTORS, DONT_SHOW);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000643
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000644 if (cache_result) object->map()->SetEnumLength(own_property_count);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000645 }
646
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 DescriptorArray* desc = object->map()->instance_descriptors();
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000648 Handle<FixedArray> keys(desc->GetEnumCache(), isolate);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000649
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000650 // In case the number of properties required in the enum are actually
651 // present, we can reuse the enum cache. Otherwise, this means that the
652 // enum cache was generated for a previous (smaller) version of the
653 // Descriptor Array. In that case we regenerate the enum cache.
654 if (own_property_count <= keys->length()) {
655 isolate->counters()->enum_cache_hits()->Increment();
656 return ReduceFixedArrayTo(keys, own_property_count);
657 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 }
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000659
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000660 Handle<Map> map(object->map());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000661
662 if (map->instance_descriptors()->IsEmpty()) {
663 isolate->counters()->enum_cache_hits()->Increment();
664 if (cache_result) map->SetEnumLength(0);
665 return isolate->factory()->empty_fixed_array();
666 }
667
668 isolate->counters()->enum_cache_misses()->Increment();
ulan@chromium.org750145a2013-03-07 15:14:13 +0000669 int num_enum = map->NumberOfDescribedProperties(ALL_DESCRIPTORS, DONT_SHOW);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000670
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000671 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000672 Handle<FixedArray> indices = isolate->factory()->NewFixedArray(num_enum);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000673
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000674 Handle<DescriptorArray> descs =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000675 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000676
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000677 int real_size = map->NumberOfOwnDescriptors();
678 int enum_size = 0;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000679 int index = 0;
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000680
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000681 for (int i = 0; i < descs->number_of_descriptors(); i++) {
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000682 PropertyDetails details = descs->GetDetails(i);
ulan@chromium.org750145a2013-03-07 15:14:13 +0000683 Object* key = descs->GetKey(i);
684 if (!(details.IsDontEnum() || key->IsSymbol())) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000685 if (i < real_size) ++enum_size;
ulan@chromium.org750145a2013-03-07 15:14:13 +0000686 storage->set(index, key);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000687 if (!indices.is_null()) {
688 if (details.type() != FIELD) {
689 indices = Handle<FixedArray>();
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000690 } else {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000691 int field_index = descs->GetFieldIndex(i);
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000692 if (field_index >= map->inobject_properties()) {
693 field_index = -(field_index - map->inobject_properties() + 1);
694 }
695 indices->set(index, Smi::FromInt(field_index));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +0000696 }
697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698 index++;
699 }
700 }
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000701 ASSERT(index == storage->length());
702
703 Handle<FixedArray> bridge_storage =
704 isolate->factory()->NewFixedArray(
705 DescriptorArray::kEnumCacheBridgeLength);
706 DescriptorArray* desc = object->map()->instance_descriptors();
707 desc->SetEnumCache(*bridge_storage,
708 *storage,
709 indices.is_null() ? Object::cast(Smi::FromInt(0))
710 : Object::cast(*indices));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000711 if (cache_result) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000712 object->map()->SetEnumLength(enum_size);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000713 }
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +0000714
715 return ReduceFixedArrayTo(storage, enum_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 } else {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000717 Handle<NameDictionary> dictionary(object->property_dictionary());
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000718
719 int length = dictionary->NumberOfElements();
720 if (length == 0) {
721 return Handle<FixedArray>(isolate->heap()->empty_fixed_array());
722 }
723
724 // The enumeration array is generated by allocating an array big enough to
725 // hold all properties that have been seen, whether they are are deleted or
726 // not. Subsequently all visible properties are added to the array. If some
727 // properties were not visible, the array is trimmed so it only contains
728 // visible properties. This improves over adding elements and sorting by
729 // index by having linear complexity rather than n*log(n).
730
731 // By comparing the monotonous NextEnumerationIndex to the NumberOfElements,
732 // we can predict the number of holes in the final array. If there will be
733 // more than 50% holes, regenerate the enumeration indices to reduce the
734 // number of holes to a minimum. This avoids allocating a large array if
735 // many properties were added but subsequently deleted.
736 int next_enumeration = dictionary->NextEnumerationIndex();
737 if (!object->IsGlobalObject() && next_enumeration > (length * 3) / 2) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000738 NameDictionary::DoGenerateNewEnumerationIndices(dictionary);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +0000739 next_enumeration = dictionary->NextEnumerationIndex();
740 }
741
742 Handle<FixedArray> storage =
743 isolate->factory()->NewFixedArray(next_enumeration);
744
745 storage = Handle<FixedArray>(dictionary->CopyEnumKeysTo(*storage));
ulan@chromium.org750145a2013-03-07 15:14:13 +0000746 ASSERT(storage->length() == object->NumberOfLocalProperties(DONT_SHOW));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 return storage;
748 }
749}
750
751
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000752DeferredHandleScope::DeferredHandleScope(Isolate* isolate)
753 : impl_(isolate->handle_scope_implementer()) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000754 impl_->BeginDeferredScope();
755 v8::ImplementationUtilities::HandleScopeData* data =
756 impl_->isolate()->handle_scope_data();
757 Object** new_next = impl_->GetSpareOrNewBlock();
758 Object** new_limit = &new_next[kHandleBlockSize];
759 ASSERT(data->limit == &impl_->blocks()->last()[kHandleBlockSize]);
760 impl_->blocks()->Add(new_next);
761
762#ifdef DEBUG
763 prev_level_ = data->level;
764#endif
765 data->level++;
766 prev_limit_ = data->limit;
767 prev_next_ = data->next;
768 data->next = new_next;
769 data->limit = new_limit;
770}
771
772
773DeferredHandleScope::~DeferredHandleScope() {
774 impl_->isolate()->handle_scope_data()->level--;
775 ASSERT(handles_detached_);
776 ASSERT(impl_->isolate()->handle_scope_data()->level == prev_level_);
777}
778
779
780DeferredHandles* DeferredHandleScope::Detach() {
781 DeferredHandles* deferred = impl_->Detach(prev_limit_);
782 v8::ImplementationUtilities::HandleScopeData* data =
783 impl_->isolate()->handle_scope_data();
784 data->next = prev_next_;
785 data->limit = prev_limit_;
786#ifdef DEBUG
787 handles_detached_ = true;
788#endif
789 return deferred;
790}
791
792
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +0000793void AddWeakObjectToCodeDependency(Heap* heap,
794 Handle<Object> object,
795 Handle<Code> code) {
796 heap->EnsureWeakObjectToCodeTable();
797 Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(*object));
798 dep = DependentCode::Insert(dep, DependentCode::kWeaklyEmbeddedGroup, code);
799 CALL_HEAP_FUNCTION_VOID(heap->isolate(),
800 heap->AddWeakObjectToCodeDependency(*object, *dep));
801}
802
803
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000804} } // namespace v8::internal