v8  5.1.281(node6.10.3)
V8 is Google's open source JavaScript engine
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>
222  V8_INLINE Local(Local<S> that)
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;
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:
361  template <class S>
362  V8_INLINE MaybeLocal(Local<S> that)
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>
370  V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
371  out->val_ = IsEmpty() ? nullptr : this->val_;
372  return !IsEmpty();
373  }
374 
375  // Will crash if the MaybeLocal<> is empty.
376  V8_INLINE Local<T> ToLocalChecked();
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>
593  V8_INLINE P* ClearWeak();
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>
702  typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
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  */
725  V8_INLINE Persistent() : PersistentBase<T>(0) { }
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  */
752  V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) {
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  */
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 
901  ~HandleScope();
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:
981  SealHandleScope(Isolate* isolate);
982  ~SealHandleScope();
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;
1057  V8_INLINE Local<Integer> ResourceLineOffset() const;
1058  V8_INLINE Local<Integer> ResourceColumnOffset() 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  */
1084  Local<Script> BindToCurrentContext();
1085 
1086  int GetId();
1087  Local<Value> GetScriptName();
1088 
1089  /**
1090  * Data read from magic sourceURL comments.
1091  */
1092  Local<Value> GetSourceURL();
1093  /**
1094  * Data read from magic sourceMappingURL comments.
1095  */
1096  Local<Value> GetSourceMappingURL();
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  */
1117  static V8_DEPRECATE_SOON(
1118  "Use maybe version",
1119  Local<Script> Compile(Local<String> source,
1120  ScriptOrigin* origin = nullptr));
1121  static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
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());
1135  V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
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  */
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);
1174  ~CachedData();
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);
1283  ~StreamedSource();
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  */
1350  static V8_DEPRECATED(
1351  "Use maybe version",
1352  Local<Script> Compile(Isolate* isolate, Source* source,
1353  CompileOptions options = kNoCompileOptions));
1354  static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
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));
1385  static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
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  */
1419  static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule(
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  */
1466  ScriptOrigin GetScriptOrigin() const;
1467 
1468  /**
1469  * Returns the resource name for the script from where the function causing
1470  * the error originates.
1471  */
1472  Local<Value> GetScriptResourceName() const;
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  */
1479  Local<StackTrace> GetStackTrace() const;
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);
1485  V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) 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);
1504  V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) 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);
1511  V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) 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  */
1567  Local<Array> AsArray();
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  */
1576  static Local<StackTrace> CurrentStackTrace(
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  */
1617  Local<String> GetScriptName() const;
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  */
1625  Local<String> GetScriptNameOrSourceURL() const;
1626 
1627  /**
1628  * Returns the name of the function associated with this stack frame.
1629  */
1630  Local<String> GetFunctionName() const;
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));
1681  static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
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);
1695  Local<Value> Get(Local<Value> key);
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;
1989  V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(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 
2028  V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const;
2029  V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
2030  V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
2031  Local<Context> context) const;
2032  V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
2033  Local<Context> context) const;
2034  V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(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);
2044  V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
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  */
2094  int GetIdentityHash();
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 {
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.
2312  kNormalString = static_cast<int>(v8::NewStringType::kNormal),
2314  };
2315 
2316  /** Allocates a new string from UTF-8 data.*/
2317  static V8_DEPRECATE_SOON(
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. **/
2325  static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8(
2326  Isolate* isolate, const char* data, v8::NewStringType type,
2327  int length = -1);
2328 
2329  /** Allocates a new string from Latin-1 data.*/
2330  static V8_DEPRECATED(
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. **/
2338  static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte(
2339  Isolate* isolate, const uint8_t* data, v8::NewStringType type,
2340  int length = -1);
2341 
2342  /** Allocates a new string from UTF-16 data.*/
2343  static V8_DEPRECATE_SOON(
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. **/
2351  static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte(
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));
2372  static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte(
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  */
2384  bool MakeExternal(ExternalStringResource* resource);
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  */
2394  static V8_DEPRECATE_SOON(
2395  "Use maybe version",
2396  Local<String> NewExternal(Isolate* isolate,
2397  ExternalOneByteStringResource* resource));
2398  static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte(
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  */
2415  bool CanMakeExternal();
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);
2427  ~Utf8Value();
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);
2499  static Local<Symbol> GetIsConcatSpreadable(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,
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));
2648  V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
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.
2663  V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2664  Local<Name> key,
2665  Local<Value> value);
2666  V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
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));
2697  V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2698  Local<Value> key);
2699 
2700  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2701  V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
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));
2723  V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
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,
2755  MaybeLocal<Value> data = MaybeLocal<Value>(),
2756  AccessControl settings = DEFAULT,
2757  PropertyAttribute attribute = None);
2758 
2759  void SetAccessorProperty(Local<Name> name, Local<Function> getter,
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);
2773  Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
2774  MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
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  */
2800  Local<Value> GetPrototype();
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));
2808  V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
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  */
2829  Local<String> GetConstructorName();
2830 
2831  /** Gets the number of internal fields for this Object. */
2832  int InternalFieldCount();
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));
2868  V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2869  Local<Name> key);
2870  V8_DEPRECATE_SOON("Use maybe version",
2871  bool HasRealNamedProperty(Local<String> key));
2872  V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
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  */
2887  V8_DEPRECATED(
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  */
2898  V8_DEPRECATED(
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  */
2940  int GetIdentityHash();
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?
2954  Local<Object> Clone();
2955 
2956  /**
2957  * Returns the context in which the object was created.
2958  */
2959  Local<Context> CreationContext();
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[]));
2975  V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context,
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();
3044  V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
3045  Local<Value> key);
3046  V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
3047  Local<Value> key,
3048  Local<Value> value);
3049  V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3050  Local<Value> key);
3051  V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
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();
3080  V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
3081  Local<Value> key);
3082  V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3083  Local<Value> key);
3084  V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
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>
3106 class ReturnValue {
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>
3156 class FunctionCallbackInfo {
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;
3167  V8_INLINE ReturnValue<T> GetReturnValue() const;
3168  // This shouldn't be public, but the arm compiler needs it.
3169  static const int kArgsLength = 7;
3170 
3171  protected:
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>
3198 class PropertyCallbackInfo {
3199  public:
3200  V8_INLINE Isolate* GetIsolate() const;
3201  V8_INLINE Local<Value> Data() const;
3202  V8_INLINE Local<Object> This() const;
3203  V8_INLINE Local<Object> Holder() const;
3204  V8_INLINE ReturnValue<T> GetReturnValue() const;
3205  V8_INLINE bool ShouldThrowOnError() const;
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;
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  */
3238  static MaybeLocal<Function> New(Local<Context> context,
3239  FunctionCallback callback,
3240  Local<Value> data = Local<Value>(),
3241  int length = 0);
3242  static V8_DEPRECATE_SOON(
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[]));
3261  V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
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  */
3274  Local<Value> GetInferredName() const;
3275 
3276  /**
3277  * displayName if it is set, otherwise name if it is configured, otherwise
3278  * function name, otherwise inferred name.
3279  */
3280  Local<Value> GetDebugName() const;
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  */
3286  Local<Value> GetDisplayName() const;
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  */
3297  int GetScriptColumnNumber() const;
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  */
3313  Local<Value> GetBoundFunction() const;
3314 
3315  ScriptOrigin GetScriptOrigin() const;
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));
3338  static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New(
3339  Local<Context> context);
3340 
3341  /**
3342  * Extract the associated promise.
3343  */
3344  Local<Promise> GetPromise();
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",
3374  V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Chain(
3375  Local<Context> context, Local<Function> handler));
3376 
3377  V8_DEPRECATED("Use maybe version",
3378  Local<Promise> Catch(Local<Function> handler));
3379  V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context,
3380  Local<Function> handler);
3381 
3382  V8_DEPRECATED("Use maybe version",
3383  Local<Promise> Then(Local<Function> handler));
3384  V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
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:
3407  Local<Object> GetTarget();
3408  Local<Value> GetHandler();
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  */
3515  static Local<ArrayBuffer> New(
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  */
3588  Local<ArrayBuffer> Buffer();
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  */
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:
3671  static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
3672  size_t byte_offset, size_t length);
3673  static Local<Uint8ClampedArray> New(
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  */
3878  static Local<SharedArrayBuffer> New(
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));
3933  static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
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  */
3956  static void DateTimeConfigurationChangeNotification(Isolate* isolate);
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 
4003  Local<String> ValueOf() const;
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 
4021  Local<Symbol> ValueOf() const;
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,
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));
4061  static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
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  */
4069  Local<String> GetSource() const;
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 
4119  void SetAccessorProperty(
4120  Local<Name> name,
4121  Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
4122  Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
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  */
4153  void SetNativeDataProperty(
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,
4158  Local<AccessorSignature> signature = Local<AccessorSignature>(),
4159  AccessControl settings = DEFAULT);
4160  void SetNativeDataProperty(
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,
4165  Local<AccessorSignature> signature = Local<AccessorSignature>(),
4166  AccessControl settings = DEFAULT);
4167 
4168  /**
4169  * During template instantiation, sets the value with the intrinsic property
4170  * from the correct context.
4171  */
4172  void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
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  */
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.*/
4461  static Local<FunctionTemplate> New(
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  */
4470  static Local<FunctionTemplate> NewWithFastHandler(
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  */
4486  void SetCallHandler(
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.*/
4497  void Inherit(Local<FunctionTemplate> parent);
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  */
4510  void SetClassName(Local<String> name);
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  */
4537  void ReadOnlyPrototype();
4538 
4539  /**
4540  * Removes the prototype property from functions created from this
4541  * FunctionTemplate.
4542  */
4543  void RemovePrototype();
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 
4594  Local<Value> data;
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 
4622  Local<Value> data;
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. */
4636  static Local<ObjectTemplate> New(
4637  Isolate* isolate,
4638  Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
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());
4643  V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
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  */
4674  void SetAccessor(
4675  Local<String> name, AccessorGetterCallback getter,
4676  AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4677  AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4678  Local<AccessorSignature> signature = Local<AccessorSignature>());
4679  void SetAccessor(
4680  Local<Name> name, AccessorNameGetterCallback getter,
4681  AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4682  AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4683  Local<AccessorSignature> signature = Local<AccessorSignature>());
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  */
4730  void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
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  */
4759  void MarkAsUndetectable();
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>());
4771  V8_DEPRECATED(
4772  "Use SetAccessCheckCallback with new AccessCheckCallback signature.",
4773  void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback,
4774  Local<Value> data = Local<Value>()));
4775 
4776  V8_DEPRECATED(
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  */
4786  int InternalFieldCount();
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:
4807  static Local<Signature> New(
4808  Isolate* isolate,
4809  Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
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:
4822  static Local<AccessorSignature> New(
4823  Isolate* isolate,
4824  Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
4825 
4826  private:
4827  AccessorSignature();
4828 };
4829 
4830 
4831 // --- Extensions ---
4832 
4835  public:
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) {
4862  return v8::Local<v8::FunctionTemplate>();
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 
4888 void V8_EXPORT RegisterExtension(Extension* extension);
4889 
4890 
4891 // --- Statics ---
4892 
4893 V8_INLINE Local<Primitive> Undefined(Isolate* isolate);
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);
4974  static Local<Value> ReferenceError(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  */
4992  static Local<StackTrace> GetStackTrace(Local<Value> exception);
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);
5096  ~MicrotasksScope();
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  */
5129 typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
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&