blob: 3f02ce8c056ff195819be8305e42174a1066c54a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#ifndef INCLUDE_V8_H_
16#define INCLUDE_V8_H_
Steve Blocka7e24c12009-10-30 11:49:00 +000017
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include <stddef.h>
19#include <stdint.h>
20#include <stdio.h>
Ben Murdochda12d292016-06-02 14:46:10 +010021#include <utility>
22#include <vector>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024#include "v8-version.h" // NOLINT(build/include)
25#include "v8config.h" // NOLINT(build/include)
Steve Blocka7e24c12009-10-30 11:49:00 +000026
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027// 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
Steve Blocka7e24c12009-10-30 11:49:00 +000031
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.
Steve Blocka7e24c12009-10-30 11:49:00 +000037#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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043# define V8_EXPORT __declspec(dllexport)
Steve Blocka7e24c12009-10-30 11:49:00 +000044#elif USING_V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045# define V8_EXPORT __declspec(dllimport)
Steve Blocka7e24c12009-10-30 11:49:00 +000046#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047# define V8_EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000048#endif // BUILDING_V8_SHARED
49
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050#else // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000051
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052// 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
Steve Blocka7e24c12009-10-30 11:49:00 +000062
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063#endif // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65/**
66 * The v8 JavaScript engine.
67 */
68namespace v8 {
69
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070class AccessorSignature;
71class Array;
72class Boolean;
73class BooleanObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000074class Context;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075class CpuProfiler;
76class Data;
77class Date;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078class External;
79class Function;
80class FunctionTemplate;
81class HeapProfiler;
82class ImplementationUtilities;
83class Int32;
84class Integer;
85class Isolate;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086template <class T>
87class Maybe;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088class Name;
Steve Blocka7e24c12009-10-30 11:49:00 +000089class Number;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090class NumberObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000091class Object;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092class ObjectOperationDescriptor;
93class ObjectTemplate;
94class Platform;
Steve Blocka7e24c12009-10-30 11:49:00 +000095class Primitive;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096class Promise;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097class Proxy;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098class RawOperationDescriptor;
99class Script;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100class SharedArrayBuffer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000101class Signature;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102class StartupData;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103class StackFrame;
104class StackTrace;
105class String;
106class StringObject;
107class Symbol;
108class SymbolObject;
109class Private;
110class Uint32;
111class Utils;
112class Value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113template <class T> class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114template <class T>
115class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116template <class T> class Eternal;
117template<class T> class NonCopyablePersistentTraits;
118template<class T> class PersistentBase;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119template <class T, class M = NonCopyablePersistentTraits<T> >
120class Persistent;
121template <class T>
122class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123template<class K, class V, class T> class PersistentValueMap;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124template <class K, class V, class T>
125class PersistentValueMapBase;
126template <class K, class V, class T>
127class GlobalValueMap;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128template<class V, class T> class PersistentValueVector;
129template<class T, class P> class WeakCallbackObject;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130class FunctionTemplate;
131class ObjectTemplate;
132class Data;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133template<typename T> class FunctionCallbackInfo;
134template<typename T> class PropertyCallbackInfo;
Kristian Monsen25f61362010-05-21 11:50:48 +0100135class StackTrace;
136class StackFrame;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138class CallHandlerHelper;
139class EscapableHandleScope;
140template<typename T> class ReturnValue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142namespace experimental {
143class FastAccessorBuilder;
144} // namespace experimental
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +0000147class Arguments;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100148class Heap;
Steve Block44f0eee2011-05-26 01:26:41 +0100149class HeapObject;
150class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151class Object;
152struct StreamedSource;
153template<typename T> class CustomArguments;
154class PropertyCallbackArguments;
155class FunctionCallbackArguments;
156class GlobalHandles;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159
Steve Blocka7e24c12009-10-30 11:49:00 +0000160/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 * General purpose unique identifier.
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163class UniqueId {
164 public:
165 explicit UniqueId(intptr_t data)
166 : data_(data) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 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};
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
Ben Murdoch257744e2011-11-30 15:57:28 +0000184// --- Handles ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100186#define TYPE_CHECK(T, S) \
187 while (false) { \
188 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 }
190
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192/**
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 * a Local<Object>); the value will still be governed by a handle
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 * behind the scenes and the same rules apply to these values as to
215 * their handles.
216 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217template <class T>
218class Local {
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 V8_INLINE Local() : val_(0) {}
221 template <class S>
222 V8_INLINE Local(Local<S> that)
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 : val_(reinterpret_cast<T*>(*that)) {
224 /**
225 * This check fails when trying to convert between incompatible
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 * handles. For example, converting from a Local<String> to a
227 * Local<Number>.
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 */
229 TYPE_CHECK(T, S);
230 }
231
232 /**
233 * Returns true if the handle is empty.
234 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 V8_INLINE bool IsEmpty() const { return val_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000236
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 /**
238 * Sets the handle to be empty. IsEmpty() will then return true.
239 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 V8_INLINE void Clear() { val_ = 0; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000241
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 V8_INLINE T* operator->() const { return val_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000243
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 V8_INLINE T* operator*() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000245
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 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 template <class S>
253 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 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_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 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 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 template <class S>
277 V8_INLINE bool operator!=(const Local<S>& that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 return !operator==(that);
279 }
280
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 template <class S> V8_INLINE bool operator!=(
282 const Persistent<S>& that) const {
283 return !operator==(that);
284 }
285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 template <class S> V8_INLINE static Local<T> Cast(Local<S> that) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000287#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 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294
Steve Blocka7e24c12009-10-30 11:49:00 +0000295
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 template <class S> V8_INLINE Local<S> As() {
Steve Block6ded16b2010-05-10 14:33:55 +0100297 return Local<S>::Cast(*this);
298 }
299
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 /**
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.
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305 V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 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;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 template<class F> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 template <class F>
316 friend class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 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;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 friend class Private;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 template<class F> friend class internal::CustomArguments;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 friend Local<Primitive> Undefined(Isolate* isolate);
325 friend Local<Primitive> Null(Isolate* isolate);
326 friend Local<Boolean> True(Isolate* isolate);
327 friend Local<Boolean> False(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 friend class HandleScope;
329 friend class EscapableHandleScope;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 template <class F1, class F2, class F3>
331 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 template<class F1, class F2> friend class PersistentValueVector;
Ben Murdochda12d292016-06-02 14:46:10 +0100333 template <class F>
334 friend class ReturnValue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335
Ben Murdoch097c5b22016-05-18 11:27:45 +0100336 explicit V8_INLINE Local(T* that) : val_(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 V8_INLINE static Local<T> New(Isolate* isolate, T* that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 T* val_;
339};
340
341
342#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
343// Local is an alias for Local for historical reasons.
344template <class T>
345using Handle = Local<T>;
346#endif
347
348
349/**
350 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
351 * the Local<> is empty before it can be used.
352 *
353 * If an API method returns a MaybeLocal<>, the API method can potentially fail
354 * either because an exception is thrown, or because an exception is pending,
355 * e.g. because a previous API call threw an exception that hasn't been caught
356 * yet, or because a TerminateExecution exception was thrown. In that case, an
357 * empty MaybeLocal is returned.
358 */
359template <class T>
360class MaybeLocal {
361 public:
362 V8_INLINE MaybeLocal() : val_(nullptr) {}
363 template <class S>
364 V8_INLINE MaybeLocal(Local<S> that)
365 : val_(reinterpret_cast<T*>(*that)) {
366 TYPE_CHECK(T, S);
367 }
368
369 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
370
371 template <class S>
372 V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
373 out->val_ = IsEmpty() ? nullptr : this->val_;
374 return !IsEmpty();
375 }
376
377 // Will crash if the MaybeLocal<> is empty.
378 V8_INLINE Local<T> ToLocalChecked();
379
380 template <class S>
381 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
382 return IsEmpty() ? default_value : Local<S>(val_);
383 }
384
385 private:
386 T* val_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387};
388
389
390// Eternal handles are set-once handles that live for the life of the isolate.
391template <class T> class Eternal {
392 public:
393 V8_INLINE Eternal() : index_(kInitialValue) { }
394 template<class S>
395 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) {
396 Set(isolate, handle);
397 }
398 // Can only be safely called if already set.
399 V8_INLINE Local<T> Get(Isolate* isolate);
400 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
401 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
402
403 private:
404 static const int kInitialValue = -1;
405 int index_;
406};
407
408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409static const int kInternalFieldsInWeakCallback = 2;
410
411
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400412template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413class WeakCallbackInfo {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400414 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 typedef void (*Callback)(const WeakCallbackInfo<T>& data);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 WeakCallbackInfo(Isolate* isolate, T* parameter,
418 void* internal_fields[kInternalFieldsInWeakCallback],
419 Callback* callback)
420 : isolate_(isolate), parameter_(parameter), callback_(callback) {
421 for (int i = 0; i < kInternalFieldsInWeakCallback; ++i) {
422 internal_fields_[i] = internal_fields[i];
423 }
424 }
425
426 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400427 V8_INLINE T* GetParameter() const { return parameter_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000428 V8_INLINE void* GetInternalField(int index) const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 V8_INLINE V8_DEPRECATED("use indexed version",
431 void* GetInternalField1() const) {
432 return internal_fields_[0];
433 }
434 V8_INLINE V8_DEPRECATED("use indexed version",
435 void* GetInternalField2() const) {
436 return internal_fields_[1];
437 }
438
Ben Murdoch097c5b22016-05-18 11:27:45 +0100439 V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.",
440 bool IsFirstPass() const) {
441 return callback_ != nullptr;
442 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443
444 // When first called, the embedder MUST Reset() the Global which triggered the
445 // callback. The Global itself is unusable for anything else. No v8 other api
446 // calls may be called in the first callback. Should additional work be
447 // required, the embedder must set a second pass callback, which will be
448 // called after all the initial callbacks are processed.
449 // Calling SetSecondPassCallback on the second pass will immediately crash.
450 void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400451
452 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 Isolate* isolate_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400454 T* parameter_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455 Callback* callback_;
456 void* internal_fields_[kInternalFieldsInWeakCallback];
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400457};
458
459
Ben Murdochc5610432016-08-08 18:44:38 +0100460// kParameter will pass a void* parameter back to the callback, kInternalFields
461// will pass the first two internal fields back to the callback, kFinalizer
462// will pass a void* parameter back, but is invoked before the object is
463// actually collected, so it can be resurrected. In the last case, it is not
464// possible to request a second pass callback.
465enum class WeakCallbackType { kParameter, kInternalFields, kFinalizer };
Steve Blocka7e24c12009-10-30 11:49:00 +0000466
467/**
468 * An object reference that is independent of any handle scope. Where
469 * a Local handle only lives as long as the HandleScope in which it was
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 * allocated, a PersistentBase handle remains valid until it is explicitly
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 * disposed.
472 *
473 * A persistent handle contains a reference to a storage cell within
474 * the v8 engine which holds an object value and which is updated by
475 * the garbage collector whenever the object is moved. A new storage
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 * cell can be created using the constructor or PersistentBase::Reset and
477 * existing handles can be disposed using PersistentBase::Reset.
478 *
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480template <class T> class PersistentBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 * If non-empty, destroy the underlying storage cell
484 * IsEmpty() will return true after this call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 V8_INLINE void Reset();
487 /**
488 * If non-empty, destroy the underlying storage cell
489 * and create a new one with the contents of other if other is non empty
490 */
491 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
494 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495 * If non-empty, destroy the underlying storage cell
496 * and create a new one with the contents of other if other is non empty
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 template <class S>
499 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
500
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400501 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
502 V8_INLINE void Empty() { val_ = 0; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000504 V8_INLINE Local<T> Get(Isolate* isolate) const {
505 return Local<T>::New(isolate, *this);
506 }
507
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 template <class S>
509 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
510 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
511 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400512 if (a == NULL) return b == NULL;
513 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 return *a == *b;
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 }
516
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000517 template <class S>
518 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
520 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400521 if (a == NULL) return b == NULL;
522 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 return *a == *b;
524 }
525
526 template <class S>
527 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
528 return !operator==(that);
529 }
530
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000531 template <class S>
532 V8_INLINE bool operator!=(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 return !operator==(that);
534 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000535
536 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000537 * Install a finalization callback on this object.
538 * NOTE: There is no guarantee as to *when* or even *if* the callback is
539 * invoked. The invocation is performed solely on a best effort basis.
540 * As always, GC-based finalization should *not* be relied upon for any
541 * critical form of resource management!
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 template <typename P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000544 V8_INLINE void SetWeak(P* parameter,
545 typename WeakCallbackInfo<P>::Callback callback,
546 WeakCallbackType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400547
Ben Murdochc5610432016-08-08 18:44:38 +0100548 /**
549 * Turns this handle into a weak phantom handle without finalization callback.
550 * The handle will be reset automatically when the garbage collector detects
551 * that the object is no longer reachable.
552 * A related function Isolate::NumberOfPhantomHandleResetsSinceLastCall
553 * returns how many phantom handles were reset by the garbage collector.
554 */
555 V8_INLINE void SetWeak();
556
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 template<typename P>
558 V8_INLINE P* ClearWeak();
559
560 // TODO(dcarney): remove this.
561 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
562
563 /**
Ben Murdochda12d292016-06-02 14:46:10 +0100564 * Allows the embedder to tell the v8 garbage collector that a certain object
565 * is alive. Only allowed when the embedder is asked to trace its heap by
566 * EmbedderHeapTracer.
567 */
Ben Murdochc5610432016-08-08 18:44:38 +0100568 V8_INLINE void RegisterExternalReference(Isolate* isolate) const;
Ben Murdochda12d292016-06-02 14:46:10 +0100569
570 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 * Marks the reference to this object independent. Garbage collector is free
572 * to ignore any object groups containing this object. Weak callback for an
573 * independent handle should not assume that it will be preceded by a global
574 * GC prologue callback or followed by a global GC epilogue callback.
575 */
576 V8_INLINE void MarkIndependent();
577
578 /**
579 * Marks the reference to this object partially dependent. Partially dependent
580 * handles only depend on other partially dependent handles and these
581 * dependencies are provided through object groups. It provides a way to build
582 * smaller object groups for young objects that represent only a subset of all
583 * external dependencies. This mark is automatically cleared after each
584 * garbage collection.
585 */
Ben Murdochc5610432016-08-08 18:44:38 +0100586 V8_INLINE V8_DEPRECATED(
587 "deprecated optimization, do not use partially dependent groups",
588 void MarkPartiallyDependent());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000590 /**
591 * Marks the reference to this object as active. The scavenge garbage
592 * collection should not reclaim the objects marked as active.
593 * This bit is cleared after the each garbage collection pass.
594 */
595 V8_INLINE void MarkActive();
596
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 V8_INLINE bool IsIndependent() const;
598
599 /** Checks if the handle holds the only reference to an object. */
600 V8_INLINE bool IsNearDeath() const;
601
602 /** Returns true if the handle's reference is weak. */
603 V8_INLINE bool IsWeak() const;
604
605 /**
606 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
607 * description in v8-profiler.h for details.
608 */
609 V8_INLINE void SetWrapperClassId(uint16_t class_id);
610
611 /**
612 * Returns the class ID previously assigned to this handle or 0 if no class ID
613 * was previously assigned.
614 */
615 V8_INLINE uint16_t WrapperClassId() const;
616
617 private:
618 friend class Isolate;
619 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620 template<class F> friend class Local;
621 template<class F1, class F2> friend class Persistent;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000622 template <class F>
623 friend class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 template<class F> friend class PersistentBase;
625 template<class F> friend class ReturnValue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000626 template <class F1, class F2, class F3>
627 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 template<class F1, class F2> friend class PersistentValueVector;
629 friend class Object;
630
631 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000632 PersistentBase(const PersistentBase& other) = delete; // NOLINT
633 void operator=(const PersistentBase&) = delete;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 V8_INLINE static T* New(Isolate* isolate, T* that);
635
636 T* val_;
637};
638
639
640/**
641 * Default traits for Persistent. This class does not allow
642 * use of the copy constructor or assignment operator.
643 * At present kResetInDestructor is not set, but that will change in a future
644 * version.
645 */
646template<class T>
647class NonCopyablePersistentTraits {
648 public:
649 typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
650 static const bool kResetInDestructor = false;
651 template<class S, class M>
652 V8_INLINE static void Copy(const Persistent<S, M>& source,
653 NonCopyablePersistent* dest) {
654 Uncompilable<Object>();
655 }
656 // TODO(dcarney): come up with a good compile error here.
657 template<class O> V8_INLINE static void Uncompilable() {
658 TYPE_CHECK(O, Primitive);
659 }
660};
661
662
663/**
664 * Helper class traits to allow copying and assignment of Persistent.
665 * This will clone the contents of storage cell, but not any of the flags, etc.
666 */
667template<class T>
668struct CopyablePersistentTraits {
669 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
670 static const bool kResetInDestructor = true;
671 template<class S, class M>
672 static V8_INLINE void Copy(const Persistent<S, M>& source,
673 CopyablePersistent* dest) {
674 // do nothing, just allow copy
675 }
676};
677
678
679/**
680 * A PersistentBase which allows copy and assignment.
681 *
682 * Copy, assignment and destructor bevavior is controlled by the traits
683 * class M.
684 *
685 * Note: Persistent class hierarchy is subject to future changes.
686 */
687template <class T, class M> class Persistent : public PersistentBase<T> {
688 public:
689 /**
690 * A Persistent with no storage cell.
691 */
692 V8_INLINE Persistent() : PersistentBase<T>(0) { }
693 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 * Construct a Persistent from a Local.
695 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 * pointing to the same object, and no flags are set.
697 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698 template <class S>
699 V8_INLINE Persistent(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
701 TYPE_CHECK(T, S);
702 }
703 /**
704 * Construct a Persistent from a Persistent.
705 * When the Persistent is non-empty, a new storage cell is created
706 * pointing to the same object, and no flags are set.
707 */
708 template <class S, class M2>
709 V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that)
710 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
711 TYPE_CHECK(T, S);
712 }
713 /**
714 * The copy constructors and assignment operator create a Persistent
715 * exactly as the Persistent constructor, but the Copy function from the
716 * traits class is called, allowing the setting of flags based on the
717 * copied Persistent.
718 */
719 V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) {
720 Copy(that);
721 }
722 template <class S, class M2>
723 V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) {
724 Copy(that);
725 }
726 V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT
727 Copy(that);
728 return *this;
729 }
730 template <class S, class M2>
731 V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT
732 Copy(that);
733 return *this;
734 }
735 /**
736 * The destructor will dispose the Persistent based on the
737 * kResetInDestructor flags in the traits class. Since not calling dispose
738 * can result in a memory leak, it is recommended to always set this flag.
739 */
740 V8_INLINE ~Persistent() {
741 if (M::kResetInDestructor) this->Reset();
742 }
743
744 // TODO(dcarney): this is pretty useless, fix or remove
745 template <class S>
746 V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000747#ifdef V8_ENABLE_CHECKS
748 // If we're going to perform the type check then we have to check
749 // that the handle isn't empty before doing the checked cast.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750 if (!that.IsEmpty()) T::Cast(*that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000751#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000752 return reinterpret_cast<Persistent<T>&>(that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 }
754
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 // TODO(dcarney): this is pretty useless, fix or remove
756 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
Steve Block6ded16b2010-05-10 14:33:55 +0100757 return Persistent<S>::Cast(*this);
758 }
759
Steve Blocka7e24c12009-10-30 11:49:00 +0000760 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 friend class Isolate;
762 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763 template<class F> friend class Local;
764 template<class F1, class F2> friend class Persistent;
765 template<class F> friend class ReturnValue;
766
Ben Murdoch097c5b22016-05-18 11:27:45 +0100767 explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 V8_INLINE T* operator*() const { return this->val_; }
769 template<class S, class M2>
770 V8_INLINE void Copy(const Persistent<S, M2>& that);
771};
772
773
774/**
775 * A PersistentBase which has move semantics.
776 *
777 * Note: Persistent class hierarchy is subject to future changes.
778 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000779template <class T>
780class Global : public PersistentBase<T> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781 public:
782 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000783 * A Global with no storage cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000785 V8_INLINE Global() : PersistentBase<T>(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000787 * Construct a Global from a Local.
788 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 * pointing to the same object, and no flags are set.
790 */
791 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000792 V8_INLINE Global(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
794 TYPE_CHECK(T, S);
795 }
796 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000797 * Construct a Global from a PersistentBase.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 * When the Persistent is non-empty, a new storage cell is created
799 * pointing to the same object, and no flags are set.
800 */
801 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that)
803 : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 TYPE_CHECK(T, S);
805 }
806 /**
807 * Move constructor.
808 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000809 V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT
810 other.val_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000811 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000812 V8_INLINE ~Global() { this->Reset(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 /**
814 * Move via assignment.
815 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 template <class S>
817 V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 TYPE_CHECK(T, S);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000819 if (this != &rhs) {
820 this->Reset();
821 this->val_ = rhs.val_;
822 rhs.val_ = nullptr;
823 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000824 return *this;
825 }
826 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 * Pass allows returning uniques from functions, etc.
828 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829 Global Pass() { return static_cast<Global&&>(*this); } // NOLINT
830
831 /*
832 * For compatibility with Chromium's base::Bind (base::Passed).
833 */
834 typedef void MoveOnlyTypeForCPP03;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000835
836 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837 template <class F>
838 friend class ReturnValue;
839 Global(const Global&) = delete;
840 void operator=(const Global&) = delete;
841 V8_INLINE T* operator*() const { return this->val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000842};
843
844
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000845// UniquePersistent is an alias for Global for historical reason.
846template <class T>
847using UniquePersistent = Global<T>;
848
849
Steve Blocka7e24c12009-10-30 11:49:00 +0000850 /**
851 * A stack-allocated class that governs a number of local handles.
852 * After a handle scope has been created, all local handles will be
853 * allocated within that handle scope until either the handle scope is
854 * deleted or another handle scope is created. If there is already a
855 * handle scope and a new one is created, all allocations will take
856 * place in the new handle scope until it is deleted. After that,
857 * new handles will again be allocated in the original handle scope.
858 *
859 * After the handle scope of a local handle has been deleted the
860 * garbage collector will no longer track the object stored in the
861 * handle and may deallocate it. The behavior of accessing a handle
862 * for which the handle scope has been deleted is undefined.
863 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000864class V8_EXPORT HandleScope {
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100866 explicit HandleScope(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000867
868 ~HandleScope();
869
870 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 * Counts the number of allocated handles.
872 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 static int NumberOfHandles(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000874
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 V8_INLINE Isolate* GetIsolate() const {
876 return reinterpret_cast<Isolate*>(isolate_);
877 }
878
879 protected:
880 V8_INLINE HandleScope() {}
881
882 void Initialize(Isolate* isolate);
883
884 static internal::Object** CreateHandle(internal::Isolate* isolate,
885 internal::Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886
887 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 // Uses heap_object to obtain the current Isolate.
889 static internal::Object** CreateHandle(internal::HeapObject* heap_object,
890 internal::Object* value);
891
892 // Make it hard to create heap-allocated or illegal handle scopes by
893 // disallowing certain operations.
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 HandleScope(const HandleScope&);
895 void operator=(const HandleScope&);
896 void* operator new(size_t size);
897 void operator delete(void*, size_t);
898
Steve Block44f0eee2011-05-26 01:26:41 +0100899 internal::Isolate* isolate_;
John Reck59135872010-11-02 12:39:01 -0700900 internal::Object** prev_next_;
901 internal::Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 // Local::New uses CreateHandle with an Isolate* parameter.
904 template<class F> friend class Local;
Steve Blocka7e24c12009-10-30 11:49:00 +0000905
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
907 // a HeapObject* in their shortcuts.
908 friend class Object;
909 friend class Context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000910};
911
912
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000913/**
914 * A HandleScope which first allocates a handle in the current scope
915 * which will be later filled with the escape value.
916 */
917class V8_EXPORT EscapableHandleScope : public HandleScope {
918 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100919 explicit EscapableHandleScope(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 V8_INLINE ~EscapableHandleScope() {}
921
922 /**
923 * Pushes the value into the previous scope and returns a handle to it.
924 * Cannot be called twice.
925 */
926 template <class T>
927 V8_INLINE Local<T> Escape(Local<T> value) {
928 internal::Object** slot =
929 Escape(reinterpret_cast<internal::Object**>(*value));
930 return Local<T>(reinterpret_cast<T*>(slot));
931 }
932
933 private:
934 internal::Object** Escape(internal::Object** escape_value);
935
936 // Make it hard to create heap-allocated or illegal handle scopes by
937 // disallowing certain operations.
938 EscapableHandleScope(const EscapableHandleScope&);
939 void operator=(const EscapableHandleScope&);
940 void* operator new(size_t size);
941 void operator delete(void*, size_t);
942
943 internal::Object** escape_slot_;
944};
945
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000946class V8_EXPORT SealHandleScope {
947 public:
948 SealHandleScope(Isolate* isolate);
949 ~SealHandleScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 private:
952 // Make it hard to create heap-allocated or illegal handle scopes by
953 // disallowing certain operations.
954 SealHandleScope(const SealHandleScope&);
955 void operator=(const SealHandleScope&);
956 void* operator new(size_t size);
957 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000958
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000959 internal::Isolate* isolate_;
960 internal::Object** prev_limit_;
961 int prev_sealed_level_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000962};
963
964
Ben Murdoch257744e2011-11-30 15:57:28 +0000965// --- Special objects ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000966
967
968/**
969 * The superclass of values and API object templates.
970 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000971class V8_EXPORT Data {
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 private:
973 Data();
974};
975
976
977/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000978 * The optional attributes of ScriptOrigin.
979 */
980class ScriptOriginOptions {
981 public:
982 V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false,
983 bool is_shared_cross_origin = false,
984 bool is_opaque = false)
985 : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) |
986 (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
987 (is_opaque ? kIsOpaque : 0)) {}
988 V8_INLINE ScriptOriginOptions(int flags)
989 : flags_(flags &
990 (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {}
991 bool IsEmbedderDebugScript() const {
992 return (flags_ & kIsEmbedderDebugScript) != 0;
993 }
994 bool IsSharedCrossOrigin() const {
995 return (flags_ & kIsSharedCrossOrigin) != 0;
996 }
997 bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
998 int Flags() const { return flags_; }
999
1000 private:
1001 enum {
1002 kIsEmbedderDebugScript = 1,
1003 kIsSharedCrossOrigin = 1 << 1,
1004 kIsOpaque = 1 << 2
1005 };
1006 const int flags_;
1007};
1008
1009/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 * The origin, within a file, of a script.
1011 */
Steve Block8defd9f2010-07-08 12:39:36 +01001012class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001014 V8_INLINE ScriptOrigin(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 Local<Value> resource_name,
1016 Local<Integer> resource_line_offset = Local<Integer>(),
1017 Local<Integer> resource_column_offset = Local<Integer>(),
1018 Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(),
1019 Local<Integer> script_id = Local<Integer>(),
1020 Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(),
1021 Local<Value> source_map_url = Local<Value>(),
1022 Local<Boolean> resource_is_opaque = Local<Boolean>());
1023 V8_INLINE Local<Value> ResourceName() const;
1024 V8_INLINE Local<Integer> ResourceLineOffset() const;
1025 V8_INLINE Local<Integer> ResourceColumnOffset() const;
1026 /**
1027 * Returns true for embedder's debugger scripts
1028 */
1029 V8_INLINE Local<Integer> ScriptID() const;
1030 V8_INLINE Local<Value> SourceMapUrl() const;
1031 V8_INLINE ScriptOriginOptions Options() const { return options_; }
1032
Steve Blocka7e24c12009-10-30 11:49:00 +00001033 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034 Local<Value> resource_name_;
1035 Local<Integer> resource_line_offset_;
1036 Local<Integer> resource_column_offset_;
1037 ScriptOriginOptions options_;
1038 Local<Integer> script_id_;
1039 Local<Value> source_map_url_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001040};
1041
1042
1043/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 * A compiled JavaScript script, not yet tied to a Context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046class V8_EXPORT UnboundScript {
Steve Blocka7e24c12009-10-30 11:49:00 +00001047 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 * Binds the script to the currently entered context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001050 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001051 Local<Script> BindToCurrentContext();
1052
1053 int GetId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001054 Local<Value> GetScriptName();
Steve Blocka7e24c12009-10-30 11:49:00 +00001055
1056 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 * Data read from magic sourceURL comments.
1058 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059 Local<Value> GetSourceURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001060 /**
1061 * Data read from magic sourceMappingURL comments.
1062 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 Local<Value> GetSourceMappingURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064
1065 /**
1066 * Returns zero based line number of the code_pos location in the script.
1067 * -1 will be returned if no information available.
1068 */
1069 int GetLineNumber(int code_pos);
1070
1071 static const int kNoScriptId = 0;
1072};
1073
1074
1075/**
1076 * A compiled JavaScript script, tied to a Context which was active when the
1077 * script was compiled.
1078 */
1079class V8_EXPORT Script {
1080 public:
1081 /**
1082 * A shorthand for ScriptCompiler::Compile().
1083 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001084 static V8_DEPRECATE_SOON(
1085 "Use maybe version",
1086 Local<Script> Compile(Local<String> source,
1087 ScriptOrigin* origin = nullptr));
1088 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1089 Local<Context> context, Local<String> source,
1090 ScriptOrigin* origin = nullptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001091
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001092 static Local<Script> V8_DEPRECATE_SOON("Use maybe version",
1093 Compile(Local<String> source,
1094 Local<String> file_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095
1096 /**
1097 * Runs the script returning the resulting value. It will be run in the
1098 * context in which it was created (ScriptCompiler::CompileBound or
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001099 * UnboundScript::BindToCurrentContext()).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001101 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run());
1102 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103
1104 /**
1105 * Returns the corresponding context-unbound script.
1106 */
1107 Local<UnboundScript> GetUnboundScript();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001108};
1109
1110
1111/**
1112 * For compiling scripts.
1113 */
1114class V8_EXPORT ScriptCompiler {
1115 public:
1116 /**
1117 * Compilation data that the embedder can cache and pass back to speed up
1118 * future compilations. The data is produced if the CompilerOptions passed to
1119 * the compilation functions in ScriptCompiler contains produce_data_to_cache
1120 * = true. The data to cache can then can be retrieved from
1121 * UnboundScript.
1122 */
1123 struct V8_EXPORT CachedData {
1124 enum BufferPolicy {
1125 BufferNotOwned,
1126 BufferOwned
1127 };
1128
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001129 CachedData()
1130 : data(NULL),
1131 length(0),
1132 rejected(false),
1133 buffer_policy(BufferNotOwned) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134
1135 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
1136 // data and guarantees that it stays alive until the CachedData object is
1137 // destroyed. If the policy is BufferOwned, the given data will be deleted
1138 // (with delete[]) when the CachedData object is destroyed.
1139 CachedData(const uint8_t* data, int length,
1140 BufferPolicy buffer_policy = BufferNotOwned);
1141 ~CachedData();
1142 // TODO(marja): Async compilation; add constructors which take a callback
1143 // which will be called when V8 no longer needs the data.
1144 const uint8_t* data;
1145 int length;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001146 bool rejected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 BufferPolicy buffer_policy;
1148
1149 private:
1150 // Prevent copying. Not implemented.
1151 CachedData(const CachedData&);
1152 CachedData& operator=(const CachedData&);
1153 };
1154
1155 /**
1156 * Source code which can be then compiled to a UnboundScript or Script.
1157 */
1158 class Source {
1159 public:
1160 // Source takes ownership of CachedData.
1161 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
1162 CachedData* cached_data = NULL);
1163 V8_INLINE Source(Local<String> source_string,
1164 CachedData* cached_data = NULL);
1165 V8_INLINE ~Source();
1166
1167 // Ownership of the CachedData or its buffers is *not* transferred to the
1168 // caller. The CachedData object is alive as long as the Source object is
1169 // alive.
1170 V8_INLINE const CachedData* GetCachedData() const;
1171
1172 private:
1173 friend class ScriptCompiler;
1174 // Prevent copying. Not implemented.
1175 Source(const Source&);
1176 Source& operator=(const Source&);
1177
1178 Local<String> source_string;
1179
1180 // Origin information
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001181 Local<Value> resource_name;
1182 Local<Integer> resource_line_offset;
1183 Local<Integer> resource_column_offset;
1184 ScriptOriginOptions resource_options;
1185 Local<Value> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001186
1187 // Cached data from previous compilation (if a kConsume*Cache flag is
1188 // set), or hold newly generated cache data (kProduce*Cache flags) are
1189 // set when calling a compile method.
1190 CachedData* cached_data;
1191 };
1192
1193 /**
1194 * For streaming incomplete script data to V8. The embedder should implement a
1195 * subclass of this class.
1196 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001197 class V8_EXPORT ExternalSourceStream {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198 public:
1199 virtual ~ExternalSourceStream() {}
1200
1201 /**
1202 * V8 calls this to request the next chunk of data from the embedder. This
1203 * function will be called on a background thread, so it's OK to block and
1204 * wait for the data, if the embedder doesn't have data yet. Returns the
1205 * length of the data returned. When the data ends, GetMoreData should
1206 * return 0. Caller takes ownership of the data.
1207 *
1208 * When streaming UTF-8 data, V8 handles multi-byte characters split between
1209 * two data chunks, but doesn't handle multi-byte characters split between
1210 * more than two data chunks. The embedder can avoid this problem by always
1211 * returning at least 2 bytes of data.
1212 *
1213 * If the embedder wants to cancel the streaming, they should make the next
1214 * GetMoreData call return 0. V8 will interpret it as end of data (and most
1215 * probably, parsing will fail). The streaming task will return as soon as
1216 * V8 has parsed the data it received so far.
1217 */
1218 virtual size_t GetMoreData(const uint8_t** src) = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001219
1220 /**
1221 * V8 calls this method to set a 'bookmark' at the current position in
1222 * the source stream, for the purpose of (maybe) later calling
1223 * ResetToBookmark. If ResetToBookmark is called later, then subsequent
1224 * calls to GetMoreData should return the same data as they did when
1225 * SetBookmark was called earlier.
1226 *
1227 * The embedder may return 'false' to indicate it cannot provide this
1228 * functionality.
1229 */
1230 virtual bool SetBookmark();
1231
1232 /**
1233 * V8 calls this to return to a previously set bookmark.
1234 */
1235 virtual void ResetToBookmark();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 };
1237
1238
1239 /**
1240 * Source code which can be streamed into V8 in pieces. It will be parsed
1241 * while streaming. It can be compiled after the streaming is complete.
1242 * StreamedSource must be kept alive while the streaming task is ran (see
1243 * ScriptStreamingTask below).
1244 */
1245 class V8_EXPORT StreamedSource {
1246 public:
1247 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8 };
1248
1249 StreamedSource(ExternalSourceStream* source_stream, Encoding encoding);
1250 ~StreamedSource();
1251
1252 // Ownership of the CachedData or its buffers is *not* transferred to the
1253 // caller. The CachedData object is alive as long as the StreamedSource
1254 // object is alive.
1255 const CachedData* GetCachedData() const;
1256
1257 internal::StreamedSource* impl() const { return impl_; }
1258
1259 private:
1260 // Prevent copying. Not implemented.
1261 StreamedSource(const StreamedSource&);
1262 StreamedSource& operator=(const StreamedSource&);
1263
1264 internal::StreamedSource* impl_;
1265 };
1266
1267 /**
1268 * A streaming task which the embedder must run on a background thread to
1269 * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript.
1270 */
1271 class ScriptStreamingTask {
1272 public:
1273 virtual ~ScriptStreamingTask() {}
1274 virtual void Run() = 0;
1275 };
1276
1277 enum CompileOptions {
1278 kNoCompileOptions = 0,
1279 kProduceParserCache,
1280 kConsumeParserCache,
1281 kProduceCodeCache,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001282 kConsumeCodeCache
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 };
1284
1285 /**
1286 * Compiles the specified script (context-independent).
1287 * Cached data as part of the source object can be optionally produced to be
1288 * consumed later to speed up compilation of identical source scripts.
1289 *
1290 * Note that when producing cached data, the source must point to NULL for
1291 * cached data. When consuming cached data, the cached data must have been
1292 * produced by the same version of V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00001293 *
Andrei Popescu402d9372010-02-26 13:31:12 +00001294 * \param source Script source code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295 * \return Compiled script object (context independent; for running it must be
1296 * bound to a context).
Andrei Popescu402d9372010-02-26 13:31:12 +00001297 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001298 static V8_DEPRECATED("Use maybe version",
1299 Local<UnboundScript> CompileUnbound(
1300 Isolate* isolate, Source* source,
1301 CompileOptions options = kNoCompileOptions));
1302 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001303 Isolate* isolate, Source* source,
1304 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001305
1306 /**
1307 * Compiles the specified script (bound to current context).
1308 *
1309 * \param source Script source code.
Andrei Popescu402d9372010-02-26 13:31:12 +00001310 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1311 * using pre_data speeds compilation if it's done multiple times.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 * Owned by caller, no references are kept when this function returns.
Andrei Popescu402d9372010-02-26 13:31:12 +00001313 * \return Compiled script object, bound to the context that was active
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314 * when this function was called. When run it will always use this
Andrei Popescu402d9372010-02-26 13:31:12 +00001315 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001317 static V8_DEPRECATED(
1318 "Use maybe version",
1319 Local<Script> Compile(Isolate* isolate, Source* source,
1320 CompileOptions options = kNoCompileOptions));
1321 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1322 Local<Context> context, Source* source,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001324
1325 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001326 * Returns a task which streams script data into V8, or NULL if the script
1327 * cannot be streamed. The user is responsible for running the task on a
1328 * background thread and deleting it. When ran, the task starts parsing the
1329 * script, and it will request data from the StreamedSource as needed. When
1330 * ScriptStreamingTask::Run exits, all data has been streamed and the script
1331 * can be compiled (see Compile below).
Andrei Popescu402d9372010-02-26 13:31:12 +00001332 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 * This API allows to start the streaming with as little data as possible, and
1334 * the remaining data (for example, the ScriptOrigin) is passed to Compile.
Andrei Popescu402d9372010-02-26 13:31:12 +00001335 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 static ScriptStreamingTask* StartStreamingScript(
1337 Isolate* isolate, StreamedSource* source,
1338 CompileOptions options = kNoCompileOptions);
Steve Blocka7e24c12009-10-30 11:49:00 +00001339
1340 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 * Compiles a streamed script (bound to current context).
1342 *
1343 * This can only be called after the streaming has finished
1344 * (ScriptStreamingTask has been run). V8 doesn't construct the source string
1345 * during streaming, so the embedder needs to pass the full source here.
Steve Blocka7e24c12009-10-30 11:49:00 +00001346 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001347 static V8_DEPRECATED("Use maybe version",
1348 Local<Script> Compile(Isolate* isolate,
1349 StreamedSource* source,
1350 Local<String> full_source_string,
1351 const ScriptOrigin& origin));
1352 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1353 Local<Context> context, StreamedSource* source,
1354 Local<String> full_source_string, const ScriptOrigin& origin);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001355
1356 /**
1357 * Return a version tag for CachedData for the current V8 version & flags.
1358 *
1359 * This value is meant only for determining whether a previously generated
1360 * CachedData instance is still valid; the tag has no other meaing.
1361 *
1362 * Background: The data carried by CachedData may depend on the exact
1363 * V8 version number or currently compiler flags. This means when
1364 * persisting CachedData, the embedder must take care to not pass in
1365 * data from another V8 version, or the same version with different
1366 * features enabled.
1367 *
1368 * The easiest way to do so is to clear the embedder's cache on any
1369 * such change.
1370 *
1371 * Alternatively, this tag can be stored alongside the cached data and
1372 * compared when it is being used.
1373 */
1374 static uint32_t CachedDataVersionTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001375
1376 /**
1377 * Compile an ES6 module.
1378 *
1379 * This is an unfinished experimental feature, and is only exposed
1380 * here for internal testing purposes.
1381 * Only parsing works at the moment. Do not use.
1382 *
1383 * TODO(adamk): Script is likely the wrong return value for this;
1384 * should return some new Module type.
1385 */
1386 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule(
1387 Local<Context> context, Source* source,
1388 CompileOptions options = kNoCompileOptions);
1389
1390 /**
1391 * Compile a function for a given context. This is equivalent to running
1392 *
1393 * with (obj) {
1394 * return function(args) { ... }
1395 * }
1396 *
1397 * It is possible to specify multiple context extensions (obj in the above
1398 * example).
1399 */
1400 static V8_DEPRECATE_SOON("Use maybe version",
1401 Local<Function> CompileFunctionInContext(
1402 Isolate* isolate, Source* source,
1403 Local<Context> context, size_t arguments_count,
1404 Local<String> arguments[],
1405 size_t context_extension_count,
1406 Local<Object> context_extensions[]));
1407 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
1408 Local<Context> context, Source* source, size_t arguments_count,
1409 Local<String> arguments[], size_t context_extension_count,
1410 Local<Object> context_extensions[]);
1411
1412 private:
1413 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
1414 Isolate* isolate, Source* source, CompileOptions options, bool is_module);
Steve Blocka7e24c12009-10-30 11:49:00 +00001415};
1416
1417
1418/**
1419 * An error message.
1420 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421class V8_EXPORT Message {
Steve Blocka7e24c12009-10-30 11:49:00 +00001422 public:
1423 Local<String> Get() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001424
1425 V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const);
1426 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
1427 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001428
1429 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001430 * Returns the origin for the script from where the function causing the
1431 * error originates.
1432 */
1433 ScriptOrigin GetScriptOrigin() const;
1434
1435 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001436 * Returns the resource name for the script from where the function causing
1437 * the error originates.
1438 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001439 Local<Value> GetScriptResourceName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001440
1441 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001442 * Exception stack trace. By default stack traces are not captured for
1443 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1444 * to change this option.
1445 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001446 Local<StackTrace> GetStackTrace() const;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001447
1448 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 * Returns the number, 1-based, of the line where the error occurred.
1450 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001451 V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const);
1452 V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001453
1454 /**
1455 * Returns the index within the script of the first character where
1456 * the error occurred.
1457 */
1458 int GetStartPosition() const;
1459
1460 /**
1461 * Returns the index within the script of the last character where
1462 * the error occurred.
1463 */
1464 int GetEndPosition() const;
1465
1466 /**
1467 * Returns the index within the line of the first character where
1468 * the error occurred.
1469 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001470 V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const);
1471 V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
1473 /**
1474 * Returns the index within the line of the last character where
1475 * the error occurred.
1476 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477 V8_DEPRECATED("Use maybe version", int GetEndColumn() const);
1478 V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001479
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001480 /**
1481 * Passes on the value set by the embedder when it fed the script from which
1482 * this Message was generated to V8.
1483 */
1484 bool IsSharedCrossOrigin() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485 bool IsOpaque() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 // TODO(1245381): Print to a string instead of on a FILE.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +01001489
1490 static const int kNoLineNumberInfo = 0;
1491 static const int kNoColumnInfo = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001492 static const int kNoScriptIdInfo = 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001493};
1494
1495
1496/**
1497 * Representation of a JavaScript stack trace. The information collected is a
1498 * snapshot of the execution stack and the information remains valid after
1499 * execution continues.
1500 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501class V8_EXPORT StackTrace {
Kristian Monsen25f61362010-05-21 11:50:48 +01001502 public:
1503 /**
1504 * Flags that determine what information is placed captured for each
1505 * StackFrame when grabbing the current stack trace.
1506 */
1507 enum StackTraceOptions {
1508 kLineNumber = 1,
1509 kColumnOffset = 1 << 1 | kLineNumber,
1510 kScriptName = 1 << 2,
1511 kFunctionName = 1 << 3,
1512 kIsEval = 1 << 4,
1513 kIsConstructor = 1 << 5,
Ben Murdochf87a2032010-10-22 12:50:53 +01001514 kScriptNameOrSourceURL = 1 << 6,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 kScriptId = 1 << 7,
1516 kExposeFramesAcrossSecurityOrigins = 1 << 8,
Kristian Monsen25f61362010-05-21 11:50:48 +01001517 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
Ben Murdochf87a2032010-10-22 12:50:53 +01001518 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
Kristian Monsen25f61362010-05-21 11:50:48 +01001519 };
1520
1521 /**
1522 * Returns a StackFrame at a particular index.
1523 */
1524 Local<StackFrame> GetFrame(uint32_t index) const;
1525
1526 /**
1527 * Returns the number of StackFrames.
1528 */
1529 int GetFrameCount() const;
1530
1531 /**
1532 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1533 */
1534 Local<Array> AsArray();
1535
1536 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001537 * Grab a snapshot of the current JavaScript execution stack.
Kristian Monsen25f61362010-05-21 11:50:48 +01001538 *
1539 * \param frame_limit The maximum number of stack frames we want to capture.
1540 * \param options Enumerates the set of things we will capture for each
1541 * StackFrame.
1542 */
1543 static Local<StackTrace> CurrentStackTrace(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 Isolate* isolate,
Kristian Monsen25f61362010-05-21 11:50:48 +01001545 int frame_limit,
1546 StackTraceOptions options = kOverview);
1547};
1548
1549
1550/**
1551 * A single JavaScript stack frame.
1552 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001553class V8_EXPORT StackFrame {
Kristian Monsen25f61362010-05-21 11:50:48 +01001554 public:
1555 /**
1556 * Returns the number, 1-based, of the line for the associate function call.
1557 * This method will return Message::kNoLineNumberInfo if it is unable to
1558 * retrieve the line number, or if kLineNumber was not passed as an option
1559 * when capturing the StackTrace.
1560 */
1561 int GetLineNumber() const;
1562
1563 /**
1564 * Returns the 1-based column offset on the line for the associated function
1565 * call.
1566 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1567 * the column number, or if kColumnOffset was not passed as an option when
1568 * capturing the StackTrace.
1569 */
1570 int GetColumn() const;
1571
1572 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573 * Returns the id of the script for the function for this StackFrame.
1574 * This method will return Message::kNoScriptIdInfo if it is unable to
1575 * retrieve the script id, or if kScriptId was not passed as an option when
1576 * capturing the StackTrace.
1577 */
1578 int GetScriptId() const;
1579
1580 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001581 * Returns the name of the resource that contains the script for the
1582 * function for this StackFrame.
1583 */
1584 Local<String> GetScriptName() const;
1585
1586 /**
Ben Murdochf87a2032010-10-22 12:50:53 +01001587 * Returns the name of the resource that contains the script for the
1588 * function for this StackFrame or sourceURL value if the script name
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 * is undefined and its source ends with //# sourceURL=... string or
1590 * deprecated //@ sourceURL=... string.
Ben Murdochf87a2032010-10-22 12:50:53 +01001591 */
1592 Local<String> GetScriptNameOrSourceURL() const;
1593
1594 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001595 * Returns the name of the function associated with this stack frame.
1596 */
1597 Local<String> GetFunctionName() const;
1598
1599 /**
1600 * Returns whether or not the associated function is compiled via a call to
1601 * eval().
1602 */
1603 bool IsEval() const;
1604
1605 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001606 * Returns whether or not the associated function is called as a
Kristian Monsen25f61362010-05-21 11:50:48 +01001607 * constructor via "new".
1608 */
1609 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001610};
1611
1612
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001613// A StateTag represents a possible state of the VM.
1614enum StateTag { JS, GC, COMPILER, OTHER, EXTERNAL, IDLE };
1615
1616
1617// A RegisterState represents the current state of registers used
1618// by the sampling profiler API.
1619struct RegisterState {
1620 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {}
1621 void* pc; // Instruction pointer.
1622 void* sp; // Stack pointer.
1623 void* fp; // Frame pointer.
1624};
1625
1626
1627// The output structure filled up by GetStackSample API function.
1628struct SampleInfo {
1629 size_t frames_count;
1630 StateTag vm_state;
1631};
1632
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633/**
Ben Murdochc5610432016-08-08 18:44:38 +01001634 * A JSON Parser and Stringifier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 */
1636class V8_EXPORT JSON {
1637 public:
1638 /**
1639 * Tries to parse the string |json_string| and returns it as value if
1640 * successful.
1641 *
1642 * \param json_string The string to parse.
1643 * \return The corresponding value if successfully parsed.
1644 */
Ben Murdochc5610432016-08-08 18:44:38 +01001645 static V8_DEPRECATED("Use the maybe version taking context",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001646 Local<Value> Parse(Local<String> json_string));
Ben Murdochc5610432016-08-08 18:44:38 +01001647 static V8_DEPRECATE_SOON("Use the maybe version taking context",
1648 MaybeLocal<Value> Parse(Isolate* isolate,
1649 Local<String> json_string));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001650 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
Ben Murdochc5610432016-08-08 18:44:38 +01001651 Local<Context> context, Local<String> json_string);
1652
1653 /**
1654 * Tries to stringify the JSON-serializable object |json_object| and returns
1655 * it as string if successful.
1656 *
1657 * \param json_object The JSON-serializable object to stringify.
1658 * \return The corresponding string if successfully stringified.
1659 */
1660 static V8_WARN_UNUSED_RESULT MaybeLocal<String> Stringify(
1661 Local<Context> context, Local<Object> json_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001662};
1663
1664
1665/**
1666 * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap
1667 * but can be created without entering a v8::Context and hence shouldn't
1668 * escape to JavaScript.
1669 */
1670class V8_EXPORT NativeWeakMap : public Data {
1671 public:
1672 static Local<NativeWeakMap> New(Isolate* isolate);
1673 void Set(Local<Value> key, Local<Value> value);
1674 Local<Value> Get(Local<Value> key);
1675 bool Has(Local<Value> key);
1676 bool Delete(Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677};
1678
1679
Ben Murdoch257744e2011-11-30 15:57:28 +00001680// --- Value ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001681
1682
1683/**
1684 * The superclass of all JavaScript values and objects.
1685 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686class V8_EXPORT Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00001687 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001688 /**
1689 * Returns true if this value is the undefined value. See ECMA-262
1690 * 4.3.10.
1691 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 V8_INLINE bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001693
1694 /**
1695 * Returns true if this value is the null value. See ECMA-262
1696 * 4.3.11.
1697 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 V8_INLINE bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001699
1700 /**
1701 * Returns true if this value is true.
1702 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001703 bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001704
1705 /**
1706 * Returns true if this value is false.
1707 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001708 bool IsFalse() const;
1709
1710 /**
1711 * Returns true if this value is a symbol or a string.
1712 * This is an experimental feature.
1713 */
1714 bool IsName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001715
1716 /**
1717 * Returns true if this value is an instance of the String type.
1718 * See ECMA-262 8.4.
1719 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001720 V8_INLINE bool IsString() const;
1721
1722 /**
1723 * Returns true if this value is a symbol.
1724 * This is an experimental feature.
1725 */
1726 bool IsSymbol() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001727
1728 /**
1729 * Returns true if this value is a function.
1730 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001731 bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001732
1733 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001734 * Returns true if this value is an array. Note that it will return false for
1735 * an Proxy for an array.
Steve Blocka7e24c12009-10-30 11:49:00 +00001736 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737 bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001738
1739 /**
1740 * Returns true if this value is an object.
1741 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001742 bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001743
1744 /**
1745 * Returns true if this value is boolean.
1746 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001747 bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001748
1749 /**
1750 * Returns true if this value is a number.
1751 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001752 bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001753
1754 /**
1755 * Returns true if this value is external.
1756 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001757 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001758
1759 /**
1760 * Returns true if this value is a 32-bit signed integer.
1761 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001762 bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001763
1764 /**
Steve Block6ded16b2010-05-10 14:33:55 +01001765 * Returns true if this value is a 32-bit unsigned integer.
1766 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767 bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +01001768
1769 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 * Returns true if this value is a Date.
1771 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001772 bool IsDate() const;
1773
1774 /**
1775 * Returns true if this value is an Arguments object.
1776 */
1777 bool IsArgumentsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001778
Iain Merrick75681382010-08-19 15:07:18 +01001779 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001780 * Returns true if this value is a Boolean object.
1781 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 bool IsBooleanObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001783
1784 /**
1785 * Returns true if this value is a Number object.
1786 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001787 bool IsNumberObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001788
1789 /**
1790 * Returns true if this value is a String object.
1791 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001792 bool IsStringObject() const;
1793
1794 /**
1795 * Returns true if this value is a Symbol object.
1796 * This is an experimental feature.
1797 */
1798 bool IsSymbolObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001799
1800 /**
1801 * Returns true if this value is a NativeError.
1802 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001803 bool IsNativeError() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001804
1805 /**
Iain Merrick75681382010-08-19 15:07:18 +01001806 * Returns true if this value is a RegExp.
1807 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001808 bool IsRegExp() const;
Iain Merrick75681382010-08-19 15:07:18 +01001809
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001811 * Returns true if this value is a Generator function.
1812 * This is an experimental feature.
1813 */
1814 bool IsGeneratorFunction() const;
1815
1816 /**
1817 * Returns true if this value is a Generator object (iterator).
1818 * This is an experimental feature.
1819 */
1820 bool IsGeneratorObject() const;
1821
1822 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001823 * Returns true if this value is a Promise.
1824 * This is an experimental feature.
1825 */
1826 bool IsPromise() const;
1827
1828 /**
1829 * Returns true if this value is a Map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001830 */
1831 bool IsMap() const;
1832
1833 /**
1834 * Returns true if this value is a Set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001835 */
1836 bool IsSet() const;
1837
1838 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001839 * Returns true if this value is a Map Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001840 */
1841 bool IsMapIterator() const;
1842
1843 /**
1844 * Returns true if this value is a Set Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001845 */
1846 bool IsSetIterator() const;
1847
1848 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001849 * Returns true if this value is a WeakMap.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001850 */
1851 bool IsWeakMap() const;
1852
1853 /**
1854 * Returns true if this value is a WeakSet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001855 */
1856 bool IsWeakSet() const;
1857
1858 /**
1859 * Returns true if this value is an ArrayBuffer.
1860 * This is an experimental feature.
1861 */
1862 bool IsArrayBuffer() const;
1863
1864 /**
1865 * Returns true if this value is an ArrayBufferView.
1866 * This is an experimental feature.
1867 */
1868 bool IsArrayBufferView() const;
1869
1870 /**
1871 * Returns true if this value is one of TypedArrays.
1872 * This is an experimental feature.
1873 */
1874 bool IsTypedArray() const;
1875
1876 /**
1877 * Returns true if this value is an Uint8Array.
1878 * This is an experimental feature.
1879 */
1880 bool IsUint8Array() const;
1881
1882 /**
1883 * Returns true if this value is an Uint8ClampedArray.
1884 * This is an experimental feature.
1885 */
1886 bool IsUint8ClampedArray() const;
1887
1888 /**
1889 * Returns true if this value is an Int8Array.
1890 * This is an experimental feature.
1891 */
1892 bool IsInt8Array() const;
1893
1894 /**
1895 * Returns true if this value is an Uint16Array.
1896 * This is an experimental feature.
1897 */
1898 bool IsUint16Array() const;
1899
1900 /**
1901 * Returns true if this value is an Int16Array.
1902 * This is an experimental feature.
1903 */
1904 bool IsInt16Array() const;
1905
1906 /**
1907 * Returns true if this value is an Uint32Array.
1908 * This is an experimental feature.
1909 */
1910 bool IsUint32Array() const;
1911
1912 /**
1913 * Returns true if this value is an Int32Array.
1914 * This is an experimental feature.
1915 */
1916 bool IsInt32Array() const;
1917
1918 /**
1919 * Returns true if this value is a Float32Array.
1920 * This is an experimental feature.
1921 */
1922 bool IsFloat32Array() const;
1923
1924 /**
1925 * Returns true if this value is a Float64Array.
1926 * This is an experimental feature.
1927 */
1928 bool IsFloat64Array() const;
1929
1930 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001931 * Returns true if this value is a SIMD Float32x4.
1932 * This is an experimental feature.
1933 */
1934 bool IsFloat32x4() const;
1935
1936 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001937 * Returns true if this value is a DataView.
1938 * This is an experimental feature.
1939 */
1940 bool IsDataView() const;
1941
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001942 /**
1943 * Returns true if this value is a SharedArrayBuffer.
1944 * This is an experimental feature.
1945 */
1946 bool IsSharedArrayBuffer() const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001948 /**
1949 * Returns true if this value is a JavaScript Proxy.
1950 */
1951 bool IsProxy() const;
1952
1953
1954 V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean(
1955 Local<Context> context) const;
1956 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
1957 Local<Context> context) const;
1958 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
1959 Local<Context> context) const;
1960 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
1961 Local<Context> context) const;
1962 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
1963 Local<Context> context) const;
1964 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
1965 Local<Context> context) const;
1966 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
1967 Local<Context> context) const;
1968 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
1969
1970 V8_DEPRECATE_SOON("Use maybe version",
1971 Local<Boolean> ToBoolean(Isolate* isolate) const);
1972 V8_DEPRECATE_SOON("Use maybe version",
1973 Local<Number> ToNumber(Isolate* isolate) const);
1974 V8_DEPRECATE_SOON("Use maybe version",
1975 Local<String> ToString(Isolate* isolate) const);
1976 V8_DEPRECATED("Use maybe version",
1977 Local<String> ToDetailString(Isolate* isolate) const);
1978 V8_DEPRECATE_SOON("Use maybe version",
1979 Local<Object> ToObject(Isolate* isolate) const);
1980 V8_DEPRECATE_SOON("Use maybe version",
1981 Local<Integer> ToInteger(Isolate* isolate) const);
1982 V8_DEPRECATED("Use maybe version",
1983 Local<Uint32> ToUint32(Isolate* isolate) const);
1984 V8_DEPRECATE_SOON("Use maybe version",
1985 Local<Int32> ToInt32(Isolate* isolate) const);
1986
1987 inline V8_DEPRECATE_SOON("Use maybe version",
1988 Local<Boolean> ToBoolean() const);
1989 inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const);
1990 inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const);
1991 inline V8_DEPRECATED("Use maybe version",
1992 Local<String> ToDetailString() const);
1993 inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const);
1994 inline V8_DEPRECATE_SOON("Use maybe version",
1995 Local<Integer> ToInteger() const);
1996 inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const);
1997 inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00001998
1999 /**
2000 * Attempts to convert a string to an array index.
2001 * Returns an empty handle if the conversion fails.
2002 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002003 V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const);
2004 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
2005 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007 V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const;
2008 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
2009 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
2010 Local<Context> context) const;
2011 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
2012 Local<Context> context) const;
2013 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
2014
2015 V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const);
2016 V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const);
2017 V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const);
2018 V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const);
2019 V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002020
2021 /** JS == */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002022 V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const);
2023 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
2024 Local<Value> that) const;
2025 bool StrictEquals(Local<Value> that) const;
2026 bool SameValue(Local<Value> that) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002027
2028 template <class T> V8_INLINE static Value* Cast(T* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002029
Ben Murdochc5610432016-08-08 18:44:38 +01002030 Local<String> TypeOf(v8::Isolate*);
2031
Steve Blocka7e24c12009-10-30 11:49:00 +00002032 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002033 V8_INLINE bool QuickIsUndefined() const;
2034 V8_INLINE bool QuickIsNull() const;
2035 V8_INLINE bool QuickIsString() const;
2036 bool FullIsUndefined() const;
2037 bool FullIsNull() const;
2038 bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002039};
2040
2041
2042/**
2043 * The superclass of primitive values. See ECMA-262 4.3.2.
2044 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002045class V8_EXPORT Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +00002046
2047
2048/**
2049 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
2050 * or false value.
2051 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002052class V8_EXPORT Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002053 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002054 bool Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002055 V8_INLINE static Boolean* Cast(v8::Value* obj);
2056 V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value);
2057
2058 private:
2059 static void CheckCast(v8::Value* obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002060};
2061
2062
2063/**
2064 * A superclass for symbols and strings.
2065 */
2066class V8_EXPORT Name : public Primitive {
2067 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002068 /**
2069 * Returns the identity hash for this object. The current implementation
2070 * uses an inline property on the object to store the identity hash.
2071 *
2072 * The return value will never be 0. Also, it is not guaranteed to be
2073 * unique.
2074 */
2075 int GetIdentityHash();
2076
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002077 V8_INLINE static Name* Cast(v8::Value* obj);
2078 private:
2079 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002080};
2081
2082
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002083enum class NewStringType { kNormal, kInternalized };
2084
2085
Steve Blocka7e24c12009-10-30 11:49:00 +00002086/**
2087 * A JavaScript string value (ECMA-262, 4.3.17).
2088 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002089class V8_EXPORT String : public Name {
Steve Blocka7e24c12009-10-30 11:49:00 +00002090 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002091 static const int kMaxLength = (1 << 28) - 16;
2092
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093 enum Encoding {
2094 UNKNOWN_ENCODING = 0x1,
2095 TWO_BYTE_ENCODING = 0x0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002096 ONE_BYTE_ENCODING = 0x4
2097 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002098 /**
2099 * Returns the number of characters in this string.
2100 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002101 int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002102
2103 /**
2104 * Returns the number of bytes in the UTF-8 encoded
2105 * representation of this string.
2106 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002107 int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002108
2109 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110 * Returns whether this string is known to contain only one byte data.
2111 * Does not read the string.
2112 * False negatives are possible.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002113 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002114 bool IsOneByte() const;
2115
2116 /**
2117 * Returns whether this string contain only one byte data.
2118 * Will read the entire string in some cases.
2119 */
2120 bool ContainsOnlyOneByte() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002121
2122 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002123 * Write the contents of the string to an external buffer.
2124 * If no arguments are given, expects the buffer to be large
2125 * enough to hold the entire string and NULL terminator. Copies
2126 * the contents of the string and the NULL terminator into the
2127 * buffer.
2128 *
Ben Murdochb0fe1622011-05-05 13:52:32 +01002129 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
2130 * before the end of the buffer.
2131 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002132 * Copies up to length characters into the output buffer.
2133 * Only null-terminates if there is enough space in the buffer.
2134 *
2135 * \param buffer The buffer into which the string will be copied.
2136 * \param start The starting position within the string at which
2137 * copying begins.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002138 * \param length The number of characters to copy from the string. For
2139 * WriteUtf8 the number of bytes in the buffer.
Steve Block6ded16b2010-05-10 14:33:55 +01002140 * \param nchars_ref The number of characters written, can be NULL.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002141 * \param options Various options that might affect performance of this or
Steve Block6ded16b2010-05-10 14:33:55 +01002142 * subsequent operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002143 * \return The number of characters copied to the buffer excluding the null
2144 * terminator. For WriteUtf8: The number of bytes copied to the buffer
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002145 * including the null terminator (if written).
Steve Blocka7e24c12009-10-30 11:49:00 +00002146 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002147 enum WriteOptions {
2148 NO_OPTIONS = 0,
2149 HINT_MANY_WRITES_EXPECTED = 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002150 NO_NULL_TERMINATION = 2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002151 PRESERVE_ONE_BYTE_NULL = 4,
2152 // Used by WriteUtf8 to replace orphan surrogate code units with the
2153 // unicode replacement character. Needs to be set to guarantee valid UTF-8
2154 // output.
2155 REPLACE_INVALID_UTF8 = 8
Steve Block6ded16b2010-05-10 14:33:55 +01002156 };
2157
Ben Murdoch589d6972011-11-30 16:04:58 +00002158 // 16-bit character codes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002159 int Write(uint16_t* buffer,
2160 int start = 0,
2161 int length = -1,
2162 int options = NO_OPTIONS) const;
2163 // One byte characters.
2164 int WriteOneByte(uint8_t* buffer,
2165 int start = 0,
2166 int length = -1,
2167 int options = NO_OPTIONS) const;
Ben Murdoch589d6972011-11-30 16:04:58 +00002168 // UTF-8 encoded characters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002169 int WriteUtf8(char* buffer,
2170 int length = -1,
2171 int* nchars_ref = NULL,
2172 int options = NO_OPTIONS) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002173
2174 /**
2175 * A zero length string.
2176 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002177 V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002178
2179 /**
2180 * Returns true if the string is external
2181 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002182 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002183
2184 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002185 * Returns true if the string is both external and one-byte.
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002187 bool IsExternalOneByte() const;
Leon Clarkee46be812010-01-19 14:06:41 +00002188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002189 class V8_EXPORT ExternalStringResourceBase { // NOLINT
Leon Clarkee46be812010-01-19 14:06:41 +00002190 public:
2191 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002192
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002193 virtual bool IsCompressible() const { return false; }
2194
Leon Clarkee46be812010-01-19 14:06:41 +00002195 protected:
2196 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002197
2198 /**
2199 * Internally V8 will call this Dispose method when the external string
2200 * resource is no longer needed. The default implementation will use the
2201 * delete operator. This method can be overridden in subclasses to
2202 * control how allocated external string resources are disposed.
2203 */
2204 virtual void Dispose() { delete this; }
2205
Leon Clarkee46be812010-01-19 14:06:41 +00002206 private:
2207 // Disallow copying and assigning.
2208 ExternalStringResourceBase(const ExternalStringResourceBase&);
2209 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002210
2211 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00002212 };
2213
Steve Blocka7e24c12009-10-30 11:49:00 +00002214 /**
2215 * An ExternalStringResource is a wrapper around a two-byte string
2216 * buffer that resides outside V8's heap. Implement an
2217 * ExternalStringResource to manage the life cycle of the underlying
2218 * buffer. Note that the string data must be immutable.
2219 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002220 class V8_EXPORT ExternalStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002221 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002222 public:
2223 /**
2224 * Override the destructor to manage the life cycle of the underlying
2225 * buffer.
2226 */
2227 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002228
2229 /**
2230 * The string data from the underlying buffer.
2231 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002232 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002233
2234 /**
2235 * The length of the string. That is, the number of two-byte characters.
2236 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002237 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002238
Steve Blocka7e24c12009-10-30 11:49:00 +00002239 protected:
2240 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002241 };
2242
2243 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002244 * An ExternalOneByteStringResource is a wrapper around an one-byte
Steve Blocka7e24c12009-10-30 11:49:00 +00002245 * string buffer that resides outside V8's heap. Implement an
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002246 * ExternalOneByteStringResource to manage the life cycle of the
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 * underlying buffer. Note that the string data must be immutable
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002248 * and that the data must be Latin-1 and not UTF-8, which would require
2249 * special treatment internally in the engine and do not allow efficient
2250 * indexing. Use String::New or convert to 16 bit data for non-Latin1.
Steve Blocka7e24c12009-10-30 11:49:00 +00002251 */
2252
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002253 class V8_EXPORT ExternalOneByteStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002254 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002255 public:
2256 /**
2257 * Override the destructor to manage the life cycle of the underlying
2258 * buffer.
2259 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002260 virtual ~ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002261 /** The string data from the underlying buffer.*/
2262 virtual const char* data() const = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002263 /** The number of Latin-1 characters in the string.*/
Steve Blocka7e24c12009-10-30 11:49:00 +00002264 virtual size_t length() const = 0;
2265 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002266 ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002267 };
2268
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002269 /**
2270 * If the string is an external string, return the ExternalStringResourceBase
2271 * regardless of the encoding, otherwise return NULL. The encoding of the
2272 * string is returned in encoding_out.
2273 */
2274 V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase(
2275 Encoding* encoding_out) const;
2276
Steve Blocka7e24c12009-10-30 11:49:00 +00002277 /**
2278 * Get the ExternalStringResource for an external string. Returns
2279 * NULL if IsExternal() doesn't return true.
2280 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002281 V8_INLINE ExternalStringResource* GetExternalStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002282
2283 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002284 * Get the ExternalOneByteStringResource for an external one-byte string.
2285 * Returns NULL if IsExternalOneByte() doesn't return true.
Steve Blocka7e24c12009-10-30 11:49:00 +00002286 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002287 const ExternalOneByteStringResource* GetExternalOneByteStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002288
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002289 V8_INLINE static String* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002290
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002291 // TODO(dcarney): remove with deprecation of New functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002292 enum NewStringType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002293 kNormalString = static_cast<int>(v8::NewStringType::kNormal),
2294 kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002295 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002296
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002297 /** Allocates a new string from UTF-8 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002298 static V8_DEPRECATE_SOON(
2299 "Use maybe version",
2300 Local<String> NewFromUtf8(Isolate* isolate, const char* data,
2301 NewStringType type = kNormalString,
2302 int length = -1));
2303
2304 /** Allocates a new string from UTF-8 data. Only returns an empty value when
2305 * length > kMaxLength. **/
2306 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8(
2307 Isolate* isolate, const char* data, v8::NewStringType type,
2308 int length = -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002309
2310 /** Allocates a new string from Latin-1 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002311 static V8_DEPRECATED(
2312 "Use maybe version",
2313 Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data,
2314 NewStringType type = kNormalString,
2315 int length = -1));
2316
2317 /** Allocates a new string from Latin-1 data. Only returns an empty value
2318 * when length > kMaxLength. **/
2319 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte(
2320 Isolate* isolate, const uint8_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002321 int length = -1);
2322
2323 /** Allocates a new string from UTF-16 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002324 static V8_DEPRECATE_SOON(
2325 "Use maybe version",
2326 Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data,
2327 NewStringType type = kNormalString,
2328 int length = -1));
2329
2330 /** Allocates a new string from UTF-16 data. Only returns an empty value when
2331 * length > kMaxLength. **/
2332 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte(
2333 Isolate* isolate, const uint16_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002334 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002335
2336 /**
Steve Block3ce2e202009-11-05 08:53:23 +00002337 * Creates a new string by concatenating the left and the right strings
2338 * passed in as parameters.
2339 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002340 static Local<String> Concat(Local<String> left, Local<String> right);
Steve Block3ce2e202009-11-05 08:53:23 +00002341
2342 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002343 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002344 * resource. When the external string is no longer live on V8's heap the
2345 * resource will be disposed by calling its Dispose method. The caller of
2346 * this function should not otherwise delete or modify the resource. Neither
2347 * should the underlying buffer be deallocated or modified except through the
2348 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002349 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002350 static V8_DEPRECATED("Use maybe version",
2351 Local<String> NewExternal(
2352 Isolate* isolate, ExternalStringResource* resource));
2353 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte(
2354 Isolate* isolate, ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002355
2356 /**
2357 * Associate an external string resource with this string by transforming it
2358 * in place so that existing references to this string in the JavaScript heap
2359 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002360 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002361 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002362 * The string is not modified if the operation fails. See NewExternal for
2363 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002364 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002365 bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002366
2367 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002368 * Creates a new external string using the one-byte data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002369 * resource. When the external string is no longer live on V8's heap the
2370 * resource will be disposed by calling its Dispose method. The caller of
2371 * this function should not otherwise delete or modify the resource. Neither
2372 * should the underlying buffer be deallocated or modified except through the
2373 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002374 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002375 static V8_DEPRECATE_SOON(
2376 "Use maybe version",
2377 Local<String> NewExternal(Isolate* isolate,
2378 ExternalOneByteStringResource* resource));
2379 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte(
2380 Isolate* isolate, ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002381
2382 /**
2383 * Associate an external string resource with this string by transforming it
2384 * in place so that existing references to this string in the JavaScript heap
2385 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002386 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002387 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002388 * The string is not modified if the operation fails. See NewExternal for
2389 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002390 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002391 bool MakeExternal(ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002392
2393 /**
2394 * Returns true if this string can be made external.
2395 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002396 bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002397
2398 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002399 * Converts an object to a UTF-8-encoded character array. Useful if
Steve Blocka7e24c12009-10-30 11:49:00 +00002400 * you want to print the object. If conversion to a string fails
Ben Murdoch589d6972011-11-30 16:04:58 +00002401 * (e.g. due to an exception in the toString() method of the object)
Steve Blocka7e24c12009-10-30 11:49:00 +00002402 * then the length() method returns 0 and the * operator returns
2403 * NULL.
2404 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002405 class V8_EXPORT Utf8Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002406 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002407 explicit Utf8Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002408 ~Utf8Value();
2409 char* operator*() { return str_; }
2410 const char* operator*() const { return str_; }
2411 int length() const { return length_; }
2412 private:
2413 char* str_;
2414 int length_;
2415
2416 // Disallow copying and assigning.
2417 Utf8Value(const Utf8Value&);
2418 void operator=(const Utf8Value&);
2419 };
2420
2421 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002422 * Converts an object to a two-byte string.
2423 * If conversion to a string fails (eg. due to an exception in the toString()
2424 * method of the object) then the length() method returns 0 and the * operator
2425 * returns NULL.
2426 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002427 class V8_EXPORT Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002428 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002429 explicit Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002430 ~Value();
2431 uint16_t* operator*() { return str_; }
2432 const uint16_t* operator*() const { return str_; }
2433 int length() const { return length_; }
2434 private:
2435 uint16_t* str_;
2436 int length_;
2437
2438 // Disallow copying and assigning.
2439 Value(const Value&);
2440 void operator=(const Value&);
2441 };
Steve Block3ce2e202009-11-05 08:53:23 +00002442
Steve Blocka7e24c12009-10-30 11:49:00 +00002443 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002444 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
2445 Encoding encoding) const;
2446 void VerifyExternalStringResource(ExternalStringResource* val) const;
2447 static void CheckCast(v8::Value* obj);
2448};
2449
2450
2451/**
2452 * A JavaScript symbol (ECMA-262 edition 6)
2453 *
2454 * This is an experimental feature. Use at your own risk.
2455 */
2456class V8_EXPORT Symbol : public Name {
2457 public:
2458 // Returns the print name string of the symbol, or undefined if none.
2459 Local<Value> Name() const;
2460
2461 // Create a symbol. If name is not empty, it will be used as the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002462 static Local<Symbol> New(Isolate* isolate,
2463 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002464
2465 // Access global symbol registry.
2466 // Note that symbols created this way are never collected, so
2467 // they should only be used for statically fixed properties.
2468 // Also, there is only one global name space for the names used as keys.
2469 // To minimize the potential for clashes, use qualified names as keys.
2470 static Local<Symbol> For(Isolate *isolate, Local<String> name);
2471
2472 // Retrieve a global symbol. Similar to |For|, but using a separate
2473 // registry that is not accessible by (and cannot clash with) JavaScript code.
2474 static Local<Symbol> ForApi(Isolate *isolate, Local<String> name);
2475
2476 // Well-known symbols
2477 static Local<Symbol> GetIterator(Isolate* isolate);
2478 static Local<Symbol> GetUnscopables(Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002479 static Local<Symbol> GetToStringTag(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002480 static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002481
2482 V8_INLINE static Symbol* Cast(v8::Value* obj);
2483
2484 private:
2485 Symbol();
2486 static void CheckCast(v8::Value* obj);
2487};
2488
2489
2490/**
2491 * A private symbol
2492 *
2493 * This is an experimental feature. Use at your own risk.
2494 */
2495class V8_EXPORT Private : public Data {
2496 public:
2497 // Returns the print name string of the private symbol, or undefined if none.
2498 Local<Value> Name() const;
2499
2500 // Create a private symbol. If name is not empty, it will be the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002501 static Local<Private> New(Isolate* isolate,
2502 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002503
2504 // Retrieve a global private symbol. If a symbol with this name has not
2505 // been retrieved in the same isolate before, it is created.
2506 // Note that private symbols created this way are never collected, so
2507 // they should only be used for statically fixed properties.
2508 // Also, there is only one global name space for the names used as keys.
2509 // To minimize the potential for clashes, use qualified names as keys,
2510 // e.g., "Class#property".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002511 static Local<Private> ForApi(Isolate* isolate, Local<String> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002512
2513 private:
2514 Private();
Steve Blocka7e24c12009-10-30 11:49:00 +00002515};
2516
2517
2518/**
2519 * A JavaScript number value (ECMA-262, 4.3.20)
2520 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521class V8_EXPORT Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002522 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002523 double Value() const;
2524 static Local<Number> New(Isolate* isolate, double value);
2525 V8_INLINE static Number* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002526 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002527 Number();
2528 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002529};
2530
2531
2532/**
2533 * A JavaScript value representing a signed integer.
2534 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535class V8_EXPORT Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00002536 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002537 static Local<Integer> New(Isolate* isolate, int32_t value);
2538 static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value);
2539 int64_t Value() const;
2540 V8_INLINE static Integer* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002541 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002542 Integer();
2543 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002544};
2545
2546
2547/**
2548 * A JavaScript value representing a 32-bit signed integer.
2549 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002550class V8_EXPORT Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002551 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002552 int32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002553 V8_INLINE static Int32* Cast(v8::Value* obj);
2554
Steve Blocka7e24c12009-10-30 11:49:00 +00002555 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002556 Int32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002557 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002558};
2559
2560
2561/**
2562 * A JavaScript value representing a 32-bit unsigned integer.
2563 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564class V8_EXPORT Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002565 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002566 uint32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002567 V8_INLINE static Uint32* Cast(v8::Value* obj);
2568
Steve Blocka7e24c12009-10-30 11:49:00 +00002569 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002570 Uint32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002571 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002572};
2573
2574
Steve Blocka7e24c12009-10-30 11:49:00 +00002575enum PropertyAttribute {
2576 None = 0,
2577 ReadOnly = 1 << 0,
2578 DontEnum = 1 << 1,
2579 DontDelete = 1 << 2
2580};
2581
2582/**
Leon Clarkef7060e22010-06-03 12:02:55 +01002583 * Accessor[Getter|Setter] are used as callback functions when
2584 * setting|getting a particular property. See Object and ObjectTemplate's
2585 * method SetAccessor.
2586 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587typedef void (*AccessorGetterCallback)(
2588 Local<String> property,
2589 const PropertyCallbackInfo<Value>& info);
2590typedef void (*AccessorNameGetterCallback)(
2591 Local<Name> property,
2592 const PropertyCallbackInfo<Value>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002593
2594
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002595typedef void (*AccessorSetterCallback)(
2596 Local<String> property,
2597 Local<Value> value,
2598 const PropertyCallbackInfo<void>& info);
2599typedef void (*AccessorNameSetterCallback)(
2600 Local<Name> property,
2601 Local<Value> value,
2602 const PropertyCallbackInfo<void>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002603
2604
2605/**
2606 * Access control specifications.
2607 *
2608 * Some accessors should be accessible across contexts. These
2609 * accessors have an explicit access control parameter which specifies
2610 * the kind of cross-context access that should be allowed.
2611 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002612 * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused.
Leon Clarkef7060e22010-06-03 12:02:55 +01002613 */
2614enum AccessControl {
2615 DEFAULT = 0,
2616 ALL_CAN_READ = 1,
2617 ALL_CAN_WRITE = 1 << 1,
2618 PROHIBITS_OVERWRITING = 1 << 2
2619};
2620
Ben Murdochda12d292016-06-02 14:46:10 +01002621/**
Ben Murdochc5610432016-08-08 18:44:38 +01002622 * Property filter bits. They can be or'ed to build a composite filter.
2623 */
2624enum PropertyFilter {
2625 ALL_PROPERTIES = 0,
2626 ONLY_WRITABLE = 1,
2627 ONLY_ENUMERABLE = 2,
2628 ONLY_CONFIGURABLE = 4,
2629 SKIP_STRINGS = 8,
2630 SKIP_SYMBOLS = 16
2631};
2632
2633/**
Ben Murdochda12d292016-06-02 14:46:10 +01002634 * Integrity level for objects.
2635 */
2636enum class IntegrityLevel { kFrozen, kSealed };
Leon Clarkef7060e22010-06-03 12:02:55 +01002637
2638/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002639 * A JavaScript object (ECMA-262, 4.3.3)
2640 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002641class V8_EXPORT Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002642 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002643 V8_DEPRECATE_SOON("Use maybe version",
2644 bool Set(Local<Value> key, Local<Value> value));
2645 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
2646 Local<Value> key, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002647
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002648 V8_DEPRECATE_SOON("Use maybe version",
2649 bool Set(uint32_t index, Local<Value> value));
2650 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
2651 Local<Value> value);
2652
2653 // Implements CreateDataProperty (ECMA-262, 7.3.4).
2654 //
2655 // Defines a configurable, writable, enumerable property with the given value
2656 // on the object unless the property already exists and is not configurable
2657 // or the object is not extensible.
2658 //
2659 // Returns true on success.
2660 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2661 Local<Name> key,
2662 Local<Value> value);
2663 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2664 uint32_t index,
2665 Local<Value> value);
2666
2667 // Implements DefineOwnProperty.
2668 //
2669 // In general, CreateDataProperty will be faster, however, does not allow
2670 // for specifying attributes.
2671 //
2672 // Returns true on success.
2673 V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty(
2674 Local<Context> context, Local<Name> key, Local<Value> value,
2675 PropertyAttribute attributes = None);
Steve Block6ded16b2010-05-10 14:33:55 +01002676
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002677 // Sets an own property on this object bypassing interceptors and
Steve Blocka7e24c12009-10-30 11:49:00 +00002678 // overriding accessors or read-only properties.
2679 //
2680 // Note that if the object has an interceptor the property will be set
2681 // locally, but since the interceptor takes precedence the local property
2682 // will only be returned if the interceptor doesn't return a value.
2683 //
2684 // Note also that this only works for named properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002685 V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty",
2686 bool ForceSet(Local<Value> key, Local<Value> value,
2687 PropertyAttribute attribs = None));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002688 V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty",
2689 Maybe<bool> ForceSet(Local<Context> context,
2690 Local<Value> key, Local<Value> value,
2691 PropertyAttribute attribs = None));
Steve Blocka7e24c12009-10-30 11:49:00 +00002692
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002693 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
2694 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2695 Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002696
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002697 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2698 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2699 uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01002700
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002701 /**
2702 * Gets the property attributes of a property which can be None or
2703 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2704 * None when the property doesn't exist.
2705 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002706 V8_DEPRECATED("Use maybe version",
2707 PropertyAttribute GetPropertyAttributes(Local<Value> key));
2708 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes(
2709 Local<Context> context, Local<Value> key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002710
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002711 /**
2712 * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3.
2713 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002714 V8_DEPRECATED("Use maybe version",
2715 Local<Value> GetOwnPropertyDescriptor(Local<String> key));
2716 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor(
2717 Local<Context> context, Local<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002718
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002719 V8_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key));
2720 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
2721 Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002722
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002723 V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key));
2724 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2725 Maybe<bool> Delete(Local<Context> context, Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002726
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002727 V8_DEPRECATED("Use maybe version", bool Has(uint32_t index));
2728 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002729
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002730 V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index));
2731 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2732 Maybe<bool> Delete(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002733
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002734 V8_DEPRECATED("Use maybe version",
2735 bool SetAccessor(Local<String> name,
2736 AccessorGetterCallback getter,
2737 AccessorSetterCallback setter = 0,
2738 Local<Value> data = Local<Value>(),
2739 AccessControl settings = DEFAULT,
2740 PropertyAttribute attribute = None));
2741 V8_DEPRECATED("Use maybe version",
2742 bool SetAccessor(Local<Name> name,
2743 AccessorNameGetterCallback getter,
2744 AccessorNameSetterCallback setter = 0,
2745 Local<Value> data = Local<Value>(),
2746 AccessControl settings = DEFAULT,
2747 PropertyAttribute attribute = None));
2748 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2749 Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name,
2750 AccessorNameGetterCallback getter,
2751 AccessorNameSetterCallback setter = 0,
2752 MaybeLocal<Value> data = MaybeLocal<Value>(),
2753 AccessControl settings = DEFAULT,
2754 PropertyAttribute attribute = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00002755
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002756 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
2757 Local<Function> setter = Local<Function>(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002758 PropertyAttribute attribute = None,
2759 AccessControl settings = DEFAULT);
2760
2761 /**
2762 * Functionality for private properties.
2763 * This is an experimental feature, use at your own risk.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002764 * Note: Private properties are not inherited. Do not rely on this, since it
2765 * may change.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002766 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002767 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
2768 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
2769 Local<Value> value);
2770 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
2771 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
Leon Clarkef7060e22010-06-03 12:02:55 +01002772
Steve Blocka7e24c12009-10-30 11:49:00 +00002773 /**
2774 * Returns an array containing the names of the enumerable properties
2775 * of this object, including properties from prototype objects. The
2776 * array returned by this method contains the same values as would
2777 * be enumerated by a for-in statement over this object.
2778 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002779 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames());
2780 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
2781 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002782
2783 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002784 * This function has the same functionality as GetPropertyNames but
2785 * the returned array doesn't contain the names of properties from
2786 * prototype objects.
2787 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002788 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames());
2789 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2790 Local<Context> context);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002791
2792 /**
Ben Murdochc5610432016-08-08 18:44:38 +01002793 * Returns an array containing the names of the filtered properties
2794 * of this object, including properties from prototype objects. The
2795 * array returned by this method contains the same values as would
2796 * be enumerated by a for-in statement over this object.
2797 */
2798 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2799 Local<Context> context, PropertyFilter filter);
2800
2801 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002802 * Get the prototype object. This does not skip objects marked to
2803 * be skipped by __proto__ and it does not consult the security
2804 * handler.
2805 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002806 Local<Value> GetPrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00002807
2808 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002809 * Set the prototype object. This does not skip objects marked to
2810 * be skipped by __proto__ and it does not consult the security
2811 * handler.
2812 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002813 V8_DEPRECATED("Use maybe version", bool SetPrototype(Local<Value> prototype));
2814 V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
2815 Local<Value> prototype);
Andrei Popescu402d9372010-02-26 13:31:12 +00002816
2817 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002818 * Finds an instance of the given function template in the prototype
2819 * chain.
2820 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002821 Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl);
Steve Blocka7e24c12009-10-30 11:49:00 +00002822
2823 /**
2824 * Call builtin Object.prototype.toString on this object.
2825 * This is different from Value::ToString() that may call
2826 * user-defined toString function. This one does not.
2827 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002828 V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString());
2829 V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString(
2830 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002831
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002832 /**
2833 * Returns the name of the function invoked as a constructor for this object.
2834 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002835 Local<String> GetConstructorName();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002836
Ben Murdochda12d292016-06-02 14:46:10 +01002837 /**
2838 * Sets the integrity level of the object.
2839 */
2840 Maybe<bool> SetIntegrityLevel(Local<Context> context, IntegrityLevel level);
2841
Steve Blocka7e24c12009-10-30 11:49:00 +00002842 /** Gets the number of internal fields for this Object. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002843 int InternalFieldCount();
2844
2845 /** Same as above, but works for Persistents */
2846 V8_INLINE static int InternalFieldCount(
2847 const PersistentBase<Object>& object) {
2848 return object.val_->InternalFieldCount();
2849 }
2850
2851 /** Gets the value from an internal field. */
2852 V8_INLINE Local<Value> GetInternalField(int index);
2853
Steve Blocka7e24c12009-10-30 11:49:00 +00002854 /** Sets the value in an internal field. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002855 void SetInternalField(int index, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002856
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002857 /**
2858 * Gets a 2-byte-aligned native pointer from an internal field. This field
2859 * must have been set by SetAlignedPointerInInternalField, everything else
2860 * leads to undefined behavior.
2861 */
2862 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00002863
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002864 /** Same as above, but works for Persistents */
2865 V8_INLINE static void* GetAlignedPointerFromInternalField(
2866 const PersistentBase<Object>& object, int index) {
2867 return object.val_->GetAlignedPointerFromInternalField(index);
2868 }
2869
2870 /**
2871 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2872 * a field, GetAlignedPointerFromInternalField must be used, everything else
2873 * leads to undefined behavior.
2874 */
2875 void SetAlignedPointerInInternalField(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002876
2877 // Testers for local properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002878 V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key));
2879 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2880 Local<Name> key);
Ben Murdochc5610432016-08-08 18:44:38 +01002881 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2882 uint32_t index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002883 V8_DEPRECATE_SOON("Use maybe version",
2884 bool HasRealNamedProperty(Local<String> key));
2885 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
2886 Local<Name> key);
2887 V8_DEPRECATE_SOON("Use maybe version",
2888 bool HasRealIndexedProperty(uint32_t index));
2889 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty(
2890 Local<Context> context, uint32_t index);
2891 V8_DEPRECATE_SOON("Use maybe version",
2892 bool HasRealNamedCallbackProperty(Local<String> key));
2893 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty(
2894 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002895
2896 /**
2897 * If result.IsEmpty() no real property was located in the prototype chain.
2898 * This means interceptors in the prototype chain are not called.
2899 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002900 V8_DEPRECATED(
2901 "Use maybe version",
2902 Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key));
2903 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain(
2904 Local<Context> context, Local<Name> key);
2905
2906 /**
2907 * Gets the property attributes of a real property in the prototype chain,
2908 * which can be None or any combination of ReadOnly, DontEnum and DontDelete.
2909 * Interceptors in the prototype chain are not called.
2910 */
2911 V8_DEPRECATED(
2912 "Use maybe version",
2913 Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain(
2914 Local<String> key));
2915 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute>
2916 GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context,
2917 Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002918
2919 /**
2920 * If result.IsEmpty() no real property was located on the object or
2921 * in the prototype chain.
2922 * This means interceptors in the prototype chain are not called.
2923 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002924 V8_DEPRECATED("Use maybe version",
2925 Local<Value> GetRealNamedProperty(Local<String> key));
2926 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty(
2927 Local<Context> context, Local<Name> key);
2928
2929 /**
2930 * Gets the property attributes of a real property which can be
2931 * None or any combination of ReadOnly, DontEnum and DontDelete.
2932 * Interceptors in the prototype chain are not called.
2933 */
2934 V8_DEPRECATED("Use maybe version",
2935 Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2936 Local<String> key));
2937 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2938 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002939
2940 /** Tests for a named lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002941 bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002942
2943 /** Tests for an index lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002944 bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002945
2946 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002947 * Returns the identity hash for this object. The current implementation
2948 * uses a hidden property on the object to store the identity hash.
Steve Blocka7e24c12009-10-30 11:49:00 +00002949 *
2950 * The return value will never be 0. Also, it is not guaranteed to be
2951 * unique.
2952 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002953 int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00002954
Steve Blocka7e24c12009-10-30 11:49:00 +00002955 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002956 * Clone this object with a fast but shallow copy. Values will point
2957 * to the same values as the original object.
2958 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002959 // TODO(dcarney): take an isolate and optionally bail out?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002960 Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00002961
2962 /**
Ben Murdoch8b112d22011-06-08 16:22:53 +01002963 * Returns the context in which the object was created.
2964 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002965 Local<Context> CreationContext();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002966
2967 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002968 * Checks whether a callback is set by the
2969 * ObjectTemplate::SetCallAsFunctionHandler method.
2970 * When an Object is callable this method returns true.
2971 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002972 bool IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00002973
2974 /**
Ben Murdochc5610432016-08-08 18:44:38 +01002975 * True if this object is a constructor.
2976 */
2977 bool IsConstructor();
2978
2979 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002980 * Call an Object as a function if a callback is set by the
Ben Murdoch257744e2011-11-30 15:57:28 +00002981 * ObjectTemplate::SetCallAsFunctionHandler method.
2982 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002983 V8_DEPRECATED("Use maybe version",
2984 Local<Value> CallAsFunction(Local<Value> recv, int argc,
2985 Local<Value> argv[]));
2986 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context,
2987 Local<Value> recv,
2988 int argc,
2989 Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00002990
2991 /**
2992 * Call an Object as a constructor if a callback is set by the
2993 * ObjectTemplate::SetCallAsFunctionHandler method.
2994 * Note: This method behaves like the Function::NewInstance method.
2995 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002996 V8_DEPRECATED("Use maybe version",
2997 Local<Value> CallAsConstructor(int argc, Local<Value> argv[]));
2998 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsConstructor(
2999 Local<Context> context, int argc, Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00003000
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003001 /**
3002 * Return the isolate to which the Object belongs to.
3003 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003004 V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003006 static Local<Object> New(Isolate* isolate);
3007
3008 V8_INLINE static Object* Cast(Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003009
Steve Blocka7e24c12009-10-30 11:49:00 +00003010 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003011 Object();
3012 static void CheckCast(Value* obj);
3013 Local<Value> SlowGetInternalField(int index);
3014 void* SlowGetAlignedPointerFromInternalField(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003015};
3016
3017
3018/**
3019 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
3020 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003021class V8_EXPORT Array : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003022 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003023 uint32_t Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00003024
3025 /**
3026 * Clones an element at index |index|. Returns an empty
3027 * handle if cloning fails (for any reason).
3028 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003029 V8_DEPRECATED("Cloning is not supported.",
3030 Local<Object> CloneElementAt(uint32_t index));
3031 V8_DEPRECATED("Cloning is not supported.",
3032 MaybeLocal<Object> CloneElementAt(Local<Context> context,
3033 uint32_t index));
Steve Blocka7e24c12009-10-30 11:49:00 +00003034
Steve Block44f0eee2011-05-26 01:26:41 +01003035 /**
3036 * Creates a JavaScript array with the given length. If the length
3037 * is negative the returned array will have length 0.
3038 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003039 static Local<Array> New(Isolate* isolate, int length = 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003040
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003041 V8_INLINE static Array* Cast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003042 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003043 Array();
3044 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003045};
3046
3047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003048/**
3049 * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
3050 */
3051class V8_EXPORT Map : public Object {
3052 public:
3053 size_t Size() const;
3054 void Clear();
3055 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
3056 Local<Value> key);
3057 V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
3058 Local<Value> key,
3059 Local<Value> value);
3060 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3061 Local<Value> key);
3062 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3063 Local<Value> key);
3064
3065 /**
3066 * Returns an array of length Size() * 2, where index N is the Nth key and
3067 * index N + 1 is the Nth value.
3068 */
3069 Local<Array> AsArray() const;
3070
3071 /**
3072 * Creates a new empty Map.
3073 */
3074 static Local<Map> New(Isolate* isolate);
3075
3076 V8_INLINE static Map* Cast(Value* obj);
3077
3078 private:
3079 Map();
3080 static void CheckCast(Value* obj);
3081};
3082
3083
3084/**
3085 * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
3086 */
3087class V8_EXPORT Set : public Object {
3088 public:
3089 size_t Size() const;
3090 void Clear();
3091 V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
3092 Local<Value> key);
3093 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3094 Local<Value> key);
3095 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3096 Local<Value> key);
3097
3098 /**
3099 * Returns an array of the keys in this Set.
3100 */
3101 Local<Array> AsArray() const;
3102
3103 /**
3104 * Creates a new empty Set.
3105 */
3106 static Local<Set> New(Isolate* isolate);
3107
3108 V8_INLINE static Set* Cast(Value* obj);
3109
3110 private:
3111 Set();
3112 static void CheckCast(Value* obj);
3113};
3114
3115
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003116template<typename T>
3117class ReturnValue {
3118 public:
3119 template <class S> V8_INLINE ReturnValue(const ReturnValue<S>& that)
3120 : value_(that.value_) {
3121 TYPE_CHECK(T, S);
3122 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003123 // Local setters
3124 template <typename S>
3125 V8_INLINE V8_DEPRECATE_SOON("Use Global<> instead",
3126 void Set(const Persistent<S>& handle));
3127 template <typename S>
3128 V8_INLINE void Set(const Global<S>& handle);
3129 template <typename S>
3130 V8_INLINE void Set(const Local<S> handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131 // Fast primitive setters
3132 V8_INLINE void Set(bool value);
3133 V8_INLINE void Set(double i);
3134 V8_INLINE void Set(int32_t i);
3135 V8_INLINE void Set(uint32_t i);
3136 // Fast JS primitive setters
3137 V8_INLINE void SetNull();
3138 V8_INLINE void SetUndefined();
3139 V8_INLINE void SetEmptyString();
3140 // Convenience getter for Isolate
Ben Murdochda12d292016-06-02 14:46:10 +01003141 V8_INLINE Isolate* GetIsolate() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003142
3143 // Pointer setter: Uncompilable to prevent inadvertent misuse.
3144 template <typename S>
3145 V8_INLINE void Set(S* whatever);
3146
Ben Murdochda12d292016-06-02 14:46:10 +01003147 // Getter. Creates a new Local<> so it comes with a certain performance
3148 // hit. If the ReturnValue was not yet set, this will return the undefined
3149 // value.
3150 V8_INLINE Local<Value> Get() const;
3151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003152 private:
3153 template<class F> friend class ReturnValue;
3154 template<class F> friend class FunctionCallbackInfo;
3155 template<class F> friend class PropertyCallbackInfo;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003156 template <class F, class G, class H>
3157 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003158 V8_INLINE void SetInternal(internal::Object* value) { *value_ = value; }
3159 V8_INLINE internal::Object* GetDefaultValue();
3160 V8_INLINE explicit ReturnValue(internal::Object** slot);
3161 internal::Object** value_;
3162};
3163
3164
3165/**
3166 * The argument information given to function call callbacks. This
3167 * class provides access to information about the context of the call,
3168 * including the receiver, the number and values of arguments, and
3169 * the holder of the function.
3170 */
3171template<typename T>
3172class FunctionCallbackInfo {
3173 public:
3174 V8_INLINE int Length() const;
3175 V8_INLINE Local<Value> operator[](int i) const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003176 V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead",
3177 Local<Function> Callee() const);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003178 V8_INLINE Local<Object> This() const;
3179 V8_INLINE Local<Object> Holder() const;
Ben Murdochc5610432016-08-08 18:44:38 +01003180 V8_INLINE Local<Value> NewTarget() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003181 V8_INLINE bool IsConstructCall() const;
3182 V8_INLINE Local<Value> Data() const;
3183 V8_INLINE Isolate* GetIsolate() const;
3184 V8_INLINE ReturnValue<T> GetReturnValue() const;
3185 // This shouldn't be public, but the arm compiler needs it.
Ben Murdochc5610432016-08-08 18:44:38 +01003186 static const int kArgsLength = 8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003187
3188 protected:
3189 friend class internal::FunctionCallbackArguments;
3190 friend class internal::CustomArguments<FunctionCallbackInfo>;
3191 static const int kHolderIndex = 0;
3192 static const int kIsolateIndex = 1;
3193 static const int kReturnValueDefaultValueIndex = 2;
3194 static const int kReturnValueIndex = 3;
3195 static const int kDataIndex = 4;
3196 static const int kCalleeIndex = 5;
3197 static const int kContextSaveIndex = 6;
Ben Murdochc5610432016-08-08 18:44:38 +01003198 static const int kNewTargetIndex = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003199
3200 V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args,
Ben Murdochc5610432016-08-08 18:44:38 +01003201 internal::Object** values, int length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003202 internal::Object** implicit_args_;
3203 internal::Object** values_;
3204 int length_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003205};
3206
3207
3208/**
3209 * The information passed to a property callback about the context
3210 * of the property access.
3211 */
3212template<typename T>
3213class PropertyCallbackInfo {
3214 public:
3215 V8_INLINE Isolate* GetIsolate() const;
3216 V8_INLINE Local<Value> Data() const;
3217 V8_INLINE Local<Object> This() const;
3218 V8_INLINE Local<Object> Holder() const;
3219 V8_INLINE ReturnValue<T> GetReturnValue() const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003220 V8_INLINE bool ShouldThrowOnError() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003221 // This shouldn't be public, but the arm compiler needs it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003222 static const int kArgsLength = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003223
3224 protected:
3225 friend class MacroAssembler;
3226 friend class internal::PropertyCallbackArguments;
3227 friend class internal::CustomArguments<PropertyCallbackInfo>;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003228 static const int kShouldThrowOnErrorIndex = 0;
3229 static const int kHolderIndex = 1;
3230 static const int kIsolateIndex = 2;
3231 static const int kReturnValueDefaultValueIndex = 3;
3232 static const int kReturnValueIndex = 4;
3233 static const int kDataIndex = 5;
3234 static const int kThisIndex = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003235
3236 V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {}
3237 internal::Object** args_;
3238};
3239
3240
3241typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
3242
Ben Murdochc5610432016-08-08 18:44:38 +01003243enum class ConstructorBehavior { kThrow, kAllow };
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003244
Steve Blocka7e24c12009-10-30 11:49:00 +00003245/**
3246 * A JavaScript function object (ECMA-262, 15.3).
3247 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003248class V8_EXPORT Function : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003249 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003250 /**
3251 * Create a function in the current execution context
3252 * for a given FunctionCallback.
3253 */
Ben Murdochc5610432016-08-08 18:44:38 +01003254 static MaybeLocal<Function> New(
3255 Local<Context> context, FunctionCallback callback,
3256 Local<Value> data = Local<Value>(), int length = 0,
3257 ConstructorBehavior behavior = ConstructorBehavior::kAllow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003258 static V8_DEPRECATE_SOON(
3259 "Use maybe version",
3260 Local<Function> New(Isolate* isolate, FunctionCallback callback,
3261 Local<Value> data = Local<Value>(), int length = 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003263 V8_DEPRECATED("Use maybe version",
3264 Local<Object> NewInstance(int argc, Local<Value> argv[]) const);
3265 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3266 Local<Context> context, int argc, Local<Value> argv[]) const;
3267
3268 V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const);
3269 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3270 Local<Context> context) const {
3271 return NewInstance(context, 0, nullptr);
3272 }
3273
3274 V8_DEPRECATE_SOON("Use maybe version",
3275 Local<Value> Call(Local<Value> recv, int argc,
3276 Local<Value> argv[]));
3277 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
3278 Local<Value> recv, int argc,
3279 Local<Value> argv[]);
3280
3281 void SetName(Local<String> name);
3282 Local<Value> GetName() const;
Andrei Popescu402d9372010-02-26 13:31:12 +00003283
3284 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003285 * Name inferred from variable or property assignment of this function.
3286 * Used to facilitate debugging and profiling of JavaScript code written
3287 * in an OO style, where many functions are anonymous but are assigned
3288 * to object properties.
3289 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003290 Local<Value> GetInferredName() const;
3291
3292 /**
3293 * displayName if it is set, otherwise name if it is configured, otherwise
3294 * function name, otherwise inferred name.
3295 */
3296 Local<Value> GetDebugName() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003297
3298 /**
3299 * User-defined name assigned to the "displayName" property of this function.
3300 * Used to facilitate debugging and profiling of JavaScript code.
3301 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003302 Local<Value> GetDisplayName() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003303
3304 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00003305 * Returns zero based line number of function body and
3306 * kLineOffsetNotFound if no information available.
3307 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003308 int GetScriptLineNumber() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003309 /**
3310 * Returns zero based column number of function body and
3311 * kLineOffsetNotFound if no information available.
3312 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003313 int GetScriptColumnNumber() const;
3314
3315 /**
3316 * Tells whether this function is builtin.
3317 */
3318 bool IsBuiltin() const;
3319
3320 /**
3321 * Returns scriptId.
3322 */
3323 int ScriptId() const;
3324
3325 /**
3326 * Returns the original function if this function is bound, else returns
3327 * v8::Undefined.
3328 */
3329 Local<Value> GetBoundFunction() const;
3330
3331 ScriptOrigin GetScriptOrigin() const;
3332 V8_INLINE static Function* Cast(Value* obj);
3333 static const int kLineOffsetNotFound;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003334
Steve Blocka7e24c12009-10-30 11:49:00 +00003335 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003336 Function();
3337 static void CheckCast(Value* obj);
3338};
3339
3340
3341/**
3342 * An instance of the built-in Promise constructor (ES6 draft).
3343 * This API is experimental. Only works with --harmony flag.
3344 */
3345class V8_EXPORT Promise : public Object {
3346 public:
3347 class V8_EXPORT Resolver : public Object {
3348 public:
3349 /**
3350 * Create a new resolver, along with an associated promise in pending state.
3351 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003352 static V8_DEPRECATE_SOON("Use maybe version",
3353 Local<Resolver> New(Isolate* isolate));
3354 static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New(
3355 Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003356
3357 /**
3358 * Extract the associated promise.
3359 */
3360 Local<Promise> GetPromise();
3361
3362 /**
3363 * Resolve/reject the associated promise with a given value.
3364 * Ignored if the promise is no longer pending.
3365 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003366 V8_DEPRECATE_SOON("Use maybe version", void Resolve(Local<Value> value));
3367 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3368 Maybe<bool> Resolve(Local<Context> context, Local<Value> value);
3369
3370 V8_DEPRECATE_SOON("Use maybe version", void Reject(Local<Value> value));
3371 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3372 Maybe<bool> Reject(Local<Context> context, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003373
3374 V8_INLINE static Resolver* Cast(Value* obj);
3375
3376 private:
3377 Resolver();
3378 static void CheckCast(Value* obj);
3379 };
3380
3381 /**
3382 * Register a resolution/rejection handler with a promise.
3383 * The handler is given the respective resolution/rejection value as
3384 * an argument. If the promise is already resolved/rejected, the handler is
3385 * invoked at the end of turn.
3386 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003387 V8_DEPRECATED("Use maybe version of Then",
3388 Local<Promise> Chain(Local<Function> handler));
3389 V8_DEPRECATED("Use Then",
3390 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Chain(
3391 Local<Context> context, Local<Function> handler));
3392
3393 V8_DEPRECATED("Use maybe version",
3394 Local<Promise> Catch(Local<Function> handler));
3395 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context,
3396 Local<Function> handler);
3397
3398 V8_DEPRECATED("Use maybe version",
3399 Local<Promise> Then(Local<Function> handler));
3400 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
3401 Local<Function> handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003402
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003403 /**
3404 * Returns true if the promise has at least one derived promise, and
3405 * therefore resolve/reject handlers (including default handler).
3406 */
3407 bool HasHandler();
3408
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003409 V8_INLINE static Promise* Cast(Value* obj);
3410
3411 private:
3412 Promise();
3413 static void CheckCast(Value* obj);
3414};
3415
3416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003417/**
3418 * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition,
3419 * 26.2.1).
3420 */
3421class V8_EXPORT Proxy : public Object {
3422 public:
3423 Local<Object> GetTarget();
3424 Local<Value> GetHandler();
3425 bool IsRevoked();
3426 void Revoke();
3427
3428 /**
3429 * Creates a new empty Map.
3430 */
3431 static MaybeLocal<Proxy> New(Local<Context> context,
3432 Local<Object> local_target,
3433 Local<Object> local_handler);
3434
3435 V8_INLINE static Proxy* Cast(Value* obj);
3436
3437 private:
3438 Proxy();
3439 static void CheckCast(Value* obj);
3440};
3441
3442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003443#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
3444// The number of required internal fields can be defined by embedder.
3445#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
3446#endif
3447
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003448
3449enum class ArrayBufferCreationMode { kInternalized, kExternalized };
3450
3451
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003452/**
3453 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
3454 * This API is experimental and may change significantly.
3455 */
3456class V8_EXPORT ArrayBuffer : public Object {
3457 public:
3458 /**
Ben Murdochc5610432016-08-08 18:44:38 +01003459 * A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003460 * The allocator is a global V8 setting. It has to be set via
3461 * Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003462 *
Ben Murdochc5610432016-08-08 18:44:38 +01003463 * Memory allocated through this allocator by V8 is accounted for as external
3464 * memory by V8. Note that V8 keeps track of the memory for all internalized
3465 * |ArrayBuffer|s. Responsibility for tracking external memory (using
3466 * Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the
3467 * embedder upon externalization and taken over upon internalization (creating
3468 * an internalized buffer from an existing buffer).
3469 *
3470 * Note that it is unsafe to call back into V8 from any of the allocator
3471 * functions.
3472 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003473 * This API is experimental and may change significantly.
3474 */
3475 class V8_EXPORT Allocator { // NOLINT
3476 public:
3477 virtual ~Allocator() {}
3478
3479 /**
3480 * Allocate |length| bytes. Return NULL if allocation is not successful.
3481 * Memory should be initialized to zeroes.
3482 */
3483 virtual void* Allocate(size_t length) = 0;
3484
3485 /**
3486 * Allocate |length| bytes. Return NULL if allocation is not successful.
3487 * Memory does not have to be initialized.
3488 */
3489 virtual void* AllocateUninitialized(size_t length) = 0;
3490 /**
3491 * Free the memory block of size |length|, pointed to by |data|.
3492 * That memory is guaranteed to be previously allocated by |Allocate|.
3493 */
3494 virtual void Free(void* data, size_t length) = 0;
3495 };
3496
3497 /**
3498 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
3499 * returns an instance of this class, populated, with a pointer to data
3500 * and byte length.
3501 *
3502 * The Data pointer of ArrayBuffer::Contents is always allocated with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003503 * Allocator::Allocate that is set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003504 *
3505 * This API is experimental and may change significantly.
3506 */
3507 class V8_EXPORT Contents { // NOLINT
3508 public:
3509 Contents() : data_(NULL), byte_length_(0) {}
3510
3511 void* Data() const { return data_; }
3512 size_t ByteLength() const { return byte_length_; }
3513
3514 private:
3515 void* data_;
3516 size_t byte_length_;
3517
3518 friend class ArrayBuffer;
3519 };
3520
3521
3522 /**
3523 * Data length in bytes.
3524 */
3525 size_t ByteLength() const;
3526
3527 /**
3528 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
3529 * Allocated memory will be owned by a created ArrayBuffer and
3530 * will be deallocated when it is garbage-collected,
3531 * unless the object is externalized.
3532 */
3533 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
3534
3535 /**
3536 * Create a new ArrayBuffer over an existing memory block.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003537 * The created array buffer is by default immediately in externalized state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003538 * The memory block will not be reclaimed when a created ArrayBuffer
3539 * is garbage-collected.
3540 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003541 static Local<ArrayBuffer> New(
3542 Isolate* isolate, void* data, size_t byte_length,
3543 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003544
3545 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003546 * Returns true if ArrayBuffer is externalized, that is, does not
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003547 * own its memory block.
3548 */
3549 bool IsExternal() const;
3550
3551 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003552 * Returns true if this ArrayBuffer may be neutered.
3553 */
3554 bool IsNeuterable() const;
3555
3556 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003557 * Neuters this ArrayBuffer and all its views (typed arrays).
3558 * Neutering sets the byte length of the buffer and all typed arrays to zero,
3559 * preventing JavaScript from ever accessing underlying backing store.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003560 * ArrayBuffer should have been externalized and must be neuterable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003561 */
3562 void Neuter();
3563
3564 /**
3565 * Make this ArrayBuffer external. The pointer to underlying memory block
3566 * and byte length are returned as |Contents| structure. After ArrayBuffer
3567 * had been etxrenalized, it does no longer owns the memory block. The caller
3568 * should take steps to free memory when it is no longer needed.
3569 *
3570 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003571 * that has been set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003572 */
3573 Contents Externalize();
3574
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003575 /**
3576 * Get a pointer to the ArrayBuffer's underlying memory block without
3577 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3578 * will become invalid as soon as the ArrayBuffer became garbage collected.
3579 *
3580 * The embedder should make sure to hold a strong reference to the
3581 * ArrayBuffer while accessing this pointer.
3582 *
3583 * The memory block is guaranteed to be allocated with |Allocator::Allocate|.
3584 */
3585 Contents GetContents();
3586
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003587 V8_INLINE static ArrayBuffer* Cast(Value* obj);
3588
3589 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3590
3591 private:
3592 ArrayBuffer();
3593 static void CheckCast(Value* obj);
3594};
3595
3596
3597#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
3598// The number of required internal fields can be defined by embedder.
3599#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
3600#endif
3601
3602
3603/**
3604 * A base class for an instance of one of "views" over ArrayBuffer,
3605 * including TypedArrays and DataView (ES6 draft 15.13).
3606 *
3607 * This API is experimental and may change significantly.
3608 */
3609class V8_EXPORT ArrayBufferView : public Object {
3610 public:
3611 /**
3612 * Returns underlying ArrayBuffer.
3613 */
3614 Local<ArrayBuffer> Buffer();
3615 /**
3616 * Byte offset in |Buffer|.
3617 */
3618 size_t ByteOffset();
3619 /**
3620 * Size of a view in bytes.
3621 */
3622 size_t ByteLength();
3623
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003624 /**
3625 * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3626 * memory without additional overhead that calling ArrayBufferView::Buffer
3627 * might incur.
3628 *
3629 * Will write at most min(|byte_length|, ByteLength) bytes starting at
3630 * ByteOffset of the underling buffer to the memory starting at |dest|.
3631 * Returns the number of bytes actually written.
3632 */
3633 size_t CopyContents(void* dest, size_t byte_length);
3634
3635 /**
3636 * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3637 * allocated.
3638 */
3639 bool HasBuffer() const;
3640
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003641 V8_INLINE static ArrayBufferView* Cast(Value* obj);
3642
3643 static const int kInternalFieldCount =
3644 V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3645
3646 private:
3647 ArrayBufferView();
3648 static void CheckCast(Value* obj);
3649};
3650
3651
3652/**
3653 * A base class for an instance of TypedArray series of constructors
3654 * (ES6 draft 15.13.6).
3655 * This API is experimental and may change significantly.
3656 */
3657class V8_EXPORT TypedArray : public ArrayBufferView {
3658 public:
3659 /**
3660 * Number of elements in this typed array
3661 * (e.g. for Int16Array, |ByteLength|/2).
3662 */
3663 size_t Length();
3664
3665 V8_INLINE static TypedArray* Cast(Value* obj);
3666
3667 private:
3668 TypedArray();
3669 static void CheckCast(Value* obj);
3670};
3671
3672
3673/**
3674 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
3675 * This API is experimental and may change significantly.
3676 */
3677class V8_EXPORT Uint8Array : public TypedArray {
3678 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003679 static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
3680 size_t byte_offset, size_t length);
3681 static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003682 size_t byte_offset, size_t length);
3683 V8_INLINE static Uint8Array* Cast(Value* obj);
3684
3685 private:
3686 Uint8Array();
3687 static void CheckCast(Value* obj);
3688};
3689
3690
3691/**
3692 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
3693 * This API is experimental and may change significantly.
3694 */
3695class V8_EXPORT Uint8ClampedArray : public TypedArray {
3696 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003697 static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
3698 size_t byte_offset, size_t length);
3699 static Local<Uint8ClampedArray> New(
3700 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
3701 size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003702 V8_INLINE static Uint8ClampedArray* Cast(Value* obj);
3703
3704 private:
3705 Uint8ClampedArray();
3706 static void CheckCast(Value* obj);
3707};
3708
3709/**
3710 * An instance of Int8Array constructor (ES6 draft 15.13.6).
3711 * This API is experimental and may change significantly.
3712 */
3713class V8_EXPORT Int8Array : public TypedArray {
3714 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003715 static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
3716 size_t byte_offset, size_t length);
3717 static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3718 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003719 V8_INLINE static Int8Array* Cast(Value* obj);
3720
3721 private:
3722 Int8Array();
3723 static void CheckCast(Value* obj);
3724};
3725
3726
3727/**
3728 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
3729 * This API is experimental and may change significantly.
3730 */
3731class V8_EXPORT Uint16Array : public TypedArray {
3732 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003733 static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
3734 size_t byte_offset, size_t length);
3735 static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3736 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003737 V8_INLINE static Uint16Array* Cast(Value* obj);
3738
3739 private:
3740 Uint16Array();
3741 static void CheckCast(Value* obj);
3742};
3743
3744
3745/**
3746 * An instance of Int16Array constructor (ES6 draft 15.13.6).
3747 * This API is experimental and may change significantly.
3748 */
3749class V8_EXPORT Int16Array : public TypedArray {
3750 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003751 static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
3752 size_t byte_offset, size_t length);
3753 static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003754 size_t byte_offset, size_t length);
3755 V8_INLINE static Int16Array* Cast(Value* obj);
3756
3757 private:
3758 Int16Array();
3759 static void CheckCast(Value* obj);
3760};
3761
3762
3763/**
3764 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
3765 * This API is experimental and may change significantly.
3766 */
3767class V8_EXPORT Uint32Array : public TypedArray {
3768 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003769 static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
3770 size_t byte_offset, size_t length);
3771 static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3772 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003773 V8_INLINE static Uint32Array* Cast(Value* obj);
3774
3775 private:
3776 Uint32Array();
3777 static void CheckCast(Value* obj);
3778};
3779
3780
3781/**
3782 * An instance of Int32Array constructor (ES6 draft 15.13.6).
3783 * This API is experimental and may change significantly.
3784 */
3785class V8_EXPORT Int32Array : public TypedArray {
3786 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003787 static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
3788 size_t byte_offset, size_t length);
3789 static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003790 size_t byte_offset, size_t length);
3791 V8_INLINE static Int32Array* Cast(Value* obj);
3792
3793 private:
3794 Int32Array();
3795 static void CheckCast(Value* obj);
3796};
3797
3798
3799/**
3800 * An instance of Float32Array constructor (ES6 draft 15.13.6).
3801 * This API is experimental and may change significantly.
3802 */
3803class V8_EXPORT Float32Array : public TypedArray {
3804 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003805 static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
3806 size_t byte_offset, size_t length);
3807 static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3808 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003809 V8_INLINE static Float32Array* Cast(Value* obj);
3810
3811 private:
3812 Float32Array();
3813 static void CheckCast(Value* obj);
3814};
3815
3816
3817/**
3818 * An instance of Float64Array constructor (ES6 draft 15.13.6).
3819 * This API is experimental and may change significantly.
3820 */
3821class V8_EXPORT Float64Array : public TypedArray {
3822 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003823 static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
3824 size_t byte_offset, size_t length);
3825 static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3826 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003827 V8_INLINE static Float64Array* Cast(Value* obj);
3828
3829 private:
3830 Float64Array();
3831 static void CheckCast(Value* obj);
3832};
3833
3834
3835/**
3836 * An instance of DataView constructor (ES6 draft 15.13.7).
3837 * This API is experimental and may change significantly.
3838 */
3839class V8_EXPORT DataView : public ArrayBufferView {
3840 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003841 static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3842 size_t byte_offset, size_t length);
3843 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003844 size_t byte_offset, size_t length);
3845 V8_INLINE static DataView* Cast(Value* obj);
3846
3847 private:
3848 DataView();
3849 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003850};
3851
3852
3853/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003854 * An instance of the built-in SharedArrayBuffer constructor.
3855 * This API is experimental and may change significantly.
3856 */
3857class V8_EXPORT SharedArrayBuffer : public Object {
3858 public:
3859 /**
3860 * The contents of an |SharedArrayBuffer|. Externalization of
3861 * |SharedArrayBuffer| returns an instance of this class, populated, with a
3862 * pointer to data and byte length.
3863 *
3864 * The Data pointer of SharedArrayBuffer::Contents is always allocated with
3865 * |ArrayBuffer::Allocator::Allocate| by the allocator specified in
3866 * v8::Isolate::CreateParams::array_buffer_allocator.
3867 *
3868 * This API is experimental and may change significantly.
3869 */
3870 class V8_EXPORT Contents { // NOLINT
3871 public:
3872 Contents() : data_(NULL), byte_length_(0) {}
3873
3874 void* Data() const { return data_; }
3875 size_t ByteLength() const { return byte_length_; }
3876
3877 private:
3878 void* data_;
3879 size_t byte_length_;
3880
3881 friend class SharedArrayBuffer;
3882 };
3883
3884
3885 /**
3886 * Data length in bytes.
3887 */
3888 size_t ByteLength() const;
3889
3890 /**
3891 * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3892 * Allocated memory will be owned by a created SharedArrayBuffer and
3893 * will be deallocated when it is garbage-collected,
3894 * unless the object is externalized.
3895 */
3896 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3897
3898 /**
3899 * Create a new SharedArrayBuffer over an existing memory block. The created
3900 * array buffer is immediately in externalized state unless otherwise
3901 * specified. The memory block will not be reclaimed when a created
3902 * SharedArrayBuffer is garbage-collected.
3903 */
3904 static Local<SharedArrayBuffer> New(
3905 Isolate* isolate, void* data, size_t byte_length,
3906 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
3907
3908 /**
3909 * Returns true if SharedArrayBuffer is externalized, that is, does not
3910 * own its memory block.
3911 */
3912 bool IsExternal() const;
3913
3914 /**
3915 * Make this SharedArrayBuffer external. The pointer to underlying memory
3916 * block and byte length are returned as |Contents| structure. After
3917 * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory
3918 * block. The caller should take steps to free memory when it is no longer
3919 * needed.
3920 *
3921 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3922 * by the allocator specified in
3923 * v8::Isolate::CreateParams::array_buffer_allocator.
3924 *
3925 */
3926 Contents Externalize();
3927
3928 /**
3929 * Get a pointer to the ArrayBuffer's underlying memory block without
3930 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3931 * will become invalid as soon as the ArrayBuffer became garbage collected.
3932 *
3933 * The embedder should make sure to hold a strong reference to the
3934 * ArrayBuffer while accessing this pointer.
3935 *
3936 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3937 * by the allocator specified in
3938 * v8::Isolate::CreateParams::array_buffer_allocator.
3939 */
3940 Contents GetContents();
3941
3942 V8_INLINE static SharedArrayBuffer* Cast(Value* obj);
3943
3944 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3945
3946 private:
3947 SharedArrayBuffer();
3948 static void CheckCast(Value* obj);
3949};
3950
3951
3952/**
Ben Murdoch257744e2011-11-30 15:57:28 +00003953 * An instance of the built-in Date constructor (ECMA-262, 15.9).
3954 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003955class V8_EXPORT Date : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00003956 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003957 static V8_DEPRECATE_SOON("Use maybe version.",
3958 Local<Value> New(Isolate* isolate, double time));
3959 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
3960 double time);
Ben Murdoch257744e2011-11-30 15:57:28 +00003961
3962 /**
3963 * A specialization of Value::NumberValue that is more efficient
3964 * because we know the structure of this object.
3965 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003966 double ValueOf() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00003967
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003968 V8_INLINE static Date* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003969
3970 /**
3971 * Notification that the embedder has changed the time zone,
3972 * daylight savings time, or other date / time configuration
3973 * parameters. V8 keeps a cache of various values used for
3974 * date / time computation. This notification will reset
3975 * those cached values for the current context so that date /
3976 * time configuration changes would be reflected in the Date
3977 * object.
3978 *
3979 * This API should not be called more than needed as it will
3980 * negatively impact the performance of date operations.
3981 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003982 static void DateTimeConfigurationChangeNotification(Isolate* isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00003983
3984 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003985 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003986};
3987
3988
3989/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003990 * A Number object (ECMA-262, 4.3.21).
3991 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003992class V8_EXPORT NumberObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003993 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003994 static Local<Value> New(Isolate* isolate, double value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003995
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003996 double ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003997
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003998 V8_INLINE static NumberObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003999
4000 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004001 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004002};
4003
4004
4005/**
4006 * A Boolean object (ECMA-262, 4.3.15).
4007 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004008class V8_EXPORT BooleanObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004009 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004010 static Local<Value> New(Isolate* isolate, bool value);
4011 V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004012
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004013 bool ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004015 V8_INLINE static BooleanObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004016
4017 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004018 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004019};
4020
4021
4022/**
4023 * A String object (ECMA-262, 4.3.18).
4024 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004025class V8_EXPORT StringObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004026 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004027 static Local<Value> New(Local<String> value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004028
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004029 Local<String> ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004030
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004031 V8_INLINE static StringObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004032
4033 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004034 static void CheckCast(v8::Value* obj);
4035};
4036
4037
4038/**
4039 * A Symbol object (ECMA-262 edition 6).
4040 *
4041 * This is an experimental feature. Use at your own risk.
4042 */
4043class V8_EXPORT SymbolObject : public Object {
4044 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004045 static Local<Value> New(Isolate* isolate, Local<Symbol> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004046
4047 Local<Symbol> ValueOf() const;
4048
4049 V8_INLINE static SymbolObject* Cast(v8::Value* obj);
4050
4051 private:
4052 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004053};
4054
4055
4056/**
Ben Murdoch257744e2011-11-30 15:57:28 +00004057 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
4058 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004059class V8_EXPORT RegExp : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00004060 public:
4061 /**
4062 * Regular expression flag bits. They can be or'ed to enable a set
4063 * of flags.
4064 */
4065 enum Flags {
4066 kNone = 0,
4067 kGlobal = 1,
4068 kIgnoreCase = 2,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004069 kMultiline = 4,
4070 kSticky = 8,
4071 kUnicode = 16
Ben Murdoch257744e2011-11-30 15:57:28 +00004072 };
4073
4074 /**
4075 * Creates a regular expression from the given pattern string and
4076 * the flags bit field. May throw a JavaScript exception as
4077 * described in ECMA-262, 15.10.4.1.
4078 *
4079 * For example,
4080 * RegExp::New(v8::String::New("foo"),
4081 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
4082 * is equivalent to evaluating "/foo/gm".
4083 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004084 static V8_DEPRECATE_SOON("Use maybe version",
4085 Local<RegExp> New(Local<String> pattern,
4086 Flags flags));
4087 static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
4088 Local<String> pattern,
4089 Flags flags);
Ben Murdoch257744e2011-11-30 15:57:28 +00004090
4091 /**
4092 * Returns the value of the source property: a string representing
4093 * the regular expression.
4094 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004095 Local<String> GetSource() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004096
4097 /**
4098 * Returns the flags bit field.
4099 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004100 Flags GetFlags() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004101
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004102 V8_INLINE static RegExp* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004103
4104 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004105 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004106};
4107
4108
4109/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004110 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
4111 * to associate C++ data structures with JavaScript objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00004112 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004113class V8_EXPORT External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00004114 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004115 static Local<External> New(Isolate* isolate, void* value);
4116 V8_INLINE static External* Cast(Value* obj);
4117 void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00004118 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004119 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004120};
4121
4122
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004123#define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator)
4124
4125enum Intrinsic {
4126#define V8_DECL_INTRINSIC(name, iname) k##name,
4127 V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
4128#undef V8_DECL_INTRINSIC
4129};
4130
4131
Ben Murdoch257744e2011-11-30 15:57:28 +00004132// --- Templates ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004133
4134
4135/**
4136 * The superclass of object and function templates.
4137 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004138class V8_EXPORT Template : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004139 public:
Ben Murdochc5610432016-08-08 18:44:38 +01004140 /**
4141 * Adds a property to each instance created by this template.
4142 *
4143 * The property must be defined either as a primitive value, or a template.
4144 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004145 void Set(Local<Name> name, Local<Data> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00004146 PropertyAttribute attributes = None);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004147 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004148
4149 void SetAccessorProperty(
4150 Local<Name> name,
4151 Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
4152 Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
4153 PropertyAttribute attribute = None,
4154 AccessControl settings = DEFAULT);
4155
4156 /**
4157 * Whenever the property with the given name is accessed on objects
4158 * created from this Template the getter and setter callbacks
4159 * are called instead of getting and setting the property directly
4160 * on the JavaScript object.
4161 *
4162 * \param name The name of the property for which an accessor is added.
4163 * \param getter The callback to invoke when getting the property.
4164 * \param setter The callback to invoke when setting the property.
4165 * \param data A piece of data that will be passed to the getter and setter
4166 * callbacks whenever they are invoked.
4167 * \param settings Access control settings for the accessor. This is a bit
4168 * field consisting of one of more of
4169 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4170 * The default is to not allow cross-context access.
4171 * ALL_CAN_READ means that all cross-context reads are allowed.
4172 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4173 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4174 * cross-context access.
4175 * \param attribute The attributes of the property for which an accessor
4176 * is added.
4177 * \param signature The signature describes valid receivers for the accessor
4178 * and is used to perform implicit instance checks against them. If the
4179 * receiver is incompatible (i.e. is not an instance of the constructor as
4180 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4181 * thrown and no callback is invoked.
4182 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004183 void SetNativeDataProperty(
4184 Local<String> name, AccessorGetterCallback getter,
4185 AccessorSetterCallback setter = 0,
4186 // TODO(dcarney): gcc can't handle Local below
4187 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4188 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4189 AccessControl settings = DEFAULT);
4190 void SetNativeDataProperty(
4191 Local<Name> name, AccessorNameGetterCallback getter,
4192 AccessorNameSetterCallback setter = 0,
4193 // TODO(dcarney): gcc can't handle Local below
4194 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4195 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4196 AccessControl settings = DEFAULT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004197
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004198 /**
4199 * During template instantiation, sets the value with the intrinsic property
4200 * from the correct context.
4201 */
4202 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
4203 PropertyAttribute attribute = None);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004204
Steve Blocka7e24c12009-10-30 11:49:00 +00004205 private:
4206 Template();
4207
4208 friend class ObjectTemplate;
4209 friend class FunctionTemplate;
4210};
4211
4212
4213/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004214 * NamedProperty[Getter|Setter] are used as interceptors on object.
4215 * See ObjectTemplate::SetNamedPropertyHandler.
4216 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004217typedef void (*NamedPropertyGetterCallback)(
4218 Local<String> property,
4219 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004220
4221
4222/**
4223 * Returns the value if the setter intercepts the request.
4224 * Otherwise, returns an empty handle.
4225 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004226typedef void (*NamedPropertySetterCallback)(
4227 Local<String> property,
4228 Local<Value> value,
4229 const PropertyCallbackInfo<Value>& info);
4230
Steve Blocka7e24c12009-10-30 11:49:00 +00004231
Steve Blocka7e24c12009-10-30 11:49:00 +00004232/**
4233 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004234 * The result is an integer encoding property attributes (like v8::None,
4235 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00004236 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004237typedef void (*NamedPropertyQueryCallback)(
4238 Local<String> property,
4239 const PropertyCallbackInfo<Integer>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004240
4241
4242/**
4243 * Returns a non-empty handle if the deleter intercepts the request.
4244 * The return value is true if the property could be deleted and false
4245 * otherwise.
4246 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004247typedef void (*NamedPropertyDeleterCallback)(
4248 Local<String> property,
4249 const PropertyCallbackInfo<Boolean>& info);
4250
Steve Blocka7e24c12009-10-30 11:49:00 +00004251
4252/**
4253 * Returns an array containing the names of the properties the named
4254 * property getter intercepts.
4255 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004256typedef void (*NamedPropertyEnumeratorCallback)(
4257 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004258
4259
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004260// TODO(dcarney): Deprecate and remove previous typedefs, and replace
4261// GenericNamedPropertyFooCallback with just NamedPropertyFooCallback.
4262/**
4263 * GenericNamedProperty[Getter|Setter] are used as interceptors on object.
4264 * See ObjectTemplate::SetNamedPropertyHandler.
4265 */
4266typedef void (*GenericNamedPropertyGetterCallback)(
4267 Local<Name> property, const PropertyCallbackInfo<Value>& info);
4268
4269
4270/**
4271 * Returns the value if the setter intercepts the request.
4272 * Otherwise, returns an empty handle.
4273 */
4274typedef void (*GenericNamedPropertySetterCallback)(
4275 Local<Name> property, Local<Value> value,
4276 const PropertyCallbackInfo<Value>& info);
4277
4278
4279/**
4280 * Returns a non-empty handle if the interceptor intercepts the request.
4281 * The result is an integer encoding property attributes (like v8::None,
4282 * v8::DontEnum, etc.)
4283 */
4284typedef void (*GenericNamedPropertyQueryCallback)(
4285 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
4286
4287
4288/**
4289 * Returns a non-empty handle if the deleter intercepts the request.
4290 * The return value is true if the property could be deleted and false
4291 * otherwise.
4292 */
4293typedef void (*GenericNamedPropertyDeleterCallback)(
4294 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
4295
4296
4297/**
4298 * Returns an array containing the names of the properties the named
4299 * property getter intercepts.
4300 */
4301typedef void (*GenericNamedPropertyEnumeratorCallback)(
4302 const PropertyCallbackInfo<Array>& info);
4303
4304
Steve Blocka7e24c12009-10-30 11:49:00 +00004305/**
4306 * Returns the value of the property if the getter intercepts the
4307 * request. Otherwise, returns an empty handle.
4308 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004309typedef void (*IndexedPropertyGetterCallback)(
4310 uint32_t index,
4311 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004312
4313
4314/**
4315 * Returns the value if the setter intercepts the request.
4316 * Otherwise, returns an empty handle.
4317 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004318typedef void (*IndexedPropertySetterCallback)(
4319 uint32_t index,
4320 Local<Value> value,
4321 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004322
4323
4324/**
4325 * Returns a non-empty handle if the interceptor intercepts the request.
Iain Merrick75681382010-08-19 15:07:18 +01004326 * The result is an integer encoding property attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004327 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004328typedef void (*IndexedPropertyQueryCallback)(
4329 uint32_t index,
4330 const PropertyCallbackInfo<Integer>& info);
4331
Steve Blocka7e24c12009-10-30 11:49:00 +00004332
4333/**
4334 * Returns a non-empty handle if the deleter intercepts the request.
4335 * The return value is true if the property could be deleted and false
4336 * otherwise.
4337 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004338typedef void (*IndexedPropertyDeleterCallback)(
4339 uint32_t index,
4340 const PropertyCallbackInfo<Boolean>& info);
4341
Steve Blocka7e24c12009-10-30 11:49:00 +00004342
4343/**
4344 * Returns an array containing the indices of the properties the
4345 * indexed property getter intercepts.
4346 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004347typedef void (*IndexedPropertyEnumeratorCallback)(
4348 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004349
4350
4351/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004352 * Access type specification.
4353 */
4354enum AccessType {
4355 ACCESS_GET,
4356 ACCESS_SET,
4357 ACCESS_HAS,
4358 ACCESS_DELETE,
4359 ACCESS_KEYS
4360};
4361
4362
4363/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004364 * Returns true if the given context should be allowed to access the given
4365 * object.
4366 */
4367typedef bool (*AccessCheckCallback)(Local<Context> accessing_context,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004368 Local<Object> accessed_object,
4369 Local<Value> data);
4370typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context,
4371 Local<Object> accessed_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004372
4373/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004374 * Returns true if cross-context access should be allowed to the named
4375 * property with the given key on the host object.
4376 */
4377typedef bool (*NamedSecurityCallback)(Local<Object> host,
4378 Local<Value> key,
4379 AccessType type,
4380 Local<Value> data);
4381
4382
4383/**
4384 * Returns true if cross-context access should be allowed to the indexed
4385 * property with the given index on the host object.
4386 */
4387typedef bool (*IndexedSecurityCallback)(Local<Object> host,
4388 uint32_t index,
4389 AccessType type,
4390 Local<Value> data);
4391
4392
4393/**
4394 * A FunctionTemplate is used to create functions at runtime. There
4395 * can only be one function created from a FunctionTemplate in a
4396 * context. The lifetime of the created function is equal to the
4397 * lifetime of the context. So in case the embedder needs to create
4398 * temporary functions that can be collected using Scripts is
4399 * preferred.
4400 *
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004401 * Any modification of a FunctionTemplate after first instantiation will trigger
4402 *a crash.
4403 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004404 * A FunctionTemplate can have properties, these properties are added to the
4405 * function object when it is created.
4406 *
4407 * A FunctionTemplate has a corresponding instance template which is
4408 * used to create object instances when the function is used as a
4409 * constructor. Properties added to the instance template are added to
4410 * each object instance.
4411 *
4412 * A FunctionTemplate can have a prototype template. The prototype template
4413 * is used to create the prototype object of the function.
4414 *
4415 * The following example shows how to use a FunctionTemplate:
4416 *
4417 * \code
4418 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
4419 * t->Set("func_property", v8::Number::New(1));
4420 *
4421 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
4422 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
4423 * proto_t->Set("proto_const", v8::Number::New(2));
4424 *
4425 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
4426 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
4427 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
4428 * instance_t->Set("instance_property", Number::New(3));
4429 *
4430 * v8::Local<v8::Function> function = t->GetFunction();
4431 * v8::Local<v8::Object> instance = function->NewInstance();
4432 * \endcode
4433 *
4434 * Let's use "function" as the JS variable name of the function object
4435 * and "instance" for the instance object created above. The function
4436 * and the instance will have the following properties:
4437 *
4438 * \code
4439 * func_property in function == true;
4440 * function.func_property == 1;
4441 *
4442 * function.prototype.proto_method() invokes 'InvokeCallback'
4443 * function.prototype.proto_const == 2;
4444 *
4445 * instance instanceof function == true;
4446 * instance.instance_accessor calls 'InstanceAccessorCallback'
4447 * instance.instance_property == 3;
4448 * \endcode
4449 *
4450 * A FunctionTemplate can inherit from another one by calling the
4451 * FunctionTemplate::Inherit method. The following graph illustrates
4452 * the semantics of inheritance:
4453 *
4454 * \code
4455 * FunctionTemplate Parent -> Parent() . prototype -> { }
4456 * ^ ^
4457 * | Inherit(Parent) | .__proto__
4458 * | |
4459 * FunctionTemplate Child -> Child() . prototype -> { }
4460 * \endcode
4461 *
4462 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
4463 * object of the Child() function has __proto__ pointing to the
4464 * Parent() function's prototype object. An instance of the Child
4465 * function has all properties on Parent's instance templates.
4466 *
4467 * Let Parent be the FunctionTemplate initialized in the previous
4468 * section and create a Child FunctionTemplate by:
4469 *
4470 * \code
4471 * Local<FunctionTemplate> parent = t;
4472 * Local<FunctionTemplate> child = FunctionTemplate::New();
4473 * child->Inherit(parent);
4474 *
4475 * Local<Function> child_function = child->GetFunction();
4476 * Local<Object> child_instance = child_function->NewInstance();
4477 * \endcode
4478 *
4479 * The Child function and Child instance will have the following
4480 * properties:
4481 *
4482 * \code
4483 * child_func.prototype.__proto__ == function.prototype;
4484 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
4485 * child_instance.instance_property == 3;
4486 * \endcode
4487 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004488class V8_EXPORT FunctionTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004489 public:
4490 /** Creates a function template.*/
4491 static Local<FunctionTemplate> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004492 Isolate* isolate, FunctionCallback callback = 0,
4493 Local<Value> data = Local<Value>(),
Ben Murdochc5610432016-08-08 18:44:38 +01004494 Local<Signature> signature = Local<Signature>(), int length = 0,
4495 ConstructorBehavior behavior = ConstructorBehavior::kAllow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004496
4497 /**
4498 * Creates a function template with a fast handler. If a fast handler is set,
4499 * the callback cannot be null.
4500 */
4501 static Local<FunctionTemplate> NewWithFastHandler(
4502 Isolate* isolate, FunctionCallback callback,
4503 experimental::FastAccessorBuilder* fast_handler = nullptr,
4504 Local<Value> data = Local<Value>(),
4505 Local<Signature> signature = Local<Signature>(), int length = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004506
Steve Blocka7e24c12009-10-30 11:49:00 +00004507 /** Returns the unique function instance in the current execution context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004508 V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction());
4509 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
4510 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004511
4512 /**
4513 * Set the call-handler callback for a FunctionTemplate. This
4514 * callback is called whenever the function created from this
4515 * FunctionTemplate is called.
4516 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004517 void SetCallHandler(
4518 FunctionCallback callback, Local<Value> data = Local<Value>(),
4519 experimental::FastAccessorBuilder* fast_handler = nullptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00004520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004521 /** Set the predefined length property for the FunctionTemplate. */
4522 void SetLength(int length);
4523
Steve Blocka7e24c12009-10-30 11:49:00 +00004524 /** Get the InstanceTemplate. */
4525 Local<ObjectTemplate> InstanceTemplate();
4526
4527 /** Causes the function template to inherit from a parent function template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004528 void Inherit(Local<FunctionTemplate> parent);
Steve Blocka7e24c12009-10-30 11:49:00 +00004529
4530 /**
4531 * A PrototypeTemplate is the template used to create the prototype object
4532 * of the function created by this template.
4533 */
4534 Local<ObjectTemplate> PrototypeTemplate();
4535
Steve Blocka7e24c12009-10-30 11:49:00 +00004536 /**
4537 * Set the class name of the FunctionTemplate. This is used for
4538 * printing objects created with the function created from the
4539 * FunctionTemplate as its constructor.
4540 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004541 void SetClassName(Local<String> name);
4542
4543
4544 /**
4545 * When set to true, no access check will be performed on the receiver of a
4546 * function call. Currently defaults to true, but this is subject to change.
4547 */
4548 void SetAcceptAnyReceiver(bool value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004549
4550 /**
4551 * Determines whether the __proto__ accessor ignores instances of
4552 * the function template. If instances of the function template are
4553 * ignored, __proto__ skips all instances and instead returns the
4554 * next object in the prototype chain.
4555 *
4556 * Call with a value of true to make the __proto__ accessor ignore
4557 * instances of the function template. Call with a value of false
4558 * to make the __proto__ accessor not ignore instances of the
4559 * function template. By default, instances of a function template
4560 * are not ignored.
4561 */
4562 void SetHiddenPrototype(bool value);
4563
4564 /**
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004565 * Sets the ReadOnly flag in the attributes of the 'prototype' property
4566 * of functions created from this FunctionTemplate to true.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004567 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004568 void ReadOnlyPrototype();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004569
4570 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004571 * Removes the prototype property from functions created from this
4572 * FunctionTemplate.
4573 */
4574 void RemovePrototype();
4575
4576 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00004577 * Returns true if the given object is an instance of this function
4578 * template.
4579 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004580 bool HasInstance(Local<Value> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004581
4582 private:
4583 FunctionTemplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004584 friend class Context;
4585 friend class ObjectTemplate;
4586};
4587
4588
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004589enum class PropertyHandlerFlags {
4590 kNone = 0,
4591 // See ALL_CAN_READ above.
4592 kAllCanRead = 1,
4593 // Will not call into interceptor for properties on the receiver or prototype
4594 // chain. Currently only valid for named interceptors.
4595 kNonMasking = 1 << 1,
4596 // Will not call into interceptor for symbol lookup. Only meaningful for
4597 // named interceptors.
4598 kOnlyInterceptStrings = 1 << 2,
4599};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004600
4601
4602struct NamedPropertyHandlerConfiguration {
4603 NamedPropertyHandlerConfiguration(
4604 /** Note: getter is required **/
4605 GenericNamedPropertyGetterCallback getter = 0,
4606 GenericNamedPropertySetterCallback setter = 0,
4607 GenericNamedPropertyQueryCallback query = 0,
4608 GenericNamedPropertyDeleterCallback deleter = 0,
4609 GenericNamedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004610 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004611 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4612 : getter(getter),
4613 setter(setter),
4614 query(query),
4615 deleter(deleter),
4616 enumerator(enumerator),
4617 data(data),
4618 flags(flags) {}
4619
4620 GenericNamedPropertyGetterCallback getter;
4621 GenericNamedPropertySetterCallback setter;
4622 GenericNamedPropertyQueryCallback query;
4623 GenericNamedPropertyDeleterCallback deleter;
4624 GenericNamedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004625 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004626 PropertyHandlerFlags flags;
4627};
4628
4629
4630struct IndexedPropertyHandlerConfiguration {
4631 IndexedPropertyHandlerConfiguration(
4632 /** Note: getter is required **/
4633 IndexedPropertyGetterCallback getter = 0,
4634 IndexedPropertySetterCallback setter = 0,
4635 IndexedPropertyQueryCallback query = 0,
4636 IndexedPropertyDeleterCallback deleter = 0,
4637 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004638 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004639 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4640 : getter(getter),
4641 setter(setter),
4642 query(query),
4643 deleter(deleter),
4644 enumerator(enumerator),
4645 data(data),
4646 flags(flags) {}
4647
4648 IndexedPropertyGetterCallback getter;
4649 IndexedPropertySetterCallback setter;
4650 IndexedPropertyQueryCallback query;
4651 IndexedPropertyDeleterCallback deleter;
4652 IndexedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004653 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004654 PropertyHandlerFlags flags;
4655};
4656
4657
Steve Blocka7e24c12009-10-30 11:49:00 +00004658/**
4659 * An ObjectTemplate is used to create objects at runtime.
4660 *
4661 * Properties added to an ObjectTemplate are added to each object
4662 * created from the ObjectTemplate.
4663 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004664class V8_EXPORT ObjectTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004665 public:
4666 /** Creates an ObjectTemplate. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004667 static Local<ObjectTemplate> New(
4668 Isolate* isolate,
4669 Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
4670 static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New());
Steve Blocka7e24c12009-10-30 11:49:00 +00004671
4672 /** Creates a new instance of this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004673 V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance());
4674 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004675
4676 /**
4677 * Sets an accessor on the object template.
4678 *
4679 * Whenever the property with the given name is accessed on objects
4680 * created from this ObjectTemplate the getter and setter callbacks
4681 * are called instead of getting and setting the property directly
4682 * on the JavaScript object.
4683 *
4684 * \param name The name of the property for which an accessor is added.
4685 * \param getter The callback to invoke when getting the property.
4686 * \param setter The callback to invoke when setting the property.
4687 * \param data A piece of data that will be passed to the getter and setter
4688 * callbacks whenever they are invoked.
4689 * \param settings Access control settings for the accessor. This is a bit
4690 * field consisting of one of more of
4691 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4692 * The default is to not allow cross-context access.
4693 * ALL_CAN_READ means that all cross-context reads are allowed.
4694 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4695 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4696 * cross-context access.
4697 * \param attribute The attributes of the property for which an accessor
4698 * is added.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004699 * \param signature The signature describes valid receivers for the accessor
4700 * and is used to perform implicit instance checks against them. If the
4701 * receiver is incompatible (i.e. is not an instance of the constructor as
4702 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4703 * thrown and no callback is invoked.
Steve Blocka7e24c12009-10-30 11:49:00 +00004704 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004705 void SetAccessor(
4706 Local<String> name, AccessorGetterCallback getter,
4707 AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4708 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4709 Local<AccessorSignature> signature = Local<AccessorSignature>());
4710 void SetAccessor(
4711 Local<Name> name, AccessorNameGetterCallback getter,
4712 AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4713 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4714 Local<AccessorSignature> signature = Local<AccessorSignature>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004715
4716 /**
4717 * Sets a named property handler on the object template.
4718 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004719 * Whenever a property whose name is a string is accessed on objects created
4720 * from this object template, the provided callback is invoked instead of
Steve Blocka7e24c12009-10-30 11:49:00 +00004721 * accessing the property directly on the JavaScript object.
4722 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004723 * Note that new code should use the second version that can intercept
4724 * symbol-named properties as well as string-named properties.
4725 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004726 * \param getter The callback to invoke when getting a property.
4727 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004728 * \param query The callback to invoke to check if a property is present,
4729 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004730 * \param deleter The callback to invoke when deleting a property.
4731 * \param enumerator The callback to invoke to enumerate all the named
4732 * properties of an object.
4733 * \param data A piece of data that will be passed to the callbacks
4734 * whenever they are invoked.
4735 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004736 // TODO(dcarney): deprecate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004737 void SetNamedPropertyHandler(NamedPropertyGetterCallback getter,
4738 NamedPropertySetterCallback setter = 0,
4739 NamedPropertyQueryCallback query = 0,
4740 NamedPropertyDeleterCallback deleter = 0,
4741 NamedPropertyEnumeratorCallback enumerator = 0,
4742 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004743 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
Steve Blocka7e24c12009-10-30 11:49:00 +00004744
4745 /**
4746 * Sets an indexed property handler on the object template.
4747 *
4748 * Whenever an indexed property is accessed on objects created from
4749 * this object template, the provided callback is invoked instead of
4750 * accessing the property directly on the JavaScript object.
4751 *
4752 * \param getter The callback to invoke when getting a property.
4753 * \param setter The callback to invoke when setting a property.
Ben Murdoch257744e2011-11-30 15:57:28 +00004754 * \param query The callback to invoke to check if an object has a property.
Steve Blocka7e24c12009-10-30 11:49:00 +00004755 * \param deleter The callback to invoke when deleting a property.
4756 * \param enumerator The callback to invoke to enumerate all the indexed
4757 * properties of an object.
4758 * \param data A piece of data that will be passed to the callbacks
4759 * whenever they are invoked.
4760 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004761 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
4762 // TODO(dcarney): deprecate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004763 void SetIndexedPropertyHandler(
4764 IndexedPropertyGetterCallback getter,
4765 IndexedPropertySetterCallback setter = 0,
4766 IndexedPropertyQueryCallback query = 0,
4767 IndexedPropertyDeleterCallback deleter = 0,
4768 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004769 Local<Value> data = Local<Value>()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004770 SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
4771 deleter, enumerator, data));
4772 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004773 /**
4774 * Sets the callback to be used when calling instances created from
4775 * this template as a function. If no callback is set, instances
4776 * behave like normal JavaScript objects that cannot be called as a
4777 * function.
4778 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004779 void SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004780 Local<Value> data = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004781
4782 /**
4783 * Mark object instances of the template as undetectable.
4784 *
4785 * In many ways, undetectable objects behave as though they are not
4786 * there. They behave like 'undefined' in conditionals and when
4787 * printed. However, properties can be accessed and called as on
4788 * normal objects.
4789 */
4790 void MarkAsUndetectable();
4791
4792 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004793 * Sets access check callback on the object template and enables access
4794 * checks.
Steve Blocka7e24c12009-10-30 11:49:00 +00004795 *
4796 * When accessing properties on instances of this object template,
4797 * the access check callback will be called to determine whether or
4798 * not to allow cross-context access to the properties.
Steve Blocka7e24c12009-10-30 11:49:00 +00004799 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004800 void SetAccessCheckCallback(AccessCheckCallback callback,
4801 Local<Value> data = Local<Value>());
Ben Murdoch097c5b22016-05-18 11:27:45 +01004802 V8_DEPRECATED(
4803 "Use SetAccessCheckCallback with new AccessCheckCallback signature.",
4804 void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback,
4805 Local<Value> data = Local<Value>()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004806
4807 V8_DEPRECATED(
4808 "Use SetAccessCheckCallback instead",
4809 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
4810 IndexedSecurityCallback indexed_handler,
4811 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00004812
4813 /**
4814 * Gets the number of internal fields for objects generated from
4815 * this template.
4816 */
4817 int InternalFieldCount();
4818
4819 /**
4820 * Sets the number of internal fields for objects generated from
4821 * this template.
4822 */
4823 void SetInternalFieldCount(int value);
4824
4825 private:
4826 ObjectTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004827 static Local<ObjectTemplate> New(internal::Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004828 Local<FunctionTemplate> constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +00004829 friend class FunctionTemplate;
4830};
4831
4832
4833/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004834 * A Signature specifies which receiver is valid for a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004835 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004836class V8_EXPORT Signature : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004837 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004838 static Local<Signature> New(
4839 Isolate* isolate,
4840 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004841
Steve Blocka7e24c12009-10-30 11:49:00 +00004842 private:
4843 Signature();
4844};
4845
4846
4847/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004848 * An AccessorSignature specifies which receivers are valid parameters
4849 * to an accessor callback.
4850 */
4851class V8_EXPORT AccessorSignature : public Data {
4852 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004853 static Local<AccessorSignature> New(
4854 Isolate* isolate,
4855 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004856
4857 private:
4858 AccessorSignature();
4859};
4860
4861
Ben Murdoch257744e2011-11-30 15:57:28 +00004862// --- Extensions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004863
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004864class V8_EXPORT ExternalOneByteStringResourceImpl
4865 : public String::ExternalOneByteStringResource {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004866 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004867 ExternalOneByteStringResourceImpl() : data_(0), length_(0) {}
4868 ExternalOneByteStringResourceImpl(const char* data, size_t length)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004869 : data_(data), length_(length) {}
4870 const char* data() const { return data_; }
4871 size_t length() const { return length_; }
4872
4873 private:
4874 const char* data_;
4875 size_t length_;
4876};
Steve Blocka7e24c12009-10-30 11:49:00 +00004877
4878/**
4879 * Ignore
4880 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004881class V8_EXPORT Extension { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00004882 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004883 // Note that the strings passed into this constructor must live as long
4884 // as the Extension itself.
Steve Blocka7e24c12009-10-30 11:49:00 +00004885 Extension(const char* name,
4886 const char* source = 0,
4887 int dep_count = 0,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004888 const char** deps = 0,
4889 int source_length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00004890 virtual ~Extension() { }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004891 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
4892 v8::Isolate* isolate, v8::Local<v8::String> name) {
4893 return v8::Local<v8::FunctionTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004894 }
4895
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004896 const char* name() const { return name_; }
4897 size_t source_length() const { return source_length_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004898 const String::ExternalOneByteStringResource* source() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004899 return &source_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004900 int dependency_count() { return dep_count_; }
4901 const char** dependencies() { return deps_; }
4902 void set_auto_enable(bool value) { auto_enable_ = value; }
4903 bool auto_enable() { return auto_enable_; }
4904
4905 private:
4906 const char* name_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004907 size_t source_length_; // expected to initialize before source_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004908 ExternalOneByteStringResourceImpl source_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004909 int dep_count_;
4910 const char** deps_;
4911 bool auto_enable_;
4912
4913 // Disallow copying and assigning.
4914 Extension(const Extension&);
4915 void operator=(const Extension&);
4916};
4917
4918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004919void V8_EXPORT RegisterExtension(Extension* extension);
Steve Blocka7e24c12009-10-30 11:49:00 +00004920
4921
Ben Murdoch257744e2011-11-30 15:57:28 +00004922// --- Statics ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004924V8_INLINE Local<Primitive> Undefined(Isolate* isolate);
4925V8_INLINE Local<Primitive> Null(Isolate* isolate);
4926V8_INLINE Local<Boolean> True(Isolate* isolate);
4927V8_INLINE Local<Boolean> False(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004928
Steve Blocka7e24c12009-10-30 11:49:00 +00004929/**
4930 * A set of constraints that specifies the limits of the runtime's memory use.
4931 * You must set the heap size before initializing the VM - the size cannot be
4932 * adjusted after the VM is initialized.
4933 *
4934 * If you are using threads then you should hold the V8::Locker lock while
4935 * setting the stack limit and you must set a non-default stack limit separately
4936 * for each thread.
Ben Murdochda12d292016-06-02 14:46:10 +01004937 *
4938 * The arguments for set_max_semi_space_size, set_max_old_space_size,
4939 * set_max_executable_size, set_code_range_size specify limits in MB.
Steve Blocka7e24c12009-10-30 11:49:00 +00004940 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004941class V8_EXPORT ResourceConstraints {
Steve Blocka7e24c12009-10-30 11:49:00 +00004942 public:
4943 ResourceConstraints();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004944
4945 /**
4946 * Configures the constraints with reasonable default values based on the
4947 * capabilities of the current device the VM is running on.
4948 *
4949 * \param physical_memory The total amount of physical memory on the current
4950 * device, in bytes.
4951 * \param virtual_memory_limit The amount of virtual memory on the current
4952 * device, in bytes, or zero, if there is no limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004953 */
4954 void ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004955 uint64_t virtual_memory_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004956
4957 int max_semi_space_size() const { return max_semi_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004958 void set_max_semi_space_size(int limit_in_mb) {
4959 max_semi_space_size_ = limit_in_mb;
4960 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004961 int max_old_space_size() const { return max_old_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004962 void set_max_old_space_size(int limit_in_mb) {
4963 max_old_space_size_ = limit_in_mb;
4964 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004965 int max_executable_size() const { return max_executable_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004966 void set_max_executable_size(int limit_in_mb) {
4967 max_executable_size_ = limit_in_mb;
4968 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004969 uint32_t* stack_limit() const { return stack_limit_; }
4970 // Sets an address beyond which the VM's stack may not grow.
4971 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004972 size_t code_range_size() const { return code_range_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004973 void set_code_range_size(size_t limit_in_mb) {
4974 code_range_size_ = limit_in_mb;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004975 }
4976
Steve Blocka7e24c12009-10-30 11:49:00 +00004977 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004978 int max_semi_space_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004979 int max_old_space_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08004980 int max_executable_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004981 uint32_t* stack_limit_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004982 size_t code_range_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004983};
4984
4985
Ben Murdoch257744e2011-11-30 15:57:28 +00004986// --- Exceptions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004987
4988
4989typedef void (*FatalErrorCallback)(const char* location, const char* message);
4990
4991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004992typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
Steve Blocka7e24c12009-10-30 11:49:00 +00004993
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004994// --- Tracing ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004995
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004996typedef void (*LogEventCallback)(const char* name, int event);
Steve Blocka7e24c12009-10-30 11:49:00 +00004997
4998/**
4999 * Create new error objects by calling the corresponding error object
5000 * constructor with the message.
5001 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005002class V8_EXPORT Exception {
Steve Blocka7e24c12009-10-30 11:49:00 +00005003 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005004 static Local<Value> RangeError(Local<String> message);
5005 static Local<Value> ReferenceError(Local<String> message);
5006 static Local<Value> SyntaxError(Local<String> message);
5007 static Local<Value> TypeError(Local<String> message);
5008 static Local<Value> Error(Local<String> message);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005009
5010 /**
5011 * Creates an error message for the given exception.
5012 * Will try to reconstruct the original stack trace from the exception value,
5013 * or capture the current stack trace if not available.
5014 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005015 static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
5016 V8_DEPRECATED("Use version with an Isolate*",
5017 static Local<Message> CreateMessage(Local<Value> exception));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005018
5019 /**
5020 * Returns the original stack trace that was captured at the creation time
5021 * of a given exception, or an empty handle if not available.
5022 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005023 static Local<StackTrace> GetStackTrace(Local<Value> exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00005024};
5025
5026
Ben Murdoch257744e2011-11-30 15:57:28 +00005027// --- Counters Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005028
5029typedef int* (*CounterLookupCallback)(const char* name);
5030
5031typedef void* (*CreateHistogramCallback)(const char* name,
5032 int min,
5033 int max,
5034 size_t buckets);
5035
5036typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
5037
Ben Murdoch257744e2011-11-30 15:57:28 +00005038// --- Memory Allocation Callback ---
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005039enum ObjectSpace {
5040 kObjectSpaceNewSpace = 1 << 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005041 kObjectSpaceOldSpace = 1 << 1,
5042 kObjectSpaceCodeSpace = 1 << 2,
5043 kObjectSpaceMapSpace = 1 << 3,
5044 kObjectSpaceLoSpace = 1 << 4,
5045 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldSpace |
5046 kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
5047 kObjectSpaceLoSpace
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005048};
Iain Merrick9ac36c92010-09-13 15:29:50 +01005049
5050 enum AllocationAction {
5051 kAllocationActionAllocate = 1 << 0,
5052 kAllocationActionFree = 1 << 1,
5053 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
5054 };
5055
5056typedef void (*MemoryAllocationCallback)(ObjectSpace space,
5057 AllocationAction action,
5058 int size);
5059
Ben Murdoch097c5b22016-05-18 11:27:45 +01005060// --- Enter/Leave Script Callback ---
5061typedef void (*BeforeCallEnteredCallback)(Isolate*);
5062typedef void (*CallCompletedCallback)(Isolate*);
5063typedef void (*DeprecatedCallCompletedCallback)();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005064
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005065// --- Promise Reject Callback ---
5066enum PromiseRejectEvent {
5067 kPromiseRejectWithNoHandler = 0,
5068 kPromiseHandlerAddedAfterReject = 1
5069};
5070
5071class PromiseRejectMessage {
5072 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005073 PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event,
5074 Local<Value> value, Local<StackTrace> stack_trace)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005075 : promise_(promise),
5076 event_(event),
5077 value_(value),
5078 stack_trace_(stack_trace) {}
5079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005080 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005081 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005082 V8_INLINE Local<Value> GetValue() const { return value_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005084 V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()",
5085 V8_INLINE Local<StackTrace> GetStackTrace() const) {
5086 return stack_trace_;
5087 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005088
5089 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005090 Local<Promise> promise_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005091 PromiseRejectEvent event_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005092 Local<Value> value_;
5093 Local<StackTrace> stack_trace_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005094};
5095
5096typedef void (*PromiseRejectCallback)(PromiseRejectMessage message);
5097
Ben Murdochda12d292016-06-02 14:46:10 +01005098// --- Microtasks Callbacks ---
5099typedef void (*MicrotasksCompletedCallback)(Isolate*);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005100typedef void (*MicrotaskCallback)(void* data);
5101
Ben Murdochda12d292016-06-02 14:46:10 +01005102
5103/**
5104 * Policy for running microtasks:
5105 * - explicit: microtasks are invoked with Isolate::RunMicrotasks() method;
5106 * - scoped: microtasks invocation is controlled by MicrotasksScope objects;
5107 * - auto: microtasks are invoked when the script call depth decrements
5108 * to zero.
5109 */
5110enum class MicrotasksPolicy { kExplicit, kScoped, kAuto };
5111
5112
5113/**
5114 * This scope is used to control microtasks when kScopeMicrotasksInvocation
5115 * is used on Isolate. In this mode every non-primitive call to V8 should be
5116 * done inside some MicrotasksScope.
5117 * Microtasks are executed when topmost MicrotasksScope marked as kRunMicrotasks
5118 * exits.
5119 * kDoNotRunMicrotasks should be used to annotate calls not intended to trigger
5120 * microtasks.
5121 */
5122class V8_EXPORT MicrotasksScope {
5123 public:
5124 enum Type { kRunMicrotasks, kDoNotRunMicrotasks };
5125
5126 MicrotasksScope(Isolate* isolate, Type type);
5127 ~MicrotasksScope();
5128
5129 /**
5130 * Runs microtasks if no kRunMicrotasks scope is currently active.
5131 */
5132 static void PerformCheckpoint(Isolate* isolate);
5133
5134 /**
5135 * Returns current depth of nested kRunMicrotasks scopes.
5136 */
5137 static int GetCurrentDepth(Isolate* isolate);
5138
Ben Murdochc5610432016-08-08 18:44:38 +01005139 /**
5140 * Returns true while microtasks are being executed.
5141 */
5142 static bool IsRunningMicrotasks(Isolate* isolate);
5143
Ben Murdochda12d292016-06-02 14:46:10 +01005144 private:
5145 internal::Isolate* const isolate_;
5146 bool run_;
5147
5148 // Prevent copying.
5149 MicrotasksScope(const MicrotasksScope&);
5150 MicrotasksScope& operator=(const MicrotasksScope&);
5151};
5152
5153
Ben Murdoch257744e2011-11-30 15:57:28 +00005154// --- Failed Access Check Callback ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005155typedef void (*FailedAccessCheckCallback)(Local<Object> target,
5156 AccessType type,
5157 Local<Value> data);
5158
Ben Murdoch257744e2011-11-30 15:57:28 +00005159// --- AllowCodeGenerationFromStrings callbacks ---
5160
5161/**
5162 * Callback to check if code generation from strings is allowed. See
5163 * Context::AllowCodeGenerationFromStrings.
5164 */
5165typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
5166
5167// --- Garbage Collection Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005168
5169/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005170 * Applications can register callback functions which will be called before and
5171 * after certain garbage collection operations. Allocations are not allowed in
5172 * the callback functions, you therefore cannot manipulate objects (set or
5173 * delete properties for example) since it is possible such operations will
5174 * result in the allocation of objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00005175 */
Steve Block6ded16b2010-05-10 14:33:55 +01005176enum GCType {
5177 kGCTypeScavenge = 1 << 0,
5178 kGCTypeMarkSweepCompact = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005179 kGCTypeIncrementalMarking = 1 << 2,
5180 kGCTypeProcessWeakCallbacks = 1 << 3,
5181 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact |
5182 kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks
Steve Block6ded16b2010-05-10 14:33:55 +01005183};
5184
Ben Murdoch097c5b22016-05-18 11:27:45 +01005185/**
5186 * GCCallbackFlags is used to notify additional information about the GC
5187 * callback.
5188 * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
5189 * constructing retained object infos.
5190 * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
5191 * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
5192 * is called synchronously without getting posted to an idle task.
5193 * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
5194 * in a phase where V8 is trying to collect all available garbage
5195 * (e.g., handling a low memory notification).
5196 */
Steve Block6ded16b2010-05-10 14:33:55 +01005197enum GCCallbackFlags {
5198 kNoGCCallbackFlags = 0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005199 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005200 kGCCallbackFlagForced = 1 << 2,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005201 kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
5202 kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
Steve Block6ded16b2010-05-10 14:33:55 +01005203};
5204
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005205typedef void (*GCCallback)(GCType type, GCCallbackFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01005206
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005207typedef void (*InterruptCallback)(Isolate* isolate, void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00005208
5209
Steve Blocka7e24c12009-10-30 11:49:00 +00005210/**
Steve Block3ce2e202009-11-05 08:53:23 +00005211 * Collection of V8 heap information.
5212 *
5213 * Instances of this class can be passed to v8::V8::HeapStatistics to
5214 * get heap statistics from V8.
5215 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005216class V8_EXPORT HeapStatistics {
Steve Block3ce2e202009-11-05 08:53:23 +00005217 public:
5218 HeapStatistics();
5219 size_t total_heap_size() { return total_heap_size_; }
Russell Brenner90bac252010-11-18 13:33:46 -08005220 size_t total_heap_size_executable() { return total_heap_size_executable_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005221 size_t total_physical_size() { return total_physical_size_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005222 size_t total_available_size() { return total_available_size_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005223 size_t used_heap_size() { return used_heap_size_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005224 size_t heap_size_limit() { return heap_size_limit_; }
Ben Murdochda12d292016-06-02 14:46:10 +01005225 size_t malloced_memory() { return malloced_memory_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005226 size_t does_zap_garbage() { return does_zap_garbage_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005227
5228 private:
Steve Block3ce2e202009-11-05 08:53:23 +00005229 size_t total_heap_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08005230 size_t total_heap_size_executable_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005231 size_t total_physical_size_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005232 size_t total_available_size_;
Steve Block3ce2e202009-11-05 08:53:23 +00005233 size_t used_heap_size_;
Ben Murdochb8e0da22011-05-16 14:20:40 +01005234 size_t heap_size_limit_;
Ben Murdochda12d292016-06-02 14:46:10 +01005235 size_t malloced_memory_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005236 bool does_zap_garbage_;
Steve Block3ce2e202009-11-05 08:53:23 +00005237
5238 friend class V8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005239 friend class Isolate;
Steve Block3ce2e202009-11-05 08:53:23 +00005240};
5241
5242
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005243class V8_EXPORT HeapSpaceStatistics {
5244 public:
5245 HeapSpaceStatistics();
5246 const char* space_name() { return space_name_; }
5247 size_t space_size() { return space_size_; }
5248 size_t space_used_size() { return space_used_size_; }
5249 size_t space_available_size() { return space_available_size_; }
5250 size_t physical_space_size() { return physical_space_size_; }
5251
5252 private:
5253 const char* space_name_;
5254 size_t space_size_;
5255 size_t space_used_size_;
5256 size_t space_available_size_;
5257 size_t physical_space_size_;
5258
5259 friend class Isolate;
5260};
5261
5262
5263class V8_EXPORT HeapObjectStatistics {
5264 public:
5265 HeapObjectStatistics();
5266 const char* object_type() { return object_type_; }
5267 const char* object_sub_type() { return object_sub_type_; }
5268 size_t object_count() { return object_count_; }
5269 size_t object_size() { return object_size_; }
5270
5271 private:
5272 const char* object_type_;
5273 const char* object_sub_type_;
5274 size_t object_count_;
5275 size_t object_size_;
5276
5277 friend class Isolate;
5278};
5279
5280
Steve Block44f0eee2011-05-26 01:26:41 +01005281class RetainedObjectInfo;
5282
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005283
Steve Block44f0eee2011-05-26 01:26:41 +01005284/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005285 * FunctionEntryHook is the type of the profile entry hook called at entry to
5286 * any generated function when function-level profiling is enabled.
5287 *
5288 * \param function the address of the function that's being entered.
5289 * \param return_addr_location points to a location on stack where the machine
5290 * return address resides. This can be used to identify the caller of
5291 * \p function, and/or modified to divert execution when \p function exits.
5292 *
5293 * \note the entry hook must not cause garbage collection.
Steve Block44f0eee2011-05-26 01:26:41 +01005294 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005295typedef void (*FunctionEntryHook)(uintptr_t function,
5296 uintptr_t return_addr_location);
5297
5298/**
5299 * A JIT code event is issued each time code is added, moved or removed.
5300 *
5301 * \note removal events are not currently issued.
5302 */
5303struct JitCodeEvent {
5304 enum EventType {
5305 CODE_ADDED,
5306 CODE_MOVED,
5307 CODE_REMOVED,
5308 CODE_ADD_LINE_POS_INFO,
5309 CODE_START_LINE_INFO_RECORDING,
5310 CODE_END_LINE_INFO_RECORDING
5311 };
5312 // Definition of the code position type. The "POSITION" type means the place
5313 // in the source code which are of interest when making stack traces to
5314 // pin-point the source location of a stack frame as close as possible.
5315 // The "STATEMENT_POSITION" means the place at the beginning of each
5316 // statement, and is used to indicate possible break locations.
5317 enum PositionType { POSITION, STATEMENT_POSITION };
5318
5319 // Type of event.
5320 EventType type;
5321 // Start of the instructions.
5322 void* code_start;
5323 // Size of the instructions.
5324 size_t code_len;
5325 // Script info for CODE_ADDED event.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005326 Local<UnboundScript> script;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005327 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
5328 // code line information which is returned from the
5329 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
5330 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
5331 void* user_data;
5332
5333 struct name_t {
5334 // Name of the object associated with the code, note that the string is not
5335 // zero-terminated.
5336 const char* str;
5337 // Number of chars in str.
5338 size_t len;
5339 };
5340
5341 struct line_info_t {
5342 // PC offset
5343 size_t offset;
5344 // Code postion
5345 size_t pos;
5346 // The position type.
5347 PositionType position_type;
5348 };
5349
5350 union {
5351 // Only valid for CODE_ADDED.
5352 struct name_t name;
5353
5354 // Only valid for CODE_ADD_LINE_POS_INFO
5355 struct line_info_t line_info;
5356
5357 // New location of instructions. Only valid for CODE_MOVED.
5358 void* new_code_start;
5359 };
5360};
5361
5362/**
5363 * Option flags passed to the SetJitCodeEventHandler function.
5364 */
5365enum JitCodeEventOptions {
5366 kJitCodeEventDefault = 0,
5367 // Generate callbacks for already existent code.
5368 kJitCodeEventEnumExisting = 1
5369};
5370
5371
5372/**
5373 * Callback function passed to SetJitCodeEventHandler.
5374 *
5375 * \param event code add, move or removal event.
5376 */
5377typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
5378
5379
5380/**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005381 * Interface for iterating through all external resources in the heap.
5382 */
5383class V8_EXPORT ExternalResourceVisitor { // NOLINT
5384 public:
5385 virtual ~ExternalResourceVisitor() {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005386 virtual void VisitExternalString(Local<String> string) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005387};
5388
5389
5390/**
5391 * Interface for iterating through all the persistent handles in the heap.
5392 */
5393class V8_EXPORT PersistentHandleVisitor { // NOLINT
5394 public:
5395 virtual ~PersistentHandleVisitor() {}
5396 virtual void VisitPersistentHandle(Persistent<Value>* value,
5397 uint16_t class_id) {}
5398};
5399
Ben Murdochda12d292016-06-02 14:46:10 +01005400/**
5401 * Memory pressure level for the MemoryPressureNotification.
5402 * kNone hints V8 that there is no memory pressure.
5403 * kModerate hints V8 to speed up incremental garbage collection at the cost of
5404 * of higher latency due to garbage collection pauses.
5405 * kCritical hints V8 to free memory as soon as possible. Garbage collection
5406 * pauses at this level will be large.
5407 */
5408enum class MemoryPressureLevel { kNone, kModerate, kCritical };
5409
5410/**
5411 * Interface for tracing through the embedder heap. During the v8 garbage
5412 * collection, v8 collects hidden fields of all potential wrappers, and at the
5413 * end of its marking phase iterates the collection and asks the embedder to
5414 * trace through its heap and call PersistentBase::RegisterExternalReference on
5415 * each js object reachable from any of the given wrappers.
5416 *
Ben Murdochc5610432016-08-08 18:44:38 +01005417 * Before the first call to the TraceWrappersFrom function TracePrologue will be
5418 * called. When the garbage collection cycle is finished, TraceEpilogue will be
5419 * called.
Ben Murdochda12d292016-06-02 14:46:10 +01005420 */
Ben Murdochc5610432016-08-08 18:44:38 +01005421class V8_EXPORT EmbedderHeapTracer {
Ben Murdochda12d292016-06-02 14:46:10 +01005422 public:
5423 /**
5424 * V8 will call this method at the beginning of the gc cycle.
5425 */
Ben Murdochc5610432016-08-08 18:44:38 +01005426 virtual void TracePrologue() = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005427 /**
5428 * V8 will call this method with internal fields of a potential wrappers.
5429 * Embedder is expected to trace its heap (synchronously) and call
5430 * PersistentBase::RegisterExternalReference() on all wrappers reachable from
5431 * any of the given wrappers.
5432 */
Ben Murdochc5610432016-08-08 18:44:38 +01005433 virtual void TraceWrappersFrom(
Ben Murdochda12d292016-06-02 14:46:10 +01005434 const std::vector<std::pair<void*, void*> >& internal_fields) = 0;
5435 /**
5436 * V8 will call this method at the end of the gc cycle. Allocation is *not*
Ben Murdochc5610432016-08-08 18:44:38 +01005437 * allowed in the TraceEpilogue.
Ben Murdochda12d292016-06-02 14:46:10 +01005438 */
Ben Murdochc5610432016-08-08 18:44:38 +01005439 virtual void TraceEpilogue() = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005440
5441 protected:
5442 virtual ~EmbedderHeapTracer() = default;
5443};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005444
5445/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005446 * Isolate represents an isolated instance of the V8 engine. V8 isolates have
5447 * completely separate states. Objects from one isolate must not be used in
5448 * other isolates. The embedder can create multiple isolates and use them in
5449 * parallel in multiple threads. An isolate can be entered by at most one
5450 * thread at any given time. The Locker/Unlocker API must be used to
5451 * synchronize.
5452 */
5453class V8_EXPORT Isolate {
Steve Block44f0eee2011-05-26 01:26:41 +01005454 public:
5455 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005456 * Initial configuration parameters for a new Isolate.
5457 */
5458 struct CreateParams {
5459 CreateParams()
5460 : entry_hook(NULL),
5461 code_event_handler(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005462 snapshot_blob(NULL),
5463 counter_lookup_callback(NULL),
5464 create_histogram_callback(NULL),
5465 add_histogram_sample_callback(NULL),
5466 array_buffer_allocator(NULL) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005467
5468 /**
5469 * The optional entry_hook allows the host application to provide the
5470 * address of a function that's invoked on entry to every V8-generated
5471 * function. Note that entry_hook is invoked at the very start of each
5472 * generated function. Furthermore, if an entry_hook is given, V8 will
5473 * always run without a context snapshot.
5474 */
5475 FunctionEntryHook entry_hook;
5476
5477 /**
5478 * Allows the host application to provide the address of a function that is
5479 * notified each time code is added, moved or removed.
5480 */
5481 JitCodeEventHandler code_event_handler;
5482
5483 /**
5484 * ResourceConstraints to use for the new Isolate.
5485 */
5486 ResourceConstraints constraints;
5487
5488 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005489 * Explicitly specify a startup snapshot blob. The embedder owns the blob.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005490 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005491 StartupData* snapshot_blob;
5492
5493
5494 /**
5495 * Enables the host application to provide a mechanism for recording
5496 * statistics counters.
5497 */
5498 CounterLookupCallback counter_lookup_callback;
5499
5500 /**
5501 * Enables the host application to provide a mechanism for recording
5502 * histograms. The CreateHistogram function returns a
5503 * histogram which will later be passed to the AddHistogramSample
5504 * function.
5505 */
5506 CreateHistogramCallback create_histogram_callback;
5507 AddHistogramSampleCallback add_histogram_sample_callback;
5508
5509 /**
5510 * The ArrayBuffer::Allocator to use for allocating and freeing the backing
5511 * store of ArrayBuffers.
5512 */
5513 ArrayBuffer::Allocator* array_buffer_allocator;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005514 };
5515
5516
5517 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005518 * Stack-allocated class which sets the isolate for all operations
5519 * executed within a local scope.
5520 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005521 class V8_EXPORT Scope {
Steve Block44f0eee2011-05-26 01:26:41 +01005522 public:
5523 explicit Scope(Isolate* isolate) : isolate_(isolate) {
5524 isolate->Enter();
5525 }
5526
5527 ~Scope() { isolate_->Exit(); }
5528
5529 private:
5530 Isolate* const isolate_;
5531
5532 // Prevent copying of Scope objects.
5533 Scope(const Scope&);
5534 Scope& operator=(const Scope&);
5535 };
5536
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005537
5538 /**
5539 * Assert that no Javascript code is invoked.
5540 */
5541 class V8_EXPORT DisallowJavascriptExecutionScope {
5542 public:
5543 enum OnFailure { CRASH_ON_FAILURE, THROW_ON_FAILURE };
5544
5545 DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure);
5546 ~DisallowJavascriptExecutionScope();
5547
5548 private:
5549 bool on_failure_;
5550 void* internal_;
5551
5552 // Prevent copying of Scope objects.
5553 DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&);
5554 DisallowJavascriptExecutionScope& operator=(
5555 const DisallowJavascriptExecutionScope&);
5556 };
5557
5558
5559 /**
5560 * Introduce exception to DisallowJavascriptExecutionScope.
5561 */
5562 class V8_EXPORT AllowJavascriptExecutionScope {
5563 public:
5564 explicit AllowJavascriptExecutionScope(Isolate* isolate);
5565 ~AllowJavascriptExecutionScope();
5566
5567 private:
5568 void* internal_throws_;
5569 void* internal_assert_;
5570
5571 // Prevent copying of Scope objects.
5572 AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&);
5573 AllowJavascriptExecutionScope& operator=(
5574 const AllowJavascriptExecutionScope&);
5575 };
5576
5577 /**
5578 * Do not run microtasks while this scope is active, even if microtasks are
5579 * automatically executed otherwise.
5580 */
5581 class V8_EXPORT SuppressMicrotaskExecutionScope {
5582 public:
5583 explicit SuppressMicrotaskExecutionScope(Isolate* isolate);
5584 ~SuppressMicrotaskExecutionScope();
5585
5586 private:
5587 internal::Isolate* isolate_;
5588
5589 // Prevent copying of Scope objects.
5590 SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
5591 SuppressMicrotaskExecutionScope& operator=(
5592 const SuppressMicrotaskExecutionScope&);
5593 };
5594
5595 /**
5596 * Types of garbage collections that can be requested via
5597 * RequestGarbageCollectionForTesting.
5598 */
5599 enum GarbageCollectionType {
5600 kFullGarbageCollection,
5601 kMinorGarbageCollection
5602 };
5603
5604 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005605 * Features reported via the SetUseCounterCallback callback. Do not change
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005606 * assigned numbers of existing items; add new features to the end of this
5607 * list.
5608 */
5609 enum UseCounterFeature {
5610 kUseAsm = 0,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005611 kBreakIterator = 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005612 kLegacyConst = 2,
5613 kMarkDequeOverflow = 3,
5614 kStoreBufferOverflow = 4,
5615 kSlotsBufferOverflow = 5,
5616 kObjectObserve = 6,
5617 kForcedGC = 7,
5618 kSloppyMode = 8,
5619 kStrictMode = 9,
5620 kStrongMode = 10,
5621 kRegExpPrototypeStickyGetter = 11,
5622 kRegExpPrototypeToString = 12,
5623 kRegExpPrototypeUnicodeGetter = 13,
5624 kIntlV8Parse = 14,
5625 kIntlPattern = 15,
5626 kIntlResolved = 16,
5627 kPromiseChain = 17,
5628 kPromiseAccept = 18,
5629 kPromiseDefer = 19,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005630 kHtmlCommentInExternalScript = 20,
5631 kHtmlComment = 21,
5632 kSloppyModeBlockScopedFunctionRedefinition = 22,
5633 kForInInitializer = 23,
5634 kArrayProtectorDirtied = 24,
5635 kArraySpeciesModified = 25,
5636 kArrayPrototypeConstructorModified = 26,
5637 kArrayInstanceProtoModified = 27,
5638 kArrayInstanceConstructorModified = 28,
Ben Murdochda12d292016-06-02 14:46:10 +01005639 kLegacyFunctionDeclaration = 29,
5640 kRegExpPrototypeSourceGetter = 30,
5641 kRegExpPrototypeOldFlagGetter = 31,
Ben Murdochc5610432016-08-08 18:44:38 +01005642 kDecimalWithLeadingZeroInStrictMode = 32,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005643
Ben Murdochc5610432016-08-08 18:44:38 +01005644 // If you add new values here, you'll also need to update Chromium's:
5645 // UseCounter.h, V8PerIsolateData.cpp, histograms.xml
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005646 kUseCounterFeatureCount // This enum value must be last.
5647 };
5648
5649 typedef void (*UseCounterCallback)(Isolate* isolate,
5650 UseCounterFeature feature);
5651
5652
Steve Block44f0eee2011-05-26 01:26:41 +01005653 /**
5654 * Creates a new isolate. Does not change the currently entered
5655 * isolate.
5656 *
5657 * When an isolate is no longer used its resources should be freed
5658 * by calling Dispose(). Using the delete operator is not allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005659 *
5660 * V8::Initialize() must have run prior to this.
Steve Block44f0eee2011-05-26 01:26:41 +01005661 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005662 static Isolate* New(const CreateParams& params);
Steve Block44f0eee2011-05-26 01:26:41 +01005663
5664 /**
5665 * Returns the entered isolate for the current thread or NULL in
5666 * case there is no current isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005667 *
5668 * This method must not be invoked before V8::Initialize() was invoked.
Steve Block44f0eee2011-05-26 01:26:41 +01005669 */
5670 static Isolate* GetCurrent();
5671
5672 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005673 * Custom callback used by embedders to help V8 determine if it should abort
5674 * when it throws and no internal handler is predicted to catch the
5675 * exception. If --abort-on-uncaught-exception is used on the command line,
5676 * then V8 will abort if either:
5677 * - no custom callback is set.
5678 * - the custom callback set returns true.
5679 * Otherwise, the custom callback will not be called and V8 will not abort.
5680 */
5681 typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*);
5682 void SetAbortOnUncaughtExceptionCallback(
5683 AbortOnUncaughtExceptionCallback callback);
5684
5685 /**
Ben Murdochda12d292016-06-02 14:46:10 +01005686 * Optional notification that the system is running low on memory.
5687 * V8 uses these notifications to guide heuristics.
5688 * It is allowed to call this function from another thread while
5689 * the isolate is executing long running JavaScript code.
5690 */
5691 void MemoryPressureNotification(MemoryPressureLevel level);
5692
5693 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005694 * Methods below this point require holding a lock (using Locker) in
5695 * a multi-threaded environment.
5696 */
5697
5698 /**
5699 * Sets this isolate as the entered one for the current thread.
5700 * Saves the previously entered one (if any), so that it can be
5701 * restored when exiting. Re-entering an isolate is allowed.
5702 */
5703 void Enter();
5704
5705 /**
5706 * Exits this isolate by restoring the previously entered one in the
5707 * current thread. The isolate may still stay the same, if it was
5708 * entered more than once.
5709 *
5710 * Requires: this == Isolate::GetCurrent().
5711 */
5712 void Exit();
5713
5714 /**
5715 * Disposes the isolate. The isolate must not be entered by any
5716 * thread to be disposable.
5717 */
5718 void Dispose();
5719
Ben Murdoch257744e2011-11-30 15:57:28 +00005720 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005721 * Discards all V8 thread-specific data for the Isolate. Should be used
5722 * if a thread is terminating and it has used an Isolate that will outlive
5723 * the thread -- all thread-specific data for an Isolate is discarded when
5724 * an Isolate is disposed so this call is pointless if an Isolate is about
5725 * to be Disposed.
5726 */
5727 void DiscardThreadSpecificMetadata();
5728
5729 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005730 * Associate embedder-specific data with the isolate. |slot| has to be
5731 * between 0 and GetNumberOfDataSlots() - 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00005732 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005733 V8_INLINE void SetData(uint32_t slot, void* data);
Ben Murdoch257744e2011-11-30 15:57:28 +00005734
5735 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005736 * Retrieve embedder-specific data from the isolate.
5737 * Returns NULL if SetData has never been called for the given |slot|.
Ben Murdoch257744e2011-11-30 15:57:28 +00005738 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005739 V8_INLINE void* GetData(uint32_t slot);
5740
5741 /**
5742 * Returns the maximum number of available embedder data slots. Valid slots
5743 * are in the range of 0 - GetNumberOfDataSlots() - 1.
5744 */
5745 V8_INLINE static uint32_t GetNumberOfDataSlots();
5746
5747 /**
5748 * Get statistics about the heap memory usage.
5749 */
5750 void GetHeapStatistics(HeapStatistics* heap_statistics);
5751
5752 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005753 * Returns the number of spaces in the heap.
5754 */
5755 size_t NumberOfHeapSpaces();
5756
5757 /**
5758 * Get the memory usage of a space in the heap.
5759 *
5760 * \param space_statistics The HeapSpaceStatistics object to fill in
5761 * statistics.
5762 * \param index The index of the space to get statistics from, which ranges
5763 * from 0 to NumberOfHeapSpaces() - 1.
5764 * \returns true on success.
5765 */
5766 bool GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
5767 size_t index);
5768
5769 /**
5770 * Returns the number of types of objects tracked in the heap at GC.
5771 */
5772 size_t NumberOfTrackedHeapObjectTypes();
5773
5774 /**
5775 * Get statistics about objects in the heap.
5776 *
5777 * \param object_statistics The HeapObjectStatistics object to fill in
5778 * statistics of objects of given type, which were live in the previous GC.
5779 * \param type_index The index of the type of object to fill details about,
5780 * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1.
5781 * \returns true on success.
5782 */
5783 bool GetHeapObjectStatisticsAtLastGC(HeapObjectStatistics* object_statistics,
5784 size_t type_index);
5785
5786 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005787 * Get a call stack sample from the isolate.
5788 * \param state Execution state.
5789 * \param frames Caller allocated buffer to store stack frames.
5790 * \param frames_limit Maximum number of frames to capture. The buffer must
5791 * be large enough to hold the number of frames.
5792 * \param sample_info The sample info is filled up by the function
5793 * provides number of actual captured stack frames and
5794 * the current VM state.
5795 * \note GetStackSample should only be called when the JS thread is paused or
5796 * interrupted. Otherwise the behavior is undefined.
5797 */
5798 void GetStackSample(const RegisterState& state, void** frames,
5799 size_t frames_limit, SampleInfo* sample_info);
5800
5801 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005802 * Adjusts the amount of registered external memory. Used to give V8 an
5803 * indication of the amount of externally allocated memory that is kept alive
5804 * by JavaScript objects. V8 uses this to decide when to perform global
5805 * garbage collections. Registering externally allocated memory will trigger
5806 * global garbage collections more often than it would otherwise in an attempt
5807 * to garbage collect the JavaScript objects that keep the externally
5808 * allocated memory alive.
5809 *
5810 * \param change_in_bytes the change in externally allocated memory that is
5811 * kept alive by JavaScript objects.
5812 * \returns the adjusted value.
5813 */
5814 V8_INLINE int64_t
5815 AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
5816
5817 /**
Ben Murdochc5610432016-08-08 18:44:38 +01005818 * Returns the number of phantom handles without callbacks that were reset
5819 * by the garbage collector since the last call to this function.
5820 */
5821 size_t NumberOfPhantomHandleResetsSinceLastCall();
5822
5823 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005824 * Returns heap profiler for this isolate. Will return NULL until the isolate
5825 * is initialized.
5826 */
5827 HeapProfiler* GetHeapProfiler();
5828
5829 /**
5830 * Returns CPU profiler for this isolate. Will return NULL unless the isolate
5831 * is initialized. It is the embedder's responsibility to stop all CPU
5832 * profiling activities if it has started any.
5833 */
5834 CpuProfiler* GetCpuProfiler();
5835
5836 /** Returns true if this isolate has a current context. */
5837 bool InContext();
5838
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005839 /**
5840 * Returns the context of the currently running JavaScript, or the context
5841 * on the top of the stack if no JavaScript is running.
5842 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005843 Local<Context> GetCurrentContext();
5844
5845 /**
5846 * Returns the context of the calling JavaScript code. That is the
5847 * context of the top-most JavaScript frame. If there are no
5848 * JavaScript frames an empty handle is returned.
5849 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005850 V8_DEPRECATE_SOON(
5851 "Calling context concept is not compatible with tail calls, and will be "
5852 "removed.",
5853 Local<Context> GetCallingContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005854
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005855 /** Returns the last context entered through V8's C++ API. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005856 Local<Context> GetEnteredContext();
5857
5858 /**
5859 * Schedules an exception to be thrown when returning to JavaScript. When an
5860 * exception has been scheduled it is illegal to invoke any JavaScript
5861 * operation; the caller must return immediately and only after the exception
5862 * has been handled does it become legal to invoke JavaScript operations.
5863 */
5864 Local<Value> ThrowException(Local<Value> exception);
5865
5866 /**
5867 * Allows the host application to group objects together. If one
5868 * object in the group is alive, all objects in the group are alive.
5869 * After each garbage collection, object groups are removed. It is
5870 * intended to be used in the before-garbage-collection callback
5871 * function, for instance to simulate DOM tree connections among JS
5872 * wrapper objects. Object groups for all dependent handles need to
5873 * be provided for kGCTypeMarkSweepCompact collections, for all other
5874 * garbage collection types it is sufficient to provide object groups
5875 * for partially dependent handles only.
5876 */
5877 template<typename T> void SetObjectGroupId(const Persistent<T>& object,
5878 UniqueId id);
5879
5880 /**
5881 * Allows the host application to declare implicit references from an object
5882 * group to an object. If the objects of the object group are alive, the child
5883 * object is alive too. After each garbage collection, all implicit references
5884 * are removed. It is intended to be used in the before-garbage-collection
5885 * callback function.
5886 */
5887 template<typename T> void SetReferenceFromGroup(UniqueId id,
5888 const Persistent<T>& child);
5889
5890 /**
5891 * Allows the host application to declare implicit references from an object
5892 * to another object. If the parent object is alive, the child object is alive
5893 * too. After each garbage collection, all implicit references are removed. It
5894 * is intended to be used in the before-garbage-collection callback function.
5895 */
5896 template<typename T, typename S>
5897 void SetReference(const Persistent<T>& parent, const Persistent<S>& child);
5898
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005899 typedef void (*GCCallback)(Isolate* isolate, GCType type,
5900 GCCallbackFlags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005901
5902 /**
5903 * Enables the host application to receive a notification before a
5904 * garbage collection. Allocations are allowed in the callback function,
5905 * but the callback is not re-entrant: if the allocation inside it will
5906 * trigger the garbage collection, the callback won't be called again.
5907 * It is possible to specify the GCType filter for your callback. But it is
5908 * not possible to register the same callback function two times with
5909 * different GCType filters.
5910 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005911 void AddGCPrologueCallback(GCCallback callback,
5912 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005913
5914 /**
5915 * This function removes callback which was installed by
5916 * AddGCPrologueCallback function.
5917 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005918 void RemoveGCPrologueCallback(GCCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005919
5920 /**
Ben Murdochda12d292016-06-02 14:46:10 +01005921 * Sets the embedder heap tracer for the isolate.
5922 */
5923 void SetEmbedderHeapTracer(EmbedderHeapTracer* tracer);
5924
5925 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005926 * Enables the host application to receive a notification after a
5927 * garbage collection. Allocations are allowed in the callback function,
5928 * but the callback is not re-entrant: if the allocation inside it will
5929 * trigger the garbage collection, the callback won't be called again.
5930 * It is possible to specify the GCType filter for your callback. But it is
5931 * not possible to register the same callback function two times with
5932 * different GCType filters.
5933 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005934 void AddGCEpilogueCallback(GCCallback callback,
5935 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005936
5937 /**
5938 * This function removes callback which was installed by
5939 * AddGCEpilogueCallback function.
5940 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005941 void RemoveGCEpilogueCallback(GCCallback callback);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005942
5943 /**
5944 * Forcefully terminate the current thread of JavaScript execution
5945 * in the given isolate.
5946 *
5947 * This method can be used by any thread even if that thread has not
5948 * acquired the V8 lock with a Locker object.
5949 */
5950 void TerminateExecution();
5951
5952 /**
5953 * Is V8 terminating JavaScript execution.
5954 *
5955 * Returns true if JavaScript execution is currently terminating
5956 * because of a call to TerminateExecution. In that case there are
5957 * still JavaScript frames on the stack and the termination
5958 * exception is still active.
5959 */
5960 bool IsExecutionTerminating();
5961
5962 /**
5963 * Resume execution capability in the given isolate, whose execution
5964 * was previously forcefully terminated using TerminateExecution().
5965 *
5966 * When execution is forcefully terminated using TerminateExecution(),
5967 * the isolate can not resume execution until all JavaScript frames
5968 * have propagated the uncatchable exception which is generated. This
5969 * method allows the program embedding the engine to handle the
5970 * termination event and resume execution capability, even if
5971 * JavaScript frames remain on the stack.
5972 *
5973 * This method can be used by any thread even if that thread has not
5974 * acquired the V8 lock with a Locker object.
5975 */
5976 void CancelTerminateExecution();
5977
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005978 /**
5979 * Request V8 to interrupt long running JavaScript code and invoke
5980 * the given |callback| passing the given |data| to it. After |callback|
5981 * returns control will be returned to the JavaScript code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005982 * There may be a number of interrupt requests in flight.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005983 * Can be called from another thread without acquiring a |Locker|.
5984 * Registered |callback| must not reenter interrupted Isolate.
5985 */
5986 void RequestInterrupt(InterruptCallback callback, void* data);
5987
5988 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005989 * Request garbage collection in this Isolate. It is only valid to call this
5990 * function if --expose_gc was specified.
5991 *
5992 * This should only be used for testing purposes and not to enforce a garbage
5993 * collection schedule. It has strong negative impact on the garbage
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005994 * collection performance. Use IdleNotificationDeadline() or
5995 * LowMemoryNotification() instead to influence the garbage collection
5996 * schedule.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005997 */
5998 void RequestGarbageCollectionForTesting(GarbageCollectionType type);
5999
6000 /**
6001 * Set the callback to invoke for logging event.
6002 */
6003 void SetEventLogger(LogEventCallback that);
6004
6005 /**
Ben Murdoch097c5b22016-05-18 11:27:45 +01006006 * Adds a callback to notify the host application right before a script
6007 * is about to run. If a script re-enters the runtime during executing, the
6008 * BeforeCallEnteredCallback is invoked for each re-entrance.
6009 * Executing scripts inside the callback will re-trigger the callback.
6010 */
6011 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
6012
6013 /**
6014 * Removes callback that was installed by AddBeforeCallEnteredCallback.
6015 */
6016 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
6017
6018 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006019 * Adds a callback to notify the host application when a script finished
6020 * running. If a script re-enters the runtime during executing, the
6021 * CallCompletedCallback is only invoked when the outer-most script
6022 * execution ends. Executing scripts inside the callback do not trigger
6023 * further callbacks.
6024 */
6025 void AddCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006026 V8_DEPRECATE_SOON(
6027 "Use callback with parameter",
6028 void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006029
6030 /**
6031 * Removes callback that was installed by AddCallCompletedCallback.
6032 */
6033 void RemoveCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006034 V8_DEPRECATE_SOON(
6035 "Use callback with parameter",
6036 void RemoveCallCompletedCallback(
6037 DeprecatedCallCompletedCallback callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006038
6039 /**
6040 * Set callback to notify about promise reject with no handler, or
6041 * revocation of such a previous notification once the handler is added.
6042 */
6043 void SetPromiseRejectCallback(PromiseRejectCallback callback);
6044
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006045 /**
6046 * Experimental: Runs the Microtask Work Queue until empty
6047 * Any exceptions thrown by microtask callbacks are swallowed.
6048 */
6049 void RunMicrotasks();
6050
6051 /**
6052 * Experimental: Enqueues the callback to the Microtask Work Queue
6053 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006054 void EnqueueMicrotask(Local<Function> microtask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006055
6056 /**
6057 * Experimental: Enqueues the callback to the Microtask Work Queue
6058 */
6059 void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL);
6060
Ben Murdochda12d292016-06-02 14:46:10 +01006061 /**
6062 * Experimental: Controls how Microtasks are invoked. See MicrotasksPolicy
6063 * for details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006064 */
Ben Murdochda12d292016-06-02 14:46:10 +01006065 void SetMicrotasksPolicy(MicrotasksPolicy policy);
6066 V8_DEPRECATE_SOON("Use SetMicrotasksPolicy",
6067 void SetAutorunMicrotasks(bool autorun));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006068
6069 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006070 * Experimental: Returns the policy controlling how Microtasks are invoked.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006071 */
Ben Murdochda12d292016-06-02 14:46:10 +01006072 MicrotasksPolicy GetMicrotasksPolicy() const;
6073 V8_DEPRECATE_SOON("Use GetMicrotasksPolicy",
6074 bool WillAutorunMicrotasks() const);
6075
6076 /**
6077 * Experimental: adds a callback to notify the host application after
6078 * microtasks were run. The callback is triggered by explicit RunMicrotasks
6079 * call or automatic microtasks execution (see SetAutorunMicrotasks).
6080 *
6081 * Callback will trigger even if microtasks were attempted to run,
6082 * but the microtasks queue was empty and no single microtask was actually
6083 * executed.
6084 *
6085 * Executing scriptsinside the callback will not re-trigger microtasks and
6086 * the callback.
6087 */
6088 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
6089
6090 /**
6091 * Removes callback that was installed by AddMicrotasksCompletedCallback.
6092 */
6093 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006094
6095 /**
6096 * Sets a callback for counting the number of times a feature of V8 is used.
6097 */
6098 void SetUseCounterCallback(UseCounterCallback callback);
6099
6100 /**
6101 * Enables the host application to provide a mechanism for recording
6102 * statistics counters.
6103 */
6104 void SetCounterFunction(CounterLookupCallback);
6105
6106 /**
6107 * Enables the host application to provide a mechanism for recording
6108 * histograms. The CreateHistogram function returns a
6109 * histogram which will later be passed to the AddHistogramSample
6110 * function.
6111 */
6112 void SetCreateHistogramFunction(CreateHistogramCallback);
6113 void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
6114
6115 /**
6116 * Optional notification that the embedder is idle.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006117 * V8 uses the notification to perform garbage collection.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006118 * This call can be used repeatedly if the embedder remains idle.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006119 * Returns true if the embedder should stop calling IdleNotificationDeadline
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006120 * until real work has been done. This indicates that V8 has done
6121 * as much cleanup as it will be able to do.
6122 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006123 * The deadline_in_seconds argument specifies the deadline V8 has to finish
6124 * garbage collection work. deadline_in_seconds is compared with
6125 * MonotonicallyIncreasingTime() and should be based on the same timebase as
6126 * that function. There is no guarantee that the actual work will be done
6127 * within the time limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006128 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006129 bool IdleNotificationDeadline(double deadline_in_seconds);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006131 V8_DEPRECATED("use IdleNotificationDeadline()",
6132 bool IdleNotification(int idle_time_in_ms));
6133
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006134 /**
6135 * Optional notification that the system is running low on memory.
6136 * V8 uses these notifications to attempt to free memory.
6137 */
6138 void LowMemoryNotification();
6139
6140 /**
6141 * Optional notification that a context has been disposed. V8 uses
6142 * these notifications to guide the GC heuristic. Returns the number
6143 * of context disposals - including this one - since the last time
6144 * V8 had a chance to clean up.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006145 *
6146 * The optional parameter |dependant_context| specifies whether the disposed
6147 * context was depending on state from other contexts or not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006148 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006149 int ContextDisposedNotification(bool dependant_context = true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006150
6151 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006152 * Optional notification that the isolate switched to the foreground.
6153 * V8 uses these notifications to guide heuristics.
6154 */
6155 void IsolateInForegroundNotification();
6156
6157 /**
6158 * Optional notification that the isolate switched to the background.
6159 * V8 uses these notifications to guide heuristics.
6160 */
6161 void IsolateInBackgroundNotification();
6162
6163 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006164 * Allows the host application to provide the address of a function that is
6165 * notified each time code is added, moved or removed.
6166 *
6167 * \param options options for the JIT code event handler.
6168 * \param event_handler the JIT code event handler, which will be invoked
6169 * each time code is added, moved or removed.
6170 * \note \p event_handler won't get notified of existent code.
6171 * \note since code removal notifications are not currently issued, the
6172 * \p event_handler may get notifications of code that overlaps earlier
6173 * code notifications. This happens when code areas are reused, and the
6174 * earlier overlapping code areas should therefore be discarded.
6175 * \note the events passed to \p event_handler and the strings they point to
6176 * are not guaranteed to live past each call. The \p event_handler must
6177 * copy strings and other parameters it needs to keep around.
6178 * \note the set of events declared in JitCodeEvent::EventType is expected to
6179 * grow over time, and the JitCodeEvent structure is expected to accrue
6180 * new members. The \p event_handler function must ignore event codes
6181 * it does not recognize to maintain future compatibility.
6182 * \note Use Isolate::CreateParams to get events for code executed during
6183 * Isolate setup.
6184 */
6185 void SetJitCodeEventHandler(JitCodeEventOptions options,
6186 JitCodeEventHandler event_handler);
6187
6188 /**
6189 * Modifies the stack limit for this Isolate.
6190 *
6191 * \param stack_limit An address beyond which the Vm's stack may not grow.
6192 *
6193 * \note If you are using threads then you should hold the V8::Locker lock
6194 * while setting the stack limit and you must set a non-default stack
6195 * limit separately for each thread.
6196 */
6197 void SetStackLimit(uintptr_t stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00006198
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006199 /**
6200 * Returns a memory range that can potentially contain jitted code.
6201 *
6202 * On Win64, embedders are advised to install function table callbacks for
6203 * these ranges, as default SEH won't be able to unwind through jitted code.
6204 *
6205 * The first page of the code range is reserved for the embedder and is
6206 * committed, writable, and executable.
6207 *
6208 * Might be empty on other platforms.
6209 *
6210 * https://code.google.com/p/v8/issues/detail?id=3598
6211 */
6212 void GetCodeRange(void** start, size_t* length_in_bytes);
6213
6214 /** Set the callback to invoke in case of fatal errors. */
6215 void SetFatalErrorHandler(FatalErrorCallback that);
6216
6217 /**
6218 * Set the callback to invoke to check if code generation from
6219 * strings should be allowed.
6220 */
6221 void SetAllowCodeGenerationFromStringsCallback(
6222 AllowCodeGenerationFromStringsCallback callback);
6223
6224 /**
6225 * Check if V8 is dead and therefore unusable. This is the case after
6226 * fatal errors such as out-of-memory situations.
6227 */
6228 bool IsDead();
6229
6230 /**
6231 * Adds a message listener.
6232 *
6233 * The same message listener can be added more than once and in that
6234 * case it will be called more than once for each message.
6235 *
6236 * If data is specified, it will be passed to the callback when it is called.
6237 * Otherwise, the exception object will be passed to the callback instead.
6238 */
6239 bool AddMessageListener(MessageCallback that,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006240 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006241
6242 /**
6243 * Remove all message listeners from the specified callback function.
6244 */
6245 void RemoveMessageListeners(MessageCallback that);
6246
6247 /** Callback function for reporting failed access checks.*/
6248 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
6249
6250 /**
6251 * Tells V8 to capture current stack trace when uncaught exception occurs
6252 * and report it to the message listeners. The option is off by default.
6253 */
6254 void SetCaptureStackTraceForUncaughtExceptions(
6255 bool capture, int frame_limit = 10,
6256 StackTrace::StackTraceOptions options = StackTrace::kOverview);
6257
6258 /**
6259 * Enables the host application to provide a mechanism to be notified
6260 * and perform custom logging when V8 Allocates Executable Memory.
6261 */
Ben Murdochc5610432016-08-08 18:44:38 +01006262 void V8_DEPRECATED(
6263 "Use a combination of RequestInterrupt and GCCallback instead",
6264 AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6265 ObjectSpace space, AllocationAction action));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006266
6267 /**
6268 * Removes callback that was installed by AddMemoryAllocationCallback.
6269 */
Ben Murdochc5610432016-08-08 18:44:38 +01006270 void V8_DEPRECATED(
6271 "Use a combination of RequestInterrupt and GCCallback instead",
6272 RemoveMemoryAllocationCallback(MemoryAllocationCallback callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006273
6274 /**
6275 * Iterates through all external resources referenced from current isolate
6276 * heap. GC is not invoked prior to iterating, therefore there is no
6277 * guarantee that visited objects are still alive.
6278 */
6279 void VisitExternalResources(ExternalResourceVisitor* visitor);
6280
6281 /**
6282 * Iterates through all the persistent handles in the current isolate's heap
6283 * that have class_ids.
6284 */
6285 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
6286
6287 /**
6288 * Iterates through all the persistent handles in the current isolate's heap
6289 * that have class_ids and are candidates to be marked as partially dependent
6290 * handles. This will visit handles to young objects created since the last
6291 * garbage collection but is free to visit an arbitrary superset of these
6292 * objects.
6293 */
6294 void VisitHandlesForPartialDependence(PersistentHandleVisitor* visitor);
6295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006296 /**
6297 * Iterates through all the persistent handles in the current isolate's heap
6298 * that have class_ids and are weak to be marked as inactive if there is no
6299 * pending activity for the handle.
6300 */
6301 void VisitWeakHandles(PersistentHandleVisitor* visitor);
6302
Steve Block44f0eee2011-05-26 01:26:41 +01006303 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006304 template <class K, class V, class Traits>
6305 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006306
Steve Block44f0eee2011-05-26 01:26:41 +01006307 Isolate();
6308 Isolate(const Isolate&);
6309 ~Isolate();
6310 Isolate& operator=(const Isolate&);
6311 void* operator new(size_t size);
6312 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006313
6314 void SetObjectGroupId(internal::Object** object, UniqueId id);
6315 void SetReferenceFromGroup(UniqueId id, internal::Object** object);
6316 void SetReference(internal::Object** parent, internal::Object** child);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006317 void ReportExternalAllocationLimitReached();
Steve Block44f0eee2011-05-26 01:26:41 +01006318};
6319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006320class V8_EXPORT StartupData {
Ben Murdoch257744e2011-11-30 15:57:28 +00006321 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006322 const char* data;
Ben Murdoch257744e2011-11-30 15:57:28 +00006323 int raw_size;
6324};
6325
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006326
6327/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006328 * EntropySource is used as a callback function when v8 needs a source
6329 * of entropy.
6330 */
6331typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
6332
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006333
6334/**
6335 * ReturnAddressLocationResolver is used as a callback function when v8 is
6336 * resolving the location of a return address on the stack. Profilers that
6337 * change the return address on the stack can use this to resolve the stack
6338 * location to whereever the profiler stashed the original return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006339 *
6340 * \param return_addr_location points to a location on stack where a machine
6341 * return address resides.
6342 * \returns either return_addr_location, or else a pointer to the profiler's
6343 * copy of the original return address.
6344 *
6345 * \note the resolver function must not cause garbage collection.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006346 */
6347typedef uintptr_t (*ReturnAddressLocationResolver)(
6348 uintptr_t return_addr_location);
6349
6350
6351/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006352 * Container class for static utility functions.
6353 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006354class V8_EXPORT V8 {
Steve Blocka7e24c12009-10-30 11:49:00 +00006355 public:
6356 /** Set the callback to invoke in case of fatal errors. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006357 V8_INLINE static V8_DEPRECATED(
6358 "Use isolate version",
6359 void SetFatalErrorHandler(FatalErrorCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006360
6361 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00006362 * Set the callback to invoke to check if code generation from
6363 * strings should be allowed.
6364 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006365 V8_INLINE static V8_DEPRECATED(
6366 "Use isolate version", void SetAllowCodeGenerationFromStringsCallback(
6367 AllowCodeGenerationFromStringsCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006368
6369 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006370 * Check if V8 is dead and therefore unusable. This is the case after
6371 * fatal errors such as out-of-memory situations.
6372 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006373 V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead());
Ben Murdoch257744e2011-11-30 15:57:28 +00006374
6375 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006376 * Hand startup data to V8, in case the embedder has chosen to build
6377 * V8 with external startup data.
6378 *
6379 * Note:
6380 * - By default the startup data is linked into the V8 library, in which
6381 * case this function is not meaningful.
6382 * - If this needs to be called, it needs to be called before V8
6383 * tries to make use of its built-ins.
6384 * - To avoid unnecessary copies of data, V8 will point directly into the
6385 * given data blob, so pretty please keep it around until V8 exit.
6386 * - Compression of the startup blob might be useful, but needs to
6387 * handled entirely on the embedders' side.
6388 * - The call will abort if the data is invalid.
6389 */
6390 static void SetNativesDataBlob(StartupData* startup_blob);
6391 static void SetSnapshotDataBlob(StartupData* startup_blob);
6392
6393 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006394 * Bootstrap an isolate and a context from scratch to create a startup
6395 * snapshot. Include the side-effects of running the optional script.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006396 * Returns { NULL, 0 } on failure.
Ben Murdochda12d292016-06-02 14:46:10 +01006397 * The caller acquires ownership of the data array in the return value.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006398 */
Ben Murdochda12d292016-06-02 14:46:10 +01006399 static StartupData CreateSnapshotDataBlob(const char* embedded_source = NULL);
6400
6401 /**
6402 * Bootstrap an isolate and a context from the cold startup blob, run the
6403 * warm-up script to trigger code compilation. The side effects are then
6404 * discarded. The resulting startup snapshot will include compiled code.
6405 * Returns { NULL, 0 } on failure.
6406 * The caller acquires ownership of the data array in the return value.
6407 * The argument startup blob is untouched.
6408 */
6409 static StartupData WarmUpSnapshotDataBlob(StartupData cold_startup_blob,
6410 const char* warmup_source);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006411
6412 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006413 * Adds a message listener.
6414 *
Ben Murdoch257744e2011-11-30 15:57:28 +00006415 * The same message listener can be added more than once and in that
Steve Blocka7e24c12009-10-30 11:49:00 +00006416 * case it will be called more than once for each message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006417 *
6418 * If data is specified, it will be passed to the callback when it is called.
6419 * Otherwise, the exception object will be passed to the callback instead.
Steve Blocka7e24c12009-10-30 11:49:00 +00006420 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006421 V8_INLINE static V8_DEPRECATED(
6422 "Use isolate version",
6423 bool AddMessageListener(MessageCallback that,
6424 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006425
6426 /**
6427 * Remove all message listeners from the specified callback function.
6428 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006429 V8_INLINE static V8_DEPRECATED(
6430 "Use isolate version", void RemoveMessageListeners(MessageCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006431
6432 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006433 * Tells V8 to capture current stack trace when uncaught exception occurs
6434 * and report it to the message listeners. The option is off by default.
6435 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006436 V8_INLINE static V8_DEPRECATED(
6437 "Use isolate version",
6438 void SetCaptureStackTraceForUncaughtExceptions(
6439 bool capture, int frame_limit = 10,
6440 StackTrace::StackTraceOptions options = StackTrace::kOverview));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006441
6442 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006443 * Sets V8 flags from a string.
6444 */
6445 static void SetFlagsFromString(const char* str, int length);
6446
6447 /**
6448 * Sets V8 flags from the command line.
6449 */
6450 static void SetFlagsFromCommandLine(int* argc,
6451 char** argv,
6452 bool remove_flags);
6453
6454 /** Get the version string. */
6455 static const char* GetVersion();
6456
Steve Blocka7e24c12009-10-30 11:49:00 +00006457 /** Callback function for reporting failed access checks.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006458 V8_INLINE static V8_DEPRECATED(
6459 "Use isolate version",
6460 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback));
Steve Blocka7e24c12009-10-30 11:49:00 +00006461
6462 /**
6463 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01006464 * garbage collection. Allocations are not allowed in the
6465 * callback function, you therefore cannot manipulate objects (set
6466 * or delete properties for example) since it is possible such
6467 * operations will result in the allocation of objects. It is possible
6468 * to specify the GCType filter for your callback. But it is not possible to
6469 * register the same callback function two times with different
6470 * GCType filters.
6471 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006472 static V8_DEPRECATED(
6473 "Use isolate version",
6474 void AddGCPrologueCallback(GCCallback callback,
6475 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006476
6477 /**
6478 * This function removes callback which was installed by
6479 * AddGCPrologueCallback function.
6480 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006481 V8_INLINE static V8_DEPRECATED(
6482 "Use isolate version",
6483 void RemoveGCPrologueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006484
6485 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006486 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01006487 * garbage collection. Allocations are not allowed in the
6488 * callback function, you therefore cannot manipulate objects (set
6489 * or delete properties for example) since it is possible such
6490 * operations will result in the allocation of objects. It is possible
6491 * to specify the GCType filter for your callback. But it is not possible to
6492 * register the same callback function two times with different
6493 * GCType filters.
6494 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006495 static V8_DEPRECATED(
6496 "Use isolate version",
6497 void AddGCEpilogueCallback(GCCallback callback,
6498 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006499
6500 /**
6501 * This function removes callback which was installed by
6502 * AddGCEpilogueCallback function.
6503 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006504 V8_INLINE static V8_DEPRECATED(
6505 "Use isolate version",
6506 void RemoveGCEpilogueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006507
6508 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006509 * Initializes V8. This function needs to be called before the first Isolate
6510 * is created. It always returns true.
Steve Blocka7e24c12009-10-30 11:49:00 +00006511 */
6512 static bool Initialize();
6513
6514 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006515 * Allows the host application to provide a callback which can be used
6516 * as a source of entropy for random number generators.
6517 */
6518 static void SetEntropySource(EntropySource source);
6519
6520 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006521 * Allows the host application to provide a callback that allows v8 to
6522 * cooperate with a profiler that rewrites return addresses on stack.
6523 */
6524 static void SetReturnAddressLocationResolver(
6525 ReturnAddressLocationResolver return_address_resolver);
6526
6527 /**
Steve Block44f0eee2011-05-26 01:26:41 +01006528 * Forcefully terminate the current thread of JavaScript execution
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006529 * in the given isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +00006530 *
6531 * This method can be used by any thread even if that thread has not
6532 * acquired the V8 lock with a Locker object.
Steve Block44f0eee2011-05-26 01:26:41 +01006533 *
6534 * \param isolate The isolate in which to terminate the current JS execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006535 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006536 V8_INLINE static V8_DEPRECATED("Use isolate version",
6537 void TerminateExecution(Isolate* isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00006538
6539 /**
Steve Block6ded16b2010-05-10 14:33:55 +01006540 * Is V8 terminating JavaScript execution.
6541 *
6542 * Returns true if JavaScript execution is currently terminating
6543 * because of a call to TerminateExecution. In that case there are
6544 * still JavaScript frames on the stack and the termination
6545 * exception is still active.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006546 *
6547 * \param isolate The isolate in which to check.
Steve Block6ded16b2010-05-10 14:33:55 +01006548 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006549 V8_INLINE static V8_DEPRECATED(
6550 "Use isolate version",
6551 bool IsExecutionTerminating(Isolate* isolate = NULL));
Steve Block6ded16b2010-05-10 14:33:55 +01006552
6553 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006554 * Resume execution capability in the given isolate, whose execution
6555 * was previously forcefully terminated using TerminateExecution().
6556 *
6557 * When execution is forcefully terminated using TerminateExecution(),
6558 * the isolate can not resume execution until all JavaScript frames
6559 * have propagated the uncatchable exception which is generated. This
6560 * method allows the program embedding the engine to handle the
6561 * termination event and resume execution capability, even if
6562 * JavaScript frames remain on the stack.
6563 *
6564 * This method can be used by any thread even if that thread has not
6565 * acquired the V8 lock with a Locker object.
6566 *
6567 * \param isolate The isolate in which to resume execution capability.
6568 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006569 V8_INLINE static V8_DEPRECATED(
6570 "Use isolate version", void CancelTerminateExecution(Isolate* isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006571
6572 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006573 * Releases any resources used by v8 and stops any utility threads
6574 * that may be running. Note that disposing v8 is permanent, it
6575 * cannot be reinitialized.
6576 *
6577 * It should generally not be necessary to dispose v8 before exiting
6578 * a process, this should happen automatically. It is only necessary
6579 * to use if the process needs the resources taken up by v8.
6580 */
6581 static bool Dispose();
6582
Steve Block3ce2e202009-11-05 08:53:23 +00006583 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006584 * Iterates through all external resources referenced from current isolate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006585 * heap. GC is not invoked prior to iterating, therefore there is no
6586 * guarantee that visited objects are still alive.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006587 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006588 V8_INLINE static V8_DEPRECATED(
6589 "Use isolate version",
6590 void VisitExternalResources(ExternalResourceVisitor* visitor));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006591
6592 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006593 * Iterates through all the persistent handles in the current isolate's heap
6594 * that have class_ids.
6595 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006596 V8_INLINE static V8_DEPRECATED(
6597 "Use isolate version",
6598 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006599
6600 /**
6601 * Iterates through all the persistent handles in isolate's heap that have
6602 * class_ids.
6603 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006604 V8_INLINE static V8_DEPRECATED(
6605 "Use isolate version",
6606 void VisitHandlesWithClassIds(Isolate* isolate,
6607 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006608
6609 /**
6610 * Iterates through all the persistent handles in the current isolate's heap
6611 * that have class_ids and are candidates to be marked as partially dependent
6612 * handles. This will visit handles to young objects created since the last
6613 * garbage collection but is free to visit an arbitrary superset of these
6614 * objects.
6615 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006616 V8_INLINE static V8_DEPRECATED(
6617 "Use isolate version",
6618 void VisitHandlesForPartialDependence(Isolate* isolate,
6619 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006620
6621 /**
6622 * Initialize the ICU library bundled with V8. The embedder should only
6623 * invoke this method when using the bundled ICU. Returns true on success.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006624 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006625 * If V8 was compiled with the ICU data in an external file, the location
6626 * of the data file has to be provided.
Steve Blocka7e24c12009-10-30 11:49:00 +00006627 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006628 static bool InitializeICU(const char* icu_data_file = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006629
6630 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006631 * Initialize the external startup data. The embedder only needs to
6632 * invoke this method when external startup data was enabled in a build.
6633 *
6634 * If V8 was compiled with the startup data in an external file, then
6635 * V8 needs to be given those external files during startup. There are
6636 * three ways to do this:
6637 * - InitializeExternalStartupData(const char*)
6638 * This will look in the given directory for files "natives_blob.bin"
6639 * and "snapshot_blob.bin" - which is what the default build calls them.
6640 * - InitializeExternalStartupData(const char*, const char*)
6641 * As above, but will directly use the two given file names.
6642 * - Call SetNativesDataBlob, SetNativesDataBlob.
6643 * This will read the blobs from the given data structures and will
6644 * not perform any file IO.
6645 */
6646 static void InitializeExternalStartupData(const char* directory_path);
6647 static void InitializeExternalStartupData(const char* natives_blob,
6648 const char* snapshot_blob);
6649 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006650 * Sets the v8::Platform to use. This should be invoked before V8 is
6651 * initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +00006652 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006653 static void InitializePlatform(Platform* platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00006654
Steve Block6ded16b2010-05-10 14:33:55 +01006655 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006656 * Clears all references to the v8::Platform. This should be invoked after
6657 * V8 was disposed.
Steve Block6ded16b2010-05-10 14:33:55 +01006658 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006659 static void ShutdownPlatform();
Steve Block6ded16b2010-05-10 14:33:55 +01006660
Steve Blocka7e24c12009-10-30 11:49:00 +00006661 private:
6662 V8();
6663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006664 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
6665 internal::Object** handle);
6666 static internal::Object** CopyPersistent(internal::Object** handle);
Steve Blocka7e24c12009-10-30 11:49:00 +00006667 static void DisposeGlobal(internal::Object** global_handle);
Ben Murdochc5610432016-08-08 18:44:38 +01006668 static void MakeWeak(internal::Object** location, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006669 WeakCallbackInfo<void>::Callback weak_callback,
6670 WeakCallbackType type);
Ben Murdochc5610432016-08-08 18:44:38 +01006671 static void MakeWeak(internal::Object** location, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006672 // Must be 0 or -1.
6673 int internal_field_index1,
6674 // Must be 1 or -1.
6675 int internal_field_index2,
6676 WeakCallbackInfo<void>::Callback weak_callback);
Ben Murdochc5610432016-08-08 18:44:38 +01006677 static void MakeWeak(internal::Object*** location_addr);
6678 static void* ClearWeak(internal::Object** location);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006679 static void Eternalize(Isolate* isolate,
6680 Value* handle,
6681 int* index);
6682 static Local<Value> GetEternal(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006683
Ben Murdochc5610432016-08-08 18:44:38 +01006684 static void RegisterExternallyReferencedObject(internal::Object** object,
6685 internal::Isolate* isolate);
6686 template <class K, class V, class T>
6687 friend class PersistentValueMapBase;
6688
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006689 static void FromJustIsNothing();
6690 static void ToLocalEmpty();
6691 static void InternalFieldOutOfBounds(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006692 template <class T> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006693 template <class T>
6694 friend class MaybeLocal;
6695 template <class T>
6696 friend class Maybe;
6697 template <class T>
6698 friend class WeakCallbackInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006699 template <class T> friend class Eternal;
6700 template <class T> friend class PersistentBase;
6701 template <class T, class M> friend class Persistent;
Steve Blocka7e24c12009-10-30 11:49:00 +00006702 friend class Context;
6703};
6704
6705
6706/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006707 * A simple Maybe type, representing an object which may or may not have a
6708 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
6709 *
6710 * If an API method returns a Maybe<>, the API method can potentially fail
6711 * either because an exception is thrown, or because an exception is pending,
6712 * e.g. because a previous API call threw an exception that hasn't been caught
6713 * yet, or because a TerminateExecution exception was thrown. In that case, a
6714 * "Nothing" value is returned.
6715 */
6716template <class T>
6717class Maybe {
6718 public:
6719 V8_INLINE bool IsNothing() const { return !has_value; }
6720 V8_INLINE bool IsJust() const { return has_value; }
6721
6722 // Will crash if the Maybe<> is nothing.
6723 V8_INLINE T FromJust() const {
6724 if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing();
6725 return value;
6726 }
6727
6728 V8_INLINE T FromMaybe(const T& default_value) const {
6729 return has_value ? value : default_value;
6730 }
6731
6732 V8_INLINE bool operator==(const Maybe& other) const {
6733 return (IsJust() == other.IsJust()) &&
6734 (!IsJust() || FromJust() == other.FromJust());
6735 }
6736
6737 V8_INLINE bool operator!=(const Maybe& other) const {
6738 return !operator==(other);
6739 }
6740
6741 private:
6742 Maybe() : has_value(false) {}
6743 explicit Maybe(const T& t) : has_value(true), value(t) {}
6744
6745 bool has_value;
6746 T value;
6747
6748 template <class U>
6749 friend Maybe<U> Nothing();
6750 template <class U>
6751 friend Maybe<U> Just(const U& u);
6752};
6753
6754
6755template <class T>
6756inline Maybe<T> Nothing() {
6757 return Maybe<T>();
6758}
6759
6760
6761template <class T>
6762inline Maybe<T> Just(const T& t) {
6763 return Maybe<T>(t);
6764}
6765
6766
6767/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006768 * An external exception handler.
6769 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006770class V8_EXPORT TryCatch {
Steve Blocka7e24c12009-10-30 11:49:00 +00006771 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006772 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006773 * Creates a new try/catch block and registers it with v8. Note that
6774 * all TryCatch blocks should be stack allocated because the memory
6775 * location itself is compared against JavaScript try/catch blocks.
Steve Blocka7e24c12009-10-30 11:49:00 +00006776 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006777 V8_DEPRECATED("Use isolate version", TryCatch());
Steve Blocka7e24c12009-10-30 11:49:00 +00006778
6779 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006780 * Creates a new try/catch block and registers it with v8. Note that
6781 * all TryCatch blocks should be stack allocated because the memory
6782 * location itself is compared against JavaScript try/catch blocks.
6783 */
6784 TryCatch(Isolate* isolate);
6785
6786 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006787 * Unregisters and deletes this try/catch block.
6788 */
6789 ~TryCatch();
6790
6791 /**
6792 * Returns true if an exception has been caught by this try/catch block.
6793 */
6794 bool HasCaught() const;
6795
6796 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006797 * For certain types of exceptions, it makes no sense to continue execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006798 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006799 * If CanContinue returns false, the correct action is to perform any C++
6800 * cleanup needed and then return. If CanContinue returns false and
6801 * HasTerminated returns true, it is possible to call
6802 * CancelTerminateExecution in order to continue calling into the engine.
Steve Blocka7e24c12009-10-30 11:49:00 +00006803 */
6804 bool CanContinue() const;
6805
6806 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006807 * Returns true if an exception has been caught due to script execution
6808 * being terminated.
6809 *
6810 * There is no JavaScript representation of an execution termination
6811 * exception. Such exceptions are thrown when the TerminateExecution
6812 * methods are called to terminate a long-running script.
6813 *
6814 * If such an exception has been thrown, HasTerminated will return true,
6815 * indicating that it is possible to call CancelTerminateExecution in order
6816 * to continue calling into the engine.
6817 */
6818 bool HasTerminated() const;
6819
6820 /**
Steve Blockd0582a62009-12-15 09:54:21 +00006821 * Throws the exception caught by this TryCatch in a way that avoids
6822 * it being caught again by this same TryCatch. As with ThrowException
6823 * it is illegal to execute any JavaScript operations after calling
6824 * ReThrow; the caller must return immediately to where the exception
6825 * is caught.
6826 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006827 Local<Value> ReThrow();
Steve Blockd0582a62009-12-15 09:54:21 +00006828
6829 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006830 * Returns the exception caught by this try/catch block. If no exception has
6831 * been caught an empty handle is returned.
6832 *
6833 * The returned handle is valid until this TryCatch block has been destroyed.
6834 */
6835 Local<Value> Exception() const;
6836
6837 /**
6838 * Returns the .stack property of the thrown object. If no .stack
6839 * property is present an empty handle is returned.
6840 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006841 V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const);
6842 V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
6843 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006844
6845 /**
6846 * Returns the message associated with this exception. If there is
6847 * no message associated an empty handle is returned.
6848 *
6849 * The returned handle is valid until this TryCatch block has been
6850 * destroyed.
6851 */
6852 Local<v8::Message> Message() const;
6853
6854 /**
6855 * Clears any exceptions that may have been caught by this try/catch block.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006856 * After this method has been called, HasCaught() will return false. Cancels
6857 * the scheduled exception if it is caught and ReThrow() is not called before.
Steve Blocka7e24c12009-10-30 11:49:00 +00006858 *
6859 * It is not necessary to clear a try/catch block before using it again; if
6860 * another exception is thrown the previously caught exception will just be
6861 * overwritten. However, it is often a good idea since it makes it easier
6862 * to determine which operation threw a given exception.
6863 */
6864 void Reset();
6865
6866 /**
6867 * Set verbosity of the external exception handler.
6868 *
6869 * By default, exceptions that are caught by an external exception
6870 * handler are not reported. Call SetVerbose with true on an
6871 * external exception handler to have exceptions caught by the
6872 * handler reported as if they were not caught.
6873 */
6874 void SetVerbose(bool value);
6875
6876 /**
6877 * Set whether or not this TryCatch should capture a Message object
6878 * which holds source information about where the exception
6879 * occurred. True by default.
6880 */
6881 void SetCaptureMessage(bool value);
6882
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006883 /**
6884 * There are cases when the raw address of C++ TryCatch object cannot be
6885 * used for comparisons with addresses into the JS stack. The cases are:
6886 * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
6887 * 2) Address sanitizer allocates local C++ object in the heap when
6888 * UseAfterReturn mode is enabled.
6889 * This method returns address that can be used for comparisons with
6890 * addresses into the JS stack. When neither simulator nor ASAN's
6891 * UseAfterReturn is enabled, then the address returned will be the address
6892 * of the C++ try catch handler itself.
6893 */
6894 static void* JSStackComparableAddress(v8::TryCatch* handler) {
6895 if (handler == NULL) return NULL;
6896 return handler->js_stack_comparable_address_;
6897 }
6898
Steve Blockd0582a62009-12-15 09:54:21 +00006899 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006900 void ResetInternal();
6901
6902 // Make it hard to create heap-allocated TryCatch blocks.
6903 TryCatch(const TryCatch&);
6904 void operator=(const TryCatch&);
6905 void* operator new(size_t size);
6906 void operator delete(void*, size_t);
6907
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006908 v8::internal::Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006909 v8::TryCatch* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006910 void* exception_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006911 void* message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006912 void* js_stack_comparable_address_;
Steve Blockd0582a62009-12-15 09:54:21 +00006913 bool is_verbose_ : 1;
6914 bool can_continue_ : 1;
6915 bool capture_message_ : 1;
6916 bool rethrow_ : 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006917 bool has_terminated_ : 1;
Steve Blockd0582a62009-12-15 09:54:21 +00006918
Steve Block44f0eee2011-05-26 01:26:41 +01006919 friend class v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +00006920};
6921
6922
Ben Murdoch257744e2011-11-30 15:57:28 +00006923// --- Context ---
Steve Blocka7e24c12009-10-30 11:49:00 +00006924
6925
6926/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006927 * A container for extension names.
Steve Blocka7e24c12009-10-30 11:49:00 +00006928 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006929class V8_EXPORT ExtensionConfiguration {
Steve Blocka7e24c12009-10-30 11:49:00 +00006930 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006931 ExtensionConfiguration() : name_count_(0), names_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00006932 ExtensionConfiguration(int name_count, const char* names[])
6933 : name_count_(name_count), names_(names) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006934
6935 const char** begin() const { return &names_[0]; }
6936 const char** end() const { return &names_[name_count_]; }
6937
Steve Blocka7e24c12009-10-30 11:49:00 +00006938 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006939 const int name_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006940 const char** names_;
6941};
6942
6943
6944/**
6945 * A sandboxed execution context with its own set of built-in objects
6946 * and functions.
6947 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006948class V8_EXPORT Context {
Steve Blocka7e24c12009-10-30 11:49:00 +00006949 public:
Steve Block1e0659c2011-05-24 12:43:12 +01006950 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006951 * Returns the global proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006952 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006953 * Global proxy object is a thin wrapper whose prototype points to actual
6954 * context's global object with the properties like Object, etc. This is done
6955 * that way for security reasons (for more details see
Steve Block1e0659c2011-05-24 12:43:12 +01006956 * https://wiki.mozilla.org/Gecko:SplitWindow).
6957 *
6958 * Please note that changes to global proxy object prototype most probably
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006959 * would break VM---v8 expects only global object as a prototype of global
6960 * proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006961 */
Steve Blocka7e24c12009-10-30 11:49:00 +00006962 Local<Object> Global();
6963
6964 /**
6965 * Detaches the global object from its context before
6966 * the global object can be reused to create a new context.
6967 */
6968 void DetachGlobal();
6969
Andrei Popescu74b3c142010-03-29 12:03:09 +01006970 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006971 * Creates a new context and returns a handle to the newly allocated
6972 * context.
Andrei Popescu74b3c142010-03-29 12:03:09 +01006973 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006974 * \param isolate The isolate in which to create the context.
Steve Block9fac8402011-05-12 15:51:54 +01006975 *
6976 * \param extensions An optional extension configuration containing
6977 * the extensions to be installed in the newly created context.
6978 *
6979 * \param global_template An optional object template from which the
6980 * global object for the newly created context will be created.
6981 *
6982 * \param global_object An optional global object to be reused for
6983 * the newly created context. This global object must have been
6984 * created by a previous call to Context::New with the same global
6985 * template. The state of the global object will be completely reset
6986 * and only object identify will remain.
Leon Clarkef7060e22010-06-03 12:02:55 +01006987 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006988 static Local<Context> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006989 Isolate* isolate, ExtensionConfiguration* extensions = NULL,
6990 Local<ObjectTemplate> global_template = Local<ObjectTemplate>(),
6991 Local<Value> global_object = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00006992
Steve Blocka7e24c12009-10-30 11:49:00 +00006993 /**
6994 * Sets the security token for the context. To access an object in
6995 * another context, the security tokens must match.
6996 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006997 void SetSecurityToken(Local<Value> token);
Steve Blocka7e24c12009-10-30 11:49:00 +00006998
6999 /** Restores the security token to the default value. */
7000 void UseDefaultSecurityToken();
7001
7002 /** Returns the security token of this context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007003 Local<Value> GetSecurityToken();
Steve Blocka7e24c12009-10-30 11:49:00 +00007004
7005 /**
7006 * Enter this context. After entering a context, all code compiled
7007 * and run is compiled and run in this context. If another context
7008 * is already entered, this old context is saved so it can be
7009 * restored when the new context is exited.
7010 */
7011 void Enter();
7012
7013 /**
7014 * Exit this context. Exiting the current context restores the
7015 * context that was in place when entering the current context.
7016 */
7017 void Exit();
7018
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007019 /** Returns an isolate associated with a current context. */
7020 v8::Isolate* GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007021
7022 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007023 * The field at kDebugIdIndex is reserved for V8 debugger implementation.
7024 * The value is propagated to the scripts compiled in given Context and
7025 * can be used for filtering scripts.
7026 */
7027 enum EmbedderDataFields { kDebugIdIndex = 0 };
7028
7029 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007030 * Gets the embedder data with the given index, which must have been set by a
7031 * previous call to SetEmbedderData with the same index. Note that index 0
7032 * currently has a special meaning for Chrome's debugger.
Steve Blocka7e24c12009-10-30 11:49:00 +00007033 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007034 V8_INLINE Local<Value> GetEmbedderData(int index);
7035
7036 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007037 * Gets the binding object used by V8 extras. Extra natives get a reference
7038 * to this object and can use it to "export" functionality by adding
7039 * properties. Extra natives can also "import" functionality by accessing
7040 * properties added by the embedder using the V8 API.
7041 */
7042 Local<Object> GetExtrasBindingObject();
7043
7044 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007045 * Sets the embedder data with the given index, growing the data as
7046 * needed. Note that index 0 currently has a special meaning for Chrome's
7047 * debugger.
7048 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007049 void SetEmbedderData(int index, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007050
7051 /**
7052 * Gets a 2-byte-aligned native pointer from the embedder data with the given
7053 * index, which must have bees set by a previous call to
7054 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
7055 * currently has a special meaning for Chrome's debugger.
7056 */
7057 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
7058
7059 /**
7060 * Sets a 2-byte-aligned native pointer in the embedder data with the given
7061 * index, growing the data as needed. Note that index 0 currently has a
7062 * special meaning for Chrome's debugger.
7063 */
7064 void SetAlignedPointerInEmbedderData(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007065
7066 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00007067 * Control whether code generation from strings is allowed. Calling
7068 * this method with false will disable 'eval' and the 'Function'
7069 * constructor for code running in this context. If 'eval' or the
7070 * 'Function' constructor are used an exception will be thrown.
7071 *
7072 * If code generation from strings is not allowed the
7073 * V8::AllowCodeGenerationFromStrings callback will be invoked if
7074 * set before blocking the call to 'eval' or the 'Function'
7075 * constructor. If that callback returns true, the call will be
7076 * allowed, otherwise an exception will be thrown. If no callback is
7077 * set an exception will be thrown.
7078 */
7079 void AllowCodeGenerationFromStrings(bool allow);
7080
7081 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007082 * Returns true if code generation from strings is allowed for the context.
7083 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
7084 */
7085 bool IsCodeGenerationFromStringsAllowed();
7086
7087 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007088 * Sets the error description for the exception that is thrown when
7089 * code generation from strings is not allowed and 'eval' or the 'Function'
7090 * constructor are called.
7091 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007092 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
7093
7094 /**
7095 * Estimate the memory in bytes retained by this context.
7096 */
7097 size_t EstimatedSize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007098
7099 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00007100 * Stack-allocated class which sets the execution context for all
7101 * operations executed within a local scope.
7102 */
Steve Block8defd9f2010-07-08 12:39:36 +01007103 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00007104 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007105 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007106 context_->Enter();
7107 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007108 V8_INLINE ~Scope() { context_->Exit(); }
7109
Steve Blocka7e24c12009-10-30 11:49:00 +00007110 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007111 Local<Context> context_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007112 };
7113
7114 private:
7115 friend class Value;
7116 friend class Script;
7117 friend class Object;
7118 friend class Function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007119
7120 Local<Value> SlowGetEmbedderData(int index);
7121 void* SlowGetAlignedPointerFromEmbedderData(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007122};
7123
7124
7125/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007126 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
7127 * to use any given V8 isolate, see the comments in the Isolate class. The
7128 * definition of 'using a V8 isolate' includes accessing handles or holding onto
7129 * object pointers obtained from V8 handles while in the particular V8 isolate.
7130 * It is up to the user of V8 to ensure, perhaps with locking, that this
7131 * constraint is not violated. In addition to any other synchronization
7132 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
7133 * used to signal thead switches to V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00007134 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007135 * v8::Locker is a scoped lock object. While it's active, i.e. between its
7136 * construction and destruction, the current thread is allowed to use the locked
7137 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
7138 * any time. In other words, the scope of a v8::Locker is a critical section.
Steve Block44f0eee2011-05-26 01:26:41 +01007139 *
Ben Murdoch257744e2011-11-30 15:57:28 +00007140 * Sample usage:
7141* \code
Steve Blocka7e24c12009-10-30 11:49:00 +00007142 * ...
7143 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007144 * v8::Locker locker(isolate);
7145 * v8::Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007146 * ...
Ben Murdoch257744e2011-11-30 15:57:28 +00007147 * // Code using V8 and isolate goes here.
Steve Blocka7e24c12009-10-30 11:49:00 +00007148 * ...
7149 * } // Destructor called here
7150 * \endcode
7151 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007152 * If you wish to stop using V8 in a thread A you can do this either by
7153 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
7154 * object:
Steve Blocka7e24c12009-10-30 11:49:00 +00007155 *
7156 * \code
7157 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007158 * isolate->Exit();
7159 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007160 * ...
7161 * // Code not using V8 goes here while V8 can run in another thread.
7162 * ...
7163 * } // Destructor called here.
Ben Murdoch257744e2011-11-30 15:57:28 +00007164 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007165 * \endcode
7166 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007167 * The Unlocker object is intended for use in a long-running callback from V8,
7168 * where you want to release the V8 lock for other threads to use.
Steve Blocka7e24c12009-10-30 11:49:00 +00007169 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007170 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
7171 * given thread. This can be useful if you have code that can be called either
7172 * from code that holds the lock or from code that does not. The Unlocker is
7173 * not recursive so you can not have several Unlockers on the stack at once, and
7174 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00007175 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007176 * An unlocker will unlock several lockers if it has to and reinstate the
7177 * correct depth of locking on its destruction, e.g.:
Steve Blocka7e24c12009-10-30 11:49:00 +00007178 *
7179 * \code
7180 * // V8 not locked.
7181 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007182 * v8::Locker locker(isolate);
7183 * Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007184 * // V8 locked.
7185 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007186 * v8::Locker another_locker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007187 * // V8 still locked (2 levels).
7188 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007189 * isolate->Exit();
7190 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007191 * // V8 not locked.
7192 * }
Ben Murdoch257744e2011-11-30 15:57:28 +00007193 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007194 * // V8 locked again (2 levels).
7195 * }
7196 * // V8 still locked (1 level).
7197 * }
7198 * // V8 Now no longer locked.
7199 * \endcode
7200 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007201class V8_EXPORT Unlocker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007202 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007203 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007204 * Initialize Unlocker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007205 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007206 V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
7207
Steve Blocka7e24c12009-10-30 11:49:00 +00007208 ~Unlocker();
Ben Murdoch257744e2011-11-30 15:57:28 +00007209 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007210 void Initialize(Isolate* isolate);
7211
Ben Murdoch257744e2011-11-30 15:57:28 +00007212 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007213};
7214
7215
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007216class V8_EXPORT Locker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007217 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007218 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007219 * Initialize Locker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007220 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007221 V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
7222
Steve Blocka7e24c12009-10-30 11:49:00 +00007223 ~Locker();
7224
7225 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007226 * Returns whether or not the locker for a given isolate, is locked by the
7227 * current thread.
Steve Blocka7e24c12009-10-30 11:49:00 +00007228 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007229 static bool IsLocked(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007230
7231 /**
7232 * Returns whether v8::Locker is being used by this V8 instance.
7233 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007234 static bool IsActive();
Steve Blocka7e24c12009-10-30 11:49:00 +00007235
7236 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007237 void Initialize(Isolate* isolate);
7238
Steve Blocka7e24c12009-10-30 11:49:00 +00007239 bool has_lock_;
7240 bool top_level_;
Ben Murdoch257744e2011-11-30 15:57:28 +00007241 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007242
Steve Blocka7e24c12009-10-30 11:49:00 +00007243 // Disallow copying and assigning.
7244 Locker(const Locker&);
7245 void operator=(const Locker&);
7246};
7247
7248
Ben Murdoch257744e2011-11-30 15:57:28 +00007249// --- Implementation ---
Steve Blocka7e24c12009-10-30 11:49:00 +00007250
7251
7252namespace internal {
7253
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007254const int kApiPointerSize = sizeof(void*); // NOLINT
7255const int kApiIntSize = sizeof(int); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007256const int kApiInt64Size = sizeof(int64_t); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00007257
7258// Tag information for HeapObject.
7259const int kHeapObjectTag = 1;
7260const int kHeapObjectTagSize = 2;
7261const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
7262
Steve Blocka7e24c12009-10-30 11:49:00 +00007263// Tag information for Smi.
7264const int kSmiTag = 0;
7265const int kSmiTagSize = 1;
7266const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
7267
Ben Murdochb8e0da22011-05-16 14:20:40 +01007268template <size_t ptr_size> struct SmiTagging;
Steve Block3ce2e202009-11-05 08:53:23 +00007269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007270template<int kSmiShiftSize>
7271V8_INLINE internal::Object* IntToSmi(int value) {
7272 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
7273 uintptr_t tagged_value =
7274 (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag;
7275 return reinterpret_cast<internal::Object*>(tagged_value);
7276}
7277
Steve Block3ce2e202009-11-05 08:53:23 +00007278// Smi constants for 32-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007279template <> struct SmiTagging<4> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007280 enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
7281 static int SmiShiftSize() { return kSmiShiftSize; }
7282 static int SmiValueSize() { return kSmiValueSize; }
7283 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007284 int shift_bits = kSmiTagSize + kSmiShiftSize;
7285 // Throw away top 32 bits and shift down (requires >> to be sign extending).
7286 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
7287 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007288 V8_INLINE static internal::Object* IntToSmi(int value) {
7289 return internal::IntToSmi<kSmiShiftSize>(value);
7290 }
7291 V8_INLINE static bool IsValidSmi(intptr_t value) {
7292 // To be representable as an tagged small integer, the two
7293 // most-significant bits of 'value' must be either 00 or 11 due to
7294 // sign-extension. To check this we add 01 to the two
7295 // most-significant bits, and check if the most-significant bit is 0
7296 //
7297 // CAUTION: The original code below:
7298 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
7299 // may lead to incorrect results according to the C language spec, and
7300 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
7301 // compiler may produce undefined results in case of signed integer
7302 // overflow. The computation must be done w/ unsigned ints.
7303 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
7304 }
Steve Block3ce2e202009-11-05 08:53:23 +00007305};
7306
7307// Smi constants for 64-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007308template <> struct SmiTagging<8> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007309 enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
7310 static int SmiShiftSize() { return kSmiShiftSize; }
7311 static int SmiValueSize() { return kSmiValueSize; }
7312 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007313 int shift_bits = kSmiTagSize + kSmiShiftSize;
7314 // Shift down and throw away top 32 bits.
7315 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
7316 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007317 V8_INLINE static internal::Object* IntToSmi(int value) {
7318 return internal::IntToSmi<kSmiShiftSize>(value);
7319 }
7320 V8_INLINE static bool IsValidSmi(intptr_t value) {
7321 // To be representable as a long smi, the value must be a 32-bit integer.
7322 return (value == static_cast<int32_t>(value));
7323 }
Steve Block3ce2e202009-11-05 08:53:23 +00007324};
7325
Ben Murdochb8e0da22011-05-16 14:20:40 +01007326typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
7327const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
7328const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007329V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
7330V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
Steve Blockd0582a62009-12-15 09:54:21 +00007331
Steve Blocka7e24c12009-10-30 11:49:00 +00007332/**
7333 * This class exports constants and functionality from within v8 that
7334 * is necessary to implement inline functions in the v8 api. Don't
7335 * depend on functions and constants defined here.
7336 */
7337class Internals {
7338 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00007339 // These values match non-compiler-dependent values defined within
7340 // the implementation of v8.
7341 static const int kHeapObjectMapOffset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007342 static const int kMapInstanceTypeAndBitFieldOffset =
7343 1 * kApiPointerSize + kApiIntSize;
7344 static const int kStringResourceOffset = 3 * kApiPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00007345
Ben Murdochc5610432016-08-08 18:44:38 +01007346 static const int kOddballKindOffset = 5 * kApiPointerSize + sizeof(double);
Ben Murdoch257744e2011-11-30 15:57:28 +00007347 static const int kForeignAddressOffset = kApiPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007348 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007349 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
7350 static const int kContextHeaderSize = 2 * kApiPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007351 static const int kContextEmbedderDataIndex = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00007352 static const int kFullStringRepresentationMask = 0x07;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007353 static const int kStringEncodingMask = 0x4;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007354 static const int kExternalTwoByteRepresentationTag = 0x02;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007355 static const int kExternalOneByteRepresentationTag = 0x06;
Steve Blocka7e24c12009-10-30 11:49:00 +00007356
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007357 static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize;
7358 static const int kAmountOfExternalAllocatedMemoryOffset =
7359 4 * kApiPointerSize;
7360 static const int kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset =
7361 kAmountOfExternalAllocatedMemoryOffset + kApiInt64Size;
7362 static const int kIsolateRootsOffset =
7363 kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size +
7364 kApiPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01007365 static const int kUndefinedValueRootIndex = 4;
7366 static const int kTheHoleValueRootIndex = 5;
7367 static const int kNullValueRootIndex = 6;
7368 static const int kTrueValueRootIndex = 7;
7369 static const int kFalseValueRootIndex = 8;
7370 static const int kEmptyStringRootIndex = 9;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007371
7372 // The external allocation limit should be below 256 MB on all architectures
7373 // to avoid that resource-constrained embedders run low on memory.
7374 static const int kExternalAllocationLimit = 192 * 1024 * 1024;
7375
7376 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
7377 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007378 static const int kNodeStateMask = 0x7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007379 static const int kNodeStateIsWeakValue = 2;
7380 static const int kNodeStateIsPendingValue = 3;
7381 static const int kNodeStateIsNearDeathValue = 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007382 static const int kNodeIsIndependentShift = 3;
7383 static const int kNodeIsPartiallyDependentShift = 4;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007384 static const int kNodeIsActiveShift = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007385
Ben Murdochc5610432016-08-08 18:44:38 +01007386 static const int kJSObjectType = 0xb7;
7387 static const int kJSApiObjectType = 0xb6;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01007388 static const int kFirstNonstringType = 0x80;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007389 static const int kOddballType = 0x83;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007390 static const int kForeignType = 0x87;
Steve Blocka7e24c12009-10-30 11:49:00 +00007391
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007392 static const int kUndefinedOddballKind = 5;
7393 static const int kNullOddballKind = 3;
7394
7395 static const uint32_t kNumIsolateDataSlots = 4;
7396
7397 V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
7398 V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
7399#ifdef V8_ENABLE_CHECKS
7400 CheckInitializedImpl(isolate);
7401#endif
7402 }
7403
7404 V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007405 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
7406 kHeapObjectTag);
7407 }
7408
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007409 V8_INLINE static int SmiValue(const internal::Object* value) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01007410 return PlatformSmiTagging::SmiToInt(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007411 }
7412
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007413 V8_INLINE static internal::Object* IntToSmi(int value) {
7414 return PlatformSmiTagging::IntToSmi(value);
7415 }
7416
7417 V8_INLINE static bool IsValidSmi(intptr_t value) {
7418 return PlatformSmiTagging::IsValidSmi(value);
7419 }
7420
7421 V8_INLINE static int GetInstanceType(const internal::Object* obj) {
Steve Block3ce2e202009-11-05 08:53:23 +00007422 typedef internal::Object O;
7423 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007424 // Map::InstanceType is defined so that it will always be loaded into
7425 // the LS 8 bits of one 16-bit word, regardless of endianess.
7426 return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff;
Steve Block3ce2e202009-11-05 08:53:23 +00007427 }
7428
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007429 V8_INLINE static int GetOddballKind(const internal::Object* obj) {
7430 typedef internal::Object O;
7431 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01007432 }
7433
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007434 V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007435 int representation = (instance_type & kFullStringRepresentationMask);
7436 return representation == kExternalTwoByteRepresentationTag;
7437 }
7438
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007439 V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) {
7440 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7441 return *addr & static_cast<uint8_t>(1U << shift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007442 }
Steve Block44f0eee2011-05-26 01:26:41 +01007443
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007444 V8_INLINE static void UpdateNodeFlag(internal::Object** obj,
7445 bool value, int shift) {
7446 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7447 uint8_t mask = static_cast<uint8_t>(1U << shift);
7448 *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
7449 }
7450
7451 V8_INLINE static uint8_t GetNodeState(internal::Object** obj) {
7452 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7453 return *addr & kNodeStateMask;
7454 }
7455
7456 V8_INLINE static void UpdateNodeState(internal::Object** obj,
7457 uint8_t value) {
7458 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7459 *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
7460 }
7461
7462 V8_INLINE static void SetEmbedderData(v8::Isolate* isolate,
7463 uint32_t slot,
7464 void* data) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007465 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007466 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7467 *reinterpret_cast<void**>(addr) = data;
7468 }
7469
7470 V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
7471 uint32_t slot) {
7472 const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) +
7473 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7474 return *reinterpret_cast<void* const*>(addr);
7475 }
7476
7477 V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate,
7478 int index) {
7479 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
7480 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
7481 }
7482
7483 template <typename T>
7484 V8_INLINE static T ReadField(const internal::Object* ptr, int offset) {
7485 const uint8_t* addr =
7486 reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag;
7487 return *reinterpret_cast<const T*>(addr);
7488 }
7489
7490 template <typename T>
7491 V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) {
7492 typedef internal::Object O;
7493 typedef internal::Internals I;
7494 O* ctx = *reinterpret_cast<O* const*>(context);
7495 int embedder_data_offset = I::kContextHeaderSize +
7496 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
7497 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
7498 int value_offset =
7499 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
7500 return I::ReadField<T>(embedder_data, value_offset);
7501 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007502};
7503
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007504} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +00007505
7506
7507template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007508Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007509 return New(isolate, that.val_);
7510}
7511
7512template <class T>
7513Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
7514 return New(isolate, that.val_);
7515}
7516
Steve Blocka7e24c12009-10-30 11:49:00 +00007517
7518template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007519Local<T> Local<T>::New(Isolate* isolate, T* that) {
7520 if (that == NULL) return Local<T>();
7521 T* that_ptr = that;
7522 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
7523 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
7524 reinterpret_cast<internal::Isolate*>(isolate), *p)));
7525}
7526
7527
7528template<class T>
7529template<class S>
7530void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
7531 TYPE_CHECK(T, S);
7532 V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
7533}
7534
7535
7536template<class T>
7537Local<T> Eternal<T>::Get(Isolate* isolate) {
7538 return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
Steve Blocka7e24c12009-10-30 11:49:00 +00007539}
7540
7541
7542template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007543Local<T> MaybeLocal<T>::ToLocalChecked() {
7544 if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty();
7545 return Local<T>(val_);
7546}
7547
7548
7549template <class T>
7550void* WeakCallbackInfo<T>::GetInternalField(int index) const {
7551#ifdef V8_ENABLE_CHECKS
7552 if (index < 0 || index >= kInternalFieldsInWeakCallback) {
7553 V8::InternalFieldOutOfBounds(index);
7554 }
7555#endif
7556 return internal_fields_[index];
7557}
7558
7559
7560template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007561T* PersistentBase<T>::New(Isolate* isolate, T* that) {
7562 if (that == NULL) return NULL;
7563 internal::Object** p = reinterpret_cast<internal::Object**>(that);
7564 return reinterpret_cast<T*>(
7565 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
7566 p));
7567}
7568
7569
7570template <class T, class M>
7571template <class S, class M2>
7572void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
7573 TYPE_CHECK(T, S);
7574 this->Reset();
7575 if (that.IsEmpty()) return;
7576 internal::Object** p = reinterpret_cast<internal::Object**>(that.val_);
7577 this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p));
7578 M::Copy(that, this);
7579}
7580
7581
7582template <class T>
7583bool PersistentBase<T>::IsIndependent() const {
7584 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007585 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007586 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7587 I::kNodeIsIndependentShift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007588}
7589
7590
7591template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007592bool PersistentBase<T>::IsNearDeath() const {
7593 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007594 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007595 uint8_t node_state =
7596 I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_));
7597 return node_state == I::kNodeStateIsNearDeathValue ||
7598 node_state == I::kNodeStateIsPendingValue;
Steve Blocka7e24c12009-10-30 11:49:00 +00007599}
7600
7601
7602template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007603bool PersistentBase<T>::IsWeak() const {
7604 typedef internal::Internals I;
7605 if (this->IsEmpty()) return false;
7606 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
7607 I::kNodeStateIsWeakValue;
7608}
7609
7610
7611template <class T>
7612void PersistentBase<T>::Reset() {
Steve Blocka7e24c12009-10-30 11:49:00 +00007613 if (this->IsEmpty()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007614 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
7615 val_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00007616}
7617
7618
7619template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007620template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007621void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007622 TYPE_CHECK(T, S);
7623 Reset();
7624 if (other.IsEmpty()) return;
7625 this->val_ = New(isolate, other.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007626}
7627
Steve Blocka7e24c12009-10-30 11:49:00 +00007628
Steve Block44f0eee2011-05-26 01:26:41 +01007629template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007630template <class S>
7631void PersistentBase<T>::Reset(Isolate* isolate,
7632 const PersistentBase<S>& other) {
7633 TYPE_CHECK(T, S);
7634 Reset();
7635 if (other.IsEmpty()) return;
7636 this->val_ = New(isolate, other.val_);
Ben Murdoch257744e2011-11-30 15:57:28 +00007637}
7638
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007639
Ben Murdoch257744e2011-11-30 15:57:28 +00007640template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007641template <typename P>
7642V8_INLINE void PersistentBase<T>::SetWeak(
7643 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7644 WeakCallbackType type) {
7645 typedef typename WeakCallbackInfo<void>::Callback Callback;
7646 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7647 reinterpret_cast<Callback>(callback), type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007648}
7649
Ben Murdochc5610432016-08-08 18:44:38 +01007650template <class T>
7651void PersistentBase<T>::SetWeak() {
7652 V8::MakeWeak(reinterpret_cast<internal::Object***>(&this->val_));
7653}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007654
7655template <class T>
7656template <typename P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007657P* PersistentBase<T>::ClearWeak() {
7658 return reinterpret_cast<P*>(
7659 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
7660}
7661
Ben Murdochda12d292016-06-02 14:46:10 +01007662template <class T>
Ben Murdochc5610432016-08-08 18:44:38 +01007663void PersistentBase<T>::RegisterExternalReference(Isolate* isolate) const {
Ben Murdochda12d292016-06-02 14:46:10 +01007664 if (IsEmpty()) return;
7665 V8::RegisterExternallyReferencedObject(
7666 reinterpret_cast<internal::Object**>(this->val_),
7667 reinterpret_cast<internal::Isolate*>(isolate));
7668}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007669
7670template <class T>
7671void PersistentBase<T>::MarkIndependent() {
7672 typedef internal::Internals I;
7673 if (this->IsEmpty()) return;
7674 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7675 true,
7676 I::kNodeIsIndependentShift);
7677}
7678
7679
7680template <class T>
7681void PersistentBase<T>::MarkPartiallyDependent() {
7682 typedef internal::Internals I;
7683 if (this->IsEmpty()) return;
7684 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7685 true,
7686 I::kNodeIsPartiallyDependentShift);
7687}
7688
7689
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007690template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007691void PersistentBase<T>::MarkActive() {
7692 typedef internal::Internals I;
7693 if (this->IsEmpty()) return;
7694 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true,
7695 I::kNodeIsActiveShift);
7696}
7697
7698
7699template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007700void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) {
7701 typedef internal::Internals I;
7702 if (this->IsEmpty()) return;
7703 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7704 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7705 *reinterpret_cast<uint16_t*>(addr) = class_id;
7706}
7707
7708
7709template <class T>
7710uint16_t PersistentBase<T>::WrapperClassId() const {
7711 typedef internal::Internals I;
7712 if (this->IsEmpty()) return 0;
7713 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7714 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7715 return *reinterpret_cast<uint16_t*>(addr);
7716}
7717
7718
7719template<typename T>
7720ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
7721
7722template<typename T>
7723template<typename S>
7724void ReturnValue<T>::Set(const Persistent<S>& handle) {
7725 TYPE_CHECK(T, S);
7726 if (V8_UNLIKELY(handle.IsEmpty())) {
7727 *value_ = GetDefaultValue();
7728 } else {
7729 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7730 }
7731}
7732
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007733template <typename T>
7734template <typename S>
7735void ReturnValue<T>::Set(const Global<S>& handle) {
7736 TYPE_CHECK(T, S);
7737 if (V8_UNLIKELY(handle.IsEmpty())) {
7738 *value_ = GetDefaultValue();
7739 } else {
7740 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7741 }
7742}
7743
7744template <typename T>
7745template <typename S>
7746void ReturnValue<T>::Set(const Local<S> handle) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007747 TYPE_CHECK(T, S);
7748 if (V8_UNLIKELY(handle.IsEmpty())) {
7749 *value_ = GetDefaultValue();
7750 } else {
7751 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7752 }
7753}
7754
7755template<typename T>
7756void ReturnValue<T>::Set(double i) {
7757 TYPE_CHECK(T, Number);
7758 Set(Number::New(GetIsolate(), i));
7759}
7760
7761template<typename T>
7762void ReturnValue<T>::Set(int32_t i) {
7763 TYPE_CHECK(T, Integer);
7764 typedef internal::Internals I;
7765 if (V8_LIKELY(I::IsValidSmi(i))) {
7766 *value_ = I::IntToSmi(i);
7767 return;
7768 }
7769 Set(Integer::New(GetIsolate(), i));
7770}
7771
7772template<typename T>
7773void ReturnValue<T>::Set(uint32_t i) {
7774 TYPE_CHECK(T, Integer);
7775 // Can't simply use INT32_MAX here for whatever reason.
7776 bool fits_into_int32_t = (i & (1U << 31)) == 0;
7777 if (V8_LIKELY(fits_into_int32_t)) {
7778 Set(static_cast<int32_t>(i));
7779 return;
7780 }
7781 Set(Integer::NewFromUnsigned(GetIsolate(), i));
7782}
7783
7784template<typename T>
7785void ReturnValue<T>::Set(bool value) {
7786 TYPE_CHECK(T, Boolean);
7787 typedef internal::Internals I;
7788 int root_index;
7789 if (value) {
7790 root_index = I::kTrueValueRootIndex;
7791 } else {
7792 root_index = I::kFalseValueRootIndex;
7793 }
7794 *value_ = *I::GetRoot(GetIsolate(), root_index);
7795}
7796
7797template<typename T>
7798void ReturnValue<T>::SetNull() {
7799 TYPE_CHECK(T, Primitive);
7800 typedef internal::Internals I;
7801 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
7802}
7803
7804template<typename T>
7805void ReturnValue<T>::SetUndefined() {
7806 TYPE_CHECK(T, Primitive);
7807 typedef internal::Internals I;
7808 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
7809}
7810
7811template<typename T>
7812void ReturnValue<T>::SetEmptyString() {
7813 TYPE_CHECK(T, String);
7814 typedef internal::Internals I;
7815 *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
7816}
7817
Ben Murdochda12d292016-06-02 14:46:10 +01007818template <typename T>
7819Isolate* ReturnValue<T>::GetIsolate() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007820 // Isolate is always the pointer below the default value on the stack.
7821 return *reinterpret_cast<Isolate**>(&value_[-2]);
7822}
7823
Ben Murdochda12d292016-06-02 14:46:10 +01007824template <typename T>
7825Local<Value> ReturnValue<T>::Get() const {
7826 typedef internal::Internals I;
7827 if (*value_ == *I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex))
7828 return Local<Value>(*Undefined(GetIsolate()));
7829 return Local<Value>::New(GetIsolate(), reinterpret_cast<Value*>(value_));
7830}
7831
7832template <typename T>
7833template <typename S>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007834void ReturnValue<T>::Set(S* whatever) {
7835 // Uncompilable to prevent inadvertent misuse.
7836 TYPE_CHECK(S*, Primitive);
7837}
7838
7839template<typename T>
7840internal::Object* ReturnValue<T>::GetDefaultValue() {
7841 // Default value is always the pointer below value_ on the stack.
7842 return value_[-1];
7843}
7844
Ben Murdochc5610432016-08-08 18:44:38 +01007845template <typename T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007846FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
7847 internal::Object** values,
Ben Murdochc5610432016-08-08 18:44:38 +01007848 int length)
7849 : implicit_args_(implicit_args), values_(values), length_(length) {}
Steve Block8defd9f2010-07-08 12:39:36 +01007850
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007851template<typename T>
7852Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
7853 if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +00007854 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
7855}
7856
7857
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007858template<typename T>
7859Local<Function> FunctionCallbackInfo<T>::Callee() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007860 return Local<Function>(reinterpret_cast<Function*>(
7861 &implicit_args_[kCalleeIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007862}
7863
7864
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007865template<typename T>
7866Local<Object> FunctionCallbackInfo<T>::This() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007867 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
7868}
7869
7870
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007871template<typename T>
7872Local<Object> FunctionCallbackInfo<T>::Holder() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007873 return Local<Object>(reinterpret_cast<Object*>(
7874 &implicit_args_[kHolderIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007875}
7876
Ben Murdochc5610432016-08-08 18:44:38 +01007877template <typename T>
7878Local<Value> FunctionCallbackInfo<T>::NewTarget() const {
7879 return Local<Value>(
7880 reinterpret_cast<Value*>(&implicit_args_[kNewTargetIndex]));
7881}
Steve Blocka7e24c12009-10-30 11:49:00 +00007882
Ben Murdochc5610432016-08-08 18:44:38 +01007883template <typename T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007884Local<Value> FunctionCallbackInfo<T>::Data() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007885 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007886}
7887
7888
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007889template<typename T>
7890Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
7891 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
7892}
7893
7894
7895template<typename T>
7896ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
7897 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
7898}
7899
7900
7901template<typename T>
7902bool FunctionCallbackInfo<T>::IsConstructCall() const {
Ben Murdochc5610432016-08-08 18:44:38 +01007903 return !NewTarget()->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +00007904}
7905
7906
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007907template<typename T>
7908int FunctionCallbackInfo<T>::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007909 return length_;
7910}
7911
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007912ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
7913 Local<Integer> resource_line_offset,
7914 Local<Integer> resource_column_offset,
7915 Local<Boolean> resource_is_shared_cross_origin,
7916 Local<Integer> script_id,
7917 Local<Boolean> resource_is_embedder_debug_script,
7918 Local<Value> source_map_url,
7919 Local<Boolean> resource_is_opaque)
7920 : resource_name_(resource_name),
7921 resource_line_offset_(resource_line_offset),
7922 resource_column_offset_(resource_column_offset),
7923 options_(!resource_is_embedder_debug_script.IsEmpty() &&
7924 resource_is_embedder_debug_script->IsTrue(),
7925 !resource_is_shared_cross_origin.IsEmpty() &&
7926 resource_is_shared_cross_origin->IsTrue(),
7927 !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()),
7928 script_id_(script_id),
7929 source_map_url_(source_map_url) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00007930
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007931Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007932
7933
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007934Local<Integer> ScriptOrigin::ResourceLineOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007935 return resource_line_offset_;
7936}
7937
7938
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007939Local<Integer> ScriptOrigin::ResourceColumnOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007940 return resource_column_offset_;
7941}
7942
7943
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007944Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007945
7946
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007947Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007948
7949
7950ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
7951 CachedData* data)
7952 : source_string(string),
7953 resource_name(origin.ResourceName()),
7954 resource_line_offset(origin.ResourceLineOffset()),
7955 resource_column_offset(origin.ResourceColumnOffset()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007956 resource_options(origin.Options()),
7957 source_map_url(origin.SourceMapUrl()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007958 cached_data(data) {}
7959
7960
7961ScriptCompiler::Source::Source(Local<String> string,
7962 CachedData* data)
7963 : source_string(string), cached_data(data) {}
7964
7965
7966ScriptCompiler::Source::~Source() {
7967 delete cached_data;
7968}
7969
7970
7971const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
7972 const {
7973 return cached_data;
7974}
7975
7976
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007977Local<Boolean> Boolean::New(Isolate* isolate, bool value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007978 return value ? True(isolate) : False(isolate);
7979}
7980
7981
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007982void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) {
7983 Set(v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
7984 .ToLocalChecked(),
7985 value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007986}
7987
7988
7989Local<Value> Object::GetInternalField(int index) {
7990#ifndef V8_ENABLE_CHECKS
Steve Blocka7e24c12009-10-30 11:49:00 +00007991 typedef internal::Object O;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007992 typedef internal::HeapObject HO;
Steve Blocka7e24c12009-10-30 11:49:00 +00007993 typedef internal::Internals I;
7994 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007995 // Fast path: If the object is a plain JSObject, which is the common case, we
7996 // know where to find the internal fields and can return the value directly.
Ben Murdochc5610432016-08-08 18:44:38 +01007997 auto instance_type = I::GetInstanceType(obj);
7998 if (instance_type == I::kJSObjectType ||
7999 instance_type == I::kJSApiObjectType) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008000 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008001 O* value = I::ReadField<O*>(obj, offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008002 O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008003 return Local<Value>(reinterpret_cast<Value*>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00008004 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008005#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008006 return SlowGetInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008007}
8008
8009
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008010void* Object::GetAlignedPointerFromInternalField(int index) {
8011#ifndef V8_ENABLE_CHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00008012 typedef internal::Object O;
8013 typedef internal::Internals I;
Steve Block3ce2e202009-11-05 08:53:23 +00008014 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008015 // Fast path: If the object is a plain JSObject, which is the common case, we
8016 // know where to find the internal fields and can return the value directly.
Ben Murdochc5610432016-08-08 18:44:38 +01008017 auto instance_type = I::GetInstanceType(obj);
8018 if (V8_LIKELY(instance_type == I::kJSObjectType ||
8019 instance_type == I::kJSApiObjectType)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008020 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008021 return I::ReadField<void*>(obj, offset);
Steve Block3ce2e202009-11-05 08:53:23 +00008022 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008023#endif
8024 return SlowGetAlignedPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008025}
8026
8027
8028String* String::Cast(v8::Value* value) {
8029#ifdef V8_ENABLE_CHECKS
8030 CheckCast(value);
8031#endif
8032 return static_cast<String*>(value);
8033}
8034
8035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008036Local<String> String::Empty(Isolate* isolate) {
8037 typedef internal::Object* S;
8038 typedef internal::Internals I;
8039 I::CheckInitialized(isolate);
8040 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
8041 return Local<String>(reinterpret_cast<String*>(slot));
8042}
8043
8044
Steve Blocka7e24c12009-10-30 11:49:00 +00008045String::ExternalStringResource* String::GetExternalStringResource() const {
8046 typedef internal::Object O;
8047 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008048 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008049 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00008050 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008051 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8052 result = reinterpret_cast<String::ExternalStringResource*>(value);
8053 } else {
8054 result = NULL;
8055 }
8056#ifdef V8_ENABLE_CHECKS
8057 VerifyExternalStringResource(result);
8058#endif
8059 return result;
8060}
8061
8062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008063String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
8064 String::Encoding* encoding_out) const {
8065 typedef internal::Object O;
8066 typedef internal::Internals I;
8067 O* obj = *reinterpret_cast<O* const*>(this);
8068 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
8069 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
8070 ExternalStringResourceBase* resource = NULL;
8071 if (type == I::kExternalOneByteRepresentationTag ||
8072 type == I::kExternalTwoByteRepresentationTag) {
8073 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8074 resource = static_cast<ExternalStringResourceBase*>(value);
8075 }
8076#ifdef V8_ENABLE_CHECKS
8077 VerifyExternalStringResourceBase(resource, *encoding_out);
8078#endif
8079 return resource;
8080}
8081
8082
8083bool Value::IsUndefined() const {
8084#ifdef V8_ENABLE_CHECKS
8085 return FullIsUndefined();
8086#else
8087 return QuickIsUndefined();
8088#endif
8089}
8090
8091bool Value::QuickIsUndefined() const {
8092 typedef internal::Object O;
8093 typedef internal::Internals I;
8094 O* obj = *reinterpret_cast<O* const*>(this);
8095 if (!I::HasHeapObjectTag(obj)) return false;
8096 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8097 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
8098}
8099
8100
8101bool Value::IsNull() const {
8102#ifdef V8_ENABLE_CHECKS
8103 return FullIsNull();
8104#else
8105 return QuickIsNull();
8106#endif
8107}
8108
8109bool Value::QuickIsNull() const {
8110 typedef internal::Object O;
8111 typedef internal::Internals I;
8112 O* obj = *reinterpret_cast<O* const*>(this);
8113 if (!I::HasHeapObjectTag(obj)) return false;
8114 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8115 return (I::GetOddballKind(obj) == I::kNullOddballKind);
8116}
8117
8118
Steve Blocka7e24c12009-10-30 11:49:00 +00008119bool Value::IsString() const {
8120#ifdef V8_ENABLE_CHECKS
8121 return FullIsString();
8122#else
8123 return QuickIsString();
8124#endif
8125}
8126
8127bool Value::QuickIsString() const {
8128 typedef internal::Object O;
8129 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008130 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008131 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00008132 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00008133}
8134
8135
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008136template <class T> Value* Value::Cast(T* value) {
8137 return static_cast<Value*>(value);
8138}
8139
8140
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008141Local<Boolean> Value::ToBoolean() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008142 return ToBoolean(Isolate::GetCurrent()->GetCurrentContext())
8143 .FromMaybe(Local<Boolean>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008144}
8145
8146
8147Local<Number> Value::ToNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008148 return ToNumber(Isolate::GetCurrent()->GetCurrentContext())
8149 .FromMaybe(Local<Number>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008150}
8151
8152
8153Local<String> Value::ToString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008154 return ToString(Isolate::GetCurrent()->GetCurrentContext())
8155 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008156}
8157
8158
8159Local<String> Value::ToDetailString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008160 return ToDetailString(Isolate::GetCurrent()->GetCurrentContext())
8161 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008162}
8163
8164
8165Local<Object> Value::ToObject() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008166 return ToObject(Isolate::GetCurrent()->GetCurrentContext())
8167 .FromMaybe(Local<Object>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008168}
8169
8170
8171Local<Integer> Value::ToInteger() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008172 return ToInteger(Isolate::GetCurrent()->GetCurrentContext())
8173 .FromMaybe(Local<Integer>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008174}
8175
8176
8177Local<Uint32> Value::ToUint32() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008178 return ToUint32(Isolate::GetCurrent()->GetCurrentContext())
8179 .FromMaybe(Local<Uint32>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008180}
8181
8182
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008183Local<Int32> Value::ToInt32() const {
8184 return ToInt32(Isolate::GetCurrent()->GetCurrentContext())
8185 .FromMaybe(Local<Int32>());
8186}
8187
8188
8189Boolean* Boolean::Cast(v8::Value* value) {
8190#ifdef V8_ENABLE_CHECKS
8191 CheckCast(value);
8192#endif
8193 return static_cast<Boolean*>(value);
8194}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008195
8196
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008197Name* Name::Cast(v8::Value* value) {
8198#ifdef V8_ENABLE_CHECKS
8199 CheckCast(value);
8200#endif
8201 return static_cast<Name*>(value);
8202}
8203
8204
8205Symbol* Symbol::Cast(v8::Value* value) {
8206#ifdef V8_ENABLE_CHECKS
8207 CheckCast(value);
8208#endif
8209 return static_cast<Symbol*>(value);
8210}
8211
8212
Steve Blocka7e24c12009-10-30 11:49:00 +00008213Number* Number::Cast(v8::Value* value) {
8214#ifdef V8_ENABLE_CHECKS
8215 CheckCast(value);
8216#endif
8217 return static_cast<Number*>(value);
8218}
8219
8220
8221Integer* Integer::Cast(v8::Value* value) {
8222#ifdef V8_ENABLE_CHECKS
8223 CheckCast(value);
8224#endif
8225 return static_cast<Integer*>(value);
8226}
8227
8228
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008229Int32* Int32::Cast(v8::Value* value) {
8230#ifdef V8_ENABLE_CHECKS
8231 CheckCast(value);
8232#endif
8233 return static_cast<Int32*>(value);
8234}
8235
8236
8237Uint32* Uint32::Cast(v8::Value* value) {
8238#ifdef V8_ENABLE_CHECKS
8239 CheckCast(value);
8240#endif
8241 return static_cast<Uint32*>(value);
8242}
8243
8244
Steve Blocka7e24c12009-10-30 11:49:00 +00008245Date* Date::Cast(v8::Value* value) {
8246#ifdef V8_ENABLE_CHECKS
8247 CheckCast(value);
8248#endif
8249 return static_cast<Date*>(value);
8250}
8251
8252
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008253StringObject* StringObject::Cast(v8::Value* value) {
8254#ifdef V8_ENABLE_CHECKS
8255 CheckCast(value);
8256#endif
8257 return static_cast<StringObject*>(value);
8258}
8259
8260
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008261SymbolObject* SymbolObject::Cast(v8::Value* value) {
8262#ifdef V8_ENABLE_CHECKS
8263 CheckCast(value);
8264#endif
8265 return static_cast<SymbolObject*>(value);
8266}
8267
8268
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008269NumberObject* NumberObject::Cast(v8::Value* value) {
8270#ifdef V8_ENABLE_CHECKS
8271 CheckCast(value);
8272#endif
8273 return static_cast<NumberObject*>(value);
8274}
8275
8276
8277BooleanObject* BooleanObject::Cast(v8::Value* value) {
8278#ifdef V8_ENABLE_CHECKS
8279 CheckCast(value);
8280#endif
8281 return static_cast<BooleanObject*>(value);
8282}
8283
8284
Ben Murdochf87a2032010-10-22 12:50:53 +01008285RegExp* RegExp::Cast(v8::Value* value) {
8286#ifdef V8_ENABLE_CHECKS
8287 CheckCast(value);
8288#endif
8289 return static_cast<RegExp*>(value);
8290}
8291
8292
Steve Blocka7e24c12009-10-30 11:49:00 +00008293Object* Object::Cast(v8::Value* value) {
8294#ifdef V8_ENABLE_CHECKS
8295 CheckCast(value);
8296#endif
8297 return static_cast<Object*>(value);
8298}
8299
8300
8301Array* Array::Cast(v8::Value* value) {
8302#ifdef V8_ENABLE_CHECKS
8303 CheckCast(value);
8304#endif
8305 return static_cast<Array*>(value);
8306}
8307
8308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008309Map* Map::Cast(v8::Value* value) {
8310#ifdef V8_ENABLE_CHECKS
8311 CheckCast(value);
8312#endif
8313 return static_cast<Map*>(value);
8314}
8315
8316
8317Set* Set::Cast(v8::Value* value) {
8318#ifdef V8_ENABLE_CHECKS
8319 CheckCast(value);
8320#endif
8321 return static_cast<Set*>(value);
8322}
8323
8324
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008325Promise* Promise::Cast(v8::Value* value) {
8326#ifdef V8_ENABLE_CHECKS
8327 CheckCast(value);
8328#endif
8329 return static_cast<Promise*>(value);
8330}
8331
8332
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008333Proxy* Proxy::Cast(v8::Value* value) {
8334#ifdef V8_ENABLE_CHECKS
8335 CheckCast(value);
8336#endif
8337 return static_cast<Proxy*>(value);
8338}
8339
8340
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008341Promise::Resolver* Promise::Resolver::Cast(v8::Value* value) {
8342#ifdef V8_ENABLE_CHECKS
8343 CheckCast(value);
8344#endif
8345 return static_cast<Promise::Resolver*>(value);
8346}
8347
8348
8349ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
8350#ifdef V8_ENABLE_CHECKS
8351 CheckCast(value);
8352#endif
8353 return static_cast<ArrayBuffer*>(value);
8354}
8355
8356
8357ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) {
8358#ifdef V8_ENABLE_CHECKS
8359 CheckCast(value);
8360#endif
8361 return static_cast<ArrayBufferView*>(value);
8362}
8363
8364
8365TypedArray* TypedArray::Cast(v8::Value* value) {
8366#ifdef V8_ENABLE_CHECKS
8367 CheckCast(value);
8368#endif
8369 return static_cast<TypedArray*>(value);
8370}
8371
8372
8373Uint8Array* Uint8Array::Cast(v8::Value* value) {
8374#ifdef V8_ENABLE_CHECKS
8375 CheckCast(value);
8376#endif
8377 return static_cast<Uint8Array*>(value);
8378}
8379
8380
8381Int8Array* Int8Array::Cast(v8::Value* value) {
8382#ifdef V8_ENABLE_CHECKS
8383 CheckCast(value);
8384#endif
8385 return static_cast<Int8Array*>(value);
8386}
8387
8388
8389Uint16Array* Uint16Array::Cast(v8::Value* value) {
8390#ifdef V8_ENABLE_CHECKS
8391 CheckCast(value);
8392#endif
8393 return static_cast<Uint16Array*>(value);
8394}
8395
8396
8397Int16Array* Int16Array::Cast(v8::Value* value) {
8398#ifdef V8_ENABLE_CHECKS
8399 CheckCast(value);
8400#endif
8401 return static_cast<Int16Array*>(value);
8402}
8403
8404
8405Uint32Array* Uint32Array::Cast(v8::Value* value) {
8406#ifdef V8_ENABLE_CHECKS
8407 CheckCast(value);
8408#endif
8409 return static_cast<Uint32Array*>(value);
8410}
8411
8412
8413Int32Array* Int32Array::Cast(v8::Value* value) {
8414#ifdef V8_ENABLE_CHECKS
8415 CheckCast(value);
8416#endif
8417 return static_cast<Int32Array*>(value);
8418}
8419
8420
8421Float32Array* Float32Array::Cast(v8::Value* value) {
8422#ifdef V8_ENABLE_CHECKS
8423 CheckCast(value);
8424#endif
8425 return static_cast<Float32Array*>(value);
8426}
8427
8428
8429Float64Array* Float64Array::Cast(v8::Value* value) {
8430#ifdef V8_ENABLE_CHECKS
8431 CheckCast(value);
8432#endif
8433 return static_cast<Float64Array*>(value);
8434}
8435
8436
8437Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
8438#ifdef V8_ENABLE_CHECKS
8439 CheckCast(value);
8440#endif
8441 return static_cast<Uint8ClampedArray*>(value);
8442}
8443
8444
8445DataView* DataView::Cast(v8::Value* value) {
8446#ifdef V8_ENABLE_CHECKS
8447 CheckCast(value);
8448#endif
8449 return static_cast<DataView*>(value);
8450}
8451
8452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008453SharedArrayBuffer* SharedArrayBuffer::Cast(v8::Value* value) {
8454#ifdef V8_ENABLE_CHECKS
8455 CheckCast(value);
8456#endif
8457 return static_cast<SharedArrayBuffer*>(value);
8458}
8459
8460
Steve Blocka7e24c12009-10-30 11:49:00 +00008461Function* Function::Cast(v8::Value* value) {
8462#ifdef V8_ENABLE_CHECKS
8463 CheckCast(value);
8464#endif
8465 return static_cast<Function*>(value);
8466}
8467
8468
8469External* External::Cast(v8::Value* value) {
8470#ifdef V8_ENABLE_CHECKS
8471 CheckCast(value);
8472#endif
8473 return static_cast<External*>(value);
8474}
8475
8476
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008477template<typename T>
8478Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
8479 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
Steve Blocka7e24c12009-10-30 11:49:00 +00008480}
8481
8482
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008483template<typename T>
8484Local<Value> PropertyCallbackInfo<T>::Data() const {
8485 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008486}
8487
8488
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008489template<typename T>
8490Local<Object> PropertyCallbackInfo<T>::This() const {
8491 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
8492}
8493
8494
8495template<typename T>
8496Local<Object> PropertyCallbackInfo<T>::Holder() const {
8497 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
8498}
8499
8500
8501template<typename T>
8502ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
8503 return ReturnValue<T>(&args_[kReturnValueIndex]);
8504}
8505
Ben Murdoch097c5b22016-05-18 11:27:45 +01008506template <typename T>
8507bool PropertyCallbackInfo<T>::ShouldThrowOnError() const {
8508 typedef internal::Internals I;
8509 return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0);
8510}
8511
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008512
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008513Local<Primitive> Undefined(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008514 typedef internal::Object* S;
8515 typedef internal::Internals I;
8516 I::CheckInitialized(isolate);
8517 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008518 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008519}
8520
8521
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008522Local<Primitive> Null(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008523 typedef internal::Object* S;
8524 typedef internal::Internals I;
8525 I::CheckInitialized(isolate);
8526 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008527 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008528}
8529
8530
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008531Local<Boolean> True(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008532 typedef internal::Object* S;
8533 typedef internal::Internals I;
8534 I::CheckInitialized(isolate);
8535 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008536 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008537}
8538
8539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008540Local<Boolean> False(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008541 typedef internal::Object* S;
8542 typedef internal::Internals I;
8543 I::CheckInitialized(isolate);
8544 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008545 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008546}
8547
8548
8549void Isolate::SetData(uint32_t slot, void* data) {
8550 typedef internal::Internals I;
8551 I::SetEmbedderData(this, slot, data);
8552}
8553
8554
8555void* Isolate::GetData(uint32_t slot) {
8556 typedef internal::Internals I;
8557 return I::GetEmbedderData(this, slot);
8558}
8559
8560
8561uint32_t Isolate::GetNumberOfDataSlots() {
8562 typedef internal::Internals I;
8563 return I::kNumIsolateDataSlots;
8564}
8565
8566
8567int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
8568 int64_t change_in_bytes) {
8569 typedef internal::Internals I;
8570 int64_t* amount_of_external_allocated_memory =
8571 reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
8572 I::kAmountOfExternalAllocatedMemoryOffset);
8573 int64_t* amount_of_external_allocated_memory_at_last_global_gc =
8574 reinterpret_cast<int64_t*>(
8575 reinterpret_cast<uint8_t*>(this) +
8576 I::kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset);
8577 int64_t amount = *amount_of_external_allocated_memory + change_in_bytes;
8578 if (change_in_bytes > 0 &&
8579 amount - *amount_of_external_allocated_memory_at_last_global_gc >
8580 I::kExternalAllocationLimit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008581 ReportExternalAllocationLimitReached();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008582 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008583 *amount_of_external_allocated_memory = amount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008584 return *amount_of_external_allocated_memory;
8585}
8586
8587
8588template<typename T>
8589void Isolate::SetObjectGroupId(const Persistent<T>& object,
8590 UniqueId id) {
8591 TYPE_CHECK(Value, T);
8592 SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id);
8593}
8594
8595
8596template<typename T>
8597void Isolate::SetReferenceFromGroup(UniqueId id,
8598 const Persistent<T>& object) {
8599 TYPE_CHECK(Value, T);
8600 SetReferenceFromGroup(id,
8601 reinterpret_cast<v8::internal::Object**>(object.val_));
8602}
8603
8604
8605template<typename T, typename S>
8606void Isolate::SetReference(const Persistent<T>& parent,
8607 const Persistent<S>& child) {
8608 TYPE_CHECK(Object, T);
8609 TYPE_CHECK(Value, S);
8610 SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_),
8611 reinterpret_cast<v8::internal::Object**>(child.val_));
8612}
8613
8614
8615Local<Value> Context::GetEmbedderData(int index) {
8616#ifndef V8_ENABLE_CHECKS
8617 typedef internal::Object O;
8618 typedef internal::HeapObject HO;
8619 typedef internal::Internals I;
8620 HO* context = *reinterpret_cast<HO**>(this);
8621 O** result =
8622 HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
8623 return Local<Value>(reinterpret_cast<Value*>(result));
8624#else
8625 return SlowGetEmbedderData(index);
8626#endif
8627}
8628
8629
8630void* Context::GetAlignedPointerFromEmbedderData(int index) {
8631#ifndef V8_ENABLE_CHECKS
8632 typedef internal::Internals I;
8633 return I::ReadEmbedderData<void*>(this, index);
8634#else
8635 return SlowGetAlignedPointerFromEmbedderData(index);
8636#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008637}
8638
8639
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008640void V8::SetAllowCodeGenerationFromStringsCallback(
8641 AllowCodeGenerationFromStringsCallback callback) {
8642 Isolate* isolate = Isolate::GetCurrent();
8643 isolate->SetAllowCodeGenerationFromStringsCallback(callback);
8644}
8645
8646
8647bool V8::IsDead() {
8648 Isolate* isolate = Isolate::GetCurrent();
8649 return isolate->IsDead();
8650}
8651
8652
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008653bool V8::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008654 Isolate* isolate = Isolate::GetCurrent();
8655 return isolate->AddMessageListener(that, data);
8656}
8657
8658
8659void V8::RemoveMessageListeners(MessageCallback that) {
8660 Isolate* isolate = Isolate::GetCurrent();
8661 isolate->RemoveMessageListeners(that);
8662}
8663
8664
8665void V8::SetFailedAccessCheckCallbackFunction(
8666 FailedAccessCheckCallback callback) {
8667 Isolate* isolate = Isolate::GetCurrent();
8668 isolate->SetFailedAccessCheckCallbackFunction(callback);
8669}
8670
8671
8672void V8::SetCaptureStackTraceForUncaughtExceptions(
8673 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8674 Isolate* isolate = Isolate::GetCurrent();
8675 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8676 options);
8677}
8678
8679
8680void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
8681 Isolate* isolate = Isolate::GetCurrent();
8682 isolate->SetFatalErrorHandler(callback);
8683}
8684
8685
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008686void V8::RemoveGCPrologueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008687 Isolate* isolate = Isolate::GetCurrent();
8688 isolate->RemoveGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008689 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008690}
8691
8692
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008693void V8::RemoveGCEpilogueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008694 Isolate* isolate = Isolate::GetCurrent();
8695 isolate->RemoveGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008696 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008697}
8698
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008699void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); }
8700
8701
8702bool V8::IsExecutionTerminating(Isolate* isolate) {
8703 if (isolate == NULL) {
8704 isolate = Isolate::GetCurrent();
8705 }
8706 return isolate->IsExecutionTerminating();
8707}
8708
8709
8710void V8::CancelTerminateExecution(Isolate* isolate) {
8711 isolate->CancelTerminateExecution();
8712}
8713
8714
8715void V8::VisitExternalResources(ExternalResourceVisitor* visitor) {
8716 Isolate* isolate = Isolate::GetCurrent();
8717 isolate->VisitExternalResources(visitor);
8718}
8719
8720
8721void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8722 Isolate* isolate = Isolate::GetCurrent();
8723 isolate->VisitHandlesWithClassIds(visitor);
8724}
8725
8726
8727void V8::VisitHandlesWithClassIds(Isolate* isolate,
8728 PersistentHandleVisitor* visitor) {
8729 isolate->VisitHandlesWithClassIds(visitor);
8730}
8731
8732
8733void V8::VisitHandlesForPartialDependence(Isolate* isolate,
8734 PersistentHandleVisitor* visitor) {
8735 isolate->VisitHandlesForPartialDependence(visitor);
8736}
8737
Steve Blocka7e24c12009-10-30 11:49:00 +00008738/**
8739 * \example shell.cc
8740 * A simple shell that takes a list of expressions on the
8741 * command-line and executes them.
8742 */
8743
8744
8745/**
8746 * \example process.cc
8747 */
8748
8749
8750} // namespace v8
8751
8752
Steve Blocka7e24c12009-10-30 11:49:00 +00008753#undef TYPE_CHECK
8754
8755
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008756#endif // INCLUDE_V8_H_