blob: 34eaddbbd71e7395c360430a2f237df2bd484145 [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "arguments.h"
33#include "bootstrapper.h"
34#include "compiler.h"
35#include "debug.h"
36#include "execution.h"
37#include "global-handles.h"
38#include "natives.h"
39#include "runtime.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080040#include "string-search.h"
Steve Blockd0582a62009-12-15 09:54:21 +000041#include "stub-cache.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043
44namespace v8 {
45namespace internal {
46
47
Steve Blocka7e24c12009-10-30 11:49:00 +000048int HandleScope::NumberOfHandles() {
Steve Block44f0eee2011-05-26 01:26:41 +010049 Isolate* isolate = Isolate::Current();
50 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
51 int n = impl->blocks()->length();
Steve Blocka7e24c12009-10-30 11:49:00 +000052 if (n == 0) return 0;
Steve Blockd0582a62009-12-15 09:54:21 +000053 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
Steve Block44f0eee2011-05-26 01:26:41 +010054 (isolate->handle_scope_data()->next - impl->blocks()->last()));
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
58Object** HandleScope::Extend() {
Steve Block44f0eee2011-05-26 01:26:41 +010059 Isolate* isolate = Isolate::Current();
60 v8::ImplementationUtilities::HandleScopeData* current =
61 isolate->handle_scope_data();
Steve Blocka7e24c12009-10-30 11:49:00 +000062
Steve Block44f0eee2011-05-26 01:26:41 +010063 Object** result = current->next;
64
65 ASSERT(result == current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000066 // Make sure there's at least one scope on the stack and that the
67 // top of the scope stack isn't a barrier.
Steve Block44f0eee2011-05-26 01:26:41 +010068 if (current->level == 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +000069 Utils::ReportApiFailure("v8::HandleScope::CreateHandle()",
70 "Cannot create a handle without a HandleScope");
71 return NULL;
72 }
Steve Block44f0eee2011-05-26 01:26:41 +010073 HandleScopeImplementer* impl = isolate->handle_scope_implementer();
Steve Blocka7e24c12009-10-30 11:49:00 +000074 // If there's more room in the last block, we use that. This is used
75 // for fast creation of scopes after scope barriers.
76 if (!impl->blocks()->is_empty()) {
77 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
Steve Block44f0eee2011-05-26 01:26:41 +010078 if (current->limit != limit) {
79 current->limit = limit;
80 ASSERT(limit - current->next < kHandleBlockSize);
Steve Blocka7e24c12009-10-30 11:49:00 +000081 }
82 }
83
84 // If we still haven't found a slot for the handle, we extend the
85 // current handle scope by allocating a new handle block.
Steve Block44f0eee2011-05-26 01:26:41 +010086 if (result == current->limit) {
Steve Blocka7e24c12009-10-30 11:49:00 +000087 // If there's a spare block, use it for growing the current scope.
88 result = impl->GetSpareOrNewBlock();
89 // Add the extension to the global list of blocks, but count the
90 // extension as part of the current scope.
91 impl->blocks()->Add(result);
Steve Block44f0eee2011-05-26 01:26:41 +010092 current->limit = &result[kHandleBlockSize];
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
94
95 return result;
96}
97
98
Steve Block44f0eee2011-05-26 01:26:41 +010099void HandleScope::DeleteExtensions(Isolate* isolate) {
100 ASSERT(isolate == Isolate::Current());
101 v8::ImplementationUtilities::HandleScopeData* current =
102 isolate->handle_scope_data();
103 isolate->handle_scope_implementer()->DeleteExtensions(current->limit);
Steve Blocka7e24c12009-10-30 11:49:00 +0000104}
105
106
107void HandleScope::ZapRange(Object** start, Object** end) {
John Reck59135872010-11-02 12:39:01 -0700108 ASSERT(end - start <= kHandleBlockSize);
109 for (Object** p = start; p != end; p++) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
111 }
112}
113
114
John Reck59135872010-11-02 12:39:01 -0700115Address HandleScope::current_level_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100116 return reinterpret_cast<Address>(
117 &Isolate::Current()->handle_scope_data()->level);
Steve Blockd0582a62009-12-15 09:54:21 +0000118}
119
120
121Address HandleScope::current_next_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100122 return reinterpret_cast<Address>(
123 &Isolate::Current()->handle_scope_data()->next);
Steve Blockd0582a62009-12-15 09:54:21 +0000124}
125
126
127Address HandleScope::current_limit_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100128 return reinterpret_cast<Address>(
129 &Isolate::Current()->handle_scope_data()->limit);
Steve Blockd0582a62009-12-15 09:54:21 +0000130}
131
132
Steve Blocka7e24c12009-10-30 11:49:00 +0000133Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content,
134 Handle<JSArray> array) {
Steve Block44f0eee2011-05-26 01:26:41 +0100135 CALL_HEAP_FUNCTION(content->GetIsolate(),
136 content->AddKeysFromJSArray(*array), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137}
138
139
140Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
141 Handle<FixedArray> second) {
Steve Block44f0eee2011-05-26 01:26:41 +0100142 CALL_HEAP_FUNCTION(first->GetIsolate(),
143 first->UnionOfKeys(*second), FixedArray);
Steve Blocka7e24c12009-10-30 11:49:00 +0000144}
145
146
147Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
148 Handle<JSFunction> constructor,
149 Handle<JSGlobalProxy> global) {
Steve Block44f0eee2011-05-26 01:26:41 +0100150 CALL_HEAP_FUNCTION(
151 constructor->GetIsolate(),
152 constructor->GetHeap()->ReinitializeJSGlobalProxy(*constructor, *global),
153 JSGlobalProxy);
Steve Blocka7e24c12009-10-30 11:49:00 +0000154}
155
156
157void SetExpectedNofProperties(Handle<JSFunction> func, int nof) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100158 // If objects constructed from this function exist then changing
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800159 // 'estimated_nof_properties' is dangerous since the previous value might
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100160 // have been compiled into the fast construct stub. More over, the inobject
161 // slack tracking logic might have adjusted the previous value, so even
162 // passing the same value is risky.
163 if (func->shared()->live_objects_may_exist()) return;
164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 func->shared()->set_expected_nof_properties(nof);
166 if (func->has_initial_map()) {
167 Handle<Map> new_initial_map =
Steve Block44f0eee2011-05-26 01:26:41 +0100168 func->GetIsolate()->factory()->CopyMapDropTransitions(
169 Handle<Map>(func->initial_map()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 new_initial_map->set_unused_property_fields(nof);
171 func->set_initial_map(*new_initial_map);
172 }
173}
174
175
176void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value) {
Steve Block44f0eee2011-05-26 01:26:41 +0100177 CALL_HEAP_FUNCTION_VOID(func->GetIsolate(),
178 func->SetPrototype(*value));
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181
182static int ExpectedNofPropertiesFromEstimate(int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100183 // If no properties are added in the constructor, they are more likely
184 // to be added later.
185 if (estimate == 0) estimate = 2;
186
187 // We do not shrink objects that go into a snapshot (yet), so we adjust
188 // the estimate conservatively.
189 if (Serializer::enabled()) return estimate + 2;
190
191 // Inobject slack tracking will reclaim redundant inobject space later,
192 // so we can afford to adjust the estimate generously.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000193 if (FLAG_clever_optimizations) {
194 return estimate + 8;
195 } else {
196 return estimate + 3;
197 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198}
199
200
201void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
202 int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100203 // See the comment in SetExpectedNofProperties.
204 if (shared->live_objects_may_exist()) return;
205
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 shared->set_expected_nof_properties(
207 ExpectedNofPropertiesFromEstimate(estimate));
208}
209
210
Steve Blocka7e24c12009-10-30 11:49:00 +0000211void FlattenString(Handle<String> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100212 CALL_HEAP_FUNCTION_VOID(string->GetIsolate(), string->TryFlatten());
Steve Block8defd9f2010-07-08 12:39:36 +0100213}
214
215
216Handle<String> FlattenGetString(Handle<String> string) {
Steve Block44f0eee2011-05-26 01:26:41 +0100217 CALL_HEAP_FUNCTION(string->GetIsolate(), string->TryFlatten(), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000218}
219
220
221Handle<Object> SetPrototype(Handle<JSFunction> function,
222 Handle<Object> prototype) {
Steve Block6ded16b2010-05-10 14:33:55 +0100223 ASSERT(function->should_have_prototype());
Steve Block44f0eee2011-05-26 01:26:41 +0100224 CALL_HEAP_FUNCTION(function->GetIsolate(),
225 Accessors::FunctionSetPrototype(*function,
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 *prototype,
227 NULL),
228 Object);
229}
230
231
Steve Blocka7e24c12009-10-30 11:49:00 +0000232Handle<Object> SetProperty(Handle<Object> object,
233 Handle<Object> key,
234 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100235 PropertyAttributes attributes,
236 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100237 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 CALL_HEAP_FUNCTION(
Steve Block44f0eee2011-05-26 01:26:41 +0100239 isolate,
240 Runtime::SetObjectProperty(
241 isolate, object, key, value, attributes, strict_mode),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100242 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000243}
244
245
246Handle<Object> ForceSetProperty(Handle<JSObject> object,
247 Handle<Object> key,
248 Handle<Object> value,
249 PropertyAttributes attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100250 Isolate* isolate = object->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 CALL_HEAP_FUNCTION(
Steve Block44f0eee2011-05-26 01:26:41 +0100252 isolate,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100253 Runtime::ForceSetObjectProperty(
Steve Block44f0eee2011-05-26 01:26:41 +0100254 isolate, object, key, value, attributes),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100255 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000256}
257
258
259Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
260 Handle<Object> key) {
Steve Block44f0eee2011-05-26 01:26:41 +0100261 Isolate* isolate = object->GetIsolate();
262 CALL_HEAP_FUNCTION(isolate,
263 Runtime::ForceDeleteObjectProperty(isolate, object, key),
264 Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265}
266
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
269 Handle<String> key,
270 Handle<Object> value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100271 PropertyAttributes attributes,
272 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100273 CALL_HEAP_FUNCTION(object->GetIsolate(),
274 object->SetPropertyWithInterceptor(*key,
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 *value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100276 attributes,
277 strict_mode),
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 Object);
279}
280
281
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000282Handle<Object> GetProperty(Handle<JSReceiver> obj,
Steve Blocka7e24c12009-10-30 11:49:00 +0000283 const char* name) {
Steve Block44f0eee2011-05-26 01:26:41 +0100284 Isolate* isolate = obj->GetIsolate();
285 Handle<String> str = isolate->factory()->LookupAsciiSymbol(name);
286 CALL_HEAP_FUNCTION(isolate, obj->GetProperty(*str), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000287}
288
289
290Handle<Object> GetProperty(Handle<Object> obj,
291 Handle<Object> key) {
Steve Block44f0eee2011-05-26 01:26:41 +0100292 Isolate* isolate = Isolate::Current();
293 CALL_HEAP_FUNCTION(isolate,
294 Runtime::GetObjectProperty(isolate, obj, key), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295}
296
297
298Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
299 Handle<JSObject> holder,
300 Handle<String> name,
301 PropertyAttributes* attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +0100302 Isolate* isolate = receiver->GetIsolate();
303 CALL_HEAP_FUNCTION(isolate,
304 holder->GetPropertyWithInterceptor(*receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 *name,
306 attributes),
307 Object);
308}
309
310
Andrei Popescu402d9372010-02-26 13:31:12 +0000311Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) {
312 const bool skip_hidden_prototypes = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100313 CALL_HEAP_FUNCTION(obj->GetIsolate(),
314 obj->SetPrototype(*value, skip_hidden_prototypes), Object);
315}
316
317
Steve Blocka7e24c12009-10-30 11:49:00 +0000318Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) {
Steve Block44f0eee2011-05-26 01:26:41 +0100319 Isolate* isolate = Isolate::Current();
320 CALL_HEAP_FUNCTION(
321 isolate,
322 isolate->heap()->LookupSingleCharacterStringFromCode(index), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000323}
324
325
Steve Block6ded16b2010-05-10 14:33:55 +0100326Handle<String> SubString(Handle<String> str,
327 int start,
328 int end,
329 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +0100330 CALL_HEAP_FUNCTION(str->GetIsolate(),
331 str->SubString(start, end, pretenure), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000332}
333
334
Steve Blocka7e24c12009-10-30 11:49:00 +0000335Handle<JSObject> Copy(Handle<JSObject> obj) {
Steve Block44f0eee2011-05-26 01:26:41 +0100336 Isolate* isolate = obj->GetIsolate();
337 CALL_HEAP_FUNCTION(isolate,
338 isolate->heap()->CopyJSObject(*obj), JSObject);
Steve Blocka7e24c12009-10-30 11:49:00 +0000339}
340
341
Leon Clarkef7060e22010-06-03 12:02:55 +0100342Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100343 CALL_HEAP_FUNCTION(obj->GetIsolate(), obj->DefineAccessor(*info), Object);
Leon Clarkef7060e22010-06-03 12:02:55 +0100344}
345
346
Steve Blocka7e24c12009-10-30 11:49:00 +0000347// Wrappers for scripts are kept alive and cached in weak global
Ben Murdoch257744e2011-11-30 15:57:28 +0000348// handles referred from foreign objects held by the scripts as long as
Steve Blocka7e24c12009-10-30 11:49:00 +0000349// they are used. When they are not used anymore, the garbage
350// collector will call the weak callback on the global handle
351// associated with the wrapper and get rid of both the wrapper and the
352// handle.
353static void ClearWrapperCache(Persistent<v8::Value> handle, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 Handle<Object> cache = Utils::OpenHandle(*handle);
355 JSValue* wrapper = JSValue::cast(*cache);
Ben Murdoch257744e2011-11-30 15:57:28 +0000356 Foreign* foreign = Script::cast(wrapper->value())->wrapper();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000357 ASSERT(foreign->foreign_address() ==
358 reinterpret_cast<Address>(cache.location()));
359 foreign->set_foreign_address(0);
Steve Block44f0eee2011-05-26 01:26:41 +0100360 Isolate* isolate = Isolate::Current();
361 isolate->global_handles()->Destroy(cache.location());
362 isolate->counters()->script_wrappers()->Decrement();
Steve Blocka7e24c12009-10-30 11:49:00 +0000363}
364
365
366Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000367 if (script->wrapper()->foreign_address() != NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 // Return the script wrapper directly from the cache.
369 return Handle<JSValue>(
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000370 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 }
Steve Block44f0eee2011-05-26 01:26:41 +0100372 Isolate* isolate = Isolate::Current();
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 // Construct a new script wrapper.
Steve Block44f0eee2011-05-26 01:26:41 +0100374 isolate->counters()->script_wrappers()->Increment();
375 Handle<JSFunction> constructor = isolate->script_function();
Steve Blocka7e24c12009-10-30 11:49:00 +0000376 Handle<JSValue> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100377 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor));
Steve Blocka7e24c12009-10-30 11:49:00 +0000378 result->set_value(*script);
379
380 // Create a new weak global handle and use it to cache the wrapper
381 // for future use. The cache will automatically be cleared by the
382 // garbage collector when it is not used anymore.
Steve Block44f0eee2011-05-26 01:26:41 +0100383 Handle<Object> handle = isolate->global_handles()->Create(*result);
384 isolate->global_handles()->MakeWeak(handle.location(), NULL,
385 &ClearWrapperCache);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000386 script->wrapper()->set_foreign_address(
387 reinterpret_cast<Address>(handle.location()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 return result;
389}
390
391
392// Init line_ends array with code positions of line ends inside script
393// source.
394void InitScriptLineEnds(Handle<Script> script) {
395 if (!script->line_ends()->IsUndefined()) return;
396
Steve Block44f0eee2011-05-26 01:26:41 +0100397 Isolate* isolate = script->GetIsolate();
398
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 if (!script->source()->IsString()) {
400 ASSERT(script->source()->IsUndefined());
Steve Block44f0eee2011-05-26 01:26:41 +0100401 Handle<FixedArray> empty = isolate->factory()->NewFixedArray(0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100402 script->set_line_ends(*empty);
Steve Blockd0582a62009-12-15 09:54:21 +0000403 ASSERT(script->line_ends()->IsFixedArray());
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 return;
405 }
406
Steve Block44f0eee2011-05-26 01:26:41 +0100407 Handle<String> src(String::cast(script->source()), isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100408
409 Handle<FixedArray> array = CalculateLineEnds(src, true);
410
Steve Block44f0eee2011-05-26 01:26:41 +0100411 if (*array != isolate->heap()->empty_fixed_array()) {
412 array->set_map(isolate->heap()->fixed_cow_array_map());
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800413 }
414
Steve Block6ded16b2010-05-10 14:33:55 +0100415 script->set_line_ends(*array);
416 ASSERT(script->line_ends()->IsFixedArray());
417}
418
419
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800420template <typename SourceChar>
Steve Block44f0eee2011-05-26 01:26:41 +0100421static void CalculateLineEnds(Isolate* isolate,
422 List<int>* line_ends,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800423 Vector<const SourceChar> src,
424 bool with_last_line) {
425 const int src_len = src.length();
Steve Block44f0eee2011-05-26 01:26:41 +0100426 StringSearch<char, SourceChar> search(isolate, CStrVector("\n"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000427
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800428 // Find and record line ends.
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 int position = 0;
430 while (position != -1 && position < src_len) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800431 position = search.Search(src, position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 if (position != -1) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800433 line_ends->Add(position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 position++;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800435 } else if (with_last_line) {
Steve Block6ded16b2010-05-10 14:33:55 +0100436 // Even if the last line misses a line end, it is counted.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800437 line_ends->Add(src_len);
438 return;
Steve Block6ded16b2010-05-10 14:33:55 +0100439 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800441}
Steve Blocka7e24c12009-10-30 11:49:00 +0000442
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800443
444Handle<FixedArray> CalculateLineEnds(Handle<String> src,
445 bool with_last_line) {
446 src = FlattenGetString(src);
447 // Rough estimate of line count based on a roughly estimated average
448 // length of (unpacked) code.
449 int line_count_estimate = src->length() >> 4;
450 List<int> line_ends(line_count_estimate);
Steve Block44f0eee2011-05-26 01:26:41 +0100451 Isolate* isolate = src->GetIsolate();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800452 {
453 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid.
454 // Dispatch on type of strings.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000455 String::FlatContent content = src->GetFlatContent();
456 ASSERT(content.IsFlat());
457 if (content.IsAscii()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100458 CalculateLineEnds(isolate,
459 &line_ends,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000460 content.ToAsciiVector(),
Steve Block44f0eee2011-05-26 01:26:41 +0100461 with_last_line);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800462 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100463 CalculateLineEnds(isolate,
464 &line_ends,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000465 content.ToUC16Vector(),
Steve Block44f0eee2011-05-26 01:26:41 +0100466 with_last_line);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800467 }
468 }
469 int line_count = line_ends.length();
Steve Block44f0eee2011-05-26 01:26:41 +0100470 Handle<FixedArray> array = isolate->factory()->NewFixedArray(line_count);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800471 for (int i = 0; i < line_count; i++) {
472 array->set(i, Smi::FromInt(line_ends[i]));
Steve Blocka7e24c12009-10-30 11:49:00 +0000473 }
Steve Block6ded16b2010-05-10 14:33:55 +0100474 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000475}
476
477
478// Convert code position into line number.
479int GetScriptLineNumber(Handle<Script> script, int code_pos) {
480 InitScriptLineEnds(script);
481 AssertNoAllocation no_allocation;
Andrei Popescu402d9372010-02-26 13:31:12 +0000482 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
Steve Blockd0582a62009-12-15 09:54:21 +0000483 const int line_ends_len = line_ends_array->length();
Steve Blocka7e24c12009-10-30 11:49:00 +0000484
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800485 if (!line_ends_len) return -1;
Andrei Popescu402d9372010-02-26 13:31:12 +0000486
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800487 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000488 return script->line_offset()->value();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800489 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000490
491 int left = 0;
492 int right = line_ends_len;
493 while (int half = (right - left) / 2) {
494 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
495 right -= half;
496 } else {
497 left += half;
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 }
499 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000500 return right + script->line_offset()->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000501}
502
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000503// Convert code position into column number.
504int GetScriptColumnNumber(Handle<Script> script, int code_pos) {
505 int line_number = GetScriptLineNumber(script, code_pos);
506 if (line_number == -1) return -1;
507
508 AssertNoAllocation no_allocation;
509 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
510 line_number = line_number - script->line_offset()->value();
511 if (line_number == 0) return code_pos + script->column_offset()->value();
512 int prev_line_end_pos =
513 Smi::cast(line_ends_array->get(line_number - 1))->value();
514 return code_pos - (prev_line_end_pos + 1);
515}
Steve Blocka7e24c12009-10-30 11:49:00 +0000516
Steve Block6ded16b2010-05-10 14:33:55 +0100517int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
518 AssertNoAllocation no_allocation;
519 if (!script->line_ends()->IsUndefined()) {
520 return GetScriptLineNumber(script, code_pos);
521 }
522 // Slow mode: we do not have line_ends. We have to iterate through source.
523 if (!script->source()->IsString()) {
524 return -1;
525 }
526 String* source = String::cast(script->source());
527 int line = 0;
528 int len = source->length();
529 for (int pos = 0; pos < len; pos++) {
530 if (pos == code_pos) {
531 break;
532 }
533 if (source->Get(pos) == '\n') {
534 line++;
535 }
536 }
537 return line;
538}
539
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541void CustomArguments::IterateInstance(ObjectVisitor* v) {
Steve Block6ded16b2010-05-10 14:33:55 +0100542 v->VisitPointers(values_, values_ + ARRAY_SIZE(values_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000543}
544
545
546// Compute the property keys from the interceptor.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000547v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000548 Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100549 Isolate* isolate = receiver->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +0100551 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 v8::AccessorInfo info(args.end());
553 v8::Handle<v8::Array> result;
554 if (!interceptor->enumerator()->IsUndefined()) {
555 v8::NamedPropertyEnumerator enum_fun =
556 v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator());
Steve Block44f0eee2011-05-26 01:26:41 +0100557 LOG(isolate, ApiObjectAccess("interceptor-named-enum", *object));
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 {
559 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100560 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 result = enum_fun(info);
562 }
563 }
564 return result;
565}
566
567
568// Compute the element keys from the interceptor.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000569v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver,
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 Handle<JSObject> object) {
Steve Block44f0eee2011-05-26 01:26:41 +0100571 Isolate* isolate = receiver->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
Steve Block44f0eee2011-05-26 01:26:41 +0100573 CustomArguments args(isolate, interceptor->data(), *receiver, *object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574 v8::AccessorInfo info(args.end());
575 v8::Handle<v8::Array> result;
576 if (!interceptor->enumerator()->IsUndefined()) {
577 v8::IndexedPropertyEnumerator enum_fun =
578 v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator());
Steve Block44f0eee2011-05-26 01:26:41 +0100579 LOG(isolate, ApiObjectAccess("interceptor-indexed-enum", *object));
Steve Blocka7e24c12009-10-30 11:49:00 +0000580 {
581 // Leaving JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +0100582 VMState state(isolate, EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 result = enum_fun(info);
584 }
585 }
586 return result;
587}
588
589
Ben Murdochf87a2032010-10-22 12:50:53 +0100590static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
591 int len = array->length();
592 for (int i = 0; i < len; i++) {
593 Object* e = array->get(i);
594 if (!(e->IsString() || e->IsNumber())) return false;
595 }
596 return true;
597}
598
599
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000600Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object,
601 KeyCollectionType type,
602 bool* threw) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100603 USE(ContainsOnlyValidKeys);
Steve Block44f0eee2011-05-26 01:26:41 +0100604 Isolate* isolate = object->GetIsolate();
605 Handle<FixedArray> content = isolate->factory()->empty_fixed_array();
606 Handle<JSObject> arguments_boilerplate = Handle<JSObject>(
607 isolate->context()->global_context()->arguments_boilerplate(),
608 isolate);
609 Handle<JSFunction> arguments_function = Handle<JSFunction>(
610 JSFunction::cast(arguments_boilerplate->map()->constructor()),
611 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000612
613 // Only collect keys if access is permitted.
614 for (Handle<Object> p = object;
Steve Block44f0eee2011-05-26 01:26:41 +0100615 *p != isolate->heap()->null_value();
616 p = Handle<Object>(p->GetPrototype(), isolate)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000617 if (p->IsJSProxy()) {
618 Handle<JSProxy> proxy(JSProxy::cast(*p), isolate);
619 Handle<Object> args[] = { proxy };
620 Handle<Object> names = Execution::Call(
621 isolate->proxy_enumerate(), object, ARRAY_SIZE(args), args, threw);
622 if (*threw) return content;
623 content = AddKeysFromJSArray(content, Handle<JSArray>::cast(names));
624 break;
625 }
626
Steve Block44f0eee2011-05-26 01:26:41 +0100627 Handle<JSObject> current(JSObject::cast(*p), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000628
629 // Check access rights if required.
630 if (current->IsAccessCheckNeeded() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100631 !isolate->MayNamedAccess(*current,
632 isolate->heap()->undefined_value(),
633 v8::ACCESS_KEYS)) {
634 isolate->ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 break;
636 }
637
638 // Compute the element keys.
639 Handle<FixedArray> element_keys =
Steve Block44f0eee2011-05-26 01:26:41 +0100640 isolate->factory()->NewFixedArray(current->NumberOfEnumElements());
Steve Blocka7e24c12009-10-30 11:49:00 +0000641 current->GetEnumElementKeys(*element_keys);
642 content = UnionOfKeys(content, element_keys);
Ben Murdochf87a2032010-10-22 12:50:53 +0100643 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000644
645 // Add the element keys from the interceptor.
646 if (current->HasIndexedInterceptor()) {
647 v8::Handle<v8::Array> result =
648 GetKeysForIndexedInterceptor(object, current);
649 if (!result.IsEmpty())
650 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100651 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 }
653
Steve Blockd0582a62009-12-15 09:54:21 +0000654 // We can cache the computed property keys if access checks are
655 // not needed and no interceptors are involved.
656 //
657 // We do not use the cache if the object has elements and
658 // therefore it does not make sense to cache the property names
659 // for arguments objects. Arguments objects will always have
660 // elements.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100661 // Wrapped strings have elements, but don't have an elements
662 // array or dictionary. So the fast inline test for whether to
663 // use the cache says yes, so we should not create a cache.
Steve Blockd0582a62009-12-15 09:54:21 +0000664 bool cache_enum_keys =
665 ((current->map()->constructor() != *arguments_function) &&
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100666 !current->IsJSValue() &&
Steve Blockd0582a62009-12-15 09:54:21 +0000667 !current->IsAccessCheckNeeded() &&
668 !current->HasNamedInterceptor() &&
669 !current->HasIndexedInterceptor());
670 // Compute the property keys and cache them if possible.
671 content =
672 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
Ben Murdochf87a2032010-10-22 12:50:53 +0100673 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000674
675 // Add the property keys from the interceptor.
676 if (current->HasNamedInterceptor()) {
677 v8::Handle<v8::Array> result =
678 GetKeysForNamedInterceptor(object, current);
679 if (!result.IsEmpty())
680 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100681 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 }
683
684 // If we only want local properties we bail out after the first
685 // iteration.
686 if (type == LOCAL_ONLY)
687 break;
688 }
689 return content;
690}
691
692
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000693Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw) {
Steve Block44f0eee2011-05-26 01:26:41 +0100694 Isolate* isolate = object->GetIsolate();
695 isolate->counters()->for_in()->Increment();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000696 Handle<FixedArray> elements =
697 GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, threw);
Steve Block44f0eee2011-05-26 01:26:41 +0100698 return isolate->factory()->NewJSArrayWithElements(elements);
Steve Blocka7e24c12009-10-30 11:49:00 +0000699}
700
701
Steve Blockd0582a62009-12-15 09:54:21 +0000702Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
703 bool cache_result) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 int index = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100705 Isolate* isolate = object->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000706 if (object->HasFastProperties()) {
707 if (object->map()->instance_descriptors()->HasEnumCache()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100708 isolate->counters()->enum_cache_hits()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000709 DescriptorArray* desc = object->map()->instance_descriptors();
Steve Block44f0eee2011-05-26 01:26:41 +0100710 return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()),
711 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 }
Steve Block44f0eee2011-05-26 01:26:41 +0100713 isolate->counters()->enum_cache_misses()->Increment();
Steve Blocka7e24c12009-10-30 11:49:00 +0000714 int num_enum = object->NumberOfEnumProperties();
Steve Block44f0eee2011-05-26 01:26:41 +0100715 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
716 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 Handle<DescriptorArray> descs =
Steve Block44f0eee2011-05-26 01:26:41 +0100718 Handle<DescriptorArray>(object->map()->instance_descriptors(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000719 for (int i = 0; i < descs->number_of_descriptors(); i++) {
720 if (descs->IsProperty(i) && !descs->IsDontEnum(i)) {
721 (*storage)->set(index, descs->GetKey(i));
722 PropertyDetails details(descs->GetDetails(i));
723 (*sort_array)->set(index, Smi::FromInt(details.index()));
724 index++;
725 }
726 }
727 (*storage)->SortPairs(*sort_array, sort_array->length());
Steve Blockd0582a62009-12-15 09:54:21 +0000728 if (cache_result) {
729 Handle<FixedArray> bridge_storage =
Steve Block44f0eee2011-05-26 01:26:41 +0100730 isolate->factory()->NewFixedArray(
731 DescriptorArray::kEnumCacheBridgeLength);
Steve Blockd0582a62009-12-15 09:54:21 +0000732 DescriptorArray* desc = object->map()->instance_descriptors();
733 desc->SetEnumCache(*bridge_storage, *storage);
734 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 ASSERT(storage->length() == index);
736 return storage;
737 } else {
738 int num_enum = object->NumberOfEnumProperties();
Steve Block44f0eee2011-05-26 01:26:41 +0100739 Handle<FixedArray> storage = isolate->factory()->NewFixedArray(num_enum);
740 Handle<FixedArray> sort_array = isolate->factory()->NewFixedArray(num_enum);
Steve Blocka7e24c12009-10-30 11:49:00 +0000741 object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array);
742 return storage;
743 }
744}
745
746
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000747Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table,
748 Handle<Object> key) {
749 CALL_HEAP_FUNCTION(table->GetIsolate(),
750 table->Add(*key),
751 ObjectHashSet);
752}
753
754
755Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table,
756 Handle<Object> key) {
757 CALL_HEAP_FUNCTION(table->GetIsolate(),
758 table->Remove(*key),
759 ObjectHashSet);
760}
761
762
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000763Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000764 Handle<Object> key,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000765 Handle<Object> value) {
766 CALL_HEAP_FUNCTION(table->GetIsolate(),
767 table->Put(*key, *value),
768 ObjectHashTable);
769}
770
771
Steve Blocka7e24c12009-10-30 11:49:00 +0000772} } // namespace v8::internal