blob: 8fe29bb7a00b1d21f1714c3c3903340243d5c0a7 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "arguments.h"
33#include "bootstrapper.h"
34#include "compiler.h"
35#include "debug.h"
36#include "execution.h"
37#include "global-handles.h"
38#include "natives.h"
39#include "runtime.h"
Steve Blockd0582a62009-12-15 09:54:21 +000040#include "stub-cache.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42namespace v8 {
43namespace internal {
44
45
46v8::ImplementationUtilities::HandleScopeData HandleScope::current_ =
47 { -1, NULL, NULL };
48
49
50int HandleScope::NumberOfHandles() {
51 int n = HandleScopeImplementer::instance()->blocks()->length();
52 if (n == 0) return 0;
Steve Blockd0582a62009-12-15 09:54:21 +000053 return ((n - 1) * kHandleBlockSize) + static_cast<int>(
54 (current_.next - HandleScopeImplementer::instance()->blocks()->last()));
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
58Object** HandleScope::Extend() {
59 Object** result = current_.next;
60
61 ASSERT(result == current_.limit);
62 // Make sure there's at least one scope on the stack and that the
63 // top of the scope stack isn't a barrier.
64 if (current_.extensions < 0) {
65 Utils::ReportApiFailure("v8::HandleScope::CreateHandle()",
66 "Cannot create a handle without a HandleScope");
67 return NULL;
68 }
69 HandleScopeImplementer* impl = HandleScopeImplementer::instance();
70 // If there's more room in the last block, we use that. This is used
71 // for fast creation of scopes after scope barriers.
72 if (!impl->blocks()->is_empty()) {
73 Object** limit = &impl->blocks()->last()[kHandleBlockSize];
74 if (current_.limit != limit) {
75 current_.limit = limit;
76 }
77 }
78
79 // If we still haven't found a slot for the handle, we extend the
80 // current handle scope by allocating a new handle block.
81 if (result == current_.limit) {
82 // If there's a spare block, use it for growing the current scope.
83 result = impl->GetSpareOrNewBlock();
84 // Add the extension to the global list of blocks, but count the
85 // extension as part of the current scope.
86 impl->blocks()->Add(result);
87 current_.extensions++;
88 current_.limit = &result[kHandleBlockSize];
89 }
90
91 return result;
92}
93
94
95void HandleScope::DeleteExtensions() {
96 ASSERT(current_.extensions != 0);
97 HandleScopeImplementer::instance()->DeleteExtensions(current_.extensions);
98}
99
100
101void HandleScope::ZapRange(Object** start, Object** end) {
102 if (start == NULL) return;
103 for (Object** p = start; p < end; p++) {
104 *reinterpret_cast<Address*>(p) = v8::internal::kHandleZapValue;
105 }
106}
107
108
Steve Blockd0582a62009-12-15 09:54:21 +0000109Address HandleScope::current_extensions_address() {
110 return reinterpret_cast<Address>(&current_.extensions);
111}
112
113
114Address HandleScope::current_next_address() {
115 return reinterpret_cast<Address>(&current_.next);
116}
117
118
119Address HandleScope::current_limit_address() {
120 return reinterpret_cast<Address>(&current_.limit);
121}
122
123
Steve Blocka7e24c12009-10-30 11:49:00 +0000124Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray> content,
125 Handle<JSArray> array) {
126 CALL_HEAP_FUNCTION(content->AddKeysFromJSArray(*array), FixedArray);
127}
128
129
130Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first,
131 Handle<FixedArray> second) {
132 CALL_HEAP_FUNCTION(first->UnionOfKeys(*second), FixedArray);
133}
134
135
136Handle<JSGlobalProxy> ReinitializeJSGlobalProxy(
137 Handle<JSFunction> constructor,
138 Handle<JSGlobalProxy> global) {
139 CALL_HEAP_FUNCTION(Heap::ReinitializeJSGlobalProxy(*constructor, *global),
140 JSGlobalProxy);
141}
142
143
144void SetExpectedNofProperties(Handle<JSFunction> func, int nof) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100145 // If objects constructed from this function exist then changing
146 // 'estimated_nof_properties' is dangerous since the previois value might
147 // have been compiled into the fast construct stub. More over, the inobject
148 // slack tracking logic might have adjusted the previous value, so even
149 // passing the same value is risky.
150 if (func->shared()->live_objects_may_exist()) return;
151
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 func->shared()->set_expected_nof_properties(nof);
153 if (func->has_initial_map()) {
154 Handle<Map> new_initial_map =
155 Factory::CopyMapDropTransitions(Handle<Map>(func->initial_map()));
156 new_initial_map->set_unused_property_fields(nof);
157 func->set_initial_map(*new_initial_map);
158 }
159}
160
161
162void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value) {
163 CALL_HEAP_FUNCTION_VOID(func->SetPrototype(*value));
164}
165
166
167static int ExpectedNofPropertiesFromEstimate(int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100168 // If no properties are added in the constructor, they are more likely
169 // to be added later.
170 if (estimate == 0) estimate = 2;
171
172 // We do not shrink objects that go into a snapshot (yet), so we adjust
173 // the estimate conservatively.
174 if (Serializer::enabled()) return estimate + 2;
175
176 // Inobject slack tracking will reclaim redundant inobject space later,
177 // so we can afford to adjust the estimate generously.
Ben Murdochf87a2032010-10-22 12:50:53 +0100178 return estimate + 8;
Steve Blocka7e24c12009-10-30 11:49:00 +0000179}
180
181
182void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared,
183 int estimate) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100184 // See the comment in SetExpectedNofProperties.
185 if (shared->live_objects_may_exist()) return;
186
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 shared->set_expected_nof_properties(
188 ExpectedNofPropertiesFromEstimate(estimate));
189}
190
191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192void NormalizeProperties(Handle<JSObject> object,
193 PropertyNormalizationMode mode,
194 int expected_additional_properties) {
195 CALL_HEAP_FUNCTION_VOID(object->NormalizeProperties(
196 mode,
197 expected_additional_properties));
198}
199
200
201void NormalizeElements(Handle<JSObject> object) {
202 CALL_HEAP_FUNCTION_VOID(object->NormalizeElements());
203}
204
205
206void TransformToFastProperties(Handle<JSObject> object,
207 int unused_property_fields) {
208 CALL_HEAP_FUNCTION_VOID(
209 object->TransformToFastProperties(unused_property_fields));
210}
211
212
213void FlattenString(Handle<String> string) {
Steve Block6ded16b2010-05-10 14:33:55 +0100214 CALL_HEAP_FUNCTION_VOID(string->TryFlatten());
Steve Block8defd9f2010-07-08 12:39:36 +0100215}
216
217
218Handle<String> FlattenGetString(Handle<String> string) {
219 Handle<String> result;
220 CALL_AND_RETRY(string->TryFlatten(),
221 { result = Handle<String>(String::cast(__object__));
222 break; },
223 return Handle<String>());
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 ASSERT(string->IsFlat());
Steve Block8defd9f2010-07-08 12:39:36 +0100225 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000226}
227
228
229Handle<Object> SetPrototype(Handle<JSFunction> function,
230 Handle<Object> prototype) {
Steve Block6ded16b2010-05-10 14:33:55 +0100231 ASSERT(function->should_have_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 CALL_HEAP_FUNCTION(Accessors::FunctionSetPrototype(*function,
233 *prototype,
234 NULL),
235 Object);
236}
237
238
239Handle<Object> SetProperty(Handle<JSObject> object,
240 Handle<String> key,
241 Handle<Object> value,
242 PropertyAttributes attributes) {
243 CALL_HEAP_FUNCTION(object->SetProperty(*key, *value, attributes), Object);
244}
245
246
247Handle<Object> SetProperty(Handle<Object> object,
248 Handle<Object> key,
249 Handle<Object> value,
250 PropertyAttributes attributes) {
251 CALL_HEAP_FUNCTION(
252 Runtime::SetObjectProperty(object, key, value, attributes), Object);
253}
254
255
256Handle<Object> ForceSetProperty(Handle<JSObject> object,
257 Handle<Object> key,
258 Handle<Object> value,
259 PropertyAttributes attributes) {
260 CALL_HEAP_FUNCTION(
261 Runtime::ForceSetObjectProperty(object, key, value, attributes), Object);
262}
263
264
Andrei Popescu31002712010-02-23 13:46:05 +0000265Handle<Object> SetNormalizedProperty(Handle<JSObject> object,
266 Handle<String> key,
267 Handle<Object> value,
268 PropertyDetails details) {
269 CALL_HEAP_FUNCTION(object->SetNormalizedProperty(*key, *value, details),
270 Object);
271}
272
273
Steve Blocka7e24c12009-10-30 11:49:00 +0000274Handle<Object> ForceDeleteProperty(Handle<JSObject> object,
275 Handle<Object> key) {
276 CALL_HEAP_FUNCTION(Runtime::ForceDeleteObjectProperty(object, key), Object);
277}
278
279
280Handle<Object> IgnoreAttributesAndSetLocalProperty(
281 Handle<JSObject> object,
282 Handle<String> key,
283 Handle<Object> value,
284 PropertyAttributes attributes) {
285 CALL_HEAP_FUNCTION(object->
286 IgnoreAttributesAndSetLocalProperty(*key, *value, attributes), Object);
287}
288
289
290Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object,
291 Handle<String> key,
292 Handle<Object> value,
293 PropertyAttributes attributes) {
294 CALL_HEAP_FUNCTION(object->SetPropertyWithInterceptor(*key,
295 *value,
296 attributes),
297 Object);
298}
299
300
301Handle<Object> GetProperty(Handle<JSObject> obj,
302 const char* name) {
303 Handle<String> str = Factory::LookupAsciiSymbol(name);
304 CALL_HEAP_FUNCTION(obj->GetProperty(*str), Object);
305}
306
307
308Handle<Object> GetProperty(Handle<Object> obj,
309 Handle<Object> key) {
310 CALL_HEAP_FUNCTION(Runtime::GetObjectProperty(obj, key), Object);
311}
312
313
Steve Block6ded16b2010-05-10 14:33:55 +0100314Handle<Object> GetElement(Handle<Object> obj,
315 uint32_t index) {
316 CALL_HEAP_FUNCTION(Runtime::GetElement(obj, index), Object);
317}
318
319
Steve Blocka7e24c12009-10-30 11:49:00 +0000320Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver,
321 Handle<JSObject> holder,
322 Handle<String> name,
323 PropertyAttributes* attributes) {
324 CALL_HEAP_FUNCTION(holder->GetPropertyWithInterceptor(*receiver,
325 *name,
326 attributes),
327 Object);
328}
329
330
331Handle<Object> GetPrototype(Handle<Object> obj) {
332 Handle<Object> result(obj->GetPrototype());
333 return result;
334}
335
336
Andrei Popescu402d9372010-02-26 13:31:12 +0000337Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value) {
338 const bool skip_hidden_prototypes = false;
339 CALL_HEAP_FUNCTION(obj->SetPrototype(*value, skip_hidden_prototypes), Object);
340}
341
342
Steve Blocka7e24c12009-10-30 11:49:00 +0000343Handle<Object> GetHiddenProperties(Handle<JSObject> obj,
344 bool create_if_needed) {
Steve Blockd0582a62009-12-15 09:54:21 +0000345 Object* holder = obj->BypassGlobalProxy();
346 if (holder->IsUndefined()) return Factory::undefined_value();
347 obj = Handle<JSObject>(JSObject::cast(holder));
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
349 if (obj->HasFastProperties()) {
350 // If the object has fast properties, check whether the first slot
351 // in the descriptor array matches the hidden symbol. Since the
352 // hidden symbols hash code is zero (and no other string has hash
353 // code zero) it will always occupy the first entry if present.
354 DescriptorArray* descriptors = obj->map()->instance_descriptors();
355 if ((descriptors->number_of_descriptors() > 0) &&
Steve Blockd0582a62009-12-15 09:54:21 +0000356 (descriptors->GetKey(0) == Heap::hidden_symbol()) &&
Steve Blocka7e24c12009-10-30 11:49:00 +0000357 descriptors->IsProperty(0)) {
358 ASSERT(descriptors->GetType(0) == FIELD);
359 return Handle<Object>(obj->FastPropertyAt(descriptors->GetFieldIndex(0)));
360 }
361 }
362
363 // Only attempt to find the hidden properties in the local object and not
364 // in the prototype chain. Note that HasLocalProperty() can cause a GC in
365 // the general case in the presence of interceptors.
Steve Blockd0582a62009-12-15 09:54:21 +0000366 if (!obj->HasHiddenPropertiesObject()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // Hidden properties object not found. Allocate a new hidden properties
368 // object if requested. Otherwise return the undefined value.
369 if (create_if_needed) {
370 Handle<Object> hidden_obj = Factory::NewJSObject(Top::object_function());
Steve Blockd0582a62009-12-15 09:54:21 +0000371 CALL_HEAP_FUNCTION(obj->SetHiddenPropertiesObject(*hidden_obj), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000372 } else {
373 return Factory::undefined_value();
374 }
375 }
Steve Blockd0582a62009-12-15 09:54:21 +0000376 return Handle<Object>(obj->GetHiddenPropertiesObject());
Steve Blocka7e24c12009-10-30 11:49:00 +0000377}
378
379
380Handle<Object> DeleteElement(Handle<JSObject> obj,
381 uint32_t index) {
382 CALL_HEAP_FUNCTION(obj->DeleteElement(index, JSObject::NORMAL_DELETION),
383 Object);
384}
385
386
387Handle<Object> DeleteProperty(Handle<JSObject> obj,
388 Handle<String> prop) {
389 CALL_HEAP_FUNCTION(obj->DeleteProperty(*prop, JSObject::NORMAL_DELETION),
390 Object);
391}
392
393
394Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index) {
395 CALL_HEAP_FUNCTION(Heap::LookupSingleCharacterStringFromCode(index), Object);
396}
397
398
Steve Block6ded16b2010-05-10 14:33:55 +0100399Handle<String> SubString(Handle<String> str,
400 int start,
401 int end,
402 PretenureFlag pretenure) {
403 CALL_HEAP_FUNCTION(str->SubString(start, end, pretenure), String);
Steve Blocka7e24c12009-10-30 11:49:00 +0000404}
405
406
407Handle<Object> SetElement(Handle<JSObject> object,
408 uint32_t index,
409 Handle<Object> value) {
Steve Block3ce2e202009-11-05 08:53:23 +0000410 if (object->HasPixelElements() || object->HasExternalArrayElements()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 if (!value->IsSmi() && !value->IsHeapNumber() && !value->IsUndefined()) {
412 bool has_exception;
413 Handle<Object> number = Execution::ToNumber(value, &has_exception);
414 if (has_exception) return Handle<Object>();
415 value = number;
416 }
417 }
418 CALL_HEAP_FUNCTION(object->SetElement(index, *value), Object);
419}
420
421
422Handle<JSObject> Copy(Handle<JSObject> obj) {
423 CALL_HEAP_FUNCTION(Heap::CopyJSObject(*obj), JSObject);
424}
425
426
Leon Clarkef7060e22010-06-03 12:02:55 +0100427Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info) {
428 CALL_HEAP_FUNCTION(obj->DefineAccessor(*info), Object);
429}
430
431
Steve Blocka7e24c12009-10-30 11:49:00 +0000432// Wrappers for scripts are kept alive and cached in weak global
433// handles referred from proxy objects held by the scripts as long as
434// they are used. When they are not used anymore, the garbage
435// collector will call the weak callback on the global handle
436// associated with the wrapper and get rid of both the wrapper and the
437// handle.
438static void ClearWrapperCache(Persistent<v8::Value> handle, void*) {
439#ifdef ENABLE_HEAP_PROTECTION
440 // Weak reference callbacks are called as if from outside V8. We
441 // need to reeenter to unprotect the heap.
442 VMState state(OTHER);
443#endif
444 Handle<Object> cache = Utils::OpenHandle(*handle);
445 JSValue* wrapper = JSValue::cast(*cache);
446 Proxy* proxy = Script::cast(wrapper->value())->wrapper();
447 ASSERT(proxy->proxy() == reinterpret_cast<Address>(cache.location()));
448 proxy->set_proxy(0);
449 GlobalHandles::Destroy(cache.location());
450 Counters::script_wrappers.Decrement();
451}
452
453
454Handle<JSValue> GetScriptWrapper(Handle<Script> script) {
455 if (script->wrapper()->proxy() != NULL) {
456 // Return the script wrapper directly from the cache.
457 return Handle<JSValue>(
458 reinterpret_cast<JSValue**>(script->wrapper()->proxy()));
459 }
460
461 // Construct a new script wrapper.
462 Counters::script_wrappers.Increment();
463 Handle<JSFunction> constructor = Top::script_function();
464 Handle<JSValue> result =
465 Handle<JSValue>::cast(Factory::NewJSObject(constructor));
466 result->set_value(*script);
467
468 // Create a new weak global handle and use it to cache the wrapper
469 // for future use. The cache will automatically be cleared by the
470 // garbage collector when it is not used anymore.
471 Handle<Object> handle = GlobalHandles::Create(*result);
472 GlobalHandles::MakeWeak(handle.location(), NULL, &ClearWrapperCache);
473 script->wrapper()->set_proxy(reinterpret_cast<Address>(handle.location()));
474 return result;
475}
476
477
478// Init line_ends array with code positions of line ends inside script
479// source.
480void InitScriptLineEnds(Handle<Script> script) {
481 if (!script->line_ends()->IsUndefined()) return;
482
483 if (!script->source()->IsString()) {
484 ASSERT(script->source()->IsUndefined());
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100485 Handle<FixedArray> empty = Factory::NewFixedArray(0);
486 script->set_line_ends(*empty);
Steve Blockd0582a62009-12-15 09:54:21 +0000487 ASSERT(script->line_ends()->IsFixedArray());
Steve Blocka7e24c12009-10-30 11:49:00 +0000488 return;
489 }
490
491 Handle<String> src(String::cast(script->source()));
Steve Block6ded16b2010-05-10 14:33:55 +0100492
493 Handle<FixedArray> array = CalculateLineEnds(src, true);
494
495 script->set_line_ends(*array);
496 ASSERT(script->line_ends()->IsFixedArray());
497}
498
499
500Handle<FixedArray> CalculateLineEnds(Handle<String> src,
501 bool with_imaginary_last_new_line) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 const int src_len = src->length();
503 Handle<String> new_line = Factory::NewStringFromAscii(CStrVector("\n"));
504
505 // Pass 1: Identify line count.
506 int line_count = 0;
507 int position = 0;
508 while (position != -1 && position < src_len) {
509 position = Runtime::StringMatch(src, new_line, position);
510 if (position != -1) {
511 position++;
512 }
Steve Block6ded16b2010-05-10 14:33:55 +0100513 if (position != -1) {
514 line_count++;
515 } else if (with_imaginary_last_new_line) {
516 // Even if the last line misses a line end, it is counted.
517 line_count++;
518 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 }
520
521 // Pass 2: Fill in line ends positions
522 Handle<FixedArray> array = Factory::NewFixedArray(line_count);
523 int array_index = 0;
524 position = 0;
525 while (position != -1 && position < src_len) {
526 position = Runtime::StringMatch(src, new_line, position);
Steve Block6ded16b2010-05-10 14:33:55 +0100527 if (position != -1) {
528 array->set(array_index++, Smi::FromInt(position++));
529 } else if (with_imaginary_last_new_line) {
530 // If the script does not end with a line ending add the final end
531 // position as just past the last line ending.
532 array->set(array_index++, Smi::FromInt(src_len));
533 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 }
535 ASSERT(array_index == line_count);
536
Steve Block6ded16b2010-05-10 14:33:55 +0100537 return array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}
539
540
541// Convert code position into line number.
542int GetScriptLineNumber(Handle<Script> script, int code_pos) {
543 InitScriptLineEnds(script);
544 AssertNoAllocation no_allocation;
Andrei Popescu402d9372010-02-26 13:31:12 +0000545 FixedArray* line_ends_array = FixedArray::cast(script->line_ends());
Steve Blockd0582a62009-12-15 09:54:21 +0000546 const int line_ends_len = line_ends_array->length();
Steve Blocka7e24c12009-10-30 11:49:00 +0000547
Andrei Popescu402d9372010-02-26 13:31:12 +0000548 if (!line_ends_len)
549 return -1;
550
551 if ((Smi::cast(line_ends_array->get(0)))->value() >= code_pos)
552 return script->line_offset()->value();
553
554 int left = 0;
555 int right = line_ends_len;
556 while (int half = (right - left) / 2) {
557 if ((Smi::cast(line_ends_array->get(left + half)))->value() > code_pos) {
558 right -= half;
559 } else {
560 left += half;
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 }
562 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000563 return right + script->line_offset()->value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000564}
565
566
Steve Block6ded16b2010-05-10 14:33:55 +0100567int GetScriptLineNumberSafe(Handle<Script> script, int code_pos) {
568 AssertNoAllocation no_allocation;
569 if (!script->line_ends()->IsUndefined()) {
570 return GetScriptLineNumber(script, code_pos);
571 }
572 // Slow mode: we do not have line_ends. We have to iterate through source.
573 if (!script->source()->IsString()) {
574 return -1;
575 }
576 String* source = String::cast(script->source());
577 int line = 0;
578 int len = source->length();
579 for (int pos = 0; pos < len; pos++) {
580 if (pos == code_pos) {
581 break;
582 }
583 if (source->Get(pos) == '\n') {
584 line++;
585 }
586 }
587 return line;
588}
589
590
Steve Blocka7e24c12009-10-30 11:49:00 +0000591void CustomArguments::IterateInstance(ObjectVisitor* v) {
Steve Block6ded16b2010-05-10 14:33:55 +0100592 v->VisitPointers(values_, values_ + ARRAY_SIZE(values_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000593}
594
595
596// Compute the property keys from the interceptor.
597v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver,
598 Handle<JSObject> object) {
599 Handle<InterceptorInfo> interceptor(object->GetNamedInterceptor());
600 CustomArguments args(interceptor->data(), *receiver, *object);
601 v8::AccessorInfo info(args.end());
602 v8::Handle<v8::Array> result;
603 if (!interceptor->enumerator()->IsUndefined()) {
604 v8::NamedPropertyEnumerator enum_fun =
605 v8::ToCData<v8::NamedPropertyEnumerator>(interceptor->enumerator());
606 LOG(ApiObjectAccess("interceptor-named-enum", *object));
607 {
608 // Leaving JavaScript.
609 VMState state(EXTERNAL);
610 result = enum_fun(info);
611 }
612 }
613 return result;
614}
615
616
617// Compute the element keys from the interceptor.
618v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver,
619 Handle<JSObject> object) {
620 Handle<InterceptorInfo> interceptor(object->GetIndexedInterceptor());
621 CustomArguments args(interceptor->data(), *receiver, *object);
622 v8::AccessorInfo info(args.end());
623 v8::Handle<v8::Array> result;
624 if (!interceptor->enumerator()->IsUndefined()) {
625 v8::IndexedPropertyEnumerator enum_fun =
626 v8::ToCData<v8::IndexedPropertyEnumerator>(interceptor->enumerator());
627 LOG(ApiObjectAccess("interceptor-indexed-enum", *object));
628 {
629 // Leaving JavaScript.
630 VMState state(EXTERNAL);
631 result = enum_fun(info);
632 }
633 }
634 return result;
635}
636
637
Ben Murdochf87a2032010-10-22 12:50:53 +0100638static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
639 int len = array->length();
640 for (int i = 0; i < len; i++) {
641 Object* e = array->get(i);
642 if (!(e->IsString() || e->IsNumber())) return false;
643 }
644 return true;
645}
646
647
Steve Blocka7e24c12009-10-30 11:49:00 +0000648Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object,
649 KeyCollectionType type) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100650 USE(ContainsOnlyValidKeys);
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 Handle<FixedArray> content = Factory::empty_fixed_array();
Steve Blockd0582a62009-12-15 09:54:21 +0000652 Handle<JSObject> arguments_boilerplate =
653 Handle<JSObject>(
654 Top::context()->global_context()->arguments_boilerplate());
655 Handle<JSFunction> arguments_function =
656 Handle<JSFunction>(
657 JSFunction::cast(arguments_boilerplate->map()->constructor()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000658
659 // Only collect keys if access is permitted.
660 for (Handle<Object> p = object;
661 *p != Heap::null_value();
662 p = Handle<Object>(p->GetPrototype())) {
663 Handle<JSObject> current(JSObject::cast(*p));
664
665 // Check access rights if required.
666 if (current->IsAccessCheckNeeded() &&
Iain Merrick75681382010-08-19 15:07:18 +0100667 !Top::MayNamedAccess(*current, Heap::undefined_value(),
668 v8::ACCESS_KEYS)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 Top::ReportFailedAccessCheck(*current, v8::ACCESS_KEYS);
670 break;
671 }
672
673 // Compute the element keys.
674 Handle<FixedArray> element_keys =
675 Factory::NewFixedArray(current->NumberOfEnumElements());
676 current->GetEnumElementKeys(*element_keys);
677 content = UnionOfKeys(content, element_keys);
Ben Murdochf87a2032010-10-22 12:50:53 +0100678 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000679
680 // Add the element keys from the interceptor.
681 if (current->HasIndexedInterceptor()) {
682 v8::Handle<v8::Array> result =
683 GetKeysForIndexedInterceptor(object, current);
684 if (!result.IsEmpty())
685 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100686 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000687 }
688
Steve Blockd0582a62009-12-15 09:54:21 +0000689 // We can cache the computed property keys if access checks are
690 // not needed and no interceptors are involved.
691 //
692 // We do not use the cache if the object has elements and
693 // therefore it does not make sense to cache the property names
694 // for arguments objects. Arguments objects will always have
695 // elements.
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100696 // Wrapped strings have elements, but don't have an elements
697 // array or dictionary. So the fast inline test for whether to
698 // use the cache says yes, so we should not create a cache.
Steve Blockd0582a62009-12-15 09:54:21 +0000699 bool cache_enum_keys =
700 ((current->map()->constructor() != *arguments_function) &&
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100701 !current->IsJSValue() &&
Steve Blockd0582a62009-12-15 09:54:21 +0000702 !current->IsAccessCheckNeeded() &&
703 !current->HasNamedInterceptor() &&
704 !current->HasIndexedInterceptor());
705 // Compute the property keys and cache them if possible.
706 content =
707 UnionOfKeys(content, GetEnumPropertyKeys(current, cache_enum_keys));
Ben Murdochf87a2032010-10-22 12:50:53 +0100708 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000709
710 // Add the property keys from the interceptor.
711 if (current->HasNamedInterceptor()) {
712 v8::Handle<v8::Array> result =
713 GetKeysForNamedInterceptor(object, current);
714 if (!result.IsEmpty())
715 content = AddKeysFromJSArray(content, v8::Utils::OpenHandle(*result));
Ben Murdochf87a2032010-10-22 12:50:53 +0100716 ASSERT(ContainsOnlyValidKeys(content));
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 }
718
719 // If we only want local properties we bail out after the first
720 // iteration.
721 if (type == LOCAL_ONLY)
722 break;
723 }
724 return content;
725}
726
727
728Handle<JSArray> GetKeysFor(Handle<JSObject> object) {
729 Counters::for_in.Increment();
730 Handle<FixedArray> elements = GetKeysInFixedArrayFor(object,
731 INCLUDE_PROTOS);
732 return Factory::NewJSArrayWithElements(elements);
733}
734
735
Steve Blockd0582a62009-12-15 09:54:21 +0000736Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object,
737 bool cache_result) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000738 int index = 0;
739 if (object->HasFastProperties()) {
740 if (object->map()->instance_descriptors()->HasEnumCache()) {
741 Counters::enum_cache_hits.Increment();
742 DescriptorArray* desc = object->map()->instance_descriptors();
743 return Handle<FixedArray>(FixedArray::cast(desc->GetEnumCache()));
744 }
745 Counters::enum_cache_misses.Increment();
746 int num_enum = object->NumberOfEnumProperties();
747 Handle<FixedArray> storage = Factory::NewFixedArray(num_enum);
748 Handle<FixedArray> sort_array = Factory::NewFixedArray(num_enum);
749 Handle<DescriptorArray> descs =
750 Handle<DescriptorArray>(object->map()->instance_descriptors());
751 for (int i = 0; i < descs->number_of_descriptors(); i++) {
752 if (descs->IsProperty(i) && !descs->IsDontEnum(i)) {
753 (*storage)->set(index, descs->GetKey(i));
754 PropertyDetails details(descs->GetDetails(i));
755 (*sort_array)->set(index, Smi::FromInt(details.index()));
756 index++;
757 }
758 }
759 (*storage)->SortPairs(*sort_array, sort_array->length());
Steve Blockd0582a62009-12-15 09:54:21 +0000760 if (cache_result) {
761 Handle<FixedArray> bridge_storage =
762 Factory::NewFixedArray(DescriptorArray::kEnumCacheBridgeLength);
763 DescriptorArray* desc = object->map()->instance_descriptors();
764 desc->SetEnumCache(*bridge_storage, *storage);
765 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 ASSERT(storage->length() == index);
767 return storage;
768 } else {
769 int num_enum = object->NumberOfEnumProperties();
770 Handle<FixedArray> storage = Factory::NewFixedArray(num_enum);
771 Handle<FixedArray> sort_array = Factory::NewFixedArray(num_enum);
772 object->property_dictionary()->CopyEnumKeysTo(*storage, *sort_array);
773 return storage;
774 }
775}
776
777
Leon Clarke4515c472010-02-03 11:58:03 +0000778bool EnsureCompiled(Handle<SharedFunctionInfo> shared,
779 ClearExceptionFlag flag) {
780 return shared->is_compiled() || CompileLazyShared(shared, flag);
781}
782
783
784static bool CompileLazyHelper(CompilationInfo* info,
785 ClearExceptionFlag flag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000786 // Compile the source information to a code object.
Leon Clarke4515c472010-02-03 11:58:03 +0000787 ASSERT(!info->shared_info()->is_compiled());
788 bool result = Compiler::CompileLazy(info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000789 ASSERT(result != Top::has_pending_exception());
790 if (!result && flag == CLEAR_EXCEPTION) Top::clear_pending_exception();
791 return result;
792}
793
794
Leon Clarke4515c472010-02-03 11:58:03 +0000795bool CompileLazyShared(Handle<SharedFunctionInfo> shared,
796 ClearExceptionFlag flag) {
Andrei Popescu31002712010-02-23 13:46:05 +0000797 CompilationInfo info(shared);
Leon Clarke4515c472010-02-03 11:58:03 +0000798 return CompileLazyHelper(&info, flag);
799}
800
801
802bool CompileLazy(Handle<JSFunction> function,
Leon Clarke4515c472010-02-03 11:58:03 +0000803 ClearExceptionFlag flag) {
Iain Merrick75681382010-08-19 15:07:18 +0100804 if (function->shared()->is_compiled()) {
805 function->set_code(function->shared()->code());
Ben Murdochf87a2032010-10-22 12:50:53 +0100806 PROFILE(FunctionCreateEvent(*function));
Iain Merrick75681382010-08-19 15:07:18 +0100807 function->shared()->set_code_age(0);
808 return true;
809 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +0100810 CompilationInfo info(function);
Iain Merrick75681382010-08-19 15:07:18 +0100811 bool result = CompileLazyHelper(&info, flag);
Ben Murdochf87a2032010-10-22 12:50:53 +0100812 ASSERT(!result || function->is_compiled());
Iain Merrick75681382010-08-19 15:07:18 +0100813 PROFILE(FunctionCreateEvent(*function));
814 return result;
815 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000816}
817
818
Leon Clarke4515c472010-02-03 11:58:03 +0000819bool CompileLazyInLoop(Handle<JSFunction> function,
Leon Clarke4515c472010-02-03 11:58:03 +0000820 ClearExceptionFlag flag) {
Iain Merrick75681382010-08-19 15:07:18 +0100821 if (function->shared()->is_compiled()) {
822 function->set_code(function->shared()->code());
Ben Murdochf87a2032010-10-22 12:50:53 +0100823 PROFILE(FunctionCreateEvent(*function));
Iain Merrick75681382010-08-19 15:07:18 +0100824 function->shared()->set_code_age(0);
825 return true;
826 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +0100827 CompilationInfo info(function);
828 info.MarkAsInLoop();
Iain Merrick75681382010-08-19 15:07:18 +0100829 bool result = CompileLazyHelper(&info, flag);
Ben Murdochf87a2032010-10-22 12:50:53 +0100830 ASSERT(!result || function->is_compiled());
Iain Merrick75681382010-08-19 15:07:18 +0100831 PROFILE(FunctionCreateEvent(*function));
832 return result;
833 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000834}
835
Leon Clarke4515c472010-02-03 11:58:03 +0000836
Steve Blocka7e24c12009-10-30 11:49:00 +0000837OptimizedObjectForAddingMultipleProperties::
838OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object,
839 int expected_additional_properties,
840 bool condition) {
841 object_ = object;
842 if (condition && object_->HasFastProperties()) {
843 // Normalize the properties of object to avoid n^2 behavior
844 // when extending the object multiple properties. Indicate the number of
845 // properties to be added.
846 unused_property_fields_ = object->map()->unused_property_fields();
847 NormalizeProperties(object_,
848 KEEP_INOBJECT_PROPERTIES,
849 expected_additional_properties);
850 has_been_transformed_ = true;
851
852 } else {
853 has_been_transformed_ = false;
854 }
855}
856
857
Steve Blocka7e24c12009-10-30 11:49:00 +0000858OptimizedObjectForAddingMultipleProperties::
859~OptimizedObjectForAddingMultipleProperties() {
860 // Reoptimize the object to allow fast property access.
861 if (has_been_transformed_) {
862 TransformToFastProperties(object_, unused_property_fields_);
863 }
864}
865
Steve Blocka7e24c12009-10-30 11:49:00 +0000866} } // namespace v8::internal