v8  5.1.281 (node 6.17.1)
V8 is Google's open source JavaScript engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
v8.h
Go to the documentation of this file.
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 /** \mainpage V8 API Reference Guide
6  *
7  * V8 is Google's open source JavaScript engine.
8  *
9  * This set of documents provides reference material generated from the
10  * V8 header file, include/v8.h.
11  *
12  * For other documentation see http://code.google.com/apis/v8/
13  */
14 
15 #ifndef INCLUDE_V8_H_
16 #define INCLUDE_V8_H_
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <utility>
22 #include <vector>
23 
24 #include "v8-version.h" // NOLINT(build/include)
25 #include "v8config.h" // NOLINT(build/include)
26 
27 // We reserve the V8_* prefix for macros defined in V8 public API and
28 // assume there are no name conflicts with the embedder's code.
29 
30 #ifdef V8_OS_WIN
31 
32 // Setup for Windows DLL export/import. When building the V8 DLL the
33 // BUILDING_V8_SHARED needs to be defined. When building a program which uses
34 // the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
35 // static library or building a program which uses the V8 static library neither
36 // BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
37 #if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
38 #error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the
39  build configuration to ensure that at most one of these is set
40 #endif
41 
42 #ifdef BUILDING_V8_SHARED
43 # define V8_EXPORT __declspec(dllexport)
44 #elif USING_V8_SHARED
45 # define V8_EXPORT __declspec(dllimport)
46 #else
47 # define V8_EXPORT
48 #endif // BUILDING_V8_SHARED
49 
50 #else // V8_OS_WIN
51 
52 // Setup for Linux shared library export.
53 #if V8_HAS_ATTRIBUTE_VISIBILITY && defined(V8_SHARED)
54 # ifdef BUILDING_V8_SHARED
55 # define V8_EXPORT __attribute__ ((visibility("default")))
56 # else
57 # define V8_EXPORT
58 # endif
59 #else
60 # define V8_EXPORT
61 #endif
62 
63 #endif // V8_OS_WIN
64 
65 /**
66  * The v8 JavaScript engine.
67  */
68 namespace v8 {
69 
70 class AccessorSignature;
71 class Array;
72 class Boolean;
73 class BooleanObject;
74 class Context;
75 class CpuProfiler;
76 class Data;
77 class Date;
78 class External;
79 class Function;
80 class FunctionTemplate;
81 class HeapProfiler;
82 class ImplementationUtilities;
83 class Int32;
84 class Integer;
85 class Isolate;
86 template <class T>
87 class Maybe;
88 class Name;
89 class Number;
90 class NumberObject;
91 class Object;
92 class ObjectOperationDescriptor;
93 class ObjectTemplate;
94 class Platform;
95 class Primitive;
96 class Promise;
97 class Proxy;
98 class RawOperationDescriptor;
99 class Script;
100 class SharedArrayBuffer;
101 class Signature;
102 class StartupData;
103 class StackFrame;
104 class StackTrace;
105 class String;
106 class StringObject;
107 class Symbol;
108 class SymbolObject;
109 class Private;
110 class Uint32;
111 class Utils;
112 class Value;
113 template <class T> class Local;
114 template <class T>
115 class MaybeLocal;
116 template <class T> class Eternal;
117 template<class T> class NonCopyablePersistentTraits;
118 template<class T> class PersistentBase;
119 template <class T, class M = NonCopyablePersistentTraits<T> >
120 class Persistent;
121 template <class T>
122 class Global;
123 template<class K, class V, class T> class PersistentValueMap;
124 template <class K, class V, class T>
126 template <class K, class V, class T>
127 class GlobalValueMap;
128 template<class V, class T> class PersistentValueVector;
129 template<class T, class P> class WeakCallbackObject;
130 class FunctionTemplate;
131 class ObjectTemplate;
132 class Data;
133 template<typename T> class FunctionCallbackInfo;
134 template<typename T> class PropertyCallbackInfo;
135 class StackTrace;
136 class StackFrame;
137 class Isolate;
138 class CallHandlerHelper;
140 template<typename T> class ReturnValue;
141 
142 namespace experimental {
143 class FastAccessorBuilder;
144 } // namespace experimental
145 
146 namespace internal {
147 class Arguments;
148 class Heap;
149 class HeapObject;
150 class Isolate;
151 class Object;
152 struct StreamedSource;
153 template<typename T> class CustomArguments;
154 class PropertyCallbackArguments;
155 class FunctionCallbackArguments;
156 class GlobalHandles;
157 } // namespace internal
158 
159 
160 /**
161  * General purpose unique identifier.
162  */
163 class UniqueId {
164  public:
165  explicit UniqueId(intptr_t data)
166  : data_(data) {}
167 
168  bool operator==(const UniqueId& other) const {
169  return data_ == other.data_;
170  }
171 
172  bool operator!=(const UniqueId& other) const {
173  return data_ != other.data_;
174  }
175 
176  bool operator<(const UniqueId& other) const {
177  return data_ < other.data_;
178  }
179 
180  private:
181  intptr_t data_;
182 };
183 
184 // --- Handles ---
185 
186 #define TYPE_CHECK(T, S)
187  while (false) {
188  *(static_cast<T* volatile*>(0)) = static_cast<S*>(0);
189  }
190 
191 
192 /**
193  * An object reference managed by the v8 garbage collector.
194  *
195  * All objects returned from v8 have to be tracked by the garbage
196  * collector so that it knows that the objects are still alive. Also,
197  * because the garbage collector may move objects, it is unsafe to
198  * point directly to an object. Instead, all objects are stored in
199  * handles which are known by the garbage collector and updated
200  * whenever an object moves. Handles should always be passed by value
201  * (except in cases like out-parameters) and they should never be
202  * allocated on the heap.
203  *
204  * There are two types of handles: local and persistent handles.
205  * Local handles are light-weight and transient and typically used in
206  * local operations. They are managed by HandleScopes. Persistent
207  * handles can be used when storing objects across several independent
208  * operations and have to be explicitly deallocated when they're no
209  * longer used.
210  *
211  * It is safe to extract the object stored in the handle by
212  * dereferencing the handle (for instance, to extract the Object* from
213  * a Local<Object>); the value will still be governed by a handle
214  * behind the scenes and the same rules apply to these values as to
215  * their handles.
216  */
217 template <class T>
218 class Local {
219  public:
220  V8_INLINE Local() : val_(0) {}
221  template <class S>
223  : val_(reinterpret_cast<T*>(*that)) {
224  /**
225  * This check fails when trying to convert between incompatible
226  * handles. For example, converting from a Local<String> to a
227  * Local<Number>.
228  */
229  TYPE_CHECK(T, S);
230  }
231 
232  /**
233  * Returns true if the handle is empty.
234  */
235  V8_INLINE bool IsEmpty() const { return val_ == 0; }
236 
237  /**
238  * Sets the handle to be empty. IsEmpty() will then return true.
239  */
240  V8_INLINE void Clear() { val_ = 0; }
241 
242  V8_INLINE T* operator->() const { return val_; }
243 
244  V8_INLINE T* operator*() const { return val_; }
245 
246  /**
247  * Checks whether two handles are the same.
248  * Returns true if both are empty, or if the objects
249  * to which they refer are identical.
250  * The handles' references are not checked.
251  */
252  template <class S>
253  V8_INLINE bool operator==(const Local<S>& that) const {
254  internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
255  internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
256  if (a == 0) return b == 0;
257  if (b == 0) return false;
258  return *a == *b;
259  }
260 
261  template <class S> V8_INLINE bool operator==(
262  const PersistentBase<S>& that) const {
263  internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
264  internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
265  if (a == 0) return b == 0;
266  if (b == 0) return false;
267  return *a == *b;
268  }
269 
270  /**
271  * Checks whether two handles are different.
272  * Returns true if only one of the handles is empty, or if
273  * the objects to which they refer are different.
274  * The handles' references are not checked.
275  */
276  template <class S>
277  V8_INLINE bool operator!=(const Local<S>& that) const {
278  return !operator==(that);
279  }
280 
281  template <class S> V8_INLINE bool operator!=(
282  const Persistent<S>& that) const {
283  return !operator==(that);
284  }
285 
286  template <class S> V8_INLINE static Local<T> Cast(Local<S> that) {
287 #ifdef V8_ENABLE_CHECKS
288  // If we're going to perform the type check then we have to check
289  // that the handle isn't empty before doing the checked cast.
290  if (that.IsEmpty()) return Local<T>();
291 #endif
292  return Local<T>(T::Cast(*that));
293  }
294 
295 
296  template <class S> V8_INLINE Local<S> As() {
297  return Local<S>::Cast(*this);
298  }
299 
300  /**
301  * Create a local handle for the content of another handle.
302  * The referee is kept alive by the local handle even when
303  * the original handle is destroyed/disposed.
304  */
305  V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that);
306  V8_INLINE static Local<T> New(Isolate* isolate,
307  const PersistentBase<T>& that);
308 
309  private:
310  friend class Utils;
311  template<class F> friend class Eternal;
312  template<class F> friend class PersistentBase;
313  template<class F, class M> friend class Persistent;
314  template<class F> friend class Local;
315  template <class F>
316  friend class MaybeLocal;
317  template<class F> friend class FunctionCallbackInfo;
318  template<class F> friend class PropertyCallbackInfo;
319  friend class String;
320  friend class Object;
321  friend class Context;
322  friend class Private;
323  template<class F> friend class internal::CustomArguments;
324  friend Local<Primitive> Undefined(Isolate* isolate);
325  friend Local<Primitive> Null(Isolate* isolate);
326  friend Local<Boolean> True(Isolate* isolate);
327  friend Local<Boolean> False(Isolate* isolate);
328  friend class HandleScope;
329  friend class EscapableHandleScope;
330  template <class F1, class F2, class F3>
332  template<class F1, class F2> friend class PersistentValueVector;
333 
334  explicit V8_INLINE Local(T* that) : val_(that) {}
335  V8_INLINE static Local<T> New(Isolate* isolate, T* that);
336  T* val_;
337 };
338 
339 
340 #if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
341 // Local is an alias for Local for historical reasons.
342 template <class T>
343 using Handle = Local<T>;
344 #endif
345 
346 
347 /**
348  * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
349  * the Local<> is empty before it can be used.
350  *
351  * If an API method returns a MaybeLocal<>, the API method can potentially fail
352  * either because an exception is thrown, or because an exception is pending,
353  * e.g. because a previous API call threw an exception that hasn't been caught
354  * yet, or because a TerminateExecution exception was thrown. In that case, an
355  * empty MaybeLocal is returned.
356  */
357 template <class T>
358 class MaybeLocal {
359  public:
360  V8_INLINE MaybeLocal() : val_(nullptr) {}
361  template <class S>
363  : val_(reinterpret_cast<T*>(*that)) {
364  TYPE_CHECK(T, S);
365  }
366 
367  V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
368 
369  template <class S>
371  out->val_ = IsEmpty() ? nullptr : this->val_;
372  return !IsEmpty();
373  }
374 
375  // Will crash if the MaybeLocal<> is empty.
377 
378  template <class S>
379  V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
380  return IsEmpty() ? default_value : Local<S>(val_);
381  }
382 
383  private:
384  T* val_;
385 };
386 
387 
388 // Eternal handles are set-once handles that live for the life of the isolate.
389 template <class T> class Eternal {
390  public:
391  V8_INLINE Eternal() : index_(kInitialValue) { }
392  template<class S>
393  V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) {
394  Set(isolate, handle);
395  }
396  // Can only be safely called if already set.
397  V8_INLINE Local<T> Get(Isolate* isolate);
398  V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
399  template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
400 
401  private:
402  static const int kInitialValue = -1;
403  int index_;
404 };
405 
406 
407 static const int kInternalFieldsInWeakCallback = 2;
408 
409 
410 template <typename T>
412  public:
413  typedef void (*Callback)(const WeakCallbackInfo<T>& data);
414 
415  WeakCallbackInfo(Isolate* isolate, T* parameter,
416  void* internal_fields[kInternalFieldsInWeakCallback],
417  Callback* callback)
418  : isolate_(isolate), parameter_(parameter), callback_(callback) {
419  for (int i = 0; i < kInternalFieldsInWeakCallback; ++i) {
420  internal_fields_[i] = internal_fields[i];
421  }
422  }
423 
424  V8_INLINE Isolate* GetIsolate() const { return isolate_; }
425  V8_INLINE T* GetParameter() const { return parameter_; }
426  V8_INLINE void* GetInternalField(int index) const;
427 
428  V8_INLINE V8_DEPRECATED("use indexed version",
429  void* GetInternalField1() const) {
430  return internal_fields_[0];
431  }
432  V8_INLINE V8_DEPRECATED("use indexed version",
433  void* GetInternalField2() const) {
434  return internal_fields_[1];
435  }
436 
437  V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.",
438  bool IsFirstPass() const) {
439  return callback_ != nullptr;
440  }
441 
442  // When first called, the embedder MUST Reset() the Global which triggered the
443  // callback. The Global itself is unusable for anything else. No v8 other api
444  // calls may be called in the first callback. Should additional work be
445  // required, the embedder must set a second pass callback, which will be
446  // called after all the initial callbacks are processed.
447  // Calling SetSecondPassCallback on the second pass will immediately crash.
448  void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
449 
450  private:
451  Isolate* isolate_;
452  T* parameter_;
453  Callback* callback_;
454  void* internal_fields_[kInternalFieldsInWeakCallback];
455 };
456 
457 
458 template <class T, class P>
460  public:
461  typedef void (*Callback)(const WeakCallbackData<T, P>& data);
462 
463  WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle)
464  : isolate_(isolate), parameter_(parameter), handle_(handle) {}
465 
466  V8_INLINE Isolate* GetIsolate() const { return isolate_; }
467  V8_INLINE P* GetParameter() const { return parameter_; }
468  V8_INLINE Local<T> GetValue() const { return handle_; }
469 
470  private:
471  Isolate* isolate_;
472  P* parameter_;
473  Local<T> handle_;
474 };
475 
476 
477 // TODO(dcarney): delete this with WeakCallbackData
478 template <class T>
479 using PhantomCallbackData = WeakCallbackInfo<T>;
480 
481 
483 
484 
485 /**
486  * An object reference that is independent of any handle scope. Where
487  * a Local handle only lives as long as the HandleScope in which it was
488  * allocated, a PersistentBase handle remains valid until it is explicitly
489  * disposed.
490  *
491  * A persistent handle contains a reference to a storage cell within
492  * the v8 engine which holds an object value and which is updated by
493  * the garbage collector whenever the object is moved. A new storage
494  * cell can be created using the constructor or PersistentBase::Reset and
495  * existing handles can be disposed using PersistentBase::Reset.
496  *
497  */
498 template <class T> class PersistentBase {
499  public:
500  /**
501  * If non-empty, destroy the underlying storage cell
502  * IsEmpty() will return true after this call.
503  */
504  V8_INLINE void Reset();
505  /**
506  * If non-empty, destroy the underlying storage cell
507  * and create a new one with the contents of other if other is non empty
508  */
509  template <class S>
510  V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
511 
512  /**
513  * If non-empty, destroy the underlying storage cell
514  * and create a new one with the contents of other if other is non empty
515  */
516  template <class S>
517  V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
518 
519  V8_INLINE bool IsEmpty() const { return val_ == NULL; }
520  V8_INLINE void Empty() { val_ = 0; }
521 
522  V8_INLINE Local<T> Get(Isolate* isolate) const {
523  return Local<T>::New(isolate, *this);
524  }
525 
526  template <class S>
527  V8_INLINE bool operator==(const PersistentBase<S>& that) const {
528  internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
529  internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
530  if (a == NULL) return b == NULL;
531  if (b == NULL) return false;
532  return *a == *b;
533  }
534 
535  template <class S>
536  V8_INLINE bool operator==(const Local<S>& that) const {
537  internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
538  internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
539  if (a == NULL) return b == NULL;
540  if (b == NULL) return false;
541  return *a == *b;
542  }
543 
544  template <class S>
545  V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
546  return !operator==(that);
547  }
548 
549  template <class S>
550  V8_INLINE bool operator!=(const Local<S>& that) const {
551  return !operator==(that);
552  }
553 
554  /**
555  * Install a finalization callback on this object.
556  * NOTE: There is no guarantee as to *when* or even *if* the callback is
557  * invoked. The invocation is performed solely on a best effort basis.
558  * As always, GC-based finalization should *not* be relied upon for any
559  * critical form of resource management!
560  */
561  template <typename P>
563  "use WeakCallbackInfo version",
564  void SetWeak(P* parameter,
565  typename WeakCallbackData<T, P>::Callback callback));
566 
567  template <typename S, typename P>
569  "use WeakCallbackInfo version",
570  void SetWeak(P* parameter,
571  typename WeakCallbackData<S, P>::Callback callback));
572 
573  // Phantom persistents work like weak persistents, except that the pointer to
574  // the object being collected is not available in the finalization callback.
575  // This enables the garbage collector to collect the object and any objects
576  // it references transitively in one GC cycle. At the moment you can either
577  // specify a parameter for the callback or the location of two internal
578  // fields in the dying object.
579  template <typename P>
581  "use SetWeak",
582  void SetPhantom(P* parameter,
583  typename WeakCallbackInfo<P>::Callback callback,
584  int internal_field_index1 = -1,
585  int internal_field_index2 = -1));
586 
587  template <typename P>
588  V8_INLINE void SetWeak(P* parameter,
589  typename WeakCallbackInfo<P>::Callback callback,
590  WeakCallbackType type);
591 
592  template<typename P>
594 
595  // TODO(dcarney): remove this.
596  V8_INLINE void ClearWeak() { ClearWeak<void>(); }
597 
598  /**
599  * Allows the embedder to tell the v8 garbage collector that a certain object
600  * is alive. Only allowed when the embedder is asked to trace its heap by
601  * EmbedderHeapTracer.
602  */
604 
605  /**
606  * Marks the reference to this object independent. Garbage collector is free
607  * to ignore any object groups containing this object. Weak callback for an
608  * independent handle should not assume that it will be preceded by a global
609  * GC prologue callback or followed by a global GC epilogue callback.
610  */
611  V8_INLINE void MarkIndependent();
612 
613  /**
614  * Marks the reference to this object partially dependent. Partially dependent
615  * handles only depend on other partially dependent handles and these
616  * dependencies are provided through object groups. It provides a way to build
617  * smaller object groups for young objects that represent only a subset of all
618  * external dependencies. This mark is automatically cleared after each
619  * garbage collection.
620  */
622 
623  /**
624  * Marks the reference to this object as active. The scavenge garbage
625  * collection should not reclaim the objects marked as active.
626  * This bit is cleared after the each garbage collection pass.
627  */
628  V8_INLINE void MarkActive();
629 
630  V8_INLINE bool IsIndependent() const;
631 
632  /** Checks if the handle holds the only reference to an object. */
633  V8_INLINE bool IsNearDeath() const;
634 
635  /** Returns true if the handle's reference is weak. */
636  V8_INLINE bool IsWeak() const;
637 
638  /**
639  * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
640  * description in v8-profiler.h for details.
641  */
642  V8_INLINE void SetWrapperClassId(uint16_t class_id);
643 
644  /**
645  * Returns the class ID previously assigned to this handle or 0 if no class ID
646  * was previously assigned.
647  */
648  V8_INLINE uint16_t WrapperClassId() const;
649 
650  private:
651  friend class Isolate;
652  friend class Utils;
653  template<class F> friend class Local;
654  template<class F1, class F2> friend class Persistent;
655  template <class F>
656  friend class Global;
657  template<class F> friend class PersistentBase;
658  template<class F> friend class ReturnValue;
659  template <class F1, class F2, class F3>
661  template<class F1, class F2> friend class PersistentValueVector;
662  friend class Object;
663 
664  explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
665  PersistentBase(const PersistentBase& other) = delete; // NOLINT
666  void operator=(const PersistentBase&) = delete;
667  V8_INLINE static T* New(Isolate* isolate, T* that);
668 
669  T* val_;
670 };
671 
672 
673 /**
674  * Default traits for Persistent. This class does not allow
675  * use of the copy constructor or assignment operator.
676  * At present kResetInDestructor is not set, but that will change in a future
677  * version.
678  */
679 template<class T>
680 class NonCopyablePersistentTraits {
681  public:
682  typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
683  static const bool kResetInDestructor = false;
684  template<class S, class M>
685  V8_INLINE static void Copy(const Persistent<S, M>& source,
686  NonCopyablePersistent* dest) {
687  Uncompilable<Object>();
688  }
689  // TODO(dcarney): come up with a good compile error here.
690  template<class O> V8_INLINE static void Uncompilable() {
691  TYPE_CHECK(O, Primitive);
692  }
693 };
694 
695 
696 /**
697  * Helper class traits to allow copying and assignment of Persistent.
698  * This will clone the contents of storage cell, but not any of the flags, etc.
699  */
700 template<class T>
703  static const bool kResetInDestructor = true;
704  template<class S, class M>
705  static V8_INLINE void Copy(const Persistent<S, M>& source,
706  CopyablePersistent* dest) {
707  // do nothing, just allow copy
708  }
709 };
710 
711 
712 /**
713  * A PersistentBase which allows copy and assignment.
714  *
715  * Copy, assignment and destructor bevavior is controlled by the traits
716  * class M.
717  *
718  * Note: Persistent class hierarchy is subject to future changes.
719  */
720 template <class T, class M> class Persistent : public PersistentBase<T> {
721  public:
722  /**
723  * A Persistent with no storage cell.
724  */
726  /**
727  * Construct a Persistent from a Local.
728  * When the Local is non-empty, a new storage cell is created
729  * pointing to the same object, and no flags are set.
730  */
731  template <class S>
732  V8_INLINE Persistent(Isolate* isolate, Local<S> that)
733  : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
734  TYPE_CHECK(T, S);
735  }
736  /**
737  * Construct a Persistent from a Persistent.
738  * When the Persistent is non-empty, a new storage cell is created
739  * pointing to the same object, and no flags are set.
740  */
741  template <class S, class M2>
742  V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that)
743  : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
744  TYPE_CHECK(T, S);
745  }
746  /**
747  * The copy constructors and assignment operator create a Persistent
748  * exactly as the Persistent constructor, but the Copy function from the
749  * traits class is called, allowing the setting of flags based on the
750  * copied Persistent.
751  */
753  Copy(that);
754  }
755  template <class S, class M2>
756  V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) {
757  Copy(that);
758  }
759  V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT
760  Copy(that);
761  return *this;
762  }
763  template <class S, class M2>
764  V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT
765  Copy(that);
766  return *this;
767  }
768  /**
769  * The destructor will dispose the Persistent based on the
770  * kResetInDestructor flags in the traits class. Since not calling dispose
771  * can result in a memory leak, it is recommended to always set this flag.
772  */
774  if (M::kResetInDestructor) this->Reset();
775  }
776 
777  // TODO(dcarney): this is pretty useless, fix or remove
778  template <class S>
779  V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT
780 #ifdef V8_ENABLE_CHECKS
781  // If we're going to perform the type check then we have to check
782  // that the handle isn't empty before doing the checked cast.
783  if (!that.IsEmpty()) T::Cast(*that);
784 #endif
785  return reinterpret_cast<Persistent<T>&>(that);
786  }
787 
788  // TODO(dcarney): this is pretty useless, fix or remove
789  template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
790  return Persistent<S>::Cast(*this);
791  }
792 
793  private:
794  friend class Isolate;
795  friend class Utils;
796  template<class F> friend class Local;
797  template<class F1, class F2> friend class Persistent;
798  template<class F> friend class ReturnValue;
799 
800  explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
801  V8_INLINE T* operator*() const { return this->val_; }
802  template<class S, class M2>
803  V8_INLINE void Copy(const Persistent<S, M2>& that);
804 };
805 
806 
807 /**
808  * A PersistentBase which has move semantics.
809  *
810  * Note: Persistent class hierarchy is subject to future changes.
811  */
812 template <class T>
813 class Global : public PersistentBase<T> {
814  public:
815  /**
816  * A Global with no storage cell.
817  */
818  V8_INLINE Global() : PersistentBase<T>(nullptr) {}
819  /**
820  * Construct a Global from a Local.
821  * When the Local is non-empty, a new storage cell is created
822  * pointing to the same object, and no flags are set.
823  */
824  template <class S>
825  V8_INLINE Global(Isolate* isolate, Local<S> that)
826  : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
827  TYPE_CHECK(T, S);
828  }
829  /**
830  * Construct a Global from a PersistentBase.
831  * When the Persistent is non-empty, a new storage cell is created
832  * pointing to the same object, and no flags are set.
833  */
834  template <class S>
835  V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that)
836  : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
837  TYPE_CHECK(T, S);
838  }
839  /**
840  * Move constructor.
841  */
842  V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT
843  other.val_ = nullptr;
844  }
845  V8_INLINE ~Global() { this->Reset(); }
846  /**
847  * Move via assignment.
848  */
849  template <class S>
850  V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT
851  TYPE_CHECK(T, S);
852  if (this != &rhs) {
853  this->Reset();
854  this->val_ = rhs.val_;
855  rhs.val_ = nullptr;
856  }
857  return *this;
858  }
859  /**
860  * Pass allows returning uniques from functions, etc.
861  */
862  Global Pass() { return static_cast<Global&&>(*this); } // NOLINT
863 
864  /*
865  * For compatibility with Chromium's base::Bind (base::Passed).
866  */
867  typedef void MoveOnlyTypeForCPP03;
868 
869  private:
870  template <class F>
871  friend class ReturnValue;
872  Global(const Global&) = delete;
873  void operator=(const Global&) = delete;
874  V8_INLINE T* operator*() const { return this->val_; }
875 };
876 
877 
878 // UniquePersistent is an alias for Global for historical reason.
879 template <class T>
880 using UniquePersistent = Global<T>;
881 
882 
883  /**
884  * A stack-allocated class that governs a number of local handles.
885  * After a handle scope has been created, all local handles will be
886  * allocated within that handle scope until either the handle scope is
887  * deleted or another handle scope is created. If there is already a
888  * handle scope and a new one is created, all allocations will take
889  * place in the new handle scope until it is deleted. After that,
890  * new handles will again be allocated in the original handle scope.
891  *
892  * After the handle scope of a local handle has been deleted the
893  * garbage collector will no longer track the object stored in the
894  * handle and may deallocate it. The behavior of accessing a handle
895  * for which the handle scope has been deleted is undefined.
896  */
898  public:
899  explicit HandleScope(Isolate* isolate);
900 
902 
903  /**
904  * Counts the number of allocated handles.
905  */
906  static int NumberOfHandles(Isolate* isolate);
907 
909  return reinterpret_cast<Isolate*>(isolate_);
910  }
911 
912  protected:
914 
915  void Initialize(Isolate* isolate);
916 
917  static internal::Object** CreateHandle(internal::Isolate* isolate,
918  internal::Object* value);
919 
920  private:
921  // Uses heap_object to obtain the current Isolate.
922  static internal::Object** CreateHandle(internal::HeapObject* heap_object,
923  internal::Object* value);
924 
925  // Make it hard to create heap-allocated or illegal handle scopes by
926  // disallowing certain operations.
927  HandleScope(const HandleScope&);
928  void operator=(const HandleScope&);
929  void* operator new(size_t size);
930  void operator delete(void*, size_t);
931 
932  internal::Isolate* isolate_;
933  internal::Object** prev_next_;
934  internal::Object** prev_limit_;
935 
936  // Local::New uses CreateHandle with an Isolate* parameter.
937  template<class F> friend class Local;
938 
939  // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
940  // a HeapObject* in their shortcuts.
941  friend class Object;
942  friend class Context;
943 };
944 
945 
946 /**
947  * A HandleScope which first allocates a handle in the current scope
948  * which will be later filled with the escape value.
949  */
951  public:
952  explicit EscapableHandleScope(Isolate* isolate);
954 
955  /**
956  * Pushes the value into the previous scope and returns a handle to it.
957  * Cannot be called twice.
958  */
959  template <class T>
960  V8_INLINE Local<T> Escape(Local<T> value) {
961  internal::Object** slot =
962  Escape(reinterpret_cast<internal::Object**>(*value));
963  return Local<T>(reinterpret_cast<T*>(slot));
964  }
965 
966  private:
967  internal::Object** Escape(internal::Object** escape_value);
968 
969  // Make it hard to create heap-allocated or illegal handle scopes by
970  // disallowing certain operations.
971  EscapableHandleScope(const EscapableHandleScope&);
972  void operator=(const EscapableHandleScope&);
973  void* operator new(size_t size);
974  void operator delete(void*, size_t);
975 
976  internal::Object** escape_slot_;
977 };
978 
980  public:
983 
984  private:
985  // Make it hard to create heap-allocated or illegal handle scopes by
986  // disallowing certain operations.
987  SealHandleScope(const SealHandleScope&);
988  void operator=(const SealHandleScope&);
989  void* operator new(size_t size);
990  void operator delete(void*, size_t);
991 
992  internal::Isolate* isolate_;
993  internal::Object** prev_limit_;
994  int prev_sealed_level_;
995 };
996 
997 
998 // --- Special objects ---
999 
1000 
1001 /**
1002  * The superclass of values and API object templates.
1003  */
1005  private:
1006  Data();
1007 };
1008 
1009 
1010 /**
1011  * The optional attributes of ScriptOrigin.
1012  */
1014  public:
1015  V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false,
1016  bool is_shared_cross_origin = false,
1017  bool is_opaque = false)
1018  : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) |
1019  (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
1020  (is_opaque ? kIsOpaque : 0)) {}
1022  : flags_(flags &
1023  (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {}
1024  bool IsEmbedderDebugScript() const {
1025  return (flags_ & kIsEmbedderDebugScript) != 0;
1026  }
1027  bool IsSharedCrossOrigin() const {
1028  return (flags_ & kIsSharedCrossOrigin) != 0;
1029  }
1030  bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
1031  int Flags() const { return flags_; }
1032 
1033  private:
1034  enum {
1035  kIsEmbedderDebugScript = 1,
1036  kIsSharedCrossOrigin = 1 << 1,
1037  kIsOpaque = 1 << 2
1038  };
1039  const int flags_;
1040 };
1041 
1042 /**
1043  * The origin, within a file, of a script.
1044  */
1046  public:
1048  Local<Value> resource_name,
1049  Local<Integer> resource_line_offset = Local<Integer>(),
1050  Local<Integer> resource_column_offset = Local<Integer>(),
1051  Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(),
1052  Local<Integer> script_id = Local<Integer>(),
1053  Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(),
1054  Local<Value> source_map_url = Local<Value>(),
1055  Local<Boolean> resource_is_opaque = Local<Boolean>());
1056  V8_INLINE Local<Value> ResourceName() const;
1059  /**
1060  * Returns true for embedder's debugger scripts
1061  */
1062  V8_INLINE Local<Integer> ScriptID() const;
1063  V8_INLINE Local<Value> SourceMapUrl() const;
1064  V8_INLINE ScriptOriginOptions Options() const { return options_; }
1065 
1066  private:
1067  Local<Value> resource_name_;
1068  Local<Integer> resource_line_offset_;
1069  Local<Integer> resource_column_offset_;
1070  ScriptOriginOptions options_;
1071  Local<Integer> script_id_;
1072  Local<Value> source_map_url_;
1073 };
1074 
1075 
1076 /**
1077  * A compiled JavaScript script, not yet tied to a Context.
1078  */
1080  public:
1081  /**
1082  * Binds the script to the currently entered context.
1083  */
1085 
1086  int GetId();
1088 
1089  /**
1090  * Data read from magic sourceURL comments.
1091  */
1093  /**
1094  * Data read from magic sourceMappingURL comments.
1095  */
1097 
1098  /**
1099  * Returns zero based line number of the code_pos location in the script.
1100  * -1 will be returned if no information available.
1101  */
1102  int GetLineNumber(int code_pos);
1103 
1104  static const int kNoScriptId = 0;
1105 };
1106 
1107 
1108 /**
1109  * A compiled JavaScript script, tied to a Context which was active when the
1110  * script was compiled.
1111  */
1113  public:
1114  /**
1115  * A shorthand for ScriptCompiler::Compile().
1116  */
1118  "Use maybe version",
1119  Local<Script> Compile(Local<String> source,
1120  ScriptOrigin* origin = nullptr));
1122  Local<Context> context, Local<String> source,
1123  ScriptOrigin* origin = nullptr);
1124 
1125  static Local<Script> V8_DEPRECATE_SOON("Use maybe version",
1126  Compile(Local<String> source,
1127  Local<String> file_name));
1128 
1129  /**
1130  * Runs the script returning the resulting value. It will be run in the
1131  * context in which it was created (ScriptCompiler::CompileBound or
1132  * UnboundScript::BindToCurrentContext()).
1133  */
1134  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run());
1136 
1137  /**
1138  * Returns the corresponding context-unbound script.
1139  */
1141 };
1142 
1143 
1144 /**
1145  * For compiling scripts.
1146  */
1148  public:
1149  /**
1150  * Compilation data that the embedder can cache and pass back to speed up
1151  * future compilations. The data is produced if the CompilerOptions passed to
1152  * the compilation functions in ScriptCompiler contains produce_data_to_cache
1153  * = true. The data to cache can then can be retrieved from
1154  * UnboundScript.
1155  */
1159  BufferOwned
1160  };
1161 
1163  : data(NULL),
1164  length(0),
1165  rejected(false),
1167 
1168  // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
1169  // data and guarantees that it stays alive until the CachedData object is
1170  // destroyed. If the policy is BufferOwned, the given data will be deleted
1171  // (with delete[]) when the CachedData object is destroyed.
1172  CachedData(const uint8_t* data, int length,
1173  BufferPolicy buffer_policy = BufferNotOwned);
1175  // TODO(marja): Async compilation; add constructors which take a callback
1176  // which will be called when V8 no longer needs the data.
1177  const uint8_t* data;
1178  int length;
1179  bool rejected;
1181 
1182  private:
1183  // Prevent copying. Not implemented.
1184  CachedData(const CachedData&);
1185  CachedData& operator=(const CachedData&);
1186  };
1187 
1188  /**
1189  * Source code which can be then compiled to a UnboundScript or Script.
1190  */
1191  class Source {
1192  public:
1193  // Source takes ownership of CachedData.
1194  V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
1195  CachedData* cached_data = NULL);
1196  V8_INLINE Source(Local<String> source_string,
1197  CachedData* cached_data = NULL);
1198  V8_INLINE ~Source();
1199 
1200  // Ownership of the CachedData or its buffers is *not* transferred to the
1201  // caller. The CachedData object is alive as long as the Source object is
1202  // alive.
1203  V8_INLINE const CachedData* GetCachedData() const;
1204 
1205  private:
1206  friend class ScriptCompiler;
1207  // Prevent copying. Not implemented.
1208  Source(const Source&);
1209  Source& operator=(const Source&);
1210 
1211  Local<String> source_string;
1212 
1213  // Origin information
1214  Local<Value> resource_name;
1215  Local<Integer> resource_line_offset;
1216  Local<Integer> resource_column_offset;
1217  ScriptOriginOptions resource_options;
1218  Local<Value> source_map_url;
1219 
1220  // Cached data from previous compilation (if a kConsume*Cache flag is
1221  // set), or hold newly generated cache data (kProduce*Cache flags) are
1222  // set when calling a compile method.
1223  CachedData* cached_data;
1224  };
1225 
1226  /**
1227  * For streaming incomplete script data to V8. The embedder should implement a
1228  * subclass of this class.
1229  */
1231  public:
1232  virtual ~ExternalSourceStream() {}
1233 
1234  /**
1235  * V8 calls this to request the next chunk of data from the embedder. This
1236  * function will be called on a background thread, so it's OK to block and
1237  * wait for the data, if the embedder doesn't have data yet. Returns the
1238  * length of the data returned. When the data ends, GetMoreData should
1239  * return 0. Caller takes ownership of the data.
1240  *
1241  * When streaming UTF-8 data, V8 handles multi-byte characters split between
1242  * two data chunks, but doesn't handle multi-byte characters split between
1243  * more than two data chunks. The embedder can avoid this problem by always
1244  * returning at least 2 bytes of data.
1245  *
1246  * If the embedder wants to cancel the streaming, they should make the next
1247  * GetMoreData call return 0. V8 will interpret it as end of data (and most
1248  * probably, parsing will fail). The streaming task will return as soon as
1249  * V8 has parsed the data it received so far.
1250  */
1251  virtual size_t GetMoreData(const uint8_t** src) = 0;
1252 
1253  /**
1254  * V8 calls this method to set a 'bookmark' at the current position in
1255  * the source stream, for the purpose of (maybe) later calling
1256  * ResetToBookmark. If ResetToBookmark is called later, then subsequent
1257  * calls to GetMoreData should return the same data as they did when
1258  * SetBookmark was called earlier.
1259  *
1260  * The embedder may return 'false' to indicate it cannot provide this
1261  * functionality.
1262  */
1263  virtual bool SetBookmark();
1264 
1265  /**
1266  * V8 calls this to return to a previously set bookmark.
1267  */
1268  virtual void ResetToBookmark();
1269  };
1270 
1271 
1272  /**
1273  * Source code which can be streamed into V8 in pieces. It will be parsed
1274  * while streaming. It can be compiled after the streaming is complete.
1275  * StreamedSource must be kept alive while the streaming task is ran (see
1276  * ScriptStreamingTask below).
1277  */
1279  public:
1281 
1282  StreamedSource(ExternalSourceStream* source_stream, Encoding encoding);
1284 
1285  // Ownership of the CachedData or its buffers is *not* transferred to the
1286  // caller. The CachedData object is alive as long as the StreamedSource
1287  // object is alive.
1288  const CachedData* GetCachedData() const;
1289 
1290  internal::StreamedSource* impl() const { return impl_; }
1291 
1292  private:
1293  // Prevent copying. Not implemented.
1294  StreamedSource(const StreamedSource&);
1295  StreamedSource& operator=(const StreamedSource&);
1296 
1297  internal::StreamedSource* impl_;
1298  };
1299 
1300  /**
1301  * A streaming task which the embedder must run on a background thread to
1302  * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript.
1303  */
1305  public:
1306  virtual ~ScriptStreamingTask() {}
1307  virtual void Run() = 0;
1308  };
1309 
1316  };
1317 
1318  /**
1319  * Compiles the specified script (context-independent).
1320  * Cached data as part of the source object can be optionally produced to be
1321  * consumed later to speed up compilation of identical source scripts.
1322  *
1323  * Note that when producing cached data, the source must point to NULL for
1324  * cached data. When consuming cached data, the cached data must have been
1325  * produced by the same version of V8.
1326  *
1327  * \param source Script source code.
1328  * \return Compiled script object (context independent; for running it must be
1329  * bound to a context).
1330  */
1331  static V8_DEPRECATED("Use maybe version",
1332  Local<UnboundScript> CompileUnbound(
1333  Isolate* isolate, Source* source,
1334  CompileOptions options = kNoCompileOptions));
1336  Isolate* isolate, Source* source,
1337  CompileOptions options = kNoCompileOptions);
1338 
1339  /**
1340  * Compiles the specified script (bound to current context).
1341  *
1342  * \param source Script source code.
1343  * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1344  * using pre_data speeds compilation if it's done multiple times.
1345  * Owned by caller, no references are kept when this function returns.
1346  * \return Compiled script object, bound to the context that was active
1347  * when this function was called. When run it will always use this
1348  * context.
1349  */
1351  "Use maybe version",
1352  Local<Script> Compile(Isolate* isolate, Source* source,
1353  CompileOptions options = kNoCompileOptions));
1355  Local<Context> context, Source* source,
1356  CompileOptions options = kNoCompileOptions);
1357 
1358  /**
1359  * Returns a task which streams script data into V8, or NULL if the script
1360  * cannot be streamed. The user is responsible for running the task on a
1361  * background thread and deleting it. When ran, the task starts parsing the
1362  * script, and it will request data from the StreamedSource as needed. When
1363  * ScriptStreamingTask::Run exits, all data has been streamed and the script
1364  * can be compiled (see Compile below).
1365  *
1366  * This API allows to start the streaming with as little data as possible, and
1367  * the remaining data (for example, the ScriptOrigin) is passed to Compile.
1368  */
1370  Isolate* isolate, StreamedSource* source,
1371  CompileOptions options = kNoCompileOptions);
1372 
1373  /**
1374  * Compiles a streamed script (bound to current context).
1375  *
1376  * This can only be called after the streaming has finished
1377  * (ScriptStreamingTask has been run). V8 doesn't construct the source string
1378  * during streaming, so the embedder needs to pass the full source here.
1379  */
1380  static V8_DEPRECATED("Use maybe version",
1381  Local<Script> Compile(Isolate* isolate,
1382  StreamedSource* source,
1383  Local<String> full_source_string,
1384  const ScriptOrigin& origin));
1386  Local<Context> context, StreamedSource* source,
1387  Local<String> full_source_string, const ScriptOrigin& origin);
1388 
1389  /**
1390  * Return a version tag for CachedData for the current V8 version & flags.
1391  *
1392  * This value is meant only for determining whether a previously generated
1393  * CachedData instance is still valid; the tag has no other meaing.
1394  *
1395  * Background: The data carried by CachedData may depend on the exact
1396  * V8 version number or currently compiler flags. This means when
1397  * persisting CachedData, the embedder must take care to not pass in
1398  * data from another V8 version, or the same version with different
1399  * features enabled.
1400  *
1401  * The easiest way to do so is to clear the embedder's cache on any
1402  * such change.
1403  *
1404  * Alternatively, this tag can be stored alongside the cached data and
1405  * compared when it is being used.
1406  */
1407  static uint32_t CachedDataVersionTag();
1408 
1409  /**
1410  * Compile an ES6 module.
1411  *
1412  * This is an unfinished experimental feature, and is only exposed
1413  * here for internal testing purposes.
1414  * Only parsing works at the moment. Do not use.
1415  *
1416  * TODO(adamk): Script is likely the wrong return value for this;
1417  * should return some new Module type.
1418  */
1420  Local<Context> context, Source* source,
1421  CompileOptions options = kNoCompileOptions);
1422 
1423  /**
1424  * Compile a function for a given context. This is equivalent to running
1425  *
1426  * with (obj) {
1427  * return function(args) { ... }
1428  * }
1429  *
1430  * It is possible to specify multiple context extensions (obj in the above
1431  * example).
1432  */
1433  static V8_DEPRECATE_SOON("Use maybe version",
1434  Local<Function> CompileFunctionInContext(
1435  Isolate* isolate, Source* source,
1436  Local<Context> context, size_t arguments_count,
1437  Local<String> arguments[],
1438  size_t context_extension_count,
1439  Local<Object> context_extensions[]));
1441  Local<Context> context, Source* source, size_t arguments_count,
1442  Local<String> arguments[], size_t context_extension_count,
1443  Local<Object> context_extensions[]);
1444 
1445  private:
1446  static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
1447  Isolate* isolate, Source* source, CompileOptions options, bool is_module);
1448 };
1449 
1450 
1451 /**
1452  * An error message.
1453  */
1455  public:
1456  Local<String> Get() const;
1457 
1458  V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const);
1460  Local<Context> context) const;
1461 
1462  /**
1463  * Returns the origin for the script from where the function causing the
1464  * error originates.
1465  */
1467 
1468  /**
1469  * Returns the resource name for the script from where the function causing
1470  * the error originates.
1471  */
1473 
1474  /**
1475  * Exception stack trace. By default stack traces are not captured for
1476  * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1477  * to change this option.
1478  */
1480 
1481  /**
1482  * Returns the number, 1-based, of the line where the error occurred.
1483  */
1484  V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const);
1486 
1487  /**
1488  * Returns the index within the script of the first character where
1489  * the error occurred.
1490  */
1491  int GetStartPosition() const;
1492 
1493  /**
1494  * Returns the index within the script of the last character where
1495  * the error occurred.
1496  */
1497  int GetEndPosition() const;
1498 
1499  /**
1500  * Returns the index within the line of the first character where
1501  * the error occurred.
1502  */
1503  V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const);
1505 
1506  /**
1507  * Returns the index within the line of the last character where
1508  * the error occurred.
1509  */
1510  V8_DEPRECATED("Use maybe version", int GetEndColumn() const);
1512 
1513  /**
1514  * Passes on the value set by the embedder when it fed the script from which
1515  * this Message was generated to V8.
1516  */
1517  bool IsSharedCrossOrigin() const;
1518  bool IsOpaque() const;
1519 
1520  // TODO(1245381): Print to a string instead of on a FILE.
1521  static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
1522 
1523  static const int kNoLineNumberInfo = 0;
1524  static const int kNoColumnInfo = 0;
1525  static const int kNoScriptIdInfo = 0;
1526 };
1527 
1528 
1529 /**
1530  * Representation of a JavaScript stack trace. The information collected is a
1531  * snapshot of the execution stack and the information remains valid after
1532  * execution continues.
1533  */
1535  public:
1536  /**
1537  * Flags that determine what information is placed captured for each
1538  * StackFrame when grabbing the current stack trace.
1539  */
1543  kScriptName = 1 << 2,
1544  kFunctionName = 1 << 3,
1545  kIsEval = 1 << 4,
1546  kIsConstructor = 1 << 5,
1548  kScriptId = 1 << 7,
1552  };
1553 
1554  /**
1555  * Returns a StackFrame at a particular index.
1556  */
1557  Local<StackFrame> GetFrame(uint32_t index) const;
1558 
1559  /**
1560  * Returns the number of StackFrames.
1561  */
1562  int GetFrameCount() const;
1563 
1564  /**
1565  * Returns StackTrace as a v8::Array that contains StackFrame objects.
1566  */
1568 
1569  /**
1570  * Grab a snapshot of the current JavaScript execution stack.
1571  *
1572  * \param frame_limit The maximum number of stack frames we want to capture.
1573  * \param options Enumerates the set of things we will capture for each
1574  * StackFrame.
1575  */
1577  Isolate* isolate,
1578  int frame_limit,
1579  StackTraceOptions options = kOverview);
1580 };
1581 
1582 
1583 /**
1584  * A single JavaScript stack frame.
1585  */
1587  public:
1588  /**
1589  * Returns the number, 1-based, of the line for the associate function call.
1590  * This method will return Message::kNoLineNumberInfo if it is unable to
1591  * retrieve the line number, or if kLineNumber was not passed as an option
1592  * when capturing the StackTrace.
1593  */
1594  int GetLineNumber() const;
1595 
1596  /**
1597  * Returns the 1-based column offset on the line for the associated function
1598  * call.
1599  * This method will return Message::kNoColumnInfo if it is unable to retrieve
1600  * the column number, or if kColumnOffset was not passed as an option when
1601  * capturing the StackTrace.
1602  */
1603  int GetColumn() const;
1604 
1605  /**
1606  * Returns the id of the script for the function for this StackFrame.
1607  * This method will return Message::kNoScriptIdInfo if it is unable to
1608  * retrieve the script id, or if kScriptId was not passed as an option when
1609  * capturing the StackTrace.
1610  */
1611  int GetScriptId() const;
1612 
1613  /**
1614  * Returns the name of the resource that contains the script for the
1615  * function for this StackFrame.
1616  */
1618 
1619  /**
1620  * Returns the name of the resource that contains the script for the
1621  * function for this StackFrame or sourceURL value if the script name
1622  * is undefined and its source ends with //# sourceURL=... string or
1623  * deprecated //@ sourceURL=... string.
1624  */
1626 
1627  /**
1628  * Returns the name of the function associated with this stack frame.
1629  */
1631 
1632  /**
1633  * Returns whether or not the associated function is compiled via a call to
1634  * eval().
1635  */
1636  bool IsEval() const;
1637 
1638  /**
1639  * Returns whether or not the associated function is called as a
1640  * constructor via "new".
1641  */
1642  bool IsConstructor() const;
1643 };
1644 
1645 
1646 // A StateTag represents a possible state of the VM.
1648 
1649 
1650 // A RegisterState represents the current state of registers used
1651 // by the sampling profiler API.
1653  RegisterState() : pc(NULL), sp(NULL), fp(NULL) {}
1654  void* pc; // Instruction pointer.
1655  void* sp; // Stack pointer.
1656  void* fp; // Frame pointer.
1657 };
1658 
1659 
1660 // The output structure filled up by GetStackSample API function.
1661 struct SampleInfo {
1664 };
1665 
1666 
1667 /**
1668  * A JSON Parser.
1669  */
1671  public:
1672  /**
1673  * Tries to parse the string |json_string| and returns it as value if
1674  * successful.
1675  *
1676  * \param json_string The string to parse.
1677  * \return The corresponding value if successfully parsed.
1678  */
1679  static V8_DEPRECATED("Use maybe version",
1680  Local<Value> Parse(Local<String> json_string));
1682  Isolate* isolate, Local<String> json_string);
1683 };
1684 
1685 
1686 /**
1687  * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap
1688  * but can be created without entering a v8::Context and hence shouldn't
1689  * escape to JavaScript.
1690  */
1691 class V8_EXPORT NativeWeakMap : public Data {
1692  public:
1693  static Local<NativeWeakMap> New(Isolate* isolate);
1694  void Set(Local<Value> key, Local<Value> value);
1696  bool Has(Local<Value> key);
1697  bool Delete(Local<Value> key);
1698 };
1699 
1700 
1701 // --- Value ---
1702 
1703 
1704 /**
1705  * The superclass of all JavaScript values and objects.
1706  */
1707 class V8_EXPORT Value : public Data {
1708  public:
1709  /**
1710  * Returns true if this value is the undefined value. See ECMA-262
1711  * 4.3.10.
1712  */
1713  V8_INLINE bool IsUndefined() const;
1714 
1715  /**
1716  * Returns true if this value is the null value. See ECMA-262
1717  * 4.3.11.
1718  */
1719  V8_INLINE bool IsNull() const;
1720 
1721  /**
1722  * Returns true if this value is true.
1723  */
1724  bool IsTrue() const;
1725 
1726  /**
1727  * Returns true if this value is false.
1728  */
1729  bool IsFalse() const;
1730 
1731  /**
1732  * Returns true if this value is a symbol or a string.
1733  * This is an experimental feature.
1734  */
1735  bool IsName() const;
1736 
1737  /**
1738  * Returns true if this value is an instance of the String type.
1739  * See ECMA-262 8.4.
1740  */
1741  V8_INLINE bool IsString() const;
1742 
1743  /**
1744  * Returns true if this value is a symbol.
1745  * This is an experimental feature.
1746  */
1747  bool IsSymbol() const;
1748 
1749  /**
1750  * Returns true if this value is a function.
1751  */
1752  bool IsFunction() const;
1753 
1754  /**
1755  * Returns true if this value is an array. Note that it will return false for
1756  * an Proxy for an array.
1757  */
1758  bool IsArray() const;
1759 
1760  /**
1761  * Returns true if this value is an object.
1762  */
1763  bool IsObject() const;
1764 
1765  /**
1766  * Returns true if this value is boolean.
1767  */
1768  bool IsBoolean() const;
1769 
1770  /**
1771  * Returns true if this value is a number.
1772  */
1773  bool IsNumber() const;
1774 
1775  /**
1776  * Returns true if this value is external.
1777  */
1778  bool IsExternal() const;
1779 
1780  /**
1781  * Returns true if this value is a 32-bit signed integer.
1782  */
1783  bool IsInt32() const;
1784 
1785  /**
1786  * Returns true if this value is a 32-bit unsigned integer.
1787  */
1788  bool IsUint32() const;
1789 
1790  /**
1791  * Returns true if this value is a Date.
1792  */
1793  bool IsDate() const;
1794 
1795  /**
1796  * Returns true if this value is an Arguments object.
1797  */
1798  bool IsArgumentsObject() const;
1799 
1800  /**
1801  * Returns true if this value is a Boolean object.
1802  */
1803  bool IsBooleanObject() const;
1804 
1805  /**
1806  * Returns true if this value is a Number object.
1807  */
1808  bool IsNumberObject() const;
1809 
1810  /**
1811  * Returns true if this value is a String object.
1812  */
1813  bool IsStringObject() const;
1814 
1815  /**
1816  * Returns true if this value is a Symbol object.
1817  * This is an experimental feature.
1818  */
1819  bool IsSymbolObject() const;
1820 
1821  /**
1822  * Returns true if this value is a NativeError.
1823  */
1824  bool IsNativeError() const;
1825 
1826  /**
1827  * Returns true if this value is a RegExp.
1828  */
1829  bool IsRegExp() const;
1830 
1831  /**
1832  * Returns true if this value is a Generator function.
1833  * This is an experimental feature.
1834  */
1835  bool IsGeneratorFunction() const;
1836 
1837  /**
1838  * Returns true if this value is a Generator object (iterator).
1839  * This is an experimental feature.
1840  */
1841  bool IsGeneratorObject() const;
1842 
1843  /**
1844  * Returns true if this value is a Promise.
1845  * This is an experimental feature.
1846  */
1847  bool IsPromise() const;
1848 
1849  /**
1850  * Returns true if this value is a Map.
1851  */
1852  bool IsMap() const;
1853 
1854  /**
1855  * Returns true if this value is a Set.
1856  */
1857  bool IsSet() const;
1858 
1859  /**
1860  * Returns true if this value is a Map Iterator.
1861  */
1862  bool IsMapIterator() const;
1863 
1864  /**
1865  * Returns true if this value is a Set Iterator.
1866  */
1867  bool IsSetIterator() const;
1868 
1869  /**
1870  * Returns true if this value is a WeakMap.
1871  */
1872  bool IsWeakMap() const;
1873 
1874  /**
1875  * Returns true if this value is a WeakSet.
1876  */
1877  bool IsWeakSet() const;
1878 
1879  /**
1880  * Returns true if this value is an ArrayBuffer.
1881  * This is an experimental feature.
1882  */
1883  bool IsArrayBuffer() const;
1884 
1885  /**
1886  * Returns true if this value is an ArrayBufferView.
1887  * This is an experimental feature.
1888  */
1889  bool IsArrayBufferView() const;
1890 
1891  /**
1892  * Returns true if this value is one of TypedArrays.
1893  * This is an experimental feature.
1894  */
1895  bool IsTypedArray() const;
1896 
1897  /**
1898  * Returns true if this value is an Uint8Array.
1899  * This is an experimental feature.
1900  */
1901  bool IsUint8Array() const;
1902 
1903  /**
1904  * Returns true if this value is an Uint8ClampedArray.
1905  * This is an experimental feature.
1906  */
1907  bool IsUint8ClampedArray() const;
1908 
1909  /**
1910  * Returns true if this value is an Int8Array.
1911  * This is an experimental feature.
1912  */
1913  bool IsInt8Array() const;
1914 
1915  /**
1916  * Returns true if this value is an Uint16Array.
1917  * This is an experimental feature.
1918  */
1919  bool IsUint16Array() const;
1920 
1921  /**
1922  * Returns true if this value is an Int16Array.
1923  * This is an experimental feature.
1924  */
1925  bool IsInt16Array() const;
1926 
1927  /**
1928  * Returns true if this value is an Uint32Array.
1929  * This is an experimental feature.
1930  */
1931  bool IsUint32Array() const;
1932 
1933  /**
1934  * Returns true if this value is an Int32Array.
1935  * This is an experimental feature.
1936  */
1937  bool IsInt32Array() const;
1938 
1939  /**
1940  * Returns true if this value is a Float32Array.
1941  * This is an experimental feature.
1942  */
1943  bool IsFloat32Array() const;
1944 
1945  /**
1946  * Returns true if this value is a Float64Array.
1947  * This is an experimental feature.
1948  */
1949  bool IsFloat64Array() const;
1950 
1951  /**
1952  * Returns true if this value is a SIMD Float32x4.
1953  * This is an experimental feature.
1954  */
1955  bool IsFloat32x4() const;
1956 
1957  /**
1958  * Returns true if this value is a DataView.
1959  * This is an experimental feature.
1960  */
1961  bool IsDataView() const;
1962 
1963  /**
1964  * Returns true if this value is a SharedArrayBuffer.
1965  * This is an experimental feature.
1966  */
1967  bool IsSharedArrayBuffer() const;
1968 
1969  /**
1970  * Returns true if this value is a JavaScript Proxy.
1971  */
1972  bool IsProxy() const;
1973 
1974 
1976  Local<Context> context) const;
1978  Local<Context> context) const;
1980  Local<Context> context) const;
1982  Local<Context> context) const;
1984  Local<Context> context) const;
1986  Local<Context> context) const;
1988  Local<Context> context) const;
1990 
1991  V8_DEPRECATE_SOON("Use maybe version",
1992  Local<Boolean> ToBoolean(Isolate* isolate) const);
1993  V8_DEPRECATE_SOON("Use maybe version",
1994  Local<Number> ToNumber(Isolate* isolate) const);
1995  V8_DEPRECATE_SOON("Use maybe version",
1996  Local<String> ToString(Isolate* isolate) const);
1997  V8_DEPRECATED("Use maybe version",
1998  Local<String> ToDetailString(Isolate* isolate) const);
1999  V8_DEPRECATE_SOON("Use maybe version",
2000  Local<Object> ToObject(Isolate* isolate) const);
2001  V8_DEPRECATE_SOON("Use maybe version",
2002  Local<Integer> ToInteger(Isolate* isolate) const);
2003  V8_DEPRECATED("Use maybe version",
2004  Local<Uint32> ToUint32(Isolate* isolate) const);
2005  V8_DEPRECATE_SOON("Use maybe version",
2006  Local<Int32> ToInt32(Isolate* isolate) const);
2007 
2008  inline V8_DEPRECATE_SOON("Use maybe version",
2009  Local<Boolean> ToBoolean() const);
2010  inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const);
2011  inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const);
2012  inline V8_DEPRECATED("Use maybe version",
2013  Local<String> ToDetailString() const);
2014  inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const);
2015  inline V8_DEPRECATE_SOON("Use maybe version",
2016  Local<Integer> ToInteger() const);
2017  inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const);
2018  inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const);
2019 
2020  /**
2021  * Attempts to convert a string to an array index.
2022  * Returns an empty handle if the conversion fails.
2023  */
2024  V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const);
2026  Local<Context> context) const;
2027 
2031  Local<Context> context) const;
2033  Local<Context> context) const;
2035 
2036  V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const);
2037  V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const);
2038  V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const);
2039  V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const);
2040  V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const);
2041 
2042  /** JS == */
2043  V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const);
2045  Local<Value> that) const;
2046  bool StrictEquals(Local<Value> that) const;
2047  bool SameValue(Local<Value> that) const;
2048 
2049  template <class T> V8_INLINE static Value* Cast(T* value);
2050 
2051  private:
2052  V8_INLINE bool QuickIsUndefined() const;
2053  V8_INLINE bool QuickIsNull() const;
2054  V8_INLINE bool QuickIsString() const;
2055  bool FullIsUndefined() const;
2056  bool FullIsNull() const;
2057  bool FullIsString() const;
2058 };
2059 
2060 
2061 /**
2062  * The superclass of primitive values. See ECMA-262 4.3.2.
2063  */
2064 class V8_EXPORT Primitive : public Value { };
2065 
2066 
2067 /**
2068  * A primitive boolean value (ECMA-262, 4.3.14). Either the true
2069  * or false value.
2070  */
2071 class V8_EXPORT Boolean : public Primitive {
2072  public:
2073  bool Value() const;
2074  V8_INLINE static Boolean* Cast(v8::Value* obj);
2075  V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value);
2076 
2077  private:
2078  static void CheckCast(v8::Value* obj);
2079 };
2080 
2081 
2082 /**
2083  * A superclass for symbols and strings.
2084  */
2085 class V8_EXPORT Name : public Primitive {
2086  public:
2087  /**
2088  * Returns the identity hash for this object. The current implementation
2089  * uses an inline property on the object to store the identity hash.
2090  *
2091  * The return value will never be 0. Also, it is not guaranteed to be
2092  * unique.
2093  */
2095 
2096  V8_INLINE static Name* Cast(v8::Value* obj);
2097  private:
2098  static void CheckCast(v8::Value* obj);
2099 };
2100 
2101 
2103 
2104 
2105 /**
2106  * A JavaScript string value (ECMA-262, 4.3.17).
2107  */
2108 class V8_EXPORT String : public Name {
2109  public:
2110  static const int kMaxLength = (1 << 28) - 16;
2111 
2112  enum Encoding {
2115  ONE_BYTE_ENCODING = 0x4
2116  };
2117  /**
2118  * Returns the number of characters in this string.
2119  */
2120  int Length() const;
2121 
2122  /**
2123  * Returns the number of bytes in the UTF-8 encoded
2124  * representation of this string.
2125  */
2126  int Utf8Length() const;
2127 
2128  /**
2129  * Returns whether this string is known to contain only one byte data.
2130  * Does not read the string.
2131  * False negatives are possible.
2132  */
2133  bool IsOneByte() const;
2134 
2135  /**
2136  * Returns whether this string contain only one byte data.
2137  * Will read the entire string in some cases.
2138  */
2139  bool ContainsOnlyOneByte() const;
2140 
2141  /**
2142  * Write the contents of the string to an external buffer.
2143  * If no arguments are given, expects the buffer to be large
2144  * enough to hold the entire string and NULL terminator. Copies
2145  * the contents of the string and the NULL terminator into the
2146  * buffer.
2147  *
2148  * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
2149  * before the end of the buffer.
2150  *
2151  * Copies up to length characters into the output buffer.
2152  * Only null-terminates if there is enough space in the buffer.
2153  *
2154  * \param buffer The buffer into which the string will be copied.
2155  * \param start The starting position within the string at which
2156  * copying begins.
2157  * \param length The number of characters to copy from the string. For
2158  * WriteUtf8 the number of bytes in the buffer.
2159  * \param nchars_ref The number of characters written, can be NULL.
2160  * \param options Various options that might affect performance of this or
2161  * subsequent operations.
2162  * \return The number of characters copied to the buffer excluding the null
2163  * terminator. For WriteUtf8: The number of bytes copied to the buffer
2164  * including the null terminator (if written).
2165  */
2171  // Used by WriteUtf8 to replace orphan surrogate code units with the
2172  // unicode replacement character. Needs to be set to guarantee valid UTF-8
2173  // output.
2175  };
2176 
2177  // 16-bit character codes.
2178  int Write(uint16_t* buffer,
2179  int start = 0,
2180  int length = -1,
2181  int options = NO_OPTIONS) const;
2182  // One byte characters.
2183  int WriteOneByte(uint8_t* buffer,
2184  int start = 0,
2185  int length = -1,
2186  int options = NO_OPTIONS) const;
2187  // UTF-8 encoded characters.
2188  int WriteUtf8(char* buffer,
2189  int length = -1,
2190  int* nchars_ref = NULL,
2191  int options = NO_OPTIONS) const;
2192 
2193  /**
2194  * A zero length string.
2195  */
2196  V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate);
2197 
2198  /**
2199  * Returns true if the string is external
2200  */
2201  bool IsExternal() const;
2202 
2203  /**
2204  * Returns true if the string is both external and one-byte.
2205  */
2206  bool IsExternalOneByte() const;
2207 
2209  public:
2211 
2212  virtual bool IsCompressible() const { return false; }
2213 
2214  protected:
2216 
2217  /**
2218  * Internally V8 will call this Dispose method when the external string
2219  * resource is no longer needed. The default implementation will use the
2220  * delete operator. This method can be overridden in subclasses to
2221  * control how allocated external string resources are disposed.
2222  */
2223  virtual void Dispose() { delete this; }
2224 
2225  private:
2226  // Disallow copying and assigning.
2227  ExternalStringResourceBase(const ExternalStringResourceBase&);
2228  void operator=(const ExternalStringResourceBase&);
2229 
2230  friend class v8::internal::Heap;
2231  };
2232 
2233  /**
2234  * An ExternalStringResource is a wrapper around a two-byte string
2235  * buffer that resides outside V8's heap. Implement an
2236  * ExternalStringResource to manage the life cycle of the underlying
2237  * buffer. Note that the string data must be immutable.
2238  */
2240  : public ExternalStringResourceBase {
2241  public:
2242  /**
2243  * Override the destructor to manage the life cycle of the underlying
2244  * buffer.
2245  */
2247 
2248  /**
2249  * The string data from the underlying buffer.
2250  */
2251  virtual const uint16_t* data() const = 0;
2252 
2253  /**
2254  * The length of the string. That is, the number of two-byte characters.
2255  */
2256  virtual size_t length() const = 0;
2257 
2258  protected:
2260  };
2261 
2262  /**
2263  * An ExternalOneByteStringResource is a wrapper around an one-byte
2264  * string buffer that resides outside V8's heap. Implement an
2265  * ExternalOneByteStringResource to manage the life cycle of the
2266  * underlying buffer. Note that the string data must be immutable
2267  * and that the data must be Latin-1 and not UTF-8, which would require
2268  * special treatment internally in the engine and do not allow efficient
2269  * indexing. Use String::New or convert to 16 bit data for non-Latin1.
2270  */
2271 
2273  : public ExternalStringResourceBase {
2274  public:
2275  /**
2276  * Override the destructor to manage the life cycle of the underlying
2277  * buffer.
2278  */
2280  /** The string data from the underlying buffer.*/
2281  virtual const char* data() const = 0;
2282  /** The number of Latin-1 characters in the string.*/
2283  virtual size_t length() const = 0;
2284  protected:
2286  };
2287 
2288  /**
2289  * If the string is an external string, return the ExternalStringResourceBase
2290  * regardless of the encoding, otherwise return NULL. The encoding of the
2291  * string is returned in encoding_out.
2292  */
2294  Encoding* encoding_out) const;
2295 
2296  /**
2297  * Get the ExternalStringResource for an external string. Returns
2298  * NULL if IsExternal() doesn't return true.
2299  */
2301 
2302  /**
2303  * Get the ExternalOneByteStringResource for an external one-byte string.
2304  * Returns NULL if IsExternalOneByte() doesn't return true.
2305  */
2307 
2308  V8_INLINE static String* Cast(v8::Value* obj);
2309 
2310  // TODO(dcarney): remove with deprecation of New functions.
2314  };
2315 
2316  /** Allocates a new string from UTF-8 data.*/
2318  "Use maybe version",
2319  Local<String> NewFromUtf8(Isolate* isolate, const char* data,
2321  int length = -1));
2322 
2323  /** Allocates a new string from UTF-8 data. Only returns an empty value when
2324  * length > kMaxLength. **/
2326  Isolate* isolate, const char* data, v8::NewStringType type,
2327  int length = -1);
2328 
2329  /** Allocates a new string from Latin-1 data.*/
2331  "Use maybe version",
2332  Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data,
2334  int length = -1));
2335 
2336  /** Allocates a new string from Latin-1 data. Only returns an empty value
2337  * when length > kMaxLength. **/
2339  Isolate* isolate, const uint8_t* data, v8::NewStringType type,
2340  int length = -1);
2341 
2342  /** Allocates a new string from UTF-16 data.*/
2344  "Use maybe version",
2345  Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data,
2347  int length = -1));
2348 
2349  /** Allocates a new string from UTF-16 data. Only returns an empty value when
2350  * length > kMaxLength. **/
2352  Isolate* isolate, const uint16_t* data, v8::NewStringType type,
2353  int length = -1);
2354 
2355  /**
2356  * Creates a new string by concatenating the left and the right strings
2357  * passed in as parameters.
2358  */
2359  static Local<String> Concat(Local<String> left, Local<String> right);
2360 
2361  /**
2362  * Creates a new external string using the data defined in the given
2363  * resource. When the external string is no longer live on V8's heap the
2364  * resource will be disposed by calling its Dispose method. The caller of
2365  * this function should not otherwise delete or modify the resource. Neither
2366  * should the underlying buffer be deallocated or modified except through the
2367  * destructor of the external string resource.
2368  */
2369  static V8_DEPRECATED("Use maybe version",
2370  Local<String> NewExternal(
2371  Isolate* isolate, ExternalStringResource* resource));
2373  Isolate* isolate, ExternalStringResource* resource);
2374 
2375  /**
2376  * Associate an external string resource with this string by transforming it
2377  * in place so that existing references to this string in the JavaScript heap
2378  * will use the external string resource. The external string resource's
2379  * character contents need to be equivalent to this string.
2380  * Returns true if the string has been changed to be an external string.
2381  * The string is not modified if the operation fails. See NewExternal for
2382  * information on the lifetime of the resource.
2383  */
2385 
2386  /**
2387  * Creates a new external string using the one-byte data defined in the given
2388  * resource. When the external string is no longer live on V8's heap the
2389  * resource will be disposed by calling its Dispose method. The caller of
2390  * this function should not otherwise delete or modify the resource. Neither
2391  * should the underlying buffer be deallocated or modified except through the
2392  * destructor of the external string resource.
2393  */
2395  "Use maybe version",
2396  Local<String> NewExternal(Isolate* isolate,
2397  ExternalOneByteStringResource* resource));
2399  Isolate* isolate, ExternalOneByteStringResource* resource);
2400 
2401  /**
2402  * Associate an external string resource with this string by transforming it
2403  * in place so that existing references to this string in the JavaScript heap
2404  * will use the external string resource. The external string resource's
2405  * character contents need to be equivalent to this string.
2406  * Returns true if the string has been changed to be an external string.
2407  * The string is not modified if the operation fails. See NewExternal for
2408  * information on the lifetime of the resource.
2409  */
2411 
2412  /**
2413  * Returns true if this string can be made external.
2414  */
2416 
2417  /**
2418  * Converts an object to a UTF-8-encoded character array. Useful if
2419  * you want to print the object. If conversion to a string fails
2420  * (e.g. due to an exception in the toString() method of the object)
2421  * then the length() method returns 0 and the * operator returns
2422  * NULL.
2423  */
2425  public:
2426  explicit Utf8Value(Local<v8::Value> obj);
2428  char* operator*() { return str_; }
2429  const char* operator*() const { return str_; }
2430  int length() const { return length_; }
2431  private:
2432  char* str_;
2433  int length_;
2434 
2435  // Disallow copying and assigning.
2436  Utf8Value(const Utf8Value&);
2437  void operator=(const Utf8Value&);
2438  };
2439 
2440  /**
2441  * Converts an object to a two-byte string.
2442  * If conversion to a string fails (eg. due to an exception in the toString()
2443  * method of the object) then the length() method returns 0 and the * operator
2444  * returns NULL.
2445  */
2447  public:
2448  explicit Value(Local<v8::Value> obj);
2449  ~Value();
2450  uint16_t* operator*() { return str_; }
2451  const uint16_t* operator*() const { return str_; }
2452  int length() const { return length_; }
2453  private:
2454  uint16_t* str_;
2455  int length_;
2456 
2457  // Disallow copying and assigning.
2458  Value(const Value&);
2459  void operator=(const Value&);
2460  };
2461 
2462  private:
2463  void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
2464  Encoding encoding) const;
2465  void VerifyExternalStringResource(ExternalStringResource* val) const;
2466  static void CheckCast(v8::Value* obj);
2467 };
2468 
2469 
2470 /**
2471  * A JavaScript symbol (ECMA-262 edition 6)
2472  *
2473  * This is an experimental feature. Use at your own risk.
2474  */
2475 class V8_EXPORT Symbol : public Name {
2476  public:
2477  // Returns the print name string of the symbol, or undefined if none.
2478  Local<Value> Name() const;
2479 
2480  // Create a symbol. If name is not empty, it will be used as the description.
2481  static Local<Symbol> New(Isolate* isolate,
2482  Local<String> name = Local<String>());
2483 
2484  // Access global symbol registry.
2485  // Note that symbols created this way are never collected, so
2486  // they should only be used for statically fixed properties.
2487  // Also, there is only one global name space for the names used as keys.
2488  // To minimize the potential for clashes, use qualified names as keys.
2489  static Local<Symbol> For(Isolate *isolate, Local<String> name);
2490 
2491  // Retrieve a global symbol. Similar to |For|, but using a separate
2492  // registry that is not accessible by (and cannot clash with) JavaScript code.
2493  static Local<Symbol> ForApi(Isolate *isolate, Local<String> name);
2494 
2495  // Well-known symbols
2496  static Local<Symbol> GetIterator(Isolate* isolate);
2497  static Local<Symbol> GetUnscopables(Isolate* isolate);
2498  static Local<Symbol> GetToStringTag(Isolate* isolate);
2500 
2501  V8_INLINE static Symbol* Cast(v8::Value* obj);
2502 
2503  private:
2504  Symbol();
2505  static void CheckCast(v8::Value* obj);
2506 };
2507 
2508 
2509 /**
2510  * A private symbol
2511  *
2512  * This is an experimental feature. Use at your own risk.
2513  */
2514 class V8_EXPORT Private : public Data {
2515  public:
2516  // Returns the print name string of the private symbol, or undefined if none.
2517  Local<Value> Name() const;
2518 
2519  // Create a private symbol. If name is not empty, it will be the description.
2520  static Local<Private> New(Isolate* isolate,
2521  Local<String> name = Local<String>());
2522 
2523  // Retrieve a global private symbol. If a symbol with this name has not
2524  // been retrieved in the same isolate before, it is created.
2525  // Note that private symbols created this way are never collected, so
2526  // they should only be used for statically fixed properties.
2527  // Also, there is only one global name space for the names used as keys.
2528  // To minimize the potential for clashes, use qualified names as keys,
2529  // e.g., "Class#property".
2530  static Local<Private> ForApi(Isolate* isolate, Local<String> name);
2531 
2532  private:
2533  Private();
2534 };
2535 
2536 
2537 /**
2538  * A JavaScript number value (ECMA-262, 4.3.20)
2539  */
2540 class V8_EXPORT Number : public Primitive {
2541  public:
2542  double Value() const;
2543  static Local<Number> New(Isolate* isolate, double value);
2544  V8_INLINE static Number* Cast(v8::Value* obj);
2545  private:
2546  Number();
2547  static void CheckCast(v8::Value* obj);
2548 };
2549 
2550 
2551 /**
2552  * A JavaScript value representing a signed integer.
2553  */
2554 class V8_EXPORT Integer : public Number {
2555  public:
2556  static Local<Integer> New(Isolate* isolate, int32_t value);
2557  static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value);
2558  int64_t Value() const;
2559  V8_INLINE static Integer* Cast(v8::Value* obj);
2560  private:
2561  Integer();
2562  static void CheckCast(v8::Value* obj);
2563 };
2564 
2565 
2566 /**
2567  * A JavaScript value representing a 32-bit signed integer.
2568  */
2569 class V8_EXPORT Int32 : public Integer {
2570  public:
2571  int32_t Value() const;
2572  V8_INLINE static Int32* Cast(v8::Value* obj);
2573 
2574  private:
2575  Int32();
2576  static void CheckCast(v8::Value* obj);
2577 };
2578 
2579 
2580 /**
2581  * A JavaScript value representing a 32-bit unsigned integer.
2582  */
2583 class V8_EXPORT Uint32 : public Integer {
2584  public:
2585  uint32_t Value() const;
2586  V8_INLINE static Uint32* Cast(v8::Value* obj);
2587 
2588  private:
2589  Uint32();
2590  static void CheckCast(v8::Value* obj);
2591 };
2592 
2593 
2595  None = 0,
2596  ReadOnly = 1 << 0,
2597  DontEnum = 1 << 1,
2598  DontDelete = 1 << 2
2599 };
2600 
2601 /**
2602  * Accessor[Getter|Setter] are used as callback functions when
2603  * setting|getting a particular property. See Object and ObjectTemplate's
2604  * method SetAccessor.
2605  */
2606 typedef void (*AccessorGetterCallback)(
2607  Local<String> property,
2608  const PropertyCallbackInfo<Value>& info);
2610  Local<Name> property,
2611  const PropertyCallbackInfo<Value>& info);
2612 
2613 
2614 typedef void (*AccessorSetterCallback)(
2615  Local<String> property,
2616  Local<Value> value,
2617  const PropertyCallbackInfo<void>& info);
2619  Local<Name> property,
2620  Local<Value> value,
2621  const PropertyCallbackInfo<void>& info);
2622 
2623 
2624 /**
2625  * Access control specifications.
2626  *
2627  * Some accessors should be accessible across contexts. These
2628  * accessors have an explicit access control parameter which specifies
2629  * the kind of cross-context access that should be allowed.
2630  *
2631  * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused.
2632  */
2634  DEFAULT = 0,
2636  ALL_CAN_WRITE = 1 << 1,
2637  PROHIBITS_OVERWRITING = 1 << 2
2638 };
2639 
2640 
2641 /**
2642  * A JavaScript object (ECMA-262, 4.3.3)
2643  */
2644 class V8_EXPORT Object : public Value {
2645  public:
2646  V8_DEPRECATE_SOON("Use maybe version",
2647  bool Set(Local<Value> key, Local<Value> value));
2649  Local<Value> key, Local<Value> value);
2650 
2651  V8_DEPRECATE_SOON("Use maybe version",
2652  bool Set(uint32_t index, Local<Value> value));
2653  V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
2654  Local<Value> value);
2655 
2656  // Implements CreateDataProperty (ECMA-262, 7.3.4).
2657  //
2658  // Defines a configurable, writable, enumerable property with the given value
2659  // on the object unless the property already exists and is not configurable
2660  // or the object is not extensible.
2661  //
2662  // Returns true on success.
2664  Local<Name> key,
2665  Local<Value> value);
2667  uint32_t index,
2668  Local<Value> value);
2669 
2670  // Implements DefineOwnProperty.
2671  //
2672  // In general, CreateDataProperty will be faster, however, does not allow
2673  // for specifying attributes.
2674  //
2675  // Returns true on success.
2677  Local<Context> context, Local<Name> key, Local<Value> value,
2678  PropertyAttribute attributes = None);
2679 
2680  // Sets an own property on this object bypassing interceptors and
2681  // overriding accessors or read-only properties.
2682  //
2683  // Note that if the object has an interceptor the property will be set
2684  // locally, but since the interceptor takes precedence the local property
2685  // will only be returned if the interceptor doesn't return a value.
2686  //
2687  // Note also that this only works for named properties.
2688  V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty",
2689  bool ForceSet(Local<Value> key, Local<Value> value,
2690  PropertyAttribute attribs = None));
2691  V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty",
2692  Maybe<bool> ForceSet(Local<Context> context,
2693  Local<Value> key, Local<Value> value,
2694  PropertyAttribute attribs = None));
2695 
2696  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
2698  Local<Value> key);
2699 
2700  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2702  uint32_t index);
2703 
2704  /**
2705  * Gets the property attributes of a property which can be None or
2706  * any combination of ReadOnly, DontEnum and DontDelete. Returns
2707  * None when the property doesn't exist.
2708  */
2709  V8_DEPRECATED("Use maybe version",
2710  PropertyAttribute GetPropertyAttributes(Local<Value> key));
2712  Local<Context> context, Local<Value> key);
2713 
2714  /**
2715  * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3.
2716  */
2717  V8_DEPRECATED("Use maybe version",
2718  Local<Value> GetOwnPropertyDescriptor(Local<String> key));
2720  Local<Context> context, Local<String> key);
2721 
2722  V8_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key));
2724  Local<Value> key);
2725 
2726  V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key));
2727  // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2728  Maybe<bool> Delete(Local<Context> context, Local<Value> key);
2729 
2730  V8_DEPRECATED("Use maybe version", bool Has(uint32_t index));
2731  V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
2732 
2733  V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index));
2734  // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2735  Maybe<bool> Delete(Local<Context> context, uint32_t index);
2736 
2737  V8_DEPRECATED("Use maybe version",
2738  bool SetAccessor(Local<String> name,
2739  AccessorGetterCallback getter,
2740  AccessorSetterCallback setter = 0,
2741  Local<Value> data = Local<Value>(),
2742  AccessControl settings = DEFAULT,
2743  PropertyAttribute attribute = None));
2744  V8_DEPRECATED("Use maybe version",
2745  bool SetAccessor(Local<Name> name,
2747  AccessorNameSetterCallback setter = 0,
2748  Local<Value> data = Local<Value>(),
2749  AccessControl settings = DEFAULT,
2750  PropertyAttribute attribute = None));
2751  // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2752  Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name,
2754  AccessorNameSetterCallback setter = 0,
2756  AccessControl settings = DEFAULT,
2757  PropertyAttribute attribute = None);
2758 
2760  Local<Function> setter = Local<Function>(),
2761  PropertyAttribute attribute = None,
2762  AccessControl settings = DEFAULT);
2763 
2764  /**
2765  * Functionality for private properties.
2766  * This is an experimental feature, use at your own risk.
2767  * Note: Private properties are not inherited. Do not rely on this, since it
2768  * may change.
2769  */
2770  Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
2771  Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
2772  Local<Value> value);
2775 
2776  /**
2777  * Returns an array containing the names of the enumerable properties
2778  * of this object, including properties from prototype objects. The
2779  * array returned by this method contains the same values as would
2780  * be enumerated by a for-in statement over this object.
2781  */
2782  V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames());
2784  Local<Context> context);
2785 
2786  /**
2787  * This function has the same functionality as GetPropertyNames but
2788  * the returned array doesn't contain the names of properties from
2789  * prototype objects.
2790  */
2791  V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames());
2793  Local<Context> context);
2794 
2795  /**
2796  * Get the prototype object. This does not skip objects marked to
2797  * be skipped by __proto__ and it does not consult the security
2798  * handler.
2799  */
2801 
2802  /**
2803  * Set the prototype object. This does not skip objects marked to
2804  * be skipped by __proto__ and it does not consult the security
2805  * handler.
2806  */
2807  V8_DEPRECATED("Use maybe version", bool SetPrototype(Local<Value> prototype));
2809  Local<Value> prototype);
2810 
2811  /**
2812  * Finds an instance of the given function template in the prototype
2813  * chain.
2814  */
2816 
2817  /**
2818  * Call builtin Object.prototype.toString on this object.
2819  * This is different from Value::ToString() that may call
2820  * user-defined toString function. This one does not.
2821  */
2822  V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString());
2824  Local<Context> context);
2825 
2826  /**
2827  * Returns the name of the function invoked as a constructor for this object.
2828  */
2830 
2831  /** Gets the number of internal fields for this Object. */
2833 
2834  /** Same as above, but works for Persistents */
2836  const PersistentBase<Object>& object) {
2837  return object.val_->InternalFieldCount();
2838  }
2839 
2840  /** Gets the value from an internal field. */
2841  V8_INLINE Local<Value> GetInternalField(int index);
2842 
2843  /** Sets the value in an internal field. */
2844  void SetInternalField(int index, Local<Value> value);
2845 
2846  /**
2847  * Gets a 2-byte-aligned native pointer from an internal field. This field
2848  * must have been set by SetAlignedPointerInInternalField, everything else
2849  * leads to undefined behavior.
2850  */
2852 
2853  /** Same as above, but works for Persistents */
2855  const PersistentBase<Object>& object, int index) {
2856  return object.val_->GetAlignedPointerFromInternalField(index);
2857  }
2858 
2859  /**
2860  * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2861  * a field, GetAlignedPointerFromInternalField must be used, everything else
2862  * leads to undefined behavior.
2863  */
2864  void SetAlignedPointerInInternalField(int index, void* value);
2865 
2866  // Testers for local properties.
2867  V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key));
2869  Local<Name> key);
2870  V8_DEPRECATE_SOON("Use maybe version",
2871  bool HasRealNamedProperty(Local<String> key));
2873  Local<Name> key);
2874  V8_DEPRECATE_SOON("Use maybe version",
2875  bool HasRealIndexedProperty(uint32_t index));
2877  Local<Context> context, uint32_t index);
2878  V8_DEPRECATE_SOON("Use maybe version",
2879  bool HasRealNamedCallbackProperty(Local<String> key));
2881  Local<Context> context, Local<Name> key);
2882 
2883  /**
2884  * If result.IsEmpty() no real property was located in the prototype chain.
2885  * This means interceptors in the prototype chain are not called.
2886  */
2888  "Use maybe version",
2889  Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key));
2891  Local<Context> context, Local<Name> key);
2892 
2893  /**
2894  * Gets the property attributes of a real property in the prototype chain,
2895  * which can be None or any combination of ReadOnly, DontEnum and DontDelete.
2896  * Interceptors in the prototype chain are not called.
2897  */
2899  "Use maybe version",
2900  Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain(
2901  Local<String> key));
2904  Local<Name> key);
2905 
2906  /**
2907  * If result.IsEmpty() no real property was located on the object or
2908  * in the prototype chain.
2909  * This means interceptors in the prototype chain are not called.
2910  */
2911  V8_DEPRECATED("Use maybe version",
2912  Local<Value> GetRealNamedProperty(Local<String> key));
2914  Local<Context> context, Local<Name> key);
2915 
2916  /**
2917  * Gets the property attributes of a real property which can be
2918  * None or any combination of ReadOnly, DontEnum and DontDelete.
2919  * Interceptors in the prototype chain are not called.
2920  */
2921  V8_DEPRECATED("Use maybe version",
2922  Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2923  Local<String> key));
2925  Local<Context> context, Local<Name> key);
2926 
2927  /** Tests for a named lookup interceptor.*/
2929 
2930  /** Tests for an index lookup interceptor.*/
2932 
2933  /**
2934  * Returns the identity hash for this object. The current implementation
2935  * uses a hidden property on the object to store the identity hash.
2936  *
2937  * The return value will never be 0. Also, it is not guaranteed to be
2938  * unique.
2939  */
2941 
2942  V8_DEPRECATED("Use v8::Object::SetPrivate instead.",
2943  bool SetHiddenValue(Local<String> key, Local<Value> value));
2944  V8_DEPRECATED("Use v8::Object::GetPrivate instead.",
2945  Local<Value> GetHiddenValue(Local<String> key));
2946  V8_DEPRECATED("Use v8::Object::DeletePrivate instead.",
2947  bool DeleteHiddenValue(Local<String> key));
2948 
2949  /**
2950  * Clone this object with a fast but shallow copy. Values will point
2951  * to the same values as the original object.
2952  */
2953  // TODO(dcarney): take an isolate and optionally bail out?
2955 
2956  /**
2957  * Returns the context in which the object was created.
2958  */
2960 
2961  /**
2962  * Checks whether a callback is set by the
2963  * ObjectTemplate::SetCallAsFunctionHandler method.
2964  * When an Object is callable this method returns true.
2965  */
2966  bool IsCallable();
2967 
2968  /**
2969  * Call an Object as a function if a callback is set by the
2970  * ObjectTemplate::SetCallAsFunctionHandler method.
2971  */
2972  V8_DEPRECATED("Use maybe version",
2973  Local<Value> CallAsFunction(Local<Value> recv, int argc,
2974  Local<Value> argv[]));
2976  Local<Value> recv,
2977  int argc,
2978  Local<Value> argv[]);
2979 
2980  /**
2981  * Call an Object as a constructor if a callback is set by the
2982  * ObjectTemplate::SetCallAsFunctionHandler method.
2983  * Note: This method behaves like the Function::NewInstance method.
2984  */
2985  V8_DEPRECATED("Use maybe version",
2986  Local<Value> CallAsConstructor(int argc, Local<Value> argv[]));
2988  Local<Context> context, int argc, Local<Value> argv[]);
2989 
2990  /**
2991  * Return the isolate to which the Object belongs to.
2992  */
2993  V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate());
2994 
2995  static Local<Object> New(Isolate* isolate);
2996 
2997  V8_INLINE static Object* Cast(Value* obj);
2998 
2999  private:
3000  Object();
3001  static void CheckCast(Value* obj);
3002  Local<Value> SlowGetInternalField(int index);
3003  void* SlowGetAlignedPointerFromInternalField(int index);
3004 };
3005 
3006 
3007 /**
3008  * An instance of the built-in array constructor (ECMA-262, 15.4.2).
3009  */
3010 class V8_EXPORT Array : public Object {
3011  public:
3012  uint32_t Length() const;
3013 
3014  /**
3015  * Clones an element at index |index|. Returns an empty
3016  * handle if cloning fails (for any reason).
3017  */
3018  V8_DEPRECATED("Cloning is not supported.",
3019  Local<Object> CloneElementAt(uint32_t index));
3020  V8_DEPRECATED("Cloning is not supported.",
3021  MaybeLocal<Object> CloneElementAt(Local<Context> context,
3022  uint32_t index));
3023 
3024  /**
3025  * Creates a JavaScript array with the given length. If the length
3026  * is negative the returned array will have length 0.
3027  */
3028  static Local<Array> New(Isolate* isolate, int length = 0);
3029 
3030  V8_INLINE static Array* Cast(Value* obj);
3031  private:
3032  Array();
3033  static void CheckCast(Value* obj);
3034 };
3035 
3036 
3037 /**
3038  * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
3039  */
3040 class V8_EXPORT Map : public Object {
3041  public:
3042  size_t Size() const;
3043  void Clear();
3045  Local<Value> key);
3047  Local<Value> key,
3048  Local<Value> value);
3050  Local<Value> key);
3052  Local<Value> key);
3053 
3054  /**
3055  * Returns an array of length Size() * 2, where index N is the Nth key and
3056  * index N + 1 is the Nth value.
3057  */
3058  Local<Array> AsArray() const;
3059 
3060  /**
3061  * Creates a new empty Map.
3062  */
3063  static Local<Map> New(Isolate* isolate);
3064 
3065  V8_INLINE static Map* Cast(Value* obj);
3066 
3067  private:
3068  Map();
3069  static void CheckCast(Value* obj);
3070 };
3071 
3072 
3073 /**
3074  * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
3075  */
3076 class V8_EXPORT Set : public Object {
3077  public:
3078  size_t Size() const;
3079  void Clear();
3081  Local<Value> key);
3083  Local<Value> key);
3085  Local<Value> key);
3086 
3087  /**
3088  * Returns an array of the keys in this Set.
3089  */
3090  Local<Array> AsArray() const;
3091 
3092  /**
3093  * Creates a new empty Set.
3094  */
3095  static Local<Set> New(Isolate* isolate);
3096 
3097  V8_INLINE static Set* Cast(Value* obj);
3098 
3099  private:
3100  Set();
3101  static void CheckCast(Value* obj);
3102 };
3103 
3104 
3105 template<typename T>
3107  public:
3108  template <class S> V8_INLINE ReturnValue(const ReturnValue<S>& that)
3109  : value_(that.value_) {
3110  TYPE_CHECK(T, S);
3111  }
3112  // Local setters
3113  template <typename S>
3114  V8_INLINE V8_DEPRECATE_SOON("Use Global<> instead",
3115  void Set(const Persistent<S>& handle));
3116  template <typename S>
3117  V8_INLINE void Set(const Global<S>& handle);
3118  template <typename S>
3119  V8_INLINE void Set(const Local<S> handle);
3120  // Fast primitive setters
3121  V8_INLINE void Set(bool value);
3122  V8_INLINE void Set(double i);
3123  V8_INLINE void Set(int32_t i);
3124  V8_INLINE void Set(uint32_t i);
3125  // Fast JS primitive setters
3126  V8_INLINE void SetNull();
3127  V8_INLINE void SetUndefined();
3128  V8_INLINE void SetEmptyString();
3129  // Convenience getter for Isolate
3131 
3132  // Pointer setter: Uncompilable to prevent inadvertent misuse.
3133  template <typename S>
3134  V8_INLINE void Set(S* whatever);
3135 
3136  private:
3137  template<class F> friend class ReturnValue;
3138  template<class F> friend class FunctionCallbackInfo;
3139  template<class F> friend class PropertyCallbackInfo;
3140  template <class F, class G, class H>
3142  V8_INLINE void SetInternal(internal::Object* value) { *value_ = value; }
3143  V8_INLINE internal::Object* GetDefaultValue();
3144  V8_INLINE explicit ReturnValue(internal::Object** slot);
3145  internal::Object** value_;
3146 };
3147 
3148 
3149 /**
3150  * The argument information given to function call callbacks. This
3151  * class provides access to information about the context of the call,
3152  * including the receiver, the number and values of arguments, and
3153  * the holder of the function.
3154  */
3155 template<typename T>
3157  public:
3158  V8_INLINE int Length() const;
3159  V8_INLINE Local<Value> operator[](int i) const;
3160  V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead",
3161  Local<Function> Callee() const);
3162  V8_INLINE Local<Object> This() const;
3163  V8_INLINE Local<Object> Holder() const;
3164  V8_INLINE bool IsConstructCall() const;
3165  V8_INLINE Local<Value> Data() const;
3166  V8_INLINE Isolate* GetIsolate() const;
3168  // This shouldn't be public, but the arm compiler needs it.
3169  static const int kArgsLength = 7;
3170 
3171  protected:
3172  friend class internal::FunctionCallbackArguments;
3174  static const int kHolderIndex = 0;
3175  static const int kIsolateIndex = 1;
3176  static const int kReturnValueDefaultValueIndex = 2;
3177  static const int kReturnValueIndex = 3;
3178  static const int kDataIndex = 4;
3179  static const int kCalleeIndex = 5;
3180  static const int kContextSaveIndex = 6;
3181 
3182  V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args,
3183  internal::Object** values,
3184  int length,
3185  bool is_construct_call);
3187  internal::Object** values_;
3188  int length_;
3190 };
3191 
3192 
3193 /**
3194  * The information passed to a property callback about the context
3195  * of the property access.
3196  */
3197 template<typename T>
3199  public:
3206  // This shouldn't be public, but the arm compiler needs it.
3207  static const int kArgsLength = 7;
3208 
3209  protected:
3210  friend class MacroAssembler;
3211  friend class internal::PropertyCallbackArguments;
3213  static const int kShouldThrowOnErrorIndex = 0;
3214  static const int kHolderIndex = 1;
3215  static const int kIsolateIndex = 2;
3216  static const int kReturnValueDefaultValueIndex = 3;
3217  static const int kReturnValueIndex = 4;
3218  static const int kDataIndex = 5;
3219  static const int kThisIndex = 6;
3220 
3221  V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {}
3222  internal::Object** args_;
3223 };
3224 
3225 
3226 typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
3227 
3228 
3229 /**
3230  * A JavaScript function object (ECMA-262, 15.3).
3231  */
3232 class V8_EXPORT Function : public Object {
3233  public:
3234  /**
3235  * Create a function in the current execution context
3236  * for a given FunctionCallback.
3237  */
3239  FunctionCallback callback,
3240  Local<Value> data = Local<Value>(),
3241  int length = 0);
3243  "Use maybe version",
3244  Local<Function> New(Isolate* isolate, FunctionCallback callback,
3245  Local<Value> data = Local<Value>(), int length = 0));
3246 
3247  V8_DEPRECATED("Use maybe version",
3248  Local<Object> NewInstance(int argc, Local<Value> argv[]) const);
3250  Local<Context> context, int argc, Local<Value> argv[]) const;
3251 
3252  V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const);
3254  Local<Context> context) const {
3255  return NewInstance(context, 0, nullptr);
3256  }
3257 
3258  V8_DEPRECATE_SOON("Use maybe version",
3259  Local<Value> Call(Local<Value> recv, int argc,
3260  Local<Value> argv[]));
3262  Local<Value> recv, int argc,
3263  Local<Value> argv[]);
3264 
3265  void SetName(Local<String> name);
3266  Local<Value> GetName() const;
3267 
3268  /**
3269  * Name inferred from variable or property assignment of this function.
3270  * Used to facilitate debugging and profiling of JavaScript code written
3271  * in an OO style, where many functions are anonymous but are assigned
3272  * to object properties.
3273  */
3275 
3276  /**
3277  * displayName if it is set, otherwise name if it is configured, otherwise
3278  * function name, otherwise inferred name.
3279  */
3281 
3282  /**
3283  * User-defined name assigned to the "displayName" property of this function.
3284  * Used to facilitate debugging and profiling of JavaScript code.
3285  */
3287 
3288  /**
3289  * Returns zero based line number of function body and
3290  * kLineOffsetNotFound if no information available.
3291  */
3292  int GetScriptLineNumber() const;
3293  /**
3294  * Returns zero based column number of function body and
3295  * kLineOffsetNotFound if no information available.
3296  */
3298 
3299  /**
3300  * Tells whether this function is builtin.
3301  */
3302  bool IsBuiltin() const;
3303 
3304  /**
3305  * Returns scriptId.
3306  */
3307  int ScriptId() const;
3308 
3309  /**
3310  * Returns the original function if this function is bound, else returns
3311  * v8::Undefined.
3312  */
3314 
3316  V8_INLINE static Function* Cast(Value* obj);
3317  static const int kLineOffsetNotFound;
3318 
3319  private:
3320  Function();
3321  static void CheckCast(Value* obj);
3322 };
3323 
3324 
3325 /**
3326  * An instance of the built-in Promise constructor (ES6 draft).
3327  * This API is experimental. Only works with --harmony flag.
3328  */
3329 class V8_EXPORT Promise : public Object {
3330  public:
3331  class V8_EXPORT Resolver : public Object {
3332  public:
3333  /**
3334  * Create a new resolver, along with an associated promise in pending state.
3335  */
3336  static V8_DEPRECATE_SOON("Use maybe version",
3337  Local<Resolver> New(Isolate* isolate));
3339  Local<Context> context);
3340 
3341  /**
3342  * Extract the associated promise.
3343  */
3345 
3346  /**
3347  * Resolve/reject the associated promise with a given value.
3348  * Ignored if the promise is no longer pending.
3349  */
3350  V8_DEPRECATE_SOON("Use maybe version", void Resolve(Local<Value> value));
3351  // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3352  Maybe<bool> Resolve(Local<Context> context, Local<Value> value);
3353 
3354  V8_DEPRECATE_SOON("Use maybe version", void Reject(Local<Value> value));
3355  // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3356  Maybe<bool> Reject(Local<Context> context, Local<Value> value);
3357 
3358  V8_INLINE static Resolver* Cast(Value* obj);
3359 
3360  private:
3361  Resolver();
3362  static void CheckCast(Value* obj);
3363  };
3364 
3365  /**
3366  * Register a resolution/rejection handler with a promise.
3367  * The handler is given the respective resolution/rejection value as
3368  * an argument. If the promise is already resolved/rejected, the handler is
3369  * invoked at the end of turn.
3370  */
3371  V8_DEPRECATED("Use maybe version of Then",
3372  Local<Promise> Chain(Local<Function> handler));
3373  V8_DEPRECATED("Use Then",
3375  Local<Context> context, Local<Function> handler));
3376 
3377  V8_DEPRECATED("Use maybe version",
3378  Local<Promise> Catch(Local<Function> handler));
3380  Local<Function> handler);
3381 
3382  V8_DEPRECATED("Use maybe version",
3383  Local<Promise> Then(Local<Function> handler));
3385  Local<Function> handler);
3386 
3387  /**
3388  * Returns true if the promise has at least one derived promise, and
3389  * therefore resolve/reject handlers (including default handler).
3390  */
3391  bool HasHandler();
3392 
3393  V8_INLINE static Promise* Cast(Value* obj);
3394 
3395  private:
3396  Promise();
3397  static void CheckCast(Value* obj);
3398 };
3399 
3400 
3401 /**
3402  * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition,
3403  * 26.2.1).
3404  */
3405 class V8_EXPORT Proxy : public Object {
3406  public:
3409  bool IsRevoked();
3410  void Revoke();
3411 
3412  /**
3413  * Creates a new empty Map.
3414  */
3415  static MaybeLocal<Proxy> New(Local<Context> context,
3416  Local<Object> local_target,
3417  Local<Object> local_handler);
3418 
3419  V8_INLINE static Proxy* Cast(Value* obj);
3420 
3421  private:
3422  Proxy();
3423  static void CheckCast(Value* obj);
3424 };
3425 
3426 
3427 #ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
3428 // The number of required internal fields can be defined by embedder.
3429 #define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
3430 #endif
3431 
3432 
3434 
3435 
3436 /**
3437  * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
3438  * This API is experimental and may change significantly.
3439  */
3440 class V8_EXPORT ArrayBuffer : public Object {
3441  public:
3442  /**
3443  * Allocator that V8 uses to allocate |ArrayBuffer|'s memory.
3444  * The allocator is a global V8 setting. It has to be set via
3445  * Isolate::CreateParams.
3446  *
3447  * This API is experimental and may change significantly.
3448  */
3449  class V8_EXPORT Allocator { // NOLINT
3450  public:
3451  virtual ~Allocator() {}
3452 
3453  /**
3454  * Allocate |length| bytes. Return NULL if allocation is not successful.
3455  * Memory should be initialized to zeroes.
3456  */
3457  virtual void* Allocate(size_t length) = 0;
3458 
3459  /**
3460  * Allocate |length| bytes. Return NULL if allocation is not successful.
3461  * Memory does not have to be initialized.
3462  */
3463  virtual void* AllocateUninitialized(size_t length) = 0;
3464  /**
3465  * Free the memory block of size |length|, pointed to by |data|.
3466  * That memory is guaranteed to be previously allocated by |Allocate|.
3467  */
3468  virtual void Free(void* data, size_t length) = 0;
3469  };
3470 
3471  /**
3472  * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
3473  * returns an instance of this class, populated, with a pointer to data
3474  * and byte length.
3475  *
3476  * The Data pointer of ArrayBuffer::Contents is always allocated with
3477  * Allocator::Allocate that is set via Isolate::CreateParams.
3478  *
3479  * This API is experimental and may change significantly.
3480  */
3481  class V8_EXPORT Contents { // NOLINT
3482  public:
3483  Contents() : data_(NULL), byte_length_(0) {}
3484 
3485  void* Data() const { return data_; }
3486  size_t ByteLength() const { return byte_length_; }
3487 
3488  private:
3489  void* data_;
3490  size_t byte_length_;
3491 
3492  friend class ArrayBuffer;
3493  };
3494 
3495 
3496  /**
3497  * Data length in bytes.
3498  */
3499  size_t ByteLength() const;
3500 
3501  /**
3502  * Create a new ArrayBuffer. Allocate |byte_length| bytes.
3503  * Allocated memory will be owned by a created ArrayBuffer and
3504  * will be deallocated when it is garbage-collected,
3505  * unless the object is externalized.
3506  */
3507  static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
3508 
3509  /**
3510  * Create a new ArrayBuffer over an existing memory block.
3511  * The created array buffer is by default immediately in externalized state.
3512  * The memory block will not be reclaimed when a created ArrayBuffer
3513  * is garbage-collected.
3514  */
3516  Isolate* isolate, void* data, size_t byte_length,
3518 
3519  /**
3520  * Returns true if ArrayBuffer is externalized, that is, does not
3521  * own its memory block.
3522  */
3523  bool IsExternal() const;
3524 
3525  /**
3526  * Returns true if this ArrayBuffer may be neutered.
3527  */
3528  bool IsNeuterable() const;
3529 
3530  /**
3531  * Neuters this ArrayBuffer and all its views (typed arrays).
3532  * Neutering sets the byte length of the buffer and all typed arrays to zero,
3533  * preventing JavaScript from ever accessing underlying backing store.
3534  * ArrayBuffer should have been externalized and must be neuterable.
3535  */
3536  void Neuter();
3537 
3538  /**
3539  * Make this ArrayBuffer external. The pointer to underlying memory block
3540  * and byte length are returned as |Contents| structure. After ArrayBuffer
3541  * had been etxrenalized, it does no longer owns the memory block. The caller
3542  * should take steps to free memory when it is no longer needed.
3543  *
3544  * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3545  * that has been set via Isolate::CreateParams.
3546  */
3548 
3549  /**
3550  * Get a pointer to the ArrayBuffer's underlying memory block without
3551  * externalizing it. If the ArrayBuffer is not externalized, this pointer
3552  * will become invalid as soon as the ArrayBuffer became garbage collected.
3553  *
3554  * The embedder should make sure to hold a strong reference to the
3555  * ArrayBuffer while accessing this pointer.
3556  *
3557  * The memory block is guaranteed to be allocated with |Allocator::Allocate|.
3558  */
3560 
3561  V8_INLINE static ArrayBuffer* Cast(Value* obj);
3562 
3564 
3565  private:
3566  ArrayBuffer();
3567  static void CheckCast(Value* obj);
3568 };
3569 
3570 
3571 #ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
3572 // The number of required internal fields can be defined by embedder.
3573 #define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
3574 #endif
3575 
3576 
3577 /**
3578  * A base class for an instance of one of "views" over ArrayBuffer,
3579  * including TypedArrays and DataView (ES6 draft 15.13).
3580  *
3581  * This API is experimental and may change significantly.
3582  */
3584  public:
3585  /**
3586  * Returns underlying ArrayBuffer.
3587  */
3589  /**
3590  * Byte offset in |Buffer|.
3591  */
3592  size_t ByteOffset();
3593  /**
3594  * Size of a view in bytes.
3595  */
3596  size_t ByteLength();
3597 
3598  /**
3599  * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3600  * memory without additional overhead that calling ArrayBufferView::Buffer
3601  * might incur.
3602  *
3603  * Will write at most min(|byte_length|, ByteLength) bytes starting at
3604  * ByteOffset of the underling buffer to the memory starting at |dest|.
3605  * Returns the number of bytes actually written.
3606  */
3607  size_t CopyContents(void* dest, size_t byte_length);
3608 
3609  /**
3610  * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3611  * allocated.
3612  */
3613  bool HasBuffer() const;
3614 
3615  V8_INLINE static ArrayBufferView* Cast(Value* obj);
3616 
3617  static const int kInternalFieldCount =
3619 
3620  private:
3621  ArrayBufferView();
3622  static void CheckCast(Value* obj);
3623 };
3624 
3625 
3626 /**
3627  * A base class for an instance of TypedArray series of constructors
3628  * (ES6 draft 15.13.6).
3629  * This API is experimental and may change significantly.
3630  */
3632  public:
3633  /**
3634  * Number of elements in this typed array
3635  * (e.g. for Int16Array, |ByteLength|/2).
3636  */
3637  size_t Length();
3638 
3639  V8_INLINE static TypedArray* Cast(Value* obj);
3640 
3641  private:
3642  TypedArray();
3643  static void CheckCast(Value* obj);
3644 };
3645 
3646 
3647 /**
3648  * An instance of Uint8Array constructor (ES6 draft 15.13.6).
3649  * This API is experimental and may change significantly.
3650  */
3652  public:
3653  static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
3654  size_t byte_offset, size_t length);
3655  static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3656  size_t byte_offset, size_t length);
3657  V8_INLINE static Uint8Array* Cast(Value* obj);
3658 
3659  private:
3660  Uint8Array();
3661  static void CheckCast(Value* obj);
3662 };
3663 
3664 
3665 /**
3666  * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
3667  * This API is experimental and may change significantly.
3668  */
3670  public:
3672  size_t byte_offset, size_t length);
3674  Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
3675  size_t length);
3676  V8_INLINE static Uint8ClampedArray* Cast(Value* obj);
3677 
3678  private:
3679  Uint8ClampedArray();
3680  static void CheckCast(Value* obj);
3681 };
3682 
3683 /**
3684  * An instance of Int8Array constructor (ES6 draft 15.13.6).
3685  * This API is experimental and may change significantly.
3686  */
3688  public:
3689  static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
3690  size_t byte_offset, size_t length);
3691  static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3692  size_t byte_offset, size_t length);
3693  V8_INLINE static Int8Array* Cast(Value* obj);
3694 
3695  private:
3696  Int8Array();
3697  static void CheckCast(Value* obj);
3698 };
3699 
3700 
3701 /**
3702  * An instance of Uint16Array constructor (ES6 draft 15.13.6).
3703  * This API is experimental and may change significantly.
3704  */
3706  public:
3707  static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
3708  size_t byte_offset, size_t length);
3709  static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3710  size_t byte_offset, size_t length);
3711  V8_INLINE static Uint16Array* Cast(Value* obj);
3712 
3713  private:
3714  Uint16Array();
3715  static void CheckCast(Value* obj);
3716 };
3717 
3718 
3719 /**
3720  * An instance of Int16Array constructor (ES6 draft 15.13.6).
3721  * This API is experimental and may change significantly.
3722  */
3724  public:
3725  static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
3726  size_t byte_offset, size_t length);
3727  static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3728  size_t byte_offset, size_t length);
3729  V8_INLINE static Int16Array* Cast(Value* obj);
3730 
3731  private:
3732  Int16Array();
3733  static void CheckCast(Value* obj);
3734 };
3735 
3736 
3737 /**
3738  * An instance of Uint32Array constructor (ES6 draft 15.13.6).
3739  * This API is experimental and may change significantly.
3740  */
3742  public:
3743  static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
3744  size_t byte_offset, size_t length);
3745  static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3746  size_t byte_offset, size_t length);
3747  V8_INLINE static Uint32Array* Cast(Value* obj);
3748 
3749  private:
3750  Uint32Array();
3751  static void CheckCast(Value* obj);
3752 };
3753 
3754 
3755 /**
3756  * An instance of Int32Array constructor (ES6 draft 15.13.6).
3757  * This API is experimental and may change significantly.
3758  */
3760  public:
3761  static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
3762  size_t byte_offset, size_t length);
3763  static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3764  size_t byte_offset, size_t length);
3765  V8_INLINE static Int32Array* Cast(Value* obj);
3766 
3767  private:
3768  Int32Array();
3769  static void CheckCast(Value* obj);
3770 };
3771 
3772 
3773 /**
3774  * An instance of Float32Array constructor (ES6 draft 15.13.6).
3775  * This API is experimental and may change significantly.
3776  */
3778  public:
3779  static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
3780  size_t byte_offset, size_t length);
3781  static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3782  size_t byte_offset, size_t length);
3783  V8_INLINE static Float32Array* Cast(Value* obj);
3784 
3785  private:
3786  Float32Array();
3787  static void CheckCast(Value* obj);
3788 };
3789 
3790 
3791 /**
3792  * An instance of Float64Array constructor (ES6 draft 15.13.6).
3793  * This API is experimental and may change significantly.
3794  */
3796  public:
3797  static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
3798  size_t byte_offset, size_t length);
3799  static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3800  size_t byte_offset, size_t length);
3801  V8_INLINE static Float64Array* Cast(Value* obj);
3802 
3803  private:
3804  Float64Array();
3805  static void CheckCast(Value* obj);
3806 };
3807 
3808 
3809 /**
3810  * An instance of DataView constructor (ES6 draft 15.13.7).
3811  * This API is experimental and may change significantly.
3812  */
3814  public:
3815  static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3816  size_t byte_offset, size_t length);
3817  static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
3818  size_t byte_offset, size_t length);
3819  V8_INLINE static DataView* Cast(Value* obj);
3820 
3821  private:
3822  DataView();
3823  static void CheckCast(Value* obj);
3824 };
3825 
3826 
3827 /**
3828  * An instance of the built-in SharedArrayBuffer constructor.
3829  * This API is experimental and may change significantly.
3830  */
3832  public:
3833  /**
3834  * The contents of an |SharedArrayBuffer|. Externalization of
3835  * |SharedArrayBuffer| returns an instance of this class, populated, with a
3836  * pointer to data and byte length.
3837  *
3838  * The Data pointer of SharedArrayBuffer::Contents is always allocated with
3839  * |ArrayBuffer::Allocator::Allocate| by the allocator specified in
3840  * v8::Isolate::CreateParams::array_buffer_allocator.
3841  *
3842  * This API is experimental and may change significantly.
3843  */
3844  class V8_EXPORT Contents { // NOLINT
3845  public:
3846  Contents() : data_(NULL), byte_length_(0) {}
3847 
3848  void* Data() const { return data_; }
3849  size_t ByteLength() const { return byte_length_; }
3850 
3851  private:
3852  void* data_;
3853  size_t byte_length_;
3854 
3855  friend class SharedArrayBuffer;
3856  };
3857 
3858 
3859  /**
3860  * Data length in bytes.
3861  */
3862  size_t ByteLength() const;
3863 
3864  /**
3865  * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3866  * Allocated memory will be owned by a created SharedArrayBuffer and
3867  * will be deallocated when it is garbage-collected,
3868  * unless the object is externalized.
3869  */
3870  static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3871 
3872  /**
3873  * Create a new SharedArrayBuffer over an existing memory block. The created
3874  * array buffer is immediately in externalized state unless otherwise
3875  * specified. The memory block will not be reclaimed when a created
3876  * SharedArrayBuffer is garbage-collected.
3877  */
3879  Isolate* isolate, void* data, size_t byte_length,
3881 
3882  /**
3883  * Returns true if SharedArrayBuffer is externalized, that is, does not
3884  * own its memory block.
3885  */
3886  bool IsExternal() const;
3887 
3888  /**
3889  * Make this SharedArrayBuffer external. The pointer to underlying memory
3890  * block and byte length are returned as |Contents| structure. After
3891  * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory
3892  * block. The caller should take steps to free memory when it is no longer
3893  * needed.
3894  *
3895  * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3896  * by the allocator specified in
3897  * v8::Isolate::CreateParams::array_buffer_allocator.
3898  *
3899  */
3901 
3902  /**
3903  * Get a pointer to the ArrayBuffer's underlying memory block without
3904  * externalizing it. If the ArrayBuffer is not externalized, this pointer
3905  * will become invalid as soon as the ArrayBuffer became garbage collected.
3906  *
3907  * The embedder should make sure to hold a strong reference to the
3908  * ArrayBuffer while accessing this pointer.
3909  *
3910  * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3911  * by the allocator specified in
3912  * v8::Isolate::CreateParams::array_buffer_allocator.
3913  */
3915 
3916  V8_INLINE static SharedArrayBuffer* Cast(Value* obj);
3917 
3919 
3920  private:
3921  SharedArrayBuffer();
3922  static void CheckCast(Value* obj);
3923 };
3924 
3925 
3926 /**
3927  * An instance of the built-in Date constructor (ECMA-262, 15.9).
3928  */
3929 class V8_EXPORT Date : public Object {
3930  public:
3931  static V8_DEPRECATE_SOON("Use maybe version.",
3932  Local<Value> New(Isolate* isolate, double time));
3934  double time);
3935 
3936  /**
3937  * A specialization of Value::NumberValue that is more efficient
3938  * because we know the structure of this object.
3939  */
3940  double ValueOf() const;
3941 
3942  V8_INLINE static Date* Cast(v8::Value* obj);
3943 
3944  /**
3945  * Notification that the embedder has changed the time zone,
3946  * daylight savings time, or other date / time configuration
3947  * parameters. V8 keeps a cache of various values used for
3948  * date / time computation. This notification will reset
3949  * those cached values for the current context so that date /
3950  * time configuration changes would be reflected in the Date
3951  * object.
3952  *
3953  * This API should not be called more than needed as it will
3954  * negatively impact the performance of date operations.
3955  */
3957 
3958  private:
3959  static void CheckCast(v8::Value* obj);
3960 };
3961 
3962 
3963 /**
3964  * A Number object (ECMA-262, 4.3.21).
3965  */
3967  public:
3968  static Local<Value> New(Isolate* isolate, double value);
3969 
3970  double ValueOf() const;
3971 
3972  V8_INLINE static NumberObject* Cast(v8::Value* obj);
3973 
3974  private:
3975  static void CheckCast(v8::Value* obj);
3976 };
3977 
3978 
3979 /**
3980  * A Boolean object (ECMA-262, 4.3.15).
3981  */
3983  public:
3984  static Local<Value> New(Isolate* isolate, bool value);
3985  V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value));
3986 
3987  bool ValueOf() const;
3988 
3989  V8_INLINE static BooleanObject* Cast(v8::Value* obj);
3990 
3991  private:
3992  static void CheckCast(v8::Value* obj);
3993 };
3994 
3995 
3996 /**
3997  * A String object (ECMA-262, 4.3.18).
3998  */
4000  public:
4001  static Local<Value> New(Local<String> value);
4002 
4004 
4005  V8_INLINE static StringObject* Cast(v8::Value* obj);
4006 
4007  private:
4008  static void CheckCast(v8::Value* obj);
4009 };
4010 
4011 
4012 /**
4013  * A Symbol object (ECMA-262 edition 6).
4014  *
4015  * This is an experimental feature. Use at your own risk.
4016  */
4018  public:
4019  static Local<Value> New(Isolate* isolate, Local<Symbol> value);
4020 
4022 
4023  V8_INLINE static SymbolObject* Cast(v8::Value* obj);
4024 
4025  private:
4026  static void CheckCast(v8::Value* obj);
4027 };
4028 
4029 
4030 /**
4031  * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
4032  */
4033 class V8_EXPORT RegExp : public Object {
4034  public:
4035  /**
4036  * Regular expression flag bits. They can be or'ed to enable a set
4037  * of flags.
4038  */
4039  enum Flags {
4040  kNone = 0,
4041  kGlobal = 1,
4044  kSticky = 8,
4045  kUnicode = 16
4046  };
4047 
4048  /**
4049  * Creates a regular expression from the given pattern string and
4050  * the flags bit field. May throw a JavaScript exception as
4051  * described in ECMA-262, 15.10.4.1.
4052  *
4053  * For example,
4054  * RegExp::New(v8::String::New("foo"),
4055  * static_cast<RegExp::Flags>(kGlobal | kMultiline))
4056  * is equivalent to evaluating "/foo/gm".
4057  */
4058  static V8_DEPRECATE_SOON("Use maybe version",
4059  Local<RegExp> New(Local<String> pattern,
4060  Flags flags));
4062  Local<String> pattern,
4063  Flags flags);
4064 
4065  /**
4066  * Returns the value of the source property: a string representing
4067  * the regular expression.
4068  */
4070 
4071  /**
4072  * Returns the flags bit field.
4073  */
4074  Flags GetFlags() const;
4075 
4076  V8_INLINE static RegExp* Cast(v8::Value* obj);
4077 
4078  private:
4079  static void CheckCast(v8::Value* obj);
4080 };
4081 
4082 
4083 /**
4084  * A JavaScript value that wraps a C++ void*. This type of value is mainly used
4085  * to associate C++ data structures with JavaScript objects.
4086  */
4087 class V8_EXPORT External : public Value {
4088  public:
4089  static Local<External> New(Isolate* isolate, void* value);
4090  V8_INLINE static External* Cast(Value* obj);
4091  void* Value() const;
4092  private:
4093  static void CheckCast(v8::Value* obj);
4094 };
4095 
4096 
4097 #define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator)
4098 
4100 #define V8_DECL_INTRINSIC(name, iname) k##name,
4102 #undef V8_DECL_INTRINSIC
4103 };
4104 
4105 
4106 // --- Templates ---
4107 
4108 
4109 /**
4110  * The superclass of object and function templates.
4111  */
4112 class V8_EXPORT Template : public Data {
4113  public:
4114  /** Adds a property to each instance created by this template.*/
4115  void Set(Local<Name> name, Local<Data> value,
4116  PropertyAttribute attributes = None);
4117  V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value);
4118 
4120  Local<Name> name,
4123  PropertyAttribute attribute = None,
4124  AccessControl settings = DEFAULT);
4125 
4126  /**
4127  * Whenever the property with the given name is accessed on objects
4128  * created from this Template the getter and setter callbacks
4129  * are called instead of getting and setting the property directly
4130  * on the JavaScript object.
4131  *
4132  * \param name The name of the property for which an accessor is added.
4133  * \param getter The callback to invoke when getting the property.
4134  * \param setter The callback to invoke when setting the property.
4135  * \param data A piece of data that will be passed to the getter and setter
4136  * callbacks whenever they are invoked.
4137  * \param settings Access control settings for the accessor. This is a bit
4138  * field consisting of one of more of
4139  * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4140  * The default is to not allow cross-context access.
4141  * ALL_CAN_READ means that all cross-context reads are allowed.
4142  * ALL_CAN_WRITE means that all cross-context writes are allowed.
4143  * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4144  * cross-context access.
4145  * \param attribute The attributes of the property for which an accessor
4146  * is added.
4147  * \param signature The signature describes valid receivers for the accessor
4148  * and is used to perform implicit instance checks against them. If the
4149  * receiver is incompatible (i.e. is not an instance of the constructor as
4150  * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4151  * thrown and no callback is invoked.
4152  */
4154  Local<String> name, AccessorGetterCallback getter,
4155  AccessorSetterCallback setter = 0,
4156  // TODO(dcarney): gcc can't handle Local below
4157  Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4159  AccessControl settings = DEFAULT);
4161  Local<Name> name, AccessorNameGetterCallback getter,
4162  AccessorNameSetterCallback setter = 0,
4163  // TODO(dcarney): gcc can't handle Local below
4164  Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4166  AccessControl settings = DEFAULT);
4167 
4168  /**
4169  * During template instantiation, sets the value with the intrinsic property
4170  * from the correct context.
4171  */
4173  PropertyAttribute attribute = None);
4174 
4175  private:
4176  Template();
4177 
4178  friend class ObjectTemplate;
4179  friend class FunctionTemplate;
4180 };
4181 
4182 
4183 /**
4184  * NamedProperty[Getter|Setter] are used as interceptors on object.
4185  * See ObjectTemplate::SetNamedPropertyHandler.
4186  */
4188  Local<String> property,
4189  const PropertyCallbackInfo<Value>& info);
4190 
4191 
4192 /**
4193  * Returns the value if the setter intercepts the request.
4194  * Otherwise, returns an empty handle.
4195  */
4197  Local<String> property,
4198  Local<Value> value,
4199  const PropertyCallbackInfo<Value>& info);
4200 
4201 
4202 /**
4203  * Returns a non-empty handle if the interceptor intercepts the request.
4204  * The result is an integer encoding property attributes (like v8::None,
4205  * v8::DontEnum, etc.)
4206  */
4208  Local<String> property,
4209  const PropertyCallbackInfo<Integer>& info);
4210 
4211 
4212 /**
4213  * Returns a non-empty handle if the deleter intercepts the request.
4214  * The return value is true if the property could be deleted and false
4215  * otherwise.
4216  */
4218  Local<String> property,
4219  const PropertyCallbackInfo<Boolean>& info);
4220 
4221 
4222 /**
4223  * Returns an array containing the names of the properties the named
4224  * property getter intercepts.
4225  */
4227  const PropertyCallbackInfo<Array>& info);
4228 
4229 
4230 // TODO(dcarney): Deprecate and remove previous typedefs, and replace
4231 // GenericNamedPropertyFooCallback with just NamedPropertyFooCallback.
4232 /**
4233  * GenericNamedProperty[Getter|Setter] are used as interceptors on object.
4234  * See ObjectTemplate::SetNamedPropertyHandler.
4235  */
4237  Local<Name> property, const PropertyCallbackInfo<Value>& info);
4238 
4239 
4240 /**
4241  * Returns the value if the setter intercepts the request.
4242  * Otherwise, returns an empty handle.
4243  */
4245  Local<Name> property, Local<Value> value,
4246  const PropertyCallbackInfo<Value>& info);
4247 
4248 
4249 /**
4250  * Returns a non-empty handle if the interceptor intercepts the request.
4251  * The result is an integer encoding property attributes (like v8::None,
4252  * v8::DontEnum, etc.)
4253  */
4255  Local<Name> property, const PropertyCallbackInfo<Integer>& info);
4256 
4257 
4258 /**
4259  * Returns a non-empty handle if the deleter intercepts the request.
4260  * The return value is true if the property could be deleted and false
4261  * otherwise.
4262  */
4264  Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
4265 
4266 
4267 /**
4268  * Returns an array containing the names of the properties the named
4269  * property getter intercepts.
4270  */
4272  const PropertyCallbackInfo<Array>& info);
4273 
4274 
4275 /**
4276  * Returns the value of the property if the getter intercepts the
4277  * request. Otherwise, returns an empty handle.
4278  */
4280  uint32_t index,
4281  const PropertyCallbackInfo<Value>& info);
4282 
4283 
4284 /**
4285  * Returns the value if the setter intercepts the request.
4286  * Otherwise, returns an empty handle.
4287  */
4289  uint32_t index,
4290  Local<Value> value,
4291  const PropertyCallbackInfo<Value>& info);
4292 
4293 
4294 /**
4295  * Returns a non-empty handle if the interceptor intercepts the request.
4296  * The result is an integer encoding property attributes.
4297  */
4299  uint32_t index,
4300  const PropertyCallbackInfo<Integer>& info);
4301 
4302 
4303 /**
4304  * Returns a non-empty handle if the deleter intercepts the request.
4305  * The return value is true if the property could be deleted and false
4306  * otherwise.
4307  */
4309  uint32_t index,
4310  const PropertyCallbackInfo<Boolean>& info);
4311 
4312 
4313 /**
4314  * Returns an array containing the indices of the properties the
4315  * indexed property getter intercepts.
4316  */
4318  const PropertyCallbackInfo<Array>& info);
4319 
4320 
4321 /**
4322  * Access type specification.
4323  */
4329  ACCESS_KEYS
4330 };
4331 
4332 
4333 /**
4334  * Returns true if the given context should be allowed to access the given
4335  * object.
4336  */
4337 typedef bool (*AccessCheckCallback)(Local<Context> accessing_context,
4338  Local<Object> accessed_object,
4339  Local<Value> data);
4340 typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context,
4341  Local<Object> accessed_object);
4342 
4343 /**
4344  * Returns true if cross-context access should be allowed to the named
4345  * property with the given key on the host object.
4346  */
4347 typedef bool (*NamedSecurityCallback)(Local<Object> host,
4348  Local<Value> key,
4349  AccessType type,
4350  Local<Value> data);
4351 
4352 
4353 /**
4354  * Returns true if cross-context access should be allowed to the indexed
4355  * property with the given index on the host object.
4356  */
4357 typedef bool (*IndexedSecurityCallback)(Local<Object> host,
4358  uint32_t index,
4359  AccessType type,
4360  Local<Value> data);
4361 
4362 
4363 /**
4364  * A FunctionTemplate is used to create functions at runtime. There
4365  * can only be one function created from a FunctionTemplate in a
4366  * context. The lifetime of the created function is equal to the
4367  * lifetime of the context. So in case the embedder needs to create
4368  * temporary functions that can be collected using Scripts is
4369  * preferred.
4370  *
4371  * Any modification of a FunctionTemplate after first instantiation will trigger
4372  *a crash.
4373  *
4374  * A FunctionTemplate can have properties, these properties are added to the
4375  * function object when it is created.
4376  *
4377  * A FunctionTemplate has a corresponding instance template which is
4378  * used to create object instances when the function is used as a
4379  * constructor. Properties added to the instance template are added to
4380  * each object instance.
4381  *
4382  * A FunctionTemplate can have a prototype template. The prototype template
4383  * is used to create the prototype object of the function.
4384  *
4385  * The following example shows how to use a FunctionTemplate:
4386  *
4387  * \code
4388  * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
4389  * t->Set("func_property", v8::Number::New(1));
4390  *
4391  * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
4392  * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
4393  * proto_t->Set("proto_const", v8::Number::New(2));
4394  *
4395  * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
4396  * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
4397  * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
4398  * instance_t->Set("instance_property", Number::New(3));
4399  *
4400  * v8::Local<v8::Function> function = t->GetFunction();
4401  * v8::Local<v8::Object> instance = function->NewInstance();
4402  * \endcode
4403  *
4404  * Let's use "function" as the JS variable name of the function object
4405  * and "instance" for the instance object created above. The function
4406  * and the instance will have the following properties:
4407  *
4408  * \code
4409  * func_property in function == true;
4410  * function.func_property == 1;
4411  *
4412  * function.prototype.proto_method() invokes 'InvokeCallback'
4413  * function.prototype.proto_const == 2;
4414  *
4415  * instance instanceof function == true;
4416  * instance.instance_accessor calls 'InstanceAccessorCallback'
4417  * instance.instance_property == 3;
4418  * \endcode
4419  *
4420  * A FunctionTemplate can inherit from another one by calling the
4421  * FunctionTemplate::Inherit method. The following graph illustrates
4422  * the semantics of inheritance:
4423  *
4424  * \code
4425  * FunctionTemplate Parent -> Parent() . prototype -> { }
4426  * ^ ^
4427  * | Inherit(Parent) | .__proto__
4428  * | |
4429  * FunctionTemplate Child -> Child() . prototype -> { }
4430  * \endcode
4431  *
4432  * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
4433  * object of the Child() function has __proto__ pointing to the
4434  * Parent() function's prototype object. An instance of the Child
4435  * function has all properties on Parent's instance templates.
4436  *
4437  * Let Parent be the FunctionTemplate initialized in the previous
4438  * section and create a Child FunctionTemplate by:
4439  *
4440  * \code
4441  * Local<FunctionTemplate> parent = t;
4442  * Local<FunctionTemplate> child = FunctionTemplate::New();
4443  * child->Inherit(parent);
4444  *
4445  * Local<Function> child_function = child->GetFunction();
4446  * Local<Object> child_instance = child_function->NewInstance();
4447  * \endcode
4448  *
4449  * The Child function and Child instance will have the following
4450  * properties:
4451  *
4452  * \code
4453  * child_func.prototype.__proto__ == function.prototype;
4454  * child_instance.instance_accessor calls 'InstanceAccessorCallback'
4455  * child_instance.instance_property == 3;
4456  * \endcode
4457  */
4459  public:
4460  /** Creates a function template.*/
4462  Isolate* isolate, FunctionCallback callback = 0,
4463  Local<Value> data = Local<Value>(),
4464  Local<Signature> signature = Local<Signature>(), int length = 0);
4465 
4466  /**
4467  * Creates a function template with a fast handler. If a fast handler is set,
4468  * the callback cannot be null.
4469  */
4471  Isolate* isolate, FunctionCallback callback,
4472  experimental::FastAccessorBuilder* fast_handler = nullptr,
4473  Local<Value> data = Local<Value>(),
4474  Local<Signature> signature = Local<Signature>(), int length = 0);
4475 
4476  /** Returns the unique function instance in the current execution context.*/
4477  V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction());
4479  Local<Context> context);
4480 
4481  /**
4482  * Set the call-handler callback for a FunctionTemplate. This
4483  * callback is called whenever the function created from this
4484  * FunctionTemplate is called.
4485  */
4487  FunctionCallback callback, Local<Value> data = Local<Value>(),
4488  experimental::FastAccessorBuilder* fast_handler = nullptr);
4489 
4490  /** Set the predefined length property for the FunctionTemplate. */
4491  void SetLength(int length);
4492 
4493  /** Get the InstanceTemplate. */
4495 
4496  /** Causes the function template to inherit from a parent function template.*/
4498 
4499  /**
4500  * A PrototypeTemplate is the template used to create the prototype object
4501  * of the function created by this template.
4502  */
4504 
4505  /**
4506  * Set the class name of the FunctionTemplate. This is used for
4507  * printing objects created with the function created from the
4508  * FunctionTemplate as its constructor.
4509  */
4511 
4512 
4513  /**
4514  * When set to true, no access check will be performed on the receiver of a
4515  * function call. Currently defaults to true, but this is subject to change.
4516  */
4517  void SetAcceptAnyReceiver(bool value);
4518 
4519  /**
4520  * Determines whether the __proto__ accessor ignores instances of
4521  * the function template. If instances of the function template are
4522  * ignored, __proto__ skips all instances and instead returns the
4523  * next object in the prototype chain.
4524  *
4525  * Call with a value of true to make the __proto__ accessor ignore
4526  * instances of the function template. Call with a value of false
4527  * to make the __proto__ accessor not ignore instances of the
4528  * function template. By default, instances of a function template
4529  * are not ignored.
4530  */
4531  void SetHiddenPrototype(bool value);
4532 
4533  /**
4534  * Sets the ReadOnly flag in the attributes of the 'prototype' property
4535  * of functions created from this FunctionTemplate to true.
4536  */
4538 
4539  /**
4540  * Removes the prototype property from functions created from this
4541  * FunctionTemplate.
4542  */
4544 
4545  /**
4546  * Returns true if the given object is an instance of this function
4547  * template.
4548  */
4549  bool HasInstance(Local<Value> object);
4550 
4551  private:
4552  FunctionTemplate();
4553  friend class Context;
4554  friend class ObjectTemplate;
4555 };
4556 
4557 
4559  kNone = 0,
4560  // See ALL_CAN_READ above.
4561  kAllCanRead = 1,
4562  // Will not call into interceptor for properties on the receiver or prototype
4563  // chain. Currently only valid for named interceptors.
4564  kNonMasking = 1 << 1,
4565  // Will not call into interceptor for symbol lookup. Only meaningful for
4566  // named interceptors.
4567  kOnlyInterceptStrings = 1 << 2,
4568 };
4569 
4570 
4573  /** Note: getter is required **/
4579  Local<Value> data = Local<Value>(),
4581  : getter(getter),
4582  setter(setter),
4583  query(query),
4584  deleter(deleter),
4585  enumerator(enumerator),
4586  data(data),
4587  flags(flags) {}
4588 
4596 };
4597 
4598 
4601  /** Note: getter is required **/
4602  IndexedPropertyGetterCallback getter = 0,
4603  IndexedPropertySetterCallback setter = 0,
4604  IndexedPropertyQueryCallback query = 0,
4605  IndexedPropertyDeleterCallback deleter = 0,
4606  IndexedPropertyEnumeratorCallback enumerator = 0,
4607  Local<Value> data = Local<Value>(),
4609  : getter(getter),
4610  setter(setter),
4611  query(query),
4612  deleter(deleter),
4613  enumerator(enumerator),
4614  data(data),
4615  flags(flags) {}
4616 
4624 };
4625 
4626 
4627 /**
4628  * An ObjectTemplate is used to create objects at runtime.
4629  *
4630  * Properties added to an ObjectTemplate are added to each object
4631  * created from the ObjectTemplate.
4632  */
4634  public:
4635  /** Creates an ObjectTemplate. */
4637  Isolate* isolate,
4639  static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New());
4640 
4641  /** Creates a new instance of this template.*/
4642  V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance());
4644 
4645  /**
4646  * Sets an accessor on the object template.
4647  *
4648  * Whenever the property with the given name is accessed on objects
4649  * created from this ObjectTemplate the getter and setter callbacks
4650  * are called instead of getting and setting the property directly
4651  * on the JavaScript object.
4652  *
4653  * \param name The name of the property for which an accessor is added.
4654  * \param getter The callback to invoke when getting the property.
4655  * \param setter The callback to invoke when setting the property.
4656  * \param data A piece of data that will be passed to the getter and setter
4657  * callbacks whenever they are invoked.
4658  * \param settings Access control settings for the accessor. This is a bit
4659  * field consisting of one of more of
4660  * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4661  * The default is to not allow cross-context access.
4662  * ALL_CAN_READ means that all cross-context reads are allowed.
4663  * ALL_CAN_WRITE means that all cross-context writes are allowed.
4664  * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4665  * cross-context access.
4666  * \param attribute The attributes of the property for which an accessor
4667  * is added.
4668  * \param signature The signature describes valid receivers for the accessor
4669  * and is used to perform implicit instance checks against them. If the
4670  * receiver is incompatible (i.e. is not an instance of the constructor as
4671  * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4672  * thrown and no callback is invoked.
4673  */
4675  Local<String> name, AccessorGetterCallback getter,
4676  AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4677  AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4680  Local<Name> name, AccessorNameGetterCallback getter,
4682  AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4684 
4685  /**
4686  * Sets a named property handler on the object template.
4687  *
4688  * Whenever a property whose name is a string is accessed on objects created
4689  * from this object template, the provided callback is invoked instead of
4690  * accessing the property directly on the JavaScript object.
4691  *
4692  * Note that new code should use the second version that can intercept
4693  * symbol-named properties as well as string-named properties.
4694  *
4695  * \param getter The callback to invoke when getting a property.
4696  * \param setter The callback to invoke when setting a property.
4697  * \param query The callback to invoke to check if a property is present,
4698  * and if present, get its attributes.
4699  * \param deleter The callback to invoke when deleting a property.
4700  * \param enumerator The callback to invoke to enumerate all the named
4701  * properties of an object.
4702  * \param data A piece of data that will be passed to the callbacks
4703  * whenever they are invoked.
4704  */
4705  // TODO(dcarney): deprecate
4707  NamedPropertySetterCallback setter = 0,
4708  NamedPropertyQueryCallback query = 0,
4709  NamedPropertyDeleterCallback deleter = 0,
4710  NamedPropertyEnumeratorCallback enumerator = 0,
4711  Local<Value> data = Local<Value>());
4712  void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
4713 
4714  /**
4715  * Sets an indexed property handler on the object template.
4716  *
4717  * Whenever an indexed property is accessed on objects created from
4718  * this object template, the provided callback is invoked instead of
4719  * accessing the property directly on the JavaScript object.
4720  *
4721  * \param getter The callback to invoke when getting a property.
4722  * \param setter The callback to invoke when setting a property.
4723  * \param query The callback to invoke to check if an object has a property.
4724  * \param deleter The callback to invoke when deleting a property.
4725  * \param enumerator The callback to invoke to enumerate all the indexed
4726  * properties of an object.
4727  * \param data A piece of data that will be passed to the callbacks
4728  * whenever they are invoked.
4729  */
4731  // TODO(dcarney): deprecate
4734  IndexedPropertySetterCallback setter = 0,
4735  IndexedPropertyQueryCallback query = 0,
4736  IndexedPropertyDeleterCallback deleter = 0,
4737  IndexedPropertyEnumeratorCallback enumerator = 0,
4738  Local<Value> data = Local<Value>()) {
4740  deleter, enumerator, data));
4741  }
4742  /**
4743  * Sets the callback to be used when calling instances created from
4744  * this template as a function. If no callback is set, instances
4745  * behave like normal JavaScript objects that cannot be called as a
4746  * function.
4747  */
4749  Local<Value> data = Local<Value>());
4750 
4751  /**
4752  * Mark object instances of the template as undetectable.
4753  *
4754  * In many ways, undetectable objects behave as though they are not
4755  * there. They behave like 'undefined' in conditionals and when
4756  * printed. However, properties can be accessed and called as on
4757  * normal objects.
4758  */
4760 
4761  /**
4762  * Sets access check callback on the object template and enables access
4763  * checks.
4764  *
4765  * When accessing properties on instances of this object template,
4766  * the access check callback will be called to determine whether or
4767  * not to allow cross-context access to the properties.
4768  */
4770  Local<Value> data = Local<Value>());
4772  "Use SetAccessCheckCallback with new AccessCheckCallback signature.",
4773  void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback,
4774  Local<Value> data = Local<Value>()));
4775 
4777  "Use SetAccessCheckCallback instead",
4778  void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
4779  IndexedSecurityCallback indexed_handler,
4780  Local<Value> data = Local<Value>()));
4781 
4782  /**
4783  * Gets the number of internal fields for objects generated from
4784  * this template.
4785  */
4787 
4788  /**
4789  * Sets the number of internal fields for objects generated from
4790  * this template.
4791  */
4792  void SetInternalFieldCount(int value);
4793 
4794  private:
4795  ObjectTemplate();
4796  static Local<ObjectTemplate> New(internal::Isolate* isolate,
4797  Local<FunctionTemplate> constructor);
4798  friend class FunctionTemplate;
4799 };
4800 
4801 
4802 /**
4803  * A Signature specifies which receiver is valid for a function.
4804  */
4805 class V8_EXPORT Signature : public Data {
4806  public:
4808  Isolate* isolate,
4810 
4811  private:
4812  Signature();
4813 };
4814 
4815 
4816 /**
4817  * An AccessorSignature specifies which receivers are valid parameters
4818  * to an accessor callback.
4819  */
4821  public:
4823  Isolate* isolate,
4825 
4826  private:
4827  AccessorSignature();
4828 };
4829 
4830 
4831 // --- Extensions ---
4832 
4835  public:
4836  ExternalOneByteStringResourceImpl() : data_(0), length_(0) {}
4837  ExternalOneByteStringResourceImpl(const char* data, size_t length)
4838  : data_(data), length_(length) {}
4839  const char* data() const { return data_; }
4840  size_t length() const { return length_; }
4841 
4842  private:
4843  const char* data_;
4844  size_t length_;
4845 };
4846 
4847 /**
4848  * Ignore
4849  */
4850 class V8_EXPORT Extension { // NOLINT
4851  public:
4852  // Note that the strings passed into this constructor must live as long
4853  // as the Extension itself.
4854  Extension(const char* name,
4855  const char* source = 0,
4856  int dep_count = 0,
4857  const char** deps = 0,
4858  int source_length = -1);
4859  virtual ~Extension() { }
4861  v8::Isolate* isolate, v8::Local<v8::String> name) {
4863  }
4864 
4865  const char* name() const { return name_; }
4866  size_t source_length() const { return source_length_; }
4868  return &source_; }
4869  int dependency_count() { return dep_count_; }
4870  const char** dependencies() { return deps_; }
4871  void set_auto_enable(bool value) { auto_enable_ = value; }
4872  bool auto_enable() { return auto_enable_; }
4873 
4874  private:
4875  const char* name_;
4876  size_t source_length_; // expected to initialize before source_
4878  int dep_count_;
4879  const char** deps_;
4880  bool auto_enable_;
4881 
4882  // Disallow copying and assigning.
4883  Extension(const Extension&);
4884  void operator=(const Extension&);
4885 };
4886 
4887 
4889 
4890 
4891 // --- Statics ---
4892 
4894 V8_INLINE Local<Primitive> Null(Isolate* isolate);
4895 V8_INLINE Local<Boolean> True(Isolate* isolate);
4896 V8_INLINE Local<Boolean> False(Isolate* isolate);
4897 
4898 /**
4899  * A set of constraints that specifies the limits of the runtime's memory use.
4900  * You must set the heap size before initializing the VM - the size cannot be
4901  * adjusted after the VM is initialized.
4902  *
4903  * If you are using threads then you should hold the V8::Locker lock while
4904  * setting the stack limit and you must set a non-default stack limit separately
4905  * for each thread.
4906  *
4907  * The arguments for set_max_semi_space_size, set_max_old_space_size,
4908  * set_max_executable_size, set_code_range_size specify limits in MB.
4909  */
4911  public:
4913 
4914  /**
4915  * Configures the constraints with reasonable default values based on the
4916  * capabilities of the current device the VM is running on.
4917  *
4918  * \param physical_memory The total amount of physical memory on the current
4919  * device, in bytes.
4920  * \param virtual_memory_limit The amount of virtual memory on the current
4921  * device, in bytes, or zero, if there is no limit.
4922  */
4923  void ConfigureDefaults(uint64_t physical_memory,
4924  uint64_t virtual_memory_limit);
4925 
4926  int max_semi_space_size() const { return max_semi_space_size_; }
4927  void set_max_semi_space_size(int limit_in_mb) {
4928  max_semi_space_size_ = limit_in_mb;
4929  }
4930  int max_old_space_size() const { return max_old_space_size_; }
4931  void set_max_old_space_size(int limit_in_mb) {
4932  max_old_space_size_ = limit_in_mb;
4933  }
4934  int max_executable_size() const { return max_executable_size_; }
4935  void set_max_executable_size(int limit_in_mb) {
4936  max_executable_size_ = limit_in_mb;
4937  }
4938  uint32_t* stack_limit() const { return stack_limit_; }
4939  // Sets an address beyond which the VM's stack may not grow.
4940  void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
4941  size_t code_range_size() const { return code_range_size_; }
4942  void set_code_range_size(size_t limit_in_mb) {
4943  code_range_size_ = limit_in_mb;
4944  }
4945 
4946  private:
4947  int max_semi_space_size_;
4948  int max_old_space_size_;
4949  int max_executable_size_;
4950  uint32_t* stack_limit_;
4951  size_t code_range_size_;
4952 };
4953 
4954 
4955 // --- Exceptions ---
4956 
4957 
4958 typedef void (*FatalErrorCallback)(const char* location, const char* message);
4959 
4960 
4961 typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
4962 
4963 // --- Tracing ---
4964 
4965 typedef void (*LogEventCallback)(const char* name, int event);
4966 
4967 /**
4968  * Create new error objects by calling the corresponding error object
4969  * constructor with the message.
4970  */
4972  public:
4973  static Local<Value> RangeError(Local<String> message);
4975  static Local<Value> SyntaxError(Local<String> message);
4976  static Local<Value> TypeError(Local<String> message);
4977  static Local<Value> Error(Local<String> message);
4978 
4979  /**
4980  * Creates an error message for the given exception.
4981  * Will try to reconstruct the original stack trace from the exception value,
4982  * or capture the current stack trace if not available.
4983  */
4984  static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
4985  V8_DEPRECATED("Use version with an Isolate*",
4986  static Local<Message> CreateMessage(Local<Value> exception));
4987 
4988  /**
4989  * Returns the original stack trace that was captured at the creation time
4990  * of a given exception, or an empty handle if not available.
4991  */
4993 };
4994 
4995 
4996 // --- Counters Callbacks ---
4997 
4998 typedef int* (*CounterLookupCallback)(const char* name);
4999 
5000 typedef void* (*CreateHistogramCallback)(const char* name,
5001  int min,
5002  int max,
5003  size_t buckets);
5004 
5005 typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
5006 
5007 // --- Memory Allocation Callback ---
5017 };
5018 
5023  };
5024 
5026  AllocationAction action,
5027  int size);
5028 
5029 // --- Enter/Leave Script Callback ---
5031 typedef void (*CallCompletedCallback)(Isolate*);
5033 
5034 // --- Promise Reject Callback ---
5038 };
5039 
5041  public:
5043  Local<Value> value, Local<StackTrace> stack_trace)
5044  : promise_(promise),
5045  event_(event),
5046  value_(value),
5047  stack_trace_(stack_trace) {}
5048 
5049  V8_INLINE Local<Promise> GetPromise() const { return promise_; }
5050  V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
5051  V8_INLINE Local<Value> GetValue() const { return value_; }
5052 
5053  V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()",
5054  V8_INLINE Local<StackTrace> GetStackTrace() const) {
5055  return stack_trace_;
5056  }
5057 
5058  private:
5059  Local<Promise> promise_;
5060  PromiseRejectEvent event_;
5061  Local<Value> value_;
5062  Local<StackTrace> stack_trace_;
5063 };
5064 
5066 
5067 // --- Microtasks Callbacks ---
5069 typedef void (*MicrotaskCallback)(void* data);
5070 
5071 
5072 /**
5073  * Policy for running microtasks:
5074  * - explicit: microtasks are invoked with Isolate::RunMicrotasks() method;
5075  * - scoped: microtasks invocation is controlled by MicrotasksScope objects;
5076  * - auto: microtasks are invoked when the script call depth decrements
5077  * to zero.
5078  */
5080 
5081 
5082 /**
5083  * This scope is used to control microtasks when kScopeMicrotasksInvocation
5084  * is used on Isolate. In this mode every non-primitive call to V8 should be
5085  * done inside some MicrotasksScope.
5086  * Microtasks are executed when topmost MicrotasksScope marked as kRunMicrotasks
5087  * exits.
5088  * kDoNotRunMicrotasks should be used to annotate calls not intended to trigger
5089  * microtasks.
5090  */
5092  public:
5094 
5095  MicrotasksScope(Isolate* isolate, Type type);
5097 
5098  /**
5099  * Runs microtasks if no kRunMicrotasks scope is currently active.
5100  */
5101  static void PerformCheckpoint(Isolate* isolate);
5102 
5103  /**
5104  * Returns current depth of nested kRunMicrotasks scopes.
5105  */
5106  static int GetCurrentDepth(Isolate* isolate);
5107 
5108  private:
5109  internal::Isolate* const isolate_;
5110  bool run_;
5111 
5112  // Prevent copying.
5113  MicrotasksScope(const MicrotasksScope&);
5114  MicrotasksScope& operator=(const MicrotasksScope&);
5115 };
5116 
5117 
5118 // --- Failed Access Check Callback ---
5119 typedef void (*FailedAccessCheckCallback)(Local<Object> target,
5120  AccessType type,
5121  Local<Value> data);
5122 
5123 // --- AllowCodeGenerationFromStrings callbacks ---
5124 
5125 /**
5126  * Callback to check if code generation from strings is allowed. See
5127  * Context::AllowCodeGenerationFromStrings.
5128  */
5130 
5131 // --- Garbage Collection Callbacks ---
5132 
5133 /**
5134  * Applications can register callback functions which will be called before and
5135  * after certain garbage collection operations. Allocations are not allowed in
5136  * the callback functions, you therefore cannot manipulate objects (set or
5137  * delete properties for example) since it is possible such operations will
5138  * result in the allocation of objects.
5139  */
5140 enum GCType {
5147 };
5148 
5149 /**
5150  * GCCallbackFlags is used to notify additional information about the GC
5151  * callback.
5152  * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
5153  * constructing retained object infos.
5154  * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
5155  * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
5156  * is called synchronously without getting posted to an idle task.
5157  * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
5158  * in a phase where V8 is trying to collect all available garbage
5159  * (e.g., handling a low memory notification).
5160  */
5167 };
5168 
5169 typedef void (*GCCallback)(GCType type, GCCallbackFlags flags);
5170 
5171 typedef void (*InterruptCallback)(Isolate* isolate, void* data);
5172 
5173 
5174 /**
5175  * Collection of V8 heap information.
5176  *
5177  * Instances of this class can be passed to v8::V8::HeapStatistics to
5178  * get heap statistics from V8.
5179  */
5181  public:
5183  size_t total_heap_size() { return total_heap_size_; }
5184  size_t total_heap_size_executable() { return total_heap_size_executable_; }
5185  size_t total_physical_size() { return total_physical_size_; }
5186  size_t total_available_size() { return total_available_size_; }
5187  size_t used_heap_size() { return used_heap_size_; }
5188  size_t heap_size_limit() { return heap_size_limit_; }
5189  size_t malloced_memory() { return malloced_memory_; }
5190  size_t does_zap_garbage() { return does_zap_garbage_; }
5191 
5192  private:
5193  size_t total_heap_size_;
5194  size_t total_heap_size_executable_;
5195  size_t total_physical_size_;
5196  size_t total_available_size_;
5197  size_t used_heap_size_;
5198  size_t heap_size_limit_;
5199  size_t malloced_memory_;
5200  bool does_zap_garbage_;
5201 
5202  friend class V8;
5203  friend class Isolate;
5204 };
5205 
5206 
5208  public:
5210  const char* space_name() { return space_name_; }
5211  size_t space_size() { return space_size_; }
5212  size_t space_used_size() { return space_used_size_; }
5213  size_t space_available_size() { return space_available_size_; }
5214  size_t physical_space_size() { return physical_space_size_; }
5215 
5216  private:
5217  const char* space_name_;
5218  size_t space_size_;
5219  size_t space_used_size_;
5220  size_t space_available_size_;
5221  size_t physical_space_size_;
5222 
5223  friend class Isolate;
5224 };
5225 
5226 
5228  public:
5230  const char* object_type() { return object_type_; }
5231  const char* object_sub_type() { return object_sub_type_; }
5232  size_t object_count() { return object_count_; }
5233  size_t object_size() { return object_size_; }
5234 
5235  private:
5236  const char* object_type_;
5237  const char* object_sub_type_;
5238  size_t object_count_;
5239  size_t object_size_;
5240 
5241  friend class Isolate;
5242 };
5243 
5244 
5245 class RetainedObjectInfo;
5246 
5247 
5248 /**
5249  * FunctionEntryHook is the type of the profile entry hook called at entry to
5250  * any generated function when function-level profiling is enabled.
5251  *
5252  * \param function the address of the function that's being entered.
5253  * \param return_addr_location points to a location on stack where the machine
5254  * return address resides. This can be used to identify the caller of
5255  * \p function, and/or modified to divert execution when \p function exits.
5256  *
5257  * \note the entry hook must not cause garbage collection.
5258  */
5259 typedef void (*FunctionEntryHook)(uintptr_t function,
5260  uintptr_t return_addr_location);
5261 
5262 /**
5263  * A JIT code event is issued each time code is added, moved or removed.
5264  *
5265  * \note removal events are not currently issued.
5266  */
5268  enum EventType {
5275  };
5276  // Definition of the code position type. The "POSITION" type means the place
5277  // in the source code which are of interest when making stack traces to
5278  // pin-point the source location of a stack frame as close as possible.
5279  // The "STATEMENT_POSITION" means the place at the beginning of each
5280  // statement, and is used to indicate possible break locations.
5282 
5283  // Type of event.
5285  // Start of the instructions.
5286  void* code_start;
5287  // Size of the instructions.
5288  size_t code_len;
5289  // Script info for CODE_ADDED event.
5291  // User-defined data for *_LINE_INFO_* event. It's used to hold the source
5292  // code line information which is returned from the
5293  // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
5294  // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
5295  void* user_data;
5296 
5297  struct name_t {
5298  // Name of the object associated with the code, note that the string is not
5299  // zero-terminated.
5300  const char* str;
5301  // Number of chars in str.
5302  size_t len;
5303  };
5304 
5305  struct line_info_t {
5306  // PC offset
5307  size_t offset;
5308  // Code postion
5309  size_t pos;
5310  // The position type.
5312  };
5313 
5314  union {
5315  // Only valid for CODE_ADDED.
5316  struct name_t name;
5317 
5318  // Only valid for CODE_ADD_LINE_POS_INFO
5319  struct line_info_t line_info;
5320 
5321  // New location of instructions. Only valid for CODE_MOVED.
5323  };
5324 };
5325 
5326 /**
5327  * Option flags passed to the SetJitCodeEventHandler function.
5328  */
5331  // Generate callbacks for already existent code.
5333 };
5334 
5335 
5336 /**
5337  * Callback function passed to SetJitCodeEventHandler.
5338  *
5339  * \param event code add, move or removal event.
5340  */
5341 typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
5342 
5343 
5344 /**
5345  * Interface for iterating through all external resources in the heap.
5346  */
5348  public:
5350  virtual void VisitExternalString(Local<String> string) {}
5351 };
5352 
5353 
5354 /**
5355  * Interface for iterating through all the persistent handles in the heap.
5356  */
5358  public:
5361  uint16_t class_id) {}
5362 };
5363 
5364 /**
5365  * Memory pressure level for the MemoryPressureNotification.
5366  * kNone hints V8 that there is no memory pressure.
5367  * kModerate hints V8 to speed up incremental garbage collection at the cost of
5368  * of higher latency due to garbage collection pauses.
5369  * kCritical hints V8 to free memory as soon as possible. Garbage collection
5370  * pauses at this level will be large.
5371  */
5373 
5374 /**
5375  * Interface for tracing through the embedder heap. During the v8 garbage
5376  * collection, v8 collects hidden fields of all potential wrappers, and at the
5377  * end of its marking phase iterates the collection and asks the embedder to
5378  * trace through its heap and call PersistentBase::RegisterExternalReference on
5379  * each js object reachable from any of the given wrappers.
5380  *
5381  * Before the first call to the TraceWrappableFrom function v8 will call
5382  * TraceRoots. When the v8 garbage collection is finished, v8 will call
5383  * ClearTracingMarks.
5384  */
5386  public:
5387  /**
5388  * V8 will call this method at the beginning of the gc cycle.
5389  */
5390  virtual void TraceRoots(Isolate* isolate) = 0;
5391 
5392  /**
5393  * V8 will call this method with internal fields of a potential wrappers.
5394  * Embedder is expected to trace its heap (synchronously) and call
5395  * PersistentBase::RegisterExternalReference() on all wrappers reachable from
5396  * any of the given wrappers.
5397  */
5398  virtual void TraceWrappableFrom(
5399  Isolate* isolate,
5400  const std::vector<std::pair<void*, void*> >& internal_fields) = 0;
5401  /**
5402  * V8 will call this method at the end of the gc cycle. Allocation is *not*
5403  * allowed in the ClearTracingMarks.
5404  */
5405  virtual void ClearTracingMarks(Isolate* isolate) = 0;
5406 
5407  protected:
5408  virtual ~EmbedderHeapTracer() = default;
5409 };
5410 
5411 /**
5412  * Isolate represents an isolated instance of the V8 engine. V8 isolates have
5413  * completely separate states. Objects from one isolate must not be used in
5414  * other isolates. The embedder can create multiple isolates and use them in
5415  * parallel in multiple threads. An isolate can be entered by at most one
5416  * thread at any given time. The Locker/Unlocker API must be used to
5417  * synchronize.
5418  */
5420  public:
5421  /**
5422  * Initial configuration parameters for a new Isolate.
5423  */
5424  struct CreateParams {
5426  : entry_hook(NULL),
5427  code_event_handler(NULL),
5428  snapshot_blob(NULL),
5432  array_buffer_allocator(NULL) {}
5433 
5434  /**
5435  * The optional entry_hook allows the host application to provide the
5436  * address of a function that's invoked on entry to every V8-generated
5437  * function. Note that entry_hook is invoked at the very start of each
5438  * generated function. Furthermore, if an entry_hook is given, V8 will
5439  * always run without a context snapshot.
5440  */
5442 
5443  /**
5444  * Allows the host application to provide the address of a function that is
5445  * notified each time code is added, moved or removed.
5446  */
5448 
5449  /**
5450  * ResourceConstraints to use for the new Isolate.
5451  */
5453 
5454  /**
5455  * Explicitly specify a startup snapshot blob. The embedder owns the blob.
5456  */
5458 
5459 
5460  /**
5461  * Enables the host application to provide a mechanism for recording
5462  * statistics counters.
5463  */
5465 
5466  /**
5467  * Enables the host application to provide a mechanism for recording
5468  * histograms. The CreateHistogram function returns a
5469  * histogram which will later be passed to the AddHistogramSample
5470  * function.
5471  */
5474 
5475  /**
5476  * The ArrayBuffer::Allocator to use for allocating and freeing the backing
5477  * store of ArrayBuffers.
5478  */
5480  };
5481 
5482 
5483  /**
5484  * Stack-allocated class which sets the isolate for all operations
5485  * executed within a local scope.
5486  */
5488  public:
5489  explicit Scope(Isolate* isolate) : isolate_(isolate) {
5490  isolate->Enter();
5491  }
5492 
5493  ~Scope() { isolate_->Exit(); }
5494 
5495  private:
5496  Isolate* const isolate_;
5497 
5498  // Prevent copying of Scope objects.
5499  Scope(const Scope&);
5500  Scope& operator=(const Scope&);
5501  };
5502 
5503 
5504  /**
5505  * Assert that no Javascript code is invoked.
5506  */
5508  public:
5510 
5513 
5514  private:
5515  bool on_failure_;
5516  void* internal_;
5517 
5518  // Prevent copying of Scope objects.
5519  DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&);
5522  };
5523 
5524 
5525  /**
5526  * Introduce exception to DisallowJavascriptExecutionScope.
5527  */
5529  public:
5532 
5533  private:
5534  void* internal_throws_;
5535  void* internal_assert_;
5536 
5537  // Prevent copying of Scope objects.
5538  AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&);
5539  AllowJavascriptExecutionScope& operator=(
5541  };
5542 
5543  /**
5544  * Do not run microtasks while this scope is active, even if microtasks are
5545  * automatically executed otherwise.
5546  */
5548  public:
5551 
5552  private:
5553  internal::Isolate* isolate_;
5554 
5555  // Prevent copying of Scope objects.
5556  SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
5559  };
5560 
5561  /**
5562  * Types of garbage collections that can be requested via
5563  * RequestGarbageCollectionForTesting.
5564  */
5568  };
5569 
5570  /**
5571  * Features reported via the SetUseCounterCallback callback. Do not change
5572  * assigned numbers of existing items; add new features to the end of this
5573  * list.
5574  */
5576  kUseAsm = 0,
5608 
5609  // If you add new values here, you'll also need to update V8Initializer.cpp
5610  // in Chromium.
5611  kUseCounterFeatureCount // This enum value must be last.
5612  };
5613 
5614  typedef void (*UseCounterCallback)(Isolate* isolate,
5615  UseCounterFeature feature);
5616 
5617 
5618  /**
5619  * Creates a new isolate. Does not change the currently entered
5620  * isolate.
5621  *
5622  * When an isolate is no longer used its resources should be freed
5623  * by calling Dispose(). Using the delete operator is not allowed.
5624  *
5625  * V8::Initialize() must have run prior to this.
5626  */
5627  static Isolate* New(const CreateParams& params);
5628 
5629  /**
5630  * Returns the entered isolate for the current thread or NULL in
5631  * case there is no current isolate.
5632  *
5633  * This method must not be invoked before V8::Initialize() was invoked.
5634  */
5635  static Isolate* GetCurrent();
5636 
5637  /**
5638  * Custom callback used by embedders to help V8 determine if it should abort
5639  * when it throws and no internal handler is predicted to catch the
5640  * exception. If --abort-on-uncaught-exception is used on the command line,
5641  * then V8 will abort if either:
5642  * - no custom callback is set.
5643  * - the custom callback set returns true.
5644  * Otherwise, the custom callback will not be called and V8 will not abort.
5645  */
5649 
5650  /**
5651  * Optional notification that the system is running low on memory.
5652  * V8 uses these notifications to guide heuristics.
5653  * It is allowed to call this function from another thread while
5654  * the isolate is executing long running JavaScript code.
5655  */
5657 
5658  /**
5659  * Methods below this point require holding a lock (using Locker) in
5660  * a multi-threaded environment.
5661  */
5662 
5663  /**
5664  * Sets this isolate as the entered one for the current thread.
5665  * Saves the previously entered one (if any), so that it can be
5666  * restored when exiting. Re-entering an isolate is allowed.
5667  */
5668  void Enter();
5669 
5670  /**
5671  * Exits this isolate by restoring the previously entered one in the
5672  * current thread. The isolate may still stay the same, if it was
5673  * entered more than once.
5674  *
5675  * Requires: this == Isolate::GetCurrent().
5676  */
5677  void Exit();
5678 
5679  /**
5680  * Disposes the isolate. The isolate must not be entered by any
5681  * thread to be disposable.
5682  */
5683  void Dispose();
5684 
5685  /**
5686  * Discards all V8 thread-specific data for the Isolate. Should be used
5687  * if a thread is terminating and it has used an Isolate that will outlive
5688  * the thread -- all thread-specific data for an Isolate is discarded when
5689  * an Isolate is disposed so this call is pointless if an Isolate is about
5690  * to be Disposed.
5691  */
5693 
5694  /**
5695  * Associate embedder-specific data with the isolate. |slot| has to be
5696  * between 0 and GetNumberOfDataSlots() - 1.
5697  */
5698  V8_INLINE void SetData(uint32_t slot, void* data);
5699 
5700  /**
5701  * Retrieve embedder-specific data from the isolate.
5702  * Returns NULL if SetData has never been called for the given |slot|.
5703  */
5704  V8_INLINE void* GetData(uint32_t slot);
5705 
5706  /**
5707  * Returns the maximum number of available embedder data slots. Valid slots
5708  * are in the range of 0 - GetNumberOfDataSlots() - 1.
5709  */
5710  V8_INLINE static uint32_t GetNumberOfDataSlots();
5711 
5712  /**
5713  * Get statistics about the heap memory usage.
5714  */
5715  void GetHeapStatistics(HeapStatistics* heap_statistics);
5716 
5717  /**
5718  * Returns the number of spaces in the heap.
5719  */
5721 
5722  /**
5723  * Get the memory usage of a space in the heap.
5724  *
5725  * \param space_statistics The HeapSpaceStatistics object to fill in
5726  * statistics.
5727  * \param index The index of the space to get statistics from, which ranges
5728  * from 0 to NumberOfHeapSpaces() - 1.
5729  * \returns true on success.
5730  */
5732  size_t index);
5733 
5734  /**
5735  * Returns the number of types of objects tracked in the heap at GC.
5736  */
5738 
5739  /**
5740  * Get statistics about objects in the heap.
5741  *
5742  * \param object_statistics The HeapObjectStatistics object to fill in
5743  * statistics of objects of given type, which were live in the previous GC.
5744  * \param type_index The index of the type of object to fill details about,
5745  * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1.
5746  * \returns true on success.
5747  */
5749  size_t type_index);
5750 
5751  /**
5752  * Get a call stack sample from the isolate.
5753  * \param state Execution state.
5754  * \param frames Caller allocated buffer to store stack frames.
5755  * \param frames_limit Maximum number of frames to capture. The buffer must
5756  * be large enough to hold the number of frames.
5757  * \param sample_info The sample info is filled up by the function
5758  * provides number of actual captured stack frames and
5759  * the current VM state.
5760  * \note GetStackSample should only be called when the JS thread is paused or
5761  * interrupted. Otherwise the behavior is undefined.
5762  */
5763  void GetStackSample(const RegisterState& state, void** frames,
5764  size_t frames_limit, SampleInfo* sample_info);
5765 
5766  /**
5767  * Adjusts the amount of registered external memory. Used to give V8 an
5768  * indication of the amount of externally allocated memory that is kept alive
5769  * by JavaScript objects. V8 uses this to decide when to perform global
5770  * garbage collections. Registering externally allocated memory will trigger
5771  * global garbage collections more often than it would otherwise in an attempt
5772  * to garbage collect the JavaScript objects that keep the externally
5773  * allocated memory alive.
5774  *
5775  * \param change_in_bytes the change in externally allocated memory that is
5776  * kept alive by JavaScript objects.
5777  * \returns the adjusted value.
5778  */
5779  V8_INLINE int64_t
5780  AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
5781 
5782  /**
5783  * Returns heap profiler for this isolate. Will return NULL until the isolate
5784  * is initialized.
5785  */
5787 
5788  /**
5789  * Returns CPU profiler for this isolate. Will return NULL unless the isolate
5790  * is initialized. It is the embedder's responsibility to stop all CPU
5791  * profiling activities if it has started any.
5792  */
5794 
5795  /** Returns true if this isolate has a current context. */
5796  bool InContext();
5797 
5798  /**
5799  * Returns the context of the currently running JavaScript, or the context
5800  * on the top of the stack if no JavaScript is running.
5801  */
5803 
5804  /**
5805  * Returns the context of the calling JavaScript code. That is the
5806  * context of the top-most JavaScript frame. If there are no
5807  * JavaScript frames an empty handle is returned.
5808  */
5810  "Calling context concept is not compatible with tail calls, and will be "
5811  "removed.",
5812  Local<Context> GetCallingContext());
5813 
5814  /** Returns the last context entered through V8's C++ API. */
5816 
5817  /**
5818  * Schedules an exception to be thrown when returning to JavaScript. When an
5819  * exception has been scheduled it is illegal to invoke any JavaScript
5820  * operation; the caller must return immediately and only after the exception
5821  * has been handled does it become legal to invoke JavaScript operations.
5822  */
5824 
5825  /**
5826  * Allows the host application to group objects together. If one
5827  * object in the group is alive, all objects in the group are alive.
5828  * After each garbage collection, object groups are removed. It is
5829  * intended to be used in the before-garbage-collection callback
5830  * function, for instance to simulate DOM tree connections among JS
5831  * wrapper objects. Object groups for all dependent handles need to
5832  * be provided for kGCTypeMarkSweepCompact collections, for all other
5833  * garbage collection types it is sufficient to provide object groups
5834  * for partially dependent handles only.
5835  */
5836  template<typename T> void SetObjectGroupId(const Persistent<T>& object,
5837  UniqueId id);
5838 
5839  /**
5840  * Allows the host application to declare implicit references from an object
5841  * group to an object. If the objects of the object group are alive, the child
5842  * object is alive too. After each garbage collection, all implicit references
5843  * are removed. It is intended to be used in the before-garbage-collection
5844  * callback function.
5845  */
5846  template<typename T> void SetReferenceFromGroup(UniqueId id,
5847  const Persistent<T>& child);
5848 
5849  /**
5850  * Allows the host application to declare implicit references from an object
5851  * to another object. If the parent object is alive, the child object is alive
5852  * too. After each garbage collection, all implicit references are removed. It
5853  * is intended to be used in the before-garbage-collection callback function.
5854  */
5855  template<typename T, typename S>
5856  void SetReference(const Persistent<T>& parent, const Persistent<S>& child);
5857 
5858  typedef void (*GCCallback)(Isolate* isolate, GCType type,
5859  GCCallbackFlags flags);
5860 
5861  /**
5862  * Enables the host application to receive a notification before a
5863  * garbage collection. Allocations are allowed in the callback function,
5864  * but the callback is not re-entrant: if the allocation inside it will
5865  * trigger the garbage collection, the callback won't be called again.
5866  * It is possible to specify the GCType filter for your callback. But it is
5867  * not possible to register the same callback function two times with
5868  * different GCType filters.
5869  */
5871  GCType gc_type_filter = kGCTypeAll);
5872 
5873  /**
5874  * This function removes callback which was installed by
5875  * AddGCPrologueCallback function.
5876  */
5878 
5879  /**
5880  * Sets the embedder heap tracer for the isolate.
5881  */
5883 
5884  /**
5885  * Enables the host application to receive a notification after a
5886  * garbage collection. Allocations are allowed in the callback function,
5887  * but the callback is not re-entrant: if the allocation inside it will
5888  * trigger the garbage collection, the callback won't be called again.
5889  * It is possible to specify the GCType filter for your callback. But it is
5890  * not possible to register the same callback function two times with
5891  * different GCType filters.
5892  */
5894  GCType gc_type_filter = kGCTypeAll);
5895 
5896  /**
5897  * This function removes callback which was installed by
5898  * AddGCEpilogueCallback function.
5899  */
5901 
5902  /**
5903  * Forcefully terminate the current thread of JavaScript execution
5904  * in the given isolate.
5905  *
5906  * This method can be used by any thread even if that thread has not
5907  * acquired the V8 lock with a Locker object.
5908  */
5910 
5911  /**
5912  * Is V8 terminating JavaScript execution.
5913  *
5914  * Returns true if JavaScript execution is currently terminating
5915  * because of a call to TerminateExecution. In that case there are
5916  * still JavaScript frames on the stack and the termination
5917  * exception is still active.
5918  */
5920 
5921  /**
5922  * Resume execution capability in the given isolate, whose execution
5923  * was previously forcefully terminated using TerminateExecution().
5924  *
5925  * When execution is forcefully terminated using TerminateExecution(),
5926  * the isolate can not resume execution until all JavaScript frames
5927  * have propagated the uncatchable exception which is generated. This
5928  * method allows the program embedding the engine to handle the
5929  * termination event and resume execution capability, even if
5930  * JavaScript frames remain on the stack.
5931  *
5932  * This method can be used by any thread even if that thread has not
5933  * acquired the V8 lock with a Locker object.
5934  */
5936 
5937  /**
5938  * Request V8 to interrupt long running JavaScript code and invoke
5939  * the given |callback| passing the given |data| to it. After |callback|
5940  * returns control will be returned to the JavaScript code.
5941  * There may be a number of interrupt requests in flight.
5942  * Can be called from another thread without acquiring a |Locker|.
5943  * Registered |callback| must not reenter interrupted Isolate.
5944  */
5945  void RequestInterrupt(InterruptCallback callback, void* data);
5946 
5947  /**
5948  * Request garbage collection in this Isolate. It is only valid to call this
5949  * function if --expose_gc was specified.
5950  *
5951  * This should only be used for testing purposes and not to enforce a garbage
5952  * collection schedule. It has strong negative impact on the garbage
5953  * collection performance. Use IdleNotificationDeadline() or
5954  * LowMemoryNotification() instead to influence the garbage collection
5955  * schedule.
5956  */
5958 
5959  /**
5960  * Set the callback to invoke for logging event.
5961  */
5963 
5964  /**
5965  * Adds a callback to notify the host application right before a script
5966  * is about to run. If a script re-enters the runtime during executing, the
5967  * BeforeCallEnteredCallback is invoked for each re-entrance.
5968  * Executing scripts inside the callback will re-trigger the callback.
5969  */
5971 
5972  /**
5973  * Removes callback that was installed by AddBeforeCallEnteredCallback.
5974  */
5976 
5977  /**
5978  * Adds a callback to notify the host application when a script finished
5979  * running. If a script re-enters the runtime during executing, the
5980  * CallCompletedCallback is only invoked when the outer-most script
5981  * execution ends. Executing scripts inside the callback do not trigger
5982  * further callbacks.
5983  */
5986  "Use callback with parameter",
5987  void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback));
5988 
5989  /**
5990  * Removes callback that was installed by AddCallCompletedCallback.
5991  */
5994  "Use callback with parameter",
5995  void RemoveCallCompletedCallback(
5997 
5998  /**
5999  * Set callback to notify about promise reject with no handler, or
6000  * revocation of such a previous notification once the handler is added.
6001  */
6003 
6004  /**
6005  * Experimental: Runs the Microtask Work Queue until empty
6006  * Any exceptions thrown by microtask callbacks are swallowed.
6007  */
6009 
6010  /**
6011  * Experimental: Enqueues the callback to the Microtask Work Queue
6012  */
6013  void EnqueueMicrotask(Local<Function> microtask);
6014 
6015  /**
6016  * Experimental: Enqueues the callback to the Microtask Work Queue
6017  */
6018  void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL);
6019 
6020  /**
6021  * Experimental: Controls how Microtasks are invoked. See MicrotasksPolicy
6022  * for details.
6023  */
6025  V8_DEPRECATE_SOON("Use SetMicrotasksPolicy",
6026  void SetAutorunMicrotasks(bool autorun));
6027 
6028  /**
6029  * Experimental: Returns the policy controlling how Microtasks are invoked.
6030  */
6032  V8_DEPRECATE_SOON("Use GetMicrotasksPolicy",
6033  bool WillAutorunMicrotasks() const);
6034 
6035  /**
6036  * Experimental: adds a callback to notify the host application after
6037  * microtasks were run. The callback is triggered by explicit RunMicrotasks
6038  * call or automatic microtasks execution (see SetAutorunMicrotasks).
6039  *
6040  * Callback will trigger even if microtasks were attempted to run,
6041  * but the microtasks queue was empty and no single microtask was actually
6042  * executed.
6043  *
6044  * Executing scriptsinside the callback will not re-trigger microtasks and
6045  * the callback.
6046  */
6048 
6049  /**
6050  * Removes callback that was installed by AddMicrotasksCompletedCallback.
6051  */
6053 
6054  /**
6055  * Sets a callback for counting the number of times a feature of V8 is used.
6056  */
6058 
6059  /**
6060  * Enables the host application to provide a mechanism for recording
6061  * statistics counters.
6062  */
6064 
6065  /**
6066  * Enables the host application to provide a mechanism for recording
6067  * histograms. The CreateHistogram function returns a
6068  * histogram which will later be passed to the AddHistogramSample
6069  * function.
6070  */
6073 
6074  /**
6075  * Optional notification that the embedder is idle.
6076  * V8 uses the notification to perform garbage collection.
6077  * This call can be used repeatedly if the embedder remains idle.
6078  * Returns true if the embedder should stop calling IdleNotificationDeadline
6079  * until real work has been done. This indicates that V8 has done
6080  * as much cleanup as it will be able to do.
6081  *
6082  * The deadline_in_seconds argument specifies the deadline V8 has to finish
6083  * garbage collection work. deadline_in_seconds is compared with
6084  * MonotonicallyIncreasingTime() and should be based on the same timebase as
6085  * that function. There is no guarantee that the actual work will be done
6086  * within the time limit.
6087  */
6088  bool IdleNotificationDeadline(double deadline_in_seconds);
6089 
6090  V8_DEPRECATED("use IdleNotificationDeadline()",
6091  bool IdleNotification(int idle_time_in_ms));
6092 
6093  /**
6094  * Optional notification that the system is running low on memory.
6095  * V8 uses these notifications to attempt to free memory.
6096  */
6098 
6099  /**
6100  * Optional notification that a context has been disposed. V8 uses
6101  * these notifications to guide the GC heuristic. Returns the number
6102  * of context disposals - including this one - since the last time
6103  * V8 had a chance to clean up.
6104  *
6105  * The optional parameter |dependant_context| specifies whether the disposed
6106  * context was depending on state from other contexts or not.
6107  */
6108  int ContextDisposedNotification(bool dependant_context = true);
6109 
6110  /**
6111  * Optional notification that the isolate switched to the foreground.
6112  * V8 uses these notifications to guide heuristics.
6113  */
6115 
6116  /**
6117  * Optional notification that the isolate switched to the background.
6118  * V8 uses these notifications to guide heuristics.
6119  */
6121 
6122  /**
6123  * Allows the host application to provide the address of a function that is
6124  * notified each time code is added, moved or removed.
6125  *
6126  * \param options options for the JIT code event handler.
6127  * \param event_handler the JIT code event handler, which will be invoked
6128  * each time code is added, moved or removed.
6129  * \note \p event_handler won't get notified of existent code.
6130  * \note since code removal notifications are not currently issued, the
6131  * \p event_handler may get notifications of code that overlaps earlier
6132  * code notifications. This happens when code areas are reused, and the
6133  * earlier overlapping code areas should therefore be discarded.
6134  * \note the events passed to \p event_handler and the strings they point to
6135  * are not guaranteed to live past each call. The \p event_handler must
6136  * copy strings and other parameters it needs to keep around.
6137  * \note the set of events declared in JitCodeEvent::EventType is expected to
6138  * grow over time, and the JitCodeEvent structure is expected to accrue
6139  * new members. The \p event_handler function must ignore event codes
6140  * it does not recognize to maintain future compatibility.
6141  * \note Use Isolate::CreateParams to get events for code executed during
6142  * Isolate setup.
6143  */
6145  JitCodeEventHandler event_handler);
6146 
6147  /**
6148  * Modifies the stack limit for this Isolate.
6149  *
6150  * \param stack_limit An address beyond which the Vm's stack may not grow.
6151  *
6152  * \note If you are using threads then you should hold the V8::Locker lock
6153  * while setting the stack limit and you must set a non-default stack
6154  * limit separately for each thread.
6155  */
6156  void SetStackLimit(uintptr_t stack_limit);
6157 
6158  /**
6159  * Returns a memory range that can potentially contain jitted code.
6160  *
6161  * On Win64, embedders are advised to install function table callbacks for
6162  * these ranges, as default SEH won't be able to unwind through jitted code.
6163  *
6164  * The first page of the code range is reserved for the embedder and is
6165  * committed, writable, and executable.
6166  *
6167  * Might be empty on other platforms.
6168  *
6169  * https://code.google.com/p/v8/issues/detail?id=3598
6170  */
6171  void GetCodeRange(void** start, size_t* length_in_bytes);
6172 
6173  /** Set the callback to invoke in case of fatal errors. */
6175 
6176  /**
6177  * Set the callback to invoke to check if code generation from
6178  * strings should be allowed.
6179  */
6182 
6183  /**
6184  * Check if V8 is dead and therefore unusable. This is the case after
6185  * fatal errors such as out-of-memory situations.
6186  */
6187  bool IsDead();
6188 
6189  /**
6190  * Adds a message listener.
6191  *
6192  * The same message listener can be added more than once and in that
6193  * case it will be called more than once for each message.
6194  *
6195  * If data is specified, it will be passed to the callback when it is called.
6196  * Otherwise, the exception object will be passed to the callback instead.
6197  */
6199  Local<Value> data = Local<Value>());
6200 
6201  /**
6202  * Remove all message listeners from the specified callback function.
6203  */
6205 
6206  /** Callback function for reporting failed access checks.*/
6208 
6209  /**
6210  * Tells V8 to capture current stack trace when uncaught exception occurs
6211  * and report it to the message listeners. The option is off by default.
6212  */
6214  bool capture, int frame_limit = 10,
6216 
6217  /**
6218  * Enables the host application to provide a mechanism to be notified
6219  * and perform custom logging when V8 Allocates Executable Memory.
6220  */
6222  ObjectSpace space, AllocationAction action);
6223 
6224  /**
6225  * Removes callback that was installed by AddMemoryAllocationCallback.
6226  */
6228 
6229  /**
6230  * Iterates through all external resources referenced from current isolate
6231  * heap. GC is not invoked prior to iterating, therefore there is no
6232  * guarantee that visited objects are still alive.
6233  */
6235 
6236  /**
6237  * Iterates through all the persistent handles in the current isolate's heap
6238  * that have class_ids.
6239  */
6241 
6242  /**
6243  * Iterates through all the persistent handles in the current isolate's heap
6244  * that have class_ids and are candidates to be marked as partially dependent
6245  * handles. This will visit handles to young objects created since the last
6246  * garbage collection but is free to visit an arbitrary superset of these
6247  * objects.
6248  */
6250 
6251  /**
6252  * Iterates through all the persistent handles in the current isolate's heap
6253  * that have class_ids and are weak to be marked as inactive if there is no
6254  * pending activity for the handle.
6255  */
6257 
6258  private:
6259  template <class K, class V, class Traits>
6261 
6262  Isolate();
6263  Isolate(const Isolate&);
6264  ~Isolate();
6265  Isolate& operator=(const Isolate&);
6266  void* operator new(size_t size);
6267  void operator delete(void*, size_t);
6268 
6269  void SetObjectGroupId(internal::Object** object, UniqueId id);
6270  void SetReferenceFromGroup(UniqueId id, internal::Object** object);
6271  void SetReference(internal::Object** parent, internal::Object** child);
6272  void ReportExternalAllocationLimitReached();
6273 };
6274 
6276  public:
6277  const char* data;
6279 };
6280 
6281 
6282 /**
6283  * EntropySource is used as a callback function when v8 needs a source
6284  * of entropy.
6285  */
6286 typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
6287 
6288 
6289 /**
6290  * ReturnAddressLocationResolver is used as a callback function when v8 is
6291  * resolving the location of a return address on the stack. Profilers that
6292  * change the return address on the stack can use this to resolve the stack
6293  * location to whereever the profiler stashed the original return address.
6294  *
6295  * \param return_addr_location points to a location on stack where a machine
6296  * return address resides.
6297  * \returns either return_addr_location, or else a pointer to the profiler's
6298  * copy of the original return address.
6299  *
6300  * \note the resolver function must not cause garbage collection.
6301  */
6302 typedef uintptr_t (*ReturnAddressLocationResolver)(
6303  uintptr_t return_addr_location);
6304 
6305 
6306 /**
6307  * Container class for static utility functions.
6308  */
6309 class V8_EXPORT V8 {
6310  public:
6311  /** Set the callback to invoke in case of fatal errors. */
6313  "Use isolate version",
6314  void SetFatalErrorHandler(FatalErrorCallback that));
6315 
6316  /**
6317  * Set the callback to invoke to check if code generation from
6318  * strings should be allowed.
6319  */
6321  "Use isolate version", void SetAllowCodeGenerationFromStringsCallback(
6323 
6324  /**
6325  * Check if V8 is dead and therefore unusable. This is the case after
6326  * fatal errors such as out-of-memory situations.
6327  */
6328  V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead());
6329 
6330  /**
6331  * Hand startup data to V8, in case the embedder has chosen to build
6332  * V8 with external startup data.
6333  *
6334  * Note:
6335  * - By default the startup data is linked into the V8 library, in which
6336  * case this function is not meaningful.
6337  * - If this needs to be called, it needs to be called before V8
6338  * tries to make use of its built-ins.
6339  * - To avoid unnecessary copies of data, V8 will point directly into the
6340  * given data blob, so pretty please keep it around until V8 exit.
6341  * - Compression of the startup blob might be useful, but needs to
6342  * handled entirely on the embedders' side.
6343  * - The call will abort if the data is invalid.
6344  */
6345  static void SetNativesDataBlob(StartupData* startup_blob);
6346  static void SetSnapshotDataBlob(StartupData* startup_blob);
6347 
6348  /**
6349  * Bootstrap an isolate and a context from scratch to create a startup
6350  * snapshot. Include the side-effects of running the optional script.
6351  * Returns { NULL, 0 } on failure.
6352  * The caller acquires ownership of the data array in the return value.
6353  */
6354  static StartupData CreateSnapshotDataBlob(const char* embedded_source = NULL);
6355 
6356  /**
6357  * Bootstrap an isolate and a context from the cold startup blob, run the
6358  * warm-up script to trigger code compilation. The side effects are then
6359  * discarded. The resulting startup snapshot will include compiled code.
6360  * Returns { NULL, 0 } on failure.
6361  * The caller acquires ownership of the data array in the return value.
6362  * The argument startup blob is untouched.
6363  */
6365  const char* warmup_source);
6366 
6367  /**
6368  * Adds a message listener.
6369  *
6370  * The same message listener can be added more than once and in that
6371  * case it will be called more than once for each message.
6372  *
6373  * If data is specified, it will be passed to the callback when it is called.
6374  * Otherwise, the exception object will be passed to the callback instead.
6375  */
6377  "Use isolate version",
6378  bool AddMessageListener(MessageCallback that,
6379  Local<Value> data = Local<Value>()));
6380 
6381  /**
6382  * Remove all message listeners from the specified callback function.
6383  */
6385  "Use isolate version", void RemoveMessageListeners(MessageCallback that));
6386 
6387  /**
6388  * Tells V8 to capture current stack trace when uncaught exception occurs
6389  * and report it to the message listeners. The option is off by default.
6390  */
6392  "Use isolate version",
6393  void SetCaptureStackTraceForUncaughtExceptions(
6394  bool capture, int frame_limit = 10,
6396 
6397  /**
6398  * Sets V8 flags from a string.
6399  */
6400  static void SetFlagsFromString(const char* str, int length);
6401 
6402  /**
6403  * Sets V8 flags from the command line.
6404  */
6405  static void SetFlagsFromCommandLine(int* argc,
6406  char** argv,
6407  bool remove_flags);
6408 
6409  /** Get the version string. */
6410  static const char* GetVersion();
6411 
6412  /** Callback function for reporting failed access checks.*/
6414  "Use isolate version",
6415  void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback));
6416 
6417  /**
6418  * Enables the host application to receive a notification before a
6419  * garbage collection. Allocations are not allowed in the
6420  * callback function, you therefore cannot manipulate objects (set
6421  * or delete properties for example) since it is possible such
6422  * operations will result in the allocation of objects. It is possible
6423  * to specify the GCType filter for your callback. But it is not possible to
6424  * register the same callback function two times with different
6425  * GCType filters.
6426  */
6428  "Use isolate version",
6429  void AddGCPrologueCallback(GCCallback callback,
6430  GCType gc_type_filter = kGCTypeAll));
6431 
6432  /**
6433  * This function removes callback which was installed by
6434  * AddGCPrologueCallback function.
6435  */
6437  "Use isolate version",
6438  void RemoveGCPrologueCallback(GCCallback callback));
6439 
6440  /**
6441  * Enables the host application to receive a notification after a
6442  * garbage collection. Allocations are not allowed in the
6443  * callback function, you therefore cannot manipulate objects (set
6444  * or delete properties for example) since it is possible such
6445  * operations will result in the allocation of objects. It is possible
6446  * to specify the GCType filter for your callback. But it is not possible to
6447  * register the same callback function two times with different
6448  * GCType filters.
6449  */
6451  "Use isolate version",
6452  void AddGCEpilogueCallback(GCCallback callback,
6453  GCType gc_type_filter = kGCTypeAll));
6454 
6455  /**
6456  * This function removes callback which was installed by
6457  * AddGCEpilogueCallback function.
6458  */
6460  "Use isolate version",
6461  void RemoveGCEpilogueCallback(GCCallback callback));
6462 
6463  /**
6464  * Enables the host application to provide a mechanism to be notified
6465  * and perform custom logging when V8 Allocates Executable Memory.
6466  */
6468  "Use isolate version",
6469  void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6470  ObjectSpace space,
6471  AllocationAction action));
6472 
6473  /**
6474  * Removes callback that was installed by AddMemoryAllocationCallback.
6475  */
6477  "Use isolate version",
6478  void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback));
6479 
6480  /**
6481  * Initializes V8. This function needs to be called before the first Isolate
6482  * is created. It always returns true.
6483  */
6484  static bool Initialize();
6485 
6486  /**
6487  * Allows the host application to provide a callback which can be used
6488  * as a source of entropy for random number generators.
6489  */
6490  static void SetEntropySource(EntropySource source);
6491 
6492  /**
6493  * Allows the host application to provide a callback that allows v8 to
6494  * cooperate with a profiler that rewrites return addresses on stack.
6495  */
6497  ReturnAddressLocationResolver return_address_resolver);
6498 
6499  /**
6500  * Forcefully terminate the current thread of JavaScript execution
6501  * in the given isolate.
6502  *
6503  * This method can be used by any thread even if that thread has not
6504  * acquired the V8 lock with a Locker object.
6505  *
6506  * \param isolate The isolate in which to terminate the current JS execution.
6507  */
6508  V8_INLINE static V8_DEPRECATED("Use isolate version",
6509  void TerminateExecution(Isolate* isolate));
6510 
6511  /**
6512  * Is V8 terminating JavaScript execution.
6513  *
6514  * Returns true if JavaScript execution is currently terminating
6515  * because of a call to TerminateExecution. In that case there are
6516  * still JavaScript frames on the stack and the termination
6517  * exception is still active.
6518  *
6519  * \param isolate The isolate in which to check.
6520  */
6522  "Use isolate version",
6523  bool IsExecutionTerminating(Isolate* isolate = NULL));
6524 
6525  /**
6526  * Resume execution capability in the given isolate, whose execution
6527  * was previously forcefully terminated using TerminateExecution().
6528  *
6529  * When execution is forcefully terminated using TerminateExecution(),
6530  * the isolate can not resume execution until all JavaScript frames
6531  * have propagated the uncatchable exception which is generated. This
6532  * method allows the program embedding the engine to handle the
6533  * termination event and resume execution capability, even if
6534  * JavaScript frames remain on the stack.
6535  *
6536  * This method can be used by any thread even if that thread has not
6537  * acquired the V8 lock with a Locker object.
6538  *
6539  * \param isolate The isolate in which to resume execution capability.
6540  */
6542  "Use isolate version", void CancelTerminateExecution(Isolate* isolate));
6543 
6544  /**
6545  * Releases any resources used by v8 and stops any utility threads
6546  * that may be running. Note that disposing v8 is permanent, it
6547  * cannot be reinitialized.
6548  *
6549  * It should generally not be necessary to dispose v8 before exiting
6550  * a process, this should happen automatically. It is only necessary
6551  * to use if the process needs the resources taken up by v8.
6552  */
6553  static bool Dispose();
6554 
6555  /**
6556  * Iterates through all external resources referenced from current isolate
6557  * heap. GC is not invoked prior to iterating, therefore there is no
6558  * guarantee that visited objects are still alive.
6559  */
6561  "Use isolate version",
6562  void VisitExternalResources(ExternalResourceVisitor* visitor));
6563 
6564  /**
6565  * Iterates through all the persistent handles in the current isolate's heap
6566  * that have class_ids.
6567  */
6569  "Use isolate version",
6570  void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor));
6571 
6572  /**
6573  * Iterates through all the persistent handles in isolate's heap that have
6574  * class_ids.
6575  */
6577  "Use isolate version",
6578  void VisitHandlesWithClassIds(Isolate* isolate,
6579  PersistentHandleVisitor* visitor));
6580 
6581  /**
6582  * Iterates through all the persistent handles in the current isolate's heap
6583  * that have class_ids and are candidates to be marked as partially dependent
6584  * handles. This will visit handles to young objects created since the last
6585  * garbage collection but is free to visit an arbitrary superset of these
6586  * objects.
6587  */
6589  "Use isolate version",
6590  void VisitHandlesForPartialDependence(Isolate* isolate,
6591  PersistentHandleVisitor* visitor));
6592 
6593  /**
6594  * Initialize the ICU library bundled with V8. The embedder should only
6595  * invoke this method when using the bundled ICU. Returns true on success.
6596  *
6597  * If V8 was compiled with the ICU data in an external file, the location
6598  * of the data file has to be provided.
6599  */
6600  static bool InitializeICU(const char* icu_data_file = NULL);
6601 
6602  /**
6603  * Initialize the external startup data. The embedder only needs to
6604  * invoke this method when external startup data was enabled in a build.
6605  *
6606  * If V8 was compiled with the startup data in an external file, then
6607  * V8 needs to be given those external files during startup. There are
6608  * three ways to do this:
6609  * - InitializeExternalStartupData(const char*)
6610  * This will look in the given directory for files "natives_blob.bin"
6611  * and "snapshot_blob.bin" - which is what the default build calls them.
6612  * - InitializeExternalStartupData(const char*, const char*)
6613  * As above, but will directly use the two given file names.
6614  * - Call SetNativesDataBlob, SetNativesDataBlob.
6615  * This will read the blobs from the given data structures and will
6616  * not perform any file IO.
6617  */
6618  static void InitializeExternalStartupData(const char* directory_path);
6619  static void InitializeExternalStartupData(const char* natives_blob,
6620  const char* snapshot_blob);
6621  /**
6622  * Sets the v8::Platform to use. This should be invoked before V8 is
6623  * initialized.
6624  */
6625  static void InitializePlatform(Platform* platform);
6626 
6627  /**
6628  * Clears all references to the v8::Platform. This should be invoked after
6629  * V8 was disposed.
6630  */
6631  static void ShutdownPlatform();
6632 
6633  private:
6634  V8();
6635 
6636  static internal::Object** GlobalizeReference(internal::Isolate* isolate,
6637  internal::Object** handle);
6638  static internal::Object** CopyPersistent(internal::Object** handle);
6639  static void DisposeGlobal(internal::Object** global_handle);
6640  typedef WeakCallbackData<Value, void>::Callback WeakCallback;
6641  static void RegisterExternallyReferencedObject(internal::Object** object,
6642  internal::Isolate* isolate);
6643  static void MakeWeak(internal::Object** global_handle, void* data,
6644  WeakCallback weak_callback);
6645  static void MakeWeak(internal::Object** global_handle, void* data,
6646  WeakCallbackInfo<void>::Callback weak_callback,
6647  WeakCallbackType type);
6648  static void MakeWeak(internal::Object** global_handle, void* data,
6649  // Must be 0 or -1.
6650  int internal_field_index1,
6651  // Must be 1 or -1.
6652  int internal_field_index2,
6653  WeakCallbackInfo<void>::Callback weak_callback);
6654  static void* ClearWeak(internal::Object** global_handle);
6655  static void Eternalize(Isolate* isolate,
6656  Value* handle,
6657  int* index);
6658  static Local<Value> GetEternal(Isolate* isolate, int index);
6659 
6660  static void FromJustIsNothing();
6661  static void ToLocalEmpty();
6662  static void InternalFieldOutOfBounds(int index);
6663  template <class T> friend class Local;
6664  template <class T>
6665  friend class MaybeLocal;
6666  template <class T>
6667  friend class Maybe;
6668  template <class T>
6669  friend class WeakCallbackInfo;
6670  template <class T> friend class Eternal;
6671  template <class T> friend class PersistentBase;
6672  template <class T, class M> friend class Persistent;
6673  friend class Context;
6674 };
6675 
6676 
6677 /**
6678  * A simple Maybe type, representing an object which may or may not have a
6679  * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
6680  *
6681  * If an API method returns a Maybe<>, the API method can potentially fail
6682  * either because an exception is thrown, or because an exception is pending,
6683  * e.g. because a previous API call threw an exception that hasn't been caught
6684  * yet, or because a TerminateExecution exception was thrown. In that case, a
6685  * "Nothing" value is returned.
6686  */
6687 template <class T>
6688 class Maybe {
6689  public:
6690  V8_INLINE bool IsNothing() const { return !has_value; }
6691  V8_INLINE bool IsJust() const { return has_value; }
6692 
6693  // Will crash if the Maybe<> is nothing.
6694  V8_INLINE T FromJust() const {
6695  if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing();
6696  return value;
6697  }
6698 
6699  V8_INLINE T FromMaybe(const T& default_value) const {
6700  return has_value ? value : default_value;
6701  }
6702 
6703  V8_INLINE bool operator==(const Maybe& other) const {
6704  return (IsJust() == other.IsJust()) &&
6705  (!IsJust() || FromJust() == other.FromJust());
6706  }
6707 
6708  V8_INLINE bool operator!=(const Maybe& other) const {
6709  return !operator==(other);
6710  }
6711 
6712  private:
6713  Maybe() : has_value(false) {}
6714  explicit Maybe(const T& t) : has_value(true), value(t) {}
6715 
6716  bool has_value;
6717  T value;
6718 
6719  template <class U>
6720  friend Maybe<U> Nothing();
6721  template <class U>
6722  friend Maybe<U> Just(const U& u);
6723 };
6724 
6725 
6726 template <class T>
6727 inline Maybe<T> Nothing() {
6728  return Maybe<T>();
6729 }
6730 
6731 
6732 template <class T>
6733 inline Maybe<T> Just(const T& t) {
6734  return Maybe<T>(t);
6735 }
6736 
6737 
6738 /**
6739  * An external exception handler.
6740  */
6742  public:
6743  /**
6744  * Creates a new try/catch block and registers it with v8. Note that
6745  * all TryCatch blocks should be stack allocated because the memory
6746  * location itself is compared against JavaScript try/catch blocks.
6747  */
6748  V8_DEPRECATED("Use isolate version", TryCatch());
6749 
6750  /**
6751  * Creates a new try/catch block and registers it with v8. Note that
6752  * all TryCatch blocks should be stack allocated because the memory
6753  * location itself is compared against JavaScript try/catch blocks.
6754  */
6755  TryCatch(Isolate* isolate);
6756 
6757  /**
6758  * Unregisters and deletes this try/catch block.
6759  */
6761 
6762  /**
6763  * Returns true if an exception has been caught by this try/catch block.
6764  */
6765  bool HasCaught() const;
6766 
6767  /**
6768  * For certain types of exceptions, it makes no sense to continue execution.
6769  *
6770  * If CanContinue returns false, the correct action is to perform any C++
6771  * cleanup needed and then return. If CanContinue returns false and
6772  * HasTerminated returns true, it is possible to call
6773  * CancelTerminateExecution in order to continue calling into the engine.
6774  */
6775  bool CanContinue() const;
6776 
6777  /**
6778  * Returns true if an exception has been caught due to script execution
6779  * being terminated.
6780  *
6781  * There is no JavaScript representation of an execution termination
6782  * exception. Such exceptions are thrown when the TerminateExecution
6783  * methods are called to terminate a long-running script.
6784  *
6785  * If such an exception has been thrown, HasTerminated will return true,
6786  * indicating that it is possible to call CancelTerminateExecution in order
6787  * to continue calling into the engine.
6788  */
6789  bool HasTerminated() const;
6790 
6791  /**
6792  * Throws the exception caught by this TryCatch in a way that avoids
6793  * it being caught again by this same TryCatch. As with ThrowException
6794  * it is illegal to execute any JavaScript operations after calling
6795  * ReThrow; the caller must return immediately to where the exception
6796  * is caught.
6797  */
6799 
6800  /**
6801  * Returns the exception caught by this try/catch block. If no exception has
6802  * been caught an empty handle is returned.
6803  *
6804  * The returned handle is valid until this TryCatch block has been destroyed.
6805  */
6807 
6808  /**
6809  * Returns the .stack property of the thrown object. If no .stack
6810  * property is present an empty handle is returned.
6811  */
6812  V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const);
6814  Local<Context> context) const;
6815 
6816  /**
6817  * Returns the message associated with this exception. If there is
6818  * no message associated an empty handle is returned.
6819  *
6820  * The returned handle is valid until this TryCatch block has been
6821  * destroyed.
6822  */
6823  Local<v8::Message> Message() const;
6824 
6825  /**
6826  * Clears any exceptions that may have been caught by this try/catch block.
6827  * After this method has been called, HasCaught() will return false. Cancels
6828  * the scheduled exception if it is caught and ReThrow() is not called before.
6829  *
6830  * It is not necessary to clear a try/catch block before using it again; if
6831  * another exception is thrown the previously caught exception will just be
6832  * overwritten. However, it is often a good idea since it makes it easier
6833  * to determine which operation threw a given exception.
6834  */
6835  void Reset();
6836 
6837  /**
6838  * Set verbosity of the external exception handler.
6839  *
6840  * By default, exceptions that are caught by an external exception
6841  * handler are not reported. Call SetVerbose with true on an
6842  * external exception handler to have exceptions caught by the
6843  * handler reported as if they were not caught.
6844  */
6845  void SetVerbose(bool value);
6846 
6847  /**
6848  * Set whether or not this TryCatch should capture a Message object
6849  * which holds source information about where the exception
6850  * occurred. True by default.
6851  */
6852  void SetCaptureMessage(bool value);
6853 
6854  /**
6855  * There are cases when the raw address of C++ TryCatch object cannot be
6856  * used for comparisons with addresses into the JS stack. The cases are:
6857  * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
6858  * 2) Address sanitizer allocates local C++ object in the heap when
6859  * UseAfterReturn mode is enabled.
6860  * This method returns address that can be used for comparisons with
6861  * addresses into the JS stack. When neither simulator nor ASAN's
6862  * UseAfterReturn is enabled, then the address returned will be the address
6863  * of the C++ try catch handler itself.
6864  */
6865  static void* JSStackComparableAddress(v8::TryCatch* handler) {
6866  if (handler == NULL) return NULL;
6867  return handler->js_stack_comparable_address_;
6868  }
6869 
6870  private:
6871  void ResetInternal();
6872 
6873  // Make it hard to create heap-allocated TryCatch blocks.
6874  TryCatch(const TryCatch&);
6875  void operator=(const TryCatch&);
6876  void* operator new(size_t size);
6877  void operator delete(void*, size_t);
6878 
6879  v8::internal::Isolate* isolate_;
6880  v8::TryCatch* next_;
6881  void* exception_;
6882  void* message_obj_;
6883  void* js_stack_comparable_address_;
6884  bool is_verbose_ : 1;
6885  bool can_continue_ : 1;
6886  bool capture_message_ : 1;
6887  bool rethrow_ : 1;
6888  bool has_terminated_ : 1;
6889 
6890  friend class v8::internal::Isolate;
6891 };
6892 
6893 
6894 // --- Context ---
6895 
6896 
6897 /**
6898  * A container for extension names.
6899  */
6901  public:
6902  ExtensionConfiguration() : name_count_(0), names_(NULL) { }
6903  ExtensionConfiguration(int name_count, const char* names[])
6904  : name_count_(name_count), names_(names) { }
6905 
6906  const char** begin() const { return &names_[0]; }
6907  const char** end() const { return &names_[name_count_]; }
6908 
6909  private:
6910  const int name_count_;
6911  const char** names_;
6912 };
6913 
6914 
6915 /**
6916  * A sandboxed execution context with its own set of built-in objects
6917  * and functions.
6918  */
6920  public:
6921  /**
6922  * Returns the global proxy object.
6923  *
6924  * Global proxy object is a thin wrapper whose prototype points to actual
6925  * context's global object with the properties like Object, etc. This is done
6926  * that way for security reasons (for more details see
6927  * https://wiki.mozilla.org/Gecko:SplitWindow).
6928  *
6929  * Please note that changes to global proxy object prototype most probably
6930  * would break VM---v8 expects only global object as a prototype of global
6931  * proxy object.
6932  */
6934 
6935  /**
6936  * Detaches the global object from its context before
6937  * the global object can be reused to create a new context.
6938  */
6940 
6941  /**
6942  * Creates a new context and returns a handle to the newly allocated
6943  * context.
6944  *
6945  * \param isolate The isolate in which to create the context.
6946  *
6947  * \param extensions An optional extension configuration containing
6948  * the extensions to be installed in the newly created context.
6949  *
6950  * \param global_template An optional object template from which the
6951  * global object for the newly created context will be created.
6952  *
6953  * \param global_object An optional global object to be reused for
6954  * the newly created context. This global object must have been
6955  * created by a previous call to Context::New with the same global
6956  * template. The state of the global object will be completely reset
6957  * and only object identify will remain.
6958  */
6959  static Local<Context> New(
6960  Isolate* isolate, ExtensionConfiguration* extensions = NULL,
6961  Local<ObjectTemplate> global_template = Local<ObjectTemplate>(),
6962  Local<Value> global_object = Local<Value>());
6963 
6964  /**
6965  * Sets the security token for the context. To access an object in
6966  * another context, the security tokens must match.
6967  */
6969 
6970  /** Restores the security token to the default value. */
6972 
6973  /** Returns the security token of this context.*/
6975 
6976  /**
6977  * Enter this context. After entering a context, all code compiled
6978  * and run is compiled and run in this context. If another context
6979  * is already entered, this old context is saved so it can be
6980  * restored when the new context is exited.
6981  */
6982  void Enter();
6983 
6984  /**
6985  * Exit this context. Exiting the current context restores the
6986  * context that was in place when entering the current context.
6987  */
6988  void Exit();
6989 
6990  /** Returns an isolate associated with a current context. */
6992 
6993  /**
6994  * The field at kDebugIdIndex is reserved for V8 debugger implementation.
6995  * The value is propagated to the scripts compiled in given Context and
6996  * can be used for filtering scripts.
6997  */
6999 
7000  /**
7001  * Gets the embedder data with the given index, which must have been set by a
7002  * previous call to SetEmbedderData with the same index. Note that index 0
7003  * currently has a special meaning for Chrome's debugger.
7004  */
7005  V8_INLINE Local<Value> GetEmbedderData(int index);
7006 
7007  /**
7008  * Gets the binding object used by V8 extras. Extra natives get a reference
7009  * to this object and can use it to "export" functionality by adding
7010  * properties. Extra natives can also "import" functionality by accessing
7011  * properties added by the embedder using the V8 API.
7012  */
7014 
7015  /**
7016  * Sets the embedder data with the given index, growing the data as
7017  * needed. Note that index 0 currently has a special meaning for Chrome's
7018  * debugger.
7019  */
7020  void SetEmbedderData(int index, Local<Value> value);
7021 
7022  /**
7023  * Gets a 2-byte-aligned native pointer from the embedder data with the given
7024  * index, which must have bees set by a previous call to
7025  * SetAlignedPointerInEmbedderData with the same index. Note that index 0
7026  * currently has a special meaning for Chrome's debugger.
7027  */
7029 
7030  /**
7031  * Sets a 2-byte-aligned native pointer in the embedder data with the given
7032  * index, growing the data as needed. Note that index 0 currently has a
7033  * special meaning for Chrome's debugger.
7034  */
7035  void SetAlignedPointerInEmbedderData(int index, void* value);
7036 
7037  /**
7038  * Control whether code generation from strings is allowed. Calling
7039  * this method with false will disable 'eval' and the 'Function'
7040  * constructor for code running in this context. If 'eval' or the
7041  * 'Function' constructor are used an exception will be thrown.
7042  *
7043  * If code generation from strings is not allowed the
7044  * V8::AllowCodeGenerationFromStrings callback will be invoked if
7045  * set before blocking the call to 'eval' or the 'Function'
7046  * constructor. If that callback returns true, the call will be
7047  * allowed, otherwise an exception will be thrown. If no callback is
7048  * set an exception will be thrown.
7049  */
7051 
7052  /**
7053  * Returns true if code generation from strings is allowed for the context.
7054  * For more details see AllowCodeGenerationFromStrings(bool) documentation.
7055  */
7057 
7058  /**
7059  * Sets the error description for the exception that is thrown when
7060  * code generation from strings is not allowed and 'eval' or the 'Function'
7061  * constructor are called.
7062  */
7064 
7065  /**
7066  * Estimate the memory in bytes retained by this context.
7067  */
7068  size_t EstimatedSize();
7069 
7070  /**
7071  * Stack-allocated class which sets the execution context for all
7072  * operations executed within a local scope.
7073  */
7074  class Scope {
7075  public:
7076  explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
7077  context_->Enter();
7078  }
7079  V8_INLINE ~Scope() { context_->Exit(); }
7080 
7081  private:
7082  Local<Context> context_;
7083  };
7084 
7085  private:
7086  friend class Value;
7087  friend class Script;
7088  friend class Object;
7089  friend class Function;
7090 
7091  Local<Value> SlowGetEmbedderData(int index);
7092  void* SlowGetAlignedPointerFromEmbedderData(int index);
7093 };
7094 
7095 
7096 /**
7097  * Multiple threads in V8 are allowed, but only one thread at a time is allowed
7098  * to use any given V8 isolate, see the comments in the Isolate class. The
7099  * definition of 'using a V8 isolate' includes accessing handles or holding onto
7100  * object pointers obtained from V8 handles while in the particular V8 isolate.
7101  * It is up to the user of V8 to ensure, perhaps with locking, that this
7102  * constraint is not violated. In addition to any other synchronization
7103  * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
7104  * used to signal thead switches to V8.
7105  *
7106  * v8::Locker is a scoped lock object. While it's active, i.e. between its
7107  * construction and destruction, the current thread is allowed to use the locked
7108  * isolate. V8 guarantees that an isolate can be locked by at most one thread at
7109  * any time. In other words, the scope of a v8::Locker is a critical section.
7110  *
7111  * Sample usage:
7112 * \code
7113  * ...
7114  * {
7115  * v8::Locker locker(isolate);
7116  * v8::Isolate::Scope isolate_scope(isolate);
7117  * ...
7118  * // Code using V8 and isolate goes here.
7119  * ...
7120  * } // Destructor called here
7121  * \endcode
7122  *
7123  * If you wish to stop using V8 in a thread A you can do this either by
7124  * destroying the v8::Locker object as above or by constructing a v8::Unlocker
7125  * object:
7126  *
7127  * \code
7128  * {
7129  * isolate->Exit();
7130  * v8::Unlocker unlocker(isolate);
7131  * ...
7132  * // Code not using V8 goes here while V8 can run in another thread.
7133  * ...
7134  * } // Destructor called here.
7135  * isolate->Enter();
7136  * \endcode
7137  *
7138  * The Unlocker object is intended for use in a long-running callback from V8,
7139  * where you want to release the V8 lock for other threads to use.
7140  *
7141  * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
7142  * given thread. This can be useful if you have code that can be called either
7143  * from code that holds the lock or from code that does not. The Unlocker is
7144  * not recursive so you can not have several Unlockers on the stack at once, and
7145  * you can not use an Unlocker in a thread that is not inside a Locker's scope.
7146  *
7147  * An unlocker will unlock several lockers if it has to and reinstate the
7148  * correct depth of locking on its destruction, e.g.:
7149  *
7150  * \code
7151  * // V8 not locked.
7152  * {
7153  * v8::Locker locker(isolate);
7154  * Isolate::Scope isolate_scope(isolate);
7155  * // V8 locked.
7156  * {
7157  * v8::Locker another_locker(isolate);
7158  * // V8 still locked (2 levels).
7159  * {
7160  * isolate->Exit();
7161  * v8::Unlocker unlocker(isolate);
7162  * // V8 not locked.
7163  * }
7164  * isolate->Enter();
7165  * // V8 locked again (2 levels).
7166  * }
7167  * // V8 still locked (1 level).
7168  * }
7169  * // V8 Now no longer locked.
7170  * \endcode
7171  */
7173  public:
7174  /**
7175  * Initialize Unlocker for a given Isolate.
7176  */
7177  V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
7178 
7180  private:
7181  void Initialize(Isolate* isolate);
7182 
7183  internal::Isolate* isolate_;
7184 };
7185 
7186 
7188  public:
7189  /**
7190  * Initialize Locker for a given Isolate.
7191  */
7192  V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
7193 
7195 
7196  /**
7197  * Returns whether or not the locker for a given isolate, is locked by the
7198  * current thread.
7199  */
7200  static bool IsLocked(Isolate* isolate);
7201 
7202  /**
7203  * Returns whether v8::Locker is being used by this V8 instance.
7204  */
7205  static bool IsActive();
7206 
7207  private:
7208  void Initialize(Isolate* isolate);
7209 
7210  bool has_lock_;
7211  bool top_level_;
7212  internal::Isolate* isolate_;
7213 
7214  // Disallow copying and assigning.
7215  Locker(const Locker&);
7216  void operator=(const Locker&);
7217 };
7218 
7219 
7220 // --- Implementation ---
7221 
7222 
7223 namespace internal {
7224 
7225 const int kApiPointerSize = sizeof(void*); // NOLINT
7226 const int kApiIntSize = sizeof(int); // NOLINT
7227 const int kApiInt64Size = sizeof(int64_t); // NOLINT
7228 
7229 // Tag information for HeapObject.
7230 const int kHeapObjectTag = 1;
7231 const int kHeapObjectTagSize = 2;
7232 const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
7233 
7234 // Tag information for Smi.
7235 const int kSmiTag = 0;
7236 const int kSmiTagSize = 1;
7237 const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
7238 
7239 template <size_t ptr_size> struct SmiTagging;
7240 
7241 template<int kSmiShiftSize>
7242 V8_INLINE internal::Object* IntToSmi(int value) {
7243  int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
7244  uintptr_t tagged_value =
7245  (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag;
7246  return reinterpret_cast<internal::Object*>(tagged_value);
7247 }
7248 
7249 // Smi constants for 32-bit systems.
7250 template <> struct SmiTagging<4> {
7251  enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
7252  static int SmiShiftSize() { return kSmiShiftSize; }
7253  static int SmiValueSize() { return kSmiValueSize; }
7254  V8_INLINE static int SmiToInt(const internal::Object* value) {
7255  int shift_bits = kSmiTagSize + kSmiShiftSize;
7256  // Throw away top 32 bits and shift down (requires >> to be sign extending).
7257  return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
7258  }
7259  V8_INLINE static internal::Object* IntToSmi(int value) {
7261  }
7262  V8_INLINE static bool IsValidSmi(intptr_t value) {
7263  // To be representable as an tagged small integer, the two
7264  // most-significant bits of 'value' must be either 00 or 11 due to
7265  // sign-extension. To check this we add 01 to the two
7266  // most-significant bits, and check if the most-significant bit is 0
7267  //
7268  // CAUTION: The original code below:
7269  // bool result = ((value + 0x40000000) & 0x80000000) == 0;
7270  // may lead to incorrect results according to the C language spec, and
7271  // in fact doesn't work correctly with gcc4.1.1 in some cases: The
7272  // compiler may produce undefined results in case of signed integer
7273  // overflow. The computation must be done w/ unsigned ints.
7274  return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
7275  }
7276 };
7277 
7278 // Smi constants for 64-bit systems.
7279 template <> struct SmiTagging<8> {
7280  enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
7281  static int SmiShiftSize() { return kSmiShiftSize; }
7282  static int SmiValueSize() { return kSmiValueSize; }
7283  V8_INLINE static int SmiToInt(const internal::Object* value) {
7284  int shift_bits = kSmiTagSize + kSmiShiftSize;
7285  // Shift down and throw away top 32 bits.
7286  return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
7287  }
7288  V8_INLINE static internal::Object* IntToSmi(int value) {
7290  }
7291  V8_INLINE static bool IsValidSmi(intptr_t value) {
7292  // To be representable as a long smi, the value must be a 32-bit integer.
7293  return (value == static_cast<int32_t>(value));
7294  }
7295 };
7296 
7300 V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
7301 V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
7302 
7303 /**
7304  * This class exports constants and functionality from within v8 that
7305  * is necessary to implement inline functions in the v8 api. Don't
7306  * depend on functions and constants defined here.
7307  */
7308 class Internals {
7309  public:
7310  // These values match non-compiler-dependent values defined within
7311  // the implementation of v8.
7312  static const int kHeapObjectMapOffset = 0;
7315  static const int kStringResourceOffset = 3 * kApiPointerSize;
7316 
7317  static const int kOddballKindOffset = 5 * kApiPointerSize;
7319  static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
7320  static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
7321  static const int kContextHeaderSize = 2 * kApiPointerSize;
7322  static const int kContextEmbedderDataIndex = 5;
7323  static const int kFullStringRepresentationMask = 0x07;
7324  static const int kStringEncodingMask = 0x4;
7325  static const int kExternalTwoByteRepresentationTag = 0x02;
7326  static const int kExternalOneByteRepresentationTag = 0x06;
7327 
7330  4 * kApiPointerSize;
7333  static const int kIsolateRootsOffset =
7336  static const int kUndefinedValueRootIndex = 4;
7337  static const int kNullValueRootIndex = 6;
7338  static const int kTrueValueRootIndex = 7;
7339  static const int kFalseValueRootIndex = 8;
7340  static const int kEmptyStringRootIndex = 9;
7341 
7342  // The external allocation limit should be below 256 MB on all architectures
7343  // to avoid that resource-constrained embedders run low on memory.
7344  static const int kExternalAllocationLimit = 192 * 1024 * 1024;
7345 
7346  static const int kNodeClassIdOffset = 1 * kApiPointerSize;
7347  static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
7348  static const int kNodeStateMask = 0x7;
7349  static const int kNodeStateIsWeakValue = 2;
7350  static const int kNodeStateIsPendingValue = 3;
7351  static const int kNodeStateIsNearDeathValue = 4;
7352  static const int kNodeIsIndependentShift = 3;
7353  static const int kNodeIsPartiallyDependentShift = 4;
7354  static const int kNodeIsActiveShift = 4;
7355 
7356  static const int kJSObjectType = 0xb8;
7357  static const int kFirstNonstringType = 0x80;
7358  static const int kOddballType = 0x83;
7359  static const int kForeignType = 0x87;
7360 
7361  static const int kUndefinedOddballKind = 5;
7362  static const int kNullOddballKind = 3;
7363 
7364  static const uint32_t kNumIsolateDataSlots = 4;
7365 
7366  V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
7367  V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
7368 #ifdef V8_ENABLE_CHECKS
7369  CheckInitializedImpl(isolate);
7370 #endif
7371  }
7372 
7373  V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) {
7374  return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
7375  kHeapObjectTag);
7376  }
7377 
7378  V8_INLINE static int SmiValue(const internal::Object* value) {
7379  return PlatformSmiTagging::SmiToInt(value);
7380  }
7381 
7382  V8_INLINE static internal::Object* IntToSmi(int value) {
7383  return PlatformSmiTagging::IntToSmi(value);
7384  }
7385 
7386  V8_INLINE static bool IsValidSmi(intptr_t value) {
7388  }
7389 
7390  V8_INLINE static int GetInstanceType(const internal::Object* obj) {
7391  typedef internal::Object O;
7393  // Map::InstanceType is defined so that it will always be loaded into
7394  // the LS 8 bits of one 16-bit word, regardless of endianess.
7395  return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff;
7396  }
7397 
7398  V8_INLINE static int GetOddballKind(const internal::Object* obj) {
7399  typedef internal::Object O;
7401  }
7402 
7403  V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
7404  int representation = (instance_type & kFullStringRepresentationMask);
7405  return representation == kExternalTwoByteRepresentationTag;
7406  }
7407 
7408  V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) {
7409  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7410  return *addr & static_cast<uint8_t>(1U << shift);
7411  }
7412 
7413  V8_INLINE static void UpdateNodeFlag(internal::Object** obj,
7414  bool value, int shift) {
7415  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7416  uint8_t mask = static_cast<uint8_t>(1U << shift);
7417  *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
7418  }
7419 
7420  V8_INLINE static uint8_t GetNodeState(internal::Object** obj) {
7421  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7422  return *addr & kNodeStateMask;
7423  }
7424 
7425  V8_INLINE static void UpdateNodeState(internal::Object** obj,
7426  uint8_t value) {
7427  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7428  *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
7429  }
7430 
7431  V8_INLINE static void SetEmbedderData(v8::Isolate* isolate,
7432  uint32_t slot,
7433  void* data) {
7434  uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
7436  *reinterpret_cast<void**>(addr) = data;
7437  }
7438 
7439  V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
7440  uint32_t slot) {
7441  const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) +
7443  return *reinterpret_cast<void* const*>(addr);
7444  }
7445 
7446  V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate,
7447  int index) {
7448  uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
7449  return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
7450  }
7451 
7452  template <typename T>
7453  V8_INLINE static T ReadField(const internal::Object* ptr, int offset) {
7454  const uint8_t* addr =
7455  reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag;
7456  return *reinterpret_cast<const T*>(addr);
7457  }
7458 
7459  template <typename T>
7460  V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) {
7461  typedef internal::Object O;
7462  typedef internal::Internals I;
7463  O* ctx = *reinterpret_cast<O* const*>(context);
7464  int embedder_data_offset = I::kContextHeaderSize +
7466  O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
7467  int value_offset =
7469  return I::ReadField<T>(embedder_data, value_offset);
7470  }
7471 };
7472 
7473 } // namespace internal
7474 
7475 
7476 template <class T>
7477 Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
7478  return New(isolate, that.val_);
7479 }
7480 
7481 template <class T>
7482 Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
7483  return New(isolate, that.val_);
7484 }
7485 
7486 
7487 template <class T>
7488 Local<T> Local<T>::New(Isolate* isolate, T* that) {
7489  if (that == NULL) return Local<T>();
7490  T* that_ptr = that;
7491  internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
7492  return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
7493  reinterpret_cast<internal::Isolate*>(isolate), *p)));
7494 }
7495 
7496 
7497 template<class T>
7498 template<class S>
7499 void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
7500  TYPE_CHECK(T, S);
7501  V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
7502 }
7503 
7504 
7505 template<class T>
7506 Local<T> Eternal<T>::Get(Isolate* isolate) {
7507  return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
7508 }
7509 
7510 
7511 template <class T>
7513  if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty();
7514  return Local<T>(val_);
7515 }
7516 
7517 
7518 template <class T>
7519 void* WeakCallbackInfo<T>::GetInternalField(int index) const {
7520 #ifdef V8_ENABLE_CHECKS
7521  if (index < 0 || index >= kInternalFieldsInWeakCallback) {
7522  V8::InternalFieldOutOfBounds(index);
7523  }
7524 #endif
7525  return internal_fields_[index];
7526 }
7527 
7528 
7529 template <class T>
7530 T* PersistentBase<T>::New(Isolate* isolate, T* that) {
7531  if (that == NULL) return NULL;
7532  internal::Object** p = reinterpret_cast<internal::Object**>(that);
7533  return reinterpret_cast<T*>(
7534  V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
7535  p));
7536 }
7537 
7538 
7539 template <class T, class M>
7540 template <class S, class M2>
7541 void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
7542  TYPE_CHECK(T, S);
7543  this->Reset();
7544  if (that.IsEmpty()) return;
7545  internal::Object** p = reinterpret_cast<internal::Object**>(that.val_);
7546  this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p));
7547  M::Copy(that, this);
7548 }
7549 
7550 
7551 template <class T>
7552 bool PersistentBase<T>::IsIndependent() const {
7553  typedef internal::Internals I;
7554  if (this->IsEmpty()) return false;
7555  return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7557 }
7558 
7559 
7560 template <class T>
7561 bool PersistentBase<T>::IsNearDeath() const {
7562  typedef internal::Internals I;
7563  if (this->IsEmpty()) return false;
7564  uint8_t node_state =
7565  I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_));
7566  return node_state == I::kNodeStateIsNearDeathValue ||
7567  node_state == I::kNodeStateIsPendingValue;
7568 }
7569 
7570 
7571 template <class T>
7572 bool PersistentBase<T>::IsWeak() const {
7573  typedef internal::Internals I;
7574  if (this->IsEmpty()) return false;
7575  return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
7577 }
7578 
7579 
7580 template <class T>
7581 void PersistentBase<T>::Reset() {
7582  if (this->IsEmpty()) return;
7583  V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
7584  val_ = 0;
7585 }
7586 
7587 
7588 template <class T>
7589 template <class S>
7590 void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
7591  TYPE_CHECK(T, S);
7592  Reset();
7593  if (other.IsEmpty()) return;
7594  this->val_ = New(isolate, other.val_);
7595 }
7596 
7597 
7598 template <class T>
7599 template <class S>
7600 void PersistentBase<T>::Reset(Isolate* isolate,
7601  const PersistentBase<S>& other) {
7602  TYPE_CHECK(T, S);
7603  Reset();
7604  if (other.IsEmpty()) return;
7605  this->val_ = New(isolate, other.val_);
7606 }
7607 
7608 
7609 template <class T>
7610 template <typename S, typename P>
7611 void PersistentBase<T>::SetWeak(
7612  P* parameter,
7613  typename WeakCallbackData<S, P>::Callback callback) {
7614  TYPE_CHECK(S, T);
7615  typedef typename WeakCallbackData<Value, void>::Callback Callback;
7616  V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7617  reinterpret_cast<Callback>(callback));
7618 }
7619 
7620 
7621 template <class T>
7622 template <typename P>
7624  P* parameter,
7625  typename WeakCallbackData<T, P>::Callback callback) {
7626  SetWeak<T, P>(parameter, callback);
7627 }
7628 
7629 
7630 template <class T>
7631 template <typename P>
7632 void PersistentBase<T>::SetPhantom(
7633  P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7634  int internal_field_index1, int internal_field_index2) {
7635  typedef typename WeakCallbackInfo<void>::Callback Callback;
7636  V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7637  internal_field_index1, internal_field_index2,
7638  reinterpret_cast<Callback>(callback));
7639 }
7640 
7641 
7642 template <class T>
7643 template <typename P>
7645  P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7646  WeakCallbackType type) {
7647  typedef typename WeakCallbackInfo<void>::Callback Callback;
7648  V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7649  reinterpret_cast<Callback>(callback), type);
7650 }
7651 
7652 
7653 template <class T>
7654 template <typename P>
7655 P* PersistentBase<T>::ClearWeak() {
7656  return reinterpret_cast<P*>(
7657  V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
7658 }
7659 
7660 template <class T>
7662  if (IsEmpty()) return;
7663  V8::RegisterExternallyReferencedObject(
7664  reinterpret_cast<internal::Object**>(this->val_),
7665  reinterpret_cast<internal::Isolate*>(isolate));
7666 }
7667 
7668 template <class T>
7670  typedef internal::Internals I;
7671  if (this->IsEmpty()) return;
7672  I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7673  true,
7675 }
7676 
7677 
7678 template <class T>
7680  typedef internal::Internals I;
7681  if (this->IsEmpty()) return;
7682  I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7683  true,
7685 }
7686 
7687 
7688 template <class T>
7690  typedef internal::Internals I;
7691  if (this->IsEmpty()) return;
7692  I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true,
7694 }
7695 
7696 
7697 template <class T>
7698 void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) {
7699  typedef internal::Internals I;
7700  if (this->IsEmpty()) return;
7701  internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7702  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7703  *reinterpret_cast<uint16_t*>(addr) = class_id;
7704 }
7705 
7706 
7707 template <class T>
7708 uint16_t PersistentBase<T>::WrapperClassId() const {
7709  typedef internal::Internals I;
7710  if (this->IsEmpty()) return 0;
7711  internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7712  uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7713  return *reinterpret_cast<uint16_t*>(addr);
7714 }
7715 
7716 
7717 template<typename T>
7718 ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
7719 
7720 template<typename T>
7721 template<typename S>
7722 void ReturnValue<T>::Set(const Persistent<S>& handle) {
7723  TYPE_CHECK(T, S);
7724  if (V8_UNLIKELY(handle.IsEmpty())) {
7725  *value_ = GetDefaultValue();
7726  } else {
7727  *value_ = *reinterpret_cast<internal::Object**>(*handle);
7728  }
7729 }
7730 
7731 template <typename T>
7732 template <typename S>
7733 void ReturnValue<T>::Set(const Global<S>& handle) {
7734  TYPE_CHECK(T, S);
7735  if (V8_UNLIKELY(handle.IsEmpty())) {
7736  *value_ = GetDefaultValue();
7737  } else {
7738  *value_ = *reinterpret_cast<internal::Object**>(*handle);
7739  }
7740 }
7741 
7742 template <typename T>
7743 template <typename S>
7744 void ReturnValue<T>::Set(const Local<S> handle) {
7745  TYPE_CHECK(T, S);
7746  if (V8_UNLIKELY(handle.IsEmpty())) {
7747  *value_ = GetDefaultValue();
7748  } else {
7749  *value_ = *reinterpret_cast<internal::Object**>(*handle);
7750  }
7751 }
7752 
7753 template<typename T>
7754 void ReturnValue<T>::Set(double i) {
7755  TYPE_CHECK(T, Number);
7757 }
7758 
7759 template<typename T>
7760 void ReturnValue<T>::Set(int32_t i) {
7761  TYPE_CHECK(T, Integer);
7762  typedef internal::Internals I;
7763  if (V8_LIKELY(I::IsValidSmi(i))) {
7764  *value_ = I::IntToSmi(i);
7765  return;
7766  }
7768 }
7769 
7770 template<typename T>
7771 void ReturnValue<T>::Set(uint32_t i) {
7772  TYPE_CHECK(T, Integer);
7773  // Can't simply use INT32_MAX here for whatever reason.
7774  bool fits_into_int32_t = (i & (1U << 31)) == 0;
7775  if (V8_LIKELY(fits_into_int32_t)) {
7776  Set(static_cast<int32_t>(i));
7777  return;
7778  }
7780 }
7781 
7782 template<typename T>
7783 void ReturnValue<T>::Set(bool value) {
7784  TYPE_CHECK(T, Boolean);
7785  typedef internal::Internals I;
7786  int root_index;
7787  if (value) {
7788  root_index = I::kTrueValueRootIndex;
7789  } else {
7790  root_index = I::kFalseValueRootIndex;
7791  }
7792  *value_ = *I::GetRoot(GetIsolate(), root_index);
7793 }
7794 
7795 template<typename T>
7796 void ReturnValue<T>::SetNull() {
7797  TYPE_CHECK(T, Primitive);
7798  typedef internal::Internals I;
7800 }
7801 
7802 template<typename T>
7804  TYPE_CHECK(T, Primitive);
7805  typedef internal::Internals I;
7807 }
7808 
7809 template<typename T>
7811  TYPE_CHECK(T, String);
7812  typedef internal::Internals I;
7814 }
7815 
7816 template<typename T>
7818  // Isolate is always the pointer below the default value on the stack.
7819  return *reinterpret_cast<Isolate**>(&value_[-2]);
7820 }
7821 
7822 template<typename T>
7823 template<typename S>
7824 void ReturnValue<T>::Set(S* whatever) {
7825  // Uncompilable to prevent inadvertent misuse.
7826  TYPE_CHECK(S*, Primitive);
7827 }
7828 
7829 template<typename T>
7830 internal::Object* ReturnValue<T>::GetDefaultValue() {
7831  // Default value is always the pointer below value_ on the stack.
7832  return value_[-1];
7833 }
7834 
7835 
7836 template<typename T>
7838  internal::Object** values,
7839  int length,
7840  bool is_construct_call)
7841  : implicit_args_(implicit_args),
7842  values_(values),
7843  length_(length),
7844  is_construct_call_(is_construct_call) { }
7845 
7846 
7847 template<typename T>
7849  if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate()));
7850  return Local<Value>(reinterpret_cast<Value*>(values_ - i));
7851 }
7852 
7853 
7854 template<typename T>
7855 Local<Function> FunctionCallbackInfo<T>::Callee() const {
7856  return Local<Function>(reinterpret_cast<Function*>(
7858 }
7859 
7860 
7861 template<typename T>
7863  return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
7864 }
7865 
7866 
7867 template<typename T>
7869  return Local<Object>(reinterpret_cast<Object*>(
7871 }
7872 
7873 
7874 template<typename T>
7876  return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
7877 }
7878 
7879 
7880 template<typename T>
7882  return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
7883 }
7884 
7885 
7886 template<typename T>
7889 }
7890 
7891 
7892 template<typename T>
7894  return is_construct_call_ & 0x1;
7895 }
7896 
7897 
7898 template<typename T>
7899 int FunctionCallbackInfo<T>::Length() const {
7900  return length_;
7901 }
7902 
7904  Local<Integer> resource_line_offset,
7905  Local<Integer> resource_column_offset,
7906  Local<Boolean> resource_is_shared_cross_origin,
7907  Local<Integer> script_id,
7908  Local<Boolean> resource_is_embedder_debug_script,
7909  Local<Value> source_map_url,
7910  Local<Boolean> resource_is_opaque)
7911  : resource_name_(resource_name),
7912  resource_line_offset_(resource_line_offset),
7913  resource_column_offset_(resource_column_offset),
7914  options_(!resource_is_embedder_debug_script.IsEmpty() &&
7915  resource_is_embedder_debug_script->IsTrue(),
7916  !resource_is_shared_cross_origin.IsEmpty() &&
7917  resource_is_shared_cross_origin->IsTrue(),
7918  !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()),
7919  script_id_(script_id),
7920  source_map_url_(source_map_url) {}
7921 
7922 Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
7923 
7924 
7926  return resource_line_offset_;
7927 }
7928 
7929 
7931  return resource_column_offset_;
7932 }
7933 
7934 
7935 Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
7936 
7937 
7938 Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
7939 
7940 
7942  CachedData* data)
7943  : source_string(string),
7944  resource_name(origin.ResourceName()),
7945  resource_line_offset(origin.ResourceLineOffset()),
7946  resource_column_offset(origin.ResourceColumnOffset()),
7947  resource_options(origin.Options()),
7948  source_map_url(origin.SourceMapUrl()),
7949  cached_data(data) {}
7950 
7951 
7953  CachedData* data)
7954  : source_string(string), cached_data(data) {}
7955 
7956 
7958  delete cached_data;
7959 }
7960 
7961 
7963  const {
7964  return cached_data;
7965 }
7966 
7967 
7968 Local<Boolean> Boolean::New(Isolate* isolate, bool value) {
7969  return value ? True(isolate) : False(isolate);
7970 }
7971 
7972 
7973 void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) {
7976  value);
7977 }
7978 
7979 
7981 #ifndef V8_ENABLE_CHECKS
7982  typedef internal::Object O;
7983  typedef internal::HeapObject HO;
7984  typedef internal::Internals I;
7985  O* obj = *reinterpret_cast<O**>(this);
7986  // Fast path: If the object is a plain JSObject, which is the common case, we
7987  // know where to find the internal fields and can return the value directly.
7988  if (I::GetInstanceType(obj) == I::kJSObjectType) {
7989  int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
7990  O* value = I::ReadField<O*>(obj, offset);
7991  O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value);
7992  return Local<Value>(reinterpret_cast<Value*>(result));
7993  }
7994 #endif
7995  return SlowGetInternalField(index);
7996 }
7997 
7998 
8000 #ifndef V8_ENABLE_CHECKS
8001  typedef internal::Object O;
8002  typedef internal::Internals I;
8003  O* obj = *reinterpret_cast<O**>(this);
8004  // Fast path: If the object is a plain JSObject, which is the common case, we
8005  // know where to find the internal fields and can return the value directly.
8007  int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
8008  return I::ReadField<void*>(obj, offset);
8009  }
8010 #endif
8011  return SlowGetAlignedPointerFromInternalField(index);
8012 }
8013 
8014 
8015 String* String::Cast(v8::Value* value) {
8016 #ifdef V8_ENABLE_CHECKS
8017  CheckCast(value);
8018 #endif
8019  return static_cast<String*>(value);
8020 }
8021 
8022 
8024  typedef internal::Object* S;
8025  typedef internal::Internals I;
8026  I::CheckInitialized(isolate);
8027  S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
8028  return Local<String>(reinterpret_cast<String*>(slot));
8029 }
8030 
8031 
8033  typedef internal::Object O;
8034  typedef internal::Internals I;
8035  O* obj = *reinterpret_cast<O* const*>(this);
8036  String::ExternalStringResource* result;
8038  void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8039  result = reinterpret_cast<String::ExternalStringResource*>(value);
8040  } else {
8041  result = NULL;
8042  }
8043 #ifdef V8_ENABLE_CHECKS
8044  VerifyExternalStringResource(result);
8045 #endif
8046  return result;
8047 }
8048 
8049 
8051  String::Encoding* encoding_out) const {
8052  typedef internal::Object O;
8053  typedef internal::Internals I;
8054  O* obj = *reinterpret_cast<O* const*>(this);
8056  *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
8057  ExternalStringResourceBase* resource = NULL;
8058  if (type == I::kExternalOneByteRepresentationTag ||
8060  void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8061  resource = static_cast<ExternalStringResourceBase*>(value);
8062  }
8063 #ifdef V8_ENABLE_CHECKS
8064  VerifyExternalStringResourceBase(resource, *encoding_out);
8065 #endif
8066  return resource;
8067 }
8068 
8069 
8070 bool Value::IsUndefined() const {
8071 #ifdef V8_ENABLE_CHECKS
8072  return FullIsUndefined();
8073 #else
8074  return QuickIsUndefined();
8075 #endif
8076 }
8077 
8078 bool Value::QuickIsUndefined() const {
8079  typedef internal::Object O;
8080  typedef internal::Internals I;
8081  O* obj = *reinterpret_cast<O* const*>(this);
8082  if (!I::HasHeapObjectTag(obj)) return false;
8083  if (I::GetInstanceType(obj) != I::kOddballType) return false;
8085 }
8086 
8087 
8088 bool Value::IsNull() const {
8089 #ifdef V8_ENABLE_CHECKS
8090  return FullIsNull();
8091 #else
8092  return QuickIsNull();
8093 #endif
8094 }
8095 
8096 bool Value::QuickIsNull() const {
8097  typedef internal::Object O;
8098  typedef internal::Internals I;
8099  O* obj = *reinterpret_cast<O* const*>(this);
8100  if (!I::HasHeapObjectTag(obj)) return false;
8101  if (I::GetInstanceType(obj) != I::kOddballType) return false;
8102  return (I::GetOddballKind(obj) == I::kNullOddballKind);
8103 }
8104 
8105 
8106 bool Value::IsString() const {
8107 #ifdef V8_ENABLE_CHECKS
8108  return FullIsString();
8109 #else
8110  return QuickIsString();
8111 #endif
8112 }
8113 
8114 bool Value::QuickIsString() const {
8115  typedef internal::Object O;
8116  typedef internal::Internals I;
8117  O* obj = *reinterpret_cast<O* const*>(this);
8118  if (!I::HasHeapObjectTag(obj)) return false;
8120 }
8121 
8122 
8123 template <class T> Value* Value::Cast(T* value) {
8124  return static_cast<Value*>(value);
8125 }
8126 
8127 
8128 Local<Boolean> Value::ToBoolean() const {
8130  .FromMaybe(Local<Boolean>());
8131 }
8132 
8133 
8134 Local<Number> Value::ToNumber() const {
8136  .FromMaybe(Local<Number>());
8137 }
8138 
8139 
8140 Local<String> Value::ToString() const {
8142  .FromMaybe(Local<String>());
8143 }
8144 
8145 
8146 Local<String> Value::ToDetailString() const {
8148  .FromMaybe(Local<String>());
8149 }
8150 
8151 
8152 Local<Object> Value::ToObject() const {
8154  .FromMaybe(Local<Object>());
8155 }
8156 
8157 
8158 Local<Integer> Value::ToInteger() const {
8160  .FromMaybe(Local<Integer>());
8161 }
8162 
8163 
8164 Local<Uint32> Value::ToUint32() const {
8166  .FromMaybe(Local<Uint32>());
8167 }
8168 
8169 
8170 Local<Int32> Value::ToInt32() const {
8172  .FromMaybe(Local<Int32>());
8173 }
8174 
8175 
8177 #ifdef V8_ENABLE_CHECKS
8178  CheckCast(value);
8179 #endif
8180  return static_cast<Boolean*>(value);
8181 }
8182 
8183 
8184 Name* Name::Cast(v8::Value* value) {
8185 #ifdef V8_ENABLE_CHECKS
8186  CheckCast(value);
8187 #endif
8188  return static_cast<Name*>(value);
8189 }
8190 
8191 
8192 Symbol* Symbol::Cast(v8::Value* value) {
8193 #ifdef V8_ENABLE_CHECKS
8194  CheckCast(value);
8195 #endif
8196  return static_cast<Symbol*>(value);
8197 }
8198 
8199 
8200 Number* Number::Cast(v8::Value* value) {
8201 #ifdef V8_ENABLE_CHECKS
8202  CheckCast(value);
8203 #endif
8204  return static_cast<Number*>(value);
8205 }
8206 
8207 
8209 #ifdef V8_ENABLE_CHECKS
8210  CheckCast(value);
8211 #endif
8212  return static_cast<Integer*>(value);
8213 }
8214 
8215 
8216 Int32* Int32::Cast(v8::Value* value) {
8217 #ifdef V8_ENABLE_CHECKS
8218  CheckCast(value);
8219 #endif
8220  return static_cast<Int32*>(value);
8221 }
8222 
8223 
8224 Uint32* Uint32::Cast(v8::Value* value) {
8225 #ifdef V8_ENABLE_CHECKS
8226  CheckCast(value);
8227 #endif
8228  return static_cast<Uint32*>(value);
8229 }
8230 
8231 
8232 Date* Date::Cast(v8::Value* value) {
8233 #ifdef V8_ENABLE_CHECKS
8234  CheckCast(value);
8235 #endif
8236  return static_cast<Date*>(value);
8237 }
8238 
8239 
8241 #ifdef V8_ENABLE_CHECKS
8242  CheckCast(value);
8243 #endif
8244  return static_cast<StringObject*>(value);
8245 }
8246 
8247 
8249 #ifdef V8_ENABLE_CHECKS
8250  CheckCast(value);
8251 #endif
8252  return static_cast<SymbolObject*>(value);
8253 }
8254 
8255 
8257 #ifdef V8_ENABLE_CHECKS
8258  CheckCast(value);
8259 #endif
8260  return static_cast<NumberObject*>(value);
8261 }
8262 
8263 
8265 #ifdef V8_ENABLE_CHECKS
8266  CheckCast(value);
8267 #endif
8268  return static_cast<BooleanObject*>(value);
8269 }
8270 
8271 
8272 RegExp* RegExp::Cast(v8::Value* value) {
8273 #ifdef V8_ENABLE_CHECKS
8274  CheckCast(value);
8275 #endif
8276  return static_cast<RegExp*>(value);
8277 }
8278 
8279 
8280 Object* Object::Cast(v8::Value* value) {
8281 #ifdef V8_ENABLE_CHECKS
8282  CheckCast(value);
8283 #endif
8284  return static_cast<Object*>(value);
8285 }
8286 
8287 
8288 Array* Array::Cast(v8::Value* value) {
8289 #ifdef V8_ENABLE_CHECKS
8290  CheckCast(value);
8291 #endif
8292  return static_cast<Array*>(value);
8293 }
8294 
8295 
8296 Map* Map::Cast(v8::Value* value) {
8297 #ifdef V8_ENABLE_CHECKS
8298  CheckCast(value);
8299 #endif
8300  return static_cast<Map*>(value);
8301 }
8302 
8303 
8304 Set* Set::Cast(v8::Value* value) {
8305 #ifdef V8_ENABLE_CHECKS
8306  CheckCast(value);
8307 #endif
8308  return static_cast<Set*>(value);
8309 }
8310 
8311 
8313 #ifdef V8_ENABLE_CHECKS
8314  CheckCast(value);
8315 #endif
8316  return static_cast<Promise*>(value);
8317 }
8318 
8319 
8320 Proxy* Proxy::Cast(v8::Value* value) {
8321 #ifdef V8_ENABLE_CHECKS
8322  CheckCast(value);
8323 #endif
8324  return static_cast<Proxy*>(value);
8325 }
8326 
8327 
8329 #ifdef V8_ENABLE_CHECKS
8330  CheckCast(value);
8331 #endif
8332  return static_cast<Promise::Resolver*>(value);
8333 }
8334 
8335 
8337 #ifdef V8_ENABLE_CHECKS
8338  CheckCast(value);
8339 #endif
8340  return static_cast<ArrayBuffer*>(value);
8341 }
8342 
8343 
8345 #ifdef V8_ENABLE_CHECKS
8346  CheckCast(value);
8347 #endif
8348  return static_cast<ArrayBufferView*>(value);
8349 }
8350 
8351 
8353 #ifdef V8_ENABLE_CHECKS
8354  CheckCast(value);
8355 #endif
8356  return static_cast<TypedArray*>(value);
8357 }
8358 
8359 
8361 #ifdef V8_ENABLE_CHECKS
8362  CheckCast(value);
8363 #endif
8364  return static_cast<Uint8Array*>(value);
8365 }
8366 
8367 
8369 #ifdef V8_ENABLE_CHECKS
8370  CheckCast(value);
8371 #endif
8372  return static_cast<Int8Array*>(value);
8373 }
8374 
8375 
8377 #ifdef V8_ENABLE_CHECKS
8378  CheckCast(value);
8379 #endif
8380  return static_cast<Uint16Array*>(value);
8381 }
8382 
8383 
8385 #ifdef V8_ENABLE_CHECKS
8386  CheckCast(value);
8387 #endif
8388  return static_cast<Int16Array*>(value);
8389 }
8390 
8391 
8393 #ifdef V8_ENABLE_CHECKS
8394  CheckCast(value);
8395 #endif
8396  return static_cast<Uint32Array*>(value);
8397 }
8398 
8399 
8401 #ifdef V8_ENABLE_CHECKS
8402  CheckCast(value);
8403 #endif
8404  return static_cast<Int32Array*>(value);
8405 }
8406 
8407 
8409 #ifdef V8_ENABLE_CHECKS
8410  CheckCast(value);
8411 #endif
8412  return static_cast<Float32Array*>(value);
8413 }
8414 
8415 
8417 #ifdef V8_ENABLE_CHECKS
8418  CheckCast(value);
8419 #endif
8420  return static_cast<Float64Array*>(value);
8421 }
8422 
8423 
8425 #ifdef V8_ENABLE_CHECKS
8426  CheckCast(value);
8427 #endif
8428  return static_cast<Uint8ClampedArray*>(value);
8429 }
8430 
8431 
8433 #ifdef V8_ENABLE_CHECKS
8434  CheckCast(value);
8435 #endif
8436  return static_cast<DataView*>(value);
8437 }
8438 
8439 
8441 #ifdef V8_ENABLE_CHECKS
8442  CheckCast(value);
8443 #endif
8444  return static_cast<SharedArrayBuffer*>(value);
8445 }
8446 
8447 
8449 #ifdef V8_ENABLE_CHECKS
8450  CheckCast(value);
8451 #endif
8452  return static_cast<Function*>(value);
8453 }
8454 
8455 
8457 #ifdef V8_ENABLE_CHECKS
8458  CheckCast(value);
8459 #endif
8460  return static_cast<External*>(value);
8461 }
8462 
8463 
8464 template<typename T>
8466  return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
8467 }
8468 
8469 
8470 template<typename T>
8472  return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
8473 }
8474 
8475 
8476 template<typename T>
8478  return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
8479 }
8480 
8481 
8482 template<typename T>
8484  return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
8485 }
8486 
8487 
8488 template<typename T>
8490  return ReturnValue<T>(&args_[kReturnValueIndex]);
8491 }
8492 
8493 template <typename T>
8495  typedef internal::Internals I;
8497 }
8498 
8499 
8501  typedef internal::Object* S;
8502  typedef internal::Internals I;
8503  I::CheckInitialized(isolate);
8504  S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
8505  return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
8506 }
8507 
8508 
8510  typedef internal::Object* S;
8511  typedef internal::Internals I;
8512  I::CheckInitialized(isolate);
8513  S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
8514  return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
8515 }
8516 
8517 
8519  typedef internal::Object* S;
8520  typedef internal::Internals I;
8521  I::CheckInitialized(isolate);
8522  S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
8523  return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
8524 }
8525 
8526 
8528  typedef internal::Object* S;
8529  typedef internal::Internals I;
8530  I::CheckInitialized(isolate);
8531  S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
8532  return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
8533 }
8534 
8535 
8536 void Isolate::SetData(uint32_t slot, void* data) {
8537  typedef internal::Internals I;
8538  I::SetEmbedderData(this, slot, data);
8539 }
8540 
8541 
8542 void* Isolate::GetData(uint32_t slot) {
8543  typedef internal::Internals I;
8544  return I::GetEmbedderData(this, slot);
8545 }
8546 
8547 
8549  typedef internal::Internals I;
8550  return I::kNumIsolateDataSlots;
8551 }
8552 
8553 
8555  int64_t change_in_bytes) {
8556  typedef internal::Internals I;
8557  int64_t* amount_of_external_allocated_memory =
8558  reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
8560  int64_t* amount_of_external_allocated_memory_at_last_global_gc =
8561  reinterpret_cast<int64_t*>(
8562  reinterpret_cast<uint8_t*>(this) +
8564  int64_t amount = *amount_of_external_allocated_memory + change_in_bytes;
8565  if (change_in_bytes > 0 &&
8566  amount - *amount_of_external_allocated_memory_at_last_global_gc >
8568  ReportExternalAllocationLimitReached();
8569  }
8570  *amount_of_external_allocated_memory = amount;
8571  return *amount_of_external_allocated_memory;
8572 }
8573 
8574 
8575 template<typename T>
8576 void Isolate::SetObjectGroupId(const Persistent<T>& object,
8577  UniqueId id) {
8578  TYPE_CHECK(Value, T);
8579  SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id);
8580 }
8581 
8582 
8583 template<typename T>
8585  const Persistent<T>& object) {
8586  TYPE_CHECK(Value, T);
8587  SetReferenceFromGroup(id,
8588  reinterpret_cast<v8::internal::Object**>(object.val_));
8589 }
8590 
8591 
8592 template<typename T, typename S>
8593 void Isolate::SetReference(const Persistent<T>& parent,
8594  const Persistent<S>& child) {
8595  TYPE_CHECK(Object, T);
8596  TYPE_CHECK(Value, S);
8597  SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_),
8598  reinterpret_cast<v8::internal::Object**>(child.val_));
8599 }
8600 
8601 
8603 #ifndef V8_ENABLE_CHECKS
8604  typedef internal::Object O;
8605  typedef internal::HeapObject HO;
8606  typedef internal::Internals I;
8607  HO* context = *reinterpret_cast<HO**>(this);
8608  O** result =
8609  HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
8610  return Local<Value>(reinterpret_cast<Value*>(result));
8611 #else
8612  return SlowGetEmbedderData(index);
8613 #endif
8614 }
8615 
8616 
8618 #ifndef V8_ENABLE_CHECKS
8619  typedef internal::Internals I;
8620  return I::ReadEmbedderData<void*>(this, index);
8621 #else
8622  return SlowGetAlignedPointerFromEmbedderData(index);
8623 #endif
8624 }
8625 
8626 
8627 void V8::SetAllowCodeGenerationFromStringsCallback(
8629  Isolate* isolate = Isolate::GetCurrent();
8631 }
8632 
8633 
8634 bool V8::IsDead() {
8635  Isolate* isolate = Isolate::GetCurrent();
8636  return isolate->IsDead();
8637 }
8638 
8639 
8640 bool V8::AddMessageListener(MessageCallback that, Local<Value> data) {
8641  Isolate* isolate = Isolate::GetCurrent();
8642  return isolate->AddMessageListener(that, data);
8643 }
8644 
8645 
8646 void V8::RemoveMessageListeners(MessageCallback that) {
8647  Isolate* isolate = Isolate::GetCurrent();
8648  isolate->RemoveMessageListeners(that);
8649 }
8650 
8651 
8652 void V8::SetFailedAccessCheckCallbackFunction(
8653  FailedAccessCheckCallback callback) {
8654  Isolate* isolate = Isolate::GetCurrent();
8656 }
8657 
8658 
8659 void V8::SetCaptureStackTraceForUncaughtExceptions(
8660  bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8661  Isolate* isolate = Isolate::GetCurrent();
8662  isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8663  options);
8664 }
8665 
8666 
8667 void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
8668  Isolate* isolate = Isolate::GetCurrent();
8669  isolate->SetFatalErrorHandler(callback);
8670 }
8671 
8672 
8673 void V8::RemoveGCPrologueCallback(GCCallback callback) {
8674  Isolate* isolate = Isolate::GetCurrent();
8676  reinterpret_cast<v8::Isolate::GCCallback>(callback));
8677 }
8678 
8679 
8680 void V8::RemoveGCEpilogueCallback(GCCallback callback) {
8681  Isolate* isolate = Isolate::GetCurrent();
8683  reinterpret_cast<v8::Isolate::GCCallback>(callback));
8684 }
8685 
8686 
8687 void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
8688  ObjectSpace space,
8689  AllocationAction action) {
8690  Isolate* isolate = Isolate::GetCurrent();
8691  isolate->AddMemoryAllocationCallback(callback, space, action);
8692 }
8693 
8694 
8695 void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
8696  Isolate* isolate = Isolate::GetCurrent();
8697  isolate->RemoveMemoryAllocationCallback(callback);
8698 }
8699 
8700 
8701 void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); }
8702 
8703 
8704 bool V8::IsExecutionTerminating(Isolate* isolate) {
8705  if (isolate == NULL) {
8706  isolate = Isolate::GetCurrent();
8707  }
8708  return isolate->IsExecutionTerminating();
8709 }
8710 
8711 
8712 void V8::CancelTerminateExecution(Isolate* isolate) {
8714 }
8715 
8716 
8717 void V8::VisitExternalResources(ExternalResourceVisitor* visitor) {
8718  Isolate* isolate = Isolate::GetCurrent();
8719  isolate->VisitExternalResources(visitor);
8720 }
8721 
8722 
8723 void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8724  Isolate* isolate = Isolate::GetCurrent();
8725  isolate->VisitHandlesWithClassIds(visitor);
8726 }
8727 
8728 
8729 void V8::VisitHandlesWithClassIds(Isolate* isolate,
8730  PersistentHandleVisitor* visitor) {
8731  isolate->VisitHandlesWithClassIds(visitor);
8732 }
8733 
8734 
8735 void V8::VisitHandlesForPartialDependence(Isolate* isolate,
8736  PersistentHandleVisitor* visitor) {
8738 }
8739 
8740 /**
8741  * \example shell.cc
8742  * A simple shell that takes a list of expressions on the
8743  * command-line and executes them.
8744  */
8745 
8746 
8747 /**
8748  * \example process.cc
8749  */
8750 
8751 
8752 } // namespace v8
8753 
8754 
8755 #undef TYPE_CHECK
8756 
8757 
8758 #endif // INCLUDE_V8_H_