blob: c62a1fd0cab997dad126f9457522d8754747c63d [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5/** \mainpage V8 API Reference Guide
6 *
7 * V8 is Google's open source JavaScript engine.
8 *
9 * This set of documents provides reference material generated from the
10 * V8 header file, include/v8.h.
11 *
12 * For other documentation see http://code.google.com/apis/v8/
13 */
14
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#ifndef INCLUDE_V8_H_
16#define INCLUDE_V8_H_
Steve Blocka7e24c12009-10-30 11:49:00 +000017
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include <stddef.h>
19#include <stdint.h>
20#include <stdio.h>
Ben Murdochda12d292016-06-02 14:46:10 +010021#include <utility>
22#include <vector>
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024#include "v8-version.h" // NOLINT(build/include)
25#include "v8config.h" // NOLINT(build/include)
Steve Blocka7e24c12009-10-30 11:49:00 +000026
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027// We reserve the V8_* prefix for macros defined in V8 public API and
28// assume there are no name conflicts with the embedder's code.
29
30#ifdef V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000031
32// Setup for Windows DLL export/import. When building the V8 DLL the
33// BUILDING_V8_SHARED needs to be defined. When building a program which uses
34// the V8 DLL USING_V8_SHARED needs to be defined. When either building the V8
35// static library or building a program which uses the V8 static library neither
36// BUILDING_V8_SHARED nor USING_V8_SHARED should be defined.
Steve Blocka7e24c12009-10-30 11:49:00 +000037#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED)
38#error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\
39 build configuration to ensure that at most one of these is set
40#endif
41
42#ifdef BUILDING_V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043# define V8_EXPORT __declspec(dllexport)
Steve Blocka7e24c12009-10-30 11:49:00 +000044#elif USING_V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045# define V8_EXPORT __declspec(dllimport)
Steve Blocka7e24c12009-10-30 11:49:00 +000046#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047# define V8_EXPORT
Steve Blocka7e24c12009-10-30 11:49:00 +000048#endif // BUILDING_V8_SHARED
49
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050#else // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000051
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052// Setup for Linux shared library export.
53#if V8_HAS_ATTRIBUTE_VISIBILITY && defined(V8_SHARED)
54# ifdef BUILDING_V8_SHARED
55# define V8_EXPORT __attribute__ ((visibility("default")))
56# else
57# define V8_EXPORT
58# endif
59#else
60# define V8_EXPORT
61#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000062
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063#endif // V8_OS_WIN
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65/**
66 * The v8 JavaScript engine.
67 */
68namespace v8 {
69
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070class AccessorSignature;
71class Array;
72class Boolean;
73class BooleanObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000074class Context;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075class CpuProfiler;
76class Data;
77class Date;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078class External;
79class Function;
80class FunctionTemplate;
81class HeapProfiler;
82class ImplementationUtilities;
83class Int32;
84class Integer;
85class Isolate;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086template <class T>
87class Maybe;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088class Name;
Steve Blocka7e24c12009-10-30 11:49:00 +000089class Number;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090class NumberObject;
Steve Blocka7e24c12009-10-30 11:49:00 +000091class Object;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092class ObjectOperationDescriptor;
93class ObjectTemplate;
94class Platform;
Steve Blocka7e24c12009-10-30 11:49:00 +000095class Primitive;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040096class Promise;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097class Proxy;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098class RawOperationDescriptor;
99class Script;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100class SharedArrayBuffer;
Steve Blocka7e24c12009-10-30 11:49:00 +0000101class Signature;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102class StartupData;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103class StackFrame;
104class StackTrace;
105class String;
106class StringObject;
107class Symbol;
108class SymbolObject;
109class Private;
110class Uint32;
111class Utils;
112class Value;
Steve Blocka7e24c12009-10-30 11:49:00 +0000113template <class T> class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114template <class T>
115class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116template <class T> class Eternal;
117template<class T> class NonCopyablePersistentTraits;
118template<class T> class PersistentBase;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119template <class T, class M = NonCopyablePersistentTraits<T> >
120class Persistent;
121template <class T>
122class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123template<class K, class V, class T> class PersistentValueMap;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124template <class K, class V, class T>
125class PersistentValueMapBase;
126template <class K, class V, class T>
127class GlobalValueMap;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128template<class V, class T> class PersistentValueVector;
129template<class T, class P> class WeakCallbackObject;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130class FunctionTemplate;
131class ObjectTemplate;
132class Data;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133template<typename T> class FunctionCallbackInfo;
134template<typename T> class PropertyCallbackInfo;
Kristian Monsen25f61362010-05-21 11:50:48 +0100135class StackTrace;
136class StackFrame;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138class CallHandlerHelper;
139class EscapableHandleScope;
140template<typename T> class ReturnValue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142namespace experimental {
143class FastAccessorBuilder;
144} // namespace experimental
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146namespace internal {
Steve Blocka7e24c12009-10-30 11:49:00 +0000147class Arguments;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100148class Heap;
Steve Block44f0eee2011-05-26 01:26:41 +0100149class HeapObject;
150class Isolate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151class Object;
152struct StreamedSource;
153template<typename T> class CustomArguments;
154class PropertyCallbackArguments;
155class FunctionCallbackArguments;
156class GlobalHandles;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +0000158
159
Steve Blocka7e24c12009-10-30 11:49:00 +0000160/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 * General purpose unique identifier.
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163class UniqueId {
164 public:
165 explicit UniqueId(intptr_t data)
166 : data_(data) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 bool operator==(const UniqueId& other) const {
169 return data_ == other.data_;
170 }
171
172 bool operator!=(const UniqueId& other) const {
173 return data_ != other.data_;
174 }
175
176 bool operator<(const UniqueId& other) const {
177 return data_ < other.data_;
178 }
179
180 private:
181 intptr_t data_;
182};
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
Ben Murdoch257744e2011-11-30 15:57:28 +0000184// --- Handles ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100186#define TYPE_CHECK(T, S) \
187 while (false) { \
188 *(static_cast<T* volatile*>(0)) = static_cast<S*>(0); \
Steve Blocka7e24c12009-10-30 11:49:00 +0000189 }
190
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191
Steve Blocka7e24c12009-10-30 11:49:00 +0000192/**
193 * An object reference managed by the v8 garbage collector.
194 *
195 * All objects returned from v8 have to be tracked by the garbage
196 * collector so that it knows that the objects are still alive. Also,
197 * because the garbage collector may move objects, it is unsafe to
198 * point directly to an object. Instead, all objects are stored in
199 * handles which are known by the garbage collector and updated
200 * whenever an object moves. Handles should always be passed by value
201 * (except in cases like out-parameters) and they should never be
202 * allocated on the heap.
203 *
204 * There are two types of handles: local and persistent handles.
205 * Local handles are light-weight and transient and typically used in
206 * local operations. They are managed by HandleScopes. Persistent
207 * handles can be used when storing objects across several independent
208 * operations and have to be explicitly deallocated when they're no
209 * longer used.
210 *
211 * It is safe to extract the object stored in the handle by
212 * dereferencing the handle (for instance, to extract the Object* from
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000213 * a Local<Object>); the value will still be governed by a handle
Steve Blocka7e24c12009-10-30 11:49:00 +0000214 * behind the scenes and the same rules apply to these values as to
215 * their handles.
216 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217template <class T>
218class Local {
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 V8_INLINE Local() : val_(0) {}
221 template <class S>
222 V8_INLINE Local(Local<S> that)
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 : val_(reinterpret_cast<T*>(*that)) {
224 /**
225 * This check fails when trying to convert between incompatible
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226 * handles. For example, converting from a Local<String> to a
227 * Local<Number>.
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 */
229 TYPE_CHECK(T, S);
230 }
231
232 /**
233 * Returns true if the handle is empty.
234 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235 V8_INLINE bool IsEmpty() const { return val_ == 0; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000236
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 /**
238 * Sets the handle to be empty. IsEmpty() will then return true.
239 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 V8_INLINE void Clear() { val_ = 0; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000241
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 V8_INLINE T* operator->() const { return val_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000243
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 V8_INLINE T* operator*() const { return val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000245
246 /**
247 * Checks whether two handles are the same.
248 * Returns true if both are empty, or if the objects
249 * to which they refer are identical.
250 * The handles' references are not checked.
251 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 template <class S>
253 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
255 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
256 if (a == 0) return b == 0;
257 if (b == 0) return false;
258 return *a == *b;
259 }
260
261 template <class S> V8_INLINE bool operator==(
262 const PersistentBase<S>& that) const {
263 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
264 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 if (a == 0) return b == 0;
266 if (b == 0) return false;
267 return *a == *b;
268 }
269
270 /**
271 * Checks whether two handles are different.
272 * Returns true if only one of the handles is empty, or if
273 * the objects to which they refer are different.
274 * The handles' references are not checked.
275 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 template <class S>
277 V8_INLINE bool operator!=(const Local<S>& that) const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 return !operator==(that);
279 }
280
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 template <class S> V8_INLINE bool operator!=(
282 const Persistent<S>& that) const {
283 return !operator==(that);
284 }
285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 template <class S> V8_INLINE static Local<T> Cast(Local<S> that) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000287#ifdef V8_ENABLE_CHECKS
288 // If we're going to perform the type check then we have to check
289 // that the handle isn't empty before doing the checked cast.
290 if (that.IsEmpty()) return Local<T>();
291#endif
292 return Local<T>(T::Cast(*that));
293 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294
Steve Blocka7e24c12009-10-30 11:49:00 +0000295
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 template <class S> V8_INLINE Local<S> As() {
Steve Block6ded16b2010-05-10 14:33:55 +0100297 return Local<S>::Cast(*this);
298 }
299
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 /**
301 * Create a local handle for the content of another handle.
302 * The referee is kept alive by the local handle even when
303 * the original handle is destroyed/disposed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000305 V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 V8_INLINE static Local<T> New(Isolate* isolate,
307 const PersistentBase<T>& that);
308
309 private:
310 friend class Utils;
311 template<class F> friend class Eternal;
312 template<class F> friend class PersistentBase;
313 template<class F, class M> friend class Persistent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 template<class F> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 template <class F>
316 friend class MaybeLocal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 template<class F> friend class FunctionCallbackInfo;
318 template<class F> friend class PropertyCallbackInfo;
319 friend class String;
320 friend class Object;
321 friend class Context;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 friend class Private;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 template<class F> friend class internal::CustomArguments;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 friend Local<Primitive> Undefined(Isolate* isolate);
325 friend Local<Primitive> Null(Isolate* isolate);
326 friend Local<Boolean> True(Isolate* isolate);
327 friend Local<Boolean> False(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 friend class HandleScope;
329 friend class EscapableHandleScope;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 template <class F1, class F2, class F3>
331 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 template<class F1, class F2> friend class PersistentValueVector;
Ben Murdochda12d292016-06-02 14:46:10 +0100333 template <class F>
334 friend class ReturnValue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335
Ben Murdoch097c5b22016-05-18 11:27:45 +0100336 explicit V8_INLINE Local(T* that) : val_(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 V8_INLINE static Local<T> New(Isolate* isolate, T* that);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 T* val_;
339};
340
341
342#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
343// Local is an alias for Local for historical reasons.
344template <class T>
345using Handle = Local<T>;
346#endif
347
348
349/**
350 * A MaybeLocal<> is a wrapper around Local<> that enforces a check whether
351 * the Local<> is empty before it can be used.
352 *
353 * If an API method returns a MaybeLocal<>, the API method can potentially fail
354 * either because an exception is thrown, or because an exception is pending,
355 * e.g. because a previous API call threw an exception that hasn't been caught
356 * yet, or because a TerminateExecution exception was thrown. In that case, an
357 * empty MaybeLocal is returned.
358 */
359template <class T>
360class MaybeLocal {
361 public:
362 V8_INLINE MaybeLocal() : val_(nullptr) {}
363 template <class S>
364 V8_INLINE MaybeLocal(Local<S> that)
365 : val_(reinterpret_cast<T*>(*that)) {
366 TYPE_CHECK(T, S);
367 }
368
369 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
370
371 template <class S>
372 V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local<S>* out) const {
373 out->val_ = IsEmpty() ? nullptr : this->val_;
374 return !IsEmpty();
375 }
376
377 // Will crash if the MaybeLocal<> is empty.
378 V8_INLINE Local<T> ToLocalChecked();
379
380 template <class S>
381 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
382 return IsEmpty() ? default_value : Local<S>(val_);
383 }
384
385 private:
386 T* val_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387};
388
389
390// Eternal handles are set-once handles that live for the life of the isolate.
391template <class T> class Eternal {
392 public:
393 V8_INLINE Eternal() : index_(kInitialValue) { }
394 template<class S>
395 V8_INLINE Eternal(Isolate* isolate, Local<S> handle) : index_(kInitialValue) {
396 Set(isolate, handle);
397 }
398 // Can only be safely called if already set.
399 V8_INLINE Local<T> Get(Isolate* isolate);
400 V8_INLINE bool IsEmpty() { return index_ == kInitialValue; }
401 template<class S> V8_INLINE void Set(Isolate* isolate, Local<S> handle);
402
403 private:
404 static const int kInitialValue = -1;
405 int index_;
406};
407
408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409static const int kInternalFieldsInWeakCallback = 2;
410
411
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400412template <typename T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413class WeakCallbackInfo {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400414 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415 typedef void (*Callback)(const WeakCallbackInfo<T>& data);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 WeakCallbackInfo(Isolate* isolate, T* parameter,
418 void* internal_fields[kInternalFieldsInWeakCallback],
419 Callback* callback)
420 : isolate_(isolate), parameter_(parameter), callback_(callback) {
421 for (int i = 0; i < kInternalFieldsInWeakCallback; ++i) {
422 internal_fields_[i] = internal_fields[i];
423 }
424 }
425
426 V8_INLINE Isolate* GetIsolate() const { return isolate_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400427 V8_INLINE T* GetParameter() const { return parameter_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000428 V8_INLINE void* GetInternalField(int index) const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 V8_INLINE V8_DEPRECATED("use indexed version",
431 void* GetInternalField1() const) {
432 return internal_fields_[0];
433 }
434 V8_INLINE V8_DEPRECATED("use indexed version",
435 void* GetInternalField2() const) {
436 return internal_fields_[1];
437 }
438
Ben Murdoch097c5b22016-05-18 11:27:45 +0100439 V8_DEPRECATED("Not realiable once SetSecondPassCallback() was used.",
440 bool IsFirstPass() const) {
441 return callback_ != nullptr;
442 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443
444 // When first called, the embedder MUST Reset() the Global which triggered the
445 // callback. The Global itself is unusable for anything else. No v8 other api
446 // calls may be called in the first callback. Should additional work be
447 // required, the embedder must set a second pass callback, which will be
448 // called after all the initial callbacks are processed.
449 // Calling SetSecondPassCallback on the second pass will immediately crash.
450 void SetSecondPassCallback(Callback callback) const { *callback_ = callback; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400451
452 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 Isolate* isolate_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400454 T* parameter_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455 Callback* callback_;
456 void* internal_fields_[kInternalFieldsInWeakCallback];
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400457};
458
459
Ben Murdochc5610432016-08-08 18:44:38 +0100460// kParameter will pass a void* parameter back to the callback, kInternalFields
461// will pass the first two internal fields back to the callback, kFinalizer
462// will pass a void* parameter back, but is invoked before the object is
463// actually collected, so it can be resurrected. In the last case, it is not
464// possible to request a second pass callback.
465enum class WeakCallbackType { kParameter, kInternalFields, kFinalizer };
Steve Blocka7e24c12009-10-30 11:49:00 +0000466
467/**
468 * An object reference that is independent of any handle scope. Where
469 * a Local handle only lives as long as the HandleScope in which it was
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 * allocated, a PersistentBase handle remains valid until it is explicitly
Steve Blocka7e24c12009-10-30 11:49:00 +0000471 * disposed.
472 *
473 * A persistent handle contains a reference to a storage cell within
474 * the v8 engine which holds an object value and which is updated by
475 * the garbage collector whenever the object is moved. A new storage
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 * cell can be created using the constructor or PersistentBase::Reset and
477 * existing handles can be disposed using PersistentBase::Reset.
478 *
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480template <class T> class PersistentBase {
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 * If non-empty, destroy the underlying storage cell
484 * IsEmpty() will return true after this call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000485 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 V8_INLINE void Reset();
487 /**
488 * If non-empty, destroy the underlying storage cell
489 * and create a new one with the contents of other if other is non empty
490 */
491 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 V8_INLINE void Reset(Isolate* isolate, const Local<S>& other);
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
494 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495 * If non-empty, destroy the underlying storage cell
496 * and create a new one with the contents of other if other is non empty
Steve Blocka7e24c12009-10-30 11:49:00 +0000497 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000498 template <class S>
499 V8_INLINE void Reset(Isolate* isolate, const PersistentBase<S>& other);
500
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400501 V8_INLINE bool IsEmpty() const { return val_ == NULL; }
502 V8_INLINE void Empty() { val_ = 0; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000504 V8_INLINE Local<T> Get(Isolate* isolate) const {
505 return Local<T>::New(isolate, *this);
506 }
507
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 template <class S>
509 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
510 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
511 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400512 if (a == NULL) return b == NULL;
513 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 return *a == *b;
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 }
516
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000517 template <class S>
518 V8_INLINE bool operator==(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 internal::Object** a = reinterpret_cast<internal::Object**>(this->val_);
520 internal::Object** b = reinterpret_cast<internal::Object**>(that.val_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400521 if (a == NULL) return b == NULL;
522 if (b == NULL) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 return *a == *b;
524 }
525
526 template <class S>
527 V8_INLINE bool operator!=(const PersistentBase<S>& that) const {
528 return !operator==(that);
529 }
530
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000531 template <class S>
532 V8_INLINE bool operator!=(const Local<S>& that) const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 return !operator==(that);
534 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000535
536 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000537 * Install a finalization callback on this object.
538 * NOTE: There is no guarantee as to *when* or even *if* the callback is
539 * invoked. The invocation is performed solely on a best effort basis.
540 * As always, GC-based finalization should *not* be relied upon for any
541 * critical form of resource management!
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 template <typename P>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000544 V8_INLINE void SetWeak(P* parameter,
545 typename WeakCallbackInfo<P>::Callback callback,
546 WeakCallbackType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400547
Ben Murdochc5610432016-08-08 18:44:38 +0100548 /**
549 * Turns this handle into a weak phantom handle without finalization callback.
550 * The handle will be reset automatically when the garbage collector detects
551 * that the object is no longer reachable.
552 * A related function Isolate::NumberOfPhantomHandleResetsSinceLastCall
553 * returns how many phantom handles were reset by the garbage collector.
554 */
555 V8_INLINE void SetWeak();
556
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 template<typename P>
558 V8_INLINE P* ClearWeak();
559
560 // TODO(dcarney): remove this.
561 V8_INLINE void ClearWeak() { ClearWeak<void>(); }
562
563 /**
Ben Murdochda12d292016-06-02 14:46:10 +0100564 * Allows the embedder to tell the v8 garbage collector that a certain object
565 * is alive. Only allowed when the embedder is asked to trace its heap by
566 * EmbedderHeapTracer.
567 */
Ben Murdochc5610432016-08-08 18:44:38 +0100568 V8_INLINE void RegisterExternalReference(Isolate* isolate) const;
Ben Murdochda12d292016-06-02 14:46:10 +0100569
570 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 * Marks the reference to this object independent. Garbage collector is free
572 * to ignore any object groups containing this object. Weak callback for an
573 * independent handle should not assume that it will be preceded by a global
574 * GC prologue callback or followed by a global GC epilogue callback.
575 */
576 V8_INLINE void MarkIndependent();
577
578 /**
579 * Marks the reference to this object partially dependent. Partially dependent
580 * handles only depend on other partially dependent handles and these
581 * dependencies are provided through object groups. It provides a way to build
582 * smaller object groups for young objects that represent only a subset of all
583 * external dependencies. This mark is automatically cleared after each
584 * garbage collection.
585 */
Ben Murdochc5610432016-08-08 18:44:38 +0100586 V8_INLINE V8_DEPRECATED(
587 "deprecated optimization, do not use partially dependent groups",
588 void MarkPartiallyDependent());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000590 /**
591 * Marks the reference to this object as active. The scavenge garbage
592 * collection should not reclaim the objects marked as active.
593 * This bit is cleared after the each garbage collection pass.
594 */
595 V8_INLINE void MarkActive();
596
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 V8_INLINE bool IsIndependent() const;
598
599 /** Checks if the handle holds the only reference to an object. */
600 V8_INLINE bool IsNearDeath() const;
601
602 /** Returns true if the handle's reference is weak. */
603 V8_INLINE bool IsWeak() const;
604
605 /**
606 * Assigns a wrapper class ID to the handle. See RetainedObjectInfo interface
607 * description in v8-profiler.h for details.
608 */
609 V8_INLINE void SetWrapperClassId(uint16_t class_id);
610
611 /**
612 * Returns the class ID previously assigned to this handle or 0 if no class ID
613 * was previously assigned.
614 */
615 V8_INLINE uint16_t WrapperClassId() const;
616
617 private:
618 friend class Isolate;
619 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620 template<class F> friend class Local;
621 template<class F1, class F2> friend class Persistent;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000622 template <class F>
623 friend class Global;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 template<class F> friend class PersistentBase;
625 template<class F> friend class ReturnValue;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000626 template <class F1, class F2, class F3>
627 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 template<class F1, class F2> friend class PersistentValueVector;
629 friend class Object;
630
631 explicit V8_INLINE PersistentBase(T* val) : val_(val) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000632 PersistentBase(const PersistentBase& other) = delete; // NOLINT
633 void operator=(const PersistentBase&) = delete;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 V8_INLINE static T* New(Isolate* isolate, T* that);
635
636 T* val_;
637};
638
639
640/**
641 * Default traits for Persistent. This class does not allow
642 * use of the copy constructor or assignment operator.
643 * At present kResetInDestructor is not set, but that will change in a future
644 * version.
645 */
646template<class T>
647class NonCopyablePersistentTraits {
648 public:
649 typedef Persistent<T, NonCopyablePersistentTraits<T> > NonCopyablePersistent;
650 static const bool kResetInDestructor = false;
651 template<class S, class M>
652 V8_INLINE static void Copy(const Persistent<S, M>& source,
653 NonCopyablePersistent* dest) {
654 Uncompilable<Object>();
655 }
656 // TODO(dcarney): come up with a good compile error here.
657 template<class O> V8_INLINE static void Uncompilable() {
658 TYPE_CHECK(O, Primitive);
659 }
660};
661
662
663/**
664 * Helper class traits to allow copying and assignment of Persistent.
665 * This will clone the contents of storage cell, but not any of the flags, etc.
666 */
667template<class T>
668struct CopyablePersistentTraits {
669 typedef Persistent<T, CopyablePersistentTraits<T> > CopyablePersistent;
670 static const bool kResetInDestructor = true;
671 template<class S, class M>
672 static V8_INLINE void Copy(const Persistent<S, M>& source,
673 CopyablePersistent* dest) {
674 // do nothing, just allow copy
675 }
676};
677
678
679/**
680 * A PersistentBase which allows copy and assignment.
681 *
682 * Copy, assignment and destructor bevavior is controlled by the traits
683 * class M.
684 *
685 * Note: Persistent class hierarchy is subject to future changes.
686 */
687template <class T, class M> class Persistent : public PersistentBase<T> {
688 public:
689 /**
690 * A Persistent with no storage cell.
691 */
692 V8_INLINE Persistent() : PersistentBase<T>(0) { }
693 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 * Construct a Persistent from a Local.
695 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 * pointing to the same object, and no flags are set.
697 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698 template <class S>
699 V8_INLINE Persistent(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
701 TYPE_CHECK(T, S);
702 }
703 /**
704 * Construct a Persistent from a Persistent.
705 * When the Persistent is non-empty, a new storage cell is created
706 * pointing to the same object, and no flags are set.
707 */
708 template <class S, class M2>
709 V8_INLINE Persistent(Isolate* isolate, const Persistent<S, M2>& that)
710 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
711 TYPE_CHECK(T, S);
712 }
713 /**
714 * The copy constructors and assignment operator create a Persistent
715 * exactly as the Persistent constructor, but the Copy function from the
716 * traits class is called, allowing the setting of flags based on the
717 * copied Persistent.
718 */
719 V8_INLINE Persistent(const Persistent& that) : PersistentBase<T>(0) {
720 Copy(that);
721 }
722 template <class S, class M2>
723 V8_INLINE Persistent(const Persistent<S, M2>& that) : PersistentBase<T>(0) {
724 Copy(that);
725 }
726 V8_INLINE Persistent& operator=(const Persistent& that) { // NOLINT
727 Copy(that);
728 return *this;
729 }
730 template <class S, class M2>
731 V8_INLINE Persistent& operator=(const Persistent<S, M2>& that) { // NOLINT
732 Copy(that);
733 return *this;
734 }
735 /**
736 * The destructor will dispose the Persistent based on the
737 * kResetInDestructor flags in the traits class. Since not calling dispose
738 * can result in a memory leak, it is recommended to always set this flag.
739 */
740 V8_INLINE ~Persistent() {
741 if (M::kResetInDestructor) this->Reset();
742 }
743
744 // TODO(dcarney): this is pretty useless, fix or remove
745 template <class S>
746 V8_INLINE static Persistent<T>& Cast(Persistent<S>& that) { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000747#ifdef V8_ENABLE_CHECKS
748 // If we're going to perform the type check then we have to check
749 // that the handle isn't empty before doing the checked cast.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750 if (!that.IsEmpty()) T::Cast(*that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000751#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000752 return reinterpret_cast<Persistent<T>&>(that);
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 }
754
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 // TODO(dcarney): this is pretty useless, fix or remove
756 template <class S> V8_INLINE Persistent<S>& As() { // NOLINT
Steve Block6ded16b2010-05-10 14:33:55 +0100757 return Persistent<S>::Cast(*this);
758 }
759
Steve Blocka7e24c12009-10-30 11:49:00 +0000760 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 friend class Isolate;
762 friend class Utils;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763 template<class F> friend class Local;
764 template<class F1, class F2> friend class Persistent;
765 template<class F> friend class ReturnValue;
766
Ben Murdoch097c5b22016-05-18 11:27:45 +0100767 explicit V8_INLINE Persistent(T* that) : PersistentBase<T>(that) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 V8_INLINE T* operator*() const { return this->val_; }
769 template<class S, class M2>
770 V8_INLINE void Copy(const Persistent<S, M2>& that);
771};
772
773
774/**
775 * A PersistentBase which has move semantics.
776 *
777 * Note: Persistent class hierarchy is subject to future changes.
778 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000779template <class T>
780class Global : public PersistentBase<T> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781 public:
782 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000783 * A Global with no storage cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000785 V8_INLINE Global() : PersistentBase<T>(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000787 * Construct a Global from a Local.
788 * When the Local is non-empty, a new storage cell is created
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 * pointing to the same object, and no flags are set.
790 */
791 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000792 V8_INLINE Global(Isolate* isolate, Local<S> that)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 : PersistentBase<T>(PersistentBase<T>::New(isolate, *that)) {
794 TYPE_CHECK(T, S);
795 }
796 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000797 * Construct a Global from a PersistentBase.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 * When the Persistent is non-empty, a new storage cell is created
799 * pointing to the same object, and no flags are set.
800 */
801 template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 V8_INLINE Global(Isolate* isolate, const PersistentBase<S>& that)
803 : PersistentBase<T>(PersistentBase<T>::New(isolate, that.val_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 TYPE_CHECK(T, S);
805 }
806 /**
807 * Move constructor.
808 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000809 V8_INLINE Global(Global&& other) : PersistentBase<T>(other.val_) { // NOLINT
810 other.val_ = nullptr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000811 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000812 V8_INLINE ~Global() { this->Reset(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813 /**
814 * Move via assignment.
815 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 template <class S>
817 V8_INLINE Global& operator=(Global<S>&& rhs) { // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 TYPE_CHECK(T, S);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000819 if (this != &rhs) {
820 this->Reset();
821 this->val_ = rhs.val_;
822 rhs.val_ = nullptr;
823 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000824 return *this;
825 }
826 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 * Pass allows returning uniques from functions, etc.
828 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829 Global Pass() { return static_cast<Global&&>(*this); } // NOLINT
830
831 /*
832 * For compatibility with Chromium's base::Bind (base::Passed).
833 */
834 typedef void MoveOnlyTypeForCPP03;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000835
836 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000837 template <class F>
838 friend class ReturnValue;
839 Global(const Global&) = delete;
840 void operator=(const Global&) = delete;
841 V8_INLINE T* operator*() const { return this->val_; }
Steve Blocka7e24c12009-10-30 11:49:00 +0000842};
843
844
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000845// UniquePersistent is an alias for Global for historical reason.
846template <class T>
847using UniquePersistent = Global<T>;
848
849
Steve Blocka7e24c12009-10-30 11:49:00 +0000850 /**
851 * A stack-allocated class that governs a number of local handles.
852 * After a handle scope has been created, all local handles will be
853 * allocated within that handle scope until either the handle scope is
854 * deleted or another handle scope is created. If there is already a
855 * handle scope and a new one is created, all allocations will take
856 * place in the new handle scope until it is deleted. After that,
857 * new handles will again be allocated in the original handle scope.
858 *
859 * After the handle scope of a local handle has been deleted the
860 * garbage collector will no longer track the object stored in the
861 * handle and may deallocate it. The behavior of accessing a handle
862 * for which the handle scope has been deleted is undefined.
863 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000864class V8_EXPORT HandleScope {
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100866 explicit HandleScope(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000867
868 ~HandleScope();
869
870 /**
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 * Counts the number of allocated handles.
872 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 static int NumberOfHandles(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000874
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 V8_INLINE Isolate* GetIsolate() const {
876 return reinterpret_cast<Isolate*>(isolate_);
877 }
878
879 protected:
880 V8_INLINE HandleScope() {}
881
882 void Initialize(Isolate* isolate);
883
884 static internal::Object** CreateHandle(internal::Isolate* isolate,
885 internal::Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886
887 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000888 // Uses heap_object to obtain the current Isolate.
889 static internal::Object** CreateHandle(internal::HeapObject* heap_object,
890 internal::Object* value);
891
892 // Make it hard to create heap-allocated or illegal handle scopes by
893 // disallowing certain operations.
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 HandleScope(const HandleScope&);
895 void operator=(const HandleScope&);
896 void* operator new(size_t size);
897 void operator delete(void*, size_t);
898
Steve Block44f0eee2011-05-26 01:26:41 +0100899 internal::Isolate* isolate_;
John Reck59135872010-11-02 12:39:01 -0700900 internal::Object** prev_next_;
901 internal::Object** prev_limit_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000902
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 // Local::New uses CreateHandle with an Isolate* parameter.
904 template<class F> friend class Local;
Steve Blocka7e24c12009-10-30 11:49:00 +0000905
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
907 // a HeapObject* in their shortcuts.
908 friend class Object;
909 friend class Context;
Steve Blocka7e24c12009-10-30 11:49:00 +0000910};
911
912
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000913/**
914 * A HandleScope which first allocates a handle in the current scope
915 * which will be later filled with the escape value.
916 */
917class V8_EXPORT EscapableHandleScope : public HandleScope {
918 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100919 explicit EscapableHandleScope(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 V8_INLINE ~EscapableHandleScope() {}
921
922 /**
923 * Pushes the value into the previous scope and returns a handle to it.
924 * Cannot be called twice.
925 */
926 template <class T>
927 V8_INLINE Local<T> Escape(Local<T> value) {
928 internal::Object** slot =
929 Escape(reinterpret_cast<internal::Object**>(*value));
930 return Local<T>(reinterpret_cast<T*>(slot));
931 }
932
933 private:
934 internal::Object** Escape(internal::Object** escape_value);
935
936 // Make it hard to create heap-allocated or illegal handle scopes by
937 // disallowing certain operations.
938 EscapableHandleScope(const EscapableHandleScope&);
939 void operator=(const EscapableHandleScope&);
940 void* operator new(size_t size);
941 void operator delete(void*, size_t);
942
943 internal::Object** escape_slot_;
944};
945
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000946class V8_EXPORT SealHandleScope {
947 public:
948 SealHandleScope(Isolate* isolate);
949 ~SealHandleScope();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 private:
952 // Make it hard to create heap-allocated or illegal handle scopes by
953 // disallowing certain operations.
954 SealHandleScope(const SealHandleScope&);
955 void operator=(const SealHandleScope&);
956 void* operator new(size_t size);
957 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000958
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000959 internal::Isolate* isolate_;
960 internal::Object** prev_limit_;
961 int prev_sealed_level_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000962};
963
964
Ben Murdoch257744e2011-11-30 15:57:28 +0000965// --- Special objects ---
Steve Blocka7e24c12009-10-30 11:49:00 +0000966
967
968/**
969 * The superclass of values and API object templates.
970 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000971class V8_EXPORT Data {
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 private:
973 Data();
974};
975
976
977/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000978 * The optional attributes of ScriptOrigin.
979 */
980class ScriptOriginOptions {
981 public:
982 V8_INLINE ScriptOriginOptions(bool is_embedder_debug_script = false,
983 bool is_shared_cross_origin = false,
984 bool is_opaque = false)
985 : flags_((is_embedder_debug_script ? kIsEmbedderDebugScript : 0) |
986 (is_shared_cross_origin ? kIsSharedCrossOrigin : 0) |
987 (is_opaque ? kIsOpaque : 0)) {}
988 V8_INLINE ScriptOriginOptions(int flags)
989 : flags_(flags &
990 (kIsEmbedderDebugScript | kIsSharedCrossOrigin | kIsOpaque)) {}
991 bool IsEmbedderDebugScript() const {
992 return (flags_ & kIsEmbedderDebugScript) != 0;
993 }
994 bool IsSharedCrossOrigin() const {
995 return (flags_ & kIsSharedCrossOrigin) != 0;
996 }
997 bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; }
998 int Flags() const { return flags_; }
999
1000 private:
1001 enum {
1002 kIsEmbedderDebugScript = 1,
1003 kIsSharedCrossOrigin = 1 << 1,
1004 kIsOpaque = 1 << 2
1005 };
1006 const int flags_;
1007};
1008
1009/**
Steve Blocka7e24c12009-10-30 11:49:00 +00001010 * The origin, within a file, of a script.
1011 */
Steve Block8defd9f2010-07-08 12:39:36 +01001012class ScriptOrigin {
Steve Blocka7e24c12009-10-30 11:49:00 +00001013 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001014 V8_INLINE ScriptOrigin(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 Local<Value> resource_name,
1016 Local<Integer> resource_line_offset = Local<Integer>(),
1017 Local<Integer> resource_column_offset = Local<Integer>(),
1018 Local<Boolean> resource_is_shared_cross_origin = Local<Boolean>(),
1019 Local<Integer> script_id = Local<Integer>(),
1020 Local<Boolean> resource_is_embedder_debug_script = Local<Boolean>(),
1021 Local<Value> source_map_url = Local<Value>(),
1022 Local<Boolean> resource_is_opaque = Local<Boolean>());
1023 V8_INLINE Local<Value> ResourceName() const;
1024 V8_INLINE Local<Integer> ResourceLineOffset() const;
1025 V8_INLINE Local<Integer> ResourceColumnOffset() const;
1026 /**
1027 * Returns true for embedder's debugger scripts
1028 */
1029 V8_INLINE Local<Integer> ScriptID() const;
1030 V8_INLINE Local<Value> SourceMapUrl() const;
1031 V8_INLINE ScriptOriginOptions Options() const { return options_; }
1032
Steve Blocka7e24c12009-10-30 11:49:00 +00001033 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034 Local<Value> resource_name_;
1035 Local<Integer> resource_line_offset_;
1036 Local<Integer> resource_column_offset_;
1037 ScriptOriginOptions options_;
1038 Local<Integer> script_id_;
1039 Local<Value> source_map_url_;
Steve Blocka7e24c12009-10-30 11:49:00 +00001040};
1041
1042
1043/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 * A compiled JavaScript script, not yet tied to a Context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046class V8_EXPORT UnboundScript {
Steve Blocka7e24c12009-10-30 11:49:00 +00001047 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 * Binds the script to the currently entered context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001050 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001051 Local<Script> BindToCurrentContext();
1052
1053 int GetId();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001054 Local<Value> GetScriptName();
Steve Blocka7e24c12009-10-30 11:49:00 +00001055
1056 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 * Data read from magic sourceURL comments.
1058 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059 Local<Value> GetSourceURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001060 /**
1061 * Data read from magic sourceMappingURL comments.
1062 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 Local<Value> GetSourceMappingURL();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064
1065 /**
1066 * Returns zero based line number of the code_pos location in the script.
1067 * -1 will be returned if no information available.
1068 */
1069 int GetLineNumber(int code_pos);
1070
1071 static const int kNoScriptId = 0;
1072};
1073
1074
1075/**
1076 * A compiled JavaScript script, tied to a Context which was active when the
1077 * script was compiled.
1078 */
1079class V8_EXPORT Script {
1080 public:
1081 /**
1082 * A shorthand for ScriptCompiler::Compile().
1083 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001084 static V8_DEPRECATE_SOON(
1085 "Use maybe version",
1086 Local<Script> Compile(Local<String> source,
1087 ScriptOrigin* origin = nullptr));
1088 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1089 Local<Context> context, Local<String> source,
1090 ScriptOrigin* origin = nullptr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001091
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001092 static Local<Script> V8_DEPRECATE_SOON("Use maybe version",
1093 Compile(Local<String> source,
1094 Local<String> file_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095
1096 /**
1097 * Runs the script returning the resulting value. It will be run in the
1098 * context in which it was created (ScriptCompiler::CompileBound or
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001099 * UnboundScript::BindToCurrentContext()).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001101 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Run());
1102 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Run(Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103
1104 /**
1105 * Returns the corresponding context-unbound script.
1106 */
1107 Local<UnboundScript> GetUnboundScript();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001108};
1109
1110
1111/**
1112 * For compiling scripts.
1113 */
1114class V8_EXPORT ScriptCompiler {
1115 public:
1116 /**
1117 * Compilation data that the embedder can cache and pass back to speed up
1118 * future compilations. The data is produced if the CompilerOptions passed to
1119 * the compilation functions in ScriptCompiler contains produce_data_to_cache
1120 * = true. The data to cache can then can be retrieved from
1121 * UnboundScript.
1122 */
1123 struct V8_EXPORT CachedData {
1124 enum BufferPolicy {
1125 BufferNotOwned,
1126 BufferOwned
1127 };
1128
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001129 CachedData()
1130 : data(NULL),
1131 length(0),
1132 rejected(false),
1133 buffer_policy(BufferNotOwned) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134
1135 // If buffer_policy is BufferNotOwned, the caller keeps the ownership of
1136 // data and guarantees that it stays alive until the CachedData object is
1137 // destroyed. If the policy is BufferOwned, the given data will be deleted
1138 // (with delete[]) when the CachedData object is destroyed.
1139 CachedData(const uint8_t* data, int length,
1140 BufferPolicy buffer_policy = BufferNotOwned);
1141 ~CachedData();
1142 // TODO(marja): Async compilation; add constructors which take a callback
1143 // which will be called when V8 no longer needs the data.
1144 const uint8_t* data;
1145 int length;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001146 bool rejected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 BufferPolicy buffer_policy;
1148
1149 private:
1150 // Prevent copying. Not implemented.
1151 CachedData(const CachedData&);
1152 CachedData& operator=(const CachedData&);
1153 };
1154
1155 /**
1156 * Source code which can be then compiled to a UnboundScript or Script.
1157 */
1158 class Source {
1159 public:
1160 // Source takes ownership of CachedData.
1161 V8_INLINE Source(Local<String> source_string, const ScriptOrigin& origin,
1162 CachedData* cached_data = NULL);
1163 V8_INLINE Source(Local<String> source_string,
1164 CachedData* cached_data = NULL);
1165 V8_INLINE ~Source();
1166
1167 // Ownership of the CachedData or its buffers is *not* transferred to the
1168 // caller. The CachedData object is alive as long as the Source object is
1169 // alive.
1170 V8_INLINE const CachedData* GetCachedData() const;
1171
1172 private:
1173 friend class ScriptCompiler;
1174 // Prevent copying. Not implemented.
1175 Source(const Source&);
1176 Source& operator=(const Source&);
1177
1178 Local<String> source_string;
1179
1180 // Origin information
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001181 Local<Value> resource_name;
1182 Local<Integer> resource_line_offset;
1183 Local<Integer> resource_column_offset;
1184 ScriptOriginOptions resource_options;
1185 Local<Value> source_map_url;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001186
1187 // Cached data from previous compilation (if a kConsume*Cache flag is
1188 // set), or hold newly generated cache data (kProduce*Cache flags) are
1189 // set when calling a compile method.
1190 CachedData* cached_data;
1191 };
1192
1193 /**
1194 * For streaming incomplete script data to V8. The embedder should implement a
1195 * subclass of this class.
1196 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001197 class V8_EXPORT ExternalSourceStream {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198 public:
1199 virtual ~ExternalSourceStream() {}
1200
1201 /**
1202 * V8 calls this to request the next chunk of data from the embedder. This
1203 * function will be called on a background thread, so it's OK to block and
1204 * wait for the data, if the embedder doesn't have data yet. Returns the
1205 * length of the data returned. When the data ends, GetMoreData should
1206 * return 0. Caller takes ownership of the data.
1207 *
1208 * When streaming UTF-8 data, V8 handles multi-byte characters split between
1209 * two data chunks, but doesn't handle multi-byte characters split between
1210 * more than two data chunks. The embedder can avoid this problem by always
1211 * returning at least 2 bytes of data.
1212 *
1213 * If the embedder wants to cancel the streaming, they should make the next
1214 * GetMoreData call return 0. V8 will interpret it as end of data (and most
1215 * probably, parsing will fail). The streaming task will return as soon as
1216 * V8 has parsed the data it received so far.
1217 */
1218 virtual size_t GetMoreData(const uint8_t** src) = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001219
1220 /**
1221 * V8 calls this method to set a 'bookmark' at the current position in
1222 * the source stream, for the purpose of (maybe) later calling
1223 * ResetToBookmark. If ResetToBookmark is called later, then subsequent
1224 * calls to GetMoreData should return the same data as they did when
1225 * SetBookmark was called earlier.
1226 *
1227 * The embedder may return 'false' to indicate it cannot provide this
1228 * functionality.
1229 */
1230 virtual bool SetBookmark();
1231
1232 /**
1233 * V8 calls this to return to a previously set bookmark.
1234 */
1235 virtual void ResetToBookmark();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 };
1237
1238
1239 /**
1240 * Source code which can be streamed into V8 in pieces. It will be parsed
1241 * while streaming. It can be compiled after the streaming is complete.
1242 * StreamedSource must be kept alive while the streaming task is ran (see
1243 * ScriptStreamingTask below).
1244 */
1245 class V8_EXPORT StreamedSource {
1246 public:
1247 enum Encoding { ONE_BYTE, TWO_BYTE, UTF8 };
1248
1249 StreamedSource(ExternalSourceStream* source_stream, Encoding encoding);
1250 ~StreamedSource();
1251
1252 // Ownership of the CachedData or its buffers is *not* transferred to the
1253 // caller. The CachedData object is alive as long as the StreamedSource
1254 // object is alive.
1255 const CachedData* GetCachedData() const;
1256
1257 internal::StreamedSource* impl() const { return impl_; }
1258
1259 private:
1260 // Prevent copying. Not implemented.
1261 StreamedSource(const StreamedSource&);
1262 StreamedSource& operator=(const StreamedSource&);
1263
1264 internal::StreamedSource* impl_;
1265 };
1266
1267 /**
1268 * A streaming task which the embedder must run on a background thread to
1269 * stream scripts into V8. Returned by ScriptCompiler::StartStreamingScript.
1270 */
1271 class ScriptStreamingTask {
1272 public:
1273 virtual ~ScriptStreamingTask() {}
1274 virtual void Run() = 0;
1275 };
1276
1277 enum CompileOptions {
1278 kNoCompileOptions = 0,
1279 kProduceParserCache,
1280 kConsumeParserCache,
1281 kProduceCodeCache,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001282 kConsumeCodeCache
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 };
1284
1285 /**
1286 * Compiles the specified script (context-independent).
1287 * Cached data as part of the source object can be optionally produced to be
1288 * consumed later to speed up compilation of identical source scripts.
1289 *
1290 * Note that when producing cached data, the source must point to NULL for
1291 * cached data. When consuming cached data, the cached data must have been
1292 * produced by the same version of V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00001293 *
Andrei Popescu402d9372010-02-26 13:31:12 +00001294 * \param source Script source code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295 * \return Compiled script object (context independent; for running it must be
1296 * bound to a context).
Andrei Popescu402d9372010-02-26 13:31:12 +00001297 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001298 static V8_DEPRECATED("Use maybe version",
1299 Local<UnboundScript> CompileUnbound(
1300 Isolate* isolate, Source* source,
1301 CompileOptions options = kNoCompileOptions));
1302 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundScript(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001303 Isolate* isolate, Source* source,
1304 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001305
1306 /**
1307 * Compiles the specified script (bound to current context).
1308 *
1309 * \param source Script source code.
Andrei Popescu402d9372010-02-26 13:31:12 +00001310 * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompile()
1311 * using pre_data speeds compilation if it's done multiple times.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 * Owned by caller, no references are kept when this function returns.
Andrei Popescu402d9372010-02-26 13:31:12 +00001313 * \return Compiled script object, bound to the context that was active
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314 * when this function was called. When run it will always use this
Andrei Popescu402d9372010-02-26 13:31:12 +00001315 * context.
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001317 static V8_DEPRECATED(
1318 "Use maybe version",
1319 Local<Script> Compile(Isolate* isolate, Source* source,
1320 CompileOptions options = kNoCompileOptions));
1321 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1322 Local<Context> context, Source* source,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 CompileOptions options = kNoCompileOptions);
Andrei Popescu402d9372010-02-26 13:31:12 +00001324
1325 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001326 * Returns a task which streams script data into V8, or NULL if the script
1327 * cannot be streamed. The user is responsible for running the task on a
1328 * background thread and deleting it. When ran, the task starts parsing the
1329 * script, and it will request data from the StreamedSource as needed. When
1330 * ScriptStreamingTask::Run exits, all data has been streamed and the script
1331 * can be compiled (see Compile below).
Andrei Popescu402d9372010-02-26 13:31:12 +00001332 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 * This API allows to start the streaming with as little data as possible, and
1334 * the remaining data (for example, the ScriptOrigin) is passed to Compile.
Andrei Popescu402d9372010-02-26 13:31:12 +00001335 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 static ScriptStreamingTask* StartStreamingScript(
1337 Isolate* isolate, StreamedSource* source,
1338 CompileOptions options = kNoCompileOptions);
Steve Blocka7e24c12009-10-30 11:49:00 +00001339
1340 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 * Compiles a streamed script (bound to current context).
1342 *
1343 * This can only be called after the streaming has finished
1344 * (ScriptStreamingTask has been run). V8 doesn't construct the source string
1345 * during streaming, so the embedder needs to pass the full source here.
Steve Blocka7e24c12009-10-30 11:49:00 +00001346 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001347 static V8_DEPRECATED("Use maybe version",
1348 Local<Script> Compile(Isolate* isolate,
1349 StreamedSource* source,
1350 Local<String> full_source_string,
1351 const ScriptOrigin& origin));
1352 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> Compile(
1353 Local<Context> context, StreamedSource* source,
1354 Local<String> full_source_string, const ScriptOrigin& origin);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001355
1356 /**
1357 * Return a version tag for CachedData for the current V8 version & flags.
1358 *
1359 * This value is meant only for determining whether a previously generated
1360 * CachedData instance is still valid; the tag has no other meaing.
1361 *
1362 * Background: The data carried by CachedData may depend on the exact
1363 * V8 version number or currently compiler flags. This means when
1364 * persisting CachedData, the embedder must take care to not pass in
1365 * data from another V8 version, or the same version with different
1366 * features enabled.
1367 *
1368 * The easiest way to do so is to clear the embedder's cache on any
1369 * such change.
1370 *
1371 * Alternatively, this tag can be stored alongside the cached data and
1372 * compared when it is being used.
1373 */
1374 static uint32_t CachedDataVersionTag();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001375
1376 /**
1377 * Compile an ES6 module.
1378 *
1379 * This is an unfinished experimental feature, and is only exposed
1380 * here for internal testing purposes.
1381 * Only parsing works at the moment. Do not use.
1382 *
1383 * TODO(adamk): Script is likely the wrong return value for this;
1384 * should return some new Module type.
1385 */
1386 static V8_WARN_UNUSED_RESULT MaybeLocal<Script> CompileModule(
1387 Local<Context> context, Source* source,
1388 CompileOptions options = kNoCompileOptions);
1389
1390 /**
1391 * Compile a function for a given context. This is equivalent to running
1392 *
1393 * with (obj) {
1394 * return function(args) { ... }
1395 * }
1396 *
1397 * It is possible to specify multiple context extensions (obj in the above
1398 * example).
1399 */
1400 static V8_DEPRECATE_SOON("Use maybe version",
1401 Local<Function> CompileFunctionInContext(
1402 Isolate* isolate, Source* source,
1403 Local<Context> context, size_t arguments_count,
1404 Local<String> arguments[],
1405 size_t context_extension_count,
1406 Local<Object> context_extensions[]));
1407 static V8_WARN_UNUSED_RESULT MaybeLocal<Function> CompileFunctionInContext(
1408 Local<Context> context, Source* source, size_t arguments_count,
1409 Local<String> arguments[], size_t context_extension_count,
1410 Local<Object> context_extensions[]);
1411
1412 private:
1413 static V8_WARN_UNUSED_RESULT MaybeLocal<UnboundScript> CompileUnboundInternal(
1414 Isolate* isolate, Source* source, CompileOptions options, bool is_module);
Steve Blocka7e24c12009-10-30 11:49:00 +00001415};
1416
1417
1418/**
1419 * An error message.
1420 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421class V8_EXPORT Message {
Steve Blocka7e24c12009-10-30 11:49:00 +00001422 public:
1423 Local<String> Get() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001424
1425 V8_DEPRECATE_SOON("Use maybe version", Local<String> GetSourceLine() const);
1426 V8_WARN_UNUSED_RESULT MaybeLocal<String> GetSourceLine(
1427 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001428
1429 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001430 * Returns the origin for the script from where the function causing the
1431 * error originates.
1432 */
1433 ScriptOrigin GetScriptOrigin() const;
1434
1435 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001436 * Returns the resource name for the script from where the function causing
1437 * the error originates.
1438 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001439 Local<Value> GetScriptResourceName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001440
1441 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001442 * Exception stack trace. By default stack traces are not captured for
1443 * uncaught exceptions. SetCaptureStackTraceForUncaughtExceptions allows
1444 * to change this option.
1445 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001446 Local<StackTrace> GetStackTrace() const;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001447
1448 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 * Returns the number, 1-based, of the line where the error occurred.
1450 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001451 V8_DEPRECATE_SOON("Use maybe version", int GetLineNumber() const);
1452 V8_WARN_UNUSED_RESULT Maybe<int> GetLineNumber(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001453
1454 /**
1455 * Returns the index within the script of the first character where
1456 * the error occurred.
1457 */
1458 int GetStartPosition() const;
1459
1460 /**
1461 * Returns the index within the script of the last character where
1462 * the error occurred.
1463 */
1464 int GetEndPosition() const;
1465
1466 /**
1467 * Returns the index within the line of the first character where
1468 * the error occurred.
1469 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001470 V8_DEPRECATE_SOON("Use maybe version", int GetStartColumn() const);
1471 V8_WARN_UNUSED_RESULT Maybe<int> GetStartColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
1473 /**
1474 * Returns the index within the line of the last character where
1475 * the error occurred.
1476 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477 V8_DEPRECATED("Use maybe version", int GetEndColumn() const);
1478 V8_WARN_UNUSED_RESULT Maybe<int> GetEndColumn(Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001479
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001480 /**
1481 * Passes on the value set by the embedder when it fed the script from which
1482 * this Message was generated to V8.
1483 */
1484 bool IsSharedCrossOrigin() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485 bool IsOpaque() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 // TODO(1245381): Print to a string instead of on a FILE.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
Kristian Monsen25f61362010-05-21 11:50:48 +01001489
1490 static const int kNoLineNumberInfo = 0;
1491 static const int kNoColumnInfo = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001492 static const int kNoScriptIdInfo = 0;
Kristian Monsen25f61362010-05-21 11:50:48 +01001493};
1494
1495
1496/**
1497 * Representation of a JavaScript stack trace. The information collected is a
1498 * snapshot of the execution stack and the information remains valid after
1499 * execution continues.
1500 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501class V8_EXPORT StackTrace {
Kristian Monsen25f61362010-05-21 11:50:48 +01001502 public:
1503 /**
1504 * Flags that determine what information is placed captured for each
1505 * StackFrame when grabbing the current stack trace.
1506 */
1507 enum StackTraceOptions {
1508 kLineNumber = 1,
1509 kColumnOffset = 1 << 1 | kLineNumber,
1510 kScriptName = 1 << 2,
1511 kFunctionName = 1 << 3,
1512 kIsEval = 1 << 4,
1513 kIsConstructor = 1 << 5,
Ben Murdochf87a2032010-10-22 12:50:53 +01001514 kScriptNameOrSourceURL = 1 << 6,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 kScriptId = 1 << 7,
1516 kExposeFramesAcrossSecurityOrigins = 1 << 8,
Kristian Monsen25f61362010-05-21 11:50:48 +01001517 kOverview = kLineNumber | kColumnOffset | kScriptName | kFunctionName,
Ben Murdochf87a2032010-10-22 12:50:53 +01001518 kDetailed = kOverview | kIsEval | kIsConstructor | kScriptNameOrSourceURL
Kristian Monsen25f61362010-05-21 11:50:48 +01001519 };
1520
1521 /**
1522 * Returns a StackFrame at a particular index.
1523 */
1524 Local<StackFrame> GetFrame(uint32_t index) const;
1525
1526 /**
1527 * Returns the number of StackFrames.
1528 */
1529 int GetFrameCount() const;
1530
1531 /**
1532 * Returns StackTrace as a v8::Array that contains StackFrame objects.
1533 */
1534 Local<Array> AsArray();
1535
1536 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001537 * Grab a snapshot of the current JavaScript execution stack.
Kristian Monsen25f61362010-05-21 11:50:48 +01001538 *
1539 * \param frame_limit The maximum number of stack frames we want to capture.
1540 * \param options Enumerates the set of things we will capture for each
1541 * StackFrame.
1542 */
1543 static Local<StackTrace> CurrentStackTrace(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 Isolate* isolate,
Kristian Monsen25f61362010-05-21 11:50:48 +01001545 int frame_limit,
1546 StackTraceOptions options = kOverview);
1547};
1548
1549
1550/**
1551 * A single JavaScript stack frame.
1552 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001553class V8_EXPORT StackFrame {
Kristian Monsen25f61362010-05-21 11:50:48 +01001554 public:
1555 /**
1556 * Returns the number, 1-based, of the line for the associate function call.
1557 * This method will return Message::kNoLineNumberInfo if it is unable to
1558 * retrieve the line number, or if kLineNumber was not passed as an option
1559 * when capturing the StackTrace.
1560 */
1561 int GetLineNumber() const;
1562
1563 /**
1564 * Returns the 1-based column offset on the line for the associated function
1565 * call.
1566 * This method will return Message::kNoColumnInfo if it is unable to retrieve
1567 * the column number, or if kColumnOffset was not passed as an option when
1568 * capturing the StackTrace.
1569 */
1570 int GetColumn() const;
1571
1572 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573 * Returns the id of the script for the function for this StackFrame.
1574 * This method will return Message::kNoScriptIdInfo if it is unable to
1575 * retrieve the script id, or if kScriptId was not passed as an option when
1576 * capturing the StackTrace.
1577 */
1578 int GetScriptId() const;
1579
1580 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001581 * Returns the name of the resource that contains the script for the
1582 * function for this StackFrame.
1583 */
1584 Local<String> GetScriptName() const;
1585
1586 /**
Ben Murdochf87a2032010-10-22 12:50:53 +01001587 * Returns the name of the resource that contains the script for the
1588 * function for this StackFrame or sourceURL value if the script name
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 * is undefined and its source ends with //# sourceURL=... string or
1590 * deprecated //@ sourceURL=... string.
Ben Murdochf87a2032010-10-22 12:50:53 +01001591 */
1592 Local<String> GetScriptNameOrSourceURL() const;
1593
1594 /**
Kristian Monsen25f61362010-05-21 11:50:48 +01001595 * Returns the name of the function associated with this stack frame.
1596 */
1597 Local<String> GetFunctionName() const;
1598
1599 /**
1600 * Returns whether or not the associated function is compiled via a call to
1601 * eval().
1602 */
1603 bool IsEval() const;
1604
1605 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00001606 * Returns whether or not the associated function is called as a
Kristian Monsen25f61362010-05-21 11:50:48 +01001607 * constructor via "new".
1608 */
1609 bool IsConstructor() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001610};
1611
1612
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001613// A StateTag represents a possible state of the VM.
1614enum StateTag { JS, GC, COMPILER, OTHER, EXTERNAL, IDLE };
1615
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001616// A RegisterState represents the current state of registers used
1617// by the sampling profiler API.
1618struct RegisterState {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001619 RegisterState() : pc(nullptr), sp(nullptr), fp(nullptr) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001620 void* pc; // Instruction pointer.
1621 void* sp; // Stack pointer.
1622 void* fp; // Frame pointer.
1623};
1624
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001625// The output structure filled up by GetStackSample API function.
1626struct SampleInfo {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001627 size_t frames_count; // Number of frames collected.
1628 StateTag vm_state; // Current VM state.
1629 void* external_callback_entry; // External callback address if VM is
1630 // executing an external callback.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001631};
1632
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633/**
Ben Murdochc5610432016-08-08 18:44:38 +01001634 * A JSON Parser and Stringifier.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 */
1636class V8_EXPORT JSON {
1637 public:
1638 /**
1639 * Tries to parse the string |json_string| and returns it as value if
1640 * successful.
1641 *
1642 * \param json_string The string to parse.
1643 * \return The corresponding value if successfully parsed.
1644 */
Ben Murdochc5610432016-08-08 18:44:38 +01001645 static V8_DEPRECATED("Use the maybe version taking context",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001646 Local<Value> Parse(Local<String> json_string));
Ben Murdochc5610432016-08-08 18:44:38 +01001647 static V8_DEPRECATE_SOON("Use the maybe version taking context",
1648 MaybeLocal<Value> Parse(Isolate* isolate,
1649 Local<String> json_string));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001650 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> Parse(
Ben Murdochc5610432016-08-08 18:44:38 +01001651 Local<Context> context, Local<String> json_string);
1652
1653 /**
1654 * Tries to stringify the JSON-serializable object |json_object| and returns
1655 * it as string if successful.
1656 *
1657 * \param json_object The JSON-serializable object to stringify.
1658 * \return The corresponding string if successfully stringified.
1659 */
1660 static V8_WARN_UNUSED_RESULT MaybeLocal<String> Stringify(
Ben Murdoch61f157c2016-09-16 13:49:30 +01001661 Local<Context> context, Local<Object> json_object,
1662 Local<String> gap = Local<String>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001663};
1664
1665
1666/**
1667 * A map whose keys are referenced weakly. It is similar to JavaScript WeakMap
1668 * but can be created without entering a v8::Context and hence shouldn't
1669 * escape to JavaScript.
1670 */
1671class V8_EXPORT NativeWeakMap : public Data {
1672 public:
1673 static Local<NativeWeakMap> New(Isolate* isolate);
1674 void Set(Local<Value> key, Local<Value> value);
1675 Local<Value> Get(Local<Value> key);
1676 bool Has(Local<Value> key);
1677 bool Delete(Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001678};
1679
1680
Ben Murdoch257744e2011-11-30 15:57:28 +00001681// --- Value ---
Steve Blocka7e24c12009-10-30 11:49:00 +00001682
1683
1684/**
1685 * The superclass of all JavaScript values and objects.
1686 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001687class V8_EXPORT Value : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00001688 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001689 /**
1690 * Returns true if this value is the undefined value. See ECMA-262
1691 * 4.3.10.
1692 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001693 V8_INLINE bool IsUndefined() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001694
1695 /**
1696 * Returns true if this value is the null value. See ECMA-262
1697 * 4.3.11.
1698 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001699 V8_INLINE bool IsNull() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001700
1701 /**
1702 * Returns true if this value is true.
1703 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704 bool IsTrue() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001705
1706 /**
1707 * Returns true if this value is false.
1708 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709 bool IsFalse() const;
1710
1711 /**
1712 * Returns true if this value is a symbol or a string.
1713 * This is an experimental feature.
1714 */
1715 bool IsName() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001716
1717 /**
1718 * Returns true if this value is an instance of the String type.
1719 * See ECMA-262 8.4.
1720 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721 V8_INLINE bool IsString() const;
1722
1723 /**
1724 * Returns true if this value is a symbol.
1725 * This is an experimental feature.
1726 */
1727 bool IsSymbol() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001728
1729 /**
1730 * Returns true if this value is a function.
1731 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732 bool IsFunction() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001733
1734 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001735 * Returns true if this value is an array. Note that it will return false for
1736 * an Proxy for an array.
Steve Blocka7e24c12009-10-30 11:49:00 +00001737 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001738 bool IsArray() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001739
1740 /**
1741 * Returns true if this value is an object.
1742 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 bool IsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001744
1745 /**
1746 * Returns true if this value is boolean.
1747 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001748 bool IsBoolean() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001749
1750 /**
1751 * Returns true if this value is a number.
1752 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001753 bool IsNumber() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001754
1755 /**
1756 * Returns true if this value is external.
1757 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001758 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001759
1760 /**
1761 * Returns true if this value is a 32-bit signed integer.
1762 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763 bool IsInt32() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001764
1765 /**
Steve Block6ded16b2010-05-10 14:33:55 +01001766 * Returns true if this value is a 32-bit unsigned integer.
1767 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001768 bool IsUint32() const;
Steve Block6ded16b2010-05-10 14:33:55 +01001769
1770 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00001771 * Returns true if this value is a Date.
1772 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001773 bool IsDate() const;
1774
1775 /**
1776 * Returns true if this value is an Arguments object.
1777 */
1778 bool IsArgumentsObject() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001779
Iain Merrick75681382010-08-19 15:07:18 +01001780 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001781 * Returns true if this value is a Boolean object.
1782 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001783 bool IsBooleanObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001784
1785 /**
1786 * Returns true if this value is a Number object.
1787 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001788 bool IsNumberObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001789
1790 /**
1791 * Returns true if this value is a String object.
1792 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793 bool IsStringObject() const;
1794
1795 /**
1796 * Returns true if this value is a Symbol object.
1797 * This is an experimental feature.
1798 */
1799 bool IsSymbolObject() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001800
1801 /**
1802 * Returns true if this value is a NativeError.
1803 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001804 bool IsNativeError() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001805
1806 /**
Iain Merrick75681382010-08-19 15:07:18 +01001807 * Returns true if this value is a RegExp.
1808 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001809 bool IsRegExp() const;
Iain Merrick75681382010-08-19 15:07:18 +01001810
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001811 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001812 * Returns true if this value is a Generator function.
1813 * This is an experimental feature.
1814 */
1815 bool IsGeneratorFunction() const;
1816
1817 /**
1818 * Returns true if this value is a Generator object (iterator).
1819 * This is an experimental feature.
1820 */
1821 bool IsGeneratorObject() const;
1822
1823 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001824 * Returns true if this value is a Promise.
1825 * This is an experimental feature.
1826 */
1827 bool IsPromise() const;
1828
1829 /**
1830 * Returns true if this value is a Map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001831 */
1832 bool IsMap() const;
1833
1834 /**
1835 * Returns true if this value is a Set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001836 */
1837 bool IsSet() const;
1838
1839 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001840 * Returns true if this value is a Map Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001841 */
1842 bool IsMapIterator() const;
1843
1844 /**
1845 * Returns true if this value is a Set Iterator.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001846 */
1847 bool IsSetIterator() const;
1848
1849 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001850 * Returns true if this value is a WeakMap.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001851 */
1852 bool IsWeakMap() const;
1853
1854 /**
1855 * Returns true if this value is a WeakSet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001856 */
1857 bool IsWeakSet() const;
1858
1859 /**
1860 * Returns true if this value is an ArrayBuffer.
1861 * This is an experimental feature.
1862 */
1863 bool IsArrayBuffer() const;
1864
1865 /**
1866 * Returns true if this value is an ArrayBufferView.
1867 * This is an experimental feature.
1868 */
1869 bool IsArrayBufferView() const;
1870
1871 /**
1872 * Returns true if this value is one of TypedArrays.
1873 * This is an experimental feature.
1874 */
1875 bool IsTypedArray() const;
1876
1877 /**
1878 * Returns true if this value is an Uint8Array.
1879 * This is an experimental feature.
1880 */
1881 bool IsUint8Array() const;
1882
1883 /**
1884 * Returns true if this value is an Uint8ClampedArray.
1885 * This is an experimental feature.
1886 */
1887 bool IsUint8ClampedArray() const;
1888
1889 /**
1890 * Returns true if this value is an Int8Array.
1891 * This is an experimental feature.
1892 */
1893 bool IsInt8Array() const;
1894
1895 /**
1896 * Returns true if this value is an Uint16Array.
1897 * This is an experimental feature.
1898 */
1899 bool IsUint16Array() const;
1900
1901 /**
1902 * Returns true if this value is an Int16Array.
1903 * This is an experimental feature.
1904 */
1905 bool IsInt16Array() const;
1906
1907 /**
1908 * Returns true if this value is an Uint32Array.
1909 * This is an experimental feature.
1910 */
1911 bool IsUint32Array() const;
1912
1913 /**
1914 * Returns true if this value is an Int32Array.
1915 * This is an experimental feature.
1916 */
1917 bool IsInt32Array() const;
1918
1919 /**
1920 * Returns true if this value is a Float32Array.
1921 * This is an experimental feature.
1922 */
1923 bool IsFloat32Array() const;
1924
1925 /**
1926 * Returns true if this value is a Float64Array.
1927 * This is an experimental feature.
1928 */
1929 bool IsFloat64Array() const;
1930
1931 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001932 * Returns true if this value is a SIMD Float32x4.
1933 * This is an experimental feature.
1934 */
1935 bool IsFloat32x4() const;
1936
1937 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001938 * Returns true if this value is a DataView.
1939 * This is an experimental feature.
1940 */
1941 bool IsDataView() const;
1942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001943 /**
1944 * Returns true if this value is a SharedArrayBuffer.
1945 * This is an experimental feature.
1946 */
1947 bool IsSharedArrayBuffer() const;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001948
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001949 /**
1950 * Returns true if this value is a JavaScript Proxy.
1951 */
1952 bool IsProxy() const;
1953
1954
1955 V8_WARN_UNUSED_RESULT MaybeLocal<Boolean> ToBoolean(
1956 Local<Context> context) const;
1957 V8_WARN_UNUSED_RESULT MaybeLocal<Number> ToNumber(
1958 Local<Context> context) const;
1959 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToString(
1960 Local<Context> context) const;
1961 V8_WARN_UNUSED_RESULT MaybeLocal<String> ToDetailString(
1962 Local<Context> context) const;
1963 V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
1964 Local<Context> context) const;
1965 V8_WARN_UNUSED_RESULT MaybeLocal<Integer> ToInteger(
1966 Local<Context> context) const;
1967 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToUint32(
1968 Local<Context> context) const;
1969 V8_WARN_UNUSED_RESULT MaybeLocal<Int32> ToInt32(Local<Context> context) const;
1970
1971 V8_DEPRECATE_SOON("Use maybe version",
1972 Local<Boolean> ToBoolean(Isolate* isolate) const);
1973 V8_DEPRECATE_SOON("Use maybe version",
1974 Local<Number> ToNumber(Isolate* isolate) const);
1975 V8_DEPRECATE_SOON("Use maybe version",
1976 Local<String> ToString(Isolate* isolate) const);
1977 V8_DEPRECATED("Use maybe version",
1978 Local<String> ToDetailString(Isolate* isolate) const);
1979 V8_DEPRECATE_SOON("Use maybe version",
1980 Local<Object> ToObject(Isolate* isolate) const);
1981 V8_DEPRECATE_SOON("Use maybe version",
1982 Local<Integer> ToInteger(Isolate* isolate) const);
1983 V8_DEPRECATED("Use maybe version",
1984 Local<Uint32> ToUint32(Isolate* isolate) const);
1985 V8_DEPRECATE_SOON("Use maybe version",
1986 Local<Int32> ToInt32(Isolate* isolate) const);
1987
1988 inline V8_DEPRECATE_SOON("Use maybe version",
1989 Local<Boolean> ToBoolean() const);
1990 inline V8_DEPRECATED("Use maybe version", Local<Number> ToNumber() const);
1991 inline V8_DEPRECATE_SOON("Use maybe version", Local<String> ToString() const);
1992 inline V8_DEPRECATED("Use maybe version",
1993 Local<String> ToDetailString() const);
1994 inline V8_DEPRECATE_SOON("Use maybe version", Local<Object> ToObject() const);
1995 inline V8_DEPRECATE_SOON("Use maybe version",
1996 Local<Integer> ToInteger() const);
1997 inline V8_DEPRECATED("Use maybe version", Local<Uint32> ToUint32() const);
1998 inline V8_DEPRECATED("Use maybe version", Local<Int32> ToInt32() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00001999
2000 /**
2001 * Attempts to convert a string to an array index.
2002 * Returns an empty handle if the conversion fails.
2003 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002004 V8_DEPRECATED("Use maybe version", Local<Uint32> ToArrayIndex() const);
2005 V8_WARN_UNUSED_RESULT MaybeLocal<Uint32> ToArrayIndex(
2006 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002007
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002008 V8_WARN_UNUSED_RESULT Maybe<bool> BooleanValue(Local<Context> context) const;
2009 V8_WARN_UNUSED_RESULT Maybe<double> NumberValue(Local<Context> context) const;
2010 V8_WARN_UNUSED_RESULT Maybe<int64_t> IntegerValue(
2011 Local<Context> context) const;
2012 V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
2013 Local<Context> context) const;
2014 V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
2015
2016 V8_DEPRECATE_SOON("Use maybe version", bool BooleanValue() const);
2017 V8_DEPRECATE_SOON("Use maybe version", double NumberValue() const);
2018 V8_DEPRECATE_SOON("Use maybe version", int64_t IntegerValue() const);
2019 V8_DEPRECATE_SOON("Use maybe version", uint32_t Uint32Value() const);
2020 V8_DEPRECATE_SOON("Use maybe version", int32_t Int32Value() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00002021
2022 /** JS == */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002023 V8_DEPRECATE_SOON("Use maybe version", bool Equals(Local<Value> that) const);
2024 V8_WARN_UNUSED_RESULT Maybe<bool> Equals(Local<Context> context,
2025 Local<Value> that) const;
2026 bool StrictEquals(Local<Value> that) const;
2027 bool SameValue(Local<Value> that) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002028
2029 template <class T> V8_INLINE static Value* Cast(T* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002030
Ben Murdochc5610432016-08-08 18:44:38 +01002031 Local<String> TypeOf(v8::Isolate*);
2032
Steve Blocka7e24c12009-10-30 11:49:00 +00002033 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002034 V8_INLINE bool QuickIsUndefined() const;
2035 V8_INLINE bool QuickIsNull() const;
2036 V8_INLINE bool QuickIsString() const;
2037 bool FullIsUndefined() const;
2038 bool FullIsNull() const;
2039 bool FullIsString() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002040};
2041
2042
2043/**
2044 * The superclass of primitive values. See ECMA-262 4.3.2.
2045 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002046class V8_EXPORT Primitive : public Value { };
Steve Blocka7e24c12009-10-30 11:49:00 +00002047
2048
2049/**
2050 * A primitive boolean value (ECMA-262, 4.3.14). Either the true
2051 * or false value.
2052 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002053class V8_EXPORT Boolean : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002054 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002055 bool Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002056 V8_INLINE static Boolean* Cast(v8::Value* obj);
2057 V8_INLINE static Local<Boolean> New(Isolate* isolate, bool value);
2058
2059 private:
2060 static void CheckCast(v8::Value* obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002061};
2062
2063
2064/**
2065 * A superclass for symbols and strings.
2066 */
2067class V8_EXPORT Name : public Primitive {
2068 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002069 /**
2070 * Returns the identity hash for this object. The current implementation
2071 * uses an inline property on the object to store the identity hash.
2072 *
2073 * The return value will never be 0. Also, it is not guaranteed to be
2074 * unique.
2075 */
2076 int GetIdentityHash();
2077
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002078 V8_INLINE static Name* Cast(v8::Value* obj);
2079 private:
2080 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002081};
2082
2083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002084enum class NewStringType { kNormal, kInternalized };
2085
2086
Steve Blocka7e24c12009-10-30 11:49:00 +00002087/**
2088 * A JavaScript string value (ECMA-262, 4.3.17).
2089 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002090class V8_EXPORT String : public Name {
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002092 static const int kMaxLength = (1 << 28) - 16;
2093
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002094 enum Encoding {
2095 UNKNOWN_ENCODING = 0x1,
2096 TWO_BYTE_ENCODING = 0x0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002097 ONE_BYTE_ENCODING = 0x4
2098 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002099 /**
2100 * Returns the number of characters in this string.
2101 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002102 int Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002103
2104 /**
2105 * Returns the number of bytes in the UTF-8 encoded
2106 * representation of this string.
2107 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002108 int Utf8Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002109
2110 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002111 * Returns whether this string is known to contain only one byte data.
2112 * Does not read the string.
2113 * False negatives are possible.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002114 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002115 bool IsOneByte() const;
2116
2117 /**
2118 * Returns whether this string contain only one byte data.
2119 * Will read the entire string in some cases.
2120 */
2121 bool ContainsOnlyOneByte() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002122
2123 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002124 * Write the contents of the string to an external buffer.
2125 * If no arguments are given, expects the buffer to be large
2126 * enough to hold the entire string and NULL terminator. Copies
2127 * the contents of the string and the NULL terminator into the
2128 * buffer.
2129 *
Ben Murdochb0fe1622011-05-05 13:52:32 +01002130 * WriteUtf8 will not write partial UTF-8 sequences, preferring to stop
2131 * before the end of the buffer.
2132 *
Steve Blocka7e24c12009-10-30 11:49:00 +00002133 * Copies up to length characters into the output buffer.
2134 * Only null-terminates if there is enough space in the buffer.
2135 *
2136 * \param buffer The buffer into which the string will be copied.
2137 * \param start The starting position within the string at which
2138 * copying begins.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002139 * \param length The number of characters to copy from the string. For
2140 * WriteUtf8 the number of bytes in the buffer.
Steve Block6ded16b2010-05-10 14:33:55 +01002141 * \param nchars_ref The number of characters written, can be NULL.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002142 * \param options Various options that might affect performance of this or
Steve Block6ded16b2010-05-10 14:33:55 +01002143 * subsequent operations.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002144 * \return The number of characters copied to the buffer excluding the null
2145 * terminator. For WriteUtf8: The number of bytes copied to the buffer
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002146 * including the null terminator (if written).
Steve Blocka7e24c12009-10-30 11:49:00 +00002147 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002148 enum WriteOptions {
2149 NO_OPTIONS = 0,
2150 HINT_MANY_WRITES_EXPECTED = 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002151 NO_NULL_TERMINATION = 2,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002152 PRESERVE_ONE_BYTE_NULL = 4,
2153 // Used by WriteUtf8 to replace orphan surrogate code units with the
2154 // unicode replacement character. Needs to be set to guarantee valid UTF-8
2155 // output.
2156 REPLACE_INVALID_UTF8 = 8
Steve Block6ded16b2010-05-10 14:33:55 +01002157 };
2158
Ben Murdoch589d6972011-11-30 16:04:58 +00002159 // 16-bit character codes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002160 int Write(uint16_t* buffer,
2161 int start = 0,
2162 int length = -1,
2163 int options = NO_OPTIONS) const;
2164 // One byte characters.
2165 int WriteOneByte(uint8_t* buffer,
2166 int start = 0,
2167 int length = -1,
2168 int options = NO_OPTIONS) const;
Ben Murdoch589d6972011-11-30 16:04:58 +00002169 // UTF-8 encoded characters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170 int WriteUtf8(char* buffer,
2171 int length = -1,
2172 int* nchars_ref = NULL,
2173 int options = NO_OPTIONS) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002174
2175 /**
2176 * A zero length string.
2177 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002178 V8_INLINE static v8::Local<v8::String> Empty(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002179
2180 /**
2181 * Returns true if the string is external
2182 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002183 bool IsExternal() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002184
2185 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186 * Returns true if the string is both external and one-byte.
Steve Blocka7e24c12009-10-30 11:49:00 +00002187 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002188 bool IsExternalOneByte() const;
Leon Clarkee46be812010-01-19 14:06:41 +00002189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002190 class V8_EXPORT ExternalStringResourceBase { // NOLINT
Leon Clarkee46be812010-01-19 14:06:41 +00002191 public:
2192 virtual ~ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002194 virtual bool IsCompressible() const { return false; }
2195
Leon Clarkee46be812010-01-19 14:06:41 +00002196 protected:
2197 ExternalStringResourceBase() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002198
2199 /**
2200 * Internally V8 will call this Dispose method when the external string
2201 * resource is no longer needed. The default implementation will use the
2202 * delete operator. This method can be overridden in subclasses to
2203 * control how allocated external string resources are disposed.
2204 */
2205 virtual void Dispose() { delete this; }
2206
Leon Clarkee46be812010-01-19 14:06:41 +00002207 private:
2208 // Disallow copying and assigning.
2209 ExternalStringResourceBase(const ExternalStringResourceBase&);
2210 void operator=(const ExternalStringResourceBase&);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002211
2212 friend class v8::internal::Heap;
Leon Clarkee46be812010-01-19 14:06:41 +00002213 };
2214
Steve Blocka7e24c12009-10-30 11:49:00 +00002215 /**
2216 * An ExternalStringResource is a wrapper around a two-byte string
2217 * buffer that resides outside V8's heap. Implement an
2218 * ExternalStringResource to manage the life cycle of the underlying
2219 * buffer. Note that the string data must be immutable.
2220 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002221 class V8_EXPORT ExternalStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002222 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002223 public:
2224 /**
2225 * Override the destructor to manage the life cycle of the underlying
2226 * buffer.
2227 */
2228 virtual ~ExternalStringResource() {}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002229
2230 /**
2231 * The string data from the underlying buffer.
2232 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002233 virtual const uint16_t* data() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002234
2235 /**
2236 * The length of the string. That is, the number of two-byte characters.
2237 */
Steve Blocka7e24c12009-10-30 11:49:00 +00002238 virtual size_t length() const = 0;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002239
Steve Blocka7e24c12009-10-30 11:49:00 +00002240 protected:
2241 ExternalStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002242 };
2243
2244 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002245 * An ExternalOneByteStringResource is a wrapper around an one-byte
Steve Blocka7e24c12009-10-30 11:49:00 +00002246 * string buffer that resides outside V8's heap. Implement an
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002247 * ExternalOneByteStringResource to manage the life cycle of the
Steve Blocka7e24c12009-10-30 11:49:00 +00002248 * underlying buffer. Note that the string data must be immutable
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002249 * and that the data must be Latin-1 and not UTF-8, which would require
2250 * special treatment internally in the engine and do not allow efficient
2251 * indexing. Use String::New or convert to 16 bit data for non-Latin1.
Steve Blocka7e24c12009-10-30 11:49:00 +00002252 */
2253
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002254 class V8_EXPORT ExternalOneByteStringResource
Leon Clarkee46be812010-01-19 14:06:41 +00002255 : public ExternalStringResourceBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00002256 public:
2257 /**
2258 * Override the destructor to manage the life cycle of the underlying
2259 * buffer.
2260 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002261 virtual ~ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002262 /** The string data from the underlying buffer.*/
2263 virtual const char* data() const = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002264 /** The number of Latin-1 characters in the string.*/
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 virtual size_t length() const = 0;
2266 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002267 ExternalOneByteStringResource() {}
Steve Blocka7e24c12009-10-30 11:49:00 +00002268 };
2269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002270 /**
2271 * If the string is an external string, return the ExternalStringResourceBase
2272 * regardless of the encoding, otherwise return NULL. The encoding of the
2273 * string is returned in encoding_out.
2274 */
2275 V8_INLINE ExternalStringResourceBase* GetExternalStringResourceBase(
2276 Encoding* encoding_out) const;
2277
Steve Blocka7e24c12009-10-30 11:49:00 +00002278 /**
2279 * Get the ExternalStringResource for an external string. Returns
2280 * NULL if IsExternal() doesn't return true.
2281 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002282 V8_INLINE ExternalStringResource* GetExternalStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002283
2284 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002285 * Get the ExternalOneByteStringResource for an external one-byte string.
2286 * Returns NULL if IsExternalOneByte() doesn't return true.
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002288 const ExternalOneByteStringResource* GetExternalOneByteStringResource() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002289
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002290 V8_INLINE static String* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292 // TODO(dcarney): remove with deprecation of New functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002293 enum NewStringType {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002294 kNormalString = static_cast<int>(v8::NewStringType::kNormal),
2295 kInternalizedString = static_cast<int>(v8::NewStringType::kInternalized)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002296 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002297
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002298 /** Allocates a new string from UTF-8 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002299 static V8_DEPRECATE_SOON(
2300 "Use maybe version",
2301 Local<String> NewFromUtf8(Isolate* isolate, const char* data,
2302 NewStringType type = kNormalString,
2303 int length = -1));
2304
2305 /** Allocates a new string from UTF-8 data. Only returns an empty value when
2306 * length > kMaxLength. **/
2307 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromUtf8(
2308 Isolate* isolate, const char* data, v8::NewStringType type,
2309 int length = -1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002310
2311 /** Allocates a new string from Latin-1 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002312 static V8_DEPRECATED(
2313 "Use maybe version",
2314 Local<String> NewFromOneByte(Isolate* isolate, const uint8_t* data,
2315 NewStringType type = kNormalString,
2316 int length = -1));
2317
2318 /** Allocates a new string from Latin-1 data. Only returns an empty value
2319 * when length > kMaxLength. **/
2320 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromOneByte(
2321 Isolate* isolate, const uint8_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002322 int length = -1);
2323
2324 /** Allocates a new string from UTF-16 data.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002325 static V8_DEPRECATE_SOON(
2326 "Use maybe version",
2327 Local<String> NewFromTwoByte(Isolate* isolate, const uint16_t* data,
2328 NewStringType type = kNormalString,
2329 int length = -1));
2330
2331 /** Allocates a new string from UTF-16 data. Only returns an empty value when
2332 * length > kMaxLength. **/
2333 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewFromTwoByte(
2334 Isolate* isolate, const uint16_t* data, v8::NewStringType type,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002335 int length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002336
2337 /**
Steve Block3ce2e202009-11-05 08:53:23 +00002338 * Creates a new string by concatenating the left and the right strings
2339 * passed in as parameters.
2340 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002341 static Local<String> Concat(Local<String> left, Local<String> right);
Steve Block3ce2e202009-11-05 08:53:23 +00002342
2343 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002344 * Creates a new external string using the data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002345 * resource. When the external string is no longer live on V8's heap the
2346 * resource will be disposed by calling its Dispose method. The caller of
2347 * this function should not otherwise delete or modify the resource. Neither
2348 * should the underlying buffer be deallocated or modified except through the
2349 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002350 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002351 static V8_DEPRECATED("Use maybe version",
2352 Local<String> NewExternal(
2353 Isolate* isolate, ExternalStringResource* resource));
2354 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalTwoByte(
2355 Isolate* isolate, ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002356
2357 /**
2358 * Associate an external string resource with this string by transforming it
2359 * in place so that existing references to this string in the JavaScript heap
2360 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002361 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002362 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002363 * The string is not modified if the operation fails. See NewExternal for
2364 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002365 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002366 bool MakeExternal(ExternalStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002367
2368 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002369 * Creates a new external string using the one-byte data defined in the given
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002370 * resource. When the external string is no longer live on V8's heap the
2371 * resource will be disposed by calling its Dispose method. The caller of
2372 * this function should not otherwise delete or modify the resource. Neither
2373 * should the underlying buffer be deallocated or modified except through the
2374 * destructor of the external string resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002375 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002376 static V8_DEPRECATE_SOON(
2377 "Use maybe version",
2378 Local<String> NewExternal(Isolate* isolate,
2379 ExternalOneByteStringResource* resource));
2380 static V8_WARN_UNUSED_RESULT MaybeLocal<String> NewExternalOneByte(
2381 Isolate* isolate, ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002382
2383 /**
2384 * Associate an external string resource with this string by transforming it
2385 * in place so that existing references to this string in the JavaScript heap
2386 * will use the external string resource. The external string resource's
Ben Murdoch257744e2011-11-30 15:57:28 +00002387 * character contents need to be equivalent to this string.
Steve Blocka7e24c12009-10-30 11:49:00 +00002388 * Returns true if the string has been changed to be an external string.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002389 * The string is not modified if the operation fails. See NewExternal for
2390 * information on the lifetime of the resource.
Steve Blocka7e24c12009-10-30 11:49:00 +00002391 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002392 bool MakeExternal(ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00002393
2394 /**
2395 * Returns true if this string can be made external.
2396 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002397 bool CanMakeExternal();
Steve Blocka7e24c12009-10-30 11:49:00 +00002398
2399 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002400 * Converts an object to a UTF-8-encoded character array. Useful if
Steve Blocka7e24c12009-10-30 11:49:00 +00002401 * you want to print the object. If conversion to a string fails
Ben Murdoch589d6972011-11-30 16:04:58 +00002402 * (e.g. due to an exception in the toString() method of the object)
Steve Blocka7e24c12009-10-30 11:49:00 +00002403 * then the length() method returns 0 and the * operator returns
2404 * NULL.
2405 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002406 class V8_EXPORT Utf8Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002407 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002408 explicit Utf8Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002409 ~Utf8Value();
2410 char* operator*() { return str_; }
2411 const char* operator*() const { return str_; }
2412 int length() const { return length_; }
2413 private:
2414 char* str_;
2415 int length_;
2416
2417 // Disallow copying and assigning.
2418 Utf8Value(const Utf8Value&);
2419 void operator=(const Utf8Value&);
2420 };
2421
2422 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002423 * Converts an object to a two-byte string.
2424 * If conversion to a string fails (eg. due to an exception in the toString()
2425 * method of the object) then the length() method returns 0 and the * operator
2426 * returns NULL.
2427 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002428 class V8_EXPORT Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002429 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002430 explicit Value(Local<v8::Value> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002431 ~Value();
2432 uint16_t* operator*() { return str_; }
2433 const uint16_t* operator*() const { return str_; }
2434 int length() const { return length_; }
2435 private:
2436 uint16_t* str_;
2437 int length_;
2438
2439 // Disallow copying and assigning.
2440 Value(const Value&);
2441 void operator=(const Value&);
2442 };
Steve Block3ce2e202009-11-05 08:53:23 +00002443
Steve Blocka7e24c12009-10-30 11:49:00 +00002444 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 void VerifyExternalStringResourceBase(ExternalStringResourceBase* v,
2446 Encoding encoding) const;
2447 void VerifyExternalStringResource(ExternalStringResource* val) const;
2448 static void CheckCast(v8::Value* obj);
2449};
2450
2451
2452/**
2453 * A JavaScript symbol (ECMA-262 edition 6)
2454 *
2455 * This is an experimental feature. Use at your own risk.
2456 */
2457class V8_EXPORT Symbol : public Name {
2458 public:
2459 // Returns the print name string of the symbol, or undefined if none.
2460 Local<Value> Name() const;
2461
2462 // Create a symbol. If name is not empty, it will be used as the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002463 static Local<Symbol> New(Isolate* isolate,
2464 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002465
2466 // Access global symbol registry.
2467 // Note that symbols created this way are never collected, so
2468 // they should only be used for statically fixed properties.
2469 // Also, there is only one global name space for the names used as keys.
2470 // To minimize the potential for clashes, use qualified names as keys.
2471 static Local<Symbol> For(Isolate *isolate, Local<String> name);
2472
2473 // Retrieve a global symbol. Similar to |For|, but using a separate
2474 // registry that is not accessible by (and cannot clash with) JavaScript code.
2475 static Local<Symbol> ForApi(Isolate *isolate, Local<String> name);
2476
2477 // Well-known symbols
2478 static Local<Symbol> GetIterator(Isolate* isolate);
2479 static Local<Symbol> GetUnscopables(Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002480 static Local<Symbol> GetToStringTag(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002481 static Local<Symbol> GetIsConcatSpreadable(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482
2483 V8_INLINE static Symbol* Cast(v8::Value* obj);
2484
2485 private:
2486 Symbol();
2487 static void CheckCast(v8::Value* obj);
2488};
2489
2490
2491/**
2492 * A private symbol
2493 *
2494 * This is an experimental feature. Use at your own risk.
2495 */
2496class V8_EXPORT Private : public Data {
2497 public:
2498 // Returns the print name string of the private symbol, or undefined if none.
2499 Local<Value> Name() const;
2500
2501 // Create a private symbol. If name is not empty, it will be the description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002502 static Local<Private> New(Isolate* isolate,
2503 Local<String> name = Local<String>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002504
2505 // Retrieve a global private symbol. If a symbol with this name has not
2506 // been retrieved in the same isolate before, it is created.
2507 // Note that private symbols created this way are never collected, so
2508 // they should only be used for statically fixed properties.
2509 // Also, there is only one global name space for the names used as keys.
2510 // To minimize the potential for clashes, use qualified names as keys,
2511 // e.g., "Class#property".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002512 static Local<Private> ForApi(Isolate* isolate, Local<String> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513
2514 private:
2515 Private();
Steve Blocka7e24c12009-10-30 11:49:00 +00002516};
2517
2518
2519/**
2520 * A JavaScript number value (ECMA-262, 4.3.20)
2521 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002522class V8_EXPORT Number : public Primitive {
Steve Blocka7e24c12009-10-30 11:49:00 +00002523 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002524 double Value() const;
2525 static Local<Number> New(Isolate* isolate, double value);
2526 V8_INLINE static Number* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002527 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 Number();
2529 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002530};
2531
2532
2533/**
2534 * A JavaScript value representing a signed integer.
2535 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002536class V8_EXPORT Integer : public Number {
Steve Blocka7e24c12009-10-30 11:49:00 +00002537 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002538 static Local<Integer> New(Isolate* isolate, int32_t value);
2539 static Local<Integer> NewFromUnsigned(Isolate* isolate, uint32_t value);
2540 int64_t Value() const;
2541 V8_INLINE static Integer* Cast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002542 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002543 Integer();
2544 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002545};
2546
2547
2548/**
2549 * A JavaScript value representing a 32-bit signed integer.
2550 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002551class V8_EXPORT Int32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002552 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 int32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002554 V8_INLINE static Int32* Cast(v8::Value* obj);
2555
Steve Blocka7e24c12009-10-30 11:49:00 +00002556 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002557 Int32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002558 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002559};
2560
2561
2562/**
2563 * A JavaScript value representing a 32-bit unsigned integer.
2564 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002565class V8_EXPORT Uint32 : public Integer {
Steve Blocka7e24c12009-10-30 11:49:00 +00002566 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567 uint32_t Value() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002568 V8_INLINE static Uint32* Cast(v8::Value* obj);
2569
Steve Blocka7e24c12009-10-30 11:49:00 +00002570 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571 Uint32();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002572 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002573};
2574
2575
Steve Blocka7e24c12009-10-30 11:49:00 +00002576enum PropertyAttribute {
2577 None = 0,
2578 ReadOnly = 1 << 0,
2579 DontEnum = 1 << 1,
2580 DontDelete = 1 << 2
2581};
2582
2583/**
Leon Clarkef7060e22010-06-03 12:02:55 +01002584 * Accessor[Getter|Setter] are used as callback functions when
2585 * setting|getting a particular property. See Object and ObjectTemplate's
2586 * method SetAccessor.
2587 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002588typedef void (*AccessorGetterCallback)(
2589 Local<String> property,
2590 const PropertyCallbackInfo<Value>& info);
2591typedef void (*AccessorNameGetterCallback)(
2592 Local<Name> property,
2593 const PropertyCallbackInfo<Value>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002594
2595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002596typedef void (*AccessorSetterCallback)(
2597 Local<String> property,
2598 Local<Value> value,
2599 const PropertyCallbackInfo<void>& info);
2600typedef void (*AccessorNameSetterCallback)(
2601 Local<Name> property,
2602 Local<Value> value,
2603 const PropertyCallbackInfo<void>& info);
Leon Clarkef7060e22010-06-03 12:02:55 +01002604
2605
2606/**
2607 * Access control specifications.
2608 *
2609 * Some accessors should be accessible across contexts. These
2610 * accessors have an explicit access control parameter which specifies
2611 * the kind of cross-context access that should be allowed.
2612 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002613 * TODO(dcarney): Remove PROHIBITS_OVERWRITING as it is now unused.
Leon Clarkef7060e22010-06-03 12:02:55 +01002614 */
2615enum AccessControl {
2616 DEFAULT = 0,
2617 ALL_CAN_READ = 1,
2618 ALL_CAN_WRITE = 1 << 1,
2619 PROHIBITS_OVERWRITING = 1 << 2
2620};
2621
Ben Murdochda12d292016-06-02 14:46:10 +01002622/**
Ben Murdochc5610432016-08-08 18:44:38 +01002623 * Property filter bits. They can be or'ed to build a composite filter.
2624 */
2625enum PropertyFilter {
2626 ALL_PROPERTIES = 0,
2627 ONLY_WRITABLE = 1,
2628 ONLY_ENUMERABLE = 2,
2629 ONLY_CONFIGURABLE = 4,
2630 SKIP_STRINGS = 8,
2631 SKIP_SYMBOLS = 16
2632};
2633
2634/**
Ben Murdoch61f157c2016-09-16 13:49:30 +01002635 * Keys/Properties filter enums:
2636 *
2637 * KeyCollectionMode limits the range of collected properties. kOwnOnly limits
2638 * the collected properties to the given Object only. kIncludesPrototypes will
2639 * include all keys of the objects's prototype chain as well.
2640 */
2641enum class KeyCollectionMode { kOwnOnly, kIncludePrototypes };
2642
2643/**
2644 * kIncludesIndices allows for integer indices to be collected, while
2645 * kSkipIndices will exclude integer indicies from being collected.
2646 */
2647enum class IndexFilter { kIncludeIndices, kSkipIndices };
2648
2649/**
Ben Murdochda12d292016-06-02 14:46:10 +01002650 * Integrity level for objects.
2651 */
2652enum class IntegrityLevel { kFrozen, kSealed };
Leon Clarkef7060e22010-06-03 12:02:55 +01002653
2654/**
Steve Blocka7e24c12009-10-30 11:49:00 +00002655 * A JavaScript object (ECMA-262, 4.3.3)
2656 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002657class V8_EXPORT Object : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00002658 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002659 V8_DEPRECATE_SOON("Use maybe version",
2660 bool Set(Local<Value> key, Local<Value> value));
2661 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
2662 Local<Value> key, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002663
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002664 V8_DEPRECATE_SOON("Use maybe version",
2665 bool Set(uint32_t index, Local<Value> value));
2666 V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
2667 Local<Value> value);
2668
2669 // Implements CreateDataProperty (ECMA-262, 7.3.4).
2670 //
2671 // Defines a configurable, writable, enumerable property with the given value
2672 // on the object unless the property already exists and is not configurable
2673 // or the object is not extensible.
2674 //
2675 // Returns true on success.
2676 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2677 Local<Name> key,
2678 Local<Value> value);
2679 V8_WARN_UNUSED_RESULT Maybe<bool> CreateDataProperty(Local<Context> context,
2680 uint32_t index,
2681 Local<Value> value);
2682
2683 // Implements DefineOwnProperty.
2684 //
2685 // In general, CreateDataProperty will be faster, however, does not allow
2686 // for specifying attributes.
2687 //
2688 // Returns true on success.
2689 V8_WARN_UNUSED_RESULT Maybe<bool> DefineOwnProperty(
2690 Local<Context> context, Local<Name> key, Local<Value> value,
2691 PropertyAttribute attributes = None);
Steve Block6ded16b2010-05-10 14:33:55 +01002692
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002693 // Sets an own property on this object bypassing interceptors and
Steve Blocka7e24c12009-10-30 11:49:00 +00002694 // overriding accessors or read-only properties.
2695 //
2696 // Note that if the object has an interceptor the property will be set
2697 // locally, but since the interceptor takes precedence the local property
2698 // will only be returned if the interceptor doesn't return a value.
2699 //
2700 // Note also that this only works for named properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002701 V8_DEPRECATED("Use CreateDataProperty / DefineOwnProperty",
2702 bool ForceSet(Local<Value> key, Local<Value> value,
2703 PropertyAttribute attribs = None));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002704 V8_DEPRECATE_SOON("Use CreateDataProperty / DefineOwnProperty",
2705 Maybe<bool> ForceSet(Local<Context> context,
2706 Local<Value> key, Local<Value> value,
2707 PropertyAttribute attribs = None));
Steve Blocka7e24c12009-10-30 11:49:00 +00002708
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002709 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
2710 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2711 Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002712
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002713 V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(uint32_t index));
2714 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
2715 uint32_t index);
Steve Block6ded16b2010-05-10 14:33:55 +01002716
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002717 /**
2718 * Gets the property attributes of a property which can be None or
2719 * any combination of ReadOnly, DontEnum and DontDelete. Returns
2720 * None when the property doesn't exist.
2721 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002722 V8_DEPRECATED("Use maybe version",
2723 PropertyAttribute GetPropertyAttributes(Local<Value> key));
2724 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetPropertyAttributes(
2725 Local<Context> context, Local<Value> key);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002726
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002727 /**
2728 * Returns Object.getOwnPropertyDescriptor as per ES5 section 15.2.3.3.
2729 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002730 V8_DEPRECATED("Use maybe version",
2731 Local<Value> GetOwnPropertyDescriptor(Local<String> key));
2732 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetOwnPropertyDescriptor(
2733 Local<Context> context, Local<String> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002734
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002735 V8_DEPRECATE_SOON("Use maybe version", bool Has(Local<Value> key));
2736 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
2737 Local<Value> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002738
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002739 V8_DEPRECATE_SOON("Use maybe version", bool Delete(Local<Value> key));
2740 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2741 Maybe<bool> Delete(Local<Context> context, Local<Value> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002742
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002743 V8_DEPRECATED("Use maybe version", bool Has(uint32_t index));
2744 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002745
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002746 V8_DEPRECATED("Use maybe version", bool Delete(uint32_t index));
2747 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2748 Maybe<bool> Delete(Local<Context> context, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002749
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002750 V8_DEPRECATED("Use maybe version",
2751 bool SetAccessor(Local<String> name,
2752 AccessorGetterCallback getter,
2753 AccessorSetterCallback setter = 0,
2754 Local<Value> data = Local<Value>(),
2755 AccessControl settings = DEFAULT,
2756 PropertyAttribute attribute = None));
2757 V8_DEPRECATED("Use maybe version",
2758 bool SetAccessor(Local<Name> name,
2759 AccessorNameGetterCallback getter,
2760 AccessorNameSetterCallback setter = 0,
2761 Local<Value> data = Local<Value>(),
2762 AccessControl settings = DEFAULT,
2763 PropertyAttribute attribute = None));
2764 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
2765 Maybe<bool> SetAccessor(Local<Context> context, Local<Name> name,
2766 AccessorNameGetterCallback getter,
2767 AccessorNameSetterCallback setter = 0,
2768 MaybeLocal<Value> data = MaybeLocal<Value>(),
2769 AccessControl settings = DEFAULT,
2770 PropertyAttribute attribute = None);
Steve Blocka7e24c12009-10-30 11:49:00 +00002771
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002772 void SetAccessorProperty(Local<Name> name, Local<Function> getter,
2773 Local<Function> setter = Local<Function>(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002774 PropertyAttribute attribute = None,
2775 AccessControl settings = DEFAULT);
2776
2777 /**
2778 * Functionality for private properties.
2779 * This is an experimental feature, use at your own risk.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002780 * Note: Private properties are not inherited. Do not rely on this, since it
2781 * may change.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002782 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002783 Maybe<bool> HasPrivate(Local<Context> context, Local<Private> key);
2784 Maybe<bool> SetPrivate(Local<Context> context, Local<Private> key,
2785 Local<Value> value);
2786 Maybe<bool> DeletePrivate(Local<Context> context, Local<Private> key);
2787 MaybeLocal<Value> GetPrivate(Local<Context> context, Local<Private> key);
Leon Clarkef7060e22010-06-03 12:02:55 +01002788
Steve Blocka7e24c12009-10-30 11:49:00 +00002789 /**
2790 * Returns an array containing the names of the enumerable properties
2791 * of this object, including properties from prototype objects. The
2792 * array returned by this method contains the same values as would
2793 * be enumerated by a for-in statement over this object.
2794 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002795 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetPropertyNames());
2796 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
2797 Local<Context> context);
Ben Murdoch61f157c2016-09-16 13:49:30 +01002798 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetPropertyNames(
2799 Local<Context> context, KeyCollectionMode mode,
2800 PropertyFilter property_filter, IndexFilter index_filter);
Steve Blocka7e24c12009-10-30 11:49:00 +00002801
2802 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002803 * This function has the same functionality as GetPropertyNames but
2804 * the returned array doesn't contain the names of properties from
2805 * prototype objects.
2806 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002807 V8_DEPRECATE_SOON("Use maybe version", Local<Array> GetOwnPropertyNames());
2808 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2809 Local<Context> context);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002810
2811 /**
Ben Murdochc5610432016-08-08 18:44:38 +01002812 * Returns an array containing the names of the filtered properties
2813 * of this object, including properties from prototype objects. The
2814 * array returned by this method contains the same values as would
2815 * be enumerated by a for-in statement over this object.
2816 */
2817 V8_WARN_UNUSED_RESULT MaybeLocal<Array> GetOwnPropertyNames(
2818 Local<Context> context, PropertyFilter filter);
2819
2820 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002821 * Get the prototype object. This does not skip objects marked to
2822 * be skipped by __proto__ and it does not consult the security
2823 * handler.
2824 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002825 Local<Value> GetPrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00002826
2827 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00002828 * Set the prototype object. This does not skip objects marked to
2829 * be skipped by __proto__ and it does not consult the security
2830 * handler.
2831 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002832 V8_DEPRECATED("Use maybe version", bool SetPrototype(Local<Value> prototype));
2833 V8_WARN_UNUSED_RESULT Maybe<bool> SetPrototype(Local<Context> context,
2834 Local<Value> prototype);
Andrei Popescu402d9372010-02-26 13:31:12 +00002835
2836 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002837 * Finds an instance of the given function template in the prototype
2838 * chain.
2839 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002840 Local<Object> FindInstanceInPrototypeChain(Local<FunctionTemplate> tmpl);
Steve Blocka7e24c12009-10-30 11:49:00 +00002841
2842 /**
2843 * Call builtin Object.prototype.toString on this object.
2844 * This is different from Value::ToString() that may call
2845 * user-defined toString function. This one does not.
2846 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002847 V8_DEPRECATED("Use maybe version", Local<String> ObjectProtoToString());
2848 V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString(
2849 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00002850
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002851 /**
2852 * Returns the name of the function invoked as a constructor for this object.
2853 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002854 Local<String> GetConstructorName();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002855
Ben Murdochda12d292016-06-02 14:46:10 +01002856 /**
2857 * Sets the integrity level of the object.
2858 */
2859 Maybe<bool> SetIntegrityLevel(Local<Context> context, IntegrityLevel level);
2860
Steve Blocka7e24c12009-10-30 11:49:00 +00002861 /** Gets the number of internal fields for this Object. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002862 int InternalFieldCount();
2863
2864 /** Same as above, but works for Persistents */
2865 V8_INLINE static int InternalFieldCount(
2866 const PersistentBase<Object>& object) {
2867 return object.val_->InternalFieldCount();
2868 }
2869
2870 /** Gets the value from an internal field. */
2871 V8_INLINE Local<Value> GetInternalField(int index);
2872
Steve Blocka7e24c12009-10-30 11:49:00 +00002873 /** Sets the value in an internal field. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002874 void SetInternalField(int index, Local<Value> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002875
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002876 /**
2877 * Gets a 2-byte-aligned native pointer from an internal field. This field
2878 * must have been set by SetAlignedPointerInInternalField, everything else
2879 * leads to undefined behavior.
2880 */
2881 V8_INLINE void* GetAlignedPointerFromInternalField(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00002882
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002883 /** Same as above, but works for Persistents */
2884 V8_INLINE static void* GetAlignedPointerFromInternalField(
2885 const PersistentBase<Object>& object, int index) {
2886 return object.val_->GetAlignedPointerFromInternalField(index);
2887 }
2888
2889 /**
2890 * Sets a 2-byte-aligned native pointer in an internal field. To retrieve such
2891 * a field, GetAlignedPointerFromInternalField must be used, everything else
2892 * leads to undefined behavior.
2893 */
2894 void SetAlignedPointerInInternalField(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002895
2896 // Testers for local properties.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002897 V8_DEPRECATED("Use maybe version", bool HasOwnProperty(Local<String> key));
2898 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2899 Local<Name> key);
Ben Murdochc5610432016-08-08 18:44:38 +01002900 V8_WARN_UNUSED_RESULT Maybe<bool> HasOwnProperty(Local<Context> context,
2901 uint32_t index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002902 V8_DEPRECATE_SOON("Use maybe version",
2903 bool HasRealNamedProperty(Local<String> key));
2904 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedProperty(Local<Context> context,
2905 Local<Name> key);
2906 V8_DEPRECATE_SOON("Use maybe version",
2907 bool HasRealIndexedProperty(uint32_t index));
2908 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealIndexedProperty(
2909 Local<Context> context, uint32_t index);
2910 V8_DEPRECATE_SOON("Use maybe version",
2911 bool HasRealNamedCallbackProperty(Local<String> key));
2912 V8_WARN_UNUSED_RESULT Maybe<bool> HasRealNamedCallbackProperty(
2913 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002914
2915 /**
2916 * If result.IsEmpty() no real property was located in the prototype chain.
2917 * This means interceptors in the prototype chain are not called.
2918 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002919 V8_DEPRECATED(
2920 "Use maybe version",
2921 Local<Value> GetRealNamedPropertyInPrototypeChain(Local<String> key));
2922 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedPropertyInPrototypeChain(
2923 Local<Context> context, Local<Name> key);
2924
2925 /**
2926 * Gets the property attributes of a real property in the prototype chain,
2927 * which can be None or any combination of ReadOnly, DontEnum and DontDelete.
2928 * Interceptors in the prototype chain are not called.
2929 */
2930 V8_DEPRECATED(
2931 "Use maybe version",
2932 Maybe<PropertyAttribute> GetRealNamedPropertyAttributesInPrototypeChain(
2933 Local<String> key));
2934 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute>
2935 GetRealNamedPropertyAttributesInPrototypeChain(Local<Context> context,
2936 Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002937
2938 /**
2939 * If result.IsEmpty() no real property was located on the object or
2940 * in the prototype chain.
2941 * This means interceptors in the prototype chain are not called.
2942 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002943 V8_DEPRECATED("Use maybe version",
2944 Local<Value> GetRealNamedProperty(Local<String> key));
2945 V8_WARN_UNUSED_RESULT MaybeLocal<Value> GetRealNamedProperty(
2946 Local<Context> context, Local<Name> key);
2947
2948 /**
2949 * Gets the property attributes of a real property which can be
2950 * None or any combination of ReadOnly, DontEnum and DontDelete.
2951 * Interceptors in the prototype chain are not called.
2952 */
2953 V8_DEPRECATED("Use maybe version",
2954 Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2955 Local<String> key));
2956 V8_WARN_UNUSED_RESULT Maybe<PropertyAttribute> GetRealNamedPropertyAttributes(
2957 Local<Context> context, Local<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002958
2959 /** Tests for a named lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002960 bool HasNamedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002961
2962 /** Tests for an index lookup interceptor.*/
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002963 bool HasIndexedLookupInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002964
2965 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002966 * Returns the identity hash for this object. The current implementation
2967 * uses a hidden property on the object to store the identity hash.
Steve Blocka7e24c12009-10-30 11:49:00 +00002968 *
2969 * The return value will never be 0. Also, it is not guaranteed to be
2970 * unique.
2971 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002972 int GetIdentityHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00002973
Steve Blocka7e24c12009-10-30 11:49:00 +00002974 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00002975 * Clone this object with a fast but shallow copy. Values will point
2976 * to the same values as the original object.
2977 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002978 // TODO(dcarney): take an isolate and optionally bail out?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002979 Local<Object> Clone();
Steve Blocka7e24c12009-10-30 11:49:00 +00002980
2981 /**
Ben Murdoch8b112d22011-06-08 16:22:53 +01002982 * Returns the context in which the object was created.
2983 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002984 Local<Context> CreationContext();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002985
2986 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00002987 * Checks whether a callback is set by the
2988 * ObjectTemplate::SetCallAsFunctionHandler method.
2989 * When an Object is callable this method returns true.
2990 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002991 bool IsCallable();
Ben Murdoch257744e2011-11-30 15:57:28 +00002992
2993 /**
Ben Murdochc5610432016-08-08 18:44:38 +01002994 * True if this object is a constructor.
2995 */
2996 bool IsConstructor();
2997
2998 /**
Ben Murdoch589d6972011-11-30 16:04:58 +00002999 * Call an Object as a function if a callback is set by the
Ben Murdoch257744e2011-11-30 15:57:28 +00003000 * ObjectTemplate::SetCallAsFunctionHandler method.
3001 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003002 V8_DEPRECATED("Use maybe version",
3003 Local<Value> CallAsFunction(Local<Value> recv, int argc,
3004 Local<Value> argv[]));
3005 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsFunction(Local<Context> context,
3006 Local<Value> recv,
3007 int argc,
3008 Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00003009
3010 /**
3011 * Call an Object as a constructor if a callback is set by the
3012 * ObjectTemplate::SetCallAsFunctionHandler method.
3013 * Note: This method behaves like the Function::NewInstance method.
3014 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003015 V8_DEPRECATED("Use maybe version",
3016 Local<Value> CallAsConstructor(int argc, Local<Value> argv[]));
3017 V8_WARN_UNUSED_RESULT MaybeLocal<Value> CallAsConstructor(
3018 Local<Context> context, int argc, Local<Value> argv[]);
Ben Murdoch257744e2011-11-30 15:57:28 +00003019
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003020 /**
3021 * Return the isolate to which the Object belongs to.
3022 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003023 V8_DEPRECATE_SOON("Keep track of isolate correctly", Isolate* GetIsolate());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003025 static Local<Object> New(Isolate* isolate);
3026
3027 V8_INLINE static Object* Cast(Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003028
Steve Blocka7e24c12009-10-30 11:49:00 +00003029 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003030 Object();
3031 static void CheckCast(Value* obj);
3032 Local<Value> SlowGetInternalField(int index);
3033 void* SlowGetAlignedPointerFromInternalField(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003034};
3035
3036
3037/**
3038 * An instance of the built-in array constructor (ECMA-262, 15.4.2).
3039 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040class V8_EXPORT Array : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003041 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003042 uint32_t Length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00003043
3044 /**
3045 * Clones an element at index |index|. Returns an empty
3046 * handle if cloning fails (for any reason).
3047 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003048 V8_DEPRECATED("Cloning is not supported.",
3049 Local<Object> CloneElementAt(uint32_t index));
3050 V8_DEPRECATED("Cloning is not supported.",
3051 MaybeLocal<Object> CloneElementAt(Local<Context> context,
3052 uint32_t index));
Steve Blocka7e24c12009-10-30 11:49:00 +00003053
Steve Block44f0eee2011-05-26 01:26:41 +01003054 /**
3055 * Creates a JavaScript array with the given length. If the length
3056 * is negative the returned array will have length 0.
3057 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003058 static Local<Array> New(Isolate* isolate, int length = 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003059
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003060 V8_INLINE static Array* Cast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003061 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003062 Array();
3063 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003064};
3065
3066
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003067/**
3068 * An instance of the built-in Map constructor (ECMA-262, 6th Edition, 23.1.1).
3069 */
3070class V8_EXPORT Map : public Object {
3071 public:
3072 size_t Size() const;
3073 void Clear();
3074 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Get(Local<Context> context,
3075 Local<Value> key);
3076 V8_WARN_UNUSED_RESULT MaybeLocal<Map> Set(Local<Context> context,
3077 Local<Value> key,
3078 Local<Value> value);
3079 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3080 Local<Value> key);
3081 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3082 Local<Value> key);
3083
3084 /**
3085 * Returns an array of length Size() * 2, where index N is the Nth key and
3086 * index N + 1 is the Nth value.
3087 */
3088 Local<Array> AsArray() const;
3089
3090 /**
3091 * Creates a new empty Map.
3092 */
3093 static Local<Map> New(Isolate* isolate);
3094
3095 V8_INLINE static Map* Cast(Value* obj);
3096
3097 private:
3098 Map();
3099 static void CheckCast(Value* obj);
3100};
3101
3102
3103/**
3104 * An instance of the built-in Set constructor (ECMA-262, 6th Edition, 23.2.1).
3105 */
3106class V8_EXPORT Set : public Object {
3107 public:
3108 size_t Size() const;
3109 void Clear();
3110 V8_WARN_UNUSED_RESULT MaybeLocal<Set> Add(Local<Context> context,
3111 Local<Value> key);
3112 V8_WARN_UNUSED_RESULT Maybe<bool> Has(Local<Context> context,
3113 Local<Value> key);
3114 V8_WARN_UNUSED_RESULT Maybe<bool> Delete(Local<Context> context,
3115 Local<Value> key);
3116
3117 /**
3118 * Returns an array of the keys in this Set.
3119 */
3120 Local<Array> AsArray() const;
3121
3122 /**
3123 * Creates a new empty Set.
3124 */
3125 static Local<Set> New(Isolate* isolate);
3126
3127 V8_INLINE static Set* Cast(Value* obj);
3128
3129 private:
3130 Set();
3131 static void CheckCast(Value* obj);
3132};
3133
3134
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003135template<typename T>
3136class ReturnValue {
3137 public:
3138 template <class S> V8_INLINE ReturnValue(const ReturnValue<S>& that)
3139 : value_(that.value_) {
3140 TYPE_CHECK(T, S);
3141 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003142 // Local setters
3143 template <typename S>
3144 V8_INLINE V8_DEPRECATE_SOON("Use Global<> instead",
3145 void Set(const Persistent<S>& handle));
3146 template <typename S>
3147 V8_INLINE void Set(const Global<S>& handle);
3148 template <typename S>
3149 V8_INLINE void Set(const Local<S> handle);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003150 // Fast primitive setters
3151 V8_INLINE void Set(bool value);
3152 V8_INLINE void Set(double i);
3153 V8_INLINE void Set(int32_t i);
3154 V8_INLINE void Set(uint32_t i);
3155 // Fast JS primitive setters
3156 V8_INLINE void SetNull();
3157 V8_INLINE void SetUndefined();
3158 V8_INLINE void SetEmptyString();
3159 // Convenience getter for Isolate
Ben Murdochda12d292016-06-02 14:46:10 +01003160 V8_INLINE Isolate* GetIsolate() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003161
3162 // Pointer setter: Uncompilable to prevent inadvertent misuse.
3163 template <typename S>
3164 V8_INLINE void Set(S* whatever);
3165
Ben Murdochda12d292016-06-02 14:46:10 +01003166 // Getter. Creates a new Local<> so it comes with a certain performance
3167 // hit. If the ReturnValue was not yet set, this will return the undefined
3168 // value.
3169 V8_INLINE Local<Value> Get() const;
3170
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003171 private:
3172 template<class F> friend class ReturnValue;
3173 template<class F> friend class FunctionCallbackInfo;
3174 template<class F> friend class PropertyCallbackInfo;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003175 template <class F, class G, class H>
3176 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003177 V8_INLINE void SetInternal(internal::Object* value) { *value_ = value; }
3178 V8_INLINE internal::Object* GetDefaultValue();
3179 V8_INLINE explicit ReturnValue(internal::Object** slot);
3180 internal::Object** value_;
3181};
3182
3183
3184/**
3185 * The argument information given to function call callbacks. This
3186 * class provides access to information about the context of the call,
3187 * including the receiver, the number and values of arguments, and
3188 * the holder of the function.
3189 */
3190template<typename T>
3191class FunctionCallbackInfo {
3192 public:
3193 V8_INLINE int Length() const;
3194 V8_INLINE Local<Value> operator[](int i) const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003195 V8_INLINE V8_DEPRECATED("Use Data() to explicitly pass Callee instead",
3196 Local<Function> Callee() const);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003197 V8_INLINE Local<Object> This() const;
3198 V8_INLINE Local<Object> Holder() const;
Ben Murdochc5610432016-08-08 18:44:38 +01003199 V8_INLINE Local<Value> NewTarget() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003200 V8_INLINE bool IsConstructCall() const;
3201 V8_INLINE Local<Value> Data() const;
3202 V8_INLINE Isolate* GetIsolate() const;
3203 V8_INLINE ReturnValue<T> GetReturnValue() const;
3204 // This shouldn't be public, but the arm compiler needs it.
Ben Murdochc5610432016-08-08 18:44:38 +01003205 static const int kArgsLength = 8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003206
3207 protected:
3208 friend class internal::FunctionCallbackArguments;
3209 friend class internal::CustomArguments<FunctionCallbackInfo>;
3210 static const int kHolderIndex = 0;
3211 static const int kIsolateIndex = 1;
3212 static const int kReturnValueDefaultValueIndex = 2;
3213 static const int kReturnValueIndex = 3;
3214 static const int kDataIndex = 4;
3215 static const int kCalleeIndex = 5;
3216 static const int kContextSaveIndex = 6;
Ben Murdochc5610432016-08-08 18:44:38 +01003217 static const int kNewTargetIndex = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003218
3219 V8_INLINE FunctionCallbackInfo(internal::Object** implicit_args,
Ben Murdochc5610432016-08-08 18:44:38 +01003220 internal::Object** values, int length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003221 internal::Object** implicit_args_;
3222 internal::Object** values_;
3223 int length_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003224};
3225
3226
3227/**
3228 * The information passed to a property callback about the context
3229 * of the property access.
3230 */
3231template<typename T>
3232class PropertyCallbackInfo {
3233 public:
3234 V8_INLINE Isolate* GetIsolate() const;
3235 V8_INLINE Local<Value> Data() const;
3236 V8_INLINE Local<Object> This() const;
3237 V8_INLINE Local<Object> Holder() const;
3238 V8_INLINE ReturnValue<T> GetReturnValue() const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003239 V8_INLINE bool ShouldThrowOnError() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003240 // This shouldn't be public, but the arm compiler needs it.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003241 static const int kArgsLength = 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003242
3243 protected:
3244 friend class MacroAssembler;
3245 friend class internal::PropertyCallbackArguments;
3246 friend class internal::CustomArguments<PropertyCallbackInfo>;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003247 static const int kShouldThrowOnErrorIndex = 0;
3248 static const int kHolderIndex = 1;
3249 static const int kIsolateIndex = 2;
3250 static const int kReturnValueDefaultValueIndex = 3;
3251 static const int kReturnValueIndex = 4;
3252 static const int kDataIndex = 5;
3253 static const int kThisIndex = 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003254
3255 V8_INLINE PropertyCallbackInfo(internal::Object** args) : args_(args) {}
3256 internal::Object** args_;
3257};
3258
3259
3260typedef void (*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
3261
Ben Murdochc5610432016-08-08 18:44:38 +01003262enum class ConstructorBehavior { kThrow, kAllow };
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003263
Steve Blocka7e24c12009-10-30 11:49:00 +00003264/**
3265 * A JavaScript function object (ECMA-262, 15.3).
3266 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003267class V8_EXPORT Function : public Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00003268 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003269 /**
3270 * Create a function in the current execution context
3271 * for a given FunctionCallback.
3272 */
Ben Murdochc5610432016-08-08 18:44:38 +01003273 static MaybeLocal<Function> New(
3274 Local<Context> context, FunctionCallback callback,
3275 Local<Value> data = Local<Value>(), int length = 0,
3276 ConstructorBehavior behavior = ConstructorBehavior::kAllow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003277 static V8_DEPRECATE_SOON(
3278 "Use maybe version",
3279 Local<Function> New(Isolate* isolate, FunctionCallback callback,
3280 Local<Value> data = Local<Value>(), int length = 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003281
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003282 V8_DEPRECATED("Use maybe version",
3283 Local<Object> NewInstance(int argc, Local<Value> argv[]) const);
3284 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3285 Local<Context> context, int argc, Local<Value> argv[]) const;
3286
3287 V8_DEPRECATED("Use maybe version", Local<Object> NewInstance() const);
3288 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(
3289 Local<Context> context) const {
3290 return NewInstance(context, 0, nullptr);
3291 }
3292
3293 V8_DEPRECATE_SOON("Use maybe version",
3294 Local<Value> Call(Local<Value> recv, int argc,
3295 Local<Value> argv[]));
3296 V8_WARN_UNUSED_RESULT MaybeLocal<Value> Call(Local<Context> context,
3297 Local<Value> recv, int argc,
3298 Local<Value> argv[]);
3299
3300 void SetName(Local<String> name);
3301 Local<Value> GetName() const;
Andrei Popescu402d9372010-02-26 13:31:12 +00003302
3303 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003304 * Name inferred from variable or property assignment of this function.
3305 * Used to facilitate debugging and profiling of JavaScript code written
3306 * in an OO style, where many functions are anonymous but are assigned
3307 * to object properties.
3308 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003309 Local<Value> GetInferredName() const;
3310
3311 /**
3312 * displayName if it is set, otherwise name if it is configured, otherwise
3313 * function name, otherwise inferred name.
3314 */
3315 Local<Value> GetDebugName() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003316
3317 /**
3318 * User-defined name assigned to the "displayName" property of this function.
3319 * Used to facilitate debugging and profiling of JavaScript code.
3320 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003321 Local<Value> GetDisplayName() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003322
3323 /**
Andrei Popescu402d9372010-02-26 13:31:12 +00003324 * Returns zero based line number of function body and
3325 * kLineOffsetNotFound if no information available.
3326 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003327 int GetScriptLineNumber() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003328 /**
3329 * Returns zero based column number of function body and
3330 * kLineOffsetNotFound if no information available.
3331 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003332 int GetScriptColumnNumber() const;
3333
3334 /**
3335 * Tells whether this function is builtin.
3336 */
3337 bool IsBuiltin() const;
3338
3339 /**
3340 * Returns scriptId.
3341 */
3342 int ScriptId() const;
3343
3344 /**
3345 * Returns the original function if this function is bound, else returns
3346 * v8::Undefined.
3347 */
3348 Local<Value> GetBoundFunction() const;
3349
3350 ScriptOrigin GetScriptOrigin() const;
3351 V8_INLINE static Function* Cast(Value* obj);
3352 static const int kLineOffsetNotFound;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003353
Steve Blocka7e24c12009-10-30 11:49:00 +00003354 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003355 Function();
3356 static void CheckCast(Value* obj);
3357};
3358
3359
3360/**
3361 * An instance of the built-in Promise constructor (ES6 draft).
3362 * This API is experimental. Only works with --harmony flag.
3363 */
3364class V8_EXPORT Promise : public Object {
3365 public:
3366 class V8_EXPORT Resolver : public Object {
3367 public:
3368 /**
3369 * Create a new resolver, along with an associated promise in pending state.
3370 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003371 static V8_DEPRECATE_SOON("Use maybe version",
3372 Local<Resolver> New(Isolate* isolate));
3373 static V8_WARN_UNUSED_RESULT MaybeLocal<Resolver> New(
3374 Local<Context> context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003375
3376 /**
3377 * Extract the associated promise.
3378 */
3379 Local<Promise> GetPromise();
3380
3381 /**
3382 * Resolve/reject the associated promise with a given value.
3383 * Ignored if the promise is no longer pending.
3384 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003385 V8_DEPRECATE_SOON("Use maybe version", void Resolve(Local<Value> value));
3386 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3387 Maybe<bool> Resolve(Local<Context> context, Local<Value> value);
3388
3389 V8_DEPRECATE_SOON("Use maybe version", void Reject(Local<Value> value));
3390 // TODO(dcarney): mark V8_WARN_UNUSED_RESULT
3391 Maybe<bool> Reject(Local<Context> context, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003392
3393 V8_INLINE static Resolver* Cast(Value* obj);
3394
3395 private:
3396 Resolver();
3397 static void CheckCast(Value* obj);
3398 };
3399
3400 /**
3401 * Register a resolution/rejection handler with a promise.
3402 * The handler is given the respective resolution/rejection value as
3403 * an argument. If the promise is already resolved/rejected, the handler is
3404 * invoked at the end of turn.
3405 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003406 V8_DEPRECATED("Use maybe version of Then",
3407 Local<Promise> Chain(Local<Function> handler));
3408 V8_DEPRECATED("Use Then",
3409 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Chain(
3410 Local<Context> context, Local<Function> handler));
3411
3412 V8_DEPRECATED("Use maybe version",
3413 Local<Promise> Catch(Local<Function> handler));
3414 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Catch(Local<Context> context,
3415 Local<Function> handler);
3416
3417 V8_DEPRECATED("Use maybe version",
3418 Local<Promise> Then(Local<Function> handler));
3419 V8_WARN_UNUSED_RESULT MaybeLocal<Promise> Then(Local<Context> context,
3420 Local<Function> handler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003421
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003422 /**
3423 * Returns true if the promise has at least one derived promise, and
3424 * therefore resolve/reject handlers (including default handler).
3425 */
3426 bool HasHandler();
3427
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003428 V8_INLINE static Promise* Cast(Value* obj);
3429
3430 private:
3431 Promise();
3432 static void CheckCast(Value* obj);
3433};
3434
3435
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003436/**
3437 * An instance of the built-in Proxy constructor (ECMA-262, 6th Edition,
3438 * 26.2.1).
3439 */
3440class V8_EXPORT Proxy : public Object {
3441 public:
3442 Local<Object> GetTarget();
3443 Local<Value> GetHandler();
3444 bool IsRevoked();
3445 void Revoke();
3446
3447 /**
3448 * Creates a new empty Map.
3449 */
3450 static MaybeLocal<Proxy> New(Local<Context> context,
3451 Local<Object> local_target,
3452 Local<Object> local_handler);
3453
3454 V8_INLINE static Proxy* Cast(Value* obj);
3455
3456 private:
3457 Proxy();
3458 static void CheckCast(Value* obj);
3459};
3460
3461
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003462#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT
3463// The number of required internal fields can be defined by embedder.
3464#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2
3465#endif
3466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003467
3468enum class ArrayBufferCreationMode { kInternalized, kExternalized };
3469
3470
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003471/**
3472 * An instance of the built-in ArrayBuffer constructor (ES6 draft 15.13.5).
3473 * This API is experimental and may change significantly.
3474 */
3475class V8_EXPORT ArrayBuffer : public Object {
3476 public:
3477 /**
Ben Murdochc5610432016-08-08 18:44:38 +01003478 * A thread-safe allocator that V8 uses to allocate |ArrayBuffer|'s memory.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003479 * The allocator is a global V8 setting. It has to be set via
3480 * Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003481 *
Ben Murdochc5610432016-08-08 18:44:38 +01003482 * Memory allocated through this allocator by V8 is accounted for as external
3483 * memory by V8. Note that V8 keeps track of the memory for all internalized
3484 * |ArrayBuffer|s. Responsibility for tracking external memory (using
3485 * Isolate::AdjustAmountOfExternalAllocatedMemory) is handed over to the
3486 * embedder upon externalization and taken over upon internalization (creating
3487 * an internalized buffer from an existing buffer).
3488 *
3489 * Note that it is unsafe to call back into V8 from any of the allocator
3490 * functions.
3491 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003492 * This API is experimental and may change significantly.
3493 */
3494 class V8_EXPORT Allocator { // NOLINT
3495 public:
3496 virtual ~Allocator() {}
3497
3498 /**
3499 * Allocate |length| bytes. Return NULL if allocation is not successful.
3500 * Memory should be initialized to zeroes.
3501 */
3502 virtual void* Allocate(size_t length) = 0;
3503
3504 /**
3505 * Allocate |length| bytes. Return NULL if allocation is not successful.
3506 * Memory does not have to be initialized.
3507 */
3508 virtual void* AllocateUninitialized(size_t length) = 0;
3509 /**
3510 * Free the memory block of size |length|, pointed to by |data|.
3511 * That memory is guaranteed to be previously allocated by |Allocate|.
3512 */
3513 virtual void Free(void* data, size_t length) = 0;
3514 };
3515
3516 /**
3517 * The contents of an |ArrayBuffer|. Externalization of |ArrayBuffer|
3518 * returns an instance of this class, populated, with a pointer to data
3519 * and byte length.
3520 *
3521 * The Data pointer of ArrayBuffer::Contents is always allocated with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003522 * Allocator::Allocate that is set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003523 *
3524 * This API is experimental and may change significantly.
3525 */
3526 class V8_EXPORT Contents { // NOLINT
3527 public:
3528 Contents() : data_(NULL), byte_length_(0) {}
3529
3530 void* Data() const { return data_; }
3531 size_t ByteLength() const { return byte_length_; }
3532
3533 private:
3534 void* data_;
3535 size_t byte_length_;
3536
3537 friend class ArrayBuffer;
3538 };
3539
3540
3541 /**
3542 * Data length in bytes.
3543 */
3544 size_t ByteLength() const;
3545
3546 /**
3547 * Create a new ArrayBuffer. Allocate |byte_length| bytes.
3548 * Allocated memory will be owned by a created ArrayBuffer and
3549 * will be deallocated when it is garbage-collected,
3550 * unless the object is externalized.
3551 */
3552 static Local<ArrayBuffer> New(Isolate* isolate, size_t byte_length);
3553
3554 /**
3555 * Create a new ArrayBuffer over an existing memory block.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003556 * The created array buffer is by default immediately in externalized state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003557 * The memory block will not be reclaimed when a created ArrayBuffer
3558 * is garbage-collected.
3559 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003560 static Local<ArrayBuffer> New(
3561 Isolate* isolate, void* data, size_t byte_length,
3562 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003563
3564 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003565 * Returns true if ArrayBuffer is externalized, that is, does not
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003566 * own its memory block.
3567 */
3568 bool IsExternal() const;
3569
3570 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003571 * Returns true if this ArrayBuffer may be neutered.
3572 */
3573 bool IsNeuterable() const;
3574
3575 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003576 * Neuters this ArrayBuffer and all its views (typed arrays).
3577 * Neutering sets the byte length of the buffer and all typed arrays to zero,
3578 * preventing JavaScript from ever accessing underlying backing store.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003579 * ArrayBuffer should have been externalized and must be neuterable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003580 */
3581 void Neuter();
3582
3583 /**
3584 * Make this ArrayBuffer external. The pointer to underlying memory block
3585 * and byte length are returned as |Contents| structure. After ArrayBuffer
3586 * had been etxrenalized, it does no longer owns the memory block. The caller
3587 * should take steps to free memory when it is no longer needed.
3588 *
3589 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003590 * that has been set via Isolate::CreateParams.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003591 */
3592 Contents Externalize();
3593
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003594 /**
3595 * Get a pointer to the ArrayBuffer's underlying memory block without
3596 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3597 * will become invalid as soon as the ArrayBuffer became garbage collected.
3598 *
3599 * The embedder should make sure to hold a strong reference to the
3600 * ArrayBuffer while accessing this pointer.
3601 *
3602 * The memory block is guaranteed to be allocated with |Allocator::Allocate|.
3603 */
3604 Contents GetContents();
3605
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003606 V8_INLINE static ArrayBuffer* Cast(Value* obj);
3607
3608 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3609
3610 private:
3611 ArrayBuffer();
3612 static void CheckCast(Value* obj);
3613};
3614
3615
3616#ifndef V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT
3617// The number of required internal fields can be defined by embedder.
3618#define V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT 2
3619#endif
3620
3621
3622/**
3623 * A base class for an instance of one of "views" over ArrayBuffer,
3624 * including TypedArrays and DataView (ES6 draft 15.13).
3625 *
3626 * This API is experimental and may change significantly.
3627 */
3628class V8_EXPORT ArrayBufferView : public Object {
3629 public:
3630 /**
3631 * Returns underlying ArrayBuffer.
3632 */
3633 Local<ArrayBuffer> Buffer();
3634 /**
3635 * Byte offset in |Buffer|.
3636 */
3637 size_t ByteOffset();
3638 /**
3639 * Size of a view in bytes.
3640 */
3641 size_t ByteLength();
3642
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003643 /**
3644 * Copy the contents of the ArrayBufferView's buffer to an embedder defined
3645 * memory without additional overhead that calling ArrayBufferView::Buffer
3646 * might incur.
3647 *
3648 * Will write at most min(|byte_length|, ByteLength) bytes starting at
3649 * ByteOffset of the underling buffer to the memory starting at |dest|.
3650 * Returns the number of bytes actually written.
3651 */
3652 size_t CopyContents(void* dest, size_t byte_length);
3653
3654 /**
3655 * Returns true if ArrayBufferView's backing ArrayBuffer has already been
3656 * allocated.
3657 */
3658 bool HasBuffer() const;
3659
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003660 V8_INLINE static ArrayBufferView* Cast(Value* obj);
3661
3662 static const int kInternalFieldCount =
3663 V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT;
3664
3665 private:
3666 ArrayBufferView();
3667 static void CheckCast(Value* obj);
3668};
3669
3670
3671/**
3672 * A base class for an instance of TypedArray series of constructors
3673 * (ES6 draft 15.13.6).
3674 * This API is experimental and may change significantly.
3675 */
3676class V8_EXPORT TypedArray : public ArrayBufferView {
3677 public:
3678 /**
3679 * Number of elements in this typed array
3680 * (e.g. for Int16Array, |ByteLength|/2).
3681 */
3682 size_t Length();
3683
3684 V8_INLINE static TypedArray* Cast(Value* obj);
3685
3686 private:
3687 TypedArray();
3688 static void CheckCast(Value* obj);
3689};
3690
3691
3692/**
3693 * An instance of Uint8Array constructor (ES6 draft 15.13.6).
3694 * This API is experimental and may change significantly.
3695 */
3696class V8_EXPORT Uint8Array : public TypedArray {
3697 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003698 static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
3699 size_t byte_offset, size_t length);
3700 static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003701 size_t byte_offset, size_t length);
3702 V8_INLINE static Uint8Array* Cast(Value* obj);
3703
3704 private:
3705 Uint8Array();
3706 static void CheckCast(Value* obj);
3707};
3708
3709
3710/**
3711 * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
3712 * This API is experimental and may change significantly.
3713 */
3714class V8_EXPORT Uint8ClampedArray : public TypedArray {
3715 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003716 static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
3717 size_t byte_offset, size_t length);
3718 static Local<Uint8ClampedArray> New(
3719 Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
3720 size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003721 V8_INLINE static Uint8ClampedArray* Cast(Value* obj);
3722
3723 private:
3724 Uint8ClampedArray();
3725 static void CheckCast(Value* obj);
3726};
3727
3728/**
3729 * An instance of Int8Array constructor (ES6 draft 15.13.6).
3730 * This API is experimental and may change significantly.
3731 */
3732class V8_EXPORT Int8Array : public TypedArray {
3733 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003734 static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
3735 size_t byte_offset, size_t length);
3736 static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3737 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003738 V8_INLINE static Int8Array* Cast(Value* obj);
3739
3740 private:
3741 Int8Array();
3742 static void CheckCast(Value* obj);
3743};
3744
3745
3746/**
3747 * An instance of Uint16Array constructor (ES6 draft 15.13.6).
3748 * This API is experimental and may change significantly.
3749 */
3750class V8_EXPORT Uint16Array : public TypedArray {
3751 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003752 static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
3753 size_t byte_offset, size_t length);
3754 static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3755 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003756 V8_INLINE static Uint16Array* Cast(Value* obj);
3757
3758 private:
3759 Uint16Array();
3760 static void CheckCast(Value* obj);
3761};
3762
3763
3764/**
3765 * An instance of Int16Array constructor (ES6 draft 15.13.6).
3766 * This API is experimental and may change significantly.
3767 */
3768class V8_EXPORT Int16Array : public TypedArray {
3769 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003770 static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
3771 size_t byte_offset, size_t length);
3772 static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003773 size_t byte_offset, size_t length);
3774 V8_INLINE static Int16Array* Cast(Value* obj);
3775
3776 private:
3777 Int16Array();
3778 static void CheckCast(Value* obj);
3779};
3780
3781
3782/**
3783 * An instance of Uint32Array constructor (ES6 draft 15.13.6).
3784 * This API is experimental and may change significantly.
3785 */
3786class V8_EXPORT Uint32Array : public TypedArray {
3787 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003788 static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
3789 size_t byte_offset, size_t length);
3790 static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3791 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003792 V8_INLINE static Uint32Array* Cast(Value* obj);
3793
3794 private:
3795 Uint32Array();
3796 static void CheckCast(Value* obj);
3797};
3798
3799
3800/**
3801 * An instance of Int32Array constructor (ES6 draft 15.13.6).
3802 * This API is experimental and may change significantly.
3803 */
3804class V8_EXPORT Int32Array : public TypedArray {
3805 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003806 static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
3807 size_t byte_offset, size_t length);
3808 static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003809 size_t byte_offset, size_t length);
3810 V8_INLINE static Int32Array* Cast(Value* obj);
3811
3812 private:
3813 Int32Array();
3814 static void CheckCast(Value* obj);
3815};
3816
3817
3818/**
3819 * An instance of Float32Array constructor (ES6 draft 15.13.6).
3820 * This API is experimental and may change significantly.
3821 */
3822class V8_EXPORT Float32Array : public TypedArray {
3823 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003824 static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
3825 size_t byte_offset, size_t length);
3826 static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3827 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003828 V8_INLINE static Float32Array* Cast(Value* obj);
3829
3830 private:
3831 Float32Array();
3832 static void CheckCast(Value* obj);
3833};
3834
3835
3836/**
3837 * An instance of Float64Array constructor (ES6 draft 15.13.6).
3838 * This API is experimental and may change significantly.
3839 */
3840class V8_EXPORT Float64Array : public TypedArray {
3841 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003842 static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
3843 size_t byte_offset, size_t length);
3844 static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
3845 size_t byte_offset, size_t length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003846 V8_INLINE static Float64Array* Cast(Value* obj);
3847
3848 private:
3849 Float64Array();
3850 static void CheckCast(Value* obj);
3851};
3852
3853
3854/**
3855 * An instance of DataView constructor (ES6 draft 15.13.7).
3856 * This API is experimental and may change significantly.
3857 */
3858class V8_EXPORT DataView : public ArrayBufferView {
3859 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003860 static Local<DataView> New(Local<ArrayBuffer> array_buffer,
3861 size_t byte_offset, size_t length);
3862 static Local<DataView> New(Local<SharedArrayBuffer> shared_array_buffer,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003863 size_t byte_offset, size_t length);
3864 V8_INLINE static DataView* Cast(Value* obj);
3865
3866 private:
3867 DataView();
3868 static void CheckCast(Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00003869};
3870
3871
3872/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003873 * An instance of the built-in SharedArrayBuffer constructor.
3874 * This API is experimental and may change significantly.
3875 */
3876class V8_EXPORT SharedArrayBuffer : public Object {
3877 public:
3878 /**
3879 * The contents of an |SharedArrayBuffer|. Externalization of
3880 * |SharedArrayBuffer| returns an instance of this class, populated, with a
3881 * pointer to data and byte length.
3882 *
3883 * The Data pointer of SharedArrayBuffer::Contents is always allocated with
3884 * |ArrayBuffer::Allocator::Allocate| by the allocator specified in
3885 * v8::Isolate::CreateParams::array_buffer_allocator.
3886 *
3887 * This API is experimental and may change significantly.
3888 */
3889 class V8_EXPORT Contents { // NOLINT
3890 public:
3891 Contents() : data_(NULL), byte_length_(0) {}
3892
3893 void* Data() const { return data_; }
3894 size_t ByteLength() const { return byte_length_; }
3895
3896 private:
3897 void* data_;
3898 size_t byte_length_;
3899
3900 friend class SharedArrayBuffer;
3901 };
3902
3903
3904 /**
3905 * Data length in bytes.
3906 */
3907 size_t ByteLength() const;
3908
3909 /**
3910 * Create a new SharedArrayBuffer. Allocate |byte_length| bytes.
3911 * Allocated memory will be owned by a created SharedArrayBuffer and
3912 * will be deallocated when it is garbage-collected,
3913 * unless the object is externalized.
3914 */
3915 static Local<SharedArrayBuffer> New(Isolate* isolate, size_t byte_length);
3916
3917 /**
3918 * Create a new SharedArrayBuffer over an existing memory block. The created
3919 * array buffer is immediately in externalized state unless otherwise
3920 * specified. The memory block will not be reclaimed when a created
3921 * SharedArrayBuffer is garbage-collected.
3922 */
3923 static Local<SharedArrayBuffer> New(
3924 Isolate* isolate, void* data, size_t byte_length,
3925 ArrayBufferCreationMode mode = ArrayBufferCreationMode::kExternalized);
3926
3927 /**
3928 * Returns true if SharedArrayBuffer is externalized, that is, does not
3929 * own its memory block.
3930 */
3931 bool IsExternal() const;
3932
3933 /**
3934 * Make this SharedArrayBuffer external. The pointer to underlying memory
3935 * block and byte length are returned as |Contents| structure. After
3936 * SharedArrayBuffer had been etxrenalized, it does no longer owns the memory
3937 * block. The caller should take steps to free memory when it is no longer
3938 * needed.
3939 *
3940 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3941 * by the allocator specified in
3942 * v8::Isolate::CreateParams::array_buffer_allocator.
3943 *
3944 */
3945 Contents Externalize();
3946
3947 /**
3948 * Get a pointer to the ArrayBuffer's underlying memory block without
3949 * externalizing it. If the ArrayBuffer is not externalized, this pointer
3950 * will become invalid as soon as the ArrayBuffer became garbage collected.
3951 *
3952 * The embedder should make sure to hold a strong reference to the
3953 * ArrayBuffer while accessing this pointer.
3954 *
3955 * The memory block is guaranteed to be allocated with |Allocator::Allocate|
3956 * by the allocator specified in
3957 * v8::Isolate::CreateParams::array_buffer_allocator.
3958 */
3959 Contents GetContents();
3960
3961 V8_INLINE static SharedArrayBuffer* Cast(Value* obj);
3962
3963 static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT;
3964
3965 private:
3966 SharedArrayBuffer();
3967 static void CheckCast(Value* obj);
3968};
3969
3970
3971/**
Ben Murdoch257744e2011-11-30 15:57:28 +00003972 * An instance of the built-in Date constructor (ECMA-262, 15.9).
3973 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003974class V8_EXPORT Date : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00003975 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003976 static V8_DEPRECATE_SOON("Use maybe version.",
3977 Local<Value> New(Isolate* isolate, double time));
3978 static V8_WARN_UNUSED_RESULT MaybeLocal<Value> New(Local<Context> context,
3979 double time);
Ben Murdoch257744e2011-11-30 15:57:28 +00003980
3981 /**
3982 * A specialization of Value::NumberValue that is more efficient
3983 * because we know the structure of this object.
3984 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003985 double ValueOf() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00003986
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003987 V8_INLINE static Date* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00003988
3989 /**
3990 * Notification that the embedder has changed the time zone,
3991 * daylight savings time, or other date / time configuration
3992 * parameters. V8 keeps a cache of various values used for
3993 * date / time computation. This notification will reset
3994 * those cached values for the current context so that date /
3995 * time configuration changes would be reflected in the Date
3996 * object.
3997 *
3998 * This API should not be called more than needed as it will
3999 * negatively impact the performance of date operations.
4000 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004001 static void DateTimeConfigurationChangeNotification(Isolate* isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00004002
4003 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004004 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004005};
4006
4007
4008/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004009 * A Number object (ECMA-262, 4.3.21).
4010 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004011class V8_EXPORT NumberObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004012 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004013 static Local<Value> New(Isolate* isolate, double value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004015 double ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004016
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004017 V8_INLINE static NumberObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004018
4019 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004020 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004021};
4022
4023
4024/**
4025 * A Boolean object (ECMA-262, 4.3.15).
4026 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004027class V8_EXPORT BooleanObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004028 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004029 static Local<Value> New(Isolate* isolate, bool value);
4030 V8_DEPRECATED("Pass an isolate", static Local<Value> New(bool value));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004032 bool ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004034 V8_INLINE static BooleanObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004035
4036 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004037 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004038};
4039
4040
4041/**
4042 * A String object (ECMA-262, 4.3.18).
4043 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004044class V8_EXPORT StringObject : public Object {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004045 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004046 static Local<Value> New(Local<String> value);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004048 Local<String> ValueOf() const;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004049
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004050 V8_INLINE static StringObject* Cast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004051
4052 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004053 static void CheckCast(v8::Value* obj);
4054};
4055
4056
4057/**
4058 * A Symbol object (ECMA-262 edition 6).
4059 *
4060 * This is an experimental feature. Use at your own risk.
4061 */
4062class V8_EXPORT SymbolObject : public Object {
4063 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004064 static Local<Value> New(Isolate* isolate, Local<Symbol> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004065
4066 Local<Symbol> ValueOf() const;
4067
4068 V8_INLINE static SymbolObject* Cast(v8::Value* obj);
4069
4070 private:
4071 static void CheckCast(v8::Value* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004072};
4073
4074
4075/**
Ben Murdoch257744e2011-11-30 15:57:28 +00004076 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
4077 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004078class V8_EXPORT RegExp : public Object {
Ben Murdoch257744e2011-11-30 15:57:28 +00004079 public:
4080 /**
4081 * Regular expression flag bits. They can be or'ed to enable a set
4082 * of flags.
4083 */
4084 enum Flags {
4085 kNone = 0,
4086 kGlobal = 1,
4087 kIgnoreCase = 2,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004088 kMultiline = 4,
4089 kSticky = 8,
4090 kUnicode = 16
Ben Murdoch257744e2011-11-30 15:57:28 +00004091 };
4092
4093 /**
4094 * Creates a regular expression from the given pattern string and
4095 * the flags bit field. May throw a JavaScript exception as
4096 * described in ECMA-262, 15.10.4.1.
4097 *
4098 * For example,
4099 * RegExp::New(v8::String::New("foo"),
4100 * static_cast<RegExp::Flags>(kGlobal | kMultiline))
4101 * is equivalent to evaluating "/foo/gm".
4102 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004103 static V8_DEPRECATE_SOON("Use maybe version",
4104 Local<RegExp> New(Local<String> pattern,
4105 Flags flags));
4106 static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
4107 Local<String> pattern,
4108 Flags flags);
Ben Murdoch257744e2011-11-30 15:57:28 +00004109
4110 /**
4111 * Returns the value of the source property: a string representing
4112 * the regular expression.
4113 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004114 Local<String> GetSource() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004115
4116 /**
4117 * Returns the flags bit field.
4118 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004119 Flags GetFlags() const;
Ben Murdoch257744e2011-11-30 15:57:28 +00004120
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004121 V8_INLINE static RegExp* Cast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004122
4123 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004124 static void CheckCast(v8::Value* obj);
Ben Murdoch257744e2011-11-30 15:57:28 +00004125};
4126
4127
4128/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004129 * A JavaScript value that wraps a C++ void*. This type of value is mainly used
4130 * to associate C++ data structures with JavaScript objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00004131 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004132class V8_EXPORT External : public Value {
Steve Blocka7e24c12009-10-30 11:49:00 +00004133 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004134 static Local<External> New(Isolate* isolate, void* value);
4135 V8_INLINE static External* Cast(Value* obj);
4136 void* Value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00004137 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004138 static void CheckCast(v8::Value* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00004139};
4140
4141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004142#define V8_INTRINSICS_LIST(F) F(ArrayProto_values, array_values_iterator)
4143
4144enum Intrinsic {
4145#define V8_DECL_INTRINSIC(name, iname) k##name,
4146 V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
4147#undef V8_DECL_INTRINSIC
4148};
4149
4150
Ben Murdoch257744e2011-11-30 15:57:28 +00004151// --- Templates ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004152
4153
4154/**
4155 * The superclass of object and function templates.
4156 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004157class V8_EXPORT Template : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004158 public:
Ben Murdochc5610432016-08-08 18:44:38 +01004159 /**
4160 * Adds a property to each instance created by this template.
4161 *
4162 * The property must be defined either as a primitive value, or a template.
4163 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004164 void Set(Local<Name> name, Local<Data> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00004165 PropertyAttribute attributes = None);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004166 V8_INLINE void Set(Isolate* isolate, const char* name, Local<Data> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004167
4168 void SetAccessorProperty(
4169 Local<Name> name,
4170 Local<FunctionTemplate> getter = Local<FunctionTemplate>(),
4171 Local<FunctionTemplate> setter = Local<FunctionTemplate>(),
4172 PropertyAttribute attribute = None,
4173 AccessControl settings = DEFAULT);
4174
4175 /**
4176 * Whenever the property with the given name is accessed on objects
4177 * created from this Template the getter and setter callbacks
4178 * are called instead of getting and setting the property directly
4179 * on the JavaScript object.
4180 *
4181 * \param name The name of the property for which an accessor is added.
4182 * \param getter The callback to invoke when getting the property.
4183 * \param setter The callback to invoke when setting the property.
4184 * \param data A piece of data that will be passed to the getter and setter
4185 * callbacks whenever they are invoked.
4186 * \param settings Access control settings for the accessor. This is a bit
4187 * field consisting of one of more of
4188 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4189 * The default is to not allow cross-context access.
4190 * ALL_CAN_READ means that all cross-context reads are allowed.
4191 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4192 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4193 * cross-context access.
4194 * \param attribute The attributes of the property for which an accessor
4195 * is added.
4196 * \param signature The signature describes valid receivers for the accessor
4197 * and is used to perform implicit instance checks against them. If the
4198 * receiver is incompatible (i.e. is not an instance of the constructor as
4199 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4200 * thrown and no callback is invoked.
4201 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004202 void SetNativeDataProperty(
4203 Local<String> name, AccessorGetterCallback getter,
4204 AccessorSetterCallback setter = 0,
4205 // TODO(dcarney): gcc can't handle Local below
4206 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4207 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4208 AccessControl settings = DEFAULT);
4209 void SetNativeDataProperty(
4210 Local<Name> name, AccessorNameGetterCallback getter,
4211 AccessorNameSetterCallback setter = 0,
4212 // TODO(dcarney): gcc can't handle Local below
4213 Local<Value> data = Local<Value>(), PropertyAttribute attribute = None,
4214 Local<AccessorSignature> signature = Local<AccessorSignature>(),
4215 AccessControl settings = DEFAULT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004216
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004217 /**
4218 * During template instantiation, sets the value with the intrinsic property
4219 * from the correct context.
4220 */
4221 void SetIntrinsicDataProperty(Local<Name> name, Intrinsic intrinsic,
4222 PropertyAttribute attribute = None);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004223
Steve Blocka7e24c12009-10-30 11:49:00 +00004224 private:
4225 Template();
4226
4227 friend class ObjectTemplate;
4228 friend class FunctionTemplate;
4229};
4230
4231
4232/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004233 * NamedProperty[Getter|Setter] are used as interceptors on object.
4234 * See ObjectTemplate::SetNamedPropertyHandler.
4235 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004236typedef void (*NamedPropertyGetterCallback)(
4237 Local<String> property,
4238 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004239
4240
4241/**
4242 * Returns the value if the setter intercepts the request.
4243 * Otherwise, returns an empty handle.
4244 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004245typedef void (*NamedPropertySetterCallback)(
4246 Local<String> property,
4247 Local<Value> value,
4248 const PropertyCallbackInfo<Value>& info);
4249
Steve Blocka7e24c12009-10-30 11:49:00 +00004250
Steve Blocka7e24c12009-10-30 11:49:00 +00004251/**
4252 * Returns a non-empty handle if the interceptor intercepts the request.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004253 * The result is an integer encoding property attributes (like v8::None,
4254 * v8::DontEnum, etc.)
Steve Blocka7e24c12009-10-30 11:49:00 +00004255 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004256typedef void (*NamedPropertyQueryCallback)(
4257 Local<String> property,
4258 const PropertyCallbackInfo<Integer>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004259
4260
4261/**
4262 * Returns a non-empty handle if the deleter intercepts the request.
4263 * The return value is true if the property could be deleted and false
4264 * otherwise.
4265 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004266typedef void (*NamedPropertyDeleterCallback)(
4267 Local<String> property,
4268 const PropertyCallbackInfo<Boolean>& info);
4269
Steve Blocka7e24c12009-10-30 11:49:00 +00004270
4271/**
4272 * Returns an array containing the names of the properties the named
4273 * property getter intercepts.
4274 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004275typedef void (*NamedPropertyEnumeratorCallback)(
4276 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004277
4278
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004279// TODO(dcarney): Deprecate and remove previous typedefs, and replace
4280// GenericNamedPropertyFooCallback with just NamedPropertyFooCallback.
4281/**
4282 * GenericNamedProperty[Getter|Setter] are used as interceptors on object.
4283 * See ObjectTemplate::SetNamedPropertyHandler.
4284 */
4285typedef void (*GenericNamedPropertyGetterCallback)(
4286 Local<Name> property, const PropertyCallbackInfo<Value>& info);
4287
4288
4289/**
4290 * Returns the value if the setter intercepts the request.
4291 * Otherwise, returns an empty handle.
4292 */
4293typedef void (*GenericNamedPropertySetterCallback)(
4294 Local<Name> property, Local<Value> value,
4295 const PropertyCallbackInfo<Value>& info);
4296
4297
4298/**
4299 * Returns a non-empty handle if the interceptor intercepts the request.
4300 * The result is an integer encoding property attributes (like v8::None,
4301 * v8::DontEnum, etc.)
4302 */
4303typedef void (*GenericNamedPropertyQueryCallback)(
4304 Local<Name> property, const PropertyCallbackInfo<Integer>& info);
4305
4306
4307/**
4308 * Returns a non-empty handle if the deleter intercepts the request.
4309 * The return value is true if the property could be deleted and false
4310 * otherwise.
4311 */
4312typedef void (*GenericNamedPropertyDeleterCallback)(
4313 Local<Name> property, const PropertyCallbackInfo<Boolean>& info);
4314
4315
4316/**
4317 * Returns an array containing the names of the properties the named
4318 * property getter intercepts.
4319 */
4320typedef void (*GenericNamedPropertyEnumeratorCallback)(
4321 const PropertyCallbackInfo<Array>& info);
4322
4323
Steve Blocka7e24c12009-10-30 11:49:00 +00004324/**
4325 * Returns the value of the property if the getter intercepts the
4326 * request. Otherwise, returns an empty handle.
4327 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004328typedef void (*IndexedPropertyGetterCallback)(
4329 uint32_t index,
4330 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004331
4332
4333/**
4334 * Returns the value if the setter intercepts the request.
4335 * Otherwise, returns an empty handle.
4336 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004337typedef void (*IndexedPropertySetterCallback)(
4338 uint32_t index,
4339 Local<Value> value,
4340 const PropertyCallbackInfo<Value>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004341
4342
4343/**
4344 * Returns a non-empty handle if the interceptor intercepts the request.
Iain Merrick75681382010-08-19 15:07:18 +01004345 * The result is an integer encoding property attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004346 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004347typedef void (*IndexedPropertyQueryCallback)(
4348 uint32_t index,
4349 const PropertyCallbackInfo<Integer>& info);
4350
Steve Blocka7e24c12009-10-30 11:49:00 +00004351
4352/**
4353 * Returns a non-empty handle if the deleter intercepts the request.
4354 * The return value is true if the property could be deleted and false
4355 * otherwise.
4356 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004357typedef void (*IndexedPropertyDeleterCallback)(
4358 uint32_t index,
4359 const PropertyCallbackInfo<Boolean>& info);
4360
Steve Blocka7e24c12009-10-30 11:49:00 +00004361
4362/**
4363 * Returns an array containing the indices of the properties the
4364 * indexed property getter intercepts.
4365 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004366typedef void (*IndexedPropertyEnumeratorCallback)(
4367 const PropertyCallbackInfo<Array>& info);
Steve Blocka7e24c12009-10-30 11:49:00 +00004368
4369
4370/**
Steve Blocka7e24c12009-10-30 11:49:00 +00004371 * Access type specification.
4372 */
4373enum AccessType {
4374 ACCESS_GET,
4375 ACCESS_SET,
4376 ACCESS_HAS,
4377 ACCESS_DELETE,
4378 ACCESS_KEYS
4379};
4380
4381
4382/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004383 * Returns true if the given context should be allowed to access the given
4384 * object.
4385 */
4386typedef bool (*AccessCheckCallback)(Local<Context> accessing_context,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004387 Local<Object> accessed_object,
4388 Local<Value> data);
Steve Blocka7e24c12009-10-30 11:49:00 +00004389
4390/**
4391 * A FunctionTemplate is used to create functions at runtime. There
4392 * can only be one function created from a FunctionTemplate in a
4393 * context. The lifetime of the created function is equal to the
4394 * lifetime of the context. So in case the embedder needs to create
4395 * temporary functions that can be collected using Scripts is
4396 * preferred.
4397 *
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004398 * Any modification of a FunctionTemplate after first instantiation will trigger
4399 *a crash.
4400 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004401 * A FunctionTemplate can have properties, these properties are added to the
4402 * function object when it is created.
4403 *
4404 * A FunctionTemplate has a corresponding instance template which is
4405 * used to create object instances when the function is used as a
4406 * constructor. Properties added to the instance template are added to
4407 * each object instance.
4408 *
4409 * A FunctionTemplate can have a prototype template. The prototype template
4410 * is used to create the prototype object of the function.
4411 *
4412 * The following example shows how to use a FunctionTemplate:
4413 *
4414 * \code
4415 * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
4416 * t->Set("func_property", v8::Number::New(1));
4417 *
4418 * v8::Local<v8::Template> proto_t = t->PrototypeTemplate();
4419 * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback));
4420 * proto_t->Set("proto_const", v8::Number::New(2));
4421 *
4422 * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate();
4423 * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback);
4424 * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...);
4425 * instance_t->Set("instance_property", Number::New(3));
4426 *
4427 * v8::Local<v8::Function> function = t->GetFunction();
4428 * v8::Local<v8::Object> instance = function->NewInstance();
4429 * \endcode
4430 *
4431 * Let's use "function" as the JS variable name of the function object
4432 * and "instance" for the instance object created above. The function
4433 * and the instance will have the following properties:
4434 *
4435 * \code
4436 * func_property in function == true;
4437 * function.func_property == 1;
4438 *
4439 * function.prototype.proto_method() invokes 'InvokeCallback'
4440 * function.prototype.proto_const == 2;
4441 *
4442 * instance instanceof function == true;
4443 * instance.instance_accessor calls 'InstanceAccessorCallback'
4444 * instance.instance_property == 3;
4445 * \endcode
4446 *
4447 * A FunctionTemplate can inherit from another one by calling the
4448 * FunctionTemplate::Inherit method. The following graph illustrates
4449 * the semantics of inheritance:
4450 *
4451 * \code
4452 * FunctionTemplate Parent -> Parent() . prototype -> { }
4453 * ^ ^
4454 * | Inherit(Parent) | .__proto__
4455 * | |
4456 * FunctionTemplate Child -> Child() . prototype -> { }
4457 * \endcode
4458 *
4459 * A FunctionTemplate 'Child' inherits from 'Parent', the prototype
4460 * object of the Child() function has __proto__ pointing to the
4461 * Parent() function's prototype object. An instance of the Child
4462 * function has all properties on Parent's instance templates.
4463 *
4464 * Let Parent be the FunctionTemplate initialized in the previous
4465 * section and create a Child FunctionTemplate by:
4466 *
4467 * \code
4468 * Local<FunctionTemplate> parent = t;
4469 * Local<FunctionTemplate> child = FunctionTemplate::New();
4470 * child->Inherit(parent);
4471 *
4472 * Local<Function> child_function = child->GetFunction();
4473 * Local<Object> child_instance = child_function->NewInstance();
4474 * \endcode
4475 *
4476 * The Child function and Child instance will have the following
4477 * properties:
4478 *
4479 * \code
4480 * child_func.prototype.__proto__ == function.prototype;
4481 * child_instance.instance_accessor calls 'InstanceAccessorCallback'
4482 * child_instance.instance_property == 3;
4483 * \endcode
4484 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004485class V8_EXPORT FunctionTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004486 public:
4487 /** Creates a function template.*/
4488 static Local<FunctionTemplate> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004489 Isolate* isolate, FunctionCallback callback = 0,
4490 Local<Value> data = Local<Value>(),
Ben Murdochc5610432016-08-08 18:44:38 +01004491 Local<Signature> signature = Local<Signature>(), int length = 0,
4492 ConstructorBehavior behavior = ConstructorBehavior::kAllow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004493
Ben Murdoch61f157c2016-09-16 13:49:30 +01004494 /** Get a template included in the snapshot by index. */
4495 static Local<FunctionTemplate> FromSnapshot(Isolate* isolate, size_t index);
4496
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004497 /**
4498 * Creates a function template with a fast handler. If a fast handler is set,
4499 * the callback cannot be null.
4500 */
4501 static Local<FunctionTemplate> NewWithFastHandler(
4502 Isolate* isolate, FunctionCallback callback,
4503 experimental::FastAccessorBuilder* fast_handler = nullptr,
4504 Local<Value> data = Local<Value>(),
4505 Local<Signature> signature = Local<Signature>(), int length = 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004506
Steve Blocka7e24c12009-10-30 11:49:00 +00004507 /** Returns the unique function instance in the current execution context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004508 V8_DEPRECATE_SOON("Use maybe version", Local<Function> GetFunction());
4509 V8_WARN_UNUSED_RESULT MaybeLocal<Function> GetFunction(
4510 Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004511
4512 /**
4513 * Set the call-handler callback for a FunctionTemplate. This
4514 * callback is called whenever the function created from this
4515 * FunctionTemplate is called.
4516 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004517 void SetCallHandler(
4518 FunctionCallback callback, Local<Value> data = Local<Value>(),
4519 experimental::FastAccessorBuilder* fast_handler = nullptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00004520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004521 /** Set the predefined length property for the FunctionTemplate. */
4522 void SetLength(int length);
4523
Steve Blocka7e24c12009-10-30 11:49:00 +00004524 /** Get the InstanceTemplate. */
4525 Local<ObjectTemplate> InstanceTemplate();
4526
4527 /** Causes the function template to inherit from a parent function template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004528 void Inherit(Local<FunctionTemplate> parent);
Steve Blocka7e24c12009-10-30 11:49:00 +00004529
4530 /**
4531 * A PrototypeTemplate is the template used to create the prototype object
4532 * of the function created by this template.
4533 */
4534 Local<ObjectTemplate> PrototypeTemplate();
4535
Steve Blocka7e24c12009-10-30 11:49:00 +00004536 /**
4537 * Set the class name of the FunctionTemplate. This is used for
4538 * printing objects created with the function created from the
4539 * FunctionTemplate as its constructor.
4540 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004541 void SetClassName(Local<String> name);
4542
4543
4544 /**
4545 * When set to true, no access check will be performed on the receiver of a
4546 * function call. Currently defaults to true, but this is subject to change.
4547 */
4548 void SetAcceptAnyReceiver(bool value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004549
4550 /**
4551 * Determines whether the __proto__ accessor ignores instances of
4552 * the function template. If instances of the function template are
4553 * ignored, __proto__ skips all instances and instead returns the
4554 * next object in the prototype chain.
4555 *
4556 * Call with a value of true to make the __proto__ accessor ignore
4557 * instances of the function template. Call with a value of false
4558 * to make the __proto__ accessor not ignore instances of the
4559 * function template. By default, instances of a function template
4560 * are not ignored.
4561 */
4562 void SetHiddenPrototype(bool value);
4563
4564 /**
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004565 * Sets the ReadOnly flag in the attributes of the 'prototype' property
4566 * of functions created from this FunctionTemplate to true.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004567 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004568 void ReadOnlyPrototype();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004569
4570 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004571 * Removes the prototype property from functions created from this
4572 * FunctionTemplate.
4573 */
4574 void RemovePrototype();
4575
4576 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00004577 * Returns true if the given object is an instance of this function
4578 * template.
4579 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004580 bool HasInstance(Local<Value> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00004581
4582 private:
4583 FunctionTemplate();
Steve Blocka7e24c12009-10-30 11:49:00 +00004584 friend class Context;
4585 friend class ObjectTemplate;
4586};
4587
4588
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004589enum class PropertyHandlerFlags {
4590 kNone = 0,
4591 // See ALL_CAN_READ above.
4592 kAllCanRead = 1,
4593 // Will not call into interceptor for properties on the receiver or prototype
4594 // chain. Currently only valid for named interceptors.
4595 kNonMasking = 1 << 1,
4596 // Will not call into interceptor for symbol lookup. Only meaningful for
4597 // named interceptors.
4598 kOnlyInterceptStrings = 1 << 2,
4599};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004600
4601
4602struct NamedPropertyHandlerConfiguration {
4603 NamedPropertyHandlerConfiguration(
4604 /** Note: getter is required **/
4605 GenericNamedPropertyGetterCallback getter = 0,
4606 GenericNamedPropertySetterCallback setter = 0,
4607 GenericNamedPropertyQueryCallback query = 0,
4608 GenericNamedPropertyDeleterCallback deleter = 0,
4609 GenericNamedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004610 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004611 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4612 : getter(getter),
4613 setter(setter),
4614 query(query),
4615 deleter(deleter),
4616 enumerator(enumerator),
4617 data(data),
4618 flags(flags) {}
4619
4620 GenericNamedPropertyGetterCallback getter;
4621 GenericNamedPropertySetterCallback setter;
4622 GenericNamedPropertyQueryCallback query;
4623 GenericNamedPropertyDeleterCallback deleter;
4624 GenericNamedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004625 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004626 PropertyHandlerFlags flags;
4627};
4628
4629
4630struct IndexedPropertyHandlerConfiguration {
4631 IndexedPropertyHandlerConfiguration(
4632 /** Note: getter is required **/
4633 IndexedPropertyGetterCallback getter = 0,
4634 IndexedPropertySetterCallback setter = 0,
4635 IndexedPropertyQueryCallback query = 0,
4636 IndexedPropertyDeleterCallback deleter = 0,
4637 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004638 Local<Value> data = Local<Value>(),
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004639 PropertyHandlerFlags flags = PropertyHandlerFlags::kNone)
4640 : getter(getter),
4641 setter(setter),
4642 query(query),
4643 deleter(deleter),
4644 enumerator(enumerator),
4645 data(data),
4646 flags(flags) {}
4647
4648 IndexedPropertyGetterCallback getter;
4649 IndexedPropertySetterCallback setter;
4650 IndexedPropertyQueryCallback query;
4651 IndexedPropertyDeleterCallback deleter;
4652 IndexedPropertyEnumeratorCallback enumerator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004653 Local<Value> data;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004654 PropertyHandlerFlags flags;
4655};
4656
4657
Steve Blocka7e24c12009-10-30 11:49:00 +00004658/**
4659 * An ObjectTemplate is used to create objects at runtime.
4660 *
4661 * Properties added to an ObjectTemplate are added to each object
4662 * created from the ObjectTemplate.
4663 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004664class V8_EXPORT ObjectTemplate : public Template {
Steve Blocka7e24c12009-10-30 11:49:00 +00004665 public:
4666 /** Creates an ObjectTemplate. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004667 static Local<ObjectTemplate> New(
4668 Isolate* isolate,
4669 Local<FunctionTemplate> constructor = Local<FunctionTemplate>());
4670 static V8_DEPRECATED("Use isolate version", Local<ObjectTemplate> New());
Steve Blocka7e24c12009-10-30 11:49:00 +00004671
Ben Murdoch61f157c2016-09-16 13:49:30 +01004672 /** Get a template included in the snapshot by index. */
4673 static Local<ObjectTemplate> FromSnapshot(Isolate* isolate, size_t index);
4674
Steve Blocka7e24c12009-10-30 11:49:00 +00004675 /** Creates a new instance of this template.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004676 V8_DEPRECATE_SOON("Use maybe version", Local<Object> NewInstance());
4677 V8_WARN_UNUSED_RESULT MaybeLocal<Object> NewInstance(Local<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00004678
4679 /**
4680 * Sets an accessor on the object template.
4681 *
4682 * Whenever the property with the given name is accessed on objects
4683 * created from this ObjectTemplate the getter and setter callbacks
4684 * are called instead of getting and setting the property directly
4685 * on the JavaScript object.
4686 *
4687 * \param name The name of the property for which an accessor is added.
4688 * \param getter The callback to invoke when getting the property.
4689 * \param setter The callback to invoke when setting the property.
4690 * \param data A piece of data that will be passed to the getter and setter
4691 * callbacks whenever they are invoked.
4692 * \param settings Access control settings for the accessor. This is a bit
4693 * field consisting of one of more of
4694 * DEFAULT = 0, ALL_CAN_READ = 1, or ALL_CAN_WRITE = 2.
4695 * The default is to not allow cross-context access.
4696 * ALL_CAN_READ means that all cross-context reads are allowed.
4697 * ALL_CAN_WRITE means that all cross-context writes are allowed.
4698 * The combination ALL_CAN_READ | ALL_CAN_WRITE can be used to allow all
4699 * cross-context access.
4700 * \param attribute The attributes of the property for which an accessor
4701 * is added.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004702 * \param signature The signature describes valid receivers for the accessor
4703 * and is used to perform implicit instance checks against them. If the
4704 * receiver is incompatible (i.e. is not an instance of the constructor as
4705 * defined by FunctionTemplate::HasInstance()), an implicit TypeError is
4706 * thrown and no callback is invoked.
Steve Blocka7e24c12009-10-30 11:49:00 +00004707 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004708 void SetAccessor(
4709 Local<String> name, AccessorGetterCallback getter,
4710 AccessorSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4711 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4712 Local<AccessorSignature> signature = Local<AccessorSignature>());
4713 void SetAccessor(
4714 Local<Name> name, AccessorNameGetterCallback getter,
4715 AccessorNameSetterCallback setter = 0, Local<Value> data = Local<Value>(),
4716 AccessControl settings = DEFAULT, PropertyAttribute attribute = None,
4717 Local<AccessorSignature> signature = Local<AccessorSignature>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004718
4719 /**
4720 * Sets a named property handler on the object template.
4721 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004722 * Whenever a property whose name is a string is accessed on objects created
4723 * from this object template, the provided callback is invoked instead of
Steve Blocka7e24c12009-10-30 11:49:00 +00004724 * accessing the property directly on the JavaScript object.
4725 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004726 * Note that new code should use the second version that can intercept
4727 * symbol-named properties as well as string-named properties.
4728 *
Steve Blocka7e24c12009-10-30 11:49:00 +00004729 * \param getter The callback to invoke when getting a property.
4730 * \param setter The callback to invoke when setting a property.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004731 * \param query The callback to invoke to check if a property is present,
4732 * and if present, get its attributes.
Steve Blocka7e24c12009-10-30 11:49:00 +00004733 * \param deleter The callback to invoke when deleting a property.
4734 * \param enumerator The callback to invoke to enumerate all the named
4735 * properties of an object.
4736 * \param data A piece of data that will be passed to the callbacks
4737 * whenever they are invoked.
4738 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004739 // TODO(dcarney): deprecate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004740 void SetNamedPropertyHandler(NamedPropertyGetterCallback getter,
4741 NamedPropertySetterCallback setter = 0,
4742 NamedPropertyQueryCallback query = 0,
4743 NamedPropertyDeleterCallback deleter = 0,
4744 NamedPropertyEnumeratorCallback enumerator = 0,
4745 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004746 void SetHandler(const NamedPropertyHandlerConfiguration& configuration);
Steve Blocka7e24c12009-10-30 11:49:00 +00004747
4748 /**
4749 * Sets an indexed property handler on the object template.
4750 *
4751 * Whenever an indexed property is accessed on objects created from
4752 * this object template, the provided callback is invoked instead of
4753 * accessing the property directly on the JavaScript object.
4754 *
4755 * \param getter The callback to invoke when getting a property.
4756 * \param setter The callback to invoke when setting a property.
Ben Murdoch257744e2011-11-30 15:57:28 +00004757 * \param query The callback to invoke to check if an object has a property.
Steve Blocka7e24c12009-10-30 11:49:00 +00004758 * \param deleter The callback to invoke when deleting a property.
4759 * \param enumerator The callback to invoke to enumerate all the indexed
4760 * properties of an object.
4761 * \param data A piece of data that will be passed to the callbacks
4762 * whenever they are invoked.
4763 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004764 void SetHandler(const IndexedPropertyHandlerConfiguration& configuration);
4765 // TODO(dcarney): deprecate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004766 void SetIndexedPropertyHandler(
4767 IndexedPropertyGetterCallback getter,
4768 IndexedPropertySetterCallback setter = 0,
4769 IndexedPropertyQueryCallback query = 0,
4770 IndexedPropertyDeleterCallback deleter = 0,
4771 IndexedPropertyEnumeratorCallback enumerator = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004772 Local<Value> data = Local<Value>()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004773 SetHandler(IndexedPropertyHandlerConfiguration(getter, setter, query,
4774 deleter, enumerator, data));
4775 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004776 /**
4777 * Sets the callback to be used when calling instances created from
4778 * this template as a function. If no callback is set, instances
4779 * behave like normal JavaScript objects that cannot be called as a
4780 * function.
4781 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004782 void SetCallAsFunctionHandler(FunctionCallback callback,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004783 Local<Value> data = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004784
4785 /**
4786 * Mark object instances of the template as undetectable.
4787 *
4788 * In many ways, undetectable objects behave as though they are not
4789 * there. They behave like 'undefined' in conditionals and when
4790 * printed. However, properties can be accessed and called as on
4791 * normal objects.
4792 */
4793 void MarkAsUndetectable();
4794
4795 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004796 * Sets access check callback on the object template and enables access
4797 * checks.
Steve Blocka7e24c12009-10-30 11:49:00 +00004798 *
4799 * When accessing properties on instances of this object template,
4800 * the access check callback will be called to determine whether or
4801 * not to allow cross-context access to the properties.
Steve Blocka7e24c12009-10-30 11:49:00 +00004802 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004803 void SetAccessCheckCallback(AccessCheckCallback callback,
4804 Local<Value> data = Local<Value>());
4805
Ben Murdoch61f157c2016-09-16 13:49:30 +01004806 /**
4807 * Like SetAccessCheckCallback but invokes an interceptor on failed access
4808 * checks instead of looking up all-can-read properties. You can only use
4809 * either this method or SetAccessCheckCallback, but not both at the same
4810 * time.
4811 */
4812 void SetAccessCheckCallbackAndHandler(
4813 AccessCheckCallback callback,
4814 const NamedPropertyHandlerConfiguration& named_handler,
4815 const IndexedPropertyHandlerConfiguration& indexed_handler,
4816 Local<Value> data = Local<Value>());
Steve Blocka7e24c12009-10-30 11:49:00 +00004817
4818 /**
4819 * Gets the number of internal fields for objects generated from
4820 * this template.
4821 */
4822 int InternalFieldCount();
4823
4824 /**
4825 * Sets the number of internal fields for objects generated from
4826 * this template.
4827 */
4828 void SetInternalFieldCount(int value);
4829
4830 private:
4831 ObjectTemplate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004832 static Local<ObjectTemplate> New(internal::Isolate* isolate,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004833 Local<FunctionTemplate> constructor);
Steve Blocka7e24c12009-10-30 11:49:00 +00004834 friend class FunctionTemplate;
4835};
4836
4837
4838/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004839 * A Signature specifies which receiver is valid for a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004840 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004841class V8_EXPORT Signature : public Data {
Steve Blocka7e24c12009-10-30 11:49:00 +00004842 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004843 static Local<Signature> New(
4844 Isolate* isolate,
4845 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004846
Steve Blocka7e24c12009-10-30 11:49:00 +00004847 private:
4848 Signature();
4849};
4850
4851
4852/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004853 * An AccessorSignature specifies which receivers are valid parameters
4854 * to an accessor callback.
4855 */
4856class V8_EXPORT AccessorSignature : public Data {
4857 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004858 static Local<AccessorSignature> New(
4859 Isolate* isolate,
4860 Local<FunctionTemplate> receiver = Local<FunctionTemplate>());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004861
4862 private:
4863 AccessorSignature();
4864};
4865
4866
Ben Murdoch257744e2011-11-30 15:57:28 +00004867// --- Extensions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004868
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004869class V8_EXPORT ExternalOneByteStringResourceImpl
4870 : public String::ExternalOneByteStringResource {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004871 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004872 ExternalOneByteStringResourceImpl() : data_(0), length_(0) {}
4873 ExternalOneByteStringResourceImpl(const char* data, size_t length)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004874 : data_(data), length_(length) {}
4875 const char* data() const { return data_; }
4876 size_t length() const { return length_; }
4877
4878 private:
4879 const char* data_;
4880 size_t length_;
4881};
Steve Blocka7e24c12009-10-30 11:49:00 +00004882
4883/**
4884 * Ignore
4885 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004886class V8_EXPORT Extension { // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00004887 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004888 // Note that the strings passed into this constructor must live as long
4889 // as the Extension itself.
Steve Blocka7e24c12009-10-30 11:49:00 +00004890 Extension(const char* name,
4891 const char* source = 0,
4892 int dep_count = 0,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004893 const char** deps = 0,
4894 int source_length = -1);
Steve Blocka7e24c12009-10-30 11:49:00 +00004895 virtual ~Extension() { }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004896 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
4897 v8::Isolate* isolate, v8::Local<v8::String> name) {
4898 return v8::Local<v8::FunctionTemplate>();
Steve Blocka7e24c12009-10-30 11:49:00 +00004899 }
4900
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004901 const char* name() const { return name_; }
4902 size_t source_length() const { return source_length_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004903 const String::ExternalOneByteStringResource* source() const {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004904 return &source_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00004905 int dependency_count() { return dep_count_; }
4906 const char** dependencies() { return deps_; }
4907 void set_auto_enable(bool value) { auto_enable_ = value; }
4908 bool auto_enable() { return auto_enable_; }
4909
4910 private:
4911 const char* name_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004912 size_t source_length_; // expected to initialize before source_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004913 ExternalOneByteStringResourceImpl source_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004914 int dep_count_;
4915 const char** deps_;
4916 bool auto_enable_;
4917
4918 // Disallow copying and assigning.
4919 Extension(const Extension&);
4920 void operator=(const Extension&);
4921};
4922
4923
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004924void V8_EXPORT RegisterExtension(Extension* extension);
Steve Blocka7e24c12009-10-30 11:49:00 +00004925
4926
Ben Murdoch257744e2011-11-30 15:57:28 +00004927// --- Statics ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004928
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004929V8_INLINE Local<Primitive> Undefined(Isolate* isolate);
4930V8_INLINE Local<Primitive> Null(Isolate* isolate);
4931V8_INLINE Local<Boolean> True(Isolate* isolate);
4932V8_INLINE Local<Boolean> False(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00004933
Steve Blocka7e24c12009-10-30 11:49:00 +00004934/**
4935 * A set of constraints that specifies the limits of the runtime's memory use.
4936 * You must set the heap size before initializing the VM - the size cannot be
4937 * adjusted after the VM is initialized.
4938 *
4939 * If you are using threads then you should hold the V8::Locker lock while
4940 * setting the stack limit and you must set a non-default stack limit separately
4941 * for each thread.
Ben Murdochda12d292016-06-02 14:46:10 +01004942 *
4943 * The arguments for set_max_semi_space_size, set_max_old_space_size,
4944 * set_max_executable_size, set_code_range_size specify limits in MB.
Steve Blocka7e24c12009-10-30 11:49:00 +00004945 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004946class V8_EXPORT ResourceConstraints {
Steve Blocka7e24c12009-10-30 11:49:00 +00004947 public:
4948 ResourceConstraints();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004949
4950 /**
4951 * Configures the constraints with reasonable default values based on the
4952 * capabilities of the current device the VM is running on.
4953 *
4954 * \param physical_memory The total amount of physical memory on the current
4955 * device, in bytes.
4956 * \param virtual_memory_limit The amount of virtual memory on the current
4957 * device, in bytes, or zero, if there is no limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004958 */
4959 void ConfigureDefaults(uint64_t physical_memory,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004960 uint64_t virtual_memory_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004961
4962 int max_semi_space_size() const { return max_semi_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004963 void set_max_semi_space_size(int limit_in_mb) {
4964 max_semi_space_size_ = limit_in_mb;
4965 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004966 int max_old_space_size() const { return max_old_space_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004967 void set_max_old_space_size(int limit_in_mb) {
4968 max_old_space_size_ = limit_in_mb;
4969 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004970 int max_executable_size() const { return max_executable_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004971 void set_max_executable_size(int limit_in_mb) {
4972 max_executable_size_ = limit_in_mb;
4973 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004974 uint32_t* stack_limit() const { return stack_limit_; }
4975 // Sets an address beyond which the VM's stack may not grow.
4976 void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004977 size_t code_range_size() const { return code_range_size_; }
Ben Murdochda12d292016-06-02 14:46:10 +01004978 void set_code_range_size(size_t limit_in_mb) {
4979 code_range_size_ = limit_in_mb;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004980 }
4981
Steve Blocka7e24c12009-10-30 11:49:00 +00004982 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004983 int max_semi_space_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004984 int max_old_space_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08004985 int max_executable_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004986 uint32_t* stack_limit_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004987 size_t code_range_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004988};
4989
4990
Ben Murdoch257744e2011-11-30 15:57:28 +00004991// --- Exceptions ---
Steve Blocka7e24c12009-10-30 11:49:00 +00004992
4993
4994typedef void (*FatalErrorCallback)(const char* location, const char* message);
4995
4996
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004997typedef void (*MessageCallback)(Local<Message> message, Local<Value> error);
Steve Blocka7e24c12009-10-30 11:49:00 +00004998
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004999// --- Tracing ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005001typedef void (*LogEventCallback)(const char* name, int event);
Steve Blocka7e24c12009-10-30 11:49:00 +00005002
5003/**
5004 * Create new error objects by calling the corresponding error object
5005 * constructor with the message.
5006 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005007class V8_EXPORT Exception {
Steve Blocka7e24c12009-10-30 11:49:00 +00005008 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005009 static Local<Value> RangeError(Local<String> message);
5010 static Local<Value> ReferenceError(Local<String> message);
5011 static Local<Value> SyntaxError(Local<String> message);
5012 static Local<Value> TypeError(Local<String> message);
5013 static Local<Value> Error(Local<String> message);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005014
5015 /**
5016 * Creates an error message for the given exception.
5017 * Will try to reconstruct the original stack trace from the exception value,
5018 * or capture the current stack trace if not available.
5019 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005020 static Local<Message> CreateMessage(Isolate* isolate, Local<Value> exception);
5021 V8_DEPRECATED("Use version with an Isolate*",
5022 static Local<Message> CreateMessage(Local<Value> exception));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005023
5024 /**
5025 * Returns the original stack trace that was captured at the creation time
5026 * of a given exception, or an empty handle if not available.
5027 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005028 static Local<StackTrace> GetStackTrace(Local<Value> exception);
Steve Blocka7e24c12009-10-30 11:49:00 +00005029};
5030
5031
Ben Murdoch257744e2011-11-30 15:57:28 +00005032// --- Counters Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005033
5034typedef int* (*CounterLookupCallback)(const char* name);
5035
5036typedef void* (*CreateHistogramCallback)(const char* name,
5037 int min,
5038 int max,
5039 size_t buckets);
5040
5041typedef void (*AddHistogramSampleCallback)(void* histogram, int sample);
5042
Ben Murdoch257744e2011-11-30 15:57:28 +00005043// --- Memory Allocation Callback ---
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005044enum ObjectSpace {
5045 kObjectSpaceNewSpace = 1 << 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005046 kObjectSpaceOldSpace = 1 << 1,
5047 kObjectSpaceCodeSpace = 1 << 2,
5048 kObjectSpaceMapSpace = 1 << 3,
5049 kObjectSpaceLoSpace = 1 << 4,
5050 kObjectSpaceAll = kObjectSpaceNewSpace | kObjectSpaceOldSpace |
5051 kObjectSpaceCodeSpace | kObjectSpaceMapSpace |
5052 kObjectSpaceLoSpace
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005053};
Iain Merrick9ac36c92010-09-13 15:29:50 +01005054
5055 enum AllocationAction {
5056 kAllocationActionAllocate = 1 << 0,
5057 kAllocationActionFree = 1 << 1,
5058 kAllocationActionAll = kAllocationActionAllocate | kAllocationActionFree
5059 };
5060
Ben Murdoch097c5b22016-05-18 11:27:45 +01005061// --- Enter/Leave Script Callback ---
5062typedef void (*BeforeCallEnteredCallback)(Isolate*);
5063typedef void (*CallCompletedCallback)(Isolate*);
5064typedef void (*DeprecatedCallCompletedCallback)();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005065
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005066// --- Promise Reject Callback ---
5067enum PromiseRejectEvent {
5068 kPromiseRejectWithNoHandler = 0,
5069 kPromiseHandlerAddedAfterReject = 1
5070};
5071
5072class PromiseRejectMessage {
5073 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005074 PromiseRejectMessage(Local<Promise> promise, PromiseRejectEvent event,
5075 Local<Value> value, Local<StackTrace> stack_trace)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005076 : promise_(promise),
5077 event_(event),
5078 value_(value),
5079 stack_trace_(stack_trace) {}
5080
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005081 V8_INLINE Local<Promise> GetPromise() const { return promise_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005082 V8_INLINE PromiseRejectEvent GetEvent() const { return event_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005083 V8_INLINE Local<Value> GetValue() const { return value_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005085 V8_DEPRECATED("Use v8::Exception::CreateMessage(GetValue())->GetStackTrace()",
5086 V8_INLINE Local<StackTrace> GetStackTrace() const) {
5087 return stack_trace_;
5088 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005089
5090 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005091 Local<Promise> promise_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005092 PromiseRejectEvent event_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005093 Local<Value> value_;
5094 Local<StackTrace> stack_trace_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005095};
5096
5097typedef void (*PromiseRejectCallback)(PromiseRejectMessage message);
5098
Ben Murdochda12d292016-06-02 14:46:10 +01005099// --- Microtasks Callbacks ---
5100typedef void (*MicrotasksCompletedCallback)(Isolate*);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005101typedef void (*MicrotaskCallback)(void* data);
5102
Ben Murdochda12d292016-06-02 14:46:10 +01005103
5104/**
5105 * Policy for running microtasks:
5106 * - explicit: microtasks are invoked with Isolate::RunMicrotasks() method;
5107 * - scoped: microtasks invocation is controlled by MicrotasksScope objects;
5108 * - auto: microtasks are invoked when the script call depth decrements
5109 * to zero.
5110 */
5111enum class MicrotasksPolicy { kExplicit, kScoped, kAuto };
5112
5113
5114/**
5115 * This scope is used to control microtasks when kScopeMicrotasksInvocation
5116 * is used on Isolate. In this mode every non-primitive call to V8 should be
5117 * done inside some MicrotasksScope.
5118 * Microtasks are executed when topmost MicrotasksScope marked as kRunMicrotasks
5119 * exits.
5120 * kDoNotRunMicrotasks should be used to annotate calls not intended to trigger
5121 * microtasks.
5122 */
5123class V8_EXPORT MicrotasksScope {
5124 public:
5125 enum Type { kRunMicrotasks, kDoNotRunMicrotasks };
5126
5127 MicrotasksScope(Isolate* isolate, Type type);
5128 ~MicrotasksScope();
5129
5130 /**
5131 * Runs microtasks if no kRunMicrotasks scope is currently active.
5132 */
5133 static void PerformCheckpoint(Isolate* isolate);
5134
5135 /**
5136 * Returns current depth of nested kRunMicrotasks scopes.
5137 */
5138 static int GetCurrentDepth(Isolate* isolate);
5139
Ben Murdochc5610432016-08-08 18:44:38 +01005140 /**
5141 * Returns true while microtasks are being executed.
5142 */
5143 static bool IsRunningMicrotasks(Isolate* isolate);
5144
Ben Murdochda12d292016-06-02 14:46:10 +01005145 private:
5146 internal::Isolate* const isolate_;
5147 bool run_;
5148
5149 // Prevent copying.
5150 MicrotasksScope(const MicrotasksScope&);
5151 MicrotasksScope& operator=(const MicrotasksScope&);
5152};
5153
5154
Ben Murdoch257744e2011-11-30 15:57:28 +00005155// --- Failed Access Check Callback ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005156typedef void (*FailedAccessCheckCallback)(Local<Object> target,
5157 AccessType type,
5158 Local<Value> data);
5159
Ben Murdoch257744e2011-11-30 15:57:28 +00005160// --- AllowCodeGenerationFromStrings callbacks ---
5161
5162/**
5163 * Callback to check if code generation from strings is allowed. See
5164 * Context::AllowCodeGenerationFromStrings.
5165 */
5166typedef bool (*AllowCodeGenerationFromStringsCallback)(Local<Context> context);
5167
5168// --- Garbage Collection Callbacks ---
Steve Blocka7e24c12009-10-30 11:49:00 +00005169
5170/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005171 * Applications can register callback functions which will be called before and
5172 * after certain garbage collection operations. Allocations are not allowed in
5173 * the callback functions, you therefore cannot manipulate objects (set or
5174 * delete properties for example) since it is possible such operations will
5175 * result in the allocation of objects.
Steve Blocka7e24c12009-10-30 11:49:00 +00005176 */
Steve Block6ded16b2010-05-10 14:33:55 +01005177enum GCType {
5178 kGCTypeScavenge = 1 << 0,
5179 kGCTypeMarkSweepCompact = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005180 kGCTypeIncrementalMarking = 1 << 2,
5181 kGCTypeProcessWeakCallbacks = 1 << 3,
5182 kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact |
5183 kGCTypeIncrementalMarking | kGCTypeProcessWeakCallbacks
Steve Block6ded16b2010-05-10 14:33:55 +01005184};
5185
Ben Murdoch097c5b22016-05-18 11:27:45 +01005186/**
5187 * GCCallbackFlags is used to notify additional information about the GC
5188 * callback.
5189 * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for
5190 * constructing retained object infos.
5191 * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing.
5192 * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback
5193 * is called synchronously without getting posted to an idle task.
5194 * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called
5195 * in a phase where V8 is trying to collect all available garbage
5196 * (e.g., handling a low memory notification).
5197 */
Steve Block6ded16b2010-05-10 14:33:55 +01005198enum GCCallbackFlags {
5199 kNoGCCallbackFlags = 0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005200 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005201 kGCCallbackFlagForced = 1 << 2,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005202 kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3,
5203 kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4,
Steve Block6ded16b2010-05-10 14:33:55 +01005204};
5205
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005206typedef void (*GCCallback)(GCType type, GCCallbackFlags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01005207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005208typedef void (*InterruptCallback)(Isolate* isolate, void* data);
Steve Blocka7e24c12009-10-30 11:49:00 +00005209
5210
Steve Blocka7e24c12009-10-30 11:49:00 +00005211/**
Steve Block3ce2e202009-11-05 08:53:23 +00005212 * Collection of V8 heap information.
5213 *
5214 * Instances of this class can be passed to v8::V8::HeapStatistics to
5215 * get heap statistics from V8.
5216 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005217class V8_EXPORT HeapStatistics {
Steve Block3ce2e202009-11-05 08:53:23 +00005218 public:
5219 HeapStatistics();
5220 size_t total_heap_size() { return total_heap_size_; }
Russell Brenner90bac252010-11-18 13:33:46 -08005221 size_t total_heap_size_executable() { return total_heap_size_executable_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005222 size_t total_physical_size() { return total_physical_size_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005223 size_t total_available_size() { return total_available_size_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005224 size_t used_heap_size() { return used_heap_size_; }
Ben Murdochb8e0da22011-05-16 14:20:40 +01005225 size_t heap_size_limit() { return heap_size_limit_; }
Ben Murdochda12d292016-06-02 14:46:10 +01005226 size_t malloced_memory() { return malloced_memory_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005227 size_t does_zap_garbage() { return does_zap_garbage_; }
Steve Block3ce2e202009-11-05 08:53:23 +00005228
5229 private:
Steve Block3ce2e202009-11-05 08:53:23 +00005230 size_t total_heap_size_;
Russell Brenner90bac252010-11-18 13:33:46 -08005231 size_t total_heap_size_executable_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005232 size_t total_physical_size_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005233 size_t total_available_size_;
Steve Block3ce2e202009-11-05 08:53:23 +00005234 size_t used_heap_size_;
Ben Murdochb8e0da22011-05-16 14:20:40 +01005235 size_t heap_size_limit_;
Ben Murdochda12d292016-06-02 14:46:10 +01005236 size_t malloced_memory_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005237 bool does_zap_garbage_;
Steve Block3ce2e202009-11-05 08:53:23 +00005238
5239 friend class V8;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005240 friend class Isolate;
Steve Block3ce2e202009-11-05 08:53:23 +00005241};
5242
5243
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005244class V8_EXPORT HeapSpaceStatistics {
5245 public:
5246 HeapSpaceStatistics();
5247 const char* space_name() { return space_name_; }
5248 size_t space_size() { return space_size_; }
5249 size_t space_used_size() { return space_used_size_; }
5250 size_t space_available_size() { return space_available_size_; }
5251 size_t physical_space_size() { return physical_space_size_; }
5252
5253 private:
5254 const char* space_name_;
5255 size_t space_size_;
5256 size_t space_used_size_;
5257 size_t space_available_size_;
5258 size_t physical_space_size_;
5259
5260 friend class Isolate;
5261};
5262
5263
5264class V8_EXPORT HeapObjectStatistics {
5265 public:
5266 HeapObjectStatistics();
5267 const char* object_type() { return object_type_; }
5268 const char* object_sub_type() { return object_sub_type_; }
5269 size_t object_count() { return object_count_; }
5270 size_t object_size() { return object_size_; }
5271
5272 private:
5273 const char* object_type_;
5274 const char* object_sub_type_;
5275 size_t object_count_;
5276 size_t object_size_;
5277
5278 friend class Isolate;
5279};
5280
Ben Murdoch61f157c2016-09-16 13:49:30 +01005281class V8_EXPORT HeapCodeStatistics {
5282 public:
5283 HeapCodeStatistics();
5284 size_t code_and_metadata_size() { return code_and_metadata_size_; }
5285 size_t bytecode_and_metadata_size() { return bytecode_and_metadata_size_; }
5286
5287 private:
5288 size_t code_and_metadata_size_;
5289 size_t bytecode_and_metadata_size_;
5290
5291 friend class Isolate;
5292};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005293
Steve Block44f0eee2011-05-26 01:26:41 +01005294class RetainedObjectInfo;
5295
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005296
Steve Block44f0eee2011-05-26 01:26:41 +01005297/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005298 * FunctionEntryHook is the type of the profile entry hook called at entry to
5299 * any generated function when function-level profiling is enabled.
5300 *
5301 * \param function the address of the function that's being entered.
5302 * \param return_addr_location points to a location on stack where the machine
5303 * return address resides. This can be used to identify the caller of
5304 * \p function, and/or modified to divert execution when \p function exits.
5305 *
5306 * \note the entry hook must not cause garbage collection.
Steve Block44f0eee2011-05-26 01:26:41 +01005307 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005308typedef void (*FunctionEntryHook)(uintptr_t function,
5309 uintptr_t return_addr_location);
5310
5311/**
5312 * A JIT code event is issued each time code is added, moved or removed.
5313 *
5314 * \note removal events are not currently issued.
5315 */
5316struct JitCodeEvent {
5317 enum EventType {
5318 CODE_ADDED,
5319 CODE_MOVED,
5320 CODE_REMOVED,
5321 CODE_ADD_LINE_POS_INFO,
5322 CODE_START_LINE_INFO_RECORDING,
5323 CODE_END_LINE_INFO_RECORDING
5324 };
5325 // Definition of the code position type. The "POSITION" type means the place
5326 // in the source code which are of interest when making stack traces to
5327 // pin-point the source location of a stack frame as close as possible.
5328 // The "STATEMENT_POSITION" means the place at the beginning of each
5329 // statement, and is used to indicate possible break locations.
5330 enum PositionType { POSITION, STATEMENT_POSITION };
5331
5332 // Type of event.
5333 EventType type;
5334 // Start of the instructions.
5335 void* code_start;
5336 // Size of the instructions.
5337 size_t code_len;
5338 // Script info for CODE_ADDED event.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005339 Local<UnboundScript> script;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005340 // User-defined data for *_LINE_INFO_* event. It's used to hold the source
5341 // code line information which is returned from the
5342 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent
5343 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events.
5344 void* user_data;
5345
5346 struct name_t {
5347 // Name of the object associated with the code, note that the string is not
5348 // zero-terminated.
5349 const char* str;
5350 // Number of chars in str.
5351 size_t len;
5352 };
5353
5354 struct line_info_t {
5355 // PC offset
5356 size_t offset;
5357 // Code postion
5358 size_t pos;
5359 // The position type.
5360 PositionType position_type;
5361 };
5362
5363 union {
5364 // Only valid for CODE_ADDED.
5365 struct name_t name;
5366
5367 // Only valid for CODE_ADD_LINE_POS_INFO
5368 struct line_info_t line_info;
5369
5370 // New location of instructions. Only valid for CODE_MOVED.
5371 void* new_code_start;
5372 };
5373};
5374
5375/**
Ben Murdoch61f157c2016-09-16 13:49:30 +01005376 * Option flags passed to the SetRAILMode function.
5377 * See documentation https://developers.google.com/web/tools/chrome-devtools/
5378 * profile/evaluate-performance/rail
5379 */
5380enum RAILMode {
5381 // Default performance mode: V8 will optimize for both latency and
5382 // throughput in this mode.
5383 PERFORMANCE_DEFAULT,
5384 // Response performance mode: In this mode very low virtual machine latency
5385 // is provided. V8 will try to avoid JavaScript execution interruptions.
5386 // Throughput may be throttled.
5387 PERFORMANCE_RESPONSE,
5388 // Animation performance mode: In this mode low virtual machine latency is
5389 // provided. V8 will try to avoid as many JavaScript execution interruptions
5390 // as possible. Throughput may be throttled
5391 PERFORMANCE_ANIMATION,
5392 // Idle performance mode: The embedder is idle. V8 can complete deferred work
5393 // in this mode.
5394 PERFORMANCE_IDLE,
5395 // Load performance mode: In this mode high throughput is provided. V8 may
5396 // turn off latency optimizations.
5397 PERFORMANCE_LOAD
5398};
5399
5400/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005401 * Option flags passed to the SetJitCodeEventHandler function.
5402 */
5403enum JitCodeEventOptions {
5404 kJitCodeEventDefault = 0,
5405 // Generate callbacks for already existent code.
5406 kJitCodeEventEnumExisting = 1
5407};
5408
5409
5410/**
5411 * Callback function passed to SetJitCodeEventHandler.
5412 *
5413 * \param event code add, move or removal event.
5414 */
5415typedef void (*JitCodeEventHandler)(const JitCodeEvent* event);
5416
5417
5418/**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005419 * Interface for iterating through all external resources in the heap.
5420 */
5421class V8_EXPORT ExternalResourceVisitor { // NOLINT
5422 public:
5423 virtual ~ExternalResourceVisitor() {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005424 virtual void VisitExternalString(Local<String> string) {}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005425};
5426
5427
5428/**
5429 * Interface for iterating through all the persistent handles in the heap.
5430 */
5431class V8_EXPORT PersistentHandleVisitor { // NOLINT
5432 public:
5433 virtual ~PersistentHandleVisitor() {}
5434 virtual void VisitPersistentHandle(Persistent<Value>* value,
5435 uint16_t class_id) {}
5436};
5437
Ben Murdochda12d292016-06-02 14:46:10 +01005438/**
5439 * Memory pressure level for the MemoryPressureNotification.
5440 * kNone hints V8 that there is no memory pressure.
5441 * kModerate hints V8 to speed up incremental garbage collection at the cost of
5442 * of higher latency due to garbage collection pauses.
5443 * kCritical hints V8 to free memory as soon as possible. Garbage collection
5444 * pauses at this level will be large.
5445 */
5446enum class MemoryPressureLevel { kNone, kModerate, kCritical };
5447
5448/**
5449 * Interface for tracing through the embedder heap. During the v8 garbage
5450 * collection, v8 collects hidden fields of all potential wrappers, and at the
5451 * end of its marking phase iterates the collection and asks the embedder to
5452 * trace through its heap and call PersistentBase::RegisterExternalReference on
5453 * each js object reachable from any of the given wrappers.
5454 *
Ben Murdochc5610432016-08-08 18:44:38 +01005455 * Before the first call to the TraceWrappersFrom function TracePrologue will be
5456 * called. When the garbage collection cycle is finished, TraceEpilogue will be
5457 * called.
Ben Murdochda12d292016-06-02 14:46:10 +01005458 */
Ben Murdochc5610432016-08-08 18:44:38 +01005459class V8_EXPORT EmbedderHeapTracer {
Ben Murdochda12d292016-06-02 14:46:10 +01005460 public:
Ben Murdoch61f157c2016-09-16 13:49:30 +01005461 enum ForceCompletionAction { FORCE_COMPLETION, DO_NOT_FORCE_COMPLETION };
5462 struct AdvanceTracingActions {
5463 explicit AdvanceTracingActions(ForceCompletionAction force_completion_)
5464 : force_completion(force_completion_) {}
5465
5466 ForceCompletionAction force_completion;
5467 };
5468 /**
5469 * V8 will call this method with internal fields of found wrappers.
5470 * Embedder is expected to store them in it's marking deque and trace
5471 * reachable wrappers from them when asked by AdvanceTracing method.
5472 */
5473 virtual void RegisterV8References(
5474 const std::vector<std::pair<void*, void*> >& internal_fields) = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005475 /**
5476 * V8 will call this method at the beginning of the gc cycle.
5477 */
Ben Murdochc5610432016-08-08 18:44:38 +01005478 virtual void TracePrologue() = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005479 /**
Ben Murdoch61f157c2016-09-16 13:49:30 +01005480 * Embedder is expected to trace its heap starting from wrappers reported by
5481 * RegisterV8References method, and call
5482 * PersistentBase::RegisterExternalReference() on all reachable wrappers.
5483 * Embedder is expected to stop tracing by the given deadline.
5484 *
5485 * Returns true if there is still work to do.
Ben Murdochda12d292016-06-02 14:46:10 +01005486 */
Ben Murdoch61f157c2016-09-16 13:49:30 +01005487 virtual bool AdvanceTracing(double deadline_in_ms,
5488 AdvanceTracingActions actions) = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005489 /**
5490 * V8 will call this method at the end of the gc cycle. Allocation is *not*
Ben Murdochc5610432016-08-08 18:44:38 +01005491 * allowed in the TraceEpilogue.
Ben Murdochda12d292016-06-02 14:46:10 +01005492 */
Ben Murdochc5610432016-08-08 18:44:38 +01005493 virtual void TraceEpilogue() = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005494
Ben Murdoch61f157c2016-09-16 13:49:30 +01005495 /**
5496 * Let embedder know v8 entered final marking pause (no more incremental steps
5497 * will follow).
5498 */
5499 virtual void EnterFinalPause() {}
5500
5501 /**
5502 * Throw away all intermediate data and reset to the initial state.
5503 */
5504 virtual void AbortTracing() {}
5505
Ben Murdochda12d292016-06-02 14:46:10 +01005506 protected:
5507 virtual ~EmbedderHeapTracer() = default;
5508};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005509
5510/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005511 * Isolate represents an isolated instance of the V8 engine. V8 isolates have
5512 * completely separate states. Objects from one isolate must not be used in
5513 * other isolates. The embedder can create multiple isolates and use them in
5514 * parallel in multiple threads. An isolate can be entered by at most one
5515 * thread at any given time. The Locker/Unlocker API must be used to
5516 * synchronize.
5517 */
5518class V8_EXPORT Isolate {
Steve Block44f0eee2011-05-26 01:26:41 +01005519 public:
5520 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005521 * Initial configuration parameters for a new Isolate.
5522 */
5523 struct CreateParams {
5524 CreateParams()
Ben Murdoch61f157c2016-09-16 13:49:30 +01005525 : entry_hook(nullptr),
5526 code_event_handler(nullptr),
5527 snapshot_blob(nullptr),
5528 counter_lookup_callback(nullptr),
5529 create_histogram_callback(nullptr),
5530 add_histogram_sample_callback(nullptr),
5531 array_buffer_allocator(nullptr),
5532 external_references(nullptr) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005533
5534 /**
5535 * The optional entry_hook allows the host application to provide the
5536 * address of a function that's invoked on entry to every V8-generated
5537 * function. Note that entry_hook is invoked at the very start of each
Ben Murdoch61f157c2016-09-16 13:49:30 +01005538 * generated function. Furthermore, if an entry_hook is given, V8 will
5539 * not use a snapshot, including custom snapshots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005540 */
5541 FunctionEntryHook entry_hook;
5542
5543 /**
5544 * Allows the host application to provide the address of a function that is
5545 * notified each time code is added, moved or removed.
5546 */
5547 JitCodeEventHandler code_event_handler;
5548
5549 /**
5550 * ResourceConstraints to use for the new Isolate.
5551 */
5552 ResourceConstraints constraints;
5553
5554 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005555 * Explicitly specify a startup snapshot blob. The embedder owns the blob.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005556 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005557 StartupData* snapshot_blob;
5558
5559
5560 /**
5561 * Enables the host application to provide a mechanism for recording
5562 * statistics counters.
5563 */
5564 CounterLookupCallback counter_lookup_callback;
5565
5566 /**
5567 * Enables the host application to provide a mechanism for recording
5568 * histograms. The CreateHistogram function returns a
5569 * histogram which will later be passed to the AddHistogramSample
5570 * function.
5571 */
5572 CreateHistogramCallback create_histogram_callback;
5573 AddHistogramSampleCallback add_histogram_sample_callback;
5574
5575 /**
5576 * The ArrayBuffer::Allocator to use for allocating and freeing the backing
5577 * store of ArrayBuffers.
5578 */
5579 ArrayBuffer::Allocator* array_buffer_allocator;
Ben Murdoch61f157c2016-09-16 13:49:30 +01005580
5581 /**
5582 * Specifies an optional nullptr-terminated array of raw addresses in the
5583 * embedder that V8 can match against during serialization and use for
5584 * deserialization. This array and its content must stay valid for the
5585 * entire lifetime of the isolate.
5586 */
5587 intptr_t* external_references;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005588 };
5589
5590
5591 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005592 * Stack-allocated class which sets the isolate for all operations
5593 * executed within a local scope.
5594 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005595 class V8_EXPORT Scope {
Steve Block44f0eee2011-05-26 01:26:41 +01005596 public:
5597 explicit Scope(Isolate* isolate) : isolate_(isolate) {
5598 isolate->Enter();
5599 }
5600
5601 ~Scope() { isolate_->Exit(); }
5602
5603 private:
5604 Isolate* const isolate_;
5605
5606 // Prevent copying of Scope objects.
5607 Scope(const Scope&);
5608 Scope& operator=(const Scope&);
5609 };
5610
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005611
5612 /**
5613 * Assert that no Javascript code is invoked.
5614 */
5615 class V8_EXPORT DisallowJavascriptExecutionScope {
5616 public:
5617 enum OnFailure { CRASH_ON_FAILURE, THROW_ON_FAILURE };
5618
5619 DisallowJavascriptExecutionScope(Isolate* isolate, OnFailure on_failure);
5620 ~DisallowJavascriptExecutionScope();
5621
5622 private:
5623 bool on_failure_;
5624 void* internal_;
5625
5626 // Prevent copying of Scope objects.
5627 DisallowJavascriptExecutionScope(const DisallowJavascriptExecutionScope&);
5628 DisallowJavascriptExecutionScope& operator=(
5629 const DisallowJavascriptExecutionScope&);
5630 };
5631
5632
5633 /**
5634 * Introduce exception to DisallowJavascriptExecutionScope.
5635 */
5636 class V8_EXPORT AllowJavascriptExecutionScope {
5637 public:
5638 explicit AllowJavascriptExecutionScope(Isolate* isolate);
5639 ~AllowJavascriptExecutionScope();
5640
5641 private:
5642 void* internal_throws_;
5643 void* internal_assert_;
5644
5645 // Prevent copying of Scope objects.
5646 AllowJavascriptExecutionScope(const AllowJavascriptExecutionScope&);
5647 AllowJavascriptExecutionScope& operator=(
5648 const AllowJavascriptExecutionScope&);
5649 };
5650
5651 /**
5652 * Do not run microtasks while this scope is active, even if microtasks are
5653 * automatically executed otherwise.
5654 */
5655 class V8_EXPORT SuppressMicrotaskExecutionScope {
5656 public:
5657 explicit SuppressMicrotaskExecutionScope(Isolate* isolate);
5658 ~SuppressMicrotaskExecutionScope();
5659
5660 private:
5661 internal::Isolate* isolate_;
5662
5663 // Prevent copying of Scope objects.
5664 SuppressMicrotaskExecutionScope(const SuppressMicrotaskExecutionScope&);
5665 SuppressMicrotaskExecutionScope& operator=(
5666 const SuppressMicrotaskExecutionScope&);
5667 };
5668
5669 /**
5670 * Types of garbage collections that can be requested via
5671 * RequestGarbageCollectionForTesting.
5672 */
5673 enum GarbageCollectionType {
5674 kFullGarbageCollection,
5675 kMinorGarbageCollection
5676 };
5677
5678 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005679 * Features reported via the SetUseCounterCallback callback. Do not change
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005680 * assigned numbers of existing items; add new features to the end of this
5681 * list.
5682 */
5683 enum UseCounterFeature {
5684 kUseAsm = 0,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005685 kBreakIterator = 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005686 kLegacyConst = 2,
5687 kMarkDequeOverflow = 3,
5688 kStoreBufferOverflow = 4,
5689 kSlotsBufferOverflow = 5,
5690 kObjectObserve = 6,
5691 kForcedGC = 7,
5692 kSloppyMode = 8,
5693 kStrictMode = 9,
5694 kStrongMode = 10,
5695 kRegExpPrototypeStickyGetter = 11,
5696 kRegExpPrototypeToString = 12,
5697 kRegExpPrototypeUnicodeGetter = 13,
5698 kIntlV8Parse = 14,
5699 kIntlPattern = 15,
5700 kIntlResolved = 16,
5701 kPromiseChain = 17,
5702 kPromiseAccept = 18,
5703 kPromiseDefer = 19,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005704 kHtmlCommentInExternalScript = 20,
5705 kHtmlComment = 21,
5706 kSloppyModeBlockScopedFunctionRedefinition = 22,
5707 kForInInitializer = 23,
5708 kArrayProtectorDirtied = 24,
5709 kArraySpeciesModified = 25,
5710 kArrayPrototypeConstructorModified = 26,
5711 kArrayInstanceProtoModified = 27,
5712 kArrayInstanceConstructorModified = 28,
Ben Murdochda12d292016-06-02 14:46:10 +01005713 kLegacyFunctionDeclaration = 29,
5714 kRegExpPrototypeSourceGetter = 30,
5715 kRegExpPrototypeOldFlagGetter = 31,
Ben Murdochc5610432016-08-08 18:44:38 +01005716 kDecimalWithLeadingZeroInStrictMode = 32,
Ben Murdoch61f157c2016-09-16 13:49:30 +01005717 kLegacyDateParser = 33,
5718 kDefineGetterOrSetterWouldThrow = 34,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005719
Ben Murdochc5610432016-08-08 18:44:38 +01005720 // If you add new values here, you'll also need to update Chromium's:
5721 // UseCounter.h, V8PerIsolateData.cpp, histograms.xml
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005722 kUseCounterFeatureCount // This enum value must be last.
5723 };
5724
5725 typedef void (*UseCounterCallback)(Isolate* isolate,
5726 UseCounterFeature feature);
5727
5728
Steve Block44f0eee2011-05-26 01:26:41 +01005729 /**
5730 * Creates a new isolate. Does not change the currently entered
5731 * isolate.
5732 *
5733 * When an isolate is no longer used its resources should be freed
5734 * by calling Dispose(). Using the delete operator is not allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005735 *
5736 * V8::Initialize() must have run prior to this.
Steve Block44f0eee2011-05-26 01:26:41 +01005737 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005738 static Isolate* New(const CreateParams& params);
Steve Block44f0eee2011-05-26 01:26:41 +01005739
5740 /**
5741 * Returns the entered isolate for the current thread or NULL in
5742 * case there is no current isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005743 *
5744 * This method must not be invoked before V8::Initialize() was invoked.
Steve Block44f0eee2011-05-26 01:26:41 +01005745 */
5746 static Isolate* GetCurrent();
5747
5748 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005749 * Custom callback used by embedders to help V8 determine if it should abort
5750 * when it throws and no internal handler is predicted to catch the
5751 * exception. If --abort-on-uncaught-exception is used on the command line,
5752 * then V8 will abort if either:
5753 * - no custom callback is set.
5754 * - the custom callback set returns true.
5755 * Otherwise, the custom callback will not be called and V8 will not abort.
5756 */
5757 typedef bool (*AbortOnUncaughtExceptionCallback)(Isolate*);
5758 void SetAbortOnUncaughtExceptionCallback(
5759 AbortOnUncaughtExceptionCallback callback);
5760
5761 /**
Ben Murdochda12d292016-06-02 14:46:10 +01005762 * Optional notification that the system is running low on memory.
5763 * V8 uses these notifications to guide heuristics.
5764 * It is allowed to call this function from another thread while
5765 * the isolate is executing long running JavaScript code.
5766 */
5767 void MemoryPressureNotification(MemoryPressureLevel level);
5768
5769 /**
Steve Block44f0eee2011-05-26 01:26:41 +01005770 * Methods below this point require holding a lock (using Locker) in
5771 * a multi-threaded environment.
5772 */
5773
5774 /**
5775 * Sets this isolate as the entered one for the current thread.
5776 * Saves the previously entered one (if any), so that it can be
5777 * restored when exiting. Re-entering an isolate is allowed.
5778 */
5779 void Enter();
5780
5781 /**
5782 * Exits this isolate by restoring the previously entered one in the
5783 * current thread. The isolate may still stay the same, if it was
5784 * entered more than once.
5785 *
5786 * Requires: this == Isolate::GetCurrent().
5787 */
5788 void Exit();
5789
5790 /**
5791 * Disposes the isolate. The isolate must not be entered by any
5792 * thread to be disposable.
5793 */
5794 void Dispose();
5795
Ben Murdoch257744e2011-11-30 15:57:28 +00005796 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005797 * Discards all V8 thread-specific data for the Isolate. Should be used
5798 * if a thread is terminating and it has used an Isolate that will outlive
5799 * the thread -- all thread-specific data for an Isolate is discarded when
5800 * an Isolate is disposed so this call is pointless if an Isolate is about
5801 * to be Disposed.
5802 */
5803 void DiscardThreadSpecificMetadata();
5804
5805 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005806 * Associate embedder-specific data with the isolate. |slot| has to be
5807 * between 0 and GetNumberOfDataSlots() - 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00005808 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005809 V8_INLINE void SetData(uint32_t slot, void* data);
Ben Murdoch257744e2011-11-30 15:57:28 +00005810
5811 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005812 * Retrieve embedder-specific data from the isolate.
5813 * Returns NULL if SetData has never been called for the given |slot|.
Ben Murdoch257744e2011-11-30 15:57:28 +00005814 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005815 V8_INLINE void* GetData(uint32_t slot);
5816
5817 /**
5818 * Returns the maximum number of available embedder data slots. Valid slots
5819 * are in the range of 0 - GetNumberOfDataSlots() - 1.
5820 */
5821 V8_INLINE static uint32_t GetNumberOfDataSlots();
5822
5823 /**
5824 * Get statistics about the heap memory usage.
5825 */
5826 void GetHeapStatistics(HeapStatistics* heap_statistics);
5827
5828 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005829 * Returns the number of spaces in the heap.
5830 */
5831 size_t NumberOfHeapSpaces();
5832
5833 /**
5834 * Get the memory usage of a space in the heap.
5835 *
5836 * \param space_statistics The HeapSpaceStatistics object to fill in
5837 * statistics.
5838 * \param index The index of the space to get statistics from, which ranges
5839 * from 0 to NumberOfHeapSpaces() - 1.
5840 * \returns true on success.
5841 */
5842 bool GetHeapSpaceStatistics(HeapSpaceStatistics* space_statistics,
5843 size_t index);
5844
5845 /**
5846 * Returns the number of types of objects tracked in the heap at GC.
5847 */
5848 size_t NumberOfTrackedHeapObjectTypes();
5849
5850 /**
5851 * Get statistics about objects in the heap.
5852 *
5853 * \param object_statistics The HeapObjectStatistics object to fill in
5854 * statistics of objects of given type, which were live in the previous GC.
5855 * \param type_index The index of the type of object to fill details about,
5856 * which ranges from 0 to NumberOfTrackedHeapObjectTypes() - 1.
5857 * \returns true on success.
5858 */
5859 bool GetHeapObjectStatisticsAtLastGC(HeapObjectStatistics* object_statistics,
5860 size_t type_index);
5861
5862 /**
Ben Murdoch61f157c2016-09-16 13:49:30 +01005863 * Get statistics about code and its metadata in the heap.
5864 *
5865 * \param object_statistics The HeapCodeStatistics object to fill in
5866 * statistics of code, bytecode and their metadata.
5867 * \returns true on success.
5868 */
5869 bool GetHeapCodeAndMetadataStatistics(HeapCodeStatistics* object_statistics);
5870
5871 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005872 * Get a call stack sample from the isolate.
5873 * \param state Execution state.
5874 * \param frames Caller allocated buffer to store stack frames.
5875 * \param frames_limit Maximum number of frames to capture. The buffer must
5876 * be large enough to hold the number of frames.
5877 * \param sample_info The sample info is filled up by the function
5878 * provides number of actual captured stack frames and
5879 * the current VM state.
5880 * \note GetStackSample should only be called when the JS thread is paused or
5881 * interrupted. Otherwise the behavior is undefined.
5882 */
5883 void GetStackSample(const RegisterState& state, void** frames,
5884 size_t frames_limit, SampleInfo* sample_info);
5885
5886 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005887 * Adjusts the amount of registered external memory. Used to give V8 an
5888 * indication of the amount of externally allocated memory that is kept alive
5889 * by JavaScript objects. V8 uses this to decide when to perform global
5890 * garbage collections. Registering externally allocated memory will trigger
5891 * global garbage collections more often than it would otherwise in an attempt
5892 * to garbage collect the JavaScript objects that keep the externally
5893 * allocated memory alive.
5894 *
5895 * \param change_in_bytes the change in externally allocated memory that is
5896 * kept alive by JavaScript objects.
5897 * \returns the adjusted value.
5898 */
5899 V8_INLINE int64_t
5900 AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
5901
5902 /**
Ben Murdochc5610432016-08-08 18:44:38 +01005903 * Returns the number of phantom handles without callbacks that were reset
5904 * by the garbage collector since the last call to this function.
5905 */
5906 size_t NumberOfPhantomHandleResetsSinceLastCall();
5907
5908 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005909 * Returns heap profiler for this isolate. Will return NULL until the isolate
5910 * is initialized.
5911 */
5912 HeapProfiler* GetHeapProfiler();
5913
5914 /**
5915 * Returns CPU profiler for this isolate. Will return NULL unless the isolate
5916 * is initialized. It is the embedder's responsibility to stop all CPU
5917 * profiling activities if it has started any.
5918 */
5919 CpuProfiler* GetCpuProfiler();
5920
5921 /** Returns true if this isolate has a current context. */
5922 bool InContext();
5923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005924 /**
5925 * Returns the context of the currently running JavaScript, or the context
5926 * on the top of the stack if no JavaScript is running.
5927 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005928 Local<Context> GetCurrentContext();
5929
5930 /**
5931 * Returns the context of the calling JavaScript code. That is the
5932 * context of the top-most JavaScript frame. If there are no
5933 * JavaScript frames an empty handle is returned.
5934 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005935 V8_DEPRECATE_SOON(
5936 "Calling context concept is not compatible with tail calls, and will be "
5937 "removed.",
5938 Local<Context> GetCallingContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005939
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005940 /** Returns the last context entered through V8's C++ API. */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005941 Local<Context> GetEnteredContext();
5942
5943 /**
5944 * Schedules an exception to be thrown when returning to JavaScript. When an
5945 * exception has been scheduled it is illegal to invoke any JavaScript
5946 * operation; the caller must return immediately and only after the exception
5947 * has been handled does it become legal to invoke JavaScript operations.
5948 */
5949 Local<Value> ThrowException(Local<Value> exception);
5950
5951 /**
5952 * Allows the host application to group objects together. If one
5953 * object in the group is alive, all objects in the group are alive.
5954 * After each garbage collection, object groups are removed. It is
5955 * intended to be used in the before-garbage-collection callback
5956 * function, for instance to simulate DOM tree connections among JS
5957 * wrapper objects. Object groups for all dependent handles need to
5958 * be provided for kGCTypeMarkSweepCompact collections, for all other
5959 * garbage collection types it is sufficient to provide object groups
5960 * for partially dependent handles only.
5961 */
5962 template<typename T> void SetObjectGroupId(const Persistent<T>& object,
5963 UniqueId id);
5964
5965 /**
5966 * Allows the host application to declare implicit references from an object
5967 * group to an object. If the objects of the object group are alive, the child
5968 * object is alive too. After each garbage collection, all implicit references
5969 * are removed. It is intended to be used in the before-garbage-collection
5970 * callback function.
5971 */
5972 template<typename T> void SetReferenceFromGroup(UniqueId id,
5973 const Persistent<T>& child);
5974
5975 /**
5976 * Allows the host application to declare implicit references from an object
5977 * to another object. If the parent object is alive, the child object is alive
5978 * too. After each garbage collection, all implicit references are removed. It
5979 * is intended to be used in the before-garbage-collection callback function.
5980 */
5981 template<typename T, typename S>
5982 void SetReference(const Persistent<T>& parent, const Persistent<S>& child);
5983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005984 typedef void (*GCCallback)(Isolate* isolate, GCType type,
5985 GCCallbackFlags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005986
5987 /**
5988 * Enables the host application to receive a notification before a
5989 * garbage collection. Allocations are allowed in the callback function,
5990 * but the callback is not re-entrant: if the allocation inside it will
5991 * trigger the garbage collection, the callback won't be called again.
5992 * It is possible to specify the GCType filter for your callback. But it is
5993 * not possible to register the same callback function two times with
5994 * different GCType filters.
5995 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005996 void AddGCPrologueCallback(GCCallback callback,
5997 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005998
5999 /**
6000 * This function removes callback which was installed by
6001 * AddGCPrologueCallback function.
6002 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006003 void RemoveGCPrologueCallback(GCCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006004
6005 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006006 * Sets the embedder heap tracer for the isolate.
6007 */
6008 void SetEmbedderHeapTracer(EmbedderHeapTracer* tracer);
6009
6010 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006011 * Enables the host application to receive a notification after a
6012 * garbage collection. Allocations are allowed in the callback function,
6013 * but the callback is not re-entrant: if the allocation inside it will
6014 * trigger the garbage collection, the callback won't be called again.
6015 * It is possible to specify the GCType filter for your callback. But it is
6016 * not possible to register the same callback function two times with
6017 * different GCType filters.
6018 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006019 void AddGCEpilogueCallback(GCCallback callback,
6020 GCType gc_type_filter = kGCTypeAll);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006021
6022 /**
6023 * This function removes callback which was installed by
6024 * AddGCEpilogueCallback function.
6025 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006026 void RemoveGCEpilogueCallback(GCCallback callback);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006027
6028 /**
6029 * Forcefully terminate the current thread of JavaScript execution
6030 * in the given isolate.
6031 *
6032 * This method can be used by any thread even if that thread has not
6033 * acquired the V8 lock with a Locker object.
6034 */
6035 void TerminateExecution();
6036
6037 /**
6038 * Is V8 terminating JavaScript execution.
6039 *
6040 * Returns true if JavaScript execution is currently terminating
6041 * because of a call to TerminateExecution. In that case there are
6042 * still JavaScript frames on the stack and the termination
6043 * exception is still active.
6044 */
6045 bool IsExecutionTerminating();
6046
6047 /**
6048 * Resume execution capability in the given isolate, whose execution
6049 * was previously forcefully terminated using TerminateExecution().
6050 *
6051 * When execution is forcefully terminated using TerminateExecution(),
6052 * the isolate can not resume execution until all JavaScript frames
6053 * have propagated the uncatchable exception which is generated. This
6054 * method allows the program embedding the engine to handle the
6055 * termination event and resume execution capability, even if
6056 * JavaScript frames remain on the stack.
6057 *
6058 * This method can be used by any thread even if that thread has not
6059 * acquired the V8 lock with a Locker object.
6060 */
6061 void CancelTerminateExecution();
6062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006063 /**
6064 * Request V8 to interrupt long running JavaScript code and invoke
6065 * the given |callback| passing the given |data| to it. After |callback|
6066 * returns control will be returned to the JavaScript code.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006067 * There may be a number of interrupt requests in flight.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006068 * Can be called from another thread without acquiring a |Locker|.
6069 * Registered |callback| must not reenter interrupted Isolate.
6070 */
6071 void RequestInterrupt(InterruptCallback callback, void* data);
6072
6073 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006074 * Request garbage collection in this Isolate. It is only valid to call this
6075 * function if --expose_gc was specified.
6076 *
6077 * This should only be used for testing purposes and not to enforce a garbage
6078 * collection schedule. It has strong negative impact on the garbage
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006079 * collection performance. Use IdleNotificationDeadline() or
6080 * LowMemoryNotification() instead to influence the garbage collection
6081 * schedule.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006082 */
6083 void RequestGarbageCollectionForTesting(GarbageCollectionType type);
6084
6085 /**
6086 * Set the callback to invoke for logging event.
6087 */
6088 void SetEventLogger(LogEventCallback that);
6089
6090 /**
Ben Murdoch097c5b22016-05-18 11:27:45 +01006091 * Adds a callback to notify the host application right before a script
6092 * is about to run. If a script re-enters the runtime during executing, the
6093 * BeforeCallEnteredCallback is invoked for each re-entrance.
6094 * Executing scripts inside the callback will re-trigger the callback.
6095 */
6096 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
6097
6098 /**
6099 * Removes callback that was installed by AddBeforeCallEnteredCallback.
6100 */
6101 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
6102
6103 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006104 * Adds a callback to notify the host application when a script finished
6105 * running. If a script re-enters the runtime during executing, the
6106 * CallCompletedCallback is only invoked when the outer-most script
6107 * execution ends. Executing scripts inside the callback do not trigger
6108 * further callbacks.
6109 */
6110 void AddCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006111 V8_DEPRECATE_SOON(
6112 "Use callback with parameter",
6113 void AddCallCompletedCallback(DeprecatedCallCompletedCallback callback));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006114
6115 /**
6116 * Removes callback that was installed by AddCallCompletedCallback.
6117 */
6118 void RemoveCallCompletedCallback(CallCompletedCallback callback);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006119 V8_DEPRECATE_SOON(
6120 "Use callback with parameter",
6121 void RemoveCallCompletedCallback(
6122 DeprecatedCallCompletedCallback callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006123
6124 /**
6125 * Set callback to notify about promise reject with no handler, or
6126 * revocation of such a previous notification once the handler is added.
6127 */
6128 void SetPromiseRejectCallback(PromiseRejectCallback callback);
6129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006130 /**
6131 * Experimental: Runs the Microtask Work Queue until empty
6132 * Any exceptions thrown by microtask callbacks are swallowed.
6133 */
6134 void RunMicrotasks();
6135
6136 /**
6137 * Experimental: Enqueues the callback to the Microtask Work Queue
6138 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006139 void EnqueueMicrotask(Local<Function> microtask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006140
6141 /**
6142 * Experimental: Enqueues the callback to the Microtask Work Queue
6143 */
6144 void EnqueueMicrotask(MicrotaskCallback microtask, void* data = NULL);
6145
Ben Murdochda12d292016-06-02 14:46:10 +01006146 /**
6147 * Experimental: Controls how Microtasks are invoked. See MicrotasksPolicy
6148 * for details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006149 */
Ben Murdochda12d292016-06-02 14:46:10 +01006150 void SetMicrotasksPolicy(MicrotasksPolicy policy);
6151 V8_DEPRECATE_SOON("Use SetMicrotasksPolicy",
6152 void SetAutorunMicrotasks(bool autorun));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006153
6154 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006155 * Experimental: Returns the policy controlling how Microtasks are invoked.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006156 */
Ben Murdochda12d292016-06-02 14:46:10 +01006157 MicrotasksPolicy GetMicrotasksPolicy() const;
6158 V8_DEPRECATE_SOON("Use GetMicrotasksPolicy",
6159 bool WillAutorunMicrotasks() const);
6160
6161 /**
6162 * Experimental: adds a callback to notify the host application after
6163 * microtasks were run. The callback is triggered by explicit RunMicrotasks
6164 * call or automatic microtasks execution (see SetAutorunMicrotasks).
6165 *
6166 * Callback will trigger even if microtasks were attempted to run,
6167 * but the microtasks queue was empty and no single microtask was actually
6168 * executed.
6169 *
6170 * Executing scriptsinside the callback will not re-trigger microtasks and
6171 * the callback.
6172 */
6173 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
6174
6175 /**
6176 * Removes callback that was installed by AddMicrotasksCompletedCallback.
6177 */
6178 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006179
6180 /**
6181 * Sets a callback for counting the number of times a feature of V8 is used.
6182 */
6183 void SetUseCounterCallback(UseCounterCallback callback);
6184
6185 /**
6186 * Enables the host application to provide a mechanism for recording
6187 * statistics counters.
6188 */
6189 void SetCounterFunction(CounterLookupCallback);
6190
6191 /**
6192 * Enables the host application to provide a mechanism for recording
6193 * histograms. The CreateHistogram function returns a
6194 * histogram which will later be passed to the AddHistogramSample
6195 * function.
6196 */
6197 void SetCreateHistogramFunction(CreateHistogramCallback);
6198 void SetAddHistogramSampleFunction(AddHistogramSampleCallback);
6199
6200 /**
6201 * Optional notification that the embedder is idle.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006202 * V8 uses the notification to perform garbage collection.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006203 * This call can be used repeatedly if the embedder remains idle.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006204 * Returns true if the embedder should stop calling IdleNotificationDeadline
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006205 * until real work has been done. This indicates that V8 has done
6206 * as much cleanup as it will be able to do.
6207 *
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006208 * The deadline_in_seconds argument specifies the deadline V8 has to finish
6209 * garbage collection work. deadline_in_seconds is compared with
6210 * MonotonicallyIncreasingTime() and should be based on the same timebase as
6211 * that function. There is no guarantee that the actual work will be done
6212 * within the time limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006213 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006214 bool IdleNotificationDeadline(double deadline_in_seconds);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006215
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006216 V8_DEPRECATED("use IdleNotificationDeadline()",
6217 bool IdleNotification(int idle_time_in_ms));
6218
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006219 /**
6220 * Optional notification that the system is running low on memory.
6221 * V8 uses these notifications to attempt to free memory.
6222 */
6223 void LowMemoryNotification();
6224
6225 /**
6226 * Optional notification that a context has been disposed. V8 uses
6227 * these notifications to guide the GC heuristic. Returns the number
6228 * of context disposals - including this one - since the last time
6229 * V8 had a chance to clean up.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006230 *
6231 * The optional parameter |dependant_context| specifies whether the disposed
6232 * context was depending on state from other contexts or not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006233 */
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006234 int ContextDisposedNotification(bool dependant_context = true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006235
6236 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006237 * Optional notification that the isolate switched to the foreground.
6238 * V8 uses these notifications to guide heuristics.
6239 */
6240 void IsolateInForegroundNotification();
6241
6242 /**
6243 * Optional notification that the isolate switched to the background.
6244 * V8 uses these notifications to guide heuristics.
6245 */
6246 void IsolateInBackgroundNotification();
6247
6248 /**
Ben Murdoch61f157c2016-09-16 13:49:30 +01006249 * Optional notification to tell V8 the current performance requirements
6250 * of the embedder based on RAIL.
6251 * V8 uses these notifications to guide heuristics.
6252 * This is an unfinished experimental feature. Semantics and implementation
6253 * may change frequently.
6254 */
6255 void SetRAILMode(RAILMode rail_mode);
6256
6257 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006258 * Allows the host application to provide the address of a function that is
6259 * notified each time code is added, moved or removed.
6260 *
6261 * \param options options for the JIT code event handler.
6262 * \param event_handler the JIT code event handler, which will be invoked
6263 * each time code is added, moved or removed.
6264 * \note \p event_handler won't get notified of existent code.
6265 * \note since code removal notifications are not currently issued, the
6266 * \p event_handler may get notifications of code that overlaps earlier
6267 * code notifications. This happens when code areas are reused, and the
6268 * earlier overlapping code areas should therefore be discarded.
6269 * \note the events passed to \p event_handler and the strings they point to
6270 * are not guaranteed to live past each call. The \p event_handler must
6271 * copy strings and other parameters it needs to keep around.
6272 * \note the set of events declared in JitCodeEvent::EventType is expected to
6273 * grow over time, and the JitCodeEvent structure is expected to accrue
6274 * new members. The \p event_handler function must ignore event codes
6275 * it does not recognize to maintain future compatibility.
6276 * \note Use Isolate::CreateParams to get events for code executed during
6277 * Isolate setup.
6278 */
6279 void SetJitCodeEventHandler(JitCodeEventOptions options,
6280 JitCodeEventHandler event_handler);
6281
6282 /**
6283 * Modifies the stack limit for this Isolate.
6284 *
6285 * \param stack_limit An address beyond which the Vm's stack may not grow.
6286 *
6287 * \note If you are using threads then you should hold the V8::Locker lock
6288 * while setting the stack limit and you must set a non-default stack
6289 * limit separately for each thread.
6290 */
6291 void SetStackLimit(uintptr_t stack_limit);
Ben Murdoch257744e2011-11-30 15:57:28 +00006292
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006293 /**
6294 * Returns a memory range that can potentially contain jitted code.
6295 *
6296 * On Win64, embedders are advised to install function table callbacks for
6297 * these ranges, as default SEH won't be able to unwind through jitted code.
6298 *
6299 * The first page of the code range is reserved for the embedder and is
6300 * committed, writable, and executable.
6301 *
6302 * Might be empty on other platforms.
6303 *
6304 * https://code.google.com/p/v8/issues/detail?id=3598
6305 */
6306 void GetCodeRange(void** start, size_t* length_in_bytes);
6307
6308 /** Set the callback to invoke in case of fatal errors. */
6309 void SetFatalErrorHandler(FatalErrorCallback that);
6310
6311 /**
6312 * Set the callback to invoke to check if code generation from
6313 * strings should be allowed.
6314 */
6315 void SetAllowCodeGenerationFromStringsCallback(
6316 AllowCodeGenerationFromStringsCallback callback);
6317
6318 /**
6319 * Check if V8 is dead and therefore unusable. This is the case after
6320 * fatal errors such as out-of-memory situations.
6321 */
6322 bool IsDead();
6323
6324 /**
6325 * Adds a message listener.
6326 *
6327 * The same message listener can be added more than once and in that
6328 * case it will be called more than once for each message.
6329 *
6330 * If data is specified, it will be passed to the callback when it is called.
6331 * Otherwise, the exception object will be passed to the callback instead.
6332 */
6333 bool AddMessageListener(MessageCallback that,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006334 Local<Value> data = Local<Value>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006335
6336 /**
6337 * Remove all message listeners from the specified callback function.
6338 */
6339 void RemoveMessageListeners(MessageCallback that);
6340
6341 /** Callback function for reporting failed access checks.*/
6342 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback);
6343
6344 /**
6345 * Tells V8 to capture current stack trace when uncaught exception occurs
6346 * and report it to the message listeners. The option is off by default.
6347 */
6348 void SetCaptureStackTraceForUncaughtExceptions(
6349 bool capture, int frame_limit = 10,
6350 StackTrace::StackTraceOptions options = StackTrace::kOverview);
6351
6352 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006353 * Iterates through all external resources referenced from current isolate
6354 * heap. GC is not invoked prior to iterating, therefore there is no
6355 * guarantee that visited objects are still alive.
6356 */
6357 void VisitExternalResources(ExternalResourceVisitor* visitor);
6358
6359 /**
6360 * Iterates through all the persistent handles in the current isolate's heap
6361 * that have class_ids.
6362 */
6363 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor);
6364
6365 /**
6366 * Iterates through all the persistent handles in the current isolate's heap
6367 * that have class_ids and are candidates to be marked as partially dependent
6368 * handles. This will visit handles to young objects created since the last
6369 * garbage collection but is free to visit an arbitrary superset of these
6370 * objects.
6371 */
6372 void VisitHandlesForPartialDependence(PersistentHandleVisitor* visitor);
6373
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006374 /**
6375 * Iterates through all the persistent handles in the current isolate's heap
6376 * that have class_ids and are weak to be marked as inactive if there is no
6377 * pending activity for the handle.
6378 */
6379 void VisitWeakHandles(PersistentHandleVisitor* visitor);
6380
Ben Murdoch61f157c2016-09-16 13:49:30 +01006381 /**
6382 * Check if this isolate is in use.
6383 * True if at least one thread Enter'ed this isolate.
6384 */
6385 bool IsInUse();
6386
Steve Block44f0eee2011-05-26 01:26:41 +01006387 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006388 template <class K, class V, class Traits>
6389 friend class PersistentValueMapBase;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006390
Steve Block44f0eee2011-05-26 01:26:41 +01006391 Isolate();
6392 Isolate(const Isolate&);
6393 ~Isolate();
6394 Isolate& operator=(const Isolate&);
6395 void* operator new(size_t size);
6396 void operator delete(void*, size_t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006397
6398 void SetObjectGroupId(internal::Object** object, UniqueId id);
6399 void SetReferenceFromGroup(UniqueId id, internal::Object** object);
6400 void SetReference(internal::Object** parent, internal::Object** child);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006401 void ReportExternalAllocationLimitReached();
Steve Block44f0eee2011-05-26 01:26:41 +01006402};
6403
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006404class V8_EXPORT StartupData {
Ben Murdoch257744e2011-11-30 15:57:28 +00006405 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006406 const char* data;
Ben Murdoch257744e2011-11-30 15:57:28 +00006407 int raw_size;
6408};
6409
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006410
6411/**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006412 * EntropySource is used as a callback function when v8 needs a source
6413 * of entropy.
6414 */
6415typedef bool (*EntropySource)(unsigned char* buffer, size_t length);
6416
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006417
6418/**
6419 * ReturnAddressLocationResolver is used as a callback function when v8 is
6420 * resolving the location of a return address on the stack. Profilers that
6421 * change the return address on the stack can use this to resolve the stack
6422 * location to whereever the profiler stashed the original return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006423 *
6424 * \param return_addr_location points to a location on stack where a machine
6425 * return address resides.
6426 * \returns either return_addr_location, or else a pointer to the profiler's
6427 * copy of the original return address.
6428 *
6429 * \note the resolver function must not cause garbage collection.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006430 */
6431typedef uintptr_t (*ReturnAddressLocationResolver)(
6432 uintptr_t return_addr_location);
6433
6434
6435/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006436 * Container class for static utility functions.
6437 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006438class V8_EXPORT V8 {
Steve Blocka7e24c12009-10-30 11:49:00 +00006439 public:
6440 /** Set the callback to invoke in case of fatal errors. */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006441 V8_INLINE static V8_DEPRECATED(
6442 "Use isolate version",
6443 void SetFatalErrorHandler(FatalErrorCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006444
6445 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00006446 * Set the callback to invoke to check if code generation from
6447 * strings should be allowed.
6448 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006449 V8_INLINE static V8_DEPRECATED(
6450 "Use isolate version", void SetAllowCodeGenerationFromStringsCallback(
6451 AllowCodeGenerationFromStringsCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006452
6453 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006454 * Check if V8 is dead and therefore unusable. This is the case after
6455 * fatal errors such as out-of-memory situations.
6456 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006457 V8_INLINE static V8_DEPRECATED("Use isolate version", bool IsDead());
Ben Murdoch257744e2011-11-30 15:57:28 +00006458
6459 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006460 * Hand startup data to V8, in case the embedder has chosen to build
6461 * V8 with external startup data.
6462 *
6463 * Note:
6464 * - By default the startup data is linked into the V8 library, in which
6465 * case this function is not meaningful.
6466 * - If this needs to be called, it needs to be called before V8
6467 * tries to make use of its built-ins.
6468 * - To avoid unnecessary copies of data, V8 will point directly into the
6469 * given data blob, so pretty please keep it around until V8 exit.
6470 * - Compression of the startup blob might be useful, but needs to
6471 * handled entirely on the embedders' side.
6472 * - The call will abort if the data is invalid.
6473 */
6474 static void SetNativesDataBlob(StartupData* startup_blob);
6475 static void SetSnapshotDataBlob(StartupData* startup_blob);
6476
6477 /**
Ben Murdochda12d292016-06-02 14:46:10 +01006478 * Bootstrap an isolate and a context from scratch to create a startup
6479 * snapshot. Include the side-effects of running the optional script.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006480 * Returns { NULL, 0 } on failure.
Ben Murdochda12d292016-06-02 14:46:10 +01006481 * The caller acquires ownership of the data array in the return value.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006482 */
Ben Murdochda12d292016-06-02 14:46:10 +01006483 static StartupData CreateSnapshotDataBlob(const char* embedded_source = NULL);
6484
6485 /**
6486 * Bootstrap an isolate and a context from the cold startup blob, run the
6487 * warm-up script to trigger code compilation. The side effects are then
6488 * discarded. The resulting startup snapshot will include compiled code.
6489 * Returns { NULL, 0 } on failure.
6490 * The caller acquires ownership of the data array in the return value.
6491 * The argument startup blob is untouched.
6492 */
6493 static StartupData WarmUpSnapshotDataBlob(StartupData cold_startup_blob,
6494 const char* warmup_source);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006495
6496 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006497 * Adds a message listener.
6498 *
Ben Murdoch257744e2011-11-30 15:57:28 +00006499 * The same message listener can be added more than once and in that
Steve Blocka7e24c12009-10-30 11:49:00 +00006500 * case it will be called more than once for each message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006501 *
6502 * If data is specified, it will be passed to the callback when it is called.
6503 * Otherwise, the exception object will be passed to the callback instead.
Steve Blocka7e24c12009-10-30 11:49:00 +00006504 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006505 V8_INLINE static V8_DEPRECATED(
6506 "Use isolate version",
6507 bool AddMessageListener(MessageCallback that,
6508 Local<Value> data = Local<Value>()));
Steve Blocka7e24c12009-10-30 11:49:00 +00006509
6510 /**
6511 * Remove all message listeners from the specified callback function.
6512 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006513 V8_INLINE static V8_DEPRECATED(
6514 "Use isolate version", void RemoveMessageListeners(MessageCallback that));
Steve Blocka7e24c12009-10-30 11:49:00 +00006515
6516 /**
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006517 * Tells V8 to capture current stack trace when uncaught exception occurs
6518 * and report it to the message listeners. The option is off by default.
6519 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006520 V8_INLINE static V8_DEPRECATED(
6521 "Use isolate version",
6522 void SetCaptureStackTraceForUncaughtExceptions(
6523 bool capture, int frame_limit = 10,
6524 StackTrace::StackTraceOptions options = StackTrace::kOverview));
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006525
6526 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006527 * Sets V8 flags from a string.
6528 */
6529 static void SetFlagsFromString(const char* str, int length);
6530
6531 /**
6532 * Sets V8 flags from the command line.
6533 */
6534 static void SetFlagsFromCommandLine(int* argc,
6535 char** argv,
6536 bool remove_flags);
6537
6538 /** Get the version string. */
6539 static const char* GetVersion();
6540
Steve Blocka7e24c12009-10-30 11:49:00 +00006541 /** Callback function for reporting failed access checks.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006542 V8_INLINE static V8_DEPRECATED(
6543 "Use isolate version",
6544 void SetFailedAccessCheckCallbackFunction(FailedAccessCheckCallback));
Steve Blocka7e24c12009-10-30 11:49:00 +00006545
6546 /**
6547 * Enables the host application to receive a notification before a
Steve Block6ded16b2010-05-10 14:33:55 +01006548 * garbage collection. Allocations are not allowed in the
6549 * callback function, you therefore cannot manipulate objects (set
6550 * or delete properties for example) since it is possible such
6551 * operations will result in the allocation of objects. It is possible
6552 * to specify the GCType filter for your callback. But it is not possible to
6553 * register the same callback function two times with different
6554 * GCType filters.
6555 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006556 static V8_DEPRECATED(
6557 "Use isolate version",
6558 void AddGCPrologueCallback(GCCallback callback,
6559 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006560
6561 /**
6562 * This function removes callback which was installed by
6563 * AddGCPrologueCallback function.
6564 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006565 V8_INLINE static V8_DEPRECATED(
6566 "Use isolate version",
6567 void RemoveGCPrologueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006568
6569 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006570 * Enables the host application to receive a notification after a
Steve Block6ded16b2010-05-10 14:33:55 +01006571 * garbage collection. Allocations are not allowed in the
6572 * callback function, you therefore cannot manipulate objects (set
6573 * or delete properties for example) since it is possible such
6574 * operations will result in the allocation of objects. It is possible
6575 * to specify the GCType filter for your callback. But it is not possible to
6576 * register the same callback function two times with different
6577 * GCType filters.
6578 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006579 static V8_DEPRECATED(
6580 "Use isolate version",
6581 void AddGCEpilogueCallback(GCCallback callback,
6582 GCType gc_type_filter = kGCTypeAll));
Steve Block6ded16b2010-05-10 14:33:55 +01006583
6584 /**
6585 * This function removes callback which was installed by
6586 * AddGCEpilogueCallback function.
6587 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006588 V8_INLINE static V8_DEPRECATED(
6589 "Use isolate version",
6590 void RemoveGCEpilogueCallback(GCCallback callback));
Steve Block6ded16b2010-05-10 14:33:55 +01006591
6592 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006593 * Initializes V8. This function needs to be called before the first Isolate
6594 * is created. It always returns true.
Steve Blocka7e24c12009-10-30 11:49:00 +00006595 */
6596 static bool Initialize();
6597
6598 /**
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006599 * Allows the host application to provide a callback which can be used
6600 * as a source of entropy for random number generators.
6601 */
6602 static void SetEntropySource(EntropySource source);
6603
6604 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006605 * Allows the host application to provide a callback that allows v8 to
6606 * cooperate with a profiler that rewrites return addresses on stack.
6607 */
6608 static void SetReturnAddressLocationResolver(
6609 ReturnAddressLocationResolver return_address_resolver);
6610
6611 /**
Steve Block44f0eee2011-05-26 01:26:41 +01006612 * Forcefully terminate the current thread of JavaScript execution
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006613 * in the given isolate.
Steve Blocka7e24c12009-10-30 11:49:00 +00006614 *
6615 * This method can be used by any thread even if that thread has not
6616 * acquired the V8 lock with a Locker object.
Steve Block44f0eee2011-05-26 01:26:41 +01006617 *
6618 * \param isolate The isolate in which to terminate the current JS execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006619 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006620 V8_INLINE static V8_DEPRECATED("Use isolate version",
6621 void TerminateExecution(Isolate* isolate));
Steve Blocka7e24c12009-10-30 11:49:00 +00006622
6623 /**
Steve Block6ded16b2010-05-10 14:33:55 +01006624 * Is V8 terminating JavaScript execution.
6625 *
6626 * Returns true if JavaScript execution is currently terminating
6627 * because of a call to TerminateExecution. In that case there are
6628 * still JavaScript frames on the stack and the termination
6629 * exception is still active.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006630 *
6631 * \param isolate The isolate in which to check.
Steve Block6ded16b2010-05-10 14:33:55 +01006632 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006633 V8_INLINE static V8_DEPRECATED(
6634 "Use isolate version",
6635 bool IsExecutionTerminating(Isolate* isolate = NULL));
Steve Block6ded16b2010-05-10 14:33:55 +01006636
6637 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006638 * Resume execution capability in the given isolate, whose execution
6639 * was previously forcefully terminated using TerminateExecution().
6640 *
6641 * When execution is forcefully terminated using TerminateExecution(),
6642 * the isolate can not resume execution until all JavaScript frames
6643 * have propagated the uncatchable exception which is generated. This
6644 * method allows the program embedding the engine to handle the
6645 * termination event and resume execution capability, even if
6646 * JavaScript frames remain on the stack.
6647 *
6648 * This method can be used by any thread even if that thread has not
6649 * acquired the V8 lock with a Locker object.
6650 *
6651 * \param isolate The isolate in which to resume execution capability.
6652 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006653 V8_INLINE static V8_DEPRECATED(
6654 "Use isolate version", void CancelTerminateExecution(Isolate* isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006655
6656 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006657 * Releases any resources used by v8 and stops any utility threads
6658 * that may be running. Note that disposing v8 is permanent, it
6659 * cannot be reinitialized.
6660 *
6661 * It should generally not be necessary to dispose v8 before exiting
6662 * a process, this should happen automatically. It is only necessary
6663 * to use if the process needs the resources taken up by v8.
6664 */
6665 static bool Dispose();
6666
Steve Block3ce2e202009-11-05 08:53:23 +00006667 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006668 * Iterates through all external resources referenced from current isolate
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006669 * heap. GC is not invoked prior to iterating, therefore there is no
6670 * guarantee that visited objects are still alive.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006671 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006672 V8_INLINE static V8_DEPRECATED(
6673 "Use isolate version",
6674 void VisitExternalResources(ExternalResourceVisitor* visitor));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006675
6676 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006677 * Iterates through all the persistent handles in the current isolate's heap
6678 * that have class_ids.
6679 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006680 V8_INLINE static V8_DEPRECATED(
6681 "Use isolate version",
6682 void VisitHandlesWithClassIds(PersistentHandleVisitor* visitor));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006683
6684 /**
6685 * Iterates through all the persistent handles in isolate's heap that have
6686 * class_ids.
6687 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006688 V8_INLINE static V8_DEPRECATED(
6689 "Use isolate version",
6690 void VisitHandlesWithClassIds(Isolate* isolate,
6691 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006692
6693 /**
6694 * Iterates through all the persistent handles in the current isolate's heap
6695 * that have class_ids and are candidates to be marked as partially dependent
6696 * handles. This will visit handles to young objects created since the last
6697 * garbage collection but is free to visit an arbitrary superset of these
6698 * objects.
6699 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006700 V8_INLINE static V8_DEPRECATED(
6701 "Use isolate version",
6702 void VisitHandlesForPartialDependence(Isolate* isolate,
6703 PersistentHandleVisitor* visitor));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006704
6705 /**
6706 * Initialize the ICU library bundled with V8. The embedder should only
6707 * invoke this method when using the bundled ICU. Returns true on success.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006708 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006709 * If V8 was compiled with the ICU data in an external file, the location
6710 * of the data file has to be provided.
Steve Blocka7e24c12009-10-30 11:49:00 +00006711 */
Ben Murdoch61f157c2016-09-16 13:49:30 +01006712 V8_DEPRECATE_SOON(
6713 "Use version with default location.",
6714 static bool InitializeICU(const char* icu_data_file = nullptr));
6715
6716 /**
6717 * Initialize the ICU library bundled with V8. The embedder should only
6718 * invoke this method when using the bundled ICU. If V8 was compiled with
6719 * the ICU data in an external file and when the default location of that
6720 * file should be used, a path to the executable must be provided.
6721 * Returns true on success.
6722 *
6723 * The default is a file called icudtl.dat side-by-side with the executable.
6724 *
6725 * Optionally, the location of the data file can be provided to override the
6726 * default.
6727 */
6728 static bool InitializeICUDefaultLocation(const char* exec_path,
6729 const char* icu_data_file = nullptr);
Steve Blocka7e24c12009-10-30 11:49:00 +00006730
6731 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006732 * Initialize the external startup data. The embedder only needs to
6733 * invoke this method when external startup data was enabled in a build.
6734 *
6735 * If V8 was compiled with the startup data in an external file, then
6736 * V8 needs to be given those external files during startup. There are
6737 * three ways to do this:
6738 * - InitializeExternalStartupData(const char*)
6739 * This will look in the given directory for files "natives_blob.bin"
6740 * and "snapshot_blob.bin" - which is what the default build calls them.
6741 * - InitializeExternalStartupData(const char*, const char*)
6742 * As above, but will directly use the two given file names.
6743 * - Call SetNativesDataBlob, SetNativesDataBlob.
6744 * This will read the blobs from the given data structures and will
6745 * not perform any file IO.
6746 */
6747 static void InitializeExternalStartupData(const char* directory_path);
6748 static void InitializeExternalStartupData(const char* natives_blob,
6749 const char* snapshot_blob);
6750 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006751 * Sets the v8::Platform to use. This should be invoked before V8 is
6752 * initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +00006753 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006754 static void InitializePlatform(Platform* platform);
Steve Blocka7e24c12009-10-30 11:49:00 +00006755
Steve Block6ded16b2010-05-10 14:33:55 +01006756 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006757 * Clears all references to the v8::Platform. This should be invoked after
6758 * V8 was disposed.
Steve Block6ded16b2010-05-10 14:33:55 +01006759 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006760 static void ShutdownPlatform();
Steve Block6ded16b2010-05-10 14:33:55 +01006761
Steve Blocka7e24c12009-10-30 11:49:00 +00006762 private:
6763 V8();
6764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006765 static internal::Object** GlobalizeReference(internal::Isolate* isolate,
6766 internal::Object** handle);
6767 static internal::Object** CopyPersistent(internal::Object** handle);
Steve Blocka7e24c12009-10-30 11:49:00 +00006768 static void DisposeGlobal(internal::Object** global_handle);
Ben Murdochc5610432016-08-08 18:44:38 +01006769 static void MakeWeak(internal::Object** location, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006770 WeakCallbackInfo<void>::Callback weak_callback,
6771 WeakCallbackType type);
Ben Murdochc5610432016-08-08 18:44:38 +01006772 static void MakeWeak(internal::Object** location, void* data,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006773 // Must be 0 or -1.
6774 int internal_field_index1,
6775 // Must be 1 or -1.
6776 int internal_field_index2,
6777 WeakCallbackInfo<void>::Callback weak_callback);
Ben Murdochc5610432016-08-08 18:44:38 +01006778 static void MakeWeak(internal::Object*** location_addr);
6779 static void* ClearWeak(internal::Object** location);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006780 static void Eternalize(Isolate* isolate,
6781 Value* handle,
6782 int* index);
6783 static Local<Value> GetEternal(Isolate* isolate, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006784
Ben Murdochc5610432016-08-08 18:44:38 +01006785 static void RegisterExternallyReferencedObject(internal::Object** object,
6786 internal::Isolate* isolate);
6787 template <class K, class V, class T>
6788 friend class PersistentValueMapBase;
6789
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006790 static void FromJustIsNothing();
6791 static void ToLocalEmpty();
6792 static void InternalFieldOutOfBounds(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006793 template <class T> friend class Local;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006794 template <class T>
6795 friend class MaybeLocal;
6796 template <class T>
6797 friend class Maybe;
6798 template <class T>
6799 friend class WeakCallbackInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006800 template <class T> friend class Eternal;
6801 template <class T> friend class PersistentBase;
6802 template <class T, class M> friend class Persistent;
Steve Blocka7e24c12009-10-30 11:49:00 +00006803 friend class Context;
6804};
6805
Ben Murdoch61f157c2016-09-16 13:49:30 +01006806/**
6807 * Helper class to create a snapshot data blob.
6808 */
6809class SnapshotCreator {
6810 public:
6811 enum class FunctionCodeHandling { kClear, kKeep };
6812
6813 /**
6814 * Create and enter an isolate, and set it up for serialization.
6815 * The isolate is either created from scratch or from an existing snapshot.
6816 * The caller keeps ownership of the argument snapshot.
6817 * \param existing_blob existing snapshot from which to create this one.
6818 * \param external_references a null-terminated array of external references
6819 * that must be equivalent to CreateParams::external_references.
6820 */
6821 SnapshotCreator(intptr_t* external_references = nullptr,
6822 StartupData* existing_blob = nullptr);
6823
6824 ~SnapshotCreator();
6825
6826 /**
6827 * \returns the isolate prepared by the snapshot creator.
6828 */
6829 Isolate* GetIsolate();
6830
6831 /**
6832 * Add a context to be included in the snapshot blob.
6833 * \returns the index of the context in the snapshot blob.
6834 */
6835 size_t AddContext(Local<Context> context);
6836
6837 /**
6838 * Add a template to be included in the snapshot blob.
6839 * \returns the index of the template in the snapshot blob.
6840 */
6841 size_t AddTemplate(Local<Template> template_obj);
6842
6843 /**
6844 * Created a snapshot data blob.
6845 * This must not be called from within a handle scope.
6846 * \param function_code_handling whether to include compiled function code
6847 * in the snapshot.
6848 * \returns { nullptr, 0 } on failure, and a startup snapshot on success. The
6849 * caller acquires ownership of the data array in the return value.
6850 */
6851 StartupData CreateBlob(FunctionCodeHandling function_code_handling);
6852
6853 private:
6854 void* data_;
6855
6856 // Disallow copying and assigning.
6857 SnapshotCreator(const SnapshotCreator&);
6858 void operator=(const SnapshotCreator&);
6859};
Steve Blocka7e24c12009-10-30 11:49:00 +00006860
6861/**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006862 * A simple Maybe type, representing an object which may or may not have a
6863 * value, see https://hackage.haskell.org/package/base/docs/Data-Maybe.html.
6864 *
6865 * If an API method returns a Maybe<>, the API method can potentially fail
6866 * either because an exception is thrown, or because an exception is pending,
6867 * e.g. because a previous API call threw an exception that hasn't been caught
6868 * yet, or because a TerminateExecution exception was thrown. In that case, a
6869 * "Nothing" value is returned.
6870 */
6871template <class T>
6872class Maybe {
6873 public:
6874 V8_INLINE bool IsNothing() const { return !has_value; }
6875 V8_INLINE bool IsJust() const { return has_value; }
6876
6877 // Will crash if the Maybe<> is nothing.
6878 V8_INLINE T FromJust() const {
6879 if (V8_UNLIKELY(!IsJust())) V8::FromJustIsNothing();
6880 return value;
6881 }
6882
6883 V8_INLINE T FromMaybe(const T& default_value) const {
6884 return has_value ? value : default_value;
6885 }
6886
6887 V8_INLINE bool operator==(const Maybe& other) const {
6888 return (IsJust() == other.IsJust()) &&
6889 (!IsJust() || FromJust() == other.FromJust());
6890 }
6891
6892 V8_INLINE bool operator!=(const Maybe& other) const {
6893 return !operator==(other);
6894 }
6895
6896 private:
6897 Maybe() : has_value(false) {}
6898 explicit Maybe(const T& t) : has_value(true), value(t) {}
6899
6900 bool has_value;
6901 T value;
6902
6903 template <class U>
6904 friend Maybe<U> Nothing();
6905 template <class U>
6906 friend Maybe<U> Just(const U& u);
6907};
6908
6909
6910template <class T>
6911inline Maybe<T> Nothing() {
6912 return Maybe<T>();
6913}
6914
6915
6916template <class T>
6917inline Maybe<T> Just(const T& t) {
6918 return Maybe<T>(t);
6919}
6920
6921
6922/**
Steve Blocka7e24c12009-10-30 11:49:00 +00006923 * An external exception handler.
6924 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006925class V8_EXPORT TryCatch {
Steve Blocka7e24c12009-10-30 11:49:00 +00006926 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006927 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006928 * Creates a new try/catch block and registers it with v8. Note that
6929 * all TryCatch blocks should be stack allocated because the memory
6930 * location itself is compared against JavaScript try/catch blocks.
Steve Blocka7e24c12009-10-30 11:49:00 +00006931 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006932 V8_DEPRECATED("Use isolate version", TryCatch());
Steve Blocka7e24c12009-10-30 11:49:00 +00006933
6934 /**
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006935 * Creates a new try/catch block and registers it with v8. Note that
6936 * all TryCatch blocks should be stack allocated because the memory
6937 * location itself is compared against JavaScript try/catch blocks.
6938 */
6939 TryCatch(Isolate* isolate);
6940
6941 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006942 * Unregisters and deletes this try/catch block.
6943 */
6944 ~TryCatch();
6945
6946 /**
6947 * Returns true if an exception has been caught by this try/catch block.
6948 */
6949 bool HasCaught() const;
6950
6951 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006952 * For certain types of exceptions, it makes no sense to continue execution.
Steve Blocka7e24c12009-10-30 11:49:00 +00006953 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006954 * If CanContinue returns false, the correct action is to perform any C++
6955 * cleanup needed and then return. If CanContinue returns false and
6956 * HasTerminated returns true, it is possible to call
6957 * CancelTerminateExecution in order to continue calling into the engine.
Steve Blocka7e24c12009-10-30 11:49:00 +00006958 */
6959 bool CanContinue() const;
6960
6961 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006962 * Returns true if an exception has been caught due to script execution
6963 * being terminated.
6964 *
6965 * There is no JavaScript representation of an execution termination
6966 * exception. Such exceptions are thrown when the TerminateExecution
6967 * methods are called to terminate a long-running script.
6968 *
6969 * If such an exception has been thrown, HasTerminated will return true,
6970 * indicating that it is possible to call CancelTerminateExecution in order
6971 * to continue calling into the engine.
6972 */
6973 bool HasTerminated() const;
6974
6975 /**
Steve Blockd0582a62009-12-15 09:54:21 +00006976 * Throws the exception caught by this TryCatch in a way that avoids
6977 * it being caught again by this same TryCatch. As with ThrowException
6978 * it is illegal to execute any JavaScript operations after calling
6979 * ReThrow; the caller must return immediately to where the exception
6980 * is caught.
6981 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006982 Local<Value> ReThrow();
Steve Blockd0582a62009-12-15 09:54:21 +00006983
6984 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00006985 * Returns the exception caught by this try/catch block. If no exception has
6986 * been caught an empty handle is returned.
6987 *
6988 * The returned handle is valid until this TryCatch block has been destroyed.
6989 */
6990 Local<Value> Exception() const;
6991
6992 /**
6993 * Returns the .stack property of the thrown object. If no .stack
6994 * property is present an empty handle is returned.
6995 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006996 V8_DEPRECATE_SOON("Use maybe version.", Local<Value> StackTrace() const);
6997 V8_WARN_UNUSED_RESULT MaybeLocal<Value> StackTrace(
6998 Local<Context> context) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006999
7000 /**
7001 * Returns the message associated with this exception. If there is
7002 * no message associated an empty handle is returned.
7003 *
7004 * The returned handle is valid until this TryCatch block has been
7005 * destroyed.
7006 */
7007 Local<v8::Message> Message() const;
7008
7009 /**
7010 * Clears any exceptions that may have been caught by this try/catch block.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007011 * After this method has been called, HasCaught() will return false. Cancels
7012 * the scheduled exception if it is caught and ReThrow() is not called before.
Steve Blocka7e24c12009-10-30 11:49:00 +00007013 *
7014 * It is not necessary to clear a try/catch block before using it again; if
7015 * another exception is thrown the previously caught exception will just be
7016 * overwritten. However, it is often a good idea since it makes it easier
7017 * to determine which operation threw a given exception.
7018 */
7019 void Reset();
7020
7021 /**
7022 * Set verbosity of the external exception handler.
7023 *
7024 * By default, exceptions that are caught by an external exception
7025 * handler are not reported. Call SetVerbose with true on an
7026 * external exception handler to have exceptions caught by the
7027 * handler reported as if they were not caught.
7028 */
7029 void SetVerbose(bool value);
7030
7031 /**
7032 * Set whether or not this TryCatch should capture a Message object
7033 * which holds source information about where the exception
7034 * occurred. True by default.
7035 */
7036 void SetCaptureMessage(bool value);
7037
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007038 /**
7039 * There are cases when the raw address of C++ TryCatch object cannot be
7040 * used for comparisons with addresses into the JS stack. The cases are:
7041 * 1) ARM, ARM64 and MIPS simulators which have separate JS stack.
7042 * 2) Address sanitizer allocates local C++ object in the heap when
7043 * UseAfterReturn mode is enabled.
7044 * This method returns address that can be used for comparisons with
7045 * addresses into the JS stack. When neither simulator nor ASAN's
7046 * UseAfterReturn is enabled, then the address returned will be the address
7047 * of the C++ try catch handler itself.
7048 */
7049 static void* JSStackComparableAddress(v8::TryCatch* handler) {
7050 if (handler == NULL) return NULL;
7051 return handler->js_stack_comparable_address_;
7052 }
7053
Steve Blockd0582a62009-12-15 09:54:21 +00007054 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007055 void ResetInternal();
7056
7057 // Make it hard to create heap-allocated TryCatch blocks.
7058 TryCatch(const TryCatch&);
7059 void operator=(const TryCatch&);
7060 void* operator new(size_t size);
7061 void operator delete(void*, size_t);
7062
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007063 v8::internal::Isolate* isolate_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007064 v8::TryCatch* next_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007065 void* exception_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007066 void* message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007067 void* js_stack_comparable_address_;
Steve Blockd0582a62009-12-15 09:54:21 +00007068 bool is_verbose_ : 1;
7069 bool can_continue_ : 1;
7070 bool capture_message_ : 1;
7071 bool rethrow_ : 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007072 bool has_terminated_ : 1;
Steve Blockd0582a62009-12-15 09:54:21 +00007073
Steve Block44f0eee2011-05-26 01:26:41 +01007074 friend class v8::internal::Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +00007075};
7076
7077
Ben Murdoch257744e2011-11-30 15:57:28 +00007078// --- Context ---
Steve Blocka7e24c12009-10-30 11:49:00 +00007079
7080
7081/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007082 * A container for extension names.
Steve Blocka7e24c12009-10-30 11:49:00 +00007083 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007084class V8_EXPORT ExtensionConfiguration {
Steve Blocka7e24c12009-10-30 11:49:00 +00007085 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007086 ExtensionConfiguration() : name_count_(0), names_(NULL) { }
Steve Blocka7e24c12009-10-30 11:49:00 +00007087 ExtensionConfiguration(int name_count, const char* names[])
7088 : name_count_(name_count), names_(names) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007089
7090 const char** begin() const { return &names_[0]; }
7091 const char** end() const { return &names_[name_count_]; }
7092
Steve Blocka7e24c12009-10-30 11:49:00 +00007093 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007094 const int name_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007095 const char** names_;
7096};
7097
7098
7099/**
7100 * A sandboxed execution context with its own set of built-in objects
7101 * and functions.
7102 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007103class V8_EXPORT Context {
Steve Blocka7e24c12009-10-30 11:49:00 +00007104 public:
Steve Block1e0659c2011-05-24 12:43:12 +01007105 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007106 * Returns the global proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01007107 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007108 * Global proxy object is a thin wrapper whose prototype points to actual
7109 * context's global object with the properties like Object, etc. This is done
7110 * that way for security reasons (for more details see
Steve Block1e0659c2011-05-24 12:43:12 +01007111 * https://wiki.mozilla.org/Gecko:SplitWindow).
7112 *
7113 * Please note that changes to global proxy object prototype most probably
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007114 * would break VM---v8 expects only global object as a prototype of global
7115 * proxy object.
Steve Block1e0659c2011-05-24 12:43:12 +01007116 */
Steve Blocka7e24c12009-10-30 11:49:00 +00007117 Local<Object> Global();
7118
7119 /**
7120 * Detaches the global object from its context before
7121 * the global object can be reused to create a new context.
7122 */
7123 void DetachGlobal();
7124
Andrei Popescu74b3c142010-03-29 12:03:09 +01007125 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007126 * Creates a new context and returns a handle to the newly allocated
7127 * context.
Andrei Popescu74b3c142010-03-29 12:03:09 +01007128 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007129 * \param isolate The isolate in which to create the context.
Steve Block9fac8402011-05-12 15:51:54 +01007130 *
7131 * \param extensions An optional extension configuration containing
7132 * the extensions to be installed in the newly created context.
7133 *
7134 * \param global_template An optional object template from which the
7135 * global object for the newly created context will be created.
7136 *
7137 * \param global_object An optional global object to be reused for
7138 * the newly created context. This global object must have been
7139 * created by a previous call to Context::New with the same global
7140 * template. The state of the global object will be completely reset
7141 * and only object identify will remain.
Leon Clarkef7060e22010-06-03 12:02:55 +01007142 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007143 static Local<Context> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007144 Isolate* isolate, ExtensionConfiguration* extensions = NULL,
7145 Local<ObjectTemplate> global_template = Local<ObjectTemplate>(),
Ben Murdoch61f157c2016-09-16 13:49:30 +01007146 Local<Value> global_object = Local<Value>(),
7147 size_t context_snapshot_index = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00007148
Steve Blocka7e24c12009-10-30 11:49:00 +00007149 /**
7150 * Sets the security token for the context. To access an object in
7151 * another context, the security tokens must match.
7152 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007153 void SetSecurityToken(Local<Value> token);
Steve Blocka7e24c12009-10-30 11:49:00 +00007154
7155 /** Restores the security token to the default value. */
7156 void UseDefaultSecurityToken();
7157
7158 /** Returns the security token of this context.*/
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007159 Local<Value> GetSecurityToken();
Steve Blocka7e24c12009-10-30 11:49:00 +00007160
7161 /**
7162 * Enter this context. After entering a context, all code compiled
7163 * and run is compiled and run in this context. If another context
7164 * is already entered, this old context is saved so it can be
7165 * restored when the new context is exited.
7166 */
7167 void Enter();
7168
7169 /**
7170 * Exit this context. Exiting the current context restores the
7171 * context that was in place when entering the current context.
7172 */
7173 void Exit();
7174
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007175 /** Returns an isolate associated with a current context. */
7176 v8::Isolate* GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +00007177
7178 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007179 * The field at kDebugIdIndex is reserved for V8 debugger implementation.
7180 * The value is propagated to the scripts compiled in given Context and
7181 * can be used for filtering scripts.
7182 */
7183 enum EmbedderDataFields { kDebugIdIndex = 0 };
7184
7185 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007186 * Gets the embedder data with the given index, which must have been set by a
7187 * previous call to SetEmbedderData with the same index. Note that index 0
7188 * currently has a special meaning for Chrome's debugger.
Steve Blocka7e24c12009-10-30 11:49:00 +00007189 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007190 V8_INLINE Local<Value> GetEmbedderData(int index);
7191
7192 /**
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007193 * Gets the binding object used by V8 extras. Extra natives get a reference
7194 * to this object and can use it to "export" functionality by adding
7195 * properties. Extra natives can also "import" functionality by accessing
7196 * properties added by the embedder using the V8 API.
7197 */
7198 Local<Object> GetExtrasBindingObject();
7199
7200 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007201 * Sets the embedder data with the given index, growing the data as
7202 * needed. Note that index 0 currently has a special meaning for Chrome's
7203 * debugger.
7204 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007205 void SetEmbedderData(int index, Local<Value> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007206
7207 /**
7208 * Gets a 2-byte-aligned native pointer from the embedder data with the given
7209 * index, which must have bees set by a previous call to
7210 * SetAlignedPointerInEmbedderData with the same index. Note that index 0
7211 * currently has a special meaning for Chrome's debugger.
7212 */
7213 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
7214
7215 /**
7216 * Sets a 2-byte-aligned native pointer in the embedder data with the given
7217 * index, growing the data as needed. Note that index 0 currently has a
7218 * special meaning for Chrome's debugger.
7219 */
7220 void SetAlignedPointerInEmbedderData(int index, void* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00007221
7222 /**
Ben Murdoch257744e2011-11-30 15:57:28 +00007223 * Control whether code generation from strings is allowed. Calling
7224 * this method with false will disable 'eval' and the 'Function'
7225 * constructor for code running in this context. If 'eval' or the
7226 * 'Function' constructor are used an exception will be thrown.
7227 *
7228 * If code generation from strings is not allowed the
7229 * V8::AllowCodeGenerationFromStrings callback will be invoked if
7230 * set before blocking the call to 'eval' or the 'Function'
7231 * constructor. If that callback returns true, the call will be
7232 * allowed, otherwise an exception will be thrown. If no callback is
7233 * set an exception will be thrown.
7234 */
7235 void AllowCodeGenerationFromStrings(bool allow);
7236
7237 /**
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007238 * Returns true if code generation from strings is allowed for the context.
7239 * For more details see AllowCodeGenerationFromStrings(bool) documentation.
7240 */
7241 bool IsCodeGenerationFromStringsAllowed();
7242
7243 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007244 * Sets the error description for the exception that is thrown when
7245 * code generation from strings is not allowed and 'eval' or the 'Function'
7246 * constructor are called.
7247 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007248 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
7249
7250 /**
7251 * Estimate the memory in bytes retained by this context.
7252 */
7253 size_t EstimatedSize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007254
7255 /**
Steve Blocka7e24c12009-10-30 11:49:00 +00007256 * Stack-allocated class which sets the execution context for all
7257 * operations executed within a local scope.
7258 */
Steve Block8defd9f2010-07-08 12:39:36 +01007259 class Scope {
Steve Blocka7e24c12009-10-30 11:49:00 +00007260 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007261 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007262 context_->Enter();
7263 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007264 V8_INLINE ~Scope() { context_->Exit(); }
7265
Steve Blocka7e24c12009-10-30 11:49:00 +00007266 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007267 Local<Context> context_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007268 };
7269
7270 private:
7271 friend class Value;
7272 friend class Script;
7273 friend class Object;
7274 friend class Function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007275
7276 Local<Value> SlowGetEmbedderData(int index);
7277 void* SlowGetAlignedPointerFromEmbedderData(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00007278};
7279
7280
7281/**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007282 * Multiple threads in V8 are allowed, but only one thread at a time is allowed
7283 * to use any given V8 isolate, see the comments in the Isolate class. The
7284 * definition of 'using a V8 isolate' includes accessing handles or holding onto
7285 * object pointers obtained from V8 handles while in the particular V8 isolate.
7286 * It is up to the user of V8 to ensure, perhaps with locking, that this
7287 * constraint is not violated. In addition to any other synchronization
7288 * mechanism that may be used, the v8::Locker and v8::Unlocker classes must be
7289 * used to signal thead switches to V8.
Steve Blocka7e24c12009-10-30 11:49:00 +00007290 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007291 * v8::Locker is a scoped lock object. While it's active, i.e. between its
7292 * construction and destruction, the current thread is allowed to use the locked
7293 * isolate. V8 guarantees that an isolate can be locked by at most one thread at
7294 * any time. In other words, the scope of a v8::Locker is a critical section.
Steve Block44f0eee2011-05-26 01:26:41 +01007295 *
Ben Murdoch257744e2011-11-30 15:57:28 +00007296 * Sample usage:
7297* \code
Steve Blocka7e24c12009-10-30 11:49:00 +00007298 * ...
7299 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007300 * v8::Locker locker(isolate);
7301 * v8::Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007302 * ...
Ben Murdoch257744e2011-11-30 15:57:28 +00007303 * // Code using V8 and isolate goes here.
Steve Blocka7e24c12009-10-30 11:49:00 +00007304 * ...
7305 * } // Destructor called here
7306 * \endcode
7307 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007308 * If you wish to stop using V8 in a thread A you can do this either by
7309 * destroying the v8::Locker object as above or by constructing a v8::Unlocker
7310 * object:
Steve Blocka7e24c12009-10-30 11:49:00 +00007311 *
7312 * \code
7313 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007314 * isolate->Exit();
7315 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007316 * ...
7317 * // Code not using V8 goes here while V8 can run in another thread.
7318 * ...
7319 * } // Destructor called here.
Ben Murdoch257744e2011-11-30 15:57:28 +00007320 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007321 * \endcode
7322 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007323 * The Unlocker object is intended for use in a long-running callback from V8,
7324 * where you want to release the V8 lock for other threads to use.
Steve Blocka7e24c12009-10-30 11:49:00 +00007325 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007326 * The v8::Locker is a recursive lock, i.e. you can lock more than once in a
7327 * given thread. This can be useful if you have code that can be called either
7328 * from code that holds the lock or from code that does not. The Unlocker is
7329 * not recursive so you can not have several Unlockers on the stack at once, and
7330 * you can not use an Unlocker in a thread that is not inside a Locker's scope.
Steve Blocka7e24c12009-10-30 11:49:00 +00007331 *
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007332 * An unlocker will unlock several lockers if it has to and reinstate the
7333 * correct depth of locking on its destruction, e.g.:
Steve Blocka7e24c12009-10-30 11:49:00 +00007334 *
7335 * \code
7336 * // V8 not locked.
7337 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007338 * v8::Locker locker(isolate);
7339 * Isolate::Scope isolate_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007340 * // V8 locked.
7341 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007342 * v8::Locker another_locker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007343 * // V8 still locked (2 levels).
7344 * {
Ben Murdoch257744e2011-11-30 15:57:28 +00007345 * isolate->Exit();
7346 * v8::Unlocker unlocker(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007347 * // V8 not locked.
7348 * }
Ben Murdoch257744e2011-11-30 15:57:28 +00007349 * isolate->Enter();
Steve Blocka7e24c12009-10-30 11:49:00 +00007350 * // V8 locked again (2 levels).
7351 * }
7352 * // V8 still locked (1 level).
7353 * }
7354 * // V8 Now no longer locked.
7355 * \endcode
7356 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007357class V8_EXPORT Unlocker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007358 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007359 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007360 * Initialize Unlocker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007361 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007362 V8_INLINE explicit Unlocker(Isolate* isolate) { Initialize(isolate); }
7363
Steve Blocka7e24c12009-10-30 11:49:00 +00007364 ~Unlocker();
Ben Murdoch257744e2011-11-30 15:57:28 +00007365 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007366 void Initialize(Isolate* isolate);
7367
Ben Murdoch257744e2011-11-30 15:57:28 +00007368 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007369};
7370
7371
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007372class V8_EXPORT Locker {
Steve Blocka7e24c12009-10-30 11:49:00 +00007373 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007374 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007375 * Initialize Locker for a given Isolate.
Ben Murdoch257744e2011-11-30 15:57:28 +00007376 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007377 V8_INLINE explicit Locker(Isolate* isolate) { Initialize(isolate); }
7378
Steve Blocka7e24c12009-10-30 11:49:00 +00007379 ~Locker();
7380
7381 /**
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007382 * Returns whether or not the locker for a given isolate, is locked by the
7383 * current thread.
Steve Blocka7e24c12009-10-30 11:49:00 +00007384 */
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007385 static bool IsLocked(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00007386
7387 /**
7388 * Returns whether v8::Locker is being used by this V8 instance.
7389 */
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007390 static bool IsActive();
Steve Blocka7e24c12009-10-30 11:49:00 +00007391
7392 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007393 void Initialize(Isolate* isolate);
7394
Steve Blocka7e24c12009-10-30 11:49:00 +00007395 bool has_lock_;
7396 bool top_level_;
Ben Murdoch257744e2011-11-30 15:57:28 +00007397 internal::Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00007398
Steve Blocka7e24c12009-10-30 11:49:00 +00007399 // Disallow copying and assigning.
7400 Locker(const Locker&);
7401 void operator=(const Locker&);
7402};
7403
7404
Ben Murdoch257744e2011-11-30 15:57:28 +00007405// --- Implementation ---
Steve Blocka7e24c12009-10-30 11:49:00 +00007406
7407
7408namespace internal {
7409
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007410const int kApiPointerSize = sizeof(void*); // NOLINT
7411const int kApiIntSize = sizeof(int); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007412const int kApiInt64Size = sizeof(int64_t); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00007413
7414// Tag information for HeapObject.
7415const int kHeapObjectTag = 1;
7416const int kHeapObjectTagSize = 2;
7417const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
7418
Steve Blocka7e24c12009-10-30 11:49:00 +00007419// Tag information for Smi.
7420const int kSmiTag = 0;
7421const int kSmiTagSize = 1;
7422const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
7423
Ben Murdochb8e0da22011-05-16 14:20:40 +01007424template <size_t ptr_size> struct SmiTagging;
Steve Block3ce2e202009-11-05 08:53:23 +00007425
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007426template<int kSmiShiftSize>
7427V8_INLINE internal::Object* IntToSmi(int value) {
7428 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
7429 uintptr_t tagged_value =
7430 (static_cast<uintptr_t>(value) << smi_shift_bits) | kSmiTag;
7431 return reinterpret_cast<internal::Object*>(tagged_value);
7432}
7433
Steve Block3ce2e202009-11-05 08:53:23 +00007434// Smi constants for 32-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007435template <> struct SmiTagging<4> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007436 enum { kSmiShiftSize = 0, kSmiValueSize = 31 };
7437 static int SmiShiftSize() { return kSmiShiftSize; }
7438 static int SmiValueSize() { return kSmiValueSize; }
7439 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007440 int shift_bits = kSmiTagSize + kSmiShiftSize;
7441 // Throw away top 32 bits and shift down (requires >> to be sign extending).
7442 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
7443 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007444 V8_INLINE static internal::Object* IntToSmi(int value) {
7445 return internal::IntToSmi<kSmiShiftSize>(value);
7446 }
7447 V8_INLINE static bool IsValidSmi(intptr_t value) {
7448 // To be representable as an tagged small integer, the two
7449 // most-significant bits of 'value' must be either 00 or 11 due to
7450 // sign-extension. To check this we add 01 to the two
7451 // most-significant bits, and check if the most-significant bit is 0
7452 //
7453 // CAUTION: The original code below:
7454 // bool result = ((value + 0x40000000) & 0x80000000) == 0;
7455 // may lead to incorrect results according to the C language spec, and
7456 // in fact doesn't work correctly with gcc4.1.1 in some cases: The
7457 // compiler may produce undefined results in case of signed integer
7458 // overflow. The computation must be done w/ unsigned ints.
7459 return static_cast<uintptr_t>(value + 0x40000000U) < 0x80000000U;
7460 }
Steve Block3ce2e202009-11-05 08:53:23 +00007461};
7462
7463// Smi constants for 64-bit systems.
Ben Murdochb8e0da22011-05-16 14:20:40 +01007464template <> struct SmiTagging<8> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007465 enum { kSmiShiftSize = 31, kSmiValueSize = 32 };
7466 static int SmiShiftSize() { return kSmiShiftSize; }
7467 static int SmiValueSize() { return kSmiValueSize; }
7468 V8_INLINE static int SmiToInt(const internal::Object* value) {
Steve Block3ce2e202009-11-05 08:53:23 +00007469 int shift_bits = kSmiTagSize + kSmiShiftSize;
7470 // Shift down and throw away top 32 bits.
7471 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
7472 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007473 V8_INLINE static internal::Object* IntToSmi(int value) {
7474 return internal::IntToSmi<kSmiShiftSize>(value);
7475 }
7476 V8_INLINE static bool IsValidSmi(intptr_t value) {
7477 // To be representable as a long smi, the value must be a 32-bit integer.
7478 return (value == static_cast<int32_t>(value));
7479 }
Steve Block3ce2e202009-11-05 08:53:23 +00007480};
7481
Ben Murdochb8e0da22011-05-16 14:20:40 +01007482typedef SmiTagging<kApiPointerSize> PlatformSmiTagging;
7483const int kSmiShiftSize = PlatformSmiTagging::kSmiShiftSize;
7484const int kSmiValueSize = PlatformSmiTagging::kSmiValueSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007485V8_INLINE static bool SmiValuesAre31Bits() { return kSmiValueSize == 31; }
7486V8_INLINE static bool SmiValuesAre32Bits() { return kSmiValueSize == 32; }
Steve Blockd0582a62009-12-15 09:54:21 +00007487
Steve Blocka7e24c12009-10-30 11:49:00 +00007488/**
7489 * This class exports constants and functionality from within v8 that
7490 * is necessary to implement inline functions in the v8 api. Don't
7491 * depend on functions and constants defined here.
7492 */
7493class Internals {
7494 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00007495 // These values match non-compiler-dependent values defined within
7496 // the implementation of v8.
7497 static const int kHeapObjectMapOffset = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007498 static const int kMapInstanceTypeAndBitFieldOffset =
7499 1 * kApiPointerSize + kApiIntSize;
7500 static const int kStringResourceOffset = 3 * kApiPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00007501
Ben Murdochc5610432016-08-08 18:44:38 +01007502 static const int kOddballKindOffset = 5 * kApiPointerSize + sizeof(double);
Ben Murdoch257744e2011-11-30 15:57:28 +00007503 static const int kForeignAddressOffset = kApiPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007504 static const int kJSObjectHeaderSize = 3 * kApiPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007505 static const int kFixedArrayHeaderSize = 2 * kApiPointerSize;
7506 static const int kContextHeaderSize = 2 * kApiPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007507 static const int kContextEmbedderDataIndex = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00007508 static const int kFullStringRepresentationMask = 0x07;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007509 static const int kStringEncodingMask = 0x4;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007510 static const int kExternalTwoByteRepresentationTag = 0x02;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007511 static const int kExternalOneByteRepresentationTag = 0x06;
Steve Blocka7e24c12009-10-30 11:49:00 +00007512
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007513 static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize;
Ben Murdoch61f157c2016-09-16 13:49:30 +01007514 static const int kExternalMemoryOffset = 4 * kApiPointerSize;
7515 static const int kExternalMemoryLimitOffset =
7516 kExternalMemoryOffset + kApiInt64Size;
7517 static const int kIsolateRootsOffset = kExternalMemoryLimitOffset +
7518 kApiInt64Size + kApiInt64Size +
7519 kApiPointerSize + kApiPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01007520 static const int kUndefinedValueRootIndex = 4;
7521 static const int kTheHoleValueRootIndex = 5;
7522 static const int kNullValueRootIndex = 6;
7523 static const int kTrueValueRootIndex = 7;
7524 static const int kFalseValueRootIndex = 8;
7525 static const int kEmptyStringRootIndex = 9;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007526
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007527 static const int kNodeClassIdOffset = 1 * kApiPointerSize;
7528 static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007529 static const int kNodeStateMask = 0x7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007530 static const int kNodeStateIsWeakValue = 2;
7531 static const int kNodeStateIsPendingValue = 3;
7532 static const int kNodeStateIsNearDeathValue = 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007533 static const int kNodeIsIndependentShift = 3;
7534 static const int kNodeIsPartiallyDependentShift = 4;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007535 static const int kNodeIsActiveShift = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007536
Ben Murdochc5610432016-08-08 18:44:38 +01007537 static const int kJSObjectType = 0xb7;
7538 static const int kJSApiObjectType = 0xb6;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01007539 static const int kFirstNonstringType = 0x80;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007540 static const int kOddballType = 0x83;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007541 static const int kForeignType = 0x87;
Steve Blocka7e24c12009-10-30 11:49:00 +00007542
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007543 static const int kUndefinedOddballKind = 5;
7544 static const int kNullOddballKind = 3;
7545
7546 static const uint32_t kNumIsolateDataSlots = 4;
7547
7548 V8_EXPORT static void CheckInitializedImpl(v8::Isolate* isolate);
7549 V8_INLINE static void CheckInitialized(v8::Isolate* isolate) {
7550#ifdef V8_ENABLE_CHECKS
7551 CheckInitializedImpl(isolate);
7552#endif
7553 }
7554
7555 V8_INLINE static bool HasHeapObjectTag(const internal::Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007556 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
7557 kHeapObjectTag);
7558 }
7559
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007560 V8_INLINE static int SmiValue(const internal::Object* value) {
Ben Murdochb8e0da22011-05-16 14:20:40 +01007561 return PlatformSmiTagging::SmiToInt(value);
Steve Block3ce2e202009-11-05 08:53:23 +00007562 }
7563
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007564 V8_INLINE static internal::Object* IntToSmi(int value) {
7565 return PlatformSmiTagging::IntToSmi(value);
7566 }
7567
7568 V8_INLINE static bool IsValidSmi(intptr_t value) {
7569 return PlatformSmiTagging::IsValidSmi(value);
7570 }
7571
7572 V8_INLINE static int GetInstanceType(const internal::Object* obj) {
Steve Block3ce2e202009-11-05 08:53:23 +00007573 typedef internal::Object O;
7574 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007575 // Map::InstanceType is defined so that it will always be loaded into
7576 // the LS 8 bits of one 16-bit word, regardless of endianess.
7577 return ReadField<uint16_t>(map, kMapInstanceTypeAndBitFieldOffset) & 0xff;
Steve Block3ce2e202009-11-05 08:53:23 +00007578 }
7579
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007580 V8_INLINE static int GetOddballKind(const internal::Object* obj) {
7581 typedef internal::Object O;
7582 return SmiValue(ReadField<O*>(obj, kOddballKindOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01007583 }
7584
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007585 V8_INLINE static bool IsExternalTwoByteString(int instance_type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00007586 int representation = (instance_type & kFullStringRepresentationMask);
7587 return representation == kExternalTwoByteRepresentationTag;
7588 }
7589
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007590 V8_INLINE static uint8_t GetNodeFlag(internal::Object** obj, int shift) {
7591 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7592 return *addr & static_cast<uint8_t>(1U << shift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007593 }
Steve Block44f0eee2011-05-26 01:26:41 +01007594
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007595 V8_INLINE static void UpdateNodeFlag(internal::Object** obj,
7596 bool value, int shift) {
7597 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7598 uint8_t mask = static_cast<uint8_t>(1U << shift);
7599 *addr = static_cast<uint8_t>((*addr & ~mask) | (value << shift));
7600 }
7601
7602 V8_INLINE static uint8_t GetNodeState(internal::Object** obj) {
7603 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7604 return *addr & kNodeStateMask;
7605 }
7606
7607 V8_INLINE static void UpdateNodeState(internal::Object** obj,
7608 uint8_t value) {
7609 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + kNodeFlagsOffset;
7610 *addr = static_cast<uint8_t>((*addr & ~kNodeStateMask) | value);
7611 }
7612
7613 V8_INLINE static void SetEmbedderData(v8::Isolate* isolate,
7614 uint32_t slot,
7615 void* data) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007616 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) +
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007617 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7618 *reinterpret_cast<void**>(addr) = data;
7619 }
7620
7621 V8_INLINE static void* GetEmbedderData(const v8::Isolate* isolate,
7622 uint32_t slot) {
7623 const uint8_t* addr = reinterpret_cast<const uint8_t*>(isolate) +
7624 kIsolateEmbedderDataOffset + slot * kApiPointerSize;
7625 return *reinterpret_cast<void* const*>(addr);
7626 }
7627
7628 V8_INLINE static internal::Object** GetRoot(v8::Isolate* isolate,
7629 int index) {
7630 uint8_t* addr = reinterpret_cast<uint8_t*>(isolate) + kIsolateRootsOffset;
7631 return reinterpret_cast<internal::Object**>(addr + index * kApiPointerSize);
7632 }
7633
7634 template <typename T>
7635 V8_INLINE static T ReadField(const internal::Object* ptr, int offset) {
7636 const uint8_t* addr =
7637 reinterpret_cast<const uint8_t*>(ptr) + offset - kHeapObjectTag;
7638 return *reinterpret_cast<const T*>(addr);
7639 }
7640
7641 template <typename T>
7642 V8_INLINE static T ReadEmbedderData(const v8::Context* context, int index) {
7643 typedef internal::Object O;
7644 typedef internal::Internals I;
7645 O* ctx = *reinterpret_cast<O* const*>(context);
7646 int embedder_data_offset = I::kContextHeaderSize +
7647 (internal::kApiPointerSize * I::kContextEmbedderDataIndex);
7648 O* embedder_data = I::ReadField<O*>(ctx, embedder_data_offset);
7649 int value_offset =
7650 I::kFixedArrayHeaderSize + (internal::kApiPointerSize * index);
7651 return I::ReadField<T>(embedder_data, value_offset);
7652 }
Steve Blocka7e24c12009-10-30 11:49:00 +00007653};
7654
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08007655} // namespace internal
Steve Blocka7e24c12009-10-30 11:49:00 +00007656
7657
7658template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007659Local<T> Local<T>::New(Isolate* isolate, Local<T> that) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007660 return New(isolate, that.val_);
7661}
7662
7663template <class T>
7664Local<T> Local<T>::New(Isolate* isolate, const PersistentBase<T>& that) {
7665 return New(isolate, that.val_);
7666}
7667
Steve Blocka7e24c12009-10-30 11:49:00 +00007668
7669template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007670Local<T> Local<T>::New(Isolate* isolate, T* that) {
7671 if (that == NULL) return Local<T>();
7672 T* that_ptr = that;
7673 internal::Object** p = reinterpret_cast<internal::Object**>(that_ptr);
7674 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
7675 reinterpret_cast<internal::Isolate*>(isolate), *p)));
7676}
7677
7678
7679template<class T>
7680template<class S>
7681void Eternal<T>::Set(Isolate* isolate, Local<S> handle) {
7682 TYPE_CHECK(T, S);
7683 V8::Eternalize(isolate, reinterpret_cast<Value*>(*handle), &this->index_);
7684}
7685
7686
7687template<class T>
7688Local<T> Eternal<T>::Get(Isolate* isolate) {
7689 return Local<T>(reinterpret_cast<T*>(*V8::GetEternal(isolate, index_)));
Steve Blocka7e24c12009-10-30 11:49:00 +00007690}
7691
7692
7693template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007694Local<T> MaybeLocal<T>::ToLocalChecked() {
7695 if (V8_UNLIKELY(val_ == nullptr)) V8::ToLocalEmpty();
7696 return Local<T>(val_);
7697}
7698
7699
7700template <class T>
7701void* WeakCallbackInfo<T>::GetInternalField(int index) const {
7702#ifdef V8_ENABLE_CHECKS
7703 if (index < 0 || index >= kInternalFieldsInWeakCallback) {
7704 V8::InternalFieldOutOfBounds(index);
7705 }
7706#endif
7707 return internal_fields_[index];
7708}
7709
7710
7711template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007712T* PersistentBase<T>::New(Isolate* isolate, T* that) {
7713 if (that == NULL) return NULL;
7714 internal::Object** p = reinterpret_cast<internal::Object**>(that);
7715 return reinterpret_cast<T*>(
7716 V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate),
7717 p));
7718}
7719
7720
7721template <class T, class M>
7722template <class S, class M2>
7723void Persistent<T, M>::Copy(const Persistent<S, M2>& that) {
7724 TYPE_CHECK(T, S);
7725 this->Reset();
7726 if (that.IsEmpty()) return;
7727 internal::Object** p = reinterpret_cast<internal::Object**>(that.val_);
7728 this->val_ = reinterpret_cast<T*>(V8::CopyPersistent(p));
7729 M::Copy(that, this);
7730}
7731
7732
7733template <class T>
7734bool PersistentBase<T>::IsIndependent() const {
7735 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007736 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007737 return I::GetNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7738 I::kNodeIsIndependentShift);
Steve Blocka7e24c12009-10-30 11:49:00 +00007739}
7740
7741
7742template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007743bool PersistentBase<T>::IsNearDeath() const {
7744 typedef internal::Internals I;
Steve Blocka7e24c12009-10-30 11:49:00 +00007745 if (this->IsEmpty()) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007746 uint8_t node_state =
7747 I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_));
7748 return node_state == I::kNodeStateIsNearDeathValue ||
7749 node_state == I::kNodeStateIsPendingValue;
Steve Blocka7e24c12009-10-30 11:49:00 +00007750}
7751
7752
7753template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007754bool PersistentBase<T>::IsWeak() const {
7755 typedef internal::Internals I;
7756 if (this->IsEmpty()) return false;
7757 return I::GetNodeState(reinterpret_cast<internal::Object**>(this->val_)) ==
7758 I::kNodeStateIsWeakValue;
7759}
7760
7761
7762template <class T>
7763void PersistentBase<T>::Reset() {
Steve Blocka7e24c12009-10-30 11:49:00 +00007764 if (this->IsEmpty()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007765 V8::DisposeGlobal(reinterpret_cast<internal::Object**>(this->val_));
7766 val_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00007767}
7768
7769
7770template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007771template <class S>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007772void PersistentBase<T>::Reset(Isolate* isolate, const Local<S>& other) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007773 TYPE_CHECK(T, S);
7774 Reset();
7775 if (other.IsEmpty()) return;
7776 this->val_ = New(isolate, other.val_);
Steve Blocka7e24c12009-10-30 11:49:00 +00007777}
7778
Steve Blocka7e24c12009-10-30 11:49:00 +00007779
Steve Block44f0eee2011-05-26 01:26:41 +01007780template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007781template <class S>
7782void PersistentBase<T>::Reset(Isolate* isolate,
7783 const PersistentBase<S>& other) {
7784 TYPE_CHECK(T, S);
7785 Reset();
7786 if (other.IsEmpty()) return;
7787 this->val_ = New(isolate, other.val_);
Ben Murdoch257744e2011-11-30 15:57:28 +00007788}
7789
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007790
Ben Murdoch257744e2011-11-30 15:57:28 +00007791template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007792template <typename P>
7793V8_INLINE void PersistentBase<T>::SetWeak(
7794 P* parameter, typename WeakCallbackInfo<P>::Callback callback,
7795 WeakCallbackType type) {
7796 typedef typename WeakCallbackInfo<void>::Callback Callback;
7797 V8::MakeWeak(reinterpret_cast<internal::Object**>(this->val_), parameter,
7798 reinterpret_cast<Callback>(callback), type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007799}
7800
Ben Murdochc5610432016-08-08 18:44:38 +01007801template <class T>
7802void PersistentBase<T>::SetWeak() {
7803 V8::MakeWeak(reinterpret_cast<internal::Object***>(&this->val_));
7804}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007805
7806template <class T>
7807template <typename P>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007808P* PersistentBase<T>::ClearWeak() {
7809 return reinterpret_cast<P*>(
7810 V8::ClearWeak(reinterpret_cast<internal::Object**>(this->val_)));
7811}
7812
Ben Murdochda12d292016-06-02 14:46:10 +01007813template <class T>
Ben Murdochc5610432016-08-08 18:44:38 +01007814void PersistentBase<T>::RegisterExternalReference(Isolate* isolate) const {
Ben Murdochda12d292016-06-02 14:46:10 +01007815 if (IsEmpty()) return;
7816 V8::RegisterExternallyReferencedObject(
7817 reinterpret_cast<internal::Object**>(this->val_),
7818 reinterpret_cast<internal::Isolate*>(isolate));
7819}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007820
7821template <class T>
7822void PersistentBase<T>::MarkIndependent() {
7823 typedef internal::Internals I;
7824 if (this->IsEmpty()) return;
7825 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7826 true,
7827 I::kNodeIsIndependentShift);
7828}
7829
7830
7831template <class T>
7832void PersistentBase<T>::MarkPartiallyDependent() {
7833 typedef internal::Internals I;
7834 if (this->IsEmpty()) return;
7835 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_),
7836 true,
7837 I::kNodeIsPartiallyDependentShift);
7838}
7839
7840
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007841template <class T>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007842void PersistentBase<T>::MarkActive() {
7843 typedef internal::Internals I;
7844 if (this->IsEmpty()) return;
7845 I::UpdateNodeFlag(reinterpret_cast<internal::Object**>(this->val_), true,
7846 I::kNodeIsActiveShift);
7847}
7848
7849
7850template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007851void PersistentBase<T>::SetWrapperClassId(uint16_t class_id) {
7852 typedef internal::Internals I;
7853 if (this->IsEmpty()) return;
7854 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7855 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7856 *reinterpret_cast<uint16_t*>(addr) = class_id;
7857}
7858
7859
7860template <class T>
7861uint16_t PersistentBase<T>::WrapperClassId() const {
7862 typedef internal::Internals I;
7863 if (this->IsEmpty()) return 0;
7864 internal::Object** obj = reinterpret_cast<internal::Object**>(this->val_);
7865 uint8_t* addr = reinterpret_cast<uint8_t*>(obj) + I::kNodeClassIdOffset;
7866 return *reinterpret_cast<uint16_t*>(addr);
7867}
7868
7869
7870template<typename T>
7871ReturnValue<T>::ReturnValue(internal::Object** slot) : value_(slot) {}
7872
7873template<typename T>
7874template<typename S>
7875void ReturnValue<T>::Set(const Persistent<S>& handle) {
7876 TYPE_CHECK(T, S);
7877 if (V8_UNLIKELY(handle.IsEmpty())) {
7878 *value_ = GetDefaultValue();
7879 } else {
7880 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7881 }
7882}
7883
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007884template <typename T>
7885template <typename S>
7886void ReturnValue<T>::Set(const Global<S>& handle) {
7887 TYPE_CHECK(T, S);
7888 if (V8_UNLIKELY(handle.IsEmpty())) {
7889 *value_ = GetDefaultValue();
7890 } else {
7891 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7892 }
7893}
7894
7895template <typename T>
7896template <typename S>
7897void ReturnValue<T>::Set(const Local<S> handle) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007898 TYPE_CHECK(T, S);
7899 if (V8_UNLIKELY(handle.IsEmpty())) {
7900 *value_ = GetDefaultValue();
7901 } else {
7902 *value_ = *reinterpret_cast<internal::Object**>(*handle);
7903 }
7904}
7905
7906template<typename T>
7907void ReturnValue<T>::Set(double i) {
7908 TYPE_CHECK(T, Number);
7909 Set(Number::New(GetIsolate(), i));
7910}
7911
7912template<typename T>
7913void ReturnValue<T>::Set(int32_t i) {
7914 TYPE_CHECK(T, Integer);
7915 typedef internal::Internals I;
7916 if (V8_LIKELY(I::IsValidSmi(i))) {
7917 *value_ = I::IntToSmi(i);
7918 return;
7919 }
7920 Set(Integer::New(GetIsolate(), i));
7921}
7922
7923template<typename T>
7924void ReturnValue<T>::Set(uint32_t i) {
7925 TYPE_CHECK(T, Integer);
7926 // Can't simply use INT32_MAX here for whatever reason.
7927 bool fits_into_int32_t = (i & (1U << 31)) == 0;
7928 if (V8_LIKELY(fits_into_int32_t)) {
7929 Set(static_cast<int32_t>(i));
7930 return;
7931 }
7932 Set(Integer::NewFromUnsigned(GetIsolate(), i));
7933}
7934
7935template<typename T>
7936void ReturnValue<T>::Set(bool value) {
7937 TYPE_CHECK(T, Boolean);
7938 typedef internal::Internals I;
7939 int root_index;
7940 if (value) {
7941 root_index = I::kTrueValueRootIndex;
7942 } else {
7943 root_index = I::kFalseValueRootIndex;
7944 }
7945 *value_ = *I::GetRoot(GetIsolate(), root_index);
7946}
7947
7948template<typename T>
7949void ReturnValue<T>::SetNull() {
7950 TYPE_CHECK(T, Primitive);
7951 typedef internal::Internals I;
7952 *value_ = *I::GetRoot(GetIsolate(), I::kNullValueRootIndex);
7953}
7954
7955template<typename T>
7956void ReturnValue<T>::SetUndefined() {
7957 TYPE_CHECK(T, Primitive);
7958 typedef internal::Internals I;
7959 *value_ = *I::GetRoot(GetIsolate(), I::kUndefinedValueRootIndex);
7960}
7961
7962template<typename T>
7963void ReturnValue<T>::SetEmptyString() {
7964 TYPE_CHECK(T, String);
7965 typedef internal::Internals I;
7966 *value_ = *I::GetRoot(GetIsolate(), I::kEmptyStringRootIndex);
7967}
7968
Ben Murdochda12d292016-06-02 14:46:10 +01007969template <typename T>
7970Isolate* ReturnValue<T>::GetIsolate() const {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007971 // Isolate is always the pointer below the default value on the stack.
7972 return *reinterpret_cast<Isolate**>(&value_[-2]);
7973}
7974
Ben Murdochda12d292016-06-02 14:46:10 +01007975template <typename T>
7976Local<Value> ReturnValue<T>::Get() const {
7977 typedef internal::Internals I;
7978 if (*value_ == *I::GetRoot(GetIsolate(), I::kTheHoleValueRootIndex))
7979 return Local<Value>(*Undefined(GetIsolate()));
7980 return Local<Value>::New(GetIsolate(), reinterpret_cast<Value*>(value_));
7981}
7982
7983template <typename T>
7984template <typename S>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007985void ReturnValue<T>::Set(S* whatever) {
7986 // Uncompilable to prevent inadvertent misuse.
7987 TYPE_CHECK(S*, Primitive);
7988}
7989
7990template<typename T>
7991internal::Object* ReturnValue<T>::GetDefaultValue() {
7992 // Default value is always the pointer below value_ on the stack.
7993 return value_[-1];
7994}
7995
Ben Murdochc5610432016-08-08 18:44:38 +01007996template <typename T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007997FunctionCallbackInfo<T>::FunctionCallbackInfo(internal::Object** implicit_args,
7998 internal::Object** values,
Ben Murdochc5610432016-08-08 18:44:38 +01007999 int length)
8000 : implicit_args_(implicit_args), values_(values), length_(length) {}
Steve Block8defd9f2010-07-08 12:39:36 +01008001
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008002template<typename T>
8003Local<Value> FunctionCallbackInfo<T>::operator[](int i) const {
8004 if (i < 0 || length_ <= i) return Local<Value>(*Undefined(GetIsolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +00008005 return Local<Value>(reinterpret_cast<Value*>(values_ - i));
8006}
8007
8008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008009template<typename T>
8010Local<Function> FunctionCallbackInfo<T>::Callee() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08008011 return Local<Function>(reinterpret_cast<Function*>(
8012 &implicit_args_[kCalleeIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008013}
8014
8015
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008016template<typename T>
8017Local<Object> FunctionCallbackInfo<T>::This() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00008018 return Local<Object>(reinterpret_cast<Object*>(values_ + 1));
8019}
8020
8021
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008022template<typename T>
8023Local<Object> FunctionCallbackInfo<T>::Holder() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08008024 return Local<Object>(reinterpret_cast<Object*>(
8025 &implicit_args_[kHolderIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008026}
8027
Ben Murdochc5610432016-08-08 18:44:38 +01008028template <typename T>
8029Local<Value> FunctionCallbackInfo<T>::NewTarget() const {
8030 return Local<Value>(
8031 reinterpret_cast<Value*>(&implicit_args_[kNewTargetIndex]));
8032}
Steve Blocka7e24c12009-10-30 11:49:00 +00008033
Ben Murdochc5610432016-08-08 18:44:38 +01008034template <typename T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008035Local<Value> FunctionCallbackInfo<T>::Data() const {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08008036 return Local<Value>(reinterpret_cast<Value*>(&implicit_args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008037}
8038
8039
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008040template<typename T>
8041Isolate* FunctionCallbackInfo<T>::GetIsolate() const {
8042 return *reinterpret_cast<Isolate**>(&implicit_args_[kIsolateIndex]);
8043}
8044
8045
8046template<typename T>
8047ReturnValue<T> FunctionCallbackInfo<T>::GetReturnValue() const {
8048 return ReturnValue<T>(&implicit_args_[kReturnValueIndex]);
8049}
8050
8051
8052template<typename T>
8053bool FunctionCallbackInfo<T>::IsConstructCall() const {
Ben Murdochc5610432016-08-08 18:44:38 +01008054 return !NewTarget()->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +00008055}
8056
8057
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008058template<typename T>
8059int FunctionCallbackInfo<T>::Length() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00008060 return length_;
8061}
8062
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008063ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
8064 Local<Integer> resource_line_offset,
8065 Local<Integer> resource_column_offset,
8066 Local<Boolean> resource_is_shared_cross_origin,
8067 Local<Integer> script_id,
8068 Local<Boolean> resource_is_embedder_debug_script,
8069 Local<Value> source_map_url,
8070 Local<Boolean> resource_is_opaque)
8071 : resource_name_(resource_name),
8072 resource_line_offset_(resource_line_offset),
8073 resource_column_offset_(resource_column_offset),
8074 options_(!resource_is_embedder_debug_script.IsEmpty() &&
8075 resource_is_embedder_debug_script->IsTrue(),
8076 !resource_is_shared_cross_origin.IsEmpty() &&
8077 resource_is_shared_cross_origin->IsTrue(),
8078 !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue()),
8079 script_id_(script_id),
8080 source_map_url_(source_map_url) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00008081
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008082Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00008083
8084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008085Local<Integer> ScriptOrigin::ResourceLineOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00008086 return resource_line_offset_;
8087}
8088
8089
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008090Local<Integer> ScriptOrigin::ResourceColumnOffset() const {
Steve Blocka7e24c12009-10-30 11:49:00 +00008091 return resource_column_offset_;
8092}
8093
8094
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008095Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
Steve Blocka7e24c12009-10-30 11:49:00 +00008096
8097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008098Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008099
8100
8101ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
8102 CachedData* data)
8103 : source_string(string),
8104 resource_name(origin.ResourceName()),
8105 resource_line_offset(origin.ResourceLineOffset()),
8106 resource_column_offset(origin.ResourceColumnOffset()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008107 resource_options(origin.Options()),
8108 source_map_url(origin.SourceMapUrl()),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008109 cached_data(data) {}
8110
8111
8112ScriptCompiler::Source::Source(Local<String> string,
8113 CachedData* data)
8114 : source_string(string), cached_data(data) {}
8115
8116
8117ScriptCompiler::Source::~Source() {
8118 delete cached_data;
8119}
8120
8121
8122const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData()
8123 const {
8124 return cached_data;
8125}
8126
8127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008128Local<Boolean> Boolean::New(Isolate* isolate, bool value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008129 return value ? True(isolate) : False(isolate);
8130}
8131
8132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008133void Template::Set(Isolate* isolate, const char* name, v8::Local<Data> value) {
8134 Set(v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
8135 .ToLocalChecked(),
8136 value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008137}
8138
8139
8140Local<Value> Object::GetInternalField(int index) {
8141#ifndef V8_ENABLE_CHECKS
Steve Blocka7e24c12009-10-30 11:49:00 +00008142 typedef internal::Object O;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008143 typedef internal::HeapObject HO;
Steve Blocka7e24c12009-10-30 11:49:00 +00008144 typedef internal::Internals I;
8145 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008146 // Fast path: If the object is a plain JSObject, which is the common case, we
8147 // know where to find the internal fields and can return the value directly.
Ben Murdochc5610432016-08-08 18:44:38 +01008148 auto instance_type = I::GetInstanceType(obj);
8149 if (instance_type == I::kJSObjectType ||
8150 instance_type == I::kJSApiObjectType) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008151 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008152 O* value = I::ReadField<O*>(obj, offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008153 O** result = HandleScope::CreateHandle(reinterpret_cast<HO*>(obj), value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008154 return Local<Value>(reinterpret_cast<Value*>(result));
Steve Blocka7e24c12009-10-30 11:49:00 +00008155 }
Steve Blocka7e24c12009-10-30 11:49:00 +00008156#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008157 return SlowGetInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008158}
8159
8160
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008161void* Object::GetAlignedPointerFromInternalField(int index) {
8162#ifndef V8_ENABLE_CHECKS
Steve Block3ce2e202009-11-05 08:53:23 +00008163 typedef internal::Object O;
8164 typedef internal::Internals I;
Steve Block3ce2e202009-11-05 08:53:23 +00008165 O* obj = *reinterpret_cast<O**>(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008166 // Fast path: If the object is a plain JSObject, which is the common case, we
8167 // know where to find the internal fields and can return the value directly.
Ben Murdochc5610432016-08-08 18:44:38 +01008168 auto instance_type = I::GetInstanceType(obj);
8169 if (V8_LIKELY(instance_type == I::kJSObjectType ||
8170 instance_type == I::kJSApiObjectType)) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08008171 int offset = I::kJSObjectHeaderSize + (internal::kApiPointerSize * index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008172 return I::ReadField<void*>(obj, offset);
Steve Block3ce2e202009-11-05 08:53:23 +00008173 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008174#endif
8175 return SlowGetAlignedPointerFromInternalField(index);
Steve Blocka7e24c12009-10-30 11:49:00 +00008176}
8177
8178
8179String* String::Cast(v8::Value* value) {
8180#ifdef V8_ENABLE_CHECKS
8181 CheckCast(value);
8182#endif
8183 return static_cast<String*>(value);
8184}
8185
8186
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008187Local<String> String::Empty(Isolate* isolate) {
8188 typedef internal::Object* S;
8189 typedef internal::Internals I;
8190 I::CheckInitialized(isolate);
8191 S* slot = I::GetRoot(isolate, I::kEmptyStringRootIndex);
8192 return Local<String>(reinterpret_cast<String*>(slot));
8193}
8194
8195
Steve Blocka7e24c12009-10-30 11:49:00 +00008196String::ExternalStringResource* String::GetExternalStringResource() const {
8197 typedef internal::Object O;
8198 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008199 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008200 String::ExternalStringResource* result;
Steve Block3ce2e202009-11-05 08:53:23 +00008201 if (I::IsExternalTwoByteString(I::GetInstanceType(obj))) {
Steve Blocka7e24c12009-10-30 11:49:00 +00008202 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8203 result = reinterpret_cast<String::ExternalStringResource*>(value);
8204 } else {
8205 result = NULL;
8206 }
8207#ifdef V8_ENABLE_CHECKS
8208 VerifyExternalStringResource(result);
8209#endif
8210 return result;
8211}
8212
8213
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008214String::ExternalStringResourceBase* String::GetExternalStringResourceBase(
8215 String::Encoding* encoding_out) const {
8216 typedef internal::Object O;
8217 typedef internal::Internals I;
8218 O* obj = *reinterpret_cast<O* const*>(this);
8219 int type = I::GetInstanceType(obj) & I::kFullStringRepresentationMask;
8220 *encoding_out = static_cast<Encoding>(type & I::kStringEncodingMask);
8221 ExternalStringResourceBase* resource = NULL;
8222 if (type == I::kExternalOneByteRepresentationTag ||
8223 type == I::kExternalTwoByteRepresentationTag) {
8224 void* value = I::ReadField<void*>(obj, I::kStringResourceOffset);
8225 resource = static_cast<ExternalStringResourceBase*>(value);
8226 }
8227#ifdef V8_ENABLE_CHECKS
8228 VerifyExternalStringResourceBase(resource, *encoding_out);
8229#endif
8230 return resource;
8231}
8232
8233
8234bool Value::IsUndefined() const {
8235#ifdef V8_ENABLE_CHECKS
8236 return FullIsUndefined();
8237#else
8238 return QuickIsUndefined();
8239#endif
8240}
8241
8242bool Value::QuickIsUndefined() const {
8243 typedef internal::Object O;
8244 typedef internal::Internals I;
8245 O* obj = *reinterpret_cast<O* const*>(this);
8246 if (!I::HasHeapObjectTag(obj)) return false;
8247 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8248 return (I::GetOddballKind(obj) == I::kUndefinedOddballKind);
8249}
8250
8251
8252bool Value::IsNull() const {
8253#ifdef V8_ENABLE_CHECKS
8254 return FullIsNull();
8255#else
8256 return QuickIsNull();
8257#endif
8258}
8259
8260bool Value::QuickIsNull() const {
8261 typedef internal::Object O;
8262 typedef internal::Internals I;
8263 O* obj = *reinterpret_cast<O* const*>(this);
8264 if (!I::HasHeapObjectTag(obj)) return false;
8265 if (I::GetInstanceType(obj) != I::kOddballType) return false;
8266 return (I::GetOddballKind(obj) == I::kNullOddballKind);
8267}
8268
8269
Steve Blocka7e24c12009-10-30 11:49:00 +00008270bool Value::IsString() const {
8271#ifdef V8_ENABLE_CHECKS
8272 return FullIsString();
8273#else
8274 return QuickIsString();
8275#endif
8276}
8277
8278bool Value::QuickIsString() const {
8279 typedef internal::Object O;
8280 typedef internal::Internals I;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008281 O* obj = *reinterpret_cast<O* const*>(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00008282 if (!I::HasHeapObjectTag(obj)) return false;
Steve Block3ce2e202009-11-05 08:53:23 +00008283 return (I::GetInstanceType(obj) < I::kFirstNonstringType);
Steve Blocka7e24c12009-10-30 11:49:00 +00008284}
8285
8286
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008287template <class T> Value* Value::Cast(T* value) {
8288 return static_cast<Value*>(value);
8289}
8290
8291
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008292Local<Boolean> Value::ToBoolean() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008293 return ToBoolean(Isolate::GetCurrent()->GetCurrentContext())
8294 .FromMaybe(Local<Boolean>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008295}
8296
8297
8298Local<Number> Value::ToNumber() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008299 return ToNumber(Isolate::GetCurrent()->GetCurrentContext())
8300 .FromMaybe(Local<Number>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008301}
8302
8303
8304Local<String> Value::ToString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008305 return ToString(Isolate::GetCurrent()->GetCurrentContext())
8306 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008307}
8308
8309
8310Local<String> Value::ToDetailString() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008311 return ToDetailString(Isolate::GetCurrent()->GetCurrentContext())
8312 .FromMaybe(Local<String>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008313}
8314
8315
8316Local<Object> Value::ToObject() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008317 return ToObject(Isolate::GetCurrent()->GetCurrentContext())
8318 .FromMaybe(Local<Object>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008319}
8320
8321
8322Local<Integer> Value::ToInteger() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008323 return ToInteger(Isolate::GetCurrent()->GetCurrentContext())
8324 .FromMaybe(Local<Integer>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008325}
8326
8327
8328Local<Uint32> Value::ToUint32() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008329 return ToUint32(Isolate::GetCurrent()->GetCurrentContext())
8330 .FromMaybe(Local<Uint32>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008331}
8332
8333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008334Local<Int32> Value::ToInt32() const {
8335 return ToInt32(Isolate::GetCurrent()->GetCurrentContext())
8336 .FromMaybe(Local<Int32>());
8337}
8338
8339
8340Boolean* Boolean::Cast(v8::Value* value) {
8341#ifdef V8_ENABLE_CHECKS
8342 CheckCast(value);
8343#endif
8344 return static_cast<Boolean*>(value);
8345}
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008346
8347
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008348Name* Name::Cast(v8::Value* value) {
8349#ifdef V8_ENABLE_CHECKS
8350 CheckCast(value);
8351#endif
8352 return static_cast<Name*>(value);
8353}
8354
8355
8356Symbol* Symbol::Cast(v8::Value* value) {
8357#ifdef V8_ENABLE_CHECKS
8358 CheckCast(value);
8359#endif
8360 return static_cast<Symbol*>(value);
8361}
8362
8363
Steve Blocka7e24c12009-10-30 11:49:00 +00008364Number* Number::Cast(v8::Value* value) {
8365#ifdef V8_ENABLE_CHECKS
8366 CheckCast(value);
8367#endif
8368 return static_cast<Number*>(value);
8369}
8370
8371
8372Integer* Integer::Cast(v8::Value* value) {
8373#ifdef V8_ENABLE_CHECKS
8374 CheckCast(value);
8375#endif
8376 return static_cast<Integer*>(value);
8377}
8378
8379
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008380Int32* Int32::Cast(v8::Value* value) {
8381#ifdef V8_ENABLE_CHECKS
8382 CheckCast(value);
8383#endif
8384 return static_cast<Int32*>(value);
8385}
8386
8387
8388Uint32* Uint32::Cast(v8::Value* value) {
8389#ifdef V8_ENABLE_CHECKS
8390 CheckCast(value);
8391#endif
8392 return static_cast<Uint32*>(value);
8393}
8394
8395
Steve Blocka7e24c12009-10-30 11:49:00 +00008396Date* Date::Cast(v8::Value* value) {
8397#ifdef V8_ENABLE_CHECKS
8398 CheckCast(value);
8399#endif
8400 return static_cast<Date*>(value);
8401}
8402
8403
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008404StringObject* StringObject::Cast(v8::Value* value) {
8405#ifdef V8_ENABLE_CHECKS
8406 CheckCast(value);
8407#endif
8408 return static_cast<StringObject*>(value);
8409}
8410
8411
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008412SymbolObject* SymbolObject::Cast(v8::Value* value) {
8413#ifdef V8_ENABLE_CHECKS
8414 CheckCast(value);
8415#endif
8416 return static_cast<SymbolObject*>(value);
8417}
8418
8419
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008420NumberObject* NumberObject::Cast(v8::Value* value) {
8421#ifdef V8_ENABLE_CHECKS
8422 CheckCast(value);
8423#endif
8424 return static_cast<NumberObject*>(value);
8425}
8426
8427
8428BooleanObject* BooleanObject::Cast(v8::Value* value) {
8429#ifdef V8_ENABLE_CHECKS
8430 CheckCast(value);
8431#endif
8432 return static_cast<BooleanObject*>(value);
8433}
8434
8435
Ben Murdochf87a2032010-10-22 12:50:53 +01008436RegExp* RegExp::Cast(v8::Value* value) {
8437#ifdef V8_ENABLE_CHECKS
8438 CheckCast(value);
8439#endif
8440 return static_cast<RegExp*>(value);
8441}
8442
8443
Steve Blocka7e24c12009-10-30 11:49:00 +00008444Object* Object::Cast(v8::Value* value) {
8445#ifdef V8_ENABLE_CHECKS
8446 CheckCast(value);
8447#endif
8448 return static_cast<Object*>(value);
8449}
8450
8451
8452Array* Array::Cast(v8::Value* value) {
8453#ifdef V8_ENABLE_CHECKS
8454 CheckCast(value);
8455#endif
8456 return static_cast<Array*>(value);
8457}
8458
8459
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008460Map* Map::Cast(v8::Value* value) {
8461#ifdef V8_ENABLE_CHECKS
8462 CheckCast(value);
8463#endif
8464 return static_cast<Map*>(value);
8465}
8466
8467
8468Set* Set::Cast(v8::Value* value) {
8469#ifdef V8_ENABLE_CHECKS
8470 CheckCast(value);
8471#endif
8472 return static_cast<Set*>(value);
8473}
8474
8475
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008476Promise* Promise::Cast(v8::Value* value) {
8477#ifdef V8_ENABLE_CHECKS
8478 CheckCast(value);
8479#endif
8480 return static_cast<Promise*>(value);
8481}
8482
8483
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008484Proxy* Proxy::Cast(v8::Value* value) {
8485#ifdef V8_ENABLE_CHECKS
8486 CheckCast(value);
8487#endif
8488 return static_cast<Proxy*>(value);
8489}
8490
8491
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008492Promise::Resolver* Promise::Resolver::Cast(v8::Value* value) {
8493#ifdef V8_ENABLE_CHECKS
8494 CheckCast(value);
8495#endif
8496 return static_cast<Promise::Resolver*>(value);
8497}
8498
8499
8500ArrayBuffer* ArrayBuffer::Cast(v8::Value* value) {
8501#ifdef V8_ENABLE_CHECKS
8502 CheckCast(value);
8503#endif
8504 return static_cast<ArrayBuffer*>(value);
8505}
8506
8507
8508ArrayBufferView* ArrayBufferView::Cast(v8::Value* value) {
8509#ifdef V8_ENABLE_CHECKS
8510 CheckCast(value);
8511#endif
8512 return static_cast<ArrayBufferView*>(value);
8513}
8514
8515
8516TypedArray* TypedArray::Cast(v8::Value* value) {
8517#ifdef V8_ENABLE_CHECKS
8518 CheckCast(value);
8519#endif
8520 return static_cast<TypedArray*>(value);
8521}
8522
8523
8524Uint8Array* Uint8Array::Cast(v8::Value* value) {
8525#ifdef V8_ENABLE_CHECKS
8526 CheckCast(value);
8527#endif
8528 return static_cast<Uint8Array*>(value);
8529}
8530
8531
8532Int8Array* Int8Array::Cast(v8::Value* value) {
8533#ifdef V8_ENABLE_CHECKS
8534 CheckCast(value);
8535#endif
8536 return static_cast<Int8Array*>(value);
8537}
8538
8539
8540Uint16Array* Uint16Array::Cast(v8::Value* value) {
8541#ifdef V8_ENABLE_CHECKS
8542 CheckCast(value);
8543#endif
8544 return static_cast<Uint16Array*>(value);
8545}
8546
8547
8548Int16Array* Int16Array::Cast(v8::Value* value) {
8549#ifdef V8_ENABLE_CHECKS
8550 CheckCast(value);
8551#endif
8552 return static_cast<Int16Array*>(value);
8553}
8554
8555
8556Uint32Array* Uint32Array::Cast(v8::Value* value) {
8557#ifdef V8_ENABLE_CHECKS
8558 CheckCast(value);
8559#endif
8560 return static_cast<Uint32Array*>(value);
8561}
8562
8563
8564Int32Array* Int32Array::Cast(v8::Value* value) {
8565#ifdef V8_ENABLE_CHECKS
8566 CheckCast(value);
8567#endif
8568 return static_cast<Int32Array*>(value);
8569}
8570
8571
8572Float32Array* Float32Array::Cast(v8::Value* value) {
8573#ifdef V8_ENABLE_CHECKS
8574 CheckCast(value);
8575#endif
8576 return static_cast<Float32Array*>(value);
8577}
8578
8579
8580Float64Array* Float64Array::Cast(v8::Value* value) {
8581#ifdef V8_ENABLE_CHECKS
8582 CheckCast(value);
8583#endif
8584 return static_cast<Float64Array*>(value);
8585}
8586
8587
8588Uint8ClampedArray* Uint8ClampedArray::Cast(v8::Value* value) {
8589#ifdef V8_ENABLE_CHECKS
8590 CheckCast(value);
8591#endif
8592 return static_cast<Uint8ClampedArray*>(value);
8593}
8594
8595
8596DataView* DataView::Cast(v8::Value* value) {
8597#ifdef V8_ENABLE_CHECKS
8598 CheckCast(value);
8599#endif
8600 return static_cast<DataView*>(value);
8601}
8602
8603
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008604SharedArrayBuffer* SharedArrayBuffer::Cast(v8::Value* value) {
8605#ifdef V8_ENABLE_CHECKS
8606 CheckCast(value);
8607#endif
8608 return static_cast<SharedArrayBuffer*>(value);
8609}
8610
8611
Steve Blocka7e24c12009-10-30 11:49:00 +00008612Function* Function::Cast(v8::Value* value) {
8613#ifdef V8_ENABLE_CHECKS
8614 CheckCast(value);
8615#endif
8616 return static_cast<Function*>(value);
8617}
8618
8619
8620External* External::Cast(v8::Value* value) {
8621#ifdef V8_ENABLE_CHECKS
8622 CheckCast(value);
8623#endif
8624 return static_cast<External*>(value);
8625}
8626
8627
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008628template<typename T>
8629Isolate* PropertyCallbackInfo<T>::GetIsolate() const {
8630 return *reinterpret_cast<Isolate**>(&args_[kIsolateIndex]);
Steve Blocka7e24c12009-10-30 11:49:00 +00008631}
8632
8633
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008634template<typename T>
8635Local<Value> PropertyCallbackInfo<T>::Data() const {
8636 return Local<Value>(reinterpret_cast<Value*>(&args_[kDataIndex]));
Steve Blocka7e24c12009-10-30 11:49:00 +00008637}
8638
8639
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008640template<typename T>
8641Local<Object> PropertyCallbackInfo<T>::This() const {
8642 return Local<Object>(reinterpret_cast<Object*>(&args_[kThisIndex]));
8643}
8644
8645
8646template<typename T>
8647Local<Object> PropertyCallbackInfo<T>::Holder() const {
8648 return Local<Object>(reinterpret_cast<Object*>(&args_[kHolderIndex]));
8649}
8650
8651
8652template<typename T>
8653ReturnValue<T> PropertyCallbackInfo<T>::GetReturnValue() const {
8654 return ReturnValue<T>(&args_[kReturnValueIndex]);
8655}
8656
Ben Murdoch097c5b22016-05-18 11:27:45 +01008657template <typename T>
8658bool PropertyCallbackInfo<T>::ShouldThrowOnError() const {
8659 typedef internal::Internals I;
8660 return args_[kShouldThrowOnErrorIndex] != I::IntToSmi(0);
8661}
8662
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008663
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008664Local<Primitive> Undefined(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008665 typedef internal::Object* S;
8666 typedef internal::Internals I;
8667 I::CheckInitialized(isolate);
8668 S* slot = I::GetRoot(isolate, I::kUndefinedValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008669 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008670}
8671
8672
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008673Local<Primitive> Null(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008674 typedef internal::Object* S;
8675 typedef internal::Internals I;
8676 I::CheckInitialized(isolate);
8677 S* slot = I::GetRoot(isolate, I::kNullValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008678 return Local<Primitive>(reinterpret_cast<Primitive*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008679}
8680
8681
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008682Local<Boolean> True(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008683 typedef internal::Object* S;
8684 typedef internal::Internals I;
8685 I::CheckInitialized(isolate);
8686 S* slot = I::GetRoot(isolate, I::kTrueValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008687 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008688}
8689
8690
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008691Local<Boolean> False(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008692 typedef internal::Object* S;
8693 typedef internal::Internals I;
8694 I::CheckInitialized(isolate);
8695 S* slot = I::GetRoot(isolate, I::kFalseValueRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008696 return Local<Boolean>(reinterpret_cast<Boolean*>(slot));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008697}
8698
8699
8700void Isolate::SetData(uint32_t slot, void* data) {
8701 typedef internal::Internals I;
8702 I::SetEmbedderData(this, slot, data);
8703}
8704
8705
8706void* Isolate::GetData(uint32_t slot) {
8707 typedef internal::Internals I;
8708 return I::GetEmbedderData(this, slot);
8709}
8710
8711
8712uint32_t Isolate::GetNumberOfDataSlots() {
8713 typedef internal::Internals I;
8714 return I::kNumIsolateDataSlots;
8715}
8716
8717
8718int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
8719 int64_t change_in_bytes) {
8720 typedef internal::Internals I;
Ben Murdoch61f157c2016-09-16 13:49:30 +01008721 int64_t* external_memory = reinterpret_cast<int64_t*>(
8722 reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryOffset);
8723 const int64_t external_memory_limit = *reinterpret_cast<int64_t*>(
8724 reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryLimitOffset);
8725 const int64_t amount = *external_memory + change_in_bytes;
8726 *external_memory = amount;
8727 if (change_in_bytes > 0 && amount > external_memory_limit) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008728 ReportExternalAllocationLimitReached();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008729 }
Ben Murdoch61f157c2016-09-16 13:49:30 +01008730 return *external_memory;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008731}
8732
8733
8734template<typename T>
8735void Isolate::SetObjectGroupId(const Persistent<T>& object,
8736 UniqueId id) {
8737 TYPE_CHECK(Value, T);
8738 SetObjectGroupId(reinterpret_cast<v8::internal::Object**>(object.val_), id);
8739}
8740
8741
8742template<typename T>
8743void Isolate::SetReferenceFromGroup(UniqueId id,
8744 const Persistent<T>& object) {
8745 TYPE_CHECK(Value, T);
8746 SetReferenceFromGroup(id,
8747 reinterpret_cast<v8::internal::Object**>(object.val_));
8748}
8749
8750
8751template<typename T, typename S>
8752void Isolate::SetReference(const Persistent<T>& parent,
8753 const Persistent<S>& child) {
8754 TYPE_CHECK(Object, T);
8755 TYPE_CHECK(Value, S);
8756 SetReference(reinterpret_cast<v8::internal::Object**>(parent.val_),
8757 reinterpret_cast<v8::internal::Object**>(child.val_));
8758}
8759
8760
8761Local<Value> Context::GetEmbedderData(int index) {
8762#ifndef V8_ENABLE_CHECKS
8763 typedef internal::Object O;
8764 typedef internal::HeapObject HO;
8765 typedef internal::Internals I;
8766 HO* context = *reinterpret_cast<HO**>(this);
8767 O** result =
8768 HandleScope::CreateHandle(context, I::ReadEmbedderData<O*>(this, index));
8769 return Local<Value>(reinterpret_cast<Value*>(result));
8770#else
8771 return SlowGetEmbedderData(index);
8772#endif
8773}
8774
8775
8776void* Context::GetAlignedPointerFromEmbedderData(int index) {
8777#ifndef V8_ENABLE_CHECKS
8778 typedef internal::Internals I;
8779 return I::ReadEmbedderData<void*>(this, index);
8780#else
8781 return SlowGetAlignedPointerFromEmbedderData(index);
8782#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00008783}
8784
8785
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008786void V8::SetAllowCodeGenerationFromStringsCallback(
8787 AllowCodeGenerationFromStringsCallback callback) {
8788 Isolate* isolate = Isolate::GetCurrent();
8789 isolate->SetAllowCodeGenerationFromStringsCallback(callback);
8790}
8791
8792
8793bool V8::IsDead() {
8794 Isolate* isolate = Isolate::GetCurrent();
8795 return isolate->IsDead();
8796}
8797
8798
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008799bool V8::AddMessageListener(MessageCallback that, Local<Value> data) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008800 Isolate* isolate = Isolate::GetCurrent();
8801 return isolate->AddMessageListener(that, data);
8802}
8803
8804
8805void V8::RemoveMessageListeners(MessageCallback that) {
8806 Isolate* isolate = Isolate::GetCurrent();
8807 isolate->RemoveMessageListeners(that);
8808}
8809
8810
8811void V8::SetFailedAccessCheckCallbackFunction(
8812 FailedAccessCheckCallback callback) {
8813 Isolate* isolate = Isolate::GetCurrent();
8814 isolate->SetFailedAccessCheckCallbackFunction(callback);
8815}
8816
8817
8818void V8::SetCaptureStackTraceForUncaughtExceptions(
8819 bool capture, int frame_limit, StackTrace::StackTraceOptions options) {
8820 Isolate* isolate = Isolate::GetCurrent();
8821 isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit,
8822 options);
8823}
8824
8825
8826void V8::SetFatalErrorHandler(FatalErrorCallback callback) {
8827 Isolate* isolate = Isolate::GetCurrent();
8828 isolate->SetFatalErrorHandler(callback);
8829}
8830
8831
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008832void V8::RemoveGCPrologueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008833 Isolate* isolate = Isolate::GetCurrent();
8834 isolate->RemoveGCPrologueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008835 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008836}
8837
8838
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008839void V8::RemoveGCEpilogueCallback(GCCallback callback) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008840 Isolate* isolate = Isolate::GetCurrent();
8841 isolate->RemoveGCEpilogueCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008842 reinterpret_cast<v8::Isolate::GCCallback>(callback));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008843}
8844
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008845void V8::TerminateExecution(Isolate* isolate) { isolate->TerminateExecution(); }
8846
8847
8848bool V8::IsExecutionTerminating(Isolate* isolate) {
8849 if (isolate == NULL) {
8850 isolate = Isolate::GetCurrent();
8851 }
8852 return isolate->IsExecutionTerminating();
8853}
8854
8855
8856void V8::CancelTerminateExecution(Isolate* isolate) {
8857 isolate->CancelTerminateExecution();
8858}
8859
8860
8861void V8::VisitExternalResources(ExternalResourceVisitor* visitor) {
8862 Isolate* isolate = Isolate::GetCurrent();
8863 isolate->VisitExternalResources(visitor);
8864}
8865
8866
8867void V8::VisitHandlesWithClassIds(PersistentHandleVisitor* visitor) {
8868 Isolate* isolate = Isolate::GetCurrent();
8869 isolate->VisitHandlesWithClassIds(visitor);
8870}
8871
8872
8873void V8::VisitHandlesWithClassIds(Isolate* isolate,
8874 PersistentHandleVisitor* visitor) {
8875 isolate->VisitHandlesWithClassIds(visitor);
8876}
8877
8878
8879void V8::VisitHandlesForPartialDependence(Isolate* isolate,
8880 PersistentHandleVisitor* visitor) {
8881 isolate->VisitHandlesForPartialDependence(visitor);
8882}
8883
Steve Blocka7e24c12009-10-30 11:49:00 +00008884/**
8885 * \example shell.cc
8886 * A simple shell that takes a list of expressions on the
8887 * command-line and executes them.
8888 */
8889
8890
8891/**
8892 * \example process.cc
8893 */
8894
8895
8896} // namespace v8
8897
8898
Steve Blocka7e24c12009-10-30 11:49:00 +00008899#undef TYPE_CHECK
8900
8901
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008902#endif // INCLUDE_V8_H_