Upstream version 9.38.207.0
[platform/framework/web/crosswalk.git] / src / v8 / src / api.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_API_H_
6 #define V8_API_H_
7
8 #include "src/v8.h"
9
10 #include "include/v8-testing.h"
11 #include "src/contexts.h"
12 #include "src/factory.h"
13 #include "src/isolate.h"
14 #include "src/list-inl.h"
15
16 namespace v8 {
17
18 // Constants used in the implementation of the API.  The most natural thing
19 // would usually be to place these with the classes that use them, but
20 // we want to keep them out of v8.h because it is an externally
21 // visible file.
22 class Consts {
23  public:
24   enum TemplateType {
25     FUNCTION_TEMPLATE = 0,
26     OBJECT_TEMPLATE = 1
27   };
28 };
29
30
31 // Utilities for working with neander-objects, primitive
32 // env-independent JSObjects used by the api.
33 class NeanderObject {
34  public:
35   explicit NeanderObject(v8::internal::Isolate* isolate, int size);
36   explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
37   explicit inline NeanderObject(v8::internal::Object* obj);
38   inline v8::internal::Object* get(int index);
39   inline void set(int index, v8::internal::Object* value);
40   inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
41   int size();
42  private:
43   v8::internal::Handle<v8::internal::JSObject> value_;
44 };
45
46
47 // Utilities for working with neander-arrays, a simple extensible
48 // array abstraction built on neander-objects.
49 class NeanderArray {
50  public:
51   explicit NeanderArray(v8::internal::Isolate* isolate);
52   explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
53   inline v8::internal::Handle<v8::internal::JSObject> value() {
54     return obj_.value();
55   }
56
57   void add(v8::internal::Handle<v8::internal::Object> value);
58
59   int length();
60
61   v8::internal::Object* get(int index);
62   // Change the value at an index to undefined value. If the index is
63   // out of bounds, the request is ignored. Returns the old value.
64   void set(int index, v8::internal::Object* value);
65  private:
66   NeanderObject obj_;
67 };
68
69
70 NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
71     : value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
72
73
74 NeanderObject::NeanderObject(v8::internal::Object* obj)
75     : value_(v8::internal::Handle<v8::internal::JSObject>(
76         v8::internal::JSObject::cast(obj))) { }
77
78
79 NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
80     : obj_(obj) { }
81
82
83 v8::internal::Object* NeanderObject::get(int offset) {
84   DCHECK(value()->HasFastObjectElements());
85   return v8::internal::FixedArray::cast(value()->elements())->get(offset);
86 }
87
88
89 void NeanderObject::set(int offset, v8::internal::Object* value) {
90   DCHECK(value_->HasFastObjectElements());
91   v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
92 }
93
94
95 template <typename T> inline T ToCData(v8::internal::Object* obj) {
96   STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
97   return reinterpret_cast<T>(
98       reinterpret_cast<intptr_t>(
99           v8::internal::Foreign::cast(obj)->foreign_address()));
100 }
101
102
103 template <typename T>
104 inline v8::internal::Handle<v8::internal::Object> FromCData(
105     v8::internal::Isolate* isolate, T obj) {
106   STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
107   return isolate->factory()->NewForeign(
108       reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
109 }
110
111
112 class ApiFunction {
113  public:
114   explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { }
115   v8::internal::Address address() { return addr_; }
116  private:
117   v8::internal::Address addr_;
118 };
119
120
121
122 class RegisteredExtension {
123  public:
124   explicit RegisteredExtension(Extension* extension);
125   static void Register(RegisteredExtension* that);
126   static void UnregisterAll();
127   Extension* extension() { return extension_; }
128   RegisteredExtension* next() { return next_; }
129   static RegisteredExtension* first_extension() { return first_extension_; }
130  private:
131   Extension* extension_;
132   RegisteredExtension* next_;
133   static RegisteredExtension* first_extension_;
134 };
135
136
137 #define OPEN_HANDLE_LIST(V)                    \
138   V(Template, TemplateInfo)                    \
139   V(FunctionTemplate, FunctionTemplateInfo)    \
140   V(ObjectTemplate, ObjectTemplateInfo)        \
141   V(Signature, SignatureInfo)                  \
142   V(AccessorSignature, FunctionTemplateInfo)   \
143   V(TypeSwitch, TypeSwitchInfo)                \
144   V(Data, Object)                              \
145   V(RegExp, JSRegExp)                          \
146   V(Object, JSObject)                          \
147   V(Array, JSArray)                            \
148   V(ArrayBuffer, JSArrayBuffer)                \
149   V(ArrayBufferView, JSArrayBufferView)        \
150   V(TypedArray, JSTypedArray)                  \
151   V(Uint8Array, JSTypedArray)                  \
152   V(Uint8ClampedArray, JSTypedArray)           \
153   V(Int8Array, JSTypedArray)                   \
154   V(Uint16Array, JSTypedArray)                 \
155   V(Int16Array, JSTypedArray)                  \
156   V(Uint32Array, JSTypedArray)                 \
157   V(Int32Array, JSTypedArray)                  \
158   V(Float32Array, JSTypedArray)                \
159   V(Float32x4Array, JSTypedArray)              \
160   V(Float64x2Array, JSTypedArray)              \
161   V(Int32x4Array, JSTypedArray)                \
162   V(Float64Array, JSTypedArray)                \
163   V(DataView, JSDataView)                      \
164   V(String, String)                            \
165   V(Symbol, Symbol)                            \
166   V(Script, JSFunction)                        \
167   V(UnboundScript, SharedFunctionInfo)         \
168   V(Function, JSFunction)                      \
169   V(Message, JSMessageObject)                  \
170   V(Context, Context)                          \
171   V(External, Object)                          \
172   V(StackTrace, JSArray)                       \
173   V(StackFrame, JSObject)                      \
174   V(DeclaredAccessorDescriptor, DeclaredAccessorDescriptor)
175
176
177 class Utils {
178  public:
179   static inline bool ApiCheck(bool condition,
180                               const char* location,
181                               const char* message) {
182     if (!condition) Utils::ReportApiFailure(location, message);
183     return condition;
184   }
185
186   static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
187   static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
188
189   static inline Local<Context> ToLocal(
190       v8::internal::Handle<v8::internal::Context> obj);
191   static inline Local<Value> ToLocal(
192       v8::internal::Handle<v8::internal::Object> obj);
193   static inline Local<Function> ToLocal(
194       v8::internal::Handle<v8::internal::JSFunction> obj);
195   static inline Local<String> ToLocal(
196       v8::internal::Handle<v8::internal::String> obj);
197   static inline Local<Symbol> ToLocal(
198       v8::internal::Handle<v8::internal::Symbol> obj);
199   static inline Local<RegExp> ToLocal(
200       v8::internal::Handle<v8::internal::JSRegExp> obj);
201   static inline Local<Object> ToLocal(
202       v8::internal::Handle<v8::internal::JSObject> obj);
203   static inline Local<Array> ToLocal(
204       v8::internal::Handle<v8::internal::JSArray> obj);
205   static inline Local<ArrayBuffer> ToLocal(
206       v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
207   static inline Local<ArrayBufferView> ToLocal(
208       v8::internal::Handle<v8::internal::JSArrayBufferView> obj);
209   static inline Local<DataView> ToLocal(
210       v8::internal::Handle<v8::internal::JSDataView> obj);
211
212   static inline Local<TypedArray> ToLocal(
213       v8::internal::Handle<v8::internal::JSTypedArray> obj);
214   static inline Local<Uint8Array> ToLocalUint8Array(
215       v8::internal::Handle<v8::internal::JSTypedArray> obj);
216   static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray(
217       v8::internal::Handle<v8::internal::JSTypedArray> obj);
218   static inline Local<Int8Array> ToLocalInt8Array(
219       v8::internal::Handle<v8::internal::JSTypedArray> obj);
220   static inline Local<Uint16Array> ToLocalUint16Array(
221       v8::internal::Handle<v8::internal::JSTypedArray> obj);
222   static inline Local<Int16Array> ToLocalInt16Array(
223       v8::internal::Handle<v8::internal::JSTypedArray> obj);
224   static inline Local<Uint32Array> ToLocalUint32Array(
225       v8::internal::Handle<v8::internal::JSTypedArray> obj);
226   static inline Local<Int32Array> ToLocalInt32Array(
227       v8::internal::Handle<v8::internal::JSTypedArray> obj);
228   static inline Local<Float32Array> ToLocalFloat32Array(
229       v8::internal::Handle<v8::internal::JSTypedArray> obj);
230   static inline Local<Float32x4Array> ToLocalFloat32x4Array(
231       v8::internal::Handle<v8::internal::JSTypedArray> obj);
232   static inline Local<Float64x2Array> ToLocalFloat64x2Array(
233       v8::internal::Handle<v8::internal::JSTypedArray> obj);
234   static inline Local<Int32x4Array> ToLocalInt32x4Array(
235       v8::internal::Handle<v8::internal::JSTypedArray> obj);
236   static inline Local<Float64Array> ToLocalFloat64Array(
237       v8::internal::Handle<v8::internal::JSTypedArray> obj);
238
239   static inline Local<Message> MessageToLocal(
240       v8::internal::Handle<v8::internal::Object> obj);
241   static inline Local<StackTrace> StackTraceToLocal(
242       v8::internal::Handle<v8::internal::JSArray> obj);
243   static inline Local<StackFrame> StackFrameToLocal(
244       v8::internal::Handle<v8::internal::JSObject> obj);
245   static inline Local<Number> NumberToLocal(
246       v8::internal::Handle<v8::internal::Object> obj);
247   static inline Local<Integer> IntegerToLocal(
248       v8::internal::Handle<v8::internal::Object> obj);
249   static inline Local<Uint32> Uint32ToLocal(
250       v8::internal::Handle<v8::internal::Object> obj);
251   static inline Local<FunctionTemplate> ToLocal(
252       v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
253   static inline Local<ObjectTemplate> ToLocal(
254       v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
255   static inline Local<Signature> ToLocal(
256       v8::internal::Handle<v8::internal::SignatureInfo> obj);
257   static inline Local<AccessorSignature> AccessorSignatureToLocal(
258       v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
259   static inline Local<TypeSwitch> ToLocal(
260       v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
261   static inline Local<External> ExternalToLocal(
262       v8::internal::Handle<v8::internal::JSObject> obj);
263   static inline Local<DeclaredAccessorDescriptor> ToLocal(
264       v8::internal::Handle<v8::internal::DeclaredAccessorDescriptor> obj);
265
266 #define DECLARE_OPEN_HANDLE(From, To) \
267   static inline v8::internal::Handle<v8::internal::To> \
268       OpenHandle(const From* that, bool allow_empty_handle = false);
269
270 OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE)
271
272 #undef DECLARE_OPEN_HANDLE
273
274   template<class From, class To>
275   static inline Local<To> Convert(v8::internal::Handle<From> obj) {
276     DCHECK(obj.is_null() || !obj->IsTheHole());
277     return Local<To>(reinterpret_cast<To*>(obj.location()));
278   }
279
280   template <class T>
281   static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
282       const v8::Persistent<T>& persistent) {
283     return v8::internal::Handle<v8::internal::Object>(
284         reinterpret_cast<v8::internal::Object**>(persistent.val_));
285   }
286
287   template <class T>
288   static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
289       v8::Persistent<T>* persistent) {
290     return OpenPersistent(*persistent);
291   }
292
293   template <class From, class To>
294   static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) {
295     return OpenHandle(*handle);
296   }
297
298  private:
299   static void ReportApiFailure(const char* location, const char* message);
300 };
301
302
303 template <class T>
304 v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
305     v8::EscapableHandleScope* scope) {
306   v8::internal::Handle<T> handle;
307   if (!is_null()) {
308     handle = *this;
309   }
310   return Utils::OpenHandle(*scope->Escape(Utils::ToLocal(handle)), true);
311 }
312
313
314 template <class T>
315 inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
316   return reinterpret_cast<T*>(obj.location());
317 }
318
319 template <class T>
320 inline v8::Local<T> ToApiHandle(
321     v8::internal::Handle<v8::internal::Object> obj) {
322   return Utils::Convert<v8::internal::Object, T>(obj);
323 }
324
325
326 // Implementations of ToLocal
327
328 #define MAKE_TO_LOCAL(Name, From, To)                                       \
329   Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
330     return Convert<v8::internal::From, v8::To>(obj);  \
331   }
332
333
334 #define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size)        \
335   Local<v8::Type##Array> Utils::ToLocal##Type##Array(                       \
336       v8::internal::Handle<v8::internal::JSTypedArray> obj) {               \
337     DCHECK(obj->type() == kExternal##Type##Array);                          \
338     return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj);       \
339   }
340
341
342 MAKE_TO_LOCAL(ToLocal, Context, Context)
343 MAKE_TO_LOCAL(ToLocal, Object, Value)
344 MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
345 MAKE_TO_LOCAL(ToLocal, String, String)
346 MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
347 MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
348 MAKE_TO_LOCAL(ToLocal, JSObject, Object)
349 MAKE_TO_LOCAL(ToLocal, JSArray, Array)
350 MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
351 MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
352 MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
353 MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
354
355 TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
356
357 MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
358 MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
359 MAKE_TO_LOCAL(ToLocal, SignatureInfo, Signature)
360 MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
361 MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
362 MAKE_TO_LOCAL(MessageToLocal, Object, Message)
363 MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace)
364 MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame)
365 MAKE_TO_LOCAL(NumberToLocal, Object, Number)
366 MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
367 MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
368 MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
369 MAKE_TO_LOCAL(ToLocal, DeclaredAccessorDescriptor, DeclaredAccessorDescriptor)
370
371 #undef MAKE_TO_LOCAL_TYPED_ARRAY
372 #undef MAKE_TO_LOCAL
373
374
375 // Implementations of OpenHandle
376
377 #define MAKE_OPEN_HANDLE(From, To)                                          \
378   v8::internal::Handle<v8::internal::To> Utils::OpenHandle(                 \
379     const v8::From* that, bool allow_empty_handle) {                        \
380     EXTRA_CHECK(allow_empty_handle || that != NULL);                        \
381     EXTRA_CHECK(that == NULL ||                                             \
382         (*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
383     return v8::internal::Handle<v8::internal::To>(                          \
384         reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
385   }
386
387 OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
388
389 #undef MAKE_OPEN_HANDLE
390 #undef OPEN_HANDLE_LIST
391
392
393 namespace internal {
394
395 // Tracks string usage to help make better decisions when
396 // externalizing strings.
397 //
398 // Implementation note: internally this class only tracks fresh
399 // strings and keeps a single use counter for them.
400 class StringTracker {
401  public:
402   // Records that the given string's characters were copied to some
403   // external buffer. If this happens often we should honor
404   // externalization requests for the string.
405   void RecordWrite(Handle<String> string) {
406     Address address = reinterpret_cast<Address>(*string);
407     Address top = isolate_->heap()->NewSpaceTop();
408     if (IsFreshString(address, top)) {
409       IncrementUseCount(top);
410     }
411   }
412
413   // Estimates freshness and use frequency of the given string based
414   // on how close it is to the new space top and the recorded usage
415   // history.
416   inline bool IsFreshUnusedString(Handle<String> string) {
417     Address address = reinterpret_cast<Address>(*string);
418     Address top = isolate_->heap()->NewSpaceTop();
419     return IsFreshString(address, top) && IsUseCountLow(top);
420   }
421
422  private:
423   StringTracker() : use_count_(0), last_top_(NULL), isolate_(NULL) { }
424
425   static inline bool IsFreshString(Address string, Address top) {
426     return top - kFreshnessLimit <= string && string <= top;
427   }
428
429   inline bool IsUseCountLow(Address top) {
430     if (last_top_ != top) return true;
431     return use_count_ < kUseLimit;
432   }
433
434   inline void IncrementUseCount(Address top) {
435     if (last_top_ != top) {
436       use_count_ = 0;
437       last_top_ = top;
438     }
439     ++use_count_;
440   }
441
442   // Single use counter shared by all fresh strings.
443   int use_count_;
444
445   // Last new space top when the use count above was valid.
446   Address last_top_;
447
448   Isolate* isolate_;
449
450   // How close to the new space top a fresh string has to be.
451   static const int kFreshnessLimit = 1024;
452
453   // The number of uses required to consider a string useful.
454   static const int kUseLimit = 32;
455
456   friend class Isolate;
457
458   DISALLOW_COPY_AND_ASSIGN(StringTracker);
459 };
460
461
462 class DeferredHandles {
463  public:
464   ~DeferredHandles();
465
466  private:
467   DeferredHandles(Object** first_block_limit, Isolate* isolate)
468       : next_(NULL),
469         previous_(NULL),
470         first_block_limit_(first_block_limit),
471         isolate_(isolate) {
472     isolate->LinkDeferredHandles(this);
473   }
474
475   void Iterate(ObjectVisitor* v);
476
477   List<Object**> blocks_;
478   DeferredHandles* next_;
479   DeferredHandles* previous_;
480   Object** first_block_limit_;
481   Isolate* isolate_;
482
483   friend class HandleScopeImplementer;
484   friend class Isolate;
485 };
486
487
488 // This class is here in order to be able to declare it a friend of
489 // HandleScope.  Moving these methods to be members of HandleScope would be
490 // neat in some ways, but it would expose internal implementation details in
491 // our public header file, which is undesirable.
492 //
493 // An isolate has a single instance of this class to hold the current thread's
494 // data. In multithreaded V8 programs this data is copied in and out of storage
495 // so that the currently executing thread always has its own copy of this
496 // data.
497 class HandleScopeImplementer {
498  public:
499   explicit HandleScopeImplementer(Isolate* isolate)
500       : isolate_(isolate),
501         blocks_(0),
502         entered_contexts_(0),
503         saved_contexts_(0),
504         spare_(NULL),
505         call_depth_(0),
506         last_handle_before_deferred_block_(NULL) { }
507
508   ~HandleScopeImplementer() {
509     DeleteArray(spare_);
510   }
511
512   // Threading support for handle data.
513   static int ArchiveSpacePerThread();
514   char* RestoreThread(char* from);
515   char* ArchiveThread(char* to);
516   void FreeThreadResources();
517
518   // Garbage collection support.
519   void Iterate(v8::internal::ObjectVisitor* v);
520   static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
521
522
523   inline internal::Object** GetSpareOrNewBlock();
524   inline void DeleteExtensions(internal::Object** prev_limit);
525
526   inline void IncrementCallDepth() {call_depth_++;}
527   inline void DecrementCallDepth() {call_depth_--;}
528   inline bool CallDepthIsZero() { return call_depth_ == 0; }
529
530   inline void EnterContext(Handle<Context> context);
531   inline void LeaveContext();
532   inline bool LastEnteredContextWas(Handle<Context> context);
533
534   // Returns the last entered context or an empty handle if no
535   // contexts have been entered.
536   inline Handle<Context> LastEnteredContext();
537
538   inline void SaveContext(Context* context);
539   inline Context* RestoreContext();
540   inline bool HasSavedContexts();
541
542   inline List<internal::Object**>* blocks() { return &blocks_; }
543   Isolate* isolate() const { return isolate_; }
544
545   void ReturnBlock(Object** block) {
546     DCHECK(block != NULL);
547     if (spare_ != NULL) DeleteArray(spare_);
548     spare_ = block;
549   }
550
551  private:
552   void ResetAfterArchive() {
553     blocks_.Initialize(0);
554     entered_contexts_.Initialize(0);
555     saved_contexts_.Initialize(0);
556     spare_ = NULL;
557     last_handle_before_deferred_block_ = NULL;
558     call_depth_ = 0;
559   }
560
561   void Free() {
562     DCHECK(blocks_.length() == 0);
563     DCHECK(entered_contexts_.length() == 0);
564     DCHECK(saved_contexts_.length() == 0);
565     blocks_.Free();
566     entered_contexts_.Free();
567     saved_contexts_.Free();
568     if (spare_ != NULL) {
569       DeleteArray(spare_);
570       spare_ = NULL;
571     }
572     DCHECK(call_depth_ == 0);
573   }
574
575   void BeginDeferredScope();
576   DeferredHandles* Detach(Object** prev_limit);
577
578   Isolate* isolate_;
579   List<internal::Object**> blocks_;
580   // Used as a stack to keep track of entered contexts.
581   List<Context*> entered_contexts_;
582   // Used as a stack to keep track of saved contexts.
583   List<Context*> saved_contexts_;
584   Object** spare_;
585   int call_depth_;
586   Object** last_handle_before_deferred_block_;
587   // This is only used for threading support.
588   HandleScopeData handle_scope_data_;
589
590   void IterateThis(ObjectVisitor* v);
591   char* RestoreThreadHelper(char* from);
592   char* ArchiveThreadHelper(char* to);
593
594   friend class DeferredHandles;
595   friend class DeferredHandleScope;
596
597   DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
598 };
599
600
601 const int kHandleBlockSize = v8::internal::KB - 2;  // fit in one page
602
603
604 void HandleScopeImplementer::SaveContext(Context* context) {
605   saved_contexts_.Add(context);
606 }
607
608
609 Context* HandleScopeImplementer::RestoreContext() {
610   return saved_contexts_.RemoveLast();
611 }
612
613
614 bool HandleScopeImplementer::HasSavedContexts() {
615   return !saved_contexts_.is_empty();
616 }
617
618
619 void HandleScopeImplementer::EnterContext(Handle<Context> context) {
620   entered_contexts_.Add(*context);
621 }
622
623
624 void HandleScopeImplementer::LeaveContext() {
625   entered_contexts_.RemoveLast();
626 }
627
628
629 bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) {
630   return !entered_contexts_.is_empty() && entered_contexts_.last() == *context;
631 }
632
633
634 Handle<Context> HandleScopeImplementer::LastEnteredContext() {
635   if (entered_contexts_.is_empty()) return Handle<Context>::null();
636   return Handle<Context>(entered_contexts_.last());
637 }
638
639
640 // If there's a spare block, use it for growing the current scope.
641 internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
642   internal::Object** block = (spare_ != NULL) ?
643       spare_ :
644       NewArray<internal::Object*>(kHandleBlockSize);
645   spare_ = NULL;
646   return block;
647 }
648
649
650 void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
651   while (!blocks_.is_empty()) {
652     internal::Object** block_start = blocks_.last();
653     internal::Object** block_limit = block_start + kHandleBlockSize;
654 #ifdef DEBUG
655     // SealHandleScope may make the prev_limit to point inside the block.
656     if (block_start <= prev_limit && prev_limit <= block_limit) {
657 #ifdef ENABLE_HANDLE_ZAPPING
658       internal::HandleScope::ZapRange(prev_limit, block_limit);
659 #endif
660       break;
661     }
662 #else
663     if (prev_limit == block_limit) break;
664 #endif
665
666     blocks_.RemoveLast();
667 #ifdef ENABLE_HANDLE_ZAPPING
668     internal::HandleScope::ZapRange(block_start, block_limit);
669 #endif
670     if (spare_ != NULL) {
671       DeleteArray(spare_);
672     }
673     spare_ = block_start;
674   }
675   DCHECK((blocks_.is_empty() && prev_limit == NULL) ||
676          (!blocks_.is_empty() && prev_limit != NULL));
677 }
678
679
680 // Interceptor functions called from generated inline caches to notify
681 // CPU profiler that external callbacks are invoked.
682 void InvokeAccessorGetterCallback(
683     v8::Local<v8::String> property,
684     const v8::PropertyCallbackInfo<v8::Value>& info,
685     v8::AccessorGetterCallback getter);
686
687 void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
688                             v8::FunctionCallback callback);
689
690 class Testing {
691  public:
692   static v8::Testing::StressType stress_type() { return stress_type_; }
693   static void set_stress_type(v8::Testing::StressType stress_type) {
694     stress_type_ = stress_type;
695   }
696
697  private:
698   static v8::Testing::StressType stress_type_;
699 };
700
701 } }  // namespace v8::internal
702
703 #endif  // V8_API_H_