blob: 703a4f4f4ad575cc230e60ceaf38aa1f05121a13 [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
460template <class T, class P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461class WeakCallbackData {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 public:
463 typedef void (*Callback)(const WeakCallbackData<T, P>& data);
464
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 WeakCallbackData(Isolate* isolate, P* parameter, Local<T> handle)
466 : isolate_(isolate), parameter_(parameter), handle_(handle) {}
467
468 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
469 V8_INLINE P* GetParameter() const { return parameter_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 V8_INLINE Local<T> GetValue() const { return handle_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000471
472 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473 Isolate* isolate_;
474 P* parameter_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 Local<T> handle_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400476};
477
478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479// TODO(dcarney): delete this with WeakCallbackData
480template <class T>
481using PhantomCallbackData = WeakCallbackInfo<T>;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400482
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400483
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484enum class WeakCallbackType { kParameter, kInternalFields };
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
486
487/**
488 * An object reference that is independent of any handle scope. Where
489 * a Local handle only lives as long as the HandleScope in which it was
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 * allocated, a PersistentBase handle remains valid until it is explicitly
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 * disposed.
492 *
493 * A persistent handle contains a reference to a storage cell within
494 * the v8 engine which holds an object value and which is updated by
495 * the garbage collector whenever the object is moved. A new storage
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 * cell can be created using the constructor or PersistentBase::Reset and
497 * existing handles can be disposed using PersistentBase::Reset.
498 *
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500template <class T> class PersistentBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 * If non-empty, destroy the underlying storage cell
504 * IsEmpty() will return true after this call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000505 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 V8_INLINE void Reset();
507 /**
508 * If non-empty, destroy the underlying storage cell
509 * and create a new one with the contents of other if other is non empty
510 */
511 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000512 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
Steve Blocka7e24c12009-10-30 11:49:00 +0000513
514 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 * If non-empty, destroy the underlying storage cell
516 * and create a new one with the contents of other if other is non empty
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 template <class S>
519 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
520
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400521 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
522 V8_INLINE void Empty() { val_ = 0; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000524 V8_INLINE Local<T> Get(Isolate* isolate) const {
525 return Local<T>::New(isolate, *this);
526 }
527
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 template <class S>
529 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
530 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
531 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400532 if (a == NULL) return b == NULL;
533 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 return *a == *b;
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 }
536
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000537 template <class S>
538 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
540 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400541 if (a == NULL) return b == NULL;
542 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 return *a == *b;
544 }
545
546 template <class S>
547 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
548 return !operator==(that);
549 }
550
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551 template <class S>
552 V8_INLINE bool operator!=(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000553 return !operator==(that);
554 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000555
556 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 * Install a finalization callback on this object.
558 * NOTE: There is no guarantee as to *when* or even *if* the callback is
559 * invoked. The invocation is performed solely on a best effort basis.
560 * As always, GC-based finalization should *not* be relied upon for any
561 * critical form of resource management!
Steve Blocka7e24c12009-10-30 11:49:00 +0000562 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 template <typename P>
564 V8_INLINE V8_DEPRECATED(
565 "use WeakCallbackInfo version",
566 void SetWeak(P* parameter,
567 typename WeakCallbackData<T, P>::Callback callback));
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000569 template <typename S, typename P>
570 V8_INLINE V8_DEPRECATED(
571 "use WeakCallbackInfo version",
572 void SetWeak(P* parameter,
573 typename WeakCallbackData<S, P>::Callback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400575 // Phantom persistents work like weak persistents, except that the pointer to
576 // the object being collected is not available in the finalization callback.
577 // This enables the garbage collector to collect the object and any objects
578 // it references transitively in one GC cycle. At the moment you can either
579 // specify a parameter for the callback or the location of two internal
580 // fields in the dying object.
581 template <typename P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582 V8_INLINE V8_DEPRECATED(
583 "use SetWeak",
584 void SetPhantom(P* parameter,
585 typename WeakCallbackInfo<P>::Callback callback,
586 int internal_field_index1 = -1,
587 int internal_field_index2 = -1));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400588
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589 template <typename P>
590 V8_INLINE void SetWeak(P* parameter,
591 typename WeakCallbackInfo<P>::Callback callback,
592 WeakCallbackType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400593
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594 template<typename P>
595 V8_INLINE P* ClearWeak();
596
597 // TODO(dcarney): remove this.
598 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
599
600 /**
Ben Murdochda12d292016-06-02 14:46:10 +0100601 * Allows the embedder to tell the v8 garbage collector that a certain object
602 * is alive. Only allowed when the embedder is asked to trace its heap by
603 * EmbedderHeapTracer.
604 */
605 V8_INLINE void RegisterExternalReference(Isolate* isolate);
606
607 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608 * Marks the reference to this object independent. Garbage collector is free
609 * to ignore any object groups containing this object. Weak callback for an
610 * independent handle should not assume that it will be preceded by a global
611 * GC prologue callback or followed by a global GC epilogue callback.
612 */
613 V8_INLINE void MarkIndependent();
614
615 /**
616 * Marks the reference to this object partially dependent. Partially dependent
617 * handles only depend on other partially dependent handles and these
618 * dependencies are provided through object groups. It provides a way to build
619 * smaller object groups for young objects that represent only a subset of all
620 * external dependencies. This mark is automatically cleared after each
621 * garbage collection.
622 */
623 V8_INLINE void MarkPartiallyDependent();
624
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625 /**
626 * Marks the reference to this object as active. The scavenge garbage
627 * collection should not reclaim the objects marked as active.
628 * This bit is cleared after the each garbage collection pass.
629 */
630 V8_INLINE void MarkActive();
631
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 V8_INLINE bool IsIndependent() const;
633
634 /** Checks if the handle holds the only reference to an object. */
635 V8_INLINE bool IsNearDeath() const;
636
637 /** Returns true if the handle's reference is weak. */
638 V8_INLINE bool IsWeak() const;
639
640 /**
641 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
642 * description in v8-profiler.h for details.
643 */
644 V8_INLINE void SetWrapperClassId(uint16_t class_id);
645
646 /**
647 * Returns the class ID previously assigned to this handle or 0 if no class ID
648 * was previously assigned.
649 */
650 V8_INLINE uint16_t WrapperClassId() const;
651
652 private:
653 friend class Isolate;
654 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 template<class F> friend class Local;
656 template<class F1, class F2> friend class Persistent;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000657 template <class F>
658 friend class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659 template<class F> friend class PersistentBase;
660 template<class F> friend class ReturnValue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000661 template <class F1, class F2, class F3>
662 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663 template<class F1, class F2> friend class PersistentValueVector;
664 friend class Object;
665
666 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000667 PersistentBase(const PersistentBase& other) = delete; // NOLINT
668 void operator=(const PersistentBase&) = delete;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669 V8_INLINE static T* New(Isolate* isolate, T* that);
670
671 T* val_;
672};
673
674
675/**
676 * Default traits for Persistent. This class does not allow
677 * use of the copy constructor or assignment operator.
678 * At present kResetInDestructor is not set, but that will change in a future
679 * version.
680 */
681template<class T>
682class NonCopyablePersistentTraits {
683 public:
684 typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
685 static const bool kResetInDestructor = false;
686 template<class S, class M>
687 V8_INLINE static void Copy(const Persistent<S, M>& source,
688 NonCopyablePersistent* dest) {
689 Uncompilable<Object>();
690 }
691 // TODO(dcarney): come up with a good compile error here.
692 template<class O> V8_INLINE static void Uncompilable() {
693 TYPE_CHECK(O, Primitive);
694 }
695};
696
697
698/**
699 * Helper class traits to allow copying and assignment of Persistent.
700 * This will clone the contents of storage cell, but not any of the flags, etc.
701 */
702template<class T>
703struct CopyablePersistentTraits {
704 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
705 static const bool kResetInDestructor = true;
706 template<class S, class M>
707 static V8_INLINE void Copy(const Persistent<S, M>& source,
708 CopyablePersistent* dest) {
709 // do nothing, just allow copy
710 }
711};
712
713
714/**
715 * A PersistentBase which allows copy and assignment.
716 *
717 * Copy, assignment and destructor bevavior is controlled by the traits
718 * class M.
719 *
720 * Note: Persistent class hierarchy is subject to future changes.
721 */
722template <class T, class M> class Persistent : public PersistentBase<T> {
723 public:
724 /**
725 * A Persistent with no storage cell.
726 */
727 V8_INLINE Persistent() : PersistentBase<T>(0) { }
728 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000729 * Construct a Persistent from a Local.
730 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 * pointing to the same object, and no flags are set.
732 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000733 template <class S>
734 V8_INLINE Persistent(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
736 TYPE_CHECK(T, S);
737 }
738 /**
739 * Construct a Persistent from a Persistent.
740 * When the Persistent is non-empty, a new storage cell is created
741 * pointing to the same object, and no flags are set.
742 */
743 template <class S, class M2>
744 V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that)
745 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
746 TYPE_CHECK(T, S);
747 }
748 /**
749 * The copy constructors and assignment operator create a Persistent
750 * exactly as the Persistent constructor, but the Copy function from the
751 * traits class is called, allowing the setting of flags based on the
752 * copied Persistent.
753 */
754 V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) {
755 Copy(that);
756 }
757 template <class S, class M2>
758 V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) {
759 Copy(that);
760 }
761 V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT
762 Copy(that);
763 return *this;
764 }
765 template <class S, class M2>
766 V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT
767 Copy(that);
768 return *this;
769 }
770 /**
771 * The destructor will dispose the Persistent based on the
772 * kResetInDestructor flags in the traits class. Since not calling dispose
773 * can result in a memory leak, it is recommended to always set this flag.
774 */
775 V8_INLINE ~Persistent() {
776 if (M::kResetInDestructor) this->Reset();
777 }
778
779 // TODO(dcarney): this is pretty useless, fix or remove
780 template <class S>
781 V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000782#ifdef V8_ENABLE_CHECKS
783 // If we're going to perform the type check then we have to check
784 // that the handle isn't empty before doing the checked cast.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785 if (!that.IsEmpty()) T::Cast(*that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000786#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000787 return reinterpret_cast<Persistent<T>&>(that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 }
789
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 // TODO(dcarney): this is pretty useless, fix or remove
791 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
Steve Block6ded16b2010-05-10 14:33:55 +0100792 return Persistent<S>::Cast(*this);
793 }
794
Steve Blocka7e24c12009-10-30 11:49:00 +0000795 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796 friend class Isolate;
797 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 template<class F> friend class Local;
799 template<class F1, class F2> friend class Persistent;
800 template<class F> friend class ReturnValue;
801
Ben Murdoch097c5b22016-05-18 11:27:45 +0100802 explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000803 V8_INLINE T* operator*() const { return this->val_; }
804 template<class S, class M2>
805 V8_INLINE void Copy(const Persistent<S, M2>& that);
806};
807
808
809/**
810 * A PersistentBase which has move semantics.
811 *
812 * Note: Persistent class hierarchy is subject to future changes.
813 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000814template <class T>
815class Global : public PersistentBase<T> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 public:
817 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 * A Global with no storage cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000819 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000820 V8_INLINE Global() : PersistentBase<T>(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000822 * Construct a Global from a Local.
823 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000824 * pointing to the same object, and no flags are set.
825 */
826 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827 V8_INLINE Global(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
829 TYPE_CHECK(T, S);
830 }
831 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000832 * Construct a Global from a PersistentBase.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 * When the Persistent is non-empty, a new storage cell is created
834 * pointing to the same object, and no flags are set.
835 */
836 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837 V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that)
838 : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839 TYPE_CHECK(T, S);
840 }
841 /**
842 * Move constructor.
843 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000844 V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT
845 other.val_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000847 V8_INLINE ~Global() { this->Reset(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 /**
849 * Move via assignment.
850 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000851 template <class S>
852 V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 TYPE_CHECK(T, S);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000854 if (this != &rhs) {
855 this->Reset();
856 this->val_ = rhs.val_;
857 rhs.val_ = nullptr;
858 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 return *this;
860 }
861 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000862 * Pass allows returning uniques from functions, etc.
863 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000864 Global Pass() { return static_cast<Global&&>(*this); } // NOLINT
865
866 /*
867 * For compatibility with Chromium's base::Bind (base::Passed).
868 */
869 typedef void MoveOnlyTypeForCPP03;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000870
871 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000872 template <class F>
873 friend class ReturnValue;
874 Global(const Global&) = delete;
875 void operator=(const Global&) = delete;
876 V8_INLINE T* operator*() const { return this->val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000877};
878
879
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000880// UniquePersistent is an alias for Global for historical reason.
881template <class T>
882using UniquePersistent = Global<T>;
883
884
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 /**
886 * A stack-allocated class that governs a number of local handles.
887 * After a handle scope has been created, all local handles will be
888 * allocated within that handle scope until either the handle scope is
889 * deleted or another handle scope is created. If there is already a
890 * handle scope and a new one is created, all allocations will take
891 * place in the new handle scope until it is deleted. After that,
892 * new handles will again be allocated in the original handle scope.
893 *
894 * After the handle scope of a local handle has been deleted the
895 * garbage collector will no longer track the object stored in the
896 * handle and may deallocate it. The behavior of accessing a handle
897 * for which the handle scope has been deleted is undefined.
898 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899class V8_EXPORT HandleScope {
Steve Blocka7e24c12009-10-30 11:49:00 +0000900 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100901 explicit HandleScope(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000902
903 ~HandleScope();
904
905 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 * Counts the number of allocated handles.
907 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908 static int NumberOfHandles(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000909
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 V8_INLINE Isolate* GetIsolate() const {
911 return reinterpret_cast<Isolate*>(isolate_);
912 }
913
914 protected:
915 V8_INLINE HandleScope() {}
916
917 void Initialize(Isolate* isolate);
918
919 static internal::Object** CreateHandle(internal::Isolate* isolate,
920 internal::Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000921
922 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923 // Uses heap_object to obtain the current Isolate.
924 static internal::Object** CreateHandle(internal::HeapObject* heap_object,
925 internal::Object* value);
926
927 // Make it hard to create heap-allocated or illegal handle scopes by
928 // disallowing certain operations.
Steve Blocka7e24c12009-10-30 11:49:00 +0000929 HandleScope(const HandleScope&);
930 void operator=(const HandleScope&);
931 void* operator new(size_t size);
932 void operator delete(void*, size_t);
933
Steve Block44f0eee2011-05-26 01:26:41 +0100934 internal::Isolate* isolate_;
John Reck59135872010-11-02 12:39:01 -0700935 internal::Object** prev_next_;
936 internal::Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000937
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 // Local::New uses CreateHandle with an Isolate* parameter.
939 template<class F> friend class Local;
Steve Blocka7e24c12009-10-30 11:49:00 +0000940
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
942 // a HeapObject* in their shortcuts.
943 friend class Object;
944 friend class Context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000945};
946
947
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948/**
949 * A HandleScope which first allocates a handle in the current scope
950 * which will be later filled with the escape value.
951 */
952class V8_EXPORT EscapableHandleScope : public HandleScope {
953 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100954 explicit EscapableHandleScope(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 V8_INLINE ~EscapableHandleScope() {}
956
957 /**
958 * Pushes the value into the previous scope and returns a handle to it.
959 * Cannot be called twice.
960 */
961 template <class T>
962 V8_INLINE Local<T> Escape(Local<T> value) {
963 internal::Object** slot =
964 Escape(reinterpret_cast<internal::Object**>(*value));
965 return Local<T>(reinterpret_cast<T*>(slot));
966 }
967
968 private:
969 internal::Object** Escape(internal::Object** escape_value);
970
971 // Make it hard to create heap-allocated or illegal handle scopes by
972 // disallowing certain operations.
973 EscapableHandleScope(const EscapableHandleScope&);
974 void operator=(const EscapableHandleScope&);
975 void* operator new(size_t size);
976 void operator delete(void*, size_t);
977
978 internal::Object** escape_slot_;
979};
980
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981class V8_EXPORT SealHandleScope {
982 public:
983 SealHandleScope(Isolate* isolate);
984 ~SealHandleScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000986 private:
987 // Make it hard to create heap-allocated or illegal handle scopes by
988 // disallowing certain operations.
989 SealHandleScope(const SealHandleScope&);
990 void operator=(const SealHandleScope&);
991 void* operator new(size_t size);
992 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000994 internal::Isolate* isolate_;
995 internal::Object** prev_limit_;
996 int prev_sealed_level_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997};
998
999
Ben Murdoch257744e2011-11-30 15:57:28 +00001000// --- Special objects ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001001
1002
1003/**
1004 * The superclass of values and API object templates.
1005 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006class V8_EXPORT Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00001007 private:
1008 Data();
1009};
1010
1011
1012/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001013 * The optional attributes of ScriptOrigin.
1014 */
1015class ScriptOriginOptions {
1016 public:
1017 V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false,
1018 bool is_shared_cross_origin = false,
1019 bool is_opaque = false)
1020 : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) |
1021 (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
1022 (is_opaque ? kIsOpaque : 0)) {}
1023 V8_INLINE ScriptOriginOptions(int flags)
1024 : flags_(flags &
1025 (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {}
1026 bool IsEmbedderDebugScript() const {
1027 return (flags_ & kIsEmbedderDebugScript) != 0;
1028 }
1029 bool IsSharedCrossOrigin() const {
1030 return (flags_ & kIsSharedCrossOrigin) != 0;
1031 }
1032 bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
1033 int Flags() const { return flags_; }
1034
1035 private:
1036 enum {
1037 kIsEmbedderDebugScript = 1,
1038 kIsSharedCrossOrigin = 1 << 1,
1039 kIsOpaque = 1 << 2
1040 };
1041 const int flags_;
1042};
1043
1044/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 * The origin, within a file, of a script.
1046 */
Steve Block8defd9f2010-07-08 12:39:36 +01001047class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 V8_INLINE ScriptOrigin(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001050 Local<Value> resource_name,
1051 Local<Integer> resource_line_offset = Local<Integer>(),
1052 Local<Integer> resource_column_offset = Local<Integer>(),
1053 Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(),
1054 Local<Integer> script_id = Local<Integer>(),
1055 Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(),
1056 Local<Value> source_map_url = Local<Value>(),
1057 Local<Boolean> resource_is_opaque = Local<Boolean>());
1058 V8_INLINE Local<Value> ResourceName() const;
1059 V8_INLINE Local<Integer> ResourceLineOffset() const;
1060 V8_INLINE Local<Integer> ResourceColumnOffset() const;
1061 /**
1062 * Returns true for embedder's debugger scripts
1063 */
1064 V8_INLINE Local<Integer> ScriptID() const;
1065 V8_INLINE Local<Value> SourceMapUrl() const;
1066 V8_INLINE ScriptOriginOptions Options() const { return options_; }
1067
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 Local<Value> resource_name_;
1070 Local<Integer> resource_line_offset_;
1071 Local<Integer> resource_column_offset_;
1072 ScriptOriginOptions options_;
1073 Local<Integer> script_id_;
1074 Local<Value> source_map_url_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001075};
1076
1077
1078/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001079 * A compiled JavaScript script, not yet tied to a Context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001080 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081class V8_EXPORT UnboundScript {
Steve Blocka7e24c12009-10-30 11:49:00 +00001082 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001083 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 * Binds the script to the currently entered context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001086 Local<Script> BindToCurrentContext();
1087
1088 int GetId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 Local<Value> GetScriptName();
Steve Blocka7e24c12009-10-30 11:49:00 +00001090
1091 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092 * Data read from magic sourceURL comments.
1093 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001094 Local<Value> GetSourceURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095 /**
1096 * Data read from magic sourceMappingURL comments.
1097 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001098 Local<Value> GetSourceMappingURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099
1100 /**
1101 * Returns zero based line number of the code_pos location in the script.
1102 * -1 will be returned if no information available.
1103 */
1104 int GetLineNumber(int code_pos);
1105
1106 static const int kNoScriptId = 0;
1107};
1108
1109
1110/**
1111 * A compiled JavaScript script, tied to a Context which was active when the
1112 * script was compiled.
1113 */
1114class V8_EXPORT Script {
1115 public:
1116 /**
1117 * A shorthand for ScriptCompiler::Compile().
1118 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001119 static V8_DEPRECATE_SOON(
1120 "Use maybe version",
1121 Local<Script> Compile(Local<String> source,
1122 ScriptOrigin* origin = nullptr));
1123 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1124 Local<Context> context, Local<String> source,
1125 ScriptOrigin* origin = nullptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001127 static Local<Script> V8_DEPRECATE_SOON("Use maybe version",
1128 Compile(Local<String> source,
1129 Local<String> file_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130
1131 /**
1132 * Runs the script returning the resulting value. It will be run in the
1133 * context in which it was created (ScriptCompiler::CompileBound or
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001134 * UnboundScript::BindToCurrentContext()).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001135 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001136 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run());
1137 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138
1139 /**
1140 * Returns the corresponding context-unbound script.
1141 */
1142 Local<UnboundScript> GetUnboundScript();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143};
1144
1145
1146/**
1147 * For compiling scripts.
1148 */
1149class V8_EXPORT ScriptCompiler {
1150 public:
1151 /**
1152 * Compilation data that the embedder can cache and pass back to speed up
1153 * future compilations. The data is produced if the CompilerOptions passed to
1154 * the compilation functions in ScriptCompiler contains produce_data_to_cache
1155 * = true. The data to cache can then can be retrieved from
1156 * UnboundScript.
1157 */
1158 struct V8_EXPORT CachedData {
1159 enum BufferPolicy {
1160 BufferNotOwned,
1161 BufferOwned
1162 };
1163
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001164 CachedData()
1165 : data(NULL),
1166 length(0),
1167 rejected(false),
1168 buffer_policy(BufferNotOwned) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001169
1170 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
1171 // data and guarantees that it stays alive until the CachedData object is
1172 // destroyed. If the policy is BufferOwned, the given data will be deleted
1173 // (with delete[]) when the CachedData object is destroyed.
1174 CachedData(const uint8_t* data, int length,
1175 BufferPolicy buffer_policy = BufferNotOwned);
1176 ~CachedData();
1177 // TODO(marja): Async compilation; add constructors which take a callback
1178 // which will be called when V8 no longer needs the data.
1179 const uint8_t* data;
1180 int length;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001181 bool rejected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001182 BufferPolicy buffer_policy;
1183
1184 private:
1185 // Prevent copying. Not implemented.
1186 CachedData(const CachedData&);
1187 CachedData& operator=(const CachedData&);
1188 };
1189
1190 /**
1191 * Source code which can be then compiled to a UnboundScript or Script.
1192 */
1193 class Source {
1194 public:
1195 // Source takes ownership of CachedData.
1196 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
1197 CachedData* cached_data = NULL);
1198 V8_INLINE Source(Local<String> source_string,
1199 CachedData* cached_data = NULL);
1200 V8_INLINE ~Source();
1201
1202 // Ownership of the CachedData or its buffers is *not* transferred to the
1203 // caller. The CachedData object is alive as long as the Source object is
1204 // alive.
1205 V8_INLINE const CachedData* GetCachedData() const;
1206
1207 private:
1208 friend class ScriptCompiler;
1209 // Prevent copying. Not implemented.
1210 Source(const Source&);
1211 Source& operator=(const Source&);
1212
1213 Local<String> source_string;
1214
1215 // Origin information
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001216 Local<Value> resource_name;
1217 Local<Integer> resource_line_offset;
1218 Local<Integer> resource_column_offset;
1219 ScriptOriginOptions resource_options;
1220 Local<Value> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001221
1222 // Cached data from previous compilation (if a kConsume*Cache flag is
1223 // set), or hold newly generated cache data (kProduce*Cache flags) are
1224 // set when calling a compile method.
1225 CachedData* cached_data;
1226 };
1227
1228 /**
1229 * For streaming incomplete script data to V8. The embedder should implement a
1230 * subclass of this class.
1231 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001232 class V8_EXPORT ExternalSourceStream {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 public:
1234 virtual ~ExternalSourceStream() {}
1235
1236 /**
1237 * V8 calls this to request the next chunk of data from the embedder. This
1238 * function will be called on a background thread, so it's OK to block and
1239 * wait for the data, if the embedder doesn't have data yet. Returns the
1240 * length of the data returned. When the data ends, GetMoreData should
1241 * return 0. Caller takes ownership of the data.
1242 *
1243 * When streaming UTF-8 data, V8 handles multi-byte characters split between
1244 * two data chunks, but doesn't handle multi-byte characters split between
1245 * more than two data chunks. The embedder can avoid this problem by always
1246 * returning at least 2 bytes of data.
1247 *
1248 * If the embedder wants to cancel the streaming, they should make the next
1249 * GetMoreData call return 0. V8 will interpret it as end of data (and most
1250 * probably, parsing will fail). The streaming task will return as soon as
1251 * V8 has parsed the data it received so far.
1252 */
1253 virtual size_t GetMoreData(const uint8_t** src) = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001254
1255 /**
1256 * V8 calls this method to set a 'bookmark' at the current position in
1257 * the source stream, for the purpose of (maybe) later calling
1258 * ResetToBookmark. If ResetToBookmark is called later, then subsequent
1259 * calls to GetMoreData should return the same data as they did when
1260 * SetBookmark was called earlier.
1261 *
1262 * The embedder may return 'false' to indicate it cannot provide this
1263 * functionality.
1264 */
1265 virtual bool SetBookmark();
1266
1267 /**
1268 * V8 calls this to return to a previously set bookmark.
1269 */
1270 virtual void ResetToBookmark();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 };
1272
1273
1274 /**
1275 * Source code which can be streamed into V8 in pieces. It will be parsed
1276 * while streaming. It can be compiled after the streaming is complete.
1277 * StreamedSource must be kept alive while the streaming task is ran (see
1278 * ScriptStreamingTask below).
1279 */
1280 class V8_EXPORT StreamedSource {
1281 public:
1282 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8 };
1283
1284 StreamedSource(ExternalSourceStream* source_stream, Encoding encoding);
1285 ~StreamedSource();
1286
1287 // Ownership of the CachedData or its buffers is *not* transferred to the
1288 // caller. The CachedData object is alive as long as the StreamedSource
1289 // object is alive.
1290 const CachedData* GetCachedData() const;
1291
1292 internal::StreamedSource* impl() const { return impl_; }
1293
1294 private:
1295 // Prevent copying. Not implemented.
1296 StreamedSource(const StreamedSource&);
1297 StreamedSource& operator=(const StreamedSource&);
1298
1299 internal::StreamedSource* impl_;
1300 };
1301
1302 /**
1303 * A streaming task which the embedder must run on a background thread to
1304 * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript.
1305 */
1306 class ScriptStreamingTask {
1307 public:
1308 virtual ~ScriptStreamingTask() {}
1309 virtual void Run() = 0;
1310 };
1311
1312 enum CompileOptions {
1313 kNoCompileOptions = 0,
1314 kProduceParserCache,
1315 kConsumeParserCache,
1316 kProduceCodeCache,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001317 kConsumeCodeCache
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 };
1319
1320 /**
1321 * Compiles the specified script (context-independent).
1322 * Cached data as part of the source object can be optionally produced to be
1323 * consumed later to speed up compilation of identical source scripts.
1324 *
1325 * Note that when producing cached data, the source must point to NULL for
1326 * cached data. When consuming cached data, the cached data must have been
1327 * produced by the same version of V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 *
Andrei Popescu402d9372010-02-26 13:31:12 +00001329 * \param source Script source code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330 * \return Compiled script object (context independent; for running it must be
1331 * bound to a context).
Andrei Popescu402d9372010-02-26 13:31:12 +00001332 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001333 static V8_DEPRECATED("Use maybe version",
1334 Local<UnboundScript> CompileUnbound(
1335 Isolate* isolate, Source* source,
1336 CompileOptions options = kNoCompileOptions));
1337 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338 Isolate* isolate, Source* source,
1339 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001340
1341 /**
1342 * Compiles the specified script (bound to current context).
1343 *
1344 * \param source Script source code.
Andrei Popescu402d9372010-02-26 13:31:12 +00001345 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1346 * using pre_data speeds compilation if it's done multiple times.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001347 * Owned by caller, no references are kept when this function returns.
Andrei Popescu402d9372010-02-26 13:31:12 +00001348 * \return Compiled script object, bound to the context that was active
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001349 * when this function was called. When run it will always use this
Andrei Popescu402d9372010-02-26 13:31:12 +00001350 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001351 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001352 static V8_DEPRECATED(
1353 "Use maybe version",
1354 Local<Script> Compile(Isolate* isolate, Source* source,
1355 CompileOptions options = kNoCompileOptions));
1356 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1357 Local<Context> context, Source* source,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001359
1360 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001361 * Returns a task which streams script data into V8, or NULL if the script
1362 * cannot be streamed. The user is responsible for running the task on a
1363 * background thread and deleting it. When ran, the task starts parsing the
1364 * script, and it will request data from the StreamedSource as needed. When
1365 * ScriptStreamingTask::Run exits, all data has been streamed and the script
1366 * can be compiled (see Compile below).
Andrei Popescu402d9372010-02-26 13:31:12 +00001367 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001368 * This API allows to start the streaming with as little data as possible, and
1369 * the remaining data (for example, the ScriptOrigin) is passed to Compile.
Andrei Popescu402d9372010-02-26 13:31:12 +00001370 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001371 static ScriptStreamingTask* StartStreamingScript(
1372 Isolate* isolate, StreamedSource* source,
1373 CompileOptions options = kNoCompileOptions);
Steve Blocka7e24c12009-10-30 11:49:00 +00001374
1375 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376 * Compiles a streamed script (bound to current context).
1377 *
1378 * This can only be called after the streaming has finished
1379 * (ScriptStreamingTask has been run). V8 doesn't construct the source string
1380 * during streaming, so the embedder needs to pass the full source here.
Steve Blocka7e24c12009-10-30 11:49:00 +00001381 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001382 static V8_DEPRECATED("Use maybe version",
1383 Local<Script> Compile(Isolate* isolate,
1384 StreamedSource* source,
1385 Local<String> full_source_string,
1386 const ScriptOrigin& origin));
1387 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1388 Local<Context> context, StreamedSource* source,
1389 Local<String> full_source_string, const ScriptOrigin& origin);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001390
1391 /**
1392 * Return a version tag for CachedData for the current V8 version & flags.
1393 *
1394 * This value is meant only for determining whether a previously generated
1395 * CachedData instance is still valid; the tag has no other meaing.
1396 *
1397 * Background: The data carried by CachedData may depend on the exact
1398 * V8 version number or currently compiler flags. This means when
1399 * persisting CachedData, the embedder must take care to not pass in
1400 * data from another V8 version, or the same version with different
1401 * features enabled.
1402 *
1403 * The easiest way to do so is to clear the embedder's cache on any
1404 * such change.
1405 *
1406 * Alternatively, this tag can be stored alongside the cached data and
1407 * compared when it is being used.
1408 */
1409 static uint32_t CachedDataVersionTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001410
1411 /**
1412 * Compile an ES6 module.
1413 *
1414 * This is an unfinished experimental feature, and is only exposed
1415 * here for internal testing purposes.
1416 * Only parsing works at the moment. Do not use.
1417 *
1418 * TODO(adamk): Script is likely the wrong return value for this;
1419 * should return some new Module type.
1420 */
1421 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule(
1422 Local<Context> context, Source* source,
1423 CompileOptions options = kNoCompileOptions);
1424
1425 /**
1426 * Compile a function for a given context. This is equivalent to running
1427 *
1428 * with (obj) {
1429 * return function(args) { ... }
1430 * }
1431 *
1432 * It is possible to specify multiple context extensions (obj in the above
1433 * example).
1434 */
1435 static V8_DEPRECATE_SOON("Use maybe version",
1436 Local<Function> CompileFunctionInContext(
1437 Isolate* isolate, Source* source,
1438 Local<Context> context, size_t arguments_count,
1439 Local<String> arguments[],
1440 size_t context_extension_count,
1441 Local<Object> context_extensions[]));
1442 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
1443 Local<Context> context, Source* source, size_t arguments_count,
1444 Local<String> arguments[], size_t context_extension_count,
1445 Local<Object> context_extensions[]);
1446
1447 private:
1448 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
1449 Isolate* isolate, Source* source, CompileOptions options, bool is_module);
Steve Blocka7e24c12009-10-30 11:49:00 +00001450};
1451
1452
1453/**
1454 * An error message.
1455 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001456class V8_EXPORT Message {
Steve Blocka7e24c12009-10-30 11:49:00 +00001457 public:
1458 Local<String> Get() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001459
1460 V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const);
1461 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
1462 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001463
1464 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001465 * Returns the origin for the script from where the function causing the
1466 * error originates.
1467 */
1468 ScriptOrigin GetScriptOrigin() const;
1469
1470 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001471 * Returns the resource name for the script from where the function causing
1472 * the error originates.
1473 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001474 Local<Value> GetScriptResourceName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001475
1476 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001477 * Exception stack trace. By default stack traces are not captured for
1478 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1479 * to change this option.
1480 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001481 Local<StackTrace> GetStackTrace() const;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001482
1483 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001484 * Returns the number, 1-based, of the line where the error occurred.
1485 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001486 V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const);
1487 V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001488
1489 /**
1490 * Returns the index within the script of the first character where
1491 * the error occurred.
1492 */
1493 int GetStartPosition() const;
1494
1495 /**
1496 * Returns the index within the script of the last character where
1497 * the error occurred.
1498 */
1499 int GetEndPosition() const;
1500
1501 /**
1502 * Returns the index within the line of the first character where
1503 * the error occurred.
1504 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001505 V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const);
1506 V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001507
1508 /**
1509 * Returns the index within the line of the last character where
1510 * the error occurred.
1511 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001512 V8_DEPRECATED("Use maybe version", int GetEndColumn() const);
1513 V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 /**
1516 * Passes on the value set by the embedder when it fed the script from which
1517 * this Message was generated to V8.
1518 */
1519 bool IsSharedCrossOrigin() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001520 bool IsOpaque() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001521
Steve Blocka7e24c12009-10-30 11:49:00 +00001522 // TODO(1245381): Print to a string instead of on a FILE.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +01001524
1525 static const int kNoLineNumberInfo = 0;
1526 static const int kNoColumnInfo = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001527 static const int kNoScriptIdInfo = 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001528};
1529
1530
1531/**
1532 * Representation of a JavaScript stack trace. The information collected is a
1533 * snapshot of the execution stack and the information remains valid after
1534 * execution continues.
1535 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001536class V8_EXPORT StackTrace {
Kristian Monsen25f61362010-05-21 11:50:48 +01001537 public:
1538 /**
1539 * Flags that determine what information is placed captured for each
1540 * StackFrame when grabbing the current stack trace.
1541 */
1542 enum StackTraceOptions {
1543 kLineNumber = 1,
1544 kColumnOffset = 1 << 1 | kLineNumber,
1545 kScriptName = 1 << 2,
1546 kFunctionName = 1 << 3,
1547 kIsEval = 1 << 4,
1548 kIsConstructor = 1 << 5,
Ben Murdochf87a2032010-10-22 12:50:53 +01001549 kScriptNameOrSourceURL = 1 << 6,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001550 kScriptId = 1 << 7,
1551 kExposeFramesAcrossSecurityOrigins = 1 << 8,
Kristian Monsen25f61362010-05-21 11:50:48 +01001552 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
Ben Murdochf87a2032010-10-22 12:50:53 +01001553 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
Kristian Monsen25f61362010-05-21 11:50:48 +01001554 };
1555
1556 /**
1557 * Returns a StackFrame at a particular index.
1558 */
1559 Local<StackFrame> GetFrame(uint32_t index) const;
1560
1561 /**
1562 * Returns the number of StackFrames.
1563 */
1564 int GetFrameCount() const;
1565
1566 /**
1567 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1568 */
1569 Local<Array> AsArray();
1570
1571 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001572 * Grab a snapshot of the current JavaScript execution stack.
Kristian Monsen25f61362010-05-21 11:50:48 +01001573 *
1574 * \param frame_limit The maximum number of stack frames we want to capture.
1575 * \param options Enumerates the set of things we will capture for each
1576 * StackFrame.
1577 */
1578 static Local<StackTrace> CurrentStackTrace(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001579 Isolate* isolate,
Kristian Monsen25f61362010-05-21 11:50:48 +01001580 int frame_limit,
1581 StackTraceOptions options = kOverview);
1582};
1583
1584
1585/**
1586 * A single JavaScript stack frame.
1587 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588class V8_EXPORT StackFrame {
Kristian Monsen25f61362010-05-21 11:50:48 +01001589 public:
1590 /**
1591 * Returns the number, 1-based, of the line for the associate function call.
1592 * This method will return Message::kNoLineNumberInfo if it is unable to
1593 * retrieve the line number, or if kLineNumber was not passed as an option
1594 * when capturing the StackTrace.
1595 */
1596 int GetLineNumber() const;
1597
1598 /**
1599 * Returns the 1-based column offset on the line for the associated function
1600 * call.
1601 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1602 * the column number, or if kColumnOffset was not passed as an option when
1603 * capturing the StackTrace.
1604 */
1605 int GetColumn() const;
1606
1607 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001608 * Returns the id of the script for the function for this StackFrame.
1609 * This method will return Message::kNoScriptIdInfo if it is unable to
1610 * retrieve the script id, or if kScriptId was not passed as an option when
1611 * capturing the StackTrace.
1612 */
1613 int GetScriptId() const;
1614
1615 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001616 * Returns the name of the resource that contains the script for the
1617 * function for this StackFrame.
1618 */
1619 Local<String> GetScriptName() const;
1620
1621 /**
Ben Murdochf87a2032010-10-22 12:50:53 +01001622 * Returns the name of the resource that contains the script for the
1623 * function for this StackFrame or sourceURL value if the script name
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001624 * is undefined and its source ends with //# sourceURL=... string or
1625 * deprecated //@ sourceURL=... string.
Ben Murdochf87a2032010-10-22 12:50:53 +01001626 */
1627 Local<String> GetScriptNameOrSourceURL() const;
1628
1629 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001630 * Returns the name of the function associated with this stack frame.
1631 */
1632 Local<String> GetFunctionName() const;
1633
1634 /**
1635 * Returns whether or not the associated function is compiled via a call to
1636 * eval().
1637 */
1638 bool IsEval() const;
1639
1640 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001641 * Returns whether or not the associated function is called as a
Kristian Monsen25f61362010-05-21 11:50:48 +01001642 * constructor via "new".
1643 */
1644 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001645};
1646
1647
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001648// A StateTag represents a possible state of the VM.
1649enum StateTag { JS, GC, COMPILER, OTHER, EXTERNAL, IDLE };
1650
1651
1652// A RegisterState represents the current state of registers used
1653// by the sampling profiler API.
1654struct RegisterState {
1655 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {}
1656 void* pc; // Instruction pointer.
1657 void* sp; // Stack pointer.
1658 void* fp; // Frame pointer.
1659};
1660
1661
1662// The output structure filled up by GetStackSample API function.
1663struct SampleInfo {
1664 size_t frames_count;
1665 StateTag vm_state;
1666};
1667
1668
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669/**
1670 * A JSON Parser.
1671 */
1672class V8_EXPORT JSON {
1673 public:
1674 /**
1675 * Tries to parse the string |json_string| and returns it as value if
1676 * successful.
1677 *
1678 * \param json_string The string to parse.
1679 * \return The corresponding value if successfully parsed.
1680 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001681 static V8_DEPRECATED("Use maybe version",
1682 Local<Value> Parse(Local<String> json_string));
1683 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
1684 Isolate* isolate, Local<String> json_string);
1685};
1686
1687
1688/**
1689 * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap
1690 * but can be created without entering a v8::Context and hence shouldn't
1691 * escape to JavaScript.
1692 */
1693class V8_EXPORT NativeWeakMap : public Data {
1694 public:
1695 static Local<NativeWeakMap> New(Isolate* isolate);
1696 void Set(Local<Value> key, Local<Value> value);
1697 Local<Value> Get(Local<Value> key);
1698 bool Has(Local<Value> key);
1699 bool Delete(Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001700};
1701
1702
Ben Murdoch257744e2011-11-30 15:57:28 +00001703// --- Value ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001704
1705
1706/**
1707 * The superclass of all JavaScript values and objects.
1708 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709class V8_EXPORT Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00001710 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001711 /**
1712 * Returns true if this value is the undefined value. See ECMA-262
1713 * 4.3.10.
1714 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001715 V8_INLINE bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001716
1717 /**
1718 * Returns true if this value is the null value. See ECMA-262
1719 * 4.3.11.
1720 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721 V8_INLINE bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001722
1723 /**
1724 * Returns true if this value is true.
1725 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001726 bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001727
1728 /**
1729 * Returns true if this value is false.
1730 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001731 bool IsFalse() const;
1732
1733 /**
1734 * Returns true if this value is a symbol or a string.
1735 * This is an experimental feature.
1736 */
1737 bool IsName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001738
1739 /**
1740 * Returns true if this value is an instance of the String type.
1741 * See ECMA-262 8.4.
1742 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 V8_INLINE bool IsString() const;
1744
1745 /**
1746 * Returns true if this value is a symbol.
1747 * This is an experimental feature.
1748 */
1749 bool IsSymbol() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001750
1751 /**
1752 * Returns true if this value is a function.
1753 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001754 bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001755
1756 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001757 * Returns true if this value is an array. Note that it will return false for
1758 * an Proxy for an array.
Steve Blocka7e24c12009-10-30 11:49:00 +00001759 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001760 bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001761
1762 /**
1763 * Returns true if this value is an object.
1764 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001765 bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001766
1767 /**
1768 * Returns true if this value is boolean.
1769 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001770 bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001771
1772 /**
1773 * Returns true if this value is a number.
1774 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775 bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001776
1777 /**
1778 * Returns true if this value is external.
1779 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001780 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001781
1782 /**
1783 * Returns true if this value is a 32-bit signed integer.
1784 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001785 bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001786
1787 /**
Steve Block6ded16b2010-05-10 14:33:55 +01001788 * Returns true if this value is a 32-bit unsigned integer.
1789 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001790 bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +01001791
1792 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001793 * Returns true if this value is a Date.
1794 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001795 bool IsDate() const;
1796
1797 /**
1798 * Returns true if this value is an Arguments object.
1799 */
1800 bool IsArgumentsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001801
Iain Merrick75681382010-08-19 15:07:18 +01001802 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001803 * Returns true if this value is a Boolean object.
1804 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001805 bool IsBooleanObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001806
1807 /**
1808 * Returns true if this value is a Number object.
1809 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810 bool IsNumberObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001811
1812 /**
1813 * Returns true if this value is a String object.
1814 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 bool IsStringObject() const;
1816
1817 /**
1818 * Returns true if this value is a Symbol object.
1819 * This is an experimental feature.
1820 */
1821 bool IsSymbolObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001822
1823 /**
1824 * Returns true if this value is a NativeError.
1825 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001826 bool IsNativeError() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001827
1828 /**
Iain Merrick75681382010-08-19 15:07:18 +01001829 * Returns true if this value is a RegExp.
1830 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001831 bool IsRegExp() const;
Iain Merrick75681382010-08-19 15:07:18 +01001832
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001833 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001834 * Returns true if this value is a Generator function.
1835 * This is an experimental feature.
1836 */
1837 bool IsGeneratorFunction() const;
1838
1839 /**
1840 * Returns true if this value is a Generator object (iterator).
1841 * This is an experimental feature.
1842 */
1843 bool IsGeneratorObject() const;
1844
1845 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 * Returns true if this value is a Promise.
1847 * This is an experimental feature.
1848 */
1849 bool IsPromise() const;
1850
1851 /**
1852 * Returns true if this value is a Map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001853 */
1854 bool IsMap() const;
1855
1856 /**
1857 * Returns true if this value is a Set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001858 */
1859 bool IsSet() const;
1860
1861 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001862 * Returns true if this value is a Map Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001863 */
1864 bool IsMapIterator() const;
1865
1866 /**
1867 * Returns true if this value is a Set Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001868 */
1869 bool IsSetIterator() const;
1870
1871 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001872 * Returns true if this value is a WeakMap.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001873 */
1874 bool IsWeakMap() const;
1875
1876 /**
1877 * Returns true if this value is a WeakSet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001878 */
1879 bool IsWeakSet() const;
1880
1881 /**
1882 * Returns true if this value is an ArrayBuffer.
1883 * This is an experimental feature.
1884 */
1885 bool IsArrayBuffer() const;
1886
1887 /**
1888 * Returns true if this value is an ArrayBufferView.
1889 * This is an experimental feature.
1890 */
1891 bool IsArrayBufferView() const;
1892
1893 /**
1894 * Returns true if this value is one of TypedArrays.
1895 * This is an experimental feature.
1896 */
1897 bool IsTypedArray() const;
1898
1899 /**
1900 * Returns true if this value is an Uint8Array.
1901 * This is an experimental feature.
1902 */
1903 bool IsUint8Array() const;
1904
1905 /**
1906 * Returns true if this value is an Uint8ClampedArray.
1907 * This is an experimental feature.
1908 */
1909 bool IsUint8ClampedArray() const;
1910
1911 /**
1912 * Returns true if this value is an Int8Array.
1913 * This is an experimental feature.
1914 */
1915 bool IsInt8Array() const;
1916
1917 /**
1918 * Returns true if this value is an Uint16Array.
1919 * This is an experimental feature.
1920 */
1921 bool IsUint16Array() const;
1922
1923 /**
1924 * Returns true if this value is an Int16Array.
1925 * This is an experimental feature.
1926 */
1927 bool IsInt16Array() const;
1928
1929 /**
1930 * Returns true if this value is an Uint32Array.
1931 * This is an experimental feature.
1932 */
1933 bool IsUint32Array() const;
1934
1935 /**
1936 * Returns true if this value is an Int32Array.
1937 * This is an experimental feature.
1938 */
1939 bool IsInt32Array() const;
1940
1941 /**
1942 * Returns true if this value is a Float32Array.
1943 * This is an experimental feature.
1944 */
1945 bool IsFloat32Array() const;
1946
1947 /**
1948 * Returns true if this value is a Float64Array.
1949 * This is an experimental feature.
1950 */
1951 bool IsFloat64Array() const;
1952
1953 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001954 * Returns true if this value is a SIMD Float32x4.
1955 * This is an experimental feature.
1956 */
1957 bool IsFloat32x4() const;
1958
1959 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001960 * Returns true if this value is a DataView.
1961 * This is an experimental feature.
1962 */
1963 bool IsDataView() const;
1964
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001965 /**
1966 * Returns true if this value is a SharedArrayBuffer.
1967 * This is an experimental feature.
1968 */
1969 bool IsSharedArrayBuffer() const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001970
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001971 /**
1972 * Returns true if this value is a JavaScript Proxy.
1973 */
1974 bool IsProxy() const;
1975
1976
1977 V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean(
1978 Local<Context> context) const;
1979 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
1980 Local<Context> context) const;
1981 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
1982 Local<Context> context) const;
1983 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
1984 Local<Context> context) const;
1985 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
1986 Local<Context> context) const;
1987 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
1988 Local<Context> context) const;
1989 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
1990 Local<Context> context) const;
1991 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
1992
1993 V8_DEPRECATE_SOON("Use maybe version",
1994 Local<Boolean> ToBoolean(Isolate* isolate) const);
1995 V8_DEPRECATE_SOON("Use maybe version",
1996 Local<Number> ToNumber(Isolate* isolate) const);
1997 V8_DEPRECATE_SOON("Use maybe version",
1998 Local<String> ToString(Isolate* isolate) const);
1999 V8_DEPRECATED("Use maybe version",
2000 Local<String> ToDetailString(Isolate* isolate) const);
2001 V8_DEPRECATE_SOON("Use maybe version",
2002 Local<Object> ToObject(Isolate* isolate) const);
2003 V8_DEPRECATE_SOON("Use maybe version",
2004 Local<Integer> ToInteger(Isolate* isolate) const);
2005 V8_DEPRECATED("Use maybe version",
2006 Local<Uint32> ToUint32(Isolate* isolate) const);
2007 V8_DEPRECATE_SOON("Use maybe version",
2008 Local<Int32> ToInt32(Isolate* isolate) const);
2009
2010 inline V8_DEPRECATE_SOON("Use maybe version",
2011 Local<Boolean> ToBoolean() const);
2012 inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const);
2013 inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const);
2014 inline V8_DEPRECATED("Use maybe version",
2015 Local<String> ToDetailString() const);
2016 inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const);
2017 inline V8_DEPRECATE_SOON("Use maybe version",
2018 Local<Integer> ToInteger() const);
2019 inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const);
2020 inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002021
2022 /**
2023 * Attempts to convert a string to an array index.
2024 * Returns an empty handle if the conversion fails.
2025 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002026 V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const);
2027 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
2028 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002029
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002030 V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const;
2031 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
2032 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
2033 Local<Context> context) const;
2034 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
2035 Local<Context> context) const;
2036 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
2037
2038 V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const);
2039 V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const);
2040 V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const);
2041 V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const);
2042 V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002043
2044 /** JS == */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002045 V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const);
2046 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
2047 Local<Value> that) const;
2048 bool StrictEquals(Local<Value> that) const;
2049 bool SameValue(Local<Value> that) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002050
2051 template <class T> V8_INLINE static Value* Cast(T* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002052
Steve Blocka7e24c12009-10-30 11:49:00 +00002053 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002054 V8_INLINE bool QuickIsUndefined() const;
2055 V8_INLINE bool QuickIsNull() const;
2056 V8_INLINE bool QuickIsString() const;
2057 bool FullIsUndefined() const;
2058 bool FullIsNull() const;
2059 bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002060};
2061
2062
2063/**
2064 * The superclass of primitive values. See ECMA-262 4.3.2.
2065 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002066class V8_EXPORT Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +00002067
2068
2069/**
2070 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
2071 * or false value.
2072 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002073class V8_EXPORT Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002074 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002075 bool Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002076 V8_INLINE static Boolean* Cast(v8::Value* obj);
2077 V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value);
2078
2079 private:
2080 static void CheckCast(v8::Value* obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002081};
2082
2083
2084/**
2085 * A superclass for symbols and strings.
2086 */
2087class V8_EXPORT Name : public Primitive {
2088 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002089 /**
2090 * Returns the identity hash for this object. The current implementation
2091 * uses an inline property on the object to store the identity hash.
2092 *
2093 * The return value will never be 0. Also, it is not guaranteed to be
2094 * unique.
2095 */
2096 int GetIdentityHash();
2097
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002098 V8_INLINE static Name* Cast(v8::Value* obj);
2099 private:
2100 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002101};
2102
2103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002104enum class NewStringType { kNormal, kInternalized };
2105
2106
Steve Blocka7e24c12009-10-30 11:49:00 +00002107/**
2108 * A JavaScript string value (ECMA-262, 4.3.17).
2109 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110class V8_EXPORT String : public Name {
Steve Blocka7e24c12009-10-30 11:49:00 +00002111 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002112 static const int kMaxLength = (1 << 28) - 16;
2113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002114 enum Encoding {
2115 UNKNOWN_ENCODING = 0x1,
2116 TWO_BYTE_ENCODING = 0x0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002117 ONE_BYTE_ENCODING = 0x4
2118 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002119 /**
2120 * Returns the number of characters in this string.
2121 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002122 int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002123
2124 /**
2125 * Returns the number of bytes in the UTF-8 encoded
2126 * representation of this string.
2127 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002128 int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002129
2130 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002131 * Returns whether this string is known to contain only one byte data.
2132 * Does not read the string.
2133 * False negatives are possible.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002134 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002135 bool IsOneByte() const;
2136
2137 /**
2138 * Returns whether this string contain only one byte data.
2139 * Will read the entire string in some cases.
2140 */
2141 bool ContainsOnlyOneByte() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002142
2143 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002144 * Write the contents of the string to an external buffer.
2145 * If no arguments are given, expects the buffer to be large
2146 * enough to hold the entire string and NULL terminator. Copies
2147 * the contents of the string and the NULL terminator into the
2148 * buffer.
2149 *
Ben Murdochb0fe1622011-05-05 13:52:32 +01002150 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
2151 * before the end of the buffer.
2152 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002153 * Copies up to length characters into the output buffer.
2154 * Only null-terminates if there is enough space in the buffer.
2155 *
2156 * \param buffer The buffer into which the string will be copied.
2157 * \param start The starting position within the string at which
2158 * copying begins.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002159 * \param length The number of characters to copy from the string. For
2160 * WriteUtf8 the number of bytes in the buffer.
Steve Block6ded16b2010-05-10 14:33:55 +01002161 * \param nchars_ref The number of characters written, can be NULL.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002162 * \param options Various options that might affect performance of this or
Steve Block6ded16b2010-05-10 14:33:55 +01002163 * subsequent operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002164 * \return The number of characters copied to the buffer excluding the null
2165 * terminator. For WriteUtf8: The number of bytes copied to the buffer
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002166 * including the null terminator (if written).
Steve Blocka7e24c12009-10-30 11:49:00 +00002167 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002168 enum WriteOptions {
2169 NO_OPTIONS = 0,
2170 HINT_MANY_WRITES_EXPECTED = 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002171 NO_NULL_TERMINATION = 2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002172 PRESERVE_ONE_BYTE_NULL = 4,
2173 // Used by WriteUtf8 to replace orphan surrogate code units with the
2174 // unicode replacement character. Needs to be set to guarantee valid UTF-8
2175 // output.
2176 REPLACE_INVALID_UTF8 = 8
Steve Block6ded16b2010-05-10 14:33:55 +01002177 };
2178
Ben Murdoch589d6972011-11-30 16:04:58 +00002179 // 16-bit character codes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002180 int Write(uint16_t* buffer,
2181 int start = 0,
2182 int length = -1,
2183 int options = NO_OPTIONS) const;
2184 // One byte characters.
2185 int WriteOneByte(uint8_t* buffer,
2186 int start = 0,
2187 int length = -1,
2188 int options = NO_OPTIONS) const;
Ben Murdoch589d6972011-11-30 16:04:58 +00002189 // UTF-8 encoded characters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002190 int WriteUtf8(char* buffer,
2191 int length = -1,
2192 int* nchars_ref = NULL,
2193 int options = NO_OPTIONS) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002194
2195 /**
2196 * A zero length string.
2197 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002198 V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002199
2200 /**
2201 * Returns true if the string is external
2202 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002203 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002204
2205 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206 * Returns true if the string is both external and one-byte.
Steve Blocka7e24c12009-10-30 11:49:00 +00002207 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002208 bool IsExternalOneByte() const;
Leon Clarkee46be812010-01-19 14:06:41 +00002209
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002210 class V8_EXPORT ExternalStringResourceBase { // NOLINT
Leon Clarkee46be812010-01-19 14:06:41 +00002211 public:
2212 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002214 virtual bool IsCompressible() const { return false; }
2215
Leon Clarkee46be812010-01-19 14:06:41 +00002216 protected:
2217 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002218
2219 /**
2220 * Internally V8 will call this Dispose method when the external string
2221 * resource is no longer needed. The default implementation will use the
2222 * delete operator. This method can be overridden in subclasses to
2223 * control how allocated external string resources are disposed.
2224 */
2225 virtual void Dispose() { delete this; }
2226
Leon Clarkee46be812010-01-19 14:06:41 +00002227 private:
2228 // Disallow copying and assigning.
2229 ExternalStringResourceBase(const ExternalStringResourceBase&);
2230 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002231
2232 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00002233 };
2234
Steve Blocka7e24c12009-10-30 11:49:00 +00002235 /**
2236 * An ExternalStringResource is a wrapper around a two-byte string
2237 * buffer that resides outside V8's heap. Implement an
2238 * ExternalStringResource to manage the life cycle of the underlying
2239 * buffer. Note that the string data must be immutable.
2240 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002241 class V8_EXPORT ExternalStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002242 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002243 public:
2244 /**
2245 * Override the destructor to manage the life cycle of the underlying
2246 * buffer.
2247 */
2248 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002249
2250 /**
2251 * The string data from the underlying buffer.
2252 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002253 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002254
2255 /**
2256 * The length of the string. That is, the number of two-byte characters.
2257 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002258 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002259
Steve Blocka7e24c12009-10-30 11:49:00 +00002260 protected:
2261 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002262 };
2263
2264 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002265 * An ExternalOneByteStringResource is a wrapper around an one-byte
Steve Blocka7e24c12009-10-30 11:49:00 +00002266 * string buffer that resides outside V8's heap. Implement an
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002267 * ExternalOneByteStringResource to manage the life cycle of the
Steve Blocka7e24c12009-10-30 11:49:00 +00002268 * underlying buffer. Note that the string data must be immutable
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002269 * and that the data must be Latin-1 and not UTF-8, which would require
2270 * special treatment internally in the engine and do not allow efficient
2271 * indexing. Use String::New or convert to 16 bit data for non-Latin1.
Steve Blocka7e24c12009-10-30 11:49:00 +00002272 */
2273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002274 class V8_EXPORT ExternalOneByteStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002275 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002276 public:
2277 /**
2278 * Override the destructor to manage the life cycle of the underlying
2279 * buffer.
2280 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002281 virtual ~ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002282 /** The string data from the underlying buffer.*/
2283 virtual const char* data() const = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002284 /** The number of Latin-1 characters in the string.*/
Steve Blocka7e24c12009-10-30 11:49:00 +00002285 virtual size_t length() const = 0;
2286 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002287 ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002288 };
2289
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002290 /**
2291 * If the string is an external string, return the ExternalStringResourceBase
2292 * regardless of the encoding, otherwise return NULL. The encoding of the
2293 * string is returned in encoding_out.
2294 */
2295 V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase(
2296 Encoding* encoding_out) const;
2297
Steve Blocka7e24c12009-10-30 11:49:00 +00002298 /**
2299 * Get the ExternalStringResource for an external string. Returns
2300 * NULL if IsExternal() doesn't return true.
2301 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 V8_INLINE ExternalStringResource* GetExternalStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002303
2304 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002305 * Get the ExternalOneByteStringResource for an external one-byte string.
2306 * Returns NULL if IsExternalOneByte() doesn't return true.
Steve Blocka7e24c12009-10-30 11:49:00 +00002307 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002308 const ExternalOneByteStringResource* GetExternalOneByteStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002309
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002310 V8_INLINE static String* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002311
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002312 // TODO(dcarney): remove with deprecation of New functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002313 enum NewStringType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002314 kNormalString = static_cast<int>(v8::NewStringType::kNormal),
2315 kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002316 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002317
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002318 /** Allocates a new string from UTF-8 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002319 static V8_DEPRECATE_SOON(
2320 "Use maybe version",
2321 Local<String> NewFromUtf8(Isolate* isolate, const char* data,
2322 NewStringType type = kNormalString,
2323 int length = -1));
2324
2325 /** Allocates a new string from UTF-8 data. Only returns an empty value when
2326 * length > kMaxLength. **/
2327 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8(
2328 Isolate* isolate, const char* data, v8::NewStringType type,
2329 int length = -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002330
2331 /** Allocates a new string from Latin-1 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002332 static V8_DEPRECATED(
2333 "Use maybe version",
2334 Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data,
2335 NewStringType type = kNormalString,
2336 int length = -1));
2337
2338 /** Allocates a new string from Latin-1 data. Only returns an empty value
2339 * when length > kMaxLength. **/
2340 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte(
2341 Isolate* isolate, const uint8_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002342 int length = -1);
2343
2344 /** Allocates a new string from UTF-16 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002345 static V8_DEPRECATE_SOON(
2346 "Use maybe version",
2347 Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data,
2348 NewStringType type = kNormalString,
2349 int length = -1));
2350
2351 /** Allocates a new string from UTF-16 data. Only returns an empty value when
2352 * length > kMaxLength. **/
2353 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte(
2354 Isolate* isolate, const uint16_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002355 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002356
2357 /**
Steve Block3ce2e202009-11-05 08:53:23 +00002358 * Creates a new string by concatenating the left and the right strings
2359 * passed in as parameters.
2360 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002361 static Local<String> Concat(Local<String> left, Local<String> right);
Steve Block3ce2e202009-11-05 08:53:23 +00002362
2363 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002364 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002365 * resource. When the external string is no longer live on V8's heap the
2366 * resource will be disposed by calling its Dispose method. The caller of
2367 * this function should not otherwise delete or modify the resource. Neither
2368 * should the underlying buffer be deallocated or modified except through the
2369 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002370 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002371 static V8_DEPRECATED("Use maybe version",
2372 Local<String> NewExternal(
2373 Isolate* isolate, ExternalStringResource* resource));
2374 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte(
2375 Isolate* isolate, ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002376
2377 /**
2378 * Associate an external string resource with this string by transforming it
2379 * in place so that existing references to this string in the JavaScript heap
2380 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002381 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002382 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002383 * The string is not modified if the operation fails. See NewExternal for
2384 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002385 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002386 bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002387
2388 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002389 * Creates a new external string using the one-byte data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002390 * resource. When the external string is no longer live on V8's heap the
2391 * resource will be disposed by calling its Dispose method. The caller of
2392 * this function should not otherwise delete or modify the resource. Neither
2393 * should the underlying buffer be deallocated or modified except through the
2394 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002395 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002396 static V8_DEPRECATE_SOON(
2397 "Use maybe version",
2398 Local<String> NewExternal(Isolate* isolate,
2399 ExternalOneByteStringResource* resource));
2400 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte(
2401 Isolate* isolate, ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002402
2403 /**
2404 * Associate an external string resource with this string by transforming it
2405 * in place so that existing references to this string in the JavaScript heap
2406 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002407 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002408 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002409 * The string is not modified if the operation fails. See NewExternal for
2410 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002411 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002412 bool MakeExternal(ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002413
2414 /**
2415 * Returns true if this string can be made external.
2416 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002417 bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002418
2419 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002420 * Converts an object to a UTF-8-encoded character array. Useful if
Steve Blocka7e24c12009-10-30 11:49:00 +00002421 * you want to print the object. If conversion to a string fails
Ben Murdoch589d6972011-11-30 16:04:58 +00002422 * (e.g. due to an exception in the toString() method of the object)
Steve Blocka7e24c12009-10-30 11:49:00 +00002423 * then the length() method returns 0 and the * operator returns
2424 * NULL.
2425 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002426 class V8_EXPORT Utf8Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002427 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002428 explicit Utf8Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002429 ~Utf8Value();
2430 char* operator*() { return str_; }
2431 const char* operator*() const { return str_; }
2432 int length() const { return length_; }
2433 private:
2434 char* str_;
2435 int length_;
2436
2437 // Disallow copying and assigning.
2438 Utf8Value(const Utf8Value&);
2439 void operator=(const Utf8Value&);
2440 };
2441
2442 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002443 * Converts an object to a two-byte string.
2444 * If conversion to a string fails (eg. due to an exception in the toString()
2445 * method of the object) then the length() method returns 0 and the * operator
2446 * returns NULL.
2447 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002448 class V8_EXPORT Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002449 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002450 explicit Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002451 ~Value();
2452 uint16_t* operator*() { return str_; }
2453 const uint16_t* operator*() const { return str_; }
2454 int length() const { return length_; }
2455 private:
2456 uint16_t* str_;
2457 int length_;
2458
2459 // Disallow copying and assigning.
2460 Value(const Value&);
2461 void operator=(const Value&);
2462 };
Steve Block3ce2e202009-11-05 08:53:23 +00002463
Steve Blocka7e24c12009-10-30 11:49:00 +00002464 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002465 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
2466 Encoding encoding) const;
2467 void VerifyExternalStringResource(ExternalStringResource* val) const;
2468 static void CheckCast(v8::Value* obj);
2469};
2470
2471
2472/**
2473 * A JavaScript symbol (ECMA-262 edition 6)
2474 *
2475 * This is an experimental feature. Use at your own risk.
2476 */
2477class V8_EXPORT Symbol : public Name {
2478 public:
2479 // Returns the print name string of the symbol, or undefined if none.
2480 Local<Value> Name() const;
2481
2482 // Create a symbol. If name is not empty, it will be used as the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002483 static Local<Symbol> New(Isolate* isolate,
2484 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002485
2486 // Access global symbol registry.
2487 // Note that symbols created this way are never collected, so
2488 // they should only be used for statically fixed properties.
2489 // Also, there is only one global name space for the names used as keys.
2490 // To minimize the potential for clashes, use qualified names as keys.
2491 static Local<Symbol> For(Isolate *isolate, Local<String> name);
2492
2493 // Retrieve a global symbol. Similar to |For|, but using a separate
2494 // registry that is not accessible by (and cannot clash with) JavaScript code.
2495 static Local<Symbol> ForApi(Isolate *isolate, Local<String> name);
2496
2497 // Well-known symbols
2498 static Local<Symbol> GetIterator(Isolate* isolate);
2499 static Local<Symbol> GetUnscopables(Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002500 static Local<Symbol> GetToStringTag(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002501 static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002502
2503 V8_INLINE static Symbol* Cast(v8::Value* obj);
2504
2505 private:
2506 Symbol();
2507 static void CheckCast(v8::Value* obj);
2508};
2509
2510
2511/**
2512 * A private symbol
2513 *
2514 * This is an experimental feature. Use at your own risk.
2515 */
2516class V8_EXPORT Private : public Data {
2517 public:
2518 // Returns the print name string of the private symbol, or undefined if none.
2519 Local<Value> Name() const;
2520
2521 // Create a private symbol. If name is not empty, it will be the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002522 static Local<Private> New(Isolate* isolate,
2523 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002524
2525 // Retrieve a global private symbol. If a symbol with this name has not
2526 // been retrieved in the same isolate before, it is created.
2527 // Note that private symbols created this way are never collected, so
2528 // they should only be used for statically fixed properties.
2529 // Also, there is only one global name space for the names used as keys.
2530 // To minimize the potential for clashes, use qualified names as keys,
2531 // e.g., "Class#property".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002532 static Local<Private> ForApi(Isolate* isolate, Local<String> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002533
2534 private:
2535 Private();
Steve Blocka7e24c12009-10-30 11:49:00 +00002536};
2537
2538
2539/**
2540 * A JavaScript number value (ECMA-262, 4.3.20)
2541 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002542class V8_EXPORT Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002543 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002544 double Value() const;
2545 static Local<Number> New(Isolate* isolate, double value);
2546 V8_INLINE static Number* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002547 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002548 Number();
2549 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002550};
2551
2552
2553/**
2554 * A JavaScript value representing a signed integer.
2555 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002556class V8_EXPORT Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00002557 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002558 static Local<Integer> New(Isolate* isolate, int32_t value);
2559 static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value);
2560 int64_t Value() const;
2561 V8_INLINE static Integer* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002562 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002563 Integer();
2564 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002565};
2566
2567
2568/**
2569 * A JavaScript value representing a 32-bit signed integer.
2570 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571class V8_EXPORT Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002572 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002573 int32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002574 V8_INLINE static Int32* Cast(v8::Value* obj);
2575
Steve Blocka7e24c12009-10-30 11:49:00 +00002576 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002577 Int32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002578 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002579};
2580
2581
2582/**
2583 * A JavaScript value representing a 32-bit unsigned integer.
2584 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002585class V8_EXPORT Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587 uint32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002588 V8_INLINE static Uint32* Cast(v8::Value* obj);
2589
Steve Blocka7e24c12009-10-30 11:49:00 +00002590 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002591 Uint32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002592 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002593};
2594
2595
Steve Blocka7e24c12009-10-30 11:49:00 +00002596enum PropertyAttribute {
2597 None = 0,
2598 ReadOnly = 1 << 0,
2599 DontEnum = 1 << 1,
2600 DontDelete = 1 << 2
2601};
2602
2603/**
Leon Clarkef7060e22010-06-03 12:02:55 +01002604 * Accessor[Getter|Setter] are used as callback functions when
2605 * setting|getting a particular property. See Object and ObjectTemplate's
2606 * method SetAccessor.
2607 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002608typedef void (*AccessorGetterCallback)(
2609 Local<String> property,
2610 const PropertyCallbackInfo<Value>& info);
2611typedef void (*AccessorNameGetterCallback)(
2612 Local<Name> property,
2613 const PropertyCallbackInfo<Value>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002614
2615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002616typedef void (*AccessorSetterCallback)(
2617 Local<String> property,
2618 Local<Value> value,
2619 const PropertyCallbackInfo<void>& info);
2620typedef void (*AccessorNameSetterCallback)(
2621 Local<Name> property,
2622 Local<Value> value,
2623 const PropertyCallbackInfo<void>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002624
2625
2626/**
2627 * Access control specifications.
2628 *
2629 * Some accessors should be accessible across contexts. These
2630 * accessors have an explicit access control parameter which specifies
2631 * the kind of cross-context access that should be allowed.
2632 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002633 * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused.
Leon Clarkef7060e22010-06-03 12:02:55 +01002634 */
2635enum AccessControl {
2636 DEFAULT = 0,
2637 ALL_CAN_READ = 1,
2638 ALL_CAN_WRITE = 1 << 1,
2639 PROHIBITS_OVERWRITING = 1 << 2
2640};
2641
Ben Murdochda12d292016-06-02 14:46:10 +01002642/**
2643 * Integrity level for objects.
2644 */
2645enum class IntegrityLevel { kFrozen, kSealed };
Leon Clarkef7060e22010-06-03 12:02:55 +01002646
2647/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002648 * A JavaScript object (ECMA-262, 4.3.3)
2649 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002650class V8_EXPORT Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002651 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002652 V8_DEPRECATE_SOON("Use maybe version",
2653 bool Set(Local<Value> key, Local<Value> value));
2654 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
2655 Local<Value> key, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002656
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002657 V8_DEPRECATE_SOON("Use maybe version",
2658 bool Set(uint32_t index, Local<Value> value));
2659 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
2660 Local<Value> value);
2661
2662 // Implements CreateDataProperty (ECMA-262, 7.3.4).
2663 //
2664 // Defines a configurable, writable, enumerable property with the given value
2665 // on the object unless the property already exists and is not configurable
2666 // or the object is not extensible.
2667 //
2668 // Returns true on success.
2669 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2670 Local<Name> key,
2671 Local<Value> value);
2672 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2673 uint32_t index,
2674 Local<Value> value);
2675
2676 // Implements DefineOwnProperty.
2677 //
2678 // In general, CreateDataProperty will be faster, however, does not allow
2679 // for specifying attributes.
2680 //
2681 // Returns true on success.
2682 V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty(
2683 Local<Context> context, Local<Name> key, Local<Value> value,
2684 PropertyAttribute attributes = None);
Steve Block6ded16b2010-05-10 14:33:55 +01002685
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002686 // Sets an own property on this object bypassing interceptors and
Steve Blocka7e24c12009-10-30 11:49:00 +00002687 // overriding accessors or read-only properties.
2688 //
2689 // Note that if the object has an interceptor the property will be set
2690 // locally, but since the interceptor takes precedence the local property
2691 // will only be returned if the interceptor doesn't return a value.
2692 //
2693 // Note also that this only works for named properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002694 V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty",
2695 bool ForceSet(Local<Value> key, Local<Value> value,
2696 PropertyAttribute attribs = None));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002697 V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty",
2698 Maybe<bool> ForceSet(Local<Context> context,
2699 Local<Value> key, Local<Value> value,
2700 PropertyAttribute attribs = None));
Steve Blocka7e24c12009-10-30 11:49:00 +00002701
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002702 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
2703 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2704 Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002705
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002706 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2707 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2708 uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01002709
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002710 /**
2711 * Gets the property attributes of a property which can be None or
2712 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2713 * None when the property doesn't exist.
2714 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002715 V8_DEPRECATED("Use maybe version",
2716 PropertyAttribute GetPropertyAttributes(Local<Value> key));
2717 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes(
2718 Local<Context> context, Local<Value> key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002719
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002720 /**
2721 * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3.
2722 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002723 V8_DEPRECATED("Use maybe version",
2724 Local<Value> GetOwnPropertyDescriptor(Local<String> key));
2725 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor(
2726 Local<Context> context, Local<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002728 V8_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key));
2729 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
2730 Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002731
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002732 V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key));
2733 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2734 Maybe<bool> Delete(Local<Context> context, Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002736 V8_DEPRECATED("Use maybe version", bool Has(uint32_t index));
2737 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002738
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002739 V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index));
2740 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2741 Maybe<bool> Delete(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002742
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002743 V8_DEPRECATED("Use maybe version",
2744 bool SetAccessor(Local<String> name,
2745 AccessorGetterCallback getter,
2746 AccessorSetterCallback setter = 0,
2747 Local<Value> data = Local<Value>(),
2748 AccessControl settings = DEFAULT,
2749 PropertyAttribute attribute = None));
2750 V8_DEPRECATED("Use maybe version",
2751 bool SetAccessor(Local<Name> name,
2752 AccessorNameGetterCallback getter,
2753 AccessorNameSetterCallback setter = 0,
2754 Local<Value> data = Local<Value>(),
2755 AccessControl settings = DEFAULT,
2756 PropertyAttribute attribute = None));
2757 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2758 Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name,
2759 AccessorNameGetterCallback getter,
2760 AccessorNameSetterCallback setter = 0,
2761 MaybeLocal<Value> data = MaybeLocal<Value>(),
2762 AccessControl settings = DEFAULT,
2763 PropertyAttribute attribute = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00002764
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002765 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
2766 Local<Function> setter = Local<Function>(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002767 PropertyAttribute attribute = None,
2768 AccessControl settings = DEFAULT);
2769
2770 /**
2771 * Functionality for private properties.
2772 * This is an experimental feature, use at your own risk.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002773 * Note: Private properties are not inherited. Do not rely on this, since it
2774 * may change.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002775 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002776 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
2777 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
2778 Local<Value> value);
2779 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
2780 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
Leon Clarkef7060e22010-06-03 12:02:55 +01002781
Steve Blocka7e24c12009-10-30 11:49:00 +00002782 /**
2783 * Returns an array containing the names of the enumerable properties
2784 * of this object, including properties from prototype objects. The
2785 * array returned by this method contains the same values as would
2786 * be enumerated by a for-in statement over this object.
2787 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002788 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames());
2789 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
2790 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002791
2792 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002793 * This function has the same functionality as GetPropertyNames but
2794 * the returned array doesn't contain the names of properties from
2795 * prototype objects.
2796 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002797 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames());
2798 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2799 Local<Context> context);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002800
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);
2881 V8_DEPRECATE_SOON("Use maybe version",
2882 bool HasRealNamedProperty(Local<String> key));
2883 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
2884 Local<Name> key);
2885 V8_DEPRECATE_SOON("Use maybe version",
2886 bool HasRealIndexedProperty(uint32_t index));
2887 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty(
2888 Local<Context> context, uint32_t index);
2889 V8_DEPRECATE_SOON("Use maybe version",
2890 bool HasRealNamedCallbackProperty(Local<String> key));
2891 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty(
2892 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002893
2894 /**
2895 * If result.IsEmpty() no real property was located in the prototype chain.
2896 * This means interceptors in the prototype chain are not called.
2897 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002898 V8_DEPRECATED(
2899 "Use maybe version",
2900 Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key));
2901 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain(
2902 Local<Context> context, Local<Name> key);
2903
2904 /**
2905 * Gets the property attributes of a real property in the prototype chain,
2906 * which can be None or any combination of ReadOnly, DontEnum and DontDelete.
2907 * Interceptors in the prototype chain are not called.
2908 */
2909 V8_DEPRECATED(
2910 "Use maybe version",
2911 Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain(
2912 Local<String> key));
2913 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute>
2914 GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context,
2915 Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002916
2917 /**
2918 * If result.IsEmpty() no real property was located on the object or
2919 * in the prototype chain.
2920 * This means interceptors in the prototype chain are not called.
2921 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002922 V8_DEPRECATED("Use maybe version",
2923 Local<Value> GetRealNamedProperty(Local<String> key));
2924 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty(
2925 Local<Context> context, Local<Name> key);
2926
2927 /**
2928 * Gets the property attributes of a real property which can be
2929 * None or any combination of ReadOnly, DontEnum and DontDelete.
2930 * Interceptors in the prototype chain are not called.
2931 */
2932 V8_DEPRECATED("Use maybe version",
2933 Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2934 Local<String> key));
2935 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2936 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002937
2938 /** Tests for a named lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002939 bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002940
2941 /** Tests for an index lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002942 bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002943
2944 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002945 * Returns the identity hash for this object. The current implementation
2946 * uses a hidden property on the object to store the identity hash.
Steve Blocka7e24c12009-10-30 11:49:00 +00002947 *
2948 * The return value will never be 0. Also, it is not guaranteed to be
2949 * unique.
2950 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002951 int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00002952
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002953 V8_DEPRECATED("Use v8::Object::SetPrivate instead.",
2954 bool SetHiddenValue(Local<String> key, Local<Value> value));
2955 V8_DEPRECATED("Use v8::Object::GetPrivate instead.",
2956 Local<Value> GetHiddenValue(Local<String> key));
2957 V8_DEPRECATED("Use v8::Object::DeletePrivate instead.",
2958 bool DeleteHiddenValue(Local<String> key));
Steve Block3ce2e202009-11-05 08:53:23 +00002959
Steve Blocka7e24c12009-10-30 11:49:00 +00002960 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002961 * Clone this object with a fast but shallow copy. Values will point
2962 * to the same values as the original object.
2963 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002964 // TODO(dcarney): take an isolate and optionally bail out?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002965 Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00002966
2967 /**
Ben Murdoch8b112d22011-06-08 16:22:53 +01002968 * Returns the context in which the object was created.
2969 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002970 Local<Context> CreationContext();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002971
2972 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002973 * Checks whether a callback is set by the
2974 * ObjectTemplate::SetCallAsFunctionHandler method.
2975 * When an Object is callable this method returns true.
2976 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002977 bool IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00002978
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;
3180 V8_INLINE bool IsConstructCall() const;
3181 V8_INLINE Local<Value> Data() const;
3182 V8_INLINE Isolate* GetIsolate() const;
3183 V8_INLINE ReturnValue<T> GetReturnValue() const;
3184 // This shouldn't be public, but the arm compiler needs it.
3185 static const int kArgsLength = 7;
3186
3187 protected:
3188 friend class internal::FunctionCallbackArguments;
3189 friend class internal::CustomArguments<FunctionCallbackInfo>;
3190 static const int kHolderIndex = 0;
3191 static const int kIsolateIndex = 1;
3192 static const int kReturnValueDefaultValueIndex = 2;
3193 static const int kReturnValueIndex = 3;
3194 static const int kDataIndex = 4;
3195 static const int kCalleeIndex = 5;
3196 static const int kContextSaveIndex = 6;
3197
3198 V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args,
3199 internal::Object** values,
3200 int length,
3201 bool is_construct_call);
3202 internal::Object** implicit_args_;
3203 internal::Object** values_;
3204 int length_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003205 int is_construct_call_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003206};
3207
3208
3209/**
3210 * The information passed to a property callback about the context
3211 * of the property access.
3212 */
3213template<typename T>
3214class PropertyCallbackInfo {
3215 public:
3216 V8_INLINE Isolate* GetIsolate() const;
3217 V8_INLINE Local<Value> Data() const;
3218 V8_INLINE Local<Object> This() const;
3219 V8_INLINE Local<Object> Holder() const;
3220 V8_INLINE ReturnValue<T> GetReturnValue() const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003221 V8_INLINE bool ShouldThrowOnError() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003222 // This shouldn't be public, but the arm compiler needs it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003223 static const int kArgsLength = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003224
3225 protected:
3226 friend class MacroAssembler;
3227 friend class internal::PropertyCallbackArguments;
3228 friend class internal::CustomArguments<PropertyCallbackInfo>;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003229 static const int kShouldThrowOnErrorIndex = 0;
3230 static const int kHolderIndex = 1;
3231 static const int kIsolateIndex = 2;
3232 static const int kReturnValueDefaultValueIndex = 3;
3233 static const int kReturnValueIndex = 4;
3234 static const int kDataIndex = 5;
3235 static const int kThisIndex = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003236
3237 V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {}
3238 internal::Object** args_;
3239};
3240
3241
3242typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
3243
3244
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 Murdoch4a90d5f2016-03-22 12:00:34 +00003254 static MaybeLocal<Function> New(Local<Context> context,
3255 FunctionCallback callback,
3256 Local<Value> data = Local<Value>(),
3257 int length = 0);
3258 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 /**
3459 * 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 *
3463 * This API is experimental and may change significantly.
3464 */
3465 class V8_EXPORT Allocator { // NOLINT
3466 public:
3467 virtual ~Allocator() {}
3468
3469 /**
3470 * Allocate |length| bytes. Return NULL if allocation is not successful.
3471 * Memory should be initialized to zeroes.
3472 */
3473 virtual void* Allocate(size_t length) = 0;
3474
3475 /**
3476 * Allocate |length| bytes. Return NULL if allocation is not successful.
3477 * Memory does not have to be initialized.
3478 */
3479 virtual void* AllocateUninitialized(size_t length) = 0;
3480 /**
3481 * Free the memory block of size |length|, pointed to by |data|.
3482 * That memory is guaranteed to be previously allocated by |Allocate|.
3483 */
3484 virtual void Free(void* data, size_t length) = 0;
3485 };
3486
3487 /**
3488 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
3489 * returns an instance of this class, populated, with a pointer to data
3490 * and byte length.
3491 *
3492 * The Data pointer of ArrayBuffer::Contents is always allocated with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003493 * Allocator::Allocate that is set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003494 *
3495 * This API is experimental and may change significantly.
3496 */
3497 class V8_EXPORT Contents { // NOLINT
3498 public:
3499 Contents() : data_(NULL), byte_length_(0) {}
3500
3501 void* Data() const { return data_; }
3502 size_t ByteLength() const { return byte_length_; }
3503
3504 private:
3505 void* data_;
3506 size_t byte_length_;
3507
3508 friend class ArrayBuffer;
3509 };
3510
3511
3512 /**
3513 * Data length in bytes.
3514 */
3515 size_t ByteLength() const;
3516
3517 /**
3518 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
3519 * Allocated memory will be owned by a created ArrayBuffer and
3520 * will be deallocated when it is garbage-collected,
3521 * unless the object is externalized.
3522 */
3523 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
3524
3525 /**
3526 * Create a new ArrayBuffer over an existing memory block.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003527 * The created array buffer is by default immediately in externalized state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003528 * The memory block will not be reclaimed when a created ArrayBuffer
3529 * is garbage-collected.
3530 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003531 static Local<ArrayBuffer> New(
3532 Isolate* isolate, void* data, size_t byte_length,
3533 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003534
3535 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003536 * Returns true if ArrayBuffer is externalized, that is, does not
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003537 * own its memory block.
3538 */
3539 bool IsExternal() const;
3540
3541 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003542 * Returns true if this ArrayBuffer may be neutered.
3543 */
3544 bool IsNeuterable() const;
3545
3546 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003547 * Neuters this ArrayBuffer and all its views (typed arrays).
3548 * Neutering sets the byte length of the buffer and all typed arrays to zero,
3549 * preventing JavaScript from ever accessing underlying backing store.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003550 * ArrayBuffer should have been externalized and must be neuterable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003551 */
3552 void Neuter();
3553
3554 /**
3555 * Make this ArrayBuffer external. The pointer to underlying memory block
3556 * and byte length are returned as |Contents| structure. After ArrayBuffer
3557 * had been etxrenalized, it does no longer owns the memory block. The caller
3558 * should take steps to free memory when it is no longer needed.
3559 *
3560 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003561 * that has been set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003562 */
3563 Contents Externalize();
3564
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003565 /**
3566 * Get a pointer to the ArrayBuffer's underlying memory block without
3567 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3568 * will become invalid as soon as the ArrayBuffer became garbage collected.
3569 *
3570 * The embedder should make sure to hold a strong reference to the
3571 * ArrayBuffer while accessing this pointer.
3572 *
3573 * The memory block is guaranteed to be allocated with |Allocator::Allocate|.
3574 */
3575 Contents GetContents();
3576
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003577 V8_INLINE static ArrayBuffer* Cast(Value* obj);
3578
3579 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3580
3581 private:
3582 ArrayBuffer();
3583 static void CheckCast(Value* obj);
3584};
3585
3586
3587#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
3588// The number of required internal fields can be defined by embedder.
3589#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
3590#endif
3591
3592
3593/**
3594 * A base class for an instance of one of "views" over ArrayBuffer,
3595 * including TypedArrays and DataView (ES6 draft 15.13).
3596 *
3597 * This API is experimental and may change significantly.
3598 */
3599class V8_EXPORT ArrayBufferView : public Object {
3600 public:
3601 /**
3602 * Returns underlying ArrayBuffer.
3603 */
3604 Local<ArrayBuffer> Buffer();
3605 /**
3606 * Byte offset in |Buffer|.
3607 */
3608 size_t ByteOffset();
3609 /**
3610 * Size of a view in bytes.
3611 */
3612 size_t ByteLength();
3613
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003614 /**
3615 * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3616 * memory without additional overhead that calling ArrayBufferView::Buffer
3617 * might incur.
3618 *
3619 * Will write at most min(|byte_length|, ByteLength) bytes starting at
3620 * ByteOffset of the underling buffer to the memory starting at |dest|.
3621 * Returns the number of bytes actually written.
3622 */
3623 size_t CopyContents(void* dest, size_t byte_length);
3624
3625 /**
3626 * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3627 * allocated.
3628 */
3629 bool HasBuffer() const;
3630
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003631 V8_INLINE static ArrayBufferView* Cast(Value* obj);
3632
3633 static const int kInternalFieldCount =
3634 V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3635
3636 private:
3637 ArrayBufferView();
3638 static void CheckCast(Value* obj);
3639};
3640
3641
3642/**
3643 * A base class for an instance of TypedArray series of constructors
3644 * (ES6 draft 15.13.6).
3645 * This API is experimental and may change significantly.
3646 */
3647class V8_EXPORT TypedArray : public ArrayBufferView {
3648 public:
3649 /**
3650 * Number of elements in this typed array
3651 * (e.g. for Int16Array, |ByteLength|/2).
3652 */
3653 size_t Length();
3654
3655 V8_INLINE static TypedArray* Cast(Value* obj);
3656
3657 private:
3658 TypedArray();
3659 static void CheckCast(Value* obj);
3660};
3661
3662
3663/**
3664 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
3665 * This API is experimental and may change significantly.
3666 */
3667class V8_EXPORT Uint8Array : public TypedArray {
3668 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003669 static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
3670 size_t byte_offset, size_t length);
3671 static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003672 size_t byte_offset, size_t length);
3673 V8_INLINE static Uint8Array* Cast(Value* obj);
3674
3675 private:
3676 Uint8Array();
3677 static void CheckCast(Value* obj);
3678};
3679
3680
3681/**
3682 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
3683 * This API is experimental and may change significantly.
3684 */
3685class V8_EXPORT Uint8ClampedArray : public TypedArray {
3686 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003687 static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
3688 size_t byte_offset, size_t length);
3689 static Local<Uint8ClampedArray> New(
3690 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
3691 size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003692 V8_INLINE static Uint8ClampedArray* Cast(Value* obj);
3693
3694 private:
3695 Uint8ClampedArray();
3696 static void CheckCast(Value* obj);
3697};
3698
3699/**
3700 * An instance of Int8Array constructor (ES6 draft 15.13.6).
3701 * This API is experimental and may change significantly.
3702 */
3703class V8_EXPORT Int8Array : public TypedArray {
3704 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003705 static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
3706 size_t byte_offset, size_t length);
3707 static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3708 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003709 V8_INLINE static Int8Array* Cast(Value* obj);
3710
3711 private:
3712 Int8Array();
3713 static void CheckCast(Value* obj);
3714};
3715
3716
3717/**
3718 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
3719 * This API is experimental and may change significantly.
3720 */
3721class V8_EXPORT Uint16Array : public TypedArray {
3722 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003723 static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
3724 size_t byte_offset, size_t length);
3725 static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3726 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003727 V8_INLINE static Uint16Array* Cast(Value* obj);
3728
3729 private:
3730 Uint16Array();
3731 static void CheckCast(Value* obj);
3732};
3733
3734
3735/**
3736 * An instance of Int16Array constructor (ES6 draft 15.13.6).
3737 * This API is experimental and may change significantly.
3738 */
3739class V8_EXPORT Int16Array : public TypedArray {
3740 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003741 static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
3742 size_t byte_offset, size_t length);
3743 static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003744 size_t byte_offset, size_t length);
3745 V8_INLINE static Int16Array* Cast(Value* obj);
3746
3747 private:
3748 Int16Array();
3749 static void CheckCast(Value* obj);
3750};
3751
3752
3753/**
3754 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
3755 * This API is experimental and may change significantly.
3756 */
3757class V8_EXPORT Uint32Array : public TypedArray {
3758 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003759 static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
3760 size_t byte_offset, size_t length);
3761 static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3762 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003763 V8_INLINE static Uint32Array* Cast(Value* obj);
3764
3765 private:
3766 Uint32Array();
3767 static void CheckCast(Value* obj);
3768};
3769
3770
3771/**
3772 * An instance of Int32Array constructor (ES6 draft 15.13.6).
3773 * This API is experimental and may change significantly.
3774 */
3775class V8_EXPORT Int32Array : public TypedArray {
3776 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003777 static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
3778 size_t byte_offset, size_t length);
3779 static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003780 size_t byte_offset, size_t length);
3781 V8_INLINE static Int32Array* Cast(Value* obj);
3782
3783 private:
3784 Int32Array();
3785 static void CheckCast(Value* obj);
3786};
3787
3788
3789/**
3790 * An instance of Float32Array constructor (ES6 draft 15.13.6).
3791 * This API is experimental and may change significantly.
3792 */
3793class V8_EXPORT Float32Array : public TypedArray {
3794 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003795 static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
3796 size_t byte_offset, size_t length);
3797 static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3798 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799 V8_INLINE static Float32Array* Cast(Value* obj);
3800
3801 private:
3802 Float32Array();
3803 static void CheckCast(Value* obj);
3804};
3805
3806
3807/**
3808 * An instance of Float64Array constructor (ES6 draft 15.13.6).
3809 * This API is experimental and may change significantly.
3810 */
3811class V8_EXPORT Float64Array : public TypedArray {
3812 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003813 static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
3814 size_t byte_offset, size_t length);
3815 static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3816 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003817 V8_INLINE static Float64Array* Cast(Value* obj);
3818
3819 private:
3820 Float64Array();
3821 static void CheckCast(Value* obj);
3822};
3823
3824
3825/**
3826 * An instance of DataView constructor (ES6 draft 15.13.7).
3827 * This API is experimental and may change significantly.
3828 */
3829class V8_EXPORT DataView : public ArrayBufferView {
3830 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003831 static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3832 size_t byte_offset, size_t length);
3833 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003834 size_t byte_offset, size_t length);
3835 V8_INLINE static DataView* Cast(Value* obj);
3836
3837 private:
3838 DataView();
3839 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003840};
3841
3842
3843/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003844 * An instance of the built-in SharedArrayBuffer constructor.
3845 * This API is experimental and may change significantly.
3846 */
3847class V8_EXPORT SharedArrayBuffer : public Object {
3848 public:
3849 /**
3850 * The contents of an |SharedArrayBuffer|. Externalization of
3851 * |SharedArrayBuffer| returns an instance of this class, populated, with a
3852 * pointer to data and byte length.
3853 *
3854 * The Data pointer of SharedArrayBuffer::Contents is always allocated with
3855 * |ArrayBuffer::Allocator::Allocate| by the allocator specified in
3856 * v8::Isolate::CreateParams::array_buffer_allocator.
3857 *
3858 * This API is experimental and may change significantly.
3859 */
3860 class V8_EXPORT Contents { // NOLINT
3861 public:
3862 Contents() : data_(NULL), byte_length_(0) {}
3863
3864 void* Data() const { return data_; }
3865 size_t ByteLength() const { return byte_length_; }
3866
3867 private:
3868 void* data_;
3869 size_t byte_length_;
3870
3871 friend class SharedArrayBuffer;
3872 };
3873
3874
3875 /**
3876 * Data length in bytes.
3877 */
3878 size_t ByteLength() const;
3879
3880 /**
3881 * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3882 * Allocated memory will be owned by a created SharedArrayBuffer and
3883 * will be deallocated when it is garbage-collected,
3884 * unless the object is externalized.
3885 */
3886 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3887
3888 /**
3889 * Create a new SharedArrayBuffer over an existing memory block. The created
3890 * array buffer is immediately in externalized state unless otherwise
3891 * specified. The memory block will not be reclaimed when a created
3892 * SharedArrayBuffer is garbage-collected.
3893 */
3894 static Local<SharedArrayBuffer> New(
3895 Isolate* isolate, void* data, size_t byte_length,
3896 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
3897
3898 /**
3899 * Returns true if SharedArrayBuffer is externalized, that is, does not
3900 * own its memory block.
3901 */
3902 bool IsExternal() const;
3903
3904 /**
3905 * Make this SharedArrayBuffer external. The pointer to underlying memory
3906 * block and byte length are returned as |Contents| structure. After
3907 * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory
3908 * block. The caller should take steps to free memory when it is no longer
3909 * needed.
3910 *
3911 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3912 * by the allocator specified in
3913 * v8::Isolate::CreateParams::array_buffer_allocator.
3914 *
3915 */
3916 Contents Externalize();
3917
3918 /**
3919 * Get a pointer to the ArrayBuffer's underlying memory block without
3920 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3921 * will become invalid as soon as the ArrayBuffer became garbage collected.
3922 *
3923 * The embedder should make sure to hold a strong reference to the
3924 * ArrayBuffer while accessing this pointer.
3925 *
3926 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3927 * by the allocator specified in
3928 * v8::Isolate::CreateParams::array_buffer_allocator.
3929 */
3930 Contents GetContents();
3931
3932 V8_INLINE static SharedArrayBuffer* Cast(Value* obj);
3933
3934 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3935
3936 private:
3937 SharedArrayBuffer();
3938 static void CheckCast(Value* obj);
3939};
3940
3941
3942/**
Ben Murdoch257744e2011-11-30 15:57:28 +00003943 * An instance of the built-in Date constructor (ECMA-262, 15.9).
3944 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003945class V8_EXPORT Date : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00003946 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003947 static V8_DEPRECATE_SOON("Use maybe version.",
3948 Local<Value> New(Isolate* isolate, double time));
3949 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
3950 double time);
Ben Murdoch257744e2011-11-30 15:57:28 +00003951
3952 /**
3953 * A specialization of Value::NumberValue that is more efficient
3954 * because we know the structure of this object.
3955 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003956 double ValueOf() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00003957
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003958 V8_INLINE static Date* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003959
3960 /**
3961 * Notification that the embedder has changed the time zone,
3962 * daylight savings time, or other date / time configuration
3963 * parameters. V8 keeps a cache of various values used for
3964 * date / time computation. This notification will reset
3965 * those cached values for the current context so that date /
3966 * time configuration changes would be reflected in the Date
3967 * object.
3968 *
3969 * This API should not be called more than needed as it will
3970 * negatively impact the performance of date operations.
3971 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003972 static void DateTimeConfigurationChangeNotification(Isolate* isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00003973
3974 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003975 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003976};
3977
3978
3979/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003980 * A Number object (ECMA-262, 4.3.21).
3981 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003982class V8_EXPORT NumberObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003983 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003984 static Local<Value> New(Isolate* isolate, double value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003985
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003986 double ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003987
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003988 V8_INLINE static NumberObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003989
3990 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003991 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003992};
3993
3994
3995/**
3996 * A Boolean object (ECMA-262, 4.3.15).
3997 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003998class V8_EXPORT BooleanObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003999 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004000 static Local<Value> New(Isolate* isolate, bool value);
4001 V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004002
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004003 bool ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004005 V8_INLINE static BooleanObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004006
4007 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004008 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004009};
4010
4011
4012/**
4013 * A String object (ECMA-262, 4.3.18).
4014 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004015class V8_EXPORT StringObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004016 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004017 static Local<Value> New(Local<String> value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004018
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004019 Local<String> ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004020
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004021 V8_INLINE static StringObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004022
4023 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004024 static void CheckCast(v8::Value* obj);
4025};
4026
4027
4028/**
4029 * A Symbol object (ECMA-262 edition 6).
4030 *
4031 * This is an experimental feature. Use at your own risk.
4032 */
4033class V8_EXPORT SymbolObject : public Object {
4034 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004035 static Local<Value> New(Isolate* isolate, Local<Symbol> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004036
4037 Local<Symbol> ValueOf() const;
4038
4039 V8_INLINE static SymbolObject* Cast(v8::Value* obj);
4040
4041 private:
4042 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004043};
4044
4045
4046/**
Ben Murdoch257744e2011-11-30 15:57:28 +00004047 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
4048 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004049class V8_EXPORT RegExp : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00004050 public:
4051 /**
4052 * Regular expression flag bits. They can be or'ed to enable a set
4053 * of flags.
4054 */
4055 enum Flags {
4056 kNone = 0,
4057 kGlobal = 1,
4058 kIgnoreCase = 2,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004059 kMultiline = 4,
4060 kSticky = 8,
4061 kUnicode = 16
Ben Murdoch257744e2011-11-30 15:57:28 +00004062 };
4063
4064 /**
4065 * Creates a regular expression from the given pattern string and
4066 * the flags bit field. May throw a JavaScript exception as
4067 * described in ECMA-262, 15.10.4.1.
4068 *
4069 * For example,
4070 * RegExp::New(v8::String::New("foo"),
4071 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
4072 * is equivalent to evaluating "/foo/gm".
4073 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004074 static V8_DEPRECATE_SOON("Use maybe version",
4075 Local<RegExp> New(Local<String> pattern,
4076 Flags flags));
4077 static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
4078 Local<String> pattern,
4079 Flags flags);
Ben Murdoch257744e2011-11-30 15:57:28 +00004080
4081 /**
4082 * Returns the value of the source property: a string representing
4083 * the regular expression.
4084 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004085 Local<String> GetSource() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004086
4087 /**
4088 * Returns the flags bit field.
4089 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004090 Flags GetFlags() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004091
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004092 V8_INLINE static RegExp* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004093
4094 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004095 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004096};
4097
4098
4099/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004100 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
4101 * to associate C++ data structures with JavaScript objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00004102 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004103class V8_EXPORT External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00004104 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004105 static Local<External> New(Isolate* isolate, void* value);
4106 V8_INLINE static External* Cast(Value* obj);
4107 void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00004108 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004109 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004110};
4111
4112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004113#define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator)
4114
4115enum Intrinsic {
4116#define V8_DECL_INTRINSIC(name, iname) k##name,
4117 V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
4118#undef V8_DECL_INTRINSIC
4119};
4120
4121
Ben Murdoch257744e2011-11-30 15:57:28 +00004122// --- Templates ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004123
4124
4125/**
4126 * The superclass of object and function templates.
4127 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004128class V8_EXPORT Template : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004129 public:
4130 /** Adds a property to each instance created by this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004131 void Set(Local<Name> name, Local<Data> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00004132 PropertyAttribute attributes = None);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004133 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004134
4135 void SetAccessorProperty(
4136 Local<Name> name,
4137 Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
4138 Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
4139 PropertyAttribute attribute = None,
4140 AccessControl settings = DEFAULT);
4141
4142 /**
4143 * Whenever the property with the given name is accessed on objects
4144 * created from this Template the getter and setter callbacks
4145 * are called instead of getting and setting the property directly
4146 * on the JavaScript object.
4147 *
4148 * \param name The name of the property for which an accessor is added.
4149 * \param getter The callback to invoke when getting the property.
4150 * \param setter The callback to invoke when setting the property.
4151 * \param data A piece of data that will be passed to the getter and setter
4152 * callbacks whenever they are invoked.
4153 * \param settings Access control settings for the accessor. This is a bit
4154 * field consisting of one of more of
4155 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4156 * The default is to not allow cross-context access.
4157 * ALL_CAN_READ means that all cross-context reads are allowed.
4158 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4159 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4160 * cross-context access.
4161 * \param attribute The attributes of the property for which an accessor
4162 * is added.
4163 * \param signature The signature describes valid receivers for the accessor
4164 * and is used to perform implicit instance checks against them. If the
4165 * receiver is incompatible (i.e. is not an instance of the constructor as
4166 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4167 * thrown and no callback is invoked.
4168 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004169 void SetNativeDataProperty(
4170 Local<String> name, AccessorGetterCallback getter,
4171 AccessorSetterCallback setter = 0,
4172 // TODO(dcarney): gcc can't handle Local below
4173 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4174 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4175 AccessControl settings = DEFAULT);
4176 void SetNativeDataProperty(
4177 Local<Name> name, AccessorNameGetterCallback getter,
4178 AccessorNameSetterCallback setter = 0,
4179 // TODO(dcarney): gcc can't handle Local below
4180 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4181 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4182 AccessControl settings = DEFAULT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004184 /**
4185 * During template instantiation, sets the value with the intrinsic property
4186 * from the correct context.
4187 */
4188 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
4189 PropertyAttribute attribute = None);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004190
Steve Blocka7e24c12009-10-30 11:49:00 +00004191 private:
4192 Template();
4193
4194 friend class ObjectTemplate;
4195 friend class FunctionTemplate;
4196};
4197
4198
4199/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004200 * NamedProperty[Getter|Setter] are used as interceptors on object.
4201 * See ObjectTemplate::SetNamedPropertyHandler.
4202 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004203typedef void (*NamedPropertyGetterCallback)(
4204 Local<String> property,
4205 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004206
4207
4208/**
4209 * Returns the value if the setter intercepts the request.
4210 * Otherwise, returns an empty handle.
4211 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212typedef void (*NamedPropertySetterCallback)(
4213 Local<String> property,
4214 Local<Value> value,
4215 const PropertyCallbackInfo<Value>& info);
4216
Steve Blocka7e24c12009-10-30 11:49:00 +00004217
Steve Blocka7e24c12009-10-30 11:49:00 +00004218/**
4219 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004220 * The result is an integer encoding property attributes (like v8::None,
4221 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00004222 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004223typedef void (*NamedPropertyQueryCallback)(
4224 Local<String> property,
4225 const PropertyCallbackInfo<Integer>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004226
4227
4228/**
4229 * Returns a non-empty handle if the deleter intercepts the request.
4230 * The return value is true if the property could be deleted and false
4231 * otherwise.
4232 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004233typedef void (*NamedPropertyDeleterCallback)(
4234 Local<String> property,
4235 const PropertyCallbackInfo<Boolean>& info);
4236
Steve Blocka7e24c12009-10-30 11:49:00 +00004237
4238/**
4239 * Returns an array containing the names of the properties the named
4240 * property getter intercepts.
4241 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004242typedef void (*NamedPropertyEnumeratorCallback)(
4243 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004244
4245
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004246// TODO(dcarney): Deprecate and remove previous typedefs, and replace
4247// GenericNamedPropertyFooCallback with just NamedPropertyFooCallback.
4248/**
4249 * GenericNamedProperty[Getter|Setter] are used as interceptors on object.
4250 * See ObjectTemplate::SetNamedPropertyHandler.
4251 */
4252typedef void (*GenericNamedPropertyGetterCallback)(
4253 Local<Name> property, const PropertyCallbackInfo<Value>& info);
4254
4255
4256/**
4257 * Returns the value if the setter intercepts the request.
4258 * Otherwise, returns an empty handle.
4259 */
4260typedef void (*GenericNamedPropertySetterCallback)(
4261 Local<Name> property, Local<Value> value,
4262 const PropertyCallbackInfo<Value>& info);
4263
4264
4265/**
4266 * Returns a non-empty handle if the interceptor intercepts the request.
4267 * The result is an integer encoding property attributes (like v8::None,
4268 * v8::DontEnum, etc.)
4269 */
4270typedef void (*GenericNamedPropertyQueryCallback)(
4271 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
4272
4273
4274/**
4275 * Returns a non-empty handle if the deleter intercepts the request.
4276 * The return value is true if the property could be deleted and false
4277 * otherwise.
4278 */
4279typedef void (*GenericNamedPropertyDeleterCallback)(
4280 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
4281
4282
4283/**
4284 * Returns an array containing the names of the properties the named
4285 * property getter intercepts.
4286 */
4287typedef void (*GenericNamedPropertyEnumeratorCallback)(
4288 const PropertyCallbackInfo<Array>& info);
4289
4290
Steve Blocka7e24c12009-10-30 11:49:00 +00004291/**
4292 * Returns the value of the property if the getter intercepts the
4293 * request. Otherwise, returns an empty handle.
4294 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004295typedef void (*IndexedPropertyGetterCallback)(
4296 uint32_t index,
4297 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004298
4299
4300/**
4301 * Returns the value if the setter intercepts the request.
4302 * Otherwise, returns an empty handle.
4303 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004304typedef void (*IndexedPropertySetterCallback)(
4305 uint32_t index,
4306 Local<Value> value,
4307 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004308
4309
4310/**
4311 * Returns a non-empty handle if the interceptor intercepts the request.
Iain Merrick75681382010-08-19 15:07:18 +01004312 * The result is an integer encoding property attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004313 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004314typedef void (*IndexedPropertyQueryCallback)(
4315 uint32_t index,
4316 const PropertyCallbackInfo<Integer>& info);
4317
Steve Blocka7e24c12009-10-30 11:49:00 +00004318
4319/**
4320 * Returns a non-empty handle if the deleter intercepts the request.
4321 * The return value is true if the property could be deleted and false
4322 * otherwise.
4323 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004324typedef void (*IndexedPropertyDeleterCallback)(
4325 uint32_t index,
4326 const PropertyCallbackInfo<Boolean>& info);
4327
Steve Blocka7e24c12009-10-30 11:49:00 +00004328
4329/**
4330 * Returns an array containing the indices of the properties the
4331 * indexed property getter intercepts.
4332 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004333typedef void (*IndexedPropertyEnumeratorCallback)(
4334 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004335
4336
4337/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004338 * Access type specification.
4339 */
4340enum AccessType {
4341 ACCESS_GET,
4342 ACCESS_SET,
4343 ACCESS_HAS,
4344 ACCESS_DELETE,
4345 ACCESS_KEYS
4346};
4347
4348
4349/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004350 * Returns true if the given context should be allowed to access the given
4351 * object.
4352 */
4353typedef bool (*AccessCheckCallback)(Local<Context> accessing_context,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004354 Local<Object> accessed_object,
4355 Local<Value> data);
4356typedef bool (*DeprecatedAccessCheckCallback)(Local<Context> accessing_context,
4357 Local<Object> accessed_object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004358
4359/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004360 * Returns true if cross-context access should be allowed to the named
4361 * property with the given key on the host object.
4362 */
4363typedef bool (*NamedSecurityCallback)(Local<Object> host,
4364 Local<Value> key,
4365 AccessType type,
4366 Local<Value> data);
4367
4368
4369/**
4370 * Returns true if cross-context access should be allowed to the indexed
4371 * property with the given index on the host object.
4372 */
4373typedef bool (*IndexedSecurityCallback)(Local<Object> host,
4374 uint32_t index,
4375 AccessType type,
4376 Local<Value> data);
4377
4378
4379/**
4380 * A FunctionTemplate is used to create functions at runtime. There
4381 * can only be one function created from a FunctionTemplate in a
4382 * context. The lifetime of the created function is equal to the
4383 * lifetime of the context. So in case the embedder needs to create
4384 * temporary functions that can be collected using Scripts is
4385 * preferred.
4386 *
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004387 * Any modification of a FunctionTemplate after first instantiation will trigger
4388 *a crash.
4389 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004390 * A FunctionTemplate can have properties, these properties are added to the
4391 * function object when it is created.
4392 *
4393 * A FunctionTemplate has a corresponding instance template which is
4394 * used to create object instances when the function is used as a
4395 * constructor. Properties added to the instance template are added to
4396 * each object instance.
4397 *
4398 * A FunctionTemplate can have a prototype template. The prototype template
4399 * is used to create the prototype object of the function.
4400 *
4401 * The following example shows how to use a FunctionTemplate:
4402 *
4403 * \code
4404 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
4405 * t->Set("func_property", v8::Number::New(1));
4406 *
4407 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
4408 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
4409 * proto_t->Set("proto_const", v8::Number::New(2));
4410 *
4411 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
4412 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
4413 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
4414 * instance_t->Set("instance_property", Number::New(3));
4415 *
4416 * v8::Local<v8::Function> function = t->GetFunction();
4417 * v8::Local<v8::Object> instance = function->NewInstance();
4418 * \endcode
4419 *
4420 * Let's use "function" as the JS variable name of the function object
4421 * and "instance" for the instance object created above. The function
4422 * and the instance will have the following properties:
4423 *
4424 * \code
4425 * func_property in function == true;
4426 * function.func_property == 1;
4427 *
4428 * function.prototype.proto_method() invokes 'InvokeCallback'
4429 * function.prototype.proto_const == 2;
4430 *
4431 * instance instanceof function == true;
4432 * instance.instance_accessor calls 'InstanceAccessorCallback'
4433 * instance.instance_property == 3;
4434 * \endcode
4435 *
4436 * A FunctionTemplate can inherit from another one by calling the
4437 * FunctionTemplate::Inherit method. The following graph illustrates
4438 * the semantics of inheritance:
4439 *
4440 * \code
4441 * FunctionTemplate Parent -> Parent() . prototype -> { }
4442 * ^ ^
4443 * | Inherit(Parent) | .__proto__
4444 * | |
4445 * FunctionTemplate Child -> Child() . prototype -> { }
4446 * \endcode
4447 *
4448 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
4449 * object of the Child() function has __proto__ pointing to the
4450 * Parent() function's prototype object. An instance of the Child
4451 * function has all properties on Parent's instance templates.
4452 *
4453 * Let Parent be the FunctionTemplate initialized in the previous
4454 * section and create a Child FunctionTemplate by:
4455 *
4456 * \code
4457 * Local<FunctionTemplate> parent = t;
4458 * Local<FunctionTemplate> child = FunctionTemplate::New();
4459 * child->Inherit(parent);
4460 *
4461 * Local<Function> child_function = child->GetFunction();
4462 * Local<Object> child_instance = child_function->NewInstance();
4463 * \endcode
4464 *
4465 * The Child function and Child instance will have the following
4466 * properties:
4467 *
4468 * \code
4469 * child_func.prototype.__proto__ == function.prototype;
4470 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
4471 * child_instance.instance_property == 3;
4472 * \endcode
4473 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004474class V8_EXPORT FunctionTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004475 public:
4476 /** Creates a function template.*/
4477 static Local<FunctionTemplate> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004478 Isolate* isolate, FunctionCallback callback = 0,
4479 Local<Value> data = Local<Value>(),
4480 Local<Signature> signature = Local<Signature>(), int length = 0);
4481
4482 /**
4483 * Creates a function template with a fast handler. If a fast handler is set,
4484 * the callback cannot be null.
4485 */
4486 static Local<FunctionTemplate> NewWithFastHandler(
4487 Isolate* isolate, FunctionCallback callback,
4488 experimental::FastAccessorBuilder* fast_handler = nullptr,
4489 Local<Value> data = Local<Value>(),
4490 Local<Signature> signature = Local<Signature>(), int length = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004491
Steve Blocka7e24c12009-10-30 11:49:00 +00004492 /** Returns the unique function instance in the current execution context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004493 V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction());
4494 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
4495 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004496
4497 /**
4498 * Set the call-handler callback for a FunctionTemplate. This
4499 * callback is called whenever the function created from this
4500 * FunctionTemplate is called.
4501 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004502 void SetCallHandler(
4503 FunctionCallback callback, Local<Value> data = Local<Value>(),
4504 experimental::FastAccessorBuilder* fast_handler = nullptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00004505
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004506 /** Set the predefined length property for the FunctionTemplate. */
4507 void SetLength(int length);
4508
Steve Blocka7e24c12009-10-30 11:49:00 +00004509 /** Get the InstanceTemplate. */
4510 Local<ObjectTemplate> InstanceTemplate();
4511
4512 /** Causes the function template to inherit from a parent function template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004513 void Inherit(Local<FunctionTemplate> parent);
Steve Blocka7e24c12009-10-30 11:49:00 +00004514
4515 /**
4516 * A PrototypeTemplate is the template used to create the prototype object
4517 * of the function created by this template.
4518 */
4519 Local<ObjectTemplate> PrototypeTemplate();
4520
Steve Blocka7e24c12009-10-30 11:49:00 +00004521 /**
4522 * Set the class name of the FunctionTemplate. This is used for
4523 * printing objects created with the function created from the
4524 * FunctionTemplate as its constructor.
4525 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004526 void SetClassName(Local<String> name);
4527
4528
4529 /**
4530 * When set to true, no access check will be performed on the receiver of a
4531 * function call. Currently defaults to true, but this is subject to change.
4532 */
4533 void SetAcceptAnyReceiver(bool value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004534
4535 /**
4536 * Determines whether the __proto__ accessor ignores instances of
4537 * the function template. If instances of the function template are
4538 * ignored, __proto__ skips all instances and instead returns the
4539 * next object in the prototype chain.
4540 *
4541 * Call with a value of true to make the __proto__ accessor ignore
4542 * instances of the function template. Call with a value of false
4543 * to make the __proto__ accessor not ignore instances of the
4544 * function template. By default, instances of a function template
4545 * are not ignored.
4546 */
4547 void SetHiddenPrototype(bool value);
4548
4549 /**
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004550 * Sets the ReadOnly flag in the attributes of the 'prototype' property
4551 * of functions created from this FunctionTemplate to true.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004552 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004553 void ReadOnlyPrototype();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004554
4555 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004556 * Removes the prototype property from functions created from this
4557 * FunctionTemplate.
4558 */
4559 void RemovePrototype();
4560
4561 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00004562 * Returns true if the given object is an instance of this function
4563 * template.
4564 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004565 bool HasInstance(Local<Value> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004566
4567 private:
4568 FunctionTemplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004569 friend class Context;
4570 friend class ObjectTemplate;
4571};
4572
4573
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004574enum class PropertyHandlerFlags {
4575 kNone = 0,
4576 // See ALL_CAN_READ above.
4577 kAllCanRead = 1,
4578 // Will not call into interceptor for properties on the receiver or prototype
4579 // chain. Currently only valid for named interceptors.
4580 kNonMasking = 1 << 1,
4581 // Will not call into interceptor for symbol lookup. Only meaningful for
4582 // named interceptors.
4583 kOnlyInterceptStrings = 1 << 2,
4584};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004585
4586
4587struct NamedPropertyHandlerConfiguration {
4588 NamedPropertyHandlerConfiguration(
4589 /** Note: getter is required **/
4590 GenericNamedPropertyGetterCallback getter = 0,
4591 GenericNamedPropertySetterCallback setter = 0,
4592 GenericNamedPropertyQueryCallback query = 0,
4593 GenericNamedPropertyDeleterCallback deleter = 0,
4594 GenericNamedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004595 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004596 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4597 : getter(getter),
4598 setter(setter),
4599 query(query),
4600 deleter(deleter),
4601 enumerator(enumerator),
4602 data(data),
4603 flags(flags) {}
4604
4605 GenericNamedPropertyGetterCallback getter;
4606 GenericNamedPropertySetterCallback setter;
4607 GenericNamedPropertyQueryCallback query;
4608 GenericNamedPropertyDeleterCallback deleter;
4609 GenericNamedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004610 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004611 PropertyHandlerFlags flags;
4612};
4613
4614
4615struct IndexedPropertyHandlerConfiguration {
4616 IndexedPropertyHandlerConfiguration(
4617 /** Note: getter is required **/
4618 IndexedPropertyGetterCallback getter = 0,
4619 IndexedPropertySetterCallback setter = 0,
4620 IndexedPropertyQueryCallback query = 0,
4621 IndexedPropertyDeleterCallback deleter = 0,
4622 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004623 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004624 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4625 : getter(getter),
4626 setter(setter),
4627 query(query),
4628 deleter(deleter),
4629 enumerator(enumerator),
4630 data(data),
4631 flags(flags) {}
4632
4633 IndexedPropertyGetterCallback getter;
4634 IndexedPropertySetterCallback setter;
4635 IndexedPropertyQueryCallback query;
4636 IndexedPropertyDeleterCallback deleter;
4637 IndexedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004638 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004639 PropertyHandlerFlags flags;
4640};
4641
4642
Steve Blocka7e24c12009-10-30 11:49:00 +00004643/**
4644 * An ObjectTemplate is used to create objects at runtime.
4645 *
4646 * Properties added to an ObjectTemplate are added to each object
4647 * created from the ObjectTemplate.
4648 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004649class V8_EXPORT ObjectTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004650 public:
4651 /** Creates an ObjectTemplate. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004652 static Local<ObjectTemplate> New(
4653 Isolate* isolate,
4654 Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
4655 static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New());
Steve Blocka7e24c12009-10-30 11:49:00 +00004656
4657 /** Creates a new instance of this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004658 V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance());
4659 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004660
4661 /**
4662 * Sets an accessor on the object template.
4663 *
4664 * Whenever the property with the given name is accessed on objects
4665 * created from this ObjectTemplate the getter and setter callbacks
4666 * are called instead of getting and setting the property directly
4667 * on the JavaScript object.
4668 *
4669 * \param name The name of the property for which an accessor is added.
4670 * \param getter The callback to invoke when getting the property.
4671 * \param setter The callback to invoke when setting the property.
4672 * \param data A piece of data that will be passed to the getter and setter
4673 * callbacks whenever they are invoked.
4674 * \param settings Access control settings for the accessor. This is a bit
4675 * field consisting of one of more of
4676 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4677 * The default is to not allow cross-context access.
4678 * ALL_CAN_READ means that all cross-context reads are allowed.
4679 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4680 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4681 * cross-context access.
4682 * \param attribute The attributes of the property for which an accessor
4683 * is added.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004684 * \param signature The signature describes valid receivers for the accessor
4685 * and is used to perform implicit instance checks against them. If the
4686 * receiver is incompatible (i.e. is not an instance of the constructor as
4687 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4688 * thrown and no callback is invoked.
Steve Blocka7e24c12009-10-30 11:49:00 +00004689 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004690 void SetAccessor(
4691 Local<String> name, AccessorGetterCallback getter,
4692 AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4693 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4694 Local<AccessorSignature> signature = Local<AccessorSignature>());
4695 void SetAccessor(
4696 Local<Name> name, AccessorNameGetterCallback getter,
4697 AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4698 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4699 Local<AccessorSignature> signature = Local<AccessorSignature>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004700
4701 /**
4702 * Sets a named property handler on the object template.
4703 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004704 * Whenever a property whose name is a string is accessed on objects created
4705 * from this object template, the provided callback is invoked instead of
Steve Blocka7e24c12009-10-30 11:49:00 +00004706 * accessing the property directly on the JavaScript object.
4707 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004708 * Note that new code should use the second version that can intercept
4709 * symbol-named properties as well as string-named properties.
4710 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004711 * \param getter The callback to invoke when getting a property.
4712 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004713 * \param query The callback to invoke to check if a property is present,
4714 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004715 * \param deleter The callback to invoke when deleting a property.
4716 * \param enumerator The callback to invoke to enumerate all the named
4717 * properties of an object.
4718 * \param data A piece of data that will be passed to the callbacks
4719 * whenever they are invoked.
4720 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004721 // TODO(dcarney): deprecate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004722 void SetNamedPropertyHandler(NamedPropertyGetterCallback getter,
4723 NamedPropertySetterCallback setter = 0,
4724 NamedPropertyQueryCallback query = 0,
4725 NamedPropertyDeleterCallback deleter = 0,
4726 NamedPropertyEnumeratorCallback enumerator = 0,
4727 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004728 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
Steve Blocka7e24c12009-10-30 11:49:00 +00004729
4730 /**
4731 * Sets an indexed property handler on the object template.
4732 *
4733 * Whenever an indexed property is accessed on objects created from
4734 * this object template, the provided callback is invoked instead of
4735 * accessing the property directly on the JavaScript object.
4736 *
4737 * \param getter The callback to invoke when getting a property.
4738 * \param setter The callback to invoke when setting a property.
Ben Murdoch257744e2011-11-30 15:57:28 +00004739 * \param query The callback to invoke to check if an object has a property.
Steve Blocka7e24c12009-10-30 11:49:00 +00004740 * \param deleter The callback to invoke when deleting a property.
4741 * \param enumerator The callback to invoke to enumerate all the indexed
4742 * properties of an object.
4743 * \param data A piece of data that will be passed to the callbacks
4744 * whenever they are invoked.
4745 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004746 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
4747 // TODO(dcarney): deprecate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004748 void SetIndexedPropertyHandler(
4749 IndexedPropertyGetterCallback getter,
4750 IndexedPropertySetterCallback setter = 0,
4751 IndexedPropertyQueryCallback query = 0,
4752 IndexedPropertyDeleterCallback deleter = 0,
4753 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004754 Local<Value> data = Local<Value>()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004755 SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
4756 deleter, enumerator, data));
4757 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004758 /**
4759 * Sets the callback to be used when calling instances created from
4760 * this template as a function. If no callback is set, instances
4761 * behave like normal JavaScript objects that cannot be called as a
4762 * function.
4763 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004764 void SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004765 Local<Value> data = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004766
4767 /**
4768 * Mark object instances of the template as undetectable.
4769 *
4770 * In many ways, undetectable objects behave as though they are not
4771 * there. They behave like 'undefined' in conditionals and when
4772 * printed. However, properties can be accessed and called as on
4773 * normal objects.
4774 */
4775 void MarkAsUndetectable();
4776
4777 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004778 * Sets access check callback on the object template and enables access
4779 * checks.
Steve Blocka7e24c12009-10-30 11:49:00 +00004780 *
4781 * When accessing properties on instances of this object template,
4782 * the access check callback will be called to determine whether or
4783 * not to allow cross-context access to the properties.
Steve Blocka7e24c12009-10-30 11:49:00 +00004784 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004785 void SetAccessCheckCallback(AccessCheckCallback callback,
4786 Local<Value> data = Local<Value>());
Ben Murdoch097c5b22016-05-18 11:27:45 +01004787 V8_DEPRECATED(
4788 "Use SetAccessCheckCallback with new AccessCheckCallback signature.",
4789 void SetAccessCheckCallback(DeprecatedAccessCheckCallback callback,
4790 Local<Value> data = Local<Value>()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004791
4792 V8_DEPRECATED(
4793 "Use SetAccessCheckCallback instead",
4794 void SetAccessCheckCallbacks(NamedSecurityCallback named_handler,
4795 IndexedSecurityCallback indexed_handler,
4796 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00004797
4798 /**
4799 * Gets the number of internal fields for objects generated from
4800 * this template.
4801 */
4802 int InternalFieldCount();
4803
4804 /**
4805 * Sets the number of internal fields for objects generated from
4806 * this template.
4807 */
4808 void SetInternalFieldCount(int value);
4809
4810 private:
4811 ObjectTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004812 static Local<ObjectTemplate> New(internal::Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004813 Local<FunctionTemplate> constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +00004814 friend class FunctionTemplate;
4815};
4816
4817
4818/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004819 * A Signature specifies which receiver is valid for a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004820 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004821class V8_EXPORT Signature : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004822 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004823 static Local<Signature> New(
4824 Isolate* isolate,
4825 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004826
Steve Blocka7e24c12009-10-30 11:49:00 +00004827 private:
4828 Signature();
4829};
4830
4831
4832/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004833 * An AccessorSignature specifies which receivers are valid parameters
4834 * to an accessor callback.
4835 */
4836class V8_EXPORT AccessorSignature : public Data {
4837 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004838 static Local<AccessorSignature> New(
4839 Isolate* isolate,
4840 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004841
4842 private:
4843 AccessorSignature();
4844};
4845
4846
Ben Murdoch257744e2011-11-30 15:57:28 +00004847// --- Extensions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004848
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004849class V8_EXPORT ExternalOneByteStringResourceImpl
4850 : public String::ExternalOneByteStringResource {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004851 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004852 ExternalOneByteStringResourceImpl() : data_(0), length_(0) {}
4853 ExternalOneByteStringResourceImpl(const char* data, size_t length)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004854 : data_(data), length_(length) {}
4855 const char* data() const { return data_; }
4856 size_t length() const { return length_; }
4857
4858 private:
4859 const char* data_;
4860 size_t length_;
4861};
Steve Blocka7e24c12009-10-30 11:49:00 +00004862
4863/**
4864 * Ignore
4865 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004866class V8_EXPORT Extension { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00004867 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004868 // Note that the strings passed into this constructor must live as long
4869 // as the Extension itself.
Steve Blocka7e24c12009-10-30 11:49:00 +00004870 Extension(const char* name,
4871 const char* source = 0,
4872 int dep_count = 0,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004873 const char** deps = 0,
4874 int source_length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00004875 virtual ~Extension() { }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004876 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
4877 v8::Isolate* isolate, v8::Local<v8::String> name) {
4878 return v8::Local<v8::FunctionTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004879 }
4880
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004881 const char* name() const { return name_; }
4882 size_t source_length() const { return source_length_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004883 const String::ExternalOneByteStringResource* source() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004884 return &source_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004885 int dependency_count() { return dep_count_; }
4886 const char** dependencies() { return deps_; }
4887 void set_auto_enable(bool value) { auto_enable_ = value; }
4888 bool auto_enable() { return auto_enable_; }
4889
4890 private:
4891 const char* name_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004892 size_t source_length_; // expected to initialize before source_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004893 ExternalOneByteStringResourceImpl source_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004894 int dep_count_;
4895 const char** deps_;
4896 bool auto_enable_;
4897
4898 // Disallow copying and assigning.
4899 Extension(const Extension&);
4900 void operator=(const Extension&);
4901};
4902
4903
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004904void V8_EXPORT RegisterExtension(Extension* extension);
Steve Blocka7e24c12009-10-30 11:49:00 +00004905
4906
Ben Murdoch257744e2011-11-30 15:57:28 +00004907// --- Statics ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004908
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004909V8_INLINE Local<Primitive> Undefined(Isolate* isolate);
4910V8_INLINE Local<Primitive> Null(Isolate* isolate);
4911V8_INLINE Local<Boolean> True(Isolate* isolate);
4912V8_INLINE Local<Boolean> False(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004913
Steve Blocka7e24c12009-10-30 11:49:00 +00004914/**
4915 * A set of constraints that specifies the limits of the runtime's memory use.
4916 * You must set the heap size before initializing the VM - the size cannot be
4917 * adjusted after the VM is initialized.
4918 *
4919 * If you are using threads then you should hold the V8::Locker lock while
4920 * setting the stack limit and you must set a non-default stack limit separately
4921 * for each thread.
Ben Murdochda12d292016-06-02 14:46:10 +01004922 *
4923 * The arguments for set_max_semi_space_size, set_max_old_space_size,
4924 * set_max_executable_size, set_code_range_size specify limits in MB.
Steve Blocka7e24c12009-10-30 11:49:00 +00004925 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004926class V8_EXPORT ResourceConstraints {
Steve Blocka7e24c12009-10-30 11:49:00 +00004927 public:
4928 ResourceConstraints();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004929
4930 /**
4931 * Configures the constraints with reasonable default values based on the
4932 * capabilities of the current device the VM is running on.
4933 *
4934 * \param physical_memory The total amount of physical memory on the current
4935 * device, in bytes.
4936 * \param virtual_memory_limit The amount of virtual memory on the current
4937 * device, in bytes, or zero, if there is no limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004938 */
4939 void ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004940 uint64_t virtual_memory_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004941
4942 int max_semi_space_size() const { return max_semi_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004943 void set_max_semi_space_size(int limit_in_mb) {
4944 max_semi_space_size_ = limit_in_mb;
4945 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004946 int max_old_space_size() const { return max_old_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004947 void set_max_old_space_size(int limit_in_mb) {
4948 max_old_space_size_ = limit_in_mb;
4949 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004950 int max_executable_size() const { return max_executable_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004951 void set_max_executable_size(int limit_in_mb) {
4952 max_executable_size_ = limit_in_mb;
4953 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004954 uint32_t* stack_limit() const { return stack_limit_; }
4955 // Sets an address beyond which the VM's stack may not grow.
4956 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004957 size_t code_range_size() const { return code_range_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004958 void set_code_range_size(size_t limit_in_mb) {
4959 code_range_size_ = limit_in_mb;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004960 }
4961
Steve Blocka7e24c12009-10-30 11:49:00 +00004962 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004963 int max_semi_space_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004964 int max_old_space_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08004965 int max_executable_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004966 uint32_t* stack_limit_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004967 size_t code_range_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004968};
4969
4970
Ben Murdoch257744e2011-11-30 15:57:28 +00004971// --- Exceptions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004972
4973
4974typedef void (*FatalErrorCallback)(const char* location, const char* message);
4975
4976
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004977typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
Steve Blocka7e24c12009-10-30 11:49:00 +00004978
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004979// --- Tracing ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004980
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004981typedef void (*LogEventCallback)(const char* name, int event);
Steve Blocka7e24c12009-10-30 11:49:00 +00004982
4983/**
4984 * Create new error objects by calling the corresponding error object
4985 * constructor with the message.
4986 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004987class V8_EXPORT Exception {
Steve Blocka7e24c12009-10-30 11:49:00 +00004988 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004989 static Local<Value> RangeError(Local<String> message);
4990 static Local<Value> ReferenceError(Local<String> message);
4991 static Local<Value> SyntaxError(Local<String> message);
4992 static Local<Value> TypeError(Local<String> message);
4993 static Local<Value> Error(Local<String> message);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004994
4995 /**
4996 * Creates an error message for the given exception.
4997 * Will try to reconstruct the original stack trace from the exception value,
4998 * or capture the current stack trace if not available.
4999 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005000 static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
5001 V8_DEPRECATED("Use version with an Isolate*",
5002 static Local<Message> CreateMessage(Local<Value> exception));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005003
5004 /**
5005 * Returns the original stack trace that was captured at the creation time
5006 * of a given exception, or an empty handle if not available.
5007 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005008 static Local<StackTrace> GetStackTrace(Local<Value> exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00005009};
5010
5011
Ben Murdoch257744e2011-11-30 15:57:28 +00005012// --- Counters Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005013
5014typedef int* (*CounterLookupCallback)(const char* name);
5015
5016typedef void* (*CreateHistogramCallback)(const char* name,
5017 int min,
5018 int max,
5019 size_t buckets);
5020
5021typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
5022
Ben Murdoch257744e2011-11-30 15:57:28 +00005023// --- Memory Allocation Callback ---
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005024enum ObjectSpace {
5025 kObjectSpaceNewSpace = 1 << 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005026 kObjectSpaceOldSpace = 1 << 1,
5027 kObjectSpaceCodeSpace = 1 << 2,
5028 kObjectSpaceMapSpace = 1 << 3,
5029 kObjectSpaceLoSpace = 1 << 4,
5030 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldSpace |
5031 kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
5032 kObjectSpaceLoSpace
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005033};
Iain Merrick9ac36c92010-09-13 15:29:50 +01005034
5035 enum AllocationAction {
5036 kAllocationActionAllocate = 1 << 0,
5037 kAllocationActionFree = 1 << 1,
5038 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
5039 };
5040
5041typedef void (*MemoryAllocationCallback)(ObjectSpace space,
5042 AllocationAction action,
5043 int size);
5044
Ben Murdoch097c5b22016-05-18 11:27:45 +01005045// --- Enter/Leave Script Callback ---
5046typedef void (*BeforeCallEnteredCallback)(Isolate*);
5047typedef void (*CallCompletedCallback)(Isolate*);
5048typedef void (*DeprecatedCallCompletedCallback)();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005049
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005050// --- Promise Reject Callback ---
5051enum PromiseRejectEvent {
5052 kPromiseRejectWithNoHandler = 0,
5053 kPromiseHandlerAddedAfterReject = 1
5054};
5055
5056class PromiseRejectMessage {
5057 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005058 PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event,
5059 Local<Value> value, Local<StackTrace> stack_trace)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005060 : promise_(promise),
5061 event_(event),
5062 value_(value),
5063 stack_trace_(stack_trace) {}
5064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005065 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005066 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005067 V8_INLINE Local<Value> GetValue() const { return value_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005068
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005069 V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()",
5070 V8_INLINE Local<StackTrace> GetStackTrace() const) {
5071 return stack_trace_;
5072 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005073
5074 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005075 Local<Promise> promise_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005076 PromiseRejectEvent event_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005077 Local<Value> value_;
5078 Local<StackTrace> stack_trace_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005079};
5080
5081typedef void (*PromiseRejectCallback)(PromiseRejectMessage message);
5082
Ben Murdochda12d292016-06-02 14:46:10 +01005083// --- Microtasks Callbacks ---
5084typedef void (*MicrotasksCompletedCallback)(Isolate*);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005085typedef void (*MicrotaskCallback)(void* data);
5086
Ben Murdochda12d292016-06-02 14:46:10 +01005087
5088/**
5089 * Policy for running microtasks:
5090 * - explicit: microtasks are invoked with Isolate::RunMicrotasks() method;
5091 * - scoped: microtasks invocation is controlled by MicrotasksScope objects;
5092 * - auto: microtasks are invoked when the script call depth decrements
5093 * to zero.
5094 */
5095enum class MicrotasksPolicy { kExplicit, kScoped, kAuto };
5096
5097
5098/**
5099 * This scope is used to control microtasks when kScopeMicrotasksInvocation
5100 * is used on Isolate. In this mode every non-primitive call to V8 should be
5101 * done inside some MicrotasksScope.
5102 * Microtasks are executed when topmost MicrotasksScope marked as kRunMicrotasks
5103 * exits.
5104 * kDoNotRunMicrotasks should be used to annotate calls not intended to trigger
5105 * microtasks.
5106 */
5107class V8_EXPORT MicrotasksScope {
5108 public:
5109 enum Type { kRunMicrotasks, kDoNotRunMicrotasks };
5110
5111 MicrotasksScope(Isolate* isolate, Type type);
5112 ~MicrotasksScope();
5113
5114 /**
5115 * Runs microtasks if no kRunMicrotasks scope is currently active.
5116 */
5117 static void PerformCheckpoint(Isolate* isolate);
5118
5119 /**
5120 * Returns current depth of nested kRunMicrotasks scopes.
5121 */
5122 static int GetCurrentDepth(Isolate* isolate);
5123
5124 private:
5125 internal::Isolate* const isolate_;
5126 bool run_;
5127
5128 // Prevent copying.
5129 MicrotasksScope(const MicrotasksScope&);
5130 MicrotasksScope& operator=(const MicrotasksScope&);
5131};
5132
5133
Ben Murdoch257744e2011-11-30 15:57:28 +00005134// --- Failed Access Check Callback ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005135typedef void (*FailedAccessCheckCallback)(Local<Object> target,
5136 AccessType type,
5137 Local<Value> data);
5138
Ben Murdoch257744e2011-11-30 15:57:28 +00005139// --- AllowCodeGenerationFromStrings callbacks ---
5140
5141/**
5142 * Callback to check if code generation from strings is allowed. See
5143 * Context::AllowCodeGenerationFromStrings.
5144 */
5145typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
5146
5147// --- Garbage Collection Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005148
5149/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005150 * Applications can register callback functions which will be called before and
5151 * after certain garbage collection operations. Allocations are not allowed in
5152 * the callback functions, you therefore cannot manipulate objects (set or
5153 * delete properties for example) since it is possible such operations will
5154 * result in the allocation of objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00005155 */
Steve Block6ded16b2010-05-10 14:33:55 +01005156enum GCType {
5157 kGCTypeScavenge = 1 << 0,
5158 kGCTypeMarkSweepCompact = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005159 kGCTypeIncrementalMarking = 1 << 2,
5160 kGCTypeProcessWeakCallbacks = 1 << 3,
5161 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact |
5162 kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks
Steve Block6ded16b2010-05-10 14:33:55 +01005163};
5164
Ben Murdoch097c5b22016-05-18 11:27:45 +01005165/**
5166 * GCCallbackFlags is used to notify additional information about the GC
5167 * callback.
5168 * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
5169 * constructing retained object infos.
5170 * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
5171 * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
5172 * is called synchronously without getting posted to an idle task.
5173 * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
5174 * in a phase where V8 is trying to collect all available garbage
5175 * (e.g., handling a low memory notification).
5176 */
Steve Block6ded16b2010-05-10 14:33:55 +01005177enum GCCallbackFlags {
5178 kNoGCCallbackFlags = 0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005179 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005180 kGCCallbackFlagForced = 1 << 2,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005181 kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
5182 kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
Steve Block6ded16b2010-05-10 14:33:55 +01005183};
5184
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005185typedef void (*GCCallback)(GCType type, GCCallbackFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01005186
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005187typedef void (*InterruptCallback)(Isolate* isolate, void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00005188
5189
Steve Blocka7e24c12009-10-30 11:49:00 +00005190/**
Steve Block3ce2e202009-11-05 08:53:23 +00005191 * Collection of V8 heap information.
5192 *
5193 * Instances of this class can be passed to v8::V8::HeapStatistics to
5194 * get heap statistics from V8.
5195 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005196class V8_EXPORT HeapStatistics {
Steve Block3ce2e202009-11-05 08:53:23 +00005197 public:
5198 HeapStatistics();
5199 size_t total_heap_size() { return total_heap_size_; }
Russell Brenner90bac252010-11-18 13:33:46 -08005200 size_t total_heap_size_executable() { return total_heap_size_executable_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005201 size_t total_physical_size() { return total_physical_size_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005202 size_t total_available_size() { return total_available_size_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005203 size_t used_heap_size() { return used_heap_size_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005204 size_t heap_size_limit() { return heap_size_limit_; }
Ben Murdochda12d292016-06-02 14:46:10 +01005205 size_t malloced_memory() { return malloced_memory_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005206 size_t does_zap_garbage() { return does_zap_garbage_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005207
5208 private:
Steve Block3ce2e202009-11-05 08:53:23 +00005209 size_t total_heap_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08005210 size_t total_heap_size_executable_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005211 size_t total_physical_size_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005212 size_t total_available_size_;
Steve Block3ce2e202009-11-05 08:53:23 +00005213 size_t used_heap_size_;
Ben Murdochb8e0da22011-05-16 14:20:40 +01005214 size_t heap_size_limit_;
Ben Murdochda12d292016-06-02 14:46:10 +01005215 size_t malloced_memory_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005216 bool does_zap_garbage_;
Steve Block3ce2e202009-11-05 08:53:23 +00005217
5218 friend class V8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005219 friend class Isolate;
Steve Block3ce2e202009-11-05 08:53:23 +00005220};
5221
5222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005223class V8_EXPORT HeapSpaceStatistics {
5224 public:
5225 HeapSpaceStatistics();
5226 const char* space_name() { return space_name_; }
5227 size_t space_size() { return space_size_; }
5228 size_t space_used_size() { return space_used_size_; }
5229 size_t space_available_size() { return space_available_size_; }
5230 size_t physical_space_size() { return physical_space_size_; }
5231
5232 private:
5233 const char* space_name_;
5234 size_t space_size_;
5235 size_t space_used_size_;
5236 size_t space_available_size_;
5237 size_t physical_space_size_;
5238
5239 friend class Isolate;
5240};
5241
5242
5243class V8_EXPORT HeapObjectStatistics {
5244 public:
5245 HeapObjectStatistics();
5246 const char* object_type() { return object_type_; }
5247 const char* object_sub_type() { return object_sub_type_; }
5248 size_t object_count() { return object_count_; }
5249 size_t object_size() { return object_size_; }
5250
5251 private:
5252 const char* object_type_;
5253 const char* object_sub_type_;
5254 size_t object_count_;
5255 size_t object_size_;
5256
5257 friend class Isolate;
5258};
5259
5260
Steve Block44f0eee2011-05-26 01:26:41 +01005261class RetainedObjectInfo;
5262
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005263
Steve Block44f0eee2011-05-26 01:26:41 +01005264/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005265 * FunctionEntryHook is the type of the profile entry hook called at entry to
5266 * any generated function when function-level profiling is enabled.
5267 *
5268 * \param function the address of the function that's being entered.
5269 * \param return_addr_location points to a location on stack where the machine
5270 * return address resides. This can be used to identify the caller of
5271 * \p function, and/or modified to divert execution when \p function exits.
5272 *
5273 * \note the entry hook must not cause garbage collection.
Steve Block44f0eee2011-05-26 01:26:41 +01005274 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005275typedef void (*FunctionEntryHook)(uintptr_t function,
5276 uintptr_t return_addr_location);
5277
5278/**
5279 * A JIT code event is issued each time code is added, moved or removed.
5280 *
5281 * \note removal events are not currently issued.
5282 */
5283struct JitCodeEvent {
5284 enum EventType {
5285 CODE_ADDED,
5286 CODE_MOVED,
5287 CODE_REMOVED,
5288 CODE_ADD_LINE_POS_INFO,
5289 CODE_START_LINE_INFO_RECORDING,
5290 CODE_END_LINE_INFO_RECORDING
5291 };
5292 // Definition of the code position type. The "POSITION" type means the place
5293 // in the source code which are of interest when making stack traces to
5294 // pin-point the source location of a stack frame as close as possible.
5295 // The "STATEMENT_POSITION" means the place at the beginning of each
5296 // statement, and is used to indicate possible break locations.
5297 enum PositionType { POSITION, STATEMENT_POSITION };
5298
5299 // Type of event.
5300 EventType type;
5301 // Start of the instructions.
5302 void* code_start;
5303 // Size of the instructions.
5304 size_t code_len;
5305 // Script info for CODE_ADDED event.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005306 Local<UnboundScript> script;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005307 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
5308 // code line information which is returned from the
5309 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
5310 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
5311 void* user_data;
5312
5313 struct name_t {
5314 // Name of the object associated with the code, note that the string is not
5315 // zero-terminated.
5316 const char* str;
5317 // Number of chars in str.
5318 size_t len;
5319 };
5320
5321 struct line_info_t {
5322 // PC offset
5323 size_t offset;
5324 // Code postion
5325 size_t pos;
5326 // The position type.
5327 PositionType position_type;
5328 };
5329
5330 union {
5331 // Only valid for CODE_ADDED.
5332 struct name_t name;
5333
5334 // Only valid for CODE_ADD_LINE_POS_INFO
5335 struct line_info_t line_info;
5336
5337 // New location of instructions. Only valid for CODE_MOVED.
5338 void* new_code_start;
5339 };
5340};
5341
5342/**
5343 * Option flags passed to the SetJitCodeEventHandler function.
5344 */
5345enum JitCodeEventOptions {
5346 kJitCodeEventDefault = 0,
5347 // Generate callbacks for already existent code.
5348 kJitCodeEventEnumExisting = 1
5349};
5350
5351
5352/**
5353 * Callback function passed to SetJitCodeEventHandler.
5354 *
5355 * \param event code add, move or removal event.
5356 */
5357typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
5358
5359
5360/**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005361 * Interface for iterating through all external resources in the heap.
5362 */
5363class V8_EXPORT ExternalResourceVisitor { // NOLINT
5364 public:
5365 virtual ~ExternalResourceVisitor() {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005366 virtual void VisitExternalString(Local<String> string) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005367};
5368
5369
5370/**
5371 * Interface for iterating through all the persistent handles in the heap.
5372 */
5373class V8_EXPORT PersistentHandleVisitor { // NOLINT
5374 public:
5375 virtual ~PersistentHandleVisitor() {}
5376 virtual void VisitPersistentHandle(Persistent<Value>* value,
5377 uint16_t class_id) {}
5378};
5379
Ben Murdochda12d292016-06-02 14:46:10 +01005380/**
5381 * Memory pressure level for the MemoryPressureNotification.
5382 * kNone hints V8 that there is no memory pressure.
5383 * kModerate hints V8 to speed up incremental garbage collection at the cost of
5384 * of higher latency due to garbage collection pauses.
5385 * kCritical hints V8 to free memory as soon as possible. Garbage collection
5386 * pauses at this level will be large.
5387 */
5388enum class MemoryPressureLevel { kNone, kModerate, kCritical };
5389
5390/**
5391 * Interface for tracing through the embedder heap. During the v8 garbage
5392 * collection, v8 collects hidden fields of all potential wrappers, and at the
5393 * end of its marking phase iterates the collection and asks the embedder to
5394 * trace through its heap and call PersistentBase::RegisterExternalReference on
5395 * each js object reachable from any of the given wrappers.
5396 *
5397 * Before the first call to the TraceWrappableFrom function v8 will call
5398 * TraceRoots. When the v8 garbage collection is finished, v8 will call
5399 * ClearTracingMarks.
5400 */
5401class EmbedderHeapTracer {
5402 public:
5403 /**
5404 * V8 will call this method at the beginning of the gc cycle.
5405 */
5406 virtual void TraceRoots(Isolate* isolate) = 0;
5407
5408 /**
5409 * V8 will call this method with internal fields of a potential wrappers.
5410 * Embedder is expected to trace its heap (synchronously) and call
5411 * PersistentBase::RegisterExternalReference() on all wrappers reachable from
5412 * any of the given wrappers.
5413 */
5414 virtual void TraceWrappableFrom(
5415 Isolate* isolate,
5416 const std::vector<std::pair<void*, void*> >& internal_fields) = 0;
5417 /**
5418 * V8 will call this method at the end of the gc cycle. Allocation is *not*
5419 * allowed in the ClearTracingMarks.
5420 */
5421 virtual void ClearTracingMarks(Isolate* isolate) = 0;
5422
5423 protected:
5424 virtual ~EmbedderHeapTracer() = default;
5425};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005426
5427/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005428 * Isolate represents an isolated instance of the V8 engine. V8 isolates have
5429 * completely separate states. Objects from one isolate must not be used in
5430 * other isolates. The embedder can create multiple isolates and use them in
5431 * parallel in multiple threads. An isolate can be entered by at most one
5432 * thread at any given time. The Locker/Unlocker API must be used to
5433 * synchronize.
5434 */
5435class V8_EXPORT Isolate {
Steve Block44f0eee2011-05-26 01:26:41 +01005436 public:
5437 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005438 * Initial configuration parameters for a new Isolate.
5439 */
5440 struct CreateParams {
5441 CreateParams()
5442 : entry_hook(NULL),
5443 code_event_handler(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005444 snapshot_blob(NULL),
5445 counter_lookup_callback(NULL),
5446 create_histogram_callback(NULL),
5447 add_histogram_sample_callback(NULL),
5448 array_buffer_allocator(NULL) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005449
5450 /**
5451 * The optional entry_hook allows the host application to provide the
5452 * address of a function that's invoked on entry to every V8-generated
5453 * function. Note that entry_hook is invoked at the very start of each
5454 * generated function. Furthermore, if an entry_hook is given, V8 will
5455 * always run without a context snapshot.
5456 */
5457 FunctionEntryHook entry_hook;
5458
5459 /**
5460 * Allows the host application to provide the address of a function that is
5461 * notified each time code is added, moved or removed.
5462 */
5463 JitCodeEventHandler code_event_handler;
5464
5465 /**
5466 * ResourceConstraints to use for the new Isolate.
5467 */
5468 ResourceConstraints constraints;
5469
5470 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005471 * Explicitly specify a startup snapshot blob. The embedder owns the blob.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005472 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005473 StartupData* snapshot_blob;
5474
5475
5476 /**
5477 * Enables the host application to provide a mechanism for recording
5478 * statistics counters.
5479 */
5480 CounterLookupCallback counter_lookup_callback;
5481
5482 /**
5483 * Enables the host application to provide a mechanism for recording
5484 * histograms. The CreateHistogram function returns a
5485 * histogram which will later be passed to the AddHistogramSample
5486 * function.
5487 */
5488 CreateHistogramCallback create_histogram_callback;
5489 AddHistogramSampleCallback add_histogram_sample_callback;
5490
5491 /**
5492 * The ArrayBuffer::Allocator to use for allocating and freeing the backing
5493 * store of ArrayBuffers.
5494 */
5495 ArrayBuffer::Allocator* array_buffer_allocator;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005496 };
5497
5498
5499 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005500 * Stack-allocated class which sets the isolate for all operations
5501 * executed within a local scope.
5502 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005503 class V8_EXPORT Scope {
Steve Block44f0eee2011-05-26 01:26:41 +01005504 public:
5505 explicit Scope(Isolate* isolate) : isolate_(isolate) {
5506 isolate->Enter();
5507 }
5508
5509 ~Scope() { isolate_->Exit(); }
5510
5511 private:
5512 Isolate* const isolate_;
5513
5514 // Prevent copying of Scope objects.
5515 Scope(const Scope&);
5516 Scope& operator=(const Scope&);
5517 };
5518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005519
5520 /**
5521 * Assert that no Javascript code is invoked.
5522 */
5523 class V8_EXPORT DisallowJavascriptExecutionScope {
5524 public:
5525 enum OnFailure { CRASH_ON_FAILURE, THROW_ON_FAILURE };
5526
5527 DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure);
5528 ~DisallowJavascriptExecutionScope();
5529
5530 private:
5531 bool on_failure_;
5532 void* internal_;
5533
5534 // Prevent copying of Scope objects.
5535 DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&);
5536 DisallowJavascriptExecutionScope& operator=(
5537 const DisallowJavascriptExecutionScope&);
5538 };
5539
5540
5541 /**
5542 * Introduce exception to DisallowJavascriptExecutionScope.
5543 */
5544 class V8_EXPORT AllowJavascriptExecutionScope {
5545 public:
5546 explicit AllowJavascriptExecutionScope(Isolate* isolate);
5547 ~AllowJavascriptExecutionScope();
5548
5549 private:
5550 void* internal_throws_;
5551 void* internal_assert_;
5552
5553 // Prevent copying of Scope objects.
5554 AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&);
5555 AllowJavascriptExecutionScope& operator=(
5556 const AllowJavascriptExecutionScope&);
5557 };
5558
5559 /**
5560 * Do not run microtasks while this scope is active, even if microtasks are
5561 * automatically executed otherwise.
5562 */
5563 class V8_EXPORT SuppressMicrotaskExecutionScope {
5564 public:
5565 explicit SuppressMicrotaskExecutionScope(Isolate* isolate);
5566 ~SuppressMicrotaskExecutionScope();
5567
5568 private:
5569 internal::Isolate* isolate_;
5570
5571 // Prevent copying of Scope objects.
5572 SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
5573 SuppressMicrotaskExecutionScope& operator=(
5574 const SuppressMicrotaskExecutionScope&);
5575 };
5576
5577 /**
5578 * Types of garbage collections that can be requested via
5579 * RequestGarbageCollectionForTesting.
5580 */
5581 enum GarbageCollectionType {
5582 kFullGarbageCollection,
5583 kMinorGarbageCollection
5584 };
5585
5586 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005587 * Features reported via the SetUseCounterCallback callback. Do not change
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005588 * assigned numbers of existing items; add new features to the end of this
5589 * list.
5590 */
5591 enum UseCounterFeature {
5592 kUseAsm = 0,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005593 kBreakIterator = 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005594 kLegacyConst = 2,
5595 kMarkDequeOverflow = 3,
5596 kStoreBufferOverflow = 4,
5597 kSlotsBufferOverflow = 5,
5598 kObjectObserve = 6,
5599 kForcedGC = 7,
5600 kSloppyMode = 8,
5601 kStrictMode = 9,
5602 kStrongMode = 10,
5603 kRegExpPrototypeStickyGetter = 11,
5604 kRegExpPrototypeToString = 12,
5605 kRegExpPrototypeUnicodeGetter = 13,
5606 kIntlV8Parse = 14,
5607 kIntlPattern = 15,
5608 kIntlResolved = 16,
5609 kPromiseChain = 17,
5610 kPromiseAccept = 18,
5611 kPromiseDefer = 19,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005612 kHtmlCommentInExternalScript = 20,
5613 kHtmlComment = 21,
5614 kSloppyModeBlockScopedFunctionRedefinition = 22,
5615 kForInInitializer = 23,
5616 kArrayProtectorDirtied = 24,
5617 kArraySpeciesModified = 25,
5618 kArrayPrototypeConstructorModified = 26,
5619 kArrayInstanceProtoModified = 27,
5620 kArrayInstanceConstructorModified = 28,
Ben Murdochda12d292016-06-02 14:46:10 +01005621 kLegacyFunctionDeclaration = 29,
5622 kRegExpPrototypeSourceGetter = 30,
5623 kRegExpPrototypeOldFlagGetter = 31,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005624
5625 // If you add new values here, you'll also need to update V8Initializer.cpp
5626 // in Chromium.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005627 kUseCounterFeatureCount // This enum value must be last.
5628 };
5629
5630 typedef void (*UseCounterCallback)(Isolate* isolate,
5631 UseCounterFeature feature);
5632
5633
Steve Block44f0eee2011-05-26 01:26:41 +01005634 /**
5635 * Creates a new isolate. Does not change the currently entered
5636 * isolate.
5637 *
5638 * When an isolate is no longer used its resources should be freed
5639 * by calling Dispose(). Using the delete operator is not allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005640 *
5641 * V8::Initialize() must have run prior to this.
Steve Block44f0eee2011-05-26 01:26:41 +01005642 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005643 static Isolate* New(const CreateParams& params);
Steve Block44f0eee2011-05-26 01:26:41 +01005644
5645 /**
5646 * Returns the entered isolate for the current thread or NULL in
5647 * case there is no current isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005648 *
5649 * This method must not be invoked before V8::Initialize() was invoked.
Steve Block44f0eee2011-05-26 01:26:41 +01005650 */
5651 static Isolate* GetCurrent();
5652
5653 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005654 * Custom callback used by embedders to help V8 determine if it should abort
5655 * when it throws and no internal handler is predicted to catch the
5656 * exception. If --abort-on-uncaught-exception is used on the command line,
5657 * then V8 will abort if either:
5658 * - no custom callback is set.
5659 * - the custom callback set returns true.
5660 * Otherwise, the custom callback will not be called and V8 will not abort.
5661 */
5662 typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*);
5663 void SetAbortOnUncaughtExceptionCallback(
5664 AbortOnUncaughtExceptionCallback callback);
5665
5666 /**
Ben Murdochda12d292016-06-02 14:46:10 +01005667 * Optional notification that the system is running low on memory.
5668 * V8 uses these notifications to guide heuristics.
5669 * It is allowed to call this function from another thread while
5670 * the isolate is executing long running JavaScript code.
5671 */
5672 void MemoryPressureNotification(MemoryPressureLevel level);
5673
5674 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005675 * Methods below this point require holding a lock (using Locker) in
5676 * a multi-threaded environment.
5677 */
5678
5679 /**
5680 * Sets this isolate as the entered one for the current thread.
5681 * Saves the previously entered one (if any), so that it can be
5682 * restored when exiting. Re-entering an isolate is allowed.
5683 */
5684 void Enter();
5685
5686 /**
5687 * Exits this isolate by restoring the previously entered one in the
5688 * current thread. The isolate may still stay the same, if it was
5689 * entered more than once.
5690 *
5691 * Requires: this == Isolate::GetCurrent().
5692 */
5693 void Exit();
5694
5695 /**
5696 * Disposes the isolate. The isolate must not be entered by any
5697 * thread to be disposable.
5698 */
5699 void Dispose();
5700
Ben Murdoch257744e2011-11-30 15:57:28 +00005701 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005702 * Discards all V8 thread-specific data for the Isolate. Should be used
5703 * if a thread is terminating and it has used an Isolate that will outlive
5704 * the thread -- all thread-specific data for an Isolate is discarded when
5705 * an Isolate is disposed so this call is pointless if an Isolate is about
5706 * to be Disposed.
5707 */
5708 void DiscardThreadSpecificMetadata();
5709
5710 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005711 * Associate embedder-specific data with the isolate. |slot| has to be
5712 * between 0 and GetNumberOfDataSlots() - 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00005713 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005714 V8_INLINE void SetData(uint32_t slot, void* data);
Ben Murdoch257744e2011-11-30 15:57:28 +00005715
5716 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005717 * Retrieve embedder-specific data from the isolate.
5718 * Returns NULL if SetData has never been called for the given |slot|.
Ben Murdoch257744e2011-11-30 15:57:28 +00005719 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005720 V8_INLINE void* GetData(uint32_t slot);
5721
5722 /**
5723 * Returns the maximum number of available embedder data slots. Valid slots
5724 * are in the range of 0 - GetNumberOfDataSlots() - 1.
5725 */
5726 V8_INLINE static uint32_t GetNumberOfDataSlots();
5727
5728 /**
5729 * Get statistics about the heap memory usage.
5730 */
5731 void GetHeapStatistics(HeapStatistics* heap_statistics);
5732
5733 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005734 * Returns the number of spaces in the heap.
5735 */
5736 size_t NumberOfHeapSpaces();
5737
5738 /**
5739 * Get the memory usage of a space in the heap.
5740 *
5741 * \param space_statistics The HeapSpaceStatistics object to fill in
5742 * statistics.
5743 * \param index The index of the space to get statistics from, which ranges
5744 * from 0 to NumberOfHeapSpaces() - 1.
5745 * \returns true on success.
5746 */
5747 bool GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
5748 size_t index);
5749
5750 /**
5751 * Returns the number of types of objects tracked in the heap at GC.
5752 */
5753 size_t NumberOfTrackedHeapObjectTypes();
5754
5755 /**
5756 * Get statistics about objects in the heap.
5757 *
5758 * \param object_statistics The HeapObjectStatistics object to fill in
5759 * statistics of objects of given type, which were live in the previous GC.
5760 * \param type_index The index of the type of object to fill details about,
5761 * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1.
5762 * \returns true on success.
5763 */
5764 bool GetHeapObjectStatisticsAtLastGC(HeapObjectStatistics* object_statistics,
5765 size_t type_index);
5766
5767 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005768 * Get a call stack sample from the isolate.
5769 * \param state Execution state.
5770 * \param frames Caller allocated buffer to store stack frames.
5771 * \param frames_limit Maximum number of frames to capture. The buffer must
5772 * be large enough to hold the number of frames.
5773 * \param sample_info The sample info is filled up by the function
5774 * provides number of actual captured stack frames and
5775 * the current VM state.
5776 * \note GetStackSample should only be called when the JS thread is paused or
5777 * interrupted. Otherwise the behavior is undefined.
5778 */
5779 void GetStackSample(const RegisterState& state, void** frames,
5780 size_t frames_limit, SampleInfo* sample_info);
5781
5782 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005783 * Adjusts the amount of registered external memory. Used to give V8 an
5784 * indication of the amount of externally allocated memory that is kept alive
5785 * by JavaScript objects. V8 uses this to decide when to perform global
5786 * garbage collections. Registering externally allocated memory will trigger
5787 * global garbage collections more often than it would otherwise in an attempt
5788 * to garbage collect the JavaScript objects that keep the externally
5789 * allocated memory alive.
5790 *
5791 * \param change_in_bytes the change in externally allocated memory that is
5792 * kept alive by JavaScript objects.
5793 * \returns the adjusted value.
5794 */
5795 V8_INLINE int64_t
5796 AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
5797
5798 /**
5799 * Returns heap profiler for this isolate. Will return NULL until the isolate
5800 * is initialized.
5801 */
5802 HeapProfiler* GetHeapProfiler();
5803
5804 /**
5805 * Returns CPU profiler for this isolate. Will return NULL unless the isolate
5806 * is initialized. It is the embedder's responsibility to stop all CPU
5807 * profiling activities if it has started any.
5808 */
5809 CpuProfiler* GetCpuProfiler();
5810
5811 /** Returns true if this isolate has a current context. */
5812 bool InContext();
5813
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005814 /**
5815 * Returns the context of the currently running JavaScript, or the context
5816 * on the top of the stack if no JavaScript is running.
5817 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005818 Local<Context> GetCurrentContext();
5819
5820 /**
5821 * Returns the context of the calling JavaScript code. That is the
5822 * context of the top-most JavaScript frame. If there are no
5823 * JavaScript frames an empty handle is returned.
5824 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005825 V8_DEPRECATE_SOON(
5826 "Calling context concept is not compatible with tail calls, and will be "
5827 "removed.",
5828 Local<Context> GetCallingContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005829
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005830 /** Returns the last context entered through V8's C++ API. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005831 Local<Context> GetEnteredContext();
5832
5833 /**
5834 * Schedules an exception to be thrown when returning to JavaScript. When an
5835 * exception has been scheduled it is illegal to invoke any JavaScript
5836 * operation; the caller must return immediately and only after the exception
5837 * has been handled does it become legal to invoke JavaScript operations.
5838 */
5839 Local<Value> ThrowException(Local<Value> exception);
5840
5841 /**
5842 * Allows the host application to group objects together. If one
5843 * object in the group is alive, all objects in the group are alive.
5844 * After each garbage collection, object groups are removed. It is
5845 * intended to be used in the before-garbage-collection callback
5846 * function, for instance to simulate DOM tree connections among JS
5847 * wrapper objects. Object groups for all dependent handles need to
5848 * be provided for kGCTypeMarkSweepCompact collections, for all other
5849 * garbage collection types it is sufficient to provide object groups
5850 * for partially dependent handles only.
5851 */
5852 template<typename T> void SetObjectGroupId(const Persistent<T>& object,
5853 UniqueId id);
5854
5855 /**
5856 * Allows the host application to declare implicit references from an object
5857 * group to an object. If the objects of the object group are alive, the child
5858 * object is alive too. After each garbage collection, all implicit references
5859 * are removed. It is intended to be used in the before-garbage-collection
5860 * callback function.
5861 */
5862 template<typename T> void SetReferenceFromGroup(UniqueId id,
5863 const Persistent<T>& child);
5864
5865 /**
5866 * Allows the host application to declare implicit references from an object
5867 * to another object. If the parent object is alive, the child object is alive
5868 * too. After each garbage collection, all implicit references are removed. It
5869 * is intended to be used in the before-garbage-collection callback function.
5870 */
5871 template<typename T, typename S>
5872 void SetReference(const Persistent<T>& parent, const Persistent<S>& child);
5873
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005874 typedef void (*GCCallback)(Isolate* isolate, GCType type,
5875 GCCallbackFlags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005876
5877 /**
5878 * Enables the host application to receive a notification before a
5879 * garbage collection. Allocations are allowed in the callback function,
5880 * but the callback is not re-entrant: if the allocation inside it will
5881 * trigger the garbage collection, the callback won't be called again.
5882 * It is possible to specify the GCType filter for your callback. But it is
5883 * not possible to register the same callback function two times with
5884 * different GCType filters.
5885 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005886 void AddGCPrologueCallback(GCCallback callback,
5887 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005888
5889 /**
5890 * This function removes callback which was installed by
5891 * AddGCPrologueCallback function.
5892 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005893 void RemoveGCPrologueCallback(GCCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005894
5895 /**
Ben Murdochda12d292016-06-02 14:46:10 +01005896 * Sets the embedder heap tracer for the isolate.
5897 */
5898 void SetEmbedderHeapTracer(EmbedderHeapTracer* tracer);
5899
5900 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005901 * Enables the host application to receive a notification after a
5902 * garbage collection. Allocations are allowed in the callback function,
5903 * but the callback is not re-entrant: if the allocation inside it will
5904 * trigger the garbage collection, the callback won't be called again.
5905 * It is possible to specify the GCType filter for your callback. But it is
5906 * not possible to register the same callback function two times with
5907 * different GCType filters.
5908 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005909 void AddGCEpilogueCallback(GCCallback callback,
5910 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005911
5912 /**
5913 * This function removes callback which was installed by
5914 * AddGCEpilogueCallback function.
5915 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005916 void RemoveGCEpilogueCallback(GCCallback callback);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005917
5918 /**
5919 * Forcefully terminate the current thread of JavaScript execution
5920 * in the given isolate.
5921 *
5922 * This method can be used by any thread even if that thread has not
5923 * acquired the V8 lock with a Locker object.
5924 */
5925 void TerminateExecution();
5926
5927 /**
5928 * Is V8 terminating JavaScript execution.
5929 *
5930 * Returns true if JavaScript execution is currently terminating
5931 * because of a call to TerminateExecution. In that case there are
5932 * still JavaScript frames on the stack and the termination
5933 * exception is still active.
5934 */
5935 bool IsExecutionTerminating();
5936
5937 /**
5938 * Resume execution capability in the given isolate, whose execution
5939 * was previously forcefully terminated using TerminateExecution().
5940 *
5941 * When execution is forcefully terminated using TerminateExecution(),
5942 * the isolate can not resume execution until all JavaScript frames
5943 * have propagated the uncatchable exception which is generated. This
5944 * method allows the program embedding the engine to handle the
5945 * termination event and resume execution capability, even if
5946 * JavaScript frames remain on the stack.
5947 *
5948 * This method can be used by any thread even if that thread has not
5949 * acquired the V8 lock with a Locker object.
5950 */
5951 void CancelTerminateExecution();
5952
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005953 /**
5954 * Request V8 to interrupt long running JavaScript code and invoke
5955 * the given |callback| passing the given |data| to it. After |callback|
5956 * returns control will be returned to the JavaScript code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005957 * There may be a number of interrupt requests in flight.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005958 * Can be called from another thread without acquiring a |Locker|.
5959 * Registered |callback| must not reenter interrupted Isolate.
5960 */
5961 void RequestInterrupt(InterruptCallback callback, void* data);
5962
5963 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005964 * Request garbage collection in this Isolate. It is only valid to call this
5965 * function if --expose_gc was specified.
5966 *
5967 * This should only be used for testing purposes and not to enforce a garbage
5968 * collection schedule. It has strong negative impact on the garbage
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005969 * collection performance. Use IdleNotificationDeadline() or
5970 * LowMemoryNotification() instead to influence the garbage collection
5971 * schedule.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005972 */
5973 void RequestGarbageCollectionForTesting(GarbageCollectionType type);
5974
5975 /**
5976 * Set the callback to invoke for logging event.
5977 */
5978 void SetEventLogger(LogEventCallback that);
5979
5980 /**
Ben Murdoch097c5b22016-05-18 11:27:45 +01005981 * Adds a callback to notify the host application right before a script
5982 * is about to run. If a script re-enters the runtime during executing, the
5983 * BeforeCallEnteredCallback is invoked for each re-entrance.
5984 * Executing scripts inside the callback will re-trigger the callback.
5985 */
5986 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
5987
5988 /**
5989 * Removes callback that was installed by AddBeforeCallEnteredCallback.
5990 */
5991 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
5992
5993 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005994 * Adds a callback to notify the host application when a script finished
5995 * running. If a script re-enters the runtime during executing, the
5996 * CallCompletedCallback is only invoked when the outer-most script
5997 * execution ends. Executing scripts inside the callback do not trigger
5998 * further callbacks.
5999 */
6000 void AddCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006001 V8_DEPRECATE_SOON(
6002 "Use callback with parameter",
6003 void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006004
6005 /**
6006 * Removes callback that was installed by AddCallCompletedCallback.
6007 */
6008 void RemoveCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006009 V8_DEPRECATE_SOON(
6010 "Use callback with parameter",
6011 void RemoveCallCompletedCallback(
6012 DeprecatedCallCompletedCallback callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006013
6014 /**
6015 * Set callback to notify about promise reject with no handler, or
6016 * revocation of such a previous notification once the handler is added.
6017 */
6018 void SetPromiseRejectCallback(PromiseRejectCallback callback);
6019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006020 /**
6021 * Experimental: Runs the Microtask Work Queue until empty
6022 * Any exceptions thrown by microtask callbacks are swallowed.
6023 */
6024 void RunMicrotasks();
6025
6026 /**
6027 * Experimental: Enqueues the callback to the Microtask Work Queue
6028 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006029 void EnqueueMicrotask(Local<Function> microtask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006030
6031 /**
6032 * Experimental: Enqueues the callback to the Microtask Work Queue
6033 */
6034 void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL);
6035
Ben Murdochda12d292016-06-02 14:46:10 +01006036 /**
6037 * Experimental: Controls how Microtasks are invoked. See MicrotasksPolicy
6038 * for details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006039 */
Ben Murdochda12d292016-06-02 14:46:10 +01006040 void SetMicrotasksPolicy(MicrotasksPolicy policy);
6041 V8_DEPRECATE_SOON("Use SetMicrotasksPolicy",
6042 void SetAutorunMicrotasks(bool autorun));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006043
6044 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006045 * Experimental: Returns the policy controlling how Microtasks are invoked.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006046 */
Ben Murdochda12d292016-06-02 14:46:10 +01006047 MicrotasksPolicy GetMicrotasksPolicy() const;
6048 V8_DEPRECATE_SOON("Use GetMicrotasksPolicy",
6049 bool WillAutorunMicrotasks() const);
6050
6051 /**
6052 * Experimental: adds a callback to notify the host application after
6053 * microtasks were run. The callback is triggered by explicit RunMicrotasks
6054 * call or automatic microtasks execution (see SetAutorunMicrotasks).
6055 *
6056 * Callback will trigger even if microtasks were attempted to run,
6057 * but the microtasks queue was empty and no single microtask was actually
6058 * executed.
6059 *
6060 * Executing scriptsinside the callback will not re-trigger microtasks and
6061 * the callback.
6062 */
6063 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
6064
6065 /**
6066 * Removes callback that was installed by AddMicrotasksCompletedCallback.
6067 */
6068 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006069
6070 /**
6071 * Sets a callback for counting the number of times a feature of V8 is used.
6072 */
6073 void SetUseCounterCallback(UseCounterCallback callback);
6074
6075 /**
6076 * Enables the host application to provide a mechanism for recording
6077 * statistics counters.
6078 */
6079 void SetCounterFunction(CounterLookupCallback);
6080
6081 /**
6082 * Enables the host application to provide a mechanism for recording
6083 * histograms. The CreateHistogram function returns a
6084 * histogram which will later be passed to the AddHistogramSample
6085 * function.
6086 */
6087 void SetCreateHistogramFunction(CreateHistogramCallback);
6088 void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
6089
6090 /**
6091 * Optional notification that the embedder is idle.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006092 * V8 uses the notification to perform garbage collection.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006093 * This call can be used repeatedly if the embedder remains idle.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006094 * Returns true if the embedder should stop calling IdleNotificationDeadline
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006095 * until real work has been done. This indicates that V8 has done
6096 * as much cleanup as it will be able to do.
6097 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006098 * The deadline_in_seconds argument specifies the deadline V8 has to finish
6099 * garbage collection work. deadline_in_seconds is compared with
6100 * MonotonicallyIncreasingTime() and should be based on the same timebase as
6101 * that function. There is no guarantee that the actual work will be done
6102 * within the time limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006103 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006104 bool IdleNotificationDeadline(double deadline_in_seconds);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006106 V8_DEPRECATED("use IdleNotificationDeadline()",
6107 bool IdleNotification(int idle_time_in_ms));
6108
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006109 /**
6110 * Optional notification that the system is running low on memory.
6111 * V8 uses these notifications to attempt to free memory.
6112 */
6113 void LowMemoryNotification();
6114
6115 /**
6116 * Optional notification that a context has been disposed. V8 uses
6117 * these notifications to guide the GC heuristic. Returns the number
6118 * of context disposals - including this one - since the last time
6119 * V8 had a chance to clean up.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006120 *
6121 * The optional parameter |dependant_context| specifies whether the disposed
6122 * context was depending on state from other contexts or not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006123 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006124 int ContextDisposedNotification(bool dependant_context = true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006125
6126 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006127 * Optional notification that the isolate switched to the foreground.
6128 * V8 uses these notifications to guide heuristics.
6129 */
6130 void IsolateInForegroundNotification();
6131
6132 /**
6133 * Optional notification that the isolate switched to the background.
6134 * V8 uses these notifications to guide heuristics.
6135 */
6136 void IsolateInBackgroundNotification();
6137
6138 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006139 * Allows the host application to provide the address of a function that is
6140 * notified each time code is added, moved or removed.
6141 *
6142 * \param options options for the JIT code event handler.
6143 * \param event_handler the JIT code event handler, which will be invoked
6144 * each time code is added, moved or removed.
6145 * \note \p event_handler won't get notified of existent code.
6146 * \note since code removal notifications are not currently issued, the
6147 * \p event_handler may get notifications of code that overlaps earlier
6148 * code notifications. This happens when code areas are reused, and the
6149 * earlier overlapping code areas should therefore be discarded.
6150 * \note the events passed to \p event_handler and the strings they point to
6151 * are not guaranteed to live past each call. The \p event_handler must
6152 * copy strings and other parameters it needs to keep around.
6153 * \note the set of events declared in JitCodeEvent::EventType is expected to
6154 * grow over time, and the JitCodeEvent structure is expected to accrue
6155 * new members. The \p event_handler function must ignore event codes
6156 * it does not recognize to maintain future compatibility.
6157 * \note Use Isolate::CreateParams to get events for code executed during
6158 * Isolate setup.
6159 */
6160 void SetJitCodeEventHandler(JitCodeEventOptions options,
6161 JitCodeEventHandler event_handler);
6162
6163 /**
6164 * Modifies the stack limit for this Isolate.
6165 *
6166 * \param stack_limit An address beyond which the Vm's stack may not grow.
6167 *
6168 * \note If you are using threads then you should hold the V8::Locker lock
6169 * while setting the stack limit and you must set a non-default stack
6170 * limit separately for each thread.
6171 */
6172 void SetStackLimit(uintptr_t stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00006173
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006174 /**
6175 * Returns a memory range that can potentially contain jitted code.
6176 *
6177 * On Win64, embedders are advised to install function table callbacks for
6178 * these ranges, as default SEH won't be able to unwind through jitted code.
6179 *
6180 * The first page of the code range is reserved for the embedder and is
6181 * committed, writable, and executable.
6182 *
6183 * Might be empty on other platforms.
6184 *
6185 * https://code.google.com/p/v8/issues/detail?id=3598
6186 */
6187 void GetCodeRange(void** start, size_t* length_in_bytes);
6188
6189 /** Set the callback to invoke in case of fatal errors. */
6190 void SetFatalErrorHandler(FatalErrorCallback that);
6191
6192 /**
6193 * Set the callback to invoke to check if code generation from
6194 * strings should be allowed.
6195 */
6196 void SetAllowCodeGenerationFromStringsCallback(
6197 AllowCodeGenerationFromStringsCallback callback);
6198
6199 /**
6200 * Check if V8 is dead and therefore unusable. This is the case after
6201 * fatal errors such as out-of-memory situations.
6202 */
6203 bool IsDead();
6204
6205 /**
6206 * Adds a message listener.
6207 *
6208 * The same message listener can be added more than once and in that
6209 * case it will be called more than once for each message.
6210 *
6211 * If data is specified, it will be passed to the callback when it is called.
6212 * Otherwise, the exception object will be passed to the callback instead.
6213 */
6214 bool AddMessageListener(MessageCallback that,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006215 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006216
6217 /**
6218 * Remove all message listeners from the specified callback function.
6219 */
6220 void RemoveMessageListeners(MessageCallback that);
6221
6222 /** Callback function for reporting failed access checks.*/
6223 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
6224
6225 /**
6226 * Tells V8 to capture current stack trace when uncaught exception occurs
6227 * and report it to the message listeners. The option is off by default.
6228 */
6229 void SetCaptureStackTraceForUncaughtExceptions(
6230 bool capture, int frame_limit = 10,
6231 StackTrace::StackTraceOptions options = StackTrace::kOverview);
6232
6233 /**
6234 * Enables the host application to provide a mechanism to be notified
6235 * and perform custom logging when V8 Allocates Executable Memory.
6236 */
6237 void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6238 ObjectSpace space, AllocationAction action);
6239
6240 /**
6241 * Removes callback that was installed by AddMemoryAllocationCallback.
6242 */
6243 void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback);
6244
6245 /**
6246 * Iterates through all external resources referenced from current isolate
6247 * heap. GC is not invoked prior to iterating, therefore there is no
6248 * guarantee that visited objects are still alive.
6249 */
6250 void VisitExternalResources(ExternalResourceVisitor* visitor);
6251
6252 /**
6253 * Iterates through all the persistent handles in the current isolate's heap
6254 * that have class_ids.
6255 */
6256 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
6257
6258 /**
6259 * Iterates through all the persistent handles in the current isolate's heap
6260 * that have class_ids and are candidates to be marked as partially dependent
6261 * handles. This will visit handles to young objects created since the last
6262 * garbage collection but is free to visit an arbitrary superset of these
6263 * objects.
6264 */
6265 void VisitHandlesForPartialDependence(PersistentHandleVisitor* visitor);
6266
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006267 /**
6268 * Iterates through all the persistent handles in the current isolate's heap
6269 * that have class_ids and are weak to be marked as inactive if there is no
6270 * pending activity for the handle.
6271 */
6272 void VisitWeakHandles(PersistentHandleVisitor* visitor);
6273
Steve Block44f0eee2011-05-26 01:26:41 +01006274 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006275 template <class K, class V, class Traits>
6276 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006277
Steve Block44f0eee2011-05-26 01:26:41 +01006278 Isolate();
6279 Isolate(const Isolate&);
6280 ~Isolate();
6281 Isolate& operator=(const Isolate&);
6282 void* operator new(size_t size);
6283 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006284
6285 void SetObjectGroupId(internal::Object** object, UniqueId id);
6286 void SetReferenceFromGroup(UniqueId id, internal::Object** object);
6287 void SetReference(internal::Object** parent, internal::Object** child);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006288 void ReportExternalAllocationLimitReached();
Steve Block44f0eee2011-05-26 01:26:41 +01006289};
6290
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006291class V8_EXPORT StartupData {
Ben Murdoch257744e2011-11-30 15:57:28 +00006292 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006293 const char* data;
Ben Murdoch257744e2011-11-30 15:57:28 +00006294 int raw_size;
6295};
6296
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006297
6298/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006299 * EntropySource is used as a callback function when v8 needs a source
6300 * of entropy.
6301 */
6302typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
6303
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006304
6305/**
6306 * ReturnAddressLocationResolver is used as a callback function when v8 is
6307 * resolving the location of a return address on the stack. Profilers that
6308 * change the return address on the stack can use this to resolve the stack
6309 * location to whereever the profiler stashed the original return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006310 *
6311 * \param return_addr_location points to a location on stack where a machine
6312 * return address resides.
6313 * \returns either return_addr_location, or else a pointer to the profiler's
6314 * copy of the original return address.
6315 *
6316 * \note the resolver function must not cause garbage collection.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006317 */
6318typedef uintptr_t (*ReturnAddressLocationResolver)(
6319 uintptr_t return_addr_location);
6320
6321
6322/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006323 * Container class for static utility functions.
6324 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006325class V8_EXPORT V8 {
Steve Blocka7e24c12009-10-30 11:49:00 +00006326 public:
6327 /** Set the callback to invoke in case of fatal errors. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006328 V8_INLINE static V8_DEPRECATED(
6329 "Use isolate version",
6330 void SetFatalErrorHandler(FatalErrorCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006331
6332 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00006333 * Set the callback to invoke to check if code generation from
6334 * strings should be allowed.
6335 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006336 V8_INLINE static V8_DEPRECATED(
6337 "Use isolate version", void SetAllowCodeGenerationFromStringsCallback(
6338 AllowCodeGenerationFromStringsCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006339
6340 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006341 * Check if V8 is dead and therefore unusable. This is the case after
6342 * fatal errors such as out-of-memory situations.
6343 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006344 V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead());
Ben Murdoch257744e2011-11-30 15:57:28 +00006345
6346 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006347 * Hand startup data to V8, in case the embedder has chosen to build
6348 * V8 with external startup data.
6349 *
6350 * Note:
6351 * - By default the startup data is linked into the V8 library, in which
6352 * case this function is not meaningful.
6353 * - If this needs to be called, it needs to be called before V8
6354 * tries to make use of its built-ins.
6355 * - To avoid unnecessary copies of data, V8 will point directly into the
6356 * given data blob, so pretty please keep it around until V8 exit.
6357 * - Compression of the startup blob might be useful, but needs to
6358 * handled entirely on the embedders' side.
6359 * - The call will abort if the data is invalid.
6360 */
6361 static void SetNativesDataBlob(StartupData* startup_blob);
6362 static void SetSnapshotDataBlob(StartupData* startup_blob);
6363
6364 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006365 * Bootstrap an isolate and a context from scratch to create a startup
6366 * snapshot. Include the side-effects of running the optional script.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006367 * Returns { NULL, 0 } on failure.
Ben Murdochda12d292016-06-02 14:46:10 +01006368 * The caller acquires ownership of the data array in the return value.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006369 */
Ben Murdochda12d292016-06-02 14:46:10 +01006370 static StartupData CreateSnapshotDataBlob(const char* embedded_source = NULL);
6371
6372 /**
6373 * Bootstrap an isolate and a context from the cold startup blob, run the
6374 * warm-up script to trigger code compilation. The side effects are then
6375 * discarded. The resulting startup snapshot will include compiled code.
6376 * Returns { NULL, 0 } on failure.
6377 * The caller acquires ownership of the data array in the return value.
6378 * The argument startup blob is untouched.
6379 */
6380 static StartupData WarmUpSnapshotDataBlob(StartupData cold_startup_blob,
6381 const char* warmup_source);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006382
6383 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006384 * Adds a message listener.
6385 *
Ben Murdoch257744e2011-11-30 15:57:28 +00006386 * The same message listener can be added more than once and in that
Steve Blocka7e24c12009-10-30 11:49:00 +00006387 * case it will be called more than once for each message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006388 *
6389 * If data is specified, it will be passed to the callback when it is called.
6390 * Otherwise, the exception object will be passed to the callback instead.
Steve Blocka7e24c12009-10-30 11:49:00 +00006391 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006392 V8_INLINE static V8_DEPRECATED(
6393 "Use isolate version",
6394 bool AddMessageListener(MessageCallback that,
6395 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006396
6397 /**
6398 * Remove all message listeners from the specified callback function.
6399 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006400 V8_INLINE static V8_DEPRECATED(
6401 "Use isolate version", void RemoveMessageListeners(MessageCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006402
6403 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006404 * Tells V8 to capture current stack trace when uncaught exception occurs
6405 * and report it to the message listeners. The option is off by default.
6406 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006407 V8_INLINE static V8_DEPRECATED(
6408 "Use isolate version",
6409 void SetCaptureStackTraceForUncaughtExceptions(
6410 bool capture, int frame_limit = 10,
6411 StackTrace::StackTraceOptions options = StackTrace::kOverview));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006412
6413 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006414 * Sets V8 flags from a string.
6415 */
6416 static void SetFlagsFromString(const char* str, int length);
6417
6418 /**
6419 * Sets V8 flags from the command line.
6420 */
6421 static void SetFlagsFromCommandLine(int* argc,
6422 char** argv,
6423 bool remove_flags);
6424
6425 /** Get the version string. */
6426 static const char* GetVersion();
6427
Steve Blocka7e24c12009-10-30 11:49:00 +00006428 /** Callback function for reporting failed access checks.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006429 V8_INLINE static V8_DEPRECATED(
6430 "Use isolate version",
6431 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback));
Steve Blocka7e24c12009-10-30 11:49:00 +00006432
6433 /**
6434 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01006435 * garbage collection. Allocations are not allowed in the
6436 * callback function, you therefore cannot manipulate objects (set
6437 * or delete properties for example) since it is possible such
6438 * operations will result in the allocation of objects. It is possible
6439 * to specify the GCType filter for your callback. But it is not possible to
6440 * register the same callback function two times with different
6441 * GCType filters.
6442 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006443 static V8_DEPRECATED(
6444 "Use isolate version",
6445 void AddGCPrologueCallback(GCCallback callback,
6446 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006447
6448 /**
6449 * This function removes callback which was installed by
6450 * AddGCPrologueCallback function.
6451 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006452 V8_INLINE static V8_DEPRECATED(
6453 "Use isolate version",
6454 void RemoveGCPrologueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006455
6456 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006457 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01006458 * garbage collection. Allocations are not allowed in the
6459 * callback function, you therefore cannot manipulate objects (set
6460 * or delete properties for example) since it is possible such
6461 * operations will result in the allocation of objects. It is possible
6462 * to specify the GCType filter for your callback. But it is not possible to
6463 * register the same callback function two times with different
6464 * GCType filters.
6465 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006466 static V8_DEPRECATED(
6467 "Use isolate version",
6468 void AddGCEpilogueCallback(GCCallback callback,
6469 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006470
6471 /**
6472 * This function removes callback which was installed by
6473 * AddGCEpilogueCallback function.
6474 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006475 V8_INLINE static V8_DEPRECATED(
6476 "Use isolate version",
6477 void RemoveGCEpilogueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006478
6479 /**
Iain Merrick9ac36c92010-09-13 15:29:50 +01006480 * Enables the host application to provide a mechanism to be notified
6481 * and perform custom logging when V8 Allocates Executable Memory.
6482 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006483 V8_INLINE static V8_DEPRECATED(
6484 "Use isolate version",
6485 void AddMemoryAllocationCallback(MemoryAllocationCallback callback,
6486 ObjectSpace space,
6487 AllocationAction action));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006488
6489 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006490 * Removes callback that was installed by AddMemoryAllocationCallback.
Iain Merrick9ac36c92010-09-13 15:29:50 +01006491 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006492 V8_INLINE static V8_DEPRECATED(
6493 "Use isolate version",
6494 void RemoveMemoryAllocationCallback(MemoryAllocationCallback callback));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006495
6496 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006497 * Initializes V8. This function needs to be called before the first Isolate
6498 * is created. It always returns true.
Steve Blocka7e24c12009-10-30 11:49:00 +00006499 */
6500 static bool Initialize();
6501
6502 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006503 * Allows the host application to provide a callback which can be used
6504 * as a source of entropy for random number generators.
6505 */
6506 static void SetEntropySource(EntropySource source);
6507
6508 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006509 * Allows the host application to provide a callback that allows v8 to
6510 * cooperate with a profiler that rewrites return addresses on stack.
6511 */
6512 static void SetReturnAddressLocationResolver(
6513 ReturnAddressLocationResolver return_address_resolver);
6514
6515 /**
Steve Block44f0eee2011-05-26 01:26:41 +01006516 * Forcefully terminate the current thread of JavaScript execution
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006517 * in the given isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +00006518 *
6519 * This method can be used by any thread even if that thread has not
6520 * acquired the V8 lock with a Locker object.
Steve Block44f0eee2011-05-26 01:26:41 +01006521 *
6522 * \param isolate The isolate in which to terminate the current JS execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006523 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006524 V8_INLINE static V8_DEPRECATED("Use isolate version",
6525 void TerminateExecution(Isolate* isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00006526
6527 /**
Steve Block6ded16b2010-05-10 14:33:55 +01006528 * Is V8 terminating JavaScript execution.
6529 *
6530 * Returns true if JavaScript execution is currently terminating
6531 * because of a call to TerminateExecution. In that case there are
6532 * still JavaScript frames on the stack and the termination
6533 * exception is still active.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006534 *
6535 * \param isolate The isolate in which to check.
Steve Block6ded16b2010-05-10 14:33:55 +01006536 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006537 V8_INLINE static V8_DEPRECATED(
6538 "Use isolate version",
6539 bool IsExecutionTerminating(Isolate* isolate = NULL));
Steve Block6ded16b2010-05-10 14:33:55 +01006540
6541 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006542 * Resume execution capability in the given isolate, whose execution
6543 * was previously forcefully terminated using TerminateExecution().
6544 *
6545 * When execution is forcefully terminated using TerminateExecution(),
6546 * the isolate can not resume execution until all JavaScript frames
6547 * have propagated the uncatchable exception which is generated. This
6548 * method allows the program embedding the engine to handle the
6549 * termination event and resume execution capability, even if
6550 * JavaScript frames remain on the stack.
6551 *
6552 * This method can be used by any thread even if that thread has not
6553 * acquired the V8 lock with a Locker object.
6554 *
6555 * \param isolate The isolate in which to resume execution capability.
6556 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006557 V8_INLINE static V8_DEPRECATED(
6558 "Use isolate version", void CancelTerminateExecution(Isolate* isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006559
6560 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006561 * Releases any resources used by v8 and stops any utility threads
6562 * that may be running. Note that disposing v8 is permanent, it
6563 * cannot be reinitialized.
6564 *
6565 * It should generally not be necessary to dispose v8 before exiting
6566 * a process, this should happen automatically. It is only necessary
6567 * to use if the process needs the resources taken up by v8.
6568 */
6569 static bool Dispose();
6570
Steve Block3ce2e202009-11-05 08:53:23 +00006571 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006572 * Iterates through all external resources referenced from current isolate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006573 * heap. GC is not invoked prior to iterating, therefore there is no
6574 * guarantee that visited objects are still alive.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006575 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006576 V8_INLINE static V8_DEPRECATED(
6577 "Use isolate version",
6578 void VisitExternalResources(ExternalResourceVisitor* visitor));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006579
6580 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006581 * Iterates through all the persistent handles in the current isolate's heap
6582 * that have class_ids.
6583 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006584 V8_INLINE static V8_DEPRECATED(
6585 "Use isolate version",
6586 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006587
6588 /**
6589 * Iterates through all the persistent handles in isolate's heap that have
6590 * class_ids.
6591 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006592 V8_INLINE static V8_DEPRECATED(
6593 "Use isolate version",
6594 void VisitHandlesWithClassIds(Isolate* isolate,
6595 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006596
6597 /**
6598 * Iterates through all the persistent handles in the current isolate's heap
6599 * that have class_ids and are candidates to be marked as partially dependent
6600 * handles. This will visit handles to young objects created since the last
6601 * garbage collection but is free to visit an arbitrary superset of these
6602 * objects.
6603 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006604 V8_INLINE static V8_DEPRECATED(
6605 "Use isolate version",
6606 void VisitHandlesForPartialDependence(Isolate* isolate,
6607 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006608
6609 /**
6610 * Initialize the ICU library bundled with V8. The embedder should only
6611 * invoke this method when using the bundled ICU. Returns true on success.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006612 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006613 * If V8 was compiled with the ICU data in an external file, the location
6614 * of the data file has to be provided.
Steve Blocka7e24c12009-10-30 11:49:00 +00006615 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006616 static bool InitializeICU(const char* icu_data_file = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006617
6618 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006619 * Initialize the external startup data. The embedder only needs to
6620 * invoke this method when external startup data was enabled in a build.
6621 *
6622 * If V8 was compiled with the startup data in an external file, then
6623 * V8 needs to be given those external files during startup. There are
6624 * three ways to do this:
6625 * - InitializeExternalStartupData(const char*)
6626 * This will look in the given directory for files "natives_blob.bin"
6627 * and "snapshot_blob.bin" - which is what the default build calls them.
6628 * - InitializeExternalStartupData(const char*, const char*)
6629 * As above, but will directly use the two given file names.
6630 * - Call SetNativesDataBlob, SetNativesDataBlob.
6631 * This will read the blobs from the given data structures and will
6632 * not perform any file IO.
6633 */
6634 static void InitializeExternalStartupData(const char* directory_path);
6635 static void InitializeExternalStartupData(const char* natives_blob,
6636 const char* snapshot_blob);
6637 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006638 * Sets the v8::Platform to use. This should be invoked before V8 is
6639 * initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +00006640 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006641 static void InitializePlatform(Platform* platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00006642
Steve Block6ded16b2010-05-10 14:33:55 +01006643 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006644 * Clears all references to the v8::Platform. This should be invoked after
6645 * V8 was disposed.
Steve Block6ded16b2010-05-10 14:33:55 +01006646 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006647 static void ShutdownPlatform();
Steve Block6ded16b2010-05-10 14:33:55 +01006648
Steve Blocka7e24c12009-10-30 11:49:00 +00006649 private:
6650 V8();
6651
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006652 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
6653 internal::Object** handle);
6654 static internal::Object** CopyPersistent(internal::Object** handle);
Steve Blocka7e24c12009-10-30 11:49:00 +00006655 static void DisposeGlobal(internal::Object** global_handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006656 typedef WeakCallbackData<Value, void>::Callback WeakCallback;
Ben Murdochda12d292016-06-02 14:46:10 +01006657 static void RegisterExternallyReferencedObject(internal::Object** object,
6658 internal::Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006659 static void MakeWeak(internal::Object** global_handle, void* data,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006660 WeakCallback weak_callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006661 static void MakeWeak(internal::Object** global_handle, void* data,
6662 WeakCallbackInfo<void>::Callback weak_callback,
6663 WeakCallbackType type);
6664 static void MakeWeak(internal::Object** global_handle, void* data,
6665 // Must be 0 or -1.
6666 int internal_field_index1,
6667 // Must be 1 or -1.
6668 int internal_field_index2,
6669 WeakCallbackInfo<void>::Callback weak_callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006670 static void* ClearWeak(internal::Object** global_handle);
6671 static void Eternalize(Isolate* isolate,
6672 Value* handle,
6673 int* index);
6674 static Local<Value> GetEternal(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006675
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006676 static void FromJustIsNothing();
6677 static void ToLocalEmpty();
6678 static void InternalFieldOutOfBounds(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006679 template <class T> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006680 template <class T>
6681 friend class MaybeLocal;
6682 template <class T>
6683 friend class Maybe;
6684 template <class T>
6685 friend class WeakCallbackInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006686 template <class T> friend class Eternal;
6687 template <class T> friend class PersistentBase;
6688 template <class T, class M> friend class Persistent;
Steve Blocka7e24c12009-10-30 11:49:00 +00006689 friend class Context;
6690};
6691
6692
6693/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006694 * A simple Maybe type, representing an object which may or may not have a
6695 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
6696 *
6697 * If an API method returns a Maybe<>, the API method can potentially fail
6698 * either because an exception is thrown, or because an exception is pending,
6699 * e.g. because a previous API call threw an exception that hasn't been caught
6700 * yet, or because a TerminateExecution exception was thrown. In that case, a
6701 * "Nothing" value is returned.
6702 */
6703template <class T>
6704class Maybe {
6705 public:
6706 V8_INLINE bool IsNothing() const { return !has_value; }
6707 V8_INLINE bool IsJust() const { return has_value; }
6708
6709 // Will crash if the Maybe<> is nothing.
6710 V8_INLINE T FromJust() const {
6711 if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing();
6712 return value;
6713 }
6714
6715 V8_INLINE T FromMaybe(const T& default_value) const {
6716 return has_value ? value : default_value;
6717 }
6718
6719 V8_INLINE bool operator==(const Maybe& other) const {
6720 return (IsJust() == other.IsJust()) &&
6721 (!IsJust() || FromJust() == other.FromJust());
6722 }
6723
6724 V8_INLINE bool operator!=(const Maybe& other) const {
6725 return !operator==(other);
6726 }
6727
6728 private:
6729 Maybe() : has_value(false) {}
6730 explicit Maybe(const T& t) : has_value(true), value(t) {}
6731
6732 bool has_value;
6733 T value;
6734
6735 template <class U>
6736 friend Maybe<U> Nothing();
6737 template <class U>
6738 friend Maybe<U> Just(const U& u);
6739};
6740
6741
6742template <class T>
6743inline Maybe<T> Nothing() {
6744 return Maybe<T>();
6745}
6746
6747
6748template <class T>
6749inline Maybe<T> Just(const T& t) {
6750 return Maybe<T>(t);
6751}
6752
6753
6754/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006755 * An external exception handler.
6756 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006757class V8_EXPORT TryCatch {
Steve Blocka7e24c12009-10-30 11:49:00 +00006758 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006759 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006760 * Creates a new try/catch block and registers it with v8. Note that
6761 * all TryCatch blocks should be stack allocated because the memory
6762 * location itself is compared against JavaScript try/catch blocks.
Steve Blocka7e24c12009-10-30 11:49:00 +00006763 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006764 V8_DEPRECATED("Use isolate version", TryCatch());
Steve Blocka7e24c12009-10-30 11:49:00 +00006765
6766 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006767 * Creates a new try/catch block and registers it with v8. Note that
6768 * all TryCatch blocks should be stack allocated because the memory
6769 * location itself is compared against JavaScript try/catch blocks.
6770 */
6771 TryCatch(Isolate* isolate);
6772
6773 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006774 * Unregisters and deletes this try/catch block.
6775 */
6776 ~TryCatch();
6777
6778 /**
6779 * Returns true if an exception has been caught by this try/catch block.
6780 */
6781 bool HasCaught() const;
6782
6783 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006784 * For certain types of exceptions, it makes no sense to continue execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006785 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006786 * If CanContinue returns false, the correct action is to perform any C++
6787 * cleanup needed and then return. If CanContinue returns false and
6788 * HasTerminated returns true, it is possible to call
6789 * CancelTerminateExecution in order to continue calling into the engine.
Steve Blocka7e24c12009-10-30 11:49:00 +00006790 */
6791 bool CanContinue() const;
6792
6793 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006794 * Returns true if an exception has been caught due to script execution
6795 * being terminated.
6796 *
6797 * There is no JavaScript representation of an execution termination
6798 * exception. Such exceptions are thrown when the TerminateExecution
6799 * methods are called to terminate a long-running script.
6800 *
6801 * If such an exception has been thrown, HasTerminated will return true,
6802 * indicating that it is possible to call CancelTerminateExecution in order
6803 * to continue calling into the engine.
6804 */
6805 bool HasTerminated() const;
6806
6807 /**
Steve Blockd0582a62009-12-15 09:54:21 +00006808 * Throws the exception caught by this TryCatch in a way that avoids
6809 * it being caught again by this same TryCatch. As with ThrowException
6810 * it is illegal to execute any JavaScript operations after calling
6811 * ReThrow; the caller must return immediately to where the exception
6812 * is caught.
6813 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006814 Local<Value> ReThrow();
Steve Blockd0582a62009-12-15 09:54:21 +00006815
6816 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006817 * Returns the exception caught by this try/catch block. If no exception has
6818 * been caught an empty handle is returned.
6819 *
6820 * The returned handle is valid until this TryCatch block has been destroyed.
6821 */
6822 Local<Value> Exception() const;
6823
6824 /**
6825 * Returns the .stack property of the thrown object. If no .stack
6826 * property is present an empty handle is returned.
6827 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006828 V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const);
6829 V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
6830 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006831
6832 /**
6833 * Returns the message associated with this exception. If there is
6834 * no message associated an empty handle is returned.
6835 *
6836 * The returned handle is valid until this TryCatch block has been
6837 * destroyed.
6838 */
6839 Local<v8::Message> Message() const;
6840
6841 /**
6842 * Clears any exceptions that may have been caught by this try/catch block.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006843 * After this method has been called, HasCaught() will return false. Cancels
6844 * the scheduled exception if it is caught and ReThrow() is not called before.
Steve Blocka7e24c12009-10-30 11:49:00 +00006845 *
6846 * It is not necessary to clear a try/catch block before using it again; if
6847 * another exception is thrown the previously caught exception will just be
6848 * overwritten. However, it is often a good idea since it makes it easier
6849 * to determine which operation threw a given exception.
6850 */
6851 void Reset();
6852
6853 /**
6854 * Set verbosity of the external exception handler.
6855 *
6856 * By default, exceptions that are caught by an external exception
6857 * handler are not reported. Call SetVerbose with true on an
6858 * external exception handler to have exceptions caught by the
6859 * handler reported as if they were not caught.
6860 */
6861 void SetVerbose(bool value);
6862
6863 /**
6864 * Set whether or not this TryCatch should capture a Message object
6865 * which holds source information about where the exception
6866 * occurred. True by default.
6867 */
6868 void SetCaptureMessage(bool value);
6869
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006870 /**
6871 * There are cases when the raw address of C++ TryCatch object cannot be
6872 * used for comparisons with addresses into the JS stack. The cases are:
6873 * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
6874 * 2) Address sanitizer allocates local C++ object in the heap when
6875 * UseAfterReturn mode is enabled.
6876 * This method returns address that can be used for comparisons with
6877 * addresses into the JS stack. When neither simulator nor ASAN's
6878 * UseAfterReturn is enabled, then the address returned will be the address
6879 * of the C++ try catch handler itself.
6880 */
6881 static void* JSStackComparableAddress(v8::TryCatch* handler) {
6882 if (handler == NULL) return NULL;
6883 return handler->js_stack_comparable_address_;
6884 }
6885
Steve Blockd0582a62009-12-15 09:54:21 +00006886 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006887 void ResetInternal();
6888
6889 // Make it hard to create heap-allocated TryCatch blocks.
6890 TryCatch(const TryCatch&);
6891 void operator=(const TryCatch&);
6892 void* operator new(size_t size);
6893 void operator delete(void*, size_t);
6894
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006895 v8::internal::Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006896 v8::TryCatch* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006897 void* exception_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006898 void* message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006899 void* js_stack_comparable_address_;
Steve Blockd0582a62009-12-15 09:54:21 +00006900 bool is_verbose_ : 1;
6901 bool can_continue_ : 1;
6902 bool capture_message_ : 1;
6903 bool rethrow_ : 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006904 bool has_terminated_ : 1;
Steve Blockd0582a62009-12-15 09:54:21 +00006905
Steve Block44f0eee2011-05-26 01:26:41 +01006906 friend class v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +00006907};
6908
6909
Ben Murdoch257744e2011-11-30 15:57:28 +00006910// --- Context ---
Steve Blocka7e24c12009-10-30 11:49:00 +00006911
6912
6913/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006914 * A container for extension names.
Steve Blocka7e24c12009-10-30 11:49:00 +00006915 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006916class V8_EXPORT ExtensionConfiguration {
Steve Blocka7e24c12009-10-30 11:49:00 +00006917 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006918 ExtensionConfiguration() : name_count_(0), names_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00006919 ExtensionConfiguration(int name_count, const char* names[])
6920 : name_count_(name_count), names_(names) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006921
6922 const char** begin() const { return &names_[0]; }
6923 const char** end() const { return &names_[name_count_]; }
6924
Steve Blocka7e24c12009-10-30 11:49:00 +00006925 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006926 const int name_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006927 const char** names_;
6928};
6929
6930
6931/**
6932 * A sandboxed execution context with its own set of built-in objects
6933 * and functions.
6934 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006935class V8_EXPORT Context {
Steve Blocka7e24c12009-10-30 11:49:00 +00006936 public:
Steve Block1e0659c2011-05-24 12:43:12 +01006937 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006938 * Returns the global proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006939 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006940 * Global proxy object is a thin wrapper whose prototype points to actual
6941 * context's global object with the properties like Object, etc. This is done
6942 * that way for security reasons (for more details see
Steve Block1e0659c2011-05-24 12:43:12 +01006943 * https://wiki.mozilla.org/Gecko:SplitWindow).
6944 *
6945 * Please note that changes to global proxy object prototype most probably
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006946 * would break VM---v8 expects only global object as a prototype of global
6947 * proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01006948 */
Steve Blocka7e24c12009-10-30 11:49:00 +00006949 Local<Object> Global();
6950
6951 /**
6952 * Detaches the global object from its context before
6953 * the global object can be reused to create a new context.
6954 */
6955 void DetachGlobal();
6956
Andrei Popescu74b3c142010-03-29 12:03:09 +01006957 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006958 * Creates a new context and returns a handle to the newly allocated
6959 * context.
Andrei Popescu74b3c142010-03-29 12:03:09 +01006960 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006961 * \param isolate The isolate in which to create the context.
Steve Block9fac8402011-05-12 15:51:54 +01006962 *
6963 * \param extensions An optional extension configuration containing
6964 * the extensions to be installed in the newly created context.
6965 *
6966 * \param global_template An optional object template from which the
6967 * global object for the newly created context will be created.
6968 *
6969 * \param global_object An optional global object to be reused for
6970 * the newly created context. This global object must have been
6971 * created by a previous call to Context::New with the same global
6972 * template. The state of the global object will be completely reset
6973 * and only object identify will remain.
Leon Clarkef7060e22010-06-03 12:02:55 +01006974 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006975 static Local<Context> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006976 Isolate* isolate, ExtensionConfiguration* extensions = NULL,
6977 Local<ObjectTemplate> global_template = Local<ObjectTemplate>(),
6978 Local<Value> global_object = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00006979
Steve Blocka7e24c12009-10-30 11:49:00 +00006980 /**
6981 * Sets the security token for the context. To access an object in
6982 * another context, the security tokens must match.
6983 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006984 void SetSecurityToken(Local<Value> token);
Steve Blocka7e24c12009-10-30 11:49:00 +00006985
6986 /** Restores the security token to the default value. */
6987 void UseDefaultSecurityToken();
6988
6989 /** Returns the security token of this context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006990 Local<Value> GetSecurityToken();
Steve Blocka7e24c12009-10-30 11:49:00 +00006991
6992 /**
6993 * Enter this context. After entering a context, all code compiled
6994 * and run is compiled and run in this context. If another context
6995 * is already entered, this old context is saved so it can be
6996 * restored when the new context is exited.
6997 */
6998 void Enter();
6999
7000 /**
7001 * Exit this context. Exiting the current context restores the
7002 * context that was in place when entering the current context.
7003 */
7004 void Exit();
7005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007006 /** Returns an isolate associated with a current context. */
7007 v8::Isolate* GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007008
7009 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007010 * The field at kDebugIdIndex is reserved for V8 debugger implementation.
7011 * The value is propagated to the scripts compiled in given Context and
7012 * can be used for filtering scripts.
7013 */
7014 enum EmbedderDataFields { kDebugIdIndex = 0 };
7015
7016 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007017 * Gets the embedder data with the given index, which must have been set by a
7018 * previous call to SetEmbedderData with the same index. Note that index 0
7019 * currently has a special meaning for Chrome's debugger.
Steve Blocka7e24c12009-10-30 11:49:00 +00007020 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007021 V8_INLINE Local<Value> GetEmbedderData(int index);
7022
7023 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007024 * Gets the binding object used by V8 extras. Extra natives get a reference
7025 * to this object and can use it to "export" functionality by adding
7026 * properties. Extra natives can also "import" functionality by accessing
7027 * properties added by the embedder using the V8 API.
7028 */
7029 Local<Object> GetExtrasBindingObject();
7030
7031 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007032 * Sets the embedder data with the given index, growing the data as
7033 * needed. Note that index 0 currently has a special meaning for Chrome's
7034 * debugger.
7035 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007036 void SetEmbedderData(int index, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007037
7038 /**
7039 * Gets a 2-byte-aligned native pointer from the embedder data with the given
7040 * index, which must have bees set by a previous call to
7041 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
7042 * currently has a special meaning for Chrome's debugger.
7043 */
7044 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
7045
7046 /**
7047 * Sets a 2-byte-aligned native pointer in the embedder data with the given
7048 * index, growing the data as needed. Note that index 0 currently has a
7049 * special meaning for Chrome's debugger.
7050 */
7051 void SetAlignedPointerInEmbedderData(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007052
7053 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00007054 * Control whether code generation from strings is allowed. Calling
7055 * this method with false will disable 'eval' and the 'Function'
7056 * constructor for code running in this context. If 'eval' or the
7057 * 'Function' constructor are used an exception will be thrown.
7058 *
7059 * If code generation from strings is not allowed the
7060 * V8::AllowCodeGenerationFromStrings callback will be invoked if
7061 * set before blocking the call to 'eval' or the 'Function'
7062 * constructor. If that callback returns true, the call will be
7063 * allowed, otherwise an exception will be thrown. If no callback is
7064 * set an exception will be thrown.
7065 */
7066 void AllowCodeGenerationFromStrings(bool allow);
7067
7068 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007069 * Returns true if code generation from strings is allowed for the context.
7070 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
7071 */
7072 bool IsCodeGenerationFromStringsAllowed();
7073
7074 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007075 * Sets the error description for the exception that is thrown when
7076 * code generation from strings is not allowed and 'eval' or the 'Function'
7077 * constructor are called.
7078 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007079 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
7080
7081 /**
7082 * Estimate the memory in bytes retained by this context.
7083 */
7084 size_t EstimatedSize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007085
7086 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00007087 * Stack-allocated class which sets the execution context for all
7088 * operations executed within a local scope.
7089 */
Steve Block8defd9f2010-07-08 12:39:36 +01007090 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00007091 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007092 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007093 context_->Enter();
7094 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007095 V8_INLINE ~Scope() { context_->Exit(); }
7096
Steve Blocka7e24c12009-10-30 11:49:00 +00007097 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007098 Local<Context> context_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007099 };
7100
7101 private:
7102 friend class Value;
7103 friend class Script;
7104 friend class Object;
7105 friend class Function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007106
7107 Local<Value> SlowGetEmbedderData(int index);
7108 void* SlowGetAlignedPointerFromEmbedderData(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007109};
7110
7111
7112/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007113 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
7114 * to use any given V8 isolate, see the comments in the Isolate class. The
7115 * definition of 'using a V8 isolate' includes accessing handles or holding onto
7116 * object pointers obtained from V8 handles while in the particular V8 isolate.
7117 * It is up to the user of V8 to ensure, perhaps with locking, that this
7118 * constraint is not violated. In addition to any other synchronization
7119 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
7120 * used to signal thead switches to V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00007121 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007122 * v8::Locker is a scoped lock object. While it's active, i.e. between its
7123 * construction and destruction, the current thread is allowed to use the locked
7124 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
7125 * any time. In other words, the scope of a v8::Locker is a critical section.
Steve Block44f0eee2011-05-26 01:26:41 +01007126 *
Ben Murdoch257744e2011-11-30 15:57:28 +00007127 * Sample usage:
7128* \code
Steve Blocka7e24c12009-10-30 11:49:00 +00007129 * ...
7130 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007131 * v8::Locker locker(isolate);
7132 * v8::Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007133 * ...
Ben Murdoch257744e2011-11-30 15:57:28 +00007134 * // Code using V8 and isolate goes here.
Steve Blocka7e24c12009-10-30 11:49:00 +00007135 * ...
7136 * } // Destructor called here
7137 * \endcode
7138 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007139 * If you wish to stop using V8 in a thread A you can do this either by
7140 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
7141 * object:
Steve Blocka7e24c12009-10-30 11:49:00 +00007142 *
7143 * \code
7144 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007145 * isolate->Exit();
7146 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007147 * ...
7148 * // Code not using V8 goes here while V8 can run in another thread.
7149 * ...
7150 * } // Destructor called here.
Ben Murdoch257744e2011-11-30 15:57:28 +00007151 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007152 * \endcode
7153 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007154 * The Unlocker object is intended for use in a long-running callback from V8,
7155 * where you want to release the V8 lock for other threads to use.
Steve Blocka7e24c12009-10-30 11:49:00 +00007156 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007157 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
7158 * given thread. This can be useful if you have code that can be called either
7159 * from code that holds the lock or from code that does not. The Unlocker is
7160 * not recursive so you can not have several Unlockers on the stack at once, and
7161 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00007162 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007163 * An unlocker will unlock several lockers if it has to and reinstate the
7164 * correct depth of locking on its destruction, e.g.:
Steve Blocka7e24c12009-10-30 11:49:00 +00007165 *
7166 * \code
7167 * // V8 not locked.
7168 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007169 * v8::Locker locker(isolate);
7170 * Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007171 * // V8 locked.
7172 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007173 * v8::Locker another_locker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007174 * // V8 still locked (2 levels).
7175 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007176 * isolate->Exit();
7177 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007178 * // V8 not locked.
7179 * }
Ben Murdoch257744e2011-11-30 15:57:28 +00007180 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007181 * // V8 locked again (2 levels).
7182 * }
7183 * // V8 still locked (1 level).
7184 * }
7185 * // V8 Now no longer locked.
7186 * \endcode
7187 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007188class V8_EXPORT Unlocker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007189 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007190 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007191 * Initialize Unlocker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007192 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007193 V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
7194
Steve Blocka7e24c12009-10-30 11:49:00 +00007195 ~Unlocker();
Ben Murdoch257744e2011-11-30 15:57:28 +00007196 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007197 void Initialize(Isolate* isolate);
7198
Ben Murdoch257744e2011-11-30 15:57:28 +00007199 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007200};
7201
7202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007203class V8_EXPORT Locker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007204 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007205 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007206 * Initialize Locker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007207 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007208 V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
7209
Steve Blocka7e24c12009-10-30 11:49:00 +00007210 ~Locker();
7211
7212 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007213 * Returns whether or not the locker for a given isolate, is locked by the
7214 * current thread.
Steve Blocka7e24c12009-10-30 11:49:00 +00007215 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007216 static bool IsLocked(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007217
7218 /**
7219 * Returns whether v8::Locker is being used by this V8 instance.
7220 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007221 static bool IsActive();
Steve Blocka7e24c12009-10-30 11:49:00 +00007222
7223 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007224 void Initialize(Isolate* isolate);
7225
Steve Blocka7e24c12009-10-30 11:49:00 +00007226 bool has_lock_;
7227 bool top_level_;
Ben Murdoch257744e2011-11-30 15:57:28 +00007228 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007229
Steve Blocka7e24c12009-10-30 11:49:00 +00007230 // Disallow copying and assigning.
7231 Locker(const Locker&);
7232 void operator=(const Locker&);
7233};
7234
7235
Ben Murdoch257744e2011-11-30 15:57:28 +00007236// --- Implementation ---
Steve Blocka7e24c12009-10-30 11:49:00 +00007237
7238
7239namespace internal {
7240
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007241const int kApiPointerSize = sizeof(void*); // NOLINT
7242const int kApiIntSize = sizeof(int); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007243const int kApiInt64Size = sizeof(int64_t); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00007244
7245// Tag information for HeapObject.
7246const int kHeapObjectTag = 1;
7247const int kHeapObjectTagSize = 2;
7248const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
7249
Steve Blocka7e24c12009-10-30 11:49:00 +00007250// Tag information for Smi.
7251const int kSmiTag = 0;
7252const int kSmiTagSize = 1;
7253const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
7254
Ben Murdochb8e0da22011-05-16 14:20:40 +01007255template <size_t ptr_size> struct SmiTagging;
Steve Block3ce2e202009-11-05 08:53:23 +00007256
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007257template<int kSmiShiftSize>
7258V8_INLINE internal::Object* IntToSmi(int value) {
7259 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
7260 uintptr_t tagged_value =
7261 (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag;
7262 return reinterpret_cast<internal::Object*>(tagged_value);
7263}
7264
Steve Block3ce2e202009-11-05 08:53:23 +00007265// Smi constants for 32-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007266template <> struct SmiTagging<4> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007267 enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
7268 static int SmiShiftSize() { return kSmiShiftSize; }
7269 static int SmiValueSize() { return kSmiValueSize; }
7270 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007271 int shift_bits = kSmiTagSize + kSmiShiftSize;
7272 // Throw away top 32 bits and shift down (requires >> to be sign extending).
7273 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
7274 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007275 V8_INLINE static internal::Object* IntToSmi(int value) {
7276 return internal::IntToSmi<kSmiShiftSize>(value);
7277 }
7278 V8_INLINE static bool IsValidSmi(intptr_t value) {
7279 // To be representable as an tagged small integer, the two
7280 // most-significant bits of 'value' must be either 00 or 11 due to
7281 // sign-extension. To check this we add 01 to the two
7282 // most-significant bits, and check if the most-significant bit is 0
7283 //
7284 // CAUTION: The original code below:
7285 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
7286 // may lead to incorrect results according to the C language spec, and
7287 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
7288 // compiler may produce undefined results in case of signed integer
7289 // overflow. The computation must be done w/ unsigned ints.
7290 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
7291 }
Steve Block3ce2e202009-11-05 08:53:23 +00007292};
7293
7294// Smi constants for 64-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007295template <> struct SmiTagging<8> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007296 enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
7297 static int SmiShiftSize() { return kSmiShiftSize; }
7298 static int SmiValueSize() { return kSmiValueSize; }
7299 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007300 int shift_bits = kSmiTagSize + kSmiShiftSize;
7301 // Shift down and throw away top 32 bits.
7302 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
7303 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007304 V8_INLINE static internal::Object* IntToSmi(int value) {
7305 return internal::IntToSmi<kSmiShiftSize>(value);
7306 }
7307 V8_INLINE static bool IsValidSmi(intptr_t value) {
7308 // To be representable as a long smi, the value must be a 32-bit integer.
7309 return (value == static_cast<int32_t>(value));
7310 }
Steve Block3ce2e202009-11-05 08:53:23 +00007311};
7312
Ben Murdochb8e0da22011-05-16 14:20:40 +01007313typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
7314const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
7315const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007316V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
7317V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
Steve Blockd0582a62009-12-15 09:54:21 +00007318
Steve Blocka7e24c12009-10-30 11:49:00 +00007319/**
7320 * This class exports constants and functionality from within v8 that
7321 * is necessary to implement inline functions in the v8 api. Don't
7322 * depend on functions and constants defined here.
7323 */
7324class Internals {
7325 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00007326 // These values match non-compiler-dependent values defined within
7327 // the implementation of v8.
7328 static const int kHeapObjectMapOffset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007329 static const int kMapInstanceTypeAndBitFieldOffset =
7330 1 * kApiPointerSize + kApiIntSize;
7331 static const int kStringResourceOffset = 3 * kApiPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00007332
Ben Murdochda12d292016-06-02 14:46:10 +01007333 static const int kOddballKindOffset = 5 * kApiPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00007334 static const int kForeignAddressOffset = kApiPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007335 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007336 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
7337 static const int kContextHeaderSize = 2 * kApiPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007338 static const int kContextEmbedderDataIndex = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00007339 static const int kFullStringRepresentationMask = 0x07;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007340 static const int kStringEncodingMask = 0x4;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007341 static const int kExternalTwoByteRepresentationTag = 0x02;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007342 static const int kExternalOneByteRepresentationTag = 0x06;
Steve Blocka7e24c12009-10-30 11:49:00 +00007343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007344 static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize;
7345 static const int kAmountOfExternalAllocatedMemoryOffset =
7346 4 * kApiPointerSize;
7347 static const int kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset =
7348 kAmountOfExternalAllocatedMemoryOffset + kApiInt64Size;
7349 static const int kIsolateRootsOffset =
7350 kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size +
7351 kApiPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01007352 static const int kUndefinedValueRootIndex = 4;
7353 static const int kTheHoleValueRootIndex = 5;
7354 static const int kNullValueRootIndex = 6;
7355 static const int kTrueValueRootIndex = 7;
7356 static const int kFalseValueRootIndex = 8;
7357 static const int kEmptyStringRootIndex = 9;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007358
7359 // The external allocation limit should be below 256 MB on all architectures
7360 // to avoid that resource-constrained embedders run low on memory.
7361 static const int kExternalAllocationLimit = 192 * 1024 * 1024;
7362
7363 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
7364 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007365 static const int kNodeStateMask = 0x7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007366 static const int kNodeStateIsWeakValue = 2;
7367 static const int kNodeStateIsPendingValue = 3;
7368 static const int kNodeStateIsNearDeathValue = 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007369 static const int kNodeIsIndependentShift = 3;
7370 static const int kNodeIsPartiallyDependentShift = 4;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007371 static const int kNodeIsActiveShift = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007372
Ben Murdochda12d292016-06-02 14:46:10 +01007373 static const int kJSObjectType = 0xb8;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01007374 static const int kFirstNonstringType = 0x80;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007375 static const int kOddballType = 0x83;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007376 static const int kForeignType = 0x87;
Steve Blocka7e24c12009-10-30 11:49:00 +00007377
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007378 static const int kUndefinedOddballKind = 5;
7379 static const int kNullOddballKind = 3;
7380
7381 static const uint32_t kNumIsolateDataSlots = 4;
7382
7383 V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
7384 V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
7385#ifdef V8_ENABLE_CHECKS
7386 CheckInitializedImpl(isolate);
7387#endif
7388 }
7389
7390 V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007391 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
7392 kHeapObjectTag);
7393 }
7394
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007395 V8_INLINE static int SmiValue(const internal::Object* value) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01007396 return PlatformSmiTagging::SmiToInt(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007397 }
7398
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007399 V8_INLINE static internal::Object* IntToSmi(int value) {
7400 return PlatformSmiTagging::IntToSmi(value);
7401 }
7402
7403 V8_INLINE static bool IsValidSmi(intptr_t value) {
7404 return PlatformSmiTagging::IsValidSmi(value);
7405 }
7406
7407 V8_INLINE static int GetInstanceType(const internal::Object* obj) {
Steve Block3ce2e202009-11-05 08:53:23 +00007408 typedef internal::Object O;
7409 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007410 // Map::InstanceType is defined so that it will always be loaded into
7411 // the LS 8 bits of one 16-bit word, regardless of endianess.
7412 return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff;
Steve Block3ce2e202009-11-05 08:53:23 +00007413 }
7414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007415 V8_INLINE static int GetOddballKind(const internal::Object* obj) {
7416 typedef internal::Object O;
7417 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01007418 }
7419
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007420 V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007421 int representation = (instance_type & kFullStringRepresentationMask);
7422 return representation == kExternalTwoByteRepresentationTag;
7423 }
7424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007425 V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) {
7426 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7427 return *addr & static_cast<uint8_t>(1U << shift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007428 }
Steve Block44f0eee2011-05-26 01:26:41 +01007429
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007430 V8_INLINE static void UpdateNodeFlag(internal::Object** obj,
7431 bool value, int shift) {
7432 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7433 uint8_t mask = static_cast<uint8_t>(1U << shift);
7434 *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
7435 }
7436
7437 V8_INLINE static uint8_t GetNodeState(internal::Object** obj) {
7438 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7439 return *addr & kNodeStateMask;
7440 }
7441
7442 V8_INLINE static void UpdateNodeState(internal::Object** obj,
7443 uint8_t value) {
7444 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7445 *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
7446 }
7447
7448 V8_INLINE static void SetEmbedderData(v8::Isolate* isolate,
7449 uint32_t slot,
7450 void* data) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007451 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007452 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7453 *reinterpret_cast<void**>(addr) = data;
7454 }
7455
7456 V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
7457 uint32_t slot) {
7458 const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) +
7459 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7460 return *reinterpret_cast<void* const*>(addr);
7461 }
7462
7463 V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate,
7464 int index) {
7465 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
7466 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
7467 }
7468
7469 template <typename T>
7470 V8_INLINE static T ReadField(const internal::Object* ptr, int offset) {
7471 const uint8_t* addr =
7472 reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag;
7473 return *reinterpret_cast<const T*>(addr);
7474 }
7475
7476 template <typename T>
7477 V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) {
7478 typedef internal::Object O;
7479 typedef internal::Internals I;
7480 O* ctx = *reinterpret_cast<O* const*>(context);
7481 int embedder_data_offset = I::kContextHeaderSize +
7482 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
7483 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
7484 int value_offset =
7485 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
7486 return I::ReadField<T>(embedder_data, value_offset);
7487 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007488};
7489
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007490} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +00007491
7492
7493template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007494Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007495 return New(isolate, that.val_);
7496}
7497
7498template <class T>
7499Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
7500 return New(isolate, that.val_);
7501}
7502
Steve Blocka7e24c12009-10-30 11:49:00 +00007503
7504template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007505Local<T> Local<T>::New(Isolate* isolate, T* that) {
7506 if (that == NULL) return Local<T>();
7507 T* that_ptr = that;
7508 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
7509 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
7510 reinterpret_cast<internal::Isolate*>(isolate), *p)));
7511}
7512
7513
7514template<class T>
7515template<class S>
7516void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
7517 TYPE_CHECK(T, S);
7518 V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
7519}
7520
7521
7522template<class T>
7523Local<T> Eternal<T>::Get(Isolate* isolate) {
7524 return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
Steve Blocka7e24c12009-10-30 11:49:00 +00007525}
7526
7527
7528template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007529Local<T> MaybeLocal<T>::ToLocalChecked() {
7530 if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty();
7531 return Local<T>(val_);
7532}
7533
7534
7535template <class T>
7536void* WeakCallbackInfo<T>::GetInternalField(int index) const {
7537#ifdef V8_ENABLE_CHECKS
7538 if (index < 0 || index >= kInternalFieldsInWeakCallback) {
7539 V8::InternalFieldOutOfBounds(index);
7540 }
7541#endif
7542 return internal_fields_[index];
7543}
7544
7545
7546template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007547T* PersistentBase<T>::New(Isolate* isolate, T* that) {
7548 if (that == NULL) return NULL;
7549 internal::Object** p = reinterpret_cast<internal::Object**>(that);
7550 return reinterpret_cast<T*>(
7551 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
7552 p));
7553}
7554
7555
7556template <class T, class M>
7557template <class S, class M2>
7558void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
7559 TYPE_CHECK(T, S);
7560 this->Reset();
7561 if (that.IsEmpty()) return;
7562 internal::Object** p = reinterpret_cast<internal::Object**>(that.val_);
7563 this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p));
7564 M::Copy(that, this);
7565}
7566
7567
7568template <class T>
7569bool PersistentBase<T>::IsIndependent() const {
7570 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007571 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007572 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7573 I::kNodeIsIndependentShift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007574}
7575
7576
7577template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007578bool PersistentBase<T>::IsNearDeath() const {
7579 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007580 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007581 uint8_t node_state =
7582 I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_));
7583 return node_state == I::kNodeStateIsNearDeathValue ||
7584 node_state == I::kNodeStateIsPendingValue;
Steve Blocka7e24c12009-10-30 11:49:00 +00007585}
7586
7587
7588template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007589bool PersistentBase<T>::IsWeak() const {
7590 typedef internal::Internals I;
7591 if (this->IsEmpty()) return false;
7592 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
7593 I::kNodeStateIsWeakValue;
7594}
7595
7596
7597template <class T>
7598void PersistentBase<T>::Reset() {
Steve Blocka7e24c12009-10-30 11:49:00 +00007599 if (this->IsEmpty()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007600 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
7601 val_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00007602}
7603
7604
7605template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007606template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007607void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007608 TYPE_CHECK(T, S);
7609 Reset();
7610 if (other.IsEmpty()) return;
7611 this->val_ = New(isolate, other.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007612}
7613
Steve Blocka7e24c12009-10-30 11:49:00 +00007614
Steve Block44f0eee2011-05-26 01:26:41 +01007615template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007616template <class S>
7617void PersistentBase<T>::Reset(Isolate* isolate,
7618 const PersistentBase<S>& other) {
7619 TYPE_CHECK(T, S);
7620 Reset();
7621 if (other.IsEmpty()) return;
7622 this->val_ = New(isolate, other.val_);
Ben Murdoch257744e2011-11-30 15:57:28 +00007623}
7624
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007625
Ben Murdoch257744e2011-11-30 15:57:28 +00007626template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007627template <typename S, typename P>
7628void PersistentBase<T>::SetWeak(
7629 P* parameter,
7630 typename WeakCallbackData<S, P>::Callback callback) {
7631 TYPE_CHECK(S, T);
7632 typedef typename WeakCallbackData<Value, void>::Callback Callback;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007633 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007634 reinterpret_cast<Callback>(callback));
Steve Block44f0eee2011-05-26 01:26:41 +01007635}
Steve Block8defd9f2010-07-08 12:39:36 +01007636
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007637
7638template <class T>
7639template <typename P>
7640void PersistentBase<T>::SetWeak(
7641 P* parameter,
7642 typename WeakCallbackData<T, P>::Callback callback) {
7643 SetWeak<T, P>(parameter, callback);
7644}
7645
7646
7647template <class T>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007648template <typename P>
7649void PersistentBase<T>::SetPhantom(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007650 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7651 int internal_field_index1, int internal_field_index2) {
7652 typedef typename WeakCallbackInfo<void>::Callback Callback;
7653 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7654 internal_field_index1, internal_field_index2,
7655 reinterpret_cast<Callback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007656}
7657
7658
7659template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007660template <typename P>
7661V8_INLINE void PersistentBase<T>::SetWeak(
7662 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7663 WeakCallbackType type) {
7664 typedef typename WeakCallbackInfo<void>::Callback Callback;
7665 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7666 reinterpret_cast<Callback>(callback), type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007667}
7668
7669
7670template <class T>
7671template <typename P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007672P* PersistentBase<T>::ClearWeak() {
7673 return reinterpret_cast<P*>(
7674 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
7675}
7676
Ben Murdochda12d292016-06-02 14:46:10 +01007677template <class T>
7678void PersistentBase<T>::RegisterExternalReference(Isolate* isolate) {
7679 if (IsEmpty()) return;
7680 V8::RegisterExternallyReferencedObject(
7681 reinterpret_cast<internal::Object**>(this->val_),
7682 reinterpret_cast<internal::Isolate*>(isolate));
7683}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007684
7685template <class T>
7686void PersistentBase<T>::MarkIndependent() {
7687 typedef internal::Internals I;
7688 if (this->IsEmpty()) return;
7689 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7690 true,
7691 I::kNodeIsIndependentShift);
7692}
7693
7694
7695template <class T>
7696void PersistentBase<T>::MarkPartiallyDependent() {
7697 typedef internal::Internals I;
7698 if (this->IsEmpty()) return;
7699 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7700 true,
7701 I::kNodeIsPartiallyDependentShift);
7702}
7703
7704
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007705template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007706void PersistentBase<T>::MarkActive() {
7707 typedef internal::Internals I;
7708 if (this->IsEmpty()) return;
7709 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true,
7710 I::kNodeIsActiveShift);
7711}
7712
7713
7714template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007715void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) {
7716 typedef internal::Internals I;
7717 if (this->IsEmpty()) return;
7718 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7719 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7720 *reinterpret_cast<uint16_t*>(addr) = class_id;
7721}
7722
7723
7724template <class T>
7725uint16_t PersistentBase<T>::WrapperClassId() const {
7726 typedef internal::Internals I;
7727 if (this->IsEmpty()) return 0;
7728 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7729 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7730 return *reinterpret_cast<uint16_t*>(addr);
7731}
7732
7733
7734template<typename T>
7735ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
7736
7737template<typename T>
7738template<typename S>
7739void ReturnValue<T>::Set(const Persistent<S>& handle) {
7740 TYPE_CHECK(T, S);
7741 if (V8_UNLIKELY(handle.IsEmpty())) {
7742 *value_ = GetDefaultValue();
7743 } else {
7744 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7745 }
7746}
7747
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007748template <typename T>
7749template <typename S>
7750void ReturnValue<T>::Set(const Global<S>& handle) {
7751 TYPE_CHECK(T, S);
7752 if (V8_UNLIKELY(handle.IsEmpty())) {
7753 *value_ = GetDefaultValue();
7754 } else {
7755 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7756 }
7757}
7758
7759template <typename T>
7760template <typename S>
7761void ReturnValue<T>::Set(const Local<S> handle) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007762 TYPE_CHECK(T, S);
7763 if (V8_UNLIKELY(handle.IsEmpty())) {
7764 *value_ = GetDefaultValue();
7765 } else {
7766 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7767 }
7768}
7769
7770template<typename T>
7771void ReturnValue<T>::Set(double i) {
7772 TYPE_CHECK(T, Number);
7773 Set(Number::New(GetIsolate(), i));
7774}
7775
7776template<typename T>
7777void ReturnValue<T>::Set(int32_t i) {
7778 TYPE_CHECK(T, Integer);
7779 typedef internal::Internals I;
7780 if (V8_LIKELY(I::IsValidSmi(i))) {
7781 *value_ = I::IntToSmi(i);
7782 return;
7783 }
7784 Set(Integer::New(GetIsolate(), i));
7785}
7786
7787template<typename T>
7788void ReturnValue<T>::Set(uint32_t i) {
7789 TYPE_CHECK(T, Integer);
7790 // Can't simply use INT32_MAX here for whatever reason.
7791 bool fits_into_int32_t = (i & (1U << 31)) == 0;
7792 if (V8_LIKELY(fits_into_int32_t)) {
7793 Set(static_cast<int32_t>(i));
7794 return;
7795 }
7796 Set(Integer::NewFromUnsigned(GetIsolate(), i));
7797}
7798
7799template<typename T>
7800void ReturnValue<T>::Set(bool value) {
7801 TYPE_CHECK(T, Boolean);
7802 typedef internal::Internals I;
7803 int root_index;
7804 if (value) {
7805 root_index = I::kTrueValueRootIndex;
7806 } else {
7807 root_index = I::kFalseValueRootIndex;
7808 }
7809 *value_ = *I::GetRoot(GetIsolate(), root_index);
7810}
7811
7812template<typename T>
7813void ReturnValue<T>::SetNull() {
7814 TYPE_CHECK(T, Primitive);
7815 typedef internal::Internals I;
7816 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
7817}
7818
7819template<typename T>
7820void ReturnValue<T>::SetUndefined() {
7821 TYPE_CHECK(T, Primitive);
7822 typedef internal::Internals I;
7823 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
7824}
7825
7826template<typename T>
7827void ReturnValue<T>::SetEmptyString() {
7828 TYPE_CHECK(T, String);
7829 typedef internal::Internals I;
7830 *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
7831}
7832
Ben Murdochda12d292016-06-02 14:46:10 +01007833template <typename T>
7834Isolate* ReturnValue<T>::GetIsolate() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007835 // Isolate is always the pointer below the default value on the stack.
7836 return *reinterpret_cast<Isolate**>(&value_[-2]);
7837}
7838
Ben Murdochda12d292016-06-02 14:46:10 +01007839template <typename T>
7840Local<Value> ReturnValue<T>::Get() const {
7841 typedef internal::Internals I;
7842 if (*value_ == *I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex))
7843 return Local<Value>(*Undefined(GetIsolate()));
7844 return Local<Value>::New(GetIsolate(), reinterpret_cast<Value*>(value_));
7845}
7846
7847template <typename T>
7848template <typename S>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007849void ReturnValue<T>::Set(S* whatever) {
7850 // Uncompilable to prevent inadvertent misuse.
7851 TYPE_CHECK(S*, Primitive);
7852}
7853
7854template<typename T>
7855internal::Object* ReturnValue<T>::GetDefaultValue() {
7856 // Default value is always the pointer below value_ on the stack.
7857 return value_[-1];
7858}
7859
7860
7861template<typename T>
7862FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
7863 internal::Object** values,
7864 int length,
7865 bool is_construct_call)
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007866 : implicit_args_(implicit_args),
7867 values_(values),
7868 length_(length),
7869 is_construct_call_(is_construct_call) { }
Steve Block8defd9f2010-07-08 12:39:36 +01007870
7871
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007872template<typename T>
7873Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
7874 if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +00007875 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
7876}
7877
7878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007879template<typename T>
7880Local<Function> FunctionCallbackInfo<T>::Callee() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007881 return Local<Function>(reinterpret_cast<Function*>(
7882 &implicit_args_[kCalleeIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007883}
7884
7885
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007886template<typename T>
7887Local<Object> FunctionCallbackInfo<T>::This() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007888 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
7889}
7890
7891
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007892template<typename T>
7893Local<Object> FunctionCallbackInfo<T>::Holder() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007894 return Local<Object>(reinterpret_cast<Object*>(
7895 &implicit_args_[kHolderIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007896}
7897
7898
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007899template<typename T>
7900Local<Value> FunctionCallbackInfo<T>::Data() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007901 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00007902}
7903
7904
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007905template<typename T>
7906Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
7907 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
7908}
7909
7910
7911template<typename T>
7912ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
7913 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
7914}
7915
7916
7917template<typename T>
7918bool FunctionCallbackInfo<T>::IsConstructCall() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007919 return is_construct_call_ & 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +00007920}
7921
7922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007923template<typename T>
7924int FunctionCallbackInfo<T>::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007925 return length_;
7926}
7927
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007928ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
7929 Local<Integer> resource_line_offset,
7930 Local<Integer> resource_column_offset,
7931 Local<Boolean> resource_is_shared_cross_origin,
7932 Local<Integer> script_id,
7933 Local<Boolean> resource_is_embedder_debug_script,
7934 Local<Value> source_map_url,
7935 Local<Boolean> resource_is_opaque)
7936 : resource_name_(resource_name),
7937 resource_line_offset_(resource_line_offset),
7938 resource_column_offset_(resource_column_offset),
7939 options_(!resource_is_embedder_debug_script.IsEmpty() &&
7940 resource_is_embedder_debug_script->IsTrue(),
7941 !resource_is_shared_cross_origin.IsEmpty() &&
7942 resource_is_shared_cross_origin->IsTrue(),
7943 !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()),
7944 script_id_(script_id),
7945 source_map_url_(source_map_url) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00007946
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007947Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007948
7949
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007950Local<Integer> ScriptOrigin::ResourceLineOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007951 return resource_line_offset_;
7952}
7953
7954
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007955Local<Integer> ScriptOrigin::ResourceColumnOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00007956 return resource_column_offset_;
7957}
7958
7959
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007960Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00007961
7962
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007963Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007964
7965
7966ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
7967 CachedData* data)
7968 : source_string(string),
7969 resource_name(origin.ResourceName()),
7970 resource_line_offset(origin.ResourceLineOffset()),
7971 resource_column_offset(origin.ResourceColumnOffset()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007972 resource_options(origin.Options()),
7973 source_map_url(origin.SourceMapUrl()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007974 cached_data(data) {}
7975
7976
7977ScriptCompiler::Source::Source(Local<String> string,
7978 CachedData* data)
7979 : source_string(string), cached_data(data) {}
7980
7981
7982ScriptCompiler::Source::~Source() {
7983 delete cached_data;
7984}
7985
7986
7987const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
7988 const {
7989 return cached_data;
7990}
7991
7992
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007993Local<Boolean> Boolean::New(Isolate* isolate, bool value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007994 return value ? True(isolate) : False(isolate);
7995}
7996
7997
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007998void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) {
7999 Set(v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
8000 .ToLocalChecked(),
8001 value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008002}
8003
8004
8005Local<Value> Object::GetInternalField(int index) {
8006#ifndef V8_ENABLE_CHECKS
Steve Blocka7e24c12009-10-30 11:49:00 +00008007 typedef internal::Object O;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008008 typedef internal::HeapObject HO;
Steve Blocka7e24c12009-10-30 11:49:00 +00008009 typedef internal::Internals I;
8010 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008011 // Fast path: If the object is a plain JSObject, which is the common case, we
8012 // know where to find the internal fields and can return the value directly.
Steve Block3ce2e202009-11-05 08:53:23 +00008013 if (I::GetInstanceType(obj) == I::kJSObjectType) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008014 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008015 O* value = I::ReadField<O*>(obj, offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008016 O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008017 return Local<Value>(reinterpret_cast<Value*>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00008018 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008019#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008020 return SlowGetInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008021}
8022
8023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008024void* Object::GetAlignedPointerFromInternalField(int index) {
8025#ifndef V8_ENABLE_CHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00008026 typedef internal::Object O;
8027 typedef internal::Internals I;
Steve Block3ce2e202009-11-05 08:53:23 +00008028 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008029 // Fast path: If the object is a plain JSObject, which is the common case, we
8030 // know where to find the internal fields and can return the value directly.
8031 if (V8_LIKELY(I::GetInstanceType(obj) == I::kJSObjectType)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008032 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008033 return I::ReadField<void*>(obj, offset);
Steve Block3ce2e202009-11-05 08:53:23 +00008034 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008035#endif
8036 return SlowGetAlignedPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008037}
8038
8039
8040String* String::Cast(v8::Value* value) {
8041#ifdef V8_ENABLE_CHECKS
8042 CheckCast(value);
8043#endif
8044 return static_cast<String*>(value);
8045}
8046
8047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008048Local<String> String::Empty(Isolate* isolate) {
8049 typedef internal::Object* S;
8050 typedef internal::Internals I;
8051 I::CheckInitialized(isolate);
8052 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
8053 return Local<String>(reinterpret_cast<String*>(slot));
8054}
8055
8056
Steve Blocka7e24c12009-10-30 11:49:00 +00008057String::ExternalStringResource* String::GetExternalStringResource() const {
8058 typedef internal::Object O;
8059 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008060 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008061 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00008062 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008063 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8064 result = reinterpret_cast<String::ExternalStringResource*>(value);
8065 } else {
8066 result = NULL;
8067 }
8068#ifdef V8_ENABLE_CHECKS
8069 VerifyExternalStringResource(result);
8070#endif
8071 return result;
8072}
8073
8074
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008075String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
8076 String::Encoding* encoding_out) const {
8077 typedef internal::Object O;
8078 typedef internal::Internals I;
8079 O* obj = *reinterpret_cast<O* const*>(this);
8080 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
8081 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
8082 ExternalStringResourceBase* resource = NULL;
8083 if (type == I::kExternalOneByteRepresentationTag ||
8084 type == I::kExternalTwoByteRepresentationTag) {
8085 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8086 resource = static_cast<ExternalStringResourceBase*>(value);
8087 }
8088#ifdef V8_ENABLE_CHECKS
8089 VerifyExternalStringResourceBase(resource, *encoding_out);
8090#endif
8091 return resource;
8092}
8093
8094
8095bool Value::IsUndefined() const {
8096#ifdef V8_ENABLE_CHECKS
8097 return FullIsUndefined();
8098#else
8099 return QuickIsUndefined();
8100#endif
8101}
8102
8103bool Value::QuickIsUndefined() const {
8104 typedef internal::Object O;
8105 typedef internal::Internals I;
8106 O* obj = *reinterpret_cast<O* const*>(this);
8107 if (!I::HasHeapObjectTag(obj)) return false;
8108 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8109 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
8110}
8111
8112
8113bool Value::IsNull() const {
8114#ifdef V8_ENABLE_CHECKS
8115 return FullIsNull();
8116#else
8117 return QuickIsNull();
8118#endif
8119}
8120
8121bool Value::QuickIsNull() const {
8122 typedef internal::Object O;
8123 typedef internal::Internals I;
8124 O* obj = *reinterpret_cast<O* const*>(this);
8125 if (!I::HasHeapObjectTag(obj)) return false;
8126 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8127 return (I::GetOddballKind(obj) == I::kNullOddballKind);
8128}
8129
8130
Steve Blocka7e24c12009-10-30 11:49:00 +00008131bool Value::IsString() const {
8132#ifdef V8_ENABLE_CHECKS
8133 return FullIsString();
8134#else
8135 return QuickIsString();
8136#endif
8137}
8138
8139bool Value::QuickIsString() const {
8140 typedef internal::Object O;
8141 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008142 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008143 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00008144 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00008145}
8146
8147
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008148template <class T> Value* Value::Cast(T* value) {
8149 return static_cast<Value*>(value);
8150}
8151
8152
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008153Local<Boolean> Value::ToBoolean() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008154 return ToBoolean(Isolate::GetCurrent()->GetCurrentContext())
8155 .FromMaybe(Local<Boolean>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008156}
8157
8158
8159Local<Number> Value::ToNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008160 return ToNumber(Isolate::GetCurrent()->GetCurrentContext())
8161 .FromMaybe(Local<Number>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008162}
8163
8164
8165Local<String> Value::ToString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008166 return ToString(Isolate::GetCurrent()->GetCurrentContext())
8167 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008168}
8169
8170
8171Local<String> Value::ToDetailString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008172 return ToDetailString(Isolate::GetCurrent()->GetCurrentContext())
8173 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008174}
8175
8176
8177Local<Object> Value::ToObject() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008178 return ToObject(Isolate::GetCurrent()->GetCurrentContext())
8179 .FromMaybe(Local<Object>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008180}
8181
8182
8183Local<Integer> Value::ToInteger() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008184 return ToInteger(Isolate::GetCurrent()->GetCurrentContext())
8185 .FromMaybe(Local<Integer>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008186}
8187
8188
8189Local<Uint32> Value::ToUint32() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008190 return ToUint32(Isolate::GetCurrent()->GetCurrentContext())
8191 .FromMaybe(Local<Uint32>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008192}
8193
8194
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008195Local<Int32> Value::ToInt32() const {
8196 return ToInt32(Isolate::GetCurrent()->GetCurrentContext())
8197 .FromMaybe(Local<Int32>());
8198}
8199
8200
8201Boolean* Boolean::Cast(v8::Value* value) {
8202#ifdef V8_ENABLE_CHECKS
8203 CheckCast(value);
8204#endif
8205 return static_cast<Boolean*>(value);
8206}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008207
8208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008209Name* Name::Cast(v8::Value* value) {
8210#ifdef V8_ENABLE_CHECKS
8211 CheckCast(value);
8212#endif
8213 return static_cast<Name*>(value);
8214}
8215
8216
8217Symbol* Symbol::Cast(v8::Value* value) {
8218#ifdef V8_ENABLE_CHECKS
8219 CheckCast(value);
8220#endif
8221 return static_cast<Symbol*>(value);
8222}
8223
8224
Steve Blocka7e24c12009-10-30 11:49:00 +00008225Number* Number::Cast(v8::Value* value) {
8226#ifdef V8_ENABLE_CHECKS
8227 CheckCast(value);
8228#endif
8229 return static_cast<Number*>(value);
8230}
8231
8232
8233Integer* Integer::Cast(v8::Value* value) {
8234#ifdef V8_ENABLE_CHECKS
8235 CheckCast(value);
8236#endif
8237 return static_cast<Integer*>(value);
8238}
8239
8240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008241Int32* Int32::Cast(v8::Value* value) {
8242#ifdef V8_ENABLE_CHECKS
8243 CheckCast(value);
8244#endif
8245 return static_cast<Int32*>(value);
8246}
8247
8248
8249Uint32* Uint32::Cast(v8::Value* value) {
8250#ifdef V8_ENABLE_CHECKS
8251 CheckCast(value);
8252#endif
8253 return static_cast<Uint32*>(value);
8254}
8255
8256
Steve Blocka7e24c12009-10-30 11:49:00 +00008257Date* Date::Cast(v8::Value* value) {
8258#ifdef V8_ENABLE_CHECKS
8259 CheckCast(value);
8260#endif
8261 return static_cast<Date*>(value);
8262}
8263
8264
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008265StringObject* StringObject::Cast(v8::Value* value) {
8266#ifdef V8_ENABLE_CHECKS
8267 CheckCast(value);
8268#endif
8269 return static_cast<StringObject*>(value);
8270}
8271
8272
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008273SymbolObject* SymbolObject::Cast(v8::Value* value) {
8274#ifdef V8_ENABLE_CHECKS
8275 CheckCast(value);
8276#endif
8277 return static_cast<SymbolObject*>(value);
8278}
8279
8280
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008281NumberObject* NumberObject::Cast(v8::Value* value) {
8282#ifdef V8_ENABLE_CHECKS
8283 CheckCast(value);
8284#endif
8285 return static_cast<NumberObject*>(value);
8286}
8287
8288
8289BooleanObject* BooleanObject::Cast(v8::Value* value) {
8290#ifdef V8_ENABLE_CHECKS
8291 CheckCast(value);
8292#endif
8293 return static_cast<BooleanObject*>(value);
8294}
8295
8296
Ben Murdochf87a2032010-10-22 12:50:53 +01008297RegExp* RegExp::Cast(v8::Value* value) {
8298#ifdef V8_ENABLE_CHECKS
8299 CheckCast(value);
8300#endif
8301 return static_cast<RegExp*>(value);
8302}
8303
8304
Steve Blocka7e24c12009-10-30 11:49:00 +00008305Object* Object::Cast(v8::Value* value) {
8306#ifdef V8_ENABLE_CHECKS
8307 CheckCast(value);
8308#endif
8309 return static_cast<Object*>(value);
8310}
8311
8312
8313Array* Array::Cast(v8::Value* value) {
8314#ifdef V8_ENABLE_CHECKS
8315 CheckCast(value);
8316#endif
8317 return static_cast<Array*>(value);
8318}
8319
8320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008321Map* Map::Cast(v8::Value* value) {
8322#ifdef V8_ENABLE_CHECKS
8323 CheckCast(value);
8324#endif
8325 return static_cast<Map*>(value);
8326}
8327
8328
8329Set* Set::Cast(v8::Value* value) {
8330#ifdef V8_ENABLE_CHECKS
8331 CheckCast(value);
8332#endif
8333 return static_cast<Set*>(value);
8334}
8335
8336
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008337Promise* Promise::Cast(v8::Value* value) {
8338#ifdef V8_ENABLE_CHECKS
8339 CheckCast(value);
8340#endif
8341 return static_cast<Promise*>(value);
8342}
8343
8344
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008345Proxy* Proxy::Cast(v8::Value* value) {
8346#ifdef V8_ENABLE_CHECKS
8347 CheckCast(value);
8348#endif
8349 return static_cast<Proxy*>(value);
8350}
8351
8352
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008353Promise::Resolver* Promise::Resolver::Cast(v8::Value* value) {
8354#ifdef V8_ENABLE_CHECKS
8355 CheckCast(value);
8356#endif
8357 return static_cast<Promise::Resolver*>(value);
8358}
8359
8360
8361ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
8362#ifdef V8_ENABLE_CHECKS
8363 CheckCast(value);
8364#endif
8365 return static_cast<ArrayBuffer*>(value);
8366}
8367
8368
8369ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) {
8370#ifdef V8_ENABLE_CHECKS
8371 CheckCast(value);
8372#endif
8373 return static_cast<ArrayBufferView*>(value);
8374}
8375
8376
8377TypedArray* TypedArray::Cast(v8::Value* value) {
8378#ifdef V8_ENABLE_CHECKS
8379 CheckCast(value);
8380#endif
8381 return static_cast<TypedArray*>(value);
8382}
8383
8384
8385Uint8Array* Uint8Array::Cast(v8::Value* value) {
8386#ifdef V8_ENABLE_CHECKS
8387 CheckCast(value);
8388#endif
8389 return static_cast<Uint8Array*>(value);
8390}
8391
8392
8393Int8Array* Int8Array::Cast(v8::Value* value) {
8394#ifdef V8_ENABLE_CHECKS
8395 CheckCast(value);
8396#endif
8397 return static_cast<Int8Array*>(value);
8398}
8399
8400
8401Uint16Array* Uint16Array::Cast(v8::Value* value) {
8402#ifdef V8_ENABLE_CHECKS
8403 CheckCast(value);
8404#endif
8405 return static_cast<Uint16Array*>(value);
8406}
8407
8408
8409Int16Array* Int16Array::Cast(v8::Value* value) {
8410#ifdef V8_ENABLE_CHECKS
8411 CheckCast(value);
8412#endif
8413 return static_cast<Int16Array*>(value);
8414}
8415
8416
8417Uint32Array* Uint32Array::Cast(v8::Value* value) {
8418#ifdef V8_ENABLE_CHECKS
8419 CheckCast(value);
8420#endif
8421 return static_cast<Uint32Array*>(value);
8422}
8423
8424
8425Int32Array* Int32Array::Cast(v8::Value* value) {
8426#ifdef V8_ENABLE_CHECKS
8427 CheckCast(value);
8428#endif
8429 return static_cast<Int32Array*>(value);
8430}
8431
8432
8433Float32Array* Float32Array::Cast(v8::Value* value) {
8434#ifdef V8_ENABLE_CHECKS
8435 CheckCast(value);
8436#endif
8437 return static_cast<Float32Array*>(value);
8438}
8439
8440
8441Float64Array* Float64Array::Cast(v8::Value* value) {
8442#ifdef V8_ENABLE_CHECKS
8443 CheckCast(value);
8444#endif
8445 return static_cast<Float64Array*>(value);
8446}
8447
8448
8449Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
8450#ifdef V8_ENABLE_CHECKS
8451 CheckCast(value);
8452#endif
8453 return static_cast<Uint8ClampedArray*>(value);
8454}
8455
8456
8457DataView* DataView::Cast(v8::Value* value) {
8458#ifdef V8_ENABLE_CHECKS
8459 CheckCast(value);
8460#endif
8461 return static_cast<DataView*>(value);
8462}
8463
8464
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008465SharedArrayBuffer* SharedArrayBuffer::Cast(v8::Value* value) {
8466#ifdef V8_ENABLE_CHECKS
8467 CheckCast(value);
8468#endif
8469 return static_cast<SharedArrayBuffer*>(value);
8470}
8471
8472
Steve Blocka7e24c12009-10-30 11:49:00 +00008473Function* Function::Cast(v8::Value* value) {
8474#ifdef V8_ENABLE_CHECKS
8475 CheckCast(value);
8476#endif
8477 return static_cast<Function*>(value);
8478}
8479
8480
8481External* External::Cast(v8::Value* value) {
8482#ifdef V8_ENABLE_CHECKS
8483 CheckCast(value);
8484#endif
8485 return static_cast<External*>(value);
8486}
8487
8488
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008489template<typename T>
8490Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
8491 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
Steve Blocka7e24c12009-10-30 11:49:00 +00008492}
8493
8494
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008495template<typename T>
8496Local<Value> PropertyCallbackInfo<T>::Data() const {
8497 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008498}
8499
8500
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008501template<typename T>
8502Local<Object> PropertyCallbackInfo<T>::This() const {
8503 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
8504}
8505
8506
8507template<typename T>
8508Local<Object> PropertyCallbackInfo<T>::Holder() const {
8509 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
8510}
8511
8512
8513template<typename T>
8514ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
8515 return ReturnValue<T>(&args_[kReturnValueIndex]);
8516}
8517
Ben Murdoch097c5b22016-05-18 11:27:45 +01008518template <typename T>
8519bool PropertyCallbackInfo<T>::ShouldThrowOnError() const {
8520 typedef internal::Internals I;
8521 return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0);
8522}
8523
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008525Local<Primitive> Undefined(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008526 typedef internal::Object* S;
8527 typedef internal::Internals I;
8528 I::CheckInitialized(isolate);
8529 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008530 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008531}
8532
8533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008534Local<Primitive> Null(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008535 typedef internal::Object* S;
8536 typedef internal::Internals I;
8537 I::CheckInitialized(isolate);
8538 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008539 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008540}
8541
8542
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008543Local<Boolean> True(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008544 typedef internal::Object* S;
8545 typedef internal::Internals I;
8546 I::CheckInitialized(isolate);
8547 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008548 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008549}
8550
8551
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008552Local<Boolean> False(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008553 typedef internal::Object* S;
8554 typedef internal::Internals I;
8555 I::CheckInitialized(isolate);
8556 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008557 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008558}
8559
8560
8561void Isolate::SetData(uint32_t slot, void* data) {
8562 typedef internal::Internals I;
8563 I::SetEmbedderData(this, slot, data);
8564}
8565
8566
8567void* Isolate::GetData(uint32_t slot) {
8568 typedef internal::Internals I;
8569 return I::GetEmbedderData(this, slot);
8570}
8571
8572
8573uint32_t Isolate::GetNumberOfDataSlots() {
8574 typedef internal::Internals I;
8575 return I::kNumIsolateDataSlots;
8576}
8577
8578
8579int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
8580 int64_t change_in_bytes) {
8581 typedef internal::Internals I;
8582 int64_t* amount_of_external_allocated_memory =
8583 reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
8584 I::kAmountOfExternalAllocatedMemoryOffset);
8585 int64_t* amount_of_external_allocated_memory_at_last_global_gc =
8586 reinterpret_cast<int64_t*>(
8587 reinterpret_cast<uint8_t*>(this) +
8588 I::kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset);
8589 int64_t amount = *amount_of_external_allocated_memory + change_in_bytes;
8590 if (change_in_bytes > 0 &&
8591 amount - *amount_of_external_allocated_memory_at_last_global_gc >
8592 I::kExternalAllocationLimit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008593 ReportExternalAllocationLimitReached();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008594 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008595 *amount_of_external_allocated_memory = amount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008596 return *amount_of_external_allocated_memory;
8597}
8598
8599
8600template<typename T>
8601void Isolate::SetObjectGroupId(const Persistent<T>& object,
8602 UniqueId id) {
8603 TYPE_CHECK(Value, T);
8604 SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id);
8605}
8606
8607
8608template<typename T>
8609void Isolate::SetReferenceFromGroup(UniqueId id,
8610 const Persistent<T>& object) {
8611 TYPE_CHECK(Value, T);
8612 SetReferenceFromGroup(id,
8613 reinterpret_cast<v8::internal::Object**>(object.val_));
8614}
8615
8616
8617template<typename T, typename S>
8618void Isolate::SetReference(const Persistent<T>& parent,
8619 const Persistent<S>& child) {
8620 TYPE_CHECK(Object, T);
8621 TYPE_CHECK(Value, S);
8622 SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_),
8623 reinterpret_cast<v8::internal::Object**>(child.val_));
8624}
8625
8626
8627Local<Value> Context::GetEmbedderData(int index) {
8628#ifndef V8_ENABLE_CHECKS
8629 typedef internal::Object O;
8630 typedef internal::HeapObject HO;
8631 typedef internal::Internals I;
8632 HO* context = *reinterpret_cast<HO**>(this);
8633 O** result =
8634 HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
8635 return Local<Value>(reinterpret_cast<Value*>(result));
8636#else
8637 return SlowGetEmbedderData(index);
8638#endif
8639}
8640
8641
8642void* Context::GetAlignedPointerFromEmbedderData(int index) {
8643#ifndef V8_ENABLE_CHECKS
8644 typedef internal::Internals I;
8645 return I::ReadEmbedderData<void*>(this, index);
8646#else
8647 return SlowGetAlignedPointerFromEmbedderData(index);
8648#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008649}
8650
8651
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008652void V8::SetAllowCodeGenerationFromStringsCallback(
8653 AllowCodeGenerationFromStringsCallback callback) {
8654 Isolate* isolate = Isolate::GetCurrent();
8655 isolate->SetAllowCodeGenerationFromStringsCallback(callback);
8656}
8657
8658
8659bool V8::IsDead() {
8660 Isolate* isolate = Isolate::GetCurrent();
8661 return isolate->IsDead();
8662}
8663
8664
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008665bool V8::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008666 Isolate* isolate = Isolate::GetCurrent();
8667 return isolate->AddMessageListener(that, data);
8668}
8669
8670
8671void V8::RemoveMessageListeners(MessageCallback that) {
8672 Isolate* isolate = Isolate::GetCurrent();
8673 isolate->RemoveMessageListeners(that);
8674}
8675
8676
8677void V8::SetFailedAccessCheckCallbackFunction(
8678 FailedAccessCheckCallback callback) {
8679 Isolate* isolate = Isolate::GetCurrent();
8680 isolate->SetFailedAccessCheckCallbackFunction(callback);
8681}
8682
8683
8684void V8::SetCaptureStackTraceForUncaughtExceptions(
8685 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8686 Isolate* isolate = Isolate::GetCurrent();
8687 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8688 options);
8689}
8690
8691
8692void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
8693 Isolate* isolate = Isolate::GetCurrent();
8694 isolate->SetFatalErrorHandler(callback);
8695}
8696
8697
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008698void V8::RemoveGCPrologueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008699 Isolate* isolate = Isolate::GetCurrent();
8700 isolate->RemoveGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008701 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008702}
8703
8704
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008705void V8::RemoveGCEpilogueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008706 Isolate* isolate = Isolate::GetCurrent();
8707 isolate->RemoveGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008708 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008709}
8710
8711
8712void V8::AddMemoryAllocationCallback(MemoryAllocationCallback callback,
8713 ObjectSpace space,
8714 AllocationAction action) {
8715 Isolate* isolate = Isolate::GetCurrent();
8716 isolate->AddMemoryAllocationCallback(callback, space, action);
8717}
8718
8719
8720void V8::RemoveMemoryAllocationCallback(MemoryAllocationCallback callback) {
8721 Isolate* isolate = Isolate::GetCurrent();
8722 isolate->RemoveMemoryAllocationCallback(callback);
8723}
8724
8725
8726void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); }
8727
8728
8729bool V8::IsExecutionTerminating(Isolate* isolate) {
8730 if (isolate == NULL) {
8731 isolate = Isolate::GetCurrent();
8732 }
8733 return isolate->IsExecutionTerminating();
8734}
8735
8736
8737void V8::CancelTerminateExecution(Isolate* isolate) {
8738 isolate->CancelTerminateExecution();
8739}
8740
8741
8742void V8::VisitExternalResources(ExternalResourceVisitor* visitor) {
8743 Isolate* isolate = Isolate::GetCurrent();
8744 isolate->VisitExternalResources(visitor);
8745}
8746
8747
8748void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8749 Isolate* isolate = Isolate::GetCurrent();
8750 isolate->VisitHandlesWithClassIds(visitor);
8751}
8752
8753
8754void V8::VisitHandlesWithClassIds(Isolate* isolate,
8755 PersistentHandleVisitor* visitor) {
8756 isolate->VisitHandlesWithClassIds(visitor);
8757}
8758
8759
8760void V8::VisitHandlesForPartialDependence(Isolate* isolate,
8761 PersistentHandleVisitor* visitor) {
8762 isolate->VisitHandlesForPartialDependence(visitor);
8763}
8764
Steve Blocka7e24c12009-10-30 11:49:00 +00008765/**
8766 * \example shell.cc
8767 * A simple shell that takes a list of expressions on the
8768 * command-line and executes them.
8769 */
8770
8771
8772/**
8773 * \example process.cc
8774 */
8775
8776
8777} // namespace v8
8778
8779
Steve Blocka7e24c12009-10-30 11:49:00 +00008780#undef TYPE_CHECK
8781
8782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008783#endif // INCLUDE_V8_H_